1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP855x Backlight Driver
5 * Copyright (C) 2011 Texas Instruments
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/i2c.h>
11 #include <linux/backlight.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
15 #include <linux/platform_data/lp855x.h>
16 #include <linux/pwm.h>
17 #include <linux/regulator/consumer.h>
19 /* LP8550/1/2/3/6 Registers */
20 #define LP855X_BRIGHTNESS_CTRL 0x00
21 #define LP855X_DEVICE_CTRL 0x01
22 #define LP855X_EEPROM_START 0xA0
23 #define LP855X_EEPROM_END 0xA7
24 #define LP8556_EPROM_START 0xA0
25 #define LP8556_EPROM_END 0xAF
27 /* LP8555/7 Registers */
28 #define LP8557_BL_CMD 0x00
29 #define LP8557_BL_MASK 0x01
30 #define LP8557_BL_ON 0x01
31 #define LP8557_BL_OFF 0x00
32 #define LP8557_BRIGHTNESS_CTRL 0x04
33 #define LP8557_CONFIG 0x10
34 #define LP8555_EPROM_START 0x10
35 #define LP8555_EPROM_END 0x7A
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
;
73 struct regulator
*supply
; /* regulator for VDD input */
74 struct regulator
*enable
; /* regulator for EN/VDDIO input */
77 static int lp855x_write_byte(struct lp855x
*lp
, u8 reg
, u8 data
)
79 return i2c_smbus_write_byte_data(lp
->client
, reg
, data
);
82 static int lp855x_update_bit(struct lp855x
*lp
, u8 reg
, u8 mask
, u8 data
)
87 ret
= i2c_smbus_read_byte_data(lp
->client
, reg
);
89 dev_err(lp
->dev
, "failed to read 0x%.2x\n", reg
);
97 return lp855x_write_byte(lp
, reg
, tmp
);
100 static bool lp855x_is_valid_rom_area(struct lp855x
*lp
, u8 addr
)
104 switch (lp
->chip_id
) {
109 start
= LP855X_EEPROM_START
;
110 end
= LP855X_EEPROM_END
;
113 start
= LP8556_EPROM_START
;
114 end
= LP8556_EPROM_END
;
117 start
= LP8555_EPROM_START
;
118 end
= LP8555_EPROM_END
;
121 start
= LP8557_EPROM_START
;
122 end
= LP8557_EPROM_END
;
128 return addr
>= start
&& addr
<= end
;
131 static int lp8557_bl_off(struct lp855x
*lp
)
133 /* BL_ON = 0 before updating EPROM settings */
134 return lp855x_update_bit(lp
, LP8557_BL_CMD
, LP8557_BL_MASK
,
138 static int lp8557_bl_on(struct lp855x
*lp
)
140 /* BL_ON = 1 after updating EPROM settings */
141 return lp855x_update_bit(lp
, LP8557_BL_CMD
, LP8557_BL_MASK
,
145 static struct lp855x_device_config lp855x_dev_cfg
= {
146 .reg_brightness
= LP855X_BRIGHTNESS_CTRL
,
147 .reg_devicectrl
= LP855X_DEVICE_CTRL
,
150 static struct lp855x_device_config lp8557_dev_cfg
= {
151 .reg_brightness
= LP8557_BRIGHTNESS_CTRL
,
152 .reg_devicectrl
= LP8557_CONFIG
,
153 .pre_init_device
= lp8557_bl_off
,
154 .post_init_device
= lp8557_bl_on
,
158 * Device specific configuration flow
160 * a) pre_init_device(optional)
161 * b) update the brightness register
162 * c) update device control register
163 * d) update ROM area(optional)
164 * e) post_init_device(optional)
167 static int lp855x_configure(struct lp855x
*lp
)
171 struct lp855x_platform_data
*pd
= lp
->pdata
;
173 switch (lp
->chip_id
) {
179 lp
->cfg
= &lp855x_dev_cfg
;
183 lp
->cfg
= &lp8557_dev_cfg
;
189 if (lp
->cfg
->pre_init_device
) {
190 ret
= lp
->cfg
->pre_init_device(lp
);
192 dev_err(lp
->dev
, "pre init device err: %d\n", ret
);
197 val
= pd
->initial_brightness
;
198 ret
= lp855x_write_byte(lp
, lp
->cfg
->reg_brightness
, val
);
202 val
= pd
->device_control
;
203 ret
= lp855x_write_byte(lp
, lp
->cfg
->reg_devicectrl
, val
);
207 if (pd
->size_program
> 0) {
208 for (i
= 0; i
< pd
->size_program
; i
++) {
209 addr
= pd
->rom_data
[i
].addr
;
210 val
= pd
->rom_data
[i
].val
;
211 if (!lp855x_is_valid_rom_area(lp
, addr
))
214 ret
= lp855x_write_byte(lp
, addr
, val
);
220 if (lp
->cfg
->post_init_device
) {
221 ret
= lp
->cfg
->post_init_device(lp
);
223 dev_err(lp
->dev
, "post init device err: %d\n", ret
);
234 static void lp855x_pwm_ctrl(struct lp855x
*lp
, int br
, int max_br
)
236 unsigned int period
= lp
->pdata
->period_ns
;
237 unsigned int duty
= br
* period
/ max_br
;
238 struct pwm_device
*pwm
;
240 /* request pwm device with the consumer name */
242 pwm
= devm_pwm_get(lp
->dev
, lp
->chipname
);
249 * FIXME: pwm_apply_args() should be removed when switching to
250 * the atomic PWM API.
255 pwm_config(lp
->pwm
, duty
, period
);
259 pwm_disable(lp
->pwm
);
262 static int lp855x_bl_update_status(struct backlight_device
*bl
)
264 struct lp855x
*lp
= bl_get_data(bl
);
265 int brightness
= bl
->props
.brightness
;
267 if (bl
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
270 if (lp
->mode
== PWM_BASED
)
271 lp855x_pwm_ctrl(lp
, brightness
, bl
->props
.max_brightness
);
272 else if (lp
->mode
== REGISTER_BASED
)
273 lp855x_write_byte(lp
, lp
->cfg
->reg_brightness
, (u8
)brightness
);
278 static const struct backlight_ops lp855x_bl_ops
= {
279 .options
= BL_CORE_SUSPENDRESUME
,
280 .update_status
= lp855x_bl_update_status
,
283 static int lp855x_backlight_register(struct lp855x
*lp
)
285 struct backlight_device
*bl
;
286 struct backlight_properties props
;
287 struct lp855x_platform_data
*pdata
= lp
->pdata
;
288 const char *name
= pdata
->name
? : DEFAULT_BL_NAME
;
290 memset(&props
, 0, sizeof(props
));
291 props
.type
= BACKLIGHT_PLATFORM
;
292 props
.max_brightness
= MAX_BRIGHTNESS
;
294 if (pdata
->initial_brightness
> props
.max_brightness
)
295 pdata
->initial_brightness
= props
.max_brightness
;
297 props
.brightness
= pdata
->initial_brightness
;
299 bl
= devm_backlight_device_register(lp
->dev
, name
, lp
->dev
, lp
,
300 &lp855x_bl_ops
, &props
);
309 static ssize_t
lp855x_get_chip_id(struct device
*dev
,
310 struct device_attribute
*attr
, char *buf
)
312 struct lp855x
*lp
= dev_get_drvdata(dev
);
314 return scnprintf(buf
, PAGE_SIZE
, "%s\n", lp
->chipname
);
317 static ssize_t
lp855x_get_bl_ctl_mode(struct device
*dev
,
318 struct device_attribute
*attr
, char *buf
)
320 struct lp855x
*lp
= dev_get_drvdata(dev
);
321 char *strmode
= NULL
;
323 if (lp
->mode
== PWM_BASED
)
324 strmode
= "pwm based";
325 else if (lp
->mode
== REGISTER_BASED
)
326 strmode
= "register based";
328 return scnprintf(buf
, PAGE_SIZE
, "%s\n", strmode
);
331 static DEVICE_ATTR(chip_id
, S_IRUGO
, lp855x_get_chip_id
, NULL
);
332 static DEVICE_ATTR(bl_ctl_mode
, S_IRUGO
, lp855x_get_bl_ctl_mode
, NULL
);
334 static struct attribute
*lp855x_attributes
[] = {
335 &dev_attr_chip_id
.attr
,
336 &dev_attr_bl_ctl_mode
.attr
,
340 static const struct attribute_group lp855x_attr_group
= {
341 .attrs
= lp855x_attributes
,
345 static int lp855x_parse_dt(struct lp855x
*lp
)
347 struct device
*dev
= lp
->dev
;
348 struct device_node
*node
= dev
->of_node
;
349 struct lp855x_platform_data
*pdata
;
353 dev_err(dev
, "no platform data\n");
357 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
361 of_property_read_string(node
, "bl-name", &pdata
->name
);
362 of_property_read_u8(node
, "dev-ctrl", &pdata
->device_control
);
363 of_property_read_u8(node
, "init-brt", &pdata
->initial_brightness
);
364 of_property_read_u32(node
, "pwm-period", &pdata
->period_ns
);
366 /* Fill ROM platform data if defined */
367 rom_length
= of_get_child_count(node
);
368 if (rom_length
> 0) {
369 struct lp855x_rom_data
*rom
;
370 struct device_node
*child
;
373 rom
= devm_kcalloc(dev
, rom_length
, sizeof(*rom
), GFP_KERNEL
);
377 for_each_child_of_node(node
, child
) {
378 of_property_read_u8(child
, "rom-addr", &rom
[i
].addr
);
379 of_property_read_u8(child
, "rom-val", &rom
[i
].val
);
383 pdata
->size_program
= rom_length
;
384 pdata
->rom_data
= &rom
[0];
392 static int lp855x_parse_dt(struct lp855x
*lp
)
398 static int lp855x_probe(struct i2c_client
*cl
, const struct i2c_device_id
*id
)
403 if (!i2c_check_functionality(cl
->adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
406 lp
= devm_kzalloc(&cl
->dev
, sizeof(struct lp855x
), GFP_KERNEL
);
412 lp
->chipname
= id
->name
;
413 lp
->chip_id
= id
->driver_data
;
414 lp
->pdata
= dev_get_platdata(&cl
->dev
);
417 ret
= lp855x_parse_dt(lp
);
422 if (lp
->pdata
->period_ns
> 0)
423 lp
->mode
= PWM_BASED
;
425 lp
->mode
= REGISTER_BASED
;
427 lp
->supply
= devm_regulator_get(lp
->dev
, "power");
428 if (IS_ERR(lp
->supply
)) {
429 if (PTR_ERR(lp
->supply
) == -EPROBE_DEFER
)
430 return -EPROBE_DEFER
;
434 lp
->enable
= devm_regulator_get_optional(lp
->dev
, "enable");
435 if (IS_ERR(lp
->enable
)) {
436 ret
= PTR_ERR(lp
->enable
);
437 if (ret
== -ENODEV
) {
440 if (ret
!= -EPROBE_DEFER
)
441 dev_err(lp
->dev
, "error getting enable regulator: %d\n",
448 ret
= regulator_enable(lp
->supply
);
450 dev_err(&cl
->dev
, "failed to enable supply: %d\n", ret
);
456 ret
= regulator_enable(lp
->enable
);
458 dev_err(lp
->dev
, "failed to enable vddio: %d\n", ret
);
463 * LP8555 datasheet says t_RESPONSE (time between VDDIO and
466 usleep_range(1000, 2000);
469 i2c_set_clientdata(cl
, lp
);
471 ret
= lp855x_configure(lp
);
473 dev_err(lp
->dev
, "device config err: %d", ret
);
477 ret
= lp855x_backlight_register(lp
);
480 "failed to register backlight. err: %d\n", ret
);
484 ret
= sysfs_create_group(&lp
->dev
->kobj
, &lp855x_attr_group
);
486 dev_err(lp
->dev
, "failed to register sysfs. err: %d\n", ret
);
490 backlight_update_status(lp
->bl
);
494 static int lp855x_remove(struct i2c_client
*cl
)
496 struct lp855x
*lp
= i2c_get_clientdata(cl
);
498 lp
->bl
->props
.brightness
= 0;
499 backlight_update_status(lp
->bl
);
501 regulator_disable(lp
->supply
);
502 sysfs_remove_group(&lp
->dev
->kobj
, &lp855x_attr_group
);
507 static const struct of_device_id lp855x_dt_ids
[] = {
508 { .compatible
= "ti,lp8550", },
509 { .compatible
= "ti,lp8551", },
510 { .compatible
= "ti,lp8552", },
511 { .compatible
= "ti,lp8553", },
512 { .compatible
= "ti,lp8555", },
513 { .compatible
= "ti,lp8556", },
514 { .compatible
= "ti,lp8557", },
517 MODULE_DEVICE_TABLE(of
, lp855x_dt_ids
);
519 static const struct i2c_device_id lp855x_ids
[] = {
529 MODULE_DEVICE_TABLE(i2c
, lp855x_ids
);
531 static struct i2c_driver lp855x_driver
= {
534 .of_match_table
= of_match_ptr(lp855x_dt_ids
),
536 .probe
= lp855x_probe
,
537 .remove
= lp855x_remove
,
538 .id_table
= lp855x_ids
,
541 module_i2c_driver(lp855x_driver
);
543 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
544 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
545 MODULE_LICENSE("GPL");