1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2016, Analogix Semiconductor.
4  * Copyright(c) 2017, Icenowy Zheng <icenowy@aosc.io>
5  *
6  * Based on anx7808 driver obtained from chromeos with copyright:
7  * Copyright(c) 2013, Google Inc.
8  */
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/types.h>
20 
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_dp_helper.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_panel.h>
28 #include <drm/drm_print.h>
29 #include <drm/drm_probe_helper.h>
30 
31 #include "analogix-i2c-dptx.h"
32 #include "analogix-i2c-txcommon.h"
33 
34 #define POLL_DELAY		50000 /* us */
35 #define POLL_TIMEOUT		5000000 /* us */
36 
37 #define I2C_IDX_DPTX		0
38 #define I2C_IDX_TXCOM		1
39 
40 static const u8 anx6345_i2c_addresses[] = {
41 	[I2C_IDX_DPTX]	= 0x70,
42 	[I2C_IDX_TXCOM]	= 0x72,
43 };
44 #define I2C_NUM_ADDRESSES	ARRAY_SIZE(anx6345_i2c_addresses)
45 
46 struct anx6345 {
47 	struct drm_dp_aux aux;
48 	struct drm_bridge bridge;
49 	struct i2c_client *client;
50 	struct edid *edid;
51 	struct drm_connector connector;
52 	struct drm_dp_link link;
53 	struct drm_panel *panel;
54 	struct regulator *dvdd12;
55 	struct regulator *dvdd25;
56 	struct gpio_desc *gpiod_reset;
57 	struct mutex lock;	/* protect EDID access */
58 
59 	/* I2C Slave addresses of ANX6345 are mapped as DPTX and SYS */
60 	struct i2c_client *i2c_clients[I2C_NUM_ADDRESSES];
61 	struct regmap *map[I2C_NUM_ADDRESSES];
62 
63 	u16 chipid;
64 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
65 
66 	bool powered;
67 };
68 
69 static inline struct anx6345 *connector_to_anx6345(struct drm_connector *c)
70 {
71 	return container_of(c, struct anx6345, connector);
72 }
73 
74 static inline struct anx6345 *bridge_to_anx6345(struct drm_bridge *bridge)
75 {
76 	return container_of(bridge, struct anx6345, bridge);
77 }
78 
79 static int anx6345_set_bits(struct regmap *map, u8 reg, u8 mask)
80 {
81 	return regmap_update_bits(map, reg, mask, mask);
82 }
83 
84 static int anx6345_clear_bits(struct regmap *map, u8 reg, u8 mask)
85 {
86 	return regmap_update_bits(map, reg, mask, 0);
87 }
88 
89 static ssize_t anx6345_aux_transfer(struct drm_dp_aux *aux,
90 				    struct drm_dp_aux_msg *msg)
91 {
92 	struct anx6345 *anx6345 = container_of(aux, struct anx6345, aux);
93 
94 	return anx_dp_aux_transfer(anx6345->map[I2C_IDX_DPTX], msg);
95 }
96 
97 static int anx6345_dp_link_training(struct anx6345 *anx6345)
98 {
99 	unsigned int value;
100 	u8 dp_bw;
101 	int err;
102 
103 	err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
104 				 SP_POWERDOWN_CTRL_REG,
105 				 SP_TOTAL_PD);
106 	if (err)
107 		return err;
108 
109 	err = drm_dp_dpcd_readb(&anx6345->aux, DP_MAX_LINK_RATE, &dp_bw);
110 	if (err < 0)
111 		return err;
112 
113 	switch (dp_bw) {
114 	case DP_LINK_BW_1_62:
115 	case DP_LINK_BW_2_7:
116 		break;
117 
118 	default:
119 		DRM_DEBUG_KMS("DP bandwidth (%#02x) not supported\n", dp_bw);
120 		return -EINVAL;
121 	}
122 
123 	err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
124 			       SP_VIDEO_MUTE);
125 	if (err)
126 		return err;
127 
128 	err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
129 				 SP_VID_CTRL1_REG, SP_VIDEO_EN);
130 	if (err)
131 		return err;
132 
133 	/* Get DPCD info */
134 	err = drm_dp_dpcd_read(&anx6345->aux, DP_DPCD_REV,
135 			       &anx6345->dpcd, DP_RECEIVER_CAP_SIZE);
136 	if (err < 0) {
137 		DRM_ERROR("Failed to read DPCD: %d\n", err);
138 		return err;
139 	}
140 
141 	/* Clear channel x SERDES power down */
142 	err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
143 				 SP_DP_ANALOG_POWER_DOWN_REG, SP_CH0_PD);
144 	if (err)
145 		return err;
146 
147 	/* Check link capabilities */
148 	err = drm_dp_link_probe(&anx6345->aux, &anx6345->link);
149 	if (err < 0) {
150 		DRM_ERROR("Failed to probe link capabilities: %d\n", err);
151 		return err;
152 	}
153 
154 	/* Power up the sink */
155 	err = drm_dp_link_power_up(&anx6345->aux, &anx6345->link);
156 	if (err < 0) {
157 		DRM_ERROR("Failed to power up DisplayPort link: %d\n", err);
158 		return err;
159 	}
160 
161 	/* Possibly enable downspread on the sink */
162 	err = regmap_write(anx6345->map[I2C_IDX_DPTX],
163 			   SP_DP_DOWNSPREAD_CTRL1_REG, 0);
164 	if (err)
165 		return err;
166 
167 	if (anx6345->dpcd[DP_MAX_DOWNSPREAD] & DP_MAX_DOWNSPREAD_0_5) {
168 		DRM_DEBUG("Enable downspread on the sink\n");
169 		/* 4000PPM */
170 		err = regmap_write(anx6345->map[I2C_IDX_DPTX],
171 				   SP_DP_DOWNSPREAD_CTRL1_REG, 8);
172 		if (err)
173 			return err;
174 
175 		err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL,
176 					 DP_SPREAD_AMP_0_5);
177 		if (err < 0)
178 			return err;
179 	} else {
180 		err = drm_dp_dpcd_writeb(&anx6345->aux, DP_DOWNSPREAD_CTRL, 0);
181 		if (err < 0)
182 			return err;
183 	}
184 
185 	/* Set the lane count and the link rate on the sink */
186 	if (drm_dp_enhanced_frame_cap(anx6345->dpcd))
187 		err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
188 				       SP_DP_SYSTEM_CTRL_BASE + 4,
189 				       SP_ENHANCED_MODE);
190 	else
191 		err = anx6345_clear_bits(anx6345->map[I2C_IDX_DPTX],
192 					 SP_DP_SYSTEM_CTRL_BASE + 4,
193 					 SP_ENHANCED_MODE);
194 	if (err)
195 		return err;
196 
197 	value = drm_dp_link_rate_to_bw_code(anx6345->link.rate);
198 	err = regmap_write(anx6345->map[I2C_IDX_DPTX],
199 			   SP_DP_MAIN_LINK_BW_SET_REG, value);
200 	if (err)
201 		return err;
202 
203 	err = regmap_write(anx6345->map[I2C_IDX_DPTX],
204 			   SP_DP_LANE_COUNT_SET_REG, anx6345->link.num_lanes);
205 	if (err)
206 		return err;
207 
208 	err = drm_dp_link_configure(&anx6345->aux, &anx6345->link);
209 	if (err < 0) {
210 		DRM_ERROR("Failed to configure DisplayPort link: %d\n", err);
211 		return err;
212 	}
213 
214 	/* Start training on the source */
215 	err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_LT_CTRL_REG,
216 			   SP_LT_EN);
217 	if (err)
218 		return err;
219 
220 	return regmap_read_poll_timeout(anx6345->map[I2C_IDX_DPTX],
221 				       SP_DP_LT_CTRL_REG,
222 				       value, !(value & SP_DP_LT_INPROGRESS),
223 				       POLL_DELAY, POLL_TIMEOUT);
224 }
225 
226 static int anx6345_tx_initialization(struct anx6345 *anx6345)
227 {
228 	int err, i;
229 
230 	/* FIXME: colordepth is hardcoded for now */
231 	err = regmap_write(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL2_REG,
232 			   SP_IN_BPC_6BIT << SP_IN_BPC_SHIFT);
233 	if (err)
234 		return err;
235 
236 	err = regmap_write(anx6345->map[I2C_IDX_DPTX], SP_DP_PLL_CTRL_REG, 0);
237 	if (err)
238 		return err;
239 
240 	err = regmap_write(anx6345->map[I2C_IDX_TXCOM],
241 			   SP_ANALOG_DEBUG1_REG, 0);
242 	if (err)
243 		return err;
244 
245 	err = regmap_write(anx6345->map[I2C_IDX_DPTX],
246 			   SP_DP_LINK_DEBUG_CTRL_REG,
247 			   SP_NEW_PRBS7 | SP_M_VID_DEBUG);
248 	if (err)
249 		return err;
250 
251 	err = regmap_write(anx6345->map[I2C_IDX_DPTX],
252 			   SP_DP_ANALOG_POWER_DOWN_REG, 0);
253 	if (err)
254 		return err;
255 
256 	/* Force HPD */
257 	err = anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
258 			       SP_DP_SYSTEM_CTRL_BASE + 3,
259 			       SP_HPD_FORCE | SP_HPD_CTRL);
260 	if (err)
261 		return err;
262 
263 	for (i = 0; i < 4; i++) {
264 		/* 4 lanes */
265 		err = regmap_write(anx6345->map[I2C_IDX_DPTX],
266 				   SP_DP_LANE0_LT_CTRL_REG + i, 0);
267 		if (err)
268 			return err;
269 	}
270 
271 	/* Reset AUX */
272 	err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM],
273 			       SP_RESET_CTRL2_REG, SP_AUX_RST);
274 	if (err)
275 		return err;
276 
277 	return anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
278 				 SP_RESET_CTRL2_REG, SP_AUX_RST);
279 }
280 
281 static void anx6345_poweron(struct anx6345 *anx6345)
282 {
283 	int err;
284 
285 	/* Ensure reset is asserted before starting power on sequence */
286 	gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
287 	usleep_range(1000, 2000);
288 
289 	err = regulator_enable(anx6345->dvdd12);
290 	if (err) {
291 		DRM_ERROR("Failed to enable dvdd12 regulator: %d\n",
292 			  err);
293 		return;
294 	}
295 
296 	/* T1 - delay between VDD12 and VDD25 should be 0-2ms */
297 	usleep_range(1000, 2000);
298 
299 	err = regulator_enable(anx6345->dvdd25);
300 	if (err) {
301 		DRM_ERROR("Failed to enable dvdd25 regulator: %d\n",
302 			  err);
303 		return;
304 	}
305 
306 	/* T2 - delay between RESETN and all power rail stable,
307 	 * should be 2-5ms
308 	 */
309 	usleep_range(2000, 5000);
310 
311 	gpiod_set_value_cansleep(anx6345->gpiod_reset, 0);
312 
313 	/* Power on registers module */
314 	anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
315 			 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
316 	anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
317 			   SP_REGISTER_PD | SP_TOTAL_PD);
318 
319 	if (anx6345->panel)
320 		drm_panel_prepare(anx6345->panel);
321 
322 	anx6345->powered = true;
323 }
324 
325 static void anx6345_poweroff(struct anx6345 *anx6345)
326 {
327 	int err;
328 
329 	gpiod_set_value_cansleep(anx6345->gpiod_reset, 1);
330 	usleep_range(1000, 2000);
331 
332 	if (anx6345->panel)
333 		drm_panel_unprepare(anx6345->panel);
334 
335 	err = regulator_disable(anx6345->dvdd25);
336 	if (err) {
337 		DRM_ERROR("Failed to disable dvdd25 regulator: %d\n",
338 			  err);
339 		return;
340 	}
341 
342 	usleep_range(5000, 10000);
343 
344 	err = regulator_disable(anx6345->dvdd12);
345 	if (err) {
346 		DRM_ERROR("Failed to disable dvdd12 regulator: %d\n",
347 			  err);
348 		return;
349 	}
350 
351 	usleep_range(1000, 2000);
352 
353 	anx6345->powered = false;
354 }
355 
356 static int anx6345_start(struct anx6345 *anx6345)
357 {
358 	int err;
359 
360 	if (!anx6345->powered)
361 		anx6345_poweron(anx6345);
362 
363 	/* Power on needed modules */
364 	err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM],
365 				SP_POWERDOWN_CTRL_REG,
366 				SP_VIDEO_PD | SP_LINK_PD);
367 
368 	err = anx6345_tx_initialization(anx6345);
369 	if (err) {
370 		DRM_ERROR("Failed eDP transmitter initialization: %d\n", err);
371 		anx6345_poweroff(anx6345);
372 		return err;
373 	}
374 
375 	err = anx6345_dp_link_training(anx6345);
376 	if (err) {
377 		DRM_ERROR("Failed link training: %d\n", err);
378 		anx6345_poweroff(anx6345);
379 		return err;
380 	}
381 
382 	/*
383 	 * This delay seems to help keep the hardware in a good state. Without
384 	 * it, there are times where it fails silently.
385 	 */
386 	usleep_range(10000, 15000);
387 
388 	return 0;
389 }
390 
391 static int anx6345_config_dp_output(struct anx6345 *anx6345)
392 {
393 	int err;
394 
395 	err = anx6345_clear_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
396 				 SP_VIDEO_MUTE);
397 	if (err)
398 		return err;
399 
400 	/* Enable DP output */
401 	err = anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_VID_CTRL1_REG,
402 			       SP_VIDEO_EN);
403 	if (err)
404 		return err;
405 
406 	/* Force stream valid */
407 	return anx6345_set_bits(anx6345->map[I2C_IDX_DPTX],
408 			       SP_DP_SYSTEM_CTRL_BASE + 3,
409 			       SP_STRM_FORCE | SP_STRM_CTRL);
410 }
411 
412 static int anx6345_get_downstream_info(struct anx6345 *anx6345)
413 {
414 	u8 value;
415 	int err;
416 
417 	err = drm_dp_dpcd_readb(&anx6345->aux, DP_SINK_COUNT, &value);
418 	if (err < 0) {
419 		DRM_ERROR("Get sink count failed %d\n", err);
420 		return err;
421 	}
422 
423 	if (!DP_GET_SINK_COUNT(value)) {
424 		DRM_ERROR("Downstream disconnected\n");
425 		return -EIO;
426 	}
427 
428 	return 0;
429 }
430 
431 static int anx6345_get_modes(struct drm_connector *connector)
432 {
433 	struct anx6345 *anx6345 = connector_to_anx6345(connector);
434 	int err, num_modes = 0;
435 	bool power_off = false;
436 
437 	mutex_lock(&anx6345->lock);
438 
439 	if (!anx6345->edid) {
440 		if (!anx6345->powered) {
441 			anx6345_poweron(anx6345);
442 			power_off = true;
443 		}
444 
445 		err = anx6345_get_downstream_info(anx6345);
446 		if (err) {
447 			DRM_ERROR("Failed to get downstream info: %d\n", err);
448 			goto unlock;
449 		}
450 
451 		anx6345->edid = drm_get_edid(connector, &anx6345->aux.ddc);
452 		if (!anx6345->edid)
453 			DRM_ERROR("Failed to read EDID from panel\n");
454 
455 		err = drm_connector_update_edid_property(connector,
456 							 anx6345->edid);
457 		if (err) {
458 			DRM_ERROR("Failed to update EDID property: %d\n", err);
459 			goto unlock;
460 		}
461 	}
462 
463 	num_modes += drm_add_edid_modes(connector, anx6345->edid);
464 
465 unlock:
466 	if (power_off)
467 		anx6345_poweroff(anx6345);
468 
469 	mutex_unlock(&anx6345->lock);
470 
471 	if (!num_modes && anx6345->panel)
472 		num_modes += drm_panel_get_modes(anx6345->panel);
473 
474 	return num_modes;
475 }
476 
477 static const struct drm_connector_helper_funcs anx6345_connector_helper_funcs = {
478 	.get_modes = anx6345_get_modes,
479 };
480 
481 static void
482 anx6345_connector_destroy(struct drm_connector *connector)
483 {
484 	struct anx6345 *anx6345 = connector_to_anx6345(connector);
485 
486 	if (anx6345->panel)
487 		drm_panel_detach(anx6345->panel);
488 	drm_connector_cleanup(connector);
489 }
490 
491 static const struct drm_connector_funcs anx6345_connector_funcs = {
492 	.fill_modes = drm_helper_probe_single_connector_modes,
493 	.destroy = anx6345_connector_destroy,
494 	.reset = drm_atomic_helper_connector_reset,
495 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
496 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
497 };
498 
499 static int anx6345_bridge_attach(struct drm_bridge *bridge)
500 {
501 	struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
502 	int err;
503 
504 	if (!bridge->encoder) {
505 		DRM_ERROR("Parent encoder object not found");
506 		return -ENODEV;
507 	}
508 
509 	/* Register aux channel */
510 	anx6345->aux.name = "DP-AUX";
511 	anx6345->aux.dev = &anx6345->client->dev;
512 	anx6345->aux.transfer = anx6345_aux_transfer;
513 
514 	err = drm_dp_aux_register(&anx6345->aux);
515 	if (err < 0) {
516 		DRM_ERROR("Failed to register aux channel: %d\n", err);
517 		return err;
518 	}
519 
520 	err = drm_connector_init(bridge->dev, &anx6345->connector,
521 				 &anx6345_connector_funcs,
522 				 DRM_MODE_CONNECTOR_eDP);
523 	if (err) {
524 		DRM_ERROR("Failed to initialize connector: %d\n", err);
525 		return err;
526 	}
527 
528 	drm_connector_helper_add(&anx6345->connector,
529 				 &anx6345_connector_helper_funcs);
530 
531 	err = drm_connector_register(&anx6345->connector);
532 	if (err) {
533 		DRM_ERROR("Failed to register connector: %d\n", err);
534 		return err;
535 	}
536 
537 	anx6345->connector.polled = DRM_CONNECTOR_POLL_HPD;
538 
539 	err = drm_connector_attach_encoder(&anx6345->connector,
540 					   bridge->encoder);
541 	if (err) {
542 		DRM_ERROR("Failed to link up connector to encoder: %d\n", err);
543 		return err;
544 	}
545 
546 	if (anx6345->panel) {
547 		err = drm_panel_attach(anx6345->panel, &anx6345->connector);
548 		if (err) {
549 			DRM_ERROR("Failed to attach panel: %d\n", err);
550 			return err;
551 		}
552 	}
553 
554 	return 0;
555 }
556 
557 static enum drm_mode_status
558 anx6345_bridge_mode_valid(struct drm_bridge *bridge,
559 			  const struct drm_display_mode *mode)
560 {
561 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
562 		return MODE_NO_INTERLACE;
563 
564 	/* Max 1200p at 5.4 Ghz, one lane */
565 	if (mode->clock > 154000)
566 		return MODE_CLOCK_HIGH;
567 
568 	return MODE_OK;
569 }
570 
571 static void anx6345_bridge_disable(struct drm_bridge *bridge)
572 {
573 	struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
574 
575 	/* Power off all modules except configuration registers access */
576 	anx6345_set_bits(anx6345->map[I2C_IDX_TXCOM], SP_POWERDOWN_CTRL_REG,
577 			 SP_HDCP_PD | SP_AUDIO_PD | SP_VIDEO_PD | SP_LINK_PD);
578 	if (anx6345->panel)
579 		drm_panel_disable(anx6345->panel);
580 
581 	if (anx6345->powered)
582 		anx6345_poweroff(anx6345);
583 }
584 
585 static void anx6345_bridge_enable(struct drm_bridge *bridge)
586 {
587 	struct anx6345 *anx6345 = bridge_to_anx6345(bridge);
588 	int err;
589 
590 	if (anx6345->panel)
591 		drm_panel_enable(anx6345->panel);
592 
593 	err = anx6345_start(anx6345);
594 	if (err) {
595 		DRM_ERROR("Failed to initialize: %d\n", err);
596 		return;
597 	}
598 
599 	err = anx6345_config_dp_output(anx6345);
600 	if (err)
601 		DRM_ERROR("Failed to enable DP output: %d\n", err);
602 }
603 
604 static const struct drm_bridge_funcs anx6345_bridge_funcs = {
605 	.attach = anx6345_bridge_attach,
606 	.mode_valid = anx6345_bridge_mode_valid,
607 	.disable = anx6345_bridge_disable,
608 	.enable = anx6345_bridge_enable,
609 };
610 
611 static void unregister_i2c_dummy_clients(struct anx6345 *anx6345)
612 {
613 	unsigned int i;
614 
615 	for (i = 1; i < ARRAY_SIZE(anx6345->i2c_clients); i++)
616 		if (anx6345->i2c_clients[i] &&
617 		    anx6345->i2c_clients[i]->addr != anx6345->client->addr)
618 			i2c_unregister_device(anx6345->i2c_clients[i]);
619 }
620 
621 static const struct regmap_config anx6345_regmap_config = {
622 	.reg_bits = 8,
623 	.val_bits = 8,
624 	.max_register = 0xff,
625 	.cache_type = REGCACHE_NONE,
626 };
627 
628 static const u16 anx6345_chipid_list[] = {
629 	0x6345,
630 };
631 
632 static bool anx6345_get_chip_id(struct anx6345 *anx6345)
633 {
634 	unsigned int i, idl, idh, version;
635 
636 	if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDL_REG, &idl))
637 		return false;
638 
639 	if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_IDH_REG, &idh))
640 		return false;
641 
642 	anx6345->chipid = (u8)idl | ((u8)idh << 8);
643 
644 	if (regmap_read(anx6345->map[I2C_IDX_TXCOM], SP_DEVICE_VERSION_REG,
645 			&version))
646 		return false;
647 
648 	for (i = 0; i < ARRAY_SIZE(anx6345_chipid_list); i++) {
649 		if (anx6345->chipid == anx6345_chipid_list[i]) {
650 			DRM_INFO("Found ANX%x (ver. %d) eDP Transmitter\n",
651 				 anx6345->chipid, version);
652 			return true;
653 		}
654 	}
655 
656 	DRM_ERROR("ANX%x (ver. %d) not supported by this driver\n",
657 		  anx6345->chipid, version);
658 
659 	return false;
660 }
661 
662 static int anx6345_i2c_probe(struct i2c_client *client,
663 			     const struct i2c_device_id *id)
664 {
665 	struct anx6345 *anx6345;
666 	struct device *dev;
667 	int i, err;
668 
669 	anx6345 = devm_kzalloc(&client->dev, sizeof(*anx6345), GFP_KERNEL);
670 	if (!anx6345)
671 		return -ENOMEM;
672 
673 	mutex_init(&anx6345->lock);
674 
675 	anx6345->bridge.of_node = client->dev.of_node;
676 
677 	anx6345->client = client;
678 	i2c_set_clientdata(client, anx6345);
679 
680 	dev = &anx6345->client->dev;
681 
682 	err = drm_of_find_panel_or_bridge(client->dev.of_node, 1, 0,
683 					  &anx6345->panel, NULL);
684 	if (err == -EPROBE_DEFER)
685 		return err;
686 
687 	if (err)
688 		DRM_DEBUG("No panel found\n");
689 
690 	/* 1.2V digital core power regulator  */
691 	anx6345->dvdd12 = devm_regulator_get(dev, "dvdd12-supply");
692 	if (IS_ERR(anx6345->dvdd12)) {
693 		DRM_ERROR("dvdd12-supply not found\n");
694 		return PTR_ERR(anx6345->dvdd12);
695 	}
696 
697 	/* 2.5V digital core power regulator  */
698 	anx6345->dvdd25 = devm_regulator_get(dev, "dvdd25-supply");
699 	if (IS_ERR(anx6345->dvdd25)) {
700 		DRM_ERROR("dvdd25-supply not found\n");
701 		return PTR_ERR(anx6345->dvdd25);
702 	}
703 
704 	/* GPIO for chip reset */
705 	anx6345->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
706 	if (IS_ERR(anx6345->gpiod_reset)) {
707 		DRM_ERROR("Reset gpio not found\n");
708 		return PTR_ERR(anx6345->gpiod_reset);
709 	}
710 
711 	/* Map slave addresses of ANX6345 */
712 	for (i = 0; i < I2C_NUM_ADDRESSES; i++) {
713 		if (anx6345_i2c_addresses[i] >> 1 != client->addr)
714 			anx6345->i2c_clients[i] = i2c_new_dummy(client->adapter,
715 						anx6345_i2c_addresses[i] >> 1);
716 		else
717 			anx6345->i2c_clients[i] = client;
718 
719 		if (!anx6345->i2c_clients[i]) {
720 			err = -ENOMEM;
721 			DRM_ERROR("Failed to reserve I2C bus %02x\n",
722 				  anx6345_i2c_addresses[i]);
723 			goto err_unregister_i2c;
724 		}
725 
726 		anx6345->map[i] = devm_regmap_init_i2c(anx6345->i2c_clients[i],
727 						       &anx6345_regmap_config);
728 		if (IS_ERR(anx6345->map[i])) {
729 			err = PTR_ERR(anx6345->map[i]);
730 			DRM_ERROR("Failed regmap initialization %02x\n",
731 				  anx6345_i2c_addresses[i]);
732 			goto err_unregister_i2c;
733 		}
734 	}
735 
736 	/* Look for supported chip ID */
737 	anx6345_poweron(anx6345);
738 	if (anx6345_get_chip_id(anx6345)) {
739 		anx6345->bridge.funcs = &anx6345_bridge_funcs;
740 		drm_bridge_add(&anx6345->bridge);
741 
742 		return 0;
743 	} else {
744 		anx6345_poweroff(anx6345);
745 		err = -ENODEV;
746 	}
747 
748 err_unregister_i2c:
749 	unregister_i2c_dummy_clients(anx6345);
750 	return err;
751 }
752 
753 static int anx6345_i2c_remove(struct i2c_client *client)
754 {
755 	struct anx6345 *anx6345 = i2c_get_clientdata(client);
756 
757 	drm_bridge_remove(&anx6345->bridge);
758 
759 	unregister_i2c_dummy_clients(anx6345);
760 
761 	kfree(anx6345->edid);
762 
763 	mutex_destroy(&anx6345->lock);
764 
765 	return 0;
766 }
767 
768 static const struct i2c_device_id anx6345_id[] = {
769 	{ "anx6345", 0 },
770 	{ /* sentinel */ }
771 };
772 MODULE_DEVICE_TABLE(i2c, anx6345_id);
773 
774 static const struct of_device_id anx6345_match_table[] = {
775 	{ .compatible = "analogix,anx6345", },
776 	{ /* sentinel */ },
777 };
778 MODULE_DEVICE_TABLE(of, anx6345_match_table);
779 
780 static struct i2c_driver anx6345_driver = {
781 	.driver = {
782 		   .name = "anx6345",
783 		   .of_match_table = of_match_ptr(anx6345_match_table),
784 		  },
785 	.probe = anx6345_i2c_probe,
786 	.remove = anx6345_i2c_remove,
787 	.id_table = anx6345_id,
788 };
789 module_i2c_driver(anx6345_driver);
790 
791 MODULE_DESCRIPTION("ANX6345 eDP Transmitter driver");
792 MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
793 MODULE_LICENSE("GPL v2");
794