67
67
68
68
#define INA226_READ_AVG (reg ) FIELD_GET(INA226_AVG_RD_MASK, reg)
69
69
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)
73
71
74
72
/* bit number of alert functions in Mask/Enable Register */
75
73
#define INA226_SHUNT_OVER_VOLTAGE_MASK BIT(15)
@@ -141,6 +139,7 @@ struct ina2xx_config {
141
139
142
140
struct ina2xx_data {
143
141
const struct ina2xx_config * config ;
142
+ enum ina2xx_ids chip ;
144
143
145
144
long rshunt ;
146
145
long current_lsb_uA ;
@@ -211,39 +210,6 @@ static u16 ina226_interval_to_reg(unsigned long interval)
211
210
return FIELD_PREP (INA226_AVG_RD_MASK , avg_bits );
212
211
}
213
212
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
-
247
213
static int ina2xx_read_reg (struct device * dev , int reg , unsigned int * regval )
248
214
{
249
215
struct ina2xx_data * data = dev_get_drvdata (dev );
@@ -651,12 +617,48 @@ static const struct attribute_group ina226_group = {
651
617
.attrs = ina226_attrs ,
652
618
};
653
619
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
+
654
657
static int ina2xx_probe (struct i2c_client * client )
655
658
{
656
659
struct device * dev = & client -> dev ;
657
660
struct ina2xx_data * data ;
658
661
struct device * hwmon_dev ;
659
- u32 val ;
660
662
int ret , group = 0 ;
661
663
enum ina2xx_ids chip ;
662
664
@@ -668,15 +670,9 @@ static int ina2xx_probe(struct i2c_client *client)
668
670
669
671
/* set the device type */
670
672
data -> config = & ina2xx_config [chip ];
673
+ data -> chip = chip ;
671
674
mutex_init (& data -> config_lock );
672
675
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
-
680
676
data -> regmap = devm_regmap_init_i2c (client , & ina2xx_regmap_config );
681
677
if (IS_ERR (data -> regmap )) {
682
678
dev_err (dev , "failed to allocate register map\n" );
@@ -687,30 +683,9 @@ static int ina2xx_probe(struct i2c_client *client)
687
683
if (ret )
688
684
return dev_err_probe (dev , ret , "failed to enable vs regulator\n" );
689
685
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" );
714
689
715
690
data -> groups [group ++ ] = & ina2xx_group ;
716
691
if (chip == ina226 )
0 commit comments