[PATCH 2/3] hwmon: (pmbus) refactor sensor initialization

From: Kallas, Pawel
Date: Wed Jul 06 2022 - 06:40:47 EST


Introduce separate function to initialize pmbus sensor attributes.
It can be used to initialize different sensor types.

Signed-off-by: Kallas, Pawel <pawel.kallas@xxxxxxxxx>
---
drivers/hwmon/pmbus/pmbus_core.c | 37 ++++++++++++++++++++------------
1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 4bcb70ab9b598..6e3ec6a223b92 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -1168,6 +1168,26 @@ static int pmbus_add_boolean(struct pmbus_data *data,
return pmbus_add_attribute(data, &a->dev_attr.attr);
}

+static void pmbus_sensor_init(struct pmbus_sensor *sensor, const char *name, const char *type,
+ int seq, int page, int phase, int reg,
+ enum pmbus_sensor_classes class, bool update, bool convert)
+{
+ if (type)
+ snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
+ name, seq, type);
+ else
+ snprintf(sensor->name, sizeof(sensor->name), "%s%d",
+ name, seq);
+
+ sensor->page = page;
+ sensor->phase = phase;
+ sensor->reg = reg;
+ sensor->class = class;
+ sensor->update = update;
+ sensor->convert = convert;
+ sensor->data = -ENODATA;
+}
+
static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
const char *name, const char *type,
int seq, int page, int phase,
@@ -1182,25 +1202,14 @@ static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
if (!sensor)
return NULL;
- a = &sensor->attribute;

- if (type)
- snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
- name, seq, type);
- else
- snprintf(sensor->name, sizeof(sensor->name), "%s%d",
- name, seq);
+ pmbus_sensor_init(sensor, name, type, seq, page, phase, reg, class, update, convert);

if (data->flags & PMBUS_WRITE_PROTECTED)
readonly = true;

- sensor->page = page;
- sensor->phase = phase;
- sensor->reg = reg;
- sensor->class = class;
- sensor->update = update;
- sensor->convert = convert;
- sensor->data = -ENODATA;
+ a = &sensor->attribute;
+
pmbus_dev_attr_init(a, sensor->name,
readonly ? 0444 : 0644,
pmbus_show_sensor, pmbus_set_sensor);