diff options
author | Axel Lin <axel.lin@ingics.com> | 2014-06-29 02:45:54 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2014-08-04 10:01:34 -0400 |
commit | a5afc18c9fbae900430b60948b940fbdf734d220 (patch) | |
tree | dbadd048593b2ec7ff9dd807f55d4ab1b9fb2bad /drivers/hwmon/hih6130.c | |
parent | 233adcefee012ae9b3f3e7d16017bce9072fe62d (diff) |
hwmon: (hih6130) Convert to devm_hwmon_device_register_with_groups
Use ATTRIBUTE_GROUPS macro and devm_hwmon_device_register_with_groups() to
simplify the code a bit.
Signed-off-by: Axel Lin <axel.lin@ingics.com>
Tested-by: Iain Paton <ipaton0@gmail.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/hih6130.c')
-rw-r--r-- | drivers/hwmon/hih6130.c | 88 |
1 files changed, 23 insertions, 65 deletions
diff --git a/drivers/hwmon/hih6130.c b/drivers/hwmon/hih6130.c index 7d68a08baaa8..0e01c4e13e33 100644 --- a/drivers/hwmon/hih6130.c +++ b/drivers/hwmon/hih6130.c | |||
@@ -46,7 +46,7 @@ | |||
46 | * @write_length: length for I2C measurement request | 46 | * @write_length: length for I2C measurement request |
47 | */ | 47 | */ |
48 | struct hih6130 { | 48 | struct hih6130 { |
49 | struct device *hwmon_dev; | 49 | struct i2c_client *client; |
50 | struct mutex lock; | 50 | struct mutex lock; |
51 | bool valid; | 51 | bool valid; |
52 | unsigned long last_update; | 52 | unsigned long last_update; |
@@ -62,7 +62,6 @@ struct hih6130 { | |||
62 | */ | 62 | */ |
63 | static inline int hih6130_temp_ticks_to_millicelsius(int ticks) | 63 | static inline int hih6130_temp_ticks_to_millicelsius(int ticks) |
64 | { | 64 | { |
65 | |||
66 | ticks = ticks >> 2; | 65 | ticks = ticks >> 2; |
67 | /* | 66 | /* |
68 | * from data sheet section 5.0 | 67 | * from data sheet section 5.0 |
@@ -78,7 +77,6 @@ static inline int hih6130_temp_ticks_to_millicelsius(int ticks) | |||
78 | */ | 77 | */ |
79 | static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) | 78 | static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) |
80 | { | 79 | { |
81 | |||
82 | ticks &= ~0xC000; /* clear status bits */ | 80 | ticks &= ~0xC000; /* clear status bits */ |
83 | /* | 81 | /* |
84 | * from data sheet section 4.0 | 82 | * from data sheet section 4.0 |
@@ -89,15 +87,16 @@ static inline int hih6130_rh_ticks_to_per_cent_mille(int ticks) | |||
89 | 87 | ||
90 | /** | 88 | /** |
91 | * hih6130_update_measurements() - get updated measurements from device | 89 | * hih6130_update_measurements() - get updated measurements from device |
92 | * @client: I2C client device | 90 | * @dev: device |
93 | * | 91 | * |
94 | * Returns 0 on success, else negative errno. | 92 | * Returns 0 on success, else negative errno. |
95 | */ | 93 | */ |
96 | static int hih6130_update_measurements(struct i2c_client *client) | 94 | static int hih6130_update_measurements(struct device *dev) |
97 | { | 95 | { |
96 | struct hih6130 *hih6130 = dev_get_drvdata(dev); | ||
97 | struct i2c_client *client = hih6130->client; | ||
98 | int ret = 0; | 98 | int ret = 0; |
99 | int t; | 99 | int t; |
100 | struct hih6130 *hih6130 = i2c_get_clientdata(client); | ||
101 | unsigned char tmp[4]; | 100 | unsigned char tmp[4]; |
102 | struct i2c_msg msgs[1] = { | 101 | struct i2c_msg msgs[1] = { |
103 | { | 102 | { |
@@ -176,9 +175,10 @@ static ssize_t hih6130_show_temperature(struct device *dev, | |||
176 | struct device_attribute *attr, | 175 | struct device_attribute *attr, |
177 | char *buf) | 176 | char *buf) |
178 | { | 177 | { |
179 | struct i2c_client *client = to_i2c_client(dev); | 178 | struct hih6130 *hih6130 = dev_get_drvdata(dev); |
180 | struct hih6130 *hih6130 = i2c_get_clientdata(client); | 179 | int ret; |
181 | int ret = hih6130_update_measurements(client); | 180 | |
181 | ret = hih6130_update_measurements(dev); | ||
182 | if (ret < 0) | 182 | if (ret < 0) |
183 | return ret; | 183 | return ret; |
184 | return sprintf(buf, "%d\n", hih6130->temperature); | 184 | return sprintf(buf, "%d\n", hih6130->temperature); |
@@ -196,9 +196,10 @@ static ssize_t hih6130_show_temperature(struct device *dev, | |||
196 | static ssize_t hih6130_show_humidity(struct device *dev, | 196 | static ssize_t hih6130_show_humidity(struct device *dev, |
197 | struct device_attribute *attr, char *buf) | 197 | struct device_attribute *attr, char *buf) |
198 | { | 198 | { |
199 | struct i2c_client *client = to_i2c_client(dev); | 199 | struct hih6130 *hih6130 = dev_get_drvdata(dev); |
200 | struct hih6130 *hih6130 = i2c_get_clientdata(client); | 200 | int ret; |
201 | int ret = hih6130_update_measurements(client); | 201 | |
202 | ret = hih6130_update_measurements(dev); | ||
202 | if (ret < 0) | 203 | if (ret < 0) |
203 | return ret; | 204 | return ret; |
204 | return sprintf(buf, "%d\n", hih6130->humidity); | 205 | return sprintf(buf, "%d\n", hih6130->humidity); |
@@ -210,79 +211,37 @@ static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, hih6130_show_temperature, | |||
210 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity, | 211 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, hih6130_show_humidity, |
211 | NULL, 0); | 212 | NULL, 0); |
212 | 213 | ||
213 | static struct attribute *hih6130_attributes[] = { | 214 | static struct attribute *hih6130_attrs[] = { |
214 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 215 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
215 | &sensor_dev_attr_humidity1_input.dev_attr.attr, | 216 | &sensor_dev_attr_humidity1_input.dev_attr.attr, |
216 | NULL | 217 | NULL |
217 | }; | 218 | }; |
218 | 219 | ||
219 | static const struct attribute_group hih6130_attr_group = { | 220 | ATTRIBUTE_GROUPS(hih6130); |
220 | .attrs = hih6130_attributes, | ||
221 | }; | ||
222 | 221 | ||
223 | /** | ||
224 | * hih6130_probe() - probe device | ||
225 | * @client: I2C client device | ||
226 | * @id: device ID | ||
227 | * | ||
228 | * Called by the I2C core when an entry in the ID table matches a | ||
229 | * device's name. | ||
230 | * Returns 0 on success. | ||
231 | */ | ||
232 | static int hih6130_probe(struct i2c_client *client, | 222 | static int hih6130_probe(struct i2c_client *client, |
233 | const struct i2c_device_id *id) | 223 | const struct i2c_device_id *id) |
234 | { | 224 | { |
225 | struct device *dev = &client->dev; | ||
235 | struct hih6130 *hih6130; | 226 | struct hih6130 *hih6130; |
236 | int err; | 227 | struct device *hwmon_dev; |
237 | 228 | ||
238 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | 229 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
239 | dev_err(&client->dev, "adapter does not support true I2C\n"); | 230 | dev_err(&client->dev, "adapter does not support true I2C\n"); |
240 | return -ENODEV; | 231 | return -ENODEV; |
241 | } | 232 | } |
242 | 233 | ||
243 | hih6130 = devm_kzalloc(&client->dev, sizeof(*hih6130), GFP_KERNEL); | 234 | hih6130 = devm_kzalloc(dev, sizeof(*hih6130), GFP_KERNEL); |
244 | if (!hih6130) | 235 | if (!hih6130) |
245 | return -ENOMEM; | 236 | return -ENOMEM; |
246 | 237 | ||
247 | i2c_set_clientdata(client, hih6130); | 238 | hih6130->client = client; |
248 | |||
249 | mutex_init(&hih6130->lock); | 239 | mutex_init(&hih6130->lock); |
250 | 240 | ||
251 | err = sysfs_create_group(&client->dev.kobj, &hih6130_attr_group); | 241 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
252 | if (err) { | 242 | hih6130, |
253 | dev_dbg(&client->dev, "could not create sysfs files\n"); | 243 | hih6130_groups); |
254 | return err; | 244 | return PTR_ERR_OR_ZERO(hwmon_dev); |
255 | } | ||
256 | |||
257 | hih6130->hwmon_dev = hwmon_device_register(&client->dev); | ||
258 | if (IS_ERR(hih6130->hwmon_dev)) { | ||
259 | dev_dbg(&client->dev, "unable to register hwmon device\n"); | ||
260 | err = PTR_ERR(hih6130->hwmon_dev); | ||
261 | goto fail_remove_sysfs; | ||
262 | } | ||
263 | |||
264 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK)) | ||
265 | hih6130->write_length = 1; | ||
266 | |||
267 | return 0; | ||
268 | |||
269 | fail_remove_sysfs: | ||
270 | sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group); | ||
271 | return err; | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * hih6130_remove() - remove device | ||
276 | * @client: I2C client device | ||
277 | */ | ||
278 | static int hih6130_remove(struct i2c_client *client) | ||
279 | { | ||
280 | struct hih6130 *hih6130 = i2c_get_clientdata(client); | ||
281 | |||
282 | hwmon_device_unregister(hih6130->hwmon_dev); | ||
283 | sysfs_remove_group(&client->dev.kobj, &hih6130_attr_group); | ||
284 | |||
285 | return 0; | ||
286 | } | 245 | } |
287 | 246 | ||
288 | /* Device ID table */ | 247 | /* Device ID table */ |
@@ -295,7 +254,6 @@ MODULE_DEVICE_TABLE(i2c, hih6130_id); | |||
295 | static struct i2c_driver hih6130_driver = { | 254 | static struct i2c_driver hih6130_driver = { |
296 | .driver.name = "hih6130", | 255 | .driver.name = "hih6130", |
297 | .probe = hih6130_probe, | 256 | .probe = hih6130_probe, |
298 | .remove = hih6130_remove, | ||
299 | .id_table = hih6130_id, | 257 | .id_table = hih6130_id, |
300 | }; | 258 | }; |
301 | 259 | ||