drm/rockchip: add DRM_RENDER_ALLOW
[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 <drm/rockchip_drm.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/module.h>
27 #include <linux/of_graph.h>
28 #include <linux/component.h>
29 #include <linux/fence.h>
30
31 #include "rockchip_drm_drv.h"
32 #include "rockchip_drm_fb.h"
33 #include "rockchip_drm_fbdev.h"
34 #include "rockchip_drm_gem.h"
35 #include "rockchip_drm_rga.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 | DRM_RENDER_ALLOW),
420         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_MAP_OFFSET,
421                           rockchip_gem_map_offset_ioctl,
422                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
423         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_ACQUIRE,
424                           rockchip_gem_cpu_acquire_ioctl,
425                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
426         DRM_IOCTL_DEF_DRV(ROCKCHIP_GEM_CPU_RELEASE,
427                           rockchip_gem_cpu_release_ioctl,
428                           DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
429         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_GET_VER, rockchip_rga_get_ver_ioctl,
430                           DRM_AUTH | DRM_RENDER_ALLOW),
431         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_SET_CMDLIST,
432                           rockchip_rga_set_cmdlist_ioctl,
433                           DRM_AUTH | DRM_RENDER_ALLOW),
434         DRM_IOCTL_DEF_DRV(ROCKCHIP_RGA_EXEC, rockchip_rga_exec_ioctl,
435                           DRM_AUTH | DRM_RENDER_ALLOW),
436 };
437
438 static const struct file_operations rockchip_drm_driver_fops = {
439         .owner = THIS_MODULE,
440         .open = drm_open,
441         .mmap = rockchip_gem_mmap,
442         .poll = drm_poll,
443         .read = drm_read,
444         .unlocked_ioctl = drm_ioctl,
445 #ifdef CONFIG_COMPAT
446         .compat_ioctl = drm_compat_ioctl,
447 #endif
448         .release = drm_release,
449 };
450
451 const struct vm_operations_struct rockchip_drm_vm_ops = {
452         .open = drm_gem_vm_open,
453         .close = drm_gem_vm_close,
454 };
455
456 static struct drm_driver rockchip_drm_driver = {
457         .driver_features        = DRIVER_MODESET | DRIVER_GEM |
458                                   DRIVER_PRIME | DRIVER_ATOMIC |
459                                   DRIVER_RENDER,
460         .load                   = rockchip_drm_load,
461         .unload                 = rockchip_drm_unload,
462         .preclose               = rockchip_drm_preclose,
463         .lastclose              = rockchip_drm_lastclose,
464         .get_vblank_counter     = drm_vblank_no_hw_counter,
465         .open                   = rockchip_drm_open,
466         .postclose              = rockchip_drm_postclose,
467         .enable_vblank          = rockchip_drm_crtc_enable_vblank,
468         .disable_vblank         = rockchip_drm_crtc_disable_vblank,
469         .gem_vm_ops             = &rockchip_drm_vm_ops,
470         .gem_free_object        = rockchip_gem_free_object,
471         .dumb_create            = rockchip_gem_dumb_create,
472         .dumb_map_offset        = rockchip_gem_dumb_map_offset,
473         .dumb_destroy           = drm_gem_dumb_destroy,
474         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
475         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
476         .gem_prime_import       = drm_gem_prime_import,
477         .gem_prime_export       = drm_gem_prime_export,
478         .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
479         .gem_prime_vmap         = rockchip_gem_prime_vmap,
480         .gem_prime_vunmap       = rockchip_gem_prime_vunmap,
481         .gem_prime_mmap         = rockchip_gem_mmap_buf,
482         .ioctls                 = rockchip_ioctls,
483         .num_ioctls             = ARRAY_SIZE(rockchip_ioctls),
484         .fops                   = &rockchip_drm_driver_fops,
485         .name   = DRIVER_NAME,
486         .desc   = DRIVER_DESC,
487         .date   = DRIVER_DATE,
488         .major  = DRIVER_MAJOR,
489         .minor  = DRIVER_MINOR,
490 };
491
492 #ifdef CONFIG_PM_SLEEP
493 static int rockchip_drm_sys_suspend(struct device *dev)
494 {
495         struct drm_device *drm = dev_get_drvdata(dev);
496         struct drm_connector *connector;
497
498         if (!drm)
499                 return 0;
500
501         drm_modeset_lock_all(drm);
502         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
503                 int old_dpms = connector->dpms;
504
505                 if (connector->funcs->dpms)
506                         connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
507
508                 /* Set the old mode back to the connector for resume */
509                 connector->dpms = old_dpms;
510         }
511         drm_modeset_unlock_all(drm);
512
513         return 0;
514 }
515
516 static int rockchip_drm_sys_resume(struct device *dev)
517 {
518         struct drm_device *drm = dev_get_drvdata(dev);
519         struct drm_connector *connector;
520         enum drm_connector_status status;
521         bool changed = false;
522
523         if (!drm)
524                 return 0;
525
526         drm_modeset_lock_all(drm);
527         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
528                 int desired_mode = connector->dpms;
529
530                 /*
531                  * at suspend time, we save dpms to connector->dpms,
532                  * restore the old_dpms, and at current time, the connector
533                  * dpms status must be DRM_MODE_DPMS_OFF.
534                  */
535                 connector->dpms = DRM_MODE_DPMS_OFF;
536
537                 /*
538                  * If the connector has been disconnected during suspend,
539                  * disconnect it from the encoder and leave it off. We'll notify
540                  * userspace at the end.
541                  */
542                 if (desired_mode == DRM_MODE_DPMS_ON) {
543                         status = connector->funcs->detect(connector, true);
544                         if (status == connector_status_disconnected) {
545                                 connector->encoder = NULL;
546                                 connector->status = status;
547                                 changed = true;
548                                 continue;
549                         }
550                 }
551                 if (connector->funcs->dpms)
552                         connector->funcs->dpms(connector, desired_mode);
553         }
554         drm_modeset_unlock_all(drm);
555
556         drm_helper_resume_force_mode(drm);
557
558         if (changed)
559                 drm_kms_helper_hotplug_event(drm);
560
561         return 0;
562 }
563 #endif
564
565 static const struct dev_pm_ops rockchip_drm_pm_ops = {
566         SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
567                                 rockchip_drm_sys_resume)
568 };
569
570 static int compare_of(struct device *dev, void *data)
571 {
572         struct device_node *np = data;
573
574         return dev->of_node == np;
575 }
576
577 static void rockchip_add_endpoints(struct device *dev,
578                                    struct component_match **match,
579                                    struct device_node *port)
580 {
581         struct device_node *ep, *remote;
582
583         for_each_child_of_node(port, ep) {
584                 remote = of_graph_get_remote_port_parent(ep);
585                 if (!remote || !of_device_is_available(remote)) {
586                         of_node_put(remote);
587                         continue;
588                 } else if (!of_device_is_available(remote->parent)) {
589                         dev_warn(dev, "parent device of %s is not available\n",
590                                  remote->full_name);
591                         of_node_put(remote);
592                         continue;
593                 }
594
595                 component_match_add(dev, match, compare_of, remote);
596                 of_node_put(remote);
597         }
598 }
599
600 static int rockchip_drm_bind(struct device *dev)
601 {
602         struct drm_device *drm;
603         int ret;
604
605         drm = drm_dev_alloc(&rockchip_drm_driver, dev);
606         if (!drm)
607                 return -ENOMEM;
608
609         ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
610         if (ret)
611                 goto err_free;
612
613         ret = drm_dev_register(drm, 0);
614         if (ret)
615                 goto err_free;
616
617         dev_set_drvdata(dev, drm);
618
619         return 0;
620
621 err_free:
622         drm_dev_unref(drm);
623         return ret;
624 }
625
626 static void rockchip_drm_unbind(struct device *dev)
627 {
628         struct drm_device *drm = dev_get_drvdata(dev);
629
630         drm_dev_unregister(drm);
631         drm_dev_unref(drm);
632         dev_set_drvdata(dev, NULL);
633 }
634
635 static const struct component_master_ops rockchip_drm_ops = {
636         .bind = rockchip_drm_bind,
637         .unbind = rockchip_drm_unbind,
638 };
639
640 static int rockchip_drm_platform_probe(struct platform_device *pdev)
641 {
642         struct device *dev = &pdev->dev;
643         struct component_match *match = NULL;
644         struct device_node *np = dev->of_node;
645         struct device_node *port;
646         int i;
647
648         if (!np)
649                 return -ENODEV;
650         /*
651          * Bind the crtc ports first, so that
652          * drm_of_find_possible_crtcs called from encoder .bind callbacks
653          * works as expected.
654          */
655         for (i = 0;; i++) {
656                 port = of_parse_phandle(np, "ports", i);
657                 if (!port)
658                         break;
659
660                 if (!of_device_is_available(port->parent)) {
661                         of_node_put(port);
662                         continue;
663                 }
664
665                 component_match_add(dev, &match, compare_of, port->parent);
666                 of_node_put(port);
667         }
668
669         if (i == 0) {
670                 dev_err(dev, "missing 'ports' property\n");
671                 return -ENODEV;
672         }
673
674         if (!match) {
675                 dev_err(dev, "No available vop found for display-subsystem.\n");
676                 return -ENODEV;
677         }
678         /*
679          * For each bound crtc, bind the encoders attached to its
680          * remote endpoint.
681          */
682         for (i = 0;; i++) {
683                 port = of_parse_phandle(np, "ports", i);
684                 if (!port)
685                         break;
686
687                 if (!of_device_is_available(port->parent)) {
688                         of_node_put(port);
689                         continue;
690                 }
691
692                 rockchip_add_endpoints(dev, &match, port);
693                 of_node_put(port);
694         }
695
696         return component_master_add_with_match(dev, &rockchip_drm_ops, match);
697 }
698
699 static int rockchip_drm_platform_remove(struct platform_device *pdev)
700 {
701         component_master_del(&pdev->dev, &rockchip_drm_ops);
702
703         return 0;
704 }
705
706 static const struct of_device_id rockchip_drm_dt_ids[] = {
707         { .compatible = "rockchip,display-subsystem", },
708         { /* sentinel */ },
709 };
710 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
711
712 static struct platform_driver rockchip_drm_platform_driver = {
713         .probe = rockchip_drm_platform_probe,
714         .remove = rockchip_drm_platform_remove,
715         .driver = {
716                 .name = "rockchip-drm",
717                 .of_match_table = rockchip_drm_dt_ids,
718                 .pm = &rockchip_drm_pm_ops,
719         },
720 };
721
722 module_platform_driver(rockchip_drm_platform_driver);
723
724 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
725 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
726 MODULE_LICENSE("GPL v2");