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>
22 static const int isink_cur[] = {
89 #define to_wm8350_led(led_cdev) \
90 container_of(led_cdev, struct wm8350_led, cdev)
92 static void wm8350_led_enable(struct wm8350_led *led)
99 ret = regulator_enable(led->isink);
101 dev_err(led->cdev.dev, "Failed to enable ISINK: %d\n", ret);
105 ret = regulator_enable(led->dcdc);
107 dev_err(led->cdev.dev, "Failed to enable DCDC: %d\n", ret);
108 regulator_disable(led->isink);
115 static void wm8350_led_disable(struct wm8350_led *led)
122 ret = regulator_disable(led->dcdc);
124 dev_err(led->cdev.dev, "Failed to disable DCDC: %d\n", ret);
128 ret = regulator_disable(led->isink);
130 dev_err(led->cdev.dev, "Failed to disable ISINK: %d\n", ret);
131 regulator_enable(led->dcdc);
138 static void led_work(struct work_struct *work)
140 struct wm8350_led *led = container_of(work, struct wm8350_led, work);
145 mutex_lock(&led->mutex);
147 spin_lock_irqsave(&led->value_lock, flags);
149 if (led->value == LED_OFF) {
150 spin_unlock_irqrestore(&led->value_lock, flags);
151 wm8350_led_disable(led);
155 /* This scales linearly into the index of valid current
156 * settings which results in a linear scaling of perceived
157 * brightness due to the non-linear current settings provided
160 uA = (led->max_uA_index * led->value) / LED_FULL;
161 spin_unlock_irqrestore(&led->value_lock, flags);
162 BUG_ON(uA >= ARRAY_SIZE(isink_cur));
164 ret = regulator_set_current_limit(led->isink, isink_cur[uA],
167 dev_err(led->cdev.dev, "Failed to set %duA: %d\n",
170 wm8350_led_enable(led);
173 mutex_unlock(&led->mutex);
176 static void wm8350_led_set(struct led_classdev *led_cdev,
177 enum led_brightness value)
179 struct wm8350_led *led = to_wm8350_led(led_cdev);
182 spin_lock_irqsave(&led->value_lock, flags);
184 schedule_work(&led->work);
185 spin_unlock_irqrestore(&led->value_lock, flags);
188 static void wm8350_led_shutdown(struct platform_device *pdev)
190 struct wm8350_led *led = platform_get_drvdata(pdev);
192 mutex_lock(&led->mutex);
193 led->value = LED_OFF;
194 wm8350_led_disable(led);
195 mutex_unlock(&led->mutex);
198 static int wm8350_led_probe(struct platform_device *pdev)
200 struct regulator *isink, *dcdc;
201 struct wm8350_led *led;
202 struct wm8350_led_platform_data *pdata = pdev->dev.platform_data;
206 dev_err(&pdev->dev, "no platform data\n");
210 if (pdata->max_uA < isink_cur[0]) {
211 dev_err(&pdev->dev, "Invalid maximum current %duA\n",
216 isink = regulator_get(&pdev->dev, "led_isink");
218 printk(KERN_ERR "%s: can't get ISINK\n", __func__);
219 return PTR_ERR(isink);
222 dcdc = regulator_get(&pdev->dev, "led_vcc");
224 printk(KERN_ERR "%s: can't get DCDC\n", __func__);
229 led = kzalloc(sizeof(*led), GFP_KERNEL);
235 led->cdev.brightness_set = wm8350_led_set;
236 led->cdev.default_trigger = pdata->default_trigger;
237 led->cdev.name = pdata->name;
238 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
239 led->enabled = regulator_is_enabled(isink);
243 for (i = 0; i < ARRAY_SIZE(isink_cur) - 1; i++)
244 if (isink_cur[i] >= pdata->max_uA)
246 led->max_uA_index = i;
247 if (pdata->max_uA != isink_cur[i])
249 "Maximum current %duA is not directly supported,"
250 " check platform data\n",
253 spin_lock_init(&led->value_lock);
254 mutex_init(&led->mutex);
255 INIT_WORK(&led->work, led_work);
256 led->value = LED_OFF;
257 platform_set_drvdata(pdev, led);
259 ret = led_classdev_register(&pdev->dev, &led->cdev);
270 regulator_put(isink);
274 static int wm8350_led_remove(struct platform_device *pdev)
276 struct wm8350_led *led = platform_get_drvdata(pdev);
278 led_classdev_unregister(&led->cdev);
279 flush_work_sync(&led->work);
280 wm8350_led_disable(led);
281 regulator_put(led->dcdc);
282 regulator_put(led->isink);
287 static struct platform_driver wm8350_led_driver = {
289 .name = "wm8350-led",
290 .owner = THIS_MODULE,
292 .probe = wm8350_led_probe,
293 .remove = wm8350_led_remove,
294 .shutdown = wm8350_led_shutdown,
297 static int __devinit wm8350_led_init(void)
299 return platform_driver_register(&wm8350_led_driver);
301 module_init(wm8350_led_init);
303 static void wm8350_led_exit(void)
305 platform_driver_unregister(&wm8350_led_driver);
307 module_exit(wm8350_led_exit);
309 MODULE_AUTHOR("Mark Brown");
310 MODULE_DESCRIPTION("WM8350 LED driver");
311 MODULE_LICENSE("GPL");
312 MODULE_ALIAS("platform:wm8350-led");