1 // SPDX-License-Identifier: GPL-2.0-only
3 * Exynos DRM Parallel output support.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
7 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
10 #include <linux/of_graph.h>
11 #include <linux/regulator/consumer.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_panel.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
18 #include <video/of_videomode.h>
19 #include <video/videomode.h>
21 #include "exynos_drm_crtc.h"
24 struct drm_encoder encoder
;
26 struct device_node
*panel_node
;
28 struct drm_panel
*panel
;
29 struct drm_connector connector
;
34 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
36 static inline struct exynos_dpi
*encoder_to_dpi(struct drm_encoder
*e
)
38 return container_of(e
, struct exynos_dpi
, encoder
);
41 static enum drm_connector_status
42 exynos_dpi_detect(struct drm_connector
*connector
, bool force
)
44 struct exynos_dpi
*ctx
= connector_to_dpi(connector
);
47 drm_panel_attach(ctx
->panel
, &ctx
->connector
);
49 return connector_status_connected
;
52 static void exynos_dpi_connector_destroy(struct drm_connector
*connector
)
54 drm_connector_unregister(connector
);
55 drm_connector_cleanup(connector
);
58 static const struct drm_connector_funcs exynos_dpi_connector_funcs
= {
59 .detect
= exynos_dpi_detect
,
60 .fill_modes
= drm_helper_probe_single_connector_modes
,
61 .destroy
= exynos_dpi_connector_destroy
,
62 .reset
= drm_atomic_helper_connector_reset
,
63 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
64 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
67 static int exynos_dpi_get_modes(struct drm_connector
*connector
)
69 struct exynos_dpi
*ctx
= connector_to_dpi(connector
);
71 /* fimd timings gets precedence over panel modes */
73 struct drm_display_mode
*mode
;
75 mode
= drm_mode_create(connector
->dev
);
77 DRM_DEV_ERROR(ctx
->dev
,
78 "failed to create a new display mode\n");
81 drm_display_mode_from_videomode(ctx
->vm
, mode
);
82 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
83 drm_mode_probed_add(connector
, mode
);
88 return drm_panel_get_modes(ctx
->panel
, connector
);
93 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs
= {
94 .get_modes
= exynos_dpi_get_modes
,
97 static int exynos_dpi_create_connector(struct drm_encoder
*encoder
)
99 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
100 struct drm_connector
*connector
= &ctx
->connector
;
103 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
105 ret
= drm_connector_init(encoder
->dev
, connector
,
106 &exynos_dpi_connector_funcs
,
107 DRM_MODE_CONNECTOR_VGA
);
109 DRM_DEV_ERROR(ctx
->dev
,
110 "failed to initialize connector with drm\n");
114 drm_connector_helper_add(connector
, &exynos_dpi_connector_helper_funcs
);
115 drm_connector_attach_encoder(connector
, encoder
);
120 static void exynos_dpi_mode_set(struct drm_encoder
*encoder
,
121 struct drm_display_mode
*mode
,
122 struct drm_display_mode
*adjusted_mode
)
126 static void exynos_dpi_enable(struct drm_encoder
*encoder
)
128 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
131 drm_panel_prepare(ctx
->panel
);
132 drm_panel_enable(ctx
->panel
);
136 static void exynos_dpi_disable(struct drm_encoder
*encoder
)
138 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
141 drm_panel_disable(ctx
->panel
);
142 drm_panel_unprepare(ctx
->panel
);
146 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs
= {
147 .mode_set
= exynos_dpi_mode_set
,
148 .enable
= exynos_dpi_enable
,
149 .disable
= exynos_dpi_disable
,
152 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs
= {
153 .destroy
= drm_encoder_cleanup
,
164 static int exynos_dpi_parse_dt(struct exynos_dpi
*ctx
)
166 struct device
*dev
= ctx
->dev
;
167 struct device_node
*dn
= dev
->of_node
;
168 struct device_node
*np
;
170 ctx
->panel_node
= of_graph_get_remote_node(dn
, FIMD_PORT_RGB
, 0);
172 np
= of_get_child_by_name(dn
, "display-timings");
174 struct videomode
*vm
;
179 vm
= devm_kzalloc(dev
, sizeof(*ctx
->vm
), GFP_KERNEL
);
183 ret
= of_get_videomode(dn
, vm
, 0);
194 if (!ctx
->panel_node
)
200 int exynos_dpi_bind(struct drm_device
*dev
, struct drm_encoder
*encoder
)
204 drm_encoder_init(dev
, encoder
, &exynos_dpi_encoder_funcs
,
205 DRM_MODE_ENCODER_TMDS
, NULL
);
207 drm_encoder_helper_add(encoder
, &exynos_dpi_encoder_helper_funcs
);
209 ret
= exynos_drm_set_possible_crtcs(encoder
, EXYNOS_DISPLAY_TYPE_LCD
);
213 ret
= exynos_dpi_create_connector(encoder
);
215 DRM_DEV_ERROR(encoder_to_dpi(encoder
)->dev
,
216 "failed to create connector ret = %d\n", ret
);
217 drm_encoder_cleanup(encoder
);
224 struct drm_encoder
*exynos_dpi_probe(struct device
*dev
)
226 struct exynos_dpi
*ctx
;
229 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
231 return ERR_PTR(-ENOMEM
);
235 ret
= exynos_dpi_parse_dt(ctx
);
237 devm_kfree(dev
, ctx
);
241 if (ctx
->panel_node
) {
242 ctx
->panel
= of_drm_find_panel(ctx
->panel_node
);
243 if (IS_ERR(ctx
->panel
))
244 return ERR_CAST(ctx
->panel
);
247 return &ctx
->encoder
;
250 int exynos_dpi_remove(struct drm_encoder
*encoder
)
252 struct exynos_dpi
*ctx
= encoder_to_dpi(encoder
);
254 exynos_dpi_disable(&ctx
->encoder
);
257 drm_panel_detach(ctx
->panel
);