2 * A hwmon driver for the Analog Devices ADT7473
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, 0x2D, 0x2E, I2C_CLIENT_END };
35 /* Insmod parameters */
36 I2C_CLIENT_INSMOD_1(adt7473);
38 /* ADT7473 registers */
39 #define ADT7473_REG_BASE_ADDR 0x20
41 #define ADT7473_REG_VOLT_BASE_ADDR 0x21
42 #define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
44 #define ADT7473_REG_TEMP_BASE_ADDR 0x25
45 #define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
46 #define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
47 #define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
49 #define ADT7473_REG_FAN_BASE_ADDR 0x28
50 #define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
52 #define ADT7473_REG_PWM_BASE_ADDR 0x30
53 #define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
54 #define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
55 #define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
56 #define ADT7473_PWM_BHVR_MASK 0xE0
57 #define ADT7473_PWM_BHVR_SHIFT 5
59 #define ADT7473_REG_CFG1 0x40
60 #define ADT7473_CFG1_START 0x01
61 #define ADT7473_CFG1_READY 0x04
62 #define ADT7473_REG_CFG2 0x73
63 #define ADT7473_REG_CFG3 0x78
64 #define ADT7473_REG_CFG4 0x7D
65 #define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
66 #define ADT7473_REG_CFG5 0x7C
67 #define ADT7473_CFG5_TEMP_TWOS 0x01
68 #define ADT7473_CFG5_TEMP_OFFSET 0x02
70 #define ADT7473_REG_DEVICE 0x3D
71 #define ADT7473_VENDOR 0x41
72 #define ADT7473_REG_VENDOR 0x3E
73 #define ADT7473_DEVICE 0x73
74 #define ADT7473_REG_REVISION 0x3F
75 #define ADT7473_REV_68 0x68
76 #define ADT7473_REV_69 0x69
78 #define ADT7473_REG_ALARM1 0x41
79 #define ADT7473_VCCP_ALARM 0x02
80 #define ADT7473_VCC_ALARM 0x04
81 #define ADT7473_R1T_ALARM 0x10
82 #define ADT7473_LT_ALARM 0x20
83 #define ADT7473_R2T_ALARM 0x40
84 #define ADT7473_OOL 0x80
85 #define ADT7473_REG_ALARM2 0x42
86 #define ADT7473_OVT_ALARM 0x02
87 #define ADT7473_FAN1_ALARM 0x04
88 #define ADT7473_FAN2_ALARM 0x08
89 #define ADT7473_FAN3_ALARM 0x10
90 #define ADT7473_FAN4_ALARM 0x20
91 #define ADT7473_R1T_SHORT 0x40
92 #define ADT7473_R2T_SHORT 0x80
94 #define ALARM2(x) ((x) << 8)
96 #define ADT7473_VOLT_COUNT 2
97 #define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
98 #define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
99 #define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
102 #define ADT7473_TEMP_COUNT 3
103 #define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
104 #define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
105 #define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
107 #define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
108 #define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
110 #define ADT7473_FAN_COUNT 4
111 #define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
112 #define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
114 #define ADT7473_PWM_COUNT 3
115 #define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
116 #define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
117 #define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
118 #define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
120 /* How often do we reread sensors values? (In jiffies) */
121 #define SENSOR_REFRESH_INTERVAL (2 * HZ)
123 /* How often do we reread sensor limit values? (In jiffies) */
124 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
126 /* datasheet says to divide this number by the fan reading to get fan rpm */
127 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
128 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
129 #define FAN_PERIOD_INVALID 65535
130 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
132 struct adt7473_data {
133 struct device *hwmon_dev;
134 struct attribute_group attrs;
138 unsigned long sensors_last_updated; /* In jiffies */
139 unsigned long limits_last_updated; /* In jiffies */
141 u8 volt[ADT7473_VOLT_COUNT];
142 s8 volt_min[ADT7473_VOLT_COUNT];
143 s8 volt_max[ADT7473_VOLT_COUNT];
145 s8 temp[ADT7473_TEMP_COUNT];
146 s8 temp_min[ADT7473_TEMP_COUNT];
147 s8 temp_max[ADT7473_TEMP_COUNT];
148 s8 temp_tmin[ADT7473_TEMP_COUNT];
149 /* This is called the !THERM limit in the datasheet */
150 s8 temp_tmax[ADT7473_TEMP_COUNT];
152 u16 fan[ADT7473_FAN_COUNT];
153 u16 fan_min[ADT7473_FAN_COUNT];
155 u8 pwm[ADT7473_PWM_COUNT];
156 u8 pwm_max[ADT7473_PWM_COUNT];
157 u8 pwm_min[ADT7473_PWM_COUNT];
158 u8 pwm_behavior[ADT7473_PWM_COUNT];
160 u8 temp_twos_complement;
164 u8 max_duty_at_overheat;
167 static int adt7473_probe(struct i2c_client *client,
168 const struct i2c_device_id *id);
169 static int adt7473_detect(struct i2c_client *client, int kind,
170 struct i2c_board_info *info);
171 static int adt7473_remove(struct i2c_client *client);
173 static const struct i2c_device_id adt7473_id[] = {
174 { "adt7473", adt7473 },
177 MODULE_DEVICE_TABLE(i2c, adt7473_id);
179 static struct i2c_driver adt7473_driver = {
180 .class = I2C_CLASS_HWMON,
184 .probe = adt7473_probe,
185 .remove = adt7473_remove,
186 .id_table = adt7473_id,
187 .detect = adt7473_detect,
188 .address_data = &addr_data,
192 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
193 * that the low byte must be read before the high byte.
195 static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
198 foo = i2c_smbus_read_byte_data(client, reg);
199 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
203 static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
206 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
207 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
210 static void adt7473_init_client(struct i2c_client *client)
212 int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);
214 if (!(reg & ADT7473_CFG1_READY)) {
215 dev_err(&client->dev, "Chip not ready.\n");
217 /* start monitoring */
218 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
219 reg | ADT7473_CFG1_START);
223 static struct adt7473_data *adt7473_update_device(struct device *dev)
225 struct i2c_client *client = to_i2c_client(dev);
226 struct adt7473_data *data = i2c_get_clientdata(client);
227 unsigned long local_jiffies = jiffies;
231 mutex_lock(&data->lock);
232 if (time_before(local_jiffies, data->sensors_last_updated +
233 SENSOR_REFRESH_INTERVAL)
234 && data->sensors_valid)
235 goto no_sensor_update;
237 for (i = 0; i < ADT7473_VOLT_COUNT; i++)
238 data->volt[i] = i2c_smbus_read_byte_data(client,
239 ADT7473_REG_VOLT(i));
241 /* Determine temperature encoding */
242 cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
243 data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);
246 * What does this do? it implies a variable temperature sensor
247 * offset, but the datasheet doesn't say anything about this bit
248 * and other parts of the datasheet imply that "offset64" mode
249 * means that you shift temp values by -64 if the above bit was set.
251 data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);
253 for (i = 0; i < ADT7473_TEMP_COUNT; i++)
254 data->temp[i] = i2c_smbus_read_byte_data(client,
255 ADT7473_REG_TEMP(i));
257 for (i = 0; i < ADT7473_FAN_COUNT; i++)
258 data->fan[i] = adt7473_read_word_data(client,
261 for (i = 0; i < ADT7473_PWM_COUNT; i++)
262 data->pwm[i] = i2c_smbus_read_byte_data(client,
265 data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
266 if (data->alarm & ADT7473_OOL)
267 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
268 ADT7473_REG_ALARM2));
270 data->sensors_last_updated = local_jiffies;
271 data->sensors_valid = 1;
274 if (time_before(local_jiffies, data->limits_last_updated +
275 LIMIT_REFRESH_INTERVAL)
276 && data->limits_valid)
279 for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
280 data->volt_min[i] = i2c_smbus_read_byte_data(client,
281 ADT7473_REG_VOLT_MIN(i));
282 data->volt_max[i] = i2c_smbus_read_byte_data(client,
283 ADT7473_REG_VOLT_MAX(i));
286 for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
287 data->temp_min[i] = i2c_smbus_read_byte_data(client,
288 ADT7473_REG_TEMP_MIN(i));
289 data->temp_max[i] = i2c_smbus_read_byte_data(client,
290 ADT7473_REG_TEMP_MAX(i));
291 data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
292 ADT7473_REG_TEMP_TMIN(i));
293 data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
294 ADT7473_REG_TEMP_TMAX(i));
297 for (i = 0; i < ADT7473_FAN_COUNT; i++)
298 data->fan_min[i] = adt7473_read_word_data(client,
299 ADT7473_REG_FAN_MIN(i));
301 for (i = 0; i < ADT7473_PWM_COUNT; i++) {
302 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
303 ADT7473_REG_PWM_MAX(i));
304 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
305 ADT7473_REG_PWM_MIN(i));
306 data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
307 ADT7473_REG_PWM_BHVR(i));
310 i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
311 data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT);
313 data->limits_last_updated = local_jiffies;
314 data->limits_valid = 1;
317 mutex_unlock(&data->lock);
325 /* IN are scaled acording to built-in resistors */
326 static const int adt7473_scaling[] = { /* .001 Volts */
329 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
331 static int decode_volt(int volt_index, u8 raw)
333 return SCALE(raw, 192, adt7473_scaling[volt_index]);
336 static u8 encode_volt(int volt_index, int cooked)
338 int raw = SCALE(cooked, adt7473_scaling[volt_index], 192);
339 return SENSORS_LIMIT(raw, 0, 255);
342 static ssize_t show_volt_min(struct device *dev,
343 struct device_attribute *devattr,
346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
347 struct adt7473_data *data = adt7473_update_device(dev);
348 return sprintf(buf, "%d\n",
349 decode_volt(attr->index, data->volt_min[attr->index]));
352 static ssize_t set_volt_min(struct device *dev,
353 struct device_attribute *devattr,
357 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
358 struct i2c_client *client = to_i2c_client(dev);
359 struct adt7473_data *data = i2c_get_clientdata(client);
362 if (strict_strtol(buf, 10, &volt))
365 volt = encode_volt(attr->index, volt);
367 mutex_lock(&data->lock);
368 data->volt_min[attr->index] = volt;
369 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
371 mutex_unlock(&data->lock);
376 static ssize_t show_volt_max(struct device *dev,
377 struct device_attribute *devattr,
380 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
381 struct adt7473_data *data = adt7473_update_device(dev);
382 return sprintf(buf, "%d\n",
383 decode_volt(attr->index, data->volt_max[attr->index]));
386 static ssize_t set_volt_max(struct device *dev,
387 struct device_attribute *devattr,
391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
392 struct i2c_client *client = to_i2c_client(dev);
393 struct adt7473_data *data = i2c_get_clientdata(client);
396 if (strict_strtol(buf, 10, &volt))
399 volt = encode_volt(attr->index, volt);
401 mutex_lock(&data->lock);
402 data->volt_max[attr->index] = volt;
403 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
405 mutex_unlock(&data->lock);
410 static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 struct adt7473_data *data = adt7473_update_device(dev);
416 return sprintf(buf, "%d\n",
417 decode_volt(attr->index, data->volt[attr->index]));
421 * This chip can report temperature data either as a two's complement
422 * number in the range -128 to 127, or as an unsigned number that must
425 static int decode_temp(u8 twos_complement, u8 raw)
427 return twos_complement ? (s8)raw : raw - 64;
430 static u8 encode_temp(u8 twos_complement, int cooked)
432 u8 ret = twos_complement ? cooked & 0xFF : cooked + 64;
433 return SENSORS_LIMIT(ret, 0, 255);
436 static ssize_t show_temp_min(struct device *dev,
437 struct device_attribute *devattr,
440 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
441 struct adt7473_data *data = adt7473_update_device(dev);
442 return sprintf(buf, "%d\n", 1000 * decode_temp(
443 data->temp_twos_complement,
444 data->temp_min[attr->index]));
447 static ssize_t set_temp_min(struct device *dev,
448 struct device_attribute *devattr,
452 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
453 struct i2c_client *client = to_i2c_client(dev);
454 struct adt7473_data *data = i2c_get_clientdata(client);
457 if (strict_strtol(buf, 10, &temp))
460 temp = DIV_ROUND_CLOSEST(temp, 1000);
461 temp = encode_temp(data->temp_twos_complement, temp);
463 mutex_lock(&data->lock);
464 data->temp_min[attr->index] = temp;
465 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
467 mutex_unlock(&data->lock);
472 static ssize_t show_temp_max(struct device *dev,
473 struct device_attribute *devattr,
476 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
477 struct adt7473_data *data = adt7473_update_device(dev);
478 return sprintf(buf, "%d\n", 1000 * decode_temp(
479 data->temp_twos_complement,
480 data->temp_max[attr->index]));
483 static ssize_t set_temp_max(struct device *dev,
484 struct device_attribute *devattr,
488 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
489 struct i2c_client *client = to_i2c_client(dev);
490 struct adt7473_data *data = i2c_get_clientdata(client);
493 if (strict_strtol(buf, 10, &temp))
496 temp = DIV_ROUND_CLOSEST(temp, 1000);
497 temp = encode_temp(data->temp_twos_complement, temp);
499 mutex_lock(&data->lock);
500 data->temp_max[attr->index] = temp;
501 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
503 mutex_unlock(&data->lock);
508 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
511 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512 struct adt7473_data *data = adt7473_update_device(dev);
513 return sprintf(buf, "%d\n", 1000 * decode_temp(
514 data->temp_twos_complement,
515 data->temp[attr->index]));
518 static ssize_t show_fan_min(struct device *dev,
519 struct device_attribute *devattr,
522 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
523 struct adt7473_data *data = adt7473_update_device(dev);
525 if (FAN_DATA_VALID(data->fan_min[attr->index]))
526 return sprintf(buf, "%d\n",
527 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
529 return sprintf(buf, "0\n");
532 static ssize_t set_fan_min(struct device *dev,
533 struct device_attribute *devattr,
534 const char *buf, size_t count)
536 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
537 struct i2c_client *client = to_i2c_client(dev);
538 struct adt7473_data *data = i2c_get_clientdata(client);
541 if (strict_strtol(buf, 10, &temp) || !temp)
544 temp = FAN_RPM_TO_PERIOD(temp);
545 temp = SENSORS_LIMIT(temp, 1, 65534);
547 mutex_lock(&data->lock);
548 data->fan_min[attr->index] = temp;
549 adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
550 mutex_unlock(&data->lock);
555 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
558 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
559 struct adt7473_data *data = adt7473_update_device(dev);
561 if (FAN_DATA_VALID(data->fan[attr->index]))
562 return sprintf(buf, "%d\n",
563 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
565 return sprintf(buf, "0\n");
568 static ssize_t show_max_duty_at_crit(struct device *dev,
569 struct device_attribute *devattr,
572 struct adt7473_data *data = adt7473_update_device(dev);
573 return sprintf(buf, "%d\n", data->max_duty_at_overheat);
576 static ssize_t set_max_duty_at_crit(struct device *dev,
577 struct device_attribute *devattr,
582 struct i2c_client *client = to_i2c_client(dev);
583 struct adt7473_data *data = i2c_get_clientdata(client);
586 if (strict_strtol(buf, 10, &temp))
589 mutex_lock(&data->lock);
590 data->max_duty_at_overheat = !!temp;
591 reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
593 reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
595 reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
596 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
597 mutex_unlock(&data->lock);
602 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
605 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
606 struct adt7473_data *data = adt7473_update_device(dev);
607 return sprintf(buf, "%d\n", data->pwm[attr->index]);
610 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
611 const char *buf, size_t count)
613 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
614 struct i2c_client *client = to_i2c_client(dev);
615 struct adt7473_data *data = i2c_get_clientdata(client);
618 if (strict_strtol(buf, 10, &temp))
621 temp = SENSORS_LIMIT(temp, 0, 255);
623 mutex_lock(&data->lock);
624 data->pwm[attr->index] = temp;
625 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
626 mutex_unlock(&data->lock);
631 static ssize_t show_pwm_max(struct device *dev,
632 struct device_attribute *devattr,
635 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
636 struct adt7473_data *data = adt7473_update_device(dev);
637 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
640 static ssize_t set_pwm_max(struct device *dev,
641 struct device_attribute *devattr,
645 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
646 struct i2c_client *client = to_i2c_client(dev);
647 struct adt7473_data *data = i2c_get_clientdata(client);
650 if (strict_strtol(buf, 10, &temp))
653 temp = SENSORS_LIMIT(temp, 0, 255);
655 mutex_lock(&data->lock);
656 data->pwm_max[attr->index] = temp;
657 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
659 mutex_unlock(&data->lock);
664 static ssize_t show_pwm_min(struct device *dev,
665 struct device_attribute *devattr,
668 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
669 struct adt7473_data *data = adt7473_update_device(dev);
670 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
673 static ssize_t set_pwm_min(struct device *dev,
674 struct device_attribute *devattr,
678 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
679 struct i2c_client *client = to_i2c_client(dev);
680 struct adt7473_data *data = i2c_get_clientdata(client);
683 if (strict_strtol(buf, 10, &temp))
686 temp = SENSORS_LIMIT(temp, 0, 255);
688 mutex_lock(&data->lock);
689 data->pwm_min[attr->index] = temp;
690 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
692 mutex_unlock(&data->lock);
697 static ssize_t show_temp_tmax(struct device *dev,
698 struct device_attribute *devattr,
701 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
702 struct adt7473_data *data = adt7473_update_device(dev);
703 return sprintf(buf, "%d\n", 1000 * decode_temp(
704 data->temp_twos_complement,
705 data->temp_tmax[attr->index]));
708 static ssize_t set_temp_tmax(struct device *dev,
709 struct device_attribute *devattr,
713 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
714 struct i2c_client *client = to_i2c_client(dev);
715 struct adt7473_data *data = i2c_get_clientdata(client);
718 if (strict_strtol(buf, 10, &temp))
721 temp = DIV_ROUND_CLOSEST(temp, 1000);
722 temp = encode_temp(data->temp_twos_complement, temp);
724 mutex_lock(&data->lock);
725 data->temp_tmax[attr->index] = temp;
726 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
728 mutex_unlock(&data->lock);
733 static ssize_t show_temp_tmin(struct device *dev,
734 struct device_attribute *devattr,
737 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
738 struct adt7473_data *data = adt7473_update_device(dev);
739 return sprintf(buf, "%d\n", 1000 * decode_temp(
740 data->temp_twos_complement,
741 data->temp_tmin[attr->index]));
744 static ssize_t set_temp_tmin(struct device *dev,
745 struct device_attribute *devattr,
749 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
750 struct i2c_client *client = to_i2c_client(dev);
751 struct adt7473_data *data = i2c_get_clientdata(client);
754 if (strict_strtol(buf, 10, &temp))
757 temp = DIV_ROUND_CLOSEST(temp, 1000);
758 temp = encode_temp(data->temp_twos_complement, temp);
760 mutex_lock(&data->lock);
761 data->temp_tmin[attr->index] = temp;
762 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
764 mutex_unlock(&data->lock);
769 static ssize_t show_pwm_enable(struct device *dev,
770 struct device_attribute *devattr,
773 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
774 struct adt7473_data *data = adt7473_update_device(dev);
776 switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
778 return sprintf(buf, "0\n");
780 return sprintf(buf, "1\n");
782 return sprintf(buf, "2\n");
786 static ssize_t set_pwm_enable(struct device *dev,
787 struct device_attribute *devattr,
792 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
793 struct i2c_client *client = to_i2c_client(dev);
794 struct adt7473_data *data = i2c_get_clientdata(client);
797 if (strict_strtol(buf, 10, &temp))
808 /* Enter automatic mode with fans off */
815 mutex_lock(&data->lock);
816 reg = i2c_smbus_read_byte_data(client,
817 ADT7473_REG_PWM_BHVR(attr->index));
818 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
819 (reg & ~ADT7473_PWM_BHVR_MASK);
820 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
822 data->pwm_behavior[attr->index] = reg;
823 mutex_unlock(&data->lock);
828 static ssize_t show_pwm_auto_temp(struct device *dev,
829 struct device_attribute *devattr,
832 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
833 struct adt7473_data *data = adt7473_update_device(dev);
834 int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;
840 return sprintf(buf, "0\n");
845 return sprintf(buf, "%d\n", bhvr + 1);
847 return sprintf(buf, "4\n");
849 /* shouldn't ever get here */
853 static ssize_t set_pwm_auto_temp(struct device *dev,
854 struct device_attribute *devattr,
859 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
860 struct i2c_client *client = to_i2c_client(dev);
861 struct adt7473_data *data = i2c_get_clientdata(client);
864 if (strict_strtol(buf, 10, &temp))
881 mutex_lock(&data->lock);
882 reg = i2c_smbus_read_byte_data(client,
883 ADT7473_REG_PWM_BHVR(attr->index));
884 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
885 (reg & ~ADT7473_PWM_BHVR_MASK);
886 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
888 data->pwm_behavior[attr->index] = reg;
889 mutex_unlock(&data->lock);
894 static ssize_t show_alarm(struct device *dev,
895 struct device_attribute *devattr,
898 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
899 struct adt7473_data *data = adt7473_update_device(dev);
901 if (data->alarm & attr->index)
902 return sprintf(buf, "1\n");
904 return sprintf(buf, "0\n");
908 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
910 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
913 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
915 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
918 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
919 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);
921 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
923 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
926 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
928 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
930 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
933 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
935 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
937 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
940 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
941 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
942 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
944 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
945 ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
946 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
948 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
949 ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));
951 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
953 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
955 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
957 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
960 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
961 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
962 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
963 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
965 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
966 ALARM2(ADT7473_FAN1_ALARM));
967 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
968 ALARM2(ADT7473_FAN2_ALARM));
969 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
970 ALARM2(ADT7473_FAN3_ALARM));
971 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
972 ALARM2(ADT7473_FAN4_ALARM));
974 static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
975 show_max_duty_at_crit, set_max_duty_at_crit, 0);
977 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
978 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
979 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
981 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
982 show_pwm_min, set_pwm_min, 0);
983 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
984 show_pwm_min, set_pwm_min, 1);
985 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
986 show_pwm_min, set_pwm_min, 2);
988 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
989 show_pwm_max, set_pwm_max, 0);
990 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
991 show_pwm_max, set_pwm_max, 1);
992 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
993 show_pwm_max, set_pwm_max, 2);
995 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
996 show_temp_tmin, set_temp_tmin, 0);
997 static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
998 show_temp_tmin, set_temp_tmin, 1);
999 static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
1000 show_temp_tmin, set_temp_tmin, 2);
1002 static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
1003 show_temp_tmax, set_temp_tmax, 0);
1004 static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
1005 show_temp_tmax, set_temp_tmax, 1);
1006 static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
1007 show_temp_tmax, set_temp_tmax, 2);
1009 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1011 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1013 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1016 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1017 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1018 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1019 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1020 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1021 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1023 static struct attribute *adt7473_attr[] =
1025 &sensor_dev_attr_in1_max.dev_attr.attr,
1026 &sensor_dev_attr_in2_max.dev_attr.attr,
1027 &sensor_dev_attr_in1_min.dev_attr.attr,
1028 &sensor_dev_attr_in2_min.dev_attr.attr,
1029 &sensor_dev_attr_in1_input.dev_attr.attr,
1030 &sensor_dev_attr_in2_input.dev_attr.attr,
1031 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1032 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1034 &sensor_dev_attr_temp1_max.dev_attr.attr,
1035 &sensor_dev_attr_temp2_max.dev_attr.attr,
1036 &sensor_dev_attr_temp3_max.dev_attr.attr,
1037 &sensor_dev_attr_temp1_min.dev_attr.attr,
1038 &sensor_dev_attr_temp2_min.dev_attr.attr,
1039 &sensor_dev_attr_temp3_min.dev_attr.attr,
1040 &sensor_dev_attr_temp1_input.dev_attr.attr,
1041 &sensor_dev_attr_temp2_input.dev_attr.attr,
1042 &sensor_dev_attr_temp3_input.dev_attr.attr,
1043 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1044 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1045 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1046 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1047 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1048 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1049 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1050 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1051 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1053 &sensor_dev_attr_fan1_min.dev_attr.attr,
1054 &sensor_dev_attr_fan2_min.dev_attr.attr,
1055 &sensor_dev_attr_fan3_min.dev_attr.attr,
1056 &sensor_dev_attr_fan4_min.dev_attr.attr,
1057 &sensor_dev_attr_fan1_input.dev_attr.attr,
1058 &sensor_dev_attr_fan2_input.dev_attr.attr,
1059 &sensor_dev_attr_fan3_input.dev_attr.attr,
1060 &sensor_dev_attr_fan4_input.dev_attr.attr,
1061 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1062 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1063 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1064 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1066 &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,
1068 &sensor_dev_attr_pwm1.dev_attr.attr,
1069 &sensor_dev_attr_pwm2.dev_attr.attr,
1070 &sensor_dev_attr_pwm3.dev_attr.attr,
1071 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1072 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1073 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1074 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1075 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1076 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1078 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1079 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1080 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1081 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1082 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1083 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1088 /* Return 0 if detection is successful, -ENODEV otherwise */
1089 static int adt7473_detect(struct i2c_client *client, int kind,
1090 struct i2c_board_info *info)
1092 struct i2c_adapter *adapter = client->adapter;
1094 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1098 int vendor, device, revision;
1100 vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
1101 if (vendor != ADT7473_VENDOR)
1104 device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
1105 if (device != ADT7473_DEVICE)
1108 revision = i2c_smbus_read_byte_data(client,
1109 ADT7473_REG_REVISION);
1110 if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69)
1113 dev_dbg(&adapter->dev, "detection forced\n");
1115 strlcpy(info->type, "adt7473", I2C_NAME_SIZE);
1120 static int adt7473_probe(struct i2c_client *client,
1121 const struct i2c_device_id *id)
1123 struct adt7473_data *data;
1126 data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
1132 i2c_set_clientdata(client, data);
1133 mutex_init(&data->lock);
1135 dev_info(&client->dev, "%s chip found\n", client->name);
1137 /* Initialize the ADT7473 chip */
1138 adt7473_init_client(client);
1140 /* Register sysfs hooks */
1141 data->attrs.attrs = adt7473_attr;
1142 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1146 data->hwmon_dev = hwmon_device_register(&client->dev);
1147 if (IS_ERR(data->hwmon_dev)) {
1148 err = PTR_ERR(data->hwmon_dev);
1155 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1162 static int adt7473_remove(struct i2c_client *client)
1164 struct adt7473_data *data = i2c_get_clientdata(client);
1166 hwmon_device_unregister(data->hwmon_dev);
1167 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1172 static int __init adt7473_init(void)
1174 return i2c_add_driver(&adt7473_driver);
1177 static void __exit adt7473_exit(void)
1179 i2c_del_driver(&adt7473_driver);
1182 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1183 MODULE_DESCRIPTION("ADT7473 driver");
1184 MODULE_LICENSE("GPL");
1186 module_init(adt7473_init);
1187 module_exit(adt7473_exit);