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>
20 #include <linux/regulator/consumer.h>
22 /* LP8550/1/2/3/6 Registers */
23 #define LP855X_BRIGHTNESS_CTRL 0x00
24 #define LP855X_DEVICE_CTRL 0x01
25 #define LP855X_EEPROM_START 0xA0
26 #define LP855X_EEPROM_END 0xA7
27 #define LP8556_EPROM_START 0xA0
28 #define LP8556_EPROM_END 0xAF
30 /* LP8555/7 Registers */
31 #define LP8557_BL_CMD 0x00
32 #define LP8557_BL_MASK 0x01
33 #define LP8557_BL_ON 0x01
34 #define LP8557_BL_OFF 0x00
35 #define LP8557_BRIGHTNESS_CTRL 0x04
36 #define LP8557_CONFIG 0x10
37 #define LP8555_EPROM_START 0x10
38 #define LP8555_EPROM_END 0x7A
39 #define LP8557_EPROM_START 0x10
40 #define LP8557_EPROM_END 0x1E
42 #define DEFAULT_BL_NAME "lcd-backlight"
43 #define MAX_BRIGHTNESS 255
45 enum lp855x_brightness_ctrl_mode
{
53 * struct lp855x_device_config
54 * @pre_init_device: init device function call before updating the brightness
55 * @reg_brightness: register address for brigthenss control
56 * @reg_devicectrl: register address for device control
57 * @post_init_device: late init device function call
59 struct lp855x_device_config
{
60 int (*pre_init_device
)(struct lp855x
*);
63 int (*post_init_device
)(struct lp855x
*);
68 enum lp855x_chip_id chip_id
;
69 enum lp855x_brightness_ctrl_mode mode
;
70 struct lp855x_device_config
*cfg
;
71 struct i2c_client
*client
;
72 struct backlight_device
*bl
;
74 struct lp855x_platform_data
*pdata
;
75 struct pwm_device
*pwm
;
76 struct regulator
*supply
; /* regulator for VDD input */
79 static int lp855x_write_byte(struct lp855x
*lp
, u8 reg
, u8 data
)
81 return i2c_smbus_write_byte_data(lp
->client
, reg
, data
);
84 static int lp855x_update_bit(struct lp855x
*lp
, u8 reg
, u8 mask
, u8 data
)
89 ret
= i2c_smbus_read_byte_data(lp
->client
, reg
);
91 dev_err(lp
->dev
, "failed to read 0x%.2x\n", reg
);
99 return lp855x_write_byte(lp
, reg
, tmp
);
102 static bool lp855x_is_valid_rom_area(struct lp855x
*lp
, u8 addr
)
106 switch (lp
->chip_id
) {
111 start
= LP855X_EEPROM_START
;
112 end
= LP855X_EEPROM_END
;
115 start
= LP8556_EPROM_START
;
116 end
= LP8556_EPROM_END
;
119 start
= LP8555_EPROM_START
;
120 end
= LP8555_EPROM_END
;
123 start
= LP8557_EPROM_START
;
124 end
= LP8557_EPROM_END
;
130 return addr
>= start
&& addr
<= end
;
133 static int lp8557_bl_off(struct lp855x
*lp
)
135 /* BL_ON = 0 before updating EPROM settings */
136 return lp855x_update_bit(lp
, LP8557_BL_CMD
, LP8557_BL_MASK
,
140 static int lp8557_bl_on(struct lp855x
*lp
)
142 /* BL_ON = 1 after updating EPROM settings */
143 return lp855x_update_bit(lp
, LP8557_BL_CMD
, LP8557_BL_MASK
,
147 static struct lp855x_device_config lp855x_dev_cfg
= {
148 .reg_brightness
= LP855X_BRIGHTNESS_CTRL
,
149 .reg_devicectrl
= LP855X_DEVICE_CTRL
,
152 static struct lp855x_device_config lp8557_dev_cfg
= {
153 .reg_brightness
= LP8557_BRIGHTNESS_CTRL
,
154 .reg_devicectrl
= LP8557_CONFIG
,
155 .pre_init_device
= lp8557_bl_off
,
156 .post_init_device
= lp8557_bl_on
,
160 * Device specific configuration flow
162 * a) pre_init_device(optional)
163 * b) update the brightness register
164 * c) update device control register
165 * d) update ROM area(optional)
166 * e) post_init_device(optional)
169 static int lp855x_configure(struct lp855x
*lp
)
173 struct lp855x_platform_data
*pd
= lp
->pdata
;
175 switch (lp
->chip_id
) {
181 lp
->cfg
= &lp855x_dev_cfg
;
185 lp
->cfg
= &lp8557_dev_cfg
;
191 if (lp
->cfg
->pre_init_device
) {
192 ret
= lp
->cfg
->pre_init_device(lp
);
194 dev_err(lp
->dev
, "pre init device err: %d\n", ret
);
199 val
= pd
->initial_brightness
;
200 ret
= lp855x_write_byte(lp
, lp
->cfg
->reg_brightness
, val
);
204 val
= pd
->device_control
;
205 ret
= lp855x_write_byte(lp
, lp
->cfg
->reg_devicectrl
, val
);
209 if (pd
->size_program
> 0) {
210 for (i
= 0; i
< pd
->size_program
; i
++) {
211 addr
= pd
->rom_data
[i
].addr
;
212 val
= pd
->rom_data
[i
].val
;
213 if (!lp855x_is_valid_rom_area(lp
, addr
))
216 ret
= lp855x_write_byte(lp
, addr
, val
);
222 if (lp
->cfg
->post_init_device
) {
223 ret
= lp
->cfg
->post_init_device(lp
);
225 dev_err(lp
->dev
, "post init device err: %d\n", ret
);
236 static void lp855x_pwm_ctrl(struct lp855x
*lp
, int br
, int max_br
)
238 unsigned int period
= lp
->pdata
->period_ns
;
239 unsigned int duty
= br
* period
/ max_br
;
240 struct pwm_device
*pwm
;
242 /* request pwm device with the consumer name */
244 pwm
= devm_pwm_get(lp
->dev
, lp
->chipname
);
251 pwm_config(lp
->pwm
, duty
, period
);
255 pwm_disable(lp
->pwm
);
258 static int lp855x_bl_update_status(struct backlight_device
*bl
)
260 struct lp855x
*lp
= bl_get_data(bl
);
261 int brightness
= bl
->props
.brightness
;
263 if (bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
266 if (lp
->mode
== PWM_BASED
)
267 lp855x_pwm_ctrl(lp
, brightness
, bl
->props
.max_brightness
);
268 else if (lp
->mode
== REGISTER_BASED
)
269 lp855x_write_byte(lp
, lp
->cfg
->reg_brightness
, (u8
)brightness
);
274 static const struct backlight_ops lp855x_bl_ops
= {
275 .options
= BL_CORE_SUSPENDRESUME
,
276 .update_status
= lp855x_bl_update_status
,
279 static int lp855x_backlight_register(struct lp855x
*lp
)
281 struct backlight_device
*bl
;
282 struct backlight_properties props
;
283 struct lp855x_platform_data
*pdata
= lp
->pdata
;
284 const char *name
= pdata
->name
? : DEFAULT_BL_NAME
;
286 memset(&props
, 0, sizeof(props
));
287 props
.type
= BACKLIGHT_PLATFORM
;
288 props
.max_brightness
= MAX_BRIGHTNESS
;
290 if (pdata
->initial_brightness
> props
.max_brightness
)
291 pdata
->initial_brightness
= props
.max_brightness
;
293 props
.brightness
= pdata
->initial_brightness
;
295 bl
= devm_backlight_device_register(lp
->dev
, name
, lp
->dev
, lp
,
296 &lp855x_bl_ops
, &props
);
305 static ssize_t
lp855x_get_chip_id(struct device
*dev
,
306 struct device_attribute
*attr
, char *buf
)
308 struct lp855x
*lp
= dev_get_drvdata(dev
);
310 return scnprintf(buf
, PAGE_SIZE
, "%s\n", lp
->chipname
);
313 static ssize_t
lp855x_get_bl_ctl_mode(struct device
*dev
,
314 struct device_attribute
*attr
, char *buf
)
316 struct lp855x
*lp
= dev_get_drvdata(dev
);
317 char *strmode
= NULL
;
319 if (lp
->mode
== PWM_BASED
)
320 strmode
= "pwm based";
321 else if (lp
->mode
== REGISTER_BASED
)
322 strmode
= "register based";
324 return scnprintf(buf
, PAGE_SIZE
, "%s\n", strmode
);
327 static DEVICE_ATTR(chip_id
, S_IRUGO
, lp855x_get_chip_id
, NULL
);
328 static DEVICE_ATTR(bl_ctl_mode
, S_IRUGO
, lp855x_get_bl_ctl_mode
, NULL
);
330 static struct attribute
*lp855x_attributes
[] = {
331 &dev_attr_chip_id
.attr
,
332 &dev_attr_bl_ctl_mode
.attr
,
336 static const struct attribute_group lp855x_attr_group
= {
337 .attrs
= lp855x_attributes
,
341 static int lp855x_parse_dt(struct lp855x
*lp
)
343 struct device
*dev
= lp
->dev
;
344 struct device_node
*node
= dev
->of_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];
388 static int lp855x_parse_dt(struct lp855x
*lp
)
394 static int lp855x_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
399 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
402 lp
= devm_kzalloc(&cl
->dev
, sizeof(struct lp855x
), GFP_KERNEL
);
408 lp
->chipname
= id
->name
;
409 lp
->chip_id
= id
->driver_data
;
410 lp
->pdata
= dev_get_platdata(&cl
->dev
);
413 ret
= lp855x_parse_dt(lp
);
418 if (lp
->pdata
->period_ns
> 0)
419 lp
->mode
= PWM_BASED
;
421 lp
->mode
= REGISTER_BASED
;
423 lp
->supply
= devm_regulator_get(lp
->dev
, "power");
424 if (IS_ERR(lp
->supply
)) {
425 if (PTR_ERR(lp
->supply
) == -EPROBE_DEFER
)
426 return -EPROBE_DEFER
;
431 ret
= regulator_enable(lp
->supply
);
433 dev_err(&cl
->dev
, "failed to enable supply: %d\n", ret
);
438 i2c_set_clientdata(cl
, lp
);
440 ret
= lp855x_configure(lp
);
442 dev_err(lp
->dev
, "device config err: %d", ret
);
446 ret
= lp855x_backlight_register(lp
);
449 "failed to register backlight. err: %d\n", ret
);
453 ret
= sysfs_create_group(&lp
->dev
->kobj
, &lp855x_attr_group
);
455 dev_err(lp
->dev
, "failed to register sysfs. err: %d\n", ret
);
459 backlight_update_status(lp
->bl
);
463 static int lp855x_remove(struct i2c_client
*cl
)
465 struct lp855x
*lp
= i2c_get_clientdata(cl
);
467 lp
->bl
->props
.brightness
= 0;
468 backlight_update_status(lp
->bl
);
470 regulator_disable(lp
->supply
);
471 sysfs_remove_group(&lp
->dev
->kobj
, &lp855x_attr_group
);
476 static const struct of_device_id lp855x_dt_ids
[] = {
477 { .compatible
= "ti,lp8550", },
478 { .compatible
= "ti,lp8551", },
479 { .compatible
= "ti,lp8552", },
480 { .compatible
= "ti,lp8553", },
481 { .compatible
= "ti,lp8555", },
482 { .compatible
= "ti,lp8556", },
483 { .compatible
= "ti,lp8557", },
486 MODULE_DEVICE_TABLE(of
, lp855x_dt_ids
);
488 static const struct i2c_device_id lp855x_ids
[] = {
498 MODULE_DEVICE_TABLE(i2c
, lp855x_ids
);
500 static struct i2c_driver lp855x_driver
= {
503 .of_match_table
= of_match_ptr(lp855x_dt_ids
),
505 .probe
= lp855x_probe
,
506 .remove
= lp855x_remove
,
507 .id_table
= lp855x_ids
,
510 module_i2c_driver(lp855x_driver
);
512 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
513 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
514 MODULE_LICENSE("GPL");