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

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

Add ID in case i2c-id.h doesn't have it.

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