xref: /linux/drivers/video/backlight/lp855x_bl.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI LP855x Backlight Driver
4  *
5  *			Copyright (C) 2011 Texas Instruments
6  */
7 
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>
15 #include <linux/of.h>
16 #include <linux/platform_data/lp855x.h>
17 #include <linux/pwm.h>
18 #include <linux/regulator/consumer.h>
19 
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
27 
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
39 
40 #define DEFAULT_BL_NAME		"lcd-backlight"
41 #define MAX_BRIGHTNESS		255
42 
43 enum lp855x_brightness_ctrl_mode {
44 	PWM_BASED = 1,
45 	REGISTER_BASED,
46 };
47 
48 struct lp855x;
49 
50 /*
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
56  */
57 struct lp855x_device_config {
58 	int (*pre_init_device)(struct lp855x *);
59 	u8 reg_brightness;
60 	u8 reg_devicectrl;
61 	int (*post_init_device)(struct lp855x *);
62 };
63 
64 struct lp855x {
65 	const char *chipname;
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;
71 	struct device *dev;
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 */
76 };
77 
78 static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
79 {
80 	return i2c_smbus_write_byte_data(lp->client, reg, data);
81 }
82 
83 static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data)
84 {
85 	int ret;
86 	u8 tmp;
87 
88 	ret = i2c_smbus_read_byte_data(lp->client, reg);
89 	if (ret < 0) {
90 		dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
91 		return ret;
92 	}
93 
94 	tmp = (u8)ret;
95 	tmp &= ~mask;
96 	tmp |= data & mask;
97 
98 	return lp855x_write_byte(lp, reg, tmp);
99 }
100 
101 static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
102 {
103 	u8 start, end;
104 
105 	switch (lp->chip_id) {
106 	case LP8550:
107 	case LP8551:
108 	case LP8552:
109 	case LP8553:
110 		start = LP855X_EEPROM_START;
111 		end = LP855X_EEPROM_END;
112 		break;
113 	case LP8556:
114 		start = LP8556_EPROM_START;
115 		end = LP8556_EPROM_END;
116 		break;
117 	case LP8555:
118 		start = LP8555_EPROM_START;
119 		end = LP8555_EPROM_END;
120 		break;
121 	case LP8557:
122 		start = LP8557_EPROM_START;
123 		end = LP8557_EPROM_END;
124 		break;
125 	default:
126 		return false;
127 	}
128 
129 	return addr >= start && addr <= end;
130 }
131 
132 static int lp8557_bl_off(struct lp855x *lp)
133 {
134 	/* BL_ON = 0 before updating EPROM settings */
135 	return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
136 				LP8557_BL_OFF);
137 }
138 
139 static int lp8557_bl_on(struct lp855x *lp)
140 {
141 	/* BL_ON = 1 after updating EPROM settings */
142 	return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK,
143 				LP8557_BL_ON);
144 }
145 
146 static struct lp855x_device_config lp855x_dev_cfg = {
147 	.reg_brightness = LP855X_BRIGHTNESS_CTRL,
148 	.reg_devicectrl = LP855X_DEVICE_CTRL,
149 };
150 
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,
156 };
157 
158 /*
159  * Device specific configuration flow
160  *
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)
166  *
167  */
168 static int lp855x_configure(struct lp855x *lp)
169 {
170 	u8 val, addr;
171 	int i, ret;
172 	struct lp855x_platform_data *pd = lp->pdata;
173 
174 	if (lp->cfg->pre_init_device) {
175 		ret = lp->cfg->pre_init_device(lp);
176 		if (ret) {
177 			dev_err(lp->dev, "pre init device err: %d\n", ret);
178 			goto err;
179 		}
180 	}
181 
182 	val = pd->initial_brightness;
183 	ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
184 	if (ret)
185 		goto err;
186 
187 	val = pd->device_control;
188 	ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
189 	if (ret)
190 		goto err;
191 
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))
197 				continue;
198 
199 			ret = lp855x_write_byte(lp, addr, val);
200 			if (ret)
201 				goto err;
202 		}
203 	}
204 
205 	if (lp->cfg->post_init_device) {
206 		ret = lp->cfg->post_init_device(lp);
207 		if (ret) {
208 			dev_err(lp->dev, "post init device err: %d\n", ret);
209 			goto err;
210 		}
211 	}
212 
213 	return 0;
214 
215 err:
216 	return ret;
217 }
218 
219 static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
220 {
221 	struct pwm_device *pwm;
222 	struct pwm_state state;
223 
224 	/* request pwm device with the consumer name */
225 	if (!lp->pwm) {
226 		pwm = devm_pwm_get(lp->dev, lp->chipname);
227 		if (IS_ERR(pwm))
228 			return;
229 
230 		lp->pwm = pwm;
231 
232 		pwm_init_state(lp->pwm, &state);
233 	} else {
234 		pwm_get_state(lp->pwm, &state);
235 	}
236 
237 	state.period = lp->pdata->period_ns;
238 	state.duty_cycle = div_u64(br * state.period, max_br);
239 	state.enabled = state.duty_cycle;
240 
241 	pwm_apply_state(lp->pwm, &state);
242 }
243 
244 static int lp855x_bl_update_status(struct backlight_device *bl)
245 {
246 	struct lp855x *lp = bl_get_data(bl);
247 	int brightness = bl->props.brightness;
248 
249 	if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
250 		brightness = 0;
251 
252 	if (lp->mode == PWM_BASED)
253 		lp855x_pwm_ctrl(lp, brightness, bl->props.max_brightness);
254 	else if (lp->mode == REGISTER_BASED)
255 		lp855x_write_byte(lp, lp->cfg->reg_brightness, (u8)brightness);
256 
257 	return 0;
258 }
259 
260 static const struct backlight_ops lp855x_bl_ops = {
261 	.options = BL_CORE_SUSPENDRESUME,
262 	.update_status = lp855x_bl_update_status,
263 };
264 
265 static int lp855x_backlight_register(struct lp855x *lp)
266 {
267 	struct backlight_device *bl;
268 	struct backlight_properties props;
269 	struct lp855x_platform_data *pdata = lp->pdata;
270 	const char *name = pdata->name ? : DEFAULT_BL_NAME;
271 
272 	memset(&props, 0, sizeof(props));
273 	props.type = BACKLIGHT_PLATFORM;
274 	props.max_brightness = MAX_BRIGHTNESS;
275 
276 	if (pdata->initial_brightness > props.max_brightness)
277 		pdata->initial_brightness = props.max_brightness;
278 
279 	props.brightness = pdata->initial_brightness;
280 
281 	bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
282 				       &lp855x_bl_ops, &props);
283 	if (IS_ERR(bl))
284 		return PTR_ERR(bl);
285 
286 	lp->bl = bl;
287 
288 	return 0;
289 }
290 
291 static ssize_t lp855x_get_chip_id(struct device *dev,
292 				struct device_attribute *attr, char *buf)
293 {
294 	struct lp855x *lp = dev_get_drvdata(dev);
295 
296 	return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
297 }
298 
299 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
300 				     struct device_attribute *attr, char *buf)
301 {
302 	struct lp855x *lp = dev_get_drvdata(dev);
303 	char *strmode = NULL;
304 
305 	if (lp->mode == PWM_BASED)
306 		strmode = "pwm based";
307 	else if (lp->mode == REGISTER_BASED)
308 		strmode = "register based";
309 
310 	return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
311 }
312 
313 static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
314 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
315 
316 static struct attribute *lp855x_attributes[] = {
317 	&dev_attr_chip_id.attr,
318 	&dev_attr_bl_ctl_mode.attr,
319 	NULL,
320 };
321 
322 static const struct attribute_group lp855x_attr_group = {
323 	.attrs = lp855x_attributes,
324 };
325 
326 #ifdef CONFIG_OF
327 static int lp855x_parse_dt(struct lp855x *lp)
328 {
329 	struct device *dev = lp->dev;
330 	struct device_node *node = dev->of_node;
331 	struct lp855x_platform_data *pdata = lp->pdata;
332 	int rom_length;
333 
334 	if (!node) {
335 		dev_err(dev, "no platform data\n");
336 		return -EINVAL;
337 	}
338 
339 	of_property_read_string(node, "bl-name", &pdata->name);
340 	of_property_read_u8(node, "dev-ctrl", &pdata->device_control);
341 	of_property_read_u8(node, "init-brt", &pdata->initial_brightness);
342 	of_property_read_u32(node, "pwm-period", &pdata->period_ns);
343 
344 	/* Fill ROM platform data if defined */
345 	rom_length = of_get_child_count(node);
346 	if (rom_length > 0) {
347 		struct lp855x_rom_data *rom;
348 		struct device_node *child;
349 		int i = 0;
350 
351 		rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL);
352 		if (!rom)
353 			return -ENOMEM;
354 
355 		for_each_child_of_node(node, child) {
356 			of_property_read_u8(child, "rom-addr", &rom[i].addr);
357 			of_property_read_u8(child, "rom-val", &rom[i].val);
358 			i++;
359 		}
360 
361 		pdata->size_program = rom_length;
362 		pdata->rom_data = &rom[0];
363 	}
364 
365 	return 0;
366 }
367 #else
368 static int lp855x_parse_dt(struct lp855x *lp)
369 {
370 	return -EINVAL;
371 }
372 #endif
373 
374 static int lp855x_parse_acpi(struct lp855x *lp)
375 {
376 	int ret;
377 
378 	/*
379 	 * On ACPI the device has already been initialized by the firmware
380 	 * and is in register mode, so we can read back the settings from
381 	 * the registers.
382 	 */
383 	ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_brightness);
384 	if (ret < 0)
385 		return ret;
386 
387 	lp->pdata->initial_brightness = ret;
388 
389 	ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_devicectrl);
390 	if (ret < 0)
391 		return ret;
392 
393 	lp->pdata->device_control = ret;
394 	return 0;
395 }
396 
397 static int lp855x_probe(struct i2c_client *cl)
398 {
399 	const struct i2c_device_id *id = i2c_client_get_device_id(cl);
400 	const struct acpi_device_id *acpi_id = NULL;
401 	struct device *dev = &cl->dev;
402 	struct lp855x *lp;
403 	int ret;
404 
405 	if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
406 		return -EIO;
407 
408 	lp = devm_kzalloc(dev, sizeof(struct lp855x), GFP_KERNEL);
409 	if (!lp)
410 		return -ENOMEM;
411 
412 	lp->client = cl;
413 	lp->dev = dev;
414 	lp->pdata = dev_get_platdata(dev);
415 
416 	if (id) {
417 		lp->chipname = id->name;
418 		lp->chip_id = id->driver_data;
419 	} else {
420 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
421 		if (!acpi_id)
422 			return -ENODEV;
423 
424 		lp->chipname = acpi_id->id;
425 		lp->chip_id = acpi_id->driver_data;
426 	}
427 
428 	switch (lp->chip_id) {
429 	case LP8550:
430 	case LP8551:
431 	case LP8552:
432 	case LP8553:
433 	case LP8556:
434 		lp->cfg = &lp855x_dev_cfg;
435 		break;
436 	case LP8555:
437 	case LP8557:
438 		lp->cfg = &lp8557_dev_cfg;
439 		break;
440 	default:
441 		return -EINVAL;
442 	}
443 
444 	if (!lp->pdata) {
445 		lp->pdata = devm_kzalloc(dev, sizeof(*lp->pdata), GFP_KERNEL);
446 		if (!lp->pdata)
447 			return -ENOMEM;
448 
449 		if (id) {
450 			ret = lp855x_parse_dt(lp);
451 			if (ret < 0)
452 				return ret;
453 		} else {
454 			ret = lp855x_parse_acpi(lp);
455 			if (ret < 0)
456 				return ret;
457 		}
458 	}
459 
460 	if (lp->pdata->period_ns > 0)
461 		lp->mode = PWM_BASED;
462 	else
463 		lp->mode = REGISTER_BASED;
464 
465 	lp->supply = devm_regulator_get(dev, "power");
466 	if (IS_ERR(lp->supply)) {
467 		if (PTR_ERR(lp->supply) == -EPROBE_DEFER)
468 			return -EPROBE_DEFER;
469 		lp->supply = NULL;
470 	}
471 
472 	lp->enable = devm_regulator_get_optional(dev, "enable");
473 	if (IS_ERR(lp->enable)) {
474 		ret = PTR_ERR(lp->enable);
475 		if (ret == -ENODEV) {
476 			lp->enable = NULL;
477 		} else {
478 			return dev_err_probe(dev, ret, "getting enable regulator\n");
479 		}
480 	}
481 
482 	if (lp->supply) {
483 		ret = regulator_enable(lp->supply);
484 		if (ret < 0) {
485 			dev_err(dev, "failed to enable supply: %d\n", ret);
486 			return ret;
487 		}
488 	}
489 
490 	if (lp->enable) {
491 		ret = regulator_enable(lp->enable);
492 		if (ret < 0) {
493 			dev_err(dev, "failed to enable vddio: %d\n", ret);
494 			goto disable_supply;
495 		}
496 
497 		/*
498 		 * LP8555 datasheet says t_RESPONSE (time between VDDIO and
499 		 * I2C) is 1ms.
500 		 */
501 		usleep_range(1000, 2000);
502 	}
503 
504 	i2c_set_clientdata(cl, lp);
505 
506 	ret = lp855x_configure(lp);
507 	if (ret) {
508 		dev_err(dev, "device config err: %d", ret);
509 		goto disable_vddio;
510 	}
511 
512 	ret = lp855x_backlight_register(lp);
513 	if (ret) {
514 		dev_err(dev, "failed to register backlight. err: %d\n", ret);
515 		goto disable_vddio;
516 	}
517 
518 	ret = sysfs_create_group(&dev->kobj, &lp855x_attr_group);
519 	if (ret) {
520 		dev_err(dev, "failed to register sysfs. err: %d\n", ret);
521 		goto disable_vddio;
522 	}
523 
524 	backlight_update_status(lp->bl);
525 
526 	return 0;
527 
528 disable_vddio:
529 	if (lp->enable)
530 		regulator_disable(lp->enable);
531 disable_supply:
532 	if (lp->supply)
533 		regulator_disable(lp->supply);
534 
535 	return ret;
536 }
537 
538 static void lp855x_remove(struct i2c_client *cl)
539 {
540 	struct lp855x *lp = i2c_get_clientdata(cl);
541 
542 	lp->bl->props.brightness = 0;
543 	backlight_update_status(lp->bl);
544 	if (lp->enable)
545 		regulator_disable(lp->enable);
546 	if (lp->supply)
547 		regulator_disable(lp->supply);
548 	sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
549 }
550 
551 static const struct of_device_id lp855x_dt_ids[] __maybe_unused = {
552 	{ .compatible = "ti,lp8550", },
553 	{ .compatible = "ti,lp8551", },
554 	{ .compatible = "ti,lp8552", },
555 	{ .compatible = "ti,lp8553", },
556 	{ .compatible = "ti,lp8555", },
557 	{ .compatible = "ti,lp8556", },
558 	{ .compatible = "ti,lp8557", },
559 	{ }
560 };
561 MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
562 
563 static const struct i2c_device_id lp855x_ids[] = {
564 	{"lp8550", LP8550},
565 	{"lp8551", LP8551},
566 	{"lp8552", LP8552},
567 	{"lp8553", LP8553},
568 	{"lp8555", LP8555},
569 	{"lp8556", LP8556},
570 	{"lp8557", LP8557},
571 	{ }
572 };
573 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
574 
575 #ifdef CONFIG_ACPI
576 static const struct acpi_device_id lp855x_acpi_match[] = {
577 	/* Xiaomi specific HID used for the LP8556 on the Mi Pad 2 */
578 	{ "XMCC0001", LP8556 },
579 	{ }
580 };
581 MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match);
582 #endif
583 
584 static struct i2c_driver lp855x_driver = {
585 	.driver = {
586 		   .name = "lp855x",
587 		   .of_match_table = of_match_ptr(lp855x_dt_ids),
588 		   .acpi_match_table = ACPI_PTR(lp855x_acpi_match),
589 		   },
590 	.probe_new = lp855x_probe,
591 	.remove = lp855x_remove,
592 	.id_table = lp855x_ids,
593 };
594 
595 module_i2c_driver(lp855x_driver);
596 
597 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
598 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
599 MODULE_LICENSE("GPL");
600