blob: 6eab0d6c262aab5d11fa8d4da17f3c5d0e7d8e6c [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>
Paul Gortmaker355b2002011-07-03 16:17:28 -040014#include <linux/module.h>
Mark Browna4f3d552009-09-05 14:09:20 +010015#include <linux/fb.h>
16#include <linux/backlight.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mark Browna4f3d552009-09-05 14:09:20 +010018
19#include <linux/mfd/wm831x/core.h>
20#include <linux/mfd/wm831x/pdata.h>
21#include <linux/mfd/wm831x/regulator.h>
22
23struct wm831x_backlight_data {
24 struct wm831x *wm831x;
25 int isink_reg;
26 int current_brightness;
27};
28
29static 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
83err:
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
95static 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
111static int wm831x_backlight_get_brightness(struct backlight_device *bl)
112{
113 struct wm831x_backlight_data *data = bl_get_data(bl);
Jingoo Han4876b662014-08-27 10:14:06 +0900114
Mark Browna4f3d552009-09-05 14:09:20 +0100115 return data->current_brightness;
116}
117
Emese Revfy9905a432009-12-14 00:58:57 +0100118static const struct backlight_ops wm831x_backlight_ops = {
Mark Browna4f3d552009-09-05 14:09:20 +0100119 .options = BL_CORE_SUSPENDRESUME,
120 .update_status = wm831x_backlight_update_status,
121 .get_brightness = wm831x_backlight_get_brightness,
122};
123
124static int wm831x_backlight_probe(struct platform_device *pdev)
125{
126 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Hanc512794c2013-11-12 15:09:04 -0800127 struct wm831x_pdata *wm831x_pdata = dev_get_platdata(pdev->dev.parent);
Mark Browna4f3d552009-09-05 14:09:20 +0100128 struct wm831x_backlight_pdata *pdata;
129 struct wm831x_backlight_data *data;
130 struct backlight_device *bl;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500131 struct backlight_properties props;
Mark Browna4f3d552009-09-05 14:09:20 +0100132 int ret, i, max_isel, isink_reg, dcdc_cfg;
133
134 /* We need platform data */
Jingoo Hanc512794c2013-11-12 15:09:04 -0800135 if (wm831x_pdata)
Mark Browna4f3d552009-09-05 14:09:20 +0100136 pdata = wm831x_pdata->backlight;
Jingoo Hanc512794c2013-11-12 15:09:04 -0800137 else
Mark Browna4f3d552009-09-05 14:09:20 +0100138 pdata = NULL;
Mark Browna4f3d552009-09-05 14:09:20 +0100139
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
Julia Lawall1107d402012-03-23 15:02:00 -0700188 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Mark Browna4f3d552009-09-05 14:09:20 +0100189 if (data == NULL)
190 return -ENOMEM;
191
192 data->wm831x = wm831x;
193 data->current_brightness = 0;
194 data->isink_reg = isink_reg;
195
Corentin Charyf5f4fd42012-05-29 15:07:20 -0700196 memset(&props, 0, sizeof(props));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700197 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500198 props.max_brightness = max_isel;
Jingoo Hanf369e662013-11-12 15:09:25 -0800199 bl = devm_backlight_device_register(&pdev->dev, "wm831x", &pdev->dev,
200 data, &wm831x_backlight_ops, &props);
Mark Browna4f3d552009-09-05 14:09:20 +0100201 if (IS_ERR(bl)) {
202 dev_err(&pdev->dev, "failed to register backlight\n");
Mark Browna4f3d552009-09-05 14:09:20 +0100203 return PTR_ERR(bl);
204 }
205
Mark Browna4f3d552009-09-05 14:09:20 +0100206 bl->props.brightness = max_isel;
207
208 platform_set_drvdata(pdev, bl);
209
210 /* Disable the DCDC if it was started so we can bootstrap */
211 wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0);
212
Mark Browna4f3d552009-09-05 14:09:20 +0100213 backlight_update_status(bl);
214
215 return 0;
216}
217
Mark Browna4f3d552009-09-05 14:09:20 +0100218static struct platform_driver wm831x_backlight_driver = {
219 .driver = {
220 .name = "wm831x-backlight",
Mark Browna4f3d552009-09-05 14:09:20 +0100221 },
222 .probe = wm831x_backlight_probe,
Mark Browna4f3d552009-09-05 14:09:20 +0100223};
224
Axel Lin81178e02012-01-10 15:09:11 -0800225module_platform_driver(wm831x_backlight_driver);
Mark Browna4f3d552009-09-05 14:09:20 +0100226
227MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs");
228MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com");
229MODULE_LICENSE("GPL");
230MODULE_ALIAS("platform:wm831x-backlight");