1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED driver for WM8350 driven LEDS.
5 * Copyright(C) 2007, 2008 Wolfson Microelectronics PLC.
8 #include <linux/kernel.h>
9 #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>
14 #include <linux/slab.h>
15 #include <linux/module.h>
18 static const int isink_cur
[] = {
85 #define to_wm8350_led(led_cdev) \
86 container_of(led_cdev, struct wm8350_led, cdev)
88 static int wm8350_led_enable(struct wm8350_led
*led
)
95 ret
= regulator_enable(led
->isink
);
97 dev_err(led
->cdev
.dev
, "Failed to enable ISINK: %d\n", ret
);
101 ret
= regulator_enable(led
->dcdc
);
103 dev_err(led
->cdev
.dev
, "Failed to enable DCDC: %d\n", ret
);
104 regulator_disable(led
->isink
);
113 static int wm8350_led_disable(struct wm8350_led
*led
)
120 ret
= regulator_disable(led
->dcdc
);
122 dev_err(led
->cdev
.dev
, "Failed to disable DCDC: %d\n", ret
);
126 ret
= regulator_disable(led
->isink
);
128 dev_err(led
->cdev
.dev
, "Failed to disable ISINK: %d\n", ret
);
129 ret
= regulator_enable(led
->dcdc
);
131 dev_err(led
->cdev
.dev
, "Failed to reenable DCDC: %d\n",
141 static int wm8350_led_set(struct led_classdev
*led_cdev
,
142 enum led_brightness value
)
144 struct wm8350_led
*led
= to_wm8350_led(led_cdev
);
151 spin_lock_irqsave(&led
->value_lock
, flags
);
153 if (led
->value
== LED_OFF
) {
154 spin_unlock_irqrestore(&led
->value_lock
, flags
);
155 return wm8350_led_disable(led
);
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
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
));
167 ret
= regulator_set_current_limit(led
->isink
, isink_cur
[uA
],
170 dev_err(led
->cdev
.dev
, "Failed to set %duA: %d\n",
175 return wm8350_led_enable(led
);
178 static void wm8350_led_shutdown(struct platform_device
*pdev
)
180 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
182 led
->value
= LED_OFF
;
183 wm8350_led_disable(led
);
186 static int wm8350_led_probe(struct platform_device
*pdev
)
188 struct regulator
*isink
, *dcdc
;
189 struct wm8350_led
*led
;
190 struct wm8350_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
194 dev_err(&pdev
->dev
, "no platform data\n");
198 if (pdata
->max_uA
< isink_cur
[0]) {
199 dev_err(&pdev
->dev
, "Invalid maximum current %duA\n",
204 isink
= devm_regulator_get(&pdev
->dev
, "led_isink");
206 dev_err(&pdev
->dev
, "%s: can't get ISINK\n", __func__
);
207 return PTR_ERR(isink
);
210 dcdc
= devm_regulator_get(&pdev
->dev
, "led_vcc");
212 dev_err(&pdev
->dev
, "%s: can't get DCDC\n", __func__
);
213 return PTR_ERR(dcdc
);
216 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
220 led
->cdev
.brightness_set_blocking
= wm8350_led_set
;
221 led
->cdev
.default_trigger
= pdata
->default_trigger
;
222 led
->cdev
.name
= pdata
->name
;
223 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
224 led
->enabled
= regulator_is_enabled(isink
);
228 for (i
= 0; i
< ARRAY_SIZE(isink_cur
) - 1; i
++)
229 if (isink_cur
[i
] >= pdata
->max_uA
)
231 led
->max_uA_index
= i
;
232 if (pdata
->max_uA
!= isink_cur
[i
])
234 "Maximum current %duA is not directly supported,"
235 " check platform data\n",
238 spin_lock_init(&led
->value_lock
);
239 led
->value
= LED_OFF
;
240 platform_set_drvdata(pdev
, led
);
242 return led_classdev_register(&pdev
->dev
, &led
->cdev
);
245 static void wm8350_led_remove(struct platform_device
*pdev
)
247 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
249 led_classdev_unregister(&led
->cdev
);
250 wm8350_led_disable(led
);
253 static struct platform_driver wm8350_led_driver
= {
255 .name
= "wm8350-led",
257 .probe
= wm8350_led_probe
,
258 .remove_new
= wm8350_led_remove
,
259 .shutdown
= wm8350_led_shutdown
,
262 module_platform_driver(wm8350_led_driver
);
264 MODULE_AUTHOR("Mark Brown");
265 MODULE_DESCRIPTION("WM8350 LED driver");
266 MODULE_LICENSE("GPL");
267 MODULE_ALIAS("platform:wm8350-led");