Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * INA3221 Triple Current/Voltage Monitor |
| 3 | * |
| 4 | * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * Andrew F. Davis <afd@ti.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/hwmon.h> |
| 18 | #include <linux/hwmon-sysfs.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/module.h> |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 22 | #include <linux/of.h> |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 24 | #include <linux/regmap.h> |
| 25 | |
| 26 | #define INA3221_DRIVER_NAME "ina3221" |
| 27 | |
| 28 | #define INA3221_CONFIG 0x00 |
| 29 | #define INA3221_SHUNT1 0x01 |
| 30 | #define INA3221_BUS1 0x02 |
| 31 | #define INA3221_SHUNT2 0x03 |
| 32 | #define INA3221_BUS2 0x04 |
| 33 | #define INA3221_SHUNT3 0x05 |
| 34 | #define INA3221_BUS3 0x06 |
| 35 | #define INA3221_CRIT1 0x07 |
| 36 | #define INA3221_WARN1 0x08 |
| 37 | #define INA3221_CRIT2 0x09 |
| 38 | #define INA3221_WARN2 0x0a |
| 39 | #define INA3221_CRIT3 0x0b |
| 40 | #define INA3221_WARN3 0x0c |
| 41 | #define INA3221_MASK_ENABLE 0x0f |
| 42 | |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 43 | #define INA3221_CONFIG_MODE_MASK GENMASK(2, 0) |
| 44 | #define INA3221_CONFIG_MODE_POWERDOWN 0 |
Nicolin Chen | 791ebc9 | 2018-09-29 14:44:06 -0700 | [diff] [blame] | 45 | #define INA3221_CONFIG_MODE_SHUNT BIT(0) |
| 46 | #define INA3221_CONFIG_MODE_BUS BIT(1) |
| 47 | #define INA3221_CONFIG_MODE_CONTINUOUS BIT(2) |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 48 | #define INA3221_CONFIG_VSH_CT_SHIFT 3 |
| 49 | #define INA3221_CONFIG_VSH_CT_MASK GENMASK(5, 3) |
| 50 | #define INA3221_CONFIG_VSH_CT(x) (((x) & GENMASK(5, 3)) >> 3) |
| 51 | #define INA3221_CONFIG_VBUS_CT_SHIFT 6 |
| 52 | #define INA3221_CONFIG_VBUS_CT_MASK GENMASK(8, 6) |
| 53 | #define INA3221_CONFIG_VBUS_CT(x) (((x) & GENMASK(8, 6)) >> 6) |
| 54 | #define INA3221_CONFIG_CHs_EN_MASK GENMASK(14, 12) |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 55 | #define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x)) |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 56 | |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 57 | #define INA3221_CONFIG_DEFAULT 0x7127 |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 58 | #define INA3221_RSHUNT_DEFAULT 10000 |
| 59 | |
| 60 | enum ina3221_fields { |
| 61 | /* Configuration */ |
| 62 | F_RST, |
| 63 | |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 64 | /* Status Flags */ |
| 65 | F_CVRF, |
| 66 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 67 | /* Alert Flags */ |
| 68 | F_WF3, F_WF2, F_WF1, |
| 69 | F_CF3, F_CF2, F_CF1, |
| 70 | |
| 71 | /* sentinel */ |
| 72 | F_MAX_FIELDS |
| 73 | }; |
| 74 | |
| 75 | static const struct reg_field ina3221_reg_fields[] = { |
| 76 | [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15), |
| 77 | |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 78 | [F_CVRF] = REG_FIELD(INA3221_MASK_ENABLE, 0, 0), |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 79 | [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3), |
| 80 | [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4), |
| 81 | [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5), |
| 82 | [F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7), |
| 83 | [F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8), |
| 84 | [F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9), |
| 85 | }; |
| 86 | |
| 87 | enum ina3221_channels { |
| 88 | INA3221_CHANNEL1, |
| 89 | INA3221_CHANNEL2, |
| 90 | INA3221_CHANNEL3, |
| 91 | INA3221_NUM_CHANNELS |
| 92 | }; |
| 93 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 94 | /** |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 95 | * struct ina3221_input - channel input source specific information |
| 96 | * @label: label of channel input source |
| 97 | * @shunt_resistor: shunt resistor value of channel input source |
| 98 | * @disconnected: connection status of channel input source |
| 99 | */ |
| 100 | struct ina3221_input { |
| 101 | const char *label; |
| 102 | int shunt_resistor; |
| 103 | bool disconnected; |
| 104 | }; |
| 105 | |
| 106 | /** |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 107 | * struct ina3221_data - device specific information |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 108 | * @pm_dev: Device pointer for pm runtime |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 109 | * @regmap: Register map of the device |
| 110 | * @fields: Register fields of the device |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 111 | * @inputs: Array of channel input source specific structures |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 112 | * @lock: mutex lock to serialize sysfs attribute accesses |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 113 | * @reg_config: Register value of INA3221_CONFIG |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 114 | * @single_shot: running in single-shot operating mode |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 115 | */ |
| 116 | struct ina3221_data { |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 117 | struct device *pm_dev; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 118 | struct regmap *regmap; |
| 119 | struct regmap_field *fields[F_MAX_FIELDS]; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 120 | struct ina3221_input inputs[INA3221_NUM_CHANNELS]; |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 121 | struct mutex lock; |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 122 | u32 reg_config; |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 123 | |
| 124 | bool single_shot; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 127 | static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel) |
| 128 | { |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 129 | return pm_runtime_active(ina->pm_dev) && |
| 130 | (ina->reg_config & INA3221_CONFIG_CHx_EN(channel)); |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 133 | /* Lookup table for Bus and Shunt conversion times in usec */ |
| 134 | static const u16 ina3221_conv_time[] = { |
| 135 | 140, 204, 332, 588, 1100, 2116, 4156, 8244, |
| 136 | }; |
| 137 | |
| 138 | static inline int ina3221_wait_for_data(struct ina3221_data *ina) |
| 139 | { |
| 140 | u32 channels = hweight16(ina->reg_config & INA3221_CONFIG_CHs_EN_MASK); |
| 141 | u32 vbus_ct_idx = INA3221_CONFIG_VBUS_CT(ina->reg_config); |
| 142 | u32 vsh_ct_idx = INA3221_CONFIG_VSH_CT(ina->reg_config); |
| 143 | u32 vbus_ct = ina3221_conv_time[vbus_ct_idx]; |
| 144 | u32 vsh_ct = ina3221_conv_time[vsh_ct_idx]; |
| 145 | u32 wait, cvrf; |
| 146 | |
| 147 | /* Calculate total conversion time */ |
| 148 | wait = channels * (vbus_ct + vsh_ct); |
| 149 | |
| 150 | /* Polling the CVRF bit to make sure read data is ready */ |
| 151 | return regmap_field_read_poll_timeout(ina->fields[F_CVRF], |
| 152 | cvrf, cvrf, wait, 100000); |
| 153 | } |
| 154 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 155 | static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg, |
| 156 | int *val) |
| 157 | { |
| 158 | unsigned int regval; |
| 159 | int ret; |
| 160 | |
| 161 | ret = regmap_read(ina->regmap, reg, ®val); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | *val = sign_extend32(regval >> 3, 12); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 170 | static const u8 ina3221_in_reg[] = { |
| 171 | INA3221_BUS1, |
| 172 | INA3221_BUS2, |
| 173 | INA3221_BUS3, |
| 174 | INA3221_SHUNT1, |
| 175 | INA3221_SHUNT2, |
| 176 | INA3221_SHUNT3, |
| 177 | }; |
| 178 | |
| 179 | static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val) |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 180 | { |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 181 | const bool is_shunt = channel > INA3221_CHANNEL3; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 182 | struct ina3221_data *ina = dev_get_drvdata(dev); |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 183 | u8 reg = ina3221_in_reg[channel]; |
| 184 | int regval, ret; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 185 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 186 | /* Translate shunt channel index to sensor channel index */ |
| 187 | channel %= INA3221_NUM_CHANNELS; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 188 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 189 | switch (attr) { |
| 190 | case hwmon_in_input: |
| 191 | if (!ina3221_is_enabled(ina, channel)) |
| 192 | return -ENODATA; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 193 | |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 194 | /* Write CONFIG register to trigger a single-shot measurement */ |
| 195 | if (ina->single_shot) |
| 196 | regmap_write(ina->regmap, INA3221_CONFIG, |
| 197 | ina->reg_config); |
| 198 | |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 199 | ret = ina3221_wait_for_data(ina); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 203 | ret = ina3221_read_value(ina, reg, ®val); |
| 204 | if (ret) |
| 205 | return ret; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 206 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Scale of shunt voltage (uV): LSB is 40uV |
| 209 | * Scale of bus voltage (mV): LSB is 8mV |
| 210 | */ |
| 211 | *val = regval * (is_shunt ? 40 : 8); |
| 212 | return 0; |
| 213 | case hwmon_in_enable: |
| 214 | *val = ina3221_is_enabled(ina, channel); |
| 215 | return 0; |
| 216 | default: |
| 217 | return -EOPNOTSUPP; |
| 218 | } |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 221 | static const u8 ina3221_curr_reg[][INA3221_NUM_CHANNELS] = { |
| 222 | [hwmon_curr_input] = { INA3221_SHUNT1, INA3221_SHUNT2, INA3221_SHUNT3 }, |
| 223 | [hwmon_curr_max] = { INA3221_WARN1, INA3221_WARN2, INA3221_WARN3 }, |
| 224 | [hwmon_curr_crit] = { INA3221_CRIT1, INA3221_CRIT2, INA3221_CRIT3 }, |
| 225 | [hwmon_curr_max_alarm] = { F_WF1, F_WF2, F_WF3 }, |
| 226 | [hwmon_curr_crit_alarm] = { F_CF1, F_CF2, F_CF3 }, |
| 227 | }; |
| 228 | |
| 229 | static int ina3221_read_curr(struct device *dev, u32 attr, |
| 230 | int channel, long *val) |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 231 | { |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 232 | struct ina3221_data *ina = dev_get_drvdata(dev); |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 233 | struct ina3221_input *input = &ina->inputs[channel]; |
| 234 | int resistance_uo = input->shunt_resistor; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 235 | u8 reg = ina3221_curr_reg[attr][channel]; |
| 236 | int regval, voltage_nv, ret; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 237 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 238 | switch (attr) { |
| 239 | case hwmon_curr_input: |
| 240 | if (!ina3221_is_enabled(ina, channel)) |
| 241 | return -ENODATA; |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 242 | |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 243 | /* Write CONFIG register to trigger a single-shot measurement */ |
| 244 | if (ina->single_shot) |
| 245 | regmap_write(ina->regmap, INA3221_CONFIG, |
| 246 | ina->reg_config); |
| 247 | |
Nicolin Chen | 4c0415a | 2018-11-05 12:48:42 -0800 | [diff] [blame] | 248 | ret = ina3221_wait_for_data(ina); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 252 | /* fall through */ |
| 253 | case hwmon_curr_crit: |
| 254 | case hwmon_curr_max: |
| 255 | ret = ina3221_read_value(ina, reg, ®val); |
| 256 | if (ret) |
| 257 | return ret; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 258 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 259 | /* Scale of shunt voltage: LSB is 40uV (40000nV) */ |
| 260 | voltage_nv = regval * 40000; |
| 261 | /* Return current in mA */ |
| 262 | *val = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo); |
| 263 | return 0; |
| 264 | case hwmon_curr_crit_alarm: |
| 265 | case hwmon_curr_max_alarm: |
Nicolin Chen | efb0489 | 2018-11-05 12:48:40 -0800 | [diff] [blame] | 266 | /* No actual register read if channel is disabled */ |
| 267 | if (!ina3221_is_enabled(ina, channel)) { |
| 268 | /* Return 0 for alert flags */ |
| 269 | *val = 0; |
| 270 | return 0; |
| 271 | } |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 272 | ret = regmap_field_read(ina->fields[reg], ®val); |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | *val = regval; |
| 276 | return 0; |
| 277 | default: |
| 278 | return -EOPNOTSUPP; |
| 279 | } |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 282 | static int ina3221_write_curr(struct device *dev, u32 attr, |
| 283 | int channel, long val) |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 284 | { |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 285 | struct ina3221_data *ina = dev_get_drvdata(dev); |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 286 | struct ina3221_input *input = &ina->inputs[channel]; |
| 287 | int resistance_uo = input->shunt_resistor; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 288 | u8 reg = ina3221_curr_reg[attr][channel]; |
| 289 | int regval, current_ma, voltage_uv; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 290 | |
| 291 | /* clamp current */ |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 292 | current_ma = clamp_val(val, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 293 | INT_MIN / resistance_uo, |
| 294 | INT_MAX / resistance_uo); |
| 295 | |
| 296 | voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000); |
| 297 | |
| 298 | /* clamp voltage */ |
| 299 | voltage_uv = clamp_val(voltage_uv, -163800, 163800); |
| 300 | |
| 301 | /* 1 / 40uV(scale) << 3(register shift) = 5 */ |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 302 | regval = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 303 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 304 | return regmap_write(ina->regmap, reg, regval); |
| 305 | } |
| 306 | |
| 307 | static int ina3221_write_enable(struct device *dev, int channel, bool enable) |
| 308 | { |
| 309 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 310 | u16 config, mask = INA3221_CONFIG_CHx_EN(channel); |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 311 | u16 config_old = ina->reg_config & mask; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 312 | int ret; |
| 313 | |
| 314 | config = enable ? mask : 0; |
| 315 | |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 316 | /* Bypass if enable status is not being changed */ |
| 317 | if (config_old == config) |
| 318 | return 0; |
| 319 | |
| 320 | /* For enabling routine, increase refcount and resume() at first */ |
| 321 | if (enable) { |
| 322 | ret = pm_runtime_get_sync(ina->pm_dev); |
| 323 | if (ret < 0) { |
| 324 | dev_err(dev, "Failed to get PM runtime\n"); |
| 325 | return ret; |
| 326 | } |
| 327 | } |
| 328 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 329 | /* Enable or disable the channel */ |
| 330 | ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 331 | if (ret) |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 332 | goto fail; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 333 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 334 | /* Cache the latest config register value */ |
| 335 | ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config); |
| 336 | if (ret) |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 337 | goto fail; |
| 338 | |
| 339 | /* For disabling routine, decrease refcount or suspend() at last */ |
| 340 | if (!enable) |
| 341 | pm_runtime_put_sync(ina->pm_dev); |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 342 | |
| 343 | return 0; |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 344 | |
| 345 | fail: |
| 346 | if (enable) { |
| 347 | dev_err(dev, "Failed to enable channel %d: error %d\n", |
| 348 | channel, ret); |
| 349 | pm_runtime_put_sync(ina->pm_dev); |
| 350 | } |
| 351 | |
| 352 | return ret; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 355 | static int ina3221_read(struct device *dev, enum hwmon_sensor_types type, |
| 356 | u32 attr, int channel, long *val) |
| 357 | { |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 358 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 359 | int ret; |
| 360 | |
| 361 | mutex_lock(&ina->lock); |
| 362 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 363 | switch (type) { |
| 364 | case hwmon_in: |
| 365 | /* 0-align channel ID */ |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 366 | ret = ina3221_read_in(dev, attr, channel - 1, val); |
| 367 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 368 | case hwmon_curr: |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 369 | ret = ina3221_read_curr(dev, attr, channel, val); |
| 370 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 371 | default: |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 372 | ret = -EOPNOTSUPP; |
| 373 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 374 | } |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 375 | |
| 376 | mutex_unlock(&ina->lock); |
| 377 | |
| 378 | return ret; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int ina3221_write(struct device *dev, enum hwmon_sensor_types type, |
| 382 | u32 attr, int channel, long val) |
| 383 | { |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 384 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 385 | int ret; |
| 386 | |
| 387 | mutex_lock(&ina->lock); |
| 388 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 389 | switch (type) { |
| 390 | case hwmon_in: |
| 391 | /* 0-align channel ID */ |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 392 | ret = ina3221_write_enable(dev, channel - 1, val); |
| 393 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 394 | case hwmon_curr: |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 395 | ret = ina3221_write_curr(dev, attr, channel, val); |
| 396 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 397 | default: |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 398 | ret = -EOPNOTSUPP; |
| 399 | break; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 400 | } |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 401 | |
| 402 | mutex_unlock(&ina->lock); |
| 403 | |
| 404 | return ret; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type, |
| 408 | u32 attr, int channel, const char **str) |
| 409 | { |
| 410 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 411 | int index = channel - 1; |
| 412 | |
| 413 | *str = ina->inputs[index].label; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static umode_t ina3221_is_visible(const void *drvdata, |
| 419 | enum hwmon_sensor_types type, |
| 420 | u32 attr, int channel) |
| 421 | { |
| 422 | const struct ina3221_data *ina = drvdata; |
| 423 | const struct ina3221_input *input = NULL; |
| 424 | |
| 425 | switch (type) { |
| 426 | case hwmon_in: |
| 427 | /* Ignore in0_ */ |
| 428 | if (channel == 0) |
| 429 | return 0; |
| 430 | |
| 431 | switch (attr) { |
| 432 | case hwmon_in_label: |
| 433 | if (channel - 1 <= INA3221_CHANNEL3) |
| 434 | input = &ina->inputs[channel - 1]; |
| 435 | /* Hide label node if label is not provided */ |
| 436 | return (input && input->label) ? 0444 : 0; |
| 437 | case hwmon_in_input: |
| 438 | return 0444; |
| 439 | case hwmon_in_enable: |
| 440 | return 0644; |
| 441 | default: |
| 442 | return 0; |
| 443 | } |
| 444 | case hwmon_curr: |
| 445 | switch (attr) { |
| 446 | case hwmon_curr_input: |
| 447 | case hwmon_curr_crit_alarm: |
| 448 | case hwmon_curr_max_alarm: |
| 449 | return 0444; |
| 450 | case hwmon_curr_crit: |
| 451 | case hwmon_curr_max: |
| 452 | return 0644; |
| 453 | default: |
| 454 | return 0; |
| 455 | } |
| 456 | default: |
| 457 | return 0; |
| 458 | } |
| 459 | } |
| 460 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 461 | #define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \ |
| 462 | HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \ |
| 463 | HWMON_C_MAX | HWMON_C_MAX_ALARM) |
| 464 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 465 | static const struct hwmon_channel_info *ina3221_info[] = { |
Guenter Roeck | 6f307b7 | 2019-03-31 10:53:47 -0700 | [diff] [blame^] | 466 | HWMON_CHANNEL_INFO(in, |
| 467 | /* 0: dummy, skipped in is_visible */ |
| 468 | HWMON_I_INPUT, |
| 469 | /* 1-3: input voltage Channels */ |
| 470 | HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, |
| 471 | HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, |
| 472 | HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, |
| 473 | /* 4-6: shunt voltage Channels */ |
| 474 | HWMON_I_INPUT, |
| 475 | HWMON_I_INPUT, |
| 476 | HWMON_I_INPUT), |
| 477 | HWMON_CHANNEL_INFO(curr, |
| 478 | INA3221_HWMON_CURR_CONFIG, |
| 479 | INA3221_HWMON_CURR_CONFIG, |
| 480 | INA3221_HWMON_CURR_CONFIG), |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 481 | NULL |
| 482 | }; |
| 483 | |
| 484 | static const struct hwmon_ops ina3221_hwmon_ops = { |
| 485 | .is_visible = ina3221_is_visible, |
| 486 | .read_string = ina3221_read_string, |
| 487 | .read = ina3221_read, |
| 488 | .write = ina3221_write, |
| 489 | }; |
| 490 | |
| 491 | static const struct hwmon_chip_info ina3221_chip_info = { |
| 492 | .ops = &ina3221_hwmon_ops, |
| 493 | .info = ina3221_info, |
| 494 | }; |
| 495 | |
| 496 | /* Extra attribute groups */ |
Guenter Roeck | a4ec92e | 2018-12-10 14:02:10 -0800 | [diff] [blame] | 497 | static ssize_t ina3221_shunt_show(struct device *dev, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 498 | struct device_attribute *attr, char *buf) |
| 499 | { |
| 500 | struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); |
| 501 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 502 | unsigned int channel = sd_attr->index; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 503 | struct ina3221_input *input = &ina->inputs[channel]; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 504 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 505 | return snprintf(buf, PAGE_SIZE, "%d\n", input->shunt_resistor); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Guenter Roeck | a4ec92e | 2018-12-10 14:02:10 -0800 | [diff] [blame] | 508 | static ssize_t ina3221_shunt_store(struct device *dev, |
| 509 | struct device_attribute *attr, |
| 510 | const char *buf, size_t count) |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 511 | { |
| 512 | struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); |
| 513 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 514 | unsigned int channel = sd_attr->index; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 515 | struct ina3221_input *input = &ina->inputs[channel]; |
Guenter Roeck | 9ad0df1 | 2016-06-24 19:41:57 -0700 | [diff] [blame] | 516 | int val; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 517 | int ret; |
| 518 | |
Guenter Roeck | 9ad0df1 | 2016-06-24 19:41:57 -0700 | [diff] [blame] | 519 | ret = kstrtoint(buf, 0, &val); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 520 | if (ret) |
| 521 | return ret; |
| 522 | |
Guenter Roeck | 9ad0df1 | 2016-06-24 19:41:57 -0700 | [diff] [blame] | 523 | val = clamp_val(val, 1, INT_MAX); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 524 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 525 | input->shunt_resistor = val; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 526 | |
| 527 | return count; |
| 528 | } |
| 529 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 530 | /* shunt resistance */ |
Guenter Roeck | a4ec92e | 2018-12-10 14:02:10 -0800 | [diff] [blame] | 531 | static SENSOR_DEVICE_ATTR_RW(shunt1_resistor, ina3221_shunt, INA3221_CHANNEL1); |
| 532 | static SENSOR_DEVICE_ATTR_RW(shunt2_resistor, ina3221_shunt, INA3221_CHANNEL2); |
| 533 | static SENSOR_DEVICE_ATTR_RW(shunt3_resistor, ina3221_shunt, INA3221_CHANNEL3); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 534 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 535 | static struct attribute *ina3221_attrs[] = { |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 536 | &sensor_dev_attr_shunt1_resistor.dev_attr.attr, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 537 | &sensor_dev_attr_shunt2_resistor.dev_attr.attr, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 538 | &sensor_dev_attr_shunt3_resistor.dev_attr.attr, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 539 | NULL, |
| 540 | }; |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 541 | ATTRIBUTE_GROUPS(ina3221); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 542 | |
| 543 | static const struct regmap_range ina3221_yes_ranges[] = { |
Nicolin Chen | c20217b | 2018-09-29 14:44:05 -0700 | [diff] [blame] | 544 | regmap_reg_range(INA3221_CONFIG, INA3221_BUS3), |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 545 | regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE), |
| 546 | }; |
| 547 | |
| 548 | static const struct regmap_access_table ina3221_volatile_table = { |
| 549 | .yes_ranges = ina3221_yes_ranges, |
| 550 | .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges), |
| 551 | }; |
| 552 | |
| 553 | static const struct regmap_config ina3221_regmap_config = { |
| 554 | .reg_bits = 8, |
| 555 | .val_bits = 16, |
| 556 | |
| 557 | .cache_type = REGCACHE_RBTREE, |
| 558 | .volatile_table = &ina3221_volatile_table, |
| 559 | }; |
| 560 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 561 | static int ina3221_probe_child_from_dt(struct device *dev, |
| 562 | struct device_node *child, |
| 563 | struct ina3221_data *ina) |
| 564 | { |
| 565 | struct ina3221_input *input; |
| 566 | u32 val; |
| 567 | int ret; |
| 568 | |
| 569 | ret = of_property_read_u32(child, "reg", &val); |
| 570 | if (ret) { |
Rob Herring | 1b1f4ef | 2018-11-16 16:05:38 -0600 | [diff] [blame] | 571 | dev_err(dev, "missing reg property of %pOFn\n", child); |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 572 | return ret; |
| 573 | } else if (val > INA3221_CHANNEL3) { |
Rob Herring | 1b1f4ef | 2018-11-16 16:05:38 -0600 | [diff] [blame] | 574 | dev_err(dev, "invalid reg %d of %pOFn\n", val, child); |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | input = &ina->inputs[val]; |
| 579 | |
| 580 | /* Log the disconnected channel input */ |
| 581 | if (!of_device_is_available(child)) { |
| 582 | input->disconnected = true; |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | /* Save the connected input label if available */ |
| 587 | of_property_read_string(child, "label", &input->label); |
| 588 | |
| 589 | /* Overwrite default shunt resistor value optionally */ |
Nicolin Chen | a6e4326 | 2018-10-08 14:24:51 -0700 | [diff] [blame] | 590 | if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) { |
| 591 | if (val < 1 || val > INT_MAX) { |
Rob Herring | 1b1f4ef | 2018-11-16 16:05:38 -0600 | [diff] [blame] | 592 | dev_err(dev, "invalid shunt resistor value %u of %pOFn\n", |
| 593 | val, child); |
Nicolin Chen | a6e4326 | 2018-10-08 14:24:51 -0700 | [diff] [blame] | 594 | return -EINVAL; |
| 595 | } |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 596 | input->shunt_resistor = val; |
Nicolin Chen | a6e4326 | 2018-10-08 14:24:51 -0700 | [diff] [blame] | 597 | } |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int ina3221_probe_from_dt(struct device *dev, struct ina3221_data *ina) |
| 603 | { |
| 604 | const struct device_node *np = dev->of_node; |
| 605 | struct device_node *child; |
| 606 | int ret; |
| 607 | |
| 608 | /* Compatible with non-DT platforms */ |
| 609 | if (!np) |
| 610 | return 0; |
| 611 | |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 612 | ina->single_shot = of_property_read_bool(np, "ti,single-shot"); |
| 613 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 614 | for_each_child_of_node(np, child) { |
| 615 | ret = ina3221_probe_child_from_dt(dev, child, ina); |
| 616 | if (ret) |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 623 | static int ina3221_probe(struct i2c_client *client, |
| 624 | const struct i2c_device_id *id) |
| 625 | { |
| 626 | struct device *dev = &client->dev; |
| 627 | struct ina3221_data *ina; |
| 628 | struct device *hwmon_dev; |
| 629 | int i, ret; |
| 630 | |
| 631 | ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL); |
| 632 | if (!ina) |
| 633 | return -ENOMEM; |
| 634 | |
| 635 | ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config); |
| 636 | if (IS_ERR(ina->regmap)) { |
| 637 | dev_err(dev, "Unable to allocate register map\n"); |
| 638 | return PTR_ERR(ina->regmap); |
| 639 | } |
| 640 | |
| 641 | for (i = 0; i < F_MAX_FIELDS; i++) { |
| 642 | ina->fields[i] = devm_regmap_field_alloc(dev, |
| 643 | ina->regmap, |
| 644 | ina3221_reg_fields[i]); |
| 645 | if (IS_ERR(ina->fields[i])) { |
| 646 | dev_err(dev, "Unable to allocate regmap fields\n"); |
| 647 | return PTR_ERR(ina->fields[i]); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | for (i = 0; i < INA3221_NUM_CHANNELS; i++) |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 652 | ina->inputs[i].shunt_resistor = INA3221_RSHUNT_DEFAULT; |
| 653 | |
| 654 | ret = ina3221_probe_from_dt(dev, ina); |
| 655 | if (ret) { |
| 656 | dev_err(dev, "Unable to probe from device tree\n"); |
| 657 | return ret; |
| 658 | } |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 659 | |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 660 | /* The driver will be reset, so use reset value */ |
| 661 | ina->reg_config = INA3221_CONFIG_DEFAULT; |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 662 | |
Nicolin Chen | 43dece1 | 2019-01-17 15:12:53 -0800 | [diff] [blame] | 663 | /* Clear continuous bit to use single-shot mode */ |
| 664 | if (ina->single_shot) |
| 665 | ina->reg_config &= ~INA3221_CONFIG_MODE_CONTINUOUS; |
| 666 | |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 667 | /* Disable channels if their inputs are disconnected */ |
| 668 | for (i = 0; i < INA3221_NUM_CHANNELS; i++) { |
| 669 | if (ina->inputs[i].disconnected) |
| 670 | ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i); |
| 671 | } |
Nicolin Chen | a9e9dd9 | 2018-10-01 18:05:23 -0700 | [diff] [blame] | 672 | |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 673 | ina->pm_dev = dev; |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 674 | mutex_init(&ina->lock); |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 675 | dev_set_drvdata(dev, ina); |
| 676 | |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 677 | /* Enable PM runtime -- status is suspended by default */ |
| 678 | pm_runtime_enable(ina->pm_dev); |
| 679 | |
| 680 | /* Initialize (resume) the device */ |
| 681 | for (i = 0; i < INA3221_NUM_CHANNELS; i++) { |
| 682 | if (ina->inputs[i].disconnected) |
| 683 | continue; |
| 684 | /* Match the refcount with number of enabled channels */ |
| 685 | ret = pm_runtime_get_sync(ina->pm_dev); |
| 686 | if (ret < 0) |
| 687 | goto fail; |
| 688 | } |
| 689 | |
Nicolin Chen | d4b0166 | 2018-10-08 13:14:24 -0700 | [diff] [blame] | 690 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina, |
| 691 | &ina3221_chip_info, |
| 692 | ina3221_groups); |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 693 | if (IS_ERR(hwmon_dev)) { |
| 694 | dev_err(dev, "Unable to register hwmon device\n"); |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 695 | ret = PTR_ERR(hwmon_dev); |
| 696 | goto fail; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | return 0; |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 700 | |
| 701 | fail: |
| 702 | pm_runtime_disable(ina->pm_dev); |
| 703 | pm_runtime_set_suspended(ina->pm_dev); |
| 704 | /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */ |
| 705 | for (i = 0; i < INA3221_NUM_CHANNELS; i++) |
| 706 | pm_runtime_put_noidle(ina->pm_dev); |
| 707 | mutex_destroy(&ina->lock); |
| 708 | |
| 709 | return ret; |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 712 | static int ina3221_remove(struct i2c_client *client) |
| 713 | { |
| 714 | struct ina3221_data *ina = dev_get_drvdata(&client->dev); |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 715 | int i; |
| 716 | |
| 717 | pm_runtime_disable(ina->pm_dev); |
| 718 | pm_runtime_set_suspended(ina->pm_dev); |
| 719 | |
| 720 | /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */ |
| 721 | for (i = 0; i < INA3221_NUM_CHANNELS; i++) |
| 722 | pm_runtime_put_noidle(ina->pm_dev); |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 723 | |
| 724 | mutex_destroy(&ina->lock); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
Arnd Bergmann | ead21c7 | 2018-10-02 23:10:47 +0200 | [diff] [blame] | 729 | static int __maybe_unused ina3221_suspend(struct device *dev) |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 730 | { |
| 731 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 732 | int ret; |
| 733 | |
| 734 | /* Save config register value and enable cache-only */ |
| 735 | ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config); |
| 736 | if (ret) |
| 737 | return ret; |
| 738 | |
| 739 | /* Set to power-down mode for power saving */ |
| 740 | ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, |
| 741 | INA3221_CONFIG_MODE_MASK, |
| 742 | INA3221_CONFIG_MODE_POWERDOWN); |
| 743 | if (ret) |
| 744 | return ret; |
| 745 | |
| 746 | regcache_cache_only(ina->regmap, true); |
| 747 | regcache_mark_dirty(ina->regmap); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
Arnd Bergmann | ead21c7 | 2018-10-02 23:10:47 +0200 | [diff] [blame] | 752 | static int __maybe_unused ina3221_resume(struct device *dev) |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 753 | { |
| 754 | struct ina3221_data *ina = dev_get_drvdata(dev); |
| 755 | int ret; |
| 756 | |
| 757 | regcache_cache_only(ina->regmap, false); |
| 758 | |
| 759 | /* Software reset the chip */ |
| 760 | ret = regmap_field_write(ina->fields[F_RST], true); |
| 761 | if (ret) { |
| 762 | dev_err(dev, "Unable to reset device\n"); |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /* Restore cached register values to hardware */ |
| 767 | ret = regcache_sync(ina->regmap); |
| 768 | if (ret) |
| 769 | return ret; |
| 770 | |
| 771 | /* Restore config register value to hardware */ |
| 772 | ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | |
| 776 | return 0; |
| 777 | } |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 778 | |
| 779 | static const struct dev_pm_ops ina3221_pm = { |
Nicolin Chen | 323aeb0 | 2018-11-05 12:48:43 -0800 | [diff] [blame] | 780 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 781 | pm_runtime_force_resume) |
| 782 | SET_RUNTIME_PM_OPS(ina3221_suspend, ina3221_resume, NULL) |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 783 | }; |
| 784 | |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 785 | static const struct of_device_id ina3221_of_match_table[] = { |
| 786 | { .compatible = "ti,ina3221", }, |
| 787 | { /* sentinel */ } |
| 788 | }; |
| 789 | MODULE_DEVICE_TABLE(of, ina3221_of_match_table); |
| 790 | |
| 791 | static const struct i2c_device_id ina3221_ids[] = { |
| 792 | { "ina3221", 0 }, |
| 793 | { /* sentinel */ } |
| 794 | }; |
| 795 | MODULE_DEVICE_TABLE(i2c, ina3221_ids); |
| 796 | |
| 797 | static struct i2c_driver ina3221_i2c_driver = { |
| 798 | .probe = ina3221_probe, |
Nicolin Chen | 87625b2 | 2018-11-05 12:48:41 -0800 | [diff] [blame] | 799 | .remove = ina3221_remove, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 800 | .driver = { |
| 801 | .name = INA3221_DRIVER_NAME, |
| 802 | .of_match_table = ina3221_of_match_table, |
Nicolin Chen | 59d608e | 2018-09-29 14:44:07 -0700 | [diff] [blame] | 803 | .pm = &ina3221_pm, |
Andrew F. Davis | 7cb6dcf | 2016-06-10 10:32:33 -0500 | [diff] [blame] | 804 | }, |
| 805 | .id_table = ina3221_ids, |
| 806 | }; |
| 807 | module_i2c_driver(ina3221_i2c_driver); |
| 808 | |
| 809 | MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); |
| 810 | MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver"); |
| 811 | MODULE_LICENSE("GPL v2"); |