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 i2c_client client;
142 struct device *hwmon_dev;
143 struct attribute_group attrs;
147 unsigned long sensors_last_updated; /* In jiffies */
148 unsigned long limits_last_updated; /* In jiffies */
150 s8 temp[ADT7470_TEMP_COUNT];
151 s8 temp_min[ADT7470_TEMP_COUNT];
152 s8 temp_max[ADT7470_TEMP_COUNT];
153 u16 fan[ADT7470_FAN_COUNT];
154 u16 fan_min[ADT7470_FAN_COUNT];
155 u16 fan_max[ADT7470_FAN_COUNT];
159 u8 pwm[ADT7470_PWM_COUNT];
160 u8 pwm_max[ADT7470_PWM_COUNT];
161 u8 pwm_automatic[ADT7470_PWM_COUNT];
162 u8 pwm_min[ADT7470_PWM_COUNT];
163 s8 pwm_tmin[ADT7470_PWM_COUNT];
164 u8 pwm_auto_temp[ADT7470_PWM_COUNT];
167 static int adt7470_attach_adapter(struct i2c_adapter *adapter);
168 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind);
169 static int adt7470_detach_client(struct i2c_client *client);
171 static struct i2c_driver adt7470_driver = {
175 .attach_adapter = adt7470_attach_adapter,
176 .detach_client = adt7470_detach_client,
180 * 16-bit registers on the ADT7470 are low-byte first. The data sheet says
181 * that the low byte must be read before the high byte.
183 static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
186 foo = i2c_smbus_read_byte_data(client, reg);
187 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
191 static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
194 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
195 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
198 static void adt7470_init_client(struct i2c_client *client)
200 int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
203 dev_err(&client->dev, "cannot read configuration register\n");
205 /* start monitoring (and do a self-test) */
206 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
210 static struct adt7470_data *adt7470_update_device(struct device *dev)
212 struct i2c_client *client = to_i2c_client(dev);
213 struct adt7470_data *data = i2c_get_clientdata(client);
214 unsigned long local_jiffies = jiffies;
218 mutex_lock(&data->lock);
219 if (time_before(local_jiffies, data->sensors_last_updated +
220 SENSOR_REFRESH_INTERVAL)
221 && data->sensors_valid)
222 goto no_sensor_update;
224 /* start reading temperature sensors */
225 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
227 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
230 * Delay is 200ms * number of tmp05 sensors. Too bad
231 * there's no way to figure out how many are connected.
232 * For now, assume 1s will work.
234 msleep(TEMP_COLLECTION_TIME);
236 /* done reading temperature sensors */
237 cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
239 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
241 for (i = 0; i < ADT7470_TEMP_COUNT; i++)
242 data->temp[i] = i2c_smbus_read_byte_data(client,
243 ADT7470_TEMP_REG(i));
245 for (i = 0; i < ADT7470_FAN_COUNT; i++)
246 data->fan[i] = adt7470_read_word_data(client,
249 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
253 data->pwm[i] = i2c_smbus_read_byte_data(client,
257 reg_mask = ADT7470_PWM2_AUTO_MASK;
259 reg_mask = ADT7470_PWM1_AUTO_MASK;
261 reg = ADT7470_REG_PWM_CFG(i);
262 if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
263 data->pwm_automatic[i] = 1;
265 data->pwm_automatic[i] = 0;
267 reg = ADT7470_REG_PWM_AUTO_TEMP(i);
268 cfg = i2c_smbus_read_byte_data(client, reg);
270 data->pwm_auto_temp[i] = cfg >> 4;
272 data->pwm_auto_temp[i] = cfg & 0xF;
275 if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
277 data->force_pwm_max = 1;
279 data->force_pwm_max = 0;
281 data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
282 if (data->alarm & ADT7470_OOL_ALARM)
283 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
284 ADT7470_REG_ALARM2));
285 data->alarms_mask = adt7470_read_word_data(client,
286 ADT7470_REG_ALARM1_MASK);
288 data->sensors_last_updated = local_jiffies;
289 data->sensors_valid = 1;
292 if (time_before(local_jiffies, data->limits_last_updated +
293 LIMIT_REFRESH_INTERVAL)
294 && data->limits_valid)
297 for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
298 data->temp_min[i] = i2c_smbus_read_byte_data(client,
299 ADT7470_TEMP_MIN_REG(i));
300 data->temp_max[i] = i2c_smbus_read_byte_data(client,
301 ADT7470_TEMP_MAX_REG(i));
304 for (i = 0; i < ADT7470_FAN_COUNT; i++) {
305 data->fan_min[i] = adt7470_read_word_data(client,
306 ADT7470_REG_FAN_MIN(i));
307 data->fan_max[i] = adt7470_read_word_data(client,
308 ADT7470_REG_FAN_MAX(i));
311 for (i = 0; i < ADT7470_PWM_COUNT; i++) {
312 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
313 ADT7470_REG_PWM_MAX(i));
314 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
315 ADT7470_REG_PWM_MIN(i));
316 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
317 ADT7470_REG_PWM_TMIN(i));
320 data->limits_last_updated = local_jiffies;
321 data->limits_valid = 1;
324 mutex_unlock(&data->lock);
328 static ssize_t show_temp_min(struct device *dev,
329 struct device_attribute *devattr,
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
333 struct adt7470_data *data = adt7470_update_device(dev);
334 return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
337 static ssize_t set_temp_min(struct device *dev,
338 struct device_attribute *devattr,
342 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
343 struct i2c_client *client = to_i2c_client(dev);
344 struct adt7470_data *data = i2c_get_clientdata(client);
345 int temp = simple_strtol(buf, NULL, 10) / 1000;
347 mutex_lock(&data->lock);
348 data->temp_min[attr->index] = temp;
349 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
351 mutex_unlock(&data->lock);
356 static ssize_t show_temp_max(struct device *dev,
357 struct device_attribute *devattr,
360 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
361 struct adt7470_data *data = adt7470_update_device(dev);
362 return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
365 static ssize_t set_temp_max(struct device *dev,
366 struct device_attribute *devattr,
370 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
371 struct i2c_client *client = to_i2c_client(dev);
372 struct adt7470_data *data = i2c_get_clientdata(client);
373 int temp = simple_strtol(buf, NULL, 10) / 1000;
375 mutex_lock(&data->lock);
376 data->temp_max[attr->index] = temp;
377 i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
379 mutex_unlock(&data->lock);
384 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
387 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
388 struct adt7470_data *data = adt7470_update_device(dev);
389 return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
392 static ssize_t show_alarm_mask(struct device *dev,
393 struct device_attribute *devattr,
396 struct adt7470_data *data = adt7470_update_device(dev);
398 return sprintf(buf, "%x\n", data->alarms_mask);
401 static ssize_t show_fan_max(struct device *dev,
402 struct device_attribute *devattr,
405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
406 struct adt7470_data *data = adt7470_update_device(dev);
408 if (FAN_DATA_VALID(data->fan_max[attr->index]))
409 return sprintf(buf, "%d\n",
410 FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
412 return sprintf(buf, "0\n");
415 static ssize_t set_fan_max(struct device *dev,
416 struct device_attribute *devattr,
417 const char *buf, size_t count)
419 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420 struct i2c_client *client = to_i2c_client(dev);
421 struct adt7470_data *data = i2c_get_clientdata(client);
422 int temp = simple_strtol(buf, NULL, 10);
426 temp = FAN_RPM_TO_PERIOD(temp);
428 mutex_lock(&data->lock);
429 data->fan_max[attr->index] = temp;
430 adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
431 mutex_unlock(&data->lock);
436 static ssize_t show_fan_min(struct device *dev,
437 struct device_attribute *devattr,
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 struct adt7470_data *data = adt7470_update_device(dev);
443 if (FAN_DATA_VALID(data->fan_min[attr->index]))
444 return sprintf(buf, "%d\n",
445 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
447 return sprintf(buf, "0\n");
450 static ssize_t set_fan_min(struct device *dev,
451 struct device_attribute *devattr,
452 const char *buf, size_t count)
454 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adt7470_data *data = i2c_get_clientdata(client);
457 int temp = simple_strtol(buf, NULL, 10);
461 temp = FAN_RPM_TO_PERIOD(temp);
463 mutex_lock(&data->lock);
464 data->fan_min[attr->index] = temp;
465 adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
466 mutex_unlock(&data->lock);
471 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
474 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
475 struct adt7470_data *data = adt7470_update_device(dev);
477 if (FAN_DATA_VALID(data->fan[attr->index]))
478 return sprintf(buf, "%d\n",
479 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
481 return sprintf(buf, "0\n");
484 static ssize_t show_force_pwm_max(struct device *dev,
485 struct device_attribute *devattr,
488 struct adt7470_data *data = adt7470_update_device(dev);
489 return sprintf(buf, "%d\n", data->force_pwm_max);
492 static ssize_t set_force_pwm_max(struct device *dev,
493 struct device_attribute *devattr,
497 struct i2c_client *client = to_i2c_client(dev);
498 struct adt7470_data *data = i2c_get_clientdata(client);
499 int temp = simple_strtol(buf, NULL, 10);
502 mutex_lock(&data->lock);
503 data->force_pwm_max = temp;
504 reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
506 reg |= ADT7470_FSPD_MASK;
508 reg &= ~ADT7470_FSPD_MASK;
509 i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
510 mutex_unlock(&data->lock);
515 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
518 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
519 struct adt7470_data *data = adt7470_update_device(dev);
520 return sprintf(buf, "%d\n", data->pwm[attr->index]);
523 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
524 const char *buf, size_t count)
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 struct i2c_client *client = to_i2c_client(dev);
528 struct adt7470_data *data = i2c_get_clientdata(client);
529 int temp = simple_strtol(buf, NULL, 10);
531 mutex_lock(&data->lock);
532 data->pwm[attr->index] = temp;
533 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
534 mutex_unlock(&data->lock);
539 static ssize_t show_pwm_max(struct device *dev,
540 struct device_attribute *devattr,
543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544 struct adt7470_data *data = adt7470_update_device(dev);
545 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
548 static ssize_t set_pwm_max(struct device *dev,
549 struct device_attribute *devattr,
553 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
554 struct i2c_client *client = to_i2c_client(dev);
555 struct adt7470_data *data = i2c_get_clientdata(client);
556 int temp = simple_strtol(buf, NULL, 10);
558 mutex_lock(&data->lock);
559 data->pwm_max[attr->index] = temp;
560 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
562 mutex_unlock(&data->lock);
567 static ssize_t show_pwm_min(struct device *dev,
568 struct device_attribute *devattr,
571 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
572 struct adt7470_data *data = adt7470_update_device(dev);
573 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
576 static ssize_t set_pwm_min(struct device *dev,
577 struct device_attribute *devattr,
581 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
582 struct i2c_client *client = to_i2c_client(dev);
583 struct adt7470_data *data = i2c_get_clientdata(client);
584 int temp = simple_strtol(buf, NULL, 10);
586 mutex_lock(&data->lock);
587 data->pwm_min[attr->index] = temp;
588 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
590 mutex_unlock(&data->lock);
595 static ssize_t show_pwm_tmax(struct device *dev,
596 struct device_attribute *devattr,
599 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
600 struct adt7470_data *data = adt7470_update_device(dev);
601 /* the datasheet says that tmax = tmin + 20C */
602 return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
605 static ssize_t show_pwm_tmin(struct device *dev,
606 struct device_attribute *devattr,
609 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
610 struct adt7470_data *data = adt7470_update_device(dev);
611 return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
614 static ssize_t set_pwm_tmin(struct device *dev,
615 struct device_attribute *devattr,
619 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
620 struct i2c_client *client = to_i2c_client(dev);
621 struct adt7470_data *data = i2c_get_clientdata(client);
622 int temp = simple_strtol(buf, NULL, 10) / 1000;
624 mutex_lock(&data->lock);
625 data->pwm_tmin[attr->index] = temp;
626 i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
628 mutex_unlock(&data->lock);
633 static ssize_t show_pwm_auto(struct device *dev,
634 struct device_attribute *devattr,
637 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
638 struct adt7470_data *data = adt7470_update_device(dev);
639 return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
642 static ssize_t set_pwm_auto(struct device *dev,
643 struct device_attribute *devattr,
647 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
648 struct i2c_client *client = to_i2c_client(dev);
649 struct adt7470_data *data = i2c_get_clientdata(client);
650 int temp = simple_strtol(buf, NULL, 10);
651 int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
652 int pwm_auto_reg_mask;
656 pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
658 pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
660 if (temp != 2 && temp != 1)
664 mutex_lock(&data->lock);
665 data->pwm_automatic[attr->index] = temp;
666 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
668 reg |= pwm_auto_reg_mask;
670 reg &= ~pwm_auto_reg_mask;
671 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
672 mutex_unlock(&data->lock);
677 static ssize_t show_pwm_auto_temp(struct device *dev,
678 struct device_attribute *devattr,
681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 struct adt7470_data *data = adt7470_update_device(dev);
683 u8 ctrl = data->pwm_auto_temp[attr->index];
686 return sprintf(buf, "%d\n", 1 << (ctrl - 1));
688 return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
691 static int cvt_auto_temp(int input)
693 if (input == ADT7470_PWM_ALL_TEMPS)
695 if (input < 1 || !is_power_of_2(input))
697 return ilog2(input) + 1;
700 static ssize_t set_pwm_auto_temp(struct device *dev,
701 struct device_attribute *devattr,
705 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
706 struct i2c_client *client = to_i2c_client(dev);
707 struct adt7470_data *data = i2c_get_clientdata(client);
708 int temp = cvt_auto_temp(simple_strtol(buf, NULL, 10));
709 int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
715 mutex_lock(&data->lock);
716 data->pwm_automatic[attr->index] = temp;
717 reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
719 if (!(attr->index % 2)) {
721 reg |= (temp << 4) & 0xF0;
727 i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
728 mutex_unlock(&data->lock);
733 static ssize_t show_alarm(struct device *dev,
734 struct device_attribute *devattr,
737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
738 struct adt7470_data *data = adt7470_update_device(dev);
740 if (data->alarm & attr->index)
741 return sprintf(buf, "1\n");
743 return sprintf(buf, "0\n");
746 static DEVICE_ATTR(alarm_mask, S_IRUGO, show_alarm_mask, NULL);
748 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
750 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
752 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
754 static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
756 static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
758 static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
760 static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
762 static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
764 static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
766 static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
769 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
771 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
773 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
775 static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
777 static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
779 static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
781 static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
783 static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
785 static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
787 static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
790 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
791 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
792 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
793 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
794 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
795 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
796 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
797 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
798 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
799 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
801 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
803 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
805 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
807 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
809 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
811 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
813 static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
815 static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
816 ALARM2(ADT7470_R8T_ALARM));
817 static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
818 ALARM2(ADT7470_R9T_ALARM));
819 static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
820 ALARM2(ADT7470_R10T_ALARM));
822 static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
824 static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
826 static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
828 static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
831 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
833 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
835 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
837 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
840 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
841 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
842 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
843 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
845 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
846 ALARM2(ADT7470_FAN1_ALARM));
847 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
848 ALARM2(ADT7470_FAN2_ALARM));
849 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
850 ALARM2(ADT7470_FAN3_ALARM));
851 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
852 ALARM2(ADT7470_FAN4_ALARM));
854 static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
855 show_force_pwm_max, set_force_pwm_max, 0);
857 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
858 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
859 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
860 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
862 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
863 show_pwm_min, set_pwm_min, 0);
864 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
865 show_pwm_min, set_pwm_min, 1);
866 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
867 show_pwm_min, set_pwm_min, 2);
868 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
869 show_pwm_min, set_pwm_min, 3);
871 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
872 show_pwm_max, set_pwm_max, 0);
873 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
874 show_pwm_max, set_pwm_max, 1);
875 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
876 show_pwm_max, set_pwm_max, 2);
877 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
878 show_pwm_max, set_pwm_max, 3);
880 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
881 show_pwm_tmin, set_pwm_tmin, 0);
882 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
883 show_pwm_tmin, set_pwm_tmin, 1);
884 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
885 show_pwm_tmin, set_pwm_tmin, 2);
886 static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
887 show_pwm_tmin, set_pwm_tmin, 3);
889 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
891 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
893 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
895 static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
898 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
900 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
902 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
904 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
907 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
908 show_pwm_auto_temp, set_pwm_auto_temp, 0);
909 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
910 show_pwm_auto_temp, set_pwm_auto_temp, 1);
911 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
912 show_pwm_auto_temp, set_pwm_auto_temp, 2);
913 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
914 show_pwm_auto_temp, set_pwm_auto_temp, 3);
916 static struct attribute *adt7470_attr[] =
918 &dev_attr_alarm_mask.attr,
919 &sensor_dev_attr_temp1_max.dev_attr.attr,
920 &sensor_dev_attr_temp2_max.dev_attr.attr,
921 &sensor_dev_attr_temp3_max.dev_attr.attr,
922 &sensor_dev_attr_temp4_max.dev_attr.attr,
923 &sensor_dev_attr_temp5_max.dev_attr.attr,
924 &sensor_dev_attr_temp6_max.dev_attr.attr,
925 &sensor_dev_attr_temp7_max.dev_attr.attr,
926 &sensor_dev_attr_temp8_max.dev_attr.attr,
927 &sensor_dev_attr_temp9_max.dev_attr.attr,
928 &sensor_dev_attr_temp10_max.dev_attr.attr,
929 &sensor_dev_attr_temp1_min.dev_attr.attr,
930 &sensor_dev_attr_temp2_min.dev_attr.attr,
931 &sensor_dev_attr_temp3_min.dev_attr.attr,
932 &sensor_dev_attr_temp4_min.dev_attr.attr,
933 &sensor_dev_attr_temp5_min.dev_attr.attr,
934 &sensor_dev_attr_temp6_min.dev_attr.attr,
935 &sensor_dev_attr_temp7_min.dev_attr.attr,
936 &sensor_dev_attr_temp8_min.dev_attr.attr,
937 &sensor_dev_attr_temp9_min.dev_attr.attr,
938 &sensor_dev_attr_temp10_min.dev_attr.attr,
939 &sensor_dev_attr_temp1_input.dev_attr.attr,
940 &sensor_dev_attr_temp2_input.dev_attr.attr,
941 &sensor_dev_attr_temp3_input.dev_attr.attr,
942 &sensor_dev_attr_temp4_input.dev_attr.attr,
943 &sensor_dev_attr_temp5_input.dev_attr.attr,
944 &sensor_dev_attr_temp6_input.dev_attr.attr,
945 &sensor_dev_attr_temp7_input.dev_attr.attr,
946 &sensor_dev_attr_temp8_input.dev_attr.attr,
947 &sensor_dev_attr_temp9_input.dev_attr.attr,
948 &sensor_dev_attr_temp10_input.dev_attr.attr,
949 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
950 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
951 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
952 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
953 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
954 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
955 &sensor_dev_attr_temp7_alarm.dev_attr.attr,
956 &sensor_dev_attr_temp8_alarm.dev_attr.attr,
957 &sensor_dev_attr_temp9_alarm.dev_attr.attr,
958 &sensor_dev_attr_temp10_alarm.dev_attr.attr,
959 &sensor_dev_attr_fan1_max.dev_attr.attr,
960 &sensor_dev_attr_fan2_max.dev_attr.attr,
961 &sensor_dev_attr_fan3_max.dev_attr.attr,
962 &sensor_dev_attr_fan4_max.dev_attr.attr,
963 &sensor_dev_attr_fan1_min.dev_attr.attr,
964 &sensor_dev_attr_fan2_min.dev_attr.attr,
965 &sensor_dev_attr_fan3_min.dev_attr.attr,
966 &sensor_dev_attr_fan4_min.dev_attr.attr,
967 &sensor_dev_attr_fan1_input.dev_attr.attr,
968 &sensor_dev_attr_fan2_input.dev_attr.attr,
969 &sensor_dev_attr_fan3_input.dev_attr.attr,
970 &sensor_dev_attr_fan4_input.dev_attr.attr,
971 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
972 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
973 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
974 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
975 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
976 &sensor_dev_attr_pwm1.dev_attr.attr,
977 &sensor_dev_attr_pwm2.dev_attr.attr,
978 &sensor_dev_attr_pwm3.dev_attr.attr,
979 &sensor_dev_attr_pwm4.dev_attr.attr,
980 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
981 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
982 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
983 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
984 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
985 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
986 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
987 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
988 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
989 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
990 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
991 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
992 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
993 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
994 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
995 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
996 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
997 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
998 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
999 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1000 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1001 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1002 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1003 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1007 static int adt7470_attach_adapter(struct i2c_adapter *adapter)
1009 if (!(adapter->class & I2C_CLASS_HWMON))
1011 return i2c_probe(adapter, &addr_data, adt7470_detect);
1014 static int adt7470_detect(struct i2c_adapter *adapter, int address, int kind)
1016 struct i2c_client *client;
1017 struct adt7470_data *data;
1020 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1023 if (!(data = kzalloc(sizeof(struct adt7470_data), GFP_KERNEL))) {
1028 client = &data->client;
1029 client->addr = address;
1030 client->adapter = adapter;
1031 client->driver = &adt7470_driver;
1033 i2c_set_clientdata(client, data);
1035 mutex_init(&data->lock);
1038 int vendor, device, revision;
1040 vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1041 if (vendor != ADT7470_VENDOR) {
1046 device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1047 if (device != ADT7470_DEVICE) {
1052 revision = i2c_smbus_read_byte_data(client,
1053 ADT7470_REG_REVISION);
1054 if (revision != ADT7470_REVISION) {
1059 dev_dbg(&adapter->dev, "detection forced\n");
1061 strlcpy(client->name, "adt7470", I2C_NAME_SIZE);
1063 if ((err = i2c_attach_client(client)))
1066 dev_info(&client->dev, "%s chip found\n", client->name);
1068 /* Initialize the ADT7470 chip */
1069 adt7470_init_client(client);
1071 /* Register sysfs hooks */
1072 data->attrs.attrs = adt7470_attr;
1073 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
1076 data->hwmon_dev = hwmon_device_register(&client->dev);
1077 if (IS_ERR(data->hwmon_dev)) {
1078 err = PTR_ERR(data->hwmon_dev);
1085 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1087 i2c_detach_client(client);
1094 static int adt7470_detach_client(struct i2c_client *client)
1096 struct adt7470_data *data = i2c_get_clientdata(client);
1098 hwmon_device_unregister(data->hwmon_dev);
1099 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1100 i2c_detach_client(client);
1105 static int __init adt7470_init(void)
1107 return i2c_add_driver(&adt7470_driver);
1110 static void __exit adt7470_exit(void)
1112 i2c_del_driver(&adt7470_driver);
1115 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1116 MODULE_DESCRIPTION("ADT7470 driver");
1117 MODULE_LICENSE("GPL");
1119 module_init(adt7470_init);
1120 module_exit(adt7470_exit);