Skip to content

Commit 51c6fa3

Browse files
committed
hwmon: (ina2xx) Consolidate chip initialization code
Move all chip initialization code into a single function. No functional change. Reviewed-by: Tzung-Bi Shih <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent ab7fbee commit 51c6fa3

File tree

1 file changed

+43
-68
lines changed

1 file changed

+43
-68
lines changed

drivers/hwmon/ina2xx.c

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@
6767

6868
#define INA226_READ_AVG(reg) FIELD_GET(INA226_AVG_RD_MASK, reg)
6969

70-
#define INA226_ALERT_POLARITY_MASK BIT(1)
71-
#define INA226_ALERT_POL_LOW 0
72-
#define INA226_ALERT_POL_HIGH 1
70+
#define INA226_ALERT_POLARITY BIT(1)
7371

7472
/* bit number of alert functions in Mask/Enable Register */
7573
#define INA226_SHUNT_OVER_VOLTAGE_MASK BIT(15)
@@ -141,6 +139,7 @@ struct ina2xx_config {
141139

142140
struct ina2xx_data {
143141
const struct ina2xx_config *config;
142+
enum ina2xx_ids chip;
144143

145144
long rshunt;
146145
long current_lsb_uA;
@@ -211,39 +210,6 @@ static u16 ina226_interval_to_reg(unsigned long interval)
211210
return FIELD_PREP(INA226_AVG_RD_MASK, avg_bits);
212211
}
213212

214-
static int ina2xx_set_alert_polarity(struct ina2xx_data *data,
215-
unsigned long val)
216-
{
217-
return regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
218-
INA226_ALERT_POLARITY_MASK,
219-
FIELD_PREP(INA226_ALERT_POLARITY_MASK, val));
220-
}
221-
222-
/*
223-
* Calibration register is set to the best value, which eliminates
224-
* truncation errors on calculating current register in hardware.
225-
* According to datasheet (eq. 3) the best values are 2048 for
226-
* ina226 and 4096 for ina219. They are hardcoded as calibration_value.
227-
*/
228-
static int ina2xx_calibrate(struct ina2xx_data *data)
229-
{
230-
return regmap_write(data->regmap, INA2XX_CALIBRATION,
231-
data->config->calibration_value);
232-
}
233-
234-
/*
235-
* Initialize the configuration and calibration registers.
236-
*/
237-
static int ina2xx_init(struct ina2xx_data *data)
238-
{
239-
int ret = regmap_write(data->regmap, INA2XX_CONFIG,
240-
data->config->config_default);
241-
if (ret < 0)
242-
return ret;
243-
244-
return ina2xx_calibrate(data);
245-
}
246-
247213
static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
248214
{
249215
struct ina2xx_data *data = dev_get_drvdata(dev);
@@ -651,12 +617,48 @@ static const struct attribute_group ina226_group = {
651617
.attrs = ina226_attrs,
652618
};
653619

620+
/*
621+
* Initialize chip
622+
*/
623+
static int ina2xx_init(struct device *dev, struct ina2xx_data *data)
624+
{
625+
struct regmap *regmap = data->regmap;
626+
u32 shunt;
627+
int ret;
628+
629+
if (device_property_read_u32(dev, "shunt-resistor", &shunt) < 0)
630+
shunt = INA2XX_RSHUNT_DEFAULT;
631+
632+
ret = ina2xx_set_shunt(data, shunt);
633+
if (ret < 0)
634+
return ret;
635+
636+
ret = regmap_write(regmap, INA2XX_CONFIG, data->config->config_default);
637+
if (ret < 0)
638+
return ret;
639+
640+
if (data->chip == ina226) {
641+
bool active_high = device_property_read_bool(dev, "ti,alert-polarity-active-high");
642+
643+
regmap_update_bits(regmap, INA226_MASK_ENABLE, INA226_ALERT_POLARITY,
644+
FIELD_PREP(INA226_ALERT_POLARITY, active_high));
645+
}
646+
647+
/*
648+
* Calibration register is set to the best value, which eliminates
649+
* truncation errors on calculating current register in hardware.
650+
* According to datasheet (eq. 3) the best values are 2048 for
651+
* ina226 and 4096 for ina219. They are hardcoded as calibration_value.
652+
*/
653+
return regmap_write(regmap, INA2XX_CALIBRATION,
654+
data->config->calibration_value);
655+
}
656+
654657
static int ina2xx_probe(struct i2c_client *client)
655658
{
656659
struct device *dev = &client->dev;
657660
struct ina2xx_data *data;
658661
struct device *hwmon_dev;
659-
u32 val;
660662
int ret, group = 0;
661663
enum ina2xx_ids chip;
662664

@@ -668,15 +670,9 @@ static int ina2xx_probe(struct i2c_client *client)
668670

669671
/* set the device type */
670672
data->config = &ina2xx_config[chip];
673+
data->chip = chip;
671674
mutex_init(&data->config_lock);
672675

673-
if (device_property_read_u32(dev, "shunt-resistor", &val) < 0)
674-
val = INA2XX_RSHUNT_DEFAULT;
675-
676-
ret = ina2xx_set_shunt(data, val);
677-
if (ret < 0)
678-
return dev_err_probe(dev, ret, "Invalid shunt resistor value\n");
679-
680676
data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
681677
if (IS_ERR(data->regmap)) {
682678
dev_err(dev, "failed to allocate register map\n");
@@ -687,30 +683,9 @@ static int ina2xx_probe(struct i2c_client *client)
687683
if (ret)
688684
return dev_err_probe(dev, ret, "failed to enable vs regulator\n");
689685

690-
if (chip == ina226) {
691-
if (device_property_read_bool(dev, "ti,alert-polarity-active-high")) {
692-
ret = ina2xx_set_alert_polarity(data,
693-
INA226_ALERT_POL_HIGH);
694-
if (ret < 0) {
695-
return dev_err_probe(dev, ret,
696-
"failed to set alert polarity active high\n");
697-
}
698-
} else {
699-
/* Set default value i.e active low */
700-
ret = ina2xx_set_alert_polarity(data,
701-
INA226_ALERT_POL_LOW);
702-
if (ret < 0) {
703-
return dev_err_probe(dev, ret,
704-
"failed to set alert polarity active low\n");
705-
}
706-
}
707-
}
708-
709-
ret = ina2xx_init(data);
710-
if (ret < 0) {
711-
dev_err(dev, "error configuring the device: %d\n", ret);
712-
return -ENODEV;
713-
}
686+
ret = ina2xx_init(dev, data);
687+
if (ret < 0)
688+
return dev_err_probe(dev, ret, "failed to configure device\n");
714689

715690
data->groups[group++] = &ina2xx_group;
716691
if (chip == ina226)

0 commit comments

Comments
 (0)