Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / video / backlight / 88pm860x_bl.c
bloba1376dc73d71cd79492c388afc340d7fba229545
1 /*
2 * Backlight driver for Marvell Semiconductor 88PM8606
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
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.
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/88pm860x.h>
20 #include <linux/module.h>
22 #define MAX_BRIGHTNESS (0xFF)
23 #define MIN_BRIGHTNESS (0)
25 #define CURRENT_BITMASK (0x1F << 1)
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;
36 static inline int wled_a(int port)
38 int ret;
40 ret = ((port - PM8606_BACKLIGHT1) << 1) + 2;
41 return ret;
44 static inline int wled_b(int port)
46 int ret;
48 ret = ((port - PM8606_BACKLIGHT1) << 1) + 3;
49 return ret;
52 /* WLED2 & WLED3 share the same IDC */
53 static inline int wled_idc(int port)
55 int ret;
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;
67 return ret;
70 static int pm860x_backlight_set(struct backlight_device *bl, int brightness)
72 struct pm860x_backlight_data *data = bl_get_data(bl);
73 struct pm860x_chip *chip = data->chip;
74 unsigned char value;
75 int ret;
77 if (brightness > MAX_BRIGHTNESS)
78 value = MAX_BRIGHTNESS;
79 else
80 value = brightness;
82 ret = pm860x_reg_write(data->i2c, wled_a(data->port), value);
83 if (ret < 0)
84 goto out;
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;
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;
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);
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);
115 if (ret < 0)
116 goto out;
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;
127 static int pm860x_backlight_update_status(struct backlight_device *bl)
129 int brightness = bl->props.brightness;
131 if (bl->props.power != FB_BLANK_UNBLANK)
132 brightness = 0;
134 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
135 brightness = 0;
137 if (bl->props.state & BL_CORE_SUSPENDED)
138 brightness = 0;
140 return pm860x_backlight_set(bl, brightness);
143 static int pm860x_backlight_get_brightness(struct backlight_device *bl)
145 struct pm860x_backlight_data *data = bl_get_data(bl);
146 struct pm860x_chip *chip = data->chip;
147 int ret;
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;
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,
165 static int pm860x_backlight_probe(struct platform_device *pdev)
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 resource *res;
172 struct backlight_properties props;
173 unsigned char value;
174 char name[MFD_NAME_SIZE];
175 int ret;
177 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
178 if (res == NULL) {
179 dev_err(&pdev->dev, "No I/O resource!\n");
180 return -EINVAL;
183 pdata = pdev->dev.platform_data;
184 if (pdata == NULL) {
185 dev_err(&pdev->dev, "platform data isn't assigned to "
186 "backlight\n");
187 return -EINVAL;
190 data = kzalloc(sizeof(struct pm860x_backlight_data), GFP_KERNEL);
191 if (data == NULL)
192 return -ENOMEM;
193 strncpy(name, res->name, MFD_NAME_SIZE);
194 data->chip = chip;
195 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \
196 : chip->companion;
197 data->current_brightness = MAX_BRIGHTNESS;
198 data->pwm = pdata->pwm;
199 data->iset = pdata->iset;
200 data->port = pdata->flags;
201 if (data->port < 0) {
202 dev_err(&pdev->dev, "wrong platform data is assigned");
203 kfree(data);
204 return -EINVAL;
207 memset(&props, 0, sizeof(struct backlight_properties));
208 props.type = BACKLIGHT_RAW;
209 props.max_brightness = MAX_BRIGHTNESS;
210 bl = backlight_device_register(name, &pdev->dev, data,
211 &pm860x_backlight_ops, &props);
212 if (IS_ERR(bl)) {
213 dev_err(&pdev->dev, "failed to register backlight\n");
214 kfree(data);
215 return PTR_ERR(bl);
217 bl->props.brightness = MAX_BRIGHTNESS;
219 platform_set_drvdata(pdev, bl);
221 /* Enable reference VSYS */
222 ret = pm860x_reg_read(data->i2c, PM8606_VSYS);
223 if (ret < 0)
224 goto out;
225 if ((ret & PM8606_VSYS_EN) == 0) {
226 value = ret | PM8606_VSYS_EN;
227 ret = pm860x_reg_write(data->i2c, PM8606_VSYS, value);
228 if (ret < 0)
229 goto out;
231 /* Enable reference OSC */
232 ret = pm860x_reg_read(data->i2c, PM8606_MISC);
233 if (ret < 0)
234 goto out;
235 if ((ret & PM8606_MISC_OSC_EN) == 0) {
236 value = ret | PM8606_MISC_OSC_EN;
237 ret = pm860x_reg_write(data->i2c, PM8606_MISC, value);
238 if (ret < 0)
239 goto out;
241 /* read current backlight */
242 ret = pm860x_backlight_get_brightness(bl);
243 if (ret < 0)
244 goto out;
246 backlight_update_status(bl);
247 return 0;
248 out:
249 backlight_device_unregister(bl);
250 kfree(data);
251 return ret;
254 static int pm860x_backlight_remove(struct platform_device *pdev)
256 struct backlight_device *bl = platform_get_drvdata(pdev);
257 struct pm860x_backlight_data *data = bl_get_data(bl);
259 backlight_device_unregister(bl);
260 kfree(data);
261 return 0;
264 static struct platform_driver pm860x_backlight_driver = {
265 .driver = {
266 .name = "88pm860x-backlight",
267 .owner = THIS_MODULE,
269 .probe = pm860x_backlight_probe,
270 .remove = pm860x_backlight_remove,
273 module_platform_driver(pm860x_backlight_driver);
275 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
276 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
277 MODULE_LICENSE("GPL");
278 MODULE_ALIAS("platform:88pm860x-backlight");