Commit | Line | Data |
---|---|---|
e692da4d SH |
1 | /* |
2 | * Freescale i.MX drm driver | |
3 | * | |
4 | * Copyright (C) 2011 Sascha Hauer, Pengutronix | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | */ | |
17b5001b | 16 | #include <linux/component.h> |
e692da4d | 17 | #include <linux/device.h> |
e7d6231e RK |
18 | #include <linux/fb.h> |
19 | #include <linux/module.h> | |
655b43cc | 20 | #include <linux/of_graph.h> |
e692da4d SH |
21 | #include <linux/platform_device.h> |
22 | #include <drm/drmP.h> | |
23 | #include <drm/drm_fb_helper.h> | |
24 | #include <drm/drm_crtc_helper.h> | |
e692da4d SH |
25 | #include <drm/drm_gem_cma_helper.h> |
26 | #include <drm/drm_fb_cma_helper.h> | |
27 | ||
28 | #include "imx-drm.h" | |
29 | ||
30 | #define MAX_CRTC 4 | |
31 | ||
887eceac RK |
32 | struct imx_drm_crtc; |
33 | ||
655b43cc PZ |
34 | struct imx_drm_component { |
35 | struct device_node *of_node; | |
36 | struct list_head list; | |
37 | }; | |
38 | ||
e692da4d SH |
39 | struct imx_drm_device { |
40 | struct drm_device *drm; | |
887eceac | 41 | struct imx_drm_crtc *crtc[MAX_CRTC]; |
e692da4d SH |
42 | int pipes; |
43 | struct drm_fbdev_cma *fbhelper; | |
44 | }; | |
45 | ||
46 | struct imx_drm_crtc { | |
47 | struct drm_crtc *crtc; | |
e692da4d SH |
48 | int pipe; |
49 | struct imx_drm_crtc_helper_funcs imx_drm_helper_funcs; | |
655b43cc | 50 | struct device_node *port; |
e692da4d SH |
51 | }; |
52 | ||
8acba02f RK |
53 | static int legacyfb_depth = 16; |
54 | module_param(legacyfb_depth, int, 0444); | |
55 | ||
b8d181e4 PZ |
56 | int imx_drm_crtc_id(struct imx_drm_crtc *crtc) |
57 | { | |
58 | return crtc->pipe; | |
59 | } | |
9c74360f | 60 | EXPORT_SYMBOL_GPL(imx_drm_crtc_id); |
b8d181e4 | 61 | |
e692da4d SH |
62 | static void imx_drm_driver_lastclose(struct drm_device *drm) |
63 | { | |
8acba02f | 64 | #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER) |
e692da4d SH |
65 | struct imx_drm_device *imxdrm = drm->dev_private; |
66 | ||
67 | if (imxdrm->fbhelper) | |
68 | drm_fbdev_cma_restore_mode(imxdrm->fbhelper); | |
8acba02f | 69 | #endif |
e692da4d SH |
70 | } |
71 | ||
72 | static int imx_drm_driver_unload(struct drm_device *drm) | |
73 | { | |
8acba02f | 74 | #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER) |
e692da4d | 75 | struct imx_drm_device *imxdrm = drm->dev_private; |
3e68439b RK |
76 | #endif |
77 | ||
78 | drm_kms_helper_poll_fini(drm); | |
e692da4d | 79 | |
3e68439b | 80 | #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER) |
8acba02f RK |
81 | if (imxdrm->fbhelper) |
82 | drm_fbdev_cma_fini(imxdrm->fbhelper); | |
83 | #endif | |
84 | ||
17b5001b RK |
85 | component_unbind_all(drm->dev, drm); |
86 | ||
020a9ea7 | 87 | drm_vblank_cleanup(drm); |
020a9ea7 | 88 | drm_mode_config_cleanup(drm); |
e692da4d SH |
89 | |
90 | return 0; | |
91 | } | |
92 | ||
fdffa6f2 | 93 | static struct imx_drm_crtc *imx_drm_find_crtc(struct drm_crtc *crtc) |
e692da4d | 94 | { |
32266b45 | 95 | struct imx_drm_device *imxdrm = crtc->dev->dev_private; |
887eceac RK |
96 | unsigned i; |
97 | ||
98 | for (i = 0; i < MAX_CRTC; i++) | |
99 | if (imxdrm->crtc[i] && imxdrm->crtc[i]->crtc == crtc) | |
100 | return imxdrm->crtc[i]; | |
e692da4d | 101 | |
e692da4d SH |
102 | return NULL; |
103 | } | |
104 | ||
f2d66aad | 105 | int imx_drm_panel_format_pins(struct drm_encoder *encoder, |
2ea42608 | 106 | u32 interface_pix_fmt, int hsync_pin, int vsync_pin) |
e692da4d | 107 | { |
e692da4d | 108 | struct imx_drm_crtc_helper_funcs *helper; |
887eceac | 109 | struct imx_drm_crtc *imx_crtc; |
e692da4d | 110 | |
f2d66aad | 111 | imx_crtc = imx_drm_find_crtc(encoder->crtc); |
887eceac RK |
112 | if (!imx_crtc) |
113 | return -EINVAL; | |
e692da4d | 114 | |
e692da4d SH |
115 | helper = &imx_crtc->imx_drm_helper_funcs; |
116 | if (helper->set_interface_pix_fmt) | |
f2d66aad RK |
117 | return helper->set_interface_pix_fmt(encoder->crtc, |
118 | encoder->encoder_type, interface_pix_fmt, | |
2ea42608 | 119 | hsync_pin, vsync_pin); |
e692da4d SH |
120 | return 0; |
121 | } | |
f2d66aad | 122 | EXPORT_SYMBOL_GPL(imx_drm_panel_format_pins); |
2ea42608 | 123 | |
f2d66aad | 124 | int imx_drm_panel_format(struct drm_encoder *encoder, u32 interface_pix_fmt) |
2ea42608 | 125 | { |
f2d66aad | 126 | return imx_drm_panel_format_pins(encoder, interface_pix_fmt, 2, 3); |
2ea42608 | 127 | } |
f2d66aad | 128 | EXPORT_SYMBOL_GPL(imx_drm_panel_format); |
e692da4d SH |
129 | |
130 | int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc) | |
131 | { | |
b5ea1492 | 132 | return drm_vblank_get(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe); |
e692da4d SH |
133 | } |
134 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get); | |
135 | ||
136 | void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc) | |
137 | { | |
b5ea1492 | 138 | drm_vblank_put(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe); |
e692da4d SH |
139 | } |
140 | EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put); | |
141 | ||
142 | void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc) | |
143 | { | |
b5ea1492 | 144 | drm_handle_vblank(imx_drm_crtc->crtc->dev, imx_drm_crtc->pipe); |
e692da4d SH |
145 | } |
146 | EXPORT_SYMBOL_GPL(imx_drm_handle_vblank); | |
147 | ||
148 | static int imx_drm_enable_vblank(struct drm_device *drm, int crtc) | |
149 | { | |
150 | struct imx_drm_device *imxdrm = drm->dev_private; | |
887eceac | 151 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[crtc]; |
e692da4d SH |
152 | int ret; |
153 | ||
e692da4d SH |
154 | if (!imx_drm_crtc) |
155 | return -EINVAL; | |
156 | ||
157 | if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank) | |
158 | return -ENOSYS; | |
159 | ||
160 | ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank( | |
161 | imx_drm_crtc->crtc); | |
162 | ||
163 | return ret; | |
164 | } | |
165 | ||
166 | static void imx_drm_disable_vblank(struct drm_device *drm, int crtc) | |
167 | { | |
168 | struct imx_drm_device *imxdrm = drm->dev_private; | |
887eceac | 169 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[crtc]; |
e692da4d | 170 | |
e692da4d SH |
171 | if (!imx_drm_crtc) |
172 | return; | |
173 | ||
174 | if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank) | |
175 | return; | |
176 | ||
177 | imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc); | |
178 | } | |
179 | ||
6ee4d7fe SH |
180 | static void imx_drm_driver_preclose(struct drm_device *drm, |
181 | struct drm_file *file) | |
182 | { | |
183 | int i; | |
184 | ||
185 | if (!file->is_master) | |
186 | return; | |
187 | ||
942325c8 RK |
188 | for (i = 0; i < MAX_CRTC; i++) |
189 | imx_drm_disable_vblank(drm, i); | |
6ee4d7fe SH |
190 | } |
191 | ||
e692da4d SH |
192 | static const struct file_operations imx_drm_driver_fops = { |
193 | .owner = THIS_MODULE, | |
194 | .open = drm_open, | |
195 | .release = drm_release, | |
196 | .unlocked_ioctl = drm_ioctl, | |
197 | .mmap = drm_gem_cma_mmap, | |
198 | .poll = drm_poll, | |
e692da4d SH |
199 | .read = drm_read, |
200 | .llseek = noop_llseek, | |
201 | }; | |
202 | ||
baa68c4b RK |
203 | int imx_drm_connector_mode_valid(struct drm_connector *connector, |
204 | struct drm_display_mode *mode) | |
205 | { | |
206 | return MODE_OK; | |
207 | } | |
208 | EXPORT_SYMBOL(imx_drm_connector_mode_valid); | |
209 | ||
8a51a33b RK |
210 | void imx_drm_connector_destroy(struct drm_connector *connector) |
211 | { | |
212 | drm_sysfs_connector_remove(connector); | |
213 | drm_connector_cleanup(connector); | |
214 | } | |
215 | EXPORT_SYMBOL_GPL(imx_drm_connector_destroy); | |
216 | ||
217 | void imx_drm_encoder_destroy(struct drm_encoder *encoder) | |
218 | { | |
219 | drm_encoder_cleanup(encoder); | |
220 | } | |
221 | EXPORT_SYMBOL_GPL(imx_drm_encoder_destroy); | |
222 | ||
3e68439b RK |
223 | static void imx_drm_output_poll_changed(struct drm_device *drm) |
224 | { | |
225 | #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER) | |
226 | struct imx_drm_device *imxdrm = drm->dev_private; | |
227 | ||
228 | drm_fbdev_cma_hotplug_event(imxdrm->fbhelper); | |
229 | #endif | |
230 | } | |
231 | ||
1df8b530 RK |
232 | static struct drm_mode_config_funcs imx_drm_mode_config_funcs = { |
233 | .fb_create = drm_fb_cma_create, | |
3e68439b | 234 | .output_poll_changed = imx_drm_output_poll_changed, |
1df8b530 RK |
235 | }; |
236 | ||
e692da4d | 237 | /* |
17b5001b RK |
238 | * Main DRM initialisation. This binds, initialises and registers |
239 | * with DRM the subcomponents of the driver. | |
e692da4d SH |
240 | */ |
241 | static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags) | |
242 | { | |
b85f2b5d | 243 | struct imx_drm_device *imxdrm; |
e355e7dd | 244 | struct drm_connector *connector; |
e692da4d SH |
245 | int ret; |
246 | ||
b85f2b5d RK |
247 | imxdrm = devm_kzalloc(drm->dev, sizeof(*imxdrm), GFP_KERNEL); |
248 | if (!imxdrm) | |
249 | return -ENOMEM; | |
250 | ||
e692da4d SH |
251 | imxdrm->drm = drm; |
252 | ||
253 | drm->dev_private = imxdrm; | |
254 | ||
255 | /* | |
256 | * enable drm irq mode. | |
4423843c | 257 | * - with irq_enabled = true, we can use the vblank feature. |
e692da4d SH |
258 | * |
259 | * P.S. note that we wouldn't use drm irq handler but | |
260 | * just specific driver own one instead because | |
261 | * drm framework supports only one irq handler and | |
262 | * drivers can well take care of their interrupts | |
263 | */ | |
4423843c | 264 | drm->irq_enabled = true; |
e692da4d | 265 | |
1df8b530 RK |
266 | /* |
267 | * set max width and height as default value(4096x4096). | |
268 | * this value would be used to check framebuffer size limitation | |
269 | * at drm_mode_addfb(). | |
270 | */ | |
271 | drm->mode_config.min_width = 64; | |
272 | drm->mode_config.min_height = 64; | |
273 | drm->mode_config.max_width = 4096; | |
274 | drm->mode_config.max_height = 4096; | |
275 | drm->mode_config.funcs = &imx_drm_mode_config_funcs; | |
276 | ||
e692da4d | 277 | drm_mode_config_init(drm); |
e692da4d | 278 | |
020a9ea7 | 279 | ret = drm_vblank_init(drm, MAX_CRTC); |
e692da4d | 280 | if (ret) |
8007875f | 281 | goto err_kms; |
e692da4d SH |
282 | |
283 | /* | |
e7d6231e RK |
284 | * with vblank_disable_allowed = true, vblank interrupt will be |
285 | * disabled by drm timer once a current process gives up ownership | |
286 | * of vblank event. (after drm_vblank_put function is called) | |
e692da4d | 287 | */ |
020a9ea7 | 288 | drm->vblank_disable_allowed = true; |
e692da4d | 289 | |
a72f8bee | 290 | platform_set_drvdata(drm->platformdev, drm); |
8d71de61 | 291 | |
17b5001b RK |
292 | /* Now try and bind all our sub-components */ |
293 | ret = component_bind_all(drm->dev, drm); | |
294 | if (ret) | |
ccec7f62 | 295 | goto err_vblank; |
e355e7dd RK |
296 | |
297 | /* | |
298 | * All components are now added, we can publish the connector sysfs | |
299 | * entries to userspace. This will generate hotplug events and so | |
300 | * userspace will expect to be able to access DRM at this point. | |
301 | */ | |
302 | list_for_each_entry(connector, &drm->mode_config.connector_list, head) { | |
303 | ret = drm_sysfs_connector_add(connector); | |
304 | if (ret) { | |
305 | dev_err(drm->dev, | |
306 | "[CONNECTOR:%d:%s] drm_sysfs_connector_add failed: %d\n", | |
307 | connector->base.id, | |
308 | drm_get_connector_name(connector), ret); | |
309 | goto err_unbind; | |
310 | } | |
311 | } | |
312 | ||
8acba02f RK |
313 | /* |
314 | * All components are now initialised, so setup the fb helper. | |
315 | * The fb helper takes copies of key hardware information, so the | |
316 | * crtcs/connectors/encoders must not change after this point. | |
317 | */ | |
318 | #if IS_ENABLED(CONFIG_DRM_IMX_FB_HELPER) | |
319 | if (legacyfb_depth != 16 && legacyfb_depth != 32) { | |
320 | dev_warn(drm->dev, "Invalid legacyfb_depth. Defaulting to 16bpp\n"); | |
321 | legacyfb_depth = 16; | |
322 | } | |
323 | imxdrm->fbhelper = drm_fbdev_cma_init(drm, legacyfb_depth, | |
324 | drm->mode_config.num_crtc, MAX_CRTC); | |
325 | if (IS_ERR(imxdrm->fbhelper)) { | |
326 | ret = PTR_ERR(imxdrm->fbhelper); | |
327 | imxdrm->fbhelper = NULL; | |
328 | goto err_unbind; | |
329 | } | |
330 | #endif | |
3e68439b RK |
331 | |
332 | drm_kms_helper_poll_init(drm); | |
333 | ||
8007875f | 334 | return 0; |
a72f8bee | 335 | |
e355e7dd RK |
336 | err_unbind: |
337 | component_unbind_all(drm->dev, drm); | |
ccec7f62 | 338 | err_vblank: |
8007875f RK |
339 | drm_vblank_cleanup(drm); |
340 | err_kms: | |
8007875f | 341 | drm_mode_config_cleanup(drm); |
e692da4d SH |
342 | |
343 | return ret; | |
344 | } | |
345 | ||
e692da4d SH |
346 | /* |
347 | * imx_drm_add_crtc - add a new crtc | |
e692da4d | 348 | */ |
32266b45 | 349 | int imx_drm_add_crtc(struct drm_device *drm, struct drm_crtc *crtc, |
e692da4d SH |
350 | struct imx_drm_crtc **new_crtc, |
351 | const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs, | |
655b43cc | 352 | struct device_node *port) |
e692da4d | 353 | { |
32266b45 | 354 | struct imx_drm_device *imxdrm = drm->dev_private; |
e692da4d | 355 | struct imx_drm_crtc *imx_drm_crtc; |
e692da4d SH |
356 | int ret; |
357 | ||
fd6040ed RK |
358 | /* |
359 | * The vblank arrays are dimensioned by MAX_CRTC - we can't | |
360 | * pass IDs greater than this to those functions. | |
361 | */ | |
e7d6231e RK |
362 | if (imxdrm->pipes >= MAX_CRTC) |
363 | return -EINVAL; | |
fd6040ed | 364 | |
e7d6231e RK |
365 | if (imxdrm->drm->open_count) |
366 | return -EBUSY; | |
e692da4d SH |
367 | |
368 | imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL); | |
e7d6231e RK |
369 | if (!imx_drm_crtc) |
370 | return -ENOMEM; | |
e692da4d SH |
371 | |
372 | imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs; | |
373 | imx_drm_crtc->pipe = imxdrm->pipes++; | |
655b43cc | 374 | imx_drm_crtc->port = port; |
e692da4d | 375 | imx_drm_crtc->crtc = crtc; |
e692da4d | 376 | |
887eceac | 377 | imxdrm->crtc[imx_drm_crtc->pipe] = imx_drm_crtc; |
e692da4d SH |
378 | |
379 | *new_crtc = imx_drm_crtc; | |
380 | ||
ec9557d7 | 381 | ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256); |
e692da4d SH |
382 | if (ret) |
383 | goto err_register; | |
384 | ||
ec9557d7 RK |
385 | drm_crtc_helper_add(crtc, |
386 | imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs); | |
387 | ||
32266b45 | 388 | drm_crtc_init(drm, crtc, |
ec9557d7 RK |
389 | imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs); |
390 | ||
e692da4d SH |
391 | return 0; |
392 | ||
393 | err_register: | |
887eceac | 394 | imxdrm->crtc[imx_drm_crtc->pipe] = NULL; |
e692da4d | 395 | kfree(imx_drm_crtc); |
e692da4d SH |
396 | return ret; |
397 | } | |
398 | EXPORT_SYMBOL_GPL(imx_drm_add_crtc); | |
399 | ||
400 | /* | |
401 | * imx_drm_remove_crtc - remove a crtc | |
402 | */ | |
403 | int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc) | |
404 | { | |
ccec7f62 | 405 | struct imx_drm_device *imxdrm = imx_drm_crtc->crtc->dev->dev_private; |
e692da4d SH |
406 | |
407 | drm_crtc_cleanup(imx_drm_crtc->crtc); | |
408 | ||
887eceac | 409 | imxdrm->crtc[imx_drm_crtc->pipe] = NULL; |
e692da4d | 410 | |
e692da4d SH |
411 | kfree(imx_drm_crtc); |
412 | ||
413 | return 0; | |
414 | } | |
415 | EXPORT_SYMBOL_GPL(imx_drm_remove_crtc); | |
416 | ||
9e2d410d | 417 | /* |
655b43cc | 418 | * Find the DRM CRTC possible mask for the connected endpoint. |
9e2d410d RK |
419 | * |
420 | * The encoder possible masks are defined by their position in the | |
421 | * mode_config crtc_list. This means that CRTCs must not be added | |
422 | * or removed once the DRM device has been fully initialised. | |
423 | */ | |
424 | static uint32_t imx_drm_find_crtc_mask(struct imx_drm_device *imxdrm, | |
655b43cc | 425 | struct device_node *endpoint) |
9e2d410d | 426 | { |
655b43cc | 427 | struct device_node *port; |
9e2d410d RK |
428 | unsigned i; |
429 | ||
655b43cc PZ |
430 | port = of_graph_get_remote_port(endpoint); |
431 | if (!port) | |
432 | return 0; | |
433 | of_node_put(port); | |
434 | ||
9e2d410d RK |
435 | for (i = 0; i < MAX_CRTC; i++) { |
436 | struct imx_drm_crtc *imx_drm_crtc = imxdrm->crtc[i]; | |
655b43cc | 437 | if (imx_drm_crtc && imx_drm_crtc->port == port) |
9e2d410d RK |
438 | return drm_crtc_mask(imx_drm_crtc->crtc); |
439 | } | |
440 | ||
441 | return 0; | |
442 | } | |
443 | ||
655b43cc PZ |
444 | static struct device_node *imx_drm_of_get_next_endpoint( |
445 | const struct device_node *parent, struct device_node *prev) | |
446 | { | |
447 | struct device_node *node = of_graph_get_next_endpoint(parent, prev); | |
448 | of_node_put(prev); | |
449 | return node; | |
450 | } | |
451 | ||
9e2d410d RK |
452 | int imx_drm_encoder_parse_of(struct drm_device *drm, |
453 | struct drm_encoder *encoder, struct device_node *np) | |
454 | { | |
455 | struct imx_drm_device *imxdrm = drm->dev_private; | |
655b43cc | 456 | struct device_node *ep = NULL; |
9e2d410d | 457 | uint32_t crtc_mask = 0; |
655b43cc | 458 | int i; |
9e2d410d | 459 | |
655b43cc PZ |
460 | for (i = 0; ; i++) { |
461 | u32 mask; | |
9e2d410d | 462 | |
655b43cc PZ |
463 | ep = imx_drm_of_get_next_endpoint(np, ep); |
464 | if (!ep) | |
9e2d410d | 465 | break; |
9e2d410d | 466 | |
655b43cc | 467 | mask = imx_drm_find_crtc_mask(imxdrm, ep); |
9e2d410d RK |
468 | |
469 | /* | |
470 | * If we failed to find the CRTC(s) which this encoder is | |
471 | * supposed to be connected to, it's because the CRTC has | |
472 | * not been registered yet. Defer probing, and hope that | |
473 | * the required CRTC is added later. | |
474 | */ | |
475 | if (mask == 0) | |
476 | return -EPROBE_DEFER; | |
477 | ||
478 | crtc_mask |= mask; | |
479 | } | |
480 | ||
655b43cc PZ |
481 | if (ep) |
482 | of_node_put(ep); | |
483 | if (i == 0) | |
484 | return -ENOENT; | |
485 | ||
9e2d410d RK |
486 | encoder->possible_crtcs = crtc_mask; |
487 | ||
488 | /* FIXME: this is the mask of outputs which can clone this output. */ | |
489 | encoder->possible_clones = ~0; | |
490 | ||
491 | return 0; | |
492 | } | |
493 | EXPORT_SYMBOL_GPL(imx_drm_encoder_parse_of); | |
494 | ||
655b43cc PZ |
495 | /* |
496 | * @node: device tree node containing encoder input ports | |
497 | * @encoder: drm_encoder | |
498 | */ | |
499 | int imx_drm_encoder_get_mux_id(struct device_node *node, | |
500 | struct drm_encoder *encoder) | |
e692da4d | 501 | { |
887eceac | 502 | struct imx_drm_crtc *imx_crtc = imx_drm_find_crtc(encoder->crtc); |
655b43cc | 503 | struct device_node *ep = NULL; |
b6babdd9 | 504 | struct of_endpoint endpoint; |
655b43cc | 505 | struct device_node *port; |
b6babdd9 | 506 | int ret; |
e692da4d | 507 | |
655b43cc PZ |
508 | if (!node || !imx_crtc) |
509 | return -EINVAL; | |
510 | ||
511 | do { | |
512 | ep = imx_drm_of_get_next_endpoint(node, ep); | |
513 | if (!ep) | |
514 | break; | |
515 | ||
516 | port = of_graph_get_remote_port(ep); | |
517 | of_node_put(port); | |
518 | if (port == imx_crtc->port) { | |
b6babdd9 | 519 | ret = of_graph_parse_endpoint(ep, &endpoint); |
c509bdc2 | 520 | return ret ? ret : endpoint.port; |
655b43cc PZ |
521 | } |
522 | } while (ep); | |
e692da4d | 523 | |
655b43cc | 524 | return -EINVAL; |
e692da4d | 525 | } |
ea8d1583 | 526 | EXPORT_SYMBOL_GPL(imx_drm_encoder_get_mux_id); |
e692da4d | 527 | |
baa70943 | 528 | static const struct drm_ioctl_desc imx_drm_ioctls[] = { |
e692da4d SH |
529 | /* none so far */ |
530 | }; | |
531 | ||
532 | static struct drm_driver imx_drm_driver = { | |
bd3665c9 | 533 | .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME, |
e692da4d SH |
534 | .load = imx_drm_driver_load, |
535 | .unload = imx_drm_driver_unload, | |
e692da4d | 536 | .lastclose = imx_drm_driver_lastclose, |
6ee4d7fe | 537 | .preclose = imx_drm_driver_preclose, |
e692da4d SH |
538 | .gem_free_object = drm_gem_cma_free_object, |
539 | .gem_vm_ops = &drm_gem_cma_vm_ops, | |
540 | .dumb_create = drm_gem_cma_dumb_create, | |
541 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, | |
43387b37 | 542 | .dumb_destroy = drm_gem_dumb_destroy, |
e692da4d | 543 | |
bd3665c9 PZ |
544 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
545 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, | |
546 | .gem_prime_import = drm_gem_prime_import, | |
547 | .gem_prime_export = drm_gem_prime_export, | |
548 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, | |
549 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, | |
550 | .gem_prime_vmap = drm_gem_cma_prime_vmap, | |
551 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, | |
552 | .gem_prime_mmap = drm_gem_cma_prime_mmap, | |
e692da4d SH |
553 | .get_vblank_counter = drm_vblank_count, |
554 | .enable_vblank = imx_drm_enable_vblank, | |
555 | .disable_vblank = imx_drm_disable_vblank, | |
556 | .ioctls = imx_drm_ioctls, | |
557 | .num_ioctls = ARRAY_SIZE(imx_drm_ioctls), | |
558 | .fops = &imx_drm_driver_fops, | |
559 | .name = "imx-drm", | |
560 | .desc = "i.MX DRM graphics", | |
561 | .date = "20120507", | |
562 | .major = 1, | |
563 | .minor = 0, | |
564 | .patchlevel = 0, | |
565 | }; | |
566 | ||
17b5001b RK |
567 | static int compare_of(struct device *dev, void *data) |
568 | { | |
655b43cc | 569 | struct device_node *np = data; |
17b5001b | 570 | |
655b43cc PZ |
571 | /* Special case for LDB, one device for two channels */ |
572 | if (of_node_cmp(np->name, "lvds-channel") == 0) { | |
573 | np = of_get_parent(np); | |
574 | of_node_put(np); | |
17b5001b RK |
575 | } |
576 | ||
655b43cc PZ |
577 | return dev->of_node == np; |
578 | } | |
17b5001b | 579 | |
655b43cc | 580 | static LIST_HEAD(imx_drm_components); |
17b5001b | 581 | |
655b43cc PZ |
582 | static int imx_drm_add_components(struct device *master, struct master *m) |
583 | { | |
584 | struct imx_drm_component *component; | |
585 | int ret; | |
17b5001b | 586 | |
655b43cc PZ |
587 | list_for_each_entry(component, &imx_drm_components, list) { |
588 | ret = component_master_add_child(m, compare_of, | |
589 | component->of_node); | |
17b5001b RK |
590 | if (ret) |
591 | return ret; | |
592 | } | |
593 | return 0; | |
594 | } | |
595 | ||
596 | static int imx_drm_bind(struct device *dev) | |
597 | { | |
598 | return drm_platform_init(&imx_drm_driver, to_platform_device(dev)); | |
599 | } | |
600 | ||
601 | static void imx_drm_unbind(struct device *dev) | |
602 | { | |
603 | drm_put_dev(dev_get_drvdata(dev)); | |
604 | } | |
605 | ||
606 | static const struct component_master_ops imx_drm_ops = { | |
607 | .add_components = imx_drm_add_components, | |
608 | .bind = imx_drm_bind, | |
609 | .unbind = imx_drm_unbind, | |
610 | }; | |
611 | ||
655b43cc PZ |
612 | static struct imx_drm_component *imx_drm_find_component(struct device *dev, |
613 | struct device_node *node) | |
614 | { | |
615 | struct imx_drm_component *component; | |
616 | ||
617 | list_for_each_entry(component, &imx_drm_components, list) | |
618 | if (component->of_node == node) | |
619 | return component; | |
620 | ||
621 | return NULL; | |
622 | } | |
623 | ||
624 | static int imx_drm_add_component(struct device *dev, struct device_node *node) | |
625 | { | |
626 | struct imx_drm_component *component; | |
627 | ||
628 | if (imx_drm_find_component(dev, node)) | |
629 | return 0; | |
630 | ||
631 | component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); | |
632 | if (!component) | |
633 | return -ENOMEM; | |
634 | ||
635 | component->of_node = node; | |
636 | list_add_tail(&component->list, &imx_drm_components); | |
637 | ||
638 | return 0; | |
639 | } | |
640 | ||
e692da4d SH |
641 | static int imx_drm_platform_probe(struct platform_device *pdev) |
642 | { | |
655b43cc | 643 | struct device_node *ep, *port, *remote; |
4cdbb4ff | 644 | int ret; |
655b43cc PZ |
645 | int i; |
646 | ||
647 | /* | |
648 | * Bind the IPU display interface ports first, so that | |
649 | * imx_drm_encoder_parse_of called from encoder .bind callbacks | |
650 | * works as expected. | |
651 | */ | |
652 | for (i = 0; ; i++) { | |
653 | port = of_parse_phandle(pdev->dev.of_node, "ports", i); | |
654 | if (!port) | |
655 | break; | |
656 | ||
657 | ret = imx_drm_add_component(&pdev->dev, port); | |
658 | if (ret < 0) | |
659 | return ret; | |
660 | } | |
661 | ||
662 | if (i == 0) { | |
663 | dev_err(&pdev->dev, "missing 'ports' property\n"); | |
664 | return -ENODEV; | |
665 | } | |
666 | ||
667 | /* Then bind all encoders */ | |
668 | for (i = 0; ; i++) { | |
669 | port = of_parse_phandle(pdev->dev.of_node, "ports", i); | |
670 | if (!port) | |
671 | break; | |
672 | ||
673 | for_each_child_of_node(port, ep) { | |
674 | remote = of_graph_get_remote_port_parent(ep); | |
675 | if (!remote || !of_device_is_available(remote)) { | |
676 | of_node_put(remote); | |
677 | continue; | |
678 | } | |
679 | ||
680 | ret = imx_drm_add_component(&pdev->dev, remote); | |
681 | of_node_put(remote); | |
682 | if (ret < 0) | |
683 | return ret; | |
684 | } | |
685 | of_node_put(port); | |
686 | } | |
4cdbb4ff RK |
687 | |
688 | ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)); | |
689 | if (ret) | |
690 | return ret; | |
691 | ||
17b5001b | 692 | return component_master_add(&pdev->dev, &imx_drm_ops); |
e692da4d SH |
693 | } |
694 | ||
695 | static int imx_drm_platform_remove(struct platform_device *pdev) | |
696 | { | |
17b5001b | 697 | component_master_del(&pdev->dev, &imx_drm_ops); |
e692da4d SH |
698 | return 0; |
699 | } | |
700 | ||
17b5001b | 701 | static const struct of_device_id imx_drm_dt_ids[] = { |
655b43cc | 702 | { .compatible = "fsl,imx-display-subsystem", }, |
17b5001b RK |
703 | { /* sentinel */ }, |
704 | }; | |
705 | MODULE_DEVICE_TABLE(of, imx_drm_dt_ids); | |
706 | ||
e692da4d SH |
707 | static struct platform_driver imx_drm_pdrv = { |
708 | .probe = imx_drm_platform_probe, | |
99c28f10 | 709 | .remove = imx_drm_platform_remove, |
e692da4d SH |
710 | .driver = { |
711 | .owner = THIS_MODULE, | |
712 | .name = "imx-drm", | |
17b5001b | 713 | .of_match_table = imx_drm_dt_ids, |
e692da4d SH |
714 | }, |
715 | }; | |
b85f2b5d | 716 | module_platform_driver(imx_drm_pdrv); |
e692da4d SH |
717 | |
718 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); | |
719 | MODULE_DESCRIPTION("i.MX drm driver core"); | |
720 | MODULE_LICENSE("GPL"); |