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