1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
10 * Bi-Directional Current/Power Monitor with I2C Interface
11 * Datasheet: https://www.ti.com/product/ina220
14 * Bi-Directional Current/Power Monitor with I2C Interface
15 * Datasheet: https://www.ti.com/product/ina226
18 * Bi-directional Current/Power Monitor with I2C Interface
19 * Datasheet: https://www.ti.com/product/ina230
21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
22 * Thanks to Jan Volkering
25 #include <linux/bitfield.h>
26 #include <linux/bits.h>
27 #include <linux/delay.h>
28 #include <linux/device.h>
29 #include <linux/err.h>
30 #include <linux/hwmon.h>
31 #include <linux/i2c.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/property.h>
36 #include <linux/regmap.h>
37 #include <linux/slab.h>
38 #include <linux/sysfs.h>
39 #include <linux/util_macros.h>
41 /* common register definitions */
42 #define INA2XX_CONFIG 0x00
43 #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
44 #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
45 #define INA2XX_POWER 0x03 /* readonly */
46 #define INA2XX_CURRENT 0x04 /* readonly */
47 #define INA2XX_CALIBRATION 0x05
49 /* INA226 register definitions */
50 #define INA226_MASK_ENABLE 0x06
51 #define INA226_ALERT_LIMIT 0x07
52 #define INA226_DIE_ID 0xFF
54 /* SY24655 register definitions */
55 #define SY24655_EIN 0x0A
56 #define SY24655_ACCUM_CONFIG 0x0D
57 #define INA2XX_MAX_REGISTERS 0x0D
59 /* settings - depend on use case */
60 #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
61 #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
62 #define INA260_CONFIG_DEFAULT 0x6527 /* averages=16 */
63 #define SY24655_CONFIG_DEFAULT 0x4527 /* averages=16 */
65 /* (only for sy24655) */
66 #define SY24655_ACCUM_CONFIG_DEFAULT 0x044C /* continuous mode, clear after read*/
68 /* worst case is 68.10 ms (~14.6Hz, ina219) */
69 #define INA2XX_CONVERSION_RATE 15
70 #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
72 #define INA2XX_RSHUNT_DEFAULT 10000
73 #define INA260_RSHUNT 2000
75 /* bit mask for reading the averaging setting in the configuration register */
76 #define INA226_AVG_RD_MASK GENMASK(11, 9)
78 #define INA226_READ_AVG(reg) FIELD_GET(INA226_AVG_RD_MASK, reg)
80 #define INA226_ALERT_LATCH_ENABLE BIT(0)
81 #define INA226_ALERT_POLARITY BIT(1)
83 /* bit number of alert functions in Mask/Enable Register */
84 #define INA226_SHUNT_OVER_VOLTAGE_MASK BIT(15)
85 #define INA226_SHUNT_UNDER_VOLTAGE_MASK BIT(14)
86 #define INA226_BUS_OVER_VOLTAGE_MASK BIT(13)
87 #define INA226_BUS_UNDER_VOLTAGE_MASK BIT(12)
88 #define INA226_POWER_OVER_LIMIT_MASK BIT(11)
90 /* bit mask for alert config bits of Mask/Enable Register */
91 #define INA226_ALERT_CONFIG_MASK GENMASK(15, 10)
92 #define INA226_ALERT_FUNCTION_FLAG BIT(4)
95 * Both bus voltage and shunt voltage conversion times for ina226 are set
96 * to 0b0100 on POR, which translates to 2200 microseconds in total.
98 #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
100 static bool ina2xx_writeable_reg(struct device
*dev
, unsigned int reg
)
104 case INA2XX_CALIBRATION
:
105 case INA226_MASK_ENABLE
:
106 case INA226_ALERT_LIMIT
:
107 case SY24655_ACCUM_CONFIG
:
114 static bool ina2xx_volatile_reg(struct device
*dev
, unsigned int reg
)
117 case INA2XX_SHUNT_VOLTAGE
:
118 case INA2XX_BUS_VOLTAGE
:
127 static const struct regmap_config ina2xx_regmap_config
= {
130 .use_single_write
= true,
131 .use_single_read
= true,
132 .max_register
= INA2XX_MAX_REGISTERS
,
133 .cache_type
= REGCACHE_MAPLE
,
134 .volatile_reg
= ina2xx_volatile_reg
,
135 .writeable_reg
= ina2xx_writeable_reg
,
138 enum ina2xx_ids
{ ina219
, ina226
, ina260
, sy24655
};
140 struct ina2xx_config
{
142 bool has_alerts
; /* chip supports alerts and limits */
143 bool has_ishunt
; /* chip has internal shunt resistor */
144 bool has_power_average
; /* chip has internal shunt resistor */
145 int calibration_value
;
147 int bus_voltage_shift
;
148 int bus_voltage_lsb
; /* uV */
149 int power_lsb_factor
;
153 const struct ina2xx_config
*config
;
154 enum ina2xx_ids chip
;
159 struct mutex config_lock
;
160 struct regmap
*regmap
;
161 struct i2c_client
*client
;
164 static const struct ina2xx_config ina2xx_config
[] = {
166 .config_default
= INA219_CONFIG_DEFAULT
,
167 .calibration_value
= 4096,
169 .bus_voltage_shift
= 3,
170 .bus_voltage_lsb
= 4000,
171 .power_lsb_factor
= 20,
174 .has_power_average
= false,
177 .config_default
= INA226_CONFIG_DEFAULT
,
178 .calibration_value
= 2048,
180 .bus_voltage_shift
= 0,
181 .bus_voltage_lsb
= 1250,
182 .power_lsb_factor
= 25,
185 .has_power_average
= false,
188 .config_default
= INA260_CONFIG_DEFAULT
,
190 .bus_voltage_shift
= 0,
191 .bus_voltage_lsb
= 1250,
192 .power_lsb_factor
= 8,
195 .has_power_average
= false,
198 .config_default
= SY24655_CONFIG_DEFAULT
,
199 .calibration_value
= 4096,
201 .bus_voltage_shift
= 0,
202 .bus_voltage_lsb
= 1250,
203 .power_lsb_factor
= 25,
206 .has_power_average
= true,
211 * Available averaging rates for ina226. The indices correspond with
212 * the bit values expected by the chip (according to the ina226 datasheet,
213 * table 3 AVG bit settings, found at
214 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
216 static const int ina226_avg_tab
[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
218 static int ina226_reg_to_interval(u16 config
)
220 int avg
= ina226_avg_tab
[INA226_READ_AVG(config
)];
223 * Multiply the total conversion time by the number of averages.
224 * Return the result in milliseconds.
226 return DIV_ROUND_CLOSEST(avg
* INA226_TOTAL_CONV_TIME_DEFAULT
, 1000);
230 * Return the new, shifted AVG field value of CONFIG register,
231 * to use with regmap_update_bits
233 static u16
ina226_interval_to_reg(long interval
)
238 * The maximum supported interval is 1,024 * (2 * 8.244ms) ~= 16.8s.
239 * Clamp to 32 seconds before calculations to avoid overflows.
241 interval
= clamp_val(interval
, 0, 32000);
243 avg
= DIV_ROUND_CLOSEST(interval
* 1000,
244 INA226_TOTAL_CONV_TIME_DEFAULT
);
245 avg_bits
= find_closest(avg
, ina226_avg_tab
,
246 ARRAY_SIZE(ina226_avg_tab
));
248 return FIELD_PREP(INA226_AVG_RD_MASK
, avg_bits
);
251 static int ina2xx_get_value(struct ina2xx_data
*data
, u8 reg
,
257 case INA2XX_SHUNT_VOLTAGE
:
258 /* signed register */
259 val
= DIV_ROUND_CLOSEST((s16
)regval
, data
->config
->shunt_div
);
261 case INA2XX_BUS_VOLTAGE
:
262 val
= (regval
>> data
->config
->bus_voltage_shift
) *
263 data
->config
->bus_voltage_lsb
;
264 val
= DIV_ROUND_CLOSEST(val
, 1000);
267 val
= regval
* data
->power_lsb_uW
;
270 /* signed register, result in mA */
271 val
= (s16
)regval
* data
->current_lsb_uA
;
272 val
= DIV_ROUND_CLOSEST(val
, 1000);
274 case INA2XX_CALIBRATION
:
278 /* programmer goofed */
288 * Read and convert register value from chip. If the register value is 0,
289 * check if the chip has been power cycled or reset. If so, re-initialize it.
291 static int ina2xx_read_init(struct device
*dev
, int reg
, long *val
)
293 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
294 struct regmap
*regmap
= data
->regmap
;
298 if (data
->config
->has_ishunt
) {
299 /* No calibration needed */
300 ret
= regmap_read(regmap
, reg
, ®val
);
303 *val
= ina2xx_get_value(data
, reg
, regval
);
307 for (retry
= 5; retry
; retry
--) {
308 ret
= regmap_read(regmap
, reg
, ®val
);
313 * If the current value in the calibration register is 0, the
314 * power and current registers will also remain at 0. In case
315 * the chip has been reset let's check the calibration
316 * register and reinitialize if needed.
317 * We do that extra read of the calibration register if there
318 * is some hint of a chip reset.
323 ret
= regmap_read_bypassed(regmap
, INA2XX_CALIBRATION
, &cal
);
328 dev_warn(dev
, "chip not calibrated, reinitializing\n");
330 regcache_mark_dirty(regmap
);
331 regcache_sync(regmap
);
334 * Let's make sure the power and current
335 * registers have been updated before trying
338 msleep(INA2XX_MAX_DELAY
);
342 *val
= ina2xx_get_value(data
, reg
, regval
);
347 * If we're here then although all write operations succeeded, the
348 * chip still returns 0 in the calibration register. Nothing more we
351 dev_err(dev
, "unable to reinitialize the chip\n");
356 * Turns alert limit values into register values.
357 * Opposite of the formula in ina2xx_get_value().
359 static u16
ina226_alert_to_reg(struct ina2xx_data
*data
, int reg
, long val
)
362 case INA2XX_SHUNT_VOLTAGE
:
363 val
= clamp_val(val
, 0, SHRT_MAX
* data
->config
->shunt_div
);
364 val
*= data
->config
->shunt_div
;
365 return clamp_val(val
, 0, SHRT_MAX
);
366 case INA2XX_BUS_VOLTAGE
:
367 val
= clamp_val(val
, 0, 200000);
368 val
= (val
* 1000) << data
->config
->bus_voltage_shift
;
369 val
= DIV_ROUND_CLOSEST(val
, data
->config
->bus_voltage_lsb
);
370 return clamp_val(val
, 0, USHRT_MAX
);
372 val
= clamp_val(val
, 0, UINT_MAX
- data
->power_lsb_uW
);
373 val
= DIV_ROUND_CLOSEST(val
, data
->power_lsb_uW
);
374 return clamp_val(val
, 0, USHRT_MAX
);
376 val
= clamp_val(val
, INT_MIN
/ 1000, INT_MAX
/ 1000);
377 /* signed register, result in mA */
378 val
= DIV_ROUND_CLOSEST(val
* 1000, data
->current_lsb_uA
);
379 return clamp_val(val
, SHRT_MIN
, SHRT_MAX
);
381 /* programmer goofed */
387 static int ina226_alert_limit_read(struct ina2xx_data
*data
, u32 mask
, int reg
, long *val
)
389 struct regmap
*regmap
= data
->regmap
;
393 mutex_lock(&data
->config_lock
);
394 ret
= regmap_read(regmap
, INA226_MASK_ENABLE
, ®val
);
399 ret
= regmap_read(regmap
, INA226_ALERT_LIMIT
, ®val
);
402 *val
= ina2xx_get_value(data
, reg
, regval
);
407 mutex_unlock(&data
->config_lock
);
411 static int ina226_alert_limit_write(struct ina2xx_data
*data
, u32 mask
, int reg
, long val
)
413 struct regmap
*regmap
= data
->regmap
;
420 * Clear all alerts first to avoid accidentally triggering ALERT pin
421 * due to register write sequence. Then, only enable the alert
422 * if the value is non-zero.
424 mutex_lock(&data
->config_lock
);
425 ret
= regmap_update_bits(regmap
, INA226_MASK_ENABLE
,
426 INA226_ALERT_CONFIG_MASK
, 0);
430 ret
= regmap_write(regmap
, INA226_ALERT_LIMIT
,
431 ina226_alert_to_reg(data
, reg
, val
));
436 ret
= regmap_update_bits(regmap
, INA226_MASK_ENABLE
,
437 INA226_ALERT_CONFIG_MASK
, mask
);
439 mutex_unlock(&data
->config_lock
);
443 static int ina2xx_chip_read(struct device
*dev
, u32 attr
, long *val
)
445 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
450 case hwmon_chip_update_interval
:
451 ret
= regmap_read(data
->regmap
, INA2XX_CONFIG
, ®val
);
455 *val
= ina226_reg_to_interval(regval
);
463 static int ina226_alert_read(struct regmap
*regmap
, u32 mask
, long *val
)
468 ret
= regmap_read_bypassed(regmap
, INA226_MASK_ENABLE
, ®val
);
472 *val
= (regval
& mask
) && (regval
& INA226_ALERT_FUNCTION_FLAG
);
477 static int ina2xx_in_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
479 int voltage_reg
= channel
? INA2XX_BUS_VOLTAGE
: INA2XX_SHUNT_VOLTAGE
;
480 u32 under_voltage_mask
= channel
? INA226_BUS_UNDER_VOLTAGE_MASK
481 : INA226_SHUNT_UNDER_VOLTAGE_MASK
;
482 u32 over_voltage_mask
= channel
? INA226_BUS_OVER_VOLTAGE_MASK
483 : INA226_SHUNT_OVER_VOLTAGE_MASK
;
484 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
485 struct regmap
*regmap
= data
->regmap
;
491 ret
= regmap_read(regmap
, voltage_reg
, ®val
);
494 *val
= ina2xx_get_value(data
, voltage_reg
, regval
);
497 return ina226_alert_limit_read(data
, under_voltage_mask
,
500 return ina226_alert_limit_read(data
, over_voltage_mask
,
502 case hwmon_in_lcrit_alarm
:
503 return ina226_alert_read(regmap
, under_voltage_mask
, val
);
504 case hwmon_in_crit_alarm
:
505 return ina226_alert_read(regmap
, over_voltage_mask
, val
);
513 * Configuring the READ_EIN (bit 10) of the ACCUM_CONFIG register to 1
514 * can clear accumulator and sample_count after reading the EIN register.
515 * This way, the average power between the last read and the current
516 * read can be obtained. By combining with accurate time data from
517 * outside, the energy consumption during that period can be calculated.
519 static int sy24655_average_power_read(struct ina2xx_data
*data
, u8 reg
, long *val
)
523 long accumulator_24
, sample_count
;
525 /* 48-bit register read */
526 ret
= i2c_smbus_read_i2c_block_data(data
->client
, reg
, 6, template);
531 accumulator_24
= ((template[3] << 16) |
534 sample_count
= ((template[0] << 16) |
537 if (sample_count
<= 0) {
542 *val
= DIV_ROUND_CLOSEST(accumulator_24
, sample_count
) * data
->power_lsb_uW
;
547 static int ina2xx_power_read(struct device
*dev
, u32 attr
, long *val
)
549 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
552 case hwmon_power_input
:
553 return ina2xx_read_init(dev
, INA2XX_POWER
, val
);
554 case hwmon_power_average
:
555 return sy24655_average_power_read(data
, SY24655_EIN
, val
);
556 case hwmon_power_crit
:
557 return ina226_alert_limit_read(data
, INA226_POWER_OVER_LIMIT_MASK
,
559 case hwmon_power_crit_alarm
:
560 return ina226_alert_read(data
->regmap
, INA226_POWER_OVER_LIMIT_MASK
, val
);
566 static int ina2xx_curr_read(struct device
*dev
, u32 attr
, long *val
)
568 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
569 struct regmap
*regmap
= data
->regmap
;
574 * While the chips supported by this driver do not directly support
575 * current limits, they do support setting shunt voltage limits.
576 * The shunt voltage divided by the shunt resistor value is the current.
577 * On top of that, calibration values are set such that in the shunt
578 * voltage register and the current register report the same values.
579 * That means we can report and configure current limits based on shunt
583 case hwmon_curr_input
:
585 * Since the shunt voltage and the current register report the
586 * same values when the chip is calibrated, we can calculate
587 * the current directly from the shunt voltage without relying
588 * on chip calibration.
590 ret
= regmap_read(regmap
, INA2XX_SHUNT_VOLTAGE
, ®val
);
593 *val
= ina2xx_get_value(data
, INA2XX_CURRENT
, regval
);
595 case hwmon_curr_lcrit
:
596 return ina226_alert_limit_read(data
, INA226_SHUNT_UNDER_VOLTAGE_MASK
,
597 INA2XX_CURRENT
, val
);
598 case hwmon_curr_crit
:
599 return ina226_alert_limit_read(data
, INA226_SHUNT_OVER_VOLTAGE_MASK
,
600 INA2XX_CURRENT
, val
);
601 case hwmon_curr_lcrit_alarm
:
602 return ina226_alert_read(regmap
, INA226_SHUNT_UNDER_VOLTAGE_MASK
, val
);
603 case hwmon_curr_crit_alarm
:
604 return ina226_alert_read(regmap
, INA226_SHUNT_OVER_VOLTAGE_MASK
, val
);
610 static int ina2xx_read(struct device
*dev
, enum hwmon_sensor_types type
,
611 u32 attr
, int channel
, long *val
)
615 return ina2xx_chip_read(dev
, attr
, val
);
617 return ina2xx_in_read(dev
, attr
, channel
, val
);
619 return ina2xx_power_read(dev
, attr
, val
);
621 return ina2xx_curr_read(dev
, attr
, val
);
627 static int ina2xx_chip_write(struct device
*dev
, u32 attr
, long val
)
629 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
632 case hwmon_chip_update_interval
:
633 return regmap_update_bits(data
->regmap
, INA2XX_CONFIG
,
635 ina226_interval_to_reg(val
));
641 static int ina2xx_in_write(struct device
*dev
, u32 attr
, int channel
, long val
)
643 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
647 return ina226_alert_limit_write(data
,
648 channel
? INA226_BUS_UNDER_VOLTAGE_MASK
: INA226_SHUNT_UNDER_VOLTAGE_MASK
,
649 channel
? INA2XX_BUS_VOLTAGE
: INA2XX_SHUNT_VOLTAGE
,
652 return ina226_alert_limit_write(data
,
653 channel
? INA226_BUS_OVER_VOLTAGE_MASK
: INA226_SHUNT_OVER_VOLTAGE_MASK
,
654 channel
? INA2XX_BUS_VOLTAGE
: INA2XX_SHUNT_VOLTAGE
,
662 static int ina2xx_power_write(struct device
*dev
, u32 attr
, long val
)
664 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
667 case hwmon_power_crit
:
668 return ina226_alert_limit_write(data
, INA226_POWER_OVER_LIMIT_MASK
,
676 static int ina2xx_curr_write(struct device
*dev
, u32 attr
, long val
)
678 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
681 case hwmon_curr_lcrit
:
682 return ina226_alert_limit_write(data
, INA226_SHUNT_UNDER_VOLTAGE_MASK
,
683 INA2XX_CURRENT
, val
);
684 case hwmon_curr_crit
:
685 return ina226_alert_limit_write(data
, INA226_SHUNT_OVER_VOLTAGE_MASK
,
686 INA2XX_CURRENT
, val
);
693 static int ina2xx_write(struct device
*dev
, enum hwmon_sensor_types type
,
694 u32 attr
, int channel
, long val
)
698 return ina2xx_chip_write(dev
, attr
, val
);
700 return ina2xx_in_write(dev
, attr
, channel
, val
);
702 return ina2xx_power_write(dev
, attr
, val
);
704 return ina2xx_curr_write(dev
, attr
, val
);
710 static umode_t
ina2xx_is_visible(const void *_data
, enum hwmon_sensor_types type
,
711 u32 attr
, int channel
)
713 const struct ina2xx_data
*data
= _data
;
714 bool has_alerts
= data
->config
->has_alerts
;
715 bool has_power_average
= data
->config
->has_power_average
;
716 enum ina2xx_ids chip
= data
->chip
;
728 case hwmon_in_lcrit_alarm
:
729 case hwmon_in_crit_alarm
:
739 case hwmon_curr_input
:
741 case hwmon_curr_lcrit
:
742 case hwmon_curr_crit
:
746 case hwmon_curr_lcrit_alarm
:
747 case hwmon_curr_crit_alarm
:
757 case hwmon_power_input
:
759 case hwmon_power_crit
:
763 case hwmon_power_crit_alarm
:
767 case hwmon_power_average
:
768 if (has_power_average
)
777 case hwmon_chip_update_interval
:
778 if (chip
== ina226
|| chip
== ina260
)
791 static const struct hwmon_channel_info
* const ina2xx_info
[] = {
792 HWMON_CHANNEL_INFO(chip
,
793 HWMON_C_UPDATE_INTERVAL
),
794 HWMON_CHANNEL_INFO(in
,
795 HWMON_I_INPUT
| HWMON_I_CRIT
| HWMON_I_CRIT_ALARM
|
796 HWMON_I_LCRIT
| HWMON_I_LCRIT_ALARM
,
797 HWMON_I_INPUT
| HWMON_I_CRIT
| HWMON_I_CRIT_ALARM
|
798 HWMON_I_LCRIT
| HWMON_I_LCRIT_ALARM
800 HWMON_CHANNEL_INFO(curr
, HWMON_C_INPUT
| HWMON_C_CRIT
| HWMON_C_CRIT_ALARM
|
801 HWMON_C_LCRIT
| HWMON_C_LCRIT_ALARM
),
802 HWMON_CHANNEL_INFO(power
,
803 HWMON_P_INPUT
| HWMON_P_CRIT
| HWMON_P_CRIT_ALARM
|
808 static const struct hwmon_ops ina2xx_hwmon_ops
= {
809 .is_visible
= ina2xx_is_visible
,
811 .write
= ina2xx_write
,
814 static const struct hwmon_chip_info ina2xx_chip_info
= {
815 .ops
= &ina2xx_hwmon_ops
,
819 /* shunt resistance */
822 * In order to keep calibration register value fixed, the product
823 * of current_lsb and shunt_resistor should also be fixed and equal
824 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
827 static int ina2xx_set_shunt(struct ina2xx_data
*data
, unsigned long val
)
829 unsigned int dividend
= DIV_ROUND_CLOSEST(1000000000,
830 data
->config
->shunt_div
);
831 if (!val
|| val
> dividend
)
835 data
->current_lsb_uA
= DIV_ROUND_CLOSEST(dividend
, val
);
836 data
->power_lsb_uW
= data
->config
->power_lsb_factor
*
837 data
->current_lsb_uA
;
842 static ssize_t
shunt_resistor_show(struct device
*dev
,
843 struct device_attribute
*da
, char *buf
)
845 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
847 return sysfs_emit(buf
, "%li\n", data
->rshunt
);
850 static ssize_t
shunt_resistor_store(struct device
*dev
,
851 struct device_attribute
*da
,
852 const char *buf
, size_t count
)
854 struct ina2xx_data
*data
= dev_get_drvdata(dev
);
858 status
= kstrtoul(buf
, 10, &val
);
862 mutex_lock(&data
->config_lock
);
863 status
= ina2xx_set_shunt(data
, val
);
864 mutex_unlock(&data
->config_lock
);
870 static DEVICE_ATTR_RW(shunt_resistor
);
872 /* pointers to created device attributes */
873 static struct attribute
*ina2xx_attrs
[] = {
874 &dev_attr_shunt_resistor
.attr
,
877 ATTRIBUTE_GROUPS(ina2xx
);
882 static int ina2xx_init(struct device
*dev
, struct ina2xx_data
*data
)
884 struct regmap
*regmap
= data
->regmap
;
888 if (data
->config
->has_ishunt
)
889 shunt
= INA260_RSHUNT
;
890 else if (device_property_read_u32(dev
, "shunt-resistor", &shunt
) < 0)
891 shunt
= INA2XX_RSHUNT_DEFAULT
;
893 ret
= ina2xx_set_shunt(data
, shunt
);
897 ret
= regmap_write(regmap
, INA2XX_CONFIG
, data
->config
->config_default
);
901 if (data
->config
->has_alerts
) {
902 bool active_high
= device_property_read_bool(dev
, "ti,alert-polarity-active-high");
904 regmap_update_bits(regmap
, INA226_MASK_ENABLE
,
905 INA226_ALERT_LATCH_ENABLE
| INA226_ALERT_POLARITY
,
906 INA226_ALERT_LATCH_ENABLE
|
907 FIELD_PREP(INA226_ALERT_POLARITY
, active_high
));
909 if (data
->config
->has_power_average
) {
910 if (data
->chip
== sy24655
) {
912 * Initialize the power accumulation method to continuous
913 * mode and clear the EIN register after each read of the
916 ret
= regmap_write(regmap
, SY24655_ACCUM_CONFIG
,
917 SY24655_ACCUM_CONFIG_DEFAULT
);
923 if (data
->config
->has_ishunt
)
927 * Calibration register is set to the best value, which eliminates
928 * truncation errors on calculating current register in hardware.
929 * According to datasheet (eq. 3) the best values are 2048 for
930 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
932 return regmap_write(regmap
, INA2XX_CALIBRATION
,
933 data
->config
->calibration_value
);
936 static int ina2xx_probe(struct i2c_client
*client
)
938 struct device
*dev
= &client
->dev
;
939 struct ina2xx_data
*data
;
940 struct device
*hwmon_dev
;
941 enum ina2xx_ids chip
;
944 chip
= (uintptr_t)i2c_get_match_data(client
);
946 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
950 /* set the device type */
951 data
->client
= client
;
952 data
->config
= &ina2xx_config
[chip
];
954 mutex_init(&data
->config_lock
);
956 data
->regmap
= devm_regmap_init_i2c(client
, &ina2xx_regmap_config
);
957 if (IS_ERR(data
->regmap
)) {
958 dev_err(dev
, "failed to allocate register map\n");
959 return PTR_ERR(data
->regmap
);
962 ret
= devm_regulator_get_enable(dev
, "vs");
964 return dev_err_probe(dev
, ret
, "failed to enable vs regulator\n");
966 ret
= ina2xx_init(dev
, data
);
968 return dev_err_probe(dev
, ret
, "failed to configure device\n");
970 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
971 data
, &ina2xx_chip_info
,
972 data
->config
->has_ishunt
?
973 NULL
: ina2xx_groups
);
974 if (IS_ERR(hwmon_dev
))
975 return PTR_ERR(hwmon_dev
);
977 dev_info(dev
, "power monitor %s (Rshunt = %li uOhm)\n",
978 client
->name
, data
->rshunt
);
983 static const struct i2c_device_id ina2xx_id
[] = {
984 { "ina219", ina219
},
985 { "ina220", ina219
},
986 { "ina226", ina226
},
987 { "ina230", ina226
},
988 { "ina231", ina226
},
989 { "ina260", ina260
},
990 { "sy24655", sy24655
},
993 MODULE_DEVICE_TABLE(i2c
, ina2xx_id
);
995 static const struct of_device_id __maybe_unused ina2xx_of_match
[] = {
997 .compatible
= "silergy,sy24655",
998 .data
= (void *)sy24655
1001 .compatible
= "ti,ina219",
1002 .data
= (void *)ina219
1005 .compatible
= "ti,ina220",
1006 .data
= (void *)ina219
1009 .compatible
= "ti,ina226",
1010 .data
= (void *)ina226
1013 .compatible
= "ti,ina230",
1014 .data
= (void *)ina226
1017 .compatible
= "ti,ina231",
1018 .data
= (void *)ina226
1021 .compatible
= "ti,ina260",
1022 .data
= (void *)ina260
1026 MODULE_DEVICE_TABLE(of
, ina2xx_of_match
);
1028 static struct i2c_driver ina2xx_driver
= {
1031 .of_match_table
= of_match_ptr(ina2xx_of_match
),
1033 .probe
= ina2xx_probe
,
1034 .id_table
= ina2xx_id
,
1037 module_i2c_driver(ina2xx_driver
);
1039 MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
1040 MODULE_DESCRIPTION("ina2xx driver");
1041 MODULE_LICENSE("GPL");