blob: eb68603136b0e78e76d6a87b2739efac997f2538 [file] [log] [blame]
Joachim Eastwood13a43fd2015-05-02 23:11:34 +02001/*
2 * GPIO driver for NXP LPC18xx/43xx.
3 *
4 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/clk.h>
13#include <linux/gpio/driver.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_gpio.h>
18#include <linux/pinctrl/consumer.h>
19#include <linux/platform_device.h>
20
21/* LPC18xx GPIO register offsets */
22#define LPC18XX_REG_DIR(n) (0x2000 + n * sizeof(u32))
23
24#define LPC18XX_MAX_PORTS 8
25#define LPC18XX_PINS_PER_PORT 32
26
27struct lpc18xx_gpio_chip {
28 struct gpio_chip gpio;
29 void __iomem *base;
30 struct clk *clk;
31 spinlock_t lock;
32};
33
34static inline struct lpc18xx_gpio_chip *to_lpc18xx_gpio(struct gpio_chip *chip)
35{
36 return container_of(chip, struct lpc18xx_gpio_chip, gpio);
37}
38
39static int lpc18xx_gpio_request(struct gpio_chip *chip, unsigned offset)
40{
41 return pinctrl_request_gpio(offset);
42}
43
44static void lpc18xx_gpio_free(struct gpio_chip *chip, unsigned offset)
45{
46 pinctrl_free_gpio(offset);
47}
48
49static void lpc18xx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
50{
51 struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
52 writeb(value ? 1 : 0, gc->base + offset);
53}
54
55static int lpc18xx_gpio_get(struct gpio_chip *chip, unsigned offset)
56{
57 struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
58 return !!readb(gc->base + offset);
59}
60
61static int lpc18xx_gpio_direction(struct gpio_chip *chip, unsigned offset,
62 bool out)
63{
64 struct lpc18xx_gpio_chip *gc = to_lpc18xx_gpio(chip);
65 unsigned long flags;
66 u32 port, pin, dir;
67
68 port = offset / LPC18XX_PINS_PER_PORT;
69 pin = offset % LPC18XX_PINS_PER_PORT;
70
71 spin_lock_irqsave(&gc->lock, flags);
72 dir = readl(gc->base + LPC18XX_REG_DIR(port));
73 if (out)
74 dir |= BIT(pin);
75 else
76 dir &= ~BIT(pin);
77 writel(dir, gc->base + LPC18XX_REG_DIR(port));
78 spin_unlock_irqrestore(&gc->lock, flags);
79
80 return 0;
81}
82
83static int lpc18xx_gpio_direction_input(struct gpio_chip *chip,
84 unsigned offset)
85{
86 return lpc18xx_gpio_direction(chip, offset, false);
87}
88
89static int lpc18xx_gpio_direction_output(struct gpio_chip *chip,
90 unsigned offset, int value)
91{
92 lpc18xx_gpio_set(chip, offset, value);
93 return lpc18xx_gpio_direction(chip, offset, true);
94}
95
96static struct gpio_chip lpc18xx_chip = {
97 .label = "lpc18xx/43xx-gpio",
98 .request = lpc18xx_gpio_request,
99 .free = lpc18xx_gpio_free,
100 .direction_input = lpc18xx_gpio_direction_input,
101 .direction_output = lpc18xx_gpio_direction_output,
102 .set = lpc18xx_gpio_set,
103 .get = lpc18xx_gpio_get,
104 .ngpio = LPC18XX_MAX_PORTS * LPC18XX_PINS_PER_PORT,
105 .owner = THIS_MODULE,
106};
107
108static int lpc18xx_gpio_probe(struct platform_device *pdev)
109{
110 struct lpc18xx_gpio_chip *gc;
111 struct resource *res;
112 int ret;
113
114 gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
115 if (!gc)
116 return -ENOMEM;
117
118 gc->gpio = lpc18xx_chip;
119 platform_set_drvdata(pdev, gc);
120
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 gc->base = devm_ioremap_resource(&pdev->dev, res);
123 if (IS_ERR(gc->base))
124 return PTR_ERR(gc->base);
125
126 gc->clk = devm_clk_get(&pdev->dev, NULL);
127 if (IS_ERR(gc->clk)) {
128 dev_err(&pdev->dev, "input clock not found\n");
129 return PTR_ERR(gc->clk);
130 }
131
132 ret = clk_prepare_enable(gc->clk);
133 if (ret) {
134 dev_err(&pdev->dev, "unable to enable clock\n");
135 return ret;
136 }
137
138 spin_lock_init(&gc->lock);
139
140 gc->gpio.dev = &pdev->dev;
141
142 ret = gpiochip_add(&gc->gpio);
143 if (ret) {
144 dev_err(&pdev->dev, "failed to add gpio chip\n");
145 clk_disable_unprepare(gc->clk);
146 return ret;
147 }
148
149 return 0;
150}
151
152static int lpc18xx_gpio_remove(struct platform_device *pdev)
153{
154 struct lpc18xx_gpio_chip *gc = platform_get_drvdata(pdev);
155
156 gpiochip_remove(&gc->gpio);
157 clk_disable_unprepare(gc->clk);
158
159 return 0;
160}
161
162static const struct of_device_id lpc18xx_gpio_match[] = {
163 { .compatible = "nxp,lpc1850-gpio" },
164 { }
165};
166MODULE_DEVICE_TABLE(of, lpc18xx_gpio_match);
167
168static struct platform_driver lpc18xx_gpio_driver = {
169 .probe = lpc18xx_gpio_probe,
170 .remove = lpc18xx_gpio_remove,
171 .driver = {
172 .name = "lpc18xx-gpio",
173 .of_match_table = lpc18xx_gpio_match,
174 },
175};
176module_platform_driver(lpc18xx_gpio_driver);
177
178MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
179MODULE_DESCRIPTION("GPIO driver for LPC18xx/43xx");
180MODULE_LICENSE("GPL v2");