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 int ret
, i
, max_isel
, isink_reg
, dcdc_cfg
;
130 /* We need platform data */
131 if (pdev
->dev
.parent
->platform_data
) {
132 wm831x_pdata
= pdev
->dev
.parent
->platform_data
;
133 pdata
= wm831x_pdata
->backlight
;
139 dev_err(&pdev
->dev
, "No platform data supplied\n");
143 /* Figure out the maximum current we can use */
144 for (i
= 0; i
< WM831X_ISINK_MAX_ISEL
; i
++) {
145 if (wm831x_isinkv_values
[i
] > pdata
->max_uA
)
150 dev_err(&pdev
->dev
, "Invalid max_uA: %duA\n", pdata
->max_uA
);
155 if (pdata
->max_uA
!= wm831x_isinkv_values
[max_isel
])
157 "Maximum current is %duA not %duA as requested\n",
158 wm831x_isinkv_values
[max_isel
], pdata
->max_uA
);
160 switch (pdata
->isink
) {
162 isink_reg
= WM831X_CURRENT_SINK_1
;
166 isink_reg
= WM831X_CURRENT_SINK_2
;
167 dcdc_cfg
= WM831X_DC4_FBSRC
;
170 dev_err(&pdev
->dev
, "Invalid ISINK %d\n", pdata
->isink
);
174 /* Configure the ISINK to use for feedback */
175 ret
= wm831x_reg_unlock(wm831x
);
179 ret
= wm831x_set_bits(wm831x
, WM831X_DC4_CONTROL
, WM831X_DC4_FBSRC
,
182 wm831x_reg_lock(wm831x
);
186 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
190 data
->wm831x
= wm831x
;
191 data
->current_brightness
= 0;
192 data
->isink_reg
= isink_reg
;
194 bl
= backlight_device_register("wm831x", &pdev
->dev
,
195 data
, &wm831x_backlight_ops
);
197 dev_err(&pdev
->dev
, "failed to register backlight\n");
202 bl
->props
.max_brightness
= max_isel
;
203 bl
->props
.brightness
= max_isel
;
205 platform_set_drvdata(pdev
, bl
);
207 /* Disable the DCDC if it was started so we can bootstrap */
208 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
211 backlight_update_status(bl
);
216 static int wm831x_backlight_remove(struct platform_device
*pdev
)
218 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
219 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
221 backlight_device_unregister(bl
);
226 static struct platform_driver wm831x_backlight_driver
= {
228 .name
= "wm831x-backlight",
229 .owner
= THIS_MODULE
,
231 .probe
= wm831x_backlight_probe
,
232 .remove
= wm831x_backlight_remove
,
235 static int __init
wm831x_backlight_init(void)
237 return platform_driver_register(&wm831x_backlight_driver
);
239 module_init(wm831x_backlight_init
);
241 static void __exit
wm831x_backlight_exit(void)
243 platform_driver_unregister(&wm831x_backlight_driver
);
245 module_exit(wm831x_backlight_exit
);
247 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
248 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
249 MODULE_LICENSE("GPL");
250 MODULE_ALIAS("platform:wm831x-backlight");