2 * GPIO Driver for Loongson 1 SoC
4 * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/module.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/platform_device.h>
14 #include <linux/bitops.h>
16 /* Loongson 1 GPIO Register Definitions */
19 #define GPIO_DATA 0x20
20 #define GPIO_OUTPUT 0x30
22 static void __iomem *gpio_reg_base;
24 static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
28 spin_lock_irqsave(&gc->bgpio_lock, flags);
29 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
30 gpio_reg_base + GPIO_CFG);
31 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
36 static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
40 spin_lock_irqsave(&gc->bgpio_lock, flags);
41 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
42 gpio_reg_base + GPIO_CFG);
43 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
46 static int ls1x_gpio_probe(struct platform_device *pdev)
48 struct device *dev = &pdev->dev;
52 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
56 gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
57 if (IS_ERR(gpio_reg_base))
58 return PTR_ERR(gpio_reg_base);
60 ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
61 gpio_reg_base + GPIO_OUTPUT, NULL,
62 NULL, gpio_reg_base + GPIO_DIR, 0);
66 gc->owner = THIS_MODULE;
67 gc->request = ls1x_gpio_request;
68 gc->free = ls1x_gpio_free;
69 gc->base = pdev->id * 32;
71 ret = devm_gpiochip_add_data(dev, gc, NULL);
75 platform_set_drvdata(pdev, gc);
76 dev_info(dev, "Loongson1 GPIO driver registered\n");
80 dev_err(dev, "failed to register GPIO device\n");
84 static struct platform_driver ls1x_gpio_driver = {
85 .probe = ls1x_gpio_probe,
91 module_platform_driver(ls1x_gpio_driver);
93 MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
94 MODULE_DESCRIPTION("Loongson1 GPIO driver");
95 MODULE_LICENSE("GPL");