drm/rockchip: add a common subdrv interfaces
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * based on exynos_drm_drv.c
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/dma-iommu.h>
18
19 #include <drm/drmP.h>
20 #include <drm/drm_crtc_helper.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_sync_helper.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/component.h>
28 #include <linux/fence.h>
29
30 #include <drm/rockchip_drm.h>
31
32 #include "rockchip_drm_drv.h"
33 #include "rockchip_drm_fb.h"
34 #include "rockchip_drm_fbdev.h"
35 #include "rockchip_drm_gem.h"
36
37 #define DRIVER_NAME     "rockchip"
38 #define DRIVER_DESC     "RockChip Soc DRM"
39 #define DRIVER_DATE     "20140818"
40 #define DRIVER_MAJOR    1
41 #define DRIVER_MINOR    0
42
43 static LIST_HEAD(rockchip_drm_subdrv_list);
44 static DEFINE_MUTEX(subdrv_list_mutex);
45
46 /*
47  * Attach a (component) device to the shared drm dma mapping from master drm
48  * device.  This is used by the VOPs to map GEM buffers to a common DMA
49  * mapping.
50  */
51 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
52                                    struct device *dev)
53 {
54         struct rockchip_drm_private *private = drm_dev->dev_private;
55         struct iommu_domain *domain = private->domain;
56         int ret;
57
58         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
59         if (ret)
60                 return ret;
61
62         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
63         ret = iommu_attach_device(domain, dev);
64         if (ret) {
65                 dev_err(dev, "Failed to attach iommu device\n");
66                 return ret;
67         }
68
69         if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) {
70                 dev_err(dev, "Failed to set dma_ops\n");
71                 iommu_detach_device(domain, dev);
72                 ret = -ENODEV;
73         }
74
75         return ret;
76 }
77
78 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
79                                     struct device *dev)
80 {
81         struct rockchip_drm_private *private = drm_dev->dev_private;
82         struct iommu_domain *domain = private->domain;
83
84         iommu_detach_device(domain, dev);
85 }
86
87 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
88                                  const struct rockchip_crtc_funcs *crtc_funcs)
89 {
90         int pipe = drm_crtc_index(crtc);
91         struct rockchip_drm_private *priv = crtc->dev->dev_private;
92
93         if (pipe > ROCKCHIP_MAX_CRTC)
94                 return -EINVAL;
95
96         priv->crtc_funcs[pipe] = crtc_funcs;
97
98         return 0;
99 }
100
101 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
102 {
103         int pipe = drm_crtc_index(crtc);
104         struct rockchip_drm_private *priv = crtc->dev->dev_private;
105
106         if (pipe > ROCKCHIP_MAX_CRTC)
107                 return;
108
109         priv->crtc_funcs[pipe] = NULL;
110 }
111
112 static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
113                                                 int pipe)
114 {
115         struct drm_crtc *crtc;
116         int i = 0;
117
118         list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
119                 if (i++ == pipe)
120                         return crtc;
121
122         return NULL;
123 }
124
125 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
126                                            unsigned int pipe)
127 {
128         struct rockchip_drm_private *priv = dev->dev_private;
129         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
130
131         if (crtc && priv->crtc_funcs[pipe] &&
132             priv->crtc_funcs[pipe]->enable_vblank)
133                 return priv->crtc_funcs[pipe]->enable_vblank(crtc);
134
135         return 0;
136 }
137
138 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
139                                              unsigned int pipe)
140 {
141         struct rockchip_drm_private *priv = dev->dev_private;
142         struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
143
144         if (crtc && priv->crtc_funcs[pipe] &&
145             priv->crtc_funcs[pipe]->enable_vblank)
146                 priv->crtc_funcs[pipe]->disable_vblank(crtc);
147 }
148
149 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
150 {
151         struct rockchip_drm_private *private;
152         struct device *dev = drm_dev->dev;
153         struct drm_connector *connector;
154         struct iommu_group *group;
155         int ret;
156
157         private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
158         if (!private)
159                 return -ENOMEM;
160
161         mutex_init(&private->commit.lock);
162         INIT_WORK(&private->commit.work, rockchip_drm_atomic_work);
163
164         drm_dev->dev_private = private;
165
166 #ifdef CONFIG_DRM_DMA_SYNC
167         private->cpu_fence_context = fence_context_alloc(1);
168         atomic_set(&private->cpu_fence_seqno, 0);
169 #endif
170
171         drm_mode_config_init(drm_dev);
172
173         rockchip_drm_mode_config_init(drm_dev);
174
175         dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
176                                       GFP_KERNEL);
177         if (!dev->dma_parms) {
178                 ret = -ENOMEM;
179                 goto err_config_cleanup;
180         }
181
182         private->domain = iommu_domain_alloc(&platform_bus_type);
183         if (!private->domain)
184                 return -ENOMEM;
185
186         ret = iommu_get_dma_cookie(private->domain);
187         if (ret)
188                 goto err_free_domain;
189
190         group = iommu_group_get(dev);
191         if (!group) {
192                 group = iommu_group_alloc();
193                 if (IS_ERR(group)) {
194                         dev_err(dev, "Failed to allocate IOMMU group\n");
195                         goto err_put_cookie;
196                 }
197
198                 ret = iommu_group_add_device(group, dev);
199                 iommu_group_put(group);
200                 if (ret) {
201                         dev_err(dev, "failed to add device to IOMMU group\n");
202                         goto err_put_cookie;
203                 }
204         }
205         /*
206          * Attach virtual iommu device, sub iommu device can share the same
207          * mapping with it.
208          */
209         ret = rockchip_drm_dma_attach_device(drm_dev, dev);
210         if (ret)
211                 goto err_group_remove_device;
212
213         /* Try to bind all sub drivers. */
214         ret = component_bind_all(dev, drm_dev);
215         if (ret)
216                 goto err_detach_device;
217
218         /*
219          * All components are now added, we can publish the connector sysfs
220          * entries to userspace.  This will generate hotplug events and so
221          * userspace will expect to be able to access DRM at this point.
222          */
223         list_for_each_entry(connector, &drm_dev->mode_config.connector_list,
224                         head) {
225                 ret = drm_connector_register(connector);
226                 if (ret) {
227                         dev_err(drm_dev->dev,
228                                 "[CONNECTOR:%d:%s] drm_connector_register failed: %d\n",
229                                 connector->base.id,
230                                 connector->name, ret);
231                         goto err_unbind;
232                 }
233         }
234
235         /* init kms poll for handling hpd */
236         drm_kms_helper_poll_init(drm_dev);
237
238         /*
239          * enable drm irq mode.
240          * - with irq_enabled = true, we can use the vblank feature.
241          */
242         drm_dev->irq_enabled = true;
243
244         ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
245         if (ret)
246                 goto err_kms_helper_poll_fini;
247
248         /*
249          * with vblank_disable_allowed = true, vblank interrupt will be disabled
250          * by drm timer once a current process gives up ownership of
251          * vblank event.(after drm_vblank_put function is called)
252          */
253         drm_dev->vblank_disable_allowed = true;
254
255         drm_mode_config_reset(drm_dev);
256
257         ret = rockchip_drm_fbdev_init(drm_dev);
258         if (ret)
259                 goto err_vblank_cleanup;
260
261         return 0;
262 err_vblank_cleanup:
263         drm_vblank_cleanup(drm_dev);
264 err_kms_helper_poll_fini:
265         drm_kms_helper_poll_fini(drm_dev);
266 err_unbind:
267         component_unbind_all(dev, drm_dev);
268 err_detach_device:
269         rockchip_drm_dma_detach_device(drm_dev, dev);
270 err_group_remove_device:
271         iommu_group_remove_device(dev);
272 err_put_cookie:
273         iommu_put_dma_cookie(private->domain);
274 err_free_domain:
275         iommu_domain_free(private->domain);
276 err_config_cleanup:
277         drm_mode_config_cleanup(drm_dev);
278         drm_dev->dev_private = NULL;
279         return ret;
280 }
281
282 static int rockchip_drm_unload(struct drm_device *drm_dev)
283 {
284         struct device *dev = drm_dev->dev;
285         struct rockchip_drm_private *private = drm_dev->dev_private;
286
287         rockchip_drm_fbdev_fini(drm_dev);
288         drm_vblank_cleanup(drm_dev);
289         drm_kms_helper_poll_fini(drm_dev);
290         component_unbind_all(dev, drm_dev);
291         rockchip_drm_dma_detach_device(drm_dev, dev);
292         iommu_group_remove_device(dev);
293         iommu_put_dma_cookie(private->domain);
294         iommu_domain_free(private->domain);
295         drm_mode_config_cleanup(drm_dev);
296         drm_dev->dev_private = NULL;
297
298         return 0;
299 }
300
301 static void rockchip_drm_crtc_cancel_pending_vblank(struct drm_crtc *crtc,
302                                                     struct drm_file *file_priv)
303 {
304         struct rockchip_drm_private *priv = crtc->dev->dev_private;
305         int pipe = drm_crtc_index(crtc);
306
307         if (pipe < ROCKCHIP_MAX_CRTC &&
308             priv->crtc_funcs[pipe] &&
309             priv->crtc_funcs[pipe]->cancel_pending_vblank)
310                 priv->crtc_funcs[pipe]->cancel_pending_vblank(crtc, file_priv);
311 }
312
313 int rockchip_drm_register_subdrv(struct drm_rockchip_subdrv *subdrv)
314 {
315         if (!subdrv)
316                 return -EINVAL;
317
318         mutex_lock(&subdrv_list_mutex);
319         list_add_tail(&subdrv->list, &rockchip_drm_subdrv_list);
320         mutex_unlock(&subdrv_list_mutex);
321
322         return 0;
323 }
324 EXPORT_SYMBOL_GPL(rockchip_drm_register_subdrv);
325
326 int rockchip_drm_unregister_subdrv(struct drm_rockchip_subdrv *subdrv)
327 {
328         if (!subdrv)
329                 return -EINVAL;
330
331         mutex_lock(&subdrv_list_mutex);
332         list_del(&subdrv->list);
333         mutex_unlock(&subdrv_list_mutex);
334
335         return 0;
336 }
337 EXPORT_SYMBOL_GPL(rockchip_drm_unregister_subdrv);
338
339 static int rockchip_drm_open(struct drm_device *dev, struct drm_file *file)
340 {
341         struct rockchip_drm_file_private *file_priv;
342         struct drm_rockchip_subdrv *subdrv;
343         int ret = 0;
344
345         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
346         if (!file_priv)
347                 return -ENOMEM;
348         INIT_LIST_HEAD(&file_priv->gem_cpu_acquire_list);
349
350         file->driver_priv = file_priv;
351
352         mutex_lock(&subdrv_list_mutex);
353         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list) {
354                 ret = subdrv->open(dev, subdrv->dev, file);
355                 if (ret) {
356                         mutex_unlock(&subdrv_list_mutex);
357                         goto err_free_file_priv;
358                 }
359         }
360         mutex_unlock(&subdrv_list_mutex);
361
362         return 0;
363
364 err_free_file_priv:
365         kfree(file_priv);
366         file_priv = NULL;
367
368         return ret;
369 }
370
371 static void rockchip_drm_preclose(struct drm_device *dev,
372                                   struct drm_file *file_priv)
373 {
374         struct rockchip_drm_file_private *file_private = file_priv->driver_priv;
375         struct rockchip_gem_object_node *cur, *d;
376         struct drm_rockchip_subdrv *subdrv;
377         struct drm_crtc *crtc;
378
379         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
380                 rockchip_drm_crtc_cancel_pending_vblank(crtc, file_priv);
381
382         mutex_lock(&dev->struct_mutex);
383         list_for_each_entry_safe(cur, d,
384                         &file_private->gem_cpu_acquire_list, list) {
385 #ifdef CONFIG_DRM_DMA_SYNC
386                 BUG_ON(!cur->rockchip_gem_obj->acquire_fence);
387                 drm_fence_signal_and_put(&cur->rockchip_gem_obj->acquire_fence);
388 #endif
389                 drm_gem_object_unreference(&cur->rockchip_gem_obj->base);
390                 kfree(cur);
391         }
392         /* since we are deleting the whole list, just initialize the header
393          * instead of calling list_del for every element
394          */
395         INIT_LIST_HEAD(&file_private->gem_cpu_acquire_list);
396         mutex_unlock(&dev->struct_mutex);
397
398         mutex_lock(&subdrv_list_mutex);
399         list_for_each_entry(subdrv, &rockchip_drm_subdrv_list, list)
400                 subdrv->close(dev, subdrv->dev, file_priv);
401         mutex_unlock(&subdrv_list_mutex);
402 }
403
404 static void rockchip_drm_postclose(struct drm_device *dev, struct drm_file *file)
405 {
406         kfree(file->driver_priv);
407         file->driver_priv = NULL;
408 }
409
410 void rockchip_drm_lastclose(struct drm_device *dev)
411 {
412         struct rockchip_drm_private *priv = dev->dev_private;
413
414         drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
415 }
416
417 static const struct drm_ioctl_desc rockchip_ioctls[] = {
418         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CREATE, rockchip_gem_create_ioctl,
419                           DRM_UNLOCKED | DRM_AUTH),
420         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
421                           rockchip_gem_map_offset_ioctl,
422                           DRM_UNLOCKED | DRM_AUTH),
423         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
424                           rockchip_gem_cpu_acquire_ioctl,
425                           DRM_UNLOCKED | DRM_AUTH),
426         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
427                           rockchip_gem_cpu_release_ioctl,
428                           DRM_UNLOCKED | DRM_AUTH),
429 };
430
431 static const struct file_operations rockchip_drm_driver_fops = {
432         .owner = THIS_MODULE,
433         .open = drm_open,
434         .mmap = rockchip_gem_mmap,
435         .poll = drm_poll,
436         .read = drm_read,
437         .unlocked_ioctl = drm_ioctl,
438 #ifdef CONFIG_COMPAT
439         .compat_ioctl = drm_compat_ioctl,
440 #endif
441         .release = drm_release,
442 };
443
444 const struct vm_operations_struct rockchip_drm_vm_ops = {
445         .open = drm_gem_vm_open,
446         .close = drm_gem_vm_close,
447 };
448
449 static struct drm_driver rockchip_drm_driver = {
450         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
451                                   DRIVER_PRIME | DRIVER_ATOMIC,
452         .load                   = rockchip_drm_load,
453         .unload                 = rockchip_drm_unload,
454         .preclose               = rockchip_drm_preclose,
455         .lastclose              = rockchip_drm_lastclose,
456         .get_vblank_counter     = drm_vblank_no_hw_counter,
457         .open                   = rockchip_drm_open,
458         .postclose              = rockchip_drm_postclose,
459         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
460         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
461         .gem_vm_ops             = &rockchip_drm_vm_ops,
462         .gem_free_object        = rockchip_gem_free_object,
463         .dumb_create            = rockchip_gem_dumb_create,
464         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
465         .dumb_destroy           = drm_gem_dumb_destroy,
466         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
467         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
468         .gem_prime_import       = drm_gem_prime_import,
469         .gem_prime_export       = drm_gem_prime_export,
470         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
471         .gem_prime_vmap         = rockchip_gem_prime_vmap,
472         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
473         .gem_prime_mmap         = rockchip_gem_mmap_buf,
474         .ioctls                 = rockchip_ioctls,
475         .num_ioctls             = ARRAY_SIZE(rockchip_ioctls),
476         .fops                   = &rockchip_drm_driver_fops,
477         .name   = DRIVER_NAME,
478         .desc   = DRIVER_DESC,
479         .date   = DRIVER_DATE,
480         .major  = DRIVER_MAJOR,
481         .minor  = DRIVER_MINOR,
482 };
483
484 #ifdef CONFIG_PM_SLEEP
485 static int rockchip_drm_sys_suspend(struct device *dev)
486 {
487         struct drm_device *drm = dev_get_drvdata(dev);
488         struct drm_connector *connector;
489
490         if (!drm)
491                 return 0;
492
493         drm_modeset_lock_all(drm);
494         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
495                 int old_dpms = connector->dpms;
496
497                 if (connector->funcs->dpms)
498                         connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
499
500                 /* Set the old mode back to the connector for resume */
501                 connector->dpms = old_dpms;
502         }
503         drm_modeset_unlock_all(drm);
504
505         return 0;
506 }
507
508 static int rockchip_drm_sys_resume(struct device *dev)
509 {
510         struct drm_device *drm = dev_get_drvdata(dev);
511         struct drm_connector *connector;
512         enum drm_connector_status status;
513         bool changed = false;
514
515         if (!drm)
516                 return 0;
517
518         drm_modeset_lock_all(drm);
519         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
520                 int desired_mode = connector->dpms;
521
522                 /*
523                  * at suspend time, we save dpms to connector->dpms,
524                  * restore the old_dpms, and at current time, the connector
525                  * dpms status must be DRM_MODE_DPMS_OFF.
526                  */
527                 connector->dpms = DRM_MODE_DPMS_OFF;
528
529                 /*
530                  * If the connector has been disconnected during suspend,
531                  * disconnect it from the encoder and leave it off. We'll notify
532                  * userspace at the end.
533                  */
534                 if (desired_mode == DRM_MODE_DPMS_ON) {
535                         status = connector->funcs->detect(connector, true);
536                         if (status == connector_status_disconnected) {
537                                 connector->encoder = NULL;
538                                 connector->status = status;
539                                 changed = true;
540                                 continue;
541                         }
542                 }
543                 if (connector->funcs->dpms)
544                         connector->funcs->dpms(connector, desired_mode);
545         }
546         drm_modeset_unlock_all(drm);
547
548         drm_helper_resume_force_mode(drm);
549
550         if (changed)
551                 drm_kms_helper_hotplug_event(drm);
552
553         return 0;
554 }
555 #endif
556
557 static const struct dev_pm_ops rockchip_drm_pm_ops = {
558         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
559                                 rockchip_drm_sys_resume)
560 };
561
562 /*
563  * @node: device tree node containing encoder input ports
564  * @encoder: drm_encoder
565  */
566 int rockchip_drm_encoder_get_mux_id(struct device_node *node,
567                                     struct drm_encoder *encoder)
568 {
569         struct device_node *ep;
570         struct drm_crtc *crtc = encoder->crtc;
571         struct of_endpoint endpoint;
572         struct device_node *port;
573         int ret;
574
575         if (!node || !crtc)
576                 return -EINVAL;
577
578         for_each_endpoint_of_node(node, ep) {
579                 port = of_graph_get_remote_port(ep);
580                 of_node_put(port);
581                 if (port == crtc->port) {
582                         ret = of_graph_parse_endpoint(ep, &endpoint);
583                         of_node_put(ep);
584                         return ret ?: endpoint.id;
585                 }
586         }
587
588         return -EINVAL;
589 }
590 EXPORT_SYMBOL_GPL(rockchip_drm_encoder_get_mux_id);
591
592 static int compare_of(struct device *dev, void *data)
593 {
594         struct device_node *np = data;
595
596         return dev->of_node == np;
597 }
598
599 static void rockchip_add_endpoints(struct device *dev,
600                                    struct component_match **match,
601                                    struct device_node *port)
602 {
603         struct device_node *ep, *remote;
604
605         for_each_child_of_node(port, ep) {
606                 remote = of_graph_get_remote_port_parent(ep);
607                 if (!remote || !of_device_is_available(remote)) {
608                         of_node_put(remote);
609                         continue;
610                 } else if (!of_device_is_available(remote->parent)) {
611                         dev_warn(dev, "parent device of %s is not available\n",
612                                  remote->full_name);
613                         of_node_put(remote);
614                         continue;
615                 }
616
617                 component_match_add(dev, match, compare_of, remote);
618                 of_node_put(remote);
619         }
620 }
621
622 static int rockchip_drm_bind(struct device *dev)
623 {
624         struct drm_device *drm;
625         int ret;
626
627         drm = drm_dev_alloc(&rockchip_drm_driver, dev);
628         if (!drm)
629                 return -ENOMEM;
630
631         ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
632         if (ret)
633                 goto err_free;
634
635         ret = drm_dev_register(drm, 0);
636         if (ret)
637                 goto err_free;
638
639         dev_set_drvdata(dev, drm);
640
641         return 0;
642
643 err_free:
644         drm_dev_unref(drm);
645         return ret;
646 }
647
648 static void rockchip_drm_unbind(struct device *dev)
649 {
650         struct drm_device *drm = dev_get_drvdata(dev);
651
652         drm_dev_unregister(drm);
653         drm_dev_unref(drm);
654         dev_set_drvdata(dev, NULL);
655 }
656
657 static const struct component_master_ops rockchip_drm_ops = {
658         .bind = rockchip_drm_bind,
659         .unbind = rockchip_drm_unbind,
660 };
661
662 static int rockchip_drm_platform_probe(struct platform_device *pdev)
663 {
664         struct device *dev = &pdev->dev;
665         struct component_match *match = NULL;
666         struct device_node *np = dev->of_node;
667         struct device_node *port;
668         int i;
669
670         if (!np)
671                 return -ENODEV;
672         /*
673          * Bind the crtc ports first, so that
674          * drm_of_find_possible_crtcs called from encoder .bind callbacks
675          * works as expected.
676          */
677         for (i = 0;; i++) {
678                 port = of_parse_phandle(np, "ports", i);
679                 if (!port)
680                         break;
681
682                 if (!of_device_is_available(port->parent)) {
683                         of_node_put(port);
684                         continue;
685                 }
686
687                 component_match_add(dev, &match, compare_of, port->parent);
688                 of_node_put(port);
689         }
690
691         if (i == 0) {
692                 dev_err(dev, "missing 'ports' property\n");
693                 return -ENODEV;
694         }
695
696         if (!match) {
697                 dev_err(dev, "No available vop found for display-subsystem.\n");
698                 return -ENODEV;
699         }
700         /*
701          * For each bound crtc, bind the encoders attached to its
702          * remote endpoint.
703          */
704         for (i = 0;; i++) {
705                 port = of_parse_phandle(np, "ports", i);
706                 if (!port)
707                         break;
708
709                 if (!of_device_is_available(port->parent)) {
710                         of_node_put(port);
711                         continue;
712                 }
713
714                 rockchip_add_endpoints(dev, &match, port);
715                 of_node_put(port);
716         }
717
718         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
719 }
720
721 static int rockchip_drm_platform_remove(struct platform_device *pdev)
722 {
723         component_master_del(&pdev->dev, &rockchip_drm_ops);
724
725         return 0;
726 }
727
728 static const struct of_device_id rockchip_drm_dt_ids[] = {
729         { .compatible = "rockchip,display-subsystem", },
730         { /* sentinel */ },
731 };
732 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
733
734 static struct platform_driver rockchip_drm_platform_driver = {
735         .probe = rockchip_drm_platform_probe,
736         .remove = rockchip_drm_platform_remove,
737         .driver = {
738                 .name = "rockchip-drm",
739                 .of_match_table = rockchip_drm_dt_ids,
740                 .pm = &rockchip_drm_pm_ops,
741         },
742 };
743
744 module_platform_driver(rockchip_drm_platform_driver);
745
746 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
747 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
748 MODULE_LICENSE("GPL v2");