diff options
author | Guenter Roeck <linux@roeck-us.net> | 2016-06-19 22:15:05 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2016-09-09 00:34:17 -0400 |
commit | 08b024338166abebcc2216f97693336f7ac0bf42 (patch) | |
tree | 9517c56cdf8f35abf90ae6094b945312ee3b5bea /drivers/hwmon | |
parent | 2ca492e22cb70a001749377506bd22eb06f60ecc (diff) |
hwmon: (lm75) Convert to use new hwmon registration API
Simplify code and reduce code size by using the new hwmon
registration API.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r-- | drivers/hwmon/Kconfig | 1 | ||||
-rw-r--r-- | drivers/hwmon/lm75.c | 185 |
2 files changed, 116 insertions, 70 deletions
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index 655a462f3622..fcdaf6773238 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig | |||
@@ -969,7 +969,6 @@ config SENSORS_LM73 | |||
969 | config SENSORS_LM75 | 969 | config SENSORS_LM75 |
970 | tristate "National Semiconductor LM75 and compatibles" | 970 | tristate "National Semiconductor LM75 and compatibles" |
971 | depends on I2C | 971 | depends on I2C |
972 | depends on THERMAL || !THERMAL_OF | ||
973 | select REGMAP_I2C | 972 | select REGMAP_I2C |
974 | help | 973 | help |
975 | If you say yes here you get support for one common type of | 974 | If you say yes here you get support for one common type of |
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c index 92f9d4bbf597..eff3b24d8473 100644 --- a/drivers/hwmon/lm75.c +++ b/drivers/hwmon/lm75.c | |||
@@ -28,7 +28,6 @@ | |||
28 | #include <linux/err.h> | 28 | #include <linux/err.h> |
29 | #include <linux/of.h> | 29 | #include <linux/of.h> |
30 | #include <linux/regmap.h> | 30 | #include <linux/regmap.h> |
31 | #include <linux/thermal.h> | ||
32 | #include "lm75.h" | 31 | #include "lm75.h" |
33 | 32 | ||
34 | 33 | ||
@@ -88,56 +87,75 @@ static inline long lm75_reg_to_mc(s16 temp, u8 resolution) | |||
88 | return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8); | 87 | return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8); |
89 | } | 88 | } |
90 | 89 | ||
91 | /* sysfs attributes for hwmon */ | 90 | static int lm75_read(struct device *dev, enum hwmon_sensor_types type, |
92 | 91 | u32 attr, int channel, long *val) | |
93 | static int lm75_read_temp(void *dev, int *temp) | ||
94 | { | 92 | { |
95 | struct lm75_data *data = dev_get_drvdata(dev); | 93 | struct lm75_data *data = dev_get_drvdata(dev); |
96 | unsigned int _temp; | 94 | unsigned int regval; |
97 | int err; | 95 | int err, reg; |
98 | 96 | ||
99 | err = regmap_read(data->regmap, LM75_REG_TEMP, &_temp); | 97 | switch (type) { |
100 | if (err < 0) | 98 | case hwmon_chip: |
101 | return err; | 99 | switch (attr) { |
102 | 100 | case hwmon_chip_update_interval: | |
103 | *temp = lm75_reg_to_mc(_temp, data->resolution); | 101 | *val = data->sample_time; |
104 | 102 | break;; | |
103 | default: | ||
104 | return -EINVAL; | ||
105 | } | ||
106 | break; | ||
107 | case hwmon_temp: | ||
108 | switch (attr) { | ||
109 | case hwmon_temp_input: | ||
110 | reg = LM75_REG_TEMP; | ||
111 | break; | ||
112 | case hwmon_temp_max: | ||
113 | reg = LM75_REG_MAX; | ||
114 | break; | ||
115 | case hwmon_temp_max_hyst: | ||
116 | reg = LM75_REG_HYST; | ||
117 | break; | ||
118 | default: | ||
119 | return -EINVAL; | ||
120 | } | ||
121 | err = regmap_read(data->regmap, reg, ®val); | ||
122 | if (err < 0) | ||
123 | return err; | ||
124 | |||
125 | *val = lm75_reg_to_mc(regval, data->resolution); | ||
126 | break; | ||
127 | default: | ||
128 | return -EINVAL; | ||
129 | } | ||
105 | return 0; | 130 | return 0; |
106 | } | 131 | } |
107 | 132 | ||
108 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, | 133 | static int lm75_write(struct device *dev, enum hwmon_sensor_types type, |
109 | char *buf) | 134 | u32 attr, int channel, long temp) |
110 | { | 135 | { |
111 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
112 | struct lm75_data *data = dev_get_drvdata(dev); | 136 | struct lm75_data *data = dev_get_drvdata(dev); |
113 | unsigned int temp = 0; | ||
114 | int err; | ||
115 | |||
116 | err = regmap_read(data->regmap, attr->index, &temp); | ||
117 | if (err < 0) | ||
118 | return err; | ||
119 | |||
120 | return sprintf(buf, "%ld\n", lm75_reg_to_mc(temp, data->resolution)); | ||
121 | } | ||
122 | |||
123 | static ssize_t set_temp(struct device *dev, struct device_attribute *da, | ||
124 | const char *buf, size_t count) | ||
125 | { | ||
126 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | ||
127 | struct lm75_data *data = dev_get_drvdata(dev); | ||
128 | long temp; | ||
129 | int error; | ||
130 | u8 resolution; | 137 | u8 resolution; |
138 | int reg; | ||
139 | |||
140 | if (type != hwmon_temp) | ||
141 | return -EINVAL; | ||
131 | 142 | ||
132 | error = kstrtol(buf, 10, &temp); | 143 | switch (attr) { |
133 | if (error) | 144 | case hwmon_temp_max: |
134 | return error; | 145 | reg = LM75_REG_MAX; |
146 | break; | ||
147 | case hwmon_temp_max_hyst: | ||
148 | reg = LM75_REG_HYST; | ||
149 | break; | ||
150 | default: | ||
151 | return -EINVAL; | ||
152 | } | ||
135 | 153 | ||
136 | /* | 154 | /* |
137 | * Resolution of limit registers is assumed to be the same as the | 155 | * Resolution of limit registers is assumed to be the same as the |
138 | * temperature input register resolution unless given explicitly. | 156 | * temperature input register resolution unless given explicitly. |
139 | */ | 157 | */ |
140 | if (attr->index && data->resolution_limits) | 158 | if (data->resolution_limits) |
141 | resolution = data->resolution_limits; | 159 | resolution = data->resolution_limits; |
142 | else | 160 | else |
143 | resolution = data->resolution; | 161 | resolution = data->resolution; |
@@ -145,45 +163,77 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da, | |||
145 | temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); | 163 | temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX); |
146 | temp = DIV_ROUND_CLOSEST(temp << (resolution - 8), | 164 | temp = DIV_ROUND_CLOSEST(temp << (resolution - 8), |
147 | 1000) << (16 - resolution); | 165 | 1000) << (16 - resolution); |
148 | error = regmap_write(data->regmap, attr->index, temp); | ||
149 | if (error < 0) | ||
150 | return error; | ||
151 | 166 | ||
152 | return count; | 167 | return regmap_write(data->regmap, reg, temp); |
153 | } | 168 | } |
154 | 169 | ||
155 | static ssize_t show_update_interval(struct device *dev, | 170 | static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type, |
156 | struct device_attribute *da, char *buf) | 171 | u32 attr, int channel) |
157 | { | 172 | { |
158 | struct lm75_data *data = dev_get_drvdata(dev); | 173 | switch (type) { |
159 | 174 | case hwmon_chip: | |
160 | return sprintf(buf, "%u\n", data->sample_time); | 175 | switch (attr) { |
176 | case hwmon_chip_update_interval: | ||
177 | return S_IRUGO; | ||
178 | } | ||
179 | break; | ||
180 | case hwmon_temp: | ||
181 | switch (attr) { | ||
182 | case hwmon_temp_input: | ||
183 | return S_IRUGO; | ||
184 | case hwmon_temp_max: | ||
185 | case hwmon_temp_max_hyst: | ||
186 | return S_IRUGO | S_IWUSR; | ||
187 | } | ||
188 | break; | ||
189 | default: | ||
190 | break; | ||
191 | } | ||
192 | return 0; | ||
161 | } | 193 | } |
162 | 194 | ||
163 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, | 195 | /*-----------------------------------------------------------------------*/ |
164 | show_temp, set_temp, LM75_REG_MAX); | ||
165 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, | ||
166 | show_temp, set_temp, LM75_REG_HYST); | ||
167 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, LM75_REG_TEMP); | ||
168 | static DEVICE_ATTR(update_interval, S_IRUGO, show_update_interval, NULL); | ||
169 | 196 | ||
170 | static struct attribute *lm75_attrs[] = { | 197 | /* device probe and removal */ |
171 | &sensor_dev_attr_temp1_input.dev_attr.attr, | ||
172 | &sensor_dev_attr_temp1_max.dev_attr.attr, | ||
173 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, | ||
174 | &dev_attr_update_interval.attr, | ||
175 | 198 | ||
176 | NULL | 199 | /* chip configuration */ |
200 | |||
201 | static const u32 lm75_chip_config[] = { | ||
202 | HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL, | ||
203 | 0 | ||
177 | }; | 204 | }; |
178 | ATTRIBUTE_GROUPS(lm75); | ||
179 | 205 | ||
180 | static const struct thermal_zone_of_device_ops lm75_of_thermal_ops = { | 206 | static const struct hwmon_channel_info lm75_chip = { |
181 | .get_temp = lm75_read_temp, | 207 | .type = hwmon_chip, |
208 | .config = lm75_chip_config, | ||
182 | }; | 209 | }; |
183 | 210 | ||
184 | /*-----------------------------------------------------------------------*/ | 211 | static const u32 lm75_temp_config[] = { |
212 | HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST, | ||
213 | 0 | ||
214 | }; | ||
185 | 215 | ||
186 | /* device probe and removal */ | 216 | static const struct hwmon_channel_info lm75_temp = { |
217 | .type = hwmon_temp, | ||
218 | .config = lm75_temp_config, | ||
219 | }; | ||
220 | |||
221 | static const struct hwmon_channel_info *lm75_info[] = { | ||
222 | &lm75_chip, | ||
223 | &lm75_temp, | ||
224 | NULL | ||
225 | }; | ||
226 | |||
227 | static const struct hwmon_ops lm75_hwmon_ops = { | ||
228 | .is_visible = lm75_is_visible, | ||
229 | .read = lm75_read, | ||
230 | .write = lm75_write, | ||
231 | }; | ||
232 | |||
233 | static const struct hwmon_chip_info lm75_chip_info = { | ||
234 | .ops = &lm75_hwmon_ops, | ||
235 | .info = lm75_info, | ||
236 | }; | ||
187 | 237 | ||
188 | static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg) | 238 | static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg) |
189 | { | 239 | { |
@@ -337,15 +387,12 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) | |||
337 | 387 | ||
338 | dev_dbg(dev, "Config %02x\n", new); | 388 | dev_dbg(dev, "Config %02x\n", new); |
339 | 389 | ||
340 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | 390 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, |
341 | data, lm75_groups); | 391 | data, &lm75_chip_info, |
392 | NULL); | ||
342 | if (IS_ERR(hwmon_dev)) | 393 | if (IS_ERR(hwmon_dev)) |
343 | return PTR_ERR(hwmon_dev); | 394 | return PTR_ERR(hwmon_dev); |
344 | 395 | ||
345 | devm_thermal_zone_of_sensor_register(hwmon_dev, 0, | ||
346 | hwmon_dev, | ||
347 | &lm75_of_thermal_ops); | ||
348 | |||
349 | dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name); | 396 | dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name); |
350 | 397 | ||
351 | return 0; | 398 | return 0; |