]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/video/backlight/lp855x_bl.c
backlight: lp855x_bl: introduce device configuration flow
[~shefty/rdma-dev.git] / drivers / video / backlight / lp855x_bl.c
1 /*
2  * TI LP855x Backlight Driver
3  *
4  *                      Copyright (C) 2011 Texas Instruments
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
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/backlight.h>
16 #include <linux/err.h>
17 #include <linux/platform_data/lp855x.h>
18 #include <linux/pwm.h>
19
20 /* Registers */
21 #define BRIGHTNESS_CTRL         0x00
22 #define DEVICE_CTRL             0x01
23 #define EEPROM_START            0xA0
24 #define EEPROM_END              0xA7
25 #define EPROM_START             0xA0
26 #define EPROM_END               0xAF
27
28 #define BUF_SIZE                20
29 #define DEFAULT_BL_NAME         "lcd-backlight"
30 #define MAX_BRIGHTNESS          255
31
32 struct lp855x;
33
34 /*
35  * struct lp855x_device_config
36  * @pre_init_device: init device function call before updating the brightness
37  * @reg_brightness: register address for brigthenss control
38  * @reg_devicectrl: register address for device control
39  * @post_init_device: late init device function call
40  */
41 struct lp855x_device_config {
42         int (*pre_init_device)(struct lp855x *);
43         u8 reg_brightness;
44         u8 reg_devicectrl;
45         int (*post_init_device)(struct lp855x *);
46 };
47
48 struct lp855x {
49         const char *chipname;
50         enum lp855x_chip_id chip_id;
51         struct lp855x_device_config *cfg;
52         struct i2c_client *client;
53         struct backlight_device *bl;
54         struct device *dev;
55         struct lp855x_platform_data *pdata;
56         struct pwm_device *pwm;
57 };
58
59 static int lp855x_read_byte(struct lp855x *lp, u8 reg, u8 *data)
60 {
61         int ret;
62
63         ret = i2c_smbus_read_byte_data(lp->client, reg);
64         if (ret < 0) {
65                 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
66                 return ret;
67         }
68
69         *data = (u8)ret;
70         return 0;
71 }
72
73 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
74 {
75         return i2c_smbus_write_byte_data(lp->client, reg, data);
76 }
77
78 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
79 {
80         u8 start, end;
81
82         switch (lp->chip_id) {
83         case LP8550:
84         case LP8551:
85         case LP8552:
86         case LP8553:
87                 start = EEPROM_START;
88                 end = EEPROM_END;
89                 break;
90         case LP8556:
91                 start = EPROM_START;
92                 end = EPROM_END;
93                 break;
94         default:
95                 return false;
96         }
97
98         return (addr >= start && addr <= end);
99 }
100
101 static struct lp855x_device_config lp855x_dev_cfg = {
102         .reg_brightness = BRIGHTNESS_CTRL,
103         .reg_devicectrl = DEVICE_CTRL,
104 };
105
106 /*
107  * Device specific configuration flow
108  *
109  *    a) pre_init_device(optional)
110  *    b) update the brightness register
111  *    c) update device control register
112  *    d) update ROM area(optional)
113  *    e) post_init_device(optional)
114  *
115  */
116 static int lp855x_configure(struct lp855x *lp)
117 {
118         u8 val, addr;
119         int i, ret;
120         struct lp855x_platform_data *pd = lp->pdata;
121
122         switch (lp->chip_id) {
123         case LP8550 ... LP8556:
124                 lp->cfg = &lp855x_dev_cfg;
125                 break;
126         default:
127                 return -EINVAL;
128         }
129
130         if (lp->cfg->pre_init_device) {
131                 ret = lp->cfg->pre_init_device(lp);
132                 if (ret) {
133                         dev_err(lp->dev, "pre init device err: %d\n", ret);
134                         goto err;
135                 }
136         }
137
138         val = pd->initial_brightness;
139         ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
140         if (ret)
141                 goto err;
142
143         val = pd->device_control;
144         ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
145         if (ret)
146                 goto err;
147
148         if (pd->load_new_rom_data && pd->size_program) {
149                 for (i = 0; i < pd->size_program; i++) {
150                         addr = pd->rom_data[i].addr;
151                         val = pd->rom_data[i].val;
152                         if (!lp855x_is_valid_rom_area(lp, addr))
153                                 continue;
154
155                         ret = lp855x_write_byte(lp, addr, val);
156                         if (ret)
157                                 goto err;
158                 }
159         }
160
161         if (lp->cfg->post_init_device) {
162                 ret = lp->cfg->post_init_device(lp);
163                 if (ret) {
164                         dev_err(lp->dev, "post init device err: %d\n", ret);
165                         goto err;
166                 }
167         }
168
169         return 0;
170
171 err:
172         return ret;
173 }
174
175 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
176 {
177         unsigned int period = lp->pdata->period_ns;
178         unsigned int duty = br * period / max_br;
179         struct pwm_device *pwm;
180
181         /* request pwm device with the consumer name */
182         if (!lp->pwm) {
183                 pwm = devm_pwm_get(lp->dev, lp->chipname);
184                 if (IS_ERR(pwm))
185                         return;
186
187                 lp->pwm = pwm;
188         }
189
190         pwm_config(lp->pwm, duty, period);
191         if (duty)
192                 pwm_enable(lp->pwm);
193         else
194                 pwm_disable(lp->pwm);
195 }
196
197 static int lp855x_bl_update_status(struct backlight_device *bl)
198 {
199         struct lp855x *lp = bl_get_data(bl);
200         enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
201
202         if (bl->props.state & BL_CORE_SUSPENDED)
203                 bl->props.brightness = 0;
204
205         if (mode == PWM_BASED) {
206                 int br = bl->props.brightness;
207                 int max_br = bl->props.max_brightness;
208
209                 lp855x_pwm_ctrl(lp, br, max_br);
210
211         } else if (mode == REGISTER_BASED) {
212                 u8 val = bl->props.brightness;
213                 lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
214         }
215
216         return 0;
217 }
218
219 static int lp855x_bl_get_brightness(struct backlight_device *bl)
220 {
221         struct lp855x *lp = bl_get_data(bl);
222         enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
223
224         if (mode == REGISTER_BASED) {
225                 u8 val = 0;
226
227                 lp855x_read_byte(lp, BRIGHTNESS_CTRL, &val);
228                 bl->props.brightness = val;
229         }
230
231         return bl->props.brightness;
232 }
233
234 static const struct backlight_ops lp855x_bl_ops = {
235         .options = BL_CORE_SUSPENDRESUME,
236         .update_status = lp855x_bl_update_status,
237         .get_brightness = lp855x_bl_get_brightness,
238 };
239
240 static int lp855x_backlight_register(struct lp855x *lp)
241 {
242         struct backlight_device *bl;
243         struct backlight_properties props;
244         struct lp855x_platform_data *pdata = lp->pdata;
245         char *name = pdata->name ? : DEFAULT_BL_NAME;
246
247         props.type = BACKLIGHT_PLATFORM;
248         props.max_brightness = MAX_BRIGHTNESS;
249
250         if (pdata->initial_brightness > props.max_brightness)
251                 pdata->initial_brightness = props.max_brightness;
252
253         props.brightness = pdata->initial_brightness;
254
255         bl = backlight_device_register(name, lp->dev, lp,
256                                        &lp855x_bl_ops, &props);
257         if (IS_ERR(bl))
258                 return PTR_ERR(bl);
259
260         lp->bl = bl;
261
262         return 0;
263 }
264
265 static void lp855x_backlight_unregister(struct lp855x *lp)
266 {
267         if (lp->bl)
268                 backlight_device_unregister(lp->bl);
269 }
270
271 static ssize_t lp855x_get_chip_id(struct device *dev,
272                                 struct device_attribute *attr, char *buf)
273 {
274         struct lp855x *lp = dev_get_drvdata(dev);
275         return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
276 }
277
278 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
279                                      struct device_attribute *attr, char *buf)
280 {
281         struct lp855x *lp = dev_get_drvdata(dev);
282         enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
283         char *strmode = NULL;
284
285         if (mode == PWM_BASED)
286                 strmode = "pwm based";
287         else if (mode == REGISTER_BASED)
288                 strmode = "register based";
289
290         return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
291 }
292
293 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
294 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
295
296 static struct attribute *lp855x_attributes[] = {
297         &dev_attr_chip_id.attr,
298         &dev_attr_bl_ctl_mode.attr,
299         NULL,
300 };
301
302 static const struct attribute_group lp855x_attr_group = {
303         .attrs = lp855x_attributes,
304 };
305
306 static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
307 {
308         struct lp855x *lp;
309         struct lp855x_platform_data *pdata = cl->dev.platform_data;
310         enum lp855x_brightness_ctrl_mode mode;
311         int ret;
312
313         if (!pdata) {
314                 dev_err(&cl->dev, "no platform data supplied\n");
315                 return -EINVAL;
316         }
317
318         if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
319                 return -EIO;
320
321         lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
322         if (!lp)
323                 return -ENOMEM;
324
325         mode = pdata->mode;
326         lp->client = cl;
327         lp->dev = &cl->dev;
328         lp->pdata = pdata;
329         lp->chipname = id->name;
330         lp->chip_id = id->driver_data;
331         i2c_set_clientdata(cl, lp);
332
333         ret = lp855x_configure(lp);
334         if (ret) {
335                 dev_err(lp->dev, "device config err: %d", ret);
336                 goto err_dev;
337         }
338
339         ret = lp855x_backlight_register(lp);
340         if (ret) {
341                 dev_err(lp->dev,
342                         "failed to register backlight. err: %d\n", ret);
343                 goto err_dev;
344         }
345
346         ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
347         if (ret) {
348                 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
349                 goto err_sysfs;
350         }
351
352         backlight_update_status(lp->bl);
353         return 0;
354
355 err_sysfs:
356         lp855x_backlight_unregister(lp);
357 err_dev:
358         return ret;
359 }
360
361 static int lp855x_remove(struct i2c_client *cl)
362 {
363         struct lp855x *lp = i2c_get_clientdata(cl);
364
365         lp->bl->props.brightness = 0;
366         backlight_update_status(lp->bl);
367         sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
368         lp855x_backlight_unregister(lp);
369
370         return 0;
371 }
372
373 static const struct i2c_device_id lp855x_ids[] = {
374         {"lp8550", LP8550},
375         {"lp8551", LP8551},
376         {"lp8552", LP8552},
377         {"lp8553", LP8553},
378         {"lp8556", LP8556},
379         { }
380 };
381 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
382
383 static struct i2c_driver lp855x_driver = {
384         .driver = {
385                    .name = "lp855x",
386                    },
387         .probe = lp855x_probe,
388         .remove = lp855x_remove,
389         .id_table = lp855x_ids,
390 };
391
392 module_i2c_driver(lp855x_driver);
393
394 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
395 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
396 MODULE_LICENSE("GPL");