blob: ecef342e5fa3b03cf3f469e3a25ecff32bbbe5d5 [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>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040048#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020049#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040050#include <linux/err.h>
Grant Coady40b5cda2005-04-30 21:41:29 +100051
52/* Addresses to scan */
53static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
54 I2C_CLIENT_END };
55
Grant Coady40b5cda2005-04-30 21:41:29 +100056/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020057I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
Grant Coady40b5cda2005-04-30 21:41:29 +100058
59/* ADM9240 registers */
60#define ADM9240_REG_MAN_ID 0x3e
61#define ADM9240_REG_DIE_REV 0x3f
62#define ADM9240_REG_CONFIG 0x40
63
64#define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
65#define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
66#define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
67#define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
68#define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
69#define ADM9240_REG_INT(nr) (0x41 + (nr))
70#define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
71#define ADM9240_REG_TEMP 0x27
72#define ADM9240_REG_TEMP_HIGH 0x39
73#define ADM9240_REG_TEMP_HYST 0x3a
74#define ADM9240_REG_ANALOG_OUT 0x19
75#define ADM9240_REG_CHASSIS_CLEAR 0x46
76#define ADM9240_REG_VID_FAN_DIV 0x47
77#define ADM9240_REG_I2C_ADDR 0x48
78#define ADM9240_REG_VID4 0x49
79#define ADM9240_REG_TEMP_CONF 0x4b
80
81/* generalised scaling with integer rounding */
82static inline int SCALE(long val, int mul, int div)
83{
84 if (val < 0)
85 return (val * mul - div / 2) / div;
86 else
87 return (val * mul + div / 2) / div;
88}
89
90/* adm9240 internally scales voltage measurements */
91static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
92
93static inline unsigned int IN_FROM_REG(u8 reg, int n)
94{
95 return SCALE(reg, nom_mv[n], 192);
96}
97
98static inline u8 IN_TO_REG(unsigned long val, int n)
99{
100 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
101}
102
103/* temperature range: -40..125, 127 disables temperature alarm */
104static inline s8 TEMP_TO_REG(long val)
105{
106 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
107}
108
109/* two fans, each with low fan speed limit */
110static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
111{
112 if (!reg) /* error */
113 return -1;
114
115 if (reg == 255)
116 return 0;
117
118 return SCALE(1350000, 1, reg * div);
119}
120
121/* analog out 0..1250mV */
122static inline u8 AOUT_TO_REG(unsigned long val)
123{
124 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
125}
126
127static inline unsigned int AOUT_FROM_REG(u8 reg)
128{
129 return SCALE(reg, 1250, 255);
130}
131
132static int adm9240_attach_adapter(struct i2c_adapter *adapter);
133static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
134static void adm9240_init_client(struct i2c_client *client);
135static int adm9240_detach_client(struct i2c_client *client);
136static struct adm9240_data *adm9240_update_device(struct device *dev);
137
138/* driver data */
139static struct i2c_driver adm9240_driver = {
140 .owner = THIS_MODULE,
141 .name = "adm9240",
142 .id = I2C_DRIVERID_ADM9240,
143 .flags = I2C_DF_NOTIFY,
144 .attach_adapter = adm9240_attach_adapter,
145 .detach_client = adm9240_detach_client,
146};
147
148/* per client data */
149struct adm9240_data {
150 enum chips type;
151 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400152 struct class_device *class_dev;
Grant Coady40b5cda2005-04-30 21:41:29 +1000153 struct semaphore update_lock;
154 char valid;
155 unsigned long last_updated_measure;
156 unsigned long last_updated_config;
157
158 u8 in[6]; /* ro in0_input */
159 u8 in_max[6]; /* rw in0_max */
160 u8 in_min[6]; /* rw in0_min */
161 u8 fan[2]; /* ro fan1_input */
162 u8 fan_min[2]; /* rw fan1_min */
163 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
164 s16 temp; /* ro temp1_input, 9-bit sign-extended */
165 s8 temp_high; /* rw temp1_max */
166 s8 temp_hyst; /* rw temp1_max_hyst */
167 u16 alarms; /* ro alarms */
Grant Coady8e8f9282005-05-13 20:26:10 +1000168 u8 aout; /* rw aout_output */
Grant Coady40b5cda2005-04-30 21:41:29 +1000169 u8 vid; /* ro vid */
170 u8 vrm; /* -- vrm set on startup, no accessor */
171};
172
Grant Coady40b5cda2005-04-30 21:41:29 +1000173/*** sysfs accessors ***/
174
175/* temperature */
176#define show_temp(value, scale) \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700177static ssize_t show_##value(struct device *dev, \
178 struct device_attribute *attr, \
179 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000180{ \
181 struct adm9240_data *data = adm9240_update_device(dev); \
182 return sprintf(buf, "%d\n", data->value * scale); \
183}
184show_temp(temp_high, 1000);
185show_temp(temp_hyst, 1000);
Grant Coady8e8f9282005-05-13 20:26:10 +1000186show_temp(temp, 500); /* 0.5'C per bit */
Grant Coady40b5cda2005-04-30 21:41:29 +1000187
188#define set_temp(value, reg) \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700189static ssize_t set_##value(struct device *dev, \
190 struct device_attribute *attr, \
191 const char *buf, size_t count) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000192{ \
193 struct i2c_client *client = to_i2c_client(dev); \
194 struct adm9240_data *data = adm9240_update_device(dev); \
195 long temp = simple_strtoul(buf, NULL, 10); \
196 \
197 down(&data->update_lock); \
198 data->value = TEMP_TO_REG(temp); \
Grant Coady205cf132005-09-17 05:32:55 +1000199 i2c_smbus_write_byte_data(client, reg, data->value); \
Grant Coady40b5cda2005-04-30 21:41:29 +1000200 up(&data->update_lock); \
201 return count; \
202}
203
204set_temp(temp_high, ADM9240_REG_TEMP_HIGH);
205set_temp(temp_hyst, ADM9240_REG_TEMP_HYST);
206
207static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
208 show_temp_high, set_temp_high);
209static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
210 show_temp_hyst, set_temp_hyst);
211static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
212
213/* voltage */
214static ssize_t show_in(struct device *dev, char *buf, int nr)
215{
216 struct adm9240_data *data = adm9240_update_device(dev);
217 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
218}
219
220static ssize_t show_in_min(struct device *dev, char *buf, int nr)
221{
222 struct adm9240_data *data = adm9240_update_device(dev);
223 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
224}
225
226static ssize_t show_in_max(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_max[nr], nr));
230}
231
232static ssize_t set_in_min(struct device *dev, const char *buf,
233 size_t count, int nr)
234{
235 struct i2c_client *client = to_i2c_client(dev);
236 struct adm9240_data *data = i2c_get_clientdata(client);
237 unsigned long val = simple_strtoul(buf, NULL, 10);
238
239 down(&data->update_lock);
240 data->in_min[nr] = IN_TO_REG(val, nr);
Grant Coady205cf132005-09-17 05:32:55 +1000241 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(nr),
242 data->in_min[nr]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000243 up(&data->update_lock);
244 return count;
245}
246
247static ssize_t set_in_max(struct device *dev, const char *buf,
248 size_t count, int nr)
249{
250 struct i2c_client *client = to_i2c_client(dev);
251 struct adm9240_data *data = i2c_get_clientdata(client);
252 unsigned long val = simple_strtoul(buf, NULL, 10);
253
254 down(&data->update_lock);
255 data->in_max[nr] = IN_TO_REG(val, nr);
Grant Coady205cf132005-09-17 05:32:55 +1000256 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(nr),
257 data->in_max[nr]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000258 up(&data->update_lock);
259 return count;
260}
261
262#define show_in_offset(offset) \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700263static ssize_t show_in##offset(struct device *dev, \
264 struct device_attribute *attr, \
265 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000266{ \
267 return show_in(dev, buf, offset); \
268} \
269static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700270static ssize_t show_in##offset##_min(struct device *dev, \
271 struct device_attribute *attr, \
272 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000273{ \
274 return show_in_min(dev, buf, offset); \
275} \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700276static ssize_t show_in##offset##_max(struct device *dev, \
277 struct device_attribute *attr, \
278 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000279{ \
280 return show_in_max(dev, buf, offset); \
281} \
282static ssize_t \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700283set_in##offset##_min(struct device *dev, \
284 struct device_attribute *attr, const char *buf, \
285 size_t count) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000286{ \
287 return set_in_min(dev, buf, count, offset); \
288} \
289static ssize_t \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700290set_in##offset##_max(struct device *dev, \
291 struct device_attribute *attr, const char *buf, \
292 size_t count) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000293{ \
294 return set_in_max(dev, buf, count, offset); \
295} \
296static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
297 show_in##offset##_min, set_in##offset##_min); \
298static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
299 show_in##offset##_max, set_in##offset##_max);
300
301show_in_offset(0);
302show_in_offset(1);
303show_in_offset(2);
304show_in_offset(3);
305show_in_offset(4);
306show_in_offset(5);
307
308/* fans */
309static ssize_t show_fan(struct device *dev, char *buf, int nr)
310{
311 struct adm9240_data *data = adm9240_update_device(dev);
312 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
313 1 << data->fan_div[nr]));
314}
315
316static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
317{
318 struct adm9240_data *data = adm9240_update_device(dev);
319 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
320 1 << data->fan_div[nr]));
321}
322
323static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
324{
325 struct adm9240_data *data = adm9240_update_device(dev);
326 return sprintf(buf, "%d\n", 1 << data->fan_div[nr]);
327}
328
329/* write new fan div, callers must hold data->update_lock */
330static void adm9240_write_fan_div(struct i2c_client *client, int nr,
331 u8 fan_div)
332{
333 u8 reg, old, shift = (nr + 2) * 2;
334
Grant Coady205cf132005-09-17 05:32:55 +1000335 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000336 old = (reg >> shift) & 3;
337 reg &= ~(3 << shift);
338 reg |= (fan_div << shift);
Grant Coady205cf132005-09-17 05:32:55 +1000339 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
Grant Coady40b5cda2005-04-30 21:41:29 +1000340 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
341 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
342}
343
344/*
345 * set fan speed low limit:
346 *
347 * - value is zero: disable fan speed low limit alarm
348 *
349 * - value is below fan speed measurement range: enable fan speed low
350 * limit alarm to be asserted while fan speed too slow to measure
351 *
352 * - otherwise: select fan clock divider to suit fan speed low limit,
353 * measurement code may adjust registers to ensure fan speed reading
354 */
355static ssize_t set_fan_min(struct device *dev, const char *buf,
356 size_t count, int nr)
357{
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adm9240_data *data = i2c_get_clientdata(client);
360 unsigned long val = simple_strtoul(buf, NULL, 10);
361 u8 new_div;
362
363 down(&data->update_lock);
364
365 if (!val) {
366 data->fan_min[nr] = 255;
367 new_div = data->fan_div[nr];
368
369 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
370 nr + 1);
371
372 } else if (val < 1350000 / (8 * 254)) {
373 new_div = 3;
374 data->fan_min[nr] = 254;
375
376 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
377 nr + 1, FAN_FROM_REG(254, 1 << new_div));
378
379 } else {
380 unsigned int new_min = 1350000 / val;
381
382 new_div = 0;
383 while (new_min > 192 && new_div < 3) {
384 new_div++;
385 new_min /= 2;
386 }
387 if (!new_min) /* keep > 0 */
388 new_min++;
389
390 data->fan_min[nr] = new_min;
391
392 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
393 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
394 }
395
396 if (new_div != data->fan_div[nr]) {
397 data->fan_div[nr] = new_div;
398 adm9240_write_fan_div(client, nr, new_div);
399 }
Grant Coady205cf132005-09-17 05:32:55 +1000400 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
Grant Coady40b5cda2005-04-30 21:41:29 +1000401 data->fan_min[nr]);
402
403 up(&data->update_lock);
404 return count;
405}
406
407#define show_fan_offset(offset) \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700408static ssize_t show_fan_##offset (struct device *dev, \
409 struct device_attribute *attr, \
410 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000411{ \
412return show_fan(dev, buf, offset - 1); \
413} \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700414static ssize_t show_fan_##offset##_div (struct device *dev, \
415 struct device_attribute *attr, \
416 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000417{ \
418return show_fan_div(dev, buf, offset - 1); \
419} \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700420static ssize_t show_fan_##offset##_min (struct device *dev, \
421 struct device_attribute *attr, \
422 char *buf) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000423{ \
424return show_fan_min(dev, buf, offset - 1); \
425} \
426static ssize_t set_fan_##offset##_min (struct device *dev, \
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700427 struct device_attribute *attr, \
428 const char *buf, size_t count) \
Grant Coady40b5cda2005-04-30 21:41:29 +1000429{ \
430return set_fan_min(dev, buf, count, offset - 1); \
431} \
432static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
433 show_fan_##offset, NULL); \
434static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
435 show_fan_##offset##_div, NULL); \
436static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
437 show_fan_##offset##_min, set_fan_##offset##_min);
438
439show_fan_offset(1);
440show_fan_offset(2);
441
442/* alarms */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700443static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000444{
445 struct adm9240_data *data = adm9240_update_device(dev);
446 return sprintf(buf, "%u\n", data->alarms);
447}
448static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
449
450/* vid */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700451static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000452{
453 struct adm9240_data *data = adm9240_update_device(dev);
454 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
455}
456static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
457
458/* analog output */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700459static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000460{
461 struct adm9240_data *data = adm9240_update_device(dev);
462 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
463}
464
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700465static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000466{
467 struct i2c_client *client = to_i2c_client(dev);
468 struct adm9240_data *data = i2c_get_clientdata(client);
469 unsigned long val = simple_strtol(buf, NULL, 10);
470
471 down(&data->update_lock);
472 data->aout = AOUT_TO_REG(val);
Grant Coady205cf132005-09-17 05:32:55 +1000473 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
Grant Coady40b5cda2005-04-30 21:41:29 +1000474 up(&data->update_lock);
475 return count;
476}
477static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
478
479/* chassis_clear */
Greg Kroah-Hartman6f637a62005-06-21 21:01:59 -0700480static ssize_t chassis_clear(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000481{
482 struct i2c_client *client = to_i2c_client(dev);
483 unsigned long val = simple_strtol(buf, NULL, 10);
484
485 if (val == 1) {
Grant Coady205cf132005-09-17 05:32:55 +1000486 i2c_smbus_write_byte_data(client,
487 ADM9240_REG_CHASSIS_CLEAR, 0x80);
Grant Coady40b5cda2005-04-30 21:41:29 +1000488 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
489 }
490 return count;
491}
492static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
493
494
495/*** sensor chip detect and driver install ***/
496
497static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
498{
499 struct i2c_client *new_client;
500 struct adm9240_data *data;
501 int err = 0;
502 const char *name = "";
503 u8 man_id, die_rev;
504
505 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
506 goto exit;
507
Grant Coady205cf132005-09-17 05:32:55 +1000508 if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000509 err = -ENOMEM;
510 goto exit;
511 }
Grant Coady40b5cda2005-04-30 21:41:29 +1000512
513 new_client = &data->client;
514 i2c_set_clientdata(new_client, data);
515 new_client->addr = address;
516 new_client->adapter = adapter;
517 new_client->driver = &adm9240_driver;
518 new_client->flags = 0;
519
520 if (kind == 0) {
521 kind = adm9240;
522 }
523
524 if (kind < 0) {
525
526 /* verify chip: reg address should match i2c address */
Grant Coady205cf132005-09-17 05:32:55 +1000527 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
Grant Coady40b5cda2005-04-30 21:41:29 +1000528 != address) {
529 dev_err(&adapter->dev, "detect fail: address match, "
530 "0x%02x\n", address);
531 goto exit_free;
532 }
533
534 /* check known chip manufacturer */
Grant Coady205cf132005-09-17 05:32:55 +1000535 man_id = i2c_smbus_read_byte_data(new_client,
536 ADM9240_REG_MAN_ID);
Grant Coady40b5cda2005-04-30 21:41:29 +1000537 if (man_id == 0x23) {
538 kind = adm9240;
539 } else if (man_id == 0xda) {
540 kind = ds1780;
541 } else if (man_id == 0x01) {
542 kind = lm81;
543 } else {
544 dev_err(&adapter->dev, "detect fail: unknown manuf, "
545 "0x%02x\n", man_id);
546 goto exit_free;
547 }
548
549 /* successful detect, print chip info */
Grant Coady205cf132005-09-17 05:32:55 +1000550 die_rev = i2c_smbus_read_byte_data(new_client,
551 ADM9240_REG_DIE_REV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000552 dev_info(&adapter->dev, "found %s revision %u\n",
553 man_id == 0x23 ? "ADM9240" :
554 man_id == 0xda ? "DS1780" : "LM81", die_rev);
555 }
556
557 /* either forced or detected chip kind */
558 if (kind == adm9240) {
559 name = "adm9240";
560 } else if (kind == ds1780) {
561 name = "ds1780";
562 } else if (kind == lm81) {
563 name = "lm81";
564 }
565
566 /* fill in the remaining client fields and attach */
567 strlcpy(new_client->name, name, I2C_NAME_SIZE);
568 data->type = kind;
569 init_MUTEX(&data->update_lock);
570
571 if ((err = i2c_attach_client(new_client)))
572 goto exit_free;
573
574 adm9240_init_client(new_client);
575
576 /* populate sysfs filesystem */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400577 data->class_dev = hwmon_device_register(&new_client->dev);
578 if (IS_ERR(data->class_dev)) {
579 err = PTR_ERR(data->class_dev);
580 goto exit_detach;
581 }
582
Grant Coady40b5cda2005-04-30 21:41:29 +1000583 device_create_file(&new_client->dev, &dev_attr_in0_input);
584 device_create_file(&new_client->dev, &dev_attr_in0_min);
585 device_create_file(&new_client->dev, &dev_attr_in0_max);
586 device_create_file(&new_client->dev, &dev_attr_in1_input);
587 device_create_file(&new_client->dev, &dev_attr_in1_min);
588 device_create_file(&new_client->dev, &dev_attr_in1_max);
589 device_create_file(&new_client->dev, &dev_attr_in2_input);
590 device_create_file(&new_client->dev, &dev_attr_in2_min);
591 device_create_file(&new_client->dev, &dev_attr_in2_max);
592 device_create_file(&new_client->dev, &dev_attr_in3_input);
593 device_create_file(&new_client->dev, &dev_attr_in3_min);
594 device_create_file(&new_client->dev, &dev_attr_in3_max);
595 device_create_file(&new_client->dev, &dev_attr_in4_input);
596 device_create_file(&new_client->dev, &dev_attr_in4_min);
597 device_create_file(&new_client->dev, &dev_attr_in4_max);
598 device_create_file(&new_client->dev, &dev_attr_in5_input);
599 device_create_file(&new_client->dev, &dev_attr_in5_min);
600 device_create_file(&new_client->dev, &dev_attr_in5_max);
601 device_create_file(&new_client->dev, &dev_attr_temp1_max);
602 device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
603 device_create_file(&new_client->dev, &dev_attr_temp1_input);
604 device_create_file(&new_client->dev, &dev_attr_fan1_input);
605 device_create_file(&new_client->dev, &dev_attr_fan1_div);
606 device_create_file(&new_client->dev, &dev_attr_fan1_min);
607 device_create_file(&new_client->dev, &dev_attr_fan2_input);
608 device_create_file(&new_client->dev, &dev_attr_fan2_div);
609 device_create_file(&new_client->dev, &dev_attr_fan2_min);
610 device_create_file(&new_client->dev, &dev_attr_alarms);
611 device_create_file(&new_client->dev, &dev_attr_aout_output);
612 device_create_file(&new_client->dev, &dev_attr_chassis_clear);
613 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
614
615 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400616
617exit_detach:
618 i2c_detach_client(new_client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000619exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400620 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000621exit:
622 return err;
623}
624
625static int adm9240_attach_adapter(struct i2c_adapter *adapter)
626{
627 if (!(adapter->class & I2C_CLASS_HWMON))
628 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200629 return i2c_probe(adapter, &addr_data, adm9240_detect);
Grant Coady40b5cda2005-04-30 21:41:29 +1000630}
631
632static int adm9240_detach_client(struct i2c_client *client)
633{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400634 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000635 int err;
636
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400637 hwmon_device_unregister(data->class_dev);
638
Jean Delvare7bef5592005-07-27 22:14:49 +0200639 if ((err = i2c_detach_client(client)))
Grant Coady40b5cda2005-04-30 21:41:29 +1000640 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000641
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400642 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000643 return 0;
644}
645
646static void adm9240_init_client(struct i2c_client *client)
647{
648 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady205cf132005-09-17 05:32:55 +1000649 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
650 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
Grant Coady40b5cda2005-04-30 21:41:29 +1000651
Jean Delvare303760b2005-07-31 21:52:01 +0200652 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
Grant Coady40b5cda2005-04-30 21:41:29 +1000653
Grant Coady8e8f9282005-05-13 20:26:10 +1000654 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
655 data->vrm % 10);
656
Grant Coady40b5cda2005-04-30 21:41:29 +1000657 if (conf & 1) { /* measurement cycle running: report state */
658
659 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
660 conf, mode);
661
662 } else { /* cold start: open limits before starting chip */
663 int i;
664
665 for (i = 0; i < 6; i++)
666 {
Grant Coady205cf132005-09-17 05:32:55 +1000667 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000668 ADM9240_REG_IN_MIN(i), 0);
Grant Coady205cf132005-09-17 05:32:55 +1000669 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000670 ADM9240_REG_IN_MAX(i), 255);
671 }
Grant Coady205cf132005-09-17 05:32:55 +1000672 i2c_smbus_write_byte_data(client,
673 ADM9240_REG_FAN_MIN(0), 255);
674 i2c_smbus_write_byte_data(client,
675 ADM9240_REG_FAN_MIN(1), 255);
676 i2c_smbus_write_byte_data(client,
677 ADM9240_REG_TEMP_HIGH, 127);
678 i2c_smbus_write_byte_data(client,
679 ADM9240_REG_TEMP_HYST, 127);
Grant Coady40b5cda2005-04-30 21:41:29 +1000680
681 /* start measurement cycle */
Grant Coady205cf132005-09-17 05:32:55 +1000682 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000683
684 dev_info(&client->dev, "cold start: config was 0x%02x "
685 "mode %u\n", conf, mode);
686 }
687}
688
689static struct adm9240_data *adm9240_update_device(struct device *dev)
690{
691 struct i2c_client *client = to_i2c_client(dev);
692 struct adm9240_data *data = i2c_get_clientdata(client);
693 int i;
694
695 down(&data->update_lock);
696
697 /* minimum measurement cycle: 1.75 seconds */
698 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
699 || !data->valid) {
700
701 for (i = 0; i < 6; i++) /* read voltages */
702 {
Grant Coady205cf132005-09-17 05:32:55 +1000703 data->in[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000704 ADM9240_REG_IN(i));
705 }
Grant Coady205cf132005-09-17 05:32:55 +1000706 data->alarms = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000707 ADM9240_REG_INT(0)) |
Grant Coady205cf132005-09-17 05:32:55 +1000708 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000709 ADM9240_REG_INT(1)) << 8;
710
711 /* read temperature: assume temperature changes less than
712 * 0.5'C per two measurement cycles thus ignore possible
713 * but unlikely aliasing error on lsb reading. --Grant */
Grant Coady205cf132005-09-17 05:32:55 +1000714 data->temp = ((i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000715 ADM9240_REG_TEMP) << 8) |
Grant Coady205cf132005-09-17 05:32:55 +1000716 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000717 ADM9240_REG_TEMP_CONF)) / 128;
718
719 for (i = 0; i < 2; i++) /* read fans */
720 {
Grant Coady205cf132005-09-17 05:32:55 +1000721 data->fan[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000722 ADM9240_REG_FAN(i));
723
724 /* adjust fan clock divider on overflow */
725 if (data->valid && data->fan[i] == 255 &&
726 data->fan_div[i] < 3) {
727
728 adm9240_write_fan_div(client, i,
729 ++data->fan_div[i]);
730
731 /* adjust fan_min if active, but not to 0 */
732 if (data->fan_min[i] < 255 &&
733 data->fan_min[i] >= 2)
734 data->fan_min[i] /= 2;
735 }
736 }
737 data->last_updated_measure = jiffies;
738 }
739
740 /* minimum config reading cycle: 300 seconds */
741 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
742 || !data->valid) {
743
744 for (i = 0; i < 6; i++)
745 {
Grant Coady205cf132005-09-17 05:32:55 +1000746 data->in_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000747 ADM9240_REG_IN_MIN(i));
Grant Coady205cf132005-09-17 05:32:55 +1000748 data->in_max[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000749 ADM9240_REG_IN_MAX(i));
750 }
751 for (i = 0; i < 2; i++)
752 {
Grant Coady205cf132005-09-17 05:32:55 +1000753 data->fan_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000754 ADM9240_REG_FAN_MIN(i));
755 }
Grant Coady205cf132005-09-17 05:32:55 +1000756 data->temp_high = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000757 ADM9240_REG_TEMP_HIGH);
Grant Coady205cf132005-09-17 05:32:55 +1000758 data->temp_hyst = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000759 ADM9240_REG_TEMP_HYST);
760
761 /* read fan divs and 5-bit VID */
Grant Coady205cf132005-09-17 05:32:55 +1000762 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000763 data->fan_div[0] = (i >> 4) & 3;
764 data->fan_div[1] = (i >> 6) & 3;
765 data->vid = i & 0x0f;
Grant Coady205cf132005-09-17 05:32:55 +1000766 data->vid |= (i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000767 ADM9240_REG_VID4) & 1) << 4;
768 /* read analog out */
Grant Coady205cf132005-09-17 05:32:55 +1000769 data->aout = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000770 ADM9240_REG_ANALOG_OUT);
771
772 data->last_updated_config = jiffies;
773 data->valid = 1;
774 }
775 up(&data->update_lock);
776 return data;
777}
778
779static int __init sensors_adm9240_init(void)
780{
781 return i2c_add_driver(&adm9240_driver);
782}
783
784static void __exit sensors_adm9240_exit(void)
785{
786 i2c_del_driver(&adm9240_driver);
787}
788
789MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
790 "Grant Coady <gcoady@gmail.com> and others");
791MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
792MODULE_LICENSE("GPL");
793
794module_init(sensors_adm9240_init);
795module_exit(sensors_adm9240_exit);
796