2 * Exynos DRM Parallel output support.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd
6 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_panel.h>
16 #include <drm/drm_atomic_helper.h>
18 #include <linux/regulator/consumer.h>
20 #include <video/of_videomode.h>
21 #include <video/videomode.h>
23 #include "exynos_drm_crtc.h"
26 struct drm_encoder encoder
;
28 struct device_node
*panel_node
;
30 struct drm_panel
*panel
;
31 struct drm_connector connector
;
36 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38 static inline struct exynos_dpi
*encoder_to_dpi(struct drm_encoder
*e
)
40 return container_of(e
, struct exynos_dpi
, encoder
);
43 static enum drm_connector_status
44 exynos_dpi_detect(struct drm_connector
*connector
, bool force
)
46 struct exynos_dpi
*ctx
= connector_to_dpi(connector
);
48 if (ctx
->panel
&& !ctx
->panel
->connector
)
49 drm_panel_attach(ctx
->panel
, &ctx
->connector
);
51 return connector_status_connected
;
54 static void exynos_dpi_connector_destroy(struct drm_connector
*connector
)
56 drm_connector_unregister(connector
);
57 drm_connector_cleanup(connector
);
60 static struct drm_connector_funcs exynos_dpi_connector_funcs
= {
61 .dpms
= drm_atomic_helper_connector_dpms
,
62 .detect
= exynos_dpi_detect
,
63 .fill_modes
= drm_helper_probe_single_connector_modes
,
64 .destroy
= exynos_dpi_connector_destroy
,
65 .reset
= drm_atomic_helper_connector_reset
,
66 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
67 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
70 static int exynos_dpi_get_modes(struct drm_connector
*connector
)
72 struct exynos_dpi
*ctx
= connector_to_dpi(connector
);
74 /* fimd timings gets precedence over panel modes */
76 struct drm_display_mode
*mode
;
78 mode
= drm_mode_create(connector
->dev
);
80 DRM_ERROR("failed to create a new display mode\n");
83 drm_display_mode_from_videomode(ctx
->vm
, mode
);
84 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
85 drm_mode_probed_add(connector
, mode
);
90 return ctx
->panel
->funcs
->get_modes(ctx
->panel
);
95 static struct drm_encoder
*
96 exynos_dpi_best_encoder(struct drm_connector
*connector
)
98 struct exynos_dpi
*ctx
= connector_to_dpi(connector
);
100 return &ctx
->encoder
;
103 static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs
= {
104 .get_modes
= exynos_dpi_get_modes
,
105 .best_encoder
= exynos_dpi_best_encoder
,
108 static int exynos_dpi_create_connector(struct drm_encoder
*encoder
)
110 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
111 struct drm_connector
*connector
= &ctx
->connector
;
114 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
116 ret
= drm_connector_init(encoder
->dev
, connector
,
117 &exynos_dpi_connector_funcs
,
118 DRM_MODE_CONNECTOR_VGA
);
120 DRM_ERROR("failed to initialize connector with drm\n");
124 drm_connector_helper_add(connector
, &exynos_dpi_connector_helper_funcs
);
125 drm_connector_register(connector
);
126 drm_mode_connector_attach_encoder(connector
, encoder
);
131 static bool exynos_dpi_mode_fixup(struct drm_encoder
*encoder
,
132 const struct drm_display_mode
*mode
,
133 struct drm_display_mode
*adjusted_mode
)
138 static void exynos_dpi_mode_set(struct drm_encoder
*encoder
,
139 struct drm_display_mode
*mode
,
140 struct drm_display_mode
*adjusted_mode
)
144 static void exynos_dpi_enable(struct drm_encoder
*encoder
)
146 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
149 drm_panel_prepare(ctx
->panel
);
150 drm_panel_enable(ctx
->panel
);
154 static void exynos_dpi_disable(struct drm_encoder
*encoder
)
156 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
159 drm_panel_disable(ctx
->panel
);
160 drm_panel_unprepare(ctx
->panel
);
164 static struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs
= {
165 .mode_fixup
= exynos_dpi_mode_fixup
,
166 .mode_set
= exynos_dpi_mode_set
,
167 .enable
= exynos_dpi_enable
,
168 .disable
= exynos_dpi_disable
,
171 static struct drm_encoder_funcs exynos_dpi_encoder_funcs
= {
172 .destroy
= drm_encoder_cleanup
,
175 /* of_* functions will be removed after merge of of_graph patches */
176 static struct device_node
*
177 of_get_child_by_name_reg(struct device_node
*parent
, const char *name
, u32 reg
)
179 struct device_node
*np
;
181 for_each_child_of_node(parent
, np
) {
184 if (!np
->name
|| of_node_cmp(np
->name
, name
))
187 if (of_property_read_u32(np
, "reg", &r
) < 0)
197 static struct device_node
*of_graph_get_port_by_reg(struct device_node
*parent
,
200 struct device_node
*ports
, *port
;
202 ports
= of_get_child_by_name(parent
, "ports");
206 port
= of_get_child_by_name_reg(parent
, "port", reg
);
213 static struct device_node
*
214 of_graph_get_endpoint_by_reg(struct device_node
*port
, u32 reg
)
216 return of_get_child_by_name_reg(port
, "endpoint", reg
);
219 static struct device_node
*
220 of_graph_get_remote_port_parent(const struct device_node
*node
)
222 struct device_node
*np
;
225 np
= of_parse_phandle(node
, "remote-endpoint", 0);
227 /* Walk 3 levels up only if there is 'ports' node. */
228 for (depth
= 3; depth
&& np
; depth
--) {
229 np
= of_get_next_parent(np
);
230 if (depth
== 2 && of_node_cmp(np
->name
, "ports"))
244 static struct device_node
*exynos_dpi_of_find_panel_node(struct device
*dev
)
246 struct device_node
*np
, *ep
;
248 np
= of_graph_get_port_by_reg(dev
->of_node
, FIMD_PORT_RGB
);
252 ep
= of_graph_get_endpoint_by_reg(np
, 0);
257 np
= of_graph_get_remote_port_parent(ep
);
263 static int exynos_dpi_parse_dt(struct exynos_dpi
*ctx
)
265 struct device
*dev
= ctx
->dev
;
266 struct device_node
*dn
= dev
->of_node
;
267 struct device_node
*np
;
269 ctx
->panel_node
= exynos_dpi_of_find_panel_node(dev
);
271 np
= of_get_child_by_name(dn
, "display-timings");
273 struct videomode
*vm
;
278 vm
= devm_kzalloc(dev
, sizeof(*ctx
->vm
), GFP_KERNEL
);
282 ret
= of_get_videomode(dn
, vm
, 0);
293 if (!ctx
->panel_node
)
299 int exynos_dpi_bind(struct drm_device
*dev
, struct drm_encoder
*encoder
)
303 ret
= exynos_drm_crtc_get_pipe_from_type(dev
, EXYNOS_DISPLAY_TYPE_LCD
);
307 encoder
->possible_crtcs
= 1 << ret
;
309 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
311 drm_encoder_init(dev
, encoder
, &exynos_dpi_encoder_funcs
,
312 DRM_MODE_ENCODER_TMDS
);
314 drm_encoder_helper_add(encoder
, &exynos_dpi_encoder_helper_funcs
);
316 ret
= exynos_dpi_create_connector(encoder
);
318 DRM_ERROR("failed to create connector ret = %d\n", ret
);
319 drm_encoder_cleanup(encoder
);
326 struct drm_encoder
*exynos_dpi_probe(struct device
*dev
)
328 struct exynos_dpi
*ctx
;
331 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
333 return ERR_PTR(-ENOMEM
);
337 ret
= exynos_dpi_parse_dt(ctx
);
339 devm_kfree(dev
, ctx
);
343 if (ctx
->panel_node
) {
344 ctx
->panel
= of_drm_find_panel(ctx
->panel_node
);
346 return ERR_PTR(-EPROBE_DEFER
);
349 return &ctx
->encoder
;
352 int exynos_dpi_remove(struct drm_encoder
*encoder
)
354 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
356 exynos_dpi_disable(&ctx
->encoder
);
359 drm_panel_detach(ctx
->panel
);