hwmon: (pmbus/mp2975) Refactor the driver
PATCH 1/3 hwmon: (pmbus/mp2975) Replace home made version of __assign_bit()
In reply to Andy ShevchenkoThe newly introduced SWAP() macro is quite generic by naming, but moreover it's a repetition of the existing __assign_bit(). With this applied, add a missing bits.h (via now required bitops.h).
--- drivers/hwmon/pmbus/mp2975.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
drivers/hwmon/pmbus/mp2975.c +15 -16
@@ -5,6 +5,7 @@* Copyright (C) 2020 Nvidia Technologies Ltd. */ +#include <linux/bitops.h> #include <linux/err.h> #include <linux/i2c.h> #include <linux/init.h>
@@ -396,7 +397,7 @@ static int mp2973_write_word_data(struct i2c_client *client, int page, int reg, u16 word) {u8 target, mask; -int ret; +long ret;if (reg !=PMBUS_SMBALERT_MASK )return -ENODATA ;
@@ -415,7 +416,6 @@ static int mp2973_write_word_data(struct i2c_client *client, int page,* Set/Clear 'bit' in 'ret' based on condition followed by define for each bit in SMBALERT_MASK. * Also bit 2 & 15 are reserved. */ -#define SWAP (val, mask, cond, bit) (((mask) & (cond)) ? ((val) & ~BIT(bit)) : ((val) | BIT(bit)))#define MP2973_TEMP_OT 0#define MP2973_VIN_UVLO 1
@@ -434,36 +434,35 @@ static int mp2973_write_word_data(struct i2c_client *client, int page,switch (target) {case PMBUS_STATUS_CML : - ret =SWAP (ret, mask,PB_CML_FAULT_INVALID_DATA ,MP2973_INVALID_DATA ); - ret =SWAP (ret, mask,PB_CML_FAULT_INVALID_COMMAND ,MP2973_INVALID_COMMAND ); - ret =SWAP (ret, mask,PB_CML_FAULT_OTHER_COMM ,MP2973_OTHER_COMM ); - ret =SWAP (ret, mask,PB_CML_FAULT_PACKET_ERROR ,MP2973_PACKET_ERROR ); +__assign_bit (MP2973_INVALID_DATA , &ret, !(mask &PB_CML_FAULT_INVALID_DATA )); +__assign_bit (MP2973_INVALID_COMMAND , &ret, !(mask &PB_CML_FAULT_INVALID_COMMAND )); +__assign_bit (MP2973_OTHER_COMM , &ret, !(mask &PB_CML_FAULT_OTHER_COMM )); +__assign_bit (MP2973_PACKET_ERROR , &ret, !(mask &PB_CML_FAULT_PACKET_ERROR ));break ;case PMBUS_STATUS_VOUT : - ret =SWAP (ret, mask,PB_VOLTAGE_UV_FAULT ,MP2973_VOLTAGE_UV ); - ret =SWAP (ret, mask,PB_VOLTAGE_OV_FAULT ,MP2973_VOLTAGE_OV ); +__assign_bit (MP2973_VOLTAGE_UV , &ret, !(mask &PB_VOLTAGE_UV_FAULT )); +__assign_bit (MP2973_VOLTAGE_OV , &ret, !(mask &PB_VOLTAGE_OV_FAULT ));break ;case PMBUS_STATUS_IOUT : - ret =SWAP (ret, mask,PB_IOUT_OC_FAULT ,MP2973_IOUT_OC ); - ret =SWAP (ret, mask,PB_IOUT_OC_LV_FAULT ,MP2973_IOUT_OC_LV ); +__assign_bit (MP2973_IOUT_OC , &ret, !(mask &PB_IOUT_OC_FAULT )); +__assign_bit (MP2973_IOUT_OC_LV , &ret, !(mask &PB_IOUT_OC_LV_FAULT ));break ;case PMBUS_STATUS_TEMPERATURE : - ret =SWAP (ret, mask,PB_TEMP_OT_FAULT ,MP2973_TEMP_OT ); +__assign_bit (MP2973_TEMP_OT , &ret, !(mask &PB_TEMP_OT_FAULT ));break ;/* * Map remaining bits to MFR specific to let the PMBUS core mask * those bits by default. */ case PMBUS_STATUS_MFR_SPECIFIC : - ret =SWAP (ret, mask,BIT (1 ),MP2973_VIN_UVLO ); - ret =SWAP (ret, mask,BIT (3 ),MP2973_VIN_OVP ); - ret =SWAP (ret, mask,BIT (4 ),MP2973_MTP_FAULT ); - ret =SWAP (ret, mask,BIT (6 ),MP2973_MTP_BLK_TRIG ); +__assign_bit (MP2973_VIN_UVLO , &ret, !(mask &BIT (1 ))); +__assign_bit (MP2973_VIN_OVP , &ret, !(mask &BIT (3 ))); +__assign_bit (MP2973_MTP_FAULT , &ret, !(mask &BIT (4 ))); +__assign_bit (MP2973_MTP_BLK_TRIG , &ret, !(mask &BIT (6 )));break ;default :return 0 ; } -#undef SWAPreturn pmbus_write_word_data (client,0 ,PMBUS_SMBALERT_MASK , ret); }
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
cover hwmon: (pmbus/mp2975) Refactor the driver
Cleanups and refactoring to make it OF-independent (by compilation). of_device.h is target to remove for the cases where it's not needed, hende the last patch, which also makes code cleaner.
Andy Shevchenko (3):
hwmon: (pmbus/mp2975) Replace home made version of __assign_bit()
hwmon: (pmbus/mp2975) Constify local pointers to pmbus_driver_info
hwmon: (pmbus/mp2975) Use i2c_get_match_data()
drivers/hwmon/pmbus/mp2975.c | 89 ++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 2/3 hwmon: (pmbus/mp2975) Constify local pointers to pmbus_driver_info
Constify the local variables pointing to "struct pmbus_driver_info" and other encoding params to annotate the function is not modifying pointed data.
--- drivers/hwmon/pmbus/mp2975.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/pmbus/mp2975.c +2 -2
@@ -942,7 +942,7 @@ mp2975_vout_per_rail_config_get(struct i2c_client *client,return 0 ; } -static struct pmbus_driver_info mp2975_info = { +static const struct pmbus_driver_info mp2975_info = { .pages =1 , .format[PSC_VOLTAGE_IN ] = linear, .format[PSC_VOLTAGE_OUT ] = direct,
@@ -967,7 +967,7 @@ static struct pmbus_driver_info mp2975_info = {#endif }; -static struct pmbus_driver_info mp2973_info = { +static const struct pmbus_driver_info mp2973_info = { .pages =1 , .format[PSC_VOLTAGE_IN ] = linear, .format[PSC_VOLTAGE_OUT ] = direct,
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
PATCH 3/3 hwmon: (pmbus/mp2975) Use i2c_get_match_data()
In reply to Andy ShevchenkoUse preferred i2c_get_match_data() instead of of_device_get_match_data() to get the driver match data. With this, adjust the includes to explicitly include the correct headers.
--- drivers/hwmon/pmbus/mp2975.c | 54 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 22 deletions(-)
drivers/hwmon/pmbus/mp2975.c +32 -22
@@ -10,8 +10,9 @@#include <linux/i2c.h> #include <linux/init.h> #include <linux/kernel.h> +#include <linux/mod_devicetable.h> #include <linux/module.h> -#include <linux/of_device.h> +#include "pmbus.h" /* Vendor specific registers. */
@@ -98,6 +99,11 @@ static const int mp2975_max_phases[][MP2975_PAGE_NUM] = { [mp2971] = { MP2971_MAX_PHASE_RAIL1, MP2971_MAX_PHASE_RAIL2 }, }; +struct mp2975_driver_info { +const struct pmbus_driver_info *info; +enum chips chip_id; +}; +struct mp2975_data {struct pmbus_driver_info info;enum chips chip_id;
@@ -111,15 +117,6 @@ struct mp2975_data {int curr_sense_gain[MP2975_PAGE_NUM ]; }; -static const struct i2c_device_id mp2975_id[] = { - {"mp2971" , mp2971}, - {"mp2973" , mp2973}, - {"mp2975" , mp2975}, - {} -}; - -MODULE_DEVICE_TABLE (i2c, mp2975_id); -static const struct regulator_desc __maybe_unused mp2975_reg_desc[] = {PMBUS_REGULATOR ("vout" ,0 ),PMBUS_REGULATOR ("vout" ,1 ),
@@ -989,29 +986,34 @@ static const struct pmbus_driver_info mp2973_info = {#endif }; +static const struct mp2975_driver_info mp2975_ddinfo[] = { + [mp2975] = { .info = &mp2975_info, .chip_id = mp2975 }, + [mp2973] = { .info = &mp2973_info, .chip_id = mp2973 }, + [mp2971] = { .info = &mp2973_info, .chip_id = mp2971 }, +}; +static int mp2975_probe (struct i2c_client *client) { +const struct mp2975_driver_info *ddinfo;struct pmbus_driver_info *info;struct mp2975_data *data;int ret; + ddinfo =i2c_get_match_data (client); +if (!ddinfo) +return -ENODEV ; + data =devm_kzalloc (&client->dev,sizeof (struct mp2975_data ),GFP_KERNEL );if (!data)return -ENOMEM ; -if (client->dev.of_node) - data->chip_id = (enum chips )(unsigned long )of_device_get_match_data (&client->dev); -else - data->chip_id =i2c_match_id (mp2975_id, client)->driver_data; + data->chip_id = ddinfo->chip_id;memcpy (data->max_phases, mp2975_max_phases[data->chip_id],sizeof (data->max_phases)); -if (data->chip_id == mp2975) -memcpy (&data->info, &mp2975_info,sizeof (*info)); -else -memcpy (&data->info, &mp2973_info,sizeof (*info)); +memcpy (&data->info, ddinfo->info,sizeof (data->info)); info = &data->info;
@@ -1070,17 +1072,25 @@ static int mp2975_probe(struct i2c_client *client) }static const struct of_device_id __maybe_unused mp2975_of_match[] = { - {.compatible ="mps,mp2971" , .data = (void *)mp2971}, - {.compatible ="mps,mp2973" , .data = (void *)mp2973}, - {.compatible ="mps,mp2975" , .data = (void *)mp2975}, + {.compatible ="mps,mp2971" , .data = &mp2975_ddinfo[mp2971]}, + {.compatible ="mps,mp2973" , .data = &mp2975_ddinfo[mp2973]}, + {.compatible ="mps,mp2975" , .data = &mp2975_ddinfo[mp2975]}, {} };MODULE_DEVICE_TABLE (of, mp2975_of_match); +static const struct i2c_device_id mp2975_id[] = { + {"mp2971" , (kernel_ulong_t)&mp2975_ddinfo[mp2971]}, + {"mp2973" , (kernel_ulong_t)&mp2975_ddinfo[mp2973]}, + {"mp2975" , (kernel_ulong_t)&mp2975_ddinfo[mp2975]}, + {} +}; +MODULE_DEVICE_TABLE (i2c, mp2975_id); +static struct i2c_driver mp2975_driver = { .driver = { .name ="mp2975" , - .of_match_table =of_match_ptr (mp2975_of_match), + .of_match_table = mp2975_of_match, }, .probe = mp2975_probe, .id_table = mp2975_id,
Signature · 1 line
2.43.0.rc1.1.gbec44491f096
Guenter Roeck
maintainerRe: [PATCH v1 1/3] hwmon: (pmbus/mp2975) Replace home made version of __assign_bit()
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
The newly introduced
SWAP()macro is quite generic by naming, but moreover it's a repetition of the existing__assign_bit(). With this applied, add a missingbits.h(via now requiredbitops.h).Signed-off-by Andy Shevchenko <[email protected]>
Not sure if I like __assign_bit() more than SWAP(), but at least it is "standard". Applied.
Thanks,
Guenter
Re: [PATCH v1 2/3] hwmon: (pmbus/mp2975) Constify local pointers to pmbus_driver_info
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Constify the local variables pointing to "struct
pmbus_driver_info" and other encoding params to annotate the function is not modifying pointed data.Signed-off-by Andy Shevchenko <[email protected]>
Applied.
Thanks,
Guenter
Guenter Roeck
maintainerRe: [PATCH v1 3/3] hwmon: (pmbus/mp2975) Use i2c_get_match_data()
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Use preferred
i2c_get_match_data()instead ofof_device_get_match_data()to get the driver match data. With this, adjust the includes to explicitly include the correct headers.Signed-off-by Andy Shevchenko <[email protected]>
Appied, with additional change noted below.
Quoted 91 lines
--- drivers/hwmon/pmbus/mp2975.c | 54 +++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 22 deletions(-)drivers/hwmon/pmbus/mp2975.c +20
-18@@ -10,8 +10,9 @@#include <linux/i2c.h> #include <linux/init.h> #include <linux/kernel.h> +#include <linux/mod_devicetable.h> #include <linux/module.h> -#include <linux/of_device.h> +#include "pmbus.h" /* Vendor specific registers. */ @@ -98,6 +99,11 @@ static const int mp2975_max_phases[][MP2975_PAGE_NUM] = { [mp2971] = { MP2971_MAX_PHASE_RAIL1, MP2971_MAX_PHASE_RAIL2 }, }; +struct mp2975_driver_info { +const struct pmbus_driver_info *info; +enum chips chip_id; +}; +struct mp2975_data {struct pmbus_driver_info info;enum chips chip_id;@@ -111,15 +117,6 @@ struct mp2975_data {int curr_sense_gain[MP2975_PAGE_NUM ]; }; -static const struct i2c_device_id mp2975_id[] = { - {"mp2971" , mp2971}, - {"mp2973" , mp2973}, - {"mp2975" , mp2975}, - {} -}; - -MODULE_DEVICE_TABLE (i2c, mp2975_id); -static const struct regulator_desc __maybe_unused mp2975_reg_desc[] = {PMBUS_REGULATOR ("vout" ,0 ),PMBUS_REGULATOR ("vout" ,1 ),@@ -989,29 +986,34 @@ static const struct pmbus_driver_info mp2973_info = {#endif }; +static const struct mp2975_driver_info mp2975_ddinfo[] = { + [mp2975] = { .info = &mp2975_info, .chip_id = mp2975 }, + [mp2973] = { .info = &mp2973_info, .chip_id = mp2973 }, + [mp2971] = { .info = &mp2973_info, .chip_id = mp2971 }, +}; +static int mp2975_probe (struct i2c_client *client) { +const struct mp2975_driver_info *ddinfo;struct pmbus_driver_info *info;struct mp2975_data *data;int ret; + ddinfo =i2c_get_match_data (client); +if (!ddinfo) +return -ENODEV ; + data =devm_kzalloc (&client->dev,sizeof (struct mp2975_data ),GFP_KERNEL );if (!data)return -ENOMEM ; -if (client->dev.of_node) - data->chip_id = (enum chips )(unsigned long )of_device_get_match_data (&client->dev); -else - data->chip_id =i2c_match_id (mp2975_id, client)->driver_data; + data->chip_id = ddinfo->chip_id;memcpy (data->max_phases, mp2975_max_phases[data->chip_id],sizeof (data->max_phases)); -if (data->chip_id == mp2975) -memcpy (&data->info, &mp2975_info,sizeof (*info)); -else -memcpy (&data->info, &mp2973_info,sizeof (*info)); +memcpy (&data->info, ddinfo->info,sizeof (data->info)); info = &data->info;
drivers/hwmon/pmbus/mp2975.c static int mp2975_probe(struct i2c_client *client) · line 1074
Quoted 3 lines
drivers/hwmon/pmbus/mp2975.c +0
-0@@ -1070,17 +1072,25 @@ static int mp2975_probe(struct i2c_client *client) }static const struct of_device_id __maybe_unused mp2975_of_match[] = {
This is no longer __maybe_unused.
Thanks,
Guenter
Quoted 26 lines
drivers/hwmon/pmbus/mp2975.c +12
-4- {.compatible = "mps,mp2971", .data = (void *)mp2971}, - {.compatible = "mps,mp2973", .data = (void *)mp2973}, - {.compatible = "mps,mp2975", .data = (void *)mp2975}, + {.compatible = "mps,mp2971", .data = &mp2975_ddinfo[mp2971]}, + {.compatible = "mps,mp2973", .data = &mp2975_ddinfo[mp2973]}, + {.compatible = "mps,mp2975", .data = &mp2975_ddinfo[mp2975]}, {} };MODULE_DEVICE_TABLE (of, mp2975_of_match); +static const struct i2c_device_id mp2975_id[] = { + {"mp2971" , (kernel_ulong_t)&mp2975_ddinfo[mp2971]}, + {"mp2973" , (kernel_ulong_t)&mp2975_ddinfo[mp2973]}, + {"mp2975" , (kernel_ulong_t)&mp2975_ddinfo[mp2975]}, + {} +}; +MODULE_DEVICE_TABLE (i2c, mp2975_id); +static struct i2c_driver mp2975_driver = { .driver = { .name ="mp2975" , - .of_match_table =of_match_ptr (mp2975_of_match), + .of_match_table = mp2975_of_match, }, .probe = mp2975_probe, .id_table = mp2975_id,
Re: [PATCH v1 1/3] hwmon: (pmbus/mp2975) Replace home made version of __assign_bit()
In reply to Guenter RoeckQuoted 8 lines · Guenter Roeck
Quoted 5 lines · Andy Shevchenko
The newly introduced
SWAP()macro is quite generic by naming, but moreover it's a repetition of the existing__assign_bit(). With this applied, add a missingbits.h(via now requiredbitops.h).Signed-off-by Andy Shevchenko <[email protected]>Not sure if I like
__assign_bit()more thanSWAP(),
To add to the mess, we have swap() already defined globally. This one steps on our toes.
Quoted 1 line
but at least it is "standard". Applied.
Thank you!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 3/3] hwmon: (pmbus/mp2975) Use i2c_get_match_data()
In reply to Guenter RoeckQuoted 1 line · Guenter Roeck
On Mon, Mar 25, 2024 at 02:07:44PM +0200, Andy Shevchenko wrote:
...
Quoted 3 lines
Quoted 1 line
static const struct
of_device_id__maybe_unusedmp2975_of_match[] = {This is no longer
__maybe_unused.
True, thank you!
Signature · 2 lines
With Best Regards, Andy Shevchenko