xref: /linux/drivers/video/backlight/88pm860x_bl.c (revision 6b7b8e488bbdedeccabdd001a78ffcbe43bb8a3a)
1 /*
2  * Backlight driver for Marvell Semiconductor 88PM8606
3  *
4  * Copyright (C) 2009 Marvell International Ltd.
5  *	Haojian Zhuang <haojian.zhuang@marvell.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/fb.h>
17 #include <linux/i2c.h>
18 #include <linux/backlight.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/88pm860x.h>
21 
22 #define MAX_BRIGHTNESS		(0xFF)
23 #define MIN_BRIGHTNESS		(0)
24 
25 #define CURRENT_BITMASK		(0x1F << 1)
26 
27 struct pm860x_backlight_data {
28 	struct pm860x_chip *chip;
29 	struct i2c_client *i2c;
30 	int	current_brightness;
31 	int	port;
32 	int	pwm;
33 	int	iset;
34 };
35 
36 static inline int wled_a(int port)
37 {
38 	int ret;
39 
40 	ret = ((port - PM8606_BACKLIGHT1) << 1) + 2;
41 	return ret;
42 }
43 
44 static inline int wled_b(int port)
45 {
46 	int ret;
47 
48 	ret = ((port - PM8606_BACKLIGHT1) << 1) + 3;
49 	return ret;
50 }
51 
52 /* WLED2 & WLED3 share the same IDC */
53 static inline int wled_idc(int port)
54 {
55 	int ret;
56 
57 	switch (port) {
58 	case PM8606_BACKLIGHT1:
59 	case PM8606_BACKLIGHT2:
60 		ret = ((port - PM8606_BACKLIGHT1) << 1) + 3;
61 		break;
62 	case PM8606_BACKLIGHT3:
63 	default:
64 		ret = ((port - PM8606_BACKLIGHT2) << 1) + 3;
65 		break;
66 	}
67 	return ret;
68 }
69 
70 static int pm860x_backlight_set(struct backlight_device *bl, int brightness)
71 {
72 	struct pm860x_backlight_data *data = bl_get_data(bl);
73 	struct pm860x_chip *chip = data->chip;
74 	unsigned char value;
75 	int ret;
76 
77 	if (brightness > MAX_BRIGHTNESS)
78 		value = MAX_BRIGHTNESS;
79 	else
80 		value = brightness;
81 
82 	ret = pm860x_reg_write(data->i2c, wled_a(data->port), value);
83 	if (ret < 0)
84 		goto out;
85 
86 	if ((data->current_brightness == 0) && brightness) {
87 		if (data->iset) {
88 			ret = pm860x_set_bits(data->i2c, wled_idc(data->port),
89 					      CURRENT_BITMASK, data->iset);
90 			if (ret < 0)
91 				goto out;
92 		}
93 		if (data->pwm) {
94 			ret = pm860x_set_bits(data->i2c, PM8606_PWM,
95 					      PM8606_PWM_FREQ_MASK, data->pwm);
96 			if (ret < 0)
97 				goto out;
98 		}
99 		if (brightness == MAX_BRIGHTNESS) {
100 			/* set WLED_ON bit as 100% */
101 			ret = pm860x_set_bits(data->i2c, wled_b(data->port),
102 					      PM8606_WLED_ON, PM8606_WLED_ON);
103 		}
104 	} else {
105 		if (brightness == MAX_BRIGHTNESS) {
106 			/* set WLED_ON bit as 100% */
107 			ret = pm860x_set_bits(data->i2c, wled_b(data->port),
108 					      PM8606_WLED_ON, PM8606_WLED_ON);
109 		} else {
110 			/* clear WLED_ON bit since it's not 100% */
111 			ret = pm860x_set_bits(data->i2c, wled_b(data->port),
112 					      PM8606_WLED_ON, 0);
113 		}
114 	}
115 	if (ret < 0)
116 		goto out;
117 
118 	dev_dbg(chip->dev, "set brightness %d\n", value);
119 	data->current_brightness = value;
120 	return 0;
121 out:
122 	dev_dbg(chip->dev, "set brightness %d failure with return "
123 		"value:%d\n", value, ret);
124 	return ret;
125 }
126 
127 static int pm860x_backlight_update_status(struct backlight_device *bl)
128 {
129 	int brightness = bl->props.brightness;
130 
131 	if (bl->props.power != FB_BLANK_UNBLANK)
132 		brightness = 0;
133 
134 	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
135 		brightness = 0;
136 
137 	if (bl->props.state & BL_CORE_SUSPENDED)
138 		brightness = 0;
139 
140 	return pm860x_backlight_set(bl, brightness);
141 }
142 
143 static int pm860x_backlight_get_brightness(struct backlight_device *bl)
144 {
145 	struct pm860x_backlight_data *data = bl_get_data(bl);
146 	struct pm860x_chip *chip = data->chip;
147 	int ret;
148 
149 	ret = pm860x_reg_read(data->i2c, wled_a(data->port));
150 	if (ret < 0)
151 		goto out;
152 	data->current_brightness = ret;
153 	dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
154 	return data->current_brightness;
155 out:
156 	return -EINVAL;
157 }
158 
159 static const struct backlight_ops pm860x_backlight_ops = {
160 	.options	= BL_CORE_SUSPENDRESUME,
161 	.update_status	= pm860x_backlight_update_status,
162 	.get_brightness	= pm860x_backlight_get_brightness,
163 };
164 
165 static int pm860x_backlight_probe(struct platform_device *pdev)
166 {
167 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
168 	struct pm860x_backlight_pdata *pdata = NULL;
169 	struct pm860x_backlight_data *data;
170 	struct backlight_device *bl;
171 	struct mfd_cell *cell;
172 	struct resource *res;
173 	struct backlight_properties props;
174 	unsigned char value;
175 	char name[MFD_NAME_SIZE];
176 	int ret;
177 
178 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
179 	if (res == NULL) {
180 		dev_err(&pdev->dev, "No I/O resource!\n");
181 		return -EINVAL;
182 	}
183 
184 	cell = pdev->dev.platform_data;
185 	if (cell == NULL)
186 		return -ENODEV;
187 	pdata = cell->mfd_data;
188 	if (pdata == NULL) {
189 		dev_err(&pdev->dev, "platform data isn't assigned to "
190 			"backlight\n");
191 		return -EINVAL;
192 	}
193 
194 	data = kzalloc(sizeof(struct pm860x_backlight_data), GFP_KERNEL);
195 	if (data == NULL)
196 		return -ENOMEM;
197 	strncpy(name, res->name, MFD_NAME_SIZE);
198 	data->chip = chip;
199 	data->i2c = (chip->id == CHIP_PM8606) ? chip->client	\
200 			: chip->companion;
201 	data->current_brightness = MAX_BRIGHTNESS;
202 	data->pwm = pdata->pwm;
203 	data->iset = pdata->iset;
204 	data->port = pdata->flags;
205 	if (data->port < 0) {
206 		dev_err(&pdev->dev, "wrong platform data is assigned");
207 		kfree(data);
208 		return -EINVAL;
209 	}
210 
211 	memset(&props, 0, sizeof(struct backlight_properties));
212 	props.type = BACKLIGHT_RAW;
213 	props.max_brightness = MAX_BRIGHTNESS;
214 	bl = backlight_device_register(name, &pdev->dev, data,
215 					&pm860x_backlight_ops, &props);
216 	if (IS_ERR(bl)) {
217 		dev_err(&pdev->dev, "failed to register backlight\n");
218 		kfree(data);
219 		return PTR_ERR(bl);
220 	}
221 	bl->props.brightness = MAX_BRIGHTNESS;
222 
223 	platform_set_drvdata(pdev, bl);
224 
225 	/* Enable reference VSYS */
226 	ret = pm860x_reg_read(data->i2c, PM8606_VSYS);
227 	if (ret < 0)
228 		goto out;
229 	if ((ret & PM8606_VSYS_EN) == 0) {
230 		value = ret | PM8606_VSYS_EN;
231 		ret = pm860x_reg_write(data->i2c, PM8606_VSYS, value);
232 		if (ret < 0)
233 			goto out;
234 	}
235 	/* Enable reference OSC */
236 	ret = pm860x_reg_read(data->i2c, PM8606_MISC);
237 	if (ret < 0)
238 		goto out;
239 	if ((ret & PM8606_MISC_OSC_EN) == 0) {
240 		value = ret | PM8606_MISC_OSC_EN;
241 		ret = pm860x_reg_write(data->i2c, PM8606_MISC, value);
242 		if (ret < 0)
243 			goto out;
244 	}
245 	/* read current backlight */
246 	ret = pm860x_backlight_get_brightness(bl);
247 	if (ret < 0)
248 		goto out;
249 
250 	backlight_update_status(bl);
251 	return 0;
252 out:
253 	backlight_device_unregister(bl);
254 	kfree(data);
255 	return ret;
256 }
257 
258 static int pm860x_backlight_remove(struct platform_device *pdev)
259 {
260 	struct backlight_device *bl = platform_get_drvdata(pdev);
261 	struct pm860x_backlight_data *data = bl_get_data(bl);
262 
263 	backlight_device_unregister(bl);
264 	kfree(data);
265 	return 0;
266 }
267 
268 static struct platform_driver pm860x_backlight_driver = {
269 	.driver		= {
270 		.name	= "88pm860x-backlight",
271 		.owner	= THIS_MODULE,
272 	},
273 	.probe		= pm860x_backlight_probe,
274 	.remove		= pm860x_backlight_remove,
275 };
276 
277 static int __init pm860x_backlight_init(void)
278 {
279 	return platform_driver_register(&pm860x_backlight_driver);
280 }
281 module_init(pm860x_backlight_init);
282 
283 static void __exit pm860x_backlight_exit(void)
284 {
285 	platform_driver_unregister(&pm860x_backlight_driver);
286 }
287 module_exit(pm860x_backlight_exit);
288 
289 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
290 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
291 MODULE_LICENSE("GPL");
292 MODULE_ALIAS("platform:88pm860x-backlight");
293