xref: /linux/drivers/gpu/drm/bridge/parade-ps8622.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Parade PS8622 eDP/LVDS bridge driver
4  *
5  * Copyright (C) 2014 Google, Inc.
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/pm.h>
16 #include <linux/regulator/consumer.h>
17 
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_bridge.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_print.h>
23 #include <drm/drm_probe_helper.h>
24 
25 /* Brightness scale on the Parade chip */
26 #define PS8622_MAX_BRIGHTNESS 0xff
27 
28 /* Timings taken from the version 1.7 datasheet for the PS8622/PS8625 */
29 #define PS8622_POWER_RISE_T1_MIN_US 10
30 #define PS8622_POWER_RISE_T1_MAX_US 10000
31 #define PS8622_RST_HIGH_T2_MIN_US 3000
32 #define PS8622_RST_HIGH_T2_MAX_US 30000
33 #define PS8622_PWMO_END_T12_MS 200
34 #define PS8622_POWER_FALL_T16_MAX_US 10000
35 #define PS8622_POWER_OFF_T17_MS 500
36 
37 #if ((PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US) > \
38 	(PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US))
39 #error "T2.min + T1.max must be less than T2.max + T1.min"
40 #endif
41 
42 struct ps8622_bridge {
43 	struct i2c_client *client;
44 	struct drm_bridge bridge;
45 	struct drm_bridge *panel_bridge;
46 	struct regulator *v12;
47 	struct backlight_device *bl;
48 
49 	struct gpio_desc *gpio_slp;
50 	struct gpio_desc *gpio_rst;
51 
52 	u32 max_lane_count;
53 	u32 lane_count;
54 
55 	bool enabled;
56 };
57 
58 static inline struct ps8622_bridge *
59 		bridge_to_ps8622(struct drm_bridge *bridge)
60 {
61 	return container_of(bridge, struct ps8622_bridge, bridge);
62 }
63 
64 static int ps8622_set(struct i2c_client *client, u8 page, u8 reg, u8 val)
65 {
66 	int ret;
67 	struct i2c_adapter *adap = client->adapter;
68 	struct i2c_msg msg;
69 	u8 data[] = {reg, val};
70 
71 	msg.addr = client->addr + page;
72 	msg.flags = 0;
73 	msg.len = sizeof(data);
74 	msg.buf = data;
75 
76 	ret = i2c_transfer(adap, &msg, 1);
77 	if (ret != 1)
78 		pr_warn("PS8622 I2C write (0x%02x,0x%02x,0x%02x) failed: %d\n",
79 			client->addr + page, reg, val, ret);
80 	return !(ret == 1);
81 }
82 
83 static int ps8622_send_config(struct ps8622_bridge *ps8622)
84 {
85 	struct i2c_client *cl = ps8622->client;
86 	int err = 0;
87 
88 	/* HPD low */
89 	err = ps8622_set(cl, 0x02, 0xa1, 0x01);
90 	if (err)
91 		goto error;
92 
93 	/* SW setting: [1:0] SW output 1.2V voltage is lower to 96% */
94 	err = ps8622_set(cl, 0x04, 0x14, 0x01);
95 	if (err)
96 		goto error;
97 
98 	/* RCO SS setting: [5:4] = b01 0.5%, b10 1%, b11 1.5% */
99 	err = ps8622_set(cl, 0x04, 0xe3, 0x20);
100 	if (err)
101 		goto error;
102 
103 	/* [7] RCO SS enable */
104 	err = ps8622_set(cl, 0x04, 0xe2, 0x80);
105 	if (err)
106 		goto error;
107 
108 	/* RPHY Setting
109 	 * [3:2] CDR tune wait cycle before measure for fine tune
110 	 * b00: 1us b01: 0.5us b10:2us, b11: 4us
111 	 */
112 	err = ps8622_set(cl, 0x04, 0x8a, 0x0c);
113 	if (err)
114 		goto error;
115 
116 	/* [3] RFD always on */
117 	err = ps8622_set(cl, 0x04, 0x89, 0x08);
118 	if (err)
119 		goto error;
120 
121 	/* CTN lock in/out: 20000ppm/80000ppm. Lock out 2 times. */
122 	err = ps8622_set(cl, 0x04, 0x71, 0x2d);
123 	if (err)
124 		goto error;
125 
126 	/* 2.7G CDR settings: NOF=40LSB for HBR CDR  setting */
127 	err = ps8622_set(cl, 0x04, 0x7d, 0x07);
128 	if (err)
129 		goto error;
130 
131 	/* [1:0] Fmin=+4bands */
132 	err = ps8622_set(cl, 0x04, 0x7b, 0x00);
133 	if (err)
134 		goto error;
135 
136 	/* [7:5] DCO_FTRNG=+-40% */
137 	err = ps8622_set(cl, 0x04, 0x7a, 0xfd);
138 	if (err)
139 		goto error;
140 
141 	/* 1.62G CDR settings: [5:2]NOF=64LSB [1:0]DCO scale is 2/5 */
142 	err = ps8622_set(cl, 0x04, 0xc0, 0x12);
143 	if (err)
144 		goto error;
145 
146 	/* Gitune=-37% */
147 	err = ps8622_set(cl, 0x04, 0xc1, 0x92);
148 	if (err)
149 		goto error;
150 
151 	/* Fbstep=100% */
152 	err = ps8622_set(cl, 0x04, 0xc2, 0x1c);
153 	if (err)
154 		goto error;
155 
156 	/* [7] LOS signal disable */
157 	err = ps8622_set(cl, 0x04, 0x32, 0x80);
158 	if (err)
159 		goto error;
160 
161 	/* RPIO Setting: [7:4] LVDS driver bias current : 75% (250mV swing) */
162 	err = ps8622_set(cl, 0x04, 0x00, 0xb0);
163 	if (err)
164 		goto error;
165 
166 	/* [7:6] Right-bar GPIO output strength is 8mA */
167 	err = ps8622_set(cl, 0x04, 0x15, 0x40);
168 	if (err)
169 		goto error;
170 
171 	/* EQ Training State Machine Setting, RCO calibration start */
172 	err = ps8622_set(cl, 0x04, 0x54, 0x10);
173 	if (err)
174 		goto error;
175 
176 	/* Logic, needs more than 10 I2C command */
177 	/* [4:0] MAX_LANE_COUNT set to max supported lanes */
178 	err = ps8622_set(cl, 0x01, 0x02, 0x80 | ps8622->max_lane_count);
179 	if (err)
180 		goto error;
181 
182 	/* [4:0] LANE_COUNT_SET set to chosen lane count */
183 	err = ps8622_set(cl, 0x01, 0x21, 0x80 | ps8622->lane_count);
184 	if (err)
185 		goto error;
186 
187 	err = ps8622_set(cl, 0x00, 0x52, 0x20);
188 	if (err)
189 		goto error;
190 
191 	/* HPD CP toggle enable */
192 	err = ps8622_set(cl, 0x00, 0xf1, 0x03);
193 	if (err)
194 		goto error;
195 
196 	err = ps8622_set(cl, 0x00, 0x62, 0x41);
197 	if (err)
198 		goto error;
199 
200 	/* Counter number, add 1ms counter delay */
201 	err = ps8622_set(cl, 0x00, 0xf6, 0x01);
202 	if (err)
203 		goto error;
204 
205 	/* [6]PWM function control by DPCD0040f[7], default is PWM block */
206 	err = ps8622_set(cl, 0x00, 0x77, 0x06);
207 	if (err)
208 		goto error;
209 
210 	/* 04h Adjust VTotal toleranceto fix the 30Hz no display issue */
211 	err = ps8622_set(cl, 0x00, 0x4c, 0x04);
212 	if (err)
213 		goto error;
214 
215 	/* DPCD00400='h00, Parade OUI ='h001cf8 */
216 	err = ps8622_set(cl, 0x01, 0xc0, 0x00);
217 	if (err)
218 		goto error;
219 
220 	/* DPCD00401='h1c */
221 	err = ps8622_set(cl, 0x01, 0xc1, 0x1c);
222 	if (err)
223 		goto error;
224 
225 	/* DPCD00402='hf8 */
226 	err = ps8622_set(cl, 0x01, 0xc2, 0xf8);
227 	if (err)
228 		goto error;
229 
230 	/* DPCD403~408 = ASCII code, D2SLV5='h4432534c5635 */
231 	err = ps8622_set(cl, 0x01, 0xc3, 0x44);
232 	if (err)
233 		goto error;
234 
235 	/* DPCD404 */
236 	err = ps8622_set(cl, 0x01, 0xc4, 0x32);
237 	if (err)
238 		goto error;
239 
240 	/* DPCD405 */
241 	err = ps8622_set(cl, 0x01, 0xc5, 0x53);
242 	if (err)
243 		goto error;
244 
245 	/* DPCD406 */
246 	err = ps8622_set(cl, 0x01, 0xc6, 0x4c);
247 	if (err)
248 		goto error;
249 
250 	/* DPCD407 */
251 	err = ps8622_set(cl, 0x01, 0xc7, 0x56);
252 	if (err)
253 		goto error;
254 
255 	/* DPCD408 */
256 	err = ps8622_set(cl, 0x01, 0xc8, 0x35);
257 	if (err)
258 		goto error;
259 
260 	/* DPCD40A, Initial Code major revision '01' */
261 	err = ps8622_set(cl, 0x01, 0xca, 0x01);
262 	if (err)
263 		goto error;
264 
265 	/* DPCD40B, Initial Code minor revision '05' */
266 	err = ps8622_set(cl, 0x01, 0xcb, 0x05);
267 	if (err)
268 		goto error;
269 
270 
271 	if (ps8622->bl) {
272 		/* DPCD720, internal PWM */
273 		err = ps8622_set(cl, 0x01, 0xa5, 0xa0);
274 		if (err)
275 			goto error;
276 
277 		/* FFh for 100% brightness, 0h for 0% brightness */
278 		err = ps8622_set(cl, 0x01, 0xa7,
279 				ps8622->bl->props.brightness);
280 		if (err)
281 			goto error;
282 	} else {
283 		/* DPCD720, external PWM */
284 		err = ps8622_set(cl, 0x01, 0xa5, 0x80);
285 		if (err)
286 			goto error;
287 	}
288 
289 	/* Set LVDS output as 6bit-VESA mapping, single LVDS channel */
290 	err = ps8622_set(cl, 0x01, 0xcc, 0x13);
291 	if (err)
292 		goto error;
293 
294 	/* Enable SSC set by register */
295 	err = ps8622_set(cl, 0x02, 0xb1, 0x20);
296 	if (err)
297 		goto error;
298 
299 	/* Set SSC enabled and +/-1% central spreading */
300 	err = ps8622_set(cl, 0x04, 0x10, 0x16);
301 	if (err)
302 		goto error;
303 
304 	/* Logic end */
305 	/* MPU Clock source: LC => RCO */
306 	err = ps8622_set(cl, 0x04, 0x59, 0x60);
307 	if (err)
308 		goto error;
309 
310 	/* LC -> RCO */
311 	err = ps8622_set(cl, 0x04, 0x54, 0x14);
312 	if (err)
313 		goto error;
314 
315 	/* HPD high */
316 	err = ps8622_set(cl, 0x02, 0xa1, 0x91);
317 
318 error:
319 	return err ? -EIO : 0;
320 }
321 
322 static int ps8622_backlight_update(struct backlight_device *bl)
323 {
324 	struct ps8622_bridge *ps8622 = dev_get_drvdata(&bl->dev);
325 	int ret, brightness = backlight_get_brightness(bl);
326 
327 	if (!ps8622->enabled)
328 		return -EINVAL;
329 
330 	ret = ps8622_set(ps8622->client, 0x01, 0xa7, brightness);
331 
332 	return ret;
333 }
334 
335 static const struct backlight_ops ps8622_backlight_ops = {
336 	.update_status	= ps8622_backlight_update,
337 };
338 
339 static void ps8622_pre_enable(struct drm_bridge *bridge,
340 			      struct drm_atomic_commit *commit)
341 {
342 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
343 	int ret;
344 
345 	if (ps8622->enabled)
346 		return;
347 
348 	gpiod_set_value(ps8622->gpio_rst, 0);
349 
350 	if (ps8622->v12) {
351 		ret = regulator_enable(ps8622->v12);
352 		if (ret)
353 			DRM_ERROR("fails to enable ps8622->v12");
354 	}
355 
356 	gpiod_set_value(ps8622->gpio_slp, 1);
357 
358 	/*
359 	 * T1 is the range of time that it takes for the power to rise after we
360 	 * enable the lcd/ps8622 fet. T2 is the range of time in which the
361 	 * data sheet specifies we should deassert the reset pin.
362 	 *
363 	 * If it takes T1.max for the power to rise, we need to wait atleast
364 	 * T2.min before deasserting the reset pin. If it takes T1.min for the
365 	 * power to rise, we need to wait at most T2.max before deasserting the
366 	 * reset pin.
367 	 */
368 	usleep_range(PS8622_RST_HIGH_T2_MIN_US + PS8622_POWER_RISE_T1_MAX_US,
369 		     PS8622_RST_HIGH_T2_MAX_US + PS8622_POWER_RISE_T1_MIN_US);
370 
371 	gpiod_set_value(ps8622->gpio_rst, 1);
372 
373 	/* wait 20ms after RST high */
374 	usleep_range(20000, 30000);
375 
376 	ret = ps8622_send_config(ps8622);
377 	if (ret) {
378 		DRM_ERROR("Failed to send config to bridge (%d)\n", ret);
379 		return;
380 	}
381 
382 	ps8622->enabled = true;
383 }
384 
385 static void ps8622_disable(struct drm_bridge *bridge,
386 			   struct drm_atomic_commit *commit)
387 {
388 	/* Delay after panel is disabled */
389 	msleep(PS8622_PWMO_END_T12_MS);
390 }
391 
392 static void ps8622_post_disable(struct drm_bridge *bridge,
393 				struct drm_atomic_commit *commit)
394 {
395 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
396 
397 	if (!ps8622->enabled)
398 		return;
399 
400 	ps8622->enabled = false;
401 
402 	/*
403 	 * This doesn't matter if the regulators are turned off, but something
404 	 * else might keep them on. In that case, we want to assert the slp gpio
405 	 * to lower power.
406 	 */
407 	gpiod_set_value(ps8622->gpio_slp, 0);
408 
409 	if (ps8622->v12)
410 		regulator_disable(ps8622->v12);
411 
412 	/*
413 	 * Sleep for at least the amount of time that it takes the power rail to
414 	 * fall to prevent asserting the rst gpio from doing anything.
415 	 */
416 	usleep_range(PS8622_POWER_FALL_T16_MAX_US,
417 		     2 * PS8622_POWER_FALL_T16_MAX_US);
418 	gpiod_set_value(ps8622->gpio_rst, 0);
419 
420 	msleep(PS8622_POWER_OFF_T17_MS);
421 }
422 
423 static int ps8622_attach(struct drm_bridge *bridge,
424 			 struct drm_encoder *encoder,
425 			 enum drm_bridge_attach_flags flags)
426 {
427 	struct ps8622_bridge *ps8622 = bridge_to_ps8622(bridge);
428 
429 	return drm_bridge_attach(ps8622->bridge.encoder, ps8622->panel_bridge,
430 				 &ps8622->bridge, flags);
431 }
432 
433 static const struct drm_bridge_funcs ps8622_bridge_funcs = {
434 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
435 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
436 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
437 	.atomic_pre_enable = ps8622_pre_enable,
438 	.atomic_disable = ps8622_disable,
439 	.atomic_post_disable = ps8622_post_disable,
440 	.attach = ps8622_attach,
441 };
442 
443 static const struct of_device_id ps8622_devices[] = {
444 	{.compatible = "parade,ps8622",},
445 	{.compatible = "parade,ps8625",},
446 	{}
447 };
448 MODULE_DEVICE_TABLE(of, ps8622_devices);
449 
450 static int ps8622_probe(struct i2c_client *client)
451 {
452 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
453 	struct device *dev = &client->dev;
454 	struct ps8622_bridge *ps8622;
455 	struct drm_bridge *panel_bridge;
456 	int ret;
457 
458 	ps8622 = devm_drm_bridge_alloc(dev, struct ps8622_bridge, bridge,
459 				       &ps8622_bridge_funcs);
460 	if (IS_ERR(ps8622))
461 		return PTR_ERR(ps8622);
462 
463 	panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0);
464 	if (IS_ERR(panel_bridge))
465 		return PTR_ERR(panel_bridge);
466 
467 	ps8622->panel_bridge = panel_bridge;
468 	ps8622->client = client;
469 
470 	ps8622->v12 = devm_regulator_get(dev, "vdd12");
471 	if (IS_ERR(ps8622->v12)) {
472 		dev_info(dev, "no 1.2v regulator found for PS8622\n");
473 		ps8622->v12 = NULL;
474 	}
475 
476 	ps8622->gpio_slp = devm_gpiod_get(dev, "sleep", GPIOD_OUT_HIGH);
477 	if (IS_ERR(ps8622->gpio_slp)) {
478 		ret = PTR_ERR(ps8622->gpio_slp);
479 		dev_err(dev, "cannot get gpio_slp %d\n", ret);
480 		return ret;
481 	}
482 
483 	/*
484 	 * Assert the reset pin high to avoid the bridge being
485 	 * initialized prematurely
486 	 */
487 	ps8622->gpio_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
488 	if (IS_ERR(ps8622->gpio_rst)) {
489 		ret = PTR_ERR(ps8622->gpio_rst);
490 		dev_err(dev, "cannot get gpio_rst %d\n", ret);
491 		return ret;
492 	}
493 
494 	ps8622->max_lane_count = id->driver_data;
495 
496 	if (of_property_read_u32(dev->of_node, "lane-count",
497 						&ps8622->lane_count)) {
498 		ps8622->lane_count = ps8622->max_lane_count;
499 	} else if (ps8622->lane_count > ps8622->max_lane_count) {
500 		dev_info(dev, "lane-count property is too high,"
501 						"using max_lane_count\n");
502 		ps8622->lane_count = ps8622->max_lane_count;
503 	}
504 
505 	if (!of_property_read_bool(dev->of_node, "use-external-pwm")) {
506 		ps8622->bl = backlight_device_register("ps8622-backlight",
507 				dev, ps8622, &ps8622_backlight_ops,
508 				NULL);
509 		if (IS_ERR(ps8622->bl)) {
510 			DRM_ERROR("failed to register backlight\n");
511 			ret = PTR_ERR(ps8622->bl);
512 			ps8622->bl = NULL;
513 			return ret;
514 		}
515 		ps8622->bl->props.max_brightness = PS8622_MAX_BRIGHTNESS;
516 		ps8622->bl->props.brightness = PS8622_MAX_BRIGHTNESS;
517 	}
518 
519 	ps8622->bridge.type = DRM_MODE_CONNECTOR_LVDS;
520 	ps8622->bridge.of_node = dev->of_node;
521 	drm_bridge_add(&ps8622->bridge);
522 
523 	i2c_set_clientdata(client, ps8622);
524 
525 	return 0;
526 }
527 
528 static void ps8622_remove(struct i2c_client *client)
529 {
530 	struct ps8622_bridge *ps8622 = i2c_get_clientdata(client);
531 
532 	backlight_device_unregister(ps8622->bl);
533 	drm_bridge_remove(&ps8622->bridge);
534 }
535 
536 static const struct i2c_device_id ps8622_i2c_table[] = {
537 	/* Device type, driver_data holds the maximal lane_count */
538 	{ .name = "ps8622", .driver_data = 1 },
539 	{ .name = "ps8625", .driver_data = 2 },
540 	{ }
541 };
542 MODULE_DEVICE_TABLE(i2c, ps8622_i2c_table);
543 
544 static struct i2c_driver ps8622_driver = {
545 	.id_table	= ps8622_i2c_table,
546 	.probe		= ps8622_probe,
547 	.remove		= ps8622_remove,
548 	.driver		= {
549 		.name	= "ps8622",
550 		.of_match_table = ps8622_devices,
551 	},
552 };
553 module_i2c_driver(ps8622_driver);
554 
555 MODULE_AUTHOR("Vincent Palatin <vpalatin@chromium.org>");
556 MODULE_DESCRIPTION("Parade ps8622/ps8625 eDP-LVDS converter driver");
557 MODULE_LICENSE("GPL v2");
558