diff options
author | Guenter Roeck <linux@roeck-us.net> | 2013-03-18 09:40:16 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2013-04-08 00:16:41 -0400 |
commit | 6bf44cedab5918bee8e8fea8c7ad187996c8c874 (patch) | |
tree | 4069175c9592340023f80e6834d8e2aa59b27f65 /drivers/hwmon | |
parent | 71bb2d211e47b9a04348149608629afc2742fee0 (diff) |
hwmon: (max6697) Use is_visible and sysfs_create_group
Simplify the code and reduce its size by using is_visible to determine
valid attributes, and sysfs_create_group to create all of them with
a single call.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r-- | drivers/hwmon/max6697.c | 209 |
1 files changed, 93 insertions, 116 deletions
diff --git a/drivers/hwmon/max6697.c b/drivers/hwmon/max6697.c index bf4aa3777fc1..328fb0353c17 100644 --- a/drivers/hwmon/max6697.c +++ b/drivers/hwmon/max6697.c | |||
@@ -399,82 +399,95 @@ static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_alarm, NULL, 5); | |||
399 | static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_alarm, NULL, 6); | 399 | static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_alarm, NULL, 6); |
400 | static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_alarm, NULL, 7); | 400 | static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_alarm, NULL, 7); |
401 | 401 | ||
402 | static struct attribute *max6697_attributes[8][7] = { | 402 | static DEVICE_ATTR(dummy, 0, NULL, NULL); |
403 | { | 403 | |
404 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 404 | static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr, |
405 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 405 | int index) |
406 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 406 | { |
407 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | 407 | struct device *dev = container_of(kobj, struct device, kobj); |
408 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 408 | struct i2c_client *client = to_i2c_client(dev); |
409 | NULL | 409 | struct max6697_data *data = i2c_get_clientdata(client); |
410 | }, { | 410 | const struct max6697_chip_data *chip = data->chip; |
411 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 411 | int channel = index / 6; /* channel number */ |
412 | &sensor_dev_attr_temp2_max.dev_attr.attr, | 412 | int nr = index % 6; /* attribute index within channel */ |
413 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 413 | |
414 | &sensor_dev_attr_temp2_crit.dev_attr.attr, | 414 | if (channel >= chip->channels) |
415 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 415 | return 0; |
416 | &sensor_dev_attr_temp2_fault.dev_attr.attr, | 416 | |
417 | NULL | 417 | if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel))) |
418 | }, { | 418 | return 0; |
419 | &sensor_dev_attr_temp3_input.dev_attr.attr, | 419 | if (nr == 5 && !(chip->have_fault & (1 << channel))) |
420 | &sensor_dev_attr_temp3_max.dev_attr.attr, | 420 | return 0; |
421 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | 421 | |
422 | &sensor_dev_attr_temp3_crit.dev_attr.attr, | 422 | return attr->mode; |
423 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | 423 | } |
424 | &sensor_dev_attr_temp3_fault.dev_attr.attr, | 424 | |
425 | NULL | 425 | /* |
426 | }, { | 426 | * max6697_is_visible uses the index into the following array to determine |
427 | &sensor_dev_attr_temp4_input.dev_attr.attr, | 427 | * if attributes should be created or not. Any change in order or content |
428 | &sensor_dev_attr_temp4_max.dev_attr.attr, | 428 | * must be matched in max6697_is_visible. |
429 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | 429 | */ |
430 | &sensor_dev_attr_temp4_crit.dev_attr.attr, | 430 | static struct attribute *max6697_attributes[] = { |
431 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, | 431 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
432 | &sensor_dev_attr_temp4_fault.dev_attr.attr, | 432 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
433 | NULL | 433 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
434 | }, { | 434 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
435 | &sensor_dev_attr_temp5_input.dev_attr.attr, | 435 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
436 | &sensor_dev_attr_temp5_max.dev_attr.attr, | 436 | &dev_attr_dummy.attr, |
437 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, | 437 | |
438 | &sensor_dev_attr_temp5_crit.dev_attr.attr, | 438 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
439 | &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, | 439 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
440 | &sensor_dev_attr_temp5_fault.dev_attr.attr, | 440 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
441 | NULL | 441 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
442 | }, { | 442 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
443 | &sensor_dev_attr_temp6_input.dev_attr.attr, | 443 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
444 | &sensor_dev_attr_temp6_max.dev_attr.attr, | 444 | |
445 | &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, | 445 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
446 | &sensor_dev_attr_temp6_crit.dev_attr.attr, | 446 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
447 | &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, | 447 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
448 | &sensor_dev_attr_temp6_fault.dev_attr.attr, | 448 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
449 | NULL | 449 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
450 | }, { | 450 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
451 | &sensor_dev_attr_temp7_input.dev_attr.attr, | 451 | |
452 | &sensor_dev_attr_temp7_max.dev_attr.attr, | 452 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
453 | &sensor_dev_attr_temp7_max_alarm.dev_attr.attr, | 453 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
454 | &sensor_dev_attr_temp7_crit.dev_attr.attr, | 454 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
455 | &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr, | 455 | &sensor_dev_attr_temp4_crit.dev_attr.attr, |
456 | &sensor_dev_attr_temp7_fault.dev_attr.attr, | 456 | &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, |
457 | NULL | 457 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
458 | }, { | 458 | |
459 | &sensor_dev_attr_temp8_input.dev_attr.attr, | 459 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
460 | &sensor_dev_attr_temp8_max.dev_attr.attr, | 460 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
461 | &sensor_dev_attr_temp8_max_alarm.dev_attr.attr, | 461 | &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, |
462 | &sensor_dev_attr_temp8_crit.dev_attr.attr, | 462 | &sensor_dev_attr_temp5_crit.dev_attr.attr, |
463 | &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr, | 463 | &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, |
464 | &sensor_dev_attr_temp8_fault.dev_attr.attr, | 464 | &sensor_dev_attr_temp5_fault.dev_attr.attr, |
465 | NULL | 465 | |
466 | } | 466 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
467 | &sensor_dev_attr_temp6_max.dev_attr.attr, | ||
468 | &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, | ||
469 | &sensor_dev_attr_temp6_crit.dev_attr.attr, | ||
470 | &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, | ||
471 | &sensor_dev_attr_temp6_fault.dev_attr.attr, | ||
472 | |||
473 | &sensor_dev_attr_temp7_input.dev_attr.attr, | ||
474 | &sensor_dev_attr_temp7_max.dev_attr.attr, | ||
475 | &sensor_dev_attr_temp7_max_alarm.dev_attr.attr, | ||
476 | &sensor_dev_attr_temp7_crit.dev_attr.attr, | ||
477 | &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr, | ||
478 | &sensor_dev_attr_temp7_fault.dev_attr.attr, | ||
479 | |||
480 | &sensor_dev_attr_temp8_input.dev_attr.attr, | ||
481 | &sensor_dev_attr_temp8_max.dev_attr.attr, | ||
482 | &sensor_dev_attr_temp8_max_alarm.dev_attr.attr, | ||
483 | &sensor_dev_attr_temp8_crit.dev_attr.attr, | ||
484 | &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr, | ||
485 | &sensor_dev_attr_temp8_fault.dev_attr.attr, | ||
486 | NULL | ||
467 | }; | 487 | }; |
468 | 488 | ||
469 | static const struct attribute_group max6697_group[8] = { | 489 | static const struct attribute_group max6697_group = { |
470 | { .attrs = max6697_attributes[0] }, | 490 | .attrs = max6697_attributes, .is_visible = max6697_is_visible, |
471 | { .attrs = max6697_attributes[1] }, | ||
472 | { .attrs = max6697_attributes[2] }, | ||
473 | { .attrs = max6697_attributes[3] }, | ||
474 | { .attrs = max6697_attributes[4] }, | ||
475 | { .attrs = max6697_attributes[5] }, | ||
476 | { .attrs = max6697_attributes[6] }, | ||
477 | { .attrs = max6697_attributes[7] }, | ||
478 | }; | 491 | }; |
479 | 492 | ||
480 | static void max6697_get_config_of(struct device_node *node, | 493 | static void max6697_get_config_of(struct device_node *node, |
@@ -606,21 +619,13 @@ done: | |||
606 | return 0; | 619 | return 0; |
607 | } | 620 | } |
608 | 621 | ||
609 | static void max6697_remove_files(struct i2c_client *client) | ||
610 | { | ||
611 | int i; | ||
612 | |||
613 | for (i = 0; i < ARRAY_SIZE(max6697_group); i++) | ||
614 | sysfs_remove_group(&client->dev.kobj, &max6697_group[i]); | ||
615 | } | ||
616 | |||
617 | static int max6697_probe(struct i2c_client *client, | 622 | static int max6697_probe(struct i2c_client *client, |
618 | const struct i2c_device_id *id) | 623 | const struct i2c_device_id *id) |
619 | { | 624 | { |
620 | struct i2c_adapter *adapter = client->adapter; | 625 | struct i2c_adapter *adapter = client->adapter; |
621 | struct device *dev = &client->dev; | 626 | struct device *dev = &client->dev; |
622 | struct max6697_data *data; | 627 | struct max6697_data *data; |
623 | int i, err; | 628 | int err; |
624 | 629 | ||
625 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 630 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
626 | return -ENODEV; | 631 | return -ENODEV; |
@@ -639,37 +644,9 @@ static int max6697_probe(struct i2c_client *client, | |||
639 | if (err) | 644 | if (err) |
640 | return err; | 645 | return err; |
641 | 646 | ||
642 | for (i = 0; i < data->chip->channels; i++) { | 647 | err = sysfs_create_group(&client->dev.kobj, &max6697_group); |
643 | err = sysfs_create_file(&dev->kobj, | 648 | if (err) |
644 | max6697_attributes[i][0]); | 649 | return err; |
645 | if (err) | ||
646 | goto error; | ||
647 | err = sysfs_create_file(&dev->kobj, | ||
648 | max6697_attributes[i][1]); | ||
649 | if (err) | ||
650 | goto error; | ||
651 | err = sysfs_create_file(&dev->kobj, | ||
652 | max6697_attributes[i][2]); | ||
653 | if (err) | ||
654 | goto error; | ||
655 | |||
656 | if (data->chip->have_crit & (1 << i)) { | ||
657 | err = sysfs_create_file(&dev->kobj, | ||
658 | max6697_attributes[i][3]); | ||
659 | if (err) | ||
660 | goto error; | ||
661 | err = sysfs_create_file(&dev->kobj, | ||
662 | max6697_attributes[i][4]); | ||
663 | if (err) | ||
664 | goto error; | ||
665 | } | ||
666 | if (data->chip->have_fault & (1 << i)) { | ||
667 | err = sysfs_create_file(&dev->kobj, | ||
668 | max6697_attributes[i][5]); | ||
669 | if (err) | ||
670 | goto error; | ||
671 | } | ||
672 | } | ||
673 | 650 | ||
674 | data->hwmon_dev = hwmon_device_register(dev); | 651 | data->hwmon_dev = hwmon_device_register(dev); |
675 | if (IS_ERR(data->hwmon_dev)) { | 652 | if (IS_ERR(data->hwmon_dev)) { |
@@ -680,7 +657,7 @@ static int max6697_probe(struct i2c_client *client, | |||
680 | return 0; | 657 | return 0; |
681 | 658 | ||
682 | error: | 659 | error: |
683 | max6697_remove_files(client); | 660 | sysfs_remove_group(&client->dev.kobj, &max6697_group); |
684 | return err; | 661 | return err; |
685 | } | 662 | } |
686 | 663 | ||
@@ -689,7 +666,7 @@ static int max6697_remove(struct i2c_client *client) | |||
689 | struct max6697_data *data = i2c_get_clientdata(client); | 666 | struct max6697_data *data = i2c_get_clientdata(client); |
690 | 667 | ||
691 | hwmon_device_unregister(data->hwmon_dev); | 668 | hwmon_device_unregister(data->hwmon_dev); |
692 | max6697_remove_files(client); | 669 | sysfs_remove_group(&client->dev.kobj, &max6697_group); |
693 | 670 | ||
694 | return 0; | 671 | return 0; |
695 | } | 672 | } |