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