blob: c952175d41136770ca484c98f61618a1e346b379 [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
Milo Kim5812c132013-11-12 15:08:57 -080029/* LP8555/7 Registers */
Kim, Milo26e8ccc2013-02-21 16:44:06 -080030#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
Milo Kim5812c132013-11-12 15:08:57 -080036#define LP8555_EPROM_START 0x10
37#define LP8555_EPROM_END 0x7A
Kim, Milo26e8ccc2013-02-21 16:44:06 -080038#define LP8557_EPROM_START 0x10
39#define LP8557_EPROM_END 0x1E
Kim, Milo7be865a2012-03-23 15:02:01 -070040
Kim, Milo7be865a2012-03-23 15:02:01 -070041#define DEFAULT_BL_NAME "lcd-backlight"
42#define MAX_BRIGHTNESS 255
43
Kim, Milo0b818572013-04-29 16:18:03 -070044enum lp855x_brightness_ctrl_mode {
45 PWM_BASED = 1,
46 REGISTER_BASED,
47};
48
Kim, Milo68853bc2013-02-21 16:44:05 -080049struct lp855x;
50
51/*
52 * struct lp855x_device_config
53 * @pre_init_device: init device function call before updating the brightness
54 * @reg_brightness: register address for brigthenss control
55 * @reg_devicectrl: register address for device control
56 * @post_init_device: late init device function call
57 */
58struct lp855x_device_config {
59 int (*pre_init_device)(struct lp855x *);
60 u8 reg_brightness;
61 u8 reg_devicectrl;
62 int (*post_init_device)(struct lp855x *);
63};
64
Kim, Milo7be865a2012-03-23 15:02:01 -070065struct lp855x {
66 const char *chipname;
67 enum lp855x_chip_id chip_id;
Kim, Milo0b818572013-04-29 16:18:03 -070068 enum lp855x_brightness_ctrl_mode mode;
Kim, Milo68853bc2013-02-21 16:44:05 -080069 struct lp855x_device_config *cfg;
Kim, Milo7be865a2012-03-23 15:02:01 -070070 struct i2c_client *client;
71 struct backlight_device *bl;
72 struct device *dev;
Kim, Milo7be865a2012-03-23 15:02:01 -070073 struct lp855x_platform_data *pdata;
Kim, Milo8cc97642012-12-17 16:00:43 -080074 struct pwm_device *pwm;
Kim, Milo7be865a2012-03-23 15:02:01 -070075};
76
Kim, Milo7be865a2012-03-23 15:02:01 -070077static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
78{
Kim, Miloeaa4d022012-12-17 16:00:45 -080079 return i2c_smbus_write_byte_data(lp->client, reg, data);
Kim, Milo7be865a2012-03-23 15:02:01 -070080}
81
Kim, Milo26e8ccc2013-02-21 16:44:06 -080082static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
83{
84 int ret;
85 u8 tmp;
86
87 ret = i2c_smbus_read_byte_data(lp->client, reg);
88 if (ret < 0) {
89 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
90 return ret;
91 }
92
93 tmp = (u8)ret;
94 tmp &= ~mask;
95 tmp |= data & mask;
96
97 return lp855x_write_byte(lp, reg, tmp);
98}
99
Kim, Milo7be865a2012-03-23 15:02:01 -0700100static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
101{
102 u8 start, end;
103
104 switch (lp->chip_id) {
105 case LP8550:
106 case LP8551:
107 case LP8552:
108 case LP8553:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800109 start = LP855X_EEPROM_START;
110 end = LP855X_EEPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700111 break;
112 case LP8556:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800113 start = LP8556_EPROM_START;
114 end = LP8556_EPROM_END;
115 break;
Milo Kim5812c132013-11-12 15:08:57 -0800116 case LP8555:
117 start = LP8555_EPROM_START;
118 end = LP8555_EPROM_END;
119 break;
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800120 case LP8557:
121 start = LP8557_EPROM_START;
122 end = LP8557_EPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700123 break;
124 default:
125 return false;
126 }
127
128 return (addr >= start && addr <= end);
129}
130
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800131static int lp8557_bl_off(struct lp855x *lp)
132{
133 /* BL_ON = 0 before updating EPROM settings */
134 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
135 LP8557_BL_OFF);
136}
137
138static int lp8557_bl_on(struct lp855x *lp)
139{
140 /* BL_ON = 1 after updating EPROM settings */
141 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
142 LP8557_BL_ON);
143}
144
Kim, Milo68853bc2013-02-21 16:44:05 -0800145static struct lp855x_device_config lp855x_dev_cfg = {
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800146 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
147 .reg_devicectrl = LP855X_DEVICE_CTRL,
148};
149
150static struct lp855x_device_config lp8557_dev_cfg = {
151 .reg_brightness = LP8557_BRIGHTNESS_CTRL,
152 .reg_devicectrl = LP8557_CONFIG,
153 .pre_init_device = lp8557_bl_off,
154 .post_init_device = lp8557_bl_on,
Kim, Milo68853bc2013-02-21 16:44:05 -0800155};
156
157/*
158 * Device specific configuration flow
159 *
160 * a) pre_init_device(optional)
161 * b) update the brightness register
162 * c) update device control register
163 * d) update ROM area(optional)
164 * e) post_init_device(optional)
165 *
166 */
167static int lp855x_configure(struct lp855x *lp)
Kim, Milo7be865a2012-03-23 15:02:01 -0700168{
169 u8 val, addr;
170 int i, ret;
171 struct lp855x_platform_data *pd = lp->pdata;
172
Kim, Milo68853bc2013-02-21 16:44:05 -0800173 switch (lp->chip_id) {
Milo Kim5812c132013-11-12 15:08:57 -0800174 case LP8550:
175 case LP8551:
176 case LP8552:
177 case LP8553:
178 case LP8556:
Kim, Milo68853bc2013-02-21 16:44:05 -0800179 lp->cfg = &lp855x_dev_cfg;
180 break;
Milo Kim5812c132013-11-12 15:08:57 -0800181 case LP8555:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800182 case LP8557:
183 lp->cfg = &lp8557_dev_cfg;
184 break;
Kim, Milo68853bc2013-02-21 16:44:05 -0800185 default:
186 return -EINVAL;
187 }
188
189 if (lp->cfg->pre_init_device) {
190 ret = lp->cfg->pre_init_device(lp);
191 if (ret) {
192 dev_err(lp->dev, "pre init device err: %d\n", ret);
193 goto err;
194 }
195 }
196
Kim, Milo7be865a2012-03-23 15:02:01 -0700197 val = pd->initial_brightness;
Kim, Milo68853bc2013-02-21 16:44:05 -0800198 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700199 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800200 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700201
202 val = pd->device_control;
Kim, Milo68853bc2013-02-21 16:44:05 -0800203 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700204 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800205 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700206
Kim, Miloc365e592013-04-29 16:18:05 -0700207 if (pd->size_program > 0) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700208 for (i = 0; i < pd->size_program; i++) {
209 addr = pd->rom_data[i].addr;
210 val = pd->rom_data[i].val;
211 if (!lp855x_is_valid_rom_area(lp, addr))
212 continue;
213
214 ret = lp855x_write_byte(lp, addr, val);
215 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800216 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700217 }
218 }
219
Kim, Milo68853bc2013-02-21 16:44:05 -0800220 if (lp->cfg->post_init_device) {
221 ret = lp->cfg->post_init_device(lp);
222 if (ret) {
223 dev_err(lp->dev, "post init device err: %d\n", ret);
224 goto err;
225 }
226 }
227
228 return 0;
229
230err:
Kim, Milo7be865a2012-03-23 15:02:01 -0700231 return ret;
232}
233
Kim, Milo8cc97642012-12-17 16:00:43 -0800234static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
235{
236 unsigned int period = lp->pdata->period_ns;
237 unsigned int duty = br * period / max_br;
238 struct pwm_device *pwm;
239
240 /* request pwm device with the consumer name */
241 if (!lp->pwm) {
242 pwm = devm_pwm_get(lp->dev, lp->chipname);
243 if (IS_ERR(pwm))
244 return;
245
246 lp->pwm = pwm;
247 }
248
249 pwm_config(lp->pwm, duty, period);
250 if (duty)
251 pwm_enable(lp->pwm);
252 else
253 pwm_disable(lp->pwm);
254}
255
Kim, Milo7be865a2012-03-23 15:02:01 -0700256static int lp855x_bl_update_status(struct backlight_device *bl)
257{
258 struct lp855x *lp = bl_get_data(bl);
Kim, Milo7be865a2012-03-23 15:02:01 -0700259
Shingo Nakao9f0a5112013-07-02 14:15:54 +0200260 if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
Kim, Milo7be865a2012-03-23 15:02:01 -0700261 bl->props.brightness = 0;
262
Kim, Milo0b818572013-04-29 16:18:03 -0700263 if (lp->mode == PWM_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700264 int br = bl->props.brightness;
265 int max_br = bl->props.max_brightness;
266
Kim, Milo8cc97642012-12-17 16:00:43 -0800267 lp855x_pwm_ctrl(lp, br, max_br);
Kim, Milo7be865a2012-03-23 15:02:01 -0700268
Kim, Milo0b818572013-04-29 16:18:03 -0700269 } else if (lp->mode == REGISTER_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700270 u8 val = bl->props.brightness;
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800271 lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700272 }
273
274 return 0;
275}
276
277static int lp855x_bl_get_brightness(struct backlight_device *bl)
278{
Kim, Milo7be865a2012-03-23 15:02:01 -0700279 return bl->props.brightness;
280}
281
282static const struct backlight_ops lp855x_bl_ops = {
283 .options = BL_CORE_SUSPENDRESUME,
284 .update_status = lp855x_bl_update_status,
285 .get_brightness = lp855x_bl_get_brightness,
286};
287
288static int lp855x_backlight_register(struct lp855x *lp)
289{
290 struct backlight_device *bl;
291 struct backlight_properties props;
292 struct lp855x_platform_data *pdata = lp->pdata;
Kim, Milo600ffd32013-04-29 16:18:02 -0700293 const char *name = pdata->name ? : DEFAULT_BL_NAME;
Kim, Milo7be865a2012-03-23 15:02:01 -0700294
295 props.type = BACKLIGHT_PLATFORM;
296 props.max_brightness = MAX_BRIGHTNESS;
297
298 if (pdata->initial_brightness > props.max_brightness)
299 pdata->initial_brightness = props.max_brightness;
300
301 props.brightness = pdata->initial_brightness;
302
303 bl = backlight_device_register(name, lp->dev, lp,
304 &lp855x_bl_ops, &props);
305 if (IS_ERR(bl))
306 return PTR_ERR(bl);
307
308 lp->bl = bl;
309
310 return 0;
311}
312
313static void lp855x_backlight_unregister(struct lp855x *lp)
314{
315 if (lp->bl)
316 backlight_device_unregister(lp->bl);
317}
318
319static ssize_t lp855x_get_chip_id(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
322 struct lp855x *lp = dev_get_drvdata(dev);
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700323 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
Kim, Milo7be865a2012-03-23 15:02:01 -0700324}
325
326static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct lp855x *lp = dev_get_drvdata(dev);
Kim, Milo7be865a2012-03-23 15:02:01 -0700330 char *strmode = NULL;
331
Kim, Milo0b818572013-04-29 16:18:03 -0700332 if (lp->mode == PWM_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700333 strmode = "pwm based";
Kim, Milo0b818572013-04-29 16:18:03 -0700334 else if (lp->mode == REGISTER_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700335 strmode = "register based";
336
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700337 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
Kim, Milo7be865a2012-03-23 15:02:01 -0700338}
339
340static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
341static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
342
343static struct attribute *lp855x_attributes[] = {
344 &dev_attr_chip_id.attr,
345 &dev_attr_bl_ctl_mode.attr,
346 NULL,
347};
348
349static const struct attribute_group lp855x_attr_group = {
350 .attrs = lp855x_attributes,
351};
352
Kim, Milo4add0662013-04-29 16:18:06 -0700353#ifdef CONFIG_OF
354static int lp855x_parse_dt(struct device *dev, struct device_node *node)
355{
356 struct lp855x_platform_data *pdata;
357 int rom_length;
358
359 if (!node) {
360 dev_err(dev, "no platform data\n");
361 return -EINVAL;
362 }
363
364 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
365 if (!pdata)
366 return -ENOMEM;
367
368 of_property_read_string(node, "bl-name", &pdata->name);
369 of_property_read_u8(node, "dev-ctrl", &pdata->device_control);
370 of_property_read_u8(node, "init-brt", &pdata->initial_brightness);
371 of_property_read_u32(node, "pwm-period", &pdata->period_ns);
372
373 /* Fill ROM platform data if defined */
374 rom_length = of_get_child_count(node);
375 if (rom_length > 0) {
376 struct lp855x_rom_data *rom;
377 struct device_node *child;
378 int i = 0;
379
380 rom = devm_kzalloc(dev, sizeof(*rom) * rom_length, GFP_KERNEL);
381 if (!rom)
382 return -ENOMEM;
383
384 for_each_child_of_node(node, child) {
385 of_property_read_u8(child, "rom-addr", &rom[i].addr);
386 of_property_read_u8(child, "rom-val", &rom[i].val);
387 i++;
388 }
389
390 pdata->size_program = rom_length;
391 pdata->rom_data = &rom[0];
392 }
393
394 dev->platform_data = pdata;
395
396 return 0;
397}
398#else
399static int lp855x_parse_dt(struct device *dev, struct device_node *node)
400{
401 return -EINVAL;
402}
403#endif
404
Kim, Milo7be865a2012-03-23 15:02:01 -0700405static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
406{
407 struct lp855x *lp;
408 struct lp855x_platform_data *pdata = cl->dev.platform_data;
Kim, Milo4add0662013-04-29 16:18:06 -0700409 struct device_node *node = cl->dev.of_node;
Kim, Milo7be865a2012-03-23 15:02:01 -0700410 int ret;
411
412 if (!pdata) {
Kim, Milo4add0662013-04-29 16:18:06 -0700413 ret = lp855x_parse_dt(&cl->dev, node);
414 if (ret < 0)
415 return ret;
416
417 pdata = cl->dev.platform_data;
Kim, Milo7be865a2012-03-23 15:02:01 -0700418 }
419
420 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
421 return -EIO;
422
423 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
424 if (!lp)
425 return -ENOMEM;
426
Kim, Milo0b818572013-04-29 16:18:03 -0700427 if (pdata->period_ns > 0)
428 lp->mode = PWM_BASED;
429 else
430 lp->mode = REGISTER_BASED;
431
Kim, Milo7be865a2012-03-23 15:02:01 -0700432 lp->client = cl;
433 lp->dev = &cl->dev;
434 lp->pdata = pdata;
435 lp->chipname = id->name;
436 lp->chip_id = id->driver_data;
437 i2c_set_clientdata(cl, lp);
438
Kim, Milo68853bc2013-02-21 16:44:05 -0800439 ret = lp855x_configure(lp);
Kim, Milo7be865a2012-03-23 15:02:01 -0700440 if (ret) {
Kim, Milo68853bc2013-02-21 16:44:05 -0800441 dev_err(lp->dev, "device config err: %d", ret);
442 goto err_dev;
Kim, Milo7be865a2012-03-23 15:02:01 -0700443 }
444
445 ret = lp855x_backlight_register(lp);
446 if (ret) {
447 dev_err(lp->dev,
448 "failed to register backlight. err: %d\n", ret);
449 goto err_dev;
450 }
451
452 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
453 if (ret) {
454 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
455 goto err_sysfs;
456 }
457
458 backlight_update_status(lp->bl);
459 return 0;
460
461err_sysfs:
462 lp855x_backlight_unregister(lp);
463err_dev:
464 return ret;
465}
466
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500467static int lp855x_remove(struct i2c_client *cl)
Kim, Milo7be865a2012-03-23 15:02:01 -0700468{
469 struct lp855x *lp = i2c_get_clientdata(cl);
470
471 lp->bl->props.brightness = 0;
472 backlight_update_status(lp->bl);
473 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
474 lp855x_backlight_unregister(lp);
475
476 return 0;
477}
478
Kim, Milo4add0662013-04-29 16:18:06 -0700479static const struct of_device_id lp855x_dt_ids[] = {
480 { .compatible = "ti,lp8550", },
481 { .compatible = "ti,lp8551", },
482 { .compatible = "ti,lp8552", },
483 { .compatible = "ti,lp8553", },
Milo Kim5812c132013-11-12 15:08:57 -0800484 { .compatible = "ti,lp8555", },
Kim, Milo4add0662013-04-29 16:18:06 -0700485 { .compatible = "ti,lp8556", },
486 { .compatible = "ti,lp8557", },
487 { }
488};
489MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
490
Kim, Milo7be865a2012-03-23 15:02:01 -0700491static const struct i2c_device_id lp855x_ids[] = {
492 {"lp8550", LP8550},
493 {"lp8551", LP8551},
494 {"lp8552", LP8552},
495 {"lp8553", LP8553},
Milo Kim5812c132013-11-12 15:08:57 -0800496 {"lp8555", LP8555},
Kim, Milo7be865a2012-03-23 15:02:01 -0700497 {"lp8556", LP8556},
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800498 {"lp8557", LP8557},
Kim, Milo7be865a2012-03-23 15:02:01 -0700499 { }
500};
501MODULE_DEVICE_TABLE(i2c, lp855x_ids);
502
503static struct i2c_driver lp855x_driver = {
504 .driver = {
505 .name = "lp855x",
Kim, Milo4add0662013-04-29 16:18:06 -0700506 .of_match_table = of_match_ptr(lp855x_dt_ids),
Kim, Milo7be865a2012-03-23 15:02:01 -0700507 },
508 .probe = lp855x_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500509 .remove = lp855x_remove,
Kim, Milo7be865a2012-03-23 15:02:01 -0700510 .id_table = lp855x_ids,
511};
512
513module_i2c_driver(lp855x_driver);
514
515MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
516MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
517MODULE_LICENSE("GPL");