1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * rpivid-mem.c - character device access to the RPiVid decoder registers
5 * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
6 * register blocks such that ffmpeg plugins can access the hardware.
8 * Jonathan Bell <jonathan@raspberrypi.org>
9 * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The names of the above-listed copyright holders may not be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * ALTERNATIVELY, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2, as published by the Free
26 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <linux/kernel.h>
42 #include <linux/module.h>
44 #include <linux/of_device.h>
45 #include <linux/platform_device.h>
47 #include <linux/slab.h>
48 #include <linux/cdev.h>
49 #include <linux/pagemap.h>
52 #define DRIVER_NAME "rpivid-mem"
53 #define DEVICE_MINOR 0
55 struct rpivid_mem_priv {
58 struct cdev rpivid_mem_cdev;
59 unsigned long regs_phys;
60 unsigned long mem_window_len;
65 static int rpivid_mem_open(struct inode *inode, struct file *file)
67 int dev = iminor(inode);
69 struct rpivid_mem_priv *priv;
71 if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
74 priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
78 file->private_data = priv;
82 static int rpivid_mem_release(struct inode *inode, struct file *file)
84 int dev = iminor(inode);
87 if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
93 static const struct vm_operations_struct rpivid_mem_vm_ops = {
94 #ifdef CONFIG_HAVE_IOREMAP_PROT
95 .access = generic_access_phys
99 static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
101 struct rpivid_mem_priv *priv;
105 priv = file->private_data;
106 pages = priv->regs_phys >> PAGE_SHIFT;
108 * The address decode is far larger than the actual number of registers.
109 * Just map the whole lot in.
111 len = min(vma->vm_end - vma->vm_start, priv->mem_window_len);
112 vma->vm_page_prot = phys_mem_access_prot(file, pages, len,
114 vma->vm_ops = &rpivid_mem_vm_ops;
115 if (remap_pfn_range(vma, vma->vm_start,
117 vma->vm_page_prot)) {
123 static const struct file_operations
125 .owner = THIS_MODULE,
126 .open = rpivid_mem_open,
127 .release = rpivid_mem_release,
128 .mmap = rpivid_mem_mmap,
131 static const struct of_device_id rpivid_mem_of_match[];
132 static int rpivid_mem_probe(struct platform_device *pdev)
135 const struct of_device_id *id;
136 struct device *dev = &pdev->dev;
137 struct resource *ioresource;
138 struct rpivid_mem_priv *priv;
140 /* Allocate buffers and instance data */
142 priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
146 goto failed_inst_alloc;
148 platform_set_drvdata(pdev, priv);
151 id = of_match_device(rpivid_mem_of_match, dev);
154 priv->name = id->data;
156 ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
158 priv->regs_phys = ioresource->start;
159 priv->mem_window_len = (ioresource->end + 1) - ioresource->start;
161 dev_err(priv->dev, "failed to get IO resource");
163 goto failed_get_resource;
166 /* Create character device entries */
168 err = alloc_chrdev_region(&priv->devid,
169 DEVICE_MINOR, 2, priv->name);
171 dev_err(priv->dev, "unable to allocate device number");
172 goto failed_alloc_chrdev;
174 cdev_init(&priv->rpivid_mem_cdev, &rpivid_mem_fops);
175 priv->rpivid_mem_cdev.owner = THIS_MODULE;
176 err = cdev_add(&priv->rpivid_mem_cdev, priv->devid, 2);
178 dev_err(priv->dev, "unable to register device");
179 goto failed_cdev_add;
182 /* Create sysfs entries */
184 priv->class = class_create(THIS_MODULE, priv->name);
185 if (IS_ERR(priv->class)) {
186 err = PTR_ERR(priv->class);
187 goto failed_class_create;
190 dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
193 goto failed_device_create;
196 dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
197 priv->name, priv->regs_phys, priv->mem_window_len);
201 failed_device_create:
202 class_destroy(priv->class);
204 cdev_del(&priv->rpivid_mem_cdev);
206 unregister_chrdev_region(priv->devid, 1);
211 dev_err(&pdev->dev, "could not load rpivid_mem");
215 static int rpivid_mem_remove(struct platform_device *pdev)
217 struct device *dev = &pdev->dev;
218 struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
220 device_destroy(priv->class, priv->devid);
221 class_destroy(priv->class);
222 cdev_del(&priv->rpivid_mem_cdev);
223 unregister_chrdev_region(priv->devid, 1);
226 dev_info(dev, "%s driver removed - OK", priv->name);
230 static const struct of_device_id rpivid_mem_of_match[] = {
232 .compatible = "raspberrypi,rpivid-hevc-decoder",
233 .data = "rpivid-hevcmem",
236 .compatible = "raspberrypi,rpivid-h264-decoder",
237 .data = "rpivid-h264mem",
240 .compatible = "raspberrypi,rpivid-vp9-decoder",
241 .data = "rpivid-vp9mem",
243 /* The "intc" is included as this block of hardware contains the
244 * "frame done" status flags.
247 .compatible = "raspberrypi,rpivid-local-intc",
248 .data = "rpivid-intcmem",
253 MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
255 static struct platform_driver rpivid_mem_driver = {
256 .probe = rpivid_mem_probe,
257 .remove = rpivid_mem_remove,
260 .owner = THIS_MODULE,
261 .of_match_table = rpivid_mem_of_match,
265 module_platform_driver(rpivid_mem_driver);
267 MODULE_ALIAS("platform:rpivid-mem");
268 MODULE_LICENSE("GPL");
269 MODULE_DESCRIPTION("Driver for accessing RPiVid decoder registers from userspace");
270 MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");