vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
blob2f0babb67c5104be6f1e617d5baa57a64d74499c
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 .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 */
75 if (ctx->vm) {
76 struct drm_display_mode *mode;
78 mode = drm_mode_create(connector->dev);
79 if (!mode) {
80 DRM_ERROR("failed to create a new display mode\n");
81 return 0;
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);
86 return 1;
89 if (ctx->panel)
90 return ctx->panel->funcs->get_modes(ctx->panel);
92 return 0;
95 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
96 .get_modes = exynos_dpi_get_modes,
99 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
101 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
102 struct drm_connector *connector = &ctx->connector;
103 int ret;
105 connector->polled = DRM_CONNECTOR_POLL_HPD;
107 ret = drm_connector_init(encoder->dev, connector,
108 &exynos_dpi_connector_funcs,
109 DRM_MODE_CONNECTOR_VGA);
110 if (ret) {
111 DRM_ERROR("failed to initialize connector with drm\n");
112 return ret;
115 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
116 drm_connector_attach_encoder(connector, encoder);
118 return 0;
121 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
122 struct drm_display_mode *mode,
123 struct drm_display_mode *adjusted_mode)
127 static void exynos_dpi_enable(struct drm_encoder *encoder)
129 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
131 if (ctx->panel) {
132 drm_panel_prepare(ctx->panel);
133 drm_panel_enable(ctx->panel);
137 static void exynos_dpi_disable(struct drm_encoder *encoder)
139 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
141 if (ctx->panel) {
142 drm_panel_disable(ctx->panel);
143 drm_panel_unprepare(ctx->panel);
147 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
148 .mode_set = exynos_dpi_mode_set,
149 .enable = exynos_dpi_enable,
150 .disable = exynos_dpi_disable,
153 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
154 .destroy = drm_encoder_cleanup,
157 enum {
158 FIMD_PORT_IN0,
159 FIMD_PORT_IN1,
160 FIMD_PORT_IN2,
161 FIMD_PORT_RGB,
162 FIMD_PORT_WRB,
165 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
167 struct device *dev = ctx->dev;
168 struct device_node *dn = dev->of_node;
169 struct device_node *np;
171 ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
173 np = of_get_child_by_name(dn, "display-timings");
174 if (np) {
175 struct videomode *vm;
176 int ret;
178 of_node_put(np);
180 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
181 if (!vm)
182 return -ENOMEM;
184 ret = of_get_videomode(dn, vm, 0);
185 if (ret < 0) {
186 devm_kfree(dev, vm);
187 return ret;
190 ctx->vm = vm;
192 return 0;
195 if (!ctx->panel_node)
196 return -EINVAL;
198 return 0;
201 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
203 int ret;
205 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
206 DRM_MODE_ENCODER_TMDS, NULL);
208 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
210 ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
211 if (ret < 0)
212 return ret;
214 ret = exynos_dpi_create_connector(encoder);
215 if (ret) {
216 DRM_ERROR("failed to create connector ret = %d\n", ret);
217 drm_encoder_cleanup(encoder);
218 return ret;
221 return 0;
224 struct drm_encoder *exynos_dpi_probe(struct device *dev)
226 struct exynos_dpi *ctx;
227 int ret;
229 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
230 if (!ctx)
231 return ERR_PTR(-ENOMEM);
233 ctx->dev = dev;
235 ret = exynos_dpi_parse_dt(ctx);
236 if (ret < 0) {
237 devm_kfree(dev, ctx);
238 return NULL;
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);
256 if (ctx->panel)
257 drm_panel_detach(ctx->panel);
259 return 0;