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
39 #define DEFAULT_BL_NAME "lcd-backlight"
40 #define MAX_BRIGHTNESS 255
45 * struct lp855x_device_config
46 * @pre_init_device: init device function call before updating the brightness
47 * @reg_brightness: register address for brigthenss control
48 * @reg_devicectrl: register address for device control
49 * @post_init_device: late init device function call
51 struct lp855x_device_config {
52 int (*pre_init_device)(struct lp855x *);
55 int (*post_init_device)(struct lp855x *);
60 enum lp855x_chip_id chip_id;
61 struct lp855x_device_config *cfg;
62 struct i2c_client *client;
63 struct backlight_device *bl;
65 struct lp855x_platform_data *pdata;
66 struct pwm_device *pwm;
69 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
71 return i2c_smbus_write_byte_data(lp->client, reg, data);
74 static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
79 ret = i2c_smbus_read_byte_data(lp->client, reg);
81 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
89 return lp855x_write_byte(lp, reg, tmp);
92 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
96 switch (lp->chip_id) {
101 start = LP855X_EEPROM_START;
102 end = LP855X_EEPROM_END;
105 start = LP8556_EPROM_START;
106 end = LP8556_EPROM_END;
109 start = LP8557_EPROM_START;
110 end = LP8557_EPROM_END;
116 return (addr >= start && addr <= end);
119 static int lp8557_bl_off(struct lp855x *lp)
121 /* BL_ON = 0 before updating EPROM settings */
122 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
126 static int lp8557_bl_on(struct lp855x *lp)
128 /* BL_ON = 1 after updating EPROM settings */
129 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
133 static struct lp855x_device_config lp855x_dev_cfg = {
134 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
135 .reg_devicectrl = LP855X_DEVICE_CTRL,
138 static struct lp855x_device_config lp8557_dev_cfg = {
139 .reg_brightness = LP8557_BRIGHTNESS_CTRL,
140 .reg_devicectrl = LP8557_CONFIG,
141 .pre_init_device = lp8557_bl_off,
142 .post_init_device = lp8557_bl_on,
146 * Device specific configuration flow
148 * a) pre_init_device(optional)
149 * b) update the brightness register
150 * c) update device control register
151 * d) update ROM area(optional)
152 * e) post_init_device(optional)
155 static int lp855x_configure(struct lp855x *lp)
159 struct lp855x_platform_data *pd = lp->pdata;
161 switch (lp->chip_id) {
162 case LP8550 ... LP8556:
163 lp->cfg = &lp855x_dev_cfg;
166 lp->cfg = &lp8557_dev_cfg;
172 if (lp->cfg->pre_init_device) {
173 ret = lp->cfg->pre_init_device(lp);
175 dev_err(lp->dev, "pre init device err: %d\n", ret);
180 val = pd->initial_brightness;
181 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
185 val = pd->device_control;
186 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
190 if (pd->load_new_rom_data && pd->size_program) {
191 for (i = 0; i < pd->size_program; i++) {
192 addr = pd->rom_data[i].addr;
193 val = pd->rom_data[i].val;
194 if (!lp855x_is_valid_rom_area(lp, addr))
197 ret = lp855x_write_byte(lp, addr, val);
203 if (lp->cfg->post_init_device) {
204 ret = lp->cfg->post_init_device(lp);
206 dev_err(lp->dev, "post init device err: %d\n", ret);
217 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
219 unsigned int period = lp->pdata->period_ns;
220 unsigned int duty = br * period / max_br;
221 struct pwm_device *pwm;
223 /* request pwm device with the consumer name */
225 pwm = devm_pwm_get(lp->dev, lp->chipname);
232 pwm_config(lp->pwm, duty, period);
236 pwm_disable(lp->pwm);
239 static int lp855x_bl_update_status(struct backlight_device *bl)
241 struct lp855x *lp = bl_get_data(bl);
242 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
244 if (bl->props.state & BL_CORE_SUSPENDED)
245 bl->props.brightness = 0;
247 if (mode == PWM_BASED) {
248 int br = bl->props.brightness;
249 int max_br = bl->props.max_brightness;
251 lp855x_pwm_ctrl(lp, br, max_br);
253 } else if (mode == REGISTER_BASED) {
254 u8 val = bl->props.brightness;
255 lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
261 static int lp855x_bl_get_brightness(struct backlight_device *bl)
263 return bl->props.brightness;
266 static const struct backlight_ops lp855x_bl_ops = {
267 .options = BL_CORE_SUSPENDRESUME,
268 .update_status = lp855x_bl_update_status,
269 .get_brightness = lp855x_bl_get_brightness,
272 static int lp855x_backlight_register(struct lp855x *lp)
274 struct backlight_device *bl;
275 struct backlight_properties props;
276 struct lp855x_platform_data *pdata = lp->pdata;
277 char *name = pdata->name ? : DEFAULT_BL_NAME;
279 props.type = BACKLIGHT_PLATFORM;
280 props.max_brightness = MAX_BRIGHTNESS;
282 if (pdata->initial_brightness > props.max_brightness)
283 pdata->initial_brightness = props.max_brightness;
285 props.brightness = pdata->initial_brightness;
287 bl = backlight_device_register(name, lp->dev, lp,
288 &lp855x_bl_ops, &props);
297 static void lp855x_backlight_unregister(struct lp855x *lp)
300 backlight_device_unregister(lp->bl);
303 static ssize_t lp855x_get_chip_id(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct lp855x *lp = dev_get_drvdata(dev);
307 return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
310 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct lp855x *lp = dev_get_drvdata(dev);
314 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
315 char *strmode = NULL;
317 if (mode == PWM_BASED)
318 strmode = "pwm based";
319 else if (mode == REGISTER_BASED)
320 strmode = "register based";
322 return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
325 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
326 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
328 static struct attribute *lp855x_attributes[] = {
329 &dev_attr_chip_id.attr,
330 &dev_attr_bl_ctl_mode.attr,
334 static const struct attribute_group lp855x_attr_group = {
335 .attrs = lp855x_attributes,
338 static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
341 struct lp855x_platform_data *pdata = cl->dev.platform_data;
342 enum lp855x_brightness_ctrl_mode mode;
346 dev_err(&cl->dev, "no platform data supplied\n");
350 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
353 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
361 lp->chipname = id->name;
362 lp->chip_id = id->driver_data;
363 i2c_set_clientdata(cl, lp);
365 ret = lp855x_configure(lp);
367 dev_err(lp->dev, "device config err: %d", ret);
371 ret = lp855x_backlight_register(lp);
374 "failed to register backlight. err: %d\n", ret);
378 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
380 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
384 backlight_update_status(lp->bl);
388 lp855x_backlight_unregister(lp);
393 static int lp855x_remove(struct i2c_client *cl)
395 struct lp855x *lp = i2c_get_clientdata(cl);
397 lp->bl->props.brightness = 0;
398 backlight_update_status(lp->bl);
399 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
400 lp855x_backlight_unregister(lp);
405 static const struct i2c_device_id lp855x_ids[] = {
414 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
416 static struct i2c_driver lp855x_driver = {
420 .probe = lp855x_probe,
421 .remove = lp855x_remove,
422 .id_table = lp855x_ids,
425 module_i2c_driver(lp855x_driver);
427 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
428 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
429 MODULE_LICENSE("GPL");