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>
This commit is contained in:
parent
4ce5b1fe31
commit
c81cc5a4c1
@ -105,10 +105,6 @@ static int fan_alarm_init(struct gpio_fan_data *fan_data,
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
err = device_create_file(&pdev->dev, &dev_attr_fan1_alarm);
|
|
||||||
if (err)
|
|
||||||
return err;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the alarm GPIO don't support interrupts, just leave
|
* If the alarm GPIO don't support interrupts, just leave
|
||||||
* without initializing the fail notification support.
|
* without initializing the fail notification support.
|
||||||
@ -121,23 +117,9 @@ static int fan_alarm_init(struct gpio_fan_data *fan_data,
|
|||||||
irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
|
irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
|
||||||
err = devm_request_irq(&pdev->dev, alarm_irq, fan_alarm_irq_handler,
|
err = devm_request_irq(&pdev->dev, alarm_irq, fan_alarm_irq_handler,
|
||||||
IRQF_SHARED, "GPIO fan alarm", fan_data);
|
IRQF_SHARED, "GPIO fan alarm", fan_data);
|
||||||
if (err)
|
|
||||||
goto err_free_sysfs;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
err_free_sysfs:
|
|
||||||
device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fan_alarm_free(struct gpio_fan_data *fan_data)
|
|
||||||
{
|
|
||||||
struct platform_device *pdev = fan_data->pdev;
|
|
||||||
|
|
||||||
device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Control GPIOs.
|
* Control GPIOs.
|
||||||
*/
|
*/
|
||||||
@ -327,6 +309,12 @@ exit_unlock:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ssize_t show_name(struct device *dev,
|
||||||
|
struct device_attribute *attr, char *buf)
|
||||||
|
{
|
||||||
|
return sprintf(buf, "gpio-fan\n");
|
||||||
|
}
|
||||||
|
|
||||||
static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
|
static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
|
||||||
static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
|
static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
|
||||||
show_pwm_enable, set_pwm_enable);
|
show_pwm_enable, set_pwm_enable);
|
||||||
@ -336,8 +324,26 @@ static DEVICE_ATTR(fan1_max, S_IRUGO, show_rpm_max, NULL);
|
|||||||
static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
|
static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
|
||||||
static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
|
static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
|
||||||
|
|
||||||
static struct attribute *gpio_fan_ctrl_attributes[] = {
|
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
|
||||||
&dev_attr_pwm1.attr,
|
|
||||||
|
static umode_t gpio_fan_is_visible(struct kobject *kobj,
|
||||||
|
struct attribute *attr, int index)
|
||||||
|
{
|
||||||
|
struct device *dev = container_of(kobj, struct device, kobj);
|
||||||
|
struct gpio_fan_data *data = dev_get_drvdata(dev);
|
||||||
|
|
||||||
|
if (index == 1 && !data->alarm)
|
||||||
|
return 0;
|
||||||
|
if (index > 1 && !data->ctrl)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return attr->mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct attribute *gpio_fan_attributes[] = {
|
||||||
|
&dev_attr_name.attr,
|
||||||
|
&dev_attr_fan1_alarm.attr, /* 1 */
|
||||||
|
&dev_attr_pwm1.attr, /* 2 */
|
||||||
&dev_attr_pwm1_enable.attr,
|
&dev_attr_pwm1_enable.attr,
|
||||||
&dev_attr_pwm1_mode.attr,
|
&dev_attr_pwm1_mode.attr,
|
||||||
&dev_attr_fan1_input.attr,
|
&dev_attr_fan1_input.attr,
|
||||||
@ -347,8 +353,9 @@ static struct attribute *gpio_fan_ctrl_attributes[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct attribute_group gpio_fan_ctrl_group = {
|
static const struct attribute_group gpio_fan_group = {
|
||||||
.attrs = gpio_fan_ctrl_attributes,
|
.attrs = gpio_fan_attributes,
|
||||||
|
.is_visible = gpio_fan_is_visible,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int fan_ctrl_init(struct gpio_fan_data *fan_data,
|
static int fan_ctrl_init(struct gpio_fan_data *fan_data,
|
||||||
@ -379,30 +386,9 @@ static int fan_ctrl_init(struct gpio_fan_data *fan_data,
|
|||||||
if (fan_data->speed_index < 0)
|
if (fan_data->speed_index < 0)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
||||||
err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
|
return 0;
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fan_ctrl_free(struct gpio_fan_data *fan_data)
|
|
||||||
{
|
|
||||||
struct platform_device *pdev = fan_data->pdev;
|
|
||||||
|
|
||||||
sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Platform driver.
|
|
||||||
*/
|
|
||||||
|
|
||||||
static ssize_t show_name(struct device *dev,
|
|
||||||
struct device_attribute *attr, char *buf)
|
|
||||||
{
|
|
||||||
return sprintf(buf, "gpio-fan\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef CONFIG_OF_GPIO
|
#ifdef CONFIG_OF_GPIO
|
||||||
/*
|
/*
|
||||||
* Translate OpenFirmware node properties into platform_data
|
* Translate OpenFirmware node properties into platform_data
|
||||||
@ -546,38 +532,30 @@ static int gpio_fan_probe(struct platform_device *pdev)
|
|||||||
|
|
||||||
/* Configure control GPIOs if available. */
|
/* Configure control GPIOs if available. */
|
||||||
if (pdata->ctrl && pdata->num_ctrl > 0) {
|
if (pdata->ctrl && pdata->num_ctrl > 0) {
|
||||||
if (!pdata->speed || pdata->num_speed <= 1) {
|
if (!pdata->speed || pdata->num_speed <= 1)
|
||||||
err = -EINVAL;
|
return -EINVAL;
|
||||||
goto err_free_alarm;
|
|
||||||
}
|
|
||||||
err = fan_ctrl_init(fan_data, pdata);
|
err = fan_ctrl_init(fan_data, pdata);
|
||||||
if (err)
|
if (err)
|
||||||
goto err_free_alarm;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = device_create_file(&pdev->dev, &dev_attr_name);
|
err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_group);
|
||||||
if (err)
|
if (err)
|
||||||
goto err_free_ctrl;
|
return err;
|
||||||
|
|
||||||
/* Make this driver part of hwmon class. */
|
/* Make this driver part of hwmon class. */
|
||||||
fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
|
fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
|
||||||
if (IS_ERR(fan_data->hwmon_dev)) {
|
if (IS_ERR(fan_data->hwmon_dev)) {
|
||||||
err = PTR_ERR(fan_data->hwmon_dev);
|
err = PTR_ERR(fan_data->hwmon_dev);
|
||||||
goto err_remove_name;
|
goto err_remove;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev_info(&pdev->dev, "GPIO fan initialized\n");
|
dev_info(&pdev->dev, "GPIO fan initialized\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err_remove_name:
|
err_remove:
|
||||||
device_remove_file(&pdev->dev, &dev_attr_name);
|
sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_group);
|
||||||
err_free_ctrl:
|
|
||||||
if (fan_data->ctrl)
|
|
||||||
fan_ctrl_free(fan_data);
|
|
||||||
err_free_alarm:
|
|
||||||
if (fan_data->alarm)
|
|
||||||
fan_alarm_free(fan_data);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -586,11 +564,7 @@ static int gpio_fan_remove(struct platform_device *pdev)
|
|||||||
struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
|
struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
|
||||||
|
|
||||||
hwmon_device_unregister(fan_data->hwmon_dev);
|
hwmon_device_unregister(fan_data->hwmon_dev);
|
||||||
device_remove_file(&pdev->dev, &dev_attr_name);
|
sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_group);
|
||||||
if (fan_data->alarm)
|
|
||||||
fan_alarm_free(fan_data);
|
|
||||||
if (fan_data->ctrl)
|
|
||||||
fan_ctrl_free(fan_data);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user