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