blob: 72b64039241a43cbe23a5d764762ab252ee8cbc4 [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>
14
15/* Loongson 1 GPIO Register Definitions */
16#define GPIO_CFG 0x0
17#define GPIO_DIR 0x10
18#define GPIO_DATA 0x20
19#define GPIO_OUTPUT 0x30
20
21static void __iomem *gpio_reg_base;
22
23static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
24{
25 unsigned long pinmask = gc->pin2mask(gc, offset);
26 unsigned long flags;
27
28 spin_lock_irqsave(&gc->bgpio_lock, flags);
29 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | pinmask,
30 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{
38 unsigned long pinmask = gc->pin2mask(gc, offset);
39 unsigned long flags;
40
41 spin_lock_irqsave(&gc->bgpio_lock, flags);
42 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~pinmask,
43 gpio_reg_base + GPIO_CFG);
44 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
45}
46
47static int ls1x_gpio_probe(struct platform_device *pdev)
48{
49 struct device *dev = &pdev->dev;
50 struct gpio_chip *gc;
51 struct resource *res;
52 int ret;
53
54 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
55 if (!gc)
56 return -ENOMEM;
57
58 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kelvin Cheungbd37c992016-04-06 20:34:53 +080059 gpio_reg_base = devm_ioremap_resource(dev, res);
60 if (IS_ERR(gpio_reg_base))
61 return PTR_ERR(gpio_reg_base);
62
63 ret = bgpio_init(gc, dev, 4, gpio_reg_base + GPIO_DATA,
64 gpio_reg_base + GPIO_OUTPUT, NULL,
65 NULL, gpio_reg_base + GPIO_DIR, 0);
66 if (ret)
67 goto err;
68
69 gc->owner = THIS_MODULE;
70 gc->request = ls1x_gpio_request;
71 gc->free = ls1x_gpio_free;
72 gc->base = pdev->id * 32;
73
74 ret = devm_gpiochip_add_data(dev, gc, NULL);
75 if (ret)
76 goto err;
77
78 platform_set_drvdata(pdev, gc);
79 dev_info(dev, "Loongson1 GPIO driver registered\n");
80
81 return 0;
82err:
83 dev_err(dev, "failed to register GPIO device\n");
84 return ret;
85}
86
87static struct platform_driver ls1x_gpio_driver = {
88 .probe = ls1x_gpio_probe,
89 .driver = {
90 .name = "ls1x-gpio",
91 },
92};
93
94module_platform_driver(ls1x_gpio_driver);
95
96MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
97MODULE_DESCRIPTION("Loongson1 GPIO driver");
98MODULE_LICENSE("GPL");