hwmon: (max1668) Convert to use with_info hwmon API

Convert to use with_info API to simplify the code and to reduce its size.

This patch reduces object file size by approximately 25%.

Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Guenter Roeck 2024-07-15 07:48:24 -07:00
parent 355c529077
commit c1b93b5414

View File

@ -10,7 +10,6 @@
#include <linux/bits.h> #include <linux/bits.h>
#include <linux/err.h> #include <linux/err.h>
#include <linux/hwmon.h> #include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/module.h> #include <linux/module.h>
@ -49,247 +48,144 @@ MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
struct max1668_data { struct max1668_data {
struct regmap *regmap; struct regmap *regmap;
const struct attribute_group *groups[3];
int channels; int channels;
}; };
static ssize_t show_temp(struct device *dev, static int max1668_read(struct device *dev, enum hwmon_sensor_types type,
struct device_attribute *devattr, char *buf) u32 attr, int channel, long *val)
{ {
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
u32 temp;
int ret;
ret = regmap_read(data->regmap, MAX1668_REG_TEMP(index), &temp);
if (ret)
return ret;
return sprintf(buf, "%d\n", sign_extend32(temp, 7) * 1000);
}
static ssize_t show_temp_max(struct device *dev,
struct device_attribute *devattr, char *buf)
{
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
u32 temp;
int ret;
ret = regmap_read(data->regmap, MAX1668_REG_LIMH(index), &temp);
if (ret)
return ret;
return sprintf(buf, "%d\n", sign_extend32(temp, 7) * 1000);
}
static ssize_t show_temp_min(struct device *dev,
struct device_attribute *devattr, char *buf)
{
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
u32 temp;
int ret;
ret = regmap_read(data->regmap, MAX1668_REG_LIML(index), &temp);
if (ret)
return ret;
return sprintf(buf, "%d\n", sign_extend32(temp, 7) * 1000);
}
static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
char *buf)
{
int index = to_sensor_dev_attr(attr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
u32 alarm;
int ret;
ret = regmap_read(data->regmap,
index >= 8 ? MAX1668_REG_STAT1 : MAX1668_REG_STAT2,
&alarm);
if (ret)
return ret;
return sprintf(buf, "%u\n", !!(alarm & BIT(index & 7)));
}
static ssize_t show_fault(struct device *dev,
struct device_attribute *devattr, char *buf)
{
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev); struct max1668_data *data = dev_get_drvdata(dev);
struct regmap *regmap = data->regmap; struct regmap *regmap = data->regmap;
u32 alarm, temp; u32 regs[2] = { MAX1668_REG_STAT1, MAX1668_REG_TEMP(channel) };
u8 regvals[2];
u32 regval;
int ret; int ret;
ret = regmap_read(regmap, MAX1668_REG_STAT1, &alarm); switch (attr) {
if (ret) case hwmon_temp_input:
return ret; ret = regmap_read(regmap, MAX1668_REG_TEMP(channel), &regval);
if (ret)
ret = regmap_read(regmap, MAX1668_REG_TEMP(index), &temp); return ret;
if (ret) *val = sign_extend32(regval, 7) * 1000;
return ret; break;
case hwmon_temp_min:
return sprintf(buf, "%u\n", (alarm & BIT(4)) && temp == 127); ret = regmap_read(regmap, MAX1668_REG_LIML(channel), &regval);
if (ret)
return ret;
*val = sign_extend32(regval, 7) * 1000;
break;
case hwmon_temp_max:
ret = regmap_read(regmap, MAX1668_REG_LIMH(channel), &regval);
if (ret)
return ret;
*val = sign_extend32(regval, 7) * 1000;
break;
case hwmon_temp_min_alarm:
ret = regmap_read(regmap,
channel ? MAX1668_REG_STAT2 : MAX1668_REG_STAT1,
&regval);
if (ret)
return ret;
if (channel)
*val = !!(regval & BIT(9 - channel * 2));
else
*val = !!(regval & BIT(5));
break;
case hwmon_temp_max_alarm:
ret = regmap_read(regmap,
channel ? MAX1668_REG_STAT2 : MAX1668_REG_STAT1,
&regval);
if (ret)
return ret;
if (channel)
*val = !!(regval & BIT(8 - channel * 2));
else
*val = !!(regval & BIT(6));
break;
case hwmon_temp_fault:
ret = regmap_multi_reg_read(regmap, regs, regvals, 2);
if (ret)
return ret;
*val = !!((regvals[0] & BIT(4)) && regvals[1] == 127);
break;
default:
return -EOPNOTSUPP;
}
return 0;
} }
static ssize_t set_temp_max(struct device *dev, static int max1668_write(struct device *dev, enum hwmon_sensor_types type,
struct device_attribute *devattr, u32 attr, int channel, long val)
const char *buf, size_t count)
{ {
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev); struct max1668_data *data = dev_get_drvdata(dev);
long temp; struct regmap *regmap = data->regmap;
int ret;
ret = kstrtol(buf, 10, &temp); val = clamp_val(val / 1000, -128, 127);
if (ret < 0)
return ret;
temp = clamp_val(temp / 1000, -128, 127); switch (attr) {
ret = regmap_write(data->regmap, MAX1668_REG_LIMH(index), temp); case hwmon_temp_min:
if (ret < 0) return regmap_write(regmap, MAX1668_REG_LIML(channel), val);
count = ret; case hwmon_temp_max:
return regmap_write(regmap, MAX1668_REG_LIMH(channel), val);
return count; default:
return -EOPNOTSUPP;
}
} }
static ssize_t set_temp_min(struct device *dev, static umode_t max1668_is_visible(const void *_data, enum hwmon_sensor_types type,
struct device_attribute *devattr, u32 attr, int channel)
const char *buf, size_t count)
{ {
int index = to_sensor_dev_attr(devattr)->index; const struct max1668_data *data = _data;
struct max1668_data *data = dev_get_drvdata(dev);
long temp;
int ret;
ret = kstrtol(buf, 10, &temp); if (channel >= data->channels)
if (ret < 0) return 0;
return ret;
temp = clamp_val(temp / 1000, -128, 127); switch (attr) {
ret = regmap_write(data->regmap, MAX1668_REG_LIML(index), temp); case hwmon_temp_min:
if (ret < 0) case hwmon_temp_max:
count = ret; return read_only ? 0444 : 0644;
case hwmon_temp_input:
return count; case hwmon_temp_min_alarm:
case hwmon_temp_max_alarm:
return 0444;
case hwmon_temp_fault:
if (channel)
return 0444;
break;
default:
break;
}
return 0;
} }
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); static const struct hwmon_channel_info * const max1668_info[] = {
static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, HWMON_CHANNEL_INFO(temp,
set_temp_max, 0); HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM,
set_temp_min, 0); HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, HWMON_T_FAULT,
set_temp_max, 1); HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
set_temp_min, 1); HWMON_T_FAULT,
static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
set_temp_max, 2); HWMON_T_FAULT,
static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
set_temp_min, 2); HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); HWMON_T_FAULT),
static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max,
set_temp_max, 3);
static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min,
set_temp_min, 3);
static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max,
set_temp_max, 4);
static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min,
set_temp_min, 4);
static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14);
static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13);
static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7);
static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6);
static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5);
static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3);
static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2);
static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1);
static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0);
static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1);
static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2);
static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3);
static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4);
/* Attributes common to MAX1668, MAX1989 and MAX1805 */
static struct attribute *max1668_attribute_common[] = {
&sensor_dev_attr_temp1_max.dev_attr.attr,
&sensor_dev_attr_temp1_min.dev_attr.attr,
&sensor_dev_attr_temp1_input.dev_attr.attr,
&sensor_dev_attr_temp2_max.dev_attr.attr,
&sensor_dev_attr_temp2_min.dev_attr.attr,
&sensor_dev_attr_temp2_input.dev_attr.attr,
&sensor_dev_attr_temp3_max.dev_attr.attr,
&sensor_dev_attr_temp3_min.dev_attr.attr,
&sensor_dev_attr_temp3_input.dev_attr.attr,
&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp2_fault.dev_attr.attr,
&sensor_dev_attr_temp3_fault.dev_attr.attr,
NULL NULL
}; };
/* Attributes not present on MAX1805 */ static const struct hwmon_ops max1668_hwmon_ops = {
static struct attribute *max1668_attribute_unique[] = { .is_visible = max1668_is_visible,
&sensor_dev_attr_temp4_max.dev_attr.attr, .read = max1668_read,
&sensor_dev_attr_temp4_min.dev_attr.attr, .write = max1668_write,
&sensor_dev_attr_temp4_input.dev_attr.attr,
&sensor_dev_attr_temp5_max.dev_attr.attr,
&sensor_dev_attr_temp5_min.dev_attr.attr,
&sensor_dev_attr_temp5_input.dev_attr.attr,
&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
&sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
&sensor_dev_attr_temp4_fault.dev_attr.attr,
&sensor_dev_attr_temp5_fault.dev_attr.attr,
NULL
}; };
static umode_t max1668_attribute_mode(struct kobject *kobj, static const struct hwmon_chip_info max1668_chip_info = {
struct attribute *attr, int index) .ops = &max1668_hwmon_ops,
{ .info = max1668_info,
umode_t ret = S_IRUGO;
if (read_only)
return ret;
if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr ||
attr == &sensor_dev_attr_temp2_max.dev_attr.attr ||
attr == &sensor_dev_attr_temp3_max.dev_attr.attr ||
attr == &sensor_dev_attr_temp4_max.dev_attr.attr ||
attr == &sensor_dev_attr_temp5_max.dev_attr.attr ||
attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
attr == &sensor_dev_attr_temp2_min.dev_attr.attr ||
attr == &sensor_dev_attr_temp3_min.dev_attr.attr ||
attr == &sensor_dev_attr_temp4_min.dev_attr.attr ||
attr == &sensor_dev_attr_temp5_min.dev_attr.attr)
ret |= S_IWUSR;
return ret;
}
static const struct attribute_group max1668_group_common = {
.attrs = max1668_attribute_common,
.is_visible = max1668_attribute_mode
};
static const struct attribute_group max1668_group_unique = {
.attrs = max1668_attribute_unique,
.is_visible = max1668_attribute_mode
}; };
/* Return 0 if detection is successful, -ENODEV otherwise */ /* Return 0 if detection is successful, -ENODEV otherwise */
@ -391,13 +287,8 @@ static int max1668_probe(struct i2c_client *client)
data->channels = (uintptr_t)i2c_get_match_data(client); data->channels = (uintptr_t)i2c_get_match_data(client);
/* sysfs hooks */ hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
data->groups[0] = &max1668_group_common; &max1668_chip_info, NULL);
if (data->channels == 5)
data->groups[1] = &max1668_group_unique;
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
data, data->groups);
return PTR_ERR_OR_ZERO(hwmon_dev); return PTR_ERR_OR_ZERO(hwmon_dev);
} }