1 /*
2  * Backlight driver for Wolfson Microelectronics WM831x PMICs
3  *
4  * Copyright 2009 Wolfson Microelectonics plc
5  *
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.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/slab.h>
18 
19 #include <linux/mfd/wm831x/core.h>
20 #include <linux/mfd/wm831x/pdata.h>
21 #include <linux/mfd/wm831x/regulator.h>
22 
23 struct wm831x_backlight_data {
24 	struct wm831x *wm831x;
25 	int isink_reg;
26 	int current_brightness;
27 };
28 
29 static int wm831x_backlight_set(struct backlight_device *bl, int brightness)
30 {
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;
35 	int ret;
36 
37 	if (power_up) {
38 		/* Enable the ISINK */
39 		ret = wm831x_set_bits(wm831x, data->isink_reg,
40 				      WM831X_CS1_ENA, WM831X_CS1_ENA);
41 		if (ret < 0)
42 			goto err;
43 
44 		/* Enable the DC-DC */
45 		ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
46 				      WM831X_DC4_ENA, WM831X_DC4_ENA);
47 		if (ret < 0)
48 			goto err;
49 	}
50 
51 	if (power_down) {
52 		/* DCDC first */
53 		ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
54 				      WM831X_DC4_ENA, 0);
55 		if (ret < 0)
56 			goto err;
57 
58 		/* ISINK */
59 		ret = wm831x_set_bits(wm831x, data->isink_reg,
60 				      WM831X_CS1_DRIVE | WM831X_CS1_ENA, 0);
61 		if (ret < 0)
62 			goto err;
63 	}
64 
65 	/* Set the new brightness */
66 	ret = wm831x_set_bits(wm831x, data->isink_reg,
67 			      WM831X_CS1_ISEL_MASK, brightness);
68 	if (ret < 0)
69 		goto err;
70 
71 	if (power_up) {
72 		/* Drive current through the ISINK */
73 		ret = wm831x_set_bits(wm831x, data->isink_reg,
74 				      WM831X_CS1_DRIVE, WM831X_CS1_DRIVE);
75 		if (ret < 0)
76 			return ret;
77 	}
78 
79 	data->current_brightness = brightness;
80 
81 	return 0;
82 
83 err:
84 	/* If we were in the middle of a power transition always shut down
85 	 * for safety.
86 	 */
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);
90 	}
91 
92 	return ret;
93 }
94 
95 static int wm831x_backlight_update_status(struct backlight_device *bl)
96 {
97 	int brightness = bl->props.brightness;
98 
99 	if (bl->props.power != FB_BLANK_UNBLANK)
100 		brightness = 0;
101 
102 	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
103 		brightness = 0;
104 
105 	if (bl->props.state & BL_CORE_SUSPENDED)
106 		brightness = 0;
107 
108 	return wm831x_backlight_set(bl, brightness);
109 }
110 
111 static int wm831x_backlight_get_brightness(struct backlight_device *bl)
112 {
113 	struct wm831x_backlight_data *data = bl_get_data(bl);
114 	return data->current_brightness;
115 }
116 
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,
121 };
122 
123 static int wm831x_backlight_probe(struct platform_device *pdev)
124 {
125 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
126 	struct wm831x_pdata *wm831x_pdata = dev_get_platdata(pdev->dev.parent);
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;
132 
133 	/* We need platform data */
134 	if (wm831x_pdata)
135 		pdata = wm831x_pdata->backlight;
136 	else
137 		pdata = NULL;
138 
139 	if (!pdata) {
140 		dev_err(&pdev->dev, "No platform data supplied\n");
141 		return -EINVAL;
142 	}
143 
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)
147 			break;
148 	}
149 
150 	if (i == 0) {
151 		dev_err(&pdev->dev, "Invalid max_uA: %duA\n", pdata->max_uA);
152 		return -EINVAL;
153 	}
154 	max_isel = i - 1;
155 
156 	if (pdata->max_uA != wm831x_isinkv_values[max_isel])
157 		dev_warn(&pdev->dev,
158 			 "Maximum current is %duA not %duA as requested\n",
159 			 wm831x_isinkv_values[max_isel], pdata->max_uA);
160 
161 	switch (pdata->isink) {
162 	case 1:
163 		isink_reg = WM831X_CURRENT_SINK_1;
164 		dcdc_cfg = 0;
165 		break;
166 	case 2:
167 		isink_reg = WM831X_CURRENT_SINK_2;
168 		dcdc_cfg = WM831X_DC4_FBSRC;
169 		break;
170 	default:
171 		dev_err(&pdev->dev, "Invalid ISINK %d\n", pdata->isink);
172 		return -EINVAL;
173 	}
174 
175 	/* Configure the ISINK to use for feedback */
176 	ret = wm831x_reg_unlock(wm831x);
177 	if (ret < 0)
178 		return ret;
179 
180 	ret = wm831x_set_bits(wm831x, WM831X_DC4_CONTROL, WM831X_DC4_FBSRC,
181 			      dcdc_cfg);
182 
183 	wm831x_reg_lock(wm831x);
184 	if (ret < 0)
185 		return ret;
186 
187 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
188 	if (data == NULL)
189 		return -ENOMEM;
190 
191 	data->wm831x = wm831x;
192 	data->current_brightness = 0;
193 	data->isink_reg = isink_reg;
194 
195 	memset(&props, 0, sizeof(props));
196 	props.type = BACKLIGHT_RAW;
197 	props.max_brightness = max_isel;
198 	bl = devm_backlight_device_register(&pdev->dev, "wm831x", &pdev->dev,
199 					data, &wm831x_backlight_ops, &props);
200 	if (IS_ERR(bl)) {
201 		dev_err(&pdev->dev, "failed to register backlight\n");
202 		return PTR_ERR(bl);
203 	}
204 
205 	bl->props.brightness = max_isel;
206 
207 	platform_set_drvdata(pdev, bl);
208 
209 	/* Disable the DCDC if it was started so we can bootstrap */
210 	wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0);
211 
212 	backlight_update_status(bl);
213 
214 	return 0;
215 }
216 
217 static struct platform_driver wm831x_backlight_driver = {
218 	.driver		= {
219 		.name	= "wm831x-backlight",
220 		.owner	= THIS_MODULE,
221 	},
222 	.probe		= wm831x_backlight_probe,
223 };
224 
225 module_platform_driver(wm831x_backlight_driver);
226 
227 MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
228 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
229 MODULE_LICENSE("GPL");
230 MODULE_ALIAS("platform:wm831x-backlight");
231