blob: 848e929c4a61c888a997e684013fcedf1c2d0b9d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Antonio Ospited4cc6a22009-12-07 15:08:13 +01002/*
3 * leds-regulator.c - LED class driver for regulator driven LEDs.
4 *
5 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
6 *
7 * Inspired by leds-wm8350 driver.
Antonio Ospited4cc6a22009-12-07 15:08:13 +01008 */
9
10#include <linux/module.h>
Linus Walleij835fc892022-04-04 22:35:22 +020011#include <linux/mod_devicetable.h>
Antonio Ospited4cc6a22009-12-07 15:08:13 +010012#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Antonio Ospited4cc6a22009-12-07 15:08:13 +010014#include <linux/leds.h>
15#include <linux/leds-regulator.h>
16#include <linux/platform_device.h>
17#include <linux/regulator/consumer.h>
18
19#define to_regulator_led(led_cdev) \
20 container_of(led_cdev, struct regulator_led, cdev)
21
22struct regulator_led {
23 struct led_classdev cdev;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010024 int enabled;
25 struct mutex mutex;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010026
27 struct regulator *vcc;
28};
29
30static inline int led_regulator_get_max_brightness(struct regulator *supply)
31{
32 int ret;
33 int voltage = regulator_list_voltage(supply, 0);
34
35 if (voltage <= 0)
36 return 1;
37
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.
41 */
42 ret = regulator_set_voltage(supply, voltage, voltage);
43 if (ret < 0)
44 return 1;
45
46 return regulator_count_voltages(supply);
47}
48
49static int led_regulator_get_voltage(struct regulator *supply,
50 enum led_brightness brightness)
51{
52 if (brightness == 0)
53 return -EINVAL;
54
55 return regulator_list_voltage(supply, brightness - 1);
56}
57
58
59static void regulator_led_enable(struct regulator_led *led)
60{
61 int ret;
62
63 if (led->enabled)
64 return;
65
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;
70 }
71
72 led->enabled = 1;
73}
74
75static void regulator_led_disable(struct regulator_led *led)
76{
77 int ret;
78
79 if (!led->enabled)
80 return;
81
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;
86 }
87
88 led->enabled = 0;
89}
90
Andrew Lunn77e85032015-08-20 12:55:39 +020091static int regulator_led_brightness_set(struct led_classdev *led_cdev,
92 enum led_brightness value)
Antonio Ospited4cc6a22009-12-07 15:08:13 +010093{
Andrew Lunn77e85032015-08-20 12:55:39 +020094 struct regulator_led *led = to_regulator_led(led_cdev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +010095 int voltage;
Andrew Lunn77e85032015-08-20 12:55:39 +020096 int ret = 0;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010097
98 mutex_lock(&led->mutex);
99
Andrew Lunn77e85032015-08-20 12:55:39 +0200100 if (value == LED_OFF) {
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100101 regulator_led_disable(led);
102 goto out;
103 }
104
105 if (led->cdev.max_brightness > 1) {
Andrew Lunn77e85032015-08-20 12:55:39 +0200106 voltage = led_regulator_get_voltage(led->vcc, value);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100107 dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
Andrew Lunn77e85032015-08-20 12:55:39 +0200108 value, voltage);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100109
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);
114 }
115
116 regulator_led_enable(led);
117
118out:
119 mutex_unlock(&led->mutex);
Andrew Lunn77e85032015-08-20 12:55:39 +0200120 return ret;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100121}
122
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500123static int regulator_led_probe(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100124{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700125 struct led_regulator_platform_data *pdata =
126 dev_get_platdata(&pdev->dev);
Linus Walleij4c350c62022-04-04 22:35:21 +0200127 struct device *dev = &pdev->dev;
Linus Walleij835fc892022-04-04 22:35:22 +0200128 struct led_init_data init_data = {};
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100129 struct regulator_led *led;
130 struct regulator *vcc;
131 int ret = 0;
132
Linus Walleij4c350c62022-04-04 22:35:21 +0200133 vcc = devm_regulator_get_exclusive(dev, "vled");
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100134 if (IS_ERR(vcc)) {
Linus Walleij835fc892022-04-04 22:35:22 +0200135 dev_err(dev, "Cannot get vcc\n");
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100136 return PTR_ERR(vcc);
137 }
138
Linus Walleij4c350c62022-04-04 22:35:21 +0200139 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
Axel Lin29ce9fe2014-10-24 07:45:58 -0700140 if (led == NULL)
141 return -ENOMEM;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100142
Linus Walleij835fc892022-04-04 22:35:22 +0200143 init_data.fwnode = dev->fwnode;
144
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100145 led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
Linus Walleij835fc892022-04-04 22:35:22 +0200146 /* 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",
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100150 pdata->brightness);
Linus Walleij835fc892022-04-04 22:35:22 +0200151 return -EINVAL;
152 }
153 led->cdev.brightness = pdata->brightness;
154 init_data.default_label = pdata->name;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100155 }
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100156
Andrew Lunn77e85032015-08-20 12:55:39 +0200157 led->cdev.brightness_set_blocking = regulator_led_brightness_set;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100158 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
159 led->vcc = vcc;
160
Antonio Ospite592ce312011-04-14 15:21:59 -0700161 /* to handle correctly an already enabled regulator */
162 if (regulator_is_enabled(led->vcc))
163 led->enabled = 1;
164
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100165 mutex_init(&led->mutex);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100166
167 platform_set_drvdata(pdev, led);
168
Linus Walleij835fc892022-04-04 22:35:22 +0200169 ret = led_classdev_register_ext(dev, &led->cdev, &init_data);
Andrew Lunn77e85032015-08-20 12:55:39 +0200170 if (ret < 0)
Axel Lin29ce9fe2014-10-24 07:45:58 -0700171 return ret;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100172
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100173 return 0;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100174}
175
Uwe Kleine-König60613022023-09-17 15:09:47 +0200176static void regulator_led_remove(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100177{
178 struct regulator_led *led = platform_get_drvdata(pdev);
179
180 led_classdev_unregister(&led->cdev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100181 regulator_led_disable(led);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100182}
183
Linus Walleij835fc892022-04-04 22:35:22 +0200184static const struct of_device_id regulator_led_of_match[] = {
185 { .compatible = "regulator-led", },
186 {}
187};
188MODULE_DEVICE_TABLE(of, regulator_led_of_match);
189
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100190static struct platform_driver regulator_led_driver = {
191 .driver = {
Linus Walleij835fc892022-04-04 22:35:22 +0200192 .name = "leds-regulator",
193 .of_match_table = regulator_led_of_match,
194 },
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100195 .probe = regulator_led_probe,
Uwe Kleine-König60613022023-09-17 15:09:47 +0200196 .remove_new = regulator_led_remove,
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100197};
198
Axel Lin892a8842012-01-10 15:09:24 -0800199module_platform_driver(regulator_led_driver);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100200
201MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
202MODULE_DESCRIPTION("Regulator driven LED driver");
203MODULE_LICENSE("GPL");
204MODULE_ALIAS("platform:leds-regulator");