ac35b0cf96655dc20f1ef817abddbcbde2e00faa
[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 | VB2_DMABUF;
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                 q->use_dma_bidirectional = 1;
399         }
400
401         q->mem_ops = &vb2_dma_contig_memops;
402         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
403
404         ret = vb2_queue_init(q);
405         if (ret) {
406                 vpu_err("Failed to initialize videobuf2 queue(capture)\n");
407                 goto err_enc_dec_exit;
408         }
409
410         /* Init videobuf2 queue for OUTPUT */
411         q = &ctx->vq_src;
412         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
413         q->drv_priv = &ctx->fh;
414         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
415         q->lock = &dev->vpu_mutex;
416         q->buf_struct_size = sizeof(struct rk3288_vpu_buf);
417
418         if (vdev == dev->vfd_enc)
419                 q->ops = get_enc_queue_ops();
420         else if (vdev == dev->vfd_dec)
421                 q->ops = get_dec_queue_ops();
422
423         q->mem_ops = &vb2_dma_contig_memops;
424         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
425
426         ret = vb2_queue_init(q);
427         if (ret) {
428                 vpu_err("Failed to initialize videobuf2 queue(output)\n");
429                 goto err_vq_dst_release;
430         }
431
432         vpu_debug_leave();
433
434         return 0;
435
436 err_vq_dst_release:
437         vb2_queue_release(&ctx->vq_dst);
438 err_enc_dec_exit:
439         if (vdev == dev->vfd_enc)
440                 rk3288_vpu_enc_exit(ctx);
441         else if (vdev == dev->vfd_dec)
442                 rk3288_vpu_dec_exit(ctx);
443 err_fh_free:
444         v4l2_fh_del(&ctx->fh);
445         v4l2_fh_exit(&ctx->fh);
446         kfree(ctx);
447 err_leave:
448         vpu_debug_leave();
449
450         return ret;
451 }
452
453 static int rk3288_vpu_release(struct file *filp)
454 {
455         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
456         struct video_device *vdev = video_devdata(filp);
457         struct rk3288_vpu_dev *dev = ctx->dev;
458
459         /*
460          * No need for extra locking because this was the last reference
461          * to this file.
462          */
463
464         vpu_debug_enter();
465
466         /*
467          * vb2_queue_release() ensures that streaming is stopped, which
468          * in turn means that there are no frames still being processed
469          * by hardware.
470          */
471         vb2_queue_release(&ctx->vq_src);
472         vb2_queue_release(&ctx->vq_dst);
473
474         v4l2_fh_del(&ctx->fh);
475         v4l2_fh_exit(&ctx->fh);
476
477         if (vdev == dev->vfd_enc)
478                 rk3288_vpu_enc_exit(ctx);
479         else if (vdev == dev->vfd_dec)
480                 rk3288_vpu_dec_exit(ctx);
481
482         kfree(ctx);
483
484         vpu_debug_leave();
485
486         return 0;
487 }
488
489 static unsigned int rk3288_vpu_poll(struct file *filp,
490                                     struct poll_table_struct *wait)
491 {
492         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
493         struct vb2_queue *src_q, *dst_q;
494         struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
495         unsigned int rc = 0;
496         unsigned long flags;
497
498         vpu_debug_enter();
499
500         src_q = &ctx->vq_src;
501         dst_q = &ctx->vq_dst;
502
503         /*
504          * There has to be at least one buffer queued on each queued_list, which
505          * means either in driver already or waiting for driver to claim it
506          * and start processing.
507          */
508         if ((!vb2_is_streaming(src_q) || list_empty(&src_q->queued_list)) &&
509             (!vb2_is_streaming(dst_q) || list_empty(&dst_q->queued_list))) {
510                 vpu_debug(0, "src q streaming %d, dst q streaming %d, src list empty(%d), dst list empty(%d)\n",
511                                 src_q->streaming, dst_q->streaming,
512                                 list_empty(&src_q->queued_list),
513                                 list_empty(&dst_q->queued_list));
514                 return POLLERR;
515         }
516
517         poll_wait(filp, &ctx->fh.wait, wait);
518         poll_wait(filp, &src_q->done_wq, wait);
519         poll_wait(filp, &dst_q->done_wq, wait);
520
521         if (v4l2_event_pending(&ctx->fh))
522                 rc |= POLLPRI;
523
524         spin_lock_irqsave(&src_q->done_lock, flags);
525
526         if (!list_empty(&src_q->done_list))
527                 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
528                                                 done_entry);
529
530         if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE ||
531                         src_vb->state == VB2_BUF_STATE_ERROR))
532                 rc |= POLLOUT | POLLWRNORM;
533
534         spin_unlock_irqrestore(&src_q->done_lock, flags);
535
536         spin_lock_irqsave(&dst_q->done_lock, flags);
537
538         if (!list_empty(&dst_q->done_list))
539                 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
540                                                 done_entry);
541
542         if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE ||
543                         dst_vb->state == VB2_BUF_STATE_ERROR))
544                 rc |= POLLIN | POLLRDNORM;
545
546         spin_unlock_irqrestore(&dst_q->done_lock, flags);
547
548         return rc;
549 }
550
551 static int rk3288_vpu_mmap(struct file *filp, struct vm_area_struct *vma)
552 {
553         struct rk3288_vpu_ctx *ctx = fh_to_ctx(filp->private_data);
554         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
555         int ret;
556
557         vpu_debug_enter();
558
559         if (offset < DST_QUEUE_OFF_BASE) {
560                 vpu_debug(4, "mmaping source\n");
561
562                 ret = vb2_mmap(&ctx->vq_src, vma);
563         } else {                /* capture */
564                 vpu_debug(4, "mmaping destination\n");
565
566                 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
567                 ret = vb2_mmap(&ctx->vq_dst, vma);
568         }
569
570         vpu_debug_leave();
571
572         return ret;
573 }
574
575 static const struct v4l2_file_operations rk3288_vpu_fops = {
576         .owner = THIS_MODULE,
577         .open = rk3288_vpu_open,
578         .release = rk3288_vpu_release,
579         .poll = rk3288_vpu_poll,
580         .unlocked_ioctl = video_ioctl2,
581         .mmap = rk3288_vpu_mmap,
582 };
583
584 /*
585  * Platform driver.
586  */
587
588 static int rk3288_vpu_probe(struct platform_device *pdev)
589 {
590         struct rk3288_vpu_dev *vpu = NULL;
591         DEFINE_DMA_ATTRS(attrs_novm);
592         DEFINE_DMA_ATTRS(attrs_nohugepage);
593         struct video_device *vfd;
594         int ret = 0;
595
596         vpu_debug_enter();
597
598         vpu = devm_kzalloc(&pdev->dev, sizeof(*vpu), GFP_KERNEL);
599         if (!vpu)
600                 return -ENOMEM;
601
602         vpu->dev = &pdev->dev;
603         vpu->pdev = pdev;
604         mutex_init(&vpu->vpu_mutex);
605         spin_lock_init(&vpu->irqlock);
606         INIT_LIST_HEAD(&vpu->ready_ctxs);
607         init_waitqueue_head(&vpu->run_wq);
608
609         ret = rk3288_vpu_hw_probe(vpu);
610         if (ret) {
611                 dev_err(&pdev->dev, "vcodec_hw_probe failed\n");
612                 goto err_hw_probe;
613         }
614
615         /*
616          * We'll do mostly sequential access, so sacrifice TLB efficiency for
617          * faster allocation.
618          */
619         dma_set_attr(DMA_ATTR_ALLOC_SINGLE_PAGES, &attrs_novm);
620
621         dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs_novm);
622         vpu->alloc_ctx = vb2_dma_contig_init_ctx_attrs(&pdev->dev,
623                                                                 &attrs_novm);
624         if (IS_ERR(vpu->alloc_ctx)) {
625                 ret = PTR_ERR(vpu->alloc_ctx);
626                 goto err_dma_contig;
627         }
628
629         dma_set_attr(DMA_ATTR_ALLOC_SINGLE_PAGES, &attrs_nohugepage);
630         vpu->alloc_ctx_vm = vb2_dma_contig_init_ctx_attrs(&pdev->dev,
631                                                           &attrs_nohugepage);
632         if (IS_ERR(vpu->alloc_ctx_vm)) {
633                 ret = PTR_ERR(vpu->alloc_ctx_vm);
634                 goto err_dma_contig_vm;
635         }
636
637         ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
638         if (ret) {
639                 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
640                 goto err_v4l2_dev_reg;
641         }
642
643         platform_set_drvdata(pdev, vpu);
644
645         ret = rk3288_vpu_enc_init_dummy_ctx(vpu);
646         if (ret) {
647                 dev_err(&pdev->dev, "Failed to create dummy encode context\n");
648                 goto err_dummy_enc;
649         }
650
651         /* encoder */
652         vfd = video_device_alloc();
653         if (!vfd) {
654                 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
655                 ret = -ENOMEM;
656                 goto err_enc_alloc;
657         }
658
659         vfd->fops = &rk3288_vpu_fops;
660         vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
661         vfd->release = video_device_release;
662         vfd->lock = &vpu->vpu_mutex;
663         vfd->v4l2_dev = &vpu->v4l2_dev;
664         vfd->vfl_dir = VFL_DIR_M2M;
665         snprintf(vfd->name, sizeof(vfd->name), "%s", RK3288_VPU_ENC_NAME);
666         vpu->vfd_enc = vfd;
667
668         video_set_drvdata(vfd, vpu);
669
670         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
671         if (ret) {
672                 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
673                 video_device_release(vfd);
674                 goto err_enc_reg;
675         }
676
677         v4l2_info(&vpu->v4l2_dev,
678                 "Rockchip RK3288 VPU encoder registered as /vpu/video%d\n",
679                 vfd->num);
680
681         /* decoder */
682         vfd = video_device_alloc();
683         if (!vfd) {
684                 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
685                 ret = -ENOMEM;
686                 goto err_dec_alloc;
687         }
688
689         vfd->fops = &rk3288_vpu_fops;
690         vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
691         vfd->release = video_device_release;
692         vfd->lock = &vpu->vpu_mutex;
693         vfd->v4l2_dev = &vpu->v4l2_dev;
694         vfd->vfl_dir = VFL_DIR_M2M;
695         snprintf(vfd->name, sizeof(vfd->name), "%s", RK3288_VPU_DEC_NAME);
696         vpu->vfd_dec = vfd;
697
698         video_set_drvdata(vfd, vpu);
699
700         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
701         if (ret) {
702                 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
703                 video_device_release(vfd);
704                 goto err_dec_reg;
705         }
706
707         v4l2_info(&vpu->v4l2_dev,
708                 "Rockchip RK3288 VPU decoder registered as /vpu/video%d\n",
709                 vfd->num);
710
711         vpu_debug_leave();
712
713         return 0;
714
715 err_dec_reg:
716         video_device_release(vpu->vfd_dec);
717 err_dec_alloc:
718         video_unregister_device(vpu->vfd_enc);
719 err_enc_reg:
720         video_device_release(vpu->vfd_enc);
721 err_enc_alloc:
722         rk3288_vpu_enc_free_dummy_ctx(vpu);
723 err_dummy_enc:
724         v4l2_device_unregister(&vpu->v4l2_dev);
725 err_v4l2_dev_reg:
726         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx_vm);
727 err_dma_contig_vm:
728         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx);
729 err_dma_contig:
730         rk3288_vpu_hw_remove(vpu);
731 err_hw_probe:
732         pr_debug("%s-- with error\n", __func__);
733         vpu_debug_leave();
734
735         return ret;
736 }
737
738 static int rk3288_vpu_remove(struct platform_device *pdev)
739 {
740         struct rk3288_vpu_dev *vpu = platform_get_drvdata(pdev);
741
742         vpu_debug_enter();
743
744         v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name);
745
746         /*
747          * We are safe here assuming that .remove() got called as
748          * a result of module removal, which guarantees that all
749          * contexts have been released.
750          */
751
752         video_unregister_device(vpu->vfd_dec);
753         video_unregister_device(vpu->vfd_enc);
754         rk3288_vpu_enc_free_dummy_ctx(vpu);
755         v4l2_device_unregister(&vpu->v4l2_dev);
756         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx_vm);
757         vb2_dma_contig_cleanup_ctx(vpu->alloc_ctx);
758         rk3288_vpu_hw_remove(vpu);
759
760         vpu_debug_leave();
761
762         return 0;
763 }
764
765 static struct platform_device_id vpu_driver_ids[] = {
766         { .name = "rk3288-vpu", },
767         { /* sentinel */ }
768 };
769
770 MODULE_DEVICE_TABLE(platform, vpu_driver_ids);
771
772 #ifdef CONFIG_OF
773 static const struct of_device_id of_rk3288_vpu_match[] = {
774         { .compatible = "rockchip,rk3288-vpu", },
775         { /* sentinel */ }
776 };
777 MODULE_DEVICE_TABLE(of, of_rk3288_vpu_match);
778 #endif
779
780 #ifdef CONFIG_PM_SLEEP
781 static int rk3288_vpu_suspend(struct device *dev)
782 {
783         struct rk3288_vpu_dev *vpu = dev_get_drvdata(dev);
784
785         set_bit(VPU_SUSPENDED, &vpu->state);
786         wait_event(vpu->run_wq, vpu->current_ctx == NULL);
787
788         return 0;
789 }
790
791 static int rk3288_vpu_resume(struct device *dev)
792 {
793         struct rk3288_vpu_dev *vpu = dev_get_drvdata(dev);
794
795         clear_bit(VPU_SUSPENDED, &vpu->state);
796         rk3288_vpu_try_run(vpu);
797
798         return 0;
799 }
800 #endif
801
802 static const struct dev_pm_ops rk3288_vpu_pm_ops = {
803         SET_SYSTEM_SLEEP_PM_OPS(rk3288_vpu_suspend, rk3288_vpu_resume)
804 };
805
806 static struct platform_driver rk3288_vpu_driver = {
807         .probe = rk3288_vpu_probe,
808         .remove = rk3288_vpu_remove,
809         .id_table = vpu_driver_ids,
810         .driver = {
811                    .name = RK3288_VPU_NAME,
812                    .owner = THIS_MODULE,
813                    .of_match_table = of_match_ptr(of_rk3288_vpu_match),
814                    .pm = &rk3288_vpu_pm_ops,
815         },
816 };
817 module_platform_driver(rk3288_vpu_driver);
818
819 MODULE_LICENSE("GPL v2");
820 MODULE_AUTHOR("Alpha Lin <Alpha.Lin@Rock-Chips.com>");
821 MODULE_AUTHOR("Tomasz Figa <tfiga@chromium.org>");
822 MODULE_DESCRIPTION("Rockchip RK3288 VPU codec driver");