2 * Exynos DRM Parallel output support.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd
6 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
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.
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_panel.h>
17 #include <linux/regulator/consumer.h>
19 #include <video/of_videomode.h>
20 #include <video/videomode.h>
22 #include "exynos_drm_drv.h"
26 struct device_node *panel_node;
28 struct drm_panel *panel;
29 struct drm_connector connector;
30 struct drm_encoder *encoder;
36 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38 static enum drm_connector_status
39 exynos_dpi_detect(struct drm_connector *connector, bool force)
41 struct exynos_dpi *ctx = connector_to_dpi(connector);
43 if (ctx->panel && !ctx->panel->connector)
44 drm_panel_attach(ctx->panel, &ctx->connector);
46 return connector_status_connected;
49 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
51 drm_connector_unregister(connector);
52 drm_connector_cleanup(connector);
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,
62 static int exynos_dpi_get_modes(struct drm_connector *connector)
64 struct exynos_dpi *ctx = connector_to_dpi(connector);
66 /* fimd timings gets precedence over panel modes */
68 struct drm_display_mode *mode;
70 mode = drm_mode_create(connector->dev);
72 DRM_ERROR("failed to create a new display mode\n");
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);
82 return ctx->panel->funcs->get_modes(ctx->panel);
87 static struct drm_encoder *
88 exynos_dpi_best_encoder(struct drm_connector *connector)
90 struct exynos_dpi *ctx = connector_to_dpi(connector);
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,
100 static int exynos_dpi_create_connector(struct exynos_drm_display *display,
101 struct drm_encoder *encoder)
103 struct exynos_dpi *ctx = display->ctx;
104 struct drm_connector *connector = &ctx->connector;
107 ctx->encoder = encoder;
109 connector->polled = DRM_CONNECTOR_POLL_HPD;
111 ret = drm_connector_init(encoder->dev, connector,
112 &exynos_dpi_connector_funcs,
113 DRM_MODE_CONNECTOR_VGA);
115 DRM_ERROR("failed to initialize connector with drm\n");
119 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
120 drm_connector_register(connector);
121 drm_mode_connector_attach_encoder(connector, encoder);
126 static void exynos_dpi_poweron(struct exynos_dpi *ctx)
129 drm_panel_prepare(ctx->panel);
130 drm_panel_enable(ctx->panel);
134 static void exynos_dpi_poweroff(struct exynos_dpi *ctx)
137 drm_panel_disable(ctx->panel);
138 drm_panel_unprepare(ctx->panel);
142 static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode)
144 struct exynos_dpi *ctx = display->ctx;
147 case DRM_MODE_DPMS_ON:
148 if (ctx->dpms_mode != DRM_MODE_DPMS_ON)
149 exynos_dpi_poweron(ctx);
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);
160 ctx->dpms_mode = mode;
163 static struct exynos_drm_display_ops exynos_dpi_display_ops = {
164 .create_connector = exynos_dpi_create_connector,
165 .dpms = exynos_dpi_dpms
168 static struct exynos_drm_display exynos_dpi_display = {
169 .type = EXYNOS_DISPLAY_TYPE_LCD,
170 .ops = &exynos_dpi_display_ops,
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)
177 struct device_node *np;
179 for_each_child_of_node(parent, np) {
182 if (!np->name || of_node_cmp(np->name, name))
185 if (of_property_read_u32(np, "reg", &r) < 0)
195 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
198 struct device_node *ports, *port;
200 ports = of_get_child_by_name(parent, "ports");
204 port = of_get_child_by_name_reg(parent, "port", reg);
211 static struct device_node *
212 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
214 return of_get_child_by_name_reg(port, "endpoint", reg);
217 static struct device_node *
218 of_graph_get_remote_port_parent(const struct device_node *node)
220 struct device_node *np;
223 np = of_parse_phandle(node, "remote-endpoint", 0);
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"))
242 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
244 struct device_node *np, *ep;
246 np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
250 ep = of_graph_get_endpoint_by_reg(np, 0);
255 np = of_graph_get_remote_port_parent(ep);
261 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
263 struct device *dev = ctx->dev;
264 struct device_node *dn = dev->of_node;
265 struct device_node *np;
267 ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
269 np = of_get_child_by_name(dn, "display-timings");
271 struct videomode *vm;
276 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
280 ret = of_get_videomode(dn, vm, 0);
291 if (!ctx->panel_node)
297 struct exynos_drm_display *exynos_dpi_probe(struct device *dev)
299 struct exynos_dpi *ctx;
302 ret = exynos_drm_component_add(dev,
303 EXYNOS_DEVICE_TYPE_CONNECTOR,
304 exynos_dpi_display.type);
308 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
310 goto err_del_component;
313 exynos_dpi_display.ctx = ctx;
314 ctx->dpms_mode = DRM_MODE_DPMS_OFF;
316 ret = exynos_dpi_parse_dt(ctx);
318 devm_kfree(dev, ctx);
319 goto err_del_component;
322 if (ctx->panel_node) {
323 ctx->panel = of_drm_find_panel(ctx->panel_node);
325 exynos_drm_component_del(dev,
326 EXYNOS_DEVICE_TYPE_CONNECTOR);
327 return ERR_PTR(-EPROBE_DEFER);
331 return &exynos_dpi_display;
334 exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
339 int exynos_dpi_remove(struct device *dev)
341 struct drm_encoder *encoder = exynos_dpi_display.encoder;
342 struct exynos_dpi *ctx = exynos_dpi_display.ctx;
344 exynos_dpi_dpms(&exynos_dpi_display, DRM_MODE_DPMS_OFF);
345 encoder->funcs->destroy(encoder);
346 drm_connector_cleanup(&ctx->connector);
348 exynos_drm_component_del(dev, EXYNOS_DEVICE_TYPE_CONNECTOR);