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