drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / leds / leds-regulator.c
blob848e929c4a61c888a997e684013fcedf1c2d0b9d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
8 */
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;
24 int enabled;
25 struct mutex mutex;
27 struct regulator *vcc;
30 static inline int led_regulator_get_max_brightness(struct regulator *supply)
32 int ret;
33 int voltage = regulator_list_voltage(supply, 0);
35 if (voltage <= 0)
36 return 1;
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);
43 if (ret < 0)
44 return 1;
46 return regulator_count_voltages(supply);
49 static int led_regulator_get_voltage(struct regulator *supply,
50 enum led_brightness brightness)
52 if (brightness == 0)
53 return -EINVAL;
55 return regulator_list_voltage(supply, brightness - 1);
59 static void regulator_led_enable(struct regulator_led *led)
61 int ret;
63 if (led->enabled)
64 return;
66 ret = regulator_enable(led->vcc);
67 if (ret != 0) {
68 dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
69 return;
72 led->enabled = 1;
75 static void regulator_led_disable(struct regulator_led *led)
77 int ret;
79 if (!led->enabled)
80 return;
82 ret = regulator_disable(led->vcc);
83 if (ret != 0) {
84 dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
85 return;
88 led->enabled = 0;
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);
95 int voltage;
96 int ret = 0;
98 mutex_lock(&led->mutex);
100 if (value == LED_OFF) {
101 regulator_led_disable(led);
102 goto out;
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",
108 value, voltage);
110 ret = regulator_set_voltage(led->vcc, voltage, voltage);
111 if (ret != 0)
112 dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
113 voltage, ret);
116 regulator_led_enable(led);
118 out:
119 mutex_unlock(&led->mutex);
120 return ret;
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;
131 int ret = 0;
133 vcc = devm_regulator_get_exclusive(dev, "vled");
134 if (IS_ERR(vcc)) {
135 dev_err(dev, "Cannot get vcc\n");
136 return PTR_ERR(vcc);
139 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
140 if (led == NULL)
141 return -ENOMEM;
143 init_data.fwnode = dev->fwnode;
145 led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
146 /* Legacy platform data label assignment */
147 if (pdata) {
148 if (pdata->brightness > led->cdev.max_brightness) {
149 dev_err(dev, "Invalid default brightness %d\n",
150 pdata->brightness);
151 return -EINVAL;
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;
159 led->vcc = vcc;
161 /* to handle correctly an already enabled regulator */
162 if (regulator_is_enabled(led->vcc))
163 led->enabled = 1;
165 mutex_init(&led->mutex);
167 platform_set_drvdata(pdev, led);
169 ret = led_classdev_register_ext(dev, &led->cdev, &init_data);
170 if (ret < 0)
171 return ret;
173 return 0;
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 = {
191 .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");