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

Revision 2445, 55.9 KB (checked in by khali, 9 years ago)

Backport memory allocation reworking from Linux 2.6.

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