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 /* ADT7473 registers */
36 #define ADT7473_REG_BASE_ADDR 0x20
38 #define ADT7473_REG_VOLT_BASE_ADDR 0x21
39 #define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
41 #define ADT7473_REG_TEMP_BASE_ADDR 0x25
42 #define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
43 #define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
44 #define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
46 #define ADT7473_REG_FAN_BASE_ADDR 0x28
47 #define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
49 #define ADT7473_REG_PWM_BASE_ADDR 0x30
50 #define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
51 #define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
52 #define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
53 #define ADT7473_PWM_BHVR_MASK 0xE0
54 #define ADT7473_PWM_BHVR_SHIFT 5
56 #define ADT7473_REG_CFG1 0x40
57 #define ADT7473_CFG1_START 0x01
58 #define ADT7473_CFG1_READY 0x04
59 #define ADT7473_REG_CFG2 0x73
60 #define ADT7473_REG_CFG3 0x78
61 #define ADT7473_REG_CFG4 0x7D
62 #define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
63 #define ADT7473_REG_CFG5 0x7C
64 #define ADT7473_CFG5_TEMP_TWOS 0x01
65 #define ADT7473_CFG5_TEMP_OFFSET 0x02
67 #define ADT7473_REG_DEVICE 0x3D
68 #define ADT7473_VENDOR 0x41
69 #define ADT7473_REG_VENDOR 0x3E
70 #define ADT7473_DEVICE 0x73
71 #define ADT7473_REG_REVISION 0x3F
72 #define ADT7473_REV_68 0x68
73 #define ADT7473_REV_69 0x69
75 #define ADT7473_REG_ALARM1 0x41
76 #define ADT7473_VCCP_ALARM 0x02
77 #define ADT7473_VCC_ALARM 0x04
78 #define ADT7473_R1T_ALARM 0x10
79 #define ADT7473_LT_ALARM 0x20
80 #define ADT7473_R2T_ALARM 0x40
81 #define ADT7473_OOL 0x80
82 #define ADT7473_REG_ALARM2 0x42
83 #define ADT7473_OVT_ALARM 0x02
84 #define ADT7473_FAN1_ALARM 0x04
85 #define ADT7473_FAN2_ALARM 0x08
86 #define ADT7473_FAN3_ALARM 0x10
87 #define ADT7473_FAN4_ALARM 0x20
88 #define ADT7473_R1T_SHORT 0x40
89 #define ADT7473_R2T_SHORT 0x80
91 #define ALARM2(x) ((x) << 8)
93 #define ADT7473_VOLT_COUNT 2
94 #define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
95 #define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
96 #define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
99 #define ADT7473_TEMP_COUNT 3
100 #define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
101 #define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
102 #define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
104 #define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
105 #define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
107 #define ADT7473_FAN_COUNT 4
108 #define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
109 #define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
111 #define ADT7473_PWM_COUNT 3
112 #define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
113 #define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
114 #define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
115 #define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
117 /* How often do we reread sensors values? (In jiffies) */
118 #define SENSOR_REFRESH_INTERVAL (2 * HZ)
120 /* How often do we reread sensor limit values? (In jiffies) */
121 #define LIMIT_REFRESH_INTERVAL (60 * HZ)
123 /* datasheet says to divide this number by the fan reading to get fan rpm */
124 #define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
125 #define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
126 #define FAN_PERIOD_INVALID 65535
127 #define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
129 struct adt7473_data
{
130 struct device
*hwmon_dev
;
131 struct attribute_group attrs
;
135 unsigned long sensors_last_updated
; /* In jiffies */
136 unsigned long limits_last_updated
; /* In jiffies */
138 u8 volt
[ADT7473_VOLT_COUNT
];
139 s8 volt_min
[ADT7473_VOLT_COUNT
];
140 s8 volt_max
[ADT7473_VOLT_COUNT
];
142 s8 temp
[ADT7473_TEMP_COUNT
];
143 s8 temp_min
[ADT7473_TEMP_COUNT
];
144 s8 temp_max
[ADT7473_TEMP_COUNT
];
145 s8 temp_tmin
[ADT7473_TEMP_COUNT
];
146 /* This is called the !THERM limit in the datasheet */
147 s8 temp_tmax
[ADT7473_TEMP_COUNT
];
149 u16 fan
[ADT7473_FAN_COUNT
];
150 u16 fan_min
[ADT7473_FAN_COUNT
];
152 u8 pwm
[ADT7473_PWM_COUNT
];
153 u8 pwm_max
[ADT7473_PWM_COUNT
];
154 u8 pwm_min
[ADT7473_PWM_COUNT
];
155 u8 pwm_behavior
[ADT7473_PWM_COUNT
];
157 u8 temp_twos_complement
;
161 u8 max_duty_at_overheat
;
164 static int adt7473_probe(struct i2c_client
*client
,
165 const struct i2c_device_id
*id
);
166 static int adt7473_detect(struct i2c_client
*client
,
167 struct i2c_board_info
*info
);
168 static int adt7473_remove(struct i2c_client
*client
);
170 static const struct i2c_device_id adt7473_id
[] = {
175 static struct i2c_driver adt7473_driver
= {
176 .class = I2C_CLASS_HWMON
,
180 .probe
= adt7473_probe
,
181 .remove
= adt7473_remove
,
182 .id_table
= adt7473_id
,
183 .detect
= adt7473_detect
,
184 .address_list
= normal_i2c
,
188 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
189 * that the low byte must be read before the high byte.
191 static inline int adt7473_read_word_data(struct i2c_client
*client
, u8 reg
)
194 foo
= i2c_smbus_read_byte_data(client
, reg
);
195 foo
|= ((u16
)i2c_smbus_read_byte_data(client
, reg
+ 1) << 8);
199 static inline int adt7473_write_word_data(struct i2c_client
*client
, u8 reg
,
202 return i2c_smbus_write_byte_data(client
, reg
, value
& 0xFF)
203 && i2c_smbus_write_byte_data(client
, reg
+ 1, value
>> 8);
206 static void adt7473_init_client(struct i2c_client
*client
)
208 int reg
= i2c_smbus_read_byte_data(client
, ADT7473_REG_CFG1
);
210 if (!(reg
& ADT7473_CFG1_READY
)) {
211 dev_err(&client
->dev
, "Chip not ready.\n");
213 /* start monitoring */
214 i2c_smbus_write_byte_data(client
, ADT7473_REG_CFG1
,
215 reg
| ADT7473_CFG1_START
);
219 static struct adt7473_data
*adt7473_update_device(struct device
*dev
)
221 struct i2c_client
*client
= to_i2c_client(dev
);
222 struct adt7473_data
*data
= i2c_get_clientdata(client
);
223 unsigned long local_jiffies
= jiffies
;
227 mutex_lock(&data
->lock
);
228 if (time_before(local_jiffies
, data
->sensors_last_updated
+
229 SENSOR_REFRESH_INTERVAL
)
230 && data
->sensors_valid
)
231 goto no_sensor_update
;
233 for (i
= 0; i
< ADT7473_VOLT_COUNT
; i
++)
234 data
->volt
[i
] = i2c_smbus_read_byte_data(client
,
235 ADT7473_REG_VOLT(i
));
237 /* Determine temperature encoding */
238 cfg
= i2c_smbus_read_byte_data(client
, ADT7473_REG_CFG5
);
239 data
->temp_twos_complement
= (cfg
& ADT7473_CFG5_TEMP_TWOS
);
242 * What does this do? it implies a variable temperature sensor
243 * offset, but the datasheet doesn't say anything about this bit
244 * and other parts of the datasheet imply that "offset64" mode
245 * means that you shift temp values by -64 if the above bit was set.
247 data
->temp_offset
= (cfg
& ADT7473_CFG5_TEMP_OFFSET
);
249 for (i
= 0; i
< ADT7473_TEMP_COUNT
; i
++)
250 data
->temp
[i
] = i2c_smbus_read_byte_data(client
,
251 ADT7473_REG_TEMP(i
));
253 for (i
= 0; i
< ADT7473_FAN_COUNT
; i
++)
254 data
->fan
[i
] = adt7473_read_word_data(client
,
257 for (i
= 0; i
< ADT7473_PWM_COUNT
; i
++)
258 data
->pwm
[i
] = i2c_smbus_read_byte_data(client
,
261 data
->alarm
= i2c_smbus_read_byte_data(client
, ADT7473_REG_ALARM1
);
262 if (data
->alarm
& ADT7473_OOL
)
263 data
->alarm
|= ALARM2(i2c_smbus_read_byte_data(client
,
264 ADT7473_REG_ALARM2
));
266 data
->sensors_last_updated
= local_jiffies
;
267 data
->sensors_valid
= 1;
270 if (time_before(local_jiffies
, data
->limits_last_updated
+
271 LIMIT_REFRESH_INTERVAL
)
272 && data
->limits_valid
)
275 for (i
= 0; i
< ADT7473_VOLT_COUNT
; i
++) {
276 data
->volt_min
[i
] = i2c_smbus_read_byte_data(client
,
277 ADT7473_REG_VOLT_MIN(i
));
278 data
->volt_max
[i
] = i2c_smbus_read_byte_data(client
,
279 ADT7473_REG_VOLT_MAX(i
));
282 for (i
= 0; i
< ADT7473_TEMP_COUNT
; i
++) {
283 data
->temp_min
[i
] = i2c_smbus_read_byte_data(client
,
284 ADT7473_REG_TEMP_MIN(i
));
285 data
->temp_max
[i
] = i2c_smbus_read_byte_data(client
,
286 ADT7473_REG_TEMP_MAX(i
));
287 data
->temp_tmin
[i
] = i2c_smbus_read_byte_data(client
,
288 ADT7473_REG_TEMP_TMIN(i
));
289 data
->temp_tmax
[i
] = i2c_smbus_read_byte_data(client
,
290 ADT7473_REG_TEMP_TMAX(i
));
293 for (i
= 0; i
< ADT7473_FAN_COUNT
; i
++)
294 data
->fan_min
[i
] = adt7473_read_word_data(client
,
295 ADT7473_REG_FAN_MIN(i
));
297 for (i
= 0; i
< ADT7473_PWM_COUNT
; i
++) {
298 data
->pwm_max
[i
] = i2c_smbus_read_byte_data(client
,
299 ADT7473_REG_PWM_MAX(i
));
300 data
->pwm_min
[i
] = i2c_smbus_read_byte_data(client
,
301 ADT7473_REG_PWM_MIN(i
));
302 data
->pwm_behavior
[i
] = i2c_smbus_read_byte_data(client
,
303 ADT7473_REG_PWM_BHVR(i
));
306 i
= i2c_smbus_read_byte_data(client
, ADT7473_REG_CFG4
);
307 data
->max_duty_at_overheat
= !!(i
& ADT7473_CFG4_MAX_DUTY_AT_OVT
);
309 data
->limits_last_updated
= local_jiffies
;
310 data
->limits_valid
= 1;
313 mutex_unlock(&data
->lock
);
321 /* IN are scaled acording to built-in resistors */
322 static const int adt7473_scaling
[] = { /* .001 Volts */
325 #define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
327 static int decode_volt(int volt_index
, u8 raw
)
329 return SCALE(raw
, 192, adt7473_scaling
[volt_index
]);
332 static u8
encode_volt(int volt_index
, int cooked
)
334 int raw
= SCALE(cooked
, adt7473_scaling
[volt_index
], 192);
335 return SENSORS_LIMIT(raw
, 0, 255);
338 static ssize_t
show_volt_min(struct device
*dev
,
339 struct device_attribute
*devattr
,
342 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
343 struct adt7473_data
*data
= adt7473_update_device(dev
);
344 return sprintf(buf
, "%d\n",
345 decode_volt(attr
->index
, data
->volt_min
[attr
->index
]));
348 static ssize_t
set_volt_min(struct device
*dev
,
349 struct device_attribute
*devattr
,
353 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
354 struct i2c_client
*client
= to_i2c_client(dev
);
355 struct adt7473_data
*data
= i2c_get_clientdata(client
);
358 if (strict_strtol(buf
, 10, &volt
))
361 volt
= encode_volt(attr
->index
, volt
);
363 mutex_lock(&data
->lock
);
364 data
->volt_min
[attr
->index
] = volt
;
365 i2c_smbus_write_byte_data(client
, ADT7473_REG_VOLT_MIN(attr
->index
),
367 mutex_unlock(&data
->lock
);
372 static ssize_t
show_volt_max(struct device
*dev
,
373 struct device_attribute
*devattr
,
376 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
377 struct adt7473_data
*data
= adt7473_update_device(dev
);
378 return sprintf(buf
, "%d\n",
379 decode_volt(attr
->index
, data
->volt_max
[attr
->index
]));
382 static ssize_t
set_volt_max(struct device
*dev
,
383 struct device_attribute
*devattr
,
387 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
388 struct i2c_client
*client
= to_i2c_client(dev
);
389 struct adt7473_data
*data
= i2c_get_clientdata(client
);
392 if (strict_strtol(buf
, 10, &volt
))
395 volt
= encode_volt(attr
->index
, volt
);
397 mutex_lock(&data
->lock
);
398 data
->volt_max
[attr
->index
] = volt
;
399 i2c_smbus_write_byte_data(client
, ADT7473_REG_VOLT_MAX(attr
->index
),
401 mutex_unlock(&data
->lock
);
406 static ssize_t
show_volt(struct device
*dev
, struct device_attribute
*devattr
,
409 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
410 struct adt7473_data
*data
= adt7473_update_device(dev
);
412 return sprintf(buf
, "%d\n",
413 decode_volt(attr
->index
, data
->volt
[attr
->index
]));
417 * This chip can report temperature data either as a two's complement
418 * number in the range -128 to 127, or as an unsigned number that must
421 static int decode_temp(u8 twos_complement
, u8 raw
)
423 return twos_complement
? (s8
)raw
: raw
- 64;
426 static u8
encode_temp(u8 twos_complement
, int cooked
)
428 u8 ret
= twos_complement
? cooked
& 0xFF : cooked
+ 64;
429 return SENSORS_LIMIT(ret
, 0, 255);
432 static ssize_t
show_temp_min(struct device
*dev
,
433 struct device_attribute
*devattr
,
436 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
437 struct adt7473_data
*data
= adt7473_update_device(dev
);
438 return sprintf(buf
, "%d\n", 1000 * decode_temp(
439 data
->temp_twos_complement
,
440 data
->temp_min
[attr
->index
]));
443 static ssize_t
set_temp_min(struct device
*dev
,
444 struct device_attribute
*devattr
,
448 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
449 struct i2c_client
*client
= to_i2c_client(dev
);
450 struct adt7473_data
*data
= i2c_get_clientdata(client
);
453 if (strict_strtol(buf
, 10, &temp
))
456 temp
= DIV_ROUND_CLOSEST(temp
, 1000);
457 temp
= encode_temp(data
->temp_twos_complement
, temp
);
459 mutex_lock(&data
->lock
);
460 data
->temp_min
[attr
->index
] = temp
;
461 i2c_smbus_write_byte_data(client
, ADT7473_REG_TEMP_MIN(attr
->index
),
463 mutex_unlock(&data
->lock
);
468 static ssize_t
show_temp_max(struct device
*dev
,
469 struct device_attribute
*devattr
,
472 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
473 struct adt7473_data
*data
= adt7473_update_device(dev
);
474 return sprintf(buf
, "%d\n", 1000 * decode_temp(
475 data
->temp_twos_complement
,
476 data
->temp_max
[attr
->index
]));
479 static ssize_t
set_temp_max(struct device
*dev
,
480 struct device_attribute
*devattr
,
484 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
485 struct i2c_client
*client
= to_i2c_client(dev
);
486 struct adt7473_data
*data
= i2c_get_clientdata(client
);
489 if (strict_strtol(buf
, 10, &temp
))
492 temp
= DIV_ROUND_CLOSEST(temp
, 1000);
493 temp
= encode_temp(data
->temp_twos_complement
, temp
);
495 mutex_lock(&data
->lock
);
496 data
->temp_max
[attr
->index
] = temp
;
497 i2c_smbus_write_byte_data(client
, ADT7473_REG_TEMP_MAX(attr
->index
),
499 mutex_unlock(&data
->lock
);
504 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*devattr
,
507 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
508 struct adt7473_data
*data
= adt7473_update_device(dev
);
509 return sprintf(buf
, "%d\n", 1000 * decode_temp(
510 data
->temp_twos_complement
,
511 data
->temp
[attr
->index
]));
514 static ssize_t
show_fan_min(struct device
*dev
,
515 struct device_attribute
*devattr
,
518 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
519 struct adt7473_data
*data
= adt7473_update_device(dev
);
521 if (FAN_DATA_VALID(data
->fan_min
[attr
->index
]))
522 return sprintf(buf
, "%d\n",
523 FAN_PERIOD_TO_RPM(data
->fan_min
[attr
->index
]));
525 return sprintf(buf
, "0\n");
528 static ssize_t
set_fan_min(struct device
*dev
,
529 struct device_attribute
*devattr
,
530 const char *buf
, size_t count
)
532 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
533 struct i2c_client
*client
= to_i2c_client(dev
);
534 struct adt7473_data
*data
= i2c_get_clientdata(client
);
537 if (strict_strtol(buf
, 10, &temp
) || !temp
)
540 temp
= FAN_RPM_TO_PERIOD(temp
);
541 temp
= SENSORS_LIMIT(temp
, 1, 65534);
543 mutex_lock(&data
->lock
);
544 data
->fan_min
[attr
->index
] = temp
;
545 adt7473_write_word_data(client
, ADT7473_REG_FAN_MIN(attr
->index
), temp
);
546 mutex_unlock(&data
->lock
);
551 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*devattr
,
554 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
555 struct adt7473_data
*data
= adt7473_update_device(dev
);
557 if (FAN_DATA_VALID(data
->fan
[attr
->index
]))
558 return sprintf(buf
, "%d\n",
559 FAN_PERIOD_TO_RPM(data
->fan
[attr
->index
]));
561 return sprintf(buf
, "0\n");
564 static ssize_t
show_max_duty_at_crit(struct device
*dev
,
565 struct device_attribute
*devattr
,
568 struct adt7473_data
*data
= adt7473_update_device(dev
);
569 return sprintf(buf
, "%d\n", data
->max_duty_at_overheat
);
572 static ssize_t
set_max_duty_at_crit(struct device
*dev
,
573 struct device_attribute
*devattr
,
578 struct i2c_client
*client
= to_i2c_client(dev
);
579 struct adt7473_data
*data
= i2c_get_clientdata(client
);
582 if (strict_strtol(buf
, 10, &temp
))
585 mutex_lock(&data
->lock
);
586 data
->max_duty_at_overheat
= !!temp
;
587 reg
= i2c_smbus_read_byte_data(client
, ADT7473_REG_CFG4
);
589 reg
|= ADT7473_CFG4_MAX_DUTY_AT_OVT
;
591 reg
&= ~ADT7473_CFG4_MAX_DUTY_AT_OVT
;
592 i2c_smbus_write_byte_data(client
, ADT7473_REG_CFG4
, reg
);
593 mutex_unlock(&data
->lock
);
598 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*devattr
,
601 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
602 struct adt7473_data
*data
= adt7473_update_device(dev
);
603 return sprintf(buf
, "%d\n", data
->pwm
[attr
->index
]);
606 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*devattr
,
607 const char *buf
, size_t count
)
609 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
610 struct i2c_client
*client
= to_i2c_client(dev
);
611 struct adt7473_data
*data
= i2c_get_clientdata(client
);
614 if (strict_strtol(buf
, 10, &temp
))
617 temp
= SENSORS_LIMIT(temp
, 0, 255);
619 mutex_lock(&data
->lock
);
620 data
->pwm
[attr
->index
] = temp
;
621 i2c_smbus_write_byte_data(client
, ADT7473_REG_PWM(attr
->index
), temp
);
622 mutex_unlock(&data
->lock
);
627 static ssize_t
show_pwm_max(struct device
*dev
,
628 struct device_attribute
*devattr
,
631 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
632 struct adt7473_data
*data
= adt7473_update_device(dev
);
633 return sprintf(buf
, "%d\n", data
->pwm_max
[attr
->index
]);
636 static ssize_t
set_pwm_max(struct device
*dev
,
637 struct device_attribute
*devattr
,
641 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
642 struct i2c_client
*client
= to_i2c_client(dev
);
643 struct adt7473_data
*data
= i2c_get_clientdata(client
);
646 if (strict_strtol(buf
, 10, &temp
))
649 temp
= SENSORS_LIMIT(temp
, 0, 255);
651 mutex_lock(&data
->lock
);
652 data
->pwm_max
[attr
->index
] = temp
;
653 i2c_smbus_write_byte_data(client
, ADT7473_REG_PWM_MAX(attr
->index
),
655 mutex_unlock(&data
->lock
);
660 static ssize_t
show_pwm_min(struct device
*dev
,
661 struct device_attribute
*devattr
,
664 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
665 struct adt7473_data
*data
= adt7473_update_device(dev
);
666 return sprintf(buf
, "%d\n", data
->pwm_min
[attr
->index
]);
669 static ssize_t
set_pwm_min(struct device
*dev
,
670 struct device_attribute
*devattr
,
674 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
675 struct i2c_client
*client
= to_i2c_client(dev
);
676 struct adt7473_data
*data
= i2c_get_clientdata(client
);
679 if (strict_strtol(buf
, 10, &temp
))
682 temp
= SENSORS_LIMIT(temp
, 0, 255);
684 mutex_lock(&data
->lock
);
685 data
->pwm_min
[attr
->index
] = temp
;
686 i2c_smbus_write_byte_data(client
, ADT7473_REG_PWM_MIN(attr
->index
),
688 mutex_unlock(&data
->lock
);
693 static ssize_t
show_temp_tmax(struct device
*dev
,
694 struct device_attribute
*devattr
,
697 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
698 struct adt7473_data
*data
= adt7473_update_device(dev
);
699 return sprintf(buf
, "%d\n", 1000 * decode_temp(
700 data
->temp_twos_complement
,
701 data
->temp_tmax
[attr
->index
]));
704 static ssize_t
set_temp_tmax(struct device
*dev
,
705 struct device_attribute
*devattr
,
709 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
710 struct i2c_client
*client
= to_i2c_client(dev
);
711 struct adt7473_data
*data
= i2c_get_clientdata(client
);
714 if (strict_strtol(buf
, 10, &temp
))
717 temp
= DIV_ROUND_CLOSEST(temp
, 1000);
718 temp
= encode_temp(data
->temp_twos_complement
, temp
);
720 mutex_lock(&data
->lock
);
721 data
->temp_tmax
[attr
->index
] = temp
;
722 i2c_smbus_write_byte_data(client
, ADT7473_REG_TEMP_TMAX(attr
->index
),
724 mutex_unlock(&data
->lock
);
729 static ssize_t
show_temp_tmin(struct device
*dev
,
730 struct device_attribute
*devattr
,
733 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
734 struct adt7473_data
*data
= adt7473_update_device(dev
);
735 return sprintf(buf
, "%d\n", 1000 * decode_temp(
736 data
->temp_twos_complement
,
737 data
->temp_tmin
[attr
->index
]));
740 static ssize_t
set_temp_tmin(struct device
*dev
,
741 struct device_attribute
*devattr
,
745 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
746 struct i2c_client
*client
= to_i2c_client(dev
);
747 struct adt7473_data
*data
= i2c_get_clientdata(client
);
750 if (strict_strtol(buf
, 10, &temp
))
753 temp
= DIV_ROUND_CLOSEST(temp
, 1000);
754 temp
= encode_temp(data
->temp_twos_complement
, temp
);
756 mutex_lock(&data
->lock
);
757 data
->temp_tmin
[attr
->index
] = temp
;
758 i2c_smbus_write_byte_data(client
, ADT7473_REG_TEMP_TMIN(attr
->index
),
760 mutex_unlock(&data
->lock
);
765 static ssize_t
show_pwm_enable(struct device
*dev
,
766 struct device_attribute
*devattr
,
769 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
770 struct adt7473_data
*data
= adt7473_update_device(dev
);
772 switch (data
->pwm_behavior
[attr
->index
] >> ADT7473_PWM_BHVR_SHIFT
) {
774 return sprintf(buf
, "0\n");
776 return sprintf(buf
, "1\n");
778 return sprintf(buf
, "2\n");
782 static ssize_t
set_pwm_enable(struct device
*dev
,
783 struct device_attribute
*devattr
,
788 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
789 struct i2c_client
*client
= to_i2c_client(dev
);
790 struct adt7473_data
*data
= i2c_get_clientdata(client
);
793 if (strict_strtol(buf
, 10, &temp
))
804 /* Enter automatic mode with fans off */
811 mutex_lock(&data
->lock
);
812 reg
= i2c_smbus_read_byte_data(client
,
813 ADT7473_REG_PWM_BHVR(attr
->index
));
814 reg
= (temp
<< ADT7473_PWM_BHVR_SHIFT
) |
815 (reg
& ~ADT7473_PWM_BHVR_MASK
);
816 i2c_smbus_write_byte_data(client
, ADT7473_REG_PWM_BHVR(attr
->index
),
818 data
->pwm_behavior
[attr
->index
] = reg
;
819 mutex_unlock(&data
->lock
);
824 static ssize_t
show_pwm_auto_temp(struct device
*dev
,
825 struct device_attribute
*devattr
,
828 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
829 struct adt7473_data
*data
= adt7473_update_device(dev
);
830 int bhvr
= data
->pwm_behavior
[attr
->index
] >> ADT7473_PWM_BHVR_SHIFT
;
836 return sprintf(buf
, "0\n");
841 return sprintf(buf
, "%d\n", bhvr
+ 1);
843 return sprintf(buf
, "4\n");
845 /* shouldn't ever get here */
849 static ssize_t
set_pwm_auto_temp(struct device
*dev
,
850 struct device_attribute
*devattr
,
855 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
856 struct i2c_client
*client
= to_i2c_client(dev
);
857 struct adt7473_data
*data
= i2c_get_clientdata(client
);
860 if (strict_strtol(buf
, 10, &temp
))
877 mutex_lock(&data
->lock
);
878 reg
= i2c_smbus_read_byte_data(client
,
879 ADT7473_REG_PWM_BHVR(attr
->index
));
880 reg
= (temp
<< ADT7473_PWM_BHVR_SHIFT
) |
881 (reg
& ~ADT7473_PWM_BHVR_MASK
);
882 i2c_smbus_write_byte_data(client
, ADT7473_REG_PWM_BHVR(attr
->index
),
884 data
->pwm_behavior
[attr
->index
] = reg
;
885 mutex_unlock(&data
->lock
);
890 static ssize_t
show_alarm(struct device
*dev
,
891 struct device_attribute
*devattr
,
894 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
895 struct adt7473_data
*data
= adt7473_update_device(dev
);
897 if (data
->alarm
& attr
->index
)
898 return sprintf(buf
, "1\n");
900 return sprintf(buf
, "0\n");
904 static SENSOR_DEVICE_ATTR(in1_max
, S_IWUSR
| S_IRUGO
, show_volt_max
,
906 static SENSOR_DEVICE_ATTR(in2_max
, S_IWUSR
| S_IRUGO
, show_volt_max
,
909 static SENSOR_DEVICE_ATTR(in1_min
, S_IWUSR
| S_IRUGO
, show_volt_min
,
911 static SENSOR_DEVICE_ATTR(in2_min
, S_IWUSR
| S_IRUGO
, show_volt_min
,
914 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, show_volt
, NULL
, 0);
915 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, show_volt
, NULL
, 1);
917 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
,
919 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
,
922 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
924 static SENSOR_DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
926 static SENSOR_DEVICE_ATTR(temp3_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
929 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
931 static SENSOR_DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
933 static SENSOR_DEVICE_ATTR(temp3_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
936 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0);
937 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1);
938 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, show_temp
, NULL
, 2);
940 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
,
941 ADT7473_R1T_ALARM
| ALARM2(ADT7473_R1T_SHORT
));
942 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
,
944 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
,
945 ADT7473_R2T_ALARM
| ALARM2(ADT7473_R2T_SHORT
));
947 static SENSOR_DEVICE_ATTR(fan1_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
949 static SENSOR_DEVICE_ATTR(fan2_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
951 static SENSOR_DEVICE_ATTR(fan3_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
953 static SENSOR_DEVICE_ATTR(fan4_min
, S_IWUSR
| S_IRUGO
, show_fan_min
,
956 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0);
957 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1);
958 static SENSOR_DEVICE_ATTR(fan3_input
, S_IRUGO
, show_fan
, NULL
, 2);
959 static SENSOR_DEVICE_ATTR(fan4_input
, S_IRUGO
, show_fan
, NULL
, 3);
961 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
,
962 ALARM2(ADT7473_FAN1_ALARM
));
963 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
,
964 ALARM2(ADT7473_FAN2_ALARM
));
965 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
,
966 ALARM2(ADT7473_FAN3_ALARM
));
967 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
,
968 ALARM2(ADT7473_FAN4_ALARM
));
970 static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit
, S_IWUSR
| S_IRUGO
,
971 show_max_duty_at_crit
, set_max_duty_at_crit
, 0);
973 static SENSOR_DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 0);
974 static SENSOR_DEVICE_ATTR(pwm2
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 1);
975 static SENSOR_DEVICE_ATTR(pwm3
, S_IWUSR
| S_IRUGO
, show_pwm
, set_pwm
, 2);
977 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm
, S_IWUSR
| S_IRUGO
,
978 show_pwm_min
, set_pwm_min
, 0);
979 static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm
, S_IWUSR
| S_IRUGO
,
980 show_pwm_min
, set_pwm_min
, 1);
981 static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm
, S_IWUSR
| S_IRUGO
,
982 show_pwm_min
, set_pwm_min
, 2);
984 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm
, S_IWUSR
| S_IRUGO
,
985 show_pwm_max
, set_pwm_max
, 0);
986 static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm
, S_IWUSR
| S_IRUGO
,
987 show_pwm_max
, set_pwm_max
, 1);
988 static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm
, S_IWUSR
| S_IRUGO
,
989 show_pwm_max
, set_pwm_max
, 2);
991 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp
, S_IWUSR
| S_IRUGO
,
992 show_temp_tmin
, set_temp_tmin
, 0);
993 static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp
, S_IWUSR
| S_IRUGO
,
994 show_temp_tmin
, set_temp_tmin
, 1);
995 static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp
, S_IWUSR
| S_IRUGO
,
996 show_temp_tmin
, set_temp_tmin
, 2);
998 static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp
, S_IWUSR
| S_IRUGO
,
999 show_temp_tmax
, set_temp_tmax
, 0);
1000 static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp
, S_IWUSR
| S_IRUGO
,
1001 show_temp_tmax
, set_temp_tmax
, 1);
1002 static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp
, S_IWUSR
| S_IRUGO
,
1003 show_temp_tmax
, set_temp_tmax
, 2);
1005 static SENSOR_DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
, show_pwm_enable
,
1007 static SENSOR_DEVICE_ATTR(pwm2_enable
, S_IWUSR
| S_IRUGO
, show_pwm_enable
,
1009 static SENSOR_DEVICE_ATTR(pwm3_enable
, S_IWUSR
| S_IRUGO
, show_pwm_enable
,
1012 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp
, S_IWUSR
| S_IRUGO
,
1013 show_pwm_auto_temp
, set_pwm_auto_temp
, 0);
1014 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp
, S_IWUSR
| S_IRUGO
,
1015 show_pwm_auto_temp
, set_pwm_auto_temp
, 1);
1016 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp
, S_IWUSR
| S_IRUGO
,
1017 show_pwm_auto_temp
, set_pwm_auto_temp
, 2);
1019 static struct attribute
*adt7473_attr
[] =
1021 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1022 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1023 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1024 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1025 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1026 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1027 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1028 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1030 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1031 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1032 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1033 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1034 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1035 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1036 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1037 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1038 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1039 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1040 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1041 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1042 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
1043 &sensor_dev_attr_temp2_auto_point1_temp
.dev_attr
.attr
,
1044 &sensor_dev_attr_temp3_auto_point1_temp
.dev_attr
.attr
,
1045 &sensor_dev_attr_temp1_auto_point2_temp
.dev_attr
.attr
,
1046 &sensor_dev_attr_temp2_auto_point2_temp
.dev_attr
.attr
,
1047 &sensor_dev_attr_temp3_auto_point2_temp
.dev_attr
.attr
,
1049 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1050 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1051 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1052 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1053 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1054 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1055 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1056 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1057 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1058 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1059 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1060 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1062 &sensor_dev_attr_pwm_use_point2_pwm_at_crit
.dev_attr
.attr
,
1064 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1065 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1066 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1067 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
1068 &sensor_dev_attr_pwm2_auto_point1_pwm
.dev_attr
.attr
,
1069 &sensor_dev_attr_pwm3_auto_point1_pwm
.dev_attr
.attr
,
1070 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
1071 &sensor_dev_attr_pwm2_auto_point2_pwm
.dev_attr
.attr
,
1072 &sensor_dev_attr_pwm3_auto_point2_pwm
.dev_attr
.attr
,
1074 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1075 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1076 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1077 &sensor_dev_attr_pwm1_auto_channels_temp
.dev_attr
.attr
,
1078 &sensor_dev_attr_pwm2_auto_channels_temp
.dev_attr
.attr
,
1079 &sensor_dev_attr_pwm3_auto_channels_temp
.dev_attr
.attr
,
1084 /* Return 0 if detection is successful, -ENODEV otherwise */
1085 static int adt7473_detect(struct i2c_client
*client
,
1086 struct i2c_board_info
*info
)
1088 struct i2c_adapter
*adapter
= client
->adapter
;
1089 int vendor
, device
, revision
;
1091 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1094 vendor
= i2c_smbus_read_byte_data(client
, ADT7473_REG_VENDOR
);
1095 if (vendor
!= ADT7473_VENDOR
)
1098 device
= i2c_smbus_read_byte_data(client
, ADT7473_REG_DEVICE
);
1099 if (device
!= ADT7473_DEVICE
)
1102 revision
= i2c_smbus_read_byte_data(client
, ADT7473_REG_REVISION
);
1103 if (revision
!= ADT7473_REV_68
&& revision
!= ADT7473_REV_69
)
1106 strlcpy(info
->type
, "adt7473", I2C_NAME_SIZE
);
1111 static int adt7473_probe(struct i2c_client
*client
,
1112 const struct i2c_device_id
*id
)
1114 struct adt7473_data
*data
;
1117 data
= kzalloc(sizeof(struct adt7473_data
), GFP_KERNEL
);
1123 i2c_set_clientdata(client
, data
);
1124 mutex_init(&data
->lock
);
1126 dev_info(&client
->dev
, "%s chip found\n", client
->name
);
1128 /* Initialize the ADT7473 chip */
1129 adt7473_init_client(client
);
1131 /* Register sysfs hooks */
1132 data
->attrs
.attrs
= adt7473_attr
;
1133 err
= sysfs_create_group(&client
->dev
.kobj
, &data
->attrs
);
1137 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1138 if (IS_ERR(data
->hwmon_dev
)) {
1139 err
= PTR_ERR(data
->hwmon_dev
);
1146 sysfs_remove_group(&client
->dev
.kobj
, &data
->attrs
);
1153 static int adt7473_remove(struct i2c_client
*client
)
1155 struct adt7473_data
*data
= i2c_get_clientdata(client
);
1157 hwmon_device_unregister(data
->hwmon_dev
);
1158 sysfs_remove_group(&client
->dev
.kobj
, &data
->attrs
);
1163 static int __init
adt7473_init(void)
1165 pr_notice("The adt7473 driver is deprecated, please use the adt7475 "
1166 "driver instead\n");
1167 return i2c_add_driver(&adt7473_driver
);
1170 static void __exit
adt7473_exit(void)
1172 i2c_del_driver(&adt7473_driver
);
1175 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1176 MODULE_DESCRIPTION("ADT7473 driver");
1177 MODULE_LICENSE("GPL");
1179 module_init(adt7473_init
);
1180 module_exit(adt7473_exit
);