1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/media-bus-format.h>
11 #include <linux/regmap.h>
12 
13 #include <drm/drm_probe_helper.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_edid.h>
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_of.h>
18 
19 #include <video/videomode.h>
20 
21 #define I2C_MAIN 0
22 #define I2C_ADDR_MAIN 0x48
23 
24 #define I2C_CEC_DSI 1
25 #define I2C_ADDR_CEC_DSI 0x49
26 
27 #define I2C_MAX_IDX 2
28 
29 struct lt8912 {
30 	struct device *dev;
31 	struct drm_bridge bridge;
32 	struct drm_connector connector;
33 
34 	struct i2c_client *i2c_client[I2C_MAX_IDX];
35 	struct regmap *regmap[I2C_MAX_IDX];
36 
37 	struct device_node *host_node;
38 	struct drm_bridge *hdmi_port;
39 
40 	struct mipi_dsi_device *dsi;
41 
42 	struct gpio_desc *gp_reset;
43 
44 	struct videomode mode;
45 
46 	u8 data_lanes;
47 	bool is_power_on;
48 	bool is_attached;
49 };
50 
lt8912_write_init_config(struct lt8912 * lt)51 static int lt8912_write_init_config(struct lt8912 *lt)
52 {
53 	const struct reg_sequence seq[] = {
54 		/* Digital clock en*/
55 		{0x08, 0xff},
56 		{0x09, 0xff},
57 		{0x0a, 0xff},
58 		{0x0b, 0x7c},
59 		{0x0c, 0xff},
60 		{0x42, 0x04},
61 
62 		/*Tx Analog*/
63 		{0x31, 0xb1},
64 		{0x32, 0xb1},
65 		{0x33, 0x0e},
66 		{0x37, 0x00},
67 		{0x38, 0x22},
68 		{0x60, 0x82},
69 
70 		/*Cbus Analog*/
71 		{0x39, 0x45},
72 		{0x3a, 0x00},
73 		{0x3b, 0x00},
74 
75 		/*HDMI Pll Analog*/
76 		{0x44, 0x31},
77 		{0x55, 0x44},
78 		{0x57, 0x01},
79 		{0x5a, 0x02},
80 
81 		/*MIPI Analog*/
82 		{0x3e, 0xd6},
83 		{0x3f, 0xd4},
84 		{0x41, 0x3c},
85 		{0xB2, 0x00},
86 	};
87 
88 	return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
89 }
90 
lt8912_write_mipi_basic_config(struct lt8912 * lt)91 static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
92 {
93 	const struct reg_sequence seq[] = {
94 		{0x12, 0x04},
95 		{0x14, 0x00},
96 		{0x15, 0x00},
97 		{0x1a, 0x03},
98 		{0x1b, 0x03},
99 	};
100 
101 	return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
102 };
103 
lt8912_write_dds_config(struct lt8912 * lt)104 static int lt8912_write_dds_config(struct lt8912 *lt)
105 {
106 	const struct reg_sequence seq[] = {
107 		{0x4e, 0xff},
108 		{0x4f, 0x56},
109 		{0x50, 0x69},
110 		{0x51, 0x80},
111 		{0x1f, 0x5e},
112 		{0x20, 0x01},
113 		{0x21, 0x2c},
114 		{0x22, 0x01},
115 		{0x23, 0xfa},
116 		{0x24, 0x00},
117 		{0x25, 0xc8},
118 		{0x26, 0x00},
119 		{0x27, 0x5e},
120 		{0x28, 0x01},
121 		{0x29, 0x2c},
122 		{0x2a, 0x01},
123 		{0x2b, 0xfa},
124 		{0x2c, 0x00},
125 		{0x2d, 0xc8},
126 		{0x2e, 0x00},
127 		{0x42, 0x64},
128 		{0x43, 0x00},
129 		{0x44, 0x04},
130 		{0x45, 0x00},
131 		{0x46, 0x59},
132 		{0x47, 0x00},
133 		{0x48, 0xf2},
134 		{0x49, 0x06},
135 		{0x4a, 0x00},
136 		{0x4b, 0x72},
137 		{0x4c, 0x45},
138 		{0x4d, 0x00},
139 		{0x52, 0x08},
140 		{0x53, 0x00},
141 		{0x54, 0xb2},
142 		{0x55, 0x00},
143 		{0x56, 0xe4},
144 		{0x57, 0x0d},
145 		{0x58, 0x00},
146 		{0x59, 0xe4},
147 		{0x5a, 0x8a},
148 		{0x5b, 0x00},
149 		{0x5c, 0x34},
150 		{0x1e, 0x4f},
151 		{0x51, 0x00},
152 	};
153 
154 	return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
155 }
156 
lt8912_write_rxlogicres_config(struct lt8912 * lt)157 static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
158 {
159 	int ret;
160 
161 	ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
162 	usleep_range(10000, 20000);
163 	ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
164 
165 	return ret;
166 };
167 
168 /* enable LVDS output with some hardcoded configuration, not required for the HDMI output */
lt8912_write_lvds_config(struct lt8912 * lt)169 static int lt8912_write_lvds_config(struct lt8912 *lt)
170 {
171 	const struct reg_sequence seq[] = {
172 		// lvds power up
173 		{0x44, 0x30},
174 		{0x51, 0x05},
175 
176 		// core pll bypass
177 		{0x50, 0x24}, // cp=50uA
178 		{0x51, 0x2d}, // Pix_clk as reference, second order passive LPF PLL
179 		{0x52, 0x04}, // loopdiv=0, use second-order PLL
180 		{0x69, 0x0e}, // CP_PRESET_DIV_RATIO
181 		{0x69, 0x8e},
182 		{0x6a, 0x00},
183 		{0x6c, 0xb8}, // RGD_CP_SOFT_K_EN,RGD_CP_SOFT_K[13:8]
184 		{0x6b, 0x51},
185 
186 		{0x04, 0xfb}, // core pll reset
187 		{0x04, 0xff},
188 
189 		// scaler bypass
190 		{0x7f, 0x00}, // disable scaler
191 		{0xa8, 0x13}, // 0x13: JEIDA, 0x33: VESA
192 
193 		{0x02, 0xf7}, // lvds pll reset
194 		{0x02, 0xff},
195 		{0x03, 0xcf},
196 		{0x03, 0xff},
197 	};
198 
199 	return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
200 };
201 
bridge_to_lt8912(struct drm_bridge * b)202 static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
203 {
204 	return container_of(b, struct lt8912, bridge);
205 }
206 
connector_to_lt8912(struct drm_connector * c)207 static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
208 {
209 	return container_of(c, struct lt8912, connector);
210 }
211 
212 static const struct regmap_config lt8912_regmap_config = {
213 	.reg_bits = 8,
214 	.val_bits = 8,
215 	.max_register = 0xff,
216 };
217 
lt8912_init_i2c(struct lt8912 * lt,struct i2c_client * client)218 static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
219 {
220 	unsigned int i;
221 	/*
222 	 * At this time we only initialize 2 chips, but the lt8912 provides
223 	 * a third interface for the audio over HDMI configuration.
224 	 */
225 	struct i2c_board_info info[] = {
226 		{ I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
227 		{ I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
228 	};
229 
230 	if (!lt)
231 		return -ENODEV;
232 
233 	for (i = 0; i < ARRAY_SIZE(info); i++) {
234 		if (i > 0) {
235 			lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
236 								 info[i].addr);
237 			if (IS_ERR(lt->i2c_client[i]))
238 				return PTR_ERR(lt->i2c_client[i]);
239 		}
240 
241 		lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
242 						     &lt8912_regmap_config);
243 		if (IS_ERR(lt->regmap[i]))
244 			return PTR_ERR(lt->regmap[i]);
245 	}
246 	return 0;
247 }
248 
lt8912_free_i2c(struct lt8912 * lt)249 static int lt8912_free_i2c(struct lt8912 *lt)
250 {
251 	unsigned int i;
252 
253 	for (i = 1; i < I2C_MAX_IDX; i++)
254 		i2c_unregister_device(lt->i2c_client[i]);
255 
256 	return 0;
257 }
258 
lt8912_hard_power_on(struct lt8912 * lt)259 static int lt8912_hard_power_on(struct lt8912 *lt)
260 {
261 	gpiod_set_value_cansleep(lt->gp_reset, 0);
262 	msleep(20);
263 
264 	return 0;
265 }
266 
lt8912_hard_power_off(struct lt8912 * lt)267 static void lt8912_hard_power_off(struct lt8912 *lt)
268 {
269 	gpiod_set_value_cansleep(lt->gp_reset, 1);
270 	msleep(20);
271 	lt->is_power_on = false;
272 }
273 
lt8912_video_setup(struct lt8912 * lt)274 static int lt8912_video_setup(struct lt8912 *lt)
275 {
276 	u32 hactive, h_total, hpw, hfp, hbp;
277 	u32 vactive, v_total, vpw, vfp, vbp;
278 	u8 settle = 0x08;
279 	int ret, hsync_activehigh, vsync_activehigh;
280 
281 	if (!lt)
282 		return -EINVAL;
283 
284 	hactive = lt->mode.hactive;
285 	hfp = lt->mode.hfront_porch;
286 	hpw = lt->mode.hsync_len;
287 	hbp = lt->mode.hback_porch;
288 	h_total = hactive + hfp + hpw + hbp;
289 	hsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH;
290 
291 	vactive = lt->mode.vactive;
292 	vfp = lt->mode.vfront_porch;
293 	vpw = lt->mode.vsync_len;
294 	vbp = lt->mode.vback_porch;
295 	v_total = vactive + vfp + vpw + vbp;
296 	vsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH;
297 
298 	if (vactive <= 600)
299 		settle = 0x04;
300 	else if (vactive == 1080)
301 		settle = 0x0a;
302 
303 	ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
304 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
305 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
306 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
307 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
308 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
309 
310 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
311 
312 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
313 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
314 
315 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
316 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
317 
318 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
319 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
320 
321 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
322 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
323 
324 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
325 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
326 
327 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
328 	ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
329 
330 	ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(0),
331 				  vsync_activehigh ? BIT(0) : 0);
332 	ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(1),
333 				  hsync_activehigh ? BIT(1) : 0);
334 	ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xb2, BIT(0),
335 				  lt->connector.display_info.is_hdmi ? BIT(0) : 0);
336 
337 	return ret;
338 }
339 
lt8912_soft_power_on(struct lt8912 * lt)340 static int lt8912_soft_power_on(struct lt8912 *lt)
341 {
342 	if (!lt->is_power_on) {
343 		u32 lanes = lt->data_lanes;
344 
345 		lt8912_write_init_config(lt);
346 		regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
347 
348 		lt8912_write_mipi_basic_config(lt);
349 
350 		lt->is_power_on = true;
351 	}
352 
353 	return 0;
354 }
355 
lt8912_video_on(struct lt8912 * lt)356 static int lt8912_video_on(struct lt8912 *lt)
357 {
358 	int ret;
359 
360 	ret = lt8912_video_setup(lt);
361 	if (ret < 0)
362 		goto end;
363 
364 	ret = lt8912_write_dds_config(lt);
365 	if (ret < 0)
366 		goto end;
367 
368 	ret = lt8912_write_rxlogicres_config(lt);
369 	if (ret < 0)
370 		goto end;
371 
372 	ret = lt8912_write_lvds_config(lt);
373 	if (ret < 0)
374 		goto end;
375 
376 end:
377 	return ret;
378 }
379 
lt8912_check_cable_status(struct lt8912 * lt)380 static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
381 {
382 	int ret;
383 	unsigned int reg_val;
384 
385 	ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, &reg_val);
386 	if (ret)
387 		return connector_status_unknown;
388 
389 	if (reg_val & BIT(7))
390 		return connector_status_connected;
391 
392 	return connector_status_disconnected;
393 }
394 
395 static enum drm_connector_status
lt8912_connector_detect(struct drm_connector * connector,bool force)396 lt8912_connector_detect(struct drm_connector *connector, bool force)
397 {
398 	struct lt8912 *lt = connector_to_lt8912(connector);
399 
400 	if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
401 		return drm_bridge_detect(lt->hdmi_port);
402 
403 	return lt8912_check_cable_status(lt);
404 }
405 
406 static const struct drm_connector_funcs lt8912_connector_funcs = {
407 	.detect = lt8912_connector_detect,
408 	.fill_modes = drm_helper_probe_single_connector_modes,
409 	.destroy = drm_connector_cleanup,
410 	.reset = drm_atomic_helper_connector_reset,
411 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
412 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
413 };
414 
415 static enum drm_mode_status
lt8912_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)416 lt8912_connector_mode_valid(struct drm_connector *connector,
417 			    struct drm_display_mode *mode)
418 {
419 	if (mode->clock > 150000)
420 		return MODE_CLOCK_HIGH;
421 
422 	if (mode->hdisplay > 1920)
423 		return MODE_BAD_HVALUE;
424 
425 	if (mode->vdisplay > 1080)
426 		return MODE_BAD_VVALUE;
427 
428 	return MODE_OK;
429 }
430 
lt8912_connector_get_modes(struct drm_connector * connector)431 static int lt8912_connector_get_modes(struct drm_connector *connector)
432 {
433 	struct edid *edid;
434 	int ret = -1;
435 	int num = 0;
436 	struct lt8912 *lt = connector_to_lt8912(connector);
437 	u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
438 
439 	edid = drm_bridge_get_edid(lt->hdmi_port, connector);
440 	if (edid) {
441 		drm_connector_update_edid_property(connector, edid);
442 		num = drm_add_edid_modes(connector, edid);
443 	} else {
444 		return ret;
445 	}
446 
447 	ret = drm_display_info_set_bus_formats(&connector->display_info,
448 					       &bus_format, 1);
449 	if (ret)
450 		num = ret;
451 
452 	kfree(edid);
453 	return num;
454 }
455 
456 static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
457 	.get_modes = lt8912_connector_get_modes,
458 	.mode_valid = lt8912_connector_mode_valid,
459 };
460 
lt8912_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adj)461 static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
462 				   const struct drm_display_mode *mode,
463 				   const struct drm_display_mode *adj)
464 {
465 	struct lt8912 *lt = bridge_to_lt8912(bridge);
466 
467 	drm_display_mode_to_videomode(adj, &lt->mode);
468 }
469 
lt8912_bridge_enable(struct drm_bridge * bridge)470 static void lt8912_bridge_enable(struct drm_bridge *bridge)
471 {
472 	struct lt8912 *lt = bridge_to_lt8912(bridge);
473 
474 	lt8912_video_on(lt);
475 }
476 
lt8912_attach_dsi(struct lt8912 * lt)477 static int lt8912_attach_dsi(struct lt8912 *lt)
478 {
479 	struct device *dev = lt->dev;
480 	struct mipi_dsi_host *host;
481 	struct mipi_dsi_device *dsi;
482 	int ret = -1;
483 	const struct mipi_dsi_device_info info = { .type = "lt8912",
484 						   .channel = 0,
485 						   .node = NULL,
486 						 };
487 
488 	host = of_find_mipi_dsi_host_by_node(lt->host_node);
489 	if (!host) {
490 		dev_err(dev, "failed to find dsi host\n");
491 		return -EPROBE_DEFER;
492 	}
493 
494 	dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
495 	if (IS_ERR(dsi)) {
496 		ret = PTR_ERR(dsi);
497 		dev_err(dev, "failed to create dsi device (%d)\n", ret);
498 		return ret;
499 	}
500 
501 	lt->dsi = dsi;
502 
503 	dsi->lanes = lt->data_lanes;
504 	dsi->format = MIPI_DSI_FMT_RGB888;
505 
506 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
507 			  MIPI_DSI_MODE_LPM |
508 			  MIPI_DSI_MODE_NO_EOT_PACKET;
509 
510 	ret = devm_mipi_dsi_attach(dev, dsi);
511 	if (ret < 0) {
512 		dev_err(dev, "failed to attach dsi to host\n");
513 		return ret;
514 	}
515 
516 	return 0;
517 }
518 
lt8912_bridge_hpd_cb(void * data,enum drm_connector_status status)519 static void lt8912_bridge_hpd_cb(void *data, enum drm_connector_status status)
520 {
521 	struct lt8912 *lt = data;
522 
523 	if (lt->bridge.dev)
524 		drm_helper_hpd_irq_event(lt->bridge.dev);
525 }
526 
lt8912_bridge_connector_init(struct drm_bridge * bridge)527 static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
528 {
529 	int ret;
530 	struct lt8912 *lt = bridge_to_lt8912(bridge);
531 	struct drm_connector *connector = &lt->connector;
532 
533 	if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD) {
534 		drm_bridge_hpd_enable(lt->hdmi_port, lt8912_bridge_hpd_cb, lt);
535 		connector->polled = DRM_CONNECTOR_POLL_HPD;
536 	} else {
537 		connector->polled = DRM_CONNECTOR_POLL_CONNECT |
538 				    DRM_CONNECTOR_POLL_DISCONNECT;
539 	}
540 
541 	ret = drm_connector_init(bridge->dev, connector,
542 				 &lt8912_connector_funcs,
543 				 lt->hdmi_port->type);
544 	if (ret)
545 		goto exit;
546 
547 	drm_connector_helper_add(connector, &lt8912_connector_helper_funcs);
548 
549 	connector->dpms = DRM_MODE_DPMS_OFF;
550 	drm_connector_attach_encoder(connector, bridge->encoder);
551 
552 exit:
553 	return ret;
554 }
555 
lt8912_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)556 static int lt8912_bridge_attach(struct drm_bridge *bridge,
557 				enum drm_bridge_attach_flags flags)
558 {
559 	struct lt8912 *lt = bridge_to_lt8912(bridge);
560 	int ret;
561 
562 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
563 		ret = lt8912_bridge_connector_init(bridge);
564 		if (ret) {
565 			dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
566 			return ret;
567 		}
568 	}
569 
570 	ret = lt8912_hard_power_on(lt);
571 	if (ret)
572 		return ret;
573 
574 	ret = lt8912_soft_power_on(lt);
575 	if (ret)
576 		goto error;
577 
578 	lt->is_attached = true;
579 
580 	return 0;
581 
582 error:
583 	lt8912_hard_power_off(lt);
584 	return ret;
585 }
586 
lt8912_bridge_detach(struct drm_bridge * bridge)587 static void lt8912_bridge_detach(struct drm_bridge *bridge)
588 {
589 	struct lt8912 *lt = bridge_to_lt8912(bridge);
590 
591 	if (lt->is_attached) {
592 		lt8912_hard_power_off(lt);
593 
594 		if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD)
595 			drm_bridge_hpd_disable(lt->hdmi_port);
596 
597 		drm_connector_unregister(&lt->connector);
598 		drm_connector_cleanup(&lt->connector);
599 	}
600 }
601 
602 static enum drm_connector_status
lt8912_bridge_detect(struct drm_bridge * bridge)603 lt8912_bridge_detect(struct drm_bridge *bridge)
604 {
605 	struct lt8912 *lt = bridge_to_lt8912(bridge);
606 
607 	if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
608 		return drm_bridge_detect(lt->hdmi_port);
609 
610 	return lt8912_check_cable_status(lt);
611 }
612 
lt8912_bridge_get_edid(struct drm_bridge * bridge,struct drm_connector * connector)613 static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
614 					   struct drm_connector *connector)
615 {
616 	struct lt8912 *lt = bridge_to_lt8912(bridge);
617 
618 	/*
619 	 * edid must be read through the ddc bus but it must be
620 	 * given to the hdmi connector node.
621 	 */
622 	if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
623 		return drm_bridge_get_edid(lt->hdmi_port, connector);
624 
625 	dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
626 	return NULL;
627 }
628 
629 static const struct drm_bridge_funcs lt8912_bridge_funcs = {
630 	.attach = lt8912_bridge_attach,
631 	.detach = lt8912_bridge_detach,
632 	.mode_set = lt8912_bridge_mode_set,
633 	.enable = lt8912_bridge_enable,
634 	.detect = lt8912_bridge_detect,
635 	.get_edid = lt8912_bridge_get_edid,
636 };
637 
lt8912_parse_dt(struct lt8912 * lt)638 static int lt8912_parse_dt(struct lt8912 *lt)
639 {
640 	struct gpio_desc *gp_reset;
641 	struct device *dev = lt->dev;
642 	int ret;
643 	int data_lanes;
644 	struct device_node *port_node;
645 
646 	gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
647 	if (IS_ERR(gp_reset)) {
648 		ret = PTR_ERR(gp_reset);
649 		if (ret != -EPROBE_DEFER)
650 			dev_err(dev, "Failed to get reset gpio: %d\n", ret);
651 		return ret;
652 	}
653 	lt->gp_reset = gp_reset;
654 
655 	data_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, -1, 1, 4);
656 	if (data_lanes < 0) {
657 		dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
658 		return data_lanes;
659 	}
660 
661 	lt->data_lanes = data_lanes;
662 
663 	lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
664 	if (!lt->host_node) {
665 		dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
666 		return -ENODEV;
667 	}
668 
669 	port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
670 	if (!port_node) {
671 		dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
672 		ret = -ENODEV;
673 		goto err_free_host_node;
674 	}
675 
676 	lt->hdmi_port = of_drm_find_bridge(port_node);
677 	if (!lt->hdmi_port) {
678 		ret = -EPROBE_DEFER;
679 		dev_err_probe(lt->dev, ret, "%s: Failed to get hdmi port\n", __func__);
680 		goto err_free_host_node;
681 	}
682 
683 	if (!of_device_is_compatible(port_node, "hdmi-connector")) {
684 		dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
685 		ret = -EINVAL;
686 		goto err_free_host_node;
687 	}
688 
689 	of_node_put(port_node);
690 	return 0;
691 
692 err_free_host_node:
693 	of_node_put(port_node);
694 	of_node_put(lt->host_node);
695 	return ret;
696 }
697 
lt8912_put_dt(struct lt8912 * lt)698 static int lt8912_put_dt(struct lt8912 *lt)
699 {
700 	of_node_put(lt->host_node);
701 	return 0;
702 }
703 
lt8912_probe(struct i2c_client * client)704 static int lt8912_probe(struct i2c_client *client)
705 {
706 	static struct lt8912 *lt;
707 	int ret = 0;
708 	struct device *dev = &client->dev;
709 
710 	lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
711 	if (!lt)
712 		return -ENOMEM;
713 
714 	lt->dev = dev;
715 	lt->i2c_client[0] = client;
716 
717 	ret = lt8912_parse_dt(lt);
718 	if (ret)
719 		goto err_dt_parse;
720 
721 	ret = lt8912_init_i2c(lt, client);
722 	if (ret)
723 		goto err_i2c;
724 
725 	i2c_set_clientdata(client, lt);
726 
727 	lt->bridge.funcs = &lt8912_bridge_funcs;
728 	lt->bridge.of_node = dev->of_node;
729 	lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
730 			  DRM_BRIDGE_OP_DETECT);
731 
732 	drm_bridge_add(&lt->bridge);
733 
734 	ret = lt8912_attach_dsi(lt);
735 	if (ret)
736 		goto err_attach;
737 
738 	return 0;
739 
740 err_attach:
741 	drm_bridge_remove(&lt->bridge);
742 	lt8912_free_i2c(lt);
743 err_i2c:
744 	lt8912_put_dt(lt);
745 err_dt_parse:
746 	return ret;
747 }
748 
lt8912_remove(struct i2c_client * client)749 static void lt8912_remove(struct i2c_client *client)
750 {
751 	struct lt8912 *lt = i2c_get_clientdata(client);
752 
753 	lt8912_bridge_detach(&lt->bridge);
754 	drm_bridge_remove(&lt->bridge);
755 	lt8912_free_i2c(lt);
756 	lt8912_put_dt(lt);
757 }
758 
759 static const struct of_device_id lt8912_dt_match[] = {
760 	{.compatible = "lontium,lt8912b"},
761 	{}
762 };
763 MODULE_DEVICE_TABLE(of, lt8912_dt_match);
764 
765 static const struct i2c_device_id lt8912_id[] = {
766 	{"lt8912", 0},
767 	{},
768 };
769 MODULE_DEVICE_TABLE(i2c, lt8912_id);
770 
771 static struct i2c_driver lt8912_i2c_driver = {
772 	.driver = {
773 		.name = "lt8912",
774 		.of_match_table = lt8912_dt_match,
775 	},
776 	.probe = lt8912_probe,
777 	.remove = lt8912_remove,
778 	.id_table = lt8912_id,
779 };
780 module_i2c_driver(lt8912_i2c_driver);
781 
782 MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
783 MODULE_DESCRIPTION("lt8912 drm driver");
784 MODULE_LICENSE("GPL v2");
785