1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
8 * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
10 * Conversion to regmap and with_info API:
11 * Copyright (C) 2024 Guenter Roeck <linux@roeck-us.net>
14 #include <linux/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/bits.h>
17 #include <linux/err.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/minmax.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/of_platform.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
33 static const unsigned short normal_i2c
[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
34 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
40 static int pwminv
; /*Inverted PWM output. */
41 module_param(pwminv
, int, 0444);
43 static int init
= 1; /*Power-on initialization.*/
44 module_param(init
, int, 0444);
46 #define AMC6821_REG_DEV_ID 0x3D
47 #define AMC6821_REG_COMP_ID 0x3E
48 #define AMC6821_REG_CONF1 0x00
49 #define AMC6821_REG_CONF2 0x01
50 #define AMC6821_REG_CONF3 0x3F
51 #define AMC6821_REG_CONF4 0x04
52 #define AMC6821_REG_STAT1 0x02
53 #define AMC6821_REG_STAT2 0x03
54 #define AMC6821_REG_TEMP_LO 0x06
55 #define AMC6821_REG_TDATA_LOW 0x08
56 #define AMC6821_REG_TDATA_HI 0x09
57 #define AMC6821_REG_LTEMP_HI 0x0A
58 #define AMC6821_REG_RTEMP_HI 0x0B
59 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
60 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
61 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
62 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
63 #define AMC6821_REG_LTEMP_CRIT 0x1B
64 #define AMC6821_REG_RTEMP_CRIT 0x1D
65 #define AMC6821_REG_PSV_TEMP 0x1C
66 #define AMC6821_REG_DCY 0x22
67 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
68 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
69 #define AMC6821_REG_DCY_LOW_TEMP 0x21
71 #define AMC6821_REG_TACH_LLIMITL 0x10
72 #define AMC6821_REG_TACH_HLIMITL 0x12
73 #define AMC6821_REG_TACH_SETTINGL 0x1e
75 #define AMC6821_CONF1_START BIT(0)
76 #define AMC6821_CONF1_FAN_INT_EN BIT(1)
77 #define AMC6821_CONF1_FANIE BIT(2)
78 #define AMC6821_CONF1_PWMINV BIT(3)
79 #define AMC6821_CONF1_FAN_FAULT_EN BIT(4)
80 #define AMC6821_CONF1_FDRC0 BIT(5)
81 #define AMC6821_CONF1_FDRC1 BIT(6)
82 #define AMC6821_CONF1_THERMOVIE BIT(7)
84 #define AMC6821_CONF2_PWM_EN BIT(0)
85 #define AMC6821_CONF2_TACH_MODE BIT(1)
86 #define AMC6821_CONF2_TACH_EN BIT(2)
87 #define AMC6821_CONF2_RTFIE BIT(3)
88 #define AMC6821_CONF2_LTOIE BIT(4)
89 #define AMC6821_CONF2_RTOIE BIT(5)
90 #define AMC6821_CONF2_PSVIE BIT(6)
91 #define AMC6821_CONF2_RST BIT(7)
93 #define AMC6821_CONF3_THERM_FAN_EN BIT(7)
94 #define AMC6821_CONF3_REV_MASK GENMASK(3, 0)
96 #define AMC6821_CONF4_OVREN BIT(4)
97 #define AMC6821_CONF4_TACH_FAST BIT(5)
98 #define AMC6821_CONF4_PSPR BIT(6)
99 #define AMC6821_CONF4_MODE BIT(7)
101 #define AMC6821_STAT1_RPM_ALARM BIT(0)
102 #define AMC6821_STAT1_FANS BIT(1)
103 #define AMC6821_STAT1_RTH BIT(2)
104 #define AMC6821_STAT1_RTL BIT(3)
105 #define AMC6821_STAT1_R_THERM BIT(4)
106 #define AMC6821_STAT1_RTF BIT(5)
107 #define AMC6821_STAT1_LTH BIT(6)
108 #define AMC6821_STAT1_LTL BIT(7)
110 #define AMC6821_STAT2_RTC BIT(3)
111 #define AMC6821_STAT2_LTC BIT(4)
112 #define AMC6821_STAT2_LPSV BIT(5)
113 #define AMC6821_STAT2_L_THERM BIT(6)
114 #define AMC6821_STAT2_THERM_IN BIT(7)
116 #define AMC6821_TEMP_SLOPE_MASK GENMASK(2, 0)
117 #define AMC6821_TEMP_LIMIT_MASK GENMASK(7, 3)
120 * Client data (each client gets its own)
123 struct amc6821_data
{
124 struct regmap
*regmap
;
125 struct mutex update_lock
;
129 * Return 0 on success or negative error code.
131 * temps returns set of three temperatures, in °C:
132 * temps[0]: Passive cooling temperature, applies to both channels
133 * temps[1]: Low temperature, start slope calculations
134 * temps[2]: High temperature
136 * Channel 0: local, channel 1: remote.
138 static int amc6821_get_auto_point_temps(struct regmap
*regmap
, int channel
, u8
*temps
)
141 AMC6821_REG_DCY_LOW_TEMP
,
142 AMC6821_REG_PSV_TEMP
,
143 channel
? AMC6821_REG_RTEMP_FAN_CTRL
: AMC6821_REG_LTEMP_FAN_CTRL
149 err
= regmap_multi_reg_read(regmap
, regs
, regvals
, 3);
152 temps
[0] = regvals
[1];
153 temps
[1] = FIELD_GET(AMC6821_TEMP_LIMIT_MASK
, regvals
[2]) * 4;
155 /* slope is 32 >> <slope bits> in °C */
156 slope
= 32 >> FIELD_GET(AMC6821_TEMP_SLOPE_MASK
, regvals
[2]);
158 temps
[2] = temps
[1] + DIV_ROUND_CLOSEST(255 - regvals
[0], slope
);
165 static int amc6821_temp_read_values(struct regmap
*regmap
, u32 attr
, int channel
, long *val
)
171 case hwmon_temp_input
:
172 reg
= channel
? AMC6821_REG_RTEMP_HI
: AMC6821_REG_LTEMP_HI
;
175 reg
= channel
? AMC6821_REG_RTEMP_LIMIT_MIN
: AMC6821_REG_LTEMP_LIMIT_MIN
;
178 reg
= channel
? AMC6821_REG_RTEMP_LIMIT_MAX
: AMC6821_REG_LTEMP_LIMIT_MAX
;
180 case hwmon_temp_crit
:
181 reg
= channel
? AMC6821_REG_RTEMP_CRIT
: AMC6821_REG_LTEMP_CRIT
;
186 err
= regmap_read(regmap
, reg
, ®val
);
189 *val
= sign_extend32(regval
, 7) * 1000;
193 static int amc6821_read_alarms(struct regmap
*regmap
, enum hwmon_sensor_types type
,
194 u32 attr
, int channel
, long *val
)
202 case hwmon_temp_min_alarm
:
203 reg
= AMC6821_REG_STAT1
;
204 mask
= channel
? AMC6821_STAT1_RTL
: AMC6821_STAT1_LTL
;
206 case hwmon_temp_max_alarm
:
207 reg
= AMC6821_REG_STAT1
;
208 mask
= channel
? AMC6821_STAT1_RTH
: AMC6821_STAT1_LTH
;
210 case hwmon_temp_crit_alarm
:
211 reg
= AMC6821_REG_STAT2
;
212 mask
= channel
? AMC6821_STAT2_RTC
: AMC6821_STAT2_LTC
;
214 case hwmon_temp_fault
:
215 reg
= AMC6821_REG_STAT1
;
216 mask
= AMC6821_STAT1_RTF
;
224 case hwmon_fan_fault
:
225 reg
= AMC6821_REG_STAT1
;
226 mask
= AMC6821_STAT1_FANS
;
235 err
= regmap_read(regmap
, reg
, ®val
);
238 *val
= !!(regval
& mask
);
242 static int amc6821_temp_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
244 struct amc6821_data
*data
= dev_get_drvdata(dev
);
247 case hwmon_temp_input
:
250 case hwmon_temp_crit
:
251 return amc6821_temp_read_values(data
->regmap
, attr
, channel
, val
);
252 case hwmon_temp_min_alarm
:
253 case hwmon_temp_max_alarm
:
254 case hwmon_temp_crit_alarm
:
255 case hwmon_temp_fault
:
256 return amc6821_read_alarms(data
->regmap
, hwmon_temp
, attr
, channel
, val
);
262 static int amc6821_temp_write(struct device
*dev
, u32 attr
, int channel
, long val
)
264 struct amc6821_data
*data
= dev_get_drvdata(dev
);
267 val
= DIV_ROUND_CLOSEST(clamp_val(val
, -128000, 127000), 1000);
271 reg
= channel
? AMC6821_REG_RTEMP_LIMIT_MIN
: AMC6821_REG_LTEMP_LIMIT_MIN
;
274 reg
= channel
? AMC6821_REG_RTEMP_LIMIT_MAX
: AMC6821_REG_LTEMP_LIMIT_MAX
;
276 case hwmon_temp_crit
:
277 reg
= channel
? AMC6821_REG_RTEMP_CRIT
: AMC6821_REG_LTEMP_CRIT
;
282 return regmap_write(data
->regmap
, reg
, val
);
285 static int amc6821_pwm_read(struct device
*dev
, u32 attr
, long *val
)
287 struct amc6821_data
*data
= dev_get_drvdata(dev
);
288 struct regmap
*regmap
= data
->regmap
;
293 case hwmon_pwm_enable
:
294 err
= regmap_read(regmap
, AMC6821_REG_CONF1
, ®val
);
297 switch (regval
& (AMC6821_CONF1_FDRC0
| AMC6821_CONF1_FDRC1
)) {
299 *val
= 1; /* manual */
301 case AMC6821_CONF1_FDRC0
:
302 *val
= 4; /* target rpm (fan1_target) controlled */
304 case AMC6821_CONF1_FDRC1
:
305 *val
= 2; /* remote temp controlled */
308 *val
= 3; /* max(local, remote) temp controlled */
313 err
= regmap_read(regmap
, AMC6821_REG_CONF2
, ®val
);
316 *val
= !!(regval
& AMC6821_CONF2_TACH_MODE
);
318 case hwmon_pwm_auto_channels_temp
:
319 err
= regmap_read(regmap
, AMC6821_REG_CONF1
, ®val
);
322 switch (regval
& (AMC6821_CONF1_FDRC0
| AMC6821_CONF1_FDRC1
)) {
324 case AMC6821_CONF1_FDRC0
:
325 *val
= 0; /* manual or target rpm controlled */
327 case AMC6821_CONF1_FDRC1
:
328 *val
= 2; /* remote temp controlled */
331 *val
= 3; /* max(local, remote) temp controlled */
335 case hwmon_pwm_input
:
336 err
= regmap_read(regmap
, AMC6821_REG_DCY
, ®val
);
346 static int amc6821_pwm_write(struct device
*dev
, u32 attr
, long val
)
348 struct amc6821_data
*data
= dev_get_drvdata(dev
);
349 struct regmap
*regmap
= data
->regmap
;
353 case hwmon_pwm_enable
:
359 mode
= AMC6821_CONF1_FDRC1
;
362 mode
= AMC6821_CONF1_FDRC0
| AMC6821_CONF1_FDRC1
;
365 mode
= AMC6821_CONF1_FDRC0
;
370 return regmap_update_bits(regmap
, AMC6821_REG_CONF1
,
371 AMC6821_CONF1_FDRC0
| AMC6821_CONF1_FDRC1
,
374 if (val
< 0 || val
> 1)
376 return regmap_update_bits(regmap
, AMC6821_REG_CONF2
,
377 AMC6821_CONF2_TACH_MODE
,
378 val
? AMC6821_CONF2_TACH_MODE
: 0);
380 case hwmon_pwm_input
:
381 if (val
< 0 || val
> 255)
383 return regmap_write(regmap
, AMC6821_REG_DCY
, val
);
389 static int amc6821_fan_read_rpm(struct regmap
*regmap
, u32 attr
, long *val
)
396 case hwmon_fan_input
:
397 reg
= AMC6821_REG_TDATA_LOW
;
400 reg
= AMC6821_REG_TACH_LLIMITL
;
403 reg
= AMC6821_REG_TACH_HLIMITL
;
405 case hwmon_fan_target
:
406 reg
= AMC6821_REG_TACH_SETTINGL
;
412 err
= regmap_bulk_read(regmap
, reg
, regs
, 2);
416 regval
= (regs
[1] << 8) | regs
[0];
417 *val
= regval
? 6000000 / regval
: 0;
422 static int amc6821_fan_read(struct device
*dev
, u32 attr
, long *val
)
424 struct amc6821_data
*data
= dev_get_drvdata(dev
);
425 struct regmap
*regmap
= data
->regmap
;
430 case hwmon_fan_input
:
433 case hwmon_fan_target
:
434 return amc6821_fan_read_rpm(regmap
, attr
, val
);
435 case hwmon_fan_fault
:
436 return amc6821_read_alarms(regmap
, hwmon_fan
, attr
, 0, val
);
437 case hwmon_fan_pulses
:
438 err
= regmap_read(regmap
, AMC6821_REG_CONF4
, ®val
);
441 *val
= (regval
& AMC6821_CONF4_PSPR
) ? 4 : 2;
448 static int amc6821_fan_write(struct device
*dev
, u32 attr
, long val
)
450 struct amc6821_data
*data
= dev_get_drvdata(dev
);
451 struct regmap
*regmap
= data
->regmap
;
455 if (attr
== hwmon_fan_pulses
) {
456 if (val
!= 2 && val
!= 4)
458 return regmap_update_bits(regmap
, AMC6821_REG_CONF4
,
460 val
== 4 ? AMC6821_CONF4_PSPR
: 0);
468 if (!val
) /* no unlimited minimum speed */
470 reg
= AMC6821_REG_TACH_LLIMITL
;
473 reg
= AMC6821_REG_TACH_HLIMITL
;
475 case hwmon_fan_target
:
476 if (!val
) /* no unlimited target speed */
478 reg
= AMC6821_REG_TACH_SETTINGL
;
484 val
= val
? 6000000 / clamp_val(val
, 1, 6000000) : 0;
485 val
= clamp_val(val
, 0, 0xffff);
487 regs
[0] = val
& 0xff;
490 return regmap_bulk_write(data
->regmap
, reg
, regs
, 2);
493 static ssize_t
temp_auto_point_temp_show(struct device
*dev
,
494 struct device_attribute
*devattr
,
497 struct amc6821_data
*data
= dev_get_drvdata(dev
);
498 int ix
= to_sensor_dev_attr_2(devattr
)->index
;
499 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
503 mutex_lock(&data
->update_lock
);
504 err
= amc6821_get_auto_point_temps(data
->regmap
, nr
, temps
);
505 mutex_unlock(&data
->update_lock
);
509 return sysfs_emit(buf
, "%d\n", temps
[ix
] * 1000);
512 static ssize_t
pwm1_auto_point_pwm_show(struct device
*dev
,
513 struct device_attribute
*devattr
,
516 struct amc6821_data
*data
= dev_get_drvdata(dev
);
517 int ix
= to_sensor_dev_attr(devattr
)->index
;
526 err
= regmap_read(data
->regmap
, AMC6821_REG_DCY_LOW_TEMP
, &val
);
534 return sysfs_emit(buf
, "%d\n", val
);
538 * Set TEMP[0-4] (low temperature) and SLP[0-2] (slope) of local or remote
539 * TEMP-FAN control register.
541 * Return 0 on success or negative error code.
543 * Channel 0: local, channel 1: remote
545 static inline int set_slope_register(struct regmap
*regmap
, int channel
, u8
*temps
)
547 u8 regval
= FIELD_PREP(AMC6821_TEMP_LIMIT_MASK
, temps
[1] / 4);
552 err
= regmap_read(regmap
, AMC6821_REG_DCY_LOW_TEMP
, &pwm
);
558 dt
= temps
[2] - temps
[1];
559 for (tmp
= 4; tmp
> 0; tmp
--) {
560 if (dt
* (32 >> tmp
) >= dpwm
)
563 regval
|= FIELD_PREP(AMC6821_TEMP_SLOPE_MASK
, tmp
);
565 return regmap_write(regmap
,
566 channel
? AMC6821_REG_RTEMP_FAN_CTRL
: AMC6821_REG_LTEMP_FAN_CTRL
,
570 static ssize_t
temp_auto_point_temp_store(struct device
*dev
,
571 struct device_attribute
*attr
,
572 const char *buf
, size_t count
)
574 struct amc6821_data
*data
= dev_get_drvdata(dev
);
575 int ix
= to_sensor_dev_attr_2(attr
)->index
;
576 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
577 struct regmap
*regmap
= data
->regmap
;
578 u8 temps
[3], otemps
[3];
582 ret
= kstrtol(buf
, 10, &val
);
586 mutex_lock(&data
->update_lock
);
588 ret
= amc6821_get_auto_point_temps(data
->regmap
, nr
, temps
);
595 * Passive cooling temperature. Range limit against low limit
598 ret
= amc6821_get_auto_point_temps(data
->regmap
, 1 - nr
, otemps
);
601 val
= DIV_ROUND_CLOSEST(clamp_val(val
, 0, 63000), 1000);
602 val
= clamp_val(val
, 0, min(temps
[1], otemps
[1]));
603 ret
= regmap_write(regmap
, AMC6821_REG_PSV_TEMP
, val
);
607 * Low limit; must be between passive and high limit,
608 * and not exceed 124. Step size is 4 degrees C.
610 val
= clamp_val(val
, DIV_ROUND_UP(temps
[0], 4) * 4000, 124000);
611 temps
[1] = DIV_ROUND_CLOSEST(val
, 4000) * 4;
613 /* Auto-adjust high limit if necessary */
614 temps
[2] = clamp_val(temps
[2], temps
[1] + 1, 255);
615 ret
= set_slope_register(regmap
, nr
, temps
);
618 /* high limit, must be higher than low limit */
619 val
= clamp_val(val
, (temps
[1] + 1) * 1000, 255000);
620 temps
[2] = DIV_ROUND_CLOSEST(val
, 1000);
621 ret
= set_slope_register(regmap
, nr
, temps
);
628 mutex_unlock(&data
->update_lock
);
629 return ret
? : count
;
632 static ssize_t
pwm1_auto_point_pwm_store(struct device
*dev
,
633 struct device_attribute
*attr
,
634 const char *buf
, size_t count
)
636 struct amc6821_data
*data
= dev_get_drvdata(dev
);
637 struct regmap
*regmap
= data
->regmap
;
641 ret
= kstrtou8(buf
, 10, &val
);
645 mutex_lock(&data
->update_lock
);
646 ret
= regmap_write(regmap
, AMC6821_REG_DCY_LOW_TEMP
, val
);
650 for (i
= 0; i
< 2; i
++) {
653 ret
= amc6821_get_auto_point_temps(regmap
, i
, temps
);
656 ret
= set_slope_register(regmap
, i
, temps
);
661 mutex_unlock(&data
->update_lock
);
662 return ret
? : count
;
665 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm
, pwm1_auto_point_pwm
, 0);
666 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm
, pwm1_auto_point_pwm
, 1);
667 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm
, pwm1_auto_point_pwm
, 2);
668 static SENSOR_DEVICE_ATTR_2_RO(temp1_auto_point1_temp
, temp_auto_point_temp
,
670 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp
, temp_auto_point_temp
,
672 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point3_temp
, temp_auto_point_temp
,
675 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp
, temp_auto_point_temp
,
677 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp
, temp_auto_point_temp
,
679 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point3_temp
, temp_auto_point_temp
,
682 static struct attribute
*amc6821_attrs
[] = {
683 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
684 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
685 &sensor_dev_attr_pwm1_auto_point3_pwm
.dev_attr
.attr
,
686 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
687 &sensor_dev_attr_temp1_auto_point2_temp
.dev_attr
.attr
,
688 &sensor_dev_attr_temp1_auto_point3_temp
.dev_attr
.attr
,
689 &sensor_dev_attr_temp2_auto_point1_temp
.dev_attr
.attr
,
690 &sensor_dev_attr_temp2_auto_point2_temp
.dev_attr
.attr
,
691 &sensor_dev_attr_temp2_auto_point3_temp
.dev_attr
.attr
,
694 ATTRIBUTE_GROUPS(amc6821
);
696 static int amc6821_read(struct device
*dev
, enum hwmon_sensor_types type
,
697 u32 attr
, int channel
, long *val
)
701 return amc6821_temp_read(dev
, attr
, channel
, val
);
703 return amc6821_fan_read(dev
, attr
, val
);
705 return amc6821_pwm_read(dev
, attr
, val
);
711 static int amc6821_write(struct device
*dev
, enum hwmon_sensor_types type
,
712 u32 attr
, int channel
, long val
)
716 return amc6821_temp_write(dev
, attr
, channel
, val
);
718 return amc6821_fan_write(dev
, attr
, val
);
720 return amc6821_pwm_write(dev
, attr
, val
);
726 static umode_t
amc6821_is_visible(const void *data
,
727 enum hwmon_sensor_types type
,
728 u32 attr
, int channel
)
733 case hwmon_temp_input
:
734 case hwmon_temp_min_alarm
:
735 case hwmon_temp_max_alarm
:
736 case hwmon_temp_crit_alarm
:
737 case hwmon_temp_fault
:
741 case hwmon_temp_crit
:
748 case hwmon_fan_input
:
749 case hwmon_fan_fault
:
751 case hwmon_fan_pulses
:
754 case hwmon_fan_target
:
762 case hwmon_pwm_enable
:
763 case hwmon_pwm_input
:
765 case hwmon_pwm_auto_channels_temp
:
775 static const struct hwmon_channel_info
* const amc6821_info
[] = {
776 HWMON_CHANNEL_INFO(temp
,
777 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
778 HWMON_T_CRIT
| HWMON_T_MIN_ALARM
|
779 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
,
780 HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
|
781 HWMON_T_CRIT
| HWMON_T_MIN_ALARM
|
782 HWMON_T_MAX_ALARM
| HWMON_T_CRIT_ALARM
|
784 HWMON_CHANNEL_INFO(fan
,
785 HWMON_F_INPUT
| HWMON_F_MIN
| HWMON_F_MAX
|
786 HWMON_F_TARGET
| HWMON_F_PULSES
| HWMON_F_FAULT
),
787 HWMON_CHANNEL_INFO(pwm
,
788 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
| HWMON_PWM_MODE
|
789 HWMON_PWM_AUTO_CHANNELS_TEMP
),
793 static const struct hwmon_ops amc6821_hwmon_ops
= {
794 .is_visible
= amc6821_is_visible
,
795 .read
= amc6821_read
,
796 .write
= amc6821_write
,
799 static const struct hwmon_chip_info amc6821_chip_info
= {
800 .ops
= &amc6821_hwmon_ops
,
801 .info
= amc6821_info
,
804 /* Return 0 if detection is successful, -ENODEV otherwise */
805 static int amc6821_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
807 struct i2c_adapter
*adapter
= client
->adapter
;
808 int address
= client
->addr
;
811 dev_dbg(&adapter
->dev
, "amc6821_detect called.\n");
813 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
814 dev_dbg(&adapter
->dev
,
815 "amc6821: I2C bus doesn't support byte mode, "
820 dev_id
= i2c_smbus_read_byte_data(client
, AMC6821_REG_DEV_ID
);
821 comp_id
= i2c_smbus_read_byte_data(client
, AMC6821_REG_COMP_ID
);
822 if (dev_id
!= 0x21 || comp_id
!= 0x49) {
823 dev_dbg(&adapter
->dev
,
824 "amc6821: detection failed at 0x%02x.\n",
830 * Bit 7 of the address register is ignored, so we can check the
833 dev_id
= i2c_smbus_read_byte_data(client
, 0x80 | AMC6821_REG_DEV_ID
);
834 comp_id
= i2c_smbus_read_byte_data(client
, 0x80 | AMC6821_REG_COMP_ID
);
835 if (dev_id
!= 0x21 || comp_id
!= 0x49) {
836 dev_dbg(&adapter
->dev
,
837 "amc6821: detection failed at 0x%02x.\n",
842 dev_info(&adapter
->dev
, "amc6821: chip found at 0x%02x.\n", address
);
843 strscpy(info
->type
, "amc6821", I2C_NAME_SIZE
);
848 static int amc6821_init_client(struct amc6821_data
*data
)
850 struct regmap
*regmap
= data
->regmap
;
854 err
= regmap_set_bits(regmap
, AMC6821_REG_CONF4
, AMC6821_CONF4_MODE
);
857 err
= regmap_clear_bits(regmap
, AMC6821_REG_CONF3
, AMC6821_CONF3_THERM_FAN_EN
);
860 err
= regmap_clear_bits(regmap
, AMC6821_REG_CONF2
,
861 AMC6821_CONF2_RTFIE
|
862 AMC6821_CONF2_LTOIE
|
863 AMC6821_CONF2_RTOIE
);
867 err
= regmap_update_bits(regmap
, AMC6821_REG_CONF1
,
868 AMC6821_CONF1_THERMOVIE
| AMC6821_CONF1_FANIE
|
869 AMC6821_CONF1_START
| AMC6821_CONF1_PWMINV
,
870 AMC6821_CONF1_START
|
871 (pwminv
? AMC6821_CONF1_PWMINV
: 0));
878 static bool amc6821_volatile_reg(struct device
*dev
, unsigned int reg
)
881 case AMC6821_REG_STAT1
:
882 case AMC6821_REG_STAT2
:
883 case AMC6821_REG_TEMP_LO
:
884 case AMC6821_REG_TDATA_LOW
:
885 case AMC6821_REG_LTEMP_HI
:
886 case AMC6821_REG_RTEMP_HI
:
887 case AMC6821_REG_TDATA_HI
:
894 static const struct regmap_config amc6821_regmap_config
= {
897 .volatile_reg
= amc6821_volatile_reg
,
898 .cache_type
= REGCACHE_MAPLE
,
901 static int amc6821_probe(struct i2c_client
*client
)
903 struct device
*dev
= &client
->dev
;
904 struct amc6821_data
*data
;
905 struct device
*hwmon_dev
;
906 struct regmap
*regmap
;
909 data
= devm_kzalloc(dev
, sizeof(struct amc6821_data
), GFP_KERNEL
);
913 regmap
= devm_regmap_init_i2c(client
, &amc6821_regmap_config
);
915 return dev_err_probe(dev
, PTR_ERR(regmap
),
916 "Failed to initialize regmap\n");
917 data
->regmap
= regmap
;
919 err
= amc6821_init_client(data
);
923 if (of_device_is_compatible(dev
->of_node
, "tsd,mule")) {
924 err
= devm_of_platform_populate(dev
);
926 return dev_err_probe(dev
, err
,
927 "Failed to create sub-devices\n");
930 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
931 data
, &amc6821_chip_info
,
933 return PTR_ERR_OR_ZERO(hwmon_dev
);
936 static const struct i2c_device_id amc6821_id
[] = {
941 MODULE_DEVICE_TABLE(i2c
, amc6821_id
);
943 static const struct of_device_id __maybe_unused amc6821_of_match
[] = {
945 .compatible
= "ti,amc6821",
948 .compatible
= "tsd,mule",
953 MODULE_DEVICE_TABLE(of
, amc6821_of_match
);
955 static struct i2c_driver amc6821_driver
= {
956 .class = I2C_CLASS_HWMON
,
959 .of_match_table
= of_match_ptr(amc6821_of_match
),
961 .probe
= amc6821_probe
,
962 .id_table
= amc6821_id
,
963 .detect
= amc6821_detect
,
964 .address_list
= normal_i2c
,
967 module_i2c_driver(amc6821_driver
);
969 MODULE_LICENSE("GPL");
970 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
971 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");