blob: 08fd87f3aecce368219ed7cb08dfdaa0aebafef6 [file] [log] [blame]
Mark Browna4f3d552009-09-05 14:09:20 +01001/*
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/fb.h>
15#include <linux/backlight.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Mark Browna4f3d552009-09-05 14:09:20 +010017
18#include <linux/mfd/wm831x/core.h>
19#include <linux/mfd/wm831x/pdata.h>
20#include <linux/mfd/wm831x/regulator.h>
21
22struct wm831x_backlight_data {
23 struct wm831x *wm831x;
24 int isink_reg;
25 int current_brightness;
26};
27
28static int wm831x_backlight_set(struct backlight_device *bl, int brightness)
29{
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;
34 int ret;
35
36 if (power_up) {
37 /* Enable the ISINK */
38 ret = wm831x_set_bits(wm831x, data->isink_reg,
39 WM831X_CS1_ENA, WM831X_CS1_ENA);
40 if (ret < 0)
41 goto err;
42
43 /* Enable the DC-DC */
44 ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
45 WM831X_DC4_ENA, WM831X_DC4_ENA);
46 if (ret < 0)
47 goto err;
48 }
49
50 if (power_down) {
51 /* DCDC first */
52 ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE,
53 WM831X_DC4_ENA, 0);
54 if (ret < 0)
55 goto err;
56
57 /* ISINK */
58 ret = wm831x_set_bits(wm831x, data->isink_reg,
59 WM831X_CS1_DRIVE | WM831X_CS1_ENA, 0);
60 if (ret < 0)
61 goto err;
62 }
63
64 /* Set the new brightness */
65 ret = wm831x_set_bits(wm831x, data->isink_reg,
66 WM831X_CS1_ISEL_MASK, brightness);
67 if (ret < 0)
68 goto err;
69
70 if (power_up) {
71 /* Drive current through the ISINK */
72 ret = wm831x_set_bits(wm831x, data->isink_reg,
73 WM831X_CS1_DRIVE, WM831X_CS1_DRIVE);
74 if (ret < 0)
75 return ret;
76 }
77
78 data->current_brightness = brightness;
79
80 return 0;
81
82err:
83 /* If we were in the middle of a power transition always shut down
84 * for safety.
85 */
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);
89 }
90
91 return ret;
92}
93
94static int wm831x_backlight_update_status(struct backlight_device *bl)
95{
96 int brightness = bl->props.brightness;
97
98 if (bl->props.power != FB_BLANK_UNBLANK)
99 brightness = 0;
100
101 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
102 brightness = 0;
103
104 if (bl->props.state & BL_CORE_SUSPENDED)
105 brightness = 0;
106
107 return wm831x_backlight_set(bl, brightness);
108}
109
110static int wm831x_backlight_get_brightness(struct backlight_device *bl)
111{
112 struct wm831x_backlight_data *data = bl_get_data(bl);
113 return data->current_brightness;
114}
115
Emese Revfy9905a432009-12-14 00:58:57 +0100116static const struct backlight_ops wm831x_backlight_ops = {
Mark Browna4f3d552009-09-05 14:09:20 +0100117 .options = BL_CORE_SUSPENDRESUME,
118 .update_status = wm831x_backlight_update_status,
119 .get_brightness = wm831x_backlight_get_brightness,
120};
121
122static int wm831x_backlight_probe(struct platform_device *pdev)
123{
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;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500129 struct backlight_properties props;
Mark Browna4f3d552009-09-05 14:09:20 +0100130 int ret, i, max_isel, isink_reg, dcdc_cfg;
131
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;
136 } else {
137 pdata = NULL;
138 }
139
140 if (!pdata) {
141 dev_err(&pdev->dev, "No platform data supplied\n");
142 return -EINVAL;
143 }
144
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)
148 break;
149 }
150
151 if (i == 0) {
152 dev_err(&pdev->dev, "Invalid max_uA: %duA\n", pdata->max_uA);
153 return -EINVAL;
154 }
155 max_isel = i - 1;
156
157 if (pdata->max_uA != wm831x_isinkv_values[max_isel])
158 dev_warn(&pdev->dev,
159 "Maximum current is %duA not %duA as requested\n",
160 wm831x_isinkv_values[max_isel], pdata->max_uA);
161
162 switch (pdata->isink) {
163 case 1:
164 isink_reg = WM831X_CURRENT_SINK_1;
165 dcdc_cfg = 0;
166 break;
167 case 2:
168 isink_reg = WM831X_CURRENT_SINK_2;
169 dcdc_cfg = WM831X_DC4_FBSRC;
170 break;
171 default:
172 dev_err(&pdev->dev, "Invalid ISINK %d\n", pdata->isink);
173 return -EINVAL;
174 }
175
176 /* Configure the ISINK to use for feedback */
177 ret = wm831x_reg_unlock(wm831x);
178 if (ret < 0)
179 return ret;
180
181 ret = wm831x_set_bits(wm831x, WM831X_DC4_CONTROL, WM831X_DC4_FBSRC,
182 dcdc_cfg);
183
184 wm831x_reg_lock(wm831x);
185 if (ret < 0)
186 return ret;
187
188 data = kzalloc(sizeof(*data), GFP_KERNEL);
189 if (data == NULL)
190 return -ENOMEM;
191
192 data->wm831x = wm831x;
193 data->current_brightness = 0;
194 data->isink_reg = isink_reg;
195
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500196 props.max_brightness = max_isel;
197 bl = backlight_device_register("wm831x", &pdev->dev, data,
198 &wm831x_backlight_ops, &props);
Mark Browna4f3d552009-09-05 14:09:20 +0100199 if (IS_ERR(bl)) {
200 dev_err(&pdev->dev, "failed to register backlight\n");
201 kfree(data);
202 return PTR_ERR(bl);
203 }
204
Mark Browna4f3d552009-09-05 14:09:20 +0100205 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
213 backlight_update_status(bl);
214
215 return 0;
216}
217
218static int wm831x_backlight_remove(struct platform_device *pdev)
219{
220 struct backlight_device *bl = platform_get_drvdata(pdev);
221 struct wm831x_backlight_data *data = bl_get_data(bl);
222
223 backlight_device_unregister(bl);
224 kfree(data);
225 return 0;
226}
227
228static struct platform_driver wm831x_backlight_driver = {
229 .driver = {
230 .name = "wm831x-backlight",
231 .owner = THIS_MODULE,
232 },
233 .probe = wm831x_backlight_probe,
234 .remove = wm831x_backlight_remove,
235};
236
237static int __init wm831x_backlight_init(void)
238{
239 return platform_driver_register(&wm831x_backlight_driver);
240}
241module_init(wm831x_backlight_init);
242
243static void __exit wm831x_backlight_exit(void)
244{
245 platform_driver_unregister(&wm831x_backlight_driver);
246}
247module_exit(wm831x_backlight_exit);
248
249MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
250MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
251MODULE_LICENSE("GPL");
252MODULE_ALIAS("platform:wm831x-backlight");