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