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