2 * A hwmon driver for the Analog Devices ADT7470
3 * Copyright (C) 2007 IBM
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
32 /* Addresses to scan */
33 static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
35 /* Insmod parameters */
36 I2C_CLIENT_INSMOD_1(adt7470);
38 /* ADT7470 registers */
39 #define ADT7470_REG_BASE_ADDR 0x20
40 #define ADT7470_REG_TEMP_BASE_ADDR 0x20
41 #define ADT7470_REG_TEMP_MAX_ADDR 0x29
42 #define ADT7470_REG_FAN_BASE_ADDR 0x2A
43 #define ADT7470_REG_FAN_MAX_ADDR 0x31
44 #define ADT7470_REG_PWM_BASE_ADDR 0x32
45 #define ADT7470_REG_PWM_MAX_ADDR 0x35
46 #define ADT7470_REG_PWM_MAX_BASE_ADDR 0x38
47 #define ADT7470_REG_PWM_MAX_MAX_ADDR 0x3B
48 #define ADT7470_REG_CFG 0x40
49 #define ADT7470_FSPD_MASK 0x04
50 #define ADT7470_REG_ALARM1 0x41
51 #define ADT7470_R1T_ALARM 0x01
52 #define ADT7470_R2T_ALARM 0x02
53 #define ADT7470_R3T_ALARM 0x04
54 #define ADT7470_R4T_ALARM 0x08
55 #define ADT7470_R5T_ALARM 0x10
56 #define ADT7470_R6T_ALARM 0x20
57 #define ADT7470_R7T_ALARM 0x40
58 #define ADT7470_OOL_ALARM 0x80
59 #define ADT7470_REG_ALARM2 0x42
60 #define ADT7470_R8T_ALARM 0x01
61 #define ADT7470_R9T_ALARM 0x02
62 #define ADT7470_R10T_ALARM 0x04
63 #define ADT7470_FAN1_ALARM 0x10
64 #define ADT7470_FAN2_ALARM 0x20
65 #define ADT7470_FAN3_ALARM 0x40
66 #define ADT7470_FAN4_ALARM 0x80
67 #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR 0x44
68 #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR 0x57
69 #define ADT7470_REG_FAN_MIN_BASE_ADDR 0x58
70 #define ADT7470_REG_FAN_MIN_MAX_ADDR 0x5F
71 #define ADT7470_REG_FAN_MAX_BASE_ADDR 0x60
72 #define ADT7470_REG_FAN_MAX_MAX_ADDR 0x67
73 #define ADT7470_REG_PWM_CFG_BASE_ADDR 0x68
74 #define ADT7470_REG_PWM12_CFG 0x68
75 #define ADT7470_PWM2_AUTO_MASK 0x40
76 #define ADT7470_PWM1_AUTO_MASK 0x80
77 #define ADT7470_REG_PWM34_CFG 0x69
78 #define ADT7470_PWM3_AUTO_MASK 0x40
79 #define ADT7470_PWM4_AUTO_MASK 0x80
80 #define ADT7470_REG_PWM_MIN_BASE_ADDR 0x6A
81 #define ADT7470_REG_PWM_MIN_MAX_ADDR 0x6D
82 #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR 0x6E
83 #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR 0x71
84 #define ADT7470_REG_ACOUSTICS12 0x75
85 #define ADT7470_REG_ACOUSTICS34 0x76
86 #define ADT7470_REG_DEVICE 0x3D
87 #define ADT7470_REG_VENDOR 0x3E
88 #define ADT7470_REG_REVISION 0x3F
89 #define ADT7470_REG_ALARM1_MASK 0x72
90 #define ADT7470_REG_ALARM2_MASK 0x73
91 #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR 0x7C
92 #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR 0x7D
93 #define ADT7470_REG_MAX_ADDR 0x81
95 #define ADT7470_TEMP_COUNT 10
96 #define ADT7470_TEMP_REG(x) (ADT7470_REG_TEMP_BASE_ADDR + (x))
97 #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
98 #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
101 #define ADT7470_FAN_COUNT 4
102 #define ADT7470_REG_FAN(x) (ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
103 #define ADT7470_REG_FAN_MIN(x) (ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
104 #define ADT7470_REG_FAN_MAX(x) (ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
106 #define ADT7470_PWM_COUNT 4
107 #define ADT7470_REG_PWM(x) (ADT7470_REG_PWM_BASE_ADDR + (x))
108 #define ADT7470_REG_PWM_MAX(x) (ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
109 #define ADT7470_REG_PWM_MIN(x) (ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
110 #define ADT7470_REG_PWM_TMIN(x) (ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
111 #define ADT7470_REG_PWM_CFG(x) (ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
112 #define ADT7470_REG_PWM_AUTO_TEMP(x) (ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
115 #define ALARM2(x) ((x) << 8)
117 #define ADT7470_VENDOR 0x41
118 #define ADT7470_DEVICE 0x70
119 /* datasheet only mentions a revision 2 */
120 #define ADT7470_REVISION 0x02
122 /* "all temps" according to hwmon sysfs interface spec */
123 #define ADT7470_PWM_ALL_TEMPS 0x3FF
125 /* How often do we reread sensors values? (In jiffies) */
126 #define SENSOR_REFRESH_INTERVAL (5 * HZ)
128 /* How often do we reread sensor limit values? (In jiffies) */
129 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
131 /* sleep 1s while gathering temperature data */
132 #define TEMP_COLLECTION_TIME 1000
134 /* datasheet says to divide this number by the fan reading to get fan rpm */
135 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
136 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
137 #define FAN_PERIOD_INVALID 65535
138 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
140 struct adt7470_data {
141 struct device *hwmon_dev;
142 struct attribute_group attrs;
146 unsigned long sensors_last_updated; /* In jiffies */
147 unsigned long limits_last_updated; /* In jiffies */
149 s8 temp[ADT7470_TEMP_COUNT];
150 s8 temp_min[ADT7470_TEMP_COUNT];
151 s8 temp_max[ADT7470_TEMP_COUNT];
152 u16 fan[ADT7470_FAN_COUNT];
153 u16 fan_min[ADT7470_FAN_COUNT];
154 u16 fan_max[ADT7470_FAN_COUNT];
158 u8 pwm[ADT7470_PWM_COUNT];
159 u8 pwm_max[ADT7470_PWM_COUNT];
160 u8 pwm_automatic[ADT7470_PWM_COUNT];
161 u8 pwm_min[ADT7470_PWM_COUNT];
162 s8 pwm_tmin[ADT7470_PWM_COUNT];
163 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
166 static int adt7470_probe(struct i2c_client *client,
167 const struct i2c_device_id *id);
168 static int adt7470_detect(struct i2c_client *client, int kind,
169 struct i2c_board_info *info);
170 static int adt7470_remove(struct i2c_client *client);
172 static const struct i2c_device_id adt7470_id[] = {
173 { "adt7470", adt7470 },
176 MODULE_DEVICE_TABLE(i2c, adt7470_id);
178 static struct i2c_driver adt7470_driver = {
179 .class = I2C_CLASS_HWMON,
183 .probe = adt7470_probe,
184 .remove = adt7470_remove,
185 .id_table = adt7470_id,
186 .detect = adt7470_detect,
187 .address_data = &addr_data,
191 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
192 * that the low byte must be read before the high byte.
194 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
197 foo = i2c_smbus_read_byte_data(client, reg);
198 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
202 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
205 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
206 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
209 static void adt7470_init_client(struct i2c_client *client)
211 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
214 dev_err(&client->dev, "cannot read configuration register\n");
216 /* start monitoring (and do a self-test) */
217 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
221 static struct adt7470_data *adt7470_update_device(struct device *dev)
223 struct i2c_client *client = to_i2c_client(dev);
224 struct adt7470_data *data = i2c_get_clientdata(client);
225 unsigned long local_jiffies = jiffies;
229 mutex_lock(&data->lock);
230 if (time_before(local_jiffies, data->sensors_last_updated +
231 SENSOR_REFRESH_INTERVAL)
232 && data->sensors_valid)
233 goto no_sensor_update;
235 /* start reading temperature sensors */
236 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
238 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
241 * Delay is 200ms * number of tmp05 sensors. Too bad
242 * there's no way to figure out how many are connected.
243 * For now, assume 1s will work.
245 msleep(TEMP_COLLECTION_TIME);
247 /* done reading temperature sensors */
248 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
250 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
252 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
253 data->temp[i] = i2c_smbus_read_byte_data(client,
254 ADT7470_TEMP_REG(i));
256 for (i = 0; i < ADT7470_FAN_COUNT; i++)
257 data->fan[i] = adt7470_read_word_data(client,
260 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
264 data->pwm[i] = i2c_smbus_read_byte_data(client,
268 reg_mask = ADT7470_PWM2_AUTO_MASK;
270 reg_mask = ADT7470_PWM1_AUTO_MASK;
272 reg = ADT7470_REG_PWM_CFG(i);
273 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
274 data->pwm_automatic[i] = 1;
276 data->pwm_automatic[i] = 0;
278 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
279 cfg = i2c_smbus_read_byte_data(client, reg);
281 data->pwm_auto_temp[i] = cfg >> 4;
283 data->pwm_auto_temp[i] = cfg & 0xF;
286 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
288 data->force_pwm_max = 1;
290 data->force_pwm_max = 0;
292 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
293 if (data->alarm & ADT7470_OOL_ALARM)
294 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
295 ADT7470_REG_ALARM2));
296 data->alarms_mask = adt7470_read_word_data(client,
297 ADT7470_REG_ALARM1_MASK);
299 data->sensors_last_updated = local_jiffies;
300 data->sensors_valid = 1;
303 if (time_before(local_jiffies, data->limits_last_updated +
304 LIMIT_REFRESH_INTERVAL)
305 && data->limits_valid)
308 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
309 data->temp_min[i] = i2c_smbus_read_byte_data(client,
310 ADT7470_TEMP_MIN_REG(i));
311 data->temp_max[i] = i2c_smbus_read_byte_data(client,
312 ADT7470_TEMP_MAX_REG(i));
315 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
316 data->fan_min[i] = adt7470_read_word_data(client,
317 ADT7470_REG_FAN_MIN(i));
318 data->fan_max[i] = adt7470_read_word_data(client,
319 ADT7470_REG_FAN_MAX(i));
322 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
323 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
324 ADT7470_REG_PWM_MAX(i));
325 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
326 ADT7470_REG_PWM_MIN(i));
327 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
328 ADT7470_REG_PWM_TMIN(i));
331 data->limits_last_updated = local_jiffies;
332 data->limits_valid = 1;
335 mutex_unlock(&data->lock);
339 static ssize_t show_temp_min(struct device *dev,
340 struct device_attribute *devattr,
343 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
344 struct adt7470_data *data = adt7470_update_device(dev);
345 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
348 static ssize_t set_temp_min(struct device *dev,
349 struct device_attribute *devattr,
353 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
354 struct i2c_client *client = to_i2c_client(dev);
355 struct adt7470_data *data = i2c_get_clientdata(client);
356 int temp = simple_strtol(buf, NULL, 10) / 1000;
358 mutex_lock(&data->lock);
359 data->temp_min[attr->index] = temp;
360 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
362 mutex_unlock(&data->lock);
367 static ssize_t show_temp_max(struct device *dev,
368 struct device_attribute *devattr,
371 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
372 struct adt7470_data *data = adt7470_update_device(dev);
373 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
376 static ssize_t set_temp_max(struct device *dev,
377 struct device_attribute *devattr,
381 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
382 struct i2c_client *client = to_i2c_client(dev);
383 struct adt7470_data *data = i2c_get_clientdata(client);
384 int temp = simple_strtol(buf, NULL, 10) / 1000;
386 mutex_lock(&data->lock);
387 data->temp_max[attr->index] = temp;
388 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
390 mutex_unlock(&data->lock);
395 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 struct adt7470_data *data = adt7470_update_device(dev);
400 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
403 static ssize_t show_alarm_mask(struct device *dev,
404 struct device_attribute *devattr,
407 struct adt7470_data *data = adt7470_update_device(dev);
409 return sprintf(buf, "%x\n", data->alarms_mask);
412 static ssize_t show_fan_max(struct device *dev,
413 struct device_attribute *devattr,
416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 struct adt7470_data *data = adt7470_update_device(dev);
419 if (FAN_DATA_VALID(data->fan_max[attr->index]))
420 return sprintf(buf, "%d\n",
421 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
423 return sprintf(buf, "0\n");
426 static ssize_t set_fan_max(struct device *dev,
427 struct device_attribute *devattr,
428 const char *buf, size_t count)
430 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
431 struct i2c_client *client = to_i2c_client(dev);
432 struct adt7470_data *data = i2c_get_clientdata(client);
433 int temp = simple_strtol(buf, NULL, 10);
437 temp = FAN_RPM_TO_PERIOD(temp);
439 mutex_lock(&data->lock);
440 data->fan_max[attr->index] = temp;
441 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
442 mutex_unlock(&data->lock);
447 static ssize_t show_fan_min(struct device *dev,
448 struct device_attribute *devattr,
451 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
452 struct adt7470_data *data = adt7470_update_device(dev);
454 if (FAN_DATA_VALID(data->fan_min[attr->index]))
455 return sprintf(buf, "%d\n",
456 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
458 return sprintf(buf, "0\n");
461 static ssize_t set_fan_min(struct device *dev,
462 struct device_attribute *devattr,
463 const char *buf, size_t count)
465 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466 struct i2c_client *client = to_i2c_client(dev);
467 struct adt7470_data *data = i2c_get_clientdata(client);
468 int temp = simple_strtol(buf, NULL, 10);
472 temp = FAN_RPM_TO_PERIOD(temp);
474 mutex_lock(&data->lock);
475 data->fan_min[attr->index] = temp;
476 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
477 mutex_unlock(&data->lock);
482 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 struct adt7470_data *data = adt7470_update_device(dev);
488 if (FAN_DATA_VALID(data->fan[attr->index]))
489 return sprintf(buf, "%d\n",
490 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
492 return sprintf(buf, "0\n");
495 static ssize_t show_force_pwm_max(struct device *dev,
496 struct device_attribute *devattr,
499 struct adt7470_data *data = adt7470_update_device(dev);
500 return sprintf(buf, "%d\n", data->force_pwm_max);
503 static ssize_t set_force_pwm_max(struct device *dev,
504 struct device_attribute *devattr,
508 struct i2c_client *client = to_i2c_client(dev);
509 struct adt7470_data *data = i2c_get_clientdata(client);
510 int temp = simple_strtol(buf, NULL, 10);
513 mutex_lock(&data->lock);
514 data->force_pwm_max = temp;
515 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
517 reg |= ADT7470_FSPD_MASK;
519 reg &= ~ADT7470_FSPD_MASK;
520 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
521 mutex_unlock(&data->lock);
526 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
529 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
530 struct adt7470_data *data = adt7470_update_device(dev);
531 return sprintf(buf, "%d\n", data->pwm[attr->index]);
534 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
535 const char *buf, size_t count)
537 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
538 struct i2c_client *client = to_i2c_client(dev);
539 struct adt7470_data *data = i2c_get_clientdata(client);
540 int temp = simple_strtol(buf, NULL, 10);
542 mutex_lock(&data->lock);
543 data->pwm[attr->index] = temp;
544 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
545 mutex_unlock(&data->lock);
550 static ssize_t show_pwm_max(struct device *dev,
551 struct device_attribute *devattr,
554 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
555 struct adt7470_data *data = adt7470_update_device(dev);
556 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
559 static ssize_t set_pwm_max(struct device *dev,
560 struct device_attribute *devattr,
564 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
565 struct i2c_client *client = to_i2c_client(dev);
566 struct adt7470_data *data = i2c_get_clientdata(client);
567 int temp = simple_strtol(buf, NULL, 10);
569 mutex_lock(&data->lock);
570 data->pwm_max[attr->index] = temp;
571 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
573 mutex_unlock(&data->lock);
578 static ssize_t show_pwm_min(struct device *dev,
579 struct device_attribute *devattr,
582 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
583 struct adt7470_data *data = adt7470_update_device(dev);
584 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
587 static ssize_t set_pwm_min(struct device *dev,
588 struct device_attribute *devattr,
592 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
593 struct i2c_client *client = to_i2c_client(dev);
594 struct adt7470_data *data = i2c_get_clientdata(client);
595 int temp = simple_strtol(buf, NULL, 10);
597 mutex_lock(&data->lock);
598 data->pwm_min[attr->index] = temp;
599 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
601 mutex_unlock(&data->lock);
606 static ssize_t show_pwm_tmax(struct device *dev,
607 struct device_attribute *devattr,
610 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
611 struct adt7470_data *data = adt7470_update_device(dev);
612 /* the datasheet says that tmax = tmin + 20C */
613 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
616 static ssize_t show_pwm_tmin(struct device *dev,
617 struct device_attribute *devattr,
620 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
621 struct adt7470_data *data = adt7470_update_device(dev);
622 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
625 static ssize_t set_pwm_tmin(struct device *dev,
626 struct device_attribute *devattr,
630 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
631 struct i2c_client *client = to_i2c_client(dev);
632 struct adt7470_data *data = i2c_get_clientdata(client);
633 int temp = simple_strtol(buf, NULL, 10) / 1000;
635 mutex_lock(&data->lock);
636 data->pwm_tmin[attr->index] = temp;
637 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
639 mutex_unlock(&data->lock);
644 static ssize_t show_pwm_auto(struct device *dev,
645 struct device_attribute *devattr,
648 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
649 struct adt7470_data *data = adt7470_update_device(dev);
650 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
653 static ssize_t set_pwm_auto(struct device *dev,
654 struct device_attribute *devattr,
658 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
659 struct i2c_client *client = to_i2c_client(dev);
660 struct adt7470_data *data = i2c_get_clientdata(client);
661 int temp = simple_strtol(buf, NULL, 10);
662 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
663 int pwm_auto_reg_mask;
667 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
669 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
671 if (temp != 2 && temp != 1)
675 mutex_lock(&data->lock);
676 data->pwm_automatic[attr->index] = temp;
677 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
679 reg |= pwm_auto_reg_mask;
681 reg &= ~pwm_auto_reg_mask;
682 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
683 mutex_unlock(&data->lock);
688 static ssize_t show_pwm_auto_temp(struct device *dev,
689 struct device_attribute *devattr,
692 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
693 struct adt7470_data *data = adt7470_update_device(dev);
694 u8 ctrl = data->pwm_auto_temp[attr->index];
697 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
699 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
702 static int cvt_auto_temp(int input)
704 if (input == ADT7470_PWM_ALL_TEMPS)
706 if (input < 1 || !is_power_of_2(input))
708 return ilog2(input) + 1;
711 static ssize_t set_pwm_auto_temp(struct device *dev,
712 struct device_attribute *devattr,
716 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
717 struct i2c_client *client = to_i2c_client(dev);
718 struct adt7470_data *data = i2c_get_clientdata(client);
719 int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10));
720 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
726 mutex_lock(&data->lock);
727 data->pwm_automatic[attr->index] = temp;
728 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
730 if (!(attr->index % 2)) {
732 reg |= (temp << 4) & 0xF0;
738 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
739 mutex_unlock(&data->lock);
744 static ssize_t show_alarm(struct device *dev,
745 struct device_attribute *devattr,
748 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
749 struct adt7470_data *data = adt7470_update_device(dev);
751 if (data->alarm & attr->index)
752 return sprintf(buf, "1\n");
754 return sprintf(buf, "0\n");
757 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
759 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
761 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
763 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
765 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
767 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
769 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
771 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
773 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
775 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
777 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
780 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
782 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
784 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
786 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
788 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
790 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
792 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
794 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
796 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
798 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
801 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
802 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
803 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
804 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
805 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
806 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
807 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
808 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
809 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
810 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
812 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
814 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
816 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
818 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
820 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
822 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
824 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
826 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
827 ALARM2(ADT7470_R8T_ALARM));
828 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
829 ALARM2(ADT7470_R9T_ALARM));
830 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
831 ALARM2(ADT7470_R10T_ALARM));
833 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
835 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
837 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
839 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
842 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
844 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
846 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
848 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
851 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
852 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
853 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
854 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
856 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
857 ALARM2(ADT7470_FAN1_ALARM));
858 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
859 ALARM2(ADT7470_FAN2_ALARM));
860 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
861 ALARM2(ADT7470_FAN3_ALARM));
862 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
863 ALARM2(ADT7470_FAN4_ALARM));
865 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
866 show_force_pwm_max, set_force_pwm_max, 0);
868 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
869 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
870 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
871 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
873 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
874 show_pwm_min, set_pwm_min, 0);
875 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
876 show_pwm_min, set_pwm_min, 1);
877 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
878 show_pwm_min, set_pwm_min, 2);
879 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
880 show_pwm_min, set_pwm_min, 3);
882 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
883 show_pwm_max, set_pwm_max, 0);
884 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
885 show_pwm_max, set_pwm_max, 1);
886 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
887 show_pwm_max, set_pwm_max, 2);
888 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
889 show_pwm_max, set_pwm_max, 3);
891 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
892 show_pwm_tmin, set_pwm_tmin, 0);
893 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
894 show_pwm_tmin, set_pwm_tmin, 1);
895 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
896 show_pwm_tmin, set_pwm_tmin, 2);
897 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
898 show_pwm_tmin, set_pwm_tmin, 3);
900 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
902 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
904 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
906 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
909 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
911 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
913 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
915 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
918 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
919 show_pwm_auto_temp, set_pwm_auto_temp, 0);
920 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
921 show_pwm_auto_temp, set_pwm_auto_temp, 1);
922 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
923 show_pwm_auto_temp, set_pwm_auto_temp, 2);
924 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
925 show_pwm_auto_temp, set_pwm_auto_temp, 3);
927 static struct attribute *adt7470_attr[] =
929 &dev_attr_alarm_mask.attr,
930 &sensor_dev_attr_temp1_max.dev_attr.attr,
931 &sensor_dev_attr_temp2_max.dev_attr.attr,
932 &sensor_dev_attr_temp3_max.dev_attr.attr,
933 &sensor_dev_attr_temp4_max.dev_attr.attr,
934 &sensor_dev_attr_temp5_max.dev_attr.attr,
935 &sensor_dev_attr_temp6_max.dev_attr.attr,
936 &sensor_dev_attr_temp7_max.dev_attr.attr,
937 &sensor_dev_attr_temp8_max.dev_attr.attr,
938 &sensor_dev_attr_temp9_max.dev_attr.attr,
939 &sensor_dev_attr_temp10_max.dev_attr.attr,
940 &sensor_dev_attr_temp1_min.dev_attr.attr,
941 &sensor_dev_attr_temp2_min.dev_attr.attr,
942 &sensor_dev_attr_temp3_min.dev_attr.attr,
943 &sensor_dev_attr_temp4_min.dev_attr.attr,
944 &sensor_dev_attr_temp5_min.dev_attr.attr,
945 &sensor_dev_attr_temp6_min.dev_attr.attr,
946 &sensor_dev_attr_temp7_min.dev_attr.attr,
947 &sensor_dev_attr_temp8_min.dev_attr.attr,
948 &sensor_dev_attr_temp9_min.dev_attr.attr,
949 &sensor_dev_attr_temp10_min.dev_attr.attr,
950 &sensor_dev_attr_temp1_input.dev_attr.attr,
951 &sensor_dev_attr_temp2_input.dev_attr.attr,
952 &sensor_dev_attr_temp3_input.dev_attr.attr,
953 &sensor_dev_attr_temp4_input.dev_attr.attr,
954 &sensor_dev_attr_temp5_input.dev_attr.attr,
955 &sensor_dev_attr_temp6_input.dev_attr.attr,
956 &sensor_dev_attr_temp7_input.dev_attr.attr,
957 &sensor_dev_attr_temp8_input.dev_attr.attr,
958 &sensor_dev_attr_temp9_input.dev_attr.attr,
959 &sensor_dev_attr_temp10_input.dev_attr.attr,
960 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
961 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
962 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
963 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
964 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
965 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
966 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
967 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
968 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
969 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
970 &sensor_dev_attr_fan1_max.dev_attr.attr,
971 &sensor_dev_attr_fan2_max.dev_attr.attr,
972 &sensor_dev_attr_fan3_max.dev_attr.attr,
973 &sensor_dev_attr_fan4_max.dev_attr.attr,
974 &sensor_dev_attr_fan1_min.dev_attr.attr,
975 &sensor_dev_attr_fan2_min.dev_attr.attr,
976 &sensor_dev_attr_fan3_min.dev_attr.attr,
977 &sensor_dev_attr_fan4_min.dev_attr.attr,
978 &sensor_dev_attr_fan1_input.dev_attr.attr,
979 &sensor_dev_attr_fan2_input.dev_attr.attr,
980 &sensor_dev_attr_fan3_input.dev_attr.attr,
981 &sensor_dev_attr_fan4_input.dev_attr.attr,
982 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
983 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
984 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
985 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
986 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
987 &sensor_dev_attr_pwm1.dev_attr.attr,
988 &sensor_dev_attr_pwm2.dev_attr.attr,
989 &sensor_dev_attr_pwm3.dev_attr.attr,
990 &sensor_dev_attr_pwm4.dev_attr.attr,
991 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
992 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
993 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
994 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
995 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
996 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
997 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
998 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
999 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1000 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1001 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1002 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
1003 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1004 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1005 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1006 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
1007 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1008 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1009 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1010 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1011 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1012 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1013 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1014 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1018 /* Return 0 if detection is successful, -ENODEV otherwise */
1019 static int adt7470_detect(struct i2c_client *client, int kind,
1020 struct i2c_board_info *info)
1022 struct i2c_adapter *adapter = client->adapter;
1024 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1028 int vendor, device, revision;
1030 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1031 if (vendor != ADT7470_VENDOR)
1034 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1035 if (device != ADT7470_DEVICE)
1038 revision = i2c_smbus_read_byte_data(client,
1039 ADT7470_REG_REVISION);
1040 if (revision != ADT7470_REVISION)
1043 dev_dbg(&adapter->dev, "detection forced\n");
1045 strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
1050 static int adt7470_probe(struct i2c_client *client,
1051 const struct i2c_device_id *id)
1053 struct adt7470_data *data;
1056 data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL);
1062 i2c_set_clientdata(client, data);
1063 mutex_init(&data->lock);
1065 dev_info(&client->dev, "%s chip found\n", client->name);
1067 /* Initialize the ADT7470 chip */
1068 adt7470_init_client(client);
1070 /* Register sysfs hooks */
1071 data->attrs.attrs = adt7470_attr;
1072 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1075 data->hwmon_dev = hwmon_device_register(&client->dev);
1076 if (IS_ERR(data->hwmon_dev)) {
1077 err = PTR_ERR(data->hwmon_dev);
1084 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1091 static int adt7470_remove(struct i2c_client *client)
1093 struct adt7470_data *data = i2c_get_clientdata(client);
1095 hwmon_device_unregister(data->hwmon_dev);
1096 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1101 static int __init adt7470_init(void)
1103 return i2c_add_driver(&adt7470_driver);
1106 static void __exit adt7470_exit(void)
1108 i2c_del_driver(&adt7470_driver);
1111 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1112 MODULE_DESCRIPTION("ADT7470 driver");
1113 MODULE_LICENSE("GPL");
1115 module_init(adt7470_init);
1116 module_exit(adt7470_exit);