Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
blob741323a2e6c3c3a55d10ab7f6d5d61536c7c2dcd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Exynos DRM Parallel output support.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
7 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
8 */
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>
17 #include <drm/drm_simple_kms_helper.h>
19 #include <video/of_videomode.h>
20 #include <video/videomode.h>
22 #include "exynos_drm_crtc.h"
24 struct exynos_dpi {
25 struct drm_encoder encoder;
26 struct device *dev;
27 struct device_node *panel_node;
29 struct drm_panel *panel;
30 struct drm_connector connector;
32 struct videomode *vm;
35 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
37 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
39 return container_of(e, struct exynos_dpi, encoder);
42 static enum drm_connector_status
43 exynos_dpi_detect(struct drm_connector *connector, bool force)
45 return connector_status_connected;
48 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
50 drm_connector_unregister(connector);
51 drm_connector_cleanup(connector);
54 static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
55 .detect = exynos_dpi_detect,
56 .fill_modes = drm_helper_probe_single_connector_modes,
57 .destroy = exynos_dpi_connector_destroy,
58 .reset = drm_atomic_helper_connector_reset,
59 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
60 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
63 static int exynos_dpi_get_modes(struct drm_connector *connector)
65 struct exynos_dpi *ctx = connector_to_dpi(connector);
67 /* fimd timings gets precedence over panel modes */
68 if (ctx->vm) {
69 struct drm_display_mode *mode;
71 mode = drm_mode_create(connector->dev);
72 if (!mode) {
73 DRM_DEV_ERROR(ctx->dev,
74 "failed to create a new display mode\n");
75 return 0;
77 drm_display_mode_from_videomode(ctx->vm, mode);
78 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
79 drm_mode_probed_add(connector, mode);
80 return 1;
83 if (ctx->panel)
84 return drm_panel_get_modes(ctx->panel, connector);
86 return 0;
89 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
90 .get_modes = exynos_dpi_get_modes,
93 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
95 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
96 struct drm_connector *connector = &ctx->connector;
97 int ret;
99 connector->polled = DRM_CONNECTOR_POLL_HPD;
101 ret = drm_connector_init(encoder->dev, connector,
102 &exynos_dpi_connector_funcs,
103 DRM_MODE_CONNECTOR_VGA);
104 if (ret) {
105 DRM_DEV_ERROR(ctx->dev,
106 "failed to initialize connector with drm\n");
107 return ret;
110 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
111 drm_connector_attach_encoder(connector, encoder);
113 return 0;
116 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
117 struct drm_display_mode *mode,
118 struct drm_display_mode *adjusted_mode)
122 static void exynos_dpi_enable(struct drm_encoder *encoder)
124 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
126 if (ctx->panel) {
127 drm_panel_prepare(ctx->panel);
128 drm_panel_enable(ctx->panel);
132 static void exynos_dpi_disable(struct drm_encoder *encoder)
134 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
136 if (ctx->panel) {
137 drm_panel_disable(ctx->panel);
138 drm_panel_unprepare(ctx->panel);
142 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
143 .mode_set = exynos_dpi_mode_set,
144 .enable = exynos_dpi_enable,
145 .disable = exynos_dpi_disable,
148 enum {
149 FIMD_PORT_IN0,
150 FIMD_PORT_IN1,
151 FIMD_PORT_IN2,
152 FIMD_PORT_RGB,
153 FIMD_PORT_WRB,
156 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
158 struct device *dev = ctx->dev;
159 struct device_node *dn = dev->of_node;
160 struct device_node *np;
162 ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
164 np = of_get_child_by_name(dn, "display-timings");
165 if (np) {
166 struct videomode *vm;
167 int ret;
169 of_node_put(np);
171 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
172 if (!vm)
173 return -ENOMEM;
175 ret = of_get_videomode(dn, vm, 0);
176 if (ret < 0) {
177 devm_kfree(dev, vm);
178 return ret;
181 ctx->vm = vm;
183 return 0;
186 if (!ctx->panel_node)
187 return -EINVAL;
189 return 0;
192 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
194 int ret;
196 drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
198 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
200 ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
201 if (ret < 0)
202 return ret;
204 ret = exynos_dpi_create_connector(encoder);
205 if (ret) {
206 DRM_DEV_ERROR(encoder_to_dpi(encoder)->dev,
207 "failed to create connector ret = %d\n", ret);
208 drm_encoder_cleanup(encoder);
209 return ret;
212 return 0;
215 struct drm_encoder *exynos_dpi_probe(struct device *dev)
217 struct exynos_dpi *ctx;
218 int ret;
220 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
221 if (!ctx)
222 return ERR_PTR(-ENOMEM);
224 ctx->dev = dev;
226 ret = exynos_dpi_parse_dt(ctx);
227 if (ret < 0) {
228 devm_kfree(dev, ctx);
229 return NULL;
232 if (ctx->panel_node) {
233 ctx->panel = of_drm_find_panel(ctx->panel_node);
234 if (IS_ERR(ctx->panel))
235 return ERR_CAST(ctx->panel);
238 return &ctx->encoder;
241 int exynos_dpi_remove(struct drm_encoder *encoder)
243 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
245 exynos_dpi_disable(&ctx->encoder);
247 return 0;