diff options
author | Guenter Roeck <linux@roeck-us.net> | 2014-02-17 13:34:29 -0500 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2014-03-03 11:01:06 -0500 |
commit | 7981c5846aa168a28b024c2461b766f16dd8f585 (patch) | |
tree | 385bb490feb3450c05b90e9433636b3ff8462fe8 /drivers/hwmon/max6639.c | |
parent | c1ea0a0431bc63a24bc0a887c94e93c1ecafd668 (diff) |
hwmon: (max6639) Convert to use devm_hwmon_device_register_with_groups
Simplify code, reduce code size, and attach sysfs attricutes to hwmon device.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Jean Delvare <jdelvare@suse.de>
Diffstat (limited to 'drivers/hwmon/max6639.c')
-rw-r--r-- | drivers/hwmon/max6639.c | 81 |
1 files changed, 26 insertions, 55 deletions
diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c index 52646bba748b..0c5bdfeb76bc 100644 --- a/drivers/hwmon/max6639.c +++ b/drivers/hwmon/max6639.c | |||
@@ -80,7 +80,7 @@ static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; | |||
80 | * Client data (each client gets its own) | 80 | * Client data (each client gets its own) |
81 | */ | 81 | */ |
82 | struct max6639_data { | 82 | struct max6639_data { |
83 | struct device *hwmon_dev; | 83 | struct i2c_client *client; |
84 | struct mutex update_lock; | 84 | struct mutex update_lock; |
85 | char valid; /* !=0 if following fields are valid */ | 85 | char valid; /* !=0 if following fields are valid */ |
86 | unsigned long last_updated; /* In jiffies */ | 86 | unsigned long last_updated; /* In jiffies */ |
@@ -104,8 +104,8 @@ struct max6639_data { | |||
104 | 104 | ||
105 | static struct max6639_data *max6639_update_device(struct device *dev) | 105 | static struct max6639_data *max6639_update_device(struct device *dev) |
106 | { | 106 | { |
107 | struct i2c_client *client = to_i2c_client(dev); | 107 | struct max6639_data *data = dev_get_drvdata(dev); |
108 | struct max6639_data *data = i2c_get_clientdata(client); | 108 | struct i2c_client *client = data->client; |
109 | struct max6639_data *ret = data; | 109 | struct max6639_data *ret = data; |
110 | int i; | 110 | int i; |
111 | int status_reg; | 111 | int status_reg; |
@@ -191,9 +191,8 @@ static ssize_t show_temp_fault(struct device *dev, | |||
191 | static ssize_t show_temp_max(struct device *dev, | 191 | static ssize_t show_temp_max(struct device *dev, |
192 | struct device_attribute *dev_attr, char *buf) | 192 | struct device_attribute *dev_attr, char *buf) |
193 | { | 193 | { |
194 | struct i2c_client *client = to_i2c_client(dev); | ||
195 | struct max6639_data *data = i2c_get_clientdata(client); | ||
196 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 194 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
195 | struct max6639_data *data = dev_get_drvdata(dev); | ||
197 | 196 | ||
198 | return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); | 197 | return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); |
199 | } | 198 | } |
@@ -202,9 +201,9 @@ static ssize_t set_temp_max(struct device *dev, | |||
202 | struct device_attribute *dev_attr, | 201 | struct device_attribute *dev_attr, |
203 | const char *buf, size_t count) | 202 | const char *buf, size_t count) |
204 | { | 203 | { |
205 | struct i2c_client *client = to_i2c_client(dev); | ||
206 | struct max6639_data *data = i2c_get_clientdata(client); | ||
207 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 204 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
205 | struct max6639_data *data = dev_get_drvdata(dev); | ||
206 | struct i2c_client *client = data->client; | ||
208 | unsigned long val; | 207 | unsigned long val; |
209 | int res; | 208 | int res; |
210 | 209 | ||
@@ -224,9 +223,8 @@ static ssize_t set_temp_max(struct device *dev, | |||
224 | static ssize_t show_temp_crit(struct device *dev, | 223 | static ssize_t show_temp_crit(struct device *dev, |
225 | struct device_attribute *dev_attr, char *buf) | 224 | struct device_attribute *dev_attr, char *buf) |
226 | { | 225 | { |
227 | struct i2c_client *client = to_i2c_client(dev); | ||
228 | struct max6639_data *data = i2c_get_clientdata(client); | ||
229 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 226 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
227 | struct max6639_data *data = dev_get_drvdata(dev); | ||
230 | 228 | ||
231 | return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); | 229 | return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); |
232 | } | 230 | } |
@@ -235,9 +233,9 @@ static ssize_t set_temp_crit(struct device *dev, | |||
235 | struct device_attribute *dev_attr, | 233 | struct device_attribute *dev_attr, |
236 | const char *buf, size_t count) | 234 | const char *buf, size_t count) |
237 | { | 235 | { |
238 | struct i2c_client *client = to_i2c_client(dev); | ||
239 | struct max6639_data *data = i2c_get_clientdata(client); | ||
240 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 236 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
237 | struct max6639_data *data = dev_get_drvdata(dev); | ||
238 | struct i2c_client *client = data->client; | ||
241 | unsigned long val; | 239 | unsigned long val; |
242 | int res; | 240 | int res; |
243 | 241 | ||
@@ -258,9 +256,8 @@ static ssize_t show_temp_emergency(struct device *dev, | |||
258 | struct device_attribute *dev_attr, | 256 | struct device_attribute *dev_attr, |
259 | char *buf) | 257 | char *buf) |
260 | { | 258 | { |
261 | struct i2c_client *client = to_i2c_client(dev); | ||
262 | struct max6639_data *data = i2c_get_clientdata(client); | ||
263 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 259 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
260 | struct max6639_data *data = dev_get_drvdata(dev); | ||
264 | 261 | ||
265 | return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); | 262 | return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); |
266 | } | 263 | } |
@@ -269,9 +266,9 @@ static ssize_t set_temp_emergency(struct device *dev, | |||
269 | struct device_attribute *dev_attr, | 266 | struct device_attribute *dev_attr, |
270 | const char *buf, size_t count) | 267 | const char *buf, size_t count) |
271 | { | 268 | { |
272 | struct i2c_client *client = to_i2c_client(dev); | ||
273 | struct max6639_data *data = i2c_get_clientdata(client); | ||
274 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 269 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
270 | struct max6639_data *data = dev_get_drvdata(dev); | ||
271 | struct i2c_client *client = data->client; | ||
275 | unsigned long val; | 272 | unsigned long val; |
276 | int res; | 273 | int res; |
277 | 274 | ||
@@ -291,9 +288,8 @@ static ssize_t set_temp_emergency(struct device *dev, | |||
291 | static ssize_t show_pwm(struct device *dev, | 288 | static ssize_t show_pwm(struct device *dev, |
292 | struct device_attribute *dev_attr, char *buf) | 289 | struct device_attribute *dev_attr, char *buf) |
293 | { | 290 | { |
294 | struct i2c_client *client = to_i2c_client(dev); | ||
295 | struct max6639_data *data = i2c_get_clientdata(client); | ||
296 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 291 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
292 | struct max6639_data *data = dev_get_drvdata(dev); | ||
297 | 293 | ||
298 | return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); | 294 | return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); |
299 | } | 295 | } |
@@ -302,9 +298,9 @@ static ssize_t set_pwm(struct device *dev, | |||
302 | struct device_attribute *dev_attr, | 298 | struct device_attribute *dev_attr, |
303 | const char *buf, size_t count) | 299 | const char *buf, size_t count) |
304 | { | 300 | { |
305 | struct i2c_client *client = to_i2c_client(dev); | ||
306 | struct max6639_data *data = i2c_get_clientdata(client); | ||
307 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 301 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
302 | struct max6639_data *data = dev_get_drvdata(dev); | ||
303 | struct i2c_client *client = data->client; | ||
308 | unsigned long val; | 304 | unsigned long val; |
309 | int res; | 305 | int res; |
310 | 306 | ||
@@ -378,7 +374,7 @@ static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 5); | |||
378 | static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4); | 374 | static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4); |
379 | 375 | ||
380 | 376 | ||
381 | static struct attribute *max6639_attributes[] = { | 377 | static struct attribute *max6639_attrs[] = { |
382 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 378 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
383 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 379 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
384 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | 380 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
@@ -403,10 +399,7 @@ static struct attribute *max6639_attributes[] = { | |||
403 | &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, | 399 | &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, |
404 | NULL | 400 | NULL |
405 | }; | 401 | }; |
406 | 402 | ATTRIBUTE_GROUPS(max6639); | |
407 | static const struct attribute_group max6639_group = { | ||
408 | .attrs = max6639_attributes, | ||
409 | }; | ||
410 | 403 | ||
411 | /* | 404 | /* |
412 | * returns respective index in rpm_ranges table | 405 | * returns respective index in rpm_ranges table |
@@ -424,9 +417,9 @@ static int rpm_range_to_reg(int range) | |||
424 | return 1; /* default: 4000 RPM */ | 417 | return 1; /* default: 4000 RPM */ |
425 | } | 418 | } |
426 | 419 | ||
427 | static int max6639_init_client(struct i2c_client *client) | 420 | static int max6639_init_client(struct i2c_client *client, |
421 | struct max6639_data *data) | ||
428 | { | 422 | { |
429 | struct max6639_data *data = i2c_get_clientdata(client); | ||
430 | struct max6639_platform_data *max6639_info = | 423 | struct max6639_platform_data *max6639_info = |
431 | dev_get_platdata(&client->dev); | 424 | dev_get_platdata(&client->dev); |
432 | int i; | 425 | int i; |
@@ -547,46 +540,25 @@ static int max6639_probe(struct i2c_client *client, | |||
547 | { | 540 | { |
548 | struct device *dev = &client->dev; | 541 | struct device *dev = &client->dev; |
549 | struct max6639_data *data; | 542 | struct max6639_data *data; |
543 | struct device *hwmon_dev; | ||
550 | int err; | 544 | int err; |
551 | 545 | ||
552 | data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); | 546 | data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); |
553 | if (!data) | 547 | if (!data) |
554 | return -ENOMEM; | 548 | return -ENOMEM; |
555 | 549 | ||
556 | i2c_set_clientdata(client, data); | 550 | data->client = client; |
557 | mutex_init(&data->update_lock); | 551 | mutex_init(&data->update_lock); |
558 | 552 | ||
559 | /* Initialize the max6639 chip */ | 553 | /* Initialize the max6639 chip */ |
560 | err = max6639_init_client(client); | 554 | err = max6639_init_client(client, data); |
561 | if (err < 0) | 555 | if (err < 0) |
562 | return err; | 556 | return err; |
563 | 557 | ||
564 | /* Register sysfs hooks */ | 558 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
565 | err = sysfs_create_group(&dev->kobj, &max6639_group); | 559 | data, |
566 | if (err) | 560 | max6639_groups); |
567 | return err; | 561 | return PTR_ERR_OR_ZERO(hwmon_dev); |
568 | |||
569 | data->hwmon_dev = hwmon_device_register(dev); | ||
570 | if (IS_ERR(data->hwmon_dev)) { | ||
571 | err = PTR_ERR(data->hwmon_dev); | ||
572 | goto error_remove; | ||
573 | } | ||
574 | |||
575 | return 0; | ||
576 | |||
577 | error_remove: | ||
578 | sysfs_remove_group(&dev->kobj, &max6639_group); | ||
579 | return err; | ||
580 | } | ||
581 | |||
582 | static int max6639_remove(struct i2c_client *client) | ||
583 | { | ||
584 | struct max6639_data *data = i2c_get_clientdata(client); | ||
585 | |||
586 | hwmon_device_unregister(data->hwmon_dev); | ||
587 | sysfs_remove_group(&client->dev.kobj, &max6639_group); | ||
588 | |||
589 | return 0; | ||
590 | } | 562 | } |
591 | 563 | ||
592 | #ifdef CONFIG_PM_SLEEP | 564 | #ifdef CONFIG_PM_SLEEP |
@@ -631,7 +603,6 @@ static struct i2c_driver max6639_driver = { | |||
631 | .pm = &max6639_pm_ops, | 603 | .pm = &max6639_pm_ops, |
632 | }, | 604 | }, |
633 | .probe = max6639_probe, | 605 | .probe = max6639_probe, |
634 | .remove = max6639_remove, | ||
635 | .id_table = max6639_id, | 606 | .id_table = max6639_id, |
636 | .detect = max6639_detect, | 607 | .detect = max6639_detect, |
637 | .address_list = normal_i2c, | 608 | .address_list = normal_i2c, |