diff options
author | Guenter Roeck <linux@roeck-us.net> | 2014-02-12 01:37:01 -0500 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2014-03-03 11:01:05 -0500 |
commit | 17eaa25c3e39bbd09ec9a032f0e058f34ba70ed2 (patch) | |
tree | ea3c569bc4e6e3578c3807a449a3708d052413d1 | |
parent | 1577f94bcefb0ef88d4fb15313076a45b8bea8a1 (diff) |
hwmon: (max6650) Convert to use devm_hwmon_device_register_with_groups
Simplify code, reduce code size, and attach sensor attributes
to hwmon device.
Reviewed-by: Jean Delvare <jdelvare@suse.de>
Acked-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r-- | drivers/hwmon/max6650.c | 77 |
1 files changed, 28 insertions, 49 deletions
diff --git a/drivers/hwmon/max6650.c b/drivers/hwmon/max6650.c index 26b24f772f5c..badff94edef8 100644 --- a/drivers/hwmon/max6650.c +++ b/drivers/hwmon/max6650.c | |||
@@ -110,7 +110,8 @@ module_param(clock, int, S_IRUGO); | |||
110 | */ | 110 | */ |
111 | 111 | ||
112 | struct max6650_data { | 112 | struct max6650_data { |
113 | struct device *hwmon_dev; | 113 | struct i2c_client *client; |
114 | const struct attribute_group *groups[3]; | ||
114 | struct mutex update_lock; | 115 | struct mutex update_lock; |
115 | int nr_fans; | 116 | int nr_fans; |
116 | char valid; /* zero until following fields are valid */ | 117 | char valid; /* zero until following fields are valid */ |
@@ -134,9 +135,9 @@ static const u8 tach_reg[] = { | |||
134 | 135 | ||
135 | static struct max6650_data *max6650_update_device(struct device *dev) | 136 | static struct max6650_data *max6650_update_device(struct device *dev) |
136 | { | 137 | { |
138 | struct max6650_data *data = dev_get_drvdata(dev); | ||
139 | struct i2c_client *client = data->client; | ||
137 | int i; | 140 | int i; |
138 | struct i2c_client *client = to_i2c_client(dev); | ||
139 | struct max6650_data *data = i2c_get_clientdata(client); | ||
140 | 141 | ||
141 | mutex_lock(&data->update_lock); | 142 | mutex_lock(&data->update_lock); |
142 | 143 | ||
@@ -254,8 +255,8 @@ static ssize_t get_target(struct device *dev, struct device_attribute *devattr, | |||
254 | static ssize_t set_target(struct device *dev, struct device_attribute *devattr, | 255 | static ssize_t set_target(struct device *dev, struct device_attribute *devattr, |
255 | const char *buf, size_t count) | 256 | const char *buf, size_t count) |
256 | { | 257 | { |
257 | struct i2c_client *client = to_i2c_client(dev); | 258 | struct max6650_data *data = dev_get_drvdata(dev); |
258 | struct max6650_data *data = i2c_get_clientdata(client); | 259 | struct i2c_client *client = data->client; |
259 | int kscale, ktach; | 260 | int kscale, ktach; |
260 | unsigned long rpm; | 261 | unsigned long rpm; |
261 | int err; | 262 | int err; |
@@ -323,8 +324,8 @@ static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr, | |||
323 | static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, | 324 | static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, |
324 | const char *buf, size_t count) | 325 | const char *buf, size_t count) |
325 | { | 326 | { |
326 | struct i2c_client *client = to_i2c_client(dev); | 327 | struct max6650_data *data = dev_get_drvdata(dev); |
327 | struct max6650_data *data = i2c_get_clientdata(client); | 328 | struct i2c_client *client = data->client; |
328 | unsigned long pwm; | 329 | unsigned long pwm; |
329 | int err; | 330 | int err; |
330 | 331 | ||
@@ -369,8 +370,8 @@ static ssize_t get_enable(struct device *dev, struct device_attribute *devattr, | |||
369 | static ssize_t set_enable(struct device *dev, struct device_attribute *devattr, | 370 | static ssize_t set_enable(struct device *dev, struct device_attribute *devattr, |
370 | const char *buf, size_t count) | 371 | const char *buf, size_t count) |
371 | { | 372 | { |
372 | struct i2c_client *client = to_i2c_client(dev); | 373 | struct max6650_data *data = dev_get_drvdata(dev); |
373 | struct max6650_data *data = i2c_get_clientdata(client); | 374 | struct i2c_client *client = data->client; |
374 | int max6650_modes[3] = {0, 3, 2}; | 375 | int max6650_modes[3] = {0, 3, 2}; |
375 | unsigned long mode; | 376 | unsigned long mode; |
376 | int err; | 377 | int err; |
@@ -419,8 +420,8 @@ static ssize_t get_div(struct device *dev, struct device_attribute *devattr, | |||
419 | static ssize_t set_div(struct device *dev, struct device_attribute *devattr, | 420 | static ssize_t set_div(struct device *dev, struct device_attribute *devattr, |
420 | const char *buf, size_t count) | 421 | const char *buf, size_t count) |
421 | { | 422 | { |
422 | struct i2c_client *client = to_i2c_client(dev); | 423 | struct max6650_data *data = dev_get_drvdata(dev); |
423 | struct max6650_data *data = i2c_get_clientdata(client); | 424 | struct i2c_client *client = data->client; |
424 | unsigned long div; | 425 | unsigned long div; |
425 | int err; | 426 | int err; |
426 | 427 | ||
@@ -465,7 +466,7 @@ static ssize_t get_alarm(struct device *dev, struct device_attribute *devattr, | |||
465 | { | 466 | { |
466 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 467 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
467 | struct max6650_data *data = max6650_update_device(dev); | 468 | struct max6650_data *data = max6650_update_device(dev); |
468 | struct i2c_client *client = to_i2c_client(dev); | 469 | struct i2c_client *client = data->client; |
469 | int alarm = 0; | 470 | int alarm = 0; |
470 | 471 | ||
471 | if (data->alarm & attr->index) { | 472 | if (data->alarm & attr->index) { |
@@ -503,7 +504,8 @@ static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a, | |||
503 | int n) | 504 | int n) |
504 | { | 505 | { |
505 | struct device *dev = container_of(kobj, struct device, kobj); | 506 | struct device *dev = container_of(kobj, struct device, kobj); |
506 | struct i2c_client *client = to_i2c_client(dev); | 507 | struct max6650_data *data = dev_get_drvdata(dev); |
508 | struct i2c_client *client = data->client; | ||
507 | u8 alarm_en = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN); | 509 | u8 alarm_en = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN); |
508 | struct device_attribute *devattr; | 510 | struct device_attribute *devattr; |
509 | 511 | ||
@@ -538,7 +540,7 @@ static struct attribute *max6650_attrs[] = { | |||
538 | NULL | 540 | NULL |
539 | }; | 541 | }; |
540 | 542 | ||
541 | static struct attribute_group max6650_attr_grp = { | 543 | static const struct attribute_group max6650_group = { |
542 | .attrs = max6650_attrs, | 544 | .attrs = max6650_attrs, |
543 | .is_visible = max6650_attrs_visible, | 545 | .is_visible = max6650_attrs_visible, |
544 | }; | 546 | }; |
@@ -550,7 +552,7 @@ static struct attribute *max6651_attrs[] = { | |||
550 | NULL | 552 | NULL |
551 | }; | 553 | }; |
552 | 554 | ||
553 | static const struct attribute_group max6651_attr_grp = { | 555 | static const struct attribute_group max6651_group = { |
554 | .attrs = max6651_attrs, | 556 | .attrs = max6651_attrs, |
555 | }; | 557 | }; |
556 | 558 | ||
@@ -558,9 +560,9 @@ static const struct attribute_group max6651_attr_grp = { | |||
558 | * Real code | 560 | * Real code |
559 | */ | 561 | */ |
560 | 562 | ||
561 | static int max6650_init_client(struct i2c_client *client) | 563 | static int max6650_init_client(struct max6650_data *data, |
564 | struct i2c_client *client) | ||
562 | { | 565 | { |
563 | struct max6650_data *data = i2c_get_clientdata(client); | ||
564 | int config; | 566 | int config; |
565 | int err = -EIO; | 567 | int err = -EIO; |
566 | 568 | ||
@@ -649,6 +651,7 @@ static int max6650_probe(struct i2c_client *client, | |||
649 | const struct i2c_device_id *id) | 651 | const struct i2c_device_id *id) |
650 | { | 652 | { |
651 | struct max6650_data *data; | 653 | struct max6650_data *data; |
654 | struct device *hwmon_dev; | ||
652 | int err; | 655 | int err; |
653 | 656 | ||
654 | data = devm_kzalloc(&client->dev, sizeof(struct max6650_data), | 657 | data = devm_kzalloc(&client->dev, sizeof(struct max6650_data), |
@@ -658,49 +661,26 @@ static int max6650_probe(struct i2c_client *client, | |||
658 | return -ENOMEM; | 661 | return -ENOMEM; |
659 | } | 662 | } |
660 | 663 | ||
661 | i2c_set_clientdata(client, data); | 664 | data->client = client; |
662 | mutex_init(&data->update_lock); | 665 | mutex_init(&data->update_lock); |
663 | data->nr_fans = id->driver_data; | 666 | data->nr_fans = id->driver_data; |
664 | 667 | ||
665 | /* | 668 | /* |
666 | * Initialize the max6650 chip | 669 | * Initialize the max6650 chip |
667 | */ | 670 | */ |
668 | err = max6650_init_client(client); | 671 | err = max6650_init_client(data, client); |
669 | if (err) | 672 | if (err) |
670 | return err; | 673 | return err; |
671 | 674 | ||
672 | err = sysfs_create_group(&client->dev.kobj, &max6650_attr_grp); | 675 | data->groups[0] = &max6650_group; |
673 | if (err) | ||
674 | return err; | ||
675 | /* 3 additional fan inputs for the MAX6651 */ | 676 | /* 3 additional fan inputs for the MAX6651 */ |
676 | if (data->nr_fans == 4) { | ||
677 | err = sysfs_create_group(&client->dev.kobj, &max6651_attr_grp); | ||
678 | if (err) | ||
679 | goto err_remove; | ||
680 | } | ||
681 | |||
682 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
683 | if (!IS_ERR(data->hwmon_dev)) | ||
684 | return 0; | ||
685 | |||
686 | err = PTR_ERR(data->hwmon_dev); | ||
687 | dev_err(&client->dev, "error registering hwmon device.\n"); | ||
688 | if (data->nr_fans == 4) | 677 | if (data->nr_fans == 4) |
689 | sysfs_remove_group(&client->dev.kobj, &max6651_attr_grp); | 678 | data->groups[1] = &max6651_group; |
690 | err_remove: | ||
691 | sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp); | ||
692 | return err; | ||
693 | } | ||
694 | |||
695 | static int max6650_remove(struct i2c_client *client) | ||
696 | { | ||
697 | struct max6650_data *data = i2c_get_clientdata(client); | ||
698 | 679 | ||
699 | hwmon_device_unregister(data->hwmon_dev); | 680 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
700 | if (data->nr_fans == 4) | 681 | client->name, data, |
701 | sysfs_remove_group(&client->dev.kobj, &max6651_attr_grp); | 682 | data->groups); |
702 | sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp); | 683 | return PTR_ERR_OR_ZERO(hwmon_dev); |
703 | return 0; | ||
704 | } | 684 | } |
705 | 685 | ||
706 | static const struct i2c_device_id max6650_id[] = { | 686 | static const struct i2c_device_id max6650_id[] = { |
@@ -715,7 +695,6 @@ static struct i2c_driver max6650_driver = { | |||
715 | .name = "max6650", | 695 | .name = "max6650", |
716 | }, | 696 | }, |
717 | .probe = max6650_probe, | 697 | .probe = max6650_probe, |
718 | .remove = max6650_remove, | ||
719 | .id_table = max6650_id, | 698 | .id_table = max6650_id, |
720 | }; | 699 | }; |
721 | 700 | ||