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>
16 #include <linux/slab.h>
18 #include <linux/mfd/wm831x/core.h>
19 #include <linux/mfd/wm831x/pdata.h>
20 #include <linux/mfd/wm831x/regulator.h>
22 struct wm831x_backlight_data
{
23 struct wm831x
*wm831x
;
25 int current_brightness
;
28 static int wm831x_backlight_set(struct backlight_device
*bl
, int brightness
)
30 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
31 struct wm831x
*wm831x
= data
->wm831x
;
32 int power_up
= !data
->current_brightness
&& brightness
;
33 int power_down
= data
->current_brightness
&& !brightness
;
37 /* Enable the ISINK */
38 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
39 WM831X_CS1_ENA
, WM831X_CS1_ENA
);
43 /* Enable the DC-DC */
44 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
45 WM831X_DC4_ENA
, WM831X_DC4_ENA
);
52 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
58 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
59 WM831X_CS1_DRIVE
| WM831X_CS1_ENA
, 0);
64 /* Set the new brightness */
65 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
66 WM831X_CS1_ISEL_MASK
, brightness
);
71 /* Drive current through the ISINK */
72 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
73 WM831X_CS1_DRIVE
, WM831X_CS1_DRIVE
);
78 data
->current_brightness
= brightness
;
83 /* If we were in the middle of a power transition always shut down
86 if (power_up
|| power_down
) {
87 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
88 wm831x_set_bits(wm831x
, data
->isink_reg
, WM831X_CS1_ENA
, 0);
94 static int wm831x_backlight_update_status(struct backlight_device
*bl
)
96 int brightness
= bl
->props
.brightness
;
98 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
101 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
104 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
107 return wm831x_backlight_set(bl
, brightness
);
110 static int wm831x_backlight_get_brightness(struct backlight_device
*bl
)
112 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
113 return data
->current_brightness
;
116 static const struct backlight_ops wm831x_backlight_ops
= {
117 .options
= BL_CORE_SUSPENDRESUME
,
118 .update_status
= wm831x_backlight_update_status
,
119 .get_brightness
= wm831x_backlight_get_brightness
,
122 static int wm831x_backlight_probe(struct platform_device
*pdev
)
124 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
125 struct wm831x_pdata
*wm831x_pdata
;
126 struct wm831x_backlight_pdata
*pdata
;
127 struct wm831x_backlight_data
*data
;
128 struct backlight_device
*bl
;
129 struct backlight_properties props
;
130 int ret
, i
, max_isel
, isink_reg
, dcdc_cfg
;
132 /* We need platform data */
133 if (pdev
->dev
.parent
->platform_data
) {
134 wm831x_pdata
= pdev
->dev
.parent
->platform_data
;
135 pdata
= wm831x_pdata
->backlight
;
141 dev_err(&pdev
->dev
, "No platform data supplied\n");
145 /* Figure out the maximum current we can use */
146 for (i
= 0; i
< WM831X_ISINK_MAX_ISEL
; i
++) {
147 if (wm831x_isinkv_values
[i
] > pdata
->max_uA
)
152 dev_err(&pdev
->dev
, "Invalid max_uA: %duA\n", pdata
->max_uA
);
157 if (pdata
->max_uA
!= wm831x_isinkv_values
[max_isel
])
159 "Maximum current is %duA not %duA as requested\n",
160 wm831x_isinkv_values
[max_isel
], pdata
->max_uA
);
162 switch (pdata
->isink
) {
164 isink_reg
= WM831X_CURRENT_SINK_1
;
168 isink_reg
= WM831X_CURRENT_SINK_2
;
169 dcdc_cfg
= WM831X_DC4_FBSRC
;
172 dev_err(&pdev
->dev
, "Invalid ISINK %d\n", pdata
->isink
);
176 /* Configure the ISINK to use for feedback */
177 ret
= wm831x_reg_unlock(wm831x
);
181 ret
= wm831x_set_bits(wm831x
, WM831X_DC4_CONTROL
, WM831X_DC4_FBSRC
,
184 wm831x_reg_lock(wm831x
);
188 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
192 data
->wm831x
= wm831x
;
193 data
->current_brightness
= 0;
194 data
->isink_reg
= isink_reg
;
196 props
.type
= BACKLIGHT_RAW
;
197 props
.max_brightness
= max_isel
;
198 bl
= backlight_device_register("wm831x", &pdev
->dev
, data
,
199 &wm831x_backlight_ops
, &props
);
201 dev_err(&pdev
->dev
, "failed to register backlight\n");
206 bl
->props
.brightness
= max_isel
;
208 platform_set_drvdata(pdev
, bl
);
210 /* Disable the DCDC if it was started so we can bootstrap */
211 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
214 backlight_update_status(bl
);
219 static int wm831x_backlight_remove(struct platform_device
*pdev
)
221 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
222 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
224 backlight_device_unregister(bl
);
229 static struct platform_driver wm831x_backlight_driver
= {
231 .name
= "wm831x-backlight",
232 .owner
= THIS_MODULE
,
234 .probe
= wm831x_backlight_probe
,
235 .remove
= wm831x_backlight_remove
,
238 static int __init
wm831x_backlight_init(void)
240 return platform_driver_register(&wm831x_backlight_driver
);
242 module_init(wm831x_backlight_init
);
244 static void __exit
wm831x_backlight_exit(void)
246 platform_driver_unregister(&wm831x_backlight_driver
);
248 module_exit(wm831x_backlight_exit
);
250 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
251 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
252 MODULE_LICENSE("GPL");
253 MODULE_ALIAS("platform:wm831x-backlight");