fb358aa45f740934cfdc69024ac5990dda709b6d
[oweals/openwrt.git] /
1 From 0be0d6439128366a8d2ac0afaf88f19209171e51 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] drivers: char: add chardev for mmap'ing Argon control
5  registers
6
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.
11
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
13 ---
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
19
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.
26 +
27 +config ARGON_MEM
28 +       tristate "Character device driver for the Argon decoder hardware"
29 +       default n
30 +       help
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
37  
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
41 --- /dev/null
42 +++ b/drivers/char/broadcom/argon-mem.c
43 @@ -0,0 +1,277 @@
44 +/**
45 + * argon-mem.c - character device access to the Argon decoder registers
46 + *
47 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
48 + * register blocks such that ffmpeg plugins can access the hardware.
49 + *
50 + * Jonathan Bell <jonathan@raspberrypi.org>
51 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
52 + *
53 + * Redistribution and use in source and binary forms, with or without
54 + * modification, are permitted provided that the following conditions
55 + * are met:
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.
65 + *
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.
69 + *
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.
81 + */
82 +
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>
93 +
94 +#define DRIVER_NAME "argon-mem"
95 +#define DEVICE_MINOR 0
96 +
97 +struct argon_mem_priv {
98 +       dev_t devid;
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;
104 +       const char *name;
105 +};
106 +
107 +static int argon_mem_open(struct inode *inode, struct file *file)
108 +{
109 +       int dev = iminor(inode);
110 +       int ret = 0;
111 +       struct argon_mem_priv *priv;
112 +       if (dev != DEVICE_MINOR)
113 +               ret = -ENXIO;
114 +
115 +       priv = container_of(inode->i_cdev, struct argon_mem_priv,
116 +                               argon_mem_cdev);
117 +       if (!priv)
118 +               return -EINVAL;
119 +       file->private_data = priv;
120 +       return ret;
121 +}
122 +
123 +static int argon_mem_release(struct inode *inode, struct file *file)
124 +{
125 +       int dev = iminor(inode);
126 +       int ret = 0;
127 +
128 +       if (dev != DEVICE_MINOR)
129 +               ret = -ENXIO;
130 +
131 +       return ret;
132 +}
133 +
134 +static const struct vm_operations_struct argon_mem_vm_ops = {
135 +#ifdef CONFIG_HAVE_IOREMAP_PROT
136 +       .access = generic_access_phys
137 +#endif
138 +};
139 +
140 +static int argon_mem_mmap(struct file *file, struct vm_area_struct *vma)
141 +{
142 +       struct argon_mem_priv *priv;
143 +       unsigned long pages;
144 +
145 +       priv = file->private_data;
146 +       pages = priv->regs_phys >> PAGE_SHIFT;
147 +       /*
148 +        * The address decode is far larger than the actual number of registers.
149 +        * Just map the whole lot in.
150 +        */
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,
156 +                       pages,
157 +                       priv->mem_window_len,
158 +                       vma->vm_page_prot)) {
159 +               return -EAGAIN;
160 +       }
161 +       return 0;
162 +}
163 +
164 +static const struct file_operations
165 +argon_mem_fops = {
166 +       .owner = THIS_MODULE,
167 +       .open = argon_mem_open,
168 +       .release = argon_mem_release,
169 +       .mmap = argon_mem_mmap,
170 +};
171 +
172 +static const struct of_device_id argon_mem_of_match[];
173 +static int argon_mem_probe(struct platform_device *pdev)
174 +{
175 +       int err;
176 +       void *ptr_err;
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;
182 +
183 +
184 +       /* Allocate buffers and instance data */
185 +
186 +       priv = kzalloc(sizeof(struct argon_mem_priv), GFP_KERNEL);
187 +
188 +       if (!priv) {
189 +               err = -ENOMEM;
190 +               goto failed_inst_alloc;
191 +       }
192 +       platform_set_drvdata(pdev, priv);
193 +
194 +       priv->dev = dev;
195 +       id = of_match_device(argon_mem_of_match, dev);
196 +       if (!id)
197 +               return -EINVAL;
198 +       priv->name = id->data;
199 +
200 +       ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 +       if (ioresource) {
202 +               priv->regs_phys = ioresource->start;
203 +               priv->mem_window_len = ioresource->end - ioresource->start;
204 +       } else {
205 +               dev_err(priv->dev, "failed to get IO resource");
206 +               err = -ENOENT;
207 +               goto failed_get_resource;
208 +       }
209 +
210 +       /* Create character device entries */
211 +
212 +       err = alloc_chrdev_region(&priv->devid,
213 +                                 DEVICE_MINOR, 1, priv->name);
214 +       if (err != 0) {
215 +               dev_err(priv->dev, "unable to allocate device number");
216 +               goto failed_alloc_chrdev;
217 +       }
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);
221 +       if (err != 0) {
222 +               dev_err(priv->dev, "unable to register device");
223 +               goto failed_cdev_add;
224 +       }
225 +
226 +       /* Create sysfs entries */
227 +
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;
232 +
233 +       argon_mem_dev = device_create(priv->class, NULL,
234 +                                       priv->devid, NULL,
235 +                                       priv->name);
236 +       ptr_err = argon_mem_dev;
237 +       if (IS_ERR(ptr_err))
238 +               goto failed_device_create;
239 +
240 +       dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
241 +               priv->name, priv->regs_phys, priv->mem_window_len);
242 +
243 +       return 0;
244 +
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);
250 +failed_cdev_add:
251 +       unregister_chrdev_region(priv->devid, 1);
252 +failed_alloc_chrdev:
253 +failed_get_resource:
254 +       kfree(priv);
255 +failed_inst_alloc:
256 +       dev_err(priv->dev, "could not load argon_mem");
257 +       return err;
258 +}
259 +
260 +static int argon_mem_remove(struct platform_device *pdev)
261 +{
262 +       struct device *dev = &pdev->dev;
263 +       struct argon_mem_priv *priv = platform_get_drvdata(pdev);
264 +
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);
269 +       kfree(priv);
270 +
271 +       dev_info(dev, "%s driver removed - OK", priv->name);
272 +       return 0;
273 +}
274 +
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";
279 +
280 +static const struct of_device_id argon_mem_of_match[] = {
281 +       {
282 +               .compatible = "raspberrypi,argon-hevc-decoder",
283 +               .data = &argon_hevc_name,
284 +       },
285 +       {
286 +               .compatible = "raspberrypi,argon-h264-decoder",
287 +               .data = &argon_h264_name,
288 +       },
289 +       {
290 +               .compatible = "raspberrypi,argon-vp9-decoder",
291 +               .data = &argon_vp9_name,
292 +       },
293 +       /* The "intc" is included as this block of hardware contains the
294 +        * "frame done" status flags.
295 +        */
296 +       {
297 +               .compatible = "raspberrypi,argon-local-intc",
298 +               .data = &argon_intc_name,
299 +       },
300 +       { /* sentinel */ },
301 +};
302 +
303 +MODULE_DEVICE_TABLE(of, argon_mem_of_match);
304 +
305 +static struct platform_driver argon_mem_driver = {
306 +       .probe = argon_mem_probe,
307 +       .remove = argon_mem_remove,
308 +       .driver = {
309 +                  .name = DRIVER_NAME,
310 +                  .owner = THIS_MODULE,
311 +                  .of_match_table = argon_mem_of_match,
312 +                  },
313 +};
314 +
315 +module_platform_driver(argon_mem_driver);
316 +
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>");