diff options
author | Guenter Roeck <linux@roeck-us.net> | 2014-04-04 12:01:34 -0400 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2014-04-04 12:01:34 -0400 |
commit | 118c9a61f158c8f2e3694ed2a3681c217328a2c1 (patch) | |
tree | b93075f013edc670ce0e6705257a54fe92486719 /drivers/hwmon | |
parent | c669ec8039e22fbbf0ac291d1c38eedf0a9b4b7a (diff) |
hwmon: (lm80) Convert to use devm_hwmon_device_register_with_groups
Simplify code, reduce code size, and attach hwmon attributes to
hwmon device.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Jean Delvare <jdelvare@suse.de>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r-- | drivers/hwmon/lm80.c | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/drivers/hwmon/lm80.c b/drivers/hwmon/lm80.c index eba89aac3ece..bd0a1ebbf867 100644 --- a/drivers/hwmon/lm80.c +++ b/drivers/hwmon/lm80.c | |||
@@ -112,7 +112,7 @@ static inline long TEMP_FROM_REG(u16 temp) | |||
112 | */ | 112 | */ |
113 | 113 | ||
114 | struct lm80_data { | 114 | struct lm80_data { |
115 | struct device *hwmon_dev; | 115 | struct i2c_client *client; |
116 | struct mutex update_lock; | 116 | struct mutex update_lock; |
117 | char error; /* !=0 if error occurred during last update */ | 117 | char error; /* !=0 if error occurred during last update */ |
118 | char valid; /* !=0 if following fields are valid */ | 118 | char valid; /* !=0 if following fields are valid */ |
@@ -140,7 +140,6 @@ static int lm80_probe(struct i2c_client *client, | |||
140 | const struct i2c_device_id *id); | 140 | const struct i2c_device_id *id); |
141 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info); | 141 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info); |
142 | static void lm80_init_client(struct i2c_client *client); | 142 | static void lm80_init_client(struct i2c_client *client); |
143 | static int lm80_remove(struct i2c_client *client); | ||
144 | static struct lm80_data *lm80_update_device(struct device *dev); | 143 | static struct lm80_data *lm80_update_device(struct device *dev); |
145 | static int lm80_read_value(struct i2c_client *client, u8 reg); | 144 | static int lm80_read_value(struct i2c_client *client, u8 reg); |
146 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); | 145 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); |
@@ -162,7 +161,6 @@ static struct i2c_driver lm80_driver = { | |||
162 | .name = "lm80", | 161 | .name = "lm80", |
163 | }, | 162 | }, |
164 | .probe = lm80_probe, | 163 | .probe = lm80_probe, |
165 | .remove = lm80_remove, | ||
166 | .id_table = lm80_id, | 164 | .id_table = lm80_id, |
167 | .detect = lm80_detect, | 165 | .detect = lm80_detect, |
168 | .address_list = normal_i2c, | 166 | .address_list = normal_i2c, |
@@ -191,8 +189,8 @@ static ssize_t set_in_##suffix(struct device *dev, \ | |||
191 | struct device_attribute *attr, const char *buf, size_t count) \ | 189 | struct device_attribute *attr, const char *buf, size_t count) \ |
192 | { \ | 190 | { \ |
193 | int nr = to_sensor_dev_attr(attr)->index; \ | 191 | int nr = to_sensor_dev_attr(attr)->index; \ |
194 | struct i2c_client *client = to_i2c_client(dev); \ | 192 | struct lm80_data *data = dev_get_drvdata(dev); \ |
195 | struct lm80_data *data = i2c_get_clientdata(client); \ | 193 | struct i2c_client *client = data->client; \ |
196 | long val; \ | 194 | long val; \ |
197 | int err = kstrtol(buf, 10, &val); \ | 195 | int err = kstrtol(buf, 10, &val); \ |
198 | if (err < 0) \ | 196 | if (err < 0) \ |
@@ -235,8 +233,8 @@ static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, | |||
235 | const char *buf, size_t count) | 233 | const char *buf, size_t count) |
236 | { | 234 | { |
237 | int nr = to_sensor_dev_attr(attr)->index; | 235 | int nr = to_sensor_dev_attr(attr)->index; |
238 | struct i2c_client *client = to_i2c_client(dev); | 236 | struct lm80_data *data = dev_get_drvdata(dev); |
239 | struct lm80_data *data = i2c_get_clientdata(client); | 237 | struct i2c_client *client = data->client; |
240 | unsigned long val; | 238 | unsigned long val; |
241 | int err = kstrtoul(buf, 10, &val); | 239 | int err = kstrtoul(buf, 10, &val); |
242 | if (err < 0) | 240 | if (err < 0) |
@@ -259,8 +257,8 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, | |||
259 | const char *buf, size_t count) | 257 | const char *buf, size_t count) |
260 | { | 258 | { |
261 | int nr = to_sensor_dev_attr(attr)->index; | 259 | int nr = to_sensor_dev_attr(attr)->index; |
262 | struct i2c_client *client = to_i2c_client(dev); | 260 | struct lm80_data *data = dev_get_drvdata(dev); |
263 | struct lm80_data *data = i2c_get_clientdata(client); | 261 | struct i2c_client *client = data->client; |
264 | unsigned long min, val; | 262 | unsigned long min, val; |
265 | u8 reg; | 263 | u8 reg; |
266 | int err = kstrtoul(buf, 10, &val); | 264 | int err = kstrtoul(buf, 10, &val); |
@@ -286,7 +284,7 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, | |||
286 | data->fan_div[nr] = 3; | 284 | data->fan_div[nr] = 3; |
287 | break; | 285 | break; |
288 | default: | 286 | default: |
289 | dev_err(&client->dev, | 287 | dev_err(dev, |
290 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", | 288 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", |
291 | val); | 289 | val); |
292 | mutex_unlock(&data->update_lock); | 290 | mutex_unlock(&data->update_lock); |
@@ -332,8 +330,8 @@ show_temp(os_hyst, temp_os_hyst); | |||
332 | static ssize_t set_temp_##suffix(struct device *dev, \ | 330 | static ssize_t set_temp_##suffix(struct device *dev, \ |
333 | struct device_attribute *attr, const char *buf, size_t count) \ | 331 | struct device_attribute *attr, const char *buf, size_t count) \ |
334 | { \ | 332 | { \ |
335 | struct i2c_client *client = to_i2c_client(dev); \ | 333 | struct lm80_data *data = dev_get_drvdata(dev); \ |
336 | struct lm80_data *data = i2c_get_clientdata(client); \ | 334 | struct i2c_client *client = data->client; \ |
337 | long val; \ | 335 | long val; \ |
338 | int err = kstrtol(buf, 10, &val); \ | 336 | int err = kstrtol(buf, 10, &val); \ |
339 | if (err < 0) \ | 337 | if (err < 0) \ |
@@ -440,7 +438,7 @@ static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13); | |||
440 | * Real code | 438 | * Real code |
441 | */ | 439 | */ |
442 | 440 | ||
443 | static struct attribute *lm80_attributes[] = { | 441 | static struct attribute *lm80_attrs[] = { |
444 | &sensor_dev_attr_in0_min.dev_attr.attr, | 442 | &sensor_dev_attr_in0_min.dev_attr.attr, |
445 | &sensor_dev_attr_in1_min.dev_attr.attr, | 443 | &sensor_dev_attr_in1_min.dev_attr.attr, |
446 | &sensor_dev_attr_in2_min.dev_attr.attr, | 444 | &sensor_dev_attr_in2_min.dev_attr.attr, |
@@ -487,10 +485,7 @@ static struct attribute *lm80_attributes[] = { | |||
487 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 485 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
488 | NULL | 486 | NULL |
489 | }; | 487 | }; |
490 | 488 | ATTRIBUTE_GROUPS(lm80); | |
491 | static const struct attribute_group lm80_group = { | ||
492 | .attrs = lm80_attributes, | ||
493 | }; | ||
494 | 489 | ||
495 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 490 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
496 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) | 491 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) |
@@ -541,14 +536,15 @@ static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) | |||
541 | static int lm80_probe(struct i2c_client *client, | 536 | static int lm80_probe(struct i2c_client *client, |
542 | const struct i2c_device_id *id) | 537 | const struct i2c_device_id *id) |
543 | { | 538 | { |
539 | struct device *dev = &client->dev; | ||
540 | struct device *hwmon_dev; | ||
544 | struct lm80_data *data; | 541 | struct lm80_data *data; |
545 | int err; | ||
546 | 542 | ||
547 | data = devm_kzalloc(&client->dev, sizeof(struct lm80_data), GFP_KERNEL); | 543 | data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL); |
548 | if (!data) | 544 | if (!data) |
549 | return -ENOMEM; | 545 | return -ENOMEM; |
550 | 546 | ||
551 | i2c_set_clientdata(client, data); | 547 | data->client = client; |
552 | mutex_init(&data->update_lock); | 548 | mutex_init(&data->update_lock); |
553 | 549 | ||
554 | /* Initialize the LM80 chip */ | 550 | /* Initialize the LM80 chip */ |
@@ -558,32 +554,10 @@ static int lm80_probe(struct i2c_client *client, | |||
558 | data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); | 554 | data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
559 | data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); | 555 | data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
560 | 556 | ||
561 | /* Register sysfs hooks */ | 557 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
562 | err = sysfs_create_group(&client->dev.kobj, &lm80_group); | 558 | data, lm80_groups); |
563 | if (err) | ||
564 | return err; | ||
565 | |||
566 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
567 | if (IS_ERR(data->hwmon_dev)) { | ||
568 | err = PTR_ERR(data->hwmon_dev); | ||
569 | goto error_remove; | ||
570 | } | ||
571 | 559 | ||
572 | return 0; | 560 | return PTR_ERR_OR_ZERO(hwmon_dev); |
573 | |||
574 | error_remove: | ||
575 | sysfs_remove_group(&client->dev.kobj, &lm80_group); | ||
576 | return err; | ||
577 | } | ||
578 | |||
579 | static int lm80_remove(struct i2c_client *client) | ||
580 | { | ||
581 | struct lm80_data *data = i2c_get_clientdata(client); | ||
582 | |||
583 | hwmon_device_unregister(data->hwmon_dev); | ||
584 | sysfs_remove_group(&client->dev.kobj, &lm80_group); | ||
585 | |||
586 | return 0; | ||
587 | } | 561 | } |
588 | 562 | ||
589 | static int lm80_read_value(struct i2c_client *client, u8 reg) | 563 | static int lm80_read_value(struct i2c_client *client, u8 reg) |
@@ -614,8 +588,8 @@ static void lm80_init_client(struct i2c_client *client) | |||
614 | 588 | ||
615 | static struct lm80_data *lm80_update_device(struct device *dev) | 589 | static struct lm80_data *lm80_update_device(struct device *dev) |
616 | { | 590 | { |
617 | struct i2c_client *client = to_i2c_client(dev); | 591 | struct lm80_data *data = dev_get_drvdata(dev); |
618 | struct lm80_data *data = i2c_get_clientdata(client); | 592 | struct i2c_client *client = data->client; |
619 | int i; | 593 | int i; |
620 | int rv; | 594 | int rv; |
621 | int prev_rv; | 595 | int prev_rv; |
@@ -627,7 +601,7 @@ static struct lm80_data *lm80_update_device(struct device *dev) | |||
627 | lm80_init_client(client); | 601 | lm80_init_client(client); |
628 | 602 | ||
629 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { | 603 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
630 | dev_dbg(&client->dev, "Starting lm80 update\n"); | 604 | dev_dbg(dev, "Starting lm80 update\n"); |
631 | for (i = 0; i <= 6; i++) { | 605 | for (i = 0; i <= 6; i++) { |
632 | rv = lm80_read_value(client, LM80_REG_IN(i)); | 606 | rv = lm80_read_value(client, LM80_REG_IN(i)); |
633 | if (rv < 0) | 607 | if (rv < 0) |