blob: 37678cf4425ad515eaac8a6c244cd53a5ced3c5e [file] [log] [blame]
Andrzej Hajda14b68732014-03-17 13:03:56 +01001/*
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>
16
17#include <linux/regulator/consumer.h>
18
19#include <video/of_videomode.h>
20#include <video/videomode.h>
21
22#include "exynos_drm_drv.h"
23
24struct exynos_dpi {
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +010025 struct exynos_drm_display display;
Andrzej Hajda14b68732014-03-17 13:03:56 +010026 struct device *dev;
27 struct device_node *panel_node;
28
29 struct drm_panel *panel;
30 struct drm_connector connector;
31 struct drm_encoder *encoder;
32
33 struct videomode *vm;
34 int dpms_mode;
35};
36
37#define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +010039static inline struct exynos_dpi *display_to_dpi(struct exynos_drm_display *d)
40{
41 return container_of(d, struct exynos_dpi, display);
42}
43
Andrzej Hajda14b68732014-03-17 13:03:56 +010044static enum drm_connector_status
45exynos_dpi_detect(struct drm_connector *connector, bool force)
46{
47 struct exynos_dpi *ctx = connector_to_dpi(connector);
48
Tomasz Figaaaa51b12014-06-10 22:57:57 +020049 if (ctx->panel && !ctx->panel->connector)
Andrzej Hajda000cc922014-04-03 16:26:00 +020050 drm_panel_attach(ctx->panel, &ctx->connector);
Andrzej Hajda14b68732014-03-17 13:03:56 +010051
Andrzej Hajda000cc922014-04-03 16:26:00 +020052 return connector_status_connected;
Andrzej Hajda14b68732014-03-17 13:03:56 +010053}
54
55static void exynos_dpi_connector_destroy(struct drm_connector *connector)
56{
Thomas Wood34ea3d32014-05-29 16:57:41 +010057 drm_connector_unregister(connector);
Andrzej Hajda14b68732014-03-17 13:03:56 +010058 drm_connector_cleanup(connector);
59}
60
61static struct drm_connector_funcs exynos_dpi_connector_funcs = {
62 .dpms = drm_helper_connector_dpms,
63 .detect = exynos_dpi_detect,
64 .fill_modes = drm_helper_probe_single_connector_modes,
65 .destroy = exynos_dpi_connector_destroy,
66};
67
68static int exynos_dpi_get_modes(struct drm_connector *connector)
69{
70 struct exynos_dpi *ctx = connector_to_dpi(connector);
71
72 /* fimd timings gets precedence over panel modes */
73 if (ctx->vm) {
74 struct drm_display_mode *mode;
75
76 mode = drm_mode_create(connector->dev);
77 if (!mode) {
78 DRM_ERROR("failed to create a new display mode\n");
79 return 0;
80 }
81 drm_display_mode_from_videomode(ctx->vm, mode);
82 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
83 drm_mode_probed_add(connector, mode);
84 return 1;
85 }
86
87 if (ctx->panel)
88 return ctx->panel->funcs->get_modes(ctx->panel);
89
90 return 0;
91}
92
Andrzej Hajda14b68732014-03-17 13:03:56 +010093static struct drm_encoder *
94exynos_dpi_best_encoder(struct drm_connector *connector)
95{
96 struct exynos_dpi *ctx = connector_to_dpi(connector);
97
98 return ctx->encoder;
99}
100
101static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
102 .get_modes = exynos_dpi_get_modes,
Andrzej Hajda14b68732014-03-17 13:03:56 +0100103 .best_encoder = exynos_dpi_best_encoder,
104};
105
106static int exynos_dpi_create_connector(struct exynos_drm_display *display,
107 struct drm_encoder *encoder)
108{
Andrzej Hajda5af3d9b2014-11-17 09:54:27 +0100109 struct exynos_dpi *ctx = display_to_dpi(display);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100110 struct drm_connector *connector = &ctx->connector;
111 int ret;
112
113 ctx->encoder = encoder;
114
Inki Daeae08fe62014-04-01 19:45:28 +0900115 connector->polled = DRM_CONNECTOR_POLL_HPD;
Andrzej Hajda14b68732014-03-17 13:03:56 +0100116
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);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100126 drm_connector_register(connector);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100127 drm_mode_connector_attach_encoder(connector, encoder);
128
129 return 0;
130}
131
132static void exynos_dpi_poweron(struct exynos_dpi *ctx)
133{
Ajay Kumar39bbde92014-07-31 23:12:05 +0530134 if (ctx->panel) {
135 drm_panel_prepare(ctx->panel);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100136 drm_panel_enable(ctx->panel);
Ajay Kumar39bbde92014-07-31 23:12:05 +0530137 }
Andrzej Hajda14b68732014-03-17 13:03:56 +0100138}
139
140static void exynos_dpi_poweroff(struct exynos_dpi *ctx)
141{
Ajay Kumar39bbde92014-07-31 23:12:05 +0530142 if (ctx->panel) {
Andrzej Hajda14b68732014-03-17 13:03:56 +0100143 drm_panel_disable(ctx->panel);
Ajay Kumar39bbde92014-07-31 23:12:05 +0530144 drm_panel_unprepare(ctx->panel);
145 }
Andrzej Hajda14b68732014-03-17 13:03:56 +0100146}
147
148static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode)
149{
Andrzej Hajda5af3d9b2014-11-17 09:54:27 +0100150 struct exynos_dpi *ctx = display_to_dpi(display);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100151
152 switch (mode) {
153 case DRM_MODE_DPMS_ON:
154 if (ctx->dpms_mode != DRM_MODE_DPMS_ON)
155 exynos_dpi_poweron(ctx);
156 break;
157 case DRM_MODE_DPMS_STANDBY:
158 case DRM_MODE_DPMS_SUSPEND:
159 case DRM_MODE_DPMS_OFF:
160 if (ctx->dpms_mode == DRM_MODE_DPMS_ON)
161 exynos_dpi_poweroff(ctx);
162 break;
163 default:
164 break;
Damien Lespiau10d9b4e2014-06-09 14:21:08 +0100165 }
Andrzej Hajda14b68732014-03-17 13:03:56 +0100166 ctx->dpms_mode = mode;
167}
168
169static struct exynos_drm_display_ops exynos_dpi_display_ops = {
170 .create_connector = exynos_dpi_create_connector,
171 .dpms = exynos_dpi_dpms
172};
173
Andrzej Hajda14b68732014-03-17 13:03:56 +0100174/* of_* functions will be removed after merge of of_graph patches */
175static struct device_node *
176of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
177{
178 struct device_node *np;
179
180 for_each_child_of_node(parent, np) {
181 u32 r;
182
183 if (!np->name || of_node_cmp(np->name, name))
184 continue;
185
186 if (of_property_read_u32(np, "reg", &r) < 0)
187 r = 0;
188
189 if (reg == r)
190 break;
191 }
192
193 return np;
194}
195
196static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
197 u32 reg)
198{
199 struct device_node *ports, *port;
200
201 ports = of_get_child_by_name(parent, "ports");
202 if (ports)
203 parent = ports;
204
205 port = of_get_child_by_name_reg(parent, "port", reg);
206
207 of_node_put(ports);
208
209 return port;
210}
211
212static struct device_node *
213of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
214{
215 return of_get_child_by_name_reg(port, "endpoint", reg);
216}
217
218static struct device_node *
219of_graph_get_remote_port_parent(const struct device_node *node)
220{
221 struct device_node *np;
222 unsigned int depth;
223
224 np = of_parse_phandle(node, "remote-endpoint", 0);
225
226 /* Walk 3 levels up only if there is 'ports' node. */
227 for (depth = 3; depth && np; depth--) {
228 np = of_get_next_parent(np);
229 if (depth == 2 && of_node_cmp(np->name, "ports"))
230 break;
231 }
232 return np;
233}
234
235enum {
236 FIMD_PORT_IN0,
237 FIMD_PORT_IN1,
238 FIMD_PORT_IN2,
239 FIMD_PORT_RGB,
240 FIMD_PORT_WRB,
241};
242
Sachin Kamatf46ed1a2014-05-22 10:32:54 +0530243static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100244{
245 struct device_node *np, *ep;
246
247 np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
248 if (!np)
249 return NULL;
250
251 ep = of_graph_get_endpoint_by_reg(np, 0);
252 of_node_put(np);
253 if (!ep)
254 return NULL;
255
256 np = of_graph_get_remote_port_parent(ep);
257 of_node_put(ep);
258
259 return np;
260}
261
262static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
263{
264 struct device *dev = ctx->dev;
265 struct device_node *dn = dev->of_node;
266 struct device_node *np;
267
268 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
269
270 np = of_get_child_by_name(dn, "display-timings");
271 if (np) {
272 struct videomode *vm;
273 int ret;
274
275 of_node_put(np);
276
277 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
278 if (!vm)
279 return -ENOMEM;
280
281 ret = of_get_videomode(dn, vm, 0);
Andrzej Hajda000cc922014-04-03 16:26:00 +0200282 if (ret < 0) {
283 devm_kfree(dev, vm);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100284 return ret;
Andrzej Hajda000cc922014-04-03 16:26:00 +0200285 }
Andrzej Hajda14b68732014-03-17 13:03:56 +0100286
287 ctx->vm = vm;
288
289 return 0;
290 }
291
292 if (!ctx->panel_node)
293 return -EINVAL;
294
295 return 0;
296}
297
Andrzej Hajda000cc922014-04-03 16:26:00 +0200298struct exynos_drm_display *exynos_dpi_probe(struct device *dev)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100299{
300 struct exynos_dpi *ctx;
301 int ret;
302
303 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
304 if (!ctx)
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100305 return ERR_PTR(-ENOMEM);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100306
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100307 ctx->display.type = EXYNOS_DISPLAY_TYPE_LCD;
308 ctx->display.ops = &exynos_dpi_display_ops;
Andrzej Hajda14b68732014-03-17 13:03:56 +0100309 ctx->dev = dev;
Andrzej Hajda14b68732014-03-17 13:03:56 +0100310 ctx->dpms_mode = DRM_MODE_DPMS_OFF;
311
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100312 ret = exynos_drm_component_add(dev,
313 EXYNOS_DEVICE_TYPE_CONNECTOR,
314 ctx->display.type);
315 if (ret)
316 return ERR_PTR(ret);
317
Andrzej Hajda14b68732014-03-17 13:03:56 +0100318 ret = exynos_dpi_parse_dt(ctx);
Andrzej Hajda000cc922014-04-03 16:26:00 +0200319 if (ret < 0) {
320 devm_kfree(dev, ctx);
Inki Daedf5225b2014-05-29 18:28:02 +0900321 goto err_del_component;
Andrzej Hajda000cc922014-04-03 16:26:00 +0200322 }
Andrzej Hajda14b68732014-03-17 13:03:56 +0100323
Andrzej Hajda000cc922014-04-03 16:26:00 +0200324 if (ctx->panel_node) {
325 ctx->panel = of_drm_find_panel(ctx->panel_node);
Inki Daedf5225b2014-05-29 18:28:02 +0900326 if (!ctx->panel) {
327 exynos_drm_component_del(dev,
328 EXYNOS_DEVICE_TYPE_CONNECTOR);
Andrzej Hajda000cc922014-04-03 16:26:00 +0200329 return ERR_PTR(-EPROBE_DEFER);
Inki Daedf5225b2014-05-29 18:28:02 +0900330 }
Andrzej Hajda000cc922014-04-03 16:26:00 +0200331 }
332
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100333 return &ctx->display;
Inki Daedf5225b2014-05-29 18:28:02 +0900334
335err_del_component:
336 exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
337
338 return NULL;
Andrzej Hajda14b68732014-03-17 13:03:56 +0100339}
340
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100341int exynos_dpi_remove(struct exynos_drm_display *display)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100342{
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100343 struct exynos_dpi *ctx = display_to_dpi(display);
Inki Daef37cd5e2014-05-09 14:25:20 +0900344
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100345 exynos_dpi_dpms(&ctx->display, DRM_MODE_DPMS_OFF);
Andrzej Hajda90eac892014-09-09 15:16:11 +0200346
Andrzej Hajda90eac892014-09-09 15:16:11 +0200347 if (ctx->panel)
348 drm_panel_detach(ctx->panel);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100349
Andrzej Hajda4cfde1f2014-11-17 09:54:26 +0100350 exynos_drm_component_del(ctx->dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
Inki Daedf5225b2014-05-29 18:28:02 +0900351
Andrzej Hajda14b68732014-03-17 13:03:56 +0100352 return 0;
353}