1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Exynos DRM Parallel output support.
4  *
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd
6  *
7  * Contacts: Andrzej Hajda <a.hajda@samsung.com>
8 */
9 
10 #include <linux/of_graph.h>
11 #include <linux/regulator/consumer.h>
12 
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_panel.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_simple_kms_helper.h>
18 
19 #include <video/of_videomode.h>
20 #include <video/videomode.h>
21 
22 #include "exynos_drm_crtc.h"
23 
24 struct exynos_dpi {
25 	struct drm_encoder encoder;
26 	struct device *dev;
27 	struct device_node *panel_node;
28 
29 	struct drm_panel *panel;
30 	struct drm_connector connector;
31 
32 	struct videomode *vm;
33 };
34 
35 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
36 
37 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
38 {
39 	return container_of(e, struct exynos_dpi, encoder);
40 }
41 
42 static enum drm_connector_status
43 exynos_dpi_detect(struct drm_connector *connector, bool force)
44 {
45 	struct exynos_dpi *ctx = connector_to_dpi(connector);
46 
47 	if (ctx->panel)
48 		drm_panel_attach(ctx->panel, &ctx->connector);
49 
50 	return connector_status_connected;
51 }
52 
53 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
54 {
55 	drm_connector_unregister(connector);
56 	drm_connector_cleanup(connector);
57 }
58 
59 static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
60 	.detect = exynos_dpi_detect,
61 	.fill_modes = drm_helper_probe_single_connector_modes,
62 	.destroy = exynos_dpi_connector_destroy,
63 	.reset = drm_atomic_helper_connector_reset,
64 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
65 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
66 };
67 
68 static int exynos_dpi_get_modes(struct drm_connector *connector)
69 {
70 	struct exynos_dpi *ctx = connector_to_dpi(connector);
71 
72 	/* fimd timings gets precedence over panel modes */
73 	if (ctx->vm) {
74 		struct drm_display_mode *mode;
75 
76 		mode = drm_mode_create(connector->dev);
77 		if (!mode) {
78 			DRM_DEV_ERROR(ctx->dev,
79 				      "failed to create a new display mode\n");
80 			return 0;
81 		}
82 		drm_display_mode_from_videomode(ctx->vm, mode);
83 		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
84 		drm_mode_probed_add(connector, mode);
85 		return 1;
86 	}
87 
88 	if (ctx->panel)
89 		return drm_panel_get_modes(ctx->panel, connector);
90 
91 	return 0;
92 }
93 
94 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
95 	.get_modes = exynos_dpi_get_modes,
96 };
97 
98 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
99 {
100 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
101 	struct drm_connector *connector = &ctx->connector;
102 	int ret;
103 
104 	connector->polled = DRM_CONNECTOR_POLL_HPD;
105 
106 	ret = drm_connector_init(encoder->dev, connector,
107 				 &exynos_dpi_connector_funcs,
108 				 DRM_MODE_CONNECTOR_VGA);
109 	if (ret) {
110 		DRM_DEV_ERROR(ctx->dev,
111 			      "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 enum {
154 	FIMD_PORT_IN0,
155 	FIMD_PORT_IN1,
156 	FIMD_PORT_IN2,
157 	FIMD_PORT_RGB,
158 	FIMD_PORT_WRB,
159 };
160 
161 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
162 {
163 	struct device *dev = ctx->dev;
164 	struct device_node *dn = dev->of_node;
165 	struct device_node *np;
166 
167 	ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
168 
169 	np = of_get_child_by_name(dn, "display-timings");
170 	if (np) {
171 		struct videomode *vm;
172 		int ret;
173 
174 		of_node_put(np);
175 
176 		vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
177 		if (!vm)
178 			return -ENOMEM;
179 
180 		ret = of_get_videomode(dn, vm, 0);
181 		if (ret < 0) {
182 			devm_kfree(dev, vm);
183 			return ret;
184 		}
185 
186 		ctx->vm = vm;
187 
188 		return 0;
189 	}
190 
191 	if (!ctx->panel_node)
192 		return -EINVAL;
193 
194 	return 0;
195 }
196 
197 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
198 {
199 	int ret;
200 
201 	drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
202 
203 	drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
204 
205 	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
206 	if (ret < 0)
207 		return ret;
208 
209 	ret = exynos_dpi_create_connector(encoder);
210 	if (ret) {
211 		DRM_DEV_ERROR(encoder_to_dpi(encoder)->dev,
212 			      "failed to create connector ret = %d\n", ret);
213 		drm_encoder_cleanup(encoder);
214 		return ret;
215 	}
216 
217 	return 0;
218 }
219 
220 struct drm_encoder *exynos_dpi_probe(struct device *dev)
221 {
222 	struct exynos_dpi *ctx;
223 	int ret;
224 
225 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
226 	if (!ctx)
227 		return ERR_PTR(-ENOMEM);
228 
229 	ctx->dev = dev;
230 
231 	ret = exynos_dpi_parse_dt(ctx);
232 	if (ret < 0) {
233 		devm_kfree(dev, ctx);
234 		return NULL;
235 	}
236 
237 	if (ctx->panel_node) {
238 		ctx->panel = of_drm_find_panel(ctx->panel_node);
239 		if (IS_ERR(ctx->panel))
240 			return ERR_CAST(ctx->panel);
241 	}
242 
243 	return &ctx->encoder;
244 }
245 
246 int exynos_dpi_remove(struct drm_encoder *encoder)
247 {
248 	struct exynos_dpi *ctx = encoder_to_dpi(encoder);
249 
250 	exynos_dpi_disable(&ctx->encoder);
251 
252 	if (ctx->panel)
253 		drm_panel_detach(ctx->panel);
254 
255 	return 0;
256 }
257