blob: c0b41f13bd4aea01e0156ff11c52e3b585be628d [file] [log] [blame]
Kim, Milo7be865a2012-03-23 15:02:01 -07001/*
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>
Kim, Milo4add0662013-04-29 16:18:06 -070017#include <linux/of.h>
Kim, Milof7f95052012-07-30 14:40:53 -070018#include <linux/platform_data/lp855x.h>
Kim, Milo8cc97642012-12-17 16:00:43 -080019#include <linux/pwm.h>
Kim, Milo7be865a2012-03-23 15:02:01 -070020
Kim, Milo26e8ccc2013-02-21 16:44:06 -080021/* LP8550/1/2/3/6 Registers */
22#define LP855X_BRIGHTNESS_CTRL 0x00
23#define LP855X_DEVICE_CTRL 0x01
24#define LP855X_EEPROM_START 0xA0
25#define LP855X_EEPROM_END 0xA7
26#define LP8556_EPROM_START 0xA0
27#define LP8556_EPROM_END 0xAF
28
29/* LP8557 Registers */
30#define LP8557_BL_CMD 0x00
31#define LP8557_BL_MASK 0x01
32#define LP8557_BL_ON 0x01
33#define LP8557_BL_OFF 0x00
34#define LP8557_BRIGHTNESS_CTRL 0x04
35#define LP8557_CONFIG 0x10
36#define LP8557_EPROM_START 0x10
37#define LP8557_EPROM_END 0x1E
Kim, Milo7be865a2012-03-23 15:02:01 -070038
Kim, Milo7be865a2012-03-23 15:02:01 -070039#define DEFAULT_BL_NAME "lcd-backlight"
40#define MAX_BRIGHTNESS 255
41
Kim, Milo0b818572013-04-29 16:18:03 -070042enum lp855x_brightness_ctrl_mode {
43 PWM_BASED = 1,
44 REGISTER_BASED,
45};
46
Kim, Milo68853bc2013-02-21 16:44:05 -080047struct lp855x;
48
49/*
50 * struct lp855x_device_config
51 * @pre_init_device: init device function call before updating the brightness
52 * @reg_brightness: register address for brigthenss control
53 * @reg_devicectrl: register address for device control
54 * @post_init_device: late init device function call
55 */
56struct lp855x_device_config {
57 int (*pre_init_device)(struct lp855x *);
58 u8 reg_brightness;
59 u8 reg_devicectrl;
60 int (*post_init_device)(struct lp855x *);
61};
62
Kim, Milo7be865a2012-03-23 15:02:01 -070063struct lp855x {
64 const char *chipname;
65 enum lp855x_chip_id chip_id;
Kim, Milo0b818572013-04-29 16:18:03 -070066 enum lp855x_brightness_ctrl_mode mode;
Kim, Milo68853bc2013-02-21 16:44:05 -080067 struct lp855x_device_config *cfg;
Kim, Milo7be865a2012-03-23 15:02:01 -070068 struct i2c_client *client;
69 struct backlight_device *bl;
70 struct device *dev;
Kim, Milo7be865a2012-03-23 15:02:01 -070071 struct lp855x_platform_data *pdata;
Kim, Milo8cc97642012-12-17 16:00:43 -080072 struct pwm_device *pwm;
Kim, Milo7be865a2012-03-23 15:02:01 -070073};
74
Kim, Milo7be865a2012-03-23 15:02:01 -070075static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
76{
Kim, Miloeaa4d022012-12-17 16:00:45 -080077 return i2c_smbus_write_byte_data(lp->client, reg, data);
Kim, Milo7be865a2012-03-23 15:02:01 -070078}
79
Kim, Milo26e8ccc2013-02-21 16:44:06 -080080static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
81{
82 int ret;
83 u8 tmp;
84
85 ret = i2c_smbus_read_byte_data(lp->client, reg);
86 if (ret < 0) {
87 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
88 return ret;
89 }
90
91 tmp = (u8)ret;
92 tmp &= ~mask;
93 tmp |= data & mask;
94
95 return lp855x_write_byte(lp, reg, tmp);
96}
97
Kim, Milo7be865a2012-03-23 15:02:01 -070098static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
99{
100 u8 start, end;
101
102 switch (lp->chip_id) {
103 case LP8550:
104 case LP8551:
105 case LP8552:
106 case LP8553:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800107 start = LP855X_EEPROM_START;
108 end = LP855X_EEPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700109 break;
110 case LP8556:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800111 start = LP8556_EPROM_START;
112 end = LP8556_EPROM_END;
113 break;
114 case LP8557:
115 start = LP8557_EPROM_START;
116 end = LP8557_EPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700117 break;
118 default:
119 return false;
120 }
121
122 return (addr >= start && addr <= end);
123}
124
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800125static int lp8557_bl_off(struct lp855x *lp)
126{
127 /* BL_ON = 0 before updating EPROM settings */
128 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
129 LP8557_BL_OFF);
130}
131
132static int lp8557_bl_on(struct lp855x *lp)
133{
134 /* BL_ON = 1 after updating EPROM settings */
135 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
136 LP8557_BL_ON);
137}
138
Kim, Milo68853bc2013-02-21 16:44:05 -0800139static struct lp855x_device_config lp855x_dev_cfg = {
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800140 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
141 .reg_devicectrl = LP855X_DEVICE_CTRL,
142};
143
144static struct lp855x_device_config lp8557_dev_cfg = {
145 .reg_brightness = LP8557_BRIGHTNESS_CTRL,
146 .reg_devicectrl = LP8557_CONFIG,
147 .pre_init_device = lp8557_bl_off,
148 .post_init_device = lp8557_bl_on,
Kim, Milo68853bc2013-02-21 16:44:05 -0800149};
150
151/*
152 * Device specific configuration flow
153 *
154 * a) pre_init_device(optional)
155 * b) update the brightness register
156 * c) update device control register
157 * d) update ROM area(optional)
158 * e) post_init_device(optional)
159 *
160 */
161static int lp855x_configure(struct lp855x *lp)
Kim, Milo7be865a2012-03-23 15:02:01 -0700162{
163 u8 val, addr;
164 int i, ret;
165 struct lp855x_platform_data *pd = lp->pdata;
166
Kim, Milo68853bc2013-02-21 16:44:05 -0800167 switch (lp->chip_id) {
168 case LP8550 ... LP8556:
169 lp->cfg = &lp855x_dev_cfg;
170 break;
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800171 case LP8557:
172 lp->cfg = &lp8557_dev_cfg;
173 break;
Kim, Milo68853bc2013-02-21 16:44:05 -0800174 default:
175 return -EINVAL;
176 }
177
178 if (lp->cfg->pre_init_device) {
179 ret = lp->cfg->pre_init_device(lp);
180 if (ret) {
181 dev_err(lp->dev, "pre init device err: %d\n", ret);
182 goto err;
183 }
184 }
185
Kim, Milo7be865a2012-03-23 15:02:01 -0700186 val = pd->initial_brightness;
Kim, Milo68853bc2013-02-21 16:44:05 -0800187 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700188 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800189 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700190
191 val = pd->device_control;
Kim, Milo68853bc2013-02-21 16:44:05 -0800192 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700193 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800194 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700195
Kim, Miloc365e592013-04-29 16:18:05 -0700196 if (pd->size_program > 0) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700197 for (i = 0; i < pd->size_program; i++) {
198 addr = pd->rom_data[i].addr;
199 val = pd->rom_data[i].val;
200 if (!lp855x_is_valid_rom_area(lp, addr))
201 continue;
202
203 ret = lp855x_write_byte(lp, addr, val);
204 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800205 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700206 }
207 }
208
Kim, Milo68853bc2013-02-21 16:44:05 -0800209 if (lp->cfg->post_init_device) {
210 ret = lp->cfg->post_init_device(lp);
211 if (ret) {
212 dev_err(lp->dev, "post init device err: %d\n", ret);
213 goto err;
214 }
215 }
216
217 return 0;
218
219err:
Kim, Milo7be865a2012-03-23 15:02:01 -0700220 return ret;
221}
222
Kim, Milo8cc97642012-12-17 16:00:43 -0800223static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
224{
225 unsigned int period = lp->pdata->period_ns;
226 unsigned int duty = br * period / max_br;
227 struct pwm_device *pwm;
228
229 /* request pwm device with the consumer name */
230 if (!lp->pwm) {
231 pwm = devm_pwm_get(lp->dev, lp->chipname);
232 if (IS_ERR(pwm))
233 return;
234
235 lp->pwm = pwm;
236 }
237
238 pwm_config(lp->pwm, duty, period);
239 if (duty)
240 pwm_enable(lp->pwm);
241 else
242 pwm_disable(lp->pwm);
243}
244
Kim, Milo7be865a2012-03-23 15:02:01 -0700245static int lp855x_bl_update_status(struct backlight_device *bl)
246{
247 struct lp855x *lp = bl_get_data(bl);
Kim, Milo7be865a2012-03-23 15:02:01 -0700248
Shingo Nakao9f0a5112013-07-02 14:15:54 +0200249 if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
Kim, Milo7be865a2012-03-23 15:02:01 -0700250 bl->props.brightness = 0;
251
Kim, Milo0b818572013-04-29 16:18:03 -0700252 if (lp->mode == PWM_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700253 int br = bl->props.brightness;
254 int max_br = bl->props.max_brightness;
255
Kim, Milo8cc97642012-12-17 16:00:43 -0800256 lp855x_pwm_ctrl(lp, br, max_br);
Kim, Milo7be865a2012-03-23 15:02:01 -0700257
Kim, Milo0b818572013-04-29 16:18:03 -0700258 } else if (lp->mode == REGISTER_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700259 u8 val = bl->props.brightness;
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800260 lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700261 }
262
263 return 0;
264}
265
266static int lp855x_bl_get_brightness(struct backlight_device *bl)
267{
Kim, Milo7be865a2012-03-23 15:02:01 -0700268 return bl->props.brightness;
269}
270
271static const struct backlight_ops lp855x_bl_ops = {
272 .options = BL_CORE_SUSPENDRESUME,
273 .update_status = lp855x_bl_update_status,
274 .get_brightness = lp855x_bl_get_brightness,
275};
276
277static int lp855x_backlight_register(struct lp855x *lp)
278{
279 struct backlight_device *bl;
280 struct backlight_properties props;
281 struct lp855x_platform_data *pdata = lp->pdata;
Kim, Milo600ffd32013-04-29 16:18:02 -0700282 const char *name = pdata->name ? : DEFAULT_BL_NAME;
Kim, Milo7be865a2012-03-23 15:02:01 -0700283
284 props.type = BACKLIGHT_PLATFORM;
285 props.max_brightness = MAX_BRIGHTNESS;
286
287 if (pdata->initial_brightness > props.max_brightness)
288 pdata->initial_brightness = props.max_brightness;
289
290 props.brightness = pdata->initial_brightness;
291
292 bl = backlight_device_register(name, lp->dev, lp,
293 &lp855x_bl_ops, &props);
294 if (IS_ERR(bl))
295 return PTR_ERR(bl);
296
297 lp->bl = bl;
298
299 return 0;
300}
301
302static void lp855x_backlight_unregister(struct lp855x *lp)
303{
304 if (lp->bl)
305 backlight_device_unregister(lp->bl);
306}
307
308static ssize_t lp855x_get_chip_id(struct device *dev,
309 struct device_attribute *attr, char *buf)
310{
311 struct lp855x *lp = dev_get_drvdata(dev);
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700312 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
Kim, Milo7be865a2012-03-23 15:02:01 -0700313}
314
315static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct lp855x *lp = dev_get_drvdata(dev);
Kim, Milo7be865a2012-03-23 15:02:01 -0700319 char *strmode = NULL;
320
Kim, Milo0b818572013-04-29 16:18:03 -0700321 if (lp->mode == PWM_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700322 strmode = "pwm based";
Kim, Milo0b818572013-04-29 16:18:03 -0700323 else if (lp->mode == REGISTER_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700324 strmode = "register based";
325
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700326 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
Kim, Milo7be865a2012-03-23 15:02:01 -0700327}
328
329static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
330static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
331
332static struct attribute *lp855x_attributes[] = {
333 &dev_attr_chip_id.attr,
334 &dev_attr_bl_ctl_mode.attr,
335 NULL,
336};
337
338static const struct attribute_group lp855x_attr_group = {
339 .attrs = lp855x_attributes,
340};
341
Kim, Milo4add0662013-04-29 16:18:06 -0700342#ifdef CONFIG_OF
343static int lp855x_parse_dt(struct device *dev, struct device_node *node)
344{
345 struct lp855x_platform_data *pdata;
346 int rom_length;
347
348 if (!node) {
349 dev_err(dev, "no platform data\n");
350 return -EINVAL;
351 }
352
353 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
354 if (!pdata)
355 return -ENOMEM;
356
357 of_property_read_string(node, "bl-name", &pdata->name);
358 of_property_read_u8(node, "dev-ctrl", &pdata->device_control);
359 of_property_read_u8(node, "init-brt", &pdata->initial_brightness);
360 of_property_read_u32(node, "pwm-period", &pdata->period_ns);
361
362 /* Fill ROM platform data if defined */
363 rom_length = of_get_child_count(node);
364 if (rom_length > 0) {
365 struct lp855x_rom_data *rom;
366 struct device_node *child;
367 int i = 0;
368
369 rom = devm_kzalloc(dev, sizeof(*rom) * rom_length, GFP_KERNEL);
370 if (!rom)
371 return -ENOMEM;
372
373 for_each_child_of_node(node, child) {
374 of_property_read_u8(child, "rom-addr", &rom[i].addr);
375 of_property_read_u8(child, "rom-val", &rom[i].val);
376 i++;
377 }
378
379 pdata->size_program = rom_length;
380 pdata->rom_data = &rom[0];
381 }
382
383 dev->platform_data = pdata;
384
385 return 0;
386}
387#else
388static int lp855x_parse_dt(struct device *dev, struct device_node *node)
389{
390 return -EINVAL;
391}
392#endif
393
Kim, Milo7be865a2012-03-23 15:02:01 -0700394static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
395{
396 struct lp855x *lp;
397 struct lp855x_platform_data *pdata = cl->dev.platform_data;
Kim, Milo4add0662013-04-29 16:18:06 -0700398 struct device_node *node = cl->dev.of_node;
Kim, Milo7be865a2012-03-23 15:02:01 -0700399 int ret;
400
401 if (!pdata) {
Kim, Milo4add0662013-04-29 16:18:06 -0700402 ret = lp855x_parse_dt(&cl->dev, node);
403 if (ret < 0)
404 return ret;
405
406 pdata = cl->dev.platform_data;
Kim, Milo7be865a2012-03-23 15:02:01 -0700407 }
408
409 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
410 return -EIO;
411
412 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
413 if (!lp)
414 return -ENOMEM;
415
Kim, Milo0b818572013-04-29 16:18:03 -0700416 if (pdata->period_ns > 0)
417 lp->mode = PWM_BASED;
418 else
419 lp->mode = REGISTER_BASED;
420
Kim, Milo7be865a2012-03-23 15:02:01 -0700421 lp->client = cl;
422 lp->dev = &cl->dev;
423 lp->pdata = pdata;
424 lp->chipname = id->name;
425 lp->chip_id = id->driver_data;
426 i2c_set_clientdata(cl, lp);
427
Kim, Milo68853bc2013-02-21 16:44:05 -0800428 ret = lp855x_configure(lp);
Kim, Milo7be865a2012-03-23 15:02:01 -0700429 if (ret) {
Kim, Milo68853bc2013-02-21 16:44:05 -0800430 dev_err(lp->dev, "device config err: %d", ret);
431 goto err_dev;
Kim, Milo7be865a2012-03-23 15:02:01 -0700432 }
433
434 ret = lp855x_backlight_register(lp);
435 if (ret) {
436 dev_err(lp->dev,
437 "failed to register backlight. err: %d\n", ret);
438 goto err_dev;
439 }
440
441 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
442 if (ret) {
443 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
444 goto err_sysfs;
445 }
446
447 backlight_update_status(lp->bl);
448 return 0;
449
450err_sysfs:
451 lp855x_backlight_unregister(lp);
452err_dev:
453 return ret;
454}
455
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500456static int lp855x_remove(struct i2c_client *cl)
Kim, Milo7be865a2012-03-23 15:02:01 -0700457{
458 struct lp855x *lp = i2c_get_clientdata(cl);
459
460 lp->bl->props.brightness = 0;
461 backlight_update_status(lp->bl);
462 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
463 lp855x_backlight_unregister(lp);
464
465 return 0;
466}
467
Kim, Milo4add0662013-04-29 16:18:06 -0700468static const struct of_device_id lp855x_dt_ids[] = {
469 { .compatible = "ti,lp8550", },
470 { .compatible = "ti,lp8551", },
471 { .compatible = "ti,lp8552", },
472 { .compatible = "ti,lp8553", },
473 { .compatible = "ti,lp8556", },
474 { .compatible = "ti,lp8557", },
475 { }
476};
477MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
478
Kim, Milo7be865a2012-03-23 15:02:01 -0700479static const struct i2c_device_id lp855x_ids[] = {
480 {"lp8550", LP8550},
481 {"lp8551", LP8551},
482 {"lp8552", LP8552},
483 {"lp8553", LP8553},
484 {"lp8556", LP8556},
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800485 {"lp8557", LP8557},
Kim, Milo7be865a2012-03-23 15:02:01 -0700486 { }
487};
488MODULE_DEVICE_TABLE(i2c, lp855x_ids);
489
490static struct i2c_driver lp855x_driver = {
491 .driver = {
492 .name = "lp855x",
Kim, Milo4add0662013-04-29 16:18:06 -0700493 .of_match_table = of_match_ptr(lp855x_dt_ids),
Kim, Milo7be865a2012-03-23 15:02:01 -0700494 },
495 .probe = lp855x_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500496 .remove = lp855x_remove,
Kim, Milo7be865a2012-03-23 15:02:01 -0700497 .id_table = lp855x_ids,
498};
499
500module_i2c_driver(lp855x_driver);
501
502MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
503MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
504MODULE_LICENSE("GPL");