Revert "backlight: fix memory leak on obscure error path"
[linux-2.6.git] / drivers / video / backlight / 88pm860x_bl.c
blobf49181c7311390c471b0abc6caa6d8ca6ae4de91
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 backlight_power_set(struct pm860x_chip *chip, int port,
71 int on)
73 int ret = -EINVAL;
75 switch (port) {
76 case PM8606_BACKLIGHT1:
77 ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) :
78 pm8606_osc_disable(chip, WLED1_DUTY);
79 break;
80 case PM8606_BACKLIGHT2:
81 ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) :
82 pm8606_osc_disable(chip, WLED2_DUTY);
83 break;
84 case PM8606_BACKLIGHT3:
85 ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) :
86 pm8606_osc_disable(chip, WLED3_DUTY);
87 break;
89 return ret;
92 static int pm860x_backlight_set(struct backlight_device *bl, int brightness)
94 struct pm860x_backlight_data *data = bl_get_data(bl);
95 struct pm860x_chip *chip = data->chip;
96 unsigned char value;
97 int ret;
99 if (brightness > MAX_BRIGHTNESS)
100 value = MAX_BRIGHTNESS;
101 else
102 value = brightness;
104 if (brightness)
105 backlight_power_set(chip, data->port, 1);
107 ret = pm860x_reg_write(data->i2c, wled_a(data->port), value);
108 if (ret < 0)
109 goto out;
111 if ((data->current_brightness == 0) && brightness) {
112 if (data->iset) {
113 ret = pm860x_set_bits(data->i2c, wled_idc(data->port),
114 CURRENT_BITMASK, data->iset);
115 if (ret < 0)
116 goto out;
118 if (data->pwm) {
119 ret = pm860x_set_bits(data->i2c, PM8606_PWM,
120 PM8606_PWM_FREQ_MASK, data->pwm);
121 if (ret < 0)
122 goto out;
124 if (brightness == MAX_BRIGHTNESS) {
125 /* set WLED_ON bit as 100% */
126 ret = pm860x_set_bits(data->i2c, wled_b(data->port),
127 PM8606_WLED_ON, PM8606_WLED_ON);
129 } else {
130 if (brightness == MAX_BRIGHTNESS) {
131 /* set WLED_ON bit as 100% */
132 ret = pm860x_set_bits(data->i2c, wled_b(data->port),
133 PM8606_WLED_ON, PM8606_WLED_ON);
134 } else {
135 /* clear WLED_ON bit since it's not 100% */
136 ret = pm860x_set_bits(data->i2c, wled_b(data->port),
137 PM8606_WLED_ON, 0);
140 if (ret < 0)
141 goto out;
143 if (brightness == 0)
144 backlight_power_set(chip, data->port, 0);
146 dev_dbg(chip->dev, "set brightness %d\n", value);
147 data->current_brightness = value;
148 return 0;
149 out:
150 dev_dbg(chip->dev, "set brightness %d failure with return "
151 "value:%d\n", value, ret);
152 return ret;
155 static int pm860x_backlight_update_status(struct backlight_device *bl)
157 int brightness = bl->props.brightness;
159 if (bl->props.power != FB_BLANK_UNBLANK)
160 brightness = 0;
162 if (bl->props.fb_blank != FB_BLANK_UNBLANK)
163 brightness = 0;
165 if (bl->props.state & BL_CORE_SUSPENDED)
166 brightness = 0;
168 return pm860x_backlight_set(bl, brightness);
171 static int pm860x_backlight_get_brightness(struct backlight_device *bl)
173 struct pm860x_backlight_data *data = bl_get_data(bl);
174 struct pm860x_chip *chip = data->chip;
175 int ret;
177 ret = pm860x_reg_read(data->i2c, wled_a(data->port));
178 if (ret < 0)
179 goto out;
180 data->current_brightness = ret;
181 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
182 return data->current_brightness;
183 out:
184 return -EINVAL;
187 static const struct backlight_ops pm860x_backlight_ops = {
188 .options = BL_CORE_SUSPENDRESUME,
189 .update_status = pm860x_backlight_update_status,
190 .get_brightness = pm860x_backlight_get_brightness,
193 static int pm860x_backlight_probe(struct platform_device *pdev)
195 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
196 struct pm860x_backlight_pdata *pdata = NULL;
197 struct pm860x_backlight_data *data;
198 struct backlight_device *bl;
199 struct resource *res;
200 struct backlight_properties props;
201 char name[MFD_NAME_SIZE];
202 int ret;
204 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
205 if (res == NULL) {
206 dev_err(&pdev->dev, "No I/O resource!\n");
207 return -EINVAL;
210 pdata = pdev->dev.platform_data;
211 if (pdata == NULL) {
212 dev_err(&pdev->dev, "platform data isn't assigned to "
213 "backlight\n");
214 return -EINVAL;
217 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data),
218 GFP_KERNEL);
219 if (data == NULL)
220 return -ENOMEM;
221 strncpy(name, res->name, MFD_NAME_SIZE);
222 data->chip = chip;
223 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \
224 : chip->companion;
225 data->current_brightness = MAX_BRIGHTNESS;
226 data->pwm = pdata->pwm;
227 data->iset = pdata->iset;
228 data->port = pdata->flags;
229 if (data->port < 0) {
230 dev_err(&pdev->dev, "wrong platform data is assigned");
231 return -EINVAL;
234 memset(&props, 0, sizeof(struct backlight_properties));
235 props.type = BACKLIGHT_RAW;
236 props.max_brightness = MAX_BRIGHTNESS;
237 bl = backlight_device_register(name, &pdev->dev, data,
238 &pm860x_backlight_ops, &props);
239 if (IS_ERR(bl)) {
240 dev_err(&pdev->dev, "failed to register backlight\n");
241 return PTR_ERR(bl);
243 bl->props.brightness = MAX_BRIGHTNESS;
245 platform_set_drvdata(pdev, bl);
247 /* read current backlight */
248 ret = pm860x_backlight_get_brightness(bl);
249 if (ret < 0)
250 goto out;
252 backlight_update_status(bl);
253 return 0;
254 out:
255 backlight_device_unregister(bl);
256 return ret;
259 static int pm860x_backlight_remove(struct platform_device *pdev)
261 struct backlight_device *bl = platform_get_drvdata(pdev);
263 backlight_device_unregister(bl);
264 return 0;
267 static struct platform_driver pm860x_backlight_driver = {
268 .driver = {
269 .name = "88pm860x-backlight",
270 .owner = THIS_MODULE,
272 .probe = pm860x_backlight_probe,
273 .remove = pm860x_backlight_remove,
276 module_platform_driver(pm860x_backlight_driver);
278 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
279 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
280 MODULE_LICENSE("GPL");
281 MODULE_ALIAS("platform:88pm860x-backlight");