Commit c81cc5a4c14d8d7cc5c891ddf6cb8e7750a44dee

Authored by Guenter Roeck
1 parent 4ce5b1fe31

hwmon: (gpio-fan) Use is_visible to determine if attributes should be created

Simplify code and reduce object size by more than 300 bytes (x86_64).

Cc: Jamie Lentin <jm@lentin.co.uk>
Cc: Simon Guinot <simon.guinot@sequanux.org>
Tested-by: Simon Guinot <simon.guinot@sequanux.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

Showing 1 changed file with 39 additions and 65 deletions Side-by-side Diff

drivers/hwmon/gpio-fan.c
... ... @@ -105,10 +105,6 @@
105 105 if (err)
106 106 return err;
107 107  
108   - err = device_create_file(&pdev->dev, &dev_attr_fan1_alarm);
109   - if (err)
110   - return err;
111   -
112 108 /*
113 109 * If the alarm GPIO don't support interrupts, just leave
114 110 * without initializing the fail notification support.
115 111  
... ... @@ -121,23 +117,9 @@
121 117 irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
122 118 err = devm_request_irq(&pdev->dev, alarm_irq, fan_alarm_irq_handler,
123 119 IRQF_SHARED, "GPIO fan alarm", fan_data);
124   - if (err)
125   - goto err_free_sysfs;
126   -
127   - return 0;
128   -
129   -err_free_sysfs:
130   - device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
131 120 return err;
132 121 }
133 122  
134   -static void fan_alarm_free(struct gpio_fan_data *fan_data)
135   -{
136   - struct platform_device *pdev = fan_data->pdev;
137   -
138   - device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
139   -}
140   -
141 123 /*
142 124 * Control GPIOs.
143 125 */
... ... @@ -327,6 +309,12 @@
327 309 return ret;
328 310 }
329 311  
  312 +static ssize_t show_name(struct device *dev,
  313 + struct device_attribute *attr, char *buf)
  314 +{
  315 + return sprintf(buf, "gpio-fan\n");
  316 +}
  317 +
330 318 static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
331 319 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
332 320 show_pwm_enable, set_pwm_enable);
... ... @@ -336,8 +324,26 @@
336 324 static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
337 325 static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
338 326  
339   -static struct attribute *gpio_fan_ctrl_attributes[] = {
340   - &dev_attr_pwm1.attr,
  327 +static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  328 +
  329 +static umode_t gpio_fan_is_visible(struct kobject *kobj,
  330 + struct attribute *attr, int index)
  331 +{
  332 + struct device *dev = container_of(kobj, struct device, kobj);
  333 + struct gpio_fan_data *data = dev_get_drvdata(dev);
  334 +
  335 + if (index == 1 && !data->alarm)
  336 + return 0;
  337 + if (index > 1 && !data->ctrl)
  338 + return 0;
  339 +
  340 + return attr->mode;
  341 +}
  342 +
  343 +static struct attribute *gpio_fan_attributes[] = {
  344 + &dev_attr_name.attr,
  345 + &dev_attr_fan1_alarm.attr, /* 1 */
  346 + &dev_attr_pwm1.attr, /* 2 */
341 347 &dev_attr_pwm1_enable.attr,
342 348 &dev_attr_pwm1_mode.attr,
343 349 &dev_attr_fan1_input.attr,
... ... @@ -347,8 +353,9 @@
347 353 NULL
348 354 };
349 355  
350   -static const struct attribute_group gpio_fan_ctrl_group = {
351   - .attrs = gpio_fan_ctrl_attributes,
  356 +static const struct attribute_group gpio_fan_group = {
  357 + .attrs = gpio_fan_attributes,
  358 + .is_visible = gpio_fan_is_visible,
352 359 };
353 360  
354 361 static int fan_ctrl_init(struct gpio_fan_data *fan_data,
355 362  
... ... @@ -379,30 +386,9 @@
379 386 if (fan_data->speed_index < 0)
380 387 return -ENODEV;
381 388  
382   - err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
383   - return err;
  389 + return 0;
384 390 }
385 391  
386   -static void fan_ctrl_free(struct gpio_fan_data *fan_data)
387   -{
388   - struct platform_device *pdev = fan_data->pdev;
389   -
390   - sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
391   -}
392   -
393   -/*
394   - * Platform driver.
395   - */
396   -
397   -static ssize_t show_name(struct device *dev,
398   - struct device_attribute *attr, char *buf)
399   -{
400   - return sprintf(buf, "gpio-fan\n");
401   -}
402   -
403   -static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
404   -
405   -
406 392 #ifdef CONFIG_OF_GPIO
407 393 /*
408 394 * Translate OpenFirmware node properties into platform_data
409 395  
410 396  
411 397  
412 398  
413 399  
... ... @@ -546,38 +532,30 @@
546 532  
547 533 /* Configure control GPIOs if available. */
548 534 if (pdata->ctrl && pdata->num_ctrl > 0) {
549   - if (!pdata->speed || pdata->num_speed <= 1) {
550   - err = -EINVAL;
551   - goto err_free_alarm;
552   - }
  535 + if (!pdata->speed || pdata->num_speed <= 1)
  536 + return -EINVAL;
553 537 err = fan_ctrl_init(fan_data, pdata);
554 538 if (err)
555   - goto err_free_alarm;
  539 + return err;
556 540 }
557 541  
558   - err = device_create_file(&pdev->dev, &dev_attr_name);
  542 + err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_group);
559 543 if (err)
560   - goto err_free_ctrl;
  544 + return err;
561 545  
562 546 /* Make this driver part of hwmon class. */
563 547 fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
564 548 if (IS_ERR(fan_data->hwmon_dev)) {
565 549 err = PTR_ERR(fan_data->hwmon_dev);
566   - goto err_remove_name;
  550 + goto err_remove;
567 551 }
568 552  
569 553 dev_info(&pdev->dev, "GPIO fan initialized\n");
570 554  
571 555 return 0;
572 556  
573   -err_remove_name:
574   - device_remove_file(&pdev->dev, &dev_attr_name);
575   -err_free_ctrl:
576   - if (fan_data->ctrl)
577   - fan_ctrl_free(fan_data);
578   -err_free_alarm:
579   - if (fan_data->alarm)
580   - fan_alarm_free(fan_data);
  557 +err_remove:
  558 + sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_group);
581 559 return err;
582 560 }
583 561  
... ... @@ -586,11 +564,7 @@
586 564 struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
587 565  
588 566 hwmon_device_unregister(fan_data->hwmon_dev);
589   - device_remove_file(&pdev->dev, &dev_attr_name);
590   - if (fan_data->alarm)
591   - fan_alarm_free(fan_data);
592   - if (fan_data->ctrl)
593   - fan_ctrl_free(fan_data);
  567 + sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_group);
594 568  
595 569 return 0;
596 570 }