gpio: moxart: Avoid forward declaration
[deliverable/linux.git] / drivers / gpio / gpio-moxart.c
CommitLineData
0299b77b
JJ
1/*
2 * MOXA ART SoCs GPIO driver.
3 *
4 * Copyright (C) 2013 Jonas Jensen
5 *
6 * Jonas Jensen <jonas.jensen@gmail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/irq.h>
16#include <linux/io.h>
17#include <linux/gpio.h>
18#include <linux/platform_device.h>
19#include <linux/module.h>
20#include <linux/of_address.h>
21#include <linux/of_gpio.h>
22#include <linux/pinctrl/consumer.h>
23#include <linux/delay.h>
24#include <linux/timer.h>
25#include <linux/bitops.h>
26
27#define GPIO_DATA_OUT 0x00
28#define GPIO_DATA_IN 0x04
29#define GPIO_PIN_DIRECTION 0x08
30
31struct moxart_gpio_chip {
32 struct gpio_chip gpio;
a87854da 33 void __iomem *base;
0299b77b
JJ
34};
35
36static inline struct moxart_gpio_chip *to_moxart_gpio(struct gpio_chip *chip)
37{
38 return container_of(chip, struct moxart_gpio_chip, gpio);
39}
40
41static int moxart_gpio_request(struct gpio_chip *chip, unsigned offset)
42{
43 return pinctrl_request_gpio(offset);
44}
45
46static void moxart_gpio_free(struct gpio_chip *chip, unsigned offset)
47{
48 pinctrl_free_gpio(offset);
49}
50
0299b77b
JJ
51static void moxart_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
52{
53 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
a87854da 54 void __iomem *ioaddr = gc->base + GPIO_DATA_OUT;
0299b77b
JJ
55 u32 reg = readl(ioaddr);
56
57 if (value)
58 reg = reg | BIT(offset);
59 else
60 reg = reg & ~BIT(offset);
61
0299b77b
JJ
62 writel(reg, ioaddr);
63}
64
65static int moxart_gpio_get(struct gpio_chip *chip, unsigned offset)
66{
67 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
a87854da 68 u32 ret = readl(gc->base + GPIO_PIN_DIRECTION);
0299b77b
JJ
69
70 if (ret & BIT(offset))
a87854da 71 return !!(readl(gc->base + GPIO_DATA_OUT) & BIT(offset));
0299b77b 72 else
a87854da 73 return !!(readl(gc->base + GPIO_DATA_IN) & BIT(offset));
0299b77b
JJ
74}
75
c9e8dbad
AL
76static int moxart_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
77{
78 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
79 void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
80
81 writel(readl(ioaddr) & ~BIT(offset), ioaddr);
82 return 0;
83}
84
85static int moxart_gpio_direction_output(struct gpio_chip *chip,
86 unsigned offset, int value)
87{
88 struct moxart_gpio_chip *gc = to_moxart_gpio(chip);
89 void __iomem *ioaddr = gc->base + GPIO_PIN_DIRECTION;
90
91 writel(readl(ioaddr) | BIT(offset), ioaddr);
92 return 0;
93}
94
0299b77b
JJ
95static struct gpio_chip moxart_template_chip = {
96 .label = "moxart-gpio",
97 .request = moxart_gpio_request,
98 .free = moxart_gpio_free,
99 .direction_input = moxart_gpio_direction_input,
100 .direction_output = moxart_gpio_direction_output,
101 .set = moxart_gpio_set,
102 .get = moxart_gpio_get,
0299b77b 103 .ngpio = 32,
50d7c9c8 104 .owner = THIS_MODULE,
0299b77b
JJ
105};
106
107static int moxart_gpio_probe(struct platform_device *pdev)
108{
109 struct device *dev = &pdev->dev;
110 struct resource *res;
111 struct moxart_gpio_chip *mgc;
112 int ret;
113
114 mgc = devm_kzalloc(dev, sizeof(*mgc), GFP_KERNEL);
115 if (!mgc) {
116 dev_err(dev, "can't allocate GPIO chip container\n");
117 return -ENOMEM;
118 }
119 mgc->gpio = moxart_template_chip;
120
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
a87854da 122 mgc->base = devm_ioremap_resource(dev, res);
ca0ae81d 123 if (IS_ERR(mgc->base))
a87854da 124 return PTR_ERR(mgc->base);
0299b77b
JJ
125
126 mgc->gpio.dev = dev;
127
128 ret = gpiochip_add(&mgc->gpio);
129 if (ret) {
130 dev_err(dev, "%s: gpiochip_add failed\n",
131 dev->of_node->full_name);
132 return ret;
133 }
134
135 return 0;
136}
137
138static const struct of_device_id moxart_gpio_match[] = {
139 { .compatible = "moxa,moxart-gpio" },
140 { }
141};
142
143static struct platform_driver moxart_gpio_driver = {
144 .driver = {
145 .name = "moxart-gpio",
146 .owner = THIS_MODULE,
147 .of_match_table = moxart_gpio_match,
148 },
149 .probe = moxart_gpio_probe,
150};
151module_platform_driver(moxart_gpio_driver);
152
153MODULE_DESCRIPTION("MOXART GPIO chip driver");
154MODULE_LICENSE("GPL");
155MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
This page took 0.041941 seconds and 5 git commands to generate.