leds: convert Network Space v2 LED driver to devm_kzalloc() and cleanup error exit...
[deliverable/linux.git] / drivers / leds / leds-s3c24xx.c
CommitLineData
54bdc470
BD
1/* drivers/leds/leds-s3c24xx.c
2 *
3 * (c) 2006 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
6 *
7 * S3C24XX - LEDs GPIO driver
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
54bdc470
BD
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/leds.h>
ec976d6e 18#include <linux/gpio.h>
5a0e3ad6 19#include <linux/slab.h>
54f4dedb 20#include <linux/module.h>
54bdc470 21
a09e64fb
RK
22#include <mach/hardware.h>
23#include <mach/regs-gpio.h>
24#include <mach/leds-gpio.h>
54bdc470
BD
25
26/* our context */
27
28struct s3c24xx_gpio_led {
29 struct led_classdev cdev;
30 struct s3c24xx_led_platdata *pdata;
31};
32
33static inline struct s3c24xx_gpio_led *pdev_to_gpio(struct platform_device *dev)
34{
35 return platform_get_drvdata(dev);
36}
37
38static inline struct s3c24xx_gpio_led *to_gpio(struct led_classdev *led_cdev)
39{
40 return container_of(led_cdev, struct s3c24xx_gpio_led, cdev);
41}
42
43static void s3c24xx_led_set(struct led_classdev *led_cdev,
44 enum led_brightness value)
45{
46 struct s3c24xx_gpio_led *led = to_gpio(led_cdev);
47 struct s3c24xx_led_platdata *pd = led->pdata;
7d84f66f 48 int state = (value ? 1 : 0) ^ (pd->flags & S3C24XX_LEDF_ACTLOW);
54bdc470 49
4a739d55 50 /* there will be a short delay between setting the output and
54bdc470
BD
51 * going from output to input when using tristate. */
52
7d84f66f 53 gpio_set_value(pd->gpio, state);
54bdc470 54
7d84f66f
SN
55 if (pd->flags & S3C24XX_LEDF_TRISTATE) {
56 if (value)
57 gpio_direction_output(pd->gpio, state);
58 else
59 gpio_direction_input(pd->gpio);
60 }
54bdc470
BD
61}
62
63static int s3c24xx_led_remove(struct platform_device *dev)
64{
65 struct s3c24xx_gpio_led *led = pdev_to_gpio(dev);
66
67 led_classdev_unregister(&led->cdev);
7d84f66f 68 gpio_free(led->pdata->gpio);
54bdc470
BD
69
70 return 0;
71}
72
73static int s3c24xx_led_probe(struct platform_device *dev)
74{
75 struct s3c24xx_led_platdata *pdata = dev->dev.platform_data;
76 struct s3c24xx_gpio_led *led;
77 int ret;
78
7e97b581
SN
79 led = devm_kzalloc(&dev->dev, sizeof(struct s3c24xx_gpio_led),
80 GFP_KERNEL);
54bdc470
BD
81 if (led == NULL) {
82 dev_err(&dev->dev, "No memory for device\n");
83 return -ENOMEM;
84 }
85
86 platform_set_drvdata(dev, led);
87
88 led->cdev.brightness_set = s3c24xx_led_set;
89 led->cdev.default_trigger = pdata->def_trigger;
90 led->cdev.name = pdata->name;
859cb7f2 91 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
54bdc470
BD
92
93 led->pdata = pdata;
94
7d84f66f
SN
95 ret = gpio_request(pdata->gpio, "S3C24XX_LED");
96 if (ret < 0)
97 return ret;
98
54bdc470
BD
99 /* no point in having a pull-up if we are always driving */
100
7d84f66f
SN
101 s3c_gpio_setpull(pdata->gpio, S3C_GPIO_PULL_NONE);
102
103 if (pdata->flags & S3C24XX_LEDF_TRISTATE)
104 gpio_direction_input(pdata->gpio);
105 else
106 gpio_direction_output(pdata->gpio,
107 pdata->flags & S3C24XX_LEDF_ACTLOW ? 1 : 0);
54bdc470
BD
108
109 /* register our new led device */
110
111 ret = led_classdev_register(&dev->dev, &led->cdev);
7e97b581 112 if (ret < 0)
54bdc470 113 dev_err(&dev->dev, "led_classdev_register failed\n");
54bdc470 114
1522d02e 115 return ret;
54bdc470
BD
116}
117
54bdc470
BD
118static struct platform_driver s3c24xx_led_driver = {
119 .probe = s3c24xx_led_probe,
120 .remove = s3c24xx_led_remove,
54bdc470
BD
121 .driver = {
122 .name = "s3c24xx_led",
123 .owner = THIS_MODULE,
124 },
125};
126
892a8843 127module_platform_driver(s3c24xx_led_driver);
54bdc470
BD
128
129MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
130MODULE_DESCRIPTION("S3C24XX LED driver");
131MODULE_LICENSE("GPL");
3c4ded97 132MODULE_ALIAS("platform:s3c24xx_led");
This page took 0.795639 seconds and 5 git commands to generate.