2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
7 * Derived from the lm83 driver by Jean Delvare
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/err.h>
22 /* Indexes for the sysfs hooks */
33 /* These are unique identifiers for the sysfs functions - unlike the
34 numbers above, these are not also indexes into an array
40 /* 7475 Common Registers */
42 #define REG_VOLTAGE_BASE 0x21
43 #define REG_TEMP_BASE 0x25
44 #define REG_TACH_BASE 0x28
45 #define REG_PWM_BASE 0x30
46 #define REG_PWM_MAX_BASE 0x38
48 #define REG_DEVID 0x3D
49 #define REG_VENDID 0x3E
51 #define REG_STATUS1 0x41
52 #define REG_STATUS2 0x42
54 #define REG_VOLTAGE_MIN_BASE 0x46
55 #define REG_VOLTAGE_MAX_BASE 0x47
57 #define REG_TEMP_MIN_BASE 0x4E
58 #define REG_TEMP_MAX_BASE 0x4F
60 #define REG_TACH_MIN_BASE 0x54
62 #define REG_PWM_CONFIG_BASE 0x5C
64 #define REG_TEMP_TRANGE_BASE 0x5F
66 #define REG_PWM_MIN_BASE 0x64
68 #define REG_TEMP_TMIN_BASE 0x67
69 #define REG_TEMP_THERM_BASE 0x6A
71 #define REG_REMOTE1_HYSTERSIS 0x6D
72 #define REG_REMOTE2_HYSTERSIS 0x6E
74 #define REG_TEMP_OFFSET_BASE 0x70
76 #define REG_EXTEND1 0x76
77 #define REG_EXTEND2 0x77
78 #define REG_CONFIG5 0x7C
80 #define CONFIG5_TWOSCOMP 0x01
81 #define CONFIG5_TEMPOFFSET 0x02
83 /* ADT7475 Settings */
85 #define ADT7475_VOLTAGE_COUNT 2
86 #define ADT7475_TEMP_COUNT 3
87 #define ADT7475_TACH_COUNT 4
88 #define ADT7475_PWM_COUNT 3
90 /* Macro to read the registers */
92 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
94 /* Macros to easily index the registers */
96 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
97 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
99 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
100 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
101 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
102 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
104 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
105 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
106 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
108 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
109 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
110 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
111 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
112 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
113 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
114 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
116 static unsigned short normal_i2c[] = { 0x2e, I2C_CLIENT_END };
118 I2C_CLIENT_INSMOD_1(adt7475);
120 static const struct i2c_device_id adt7475_id[] = {
121 { "adt7475", adt7475 },
124 MODULE_DEVICE_TABLE(i2c, adt7475_id);
126 struct adt7475_data {
127 struct device *hwmon_dev;
130 unsigned long measure_updated;
131 unsigned long limits_updated;
145 static struct i2c_driver adt7475_driver;
146 static struct adt7475_data *adt7475_update_device(struct device *dev);
147 static void adt7475_read_hystersis(struct i2c_client *client);
148 static void adt7475_read_pwm(struct i2c_client *client, int index);
150 /* Given a temp value, convert it to register value */
152 static inline u16 temp2reg(struct adt7475_data *data, long val)
156 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
157 val = SENSORS_LIMIT(val, -64000, 191000);
158 ret = (val + 64500) / 1000;
160 val = SENSORS_LIMIT(val, -128000, 127000);
162 ret = (256500 + val) / 1000;
164 ret = (val + 500) / 1000;
170 /* Given a register value, convert it to a real temp value */
172 static inline int reg2temp(struct adt7475_data *data, u16 reg)
174 if (data->config5 & CONFIG5_TWOSCOMP) {
176 return (reg - 1024) * 250;
180 return (reg - 256) * 250;
183 static inline int tach2rpm(u16 tach)
185 if (tach == 0 || tach == 0xFFFF)
188 return (90000 * 60) / tach;
191 static inline u16 rpm2tach(unsigned long rpm)
196 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
199 static inline int reg2vcc(u16 reg)
201 return (4296 * reg) / 1000;
204 static inline int reg2vccp(u16 reg)
206 return (2929 * reg) / 1000;
209 static inline u16 vcc2reg(long vcc)
211 vcc = SENSORS_LIMIT(vcc, 0, 4396);
212 return (vcc * 1000) / 4296;
215 static inline u16 vccp2reg(long vcc)
217 vcc = SENSORS_LIMIT(vcc, 0, 2998);
218 return (vcc * 1000) / 2929;
221 static u16 adt7475_read_word(struct i2c_client *client, int reg)
225 val = i2c_smbus_read_byte_data(client, reg);
226 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
231 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
233 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
234 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
237 /* Find the nearest value in a table - used for pwm frequency and
239 static int find_nearest(long val, const int *array, int size)
246 if (val > array[size - 1])
249 for (i = 0; i < size - 1; i++) {
252 if (val > array[i + 1])
256 b = array[i + 1] - val;
258 return (a <= b) ? i : i + 1;
264 static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
267 struct adt7475_data *data = adt7475_update_device(dev);
268 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
273 return sprintf(buf, "%d\n",
274 (data->alarms >> (sattr->index + 1)) & 1);
276 val = data->voltage[sattr->nr][sattr->index];
277 return sprintf(buf, "%d\n",
279 0 ? reg2vccp(val) : reg2vcc(val));
283 static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
287 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
288 struct i2c_client *client = to_i2c_client(dev);
289 struct adt7475_data *data = i2c_get_clientdata(client);
293 if (strict_strtol(buf, 10, &val))
296 mutex_lock(&data->lock);
298 data->voltage[sattr->nr][sattr->index] =
299 sattr->index ? vcc2reg(val) : vccp2reg(val);
301 if (sattr->nr == MIN)
302 reg = VOLTAGE_MIN_REG(sattr->index);
304 reg = VOLTAGE_MAX_REG(sattr->index);
306 i2c_smbus_write_byte_data(client, reg,
307 data->voltage[sattr->nr][sattr->index] >> 2);
308 mutex_unlock(&data->lock);
313 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
316 struct adt7475_data *data = adt7475_update_device(dev);
317 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
322 mutex_lock(&data->lock);
323 out = data->temp[sattr->nr][sattr->index];
324 if (sattr->index != 1)
325 out = (out >> 4) & 0xF;
328 /* Show the value as an absolute number tied to
330 out = reg2temp(data, data->temp[THERM][sattr->index]) -
332 mutex_unlock(&data->lock);
336 /* Offset is always 2's complement, regardless of the
337 * setting in CONFIG5 */
338 mutex_lock(&data->lock);
339 out = (s8)data->temp[sattr->nr][sattr->index];
340 if (data->config5 & CONFIG5_TEMPOFFSET)
344 mutex_unlock(&data->lock);
348 out = (data->alarms >> (sattr->index + 4)) & 1;
352 /* Note - only for remote1 and remote2 */
353 out = data->alarms & (sattr->index ? 0x8000 : 0x4000);
358 /* All other temp values are in the configured format */
359 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
362 return sprintf(buf, "%d\n", out);
365 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
369 struct i2c_client *client = to_i2c_client(dev);
370 struct adt7475_data *data = i2c_get_clientdata(client);
371 unsigned char reg = 0;
376 if (strict_strtol(buf, 10, &val))
379 mutex_lock(&data->lock);
381 /* We need the config register in all cases for temp <-> reg conv. */
382 data->config5 = adt7475_read(REG_CONFIG5);
386 if (data->config5 & CONFIG5_TEMPOFFSET) {
387 val = SENSORS_LIMIT(val, -63000, 127000);
388 out = data->temp[OFFSET][sattr->index] = val / 1000;
390 val = SENSORS_LIMIT(val, -63000, 64000);
391 out = data->temp[OFFSET][sattr->index] = val / 500;
396 /* The value will be given as an absolute value, turn it
397 into an offset based on THERM */
399 /* Read fresh THERM and HYSTERSIS values from the chip */
400 data->temp[THERM][sattr->index] =
401 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
402 adt7475_read_hystersis(client);
404 temp = reg2temp(data, data->temp[THERM][sattr->index]);
405 val = SENSORS_LIMIT(val, temp - 15000, temp);
406 val = (temp - val) / 1000;
408 if (sattr->index != 1) {
409 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
410 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
412 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
413 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
416 out = data->temp[HYSTERSIS][sattr->index];
420 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
422 /* We maintain an extra 2 digits of precision for simplicity
423 * - shift those back off before writing the value */
424 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
429 reg = TEMP_MIN_REG(sattr->index);
432 reg = TEMP_MAX_REG(sattr->index);
435 reg = TEMP_OFFSET_REG(sattr->index);
438 reg = TEMP_TMIN_REG(sattr->index);
441 reg = TEMP_THERM_REG(sattr->index);
444 if (sattr->index != 2)
445 reg = REG_REMOTE1_HYSTERSIS;
447 reg = REG_REMOTE2_HYSTERSIS;
452 i2c_smbus_write_byte_data(client, reg, out);
454 mutex_unlock(&data->lock);
458 /* Table of autorange values - the user will write the value in millidegrees,
459 and we'll convert it */
460 static const int autorange_table[] = {
461 2000, 2500, 3330, 4000, 5000, 6670, 8000,
462 10000, 13330, 16000, 20000, 26670, 32000, 40000,
466 static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
469 struct adt7475_data *data = adt7475_update_device(dev);
470 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
473 mutex_lock(&data->lock);
474 out = (data->range[sattr->index] >> 4) & 0x0F;
475 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
476 mutex_unlock(&data->lock);
478 return sprintf(buf, "%d\n", val + autorange_table[out]);
481 static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
482 const char *buf, size_t count)
484 struct i2c_client *client = to_i2c_client(dev);
485 struct adt7475_data *data = i2c_get_clientdata(client);
486 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
490 if (strict_strtol(buf, 10, &val))
493 mutex_lock(&data->lock);
495 /* Get a fresh copy of the needed registers */
496 data->config5 = adt7475_read(REG_CONFIG5);
497 data->temp[AUTOMIN][sattr->index] =
498 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
499 data->range[sattr->index] =
500 adt7475_read(TEMP_TRANGE_REG(sattr->index));
502 /* The user will write an absolute value, so subtract the start point
503 to figure the range */
504 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
505 val = SENSORS_LIMIT(val, temp + autorange_table[0],
506 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
509 /* Find the nearest table entry to what the user wrote */
510 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
512 data->range[sattr->index] &= ~0xF0;
513 data->range[sattr->index] |= val << 4;
515 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
516 data->range[sattr->index]);
518 mutex_unlock(&data->lock);
522 static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
525 struct adt7475_data *data = adt7475_update_device(dev);
526 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
529 if (sattr->nr == ALARM)
530 out = (data->alarms >> (sattr->index + 10)) & 1;
532 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
534 return sprintf(buf, "%d\n", out);
537 static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
538 const char *buf, size_t count)
541 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
542 struct i2c_client *client = to_i2c_client(dev);
543 struct adt7475_data *data = i2c_get_clientdata(client);
546 if (strict_strtoul(buf, 10, &val))
549 mutex_lock(&data->lock);
551 data->tach[MIN][sattr->index] = rpm2tach(val);
553 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
554 data->tach[MIN][sattr->index]);
556 mutex_unlock(&data->lock);
560 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
563 struct adt7475_data *data = adt7475_update_device(dev);
564 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
566 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
569 static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
572 struct adt7475_data *data = adt7475_update_device(dev);
573 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
575 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
578 static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
581 struct adt7475_data *data = adt7475_update_device(dev);
582 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
584 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
587 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
591 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
592 struct i2c_client *client = to_i2c_client(dev);
593 struct adt7475_data *data = i2c_get_clientdata(client);
594 unsigned char reg = 0;
597 if (strict_strtol(buf, 10, &val))
600 mutex_lock(&data->lock);
604 /* Get a fresh value for CONTROL */
605 data->pwm[CONTROL][sattr->index] =
606 adt7475_read(PWM_CONFIG_REG(sattr->index));
608 /* If we are not in manual mode, then we shouldn't allow
609 * the user to set the pwm speed */
610 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
611 mutex_unlock(&data->lock);
615 reg = PWM_REG(sattr->index);
619 reg = PWM_MIN_REG(sattr->index);
623 reg = PWM_MAX_REG(sattr->index);
627 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
628 i2c_smbus_write_byte_data(client, reg,
629 data->pwm[sattr->nr][sattr->index]);
631 mutex_unlock(&data->lock);
636 /* Called by set_pwmctrl and set_pwmchan */
638 static int hw_set_pwm(struct i2c_client *client, int index,
639 unsigned int pwmctl, unsigned int pwmchan)
641 struct adt7475_data *data = i2c_get_clientdata(client);
646 val = 0x03; /* Run at full speed */
649 val = 0x07; /* Manual mode */
654 /* Remote1 controls PWM */
658 /* local controls PWM */
662 /* remote2 controls PWM */
666 /* local/remote2 control PWM */
670 /* All three control PWM */
681 data->pwmctl[index] = pwmctl;
682 data->pwmchan[index] = pwmchan;
684 data->pwm[CONTROL][index] &= ~0xE0;
685 data->pwm[CONTROL][index] |= (val & 7) << 5;
687 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
688 data->pwm[CONTROL][index]);
693 static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
694 const char *buf, size_t count)
696 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
697 struct i2c_client *client = to_i2c_client(dev);
698 struct adt7475_data *data = i2c_get_clientdata(client);
702 if (strict_strtol(buf, 10, &val))
705 mutex_lock(&data->lock);
706 /* Read Modify Write PWM values */
707 adt7475_read_pwm(client, sattr->index);
708 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
711 mutex_unlock(&data->lock);
716 static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
717 const char *buf, size_t count)
719 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
720 struct i2c_client *client = to_i2c_client(dev);
721 struct adt7475_data *data = i2c_get_clientdata(client);
725 if (strict_strtol(buf, 10, &val))
728 mutex_lock(&data->lock);
729 /* Read Modify Write PWM values */
730 adt7475_read_pwm(client, sattr->index);
731 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
734 mutex_unlock(&data->lock);
739 /* List of frequencies for the PWM */
740 static const int pwmfreq_table[] = {
741 11, 14, 22, 29, 35, 44, 58, 88
744 static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
747 struct adt7475_data *data = adt7475_update_device(dev);
748 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
750 return sprintf(buf, "%d\n",
751 pwmfreq_table[data->range[sattr->index] & 7]);
754 static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
755 const char *buf, size_t count)
757 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
758 struct i2c_client *client = to_i2c_client(dev);
759 struct adt7475_data *data = i2c_get_clientdata(client);
763 if (strict_strtol(buf, 10, &val))
766 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
768 mutex_lock(&data->lock);
770 data->range[sattr->index] =
771 adt7475_read(TEMP_TRANGE_REG(sattr->index));
772 data->range[sattr->index] &= ~7;
773 data->range[sattr->index] |= out;
775 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
776 data->range[sattr->index]);
778 mutex_unlock(&data->lock);
782 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
783 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
784 set_voltage, MAX, 0);
785 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
786 set_voltage, MIN, 0);
787 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
788 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
789 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
790 set_voltage, MAX, 1);
791 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
792 set_voltage, MIN, 1);
793 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
794 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
795 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
796 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
797 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
799 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
801 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
802 set_temp, OFFSET, 0);
803 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
804 show_temp, set_temp, AUTOMIN, 0);
805 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
806 show_point2, set_point2, 0, 0);
807 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
809 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
810 set_temp, HYSTERSIS, 0);
811 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
812 static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
813 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
815 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
817 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
818 set_temp, OFFSET, 1);
819 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
820 show_temp, set_temp, AUTOMIN, 1);
821 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
822 show_point2, set_point2, 0, 1);
823 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
825 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
826 set_temp, HYSTERSIS, 1);
827 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
828 static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
829 static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
830 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
832 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
834 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
835 set_temp, OFFSET, 2);
836 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
837 show_temp, set_temp, AUTOMIN, 2);
838 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
839 show_point2, set_point2, 0, 2);
840 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
842 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
843 set_temp, HYSTERSIS, 2);
844 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
845 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
847 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
848 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
849 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
851 static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
852 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
853 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
855 static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
856 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
857 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
859 static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
860 static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
862 static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
863 set_pwmfreq, INPUT, 0);
864 static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
865 set_pwmctrl, INPUT, 0);
866 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channel_temp, S_IRUGO | S_IWUSR,
867 show_pwmchan, set_pwmchan, INPUT, 0);
868 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
870 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
872 static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
874 static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
875 set_pwmfreq, INPUT, 1);
876 static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
877 set_pwmctrl, INPUT, 1);
878 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channel_temp, S_IRUGO | S_IWUSR,
879 show_pwmchan, set_pwmchan, INPUT, 1);
880 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
882 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
884 static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
886 static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
887 set_pwmfreq, INPUT, 2);
888 static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
889 set_pwmctrl, INPUT, 2);
890 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channel_temp, S_IRUGO | S_IWUSR,
891 show_pwmchan, set_pwmchan, INPUT, 2);
892 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
894 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
897 static struct attribute *adt7475_attrs[] = {
898 &sensor_dev_attr_in1_input.dev_attr.attr,
899 &sensor_dev_attr_in1_max.dev_attr.attr,
900 &sensor_dev_attr_in1_min.dev_attr.attr,
901 &sensor_dev_attr_in1_alarm.dev_attr.attr,
902 &sensor_dev_attr_in2_input.dev_attr.attr,
903 &sensor_dev_attr_in2_max.dev_attr.attr,
904 &sensor_dev_attr_in2_min.dev_attr.attr,
905 &sensor_dev_attr_in2_alarm.dev_attr.attr,
906 &sensor_dev_attr_temp1_input.dev_attr.attr,
907 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
908 &sensor_dev_attr_temp1_fault.dev_attr.attr,
909 &sensor_dev_attr_temp1_max.dev_attr.attr,
910 &sensor_dev_attr_temp1_min.dev_attr.attr,
911 &sensor_dev_attr_temp1_offset.dev_attr.attr,
912 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
913 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
914 &sensor_dev_attr_temp1_crit.dev_attr.attr,
915 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
916 &sensor_dev_attr_temp2_input.dev_attr.attr,
917 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
918 &sensor_dev_attr_temp2_max.dev_attr.attr,
919 &sensor_dev_attr_temp2_min.dev_attr.attr,
920 &sensor_dev_attr_temp2_offset.dev_attr.attr,
921 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
922 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
923 &sensor_dev_attr_temp2_crit.dev_attr.attr,
924 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
925 &sensor_dev_attr_temp3_input.dev_attr.attr,
926 &sensor_dev_attr_temp3_fault.dev_attr.attr,
927 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
928 &sensor_dev_attr_temp3_max.dev_attr.attr,
929 &sensor_dev_attr_temp3_min.dev_attr.attr,
930 &sensor_dev_attr_temp3_offset.dev_attr.attr,
931 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
932 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
933 &sensor_dev_attr_temp3_crit.dev_attr.attr,
934 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
935 &sensor_dev_attr_fan1_input.dev_attr.attr,
936 &sensor_dev_attr_fan1_min.dev_attr.attr,
937 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
938 &sensor_dev_attr_fan2_input.dev_attr.attr,
939 &sensor_dev_attr_fan2_min.dev_attr.attr,
940 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
941 &sensor_dev_attr_fan3_input.dev_attr.attr,
942 &sensor_dev_attr_fan3_min.dev_attr.attr,
943 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
944 &sensor_dev_attr_fan4_input.dev_attr.attr,
945 &sensor_dev_attr_fan4_min.dev_attr.attr,
946 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
947 &sensor_dev_attr_pwm1.dev_attr.attr,
948 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
949 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
950 &sensor_dev_attr_pwm1_auto_channel_temp.dev_attr.attr,
951 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
952 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
953 &sensor_dev_attr_pwm2.dev_attr.attr,
954 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
955 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
956 &sensor_dev_attr_pwm2_auto_channel_temp.dev_attr.attr,
957 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
958 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
959 &sensor_dev_attr_pwm3.dev_attr.attr,
960 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
961 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
962 &sensor_dev_attr_pwm3_auto_channel_temp.dev_attr.attr,
963 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
964 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
968 struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
970 static int adt7475_detect(struct i2c_client *client, int kind,
971 struct i2c_board_info *info)
973 struct i2c_adapter *adapter = client->adapter;
975 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
979 if (adt7475_read(REG_VENDID) != 0x41 ||
980 adt7475_read(REG_DEVID) != 0x75) {
981 dev_err(&adapter->dev,
982 "Couldn't detect a adt7475 part at 0x%02x\n",
983 (unsigned int)client->addr);
988 strlcpy(info->type, adt7475_id[0].name, I2C_NAME_SIZE);
993 static int adt7475_probe(struct i2c_client *client,
994 const struct i2c_device_id *id)
996 struct adt7475_data *data;
999 data = kzalloc(sizeof(*data), GFP_KERNEL);
1003 mutex_init(&data->lock);
1004 i2c_set_clientdata(client, data);
1006 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1007 pwm's which are disabled to manual mode with 0% duty cycle */
1008 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1009 adt7475_read_pwm(client, i);
1011 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1015 data->hwmon_dev = hwmon_device_register(&client->dev);
1016 if (IS_ERR(data->hwmon_dev)) {
1017 ret = PTR_ERR(data->hwmon_dev);
1024 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1030 static int adt7475_remove(struct i2c_client *client)
1032 struct adt7475_data *data = i2c_get_clientdata(client);
1034 hwmon_device_unregister(data->hwmon_dev);
1035 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1041 static struct i2c_driver adt7475_driver = {
1042 .class = I2C_CLASS_HWMON,
1046 .probe = adt7475_probe,
1047 .remove = adt7475_remove,
1048 .id_table = adt7475_id,
1049 .detect = adt7475_detect,
1050 .address_data = &addr_data,
1053 static void adt7475_read_hystersis(struct i2c_client *client)
1055 struct adt7475_data *data = i2c_get_clientdata(client);
1057 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1058 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1059 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1062 static void adt7475_read_pwm(struct i2c_client *client, int index)
1064 struct adt7475_data *data = i2c_get_clientdata(client);
1067 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1069 /* Figure out the internal value for pwmctrl and pwmchan
1070 based on the current settings */
1071 v = (data->pwm[CONTROL][index] >> 5) & 7;
1074 data->pwmctl[index] = 0;
1076 data->pwmctl[index] = 1;
1078 /* The fan is disabled - we don't want to
1079 support that, so change to manual mode and
1080 set the duty cycle to 0 instead
1082 data->pwm[INPUT][index] = 0;
1083 data->pwm[CONTROL][index] &= ~0xE0;
1084 data->pwm[CONTROL][index] |= (7 << 5);
1086 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1087 data->pwm[INPUT][index]);
1089 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1090 data->pwm[CONTROL][index]);
1092 data->pwmctl[index] = 1;
1094 data->pwmctl[index] = 2;
1098 data->pwmchan[index] = 1;
1101 data->pwmchan[index] = 2;
1104 data->pwmchan[index] = 4;
1107 data->pwmchan[index] = 6;
1110 data->pwmchan[index] = 7;
1116 static struct adt7475_data *adt7475_update_device(struct device *dev)
1118 struct i2c_client *client = to_i2c_client(dev);
1119 struct adt7475_data *data = i2c_get_clientdata(client);
1123 mutex_lock(&data->lock);
1125 /* Measurement values update every 2 seconds */
1126 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1128 data->alarms = adt7475_read(REG_STATUS2) << 8;
1129 data->alarms |= adt7475_read(REG_STATUS1);
1131 ext = adt7475_read(REG_EXTEND1);
1132 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++)
1133 data->voltage[INPUT][i] =
1134 (adt7475_read(VOLTAGE_REG(i)) << 2) |
1135 ((ext >> ((i + 1) * 2)) & 3);
1137 ext = adt7475_read(REG_EXTEND2);
1138 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1139 data->temp[INPUT][i] =
1140 (adt7475_read(TEMP_REG(i)) << 2) |
1141 ((ext >> ((i + 1) * 2)) & 3);
1143 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1144 data->tach[INPUT][i] =
1145 adt7475_read_word(client, TACH_REG(i));
1147 /* Updated by hw when in auto mode */
1148 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1149 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1151 data->measure_updated = jiffies;
1154 /* Limits and settings, should never change update every 60 seconds */
1155 if (time_after(jiffies, data->limits_updated + HZ * 2) ||
1157 data->config5 = adt7475_read(REG_CONFIG5);
1159 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1160 /* Adjust values so they match the input precision */
1161 data->voltage[MIN][i] =
1162 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1163 data->voltage[MAX][i] =
1164 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1167 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1168 /* Adjust values so they match the input precision */
1169 data->temp[MIN][i] =
1170 adt7475_read(TEMP_MIN_REG(i)) << 2;
1171 data->temp[MAX][i] =
1172 adt7475_read(TEMP_MAX_REG(i)) << 2;
1173 data->temp[AUTOMIN][i] =
1174 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1175 data->temp[THERM][i] =
1176 adt7475_read(TEMP_THERM_REG(i)) << 2;
1177 data->temp[OFFSET][i] =
1178 adt7475_read(TEMP_OFFSET_REG(i));
1180 adt7475_read_hystersis(client);
1182 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1183 data->tach[MIN][i] =
1184 adt7475_read_word(client, TACH_MIN_REG(i));
1186 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1187 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1188 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1189 /* Set the channel and control information */
1190 adt7475_read_pwm(client, i);
1193 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1194 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1195 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1197 data->limits_updated = jiffies;
1201 mutex_unlock(&data->lock);
1206 static int __init sensors_adt7475_init(void)
1208 return i2c_add_driver(&adt7475_driver);
1211 static void __exit sensors_adt7475_exit(void)
1213 i2c_del_driver(&adt7475_driver);
1216 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1217 MODULE_DESCRIPTION("adt7475 driver");
1218 MODULE_LICENSE("GPL");
1220 module_init(sensors_adt7475_init);
1221 module_exit(sensors_adt7475_exit);