Merge tag 'drm/panel/for-3.17-rc1' of git://anongit.freedesktop.org/tegra/linux into...
[cascardo/linux.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
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>
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
24 struct 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
38 static enum drm_connector_status
39 exynos_dpi_detect(struct drm_connector *connector, bool force)
40 {
41         struct exynos_dpi *ctx = connector_to_dpi(connector);
42
43         if (ctx->panel && !ctx->panel->connector)
44                 drm_panel_attach(ctx->panel, &ctx->connector);
45
46         return connector_status_connected;
47 }
48
49 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
50 {
51         drm_connector_unregister(connector);
52         drm_connector_cleanup(connector);
53 }
54
55 static struct drm_connector_funcs exynos_dpi_connector_funcs = {
56         .dpms = drm_helper_connector_dpms,
57         .detect = exynos_dpi_detect,
58         .fill_modes = drm_helper_probe_single_connector_modes,
59         .destroy = exynos_dpi_connector_destroy,
60 };
61
62 static int exynos_dpi_get_modes(struct drm_connector *connector)
63 {
64         struct exynos_dpi *ctx = connector_to_dpi(connector);
65
66         /* fimd timings gets precedence over panel modes */
67         if (ctx->vm) {
68                 struct drm_display_mode *mode;
69
70                 mode = drm_mode_create(connector->dev);
71                 if (!mode) {
72                         DRM_ERROR("failed to create a new display mode\n");
73                         return 0;
74                 }
75                 drm_display_mode_from_videomode(ctx->vm, mode);
76                 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
77                 drm_mode_probed_add(connector, mode);
78                 return 1;
79         }
80
81         if (ctx->panel)
82                 return ctx->panel->funcs->get_modes(ctx->panel);
83
84         return 0;
85 }
86
87 static struct drm_encoder *
88 exynos_dpi_best_encoder(struct drm_connector *connector)
89 {
90         struct exynos_dpi *ctx = connector_to_dpi(connector);
91
92         return ctx->encoder;
93 }
94
95 static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
96         .get_modes = exynos_dpi_get_modes,
97         .best_encoder = exynos_dpi_best_encoder,
98 };
99
100 static int exynos_dpi_create_connector(struct exynos_drm_display *display,
101                                        struct drm_encoder *encoder)
102 {
103         struct exynos_dpi *ctx = display->ctx;
104         struct drm_connector *connector = &ctx->connector;
105         int ret;
106
107         ctx->encoder = encoder;
108
109         connector->polled = DRM_CONNECTOR_POLL_HPD;
110
111         ret = drm_connector_init(encoder->dev, connector,
112                                  &exynos_dpi_connector_funcs,
113                                  DRM_MODE_CONNECTOR_VGA);
114         if (ret) {
115                 DRM_ERROR("failed to initialize connector with drm\n");
116                 return ret;
117         }
118
119         drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
120         drm_connector_register(connector);
121         drm_mode_connector_attach_encoder(connector, encoder);
122
123         return 0;
124 }
125
126 static void exynos_dpi_poweron(struct exynos_dpi *ctx)
127 {
128         if (ctx->panel) {
129                 drm_panel_prepare(ctx->panel);
130                 drm_panel_enable(ctx->panel);
131         }
132 }
133
134 static void exynos_dpi_poweroff(struct exynos_dpi *ctx)
135 {
136         if (ctx->panel) {
137                 drm_panel_disable(ctx->panel);
138                 drm_panel_unprepare(ctx->panel);
139         }
140 }
141
142 static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode)
143 {
144         struct exynos_dpi *ctx = display->ctx;
145
146         switch (mode) {
147         case DRM_MODE_DPMS_ON:
148                 if (ctx->dpms_mode != DRM_MODE_DPMS_ON)
149                                 exynos_dpi_poweron(ctx);
150                         break;
151         case DRM_MODE_DPMS_STANDBY:
152         case DRM_MODE_DPMS_SUSPEND:
153         case DRM_MODE_DPMS_OFF:
154                 if (ctx->dpms_mode == DRM_MODE_DPMS_ON)
155                         exynos_dpi_poweroff(ctx);
156                 break;
157         default:
158                 break;
159         }
160         ctx->dpms_mode = mode;
161 }
162
163 static struct exynos_drm_display_ops exynos_dpi_display_ops = {
164         .create_connector = exynos_dpi_create_connector,
165         .dpms = exynos_dpi_dpms
166 };
167
168 static struct exynos_drm_display exynos_dpi_display = {
169         .type = EXYNOS_DISPLAY_TYPE_LCD,
170         .ops = &exynos_dpi_display_ops,
171 };
172
173 /* of_* functions will be removed after merge of of_graph patches */
174 static struct device_node *
175 of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
176 {
177         struct device_node *np;
178
179         for_each_child_of_node(parent, np) {
180                 u32 r;
181
182                 if (!np->name || of_node_cmp(np->name, name))
183                         continue;
184
185                 if (of_property_read_u32(np, "reg", &r) < 0)
186                         r = 0;
187
188                 if (reg == r)
189                         break;
190         }
191
192         return np;
193 }
194
195 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
196                                                     u32 reg)
197 {
198         struct device_node *ports, *port;
199
200         ports = of_get_child_by_name(parent, "ports");
201         if (ports)
202                 parent = ports;
203
204         port = of_get_child_by_name_reg(parent, "port", reg);
205
206         of_node_put(ports);
207
208         return port;
209 }
210
211 static struct device_node *
212 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
213 {
214         return of_get_child_by_name_reg(port, "endpoint", reg);
215 }
216
217 static struct device_node *
218 of_graph_get_remote_port_parent(const struct device_node *node)
219 {
220         struct device_node *np;
221         unsigned int depth;
222
223         np = of_parse_phandle(node, "remote-endpoint", 0);
224
225         /* Walk 3 levels up only if there is 'ports' node. */
226         for (depth = 3; depth && np; depth--) {
227                 np = of_get_next_parent(np);
228                 if (depth == 2 && of_node_cmp(np->name, "ports"))
229                         break;
230         }
231         return np;
232 }
233
234 enum {
235         FIMD_PORT_IN0,
236         FIMD_PORT_IN1,
237         FIMD_PORT_IN2,
238         FIMD_PORT_RGB,
239         FIMD_PORT_WRB,
240 };
241
242 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
243 {
244         struct device_node *np, *ep;
245
246         np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
247         if (!np)
248                 return NULL;
249
250         ep = of_graph_get_endpoint_by_reg(np, 0);
251         of_node_put(np);
252         if (!ep)
253                 return NULL;
254
255         np = of_graph_get_remote_port_parent(ep);
256         of_node_put(ep);
257
258         return np;
259 }
260
261 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
262 {
263         struct device *dev = ctx->dev;
264         struct device_node *dn = dev->of_node;
265         struct device_node *np;
266
267         ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
268
269         np = of_get_child_by_name(dn, "display-timings");
270         if (np) {
271                 struct videomode *vm;
272                 int ret;
273
274                 of_node_put(np);
275
276                 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
277                 if (!vm)
278                         return -ENOMEM;
279
280                 ret = of_get_videomode(dn, vm, 0);
281                 if (ret < 0) {
282                         devm_kfree(dev, vm);
283                         return ret;
284                 }
285
286                 ctx->vm = vm;
287
288                 return 0;
289         }
290
291         if (!ctx->panel_node)
292                 return -EINVAL;
293
294         return 0;
295 }
296
297 struct exynos_drm_display *exynos_dpi_probe(struct device *dev)
298 {
299         struct exynos_dpi *ctx;
300         int ret;
301
302         ret = exynos_drm_component_add(dev,
303                                         EXYNOS_DEVICE_TYPE_CONNECTOR,
304                                         exynos_dpi_display.type);
305         if (ret)
306                 return ERR_PTR(ret);
307
308         ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
309         if (!ctx)
310                 goto err_del_component;
311
312         ctx->dev = dev;
313         exynos_dpi_display.ctx = ctx;
314         ctx->dpms_mode = DRM_MODE_DPMS_OFF;
315
316         ret = exynos_dpi_parse_dt(ctx);
317         if (ret < 0) {
318                 devm_kfree(dev, ctx);
319                 goto err_del_component;
320         }
321
322         if (ctx->panel_node) {
323                 ctx->panel = of_drm_find_panel(ctx->panel_node);
324                 if (!ctx->panel) {
325                         exynos_drm_component_del(dev,
326                                                 EXYNOS_DEVICE_TYPE_CONNECTOR);
327                         return ERR_PTR(-EPROBE_DEFER);
328                 }
329         }
330
331         return &exynos_dpi_display;
332
333 err_del_component:
334         exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
335
336         return NULL;
337 }
338
339 int exynos_dpi_remove(struct device *dev)
340 {
341         struct drm_encoder *encoder = exynos_dpi_display.encoder;
342         struct exynos_dpi *ctx = exynos_dpi_display.ctx;
343
344         exynos_dpi_dpms(&exynos_dpi_display, DRM_MODE_DPMS_OFF);
345         encoder->funcs->destroy(encoder);
346         drm_connector_cleanup(&ctx->connector);
347
348         exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
349
350         return 0;
351 }