1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC4282 I2C High Current Hot Swap Controller over I2C
5 * Copyright 2023 Analog Devices Inc.
7 #include <linux/bitfield.h>
8 #include <linux/cleanup.h>
10 #include <linux/clk-provider.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/i2c.h>
17 #include <linux/math.h>
18 #include <linux/minmax.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/mutex.h>
22 #include <linux/regmap.h>
23 #include <linux/property.h>
24 #include <linux/string.h>
25 #include <linux/units.h>
26 #include <linux/util_macros.h>
28 #define LTC4282_CTRL_LSB 0x00
29 #define LTC4282_CTRL_OV_RETRY_MASK BIT(0)
30 #define LTC4282_CTRL_UV_RETRY_MASK BIT(1)
31 #define LTC4282_CTRL_OC_RETRY_MASK BIT(2)
32 #define LTC4282_CTRL_ON_ACTIVE_LOW_MASK BIT(5)
33 #define LTC4282_CTRL_ON_DELAY_MASK BIT(6)
34 #define LTC4282_CTRL_MSB 0x01
35 #define LTC4282_CTRL_VIN_MODE_MASK GENMASK(1, 0)
36 #define LTC4282_CTRL_OV_MODE_MASK GENMASK(3, 2)
37 #define LTC4282_CTRL_UV_MODE_MASK GENMASK(5, 4)
38 #define LTC4282_FAULT_LOG 0x04
39 #define LTC4282_OV_FAULT_MASK BIT(0)
40 #define LTC4282_UV_FAULT_MASK BIT(1)
41 #define LTC4282_VDD_FAULT_MASK \
42 (LTC4282_OV_FAULT_MASK | LTC4282_UV_FAULT_MASK)
43 #define LTC4282_OC_FAULT_MASK BIT(2)
44 #define LTC4282_POWER_BAD_FAULT_MASK BIT(3)
45 #define LTC4282_FET_SHORT_FAULT_MASK BIT(5)
46 #define LTC4282_FET_BAD_FAULT_MASK BIT(6)
47 #define LTC4282_FET_FAILURE_FAULT_MASK \
48 (LTC4282_FET_SHORT_FAULT_MASK | LTC4282_FET_BAD_FAULT_MASK)
49 #define LTC4282_ADC_ALERT_LOG 0x05
50 #define LTC4282_GPIO_ALARM_L_MASK BIT(0)
51 #define LTC4282_GPIO_ALARM_H_MASK BIT(1)
52 #define LTC4282_VSOURCE_ALARM_L_MASK BIT(2)
53 #define LTC4282_VSOURCE_ALARM_H_MASK BIT(3)
54 #define LTC4282_VSENSE_ALARM_L_MASK BIT(4)
55 #define LTC4282_VSENSE_ALARM_H_MASK BIT(5)
56 #define LTC4282_POWER_ALARM_L_MASK BIT(6)
57 #define LTC4282_POWER_ALARM_H_MASK BIT(7)
58 #define LTC4282_FET_BAD_FAULT_TIMEOUT 0x06
59 #define LTC4282_FET_BAD_MAX_TIMEOUT 255
60 #define LTC4282_GPIO_CONFIG 0x07
61 #define LTC4282_GPIO_2_FET_STRESS_MASK BIT(1)
62 #define LTC4282_GPIO_1_CONFIG_MASK GENMASK(5, 4)
63 #define LTC4282_VGPIO_MIN 0x08
64 #define LTC4282_VGPIO_MAX 0x09
65 #define LTC4282_VSOURCE_MIN 0x0a
66 #define LTC4282_VSOURCE_MAX 0x0b
67 #define LTC4282_VSENSE_MIN 0x0c
68 #define LTC4282_VSENSE_MAX 0x0d
69 #define LTC4282_POWER_MIN 0x0e
70 #define LTC4282_POWER_MAX 0x0f
71 #define LTC4282_CLK_DIV 0x10
72 #define LTC4282_CLK_DIV_MASK GENMASK(4, 0)
73 #define LTC4282_CLKOUT_MASK GENMASK(6, 5)
74 #define LTC4282_ILIM_ADJUST 0x11
75 #define LTC4282_GPIO_MODE_MASK BIT(1)
76 #define LTC4282_VDD_MONITOR_MASK BIT(2)
77 #define LTC4282_FOLDBACK_MODE_MASK GENMASK(4, 3)
78 #define LTC4282_ILIM_ADJUST_MASK GENMASK(7, 5)
79 #define LTC4282_ENERGY 0x12
80 #define LTC4282_TIME_COUNTER 0x18
81 #define LTC4282_ALERT_CTRL 0x1c
82 #define LTC4282_ALERT_OUT_MASK BIT(6)
83 #define LTC4282_ADC_CTRL 0x1d
84 #define LTC4282_FAULT_LOG_EN_MASK BIT(2)
85 #define LTC4282_METER_HALT_MASK BIT(5)
86 #define LTC4282_METER_RESET_MASK BIT(6)
87 #define LTC4282_RESET_MASK BIT(7)
88 #define LTC4282_STATUS_LSB 0x1e
89 #define LTC4282_OV_STATUS_MASK BIT(0)
90 #define LTC4282_UV_STATUS_MASK BIT(1)
91 #define LTC4282_VDD_STATUS_MASK \
92 (LTC4282_OV_STATUS_MASK | LTC4282_UV_STATUS_MASK)
93 #define LTC4282_OC_STATUS_MASK BIT(2)
94 #define LTC4282_POWER_GOOD_MASK BIT(3)
95 #define LTC4282_FET_FAILURE_MASK GENMASK(6, 5)
96 #define LTC4282_STATUS_MSB 0x1f
97 #define LTC4282_RESERVED_1 0x32
98 #define LTC4282_RESERVED_2 0x33
99 #define LTC4282_VGPIO 0x34
100 #define LTC4282_VGPIO_LOWEST 0x36
101 #define LTC4282_VGPIO_HIGHEST 0x38
102 #define LTC4282_VSOURCE 0x3a
103 #define LTC4282_VSOURCE_LOWEST 0x3c
104 #define LTC4282_VSOURCE_HIGHEST 0x3e
105 #define LTC4282_VSENSE 0x40
106 #define LTC4282_VSENSE_LOWEST 0x42
107 #define LTC4282_VSENSE_HIGHEST 0x44
108 #define LTC4282_POWER 0x46
109 #define LTC4282_POWER_LOWEST 0x48
110 #define LTC4282_POWER_HIGHEST 0x4a
111 #define LTC4282_RESERVED_3 0x50
113 #define LTC4282_CLKIN_MIN (250 * KILO)
114 #define LTC4282_CLKIN_MAX (15500 * KILO)
115 #define LTC4282_CLKIN_RANGE (LTC4282_CLKIN_MAX - LTC4282_CLKIN_MIN + 1)
116 #define LTC4282_CLKOUT_SYSTEM (250 * KILO)
117 #define LTC4282_CLKOUT_CNV 15
120 LTC4282_CHAN_VSOURCE
,
125 struct ltc4282_cache
{
133 struct ltc4282_state
{
135 /* Protect against multiple accesses to the device registers */
137 struct clk_hw clk_hw
;
139 * Used to cache values for VDD/VSOURCE depending which will be used
140 * when hwmon is not enabled for that channel. Needed because they share
141 * the same registers.
143 struct ltc4282_cache in0_1_cache
[LTC4282_CHAN_VGPIO
];
158 static int ltc4282_set_rate(struct clk_hw
*hw
,
159 unsigned long rate
, unsigned long parent_rate
)
161 struct ltc4282_state
*st
= container_of(hw
, struct ltc4282_state
,
163 u32 val
= LTC4282_CLKOUT_INT
;
165 if (rate
== LTC4282_CLKOUT_CNV
)
166 val
= LTC4282_CLKOUT_TICK
;
168 return regmap_update_bits(st
->map
, LTC4282_CLK_DIV
, LTC4282_CLKOUT_MASK
,
169 FIELD_PREP(LTC4282_CLKOUT_MASK
, val
));
173 * Note the 15HZ conversion rate assumes 12bit ADC which is what we are
174 * supporting for now.
176 static const unsigned int ltc4282_out_rates
[] = {
177 LTC4282_CLKOUT_CNV
, LTC4282_CLKOUT_SYSTEM
180 static long ltc4282_round_rate(struct clk_hw
*hw
, unsigned long rate
,
181 unsigned long *parent_rate
)
183 int idx
= find_closest(rate
, ltc4282_out_rates
,
184 ARRAY_SIZE(ltc4282_out_rates
));
186 return ltc4282_out_rates
[idx
];
189 static unsigned long ltc4282_recalc_rate(struct clk_hw
*hw
,
190 unsigned long parent
)
192 struct ltc4282_state
*st
= container_of(hw
, struct ltc4282_state
,
197 ret
= regmap_read(st
->map
, LTC4282_CLK_DIV
, &clkdiv
);
201 clkdiv
= FIELD_GET(LTC4282_CLKOUT_MASK
, clkdiv
);
204 if (clkdiv
== LTC4282_CLKOUT_INT
)
205 return LTC4282_CLKOUT_SYSTEM
;
207 return LTC4282_CLKOUT_CNV
;
210 static void ltc4282_disable(struct clk_hw
*clk_hw
)
212 struct ltc4282_state
*st
= container_of(clk_hw
, struct ltc4282_state
,
215 regmap_clear_bits(st
->map
, LTC4282_CLK_DIV
, LTC4282_CLKOUT_MASK
);
218 static int ltc4282_read_voltage_word(const struct ltc4282_state
*st
, u32 reg
,
224 ret
= regmap_bulk_read(st
->map
, reg
, &in
, sizeof(in
));
229 * This is also used to calculate current in which case fs comes in
230 * 10 * uV. Hence the ULL usage.
232 *val
= DIV_ROUND_CLOSEST_ULL(be16_to_cpu(in
) * (u64
)fs
, U16_MAX
);
236 static int ltc4282_read_voltage_byte_cached(const struct ltc4282_state
*st
,
237 u32 reg
, u32 fs
, long *val
,
246 ret
= regmap_read(st
->map
, reg
, &in
);
251 *val
= DIV_ROUND_CLOSEST(in
* fs
, U8_MAX
);
255 static int ltc4282_read_voltage_byte(const struct ltc4282_state
*st
, u32 reg
,
258 return ltc4282_read_voltage_byte_cached(st
, reg
, fs
, val
, NULL
);
261 static int __ltc4282_read_alarm(struct ltc4282_state
*st
, u32 reg
, u32 mask
,
267 ret
= regmap_read(st
->map
, reg
, &alarm
);
271 *val
= !!(alarm
& mask
);
273 /* if not status/fault logs, clear the alarm after reading it */
274 if (reg
!= LTC4282_STATUS_LSB
&& reg
!= LTC4282_FAULT_LOG
)
275 return regmap_clear_bits(st
->map
, reg
, mask
);
280 static int ltc4282_read_alarm(struct ltc4282_state
*st
, u32 reg
, u32 mask
,
283 guard(mutex
)(&st
->lock
);
284 return __ltc4282_read_alarm(st
, reg
, mask
, val
);
287 static int ltc4282_vdd_source_read_in(struct ltc4282_state
*st
, u32 channel
,
290 guard(mutex
)(&st
->lock
);
291 if (!st
->in0_1_cache
[channel
].en
)
294 return ltc4282_read_voltage_word(st
, LTC4282_VSOURCE
, st
->vfs_out
, val
);
297 static int ltc4282_vdd_source_read_hist(struct ltc4282_state
*st
, u32 reg
,
298 u32 channel
, long *cached
, long *val
)
302 guard(mutex
)(&st
->lock
);
303 if (!st
->in0_1_cache
[channel
].en
) {
308 ret
= ltc4282_read_voltage_word(st
, reg
, st
->vfs_out
, val
);
316 static int ltc4282_vdd_source_read_lim(struct ltc4282_state
*st
, u32 reg
,
317 u32 channel
, u32
*cached
, long *val
)
319 guard(mutex
)(&st
->lock
);
320 if (!st
->in0_1_cache
[channel
].en
)
321 return ltc4282_read_voltage_byte_cached(st
, reg
, st
->vfs_out
,
324 return ltc4282_read_voltage_byte(st
, reg
, st
->vfs_out
, val
);
327 static int ltc4282_vdd_source_read_alm(struct ltc4282_state
*st
, u32 mask
,
328 u32 channel
, long *val
)
330 guard(mutex
)(&st
->lock
);
331 if (!st
->in0_1_cache
[channel
].en
) {
333 * Do this otherwise alarms can get confused because we clear
334 * them after reading them. So, if someone mistakenly reads
335 * VSOURCE right before VDD (or the other way around), we might
336 * get no alarm just because it was cleared when reading VSOURCE
337 * and had no time for a new conversion and thus having the
344 return __ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
, mask
, val
);
347 static int ltc4282_read_in(struct ltc4282_state
*st
, u32 attr
, long *val
,
352 if (channel
== LTC4282_CHAN_VGPIO
)
353 return ltc4282_read_voltage_word(st
, LTC4282_VGPIO
,
356 return ltc4282_vdd_source_read_in(st
, channel
, val
);
357 case hwmon_in_highest
:
358 if (channel
== LTC4282_CHAN_VGPIO
)
359 return ltc4282_read_voltage_word(st
,
360 LTC4282_VGPIO_HIGHEST
,
363 return ltc4282_vdd_source_read_hist(st
, LTC4282_VSOURCE_HIGHEST
,
365 &st
->in0_1_cache
[channel
].in_highest
, val
);
366 case hwmon_in_lowest
:
367 if (channel
== LTC4282_CHAN_VGPIO
)
368 return ltc4282_read_voltage_word(st
, LTC4282_VGPIO_LOWEST
,
371 return ltc4282_vdd_source_read_hist(st
, LTC4282_VSOURCE_LOWEST
,
373 &st
->in0_1_cache
[channel
].in_lowest
, val
);
374 case hwmon_in_max_alarm
:
375 if (channel
== LTC4282_CHAN_VGPIO
)
376 return ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
377 LTC4282_GPIO_ALARM_H_MASK
,
380 return ltc4282_vdd_source_read_alm(st
,
381 LTC4282_VSOURCE_ALARM_H_MASK
,
383 case hwmon_in_min_alarm
:
384 if (channel
== LTC4282_CHAN_VGPIO
)
385 ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
386 LTC4282_GPIO_ALARM_L_MASK
, val
);
388 return ltc4282_vdd_source_read_alm(st
,
389 LTC4282_VSOURCE_ALARM_L_MASK
,
391 case hwmon_in_crit_alarm
:
392 return ltc4282_read_alarm(st
, LTC4282_STATUS_LSB
,
393 LTC4282_OV_STATUS_MASK
, val
);
394 case hwmon_in_lcrit_alarm
:
395 return ltc4282_read_alarm(st
, LTC4282_STATUS_LSB
,
396 LTC4282_UV_STATUS_MASK
, val
);
398 if (channel
== LTC4282_CHAN_VGPIO
)
399 return ltc4282_read_voltage_byte(st
, LTC4282_VGPIO_MAX
,
402 return ltc4282_vdd_source_read_lim(st
, LTC4282_VSOURCE_MAX
,
404 &st
->in0_1_cache
[channel
].in_max_raw
, val
);
406 if (channel
== LTC4282_CHAN_VGPIO
)
407 return ltc4282_read_voltage_byte(st
, LTC4282_VGPIO_MIN
,
410 return ltc4282_vdd_source_read_lim(st
, LTC4282_VSOURCE_MIN
,
412 &st
->in0_1_cache
[channel
].in_min_raw
, val
);
413 case hwmon_in_enable
:
414 scoped_guard(mutex
, &st
->lock
) {
415 *val
= st
->in0_1_cache
[channel
].en
;
420 * We report failure if we detect either a fer_bad or a
421 * fet_short in the status register.
423 return ltc4282_read_alarm(st
, LTC4282_STATUS_LSB
,
424 LTC4282_FET_FAILURE_MASK
, val
);
430 static int ltc4282_read_current_word(const struct ltc4282_state
*st
, u32 reg
,
437 * We pass in full scale in 10 * micro (note that 40 is already
438 * millivolt) so we have better approximations to calculate current.
440 ret
= ltc4282_read_voltage_word(st
, reg
, DECA
* 40 * MILLI
, &in
);
444 *val
= DIV_ROUND_CLOSEST(in
* MILLI
, st
->rsense
);
449 static int ltc4282_read_current_byte(const struct ltc4282_state
*st
, u32 reg
,
455 ret
= ltc4282_read_voltage_byte(st
, reg
, DECA
* 40 * MILLI
, &in
);
459 *val
= DIV_ROUND_CLOSEST(in
* MILLI
, st
->rsense
);
464 static int ltc4282_read_curr(struct ltc4282_state
*st
, const u32 attr
,
468 case hwmon_curr_input
:
469 return ltc4282_read_current_word(st
, LTC4282_VSENSE
, val
);
470 case hwmon_curr_highest
:
471 return ltc4282_read_current_word(st
, LTC4282_VSENSE_HIGHEST
,
473 case hwmon_curr_lowest
:
474 return ltc4282_read_current_word(st
, LTC4282_VSENSE_LOWEST
,
477 return ltc4282_read_current_byte(st
, LTC4282_VSENSE_MAX
, val
);
479 return ltc4282_read_current_byte(st
, LTC4282_VSENSE_MIN
, val
);
480 case hwmon_curr_max_alarm
:
481 return ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
482 LTC4282_VSENSE_ALARM_H_MASK
, val
);
483 case hwmon_curr_min_alarm
:
484 return ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
485 LTC4282_VSENSE_ALARM_L_MASK
, val
);
486 case hwmon_curr_crit_alarm
:
487 return ltc4282_read_alarm(st
, LTC4282_STATUS_LSB
,
488 LTC4282_OC_STATUS_MASK
, val
);
494 static int ltc4282_read_power_word(const struct ltc4282_state
*st
, u32 reg
,
497 u64 temp
= DECA
* 40ULL * st
->vfs_out
* BIT(16), temp_2
;
502 ret
= regmap_bulk_read(st
->map
, reg
, &raw
, sizeof(raw
));
506 power
= be16_to_cpu(raw
);
509 * P = CODE(16b) * 0.040 * Vfs(out) * 2^16 / ((2^16 - 1)^2 * Rsense)
511 if (check_mul_overflow(power
* temp
, MICRO
, &temp_2
)) {
512 temp
= DIV_ROUND_CLOSEST_ULL(power
* temp
, U16_MAX
);
513 *val
= DIV64_U64_ROUND_CLOSEST(temp
* MICRO
,
514 U16_MAX
* (u64
)st
->rsense
);
518 *val
= DIV64_U64_ROUND_CLOSEST(temp_2
,
519 st
->rsense
* int_pow(U16_MAX
, 2));
524 static int ltc4282_read_power_byte(const struct ltc4282_state
*st
, u32 reg
,
531 ret
= regmap_read(st
->map
, reg
, &power
);
535 temp
= power
* 40 * DECA
* st
->vfs_out
* BIT_ULL(8);
536 *val
= DIV64_U64_ROUND_CLOSEST(temp
* MICRO
,
537 int_pow(U8_MAX
, 2) * st
->rsense
);
542 static int ltc4282_read_energy(const struct ltc4282_state
*st
, u64
*val
)
548 ret
= regmap_bulk_read(st
->map
, LTC4282_ENERGY
, &raw
, 6);
552 energy
= be64_to_cpu(raw
) >> 16;
554 * The formula for energy is given by:
555 * E = CODE(48b) * 0.040 * Vfs(out) * Tconv * 256 /
556 * ((2^16 - 1)^2 * Rsense)
558 * Since we only support 12bit ADC, Tconv = 0.065535s. Passing Vfs(out)
559 * and 0.040 to mV and Tconv to us, we can simplify the formula to:
560 * E = CODE(48b) * 40 * Vfs(out) * 256 / (U16_MAX * Rsense)
562 * As Rsense can have tenths of micro-ohm resolution, we need to
563 * multiply by DECA to get microujoule.
565 if (check_mul_overflow(DECA
* st
->vfs_out
* 40 * BIT(8), energy
, &temp
)) {
566 temp
= DIV_ROUND_CLOSEST(DECA
* st
->vfs_out
* 40 * BIT(8), U16_MAX
);
567 *val
= DIV_ROUND_CLOSEST_ULL(temp
* energy
, st
->rsense
);
571 *val
= DIV64_U64_ROUND_CLOSEST(temp
, U16_MAX
* (u64
)st
->rsense
);
576 static int ltc4282_read_power(struct ltc4282_state
*st
, const u32 attr
,
580 case hwmon_power_input
:
581 return ltc4282_read_power_word(st
, LTC4282_POWER
, val
);
582 case hwmon_power_input_highest
:
583 return ltc4282_read_power_word(st
, LTC4282_POWER_HIGHEST
, val
);
584 case hwmon_power_input_lowest
:
585 return ltc4282_read_power_word(st
, LTC4282_POWER_LOWEST
, val
);
586 case hwmon_power_max_alarm
:
587 return ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
588 LTC4282_POWER_ALARM_H_MASK
, val
);
589 case hwmon_power_min_alarm
:
590 return ltc4282_read_alarm(st
, LTC4282_ADC_ALERT_LOG
,
591 LTC4282_POWER_ALARM_L_MASK
, val
);
592 case hwmon_power_max
:
593 return ltc4282_read_power_byte(st
, LTC4282_POWER_MAX
, val
);
594 case hwmon_power_min
:
595 return ltc4282_read_power_byte(st
, LTC4282_POWER_MIN
, val
);
601 static int ltc4282_read(struct device
*dev
, enum hwmon_sensor_types type
,
602 u32 attr
, int channel
, long *val
)
604 struct ltc4282_state
*st
= dev_get_drvdata(dev
);
608 return ltc4282_read_in(st
, attr
, val
, channel
);
610 return ltc4282_read_curr(st
, attr
, val
);
612 return ltc4282_read_power(st
, attr
, val
);
614 scoped_guard(mutex
, &st
->lock
) {
615 *val
= st
->energy_en
;
623 static int ltc4282_write_power_byte(const struct ltc4282_state
*st
, u32 reg
,
629 if (val
> st
->power_max
)
632 temp
= val
* int_pow(U8_MAX
, 2) * st
->rsense
;
633 power
= DIV64_U64_ROUND_CLOSEST(temp
,
634 MICRO
* DECA
* 256ULL * st
->vfs_out
* 40);
636 return regmap_write(st
->map
, reg
, power
);
639 static int ltc4282_write_power_word(const struct ltc4282_state
*st
, u32 reg
,
642 u64 temp
= int_pow(U16_MAX
, 2) * st
->rsense
, temp_2
;
646 if (check_mul_overflow(temp
, val
, &temp_2
)) {
647 temp
= DIV_ROUND_CLOSEST_ULL(temp
, DECA
* MICRO
);
648 code
= DIV64_U64_ROUND_CLOSEST(temp
* val
,
649 40ULL * BIT(16) * st
->vfs_out
);
651 temp
= DECA
* MICRO
* 40ULL * BIT(16) * st
->vfs_out
;
652 code
= DIV64_U64_ROUND_CLOSEST(temp_2
, temp
);
655 __raw
= cpu_to_be16(code
);
656 return regmap_bulk_write(st
->map
, reg
, &__raw
, sizeof(__raw
));
659 static int __ltc4282_in_write_history(const struct ltc4282_state
*st
, u32 reg
,
660 long lowest
, long highest
, u32 fs
)
666 tmp
= DIV_ROUND_CLOSEST(U16_MAX
* lowest
, fs
);
668 __raw
= cpu_to_be16(tmp
);
670 ret
= regmap_bulk_write(st
->map
, reg
, &__raw
, 2);
674 tmp
= DIV_ROUND_CLOSEST(U16_MAX
* highest
, fs
);
676 __raw
= cpu_to_be16(tmp
);
678 return regmap_bulk_write(st
->map
, reg
+ 2, &__raw
, 2);
681 static int ltc4282_in_write_history(struct ltc4282_state
*st
, u32 reg
,
682 long lowest
, long highest
, u32 fs
)
684 guard(mutex
)(&st
->lock
);
685 return __ltc4282_in_write_history(st
, reg
, lowest
, highest
, fs
);
688 static int ltc4282_power_reset_hist(struct ltc4282_state
*st
)
692 guard(mutex
)(&st
->lock
);
694 ret
= ltc4282_write_power_word(st
, LTC4282_POWER_LOWEST
,
699 ret
= ltc4282_write_power_word(st
, LTC4282_POWER_HIGHEST
, 0);
703 /* now, let's also clear possible power_bad fault logs */
704 return regmap_clear_bits(st
->map
, LTC4282_FAULT_LOG
,
705 LTC4282_POWER_BAD_FAULT_MASK
);
708 static int ltc4282_write_power(struct ltc4282_state
*st
, u32 attr
,
712 case hwmon_power_max
:
713 return ltc4282_write_power_byte(st
, LTC4282_POWER_MAX
, val
);
714 case hwmon_power_min
:
715 return ltc4282_write_power_byte(st
, LTC4282_POWER_MIN
, val
);
716 case hwmon_power_reset_history
:
717 return ltc4282_power_reset_hist(st
);
723 static int ltc4282_write_voltage_byte_cached(const struct ltc4282_state
*st
,
724 u32 reg
, u32 fs
, long val
,
729 val
= clamp_val(val
, 0, fs
);
730 in
= DIV_ROUND_CLOSEST(val
* U8_MAX
, fs
);
737 return regmap_write(st
->map
, reg
, in
);
740 static int ltc4282_write_voltage_byte(const struct ltc4282_state
*st
, u32 reg
,
743 return ltc4282_write_voltage_byte_cached(st
, reg
, fs
, val
, NULL
);
746 static int ltc4282_cache_history(struct ltc4282_state
*st
, u32 channel
)
751 ret
= ltc4282_read_voltage_word(st
, LTC4282_VSOURCE_LOWEST
, st
->vfs_out
,
756 st
->in0_1_cache
[channel
].in_lowest
= val
;
758 ret
= ltc4282_read_voltage_word(st
, LTC4282_VSOURCE_HIGHEST
,
763 st
->in0_1_cache
[channel
].in_highest
= val
;
765 ret
= regmap_read(st
->map
, LTC4282_VSOURCE_MIN
,
766 &st
->in0_1_cache
[channel
].in_min_raw
);
770 return regmap_read(st
->map
, LTC4282_VSOURCE_MAX
,
771 &st
->in0_1_cache
[channel
].in_max_raw
);
774 static int ltc4282_cache_sync(struct ltc4282_state
*st
, u32 channel
)
778 ret
= __ltc4282_in_write_history(st
, LTC4282_VSOURCE_LOWEST
,
779 st
->in0_1_cache
[channel
].in_lowest
,
780 st
->in0_1_cache
[channel
].in_highest
,
785 ret
= regmap_write(st
->map
, LTC4282_VSOURCE_MIN
,
786 st
->in0_1_cache
[channel
].in_min_raw
);
790 return regmap_write(st
->map
, LTC4282_VSOURCE_MAX
,
791 st
->in0_1_cache
[channel
].in_max_raw
);
794 static int ltc4282_vdd_source_write_lim(struct ltc4282_state
*st
, u32 reg
,
795 int channel
, u32
*cache
, long val
)
799 guard(mutex
)(&st
->lock
);
800 if (st
->in0_1_cache
[channel
].en
)
801 ret
= ltc4282_write_voltage_byte(st
, reg
, st
->vfs_out
, val
);
803 ret
= ltc4282_write_voltage_byte_cached(st
, reg
, st
->vfs_out
,
809 static int ltc4282_vdd_source_reset_hist(struct ltc4282_state
*st
, int channel
)
811 long lowest
= st
->vfs_out
;
814 if (channel
== LTC4282_CHAN_VDD
)
817 guard(mutex
)(&st
->lock
);
818 if (st
->in0_1_cache
[channel
].en
) {
819 ret
= __ltc4282_in_write_history(st
, LTC4282_VSOURCE_LOWEST
,
820 lowest
, 0, st
->vfs_out
);
825 st
->in0_1_cache
[channel
].in_lowest
= lowest
;
826 st
->in0_1_cache
[channel
].in_highest
= 0;
829 * We are also clearing possible fault logs in reset_history. Clearing
830 * the logs might be important when the auto retry bits are not enabled
831 * as the chip only enables the output again after having these logs
832 * cleared. As some of these logs are related to limits, it makes sense
833 * to clear them in here. For VDD, we need to clear under/over voltage
834 * events. For VSOURCE, fet_short and fet_bad...
836 if (channel
== LTC4282_CHAN_VSOURCE
)
837 return regmap_clear_bits(st
->map
, LTC4282_FAULT_LOG
,
838 LTC4282_FET_FAILURE_FAULT_MASK
);
840 return regmap_clear_bits(st
->map
, LTC4282_FAULT_LOG
,
841 LTC4282_VDD_FAULT_MASK
);
845 * We need to mux between VSOURCE and VDD which means they are mutually
846 * exclusive. Moreover, we can't really disable both VDD and VSOURCE as the ADC
847 * is continuously running (we cannot independently halt it without also
848 * stopping VGPIO). Hence, the logic is that disabling or enabling VDD will
849 * automatically have the reverse effect on VSOURCE and vice-versa.
851 static int ltc4282_vdd_source_enable(struct ltc4282_state
*st
, int channel
,
854 int ret
, other_chan
= ~channel
& 0x1;
857 guard(mutex
)(&st
->lock
);
858 if (st
->in0_1_cache
[channel
].en
== !!val
)
861 /* clearing the bit makes the ADC to monitor VDD */
862 if (channel
== LTC4282_CHAN_VDD
)
865 ret
= regmap_update_bits(st
->map
, LTC4282_ILIM_ADJUST
,
866 LTC4282_VDD_MONITOR_MASK
,
867 FIELD_PREP(LTC4282_VDD_MONITOR_MASK
, !!__val
));
871 st
->in0_1_cache
[channel
].en
= !!val
;
872 st
->in0_1_cache
[other_chan
].en
= !val
;
874 if (st
->in0_1_cache
[channel
].en
) {
876 * Then, we are disabling @other_chan. Let's save it's current
879 ret
= ltc4282_cache_history(st
, other_chan
);
883 return ltc4282_cache_sync(st
, channel
);
886 * Then, we are enabling @other_chan. We need to do the opposite from
889 ret
= ltc4282_cache_history(st
, channel
);
893 return ltc4282_cache_sync(st
, other_chan
);
896 static int ltc4282_write_in(struct ltc4282_state
*st
, u32 attr
, long val
,
901 if (channel
== LTC4282_CHAN_VGPIO
)
902 return ltc4282_write_voltage_byte(st
, LTC4282_VGPIO_MAX
,
905 return ltc4282_vdd_source_write_lim(st
, LTC4282_VSOURCE_MAX
,
907 &st
->in0_1_cache
[channel
].in_max_raw
, val
);
909 if (channel
== LTC4282_CHAN_VGPIO
)
910 return ltc4282_write_voltage_byte(st
, LTC4282_VGPIO_MIN
,
913 return ltc4282_vdd_source_write_lim(st
, LTC4282_VSOURCE_MIN
,
915 &st
->in0_1_cache
[channel
].in_min_raw
, val
);
916 case hwmon_in_reset_history
:
917 if (channel
== LTC4282_CHAN_VGPIO
)
918 return ltc4282_in_write_history(st
,
919 LTC4282_VGPIO_LOWEST
,
922 return ltc4282_vdd_source_reset_hist(st
, channel
);
923 case hwmon_in_enable
:
924 return ltc4282_vdd_source_enable(st
, channel
, val
);
930 static int ltc4282_curr_reset_hist(struct ltc4282_state
*st
)
934 guard(mutex
)(&st
->lock
);
936 ret
= __ltc4282_in_write_history(st
, LTC4282_VSENSE_LOWEST
,
937 st
->vsense_max
, 0, 40 * MILLI
);
941 /* now, let's also clear possible overcurrent fault logs */
942 return regmap_clear_bits(st
->map
, LTC4282_FAULT_LOG
,
943 LTC4282_OC_FAULT_MASK
);
946 static int ltc4282_write_curr(struct ltc4282_state
*st
, u32 attr
,
949 /* need to pass it in millivolt */
950 u32 in
= DIV_ROUND_CLOSEST_ULL((u64
)val
* st
->rsense
, DECA
* MICRO
);
954 return ltc4282_write_voltage_byte(st
, LTC4282_VSENSE_MAX
, 40,
957 return ltc4282_write_voltage_byte(st
, LTC4282_VSENSE_MIN
, 40,
959 case hwmon_curr_reset_history
:
960 return ltc4282_curr_reset_hist(st
);
966 static int ltc4282_energy_enable_set(struct ltc4282_state
*st
, long val
)
970 guard(mutex
)(&st
->lock
);
971 /* setting the bit halts the meter */
972 ret
= regmap_update_bits(st
->map
, LTC4282_ADC_CTRL
,
973 LTC4282_METER_HALT_MASK
,
974 FIELD_PREP(LTC4282_METER_HALT_MASK
, !val
));
978 st
->energy_en
= !!val
;
983 static int ltc4282_write(struct device
*dev
,
984 enum hwmon_sensor_types type
,
985 u32 attr
, int channel
, long val
)
987 struct ltc4282_state
*st
= dev_get_drvdata(dev
);
991 return ltc4282_write_power(st
, attr
, val
);
993 return ltc4282_write_in(st
, attr
, val
, channel
);
995 return ltc4282_write_curr(st
, attr
, val
);
997 return ltc4282_energy_enable_set(st
, val
);
1003 static umode_t
ltc4282_in_is_visible(const struct ltc4282_state
*st
, u32 attr
)
1006 case hwmon_in_input
:
1007 case hwmon_in_highest
:
1008 case hwmon_in_lowest
:
1009 case hwmon_in_max_alarm
:
1010 case hwmon_in_min_alarm
:
1011 case hwmon_in_label
:
1012 case hwmon_in_lcrit_alarm
:
1013 case hwmon_in_crit_alarm
:
1014 case hwmon_in_fault
:
1018 case hwmon_in_enable
:
1019 case hwmon_in_reset_history
:
1026 static umode_t
ltc4282_curr_is_visible(u32 attr
)
1029 case hwmon_curr_input
:
1030 case hwmon_curr_highest
:
1031 case hwmon_curr_lowest
:
1032 case hwmon_curr_max_alarm
:
1033 case hwmon_curr_min_alarm
:
1034 case hwmon_curr_crit_alarm
:
1035 case hwmon_curr_label
:
1037 case hwmon_curr_max
:
1038 case hwmon_curr_min
:
1039 case hwmon_curr_reset_history
:
1046 static umode_t
ltc4282_power_is_visible(u32 attr
)
1049 case hwmon_power_input
:
1050 case hwmon_power_input_highest
:
1051 case hwmon_power_input_lowest
:
1052 case hwmon_power_label
:
1053 case hwmon_power_max_alarm
:
1054 case hwmon_power_min_alarm
:
1056 case hwmon_power_max
:
1057 case hwmon_power_min
:
1058 case hwmon_power_reset_history
:
1065 static umode_t
ltc4282_is_visible(const void *data
,
1066 enum hwmon_sensor_types type
,
1067 u32 attr
, int channel
)
1071 return ltc4282_in_is_visible(data
, attr
);
1073 return ltc4282_curr_is_visible(attr
);
1075 return ltc4282_power_is_visible(attr
);
1077 /* hwmon_energy_enable */
1084 static const char * const ltc4282_in_strs
[] = {
1085 "VSOURCE", "VDD", "VGPIO"
1088 static int ltc4282_read_labels(struct device
*dev
,
1089 enum hwmon_sensor_types type
,
1090 u32 attr
, int channel
, const char **str
)
1094 *str
= ltc4282_in_strs
[channel
];
1107 static ssize_t
ltc4282_energy_show(struct device
*dev
,
1108 struct device_attribute
*da
, char *buf
)
1110 struct ltc4282_state
*st
= dev_get_drvdata(dev
);
1114 guard(mutex
)(&st
->lock
);
1118 ret
= ltc4282_read_energy(st
, &energy
);
1122 return sysfs_emit(buf
, "%llu\n", energy
);
1125 static const struct clk_ops ltc4282_ops
= {
1126 .recalc_rate
= ltc4282_recalc_rate
,
1127 .round_rate
= ltc4282_round_rate
,
1128 .set_rate
= ltc4282_set_rate
,
1129 .disable
= ltc4282_disable
,
1132 static int ltc428_clk_provider_setup(struct ltc4282_state
*st
,
1135 struct clk_init_data init
;
1138 if (!IS_ENABLED(CONFIG_COMMON_CLK
))
1141 init
.name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-clk",
1142 fwnode_get_name(dev_fwnode(dev
)));
1146 init
.ops
= <c4282_ops
;
1147 init
.flags
= CLK_GET_RATE_NOCACHE
;
1148 st
->clk_hw
.init
= &init
;
1150 ret
= devm_clk_hw_register(dev
, &st
->clk_hw
);
1154 return devm_of_clk_add_hw_provider(dev
, of_clk_hw_simple_get
,
1158 static int ltc428_clks_setup(struct ltc4282_state
*st
, struct device
*dev
)
1165 ret
= ltc428_clk_provider_setup(st
, dev
);
1169 clkin
= devm_clk_get_optional_enabled(dev
, NULL
);
1171 return dev_err_probe(dev
, PTR_ERR(clkin
),
1172 "Failed to get clkin");
1176 rate
= clk_get_rate(clkin
);
1177 if (!in_range(rate
, LTC4282_CLKIN_MIN
, LTC4282_CLKIN_RANGE
))
1178 return dev_err_probe(dev
, -EINVAL
,
1179 "Invalid clkin range(%lu) [%lu %lu]\n",
1180 rate
, LTC4282_CLKIN_MIN
,
1184 * Clocks faster than 250KHZ should be reduced to 250KHZ. The clock
1185 * frequency is divided by twice the value in the register.
1187 val
= rate
/ (2 * LTC4282_CLKIN_MIN
);
1189 return regmap_update_bits(st
->map
, LTC4282_CLK_DIV
,
1190 LTC4282_CLK_DIV_MASK
,
1191 FIELD_PREP(LTC4282_CLK_DIV_MASK
, val
));
1194 static const int ltc4282_curr_lim_uv
[] = {
1195 12500, 15625, 18750, 21875, 25000, 28125, 31250, 34375
1198 static int ltc4282_get_defaults(struct ltc4282_state
*st
, u32
*vin_mode
)
1200 u32 reg_val
, ilm_adjust
;
1203 ret
= regmap_read(st
->map
, LTC4282_ADC_CTRL
, ®_val
);
1207 st
->energy_en
= !FIELD_GET(LTC4282_METER_HALT_MASK
, reg_val
);
1209 ret
= regmap_read(st
->map
, LTC4282_CTRL_MSB
, ®_val
);
1213 *vin_mode
= FIELD_GET(LTC4282_CTRL_VIN_MODE_MASK
, reg_val
);
1215 ret
= regmap_read(st
->map
, LTC4282_ILIM_ADJUST
, ®_val
);
1219 ilm_adjust
= FIELD_GET(LTC4282_ILIM_ADJUST_MASK
, reg_val
);
1220 st
->vsense_max
= ltc4282_curr_lim_uv
[ilm_adjust
];
1222 st
->in0_1_cache
[LTC4282_CHAN_VSOURCE
].en
= FIELD_GET(LTC4282_VDD_MONITOR_MASK
,
1224 if (!st
->in0_1_cache
[LTC4282_CHAN_VSOURCE
].en
) {
1225 st
->in0_1_cache
[LTC4282_CHAN_VDD
].en
= true;
1226 return regmap_read(st
->map
, LTC4282_VSOURCE_MAX
,
1227 &st
->in0_1_cache
[LTC4282_CHAN_VSOURCE
].in_max_raw
);
1230 return regmap_read(st
->map
, LTC4282_VSOURCE_MAX
,
1231 &st
->in0_1_cache
[LTC4282_CHAN_VDD
].in_max_raw
);
1235 * Set max limits for ISENSE and Power as that depends on the max voltage on
1236 * rsense that is defined in ILIM_ADJUST. This is specially important for power
1237 * because for some rsense and vfsout values, if we allow the default raw 255
1238 * value, that would overflow long in 32bit archs when reading back the max
1241 * Also set meaningful historic values for VDD and VSOURCE
1242 * (0 would not mean much).
1244 static int ltc4282_set_max_limits(struct ltc4282_state
*st
)
1248 ret
= ltc4282_write_voltage_byte(st
, LTC4282_VSENSE_MAX
, 40 * MILLI
,
1253 /* Power is given by ISENSE * Vout. */
1254 st
->power_max
= DIV_ROUND_CLOSEST(st
->vsense_max
* DECA
* MILLI
, st
->rsense
) * st
->vfs_out
;
1255 ret
= ltc4282_write_power_byte(st
, LTC4282_POWER_MAX
, st
->power_max
);
1259 if (st
->in0_1_cache
[LTC4282_CHAN_VDD
].en
) {
1260 st
->in0_1_cache
[LTC4282_CHAN_VSOURCE
].in_lowest
= st
->vfs_out
;
1261 return __ltc4282_in_write_history(st
, LTC4282_VSOURCE_LOWEST
,
1262 st
->vdd
, 0, st
->vfs_out
);
1265 st
->in0_1_cache
[LTC4282_CHAN_VDD
].in_lowest
= st
->vdd
;
1266 return __ltc4282_in_write_history(st
, LTC4282_VSOURCE_LOWEST
,
1267 st
->vfs_out
, 0, st
->vfs_out
);
1270 static const char * const ltc4282_gpio1_modes
[] = {
1271 "power_bad", "power_good"
1274 static const char * const ltc4282_gpio2_modes
[] = {
1275 "adc_input", "stress_fet"
1278 static int ltc4282_gpio_setup(struct ltc4282_state
*st
, struct device
*dev
)
1280 const char *func
= NULL
;
1283 ret
= device_property_read_string(dev
, "adi,gpio1-mode", &func
);
1285 ret
= match_string(ltc4282_gpio1_modes
,
1286 ARRAY_SIZE(ltc4282_gpio1_modes
), func
);
1288 return dev_err_probe(dev
, ret
,
1289 "Invalid func(%s) for gpio1\n",
1292 ret
= regmap_update_bits(st
->map
, LTC4282_GPIO_CONFIG
,
1293 LTC4282_GPIO_1_CONFIG_MASK
,
1294 FIELD_PREP(LTC4282_GPIO_1_CONFIG_MASK
, ret
));
1299 ret
= device_property_read_string(dev
, "adi,gpio2-mode", &func
);
1301 ret
= match_string(ltc4282_gpio2_modes
,
1302 ARRAY_SIZE(ltc4282_gpio2_modes
), func
);
1304 return dev_err_probe(dev
, ret
,
1305 "Invalid func(%s) for gpio2\n",
1308 /* setting the bit to 1 so the ADC to monitors GPIO2 */
1309 ret
= regmap_set_bits(st
->map
, LTC4282_ILIM_ADJUST
,
1310 LTC4282_GPIO_MODE_MASK
);
1312 ret
= regmap_update_bits(st
->map
, LTC4282_GPIO_CONFIG
,
1313 LTC4282_GPIO_2_FET_STRESS_MASK
,
1314 FIELD_PREP(LTC4282_GPIO_2_FET_STRESS_MASK
, 1));
1321 if (!device_property_read_bool(dev
, "adi,gpio3-monitor-enable"))
1324 if (func
&& !strcmp(func
, "adc_input"))
1325 return dev_err_probe(dev
, -EINVAL
,
1326 "Cannot have both gpio2 and gpio3 muxed into the ADC");
1328 return regmap_clear_bits(st
->map
, LTC4282_ILIM_ADJUST
,
1329 LTC4282_GPIO_MODE_MASK
);
1332 static const char * const ltc4282_dividers
[] = {
1333 "external", "vdd_5_percent", "vdd_10_percent", "vdd_15_percent"
1336 /* This maps the Vout full scale for the given Vin mode */
1337 static const u16 ltc4282_vfs_milli
[] = { 5540, 8320, 16640, 33280 };
1339 static const u16 ltc4282_vdd_milli
[] = { 3300, 5000, 12000, 24000 };
1348 static int ltc4282_setup(struct ltc4282_state
*st
, struct device
*dev
)
1350 const char *divider
;
1354 /* The part has an eeprom so let's get the needed defaults from it */
1355 ret
= ltc4282_get_defaults(st
, &vin_mode
);
1359 ret
= device_property_read_u32(dev
, "adi,rsense-nano-ohms",
1362 return dev_err_probe(dev
, ret
,
1363 "Failed to read adi,rsense-nano-ohms\n");
1364 if (st
->rsense
< CENTI
)
1365 return dev_err_probe(dev
, -EINVAL
,
1366 "adi,rsense-nano-ohms too small (< %lu)\n",
1370 * The resolution for rsense is tenths of micro (eg: 62.5 uOhm) which
1371 * means we need nano in the bindings. However, to make things easier to
1372 * handle (with respect to overflows) we divide it by 100 as we don't
1373 * really need the last two digits.
1375 st
->rsense
/= CENTI
;
1378 ret
= device_property_read_u32(dev
, "adi,vin-mode-microvolt", &val
);
1382 val
= LTC4282_VIN_3_3V
;
1385 val
= LTC4282_VIN_5V
;
1388 val
= LTC4282_VIN_12V
;
1391 val
= LTC4282_VIN_24V
;
1394 return dev_err_probe(dev
, -EINVAL
,
1395 "Invalid val(%u) for vin-mode-microvolt\n",
1399 ret
= regmap_update_bits(st
->map
, LTC4282_CTRL_MSB
,
1400 LTC4282_CTRL_VIN_MODE_MASK
,
1401 FIELD_PREP(LTC4282_CTRL_VIN_MODE_MASK
, val
));
1405 /* Foldback mode should also be set to the input voltage */
1406 ret
= regmap_update_bits(st
->map
, LTC4282_ILIM_ADJUST
,
1407 LTC4282_FOLDBACK_MODE_MASK
,
1408 FIELD_PREP(LTC4282_FOLDBACK_MODE_MASK
, val
));
1413 st
->vfs_out
= ltc4282_vfs_milli
[val
];
1414 st
->vdd
= ltc4282_vdd_milli
[val
];
1416 ret
= device_property_read_u32(dev
, "adi,current-limit-sense-microvolt",
1447 return dev_err_probe(dev
, -EINVAL
,
1448 "Invalid val(%u) for adi,current-limit-microvolt\n",
1452 ret
= regmap_update_bits(st
->map
, LTC4282_ILIM_ADJUST
,
1453 LTC4282_ILIM_ADJUST_MASK
,
1454 FIELD_PREP(LTC4282_ILIM_ADJUST_MASK
, reg_val
));
1459 ret
= ltc4282_set_max_limits(st
);
1463 ret
= device_property_read_string(dev
, "adi,overvoltage-dividers",
1466 int div
= match_string(ltc4282_dividers
,
1467 ARRAY_SIZE(ltc4282_dividers
), divider
);
1469 return dev_err_probe(dev
, -EINVAL
,
1470 "Invalid val(%s) for adi,overvoltage-divider\n",
1473 ret
= regmap_update_bits(st
->map
, LTC4282_CTRL_MSB
,
1474 LTC4282_CTRL_OV_MODE_MASK
,
1475 FIELD_PREP(LTC4282_CTRL_OV_MODE_MASK
, div
));
1478 ret
= device_property_read_string(dev
, "adi,undervoltage-dividers",
1481 int div
= match_string(ltc4282_dividers
,
1482 ARRAY_SIZE(ltc4282_dividers
), divider
);
1484 return dev_err_probe(dev
, -EINVAL
,
1485 "Invalid val(%s) for adi,undervoltage-divider\n",
1488 ret
= regmap_update_bits(st
->map
, LTC4282_CTRL_MSB
,
1489 LTC4282_CTRL_UV_MODE_MASK
,
1490 FIELD_PREP(LTC4282_CTRL_UV_MODE_MASK
, div
));
1493 if (device_property_read_bool(dev
, "adi,overcurrent-retry")) {
1494 ret
= regmap_set_bits(st
->map
, LTC4282_CTRL_LSB
,
1495 LTC4282_CTRL_OC_RETRY_MASK
);
1500 if (device_property_read_bool(dev
, "adi,overvoltage-retry-disable")) {
1501 ret
= regmap_clear_bits(st
->map
, LTC4282_CTRL_LSB
,
1502 LTC4282_CTRL_OV_RETRY_MASK
);
1507 if (device_property_read_bool(dev
, "adi,undervoltage-retry-disable")) {
1508 ret
= regmap_clear_bits(st
->map
, LTC4282_CTRL_LSB
,
1509 LTC4282_CTRL_UV_RETRY_MASK
);
1514 if (device_property_read_bool(dev
, "adi,fault-log-enable")) {
1515 ret
= regmap_set_bits(st
->map
, LTC4282_ADC_CTRL
,
1516 LTC4282_FAULT_LOG_EN_MASK
);
1521 if (device_property_read_bool(dev
, "adi,fault-log-enable")) {
1522 ret
= regmap_set_bits(st
->map
, LTC4282_ADC_CTRL
, LTC4282_FAULT_LOG_EN_MASK
);
1527 ret
= device_property_read_u32(dev
, "adi,fet-bad-timeout-ms", &val
);
1529 if (val
> LTC4282_FET_BAD_MAX_TIMEOUT
)
1530 return dev_err_probe(dev
, -EINVAL
,
1531 "Invalid value(%u) for adi,fet-bad-timeout-ms",
1534 ret
= regmap_write(st
->map
, LTC4282_FET_BAD_FAULT_TIMEOUT
, val
);
1539 return ltc4282_gpio_setup(st
, dev
);
1542 static bool ltc4282_readable_reg(struct device
*dev
, unsigned int reg
)
1544 if (reg
== LTC4282_RESERVED_1
|| reg
== LTC4282_RESERVED_2
)
1550 static bool ltc4282_writable_reg(struct device
*dev
, unsigned int reg
)
1552 if (reg
== LTC4282_STATUS_LSB
|| reg
== LTC4282_STATUS_MSB
)
1554 if (reg
== LTC4282_RESERVED_1
|| reg
== LTC4282_RESERVED_2
)
1560 static const struct regmap_config ltc4282_regmap_config
= {
1563 .max_register
= LTC4282_RESERVED_3
,
1564 .readable_reg
= ltc4282_readable_reg
,
1565 .writeable_reg
= ltc4282_writable_reg
,
1568 static const struct hwmon_channel_info
* const ltc4282_info
[] = {
1569 HWMON_CHANNEL_INFO(in
,
1570 HWMON_I_INPUT
| HWMON_I_LOWEST
| HWMON_I_HIGHEST
|
1571 HWMON_I_MAX
| HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
1572 HWMON_I_MAX_ALARM
| HWMON_I_ENABLE
|
1573 HWMON_I_RESET_HISTORY
| HWMON_I_FAULT
|
1575 HWMON_I_INPUT
| HWMON_I_LOWEST
| HWMON_I_HIGHEST
|
1576 HWMON_I_MAX
| HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
1577 HWMON_I_MAX_ALARM
| HWMON_I_LCRIT_ALARM
|
1578 HWMON_I_CRIT_ALARM
| HWMON_I_ENABLE
|
1579 HWMON_I_RESET_HISTORY
| HWMON_I_LABEL
,
1580 HWMON_I_INPUT
| HWMON_I_LOWEST
| HWMON_I_HIGHEST
|
1581 HWMON_I_MAX
| HWMON_I_MIN
| HWMON_I_MIN_ALARM
|
1582 HWMON_I_RESET_HISTORY
| HWMON_I_MAX_ALARM
|
1584 HWMON_CHANNEL_INFO(curr
,
1585 HWMON_C_INPUT
| HWMON_C_LOWEST
| HWMON_C_HIGHEST
|
1586 HWMON_C_MAX
| HWMON_C_MIN
| HWMON_C_MIN_ALARM
|
1587 HWMON_C_MAX_ALARM
| HWMON_C_CRIT_ALARM
|
1588 HWMON_C_RESET_HISTORY
| HWMON_C_LABEL
),
1589 HWMON_CHANNEL_INFO(power
,
1590 HWMON_P_INPUT
| HWMON_P_INPUT_LOWEST
|
1591 HWMON_P_INPUT_HIGHEST
| HWMON_P_MAX
| HWMON_P_MIN
|
1592 HWMON_P_MAX_ALARM
| HWMON_P_MIN_ALARM
|
1593 HWMON_P_RESET_HISTORY
| HWMON_P_LABEL
),
1594 HWMON_CHANNEL_INFO(energy
,
1599 static const struct hwmon_ops ltc4282_hwmon_ops
= {
1600 .read
= ltc4282_read
,
1601 .write
= ltc4282_write
,
1602 .is_visible
= ltc4282_is_visible
,
1603 .read_string
= ltc4282_read_labels
,
1606 static const struct hwmon_chip_info ltc2947_chip_info
= {
1607 .ops
= <c4282_hwmon_ops
,
1608 .info
= ltc4282_info
,
1611 /* energy attributes are 6bytes wide so we need u64 */
1612 static SENSOR_DEVICE_ATTR_RO(energy1_input
, ltc4282_energy
, 0);
1614 static struct attribute
*ltc4282_attrs
[] = {
1615 &sensor_dev_attr_energy1_input
.dev_attr
.attr
,
1618 ATTRIBUTE_GROUPS(ltc4282
);
1620 static int ltc4282_show_fault_log(void *arg
, u64
*val
, u32 mask
)
1622 struct ltc4282_state
*st
= arg
;
1626 ret
= ltc4282_read_alarm(st
, LTC4282_FAULT_LOG
, mask
, &alarm
);
1635 static int ltc4282_show_curr1_crit_fault_log(void *arg
, u64
*val
)
1637 return ltc4282_show_fault_log(arg
, val
, LTC4282_OC_FAULT_MASK
);
1639 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_curr1_crit_fault_log
,
1640 ltc4282_show_curr1_crit_fault_log
, NULL
, "%llu\n");
1642 static int ltc4282_show_in1_lcrit_fault_log(void *arg
, u64
*val
)
1644 return ltc4282_show_fault_log(arg
, val
, LTC4282_UV_FAULT_MASK
);
1646 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_in1_lcrit_fault_log
,
1647 ltc4282_show_in1_lcrit_fault_log
, NULL
, "%llu\n");
1649 static int ltc4282_show_in1_crit_fault_log(void *arg
, u64
*val
)
1651 return ltc4282_show_fault_log(arg
, val
, LTC4282_OV_FAULT_MASK
);
1653 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_in1_crit_fault_log
,
1654 ltc4282_show_in1_crit_fault_log
, NULL
, "%llu\n");
1656 static int ltc4282_show_fet_bad_fault_log(void *arg
, u64
*val
)
1658 return ltc4282_show_fault_log(arg
, val
, LTC4282_FET_BAD_FAULT_MASK
);
1660 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_fet_bad_fault_log
,
1661 ltc4282_show_fet_bad_fault_log
, NULL
, "%llu\n");
1663 static int ltc4282_show_fet_short_fault_log(void *arg
, u64
*val
)
1665 return ltc4282_show_fault_log(arg
, val
, LTC4282_FET_SHORT_FAULT_MASK
);
1667 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_fet_short_fault_log
,
1668 ltc4282_show_fet_short_fault_log
, NULL
, "%llu\n");
1670 static int ltc4282_show_power1_bad_fault_log(void *arg
, u64
*val
)
1672 return ltc4282_show_fault_log(arg
, val
, LTC4282_POWER_BAD_FAULT_MASK
);
1674 DEFINE_DEBUGFS_ATTRIBUTE(ltc4282_power1_bad_fault_log
,
1675 ltc4282_show_power1_bad_fault_log
, NULL
, "%llu\n");
1677 static void ltc4282_debugfs_remove(void *dir
)
1679 debugfs_remove_recursive(dir
);
1682 static void ltc4282_debugfs_init(struct ltc4282_state
*st
,
1683 struct i2c_client
*i2c
,
1684 const struct device
*hwmon
)
1686 const char *debugfs_name
;
1687 struct dentry
*dentry
;
1690 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
1693 debugfs_name
= devm_kasprintf(&i2c
->dev
, GFP_KERNEL
, "ltc4282-%s",
1698 dentry
= debugfs_create_dir(debugfs_name
, NULL
);
1702 ret
= devm_add_action_or_reset(&i2c
->dev
, ltc4282_debugfs_remove
,
1707 debugfs_create_file_unsafe("power1_bad_fault_log", 0400, dentry
, st
,
1708 <c4282_power1_bad_fault_log
);
1709 debugfs_create_file_unsafe("in0_fet_short_fault_log", 0400, dentry
, st
,
1710 <c4282_fet_short_fault_log
);
1711 debugfs_create_file_unsafe("in0_fet_bad_fault_log", 0400, dentry
, st
,
1712 <c4282_fet_bad_fault_log
);
1713 debugfs_create_file_unsafe("in1_crit_fault_log", 0400, dentry
, st
,
1714 <c4282_in1_crit_fault_log
);
1715 debugfs_create_file_unsafe("in1_lcrit_fault_log", 0400, dentry
, st
,
1716 <c4282_in1_lcrit_fault_log
);
1717 debugfs_create_file_unsafe("curr1_crit_fault_log", 0400, dentry
, st
,
1718 <c4282_curr1_crit_fault_log
);
1721 static int ltc4282_probe(struct i2c_client
*i2c
)
1723 struct device
*dev
= &i2c
->dev
, *hwmon
;
1724 struct ltc4282_state
*st
;
1727 st
= devm_kzalloc(dev
, sizeof(*st
), GFP_KERNEL
);
1729 return dev_err_probe(dev
, -ENOMEM
,
1730 "Failed to allocate memory\n");
1732 st
->map
= devm_regmap_init_i2c(i2c
, <c4282_regmap_config
);
1733 if (IS_ERR(st
->map
))
1734 return dev_err_probe(dev
, PTR_ERR(st
->map
),
1735 "failed regmap init\n");
1738 ret
= regmap_set_bits(st
->map
, LTC4282_ADC_CTRL
, LTC4282_RESET_MASK
);
1742 /* Yes, it's big but it is as specified in the datasheet */
1745 ret
= ltc428_clks_setup(st
, dev
);
1749 ret
= ltc4282_setup(st
, dev
);
1753 mutex_init(&st
->lock
);
1754 hwmon
= devm_hwmon_device_register_with_info(dev
, "ltc4282", st
,
1758 return PTR_ERR(hwmon
);
1760 ltc4282_debugfs_init(st
, i2c
, hwmon
);
1765 static const struct of_device_id ltc4282_of_match
[] = {
1766 { .compatible
= "adi,ltc4282" },
1769 MODULE_DEVICE_TABLE(of
, ltc4282_of_match
);
1771 static struct i2c_driver ltc4282_driver
= {
1774 .of_match_table
= ltc4282_of_match
,
1776 .probe
= ltc4282_probe
,
1778 module_i2c_driver(ltc4282_driver
);
1780 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1781 MODULE_DESCRIPTION("LTC4282 I2C High Current Hot Swap Controller");
1782 MODULE_LICENSE("GPL");