• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * i.MX drm driver - parallel display implementation
4  *
5  * Copyright (C) 2012 Sascha Hauer, Pengutronix
6  */
7 
8 #include <linux/component.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/videodev2.h>
12 
13 #include <video/of_display_timing.h>
14 
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_bridge.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_managed.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_panel.h>
21 #include <drm/drm_probe_helper.h>
22 #include <drm/drm_simple_kms_helper.h>
23 
24 #include "imx-drm.h"
25 
26 struct imx_parallel_display_encoder {
27 	struct drm_connector connector;
28 	struct drm_encoder encoder;
29 	struct drm_bridge bridge;
30 	struct imx_parallel_display *pd;
31 };
32 
33 struct imx_parallel_display {
34 	struct device *dev;
35 	void *edid;
36 	u32 bus_format;
37 	u32 bus_flags;
38 	struct drm_display_mode mode;
39 	struct drm_panel *panel;
40 	struct drm_bridge *next_bridge;
41 };
42 
con_to_imxpd(struct drm_connector * c)43 static inline struct imx_parallel_display *con_to_imxpd(struct drm_connector *c)
44 {
45 	return container_of(c, struct imx_parallel_display_encoder, connector)->pd;
46 }
47 
bridge_to_imxpd(struct drm_bridge * b)48 static inline struct imx_parallel_display *bridge_to_imxpd(struct drm_bridge *b)
49 {
50 	return container_of(b, struct imx_parallel_display_encoder, bridge)->pd;
51 }
52 
imx_pd_connector_get_modes(struct drm_connector * connector)53 static int imx_pd_connector_get_modes(struct drm_connector *connector)
54 {
55 	struct imx_parallel_display *imxpd = con_to_imxpd(connector);
56 	struct device_node *np = imxpd->dev->of_node;
57 	int num_modes;
58 
59 	num_modes = drm_panel_get_modes(imxpd->panel, connector);
60 	if (num_modes > 0)
61 		return num_modes;
62 
63 	if (imxpd->edid) {
64 		drm_connector_update_edid_property(connector, imxpd->edid);
65 		num_modes = drm_add_edid_modes(connector, imxpd->edid);
66 	}
67 
68 	if (np) {
69 		struct drm_display_mode *mode = drm_mode_create(connector->dev);
70 		int ret;
71 
72 		if (!mode)
73 			return -EINVAL;
74 
75 		ret = of_get_drm_display_mode(np, &imxpd->mode,
76 					      &imxpd->bus_flags,
77 					      OF_USE_NATIVE_MODE);
78 		if (ret) {
79 			drm_mode_destroy(connector->dev, mode);
80 			return ret;
81 		}
82 
83 		drm_mode_copy(mode, &imxpd->mode);
84 		mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
85 		drm_mode_probed_add(connector, mode);
86 		num_modes++;
87 	}
88 
89 	return num_modes;
90 }
91 
imx_pd_bridge_enable(struct drm_bridge * bridge)92 static void imx_pd_bridge_enable(struct drm_bridge *bridge)
93 {
94 	struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge);
95 
96 	drm_panel_prepare(imxpd->panel);
97 	drm_panel_enable(imxpd->panel);
98 }
99 
imx_pd_bridge_disable(struct drm_bridge * bridge)100 static void imx_pd_bridge_disable(struct drm_bridge *bridge)
101 {
102 	struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge);
103 
104 	drm_panel_disable(imxpd->panel);
105 	drm_panel_unprepare(imxpd->panel);
106 }
107 
108 static const u32 imx_pd_bus_fmts[] = {
109 	MEDIA_BUS_FMT_RGB888_1X24,
110 	MEDIA_BUS_FMT_BGR888_1X24,
111 	MEDIA_BUS_FMT_GBR888_1X24,
112 	MEDIA_BUS_FMT_RGB666_1X18,
113 	MEDIA_BUS_FMT_RGB666_1X24_CPADHI,
114 	MEDIA_BUS_FMT_RGB565_1X16,
115 };
116 
117 static u32 *
imx_pd_bridge_atomic_get_output_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,unsigned int * num_output_fmts)118 imx_pd_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
119 					 struct drm_bridge_state *bridge_state,
120 					 struct drm_crtc_state *crtc_state,
121 					 struct drm_connector_state *conn_state,
122 					 unsigned int *num_output_fmts)
123 {
124 	struct drm_display_info *di = &conn_state->connector->display_info;
125 	struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge);
126 	u32 *output_fmts;
127 
128 	if (!imxpd->bus_format && !di->num_bus_formats) {
129 		*num_output_fmts = ARRAY_SIZE(imx_pd_bus_fmts);
130 		return kmemdup(imx_pd_bus_fmts, sizeof(imx_pd_bus_fmts),
131 			       GFP_KERNEL);
132 	}
133 
134 	*num_output_fmts = 1;
135 	output_fmts = kmalloc(sizeof(*output_fmts), GFP_KERNEL);
136 	if (!output_fmts)
137 		return NULL;
138 
139 	if (!imxpd->bus_format && di->num_bus_formats)
140 		output_fmts[0] = di->bus_formats[0];
141 	else
142 		output_fmts[0] = imxpd->bus_format;
143 
144 	return output_fmts;
145 }
146 
imx_pd_format_supported(u32 output_fmt)147 static bool imx_pd_format_supported(u32 output_fmt)
148 {
149 	unsigned int i;
150 
151 	for (i = 0; i < ARRAY_SIZE(imx_pd_bus_fmts); i++) {
152 		if (imx_pd_bus_fmts[i] == output_fmt)
153 			return true;
154 	}
155 
156 	return false;
157 }
158 
159 static u32 *
imx_pd_bridge_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)160 imx_pd_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
161 					struct drm_bridge_state *bridge_state,
162 					struct drm_crtc_state *crtc_state,
163 					struct drm_connector_state *conn_state,
164 					u32 output_fmt,
165 					unsigned int *num_input_fmts)
166 {
167 	struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge);
168 	u32 *input_fmts;
169 
170 	/*
171 	 * If the next bridge does not support bus format negotiation, let's
172 	 * use the static bus format definition (imxpd->bus_format) if it's
173 	 * specified, RGB888 when it's not.
174 	 */
175 	if (output_fmt == MEDIA_BUS_FMT_FIXED)
176 		output_fmt = imxpd->bus_format ? : MEDIA_BUS_FMT_RGB888_1X24;
177 
178 	/* Now make sure the requested output format is supported. */
179 	if ((imxpd->bus_format && imxpd->bus_format != output_fmt) ||
180 	    !imx_pd_format_supported(output_fmt)) {
181 		*num_input_fmts = 0;
182 		return NULL;
183 	}
184 
185 	*num_input_fmts = 1;
186 	input_fmts = kmalloc(sizeof(*input_fmts), GFP_KERNEL);
187 	if (!input_fmts)
188 		return NULL;
189 
190 	input_fmts[0] = output_fmt;
191 	return input_fmts;
192 }
193 
imx_pd_bridge_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)194 static int imx_pd_bridge_atomic_check(struct drm_bridge *bridge,
195 				      struct drm_bridge_state *bridge_state,
196 				      struct drm_crtc_state *crtc_state,
197 				      struct drm_connector_state *conn_state)
198 {
199 	struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
200 	struct drm_display_info *di = &conn_state->connector->display_info;
201 	struct imx_parallel_display *imxpd = bridge_to_imxpd(bridge);
202 	struct drm_bridge_state *next_bridge_state = NULL;
203 	struct drm_bridge *next_bridge;
204 	u32 bus_flags, bus_fmt;
205 
206 	next_bridge = drm_bridge_get_next_bridge(bridge);
207 	if (next_bridge)
208 		next_bridge_state = drm_atomic_get_new_bridge_state(crtc_state->state,
209 								    next_bridge);
210 
211 	if (next_bridge_state)
212 		bus_flags = next_bridge_state->input_bus_cfg.flags;
213 	else if (di->num_bus_formats)
214 		bus_flags = di->bus_flags;
215 	else
216 		bus_flags = imxpd->bus_flags;
217 
218 	bus_fmt = bridge_state->input_bus_cfg.format;
219 	if (!imx_pd_format_supported(bus_fmt))
220 		return -EINVAL;
221 
222 	bridge_state->output_bus_cfg.flags = bus_flags;
223 	bridge_state->input_bus_cfg.flags = bus_flags;
224 	imx_crtc_state->bus_flags = bus_flags;
225 	imx_crtc_state->bus_format = bridge_state->input_bus_cfg.format;
226 	imx_crtc_state->di_hsync_pin = 2;
227 	imx_crtc_state->di_vsync_pin = 3;
228 
229 	return 0;
230 }
231 
232 static const struct drm_connector_funcs imx_pd_connector_funcs = {
233 	.fill_modes = drm_helper_probe_single_connector_modes,
234 	.destroy = imx_drm_connector_destroy,
235 	.reset = drm_atomic_helper_connector_reset,
236 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
237 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
238 };
239 
240 static const struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
241 	.get_modes = imx_pd_connector_get_modes,
242 };
243 
244 static const struct drm_bridge_funcs imx_pd_bridge_funcs = {
245 	.enable = imx_pd_bridge_enable,
246 	.disable = imx_pd_bridge_disable,
247 	.atomic_reset = drm_atomic_helper_bridge_reset,
248 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
249 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
250 	.atomic_check = imx_pd_bridge_atomic_check,
251 	.atomic_get_input_bus_fmts = imx_pd_bridge_atomic_get_input_bus_fmts,
252 	.atomic_get_output_bus_fmts = imx_pd_bridge_atomic_get_output_bus_fmts,
253 };
254 
imx_pd_bind(struct device * dev,struct device * master,void * data)255 static int imx_pd_bind(struct device *dev, struct device *master, void *data)
256 {
257 	struct drm_device *drm = data;
258 	struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
259 	struct imx_parallel_display_encoder *imxpd_encoder;
260 	struct drm_connector *connector;
261 	struct drm_encoder *encoder;
262 	struct drm_bridge *bridge;
263 	int ret;
264 
265 	imxpd_encoder = drmm_simple_encoder_alloc(drm, struct imx_parallel_display_encoder,
266 						  encoder, DRM_MODE_ENCODER_NONE);
267 	if (IS_ERR(imxpd_encoder))
268 		return PTR_ERR(imxpd_encoder);
269 
270 	imxpd_encoder->pd = imxpd;
271 	connector = &imxpd_encoder->connector;
272 	encoder = &imxpd_encoder->encoder;
273 	bridge = &imxpd_encoder->bridge;
274 
275 	ret = imx_drm_encoder_parse_of(drm, encoder, imxpd->dev->of_node);
276 	if (ret)
277 		return ret;
278 
279 	/* set the connector's dpms to OFF so that
280 	 * drm_helper_connector_dpms() won't return
281 	 * immediately since the current state is ON
282 	 * at this point.
283 	 */
284 	connector->dpms = DRM_MODE_DPMS_OFF;
285 
286 	bridge->funcs = &imx_pd_bridge_funcs;
287 	drm_bridge_attach(encoder, bridge, NULL, 0);
288 
289 	if (imxpd->next_bridge) {
290 		ret = drm_bridge_attach(encoder, imxpd->next_bridge, bridge, 0);
291 		if (ret < 0)
292 			return ret;
293 	} else {
294 		drm_connector_helper_add(connector,
295 					 &imx_pd_connector_helper_funcs);
296 		drm_connector_init(drm, connector, &imx_pd_connector_funcs,
297 				   DRM_MODE_CONNECTOR_DPI);
298 
299 		drm_connector_attach_encoder(connector, encoder);
300 	}
301 
302 	return 0;
303 }
304 
305 static const struct component_ops imx_pd_ops = {
306 	.bind	= imx_pd_bind,
307 };
308 
imx_pd_probe(struct platform_device * pdev)309 static int imx_pd_probe(struct platform_device *pdev)
310 {
311 	struct device *dev = &pdev->dev;
312 	struct device_node *np = dev->of_node;
313 	const u8 *edidp;
314 	struct imx_parallel_display *imxpd;
315 	int edid_len;
316 	int ret;
317 	u32 bus_format = 0;
318 	const char *fmt;
319 
320 	imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
321 	if (!imxpd)
322 		return -ENOMEM;
323 
324 	/* port@1 is the output port */
325 	ret = drm_of_find_panel_or_bridge(np, 1, 0, &imxpd->panel,
326 					  &imxpd->next_bridge);
327 	if (ret && ret != -ENODEV)
328 		return ret;
329 
330 	edidp = of_get_property(np, "edid", &edid_len);
331 	if (edidp)
332 		imxpd->edid = devm_kmemdup(dev, edidp, edid_len, GFP_KERNEL);
333 
334 	ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
335 	if (!ret) {
336 		if (!strcmp(fmt, "rgb24"))
337 			bus_format = MEDIA_BUS_FMT_RGB888_1X24;
338 		else if (!strcmp(fmt, "rgb565"))
339 			bus_format = MEDIA_BUS_FMT_RGB565_1X16;
340 		else if (!strcmp(fmt, "bgr666"))
341 			bus_format = MEDIA_BUS_FMT_RGB666_1X18;
342 		else if (!strcmp(fmt, "lvds666"))
343 			bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
344 	}
345 	imxpd->bus_format = bus_format;
346 
347 	imxpd->dev = dev;
348 
349 	platform_set_drvdata(pdev, imxpd);
350 
351 	return component_add(dev, &imx_pd_ops);
352 }
353 
imx_pd_remove(struct platform_device * pdev)354 static int imx_pd_remove(struct platform_device *pdev)
355 {
356 	component_del(&pdev->dev, &imx_pd_ops);
357 
358 	return 0;
359 }
360 
361 static const struct of_device_id imx_pd_dt_ids[] = {
362 	{ .compatible = "fsl,imx-parallel-display", },
363 	{ /* sentinel */ }
364 };
365 MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
366 
367 static struct platform_driver imx_pd_driver = {
368 	.probe		= imx_pd_probe,
369 	.remove		= imx_pd_remove,
370 	.driver		= {
371 		.of_match_table = imx_pd_dt_ids,
372 		.name	= "imx-parallel-display",
373 	},
374 };
375 
376 module_platform_driver(imx_pd_driver);
377 
378 MODULE_DESCRIPTION("i.MX parallel display driver");
379 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
380 MODULE_LICENSE("GPL");
381 MODULE_ALIAS("platform:imx-parallel-display");
382