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/platform_device.h>
14 #include <linux/leds.h>
15 #include <linux/err.h>
16 #include <linux/mfd/wm8350/pmic.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
19 #include <linux/module.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 ret
= regulator_enable(led
->dcdc
);
133 dev_err(led
->cdev
.dev
, "Failed to reenable DCDC: %d\n",
141 static void led_work(struct work_struct
*work
)
143 struct wm8350_led
*led
= container_of(work
, struct wm8350_led
, work
);
148 mutex_lock(&led
->mutex
);
150 spin_lock_irqsave(&led
->value_lock
, flags
);
152 if (led
->value
== LED_OFF
) {
153 spin_unlock_irqrestore(&led
->value_lock
, flags
);
154 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",
173 wm8350_led_enable(led
);
176 mutex_unlock(&led
->mutex
);
179 static void wm8350_led_set(struct led_classdev
*led_cdev
,
180 enum led_brightness value
)
182 struct wm8350_led
*led
= to_wm8350_led(led_cdev
);
185 spin_lock_irqsave(&led
->value_lock
, flags
);
187 schedule_work(&led
->work
);
188 spin_unlock_irqrestore(&led
->value_lock
, flags
);
191 static void wm8350_led_shutdown(struct platform_device
*pdev
)
193 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
195 mutex_lock(&led
->mutex
);
196 led
->value
= LED_OFF
;
197 wm8350_led_disable(led
);
198 mutex_unlock(&led
->mutex
);
201 static int wm8350_led_probe(struct platform_device
*pdev
)
203 struct regulator
*isink
, *dcdc
;
204 struct wm8350_led
*led
;
205 struct wm8350_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
209 dev_err(&pdev
->dev
, "no platform data\n");
213 if (pdata
->max_uA
< isink_cur
[0]) {
214 dev_err(&pdev
->dev
, "Invalid maximum current %duA\n",
219 isink
= devm_regulator_get(&pdev
->dev
, "led_isink");
221 dev_err(&pdev
->dev
, "%s: can't get ISINK\n", __func__
);
222 return PTR_ERR(isink
);
225 dcdc
= devm_regulator_get(&pdev
->dev
, "led_vcc");
227 dev_err(&pdev
->dev
, "%s: can't get DCDC\n", __func__
);
228 return PTR_ERR(dcdc
);
231 led
= devm_kzalloc(&pdev
->dev
, 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 return led_classdev_register(&pdev
->dev
, &led
->cdev
);
262 static int wm8350_led_remove(struct platform_device
*pdev
)
264 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
266 led_classdev_unregister(&led
->cdev
);
267 flush_work(&led
->work
);
268 wm8350_led_disable(led
);
272 static struct platform_driver wm8350_led_driver
= {
274 .name
= "wm8350-led",
275 .owner
= THIS_MODULE
,
277 .probe
= wm8350_led_probe
,
278 .remove
= wm8350_led_remove
,
279 .shutdown
= wm8350_led_shutdown
,
282 module_platform_driver(wm8350_led_driver
);
284 MODULE_AUTHOR("Mark Brown");
285 MODULE_DESCRIPTION("WM8350 LED driver");
286 MODULE_LICENSE("GPL");
287 MODULE_ALIAS("platform:wm8350-led");