WIP FPC-III support
[linux/fpc-iii.git] / drivers / hwmon / ltc2992.c
blob4382105bf14205d5658401464b839b3fa40c0952
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /*
3 * LTC2992 - Dual Wide Range Power Monitor
5 * Copyright 2020 Analog Devices Inc.
6 */
8 #include <linux/bitfield.h>
9 #include <linux/bitops.h>
10 #include <linux/err.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/hwmon.h>
13 #include <linux/i2c.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
19 #define LTC2992_CTRLB 0x01
20 #define LTC2992_FAULT1 0x03
21 #define LTC2992_POWER1 0x05
22 #define LTC2992_POWER1_MAX 0x08
23 #define LTC2992_POWER1_MIN 0x0B
24 #define LTC2992_POWER1_MAX_THRESH 0x0E
25 #define LTC2992_POWER1_MIN_THRESH 0x11
26 #define LTC2992_DSENSE1 0x14
27 #define LTC2992_DSENSE1_MAX 0x16
28 #define LTC2992_DSENSE1_MIN 0x18
29 #define LTC2992_DSENSE1_MAX_THRESH 0x1A
30 #define LTC2992_DSENSE1_MIN_THRESH 0x1C
31 #define LTC2992_SENSE1 0x1E
32 #define LTC2992_SENSE1_MAX 0x20
33 #define LTC2992_SENSE1_MIN 0x22
34 #define LTC2992_SENSE1_MAX_THRESH 0x24
35 #define LTC2992_SENSE1_MIN_THRESH 0x26
36 #define LTC2992_G1 0x28
37 #define LTC2992_G1_MAX 0x2A
38 #define LTC2992_G1_MIN 0x2C
39 #define LTC2992_G1_MAX_THRESH 0x2E
40 #define LTC2992_G1_MIN_THRESH 0x30
41 #define LTC2992_FAULT2 0x35
42 #define LTC2992_G2 0x5A
43 #define LTC2992_G2_MAX 0x5C
44 #define LTC2992_G2_MIN 0x5E
45 #define LTC2992_G2_MAX_THRESH 0x60
46 #define LTC2992_G2_MIN_THRESH 0x62
47 #define LTC2992_G3 0x64
48 #define LTC2992_G3_MAX 0x66
49 #define LTC2992_G3_MIN 0x68
50 #define LTC2992_G3_MAX_THRESH 0x6A
51 #define LTC2992_G3_MIN_THRESH 0x6C
52 #define LTC2992_G4 0x6E
53 #define LTC2992_G4_MAX 0x70
54 #define LTC2992_G4_MIN 0x72
55 #define LTC2992_G4_MAX_THRESH 0x74
56 #define LTC2992_G4_MIN_THRESH 0x76
57 #define LTC2992_FAULT3 0x92
58 #define LTC2992_GPIO_STATUS 0x95
59 #define LTC2992_GPIO_IO_CTRL 0x96
60 #define LTC2992_GPIO_CTRL 0x97
62 #define LTC2992_POWER(x) (LTC2992_POWER1 + ((x) * 0x32))
63 #define LTC2992_POWER_MAX(x) (LTC2992_POWER1_MAX + ((x) * 0x32))
64 #define LTC2992_POWER_MIN(x) (LTC2992_POWER1_MIN + ((x) * 0x32))
65 #define LTC2992_POWER_MAX_THRESH(x) (LTC2992_POWER1_MAX_THRESH + ((x) * 0x32))
66 #define LTC2992_POWER_MIN_THRESH(x) (LTC2992_POWER1_MIN_THRESH + ((x) * 0x32))
67 #define LTC2992_DSENSE(x) (LTC2992_DSENSE1 + ((x) * 0x32))
68 #define LTC2992_DSENSE_MAX(x) (LTC2992_DSENSE1_MAX + ((x) * 0x32))
69 #define LTC2992_DSENSE_MIN(x) (LTC2992_DSENSE1_MIN + ((x) * 0x32))
70 #define LTC2992_DSENSE_MAX_THRESH(x) (LTC2992_DSENSE1_MAX_THRESH + ((x) * 0x32))
71 #define LTC2992_DSENSE_MIN_THRESH(x) (LTC2992_DSENSE1_MIN_THRESH + ((x) * 0x32))
72 #define LTC2992_SENSE(x) (LTC2992_SENSE1 + ((x) * 0x32))
73 #define LTC2992_SENSE_MAX(x) (LTC2992_SENSE1_MAX + ((x) * 0x32))
74 #define LTC2992_SENSE_MIN(x) (LTC2992_SENSE1_MIN + ((x) * 0x32))
75 #define LTC2992_SENSE_MAX_THRESH(x) (LTC2992_SENSE1_MAX_THRESH + ((x) * 0x32))
76 #define LTC2992_SENSE_MIN_THRESH(x) (LTC2992_SENSE1_MIN_THRESH + ((x) * 0x32))
77 #define LTC2992_POWER_FAULT(x) (LTC2992_FAULT1 + ((x) * 0x32))
78 #define LTC2992_SENSE_FAULT(x) (LTC2992_FAULT1 + ((x) * 0x32))
79 #define LTC2992_DSENSE_FAULT(x) (LTC2992_FAULT1 + ((x) * 0x32))
81 /* CTRLB register bitfields */
82 #define LTC2992_RESET_HISTORY BIT(3)
84 /* FAULT1 FAULT2 registers common bitfields */
85 #define LTC2992_POWER_FAULT_MSK(x) (BIT(6) << (x))
86 #define LTC2992_DSENSE_FAULT_MSK(x) (BIT(4) << (x))
87 #define LTC2992_SENSE_FAULT_MSK(x) (BIT(2) << (x))
89 /* FAULT1 bitfields */
90 #define LTC2992_GPIO1_FAULT_MSK(x) (BIT(0) << (x))
92 /* FAULT2 bitfields */
93 #define LTC2992_GPIO2_FAULT_MSK(x) (BIT(0) << (x))
95 /* FAULT3 bitfields */
96 #define LTC2992_GPIO3_FAULT_MSK(x) (BIT(6) << (x))
97 #define LTC2992_GPIO4_FAULT_MSK(x) (BIT(4) << (x))
99 #define LTC2992_IADC_NANOV_LSB 12500
100 #define LTC2992_VADC_UV_LSB 25000
101 #define LTC2992_VADC_GPIO_UV_LSB 500
103 #define LTC2992_GPIO_NR 4
104 #define LTC2992_GPIO1_BIT 7
105 #define LTC2992_GPIO2_BIT 6
106 #define LTC2992_GPIO3_BIT 0
107 #define LTC2992_GPIO4_BIT 6
108 #define LTC2992_GPIO_BIT(x) (LTC2992_GPIO_NR - (x) - 1)
110 struct ltc2992_state {
111 struct i2c_client *client;
112 struct gpio_chip gc;
113 struct mutex gpio_mutex; /* lock for gpio access */
114 const char *gpio_names[LTC2992_GPIO_NR];
115 struct regmap *regmap;
116 u32 r_sense_uohm[2];
119 struct ltc2992_gpio_regs {
120 u8 data;
121 u8 max;
122 u8 min;
123 u8 max_thresh;
124 u8 min_thresh;
125 u8 alarm;
126 u8 min_alarm_msk;
127 u8 max_alarm_msk;
128 u8 ctrl;
129 u8 ctrl_bit;
132 static const struct ltc2992_gpio_regs ltc2992_gpio_addr_map[] = {
134 .data = LTC2992_G1,
135 .max = LTC2992_G1_MAX,
136 .min = LTC2992_G1_MIN,
137 .max_thresh = LTC2992_G1_MAX_THRESH,
138 .min_thresh = LTC2992_G1_MIN_THRESH,
139 .alarm = LTC2992_FAULT1,
140 .min_alarm_msk = LTC2992_GPIO1_FAULT_MSK(0),
141 .max_alarm_msk = LTC2992_GPIO1_FAULT_MSK(1),
142 .ctrl = LTC2992_GPIO_IO_CTRL,
143 .ctrl_bit = LTC2992_GPIO1_BIT,
146 .data = LTC2992_G2,
147 .max = LTC2992_G2_MAX,
148 .min = LTC2992_G2_MIN,
149 .max_thresh = LTC2992_G2_MAX_THRESH,
150 .min_thresh = LTC2992_G2_MIN_THRESH,
151 .alarm = LTC2992_FAULT2,
152 .min_alarm_msk = LTC2992_GPIO2_FAULT_MSK(0),
153 .max_alarm_msk = LTC2992_GPIO2_FAULT_MSK(1),
154 .ctrl = LTC2992_GPIO_IO_CTRL,
155 .ctrl_bit = LTC2992_GPIO2_BIT,
158 .data = LTC2992_G3,
159 .max = LTC2992_G3_MAX,
160 .min = LTC2992_G3_MIN,
161 .max_thresh = LTC2992_G3_MAX_THRESH,
162 .min_thresh = LTC2992_G3_MIN_THRESH,
163 .alarm = LTC2992_FAULT3,
164 .min_alarm_msk = LTC2992_GPIO3_FAULT_MSK(0),
165 .max_alarm_msk = LTC2992_GPIO3_FAULT_MSK(1),
166 .ctrl = LTC2992_GPIO_IO_CTRL,
167 .ctrl_bit = LTC2992_GPIO3_BIT,
170 .data = LTC2992_G4,
171 .max = LTC2992_G4_MAX,
172 .min = LTC2992_G4_MIN,
173 .max_thresh = LTC2992_G4_MAX_THRESH,
174 .min_thresh = LTC2992_G4_MIN_THRESH,
175 .alarm = LTC2992_FAULT3,
176 .min_alarm_msk = LTC2992_GPIO4_FAULT_MSK(0),
177 .max_alarm_msk = LTC2992_GPIO4_FAULT_MSK(1),
178 .ctrl = LTC2992_GPIO_CTRL,
179 .ctrl_bit = LTC2992_GPIO4_BIT,
183 static const char *ltc2992_gpio_names[LTC2992_GPIO_NR] = {
184 "GPIO1", "GPIO2", "GPIO3", "GPIO4",
187 static int ltc2992_read_reg(struct ltc2992_state *st, u8 addr, const u8 reg_len)
189 u8 regvals[4];
190 int val;
191 int ret;
192 int i;
194 ret = regmap_bulk_read(st->regmap, addr, regvals, reg_len);
195 if (ret < 0)
196 return ret;
198 val = 0;
199 for (i = 0; i < reg_len; i++)
200 val |= regvals[reg_len - i - 1] << (i * 8);
202 return val;
205 static int ltc2992_write_reg(struct ltc2992_state *st, u8 addr, const u8 reg_len, u32 val)
207 u8 regvals[4];
208 int i;
210 for (i = 0; i < reg_len; i++)
211 regvals[reg_len - i - 1] = (val >> (i * 8)) & 0xFF;
213 return regmap_bulk_write(st->regmap, addr, regvals, reg_len);
216 static int ltc2992_gpio_get(struct gpio_chip *chip, unsigned int offset)
218 struct ltc2992_state *st = gpiochip_get_data(chip);
219 unsigned long gpio_status;
220 int reg;
222 mutex_lock(&st->gpio_mutex);
223 reg = ltc2992_read_reg(st, LTC2992_GPIO_STATUS, 1);
224 mutex_unlock(&st->gpio_mutex);
226 if (reg < 0)
227 return reg;
229 gpio_status = reg;
231 return !test_bit(LTC2992_GPIO_BIT(offset), &gpio_status);
234 static int ltc2992_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
235 unsigned long *bits)
237 struct ltc2992_state *st = gpiochip_get_data(chip);
238 unsigned long gpio_status;
239 unsigned int gpio_nr;
240 int reg;
242 mutex_lock(&st->gpio_mutex);
243 reg = ltc2992_read_reg(st, LTC2992_GPIO_STATUS, 1);
244 mutex_unlock(&st->gpio_mutex);
246 if (reg < 0)
247 return reg;
249 gpio_status = reg;
251 gpio_nr = 0;
252 for_each_set_bit_from(gpio_nr, mask, LTC2992_GPIO_NR) {
253 if (test_bit(LTC2992_GPIO_BIT(gpio_nr), &gpio_status))
254 set_bit(gpio_nr, bits);
257 return 0;
260 static void ltc2992_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
262 struct ltc2992_state *st = gpiochip_get_data(chip);
263 unsigned long gpio_ctrl;
264 int reg;
266 mutex_lock(&st->gpio_mutex);
267 reg = ltc2992_read_reg(st, ltc2992_gpio_addr_map[offset].ctrl, 1);
268 if (reg < 0) {
269 mutex_unlock(&st->gpio_mutex);
270 return;
273 gpio_ctrl = reg;
274 assign_bit(ltc2992_gpio_addr_map[offset].ctrl_bit, &gpio_ctrl, value);
276 ltc2992_write_reg(st, ltc2992_gpio_addr_map[offset].ctrl, 1, gpio_ctrl);
277 mutex_unlock(&st->gpio_mutex);
280 static void ltc2992_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
281 unsigned long *bits)
283 struct ltc2992_state *st = gpiochip_get_data(chip);
284 unsigned long gpio_ctrl_io = 0;
285 unsigned long gpio_ctrl = 0;
286 unsigned int gpio_nr;
288 for_each_set_bit(gpio_nr, mask, LTC2992_GPIO_NR) {
289 if (gpio_nr < 3)
290 assign_bit(ltc2992_gpio_addr_map[gpio_nr].ctrl_bit, &gpio_ctrl_io, true);
292 if (gpio_nr == 3)
293 assign_bit(ltc2992_gpio_addr_map[gpio_nr].ctrl_bit, &gpio_ctrl, true);
296 mutex_lock(&st->gpio_mutex);
297 ltc2992_write_reg(st, LTC2992_GPIO_IO_CTRL, 1, gpio_ctrl_io);
298 ltc2992_write_reg(st, LTC2992_GPIO_CTRL, 1, gpio_ctrl);
299 mutex_unlock(&st->gpio_mutex);
302 static int ltc2992_config_gpio(struct ltc2992_state *st)
304 const char *name = dev_name(&st->client->dev);
305 char *gpio_name;
306 int ret;
307 int i;
309 ret = ltc2992_write_reg(st, LTC2992_GPIO_IO_CTRL, 1, 0);
310 if (ret < 0)
311 return ret;
313 mutex_init(&st->gpio_mutex);
315 for (i = 0; i < ARRAY_SIZE(st->gpio_names); i++) {
316 gpio_name = devm_kasprintf(&st->client->dev, GFP_KERNEL, "ltc2992-%x-%s",
317 st->client->addr, ltc2992_gpio_names[i]);
318 if (!gpio_name)
319 return -ENOMEM;
321 st->gpio_names[i] = gpio_name;
324 st->gc.label = name;
325 st->gc.parent = &st->client->dev;
326 st->gc.owner = THIS_MODULE;
327 st->gc.base = -1;
328 st->gc.names = st->gpio_names;
329 st->gc.ngpio = ARRAY_SIZE(st->gpio_names);
330 st->gc.get = ltc2992_gpio_get;
331 st->gc.get_multiple = ltc2992_gpio_get_multiple;
332 st->gc.set = ltc2992_gpio_set;
333 st->gc.set_multiple = ltc2992_gpio_set_multiple;
335 ret = devm_gpiochip_add_data(&st->client->dev, &st->gc, st);
336 if (ret)
337 dev_err(&st->client->dev, "GPIO registering failed (%d)\n", ret);
339 return ret;
342 static umode_t ltc2992_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
343 int channel)
345 const struct ltc2992_state *st = data;
347 switch (type) {
348 case hwmon_chip:
349 switch (attr) {
350 case hwmon_chip_in_reset_history:
351 return 0200;
353 break;
354 case hwmon_in:
355 switch (attr) {
356 case hwmon_in_input:
357 case hwmon_in_lowest:
358 case hwmon_in_highest:
359 case hwmon_in_min_alarm:
360 case hwmon_in_max_alarm:
361 return 0444;
362 case hwmon_in_min:
363 case hwmon_in_max:
364 return 0644;
366 break;
367 case hwmon_curr:
368 switch (attr) {
369 case hwmon_curr_input:
370 case hwmon_curr_lowest:
371 case hwmon_curr_highest:
372 case hwmon_curr_min_alarm:
373 case hwmon_curr_max_alarm:
374 if (st->r_sense_uohm[channel])
375 return 0444;
376 break;
377 case hwmon_curr_min:
378 case hwmon_curr_max:
379 if (st->r_sense_uohm[channel])
380 return 0644;
381 break;
383 break;
384 case hwmon_power:
385 switch (attr) {
386 case hwmon_power_input:
387 case hwmon_power_input_lowest:
388 case hwmon_power_input_highest:
389 case hwmon_power_min_alarm:
390 case hwmon_power_max_alarm:
391 if (st->r_sense_uohm[channel])
392 return 0444;
393 break;
394 case hwmon_power_min:
395 case hwmon_power_max:
396 if (st->r_sense_uohm[channel])
397 return 0644;
398 break;
400 break;
401 default:
402 break;
405 return 0;
408 static int ltc2992_get_voltage(struct ltc2992_state *st, u32 reg, u32 scale, long *val)
410 int reg_val;
412 reg_val = ltc2992_read_reg(st, reg, 2);
413 if (reg_val < 0)
414 return reg_val;
416 reg_val = reg_val >> 4;
417 *val = DIV_ROUND_CLOSEST(reg_val * scale, 1000);
419 return 0;
422 static int ltc2992_set_voltage(struct ltc2992_state *st, u32 reg, u32 scale, long val)
424 val = DIV_ROUND_CLOSEST(val * 1000, scale);
425 val = val << 4;
427 return ltc2992_write_reg(st, reg, 2, val);
430 static int ltc2992_read_gpio_alarm(struct ltc2992_state *st, int nr_gpio, u32 attr, long *val)
432 int reg_val;
433 u32 mask;
435 if (attr == hwmon_in_max_alarm)
436 mask = ltc2992_gpio_addr_map[nr_gpio].max_alarm_msk;
437 else
438 mask = ltc2992_gpio_addr_map[nr_gpio].min_alarm_msk;
440 reg_val = ltc2992_read_reg(st, ltc2992_gpio_addr_map[nr_gpio].alarm, 1);
441 if (reg_val < 0)
442 return reg_val;
444 *val = !!(reg_val & mask);
445 reg_val &= ~mask;
447 return ltc2992_write_reg(st, ltc2992_gpio_addr_map[nr_gpio].alarm, 1, reg_val);
450 static int ltc2992_read_gpios_in(struct device *dev, u32 attr, int nr_gpio, long *val)
452 struct ltc2992_state *st = dev_get_drvdata(dev);
453 u32 reg;
455 switch (attr) {
456 case hwmon_in_input:
457 reg = ltc2992_gpio_addr_map[nr_gpio].data;
458 break;
459 case hwmon_in_lowest:
460 reg = ltc2992_gpio_addr_map[nr_gpio].min;
461 break;
462 case hwmon_in_highest:
463 reg = ltc2992_gpio_addr_map[nr_gpio].max;
464 break;
465 case hwmon_in_min:
466 reg = ltc2992_gpio_addr_map[nr_gpio].min_thresh;
467 break;
468 case hwmon_in_max:
469 reg = ltc2992_gpio_addr_map[nr_gpio].max_thresh;
470 break;
471 case hwmon_in_min_alarm:
472 case hwmon_in_max_alarm:
473 return ltc2992_read_gpio_alarm(st, nr_gpio, attr, val);
474 default:
475 return -EOPNOTSUPP;
478 return ltc2992_get_voltage(st, reg, LTC2992_VADC_GPIO_UV_LSB, val);
481 static int ltc2992_read_in_alarm(struct ltc2992_state *st, int channel, long *val, u32 attr)
483 int reg_val;
484 u32 mask;
486 if (attr == hwmon_in_max_alarm)
487 mask = LTC2992_SENSE_FAULT_MSK(1);
488 else
489 mask = LTC2992_SENSE_FAULT_MSK(0);
491 reg_val = ltc2992_read_reg(st, LTC2992_SENSE_FAULT(channel), 1);
492 if (reg_val < 0)
493 return reg_val;
495 *val = !!(reg_val & mask);
496 reg_val &= ~mask;
498 return ltc2992_write_reg(st, LTC2992_SENSE_FAULT(channel), 1, reg_val);
501 static int ltc2992_read_in(struct device *dev, u32 attr, int channel, long *val)
503 struct ltc2992_state *st = dev_get_drvdata(dev);
504 u32 reg;
506 if (channel > 1)
507 return ltc2992_read_gpios_in(dev, attr, channel - 2, val);
509 switch (attr) {
510 case hwmon_in_input:
511 reg = LTC2992_SENSE(channel);
512 break;
513 case hwmon_in_lowest:
514 reg = LTC2992_SENSE_MIN(channel);
515 break;
516 case hwmon_in_highest:
517 reg = LTC2992_SENSE_MAX(channel);
518 break;
519 case hwmon_in_min:
520 reg = LTC2992_SENSE_MIN_THRESH(channel);
521 break;
522 case hwmon_in_max:
523 reg = LTC2992_SENSE_MAX_THRESH(channel);
524 break;
525 case hwmon_in_min_alarm:
526 case hwmon_in_max_alarm:
527 return ltc2992_read_in_alarm(st, channel, val, attr);
528 default:
529 return -EOPNOTSUPP;
532 return ltc2992_get_voltage(st, reg, LTC2992_VADC_UV_LSB, val);
535 static int ltc2992_get_current(struct ltc2992_state *st, u32 reg, u32 channel, long *val)
537 int reg_val;
539 reg_val = ltc2992_read_reg(st, reg, 2);
540 if (reg_val < 0)
541 return reg_val;
543 reg_val = reg_val >> 4;
544 *val = DIV_ROUND_CLOSEST(reg_val * LTC2992_IADC_NANOV_LSB, st->r_sense_uohm[channel]);
546 return 0;
549 static int ltc2992_set_current(struct ltc2992_state *st, u32 reg, u32 channel, long val)
551 u32 reg_val;
553 reg_val = DIV_ROUND_CLOSEST(val * st->r_sense_uohm[channel], LTC2992_IADC_NANOV_LSB);
554 reg_val = reg_val << 4;
556 return ltc2992_write_reg(st, reg, 2, reg_val);
559 static int ltc2992_read_curr_alarm(struct ltc2992_state *st, int channel, long *val, u32 attr)
561 int reg_val;
562 u32 mask;
564 if (attr == hwmon_curr_max_alarm)
565 mask = LTC2992_DSENSE_FAULT_MSK(1);
566 else
567 mask = LTC2992_DSENSE_FAULT_MSK(0);
569 reg_val = ltc2992_read_reg(st, LTC2992_DSENSE_FAULT(channel), 1);
570 if (reg_val < 0)
571 return reg_val;
573 *val = !!(reg_val & mask);
575 reg_val &= ~mask;
576 return ltc2992_write_reg(st, LTC2992_DSENSE_FAULT(channel), 1, reg_val);
579 static int ltc2992_read_curr(struct device *dev, u32 attr, int channel, long *val)
581 struct ltc2992_state *st = dev_get_drvdata(dev);
582 u32 reg;
584 switch (attr) {
585 case hwmon_curr_input:
586 reg = LTC2992_DSENSE(channel);
587 break;
588 case hwmon_curr_lowest:
589 reg = LTC2992_DSENSE_MIN(channel);
590 break;
591 case hwmon_curr_highest:
592 reg = LTC2992_DSENSE_MAX(channel);
593 break;
594 case hwmon_curr_min:
595 reg = LTC2992_DSENSE_MIN_THRESH(channel);
596 break;
597 case hwmon_curr_max:
598 reg = LTC2992_DSENSE_MAX_THRESH(channel);
599 break;
600 case hwmon_curr_min_alarm:
601 case hwmon_curr_max_alarm:
602 return ltc2992_read_curr_alarm(st, channel, val, attr);
603 default:
604 return -EOPNOTSUPP;
607 return ltc2992_get_current(st, reg, channel, val);
610 static int ltc2992_get_power(struct ltc2992_state *st, u32 reg, u32 channel, long *val)
612 int reg_val;
614 reg_val = ltc2992_read_reg(st, reg, 3);
615 if (reg_val < 0)
616 return reg_val;
618 *val = mul_u64_u32_div(reg_val, LTC2992_VADC_UV_LSB * LTC2992_IADC_NANOV_LSB,
619 st->r_sense_uohm[channel] * 1000);
621 return 0;
624 static int ltc2992_set_power(struct ltc2992_state *st, u32 reg, u32 channel, long val)
626 u32 reg_val;
628 reg_val = mul_u64_u32_div(val, st->r_sense_uohm[channel] * 1000,
629 LTC2992_VADC_UV_LSB * LTC2992_IADC_NANOV_LSB);
631 return ltc2992_write_reg(st, reg, 3, reg_val);
634 static int ltc2992_read_power_alarm(struct ltc2992_state *st, int channel, long *val, u32 attr)
636 int reg_val;
637 u32 mask;
639 if (attr == hwmon_power_max_alarm)
640 mask = LTC2992_POWER_FAULT_MSK(1);
641 else
642 mask = LTC2992_POWER_FAULT_MSK(0);
644 reg_val = ltc2992_read_reg(st, LTC2992_POWER_FAULT(channel), 1);
645 if (reg_val < 0)
646 return reg_val;
648 *val = !!(reg_val & mask);
649 reg_val &= ~mask;
651 return ltc2992_write_reg(st, LTC2992_POWER_FAULT(channel), 1, reg_val);
654 static int ltc2992_read_power(struct device *dev, u32 attr, int channel, long *val)
656 struct ltc2992_state *st = dev_get_drvdata(dev);
657 u32 reg;
659 switch (attr) {
660 case hwmon_power_input:
661 reg = LTC2992_POWER(channel);
662 break;
663 case hwmon_power_input_lowest:
664 reg = LTC2992_POWER_MIN(channel);
665 break;
666 case hwmon_power_input_highest:
667 reg = LTC2992_POWER_MAX(channel);
668 break;
669 case hwmon_power_min:
670 reg = LTC2992_POWER_MIN_THRESH(channel);
671 break;
672 case hwmon_power_max:
673 reg = LTC2992_POWER_MAX_THRESH(channel);
674 break;
675 case hwmon_power_min_alarm:
676 case hwmon_power_max_alarm:
677 return ltc2992_read_power_alarm(st, channel, val, attr);
678 default:
679 return -EOPNOTSUPP;
682 return ltc2992_get_power(st, reg, channel, val);
685 static int ltc2992_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
686 long *val)
688 switch (type) {
689 case hwmon_in:
690 return ltc2992_read_in(dev, attr, channel, val);
691 case hwmon_curr:
692 return ltc2992_read_curr(dev, attr, channel, val);
693 case hwmon_power:
694 return ltc2992_read_power(dev, attr, channel, val);
695 default:
696 return -EOPNOTSUPP;
700 static int ltc2992_write_curr(struct device *dev, u32 attr, int channel, long val)
702 struct ltc2992_state *st = dev_get_drvdata(dev);
703 u32 reg;
705 switch (attr) {
706 case hwmon_curr_min:
707 reg = LTC2992_DSENSE_MIN_THRESH(channel);
708 break;
709 case hwmon_curr_max:
710 reg = LTC2992_DSENSE_MAX_THRESH(channel);
711 break;
712 default:
713 return -EOPNOTSUPP;
716 return ltc2992_set_current(st, reg, channel, val);
719 static int ltc2992_write_gpios_in(struct device *dev, u32 attr, int nr_gpio, long val)
721 struct ltc2992_state *st = dev_get_drvdata(dev);
722 u32 reg;
724 switch (attr) {
725 case hwmon_in_min:
726 reg = ltc2992_gpio_addr_map[nr_gpio].min_thresh;
727 break;
728 case hwmon_in_max:
729 reg = ltc2992_gpio_addr_map[nr_gpio].max_thresh;
730 break;
731 default:
732 return -EOPNOTSUPP;
735 return ltc2992_set_voltage(st, reg, LTC2992_VADC_GPIO_UV_LSB, val);
738 static int ltc2992_write_in(struct device *dev, u32 attr, int channel, long val)
740 struct ltc2992_state *st = dev_get_drvdata(dev);
741 u32 reg;
743 if (channel > 1)
744 return ltc2992_write_gpios_in(dev, attr, channel - 2, val);
746 switch (attr) {
747 case hwmon_in_min:
748 reg = LTC2992_SENSE_MIN_THRESH(channel);
749 break;
750 case hwmon_in_max:
751 reg = LTC2992_SENSE_MAX_THRESH(channel);
752 break;
753 default:
754 return -EOPNOTSUPP;
757 return ltc2992_set_voltage(st, reg, LTC2992_VADC_UV_LSB, val);
760 static int ltc2992_write_power(struct device *dev, u32 attr, int channel, long val)
762 struct ltc2992_state *st = dev_get_drvdata(dev);
763 u32 reg;
765 switch (attr) {
766 case hwmon_power_min:
767 reg = LTC2992_POWER_MIN_THRESH(channel);
768 break;
769 case hwmon_power_max:
770 reg = LTC2992_POWER_MAX_THRESH(channel);
771 break;
772 default:
773 return -EOPNOTSUPP;
776 return ltc2992_set_power(st, reg, channel, val);
779 static int ltc2992_write_chip(struct device *dev, u32 attr, int channel, long val)
781 struct ltc2992_state *st = dev_get_drvdata(dev);
783 switch (attr) {
784 case hwmon_chip_in_reset_history:
785 return regmap_update_bits(st->regmap, LTC2992_CTRLB, LTC2992_RESET_HISTORY,
786 LTC2992_RESET_HISTORY);
787 default:
788 return -EOPNOTSUPP;
792 static int ltc2992_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
793 long val)
795 switch (type) {
796 case hwmon_chip:
797 return ltc2992_write_chip(dev, attr, channel, val);
798 case hwmon_in:
799 return ltc2992_write_in(dev, attr, channel, val);
800 case hwmon_curr:
801 return ltc2992_write_curr(dev, attr, channel, val);
802 case hwmon_power:
803 return ltc2992_write_power(dev, attr, channel, val);
804 default:
805 return -EOPNOTSUPP;
809 static const struct hwmon_ops ltc2992_hwmon_ops = {
810 .is_visible = ltc2992_is_visible,
811 .read = ltc2992_read,
812 .write = ltc2992_write,
815 static const u32 ltc2992_chip_config[] = {
816 HWMON_C_IN_RESET_HISTORY,
820 static const struct hwmon_channel_info ltc2992_chip = {
821 .type = hwmon_chip,
822 .config = ltc2992_chip_config,
825 static const u32 ltc2992_in_config[] = {
826 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
827 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
828 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
829 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
830 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
831 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
832 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
833 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
834 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
835 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
836 HWMON_I_INPUT | HWMON_I_LOWEST | HWMON_I_HIGHEST | HWMON_I_MIN | HWMON_I_MAX |
837 HWMON_I_MIN_ALARM | HWMON_I_MAX_ALARM,
841 static const struct hwmon_channel_info ltc2992_in = {
842 .type = hwmon_in,
843 .config = ltc2992_in_config,
846 static const u32 ltc2992_curr_config[] = {
847 HWMON_C_INPUT | HWMON_C_LOWEST | HWMON_C_HIGHEST | HWMON_C_MIN | HWMON_C_MAX |
848 HWMON_C_MIN_ALARM | HWMON_C_MAX_ALARM,
849 HWMON_C_INPUT | HWMON_C_LOWEST | HWMON_C_HIGHEST | HWMON_C_MIN | HWMON_C_MAX |
850 HWMON_C_MIN_ALARM | HWMON_C_MAX_ALARM,
854 static const struct hwmon_channel_info ltc2992_curr = {
855 .type = hwmon_curr,
856 .config = ltc2992_curr_config,
859 static const u32 ltc2992_power_config[] = {
860 HWMON_P_INPUT | HWMON_P_INPUT_LOWEST | HWMON_P_INPUT_HIGHEST | HWMON_P_MIN | HWMON_P_MAX |
861 HWMON_P_MIN_ALARM | HWMON_P_MAX_ALARM,
862 HWMON_P_INPUT | HWMON_P_INPUT_LOWEST | HWMON_P_INPUT_HIGHEST | HWMON_P_MIN | HWMON_P_MAX |
863 HWMON_P_MIN_ALARM | HWMON_P_MAX_ALARM,
867 static const struct hwmon_channel_info ltc2992_power = {
868 .type = hwmon_power,
869 .config = ltc2992_power_config,
872 static const struct hwmon_channel_info *ltc2992_info[] = {
873 &ltc2992_chip,
874 &ltc2992_in,
875 &ltc2992_curr,
876 &ltc2992_power,
877 NULL
880 static const struct hwmon_chip_info ltc2992_chip_info = {
881 .ops = &ltc2992_hwmon_ops,
882 .info = ltc2992_info,
885 static const struct regmap_config ltc2992_regmap_config = {
886 .reg_bits = 8,
887 .val_bits = 8,
888 .max_register = 0xE8,
891 static int ltc2992_parse_dt(struct ltc2992_state *st)
893 struct fwnode_handle *fwnode;
894 struct fwnode_handle *child;
895 u32 addr;
896 u32 val;
897 int ret;
899 fwnode = dev_fwnode(&st->client->dev);
901 fwnode_for_each_available_child_node(fwnode, child) {
902 ret = fwnode_property_read_u32(child, "reg", &addr);
903 if (ret < 0)
904 return ret;
906 if (addr > 1)
907 return -EINVAL;
909 ret = fwnode_property_read_u32(child, "shunt-resistor-micro-ohms", &val);
910 if (!ret)
911 st->r_sense_uohm[addr] = val;
914 return 0;
917 static int ltc2992_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
919 struct device *hwmon_dev;
920 struct ltc2992_state *st;
921 int ret;
923 st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
924 if (!st)
925 return -ENOMEM;
927 st->client = client;
928 st->regmap = devm_regmap_init_i2c(client, &ltc2992_regmap_config);
929 if (IS_ERR(st->regmap))
930 return PTR_ERR(st->regmap);
932 ret = ltc2992_parse_dt(st);
933 if (ret < 0)
934 return ret;
936 ret = ltc2992_config_gpio(st);
937 if (ret < 0)
938 return ret;
940 hwmon_dev = devm_hwmon_device_register_with_info(&client->dev, client->name, st,
941 &ltc2992_chip_info, NULL);
943 return PTR_ERR_OR_ZERO(hwmon_dev);
946 static const struct of_device_id ltc2992_of_match[] = {
947 { .compatible = "adi,ltc2992" },
950 MODULE_DEVICE_TABLE(of, ltc2992_of_match);
952 static const struct i2c_device_id ltc2992_i2c_id[] = {
953 {"ltc2992", 0},
956 MODULE_DEVICE_TABLE(i2c, ltc2992_i2c_id);
958 static struct i2c_driver ltc2992_i2c_driver = {
959 .driver = {
960 .name = "ltc2992",
961 .of_match_table = ltc2992_of_match,
963 .probe = ltc2992_i2c_probe,
964 .id_table = ltc2992_i2c_id,
967 module_i2c_driver(ltc2992_i2c_driver);
969 MODULE_AUTHOR("Alexandru Tachici <alexandru.tachici@analog.com>");
970 MODULE_DESCRIPTION("Hwmon driver for Linear Technology 2992");
971 MODULE_LICENSE("Dual BSD/GPL");