drm/exynos: simplify calculation of possible CRTCs
[linux-2.6-block.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
AH
17
18#include <linux/regulator/consumer.h>
19
20#include <video/of_videomode.h>
21#include <video/videomode.h>
22
23#include "exynos_drm_drv.h"
24
25struct exynos_dpi {
4cfde1f2 26 struct exynos_drm_display display;
14b6873a
AH
27 struct device *dev;
28 struct device_node *panel_node;
29
30 struct drm_panel *panel;
31 struct drm_connector connector;
32 struct drm_encoder *encoder;
33
34 struct videomode *vm;
14b6873a
AH
35};
36
37#define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38
4cfde1f2
AH
39static inline struct exynos_dpi *display_to_dpi(struct exynos_drm_display *d)
40{
41 return container_of(d, struct exynos_dpi, display);
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
61static 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
101 return ctx->encoder;
102}
103
104static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
105 .get_modes = exynos_dpi_get_modes,
14b6873a
AH
106 .best_encoder = exynos_dpi_best_encoder,
107};
108
109static int exynos_dpi_create_connector(struct exynos_drm_display *display,
110 struct drm_encoder *encoder)
111{
5af3d9bb 112 struct exynos_dpi *ctx = display_to_dpi(display);
14b6873a
AH
113 struct drm_connector *connector = &ctx->connector;
114 int ret;
115
116 ctx->encoder = encoder;
117
ae08fe6c 118 connector->polled = DRM_CONNECTOR_POLL_HPD;
14b6873a
AH
119
120 ret = drm_connector_init(encoder->dev, connector,
121 &exynos_dpi_connector_funcs,
122 DRM_MODE_CONNECTOR_VGA);
123 if (ret) {
124 DRM_ERROR("failed to initialize connector with drm\n");
125 return ret;
126 }
127
128 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
34ea3d38 129 drm_connector_register(connector);
14b6873a
AH
130 drm_mode_connector_attach_encoder(connector, encoder);
131
132 return 0;
133}
134
b6595dc7 135static void exynos_dpi_enable(struct exynos_drm_display *display)
14b6873a 136{
b6595dc7
GP
137 struct exynos_dpi *ctx = display_to_dpi(display);
138
39bbde9c
AK
139 if (ctx->panel) {
140 drm_panel_prepare(ctx->panel);
14b6873a 141 drm_panel_enable(ctx->panel);
39bbde9c 142 }
14b6873a
AH
143}
144
b6595dc7 145static void exynos_dpi_disable(struct exynos_drm_display *display)
14b6873a 146{
b6595dc7
GP
147 struct exynos_dpi *ctx = display_to_dpi(display);
148
39bbde9c 149 if (ctx->panel) {
14b6873a 150 drm_panel_disable(ctx->panel);
39bbde9c
AK
151 drm_panel_unprepare(ctx->panel);
152 }
14b6873a
AH
153}
154
14b6873a
AH
155static struct exynos_drm_display_ops exynos_dpi_display_ops = {
156 .create_connector = exynos_dpi_create_connector,
b6595dc7
GP
157 .enable = exynos_dpi_enable,
158 .disable = exynos_dpi_disable,
14b6873a
AH
159};
160
14b6873a
AH
161/* of_* functions will be removed after merge of of_graph patches */
162static struct device_node *
163of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
164{
165 struct device_node *np;
166
167 for_each_child_of_node(parent, np) {
168 u32 r;
169
170 if (!np->name || of_node_cmp(np->name, name))
171 continue;
172
173 if (of_property_read_u32(np, "reg", &r) < 0)
174 r = 0;
175
176 if (reg == r)
177 break;
178 }
179
180 return np;
181}
182
183static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
184 u32 reg)
185{
186 struct device_node *ports, *port;
187
188 ports = of_get_child_by_name(parent, "ports");
189 if (ports)
190 parent = ports;
191
192 port = of_get_child_by_name_reg(parent, "port", reg);
193
194 of_node_put(ports);
195
196 return port;
197}
198
199static struct device_node *
200of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
201{
202 return of_get_child_by_name_reg(port, "endpoint", reg);
203}
204
205static struct device_node *
206of_graph_get_remote_port_parent(const struct device_node *node)
207{
208 struct device_node *np;
209 unsigned int depth;
210
211 np = of_parse_phandle(node, "remote-endpoint", 0);
212
213 /* Walk 3 levels up only if there is 'ports' node. */
214 for (depth = 3; depth && np; depth--) {
215 np = of_get_next_parent(np);
216 if (depth == 2 && of_node_cmp(np->name, "ports"))
217 break;
218 }
219 return np;
220}
221
222enum {
223 FIMD_PORT_IN0,
224 FIMD_PORT_IN1,
225 FIMD_PORT_IN2,
226 FIMD_PORT_RGB,
227 FIMD_PORT_WRB,
228};
229
f46ed1ab 230static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
14b6873a
AH
231{
232 struct device_node *np, *ep;
233
234 np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
235 if (!np)
236 return NULL;
237
238 ep = of_graph_get_endpoint_by_reg(np, 0);
239 of_node_put(np);
240 if (!ep)
241 return NULL;
242
243 np = of_graph_get_remote_port_parent(ep);
244 of_node_put(ep);
245
246 return np;
247}
248
249static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
250{
251 struct device *dev = ctx->dev;
252 struct device_node *dn = dev->of_node;
253 struct device_node *np;
254
255 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
256
257 np = of_get_child_by_name(dn, "display-timings");
258 if (np) {
259 struct videomode *vm;
260 int ret;
261
262 of_node_put(np);
263
264 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
265 if (!vm)
266 return -ENOMEM;
267
268 ret = of_get_videomode(dn, vm, 0);
000cc920
AH
269 if (ret < 0) {
270 devm_kfree(dev, vm);
14b6873a 271 return ret;
000cc920 272 }
14b6873a
AH
273
274 ctx->vm = vm;
275
276 return 0;
277 }
278
279 if (!ctx->panel_node)
280 return -EINVAL;
281
282 return 0;
283}
284
000cc920 285struct exynos_drm_display *exynos_dpi_probe(struct device *dev)
14b6873a
AH
286{
287 struct exynos_dpi *ctx;
288 int ret;
289
290 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
291 if (!ctx)
4cfde1f2 292 return ERR_PTR(-ENOMEM);
14b6873a 293
4cfde1f2
AH
294 ctx->display.type = EXYNOS_DISPLAY_TYPE_LCD;
295 ctx->display.ops = &exynos_dpi_display_ops;
14b6873a 296 ctx->dev = dev;
14b6873a
AH
297
298 ret = exynos_dpi_parse_dt(ctx);
000cc920
AH
299 if (ret < 0) {
300 devm_kfree(dev, ctx);
86650408 301 return NULL;
000cc920
AH
302 }
303
304 if (ctx->panel_node) {
305 ctx->panel = of_drm_find_panel(ctx->panel_node);
86650408 306 if (!ctx->panel)
000cc920
AH
307 return ERR_PTR(-EPROBE_DEFER);
308 }
14b6873a 309
4cfde1f2 310 return &ctx->display;
14b6873a
AH
311}
312
4cfde1f2 313int exynos_dpi_remove(struct exynos_drm_display *display)
14b6873a 314{
4cfde1f2 315 struct exynos_dpi *ctx = display_to_dpi(display);
f37cd5e8 316
b6595dc7 317 exynos_dpi_disable(&ctx->display);
90eac897 318
90eac897
AH
319 if (ctx->panel)
320 drm_panel_detach(ctx->panel);
14b6873a
AH
321
322 return 0;
323}