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>
25 #include <linux/util_macros.h>
27 #define INA3221_DRIVER_NAME "ina3221"
29 #define INA3221_CONFIG 0x00
30 #define INA3221_SHUNT1 0x01
31 #define INA3221_BUS1 0x02
32 #define INA3221_SHUNT2 0x03
33 #define INA3221_BUS2 0x04
34 #define INA3221_SHUNT3 0x05
35 #define INA3221_BUS3 0x06
36 #define INA3221_CRIT1 0x07
37 #define INA3221_WARN1 0x08
38 #define INA3221_CRIT2 0x09
39 #define INA3221_WARN2 0x0a
40 #define INA3221_CRIT3 0x0b
41 #define INA3221_WARN3 0x0c
42 #define INA3221_MASK_ENABLE 0x0f
44 #define INA3221_CONFIG_MODE_MASK GENMASK(2, 0)
45 #define INA3221_CONFIG_MODE_POWERDOWN 0
46 #define INA3221_CONFIG_MODE_SHUNT BIT(0)
47 #define INA3221_CONFIG_MODE_BUS BIT(1)
48 #define INA3221_CONFIG_MODE_CONTINUOUS BIT(2)
49 #define INA3221_CONFIG_VSH_CT_SHIFT 3
50 #define INA3221_CONFIG_VSH_CT_MASK GENMASK(5, 3)
51 #define INA3221_CONFIG_VSH_CT(x) (((x) & GENMASK(5, 3)) >> 3)
52 #define INA3221_CONFIG_VBUS_CT_SHIFT 6
53 #define INA3221_CONFIG_VBUS_CT_MASK GENMASK(8, 6)
54 #define INA3221_CONFIG_VBUS_CT(x) (((x) & GENMASK(8, 6)) >> 6)
55 #define INA3221_CONFIG_AVG_SHIFT 9
56 #define INA3221_CONFIG_AVG_MASK GENMASK(11, 9)
57 #define INA3221_CONFIG_AVG(x) (((x) & GENMASK(11, 9)) >> 9)
58 #define INA3221_CONFIG_CHs_EN_MASK GENMASK(14, 12)
59 #define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x))
61 #define INA3221_CONFIG_DEFAULT 0x7127
62 #define INA3221_RSHUNT_DEFAULT 10000
79 static const struct reg_field ina3221_reg_fields[] = {
80 [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15),
82 [F_CVRF] = REG_FIELD(INA3221_MASK_ENABLE, 0, 0),
83 [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3),
84 [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4),
85 [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5),
86 [F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7),
87 [F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8),
88 [F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9),
91 enum ina3221_channels {
99 * struct ina3221_input - channel input source specific information
100 * @label: label of channel input source
101 * @shunt_resistor: shunt resistor value of channel input source
102 * @disconnected: connection status of channel input source
104 struct ina3221_input {
111 * struct ina3221_data - device specific information
112 * @pm_dev: Device pointer for pm runtime
113 * @regmap: Register map of the device
114 * @fields: Register fields of the device
115 * @inputs: Array of channel input source specific structures
116 * @lock: mutex lock to serialize sysfs attribute accesses
117 * @reg_config: Register value of INA3221_CONFIG
118 * @single_shot: running in single-shot operating mode
120 struct ina3221_data {
121 struct device *pm_dev;
122 struct regmap *regmap;
123 struct regmap_field *fields[F_MAX_FIELDS];
124 struct ina3221_input inputs[INA3221_NUM_CHANNELS];
131 static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel)
133 return pm_runtime_active(ina->pm_dev) &&
134 (ina->reg_config & INA3221_CONFIG_CHx_EN(channel));
137 /* Lookup table for Bus and Shunt conversion times in usec */
138 static const u16 ina3221_conv_time[] = {
139 140, 204, 332, 588, 1100, 2116, 4156, 8244,
142 /* Lookup table for number of samples using in averaging mode */
143 static const int ina3221_avg_samples[] = {
144 1, 4, 16, 64, 128, 256, 512, 1024,
147 /* Converting update_interval in msec to conversion time in usec */
148 static inline u32 ina3221_interval_ms_to_conv_time(u16 config, int interval)
150 u32 channels = hweight16(config & INA3221_CONFIG_CHs_EN_MASK);
151 u32 samples_idx = INA3221_CONFIG_AVG(config);
152 u32 samples = ina3221_avg_samples[samples_idx];
154 /* Bisect the result to Bus and Shunt conversion times */
155 return DIV_ROUND_CLOSEST(interval * 1000 / 2, channels * samples);
158 /* Converting CONFIG register value to update_interval in usec */
159 static inline u32 ina3221_reg_to_interval_us(u16 config)
161 u32 channels = hweight16(config & INA3221_CONFIG_CHs_EN_MASK);
162 u32 vbus_ct_idx = INA3221_CONFIG_VBUS_CT(config);
163 u32 vsh_ct_idx = INA3221_CONFIG_VSH_CT(config);
164 u32 samples_idx = INA3221_CONFIG_AVG(config);
165 u32 samples = ina3221_avg_samples[samples_idx];
166 u32 vbus_ct = ina3221_conv_time[vbus_ct_idx];
167 u32 vsh_ct = ina3221_conv_time[vsh_ct_idx];
169 /* Calculate total conversion time */
170 return channels * (vbus_ct + vsh_ct) * samples;
173 static inline int ina3221_wait_for_data(struct ina3221_data *ina)
177 wait = ina3221_reg_to_interval_us(ina->reg_config);
179 /* Polling the CVRF bit to make sure read data is ready */
180 return regmap_field_read_poll_timeout(ina->fields[F_CVRF],
181 cvrf, cvrf, wait, 100000);
184 static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
190 ret = regmap_read(ina->regmap, reg, ®val);
194 *val = sign_extend32(regval >> 3, 12);
199 static const u8 ina3221_in_reg[] = {
208 static int ina3221_read_chip(struct device *dev, u32 attr, long *val)
210 struct ina3221_data *ina = dev_get_drvdata(dev);
214 case hwmon_chip_samples:
215 regval = INA3221_CONFIG_AVG(ina->reg_config);
216 *val = ina3221_avg_samples[regval];
218 case hwmon_chip_update_interval:
220 *val = ina3221_reg_to_interval_us(ina->reg_config);
221 *val = DIV_ROUND_CLOSEST(*val, 1000);
228 static int ina3221_read_in(struct device *dev, u32 attr, int channel, long *val)
230 const bool is_shunt = channel > INA3221_CHANNEL3;
231 struct ina3221_data *ina = dev_get_drvdata(dev);
232 u8 reg = ina3221_in_reg[channel];
235 /* Translate shunt channel index to sensor channel index */
236 channel %= INA3221_NUM_CHANNELS;
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);
252 ret = ina3221_read_value(ina, reg, ®val);
257 * Scale of shunt voltage (uV): LSB is 40uV
258 * Scale of bus voltage (mV): LSB is 8mV
260 *val = regval * (is_shunt ? 40 : 8);
262 case hwmon_in_enable:
263 *val = ina3221_is_enabled(ina, channel);
270 static const u8 ina3221_curr_reg[][INA3221_NUM_CHANNELS] = {
271 [hwmon_curr_input] = { INA3221_SHUNT1, INA3221_SHUNT2, INA3221_SHUNT3 },
272 [hwmon_curr_max] = { INA3221_WARN1, INA3221_WARN2, INA3221_WARN3 },
273 [hwmon_curr_crit] = { INA3221_CRIT1, INA3221_CRIT2, INA3221_CRIT3 },
274 [hwmon_curr_max_alarm] = { F_WF1, F_WF2, F_WF3 },
275 [hwmon_curr_crit_alarm] = { F_CF1, F_CF2, F_CF3 },
278 static int ina3221_read_curr(struct device *dev, u32 attr,
279 int channel, long *val)
281 struct ina3221_data *ina = dev_get_drvdata(dev);
282 struct ina3221_input *input = &ina->inputs[channel];
283 int resistance_uo = input->shunt_resistor;
284 u8 reg = ina3221_curr_reg[attr][channel];
285 int regval, voltage_nv, ret;
288 case hwmon_curr_input:
289 if (!ina3221_is_enabled(ina, channel))
292 /* Write CONFIG register to trigger a single-shot measurement */
293 if (ina->single_shot)
294 regmap_write(ina->regmap, INA3221_CONFIG,
297 ret = ina3221_wait_for_data(ina);
302 case hwmon_curr_crit:
304 ret = ina3221_read_value(ina, reg, ®val);
308 /* Scale of shunt voltage: LSB is 40uV (40000nV) */
309 voltage_nv = regval * 40000;
310 /* Return current in mA */
311 *val = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo);
313 case hwmon_curr_crit_alarm:
314 case hwmon_curr_max_alarm:
315 /* No actual register read if channel is disabled */
316 if (!ina3221_is_enabled(ina, channel)) {
317 /* Return 0 for alert flags */
321 ret = regmap_field_read(ina->fields[reg], ®val);
331 static int ina3221_write_chip(struct device *dev, u32 attr, long val)
333 struct ina3221_data *ina = dev_get_drvdata(dev);
338 case hwmon_chip_samples:
339 idx = find_closest(val, ina3221_avg_samples,
340 ARRAY_SIZE(ina3221_avg_samples));
342 tmp = (ina->reg_config & ~INA3221_CONFIG_AVG_MASK) |
343 (idx << INA3221_CONFIG_AVG_SHIFT);
344 ret = regmap_write(ina->regmap, INA3221_CONFIG, tmp);
348 /* Update reg_config accordingly */
349 ina->reg_config = tmp;
351 case hwmon_chip_update_interval:
352 tmp = ina3221_interval_ms_to_conv_time(ina->reg_config, val);
353 idx = find_closest(tmp, ina3221_conv_time,
354 ARRAY_SIZE(ina3221_conv_time));
356 /* Update Bus and Shunt voltage conversion times */
357 tmp = INA3221_CONFIG_VBUS_CT_MASK | INA3221_CONFIG_VSH_CT_MASK;
358 tmp = (ina->reg_config & ~tmp) |
359 (idx << INA3221_CONFIG_VBUS_CT_SHIFT) |
360 (idx << INA3221_CONFIG_VSH_CT_SHIFT);
361 ret = regmap_write(ina->regmap, INA3221_CONFIG, tmp);
365 /* Update reg_config accordingly */
366 ina->reg_config = tmp;
373 static int ina3221_write_curr(struct device *dev, u32 attr,
374 int channel, long val)
376 struct ina3221_data *ina = dev_get_drvdata(dev);
377 struct ina3221_input *input = &ina->inputs[channel];
378 int resistance_uo = input->shunt_resistor;
379 u8 reg = ina3221_curr_reg[attr][channel];
380 int regval, current_ma, voltage_uv;
383 current_ma = clamp_val(val,
384 INT_MIN / resistance_uo,
385 INT_MAX / resistance_uo);
387 voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
390 voltage_uv = clamp_val(voltage_uv, -163800, 163800);
392 /* 1 / 40uV(scale) << 3(register shift) = 5 */
393 regval = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
395 return regmap_write(ina->regmap, reg, regval);
398 static int ina3221_write_enable(struct device *dev, int channel, bool enable)
400 struct ina3221_data *ina = dev_get_drvdata(dev);
401 u16 config, mask = INA3221_CONFIG_CHx_EN(channel);
402 u16 config_old = ina->reg_config & mask;
406 config = enable ? mask : 0;
408 /* Bypass if enable status is not being changed */
409 if (config_old == config)
412 /* For enabling routine, increase refcount and resume() at first */
414 ret = pm_runtime_get_sync(ina->pm_dev);
416 dev_err(dev, "Failed to get PM runtime\n");
421 /* Enable or disable the channel */
422 tmp = (ina->reg_config & ~mask) | (config & mask);
423 ret = regmap_write(ina->regmap, INA3221_CONFIG, tmp);
427 /* Cache the latest config register value */
428 ina->reg_config = tmp;
430 /* For disabling routine, decrease refcount or suspend() at last */
432 pm_runtime_put_sync(ina->pm_dev);
438 dev_err(dev, "Failed to enable channel %d: error %d\n",
440 pm_runtime_put_sync(ina->pm_dev);
446 static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
447 u32 attr, int channel, long *val)
449 struct ina3221_data *ina = dev_get_drvdata(dev);
452 mutex_lock(&ina->lock);
456 ret = ina3221_read_chip(dev, attr, val);
459 /* 0-align channel ID */
460 ret = ina3221_read_in(dev, attr, channel - 1, val);
463 ret = ina3221_read_curr(dev, attr, channel, val);
470 mutex_unlock(&ina->lock);
475 static int ina3221_write(struct device *dev, enum hwmon_sensor_types type,
476 u32 attr, int channel, long val)
478 struct ina3221_data *ina = dev_get_drvdata(dev);
481 mutex_lock(&ina->lock);
485 ret = ina3221_write_chip(dev, attr, val);
488 /* 0-align channel ID */
489 ret = ina3221_write_enable(dev, channel - 1, val);
492 ret = ina3221_write_curr(dev, attr, channel, val);
499 mutex_unlock(&ina->lock);
504 static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type,
505 u32 attr, int channel, const char **str)
507 struct ina3221_data *ina = dev_get_drvdata(dev);
508 int index = channel - 1;
510 *str = ina->inputs[index].label;
515 static umode_t ina3221_is_visible(const void *drvdata,
516 enum hwmon_sensor_types type,
517 u32 attr, int channel)
519 const struct ina3221_data *ina = drvdata;
520 const struct ina3221_input *input = NULL;
525 case hwmon_chip_samples:
526 case hwmon_chip_update_interval:
538 if (channel - 1 <= INA3221_CHANNEL3)
539 input = &ina->inputs[channel - 1];
540 /* Hide label node if label is not provided */
541 return (input && input->label) ? 0444 : 0;
544 case hwmon_in_enable:
551 case hwmon_curr_input:
552 case hwmon_curr_crit_alarm:
553 case hwmon_curr_max_alarm:
555 case hwmon_curr_crit:
566 #define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \
567 HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \
568 HWMON_C_MAX | HWMON_C_MAX_ALARM)
570 static const struct hwmon_channel_info *ina3221_info[] = {
571 HWMON_CHANNEL_INFO(chip,
573 HWMON_C_UPDATE_INTERVAL),
574 HWMON_CHANNEL_INFO(in,
575 /* 0: dummy, skipped in is_visible */
577 /* 1-3: input voltage Channels */
578 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
579 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
580 HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
581 /* 4-6: shunt voltage Channels */
585 HWMON_CHANNEL_INFO(curr,
586 INA3221_HWMON_CURR_CONFIG,
587 INA3221_HWMON_CURR_CONFIG,
588 INA3221_HWMON_CURR_CONFIG),
592 static const struct hwmon_ops ina3221_hwmon_ops = {
593 .is_visible = ina3221_is_visible,
594 .read_string = ina3221_read_string,
595 .read = ina3221_read,
596 .write = ina3221_write,
599 static const struct hwmon_chip_info ina3221_chip_info = {
600 .ops = &ina3221_hwmon_ops,
601 .info = ina3221_info,
604 /* Extra attribute groups */
605 static ssize_t ina3221_shunt_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
608 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
609 struct ina3221_data *ina = dev_get_drvdata(dev);
610 unsigned int channel = sd_attr->index;
611 struct ina3221_input *input = &ina->inputs[channel];
613 return snprintf(buf, PAGE_SIZE, "%d\n", input->shunt_resistor);
616 static ssize_t ina3221_shunt_store(struct device *dev,
617 struct device_attribute *attr,
618 const char *buf, size_t count)
620 struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
621 struct ina3221_data *ina = dev_get_drvdata(dev);
622 unsigned int channel = sd_attr->index;
623 struct ina3221_input *input = &ina->inputs[channel];
627 ret = kstrtoint(buf, 0, &val);
631 val = clamp_val(val, 1, INT_MAX);
633 input->shunt_resistor = val;
638 /* shunt resistance */
639 static SENSOR_DEVICE_ATTR_RW(shunt1_resistor, ina3221_shunt, INA3221_CHANNEL1);
640 static SENSOR_DEVICE_ATTR_RW(shunt2_resistor, ina3221_shunt, INA3221_CHANNEL2);
641 static SENSOR_DEVICE_ATTR_RW(shunt3_resistor, ina3221_shunt, INA3221_CHANNEL3);
643 static struct attribute *ina3221_attrs[] = {
644 &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
645 &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
646 &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
649 ATTRIBUTE_GROUPS(ina3221);
651 static const struct regmap_range ina3221_yes_ranges[] = {
652 regmap_reg_range(INA3221_CONFIG, INA3221_BUS3),
653 regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
656 static const struct regmap_access_table ina3221_volatile_table = {
657 .yes_ranges = ina3221_yes_ranges,
658 .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
661 static const struct regmap_config ina3221_regmap_config = {
665 .cache_type = REGCACHE_RBTREE,
666 .volatile_table = &ina3221_volatile_table,
669 static int ina3221_probe_child_from_dt(struct device *dev,
670 struct device_node *child,
671 struct ina3221_data *ina)
673 struct ina3221_input *input;
677 ret = of_property_read_u32(child, "reg", &val);
679 dev_err(dev, "missing reg property of %pOFn\n", child);
681 } else if (val > INA3221_CHANNEL3) {
682 dev_err(dev, "invalid reg %d of %pOFn\n", val, child);
686 input = &ina->inputs[val];
688 /* Log the disconnected channel input */
689 if (!of_device_is_available(child)) {
690 input->disconnected = true;
694 /* Save the connected input label if available */
695 of_property_read_string(child, "label", &input->label);
697 /* Overwrite default shunt resistor value optionally */
698 if (!of_property_read_u32(child, "shunt-resistor-micro-ohms", &val)) {
699 if (val < 1 || val > INT_MAX) {
700 dev_err(dev, "invalid shunt resistor value %u of %pOFn\n",
704 input->shunt_resistor = val;
710 static int ina3221_probe_from_dt(struct device *dev, struct ina3221_data *ina)
712 const struct device_node *np = dev->of_node;
713 struct device_node *child;
716 /* Compatible with non-DT platforms */
720 ina->single_shot = of_property_read_bool(np, "ti,single-shot");
722 for_each_child_of_node(np, child) {
723 ret = ina3221_probe_child_from_dt(dev, child, ina);
731 static int ina3221_probe(struct i2c_client *client,
732 const struct i2c_device_id *id)
734 struct device *dev = &client->dev;
735 struct ina3221_data *ina;
736 struct device *hwmon_dev;
739 ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
743 ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
744 if (IS_ERR(ina->regmap)) {
745 dev_err(dev, "Unable to allocate register map\n");
746 return PTR_ERR(ina->regmap);
749 for (i = 0; i < F_MAX_FIELDS; i++) {
750 ina->fields[i] = devm_regmap_field_alloc(dev,
752 ina3221_reg_fields[i]);
753 if (IS_ERR(ina->fields[i])) {
754 dev_err(dev, "Unable to allocate regmap fields\n");
755 return PTR_ERR(ina->fields[i]);
759 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
760 ina->inputs[i].shunt_resistor = INA3221_RSHUNT_DEFAULT;
762 ret = ina3221_probe_from_dt(dev, ina);
764 dev_err(dev, "Unable to probe from device tree\n");
768 /* The driver will be reset, so use reset value */
769 ina->reg_config = INA3221_CONFIG_DEFAULT;
771 /* Clear continuous bit to use single-shot mode */
772 if (ina->single_shot)
773 ina->reg_config &= ~INA3221_CONFIG_MODE_CONTINUOUS;
775 /* Disable channels if their inputs are disconnected */
776 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
777 if (ina->inputs[i].disconnected)
778 ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i);
782 mutex_init(&ina->lock);
783 dev_set_drvdata(dev, ina);
785 /* Enable PM runtime -- status is suspended by default */
786 pm_runtime_enable(ina->pm_dev);
788 /* Initialize (resume) the device */
789 for (i = 0; i < INA3221_NUM_CHANNELS; i++) {
790 if (ina->inputs[i].disconnected)
792 /* Match the refcount with number of enabled channels */
793 ret = pm_runtime_get_sync(ina->pm_dev);
798 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina,
801 if (IS_ERR(hwmon_dev)) {
802 dev_err(dev, "Unable to register hwmon device\n");
803 ret = PTR_ERR(hwmon_dev);
810 pm_runtime_disable(ina->pm_dev);
811 pm_runtime_set_suspended(ina->pm_dev);
812 /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */
813 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
814 pm_runtime_put_noidle(ina->pm_dev);
815 mutex_destroy(&ina->lock);
820 static int ina3221_remove(struct i2c_client *client)
822 struct ina3221_data *ina = dev_get_drvdata(&client->dev);
825 pm_runtime_disable(ina->pm_dev);
826 pm_runtime_set_suspended(ina->pm_dev);
828 /* pm_runtime_put_noidle() will decrease the PM refcount until 0 */
829 for (i = 0; i < INA3221_NUM_CHANNELS; i++)
830 pm_runtime_put_noidle(ina->pm_dev);
832 mutex_destroy(&ina->lock);
837 static int __maybe_unused ina3221_suspend(struct device *dev)
839 struct ina3221_data *ina = dev_get_drvdata(dev);
842 /* Save config register value and enable cache-only */
843 ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
847 /* Set to power-down mode for power saving */
848 ret = regmap_update_bits(ina->regmap, INA3221_CONFIG,
849 INA3221_CONFIG_MODE_MASK,
850 INA3221_CONFIG_MODE_POWERDOWN);
854 regcache_cache_only(ina->regmap, true);
855 regcache_mark_dirty(ina->regmap);
860 static int __maybe_unused ina3221_resume(struct device *dev)
862 struct ina3221_data *ina = dev_get_drvdata(dev);
865 regcache_cache_only(ina->regmap, false);
867 /* Software reset the chip */
868 ret = regmap_field_write(ina->fields[F_RST], true);
870 dev_err(dev, "Unable to reset device\n");
874 /* Restore cached register values to hardware */
875 ret = regcache_sync(ina->regmap);
879 /* Restore config register value to hardware */
880 ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
887 static const struct dev_pm_ops ina3221_pm = {
888 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
889 pm_runtime_force_resume)
890 SET_RUNTIME_PM_OPS(ina3221_suspend, ina3221_resume, NULL)
893 static const struct of_device_id ina3221_of_match_table[] = {
894 { .compatible = "ti,ina3221", },
897 MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
899 static const struct i2c_device_id ina3221_ids[] = {
903 MODULE_DEVICE_TABLE(i2c, ina3221_ids);
905 static struct i2c_driver ina3221_i2c_driver = {
906 .probe = ina3221_probe,
907 .remove = ina3221_remove,
909 .name = INA3221_DRIVER_NAME,
910 .of_match_table = ina3221_of_match_table,
913 .id_table = ina3221_ids,
915 module_i2c_driver(ina3221_i2c_driver);
917 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
918 MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
919 MODULE_LICENSE("GPL v2");