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>
21 static const int isink_cur[] = {
88 #define to_wm8350_led(led_cdev) \
89 container_of(led_cdev, struct wm8350_led, cdev)
91 static void wm8350_led_enable(struct wm8350_led *led)
98 ret = regulator_enable(led->isink);
100 dev_err(led->cdev.dev, "Failed to enable ISINK: %d\n", ret);
104 ret = regulator_enable(led->dcdc);
106 dev_err(led->cdev.dev, "Failed to enable DCDC: %d\n", ret);
107 regulator_disable(led->isink);
114 static void wm8350_led_disable(struct wm8350_led *led)
121 ret = regulator_disable(led->dcdc);
123 dev_err(led->cdev.dev, "Failed to disable DCDC: %d\n", ret);
127 ret = regulator_disable(led->isink);
129 dev_err(led->cdev.dev, "Failed to disable ISINK: %d\n", ret);
130 regulator_enable(led->dcdc);
137 static void led_work(struct work_struct *work)
139 struct wm8350_led *led = container_of(work, struct wm8350_led, work);
144 mutex_lock(&led->mutex);
146 spin_lock_irqsave(&led->value_lock, flags);
148 if (led->value == LED_OFF) {
149 spin_unlock_irqrestore(&led->value_lock, flags);
150 wm8350_led_disable(led);
154 /* This scales linearly into the index of valid current
155 * settings which results in a linear scaling of perceived
156 * brightness due to the non-linear current settings provided
159 uA = (led->max_uA_index * led->value) / LED_FULL;
160 spin_unlock_irqrestore(&led->value_lock, flags);
161 BUG_ON(uA >= ARRAY_SIZE(isink_cur));
163 ret = regulator_set_current_limit(led->isink, isink_cur[uA],
166 dev_err(led->cdev.dev, "Failed to set %duA: %d\n",
169 wm8350_led_enable(led);
172 mutex_unlock(&led->mutex);
175 static void wm8350_led_set(struct led_classdev *led_cdev,
176 enum led_brightness value)
178 struct wm8350_led *led = to_wm8350_led(led_cdev);
181 spin_lock_irqsave(&led->value_lock, flags);
183 schedule_work(&led->work);
184 spin_unlock_irqrestore(&led->value_lock, flags);
187 static void wm8350_led_shutdown(struct platform_device *pdev)
189 struct wm8350_led *led = platform_get_drvdata(pdev);
191 mutex_lock(&led->mutex);
192 led->value = LED_OFF;
193 wm8350_led_disable(led);
194 mutex_unlock(&led->mutex);
197 static int wm8350_led_probe(struct platform_device *pdev)
199 struct regulator *isink, *dcdc;
200 struct wm8350_led *led;
201 struct wm8350_led_platform_data *pdata = pdev->dev.platform_data;
205 dev_err(&pdev->dev, "no platform data\n");
209 if (pdata->max_uA < isink_cur[0]) {
210 dev_err(&pdev->dev, "Invalid maximum current %duA\n",
215 isink = regulator_get(&pdev->dev, "led_isink");
217 printk(KERN_ERR "%s: cant get ISINK\n", __func__);
218 return PTR_ERR(isink);
221 dcdc = regulator_get(&pdev->dev, "led_vcc");
223 printk(KERN_ERR "%s: cant get DCDC\n", __func__);
228 led = kzalloc(sizeof(*led), GFP_KERNEL);
234 led->cdev.brightness_set = wm8350_led_set;
235 led->cdev.default_trigger = pdata->default_trigger;
236 led->cdev.name = pdata->name;
237 led->cdev.flags |= LED_CORE_SUSPENDRESUME;
238 led->enabled = regulator_is_enabled(isink);
242 for (i = 0; i < ARRAY_SIZE(isink_cur) - 1; i++)
243 if (isink_cur[i] >= pdata->max_uA)
245 led->max_uA_index = i;
246 if (pdata->max_uA != isink_cur[i])
248 "Maximum current %duA is not directly supported,"
249 " check platform data\n",
252 spin_lock_init(&led->value_lock);
253 mutex_init(&led->mutex);
254 INIT_WORK(&led->work, led_work);
255 led->value = LED_OFF;
256 platform_set_drvdata(pdev, led);
258 ret = led_classdev_register(&pdev->dev, &led->cdev);
269 regulator_put(isink);
273 static int wm8350_led_remove(struct platform_device *pdev)
275 struct wm8350_led *led = platform_get_drvdata(pdev);
277 led_classdev_unregister(&led->cdev);
278 flush_scheduled_work();
279 wm8350_led_disable(led);
280 regulator_put(led->dcdc);
281 regulator_put(led->isink);
286 static struct platform_driver wm8350_led_driver = {
288 .name = "wm8350-led",
289 .owner = THIS_MODULE,
291 .probe = wm8350_led_probe,
292 .remove = wm8350_led_remove,
293 .shutdown = wm8350_led_shutdown,
296 static int __devinit wm8350_led_init(void)
298 return platform_driver_register(&wm8350_led_driver);
300 module_init(wm8350_led_init);
302 static void wm8350_led_exit(void)
304 platform_driver_unregister(&wm8350_led_driver);
306 module_exit(wm8350_led_exit);
308 MODULE_AUTHOR("Mark Brown");
309 MODULE_DESCRIPTION("WM8350 LED driver");
310 MODULE_LICENSE("GPL");
311 MODULE_ALIAS("platform:wm8350-led");