blob: 1b1ee94eeab4762a9091b2d1a366e19dcba37629 [file] [log] [blame]
Kelvin Cheungbd37c992016-04-06 20:34:53 +08001/*
2 * GPIO Driver for Loongson 1 SoC
3 *
4 * Copyright (C) 2015-2016 Zhang, Keguang <keguang.zhang@gmail.com>
5 *
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.
9 */
10
Paul Gortmaker5f604502016-09-12 18:16:29 -040011#include <linux/module.h>
Kelvin Cheungbd37c992016-04-06 20:34:53 +080012#include <linux/gpio/driver.h>
13#include <linux/platform_device.h>
Linus Walleijfe294162017-10-20 14:42:01 +020014#include <linux/bitops.h>
Kelvin Cheungbd37c992016-04-06 20:34:53 +080015
16/* Loongson 1 GPIO Register Definitions */
17#define GPIO_CFG 0x0
18#define GPIO_DIR 0x10
19#define GPIO_DATA 0x20
20#define GPIO_OUTPUT 0x30
21
22static void __iomem *gpio_reg_base;
23
24static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
25{
Kelvin Cheungbd37c992016-04-06 20:34:53 +080026 unsigned long flags;
27
28 spin_lock_irqsave(&gc->bgpio_lock, flags);
Linus Walleijfe294162017-10-20 14:42:01 +020029 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
Kelvin Cheungbd37c992016-04-06 20:34:53 +080030 gpio_reg_base + GPIO_CFG);
31 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
32
33 return 0;
34}
35
36static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
37{
Kelvin Cheungbd37c992016-04-06 20:34:53 +080038 unsigned long flags;
39
40 spin_lock_irqsave(&gc->bgpio_lock, flags);
Linus Walleijfe294162017-10-20 14:42:01 +020041 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
Kelvin Cheungbd37c992016-04-06 20:34:53 +080042 gpio_reg_base + GPIO_CFG);
43 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
44}
45
46static int ls1x_gpio_probe(struct platform_device *pdev)
47{
48 struct device *dev = &pdev->dev;
49 struct gpio_chip *gc;
Kelvin Cheungbd37c992016-04-06 20:34:53 +080050 int ret;
51
52 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
53 if (!gc)
54 return -ENOMEM;
55
Enrico Weigelt, metux IT consult62fe0722019-03-11 19:54:56 +010056 gpio_reg_base = devm_platform_ioremap_resource(pdev, 0);
Kelvin Cheungbd37c992016-04-06 20:34:53 +080057 if (IS_ERR(gpio_reg_base))
58 return PTR_ERR(gpio_reg_base);
59
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);
63 if (ret)
64 goto err;
65
66 gc->owner = THIS_MODULE;
67 gc->request = ls1x_gpio_request;
68 gc->free = ls1x_gpio_free;
69 gc->base = pdev->id * 32;
70
71 ret = devm_gpiochip_add_data(dev, gc, NULL);
72 if (ret)
73 goto err;
74
75 platform_set_drvdata(pdev, gc);
76 dev_info(dev, "Loongson1 GPIO driver registered\n");
77
78 return 0;
79err:
80 dev_err(dev, "failed to register GPIO device\n");
81 return ret;
82}
83
84static struct platform_driver ls1x_gpio_driver = {
85 .probe = ls1x_gpio_probe,
86 .driver = {
87 .name = "ls1x-gpio",
88 },
89};
90
91module_platform_driver(ls1x_gpio_driver);
92
93MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
94MODULE_DESCRIPTION("Loongson1 GPIO driver");
95MODULE_LICENSE("GPL");