Files
build/patch/kernel/archive/meson64-6.1/driver-amlgpio-mem.patch
Patrick Yavitz 40530ee344 Improve Meson64 Support: ODROID
Backport DTS/DTSI changes from linux-6.4.y to 6.1.y
Add meson64-reboot driver to all boards
Add board: ODROID N2L
Add uart_A uart_AO_B uart_B uart_C where appropriate
U-Boot v2023.07.02: ODROID N2/N2L/N2Plus/C4

Meson64-reboot driver: (source: tobetter)
While the current meson64-reboot driver is cleaner and doesn't
depend on modding other kernel sources, its functionality leaves
much to be desired. One example can be found in the ODROID C4.
Using the current driver, the board will not properly power off,
leaving the POWER LED still on. The new driver powers off the unit
completely.

Fan control: (ODROID N2L/N2PLus)
Added service and script for controlling the trip point.
fanctrl: arguments: 65 55 45 35 25 menu run

                              ┌──┤ Fan Control ├──┐
                              │                   │
                              │    6) 65°C        │
                              │    5) 55°C        │
                              │    4) 45°C        │
                              │    3) 35°C        │
                              │    2) 25°C        │
                              │    E) Exit ..     │
                              │                   │
                              │                   │
                              │      <Ok>         │
                              │                   │
                              └───────────────────┘

NOTES: (N2L/HC4): I do not own the units so I can't run tests.

Signed-off-by: Patrick Yavitz <pyavitz@xxxxx.com>
2023-08-20 19:20:06 +02:00

336 lines
10 KiB
Diff

