]> git.karo-electronics.de Git - linux-beck.git/commitdiff
gpio: Loongson1: add Loongson1 GPIO driver
authorKelvin Cheung <keguang.zhang@gmail.com>
Wed, 6 Apr 2016 12:34:53 +0000 (20:34 +0800)
committerLinus Walleij <linus.walleij@linaro.org>
Sat, 9 Apr 2016 18:14:35 +0000 (20:14 +0200)
This patch adds GPIO driver for Loongson1B.

Signed-off-by: Kelvin Cheung <keguang.zhang@gmail.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/gpio/Kconfig
drivers/gpio/Makefile
drivers/gpio/gpio-loongson1.c [new file with mode: 0644]

index 08a93e0b35c5d8bf14174a9d7cc3ba2a59855f6a..37f03786b0e6b2451cda75ee43b23826ba595f25 100644 (file)
@@ -511,6 +511,13 @@ config GPIO_ZX
        help
          Say yes here to support the GPIO device on ZTE ZX SoCs.
 
+config GPIO_LOONGSON1
+       tristate "Loongson1 GPIO support"
+       depends on MACH_LOONGSON32
+       select GPIO_GENERIC
+       help
+         Say Y or M here to support GPIO on Loongson1 SoCs.
+
 endmenu
 
 menu "Port-mapped I/O GPIO drivers"
index 1e0b74f3b1ed98021f3d05b067b749b7cb1c652d..40ab9134a40cd52cf936534c55e77e65fdd02dc1 100644 (file)
@@ -127,3 +127,4 @@ obj-$(CONFIG_GPIO_XTENSA)   += gpio-xtensa.o
 obj-$(CONFIG_GPIO_ZEVIO)       += gpio-zevio.o
 obj-$(CONFIG_GPIO_ZYNQ)                += gpio-zynq.o
 obj-$(CONFIG_GPIO_ZX)          += gpio-zx.o
+obj-$(CONFIG_GPIO_LOONGSON1)   += gpio-loongson1.o
diff --git a/drivers/gpio/gpio-loongson1.c b/drivers/gpio/gpio-loongson1.c
new file mode 100644 (file)
index 0000000..10c09bd
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * GPIO Driver for Loongson 1 SoC
+ *
+ * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/gpio/driver.h>
+#include <linux/platform_device.h>
+
+/* Loongson 1 GPIO Register Definitions */
+#define GPIO_CFG               0x0
+#define GPIO_DIR               0x10
+#define GPIO_DATA              0x20
+#define GPIO_OUTPUT            0x30
+
+static void __iomem *gpio_reg_base;
+
+static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
+{
+       unsigned long pinmask = gc->pin2mask(gc, offset);
+       unsigned long flags;
+
+       spin_lock_irqsave(&gc->bgpio_lock, flags);
+       __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | pinmask,
+                    gpio_reg_base + GPIO_CFG);
+       spin_unlock_irqrestore(&gc->bgpio_lock, flags);
+
+       return 0;
+}
+
+static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
+{
+       unsigned long pinmask = gc->pin2mask(gc, offset);
+       unsigned long flags;
+
+       spin_lock_irqsave(&gc->bgpio_lock, flags);
+       __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~pinmask,
+                    gpio_reg_base + GPIO_CFG);
+       spin_unlock_irqrestore(&gc->bgpio_lock, flags);
+}
+
+static int ls1x_gpio_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct gpio_chip *gc;
+       struct resource *res;
+       int ret;
+
+       gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
+       if (!gc)
+               return -ENOMEM;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if (!res) {
+               dev_err(dev, "failed to get I/O memory\n");
+               return -EINVAL;
+       }
+
+       gpio_reg_base = devm_ioremap_resource(dev, res);
+       if (IS_ERR(gpio_reg_base))
+               return PTR_ERR(gpio_reg_base);
+
+       ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
+                        gpio_reg_base + GPIO_OUTPUT, NULL,
+                        NULL, gpio_reg_base + GPIO_DIR, 0);
+       if (ret)
+               goto err;
+
+       gc->owner = THIS_MODULE;
+       gc->request = ls1x_gpio_request;
+       gc->free = ls1x_gpio_free;
+       gc->base = pdev->id * 32;
+
+       ret = devm_gpiochip_add_data(dev, gc, NULL);
+       if (ret)
+               goto err;
+
+       platform_set_drvdata(pdev, gc);
+       dev_info(dev, "Loongson1 GPIO driver registered\n");
+
+       return 0;
+err:
+       dev_err(dev, "failed to register GPIO device\n");
+       return ret;
+}
+
+static struct platform_driver ls1x_gpio_driver = {
+       .probe  = ls1x_gpio_probe,
+       .driver = {
+               .name   = "ls1x-gpio",
+       },
+};
+
+module_platform_driver(ls1x_gpio_driver);
+
+MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
+MODULE_DESCRIPTION("Loongson1 GPIO driver");
+MODULE_LICENSE("GPL");