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/slab.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds.h>
19 #include <linux/leds-regulator.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
23 #define to_regulator_led(led_cdev) \
24 container_of(led_cdev, struct regulator_led, cdev)
26 struct regulator_led
{
27 struct led_classdev cdev
;
28 enum led_brightness value
;
31 struct work_struct work
;
33 struct regulator
*vcc
;
36 static inline int led_regulator_get_max_brightness(struct regulator
*supply
)
39 int voltage
= regulator_list_voltage(supply
, 0);
44 /* even if regulator can't change voltages,
45 * we still assume it can change status
46 * and the LED can be turned on and off.
48 ret
= regulator_set_voltage(supply
, voltage
, voltage
);
52 return regulator_count_voltages(supply
);
55 static int led_regulator_get_voltage(struct regulator
*supply
,
56 enum led_brightness brightness
)
61 return regulator_list_voltage(supply
, brightness
- 1);
65 static void regulator_led_enable(struct regulator_led
*led
)
72 ret
= regulator_enable(led
->vcc
);
74 dev_err(led
->cdev
.dev
, "Failed to enable vcc: %d\n", ret
);
81 static void regulator_led_disable(struct regulator_led
*led
)
88 ret
= regulator_disable(led
->vcc
);
90 dev_err(led
->cdev
.dev
, "Failed to disable vcc: %d\n", ret
);
97 static void regulator_led_set_value(struct regulator_led
*led
)
102 mutex_lock(&led
->mutex
);
104 if (led
->value
== LED_OFF
) {
105 regulator_led_disable(led
);
109 if (led
->cdev
.max_brightness
> 1) {
110 voltage
= led_regulator_get_voltage(led
->vcc
, led
->value
);
111 dev_dbg(led
->cdev
.dev
, "brightness: %d voltage: %d\n",
112 led
->value
, voltage
);
114 ret
= regulator_set_voltage(led
->vcc
, voltage
, voltage
);
116 dev_err(led
->cdev
.dev
, "Failed to set voltage %d: %d\n",
120 regulator_led_enable(led
);
123 mutex_unlock(&led
->mutex
);
126 static void led_work(struct work_struct
*work
)
128 struct regulator_led
*led
;
130 led
= container_of(work
, struct regulator_led
, work
);
131 regulator_led_set_value(led
);
134 static void regulator_led_brightness_set(struct led_classdev
*led_cdev
,
135 enum led_brightness value
)
137 struct regulator_led
*led
= to_regulator_led(led_cdev
);
140 schedule_work(&led
->work
);
143 static int regulator_led_probe(struct platform_device
*pdev
)
145 struct led_regulator_platform_data
*pdata
=
146 dev_get_platdata(&pdev
->dev
);
147 struct regulator_led
*led
;
148 struct regulator
*vcc
;
152 dev_err(&pdev
->dev
, "no platform data\n");
156 vcc
= regulator_get_exclusive(&pdev
->dev
, "vled");
158 dev_err(&pdev
->dev
, "Cannot get vcc for %s\n", pdata
->name
);
162 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
168 led
->cdev
.max_brightness
= led_regulator_get_max_brightness(vcc
);
169 if (pdata
->brightness
> led
->cdev
.max_brightness
) {
170 dev_err(&pdev
->dev
, "Invalid default brightness %d\n",
175 led
->value
= pdata
->brightness
;
177 led
->cdev
.brightness_set
= regulator_led_brightness_set
;
178 led
->cdev
.name
= pdata
->name
;
179 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
182 /* to handle correctly an already enabled regulator */
183 if (regulator_is_enabled(led
->vcc
))
186 mutex_init(&led
->mutex
);
187 INIT_WORK(&led
->work
, led_work
);
189 platform_set_drvdata(pdev
, led
);
191 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
193 cancel_work_sync(&led
->work
);
197 /* to expose the default value to userspace */
198 led
->cdev
.brightness
= led
->value
;
200 /* Set the default led status */
201 regulator_led_set_value(led
);
210 static int regulator_led_remove(struct platform_device
*pdev
)
212 struct regulator_led
*led
= platform_get_drvdata(pdev
);
214 led_classdev_unregister(&led
->cdev
);
215 cancel_work_sync(&led
->work
);
216 regulator_led_disable(led
);
217 regulator_put(led
->vcc
);
221 static struct platform_driver regulator_led_driver
= {
223 .name
= "leds-regulator",
224 .owner
= THIS_MODULE
,
226 .probe
= regulator_led_probe
,
227 .remove
= regulator_led_remove
,
230 module_platform_driver(regulator_led_driver
);
232 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
233 MODULE_DESCRIPTION("Regulator driven LED driver");
234 MODULE_LICENSE("GPL");
235 MODULE_ALIAS("platform:leds-regulator");