CHROMIUM: [media] rk3288-vpu: Workaround for encode after decode
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / rk3288-vpu / rk3288_vpu.c
1 /*
2  * Rockchip RK3288 VPU codec driver
3  *
4  * Copyright (C) 2014 Google, Inc.
5  *      Tomasz Figa <tfiga@chromium.org>
6  *
7  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
8  *
9  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
10  *
11  * This software is licensed under the terms of the GNU General Public
12  * License version 2, as published by the Free Software Foundation, and
13  * may be copied, distributed, and modified under those terms.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20
21 #include "rk3288_vpu_common.h"
22
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-event.h>
29 #include <linux/workqueue.h>
30 #include <linux/of.h>
31 #include <media/videobuf2-core.h>
32 #include <media/videobuf2-dma-contig.h>
33
34 #include "rk3288_vpu_dec.h"
35 #include "rk3288_vpu_enc.h"
36 #include "rk3288_vpu_hw.h"
37
38 int debug;
39 module_param(debug, int, S_IRUGO | S_IWUSR);
40 MODULE_PARM_DESC(debug,
41                  "Debug level - higher value produces more verbose messages");
42
43 /*
44  * DMA coherent helpers.
45  */
46
47 int rk3288_vpu_aux_buf_alloc(struct rk3288_vpu_dev *vpu,
48                             struct rk3288_vpu_aux_buf *buf, size_t size)
49 {
50         buf->cpu = dma_alloc_coherent(vpu->dev, size, &buf->dma, GFP_KERNEL);
51         if (!buf->cpu)
52                 return -ENOMEM;
53
54         buf->size = size;
55         return 0;
56 }
57
58 void rk3288_vpu_aux_buf_free(struct rk3288_vpu_dev *vpu,
59                              struct rk3288_vpu_aux_buf *buf)
60 {
61         dma_free_coherent(vpu->dev, buf->size, buf->cpu, buf->dma);
62
63         buf->cpu = NULL;
64         buf->dma = 0;
65         buf->size = 0;
66 }
67
68 /*
69  * Context scheduling.
70  */
71
72 static void rk3288_vpu_prepare_run(struct rk3288_vpu_ctx *ctx)
73 {
74         if (ctx->run_ops->prepare_run)
75                 ctx->run_ops->prepare_run(ctx);
76 }
77
78 static void __rk3288_vpu_dequeue_run_locked(struct rk3288_vpu_ctx *ctx)
79 {
80         struct rk3288_vpu_buf *src, *dst;
81
82         /*
83          * Since ctx was dequeued from ready_ctxs list, we know that it has
84          * at least one buffer in each queue.
85          */
86         src = list_first_entry(&ctx->src_queue, struct rk3288_vpu_buf, list);
87         dst = list_first_entry(&ctx->dst_queue, struct rk3288_vpu_buf, list);
88
89         list_del(&src->list);
90         list_del(&dst->list);
91
92         ctx->run.src = src;
93         ctx->run.dst = dst;
94 }
95
96 static struct rk3288_vpu_ctx *
97 rk3288_vpu_encode_after_decode_war(struct rk3288_vpu_ctx *ctx)
98 {
99         struct rk3288_vpu_dev *dev = ctx->dev;
100
101         if (dev->was_decoding && rk3288_vpu_ctx_is_encoder(ctx))
102                 return dev->dummy_encode_ctx;
103
104         return ctx;
105 }
106
107 static void rk3288_vpu_try_run(struct rk3288_vpu_dev *dev)
108 {
109         struct rk3288_vpu_ctx *ctx = NULL;
110         unsigned long flags;
111
112         vpu_debug_enter();
113
114         spin_lock_irqsave(&dev->irqlock, flags);
115
116         if (list_empty(&dev->ready_ctxs) ||
117             test_bit(VPU_SUSPENDED, &dev->state))
118                 /* Nothing to do. */
119                 goto out;
120
121         if (test_and_set_bit(VPU_RUNNING, &dev->state))
122                 /*
123                 * The hardware is already running. We will pick another
124                 * run after we get the notification in rk3288_vpu_run_done().
125                 */
126                 goto out;
127
128         ctx = list_entry(dev->ready_ctxs.next, struct rk3288_vpu_ctx, list);
129
130         /*
131          * WAR for corrupted hardware state when encoding directly after
132          * certain decoding runs.
133          *
134          * If previous context was decoding and currently picked one is
135          * encoding then we need to execute a dummy encode with proper
136          * settings to reinitialize certain internal hardware state.
137          */
138         ctx = rk3288_vpu_encode_after_decode_war(ctx);
139
140         if (!rk3288_vpu_ctx_is_dummy_encode(ctx)) {
141                 list_del_init(&ctx->list);
142                 __rk3288_vpu_dequeue_run_locked(ctx);
143         }
144
145         dev->current_ctx = ctx;
146         dev->was_decoding = !rk3288_vpu_ctx_is_encoder(ctx);
147
148 out:
149         spin_unlock_irqrestore(&dev->irqlock, flags);
150
151         if (ctx) {
152                 rk3288_vpu_prepare_run(ctx);
153                 rk3288_vpu_run(ctx);
154         }
155
156         vpu_debug_leave();
157 }
158
159 static void __rk3288_vpu_try_context_locked(struct rk3288_vpu_dev *dev,
160                                             struct rk3288_vpu_ctx *ctx)
161 {
162         if (!list_empty(&ctx->list))
163                 /* Context already queued. */
164                 return;
165
166         if (!list_empty(&ctx->dst_queue) && !list_empty(&ctx->src_queue))
167                 list_add_tail(&ctx->list, &dev->ready_ctxs);
168 }
169
170 void rk3288_vpu_run_done(struct rk3288_vpu_ctx *ctx,
171                          enum vb2_buffer_state result)
172 {
173         struct rk3288_vpu_dev *dev = ctx->dev;
174         unsigned long flags;
175
176         vpu_debug_enter();
177
178         if (ctx->run_ops->run_done)
179                 ctx->run_ops->run_done(ctx, result);
180
181         if (!rk3288_vpu_ctx_is_dummy_encode(ctx)) {
182                 struct vb2_buffer *src = &ctx->run.src->b;
183                 struct vb2_buffer *dst = &ctx->run.dst->b;
184
185                 dst->v4l2_buf.timestamp = src->v4l2_buf.timestamp;
186                 vb2_buffer_done(&ctx->run.src->b, result);
187                 vb2_buffer_done(&ctx->run.dst->b, result);
188         }
189
190         dev->current_ctx = NULL;
191         wake_up_all(&dev->run_wq);
192
193         spin_lock_irqsave(&dev->irqlock, flags);
194
195         __rk3288_vpu_try_context_locked(dev, ctx);
196         clear_bit(VPU_RUNNING, &dev->state);
197
198         spin_unlock_irqrestore(&dev->irqlock, flags);
199
200         /* Try scheduling another run to see if we have anything left to do. */
201         rk3288_vpu_try_run(dev);
202
203         vpu_debug_leave();
204 }
205
206 void rk3288_vpu_try_context(struct rk3288_vpu_dev *dev,
207                             struct rk3288_vpu_ctx *ctx)
208 {
209         unsigned long flags;
210
211         vpu_debug_enter();
212
213         spin_lock_irqsave(&dev->irqlock, flags);
214
215         __rk3288_vpu_try_context_locked(dev, ctx);
216
217         spin_unlock_irqrestore(&dev->irqlock, flags);
218
219         rk3288_vpu_try_run(dev);
220
221         vpu_debug_enter();
222 }
223
224 /*
225  * Control registration.
226  */
227
228 #define IS_VPU_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) && \
229                           V4L2_CTRL_DRIVER_PRIV(x))
230
231 int rk3288_vpu_ctrls_setup(struct rk3288_vpu_ctx *ctx,
232                            const struct v4l2_ctrl_ops *ctrl_ops,
233                            struct rk3288_vpu_control *controls,
234                            unsigned num_ctrls,
235                            const char *const *(*get_menu)(u32))
236 {
237         struct v4l2_ctrl_config cfg;
238         int i;
239
240         if (num_ctrls > ARRAY_SIZE(ctx->ctrls)) {
241                 vpu_err("context control array not large enough\n");
242                 return -ENOSPC;
243         }
244
245         v4l2_ctrl_handler_init(&ctx->ctrl_handler, num_ctrls);
246         if (ctx->ctrl_handler.error) {
247                 vpu_err("v4l2_ctrl_handler_init failed\n");
248                 return ctx->ctrl_handler.error;
249         }
250
251         for (i = 0; i < num_ctrls; i++) {
252                 if (IS_VPU_PRIV(controls[i].id)
253                     || controls[i].id >= V4L2_CID_CUSTOM_BASE
254                     || controls[i].type == V4L2_CTRL_TYPE_PRIVATE) {
255                         memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
256
257                         cfg.ops = ctrl_ops;
258                         cfg.id = controls[i].id;
259                         cfg.min = controls[i].minimum;
260                         cfg.max = controls[i].maximum;
261                         cfg.max_stores = controls[i].max_stores;
262                         cfg.def = controls[i].default_value;
263                         cfg.name = controls[i].name;
264                         cfg.type = controls[i].type;
265                         cfg.elem_size = controls[i].elem_size;
266                         memcpy(cfg.dims, controls[i].dims, sizeof(cfg.dims));
267
268                         if (cfg.type == V4L2_CTRL_TYPE_MENU) {
269                                 cfg.menu_skip_mask = cfg.menu_skip_mask;
270                                 cfg.qmenu = get_menu(cfg.id);
271                         } else {
272                                 cfg.step = controls[i].step;
273                         }
274
275                         ctx->ctrls[i] = v4l2_ctrl_new_custom(
276                                 &ctx->ctrl_handler, &cfg, NULL);
277                 } else {
278                         if (controls[i].type == V4L2_CTRL_TYPE_MENU) {
279                                 ctx->ctrls[i] =
280                                     v4l2_ctrl_new_std_menu
281                                         (&ctx->ctrl_handler,
282                                          ctrl_ops,
283                                          controls[i].id,
284                                          controls[i].maximum,
285                                          0,
286                                          controls[i].
287                                          default_value);
288                         } else {
289                                 ctx->ctrls[i] =
290                                     v4l2_ctrl_new_std(&ctx->ctrl_handler,
291                                                       ctrl_ops,
292                                                       controls[i].id,
293                                                       controls[i].minimum,
294                                                       controls[i].maximum,
295                                                       controls[i].step,
296                                                       controls[i].
297                                                       default_value);
298                         }
299                 }
300
301                 if (ctx->ctrl_handler.error) {
302                         vpu_err("Adding control (%d) failed\n", i);
303                         return ctx->ctrl_handler.error;
304                 }
305
306                 if (controls[i].is_volatile && ctx->ctrls[i])
307                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
308                 if (controls[i].is_read_only && ctx->ctrls[i])
309                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_READ_ONLY;
310                 if (controls[i].can_store && ctx->ctrls[i])
311                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_CAN_STORE;
312         }
313
314         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
315         ctx->num_ctrls = num_ctrls;
316         return 0;
317 }
318
319 void rk3288_vpu_ctrls_delete(struct rk3288_vpu_ctx *ctx)
320 {
321         int i;
322
323         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
324         for (i = 0; i < ctx->num_ctrls; i++)
325                 ctx->ctrls[i] = NULL;
326 }
327
328 /*
329  * V4L2 file operations.
330  */
331
332 static int rk3288_vpu_open(struct file *filp)
333 {
334         struct video_device *vdev = video_devdata(filp);
335         struct rk3288_vpu_dev *dev = video_drvdata(filp);
336         struct rk3288_vpu_ctx *ctx = NULL;
337         struct vb2_queue *q;
338         int ret = 0;
339
340         /*
341          * We do not need any extra locking here, because we operate only
342          * on local data here, except reading few fields from dev, which
343          * do not change through device's lifetime (which is guaranteed by
344          * reference on module from open()) and V4L2 internal objects (such
345          * as vdev and ctx->fh), which have proper locking done in respective
346          * helper functions used here.
347          */
348
349         vpu_debug_enter();
350
351         /* Allocate memory for context */
352         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
353         if (!ctx) {
354                 ret = -ENOMEM;
355                 goto err_leave;
356         }
357
358         v4l2_fh_init(&ctx->fh, video_devdata(filp));
359         filp->private_data = &ctx->fh;
360         v4l2_fh_add(&ctx->fh);
361         ctx->dev = dev;
362         INIT_LIST_HEAD(&ctx->src_queue);
363         INIT_LIST_HEAD(&ctx->dst_queue);
364         INIT_LIST_HEAD(&ctx->list);
365
366         if (vdev == dev->vfd_enc) {
367                 /* only for encoder */
368                 ret = rk3288_vpu_enc_init(ctx);
369                 if (ret) {
370                         vpu_err("Failed to initialize encoder context\n");
371                         goto err_fh_free;
372                 }
373         } else if (vdev == dev->vfd_dec) {
374                 /* only for decoder */
375                 ret = rk3288_vpu_dec_init(ctx);
376                 if (ret) {
377                         vpu_err("Failed to initialize decoder context\n");
378                         goto err_fh_free;
379                 }
380         } else {
381                 ret = -ENOENT;
382                 goto err_fh_free;
383         }
384         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
385
386         /* Init videobuf2 queue for CAPTURE */
387         q = &ctx->vq_dst;
388         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
389         q->drv_priv = &ctx->fh;
390         q->io_modes = VB2_MMAP | VB2_USERPTR;
391         q->lock = &dev->vpu_mutex;
392         q->buf_struct_size = sizeof(struct rk3288_vpu_buf);
393
394         if (vdev == dev->vfd_enc)
395                 q->ops = get_enc_queue_ops();
396         else if (vdev == dev->vfd_dec)
397                 q->ops = get_dec_queue_ops();
398
399         q->mem_ops = &vb2_dma_contig_memops;
400         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
401
402         ret = vb2_queue_init(q);
403         if (ret) {
404                 vpu_err("Failed to initialize videobuf2 queue(capture)\n");
405                 goto err_enc_dec_exit;
406         }
407
408         /* Init videobuf2 queue for OUTPUT */
409         q = &ctx->vq_src;
410         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
411         q->drv_priv = &ctx->fh;
412         q->io_modes = VB2_MMAP | VB2_USERPTR;
413         q->lock = &dev->vpu_mutex;
414         q->buf_struct_size = sizeof(struct rk3288_vpu_buf);
415
416         if (vdev == dev->vfd_enc)
417                 q->ops = get_enc_queue_ops();
418         else if (vdev == dev->vfd_dec)
419                 q->ops = get_dec_queue_ops();
420
421         q->mem_ops = &vb2_dma_contig_memops;
422         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
423
424         ret = vb2_queue_init(q);
425         if (ret) {
426                 vpu_err("Failed to initialize videobuf2 queue(output)\n");
427                 goto err_vq_dst_release;
428         }
429
430         vpu_debug_leave();
431
432         return 0;
433
434 err_vq_dst_release:
435         vb2_queue_release(&ctx->vq_dst);
436 err_enc_dec_exit:
437         if (vdev == dev->vfd_enc)
438                 rk3288_vpu_enc_exit(ctx);
439         else if (vdev == dev->vfd_dec)
440                 rk3288_vpu_dec_exit(ctx);
441 err_fh_free:
442         v4l2_fh_del(&ctx->fh);
443         v4l2_fh_exit(&ctx->fh);
444         kfree(ctx);
445 err_leave:
446         vpu_debug_leave();
447
448         return ret;
449 }
450
451 static int rk3288_vpu_release(struct file *filp)
452 {
453         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
454         struct video_device *vdev = video_devdata(filp);
455         struct rk3288_vpu_dev *dev = ctx->dev;
456
457         /*
458          * No need for extra locking because this was the last reference
459          * to this file.
460          */
461
462         vpu_debug_enter();
463
464         /*
465          * vb2_queue_release() ensures that streaming is stopped, which
466          * in turn means that there are no frames still being processed
467          * by hardware.
468          */
469         vb2_queue_release(&ctx->vq_src);
470         vb2_queue_release(&ctx->vq_dst);
471
472         v4l2_fh_del(&ctx->fh);
473         v4l2_fh_exit(&ctx->fh);
474
475         if (vdev == dev->vfd_enc)
476                 rk3288_vpu_enc_exit(ctx);
477         else if (vdev == dev->vfd_dec)
478                 rk3288_vpu_dec_exit(ctx);
479
480         kfree(ctx);
481
482         vpu_debug_leave();
483
484         return 0;
485 }
486
487 static unsigned int rk3288_vpu_poll(struct file *filp,
488                                     struct poll_table_struct *wait)
489 {
490         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
491         struct vb2_queue *src_q, *dst_q;
492         struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
493         unsigned int rc = 0;
494         unsigned long flags;
495
496         vpu_debug_enter();
497
498         src_q = &ctx->vq_src;
499         dst_q = &ctx->vq_dst;
500
501         /*
502          * There has to be at least one buffer queued on each queued_list, which
503          * means either in driver already or waiting for driver to claim it
504          * and start processing.
505          */
506         if ((!vb2_is_streaming(src_q) || list_empty(&src_q->queued_list)) &&
507             (!vb2_is_streaming(dst_q) || list_empty(&dst_q->queued_list))) {
508                 vpu_debug(0, "src q streaming %d, dst q streaming %d, src list empty(%d), dst list empty(%d)\n",
509                                 src_q->streaming, dst_q->streaming,
510                                 list_empty(&src_q->queued_list),
511                                 list_empty(&dst_q->queued_list));
512                 return POLLERR;
513         }
514
515         poll_wait(filp, &ctx->fh.wait, wait);
516         poll_wait(filp, &src_q->done_wq, wait);
517         poll_wait(filp, &dst_q->done_wq, wait);
518
519         if (v4l2_event_pending(&ctx->fh))
520                 rc |= POLLPRI;
521
522         spin_lock_irqsave(&src_q->done_lock, flags);
523
524         if (!list_empty(&src_q->done_list))
525                 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
526                                                 done_entry);
527
528         if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE ||
529                         src_vb->state == VB2_BUF_STATE_ERROR))
530                 rc |= POLLOUT | POLLWRNORM;
531
532         spin_unlock_irqrestore(&src_q->done_lock, flags);
533
534         spin_lock_irqsave(&dst_q->done_lock, flags);
535
536         if (!list_empty(&dst_q->done_list))
537                 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
538                                                 done_entry);
539
540         if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE ||
541                         dst_vb->state == VB2_BUF_STATE_ERROR))
542                 rc |= POLLIN | POLLRDNORM;
543
544         spin_unlock_irqrestore(&dst_q->done_lock, flags);
545
546         return rc;
547 }
548
549 static int rk3288_vpu_mmap(struct file *filp, struct vm_area_struct *vma)
550 {
551         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
552         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
553         int ret;
554
555         vpu_debug_enter();
556
557         if (offset < DST_QUEUE_OFF_BASE) {
558                 vpu_debug(4, "mmaping source\n");
559
560                 ret = vb2_mmap(&ctx->vq_src, vma);
561         } else {                /* capture */
562                 vpu_debug(4, "mmaping destination\n");
563
564                 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
565                 ret = vb2_mmap(&ctx->vq_dst, vma);
566         }
567
568         vpu_debug_leave();
569
570         return ret;
571 }
572
573 static const struct v4l2_file_operations rk3288_vpu_fops = {
574         .owner = THIS_MODULE,
575         .open = rk3288_vpu_open,
576         .release = rk3288_vpu_release,
577         .poll = rk3288_vpu_poll,
578         .unlocked_ioctl = video_ioctl2,
579         .mmap = rk3288_vpu_mmap,
580 };
581
582 /*
583  * Platform driver.
584  */
585
586 static int rk3288_vpu_probe(struct platform_device *pdev)
587 {
588         struct rk3288_vpu_dev *vpu = NULL;
589         DEFINE_DMA_ATTRS(attrs_novm);
590         struct video_device *vfd;
591         int ret = 0;
592
593         vpu_debug_enter();
594
595         vpu = devm_kzalloc(&pdev->dev, sizeof(*vpu), GFP_KERNEL);
596         if (!vpu)
597                 return -ENOMEM;
598
599         vpu->dev = &pdev->dev;
600         vpu->pdev = pdev;
601         mutex_init(&vpu->vpu_mutex);
602         spin_lock_init(&vpu->irqlock);
603         INIT_LIST_HEAD(&vpu->ready_ctxs);
604         init_waitqueue_head(&vpu->run_wq);
605
606         ret = rk3288_vpu_hw_probe(vpu);
607         if (ret) {
608                 dev_err(&pdev->dev, "vcodec_hw_probe failed\n");
609                 goto err_hw_probe;
610         }
611
612         dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs_novm);
613         vpu->alloc_ctx = vb2_dma_contig_init_ctx_attrs(&pdev->dev,
614                                                                 &attrs_novm);
615         if (IS_ERR(vpu->alloc_ctx)) {
616                 ret = PTR_ERR(vpu->alloc_ctx);
617                 goto err_dma_contig;
618         }
619
620         vpu->alloc_ctx_vm = vb2_dma_contig_init_ctx(&pdev->dev);
621         if (IS_ERR(vpu->alloc_ctx_vm)) {
622                 ret = PTR_ERR(vpu->alloc_ctx_vm);
623                 goto err_dma_contig_vm;
624         }
625
626         ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
627         if (ret) {
628                 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
629                 goto err_v4l2_dev_reg;
630         }
631
632         platform_set_drvdata(pdev, vpu);
633
634         ret = rk3288_vpu_enc_init_dummy_ctx(vpu);
635         if (ret) {
636                 dev_err(&pdev->dev, "Failed to create dummy encode context\n");
637                 goto err_dummy_enc;
638         }
639
640         /* encoder */
641         vfd = video_device_alloc();
642         if (!vfd) {
643                 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
644                 ret = -ENOMEM;
645                 goto err_enc_alloc;
646         }
647
648         vfd->fops = &rk3288_vpu_fops;
649         vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
650         vfd->release = video_device_release;
651         vfd->lock = &vpu->vpu_mutex;
652         vfd->v4l2_dev = &vpu->v4l2_dev;
653         vfd->vfl_dir = VFL_DIR_M2M;
654         snprintf(vfd->name, sizeof(vfd->name), "%s", RK3288_VPU_ENC_NAME);
655         vpu->vfd_enc = vfd;
656
657         video_set_drvdata(vfd, vpu);
658
659         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
660         if (ret) {
661                 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
662                 video_device_release(vfd);
663                 goto err_enc_reg;
664         }
665
666         v4l2_info(&vpu->v4l2_dev,
667                 "Rockchip RK3288 VPU encoder registered as /vpu/video%d\n",
668                 vfd->num);
669
670         /* decoder */
671         vfd = video_device_alloc();
672         if (!vfd) {
673                 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
674                 ret = -ENOMEM;
675                 goto err_dec_alloc;
676         }
677
678         vfd->fops = &rk3288_vpu_fops;
679         vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
680         vfd->release = video_device_release;
681         vfd->lock = &vpu->vpu_mutex;
682         vfd->v4l2_dev = &vpu->v4l2_dev;
683         vfd->vfl_dir = VFL_DIR_M2M;
684         snprintf(vfd->name, sizeof(vfd->name), "%s", RK3288_VPU_DEC_NAME);
685         vpu->vfd_dec = vfd;
686
687         video_set_drvdata(vfd, vpu);
688
689         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
690         if (ret) {
691                 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
692                 video_device_release(vfd);
693                 goto err_dec_reg;
694         }
695
696         v4l2_info(&vpu->v4l2_dev,
697                 "Rockchip RK3288 VPU decoder registered as /vpu/video%d\n",
698                 vfd->num);
699
700         vpu_debug_leave();
701
702         return 0;
703
704 err_dec_reg:
705         video_device_release(vpu->vfd_dec);
706 err_dec_alloc:
707         video_unregister_device(vpu->vfd_enc);
708 err_enc_reg:
709         video_device_release(vpu->vfd_enc);
710 err_enc_alloc:
711         rk3288_vpu_enc_free_dummy_ctx(vpu);
712 err_dummy_enc:
713         v4l2_device_unregister(&vpu->v4l2_dev);
714 err_v4l2_dev_reg:
715         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx_vm);
716 err_dma_contig_vm:
717         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx);
718 err_dma_contig:
719         rk3288_vpu_hw_remove(vpu);
720 err_hw_probe:
721         pr_debug("%s-- with error\n", __func__);
722         vpu_debug_leave();
723
724         return ret;
725 }
726
727 static int rk3288_vpu_remove(struct platform_device *pdev)
728 {
729         struct rk3288_vpu_dev *vpu = platform_get_drvdata(pdev);
730
731         vpu_debug_enter();
732
733         v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name);
734
735         /*
736          * We are safe here assuming that .remove() got called as
737          * a result of module removal, which guarantees that all
738          * contexts have been released.
739          */
740
741         video_unregister_device(vpu->vfd_dec);
742         video_unregister_device(vpu->vfd_enc);
743         rk3288_vpu_enc_free_dummy_ctx(vpu);
744         v4l2_device_unregister(&vpu->v4l2_dev);
745         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx_vm);
746         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx);
747         rk3288_vpu_hw_remove(vpu);
748
749         vpu_debug_leave();
750
751         return 0;
752 }
753
754 static struct platform_device_id vpu_driver_ids[] = {
755         { .name = "rk3288-vpu", },
756         { /* sentinel */ }
757 };
758
759 MODULE_DEVICE_TABLE(platform, vpu_driver_ids);
760
761 #ifdef CONFIG_OF
762 static const struct of_device_id of_rk3288_vpu_match[] = {
763         { .compatible = "rockchip,rk3288-vpu", },
764         { /* sentinel */ }
765 };
766 MODULE_DEVICE_TABLE(of, of_rk3288_vpu_match);
767 #endif
768
769 #ifdef CONFIG_PM_SLEEP
770 static int rk3288_vpu_suspend(struct device *dev)
771 {
772         struct rk3288_vpu_dev *vpu = dev_get_drvdata(dev);
773
774         set_bit(VPU_SUSPENDED, &vpu->state);
775         wait_event(vpu->run_wq, vpu->current_ctx == NULL);
776
777         return 0;
778 }
779
780 static int rk3288_vpu_resume(struct device *dev)
781 {
782         struct rk3288_vpu_dev *vpu = dev_get_drvdata(dev);
783
784         clear_bit(VPU_SUSPENDED, &vpu->state);
785         rk3288_vpu_try_run(vpu);
786
787         return 0;
788 }
789 #endif
790
791 static const struct dev_pm_ops rk3288_vpu_pm_ops = {
792         SET_SYSTEM_SLEEP_PM_OPS(rk3288_vpu_suspend, rk3288_vpu_resume)
793 };
794
795 static struct platform_driver rk3288_vpu_driver = {
796         .probe = rk3288_vpu_probe,
797         .remove = rk3288_vpu_remove,
798         .id_table = vpu_driver_ids,
799         .driver = {
800                    .name = RK3288_VPU_NAME,
801                    .owner = THIS_MODULE,
802                    .of_match_table = of_match_ptr(of_rk3288_vpu_match),
803                    .pm = &rk3288_vpu_pm_ops,
804         },
805 };
806 module_platform_driver(rk3288_vpu_driver);
807
808 MODULE_LICENSE("GPL v2");
809 MODULE_AUTHOR("Alpha Lin <Alpha.Lin@Rock-Chips.com>");
810 MODULE_AUTHOR("Tomasz Figa <tfiga@chromium.org>");
811 MODULE_DESCRIPTION("Rockchip RK3288 VPU codec driver");