Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (g762) Convert to hwmon_device_register_with_groups

Use ATTRIBUTE_GROUPS macro and hwmon_device_register_with_groups() to simplify
the code a bit.

Use hwmon_device_register_with_groups rather than the device managed version to
ensure g762_of_clock_disable() is called after hwmon_device_unregister().

Signed-off-by: Axel Lin <axel.lin@ingics.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Axel Lin and committed by
Guenter Roeck
398e16db 9dd304f8

+27 -44
+27 -44
drivers/hwmon/g762.c
··· 128 128 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2)) 129 129 130 130 struct g762_data { 131 - struct i2c_client *client; 132 131 struct device *hwmon_dev; 132 + struct i2c_client *client; 133 133 struct clk *clk; 134 134 135 135 /* update mutex */ ··· 206 206 /* helper to grab and cache data, at most one time per second */ 207 207 static struct g762_data *g762_update_client(struct device *dev) 208 208 { 209 - struct i2c_client *client = to_i2c_client(dev); 210 - struct g762_data *data = i2c_get_clientdata(client); 209 + struct g762_data *data = dev_get_drvdata(dev); 210 + struct i2c_client *client = data->client; 211 211 int ret = 0; 212 212 213 213 mutex_lock(&data->update_lock); ··· 266 266 */ 267 267 static int do_set_clk_freq(struct device *dev, unsigned long val) 268 268 { 269 - struct i2c_client *client = to_i2c_client(dev); 270 - struct g762_data *data = i2c_get_clientdata(client); 269 + struct g762_data *data = dev_get_drvdata(dev); 271 270 272 271 if (val > 0xffffff) 273 272 return -EINVAL; ··· 281 282 /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ 282 283 static int do_set_pwm_mode(struct device *dev, unsigned long val) 283 284 { 284 - struct i2c_client *client = to_i2c_client(dev); 285 285 struct g762_data *data = g762_update_client(dev); 286 286 int ret; 287 287 ··· 299 301 ret = -EINVAL; 300 302 goto out; 301 303 } 302 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 304 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 303 305 data->fan_cmd1); 304 306 data->valid = false; 305 307 out: ··· 311 313 /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ 312 314 static int do_set_fan_div(struct device *dev, unsigned long val) 313 315 { 314 - struct i2c_client *client = to_i2c_client(dev); 315 316 struct g762_data *data = g762_update_client(dev); 316 317 int ret; 317 318 ··· 339 342 ret = -EINVAL; 340 343 goto out; 341 344 } 342 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 345 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 343 346 data->fan_cmd1); 344 347 data->valid = false; 345 348 out: ··· 351 354 /* Set fan gear mode. Accepts either 0, 1 or 2. */ 352 355 static int do_set_fan_gear_mode(struct device *dev, unsigned long val) 353 356 { 354 - struct i2c_client *client = to_i2c_client(dev); 355 357 struct g762_data *data = g762_update_client(dev); 356 358 int ret; 357 359 ··· 375 379 ret = -EINVAL; 376 380 goto out; 377 381 } 378 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, 382 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, 379 383 data->fan_cmd2); 380 384 data->valid = false; 381 385 out: ··· 387 391 /* Set number of fan pulses per revolution. Accepts either 2 or 4. */ 388 392 static int do_set_fan_pulses(struct device *dev, unsigned long val) 389 393 { 390 - struct i2c_client *client = to_i2c_client(dev); 391 394 struct g762_data *data = g762_update_client(dev); 392 395 int ret; 393 396 ··· 405 410 ret = -EINVAL; 406 411 goto out; 407 412 } 408 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 413 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 409 414 data->fan_cmd1); 410 415 data->valid = false; 411 416 out: ··· 417 422 /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ 418 423 static int do_set_pwm_enable(struct device *dev, unsigned long val) 419 424 { 420 - struct i2c_client *client = to_i2c_client(dev); 421 425 struct g762_data *data = g762_update_client(dev); 422 426 int ret; 423 427 ··· 438 444 * value of 254 if it is 255 when switching to open-loop. 439 445 */ 440 446 if (data->set_cnt == 0xff) 441 - i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, 442 - 254); 447 + i2c_smbus_write_byte_data(data->client, 448 + G762_REG_SET_CNT, 254); 443 449 break; 444 450 default: 445 451 ret = -EINVAL; 446 452 goto out; 447 453 } 448 454 449 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 455 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 450 456 data->fan_cmd1); 451 457 data->valid = false; 452 458 out: ··· 458 464 /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ 459 465 static int do_set_pwm_polarity(struct device *dev, unsigned long val) 460 466 { 461 - struct i2c_client *client = to_i2c_client(dev); 462 467 struct g762_data *data = g762_update_client(dev); 463 468 int ret; 464 469 ··· 476 483 ret = -EINVAL; 477 484 goto out; 478 485 } 479 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 486 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 480 487 data->fan_cmd1); 481 488 data->valid = false; 482 489 out: ··· 491 498 */ 492 499 static int do_set_pwm(struct device *dev, unsigned long val) 493 500 { 494 - struct i2c_client *client = to_i2c_client(dev); 495 - struct g762_data *data = i2c_get_clientdata(client); 501 + struct g762_data *data = dev_get_drvdata(dev); 502 + struct i2c_client *client = data->client; 496 503 int ret; 497 504 498 505 if (val > 255) ··· 512 519 */ 513 520 static int do_set_fan_target(struct device *dev, unsigned long val) 514 521 { 515 - struct i2c_client *client = to_i2c_client(dev); 516 522 struct g762_data *data = g762_update_client(dev); 517 523 int ret; 518 524 ··· 523 531 G762_PULSE_FROM_REG(data->fan_cmd1), 524 532 G762_CLKDIV_FROM_REG(data->fan_cmd1), 525 533 G762_GEARMULT_FROM_REG(data->fan_cmd2)); 526 - ret = i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, 534 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT, 527 535 data->set_cnt); 528 536 data->valid = false; 529 537 mutex_unlock(&data->update_lock); ··· 534 542 /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ 535 543 static int do_set_fan_startv(struct device *dev, unsigned long val) 536 544 { 537 - struct i2c_client *client = to_i2c_client(dev); 538 545 struct g762_data *data = g762_update_client(dev); 539 546 int ret; 540 547 ··· 562 571 ret = -EINVAL; 563 572 goto out; 564 573 } 565 - ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, 574 + ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, 566 575 data->fan_cmd2); 567 576 data->valid = false; 568 577 out: ··· 1017 1026 get_fan_pulses, set_fan_pulses); 1018 1027 1019 1028 /* Driver data */ 1020 - static struct attribute *g762_attributes[] = { 1029 + static struct attribute *g762_attrs[] = { 1021 1030 &dev_attr_fan1_input.attr, 1022 1031 &dev_attr_fan1_alarm.attr, 1023 1032 &dev_attr_fan1_fault.attr, ··· 1030 1039 NULL 1031 1040 }; 1032 1041 1033 - static const struct attribute_group g762_group = { 1034 - .attrs = g762_attributes, 1035 - }; 1042 + ATTRIBUTE_GROUPS(g762); 1036 1043 1037 1044 /* 1038 1045 * Enable both fan failure detection and fan out of control protection. The ··· 1039 1050 */ 1040 1051 static inline int g762_fan_init(struct device *dev) 1041 1052 { 1042 - struct i2c_client *client = to_i2c_client(dev); 1043 1053 struct g762_data *data = g762_update_client(dev); 1044 1054 1045 1055 if (IS_ERR(data)) ··· 1048 1060 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; 1049 1061 data->valid = false; 1050 1062 1051 - return i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, 1063 + return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 1052 1064 data->fan_cmd1); 1053 1065 } 1054 1066 1055 1067 static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) 1056 1068 { 1069 + struct device *dev = &client->dev; 1057 1070 struct g762_data *data; 1058 1071 int ret; 1059 1072 ··· 1062 1073 I2C_FUNC_SMBUS_BYTE_DATA)) 1063 1074 return -ENODEV; 1064 1075 1065 - data = devm_kzalloc(&client->dev, sizeof(struct g762_data), GFP_KERNEL); 1076 + data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL); 1066 1077 if (!data) 1067 1078 return -ENOMEM; 1068 1079 ··· 1071 1082 mutex_init(&data->update_lock); 1072 1083 1073 1084 /* Enable fan failure detection and fan out of control protection */ 1074 - ret = g762_fan_init(&client->dev); 1085 + ret = g762_fan_init(dev); 1075 1086 if (ret) 1076 1087 return ret; 1077 1088 ··· 1087 1098 if (ret) 1088 1099 goto clock_dis; 1089 1100 1090 - /* Register sysfs hooks */ 1091 - ret = sysfs_create_group(&client->dev.kobj, &g762_group); 1092 - if (ret) 1093 - goto clock_dis; 1094 - 1095 - data->hwmon_dev = hwmon_device_register(&client->dev); 1101 + data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, 1102 + client->name, 1103 + data, 1104 + g762_groups); 1096 1105 if (IS_ERR(data->hwmon_dev)) { 1097 1106 ret = PTR_ERR(data->hwmon_dev); 1098 - goto sysfs_rem; 1107 + goto clock_dis; 1099 1108 } 1100 1109 1101 1110 return 0; 1102 - 1103 - sysfs_rem: 1104 - sysfs_remove_group(&client->dev.kobj, &g762_group); 1105 1111 1106 1112 clock_dis: 1107 1113 g762_of_clock_disable(client); ··· 1109 1125 struct g762_data *data = i2c_get_clientdata(client); 1110 1126 1111 1127 hwmon_device_unregister(data->hwmon_dev); 1112 - sysfs_remove_group(&client->dev.kobj, &g762_group); 1113 1128 g762_of_clock_disable(client); 1114 1129 1115 1130 return 0;