4 * Driver for LEDs connected to GPIO lines
6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
9 * This file was derived from:
10 * /drivers/led/leds-s3c24xx.c
11 * (c) 2006 Simtec Electronics, Ben Dooks <ben@simtec.co.uk>
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.
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.
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.
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>
36 #include <linux/gpio_leds.h>
41 #define DRV_NAME "gpio-led"
42 #define DRV_DESC "GPIO LEDs driver"
44 struct gpio_led_device
{
45 struct led_classdev cdev
;
46 struct gpio_led_platform_data
*pdata
;
49 static inline struct gpio_led_device
*pdev_to_led(struct platform_device
*dev
)
51 return platform_get_drvdata(dev
);
54 static inline struct gpio_led_device
*class_to_led(struct led_classdev
*led_cdev
)
56 return container_of(led_cdev
, struct gpio_led_device
, cdev
);
59 static void gpio_led_set(struct led_classdev
*led_cdev
,
60 enum led_brightness brightness
)
62 struct gpio_led_device
*led
;
63 struct gpio_led_platform_data
*pdata
;
65 led
= class_to_led(led_cdev
);
70 gpio_direction_output(pdata
->gpio
, pdata
->value_on
);
73 gpio_direction_output(pdata
->gpio
, pdata
->value_off
);
76 gpio_direction_output(pdata
->gpio
, brightness
);
81 static int __devinit
gpio_led_probe(struct platform_device
*dev
)
83 struct gpio_led_platform_data
*pdata
;
84 struct gpio_led_device
*led
;
87 pdata
= dev
->dev
.platform_data
;
89 dev_err(&dev
->dev
, "no platform data, id=%d\n", dev
->id
);
94 if (pdata
->name
== NULL
) {
95 dev_err(&dev
->dev
, "no led name specified\n");
100 ret
= gpio_request(pdata
->gpio
, pdata
->name
);
102 dev_err(&dev
->dev
, "gpio_request failed\n");
106 led
= kzalloc(sizeof(*led
), GFP_KERNEL
);
108 dev_err(&dev
->dev
, "no memory for device");
113 platform_set_drvdata(dev
, led
);
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
;
121 ret
= led_classdev_register(&dev
->dev
, &led
->cdev
);
123 dev_err(&dev
->dev
, "led_classdev_register failed");
132 gpio_free(pdata
->gpio
);
137 static int __devexit
gpio_led_remove(struct platform_device
*dev
)
139 struct gpio_led_device
*led
;
140 struct gpio_led_platform_data
*pdata
;
142 pdata
= dev
->dev
.platform_data
;
144 led
= pdev_to_led(dev
);
145 led_classdev_unregister(&led
->cdev
);
148 gpio_free(pdata
->gpio
);
154 static int gpio_led_suspend(struct platform_device
*dev
,
157 struct gpio_led_device
*led
;
159 led
= pdev_to_led(dev
);
160 led_classdev_suspend(&led
->cdev
);
165 static int gpio_led_resume(struct platform_device
*dev
)
167 struct gpio_led_device
*led
;
169 led
= pdev_to_led(dev
);
170 led_classdev_resume(&led
->cdev
);
174 #endif /* CONFIG_PM */
176 static struct platform_driver gpio_led_driver
= {
177 .probe
= gpio_led_probe
,
178 .remove
= __devexit_p(gpio_led_remove
),
180 .suspend
= gpio_led_suspend
,
181 .resume
= gpio_led_resume
,
185 .owner
= THIS_MODULE
,
189 static int __init
gpio_led_init(void)
193 ret
= platform_driver_register(&gpio_led_driver
);
195 printk(KERN_ALERT DRV_DESC
" register failed\n");
197 printk(KERN_INFO DRV_DESC
" registered\n");
202 static void __exit
gpio_led_exit(void)
204 platform_driver_unregister(&gpio_led_driver
);
207 module_init(gpio_led_init
);
208 module_exit(gpio_led_exit
);
210 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
211 MODULE_DESCRIPTION(DRV_DESC
);
212 MODULE_LICENSE("GPL");