Skip to content

Commit bb25cdc

Browse files
committed
hwmon: (ina2xx) Re-initialize chip using regmap functions
If it is necessary to re-initialize the chip, for example because it has been power cycled, use regmap functions to update register contents. This ensures that all registers, including the configuration register and alert registers, are updated to previously configured values without having to locally cache everything. For this to work, volatile registers have to be marked as volatile. Also, the cache needs to be bypassed when reading the calibration and mask_enable registers. While the calibration register is not volatile, it will be reset to 0 if the chip has been power cycled. Most of the bits in the mask_enable register are configuration bits, except for bit 4 which reports if an alert has been observed. Both registers need to be marked as non-volatile to be updated after a power cycle, but it is necessary to bypass the cache when reading them to detect if the chip has been power cycled and to read the alert status. The chip does not support register auto-increments. It is therefore necessary to configure regmap to use single register read/write operations. Otherwise regmap tries to write all registers in a single operation when synchronizing register contents with the hardware, and the synchronization fails. Another necessary change is to declare ina226_alert_to_reg() as u16. So far it returned an s16 which is sign extended to a large negative value which is then sent to regmap as unsigned int, causing an -EINVAL error return. Reviewed-by: Tzung-Bi Shih <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent d491e78 commit bb25cdc

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

drivers/hwmon/ina2xx.c

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,41 @@
9191
*/
9292
#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
9393

94+
static bool ina2xx_writeable_reg(struct device *dev, unsigned int reg)
95+
{
96+
switch (reg) {
97+
case INA2XX_CONFIG:
98+
case INA2XX_CALIBRATION:
99+
case INA226_MASK_ENABLE:
100+
case INA226_ALERT_LIMIT:
101+
return true;
102+
default:
103+
return false;
104+
}
105+
}
106+
107+
static bool ina2xx_volatile_reg(struct device *dev, unsigned int reg)
108+
{
109+
switch (reg) {
110+
case INA2XX_SHUNT_VOLTAGE:
111+
case INA2XX_BUS_VOLTAGE:
112+
case INA2XX_POWER:
113+
case INA2XX_CURRENT:
114+
return true;
115+
default:
116+
return false;
117+
}
118+
}
119+
94120
static const struct regmap_config ina2xx_regmap_config = {
95121
.reg_bits = 8,
96122
.val_bits = 16,
123+
.use_single_write = true,
124+
.use_single_read = true,
97125
.max_register = INA2XX_MAX_REGISTERS,
126+
.cache_type = REGCACHE_MAPLE,
127+
.volatile_reg = ina2xx_volatile_reg,
128+
.writeable_reg = ina2xx_writeable_reg,
98129
};
99130

100131
enum ina2xx_ids { ina219, ina226 };
@@ -229,16 +260,16 @@ static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
229260
if (*regval == 0) {
230261
unsigned int cal;
231262

232-
ret = regmap_read(regmap, INA2XX_CALIBRATION, &cal);
263+
ret = regmap_read_bypassed(regmap, INA2XX_CALIBRATION, &cal);
233264
if (ret < 0)
234265
return ret;
235266

236267
if (cal == 0) {
237268
dev_warn(dev, "chip not calibrated, reinitializing\n");
238269

239-
ret = ina2xx_init(data);
240-
if (ret < 0)
241-
return ret;
270+
regcache_mark_dirty(regmap);
271+
regcache_sync(regmap);
272+
242273
/*
243274
* Let's make sure the power and current
244275
* registers have been updated before trying
@@ -340,7 +371,7 @@ static int ina226_reg_to_alert(struct ina2xx_data *data, u32 mask, u16 regval)
340371
* Turns alert limit values into register values.
341372
* Opposite of the formula in ina2xx_get_value().
342373
*/
343-
static s16 ina226_alert_to_reg(struct ina2xx_data *data, u32 mask, int val)
374+
static u16 ina226_alert_to_reg(struct ina2xx_data *data, u32 mask, int val)
344375
{
345376
switch (mask) {
346377
case INA226_SHUNT_OVER_VOLTAGE_MASK:
@@ -439,16 +470,17 @@ static ssize_t ina226_alarm_show(struct device *dev,
439470
{
440471
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
441472
struct ina2xx_data *data = dev_get_drvdata(dev);
442-
int regval;
473+
unsigned int mask;
443474
int alarm = 0;
444475
int ret;
445476

446-
ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
477+
ret = regmap_read_bypassed(data->regmap, INA226_MASK_ENABLE, &mask);
447478
if (ret)
448479
return ret;
449480

450-
alarm = (regval & attr->index) &&
451-
(regval & INA226_ALERT_FUNCTION_FLAG);
481+
alarm = (mask & attr->index) &&
482+
(mask & INA226_ALERT_FUNCTION_FLAG);
483+
452484
return sysfs_emit(buf, "%d\n", alarm);
453485
}
454486

0 commit comments

Comments
 (0)