From fd791cc533cd31706826a640ca2012b9fda32f91 Mon Sep 17 00:00:00 2001
From: Patrick Yavitz <pyavitz@xxxxx.com>
Date: Tue, 25 Jul 2023 12:58:44 -0400
Subject: [PATCH] amlogic gpio memory device driver
Provides users with root-free access to the GPIO registers
on Meson g12 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.
Signed-off-by: Patrick Yavitz <pyavitz@xxxxx.com>
---
drivers/char/Kconfig | 9 ++
drivers/char/Makefile | 2 +
drivers/char/aml-gpiomem.c | 280 +++++++++++++++++++++++++++++++++++++
3 files changed, 291 insertions(+)
create mode 100644 drivers/char/aml-gpiomem.c
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index 0f378d29dab0..7125a7b47f73 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -459,4 +459,13 @@ config RANDOM_TRUST_BOOTLOADER
believe its RNG facilities may be faulty. This may also be configured
at boot time with "random.trust_bootloader=on/off".
+config AMLOGIC_GPIOMEM
+ tristate "/dev/gpiomem rootless GPIO access via mmap() on the Amlogic"
+ default m
+ help
+ Provides users with root-free access to the GPIO registers
+ on Meson g12 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
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index 1b35d1724565..d223ef86c152 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -45,3 +45,5 @@ obj-$(CONFIG_PS3_FLASH) += ps3flash.o
obj-$(CONFIG_XILLYBUS_CLASS) += 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..b0d1df7ec1e4
--- /dev/null
+++ b/drivers/char/aml-gpiomem.c
@@ -0,0 +1,280 @@
+/*
+ * linux/drivers/char/aml-gpiomem.c
+ *
+ * GPIO memory device driver
+ *
+ * Creates a chardev /dev/gpiomem which will provide user access to
+ * the Meson g12 GPIO registers when it is mmap()'d.
+ * No longer need root for user GPIO access, but without relaxing permissions
+ * on /dev/mem.
+ *
+ * Copyright (c) 2017 Hardkernel Co., Ltd.
+ *
+ * This driver is based on bcm2835-gpiomem.c in Raspberrypi's linux kernel 4.4:
+ * 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>
+#include <linux/of.h>
+#include <asm/io.h>
+
+#define DEVICE_NAME "aml-gpiomem"
+#define DRIVER_NAME "gpiomem-aml"
+#define DEVICE_MINOR 0
+
+struct regs_phys {
+ unsigned long start;
+ unsigned long end;
+};
+
+struct aml_gpiomem_instance {
+ struct regs_phys gpio_regs_phys[32];
+ int gpio_area_count;
+ struct device *dev;
+};
+
+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 int aml_gpiomem_open(struct inode *inode, struct file *file)
+{
+ int dev = iminor(inode);
+ int ret = 0;
+
+ dev_info(inst->dev, "gpiomem device opened.");
+
+ 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;
+
+ 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)
+{
+ int gpio_area = 0;
+ unsigned long start = vma->vm_pgoff << PAGE_SHIFT;
+ unsigned long end = start + vma->vm_end - vma->vm_start;
+
+ while (gpio_area < inst->gpio_area_count) {
+ if ((inst->gpio_regs_phys[gpio_area].start >= start) &&
+ (inst->gpio_regs_phys[gpio_area].end <= end))
+ goto found;
+ gpio_area++;
+ }
+
+ return -EACCES;
+
+found:
+ vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ vma->vm_page_prot);
+
+ vma->vm_ops = &aml_gpiomem_vm_ops;
+
+ if (remap_pfn_range(vma, vma->vm_start,
+ vma->vm_pgoff,
+ vma->vm_end - vma->vm_start,
+ 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,
+};
+
+static int aml_gpiomem_probe(struct platform_device *pdev)
+{
+ int err = 0;
+ struct device *dev = &pdev->dev;
+ struct device_node *np = dev->of_node;
+ struct resource *res = NULL;
+ int i = 0;
+
+ /* 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;
+ inst->gpio_area_count = of_property_count_elems_of_size(np, "reg",
+ sizeof(u32)) / 4;
+
+ if (inst->gpio_area_count > 32 || inst->gpio_area_count <= 0) {
+ dev_err(inst->dev, "failed to get gpio register area.");
+ err = -EINVAL;
+ goto failed_inst_alloc;
+ }
+
+ dev_info(inst->dev, "Initialised: GPIO register area is %d",
+ inst->gpio_area_count);
+
+ for (i = 0; i < inst->gpio_area_count; ++i) {
+ res = platform_get_resource(pdev, IORESOURCE_MEM, i);
+ if (res) {
+ inst->gpio_regs_phys[i].start = res->start;
+ inst->gpio_regs_phys[i].end = res->end;
+ } else {
+ dev_err(inst->dev, "failed to get IO resource area %d", i);
+ err = -ENOENT;
+ goto failed_get_resource;
+ }
+ }
+
+ /* Create character device entries */
+ err = alloc_chrdev_region(&aml_gpiomem_devid,
+ DEVICE_MINOR, 1, DEVICE_NAME);
+ 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, DEVICE_NAME);
+ err = IS_ERR(aml_gpiomem_class);
+ if (err)
+ goto failed_class_create;
+
+ aml_gpiomem_dev = device_create(aml_gpiomem_class, NULL,
+ aml_gpiomem_devid, NULL,
+ "gpiomem");
+ err = IS_ERR(aml_gpiomem_dev);
+ if (err)
+ goto failed_device_create;
+
+ for (i = 0; i < inst->gpio_area_count; ++i) {
+ dev_info(inst->dev,
+ "Initialised: Registers at start:0x%08lx end:0x%08lx size:0x%08lx",
+ inst->gpio_regs_phys[i].start,
+ inst->gpio_regs_phys[i].end,
+ inst->gpio_regs_phys[i].end - inst->gpio_regs_phys[i].start);
+ }
+
+ return 0;
+
+failed_device_create:
+ class_destroy(aml_gpiomem_class);
+failed_class_create:
+ cdev_del(&aml_gpiomem_cdev);
+failed_cdev_add:
+ unregister_chrdev_region(aml_gpiomem_devid, 1);
+failed_alloc_chrdev:
+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 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;
+}
+
+static const struct of_device_id aml_gpiomem_of_match[] = {
+ {.compatible = "amlogic, gpiomem",},
+ { },
+};
+MODULE_DEVICE_TABLE(of, aml_gpiomem_of_match);
+
+static struct platform_driver aml_gpiomem_driver = {
+ .driver = {
+ .name = DRIVER_NAME,
+ .owner = THIS_MODULE,
+ .of_match_table = aml_gpiomem_of_match,
+ },
+ .probe = aml_gpiomem_probe,
+ .remove = aml_gpiomem_remove,
+};
+
+module_platform_driver(aml_gpiomem_driver);
+
+MODULE_ALIAS("platform:gpiomem-aml");
+MODULE_DESCRIPTION("AMLogic gpiomem driver for accessing GPIO from userspace");
+MODULE_AUTHOR("Brian Kim <brian.kim@hardkernel.com>");
+MODULE_LICENSE("GPL");
--
2.39.2