blob: 208c98918433e873035e29195c0ed723a04edb6b [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>
11#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Antonio Ospited4cc6a22009-12-07 15:08:13 +010013#include <linux/leds.h>
14#include <linux/leds-regulator.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/consumer.h>
17
18#define to_regulator_led(led_cdev) \
19 container_of(led_cdev, struct regulator_led, cdev)
20
21struct regulator_led {
22 struct led_classdev cdev;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010023 int enabled;
24 struct mutex mutex;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010025
26 struct regulator *vcc;
27};
28
29static inline int led_regulator_get_max_brightness(struct regulator *supply)
30{
31 int ret;
32 int voltage = regulator_list_voltage(supply, 0);
33
34 if (voltage <= 0)
35 return 1;
36
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.
40 */
41 ret = regulator_set_voltage(supply, voltage, voltage);
42 if (ret < 0)
43 return 1;
44
45 return regulator_count_voltages(supply);
46}
47
48static int led_regulator_get_voltage(struct regulator *supply,
49 enum led_brightness brightness)
50{
51 if (brightness == 0)
52 return -EINVAL;
53
54 return regulator_list_voltage(supply, brightness - 1);
55}
56
57
58static void regulator_led_enable(struct regulator_led *led)
59{
60 int ret;
61
62 if (led->enabled)
63 return;
64
65 ret = regulator_enable(led->vcc);
66 if (ret != 0) {
67 dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
68 return;
69 }
70
71 led->enabled = 1;
72}
73
74static void regulator_led_disable(struct regulator_led *led)
75{
76 int ret;
77
78 if (!led->enabled)
79 return;
80
81 ret = regulator_disable(led->vcc);
82 if (ret != 0) {
83 dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
84 return;
85 }
86
87 led->enabled = 0;
88}
89
Andrew Lunn77e85032015-08-20 12:55:39 +020090static int regulator_led_brightness_set(struct led_classdev *led_cdev,
91 enum led_brightness value)
Antonio Ospited4cc6a22009-12-07 15:08:13 +010092{
Andrew Lunn77e85032015-08-20 12:55:39 +020093 struct regulator_led *led = to_regulator_led(led_cdev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +010094 int voltage;
Andrew Lunn77e85032015-08-20 12:55:39 +020095 int ret = 0;
Antonio Ospited4cc6a22009-12-07 15:08:13 +010096
97 mutex_lock(&led->mutex);
98
Andrew Lunn77e85032015-08-20 12:55:39 +020099 if (value == LED_OFF) {
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100100 regulator_led_disable(led);
101 goto out;
102 }
103
104 if (led->cdev.max_brightness > 1) {
Andrew Lunn77e85032015-08-20 12:55:39 +0200105 voltage = led_regulator_get_voltage(led->vcc, value);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100106 dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
Andrew Lunn77e85032015-08-20 12:55:39 +0200107 value, voltage);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100108
109 ret = regulator_set_voltage(led->vcc, voltage, voltage);
110 if (ret != 0)
111 dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
112 voltage, ret);
113 }
114
115 regulator_led_enable(led);
116
117out:
118 mutex_unlock(&led->mutex);
Andrew Lunn77e85032015-08-20 12:55:39 +0200119 return ret;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100120}
121
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500122static int regulator_led_probe(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100123{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700124 struct led_regulator_platform_data *pdata =
125 dev_get_platdata(&pdev->dev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100126 struct regulator_led *led;
127 struct regulator *vcc;
128 int ret = 0;
129
130 if (pdata == NULL) {
131 dev_err(&pdev->dev, "no platform data\n");
132 return -ENODEV;
133 }
134
Axel Lin29ce9fe2014-10-24 07:45:58 -0700135 vcc = devm_regulator_get_exclusive(&pdev->dev, "vled");
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100136 if (IS_ERR(vcc)) {
137 dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
138 return PTR_ERR(vcc);
139 }
140
Sachin Kamatd4158962012-07-03 19:47:54 +0800141 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
Axel Lin29ce9fe2014-10-24 07:45:58 -0700142 if (led == NULL)
143 return -ENOMEM;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100144
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",
148 pdata->brightness);
Axel Lin29ce9fe2014-10-24 07:45:58 -0700149 return -EINVAL;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100150 }
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100151
Andrew Lunn77e85032015-08-20 12:55:39 +0200152 led->cdev.brightness_set_blocking = regulator_led_brightness_set;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100153 led->cdev.name = pdata->name;
154 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
155 led->vcc = vcc;
156
Antonio Ospite592ce312011-04-14 15:21:59 -0700157 /* to handle correctly an already enabled regulator */
158 if (regulator_is_enabled(led->vcc))
159 led->enabled = 1;
160
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100161 mutex_init(&led->mutex);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100162
163 platform_set_drvdata(pdev, led);
164
165 ret = led_classdev_register(&pdev->dev, &led->cdev);
Andrew Lunn77e85032015-08-20 12:55:39 +0200166 if (ret < 0)
Axel Lin29ce9fe2014-10-24 07:45:58 -0700167 return ret;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100168
169 /* to expose the default value to userspace */
Andrew Lunn77e85032015-08-20 12:55:39 +0200170 led->cdev.brightness = pdata->brightness;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100171
172 /* Set the default led status */
Andrew Lunn77e85032015-08-20 12:55:39 +0200173 regulator_led_brightness_set(&led->cdev, led->cdev.brightness);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100174
175 return 0;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100176}
177
Bill Pemberton678e8a62012-11-19 13:26:00 -0500178static int regulator_led_remove(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100179{
180 struct regulator_led *led = platform_get_drvdata(pdev);
181
182 led_classdev_unregister(&led->cdev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100183 regulator_led_disable(led);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100184 return 0;
185}
186
187static struct platform_driver regulator_led_driver = {
188 .driver = {
189 .name = "leds-regulator",
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100190 },
191 .probe = regulator_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500192 .remove = regulator_led_remove,
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100193};
194
Axel Lin892a8842012-01-10 15:09:24 -0800195module_platform_driver(regulator_led_driver);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100196
197MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
198MODULE_DESCRIPTION("Regulator driven LED driver");
199MODULE_LICENSE("GPL");
200MODULE_ALIAS("platform:leds-regulator");