xref: /linux/drivers/video/backlight/lp855x_bl.c (revision 0e2b2a76278153d1ac312b0691cb65dabb9aef3e)
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_state state;
222 
223 	pwm_get_state(lp->pwm, &state);
224 
225 	state.duty_cycle = div_u64(br * state.period, max_br);
226 	state.enabled = state.duty_cycle;
227 
228 	pwm_apply_state(lp->pwm, &state);
229 }
230 
231 static int lp855x_bl_update_status(struct backlight_device *bl)
232 {
233 	struct lp855x *lp = bl_get_data(bl);
234 	int brightness = bl->props.brightness;
235 
236 	if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
237 		brightness = 0;
238 
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);
243 
244 	return 0;
245 }
246 
247 static const struct backlight_ops lp855x_bl_ops = {
248 	.options = BL_CORE_SUSPENDRESUME,
249 	.update_status = lp855x_bl_update_status,
250 };
251 
252 static int lp855x_backlight_register(struct lp855x *lp)
253 {
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;
258 
259 	memset(&props, 0, sizeof(props));
260 	props.type = BACKLIGHT_PLATFORM;
261 	props.max_brightness = MAX_BRIGHTNESS;
262 
263 	if (pdata->initial_brightness > props.max_brightness)
264 		pdata->initial_brightness = props.max_brightness;
265 
266 	props.brightness = pdata->initial_brightness;
267 
268 	bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
269 				       &lp855x_bl_ops, &props);
270 	if (IS_ERR(bl))
271 		return PTR_ERR(bl);
272 
273 	lp->bl = bl;
274 
275 	return 0;
276 }
277 
278 static ssize_t lp855x_get_chip_id(struct device *dev,
279 				struct device_attribute *attr, char *buf)
280 {
281 	struct lp855x *lp = dev_get_drvdata(dev);
282 
283 	return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
284 }
285 
286 static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
287 				     struct device_attribute *attr, char *buf)
288 {
289 	struct lp855x *lp = dev_get_drvdata(dev);
290 	char *strmode = NULL;
291 
292 	if (lp->mode == PWM_BASED)
293 		strmode = "pwm based";
294 	else if (lp->mode == REGISTER_BASED)
295 		strmode = "register based";
296 
297 	return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
298 }
299 
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);
302 
303 static struct attribute *lp855x_attributes[] = {
304 	&dev_attr_chip_id.attr,
305 	&dev_attr_bl_ctl_mode.attr,
306 	NULL,
307 };
308 
309 static const struct attribute_group lp855x_attr_group = {
310 	.attrs = lp855x_attributes,
311 };
312 
313 #ifdef CONFIG_OF
314 static int lp855x_parse_dt(struct lp855x *lp)
315 {
316 	struct device *dev = lp->dev;
317 	struct device_node *node = dev->of_node;
318 	struct lp855x_platform_data *pdata = lp->pdata;
319 	int rom_length;
320 
321 	if (!node) {
322 		dev_err(dev, "no platform data\n");
323 		return -EINVAL;
324 	}
325 
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);
331 
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;
337 		int i = 0;
338 
339 		rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL);
340 		if (!rom)
341 			return -ENOMEM;
342 
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);
346 			i++;
347 		}
348 
349 		pdata->size_program = rom_length;
350 		pdata->rom_data = &rom[0];
351 	}
352 
353 	return 0;
354 }
355 #else
356 static int lp855x_parse_dt(struct lp855x *lp)
357 {
358 	return -EINVAL;
359 }
360 #endif
361 
362 static int lp855x_parse_acpi(struct lp855x *lp)
363 {
364 	int ret;
365 
366 	/*
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
369 	 * the registers.
370 	 */
371 	ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_brightness);
372 	if (ret < 0)
373 		return ret;
374 
375 	lp->pdata->initial_brightness = ret;
376 
377 	ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_devicectrl);
378 	if (ret < 0)
379 		return ret;
380 
381 	lp->pdata->device_control = ret;
382 	return 0;
383 }
384 
385 static int lp855x_probe(struct i2c_client *cl)
386 {
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;
391 	struct lp855x *lp;
392 	int ret;
393 
394 	if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
395 		return -EIO;
396 
397 	lp = devm_kzalloc(dev, sizeof(struct lp855x), GFP_KERNEL);
398 	if (!lp)
399 		return -ENOMEM;
400 
401 	lp->client = cl;
402 	lp->dev = dev;
403 	lp->pdata = dev_get_platdata(dev);
404 
405 	if (id) {
406 		lp->chipname = id->name;
407 		lp->chip_id = id->driver_data;
408 	} else {
409 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
410 		if (!acpi_id)
411 			return -ENODEV;
412 
413 		lp->chipname = acpi_id->id;
414 		lp->chip_id = acpi_id->driver_data;
415 	}
416 
417 	switch (lp->chip_id) {
418 	case LP8550:
419 	case LP8551:
420 	case LP8552:
421 	case LP8553:
422 	case LP8556:
423 		lp->cfg = &lp855x_dev_cfg;
424 		break;
425 	case LP8555:
426 	case LP8557:
427 		lp->cfg = &lp8557_dev_cfg;
428 		break;
429 	default:
430 		return -EINVAL;
431 	}
432 
433 	if (!lp->pdata) {
434 		lp->pdata = devm_kzalloc(dev, sizeof(*lp->pdata), GFP_KERNEL);
435 		if (!lp->pdata)
436 			return -ENOMEM;
437 
438 		if (id) {
439 			ret = lp855x_parse_dt(lp);
440 			if (ret < 0)
441 				return ret;
442 		} else {
443 			ret = lp855x_parse_acpi(lp);
444 			if (ret < 0)
445 				return ret;
446 		}
447 	}
448 
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;
453 		lp->supply = NULL;
454 	}
455 
456 	lp->enable = devm_regulator_get_optional(dev, "enable");
457 	if (IS_ERR(lp->enable)) {
458 		ret = PTR_ERR(lp->enable);
459 		if (ret == -ENODEV)
460 			lp->enable = NULL;
461 		else
462 			return dev_err_probe(dev, ret, "getting enable regulator\n");
463 	}
464 
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)
469 			lp->pwm = NULL;
470 		else
471 			return dev_err_probe(dev, ret, "getting PWM\n");
472 
473 		lp->mode = REGISTER_BASED;
474 		dev_dbg(dev, "mode: register based\n");
475 	} else {
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);
481 
482 		lp->mode = PWM_BASED;
483 		dev_dbg(dev, "mode: PWM based\n");
484 	}
485 
486 	if (lp->supply) {
487 		ret = regulator_enable(lp->supply);
488 		if (ret < 0) {
489 			dev_err(dev, "failed to enable supply: %d\n", ret);
490 			return ret;
491 		}
492 	}
493 
494 	if (lp->enable) {
495 		ret = regulator_enable(lp->enable);
496 		if (ret < 0) {
497 			dev_err(dev, "failed to enable vddio: %d\n", ret);
498 			goto disable_supply;
499 		}
500 
501 		/*
502 		 * LP8555 datasheet says t_RESPONSE (time between VDDIO and
503 		 * I2C) is 1ms.
504 		 */
505 		usleep_range(1000, 2000);
506 	}
507 
508 	i2c_set_clientdata(cl, lp);
509 
510 	ret = lp855x_configure(lp);
511 	if (ret) {
512 		dev_err(dev, "device config err: %d", ret);
513 		goto disable_vddio;
514 	}
515 
516 	ret = lp855x_backlight_register(lp);
517 	if (ret) {
518 		dev_err(dev, "failed to register backlight. err: %d\n", ret);
519 		goto disable_vddio;
520 	}
521 
522 	ret = sysfs_create_group(&dev->kobj, &lp855x_attr_group);
523 	if (ret) {
524 		dev_err(dev, "failed to register sysfs. err: %d\n", ret);
525 		goto disable_vddio;
526 	}
527 
528 	backlight_update_status(lp->bl);
529 
530 	return 0;
531 
532 disable_vddio:
533 	if (lp->enable)
534 		regulator_disable(lp->enable);
535 disable_supply:
536 	if (lp->supply)
537 		regulator_disable(lp->supply);
538 
539 	return ret;
540 }
541 
542 static void lp855x_remove(struct i2c_client *cl)
543 {
544 	struct lp855x *lp = i2c_get_clientdata(cl);
545 
546 	lp->bl->props.brightness = 0;
547 	backlight_update_status(lp->bl);
548 	if (lp->enable)
549 		regulator_disable(lp->enable);
550 	if (lp->supply)
551 		regulator_disable(lp->supply);
552 	sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
553 }
554 
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", },
563 	{ }
564 };
565 MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
566 
567 static const struct i2c_device_id lp855x_ids[] = {
568 	{"lp8550", LP8550},
569 	{"lp8551", LP8551},
570 	{"lp8552", LP8552},
571 	{"lp8553", LP8553},
572 	{"lp8555", LP8555},
573 	{"lp8556", LP8556},
574 	{"lp8557", LP8557},
575 	{ }
576 };
577 MODULE_DEVICE_TABLE(i2c, lp855x_ids);
578 
579 #ifdef CONFIG_ACPI
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 },
583 	{ }
584 };
585 MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match);
586 #endif
587 
588 static struct i2c_driver lp855x_driver = {
589 	.driver = {
590 		   .name = "lp855x",
591 		   .of_match_table = of_match_ptr(lp855x_dt_ids),
592 		   .acpi_match_table = ACPI_PTR(lp855x_acpi_match),
593 		   },
594 	.probe = lp855x_probe,
595 	.remove = lp855x_remove,
596 	.id_table = lp855x_ids,
597 };
598 
599 module_i2c_driver(lp855x_driver);
600 
601 MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
602 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
603 MODULE_LICENSE("GPL");
604