]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
hwmon: (ina238) Add ina238_config to save configurations for different chips
authorWenliang Yan <wenliang202407@163.com>
Tue, 6 May 2025 05:37:38 +0000 (01:37 -0400)
committerGuenter Roeck <linux@roeck-us.net>
Mon, 12 May 2025 16:40:56 +0000 (09:40 -0700)
Add structure ina238_config to store proprietary properties for different
chips to meet different chip adaptations

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
Link: https://lore.kernel.org/r/20250506053741.4837-2-wenliang202407@163.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/ina238.c

index 2d9f12f68d501e31b047924c34869baf50ea34ec..c8d6bd3041b3c9cd2bc6dacba4fe1389a74a3a95 100644 (file)
@@ -102,7 +102,20 @@ static const struct regmap_config ina238_regmap_config = {
        .val_bits = 16,
 };
 
+enum ina238_ids { ina238, ina237 };
+
+struct ina238_config {
+       bool has_power_highest;         /* chip detection power peak */
+       bool has_energy;                        /* chip detection energy */
+       u8 temp_shift;                          /* fixed parameters for temp calculate */
+       u32 power_calculate_factor;     /* fixed parameters for power calculate */
+       u16 config_default;                     /* Power-on default state */
+       int bus_voltage_lsb;            /* use for temperature calculate, uV/lsb */
+       int temp_lsb;                           /* use for temperature calculate */
+};
+
 struct ina238_data {
+       const struct ina238_config *config;
        struct i2c_client *client;
        struct mutex config_lock;
        struct regmap *regmap;
@@ -110,6 +123,27 @@ struct ina238_data {
        int gain;
 };
 
+static const struct ina238_config ina238_config[] = {
+       [ina238] = {
+               .has_energy = false,
+               .has_power_highest = false,
+               .temp_shift = 4,
+               .power_calculate_factor = 20,
+               .config_default = INA238_CONFIG_DEFAULT,
+               .bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
+               .temp_lsb = INA238_DIE_TEMP_LSB,
+       },
+       [ina237] = {
+               .has_energy = false,
+               .has_power_highest = false,
+               .temp_shift = 4,
+               .power_calculate_factor = 20,
+               .config_default = INA238_CONFIG_DEFAULT,
+               .bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
+               .temp_lsb = INA238_DIE_TEMP_LSB,
+       },
+};
+
 static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
 {
        u8 data[3];
@@ -536,14 +570,20 @@ static int ina238_probe(struct i2c_client *client)
        struct device *dev = &client->dev;
        struct device *hwmon_dev;
        struct ina238_data *data;
+       enum ina238_ids chip;
        int config;
        int ret;
 
+       chip = (uintptr_t)i2c_get_match_data(client);
+
        data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
        if (!data)
                return -ENOMEM;
 
        data->client = client;
+       /* set the device type */
+       data->config = &ina238_config[chip];
+
        mutex_init(&data->config_lock);
 
        data->regmap = devm_regmap_init_i2c(client, &ina238_regmap_config);
@@ -570,7 +610,7 @@ static int ina238_probe(struct i2c_client *client)
        }
 
        /* Setup CONFIG register */
-       config = INA238_CONFIG_DEFAULT;
+       config = data->config->config_default;
        if (data->gain == 1)
                config |= INA238_CONFIG_ADCRANGE; /* ADCRANGE = 1 is /1 */
        ret = regmap_write(data->regmap, INA238_CONFIG, config);
@@ -616,15 +656,22 @@ static int ina238_probe(struct i2c_client *client)
 }
 
 static const struct i2c_device_id ina238_id[] = {
-       { "ina238" },
+       { "ina237", ina237 },
+       { "ina238", ina238 },
        { }
 };
 MODULE_DEVICE_TABLE(i2c, ina238_id);
 
 static const struct of_device_id __maybe_unused ina238_of_match[] = {
-       { .compatible = "ti,ina237" },
-       { .compatible = "ti,ina238" },
-       { },
+       {
+               .compatible = "ti,ina237",
+               .data = (void *)ina237
+       },
+       {
+               .compatible = "ti,ina238",
+               .data = (void *)ina238
+       },
+       { }
 };
 MODULE_DEVICE_TABLE(of, ina238_of_match);