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>
6 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
8 * Derived from the lm83 driver by Jean Delvare
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/hwmon-vid.h>
23 #include <linux/err.h>
24 #include <linux/jiffies.h>
25 #include <linux/util_macros.h>
27 /* Indexes for the sysfs hooks */
39 * These are unique identifiers for the sysfs functions - unlike the
40 * numbers above, these are not also indexes into an array
46 /* 7475 Common Registers */
48 #define REG_DEVREV2 0x12 /* ADT7490 only */
50 #define REG_VTT 0x1E /* ADT7490 only */
51 #define REG_EXTEND3 0x1F /* ADT7490 only */
53 #define REG_VOLTAGE_BASE 0x20
54 #define REG_TEMP_BASE 0x25
55 #define REG_TACH_BASE 0x28
56 #define REG_PWM_BASE 0x30
57 #define REG_PWM_MAX_BASE 0x38
59 #define REG_DEVID 0x3D
60 #define REG_VENDID 0x3E
61 #define REG_DEVID2 0x3F
63 #define REG_CONFIG1 0x40
65 #define REG_STATUS1 0x41
66 #define REG_STATUS2 0x42
68 #define REG_VID 0x43 /* ADT7476 only */
70 #define REG_VOLTAGE_MIN_BASE 0x44
71 #define REG_VOLTAGE_MAX_BASE 0x45
73 #define REG_TEMP_MIN_BASE 0x4E
74 #define REG_TEMP_MAX_BASE 0x4F
76 #define REG_TACH_MIN_BASE 0x54
78 #define REG_PWM_CONFIG_BASE 0x5C
80 #define REG_TEMP_TRANGE_BASE 0x5F
82 #define REG_ENHANCE_ACOUSTICS1 0x62
83 #define REG_ENHANCE_ACOUSTICS2 0x63
85 #define REG_PWM_MIN_BASE 0x64
87 #define REG_TEMP_TMIN_BASE 0x67
88 #define REG_TEMP_THERM_BASE 0x6A
90 #define REG_REMOTE1_HYSTERSIS 0x6D
91 #define REG_REMOTE2_HYSTERSIS 0x6E
93 #define REG_TEMP_OFFSET_BASE 0x70
95 #define REG_CONFIG2 0x73
97 #define REG_EXTEND1 0x76
98 #define REG_EXTEND2 0x77
100 #define REG_CONFIG3 0x78
101 #define REG_CONFIG5 0x7C
102 #define REG_CONFIG4 0x7D
104 #define REG_STATUS4 0x81 /* ADT7490 only */
106 #define REG_VTT_MIN 0x84 /* ADT7490 only */
107 #define REG_VTT_MAX 0x86 /* ADT7490 only */
109 #define VID_VIDSEL 0x80 /* ADT7476 only */
111 #define CONFIG2_ATTN 0x20
113 #define CONFIG3_SMBALERT 0x01
114 #define CONFIG3_THERM 0x02
116 #define CONFIG4_PINFUNC 0x03
117 #define CONFIG4_MAXDUTY 0x08
118 #define CONFIG4_ATTN_IN10 0x30
119 #define CONFIG4_ATTN_IN43 0xC0
121 #define CONFIG5_TWOSCOMP 0x01
122 #define CONFIG5_TEMPOFFSET 0x02
123 #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
125 /* ADT7475 Settings */
127 #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
128 #define ADT7475_TEMP_COUNT 3
129 #define ADT7475_TACH_COUNT 4
130 #define ADT7475_PWM_COUNT 3
132 /* Macro to read the registers */
134 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
136 /* Macros to easily index the registers */
138 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
139 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
141 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
142 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
143 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
144 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
146 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
147 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
148 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
150 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
151 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
152 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
153 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
154 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
155 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
156 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
158 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
160 enum chips
{ adt7473
, adt7475
, adt7476
, adt7490
};
162 static const struct i2c_device_id adt7475_id
[] = {
163 { "adt7473", adt7473
},
164 { "adt7475", adt7475
},
165 { "adt7476", adt7476
},
166 { "adt7490", adt7490
},
169 MODULE_DEVICE_TABLE(i2c
, adt7475_id
);
171 static const struct of_device_id adt7475_of_match
[] = {
173 .compatible
= "adi,adt7473",
174 .data
= (void *)adt7473
177 .compatible
= "adi,adt7475",
178 .data
= (void *)adt7475
181 .compatible
= "adi,adt7476",
182 .data
= (void *)adt7476
185 .compatible
= "adi,adt7490",
186 .data
= (void *)adt7490
190 MODULE_DEVICE_TABLE(of
, adt7475_of_match
);
192 struct adt7475_data
{
193 struct device
*hwmon_dev
;
196 unsigned long measure_updated
;
197 unsigned long limits_updated
;
203 u8 bypass_attn
; /* Bypass voltage attenuator */
221 static struct i2c_driver adt7475_driver
;
222 static struct adt7475_data
*adt7475_update_device(struct device
*dev
);
223 static void adt7475_read_hystersis(struct i2c_client
*client
);
224 static void adt7475_read_pwm(struct i2c_client
*client
, int index
);
226 /* Given a temp value, convert it to register value */
228 static inline u16
temp2reg(struct adt7475_data
*data
, long val
)
232 if (!(data
->config5
& CONFIG5_TWOSCOMP
)) {
233 val
= clamp_val(val
, -64000, 191000);
234 ret
= (val
+ 64500) / 1000;
236 val
= clamp_val(val
, -128000, 127000);
238 ret
= (256500 + val
) / 1000;
240 ret
= (val
+ 500) / 1000;
246 /* Given a register value, convert it to a real temp value */
248 static inline int reg2temp(struct adt7475_data
*data
, u16 reg
)
250 if (data
->config5
& CONFIG5_TWOSCOMP
) {
252 return (reg
- 1024) * 250;
256 return (reg
- 256) * 250;
259 static inline int tach2rpm(u16 tach
)
261 if (tach
== 0 || tach
== 0xFFFF)
264 return (90000 * 60) / tach
;
267 static inline u16
rpm2tach(unsigned long rpm
)
272 return clamp_val((90000 * 60) / rpm
, 1, 0xFFFF);
275 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
276 static const int adt7473_in_scaling
[ADT7475_VOLTAGE_COUNT
+ 1][2] = {
277 { 45, 94 }, /* +2.5V */
278 { 175, 525 }, /* Vccp */
279 { 68, 71 }, /* Vcc */
280 { 93, 47 }, /* +5V */
281 { 120, 20 }, /* +12V */
282 { 45, 45 }, /* Vtt */
285 static inline int reg2volt(int channel
, u16 reg
, u8 bypass_attn
)
287 const int *r
= adt7473_in_scaling
[channel
];
289 if (bypass_attn
& (1 << channel
))
290 return DIV_ROUND_CLOSEST(reg
* 2250, 1024);
291 return DIV_ROUND_CLOSEST(reg
* (r
[0] + r
[1]) * 2250, r
[1] * 1024);
294 static inline u16
volt2reg(int channel
, long volt
, u8 bypass_attn
)
296 const int *r
= adt7473_in_scaling
[channel
];
299 if (bypass_attn
& (1 << channel
))
300 reg
= (volt
* 1024) / 2250;
302 reg
= (volt
* r
[1] * 1024) / ((r
[0] + r
[1]) * 2250);
303 return clamp_val(reg
, 0, 1023) & (0xff << 2);
306 static u16
adt7475_read_word(struct i2c_client
*client
, int reg
)
310 val
= i2c_smbus_read_byte_data(client
, reg
);
311 val
|= (i2c_smbus_read_byte_data(client
, reg
+ 1) << 8);
316 static void adt7475_write_word(struct i2c_client
*client
, int reg
, u16 val
)
318 i2c_smbus_write_byte_data(client
, reg
+ 1, val
>> 8);
319 i2c_smbus_write_byte_data(client
, reg
, val
& 0xFF);
322 static ssize_t
show_voltage(struct device
*dev
, struct device_attribute
*attr
,
325 struct adt7475_data
*data
= adt7475_update_device(dev
);
326 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
331 return sprintf(buf
, "%d\n",
332 (data
->alarms
>> sattr
->index
) & 1);
334 val
= data
->voltage
[sattr
->nr
][sattr
->index
];
335 return sprintf(buf
, "%d\n",
336 reg2volt(sattr
->index
, val
, data
->bypass_attn
));
340 static ssize_t
set_voltage(struct device
*dev
, struct device_attribute
*attr
,
341 const char *buf
, size_t count
)
344 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
345 struct i2c_client
*client
= to_i2c_client(dev
);
346 struct adt7475_data
*data
= i2c_get_clientdata(client
);
350 if (kstrtol(buf
, 10, &val
))
353 mutex_lock(&data
->lock
);
355 data
->voltage
[sattr
->nr
][sattr
->index
] =
356 volt2reg(sattr
->index
, val
, data
->bypass_attn
);
358 if (sattr
->index
< ADT7475_VOLTAGE_COUNT
) {
359 if (sattr
->nr
== MIN
)
360 reg
= VOLTAGE_MIN_REG(sattr
->index
);
362 reg
= VOLTAGE_MAX_REG(sattr
->index
);
364 if (sattr
->nr
== MIN
)
370 i2c_smbus_write_byte_data(client
, reg
,
371 data
->voltage
[sattr
->nr
][sattr
->index
] >> 2);
372 mutex_unlock(&data
->lock
);
377 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
380 struct adt7475_data
*data
= adt7475_update_device(dev
);
381 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
386 mutex_lock(&data
->lock
);
387 out
= data
->temp
[sattr
->nr
][sattr
->index
];
388 if (sattr
->index
!= 1)
389 out
= (out
>> 4) & 0xF;
393 * Show the value as an absolute number tied to
396 out
= reg2temp(data
, data
->temp
[THERM
][sattr
->index
]) -
398 mutex_unlock(&data
->lock
);
403 * Offset is always 2's complement, regardless of the
406 mutex_lock(&data
->lock
);
407 out
= (s8
)data
->temp
[sattr
->nr
][sattr
->index
];
408 if (data
->config5
& CONFIG5_TEMPOFFSET
)
412 mutex_unlock(&data
->lock
);
416 out
= (data
->alarms
>> (sattr
->index
+ 4)) & 1;
420 /* Note - only for remote1 and remote2 */
421 out
= !!(data
->alarms
& (sattr
->index
? 0x8000 : 0x4000));
425 /* All other temp values are in the configured format */
426 out
= reg2temp(data
, data
->temp
[sattr
->nr
][sattr
->index
]);
429 return sprintf(buf
, "%d\n", out
);
432 static ssize_t
set_temp(struct device
*dev
, struct device_attribute
*attr
,
433 const char *buf
, size_t count
)
435 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
436 struct i2c_client
*client
= to_i2c_client(dev
);
437 struct adt7475_data
*data
= i2c_get_clientdata(client
);
438 unsigned char reg
= 0;
443 if (kstrtol(buf
, 10, &val
))
446 mutex_lock(&data
->lock
);
448 /* We need the config register in all cases for temp <-> reg conv. */
449 data
->config5
= adt7475_read(REG_CONFIG5
);
453 if (data
->config5
& CONFIG5_TEMPOFFSET
) {
454 val
= clamp_val(val
, -63000, 127000);
455 out
= data
->temp
[OFFSET
][sattr
->index
] = val
/ 1000;
457 val
= clamp_val(val
, -63000, 64000);
458 out
= data
->temp
[OFFSET
][sattr
->index
] = val
/ 500;
464 * The value will be given as an absolute value, turn it
465 * into an offset based on THERM
468 /* Read fresh THERM and HYSTERSIS values from the chip */
469 data
->temp
[THERM
][sattr
->index
] =
470 adt7475_read(TEMP_THERM_REG(sattr
->index
)) << 2;
471 adt7475_read_hystersis(client
);
473 temp
= reg2temp(data
, data
->temp
[THERM
][sattr
->index
]);
474 val
= clamp_val(val
, temp
- 15000, temp
);
475 val
= (temp
- val
) / 1000;
477 if (sattr
->index
!= 1) {
478 data
->temp
[HYSTERSIS
][sattr
->index
] &= 0xF0;
479 data
->temp
[HYSTERSIS
][sattr
->index
] |= (val
& 0xF) << 4;
481 data
->temp
[HYSTERSIS
][sattr
->index
] &= 0x0F;
482 data
->temp
[HYSTERSIS
][sattr
->index
] |= (val
& 0xF);
485 out
= data
->temp
[HYSTERSIS
][sattr
->index
];
489 data
->temp
[sattr
->nr
][sattr
->index
] = temp2reg(data
, val
);
492 * We maintain an extra 2 digits of precision for simplicity
493 * - shift those back off before writing the value
495 out
= (u8
) (data
->temp
[sattr
->nr
][sattr
->index
] >> 2);
500 reg
= TEMP_MIN_REG(sattr
->index
);
503 reg
= TEMP_MAX_REG(sattr
->index
);
506 reg
= TEMP_OFFSET_REG(sattr
->index
);
509 reg
= TEMP_TMIN_REG(sattr
->index
);
512 reg
= TEMP_THERM_REG(sattr
->index
);
515 if (sattr
->index
!= 2)
516 reg
= REG_REMOTE1_HYSTERSIS
;
518 reg
= REG_REMOTE2_HYSTERSIS
;
523 i2c_smbus_write_byte_data(client
, reg
, out
);
525 mutex_unlock(&data
->lock
);
529 /* Assuming CONFIG6[SLOW] is 0 */
530 static const int ad7475_st_map
[] = {
531 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
534 static ssize_t
show_temp_st(struct device
*dev
, struct device_attribute
*attr
,
537 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
538 struct i2c_client
*client
= to_i2c_client(dev
);
539 struct adt7475_data
*data
= i2c_get_clientdata(client
);
542 switch (sattr
->index
) {
544 val
= data
->enh_acoustics
[0] & 0xf;
547 val
= (data
->enh_acoustics
[1] >> 4) & 0xf;
551 val
= data
->enh_acoustics
[1] & 0xf;
556 return sprintf(buf
, "%d\n", ad7475_st_map
[val
& 0x7]);
558 return sprintf(buf
, "0\n");
561 static ssize_t
set_temp_st(struct device
*dev
, struct device_attribute
*attr
,
562 const char *buf
, size_t count
)
564 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
565 struct i2c_client
*client
= to_i2c_client(dev
);
566 struct adt7475_data
*data
= i2c_get_clientdata(client
);
571 if (kstrtoul(buf
, 10, &val
))
574 switch (sattr
->index
) {
576 reg
= REG_ENHANCE_ACOUSTICS1
;
581 reg
= REG_ENHANCE_ACOUSTICS2
;
587 reg
= REG_ENHANCE_ACOUSTICS2
;
594 val
= find_closest_descending(val
, ad7475_st_map
,
595 ARRAY_SIZE(ad7475_st_map
));
599 mutex_lock(&data
->lock
);
601 data
->enh_acoustics
[idx
] &= ~(0xf << shift
);
602 data
->enh_acoustics
[idx
] |= (val
<< shift
);
604 i2c_smbus_write_byte_data(client
, reg
, data
->enh_acoustics
[idx
]);
606 mutex_unlock(&data
->lock
);
612 * Table of autorange values - the user will write the value in millidegrees,
613 * and we'll convert it
615 static const int autorange_table
[] = {
616 2000, 2500, 3330, 4000, 5000, 6670, 8000,
617 10000, 13330, 16000, 20000, 26670, 32000, 40000,
621 static ssize_t
show_point2(struct device
*dev
, struct device_attribute
*attr
,
624 struct adt7475_data
*data
= adt7475_update_device(dev
);
625 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
628 mutex_lock(&data
->lock
);
629 out
= (data
->range
[sattr
->index
] >> 4) & 0x0F;
630 val
= reg2temp(data
, data
->temp
[AUTOMIN
][sattr
->index
]);
631 mutex_unlock(&data
->lock
);
633 return sprintf(buf
, "%d\n", val
+ autorange_table
[out
]);
636 static ssize_t
set_point2(struct device
*dev
, struct device_attribute
*attr
,
637 const char *buf
, size_t count
)
639 struct i2c_client
*client
= to_i2c_client(dev
);
640 struct adt7475_data
*data
= i2c_get_clientdata(client
);
641 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
645 if (kstrtol(buf
, 10, &val
))
648 mutex_lock(&data
->lock
);
650 /* Get a fresh copy of the needed registers */
651 data
->config5
= adt7475_read(REG_CONFIG5
);
652 data
->temp
[AUTOMIN
][sattr
->index
] =
653 adt7475_read(TEMP_TMIN_REG(sattr
->index
)) << 2;
654 data
->range
[sattr
->index
] =
655 adt7475_read(TEMP_TRANGE_REG(sattr
->index
));
658 * The user will write an absolute value, so subtract the start point
659 * to figure the range
661 temp
= reg2temp(data
, data
->temp
[AUTOMIN
][sattr
->index
]);
662 val
= clamp_val(val
, temp
+ autorange_table
[0],
663 temp
+ autorange_table
[ARRAY_SIZE(autorange_table
) - 1]);
666 /* Find the nearest table entry to what the user wrote */
667 val
= find_closest(val
, autorange_table
, ARRAY_SIZE(autorange_table
));
669 data
->range
[sattr
->index
] &= ~0xF0;
670 data
->range
[sattr
->index
] |= val
<< 4;
672 i2c_smbus_write_byte_data(client
, TEMP_TRANGE_REG(sattr
->index
),
673 data
->range
[sattr
->index
]);
675 mutex_unlock(&data
->lock
);
679 static ssize_t
show_tach(struct device
*dev
, struct device_attribute
*attr
,
682 struct adt7475_data
*data
= adt7475_update_device(dev
);
683 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
686 if (sattr
->nr
== ALARM
)
687 out
= (data
->alarms
>> (sattr
->index
+ 10)) & 1;
689 out
= tach2rpm(data
->tach
[sattr
->nr
][sattr
->index
]);
691 return sprintf(buf
, "%d\n", out
);
694 static ssize_t
set_tach(struct device
*dev
, struct device_attribute
*attr
,
695 const char *buf
, size_t count
)
698 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
699 struct i2c_client
*client
= to_i2c_client(dev
);
700 struct adt7475_data
*data
= i2c_get_clientdata(client
);
703 if (kstrtoul(buf
, 10, &val
))
706 mutex_lock(&data
->lock
);
708 data
->tach
[MIN
][sattr
->index
] = rpm2tach(val
);
710 adt7475_write_word(client
, TACH_MIN_REG(sattr
->index
),
711 data
->tach
[MIN
][sattr
->index
]);
713 mutex_unlock(&data
->lock
);
717 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
720 struct adt7475_data
*data
= adt7475_update_device(dev
);
721 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
723 return sprintf(buf
, "%d\n", data
->pwm
[sattr
->nr
][sattr
->index
]);
726 static ssize_t
show_pwmchan(struct device
*dev
, struct device_attribute
*attr
,
729 struct adt7475_data
*data
= adt7475_update_device(dev
);
730 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
732 return sprintf(buf
, "%d\n", data
->pwmchan
[sattr
->index
]);
735 static ssize_t
show_pwmctrl(struct device
*dev
, struct device_attribute
*attr
,
738 struct adt7475_data
*data
= adt7475_update_device(dev
);
739 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
741 return sprintf(buf
, "%d\n", data
->pwmctl
[sattr
->index
]);
744 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
745 const char *buf
, size_t count
)
748 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
749 struct i2c_client
*client
= to_i2c_client(dev
);
750 struct adt7475_data
*data
= i2c_get_clientdata(client
);
751 unsigned char reg
= 0;
754 if (kstrtol(buf
, 10, &val
))
757 mutex_lock(&data
->lock
);
761 /* Get a fresh value for CONTROL */
762 data
->pwm
[CONTROL
][sattr
->index
] =
763 adt7475_read(PWM_CONFIG_REG(sattr
->index
));
766 * If we are not in manual mode, then we shouldn't allow
767 * the user to set the pwm speed
769 if (((data
->pwm
[CONTROL
][sattr
->index
] >> 5) & 7) != 7) {
770 mutex_unlock(&data
->lock
);
774 reg
= PWM_REG(sattr
->index
);
778 reg
= PWM_MIN_REG(sattr
->index
);
782 reg
= PWM_MAX_REG(sattr
->index
);
786 data
->pwm
[sattr
->nr
][sattr
->index
] = clamp_val(val
, 0, 0xFF);
787 i2c_smbus_write_byte_data(client
, reg
,
788 data
->pwm
[sattr
->nr
][sattr
->index
]);
789 mutex_unlock(&data
->lock
);
794 static ssize_t
show_stall_disable(struct device
*dev
,
795 struct device_attribute
*attr
, char *buf
)
797 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
798 struct i2c_client
*client
= to_i2c_client(dev
);
799 struct adt7475_data
*data
= i2c_get_clientdata(client
);
800 u8 mask
= BIT(5 + sattr
->index
);
802 return sprintf(buf
, "%d\n", !!(data
->enh_acoustics
[0] & mask
));
805 static ssize_t
set_stall_disable(struct device
*dev
,
806 struct device_attribute
*attr
, const char *buf
,
809 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
810 struct i2c_client
*client
= to_i2c_client(dev
);
811 struct adt7475_data
*data
= i2c_get_clientdata(client
);
813 u8 mask
= BIT(5 + sattr
->index
);
815 if (kstrtol(buf
, 10, &val
))
818 mutex_lock(&data
->lock
);
820 data
->enh_acoustics
[0] &= ~mask
;
822 data
->enh_acoustics
[0] |= mask
;
824 i2c_smbus_write_byte_data(client
, REG_ENHANCE_ACOUSTICS1
,
825 data
->enh_acoustics
[0]);
827 mutex_unlock(&data
->lock
);
832 /* Called by set_pwmctrl and set_pwmchan */
834 static int hw_set_pwm(struct i2c_client
*client
, int index
,
835 unsigned int pwmctl
, unsigned int pwmchan
)
837 struct adt7475_data
*data
= i2c_get_clientdata(client
);
842 val
= 0x03; /* Run at full speed */
845 val
= 0x07; /* Manual mode */
850 /* Remote1 controls PWM */
854 /* local controls PWM */
858 /* remote2 controls PWM */
862 /* local/remote2 control PWM */
866 /* All three control PWM */
877 data
->pwmctl
[index
] = pwmctl
;
878 data
->pwmchan
[index
] = pwmchan
;
880 data
->pwm
[CONTROL
][index
] &= ~0xE0;
881 data
->pwm
[CONTROL
][index
] |= (val
& 7) << 5;
883 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
884 data
->pwm
[CONTROL
][index
]);
889 static ssize_t
set_pwmchan(struct device
*dev
, struct device_attribute
*attr
,
890 const char *buf
, size_t count
)
892 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
893 struct i2c_client
*client
= to_i2c_client(dev
);
894 struct adt7475_data
*data
= i2c_get_clientdata(client
);
898 if (kstrtol(buf
, 10, &val
))
901 mutex_lock(&data
->lock
);
902 /* Read Modify Write PWM values */
903 adt7475_read_pwm(client
, sattr
->index
);
904 r
= hw_set_pwm(client
, sattr
->index
, data
->pwmctl
[sattr
->index
], val
);
907 mutex_unlock(&data
->lock
);
912 static ssize_t
set_pwmctrl(struct device
*dev
, struct device_attribute
*attr
,
913 const char *buf
, size_t count
)
915 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
916 struct i2c_client
*client
= to_i2c_client(dev
);
917 struct adt7475_data
*data
= i2c_get_clientdata(client
);
921 if (kstrtol(buf
, 10, &val
))
924 mutex_lock(&data
->lock
);
925 /* Read Modify Write PWM values */
926 adt7475_read_pwm(client
, sattr
->index
);
927 r
= hw_set_pwm(client
, sattr
->index
, val
, data
->pwmchan
[sattr
->index
]);
930 mutex_unlock(&data
->lock
);
935 /* List of frequencies for the PWM */
936 static const int pwmfreq_table
[] = {
937 11, 14, 22, 29, 35, 44, 58, 88, 22500
940 static ssize_t
show_pwmfreq(struct device
*dev
, struct device_attribute
*attr
,
943 struct adt7475_data
*data
= adt7475_update_device(dev
);
944 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
945 int i
= clamp_val(data
->range
[sattr
->index
] & 0xf, 0,
946 ARRAY_SIZE(pwmfreq_table
) - 1);
948 return sprintf(buf
, "%d\n", pwmfreq_table
[i
]);
951 static ssize_t
set_pwmfreq(struct device
*dev
, struct device_attribute
*attr
,
952 const char *buf
, size_t count
)
954 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
955 struct i2c_client
*client
= to_i2c_client(dev
);
956 struct adt7475_data
*data
= i2c_get_clientdata(client
);
960 if (kstrtol(buf
, 10, &val
))
963 out
= find_closest(val
, pwmfreq_table
, ARRAY_SIZE(pwmfreq_table
));
965 mutex_lock(&data
->lock
);
967 data
->range
[sattr
->index
] =
968 adt7475_read(TEMP_TRANGE_REG(sattr
->index
));
969 data
->range
[sattr
->index
] &= ~0xf;
970 data
->range
[sattr
->index
] |= out
;
972 i2c_smbus_write_byte_data(client
, TEMP_TRANGE_REG(sattr
->index
),
973 data
->range
[sattr
->index
]);
975 mutex_unlock(&data
->lock
);
979 static ssize_t
pwm_use_point2_pwm_at_crit_show(struct device
*dev
,
980 struct device_attribute
*devattr
,
983 struct adt7475_data
*data
= adt7475_update_device(dev
);
984 return sprintf(buf
, "%d\n", !!(data
->config4
& CONFIG4_MAXDUTY
));
987 static ssize_t
pwm_use_point2_pwm_at_crit_store(struct device
*dev
,
988 struct device_attribute
*devattr
,
989 const char *buf
, size_t count
)
991 struct i2c_client
*client
= to_i2c_client(dev
);
992 struct adt7475_data
*data
= i2c_get_clientdata(client
);
995 if (kstrtol(buf
, 10, &val
))
997 if (val
!= 0 && val
!= 1)
1000 mutex_lock(&data
->lock
);
1001 data
->config4
= i2c_smbus_read_byte_data(client
, REG_CONFIG4
);
1003 data
->config4
|= CONFIG4_MAXDUTY
;
1005 data
->config4
&= ~CONFIG4_MAXDUTY
;
1006 i2c_smbus_write_byte_data(client
, REG_CONFIG4
, data
->config4
);
1007 mutex_unlock(&data
->lock
);
1012 static ssize_t
vrm_show(struct device
*dev
, struct device_attribute
*devattr
,
1015 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1016 return sprintf(buf
, "%d\n", (int)data
->vrm
);
1019 static ssize_t
vrm_store(struct device
*dev
, struct device_attribute
*devattr
,
1020 const char *buf
, size_t count
)
1022 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1025 if (kstrtol(buf
, 10, &val
))
1027 if (val
< 0 || val
> 255)
1034 static ssize_t
cpu0_vid_show(struct device
*dev
,
1035 struct device_attribute
*devattr
, char *buf
)
1037 struct adt7475_data
*data
= adt7475_update_device(dev
);
1038 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
1041 static SENSOR_DEVICE_ATTR_2(in0_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 0);
1042 static SENSOR_DEVICE_ATTR_2(in0_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1043 set_voltage
, MAX
, 0);
1044 static SENSOR_DEVICE_ATTR_2(in0_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1045 set_voltage
, MIN
, 0);
1046 static SENSOR_DEVICE_ATTR_2(in0_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 0);
1047 static SENSOR_DEVICE_ATTR_2(in1_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 1);
1048 static SENSOR_DEVICE_ATTR_2(in1_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1049 set_voltage
, MAX
, 1);
1050 static SENSOR_DEVICE_ATTR_2(in1_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1051 set_voltage
, MIN
, 1);
1052 static SENSOR_DEVICE_ATTR_2(in1_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 1);
1053 static SENSOR_DEVICE_ATTR_2(in2_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 2);
1054 static SENSOR_DEVICE_ATTR_2(in2_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1055 set_voltage
, MAX
, 2);
1056 static SENSOR_DEVICE_ATTR_2(in2_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1057 set_voltage
, MIN
, 2);
1058 static SENSOR_DEVICE_ATTR_2(in2_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 2);
1059 static SENSOR_DEVICE_ATTR_2(in3_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 3);
1060 static SENSOR_DEVICE_ATTR_2(in3_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1061 set_voltage
, MAX
, 3);
1062 static SENSOR_DEVICE_ATTR_2(in3_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1063 set_voltage
, MIN
, 3);
1064 static SENSOR_DEVICE_ATTR_2(in3_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 3);
1065 static SENSOR_DEVICE_ATTR_2(in4_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 4);
1066 static SENSOR_DEVICE_ATTR_2(in4_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1067 set_voltage
, MAX
, 4);
1068 static SENSOR_DEVICE_ATTR_2(in4_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1069 set_voltage
, MIN
, 4);
1070 static SENSOR_DEVICE_ATTR_2(in4_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 8);
1071 static SENSOR_DEVICE_ATTR_2(in5_input
, S_IRUGO
, show_voltage
, NULL
, INPUT
, 5);
1072 static SENSOR_DEVICE_ATTR_2(in5_max
, S_IRUGO
| S_IWUSR
, show_voltage
,
1073 set_voltage
, MAX
, 5);
1074 static SENSOR_DEVICE_ATTR_2(in5_min
, S_IRUGO
| S_IWUSR
, show_voltage
,
1075 set_voltage
, MIN
, 5);
1076 static SENSOR_DEVICE_ATTR_2(in5_alarm
, S_IRUGO
, show_voltage
, NULL
, ALARM
, 31);
1077 static SENSOR_DEVICE_ATTR_2(temp1_input
, S_IRUGO
, show_temp
, NULL
, INPUT
, 0);
1078 static SENSOR_DEVICE_ATTR_2(temp1_alarm
, S_IRUGO
, show_temp
, NULL
, ALARM
, 0);
1079 static SENSOR_DEVICE_ATTR_2(temp1_fault
, S_IRUGO
, show_temp
, NULL
, FAULT
, 0);
1080 static SENSOR_DEVICE_ATTR_2(temp1_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1082 static SENSOR_DEVICE_ATTR_2(temp1_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1084 static SENSOR_DEVICE_ATTR_2(temp1_offset
, S_IRUGO
| S_IWUSR
, show_temp
,
1085 set_temp
, OFFSET
, 0);
1086 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp
, S_IRUGO
| S_IWUSR
,
1087 show_temp
, set_temp
, AUTOMIN
, 0);
1088 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp
, S_IRUGO
| S_IWUSR
,
1089 show_point2
, set_point2
, 0, 0);
1090 static SENSOR_DEVICE_ATTR_2(temp1_crit
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1092 static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst
, S_IRUGO
| S_IWUSR
, show_temp
,
1093 set_temp
, HYSTERSIS
, 0);
1094 static SENSOR_DEVICE_ATTR_2(temp1_smoothing
, S_IRUGO
| S_IWUSR
, show_temp_st
,
1096 static SENSOR_DEVICE_ATTR_2(temp2_input
, S_IRUGO
, show_temp
, NULL
, INPUT
, 1);
1097 static SENSOR_DEVICE_ATTR_2(temp2_alarm
, S_IRUGO
, show_temp
, NULL
, ALARM
, 1);
1098 static SENSOR_DEVICE_ATTR_2(temp2_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1100 static SENSOR_DEVICE_ATTR_2(temp2_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1102 static SENSOR_DEVICE_ATTR_2(temp2_offset
, S_IRUGO
| S_IWUSR
, show_temp
,
1103 set_temp
, OFFSET
, 1);
1104 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp
, S_IRUGO
| S_IWUSR
,
1105 show_temp
, set_temp
, AUTOMIN
, 1);
1106 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp
, S_IRUGO
| S_IWUSR
,
1107 show_point2
, set_point2
, 0, 1);
1108 static SENSOR_DEVICE_ATTR_2(temp2_crit
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1110 static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst
, S_IRUGO
| S_IWUSR
, show_temp
,
1111 set_temp
, HYSTERSIS
, 1);
1112 static SENSOR_DEVICE_ATTR_2(temp2_smoothing
, S_IRUGO
| S_IWUSR
, show_temp_st
,
1114 static SENSOR_DEVICE_ATTR_2(temp3_input
, S_IRUGO
, show_temp
, NULL
, INPUT
, 2);
1115 static SENSOR_DEVICE_ATTR_2(temp3_alarm
, S_IRUGO
, show_temp
, NULL
, ALARM
, 2);
1116 static SENSOR_DEVICE_ATTR_2(temp3_fault
, S_IRUGO
, show_temp
, NULL
, FAULT
, 2);
1117 static SENSOR_DEVICE_ATTR_2(temp3_max
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1119 static SENSOR_DEVICE_ATTR_2(temp3_min
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1121 static SENSOR_DEVICE_ATTR_2(temp3_offset
, S_IRUGO
| S_IWUSR
, show_temp
,
1122 set_temp
, OFFSET
, 2);
1123 static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp
, S_IRUGO
| S_IWUSR
,
1124 show_temp
, set_temp
, AUTOMIN
, 2);
1125 static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp
, S_IRUGO
| S_IWUSR
,
1126 show_point2
, set_point2
, 0, 2);
1127 static SENSOR_DEVICE_ATTR_2(temp3_crit
, S_IRUGO
| S_IWUSR
, show_temp
, set_temp
,
1129 static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst
, S_IRUGO
| S_IWUSR
, show_temp
,
1130 set_temp
, HYSTERSIS
, 2);
1131 static SENSOR_DEVICE_ATTR_2(temp3_smoothing
, S_IRUGO
| S_IWUSR
, show_temp_st
,
1133 static SENSOR_DEVICE_ATTR_2(fan1_input
, S_IRUGO
, show_tach
, NULL
, INPUT
, 0);
1134 static SENSOR_DEVICE_ATTR_2(fan1_min
, S_IRUGO
| S_IWUSR
, show_tach
, set_tach
,
1136 static SENSOR_DEVICE_ATTR_2(fan1_alarm
, S_IRUGO
, show_tach
, NULL
, ALARM
, 0);
1137 static SENSOR_DEVICE_ATTR_2(fan2_input
, S_IRUGO
, show_tach
, NULL
, INPUT
, 1);
1138 static SENSOR_DEVICE_ATTR_2(fan2_min
, S_IRUGO
| S_IWUSR
, show_tach
, set_tach
,
1140 static SENSOR_DEVICE_ATTR_2(fan2_alarm
, S_IRUGO
, show_tach
, NULL
, ALARM
, 1);
1141 static SENSOR_DEVICE_ATTR_2(fan3_input
, S_IRUGO
, show_tach
, NULL
, INPUT
, 2);
1142 static SENSOR_DEVICE_ATTR_2(fan3_min
, S_IRUGO
| S_IWUSR
, show_tach
, set_tach
,
1144 static SENSOR_DEVICE_ATTR_2(fan3_alarm
, S_IRUGO
, show_tach
, NULL
, ALARM
, 2);
1145 static SENSOR_DEVICE_ATTR_2(fan4_input
, S_IRUGO
, show_tach
, NULL
, INPUT
, 3);
1146 static SENSOR_DEVICE_ATTR_2(fan4_min
, S_IRUGO
| S_IWUSR
, show_tach
, set_tach
,
1148 static SENSOR_DEVICE_ATTR_2(fan4_alarm
, S_IRUGO
, show_tach
, NULL
, ALARM
, 3);
1149 static SENSOR_DEVICE_ATTR_2(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, INPUT
,
1151 static SENSOR_DEVICE_ATTR_2(pwm1_freq
, S_IRUGO
| S_IWUSR
, show_pwmfreq
,
1152 set_pwmfreq
, INPUT
, 0);
1153 static SENSOR_DEVICE_ATTR_2(pwm1_enable
, S_IRUGO
| S_IWUSR
, show_pwmctrl
,
1154 set_pwmctrl
, INPUT
, 0);
1155 static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp
, S_IRUGO
| S_IWUSR
,
1156 show_pwmchan
, set_pwmchan
, INPUT
, 0);
1157 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1159 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1161 static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable
, S_IRUGO
| S_IWUSR
,
1162 show_stall_disable
, set_stall_disable
, 0, 0);
1163 static SENSOR_DEVICE_ATTR_2(pwm2
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, INPUT
,
1165 static SENSOR_DEVICE_ATTR_2(pwm2_freq
, S_IRUGO
| S_IWUSR
, show_pwmfreq
,
1166 set_pwmfreq
, INPUT
, 1);
1167 static SENSOR_DEVICE_ATTR_2(pwm2_enable
, S_IRUGO
| S_IWUSR
, show_pwmctrl
,
1168 set_pwmctrl
, INPUT
, 1);
1169 static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp
, S_IRUGO
| S_IWUSR
,
1170 show_pwmchan
, set_pwmchan
, INPUT
, 1);
1171 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1173 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1175 static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable
, S_IRUGO
| S_IWUSR
,
1176 show_stall_disable
, set_stall_disable
, 0, 1);
1177 static SENSOR_DEVICE_ATTR_2(pwm3
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
, INPUT
,
1179 static SENSOR_DEVICE_ATTR_2(pwm3_freq
, S_IRUGO
| S_IWUSR
, show_pwmfreq
,
1180 set_pwmfreq
, INPUT
, 2);
1181 static SENSOR_DEVICE_ATTR_2(pwm3_enable
, S_IRUGO
| S_IWUSR
, show_pwmctrl
,
1182 set_pwmctrl
, INPUT
, 2);
1183 static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp
, S_IRUGO
| S_IWUSR
,
1184 show_pwmchan
, set_pwmchan
, INPUT
, 2);
1185 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1187 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm
, S_IRUGO
| S_IWUSR
, show_pwm
,
1189 static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable
, S_IRUGO
| S_IWUSR
,
1190 show_stall_disable
, set_stall_disable
, 0, 2);
1192 /* Non-standard name, might need revisiting */
1193 static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit
);
1195 static DEVICE_ATTR_RW(vrm
);
1196 static DEVICE_ATTR_RO(cpu0_vid
);
1198 static struct attribute
*adt7475_attrs
[] = {
1199 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1200 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1201 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1202 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1203 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1204 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1205 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1206 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1207 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1208 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1209 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1210 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1211 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1212 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
1213 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
1214 &sensor_dev_attr_temp1_auto_point2_temp
.dev_attr
.attr
,
1215 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
1216 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
1217 &sensor_dev_attr_temp1_smoothing
.dev_attr
.attr
,
1218 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1219 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1220 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1221 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1222 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
1223 &sensor_dev_attr_temp2_auto_point1_temp
.dev_attr
.attr
,
1224 &sensor_dev_attr_temp2_auto_point2_temp
.dev_attr
.attr
,
1225 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
1226 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
1227 &sensor_dev_attr_temp2_smoothing
.dev_attr
.attr
,
1228 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1229 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1230 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1231 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1232 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1233 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
1234 &sensor_dev_attr_temp3_auto_point1_temp
.dev_attr
.attr
,
1235 &sensor_dev_attr_temp3_auto_point2_temp
.dev_attr
.attr
,
1236 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
1237 &sensor_dev_attr_temp3_crit_hyst
.dev_attr
.attr
,
1238 &sensor_dev_attr_temp3_smoothing
.dev_attr
.attr
,
1239 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1240 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1241 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1242 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1243 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1244 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1245 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1246 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1247 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1248 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1249 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1250 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1251 &sensor_dev_attr_pwm1_auto_channels_temp
.dev_attr
.attr
,
1252 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
1253 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
1254 &sensor_dev_attr_pwm1_stall_disable
.dev_attr
.attr
,
1255 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1256 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1257 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1258 &sensor_dev_attr_pwm3_auto_channels_temp
.dev_attr
.attr
,
1259 &sensor_dev_attr_pwm3_auto_point1_pwm
.dev_attr
.attr
,
1260 &sensor_dev_attr_pwm3_auto_point2_pwm
.dev_attr
.attr
,
1261 &sensor_dev_attr_pwm3_stall_disable
.dev_attr
.attr
,
1262 &dev_attr_pwm_use_point2_pwm_at_crit
.attr
,
1266 static struct attribute
*fan4_attrs
[] = {
1267 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1268 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1269 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1273 static struct attribute
*pwm2_attrs
[] = {
1274 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1275 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1276 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1277 &sensor_dev_attr_pwm2_auto_channels_temp
.dev_attr
.attr
,
1278 &sensor_dev_attr_pwm2_auto_point1_pwm
.dev_attr
.attr
,
1279 &sensor_dev_attr_pwm2_auto_point2_pwm
.dev_attr
.attr
,
1280 &sensor_dev_attr_pwm2_stall_disable
.dev_attr
.attr
,
1284 static struct attribute
*in0_attrs
[] = {
1285 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1286 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1287 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1288 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1292 static struct attribute
*in3_attrs
[] = {
1293 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1294 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1295 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1296 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1300 static struct attribute
*in4_attrs
[] = {
1301 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1302 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1303 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1304 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1308 static struct attribute
*in5_attrs
[] = {
1309 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1310 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1311 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1312 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1316 static struct attribute
*vid_attrs
[] = {
1317 &dev_attr_cpu0_vid
.attr
,
1322 static const struct attribute_group adt7475_attr_group
= { .attrs
= adt7475_attrs
};
1323 static const struct attribute_group fan4_attr_group
= { .attrs
= fan4_attrs
};
1324 static const struct attribute_group pwm2_attr_group
= { .attrs
= pwm2_attrs
};
1325 static const struct attribute_group in0_attr_group
= { .attrs
= in0_attrs
};
1326 static const struct attribute_group in3_attr_group
= { .attrs
= in3_attrs
};
1327 static const struct attribute_group in4_attr_group
= { .attrs
= in4_attrs
};
1328 static const struct attribute_group in5_attr_group
= { .attrs
= in5_attrs
};
1329 static const struct attribute_group vid_attr_group
= { .attrs
= vid_attrs
};
1331 static int adt7475_detect(struct i2c_client
*client
,
1332 struct i2c_board_info
*info
)
1334 struct i2c_adapter
*adapter
= client
->adapter
;
1335 int vendid
, devid
, devid2
;
1338 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1341 vendid
= adt7475_read(REG_VENDID
);
1342 devid2
= adt7475_read(REG_DEVID2
);
1343 if (vendid
!= 0x41 || /* Analog Devices */
1344 (devid2
& 0xf8) != 0x68)
1347 devid
= adt7475_read(REG_DEVID
);
1350 else if (devid
== 0x75 && client
->addr
== 0x2e)
1352 else if (devid
== 0x76)
1354 else if ((devid2
& 0xfc) == 0x6c)
1357 dev_dbg(&adapter
->dev
,
1358 "Couldn't detect an ADT7473/75/76/90 part at "
1359 "0x%02x\n", (unsigned int)client
->addr
);
1363 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1368 static void adt7475_remove_files(struct i2c_client
*client
,
1369 struct adt7475_data
*data
)
1371 sysfs_remove_group(&client
->dev
.kobj
, &adt7475_attr_group
);
1373 sysfs_remove_group(&client
->dev
.kobj
, &fan4_attr_group
);
1375 sysfs_remove_group(&client
->dev
.kobj
, &pwm2_attr_group
);
1376 if (data
->has_voltage
& (1 << 0))
1377 sysfs_remove_group(&client
->dev
.kobj
, &in0_attr_group
);
1378 if (data
->has_voltage
& (1 << 3))
1379 sysfs_remove_group(&client
->dev
.kobj
, &in3_attr_group
);
1380 if (data
->has_voltage
& (1 << 4))
1381 sysfs_remove_group(&client
->dev
.kobj
, &in4_attr_group
);
1382 if (data
->has_voltage
& (1 << 5))
1383 sysfs_remove_group(&client
->dev
.kobj
, &in5_attr_group
);
1385 sysfs_remove_group(&client
->dev
.kobj
, &vid_attr_group
);
1388 static int adt7475_probe(struct i2c_client
*client
,
1389 const struct i2c_device_id
*id
)
1392 static const char * const names
[] = {
1393 [adt7473
] = "ADT7473",
1394 [adt7475
] = "ADT7475",
1395 [adt7476
] = "ADT7476",
1396 [adt7490
] = "ADT7490",
1399 struct adt7475_data
*data
;
1400 int i
, ret
= 0, revision
;
1401 u8 config2
, config3
;
1403 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1407 mutex_init(&data
->lock
);
1408 i2c_set_clientdata(client
, data
);
1410 if (client
->dev
.of_node
)
1411 chip
= (enum chips
)of_device_get_match_data(&client
->dev
);
1413 chip
= id
->driver_data
;
1415 /* Initialize device-specific values */
1418 data
->has_voltage
= 0x0e; /* in1 to in3 */
1419 revision
= adt7475_read(REG_DEVID2
) & 0x07;
1422 data
->has_voltage
= 0x3e; /* in1 to in5 */
1423 revision
= adt7475_read(REG_DEVID2
) & 0x03;
1424 if (revision
== 0x03)
1425 revision
+= adt7475_read(REG_DEVREV2
);
1428 data
->has_voltage
= 0x06; /* in1, in2 */
1429 revision
= adt7475_read(REG_DEVID2
) & 0x07;
1432 config3
= adt7475_read(REG_CONFIG3
);
1433 /* Pin PWM2 may alternatively be used for ALERT output */
1434 if (!(config3
& CONFIG3_SMBALERT
))
1436 /* Meaning of this bit is inverted for the ADT7473-1 */
1437 if (id
->driver_data
== adt7473
&& revision
>= 1)
1438 data
->has_pwm2
= !data
->has_pwm2
;
1440 data
->config4
= adt7475_read(REG_CONFIG4
);
1441 /* Pin TACH4 may alternatively be used for THERM */
1442 if ((data
->config4
& CONFIG4_PINFUNC
) == 0x0)
1446 * THERM configuration is more complex on the ADT7476 and ADT7490,
1447 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1450 if (id
->driver_data
== adt7490
) {
1451 if ((data
->config4
& CONFIG4_PINFUNC
) == 0x1 &&
1452 !(config3
& CONFIG3_THERM
))
1455 if (id
->driver_data
== adt7476
|| id
->driver_data
== adt7490
) {
1456 if (!(config3
& CONFIG3_THERM
) ||
1457 (data
->config4
& CONFIG4_PINFUNC
) == 0x1)
1458 data
->has_voltage
|= (1 << 0); /* in0 */
1462 * On the ADT7476, the +12V input pin may instead be used as VID5,
1463 * and VID pins may alternatively be used as GPIO
1465 if (id
->driver_data
== adt7476
) {
1466 u8 vid
= adt7475_read(REG_VID
);
1467 if (!(vid
& VID_VIDSEL
))
1468 data
->has_voltage
|= (1 << 4); /* in4 */
1470 data
->has_vid
= !(adt7475_read(REG_CONFIG5
) & CONFIG5_VIDGPIO
);
1473 /* Voltage attenuators can be bypassed, globally or individually */
1474 config2
= adt7475_read(REG_CONFIG2
);
1475 if (config2
& CONFIG2_ATTN
) {
1476 data
->bypass_attn
= (0x3 << 3) | 0x3;
1478 data
->bypass_attn
= ((data
->config4
& CONFIG4_ATTN_IN10
) >> 4) |
1479 ((data
->config4
& CONFIG4_ATTN_IN43
) >> 3);
1481 data
->bypass_attn
&= data
->has_voltage
;
1484 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1485 * pwm's which are disabled to manual mode with 0% duty cycle
1487 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++)
1488 adt7475_read_pwm(client
, i
);
1490 /* Start monitoring */
1494 i2c_smbus_write_byte_data(client
, REG_CONFIG1
,
1495 adt7475_read(REG_CONFIG1
) | 0x01);
1501 ret
= sysfs_create_group(&client
->dev
.kobj
, &adt7475_attr_group
);
1505 /* Features that can be disabled individually */
1506 if (data
->has_fan4
) {
1507 ret
= sysfs_create_group(&client
->dev
.kobj
, &fan4_attr_group
);
1511 if (data
->has_pwm2
) {
1512 ret
= sysfs_create_group(&client
->dev
.kobj
, &pwm2_attr_group
);
1516 if (data
->has_voltage
& (1 << 0)) {
1517 ret
= sysfs_create_group(&client
->dev
.kobj
, &in0_attr_group
);
1521 if (data
->has_voltage
& (1 << 3)) {
1522 ret
= sysfs_create_group(&client
->dev
.kobj
, &in3_attr_group
);
1526 if (data
->has_voltage
& (1 << 4)) {
1527 ret
= sysfs_create_group(&client
->dev
.kobj
, &in4_attr_group
);
1531 if (data
->has_voltage
& (1 << 5)) {
1532 ret
= sysfs_create_group(&client
->dev
.kobj
, &in5_attr_group
);
1536 if (data
->has_vid
) {
1537 data
->vrm
= vid_which_vrm();
1538 ret
= sysfs_create_group(&client
->dev
.kobj
, &vid_attr_group
);
1543 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
1544 if (IS_ERR(data
->hwmon_dev
)) {
1545 ret
= PTR_ERR(data
->hwmon_dev
);
1549 dev_info(&client
->dev
, "%s device, revision %d\n",
1550 names
[id
->driver_data
], revision
);
1551 if ((data
->has_voltage
& 0x11) || data
->has_fan4
|| data
->has_pwm2
)
1552 dev_info(&client
->dev
, "Optional features:%s%s%s%s%s\n",
1553 (data
->has_voltage
& (1 << 0)) ? " in0" : "",
1554 (data
->has_voltage
& (1 << 4)) ? " in4" : "",
1555 data
->has_fan4
? " fan4" : "",
1556 data
->has_pwm2
? " pwm2" : "",
1557 data
->has_vid
? " vid" : "");
1558 if (data
->bypass_attn
)
1559 dev_info(&client
->dev
, "Bypassing attenuators on:%s%s%s%s\n",
1560 (data
->bypass_attn
& (1 << 0)) ? " in0" : "",
1561 (data
->bypass_attn
& (1 << 1)) ? " in1" : "",
1562 (data
->bypass_attn
& (1 << 3)) ? " in3" : "",
1563 (data
->bypass_attn
& (1 << 4)) ? " in4" : "");
1568 adt7475_remove_files(client
, data
);
1572 static int adt7475_remove(struct i2c_client
*client
)
1574 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1576 hwmon_device_unregister(data
->hwmon_dev
);
1577 adt7475_remove_files(client
, data
);
1582 static struct i2c_driver adt7475_driver
= {
1583 .class = I2C_CLASS_HWMON
,
1586 .of_match_table
= of_match_ptr(adt7475_of_match
),
1588 .probe
= adt7475_probe
,
1589 .remove
= adt7475_remove
,
1590 .id_table
= adt7475_id
,
1591 .detect
= adt7475_detect
,
1592 .address_list
= normal_i2c
,
1595 static void adt7475_read_hystersis(struct i2c_client
*client
)
1597 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1599 data
->temp
[HYSTERSIS
][0] = (u16
) adt7475_read(REG_REMOTE1_HYSTERSIS
);
1600 data
->temp
[HYSTERSIS
][1] = data
->temp
[HYSTERSIS
][0];
1601 data
->temp
[HYSTERSIS
][2] = (u16
) adt7475_read(REG_REMOTE2_HYSTERSIS
);
1604 static void adt7475_read_pwm(struct i2c_client
*client
, int index
)
1606 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1609 data
->pwm
[CONTROL
][index
] = adt7475_read(PWM_CONFIG_REG(index
));
1612 * Figure out the internal value for pwmctrl and pwmchan
1613 * based on the current settings
1615 v
= (data
->pwm
[CONTROL
][index
] >> 5) & 7;
1618 data
->pwmctl
[index
] = 0;
1620 data
->pwmctl
[index
] = 1;
1623 * The fan is disabled - we don't want to
1624 * support that, so change to manual mode and
1625 * set the duty cycle to 0 instead
1627 data
->pwm
[INPUT
][index
] = 0;
1628 data
->pwm
[CONTROL
][index
] &= ~0xE0;
1629 data
->pwm
[CONTROL
][index
] |= (7 << 5);
1631 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
1632 data
->pwm
[INPUT
][index
]);
1634 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
1635 data
->pwm
[CONTROL
][index
]);
1637 data
->pwmctl
[index
] = 1;
1639 data
->pwmctl
[index
] = 2;
1643 data
->pwmchan
[index
] = 1;
1646 data
->pwmchan
[index
] = 2;
1649 data
->pwmchan
[index
] = 4;
1652 data
->pwmchan
[index
] = 6;
1655 data
->pwmchan
[index
] = 7;
1661 static struct adt7475_data
*adt7475_update_device(struct device
*dev
)
1663 struct i2c_client
*client
= to_i2c_client(dev
);
1664 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1668 mutex_lock(&data
->lock
);
1670 /* Measurement values update every 2 seconds */
1671 if (time_after(jiffies
, data
->measure_updated
+ HZ
* 2) ||
1673 data
->alarms
= adt7475_read(REG_STATUS2
) << 8;
1674 data
->alarms
|= adt7475_read(REG_STATUS1
);
1676 ext
= (adt7475_read(REG_EXTEND2
) << 8) |
1677 adt7475_read(REG_EXTEND1
);
1678 for (i
= 0; i
< ADT7475_VOLTAGE_COUNT
; i
++) {
1679 if (!(data
->has_voltage
& (1 << i
)))
1681 data
->voltage
[INPUT
][i
] =
1682 (adt7475_read(VOLTAGE_REG(i
)) << 2) |
1683 ((ext
>> (i
* 2)) & 3);
1686 for (i
= 0; i
< ADT7475_TEMP_COUNT
; i
++)
1687 data
->temp
[INPUT
][i
] =
1688 (adt7475_read(TEMP_REG(i
)) << 2) |
1689 ((ext
>> ((i
+ 5) * 2)) & 3);
1691 if (data
->has_voltage
& (1 << 5)) {
1692 data
->alarms
|= adt7475_read(REG_STATUS4
) << 24;
1693 ext
= adt7475_read(REG_EXTEND3
);
1694 data
->voltage
[INPUT
][5] = adt7475_read(REG_VTT
) << 2 |
1698 for (i
= 0; i
< ADT7475_TACH_COUNT
; i
++) {
1699 if (i
== 3 && !data
->has_fan4
)
1701 data
->tach
[INPUT
][i
] =
1702 adt7475_read_word(client
, TACH_REG(i
));
1705 /* Updated by hw when in auto mode */
1706 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++) {
1707 if (i
== 1 && !data
->has_pwm2
)
1709 data
->pwm
[INPUT
][i
] = adt7475_read(PWM_REG(i
));
1713 data
->vid
= adt7475_read(REG_VID
) & 0x3f;
1715 data
->measure_updated
= jiffies
;
1718 /* Limits and settings, should never change update every 60 seconds */
1719 if (time_after(jiffies
, data
->limits_updated
+ HZ
* 60) ||
1721 data
->config4
= adt7475_read(REG_CONFIG4
);
1722 data
->config5
= adt7475_read(REG_CONFIG5
);
1724 for (i
= 0; i
< ADT7475_VOLTAGE_COUNT
; i
++) {
1725 if (!(data
->has_voltage
& (1 << i
)))
1727 /* Adjust values so they match the input precision */
1728 data
->voltage
[MIN
][i
] =
1729 adt7475_read(VOLTAGE_MIN_REG(i
)) << 2;
1730 data
->voltage
[MAX
][i
] =
1731 adt7475_read(VOLTAGE_MAX_REG(i
)) << 2;
1734 if (data
->has_voltage
& (1 << 5)) {
1735 data
->voltage
[MIN
][5] = adt7475_read(REG_VTT_MIN
) << 2;
1736 data
->voltage
[MAX
][5] = adt7475_read(REG_VTT_MAX
) << 2;
1739 for (i
= 0; i
< ADT7475_TEMP_COUNT
; i
++) {
1740 /* Adjust values so they match the input precision */
1741 data
->temp
[MIN
][i
] =
1742 adt7475_read(TEMP_MIN_REG(i
)) << 2;
1743 data
->temp
[MAX
][i
] =
1744 adt7475_read(TEMP_MAX_REG(i
)) << 2;
1745 data
->temp
[AUTOMIN
][i
] =
1746 adt7475_read(TEMP_TMIN_REG(i
)) << 2;
1747 data
->temp
[THERM
][i
] =
1748 adt7475_read(TEMP_THERM_REG(i
)) << 2;
1749 data
->temp
[OFFSET
][i
] =
1750 adt7475_read(TEMP_OFFSET_REG(i
));
1752 adt7475_read_hystersis(client
);
1754 for (i
= 0; i
< ADT7475_TACH_COUNT
; i
++) {
1755 if (i
== 3 && !data
->has_fan4
)
1757 data
->tach
[MIN
][i
] =
1758 adt7475_read_word(client
, TACH_MIN_REG(i
));
1761 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++) {
1762 if (i
== 1 && !data
->has_pwm2
)
1764 data
->pwm
[MAX
][i
] = adt7475_read(PWM_MAX_REG(i
));
1765 data
->pwm
[MIN
][i
] = adt7475_read(PWM_MIN_REG(i
));
1766 /* Set the channel and control information */
1767 adt7475_read_pwm(client
, i
);
1770 data
->range
[0] = adt7475_read(TEMP_TRANGE_REG(0));
1771 data
->range
[1] = adt7475_read(TEMP_TRANGE_REG(1));
1772 data
->range
[2] = adt7475_read(TEMP_TRANGE_REG(2));
1774 data
->limits_updated
= jiffies
;
1778 mutex_unlock(&data
->lock
);
1783 module_i2c_driver(adt7475_driver
);
1785 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1786 MODULE_DESCRIPTION("adt7475 driver");
1787 MODULE_LICENSE("GPL");