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 int 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
);
117 static int wm8350_led_disable(struct wm8350_led
*led
)
124 ret
= regulator_disable(led
->dcdc
);
126 dev_err(led
->cdev
.dev
, "Failed to disable DCDC: %d\n", ret
);
130 ret
= regulator_disable(led
->isink
);
132 dev_err(led
->cdev
.dev
, "Failed to disable ISINK: %d\n", ret
);
133 ret
= regulator_enable(led
->dcdc
);
135 dev_err(led
->cdev
.dev
, "Failed to reenable DCDC: %d\n",
145 static int wm8350_led_set(struct led_classdev
*led_cdev
,
146 enum led_brightness value
)
148 struct wm8350_led
*led
= to_wm8350_led(led_cdev
);
155 spin_lock_irqsave(&led
->value_lock
, flags
);
157 if (led
->value
== LED_OFF
) {
158 spin_unlock_irqrestore(&led
->value_lock
, flags
);
159 return wm8350_led_disable(led
);
162 /* This scales linearly into the index of valid current
163 * settings which results in a linear scaling of perceived
164 * brightness due to the non-linear current settings provided
167 uA
= (led
->max_uA_index
* led
->value
) / LED_FULL
;
168 spin_unlock_irqrestore(&led
->value_lock
, flags
);
169 BUG_ON(uA
>= ARRAY_SIZE(isink_cur
));
171 ret
= regulator_set_current_limit(led
->isink
, isink_cur
[uA
],
174 dev_err(led
->cdev
.dev
, "Failed to set %duA: %d\n",
179 return wm8350_led_enable(led
);
182 static void wm8350_led_shutdown(struct platform_device
*pdev
)
184 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
186 led
->value
= LED_OFF
;
187 wm8350_led_disable(led
);
190 static int wm8350_led_probe(struct platform_device
*pdev
)
192 struct regulator
*isink
, *dcdc
;
193 struct wm8350_led
*led
;
194 struct wm8350_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
198 dev_err(&pdev
->dev
, "no platform data\n");
202 if (pdata
->max_uA
< isink_cur
[0]) {
203 dev_err(&pdev
->dev
, "Invalid maximum current %duA\n",
208 isink
= devm_regulator_get(&pdev
->dev
, "led_isink");
210 dev_err(&pdev
->dev
, "%s: can't get ISINK\n", __func__
);
211 return PTR_ERR(isink
);
214 dcdc
= devm_regulator_get(&pdev
->dev
, "led_vcc");
216 dev_err(&pdev
->dev
, "%s: can't get DCDC\n", __func__
);
217 return PTR_ERR(dcdc
);
220 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
224 led
->cdev
.brightness_set_blocking
= wm8350_led_set
;
225 led
->cdev
.default_trigger
= pdata
->default_trigger
;
226 led
->cdev
.name
= pdata
->name
;
227 led
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
228 led
->enabled
= regulator_is_enabled(isink
);
232 for (i
= 0; i
< ARRAY_SIZE(isink_cur
) - 1; i
++)
233 if (isink_cur
[i
] >= pdata
->max_uA
)
235 led
->max_uA_index
= i
;
236 if (pdata
->max_uA
!= isink_cur
[i
])
238 "Maximum current %duA is not directly supported,"
239 " check platform data\n",
242 spin_lock_init(&led
->value_lock
);
243 led
->value
= LED_OFF
;
244 platform_set_drvdata(pdev
, led
);
246 return led_classdev_register(&pdev
->dev
, &led
->cdev
);
249 static int wm8350_led_remove(struct platform_device
*pdev
)
251 struct wm8350_led
*led
= platform_get_drvdata(pdev
);
253 led_classdev_unregister(&led
->cdev
);
254 wm8350_led_disable(led
);
258 static struct platform_driver wm8350_led_driver
= {
260 .name
= "wm8350-led",
262 .probe
= wm8350_led_probe
,
263 .remove
= wm8350_led_remove
,
264 .shutdown
= wm8350_led_shutdown
,
267 module_platform_driver(wm8350_led_driver
);
269 MODULE_AUTHOR("Mark Brown");
270 MODULE_DESCRIPTION("WM8350 LED driver");
271 MODULE_LICENSE("GPL");
272 MODULE_ALIAS("platform:wm8350-led");