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/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
;
30 struct regulator
*vcc
;
33 static inline int led_regulator_get_max_brightness(struct regulator
*supply
)
36 int voltage
= regulator_list_voltage(supply
, 0);
41 /* even if regulator can't change voltages,
42 * we still assume it can change status
43 * and the LED can be turned on and off.
45 ret
= regulator_set_voltage(supply
, voltage
, voltage
);
49 return regulator_count_voltages(supply
);
52 static int led_regulator_get_voltage(struct regulator
*supply
,
53 enum led_brightness brightness
)
58 return regulator_list_voltage(supply
, brightness
- 1);
62 static void regulator_led_enable(struct regulator_led
*led
)
69 ret
= regulator_enable(led
->vcc
);
71 dev_err(led
->cdev
.dev
, "Failed to enable vcc: %d\n", ret
);
78 static void regulator_led_disable(struct regulator_led
*led
)
85 ret
= regulator_disable(led
->vcc
);
87 dev_err(led
->cdev
.dev
, "Failed to disable vcc: %d\n", ret
);
94 static int regulator_led_brightness_set(struct led_classdev
*led_cdev
,
95 enum led_brightness value
)
97 struct regulator_led
*led
= to_regulator_led(led_cdev
);
101 mutex_lock(&led
->mutex
);
103 if (value
== LED_OFF
) {
104 regulator_led_disable(led
);
108 if (led
->cdev
.max_brightness
> 1) {
109 voltage
= led_regulator_get_voltage(led
->vcc
, value
);
110 dev_dbg(led
->cdev
.dev
, "brightness: %d voltage: %d\n",
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
);
126 static int regulator_led_probe(struct platform_device
*pdev
)
128 struct led_regulator_platform_data
*pdata
=
129 dev_get_platdata(&pdev
->dev
);
130 struct regulator_led
*led
;
131 struct regulator
*vcc
;
135 dev_err(&pdev
->dev
, "no platform data\n");
139 vcc
= devm_regulator_get_exclusive(&pdev
->dev
, "vled");
141 dev_err(&pdev
->dev
, "Cannot get vcc for %s\n", pdata
->name
);
145 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
149 led
->cdev
.max_brightness
= led_regulator_get_max_brightness(vcc
);
150 if (pdata
->brightness
> led
->cdev
.max_brightness
) {
151 dev_err(&pdev
->dev
, "Invalid default brightness %d\n",
156 led
->cdev
.brightness_set_blocking
= regulator_led_brightness_set
;
157 led
->cdev
.name
= pdata
->name
;
158 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
161 /* to handle correctly an already enabled regulator */
162 if (regulator_is_enabled(led
->vcc
))
165 mutex_init(&led
->mutex
);
167 platform_set_drvdata(pdev
, led
);
169 ret
= led_classdev_register(&pdev
->dev
, &led
->cdev
);
173 /* to expose the default value to userspace */
174 led
->cdev
.brightness
= pdata
->brightness
;
176 /* Set the default led status */
177 regulator_led_brightness_set(&led
->cdev
, led
->cdev
.brightness
);
182 static int regulator_led_remove(struct platform_device
*pdev
)
184 struct regulator_led
*led
= platform_get_drvdata(pdev
);
186 led_classdev_unregister(&led
->cdev
);
187 regulator_led_disable(led
);
191 static struct platform_driver regulator_led_driver
= {
193 .name
= "leds-regulator",
195 .probe
= regulator_led_probe
,
196 .remove
= regulator_led_remove
,
199 module_platform_driver(regulator_led_driver
);
201 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
202 MODULE_DESCRIPTION("Regulator driven LED driver");
203 MODULE_LICENSE("GPL");
204 MODULE_ALIAS("platform:leds-regulator");