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>
21 #define BRIGHTNESS_CTRL 0x00
22 #define DEVICE_CTRL 0x01
23 #define EEPROM_START 0xA0
24 #define EEPROM_END 0xA7
25 #define EPROM_START 0xA0
26 #define EPROM_END 0xAF
29 #define DEFAULT_BL_NAME "lcd-backlight"
30 #define MAX_BRIGHTNESS 255
35 * struct lp855x_device_config
36 * @pre_init_device: init device function call before updating the brightness
37 * @reg_brightness: register address for brigthenss control
38 * @reg_devicectrl: register address for device control
39 * @post_init_device: late init device function call
41 struct lp855x_device_config {
42 int (*pre_init_device)(struct lp855x *);
45 int (*post_init_device)(struct lp855x *);
50 enum lp855x_chip_id chip_id;
51 struct lp855x_device_config *cfg;
52 struct i2c_client *client;
53 struct backlight_device *bl;
55 struct lp855x_platform_data *pdata;
56 struct pwm_device *pwm;
59 static int lp855x_read_byte(struct lp855x *lp, u8 reg, u8 *data)
63 ret = i2c_smbus_read_byte_data(lp->client, reg);
65 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
73 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
75 return i2c_smbus_write_byte_data(lp->client, reg, data);
78 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
82 switch (lp->chip_id) {
98 return (addr >= start && addr <= end);
101 static struct lp855x_device_config lp855x_dev_cfg = {
102 .reg_brightness = BRIGHTNESS_CTRL,
103 .reg_devicectrl = DEVICE_CTRL,
107 * Device specific configuration flow
109 * a) pre_init_device(optional)
110 * b) update the brightness register
111 * c) update device control register
112 * d) update ROM area(optional)
113 * e) post_init_device(optional)
116 static int lp855x_configure(struct lp855x *lp)
120 struct lp855x_platform_data *pd = lp->pdata;
122 switch (lp->chip_id) {
123 case LP8550 ... LP8556:
124 lp->cfg = &lp855x_dev_cfg;
130 if (lp->cfg->pre_init_device) {
131 ret = lp->cfg->pre_init_device(lp);
133 dev_err(lp->dev, "pre init device err: %d\n", ret);
138 val = pd->initial_brightness;
139 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
143 val = pd->device_control;
144 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
148 if (pd->load_new_rom_data && pd->size_program) {
149 for (i = 0; i < pd->size_program; i++) {
150 addr = pd->rom_data[i].addr;
151 val = pd->rom_data[i].val;
152 if (!lp855x_is_valid_rom_area(lp, addr))
155 ret = lp855x_write_byte(lp, addr, val);
161 if (lp->cfg->post_init_device) {
162 ret = lp->cfg->post_init_device(lp);
164 dev_err(lp->dev, "post init device err: %d\n", ret);
175 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
177 unsigned int period = lp->pdata->period_ns;
178 unsigned int duty = br * period / max_br;
179 struct pwm_device *pwm;
181 /* request pwm device with the consumer name */
183 pwm = devm_pwm_get(lp->dev, lp->chipname);
190 pwm_config(lp->pwm, duty, period);
194 pwm_disable(lp->pwm);
197 static int lp855x_bl_update_status(struct backlight_device *bl)
199 struct lp855x *lp = bl_get_data(bl);
200 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
202 if (bl->props.state & BL_CORE_SUSPENDED)
203 bl->props.brightness = 0;
205 if (mode == PWM_BASED) {
206 int br = bl->props.brightness;
207 int max_br = bl->props.max_brightness;
209 lp855x_pwm_ctrl(lp, br, max_br);
211 } else if (mode == REGISTER_BASED) {
212 u8 val = bl->props.brightness;
213 lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
219 static int lp855x_bl_get_brightness(struct backlight_device *bl)
221 struct lp855x *lp = bl_get_data(bl);
222 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
224 if (mode == REGISTER_BASED) {
227 lp855x_read_byte(lp, BRIGHTNESS_CTRL, &val);
228 bl->props.brightness = val;
231 return bl->props.brightness;
234 static const struct backlight_ops lp855x_bl_ops = {
235 .options = BL_CORE_SUSPENDRESUME,
236 .update_status = lp855x_bl_update_status,
237 .get_brightness = lp855x_bl_get_brightness,
240 static int lp855x_backlight_register(struct lp855x *lp)
242 struct backlight_device *bl;
243 struct backlight_properties props;
244 struct lp855x_platform_data *pdata = lp->pdata;
245 char *name = pdata->name ? : DEFAULT_BL_NAME;
247 props.type = BACKLIGHT_PLATFORM;
248 props.max_brightness = MAX_BRIGHTNESS;
250 if (pdata->initial_brightness > props.max_brightness)
251 pdata->initial_brightness = props.max_brightness;
253 props.brightness = pdata->initial_brightness;
255 bl = backlight_device_register(name, lp->dev, lp,
256 &lp855x_bl_ops, &props);
265 static void lp855x_backlight_unregister(struct lp855x *lp)
268 backlight_device_unregister(lp->bl);
271 static ssize_t lp855x_get_chip_id(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct lp855x *lp = dev_get_drvdata(dev);
275 return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
278 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
279 struct device_attribute *attr, char *buf)
281 struct lp855x *lp = dev_get_drvdata(dev);
282 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
283 char *strmode = NULL;
285 if (mode == PWM_BASED)
286 strmode = "pwm based";
287 else if (mode == REGISTER_BASED)
288 strmode = "register based";
290 return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
293 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
294 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
296 static struct attribute *lp855x_attributes[] = {
297 &dev_attr_chip_id.attr,
298 &dev_attr_bl_ctl_mode.attr,
302 static const struct attribute_group lp855x_attr_group = {
303 .attrs = lp855x_attributes,
306 static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
309 struct lp855x_platform_data *pdata = cl->dev.platform_data;
310 enum lp855x_brightness_ctrl_mode mode;
314 dev_err(&cl->dev, "no platform data supplied\n");
318 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
321 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
329 lp->chipname = id->name;
330 lp->chip_id = id->driver_data;
331 i2c_set_clientdata(cl, lp);
333 ret = lp855x_configure(lp);
335 dev_err(lp->dev, "device config err: %d", ret);
339 ret = lp855x_backlight_register(lp);
342 "failed to register backlight. err: %d\n", ret);
346 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
348 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
352 backlight_update_status(lp->bl);
356 lp855x_backlight_unregister(lp);
361 static int lp855x_remove(struct i2c_client *cl)
363 struct lp855x *lp = i2c_get_clientdata(cl);
365 lp->bl->props.brightness = 0;
366 backlight_update_status(lp->bl);
367 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
368 lp855x_backlight_unregister(lp);
373 static const struct i2c_device_id lp855x_ids[] = {
381 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
383 static struct i2c_driver lp855x_driver = {
387 .probe = lp855x_probe,
388 .remove = lp855x_remove,
389 .id_table = lp855x_ids,
392 module_i2c_driver(lp855x_driver);
394 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
395 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
396 MODULE_LICENSE("GPL");