1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight driver for Wolfson Microelectronics WM831x PMICs
5 * Copyright 2009 Wolfson Microelectonics plc
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/platform_device.h>
11 #include <linux/module.h>
13 #include <linux/backlight.h>
14 #include <linux/slab.h>
16 #include <linux/mfd/wm831x/core.h>
17 #include <linux/mfd/wm831x/pdata.h>
18 #include <linux/mfd/wm831x/regulator.h>
20 struct wm831x_backlight_data
{
21 struct wm831x
*wm831x
;
23 int current_brightness
;
26 static int wm831x_backlight_set(struct backlight_device
*bl
, int brightness
)
28 struct wm831x_backlight_data
*data
= bl_get_data(bl
);
29 struct wm831x
*wm831x
= data
->wm831x
;
30 int power_up
= !data
->current_brightness
&& brightness
;
31 int power_down
= data
->current_brightness
&& !brightness
;
35 /* Enable the ISINK */
36 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
37 WM831X_CS1_ENA
, WM831X_CS1_ENA
);
41 /* Enable the DC-DC */
42 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
43 WM831X_DC4_ENA
, WM831X_DC4_ENA
);
50 ret
= wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
,
56 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
57 WM831X_CS1_DRIVE
| WM831X_CS1_ENA
, 0);
62 /* Set the new brightness */
63 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
64 WM831X_CS1_ISEL_MASK
, brightness
);
69 /* Drive current through the ISINK */
70 ret
= wm831x_set_bits(wm831x
, data
->isink_reg
,
71 WM831X_CS1_DRIVE
, WM831X_CS1_DRIVE
);
76 data
->current_brightness
= brightness
;
81 /* If we were in the middle of a power transition always shut down
84 if (power_up
|| power_down
) {
85 wm831x_set_bits(wm831x
, WM831X_DCDC_ENABLE
, WM831X_DC4_ENA
, 0);
86 wm831x_set_bits(wm831x
, data
->isink_reg
, WM831X_CS1_ENA
, 0);
92 static int wm831x_backlight_update_status(struct backlight_device
*bl
)
94 int brightness
= bl
->props
.brightness
;
96 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
99 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
102 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
105 return wm831x_backlight_set(bl
, brightness
);
108 static int wm831x_backlight_get_brightness(struct backlight_device
*bl
)
110 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
= dev_get_platdata(pdev
->dev
.parent
);
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 */
133 pdata
= wm831x_pdata
->backlight
;
138 dev_err(&pdev
->dev
, "No platform data supplied\n");
142 /* Figure out the maximum current we can use */
143 for (i
= 0; i
< WM831X_ISINK_MAX_ISEL
; i
++) {
144 if (wm831x_isinkv_values
[i
] > pdata
->max_uA
)
149 dev_err(&pdev
->dev
, "Invalid max_uA: %duA\n", pdata
->max_uA
);
154 if (pdata
->max_uA
!= wm831x_isinkv_values
[max_isel
])
156 "Maximum current is %duA not %duA as requested\n",
157 wm831x_isinkv_values
[max_isel
], pdata
->max_uA
);
159 switch (pdata
->isink
) {
161 isink_reg
= WM831X_CURRENT_SINK_1
;
165 isink_reg
= WM831X_CURRENT_SINK_2
;
166 dcdc_cfg
= WM831X_DC4_FBSRC
;
169 dev_err(&pdev
->dev
, "Invalid ISINK %d\n", pdata
->isink
);
173 /* Configure the ISINK to use for feedback */
174 ret
= wm831x_reg_unlock(wm831x
);
178 ret
= wm831x_set_bits(wm831x
, WM831X_DC4_CONTROL
, WM831X_DC4_FBSRC
,
181 wm831x_reg_lock(wm831x
);
185 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
189 data
->wm831x
= wm831x
;
190 data
->current_brightness
= 0;
191 data
->isink_reg
= isink_reg
;
193 memset(&props
, 0, sizeof(props
));
194 props
.type
= BACKLIGHT_RAW
;
195 props
.max_brightness
= max_isel
;
196 bl
= devm_backlight_device_register(&pdev
->dev
, "wm831x", &pdev
->dev
,
197 data
, &wm831x_backlight_ops
, &props
);
199 dev_err(&pdev
->dev
, "failed to register backlight\n");
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);
210 backlight_update_status(bl
);
215 static struct platform_driver wm831x_backlight_driver
= {
217 .name
= "wm831x-backlight",
219 .probe
= wm831x_backlight_probe
,
222 module_platform_driver(wm831x_backlight_driver
);
224 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
225 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
226 MODULE_LICENSE("GPL");
227 MODULE_ALIAS("platform:wm831x-backlight");