blob: 358430db6e66d2e2adce01fff0a0ffa66ed8c830 [file] [log] [blame]
Antonio Ospited4cc6a22009-12-07 15:08:13 +01001/*
2 * leds-regulator.c - LED class driver for regulator driven LEDs.
3 *
4 * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
5 *
6 * Inspired by leds-wm8350 driver.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Antonio Ospited4cc6a22009-12-07 15:08:13 +010017#include <linux/workqueue.h>
18#include <linux/leds.h>
19#include <linux/leds-regulator.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/consumer.h>
22
23#define to_regulator_led(led_cdev) \
24 container_of(led_cdev, struct regulator_led, cdev)
25
26struct regulator_led {
27 struct led_classdev cdev;
28 enum led_brightness value;
29 int enabled;
30 struct mutex mutex;
31 struct work_struct work;
32
33 struct regulator *vcc;
34};
35
36static inline int led_regulator_get_max_brightness(struct regulator *supply)
37{
38 int ret;
39 int voltage = regulator_list_voltage(supply, 0);
40
41 if (voltage <= 0)
42 return 1;
43
44 /* even if regulator can't change voltages,
45 * we still assume it can change status
46 * and the LED can be turned on and off.
47 */
48 ret = regulator_set_voltage(supply, voltage, voltage);
49 if (ret < 0)
50 return 1;
51
52 return regulator_count_voltages(supply);
53}
54
55static int led_regulator_get_voltage(struct regulator *supply,
56 enum led_brightness brightness)
57{
58 if (brightness == 0)
59 return -EINVAL;
60
61 return regulator_list_voltage(supply, brightness - 1);
62}
63
64
65static void regulator_led_enable(struct regulator_led *led)
66{
67 int ret;
68
69 if (led->enabled)
70 return;
71
72 ret = regulator_enable(led->vcc);
73 if (ret != 0) {
74 dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
75 return;
76 }
77
78 led->enabled = 1;
79}
80
81static void regulator_led_disable(struct regulator_led *led)
82{
83 int ret;
84
85 if (!led->enabled)
86 return;
87
88 ret = regulator_disable(led->vcc);
89 if (ret != 0) {
90 dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
91 return;
92 }
93
94 led->enabled = 0;
95}
96
97static void regulator_led_set_value(struct regulator_led *led)
98{
99 int voltage;
100 int ret;
101
102 mutex_lock(&led->mutex);
103
104 if (led->value == LED_OFF) {
105 regulator_led_disable(led);
106 goto out;
107 }
108
109 if (led->cdev.max_brightness > 1) {
110 voltage = led_regulator_get_voltage(led->vcc, led->value);
111 dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
112 led->value, voltage);
113
114 ret = regulator_set_voltage(led->vcc, voltage, voltage);
115 if (ret != 0)
116 dev_err(led->cdev.dev, "Failed to set voltage %d: %d\n",
117 voltage, ret);
118 }
119
120 regulator_led_enable(led);
121
122out:
123 mutex_unlock(&led->mutex);
124}
125
126static void led_work(struct work_struct *work)
127{
128 struct regulator_led *led;
129
130 led = container_of(work, struct regulator_led, work);
131 regulator_led_set_value(led);
132}
133
134static void regulator_led_brightness_set(struct led_classdev *led_cdev,
135 enum led_brightness value)
136{
137 struct regulator_led *led = to_regulator_led(led_cdev);
138
139 led->value = value;
140 schedule_work(&led->work);
141}
142
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500143static int regulator_led_probe(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100144{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700145 struct led_regulator_platform_data *pdata =
146 dev_get_platdata(&pdev->dev);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100147 struct regulator_led *led;
148 struct regulator *vcc;
149 int ret = 0;
150
151 if (pdata == NULL) {
152 dev_err(&pdev->dev, "no platform data\n");
153 return -ENODEV;
154 }
155
156 vcc = regulator_get_exclusive(&pdev->dev, "vled");
157 if (IS_ERR(vcc)) {
158 dev_err(&pdev->dev, "Cannot get vcc for %s\n", pdata->name);
159 return PTR_ERR(vcc);
160 }
161
Sachin Kamatd4158962012-07-03 19:47:54 +0800162 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100163 if (led == NULL) {
164 ret = -ENOMEM;
165 goto err_vcc;
166 }
167
168 led->cdev.max_brightness = led_regulator_get_max_brightness(vcc);
169 if (pdata->brightness > led->cdev.max_brightness) {
170 dev_err(&pdev->dev, "Invalid default brightness %d\n",
171 pdata->brightness);
172 ret = -EINVAL;
Sachin Kamatd4158962012-07-03 19:47:54 +0800173 goto err_vcc;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100174 }
175 led->value = pdata->brightness;
176
177 led->cdev.brightness_set = regulator_led_brightness_set;
178 led->cdev.name = pdata->name;
179 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
180 led->vcc = vcc;
181
Antonio Ospite592ce312011-04-14 15:21:59 -0700182 /* to handle correctly an already enabled regulator */
183 if (regulator_is_enabled(led->vcc))
184 led->enabled = 1;
185
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100186 mutex_init(&led->mutex);
187 INIT_WORK(&led->work, led_work);
188
189 platform_set_drvdata(pdev, led);
190
191 ret = led_classdev_register(&pdev->dev, &led->cdev);
192 if (ret < 0) {
193 cancel_work_sync(&led->work);
Sachin Kamatd4158962012-07-03 19:47:54 +0800194 goto err_vcc;
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100195 }
196
197 /* to expose the default value to userspace */
198 led->cdev.brightness = led->value;
199
200 /* Set the default led status */
201 regulator_led_set_value(led);
202
203 return 0;
204
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100205err_vcc:
206 regulator_put(vcc);
207 return ret;
208}
209
Bill Pemberton678e8a62012-11-19 13:26:00 -0500210static int regulator_led_remove(struct platform_device *pdev)
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100211{
212 struct regulator_led *led = platform_get_drvdata(pdev);
213
214 led_classdev_unregister(&led->cdev);
215 cancel_work_sync(&led->work);
216 regulator_led_disable(led);
217 regulator_put(led->vcc);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100218 return 0;
219}
220
221static struct platform_driver regulator_led_driver = {
222 .driver = {
223 .name = "leds-regulator",
224 .owner = THIS_MODULE,
225 },
226 .probe = regulator_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500227 .remove = regulator_led_remove,
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100228};
229
Axel Lin892a8842012-01-10 15:09:24 -0800230module_platform_driver(regulator_led_driver);
Antonio Ospited4cc6a22009-12-07 15:08:13 +0100231
232MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
233MODULE_DESCRIPTION("Regulator driven LED driver");
234MODULE_LICENSE("GPL");
235MODULE_ALIAS("platform:leds-regulator");