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