1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP855x Backlight Driver
5 * Copyright (C) 2011 Texas Instruments
8 #include <linux/acpi.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/backlight.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
16 #include <linux/platform_data/lp855x.h>
17 #include <linux/pwm.h>
18 #include <linux/regulator/consumer.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 /* LP8555/7 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 LP8555_EPROM_START 0x10
36 #define LP8555_EPROM_END 0x7A
37 #define LP8557_EPROM_START 0x10
38 #define LP8557_EPROM_END 0x1E
40 #define DEFAULT_BL_NAME "lcd-backlight"
41 #define MAX_BRIGHTNESS 255
43 enum lp855x_brightness_ctrl_mode {
51 * struct lp855x_device_config
52 * @pre_init_device: init device function call before updating the brightness
53 * @reg_brightness: register address for brigthenss control
54 * @reg_devicectrl: register address for device control
55 * @post_init_device: late init device function call
57 struct lp855x_device_config {
58 int (*pre_init_device)(struct lp855x *);
61 int (*post_init_device)(struct lp855x *);
66 enum lp855x_chip_id chip_id;
67 enum lp855x_brightness_ctrl_mode mode;
68 struct lp855x_device_config *cfg;
69 struct i2c_client *client;
70 struct backlight_device *bl;
72 struct lp855x_platform_data *pdata;
73 struct pwm_device *pwm;
74 struct regulator *supply; /* regulator for VDD input */
75 struct regulator *enable; /* regulator for EN/VDDIO input */
78 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
80 return i2c_smbus_write_byte_data(lp->client, reg, data);
83 static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
88 ret = i2c_smbus_read_byte_data(lp->client, reg);
90 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
98 return lp855x_write_byte(lp, reg, tmp);
101 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
105 switch (lp->chip_id) {
110 start = LP855X_EEPROM_START;
111 end = LP855X_EEPROM_END;
114 start = LP8556_EPROM_START;
115 end = LP8556_EPROM_END;
118 start = LP8555_EPROM_START;
119 end = LP8555_EPROM_END;
122 start = LP8557_EPROM_START;
123 end = LP8557_EPROM_END;
129 return addr >= start && addr <= end;
132 static int lp8557_bl_off(struct lp855x *lp)
134 /* BL_ON = 0 before updating EPROM settings */
135 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
139 static int lp8557_bl_on(struct lp855x *lp)
141 /* BL_ON = 1 after updating EPROM settings */
142 return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
146 static struct lp855x_device_config lp855x_dev_cfg = {
147 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
148 .reg_devicectrl = LP855X_DEVICE_CTRL,
151 static struct lp855x_device_config lp8557_dev_cfg = {
152 .reg_brightness = LP8557_BRIGHTNESS_CTRL,
153 .reg_devicectrl = LP8557_CONFIG,
154 .pre_init_device = lp8557_bl_off,
155 .post_init_device = lp8557_bl_on,
159 * Device specific configuration flow
161 * a) pre_init_device(optional)
162 * b) update the brightness register
163 * c) update device control register
164 * d) update ROM area(optional)
165 * e) post_init_device(optional)
168 static int lp855x_configure(struct lp855x *lp)
172 struct lp855x_platform_data *pd = lp->pdata;
174 if (lp->cfg->pre_init_device) {
175 ret = lp->cfg->pre_init_device(lp);
177 dev_err(lp->dev, "pre init device err: %d\n", ret);
182 val = pd->initial_brightness;
183 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
187 val = pd->device_control;
188 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
192 if (pd->size_program > 0) {
193 for (i = 0; i < pd->size_program; i++) {
194 addr = pd->rom_data[i].addr;
195 val = pd->rom_data[i].val;
196 if (!lp855x_is_valid_rom_area(lp, addr))
199 ret = lp855x_write_byte(lp, addr, val);
205 if (lp->cfg->post_init_device) {
206 ret = lp->cfg->post_init_device(lp);
208 dev_err(lp->dev, "post init device err: %d\n", ret);
219 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
221 struct pwm_state state;
223 pwm_get_state(lp->pwm, &state);
225 state.duty_cycle = div_u64(br * state.period, max_br);
226 state.enabled = state.duty_cycle;
228 pwm_apply_state(lp->pwm, &state);
231 static int lp855x_bl_update_status(struct backlight_device *bl)
233 struct lp855x *lp = bl_get_data(bl);
234 int brightness = bl->props.brightness;
236 if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
239 if (lp->mode == PWM_BASED)
240 lp855x_pwm_ctrl(lp, brightness, bl->props.max_brightness);
241 else if (lp->mode == REGISTER_BASED)
242 lp855x_write_byte(lp, lp->cfg->reg_brightness, (u8)brightness);
247 static const struct backlight_ops lp855x_bl_ops = {
248 .options = BL_CORE_SUSPENDRESUME,
249 .update_status = lp855x_bl_update_status,
252 static int lp855x_backlight_register(struct lp855x *lp)
254 struct backlight_device *bl;
255 struct backlight_properties props;
256 struct lp855x_platform_data *pdata = lp->pdata;
257 const char *name = pdata->name ? : DEFAULT_BL_NAME;
259 memset(&props, 0, sizeof(props));
260 props.type = BACKLIGHT_PLATFORM;
261 props.max_brightness = MAX_BRIGHTNESS;
263 if (pdata->initial_brightness > props.max_brightness)
264 pdata->initial_brightness = props.max_brightness;
266 props.brightness = pdata->initial_brightness;
268 bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
269 &lp855x_bl_ops, &props);
278 static ssize_t lp855x_get_chip_id(struct device *dev,
279 struct device_attribute *attr, char *buf)
281 struct lp855x *lp = dev_get_drvdata(dev);
283 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
286 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct lp855x *lp = dev_get_drvdata(dev);
290 char *strmode = NULL;
292 if (lp->mode == PWM_BASED)
293 strmode = "pwm based";
294 else if (lp->mode == REGISTER_BASED)
295 strmode = "register based";
297 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
300 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
301 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
303 static struct attribute *lp855x_attributes[] = {
304 &dev_attr_chip_id.attr,
305 &dev_attr_bl_ctl_mode.attr,
309 static const struct attribute_group lp855x_attr_group = {
310 .attrs = lp855x_attributes,
314 static int lp855x_parse_dt(struct lp855x *lp)
316 struct device *dev = lp->dev;
317 struct device_node *node = dev->of_node;
318 struct lp855x_platform_data *pdata = lp->pdata;
322 dev_err(dev, "no platform data\n");
326 of_property_read_string(node, "bl-name", &pdata->name);
327 of_property_read_u8(node, "dev-ctrl", &pdata->device_control);
328 of_property_read_u8(node, "init-brt", &pdata->initial_brightness);
329 /* Deprecated, specify period in pwms property instead */
330 of_property_read_u32(node, "pwm-period", &pdata->period_ns);
332 /* Fill ROM platform data if defined */
333 rom_length = of_get_child_count(node);
334 if (rom_length > 0) {
335 struct lp855x_rom_data *rom;
336 struct device_node *child;
339 rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL);
343 for_each_child_of_node(node, child) {
344 of_property_read_u8(child, "rom-addr", &rom[i].addr);
345 of_property_read_u8(child, "rom-val", &rom[i].val);
349 pdata->size_program = rom_length;
350 pdata->rom_data = &rom[0];
356 static int lp855x_parse_dt(struct lp855x *lp)
362 static int lp855x_parse_acpi(struct lp855x *lp)
367 * On ACPI the device has already been initialized by the firmware
368 * and is in register mode, so we can read back the settings from
371 ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_brightness);
375 lp->pdata->initial_brightness = ret;
377 ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_devicectrl);
381 lp->pdata->device_control = ret;
385 static int lp855x_probe(struct i2c_client *cl)
387 const struct i2c_device_id *id = i2c_client_get_device_id(cl);
388 const struct acpi_device_id *acpi_id = NULL;
389 struct device *dev = &cl->dev;
390 struct pwm_state pwmstate;
394 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
397 lp = devm_kzalloc(dev, sizeof(struct lp855x), GFP_KERNEL);
403 lp->pdata = dev_get_platdata(dev);
406 lp->chipname = id->name;
407 lp->chip_id = id->driver_data;
409 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
413 lp->chipname = acpi_id->id;
414 lp->chip_id = acpi_id->driver_data;
417 switch (lp->chip_id) {
423 lp->cfg = &lp855x_dev_cfg;
427 lp->cfg = &lp8557_dev_cfg;
434 lp->pdata = devm_kzalloc(dev, sizeof(*lp->pdata), GFP_KERNEL);
439 ret = lp855x_parse_dt(lp);
443 ret = lp855x_parse_acpi(lp);
449 lp->supply = devm_regulator_get(dev, "power");
450 if (IS_ERR(lp->supply)) {
451 if (PTR_ERR(lp->supply) == -EPROBE_DEFER)
452 return -EPROBE_DEFER;
456 lp->enable = devm_regulator_get_optional(dev, "enable");
457 if (IS_ERR(lp->enable)) {
458 ret = PTR_ERR(lp->enable);
462 return dev_err_probe(dev, ret, "getting enable regulator\n");
465 lp->pwm = devm_pwm_get(lp->dev, lp->chipname);
466 if (IS_ERR(lp->pwm)) {
467 ret = PTR_ERR(lp->pwm);
468 if (ret == -ENODEV || ret == -EINVAL)
471 return dev_err_probe(dev, ret, "getting PWM\n");
473 lp->mode = REGISTER_BASED;
474 dev_dbg(dev, "mode: register based\n");
476 pwm_init_state(lp->pwm, &pwmstate);
477 /* Legacy platform data compatibility */
478 if (lp->pdata->period_ns > 0)
479 pwmstate.period = lp->pdata->period_ns;
480 pwm_apply_state(lp->pwm, &pwmstate);
482 lp->mode = PWM_BASED;
483 dev_dbg(dev, "mode: PWM based\n");
487 ret = regulator_enable(lp->supply);
489 dev_err(dev, "failed to enable supply: %d\n", ret);
495 ret = regulator_enable(lp->enable);
497 dev_err(dev, "failed to enable vddio: %d\n", ret);
502 * LP8555 datasheet says t_RESPONSE (time between VDDIO and
505 usleep_range(1000, 2000);
508 i2c_set_clientdata(cl, lp);
510 ret = lp855x_configure(lp);
512 dev_err(dev, "device config err: %d", ret);
516 ret = lp855x_backlight_register(lp);
518 dev_err(dev, "failed to register backlight. err: %d\n", ret);
522 ret = sysfs_create_group(&dev->kobj, &lp855x_attr_group);
524 dev_err(dev, "failed to register sysfs. err: %d\n", ret);
528 backlight_update_status(lp->bl);
534 regulator_disable(lp->enable);
537 regulator_disable(lp->supply);
542 static void lp855x_remove(struct i2c_client *cl)
544 struct lp855x *lp = i2c_get_clientdata(cl);
546 lp->bl->props.brightness = 0;
547 backlight_update_status(lp->bl);
549 regulator_disable(lp->enable);
551 regulator_disable(lp->supply);
552 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
555 static const struct of_device_id lp855x_dt_ids[] __maybe_unused = {
556 { .compatible = "ti,lp8550", },
557 { .compatible = "ti,lp8551", },
558 { .compatible = "ti,lp8552", },
559 { .compatible = "ti,lp8553", },
560 { .compatible = "ti,lp8555", },
561 { .compatible = "ti,lp8556", },
562 { .compatible = "ti,lp8557", },
565 MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
567 static const struct i2c_device_id lp855x_ids[] = {
577 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
580 static const struct acpi_device_id lp855x_acpi_match[] = {
581 /* Xiaomi specific HID used for the LP8556 on the Mi Pad 2 */
582 { "XMCC0001", LP8556 },
585 MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match);
588 static struct i2c_driver lp855x_driver = {
591 .of_match_table = of_match_ptr(lp855x_dt_ids),
592 .acpi_match_table = ACPI_PTR(lp855x_acpi_match),
594 .probe = lp855x_probe,
595 .remove = lp855x_remove,
596 .id_table = lp855x_ids,
599 module_i2c_driver(lp855x_driver);
601 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
602 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
603 MODULE_LICENSE("GPL");