1

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/err.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/init.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 regmap *regmap;
const struct attribute_group *groups[3];
int channels;
};
static ssize_t show_temp(struct device *dev,
struct device_attribute *devattr, char *buf)
static int max1668_read(struct device *dev, enum hwmon_sensor_types type,
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 regmap *regmap = data->regmap;
u32 alarm, temp;
u32 regs[2] = { MAX1668_REG_STAT1, MAX1668_REG_TEMP(channel) };
u8 regvals[2];
u32 regval;
int ret;
ret = regmap_read(regmap, MAX1668_REG_STAT1, &alarm);
if (ret)
return ret;
ret = regmap_read(regmap, MAX1668_REG_TEMP(index), &temp);
if (ret)
return ret;
return sprintf(buf, "%u\n", (alarm & BIT(4)) && temp == 127);
switch (attr) {
case hwmon_temp_input:
ret = regmap_read(regmap, MAX1668_REG_TEMP(channel), &regval);
if (ret)
return ret;
*val = sign_extend32(regval, 7) * 1000;
break;
case hwmon_temp_min:
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,
struct device_attribute *devattr,
const char *buf, size_t count)
static int max1668_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
long temp;
int ret;
struct regmap *regmap = data->regmap;
ret = kstrtol(buf, 10, &temp);
if (ret < 0)
return ret;
val = clamp_val(val / 1000, -128, 127);
temp = clamp_val(temp / 1000, -128, 127);
ret = regmap_write(data->regmap, MAX1668_REG_LIMH(index), temp);
if (ret < 0)
count = ret;
return count;
switch (attr) {
case hwmon_temp_min:
return regmap_write(regmap, MAX1668_REG_LIML(channel), val);
case hwmon_temp_max:
return regmap_write(regmap, MAX1668_REG_LIMH(channel), val);
default:
return -EOPNOTSUPP;
}
}
static ssize_t set_temp_min(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count)
static umode_t max1668_is_visible(const void *_data, enum hwmon_sensor_types type,
u32 attr, int channel)
{
int index = to_sensor_dev_attr(devattr)->index;
struct max1668_data *data = dev_get_drvdata(dev);
long temp;
int ret;
const struct max1668_data *data = _data;
ret = kstrtol(buf, 10, &temp);
if (ret < 0)
return ret;
if (channel >= data->channels)
return 0;
temp = clamp_val(temp / 1000, -128, 127);
ret = regmap_write(data->regmap, MAX1668_REG_LIML(index), temp);
if (ret < 0)
count = ret;
return count;
switch (attr) {
case hwmon_temp_min:
case hwmon_temp_max:
return read_only ? 0444 : 0644;
case hwmon_temp_input:
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 SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max,
set_temp_max, 0);
static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min,
set_temp_min, 0);
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max,
set_temp_max, 1);
static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min,
set_temp_min, 1);
static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max,
set_temp_max, 2);
static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min,
set_temp_min, 2);
static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
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,
static const struct hwmon_channel_info * const max1668_info[] = {
HWMON_CHANNEL_INFO(temp,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
HWMON_T_FAULT,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
HWMON_T_FAULT,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
HWMON_T_FAULT,
HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
HWMON_T_FAULT),
NULL
};
/* Attributes not present on MAX1805 */
static struct attribute *max1668_attribute_unique[] = {
&sensor_dev_attr_temp4_max.dev_attr.attr,
&sensor_dev_attr_temp4_min.dev_attr.attr,
&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 const struct hwmon_ops max1668_hwmon_ops = {
.is_visible = max1668_is_visible,
.read = max1668_read,
.write = max1668_write,
};
static umode_t max1668_attribute_mode(struct kobject *kobj,
struct attribute *attr, int index)
{
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
static const struct hwmon_chip_info max1668_chip_info = {
.ops = &max1668_hwmon_ops,
.info = max1668_info,
};
/* 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);
/* sysfs hooks */
data->groups[0] = &max1668_group_common;
if (data->channels == 5)
data->groups[1] = &max1668_group_unique;
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
data, data->groups);
hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
&max1668_chip_info, NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}