Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Texas Instruments INA219, INA226 power monitor chips |
| 4 | * |
| 5 | * INA219: |
| 6 | * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface |
Alexander A. Klimov | 49dc2fb | 2020-07-19 20:15:30 +0200 | [diff] [blame] | 7 | * Datasheet: https://www.ti.com/product/ina219 |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 8 | * |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 9 | * INA220: |
| 10 | * Bi-Directional Current/Power Monitor with I2C Interface |
Alexander A. Klimov | 49dc2fb | 2020-07-19 20:15:30 +0200 | [diff] [blame] | 11 | * Datasheet: https://www.ti.com/product/ina220 |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 12 | * |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 13 | * INA226: |
| 14 | * Bi-Directional Current/Power Monitor with I2C Interface |
Alexander A. Klimov | 49dc2fb | 2020-07-19 20:15:30 +0200 | [diff] [blame] | 15 | * Datasheet: https://www.ti.com/product/ina226 |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 16 | * |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 17 | * INA230: |
| 18 | * Bi-directional Current/Power Monitor with I2C Interface |
Alexander A. Klimov | 49dc2fb | 2020-07-19 20:15:30 +0200 | [diff] [blame] | 19 | * Datasheet: https://www.ti.com/product/ina230 |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 20 | * |
Lothar Felten | 3ad8670 | 2018-08-14 09:09:37 +0200 | [diff] [blame] | 21 | * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com> |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 22 | * Thanks to Jan Volkering |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/hwmon.h> |
| 32 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | dcd8f39 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 33 | #include <linux/jiffies.h> |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 34 | #include <linux/of_device.h> |
Tang Yuantian | 31e7ad7 | 2013-06-19 14:50:20 +0800 | [diff] [blame] | 35 | #include <linux/of.h> |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 36 | #include <linux/delay.h> |
Bartosz Golaszewski | d38df34 | 2015-04-16 12:43:34 -0700 | [diff] [blame] | 37 | #include <linux/util_macros.h> |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 38 | #include <linux/regmap.h> |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 39 | |
| 40 | #include <linux/platform_data/ina2xx.h> |
| 41 | |
| 42 | /* common register definitions */ |
| 43 | #define INA2XX_CONFIG 0x00 |
| 44 | #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */ |
| 45 | #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */ |
| 46 | #define INA2XX_POWER 0x03 /* readonly */ |
| 47 | #define INA2XX_CURRENT 0x04 /* readonly */ |
| 48 | #define INA2XX_CALIBRATION 0x05 |
| 49 | |
| 50 | /* INA226 register definitions */ |
| 51 | #define INA226_MASK_ENABLE 0x06 |
| 52 | #define INA226_ALERT_LIMIT 0x07 |
| 53 | #define INA226_DIE_ID 0xFF |
| 54 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 55 | /* register count */ |
| 56 | #define INA219_REGISTERS 6 |
| 57 | #define INA226_REGISTERS 8 |
| 58 | |
| 59 | #define INA2XX_MAX_REGISTERS 8 |
| 60 | |
| 61 | /* settings - depend on use case */ |
| 62 | #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */ |
| 63 | #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */ |
| 64 | |
| 65 | /* worst case is 68.10 ms (~14.6Hz, ina219) */ |
| 66 | #define INA2XX_CONVERSION_RATE 15 |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 67 | #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */ |
| 68 | |
| 69 | #define INA2XX_RSHUNT_DEFAULT 10000 |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 70 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 71 | /* bit mask for reading the averaging setting in the configuration register */ |
| 72 | #define INA226_AVG_RD_MASK 0x0E00 |
| 73 | |
| 74 | #define INA226_READ_AVG(reg) (((reg) & INA226_AVG_RD_MASK) >> 9) |
| 75 | #define INA226_SHIFT_AVG(val) ((val) << 9) |
| 76 | |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 77 | /* bit number of alert functions in Mask/Enable Register */ |
| 78 | #define INA226_SHUNT_OVER_VOLTAGE_BIT 15 |
| 79 | #define INA226_SHUNT_UNDER_VOLTAGE_BIT 14 |
| 80 | #define INA226_BUS_OVER_VOLTAGE_BIT 13 |
| 81 | #define INA226_BUS_UNDER_VOLTAGE_BIT 12 |
| 82 | #define INA226_POWER_OVER_LIMIT_BIT 11 |
| 83 | |
| 84 | /* bit mask for alert config bits of Mask/Enable Register */ |
| 85 | #define INA226_ALERT_CONFIG_MASK 0xFC00 |
| 86 | #define INA226_ALERT_FUNCTION_FLAG BIT(4) |
| 87 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 88 | /* common attrs, ina226 attrs and NULL */ |
| 89 | #define INA2XX_MAX_ATTRIBUTE_GROUPS 3 |
| 90 | |
| 91 | /* |
| 92 | * Both bus voltage and shunt voltage conversion times for ina226 are set |
| 93 | * to 0b0100 on POR, which translates to 2200 microseconds in total. |
| 94 | */ |
| 95 | #define INA226_TOTAL_CONV_TIME_DEFAULT 2200 |
| 96 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 97 | static struct regmap_config ina2xx_regmap_config = { |
| 98 | .reg_bits = 8, |
| 99 | .val_bits = 16, |
| 100 | }; |
| 101 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 102 | enum ina2xx_ids { ina219, ina226 }; |
| 103 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 104 | struct ina2xx_config { |
| 105 | u16 config_default; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 106 | int calibration_value; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 107 | int registers; |
| 108 | int shunt_div; |
| 109 | int bus_voltage_shift; |
| 110 | int bus_voltage_lsb; /* uV */ |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 111 | int power_lsb_factor; |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 114 | struct ina2xx_data { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 115 | const struct ina2xx_config *config; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 116 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 117 | long rshunt; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 118 | long current_lsb_uA; |
| 119 | long power_lsb_uW; |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 120 | struct mutex config_lock; |
| 121 | struct regmap *regmap; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 122 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 123 | const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS]; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 126 | static const struct ina2xx_config ina2xx_config[] = { |
| 127 | [ina219] = { |
| 128 | .config_default = INA219_CONFIG_DEFAULT, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 129 | .calibration_value = 4096, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 130 | .registers = INA219_REGISTERS, |
| 131 | .shunt_div = 100, |
| 132 | .bus_voltage_shift = 3, |
| 133 | .bus_voltage_lsb = 4000, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 134 | .power_lsb_factor = 20, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 135 | }, |
| 136 | [ina226] = { |
| 137 | .config_default = INA226_CONFIG_DEFAULT, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 138 | .calibration_value = 2048, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 139 | .registers = INA226_REGISTERS, |
| 140 | .shunt_div = 400, |
| 141 | .bus_voltage_shift = 0, |
| 142 | .bus_voltage_lsb = 1250, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 143 | .power_lsb_factor = 25, |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 144 | }, |
| 145 | }; |
| 146 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 147 | /* |
| 148 | * Available averaging rates for ina226. The indices correspond with |
| 149 | * the bit values expected by the chip (according to the ina226 datasheet, |
| 150 | * table 3 AVG bit settings, found at |
Alexander A. Klimov | 49dc2fb | 2020-07-19 20:15:30 +0200 | [diff] [blame] | 151 | * https://www.ti.com/lit/ds/symlink/ina226.pdf. |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 152 | */ |
| 153 | static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 }; |
| 154 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 155 | static int ina226_reg_to_interval(u16 config) |
| 156 | { |
| 157 | int avg = ina226_avg_tab[INA226_READ_AVG(config)]; |
| 158 | |
| 159 | /* |
| 160 | * Multiply the total conversion time by the number of averages. |
| 161 | * Return the result in milliseconds. |
| 162 | */ |
| 163 | return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000); |
| 164 | } |
| 165 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 166 | /* |
| 167 | * Return the new, shifted AVG field value of CONFIG register, |
| 168 | * to use with regmap_update_bits |
| 169 | */ |
| 170 | static u16 ina226_interval_to_reg(int interval) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 171 | { |
| 172 | int avg, avg_bits; |
| 173 | |
| 174 | avg = DIV_ROUND_CLOSEST(interval * 1000, |
| 175 | INA226_TOTAL_CONV_TIME_DEFAULT); |
Bartosz Golaszewski | d38df34 | 2015-04-16 12:43:34 -0700 | [diff] [blame] | 176 | avg_bits = find_closest(avg, ina226_avg_tab, |
| 177 | ARRAY_SIZE(ina226_avg_tab)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 178 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 179 | return INA226_SHIFT_AVG(avg_bits); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 182 | /* |
| 183 | * Calibration register is set to the best value, which eliminates |
| 184 | * truncation errors on calculating current register in hardware. |
| 185 | * According to datasheet (eq. 3) the best values are 2048 for |
| 186 | * ina226 and 4096 for ina219. They are hardcoded as calibration_value. |
| 187 | */ |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 188 | static int ina2xx_calibrate(struct ina2xx_data *data) |
| 189 | { |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 190 | return regmap_write(data->regmap, INA2XX_CALIBRATION, |
| 191 | data->config->calibration_value); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Initialize the configuration and calibration registers. |
| 196 | */ |
| 197 | static int ina2xx_init(struct ina2xx_data *data) |
| 198 | { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 199 | int ret = regmap_write(data->regmap, INA2XX_CONFIG, |
| 200 | data->config->config_default); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | return ret; |
| 203 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 204 | return ina2xx_calibrate(data); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 207 | static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 208 | { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 209 | struct ina2xx_data *data = dev_get_drvdata(dev); |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 210 | int ret, retry; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 211 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 212 | dev_dbg(dev, "Starting register %d read\n", reg); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 213 | |
| 214 | for (retry = 5; retry; retry--) { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 215 | |
| 216 | ret = regmap_read(data->regmap, reg, regval); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | |
| 220 | dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval); |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * If the current value in the calibration register is 0, the |
| 224 | * power and current registers will also remain at 0. In case |
| 225 | * the chip has been reset let's check the calibration |
| 226 | * register and reinitialize if needed. |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 227 | * We do that extra read of the calibration register if there |
| 228 | * is some hint of a chip reset. |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 229 | */ |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 230 | if (*regval == 0) { |
| 231 | unsigned int cal; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 232 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 233 | ret = regmap_read(data->regmap, INA2XX_CALIBRATION, |
| 234 | &cal); |
| 235 | if (ret < 0) |
| 236 | return ret; |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 237 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 238 | if (cal == 0) { |
| 239 | dev_warn(dev, "chip not calibrated, reinitializing\n"); |
| 240 | |
| 241 | ret = ina2xx_init(data); |
| 242 | if (ret < 0) |
| 243 | return ret; |
| 244 | /* |
| 245 | * Let's make sure the power and current |
| 246 | * registers have been updated before trying |
| 247 | * again. |
| 248 | */ |
| 249 | msleep(INA2XX_MAX_DELAY); |
| 250 | continue; |
| 251 | } |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 252 | } |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * If we're here then although all write operations succeeded, the |
| 258 | * chip still returns 0 in the calibration register. Nothing more we |
| 259 | * can do here. |
| 260 | */ |
| 261 | dev_err(dev, "unable to reinitialize the chip\n"); |
| 262 | return -ENODEV; |
| 263 | } |
| 264 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 265 | static int ina2xx_get_value(struct ina2xx_data *data, u8 reg, |
| 266 | unsigned int regval) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 267 | { |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 268 | int val; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 269 | |
| 270 | switch (reg) { |
| 271 | case INA2XX_SHUNT_VOLTAGE: |
Fabio Baltieri | c0214f9 | 2014-06-08 22:06:24 +0100 | [diff] [blame] | 272 | /* signed register */ |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 273 | val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 274 | break; |
| 275 | case INA2XX_BUS_VOLTAGE: |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 276 | val = (regval >> data->config->bus_voltage_shift) |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 277 | * data->config->bus_voltage_lsb; |
| 278 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 279 | break; |
| 280 | case INA2XX_POWER: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 281 | val = regval * data->power_lsb_uW; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 282 | break; |
| 283 | case INA2XX_CURRENT: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 284 | /* signed register, result in mA */ |
Nicolin Chen | 38cd989e | 2018-11-13 19:48:54 -0800 | [diff] [blame] | 285 | val = (s16)regval * data->current_lsb_uA; |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 286 | val = DIV_ROUND_CLOSEST(val, 1000); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 287 | break; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 288 | case INA2XX_CALIBRATION: |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 289 | val = regval; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 290 | break; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 291 | default: |
| 292 | /* programmer goofed */ |
| 293 | WARN_ON_ONCE(1); |
| 294 | val = 0; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | return val; |
| 299 | } |
| 300 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 301 | static ssize_t ina2xx_value_show(struct device *dev, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 302 | struct device_attribute *da, char *buf) |
| 303 | { |
| 304 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 305 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 306 | unsigned int regval; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 307 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 308 | int err = ina2xx_read_reg(dev, attr->index, ®val); |
| 309 | |
| 310 | if (err < 0) |
| 311 | return err; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 312 | |
Zihao Tang | af9a973 | 2021-03-16 19:00:57 +0800 | [diff] [blame] | 313 | return sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval)); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 316 | static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval) |
| 317 | { |
| 318 | int reg; |
| 319 | |
| 320 | switch (bit) { |
| 321 | case INA226_SHUNT_OVER_VOLTAGE_BIT: |
| 322 | case INA226_SHUNT_UNDER_VOLTAGE_BIT: |
| 323 | reg = INA2XX_SHUNT_VOLTAGE; |
| 324 | break; |
| 325 | case INA226_BUS_OVER_VOLTAGE_BIT: |
| 326 | case INA226_BUS_UNDER_VOLTAGE_BIT: |
| 327 | reg = INA2XX_BUS_VOLTAGE; |
| 328 | break; |
| 329 | case INA226_POWER_OVER_LIMIT_BIT: |
| 330 | reg = INA2XX_POWER; |
| 331 | break; |
| 332 | default: |
| 333 | /* programmer goofed */ |
| 334 | WARN_ON_ONCE(1); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | return ina2xx_get_value(data, reg, regval); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Turns alert limit values into register values. |
| 343 | * Opposite of the formula in ina2xx_get_value(). |
| 344 | */ |
| 345 | static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val) |
| 346 | { |
| 347 | switch (bit) { |
| 348 | case INA226_SHUNT_OVER_VOLTAGE_BIT: |
| 349 | case INA226_SHUNT_UNDER_VOLTAGE_BIT: |
| 350 | val *= data->config->shunt_div; |
| 351 | return clamp_val(val, SHRT_MIN, SHRT_MAX); |
| 352 | case INA226_BUS_OVER_VOLTAGE_BIT: |
| 353 | case INA226_BUS_UNDER_VOLTAGE_BIT: |
| 354 | val = (val * 1000) << data->config->bus_voltage_shift; |
| 355 | val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb); |
| 356 | return clamp_val(val, 0, SHRT_MAX); |
| 357 | case INA226_POWER_OVER_LIMIT_BIT: |
| 358 | val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW); |
| 359 | return clamp_val(val, 0, USHRT_MAX); |
| 360 | default: |
| 361 | /* programmer goofed */ |
| 362 | WARN_ON_ONCE(1); |
| 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | static ssize_t ina226_alert_show(struct device *dev, |
| 368 | struct device_attribute *da, char *buf) |
| 369 | { |
| 370 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 371 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 372 | int regval; |
| 373 | int val = 0; |
| 374 | int ret; |
| 375 | |
| 376 | mutex_lock(&data->config_lock); |
| 377 | ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val); |
| 378 | if (ret) |
| 379 | goto abort; |
| 380 | |
| 381 | if (regval & BIT(attr->index)) { |
| 382 | ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, ®val); |
| 383 | if (ret) |
| 384 | goto abort; |
| 385 | val = ina226_reg_to_alert(data, attr->index, regval); |
| 386 | } |
| 387 | |
Guenter Roeck | 1f4d4af | 2021-03-21 20:49:10 -0700 | [diff] [blame] | 388 | ret = sysfs_emit(buf, "%d\n", val); |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 389 | abort: |
| 390 | mutex_unlock(&data->config_lock); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static ssize_t ina226_alert_store(struct device *dev, |
| 395 | struct device_attribute *da, |
| 396 | const char *buf, size_t count) |
| 397 | { |
| 398 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 399 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 400 | unsigned long val; |
| 401 | int ret; |
| 402 | |
| 403 | ret = kstrtoul(buf, 10, &val); |
| 404 | if (ret < 0) |
| 405 | return ret; |
| 406 | |
| 407 | /* |
| 408 | * Clear all alerts first to avoid accidentally triggering ALERT pin |
| 409 | * due to register write sequence. Then, only enable the alert |
| 410 | * if the value is non-zero. |
| 411 | */ |
| 412 | mutex_lock(&data->config_lock); |
| 413 | ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE, |
| 414 | INA226_ALERT_CONFIG_MASK, 0); |
| 415 | if (ret < 0) |
| 416 | goto abort; |
| 417 | |
| 418 | ret = regmap_write(data->regmap, INA226_ALERT_LIMIT, |
| 419 | ina226_alert_to_reg(data, attr->index, val)); |
| 420 | if (ret < 0) |
| 421 | goto abort; |
| 422 | |
| 423 | if (val != 0) { |
| 424 | ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE, |
| 425 | INA226_ALERT_CONFIG_MASK, |
| 426 | BIT(attr->index)); |
| 427 | if (ret < 0) |
| 428 | goto abort; |
| 429 | } |
| 430 | |
| 431 | ret = count; |
| 432 | abort: |
| 433 | mutex_unlock(&data->config_lock); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static ssize_t ina226_alarm_show(struct device *dev, |
| 438 | struct device_attribute *da, char *buf) |
| 439 | { |
| 440 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 441 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 442 | int regval; |
| 443 | int alarm = 0; |
| 444 | int ret; |
| 445 | |
| 446 | ret = regmap_read(data->regmap, INA226_MASK_ENABLE, ®val); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | |
| 450 | alarm = (regval & BIT(attr->index)) && |
| 451 | (regval & INA226_ALERT_FUNCTION_FLAG); |
Zihao Tang | af9a973 | 2021-03-16 19:00:57 +0800 | [diff] [blame] | 452 | return sysfs_emit(buf, "%d\n", alarm); |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 455 | /* |
| 456 | * In order to keep calibration register value fixed, the product |
| 457 | * of current_lsb and shunt_resistor should also be fixed and equal |
| 458 | * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order |
| 459 | * to keep the scale. |
| 460 | */ |
| 461 | static int ina2xx_set_shunt(struct ina2xx_data *data, long val) |
| 462 | { |
| 463 | unsigned int dividend = DIV_ROUND_CLOSEST(1000000000, |
| 464 | data->config->shunt_div); |
| 465 | if (val <= 0 || val > dividend) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | mutex_lock(&data->config_lock); |
| 469 | data->rshunt = val; |
| 470 | data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val); |
| 471 | data->power_lsb_uW = data->config->power_lsb_factor * |
| 472 | data->current_lsb_uA; |
| 473 | mutex_unlock(&data->config_lock); |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 478 | static ssize_t ina2xx_shunt_show(struct device *dev, |
| 479 | struct device_attribute *da, char *buf) |
Lothar Felten | 3ad8670 | 2018-08-14 09:09:37 +0200 | [diff] [blame] | 480 | { |
| 481 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 482 | |
Zihao Tang | af9a973 | 2021-03-16 19:00:57 +0800 | [diff] [blame] | 483 | return sysfs_emit(buf, "%li\n", data->rshunt); |
Lothar Felten | 3ad8670 | 2018-08-14 09:09:37 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 486 | static ssize_t ina2xx_shunt_store(struct device *dev, |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 487 | struct device_attribute *da, |
| 488 | const char *buf, size_t count) |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 489 | { |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 490 | unsigned long val; |
| 491 | int status; |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 492 | struct ina2xx_data *data = dev_get_drvdata(dev); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 493 | |
| 494 | status = kstrtoul(buf, 10, &val); |
| 495 | if (status < 0) |
| 496 | return status; |
| 497 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 498 | status = ina2xx_set_shunt(data, val); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 499 | if (status < 0) |
| 500 | return status; |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 501 | return count; |
| 502 | } |
| 503 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 504 | static ssize_t ina226_interval_store(struct device *dev, |
| 505 | struct device_attribute *da, |
| 506 | const char *buf, size_t count) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 507 | { |
| 508 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 509 | unsigned long val; |
| 510 | int status; |
| 511 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 512 | status = kstrtoul(buf, 10, &val); |
| 513 | if (status < 0) |
| 514 | return status; |
| 515 | |
| 516 | if (val > INT_MAX || val == 0) |
| 517 | return -EINVAL; |
| 518 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 519 | status = regmap_update_bits(data->regmap, INA2XX_CONFIG, |
| 520 | INA226_AVG_RD_MASK, |
| 521 | ina226_interval_to_reg(val)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 522 | if (status < 0) |
| 523 | return status; |
| 524 | |
| 525 | return count; |
| 526 | } |
| 527 | |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 528 | static ssize_t ina226_interval_show(struct device *dev, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 529 | struct device_attribute *da, char *buf) |
| 530 | { |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 531 | struct ina2xx_data *data = dev_get_drvdata(dev); |
| 532 | int status; |
| 533 | unsigned int regval; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 534 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 535 | status = regmap_read(data->regmap, INA2XX_CONFIG, ®val); |
| 536 | if (status) |
| 537 | return status; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 538 | |
Zihao Tang | af9a973 | 2021-03-16 19:00:57 +0800 | [diff] [blame] | 539 | return sysfs_emit(buf, "%d\n", ina226_reg_to_interval(regval)); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 542 | /* shunt voltage */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 543 | static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE); |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 544 | /* shunt voltage over/under voltage alert setting and alarm */ |
| 545 | static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert, |
| 546 | INA226_SHUNT_OVER_VOLTAGE_BIT); |
| 547 | static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert, |
| 548 | INA226_SHUNT_UNDER_VOLTAGE_BIT); |
| 549 | static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm, |
| 550 | INA226_SHUNT_OVER_VOLTAGE_BIT); |
| 551 | static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm, |
| 552 | INA226_SHUNT_UNDER_VOLTAGE_BIT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 553 | |
| 554 | /* bus voltage */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 555 | static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE); |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 556 | /* bus voltage over/under voltage alert setting and alarm */ |
| 557 | static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert, |
| 558 | INA226_BUS_OVER_VOLTAGE_BIT); |
| 559 | static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert, |
| 560 | INA226_BUS_UNDER_VOLTAGE_BIT); |
| 561 | static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm, |
| 562 | INA226_BUS_OVER_VOLTAGE_BIT); |
| 563 | static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm, |
| 564 | INA226_BUS_UNDER_VOLTAGE_BIT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 565 | |
| 566 | /* calculated current */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 567 | static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 568 | |
| 569 | /* calculated power */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 570 | static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER); |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 571 | /* over-limit power alert setting and alarm */ |
| 572 | static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert, |
| 573 | INA226_POWER_OVER_LIMIT_BIT); |
| 574 | static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm, |
| 575 | INA226_POWER_OVER_LIMIT_BIT); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 576 | |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 577 | /* shunt resistance */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 578 | static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION); |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 579 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 580 | /* update interval (ina226 only) */ |
Guenter Roeck | 6a0f234 | 2018-12-06 11:06:23 -0800 | [diff] [blame] | 581 | static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 582 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 583 | /* pointers to created device attributes */ |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 584 | static struct attribute *ina2xx_attrs[] = { |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 585 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 586 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 587 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 588 | &sensor_dev_attr_power1_input.dev_attr.attr, |
Bartosz Golaszewski | 8a5fc79 | 2015-01-05 15:20:55 +0100 | [diff] [blame] | 589 | &sensor_dev_attr_shunt_resistor.dev_attr.attr, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 590 | NULL, |
| 591 | }; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 592 | |
| 593 | static const struct attribute_group ina2xx_group = { |
| 594 | .attrs = ina2xx_attrs, |
| 595 | }; |
| 596 | |
| 597 | static struct attribute *ina226_attrs[] = { |
Alex Qiu | 5a56a39 | 2020-05-04 17:59:45 -0700 | [diff] [blame] | 598 | &sensor_dev_attr_in0_crit.dev_attr.attr, |
| 599 | &sensor_dev_attr_in0_lcrit.dev_attr.attr, |
| 600 | &sensor_dev_attr_in0_crit_alarm.dev_attr.attr, |
| 601 | &sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr, |
| 602 | &sensor_dev_attr_in1_crit.dev_attr.attr, |
| 603 | &sensor_dev_attr_in1_lcrit.dev_attr.attr, |
| 604 | &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, |
| 605 | &sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr, |
| 606 | &sensor_dev_attr_power1_crit.dev_attr.attr, |
| 607 | &sensor_dev_attr_power1_crit_alarm.dev_attr.attr, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 608 | &sensor_dev_attr_update_interval.dev_attr.attr, |
| 609 | NULL, |
| 610 | }; |
| 611 | |
| 612 | static const struct attribute_group ina226_group = { |
| 613 | .attrs = ina226_attrs, |
| 614 | }; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 615 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 616 | static const struct i2c_device_id ina2xx_id[]; |
| 617 | |
| 618 | static int ina2xx_probe(struct i2c_client *client) |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 619 | { |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 620 | struct device *dev = &client->dev; |
| 621 | struct ina2xx_data *data; |
| 622 | struct device *hwmon_dev; |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 623 | u32 val; |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 624 | int ret, group = 0; |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 625 | enum ina2xx_ids chip; |
| 626 | |
| 627 | if (client->dev.of_node) |
| 628 | chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev); |
| 629 | else |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 630 | chip = i2c_match_id(ina2xx_id, client)->driver_data; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 631 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 632 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 633 | if (!data) |
| 634 | return -ENOMEM; |
| 635 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 636 | /* set the device type */ |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 637 | data->config = &ina2xx_config[chip]; |
Marek Szyprowski | 0c4c586 | 2018-01-15 14:58:21 +0100 | [diff] [blame] | 638 | mutex_init(&data->config_lock); |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 639 | |
Marc Titinger | 001e2e7 | 2015-10-27 10:51:08 +0100 | [diff] [blame] | 640 | if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) { |
| 641 | struct ina2xx_platform_data *pdata = dev_get_platdata(dev); |
| 642 | |
| 643 | if (pdata) |
| 644 | val = pdata->shunt_uohms; |
| 645 | else |
| 646 | val = INA2XX_RSHUNT_DEFAULT; |
| 647 | } |
| 648 | |
Maciej Purski | 5d389b1 | 2017-11-22 16:32:15 +0100 | [diff] [blame] | 649 | ina2xx_set_shunt(data, val); |
Marc Titinger | 001e2e7 | 2015-10-27 10:51:08 +0100 | [diff] [blame] | 650 | |
Marc Titinger | a0de56c | 2015-10-28 12:04:53 +0100 | [diff] [blame] | 651 | ina2xx_regmap_config.max_register = data->config->registers; |
| 652 | |
| 653 | data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config); |
| 654 | if (IS_ERR(data->regmap)) { |
| 655 | dev_err(dev, "failed to allocate register map\n"); |
| 656 | return PTR_ERR(data->regmap); |
| 657 | } |
| 658 | |
Bartosz Golaszewski | 509416a | 2015-01-05 15:20:52 +0100 | [diff] [blame] | 659 | ret = ina2xx_init(data); |
| 660 | if (ret < 0) { |
| 661 | dev_err(dev, "error configuring the device: %d\n", ret); |
| 662 | return -ENODEV; |
| 663 | } |
| 664 | |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 665 | data->groups[group++] = &ina2xx_group; |
Nicolin Chen | 70df9eb | 2018-11-09 16:42:14 -0800 | [diff] [blame] | 666 | if (chip == ina226) |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 667 | data->groups[group++] = &ina226_group; |
| 668 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 669 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
Bartosz Golaszewski | 72a87a4 | 2015-01-09 17:03:42 +0100 | [diff] [blame] | 670 | data, data->groups); |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 671 | if (IS_ERR(hwmon_dev)) |
| 672 | return PTR_ERR(hwmon_dev); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 673 | |
Guenter Roeck | 468bf0e | 2013-09-02 13:16:19 -0700 | [diff] [blame] | 674 | dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n", |
Nicolin Chen | 70df9eb | 2018-11-09 16:42:14 -0800 | [diff] [blame] | 675 | client->name, data->rshunt); |
Guenter Roeck | 6106db2 | 2012-05-12 11:21:01 -0700 | [diff] [blame] | 676 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 677 | return 0; |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static const struct i2c_device_id ina2xx_id[] = { |
| 681 | { "ina219", ina219 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 682 | { "ina220", ina219 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 683 | { "ina226", ina226 }, |
Guenter Roeck | dc92cd0 | 2012-05-12 11:33:11 -0700 | [diff] [blame] | 684 | { "ina230", ina226 }, |
Kevin Hilman | add513b | 2015-01-14 17:34:58 -0800 | [diff] [blame] | 685 | { "ina231", ina226 }, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 686 | { } |
| 687 | }; |
| 688 | MODULE_DEVICE_TABLE(i2c, ina2xx_id); |
| 689 | |
Guenter Roeck | df6b8c7 | 2019-04-04 08:00:57 -0700 | [diff] [blame] | 690 | static const struct of_device_id __maybe_unused ina2xx_of_match[] = { |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 691 | { |
| 692 | .compatible = "ti,ina219", |
| 693 | .data = (void *)ina219 |
| 694 | }, |
| 695 | { |
| 696 | .compatible = "ti,ina220", |
| 697 | .data = (void *)ina219 |
| 698 | }, |
| 699 | { |
| 700 | .compatible = "ti,ina226", |
| 701 | .data = (void *)ina226 |
| 702 | }, |
| 703 | { |
| 704 | .compatible = "ti,ina230", |
| 705 | .data = (void *)ina226 |
| 706 | }, |
| 707 | { |
| 708 | .compatible = "ti,ina231", |
| 709 | .data = (void *)ina226 |
| 710 | }, |
| 711 | { }, |
| 712 | }; |
| 713 | MODULE_DEVICE_TABLE(of, ina2xx_of_match); |
| 714 | |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 715 | static struct i2c_driver ina2xx_driver = { |
| 716 | .driver = { |
| 717 | .name = "ina2xx", |
Javier Martinez Canillas | bd0ddd4 | 2017-02-24 10:13:00 -0300 | [diff] [blame] | 718 | .of_match_table = of_match_ptr(ina2xx_of_match), |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 719 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 720 | .probe_new = ina2xx_probe, |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 721 | .id_table = ina2xx_id, |
| 722 | }; |
| 723 | |
Wei Yongjun | d835ca0 | 2012-10-08 20:40:35 +0800 | [diff] [blame] | 724 | module_i2c_driver(ina2xx_driver); |
Felten, Lothar | f7c2fe3 | 2012-05-12 04:36:38 -0400 | [diff] [blame] | 725 | |
| 726 | MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>"); |
| 727 | MODULE_DESCRIPTION("ina2xx driver"); |
| 728 | MODULE_LICENSE("GPL"); |