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 	.detect = exynos_dpi_detect,
63 	.fill_modes = drm_helper_probe_single_connector_modes,
64 	.destroy = exynos_dpi_connector_destroy,
65 	.reset = drm_atomic_helper_connector_reset,
66 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
67 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
68 };
69 
70 static int exynos_dpi_get_modes(struct drm_connector *connector)
71 {
72 	struct exynos_dpi *ctx = connector_to_dpi(connector);
73 
74 	/* fimd timings gets precedence over panel modes */
75 	if (ctx->vm) {
76 		struct drm_display_mode *mode;
77 
78 		mode = drm_mode_create(connector->dev);
79 		if (!mode) {
80 			DRM_ERROR("failed to create a new display mode\n");
81 			return 0;
82 		}
83 		drm_display_mode_from_videomode(ctx->vm, mode);
84 		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
85 		drm_mode_probed_add(connector, mode);
86 		return 1;
87 	}
88 
89 	if (ctx->panel)
90 		return ctx->panel->funcs->get_modes(ctx->panel);
91 
92 	return 0;
93 }
94 
95 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
96 	.get_modes = exynos_dpi_get_modes,
97 };
98 
99 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
100 {
101 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
102 	struct drm_connector *connector = &ctx->connector;
103 	int ret;
104 
105 	connector->polled = DRM_CONNECTOR_POLL_HPD;
106 
107 	ret = drm_connector_init(encoder->dev, connector,
108 				 &exynos_dpi_connector_funcs,
109 				 DRM_MODE_CONNECTOR_VGA);
110 	if (ret) {
111 		DRM_ERROR("failed to initialize connector with drm\n");
112 		return ret;
113 	}
114 
115 	drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
116 	drm_connector_attach_encoder(connector, encoder);
117 
118 	return 0;
119 }
120 
121 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
122 				struct drm_display_mode *mode,
123 				struct drm_display_mode *adjusted_mode)
124 {
125 }
126 
127 static void exynos_dpi_enable(struct drm_encoder *encoder)
128 {
129 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
130 
131 	if (ctx->panel) {
132 		drm_panel_prepare(ctx->panel);
133 		drm_panel_enable(ctx->panel);
134 	}
135 }
136 
137 static void exynos_dpi_disable(struct drm_encoder *encoder)
138 {
139 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
140 
141 	if (ctx->panel) {
142 		drm_panel_disable(ctx->panel);
143 		drm_panel_unprepare(ctx->panel);
144 	}
145 }
146 
147 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
148 	.mode_set = exynos_dpi_mode_set,
149 	.enable = exynos_dpi_enable,
150 	.disable = exynos_dpi_disable,
151 };
152 
153 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
154 	.destroy = drm_encoder_cleanup,
155 };
156 
157 enum {
158 	FIMD_PORT_IN0,
159 	FIMD_PORT_IN1,
160 	FIMD_PORT_IN2,
161 	FIMD_PORT_RGB,
162 	FIMD_PORT_WRB,
163 };
164 
165 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
166 {
167 	struct device *dev = ctx->dev;
168 	struct device_node *dn = dev->of_node;
169 	struct device_node *np;
170 
171 	ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
172 
173 	np = of_get_child_by_name(dn, "display-timings");
174 	if (np) {
175 		struct videomode *vm;
176 		int ret;
177 
178 		of_node_put(np);
179 
180 		vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
181 		if (!vm)
182 			return -ENOMEM;
183 
184 		ret = of_get_videomode(dn, vm, 0);
185 		if (ret < 0) {
186 			devm_kfree(dev, vm);
187 			return ret;
188 		}
189 
190 		ctx->vm = vm;
191 
192 		return 0;
193 	}
194 
195 	if (!ctx->panel_node)
196 		return -EINVAL;
197 
198 	return 0;
199 }
200 
201 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
202 {
203 	int ret;
204 
205 	drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
206 			 DRM_MODE_ENCODER_TMDS, NULL);
207 
208 	drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
209 
210 	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
211 	if (ret < 0)
212 		return ret;
213 
214 	ret = exynos_dpi_create_connector(encoder);
215 	if (ret) {
216 		DRM_ERROR("failed to create connector ret = %d\n", ret);
217 		drm_encoder_cleanup(encoder);
218 		return ret;
219 	}
220 
221 	return 0;
222 }
223 
224 struct drm_encoder *exynos_dpi_probe(struct device *dev)
225 {
226 	struct exynos_dpi *ctx;
227 	int ret;
228 
229 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
230 	if (!ctx)
231 		return ERR_PTR(-ENOMEM);
232 
233 	ctx->dev = dev;
234 
235 	ret = exynos_dpi_parse_dt(ctx);
236 	if (ret < 0) {
237 		devm_kfree(dev, ctx);
238 		return NULL;
239 	}
240 
241 	if (ctx->panel_node) {
242 		ctx->panel = of_drm_find_panel(ctx->panel_node);
243 		if (IS_ERR(ctx->panel))
244 			return ERR_CAST(ctx->panel);
245 	}
246 
247 	return &ctx->encoder;
248 }
249 
250 int exynos_dpi_remove(struct drm_encoder *encoder)
251 {
252 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
253 
254 	exynos_dpi_disable(&ctx->encoder);
255 
256 	if (ctx->panel)
257 		drm_panel_detach(ctx->panel);
258 
259 	return 0;
260 }
261