1 // SPDX-License-Identifier: GPL-2.0-only
3 * leds-regulator.c - LED class driver for regulator driven LEDs.
5 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
7 * Inspired by leds-wm8350 driver.
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/leds.h>
15 #include <linux/leds-regulator.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
19 #define to_regulator_led(led_cdev) \
20 container_of(led_cdev, struct regulator_led, cdev)
22 struct regulator_led
{
23 struct led_classdev cdev
;
27 struct regulator
*vcc
;
30 static inline int led_regulator_get_max_brightness(struct regulator
*supply
)
33 int voltage
= regulator_list_voltage(supply
, 0);
38 /* even if regulator can't change voltages,
39 * we still assume it can change status
40 * and the LED can be turned on and off.
42 ret
= regulator_set_voltage(supply
, voltage
, voltage
);
46 return regulator_count_voltages(supply
);
49 static int led_regulator_get_voltage(struct regulator
*supply
,
50 enum led_brightness brightness
)
55 return regulator_list_voltage(supply
, brightness
- 1);
59 static void regulator_led_enable(struct regulator_led
*led
)
66 ret
= regulator_enable(led
->vcc
);
68 dev_err(led
->cdev
.dev
, "Failed to enable vcc: %d\n", ret
);
75 static void regulator_led_disable(struct regulator_led
*led
)
82 ret
= regulator_disable(led
->vcc
);
84 dev_err(led
->cdev
.dev
, "Failed to disable vcc: %d\n", ret
);
91 static int regulator_led_brightness_set(struct led_classdev
*led_cdev
,
92 enum led_brightness value
)
94 struct regulator_led
*led
= to_regulator_led(led_cdev
);
98 mutex_lock(&led
->mutex
);
100 if (value
== LED_OFF
) {
101 regulator_led_disable(led
);
105 if (led
->cdev
.max_brightness
> 1) {
106 voltage
= led_regulator_get_voltage(led
->vcc
, value
);
107 dev_dbg(led
->cdev
.dev
, "brightness: %d voltage: %d\n",
110 ret
= regulator_set_voltage(led
->vcc
, voltage
, voltage
);
112 dev_err(led
->cdev
.dev
, "Failed to set voltage %d: %d\n",
116 regulator_led_enable(led
);
119 mutex_unlock(&led
->mutex
);
123 static int regulator_led_probe(struct platform_device
*pdev
)
125 struct led_regulator_platform_data
*pdata
=
126 dev_get_platdata(&pdev
->dev
);
127 struct device
*dev
= &pdev
->dev
;
128 struct led_init_data init_data
= {};
129 struct regulator_led
*led
;
130 struct regulator
*vcc
;
133 vcc
= devm_regulator_get_exclusive(dev
, "vled");
135 dev_err(dev
, "Cannot get vcc\n");
139 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
143 init_data
.fwnode
= dev
->fwnode
;
145 led
->cdev
.max_brightness
= led_regulator_get_max_brightness(vcc
);
146 /* Legacy platform data label assignment */
148 if (pdata
->brightness
> led
->cdev
.max_brightness
) {
149 dev_err(dev
, "Invalid default brightness %d\n",
153 led
->cdev
.brightness
= pdata
->brightness
;
154 init_data
.default_label
= pdata
->name
;
157 led
->cdev
.brightness_set_blocking
= regulator_led_brightness_set
;
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_ext(dev
, &led
->cdev
, &init_data
);
176 static void regulator_led_remove(struct platform_device
*pdev
)
178 struct regulator_led
*led
= platform_get_drvdata(pdev
);
180 led_classdev_unregister(&led
->cdev
);
181 regulator_led_disable(led
);
184 static const struct of_device_id regulator_led_of_match
[] = {
185 { .compatible
= "regulator-led", },
188 MODULE_DEVICE_TABLE(of
, regulator_led_of_match
);
190 static struct platform_driver regulator_led_driver
= {
192 .name
= "leds-regulator",
193 .of_match_table
= regulator_led_of_match
,
195 .probe
= regulator_led_probe
,
196 .remove_new
= 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");