2 * INA3221 Triple Current/Voltage Monitor
4 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.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
114 * @single_shot: running in single-shot operating mode
116 struct ina3221_data {
117 struct device *pm_dev;
118 struct regmap *regmap;
119 struct regmap_field *fields[F_MAX_FIELDS];
120 struct ina3221_input inputs[INA3221_NUM_CHANNELS];
127 static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel)
129 return pm_runtime_active(ina->pm_dev) &&
130 (ina->reg_config & INA3221_CONFIG_CHx_EN(channel));
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,
138 static inline int ina3221_wait_for_data(struct ina3221_data *ina)
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];
147 /* Calculate total conversion time */
148 wait = channels * (vbus_ct + vsh_ct);
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);
155 static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
161 ret = regmap_read(ina->regmap, reg, ®val);
165 *val = sign_extend32(regval >> 3, 12);
170 static const u8 ina3221_in_reg[] = {
179 static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val)
181 const bool is_shunt = channel > INA3221_CHANNEL3;
182 struct ina3221_data *ina = dev_get_drvdata(dev);
183 u8 reg = ina3221_in_reg[channel];
186 /* Translate shunt channel index to sensor channel index */
187 channel %= INA3221_NUM_CHANNELS;
191 if (!ina3221_is_enabled(ina, channel))
194 /* Write CONFIG register to trigger a single-shot measurement */
195 if (ina->single_shot)
196 regmap_write(ina->regmap, INA3221_CONFIG,
199 ret = ina3221_wait_for_data(ina);
203 ret = ina3221_read_value(ina, reg, ®val);
208 * Scale of shunt voltage (uV): LSB is 40uV
209 * Scale of bus voltage (mV): LSB is 8mV
211 *val = regval * (is_shunt ? 40 : 8);
213 case hwmon_in_enable:
214 *val = ina3221_is_enabled(ina, channel);
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 },
229 static int ina3221_read_curr(struct device *dev, u32 attr,
230 int channel, long *val)
232 struct ina3221_data *ina = dev_get_drvdata(dev);
233 struct ina3221_input *input = &ina->inputs[channel];
234 int resistance_uo = input->shunt_resistor;
235 u8 reg = ina3221_curr_reg[attr][channel];
236 int regval, voltage_nv, ret;
239 case hwmon_curr_input:
240 if (!ina3221_is_enabled(ina, channel))
243 /* Write CONFIG register to trigger a single-shot measurement */
244 if (ina->single_shot)
245 regmap_write(ina->regmap, INA3221_CONFIG,
248 ret = ina3221_wait_for_data(ina);
253 case hwmon_curr_crit:
255 ret = ina3221_read_value(ina, reg, ®val);
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);
264 case hwmon_curr_crit_alarm:
265 case hwmon_curr_max_alarm:
266 /* No actual register read if channel is disabled */
267 if (!ina3221_is_enabled(ina, channel)) {
268 /* Return 0 for alert flags */
272 ret = regmap_field_read(ina->fields[reg], ®val);
282 static int ina3221_write_curr(struct device *dev, u32 attr,
283 int channel, long val)
285 struct ina3221_data *ina = dev_get_drvdata(dev);
286 struct ina3221_input *input = &ina->inputs[channel];
287 int resistance_uo = input->shunt_resistor;
288 u8 reg = ina3221_curr_reg[attr][channel];
289 int regval, current_ma, voltage_uv;
292 current_ma = clamp_val(val,
293 INT_MIN / resistance_uo,
294 INT_MAX / resistance_uo);
296 voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
299 voltage_uv = clamp_val(voltage_uv, -163800, 163800);
301 /* 1 / 40uV(scale) << 3(register shift) = 5 */
302 regval = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
304 return regmap_write(ina->regmap, reg, regval);
307 static int ina3221_write_enable(struct device *dev, int channel, bool enable)
309 struct ina3221_data *ina = dev_get_drvdata(dev);
310 u16 config, mask = INA3221_CONFIG_CHx_EN(channel);
311 u16 config_old = ina->reg_config & mask;
314 config = enable ? mask : 0;
316 /* Bypass if enable status is not being changed */
317 if (config_old == config)
320 /* For enabling routine, increase refcount and resume() at first */
322 ret = pm_runtime_get_sync(ina->pm_dev);
324 dev_err(dev, "Failed to get PM runtime\n");
329 /* Enable or disable the channel */
330 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config);
334 /* Cache the latest config register value */
335 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
339 /* For disabling routine, decrease refcount or suspend() at last */
341 pm_runtime_put_sync(ina->pm_dev);
347 dev_err(dev, "Failed to enable channel %d: error %d\n",
349 pm_runtime_put_sync(ina->pm_dev);
355 static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
356 u32 attr, int channel, long *val)
358 struct ina3221_data *ina = dev_get_drvdata(dev);
361 mutex_lock(&ina->lock);
365 /* 0-align channel ID */
366 ret = ina3221_read_in(dev, attr, channel - 1, val);
369 ret = ina3221_read_curr(dev, attr, channel, val);
376 mutex_unlock(&ina->lock);
381 static int ina3221_write(struct device *dev, enum hwmon_sensor_types type,
382 u32 attr, int channel, long val)
384 struct ina3221_data *ina = dev_get_drvdata(dev);
387 mutex_lock(&ina->lock);
391 /* 0-align channel ID */
392 ret = ina3221_write_enable(dev, channel - 1, val);
395 ret = ina3221_write_curr(dev, attr, channel, val);
402 mutex_unlock(&ina->lock);
407 static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type,
408 u32 attr, int channel, const char **str)
410 struct ina3221_data *ina = dev_get_drvdata(dev);
411 int index = channel - 1;
413 *str = ina->inputs[index].label;
418 static umode_t ina3221_is_visible(const void *drvdata,
419 enum hwmon_sensor_types type,
420 u32 attr, int channel)
422 const struct ina3221_data *ina = drvdata;
423 const struct ina3221_input *input = NULL;
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;
439 case hwmon_in_enable:
446 case hwmon_curr_input:
447 case hwmon_curr_crit_alarm:
448 case hwmon_curr_max_alarm:
450 case hwmon_curr_crit:
461 static const u32 ina3221_in_config[] = {
462 /* 0: dummy, skipped in is_visible */
464 /* 1-3: input voltage Channels */
465 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
466 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
467 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
468 /* 4-6: shunt voltage Channels */
475 static const struct hwmon_channel_info ina3221_in = {
477 .config = ina3221_in_config,
480 #define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \
481 HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \
482 HWMON_C_MAX | HWMON_C_MAX_ALARM)
484 static const u32 ina3221_curr_config[] = {
485 INA3221_HWMON_CURR_CONFIG,
486 INA3221_HWMON_CURR_CONFIG,
487 INA3221_HWMON_CURR_CONFIG,
491 static const struct hwmon_channel_info ina3221_curr = {
493 .config = ina3221_curr_config,
496 static const struct hwmon_channel_info *ina3221_info[] = {
502 static const struct hwmon_ops ina3221_hwmon_ops = {
503 .is_visible = ina3221_is_visible,
504 .read_string = ina3221_read_string,
505 .read = ina3221_read,
506 .write = ina3221_write,
509 static const struct hwmon_chip_info ina3221_chip_info = {
510 .ops = &ina3221_hwmon_ops,
511 .info = ina3221_info,
514 /* Extra attribute groups */
515 static ssize_t ina3221_shunt_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
519 struct ina3221_data *ina = dev_get_drvdata(dev);
520 unsigned int channel = sd_attr->index;
521 struct ina3221_input *input = &ina->inputs[channel];
523 return snprintf(buf, PAGE_SIZE, "%d\n", input->shunt_resistor);
526 static ssize_t ina3221_shunt_store(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf, size_t count)
530 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
531 struct ina3221_data *ina = dev_get_drvdata(dev);
532 unsigned int channel = sd_attr->index;
533 struct ina3221_input *input = &ina->inputs[channel];
537 ret = kstrtoint(buf, 0, &val);
541 val = clamp_val(val, 1, INT_MAX);
543 input->shunt_resistor = val;
548 /* shunt resistance */
549 static SENSOR_DEVICE_ATTR_RW(shunt1_resistor, ina3221_shunt, INA3221_CHANNEL1);
550 static SENSOR_DEVICE_ATTR_RW(shunt2_resistor, ina3221_shunt, INA3221_CHANNEL2);
551 static SENSOR_DEVICE_ATTR_RW(shunt3_resistor, ina3221_shunt, INA3221_CHANNEL3);
553 static struct attribute *ina3221_attrs[] = {
554 &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
555 &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
556 &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
559 ATTRIBUTE_GROUPS(ina3221);
561 static const struct regmap_range ina3221_yes_ranges[] = {
562 regmap_reg_range(INA3221_CONFIG, INA3221_BUS3),
563 regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
566 static const struct regmap_access_table ina3221_volatile_table = {
567 .yes_ranges = ina3221_yes_ranges,
568 .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
571 static const struct regmap_config ina3221_regmap_config = {
575 .cache_type = REGCACHE_RBTREE,
576 .volatile_table = &ina3221_volatile_table,
579 static int ina3221_probe_child_from_dt(struct device *dev,
580 struct device_node *child,
581 struct ina3221_data *ina)
583 struct ina3221_input *input;
587 ret = of_property_read_u32(child, "reg", &val);
589 dev_err(dev, "missing reg property of %pOFn\n", child);
591 } else if (val > INA3221_CHANNEL3) {
592 dev_err(dev, "invalid reg %d of %pOFn\n", val, child);
596 input = &ina->inputs[val];
598 /* Log the disconnected channel input */
599 if (!of_device_is_available(child)) {
600 input->disconnected = true;
604 /* Save the connected input label if available */
605 of_property_read_string(child, "label", &input->label);
607 /* Overwrite default shunt resistor value optionally */
608 if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
609 if (val < 1 || val > INT_MAX) {
610 dev_err(dev, "invalid shunt resistor value %u of %pOFn\n",
614 input->shunt_resistor = val;
620 static int ina3221_probe_from_dt(struct device *dev, struct ina3221_data *ina)
622 const struct device_node *np = dev->of_node;
623 struct device_node *child;
626 /* Compatible with non-DT platforms */
630 ina->single_shot = of_property_read_bool(np, "ti,single-shot");
632 for_each_child_of_node(np, child) {
633 ret = ina3221_probe_child_from_dt(dev, child, ina);
641 static int ina3221_probe(struct i2c_client *client,
642 const struct i2c_device_id *id)
644 struct device *dev = &client->dev;
645 struct ina3221_data *ina;
646 struct device *hwmon_dev;
649 ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
653 ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
654 if (IS_ERR(ina->regmap)) {
655 dev_err(dev, "Unable to allocate register map\n");
656 return PTR_ERR(ina->regmap);
659 for (i = 0; i < F_MAX_FIELDS; i++) {
660 ina->fields[i] = devm_regmap_field_alloc(dev,
662 ina3221_reg_fields[i]);
663 if (IS_ERR(ina->fields[i])) {
664 dev_err(dev, "Unable to allocate regmap fields\n");
665 return PTR_ERR(ina->fields[i]);
669 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
670 ina->inputs[i].shunt_resistor = INA3221_RSHUNT_DEFAULT;
672 ret = ina3221_probe_from_dt(dev, ina);
674 dev_err(dev, "Unable to probe from device tree\n");
678 /* The driver will be reset, so use reset value */
679 ina->reg_config = INA3221_CONFIG_DEFAULT;
681 /* Clear continuous bit to use single-shot mode */
682 if (ina->single_shot)
683 ina->reg_config &= ~INA3221_CONFIG_MODE_CONTINUOUS;
685 /* Disable channels if their inputs are disconnected */
686 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
687 if (ina->inputs[i].disconnected)
688 ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i);
692 mutex_init(&ina->lock);
693 dev_set_drvdata(dev, ina);
695 /* Enable PM runtime -- status is suspended by default */
696 pm_runtime_enable(ina->pm_dev);
698 /* Initialize (resume) the device */
699 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
700 if (ina->inputs[i].disconnected)
702 /* Match the refcount with number of enabled channels */
703 ret = pm_runtime_get_sync(ina->pm_dev);
708 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina,
711 if (IS_ERR(hwmon_dev)) {
712 dev_err(dev, "Unable to register hwmon device\n");
713 ret = PTR_ERR(hwmon_dev);
720 pm_runtime_disable(ina->pm_dev);
721 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);
725 mutex_destroy(&ina->lock);
730 static int ina3221_remove(struct i2c_client *client)
732 struct ina3221_data *ina = dev_get_drvdata(&client->dev);
735 pm_runtime_disable(ina->pm_dev);
736 pm_runtime_set_suspended(ina->pm_dev);
738 /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */
739 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
740 pm_runtime_put_noidle(ina->pm_dev);
742 mutex_destroy(&ina->lock);
747 static int __maybe_unused ina3221_suspend(struct device *dev)
749 struct ina3221_data *ina = dev_get_drvdata(dev);
752 /* Save config register value and enable cache-only */
753 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
757 /* Set to power-down mode for power saving */
758 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG,
759 INA3221_CONFIG_MODE_MASK,
760 INA3221_CONFIG_MODE_POWERDOWN);
764 regcache_cache_only(ina->regmap, true);
765 regcache_mark_dirty(ina->regmap);
770 static int __maybe_unused ina3221_resume(struct device *dev)
772 struct ina3221_data *ina = dev_get_drvdata(dev);
775 regcache_cache_only(ina->regmap, false);
777 /* Software reset the chip */
778 ret = regmap_field_write(ina->fields[F_RST], true);
780 dev_err(dev, "Unable to reset device\n");
784 /* Restore cached register values to hardware */
785 ret = regcache_sync(ina->regmap);
789 /* Restore config register value to hardware */
790 ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
797 static const struct dev_pm_ops ina3221_pm = {
798 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
799 pm_runtime_force_resume)
800 SET_RUNTIME_PM_OPS(ina3221_suspend, ina3221_resume, NULL)
803 static const struct of_device_id ina3221_of_match_table[] = {
804 { .compatible = "ti,ina3221", },
807 MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
809 static const struct i2c_device_id ina3221_ids[] = {
813 MODULE_DEVICE_TABLE(i2c, ina3221_ids);
815 static struct i2c_driver ina3221_i2c_driver = {
816 .probe = ina3221_probe,
817 .remove = ina3221_remove,
819 .name = INA3221_DRIVER_NAME,
820 .of_match_table = ina3221_of_match_table,
823 .id_table = ina3221_ids,
825 module_i2c_driver(ina3221_i2c_driver);
828 MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
829 MODULE_LICENSE("GPL v2");