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>
14 #include <linux/module.h>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/pdata.h>
21 #include <linux/mfd/wm831x/regulator.h>
23 struct wm831x_backlight_data
{
24 struct wm831x
*wm831x
;
26 int current_brightness
;
29 static int wm831x_backlight_set(struct backlight_device
*bl
, int brightness
)
31 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
32 struct wm831x
*wm831x
= data
->wm831x
;
33 int power_up
= !data
->current_brightness
&& brightness
;
34 int power_down
= data
->current_brightness
&& !brightness
;
38 /* Enable the ISINK */
39 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
40 WM831X_CS1_ENA
, WM831X_CS1_ENA
);
44 /* Enable the DC-DC */
45 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
46 WM831X_DC4_ENA
, WM831X_DC4_ENA
);
53 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
59 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
60 WM831X_CS1_DRIVE
| WM831X_CS1_ENA
, 0);
65 /* Set the new brightness */
66 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
67 WM831X_CS1_ISEL_MASK
, brightness
);
72 /* Drive current through the ISINK */
73 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
74 WM831X_CS1_DRIVE
, WM831X_CS1_DRIVE
);
79 data
->current_brightness
= brightness
;
84 /* If we were in the middle of a power transition always shut down
87 if (power_up
|| power_down
) {
88 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
89 wm831x_set_bits(wm831x
, data
->isink_reg
, WM831X_CS1_ENA
, 0);
95 static int wm831x_backlight_update_status(struct backlight_device
*bl
)
97 int brightness
= bl
->props
.brightness
;
99 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
102 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
105 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
108 return wm831x_backlight_set(bl
, brightness
);
111 static int wm831x_backlight_get_brightness(struct backlight_device
*bl
)
113 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
114 return data
->current_brightness
;
117 static const struct backlight_ops wm831x_backlight_ops
= {
118 .options
= BL_CORE_SUSPENDRESUME
,
119 .update_status
= wm831x_backlight_update_status
,
120 .get_brightness
= wm831x_backlight_get_brightness
,
123 static int wm831x_backlight_probe(struct platform_device
*pdev
)
125 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
126 struct wm831x_pdata
*wm831x_pdata
;
127 struct wm831x_backlight_pdata
*pdata
;
128 struct wm831x_backlight_data
*data
;
129 struct backlight_device
*bl
;
130 struct backlight_properties props
;
131 int ret
, i
, max_isel
, isink_reg
, dcdc_cfg
;
133 /* We need platform data */
134 if (pdev
->dev
.parent
->platform_data
) {
135 wm831x_pdata
= pdev
->dev
.parent
->platform_data
;
136 pdata
= wm831x_pdata
->backlight
;
142 dev_err(&pdev
->dev
, "No platform data supplied\n");
146 /* Figure out the maximum current we can use */
147 for (i
= 0; i
< WM831X_ISINK_MAX_ISEL
; i
++) {
148 if (wm831x_isinkv_values
[i
] > pdata
->max_uA
)
153 dev_err(&pdev
->dev
, "Invalid max_uA: %duA\n", pdata
->max_uA
);
158 if (pdata
->max_uA
!= wm831x_isinkv_values
[max_isel
])
160 "Maximum current is %duA not %duA as requested\n",
161 wm831x_isinkv_values
[max_isel
], pdata
->max_uA
);
163 switch (pdata
->isink
) {
165 isink_reg
= WM831X_CURRENT_SINK_1
;
169 isink_reg
= WM831X_CURRENT_SINK_2
;
170 dcdc_cfg
= WM831X_DC4_FBSRC
;
173 dev_err(&pdev
->dev
, "Invalid ISINK %d\n", pdata
->isink
);
177 /* Configure the ISINK to use for feedback */
178 ret
= wm831x_reg_unlock(wm831x
);
182 ret
= wm831x_set_bits(wm831x
, WM831X_DC4_CONTROL
, WM831X_DC4_FBSRC
,
185 wm831x_reg_lock(wm831x
);
189 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
193 data
->wm831x
= wm831x
;
194 data
->current_brightness
= 0;
195 data
->isink_reg
= isink_reg
;
197 props
.type
= BACKLIGHT_RAW
;
198 props
.max_brightness
= max_isel
;
199 bl
= backlight_device_register("wm831x", &pdev
->dev
, data
,
200 &wm831x_backlight_ops
, &props
);
202 dev_err(&pdev
->dev
, "failed to register backlight\n");
207 bl
->props
.brightness
= max_isel
;
209 platform_set_drvdata(pdev
, bl
);
211 /* Disable the DCDC if it was started so we can bootstrap */
212 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
215 backlight_update_status(bl
);
220 static int wm831x_backlight_remove(struct platform_device
*pdev
)
222 struct backlight_device
*bl
= platform_get_drvdata(pdev
);
223 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
225 backlight_device_unregister(bl
);
230 static struct platform_driver wm831x_backlight_driver
= {
232 .name
= "wm831x-backlight",
233 .owner
= THIS_MODULE
,
235 .probe
= wm831x_backlight_probe
,
236 .remove
= wm831x_backlight_remove
,
239 module_platform_driver(wm831x_backlight_driver
);
241 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
242 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
243 MODULE_LICENSE("GPL");
244 MODULE_ALIAS("platform:wm831x-backlight");