]> err.no Git - linux-2.6/blob - drivers/hwmon/adm9240.c
82edf286ebdc878e7b4686b4019797efe73d8db7
[linux-2.6] / drivers / hwmon / adm9240.c
1 /*
2  * adm9240.c    Part of lm_sensors, Linux kernel modules for hardware
3  *              monitoring
4  *
5  * Copyright (C) 1999   Frodo Looijaard <frodol@dds.nl>
6  *                      Philip Edelbrock <phil@netroedge.com>
7  * Copyright (C) 2003   Michiel Rook <michiel@grendelproject.nl>
8  * Copyright (C) 2005   Grant Coady <gcoady@gmail.com> with valuable
9  *                              guidance from Jean Delvare
10  *
11  * Driver supports      Analog Devices          ADM9240
12  *                      Dallas Semiconductor    DS1780
13  *                      National Semiconductor  LM81
14  *
15  * ADM9240 is the reference, DS1780 and LM81 are register compatibles
16  *
17  * Voltage      Six inputs are scaled by chip, VID also reported
18  * Temperature  Chip temperature to 0.5'C, maximum and max_hysteris
19  * Fans         2 fans, low speed alarm, automatic fan clock divider
20  * Alarms       16-bit map of active alarms
21  * Analog Out   0..1250 mV output
22  *
23  * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
24  *
25  * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
26  *
27  * LM81 extended temp reading not implemented
28  *
29  * This program is free software; you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation; either version 2 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42  */
43
44 #include <linux/init.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/i2c-sensor.h>
49 #include <linux/i2c-vid.h>
50 #include <linux/hwmon.h>
51 #include <linux/err.h>
52
53 /* Addresses to scan */
54 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
55                                         I2C_CLIENT_END };
56
57 /* Insmod parameters */
58 SENSORS_INSMOD_3(adm9240, ds1780, lm81);
59
60 /* ADM9240 registers */
61 #define ADM9240_REG_MAN_ID              0x3e
62 #define ADM9240_REG_DIE_REV             0x3f
63 #define ADM9240_REG_CONFIG              0x40
64
65 #define ADM9240_REG_IN(nr)              (0x20 + (nr))   /* 0..5 */
66 #define ADM9240_REG_IN_MAX(nr)          (0x2b + (nr) * 2)
67 #define ADM9240_REG_IN_MIN(nr)          (0x2c + (nr) * 2)
68 #define ADM9240_REG_FAN(nr)             (0x28 + (nr))   /* 0..1 */
69 #define ADM9240_REG_FAN_MIN(nr)         (0x3b + (nr))
70 #define ADM9240_REG_INT(nr)             (0x41 + (nr))
71 #define ADM9240_REG_INT_MASK(nr)        (0x43 + (nr))
72 #define ADM9240_REG_TEMP                0x27
73 #define ADM9240_REG_TEMP_HIGH           0x39
74 #define ADM9240_REG_TEMP_HYST           0x3a
75 #define ADM9240_REG_ANALOG_OUT          0x19
76 #define ADM9240_REG_CHASSIS_CLEAR       0x46
77 #define ADM9240_REG_VID_FAN_DIV         0x47
78 #define ADM9240_REG_I2C_ADDR            0x48
79 #define ADM9240_REG_VID4                0x49
80 #define ADM9240_REG_TEMP_CONF           0x4b
81
82 /* generalised scaling with integer rounding */
83 static inline int SCALE(long val, int mul, int div)
84 {
85         if (val < 0)
86                 return (val * mul - div / 2) / div;
87         else
88                 return (val * mul + div / 2) / div;
89 }
90
91 /* adm9240 internally scales voltage measurements */
92 static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
93
94 static inline unsigned int IN_FROM_REG(u8 reg, int n)
95 {
96         return SCALE(reg, nom_mv[n], 192);
97 }
98
99 static inline u8 IN_TO_REG(unsigned long val, int n)
100 {
101         return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
102 }
103
104 /* temperature range: -40..125, 127 disables temperature alarm */
105 static inline s8 TEMP_TO_REG(long val)
106 {
107         return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
108 }
109
110 /* two fans, each with low fan speed limit */
111 static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
112 {
113         if (!reg) /* error */
114                 return -1;
115
116         if (reg == 255)
117                 return 0;
118
119         return SCALE(1350000, 1, reg * div);
120 }
121
122 /* analog out 0..1250mV */
123 static inline u8 AOUT_TO_REG(unsigned long val)
124 {
125         return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
126 }
127
128 static inline unsigned int AOUT_FROM_REG(u8 reg)
129 {
130         return SCALE(reg, 1250, 255);
131 }
132
133 static int adm9240_attach_adapter(struct i2c_adapter *adapter);
134 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
135 static void adm9240_init_client(struct i2c_client *client);
136 static int adm9240_detach_client(struct i2c_client *client);
137 static struct adm9240_data *adm9240_update_device(struct device *dev);
138
139 /* driver data */
140 static struct i2c_driver adm9240_driver = {
141         .owner          = THIS_MODULE,
142         .name           = "adm9240",
143         .id             = I2C_DRIVERID_ADM9240,
144         .flags          = I2C_DF_NOTIFY,
145         .attach_adapter = adm9240_attach_adapter,
146         .detach_client  = adm9240_detach_client,
147 };
148
149 /* per client data */
150 struct adm9240_data {
151         enum chips type;
152         struct i2c_client client;
153         struct class_device *class_dev;
154         struct semaphore update_lock;
155         char valid;
156         unsigned long last_updated_measure;
157         unsigned long last_updated_config;
158
159         u8 in[6];               /* ro   in0_input */
160         u8 in_max[6];           /* rw   in0_max */
161         u8 in_min[6];           /* rw   in0_min */
162         u8 fan[2];              /* ro   fan1_input */
163         u8 fan_min[2];          /* rw   fan1_min */
164         u8 fan_div[2];          /* rw   fan1_div, read-only accessor */
165         s16 temp;               /* ro   temp1_input, 9-bit sign-extended */
166         s8 temp_high;           /* rw   temp1_max */
167         s8 temp_hyst;           /* rw   temp1_max_hyst */
168         u16 alarms;             /* ro   alarms */
169         u8 aout;                /* rw   aout_output */
170         u8 vid;                 /* ro   vid */
171         u8 vrm;                 /* --   vrm set on startup, no accessor */
172 };
173
174 /* i2c byte read/write interface */
175 static int adm9240_read_value(struct i2c_client *client, u8 reg)
176 {
177         return i2c_smbus_read_byte_data(client, reg);
178 }
179
180 static int adm9240_write_value(struct i2c_client *client, u8 reg, u8 value)
181 {
182         return i2c_smbus_write_byte_data(client, reg, value);
183 }
184
185 /*** sysfs accessors ***/
186
187 /* temperature */
188 #define show_temp(value, scale)                                 \
189 static ssize_t show_##value(struct device *dev,                 \
190                             struct device_attribute *attr,      \
191                             char *buf)                          \
192 {                                                               \
193         struct adm9240_data *data = adm9240_update_device(dev); \
194         return sprintf(buf, "%d\n", data->value * scale);       \
195 }
196 show_temp(temp_high, 1000);
197 show_temp(temp_hyst, 1000);
198 show_temp(temp, 500); /* 0.5'C per bit */
199
200 #define set_temp(value, reg)                                    \
201 static ssize_t set_##value(struct device *dev,                  \
202                            struct device_attribute *attr,       \
203                            const char *buf, size_t count)       \
204 {                                                               \
205         struct i2c_client *client = to_i2c_client(dev);         \
206         struct adm9240_data *data = adm9240_update_device(dev); \
207         long temp = simple_strtoul(buf, NULL, 10);              \
208                                                                 \
209         down(&data->update_lock);                               \
210         data->value = TEMP_TO_REG(temp);                        \
211         adm9240_write_value(client, reg, data->value);          \
212         up(&data->update_lock);                                 \
213         return count;                                           \
214 }
215
216 set_temp(temp_high, ADM9240_REG_TEMP_HIGH);
217 set_temp(temp_hyst, ADM9240_REG_TEMP_HYST);
218
219 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
220                 show_temp_high, set_temp_high);
221 static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
222                 show_temp_hyst, set_temp_hyst);
223 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
224
225 /* voltage */
226 static ssize_t show_in(struct device *dev, char *buf, int nr)
227 {
228         struct adm9240_data *data = adm9240_update_device(dev);
229         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
230 }
231
232 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
233 {
234         struct adm9240_data *data = adm9240_update_device(dev);
235         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
236 }
237
238 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
239 {
240         struct adm9240_data *data = adm9240_update_device(dev);
241         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
242 }
243
244 static ssize_t set_in_min(struct device *dev, const char *buf,
245                 size_t count, int nr)
246 {
247         struct i2c_client *client = to_i2c_client(dev);
248         struct adm9240_data *data = i2c_get_clientdata(client);
249         unsigned long val = simple_strtoul(buf, NULL, 10);
250
251         down(&data->update_lock);
252         data->in_min[nr] = IN_TO_REG(val, nr);
253         adm9240_write_value(client, ADM9240_REG_IN_MIN(nr), data->in_min[nr]);
254         up(&data->update_lock);
255         return count;
256 }
257
258 static ssize_t set_in_max(struct device *dev, const char *buf,
259                 size_t count, int nr)
260 {
261         struct i2c_client *client = to_i2c_client(dev);
262         struct adm9240_data *data = i2c_get_clientdata(client);
263         unsigned long val = simple_strtoul(buf, NULL, 10);
264
265         down(&data->update_lock);
266         data->in_max[nr] = IN_TO_REG(val, nr);
267         adm9240_write_value(client, ADM9240_REG_IN_MAX(nr), data->in_max[nr]);
268         up(&data->update_lock);
269         return count;
270 }
271
272 #define show_in_offset(offset)                                          \
273 static ssize_t show_in##offset(struct device *dev,                      \
274                                struct device_attribute *attr,           \
275                                char *buf)                               \
276 {                                                                       \
277         return show_in(dev, buf, offset);                               \
278 }                                                                       \
279 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
280 static ssize_t show_in##offset##_min(struct device *dev,                \
281                                      struct device_attribute *attr,     \
282                                      char *buf)                         \
283 {                                                                       \
284         return show_in_min(dev, buf, offset);                           \
285 }                                                                       \
286 static ssize_t show_in##offset##_max(struct device *dev,                \
287                                      struct device_attribute *attr,     \
288                                      char *buf)                         \
289 {                                                                       \
290         return show_in_max(dev, buf, offset);                           \
291 }                                                                       \
292 static ssize_t                                                          \
293 set_in##offset##_min(struct device *dev,                                \
294                      struct device_attribute *attr, const char *buf,    \
295                      size_t count)                                      \
296 {                                                                       \
297         return set_in_min(dev, buf, count, offset);                     \
298 }                                                                       \
299 static ssize_t                                                          \
300 set_in##offset##_max(struct device *dev,                                \
301                      struct device_attribute *attr, const char *buf,    \
302                      size_t count)                                      \
303 {                                                                       \
304         return set_in_max(dev, buf, count, offset);                     \
305 }                                                                       \
306 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,                 \
307                 show_in##offset##_min, set_in##offset##_min);           \
308 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,                 \
309                 show_in##offset##_max, set_in##offset##_max);
310
311 show_in_offset(0);
312 show_in_offset(1);
313 show_in_offset(2);
314 show_in_offset(3);
315 show_in_offset(4);
316 show_in_offset(5);
317
318 /* fans */
319 static ssize_t show_fan(struct device *dev, char *buf, int nr)
320 {
321         struct adm9240_data *data = adm9240_update_device(dev);
322         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
323                                 1 << data->fan_div[nr]));
324 }
325
326 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
327 {
328         struct adm9240_data *data = adm9240_update_device(dev);
329         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
330                                 1 << data->fan_div[nr]));
331 }
332
333 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
334 {
335         struct adm9240_data *data = adm9240_update_device(dev);
336         return sprintf(buf, "%d\n", 1 << data->fan_div[nr]);
337 }
338
339 /* write new fan div, callers must hold data->update_lock */
340 static void adm9240_write_fan_div(struct i2c_client *client, int nr,
341                 u8 fan_div)
342 {
343         u8 reg, old, shift = (nr + 2) * 2;
344
345         reg = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
346         old = (reg >> shift) & 3;
347         reg &= ~(3 << shift);
348         reg |= (fan_div << shift);
349         adm9240_write_value(client, ADM9240_REG_VID_FAN_DIV, reg);
350         dev_dbg(&client->dev, "fan%d clock divider changed from %u "
351                         "to %u\n", nr + 1, 1 << old, 1 << fan_div);
352 }
353
354 /* 
355  * set fan speed low limit:
356  *
357  * - value is zero: disable fan speed low limit alarm
358  *
359  * - value is below fan speed measurement range: enable fan speed low
360  *   limit alarm to be asserted while fan speed too slow to measure
361  *
362  * - otherwise: select fan clock divider to suit fan speed low limit,
363  *   measurement code may adjust registers to ensure fan speed reading
364  */
365 static ssize_t set_fan_min(struct device *dev, const char *buf,
366                 size_t count, int nr)
367 {
368         struct i2c_client *client = to_i2c_client(dev);
369         struct adm9240_data *data = i2c_get_clientdata(client);
370         unsigned long val = simple_strtoul(buf, NULL, 10);
371         u8 new_div;
372
373         down(&data->update_lock);
374
375         if (!val) {
376                 data->fan_min[nr] = 255;
377                 new_div = data->fan_div[nr];
378
379                 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
380                                 nr + 1);
381
382         } else if (val < 1350000 / (8 * 254)) {
383                 new_div = 3;
384                 data->fan_min[nr] = 254;
385
386                 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
387                                 nr + 1, FAN_FROM_REG(254, 1 << new_div));
388
389         } else {
390                 unsigned int new_min = 1350000 / val;
391
392                 new_div = 0;
393                 while (new_min > 192 && new_div < 3) {
394                         new_div++;
395                         new_min /= 2;
396                 }
397                 if (!new_min) /* keep > 0 */
398                         new_min++;
399
400                 data->fan_min[nr] = new_min;
401
402                 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
403                                 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
404         }
405
406         if (new_div != data->fan_div[nr]) {
407                 data->fan_div[nr] = new_div;
408                 adm9240_write_fan_div(client, nr, new_div);
409         }
410         adm9240_write_value(client, ADM9240_REG_FAN_MIN(nr),
411                         data->fan_min[nr]);
412
413         up(&data->update_lock);
414         return count;
415 }
416
417 #define show_fan_offset(offset)                                         \
418 static ssize_t show_fan_##offset (struct device *dev,                   \
419                                   struct device_attribute *attr,        \
420                                   char *buf)                            \
421 {                                                                       \
422 return show_fan(dev, buf, offset - 1);                                  \
423 }                                                                       \
424 static ssize_t show_fan_##offset##_div (struct device *dev,             \
425                                         struct device_attribute *attr,  \
426                                         char *buf)                      \
427 {                                                                       \
428 return show_fan_div(dev, buf, offset - 1);                              \
429 }                                                                       \
430 static ssize_t show_fan_##offset##_min (struct device *dev,             \
431                                         struct device_attribute *attr,  \
432                                         char *buf)                      \
433 {                                                                       \
434 return show_fan_min(dev, buf, offset - 1);                              \
435 }                                                                       \
436 static ssize_t set_fan_##offset##_min (struct device *dev,              \
437                                        struct device_attribute *attr,   \
438                                        const char *buf, size_t count)   \
439 {                                                                       \
440 return set_fan_min(dev, buf, count, offset - 1);                        \
441 }                                                                       \
442 static DEVICE_ATTR(fan##offset##_input, S_IRUGO,                        \
443                 show_fan_##offset, NULL);                               \
444 static DEVICE_ATTR(fan##offset##_div, S_IRUGO,                          \
445                 show_fan_##offset##_div, NULL);                         \
446 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
447                 show_fan_##offset##_min, set_fan_##offset##_min);
448
449 show_fan_offset(1);
450 show_fan_offset(2);
451
452 /* alarms */
453 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
454 {
455         struct adm9240_data *data = adm9240_update_device(dev);
456         return sprintf(buf, "%u\n", data->alarms);
457 }
458 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
459
460 /* vid */
461 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
462 {
463         struct adm9240_data *data = adm9240_update_device(dev);
464         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
465 }
466 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
467
468 /* analog output */
469 static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
470 {
471         struct adm9240_data *data = adm9240_update_device(dev);
472         return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
473 }
474
475 static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
476 {
477         struct i2c_client *client = to_i2c_client(dev);
478         struct adm9240_data *data = i2c_get_clientdata(client);
479         unsigned long val = simple_strtol(buf, NULL, 10);
480
481         down(&data->update_lock);
482         data->aout = AOUT_TO_REG(val);
483         adm9240_write_value(client, ADM9240_REG_ANALOG_OUT, data->aout);
484         up(&data->update_lock);
485         return count;
486 }
487 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
488
489 /* chassis_clear */
490 static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
491 {
492         struct i2c_client *client = to_i2c_client(dev);
493         unsigned long val = simple_strtol(buf, NULL, 10);
494
495         if (val == 1) {
496                 adm9240_write_value(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
497                 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
498         }
499         return count;
500 }
501 static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
502
503
504 /*** sensor chip detect and driver install ***/
505
506 static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
507 {
508         struct i2c_client *new_client;
509         struct adm9240_data *data;
510         int err = 0;
511         const char *name = "";
512         u8 man_id, die_rev;
513
514         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
515                 goto exit;
516
517         if (!(data = kmalloc(sizeof(struct adm9240_data), GFP_KERNEL))) {
518                 err = -ENOMEM;
519                 goto exit;
520         }
521         memset(data, 0, sizeof(struct adm9240_data));
522
523         new_client = &data->client;
524         i2c_set_clientdata(new_client, data);
525         new_client->addr = address;
526         new_client->adapter = adapter;
527         new_client->driver = &adm9240_driver;
528         new_client->flags = 0;
529
530         if (kind == 0) {
531                 kind = adm9240;
532         }
533
534         if (kind < 0) {
535
536                 /* verify chip: reg address should match i2c address */
537                 if (adm9240_read_value(new_client, ADM9240_REG_I2C_ADDR)
538                                 != address) {
539                         dev_err(&adapter->dev, "detect fail: address match, "
540                                         "0x%02x\n", address);
541                         goto exit_free;
542                 }
543
544                 /* check known chip manufacturer */
545                 man_id = adm9240_read_value(new_client, ADM9240_REG_MAN_ID);
546
547                 if (man_id == 0x23) {
548                         kind = adm9240;
549                 } else if (man_id == 0xda) {
550                         kind = ds1780;
551                 } else if (man_id == 0x01) {
552                         kind = lm81;
553                 } else {
554                         dev_err(&adapter->dev, "detect fail: unknown manuf, "
555                                         "0x%02x\n", man_id);
556                         goto exit_free;
557                 }
558
559                 /* successful detect, print chip info */
560                 die_rev = adm9240_read_value(new_client, ADM9240_REG_DIE_REV);
561                 dev_info(&adapter->dev, "found %s revision %u\n",
562                                 man_id == 0x23 ? "ADM9240" :
563                                 man_id == 0xda ? "DS1780" : "LM81", die_rev);
564         }
565
566         /* either forced or detected chip kind */
567         if (kind == adm9240) {
568                 name = "adm9240";
569         } else if (kind == ds1780) {
570                 name = "ds1780";
571         } else if (kind == lm81) {
572                 name = "lm81";
573         }
574
575         /* fill in the remaining client fields and attach */
576         strlcpy(new_client->name, name, I2C_NAME_SIZE);
577         data->type = kind;
578         init_MUTEX(&data->update_lock);
579
580         if ((err = i2c_attach_client(new_client)))
581                 goto exit_free;
582
583         adm9240_init_client(new_client);
584
585         /* populate sysfs filesystem */
586         data->class_dev = hwmon_device_register(&new_client->dev);
587         if (IS_ERR(data->class_dev)) {
588                 err = PTR_ERR(data->class_dev);
589                 goto exit_detach;
590         }
591
592         device_create_file(&new_client->dev, &dev_attr_in0_input);
593         device_create_file(&new_client->dev, &dev_attr_in0_min);
594         device_create_file(&new_client->dev, &dev_attr_in0_max);
595         device_create_file(&new_client->dev, &dev_attr_in1_input);
596         device_create_file(&new_client->dev, &dev_attr_in1_min);
597         device_create_file(&new_client->dev, &dev_attr_in1_max);
598         device_create_file(&new_client->dev, &dev_attr_in2_input);
599         device_create_file(&new_client->dev, &dev_attr_in2_min);
600         device_create_file(&new_client->dev, &dev_attr_in2_max);
601         device_create_file(&new_client->dev, &dev_attr_in3_input);
602         device_create_file(&new_client->dev, &dev_attr_in3_min);
603         device_create_file(&new_client->dev, &dev_attr_in3_max);
604         device_create_file(&new_client->dev, &dev_attr_in4_input);
605         device_create_file(&new_client->dev, &dev_attr_in4_min);
606         device_create_file(&new_client->dev, &dev_attr_in4_max);
607         device_create_file(&new_client->dev, &dev_attr_in5_input);
608         device_create_file(&new_client->dev, &dev_attr_in5_min);
609         device_create_file(&new_client->dev, &dev_attr_in5_max);
610         device_create_file(&new_client->dev, &dev_attr_temp1_max);
611         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
612         device_create_file(&new_client->dev, &dev_attr_temp1_input);
613         device_create_file(&new_client->dev, &dev_attr_fan1_input);
614         device_create_file(&new_client->dev, &dev_attr_fan1_div);
615         device_create_file(&new_client->dev, &dev_attr_fan1_min);
616         device_create_file(&new_client->dev, &dev_attr_fan2_input);
617         device_create_file(&new_client->dev, &dev_attr_fan2_div);
618         device_create_file(&new_client->dev, &dev_attr_fan2_min);
619         device_create_file(&new_client->dev, &dev_attr_alarms);
620         device_create_file(&new_client->dev, &dev_attr_aout_output);
621         device_create_file(&new_client->dev, &dev_attr_chassis_clear);
622         device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
623
624         return 0;
625
626 exit_detach:
627         i2c_detach_client(new_client);
628 exit_free:
629         kfree(data);
630 exit:
631         return err;
632 }
633
634 static int adm9240_attach_adapter(struct i2c_adapter *adapter)
635 {
636         if (!(adapter->class & I2C_CLASS_HWMON))
637                 return 0;
638         return i2c_detect(adapter, &addr_data, adm9240_detect);
639 }
640
641 static int adm9240_detach_client(struct i2c_client *client)
642 {
643         struct adm9240_data *data = i2c_get_clientdata(client);
644         int err;
645
646         hwmon_device_unregister(data->class_dev);
647
648         if ((err = i2c_detach_client(client)))
649                 return err;
650
651         kfree(data);
652         return 0;
653 }
654
655 static void adm9240_init_client(struct i2c_client *client)
656 {
657         struct adm9240_data *data = i2c_get_clientdata(client);
658         u8 conf = adm9240_read_value(client, ADM9240_REG_CONFIG);
659         u8 mode = adm9240_read_value(client, ADM9240_REG_TEMP_CONF) & 3;
660
661         data->vrm = i2c_which_vrm(); /* need this to report vid as mV */
662
663         dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
664                         data->vrm % 10);
665
666         if (conf & 1) { /* measurement cycle running: report state */
667
668                 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
669                                 conf, mode);
670
671         } else { /* cold start: open limits before starting chip */
672                 int i;
673
674                 for (i = 0; i < 6; i++)
675                 {
676                         adm9240_write_value(client,
677                                         ADM9240_REG_IN_MIN(i), 0);
678                         adm9240_write_value(client,
679                                         ADM9240_REG_IN_MAX(i), 255);
680                 }
681                 adm9240_write_value(client, ADM9240_REG_FAN_MIN(0), 255);
682                 adm9240_write_value(client, ADM9240_REG_FAN_MIN(1), 255);
683                 adm9240_write_value(client, ADM9240_REG_TEMP_HIGH, 127);
684                 adm9240_write_value(client, ADM9240_REG_TEMP_HYST, 127);
685
686                 /* start measurement cycle */
687                 adm9240_write_value(client, ADM9240_REG_CONFIG, 1);
688
689                 dev_info(&client->dev, "cold start: config was 0x%02x "
690                                 "mode %u\n", conf, mode);
691         }
692 }
693
694 static struct adm9240_data *adm9240_update_device(struct device *dev)
695 {
696         struct i2c_client *client = to_i2c_client(dev);
697         struct adm9240_data *data = i2c_get_clientdata(client);
698         int i;
699
700         down(&data->update_lock);
701
702         /* minimum measurement cycle: 1.75 seconds */
703         if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
704                         || !data->valid) {
705
706                 for (i = 0; i < 6; i++) /* read voltages */
707                 {
708                         data->in[i] = adm9240_read_value(client,
709                                         ADM9240_REG_IN(i));
710                 }
711                 data->alarms = adm9240_read_value(client,
712                                         ADM9240_REG_INT(0)) |
713                                         adm9240_read_value(client,
714                                         ADM9240_REG_INT(1)) << 8;
715
716                 /* read temperature: assume temperature changes less than
717                  * 0.5'C per two measurement cycles thus ignore possible
718                  * but unlikely aliasing error on lsb reading. --Grant */
719                 data->temp = ((adm9240_read_value(client,
720                                         ADM9240_REG_TEMP) << 8) |
721                                         adm9240_read_value(client,
722                                         ADM9240_REG_TEMP_CONF)) / 128;
723
724                 for (i = 0; i < 2; i++) /* read fans */
725                 {
726                         data->fan[i] = adm9240_read_value(client,
727                                         ADM9240_REG_FAN(i));
728
729                         /* adjust fan clock divider on overflow */
730                         if (data->valid && data->fan[i] == 255 &&
731                                         data->fan_div[i] < 3) {
732
733                                 adm9240_write_fan_div(client, i,
734                                                 ++data->fan_div[i]);
735
736                                 /* adjust fan_min if active, but not to 0 */
737                                 if (data->fan_min[i] < 255 &&
738                                                 data->fan_min[i] >= 2)
739                                         data->fan_min[i] /= 2;
740                         }
741                 }
742                 data->last_updated_measure = jiffies;
743         }
744
745         /* minimum config reading cycle: 300 seconds */
746         if (time_after(jiffies, data->last_updated_config + (HZ * 300))
747                         || !data->valid) {
748
749                 for (i = 0; i < 6; i++)
750                 {
751                         data->in_min[i] = adm9240_read_value(client,
752                                         ADM9240_REG_IN_MIN(i));
753                         data->in_max[i] = adm9240_read_value(client,
754                                         ADM9240_REG_IN_MAX(i));
755                 }
756                 for (i = 0; i < 2; i++)
757                 {
758                         data->fan_min[i] = adm9240_read_value(client,
759                                         ADM9240_REG_FAN_MIN(i));
760                 }
761                 data->temp_high = adm9240_read_value(client,
762                                 ADM9240_REG_TEMP_HIGH);
763                 data->temp_hyst = adm9240_read_value(client,
764                                 ADM9240_REG_TEMP_HYST);
765
766                 /* read fan divs and 5-bit VID */
767                 i = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
768                 data->fan_div[0] = (i >> 4) & 3;
769                 data->fan_div[1] = (i >> 6) & 3;
770                 data->vid = i & 0x0f;
771                 data->vid |= (adm9240_read_value(client,
772                                         ADM9240_REG_VID4) & 1) << 4;
773                 /* read analog out */
774                 data->aout = adm9240_read_value(client,
775                                 ADM9240_REG_ANALOG_OUT);
776
777                 data->last_updated_config = jiffies;
778                 data->valid = 1;
779         }
780         up(&data->update_lock);
781         return data;
782 }
783
784 static int __init sensors_adm9240_init(void)
785 {
786         return i2c_add_driver(&adm9240_driver);
787 }
788
789 static void __exit sensors_adm9240_exit(void)
790 {
791         i2c_del_driver(&adm9240_driver);
792 }
793
794 MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
795                 "Grant Coady <gcoady@gmail.com> and others");
796 MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
797 MODULE_LICENSE("GPL");
798
799 module_init(sensors_adm9240_init);
800 module_exit(sensors_adm9240_exit);
801