diff options
author | Guenter Roeck <linux@roeck-us.net> | 2014-04-04 12:01:33 -0400 |
---|---|---|
committer | Jean Delvare <jdelvare@suse.de> | 2014-04-04 12:01:33 -0400 |
commit | e19eea84078dfa5ab63c661b3d4367c8621590e0 (patch) | |
tree | 814b0db5d9ecb25572dcfb67e9d813b4e35ff9f0 | |
parent | b76552b310645fddd76dad5e87d0e8eb3d15e4de (diff) |
hwmon: (lm63) Convert to use devm_hwmon_device_register_with_groups
Simplify code, reduce code size, attach hwmon attributes to hwmon device.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Jean Delvare <jdelvare@suse.de>
-rw-r--r-- | drivers/hwmon/lm63.c | 106 |
1 files changed, 40 insertions, 66 deletions
diff --git a/drivers/hwmon/lm63.c b/drivers/hwmon/lm63.c index 7dc4873b343f..c5291645e5dc 100644 --- a/drivers/hwmon/lm63.c +++ b/drivers/hwmon/lm63.c | |||
@@ -155,7 +155,7 @@ enum chips { lm63, lm64, lm96163 }; | |||
155 | */ | 155 | */ |
156 | 156 | ||
157 | struct lm63_data { | 157 | struct lm63_data { |
158 | struct device *hwmon_dev; | 158 | struct i2c_client *client; |
159 | struct mutex update_lock; | 159 | struct mutex update_lock; |
160 | const struct attribute_group *groups[5]; | 160 | const struct attribute_group *groups[5]; |
161 | char valid; /* zero until following fields are valid */ | 161 | char valid; /* zero until following fields are valid */ |
@@ -219,9 +219,9 @@ static inline int lut_temp_to_reg(struct lm63_data *data, long val) | |||
219 | * Update the lookup table register cache. | 219 | * Update the lookup table register cache. |
220 | * client->update_lock must be held when calling this function. | 220 | * client->update_lock must be held when calling this function. |
221 | */ | 221 | */ |
222 | static void lm63_update_lut(struct i2c_client *client) | 222 | static void lm63_update_lut(struct lm63_data *data) |
223 | { | 223 | { |
224 | struct lm63_data *data = i2c_get_clientdata(client); | 224 | struct i2c_client *client = data->client; |
225 | int i; | 225 | int i; |
226 | 226 | ||
227 | if (time_after(jiffies, data->lut_last_updated + 5 * HZ) || | 227 | if (time_after(jiffies, data->lut_last_updated + 5 * HZ) || |
@@ -242,8 +242,8 @@ static void lm63_update_lut(struct i2c_client *client) | |||
242 | 242 | ||
243 | static struct lm63_data *lm63_update_device(struct device *dev) | 243 | static struct lm63_data *lm63_update_device(struct device *dev) |
244 | { | 244 | { |
245 | struct i2c_client *client = to_i2c_client(dev); | 245 | struct lm63_data *data = dev_get_drvdata(dev); |
246 | struct lm63_data *data = i2c_get_clientdata(client); | 246 | struct i2c_client *client = data->client; |
247 | unsigned long next_update; | 247 | unsigned long next_update; |
248 | 248 | ||
249 | mutex_lock(&data->update_lock); | 249 | mutex_lock(&data->update_lock); |
@@ -311,7 +311,7 @@ static struct lm63_data *lm63_update_device(struct device *dev) | |||
311 | data->valid = 1; | 311 | data->valid = 1; |
312 | } | 312 | } |
313 | 313 | ||
314 | lm63_update_lut(client); | 314 | lm63_update_lut(data); |
315 | 315 | ||
316 | mutex_unlock(&data->update_lock); | 316 | mutex_unlock(&data->update_lock); |
317 | 317 | ||
@@ -322,18 +322,17 @@ static struct lm63_data *lm63_update_device(struct device *dev) | |||
322 | * Trip points in the lookup table should be in ascending order for both | 322 | * Trip points in the lookup table should be in ascending order for both |
323 | * temperatures and PWM output values. | 323 | * temperatures and PWM output values. |
324 | */ | 324 | */ |
325 | static int lm63_lut_looks_bad(struct i2c_client *client) | 325 | static int lm63_lut_looks_bad(struct device *dev, struct lm63_data *data) |
326 | { | 326 | { |
327 | struct lm63_data *data = i2c_get_clientdata(client); | ||
328 | int i; | 327 | int i; |
329 | 328 | ||
330 | mutex_lock(&data->update_lock); | 329 | mutex_lock(&data->update_lock); |
331 | lm63_update_lut(client); | 330 | lm63_update_lut(data); |
332 | 331 | ||
333 | for (i = 1; i < data->lut_size; i++) { | 332 | for (i = 1; i < data->lut_size; i++) { |
334 | if (data->pwm1[1 + i - 1] > data->pwm1[1 + i] | 333 | if (data->pwm1[1 + i - 1] > data->pwm1[1 + i] |
335 | || data->temp8[3 + i - 1] > data->temp8[3 + i]) { | 334 | || data->temp8[3 + i - 1] > data->temp8[3 + i]) { |
336 | dev_warn(&client->dev, | 335 | dev_warn(dev, |
337 | "Lookup table doesn't look sane (check entries %d and %d)\n", | 336 | "Lookup table doesn't look sane (check entries %d and %d)\n", |
338 | i, i + 1); | 337 | i, i + 1); |
339 | break; | 338 | break; |
@@ -359,8 +358,8 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, | |||
359 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, | 358 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
360 | const char *buf, size_t count) | 359 | const char *buf, size_t count) |
361 | { | 360 | { |
362 | struct i2c_client *client = to_i2c_client(dev); | 361 | struct lm63_data *data = dev_get_drvdata(dev); |
363 | struct lm63_data *data = i2c_get_clientdata(client); | 362 | struct i2c_client *client = data->client; |
364 | unsigned long val; | 363 | unsigned long val; |
365 | int err; | 364 | int err; |
366 | 365 | ||
@@ -400,8 +399,8 @@ static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr, | |||
400 | const char *buf, size_t count) | 399 | const char *buf, size_t count) |
401 | { | 400 | { |
402 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 401 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
403 | struct i2c_client *client = to_i2c_client(dev); | 402 | struct lm63_data *data = dev_get_drvdata(dev); |
404 | struct lm63_data *data = i2c_get_clientdata(client); | 403 | struct i2c_client *client = data->client; |
405 | int nr = attr->index; | 404 | int nr = attr->index; |
406 | unsigned long val; | 405 | unsigned long val; |
407 | int err; | 406 | int err; |
@@ -436,8 +435,8 @@ static ssize_t set_pwm1_enable(struct device *dev, | |||
436 | struct device_attribute *dummy, | 435 | struct device_attribute *dummy, |
437 | const char *buf, size_t count) | 436 | const char *buf, size_t count) |
438 | { | 437 | { |
439 | struct i2c_client *client = to_i2c_client(dev); | 438 | struct lm63_data *data = dev_get_drvdata(dev); |
440 | struct lm63_data *data = i2c_get_clientdata(client); | 439 | struct i2c_client *client = data->client; |
441 | unsigned long val; | 440 | unsigned long val; |
442 | int err; | 441 | int err; |
443 | 442 | ||
@@ -451,7 +450,7 @@ static ssize_t set_pwm1_enable(struct device *dev, | |||
451 | * Only let the user switch to automatic mode if the lookup table | 450 | * Only let the user switch to automatic mode if the lookup table |
452 | * looks sane. | 451 | * looks sane. |
453 | */ | 452 | */ |
454 | if (val == 2 && lm63_lut_looks_bad(client)) | 453 | if (val == 2 && lm63_lut_looks_bad(dev, data)) |
455 | return -EPERM; | 454 | return -EPERM; |
456 | 455 | ||
457 | mutex_lock(&data->update_lock); | 456 | mutex_lock(&data->update_lock); |
@@ -462,7 +461,7 @@ static ssize_t set_pwm1_enable(struct device *dev, | |||
462 | else | 461 | else |
463 | data->config_fan &= ~0x20; | 462 | data->config_fan &= ~0x20; |
464 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, | 463 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, |
465 | data->config_fan); | 464 | data->config_fan); |
466 | mutex_unlock(&data->update_lock); | 465 | mutex_unlock(&data->update_lock); |
467 | return count; | 466 | return count; |
468 | } | 467 | } |
@@ -506,8 +505,8 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, | |||
506 | const char *buf, size_t count) | 505 | const char *buf, size_t count) |
507 | { | 506 | { |
508 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 507 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
509 | struct i2c_client *client = to_i2c_client(dev); | 508 | struct lm63_data *data = dev_get_drvdata(dev); |
510 | struct lm63_data *data = i2c_get_clientdata(client); | 509 | struct i2c_client *client = data->client; |
511 | int nr = attr->index; | 510 | int nr = attr->index; |
512 | long val; | 511 | long val; |
513 | int err; | 512 | int err; |
@@ -580,8 +579,8 @@ static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | |||
580 | }; | 579 | }; |
581 | 580 | ||
582 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 581 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
583 | struct i2c_client *client = to_i2c_client(dev); | 582 | struct lm63_data *data = dev_get_drvdata(dev); |
584 | struct lm63_data *data = i2c_get_clientdata(client); | 583 | struct i2c_client *client = data->client; |
585 | long val; | 584 | long val; |
586 | int err; | 585 | int err; |
587 | int nr = attr->index; | 586 | int nr = attr->index; |
@@ -636,8 +635,8 @@ static ssize_t set_temp2_crit_hyst(struct device *dev, | |||
636 | struct device_attribute *dummy, | 635 | struct device_attribute *dummy, |
637 | const char *buf, size_t count) | 636 | const char *buf, size_t count) |
638 | { | 637 | { |
639 | struct i2c_client *client = to_i2c_client(dev); | 638 | struct lm63_data *data = dev_get_drvdata(dev); |
640 | struct lm63_data *data = i2c_get_clientdata(client); | 639 | struct i2c_client *client = data->client; |
641 | long val; | 640 | long val; |
642 | int err; | 641 | int err; |
643 | long hyst; | 642 | long hyst; |
@@ -658,11 +657,11 @@ static ssize_t set_temp2_crit_hyst(struct device *dev, | |||
658 | * Set conversion rate. | 657 | * Set conversion rate. |
659 | * client->update_lock must be held when calling this function. | 658 | * client->update_lock must be held when calling this function. |
660 | */ | 659 | */ |
661 | static void lm63_set_convrate(struct i2c_client *client, struct lm63_data *data, | 660 | static void lm63_set_convrate(struct lm63_data *data, unsigned int interval) |
662 | unsigned int interval) | ||
663 | { | 661 | { |
664 | int i; | 662 | struct i2c_client *client = data->client; |
665 | unsigned int update_interval; | 663 | unsigned int update_interval; |
664 | int i; | ||
666 | 665 | ||
667 | /* Shift calculations to avoid rounding errors */ | 666 | /* Shift calculations to avoid rounding errors */ |
668 | interval <<= 6; | 667 | interval <<= 6; |
@@ -690,8 +689,7 @@ static ssize_t set_update_interval(struct device *dev, | |||
690 | struct device_attribute *attr, | 689 | struct device_attribute *attr, |
691 | const char *buf, size_t count) | 690 | const char *buf, size_t count) |
692 | { | 691 | { |
693 | struct i2c_client *client = to_i2c_client(dev); | 692 | struct lm63_data *data = dev_get_drvdata(dev); |
694 | struct lm63_data *data = i2c_get_clientdata(client); | ||
695 | unsigned long val; | 693 | unsigned long val; |
696 | int err; | 694 | int err; |
697 | 695 | ||
@@ -700,7 +698,7 @@ static ssize_t set_update_interval(struct device *dev, | |||
700 | return err; | 698 | return err; |
701 | 699 | ||
702 | mutex_lock(&data->update_lock); | 700 | mutex_lock(&data->update_lock); |
703 | lm63_set_convrate(client, data, clamp_val(val, 0, 100000)); | 701 | lm63_set_convrate(data, clamp_val(val, 0, 100000)); |
704 | mutex_unlock(&data->update_lock); | 702 | mutex_unlock(&data->update_lock); |
705 | 703 | ||
706 | return count; | 704 | return count; |
@@ -709,8 +707,7 @@ static ssize_t set_update_interval(struct device *dev, | |||
709 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, | 707 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, |
710 | char *buf) | 708 | char *buf) |
711 | { | 709 | { |
712 | struct i2c_client *client = to_i2c_client(dev); | 710 | struct lm63_data *data = dev_get_drvdata(dev); |
713 | struct lm63_data *data = i2c_get_clientdata(client); | ||
714 | 711 | ||
715 | return sprintf(buf, data->trutherm ? "1\n" : "2\n"); | 712 | return sprintf(buf, data->trutherm ? "1\n" : "2\n"); |
716 | } | 713 | } |
@@ -718,8 +715,8 @@ static ssize_t show_type(struct device *dev, struct device_attribute *attr, | |||
718 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, | 715 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, |
719 | const char *buf, size_t count) | 716 | const char *buf, size_t count) |
720 | { | 717 | { |
721 | struct i2c_client *client = to_i2c_client(dev); | 718 | struct lm63_data *data = dev_get_drvdata(dev); |
722 | struct lm63_data *data = i2c_get_clientdata(client); | 719 | struct i2c_client *client = data->client; |
723 | unsigned long val; | 720 | unsigned long val; |
724 | int ret; | 721 | int ret; |
725 | u8 reg; | 722 | u8 reg; |
@@ -956,8 +953,7 @@ static umode_t lm63_attribute_mode(struct kobject *kobj, | |||
956 | struct attribute *attr, int index) | 953 | struct attribute *attr, int index) |
957 | { | 954 | { |
958 | struct device *dev = container_of(kobj, struct device, kobj); | 955 | struct device *dev = container_of(kobj, struct device, kobj); |
959 | struct i2c_client *client = to_i2c_client(dev); | 956 | struct lm63_data *data = dev_get_drvdata(dev); |
960 | struct lm63_data *data = i2c_get_clientdata(client); | ||
961 | 957 | ||
962 | if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr | 958 | if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr |
963 | && (data->kind == lm64 || | 959 | && (data->kind == lm64 || |
@@ -1036,9 +1032,9 @@ static int lm63_detect(struct i2c_client *client, | |||
1036 | * Ideally we shouldn't have to initialize anything, since the BIOS | 1032 | * Ideally we shouldn't have to initialize anything, since the BIOS |
1037 | * should have taken care of everything | 1033 | * should have taken care of everything |
1038 | */ | 1034 | */ |
1039 | static void lm63_init_client(struct i2c_client *client) | 1035 | static void lm63_init_client(struct lm63_data *data) |
1040 | { | 1036 | { |
1041 | struct lm63_data *data = i2c_get_clientdata(client); | 1037 | struct i2c_client *client = data->client; |
1042 | struct device *dev = &client->dev; | 1038 | struct device *dev = &client->dev; |
1043 | u8 convrate; | 1039 | u8 convrate; |
1044 | 1040 | ||
@@ -1116,15 +1112,15 @@ static int lm63_probe(struct i2c_client *client, | |||
1116 | const struct i2c_device_id *id) | 1112 | const struct i2c_device_id *id) |
1117 | { | 1113 | { |
1118 | struct device *dev = &client->dev; | 1114 | struct device *dev = &client->dev; |
1115 | struct device *hwmon_dev; | ||
1119 | struct lm63_data *data; | 1116 | struct lm63_data *data; |
1120 | int groups = 0; | 1117 | int groups = 0; |
1121 | int err; | ||
1122 | 1118 | ||
1123 | data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL); | 1119 | data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL); |
1124 | if (!data) | 1120 | if (!data) |
1125 | return -ENOMEM; | 1121 | return -ENOMEM; |
1126 | 1122 | ||
1127 | i2c_set_clientdata(client, data); | 1123 | data->client = client; |
1128 | data->valid = 0; | 1124 | data->valid = 0; |
1129 | mutex_init(&data->update_lock); | 1125 | mutex_init(&data->update_lock); |
1130 | 1126 | ||
@@ -1134,7 +1130,7 @@ static int lm63_probe(struct i2c_client *client, | |||
1134 | data->temp2_offset = 16000; | 1130 | data->temp2_offset = 16000; |
1135 | 1131 | ||
1136 | /* Initialize chip */ | 1132 | /* Initialize chip */ |
1137 | lm63_init_client(client); | 1133 | lm63_init_client(data); |
1138 | 1134 | ||
1139 | /* Register sysfs hooks */ | 1135 | /* Register sysfs hooks */ |
1140 | data->groups[groups++] = &lm63_group; | 1136 | data->groups[groups++] = &lm63_group; |
@@ -1146,30 +1142,9 @@ static int lm63_probe(struct i2c_client *client, | |||
1146 | data->groups[groups++] = &lm63_group_extra_lut; | 1142 | data->groups[groups++] = &lm63_group_extra_lut; |
1147 | } | 1143 | } |
1148 | 1144 | ||
1149 | err = sysfs_create_groups(&dev->kobj, data->groups); | 1145 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
1150 | if (err) | 1146 | data, data->groups); |
1151 | return err; | 1147 | return PTR_ERR_OR_ZERO(hwmon_dev); |
1152 | |||
1153 | data->hwmon_dev = hwmon_device_register(dev); | ||
1154 | if (IS_ERR(data->hwmon_dev)) { | ||
1155 | err = PTR_ERR(data->hwmon_dev); | ||
1156 | goto exit_remove_files; | ||
1157 | } | ||
1158 | |||
1159 | return 0; | ||
1160 | |||
1161 | exit_remove_files: | ||
1162 | sysfs_remove_groups(&dev->kobj, data->groups); | ||
1163 | return err; | ||
1164 | } | ||
1165 | |||
1166 | static int lm63_remove(struct i2c_client *client) | ||
1167 | { | ||
1168 | struct lm63_data *data = i2c_get_clientdata(client); | ||
1169 | |||
1170 | hwmon_device_unregister(data->hwmon_dev); | ||
1171 | sysfs_remove_groups(&client->dev.kobj, data->groups); | ||
1172 | return 0; | ||
1173 | } | 1148 | } |
1174 | 1149 | ||
1175 | /* | 1150 | /* |
@@ -1190,7 +1165,6 @@ static struct i2c_driver lm63_driver = { | |||
1190 | .name = "lm63", | 1165 | .name = "lm63", |
1191 | }, | 1166 | }, |
1192 | .probe = lm63_probe, | 1167 | .probe = lm63_probe, |
1193 | .remove = lm63_remove, | ||
1194 | .id_table = lm63_id, | 1168 | .id_table = lm63_id, |
1195 | .detect = lm63_detect, | 1169 | .detect = lm63_detect, |
1196 | .address_list = normal_i2c, | 1170 | .address_list = normal_i2c, |