gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / platform / s5p-mfc / s5p_mfc_opr.c
blobbb65671eea91cd7e08626e7d2269f14f9a1c771c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/media/platform/s5p-mfc/s5p_mfc_opr.c
5 * Samsung MFC (Multi Function Codec - FIMV) driver
6 * This file contains hw related functions.
8 * Kamil Debski, Copyright (c) 2012 Samsung Electronics Co., Ltd.
9 * http://www.samsung.com/
12 #include "s5p_mfc_debug.h"
13 #include "s5p_mfc_opr.h"
14 #include "s5p_mfc_opr_v5.h"
15 #include "s5p_mfc_opr_v6.h"
17 static struct s5p_mfc_hw_ops *s5p_mfc_ops;
19 void s5p_mfc_init_hw_ops(struct s5p_mfc_dev *dev)
21 if (IS_MFCV6_PLUS(dev)) {
22 s5p_mfc_ops = s5p_mfc_init_hw_ops_v6();
23 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START_V6;
24 } else {
25 s5p_mfc_ops = s5p_mfc_init_hw_ops_v5();
26 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START;
28 dev->mfc_ops = s5p_mfc_ops;
31 void s5p_mfc_init_regs(struct s5p_mfc_dev *dev)
33 if (IS_MFCV6_PLUS(dev))
34 dev->mfc_regs = s5p_mfc_init_regs_v6_plus(dev);
37 int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
38 struct s5p_mfc_priv_buf *b)
40 unsigned int bits = dev->mem_size >> PAGE_SHIFT;
41 unsigned int count = b->size >> PAGE_SHIFT;
42 unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1;
43 unsigned int start, offset;
45 mfc_debug(3, "Allocating priv: %zu\n", b->size);
47 if (dev->mem_virt) {
48 start = bitmap_find_next_zero_area(dev->mem_bitmap, bits, 0, count, align);
49 if (start > bits)
50 goto no_mem;
52 bitmap_set(dev->mem_bitmap, start, count);
53 offset = start << PAGE_SHIFT;
54 b->virt = dev->mem_virt + offset;
55 b->dma = dev->mem_base + offset;
56 } else {
57 struct device *mem_dev = dev->mem_dev[mem_ctx];
58 dma_addr_t base = dev->dma_base[mem_ctx];
60 b->ctx = mem_ctx;
61 b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
62 if (!b->virt)
63 goto no_mem;
64 if (b->dma < base) {
65 mfc_err("Invalid memory configuration - buffer (%pad) is below base memory address(%pad)\n",
66 &b->dma, &base);
67 dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
68 return -ENOMEM;
72 mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
73 return 0;
74 no_mem:
75 mfc_err("Allocating private buffer of size %zu failed\n", b->size);
76 return -ENOMEM;
79 int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
80 struct s5p_mfc_priv_buf *b)
82 struct device *mem_dev = dev->mem_dev[mem_ctx];
84 mfc_debug(3, "Allocating generic buf: %zu\n", b->size);
86 b->ctx = mem_ctx;
87 b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
88 if (!b->virt)
89 goto no_mem;
91 mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
92 return 0;
93 no_mem:
94 mfc_err("Allocating generic buffer of size %zu failed\n", b->size);
95 return -ENOMEM;
98 void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev,
99 struct s5p_mfc_priv_buf *b)
101 if (dev->mem_virt) {
102 unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT;
103 unsigned int count = b->size >> PAGE_SHIFT;
105 bitmap_clear(dev->mem_bitmap, start, count);
106 } else {
107 struct device *mem_dev = dev->mem_dev[b->ctx];
109 dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
111 b->virt = NULL;
112 b->dma = 0;
113 b->size = 0;
116 void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev,
117 struct s5p_mfc_priv_buf *b)
119 struct device *mem_dev = dev->mem_dev[b->ctx];
120 dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
121 b->virt = NULL;
122 b->dma = 0;
123 b->size = 0;