root/lm-sensors/trunk/kernel/chips/adm1026.c @ 2249

Revision 2249, 56.0 KB (checked in by khali, 9 years ago)

Remove use of temporary ID.

Defined addresses as a range.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
3             monitoring
4    Copyright (c) 2002, 2003  Philip Pokorny <ppokorny@penguincomputing.com>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20    CHANGELOG
21
22    2003-03-13   Initial development
23    2003-05-07   First Release.  Includes GPIO fixup and full
24                 functionality.
25    2003-05-18   Minor fixups and tweaks.
26                 Print GPIO config after fixup.
27                 Adjust fan MIN if DIV changes.
28    2003-05-21   Fix printing of FAN/GPIO config
29                 Fix silly bug in fan_div logic
30                 Fix fan_min handling so that 0xff is 0 is 0xff
31    2003-05-25   Fix more silly typos...
32    2003-06-11   Change FAN_xx_REG macros to use different scaling
33                 Most (all?) drivers assume two pulses per rev fans
34                 and the old scaling was producing double the RPM's
35                 Thanks to Jerome Hsiao @ Arima for pointing this out.
36        2004-01-27   Remove use of temporary ID.
37                 Define addresses as a range.
38*/
39
40#include <linux/version.h>
41#include <linux/module.h>
42#include <linux/slab.h>
43#include <linux/proc_fs.h>
44#include <linux/ioport.h>
45#include <linux/sysctl.h>
46#include <asm/errno.h>
47#include <asm/io.h>
48#include <linux/types.h>
49#include <linux/i2c.h>
50#include <linux/i2c-proc.h>
51#include <linux/init.h>
52#include "version.h"
53#include "sensors_vid.h"
54
55/* Addresses to scan */
56static unsigned short normal_i2c[] = { SENSORS_I2C_END };
57static unsigned short normal_i2c_range[] = { 0x2c, 0x2e, SENSORS_I2C_END };
58static unsigned int normal_isa[] = { SENSORS_ISA_END };
59static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
60
61/* Insmod parameters */
62SENSORS_INSMOD_1(adm1026);
63
64static int gpio_input[17]  = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
65                                -1, -1, -1, -1, -1, -1, -1, -1 };
66static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
67                                -1, -1, -1, -1, -1, -1, -1, -1 };
68static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
69                                -1, -1, -1, -1, -1, -1, -1, -1 };
70static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
71                                -1, -1, -1, -1, -1, -1, -1, -1 };
72static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
73MODULE_PARM(gpio_input,"1-17i");
74MODULE_PARM_DESC(gpio_input,"List of GPIO pins (0-16) to program as inputs");
75MODULE_PARM(gpio_output,"1-17i");
76MODULE_PARM_DESC(gpio_output,"List of GPIO pins (0-16) to program as outputs");
77MODULE_PARM(gpio_inverted,"1-17i");
78MODULE_PARM_DESC(gpio_inverted,"List of GPIO pins (0-16) to program as inverted");
79MODULE_PARM(gpio_normal,"1-17i");
80MODULE_PARM_DESC(gpio_normal,"List of GPIO pins (0-16) to program as normal/non-inverted");
81MODULE_PARM(gpio_fan,"1-8i");
82MODULE_PARM_DESC(gpio_fan,"List of GPIO pins (0-7) to program as fan tachs");
83
84/* Many ADM1026 constants specified below */
85
86/* The ADM1026 registers */
87#define ADM1026_REG_CONFIG1  (0x00)
88#define CFG1_MONITOR     (0x01)
89#define CFG1_INT_ENABLE  (0x02)
90#define CFG1_INT_CLEAR   (0x04)
91#define CFG1_AIN8_9      (0x08)
92#define CFG1_THERM_HOT   (0x10)
93#define CFG1_DAC_AFC     (0x20)
94#define CFG1_PWM_AFC     (0x40)
95#define CFG1_RESET       (0x80)
96#define ADM1026_REG_CONFIG2  (0x01)
97/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
98#define ADM1026_REG_CONFIG3  (0x07)
99#define CFG3_GPIO16_ENABLE  (0x01)
100#define CFG3_CI_CLEAR  (0x02)
101#define CFG3_VREF_250  (0x04)
102#define CFG3_GPIO16_DIR  (0x40)
103#define CFG3_GPIO16_POL  (0x80)
104#define ADM1026_REG_E2CONFIG  (0x13)
105#define E2CFG_READ  (0x01)
106#define E2CFG_WRITE  (0x02)
107#define E2CFG_ERASE  (0x04)
108#define E2CFG_ROM  (0x08)
109#define E2CFG_CLK_EXT  (0x80)
110
111/* There are 10 general analog inputs and 7 dedicated inputs
112 * They are:
113 *    0 - 9  =  AIN0 - AIN9
114 *       10  =  Vbat
115 *       11  =  3.3V Standby
116 *       12  =  3.3V Main
117 *       13  =  +5V
118 *       14  =  Vccp (CPU core voltage)
119 *       15  =  +12V
120 *       16  =  -12V
121 */
122static u16 REG_IN[] = {
123                0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
124                0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
125                0x2b, 0x2c, 0x2d, 0x2e, 0x2f
126        };
127static u16 REG_IN_MIN[] = {
128                0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
129                0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
130                0x4b, 0x4c, 0x4d, 0x4e, 0x4f
131        };
132static u16 REG_IN_MAX[] = {
133                0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
134                0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
135                0x43, 0x44, 0x45, 0x46, 0x47
136        };
137#define ADM1026_REG_IN(nr) (REG_IN[(nr)])
138#define ADM1026_REG_IN_MIN(nr) (REG_IN_MIN[(nr)])
139#define ADM1026_REG_IN_MAX(nr) (REG_IN_MAX[(nr)])
140
141/* Temperatures are:
142 *    0 - Internal
143 *    1 - External 1
144 *    2 - External 2
145 */
146static u16 REG_TEMP[] = { 0x1f, 0x28, 0x29 };
147static u16 REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
148static u16 REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
149static u16 REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
150static u16 REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
151static u16 REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
152#define ADM1026_REG_TEMP(nr) (REG_TEMP[(nr)])
153#define ADM1026_REG_TEMP_MIN(nr) (REG_TEMP_MIN[(nr)])
154#define ADM1026_REG_TEMP_MAX(nr) (REG_TEMP_MAX[(nr)])
155#define ADM1026_REG_TEMP_TMIN(nr) (REG_TEMP_TMIN[(nr)])
156#define ADM1026_REG_TEMP_THERM(nr) (REG_TEMP_THERM[(nr)])
157#define ADM1026_REG_TEMP_OFFSET(nr) (REG_TEMP_OFFSET[(nr)])
158
159#define ADM1026_REG_FAN(nr) (0x38 + (nr))
160#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
161#define ADM1026_REG_FAN_DIV_0_3 (0x02)
162#define ADM1026_REG_FAN_DIV_4_7 (0x03)
163
164#define ADM1026_REG_DAC  (0x04)
165#define ADM1026_REG_PWM  (0x05)
166
167#define ADM1026_REG_GPIO_CFG_0_3 (0x08)
168#define ADM1026_REG_GPIO_CFG_4_7 (0x09)
169#define ADM1026_REG_GPIO_CFG_8_11 (0x0a)
170#define ADM1026_REG_GPIO_CFG_12_15 (0x0b)
171/* CFG_16 in REG_CFG3 */
172#define ADM1026_REG_GPIO_STATUS_0_7 (0x24)
173#define ADM1026_REG_GPIO_STATUS_8_15 (0x25)
174/* STATUS_16 in REG_STATUS4 */
175#define ADM1026_REG_GPIO_MASK_0_7 (0x1c)
176#define ADM1026_REG_GPIO_MASK_8_15 (0x1d)
177/* MASK_16 in REG_MASK4 */
178
179#define ADM1026_REG_COMPANY 0x16
180#define ADM1026_REG_VERSTEP 0x17
181/* These are the recognized values for the above regs */
182#define ADM1026_COMPANY_ANALOG_DEV 0x41
183#define ADM1026_VERSTEP_GENERIC 0x40
184#define ADM1026_VERSTEP_ADM1026 0x44
185
186#define ADM1026_REG_MASK1 0x18
187#define ADM1026_REG_MASK2 0x19
188#define ADM1026_REG_MASK3 0x1a
189#define ADM1026_REG_MASK4 0x1b
190
191#define ADM1026_REG_STATUS1 0x20
192#define ADM1026_REG_STATUS2 0x21
193#define ADM1026_REG_STATUS3 0x22
194#define ADM1026_REG_STATUS4 0x23
195
196/* Conversions. Rounding and limit checking is only done on the TO_REG
197   variants. Note that you should be a bit careful with which arguments
198   these macros are called: arguments may be evaluated more than once.
199 */
200
201/* IN are scaled acording to built-in resistors.  These are the
202 *   voltages corresponding to 3/4 of full scale (192 or 0xc0)
203 *   NOTE: The -12V input needs an additional factor to account
204 *      for the Vref pullup resistor.
205 *      NEG12_OFFSET = SCALE * Vref / V-192 - Vref
206 *                   = 13875 * 2.50 / 1.875 - 2500
207 *                   = 16000
208 */
209#if 1
210/* The values in this table are based on Table II, page 15 of the
211 *    datasheet.
212 */
213static int adm1026_scaling[] = {  /* .001 Volts */
214                2250, 2250, 2250, 2250, 2250, 2250, 
215                1875, 1875, 1875, 1875, 3000, 3330, 
216                3330, 4995, 2250, 12000, 13875
217        };
218#define NEG12_OFFSET  16000
219#else
220/* The values in this table are based on the resistors in
221 *    Figure 5 on page 16.  But the 3.3V inputs are not in
222 *    the figure and the values for the 5V input are wrong.
223 *    For 5V, I'm guessing that R2 at 55.2k is right, but
224 *    the total resistance should be 1400 or 1449 like the
225 *    other inputs.  Using 1449, gives 4.922V at 192.
226 */
227static int adm1026_scaling[] = {  /* .001 Volts */
228                2249, 2249, 2249, 2249, 2249, 2249, 
229                1875, 1875, 1875, 1875, 3329, 3329, 
230                3329, 4922, 2249, 11969, 13889
231        };
232#define NEG12_OFFSET  16019
233#endif
234
235#define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
236#define INS_TO_REG(n,val)  (SENSORS_LIMIT(SCALE(val,adm1026_scaling[n],192),0,255))
237#if 0   /* If we have extended A/D bits */
238#define INSEXT_FROM_REG(n,val,ext) (SCALE((val)*4 + (ext),192*4,adm1026_scaling[n]))
239#define INS_FROM_REG(n,val) (INSEXT_FROM_REG(n,val,0))
240#else
241#define INS_FROM_REG(n,val) (SCALE(val,192,adm1026_scaling[n]))
242#endif
243
244/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
245 *   and we assume a 2 pulse-per-rev fan tach signal
246 *      22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
247 */
248#define FAN_TO_REG(val,div)  ((val)<=0 ? 0xff : SENSORS_LIMIT(1350000/((val)*(div)),1,254))
249#define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==0xff ? 0 : 1350000/((val)*(div)))
250#define DIV_FROM_REG(val) (1<<(val))
251#define DIV_TO_REG(val) ((val)>=8 ? 3 : (val)>=4 ? 2 : (val)>=2 ? 1 : 0)
252
253/* Temperature is reported in 1 degC increments */
254#define TEMP_TO_REG(val) (SENSORS_LIMIT(val,-127,127))
255#define TEMP_FROM_REG(val) (val)
256#define OFFSET_TO_REG(val) (SENSORS_LIMIT(val,-127,127))
257#define OFFSET_FROM_REG(val) (val)
258
259#define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
260#define PWM_FROM_REG(val) (val)
261
262/* Analog output is a voltage, but it's used like a PWM
263 *   Seems like this should be scaled, but to be consistent
264 *   with other drivers, we do it this way.
265 */
266#define DAC_TO_REG(val) (SENSORS_LIMIT(val,0,255))
267#define DAC_FROM_REG(val) (val)
268
269/* sensors_vid.h defines vid_from_reg() */
270#define VID_FROM_REG(val,vrm) (vid_from_reg(val,vrm))
271
272#define ALARMS_FROM_REG(val) (val)
273
274/* Unlike some other drivers we DO NOT set initial limits.  Use
275 * the config file to set limits.
276 */
277
278/* Typically used with systems using a v9.1 VRM spec ? */
279#define ADM1026_INIT_VRM  91
280#define ADM1026_INIT_VID  -1
281
282/* Chip sampling rates
283 *
284 * Some sensors are not updated more frequently than once per second
285 *    so it doesn't make sense to read them more often than that.
286 *    We cache the results and return the saved data if the driver
287 *    is called again before a second has elapsed.
288 *
289 * Also, there is significant configuration data for this chip
290 *    So, we keep the config data up to date in the cache
291 *    when it is written and only sample it once every 5 *minutes*
292 */
293#define ADM1026_DATA_INTERVAL  (1 * HZ)
294#define ADM1026_CONFIG_INTERVAL  (5 * 60 * HZ)
295
296/* We allow for multiple chips in a single system.
297 *
298 * For each registered ADM1026, we need to keep state information
299 * at client->data. The adm1026_data structure is dynamically
300 * allocated, when a new client structure is allocated. */
301
302struct adm1026_data {
303        struct semaphore lock;
304        int sysctl_id;
305        enum chips type;
306
307        struct semaphore update_lock;
308        int valid;              /* !=0 if following fields are valid */
309        unsigned long last_reading;     /* In jiffies */
310        unsigned long last_config;      /* In jiffies */
311
312        u8 in[17];              /* Register value */
313        u8 in_max[17];          /* Register value */
314        u8 in_min[17];          /* Register value */
315        s8 temp[3];             /* Register value */
316        s8 temp_min[3];         /* Register value */
317        s8 temp_max[3];         /* Register value */
318        s8 temp_tmin[3];        /* Register value */
319        s8 temp_therm[3];       /* Register value */
320        s8 temp_offset[3];      /* Register value */
321        u8 fan[8];              /* Register value */
322        u8 fan_min[8];          /* Register value */
323        u8 fan_div[8];          /* Decoded value */
324        u8 pwm;                 /* Register value */
325        u8 analog_out;          /* Register value */
326        int vid;                /* Decoded value */
327        u8 vrm;                 /* VRM version */
328        long alarms;            /* Register encoding, combined */
329        long alarm_mask;        /* Register encoding, combined */
330        long gpio;              /* Register encoding, combined */
331        long gpio_mask;         /* Register encoding, combined */
332        u8 gpio_config[17];     /* Decoded value */
333        u8 config1;             /* Register value */
334        u8 config2;             /* Register value */
335        u8 config3;             /* Register value */
336};
337
338static int adm1026_attach_adapter(struct i2c_adapter *adapter);
339static int adm1026_detect(struct i2c_adapter *adapter, int address,
340                        unsigned short flags, int kind);
341static int adm1026_detach_client(struct i2c_client *client);
342
343static int adm1026_read_value(struct i2c_client *client, u8 register);
344static int adm1026_write_value(struct i2c_client *client, u8 register, int value);
345static void adm1026_print_gpio(struct i2c_client *client);
346static void adm1026_fixup_gpio(struct i2c_client *client);
347static void adm1026_update_client(struct i2c_client *client);
348static void adm1026_init_client(struct i2c_client *client);
349
350
351static void adm1026_in(struct i2c_client *client, int operation, int ctl_name,
352                        int *nrels_mag, long *results);
353static void adm1026_in16(struct i2c_client *client, int operation, int ctl_name,
354                        int *nrels_mag, long *results);
355static void adm1026_fan(struct i2c_client *client, int operation,
356                        int ctl_name, int *nrels_mag, long *results);
357static void adm1026_fixup_fan_min(struct i2c_client *client,
358                         int fan, int old_div);
359static void adm1026_fan_div(struct i2c_client *client, int operation,
360                        int ctl_name, int *nrels_mag, long *results);
361static void adm1026_temp(struct i2c_client *client, int operation,
362                        int ctl_name, int *nrels_mag, long *results);
363static void adm1026_temp_offset(struct i2c_client *client, int operation,
364                        int ctl_name, int *nrels_mag, long *results);
365static void adm1026_temp_tmin(struct i2c_client *client, int operation,
366                        int ctl_name, int *nrels_mag, long *results);
367static void adm1026_temp_therm(struct i2c_client *client, int operation,
368                        int ctl_name, int *nrels_mag, long *results);
369static void adm1026_vid(struct i2c_client *client, int operation,
370                        int ctl_name, int *nrels_mag, long *results);
371static void adm1026_vrm(struct i2c_client *client, int operation,
372                        int ctl_name, int *nrels_mag, long *results);
373static void adm1026_alarms(struct i2c_client *client, int operation,
374                        int ctl_name, int *nrels_mag, long *results);
375static void adm1026_alarm_mask(struct i2c_client *client, int operation,
376                        int ctl_name, int *nrels_mag, long *results);
377static void adm1026_gpio(struct i2c_client *client, int operation,
378                        int ctl_name, int *nrels_mag, long *results);
379static void adm1026_gpio_mask(struct i2c_client *client, int operation,
380                        int ctl_name, int *nrels_mag, long *results);
381static void adm1026_pwm(struct i2c_client *client, int operation,
382                        int ctl_name, int *nrels_mag, long *results);
383static void adm1026_analog_out(struct i2c_client *client, int operation,
384                        int ctl_name, int *nrels_mag, long *results);
385static void adm1026_afc(struct i2c_client *client, int operation,
386                        int ctl_name, int *nrels_mag, long *results);
387
388static struct i2c_driver adm1026_driver = {
389        .owner          = THIS_MODULE,
390        .name           = "ADM1026 compatible sensor driver",
391        .id             = I2C_DRIVERID_ADM1026,
392        .flags          = I2C_DF_NOTIFY,
393        .attach_adapter = &adm1026_attach_adapter,
394        .detach_client  = &adm1026_detach_client,
395};
396
397/* Unique ID assigned to each ADM1026 detected */
398static int adm1026_id = 0;
399
400/* -- SENSORS SYSCTL START -- */
401#define ADM1026_SYSCTL_FAN0                 1000
402#define ADM1026_SYSCTL_FAN1                 1001
403#define ADM1026_SYSCTL_FAN2                 1002
404#define ADM1026_SYSCTL_FAN3                 1003
405#define ADM1026_SYSCTL_FAN4                 1004
406#define ADM1026_SYSCTL_FAN5                 1005
407#define ADM1026_SYSCTL_FAN6                 1006
408#define ADM1026_SYSCTL_FAN7                 1007
409#define ADM1026_SYSCTL_FAN_DIV              1008
410#define ADM1026_SYSCTL_GPIO                 1009
411#define ADM1026_SYSCTL_GPIO_MASK            1010
412#define ADM1026_SYSCTL_ALARMS               1011
413#define ADM1026_SYSCTL_ALARM_MASK           1012
414#define ADM1026_SYSCTL_IN0                  1013
415#define ADM1026_SYSCTL_IN1                  1014
416#define ADM1026_SYSCTL_IN2                  1015
417#define ADM1026_SYSCTL_IN3                  1016
418#define ADM1026_SYSCTL_IN4                  1017
419#define ADM1026_SYSCTL_IN5                  1018
420#define ADM1026_SYSCTL_IN6                  1019
421#define ADM1026_SYSCTL_IN7                  1020
422#define ADM1026_SYSCTL_IN8                  1021
423#define ADM1026_SYSCTL_IN9                  1022
424#define ADM1026_SYSCTL_IN10                 1023
425#define ADM1026_SYSCTL_IN11                 1024
426#define ADM1026_SYSCTL_IN12                 1025
427#define ADM1026_SYSCTL_IN13                 1026
428#define ADM1026_SYSCTL_IN14                 1027
429#define ADM1026_SYSCTL_IN15                 1028
430#define ADM1026_SYSCTL_IN16                 1029
431#define ADM1026_SYSCTL_PWM                  1030
432#define ADM1026_SYSCTL_ANALOG_OUT           1031
433#define ADM1026_SYSCTL_AFC                  1032
434#define ADM1026_SYSCTL_TEMP1                1033
435#define ADM1026_SYSCTL_TEMP2                1034
436#define ADM1026_SYSCTL_TEMP3                1035
437#define ADM1026_SYSCTL_TEMP_OFFSET1         1036
438#define ADM1026_SYSCTL_TEMP_OFFSET2         1037
439#define ADM1026_SYSCTL_TEMP_OFFSET3         1038
440#define ADM1026_SYSCTL_TEMP_THERM1          1039
441#define ADM1026_SYSCTL_TEMP_THERM2          1040
442#define ADM1026_SYSCTL_TEMP_THERM3          1041
443#define ADM1026_SYSCTL_TEMP_TMIN1           1042
444#define ADM1026_SYSCTL_TEMP_TMIN2           1043
445#define ADM1026_SYSCTL_TEMP_TMIN3           1044
446#define ADM1026_SYSCTL_VID                  1045
447#define ADM1026_SYSCTL_VRM                  1046
448
449#define ADM1026_ALARM_TEMP2   (1L <<  0)
450#define ADM1026_ALARM_TEMP3   (1L <<  1)
451#define ADM1026_ALARM_IN9     (1L <<  1)
452#define ADM1026_ALARM_IN11    (1L <<  2)
453#define ADM1026_ALARM_IN12    (1L <<  3)
454#define ADM1026_ALARM_IN13    (1L <<  4)
455#define ADM1026_ALARM_IN14    (1L <<  5)
456#define ADM1026_ALARM_IN15    (1L <<  6)
457#define ADM1026_ALARM_IN16    (1L <<  7)
458#define ADM1026_ALARM_IN0     (1L <<  8)
459#define ADM1026_ALARM_IN1     (1L <<  9)
460#define ADM1026_ALARM_IN2     (1L << 10)
461#define ADM1026_ALARM_IN3     (1L << 11)
462#define ADM1026_ALARM_IN4     (1L << 12)
463#define ADM1026_ALARM_IN5     (1L << 13)
464#define ADM1026_ALARM_IN6     (1L << 14)
465#define ADM1026_ALARM_IN7     (1L << 15)
466#define ADM1026_ALARM_FAN0    (1L << 16)
467#define ADM1026_ALARM_FAN1    (1L << 17)
468#define ADM1026_ALARM_FAN2    (1L << 18)
469#define ADM1026_ALARM_FAN3    (1L << 19)
470#define ADM1026_ALARM_FAN4    (1L << 20)
471#define ADM1026_ALARM_FAN5    (1L << 21)
472#define ADM1026_ALARM_FAN6    (1L << 22)
473#define ADM1026_ALARM_FAN7    (1L << 23)
474#define ADM1026_ALARM_TEMP1   (1L << 24)
475#define ADM1026_ALARM_IN10    (1L << 25)
476#define ADM1026_ALARM_IN8     (1L << 26)
477#define ADM1026_ALARM_THERM   (1L << 27)
478#define ADM1026_ALARM_AFC_FAN (1L << 28)
479#define ADM1026_ALARM_UNUSED  (1L << 29)
480#define ADM1026_ALARM_CI      (1L << 30)
481/* -- SENSORS SYSCTL END -- */
482
483/* The /proc/sys entries */
484/* These files are created for each detected ADM1026. This is just a template;
485 *    The actual list is built from this and additional per-chip
486 *    custom lists below.  Note the XXX_LEN macros.  These must be
487 *    compile time constants because they will be used to allocate
488 *    space for the final template passed to i2c_register_entry.
489 *    We depend on the ability of GCC to evaluate expressions at
490 *    compile time to turn these expressions into compile time
491 *    constants, but this can generate a warning.
492 */
493static ctl_table adm1026_common[] = {
494        {ADM1026_SYSCTL_IN0, "in0", NULL, 0, 0644, NULL, &i2c_proc_real,
495                &i2c_sysctl_real, NULL, &adm1026_in},
496        {ADM1026_SYSCTL_IN1, "in1", NULL, 0, 0644, NULL, &i2c_proc_real,
497                &i2c_sysctl_real, NULL, &adm1026_in},
498        {ADM1026_SYSCTL_IN2, "in2", NULL, 0, 0644, NULL, &i2c_proc_real,
499                &i2c_sysctl_real, NULL, &adm1026_in},
500        {ADM1026_SYSCTL_IN3, "in3", NULL, 0, 0644, NULL, &i2c_proc_real,
501                &i2c_sysctl_real, NULL, &adm1026_in},
502        {ADM1026_SYSCTL_IN4, "in4", NULL, 0, 0644, NULL, &i2c_proc_real,
503                &i2c_sysctl_real, NULL, &adm1026_in},
504        {ADM1026_SYSCTL_IN5, "in5", NULL, 0, 0644, NULL, &i2c_proc_real,
505                &i2c_sysctl_real, NULL, &adm1026_in},
506        {ADM1026_SYSCTL_IN6, "in6", NULL, 0, 0644, NULL, &i2c_proc_real,
507                &i2c_sysctl_real, NULL, &adm1026_in},
508        {ADM1026_SYSCTL_IN7, "in7", NULL, 0, 0644, NULL, &i2c_proc_real,
509                &i2c_sysctl_real, NULL, &adm1026_in},
510        {ADM1026_SYSCTL_IN8, "in8", NULL, 0, 0644, NULL, &i2c_proc_real,
511                &i2c_sysctl_real, NULL, &adm1026_in},
512        {ADM1026_SYSCTL_IN9, "in9", NULL, 0, 0644, NULL, &i2c_proc_real,
513                &i2c_sysctl_real, NULL, &adm1026_in},
514        {ADM1026_SYSCTL_IN10, "in10", NULL, 0, 0644, NULL, &i2c_proc_real,
515                &i2c_sysctl_real, NULL, &adm1026_in},
516        {ADM1026_SYSCTL_IN11, "in11", NULL, 0, 0644, NULL, &i2c_proc_real,
517                &i2c_sysctl_real, NULL, &adm1026_in},
518        {ADM1026_SYSCTL_IN12, "in12", NULL, 0, 0644, NULL, &i2c_proc_real,
519                &i2c_sysctl_real, NULL, &adm1026_in},
520        {ADM1026_SYSCTL_IN13, "in13", NULL, 0, 0644, NULL, &i2c_proc_real,
521                &i2c_sysctl_real, NULL, &adm1026_in},
522        {ADM1026_SYSCTL_IN14, "in14", NULL, 0, 0644, NULL, &i2c_proc_real,
523                &i2c_sysctl_real, NULL, &adm1026_in},
524        {ADM1026_SYSCTL_IN15, "in15", NULL, 0, 0644, NULL, &i2c_proc_real,
525                &i2c_sysctl_real, NULL, &adm1026_in},
526        {ADM1026_SYSCTL_IN16, "in16", NULL, 0, 0644, NULL, &i2c_proc_real,
527                &i2c_sysctl_real, NULL, &adm1026_in16},
528
529        {ADM1026_SYSCTL_FAN0, "fan0", NULL, 0, 0644, NULL, &i2c_proc_real,
530                &i2c_sysctl_real, NULL, &adm1026_fan},
531        {ADM1026_SYSCTL_FAN1, "fan1", NULL, 0, 0644, NULL, &i2c_proc_real,
532                &i2c_sysctl_real, NULL, &adm1026_fan},
533        {ADM1026_SYSCTL_FAN2, "fan2", NULL, 0, 0644, NULL, &i2c_proc_real,
534                &i2c_sysctl_real, NULL, &adm1026_fan},
535        {ADM1026_SYSCTL_FAN3, "fan3", NULL, 0, 0644, NULL, &i2c_proc_real,
536                &i2c_sysctl_real, NULL, &adm1026_fan},
537        {ADM1026_SYSCTL_FAN4, "fan4", NULL, 0, 0644, NULL, &i2c_proc_real,
538                &i2c_sysctl_real, NULL, &adm1026_fan},
539        {ADM1026_SYSCTL_FAN5, "fan5", NULL, 0, 0644, NULL, &i2c_proc_real,
540                &i2c_sysctl_real, NULL, &adm1026_fan},
541        {ADM1026_SYSCTL_FAN6, "fan6", NULL, 0, 0644, NULL, &i2c_proc_real,
542                &i2c_sysctl_real, NULL, &adm1026_fan},
543        {ADM1026_SYSCTL_FAN7, "fan7", NULL, 0, 0644, NULL, &i2c_proc_real,
544                &i2c_sysctl_real, NULL, &adm1026_fan},
545        {ADM1026_SYSCTL_FAN_DIV, "fan_div", NULL, 0, 0644, NULL,
546                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_fan_div},
547
548        {ADM1026_SYSCTL_TEMP1, "temp1", NULL, 0, 0644, NULL, &i2c_proc_real,
549                &i2c_sysctl_real, NULL, &adm1026_temp},
550        {ADM1026_SYSCTL_TEMP2, "temp2", NULL, 0, 0644, NULL, &i2c_proc_real,
551                &i2c_sysctl_real, NULL, &adm1026_temp},
552        {ADM1026_SYSCTL_TEMP3, "temp3", NULL, 0, 0644, NULL, &i2c_proc_real,
553                &i2c_sysctl_real, NULL, &adm1026_temp},
554        {ADM1026_SYSCTL_TEMP_OFFSET1, "temp1_offset", NULL, 0, 0644, NULL,
555                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_offset},
556        {ADM1026_SYSCTL_TEMP_OFFSET2, "temp2_offset", NULL, 0, 0644, NULL,
557                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_offset},
558        {ADM1026_SYSCTL_TEMP_OFFSET3, "temp3_offset", NULL, 0, 0644, NULL,
559                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_offset},
560        {ADM1026_SYSCTL_TEMP_TMIN1, "temp1_tmin", NULL, 0, 0644, NULL,
561                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_tmin},
562        {ADM1026_SYSCTL_TEMP_TMIN2, "temp2_tmin", NULL, 0, 0644, NULL,
563                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_tmin},
564        {ADM1026_SYSCTL_TEMP_TMIN3, "temp3_tmin", NULL, 0, 0644, NULL,
565                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_tmin},
566        {ADM1026_SYSCTL_TEMP_THERM1, "temp1_therm", NULL, 0, 0644, NULL,
567                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_therm},
568        {ADM1026_SYSCTL_TEMP_THERM2, "temp2_therm", NULL, 0, 0644, NULL,
569                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_therm},
570        {ADM1026_SYSCTL_TEMP_THERM3, "temp3_therm", NULL, 0, 0644, NULL,
571                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_temp_therm},
572
573        {ADM1026_SYSCTL_VID, "vid", NULL, 0, 0644, NULL, &i2c_proc_real,
574                &i2c_sysctl_real, NULL, &adm1026_vid},
575        {ADM1026_SYSCTL_VRM, "vrm", NULL, 0, 0644, NULL, &i2c_proc_real,
576                &i2c_sysctl_real, NULL, &adm1026_vrm},
577
578        {ADM1026_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
579                &i2c_sysctl_real, NULL, &adm1026_alarms},
580        {ADM1026_SYSCTL_ALARM_MASK, "alarm_mask", NULL, 0, 0644, NULL,
581                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_alarm_mask},
582
583        {ADM1026_SYSCTL_GPIO, "gpio", NULL, 0, 0644, NULL, &i2c_proc_real,
584                &i2c_sysctl_real, NULL, &adm1026_gpio},
585        {ADM1026_SYSCTL_GPIO_MASK, "gpio_mask", NULL, 0, 0644, NULL,
586                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_gpio_mask},
587
588        {ADM1026_SYSCTL_PWM, "pwm", NULL, 0, 0644, NULL, &i2c_proc_real,
589                &i2c_sysctl_real, NULL, &adm1026_pwm},
590        {ADM1026_SYSCTL_ANALOG_OUT, "analog_out", NULL, 0, 0644, NULL,
591                &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1026_analog_out},
592        {ADM1026_SYSCTL_AFC, "afc", NULL, 0, 0644, NULL, &i2c_proc_real,
593                &i2c_sysctl_real, NULL, &adm1026_afc},
594
595        {0}
596};
597#define CTLTBL_COMMON (sizeof(adm1026_common)/sizeof(adm1026_common[0]))
598
599#define MAX2(a,b) ((a)>(b)?(a):(b))
600#define MAX3(a,b,c) ((a)>(b)?MAX2((a),(c)):MAX2((b),(c)))
601#define MAX4(a,b,c,d) ((a)>(b)?MAX3((a),(c),(d)):MAX3((b),(c),(d)))
602
603#define CTLTBL_MAX (CTLTBL_COMMON)
604
605/* This function is called when:
606     * the module is loaded
607     * a new adapter is loaded
608 */
609int adm1026_attach_adapter(struct i2c_adapter *adapter)
610{
611        return i2c_detect(adapter, &addr_data, adm1026_detect);
612}
613
614/* This function is called by i2c_detect */
615int adm1026_detect(struct i2c_adapter *adapter, int address,
616                unsigned short flags, int kind)
617{
618        int i;
619        int company, verstep ;
620        struct i2c_client *new_client;
621        struct adm1026_data *data;
622        int err = 0;
623        const char *type_name = "";
624        struct ctl_table template[CTLTBL_MAX] ;
625        struct ctl_table * template_next = template ;
626
627        if (i2c_is_isa_adapter(adapter)) {
628                /* This chip has no ISA interface */
629                goto ERROR0 ;
630        }
631
632        if (!i2c_check_functionality(adapter,
633                                        I2C_FUNC_SMBUS_BYTE_DATA)) {
634                /* We need to be able to do byte I/O */
635                goto ERROR0 ;
636        }
637
638        /* OK. For now, we presume we have a valid client. We now create the
639           client structure, even though we cannot fill it completely yet.
640           But it allows us to access adm1026_{read,write}_value. */
641
642        if (!(new_client = kmalloc((sizeof(struct i2c_client)) +
643                                   sizeof(struct adm1026_data),
644                                   GFP_KERNEL))) {
645                err = -ENOMEM;
646                goto ERROR0;
647        }
648
649        data = (struct adm1026_data *) (new_client + 1);
650        new_client->addr = address;
651        new_client->data = data;
652        new_client->adapter = adapter;
653        new_client->driver = &adm1026_driver;
654        new_client->flags = 0;
655
656        /* Now, we do the remaining detection. */
657
658        company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
659        verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
660
661#ifdef DEBUG
662        printk("adm1026: Detecting device at %d,0x%02x with"
663                " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
664                i2c_adapter_id(new_client->adapter), new_client->addr,
665                company, verstep
666            );
667#endif
668
669        /* If auto-detecting, Determine the chip type. */
670        if (kind <= 0) {
671#ifdef DEBUG
672                printk("adm1026: Autodetecting device at %d,0x%02x ...\n",
673                        i2c_adapter_id(adapter), address );
674#endif
675                if( company == ADM1026_COMPANY_ANALOG_DEV
676                    && verstep == ADM1026_VERSTEP_ADM1026 ) {
677                        kind = adm1026 ;
678                } else if( company == ADM1026_COMPANY_ANALOG_DEV
679                    && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC ) {
680                        printk("adm1026: Unrecgonized stepping 0x%02x"
681                            " Defaulting to ADM1026.\n", verstep );
682                        kind = adm1026 ;
683                } else if( (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC ) {
684                        printk("adm1026: Found version/stepping 0x%02x"
685                            " Assuming generic ADM1026.\n", verstep );
686                        kind = any_chip ;
687                } else {
688#ifdef DEBUG
689                        printk("adm1026: Autodetection failed\n");
690#endif
691                        /* Not an ADM1026 ... */
692                        if( kind == 0 ) {  /* User used force=x,y */
693                            printk("adm1026: Generic ADM1026 Version 6 not"
694                                " found at %d,0x%02x. Try force_adm1026.\n",
695                                i2c_adapter_id(adapter), address );
696                        }
697                        err = 0 ;
698                        goto ERROR1;
699                }
700        }
701
702        /* Fill in the chip specific driver values */
703        switch (kind) {
704        case any_chip :
705                type_name = "adm1026";
706                strcpy(new_client->name, "Generic ADM1026");
707                template_next = template ;  /* None used */
708                break ;
709        case adm1026 :
710                type_name = "adm1026";
711                strcpy(new_client->name, "Analog Devices ADM1026");
712                template_next = template ;
713                break ;
714#if 0
715        /* Example of another adm1026 "compatible" device */
716        case adx1000 :
717                type_name = "adx1000";
718                strcpy(new_client->name, "Compatible ADX1000");
719                memcpy( template, adx_specific, sizeof(adx_specific) );
720                template_next = template + CTLTBL_ADX1000 ;
721                break ;
722#endif
723        default :
724                printk("adm1026: Internal error, invalid kind (%d)!", kind);
725                err = -EFAULT ;
726                goto ERROR1;
727        }
728
729        /* Fill in the remaining client fields */
730        new_client->id = adm1026_id++;
731        printk("adm1026(%d): Assigning ID %d to %s at %d,0x%02x\n",
732                new_client->id, new_client->id, new_client->name,
733                i2c_adapter_id(new_client->adapter),
734                new_client->addr
735            );
736
737        /* Housekeeping values */
738        data->type = kind;
739        data->valid = 0;
740
741        /* Set the VRM version */
742        data->vrm = ADM1026_INIT_VRM ;
743        data->vid = ADM1026_INIT_VID ;
744
745        init_MUTEX(&data->update_lock);
746
747        /* Initialize the ADM1026 chip */
748        adm1026_init_client(new_client);
749
750        /* Tell the I2C layer a new client has arrived */
751        if ((err = i2c_attach_client(new_client)))
752                goto ERROR1;
753
754        /* Finish out the template */
755        memcpy(template_next, adm1026_common, sizeof(adm1026_common));
756
757        /* Register a new directory entry with module sensors */
758        if ((i = i2c_register_entry(new_client,
759                                        type_name,
760                                        template)) < 0) {
761                err = i;
762                goto ERROR2;
763        }
764        data->sysctl_id = i;
765
766        return 0;
767
768        /* Error out and cleanup code */
769    ERROR2:
770        i2c_detach_client(new_client);
771    ERROR1:
772        kfree(new_client);
773    ERROR0:
774        return err;
775}
776
777int adm1026_detach_client(struct i2c_client *client)
778{
779        int err;
780        int id ;
781
782        id = client->id;
783        i2c_deregister_entry(((struct adm1026_data *)(client->data))->sysctl_id);
784
785        if ((err = i2c_detach_client(client))) {
786                printk("adm1026(%d): Client deregistration failed,"
787                        " client not detached.\n", id );
788                return err;
789        }
790
791        kfree(client);
792
793        return 0;
794}
795
796int adm1026_read_value(struct i2c_client *client, u8 reg)
797{
798        int res;
799
800        if( reg < 0x80 ) {
801                /* "RAM" locations */
802                res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
803        } else {
804                /* EEPROM, do nothing */
805                res = 0 ;
806        }
807
808        return res ;
809}
810
811int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
812{
813        int res ;
814
815        if( reg < 0x80 ) {
816                /* "RAM" locations */
817                res = i2c_smbus_write_byte_data(client, reg, value);
818        } else {
819                /* EEPROM, do nothing */
820                res = 0 ;
821        }
822
823        return res ;
824}
825
826/* Called when we have found a new ADM1026. */
827void adm1026_init_client(struct i2c_client *client)
828{
829        int value ;
830        int i;
831        struct adm1026_data *data = client->data;
832
833#ifdef DEBUG
834        printk("adm1026(%d): Initializing device\n", client->id);
835#endif
836
837        /* Read chip config */
838        data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
839        data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
840        data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
841
842        /* Inform user of chip config */
843#ifdef DEBUG
844        printk("adm1026(%d): ADM1026_REG_CONFIG1 is: 0x%02x\n",
845                client->id, data->config1 );
846#endif
847        if( (data->config1 & CFG1_MONITOR) == 0 ) {
848                printk("adm1026(%d): Monitoring not currently enabled.\n",
849                            client->id );
850        }
851        if( data->config1 & CFG1_INT_ENABLE ) {
852                printk("adm1026(%d): SMBALERT interrupts are enabled.\n",
853                            client->id );
854        }
855        if( data->config1 & CFG1_AIN8_9 ) {
856                printk("adm1026(%d): in8 and in9 enabled.  temp3 disabled.\n",
857                            client->id );
858        } else {
859                printk("adm1026(%d): temp3 enabled.  in8 and in9 disabled.\n",
860                            client->id );
861        }
862        if( data->config1 & CFG1_THERM_HOT ) {
863                printk("adm1026(%d): Automatic THERM, PWM, and temp limits enabled.\n",
864                            client->id );
865        }
866
867        value = data->config3 ;
868        if( data->config3 & CFG3_GPIO16_ENABLE ) {
869                printk("adm1026(%d): GPIO16 enabled.  THERM pin disabled.\n",
870                            client->id );
871        } else {
872                printk("adm1026(%d): THERM pin enabled.  GPIO16 disabled.\n",
873                            client->id );
874        }
875        if( data->config3 & CFG3_VREF_250 ) {
876                printk("adm1026(%d): Vref is 2.50 Volts.\n", client->id );
877        } else {
878                printk("adm1026(%d): Vref is 1.82 Volts.\n", client->id );
879        }
880
881        /* Read and pick apart the existing GPIO configuration */
882        value = 0 ;
883        for( i = 0 ; i <= 15 ; ++i ) {
884                if( (i & 0x03) == 0 ) {
885                        value = adm1026_read_value(client,
886                                        ADM1026_REG_GPIO_CFG_0_3 + i/4 );
887                }
888                data->gpio_config[i] = value & 0x03 ;
889                value >>= 2 ;
890        }
891        data->gpio_config[16] = (data->config3 >> 6) & 0x03 ;
892
893        /* ... and then print it */
894        adm1026_print_gpio(client);
895
896        /* If the user asks us to reprogram the GPIO config, then
897         *   do it now.  But only if this is the first ADM1026.
898         */
899        if( client->id == 0
900            && (gpio_input[0] != -1 || gpio_output[0] != -1
901                || gpio_inverted[0] != -1 || gpio_normal[0] != -1
902                || gpio_fan[0] != -1 ) ) {
903                adm1026_fixup_gpio(client);
904        }
905
906        /* WE INTENTIONALLY make no changes to the limits,
907         *   offsets, pwms and fans.  If they were
908         *   configured, we don't want to mess with them.
909         *   If they weren't, the default is generally safe
910         *   and will suffice until 'sensors -s' can be run.
911         */
912
913        /* Start monitoring */
914        value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
915
916        /* Set MONITOR, clear interrupt acknowledge and s/w reset */
917        value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET) ;
918#ifdef DEBUG
919        printk("adm1026(%d): Setting CONFIG to: 0x%02x\n", client->id, value );
920#endif
921        data->config1 = value ;
922        adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
923
924}
925
926void adm1026_print_gpio(struct i2c_client *client)
927{
928        struct adm1026_data *data = client->data;
929        int  i ;
930
931        printk("adm1026(%d): GPIO config is:\nadm1026(%d):",
932                            client->id, client->id );
933        for( i = 0 ; i <= 7 ; ++i ) {
934                if( data->config2 & (1 << i) ) {
935                        printk( " %sGP%s%d",
936                                data->gpio_config[i] & 0x02 ? "" : "!",
937                                data->gpio_config[i] & 0x01 ? "OUT" : "IN",
938                                i );
939                } else {
940                        printk( " FAN%d", i );
941                }
942        }
943        printk( "\nadm1026(%d):", client->id );
944        for( i = 8 ; i <= 15 ; ++i ) {
945                printk( " %sGP%s%d",
946                        data->gpio_config[i] & 0x02 ? "" : "!",
947                        data->gpio_config[i] & 0x01 ? "OUT" : "IN",
948                        i );
949        }
950        if( data->config3 & CFG3_GPIO16_ENABLE ) {
951                printk( " %sGP%s16\n",
952                        data->gpio_config[16] & 0x02 ? "" : "!",
953                        data->gpio_config[16] & 0x01 ? "OUT" : "IN" );
954        } else {
955                /* GPIO16 is THERM */
956                printk( " THERM\n" );
957        }
958}
959
960void adm1026_fixup_gpio(struct i2c_client *client)
961{
962        struct adm1026_data *data = client->data;
963        int  i ;
964        int  value ;
965
966        /* Make the changes requested. */
967        /* We may need to unlock/stop monitoring or soft-reset the
968         *    chip before we can make changes.  This hasn't been
969         *    tested much.  FIXME
970         */
971
972        /* Make outputs */
973        for( i = 0 ; i <= 16 ; ++i ) {
974                if( gpio_output[i] >= 0 && gpio_output[i] <= 16 ) {
975                        data->gpio_config[gpio_output[i]] |= 0x01 ;
976                }
977                /* if GPIO0-7 is output, it isn't a FAN tach */
978                if( gpio_output[i] >= 0 && gpio_output[i] <= 7 ) {
979                        data->config2 |= 1 << gpio_output[i] ;
980                }
981        }
982
983        /* Input overrides output */
984        for( i = 0 ; i <= 16 ; ++i ) {
985                if( gpio_input[i] >= 0 && gpio_input[i] <= 16 ) {
986                        data->gpio_config[gpio_input[i]] &= ~ 0x01 ;
987                }
988                /* if GPIO0-7 is input, it isn't a FAN tach */
989                if( gpio_input[i] >= 0 && gpio_input[i] <= 7 ) {
990                        data->config2 |= 1 << gpio_input[i] ;
991                }
992        }
993
994        /* Inverted  */
995        for( i = 0 ; i <= 16 ; ++i ) {
996                if( gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16 ) {
997                        data->gpio_config[gpio_inverted[i]] &= ~ 0x02 ;
998                }
999        }
1000
1001        /* Normal overrides inverted  */
1002        for( i = 0 ; i <= 16 ; ++i ) {
1003                if( gpio_normal[i] >= 0 && gpio_normal[i] <= 16 ) {
1004                        data->gpio_config[gpio_normal[i]] |= 0x02 ;
1005                }
1006        }
1007
1008        /* Fan overrides input and output */
1009        for( i = 0 ; i <= 7 ; ++i ) {
1010                if( gpio_fan[i] >= 0 && gpio_fan[i] <= 7 ) {
1011                        data->config2 &= ~( 1 << gpio_fan[i] );
1012                }
1013        }
1014
1015        /* Write new configs to registers */
1016        adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
1017        data->config3 = (data->config3 & 0x3f)
1018                        | ((data->gpio_config[16] & 0x03) << 6) ;
1019        adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
1020        for( i = 15, value = 0 ; i >= 0 ; --i ) {
1021                value <<= 2 ;
1022                value |= data->gpio_config[i] & 0x03 ;
1023                if( (i & 0x03) == 0 ) {
1024                        adm1026_write_value(client,
1025                                        ADM1026_REG_GPIO_CFG_0_3 + i/4,
1026                                        value );
1027                        value = 0 ;
1028                }
1029        }
1030
1031        /* Print the new config */
1032        adm1026_print_gpio(client);
1033}
1034
1035void adm1026_update_client(struct i2c_client *client)
1036{
1037        struct adm1026_data *data = client->data;
1038        int i;
1039        long value, alarms, gpio ;
1040
1041        down(&data->update_lock);
1042
1043        if (!data->valid
1044            || (jiffies - data->last_reading > ADM1026_DATA_INTERVAL )) {
1045                /* Things that change quickly */
1046
1047#ifdef DEBUG
1048                printk("adm1026(%d): Reading sensor values\n", client->id);
1049#endif
1050                for (i = 0 ; i <= 16 ; ++i) {
1051                        data->in[i] =
1052                            adm1026_read_value(client, ADM1026_REG_IN(i));
1053                }
1054
1055                for (i = 0 ; i <= 7 ; ++i) {
1056                        data->fan[i] =
1057                            adm1026_read_value(client, ADM1026_REG_FAN(i));
1058                }
1059
1060                for (i = 0 ; i <= 2 ; ++i) {
1061                        /* NOTE: temp[] is s8 and we assume 2's complement
1062                         *   "conversion" in the assignment   */
1063                        data->temp[i] =
1064                            adm1026_read_value(client, ADM1026_REG_TEMP(i));
1065                }
1066
1067                data->pwm = adm1026_read_value(client, ADM1026_REG_PWM);
1068                data->analog_out = adm1026_read_value(client, ADM1026_REG_DAC);
1069
1070                /* GPIO16 is MSbit of alarms, move it to gpio */
1071                alarms  = adm1026_read_value(client, ADM1026_REG_STATUS4);
1072                gpio = alarms & 0x80 ? 0x0100 : 0 ;  /* GPIO16 */
1073                alarms &= 0x7f ;
1074                alarms <<= 8 ;
1075                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
1076                alarms <<= 8 ;
1077                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
1078                alarms <<= 8 ;
1079                alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
1080                data->alarms = alarms ;
1081
1082                /* Read the GPIO values */
1083                gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_STATUS_8_15);
1084                gpio <<= 8 ;
1085                gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_STATUS_0_7);
1086                data->gpio = gpio ;
1087
1088                data->last_reading = jiffies ;
1089        };  /* last_reading */
1090
1091        if (!data->valid
1092            || (jiffies - data->last_config > ADM1026_CONFIG_INTERVAL) ) {
1093                /* Things that don't change often */
1094
1095#ifdef DEBUG
1096                printk("adm1026(%d): Reading config values\n", client->id);
1097#endif
1098                for (i = 0 ; i <= 16 ; ++i) {
1099                        data->in_min[i] =
1100                            adm1026_read_value(client, ADM1026_REG_IN_MIN(i));
1101                        data->in_max[i] =
1102                            adm1026_read_value(client, ADM1026_REG_IN_MAX(i));
1103                }
1104
1105                value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
1106                        | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
1107                for (i = 0 ; i <= 7 ; ++i) {
1108                        data->fan_min[i] =
1109                            adm1026_read_value(client, ADM1026_REG_FAN_MIN(i));
1110                        data->fan_div[i] = DIV_FROM_REG(value & 0x03);
1111                        value >>= 2 ;
1112                }
1113
1114                for (i = 0; i <= 2; ++i) {
1115                        /* NOTE: temp_xxx[] are s8 and we assume 2's complement
1116                         *   "conversion" in the assignment   */
1117                        data->temp_min[i] =
1118                            adm1026_read_value(client, ADM1026_REG_TEMP_MIN(i));
1119                        data->temp_max[i] =
1120                            adm1026_read_value(client, ADM1026_REG_TEMP_MAX(i));
1121                        data->temp_tmin[i] =
1122                            adm1026_read_value(client, ADM1026_REG_TEMP_TMIN(i));
1123                        data->temp_therm[i] =
1124                            adm1026_read_value(client, ADM1026_REG_TEMP_THERM(i));
1125                        data->temp_offset[i] =
1126                            adm1026_read_value(client, ADM1026_REG_TEMP_OFFSET(i));
1127                }
1128
1129                /* Read the STATUS/alarm masks */
1130                alarms  = adm1026_read_value(client, ADM1026_REG_MASK4);
1131                gpio    = alarms & 0x80 ? 0x0100 : 0 ;  /* GPIO16 */
1132                alarms  = (alarms & 0x7f) << 8 ;
1133                alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
1134                alarms <<= 8 ;
1135                alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
1136                alarms <<= 8 ;
1137                alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
1138                data->alarm_mask = alarms ;
1139
1140                /* Read the GPIO values */
1141                gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_8_15);
1142                gpio <<= 8 ;
1143                gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
1144                data->gpio_mask = gpio ;
1145
1146                /* Read the GPIO config */
1147                data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
1148                data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
1149                data->gpio_config[16] = (data->config3 >> 6) & 0x03 ;
1150
1151                value = 0 ;
1152                for( i = 0 ; i <= 15 ; ++i ) {
1153                        if( (i & 0x03) == 0 ) {
1154                                value = adm1026_read_value(client,
1155                                            ADM1026_REG_GPIO_CFG_0_3 + i/4 );
1156                        }
1157                        data->gpio_config[i] = value & 0x03 ;
1158                        value >>= 2 ;
1159                }
1160
1161                data->last_config = jiffies;
1162        };  /* last_config */
1163
1164        /* We don't know where or even _if_ the VID might be on the GPIO
1165         *    pins.  But the datasheet gives an example config showing
1166         *    GPIO11-15 being used to monitor VID0-4, so we go with that
1167         *    but make the vid WRITEABLE so if it's wrong, the user can
1168         *    set it in /etc/sensors.conf perhaps using an expression or
1169         *    0 to trigger a re-read from the GPIO pins.
1170         */
1171        if( data->vid == ADM1026_INIT_VID ) {
1172                /* Hasn't been set yet, make a bold assumption */
1173                printk("adm1026(%d): Setting VID from GPIO11-15.\n",
1174                            client->id );
1175                data->vid = (data->gpio >> 11) & 0x1f ;
1176        }
1177       
1178        data->valid = 1;
1179
1180        up(&data->update_lock);
1181}
1182
1183
1184/* The following functions are the call-back functions of the /proc/sys and
1185   sysctl files.  The appropriate function is referenced in the ctl_table
1186   extra1 field.
1187
1188   Each function must return the magnitude (power of 10 to divide the
1189   data with) if it is called with operation set to SENSORS_PROC_REAL_INFO.
1190   It must put a maximum of *nrels elements in results reflecting the
1191   data of this file, and set *nrels to the number it actually put in
1192   it, if operation is SENSORS_PROC_REAL_READ.  Finally, it must get
1193   up to *nrels elements from results and write them to the chip, if
1194   operations is SENSORS_PROC_REAL_WRITE.
1195 */
1196void adm1026_in(struct i2c_client *client, int operation, int ctl_name,
1197             int *nrels_mag, long *results)
1198{
1199        struct adm1026_data *data = client->data;
1200        int nr = ctl_name - ADM1026_SYSCTL_IN0;
1201
1202        /* We handle in0 - in15 here.  in16 (-12V) is handled below */
1203        if (nr < 0 || nr > 15)
1204                return ;  /* ERROR */
1205
1206        if (operation == SENSORS_PROC_REAL_INFO)
1207                *nrels_mag = 3;  /* 1.000 */
1208        else if (operation == SENSORS_PROC_REAL_READ) {
1209                adm1026_update_client(client);
1210                results[0] = INS_FROM_REG(nr,data->in_min[nr]);
1211                results[1] = INS_FROM_REG(nr,data->in_max[nr]);
1212                results[2] = INS_FROM_REG(nr,data->in[nr]);
1213                *nrels_mag = 3;
1214        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1215                down(&data->update_lock);
1216                if (*nrels_mag > 1) {
1217                        data->in_max[nr] = INS_TO_REG(nr,results[1]);
1218                        adm1026_write_value(client, ADM1026_REG_IN_MAX(nr),
1219                                         data->in_max[nr]);
1220                }
1221                if (*nrels_mag > 0) {
1222                        data->in_min[nr] = INS_TO_REG(nr,results[0]);
1223                        adm1026_write_value(client, ADM1026_REG_IN_MIN(nr),
1224                                         data->in_min[nr]);
1225                }
1226                up(&data->update_lock);
1227        }
1228}
1229
1230void adm1026_in16(struct i2c_client *client, int operation, int ctl_name,
1231             int *nrels_mag, long *results)
1232{
1233        struct adm1026_data *data = client->data;
1234        int nr = ctl_name - ADM1026_SYSCTL_IN0;
1235
1236        /* We handle in16 (-12V) here */
1237        if (nr != 16)
1238                return ;  /* ERROR */
1239
1240        /* Apply offset and swap min/max so that min is 90% of
1241         *    target and max is 110% of target.
1242         */
1243
1244        if (operation == SENSORS_PROC_REAL_INFO)
1245                *nrels_mag = 3;  /* 1.000 */
1246        else if (operation == SENSORS_PROC_REAL_READ) {
1247                adm1026_update_client(client);
1248                results[0] = INS_FROM_REG(nr,data->in_max[nr])-NEG12_OFFSET ;
1249                results[1] = INS_FROM_REG(nr,data->in_min[nr])-NEG12_OFFSET ;
1250                results[2] = INS_FROM_REG(nr,data->in[nr])-NEG12_OFFSET ;
1251                *nrels_mag = 3;
1252        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1253                down(&data->update_lock);
1254                if (*nrels_mag > 1) {
1255                        data->in_min[nr] = INS_TO_REG(nr,results[1]+NEG12_OFFSET);
1256                        adm1026_write_value(client, ADM1026_REG_IN_MIN(nr),
1257                                         data->in_min[nr]);
1258                }
1259                if (*nrels_mag > 0) {
1260                        data->in_max[nr] = INS_TO_REG(nr,results[0]+NEG12_OFFSET);
1261                        adm1026_write_value(client, ADM1026_REG_IN_MAX(nr),
1262                                         data->in_max[nr]);
1263                }
1264                up(&data->update_lock);
1265        }
1266}
1267
1268void adm1026_fan(struct i2c_client *client, int operation, int ctl_name,
1269              int *nrels_mag, long *results)
1270{
1271        struct adm1026_data *data = client->data;
1272        int nr = ctl_name - ADM1026_SYSCTL_FAN0 ;
1273
1274        if (nr < 0 || nr > 7)
1275                return ;  /* ERROR */
1276
1277        if (operation == SENSORS_PROC_REAL_INFO)
1278                *nrels_mag = 0;
1279        else if (operation == SENSORS_PROC_REAL_READ) {
1280                adm1026_update_client(client);
1281                results[0] = FAN_FROM_REG(data->fan_min[nr], data->fan_div[nr]);
1282                results[1] = FAN_FROM_REG(data->fan[nr], data->fan_div[nr]);
1283                *nrels_mag = 2;
1284        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1285                down(&data->update_lock);
1286                if (*nrels_mag > 0) {
1287                        data->fan_min[nr] = FAN_TO_REG(results[0],
1288                                                        data->fan_div[nr]);
1289                        adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
1290                                         data->fan_min[nr]);
1291                }
1292                up(&data->update_lock);
1293        }
1294}
1295
1296/* Adjust fan_min to account for new fan divisor */
1297void adm1026_fixup_fan_min(struct i2c_client *client, int fan, int old_div)
1298{
1299        struct adm1026_data *data = client->data;
1300        int  new_div = data->fan_div[fan] ;
1301        int  new_min;
1302
1303        /* 0 and 0xff are special.  Don't adjust them */
1304        if( data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff ) {
1305                return ;
1306        }
1307
1308        new_min = data->fan_min[fan] * old_div / new_div ;
1309        new_min = SENSORS_LIMIT(new_min, 1, 254);
1310        data->fan_min[fan] = new_min ;
1311        adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
1312}
1313
1314void adm1026_fan_div(struct i2c_client *client, int operation, int ctl_name,
1315                int *nrels_mag, long *results)
1316{
1317        struct adm1026_data *data = client->data;
1318        int i ;
1319        int value, div, old ;
1320
1321        if (ctl_name != ADM1026_SYSCTL_FAN_DIV)
1322                return ;  /* ERROR */
1323
1324        if (operation == SENSORS_PROC_REAL_INFO)
1325                *nrels_mag = 0;
1326        else if (operation == SENSORS_PROC_REAL_READ) {
1327                adm1026_update_client(client);
1328                for( i = 0 ; i <= 7 ; ++i ) {
1329                        results[i] = data->fan_div[i] ;
1330                }
1331                *nrels_mag = 8;
1332        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1333                down(&data->update_lock);
1334                value = 0 ;
1335                for( i = 7 ; i >= 0 ; --i ) {
1336                        value <<= 2 ;
1337                        if (*nrels_mag > i) {
1338                                old = data->fan_div[i] ;
1339                                div = DIV_TO_REG(results[i]) ;
1340                                data->fan_div[i] = DIV_FROM_REG(div) ;
1341                                if( data->fan_div[i] != old ) {
1342                                        adm1026_fixup_fan_min(client,i,old);
1343                                }
1344                        } else {
1345                                div = DIV_TO_REG(data->fan_div[i]) ;
1346                        }
1347                        value |= div ;
1348                }
1349                adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
1350                        value & 0xff);
1351                adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
1352                        (value >> 8) & 0xff);
1353                up(&data->update_lock);
1354        }
1355}
1356
1357void adm1026_temp(struct i2c_client *client, int operation, int ctl_name,
1358               int *nrels_mag, long *results)
1359{
1360        struct adm1026_data *data = client->data;
1361        int nr = ctl_name - ADM1026_SYSCTL_TEMP1 ;
1362
1363        if (nr < 0 || nr > 2)
1364                return ;  /* ERROR */
1365
1366        if (operation == SENSORS_PROC_REAL_INFO)
1367                *nrels_mag = 0;
1368        else if (operation == SENSORS_PROC_REAL_READ) {
1369                adm1026_update_client(client);
1370                results[0] = TEMP_FROM_REG(data->temp_min[nr]);
1371                results[1] = TEMP_FROM_REG(data->temp_max[nr]);
1372                results[2] = TEMP_FROM_REG(data->temp[nr]);
1373                *nrels_mag = 3;
1374        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1375                down(&data->update_lock);
1376                if (*nrels_mag > 1) {
1377                        data->temp_max[nr] = TEMP_TO_REG(results[1]);
1378                        adm1026_write_value(client, ADM1026_REG_TEMP_MAX(nr),
1379                                         data->temp_max[nr]);
1380                }
1381                if (*nrels_mag > 0) {
1382                        data->temp_min[nr] = TEMP_TO_REG(results[0]);
1383                        adm1026_write_value(client, ADM1026_REG_TEMP_MIN(nr),
1384                                         data->temp_min[nr]);
1385                }
1386                up(&data->update_lock);
1387        }
1388}
1389
1390void adm1026_temp_offset(struct i2c_client *client, int operation, int ctl_name,
1391                int *nrels_mag, long *results)
1392{
1393        struct adm1026_data *data = client->data;
1394        int nr = ctl_name - ADM1026_SYSCTL_TEMP_OFFSET1 ;
1395
1396        if (nr < 0 || nr > 2)
1397                return ;  /* ERROR */
1398
1399        if (operation == SENSORS_PROC_REAL_INFO)
1400                *nrels_mag = 0;
1401        else if (operation == SENSORS_PROC_REAL_READ) {
1402                adm1026_update_client(client);
1403                results[0] = TEMP_FROM_REG(data->temp_offset[nr]);
1404                *nrels_mag = 1;
1405        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1406                down(&data->update_lock);
1407                if (*nrels_mag > 0) {
1408                        data->temp_offset[nr] = TEMP_TO_REG(results[0]);
1409                        adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET(nr),
1410                            data->temp_offset[nr]);
1411                }
1412                up(&data->update_lock);
1413        }
1414}
1415
1416void adm1026_temp_tmin(struct i2c_client *client, int operation, int ctl_name,
1417                int *nrels_mag, long *results)
1418{
1419        struct adm1026_data *data = client->data;
1420        int nr = ctl_name - ADM1026_SYSCTL_TEMP_TMIN1 ;
1421
1422        if (nr < 0 || nr > 2)
1423                return ;  /* ERROR */
1424
1425        if (operation == SENSORS_PROC_REAL_INFO)
1426                *nrels_mag = 0;
1427        else if (operation == SENSORS_PROC_REAL_READ) {
1428                adm1026_update_client(client);
1429                results[0] = TEMP_FROM_REG(data->temp_tmin[nr]);
1430                *nrels_mag = 1;
1431        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1432                down(&data->update_lock);
1433                if (*nrels_mag > 0) {
1434                        data->temp_tmin[nr] = TEMP_TO_REG(results[0]);
1435                        adm1026_write_value(client, ADM1026_REG_TEMP_TMIN(nr),
1436                            data->temp_tmin[nr]);
1437                }
1438                up(&data->update_lock);
1439        }
1440}
1441
1442void adm1026_temp_therm(struct i2c_client *client, int operation, int ctl_name,
1443                int *nrels_mag, long *results)
1444{
1445        struct adm1026_data *data = client->data;
1446        int nr = ctl_name - ADM1026_SYSCTL_TEMP_THERM1 ;
1447
1448        if (nr < 0 || nr > 2)
1449                return ;  /* ERROR */
1450
1451        if (operation == SENSORS_PROC_REAL_INFO)
1452                *nrels_mag = 0;
1453        else if (operation == SENSORS_PROC_REAL_READ) {
1454                adm1026_update_client(client);
1455                results[0] = TEMP_FROM_REG(data->temp_therm[nr]);
1456                *nrels_mag = 1;
1457        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1458                down(&data->update_lock);
1459                if (*nrels_mag > 0) {
1460                        data->temp_therm[nr] = TEMP_TO_REG(results[0]);
1461                        adm1026_write_value(client, ADM1026_REG_TEMP_THERM(nr),
1462                            data->temp_therm[nr]);
1463                }
1464                up(&data->update_lock);
1465        }
1466}
1467
1468void adm1026_pwm(struct i2c_client *client, int operation, int ctl_name,
1469              int *nrels_mag, long *results)
1470{
1471        struct adm1026_data *data = client->data;
1472
1473        if (ctl_name != ADM1026_SYSCTL_PWM)
1474                return ;  /* ERROR */
1475
1476        if (operation == SENSORS_PROC_REAL_INFO)
1477                *nrels_mag = 0;
1478        else if (operation == SENSORS_PROC_REAL_READ) {
1479                adm1026_update_client(client);
1480                results[0] = PWM_FROM_REG(data->pwm);
1481                results[1] = 1 ;  /* Always enabled */
1482                *nrels_mag = 2;
1483        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1484                down(&data->update_lock);
1485                /* PWM enable is read-only */
1486                if (*nrels_mag > 0) {
1487                        data->pwm = PWM_TO_REG(results[0]);
1488                        adm1026_write_value(client, ADM1026_REG_PWM,
1489                                         data->pwm);
1490                }
1491                up(&data->update_lock);
1492        }
1493}
1494
1495void adm1026_analog_out(struct i2c_client *client, int operation, int ctl_name,
1496              int *nrels_mag, long *results)
1497{
1498        struct adm1026_data *data = client->data;
1499
1500        if (ctl_name != ADM1026_SYSCTL_ANALOG_OUT)
1501                return ;  /* ERROR */
1502
1503        if (operation == SENSORS_PROC_REAL_INFO)
1504                *nrels_mag = 0;  /* 0 - 255 */
1505        else if (operation == SENSORS_PROC_REAL_READ) {
1506                adm1026_update_client(client);
1507                results[0] = DAC_FROM_REG(data->analog_out);
1508                *nrels_mag = 1;
1509        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1510                down(&data->update_lock);
1511                if (*nrels_mag > 0) {
1512                        data->analog_out = DAC_TO_REG(results[0]);
1513                        adm1026_write_value(client, ADM1026_REG_DAC,
1514                                         data->analog_out);
1515                }
1516                up(&data->update_lock);
1517        }
1518}
1519
1520void adm1026_afc(struct i2c_client *client, int operation, int ctl_name,
1521              int *nrels_mag, long *results)
1522{
1523        struct adm1026_data *data = client->data;
1524
1525        if (ctl_name != ADM1026_SYSCTL_AFC)
1526                return ;  /* ERROR */
1527
1528        /* PWM auto fan control, DAC auto fan control */
1529
1530        if (operation == SENSORS_PROC_REAL_INFO)
1531                *nrels_mag = 0;
1532        else if (operation == SENSORS_PROC_REAL_READ) {
1533                adm1026_update_client(client);
1534                results[0] = (data->config1 & CFG1_PWM_AFC) != 0 ;
1535                results[1] = (data->config1 & CFG1_DAC_AFC) != 0 ;
1536                *nrels_mag = 2;
1537        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1538                down(&data->update_lock);
1539                if (*nrels_mag > 1) {
1540                        data->config1 = (data->config1 & ~CFG1_DAC_AFC)
1541                                | (results[1] ? CFG1_DAC_AFC : 0) ;
1542                }
1543                if (*nrels_mag > 0) {
1544                        data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1545                                | (results[0] ? CFG1_PWM_AFC : 0) ;
1546                        adm1026_write_value(client, ADM1026_REG_CONFIG1,
1547                                         data->config1);
1548                }
1549                up(&data->update_lock);
1550        }
1551}
1552
1553void adm1026_vid(struct i2c_client *client, int operation, int ctl_name,
1554              int *nrels_mag, long *results)
1555{
1556        struct adm1026_data *data = client->data;
1557
1558        if( ctl_name != ADM1026_SYSCTL_VID )
1559                return ;  /* ERROR */
1560
1561        if (operation == SENSORS_PROC_REAL_INFO)
1562                *nrels_mag = 3;
1563        else if (operation == SENSORS_PROC_REAL_READ) {
1564                adm1026_update_client(client);
1565                results[0] = VID_FROM_REG((data->vid)&0x3f,data->vrm);
1566                *nrels_mag = 1;
1567        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1568                down(&data->update_lock);
1569                /* Hmmm... There isn't a VID_TO_REG mapping */
1570                if (*nrels_mag > 0) {
1571                        if( results[0] >= 0 ) {
1572                                data->vid = results[0] & 0x3f ;
1573                        } else {
1574                                data->vid = ADM1026_INIT_VID ;
1575                        }
1576                }
1577                up(&data->update_lock);
1578        }
1579
1580}
1581
1582void adm1026_vrm(struct i2c_client *client, int operation, int ctl_name,
1583              int *nrels_mag, long *results)
1584{
1585        struct adm1026_data *data = client->data;
1586
1587        if( ctl_name != ADM1026_SYSCTL_VRM )
1588                return ;  /* ERROR */
1589
1590        if (operation == SENSORS_PROC_REAL_INFO)
1591                *nrels_mag = 1;
1592        else if (operation == SENSORS_PROC_REAL_READ) {
1593                results[0] = data->vrm ;
1594                *nrels_mag = 1;
1595        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1596                if (*nrels_mag > 0) {
1597                        data->vrm = results[0] ;
1598                }
1599        }
1600}
1601
1602void adm1026_alarms(struct i2c_client *client, int operation, int ctl_name,
1603                int *nrels_mag, long *results)
1604{
1605        struct adm1026_data *data = client->data;
1606
1607        if( ctl_name != ADM1026_SYSCTL_ALARMS )
1608                return ;  /* ERROR */
1609
1610        if (operation == SENSORS_PROC_REAL_INFO)
1611                *nrels_mag = 0;
1612        else if (operation == SENSORS_PROC_REAL_READ) {
1613                adm1026_update_client(client);
1614                results[0] = data->alarms ;
1615                *nrels_mag = 1;
1616        }
1617        /* FIXME: Perhaps we should implement a write function
1618         *   to clear an alarm?
1619         */
1620}
1621
1622void adm1026_alarm_mask(struct i2c_client *client, int operation,
1623                int ctl_name, int *nrels_mag, long *results)
1624{
1625        struct adm1026_data *data = client->data;
1626        unsigned long mask ;
1627
1628        if( ctl_name != ADM1026_SYSCTL_ALARM_MASK )
1629                return ;  /* ERROR */
1630
1631        if (operation == SENSORS_PROC_REAL_INFO)
1632                *nrels_mag = 0;
1633        else if (operation == SENSORS_PROC_REAL_READ) {
1634                adm1026_update_client(client);
1635                results[0] = data->alarm_mask ;
1636                *nrels_mag = 1;
1637        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1638                down(&data->update_lock);
1639                if (*nrels_mag > 0) {
1640                        data->alarm_mask = results[0] & 0x7fffffff ;
1641                        mask = data->alarm_mask
1642                                | (data->gpio_mask & 0x10000 ? 0x80000000 : 0) ;
1643                        adm1026_write_value(client, ADM1026_REG_MASK1,
1644                                        mask & 0xff);
1645                        mask >>= 8 ;
1646                        adm1026_write_value(client, ADM1026_REG_MASK2,
1647                                        mask & 0xff);
1648                        mask >>= 8 ;
1649                        adm1026_write_value(client, ADM1026_REG_MASK3,
1650                                        mask & 0xff);
1651                        mask >>= 8 ;
1652                        adm1026_write_value(client, ADM1026_REG_MASK4,
1653                                        mask & 0xff);
1654                }
1655                up(&data->update_lock);
1656        }
1657}
1658
1659void adm1026_gpio(struct i2c_client *client, int operation, int ctl_name,
1660                int *nrels_mag, long *results)
1661{
1662        struct adm1026_data *data = client->data;
1663        long gpio ;
1664
1665        if( ctl_name != ADM1026_SYSCTL_GPIO )
1666                return ;  /* ERROR */
1667
1668        if (operation == SENSORS_PROC_REAL_INFO)
1669                *nrels_mag = 0;
1670        else if (operation == SENSORS_PROC_REAL_READ) {
1671                adm1026_update_client(client);
1672                results[0] = data->gpio ;
1673                *nrels_mag = 1;
1674        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1675                down(&data->update_lock);
1676                if (*nrels_mag > 0) {
1677                        data->gpio = results[0] & 0x1ffff ;
1678                        gpio = data->gpio ;
1679                        adm1026_write_value(client,
1680                                ADM1026_REG_GPIO_STATUS_0_7,
1681                                gpio & 0xff );
1682                        gpio >>= 8 ;
1683                        adm1026_write_value(client,
1684                                ADM1026_REG_GPIO_STATUS_8_15,
1685                                gpio & 0xff );
1686                        gpio = ((gpio >> 1) & 0x80)
1687                                | (data->alarms >> 24 & 0x7f);
1688                        adm1026_write_value(client,
1689                                ADM1026_REG_STATUS4,
1690                                gpio & 0xff );
1691                }
1692                up(&data->update_lock);
1693        }
1694}
1695
1696void adm1026_gpio_mask(struct i2c_client *client, int operation,
1697                int ctl_name, int *nrels_mag, long *results)
1698{
1699        struct adm1026_data *data = client->data;
1700        long mask ;
1701
1702        if( ctl_name != ADM1026_SYSCTL_GPIO_MASK )
1703                return ;  /* ERROR */
1704
1705        if (operation == SENSORS_PROC_REAL_INFO)
1706                *nrels_mag = 0;
1707        else if (operation == SENSORS_PROC_REAL_READ) {
1708                adm1026_update_client(client);
1709                results[0] = data->gpio_mask ;
1710                *nrels_mag = 1;
1711        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1712                down(&data->update_lock);
1713                if (*nrels_mag > 0) {
1714                        data->gpio_mask = results[0] & 0x1ffff ;
1715                        mask = data->gpio_mask ;
1716                        adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,
1717                                        mask & 0xff);
1718                        mask >>= 8 ;
1719                        adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,
1720                                        mask & 0xff);
1721                        mask = ((mask >> 1) & 0x80)
1722                                | (data->alarm_mask >> 24 & 0x7f);
1723                        adm1026_write_value(client, ADM1026_REG_MASK1,
1724                                        mask & 0xff);
1725                }
1726                up(&data->update_lock);
1727        }
1728}
1729
1730static int __init sm_adm1026_init(void)
1731{
1732        printk("adm1026: Version %s (%s)\n", LM_VERSION, LM_DATE);
1733        printk("adm1026: See http://www.penguincomputing.com/lm_sensors for more info.\n" );
1734        return i2c_add_driver(&adm1026_driver);
1735}
1736
1737static void __exit sm_adm1026_exit(void)
1738{
1739        i2c_del_driver(&adm1026_driver);
1740}
1741
1742MODULE_LICENSE("GPL");
1743MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com");
1744MODULE_DESCRIPTION("ADM1026 driver");
1745
1746module_init(sm_adm1026_init);
1747module_exit(sm_adm1026_exit);
Note: See TracBrowser for help on using the browser.