2 * INA3221 Triple Current/Voltage Monitor
4 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Andrew F. Davis <afd@ti.com>
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.
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.
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
26 #define INA3221_DRIVER_NAME "ina3221"
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
43 #define INA3221_CONFIG_MODE_MASK GENMASK(2, 0)
44 #define INA3221_CONFIG_MODE_POWERDOWN 0
45 #define INA3221_CONFIG_MODE_SHUNT BIT(0)
46 #define INA3221_CONFIG_MODE_BUS BIT(1)
47 #define INA3221_CONFIG_MODE_CONTINUOUS BIT(2)
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)
55 #define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x))
57 #define INA3221_CONFIG_DEFAULT 0x7127
58 #define INA3221_RSHUNT_DEFAULT 10000
75 static const struct reg_field ina3221_reg_fields[] = {
76 [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15),
78 [F_CVRF] = REG_FIELD(INA3221_MASK_ENABLE, 0, 0),
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),
87 enum ina3221_channels {
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
100 struct ina3221_input {
107 * struct ina3221_data - device specific information
108 * @pm_dev: Device pointer for pm runtime
109 * @regmap: Register map of the device
110 * @fields: Register fields of the device
111 * @inputs: Array of channel input source specific structures
112 * @lock: mutex lock to serialize sysfs attribute accesses
113 * @reg_config: Register value of INA3221_CONFIG
115 struct ina3221_data {
116 struct device *pm_dev;
117 struct regmap *regmap;
118 struct regmap_field *fields[F_MAX_FIELDS];
119 struct ina3221_input inputs[INA3221_NUM_CHANNELS];
124 static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel)
126 return pm_runtime_active(ina->pm_dev) &&
127 (ina->reg_config & INA3221_CONFIG_CHx_EN(channel));
130 /* Lookup table for Bus and Shunt conversion times in usec */
131 static const u16 ina3221_conv_time[] = {
132 140, 204, 332, 588, 1100, 2116, 4156, 8244,
135 static inline int ina3221_wait_for_data(struct ina3221_data *ina)
137 u32 channels = hweight16(ina->reg_config & INA3221_CONFIG_CHs_EN_MASK);
138 u32 vbus_ct_idx = INA3221_CONFIG_VBUS_CT(ina->reg_config);
139 u32 vsh_ct_idx = INA3221_CONFIG_VSH_CT(ina->reg_config);
140 u32 vbus_ct = ina3221_conv_time[vbus_ct_idx];
141 u32 vsh_ct = ina3221_conv_time[vsh_ct_idx];
144 /* Calculate total conversion time */
145 wait = channels * (vbus_ct + vsh_ct);
147 /* Polling the CVRF bit to make sure read data is ready */
148 return regmap_field_read_poll_timeout(ina->fields[F_CVRF],
149 cvrf, cvrf, wait, 100000);
152 static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
158 ret = regmap_read(ina->regmap, reg, ®val);
162 *val = sign_extend32(regval >> 3, 12);
167 static const u8 ina3221_in_reg[] = {
176 static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val)
178 const bool is_shunt = channel > INA3221_CHANNEL3;
179 struct ina3221_data *ina = dev_get_drvdata(dev);
180 u8 reg = ina3221_in_reg[channel];
183 /* Translate shunt channel index to sensor channel index */
184 channel %= INA3221_NUM_CHANNELS;
188 if (!ina3221_is_enabled(ina, channel))
191 ret = ina3221_wait_for_data(ina);
195 ret = ina3221_read_value(ina, reg, ®val);
200 * Scale of shunt voltage (uV): LSB is 40uV
201 * Scale of bus voltage (mV): LSB is 8mV
203 *val = regval * (is_shunt ? 40 : 8);
205 case hwmon_in_enable:
206 *val = ina3221_is_enabled(ina, channel);
213 static const u8 ina3221_curr_reg[][INA3221_NUM_CHANNELS] = {
214 [hwmon_curr_input] = { INA3221_SHUNT1, INA3221_SHUNT2, INA3221_SHUNT3 },
215 [hwmon_curr_max] = { INA3221_WARN1, INA3221_WARN2, INA3221_WARN3 },
216 [hwmon_curr_crit] = { INA3221_CRIT1, INA3221_CRIT2, INA3221_CRIT3 },
217 [hwmon_curr_max_alarm] = { F_WF1, F_WF2, F_WF3 },
218 [hwmon_curr_crit_alarm] = { F_CF1, F_CF2, F_CF3 },
221 static int ina3221_read_curr(struct device *dev, u32 attr,
222 int channel, long *val)
224 struct ina3221_data *ina = dev_get_drvdata(dev);
225 struct ina3221_input *input = &ina->inputs[channel];
226 int resistance_uo = input->shunt_resistor;
227 u8 reg = ina3221_curr_reg[attr][channel];
228 int regval, voltage_nv, ret;
231 case hwmon_curr_input:
232 if (!ina3221_is_enabled(ina, channel))
235 ret = ina3221_wait_for_data(ina);
240 case hwmon_curr_crit:
242 ret = ina3221_read_value(ina, reg, ®val);
246 /* Scale of shunt voltage: LSB is 40uV (40000nV) */
247 voltage_nv = regval * 40000;
248 /* Return current in mA */
249 *val = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo);
251 case hwmon_curr_crit_alarm:
252 case hwmon_curr_max_alarm:
253 /* No actual register read if channel is disabled */
254 if (!ina3221_is_enabled(ina, channel)) {
255 /* Return 0 for alert flags */
259 ret = regmap_field_read(ina->fields[reg], ®val);
269 static int ina3221_write_curr(struct device *dev, u32 attr,
270 int channel, long val)
272 struct ina3221_data *ina = dev_get_drvdata(dev);
273 struct ina3221_input *input = &ina->inputs[channel];
274 int resistance_uo = input->shunt_resistor;
275 u8 reg = ina3221_curr_reg[attr][channel];
276 int regval, current_ma, voltage_uv;
279 current_ma = clamp_val(val,
280 INT_MIN / resistance_uo,
281 INT_MAX / resistance_uo);
283 voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
286 voltage_uv = clamp_val(voltage_uv, -163800, 163800);
288 /* 1 / 40uV(scale) << 3(register shift) = 5 */
289 regval = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
291 return regmap_write(ina->regmap, reg, regval);
294 static int ina3221_write_enable(struct device *dev, int channel, bool enable)
296 struct ina3221_data *ina = dev_get_drvdata(dev);
297 u16 config, mask = INA3221_CONFIG_CHx_EN(channel);
298 u16 config_old = ina->reg_config & mask;
301 config = enable ? mask : 0;
303 /* Bypass if enable status is not being changed */
304 if (config_old == config)
307 /* For enabling routine, increase refcount and resume() at first */
309 ret = pm_runtime_get_sync(ina->pm_dev);
311 dev_err(dev, "Failed to get PM runtime\n");
316 /* Enable or disable the channel */
317 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config);
321 /* Cache the latest config register value */
322 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
326 /* For disabling routine, decrease refcount or suspend() at last */
328 pm_runtime_put_sync(ina->pm_dev);
334 dev_err(dev, "Failed to enable channel %d: error %d\n",
336 pm_runtime_put_sync(ina->pm_dev);
342 static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
343 u32 attr, int channel, long *val)
345 struct ina3221_data *ina = dev_get_drvdata(dev);
348 mutex_lock(&ina->lock);
352 /* 0-align channel ID */
353 ret = ina3221_read_in(dev, attr, channel - 1, val);
356 ret = ina3221_read_curr(dev, attr, channel, val);
363 mutex_unlock(&ina->lock);
368 static int ina3221_write(struct device *dev, enum hwmon_sensor_types type,
369 u32 attr, int channel, long val)
371 struct ina3221_data *ina = dev_get_drvdata(dev);
374 mutex_lock(&ina->lock);
378 /* 0-align channel ID */
379 ret = ina3221_write_enable(dev, channel - 1, val);
382 ret = ina3221_write_curr(dev, attr, channel, val);
389 mutex_unlock(&ina->lock);
394 static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type,
395 u32 attr, int channel, const char **str)
397 struct ina3221_data *ina = dev_get_drvdata(dev);
398 int index = channel - 1;
400 *str = ina->inputs[index].label;
405 static umode_t ina3221_is_visible(const void *drvdata,
406 enum hwmon_sensor_types type,
407 u32 attr, int channel)
409 const struct ina3221_data *ina = drvdata;
410 const struct ina3221_input *input = NULL;
420 if (channel - 1 <= INA3221_CHANNEL3)
421 input = &ina->inputs[channel - 1];
422 /* Hide label node if label is not provided */
423 return (input && input->label) ? 0444 : 0;
426 case hwmon_in_enable:
433 case hwmon_curr_input:
434 case hwmon_curr_crit_alarm:
435 case hwmon_curr_max_alarm:
437 case hwmon_curr_crit:
448 static const u32 ina3221_in_config[] = {
449 /* 0: dummy, skipped in is_visible */
451 /* 1-3: input voltage Channels */
452 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
453 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
454 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
455 /* 4-6: shunt voltage Channels */
462 static const struct hwmon_channel_info ina3221_in = {
464 .config = ina3221_in_config,
467 #define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \
468 HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \
469 HWMON_C_MAX | HWMON_C_MAX_ALARM)
471 static const u32 ina3221_curr_config[] = {
472 INA3221_HWMON_CURR_CONFIG,
473 INA3221_HWMON_CURR_CONFIG,
474 INA3221_HWMON_CURR_CONFIG,
478 static const struct hwmon_channel_info ina3221_curr = {
480 .config = ina3221_curr_config,
483 static const struct hwmon_channel_info *ina3221_info[] = {
489 static const struct hwmon_ops ina3221_hwmon_ops = {
490 .is_visible = ina3221_is_visible,
491 .read_string = ina3221_read_string,
492 .read = ina3221_read,
493 .write = ina3221_write,
496 static const struct hwmon_chip_info ina3221_chip_info = {
497 .ops = &ina3221_hwmon_ops,
498 .info = ina3221_info,
501 /* Extra attribute groups */
502 static ssize_t ina3221_show_shunt(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
506 struct ina3221_data *ina = dev_get_drvdata(dev);
507 unsigned int channel = sd_attr->index;
508 struct ina3221_input *input = &ina->inputs[channel];
510 return snprintf(buf, PAGE_SIZE, "%d\n", input->shunt_resistor);
513 static ssize_t ina3221_set_shunt(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
517 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
518 struct ina3221_data *ina = dev_get_drvdata(dev);
519 unsigned int channel = sd_attr->index;
520 struct ina3221_input *input = &ina->inputs[channel];
524 ret = kstrtoint(buf, 0, &val);
528 val = clamp_val(val, 1, INT_MAX);
530 input->shunt_resistor = val;
535 /* shunt resistance */
536 static SENSOR_DEVICE_ATTR(shunt1_resistor, S_IRUGO | S_IWUSR,
537 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL1);
538 static SENSOR_DEVICE_ATTR(shunt2_resistor, S_IRUGO | S_IWUSR,
539 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL2);
540 static SENSOR_DEVICE_ATTR(shunt3_resistor, S_IRUGO | S_IWUSR,
541 ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL3);
543 static struct attribute *ina3221_attrs[] = {
544 &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
545 &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
546 &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
549 ATTRIBUTE_GROUPS(ina3221);
551 static const struct regmap_range ina3221_yes_ranges[] = {
552 regmap_reg_range(INA3221_CONFIG, INA3221_BUS3),
553 regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
556 static const struct regmap_access_table ina3221_volatile_table = {
557 .yes_ranges = ina3221_yes_ranges,
558 .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
561 static const struct regmap_config ina3221_regmap_config = {
565 .cache_type = REGCACHE_RBTREE,
566 .volatile_table = &ina3221_volatile_table,
569 static int ina3221_probe_child_from_dt(struct device *dev,
570 struct device_node *child,
571 struct ina3221_data *ina)
573 struct ina3221_input *input;
577 ret = of_property_read_u32(child, "reg", &val);
579 dev_err(dev, "missing reg property of %pOFn\n", child);
581 } else if (val > INA3221_CHANNEL3) {
582 dev_err(dev, "invalid reg %d of %pOFn\n", val, child);
586 input = &ina->inputs[val];
588 /* Log the disconnected channel input */
589 if (!of_device_is_available(child)) {
590 input->disconnected = true;
594 /* Save the connected input label if available */
595 of_property_read_string(child, "label", &input->label);
597 /* Overwrite default shunt resistor value optionally */
598 if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
599 if (val < 1 || val > INT_MAX) {
600 dev_err(dev, "invalid shunt resistor value %u of %pOFn\n",
604 input->shunt_resistor = val;
610 static int ina3221_probe_from_dt(struct device *dev, struct ina3221_data *ina)
612 const struct device_node *np = dev->of_node;
613 struct device_node *child;
616 /* Compatible with non-DT platforms */
620 for_each_child_of_node(np, child) {
621 ret = ina3221_probe_child_from_dt(dev, child, ina);
629 static int ina3221_probe(struct i2c_client *client,
630 const struct i2c_device_id *id)
632 struct device *dev = &client->dev;
633 struct ina3221_data *ina;
634 struct device *hwmon_dev;
637 ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
641 ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
642 if (IS_ERR(ina->regmap)) {
643 dev_err(dev, "Unable to allocate register map\n");
644 return PTR_ERR(ina->regmap);
647 for (i = 0; i < F_MAX_FIELDS; i++) {
648 ina->fields[i] = devm_regmap_field_alloc(dev,
650 ina3221_reg_fields[i]);
651 if (IS_ERR(ina->fields[i])) {
652 dev_err(dev, "Unable to allocate regmap fields\n");
653 return PTR_ERR(ina->fields[i]);
657 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
658 ina->inputs[i].shunt_resistor = INA3221_RSHUNT_DEFAULT;
660 ret = ina3221_probe_from_dt(dev, ina);
662 dev_err(dev, "Unable to probe from device tree\n");
666 /* The driver will be reset, so use reset value */
667 ina->reg_config = INA3221_CONFIG_DEFAULT;
669 /* Disable channels if their inputs are disconnected */
670 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
671 if (ina->inputs[i].disconnected)
672 ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i);
676 mutex_init(&ina->lock);
677 dev_set_drvdata(dev, ina);
679 /* Enable PM runtime -- status is suspended by default */
680 pm_runtime_enable(ina->pm_dev);
682 /* Initialize (resume) the device */
683 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
684 if (ina->inputs[i].disconnected)
686 /* Match the refcount with number of enabled channels */
687 ret = pm_runtime_get_sync(ina->pm_dev);
692 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina,
695 if (IS_ERR(hwmon_dev)) {
696 dev_err(dev, "Unable to register hwmon device\n");
697 ret = PTR_ERR(hwmon_dev);
704 pm_runtime_disable(ina->pm_dev);
705 pm_runtime_set_suspended(ina->pm_dev);
706 /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */
707 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
708 pm_runtime_put_noidle(ina->pm_dev);
709 mutex_destroy(&ina->lock);
714 static int ina3221_remove(struct i2c_client *client)
716 struct ina3221_data *ina = dev_get_drvdata(&client->dev);
719 pm_runtime_disable(ina->pm_dev);
720 pm_runtime_set_suspended(ina->pm_dev);
722 /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */
723 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
724 pm_runtime_put_noidle(ina->pm_dev);
726 mutex_destroy(&ina->lock);
731 static int __maybe_unused ina3221_suspend(struct device *dev)
733 struct ina3221_data *ina = dev_get_drvdata(dev);
736 /* Save config register value and enable cache-only */
737 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
741 /* Set to power-down mode for power saving */
742 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG,
743 INA3221_CONFIG_MODE_MASK,
744 INA3221_CONFIG_MODE_POWERDOWN);
748 regcache_cache_only(ina->regmap, true);
749 regcache_mark_dirty(ina->regmap);
754 static int __maybe_unused ina3221_resume(struct device *dev)
756 struct ina3221_data *ina = dev_get_drvdata(dev);
759 regcache_cache_only(ina->regmap, false);
761 /* Software reset the chip */
762 ret = regmap_field_write(ina->fields[F_RST], true);
764 dev_err(dev, "Unable to reset device\n");
768 /* Restore cached register values to hardware */
769 ret = regcache_sync(ina->regmap);
773 /* Restore config register value to hardware */
774 ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
781 static const struct dev_pm_ops ina3221_pm = {
782 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
783 pm_runtime_force_resume)
784 SET_RUNTIME_PM_OPS(ina3221_suspend, ina3221_resume, NULL)
787 static const struct of_device_id ina3221_of_match_table[] = {
788 { .compatible = "ti,ina3221", },
791 MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
793 static const struct i2c_device_id ina3221_ids[] = {
797 MODULE_DEVICE_TABLE(i2c, ina3221_ids);
799 static struct i2c_driver ina3221_i2c_driver = {
800 .probe = ina3221_probe,
801 .remove = ina3221_remove,
803 .name = INA3221_DRIVER_NAME,
804 .of_match_table = ina3221_of_match_table,
807 .id_table = ina3221_ids,
809 module_i2c_driver(ina3221_i2c_driver);
811 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
812 MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
813 MODULE_LICENSE("GPL v2");