vserver 1.9.5.x5
[linux-2.6.git] / drivers / i2c / chips / adm1031.c
1 /*
2   adm1031.c - Part of lm_sensors, Linux kernel modules for hardware
3   monitoring
4   Based on lm75.c and lm85.c
5   Supports adm1030 / adm1031
6   Copyright (C) 2004 Alexandre d'Alton <alex@alexdalton.org>
7   Reworked by Jean Delvare <khali@linux-fr.org>
8   
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c-sensor.h>
29
30 /* Following macros takes channel parameter starting from 0 to 2 */
31 #define ADM1031_REG_FAN_SPEED(nr)       (0x08 + (nr))
32 #define ADM1031_REG_FAN_DIV(nr)         (0x20  + (nr))
33 #define ADM1031_REG_PWM                 (0x22)
34 #define ADM1031_REG_FAN_MIN(nr)         (0x10 + (nr))
35
36 #define ADM1031_REG_TEMP_MAX(nr)        (0x14  + 4*(nr))
37 #define ADM1031_REG_TEMP_MIN(nr)        (0x15  + 4*(nr))
38 #define ADM1031_REG_TEMP_CRIT(nr)       (0x16  + 4*(nr))
39
40 #define ADM1031_REG_TEMP(nr)            (0xa + (nr))
41 #define ADM1031_REG_AUTO_TEMP(nr)       (0x24 + (nr))
42
43 #define ADM1031_REG_STATUS(nr)          (0x2 + (nr))
44
45 #define ADM1031_REG_CONF1               0x0
46 #define ADM1031_REG_CONF2               0x1
47 #define ADM1031_REG_EXT_TEMP            0x6
48
49 #define ADM1031_CONF1_MONITOR_ENABLE    0x01    /* Monitoring enable */
50 #define ADM1031_CONF1_PWM_INVERT        0x08    /* PWM Invert */
51 #define ADM1031_CONF1_AUTO_MODE         0x80    /* Auto FAN */
52
53 #define ADM1031_CONF2_PWM1_ENABLE       0x01
54 #define ADM1031_CONF2_PWM2_ENABLE       0x02
55 #define ADM1031_CONF2_TACH1_ENABLE      0x04
56 #define ADM1031_CONF2_TACH2_ENABLE      0x08
57 #define ADM1031_CONF2_TEMP_ENABLE(chan) (0x10 << (chan))
58
59 /* Addresses to scan */
60 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
61 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
62
63 /* Insmod parameters */
64 SENSORS_INSMOD_2(adm1030, adm1031);
65
66 typedef u8 auto_chan_table_t[8][2];
67
68 /* Each client has this additional data */
69 struct adm1031_data {
70         struct i2c_client client;
71         struct semaphore update_lock;
72         int chip_type;
73         char valid;             /* !=0 if following fields are valid */
74         unsigned long last_updated;     /* In jiffies */
75         /* The chan_select_table contains the possible configurations for
76          * auto fan control.
77          */
78         auto_chan_table_t *chan_select_table;
79         u16 alarm;
80         u8 conf1;
81         u8 conf2;
82         u8 fan[2];
83         u8 fan_div[2];
84         u8 fan_min[2];
85         u8 pwm[2];
86         u8 old_pwm[2];
87         s8 temp[3];
88         u8 ext_temp[3];
89         u8 auto_temp[3];
90         u8 auto_temp_min[3];
91         u8 auto_temp_off[3];
92         u8 auto_temp_max[3];
93         s8 temp_min[3];
94         s8 temp_max[3];
95         s8 temp_crit[3];
96 };
97
98 static int adm1031_attach_adapter(struct i2c_adapter *adapter);
99 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind);
100 static void adm1031_init_client(struct i2c_client *client);
101 static int adm1031_detach_client(struct i2c_client *client);
102 static struct adm1031_data *adm1031_update_device(struct device *dev);
103
104 /* This is the driver that will be inserted */
105 static struct i2c_driver adm1031_driver = {
106         .owner = THIS_MODULE,
107         .name = "adm1031",
108         .flags = I2C_DF_NOTIFY,
109         .attach_adapter = adm1031_attach_adapter,
110         .detach_client = adm1031_detach_client,
111 };
112
113 static int adm1031_id;
114
115 static inline u8 adm1031_read_value(struct i2c_client *client, u8 reg)
116 {
117         return i2c_smbus_read_byte_data(client, reg);
118 }
119
120 static inline int
121 adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
122 {
123         return i2c_smbus_write_byte_data(client, reg, value);
124 }
125
126
127 #define TEMP_TO_REG(val)                (((val) < 0 ? ((val - 500) / 1000) : \
128                                         ((val + 500) / 1000)))
129
130 #define TEMP_FROM_REG(val)              ((val) * 1000)
131
132 #define TEMP_FROM_REG_EXT(val, ext)     (TEMP_FROM_REG(val) + (ext) * 125)
133
134 #define FAN_FROM_REG(reg, div)          ((reg) ? (11250 * 60) / ((reg) * (div)) : 0)
135
136 static int FAN_TO_REG(int reg, int div)
137 {
138         int tmp;
139         tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
140         return tmp > 255 ? 255 : tmp;
141 }
142
143 #define FAN_DIV_FROM_REG(reg)           (1<<(((reg)&0xc0)>>6))
144
145 #define PWM_TO_REG(val)                 (SENSORS_LIMIT((val), 0, 255) >> 4)
146 #define PWM_FROM_REG(val)               ((val) << 4)
147
148 #define FAN_CHAN_FROM_REG(reg)          (((reg) >> 5) & 7)
149 #define FAN_CHAN_TO_REG(val, reg)       \
150         (((reg) & 0x1F) | (((val) << 5) & 0xe0))
151
152 #define AUTO_TEMP_MIN_TO_REG(val, reg)  \
153         ((((val)/500) & 0xf8)|((reg) & 0x7))
154 #define AUTO_TEMP_RANGE_FROM_REG(reg)   (5000 * (1<< ((reg)&0x7)))
155 #define AUTO_TEMP_MIN_FROM_REG(reg)     (1000 * ((((reg) >> 3) & 0x1f) << 2))
156
157 #define AUTO_TEMP_MIN_FROM_REG_DEG(reg) ((((reg) >> 3) & 0x1f) << 2)
158
159 #define AUTO_TEMP_OFF_FROM_REG(reg)             \
160         (AUTO_TEMP_MIN_FROM_REG(reg) - 5000)
161
162 #define AUTO_TEMP_MAX_FROM_REG(reg)             \
163         (AUTO_TEMP_RANGE_FROM_REG(reg) +        \
164         AUTO_TEMP_MIN_FROM_REG(reg))
165
166 static int AUTO_TEMP_MAX_TO_REG(int val, int reg, int pwm)
167 {
168         int ret;
169         int range = val - AUTO_TEMP_MIN_FROM_REG(reg);
170
171         range = ((val - AUTO_TEMP_MIN_FROM_REG(reg))*10)/(16 - pwm);
172         ret = ((reg & 0xf8) |
173                (range < 10000 ? 0 :
174                 range < 20000 ? 1 :
175                 range < 40000 ? 2 : range < 80000 ? 3 : 4));
176         return ret;
177 }
178
179 /* FAN auto control */
180 #define GET_FAN_AUTO_BITFIELD(data, idx)        \
181         (*(data)->chan_select_table)[FAN_CHAN_FROM_REG((data)->conf1)][idx%2]
182
183 /* The tables below contains the possible values for the auto fan 
184  * control bitfields. the index in the table is the register value.
185  * MSb is the auto fan control enable bit, so the four first entries
186  * in the table disables auto fan control when both bitfields are zero.
187  */
188 static auto_chan_table_t auto_channel_select_table_adm1031 = {
189         {0, 0}, {0, 0}, {0, 0}, {0, 0},
190         {2 /*0b010 */ , 4 /*0b100 */ },
191         {2 /*0b010 */ , 2 /*0b010 */ },
192         {4 /*0b100 */ , 4 /*0b100 */ },
193         {7 /*0b111 */ , 7 /*0b111 */ },
194 };
195
196 static auto_chan_table_t auto_channel_select_table_adm1030 = {
197         {0, 0}, {0, 0}, {0, 0}, {0, 0},
198         {2 /*0b10 */            , 0},
199         {0xff /*invalid */      , 0},
200         {0xff /*invalid */      , 0},
201         {3 /*0b11 */            , 0},
202 };
203
204 /* That function checks if a bitfield is valid and returns the other bitfield
205  * nearest match if no exact match where found.
206  */
207 static int
208 get_fan_auto_nearest(struct adm1031_data *data,
209                      int chan, u8 val, u8 reg, u8 * new_reg)
210 {
211         int i;
212         int first_match = -1, exact_match = -1;
213         u8 other_reg_val =
214             (*data->chan_select_table)[FAN_CHAN_FROM_REG(reg)][chan ? 0 : 1];
215
216         if (val == 0) {
217                 *new_reg = 0;
218                 return 0;
219         }
220
221         for (i = 0; i < 8; i++) {
222                 if ((val == (*data->chan_select_table)[i][chan]) &&
223                     ((*data->chan_select_table)[i][chan ? 0 : 1] ==
224                      other_reg_val)) {
225                         /* We found an exact match */
226                         exact_match = i;
227                         break;
228                 } else if (val == (*data->chan_select_table)[i][chan] &&
229                            first_match == -1) {
230                         /* Save the first match in case of an exact match has not been
231                          * found 
232                          */
233                         first_match = i;
234                 }
235         }
236
237         if (exact_match >= 0) {
238                 *new_reg = exact_match;
239         } else if (first_match >= 0) {
240                 *new_reg = first_match;
241         } else {
242                 return -EINVAL;
243         }
244         return 0;
245 }
246
247 static ssize_t show_fan_auto_channel(struct device *dev, char *buf, int nr)
248 {
249         struct adm1031_data *data = adm1031_update_device(dev);
250         return sprintf(buf, "%d\n", GET_FAN_AUTO_BITFIELD(data, nr));
251 }
252
253 static ssize_t
254 set_fan_auto_channel(struct device *dev, const char *buf, size_t count, int nr)
255 {
256         struct i2c_client *client = to_i2c_client(dev);
257         struct adm1031_data *data = i2c_get_clientdata(client);
258         int val;
259         u8 reg;
260         int ret;
261         u8 old_fan_mode;
262
263         old_fan_mode = data->conf1;
264
265         down(&data->update_lock);
266         val = simple_strtol(buf, NULL, 10);
267         
268         if ((ret = get_fan_auto_nearest(data, nr, val, data->conf1, &reg))) {
269                 up(&data->update_lock);
270                 return ret;
271         }
272         if (((data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1)) & ADM1031_CONF1_AUTO_MODE) ^ 
273             (old_fan_mode & ADM1031_CONF1_AUTO_MODE)) {
274                 if (data->conf1 & ADM1031_CONF1_AUTO_MODE){
275                         /* Switch to Auto Fan Mode 
276                          * Save PWM registers 
277                          * Set PWM registers to 33% Both */
278                         data->old_pwm[0] = data->pwm[0];
279                         data->old_pwm[1] = data->pwm[1];
280                         adm1031_write_value(client, ADM1031_REG_PWM, 0x55);
281                 } else {
282                         /* Switch to Manual Mode */
283                         data->pwm[0] = data->old_pwm[0];
284                         data->pwm[1] = data->old_pwm[1];
285                         /* Restore PWM registers */
286                         adm1031_write_value(client, ADM1031_REG_PWM, 
287                                             data->pwm[0] | (data->pwm[1] << 4));
288                 }
289         }
290         data->conf1 = FAN_CHAN_TO_REG(reg, data->conf1);
291         adm1031_write_value(client, ADM1031_REG_CONF1, data->conf1);
292         up(&data->update_lock);
293         return count;
294 }
295
296 #define fan_auto_channel_offset(offset)                                         \
297 static ssize_t show_fan_auto_channel_##offset (struct device *dev, char *buf)   \
298 {                                                                               \
299         return show_fan_auto_channel(dev, buf, offset - 1);                     \
300 }                                                                               \
301 static ssize_t set_fan_auto_channel_##offset (struct device *dev,               \
302         const char *buf, size_t count)                                          \
303 {                                                                               \
304         return set_fan_auto_channel(dev, buf, count, offset - 1);               \
305 }                                                                               \
306 static DEVICE_ATTR(auto_fan##offset##_channel, S_IRUGO | S_IWUSR,               \
307                    show_fan_auto_channel_##offset,                              \
308                    set_fan_auto_channel_##offset)
309
310 fan_auto_channel_offset(1);
311 fan_auto_channel_offset(2);
312
313 /* Auto Temps */
314 static ssize_t show_auto_temp_off(struct device *dev, char *buf, int nr)
315 {
316         struct adm1031_data *data = adm1031_update_device(dev);
317         return sprintf(buf, "%d\n", 
318                        AUTO_TEMP_OFF_FROM_REG(data->auto_temp[nr]));
319 }
320 static ssize_t show_auto_temp_min(struct device *dev, char *buf, int nr)
321 {
322         struct adm1031_data *data = adm1031_update_device(dev);
323         return sprintf(buf, "%d\n",
324                        AUTO_TEMP_MIN_FROM_REG(data->auto_temp[nr]));
325 }
326 static ssize_t
327 set_auto_temp_min(struct device *dev, const char *buf, size_t count, int nr)
328 {
329         struct i2c_client *client = to_i2c_client(dev);
330         struct adm1031_data *data = i2c_get_clientdata(client);
331         int val;
332
333         down(&data->update_lock);
334         val = simple_strtol(buf, NULL, 10);
335         data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
336         adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
337                             data->auto_temp[nr]);
338         up(&data->update_lock);
339         return count;
340 }
341 static ssize_t show_auto_temp_max(struct device *dev, char *buf, int nr)
342 {
343         struct adm1031_data *data = adm1031_update_device(dev);
344         return sprintf(buf, "%d\n",
345                        AUTO_TEMP_MAX_FROM_REG(data->auto_temp[nr]));
346 }
347 static ssize_t
348 set_auto_temp_max(struct device *dev, const char *buf, size_t count, int nr)
349 {
350         struct i2c_client *client = to_i2c_client(dev);
351         struct adm1031_data *data = i2c_get_clientdata(client);
352         int val;
353
354         down(&data->update_lock);
355         val = simple_strtol(buf, NULL, 10);
356         data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr], data->pwm[nr]);
357         adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
358                             data->temp_max[nr]);
359         up(&data->update_lock);
360         return count;
361 }
362
363 #define auto_temp_reg(offset)                                                   \
364 static ssize_t show_auto_temp_##offset##_off (struct device *dev, char *buf)    \
365 {                                                                               \
366         return show_auto_temp_off(dev, buf, offset - 1);                        \
367 }                                                                               \
368 static ssize_t show_auto_temp_##offset##_min (struct device *dev, char *buf)    \
369 {                                                                               \
370         return show_auto_temp_min(dev, buf, offset - 1);                        \
371 }                                                                               \
372 static ssize_t show_auto_temp_##offset##_max (struct device *dev, char *buf)    \
373 {                                                                               \
374         return show_auto_temp_max(dev, buf, offset - 1);                        \
375 }                                                                               \
376 static ssize_t set_auto_temp_##offset##_min (struct device *dev,                \
377                                              const char *buf, size_t count)     \
378 {                                                                               \
379         return set_auto_temp_min(dev, buf, count, offset - 1);          \
380 }                                                                               \
381 static ssize_t set_auto_temp_##offset##_max (struct device *dev,                \
382                                              const char *buf, size_t count)     \
383 {                                                                               \
384         return set_auto_temp_max(dev, buf, count, offset - 1);          \
385 }                                                                               \
386 static DEVICE_ATTR(auto_temp##offset##_off, S_IRUGO,                            \
387                    show_auto_temp_##offset##_off, NULL);                        \
388 static DEVICE_ATTR(auto_temp##offset##_min, S_IRUGO | S_IWUSR,                  \
389                    show_auto_temp_##offset##_min, set_auto_temp_##offset##_min);\
390 static DEVICE_ATTR(auto_temp##offset##_max, S_IRUGO | S_IWUSR,                  \
391                    show_auto_temp_##offset##_max, set_auto_temp_##offset##_max)
392
393 auto_temp_reg(1);
394 auto_temp_reg(2);
395 auto_temp_reg(3);
396
397 /* pwm */
398 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
399 {
400         struct adm1031_data *data = adm1031_update_device(dev);
401         return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm[nr]));
402 }
403 static ssize_t
404 set_pwm(struct device *dev, const char *buf, size_t count, int nr)
405 {
406         struct i2c_client *client = to_i2c_client(dev);
407         struct adm1031_data *data = i2c_get_clientdata(client);
408         int val;
409         int reg;
410         down(&data->update_lock);
411         val = simple_strtol(buf, NULL, 10);
412         if ((data->conf1 & ADM1031_CONF1_AUTO_MODE) && 
413             (((val>>4) & 0xf) != 5)) {
414                 /* In automatic mode, the only PWM accepted is 33% */
415                 up(&data->update_lock);
416                 return -EINVAL;
417         }
418         data->pwm[nr] = PWM_TO_REG(val);
419         reg = adm1031_read_value(client, ADM1031_REG_PWM);
420         adm1031_write_value(client, ADM1031_REG_PWM,
421                             nr ? ((data->pwm[nr] << 4) & 0xf0) | (reg & 0xf)
422                             : (data->pwm[nr] & 0xf) | (reg & 0xf0));
423         up(&data->update_lock);
424         return count;
425 }
426
427 #define pwm_reg(offset)                                                 \
428 static ssize_t show_pwm_##offset (struct device *dev, char *buf)        \
429 {                                                                       \
430         return show_pwm(dev, buf, offset - 1);                  \
431 }                                                                       \
432 static ssize_t set_pwm_##offset (struct device *dev,                    \
433                                  const char *buf, size_t count)         \
434 {                                                                       \
435         return set_pwm(dev, buf, count, offset - 1);            \
436 }                                                                       \
437 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,                      \
438                    show_pwm_##offset, set_pwm_##offset)
439
440 pwm_reg(1);
441 pwm_reg(2);
442
443 /* Fans */
444
445 /*
446  * That function checks the cases where the fan reading is not
447  * relevent.  It is used to provide 0 as fan reading when the fan is
448  * not supposed to run
449  */
450 static int trust_fan_readings(struct adm1031_data *data, int chan)
451 {
452         int res = 0;
453
454         if (data->conf1 & ADM1031_CONF1_AUTO_MODE) {
455                 switch (data->conf1 & 0x60) {
456                 case 0x00:      /* remote temp1 controls fan1 remote temp2 controls fan2 */
457                         res = data->temp[chan+1] >=
458                               AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[chan+1]);
459                         break;
460                 case 0x20:      /* remote temp1 controls both fans */
461                         res =
462                             data->temp[1] >=
463                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1]);
464                         break;
465                 case 0x40:      /* remote temp2 controls both fans */
466                         res =
467                             data->temp[2] >=
468                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]);
469                         break;
470                 case 0x60:      /* max controls both fans */
471                         res =
472                             data->temp[0] >=
473                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[0])
474                             || data->temp[1] >=
475                             AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[1])
476                             || (data->chip_type == adm1031 
477                                 && data->temp[2] >=
478                                 AUTO_TEMP_MIN_FROM_REG_DEG(data->auto_temp[2]));
479                         break;
480                 }
481         } else {
482                 res = data->pwm[chan] > 0;
483         }
484         return res;
485 }
486
487
488 static ssize_t show_fan(struct device *dev, char *buf, int nr)
489 {
490         struct adm1031_data *data = adm1031_update_device(dev);
491         int value;
492
493         value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
494                                  FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
495         return sprintf(buf, "%d\n", value);
496 }
497
498 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
499 {
500         struct adm1031_data *data = adm1031_update_device(dev);
501         return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[nr]));
502 }
503 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
504 {
505         struct adm1031_data *data = adm1031_update_device(dev);
506         return sprintf(buf, "%d\n",
507                        FAN_FROM_REG(data->fan_min[nr],
508                                     FAN_DIV_FROM_REG(data->fan_div[nr])));
509 }
510 static ssize_t
511 set_fan_min(struct device *dev, const char *buf, size_t count, int nr)
512 {
513         struct i2c_client *client = to_i2c_client(dev);
514         struct adm1031_data *data = i2c_get_clientdata(client);
515         int val;
516
517         down(&data->update_lock);
518         val = simple_strtol(buf, NULL, 10);
519         if (val) {
520                 data->fan_min[nr] = 
521                         FAN_TO_REG(val, FAN_DIV_FROM_REG(data->fan_div[nr]));
522         } else {
523                 data->fan_min[nr] = 0xff;
524         }
525         adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), data->fan_min[nr]);
526         up(&data->update_lock);
527         return count;
528 }
529 static ssize_t
530 set_fan_div(struct device *dev, const char *buf, size_t count, int nr)
531 {
532         struct i2c_client *client = to_i2c_client(dev);
533         struct adm1031_data *data = i2c_get_clientdata(client);
534         int val;
535         u8 tmp;
536         int old_div = FAN_DIV_FROM_REG(data->fan_div[nr]);
537         int new_min;
538
539         val = simple_strtol(buf, NULL, 10);
540         tmp = val == 8 ? 0xc0 :
541               val == 4 ? 0x80 :
542               val == 2 ? 0x40 : 
543               val == 1 ? 0x00 :  
544               0xff;
545         if (tmp == 0xff)
546                 return -EINVAL;
547         down(&data->update_lock);
548         data->fan_div[nr] = (tmp & 0xC0) | (0x3f & data->fan_div[nr]);
549         new_min = data->fan_min[nr] * old_div / 
550                 FAN_DIV_FROM_REG(data->fan_div[nr]);
551         data->fan_min[nr] = new_min > 0xff ? 0xff : new_min;
552         data->fan[nr] = data->fan[nr] * old_div / 
553                 FAN_DIV_FROM_REG(data->fan_div[nr]);
554
555         adm1031_write_value(client, ADM1031_REG_FAN_DIV(nr), 
556                             data->fan_div[nr]);
557         adm1031_write_value(client, ADM1031_REG_FAN_MIN(nr), 
558                             data->fan_min[nr]);
559         up(&data->update_lock);
560         return count;
561 }
562
563 #define fan_offset(offset)                                              \
564 static ssize_t show_fan_##offset (struct device *dev, char *buf)        \
565 {                                                                       \
566         return show_fan(dev, buf, offset - 1);                  \
567 }                                                                       \
568 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf)  \
569 {                                                                       \
570         return show_fan_min(dev, buf, offset - 1);                      \
571 }                                                                       \
572 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf)  \
573 {                                                                       \
574         return show_fan_div(dev, buf, offset - 1);                      \
575 }                                                                       \
576 static ssize_t set_fan_##offset##_min (struct device *dev,              \
577         const char *buf, size_t count)                                  \
578 {                                                                       \
579         return set_fan_min(dev, buf, count, offset - 1);                \
580 }                                                                       \
581 static ssize_t set_fan_##offset##_div (struct device *dev,              \
582         const char *buf, size_t count)                                  \
583 {                                                                       \
584         return set_fan_div(dev, buf, count, offset - 1);                \
585 }                                                                       \
586 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset,     \
587                    NULL);                                               \
588 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
589                    show_fan_##offset##_min, set_fan_##offset##_min);    \
590 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,                \
591                    show_fan_##offset##_div, set_fan_##offset##_div);    \
592 static DEVICE_ATTR(auto_fan##offset##_min_pwm, S_IRUGO | S_IWUSR,       \
593                    show_pwm_##offset, set_pwm_##offset)
594
595 fan_offset(1);
596 fan_offset(2);
597
598
599 /* Temps */
600 static ssize_t show_temp(struct device *dev, char *buf, int nr)
601 {
602         struct adm1031_data *data = adm1031_update_device(dev);
603         int ext;
604         ext = nr == 0 ?
605             ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
606             (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
607         return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
608 }
609 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
610 {
611         struct adm1031_data *data = adm1031_update_device(dev);
612         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
613 }
614 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
615 {
616         struct adm1031_data *data = adm1031_update_device(dev);
617         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
618 }
619 static ssize_t show_temp_crit(struct device *dev, char *buf, int nr)
620 {
621         struct adm1031_data *data = adm1031_update_device(dev);
622         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
623 }
624 static ssize_t
625 set_temp_min(struct device *dev, const char *buf, size_t count, int nr)
626 {
627         struct i2c_client *client = to_i2c_client(dev);
628         struct adm1031_data *data = i2c_get_clientdata(client);
629         int val;
630
631         val = simple_strtol(buf, NULL, 10);
632         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
633         down(&data->update_lock);
634         data->temp_min[nr] = TEMP_TO_REG(val);
635         adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
636                             data->temp_min[nr]);
637         up(&data->update_lock);
638         return count;
639 }
640 static ssize_t
641 set_temp_max(struct device *dev, const char *buf, size_t count, int nr)
642 {
643         struct i2c_client *client = to_i2c_client(dev);
644         struct adm1031_data *data = i2c_get_clientdata(client);
645         int val;
646
647         val = simple_strtol(buf, NULL, 10);
648         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
649         down(&data->update_lock);
650         data->temp_max[nr] = TEMP_TO_REG(val);
651         adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
652                             data->temp_max[nr]);
653         up(&data->update_lock);
654         return count;
655 }
656 static ssize_t
657 set_temp_crit(struct device *dev, const char *buf, size_t count, int nr)
658 {
659         struct i2c_client *client = to_i2c_client(dev);
660         struct adm1031_data *data = i2c_get_clientdata(client);
661         int val;
662
663         val = simple_strtol(buf, NULL, 10);
664         val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
665         down(&data->update_lock);
666         data->temp_crit[nr] = TEMP_TO_REG(val);
667         adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
668                             data->temp_crit[nr]);
669         up(&data->update_lock);
670         return count;
671 }
672
673 #define temp_reg(offset)                                                        \
674 static ssize_t show_temp_##offset (struct device *dev, char *buf)               \
675 {                                                                               \
676         return show_temp(dev, buf, offset - 1);                         \
677 }                                                                               \
678 static ssize_t show_temp_##offset##_min (struct device *dev, char *buf)         \
679 {                                                                               \
680         return show_temp_min(dev, buf, offset - 1);                             \
681 }                                                                               \
682 static ssize_t show_temp_##offset##_max (struct device *dev, char *buf)         \
683 {                                                                               \
684         return show_temp_max(dev, buf, offset - 1);                             \
685 }                                                                               \
686 static ssize_t show_temp_##offset##_crit (struct device *dev, char *buf)        \
687 {                                                                               \
688         return show_temp_crit(dev, buf, offset - 1);                    \
689 }                                                                               \
690 static ssize_t set_temp_##offset##_min (struct device *dev,                     \
691                                         const char *buf, size_t count)          \
692 {                                                                               \
693         return set_temp_min(dev, buf, count, offset - 1);                       \
694 }                                                                               \
695 static ssize_t set_temp_##offset##_max (struct device *dev,                     \
696                                         const char *buf, size_t count)          \
697 {                                                                               \
698         return set_temp_max(dev, buf, count, offset - 1);                       \
699 }                                                                               \
700 static ssize_t set_temp_##offset##_crit (struct device *dev,                    \
701                                          const char *buf, size_t count)         \
702 {                                                                               \
703         return set_temp_crit(dev, buf, count, offset - 1);                      \
704 }                                                                               \
705 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset,           \
706                    NULL);                                                       \
707 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,                       \
708                    show_temp_##offset##_min, set_temp_##offset##_min);          \
709 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,                       \
710                    show_temp_##offset##_max, set_temp_##offset##_max);          \
711 static DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,                      \
712                    show_temp_##offset##_crit, set_temp_##offset##_crit)
713
714 temp_reg(1);
715 temp_reg(2);
716 temp_reg(3);
717
718 /* Alarms */
719 static ssize_t show_alarms(struct device *dev, char *buf)
720 {
721         struct adm1031_data *data = adm1031_update_device(dev);
722         return sprintf(buf, "%d\n", data->alarm);
723 }
724
725 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
726
727
728 static int adm1031_attach_adapter(struct i2c_adapter *adapter)
729 {
730         if (!(adapter->class & I2C_CLASS_HWMON))
731                 return 0;
732         return i2c_detect(adapter, &addr_data, adm1031_detect);
733 }
734
735 /* This function is called by i2c_detect */
736 static int adm1031_detect(struct i2c_adapter *adapter, int address, int kind)
737 {
738         struct i2c_client *new_client;
739         struct adm1031_data *data;
740         int err = 0;
741         const char *name = "";
742
743         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
744                 goto exit;
745
746         if (!(data = kmalloc(sizeof(struct adm1031_data), GFP_KERNEL))) {
747                 err = -ENOMEM;
748                 goto exit;
749         }
750         memset(data, 0, sizeof(struct adm1031_data));
751
752         new_client = &data->client;
753         i2c_set_clientdata(new_client, data);
754         new_client->addr = address;
755         new_client->adapter = adapter;
756         new_client->driver = &adm1031_driver;
757         new_client->flags = 0;
758
759         if (kind < 0) {
760                 int id, co;
761                 id = i2c_smbus_read_byte_data(new_client, 0x3d);
762                 co = i2c_smbus_read_byte_data(new_client, 0x3e);
763
764                 if (!((id == 0x31 || id == 0x30) && co == 0x41))
765                         goto exit_free;
766                 kind = (id == 0x30) ? adm1030 : adm1031;
767         }
768
769         if (kind <= 0)
770                 kind = adm1031;
771
772         /* Given the detected chip type, set the chip name and the
773          * auto fan control helper table. */
774         if (kind == adm1030) {
775                 name = "adm1030";
776                 data->chan_select_table = &auto_channel_select_table_adm1030;
777         } else if (kind == adm1031) {
778                 name = "adm1031";
779                 data->chan_select_table = &auto_channel_select_table_adm1031;
780         }
781         data->chip_type = kind;
782
783         strlcpy(new_client->name, name, I2C_NAME_SIZE);
784
785         new_client->id = adm1031_id++;
786         data->valid = 0;
787         init_MUTEX(&data->update_lock);
788
789         /* Tell the I2C layer a new client has arrived */
790         if ((err = i2c_attach_client(new_client)))
791                 goto exit_free;
792
793         /* Initialize the ADM1031 chip */
794         adm1031_init_client(new_client);
795
796         /* Register sysfs hooks */
797         device_create_file(&new_client->dev, &dev_attr_fan1_input);
798         device_create_file(&new_client->dev, &dev_attr_fan1_div);
799         device_create_file(&new_client->dev, &dev_attr_fan1_min);
800         device_create_file(&new_client->dev, &dev_attr_pwm1);
801         device_create_file(&new_client->dev, &dev_attr_auto_fan1_channel);
802         device_create_file(&new_client->dev, &dev_attr_temp1_input);
803         device_create_file(&new_client->dev, &dev_attr_temp1_min);
804         device_create_file(&new_client->dev, &dev_attr_temp1_max);
805         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
806         device_create_file(&new_client->dev, &dev_attr_temp2_input);
807         device_create_file(&new_client->dev, &dev_attr_temp2_min);
808         device_create_file(&new_client->dev, &dev_attr_temp2_max);
809         device_create_file(&new_client->dev, &dev_attr_temp2_crit);
810
811         device_create_file(&new_client->dev, &dev_attr_auto_temp1_off);
812         device_create_file(&new_client->dev, &dev_attr_auto_temp1_min);
813         device_create_file(&new_client->dev, &dev_attr_auto_temp1_max);
814
815         device_create_file(&new_client->dev, &dev_attr_auto_temp2_off);
816         device_create_file(&new_client->dev, &dev_attr_auto_temp2_min);
817         device_create_file(&new_client->dev, &dev_attr_auto_temp2_max);
818
819         device_create_file(&new_client->dev, &dev_attr_auto_fan1_min_pwm);
820
821         device_create_file(&new_client->dev, &dev_attr_alarms);
822
823         if (kind == adm1031) {
824                 device_create_file(&new_client->dev, &dev_attr_fan2_input);
825                 device_create_file(&new_client->dev, &dev_attr_fan2_div);
826                 device_create_file(&new_client->dev, &dev_attr_fan2_min);
827                 device_create_file(&new_client->dev, &dev_attr_pwm2);
828                 device_create_file(&new_client->dev,
829                                    &dev_attr_auto_fan2_channel);
830                 device_create_file(&new_client->dev, &dev_attr_temp3_input);
831                 device_create_file(&new_client->dev, &dev_attr_temp3_min);
832                 device_create_file(&new_client->dev, &dev_attr_temp3_max);
833                 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
834                 device_create_file(&new_client->dev, &dev_attr_auto_temp3_off);
835                 device_create_file(&new_client->dev, &dev_attr_auto_temp3_min);
836                 device_create_file(&new_client->dev, &dev_attr_auto_temp3_max);
837                 device_create_file(&new_client->dev, &dev_attr_auto_fan2_min_pwm);
838         }
839
840         return 0;
841
842 exit_free:
843         kfree(new_client);
844 exit:
845         return err;
846 }
847
848 static int adm1031_detach_client(struct i2c_client *client)
849 {
850         int ret;
851         if ((ret = i2c_detach_client(client)) != 0) {
852                 return ret;
853         }
854         kfree(client);
855         return 0;
856 }
857
858 static void adm1031_init_client(struct i2c_client *client)
859 {
860         unsigned int read_val;
861         unsigned int mask;
862         struct adm1031_data *data = i2c_get_clientdata(client);
863
864         mask = (ADM1031_CONF2_PWM1_ENABLE | ADM1031_CONF2_TACH1_ENABLE);
865         if (data->chip_type == adm1031) {
866                 mask |= (ADM1031_CONF2_PWM2_ENABLE |
867                         ADM1031_CONF2_TACH2_ENABLE);
868         } 
869         /* Initialize the ADM1031 chip (enables fan speed reading ) */
870         read_val = adm1031_read_value(client, ADM1031_REG_CONF2);
871         if ((read_val | mask) != read_val) {
872             adm1031_write_value(client, ADM1031_REG_CONF2, read_val | mask);
873         }
874
875         read_val = adm1031_read_value(client, ADM1031_REG_CONF1);
876         if ((read_val | ADM1031_CONF1_MONITOR_ENABLE) != read_val) {
877             adm1031_write_value(client, ADM1031_REG_CONF1, read_val |
878                                 ADM1031_CONF1_MONITOR_ENABLE);
879         }
880
881 }
882
883 static struct adm1031_data *adm1031_update_device(struct device *dev)
884 {
885         struct i2c_client *client = to_i2c_client(dev);
886         struct adm1031_data *data = i2c_get_clientdata(client);
887         int chan;
888
889         down(&data->update_lock);
890
891         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
892             (jiffies < data->last_updated) || !data->valid) {
893
894                 dev_dbg(&client->dev, "Starting adm1031 update\n");
895                 for (chan = 0;
896                      chan < ((data->chip_type == adm1031) ? 3 : 2); chan++) {
897                         u8 oldh, newh;
898
899                         oldh =
900                             adm1031_read_value(client, ADM1031_REG_TEMP(chan));
901                         data->ext_temp[chan] =
902                             adm1031_read_value(client, ADM1031_REG_EXT_TEMP);
903                         newh =
904                             adm1031_read_value(client, ADM1031_REG_TEMP(chan));
905                         if (newh != oldh) {
906                                 data->ext_temp[chan] =
907                                     adm1031_read_value(client,
908                                                        ADM1031_REG_EXT_TEMP);
909 #ifdef DEBUG
910                                 oldh =
911                                     adm1031_read_value(client,
912                                                        ADM1031_REG_TEMP(chan));
913
914                                 /* oldh is actually newer */
915                                 if (newh != oldh)
916                                         dev_warn(&client->dev,
917                                                  "Remote temperature may be "
918                                                  "wrong.\n");
919 #endif
920                         }
921                         data->temp[chan] = newh;
922
923                         data->temp_min[chan] =
924                             adm1031_read_value(client,
925                                                ADM1031_REG_TEMP_MIN(chan));
926                         data->temp_max[chan] =
927                             adm1031_read_value(client,
928                                                ADM1031_REG_TEMP_MAX(chan));
929                         data->temp_crit[chan] =
930                             adm1031_read_value(client,
931                                                ADM1031_REG_TEMP_CRIT(chan));
932                         data->auto_temp[chan] =
933                             adm1031_read_value(client,
934                                                ADM1031_REG_AUTO_TEMP(chan));
935
936                 }
937
938                 data->conf1 = adm1031_read_value(client, ADM1031_REG_CONF1);
939                 data->conf2 = adm1031_read_value(client, ADM1031_REG_CONF2);
940
941                 data->alarm = adm1031_read_value(client, ADM1031_REG_STATUS(0))
942                              | (adm1031_read_value(client, ADM1031_REG_STATUS(1))
943                                 << 8);
944                 if (data->chip_type == adm1030) {
945                         data->alarm &= 0xc0ff;
946                 }
947                 
948                 for (chan=0; chan<(data->chip_type == adm1030 ? 1 : 2); chan++) {
949                         data->fan_div[chan] =
950                             adm1031_read_value(client, ADM1031_REG_FAN_DIV(chan));
951                         data->fan_min[chan] =
952                             adm1031_read_value(client, ADM1031_REG_FAN_MIN(chan));
953                         data->fan[chan] =
954                             adm1031_read_value(client, ADM1031_REG_FAN_SPEED(chan));
955                         data->pwm[chan] =
956                             0xf & (adm1031_read_value(client, ADM1031_REG_PWM) >> 
957                                    (4*chan));
958                 }
959                 data->last_updated = jiffies;
960                 data->valid = 1;
961         }
962
963         up(&data->update_lock);
964
965         return data;
966 }
967
968 static int __init sensors_adm1031_init(void)
969 {
970         return i2c_add_driver(&adm1031_driver);
971 }
972
973 static void __exit sensors_adm1031_exit(void)
974 {
975         i2c_del_driver(&adm1031_driver);
976 }
977
978 MODULE_AUTHOR("Alexandre d'Alton <alex@alexdalton.org>");
979 MODULE_DESCRIPTION("ADM1031/ADM1030 driver");
980 MODULE_LICENSE("GPL");
981
982 module_init(sensors_adm1031_init);
983 module_exit(sensors_adm1031_exit);