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 #include <drm/drm_atomic_helper.h>
17 
18 #include <linux/of_graph.h>
19 #include <linux/regulator/consumer.h>
20 
21 #include <video/of_videomode.h>
22 #include <video/videomode.h>
23 
24 #include "exynos_drm_crtc.h"
25 
26 struct exynos_dpi {
27 	struct drm_encoder encoder;
28 	struct device *dev;
29 	struct device_node *panel_node;
30 
31 	struct drm_panel *panel;
32 	struct drm_connector connector;
33 
34 	struct videomode *vm;
35 };
36 
37 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
38 
39 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
40 {
41 	return container_of(e, struct exynos_dpi, encoder);
42 }
43 
44 static enum drm_connector_status
45 exynos_dpi_detect(struct drm_connector *connector, bool force)
46 {
47 	struct exynos_dpi *ctx = connector_to_dpi(connector);
48 
49 	if (ctx->panel && !ctx->panel->connector)
50 		drm_panel_attach(ctx->panel, &ctx->connector);
51 
52 	return connector_status_connected;
53 }
54 
55 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
56 {
57 	drm_connector_unregister(connector);
58 	drm_connector_cleanup(connector);
59 }
60 
61 static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
62 	.dpms = drm_atomic_helper_connector_dpms,
63 	.detect = exynos_dpi_detect,
64 	.fill_modes = drm_helper_probe_single_connector_modes,
65 	.destroy = exynos_dpi_connector_destroy,
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,
69 };
70 
71 static 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 
96 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
97 	.get_modes = exynos_dpi_get_modes,
98 };
99 
100 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
101 {
102 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
103 	struct drm_connector *connector = &ctx->connector;
104 	int ret;
105 
106 	connector->polled = DRM_CONNECTOR_POLL_HPD;
107 
108 	ret = drm_connector_init(encoder->dev, connector,
109 				 &exynos_dpi_connector_funcs,
110 				 DRM_MODE_CONNECTOR_VGA);
111 	if (ret) {
112 		DRM_ERROR("failed to initialize connector with drm\n");
113 		return ret;
114 	}
115 
116 	drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
117 	drm_connector_register(connector);
118 	drm_mode_connector_attach_encoder(connector, encoder);
119 
120 	return 0;
121 }
122 
123 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
124 				struct drm_display_mode *mode,
125 				struct drm_display_mode *adjusted_mode)
126 {
127 }
128 
129 static void exynos_dpi_enable(struct drm_encoder *encoder)
130 {
131 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
132 
133 	if (ctx->panel) {
134 		drm_panel_prepare(ctx->panel);
135 		drm_panel_enable(ctx->panel);
136 	}
137 }
138 
139 static void exynos_dpi_disable(struct drm_encoder *encoder)
140 {
141 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
142 
143 	if (ctx->panel) {
144 		drm_panel_disable(ctx->panel);
145 		drm_panel_unprepare(ctx->panel);
146 	}
147 }
148 
149 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
150 	.mode_set = exynos_dpi_mode_set,
151 	.enable = exynos_dpi_enable,
152 	.disable = exynos_dpi_disable,
153 };
154 
155 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
156 	.destroy = drm_encoder_cleanup,
157 };
158 
159 enum {
160 	FIMD_PORT_IN0,
161 	FIMD_PORT_IN1,
162 	FIMD_PORT_IN2,
163 	FIMD_PORT_RGB,
164 	FIMD_PORT_WRB,
165 };
166 
167 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
168 {
169 	struct device_node *np, *ep;
170 
171 	ep = of_graph_get_endpoint_by_regs(dev->of_node, FIMD_PORT_RGB, 0);
172 	if (!ep)
173 		return NULL;
174 
175 	np = of_graph_get_remote_port_parent(ep);
176 	of_node_put(ep);
177 
178 	return np;
179 }
180 
181 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
182 {
183 	struct device *dev = ctx->dev;
184 	struct device_node *dn = dev->of_node;
185 	struct device_node *np;
186 
187 	ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
188 
189 	np = of_get_child_by_name(dn, "display-timings");
190 	if (np) {
191 		struct videomode *vm;
192 		int ret;
193 
194 		of_node_put(np);
195 
196 		vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
197 		if (!vm)
198 			return -ENOMEM;
199 
200 		ret = of_get_videomode(dn, vm, 0);
201 		if (ret < 0) {
202 			devm_kfree(dev, vm);
203 			return ret;
204 		}
205 
206 		ctx->vm = vm;
207 
208 		return 0;
209 	}
210 
211 	if (!ctx->panel_node)
212 		return -EINVAL;
213 
214 	return 0;
215 }
216 
217 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
218 {
219 	int ret;
220 
221 	ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD);
222 	if (ret < 0)
223 		return ret;
224 
225 	encoder->possible_crtcs = 1 << ret;
226 
227 	DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
228 
229 	drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
230 			 DRM_MODE_ENCODER_TMDS, NULL);
231 
232 	drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
233 
234 	ret = exynos_dpi_create_connector(encoder);
235 	if (ret) {
236 		DRM_ERROR("failed to create connector ret = %d\n", ret);
237 		drm_encoder_cleanup(encoder);
238 		return ret;
239 	}
240 
241 	return 0;
242 }
243 
244 struct drm_encoder *exynos_dpi_probe(struct device *dev)
245 {
246 	struct exynos_dpi *ctx;
247 	int ret;
248 
249 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
250 	if (!ctx)
251 		return ERR_PTR(-ENOMEM);
252 
253 	ctx->dev = dev;
254 
255 	ret = exynos_dpi_parse_dt(ctx);
256 	if (ret < 0) {
257 		devm_kfree(dev, ctx);
258 		return NULL;
259 	}
260 
261 	if (ctx->panel_node) {
262 		ctx->panel = of_drm_find_panel(ctx->panel_node);
263 		if (!ctx->panel)
264 			return ERR_PTR(-EPROBE_DEFER);
265 	}
266 
267 	return &ctx->encoder;
268 }
269 
270 int exynos_dpi_remove(struct drm_encoder *encoder)
271 {
272 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
273 
274 	exynos_dpi_disable(&ctx->encoder);
275 
276 	if (ctx->panel)
277 		drm_panel_detach(ctx->panel);
278 
279 	return 0;
280 }
281