This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / i2c / chips / adm1026.c
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     Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
6
7     Chip details at:
8
9     <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
10
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/i2c-sensor.h>
32 #include <linux/i2c-vid.h>
33
34 /* Addresses to scan */
35 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
36 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
37
38 /* Insmod parameters */
39 SENSORS_INSMOD_1(adm1026);
40
41 static int gpio_input[17]  = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
42                                 -1, -1, -1, -1, -1, -1, -1, -1 }; 
43 static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
44                                 -1, -1, -1, -1, -1, -1, -1, -1 };
45 static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
46                                 -1, -1, -1, -1, -1, -1, -1, -1 };
47 static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
48                                 -1, -1, -1, -1, -1, -1, -1, -1 };
49 static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
50 module_param_array(gpio_input,int,NULL,0);
51 MODULE_PARM_DESC(gpio_input,"List of GPIO pins (0-16) to program as inputs");
52 module_param_array(gpio_output,int,NULL,0);
53 MODULE_PARM_DESC(gpio_output,"List of GPIO pins (0-16) to program as "
54         "outputs");
55 module_param_array(gpio_inverted,int,NULL,0);
56 MODULE_PARM_DESC(gpio_inverted,"List of GPIO pins (0-16) to program as "
57         "inverted");
58 module_param_array(gpio_normal,int,NULL,0);
59 MODULE_PARM_DESC(gpio_normal,"List of GPIO pins (0-16) to program as "
60         "normal/non-inverted");
61 module_param_array(gpio_fan,int,NULL,0);
62 MODULE_PARM_DESC(gpio_fan,"List of GPIO pins (0-7) to program as fan tachs");
63
64 /* Many ADM1026 constants specified below */
65
66 /* The ADM1026 registers */
67 #define ADM1026_REG_CONFIG1  0x00
68 #define CFG1_MONITOR     0x01
69 #define CFG1_INT_ENABLE  0x02
70 #define CFG1_INT_CLEAR   0x04
71 #define CFG1_AIN8_9      0x08
72 #define CFG1_THERM_HOT   0x10
73 #define CFG1_DAC_AFC     0x20
74 #define CFG1_PWM_AFC     0x40
75 #define CFG1_RESET       0x80
76 #define ADM1026_REG_CONFIG2  0x01
77 /* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
78 #define ADM1026_REG_CONFIG3  0x07
79 #define CFG3_GPIO16_ENABLE  0x01
80 #define CFG3_CI_CLEAR  0x02
81 #define CFG3_VREF_250  0x04
82 #define CFG3_GPIO16_DIR  0x40
83 #define CFG3_GPIO16_POL  0x80
84 #define ADM1026_REG_E2CONFIG  0x13
85 #define E2CFG_READ  0x01
86 #define E2CFG_WRITE  0x02
87 #define E2CFG_ERASE  0x04
88 #define E2CFG_ROM  0x08
89 #define E2CFG_CLK_EXT  0x80
90
91 /* There are 10 general analog inputs and 7 dedicated inputs
92  * They are:
93  *    0 - 9  =  AIN0 - AIN9
94  *       10  =  Vbat
95  *       11  =  3.3V Standby
96  *       12  =  3.3V Main
97  *       13  =  +5V
98  *       14  =  Vccp (CPU core voltage)
99  *       15  =  +12V
100  *       16  =  -12V
101  */
102 static u16 ADM1026_REG_IN[] = {
103                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
104                 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
105                 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
106         };
107 static u16 ADM1026_REG_IN_MIN[] = {
108                 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
109                 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
110                 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
111         };
112 static u16 ADM1026_REG_IN_MAX[] = {
113                 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
114                 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
115                 0x43, 0x44, 0x45, 0x46, 0x47
116         };
117
118 /* Temperatures are:
119  *    0 - Internal
120  *    1 - External 1
121  *    2 - External 2
122  */
123 static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
124 static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
125 static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
126 static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
127 static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
128 static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
129
130 #define ADM1026_REG_FAN(nr) (0x38 + (nr))
131 #define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
132 #define ADM1026_REG_FAN_DIV_0_3 0x02
133 #define ADM1026_REG_FAN_DIV_4_7 0x03
134
135 #define ADM1026_REG_DAC  0x04
136 #define ADM1026_REG_PWM  0x05
137
138 #define ADM1026_REG_GPIO_CFG_0_3 0x08
139 #define ADM1026_REG_GPIO_CFG_4_7 0x09
140 #define ADM1026_REG_GPIO_CFG_8_11 0x0a
141 #define ADM1026_REG_GPIO_CFG_12_15 0x0b
142 /* CFG_16 in REG_CFG3 */
143 #define ADM1026_REG_GPIO_STATUS_0_7 0x24
144 #define ADM1026_REG_GPIO_STATUS_8_15 0x25
145 /* STATUS_16 in REG_STATUS4 */
146 #define ADM1026_REG_GPIO_MASK_0_7 0x1c
147 #define ADM1026_REG_GPIO_MASK_8_15 0x1d
148 /* MASK_16 in REG_MASK4 */
149
150 #define ADM1026_REG_COMPANY 0x16
151 #define ADM1026_REG_VERSTEP 0x17
152 /* These are the recognized values for the above regs */
153 #define ADM1026_COMPANY_ANALOG_DEV 0x41
154 #define ADM1026_VERSTEP_GENERIC 0x40
155 #define ADM1026_VERSTEP_ADM1026 0x44
156
157 #define ADM1026_REG_MASK1 0x18
158 #define ADM1026_REG_MASK2 0x19
159 #define ADM1026_REG_MASK3 0x1a
160 #define ADM1026_REG_MASK4 0x1b
161
162 #define ADM1026_REG_STATUS1 0x20
163 #define ADM1026_REG_STATUS2 0x21
164 #define ADM1026_REG_STATUS3 0x22
165 #define ADM1026_REG_STATUS4 0x23
166
167 #define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
168 #define ADM1026_FAN_CONTROL_TEMP_RANGE 20
169 #define ADM1026_PWM_MAX 255
170
171 /* Conversions. Rounding and limit checking is only done on the TO_REG 
172  * variants. Note that you should be a bit careful with which arguments
173  * these macros are called: arguments may be evaluated more than once.
174  */
175
176 /* IN are scaled acording to built-in resistors.  These are the
177  *   voltages corresponding to 3/4 of full scale (192 or 0xc0)
178  *   NOTE: The -12V input needs an additional factor to account
179  *      for the Vref pullup resistor.
180  *      NEG12_OFFSET = SCALE * Vref / V-192 - Vref
181  *                   = 13875 * 2.50 / 1.875 - 2500
182  *                   = 16000
183  *
184  * The values in this table are based on Table II, page 15 of the
185  *    datasheet.
186  */
187 static int adm1026_scaling[] = {  /* .001 Volts */
188                 2250, 2250, 2250, 2250, 2250, 2250, 
189                 1875, 1875, 1875, 1875, 3000, 3330, 
190                 3330, 4995, 2250, 12000, 13875
191         };
192 #define NEG12_OFFSET  16000
193 #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
194 #define INS_TO_REG(n,val)  (SENSORS_LIMIT(SCALE(val,adm1026_scaling[n],192),\
195         0,255))
196 #define INS_FROM_REG(n,val) (SCALE(val,192,adm1026_scaling[n]))
197
198 /* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
199  *   and we assume a 2 pulse-per-rev fan tach signal
200  *      22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
201  */
202 #define FAN_TO_REG(val,div)  ((val)<=0 ? 0xff : SENSORS_LIMIT(1350000/((val)*\
203         (div)),1,254)) 
204 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==0xff ? 0 : 1350000/((val)*\
205         (div)))
206 #define DIV_FROM_REG(val) (1<<(val))
207 #define DIV_TO_REG(val) ((val)>=8 ? 3 : (val)>=4 ? 2 : (val)>=2 ? 1 : 0)
208
209 /* Temperature is reported in 1 degC increments */
210 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
211         -127,127))
212 #define TEMP_FROM_REG(val) ((val) * 1000)
213 #define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
214         -127,127))
215 #define OFFSET_FROM_REG(val) ((val) * 1000)
216
217 #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
218 #define PWM_FROM_REG(val) (val)
219
220 #define PWM_MIN_TO_REG(val) ((val) & 0xf0)
221 #define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
222
223 /* Analog output is a voltage, and scaled to millivolts.  The datasheet 
224  *   indicates that the DAC could be used to drive the fans, but in our 
225  *   example board (Arima HDAMA) it isn't connected to the fans at all.
226  */
227 #define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500),0,255)) 
228 #define DAC_FROM_REG(val) (((val)*2500)/255)
229
230 /* Typically used with systems using a v9.1 VRM spec ? */
231 #define ADM1026_INIT_VRM  91
232
233 /* Chip sampling rates
234  *
235  * Some sensors are not updated more frequently than once per second
236  *    so it doesn't make sense to read them more often than that.
237  *    We cache the results and return the saved data if the driver
238  *    is called again before a second has elapsed.
239  *
240  * Also, there is significant configuration data for this chip
241  *    So, we keep the config data up to date in the cache
242  *    when it is written and only sample it once every 5 *minutes*
243  */
244 #define ADM1026_DATA_INTERVAL  (1 * HZ)
245 #define ADM1026_CONFIG_INTERVAL  (5 * 60 * HZ)
246
247 /* We allow for multiple chips in a single system.
248  *
249  * For each registered ADM1026, we need to keep state information
250  * at client->data. The adm1026_data structure is dynamically
251  * allocated, when a new client structure is allocated. */
252
253 struct pwm_data {
254         u8 pwm;
255         u8 enable;
256         u8 auto_pwm_min;
257 };
258
259 struct adm1026_data {
260         struct i2c_client client;
261         struct semaphore lock;
262         enum chips type;
263
264         struct semaphore update_lock;
265         int valid;              /* !=0 if following fields are valid */
266         unsigned long last_reading;     /* In jiffies */
267         unsigned long last_config;      /* In jiffies */
268
269         u8 in[17];              /* Register value */
270         u8 in_max[17];          /* Register value */
271         u8 in_min[17];          /* Register value */
272         s8 temp[3];             /* Register value */
273         s8 temp_min[3];         /* Register value */
274         s8 temp_max[3];         /* Register value */
275         s8 temp_tmin[3];        /* Register value */
276         s8 temp_crit[3];        /* Register value */
277         s8 temp_offset[3];      /* Register value */
278         u8 fan[8];              /* Register value */
279         u8 fan_min[8];          /* Register value */
280         u8 fan_div[8];          /* Decoded value */
281         struct pwm_data pwm1;   /* Pwm control values */
282         int vid;                /* Decoded value */
283         u8 vrm;                 /* VRM version */
284         u8 analog_out;          /* Register value (DAC) */
285         long alarms;            /* Register encoding, combined */
286         long alarm_mask;        /* Register encoding, combined */
287         long gpio;              /* Register encoding, combined */
288         long gpio_mask;         /* Register encoding, combined */
289         u8 gpio_config[17];     /* Decoded value */
290         u8 config1;             /* Register value */
291         u8 config2;             /* Register value */
292         u8 config3;             /* Register value */
293 };
294
295 static int adm1026_attach_adapter(struct i2c_adapter *adapter);
296 static int adm1026_detect(struct i2c_adapter *adapter, int address,
297         int kind);
298 static int adm1026_detach_client(struct i2c_client *client);
299 static int adm1026_read_value(struct i2c_client *client, u8 register);
300 static int adm1026_write_value(struct i2c_client *client, u8 register,
301         int value); 
302 static void adm1026_print_gpio(struct i2c_client *client);
303 static void adm1026_fixup_gpio(struct i2c_client *client); 
304 static struct adm1026_data *adm1026_update_device(struct device *dev);
305 static void adm1026_init_client(struct i2c_client *client);
306
307
308 static struct i2c_driver adm1026_driver = {
309         .owner          = THIS_MODULE,
310         .name           = "adm1026",
311         .flags          = I2C_DF_NOTIFY,
312         .attach_adapter = adm1026_attach_adapter,
313         .detach_client  = adm1026_detach_client,
314 };
315
316 static int adm1026_id;
317
318 int adm1026_attach_adapter(struct i2c_adapter *adapter)
319 {
320         if (!(adapter->class & I2C_CLASS_HWMON)) {
321                 return 0;
322         }
323         return i2c_detect(adapter, &addr_data, adm1026_detect);
324 }
325
326 int adm1026_detach_client(struct i2c_client *client)
327 {
328         i2c_detach_client(client);
329         kfree(client);
330         return 0;
331 }
332
333 int adm1026_read_value(struct i2c_client *client, u8 reg)
334 {
335         int res;
336
337         if (reg < 0x80) {
338                 /* "RAM" locations */
339                 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
340         } else {
341                 /* EEPROM, do nothing */
342                 res = 0;
343         }
344         return res;
345 }
346
347 int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
348 {
349         int res;
350
351         if (reg < 0x80) {
352                 /* "RAM" locations */
353                 res = i2c_smbus_write_byte_data(client, reg, value);
354         } else {
355                 /* EEPROM, do nothing */
356                 res = 0;
357         }
358         return res;
359 }
360
361 void adm1026_init_client(struct i2c_client *client)
362 {
363         int value, i;
364         struct adm1026_data *data = i2c_get_clientdata(client);
365
366         dev_dbg(&client->dev,"(%d): Initializing device\n", client->id);
367         /* Read chip config */
368         data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
369         data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
370         data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
371
372         /* Inform user of chip config */
373         dev_dbg(&client->dev, "(%d): ADM1026_REG_CONFIG1 is: 0x%02x\n",
374                 client->id, data->config1);
375         if ((data->config1 & CFG1_MONITOR) == 0) {
376                 dev_dbg(&client->dev, "(%d): Monitoring not currently "
377                         "enabled.\n", client->id);
378         }
379         if (data->config1 & CFG1_INT_ENABLE) {
380                 dev_dbg(&client->dev, "(%d): SMBALERT interrupts are "
381                         "enabled.\n", client->id);
382         }
383         if (data->config1 & CFG1_AIN8_9) {
384                 dev_dbg(&client->dev, "(%d): in8 and in9 enabled. "
385                         "temp3 disabled.\n", client->id);
386         } else {
387                 dev_dbg(&client->dev, "(%d): temp3 enabled.  in8 and "
388                         "in9 disabled.\n", client->id);
389         }
390         if (data->config1 & CFG1_THERM_HOT) {
391                 dev_dbg(&client->dev, "(%d): Automatic THERM, PWM, "
392                         "and temp limits enabled.\n", client->id);
393         }
394
395         value = data->config3;
396         if (data->config3 & CFG3_GPIO16_ENABLE) {
397                 dev_dbg(&client->dev, "(%d): GPIO16 enabled.  THERM"
398                         "pin disabled.\n", client->id);
399         } else {
400                 dev_dbg(&client->dev, "(%d): THERM pin enabled.  "
401                         "GPIO16 disabled.\n", client->id);
402         }
403         if (data->config3 & CFG3_VREF_250) {
404                 dev_dbg(&client->dev, "(%d): Vref is 2.50 Volts.\n",
405                         client->id);
406         } else {
407                 dev_dbg(&client->dev, "(%d): Vref is 1.82 Volts.\n",
408                         client->id);
409         }
410         /* Read and pick apart the existing GPIO configuration */
411         value = 0;
412         for (i = 0;i <= 15;++i) {
413                 if ((i & 0x03) == 0) {
414                         value = adm1026_read_value(client,
415                                         ADM1026_REG_GPIO_CFG_0_3 + i/4);
416                 }
417                 data->gpio_config[i] = value & 0x03;
418                 value >>= 2;
419         }
420         data->gpio_config[16] = (data->config3 >> 6) & 0x03;
421
422         /* ... and then print it */
423         adm1026_print_gpio(client);
424
425         /* If the user asks us to reprogram the GPIO config, then
426          *   do it now.  But only if this is the first ADM1026.
427          */
428         if (client->id == 0
429             && (gpio_input[0] != -1 || gpio_output[0] != -1
430                 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
431                 || gpio_fan[0] != -1)) {
432                 adm1026_fixup_gpio(client);
433         }
434
435         /* WE INTENTIONALLY make no changes to the limits,
436          *   offsets, pwms, fans and zones.  If they were
437          *   configured, we don't want to mess with them.
438          *   If they weren't, the default is 100% PWM, no
439          *   control and will suffice until 'sensors -s'
440          *   can be run by the user.  We DO set the default 
441          *   value for pwm1.auto_pwm_min to its maximum
442          *   so that enabling automatic pwm fan control
443          *   without first setting a value for pwm1.auto_pwm_min 
444          *   will not result in potentially dangerous fan speed decrease.
445          */
446         data->pwm1.auto_pwm_min=255;
447         /* Start monitoring */
448         value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
449         /* Set MONITOR, clear interrupt acknowledge and s/w reset */
450         value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
451         dev_dbg(&client->dev, "(%d): Setting CONFIG to: 0x%02x\n",
452                 client->id, value);
453         data->config1 = value;
454         adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
455 }
456
457 void adm1026_print_gpio(struct i2c_client *client)
458 {
459         struct adm1026_data *data = i2c_get_clientdata(client);
460         int  i;
461
462         dev_dbg(&client->dev, "(%d): GPIO config is:",
463                             client->id);
464         for (i = 0;i <= 7;++i) {
465                 if (data->config2 & (1 << i)) {
466                         dev_dbg(&client->dev, "\t(%d): %sGP%s%d\n", client->id,
467                                 data->gpio_config[i] & 0x02 ? "" : "!",
468                                 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
469                                 i);
470                 } else {
471                         dev_dbg(&client->dev, "\t(%d): FAN%d\n",
472                                 client->id, i);
473                 }
474         }
475         for (i = 8;i <= 15;++i) {
476                 dev_dbg(&client->dev, "\t(%d): %sGP%s%d\n", client->id,
477                         data->gpio_config[i] & 0x02 ? "" : "!",
478                         data->gpio_config[i] & 0x01 ? "OUT" : "IN",
479                         i);
480         }
481         if (data->config3 & CFG3_GPIO16_ENABLE) {
482                 dev_dbg(&client->dev, "\t(%d): %sGP%s16\n", client->id,
483                         data->gpio_config[16] & 0x02 ? "" : "!",
484                         data->gpio_config[16] & 0x01 ? "OUT" : "IN");
485         } else {
486                 /* GPIO16 is THERM  */
487                 dev_dbg(&client->dev, "\t(%d): THERM\n", client->id);
488         }
489 }
490
491 void adm1026_fixup_gpio(struct i2c_client *client)
492 {
493         struct adm1026_data *data = i2c_get_clientdata(client);
494         int  i;
495         int  value;
496
497         /* Make the changes requested. */
498         /* We may need to unlock/stop monitoring or soft-reset the
499          *    chip before we can make changes.  This hasn't been
500          *    tested much.  FIXME
501          */
502
503         /* Make outputs */
504         for (i = 0;i <= 16;++i) {
505                 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
506                         data->gpio_config[gpio_output[i]] |= 0x01;
507                 }
508                 /* if GPIO0-7 is output, it isn't a FAN tach */
509                 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
510                         data->config2 |= 1 << gpio_output[i];
511                 }
512         }
513
514         /* Input overrides output */
515         for (i = 0;i <= 16;++i) {
516                 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
517                         data->gpio_config[gpio_input[i]] &= ~ 0x01;
518                 }
519                 /* if GPIO0-7 is input, it isn't a FAN tach */
520                 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
521                         data->config2 |= 1 << gpio_input[i];
522                 }
523         }
524
525         /* Inverted  */
526         for (i = 0;i <= 16;++i) {
527                 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
528                         data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
529                 }
530         }
531
532         /* Normal overrides inverted  */
533         for (i = 0;i <= 16;++i) {
534                 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
535                         data->gpio_config[gpio_normal[i]] |= 0x02;
536                 }
537         }
538
539         /* Fan overrides input and output */
540         for (i = 0;i <= 7;++i) {
541                 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
542                         data->config2 &= ~(1 << gpio_fan[i]);
543                 }
544         }
545
546         /* Write new configs to registers */
547         adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
548         data->config3 = (data->config3 & 0x3f)
549                         | ((data->gpio_config[16] & 0x03) << 6);
550         adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
551         for (i = 15, value = 0;i >= 0;--i) {
552                 value <<= 2;
553                 value |= data->gpio_config[i] & 0x03;
554                 if ((i & 0x03) == 0) {
555                         adm1026_write_value(client,
556                                         ADM1026_REG_GPIO_CFG_0_3 + i/4,
557                                         value);
558                         value = 0;
559                 }
560         }
561
562         /* Print the new config */
563         adm1026_print_gpio(client);
564 }
565
566
567 static struct adm1026_data *adm1026_update_device(struct device *dev)
568 {
569         struct i2c_client *client = to_i2c_client(dev);
570         struct adm1026_data *data = i2c_get_clientdata(client);
571         int i;
572         long value, alarms, gpio;
573
574         down(&data->update_lock);
575         if (!data->valid
576             || (jiffies - data->last_reading > ADM1026_DATA_INTERVAL)) {
577                 /* Things that change quickly */
578                 dev_dbg(&client->dev,"(%d): Reading sensor values\n", 
579                         client->id);
580                 for (i = 0;i <= 16;++i) {
581                         data->in[i] =
582                             adm1026_read_value(client, ADM1026_REG_IN[i]);
583                 }
584
585                 for (i = 0;i <= 7;++i) {
586                         data->fan[i] =
587                             adm1026_read_value(client, ADM1026_REG_FAN(i));
588                 }
589
590                 for (i = 0;i <= 2;++i) {
591                         /* NOTE: temp[] is s8 and we assume 2's complement
592                          *   "conversion" in the assignment   */
593                         data->temp[i] =
594                             adm1026_read_value(client, ADM1026_REG_TEMP[i]);
595                 }
596
597                 data->pwm1.pwm = adm1026_read_value(client, 
598                         ADM1026_REG_PWM);
599                 data->analog_out = adm1026_read_value(client, 
600                         ADM1026_REG_DAC);
601                 /* GPIO16 is MSbit of alarms, move it to gpio */
602                 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
603                 gpio = alarms & 0x80 ? 0x0100 : 0;  /* GPIO16 */
604                 alarms &= 0x7f;
605                 alarms <<= 8;
606                 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
607                 alarms <<= 8;
608                 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
609                 alarms <<= 8;
610                 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
611                 data->alarms = alarms;
612
613                 /* Read the GPIO values */
614                 gpio |= adm1026_read_value(client, 
615                         ADM1026_REG_GPIO_STATUS_8_15);
616                 gpio <<= 8;
617                 gpio |= adm1026_read_value(client, 
618                         ADM1026_REG_GPIO_STATUS_0_7);
619                 data->gpio = gpio;
620
621                 data->last_reading = jiffies;
622         };  /* last_reading */
623
624         if (!data->valid || (jiffies - data->last_config > 
625                 ADM1026_CONFIG_INTERVAL)) {
626                 /* Things that don't change often */
627                 dev_dbg(&client->dev, "(%d): Reading config values\n", 
628                         client->id);
629                 for (i = 0;i <= 16;++i) {
630                         data->in_min[i] = adm1026_read_value(client, 
631                                 ADM1026_REG_IN_MIN[i]);
632                         data->in_max[i] = adm1026_read_value(client, 
633                                 ADM1026_REG_IN_MAX[i]);
634                 }
635
636                 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
637                         | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
638                         << 8);
639                 for (i = 0;i <= 7;++i) {
640                         data->fan_min[i] = adm1026_read_value(client, 
641                                 ADM1026_REG_FAN_MIN(i));
642                         data->fan_div[i] = DIV_FROM_REG(value & 0x03);
643                         value >>= 2;
644                 }
645
646                 for (i = 0; i <= 2; ++i) {
647                         /* NOTE: temp_xxx[] are s8 and we assume 2's 
648                          *    complement "conversion" in the assignment
649                          */
650                         data->temp_min[i] = adm1026_read_value(client, 
651                                 ADM1026_REG_TEMP_MIN[i]);
652                         data->temp_max[i] = adm1026_read_value(client, 
653                                 ADM1026_REG_TEMP_MAX[i]);
654                         data->temp_tmin[i] = adm1026_read_value(client, 
655                                 ADM1026_REG_TEMP_TMIN[i]);
656                         data->temp_crit[i] = adm1026_read_value(client, 
657                                 ADM1026_REG_TEMP_THERM[i]);
658                         data->temp_offset[i] = adm1026_read_value(client, 
659                                 ADM1026_REG_TEMP_OFFSET[i]);
660                 }
661
662                 /* Read the STATUS/alarm masks */
663                 alarms  = adm1026_read_value(client, ADM1026_REG_MASK4);
664                 gpio    = alarms & 0x80 ? 0x0100 : 0;  /* GPIO16 */
665                 alarms  = (alarms & 0x7f) << 8;
666                 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
667                 alarms <<= 8;
668                 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
669                 alarms <<= 8;
670                 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
671                 data->alarm_mask = alarms;
672
673                 /* Read the GPIO values */
674                 gpio |= adm1026_read_value(client, 
675                         ADM1026_REG_GPIO_MASK_8_15);
676                 gpio <<= 8;
677                 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
678                 data->gpio_mask = gpio;
679
680                 /* Read various values from CONFIG1 */
681                 data->config1 = adm1026_read_value(client, 
682                         ADM1026_REG_CONFIG1);
683                 if (data->config1 & CFG1_PWM_AFC) {
684                         data->pwm1.enable = 2;
685                         data->pwm1.auto_pwm_min = 
686                                 PWM_MIN_FROM_REG(data->pwm1.pwm);
687                 }
688                 /* Read the GPIO config */
689                 data->config2 = adm1026_read_value(client, 
690                         ADM1026_REG_CONFIG2);
691                 data->config3 = adm1026_read_value(client, 
692                         ADM1026_REG_CONFIG3);
693                 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
694
695                 value = 0;
696                 for (i = 0;i <= 15;++i) {
697                         if ((i & 0x03) == 0) {
698                                 value = adm1026_read_value(client,
699                                             ADM1026_REG_GPIO_CFG_0_3 + i/4);
700                         }
701                         data->gpio_config[i] = value & 0x03;
702                         value >>= 2;
703                 }
704
705                 data->last_config = jiffies;
706         };  /* last_config */
707
708         dev_dbg(&client->dev, "(%d): Setting VID from GPIO11-15.\n", 
709                 client->id);
710         data->vid = (data->gpio >> 11) & 0x1f;
711         data->valid = 1;
712         up(&data->update_lock);
713         return data;
714 }
715
716 static ssize_t show_in(struct device *dev, char *buf, int nr)
717 {
718         struct adm1026_data *data = adm1026_update_device(dev);
719         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in[nr]));
720 }
721 static ssize_t show_in_min(struct device *dev, char *buf, int nr) 
722 {
723         struct adm1026_data *data = adm1026_update_device(dev); 
724         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]));
725 }
726 static ssize_t set_in_min(struct device *dev, const char *buf, 
727                 size_t count, int nr)
728 {
729         struct i2c_client *client = to_i2c_client(dev);
730         struct adm1026_data *data = i2c_get_clientdata(client);
731         int     val;
732
733         down(&data->update_lock);
734         val = simple_strtol(buf, NULL, 10);
735         data->in_min[nr] = INS_TO_REG(nr, val);
736         adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
737         up(&data->update_lock);
738         return count; 
739 }
740 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
741 {
742         struct adm1026_data *data = adm1026_update_device(dev);
743         return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]));
744 }
745 static ssize_t set_in_max(struct device *dev, const char *buf,
746                 size_t count, int nr)
747 {
748         struct i2c_client *client = to_i2c_client(dev);
749         struct adm1026_data *data = i2c_get_clientdata(client);
750         int     val;
751
752         down(&data->update_lock);
753         val = simple_strtol(buf, NULL, 10);
754         data->in_max[nr] = INS_TO_REG(nr, val);
755         adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
756         up(&data->update_lock);
757         return count;
758 }
759
760 #define in_reg(offset)                                                    \
761 static ssize_t show_in##offset (struct device *dev, char *buf)            \
762 {                                                                         \
763         return show_in(dev, buf, offset);                                 \
764 }                                                                         \
765 static ssize_t show_in##offset##_min (struct device *dev, char *buf)      \
766 {                                                                         \
767         return show_in_min(dev, buf, offset);                             \
768 }                                                                         \
769 static ssize_t set_in##offset##_min (struct device *dev,                  \
770         const char *buf, size_t count)                                    \
771 {                                                                         \
772         return set_in_min(dev, buf, count, offset);                       \
773 }                                                                         \
774 static ssize_t show_in##offset##_max (struct device *dev, char *buf)      \
775 {                                                                         \
776         return show_in_max(dev, buf, offset);                             \
777 }                                                                         \
778 static ssize_t set_in##offset##_max (struct device *dev,                  \
779         const char *buf, size_t count)                                    \
780 {                                                                         \
781         return set_in_max(dev, buf, count, offset);                       \
782 }                                                                         \
783 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);   \
784 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,                   \
785                 show_in##offset##_min, set_in##offset##_min);             \
786 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,                   \
787                 show_in##offset##_max, set_in##offset##_max);
788
789
790 in_reg(0);
791 in_reg(1);
792 in_reg(2);
793 in_reg(3);
794 in_reg(4);
795 in_reg(5);
796 in_reg(6);
797 in_reg(7);
798 in_reg(8);
799 in_reg(9);
800 in_reg(10);
801 in_reg(11);
802 in_reg(12);
803 in_reg(13);
804 in_reg(14);
805 in_reg(15);
806
807 static ssize_t show_in16(struct device *dev, char *buf)
808 {
809         struct adm1026_data *data = adm1026_update_device(dev);
810         return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in[16]) -
811                 NEG12_OFFSET);
812 }
813 static ssize_t show_in16_min(struct device *dev, char *buf) 
814 {
815         struct adm1026_data *data = adm1026_update_device(dev); 
816         return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_min[16])
817                 - NEG12_OFFSET);
818 }
819 static ssize_t set_in16_min(struct device *dev, const char *buf, size_t count)
820 {
821         struct i2c_client *client = to_i2c_client(dev);
822         struct adm1026_data *data = i2c_get_clientdata(client);
823         int     val;
824
825         down(&data->update_lock);
826         val = simple_strtol(buf, NULL, 10);
827         data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
828         adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
829         up(&data->update_lock);
830         return count; 
831 }
832 static ssize_t show_in16_max(struct device *dev, char *buf)
833 {
834         struct adm1026_data *data = adm1026_update_device(dev);
835         return sprintf(buf,"%d\n", INS_FROM_REG(16, data->in_max[16])
836                         - NEG12_OFFSET);
837 }
838 static ssize_t set_in16_max(struct device *dev, const char *buf, size_t count)
839 {
840         struct i2c_client *client = to_i2c_client(dev);
841         struct adm1026_data *data = i2c_get_clientdata(client);
842         int     val;
843
844         down(&data->update_lock);
845         val = simple_strtol(buf, NULL, 10);
846         data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
847         adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
848         up(&data->update_lock);
849         return count;
850 }
851
852 static DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL);
853 static DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min);
854 static DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max);
855
856
857
858
859 /* Now add fan read/write functions */
860
861 static ssize_t show_fan(struct device *dev, char *buf, int nr)
862 {
863         struct adm1026_data *data = adm1026_update_device(dev);
864         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
865                 data->fan_div[nr]));
866 }
867 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
868 {
869         struct adm1026_data *data = adm1026_update_device(dev);
870         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
871                 data->fan_div[nr]));
872 }
873 static ssize_t set_fan_min(struct device *dev, const char *buf,
874                 size_t count, int nr)
875 {
876         struct i2c_client *client = to_i2c_client(dev);
877         struct adm1026_data *data = i2c_get_clientdata(client);
878         int     val;
879
880         down(&data->update_lock);
881         val = simple_strtol(buf, NULL, 10);
882         data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
883         adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
884                 data->fan_min[nr]);
885         up(&data->update_lock);
886         return count;
887 }
888
889 #define fan_offset(offset)                                                  \
890 static ssize_t show_fan_##offset (struct device *dev, char *buf)            \
891 {                                                                           \
892         return show_fan(dev, buf, offset - 1);                              \
893 }                                                                           \
894 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)      \
895 {                                                                           \
896         return show_fan_min(dev, buf, offset - 1);                          \
897 }                                                                           \
898 static ssize_t set_fan_##offset##_min (struct device *dev,                  \
899         const char *buf, size_t count)                                      \
900 {                                                                           \
901         return set_fan_min(dev, buf, count, offset - 1);                    \
902 }                                                                           \
903 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);  \
904 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                    \
905                 show_fan_##offset##_min, set_fan_##offset##_min);
906
907 fan_offset(1);
908 fan_offset(2);
909 fan_offset(3);
910 fan_offset(4);
911 fan_offset(5);
912 fan_offset(6);
913 fan_offset(7);
914 fan_offset(8);
915
916 /* Adjust fan_min to account for new fan divisor */
917 static void fixup_fan_min(struct device *dev, int fan, int old_div)
918 {
919         struct i2c_client *client = to_i2c_client(dev);
920         struct adm1026_data *data = i2c_get_clientdata(client);
921         int    new_min;
922         int    new_div = data->fan_div[fan];
923
924         /* 0 and 0xff are special.  Don't adjust them */
925         if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
926                 return;
927         }
928
929         new_min = data->fan_min[fan] * old_div / new_div;
930         new_min = SENSORS_LIMIT(new_min, 1, 254);
931         data->fan_min[fan] = new_min;
932         adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
933 }
934
935 /* Now add fan_div read/write functions */
936 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
937 {
938         struct adm1026_data *data = adm1026_update_device(dev);
939         return sprintf(buf,"%d\n", data->fan_div[nr]);
940 }
941 static ssize_t set_fan_div(struct device *dev, const char *buf,
942                 size_t count, int nr)
943 {
944         struct i2c_client *client = to_i2c_client(dev);
945         struct adm1026_data *data = i2c_get_clientdata(client);
946         int    val,orig_div,new_div,shift;
947
948         val = simple_strtol(buf, NULL, 10);
949         new_div = DIV_TO_REG(val); 
950         if (new_div == 0) {
951                 return -EINVAL;
952         }
953         down(&data->update_lock);
954         orig_div = data->fan_div[nr];
955         data->fan_div[nr] = DIV_FROM_REG(new_div);
956
957         if (nr < 4) { /* 0 <= nr < 4 */
958                 shift = 2 * nr;
959                 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
960                         ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
961                         (new_div << shift)));
962         } else { /* 3 < nr < 8 */
963                 shift = 2 * (nr - 4);
964                 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
965                         ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
966                         (new_div << shift)));
967         }
968
969         if (data->fan_div[nr] != orig_div) {
970                 fixup_fan_min(dev,nr,orig_div);
971         }
972         up(&data->update_lock);
973         return count;
974 }
975
976 #define fan_offset_div(offset)                                          \
977 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf)  \
978 {                                                                       \
979         return show_fan_div(dev, buf, offset - 1);                      \
980 }                                                                       \
981 static ssize_t set_fan_##offset##_div (struct device *dev,              \
982         const char *buf, size_t count)                                  \
983 {                                                                       \
984         return set_fan_div(dev, buf, count, offset - 1);                \
985 }                                                                       \
986 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,                \
987                 show_fan_##offset##_div, set_fan_##offset##_div);
988
989 fan_offset_div(1);
990 fan_offset_div(2);
991 fan_offset_div(3);
992 fan_offset_div(4);
993 fan_offset_div(5);
994 fan_offset_div(6);
995 fan_offset_div(7);
996 fan_offset_div(8);
997
998 /* Temps */
999 static ssize_t show_temp(struct device *dev, char *buf, int nr)
1000 {
1001         struct adm1026_data *data = adm1026_update_device(dev);
1002         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp[nr]));
1003 }
1004 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
1005 {
1006         struct adm1026_data *data = adm1026_update_device(dev);
1007         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]));
1008 }
1009 static ssize_t set_temp_min(struct device *dev, const char *buf,
1010                 size_t count, int nr)
1011 {
1012         struct i2c_client *client = to_i2c_client(dev);
1013         struct adm1026_data *data = i2c_get_clientdata(client);
1014         int     val;
1015
1016         down(&data->update_lock);
1017         val = simple_strtol(buf, NULL, 10);
1018         data->temp_min[nr] = TEMP_TO_REG(val);
1019         adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
1020                 data->temp_min[nr]);
1021         up(&data->update_lock);
1022         return count;
1023 }
1024 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
1025 {
1026         struct adm1026_data *data = adm1026_update_device(dev);
1027         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1028 }
1029 static ssize_t set_temp_max(struct device *dev, const char *buf,
1030                 size_t count, int nr)
1031 {
1032         struct i2c_client *client = to_i2c_client(dev);
1033         struct adm1026_data *data = i2c_get_clientdata(client);
1034         int     val;
1035
1036         down(&data->update_lock);
1037         val = simple_strtol(buf, NULL, 10);
1038         data->temp_max[nr] = TEMP_TO_REG(val);
1039         adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1040                 data->temp_max[nr]);
1041         up(&data->update_lock);
1042         return count;
1043 }
1044 #define temp_reg(offset)                                                      \
1045 static ssize_t show_temp_##offset (struct device *dev, char *buf)             \
1046 {                                                                             \
1047         return show_temp(dev, buf, offset - 1);                               \
1048 }                                                                             \
1049 static ssize_t show_temp_##offset##_min (struct device *dev, char *buf)       \
1050 {                                                                             \
1051         return show_temp_min(dev, buf, offset - 1);                           \
1052 }                                                                             \
1053 static ssize_t show_temp_##offset##_max (struct device *dev, char *buf)       \
1054 {                                                                             \
1055         return show_temp_max(dev, buf, offset - 1);                           \
1056 }                                                                             \
1057 static ssize_t set_temp_##offset##_min (struct device *dev,                   \
1058         const char *buf, size_t count)                                        \
1059 {                                                                             \
1060         return set_temp_min(dev, buf, count, offset - 1);                     \
1061 }                                                                             \
1062 static ssize_t set_temp_##offset##_max (struct device *dev,                   \
1063         const char *buf, size_t count)                                        \
1064 {                                                                             \
1065         return set_temp_max(dev, buf, count, offset - 1);                     \
1066 }                                                                             \
1067 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL);  \
1068 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,                     \
1069                 show_temp_##offset##_min, set_temp_##offset##_min);           \
1070 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,                     \
1071                 show_temp_##offset##_max, set_temp_##offset##_max);
1072
1073
1074 temp_reg(1);
1075 temp_reg(2);
1076 temp_reg(3);
1077
1078 static ssize_t show_temp_offset(struct device *dev, char *buf, int nr)
1079 {
1080         struct adm1026_data *data = adm1026_update_device(dev);
1081         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1082 }
1083 static ssize_t set_temp_offset(struct device *dev, const char *buf,
1084                 size_t count, int nr)
1085 {
1086         struct i2c_client *client = to_i2c_client(dev);
1087         struct adm1026_data *data = i2c_get_clientdata(client);
1088         int     val;
1089
1090         down(&data->update_lock);
1091         val = simple_strtol(buf, NULL, 10);
1092         data->temp_offset[nr] = TEMP_TO_REG(val);
1093         adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1094                 data->temp_offset[nr]);
1095         up(&data->update_lock);
1096         return count;
1097 }
1098
1099 #define temp_offset_reg(offset)                                             \
1100 static ssize_t show_temp_##offset##_offset (struct device *dev, char *buf)  \
1101 {                                                                           \
1102         return show_temp_offset(dev, buf, offset - 1);                      \
1103 }                                                                           \
1104 static ssize_t set_temp_##offset##_offset (struct device *dev,              \
1105         const char *buf, size_t count)                                      \
1106 {                                                                           \
1107         return set_temp_offset(dev, buf, count, offset - 1);                \
1108 }                                                                           \
1109 static DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,                \
1110                 show_temp_##offset##_offset, set_temp_##offset##_offset);
1111
1112 temp_offset_reg(1);
1113 temp_offset_reg(2);
1114 temp_offset_reg(3);
1115
1116 static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev, char *buf,
1117                 int nr)
1118 {
1119         struct adm1026_data *data = adm1026_update_device(dev);
1120         return sprintf(buf,"%d\n", TEMP_FROM_REG(
1121                 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1122 }
1123 static ssize_t show_temp_auto_point2_temp(struct device *dev, char *buf,
1124                 int nr)
1125 {
1126         struct adm1026_data *data = adm1026_update_device(dev);
1127         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1128                 ADM1026_FAN_CONTROL_TEMP_RANGE));
1129 }
1130 static ssize_t show_temp_auto_point1_temp(struct device *dev, char *buf,
1131                 int nr)
1132 {
1133         struct adm1026_data *data = adm1026_update_device(dev);
1134         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1135 }
1136 static ssize_t set_temp_auto_point1_temp(struct device *dev, const char *buf,
1137                 size_t count, int nr)
1138 {
1139         struct i2c_client *client = to_i2c_client(dev);
1140         struct adm1026_data *data = i2c_get_clientdata(client);
1141         int     val;
1142
1143         down(&data->update_lock);
1144         val = simple_strtol(buf, NULL, 10);
1145         data->temp_tmin[nr] = TEMP_TO_REG(val);
1146         adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1147                 data->temp_tmin[nr]);
1148         up(&data->update_lock);
1149         return count;
1150 }
1151
1152 #define temp_auto_point(offset)                                             \
1153 static ssize_t show_temp##offset##_auto_point1_temp (struct device *dev,    \
1154         char *buf)                                                          \
1155 {                                                                           \
1156         return show_temp_auto_point1_temp(dev, buf, offset - 1);            \
1157 }                                                                           \
1158 static ssize_t set_temp##offset##_auto_point1_temp (struct device *dev,     \
1159         const char *buf, size_t count)                                      \
1160 {                                                                           \
1161         return set_temp_auto_point1_temp(dev, buf, count, offset - 1);      \
1162 }                                                                           \
1163 static ssize_t show_temp##offset##_auto_point1_temp_hyst (struct device     \
1164         *dev, char *buf)                                                    \
1165 {                                                                           \
1166         return show_temp_auto_point1_temp_hyst(dev, buf, offset - 1);       \
1167 }                                                                           \
1168 static ssize_t show_temp##offset##_auto_point2_temp (struct device *dev,    \
1169          char *buf)                                                         \
1170 {                                                                           \
1171         return show_temp_auto_point2_temp(dev, buf, offset - 1);            \
1172 }                                                                           \
1173 static DEVICE_ATTR(temp##offset##_auto_point1_temp, S_IRUGO | S_IWUSR,      \
1174                 show_temp##offset##_auto_point1_temp,                       \
1175                 set_temp##offset##_auto_point1_temp);                       \
1176 static DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,           \
1177                 show_temp##offset##_auto_point1_temp_hyst, NULL);           \
1178 static DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO,                \
1179                 show_temp##offset##_auto_point2_temp, NULL);
1180
1181 temp_auto_point(1);
1182 temp_auto_point(2);
1183 temp_auto_point(3);
1184
1185 static ssize_t show_temp_crit_enable(struct device *dev, char *buf)
1186 {
1187         struct adm1026_data *data = adm1026_update_device(dev);
1188         return sprintf(buf,"%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1189 }
1190 static ssize_t set_temp_crit_enable(struct device *dev, const char *buf,
1191                 size_t count)
1192 {
1193         struct i2c_client *client = to_i2c_client(dev);
1194         struct adm1026_data *data = i2c_get_clientdata(client);
1195         int     val;
1196
1197         val = simple_strtol(buf, NULL, 10);
1198         if ((val == 1) || (val==0)) {
1199                 down(&data->update_lock);
1200                 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1201                 adm1026_write_value(client, ADM1026_REG_CONFIG1, 
1202                         data->config1);
1203                 up(&data->update_lock);
1204         }
1205         return count;
1206 }
1207
1208 static DEVICE_ATTR(temp1_crit_enable, S_IRUGO | S_IWUSR, 
1209         show_temp_crit_enable, set_temp_crit_enable);
1210
1211 static DEVICE_ATTR(temp2_crit_enable, S_IRUGO | S_IWUSR, 
1212         show_temp_crit_enable, set_temp_crit_enable);
1213
1214 static DEVICE_ATTR(temp3_crit_enable, S_IRUGO | S_IWUSR, 
1215         show_temp_crit_enable, set_temp_crit_enable);
1216
1217
1218 static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
1219 {
1220         struct adm1026_data *data = adm1026_update_device(dev);
1221         return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1222 }
1223 static ssize_t set_temp_crit(struct device *dev, const char *buf,
1224                 size_t count, int nr)
1225 {
1226         struct i2c_client *client = to_i2c_client(dev);
1227         struct adm1026_data *data = i2c_get_clientdata(client);
1228         int     val;
1229
1230         down(&data->update_lock);
1231         val = simple_strtol(buf, NULL, 10);
1232         data->temp_crit[nr] = TEMP_TO_REG(val);
1233         adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1234                 data->temp_crit[nr]);
1235         up(&data->update_lock);
1236         return count;
1237 }
1238
1239 #define temp_crit_reg(offset)                                             \
1240 static ssize_t show_temp_##offset##_crit (struct device *dev, char *buf)  \
1241 {                                                                         \
1242         return show_temp_crit(dev, buf, offset - 1);                      \
1243 }                                                                         \
1244 static ssize_t set_temp_##offset##_crit (struct device *dev,              \
1245         const char *buf, size_t count)                                    \
1246 {                                                                         \
1247         return set_temp_crit(dev, buf, count, offset - 1);                \
1248 }                                                                         \
1249 static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,                \
1250                 show_temp_##offset##_crit, set_temp_##offset##_crit);
1251
1252 temp_crit_reg(1);
1253 temp_crit_reg(2);
1254 temp_crit_reg(3);
1255
1256 static ssize_t show_analog_out_reg(struct device *dev, char *buf)
1257 {
1258         struct adm1026_data *data = adm1026_update_device(dev);
1259         return sprintf(buf,"%d\n", DAC_FROM_REG(data->analog_out));
1260 }
1261 static ssize_t set_analog_out_reg(struct device *dev, const char *buf,
1262                 size_t count)
1263 {
1264         struct i2c_client *client = to_i2c_client(dev);
1265         struct adm1026_data *data = i2c_get_clientdata(client);
1266         int     val;
1267
1268         down(&data->update_lock);
1269         val = simple_strtol(buf, NULL, 10);
1270         data->analog_out = DAC_TO_REG(val);
1271         adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1272         up(&data->update_lock);
1273         return count;
1274 }
1275
1276 static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg, 
1277         set_analog_out_reg);
1278
1279 static ssize_t show_vid_reg(struct device *dev, char *buf)
1280 {
1281         struct adm1026_data *data = adm1026_update_device(dev);
1282         return sprintf(buf,"%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
1283 }
1284
1285 static DEVICE_ATTR(vid, S_IRUGO, show_vid_reg, NULL);
1286
1287 static ssize_t show_vrm_reg(struct device *dev, char *buf)
1288 {
1289         struct adm1026_data *data = adm1026_update_device(dev);
1290         return sprintf(buf,"%d\n", data->vrm);
1291 }
1292 static ssize_t store_vrm_reg(struct device *dev, const char *buf,
1293                 size_t count)
1294 {
1295         struct i2c_client *client = to_i2c_client(dev);
1296         struct adm1026_data *data = i2c_get_clientdata(client);
1297
1298         data->vrm = simple_strtol(buf, NULL, 10);
1299         return count;
1300 }
1301
1302 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1303
1304 static ssize_t show_alarms_reg(struct device *dev, char *buf)
1305 {
1306         struct adm1026_data *data = adm1026_update_device(dev);
1307         return sprintf(buf, "%ld\n", (long) (data->alarms));
1308 }
1309
1310 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1311
1312 static ssize_t show_alarm_mask(struct device *dev, char *buf)
1313 {
1314         struct adm1026_data *data = adm1026_update_device(dev);
1315         return sprintf(buf,"%ld\n", data->alarm_mask);
1316 }
1317 static ssize_t set_alarm_mask(struct device *dev, const char *buf,
1318                 size_t count)
1319 {
1320         struct i2c_client *client = to_i2c_client(dev);
1321         struct adm1026_data *data = i2c_get_clientdata(client);
1322         int     val;
1323         unsigned long mask;
1324
1325         down(&data->update_lock);
1326         val = simple_strtol(buf, NULL, 10);
1327         data->alarm_mask = val & 0x7fffffff;
1328         mask = data->alarm_mask
1329                 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1330         adm1026_write_value(client, ADM1026_REG_MASK1,
1331                 mask & 0xff);
1332         mask >>= 8;
1333         adm1026_write_value(client, ADM1026_REG_MASK2,
1334                 mask & 0xff);
1335         mask >>= 8;
1336         adm1026_write_value(client, ADM1026_REG_MASK3,
1337                 mask & 0xff);
1338         mask >>= 8;
1339         adm1026_write_value(client, ADM1026_REG_MASK4,
1340                 mask & 0xff);
1341         up(&data->update_lock);
1342         return count;
1343 }
1344
1345 static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1346         set_alarm_mask);
1347
1348
1349 static ssize_t show_gpio(struct device *dev, char *buf)
1350 {
1351         struct adm1026_data *data = adm1026_update_device(dev);
1352         return sprintf(buf,"%ld\n", data->gpio);
1353 }
1354 static ssize_t set_gpio(struct device *dev, const char *buf,
1355                 size_t count)
1356 {
1357         struct i2c_client *client = to_i2c_client(dev);
1358         struct adm1026_data *data = i2c_get_clientdata(client);
1359         int     val;
1360         long   gpio;
1361
1362         down(&data->update_lock);
1363         val = simple_strtol(buf, NULL, 10);
1364         data->gpio = val & 0x1ffff;
1365         gpio = data->gpio;
1366         adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7,gpio & 0xff);
1367         gpio >>= 8;
1368         adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15,gpio & 0xff);
1369         gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1370         adm1026_write_value(client, ADM1026_REG_STATUS4,gpio & 0xff);
1371         up(&data->update_lock);
1372         return count;
1373 }
1374
1375 static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1376
1377
1378 static ssize_t show_gpio_mask(struct device *dev, char *buf)
1379 {
1380         struct adm1026_data *data = adm1026_update_device(dev);
1381         return sprintf(buf,"%ld\n", data->gpio_mask);
1382 }
1383 static ssize_t set_gpio_mask(struct device *dev, const char *buf,
1384                 size_t count)
1385 {
1386         struct i2c_client *client = to_i2c_client(dev);
1387         struct adm1026_data *data = i2c_get_clientdata(client);
1388         int     val;
1389         long   mask;
1390
1391         down(&data->update_lock);
1392         val = simple_strtol(buf, NULL, 10);
1393         data->gpio_mask = val & 0x1ffff;
1394         mask = data->gpio_mask;
1395         adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7,mask & 0xff);
1396         mask >>= 8;
1397         adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15,mask & 0xff);
1398         mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1399         adm1026_write_value(client, ADM1026_REG_MASK1,mask & 0xff);
1400         up(&data->update_lock);
1401         return count;
1402 }
1403
1404 static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1405
1406 static ssize_t show_pwm_reg(struct device *dev, char *buf)
1407 {
1408         struct adm1026_data *data = adm1026_update_device(dev);
1409         return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm1.pwm));
1410 }
1411 static ssize_t set_pwm_reg(struct device *dev, const char *buf,
1412                 size_t count)
1413 {
1414         struct i2c_client *client = to_i2c_client(dev);
1415         struct adm1026_data *data = i2c_get_clientdata(client);
1416         int     val;
1417
1418         if (data->pwm1.enable == 1) {
1419                 down(&data->update_lock);
1420                 val = simple_strtol(buf, NULL, 10);
1421                 data->pwm1.pwm = PWM_TO_REG(val);
1422                 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1423                 up(&data->update_lock);
1424         }
1425         return count;
1426 }
1427 static ssize_t show_auto_pwm_min(struct device *dev, char *buf)
1428 {
1429         struct adm1026_data *data = adm1026_update_device(dev);
1430         return sprintf(buf,"%d\n", data->pwm1.auto_pwm_min);
1431 }
1432 static ssize_t set_auto_pwm_min(struct device *dev, const char *buf,
1433                 size_t count)
1434 {
1435         struct i2c_client *client = to_i2c_client(dev);
1436         struct adm1026_data *data = i2c_get_clientdata(client);
1437         int     val;
1438
1439         down(&data->update_lock);
1440         val = simple_strtol(buf, NULL, 10);
1441         data->pwm1.auto_pwm_min = SENSORS_LIMIT(val,0,255);
1442         if (data->pwm1.enable == 2) { /* apply immediately */
1443                 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1444                         PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); 
1445                 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1446         }
1447         up(&data->update_lock);
1448         return count;
1449 }
1450 static ssize_t show_auto_pwm_max(struct device *dev, char *buf)
1451 {
1452         return sprintf(buf,"%d\n", ADM1026_PWM_MAX);
1453 }
1454 static ssize_t show_pwm_enable(struct device *dev, char *buf)
1455 {
1456         struct adm1026_data *data = adm1026_update_device(dev);
1457         return sprintf(buf,"%d\n", data->pwm1.enable);
1458 }
1459 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
1460                 size_t count)
1461 {
1462         struct i2c_client *client = to_i2c_client(dev);
1463         struct adm1026_data *data = i2c_get_clientdata(client);
1464         int     val;
1465         int     old_enable;
1466
1467         val = simple_strtol(buf, NULL, 10);
1468         if ((val >= 0) && (val < 3)) {
1469                 down(&data->update_lock);
1470                 old_enable = data->pwm1.enable;
1471                 data->pwm1.enable = val;
1472                 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1473                                 | ((val == 2) ? CFG1_PWM_AFC : 0);
1474                 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1475                         data->config1);
1476                 if (val == 2) {  /* apply pwm1_auto_pwm_min to pwm1 */
1477                         data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1478                                 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min)); 
1479                         adm1026_write_value(client, ADM1026_REG_PWM, 
1480                                 data->pwm1.pwm);
1481                 } else if (!((old_enable == 1) && (val == 1))) {
1482                         /* set pwm to safe value */
1483                         data->pwm1.pwm = 255;
1484                         adm1026_write_value(client, ADM1026_REG_PWM, 
1485                                 data->pwm1.pwm);
1486                 }
1487                 up(&data->update_lock);
1488         }
1489         return count;
1490 }
1491
1492 /* enable PWM fan control */
1493 static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); 
1494 static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); 
1495 static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg); 
1496 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable, 
1497         set_pwm_enable);
1498 static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable, 
1499         set_pwm_enable);
1500 static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable, 
1501         set_pwm_enable);
1502 static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR, 
1503         show_auto_pwm_min, set_auto_pwm_min);
1504 static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR, 
1505         show_auto_pwm_min, set_auto_pwm_min);
1506 static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR, 
1507         show_auto_pwm_min, set_auto_pwm_min);
1508
1509 static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1510 static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1511 static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1512
1513 int adm1026_detect(struct i2c_adapter *adapter, int address,
1514                 int kind)
1515 {
1516         int company, verstep;
1517         struct i2c_client *new_client;
1518         struct adm1026_data *data;
1519         int err = 0;
1520         const char *type_name = "";
1521
1522         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1523                 /* We need to be able to do byte I/O */
1524                 goto exit;
1525         };
1526
1527         /* OK. For now, we presume we have a valid client. We now create the
1528            client structure, even though we cannot fill it completely yet.
1529            But it allows us to access adm1026_{read,write}_value. */
1530
1531         if (!(data = kmalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
1532                 err = -ENOMEM;
1533                 goto exit;
1534         }
1535
1536         memset(data, 0, sizeof(struct adm1026_data));
1537
1538         new_client = &data->client;
1539         i2c_set_clientdata(new_client, data);
1540         new_client->addr = address;
1541         new_client->adapter = adapter;
1542         new_client->driver = &adm1026_driver;
1543         new_client->flags = 0;
1544
1545         /* Now, we do the remaining detection. */
1546
1547         company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
1548         verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
1549
1550         dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
1551                 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1552                 i2c_adapter_id(new_client->adapter), new_client->addr,
1553                 company, verstep);
1554
1555         /* If auto-detecting, Determine the chip type. */
1556         if (kind <= 0) {
1557                 dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
1558                         "...\n", i2c_adapter_id(adapter), address);
1559                 if (company == ADM1026_COMPANY_ANALOG_DEV
1560                     && verstep == ADM1026_VERSTEP_ADM1026) {
1561                         kind = adm1026;
1562                 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1563                         && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1564                         dev_err(&adapter->dev, ": Unrecognized stepping "
1565                                 "0x%02x. Defaulting to ADM1026.\n", verstep);
1566                         kind = adm1026;
1567                 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1568                         dev_err(&adapter->dev, ": Found version/stepping "
1569                                 "0x%02x. Assuming generic ADM1026.\n",
1570                                 verstep);
1571                         kind = any_chip;
1572                 } else {
1573                         dev_dbg(&new_client->dev, ": Autodetection "
1574                                 "failed\n");
1575                         /* Not an ADM1026 ... */
1576                         if (kind == 0)  { /* User used force=x,y */
1577                                 dev_err(&adapter->dev, "Generic ADM1026 not "
1578                                         "found at %d,0x%02x.  Try "
1579                                         "force_adm1026.\n",
1580                                         i2c_adapter_id(adapter), address);
1581                         }
1582                         err = 0;
1583                         goto exitfree;
1584                 }
1585         }
1586
1587         /* Fill in the chip specific driver values */
1588         switch (kind) {
1589         case any_chip :
1590                 type_name = "adm1026";
1591                 break;
1592         case adm1026 :
1593                 type_name = "adm1026";
1594                 break;
1595         default :
1596                 dev_err(&adapter->dev, ": Internal error, invalid "
1597                         "kind (%d)!", kind);
1598                 err = -EFAULT;
1599                 goto exitfree;
1600         }
1601         strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1602
1603         /* Fill in the remaining client fields */
1604         new_client->id = adm1026_id++;
1605         data->type = kind;
1606         data->valid = 0;
1607         init_MUTEX(&data->update_lock);
1608
1609         dev_dbg(&new_client->dev, "(%d): Assigning ID %d to %s at %d,0x%02x\n",
1610                 new_client->id, new_client->id, new_client->name,
1611                 i2c_adapter_id(new_client->adapter),
1612                 new_client->addr);
1613
1614         /* Tell the I2C layer a new client has arrived */
1615         if ((err = i2c_attach_client(new_client)))
1616                 goto exitfree;
1617
1618         /* Set the VRM version */
1619         data->vrm = i2c_which_vrm();
1620
1621         /* Initialize the ADM1026 chip */
1622         adm1026_init_client(new_client);
1623
1624         /* Register sysfs hooks */
1625         device_create_file(&new_client->dev, &dev_attr_in0_input);
1626         device_create_file(&new_client->dev, &dev_attr_in0_max);
1627         device_create_file(&new_client->dev, &dev_attr_in0_min);
1628         device_create_file(&new_client->dev, &dev_attr_in1_input);
1629         device_create_file(&new_client->dev, &dev_attr_in1_max);
1630         device_create_file(&new_client->dev, &dev_attr_in1_min);
1631         device_create_file(&new_client->dev, &dev_attr_in2_input);
1632         device_create_file(&new_client->dev, &dev_attr_in2_max);
1633         device_create_file(&new_client->dev, &dev_attr_in2_min);
1634         device_create_file(&new_client->dev, &dev_attr_in3_input);
1635         device_create_file(&new_client->dev, &dev_attr_in3_max);
1636         device_create_file(&new_client->dev, &dev_attr_in3_min);
1637         device_create_file(&new_client->dev, &dev_attr_in4_input);
1638         device_create_file(&new_client->dev, &dev_attr_in4_max);
1639         device_create_file(&new_client->dev, &dev_attr_in4_min);
1640         device_create_file(&new_client->dev, &dev_attr_in5_input);
1641         device_create_file(&new_client->dev, &dev_attr_in5_max);
1642         device_create_file(&new_client->dev, &dev_attr_in5_min);
1643         device_create_file(&new_client->dev, &dev_attr_in6_input);
1644         device_create_file(&new_client->dev, &dev_attr_in6_max);
1645         device_create_file(&new_client->dev, &dev_attr_in6_min);
1646         device_create_file(&new_client->dev, &dev_attr_in7_input);
1647         device_create_file(&new_client->dev, &dev_attr_in7_max);
1648         device_create_file(&new_client->dev, &dev_attr_in7_min);
1649         device_create_file(&new_client->dev, &dev_attr_in8_input);
1650         device_create_file(&new_client->dev, &dev_attr_in8_max);
1651         device_create_file(&new_client->dev, &dev_attr_in8_min);
1652         device_create_file(&new_client->dev, &dev_attr_in9_input);
1653         device_create_file(&new_client->dev, &dev_attr_in9_max);
1654         device_create_file(&new_client->dev, &dev_attr_in9_min);
1655         device_create_file(&new_client->dev, &dev_attr_in10_input);
1656         device_create_file(&new_client->dev, &dev_attr_in10_max);
1657         device_create_file(&new_client->dev, &dev_attr_in10_min);
1658         device_create_file(&new_client->dev, &dev_attr_in11_input);
1659         device_create_file(&new_client->dev, &dev_attr_in11_max);
1660         device_create_file(&new_client->dev, &dev_attr_in11_min);
1661         device_create_file(&new_client->dev, &dev_attr_in12_input);
1662         device_create_file(&new_client->dev, &dev_attr_in12_max);
1663         device_create_file(&new_client->dev, &dev_attr_in12_min);
1664         device_create_file(&new_client->dev, &dev_attr_in13_input);
1665         device_create_file(&new_client->dev, &dev_attr_in13_max);
1666         device_create_file(&new_client->dev, &dev_attr_in13_min);
1667         device_create_file(&new_client->dev, &dev_attr_in14_input);
1668         device_create_file(&new_client->dev, &dev_attr_in14_max);
1669         device_create_file(&new_client->dev, &dev_attr_in14_min);
1670         device_create_file(&new_client->dev, &dev_attr_in15_input);
1671         device_create_file(&new_client->dev, &dev_attr_in15_max);
1672         device_create_file(&new_client->dev, &dev_attr_in15_min);
1673         device_create_file(&new_client->dev, &dev_attr_in16_input);
1674         device_create_file(&new_client->dev, &dev_attr_in16_max);
1675         device_create_file(&new_client->dev, &dev_attr_in16_min);
1676         device_create_file(&new_client->dev, &dev_attr_fan1_input);
1677         device_create_file(&new_client->dev, &dev_attr_fan1_div);
1678         device_create_file(&new_client->dev, &dev_attr_fan1_min);
1679         device_create_file(&new_client->dev, &dev_attr_fan2_input);
1680         device_create_file(&new_client->dev, &dev_attr_fan2_div);
1681         device_create_file(&new_client->dev, &dev_attr_fan2_min);
1682         device_create_file(&new_client->dev, &dev_attr_fan3_input);
1683         device_create_file(&new_client->dev, &dev_attr_fan3_div);
1684         device_create_file(&new_client->dev, &dev_attr_fan3_min);
1685         device_create_file(&new_client->dev, &dev_attr_fan4_input);
1686         device_create_file(&new_client->dev, &dev_attr_fan4_div);
1687         device_create_file(&new_client->dev, &dev_attr_fan4_min);
1688         device_create_file(&new_client->dev, &dev_attr_fan5_input);
1689         device_create_file(&new_client->dev, &dev_attr_fan5_div);
1690         device_create_file(&new_client->dev, &dev_attr_fan5_min);
1691         device_create_file(&new_client->dev, &dev_attr_fan6_input);
1692         device_create_file(&new_client->dev, &dev_attr_fan6_div);
1693         device_create_file(&new_client->dev, &dev_attr_fan6_min);
1694         device_create_file(&new_client->dev, &dev_attr_fan7_input);
1695         device_create_file(&new_client->dev, &dev_attr_fan7_div);
1696         device_create_file(&new_client->dev, &dev_attr_fan7_min);
1697         device_create_file(&new_client->dev, &dev_attr_fan8_input);
1698         device_create_file(&new_client->dev, &dev_attr_fan8_div);
1699         device_create_file(&new_client->dev, &dev_attr_fan8_min);
1700         device_create_file(&new_client->dev, &dev_attr_temp1_input);
1701         device_create_file(&new_client->dev, &dev_attr_temp1_max);
1702         device_create_file(&new_client->dev, &dev_attr_temp1_min);
1703         device_create_file(&new_client->dev, &dev_attr_temp2_input);
1704         device_create_file(&new_client->dev, &dev_attr_temp2_max);
1705         device_create_file(&new_client->dev, &dev_attr_temp2_min);
1706         device_create_file(&new_client->dev, &dev_attr_temp3_input);
1707         device_create_file(&new_client->dev, &dev_attr_temp3_max);
1708         device_create_file(&new_client->dev, &dev_attr_temp3_min);
1709         device_create_file(&new_client->dev, &dev_attr_temp1_offset);
1710         device_create_file(&new_client->dev, &dev_attr_temp2_offset);
1711         device_create_file(&new_client->dev, &dev_attr_temp3_offset);
1712         device_create_file(&new_client->dev, 
1713                 &dev_attr_temp1_auto_point1_temp);
1714         device_create_file(&new_client->dev, 
1715                 &dev_attr_temp2_auto_point1_temp);
1716         device_create_file(&new_client->dev, 
1717                 &dev_attr_temp3_auto_point1_temp);
1718         device_create_file(&new_client->dev,
1719                 &dev_attr_temp1_auto_point1_temp_hyst);
1720         device_create_file(&new_client->dev,
1721                 &dev_attr_temp2_auto_point1_temp_hyst);
1722         device_create_file(&new_client->dev,
1723                 &dev_attr_temp3_auto_point1_temp_hyst);
1724         device_create_file(&new_client->dev, 
1725                 &dev_attr_temp1_auto_point2_temp);
1726         device_create_file(&new_client->dev, 
1727                 &dev_attr_temp2_auto_point2_temp);
1728         device_create_file(&new_client->dev, 
1729                 &dev_attr_temp3_auto_point2_temp);
1730         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
1731         device_create_file(&new_client->dev, &dev_attr_temp2_crit);
1732         device_create_file(&new_client->dev, &dev_attr_temp3_crit);
1733         device_create_file(&new_client->dev, &dev_attr_temp1_crit_enable);
1734         device_create_file(&new_client->dev, &dev_attr_temp2_crit_enable);
1735         device_create_file(&new_client->dev, &dev_attr_temp3_crit_enable);
1736         device_create_file(&new_client->dev, &dev_attr_vid);
1737         device_create_file(&new_client->dev, &dev_attr_vrm);
1738         device_create_file(&new_client->dev, &dev_attr_alarms);
1739         device_create_file(&new_client->dev, &dev_attr_alarm_mask);
1740         device_create_file(&new_client->dev, &dev_attr_gpio);
1741         device_create_file(&new_client->dev, &dev_attr_gpio_mask);
1742         device_create_file(&new_client->dev, &dev_attr_pwm1);
1743         device_create_file(&new_client->dev, &dev_attr_pwm2);
1744         device_create_file(&new_client->dev, &dev_attr_pwm3);
1745         device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
1746         device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
1747         device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
1748         device_create_file(&new_client->dev, &dev_attr_temp1_auto_point1_pwm);
1749         device_create_file(&new_client->dev, &dev_attr_temp2_auto_point1_pwm);
1750         device_create_file(&new_client->dev, &dev_attr_temp3_auto_point1_pwm);
1751         device_create_file(&new_client->dev, &dev_attr_temp1_auto_point2_pwm);
1752         device_create_file(&new_client->dev, &dev_attr_temp2_auto_point2_pwm);
1753         device_create_file(&new_client->dev, &dev_attr_temp3_auto_point2_pwm);
1754         device_create_file(&new_client->dev, &dev_attr_analog_out);
1755         return 0;
1756
1757         /* Error out and cleanup code */
1758 exitfree:
1759         kfree(new_client);
1760 exit:
1761         return err;
1762 }
1763 static int __init sm_adm1026_init(void)
1764 {
1765         return i2c_add_driver(&adm1026_driver);
1766 }
1767
1768 static void  __exit sm_adm1026_exit(void)
1769 {
1770         i2c_del_driver(&adm1026_driver);
1771 }
1772
1773 MODULE_LICENSE("GPL");
1774 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1775               "Justin Thiessen <jthiessen@penguincomputing.com>");
1776 MODULE_DESCRIPTION("ADM1026 driver");
1777
1778 module_init(sm_adm1026_init);
1779 module_exit(sm_adm1026_exit);