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 	/* 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 
59 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
60 {
61 	drm_sysfs_connector_remove(connector);
62 	drm_connector_cleanup(connector);
63 }
64 
65 static 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 
72 static 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 
97 static int exynos_dpi_mode_valid(struct drm_connector *connector,
98 				 struct drm_display_mode *mode)
99 {
100 	return MODE_OK;
101 }
102 
103 static struct drm_encoder *
104 exynos_dpi_best_encoder(struct drm_connector *connector)
105 {
106 	struct exynos_dpi *ctx = connector_to_dpi(connector);
107 
108 	return ctx->encoder;
109 }
110 
111 static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
112 	.get_modes = exynos_dpi_get_modes,
113 	.mode_valid = exynos_dpi_mode_valid,
114 	.best_encoder = exynos_dpi_best_encoder,
115 };
116 
117 static int exynos_dpi_create_connector(struct exynos_drm_display *display,
118 				       struct drm_encoder *encoder)
119 {
120 	struct exynos_dpi *ctx = display->ctx;
121 	struct drm_connector *connector = &ctx->connector;
122 	int ret;
123 
124 	ctx->encoder = encoder;
125 
126 	if (ctx->panel_node)
127 		connector->polled = DRM_CONNECTOR_POLL_CONNECT;
128 	else
129 		connector->polled = DRM_CONNECTOR_POLL_HPD;
130 
131 	ret = drm_connector_init(encoder->dev, connector,
132 				 &exynos_dpi_connector_funcs,
133 				 DRM_MODE_CONNECTOR_VGA);
134 	if (ret) {
135 		DRM_ERROR("failed to initialize connector with drm\n");
136 		return ret;
137 	}
138 
139 	drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
140 	drm_sysfs_connector_add(connector);
141 	drm_mode_connector_attach_encoder(connector, encoder);
142 
143 	return 0;
144 }
145 
146 static void exynos_dpi_poweron(struct exynos_dpi *ctx)
147 {
148 	if (ctx->panel)
149 		drm_panel_enable(ctx->panel);
150 }
151 
152 static void exynos_dpi_poweroff(struct exynos_dpi *ctx)
153 {
154 	if (ctx->panel)
155 		drm_panel_disable(ctx->panel);
156 }
157 
158 static void exynos_dpi_dpms(struct exynos_drm_display *display, int mode)
159 {
160 	struct exynos_dpi *ctx = display->ctx;
161 
162 	switch (mode) {
163 	case DRM_MODE_DPMS_ON:
164 		if (ctx->dpms_mode != DRM_MODE_DPMS_ON)
165 				exynos_dpi_poweron(ctx);
166 			break;
167 	case DRM_MODE_DPMS_STANDBY:
168 	case DRM_MODE_DPMS_SUSPEND:
169 	case DRM_MODE_DPMS_OFF:
170 		if (ctx->dpms_mode == DRM_MODE_DPMS_ON)
171 			exynos_dpi_poweroff(ctx);
172 		break;
173 	default:
174 		break;
175 	};
176 	ctx->dpms_mode = mode;
177 }
178 
179 static struct exynos_drm_display_ops exynos_dpi_display_ops = {
180 	.create_connector = exynos_dpi_create_connector,
181 	.dpms = exynos_dpi_dpms
182 };
183 
184 static struct exynos_drm_display exynos_dpi_display = {
185 	.type = EXYNOS_DISPLAY_TYPE_LCD,
186 	.ops = &exynos_dpi_display_ops,
187 };
188 
189 /* of_* functions will be removed after merge of of_graph patches */
190 static struct device_node *
191 of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
192 {
193 	struct device_node *np;
194 
195 	for_each_child_of_node(parent, np) {
196 		u32 r;
197 
198 		if (!np->name || of_node_cmp(np->name, name))
199 			continue;
200 
201 		if (of_property_read_u32(np, "reg", &r) < 0)
202 			r = 0;
203 
204 		if (reg == r)
205 			break;
206 	}
207 
208 	return np;
209 }
210 
211 static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
212 						    u32 reg)
213 {
214 	struct device_node *ports, *port;
215 
216 	ports = of_get_child_by_name(parent, "ports");
217 	if (ports)
218 		parent = ports;
219 
220 	port = of_get_child_by_name_reg(parent, "port", reg);
221 
222 	of_node_put(ports);
223 
224 	return port;
225 }
226 
227 static struct device_node *
228 of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
229 {
230 	return of_get_child_by_name_reg(port, "endpoint", reg);
231 }
232 
233 static struct device_node *
234 of_graph_get_remote_port_parent(const struct device_node *node)
235 {
236 	struct device_node *np;
237 	unsigned int depth;
238 
239 	np = of_parse_phandle(node, "remote-endpoint", 0);
240 
241 	/* Walk 3 levels up only if there is 'ports' node. */
242 	for (depth = 3; depth && np; depth--) {
243 		np = of_get_next_parent(np);
244 		if (depth == 2 && of_node_cmp(np->name, "ports"))
245 			break;
246 	}
247 	return np;
248 }
249 
250 enum {
251 	FIMD_PORT_IN0,
252 	FIMD_PORT_IN1,
253 	FIMD_PORT_IN2,
254 	FIMD_PORT_RGB,
255 	FIMD_PORT_WRB,
256 };
257 
258 static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
259 {
260 	struct device_node *np, *ep;
261 
262 	np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
263 	if (!np)
264 		return NULL;
265 
266 	ep = of_graph_get_endpoint_by_reg(np, 0);
267 	of_node_put(np);
268 	if (!ep)
269 		return NULL;
270 
271 	np = of_graph_get_remote_port_parent(ep);
272 	of_node_put(ep);
273 
274 	return np;
275 }
276 
277 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
278 {
279 	struct device *dev = ctx->dev;
280 	struct device_node *dn = dev->of_node;
281 	struct device_node *np;
282 
283 	ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
284 
285 	np = of_get_child_by_name(dn, "display-timings");
286 	if (np) {
287 		struct videomode *vm;
288 		int ret;
289 
290 		of_node_put(np);
291 
292 		vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
293 		if (!vm)
294 			return -ENOMEM;
295 
296 		ret = of_get_videomode(dn, vm, 0);
297 		if (ret < 0)
298 			return ret;
299 
300 		ctx->vm = vm;
301 
302 		return 0;
303 	}
304 
305 	if (!ctx->panel_node)
306 		return -EINVAL;
307 
308 	return 0;
309 }
310 
311 int exynos_dpi_probe(struct device *dev)
312 {
313 	struct exynos_dpi *ctx;
314 	int ret;
315 
316 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
317 	if (!ctx)
318 		return -ENOMEM;
319 
320 	ctx->dev = dev;
321 	exynos_dpi_display.ctx = ctx;
322 	ctx->dpms_mode = DRM_MODE_DPMS_OFF;
323 
324 	ret = exynos_dpi_parse_dt(ctx);
325 	if (ret < 0)
326 		return ret;
327 
328 	exynos_drm_display_register(&exynos_dpi_display);
329 
330 	return 0;
331 }
332 
333 int exynos_dpi_remove(struct device *dev)
334 {
335 	exynos_dpi_dpms(&exynos_dpi_display, DRM_MODE_DPMS_OFF);
336 	exynos_drm_display_unregister(&exynos_dpi_display);
337 
338 	return 0;
339 }
340