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 regulator_enable(led
->dcdc
);
139 static void led_work(struct work_struct
*work
)
141 struct wm8350_led
*led
= container_of(work
, struct wm8350_led
, work
);
146 mutex_lock(&led
->mutex
);
148 spin_lock_irqsave(&led
->value_lock
, flags
);
150 if (led
->value
== LED_OFF
) {
151 spin_unlock_irqrestore(&led
->value_lock
, flags
);
152 wm8350_led_disable(led
);
156 /* This scales linearly into the index of valid current
157 * settings which results in a linear scaling of perceived
158 * brightness due to the non-linear current settings provided
161 uA
= (led
->max_uA_index
* led
->value
) / LED_FULL
;
162 spin_unlock_irqrestore(&led
->value_lock
, flags
);
163 BUG_ON(uA
>= ARRAY_SIZE(isink_cur
));
165 ret
= regulator_set_current_limit(led
->isink
, isink_cur
[uA
],
168 dev_err(led
->cdev
.dev
, "Failed to set %duA: %d\n",
171 wm8350_led_enable(led
);
174 mutex_unlock(&led
->mutex
);
177 static void wm8350_led_set(struct led_classdev
*led_cdev
,
178 enum led_brightness value
)
180 struct wm8350_led
*led
= to_wm8350_led(led_cdev
);
183 spin_lock_irqsave(&led
->value_lock
, flags
);
185 schedule_work(&led
->work
);
186 spin_unlock_irqrestore(&led
->value_lock
, flags
);
189 static void wm8350_led_shutdown(struct platform_device
*pdev
)
191 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
193 mutex_lock(&led
->mutex
);
194 led
->value
= LED_OFF
;
195 wm8350_led_disable(led
);
196 mutex_unlock(&led
->mutex
);
199 static int wm8350_led_probe(struct platform_device
*pdev
)
201 struct regulator
*isink
, *dcdc
;
202 struct wm8350_led
*led
;
203 struct wm8350_led_platform_data
*pdata
= pdev
->dev
.platform_data
;
207 dev_err(&pdev
->dev
, "no platform data\n");
211 if (pdata
->max_uA
< isink_cur
[0]) {
212 dev_err(&pdev
->dev
, "Invalid maximum current %duA\n",
217 isink
= regulator_get(&pdev
->dev
, "led_isink");
219 printk(KERN_ERR
"%s: can't get ISINK\n", __func__
);
220 return PTR_ERR(isink
);
223 dcdc
= regulator_get(&pdev
->dev
, "led_vcc");
225 printk(KERN_ERR
"%s: can't get DCDC\n", __func__
);
230 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 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_work_sync(&led
->work
);
279 wm8350_led_disable(led
);
280 regulator_put(led
->dcdc
);
281 regulator_put(led
->isink
);
285 static struct platform_driver wm8350_led_driver
= {
287 .name
= "wm8350-led",
288 .owner
= THIS_MODULE
,
290 .probe
= wm8350_led_probe
,
291 .remove
= wm8350_led_remove
,
292 .shutdown
= wm8350_led_shutdown
,
295 module_platform_driver(wm8350_led_driver
);
297 MODULE_AUTHOR("Mark Brown");
298 MODULE_DESCRIPTION("WM8350 LED driver");
299 MODULE_LICENSE("GPL");
300 MODULE_ALIAS("platform:wm8350-led");