Merge tag 'pwm/for-4.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[deliverable/linux.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
CommitLineData
14b6873a
AH
1/*
2 * Exynos DRM Parallel output support.
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd
5 *
6 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
7 *
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.
11*/
12
13#include <drm/drmP.h>
14#include <drm/drm_crtc_helper.h>
15#include <drm/drm_panel.h>
4ea9526b 16#include <drm/drm_atomic_helper.h>
14b6873a 17
fb38b1f6 18#include <linux/of_graph.h>
14b6873a
AH
19#include <linux/regulator/consumer.h>
20
21#include <video/of_videomode.h>
22#include <video/videomode.h>
23
a2986e80 24#include "exynos_drm_crtc.h"
14b6873a
AH
25
26struct exynos_dpi {
2b8376c8 27 struct drm_encoder encoder;
14b6873a
AH
28 struct device *dev;
29 struct device_node *panel_node;
30
31 struct drm_panel *panel;
32 struct drm_connector connector;
14b6873a
AH
33
34 struct videomode *vm;
14b6873a
AH
35};
36
37#define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38
2b8376c8 39static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
4cfde1f2 40{
cf67cc9a 41 return container_of(e, struct exynos_dpi, encoder);
4cfde1f2
AH
42}
43
14b6873a
AH
44static enum drm_connector_status
45exynos_dpi_detect(struct drm_connector *connector, bool force)
46{
47 struct exynos_dpi *ctx = connector_to_dpi(connector);
48
aaa51b13 49 if (ctx->panel && !ctx->panel->connector)
000cc920 50 drm_panel_attach(ctx->panel, &ctx->connector);
14b6873a 51
000cc920 52 return connector_status_connected;
14b6873a
AH
53}
54
55static void exynos_dpi_connector_destroy(struct drm_connector *connector)
56{
34ea3d38 57 drm_connector_unregister(connector);
14b6873a
AH
58 drm_connector_cleanup(connector);
59}
60
800ba2b5 61static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
63498e30 62 .dpms = drm_atomic_helper_connector_dpms,
14b6873a
AH
63 .detect = exynos_dpi_detect,
64 .fill_modes = drm_helper_probe_single_connector_modes,
65 .destroy = exynos_dpi_connector_destroy,
4ea9526b
GP
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,
14b6873a
AH
69};
70
71static int exynos_dpi_get_modes(struct drm_connector *connector)
72{
73 struct exynos_dpi *ctx = connector_to_dpi(connector);
74
75 /* fimd timings gets precedence over panel modes */
76 if (ctx->vm) {
77 struct drm_display_mode *mode;
78
79 mode = drm_mode_create(connector->dev);
80 if (!mode) {
81 DRM_ERROR("failed to create a new display mode\n");
82 return 0;
83 }
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;
88 }
89
90 if (ctx->panel)
91 return ctx->panel->funcs->get_modes(ctx->panel);
92
93 return 0;
94}
95
14b6873a
AH
96static struct drm_encoder *
97exynos_dpi_best_encoder(struct drm_connector *connector)
98{
99 struct exynos_dpi *ctx = connector_to_dpi(connector);
100
2b8376c8 101 return &ctx->encoder;
14b6873a
AH
102}
103
800ba2b5 104static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
14b6873a 105 .get_modes = exynos_dpi_get_modes,
14b6873a
AH
106 .best_encoder = exynos_dpi_best_encoder,
107};
108
2b8376c8 109static int exynos_dpi_create_connector(struct drm_encoder *encoder)
14b6873a 110{
2b8376c8 111 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
14b6873a
AH
112 struct drm_connector *connector = &ctx->connector;
113 int ret;
114
ae08fe6c 115 connector->polled = DRM_CONNECTOR_POLL_HPD;
14b6873a
AH
116
117 ret = drm_connector_init(encoder->dev, connector,
118 &exynos_dpi_connector_funcs,
119 DRM_MODE_CONNECTOR_VGA);
120 if (ret) {
121 DRM_ERROR("failed to initialize connector with drm\n");
122 return ret;
123 }
124
125 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
34ea3d38 126 drm_connector_register(connector);
14b6873a
AH
127 drm_mode_connector_attach_encoder(connector, encoder);
128
129 return 0;
130}
131
2b8376c8
GP
132static void exynos_dpi_mode_set(struct drm_encoder *encoder,
133 struct drm_display_mode *mode,
134 struct drm_display_mode *adjusted_mode)
135{
136}
137
138static void exynos_dpi_enable(struct drm_encoder *encoder)
14b6873a 139{
cf67cc9a 140 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
b6595dc7 141
39bbde9c
AK
142 if (ctx->panel) {
143 drm_panel_prepare(ctx->panel);
14b6873a 144 drm_panel_enable(ctx->panel);
39bbde9c 145 }
14b6873a
AH
146}
147
2b8376c8 148static void exynos_dpi_disable(struct drm_encoder *encoder)
14b6873a 149{
cf67cc9a 150 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
b6595dc7 151
39bbde9c 152 if (ctx->panel) {
14b6873a 153 drm_panel_disable(ctx->panel);
39bbde9c
AK
154 drm_panel_unprepare(ctx->panel);
155 }
14b6873a
AH
156}
157
800ba2b5 158static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
2b8376c8 159 .mode_set = exynos_dpi_mode_set,
b6595dc7
GP
160 .enable = exynos_dpi_enable,
161 .disable = exynos_dpi_disable,
14b6873a
AH
162};
163
800ba2b5 164static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
2b8376c8
GP
165 .destroy = drm_encoder_cleanup,
166};
167
14b6873a
AH
168enum {
169 FIMD_PORT_IN0,
170 FIMD_PORT_IN1,
171 FIMD_PORT_IN2,
172 FIMD_PORT_RGB,
173 FIMD_PORT_WRB,
174};
175
f46ed1ab 176static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
14b6873a
AH
177{
178 struct device_node *np, *ep;
179
fb38b1f6 180 ep = of_graph_get_endpoint_by_regs(dev->of_node, FIMD_PORT_RGB, 0);
14b6873a
AH
181 if (!ep)
182 return NULL;
183
184 np = of_graph_get_remote_port_parent(ep);
185 of_node_put(ep);
186
187 return np;
188}
189
190static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
191{
192 struct device *dev = ctx->dev;
193 struct device_node *dn = dev->of_node;
194 struct device_node *np;
195
196 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
197
198 np = of_get_child_by_name(dn, "display-timings");
199 if (np) {
200 struct videomode *vm;
201 int ret;
202
203 of_node_put(np);
204
205 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
206 if (!vm)
207 return -ENOMEM;
208
209 ret = of_get_videomode(dn, vm, 0);
000cc920
AH
210 if (ret < 0) {
211 devm_kfree(dev, vm);
14b6873a 212 return ret;
000cc920 213 }
14b6873a
AH
214
215 ctx->vm = vm;
216
217 return 0;
218 }
219
220 if (!ctx->panel_node)
221 return -EINVAL;
222
223 return 0;
224}
225
2b8376c8 226int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
a2986e80
GP
227{
228 int ret;
229
2b8376c8
GP
230 ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD);
231 if (ret < 0)
a2986e80 232 return ret;
a2986e80 233
2b8376c8
GP
234 encoder->possible_crtcs = 1 << ret;
235
236 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
237
238 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
13a3d91f 239 DRM_MODE_ENCODER_TMDS, NULL);
2b8376c8
GP
240
241 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
242
243 ret = exynos_dpi_create_connector(encoder);
a2986e80
GP
244 if (ret) {
245 DRM_ERROR("failed to create connector ret = %d\n", ret);
2b8376c8 246 drm_encoder_cleanup(encoder);
a2986e80
GP
247 return ret;
248 }
249
250 return 0;
251}
252
2b8376c8 253struct drm_encoder *exynos_dpi_probe(struct device *dev)
14b6873a
AH
254{
255 struct exynos_dpi *ctx;
256 int ret;
257
258 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
259 if (!ctx)
4cfde1f2 260 return ERR_PTR(-ENOMEM);
14b6873a
AH
261
262 ctx->dev = dev;
14b6873a
AH
263
264 ret = exynos_dpi_parse_dt(ctx);
000cc920
AH
265 if (ret < 0) {
266 devm_kfree(dev, ctx);
86650408 267 return NULL;
000cc920
AH
268 }
269
270 if (ctx->panel_node) {
271 ctx->panel = of_drm_find_panel(ctx->panel_node);
86650408 272 if (!ctx->panel)
000cc920
AH
273 return ERR_PTR(-EPROBE_DEFER);
274 }
14b6873a 275
cf67cc9a 276 return &ctx->encoder;
14b6873a
AH
277}
278
2b8376c8 279int exynos_dpi_remove(struct drm_encoder *encoder)
14b6873a 280{
cf67cc9a 281 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
f37cd5e8 282
cf67cc9a 283 exynos_dpi_disable(&ctx->encoder);
90eac897 284
90eac897
AH
285 if (ctx->panel)
286 drm_panel_detach(ctx->panel);
14b6873a
AH
287
288 return 0;
289}
This page took 0.138061 seconds and 5 git commands to generate.