Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
blobad6b73c7fc59725e4aa3a3e77246ad348e993a1a
1 /*
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.
13 #include <drm/drmP.h>
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_panel.h>
16 #include <drm/drm_atomic_helper.h>
18 #include <linux/of_graph.h>
19 #include <linux/regulator/consumer.h>
21 #include <video/of_videomode.h>
22 #include <video/videomode.h>
24 #include "exynos_drm_crtc.h"
26 struct exynos_dpi {
27 struct drm_encoder encoder;
28 struct device *dev;
29 struct device_node *panel_node;
31 struct drm_panel *panel;
32 struct drm_connector connector;
34 struct videomode *vm;
37 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
39 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
41 return container_of(e, struct exynos_dpi, encoder);
44 static enum drm_connector_status
45 exynos_dpi_detect(struct drm_connector *connector, bool force)
47 struct exynos_dpi *ctx = connector_to_dpi(connector);
49 if (ctx->panel && !ctx->panel->connector)
50 drm_panel_attach(ctx->panel, &ctx->connector);
52 return connector_status_connected;
55 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
57 drm_connector_unregister(connector);
58 drm_connector_cleanup(connector);
61 static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
62 .dpms = drm_atomic_helper_connector_dpms,
63 .detect = exynos_dpi_detect,
64 .fill_modes = drm_helper_probe_single_connector_modes,
65 .destroy = exynos_dpi_connector_destroy,
66 .reset = drm_atomic_helper_connector_reset,
67 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
68 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
71 static int exynos_dpi_get_modes(struct drm_connector *connector)
73 struct exynos_dpi *ctx = connector_to_dpi(connector);
75 /* fimd timings gets precedence over panel modes */
76 if (ctx->vm) {
77 struct drm_display_mode *mode;
79 mode = drm_mode_create(connector->dev);
80 if (!mode) {
81 DRM_ERROR("failed to create a new display mode\n");
82 return 0;
84 drm_display_mode_from_videomode(ctx->vm, mode);
85 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
86 drm_mode_probed_add(connector, mode);
87 return 1;
90 if (ctx->panel)
91 return ctx->panel->funcs->get_modes(ctx->panel);
93 return 0;
96 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
97 .get_modes = exynos_dpi_get_modes,
100 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
102 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
103 struct drm_connector *connector = &ctx->connector;
104 int ret;
106 connector->polled = DRM_CONNECTOR_POLL_HPD;
108 ret = drm_connector_init(encoder->dev, connector,
109 &exynos_dpi_connector_funcs,
110 DRM_MODE_CONNECTOR_VGA);
111 if (ret) {
112 DRM_ERROR("failed to initialize connector with drm\n");
113 return ret;
116 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
117 drm_connector_register(connector);
118 drm_mode_connector_attach_encoder(connector, encoder);
120 return 0;
123 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
124 struct drm_display_mode *mode,
125 struct drm_display_mode *adjusted_mode)
129 static void exynos_dpi_enable(struct drm_encoder *encoder)
131 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
133 if (ctx->panel) {
134 drm_panel_prepare(ctx->panel);
135 drm_panel_enable(ctx->panel);
139 static void exynos_dpi_disable(struct drm_encoder *encoder)
141 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
143 if (ctx->panel) {
144 drm_panel_disable(ctx->panel);
145 drm_panel_unprepare(ctx->panel);
149 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
150 .mode_set = exynos_dpi_mode_set,
151 .enable = exynos_dpi_enable,
152 .disable = exynos_dpi_disable,
155 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
156 .destroy = drm_encoder_cleanup,
159 enum {
160 FIMD_PORT_IN0,
161 FIMD_PORT_IN1,
162 FIMD_PORT_IN2,
163 FIMD_PORT_RGB,
164 FIMD_PORT_WRB,
167 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
169 struct device_node *np, *ep;
171 ep = of_graph_get_endpoint_by_regs(dev->of_node, FIMD_PORT_RGB, 0);
172 if (!ep)
173 return NULL;
175 np = of_graph_get_remote_port_parent(ep);
176 of_node_put(ep);
178 return np;
181 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
183 struct device *dev = ctx->dev;
184 struct device_node *dn = dev->of_node;
185 struct device_node *np;
187 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
189 np = of_get_child_by_name(dn, "display-timings");
190 if (np) {
191 struct videomode *vm;
192 int ret;
194 of_node_put(np);
196 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
197 if (!vm)
198 return -ENOMEM;
200 ret = of_get_videomode(dn, vm, 0);
201 if (ret < 0) {
202 devm_kfree(dev, vm);
203 return ret;
206 ctx->vm = vm;
208 return 0;
211 if (!ctx->panel_node)
212 return -EINVAL;
214 return 0;
217 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
219 int ret;
221 ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD);
222 if (ret < 0)
223 return ret;
225 encoder->possible_crtcs = 1 << ret;
227 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
229 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
230 DRM_MODE_ENCODER_TMDS, NULL);
232 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
234 ret = exynos_dpi_create_connector(encoder);
235 if (ret) {
236 DRM_ERROR("failed to create connector ret = %d\n", ret);
237 drm_encoder_cleanup(encoder);
238 return ret;
241 return 0;
244 struct drm_encoder *exynos_dpi_probe(struct device *dev)
246 struct exynos_dpi *ctx;
247 int ret;
249 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
250 if (!ctx)
251 return ERR_PTR(-ENOMEM);
253 ctx->dev = dev;
255 ret = exynos_dpi_parse_dt(ctx);
256 if (ret < 0) {
257 devm_kfree(dev, ctx);
258 return NULL;
261 if (ctx->panel_node) {
262 ctx->panel = of_drm_find_panel(ctx->panel_node);
263 if (!ctx->panel)
264 return ERR_PTR(-EPROBE_DEFER);
267 return &ctx->encoder;
270 int exynos_dpi_remove(struct drm_encoder *encoder)
272 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
274 exynos_dpi_disable(&ctx->encoder);
276 if (ctx->panel)
277 drm_panel_detach(ctx->panel);
279 return 0;