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