Commit 0208531d authored by Guenter Roeck's avatar Guenter Roeck

hwmon: (tmp102) Convert to use new hwmon registration API

Simplify code and reduce code size by using the new hwmon
registration API.
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent eb1c8f43
...@@ -1571,7 +1571,6 @@ config SENSORS_THMC50 ...@@ -1571,7 +1571,6 @@ config SENSORS_THMC50
config SENSORS_TMP102 config SENSORS_TMP102
tristate "Texas Instruments TMP102" tristate "Texas Instruments TMP102"
depends on I2C depends on I2C
depends on THERMAL || !THERMAL_OF
select REGMAP_I2C select REGMAP_I2C
help help
If you say yes here you get support for Texas Instruments TMP102 If you say yes here you get support for Texas Instruments TMP102
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/regmap.h> #include <linux/regmap.h>
#include <linux/thermal.h>
#include <linux/of.h> #include <linux/of.h>
#define DRIVER_NAME "tmp102" #define DRIVER_NAME "tmp102"
...@@ -79,84 +78,113 @@ static inline u16 tmp102_mC_to_reg(int val) ...@@ -79,84 +78,113 @@ static inline u16 tmp102_mC_to_reg(int val)
return (val * 128) / 1000; return (val * 128) / 1000;
} }
static int tmp102_read_temp(void *dev, int *temp) static int tmp102_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *temp)
{ {
struct tmp102 *tmp102 = dev_get_drvdata(dev); struct tmp102 *tmp102 = dev_get_drvdata(dev);
unsigned int reg; unsigned int regval;
int ret; int err, reg;
if (time_before(jiffies, tmp102->ready_time)) { switch (attr) {
dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__); case hwmon_temp_input:
return -EAGAIN; /* Is it too early to return a conversion ? */
if (time_before(jiffies, tmp102->ready_time)) {
dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
return -EAGAIN;
}
reg = TMP102_TEMP_REG;
break;
case hwmon_temp_max_hyst:
reg = TMP102_TLOW_REG;
break;
case hwmon_temp_max:
reg = TMP102_THIGH_REG;
break;
default:
return -EOPNOTSUPP;
} }
ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, &reg); err = regmap_read(tmp102->regmap, reg, &regval);
if (ret < 0) if (err < 0)
return ret; return err;
*temp = tmp102_reg_to_mC(regval);
*temp = tmp102_reg_to_mC(reg);
return 0; return 0;
} }
static ssize_t tmp102_show_temp(struct device *dev, static int tmp102_write(struct device *dev, enum hwmon_sensor_types type,
struct device_attribute *attr, u32 attr, int channel, long temp)
char *buf)
{ {
struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
struct tmp102 *tmp102 = dev_get_drvdata(dev); struct tmp102 *tmp102 = dev_get_drvdata(dev);
int regaddr = sda->index; int reg;
unsigned int reg;
int err; switch (attr) {
case hwmon_temp_max_hyst:
if (regaddr == TMP102_TEMP_REG && reg = TMP102_TLOW_REG;
time_before(jiffies, tmp102->ready_time)) break;
return -EAGAIN; case hwmon_temp_max:
reg = TMP102_THIGH_REG;
err = regmap_read(tmp102->regmap, regaddr, &reg); break;
if (err < 0) default:
return err; return -EOPNOTSUPP;
}
return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg)); temp = clamp_val(temp, -256000, 255000);
return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(temp));
} }
static ssize_t tmp102_set_temp(struct device *dev, static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type,
struct device_attribute *attr, u32 attr, int channel)
const char *buf, size_t count)
{ {
struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); if (type != hwmon_temp)
struct tmp102 *tmp102 = dev_get_drvdata(dev); return 0;
int reg = sda->index;
long val; switch (attr) {
int err; case hwmon_temp_input:
return S_IRUGO;
if (kstrtol(buf, 10, &val) < 0) case hwmon_temp_max_hyst:
return -EINVAL; case hwmon_temp_max:
val = clamp_val(val, -256000, 255000); return S_IRUGO | S_IWUSR;
default:
err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val)); return 0;
return err ? : count; }
} }
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL, static u32 tmp102_chip_config[] = {
TMP102_TEMP_REG); HWMON_C_REGISTER_TZ,
0
};
static const struct hwmon_channel_info tmp102_chip = {
.type = hwmon_chip,
.config = tmp102_chip_config,
};
static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, static u32 tmp102_temp_config[] = {
tmp102_set_temp, TMP102_TLOW_REG); HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
0
};
static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, static const struct hwmon_channel_info tmp102_temp = {
tmp102_set_temp, TMP102_THIGH_REG); .type = hwmon_temp,
.config = tmp102_temp_config,
};
static struct attribute *tmp102_attrs[] = { static const struct hwmon_channel_info *tmp102_info[] = {
&sensor_dev_attr_temp1_input.dev_attr.attr, &tmp102_chip,
&sensor_dev_attr_temp1_max_hyst.dev_attr.attr, &tmp102_temp,
&sensor_dev_attr_temp1_max.dev_attr.attr,
NULL NULL
}; };
ATTRIBUTE_GROUPS(tmp102);
static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = { static const struct hwmon_ops tmp102_hwmon_ops = {
.get_temp = tmp102_read_temp, .is_visible = tmp102_is_visible,
.read = tmp102_read,
.write = tmp102_write,
};
static const struct hwmon_chip_info tmp102_chip_info = {
.ops = &tmp102_hwmon_ops,
.info = tmp102_info,
}; };
static void tmp102_restore_config(void *data) static void tmp102_restore_config(void *data)
...@@ -188,7 +216,7 @@ static const struct regmap_config tmp102_regmap_config = { ...@@ -188,7 +216,7 @@ static const struct regmap_config tmp102_regmap_config = {
}; };
static int tmp102_probe(struct i2c_client *client, static int tmp102_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct device *dev = &client->dev; struct device *dev = &client->dev;
struct device *hwmon_dev; struct device *hwmon_dev;
...@@ -249,16 +277,14 @@ static int tmp102_probe(struct i2c_client *client, ...@@ -249,16 +277,14 @@ static int tmp102_probe(struct i2c_client *client,
tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS); tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
} }
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
tmp102, tmp102,
tmp102_groups); &tmp102_chip_info,
NULL);
if (IS_ERR(hwmon_dev)) { if (IS_ERR(hwmon_dev)) {
dev_dbg(dev, "unable to register hwmon device\n"); dev_dbg(dev, "unable to register hwmon device\n");
return PTR_ERR(hwmon_dev); return PTR_ERR(hwmon_dev);
} }
devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
&tmp102_of_thermal_ops);
dev_info(dev, "initialized\n"); dev_info(dev, "initialized\n");
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment