1 From 910f74bae7c97fafd4a834b9a5e261d4ef2d9676 Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.org>
3 Date: Thu, 9 May 2019 14:30:37 +0100
4 Subject: [PATCH 552/773] drivers: char: add chardev for mmap'ing Argon control
7 Based on the gpiomem driver, allow mapping of the decoder register
8 spaces such that userspace can access control/status registers.
9 This driver is intended for use with a custom ffmpeg backend accelerator
10 prior to a v4l2 driver being written.
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
14 drivers/char/broadcom/Kconfig | 8 +
15 drivers/char/broadcom/Makefile | 1 +
16 drivers/char/broadcom/argon-mem.c | 277 ++++++++++++++++++++++++++++++
17 3 files changed, 286 insertions(+)
18 create mode 100644 drivers/char/broadcom/argon-mem.c
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -49,3 +49,11 @@ config BCM2835_SMI_DEV
23 This driver provides a character device interface (ioctl + read/write) to
24 Broadcom's Secondary Memory interface. The low-level functionality is provided
25 by the SMI driver itself.
28 + tristate "Character device driver for the Argon decoder hardware"
31 + This driver provides a character device interface for memory-map operations
32 + so userspace tools can access the control and status registers of the Argon
33 + video decoder hardware.
34 --- a/drivers/char/broadcom/Makefile
35 +++ b/drivers/char/broadcom/Makefile
36 @@ -4,3 +4,4 @@ obj-$(CONFIG_BCM_VC_SM) += vc_sm
38 obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
39 obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
40 +obj-$(CONFIG_ARGON_MEM) += argon-mem.o
42 +++ b/drivers/char/broadcom/argon-mem.c
45 + * argon-mem.c - character device access to the Argon decoder registers
47 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
48 + * register blocks such that ffmpeg plugins can access the hardware.
50 + * Jonathan Bell <jonathan@raspberrypi.org>
51 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
53 + * Redistribution and use in source and binary forms, with or without
54 + * modification, are permitted provided that the following conditions
56 + * 1. Redistributions of source code must retain the above copyright
57 + * notice, this list of conditions, and the following disclaimer,
58 + * without modification.
59 + * 2. Redistributions in binary form must reproduce the above copyright
60 + * notice, this list of conditions and the following disclaimer in the
61 + * documentation and/or other materials provided with the distribution.
62 + * 3. The names of the above-listed copyright holders may not be used
63 + * to endorse or promote products derived from this software without
64 + * specific prior written permission.
66 + * ALTERNATIVELY, this software may be distributed under the terms of the
67 + * GNU General Public License ("GPL") version 2, as published by the Free
68 + * Software Foundation.
70 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
71 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
72 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
73 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
75 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
76 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
77 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
78 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
80 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 +#include <linux/kernel.h>
84 +#include <linux/module.h>
85 +#include <linux/of.h>
86 +#include <linux/of_device.h>
87 +#include <linux/platform_device.h>
88 +#include <linux/mm.h>
89 +#include <linux/slab.h>
90 +#include <linux/cdev.h>
91 +#include <linux/pagemap.h>
92 +#include <linux/io.h>
94 +#define DRIVER_NAME "argon-mem"
95 +#define DEVICE_MINOR 0
97 +struct argon_mem_priv {
99 + struct class *class;
100 + struct cdev argon_mem_cdev;
101 + unsigned long regs_phys;
102 + unsigned long mem_window_len;
103 + struct device *dev;
107 +static int argon_mem_open(struct inode *inode, struct file *file)
109 + int dev = iminor(inode);
111 + struct argon_mem_priv *priv;
112 + if (dev != DEVICE_MINOR)
115 + priv = container_of(inode->i_cdev, struct argon_mem_priv,
119 + file->private_data = priv;
123 +static int argon_mem_release(struct inode *inode, struct file *file)
125 + int dev = iminor(inode);
128 + if (dev != DEVICE_MINOR)
134 +static const struct vm_operations_struct argon_mem_vm_ops = {
135 +#ifdef CONFIG_HAVE_IOREMAP_PROT
136 + .access = generic_access_phys
140 +static int argon_mem_mmap(struct file *file, struct vm_area_struct *vma)
142 + struct argon_mem_priv *priv;
143 + unsigned long pages;
145 + priv = file->private_data;
146 + pages = priv->regs_phys >> PAGE_SHIFT;
148 + * The address decode is far larger than the actual number of registers.
149 + * Just map the whole lot in.
151 + vma->vm_page_prot = phys_mem_access_prot(file, pages,
152 + priv->mem_window_len,
153 + vma->vm_page_prot);
154 + vma->vm_ops = &argon_mem_vm_ops;
155 + if (remap_pfn_range(vma, vma->vm_start,
157 + priv->mem_window_len,
158 + vma->vm_page_prot)) {
164 +static const struct file_operations
166 + .owner = THIS_MODULE,
167 + .open = argon_mem_open,
168 + .release = argon_mem_release,
169 + .mmap = argon_mem_mmap,
172 +static const struct of_device_id argon_mem_of_match[];
173 +static int argon_mem_probe(struct platform_device *pdev)
177 + const struct of_device_id *id;
178 + struct device *dev = &pdev->dev;
179 + struct device *argon_mem_dev;
180 + struct resource *ioresource;
181 + struct argon_mem_priv *priv;
184 + /* Allocate buffers and instance data */
186 + priv = kzalloc(sizeof(struct argon_mem_priv), GFP_KERNEL);
190 + goto failed_inst_alloc;
192 + platform_set_drvdata(pdev, priv);
195 + id = of_match_device(argon_mem_of_match, dev);
198 + priv->name = id->data;
200 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
202 + priv->regs_phys = ioresource->start;
203 + priv->mem_window_len = ioresource->end - ioresource->start;
205 + dev_err(priv->dev, "failed to get IO resource");
207 + goto failed_get_resource;
210 + /* Create character device entries */
212 + err = alloc_chrdev_region(&priv->devid,
213 + DEVICE_MINOR, 1, priv->name);
215 + dev_err(priv->dev, "unable to allocate device number");
216 + goto failed_alloc_chrdev;
218 + cdev_init(&priv->argon_mem_cdev, &argon_mem_fops);
219 + priv->argon_mem_cdev.owner = THIS_MODULE;
220 + err = cdev_add(&priv->argon_mem_cdev, priv->devid, 1);
222 + dev_err(priv->dev, "unable to register device");
223 + goto failed_cdev_add;
226 + /* Create sysfs entries */
228 + priv->class = class_create(THIS_MODULE, priv->name);
229 + ptr_err = priv->class;
230 + if (IS_ERR(ptr_err))
231 + goto failed_class_create;
233 + argon_mem_dev = device_create(priv->class, NULL,
236 + ptr_err = argon_mem_dev;
237 + if (IS_ERR(ptr_err))
238 + goto failed_device_create;
240 + dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
241 + priv->name, priv->regs_phys, priv->mem_window_len);
245 +failed_device_create:
246 + class_destroy(priv->class);
247 +failed_class_create:
248 + cdev_del(&priv->argon_mem_cdev);
249 + err = PTR_ERR(ptr_err);
251 + unregister_chrdev_region(priv->devid, 1);
252 +failed_alloc_chrdev:
253 +failed_get_resource:
256 + dev_err(priv->dev, "could not load argon_mem");
260 +static int argon_mem_remove(struct platform_device *pdev)
262 + struct device *dev = &pdev->dev;
263 + struct argon_mem_priv *priv = platform_get_drvdata(pdev);
265 + device_destroy(priv->class, priv->devid);
266 + class_destroy(priv->class);
267 + cdev_del(&priv->argon_mem_cdev);
268 + unregister_chrdev_region(priv->devid, 1);
271 + dev_info(dev, "%s driver removed - OK", priv->name);
275 +static const char argon_hevc_name[] = "argon-hevcmem";
276 +static const char argon_h264_name[] = "argon-h264mem";
277 +static const char argon_vp9_name[] = "argon-vp9mem";
278 +static const char argon_intc_name[] = "argon-intcmem";
280 +static const struct of_device_id argon_mem_of_match[] = {
282 + .compatible = "raspberrypi,argon-hevc-decoder",
283 + .data = &argon_hevc_name,
286 + .compatible = "raspberrypi,argon-h264-decoder",
287 + .data = &argon_h264_name,
290 + .compatible = "raspberrypi,argon-vp9-decoder",
291 + .data = &argon_vp9_name,
293 + /* The "intc" is included as this block of hardware contains the
294 + * "frame done" status flags.
297 + .compatible = "raspberrypi,argon-local-intc",
298 + .data = &argon_intc_name,
300 + { /* sentinel */ },
303 +MODULE_DEVICE_TABLE(of, argon_mem_of_match);
305 +static struct platform_driver argon_mem_driver = {
306 + .probe = argon_mem_probe,
307 + .remove = argon_mem_remove,
309 + .name = DRIVER_NAME,
310 + .owner = THIS_MODULE,
311 + .of_match_table = argon_mem_of_match,
315 +module_platform_driver(argon_mem_driver);
317 +MODULE_ALIAS("platform:argon-mem");
318 +MODULE_LICENSE("GPL");
319 +MODULE_DESCRIPTION("Driver for accessing Argon decoder registers from userspace");
320 +MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");