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/err.h>
12 #include <linux/slab.h>
13 #include <linux/leds.h>
14 #include <linux/leds-regulator.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
18 #define to_regulator_led(led_cdev) \
19 container_of(led_cdev, struct regulator_led, cdev)
21 struct regulator_led {
22 struct led_classdev cdev;
26 struct regulator *vcc;
29 static inline int led_regulator_get_max_brightness(struct regulator *supply)
32 int voltage = regulator_list_voltage(supply, 0);
37 /* even if regulator can't change voltages,
38 * we still assume it can change status
39 * and the LED can be turned on and off.
41 ret = regulator_set_voltage(supply, voltage, voltage);
45 return regulator_count_voltages(supply);
48 static int led_regulator_get_voltage(struct regulator *supply,
49 enum led_brightness brightness)
54 return regulator_list_voltage(supply, brightness - 1);
58 static void regulator_led_enable(struct regulator_led *led)
65 ret = regulator_enable(led->vcc);
67 dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
74 static void regulator_led_disable(struct regulator_led *led)
81 ret = regulator_disable(led->vcc);
83 dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
90 static int regulator_led_brightness_set(struct led_classdev *led_cdev,
91 enum led_brightness value)
93 struct regulator_led *led = to_regulator_led(led_cdev);
97 mutex_lock(&led->mutex);
99 if (value == LED_OFF) {
100 regulator_led_disable(led);
104 if (led->cdev.max_brightness > 1) {
105 voltage = led_regulator_get_voltage(led->vcc, value);
106 dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
109 ret = regulator_set_voltage(led->vcc, voltage, voltage);
111 dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
115 regulator_led_enable(led);
118 mutex_unlock(&led->mutex);
122 static int regulator_led_probe(struct platform_device *pdev)
124 struct led_regulator_platform_data *pdata =
125 dev_get_platdata(&pdev->dev);
126 struct regulator_led *led;
127 struct regulator *vcc;
131 dev_err(&pdev->dev, "no platform data\n");
135 vcc = devm_regulator_get_exclusive(&pdev->dev, "vled");
137 dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
141 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
145 led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
146 if (pdata->brightness > led->cdev.max_brightness) {
147 dev_err(&pdev->dev, "Invalid default brightness %d\n",
152 led->cdev.brightness_set_blocking = regulator_led_brightness_set;
153 led->cdev.name = pdata->name;
154 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
157 /* to handle correctly an already enabled regulator */
158 if (regulator_is_enabled(led->vcc))
161 mutex_init(&led->mutex);
163 platform_set_drvdata(pdev, led);
165 ret = led_classdev_register(&pdev->dev, &led->cdev);
169 /* to expose the default value to userspace */
170 led->cdev.brightness = pdata->brightness;
172 /* Set the default led status */
173 regulator_led_brightness_set(&led->cdev, led->cdev.brightness);
178 static int regulator_led_remove(struct platform_device *pdev)
180 struct regulator_led *led = platform_get_drvdata(pdev);
182 led_classdev_unregister(&led->cdev);
183 regulator_led_disable(led);
187 static struct platform_driver regulator_led_driver = {
189 .name = "leds-regulator",
191 .probe = regulator_led_probe,
192 .remove = regulator_led_remove,
195 module_platform_driver(regulator_led_driver);
197 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
198 MODULE_DESCRIPTION("Regulator driven LED driver");
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS("platform:leds-regulator");