[adm5120] fix memory size detection (#2244)
[openwrt.git] / target / linux / adm5120 / files / drivers / leds / leds-gpio.c
1 /*
2 * $Id$
3 *
4 * Driver for LEDs connected to GPIO lines
5 *
6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
8 *
9 * This file was derived from:
10 * /drivers/led/leds-s3c24xx.c
11 * (c) 2006 Simtec Electronics, Ben Dooks <ben@simtec.co.uk>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the
25 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 *
28 */
29
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/platform_device.h>
33 #include <linux/leds.h>
34 #include <linux/err.h>
35
36 #include <linux/gpio_leds.h>
37
38 #include <asm/io.h>
39 #include <asm/gpio.h>
40
41 #define DRV_NAME "gpio-led"
42 #define DRV_DESC "GPIO LEDs driver"
43
44 struct gpio_led_device {
45 struct led_classdev cdev;
46 struct gpio_led_platform_data *pdata;
47 };
48
49 static inline struct gpio_led_device *pdev_to_led(struct platform_device *dev)
50 {
51 return platform_get_drvdata(dev);
52 }
53
54 static inline struct gpio_led_device *class_to_led(struct led_classdev *led_cdev)
55 {
56 return container_of(led_cdev, struct gpio_led_device, cdev);
57 }
58
59 static void gpio_led_set(struct led_classdev *led_cdev,
60 enum led_brightness brightness)
61 {
62 struct gpio_led_device *led;
63 struct gpio_led_platform_data *pdata;
64
65 led = class_to_led(led_cdev);
66 pdata = led->pdata;
67
68 switch (brightness) {
69 case LED_FULL:
70 gpio_direction_output(pdata->gpio, pdata->value_on);
71 break;
72 case LED_OFF:
73 gpio_direction_output(pdata->gpio, pdata->value_off);
74 break;
75 default:
76 gpio_direction_output(pdata->gpio, brightness);
77 break;
78 }
79 }
80
81 static int __devinit gpio_led_probe(struct platform_device *dev)
82 {
83 struct gpio_led_platform_data *pdata;
84 struct gpio_led_device *led;
85 int ret;
86
87 pdata = dev->dev.platform_data;
88 if (pdata == NULL) {
89 dev_err(&dev->dev, "no platform data, id=%d\n", dev->id);
90 ret = -EINVAL;
91 goto err;
92 }
93
94 if (pdata->name == NULL) {
95 dev_err(&dev->dev, "no led name specified\n");
96 ret = -EINVAL;
97 goto err;
98 }
99
100 ret = gpio_request(pdata->gpio, pdata->name);
101 if (ret) {
102 dev_err(&dev->dev, "gpio_request failed\n");
103 goto err;
104 }
105
106 led = kzalloc(sizeof(*led), GFP_KERNEL);
107 if (led == NULL) {
108 dev_err(&dev->dev, "no memory for device");
109 ret = -ENOMEM;
110 goto err_free_gpio;
111 }
112
113 platform_set_drvdata(dev, led);
114 led->pdata = pdata;
115 led->cdev.name = pdata->name;
116 led->cdev.brightness_set = gpio_led_set;
117 #ifdef CONFIG_LEDS_TRIGGERS
118 led->cdev.default_trigger = pdata->trigger;
119 #endif
120
121 ret = led_classdev_register(&dev->dev, &led->cdev);
122 if (ret < 0) {
123 dev_err(&dev->dev, "led_classdev_register failed");
124 goto err_free_led;
125 }
126
127 return 0;
128
129 err_free_led:
130 kfree(led);
131 err_free_gpio:
132 gpio_free(pdata->gpio);
133 err:
134 return ret;
135 }
136
137 static int __devexit gpio_led_remove(struct platform_device *dev)
138 {
139 struct gpio_led_device *led;
140 struct gpio_led_platform_data *pdata;
141
142 pdata = dev->dev.platform_data;
143
144 led = pdev_to_led(dev);
145 led_classdev_unregister(&led->cdev);
146 kfree(led);
147
148 gpio_free(pdata->gpio);
149
150 return 0;
151 }
152
153 #ifdef CONFIG_PM
154 static int gpio_led_suspend(struct platform_device *dev,
155 pm_message_t state)
156 {
157 struct gpio_led_device *led;
158
159 led = pdev_to_led(dev);
160 led_classdev_suspend(&led->cdev);
161
162 return 0;
163 }
164
165 static int gpio_led_resume(struct platform_device *dev)
166 {
167 struct gpio_led_device *led;
168
169 led = pdev_to_led(dev);
170 led_classdev_resume(&led->cdev);
171
172 return 0;
173 }
174 #endif /* CONFIG_PM */
175
176 static struct platform_driver gpio_led_driver = {
177 .probe = gpio_led_probe,
178 .remove = __devexit_p(gpio_led_remove),
179 #ifdef CONFIG_PM
180 .suspend = gpio_led_suspend,
181 .resume = gpio_led_resume,
182 #endif
183 .driver = {
184 .name = DRV_NAME,
185 .owner = THIS_MODULE,
186 },
187 };
188
189 static int __init gpio_led_init(void)
190 {
191 int ret;
192
193 ret = platform_driver_register(&gpio_led_driver);
194 if (ret)
195 printk(KERN_ALERT DRV_DESC " register failed\n");
196 else
197 printk(KERN_INFO DRV_DESC " registered\n");
198
199 return ret;
200 }
201
202 static void __exit gpio_led_exit(void)
203 {
204 platform_driver_unregister(&gpio_led_driver);
205 }
206
207 module_init(gpio_led_init);
208 module_exit(gpio_led_exit);
209
210 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
211 MODULE_DESCRIPTION(DRV_DESC);
212 MODULE_LICENSE("GPL");
This page took 0.054979 seconds and 5 git commands to generate.