blob: f5503af521482ea98959fd347309a6107d739a28 [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 {
25 struct device *dev;
26 struct device_node *panel_node;
27
28 struct drm_panel *panel;
29 struct drm_connector connector;
30 struct drm_encoder *encoder;
31
32 struct videomode *vm;
33 int dpms_mode;
34};
35
36#define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
37
38static enum drm_connector_status
39exynos_dpi_detect(struct drm_connector *connector, bool force)
40{
41 struct exynos_dpi *ctx = connector_to_dpi(connector);
42
43 /* panels supported only by boot-loader are always connected */
44 if (!ctx->panel_node)
45 return connector_status_connected;
46
47 if (!ctx->panel) {
48 ctx->panel = of_drm_find_panel(ctx->panel_node);
49 if (ctx->panel)
50 drm_panel_attach(ctx->panel, &ctx->connector);
51 }
52
53 if (ctx->panel)
54 return connector_status_connected;
55
56 return connector_status_disconnected;
57}
58
59static void exynos_dpi_connector_destroy(struct drm_connector *connector)
60{
61 drm_sysfs_connector_remove(connector);
62 drm_connector_cleanup(connector);
63}
64
65static struct drm_connector_funcs exynos_dpi_connector_funcs = {
66 .dpms = drm_helper_connector_dpms,
67 .detect = exynos_dpi_detect,
68 .fill_modes = drm_helper_probe_single_connector_modes,
69 .destroy = exynos_dpi_connector_destroy,
70};
71
72static int exynos_dpi_get_modes(struct drm_connector *connector)
73{
74 struct exynos_dpi *ctx = connector_to_dpi(connector);
75
76 /* fimd timings gets precedence over panel modes */
77 if (ctx->vm) {
78 struct drm_display_mode *mode;
79
80 mode = drm_mode_create(connector->dev);
81 if (!mode) {
82 DRM_ERROR("failed to create a new display mode\n");
83 return 0;
84 }
85 drm_display_mode_from_videomode(ctx->vm, mode);
86 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
87 drm_mode_probed_add(connector, mode);
88 return 1;
89 }
90
91 if (ctx->panel)
92 return ctx->panel->funcs->get_modes(ctx->panel);
93
94 return 0;
95}
96
Andrzej Hajda14b68732014-03-17 13:03:56 +010097static struct drm_encoder *
98exynos_dpi_best_encoder(struct drm_connector *connector)
99{
100 struct exynos_dpi *ctx = connector_to_dpi(connector);
101
102 return ctx->encoder;
103}
104
105static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
106 .get_modes = exynos_dpi_get_modes,
Andrzej Hajda14b68732014-03-17 13:03:56 +0100107 .best_encoder = exynos_dpi_best_encoder,
108};
109
110static int exynos_dpi_create_connector(struct exynos_drm_display *display,
111 struct drm_encoder *encoder)
112{
113 struct exynos_dpi *ctx = display->ctx;
114 struct drm_connector *connector = &ctx->connector;
115 int ret;
116
117 ctx->encoder = encoder;
118
119 if (ctx->panel_node)
120 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
121 else
122 connector->polled = DRM_CONNECTOR_POLL_HPD;
123
124 ret = drm_connector_init(encoder->dev, connector,
125 &exynos_dpi_connector_funcs,
126 DRM_MODE_CONNECTOR_VGA);
127 if (ret) {
128 DRM_ERROR("failed to initialize connector with drm\n");
129 return ret;
130 }
131
132 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
133 drm_sysfs_connector_add(connector);
134 drm_mode_connector_attach_encoder(connector, encoder);
135
136 return 0;
137}
138
139static void exynos_dpi_poweron(struct exynos_dpi *ctx)
140{
141 if (ctx->panel)
142 drm_panel_enable(ctx->panel);
143}
144
145static void exynos_dpi_poweroff(struct exynos_dpi *ctx)
146{
147 if (ctx->panel)
148 drm_panel_disable(ctx->panel);
149}
150
151static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode)
152{
153 struct exynos_dpi *ctx = display->ctx;
154
155 switch (mode) {
156 case DRM_MODE_DPMS_ON:
157 if (ctx->dpms_mode != DRM_MODE_DPMS_ON)
158 exynos_dpi_poweron(ctx);
159 break;
160 case DRM_MODE_DPMS_STANDBY:
161 case DRM_MODE_DPMS_SUSPEND:
162 case DRM_MODE_DPMS_OFF:
163 if (ctx->dpms_mode == DRM_MODE_DPMS_ON)
164 exynos_dpi_poweroff(ctx);
165 break;
166 default:
167 break;
168 };
169 ctx->dpms_mode = mode;
170}
171
172static struct exynos_drm_display_ops exynos_dpi_display_ops = {
173 .create_connector = exynos_dpi_create_connector,
174 .dpms = exynos_dpi_dpms
175};
176
177static struct exynos_drm_display exynos_dpi_display = {
178 .type = EXYNOS_DISPLAY_TYPE_LCD,
179 .ops = &exynos_dpi_display_ops,
180};
181
182/* of_* functions will be removed after merge of of_graph patches */
183static struct device_node *
184of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
185{
186 struct device_node *np;
187
188 for_each_child_of_node(parent, np) {
189 u32 r;
190
191 if (!np->name || of_node_cmp(np->name, name))
192 continue;
193
194 if (of_property_read_u32(np, "reg", &r) < 0)
195 r = 0;
196
197 if (reg == r)
198 break;
199 }
200
201 return np;
202}
203
204static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
205 u32 reg)
206{
207 struct device_node *ports, *port;
208
209 ports = of_get_child_by_name(parent, "ports");
210 if (ports)
211 parent = ports;
212
213 port = of_get_child_by_name_reg(parent, "port", reg);
214
215 of_node_put(ports);
216
217 return port;
218}
219
220static struct device_node *
221of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
222{
223 return of_get_child_by_name_reg(port, "endpoint", reg);
224}
225
226static struct device_node *
227of_graph_get_remote_port_parent(const struct device_node *node)
228{
229 struct device_node *np;
230 unsigned int depth;
231
232 np = of_parse_phandle(node, "remote-endpoint", 0);
233
234 /* Walk 3 levels up only if there is 'ports' node. */
235 for (depth = 3; depth && np; depth--) {
236 np = of_get_next_parent(np);
237 if (depth == 2 && of_node_cmp(np->name, "ports"))
238 break;
239 }
240 return np;
241}
242
243enum {
244 FIMD_PORT_IN0,
245 FIMD_PORT_IN1,
246 FIMD_PORT_IN2,
247 FIMD_PORT_RGB,
248 FIMD_PORT_WRB,
249};
250
Inki Daef37cd5e2014-05-09 14:25:20 +0900251struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100252{
253 struct device_node *np, *ep;
254
255 np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
256 if (!np)
257 return NULL;
258
259 ep = of_graph_get_endpoint_by_reg(np, 0);
260 of_node_put(np);
261 if (!ep)
262 return NULL;
263
264 np = of_graph_get_remote_port_parent(ep);
265 of_node_put(ep);
266
267 return np;
268}
269
270static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
271{
272 struct device *dev = ctx->dev;
273 struct device_node *dn = dev->of_node;
274 struct device_node *np;
275
276 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
277
278 np = of_get_child_by_name(dn, "display-timings");
279 if (np) {
280 struct videomode *vm;
281 int ret;
282
283 of_node_put(np);
284
285 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
286 if (!vm)
287 return -ENOMEM;
288
289 ret = of_get_videomode(dn, vm, 0);
290 if (ret < 0)
291 return ret;
292
293 ctx->vm = vm;
294
295 return 0;
296 }
297
298 if (!ctx->panel_node)
299 return -EINVAL;
300
301 return 0;
302}
303
Inki Daef37cd5e2014-05-09 14:25:20 +0900304int exynos_dpi_probe(struct drm_device *drm_dev, struct device *dev)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100305{
306 struct exynos_dpi *ctx;
307 int ret;
308
309 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
310 if (!ctx)
311 return -ENOMEM;
312
313 ctx->dev = dev;
314 exynos_dpi_display.ctx = ctx;
315 ctx->dpms_mode = DRM_MODE_DPMS_OFF;
316
317 ret = exynos_dpi_parse_dt(ctx);
318 if (ret < 0)
319 return ret;
320
Inki Daef37cd5e2014-05-09 14:25:20 +0900321 return exynos_drm_create_enc_conn(drm_dev, &exynos_dpi_display);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100322}
323
Inki Daef37cd5e2014-05-09 14:25:20 +0900324int exynos_dpi_remove(struct drm_device *drm_dev, struct device *dev)
Andrzej Hajda14b68732014-03-17 13:03:56 +0100325{
Inki Daef37cd5e2014-05-09 14:25:20 +0900326 struct drm_encoder *encoder = exynos_dpi_display.encoder;
327 struct exynos_dpi *ctx = exynos_dpi_display.ctx;
328
Andrzej Hajda14b68732014-03-17 13:03:56 +0100329 exynos_dpi_dpms(&exynos_dpi_display, DRM_MODE_DPMS_OFF);
Inki Daef37cd5e2014-05-09 14:25:20 +0900330 encoder->funcs->destroy(encoder);
331 drm_connector_cleanup(&ctx->connector);
Andrzej Hajda14b68732014-03-17 13:03:56 +0100332
333 return 0;
334}