1 // SPDX-License-Identifier: GPL-2.0-only
3 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
4 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
5 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
7 * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
9 * Derived from the lm83 driver by Jean Delvare
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/hwmon-vid.h>
20 #include <linux/err.h>
21 #include <linux/jiffies.h>
22 #include <linux/util_macros.h>
24 /* Indexes for the sysfs hooks */
36 * These are unique identifiers for the sysfs functions - unlike the
37 * numbers above, these are not also indexes into an array
43 /* 7475 Common Registers */
45 #define REG_DEVREV2 0x12 /* ADT7490 only */
47 #define REG_VTT 0x1E /* ADT7490 only */
48 #define REG_EXTEND3 0x1F /* ADT7490 only */
50 #define REG_VOLTAGE_BASE 0x20
51 #define REG_TEMP_BASE 0x25
52 #define REG_TACH_BASE 0x28
53 #define REG_PWM_BASE 0x30
54 #define REG_PWM_MAX_BASE 0x38
56 #define REG_DEVID 0x3D
57 #define REG_VENDID 0x3E
58 #define REG_DEVID2 0x3F
60 #define REG_CONFIG1 0x40
62 #define REG_STATUS1 0x41
63 #define REG_STATUS2 0x42
65 #define REG_VID 0x43 /* ADT7476 only */
67 #define REG_VOLTAGE_MIN_BASE 0x44
68 #define REG_VOLTAGE_MAX_BASE 0x45
70 #define REG_TEMP_MIN_BASE 0x4E
71 #define REG_TEMP_MAX_BASE 0x4F
73 #define REG_TACH_MIN_BASE 0x54
75 #define REG_PWM_CONFIG_BASE 0x5C
77 #define REG_TEMP_TRANGE_BASE 0x5F
79 #define REG_ENHANCE_ACOUSTICS1 0x62
80 #define REG_ENHANCE_ACOUSTICS2 0x63
82 #define REG_PWM_MIN_BASE 0x64
84 #define REG_TEMP_TMIN_BASE 0x67
85 #define REG_TEMP_THERM_BASE 0x6A
87 #define REG_REMOTE1_HYSTERSIS 0x6D
88 #define REG_REMOTE2_HYSTERSIS 0x6E
90 #define REG_TEMP_OFFSET_BASE 0x70
92 #define REG_CONFIG2 0x73
94 #define REG_EXTEND1 0x76
95 #define REG_EXTEND2 0x77
97 #define REG_CONFIG3 0x78
98 #define REG_CONFIG5 0x7C
99 #define REG_CONFIG4 0x7D
101 #define REG_STATUS4 0x81 /* ADT7490 only */
103 #define REG_VTT_MIN 0x84 /* ADT7490 only */
104 #define REG_VTT_MAX 0x86 /* ADT7490 only */
106 #define VID_VIDSEL 0x80 /* ADT7476 only */
108 #define CONFIG2_ATTN 0x20
110 #define CONFIG3_SMBALERT 0x01
111 #define CONFIG3_THERM 0x02
113 #define CONFIG4_PINFUNC 0x03
114 #define CONFIG4_MAXDUTY 0x08
115 #define CONFIG4_ATTN_IN10 0x30
116 #define CONFIG4_ATTN_IN43 0xC0
118 #define CONFIG5_TWOSCOMP 0x01
119 #define CONFIG5_TEMPOFFSET 0x02
120 #define CONFIG5_VIDGPIO 0x10 /* ADT7476 only */
122 /* ADT7475 Settings */
124 #define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
125 #define ADT7475_TEMP_COUNT 3
126 #define ADT7475_TACH_COUNT 4
127 #define ADT7475_PWM_COUNT 3
129 /* Macro to read the registers */
131 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
133 /* Macros to easily index the registers */
135 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
136 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
138 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
139 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
140 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
141 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
143 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
144 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
145 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
147 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
148 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
149 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
150 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
151 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
152 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
153 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
155 static const unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
157 enum chips
{ adt7473
, adt7475
, adt7476
, adt7490
};
159 static const struct i2c_device_id adt7475_id
[] = {
160 { "adt7473", adt7473
},
161 { "adt7475", adt7475
},
162 { "adt7476", adt7476
},
163 { "adt7490", adt7490
},
166 MODULE_DEVICE_TABLE(i2c
, adt7475_id
);
168 static const struct of_device_id __maybe_unused adt7475_of_match
[] = {
170 .compatible
= "adi,adt7473",
171 .data
= (void *)adt7473
174 .compatible
= "adi,adt7475",
175 .data
= (void *)adt7475
178 .compatible
= "adi,adt7476",
179 .data
= (void *)adt7476
182 .compatible
= "adi,adt7490",
183 .data
= (void *)adt7490
187 MODULE_DEVICE_TABLE(of
, adt7475_of_match
);
189 struct adt7475_data
{
190 struct i2c_client
*client
;
193 unsigned long measure_updated
;
199 u8 bypass_attn
; /* Bypass voltage attenuator */
215 const struct attribute_group
*groups
[9];
218 static struct i2c_driver adt7475_driver
;
219 static struct adt7475_data
*adt7475_update_device(struct device
*dev
);
220 static void adt7475_read_hystersis(struct i2c_client
*client
);
221 static void adt7475_read_pwm(struct i2c_client
*client
, int index
);
223 /* Given a temp value, convert it to register value */
225 static inline u16
temp2reg(struct adt7475_data
*data
, long val
)
229 if (!(data
->config5
& CONFIG5_TWOSCOMP
)) {
230 val
= clamp_val(val
, -64000, 191000);
231 ret
= (val
+ 64500) / 1000;
233 val
= clamp_val(val
, -128000, 127000);
235 ret
= (256500 + val
) / 1000;
237 ret
= (val
+ 500) / 1000;
243 /* Given a register value, convert it to a real temp value */
245 static inline int reg2temp(struct adt7475_data
*data
, u16 reg
)
247 if (data
->config5
& CONFIG5_TWOSCOMP
) {
249 return (reg
- 1024) * 250;
253 return (reg
- 256) * 250;
256 static inline int tach2rpm(u16 tach
)
258 if (tach
== 0 || tach
== 0xFFFF)
261 return (90000 * 60) / tach
;
264 static inline u16
rpm2tach(unsigned long rpm
)
269 return clamp_val((90000 * 60) / rpm
, 1, 0xFFFF);
272 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
273 static const int adt7473_in_scaling
[ADT7475_VOLTAGE_COUNT
+ 1][2] = {
274 { 45, 94 }, /* +2.5V */
275 { 175, 525 }, /* Vccp */
276 { 68, 71 }, /* Vcc */
277 { 93, 47 }, /* +5V */
278 { 120, 20 }, /* +12V */
279 { 45, 45 }, /* Vtt */
282 static inline int reg2volt(int channel
, u16 reg
, u8 bypass_attn
)
284 const int *r
= adt7473_in_scaling
[channel
];
286 if (bypass_attn
& (1 << channel
))
287 return DIV_ROUND_CLOSEST(reg
* 2250, 1024);
288 return DIV_ROUND_CLOSEST(reg
* (r
[0] + r
[1]) * 2250, r
[1] * 1024);
291 static inline u16
volt2reg(int channel
, long volt
, u8 bypass_attn
)
293 const int *r
= adt7473_in_scaling
[channel
];
296 if (bypass_attn
& (1 << channel
))
297 reg
= (volt
* 1024) / 2250;
299 reg
= (volt
* r
[1] * 1024) / ((r
[0] + r
[1]) * 2250);
300 return clamp_val(reg
, 0, 1023) & (0xff << 2);
303 static int adt7475_read_word(struct i2c_client
*client
, int reg
)
307 val1
= i2c_smbus_read_byte_data(client
, reg
);
310 val2
= i2c_smbus_read_byte_data(client
, reg
+ 1);
314 return val1
| (val2
<< 8);
317 static void adt7475_write_word(struct i2c_client
*client
, int reg
, u16 val
)
319 i2c_smbus_write_byte_data(client
, reg
+ 1, val
>> 8);
320 i2c_smbus_write_byte_data(client
, reg
, val
& 0xFF);
323 static ssize_t
voltage_show(struct device
*dev
, struct device_attribute
*attr
,
326 struct adt7475_data
*data
= adt7475_update_device(dev
);
327 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
331 return PTR_ERR(data
);
335 return sprintf(buf
, "%d\n",
336 (data
->alarms
>> sattr
->index
) & 1);
338 val
= data
->voltage
[sattr
->nr
][sattr
->index
];
339 return sprintf(buf
, "%d\n",
340 reg2volt(sattr
->index
, val
, data
->bypass_attn
));
344 static ssize_t
voltage_store(struct device
*dev
,
345 struct device_attribute
*attr
, const char *buf
,
349 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
350 struct adt7475_data
*data
= dev_get_drvdata(dev
);
351 struct i2c_client
*client
= data
->client
;
355 if (kstrtol(buf
, 10, &val
))
358 mutex_lock(&data
->lock
);
360 data
->voltage
[sattr
->nr
][sattr
->index
] =
361 volt2reg(sattr
->index
, val
, data
->bypass_attn
);
363 if (sattr
->index
< ADT7475_VOLTAGE_COUNT
) {
364 if (sattr
->nr
== MIN
)
365 reg
= VOLTAGE_MIN_REG(sattr
->index
);
367 reg
= VOLTAGE_MAX_REG(sattr
->index
);
369 if (sattr
->nr
== MIN
)
375 i2c_smbus_write_byte_data(client
, reg
,
376 data
->voltage
[sattr
->nr
][sattr
->index
] >> 2);
377 mutex_unlock(&data
->lock
);
382 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*attr
,
385 struct adt7475_data
*data
= adt7475_update_device(dev
);
386 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
390 return PTR_ERR(data
);
394 mutex_lock(&data
->lock
);
395 out
= data
->temp
[sattr
->nr
][sattr
->index
];
396 if (sattr
->index
!= 1)
397 out
= (out
>> 4) & 0xF;
401 * Show the value as an absolute number tied to
404 out
= reg2temp(data
, data
->temp
[THERM
][sattr
->index
]) -
406 mutex_unlock(&data
->lock
);
411 * Offset is always 2's complement, regardless of the
414 mutex_lock(&data
->lock
);
415 out
= (s8
)data
->temp
[sattr
->nr
][sattr
->index
];
416 if (data
->config5
& CONFIG5_TEMPOFFSET
)
420 mutex_unlock(&data
->lock
);
424 out
= (data
->alarms
>> (sattr
->index
+ 4)) & 1;
428 /* Note - only for remote1 and remote2 */
429 out
= !!(data
->alarms
& (sattr
->index
? 0x8000 : 0x4000));
433 /* All other temp values are in the configured format */
434 out
= reg2temp(data
, data
->temp
[sattr
->nr
][sattr
->index
]);
437 return sprintf(buf
, "%d\n", out
);
440 static ssize_t
temp_store(struct device
*dev
, struct device_attribute
*attr
,
441 const char *buf
, size_t count
)
443 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
444 struct adt7475_data
*data
= dev_get_drvdata(dev
);
445 struct i2c_client
*client
= data
->client
;
446 unsigned char reg
= 0;
451 if (kstrtol(buf
, 10, &val
))
454 mutex_lock(&data
->lock
);
456 /* We need the config register in all cases for temp <-> reg conv. */
457 data
->config5
= adt7475_read(REG_CONFIG5
);
461 if (data
->config5
& CONFIG5_TEMPOFFSET
) {
462 val
= clamp_val(val
, -63000, 127000);
463 out
= data
->temp
[OFFSET
][sattr
->index
] = val
/ 1000;
465 val
= clamp_val(val
, -63000, 64000);
466 out
= data
->temp
[OFFSET
][sattr
->index
] = val
/ 500;
472 * The value will be given as an absolute value, turn it
473 * into an offset based on THERM
476 /* Read fresh THERM and HYSTERSIS values from the chip */
477 data
->temp
[THERM
][sattr
->index
] =
478 adt7475_read(TEMP_THERM_REG(sattr
->index
)) << 2;
479 adt7475_read_hystersis(client
);
481 temp
= reg2temp(data
, data
->temp
[THERM
][sattr
->index
]);
482 val
= clamp_val(val
, temp
- 15000, temp
);
483 val
= (temp
- val
) / 1000;
485 if (sattr
->index
!= 1) {
486 data
->temp
[HYSTERSIS
][sattr
->index
] &= 0xF0;
487 data
->temp
[HYSTERSIS
][sattr
->index
] |= (val
& 0xF) << 4;
489 data
->temp
[HYSTERSIS
][sattr
->index
] &= 0x0F;
490 data
->temp
[HYSTERSIS
][sattr
->index
] |= (val
& 0xF);
493 out
= data
->temp
[HYSTERSIS
][sattr
->index
];
497 data
->temp
[sattr
->nr
][sattr
->index
] = temp2reg(data
, val
);
500 * We maintain an extra 2 digits of precision for simplicity
501 * - shift those back off before writing the value
503 out
= (u8
) (data
->temp
[sattr
->nr
][sattr
->index
] >> 2);
508 reg
= TEMP_MIN_REG(sattr
->index
);
511 reg
= TEMP_MAX_REG(sattr
->index
);
514 reg
= TEMP_OFFSET_REG(sattr
->index
);
517 reg
= TEMP_TMIN_REG(sattr
->index
);
520 reg
= TEMP_THERM_REG(sattr
->index
);
523 if (sattr
->index
!= 2)
524 reg
= REG_REMOTE1_HYSTERSIS
;
526 reg
= REG_REMOTE2_HYSTERSIS
;
531 i2c_smbus_write_byte_data(client
, reg
, out
);
533 mutex_unlock(&data
->lock
);
537 /* Assuming CONFIG6[SLOW] is 0 */
538 static const int ad7475_st_map
[] = {
539 37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
542 static ssize_t
temp_st_show(struct device
*dev
, struct device_attribute
*attr
,
545 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
546 struct adt7475_data
*data
= dev_get_drvdata(dev
);
549 switch (sattr
->index
) {
551 val
= data
->enh_acoustics
[0] & 0xf;
554 val
= (data
->enh_acoustics
[1] >> 4) & 0xf;
558 val
= data
->enh_acoustics
[1] & 0xf;
563 return sprintf(buf
, "%d\n", ad7475_st_map
[val
& 0x7]);
565 return sprintf(buf
, "0\n");
568 static ssize_t
temp_st_store(struct device
*dev
,
569 struct device_attribute
*attr
, const char *buf
,
572 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
573 struct adt7475_data
*data
= dev_get_drvdata(dev
);
574 struct i2c_client
*client
= data
->client
;
579 if (kstrtoul(buf
, 10, &val
))
582 switch (sattr
->index
) {
584 reg
= REG_ENHANCE_ACOUSTICS1
;
589 reg
= REG_ENHANCE_ACOUSTICS2
;
595 reg
= REG_ENHANCE_ACOUSTICS2
;
602 val
= find_closest_descending(val
, ad7475_st_map
,
603 ARRAY_SIZE(ad7475_st_map
));
607 mutex_lock(&data
->lock
);
609 data
->enh_acoustics
[idx
] &= ~(0xf << shift
);
610 data
->enh_acoustics
[idx
] |= (val
<< shift
);
612 i2c_smbus_write_byte_data(client
, reg
, data
->enh_acoustics
[idx
]);
614 mutex_unlock(&data
->lock
);
620 * Table of autorange values - the user will write the value in millidegrees,
621 * and we'll convert it
623 static const int autorange_table
[] = {
624 2000, 2500, 3330, 4000, 5000, 6670, 8000,
625 10000, 13330, 16000, 20000, 26670, 32000, 40000,
629 static ssize_t
point2_show(struct device
*dev
, struct device_attribute
*attr
,
632 struct adt7475_data
*data
= adt7475_update_device(dev
);
633 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
637 return PTR_ERR(data
);
639 mutex_lock(&data
->lock
);
640 out
= (data
->range
[sattr
->index
] >> 4) & 0x0F;
641 val
= reg2temp(data
, data
->temp
[AUTOMIN
][sattr
->index
]);
642 mutex_unlock(&data
->lock
);
644 return sprintf(buf
, "%d\n", val
+ autorange_table
[out
]);
647 static ssize_t
point2_store(struct device
*dev
, struct device_attribute
*attr
,
648 const char *buf
, size_t count
)
650 struct adt7475_data
*data
= dev_get_drvdata(dev
);
651 struct i2c_client
*client
= data
->client
;
652 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
656 if (kstrtol(buf
, 10, &val
))
659 mutex_lock(&data
->lock
);
661 /* Get a fresh copy of the needed registers */
662 data
->config5
= adt7475_read(REG_CONFIG5
);
663 data
->temp
[AUTOMIN
][sattr
->index
] =
664 adt7475_read(TEMP_TMIN_REG(sattr
->index
)) << 2;
665 data
->range
[sattr
->index
] =
666 adt7475_read(TEMP_TRANGE_REG(sattr
->index
));
669 * The user will write an absolute value, so subtract the start point
670 * to figure the range
672 temp
= reg2temp(data
, data
->temp
[AUTOMIN
][sattr
->index
]);
673 val
= clamp_val(val
, temp
+ autorange_table
[0],
674 temp
+ autorange_table
[ARRAY_SIZE(autorange_table
) - 1]);
677 /* Find the nearest table entry to what the user wrote */
678 val
= find_closest(val
, autorange_table
, ARRAY_SIZE(autorange_table
));
680 data
->range
[sattr
->index
] &= ~0xF0;
681 data
->range
[sattr
->index
] |= val
<< 4;
683 i2c_smbus_write_byte_data(client
, TEMP_TRANGE_REG(sattr
->index
),
684 data
->range
[sattr
->index
]);
686 mutex_unlock(&data
->lock
);
690 static ssize_t
tach_show(struct device
*dev
, struct device_attribute
*attr
,
693 struct adt7475_data
*data
= adt7475_update_device(dev
);
694 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
698 return PTR_ERR(data
);
700 if (sattr
->nr
== ALARM
)
701 out
= (data
->alarms
>> (sattr
->index
+ 10)) & 1;
703 out
= tach2rpm(data
->tach
[sattr
->nr
][sattr
->index
]);
705 return sprintf(buf
, "%d\n", out
);
708 static ssize_t
tach_store(struct device
*dev
, struct device_attribute
*attr
,
709 const char *buf
, size_t count
)
712 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
713 struct adt7475_data
*data
= dev_get_drvdata(dev
);
714 struct i2c_client
*client
= data
->client
;
717 if (kstrtoul(buf
, 10, &val
))
720 mutex_lock(&data
->lock
);
722 data
->tach
[MIN
][sattr
->index
] = rpm2tach(val
);
724 adt7475_write_word(client
, TACH_MIN_REG(sattr
->index
),
725 data
->tach
[MIN
][sattr
->index
]);
727 mutex_unlock(&data
->lock
);
731 static ssize_t
pwm_show(struct device
*dev
, struct device_attribute
*attr
,
734 struct adt7475_data
*data
= adt7475_update_device(dev
);
735 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
738 return PTR_ERR(data
);
740 return sprintf(buf
, "%d\n", data
->pwm
[sattr
->nr
][sattr
->index
]);
743 static ssize_t
pwmchan_show(struct device
*dev
, struct device_attribute
*attr
,
746 struct adt7475_data
*data
= adt7475_update_device(dev
);
747 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
750 return PTR_ERR(data
);
752 return sprintf(buf
, "%d\n", data
->pwmchan
[sattr
->index
]);
755 static ssize_t
pwmctrl_show(struct device
*dev
, struct device_attribute
*attr
,
758 struct adt7475_data
*data
= adt7475_update_device(dev
);
759 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
762 return PTR_ERR(data
);
764 return sprintf(buf
, "%d\n", data
->pwmctl
[sattr
->index
]);
767 static ssize_t
pwm_store(struct device
*dev
, struct device_attribute
*attr
,
768 const char *buf
, size_t count
)
771 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
772 struct adt7475_data
*data
= dev_get_drvdata(dev
);
773 struct i2c_client
*client
= data
->client
;
774 unsigned char reg
= 0;
777 if (kstrtol(buf
, 10, &val
))
780 mutex_lock(&data
->lock
);
784 /* Get a fresh value for CONTROL */
785 data
->pwm
[CONTROL
][sattr
->index
] =
786 adt7475_read(PWM_CONFIG_REG(sattr
->index
));
789 * If we are not in manual mode, then we shouldn't allow
790 * the user to set the pwm speed
792 if (((data
->pwm
[CONTROL
][sattr
->index
] >> 5) & 7) != 7) {
793 mutex_unlock(&data
->lock
);
797 reg
= PWM_REG(sattr
->index
);
801 reg
= PWM_MIN_REG(sattr
->index
);
805 reg
= PWM_MAX_REG(sattr
->index
);
809 data
->pwm
[sattr
->nr
][sattr
->index
] = clamp_val(val
, 0, 0xFF);
810 i2c_smbus_write_byte_data(client
, reg
,
811 data
->pwm
[sattr
->nr
][sattr
->index
]);
812 mutex_unlock(&data
->lock
);
817 static ssize_t
stall_disable_show(struct device
*dev
,
818 struct device_attribute
*attr
, char *buf
)
820 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
821 struct adt7475_data
*data
= dev_get_drvdata(dev
);
823 u8 mask
= BIT(5 + sattr
->index
);
825 return sprintf(buf
, "%d\n", !!(data
->enh_acoustics
[0] & mask
));
828 static ssize_t
stall_disable_store(struct device
*dev
,
829 struct device_attribute
*attr
,
830 const char *buf
, size_t count
)
832 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
833 struct adt7475_data
*data
= dev_get_drvdata(dev
);
834 struct i2c_client
*client
= data
->client
;
836 u8 mask
= BIT(5 + sattr
->index
);
838 if (kstrtol(buf
, 10, &val
))
841 mutex_lock(&data
->lock
);
843 data
->enh_acoustics
[0] &= ~mask
;
845 data
->enh_acoustics
[0] |= mask
;
847 i2c_smbus_write_byte_data(client
, REG_ENHANCE_ACOUSTICS1
,
848 data
->enh_acoustics
[0]);
850 mutex_unlock(&data
->lock
);
855 /* Called by set_pwmctrl and set_pwmchan */
857 static int hw_set_pwm(struct i2c_client
*client
, int index
,
858 unsigned int pwmctl
, unsigned int pwmchan
)
860 struct adt7475_data
*data
= i2c_get_clientdata(client
);
865 val
= 0x03; /* Run at full speed */
868 val
= 0x07; /* Manual mode */
873 /* Remote1 controls PWM */
877 /* local controls PWM */
881 /* remote2 controls PWM */
885 /* local/remote2 control PWM */
889 /* All three control PWM */
900 data
->pwmctl
[index
] = pwmctl
;
901 data
->pwmchan
[index
] = pwmchan
;
903 data
->pwm
[CONTROL
][index
] &= ~0xE0;
904 data
->pwm
[CONTROL
][index
] |= (val
& 7) << 5;
906 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
907 data
->pwm
[CONTROL
][index
]);
912 static ssize_t
pwmchan_store(struct device
*dev
,
913 struct device_attribute
*attr
, const char *buf
,
916 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
917 struct adt7475_data
*data
= dev_get_drvdata(dev
);
918 struct i2c_client
*client
= data
->client
;
922 if (kstrtol(buf
, 10, &val
))
925 mutex_lock(&data
->lock
);
926 /* Read Modify Write PWM values */
927 adt7475_read_pwm(client
, sattr
->index
);
928 r
= hw_set_pwm(client
, sattr
->index
, data
->pwmctl
[sattr
->index
], val
);
931 mutex_unlock(&data
->lock
);
936 static ssize_t
pwmctrl_store(struct device
*dev
,
937 struct device_attribute
*attr
, const char *buf
,
940 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
941 struct adt7475_data
*data
= dev_get_drvdata(dev
);
942 struct i2c_client
*client
= data
->client
;
946 if (kstrtol(buf
, 10, &val
))
949 mutex_lock(&data
->lock
);
950 /* Read Modify Write PWM values */
951 adt7475_read_pwm(client
, sattr
->index
);
952 r
= hw_set_pwm(client
, sattr
->index
, val
, data
->pwmchan
[sattr
->index
]);
955 mutex_unlock(&data
->lock
);
960 /* List of frequencies for the PWM */
961 static const int pwmfreq_table
[] = {
962 11, 14, 22, 29, 35, 44, 58, 88, 22500
965 static ssize_t
pwmfreq_show(struct device
*dev
, struct device_attribute
*attr
,
968 struct adt7475_data
*data
= adt7475_update_device(dev
);
969 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
973 return PTR_ERR(data
);
974 idx
= clamp_val(data
->range
[sattr
->index
] & 0xf, 0,
975 ARRAY_SIZE(pwmfreq_table
) - 1);
977 return sprintf(buf
, "%d\n", pwmfreq_table
[idx
]);
980 static ssize_t
pwmfreq_store(struct device
*dev
,
981 struct device_attribute
*attr
, const char *buf
,
984 struct sensor_device_attribute_2
*sattr
= to_sensor_dev_attr_2(attr
);
985 struct adt7475_data
*data
= dev_get_drvdata(dev
);
986 struct i2c_client
*client
= data
->client
;
990 if (kstrtol(buf
, 10, &val
))
993 out
= find_closest(val
, pwmfreq_table
, ARRAY_SIZE(pwmfreq_table
));
995 mutex_lock(&data
->lock
);
997 data
->range
[sattr
->index
] =
998 adt7475_read(TEMP_TRANGE_REG(sattr
->index
));
999 data
->range
[sattr
->index
] &= ~0xf;
1000 data
->range
[sattr
->index
] |= out
;
1002 i2c_smbus_write_byte_data(client
, TEMP_TRANGE_REG(sattr
->index
),
1003 data
->range
[sattr
->index
]);
1005 mutex_unlock(&data
->lock
);
1009 static ssize_t
pwm_use_point2_pwm_at_crit_show(struct device
*dev
,
1010 struct device_attribute
*devattr
,
1013 struct adt7475_data
*data
= adt7475_update_device(dev
);
1016 return PTR_ERR(data
);
1018 return sprintf(buf
, "%d\n", !!(data
->config4
& CONFIG4_MAXDUTY
));
1021 static ssize_t
pwm_use_point2_pwm_at_crit_store(struct device
*dev
,
1022 struct device_attribute
*devattr
,
1023 const char *buf
, size_t count
)
1025 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1026 struct i2c_client
*client
= data
->client
;
1029 if (kstrtol(buf
, 10, &val
))
1031 if (val
!= 0 && val
!= 1)
1034 mutex_lock(&data
->lock
);
1035 data
->config4
= i2c_smbus_read_byte_data(client
, REG_CONFIG4
);
1037 data
->config4
|= CONFIG4_MAXDUTY
;
1039 data
->config4
&= ~CONFIG4_MAXDUTY
;
1040 i2c_smbus_write_byte_data(client
, REG_CONFIG4
, data
->config4
);
1041 mutex_unlock(&data
->lock
);
1046 static ssize_t
vrm_show(struct device
*dev
, struct device_attribute
*devattr
,
1049 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1050 return sprintf(buf
, "%d\n", (int)data
->vrm
);
1053 static ssize_t
vrm_store(struct device
*dev
, struct device_attribute
*devattr
,
1054 const char *buf
, size_t count
)
1056 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1059 if (kstrtol(buf
, 10, &val
))
1061 if (val
< 0 || val
> 255)
1068 static ssize_t
cpu0_vid_show(struct device
*dev
,
1069 struct device_attribute
*devattr
, char *buf
)
1071 struct adt7475_data
*data
= adt7475_update_device(dev
);
1074 return PTR_ERR(data
);
1076 return sprintf(buf
, "%d\n", vid_from_reg(data
->vid
, data
->vrm
));
1079 static SENSOR_DEVICE_ATTR_2_RO(in0_input
, voltage
, INPUT
, 0);
1080 static SENSOR_DEVICE_ATTR_2_RW(in0_max
, voltage
, MAX
, 0);
1081 static SENSOR_DEVICE_ATTR_2_RW(in0_min
, voltage
, MIN
, 0);
1082 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm
, voltage
, ALARM
, 0);
1083 static SENSOR_DEVICE_ATTR_2_RO(in1_input
, voltage
, INPUT
, 1);
1084 static SENSOR_DEVICE_ATTR_2_RW(in1_max
, voltage
, MAX
, 1);
1085 static SENSOR_DEVICE_ATTR_2_RW(in1_min
, voltage
, MIN
, 1);
1086 static SENSOR_DEVICE_ATTR_2_RO(in1_alarm
, voltage
, ALARM
, 1);
1087 static SENSOR_DEVICE_ATTR_2_RO(in2_input
, voltage
, INPUT
, 2);
1088 static SENSOR_DEVICE_ATTR_2_RW(in2_max
, voltage
, MAX
, 2);
1089 static SENSOR_DEVICE_ATTR_2_RW(in2_min
, voltage
, MIN
, 2);
1090 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm
, voltage
, ALARM
, 2);
1091 static SENSOR_DEVICE_ATTR_2_RO(in3_input
, voltage
, INPUT
, 3);
1092 static SENSOR_DEVICE_ATTR_2_RW(in3_max
, voltage
, MAX
, 3);
1093 static SENSOR_DEVICE_ATTR_2_RW(in3_min
, voltage
, MIN
, 3);
1094 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm
, voltage
, ALARM
, 3);
1095 static SENSOR_DEVICE_ATTR_2_RO(in4_input
, voltage
, INPUT
, 4);
1096 static SENSOR_DEVICE_ATTR_2_RW(in4_max
, voltage
, MAX
, 4);
1097 static SENSOR_DEVICE_ATTR_2_RW(in4_min
, voltage
, MIN
, 4);
1098 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm
, voltage
, ALARM
, 8);
1099 static SENSOR_DEVICE_ATTR_2_RO(in5_input
, voltage
, INPUT
, 5);
1100 static SENSOR_DEVICE_ATTR_2_RW(in5_max
, voltage
, MAX
, 5);
1101 static SENSOR_DEVICE_ATTR_2_RW(in5_min
, voltage
, MIN
, 5);
1102 static SENSOR_DEVICE_ATTR_2_RO(in5_alarm
, voltage
, ALARM
, 31);
1103 static SENSOR_DEVICE_ATTR_2_RO(temp1_input
, temp
, INPUT
, 0);
1104 static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm
, temp
, ALARM
, 0);
1105 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault
, temp
, FAULT
, 0);
1106 static SENSOR_DEVICE_ATTR_2_RW(temp1_max
, temp
, MAX
, 0);
1107 static SENSOR_DEVICE_ATTR_2_RW(temp1_min
, temp
, MIN
, 0);
1108 static SENSOR_DEVICE_ATTR_2_RW(temp1_offset
, temp
, OFFSET
, 0);
1109 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp
, temp
, AUTOMIN
, 0);
1110 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp
, point2
, 0, 0);
1111 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit
, temp
, THERM
, 0);
1112 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst
, temp
, HYSTERSIS
, 0);
1113 static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing
, temp_st
, 0, 0);
1114 static SENSOR_DEVICE_ATTR_2_RO(temp2_input
, temp
, INPUT
, 1);
1115 static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm
, temp
, ALARM
, 1);
1116 static SENSOR_DEVICE_ATTR_2_RW(temp2_max
, temp
, MAX
, 1);
1117 static SENSOR_DEVICE_ATTR_2_RW(temp2_min
, temp
, MIN
, 1);
1118 static SENSOR_DEVICE_ATTR_2_RW(temp2_offset
, temp
, OFFSET
, 1);
1119 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp
, temp
, AUTOMIN
, 1);
1120 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp
, point2
, 0, 1);
1121 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit
, temp
, THERM
, 1);
1122 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst
, temp
, HYSTERSIS
, 1);
1123 static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing
, temp_st
, 0, 1);
1124 static SENSOR_DEVICE_ATTR_2_RO(temp3_input
, temp
, INPUT
, 2);
1125 static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm
, temp
, ALARM
, 2);
1126 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault
, temp
, FAULT
, 2);
1127 static SENSOR_DEVICE_ATTR_2_RW(temp3_max
, temp
, MAX
, 2);
1128 static SENSOR_DEVICE_ATTR_2_RW(temp3_min
, temp
, MIN
, 2);
1129 static SENSOR_DEVICE_ATTR_2_RW(temp3_offset
, temp
, OFFSET
, 2);
1130 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp
, temp
, AUTOMIN
, 2);
1131 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp
, point2
, 0, 2);
1132 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit
, temp
, THERM
, 2);
1133 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst
, temp
, HYSTERSIS
, 2);
1134 static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing
, temp_st
, 0, 2);
1135 static SENSOR_DEVICE_ATTR_2_RO(fan1_input
, tach
, INPUT
, 0);
1136 static SENSOR_DEVICE_ATTR_2_RW(fan1_min
, tach
, MIN
, 0);
1137 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm
, tach
, ALARM
, 0);
1138 static SENSOR_DEVICE_ATTR_2_RO(fan2_input
, tach
, INPUT
, 1);
1139 static SENSOR_DEVICE_ATTR_2_RW(fan2_min
, tach
, MIN
, 1);
1140 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm
, tach
, ALARM
, 1);
1141 static SENSOR_DEVICE_ATTR_2_RO(fan3_input
, tach
, INPUT
, 2);
1142 static SENSOR_DEVICE_ATTR_2_RW(fan3_min
, tach
, MIN
, 2);
1143 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm
, tach
, ALARM
, 2);
1144 static SENSOR_DEVICE_ATTR_2_RO(fan4_input
, tach
, INPUT
, 3);
1145 static SENSOR_DEVICE_ATTR_2_RW(fan4_min
, tach
, MIN
, 3);
1146 static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm
, tach
, ALARM
, 3);
1147 static SENSOR_DEVICE_ATTR_2_RW(pwm1
, pwm
, INPUT
, 0);
1148 static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq
, pwmfreq
, INPUT
, 0);
1149 static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable
, pwmctrl
, INPUT
, 0);
1150 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp
, pwmchan
, INPUT
, 0);
1151 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm
, pwm
, MIN
, 0);
1152 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm
, pwm
, MAX
, 0);
1153 static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable
, stall_disable
, 0, 0);
1154 static SENSOR_DEVICE_ATTR_2_RW(pwm2
, pwm
, INPUT
, 1);
1155 static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq
, pwmfreq
, INPUT
, 1);
1156 static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable
, pwmctrl
, INPUT
, 1);
1157 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp
, pwmchan
, INPUT
, 1);
1158 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm
, pwm
, MIN
, 1);
1159 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm
, pwm
, MAX
, 1);
1160 static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable
, stall_disable
, 0, 1);
1161 static SENSOR_DEVICE_ATTR_2_RW(pwm3
, pwm
, INPUT
, 2);
1162 static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq
, pwmfreq
, INPUT
, 2);
1163 static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable
, pwmctrl
, INPUT
, 2);
1164 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp
, pwmchan
, INPUT
, 2);
1165 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm
, pwm
, MIN
, 2);
1166 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm
, pwm
, MAX
, 2);
1167 static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable
, stall_disable
, 0, 2);
1169 /* Non-standard name, might need revisiting */
1170 static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit
);
1172 static DEVICE_ATTR_RW(vrm
);
1173 static DEVICE_ATTR_RO(cpu0_vid
);
1175 static struct attribute
*adt7475_attrs
[] = {
1176 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1177 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1178 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1179 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1180 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1181 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1182 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1183 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1184 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1185 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1186 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
1187 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1188 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1189 &sensor_dev_attr_temp1_offset
.dev_attr
.attr
,
1190 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
1191 &sensor_dev_attr_temp1_auto_point2_temp
.dev_attr
.attr
,
1192 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
1193 &sensor_dev_attr_temp1_crit_hyst
.dev_attr
.attr
,
1194 &sensor_dev_attr_temp1_smoothing
.dev_attr
.attr
,
1195 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1196 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1197 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1198 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1199 &sensor_dev_attr_temp2_offset
.dev_attr
.attr
,
1200 &sensor_dev_attr_temp2_auto_point1_temp
.dev_attr
.attr
,
1201 &sensor_dev_attr_temp2_auto_point2_temp
.dev_attr
.attr
,
1202 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
1203 &sensor_dev_attr_temp2_crit_hyst
.dev_attr
.attr
,
1204 &sensor_dev_attr_temp2_smoothing
.dev_attr
.attr
,
1205 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1206 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
1207 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1208 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1209 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1210 &sensor_dev_attr_temp3_offset
.dev_attr
.attr
,
1211 &sensor_dev_attr_temp3_auto_point1_temp
.dev_attr
.attr
,
1212 &sensor_dev_attr_temp3_auto_point2_temp
.dev_attr
.attr
,
1213 &sensor_dev_attr_temp3_crit
.dev_attr
.attr
,
1214 &sensor_dev_attr_temp3_crit_hyst
.dev_attr
.attr
,
1215 &sensor_dev_attr_temp3_smoothing
.dev_attr
.attr
,
1216 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1217 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1218 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1219 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1220 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1221 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1222 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1223 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1224 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1225 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1226 &sensor_dev_attr_pwm1_freq
.dev_attr
.attr
,
1227 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1228 &sensor_dev_attr_pwm1_auto_channels_temp
.dev_attr
.attr
,
1229 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
1230 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
1231 &sensor_dev_attr_pwm1_stall_disable
.dev_attr
.attr
,
1232 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1233 &sensor_dev_attr_pwm3_freq
.dev_attr
.attr
,
1234 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1235 &sensor_dev_attr_pwm3_auto_channels_temp
.dev_attr
.attr
,
1236 &sensor_dev_attr_pwm3_auto_point1_pwm
.dev_attr
.attr
,
1237 &sensor_dev_attr_pwm3_auto_point2_pwm
.dev_attr
.attr
,
1238 &sensor_dev_attr_pwm3_stall_disable
.dev_attr
.attr
,
1239 &dev_attr_pwm_use_point2_pwm_at_crit
.attr
,
1243 static struct attribute
*fan4_attrs
[] = {
1244 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
1245 &sensor_dev_attr_fan4_min
.dev_attr
.attr
,
1246 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1250 static struct attribute
*pwm2_attrs
[] = {
1251 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1252 &sensor_dev_attr_pwm2_freq
.dev_attr
.attr
,
1253 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1254 &sensor_dev_attr_pwm2_auto_channels_temp
.dev_attr
.attr
,
1255 &sensor_dev_attr_pwm2_auto_point1_pwm
.dev_attr
.attr
,
1256 &sensor_dev_attr_pwm2_auto_point2_pwm
.dev_attr
.attr
,
1257 &sensor_dev_attr_pwm2_stall_disable
.dev_attr
.attr
,
1261 static struct attribute
*in0_attrs
[] = {
1262 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1263 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1264 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1265 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1269 static struct attribute
*in3_attrs
[] = {
1270 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1271 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1272 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1273 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1277 static struct attribute
*in4_attrs
[] = {
1278 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1279 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1280 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1281 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1285 static struct attribute
*in5_attrs
[] = {
1286 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1287 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1288 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1289 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1293 static struct attribute
*vid_attrs
[] = {
1294 &dev_attr_cpu0_vid
.attr
,
1299 static const struct attribute_group adt7475_attr_group
= { .attrs
= adt7475_attrs
};
1300 static const struct attribute_group fan4_attr_group
= { .attrs
= fan4_attrs
};
1301 static const struct attribute_group pwm2_attr_group
= { .attrs
= pwm2_attrs
};
1302 static const struct attribute_group in0_attr_group
= { .attrs
= in0_attrs
};
1303 static const struct attribute_group in3_attr_group
= { .attrs
= in3_attrs
};
1304 static const struct attribute_group in4_attr_group
= { .attrs
= in4_attrs
};
1305 static const struct attribute_group in5_attr_group
= { .attrs
= in5_attrs
};
1306 static const struct attribute_group vid_attr_group
= { .attrs
= vid_attrs
};
1308 static int adt7475_detect(struct i2c_client
*client
,
1309 struct i2c_board_info
*info
)
1311 struct i2c_adapter
*adapter
= client
->adapter
;
1312 int vendid
, devid
, devid2
;
1315 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1318 vendid
= adt7475_read(REG_VENDID
);
1319 devid2
= adt7475_read(REG_DEVID2
);
1320 if (vendid
!= 0x41 || /* Analog Devices */
1321 (devid2
& 0xf8) != 0x68)
1324 devid
= adt7475_read(REG_DEVID
);
1327 else if (devid
== 0x75 && client
->addr
== 0x2e)
1329 else if (devid
== 0x76)
1331 else if ((devid2
& 0xfc) == 0x6c)
1334 dev_dbg(&adapter
->dev
,
1335 "Couldn't detect an ADT7473/75/76/90 part at "
1336 "0x%02x\n", (unsigned int)client
->addr
);
1340 strlcpy(info
->type
, name
, I2C_NAME_SIZE
);
1345 static int adt7475_update_limits(struct i2c_client
*client
)
1347 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1351 ret
= adt7475_read(REG_CONFIG4
);
1354 data
->config4
= ret
;
1356 ret
= adt7475_read(REG_CONFIG5
);
1359 data
->config5
= ret
;
1361 for (i
= 0; i
< ADT7475_VOLTAGE_COUNT
; i
++) {
1362 if (!(data
->has_voltage
& (1 << i
)))
1364 /* Adjust values so they match the input precision */
1365 ret
= adt7475_read(VOLTAGE_MIN_REG(i
));
1368 data
->voltage
[MIN
][i
] = ret
<< 2;
1370 ret
= adt7475_read(VOLTAGE_MAX_REG(i
));
1373 data
->voltage
[MAX
][i
] = ret
<< 2;
1376 if (data
->has_voltage
& (1 << 5)) {
1377 ret
= adt7475_read(REG_VTT_MIN
);
1380 data
->voltage
[MIN
][5] = ret
<< 2;
1382 ret
= adt7475_read(REG_VTT_MAX
);
1385 data
->voltage
[MAX
][5] = ret
<< 2;
1388 for (i
= 0; i
< ADT7475_TEMP_COUNT
; i
++) {
1389 /* Adjust values so they match the input precision */
1390 ret
= adt7475_read(TEMP_MIN_REG(i
));
1393 data
->temp
[MIN
][i
] = ret
<< 2;
1395 ret
= adt7475_read(TEMP_MAX_REG(i
));
1398 data
->temp
[MAX
][i
] = ret
<< 2;
1400 ret
= adt7475_read(TEMP_TMIN_REG(i
));
1403 data
->temp
[AUTOMIN
][i
] = ret
<< 2;
1405 ret
= adt7475_read(TEMP_THERM_REG(i
));
1408 data
->temp
[THERM
][i
] = ret
<< 2;
1410 ret
= adt7475_read(TEMP_OFFSET_REG(i
));
1413 data
->temp
[OFFSET
][i
] = ret
;
1415 adt7475_read_hystersis(client
);
1417 for (i
= 0; i
< ADT7475_TACH_COUNT
; i
++) {
1418 if (i
== 3 && !data
->has_fan4
)
1420 ret
= adt7475_read_word(client
, TACH_MIN_REG(i
));
1423 data
->tach
[MIN
][i
] = ret
;
1426 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++) {
1427 if (i
== 1 && !data
->has_pwm2
)
1429 ret
= adt7475_read(PWM_MAX_REG(i
));
1432 data
->pwm
[MAX
][i
] = ret
;
1434 ret
= adt7475_read(PWM_MIN_REG(i
));
1437 data
->pwm
[MIN
][i
] = ret
;
1438 /* Set the channel and control information */
1439 adt7475_read_pwm(client
, i
);
1442 ret
= adt7475_read(TEMP_TRANGE_REG(0));
1445 data
->range
[0] = ret
;
1447 ret
= adt7475_read(TEMP_TRANGE_REG(1));
1450 data
->range
[1] = ret
;
1452 ret
= adt7475_read(TEMP_TRANGE_REG(2));
1455 data
->range
[2] = ret
;
1460 static int adt7475_probe(struct i2c_client
*client
,
1461 const struct i2c_device_id
*id
)
1464 static const char * const names
[] = {
1465 [adt7473
] = "ADT7473",
1466 [adt7475
] = "ADT7475",
1467 [adt7476
] = "ADT7476",
1468 [adt7490
] = "ADT7490",
1471 struct adt7475_data
*data
;
1472 struct device
*hwmon_dev
;
1473 int i
, ret
= 0, revision
, group_num
= 0;
1474 u8 config2
, config3
;
1476 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
1480 mutex_init(&data
->lock
);
1481 data
->client
= client
;
1482 i2c_set_clientdata(client
, data
);
1484 if (client
->dev
.of_node
)
1485 chip
= (enum chips
)of_device_get_match_data(&client
->dev
);
1487 chip
= id
->driver_data
;
1489 /* Initialize device-specific values */
1492 data
->has_voltage
= 0x0e; /* in1 to in3 */
1493 revision
= adt7475_read(REG_DEVID2
) & 0x07;
1496 data
->has_voltage
= 0x3e; /* in1 to in5 */
1497 revision
= adt7475_read(REG_DEVID2
) & 0x03;
1498 if (revision
== 0x03)
1499 revision
+= adt7475_read(REG_DEVREV2
);
1502 data
->has_voltage
= 0x06; /* in1, in2 */
1503 revision
= adt7475_read(REG_DEVID2
) & 0x07;
1506 config3
= adt7475_read(REG_CONFIG3
);
1507 /* Pin PWM2 may alternatively be used for ALERT output */
1508 if (!(config3
& CONFIG3_SMBALERT
))
1510 /* Meaning of this bit is inverted for the ADT7473-1 */
1511 if (id
->driver_data
== adt7473
&& revision
>= 1)
1512 data
->has_pwm2
= !data
->has_pwm2
;
1514 data
->config4
= adt7475_read(REG_CONFIG4
);
1515 /* Pin TACH4 may alternatively be used for THERM */
1516 if ((data
->config4
& CONFIG4_PINFUNC
) == 0x0)
1520 * THERM configuration is more complex on the ADT7476 and ADT7490,
1521 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1524 if (id
->driver_data
== adt7490
) {
1525 if ((data
->config4
& CONFIG4_PINFUNC
) == 0x1 &&
1526 !(config3
& CONFIG3_THERM
))
1529 if (id
->driver_data
== adt7476
|| id
->driver_data
== adt7490
) {
1530 if (!(config3
& CONFIG3_THERM
) ||
1531 (data
->config4
& CONFIG4_PINFUNC
) == 0x1)
1532 data
->has_voltage
|= (1 << 0); /* in0 */
1536 * On the ADT7476, the +12V input pin may instead be used as VID5,
1537 * and VID pins may alternatively be used as GPIO
1539 if (id
->driver_data
== adt7476
) {
1540 u8 vid
= adt7475_read(REG_VID
);
1541 if (!(vid
& VID_VIDSEL
))
1542 data
->has_voltage
|= (1 << 4); /* in4 */
1544 data
->has_vid
= !(adt7475_read(REG_CONFIG5
) & CONFIG5_VIDGPIO
);
1547 /* Voltage attenuators can be bypassed, globally or individually */
1548 config2
= adt7475_read(REG_CONFIG2
);
1549 if (config2
& CONFIG2_ATTN
) {
1550 data
->bypass_attn
= (0x3 << 3) | 0x3;
1552 data
->bypass_attn
= ((data
->config4
& CONFIG4_ATTN_IN10
) >> 4) |
1553 ((data
->config4
& CONFIG4_ATTN_IN43
) >> 3);
1555 data
->bypass_attn
&= data
->has_voltage
;
1558 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1559 * pwm's which are disabled to manual mode with 0% duty cycle
1561 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++)
1562 adt7475_read_pwm(client
, i
);
1564 /* Start monitoring */
1568 i2c_smbus_write_byte_data(client
, REG_CONFIG1
,
1569 adt7475_read(REG_CONFIG1
) | 0x01);
1575 data
->groups
[group_num
++] = &adt7475_attr_group
;
1577 /* Features that can be disabled individually */
1578 if (data
->has_fan4
) {
1579 data
->groups
[group_num
++] = &fan4_attr_group
;
1581 if (data
->has_pwm2
) {
1582 data
->groups
[group_num
++] = &pwm2_attr_group
;
1584 if (data
->has_voltage
& (1 << 0)) {
1585 data
->groups
[group_num
++] = &in0_attr_group
;
1587 if (data
->has_voltage
& (1 << 3)) {
1588 data
->groups
[group_num
++] = &in3_attr_group
;
1590 if (data
->has_voltage
& (1 << 4)) {
1591 data
->groups
[group_num
++] = &in4_attr_group
;
1593 if (data
->has_voltage
& (1 << 5)) {
1594 data
->groups
[group_num
++] = &in5_attr_group
;
1596 if (data
->has_vid
) {
1597 data
->vrm
= vid_which_vrm();
1598 data
->groups
[group_num
] = &vid_attr_group
;
1601 /* register device with all the acquired attributes */
1602 hwmon_dev
= devm_hwmon_device_register_with_groups(&client
->dev
,
1606 if (IS_ERR(hwmon_dev
)) {
1607 ret
= PTR_ERR(hwmon_dev
);
1611 dev_info(&client
->dev
, "%s device, revision %d\n",
1612 names
[id
->driver_data
], revision
);
1613 if ((data
->has_voltage
& 0x11) || data
->has_fan4
|| data
->has_pwm2
)
1614 dev_info(&client
->dev
, "Optional features:%s%s%s%s%s\n",
1615 (data
->has_voltage
& (1 << 0)) ? " in0" : "",
1616 (data
->has_voltage
& (1 << 4)) ? " in4" : "",
1617 data
->has_fan4
? " fan4" : "",
1618 data
->has_pwm2
? " pwm2" : "",
1619 data
->has_vid
? " vid" : "");
1620 if (data
->bypass_attn
)
1621 dev_info(&client
->dev
, "Bypassing attenuators on:%s%s%s%s\n",
1622 (data
->bypass_attn
& (1 << 0)) ? " in0" : "",
1623 (data
->bypass_attn
& (1 << 1)) ? " in1" : "",
1624 (data
->bypass_attn
& (1 << 3)) ? " in3" : "",
1625 (data
->bypass_attn
& (1 << 4)) ? " in4" : "");
1627 /* Limits and settings, should never change update more than once */
1628 ret
= adt7475_update_limits(client
);
1635 static struct i2c_driver adt7475_driver
= {
1636 .class = I2C_CLASS_HWMON
,
1639 .of_match_table
= of_match_ptr(adt7475_of_match
),
1641 .probe
= adt7475_probe
,
1642 .id_table
= adt7475_id
,
1643 .detect
= adt7475_detect
,
1644 .address_list
= normal_i2c
,
1647 static void adt7475_read_hystersis(struct i2c_client
*client
)
1649 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1651 data
->temp
[HYSTERSIS
][0] = (u16
) adt7475_read(REG_REMOTE1_HYSTERSIS
);
1652 data
->temp
[HYSTERSIS
][1] = data
->temp
[HYSTERSIS
][0];
1653 data
->temp
[HYSTERSIS
][2] = (u16
) adt7475_read(REG_REMOTE2_HYSTERSIS
);
1656 static void adt7475_read_pwm(struct i2c_client
*client
, int index
)
1658 struct adt7475_data
*data
= i2c_get_clientdata(client
);
1661 data
->pwm
[CONTROL
][index
] = adt7475_read(PWM_CONFIG_REG(index
));
1664 * Figure out the internal value for pwmctrl and pwmchan
1665 * based on the current settings
1667 v
= (data
->pwm
[CONTROL
][index
] >> 5) & 7;
1670 data
->pwmctl
[index
] = 0;
1672 data
->pwmctl
[index
] = 1;
1675 * The fan is disabled - we don't want to
1676 * support that, so change to manual mode and
1677 * set the duty cycle to 0 instead
1679 data
->pwm
[INPUT
][index
] = 0;
1680 data
->pwm
[CONTROL
][index
] &= ~0xE0;
1681 data
->pwm
[CONTROL
][index
] |= (7 << 5);
1683 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
1684 data
->pwm
[INPUT
][index
]);
1686 i2c_smbus_write_byte_data(client
, PWM_CONFIG_REG(index
),
1687 data
->pwm
[CONTROL
][index
]);
1689 data
->pwmctl
[index
] = 1;
1691 data
->pwmctl
[index
] = 2;
1695 data
->pwmchan
[index
] = 1;
1698 data
->pwmchan
[index
] = 2;
1701 data
->pwmchan
[index
] = 4;
1704 data
->pwmchan
[index
] = 6;
1707 data
->pwmchan
[index
] = 7;
1713 static int adt7475_update_measure(struct device
*dev
)
1715 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1716 struct i2c_client
*client
= data
->client
;
1721 ret
= adt7475_read(REG_STATUS2
);
1724 data
->alarms
= ret
<< 8;
1726 ret
= adt7475_read(REG_STATUS1
);
1729 data
->alarms
|= ret
;
1731 ret
= adt7475_read(REG_EXTEND2
);
1737 ret
= adt7475_read(REG_EXTEND1
);
1743 for (i
= 0; i
< ADT7475_VOLTAGE_COUNT
; i
++) {
1744 if (!(data
->has_voltage
& (1 << i
)))
1746 ret
= adt7475_read(VOLTAGE_REG(i
));
1749 data
->voltage
[INPUT
][i
] =
1751 ((ext
>> (i
* 2)) & 3);
1754 for (i
= 0; i
< ADT7475_TEMP_COUNT
; i
++) {
1755 ret
= adt7475_read(TEMP_REG(i
));
1758 data
->temp
[INPUT
][i
] =
1760 ((ext
>> ((i
+ 5) * 2)) & 3);
1763 if (data
->has_voltage
& (1 << 5)) {
1764 ret
= adt7475_read(REG_STATUS4
);
1767 data
->alarms
|= ret
<< 24;
1769 ret
= adt7475_read(REG_EXTEND3
);
1774 ret
= adt7475_read(REG_VTT
);
1777 data
->voltage
[INPUT
][5] = ret
<< 2 |
1781 for (i
= 0; i
< ADT7475_TACH_COUNT
; i
++) {
1782 if (i
== 3 && !data
->has_fan4
)
1784 ret
= adt7475_read_word(client
, TACH_REG(i
));
1787 data
->tach
[INPUT
][i
] = ret
;
1790 /* Updated by hw when in auto mode */
1791 for (i
= 0; i
< ADT7475_PWM_COUNT
; i
++) {
1792 if (i
== 1 && !data
->has_pwm2
)
1794 ret
= adt7475_read(PWM_REG(i
));
1797 data
->pwm
[INPUT
][i
] = ret
;
1800 if (data
->has_vid
) {
1801 ret
= adt7475_read(REG_VID
);
1804 data
->vid
= ret
& 0x3f;
1810 static struct adt7475_data
*adt7475_update_device(struct device
*dev
)
1812 struct adt7475_data
*data
= dev_get_drvdata(dev
);
1815 mutex_lock(&data
->lock
);
1817 /* Measurement values update every 2 seconds */
1818 if (time_after(jiffies
, data
->measure_updated
+ HZ
* 2) ||
1820 ret
= adt7475_update_measure(dev
);
1822 data
->valid
= false;
1823 mutex_unlock(&data
->lock
);
1824 return ERR_PTR(ret
);
1826 data
->measure_updated
= jiffies
;
1830 mutex_unlock(&data
->lock
);
1835 module_i2c_driver(adt7475_driver
);
1837 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1838 MODULE_DESCRIPTION("adt7475 driver");
1839 MODULE_LICENSE("GPL");