1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  * Author:
5  *      Mark Yao <mark.yao@rock-chips.com>
6  *      Sandy Huang <hjc@rock-chips.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/component.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/of_graph.h>
13 #include <linux/phy/phy.h>
14 #include <linux/pinctrl/devinfo.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <linux/reset.h>
19 
20 #include <drm/display/drm_dp_helper.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_bridge.h>
23 #include <drm/drm_bridge_connector.h>
24 #include <drm/drm_of.h>
25 #include <drm/drm_panel.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_simple_kms_helper.h>
28 
29 #include "rockchip_drm_drv.h"
30 #include "rockchip_drm_vop.h"
31 #include "rockchip_lvds.h"
32 
33 #define DISPLAY_OUTPUT_RGB		0
34 #define DISPLAY_OUTPUT_LVDS		1
35 #define DISPLAY_OUTPUT_DUAL_LVDS	2
36 
37 struct rockchip_lvds;
38 
39 /**
40  * struct rockchip_lvds_soc_data - rockchip lvds Soc private data
41  * @probe: LVDS platform probe function
42  * @helper_funcs: LVDS connector helper functions
43  */
44 struct rockchip_lvds_soc_data {
45 	int (*probe)(struct platform_device *pdev, struct rockchip_lvds *lvds);
46 	const struct drm_encoder_helper_funcs *helper_funcs;
47 };
48 
49 struct rockchip_lvds {
50 	struct device *dev;
51 	void __iomem *regs;
52 	struct regmap *grf;
53 	struct clk *pclk;
54 	struct phy *dphy;
55 	const struct rockchip_lvds_soc_data *soc_data;
56 	int output; /* rgb lvds or dual lvds output */
57 	int format; /* vesa or jeida format */
58 	struct drm_device *drm_dev;
59 	struct drm_panel *panel;
60 	struct drm_bridge *bridge;
61 	struct drm_connector connector;
62 	struct rockchip_encoder encoder;
63 	struct dev_pin_info *pins;
64 };
65 
66 static inline struct rockchip_lvds *connector_to_lvds(struct drm_connector *connector)
67 {
68 	return container_of(connector, struct rockchip_lvds, connector);
69 }
70 
71 static inline struct rockchip_lvds *encoder_to_lvds(struct drm_encoder *encoder)
72 {
73 	struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
74 
75 	return container_of(rkencoder, struct rockchip_lvds, encoder);
76 }
77 
78 static inline void rk3288_writel(struct rockchip_lvds *lvds, u32 offset,
79 				 u32 val)
80 {
81 	writel_relaxed(val, lvds->regs + offset);
82 	if (lvds->output == DISPLAY_OUTPUT_LVDS)
83 		return;
84 	writel_relaxed(val, lvds->regs + offset + RK3288_LVDS_CH1_OFFSET);
85 }
86 
87 static inline int rockchip_lvds_name_to_format(const char *s)
88 {
89 	if (strncmp(s, "jeida-18", 8) == 0)
90 		return LVDS_JEIDA_18;
91 	else if (strncmp(s, "jeida-24", 8) == 0)
92 		return LVDS_JEIDA_24;
93 	else if (strncmp(s, "vesa-24", 7) == 0)
94 		return LVDS_VESA_24;
95 
96 	return -EINVAL;
97 }
98 
99 static inline int rockchip_lvds_name_to_output(const char *s)
100 {
101 	if (strncmp(s, "rgb", 3) == 0)
102 		return DISPLAY_OUTPUT_RGB;
103 	else if (strncmp(s, "lvds", 4) == 0)
104 		return DISPLAY_OUTPUT_LVDS;
105 	else if (strncmp(s, "duallvds", 8) == 0)
106 		return DISPLAY_OUTPUT_DUAL_LVDS;
107 
108 	return -EINVAL;
109 }
110 
111 static const struct drm_connector_funcs rockchip_lvds_connector_funcs = {
112 	.fill_modes = drm_helper_probe_single_connector_modes,
113 	.destroy = drm_connector_cleanup,
114 	.reset = drm_atomic_helper_connector_reset,
115 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
116 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
117 };
118 
119 static int rockchip_lvds_connector_get_modes(struct drm_connector *connector)
120 {
121 	struct rockchip_lvds *lvds = connector_to_lvds(connector);
122 	struct drm_panel *panel = lvds->panel;
123 
124 	return drm_panel_get_modes(panel, connector);
125 }
126 
127 static const
128 struct drm_connector_helper_funcs rockchip_lvds_connector_helper_funcs = {
129 	.get_modes = rockchip_lvds_connector_get_modes,
130 };
131 
132 static int
133 rockchip_lvds_encoder_atomic_check(struct drm_encoder *encoder,
134 				   struct drm_crtc_state *crtc_state,
135 				   struct drm_connector_state *conn_state)
136 {
137 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
138 
139 	s->output_mode = ROCKCHIP_OUT_MODE_P888;
140 	s->output_type = DRM_MODE_CONNECTOR_LVDS;
141 
142 	return 0;
143 }
144 
145 static int rk3288_lvds_poweron(struct rockchip_lvds *lvds)
146 {
147 	int ret;
148 	u32 val;
149 
150 	ret = clk_enable(lvds->pclk);
151 	if (ret < 0) {
152 		DRM_DEV_ERROR(lvds->dev, "failed to enable lvds pclk %d\n", ret);
153 		return ret;
154 	}
155 	ret = pm_runtime_resume_and_get(lvds->dev);
156 	if (ret < 0) {
157 		DRM_DEV_ERROR(lvds->dev, "failed to get pm runtime: %d\n", ret);
158 		clk_disable(lvds->pclk);
159 		return ret;
160 	}
161 	val = RK3288_LVDS_CH0_REG0_LANE4_EN | RK3288_LVDS_CH0_REG0_LANE3_EN |
162 		RK3288_LVDS_CH0_REG0_LANE2_EN | RK3288_LVDS_CH0_REG0_LANE1_EN |
163 		RK3288_LVDS_CH0_REG0_LANE0_EN;
164 	if (lvds->output == DISPLAY_OUTPUT_RGB) {
165 		val |= RK3288_LVDS_CH0_REG0_TTL_EN |
166 			RK3288_LVDS_CH0_REG0_LANECK_EN;
167 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG0, val);
168 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG2,
169 			      RK3288_LVDS_PLL_FBDIV_REG2(0x46));
170 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG4,
171 			      RK3288_LVDS_CH0_REG4_LANECK_TTL_MODE |
172 			      RK3288_LVDS_CH0_REG4_LANE4_TTL_MODE |
173 			      RK3288_LVDS_CH0_REG4_LANE3_TTL_MODE |
174 			      RK3288_LVDS_CH0_REG4_LANE2_TTL_MODE |
175 			      RK3288_LVDS_CH0_REG4_LANE1_TTL_MODE |
176 			      RK3288_LVDS_CH0_REG4_LANE0_TTL_MODE);
177 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG5,
178 			      RK3288_LVDS_CH0_REG5_LANECK_TTL_DATA |
179 			      RK3288_LVDS_CH0_REG5_LANE4_TTL_DATA |
180 			      RK3288_LVDS_CH0_REG5_LANE3_TTL_DATA |
181 			      RK3288_LVDS_CH0_REG5_LANE2_TTL_DATA |
182 			      RK3288_LVDS_CH0_REG5_LANE1_TTL_DATA |
183 			      RK3288_LVDS_CH0_REG5_LANE0_TTL_DATA);
184 	} else {
185 		val |= RK3288_LVDS_CH0_REG0_LVDS_EN |
186 			    RK3288_LVDS_CH0_REG0_LANECK_EN;
187 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG0, val);
188 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG1,
189 			      RK3288_LVDS_CH0_REG1_LANECK_BIAS |
190 			      RK3288_LVDS_CH0_REG1_LANE4_BIAS |
191 			      RK3288_LVDS_CH0_REG1_LANE3_BIAS |
192 			      RK3288_LVDS_CH0_REG1_LANE2_BIAS |
193 			      RK3288_LVDS_CH0_REG1_LANE1_BIAS |
194 			      RK3288_LVDS_CH0_REG1_LANE0_BIAS);
195 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG2,
196 			      RK3288_LVDS_CH0_REG2_RESERVE_ON |
197 			      RK3288_LVDS_CH0_REG2_LANECK_LVDS_MODE |
198 			      RK3288_LVDS_CH0_REG2_LANE4_LVDS_MODE |
199 			      RK3288_LVDS_CH0_REG2_LANE3_LVDS_MODE |
200 			      RK3288_LVDS_CH0_REG2_LANE2_LVDS_MODE |
201 			      RK3288_LVDS_CH0_REG2_LANE1_LVDS_MODE |
202 			      RK3288_LVDS_CH0_REG2_LANE0_LVDS_MODE |
203 			      RK3288_LVDS_PLL_FBDIV_REG2(0x46));
204 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG4, 0x00);
205 		rk3288_writel(lvds, RK3288_LVDS_CH0_REG5, 0x00);
206 	}
207 	rk3288_writel(lvds, RK3288_LVDS_CH0_REG3,
208 		      RK3288_LVDS_PLL_FBDIV_REG3(0x46));
209 	rk3288_writel(lvds, RK3288_LVDS_CH0_REGD,
210 		      RK3288_LVDS_PLL_PREDIV_REGD(0x0a));
211 	rk3288_writel(lvds, RK3288_LVDS_CH0_REG20,
212 		      RK3288_LVDS_CH0_REG20_LSB);
213 
214 	rk3288_writel(lvds, RK3288_LVDS_CFG_REGC,
215 		      RK3288_LVDS_CFG_REGC_PLL_ENABLE);
216 	rk3288_writel(lvds, RK3288_LVDS_CFG_REG21,
217 		      RK3288_LVDS_CFG_REG21_TX_ENABLE);
218 
219 	return 0;
220 }
221 
222 static void rk3288_lvds_poweroff(struct rockchip_lvds *lvds)
223 {
224 	int ret;
225 	u32 val;
226 
227 	rk3288_writel(lvds, RK3288_LVDS_CFG_REG21,
228 		      RK3288_LVDS_CFG_REG21_TX_ENABLE);
229 	rk3288_writel(lvds, RK3288_LVDS_CFG_REGC,
230 		      RK3288_LVDS_CFG_REGC_PLL_ENABLE);
231 	val = LVDS_DUAL | LVDS_TTL_EN | LVDS_CH0_EN | LVDS_CH1_EN | LVDS_PWRDN;
232 	val |= val << 16;
233 	ret = regmap_write(lvds->grf, RK3288_LVDS_GRF_SOC_CON7, val);
234 	if (ret != 0)
235 		DRM_DEV_ERROR(lvds->dev, "Could not write to GRF: %d\n", ret);
236 
237 	pm_runtime_put(lvds->dev);
238 	clk_disable(lvds->pclk);
239 }
240 
241 static int rk3288_lvds_grf_config(struct drm_encoder *encoder,
242 				  struct drm_display_mode *mode)
243 {
244 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
245 	u8 pin_hsync = (mode->flags & DRM_MODE_FLAG_PHSYNC) ? 1 : 0;
246 	u8 pin_dclk = (mode->flags & DRM_MODE_FLAG_PCSYNC) ? 1 : 0;
247 	u32 val;
248 	int ret;
249 
250 	/* iomux to LCD data/sync mode */
251 	if (lvds->output == DISPLAY_OUTPUT_RGB)
252 		if (lvds->pins && !IS_ERR(lvds->pins->default_state))
253 			pinctrl_select_state(lvds->pins->p,
254 					     lvds->pins->default_state);
255 	val = lvds->format | LVDS_CH0_EN;
256 	if (lvds->output == DISPLAY_OUTPUT_RGB)
257 		val |= LVDS_TTL_EN | LVDS_CH1_EN;
258 	else if (lvds->output == DISPLAY_OUTPUT_DUAL_LVDS)
259 		val |= LVDS_DUAL | LVDS_CH1_EN;
260 
261 	if ((mode->htotal - mode->hsync_start) & 0x01)
262 		val |= LVDS_START_PHASE_RST_1;
263 
264 	val |= (pin_dclk << 8) | (pin_hsync << 9);
265 	val |= (0xffff << 16);
266 	ret = regmap_write(lvds->grf, RK3288_LVDS_GRF_SOC_CON7, val);
267 	if (ret)
268 		DRM_DEV_ERROR(lvds->dev, "Could not write to GRF: %d\n", ret);
269 
270 	return ret;
271 }
272 
273 static int rk3288_lvds_set_vop_source(struct rockchip_lvds *lvds,
274 				      struct drm_encoder *encoder)
275 {
276 	u32 val;
277 	int ret;
278 
279 	ret = drm_of_encoder_active_endpoint_id(lvds->dev->of_node, encoder);
280 	if (ret < 0)
281 		return ret;
282 
283 	val = RK3288_LVDS_SOC_CON6_SEL_VOP_LIT << 16;
284 	if (ret)
285 		val |= RK3288_LVDS_SOC_CON6_SEL_VOP_LIT;
286 
287 	ret = regmap_write(lvds->grf, RK3288_LVDS_GRF_SOC_CON6, val);
288 	if (ret < 0)
289 		return ret;
290 
291 	return 0;
292 }
293 
294 static void rk3288_lvds_encoder_enable(struct drm_encoder *encoder)
295 {
296 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
297 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
298 	int ret;
299 
300 	drm_panel_prepare(lvds->panel);
301 
302 	ret = rk3288_lvds_poweron(lvds);
303 	if (ret < 0) {
304 		DRM_DEV_ERROR(lvds->dev, "failed to power on LVDS: %d\n", ret);
305 		drm_panel_unprepare(lvds->panel);
306 		return;
307 	}
308 
309 	ret = rk3288_lvds_grf_config(encoder, mode);
310 	if (ret) {
311 		DRM_DEV_ERROR(lvds->dev, "failed to configure LVDS: %d\n", ret);
312 		drm_panel_unprepare(lvds->panel);
313 		return;
314 	}
315 
316 	ret = rk3288_lvds_set_vop_source(lvds, encoder);
317 	if (ret) {
318 		DRM_DEV_ERROR(lvds->dev, "failed to set VOP source: %d\n", ret);
319 		drm_panel_unprepare(lvds->panel);
320 		return;
321 	}
322 
323 	drm_panel_enable(lvds->panel);
324 }
325 
326 static void rk3288_lvds_encoder_disable(struct drm_encoder *encoder)
327 {
328 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
329 
330 	drm_panel_disable(lvds->panel);
331 	rk3288_lvds_poweroff(lvds);
332 	drm_panel_unprepare(lvds->panel);
333 }
334 
335 static int px30_lvds_poweron(struct rockchip_lvds *lvds)
336 {
337 	int ret;
338 
339 	ret = pm_runtime_resume_and_get(lvds->dev);
340 	if (ret < 0) {
341 		DRM_DEV_ERROR(lvds->dev, "failed to get pm runtime: %d\n", ret);
342 		return ret;
343 	}
344 
345 	/* Enable LVDS mode */
346 	ret = regmap_update_bits(lvds->grf, PX30_LVDS_GRF_PD_VO_CON1,
347 				  PX30_LVDS_MODE_EN(1) | PX30_LVDS_P2S_EN(1),
348 				  PX30_LVDS_MODE_EN(1) | PX30_LVDS_P2S_EN(1));
349 	if (ret)
350 		pm_runtime_put(lvds->dev);
351 
352 	return ret;
353 }
354 
355 static void px30_lvds_poweroff(struct rockchip_lvds *lvds)
356 {
357 	regmap_update_bits(lvds->grf, PX30_LVDS_GRF_PD_VO_CON1,
358 			   PX30_LVDS_MODE_EN(1) | PX30_LVDS_P2S_EN(1),
359 			   PX30_LVDS_MODE_EN(0) | PX30_LVDS_P2S_EN(0));
360 
361 	pm_runtime_put(lvds->dev);
362 }
363 
364 static int px30_lvds_grf_config(struct drm_encoder *encoder,
365 				struct drm_display_mode *mode)
366 {
367 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
368 
369 	if (lvds->output != DISPLAY_OUTPUT_LVDS) {
370 		DRM_DEV_ERROR(lvds->dev, "Unsupported display output %d\n",
371 			      lvds->output);
372 		return -EINVAL;
373 	}
374 
375 	/* Set format */
376 	return regmap_update_bits(lvds->grf, PX30_LVDS_GRF_PD_VO_CON1,
377 				  PX30_LVDS_FORMAT(lvds->format),
378 				  PX30_LVDS_FORMAT(lvds->format));
379 }
380 
381 static int px30_lvds_set_vop_source(struct rockchip_lvds *lvds,
382 				    struct drm_encoder *encoder)
383 {
384 	int vop;
385 
386 	vop = drm_of_encoder_active_endpoint_id(lvds->dev->of_node, encoder);
387 	if (vop < 0)
388 		return vop;
389 
390 	return regmap_update_bits(lvds->grf, PX30_LVDS_GRF_PD_VO_CON1,
391 				  PX30_LVDS_VOP_SEL(1),
392 				  PX30_LVDS_VOP_SEL(vop));
393 }
394 
395 static void px30_lvds_encoder_enable(struct drm_encoder *encoder)
396 {
397 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
398 	struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
399 	int ret;
400 
401 	drm_panel_prepare(lvds->panel);
402 
403 	ret = px30_lvds_poweron(lvds);
404 	if (ret) {
405 		DRM_DEV_ERROR(lvds->dev, "failed to power on LVDS: %d\n", ret);
406 		drm_panel_unprepare(lvds->panel);
407 		return;
408 	}
409 
410 	ret = px30_lvds_grf_config(encoder, mode);
411 	if (ret) {
412 		DRM_DEV_ERROR(lvds->dev, "failed to configure LVDS: %d\n", ret);
413 		drm_panel_unprepare(lvds->panel);
414 		return;
415 	}
416 
417 	ret = px30_lvds_set_vop_source(lvds, encoder);
418 	if (ret) {
419 		DRM_DEV_ERROR(lvds->dev, "failed to set VOP source: %d\n", ret);
420 		drm_panel_unprepare(lvds->panel);
421 		return;
422 	}
423 
424 	drm_panel_enable(lvds->panel);
425 }
426 
427 static void px30_lvds_encoder_disable(struct drm_encoder *encoder)
428 {
429 	struct rockchip_lvds *lvds = encoder_to_lvds(encoder);
430 
431 	drm_panel_disable(lvds->panel);
432 	px30_lvds_poweroff(lvds);
433 	drm_panel_unprepare(lvds->panel);
434 }
435 
436 static const
437 struct drm_encoder_helper_funcs rk3288_lvds_encoder_helper_funcs = {
438 	.enable = rk3288_lvds_encoder_enable,
439 	.disable = rk3288_lvds_encoder_disable,
440 	.atomic_check = rockchip_lvds_encoder_atomic_check,
441 };
442 
443 static const
444 struct drm_encoder_helper_funcs px30_lvds_encoder_helper_funcs = {
445 	.enable = px30_lvds_encoder_enable,
446 	.disable = px30_lvds_encoder_disable,
447 	.atomic_check = rockchip_lvds_encoder_atomic_check,
448 };
449 
450 static int rk3288_lvds_probe(struct platform_device *pdev,
451 			     struct rockchip_lvds *lvds)
452 {
453 	int ret;
454 
455 	lvds->regs = devm_platform_ioremap_resource(pdev, 0);
456 	if (IS_ERR(lvds->regs))
457 		return PTR_ERR(lvds->regs);
458 
459 	lvds->pclk = devm_clk_get(lvds->dev, "pclk_lvds");
460 	if (IS_ERR(lvds->pclk)) {
461 		DRM_DEV_ERROR(lvds->dev, "could not get pclk_lvds\n");
462 		return PTR_ERR(lvds->pclk);
463 	}
464 
465 	lvds->pins = devm_kzalloc(lvds->dev, sizeof(*lvds->pins),
466 				  GFP_KERNEL);
467 	if (!lvds->pins)
468 		return -ENOMEM;
469 
470 	lvds->pins->p = devm_pinctrl_get(lvds->dev);
471 	if (IS_ERR(lvds->pins->p)) {
472 		DRM_DEV_ERROR(lvds->dev, "no pinctrl handle\n");
473 		devm_kfree(lvds->dev, lvds->pins);
474 		lvds->pins = NULL;
475 	} else {
476 		lvds->pins->default_state =
477 			pinctrl_lookup_state(lvds->pins->p, "lcdc");
478 		if (IS_ERR(lvds->pins->default_state)) {
479 			DRM_DEV_ERROR(lvds->dev, "no default pinctrl state\n");
480 			devm_kfree(lvds->dev, lvds->pins);
481 			lvds->pins = NULL;
482 		}
483 	}
484 
485 	ret = clk_prepare(lvds->pclk);
486 	if (ret < 0) {
487 		DRM_DEV_ERROR(lvds->dev, "failed to prepare pclk_lvds\n");
488 		return ret;
489 	}
490 
491 	return 0;
492 }
493 
494 static int px30_lvds_probe(struct platform_device *pdev,
495 			   struct rockchip_lvds *lvds)
496 {
497 	int ret;
498 
499 	/* MSB */
500 	ret =  regmap_update_bits(lvds->grf, PX30_LVDS_GRF_PD_VO_CON1,
501 				  PX30_LVDS_MSBSEL(1),
502 				  PX30_LVDS_MSBSEL(1));
503 	if (ret)
504 		return ret;
505 
506 	/* PHY */
507 	lvds->dphy = devm_phy_get(&pdev->dev, "dphy");
508 	if (IS_ERR(lvds->dphy))
509 		return PTR_ERR(lvds->dphy);
510 
511 	ret = phy_init(lvds->dphy);
512 	if (ret)
513 		return ret;
514 
515 	ret = phy_set_mode(lvds->dphy, PHY_MODE_LVDS);
516 	if (ret)
517 		return ret;
518 
519 	return phy_power_on(lvds->dphy);
520 }
521 
522 static const struct rockchip_lvds_soc_data rk3288_lvds_data = {
523 	.probe = rk3288_lvds_probe,
524 	.helper_funcs = &rk3288_lvds_encoder_helper_funcs,
525 };
526 
527 static const struct rockchip_lvds_soc_data px30_lvds_data = {
528 	.probe = px30_lvds_probe,
529 	.helper_funcs = &px30_lvds_encoder_helper_funcs,
530 };
531 
532 static const struct of_device_id rockchip_lvds_dt_ids[] = {
533 	{
534 		.compatible = "rockchip,rk3288-lvds",
535 		.data = &rk3288_lvds_data
536 	},
537 	{
538 		.compatible = "rockchip,px30-lvds",
539 		.data = &px30_lvds_data
540 	},
541 	{}
542 };
543 MODULE_DEVICE_TABLE(of, rockchip_lvds_dt_ids);
544 
545 static int rockchip_lvds_bind(struct device *dev, struct device *master,
546 			      void *data)
547 {
548 	struct rockchip_lvds *lvds = dev_get_drvdata(dev);
549 	struct drm_device *drm_dev = data;
550 	struct drm_encoder *encoder;
551 	struct drm_connector *connector;
552 	struct device_node *remote = NULL;
553 	struct device_node  *port, *endpoint;
554 	int ret = 0, child_count = 0;
555 	const char *name;
556 	u32 endpoint_id = 0;
557 
558 	lvds->drm_dev = drm_dev;
559 	port = of_graph_get_port_by_id(dev->of_node, 1);
560 	if (!port) {
561 		DRM_DEV_ERROR(dev,
562 			      "can't found port point, please init lvds panel port!\n");
563 		return -EINVAL;
564 	}
565 	for_each_child_of_node(port, endpoint) {
566 		child_count++;
567 		of_property_read_u32(endpoint, "reg", &endpoint_id);
568 		ret = drm_of_find_panel_or_bridge(dev->of_node, 1, endpoint_id,
569 						  &lvds->panel, &lvds->bridge);
570 		if (!ret) {
571 			of_node_put(endpoint);
572 			break;
573 		}
574 	}
575 	if (!child_count) {
576 		DRM_DEV_ERROR(dev, "lvds port does not have any children\n");
577 		ret = -EINVAL;
578 		goto err_put_port;
579 	} else if (ret) {
580 		DRM_DEV_ERROR(dev, "failed to find panel and bridge node\n");
581 		ret = -EPROBE_DEFER;
582 		goto err_put_port;
583 	}
584 	if (lvds->panel)
585 		remote = lvds->panel->dev->of_node;
586 	else
587 		remote = lvds->bridge->of_node;
588 	if (of_property_read_string(dev->of_node, "rockchip,output", &name))
589 		/* default set it as output rgb */
590 		lvds->output = DISPLAY_OUTPUT_RGB;
591 	else
592 		lvds->output = rockchip_lvds_name_to_output(name);
593 
594 	if (lvds->output < 0) {
595 		DRM_DEV_ERROR(dev, "invalid output type [%s]\n", name);
596 		ret = lvds->output;
597 		goto err_put_remote;
598 	}
599 
600 	if (of_property_read_string(remote, "data-mapping", &name))
601 		/* default set it as format vesa 18 */
602 		lvds->format = LVDS_VESA_18;
603 	else
604 		lvds->format = rockchip_lvds_name_to_format(name);
605 
606 	if (lvds->format < 0) {
607 		DRM_DEV_ERROR(dev, "invalid data-mapping format [%s]\n", name);
608 		ret = lvds->format;
609 		goto err_put_remote;
610 	}
611 
612 	encoder = &lvds->encoder.encoder;
613 	encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
614 							     dev->of_node);
615 
616 	ret = drm_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_LVDS);
617 	if (ret < 0) {
618 		DRM_DEV_ERROR(drm_dev->dev,
619 			      "failed to initialize encoder: %d\n", ret);
620 		goto err_put_remote;
621 	}
622 
623 	drm_encoder_helper_add(encoder, lvds->soc_data->helper_funcs);
624 	connector = &lvds->connector;
625 
626 	if (lvds->panel) {
627 		connector->dpms = DRM_MODE_DPMS_OFF;
628 		ret = drm_connector_init(drm_dev, connector,
629 					 &rockchip_lvds_connector_funcs,
630 					 DRM_MODE_CONNECTOR_LVDS);
631 		if (ret < 0) {
632 			DRM_DEV_ERROR(drm_dev->dev,
633 				      "failed to initialize connector: %d\n", ret);
634 			goto err_free_encoder;
635 		}
636 
637 		drm_connector_helper_add(connector,
638 					 &rockchip_lvds_connector_helper_funcs);
639 	} else {
640 		ret = drm_bridge_attach(encoder, lvds->bridge, NULL,
641 					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
642 		if (ret)
643 			goto err_free_encoder;
644 
645 		connector = drm_bridge_connector_init(lvds->drm_dev, encoder);
646 		if (IS_ERR(connector)) {
647 			DRM_DEV_ERROR(drm_dev->dev,
648 				      "failed to initialize bridge connector: %pe\n",
649 				      connector);
650 			ret = PTR_ERR(connector);
651 			goto err_free_encoder;
652 		}
653 	}
654 
655 	ret = drm_connector_attach_encoder(connector, encoder);
656 	if (ret < 0) {
657 		DRM_DEV_ERROR(drm_dev->dev,
658 			      "failed to attach encoder: %d\n", ret);
659 		goto err_free_connector;
660 	}
661 
662 	pm_runtime_enable(dev);
663 	of_node_put(remote);
664 	of_node_put(port);
665 
666 	return 0;
667 
668 err_free_connector:
669 	drm_connector_cleanup(connector);
670 err_free_encoder:
671 	drm_encoder_cleanup(encoder);
672 err_put_remote:
673 	of_node_put(remote);
674 err_put_port:
675 	of_node_put(port);
676 
677 	return ret;
678 }
679 
680 static void rockchip_lvds_unbind(struct device *dev, struct device *master,
681 				void *data)
682 {
683 	struct rockchip_lvds *lvds = dev_get_drvdata(dev);
684 	const struct drm_encoder_helper_funcs *encoder_funcs;
685 
686 	encoder_funcs = lvds->soc_data->helper_funcs;
687 	encoder_funcs->disable(&lvds->encoder.encoder);
688 	pm_runtime_disable(dev);
689 	drm_connector_cleanup(&lvds->connector);
690 	drm_encoder_cleanup(&lvds->encoder.encoder);
691 }
692 
693 static const struct component_ops rockchip_lvds_component_ops = {
694 	.bind = rockchip_lvds_bind,
695 	.unbind = rockchip_lvds_unbind,
696 };
697 
698 static int rockchip_lvds_probe(struct platform_device *pdev)
699 {
700 	struct device *dev = &pdev->dev;
701 	struct rockchip_lvds *lvds;
702 	const struct of_device_id *match;
703 	int ret;
704 
705 	if (!dev->of_node)
706 		return -ENODEV;
707 
708 	lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
709 	if (!lvds)
710 		return -ENOMEM;
711 
712 	lvds->dev = dev;
713 	match = of_match_node(rockchip_lvds_dt_ids, dev->of_node);
714 	if (!match)
715 		return -ENODEV;
716 	lvds->soc_data = match->data;
717 
718 	lvds->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
719 						    "rockchip,grf");
720 	if (IS_ERR(lvds->grf)) {
721 		DRM_DEV_ERROR(dev, "missing rockchip,grf property\n");
722 		return PTR_ERR(lvds->grf);
723 	}
724 
725 	ret = lvds->soc_data->probe(pdev, lvds);
726 	if (ret) {
727 		DRM_DEV_ERROR(dev, "Platform initialization failed\n");
728 		return ret;
729 	}
730 
731 	dev_set_drvdata(dev, lvds);
732 
733 	ret = component_add(&pdev->dev, &rockchip_lvds_component_ops);
734 	if (ret < 0) {
735 		DRM_DEV_ERROR(dev, "failed to add component\n");
736 		clk_unprepare(lvds->pclk);
737 	}
738 
739 	return ret;
740 }
741 
742 static int rockchip_lvds_remove(struct platform_device *pdev)
743 {
744 	struct rockchip_lvds *lvds = platform_get_drvdata(pdev);
745 
746 	component_del(&pdev->dev, &rockchip_lvds_component_ops);
747 	clk_unprepare(lvds->pclk);
748 
749 	return 0;
750 }
751 
752 struct platform_driver rockchip_lvds_driver = {
753 	.probe = rockchip_lvds_probe,
754 	.remove = rockchip_lvds_remove,
755 	.driver = {
756 		   .name = "rockchip-lvds",
757 		   .of_match_table = of_match_ptr(rockchip_lvds_dt_ids),
758 	},
759 };
760