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>
17 #include <linux/platform_data/lp855x.h>
18 #include <linux/pwm.h>
20 /* LP8550/1/2/3/6 Registers */
21 #define LP855X_BRIGHTNESS_CTRL 0x00
22 #define LP855X_DEVICE_CTRL 0x01
23 #define LP855X_EEPROM_START 0xA0
24 #define LP855X_EEPROM_END 0xA7
25 #define LP8556_EPROM_START 0xA0
26 #define LP8556_EPROM_END 0xAF
28 /* LP8557 Registers */
29 #define LP8557_BL_CMD 0x00
30 #define LP8557_BL_MASK 0x01
31 #define LP8557_BL_ON 0x01
32 #define LP8557_BL_OFF 0x00
33 #define LP8557_BRIGHTNESS_CTRL 0x04
34 #define LP8557_CONFIG 0x10
35 #define LP8557_EPROM_START 0x10
36 #define LP8557_EPROM_END 0x1E
38 #define DEFAULT_BL_NAME "lcd-backlight"
39 #define MAX_BRIGHTNESS 255
44 * struct lp855x_device_config
45 * @pre_init_device: init device function call before updating the brightness
46 * @reg_brightness: register address for brigthenss control
47 * @reg_devicectrl: register address for device control
48 * @post_init_device: late init device function call
50 struct lp855x_device_config {
51 int (*pre_init_device)(struct lp855x *);
54 int (*post_init_device)(struct lp855x *);
59 enum lp855x_chip_id chip_id;
60 struct lp855x_device_config *cfg;
61 struct i2c_client *client;
62 struct backlight_device *bl;
64 struct lp855x_platform_data *pdata;
65 struct pwm_device *pwm;
68 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
70 return i2c_smbus_write_byte_data(lp->client, reg, data);
73 static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
78 ret = i2c_smbus_read_byte_data(lp->client, reg);
80 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
88 return lp855x_write_byte(lp, reg, tmp);
91 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
95 switch (lp->chip_id) {
100 start = LP855X_EEPROM_START;
101 end = LP855X_EEPROM_END;
104 start = LP8556_EPROM_START;
105 end = LP8556_EPROM_END;
108 start = LP8557_EPROM_START;
109 end = LP8557_EPROM_END;
115 return (addr >= start && addr <= end);
118 static int lp8557_bl_off(struct lp855x *lp)
120 /* BL_ON = 0 before updating EPROM settings */
121 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
125 static int lp8557_bl_on(struct lp855x *lp)
127 /* BL_ON = 1 after updating EPROM settings */
128 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
132 static struct lp855x_device_config lp855x_dev_cfg = {
133 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
134 .reg_devicectrl = LP855X_DEVICE_CTRL,
137 static struct lp855x_device_config lp8557_dev_cfg = {
138 .reg_brightness = LP8557_BRIGHTNESS_CTRL,
139 .reg_devicectrl = LP8557_CONFIG,
140 .pre_init_device = lp8557_bl_off,
141 .post_init_device = lp8557_bl_on,
145 * Device specific configuration flow
147 * a) pre_init_device(optional)
148 * b) update the brightness register
149 * c) update device control register
150 * d) update ROM area(optional)
151 * e) post_init_device(optional)
154 static int lp855x_configure(struct lp855x *lp)
158 struct lp855x_platform_data *pd = lp->pdata;
160 switch (lp->chip_id) {
161 case LP8550 ... LP8556:
162 lp->cfg = &lp855x_dev_cfg;
165 lp->cfg = &lp8557_dev_cfg;
171 if (lp->cfg->pre_init_device) {
172 ret = lp->cfg->pre_init_device(lp);
174 dev_err(lp->dev, "pre init device err: %d\n", ret);
179 val = pd->initial_brightness;
180 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
184 val = pd->device_control;
185 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
189 if (pd->load_new_rom_data && pd->size_program) {
190 for (i = 0; i < pd->size_program; i++) {
191 addr = pd->rom_data[i].addr;
192 val = pd->rom_data[i].val;
193 if (!lp855x_is_valid_rom_area(lp, addr))
196 ret = lp855x_write_byte(lp, addr, val);
202 if (lp->cfg->post_init_device) {
203 ret = lp->cfg->post_init_device(lp);
205 dev_err(lp->dev, "post init device err: %d\n", ret);
216 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
218 unsigned int period = lp->pdata->period_ns;
219 unsigned int duty = br * period / max_br;
220 struct pwm_device *pwm;
222 /* request pwm device with the consumer name */
224 pwm = devm_pwm_get(lp->dev, lp->chipname);
231 pwm_config(lp->pwm, duty, period);
235 pwm_disable(lp->pwm);
238 static int lp855x_bl_update_status(struct backlight_device *bl)
240 struct lp855x *lp = bl_get_data(bl);
241 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
243 if (bl->props.state & BL_CORE_SUSPENDED)
244 bl->props.brightness = 0;
246 if (mode == PWM_BASED) {
247 int br = bl->props.brightness;
248 int max_br = bl->props.max_brightness;
250 lp855x_pwm_ctrl(lp, br, max_br);
252 } else if (mode == REGISTER_BASED) {
253 u8 val = bl->props.brightness;
254 lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
260 static int lp855x_bl_get_brightness(struct backlight_device *bl)
262 return bl->props.brightness;
265 static const struct backlight_ops lp855x_bl_ops = {
266 .options = BL_CORE_SUSPENDRESUME,
267 .update_status = lp855x_bl_update_status,
268 .get_brightness = lp855x_bl_get_brightness,
271 static int lp855x_backlight_register(struct lp855x *lp)
273 struct backlight_device *bl;
274 struct backlight_properties props;
275 struct lp855x_platform_data *pdata = lp->pdata;
276 const char *name = pdata->name ? : DEFAULT_BL_NAME;
278 props.type = BACKLIGHT_PLATFORM;
279 props.max_brightness = MAX_BRIGHTNESS;
281 if (pdata->initial_brightness > props.max_brightness)
282 pdata->initial_brightness = props.max_brightness;
284 props.brightness = pdata->initial_brightness;
286 bl = backlight_device_register(name, lp->dev, lp,
287 &lp855x_bl_ops, &props);
296 static void lp855x_backlight_unregister(struct lp855x *lp)
299 backlight_device_unregister(lp->bl);
302 static ssize_t lp855x_get_chip_id(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct lp855x *lp = dev_get_drvdata(dev);
306 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
309 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct lp855x *lp = dev_get_drvdata(dev);
313 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
314 char *strmode = NULL;
316 if (mode == PWM_BASED)
317 strmode = "pwm based";
318 else if (mode == REGISTER_BASED)
319 strmode = "register based";
321 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
324 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
325 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
327 static struct attribute *lp855x_attributes[] = {
328 &dev_attr_chip_id.attr,
329 &dev_attr_bl_ctl_mode.attr,
333 static const struct attribute_group lp855x_attr_group = {
334 .attrs = lp855x_attributes,
337 static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
340 struct lp855x_platform_data *pdata = cl->dev.platform_data;
344 dev_err(&cl->dev, "no platform data supplied\n");
348 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
351 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
358 lp->chipname = id->name;
359 lp->chip_id = id->driver_data;
360 i2c_set_clientdata(cl, lp);
362 ret = lp855x_configure(lp);
364 dev_err(lp->dev, "device config err: %d", ret);
368 ret = lp855x_backlight_register(lp);
371 "failed to register backlight. err: %d\n", ret);
375 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
377 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
381 backlight_update_status(lp->bl);
385 lp855x_backlight_unregister(lp);
390 static int lp855x_remove(struct i2c_client *cl)
392 struct lp855x *lp = i2c_get_clientdata(cl);
394 lp->bl->props.brightness = 0;
395 backlight_update_status(lp->bl);
396 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
397 lp855x_backlight_unregister(lp);
402 static const struct i2c_device_id lp855x_ids[] = {
411 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
413 static struct i2c_driver lp855x_driver = {
417 .probe = lp855x_probe,
418 .remove = lp855x_remove,
419 .id_table = lp855x_ids,
422 module_i2c_driver(lp855x_driver);
424 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
425 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
426 MODULE_LICENSE("GPL");