2 * leds-regulator.c - LED class driver for regulator driven LEDs.
4 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
6 * Inspired by leds-wm8350 driver.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/workqueue.h>
17 #include <linux/leds.h>
18 #include <linux/leds-regulator.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
22 #define to_regulator_led(led_cdev) \
23 container_of(led_cdev, struct regulator_led, cdev)
25 struct regulator_led
{
26 struct led_classdev cdev
;
27 enum led_brightness value
;
30 struct work_struct work
;
32 struct regulator
*vcc
;
35 static inline int led_regulator_get_max_brightness(struct regulator
*supply
)
38 int voltage
= regulator_list_voltage(supply
, 0);
43 /* even if regulator can't change voltages,
44 * we still assume it can change status
45 * and the LED can be turned on and off.
47 ret
= regulator_set_voltage(supply
, voltage
, voltage
);
51 return regulator_count_voltages(supply
);
54 static int led_regulator_get_voltage(struct regulator
*supply
,
55 enum led_brightness brightness
)
60 return regulator_list_voltage(supply
, brightness
- 1);
64 static void regulator_led_enable(struct regulator_led
*led
)
71 ret
= regulator_enable(led
->vcc
);
73 dev_err(led
->cdev
.dev
, "Failed to enable vcc: %d\n", ret
);
80 static void regulator_led_disable(struct regulator_led
*led
)
87 ret
= regulator_disable(led
->vcc
);
89 dev_err(led
->cdev
.dev
, "Failed to disable vcc: %d\n", ret
);
96 static void regulator_led_set_value(struct regulator_led
*led
)
101 mutex_lock(&led
->mutex
);
103 if (led
->value
== LED_OFF
) {
104 regulator_led_disable(led
);
108 if (led
->cdev
.max_brightness
> 1) {
109 voltage
= led_regulator_get_voltage(led
->vcc
, led
->value
);
110 dev_dbg(led
->cdev
.dev
, "brightness: %d voltage: %d\n",
111 led
->value
, voltage
);
113 ret
= regulator_set_voltage(led
->vcc
, voltage
, voltage
);
115 dev_err(led
->cdev
.dev
, "Failed to set voltage %d: %d\n",
119 regulator_led_enable(led
);
122 mutex_unlock(&led
->mutex
);
125 static void led_work(struct work_struct
*work
)
127 struct regulator_led
*led
;
129 led
= container_of(work
, struct regulator_led
, work
);
130 regulator_led_set_value(led
);
133 static void regulator_led_brightness_set(struct led_classdev
*led_cdev
,
134 enum led_brightness value
)
136 struct regulator_led
*led
= to_regulator_led(led_cdev
);
139 schedule_work(&led
->work
);
142 static int __devinit
regulator_led_probe(struct platform_device
*pdev
)
144 struct led_regulator_platform_data
*pdata
= pdev
->dev
.platform_data
;
145 struct regulator_led
*led
;
146 struct regulator
*vcc
;
150 dev_err(&pdev
->dev
, "no platform data\n");
154 vcc
= regulator_get_exclusive(&pdev
->dev
, "vled");
156 dev_err(&pdev
->dev
, "Cannot get vcc for %s\n", pdata
->name
);
160 led
= kzalloc(sizeof(*led
), GFP_KERNEL
);
166 led
->cdev
.max_brightness
= led_regulator_get_max_brightness(vcc
);
167 if (pdata
->brightness
> led
->cdev
.max_brightness
) {
168 dev_err(&pdev
->dev
, "Invalid default brightness %d\n",
173 led
->value
= pdata
->brightness
;
175 led
->cdev
.brightness_set
= regulator_led_brightness_set
;
176 led
->cdev
.name
= pdata
->name
;
177 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
180 mutex_init(&led
->mutex
);
181 INIT_WORK(&led
->work
, led_work
);
183 platform_set_drvdata(pdev
, led
);
185 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
187 cancel_work_sync(&led
->work
);
191 /* to expose the default value to userspace */
192 led
->cdev
.brightness
= led
->value
;
194 /* Set the default led status */
195 regulator_led_set_value(led
);
206 static int __devexit
regulator_led_remove(struct platform_device
*pdev
)
208 struct regulator_led
*led
= platform_get_drvdata(pdev
);
210 led_classdev_unregister(&led
->cdev
);
211 cancel_work_sync(&led
->work
);
212 regulator_led_disable(led
);
213 regulator_put(led
->vcc
);
218 static struct platform_driver regulator_led_driver
= {
220 .name
= "leds-regulator",
221 .owner
= THIS_MODULE
,
223 .probe
= regulator_led_probe
,
224 .remove
= __devexit_p(regulator_led_remove
),
227 static int __init
regulator_led_init(void)
229 return platform_driver_register(®ulator_led_driver
);
231 module_init(regulator_led_init
);
233 static void __exit
regulator_led_exit(void)
235 platform_driver_unregister(®ulator_led_driver
);
237 module_exit(regulator_led_exit
);
239 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
240 MODULE_DESCRIPTION("Regulator driven LED driver");
241 MODULE_LICENSE("GPL");
242 MODULE_ALIAS("platform:leds-regulator");