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