2 * TI LP855x Backlight Driver
4 * Copyright (C) 2011 Texas Instruments
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.
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>
18 #include <linux/platform_data/lp855x.h>
19 #include <linux/pwm.h>
21 /* 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
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
39 #define DEFAULT_BL_NAME "lcd-backlight"
40 #define MAX_BRIGHTNESS 255
42 enum lp855x_brightness_ctrl_mode {
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
56 struct lp855x_device_config {
57 int (*pre_init_device)(struct lp855x *);
60 int (*post_init_device)(struct lp855x *);
65 enum lp855x_chip_id chip_id;
66 enum lp855x_brightness_ctrl_mode mode;
67 struct lp855x_device_config *cfg;
68 struct i2c_client *client;
69 struct backlight_device *bl;
71 struct lp855x_platform_data *pdata;
72 struct pwm_device *pwm;
75 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
77 return i2c_smbus_write_byte_data(lp->client, reg, data);
80 static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
85 ret = i2c_smbus_read_byte_data(lp->client, reg);
87 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
95 return lp855x_write_byte(lp, reg, tmp);
98 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
102 switch (lp->chip_id) {
107 start = LP855X_EEPROM_START;
108 end = LP855X_EEPROM_END;
111 start = LP8556_EPROM_START;
112 end = LP8556_EPROM_END;
115 start = LP8557_EPROM_START;
116 end = LP8557_EPROM_END;
122 return (addr >= start && addr <= end);
125 static int lp8557_bl_off(struct lp855x *lp)
127 /* BL_ON = 0 before updating EPROM settings */
128 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
132 static int lp8557_bl_on(struct lp855x *lp)
134 /* BL_ON = 1 after updating EPROM settings */
135 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
139 static struct lp855x_device_config lp855x_dev_cfg = {
140 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
141 .reg_devicectrl = LP855X_DEVICE_CTRL,
144 static 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,
152 * Device specific configuration flow
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)
161 static int lp855x_configure(struct lp855x *lp)
165 struct lp855x_platform_data *pd = lp->pdata;
167 switch (lp->chip_id) {
168 case LP8550 ... LP8556:
169 lp->cfg = &lp855x_dev_cfg;
172 lp->cfg = &lp8557_dev_cfg;
178 if (lp->cfg->pre_init_device) {
179 ret = lp->cfg->pre_init_device(lp);
181 dev_err(lp->dev, "pre init device err: %d\n", ret);
186 val = pd->initial_brightness;
187 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
191 val = pd->device_control;
192 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
196 if (pd->size_program > 0) {
197 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))
203 ret = lp855x_write_byte(lp, addr, val);
209 if (lp->cfg->post_init_device) {
210 ret = lp->cfg->post_init_device(lp);
212 dev_err(lp->dev, "post init device err: %d\n", ret);
223 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
225 unsigned int period = lp->pdata->period_ns;
226 unsigned int duty = br * period / max_br;
227 struct pwm_device *pwm;
229 /* request pwm device with the consumer name */
231 pwm = devm_pwm_get(lp->dev, lp->chipname);
238 pwm_config(lp->pwm, duty, period);
242 pwm_disable(lp->pwm);
245 static int lp855x_bl_update_status(struct backlight_device *bl)
247 struct lp855x *lp = bl_get_data(bl);
249 if (bl->props.state & BL_CORE_SUSPENDED)
250 bl->props.brightness = 0;
252 if (lp->mode == PWM_BASED) {
253 int br = bl->props.brightness;
254 int max_br = bl->props.max_brightness;
256 lp855x_pwm_ctrl(lp, br, max_br);
258 } else if (lp->mode == REGISTER_BASED) {
259 u8 val = bl->props.brightness;
260 lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
266 static int lp855x_bl_get_brightness(struct backlight_device *bl)
268 return bl->props.brightness;
271 static 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,
277 static int lp855x_backlight_register(struct lp855x *lp)
279 struct backlight_device *bl;
280 struct backlight_properties props;
281 struct lp855x_platform_data *pdata = lp->pdata;
282 const char *name = pdata->name ? : DEFAULT_BL_NAME;
284 props.type = BACKLIGHT_PLATFORM;
285 props.max_brightness = MAX_BRIGHTNESS;
287 if (pdata->initial_brightness > props.max_brightness)
288 pdata->initial_brightness = props.max_brightness;
290 props.brightness = pdata->initial_brightness;
292 bl = backlight_device_register(name, lp->dev, lp,
293 &lp855x_bl_ops, &props);
302 static void lp855x_backlight_unregister(struct lp855x *lp)
305 backlight_device_unregister(lp->bl);
308 static ssize_t lp855x_get_chip_id(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 struct lp855x *lp = dev_get_drvdata(dev);
312 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
315 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct lp855x *lp = dev_get_drvdata(dev);
319 char *strmode = NULL;
321 if (lp->mode == PWM_BASED)
322 strmode = "pwm based";
323 else if (lp->mode == REGISTER_BASED)
324 strmode = "register based";
326 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
329 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
330 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
332 static struct attribute *lp855x_attributes[] = {
333 &dev_attr_chip_id.attr,
334 &dev_attr_bl_ctl_mode.attr,
338 static const struct attribute_group lp855x_attr_group = {
339 .attrs = lp855x_attributes,
343 static int lp855x_parse_dt(struct device *dev, struct device_node *node)
345 struct lp855x_platform_data *pdata;
349 dev_err(dev, "no platform data\n");
353 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
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);
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;
369 rom = devm_kzalloc(dev, sizeof(*rom) * rom_length, GFP_KERNEL);
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);
379 pdata->size_program = rom_length;
380 pdata->rom_data = &rom[0];
383 dev->platform_data = pdata;
388 static int lp855x_parse_dt(struct device *dev, struct device_node *node)
394 static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
397 struct lp855x_platform_data *pdata = cl->dev.platform_data;
398 struct device_node *node = cl->dev.of_node;
402 ret = lp855x_parse_dt(&cl->dev, node);
406 pdata = cl->dev.platform_data;
409 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
412 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
416 if (pdata->period_ns > 0)
417 lp->mode = PWM_BASED;
419 lp->mode = REGISTER_BASED;
424 lp->chipname = id->name;
425 lp->chip_id = id->driver_data;
426 i2c_set_clientdata(cl, lp);
428 ret = lp855x_configure(lp);
430 dev_err(lp->dev, "device config err: %d", ret);
434 ret = lp855x_backlight_register(lp);
437 "failed to register backlight. err: %d\n", ret);
441 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
443 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
447 backlight_update_status(lp->bl);
451 lp855x_backlight_unregister(lp);
456 static int lp855x_remove(struct i2c_client *cl)
458 struct lp855x *lp = i2c_get_clientdata(cl);
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);
468 static 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", },
477 MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
479 static const struct i2c_device_id lp855x_ids[] = {
488 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
490 static struct i2c_driver lp855x_driver = {
493 .of_match_table = of_match_ptr(lp855x_dt_ids),
495 .probe = lp855x_probe,
496 .remove = lp855x_remove,
497 .id_table = lp855x_ids,
500 module_i2c_driver(lp855x_driver);
502 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
503 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
504 MODULE_LICENSE("GPL");