blob: 8f243c4137234b297009a3814c463f115fff1fa0 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark Brown0081e802008-12-04 16:52:33 +00002/*
3 * LED driver for WM8350 driven LEDS.
4 *
5 * Copyright(C) 2007, 2008 Wolfson Microelectronics PLC.
Mark Brown0081e802008-12-04 16:52:33 +00006 */
7
8#include <linux/kernel.h>
Mark Brown0081e802008-12-04 16:52:33 +00009#include <linux/platform_device.h>
10#include <linux/leds.h>
11#include <linux/err.h>
12#include <linux/mfd/wm8350/pmic.h>
13#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040015#include <linux/module.h>
Mark Brown0081e802008-12-04 16:52:33 +000016
17/* Microamps */
18static const int isink_cur[] = {
19 4,
20 5,
21 6,
22 7,
23 8,
24 10,
25 11,
26 14,
27 16,
28 19,
29 23,
30 27,
31 32,
32 39,
33 46,
34 54,
35 65,
36 77,
37 92,
38 109,
39 130,
40 154,
41 183,
42 218,
43 259,
44 308,
45 367,
46 436,
47 518,
48 616,
49 733,
50 872,
51 1037,
52 1233,
53 1466,
54 1744,
55 2073,
56 2466,
57 2933,
58 3487,
59 4147,
60 4932,
61 5865,
62 6975,
63 8294,
64 9864,
65 11730,
66 13949,
67 16589,
68 19728,
69 23460,
70 27899,
71 33178,
72 39455,
73 46920,
74 55798,
75 66355,
76 78910,
77 93840,
78 111596,
79 132710,
80 157820,
81 187681,
82 223191
83};
84
85#define to_wm8350_led(led_cdev) \
86 container_of(led_cdev, struct wm8350_led, cdev)
87
Andrew Lunn0dd756f2015-08-20 12:59:45 +020088static int wm8350_led_enable(struct wm8350_led *led)
Mark Brown0081e802008-12-04 16:52:33 +000089{
Andrew Lunn0dd756f2015-08-20 12:59:45 +020090 int ret = 0;
Mark Brown0081e802008-12-04 16:52:33 +000091
92 if (led->enabled)
Andrew Lunn0dd756f2015-08-20 12:59:45 +020093 return ret;
Mark Brown0081e802008-12-04 16:52:33 +000094
95 ret = regulator_enable(led->isink);
96 if (ret != 0) {
97 dev_err(led->cdev.dev, "Failed to enable ISINK: %d\n", ret);
Andrew Lunn0dd756f2015-08-20 12:59:45 +020098 return ret;
Mark Brown0081e802008-12-04 16:52:33 +000099 }
100
101 ret = regulator_enable(led->dcdc);
102 if (ret != 0) {
103 dev_err(led->cdev.dev, "Failed to enable DCDC: %d\n", ret);
104 regulator_disable(led->isink);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200105 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000106 }
107
108 led->enabled = 1;
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200109
110 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000111}
112
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200113static int wm8350_led_disable(struct wm8350_led *led)
Mark Brown0081e802008-12-04 16:52:33 +0000114{
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200115 int ret = 0;
Mark Brown0081e802008-12-04 16:52:33 +0000116
117 if (!led->enabled)
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200118 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000119
120 ret = regulator_disable(led->dcdc);
121 if (ret != 0) {
122 dev_err(led->cdev.dev, "Failed to disable DCDC: %d\n", ret);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200123 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000124 }
125
126 ret = regulator_disable(led->isink);
127 if (ret != 0) {
128 dev_err(led->cdev.dev, "Failed to disable ISINK: %d\n", ret);
Mark Brownd67eb8e2013-03-05 21:01:37 -0800129 ret = regulator_enable(led->dcdc);
130 if (ret != 0)
131 dev_err(led->cdev.dev, "Failed to reenable DCDC: %d\n",
132 ret);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200133 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000134 }
135
136 led->enabled = 0;
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200137
138 return ret;
Mark Brown0081e802008-12-04 16:52:33 +0000139}
140
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200141static int wm8350_led_set(struct led_classdev *led_cdev,
142 enum led_brightness value)
Mark Brown0081e802008-12-04 16:52:33 +0000143{
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200144 struct wm8350_led *led = to_wm8350_led(led_cdev);
145 unsigned long flags;
Mark Brown0081e802008-12-04 16:52:33 +0000146 int ret;
147 int uA;
Mark Brown0081e802008-12-04 16:52:33 +0000148
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200149 led->value = value;
Mark Brown0081e802008-12-04 16:52:33 +0000150
151 spin_lock_irqsave(&led->value_lock, flags);
152
153 if (led->value == LED_OFF) {
154 spin_unlock_irqrestore(&led->value_lock, flags);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200155 return wm8350_led_disable(led);
Mark Brown0081e802008-12-04 16:52:33 +0000156 }
157
158 /* This scales linearly into the index of valid current
159 * settings which results in a linear scaling of perceived
160 * brightness due to the non-linear current settings provided
161 * by the hardware.
162 */
163 uA = (led->max_uA_index * led->value) / LED_FULL;
164 spin_unlock_irqrestore(&led->value_lock, flags);
165 BUG_ON(uA >= ARRAY_SIZE(isink_cur));
166
167 ret = regulator_set_current_limit(led->isink, isink_cur[uA],
168 isink_cur[uA]);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200169 if (ret != 0) {
Mark Brown0081e802008-12-04 16:52:33 +0000170 dev_err(led->cdev.dev, "Failed to set %duA: %d\n",
171 isink_cur[uA], ret);
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200172 return ret;
173 }
Mark Brown0081e802008-12-04 16:52:33 +0000174
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200175 return wm8350_led_enable(led);
Mark Brown0081e802008-12-04 16:52:33 +0000176}
177
Mark Brown0081e802008-12-04 16:52:33 +0000178static void wm8350_led_shutdown(struct platform_device *pdev)
179{
180 struct wm8350_led *led = platform_get_drvdata(pdev);
181
Mark Brown0081e802008-12-04 16:52:33 +0000182 led->value = LED_OFF;
183 wm8350_led_disable(led);
Mark Brown0081e802008-12-04 16:52:33 +0000184}
185
186static int wm8350_led_probe(struct platform_device *pdev)
187{
188 struct regulator *isink, *dcdc;
189 struct wm8350_led *led;
Jingoo Han87aae1e2013-07-30 01:07:35 -0700190 struct wm8350_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Axel Lin490dcee2012-08-10 10:08:32 +0800191 int i;
Mark Brown0081e802008-12-04 16:52:33 +0000192
193 if (pdata == NULL) {
194 dev_err(&pdev->dev, "no platform data\n");
195 return -ENODEV;
196 }
197
198 if (pdata->max_uA < isink_cur[0]) {
199 dev_err(&pdev->dev, "Invalid maximum current %duA\n",
200 pdata->max_uA);
201 return -EINVAL;
202 }
203
Axel Lin490dcee2012-08-10 10:08:32 +0800204 isink = devm_regulator_get(&pdev->dev, "led_isink");
Mark Brown0081e802008-12-04 16:52:33 +0000205 if (IS_ERR(isink)) {
Sachin Kamatb75d2802012-11-25 12:45:11 +0530206 dev_err(&pdev->dev, "%s: can't get ISINK\n", __func__);
Mark Brown0081e802008-12-04 16:52:33 +0000207 return PTR_ERR(isink);
208 }
209
Axel Lin490dcee2012-08-10 10:08:32 +0800210 dcdc = devm_regulator_get(&pdev->dev, "led_vcc");
Mark Brown0081e802008-12-04 16:52:33 +0000211 if (IS_ERR(dcdc)) {
Sachin Kamatb75d2802012-11-25 12:45:11 +0530212 dev_err(&pdev->dev, "%s: can't get DCDC\n", __func__);
Axel Lin490dcee2012-08-10 10:08:32 +0800213 return PTR_ERR(dcdc);
Mark Brown0081e802008-12-04 16:52:33 +0000214 }
215
Mark Brownc957b612012-01-10 15:09:46 -0800216 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
Axel Lin490dcee2012-08-10 10:08:32 +0800217 if (led == NULL)
218 return -ENOMEM;
Mark Brown0081e802008-12-04 16:52:33 +0000219
Andrew Lunn0dd756f2015-08-20 12:59:45 +0200220 led->cdev.brightness_set_blocking = wm8350_led_set;
Mark Brown0081e802008-12-04 16:52:33 +0000221 led->cdev.default_trigger = pdata->default_trigger;
222 led->cdev.name = pdata->name;
Richard Purdie859cb7f2009-01-08 17:55:03 +0000223 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
Mark Brown0081e802008-12-04 16:52:33 +0000224 led->enabled = regulator_is_enabled(isink);
225 led->isink = isink;
226 led->dcdc = dcdc;
227
228 for (i = 0; i < ARRAY_SIZE(isink_cur) - 1; i++)
229 if (isink_cur[i] >= pdata->max_uA)
230 break;
231 led->max_uA_index = i;
232 if (pdata->max_uA != isink_cur[i])
233 dev_warn(&pdev->dev,
234 "Maximum current %duA is not directly supported,"
235 " check platform data\n",
236 pdata->max_uA);
237
238 spin_lock_init(&led->value_lock);
Mark Brown0081e802008-12-04 16:52:33 +0000239 led->value = LED_OFF;
240 platform_set_drvdata(pdev, led);
241
Axel Lin490dcee2012-08-10 10:08:32 +0800242 return led_classdev_register(&pdev->dev, &led->cdev);
Mark Brown0081e802008-12-04 16:52:33 +0000243}
244
245static int wm8350_led_remove(struct platform_device *pdev)
246{
247 struct wm8350_led *led = platform_get_drvdata(pdev);
248
249 led_classdev_unregister(&led->cdev);
Mark Brown0081e802008-12-04 16:52:33 +0000250 wm8350_led_disable(led);
Mark Brown0081e802008-12-04 16:52:33 +0000251 return 0;
252}
253
254static struct platform_driver wm8350_led_driver = {
255 .driver = {
256 .name = "wm8350-led",
Mark Brown0081e802008-12-04 16:52:33 +0000257 },
258 .probe = wm8350_led_probe,
259 .remove = wm8350_led_remove,
260 .shutdown = wm8350_led_shutdown,
Mark Brown0081e802008-12-04 16:52:33 +0000261};
262
Axel Lin892a8842012-01-10 15:09:24 -0800263module_platform_driver(wm8350_led_driver);
Mark Brown0081e802008-12-04 16:52:33 +0000264
265MODULE_AUTHOR("Mark Brown");
266MODULE_DESCRIPTION("WM8350 LED driver");
267MODULE_LICENSE("GPL");
268MODULE_ALIAS("platform:wm8350-led");