blob: 07dd6ef58d3e1608909135378c929576a4f3d827 [file] [log] [blame]
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -05001/*
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 Chen87625b22018-11-05 12:48:41 -080021#include <linux/mutex.h>
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -050022#include <linux/of.h>
23#include <linux/regmap.h>
24
25#define INA3221_DRIVER_NAME "ina3221"
26
27#define INA3221_CONFIG 0x00
28#define INA3221_SHUNT1 0x01
29#define INA3221_BUS1 0x02
30#define INA3221_SHUNT2 0x03
31#define INA3221_BUS2 0x04
32#define INA3221_SHUNT3 0x05
33#define INA3221_BUS3 0x06
34#define INA3221_CRIT1 0x07
35#define INA3221_WARN1 0x08
36#define INA3221_CRIT2 0x09
37#define INA3221_WARN2 0x0a
38#define INA3221_CRIT3 0x0b
39#define INA3221_WARN3 0x0c
40#define INA3221_MASK_ENABLE 0x0f
41
Nicolin Chen59d608e2018-09-29 14:44:07 -070042#define INA3221_CONFIG_MODE_MASK GENMASK(2, 0)
43#define INA3221_CONFIG_MODE_POWERDOWN 0
Nicolin Chen791ebc92018-09-29 14:44:06 -070044#define INA3221_CONFIG_MODE_SHUNT BIT(0)
45#define INA3221_CONFIG_MODE_BUS BIT(1)
46#define INA3221_CONFIG_MODE_CONTINUOUS BIT(2)
Nicolin Chen4c0415a2018-11-05 12:48:42 -080047#define INA3221_CONFIG_VSH_CT_SHIFT 3
48#define INA3221_CONFIG_VSH_CT_MASK GENMASK(5, 3)
49#define INA3221_CONFIG_VSH_CT(x) (((x) & GENMASK(5, 3)) >> 3)
50#define INA3221_CONFIG_VBUS_CT_SHIFT 6
51#define INA3221_CONFIG_VBUS_CT_MASK GENMASK(8, 6)
52#define INA3221_CONFIG_VBUS_CT(x) (((x) & GENMASK(8, 6)) >> 6)
53#define INA3221_CONFIG_CHs_EN_MASK GENMASK(14, 12)
Nicolin Chena9e9dd92018-10-01 18:05:23 -070054#define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x))
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -050055
56#define INA3221_RSHUNT_DEFAULT 10000
57
58enum ina3221_fields {
59 /* Configuration */
60 F_RST,
61
Nicolin Chen4c0415a2018-11-05 12:48:42 -080062 /* Status Flags */
63 F_CVRF,
64
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -050065 /* Alert Flags */
66 F_WF3, F_WF2, F_WF1,
67 F_CF3, F_CF2, F_CF1,
68
69 /* sentinel */
70 F_MAX_FIELDS
71};
72
73static const struct reg_field ina3221_reg_fields[] = {
74 [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15),
75
Nicolin Chen4c0415a2018-11-05 12:48:42 -080076 [F_CVRF] = REG_FIELD(INA3221_MASK_ENABLE, 0, 0),
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -050077 [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3),
78 [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4),
79 [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5),
80 [F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7),
81 [F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8),
82 [F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9),
83};
84
85enum ina3221_channels {
86 INA3221_CHANNEL1,
87 INA3221_CHANNEL2,
88 INA3221_CHANNEL3,
89 INA3221_NUM_CHANNELS
90};
91
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -050092/**
Nicolin Chena9e9dd92018-10-01 18:05:23 -070093 * struct ina3221_input - channel input source specific information
94 * @label: label of channel input source
95 * @shunt_resistor: shunt resistor value of channel input source
96 * @disconnected: connection status of channel input source
97 */
98struct ina3221_input {
99 const char *label;
100 int shunt_resistor;
101 bool disconnected;
102};
103
104/**
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500105 * struct ina3221_data - device specific information
106 * @regmap: Register map of the device
107 * @fields: Register fields of the device
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700108 * @inputs: Array of channel input source specific structures
Nicolin Chen87625b22018-11-05 12:48:41 -0800109 * @lock: mutex lock to serialize sysfs attribute accesses
Nicolin Chen59d608e2018-09-29 14:44:07 -0700110 * @reg_config: Register value of INA3221_CONFIG
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500111 */
112struct ina3221_data {
113 struct regmap *regmap;
114 struct regmap_field *fields[F_MAX_FIELDS];
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700115 struct ina3221_input inputs[INA3221_NUM_CHANNELS];
Nicolin Chen87625b22018-11-05 12:48:41 -0800116 struct mutex lock;
Nicolin Chen59d608e2018-09-29 14:44:07 -0700117 u32 reg_config;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500118};
119
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700120static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel)
121{
122 return ina->reg_config & INA3221_CONFIG_CHx_EN(channel);
123}
124
Nicolin Chen4c0415a2018-11-05 12:48:42 -0800125/* Lookup table for Bus and Shunt conversion times in usec */
126static const u16 ina3221_conv_time[] = {
127 140, 204, 332, 588, 1100, 2116, 4156, 8244,
128};
129
130static inline int ina3221_wait_for_data(struct ina3221_data *ina)
131{
132 u32 channels = hweight16(ina->reg_config & INA3221_CONFIG_CHs_EN_MASK);
133 u32 vbus_ct_idx = INA3221_CONFIG_VBUS_CT(ina->reg_config);
134 u32 vsh_ct_idx = INA3221_CONFIG_VSH_CT(ina->reg_config);
135 u32 vbus_ct = ina3221_conv_time[vbus_ct_idx];
136 u32 vsh_ct = ina3221_conv_time[vsh_ct_idx];
137 u32 wait, cvrf;
138
139 /* Calculate total conversion time */
140 wait = channels * (vbus_ct + vsh_ct);
141
142 /* Polling the CVRF bit to make sure read data is ready */
143 return regmap_field_read_poll_timeout(ina->fields[F_CVRF],
144 cvrf, cvrf, wait, 100000);
145}
146
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500147static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
148 int *val)
149{
150 unsigned int regval;
151 int ret;
152
153 ret = regmap_read(ina->regmap, reg, &regval);
154 if (ret)
155 return ret;
156
157 *val = sign_extend32(regval >> 3, 12);
158
159 return 0;
160}
161
Nicolin Chend4b01662018-10-08 13:14:24 -0700162static const u8 ina3221_in_reg[] = {
163 INA3221_BUS1,
164 INA3221_BUS2,
165 INA3221_BUS3,
166 INA3221_SHUNT1,
167 INA3221_SHUNT2,
168 INA3221_SHUNT3,
169};
170
171static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val)
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500172{
Nicolin Chend4b01662018-10-08 13:14:24 -0700173 const bool is_shunt = channel > INA3221_CHANNEL3;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500174 struct ina3221_data *ina = dev_get_drvdata(dev);
Nicolin Chend4b01662018-10-08 13:14:24 -0700175 u8 reg = ina3221_in_reg[channel];
176 int regval, ret;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500177
Nicolin Chend4b01662018-10-08 13:14:24 -0700178 /* Translate shunt channel index to sensor channel index */
179 channel %= INA3221_NUM_CHANNELS;
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700180
Nicolin Chend4b01662018-10-08 13:14:24 -0700181 switch (attr) {
182 case hwmon_in_input:
183 if (!ina3221_is_enabled(ina, channel))
184 return -ENODATA;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500185
Nicolin Chen4c0415a2018-11-05 12:48:42 -0800186 ret = ina3221_wait_for_data(ina);
187 if (ret)
188 return ret;
189
Nicolin Chend4b01662018-10-08 13:14:24 -0700190 ret = ina3221_read_value(ina, reg, &regval);
191 if (ret)
192 return ret;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500193
Nicolin Chend4b01662018-10-08 13:14:24 -0700194 /*
195 * Scale of shunt voltage (uV): LSB is 40uV
196 * Scale of bus voltage (mV): LSB is 8mV
197 */
198 *val = regval * (is_shunt ? 40 : 8);
199 return 0;
200 case hwmon_in_enable:
201 *val = ina3221_is_enabled(ina, channel);
202 return 0;
203 default:
204 return -EOPNOTSUPP;
205 }
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500206}
207
Nicolin Chend4b01662018-10-08 13:14:24 -0700208static const u8 ina3221_curr_reg[][INA3221_NUM_CHANNELS] = {
209 [hwmon_curr_input] = { INA3221_SHUNT1, INA3221_SHUNT2, INA3221_SHUNT3 },
210 [hwmon_curr_max] = { INA3221_WARN1, INA3221_WARN2, INA3221_WARN3 },
211 [hwmon_curr_crit] = { INA3221_CRIT1, INA3221_CRIT2, INA3221_CRIT3 },
212 [hwmon_curr_max_alarm] = { F_WF1, F_WF2, F_WF3 },
213 [hwmon_curr_crit_alarm] = { F_CF1, F_CF2, F_CF3 },
214};
215
216static int ina3221_read_curr(struct device *dev, u32 attr,
217 int channel, long *val)
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500218{
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500219 struct ina3221_data *ina = dev_get_drvdata(dev);
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700220 struct ina3221_input *input = &ina->inputs[channel];
221 int resistance_uo = input->shunt_resistor;
Nicolin Chend4b01662018-10-08 13:14:24 -0700222 u8 reg = ina3221_curr_reg[attr][channel];
223 int regval, voltage_nv, ret;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500224
Nicolin Chend4b01662018-10-08 13:14:24 -0700225 switch (attr) {
226 case hwmon_curr_input:
227 if (!ina3221_is_enabled(ina, channel))
228 return -ENODATA;
Nicolin Chen4c0415a2018-11-05 12:48:42 -0800229
230 ret = ina3221_wait_for_data(ina);
231 if (ret)
232 return ret;
233
Nicolin Chend4b01662018-10-08 13:14:24 -0700234 /* fall through */
235 case hwmon_curr_crit:
236 case hwmon_curr_max:
237 ret = ina3221_read_value(ina, reg, &regval);
238 if (ret)
239 return ret;
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700240
Nicolin Chend4b01662018-10-08 13:14:24 -0700241 /* Scale of shunt voltage: LSB is 40uV (40000nV) */
242 voltage_nv = regval * 40000;
243 /* Return current in mA */
244 *val = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo);
245 return 0;
246 case hwmon_curr_crit_alarm:
247 case hwmon_curr_max_alarm:
Nicolin Chenefb04892018-11-05 12:48:40 -0800248 /* No actual register read if channel is disabled */
249 if (!ina3221_is_enabled(ina, channel)) {
250 /* Return 0 for alert flags */
251 *val = 0;
252 return 0;
253 }
Nicolin Chend4b01662018-10-08 13:14:24 -0700254 ret = regmap_field_read(ina->fields[reg], &regval);
255 if (ret)
256 return ret;
257 *val = regval;
258 return 0;
259 default:
260 return -EOPNOTSUPP;
261 }
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500262}
263
Nicolin Chend4b01662018-10-08 13:14:24 -0700264static int ina3221_write_curr(struct device *dev, u32 attr,
265 int channel, long val)
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500266{
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500267 struct ina3221_data *ina = dev_get_drvdata(dev);
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700268 struct ina3221_input *input = &ina->inputs[channel];
269 int resistance_uo = input->shunt_resistor;
Nicolin Chend4b01662018-10-08 13:14:24 -0700270 u8 reg = ina3221_curr_reg[attr][channel];
271 int regval, current_ma, voltage_uv;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500272
273 /* clamp current */
Nicolin Chend4b01662018-10-08 13:14:24 -0700274 current_ma = clamp_val(val,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500275 INT_MIN / resistance_uo,
276 INT_MAX / resistance_uo);
277
278 voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
279
280 /* clamp voltage */
281 voltage_uv = clamp_val(voltage_uv, -163800, 163800);
282
283 /* 1 / 40uV(scale) << 3(register shift) = 5 */
Nicolin Chend4b01662018-10-08 13:14:24 -0700284 regval = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500285
Nicolin Chend4b01662018-10-08 13:14:24 -0700286 return regmap_write(ina->regmap, reg, regval);
287}
288
289static int ina3221_write_enable(struct device *dev, int channel, bool enable)
290{
291 struct ina3221_data *ina = dev_get_drvdata(dev);
292 u16 config, mask = INA3221_CONFIG_CHx_EN(channel);
293 int ret;
294
295 config = enable ? mask : 0;
296
297 /* Enable or disable the channel */
298 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500299 if (ret)
300 return ret;
301
Nicolin Chend4b01662018-10-08 13:14:24 -0700302 /* Cache the latest config register value */
303 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
304 if (ret)
305 return ret;
306
307 return 0;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500308}
309
Nicolin Chend4b01662018-10-08 13:14:24 -0700310static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
311 u32 attr, int channel, long *val)
312{
Nicolin Chen87625b22018-11-05 12:48:41 -0800313 struct ina3221_data *ina = dev_get_drvdata(dev);
314 int ret;
315
316 mutex_lock(&ina->lock);
317
Nicolin Chend4b01662018-10-08 13:14:24 -0700318 switch (type) {
319 case hwmon_in:
320 /* 0-align channel ID */
Nicolin Chen87625b22018-11-05 12:48:41 -0800321 ret = ina3221_read_in(dev, attr, channel - 1, val);
322 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700323 case hwmon_curr:
Nicolin Chen87625b22018-11-05 12:48:41 -0800324 ret = ina3221_read_curr(dev, attr, channel, val);
325 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700326 default:
Nicolin Chen87625b22018-11-05 12:48:41 -0800327 ret = -EOPNOTSUPP;
328 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700329 }
Nicolin Chen87625b22018-11-05 12:48:41 -0800330
331 mutex_unlock(&ina->lock);
332
333 return ret;
Nicolin Chend4b01662018-10-08 13:14:24 -0700334}
335
336static int ina3221_write(struct device *dev, enum hwmon_sensor_types type,
337 u32 attr, int channel, long val)
338{
Nicolin Chen87625b22018-11-05 12:48:41 -0800339 struct ina3221_data *ina = dev_get_drvdata(dev);
340 int ret;
341
342 mutex_lock(&ina->lock);
343
Nicolin Chend4b01662018-10-08 13:14:24 -0700344 switch (type) {
345 case hwmon_in:
346 /* 0-align channel ID */
Nicolin Chen87625b22018-11-05 12:48:41 -0800347 ret = ina3221_write_enable(dev, channel - 1, val);
348 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700349 case hwmon_curr:
Nicolin Chen87625b22018-11-05 12:48:41 -0800350 ret = ina3221_write_curr(dev, attr, channel, val);
351 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700352 default:
Nicolin Chen87625b22018-11-05 12:48:41 -0800353 ret = -EOPNOTSUPP;
354 break;
Nicolin Chend4b01662018-10-08 13:14:24 -0700355 }
Nicolin Chen87625b22018-11-05 12:48:41 -0800356
357 mutex_unlock(&ina->lock);
358
359 return ret;
Nicolin Chend4b01662018-10-08 13:14:24 -0700360}
361
362static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type,
363 u32 attr, int channel, const char **str)
364{
365 struct ina3221_data *ina = dev_get_drvdata(dev);
366 int index = channel - 1;
367
368 *str = ina->inputs[index].label;
369
370 return 0;
371}
372
373static umode_t ina3221_is_visible(const void *drvdata,
374 enum hwmon_sensor_types type,
375 u32 attr, int channel)
376{
377 const struct ina3221_data *ina = drvdata;
378 const struct ina3221_input *input = NULL;
379
380 switch (type) {
381 case hwmon_in:
382 /* Ignore in0_ */
383 if (channel == 0)
384 return 0;
385
386 switch (attr) {
387 case hwmon_in_label:
388 if (channel - 1 <= INA3221_CHANNEL3)
389 input = &ina->inputs[channel - 1];
390 /* Hide label node if label is not provided */
391 return (input && input->label) ? 0444 : 0;
392 case hwmon_in_input:
393 return 0444;
394 case hwmon_in_enable:
395 return 0644;
396 default:
397 return 0;
398 }
399 case hwmon_curr:
400 switch (attr) {
401 case hwmon_curr_input:
402 case hwmon_curr_crit_alarm:
403 case hwmon_curr_max_alarm:
404 return 0444;
405 case hwmon_curr_crit:
406 case hwmon_curr_max:
407 return 0644;
408 default:
409 return 0;
410 }
411 default:
412 return 0;
413 }
414}
415
416static const u32 ina3221_in_config[] = {
417 /* 0: dummy, skipped in is_visible */
418 HWMON_I_INPUT,
419 /* 1-3: input voltage Channels */
420 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
421 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
422 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
423 /* 4-6: shunt voltage Channels */
424 HWMON_I_INPUT,
425 HWMON_I_INPUT,
426 HWMON_I_INPUT,
427 0
428};
429
430static const struct hwmon_channel_info ina3221_in = {
431 .type = hwmon_in,
432 .config = ina3221_in_config,
433};
434
435#define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \
436 HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \
437 HWMON_C_MAX | HWMON_C_MAX_ALARM)
438
439static const u32 ina3221_curr_config[] = {
440 INA3221_HWMON_CURR_CONFIG,
441 INA3221_HWMON_CURR_CONFIG,
442 INA3221_HWMON_CURR_CONFIG,
443 0
444};
445
446static const struct hwmon_channel_info ina3221_curr = {
447 .type = hwmon_curr,
448 .config = ina3221_curr_config,
449};
450
451static const struct hwmon_channel_info *ina3221_info[] = {
452 &ina3221_in,
453 &ina3221_curr,
454 NULL
455};
456
457static const struct hwmon_ops ina3221_hwmon_ops = {
458 .is_visible = ina3221_is_visible,
459 .read_string = ina3221_read_string,
460 .read = ina3221_read,
461 .write = ina3221_write,
462};
463
464static const struct hwmon_chip_info ina3221_chip_info = {
465 .ops = &ina3221_hwmon_ops,
466 .info = ina3221_info,
467};
468
469/* Extra attribute groups */
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500470static ssize_t ina3221_show_shunt(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
473 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
474 struct ina3221_data *ina = dev_get_drvdata(dev);
475 unsigned int channel = sd_attr->index;
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700476 struct ina3221_input *input = &ina->inputs[channel];
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500477
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700478 return snprintf(buf, PAGE_SIZE, "%d\n", input->shunt_resistor);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500479}
480
481static ssize_t ina3221_set_shunt(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
484{
485 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
486 struct ina3221_data *ina = dev_get_drvdata(dev);
487 unsigned int channel = sd_attr->index;
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700488 struct ina3221_input *input = &ina->inputs[channel];
Guenter Roeck9ad0df12016-06-24 19:41:57 -0700489 int val;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500490 int ret;
491
Guenter Roeck9ad0df12016-06-24 19:41:57 -0700492 ret = kstrtoint(buf, 0, &val);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500493 if (ret)
494 return ret;
495
Guenter Roeck9ad0df12016-06-24 19:41:57 -0700496 val = clamp_val(val, 1, INT_MAX);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500497
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700498 input->shunt_resistor = val;
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500499
500 return count;
501}
502
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500503/* shunt resistance */
504static SENSOR_DEVICE_ATTR(shunt1_resistor, S_IRUGO | S_IWUSR,
505 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL1);
506static SENSOR_DEVICE_ATTR(shunt2_resistor, S_IRUGO | S_IWUSR,
507 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL2);
508static SENSOR_DEVICE_ATTR(shunt3_resistor, S_IRUGO | S_IWUSR,
509 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL3);
510
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500511static struct attribute *ina3221_attrs[] = {
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500512 &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500513 &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500514 &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500515 NULL,
516};
Nicolin Chend4b01662018-10-08 13:14:24 -0700517ATTRIBUTE_GROUPS(ina3221);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500518
519static const struct regmap_range ina3221_yes_ranges[] = {
Nicolin Chenc20217b2018-09-29 14:44:05 -0700520 regmap_reg_range(INA3221_CONFIG, INA3221_BUS3),
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500521 regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
522};
523
524static const struct regmap_access_table ina3221_volatile_table = {
525 .yes_ranges = ina3221_yes_ranges,
526 .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
527};
528
529static const struct regmap_config ina3221_regmap_config = {
530 .reg_bits = 8,
531 .val_bits = 16,
532
533 .cache_type = REGCACHE_RBTREE,
534 .volatile_table = &ina3221_volatile_table,
535};
536
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700537static int ina3221_probe_child_from_dt(struct device *dev,
538 struct device_node *child,
539 struct ina3221_data *ina)
540{
541 struct ina3221_input *input;
542 u32 val;
543 int ret;
544
545 ret = of_property_read_u32(child, "reg", &val);
546 if (ret) {
547 dev_err(dev, "missing reg property of %s\n", child->name);
548 return ret;
549 } else if (val > INA3221_CHANNEL3) {
550 dev_err(dev, "invalid reg %d of %s\n", val, child->name);
551 return ret;
552 }
553
554 input = &ina->inputs[val];
555
556 /* Log the disconnected channel input */
557 if (!of_device_is_available(child)) {
558 input->disconnected = true;
559 return 0;
560 }
561
562 /* Save the connected input label if available */
563 of_property_read_string(child, "label", &input->label);
564
565 /* Overwrite default shunt resistor value optionally */
Nicolin Chena6e43262018-10-08 14:24:51 -0700566 if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
567 if (val < 1 || val > INT_MAX) {
568 dev_err(dev, "invalid shunt resistor value %u of %s\n",
569 val, child->name);
570 return -EINVAL;
571 }
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700572 input->shunt_resistor = val;
Nicolin Chena6e43262018-10-08 14:24:51 -0700573 }
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700574
575 return 0;
576}
577
578static int ina3221_probe_from_dt(struct device *dev, struct ina3221_data *ina)
579{
580 const struct device_node *np = dev->of_node;
581 struct device_node *child;
582 int ret;
583
584 /* Compatible with non-DT platforms */
585 if (!np)
586 return 0;
587
588 for_each_child_of_node(np, child) {
589 ret = ina3221_probe_child_from_dt(dev, child, ina);
590 if (ret)
591 return ret;
592 }
593
594 return 0;
595}
596
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500597static int ina3221_probe(struct i2c_client *client,
598 const struct i2c_device_id *id)
599{
600 struct device *dev = &client->dev;
601 struct ina3221_data *ina;
602 struct device *hwmon_dev;
603 int i, ret;
604
605 ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
606 if (!ina)
607 return -ENOMEM;
608
609 ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
610 if (IS_ERR(ina->regmap)) {
611 dev_err(dev, "Unable to allocate register map\n");
612 return PTR_ERR(ina->regmap);
613 }
614
615 for (i = 0; i < F_MAX_FIELDS; i++) {
616 ina->fields[i] = devm_regmap_field_alloc(dev,
617 ina->regmap,
618 ina3221_reg_fields[i]);
619 if (IS_ERR(ina->fields[i])) {
620 dev_err(dev, "Unable to allocate regmap fields\n");
621 return PTR_ERR(ina->fields[i]);
622 }
623 }
624
625 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700626 ina->inputs[i].shunt_resistor = INA3221_RSHUNT_DEFAULT;
627
628 ret = ina3221_probe_from_dt(dev, ina);
629 if (ret) {
630 dev_err(dev, "Unable to probe from device tree\n");
631 return ret;
632 }
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500633
634 ret = regmap_field_write(ina->fields[F_RST], true);
635 if (ret) {
636 dev_err(dev, "Unable to reset device\n");
637 return ret;
638 }
639
Nicolin Chena9e9dd92018-10-01 18:05:23 -0700640 /* Sync config register after reset */
641 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
642 if (ret)
643 return ret;
644
645 /* Disable channels if their inputs are disconnected */
646 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
647 if (ina->inputs[i].disconnected)
648 ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i);
649 }
650 ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
651 if (ret)
652 return ret;
653
Nicolin Chen87625b22018-11-05 12:48:41 -0800654 mutex_init(&ina->lock);
Nicolin Chen59d608e2018-09-29 14:44:07 -0700655 dev_set_drvdata(dev, ina);
656
Nicolin Chend4b01662018-10-08 13:14:24 -0700657 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina,
658 &ina3221_chip_info,
659 ina3221_groups);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500660 if (IS_ERR(hwmon_dev)) {
661 dev_err(dev, "Unable to register hwmon device\n");
Nicolin Chen87625b22018-11-05 12:48:41 -0800662 mutex_destroy(&ina->lock);
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500663 return PTR_ERR(hwmon_dev);
664 }
665
666 return 0;
667}
668
Nicolin Chen87625b22018-11-05 12:48:41 -0800669static int ina3221_remove(struct i2c_client *client)
670{
671 struct ina3221_data *ina = dev_get_drvdata(&client->dev);
672
673 mutex_destroy(&ina->lock);
674
675 return 0;
676}
677
Arnd Bergmannead21c72018-10-02 23:10:47 +0200678static int __maybe_unused ina3221_suspend(struct device *dev)
Nicolin Chen59d608e2018-09-29 14:44:07 -0700679{
680 struct ina3221_data *ina = dev_get_drvdata(dev);
681 int ret;
682
683 /* Save config register value and enable cache-only */
684 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
685 if (ret)
686 return ret;
687
688 /* Set to power-down mode for power saving */
689 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG,
690 INA3221_CONFIG_MODE_MASK,
691 INA3221_CONFIG_MODE_POWERDOWN);
692 if (ret)
693 return ret;
694
695 regcache_cache_only(ina->regmap, true);
696 regcache_mark_dirty(ina->regmap);
697
698 return 0;
699}
700
Arnd Bergmannead21c72018-10-02 23:10:47 +0200701static int __maybe_unused ina3221_resume(struct device *dev)
Nicolin Chen59d608e2018-09-29 14:44:07 -0700702{
703 struct ina3221_data *ina = dev_get_drvdata(dev);
704 int ret;
705
706 regcache_cache_only(ina->regmap, false);
707
708 /* Software reset the chip */
709 ret = regmap_field_write(ina->fields[F_RST], true);
710 if (ret) {
711 dev_err(dev, "Unable to reset device\n");
712 return ret;
713 }
714
715 /* Restore cached register values to hardware */
716 ret = regcache_sync(ina->regmap);
717 if (ret)
718 return ret;
719
720 /* Restore config register value to hardware */
721 ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
722 if (ret)
723 return ret;
724
725 return 0;
726}
Nicolin Chen59d608e2018-09-29 14:44:07 -0700727
728static const struct dev_pm_ops ina3221_pm = {
729 SET_SYSTEM_SLEEP_PM_OPS(ina3221_suspend, ina3221_resume)
730};
731
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500732static const struct of_device_id ina3221_of_match_table[] = {
733 { .compatible = "ti,ina3221", },
734 { /* sentinel */ }
735};
736MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
737
738static const struct i2c_device_id ina3221_ids[] = {
739 { "ina3221", 0 },
740 { /* sentinel */ }
741};
742MODULE_DEVICE_TABLE(i2c, ina3221_ids);
743
744static struct i2c_driver ina3221_i2c_driver = {
745 .probe = ina3221_probe,
Nicolin Chen87625b22018-11-05 12:48:41 -0800746 .remove = ina3221_remove,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500747 .driver = {
748 .name = INA3221_DRIVER_NAME,
749 .of_match_table = ina3221_of_match_table,
Nicolin Chen59d608e2018-09-29 14:44:07 -0700750 .pm = &ina3221_pm,
Andrew F. Davis7cb6dcf2016-06-10 10:32:33 -0500751 },
752 .id_table = ina3221_ids,
753};
754module_i2c_driver(ina3221_i2c_driver);
755
756MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
757MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
758MODULE_LICENSE("GPL v2");