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_MAX_ADDR 0x22
43 #define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
44 #define ADT7473_REG_VOLT_MIN_MAX_ADDR 0x49
46 #define ADT7473_REG_TEMP_BASE_ADDR 0x25
47 #define ADT7473_REG_TEMP_MAX_ADDR 0x27
48 #define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
49 #define ADT7473_REG_TEMP_LIMITS_MAX_ADDR 0x53
50 #define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
51 #define ADT7473_REG_TEMP_TMIN_MAX_ADDR 0x69
52 #define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
53 #define ADT7473_REG_TEMP_TMAX_MAX_ADDR 0x6C
55 #define ADT7473_REG_FAN_BASE_ADDR 0x28
56 #define ADT7473_REG_FAN_MAX_ADDR 0x2F
57 #define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
58 #define ADT7473_REG_FAN_MIN_MAX_ADDR 0x5B
60 #define ADT7473_REG_PWM_BASE_ADDR 0x30
61 #define ADT7473_REG_PWM_MAX_ADDR 0x32
62 #define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
63 #define ADT7473_REG_PWM_MIN_MAX_ADDR 0x66
64 #define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
65 #define ADT7473_REG_PWM_MAX_MAX_ADDR 0x3A
66 #define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
67 #define ADT7473_REG_PWM_BHVR_MAX_ADDR 0x5E
68 #define ADT7473_PWM_BHVR_MASK 0xE0
69 #define ADT7473_PWM_BHVR_SHIFT 5
71 #define ADT7473_REG_CFG1 0x40
72 #define ADT7473_CFG1_START 0x01
73 #define ADT7473_CFG1_READY 0x04
74 #define ADT7473_REG_CFG2 0x73
75 #define ADT7473_REG_CFG3 0x78
76 #define ADT7473_REG_CFG4 0x7D
77 #define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
78 #define ADT7473_REG_CFG5 0x7C
79 #define ADT7473_CFG5_TEMP_TWOS 0x01
80 #define ADT7473_CFG5_TEMP_OFFSET 0x02
82 #define ADT7473_REG_DEVICE 0x3D
83 #define ADT7473_VENDOR 0x41
84 #define ADT7473_REG_VENDOR 0x3E
85 #define ADT7473_DEVICE 0x73
86 #define ADT7473_REG_REVISION 0x3F
87 #define ADT7473_REV_68 0x68
88 #define ADT7473_REV_69 0x69
90 #define ADT7473_REG_ALARM1 0x41
91 #define ADT7473_VCCP_ALARM 0x02
92 #define ADT7473_VCC_ALARM 0x04
93 #define ADT7473_R1T_ALARM 0x10
94 #define ADT7473_LT_ALARM 0x20
95 #define ADT7473_R2T_ALARM 0x40
96 #define ADT7473_OOL 0x80
97 #define ADT7473_REG_ALARM2 0x42
98 #define ADT7473_OVT_ALARM 0x02
99 #define ADT7473_FAN1_ALARM 0x04
100 #define ADT7473_FAN2_ALARM 0x08
101 #define ADT7473_FAN3_ALARM 0x10
102 #define ADT7473_FAN4_ALARM 0x20
103 #define ADT7473_R1T_SHORT 0x40
104 #define ADT7473_R2T_SHORT 0x80
105 #define ADT7473_REG_MAX_ADDR 0x80
107 #define ALARM2(x) ((x) << 8)
109 #define ADT7473_VOLT_COUNT 2
110 #define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
111 #define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
112 #define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
115 #define ADT7473_TEMP_COUNT 3
116 #define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
117 #define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
118 #define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
120 #define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
121 #define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
123 #define ADT7473_FAN_COUNT 4
124 #define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
125 #define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
127 #define ADT7473_PWM_COUNT 3
128 #define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
129 #define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
130 #define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
131 #define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
133 /* How often do we reread sensors values? (In jiffies) */
134 #define SENSOR_REFRESH_INTERVAL (2 * HZ)
136 /* How often do we reread sensor limit values? (In jiffies) */
137 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
139 /* datasheet says to divide this number by the fan reading to get fan rpm */
140 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
141 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
142 #define FAN_PERIOD_INVALID 65535
143 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
145 struct adt7473_data {
146 struct i2c_client client;
147 struct device *hwmon_dev;
148 struct attribute_group attrs;
152 unsigned long sensors_last_updated; /* In jiffies */
153 unsigned long limits_last_updated; /* In jiffies */
155 u8 volt[ADT7473_VOLT_COUNT];
156 s8 volt_min[ADT7473_VOLT_COUNT];
157 s8 volt_max[ADT7473_VOLT_COUNT];
159 s8 temp[ADT7473_TEMP_COUNT];
160 s8 temp_min[ADT7473_TEMP_COUNT];
161 s8 temp_max[ADT7473_TEMP_COUNT];
162 s8 temp_tmin[ADT7473_TEMP_COUNT];
163 /* This is called the !THERM limit in the datasheet */
164 s8 temp_tmax[ADT7473_TEMP_COUNT];
166 u16 fan[ADT7473_FAN_COUNT];
167 u16 fan_min[ADT7473_FAN_COUNT];
169 u8 pwm[ADT7473_PWM_COUNT];
170 u8 pwm_max[ADT7473_PWM_COUNT];
171 u8 pwm_min[ADT7473_PWM_COUNT];
172 u8 pwm_behavior[ADT7473_PWM_COUNT];
174 u8 temp_twos_complement;
178 u8 max_duty_at_overheat;
181 static int adt7473_attach_adapter(struct i2c_adapter *adapter);
182 static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind);
183 static int adt7473_detach_client(struct i2c_client *client);
185 static struct i2c_driver adt7473_driver = {
189 .attach_adapter = adt7473_attach_adapter,
190 .detach_client = adt7473_detach_client,
194 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
195 * that the low byte must be read before the high byte.
197 static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
200 foo = i2c_smbus_read_byte_data(client, reg);
201 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
205 static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
208 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
209 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
212 static void adt7473_init_client(struct i2c_client *client)
214 int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);
216 if (!(reg & ADT7473_CFG1_READY)) {
217 dev_err(&client->dev, "Chip not ready.\n");
219 /* start monitoring */
220 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
221 reg | ADT7473_CFG1_START);
225 static struct adt7473_data *adt7473_update_device(struct device *dev)
227 struct i2c_client *client = to_i2c_client(dev);
228 struct adt7473_data *data = i2c_get_clientdata(client);
229 unsigned long local_jiffies = jiffies;
233 mutex_lock(&data->lock);
234 if (time_before(local_jiffies, data->sensors_last_updated +
235 SENSOR_REFRESH_INTERVAL)
236 && data->sensors_valid)
237 goto no_sensor_update;
239 for (i = 0; i < ADT7473_VOLT_COUNT; i++)
240 data->volt[i] = i2c_smbus_read_byte_data(client,
241 ADT7473_REG_VOLT(i));
243 /* Determine temperature encoding */
244 cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
245 data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);
248 * What does this do? it implies a variable temperature sensor
249 * offset, but the datasheet doesn't say anything about this bit
250 * and other parts of the datasheet imply that "offset64" mode
251 * means that you shift temp values by -64 if the above bit was set.
253 data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);
255 for (i = 0; i < ADT7473_TEMP_COUNT; i++)
256 data->temp[i] = i2c_smbus_read_byte_data(client,
257 ADT7473_REG_TEMP(i));
259 for (i = 0; i < ADT7473_FAN_COUNT; i++)
260 data->fan[i] = adt7473_read_word_data(client,
263 for (i = 0; i < ADT7473_PWM_COUNT; i++)
264 data->pwm[i] = i2c_smbus_read_byte_data(client,
267 data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
268 if (data->alarm & ADT7473_OOL)
269 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
270 ADT7473_REG_ALARM2));
272 data->sensors_last_updated = local_jiffies;
273 data->sensors_valid = 1;
276 if (time_before(local_jiffies, data->limits_last_updated +
277 LIMIT_REFRESH_INTERVAL)
278 && data->limits_valid)
281 for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
282 data->volt_min[i] = i2c_smbus_read_byte_data(client,
283 ADT7473_REG_VOLT_MIN(i));
284 data->volt_max[i] = i2c_smbus_read_byte_data(client,
285 ADT7473_REG_VOLT_MAX(i));
288 for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
289 data->temp_min[i] = i2c_smbus_read_byte_data(client,
290 ADT7473_REG_TEMP_MIN(i));
291 data->temp_max[i] = i2c_smbus_read_byte_data(client,
292 ADT7473_REG_TEMP_MAX(i));
293 data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
294 ADT7473_REG_TEMP_TMIN(i));
295 data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
296 ADT7473_REG_TEMP_TMAX(i));
299 for (i = 0; i < ADT7473_FAN_COUNT; i++)
300 data->fan_min[i] = adt7473_read_word_data(client,
301 ADT7473_REG_FAN_MIN(i));
303 for (i = 0; i < ADT7473_PWM_COUNT; i++) {
304 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
305 ADT7473_REG_PWM_MAX(i));
306 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
307 ADT7473_REG_PWM_MIN(i));
308 data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
309 ADT7473_REG_PWM_BHVR(i));
312 i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
313 data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT);
315 data->limits_last_updated = local_jiffies;
316 data->limits_valid = 1;
319 mutex_unlock(&data->lock);
324 * On this chip, voltages are given as a count of steps between a minimum
325 * and maximum voltage, not a direct voltage.
327 static const int volt_convert_table[][2] = {
332 static int decode_volt(int volt_index, u8 raw)
334 int cmax = volt_convert_table[volt_index][0];
335 int cmin = volt_convert_table[volt_index][1];
336 return ((raw * (cmax - cmin)) / 255) + cmin;
339 static u8 encode_volt(int volt_index, int cooked)
341 int cmax = volt_convert_table[volt_index][0];
342 int cmin = volt_convert_table[volt_index][1];
347 else if (cooked < cmin)
350 x = ((cooked - cmin) * 255) / (cmax - cmin);
355 static ssize_t show_volt_min(struct device *dev,
356 struct device_attribute *devattr,
359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
360 struct adt7473_data *data = adt7473_update_device(dev);
361 return sprintf(buf, "%d\n",
362 decode_volt(attr->index, data->volt_min[attr->index]));
365 static ssize_t set_volt_min(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 adt7473_data *data = i2c_get_clientdata(client);
373 int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
375 mutex_lock(&data->lock);
376 data->volt_min[attr->index] = volt;
377 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
379 mutex_unlock(&data->lock);
384 static ssize_t show_volt_max(struct device *dev,
385 struct device_attribute *devattr,
388 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
389 struct adt7473_data *data = adt7473_update_device(dev);
390 return sprintf(buf, "%d\n",
391 decode_volt(attr->index, data->volt_max[attr->index]));
394 static ssize_t set_volt_max(struct device *dev,
395 struct device_attribute *devattr,
399 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
400 struct i2c_client *client = to_i2c_client(dev);
401 struct adt7473_data *data = i2c_get_clientdata(client);
402 int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
404 mutex_lock(&data->lock);
405 data->volt_max[attr->index] = volt;
406 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
408 mutex_unlock(&data->lock);
413 static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
416 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
417 struct adt7473_data *data = adt7473_update_device(dev);
419 return sprintf(buf, "%d\n",
420 decode_volt(attr->index, data->volt[attr->index]));
424 * This chip can report temperature data either as a two's complement
425 * number in the range -128 to 127, or as an unsigned number that must
428 static int decode_temp(u8 twos_complement, u8 raw)
430 return twos_complement ? (s8)raw : raw - 64;
433 static u8 encode_temp(u8 twos_complement, int cooked)
435 return twos_complement ? cooked & 0xFF : cooked + 64;
438 static ssize_t show_temp_min(struct device *dev,
439 struct device_attribute *devattr,
442 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
443 struct adt7473_data *data = adt7473_update_device(dev);
444 return sprintf(buf, "%d\n", 1000 * decode_temp(
445 data->temp_twos_complement,
446 data->temp_min[attr->index]));
449 static ssize_t set_temp_min(struct device *dev,
450 struct device_attribute *devattr,
454 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adt7473_data *data = i2c_get_clientdata(client);
457 int temp = simple_strtol(buf, NULL, 10) / 1000;
458 temp = encode_temp(data->temp_twos_complement, temp);
460 mutex_lock(&data->lock);
461 data->temp_min[attr->index] = temp;
462 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
464 mutex_unlock(&data->lock);
469 static ssize_t show_temp_max(struct device *dev,
470 struct device_attribute *devattr,
473 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
474 struct adt7473_data *data = adt7473_update_device(dev);
475 return sprintf(buf, "%d\n", 1000 * decode_temp(
476 data->temp_twos_complement,
477 data->temp_max[attr->index]));
480 static ssize_t set_temp_max(struct device *dev,
481 struct device_attribute *devattr,
485 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
486 struct i2c_client *client = to_i2c_client(dev);
487 struct adt7473_data *data = i2c_get_clientdata(client);
488 int temp = simple_strtol(buf, NULL, 10) / 1000;
489 temp = encode_temp(data->temp_twos_complement, temp);
491 mutex_lock(&data->lock);
492 data->temp_max[attr->index] = temp;
493 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
495 mutex_unlock(&data->lock);
500 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
503 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
504 struct adt7473_data *data = adt7473_update_device(dev);
505 return sprintf(buf, "%d\n", 1000 * decode_temp(
506 data->temp_twos_complement,
507 data->temp[attr->index]));
510 static ssize_t show_fan_min(struct device *dev,
511 struct device_attribute *devattr,
514 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
515 struct adt7473_data *data = adt7473_update_device(dev);
517 if (FAN_DATA_VALID(data->fan_min[attr->index]))
518 return sprintf(buf, "%d\n",
519 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
521 return sprintf(buf, "0\n");
524 static ssize_t set_fan_min(struct device *dev,
525 struct device_attribute *devattr,
526 const char *buf, size_t count)
528 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
529 struct i2c_client *client = to_i2c_client(dev);
530 struct adt7473_data *data = i2c_get_clientdata(client);
531 int temp = simple_strtol(buf, NULL, 10);
535 temp = FAN_RPM_TO_PERIOD(temp);
537 mutex_lock(&data->lock);
538 data->fan_min[attr->index] = temp;
539 adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
540 mutex_unlock(&data->lock);
545 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
548 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
549 struct adt7473_data *data = adt7473_update_device(dev);
551 if (FAN_DATA_VALID(data->fan[attr->index]))
552 return sprintf(buf, "%d\n",
553 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
555 return sprintf(buf, "0\n");
558 static ssize_t show_max_duty_at_crit(struct device *dev,
559 struct device_attribute *devattr,
562 struct adt7473_data *data = adt7473_update_device(dev);
563 return sprintf(buf, "%d\n", data->max_duty_at_overheat);
566 static ssize_t set_max_duty_at_crit(struct device *dev,
567 struct device_attribute *devattr,
572 struct i2c_client *client = to_i2c_client(dev);
573 struct adt7473_data *data = i2c_get_clientdata(client);
574 int temp = simple_strtol(buf, NULL, 10);
577 mutex_lock(&data->lock);
578 data->max_duty_at_overheat = temp;
579 reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
581 reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
583 reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
584 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
585 mutex_unlock(&data->lock);
590 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
593 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
594 struct adt7473_data *data = adt7473_update_device(dev);
595 return sprintf(buf, "%d\n", data->pwm[attr->index]);
598 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
599 const char *buf, size_t count)
601 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
602 struct i2c_client *client = to_i2c_client(dev);
603 struct adt7473_data *data = i2c_get_clientdata(client);
604 int temp = simple_strtol(buf, NULL, 10);
606 mutex_lock(&data->lock);
607 data->pwm[attr->index] = temp;
608 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
609 mutex_unlock(&data->lock);
614 static ssize_t show_pwm_max(struct device *dev,
615 struct device_attribute *devattr,
618 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
619 struct adt7473_data *data = adt7473_update_device(dev);
620 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
623 static ssize_t set_pwm_max(struct device *dev,
624 struct device_attribute *devattr,
628 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
629 struct i2c_client *client = to_i2c_client(dev);
630 struct adt7473_data *data = i2c_get_clientdata(client);
631 int temp = simple_strtol(buf, NULL, 10);
633 mutex_lock(&data->lock);
634 data->pwm_max[attr->index] = temp;
635 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
637 mutex_unlock(&data->lock);
642 static ssize_t show_pwm_min(struct device *dev,
643 struct device_attribute *devattr,
646 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
647 struct adt7473_data *data = adt7473_update_device(dev);
648 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
651 static ssize_t set_pwm_min(struct device *dev,
652 struct device_attribute *devattr,
656 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
657 struct i2c_client *client = to_i2c_client(dev);
658 struct adt7473_data *data = i2c_get_clientdata(client);
659 int temp = simple_strtol(buf, NULL, 10);
661 mutex_lock(&data->lock);
662 data->pwm_min[attr->index] = temp;
663 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
665 mutex_unlock(&data->lock);
670 static ssize_t show_temp_tmax(struct device *dev,
671 struct device_attribute *devattr,
674 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
675 struct adt7473_data *data = adt7473_update_device(dev);
676 return sprintf(buf, "%d\n", 1000 * decode_temp(
677 data->temp_twos_complement,
678 data->temp_tmax[attr->index]));
681 static ssize_t set_temp_tmax(struct device *dev,
682 struct device_attribute *devattr,
686 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
687 struct i2c_client *client = to_i2c_client(dev);
688 struct adt7473_data *data = i2c_get_clientdata(client);
689 int temp = simple_strtol(buf, NULL, 10) / 1000;
690 temp = encode_temp(data->temp_twos_complement, temp);
692 mutex_lock(&data->lock);
693 data->temp_tmax[attr->index] = temp;
694 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
696 mutex_unlock(&data->lock);
701 static ssize_t show_temp_tmin(struct device *dev,
702 struct device_attribute *devattr,
705 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
706 struct adt7473_data *data = adt7473_update_device(dev);
707 return sprintf(buf, "%d\n", 1000 * decode_temp(
708 data->temp_twos_complement,
709 data->temp_tmin[attr->index]));
712 static ssize_t set_temp_tmin(struct device *dev,
713 struct device_attribute *devattr,
717 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
718 struct i2c_client *client = to_i2c_client(dev);
719 struct adt7473_data *data = i2c_get_clientdata(client);
720 int temp = simple_strtol(buf, NULL, 10) / 1000;
721 temp = encode_temp(data->temp_twos_complement, temp);
723 mutex_lock(&data->lock);
724 data->temp_tmin[attr->index] = temp;
725 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
727 mutex_unlock(&data->lock);
732 static ssize_t show_pwm_enable(struct device *dev,
733 struct device_attribute *devattr,
736 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
737 struct adt7473_data *data = adt7473_update_device(dev);
739 switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
741 return sprintf(buf, "0\n");
743 return sprintf(buf, "1\n");
745 return sprintf(buf, "2\n");
749 static ssize_t set_pwm_enable(struct device *dev,
750 struct device_attribute *devattr,
755 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
756 struct i2c_client *client = to_i2c_client(dev);
757 struct adt7473_data *data = i2c_get_clientdata(client);
758 int temp = simple_strtol(buf, NULL, 10);
768 /* Enter automatic mode with fans off */
775 mutex_lock(&data->lock);
776 reg = i2c_smbus_read_byte_data(client,
777 ADT7473_REG_PWM_BHVR(attr->index));
778 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
779 (reg & ~ADT7473_PWM_BHVR_MASK);
780 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
782 data->pwm_behavior[attr->index] = reg;
783 mutex_unlock(&data->lock);
788 static ssize_t show_pwm_auto_temp(struct device *dev,
789 struct device_attribute *devattr,
792 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
793 struct adt7473_data *data = adt7473_update_device(dev);
794 int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;
800 return sprintf(buf, "0\n");
805 return sprintf(buf, "%d\n", bhvr + 1);
807 return sprintf(buf, "4\n");
809 /* shouldn't ever get here */
813 static ssize_t set_pwm_auto_temp(struct device *dev,
814 struct device_attribute *devattr,
819 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
820 struct i2c_client *client = to_i2c_client(dev);
821 struct adt7473_data *data = i2c_get_clientdata(client);
822 int temp = simple_strtol(buf, NULL, 10);
838 mutex_lock(&data->lock);
839 reg = i2c_smbus_read_byte_data(client,
840 ADT7473_REG_PWM_BHVR(attr->index));
841 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
842 (reg & ~ADT7473_PWM_BHVR_MASK);
843 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
845 data->pwm_behavior[attr->index] = reg;
846 mutex_unlock(&data->lock);
851 static ssize_t show_alarm(struct device *dev,
852 struct device_attribute *devattr,
855 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
856 struct adt7473_data *data = adt7473_update_device(dev);
858 if (data->alarm & attr->index)
859 return sprintf(buf, "1\n");
861 return sprintf(buf, "0\n");
865 static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
867 static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
870 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
872 static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
875 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
876 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);
878 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
880 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
883 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
885 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
887 static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
890 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
892 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
894 static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
897 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
898 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
899 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
901 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
902 ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
903 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
905 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
906 ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));
908 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
910 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
912 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
914 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
917 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
918 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
919 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
920 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
922 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
923 ALARM2(ADT7473_FAN1_ALARM));
924 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
925 ALARM2(ADT7473_FAN2_ALARM));
926 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
927 ALARM2(ADT7473_FAN3_ALARM));
928 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
929 ALARM2(ADT7473_FAN4_ALARM));
931 static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
932 show_max_duty_at_crit, set_max_duty_at_crit, 0);
934 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
935 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
936 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
938 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
939 show_pwm_min, set_pwm_min, 0);
940 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
941 show_pwm_min, set_pwm_min, 1);
942 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
943 show_pwm_min, set_pwm_min, 2);
945 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
946 show_pwm_max, set_pwm_max, 0);
947 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
948 show_pwm_max, set_pwm_max, 1);
949 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
950 show_pwm_max, set_pwm_max, 2);
952 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
953 show_temp_tmin, set_temp_tmin, 0);
954 static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
955 show_temp_tmin, set_temp_tmin, 1);
956 static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
957 show_temp_tmin, set_temp_tmin, 2);
959 static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
960 show_temp_tmax, set_temp_tmax, 0);
961 static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
962 show_temp_tmax, set_temp_tmax, 1);
963 static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
964 show_temp_tmax, set_temp_tmax, 2);
966 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
968 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
970 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
973 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
974 show_pwm_auto_temp, set_pwm_auto_temp, 0);
975 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
976 show_pwm_auto_temp, set_pwm_auto_temp, 1);
977 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
978 show_pwm_auto_temp, set_pwm_auto_temp, 2);
980 static struct attribute *adt7473_attr[] =
982 &sensor_dev_attr_in1_max.dev_attr.attr,
983 &sensor_dev_attr_in2_max.dev_attr.attr,
984 &sensor_dev_attr_in1_min.dev_attr.attr,
985 &sensor_dev_attr_in2_min.dev_attr.attr,
986 &sensor_dev_attr_in1_input.dev_attr.attr,
987 &sensor_dev_attr_in2_input.dev_attr.attr,
988 &sensor_dev_attr_in1_alarm.dev_attr.attr,
989 &sensor_dev_attr_in2_alarm.dev_attr.attr,
991 &sensor_dev_attr_temp1_max.dev_attr.attr,
992 &sensor_dev_attr_temp2_max.dev_attr.attr,
993 &sensor_dev_attr_temp3_max.dev_attr.attr,
994 &sensor_dev_attr_temp1_min.dev_attr.attr,
995 &sensor_dev_attr_temp2_min.dev_attr.attr,
996 &sensor_dev_attr_temp3_min.dev_attr.attr,
997 &sensor_dev_attr_temp1_input.dev_attr.attr,
998 &sensor_dev_attr_temp2_input.dev_attr.attr,
999 &sensor_dev_attr_temp3_input.dev_attr.attr,
1000 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1001 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1002 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1003 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1004 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1005 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1006 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1007 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1008 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1010 &sensor_dev_attr_fan1_min.dev_attr.attr,
1011 &sensor_dev_attr_fan2_min.dev_attr.attr,
1012 &sensor_dev_attr_fan3_min.dev_attr.attr,
1013 &sensor_dev_attr_fan4_min.dev_attr.attr,
1014 &sensor_dev_attr_fan1_input.dev_attr.attr,
1015 &sensor_dev_attr_fan2_input.dev_attr.attr,
1016 &sensor_dev_attr_fan3_input.dev_attr.attr,
1017 &sensor_dev_attr_fan4_input.dev_attr.attr,
1018 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1019 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1020 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1021 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1023 &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,
1025 &sensor_dev_attr_pwm1.dev_attr.attr,
1026 &sensor_dev_attr_pwm2.dev_attr.attr,
1027 &sensor_dev_attr_pwm3.dev_attr.attr,
1028 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1029 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1030 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1031 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1032 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1033 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1035 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1036 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1037 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1038 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1039 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1040 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1045 static int adt7473_attach_adapter(struct i2c_adapter *adapter)
1047 if (!(adapter->class & I2C_CLASS_HWMON))
1049 return i2c_probe(adapter, &addr_data, adt7473_detect);
1052 static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
1054 struct i2c_client *client;
1055 struct adt7473_data *data;
1058 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1061 data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
1067 client = &data->client;
1068 client->addr = address;
1069 client->adapter = adapter;
1070 client->driver = &adt7473_driver;
1072 i2c_set_clientdata(client, data);
1074 mutex_init(&data->lock);
1077 int vendor, device, revision;
1079 vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
1080 if (vendor != ADT7473_VENDOR) {
1085 device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
1086 if (device != ADT7473_DEVICE) {
1091 revision = i2c_smbus_read_byte_data(client,
1092 ADT7473_REG_REVISION);
1093 if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69) {
1098 dev_dbg(&adapter->dev, "detection forced\n");
1100 strlcpy(client->name, "adt7473", I2C_NAME_SIZE);
1102 err = i2c_attach_client(client);
1106 dev_info(&client->dev, "%s chip found\n", client->name);
1108 /* Initialize the ADT7473 chip */
1109 adt7473_init_client(client);
1111 /* Register sysfs hooks */
1112 data->attrs.attrs = adt7473_attr;
1113 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1117 data->hwmon_dev = hwmon_device_register(&client->dev);
1118 if (IS_ERR(data->hwmon_dev)) {
1119 err = PTR_ERR(data->hwmon_dev);
1126 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1128 i2c_detach_client(client);
1135 static int adt7473_detach_client(struct i2c_client *client)
1137 struct adt7473_data *data = i2c_get_clientdata(client);
1139 hwmon_device_unregister(data->hwmon_dev);
1140 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1141 i2c_detach_client(client);
1146 static int __init adt7473_init(void)
1148 return i2c_add_driver(&adt7473_driver);
1151 static void __exit adt7473_exit(void)
1153 i2c_del_driver(&adt7473_driver);
1156 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1157 MODULE_DESCRIPTION("ADT7473 driver");
1158 MODULE_LICENSE("GPL");
1160 module_init(adt7473_init);
1161 module_exit(adt7473_exit);