1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC2947 high precision power and energy monitor
5 * Copyright 2019 Analog Devices Inc.
7 #include <linux/bitfield.h>
8 #include <linux/bits.h>
10 #include <linux/device.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/module.h>
15 #include <linux/regmap.h>
20 #define LTC2947_REG_PAGE_CTRL 0xFF
21 #define LTC2947_REG_CTRL 0xF0
22 #define LTC2947_REG_TBCTL 0xE9
23 #define LTC2947_CONT_MODE_MASK BIT(3)
24 #define LTC2947_CONT_MODE(x) FIELD_PREP(LTC2947_CONT_MODE_MASK, x)
25 #define LTC2947_PRE_MASK GENMASK(2, 0)
26 #define LTC2947_PRE(x) FIELD_PREP(LTC2947_PRE_MASK, x)
27 #define LTC2947_DIV_MASK GENMASK(7, 3)
28 #define LTC2947_DIV(x) FIELD_PREP(LTC2947_DIV_MASK, x)
29 #define LTC2947_SHUTDOWN_MASK BIT(0)
30 #define LTC2947_REG_ACCUM_POL 0xE1
31 #define LTC2947_ACCUM_POL_1_MASK GENMASK(1, 0)
32 #define LTC2947_ACCUM_POL_1(x) FIELD_PREP(LTC2947_ACCUM_POL_1_MASK, x)
33 #define LTC2947_ACCUM_POL_2_MASK GENMASK(3, 2)
34 #define LTC2947_ACCUM_POL_2(x) FIELD_PREP(LTC2947_ACCUM_POL_2_MASK, x)
35 #define LTC2947_REG_ACCUM_DEADBAND 0xE4
36 #define LTC2947_REG_GPIOSTATCTL 0x67
37 #define LTC2947_GPIO_EN_MASK BIT(0)
38 #define LTC2947_GPIO_EN(x) FIELD_PREP(LTC2947_GPIO_EN_MASK, x)
39 #define LTC2947_GPIO_FAN_EN_MASK BIT(6)
40 #define LTC2947_GPIO_FAN_EN(x) FIELD_PREP(LTC2947_GPIO_FAN_EN_MASK, x)
41 #define LTC2947_GPIO_FAN_POL_MASK BIT(7)
42 #define LTC2947_GPIO_FAN_POL(x) FIELD_PREP(LTC2947_GPIO_FAN_POL_MASK, x)
43 #define LTC2947_REG_GPIO_ACCUM 0xE3
45 #define LTC2947_CLK_MIN 200000
47 #define LTC2947_CLK_MAX 25000000
48 #define LTC2947_PAGE0 0
49 #define LTC2947_PAGE1 1
50 /* Voltage registers */
51 #define LTC2947_REG_VOLTAGE 0xA0
52 #define LTC2947_REG_VOLTAGE_MAX 0x50
53 #define LTC2947_REG_VOLTAGE_MIN 0x52
54 #define LTC2947_REG_VOLTAGE_THRE_H 0x90
55 #define LTC2947_REG_VOLTAGE_THRE_L 0x92
56 #define LTC2947_REG_DVCC 0xA4
57 #define LTC2947_REG_DVCC_MAX 0x58
58 #define LTC2947_REG_DVCC_MIN 0x5A
59 #define LTC2947_REG_DVCC_THRE_H 0x98
60 #define LTC2947_REG_DVCC_THRE_L 0x9A
61 #define LTC2947_VOLTAGE_GEN_CHAN 0
62 #define LTC2947_VOLTAGE_DVCC_CHAN 1
64 #define VOLTAGE_MAX 15500
65 #define VOLTAGE_MIN -300
66 #define VDVCC_MAX 15000
67 #define VDVCC_MIN 4750
68 /* Current registers */
69 #define LTC2947_REG_CURRENT 0x90
70 #define LTC2947_REG_CURRENT_MAX 0x40
71 #define LTC2947_REG_CURRENT_MIN 0x42
72 #define LTC2947_REG_CURRENT_THRE_H 0x80
73 #define LTC2947_REG_CURRENT_THRE_L 0x82
75 #define CURRENT_MAX 30000
76 #define CURRENT_MIN -30000
78 #define LTC2947_REG_POWER 0x93
79 #define LTC2947_REG_POWER_MAX 0x44
80 #define LTC2947_REG_POWER_MIN 0x46
81 #define LTC2947_REG_POWER_THRE_H 0x84
82 #define LTC2947_REG_POWER_THRE_L 0x86
84 #define POWER_MAX 450000000
85 #define POWER_MIN -450000000
86 /* Temperature registers */
87 #define LTC2947_REG_TEMP 0xA2
88 #define LTC2947_REG_TEMP_MAX 0x54
89 #define LTC2947_REG_TEMP_MIN 0x56
90 #define LTC2947_REG_TEMP_THRE_H 0x94
91 #define LTC2947_REG_TEMP_THRE_L 0x96
92 #define LTC2947_REG_TEMP_FAN_THRE_H 0x9C
93 #define LTC2947_REG_TEMP_FAN_THRE_L 0x9E
94 #define LTC2947_TEMP_FAN_CHAN 1
95 /* in millidegress Celsius */
96 #define TEMP_MAX 85000
97 #define TEMP_MIN -40000
98 /* Energy registers */
99 #define LTC2947_REG_ENERGY1 0x06
100 #define LTC2947_REG_ENERGY2 0x16
101 /* Status/Alarm/Overflow registers */
102 #define LTC2947_REG_STATUS 0x80
103 #define LTC2947_REG_STATVT 0x81
104 #define LTC2947_REG_STATIP 0x82
105 #define LTC2947_REG_STATVDVCC 0x87
107 #define LTC2947_ALERTS_SIZE (LTC2947_REG_STATVDVCC - LTC2947_REG_STATUS)
108 #define LTC2947_MAX_VOLTAGE_MASK BIT(0)
109 #define LTC2947_MIN_VOLTAGE_MASK BIT(1)
110 #define LTC2947_MAX_CURRENT_MASK BIT(0)
111 #define LTC2947_MIN_CURRENT_MASK BIT(1)
112 #define LTC2947_MAX_POWER_MASK BIT(2)
113 #define LTC2947_MIN_POWER_MASK BIT(3)
114 #define LTC2947_MAX_TEMP_MASK BIT(2)
115 #define LTC2947_MIN_TEMP_MASK BIT(3)
116 #define LTC2947_MAX_TEMP_FAN_MASK BIT(4)
117 #define LTC2947_MIN_TEMP_FAN_MASK BIT(5)
119 struct ltc2947_data
{
123 * The mutex is needed because the device has 2 memory pages. When
124 * reading/writing the correct page needs to be set so that, the
125 * complete sequence select_page->read/write needs to be protected.
132 static int __ltc2947_val_read16(const struct ltc2947_data
*st
, const u8 reg
,
138 ret
= regmap_bulk_read(st
->map
, reg
, &__val
, 2);
142 *val
= be16_to_cpu(__val
);
147 static int __ltc2947_val_read24(const struct ltc2947_data
*st
, const u8 reg
,
153 ret
= regmap_bulk_read(st
->map
, reg
, &__val
, 3);
157 *val
= be32_to_cpu(__val
) >> 8;
162 static int __ltc2947_val_read64(const struct ltc2947_data
*st
, const u8 reg
,
168 ret
= regmap_bulk_read(st
->map
, reg
, &__val
, 6);
172 *val
= be64_to_cpu(__val
) >> 16;
177 static int ltc2947_val_read(struct ltc2947_data
*st
, const u8 reg
,
178 const u8 page
, const size_t size
, s64
*val
)
183 mutex_lock(&st
->lock
);
185 ret
= regmap_write(st
->map
, LTC2947_REG_PAGE_CTRL
, page
);
187 mutex_unlock(&st
->lock
);
191 dev_dbg(st
->dev
, "Read val, reg:%02X, p:%d sz:%zu\n", reg
, page
,
196 ret
= __ltc2947_val_read16(st
, reg
, &__val
);
199 ret
= __ltc2947_val_read24(st
, reg
, &__val
);
202 ret
= __ltc2947_val_read64(st
, reg
, &__val
);
209 mutex_unlock(&st
->lock
);
214 *val
= sign_extend64(__val
, (8 * size
) - 1);
216 dev_dbg(st
->dev
, "Got s:%lld, u:%016llX\n", *val
, __val
);
221 static int __ltc2947_val_write64(const struct ltc2947_data
*st
, const u8 reg
,
226 __val
= cpu_to_be64(val
<< 16);
227 return regmap_bulk_write(st
->map
, reg
, &__val
, 6);
230 static int __ltc2947_val_write16(const struct ltc2947_data
*st
, const u8 reg
,
235 __val
= cpu_to_be16(val
);
236 return regmap_bulk_write(st
->map
, reg
, &__val
, 2);
239 static int ltc2947_val_write(struct ltc2947_data
*st
, const u8 reg
,
240 const u8 page
, const size_t size
, const u64 val
)
244 mutex_lock(&st
->lock
);
245 /* set device on correct page */
246 ret
= regmap_write(st
->map
, LTC2947_REG_PAGE_CTRL
, page
);
248 mutex_unlock(&st
->lock
);
252 dev_dbg(st
->dev
, "Write val, r:%02X, p:%d, sz:%zu, val:%016llX\n",
253 reg
, page
, size
, val
);
257 ret
= __ltc2947_val_write16(st
, reg
, val
);
260 ret
= __ltc2947_val_write64(st
, reg
, val
);
267 mutex_unlock(&st
->lock
);
272 static int ltc2947_reset_history(struct ltc2947_data
*st
, const u8 reg_h
,
277 * let's reset the tracking register's. Tracking register's have all
280 ret
= ltc2947_val_write(st
, reg_h
, LTC2947_PAGE0
, 2, 0x8000U
);
284 return ltc2947_val_write(st
, reg_l
, LTC2947_PAGE0
, 2, 0x7FFFU
);
287 static int ltc2947_alarm_read(struct ltc2947_data
*st
, const u8 reg
,
288 const u32 mask
, long *val
)
290 u8 offset
= reg
- LTC2947_REG_STATUS
;
291 /* +1 to include status reg */
292 char alarms
[LTC2947_ALERTS_SIZE
+ 1];
295 memset(alarms
, 0, sizeof(alarms
));
297 mutex_lock(&st
->lock
);
299 ret
= regmap_write(st
->map
, LTC2947_REG_PAGE_CTRL
, LTC2947_PAGE0
);
303 dev_dbg(st
->dev
, "Read alarm, reg:%02X, mask:%02X\n", reg
, mask
);
305 * As stated in the datasheet, when Threshold and Overflow registers
306 * are used, the status and all alert registers must be read in one
307 * multi-byte transaction.
309 ret
= regmap_bulk_read(st
->map
, LTC2947_REG_STATUS
, alarms
,
315 *val
= !!(alarms
[offset
] & mask
);
317 mutex_unlock(&st
->lock
);
321 static ssize_t
ltc2947_show_value(struct device
*dev
,
322 struct device_attribute
*da
, char *buf
)
324 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
325 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
329 ret
= ltc2947_val_read(st
, attr
->index
, LTC2947_PAGE0
, 6, &val
);
333 /* value in microJoule. st->lsb_energy was multiplied by 10E9 */
334 val
= div_s64(val
* st
->lsb_energy
, 1000);
336 return sprintf(buf
, "%lld\n", val
);
339 static int ltc2947_read_temp(struct device
*dev
, const u32 attr
, long *val
,
343 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
347 case hwmon_temp_input
:
348 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP
, LTC2947_PAGE0
,
351 case hwmon_temp_highest
:
352 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_MAX
, LTC2947_PAGE0
,
355 case hwmon_temp_lowest
:
356 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_MIN
, LTC2947_PAGE0
,
359 case hwmon_temp_max_alarm
:
360 if (channel
== LTC2947_TEMP_FAN_CHAN
)
361 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
362 LTC2947_MAX_TEMP_FAN_MASK
,
365 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
366 LTC2947_MAX_TEMP_MASK
, val
);
367 case hwmon_temp_min_alarm
:
368 if (channel
== LTC2947_TEMP_FAN_CHAN
)
369 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
370 LTC2947_MIN_TEMP_FAN_MASK
,
373 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
374 LTC2947_MIN_TEMP_MASK
, val
);
376 if (channel
== LTC2947_TEMP_FAN_CHAN
)
377 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_FAN_THRE_H
,
378 LTC2947_PAGE1
, 2, &__val
);
380 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_THRE_H
,
381 LTC2947_PAGE1
, 2, &__val
);
384 if (channel
== LTC2947_TEMP_FAN_CHAN
)
385 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_FAN_THRE_L
,
386 LTC2947_PAGE1
, 2, &__val
);
388 ret
= ltc2947_val_read(st
, LTC2947_REG_TEMP_THRE_L
,
389 LTC2947_PAGE1
, 2, &__val
);
398 /* in milidegrees celcius, temp is given by: */
399 *val
= (__val
* 204) + 550;
404 static int ltc2947_read_power(struct device
*dev
, const u32 attr
, long *val
)
406 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
408 u32 lsb
= 200000; /* in uW */
412 case hwmon_power_input
:
413 ret
= ltc2947_val_read(st
, LTC2947_REG_POWER
, LTC2947_PAGE0
,
417 case hwmon_power_input_highest
:
418 ret
= ltc2947_val_read(st
, LTC2947_REG_POWER_MAX
, LTC2947_PAGE0
,
421 case hwmon_power_input_lowest
:
422 ret
= ltc2947_val_read(st
, LTC2947_REG_POWER_MIN
, LTC2947_PAGE0
,
425 case hwmon_power_max_alarm
:
426 return ltc2947_alarm_read(st
, LTC2947_REG_STATIP
,
427 LTC2947_MAX_POWER_MASK
, val
);
428 case hwmon_power_min_alarm
:
429 return ltc2947_alarm_read(st
, LTC2947_REG_STATIP
,
430 LTC2947_MIN_POWER_MASK
, val
);
431 case hwmon_power_max
:
432 ret
= ltc2947_val_read(st
, LTC2947_REG_POWER_THRE_H
,
433 LTC2947_PAGE1
, 2, &__val
);
435 case hwmon_power_min
:
436 ret
= ltc2947_val_read(st
, LTC2947_REG_POWER_THRE_L
,
437 LTC2947_PAGE1
, 2, &__val
);
451 static int ltc2947_read_curr(struct device
*dev
, const u32 attr
, long *val
)
453 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
455 u8 lsb
= 12; /* in mA */
459 case hwmon_curr_input
:
460 ret
= ltc2947_val_read(st
, LTC2947_REG_CURRENT
,
461 LTC2947_PAGE0
, 3, &__val
);
464 case hwmon_curr_highest
:
465 ret
= ltc2947_val_read(st
, LTC2947_REG_CURRENT_MAX
,
466 LTC2947_PAGE0
, 2, &__val
);
468 case hwmon_curr_lowest
:
469 ret
= ltc2947_val_read(st
, LTC2947_REG_CURRENT_MIN
,
470 LTC2947_PAGE0
, 2, &__val
);
472 case hwmon_curr_max_alarm
:
473 return ltc2947_alarm_read(st
, LTC2947_REG_STATIP
,
474 LTC2947_MAX_CURRENT_MASK
, val
);
475 case hwmon_curr_min_alarm
:
476 return ltc2947_alarm_read(st
, LTC2947_REG_STATIP
,
477 LTC2947_MIN_CURRENT_MASK
, val
);
479 ret
= ltc2947_val_read(st
, LTC2947_REG_CURRENT_THRE_H
,
480 LTC2947_PAGE1
, 2, &__val
);
483 ret
= ltc2947_val_read(st
, LTC2947_REG_CURRENT_THRE_L
,
484 LTC2947_PAGE1
, 2, &__val
);
498 static int ltc2947_read_in(struct device
*dev
, const u32 attr
, long *val
,
501 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
503 u8 lsb
= 2; /* in mV */
506 if (channel
< 0 || channel
> LTC2947_VOLTAGE_DVCC_CHAN
) {
507 dev_err(st
->dev
, "Invalid chan%d for voltage", channel
);
513 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
514 ret
= ltc2947_val_read(st
, LTC2947_REG_DVCC
,
515 LTC2947_PAGE0
, 2, &__val
);
518 ret
= ltc2947_val_read(st
, LTC2947_REG_VOLTAGE
,
519 LTC2947_PAGE0
, 2, &__val
);
522 case hwmon_in_highest
:
523 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
524 ret
= ltc2947_val_read(st
, LTC2947_REG_DVCC_MAX
,
525 LTC2947_PAGE0
, 2, &__val
);
528 ret
= ltc2947_val_read(st
, LTC2947_REG_VOLTAGE_MAX
,
529 LTC2947_PAGE0
, 2, &__val
);
532 case hwmon_in_lowest
:
533 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
534 ret
= ltc2947_val_read(st
, LTC2947_REG_DVCC_MIN
,
535 LTC2947_PAGE0
, 2, &__val
);
538 ret
= ltc2947_val_read(st
, LTC2947_REG_VOLTAGE_MIN
,
539 LTC2947_PAGE0
, 2, &__val
);
542 case hwmon_in_max_alarm
:
543 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
)
544 return ltc2947_alarm_read(st
, LTC2947_REG_STATVDVCC
,
545 LTC2947_MAX_VOLTAGE_MASK
,
548 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
549 LTC2947_MAX_VOLTAGE_MASK
, val
);
550 case hwmon_in_min_alarm
:
551 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
)
552 return ltc2947_alarm_read(st
, LTC2947_REG_STATVDVCC
,
553 LTC2947_MIN_VOLTAGE_MASK
,
556 return ltc2947_alarm_read(st
, LTC2947_REG_STATVT
,
557 LTC2947_MIN_VOLTAGE_MASK
, val
);
559 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
560 ret
= ltc2947_val_read(st
, LTC2947_REG_DVCC_THRE_H
,
561 LTC2947_PAGE1
, 2, &__val
);
564 ret
= ltc2947_val_read(st
, LTC2947_REG_VOLTAGE_THRE_H
,
565 LTC2947_PAGE1
, 2, &__val
);
569 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
570 ret
= ltc2947_val_read(st
, LTC2947_REG_DVCC_THRE_L
,
571 LTC2947_PAGE1
, 2, &__val
);
574 ret
= ltc2947_val_read(st
, LTC2947_REG_VOLTAGE_THRE_L
,
575 LTC2947_PAGE1
, 2, &__val
);
590 static int ltc2947_read(struct device
*dev
, enum hwmon_sensor_types type
,
591 u32 attr
, int channel
, long *val
)
595 return ltc2947_read_in(dev
, attr
, val
, channel
);
597 return ltc2947_read_curr(dev
, attr
, val
);
599 return ltc2947_read_power(dev
, attr
, val
);
601 return ltc2947_read_temp(dev
, attr
, val
, channel
);
607 static int ltc2947_write_temp(struct device
*dev
, const u32 attr
,
608 long val
, const int channel
)
610 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
612 if (channel
< 0 || channel
> LTC2947_TEMP_FAN_CHAN
) {
613 dev_err(st
->dev
, "Invalid chan%d for temperature", channel
);
618 case hwmon_temp_reset_history
:
621 return ltc2947_reset_history(st
, LTC2947_REG_TEMP_MAX
,
622 LTC2947_REG_TEMP_MIN
);
624 val
= clamp_val(val
, TEMP_MIN
, TEMP_MAX
);
625 if (channel
== LTC2947_TEMP_FAN_CHAN
) {
629 return ltc2947_val_write(st
,
630 LTC2947_REG_TEMP_FAN_THRE_H
,
632 DIV_ROUND_CLOSEST(val
- 550, 204));
635 return ltc2947_val_write(st
, LTC2947_REG_TEMP_THRE_H
,
637 DIV_ROUND_CLOSEST(val
- 550, 204));
639 val
= clamp_val(val
, TEMP_MIN
, TEMP_MAX
);
640 if (channel
== LTC2947_TEMP_FAN_CHAN
) {
644 return ltc2947_val_write(st
,
645 LTC2947_REG_TEMP_FAN_THRE_L
,
647 DIV_ROUND_CLOSEST(val
- 550, 204));
650 return ltc2947_val_write(st
, LTC2947_REG_TEMP_THRE_L
,
652 DIV_ROUND_CLOSEST(val
- 550, 204));
658 static int ltc2947_write_power(struct device
*dev
, const u32 attr
,
661 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
664 case hwmon_power_reset_history
:
667 return ltc2947_reset_history(st
, LTC2947_REG_POWER_MAX
,
668 LTC2947_REG_POWER_MIN
);
669 case hwmon_power_max
:
670 val
= clamp_val(val
, POWER_MIN
, POWER_MAX
);
671 return ltc2947_val_write(st
, LTC2947_REG_POWER_THRE_H
,
673 DIV_ROUND_CLOSEST(val
, 200000));
674 case hwmon_power_min
:
675 val
= clamp_val(val
, POWER_MIN
, POWER_MAX
);
676 return ltc2947_val_write(st
, LTC2947_REG_POWER_THRE_L
,
678 DIV_ROUND_CLOSEST(val
, 200000));
684 static int ltc2947_write_curr(struct device
*dev
, const u32 attr
,
687 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
690 case hwmon_curr_reset_history
:
693 return ltc2947_reset_history(st
, LTC2947_REG_CURRENT_MAX
,
694 LTC2947_REG_CURRENT_MIN
);
696 val
= clamp_val(val
, CURRENT_MIN
, CURRENT_MAX
);
697 return ltc2947_val_write(st
, LTC2947_REG_CURRENT_THRE_H
,
699 DIV_ROUND_CLOSEST(val
, 12));
701 val
= clamp_val(val
, CURRENT_MIN
, CURRENT_MAX
);
702 return ltc2947_val_write(st
, LTC2947_REG_CURRENT_THRE_L
,
704 DIV_ROUND_CLOSEST(val
, 12));
710 static int ltc2947_write_in(struct device
*dev
, const u32 attr
, long val
,
713 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
715 if (channel
> LTC2947_VOLTAGE_DVCC_CHAN
) {
716 dev_err(st
->dev
, "Invalid chan%d for voltage", channel
);
721 case hwmon_in_reset_history
:
725 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
)
726 return ltc2947_reset_history(st
, LTC2947_REG_DVCC_MAX
,
727 LTC2947_REG_DVCC_MIN
);
729 return ltc2947_reset_history(st
, LTC2947_REG_VOLTAGE_MAX
,
730 LTC2947_REG_VOLTAGE_MIN
);
732 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
733 val
= clamp_val(val
, VDVCC_MIN
, VDVCC_MAX
);
734 return ltc2947_val_write(st
, LTC2947_REG_DVCC_THRE_H
,
736 DIV_ROUND_CLOSEST(val
, 145));
739 val
= clamp_val(val
, VOLTAGE_MIN
, VOLTAGE_MAX
);
740 return ltc2947_val_write(st
, LTC2947_REG_VOLTAGE_THRE_H
,
742 DIV_ROUND_CLOSEST(val
, 2));
744 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
) {
745 val
= clamp_val(val
, VDVCC_MIN
, VDVCC_MAX
);
746 return ltc2947_val_write(st
, LTC2947_REG_DVCC_THRE_L
,
748 DIV_ROUND_CLOSEST(val
, 145));
751 val
= clamp_val(val
, VOLTAGE_MIN
, VOLTAGE_MAX
);
752 return ltc2947_val_write(st
, LTC2947_REG_VOLTAGE_THRE_L
,
754 DIV_ROUND_CLOSEST(val
, 2));
760 static int ltc2947_write(struct device
*dev
,
761 enum hwmon_sensor_types type
,
762 u32 attr
, int channel
, long val
)
766 return ltc2947_write_in(dev
, attr
, val
, channel
);
768 return ltc2947_write_curr(dev
, attr
, val
);
770 return ltc2947_write_power(dev
, attr
, val
);
772 return ltc2947_write_temp(dev
, attr
, val
, channel
);
778 static int ltc2947_read_labels(struct device
*dev
,
779 enum hwmon_sensor_types type
,
780 u32 attr
, int channel
, const char **str
)
784 if (channel
== LTC2947_VOLTAGE_DVCC_CHAN
)
793 if (channel
== LTC2947_TEMP_FAN_CHAN
)
806 static int ltc2947_in_is_visible(const u32 attr
)
810 case hwmon_in_highest
:
811 case hwmon_in_lowest
:
812 case hwmon_in_max_alarm
:
813 case hwmon_in_min_alarm
:
816 case hwmon_in_reset_history
:
826 static int ltc2947_curr_is_visible(const u32 attr
)
829 case hwmon_curr_input
:
830 case hwmon_curr_highest
:
831 case hwmon_curr_lowest
:
832 case hwmon_curr_max_alarm
:
833 case hwmon_curr_min_alarm
:
834 case hwmon_curr_label
:
836 case hwmon_curr_reset_history
:
846 static int ltc2947_power_is_visible(const u32 attr
)
849 case hwmon_power_input
:
850 case hwmon_power_input_highest
:
851 case hwmon_power_input_lowest
:
852 case hwmon_power_label
:
853 case hwmon_power_max_alarm
:
854 case hwmon_power_min_alarm
:
856 case hwmon_power_reset_history
:
858 case hwmon_power_max
:
859 case hwmon_power_min
:
866 static int ltc2947_temp_is_visible(const u32 attr
)
869 case hwmon_temp_input
:
870 case hwmon_temp_highest
:
871 case hwmon_temp_lowest
:
872 case hwmon_temp_max_alarm
:
873 case hwmon_temp_min_alarm
:
874 case hwmon_temp_label
:
876 case hwmon_temp_reset_history
:
886 static umode_t
ltc2947_is_visible(const void *data
,
887 enum hwmon_sensor_types type
,
888 u32 attr
, int channel
)
892 return ltc2947_in_is_visible(attr
);
894 return ltc2947_curr_is_visible(attr
);
896 return ltc2947_power_is_visible(attr
);
898 return ltc2947_temp_is_visible(attr
);
904 static const struct hwmon_channel_info
*ltc2947_info
[] = {
905 HWMON_CHANNEL_INFO(in
,
906 HWMON_I_INPUT
| HWMON_I_LOWEST
| HWMON_I_HIGHEST
|
907 HWMON_I_MAX
| HWMON_I_MIN
| HWMON_I_RESET_HISTORY
|
908 HWMON_I_MIN_ALARM
| HWMON_I_MAX_ALARM
|
910 HWMON_I_INPUT
| HWMON_I_LOWEST
| HWMON_I_HIGHEST
|
911 HWMON_I_MAX
| HWMON_I_MIN
| HWMON_I_RESET_HISTORY
|
912 HWMON_I_MIN_ALARM
| HWMON_I_MAX_ALARM
|
914 HWMON_CHANNEL_INFO(curr
,
915 HWMON_C_INPUT
| HWMON_C_LOWEST
| HWMON_C_HIGHEST
|
916 HWMON_C_MAX
| HWMON_C_MIN
| HWMON_C_RESET_HISTORY
|
917 HWMON_C_MIN_ALARM
| HWMON_C_MAX_ALARM
|
919 HWMON_CHANNEL_INFO(power
,
920 HWMON_P_INPUT
| HWMON_P_INPUT_LOWEST
|
921 HWMON_P_INPUT_HIGHEST
| HWMON_P_MAX
| HWMON_P_MIN
|
922 HWMON_P_RESET_HISTORY
| HWMON_P_MAX_ALARM
|
923 HWMON_P_MIN_ALARM
| HWMON_P_LABEL
),
924 HWMON_CHANNEL_INFO(temp
,
925 HWMON_T_INPUT
| HWMON_T_LOWEST
| HWMON_T_HIGHEST
|
926 HWMON_T_MAX
| HWMON_T_MIN
| HWMON_T_RESET_HISTORY
|
927 HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
|
929 HWMON_T_MAX_ALARM
| HWMON_T_MIN_ALARM
| HWMON_T_MAX
|
930 HWMON_T_MIN
| HWMON_T_LABEL
),
934 static const struct hwmon_ops ltc2947_hwmon_ops
= {
935 .is_visible
= ltc2947_is_visible
,
936 .read
= ltc2947_read
,
937 .write
= ltc2947_write
,
938 .read_string
= ltc2947_read_labels
,
941 static const struct hwmon_chip_info ltc2947_chip_info
= {
942 .ops
= <c2947_hwmon_ops
,
943 .info
= ltc2947_info
,
946 /* energy attributes are 6bytes wide so we need u64 */
947 static SENSOR_DEVICE_ATTR(energy1_input
, 0444, ltc2947_show_value
, NULL
,
948 LTC2947_REG_ENERGY1
);
949 static SENSOR_DEVICE_ATTR(energy2_input
, 0444, ltc2947_show_value
, NULL
,
950 LTC2947_REG_ENERGY2
);
952 static struct attribute
*ltc2947_attrs
[] = {
953 &sensor_dev_attr_energy1_input
.dev_attr
.attr
,
954 &sensor_dev_attr_energy2_input
.dev_attr
.attr
,
957 ATTRIBUTE_GROUPS(ltc2947
);
959 static void ltc2947_clk_disable(void *data
)
961 struct clk
*extclk
= data
;
963 clk_disable_unprepare(extclk
);
966 static int ltc2947_setup(struct ltc2947_data
*st
)
970 u32 dummy
, deadband
, pol
;
973 /* clear status register by reading it */
974 ret
= regmap_read(st
->map
, LTC2947_REG_STATUS
, &dummy
);
978 * Set max/min for power here since the default values x scale
979 * would overflow on 32bit arch
981 ret
= ltc2947_val_write(st
, LTC2947_REG_POWER_THRE_H
, LTC2947_PAGE1
, 2,
986 ret
= ltc2947_val_write(st
, LTC2947_REG_POWER_THRE_L
, LTC2947_PAGE1
, 2,
991 /* check external clock presence */
992 extclk
= devm_clk_get(st
->dev
, NULL
);
993 if (!IS_ERR(extclk
)) {
994 unsigned long rate_hz
;
995 u8 pre
= 0, div
, tbctl
;
998 /* let's calculate and set the right valus in TBCTL */
999 rate_hz
= clk_get_rate(extclk
);
1000 if (rate_hz
< LTC2947_CLK_MIN
|| rate_hz
> LTC2947_CLK_MAX
) {
1001 dev_err(st
->dev
, "Invalid rate:%lu for external clock",
1006 ret
= clk_prepare_enable(extclk
);
1010 ret
= devm_add_action_or_reset(st
->dev
, ltc2947_clk_disable
,
1014 /* as in table 1 of the datasheet */
1015 if (rate_hz
>= LTC2947_CLK_MIN
&& rate_hz
<= 1000000)
1017 else if (rate_hz
> 1000000 && rate_hz
<= 2000000)
1019 else if (rate_hz
> 2000000 && rate_hz
<= 4000000)
1021 else if (rate_hz
> 4000000 && rate_hz
<= 8000000)
1023 else if (rate_hz
> 8000000 && rate_hz
<= 16000000)
1025 else if (rate_hz
> 16000000 && rate_hz
<= LTC2947_CLK_MAX
)
1029 * floor(fref / (2^PRE * 32768))
1031 div
= rate_hz
/ ((1 << pre
) * 32768);
1032 tbctl
= LTC2947_PRE(pre
) | LTC2947_DIV(div
);
1034 ret
= regmap_write(st
->map
, LTC2947_REG_TBCTL
, tbctl
);
1038 * The energy lsb is given by (in W*s):
1039 * 06416 * (1/fref) * 2^PRE * (DIV + 1)
1040 * The value is multiplied by 10E9
1042 aux
= (div
+ 1) * ((1 << pre
) * 641600000ULL);
1043 st
->lsb_energy
= DIV_ROUND_CLOSEST_ULL(aux
, rate_hz
);
1045 /* 19.89E-6 * 10E9 */
1046 st
->lsb_energy
= 19890;
1048 ret
= of_property_read_u32_array(st
->dev
->of_node
,
1049 "adi,accumulator-ctl-pol", accum
,
1052 u32 accum_reg
= LTC2947_ACCUM_POL_1(accum
[0]) |
1053 LTC2947_ACCUM_POL_2(accum
[1]);
1055 ret
= regmap_write(st
->map
, LTC2947_REG_ACCUM_POL
, accum_reg
);
1059 ret
= of_property_read_u32(st
->dev
->of_node
,
1060 "adi,accumulation-deadband-microamp",
1063 /* the LSB is the same as the current, so 3mA */
1064 ret
= regmap_write(st
->map
, LTC2947_REG_ACCUM_DEADBAND
,
1065 deadband
/ (1000 * 3));
1069 /* check gpio cfg */
1070 ret
= of_property_read_u32(st
->dev
->of_node
, "adi,gpio-out-pol", &pol
);
1072 /* setup GPIO as output */
1073 u32 gpio_ctl
= LTC2947_GPIO_EN(1) | LTC2947_GPIO_FAN_EN(1) |
1074 LTC2947_GPIO_FAN_POL(pol
);
1076 st
->gpio_out
= true;
1077 ret
= regmap_write(st
->map
, LTC2947_REG_GPIOSTATCTL
, gpio_ctl
);
1081 ret
= of_property_read_u32_array(st
->dev
->of_node
, "adi,gpio-in-accum",
1082 accum
, ARRAY_SIZE(accum
));
1085 * Setup the accum options. The gpioctl is already defined as
1088 u32 accum_val
= LTC2947_ACCUM_POL_1(accum
[0]) |
1089 LTC2947_ACCUM_POL_2(accum
[1]);
1093 "Cannot have input gpio config if already configured as output");
1097 ret
= regmap_write(st
->map
, LTC2947_REG_GPIO_ACCUM
, accum_val
);
1102 /* set continuos mode */
1103 return regmap_update_bits(st
->map
, LTC2947_REG_CTRL
,
1104 LTC2947_CONT_MODE_MASK
, LTC2947_CONT_MODE(1));
1107 int ltc2947_core_probe(struct regmap
*map
, const char *name
)
1109 struct ltc2947_data
*st
;
1110 struct device
*dev
= regmap_get_device(map
);
1111 struct device
*hwmon
;
1114 st
= devm_kzalloc(dev
, sizeof(*st
), GFP_KERNEL
);
1120 dev_set_drvdata(dev
, st
);
1121 mutex_init(&st
->lock
);
1123 ret
= ltc2947_setup(st
);
1127 hwmon
= devm_hwmon_device_register_with_info(dev
, name
, st
,
1130 return PTR_ERR_OR_ZERO(hwmon
);
1132 EXPORT_SYMBOL_GPL(ltc2947_core_probe
);
1134 static int __maybe_unused
ltc2947_resume(struct device
*dev
)
1136 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
1140 /* dummy read to wake the device */
1141 ret
= regmap_read(st
->map
, LTC2947_REG_CTRL
, &ctrl
);
1145 * Wait for the device. It takes 100ms to wake up so, 10ms extra
1149 ret
= regmap_read(st
->map
, LTC2947_REG_CTRL
, &ctrl
);
1152 /* ctrl should be 0 */
1154 dev_err(st
->dev
, "Device failed to wake up, ctl:%02X\n", ctrl
);
1158 /* set continuous mode */
1159 return regmap_update_bits(st
->map
, LTC2947_REG_CTRL
,
1160 LTC2947_CONT_MODE_MASK
, LTC2947_CONT_MODE(1));
1163 static int __maybe_unused
ltc2947_suspend(struct device
*dev
)
1165 struct ltc2947_data
*st
= dev_get_drvdata(dev
);
1167 return regmap_update_bits(st
->map
, LTC2947_REG_CTRL
,
1168 LTC2947_SHUTDOWN_MASK
, 1);
1171 SIMPLE_DEV_PM_OPS(ltc2947_pm_ops
, ltc2947_suspend
, ltc2947_resume
);
1172 EXPORT_SYMBOL_GPL(ltc2947_pm_ops
);
1174 const struct of_device_id ltc2947_of_match
[] = {
1175 { .compatible
= "adi,ltc2947" },
1178 EXPORT_SYMBOL_GPL(ltc2947_of_match
);
1179 MODULE_DEVICE_TABLE(of
, ltc2947_of_match
);
1181 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1182 MODULE_DESCRIPTION("LTC2947 power and energy monitor core driver");
1183 MODULE_LICENSE("GPL");