-
Notifications
You must be signed in to change notification settings - Fork 134
/
0055-VIMs-add-gpiomem-support.patch
427 lines (417 loc) · 12.5 KB
/
0055-VIMs-add-gpiomem-support.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
From 0c998d213c23664f632657308fdeea90960b805b Mon Sep 17 00:00:00 2001
From: Nick Xie <nick@khadas.com>
Date: Sat, 15 May 2021 16:39:23 +0800
Subject: [PATCH 55/67] VIMs: add gpiomem support
Signed-off-by: Nick Xie <nick@khadas.com>
---
.../amlogic/meson-gxl-s905x-khadas-vim.dts | 14 +
.../dts/amlogic/meson-gxm-khadas-vim2.dts | 14 +
.../boot/dts/amlogic/meson-khadas-vim3.dtsi | 14 +
drivers/char/Kconfig | 9 +
drivers/char/Makefile | 1 +
drivers/char/aml-gpiomem.c | 298 ++++++++++++++++++
6 files changed, 350 insertions(+)
create mode 100644 drivers/char/aml-gpiomem.c
diff --git a/arch/arm64/boot/dts/amlogic/meson-gxl-s905x-khadas-vim.dts b/arch/arm64/boot/dts/amlogic/meson-gxl-s905x-khadas-vim.dts
index 02473c9e35a6..60d4d0e6aaa4 100644
--- a/arch/arm64/boot/dts/amlogic/meson-gxl-s905x-khadas-vim.dts
+++ b/arch/arm64/boot/dts/amlogic/meson-gxl-s905x-khadas-vim.dts
@@ -44,6 +44,20 @@ power-button {
};
};
+ gpiomem {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xda834000 0x0 0x1000>;
+ dev_name = "gpiomem";
+ status = "okay";
+ };
+
+ gpiomem_ao {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xc8100000 0x0 0x1000>;
+ dev_name = "gpiomem-ao";
+ status = "okay";
+ };
+
led-controller {
compatible = "pwm-leds";
diff --git a/arch/arm64/boot/dts/amlogic/meson-gxm-khadas-vim2.dts b/arch/arm64/boot/dts/amlogic/meson-gxm-khadas-vim2.dts
index f3c5c4a0f160..012fc6723596 100644
--- a/arch/arm64/boot/dts/amlogic/meson-gxm-khadas-vim2.dts
+++ b/arch/arm64/boot/dts/amlogic/meson-gxm-khadas-vim2.dts
@@ -71,6 +71,20 @@ power-button {
};
};
+ gpiomem {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xda834000 0x0 0x1000>;
+ dev_name = "gpiomem";
+ status = "okay";
+ };
+
+ gpiomem_ao {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xc8100000 0x0 0x1000>;
+ dev_name = "gpiomem-ao";
+ status = "okay";
+ };
+
hdmi-connector {
compatible = "hdmi-connector";
type = "a";
diff --git a/arch/arm64/boot/dts/amlogic/meson-khadas-vim3.dtsi b/arch/arm64/boot/dts/amlogic/meson-khadas-vim3.dtsi
index c84bed336d06..7fa1dc10da7e 100644
--- a/arch/arm64/boot/dts/amlogic/meson-khadas-vim3.dtsi
+++ b/arch/arm64/boot/dts/amlogic/meson-khadas-vim3.dtsi
@@ -40,6 +40,20 @@ button-function {
};
};
+ gpiomem {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xff634000 0x0 0x1000>;
+ dev_name = "gpiomem";
+ status = "okay";
+ };
+
+ gpiomem_ao {
+ compatible = "amlogic, gpiomem";
+ reg = <0x0 0xff800000 0x0 0x1000>;
+ dev_name = "gpiomem-ao";
+ status = "okay";
+ };
+
leds {
compatible = "gpio-leds";
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index d229a2d0c017..97890e9697b7 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -471,6 +471,15 @@ config ADI
and SSM (Silicon Secured Memory). Intended consumers of this
driver include crash and makedumpfile.
+config AMLOGIC_GPIOMEM
+ tristate "/dev/gpiomem rootless GPIO access via mmap() on the Amlogic"
+ default y
+ help
+ Provides users with root-free access to the GPIO registers
+ on Meson platform. Calling mmap(/dev/gpiomem) will map the GPIO
+ register page to the user's pointer. This drvier can allow to access
+ gpio memory area in user account.
+
endmenu
config RANDOM_TRUST_CPU
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index ffce287ef415..063431b3d763 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -47,3 +47,4 @@ obj-$(CONFIG_PS3_FLASH) += ps3flash.o
obj-$(CONFIG_XILLYBUS) += xillybus/
obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
obj-$(CONFIG_ADI) += adi.o
+obj-$(CONFIG_AMLOGIC_GPIOMEM) += aml-gpiomem.o
diff --git a/drivers/char/aml-gpiomem.c b/drivers/char/aml-gpiomem.c
new file mode 100644
index 000000000000..5c71560acc69
--- /dev/null
+++ b/drivers/char/aml-gpiomem.c
@@ -0,0 +1,298 @@
+/**
+ * GPIO memory device driver
+ *
+ * Creates a chardev /dev/gpiomem which will provide user access to
+ * the Meson's GPIO registers when it is mmap()'d.
+ * No longer need root for user GPIO access, but without relaxing permissions
+ * on /dev/mem.
+ *
+ * Copyright (c) 2019 Wesion Co., Ltd.
+ *
+ * This driver is based on bcm2835-gpiomem.c:
+ * Written by Luke Wren <luke@raspberrypi.org>
+ * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions, and the following disclaimer,
+ * without modification.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The names of the above-listed copyright holders may not be used
+ * to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * ALTERNATIVELY, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2, as published by the Free
+ * Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/cdev.h>
+#include <linux/pagemap.h>
+#include <linux/io.h>
+
+#define DEVICE_NAME "aml-gpiomem"
+#define DRIVER_NAME "gpiomem-aml"
+#define DEVICE_MINOR 0
+
+#define DEVICE_NUM_MAX 2
+
+struct aml_gpiomem_instance {
+ unsigned long gpio_regs_phys;
+ struct device *dev;
+ char dev_name[32];
+ int major;
+};
+
+static struct cdev aml_gpiomem_cdev;
+static dev_t aml_gpiomem_devid;
+static struct class *aml_gpiomem_class;
+static struct device *aml_gpiomem_dev;
+//static struct aml_gpiomem_instance *inst;
+static struct aml_gpiomem_instance *gpiomem_instances[DEVICE_NUM_MAX];
+static int instance_num = 0;
+
+/****************************************************************************
+*
+* GPIO mem chardev file ops
+*
+***************************************************************************/
+
+static int aml_gpiomem_open(struct inode *inode, struct file *file)
+{
+ int dev = iminor(inode);
+ int major = imajor(inode);
+ int ret = 0;
+ int i = 0;
+ struct aml_gpiomem_instance *inst = NULL;
+
+ for (i=0; i<DEVICE_NUM_MAX; i++) {
+ if (major == gpiomem_instances[i]->major) {
+ inst = gpiomem_instances[i];
+ file->private_data = gpiomem_instances[i];
+ }
+ }
+
+ if (!inst)
+ return -ENXIO;
+
+ if (dev != DEVICE_MINOR) {
+ dev_err(inst->dev, "Unknown minor device: %d", dev);
+ ret = -ENXIO;
+ }
+ return ret;
+}
+
+static int aml_gpiomem_release(struct inode *inode, struct file *file)
+{
+ int dev = iminor(inode);
+ int ret = 0;
+ struct aml_gpiomem_instance *inst = file->private_data;
+
+ if (dev != DEVICE_MINOR) {
+ dev_err(inst->dev, "Unknown minor device %d", dev);
+ ret = -ENXIO;
+ }
+ return ret;
+}
+
+static const struct vm_operations_struct aml_gpiomem_vm_ops = {
+#ifdef CONFIG_HAVE_IOREMAP_PROT
+ .access = generic_access_phys
+#endif
+};
+
+static int aml_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
+{
+ /* Ignore what the user says - they're getting the GPIO regs
+ whether they like it or not! */
+ struct aml_gpiomem_instance *inst = file->private_data;
+ unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
+
+ vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
+ PAGE_SIZE,
+ vma->vm_page_prot);
+ vma->vm_ops = &aml_gpiomem_vm_ops;
+ if (remap_pfn_range(vma, vma->vm_start,
+ gpio_page,
+ PAGE_SIZE,
+ vma->vm_page_prot)) {
+ return -EAGAIN;
+ }
+ return 0;
+}
+
+static const struct file_operations
+aml_gpiomem_fops = {
+ .owner = THIS_MODULE,
+ .open = aml_gpiomem_open,
+ .release = aml_gpiomem_release,
+ .mmap = aml_gpiomem_mmap,
+};
+
+
+ /****************************************************************************
+*
+* Probe and remove functions
+*
+***************************************************************************/
+
+
+static int aml_gpiomem_probe(struct platform_device *pdev)
+{
+ int err;
+ void *ptr_err;
+ struct device *dev = &pdev->dev;
+ struct resource *ioresource;
+ const char *str;
+ char tmp[64];
+ struct aml_gpiomem_instance *inst = NULL;
+
+ /* Allocate buffers and instance data */
+ inst = kzalloc(sizeof(struct aml_gpiomem_instance), GFP_KERNEL);
+
+ if (!inst) {
+ err = -ENOMEM;
+ goto failed_inst_alloc;
+ }
+
+ inst->dev = dev;
+
+ platform_set_drvdata(pdev, inst);
+
+ ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (ioresource) {
+ inst->gpio_regs_phys = ioresource->start;
+ } else {
+ dev_err(inst->dev, "failed to get IO resource");
+ err = -ENOENT;
+ goto failed_get_resource;
+ }
+
+ err = of_property_read_string(dev->of_node, "dev_name", &str);
+ if (err) {
+ dev_err(inst->dev, "failed to get device name\n");
+ err = -ENOENT;
+ goto failed_get_device_name;
+ }
+ strncpy(inst->dev_name, str, 32);
+ inst->dev_name[31] = '\0';
+
+ /* Create character device entries */
+ sprintf(tmp, "aml-%s", inst->dev_name);
+ err = alloc_chrdev_region(&aml_gpiomem_devid,
+ DEVICE_MINOR, 1, tmp);
+ if (err != 0) {
+ dev_err(inst->dev, "unable to allocate device number");
+ goto failed_alloc_chrdev;
+ }
+ cdev_init(&aml_gpiomem_cdev, &aml_gpiomem_fops);
+ aml_gpiomem_cdev.owner = THIS_MODULE;
+ err = cdev_add(&aml_gpiomem_cdev, aml_gpiomem_devid, 1);
+ if (err != 0) {
+ dev_err(inst->dev, "unable to register device");
+ goto failed_cdev_add;
+ }
+
+ /* Create sysfs entries */
+ aml_gpiomem_class = class_create(THIS_MODULE, tmp);
+ ptr_err = aml_gpiomem_class;
+ if (IS_ERR(ptr_err))
+ goto failed_class_create;
+
+ aml_gpiomem_dev = device_create(aml_gpiomem_class, NULL,
+ aml_gpiomem_devid, NULL,
+ inst->dev_name);
+ ptr_err = aml_gpiomem_dev;
+ if (IS_ERR(ptr_err))
+ goto failed_device_create;
+
+ inst->major = MAJOR(aml_gpiomem_devid);
+ gpiomem_instances[instance_num++] = inst;
+
+ dev_info(inst->dev, "Initialised: Registers at 0x%08lx\n",
+ inst->gpio_regs_phys);
+
+ return 0;
+
+failed_device_create:
+ class_destroy(aml_gpiomem_class);
+failed_class_create:
+ cdev_del(&aml_gpiomem_cdev);
+ err = PTR_ERR(ptr_err);
+failed_cdev_add:
+ unregister_chrdev_region(aml_gpiomem_devid, 1);
+failed_alloc_chrdev:
+failed_get_device_name:
+failed_get_resource:
+ kfree(inst);
+failed_inst_alloc:
+ dev_err(inst->dev, "could not load aml_gpiomem");
+ return err;
+}
+
+static int aml_gpiomem_remove(struct platform_device *pdev)
+{
+ struct aml_gpiomem_instance *inst = platform_get_drvdata(pdev);
+ struct device *dev = inst->dev;
+
+ kfree(inst);
+ device_destroy(aml_gpiomem_class, aml_gpiomem_devid);
+ class_destroy(aml_gpiomem_class);
+ cdev_del(&aml_gpiomem_cdev);
+ unregister_chrdev_region(aml_gpiomem_devid, 1);
+
+ dev_info(dev, "GPIO mem driver removed - OK");
+ return 0;
+}
+
+ /****************************************************************************
+*
+* Register the driver with device tree
+*
+***************************************************************************/
+
+static const struct of_device_id aml_gpiomem_of_match[] = {
+ {.compatible = "amlogic, gpiomem",},
+ { /* sentinel */ },
+};
+
+MODULE_DEVICE_TABLE(of, aml_gpiomem_of_match);
+
+static struct platform_driver aml_gpiomem_driver = {
+ .probe = aml_gpiomem_probe,
+ .remove = aml_gpiomem_remove,
+ .driver = {
+ .name = DRIVER_NAME,
+ .owner = THIS_MODULE,
+ .of_match_table = aml_gpiomem_of_match,
+ },
+};
+
+module_platform_driver(aml_gpiomem_driver);
+
+MODULE_ALIAS("platform:gpiomem-aml");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
+MODULE_AUTHOR("Nick Xie <nick@khadas.com>");
--
2.25.1