drivers/video/backlight/88pm860x_bl.c: drop devm_kfree of devm_kzalloc'd data
[~shefty/rdma-dev.git] / drivers / video / backlight / 88pm860x_bl.c
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/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/fb.h>
18 #include <linux/i2c.h>
19 #include <linux/backlight.h>
20 #include <linux/mfd/88pm860x.h>
21 #include <linux/module.h>
22
23 #define MAX_BRIGHTNESS          (0xFF)
24 #define MIN_BRIGHTNESS          (0)
25
26 #define CURRENT_BITMASK         (0x1F << 1)
27
28 struct pm860x_backlight_data {
29         struct pm860x_chip *chip;
30         struct i2c_client *i2c;
31         int     current_brightness;
32         int     port;
33         int     pwm;
34         int     iset;
35         int     reg_duty_cycle;
36         int     reg_always_on;
37         int     reg_current;
38 };
39
40 static int backlight_power_set(struct pm860x_chip *chip, int port,
41                 int on)
42 {
43         int ret = -EINVAL;
44
45         switch (port) {
46         case 0:
47                 ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) :
48                         pm8606_osc_disable(chip, WLED1_DUTY);
49                 break;
50         case 1:
51                 ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) :
52                         pm8606_osc_disable(chip, WLED2_DUTY);
53                 break;
54         case 2:
55                 ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) :
56                         pm8606_osc_disable(chip, WLED3_DUTY);
57                 break;
58         }
59         return ret;
60 }
61
62 static int pm860x_backlight_set(struct backlight_device *bl, int brightness)
63 {
64         struct pm860x_backlight_data *data = bl_get_data(bl);
65         struct pm860x_chip *chip = data->chip;
66         unsigned char value;
67         int ret;
68
69         if (brightness > MAX_BRIGHTNESS)
70                 value = MAX_BRIGHTNESS;
71         else
72                 value = brightness;
73
74         if (brightness)
75                 backlight_power_set(chip, data->port, 1);
76
77         ret = pm860x_reg_write(data->i2c, data->reg_duty_cycle, value);
78         if (ret < 0)
79                 goto out;
80
81         if ((data->current_brightness == 0) && brightness) {
82                 if (data->iset) {
83                         ret = pm860x_set_bits(data->i2c, data->reg_current,
84                                               CURRENT_BITMASK, data->iset);
85                         if (ret < 0)
86                                 goto out;
87                 }
88                 if (data->pwm) {
89                         ret = pm860x_set_bits(data->i2c, PM8606_PWM,
90                                               PM8606_PWM_FREQ_MASK, data->pwm);
91                         if (ret < 0)
92                                 goto out;
93                 }
94                 if (brightness == MAX_BRIGHTNESS) {
95                         /* set WLED_ON bit as 100% */
96                         ret = pm860x_set_bits(data->i2c, data->reg_always_on,
97                                               PM8606_WLED_ON, PM8606_WLED_ON);
98                 }
99         } else {
100                 if (brightness == MAX_BRIGHTNESS) {
101                         /* set WLED_ON bit as 100% */
102                         ret = pm860x_set_bits(data->i2c, data->reg_always_on,
103                                               PM8606_WLED_ON, PM8606_WLED_ON);
104                 } else {
105                         /* clear WLED_ON bit since it's not 100% */
106                         ret = pm860x_set_bits(data->i2c, data->reg_always_on,
107                                               PM8606_WLED_ON, 0);
108                 }
109         }
110         if (ret < 0)
111                 goto out;
112
113         if (brightness == 0)
114                 backlight_power_set(chip, data->port, 0);
115
116         dev_dbg(chip->dev, "set brightness %d\n", value);
117         data->current_brightness = value;
118         return 0;
119 out:
120         dev_dbg(chip->dev, "set brightness %d failure with return value: %d\n",
121                 value, ret);
122         return ret;
123 }
124
125 static int pm860x_backlight_update_status(struct backlight_device *bl)
126 {
127         int brightness = bl->props.brightness;
128
129         if (bl->props.power != FB_BLANK_UNBLANK)
130                 brightness = 0;
131
132         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
133                 brightness = 0;
134
135         if (bl->props.state & BL_CORE_SUSPENDED)
136                 brightness = 0;
137
138         return pm860x_backlight_set(bl, brightness);
139 }
140
141 static int pm860x_backlight_get_brightness(struct backlight_device *bl)
142 {
143         struct pm860x_backlight_data *data = bl_get_data(bl);
144         struct pm860x_chip *chip = data->chip;
145         int ret;
146
147         ret = pm860x_reg_read(data->i2c, data->reg_duty_cycle);
148         if (ret < 0)
149                 goto out;
150         data->current_brightness = ret;
151         dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
152         return data->current_brightness;
153 out:
154         return -EINVAL;
155 }
156
157 static const struct backlight_ops pm860x_backlight_ops = {
158         .options        = BL_CORE_SUSPENDRESUME,
159         .update_status  = pm860x_backlight_update_status,
160         .get_brightness = pm860x_backlight_get_brightness,
161 };
162
163 #ifdef CONFIG_OF
164 static int pm860x_backlight_dt_init(struct platform_device *pdev,
165                                     struct pm860x_backlight_data *data,
166                                     char *name)
167 {
168         struct device_node *nproot = pdev->dev.parent->of_node, *np;
169         int iset = 0;
170         if (!nproot)
171                 return -ENODEV;
172         nproot = of_find_node_by_name(nproot, "backlights");
173         if (!nproot) {
174                 dev_err(&pdev->dev, "failed to find backlights node\n");
175                 return -ENODEV;
176         }
177         for_each_child_of_node(nproot, np) {
178                 if (!of_node_cmp(np->name, name)) {
179                         of_property_read_u32(np, "marvell,88pm860x-iset",
180                                              &iset);
181                         data->iset = PM8606_WLED_CURRENT(iset);
182                         of_property_read_u32(np, "marvell,88pm860x-pwm",
183                                              &data->pwm);
184                         break;
185                 }
186         }
187         return 0;
188 }
189 #else
190 #define pm860x_backlight_dt_init(x, y, z)       (-1)
191 #endif
192
193 static int pm860x_backlight_probe(struct platform_device *pdev)
194 {
195         struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
196         struct pm860x_backlight_pdata *pdata = pdev->dev.platform_data;
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 = 0;
203
204         data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data),
205                             GFP_KERNEL);
206         if (data == NULL)
207                 return -ENOMEM;
208         res = platform_get_resource_byname(pdev, IORESOURCE_REG, "duty cycle");
209         if (!res) {
210                 dev_err(&pdev->dev, "No REG resource for duty cycle\n");
211                 return -ENXIO;
212         }
213         data->reg_duty_cycle = res->start;
214         res = platform_get_resource_byname(pdev, IORESOURCE_REG, "always on");
215         if (!res) {
216                 dev_err(&pdev->dev, "No REG resorce for always on\n");
217                 return -ENXIO;
218         }
219         data->reg_always_on = res->start;
220         res = platform_get_resource_byname(pdev, IORESOURCE_REG, "current");
221         if (!res) {
222                 dev_err(&pdev->dev, "No REG resource for current\n");
223                 return -ENXIO;
224         }
225         data->reg_current = res->start;
226
227         memset(name, 0, MFD_NAME_SIZE);
228         sprintf(name, "backlight-%d", pdev->id);
229         data->port = pdev->id;
230         data->chip = chip;
231         data->i2c = (chip->id == CHIP_PM8606) ? chip->client    \
232                         : chip->companion;
233         data->current_brightness = MAX_BRIGHTNESS;
234         if (pm860x_backlight_dt_init(pdev, data, name)) {
235                 if (pdata) {
236                         data->pwm = pdata->pwm;
237                         data->iset = pdata->iset;
238                 }
239         }
240
241         memset(&props, 0, sizeof(struct backlight_properties));
242         props.type = BACKLIGHT_RAW;
243         props.max_brightness = MAX_BRIGHTNESS;
244         bl = backlight_device_register(name, &pdev->dev, data,
245                                         &pm860x_backlight_ops, &props);
246         if (IS_ERR(bl)) {
247                 dev_err(&pdev->dev, "failed to register backlight\n");
248                 return PTR_ERR(bl);
249         }
250         bl->props.brightness = MAX_BRIGHTNESS;
251
252         platform_set_drvdata(pdev, bl);
253
254         /* read current backlight */
255         ret = pm860x_backlight_get_brightness(bl);
256         if (ret < 0)
257                 goto out_brt;
258
259         backlight_update_status(bl);
260         return 0;
261 out_brt:
262         backlight_device_unregister(bl);
263         return ret;
264 }
265
266 static int pm860x_backlight_remove(struct platform_device *pdev)
267 {
268         struct backlight_device *bl = platform_get_drvdata(pdev);
269
270         backlight_device_unregister(bl);
271         return 0;
272 }
273
274 static struct platform_driver pm860x_backlight_driver = {
275         .driver         = {
276                 .name   = "88pm860x-backlight",
277                 .owner  = THIS_MODULE,
278         },
279         .probe          = pm860x_backlight_probe,
280         .remove         = pm860x_backlight_remove,
281 };
282
283 module_platform_driver(pm860x_backlight_driver);
284
285 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606");
286 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
287 MODULE_LICENSE("GPL");
288 MODULE_ALIAS("platform:88pm860x-backlight");