2 * Backlight driver for Wolfson Microelectronics WM831x PMICs
4 * Copyright 2009 Wolfson Microelectonics 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.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
15 #include <linux/backlight.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/pdata.h>
19 #include <linux/mfd/wm831x/regulator.h>
21 struct wm831x_backlight_data
{
22 struct wm831x
*wm831x
;
24 int current_brightness
;
27 static int wm831x_backlight_set(struct backlight_device
*bl
, int brightness
)
29 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
30 struct wm831x
*wm831x
= data
->wm831x
;
31 int power_up
= !data
->current_brightness
&& brightness
;
32 int power_down
= data
->current_brightness
&& !brightness
;
36 /* Enable the ISINK */
37 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
38 WM831X_CS1_ENA
, WM831X_CS1_ENA
);
42 /* Enable the DC-DC */
43 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
44 WM831X_DC4_ENA
, WM831X_DC4_ENA
);
51 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
57 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
58 WM831X_CS1_DRIVE
| WM831X_CS1_ENA
, 0);
63 /* Set the new brightness */
64 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
65 WM831X_CS1_ISEL_MASK
, brightness
);
70 /* Drive current through the ISINK */
71 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
72 WM831X_CS1_DRIVE
, WM831X_CS1_DRIVE
);
77 data
->current_brightness
= brightness
;
82 /* If we were in the middle of a power transition always shut down
85 if (power_up
|| power_down
) {
86 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
87 wm831x_set_bits(wm831x
, data
->isink_reg
, WM831X_CS1_ENA
, 0);
93 static int wm831x_backlight_update_status(struct backlight_device
*bl
)
95 int brightness
= bl
->props
.brightness
;
97 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
100 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
103 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
106 return wm831x_backlight_set(bl
, brightness
);
109 static int wm831x_backlight_get_brightness(struct backlight_device
*bl
)
111 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
112 return data
->current_brightness
;
115 static const struct backlight_ops wm831x_backlight_ops
= {
116 .options
= BL_CORE_SUSPENDRESUME
,
117 .update_status
= wm831x_backlight_update_status
,
118 .get_brightness
= wm831x_backlight_get_brightness
,
121 static int wm831x_backlight_probe(struct platform_device
*pdev
)
123 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
124 struct wm831x_pdata
*wm831x_pdata
;
125 struct wm831x_backlight_pdata
*pdata
;
126 struct wm831x_backlight_data
*data
;
127 struct backlight_device
*bl
;
128 struct backlight_properties props
;
129 int ret
, i
, max_isel
, isink_reg
, dcdc_cfg
;
131 /* We need platform data */
132 if (pdev
->dev
.parent
->platform_data
) {
133 wm831x_pdata
= pdev
->dev
.parent
->platform_data
;
134 pdata
= wm831x_pdata
->backlight
;
140 dev_err(&pdev
->dev
, "No platform data supplied\n");
144 /* Figure out the maximum current we can use */
145 for (i
= 0; i
< WM831X_ISINK_MAX_ISEL
; i
++) {
146 if (wm831x_isinkv_values
[i
] > pdata
->max_uA
)
151 dev_err(&pdev
->dev
, "Invalid max_uA: %duA\n", pdata
->max_uA
);
156 if (pdata
->max_uA
!= wm831x_isinkv_values
[max_isel
])
158 "Maximum current is %duA not %duA as requested\n",
159 wm831x_isinkv_values
[max_isel
], pdata
->max_uA
);
161 switch (pdata
->isink
) {
163 isink_reg
= WM831X_CURRENT_SINK_1
;
167 isink_reg
= WM831X_CURRENT_SINK_2
;
168 dcdc_cfg
= WM831X_DC4_FBSRC
;
171 dev_err(&pdev
->dev
, "Invalid ISINK %d\n", pdata
->isink
);
175 /* Configure the ISINK to use for feedback */
176 ret
= wm831x_reg_unlock(wm831x
);
180 ret
= wm831x_set_bits(wm831x
, WM831X_DC4_CONTROL
, WM831X_DC4_FBSRC
,
183 wm831x_reg_lock(wm831x
);
187 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
191 data
->wm831x
= wm831x
;
192 data
->current_brightness
= 0;
193 data
->isink_reg
= isink_reg
;
195 props
.max_brightness
= max_isel
;
196 bl
= backlight_device_register("wm831x", &pdev
->dev
, data
,
197 &wm831x_backlight_ops
, &props
);
199 dev_err(&pdev
->dev
, "failed to register backlight\n");
204 bl
->props
.brightness
= max_isel
;
206 platform_set_drvdata(pdev
, bl
);
208 /* Disable the DCDC if it was started so we can bootstrap */
209 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
212 backlight_update_status(bl
);
217 static int wm831x_backlight_remove(struct platform_device
*pdev
)
219 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
220 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
222 backlight_device_unregister(bl
);
227 static struct platform_driver wm831x_backlight_driver
= {
229 .name
= "wm831x-backlight",
230 .owner
= THIS_MODULE
,
232 .probe
= wm831x_backlight_probe
,
233 .remove
= wm831x_backlight_remove
,
236 static int __init
wm831x_backlight_init(void)
238 return platform_driver_register(&wm831x_backlight_driver
);
240 module_init(wm831x_backlight_init
);
242 static void __exit
wm831x_backlight_exit(void)
244 platform_driver_unregister(&wm831x_backlight_driver
);
246 module_exit(wm831x_backlight_exit
);
248 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
249 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
250 MODULE_LICENSE("GPL");
251 MODULE_ALIAS("platform:wm831x-backlight");