Commit 9bfd7d9e authored by Uwe Kleine-König's avatar Uwe Kleine-König Committed by Jacek Anaszewski

leds: gpio trigger: simplifications from core changes

The trigger core learned error handling for the activate callback and
to handle device attributes. Also make use of the module_led_trigger()
helper to simplify trigger registration.
Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Acked-by: default avatarPavel Machek <pavel@ucw.cz>
Signed-off-by: default avatarJacek Anaszewski <jacek.anaszewski@gmail.com>
parent e4786ba0
...@@ -28,7 +28,7 @@ struct gpio_trig_data { ...@@ -28,7 +28,7 @@ struct gpio_trig_data {
static irqreturn_t gpio_trig_irq(int irq, void *_led) static irqreturn_t gpio_trig_irq(int irq, void *_led)
{ {
struct led_classdev *led = _led; struct led_classdev *led = _led;
struct gpio_trig_data *gpio_data = led->trigger_data; struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
int tmp; int tmp;
tmp = gpio_get_value_cansleep(gpio_data->gpio); tmp = gpio_get_value_cansleep(gpio_data->gpio);
...@@ -51,8 +51,7 @@ static irqreturn_t gpio_trig_irq(int irq, void *_led) ...@@ -51,8 +51,7 @@ static irqreturn_t gpio_trig_irq(int irq, void *_led)
static ssize_t gpio_trig_brightness_show(struct device *dev, static ssize_t gpio_trig_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
struct gpio_trig_data *gpio_data = led->trigger_data;
return sprintf(buf, "%u\n", gpio_data->desired_brightness); return sprintf(buf, "%u\n", gpio_data->desired_brightness);
} }
...@@ -60,8 +59,7 @@ static ssize_t gpio_trig_brightness_show(struct device *dev, ...@@ -60,8 +59,7 @@ static ssize_t gpio_trig_brightness_show(struct device *dev,
static ssize_t gpio_trig_brightness_store(struct device *dev, static ssize_t gpio_trig_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n) struct device_attribute *attr, const char *buf, size_t n)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
struct gpio_trig_data *gpio_data = led->trigger_data;
unsigned desired_brightness; unsigned desired_brightness;
int ret; int ret;
...@@ -81,8 +79,7 @@ static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show, ...@@ -81,8 +79,7 @@ static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
static ssize_t gpio_trig_inverted_show(struct device *dev, static ssize_t gpio_trig_inverted_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
struct gpio_trig_data *gpio_data = led->trigger_data;
return sprintf(buf, "%u\n", gpio_data->inverted); return sprintf(buf, "%u\n", gpio_data->inverted);
} }
...@@ -90,8 +87,8 @@ static ssize_t gpio_trig_inverted_show(struct device *dev, ...@@ -90,8 +87,8 @@ static ssize_t gpio_trig_inverted_show(struct device *dev,
static ssize_t gpio_trig_inverted_store(struct device *dev, static ssize_t gpio_trig_inverted_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n) struct device_attribute *attr, const char *buf, size_t n)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct led_classdev *led = led_trigger_get_led(dev);
struct gpio_trig_data *gpio_data = led->trigger_data; struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
unsigned long inverted; unsigned long inverted;
int ret; int ret;
...@@ -115,8 +112,7 @@ static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show, ...@@ -115,8 +112,7 @@ static DEVICE_ATTR(inverted, 0644, gpio_trig_inverted_show,
static ssize_t gpio_trig_gpio_show(struct device *dev, static ssize_t gpio_trig_gpio_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
struct gpio_trig_data *gpio_data = led->trigger_data;
return sprintf(buf, "%u\n", gpio_data->gpio); return sprintf(buf, "%u\n", gpio_data->gpio);
} }
...@@ -124,8 +120,8 @@ static ssize_t gpio_trig_gpio_show(struct device *dev, ...@@ -124,8 +120,8 @@ static ssize_t gpio_trig_gpio_show(struct device *dev,
static ssize_t gpio_trig_gpio_store(struct device *dev, static ssize_t gpio_trig_gpio_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n) struct device_attribute *attr, const char *buf, size_t n)
{ {
struct led_classdev *led = dev_get_drvdata(dev); struct led_classdev *led = led_trigger_get_led(dev);
struct gpio_trig_data *gpio_data = led->trigger_data; struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
unsigned gpio; unsigned gpio;
int ret; int ret;
...@@ -162,77 +158,44 @@ static ssize_t gpio_trig_gpio_store(struct device *dev, ...@@ -162,77 +158,44 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
} }
static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store); static DEVICE_ATTR(gpio, 0644, gpio_trig_gpio_show, gpio_trig_gpio_store);
static struct attribute *gpio_trig_attrs[] = {
&dev_attr_desired_brightness.attr,
&dev_attr_inverted.attr,
&dev_attr_gpio.attr,
NULL
};
ATTRIBUTE_GROUPS(gpio_trig);
static int gpio_trig_activate(struct led_classdev *led) static int gpio_trig_activate(struct led_classdev *led)
{ {
struct gpio_trig_data *gpio_data; struct gpio_trig_data *gpio_data;
int ret;
gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL); gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL);
if (!gpio_data) if (!gpio_data)
return 0; return -ENOMEM;
ret = device_create_file(led->dev, &dev_attr_gpio);
if (ret)
goto err_gpio;
ret = device_create_file(led->dev, &dev_attr_inverted);
if (ret)
goto err_inverted;
ret = device_create_file(led->dev, &dev_attr_desired_brightness);
if (ret)
goto err_brightness;
gpio_data->led = led; gpio_data->led = led;
led->trigger_data = gpio_data; led_set_trigger_data(led, gpio_data);
led->activated = true;
return 0;
err_brightness:
device_remove_file(led->dev, &dev_attr_inverted);
err_inverted:
device_remove_file(led->dev, &dev_attr_gpio);
err_gpio:
kfree(gpio_data);
return 0; return 0;
} }
static void gpio_trig_deactivate(struct led_classdev *led) static void gpio_trig_deactivate(struct led_classdev *led)
{ {
struct gpio_trig_data *gpio_data = led->trigger_data; struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
if (led->activated) { if (gpio_data->gpio != 0)
device_remove_file(led->dev, &dev_attr_gpio); free_irq(gpio_to_irq(gpio_data->gpio), led);
device_remove_file(led->dev, &dev_attr_inverted); kfree(gpio_data);
device_remove_file(led->dev, &dev_attr_desired_brightness);
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
kfree(gpio_data);
led->activated = false;
}
} }
static struct led_trigger gpio_led_trigger = { static struct led_trigger gpio_led_trigger = {
.name = "gpio", .name = "gpio",
.activate = gpio_trig_activate, .activate = gpio_trig_activate,
.deactivate = gpio_trig_deactivate, .deactivate = gpio_trig_deactivate,
.groups = gpio_trig_groups,
}; };
module_led_trigger(gpio_led_trigger);
static int __init gpio_trig_init(void)
{
return led_trigger_register(&gpio_led_trigger);
}
module_init(gpio_trig_init);
static void __exit gpio_trig_exit(void)
{
led_trigger_unregister(&gpio_led_trigger);
}
module_exit(gpio_trig_exit);
MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>");
MODULE_DESCRIPTION("GPIO LED trigger"); MODULE_DESCRIPTION("GPIO LED trigger");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment