2 * LED driver for WM8350 driven LEDS.
4 * Copyright(C) 2007, 2008 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/leds.h>
16 #include <linux/err.h>
17 #include <linux/mfd/wm8350/pmic.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
23 static const int isink_cur[] = {
90 #define to_wm8350_led(led_cdev) \
91 container_of(led_cdev, struct wm8350_led, cdev)
93 static void wm8350_led_enable(struct wm8350_led *led)
100 ret = regulator_enable(led->isink);
102 dev_err(led->cdev.dev, "Failed to enable ISINK: %d\n", ret);
106 ret = regulator_enable(led->dcdc);
108 dev_err(led->cdev.dev, "Failed to enable DCDC: %d\n", ret);
109 regulator_disable(led->isink);
116 static void wm8350_led_disable(struct wm8350_led *led)
123 ret = regulator_disable(led->dcdc);
125 dev_err(led->cdev.dev, "Failed to disable DCDC: %d\n", ret);
129 ret = regulator_disable(led->isink);
131 dev_err(led->cdev.dev, "Failed to disable ISINK: %d\n", ret);
132 ret = regulator_enable(led->dcdc);
134 dev_err(led->cdev.dev, "Failed to reenable DCDC: %d\n",
142 static void led_work(struct work_struct *work)
144 struct wm8350_led *led = container_of(work, struct wm8350_led, work);
149 mutex_lock(&led->mutex);
151 spin_lock_irqsave(&led->value_lock, flags);
153 if (led->value == LED_OFF) {
154 spin_unlock_irqrestore(&led->value_lock, flags);
155 wm8350_led_disable(led);
159 /* This scales linearly into the index of valid current
160 * settings which results in a linear scaling of perceived
161 * brightness due to the non-linear current settings provided
164 uA = (led->max_uA_index * led->value) / LED_FULL;
165 spin_unlock_irqrestore(&led->value_lock, flags);
166 BUG_ON(uA >= ARRAY_SIZE(isink_cur));
168 ret = regulator_set_current_limit(led->isink, isink_cur[uA],
171 dev_err(led->cdev.dev, "Failed to set %duA: %d\n",
174 wm8350_led_enable(led);
177 mutex_unlock(&led->mutex);
180 static void wm8350_led_set(struct led_classdev *led_cdev,
181 enum led_brightness value)
183 struct wm8350_led *led = to_wm8350_led(led_cdev);
186 spin_lock_irqsave(&led->value_lock, flags);
188 schedule_work(&led->work);
189 spin_unlock_irqrestore(&led->value_lock, flags);
192 static void wm8350_led_shutdown(struct platform_device *pdev)
194 struct wm8350_led *led = platform_get_drvdata(pdev);
196 mutex_lock(&led->mutex);
197 led->value = LED_OFF;
198 wm8350_led_disable(led);
199 mutex_unlock(&led->mutex);
202 static int wm8350_led_probe(struct platform_device *pdev)
204 struct regulator *isink, *dcdc;
205 struct wm8350_led *led;
206 struct wm8350_led_platform_data *pdata = pdev->dev.platform_data;
210 dev_err(&pdev->dev, "no platform data\n");
214 if (pdata->max_uA < isink_cur[0]) {
215 dev_err(&pdev->dev, "Invalid maximum current %duA\n",
220 isink = devm_regulator_get(&pdev->dev, "led_isink");
222 dev_err(&pdev->dev, "%s: can't get ISINK\n", __func__);
223 return PTR_ERR(isink);
226 dcdc = devm_regulator_get(&pdev->dev, "led_vcc");
228 dev_err(&pdev->dev, "%s: can't get DCDC\n", __func__);
229 return PTR_ERR(dcdc);
232 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
236 led->cdev.brightness_set = wm8350_led_set;
237 led->cdev.default_trigger = pdata->default_trigger;
238 led->cdev.name = pdata->name;
239 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
240 led->enabled = regulator_is_enabled(isink);
244 for (i = 0; i < ARRAY_SIZE(isink_cur) - 1; i++)
245 if (isink_cur[i] >= pdata->max_uA)
247 led->max_uA_index = i;
248 if (pdata->max_uA != isink_cur[i])
250 "Maximum current %duA is not directly supported,"
251 " check platform data\n",
254 spin_lock_init(&led->value_lock);
255 mutex_init(&led->mutex);
256 INIT_WORK(&led->work, led_work);
257 led->value = LED_OFF;
258 platform_set_drvdata(pdev, led);
260 return led_classdev_register(&pdev->dev, &led->cdev);
263 static int wm8350_led_remove(struct platform_device *pdev)
265 struct wm8350_led *led = platform_get_drvdata(pdev);
267 led_classdev_unregister(&led->cdev);
268 flush_work(&led->work);
269 wm8350_led_disable(led);
273 static struct platform_driver wm8350_led_driver = {
275 .name = "wm8350-led",
276 .owner = THIS_MODULE,
278 .probe = wm8350_led_probe,
279 .remove = wm8350_led_remove,
280 .shutdown = wm8350_led_shutdown,
283 module_platform_driver(wm8350_led_driver);
285 MODULE_AUTHOR("Mark Brown");
286 MODULE_DESCRIPTION("WM8350 LED driver");
287 MODULE_LICENSE("GPL");
288 MODULE_ALIAS("platform:wm8350-led");