blob: 7b7bd26f704aa2c3e5c10ff998a14f756d7d2d59 [file] [log] [blame]
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001/*
2 * Driver for Broadcom BCM2835 GPIO unit (pinctrl + GPIO)
3 *
4 * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
5 *
6 * This driver is inspired by:
7 * pinctrl-nomadik.c, please see original file for copyright information
8 * pinctrl-tegra.c, please see original file for copyright information
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/bitmap.h>
22#include <linux/bug.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/err.h>
Linus Walleije19a5f72015-12-08 22:01:00 +010026#include <linux/gpio/driver.h>
Simon Arlotte1b2dc72012-09-27 22:10:11 -060027#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/irqdesc.h>
Paul Gortmaker34f46842017-05-22 16:56:48 -040030#include <linux/init.h>
Simon Arlotte1b2dc72012-09-27 22:10:11 -060031#include <linux/of_address.h>
32#include <linux/of.h>
33#include <linux/of_irq.h>
34#include <linux/pinctrl/consumer.h>
35#include <linux/pinctrl/machine.h>
36#include <linux/pinctrl/pinconf.h>
37#include <linux/pinctrl/pinctrl.h>
38#include <linux/pinctrl/pinmux.h>
39#include <linux/platform_device.h>
40#include <linux/seq_file.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/types.h>
44
45#define MODULE_NAME "pinctrl-bcm2835"
46#define BCM2835_NUM_GPIOS 54
47#define BCM2835_NUM_BANKS 2
Phil Elwell00445b52015-02-24 13:40:50 +000048#define BCM2835_NUM_IRQS 3
Simon Arlotte1b2dc72012-09-27 22:10:11 -060049
50#define BCM2835_PIN_BITMAP_SZ \
51 DIV_ROUND_UP(BCM2835_NUM_GPIOS, sizeof(unsigned long) * 8)
52
53/* GPIO register offsets */
54#define GPFSEL0 0x0 /* Function Select */
55#define GPSET0 0x1c /* Pin Output Set */
56#define GPCLR0 0x28 /* Pin Output Clear */
57#define GPLEV0 0x34 /* Pin Level */
58#define GPEDS0 0x40 /* Pin Event Detect Status */
59#define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
60#define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
61#define GPHEN0 0x64 /* Pin High Detect Enable */
62#define GPLEN0 0x70 /* Pin Low Detect Enable */
63#define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
64#define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
65#define GPPUD 0x94 /* Pin Pull-up/down Enable */
66#define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
67
68#define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
69#define FSEL_SHIFT(p) (((p) % 10) * 3)
70#define GPIO_REG_OFFSET(p) ((p) / 32)
71#define GPIO_REG_SHIFT(p) ((p) % 32)
72
73enum bcm2835_pinconf_param {
74 /* argument: bcm2835_pinconf_pull */
75 BCM2835_PINCONF_PARAM_PULL,
76};
77
Simon Arlotte1b2dc72012-09-27 22:10:11 -060078#define BCM2835_PINCONF_PACK(_param_, _arg_) ((_param_) << 16 | (_arg_))
79#define BCM2835_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16)
80#define BCM2835_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff)
81
Simon Arlotte1b2dc72012-09-27 22:10:11 -060082struct bcm2835_pinctrl {
83 struct device *dev;
84 void __iomem *base;
Phil Elwell00445b52015-02-24 13:40:50 +000085 int irq[BCM2835_NUM_IRQS];
Simon Arlotte1b2dc72012-09-27 22:10:11 -060086
87 /* note: locking assumes each bank will have its own unsigned long */
88 unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
89 unsigned int irq_type[BCM2835_NUM_GPIOS];
90
91 struct pinctrl_dev *pctl_dev;
Simon Arlotte1b2dc72012-09-27 22:10:11 -060092 struct gpio_chip gpio_chip;
93 struct pinctrl_gpio_range gpio_range;
94
Linus Walleij85ae9e52016-11-14 18:48:19 +010095 int irq_group[BCM2835_NUM_IRQS];
Simon Arlotte1b2dc72012-09-27 22:10:11 -060096 spinlock_t irq_lock[BCM2835_NUM_BANKS];
97};
98
Simon Arlotte1b2dc72012-09-27 22:10:11 -060099/* pins are just named GPIO0..GPIO53 */
100#define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
Sachin Kamate8ed9122013-06-18 14:34:24 +0530101static struct pinctrl_pin_desc bcm2835_gpio_pins[] = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600102 BCM2835_GPIO_PIN(0),
103 BCM2835_GPIO_PIN(1),
104 BCM2835_GPIO_PIN(2),
105 BCM2835_GPIO_PIN(3),
106 BCM2835_GPIO_PIN(4),
107 BCM2835_GPIO_PIN(5),
108 BCM2835_GPIO_PIN(6),
109 BCM2835_GPIO_PIN(7),
110 BCM2835_GPIO_PIN(8),
111 BCM2835_GPIO_PIN(9),
112 BCM2835_GPIO_PIN(10),
113 BCM2835_GPIO_PIN(11),
114 BCM2835_GPIO_PIN(12),
115 BCM2835_GPIO_PIN(13),
116 BCM2835_GPIO_PIN(14),
117 BCM2835_GPIO_PIN(15),
118 BCM2835_GPIO_PIN(16),
119 BCM2835_GPIO_PIN(17),
120 BCM2835_GPIO_PIN(18),
121 BCM2835_GPIO_PIN(19),
122 BCM2835_GPIO_PIN(20),
123 BCM2835_GPIO_PIN(21),
124 BCM2835_GPIO_PIN(22),
125 BCM2835_GPIO_PIN(23),
126 BCM2835_GPIO_PIN(24),
127 BCM2835_GPIO_PIN(25),
128 BCM2835_GPIO_PIN(26),
129 BCM2835_GPIO_PIN(27),
130 BCM2835_GPIO_PIN(28),
131 BCM2835_GPIO_PIN(29),
132 BCM2835_GPIO_PIN(30),
133 BCM2835_GPIO_PIN(31),
134 BCM2835_GPIO_PIN(32),
135 BCM2835_GPIO_PIN(33),
136 BCM2835_GPIO_PIN(34),
137 BCM2835_GPIO_PIN(35),
138 BCM2835_GPIO_PIN(36),
139 BCM2835_GPIO_PIN(37),
140 BCM2835_GPIO_PIN(38),
141 BCM2835_GPIO_PIN(39),
142 BCM2835_GPIO_PIN(40),
143 BCM2835_GPIO_PIN(41),
144 BCM2835_GPIO_PIN(42),
145 BCM2835_GPIO_PIN(43),
146 BCM2835_GPIO_PIN(44),
147 BCM2835_GPIO_PIN(45),
148 BCM2835_GPIO_PIN(46),
149 BCM2835_GPIO_PIN(47),
150 BCM2835_GPIO_PIN(48),
151 BCM2835_GPIO_PIN(49),
152 BCM2835_GPIO_PIN(50),
153 BCM2835_GPIO_PIN(51),
154 BCM2835_GPIO_PIN(52),
155 BCM2835_GPIO_PIN(53),
156};
157
158/* one pin per group */
159static const char * const bcm2835_gpio_groups[] = {
160 "gpio0",
161 "gpio1",
162 "gpio2",
163 "gpio3",
164 "gpio4",
165 "gpio5",
166 "gpio6",
167 "gpio7",
168 "gpio8",
169 "gpio9",
170 "gpio10",
171 "gpio11",
172 "gpio12",
173 "gpio13",
174 "gpio14",
175 "gpio15",
176 "gpio16",
177 "gpio17",
178 "gpio18",
179 "gpio19",
180 "gpio20",
181 "gpio21",
182 "gpio22",
183 "gpio23",
184 "gpio24",
185 "gpio25",
186 "gpio26",
187 "gpio27",
188 "gpio28",
189 "gpio29",
190 "gpio30",
191 "gpio31",
192 "gpio32",
193 "gpio33",
194 "gpio34",
195 "gpio35",
196 "gpio36",
197 "gpio37",
198 "gpio38",
199 "gpio39",
200 "gpio40",
201 "gpio41",
202 "gpio42",
203 "gpio43",
204 "gpio44",
205 "gpio45",
206 "gpio46",
207 "gpio47",
208 "gpio48",
209 "gpio49",
210 "gpio50",
211 "gpio51",
212 "gpio52",
213 "gpio53",
214};
215
216enum bcm2835_fsel {
217 BCM2835_FSEL_GPIO_IN = 0,
218 BCM2835_FSEL_GPIO_OUT = 1,
219 BCM2835_FSEL_ALT0 = 4,
220 BCM2835_FSEL_ALT1 = 5,
221 BCM2835_FSEL_ALT2 = 6,
222 BCM2835_FSEL_ALT3 = 7,
223 BCM2835_FSEL_ALT4 = 3,
224 BCM2835_FSEL_ALT5 = 2,
225 BCM2835_FSEL_COUNT = 8,
226 BCM2835_FSEL_MASK = 0x7,
227};
228
229static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
230 [BCM2835_FSEL_GPIO_IN] = "gpio_in",
231 [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
232 [BCM2835_FSEL_ALT0] = "alt0",
233 [BCM2835_FSEL_ALT1] = "alt1",
234 [BCM2835_FSEL_ALT2] = "alt2",
235 [BCM2835_FSEL_ALT3] = "alt3",
236 [BCM2835_FSEL_ALT4] = "alt4",
237 [BCM2835_FSEL_ALT5] = "alt5",
238};
239
240static const char * const irq_type_names[] = {
241 [IRQ_TYPE_NONE] = "none",
242 [IRQ_TYPE_EDGE_RISING] = "edge-rising",
243 [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
244 [IRQ_TYPE_EDGE_BOTH] = "edge-both",
245 [IRQ_TYPE_LEVEL_HIGH] = "level-high",
246 [IRQ_TYPE_LEVEL_LOW] = "level-low",
247};
248
249static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
250{
251 return readl(pc->base + reg);
252}
253
254static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
255 u32 val)
256{
257 writel(val, pc->base + reg);
258}
259
260static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
261 unsigned bit)
262{
263 reg += GPIO_REG_OFFSET(bit) * 4;
264 return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
265}
266
267/* note NOT a read/modify/write cycle */
268static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
269 unsigned reg, unsigned bit)
270{
271 reg += GPIO_REG_OFFSET(bit) * 4;
272 bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
273}
274
275static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
276 struct bcm2835_pinctrl *pc, unsigned pin)
277{
278 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
279 enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
280
281 dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
282 bcm2835_functions[status]);
283
284 return status;
285}
286
287static inline void bcm2835_pinctrl_fsel_set(
288 struct bcm2835_pinctrl *pc, unsigned pin,
289 enum bcm2835_fsel fsel)
290{
291 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
292 enum bcm2835_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
293
294 dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
295 bcm2835_functions[cur]);
296
297 if (cur == fsel)
298 return;
299
300 if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
301 /* always transition through GPIO_IN */
302 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
303 val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
304
305 dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
306 bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
307 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
308 }
309
310 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
311 val |= fsel << FSEL_SHIFT(pin);
312
313 dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
314 bcm2835_functions[fsel]);
315 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
316}
317
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600318static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
319{
320 return pinctrl_gpio_direction_input(chip->base + offset);
321}
322
323static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
324{
Linus Walleije19a5f72015-12-08 22:01:00 +0100325 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600326
327 return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
328}
329
Stefan Wahren20b3d2a2016-03-28 14:58:24 +0000330static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
331{
332 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
333 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
334
335 /* Alternative function doesn't clearly provide a direction */
336 if (fsel > BCM2835_FSEL_GPIO_OUT)
337 return -EINVAL;
338
339 return (fsel == BCM2835_FSEL_GPIO_IN);
340}
341
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600342static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
343{
Linus Walleije19a5f72015-12-08 22:01:00 +0100344 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600345
346 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
347}
348
Stefan Wahren4c02cba2015-11-19 00:32:27 +0000349static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
350 unsigned offset, int value)
351{
352 bcm2835_gpio_set(chip, offset, value);
353 return pinctrl_gpio_direction_output(chip->base + offset);
354}
355
Gustavo A. R. Silva531bcf72017-07-11 13:03:39 -0500356static const struct gpio_chip bcm2835_gpio_chip = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600357 .label = MODULE_NAME,
358 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200359 .request = gpiochip_generic_request,
360 .free = gpiochip_generic_free,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600361 .direction_input = bcm2835_gpio_direction_input,
362 .direction_output = bcm2835_gpio_direction_output,
Stefan Wahren20b3d2a2016-03-28 14:58:24 +0000363 .get_direction = bcm2835_gpio_get_direction,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600364 .get = bcm2835_gpio_get,
365 .set = bcm2835_gpio_set,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600366 .base = -1,
367 .ngpio = BCM2835_NUM_GPIOS,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100368 .can_sleep = false,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600369};
370
Linus Walleij85ae9e52016-11-14 18:48:19 +0100371static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
372 unsigned int bank, u32 mask)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600373{
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600374 unsigned long events;
375 unsigned offset;
376 unsigned gpio;
377 unsigned int type;
378
379 events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
Phil Elwell00445b52015-02-24 13:40:50 +0000380 events &= mask;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600381 events &= pc->enabled_irq_map[bank];
382 for_each_set_bit(offset, &events, 32) {
383 gpio = (32 * bank) + offset;
Linus Walleij85ae9e52016-11-14 18:48:19 +0100384 /* FIXME: no clue why the code looks up the type here */
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600385 type = pc->irq_type[gpio];
386
Linus Walleij85ae9e52016-11-14 18:48:19 +0100387 generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irqdomain,
388 gpio));
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600389 }
Phil Elwell00445b52015-02-24 13:40:50 +0000390}
391
Linus Walleij85ae9e52016-11-14 18:48:19 +0100392static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
Phil Elwell00445b52015-02-24 13:40:50 +0000393{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100394 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
395 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
396 struct irq_chip *host_chip = irq_desc_get_chip(desc);
397 int irq = irq_desc_get_irq(desc);
398 int group;
399 int i;
Phil Elwell00445b52015-02-24 13:40:50 +0000400
Linus Walleij85ae9e52016-11-14 18:48:19 +0100401 for (i = 0; i < ARRAY_SIZE(pc->irq); i++) {
402 if (pc->irq[i] == irq) {
403 group = pc->irq_group[i];
404 break;
405 }
406 }
407 /* This should not happen, every IRQ has a bank */
408 if (i == ARRAY_SIZE(pc->irq))
409 BUG();
410
411 chained_irq_enter(host_chip, desc);
412
413 switch (group) {
Phil Elwell00445b52015-02-24 13:40:50 +0000414 case 0: /* IRQ0 covers GPIOs 0-27 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100415 bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
Phil Elwell00445b52015-02-24 13:40:50 +0000416 break;
417 case 1: /* IRQ1 covers GPIOs 28-45 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100418 bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
419 bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
Phil Elwell00445b52015-02-24 13:40:50 +0000420 break;
421 case 2: /* IRQ2 covers GPIOs 46-53 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100422 bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
Phil Elwell00445b52015-02-24 13:40:50 +0000423 break;
424 }
425
Linus Walleij85ae9e52016-11-14 18:48:19 +0100426 chained_irq_exit(host_chip, desc);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600427}
428
429static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
430 unsigned reg, unsigned offset, bool enable)
431{
432 u32 value;
433 reg += GPIO_REG_OFFSET(offset) * 4;
434 value = bcm2835_gpio_rd(pc, reg);
435 if (enable)
436 value |= BIT(GPIO_REG_SHIFT(offset));
437 else
438 value &= ~(BIT(GPIO_REG_SHIFT(offset)));
439 bcm2835_gpio_wr(pc, reg, value);
440}
441
442/* fast path for IRQ handler */
443static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
444 unsigned offset, bool enable)
445{
446 switch (pc->irq_type[offset]) {
447 case IRQ_TYPE_EDGE_RISING:
448 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
449 break;
450
451 case IRQ_TYPE_EDGE_FALLING:
452 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
453 break;
454
455 case IRQ_TYPE_EDGE_BOTH:
456 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
457 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
458 break;
459
460 case IRQ_TYPE_LEVEL_HIGH:
461 __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
462 break;
463
464 case IRQ_TYPE_LEVEL_LOW:
465 __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
466 break;
467 }
468}
469
470static void bcm2835_gpio_irq_enable(struct irq_data *data)
471{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100472 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
473 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600474 unsigned gpio = irqd_to_hwirq(data);
475 unsigned offset = GPIO_REG_SHIFT(gpio);
476 unsigned bank = GPIO_REG_OFFSET(gpio);
477 unsigned long flags;
478
479 spin_lock_irqsave(&pc->irq_lock[bank], flags);
480 set_bit(offset, &pc->enabled_irq_map[bank]);
481 bcm2835_gpio_irq_config(pc, gpio, true);
482 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
483}
484
485static void bcm2835_gpio_irq_disable(struct irq_data *data)
486{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100487 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
488 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600489 unsigned gpio = irqd_to_hwirq(data);
490 unsigned offset = GPIO_REG_SHIFT(gpio);
491 unsigned bank = GPIO_REG_OFFSET(gpio);
492 unsigned long flags;
493
494 spin_lock_irqsave(&pc->irq_lock[bank], flags);
495 bcm2835_gpio_irq_config(pc, gpio, false);
Jonathan Bell714b1dd2015-06-30 12:35:39 +0100496 /* Clear events that were latched prior to clearing event sources */
497 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600498 clear_bit(offset, &pc->enabled_irq_map[bank]);
499 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
500}
501
502static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
503 unsigned offset, unsigned int type)
504{
505 switch (type) {
506 case IRQ_TYPE_NONE:
507 case IRQ_TYPE_EDGE_RISING:
508 case IRQ_TYPE_EDGE_FALLING:
509 case IRQ_TYPE_EDGE_BOTH:
510 case IRQ_TYPE_LEVEL_HIGH:
511 case IRQ_TYPE_LEVEL_LOW:
512 pc->irq_type[offset] = type;
513 break;
514
515 default:
516 return -EINVAL;
517 }
518 return 0;
519}
520
521/* slower path for reconfiguring IRQ type */
522static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
523 unsigned offset, unsigned int type)
524{
525 switch (type) {
526 case IRQ_TYPE_NONE:
527 if (pc->irq_type[offset] != type) {
528 bcm2835_gpio_irq_config(pc, offset, false);
529 pc->irq_type[offset] = type;
530 }
531 break;
532
533 case IRQ_TYPE_EDGE_RISING:
534 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
535 /* RISING already enabled, disable FALLING */
536 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
537 bcm2835_gpio_irq_config(pc, offset, false);
538 pc->irq_type[offset] = type;
539 } else if (pc->irq_type[offset] != type) {
540 bcm2835_gpio_irq_config(pc, offset, false);
541 pc->irq_type[offset] = type;
542 bcm2835_gpio_irq_config(pc, offset, true);
543 }
544 break;
545
546 case IRQ_TYPE_EDGE_FALLING:
547 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
548 /* FALLING already enabled, disable RISING */
549 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
550 bcm2835_gpio_irq_config(pc, offset, false);
551 pc->irq_type[offset] = type;
552 } else if (pc->irq_type[offset] != type) {
553 bcm2835_gpio_irq_config(pc, offset, false);
554 pc->irq_type[offset] = type;
555 bcm2835_gpio_irq_config(pc, offset, true);
556 }
557 break;
558
559 case IRQ_TYPE_EDGE_BOTH:
560 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
561 /* RISING already enabled, enable FALLING too */
562 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
563 bcm2835_gpio_irq_config(pc, offset, true);
564 pc->irq_type[offset] = type;
565 } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
566 /* FALLING already enabled, enable RISING too */
567 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
568 bcm2835_gpio_irq_config(pc, offset, true);
569 pc->irq_type[offset] = type;
570 } else if (pc->irq_type[offset] != type) {
571 bcm2835_gpio_irq_config(pc, offset, false);
572 pc->irq_type[offset] = type;
573 bcm2835_gpio_irq_config(pc, offset, true);
574 }
575 break;
576
577 case IRQ_TYPE_LEVEL_HIGH:
578 case IRQ_TYPE_LEVEL_LOW:
579 if (pc->irq_type[offset] != type) {
580 bcm2835_gpio_irq_config(pc, offset, false);
581 pc->irq_type[offset] = type;
582 bcm2835_gpio_irq_config(pc, offset, true);
583 }
584 break;
585
586 default:
587 return -EINVAL;
588 }
589 return 0;
590}
591
592static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
593{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100594 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
595 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600596 unsigned gpio = irqd_to_hwirq(data);
597 unsigned offset = GPIO_REG_SHIFT(gpio);
598 unsigned bank = GPIO_REG_OFFSET(gpio);
599 unsigned long flags;
600 int ret;
601
602 spin_lock_irqsave(&pc->irq_lock[bank], flags);
603
604 if (test_bit(offset, &pc->enabled_irq_map[bank]))
605 ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
606 else
607 ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
608
Charles Keepaxb8a19382015-04-07 11:43:45 +0100609 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixner1aa74fd2015-06-23 15:52:41 +0200610 irq_set_handler_locked(data, handle_edge_irq);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100611 else
Thomas Gleixner1aa74fd2015-06-23 15:52:41 +0200612 irq_set_handler_locked(data, handle_level_irq);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100613
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600614 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
615
616 return ret;
617}
618
Charles Keepaxb8a19382015-04-07 11:43:45 +0100619static void bcm2835_gpio_irq_ack(struct irq_data *data)
620{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100621 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
622 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100623 unsigned gpio = irqd_to_hwirq(data);
624
625 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
626}
627
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600628static struct irq_chip bcm2835_gpio_irq_chip = {
629 .name = MODULE_NAME,
630 .irq_enable = bcm2835_gpio_irq_enable,
631 .irq_disable = bcm2835_gpio_irq_disable,
632 .irq_set_type = bcm2835_gpio_irq_set_type,
Charles Keepaxb8a19382015-04-07 11:43:45 +0100633 .irq_ack = bcm2835_gpio_irq_ack,
634 .irq_mask = bcm2835_gpio_irq_disable,
635 .irq_unmask = bcm2835_gpio_irq_enable,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600636};
637
638static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
639{
640 return ARRAY_SIZE(bcm2835_gpio_groups);
641}
642
643static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
644 unsigned selector)
645{
646 return bcm2835_gpio_groups[selector];
647}
648
649static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
650 unsigned selector,
651 const unsigned **pins,
652 unsigned *num_pins)
653{
654 *pins = &bcm2835_gpio_pins[selector].number;
655 *num_pins = 1;
656
657 return 0;
658}
659
660static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
661 struct seq_file *s,
662 unsigned offset)
663{
664 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
Linus Walleij85ae9e52016-11-14 18:48:19 +0100665 struct gpio_chip *chip = &pc->gpio_chip;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600666 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
667 const char *fname = bcm2835_functions[fsel];
668 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
Linus Walleij85ae9e52016-11-14 18:48:19 +0100669 int irq = irq_find_mapping(chip->irqdomain, offset);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600670
671 seq_printf(s, "function %s in %s; irq %d (%s)",
672 fname, value ? "hi" : "lo",
673 irq, irq_type_names[pc->irq_type[offset]]);
674}
675
676static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
677 struct pinctrl_map *maps, unsigned num_maps)
678{
679 int i;
680
681 for (i = 0; i < num_maps; i++)
682 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
683 kfree(maps[i].data.configs.configs);
684
685 kfree(maps);
686}
687
688static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
689 struct device_node *np, u32 pin, u32 fnum,
690 struct pinctrl_map **maps)
691{
692 struct pinctrl_map *map = *maps;
693
694 if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
Rob Herringf5292d02017-07-18 16:43:23 -0500695 dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600696 return -EINVAL;
697 }
698
699 map->type = PIN_MAP_TYPE_MUX_GROUP;
700 map->data.mux.group = bcm2835_gpio_groups[pin];
701 map->data.mux.function = bcm2835_functions[fnum];
702 (*maps)++;
703
704 return 0;
705}
706
707static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
708 struct device_node *np, u32 pin, u32 pull,
709 struct pinctrl_map **maps)
710{
711 struct pinctrl_map *map = *maps;
712 unsigned long *configs;
713
714 if (pull > 2) {
Rob Herringf5292d02017-07-18 16:43:23 -0500715 dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600716 return -EINVAL;
717 }
718
719 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
720 if (!configs)
721 return -ENOMEM;
722 configs[0] = BCM2835_PINCONF_PACK(BCM2835_PINCONF_PARAM_PULL, pull);
723
724 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
725 map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
726 map->data.configs.configs = configs;
727 map->data.configs.num_configs = 1;
728 (*maps)++;
729
730 return 0;
731}
732
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600733static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
734 struct device_node *np,
735 struct pinctrl_map **map, unsigned *num_maps)
736{
737 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
738 struct property *pins, *funcs, *pulls;
739 int num_pins, num_funcs, num_pulls, maps_per_pin;
740 struct pinctrl_map *maps, *cur_map;
741 int i, err;
742 u32 pin, func, pull;
743
744 pins = of_find_property(np, "brcm,pins", NULL);
745 if (!pins) {
Rob Herringf5292d02017-07-18 16:43:23 -0500746 dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600747 return -EINVAL;
748 }
749
750 funcs = of_find_property(np, "brcm,function", NULL);
751 pulls = of_find_property(np, "brcm,pull", NULL);
752
753 if (!funcs && !pulls) {
754 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500755 "%pOF: neither brcm,function nor brcm,pull specified\n",
756 np);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600757 return -EINVAL;
758 }
759
760 num_pins = pins->length / 4;
761 num_funcs = funcs ? (funcs->length / 4) : 0;
762 num_pulls = pulls ? (pulls->length / 4) : 0;
763
764 if (num_funcs > 1 && num_funcs != num_pins) {
765 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500766 "%pOF: brcm,function must have 1 or %d entries\n",
767 np, num_pins);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600768 return -EINVAL;
769 }
770
771 if (num_pulls > 1 && num_pulls != num_pins) {
772 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500773 "%pOF: brcm,pull must have 1 or %d entries\n",
774 np, num_pins);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600775 return -EINVAL;
776 }
777
778 maps_per_pin = 0;
779 if (num_funcs)
780 maps_per_pin++;
781 if (num_pulls)
782 maps_per_pin++;
783 cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
784 GFP_KERNEL);
785 if (!maps)
786 return -ENOMEM;
787
788 for (i = 0; i < num_pins; i++) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000789 err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
790 if (err)
791 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600792 if (pin >= ARRAY_SIZE(bcm2835_gpio_pins)) {
Rob Herringf5292d02017-07-18 16:43:23 -0500793 dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
794 np, pin);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600795 err = -EINVAL;
796 goto out;
797 }
798
799 if (num_funcs) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000800 err = of_property_read_u32_index(np, "brcm,function",
801 (num_funcs > 1) ? i : 0, &func);
802 if (err)
803 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600804 err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
805 func, &cur_map);
806 if (err)
807 goto out;
808 }
809 if (num_pulls) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000810 err = of_property_read_u32_index(np, "brcm,pull",
Phil Elwell2c7e3302016-02-29 17:30:08 -0800811 (num_pulls > 1) ? i : 0, &pull);
Stephen Warrence63d6d2013-03-28 05:09:57 +0000812 if (err)
813 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600814 err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
815 pull, &cur_map);
816 if (err)
817 goto out;
818 }
819 }
820
821 *map = maps;
822 *num_maps = num_pins * maps_per_pin;
823
824 return 0;
825
826out:
Stefan Wahren53653c62015-12-21 00:44:04 +0000827 bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600828 return err;
829}
830
Laurent Pinchart022ab142013-02-16 10:25:07 +0100831static const struct pinctrl_ops bcm2835_pctl_ops = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600832 .get_groups_count = bcm2835_pctl_get_groups_count,
833 .get_group_name = bcm2835_pctl_get_group_name,
834 .get_group_pins = bcm2835_pctl_get_group_pins,
835 .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
836 .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
837 .dt_free_map = bcm2835_pctl_dt_free_map,
838};
839
Phil Elwellccca1ad2016-05-06 12:32:47 +0100840static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
841 unsigned offset)
842{
843 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
844
845 /* disable by setting to GPIO_IN */
846 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
847 return 0;
848}
849
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600850static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
851{
852 return BCM2835_FSEL_COUNT;
853}
854
855static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
856 unsigned selector)
857{
858 return bcm2835_functions[selector];
859}
860
861static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
862 unsigned selector,
863 const char * const **groups,
864 unsigned * const num_groups)
865{
866 /* every pin can do every function */
867 *groups = bcm2835_gpio_groups;
868 *num_groups = ARRAY_SIZE(bcm2835_gpio_groups);
869
870 return 0;
871}
872
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200873static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600874 unsigned func_selector,
875 unsigned group_selector)
876{
877 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
878
879 bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
880
881 return 0;
882}
883
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600884static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
885 struct pinctrl_gpio_range *range,
886 unsigned offset)
887{
888 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
889
890 /* disable by setting to GPIO_IN */
891 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
892}
893
894static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
895 struct pinctrl_gpio_range *range,
896 unsigned offset,
897 bool input)
898{
899 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
900 enum bcm2835_fsel fsel = input ?
901 BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
902
903 bcm2835_pinctrl_fsel_set(pc, offset, fsel);
904
905 return 0;
906}
907
Laurent Pinchart022ab142013-02-16 10:25:07 +0100908static const struct pinmux_ops bcm2835_pmx_ops = {
Phil Elwellccca1ad2016-05-06 12:32:47 +0100909 .free = bcm2835_pmx_free,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600910 .get_functions_count = bcm2835_pmx_get_functions_count,
911 .get_function_name = bcm2835_pmx_get_function_name,
912 .get_function_groups = bcm2835_pmx_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200913 .set_mux = bcm2835_pmx_set,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600914 .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
915 .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
916};
917
918static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
919 unsigned pin, unsigned long *config)
920{
921 /* No way to read back config in HW */
922 return -ENOTSUPP;
923}
924
925static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700926 unsigned pin, unsigned long *configs,
927 unsigned num_configs)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600928{
929 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
Sherman Yin03b054e2013-08-27 11:32:12 -0700930 enum bcm2835_pinconf_param param;
931 u16 arg;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600932 u32 off, bit;
Sherman Yin03b054e2013-08-27 11:32:12 -0700933 int i;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600934
Sherman Yin03b054e2013-08-27 11:32:12 -0700935 for (i = 0; i < num_configs; i++) {
936 param = BCM2835_PINCONF_UNPACK_PARAM(configs[i]);
937 arg = BCM2835_PINCONF_UNPACK_ARG(configs[i]);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600938
Sherman Yin03b054e2013-08-27 11:32:12 -0700939 if (param != BCM2835_PINCONF_PARAM_PULL)
940 return -EINVAL;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600941
Sherman Yin03b054e2013-08-27 11:32:12 -0700942 off = GPIO_REG_OFFSET(pin);
943 bit = GPIO_REG_SHIFT(pin);
944
945 bcm2835_gpio_wr(pc, GPPUD, arg & 3);
946 /*
Stefan Wahrenb83bd892016-10-31 13:21:33 +0000947 * BCM2835 datasheet say to wait 150 cycles, but not of what.
948 * But the VideoCore firmware delay for this operation
949 * based nearly on the same amount of VPU cycles and this clock
950 * runs at 250 MHz.
Sherman Yin03b054e2013-08-27 11:32:12 -0700951 */
Stefan Wahrenb83bd892016-10-31 13:21:33 +0000952 udelay(1);
Sherman Yin03b054e2013-08-27 11:32:12 -0700953 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
Stefan Wahrenb83bd892016-10-31 13:21:33 +0000954 udelay(1);
Sherman Yin03b054e2013-08-27 11:32:12 -0700955 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
956 } /* for each config */
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600957
958 return 0;
959}
960
Laurent Pinchart022ab142013-02-16 10:25:07 +0100961static const struct pinconf_ops bcm2835_pinconf_ops = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600962 .pin_config_get = bcm2835_pinconf_get,
963 .pin_config_set = bcm2835_pinconf_set,
964};
965
966static struct pinctrl_desc bcm2835_pinctrl_desc = {
967 .name = MODULE_NAME,
968 .pins = bcm2835_gpio_pins,
969 .npins = ARRAY_SIZE(bcm2835_gpio_pins),
970 .pctlops = &bcm2835_pctl_ops,
971 .pmxops = &bcm2835_pmx_ops,
972 .confops = &bcm2835_pinconf_ops,
973 .owner = THIS_MODULE,
974};
975
Bill Pemberton84db00b2012-11-19 13:25:13 -0500976static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600977 .name = MODULE_NAME,
978 .npins = BCM2835_NUM_GPIOS,
979};
980
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800981static int bcm2835_pinctrl_probe(struct platform_device *pdev)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600982{
983 struct device *dev = &pdev->dev;
984 struct device_node *np = dev->of_node;
985 struct bcm2835_pinctrl *pc;
986 struct resource iomem;
987 int err, i;
988 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2835_NUM_GPIOS);
989 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2835_NUM_GPIOS);
990
991 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
992 if (!pc)
993 return -ENOMEM;
994
995 platform_set_drvdata(pdev, pc);
996 pc->dev = dev;
997
998 err = of_address_to_resource(np, 0, &iomem);
999 if (err) {
1000 dev_err(dev, "could not get IO memory\n");
1001 return err;
1002 }
1003
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001004 pc->base = devm_ioremap_resource(dev, &iomem);
1005 if (IS_ERR(pc->base))
1006 return PTR_ERR(pc->base);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001007
1008 pc->gpio_chip = bcm2835_gpio_chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001009 pc->gpio_chip.parent = dev;
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001010 pc->gpio_chip.of_node = np;
1011
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001012 for (i = 0; i < BCM2835_NUM_BANKS; i++) {
1013 unsigned long events;
1014 unsigned offset;
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001015
1016 /* clear event detection flags */
1017 bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
1018 bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
1019 bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
1020 bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
1021 bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
1022 bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
1023
1024 /* clear all the events */
1025 events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
1026 for_each_set_bit(offset, &events, 32)
1027 bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
1028
Phil Elwell00445b52015-02-24 13:40:50 +00001029 spin_lock_init(&pc->irq_lock[i]);
1030 }
1031
Linus Walleije19a5f72015-12-08 22:01:00 +01001032 err = gpiochip_add_data(&pc->gpio_chip, pc);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001033 if (err) {
1034 dev_err(dev, "could not add GPIO chip\n");
1035 return err;
1036 }
1037
Linus Walleij85ae9e52016-11-14 18:48:19 +01001038 err = gpiochip_irqchip_add(&pc->gpio_chip, &bcm2835_gpio_irq_chip,
1039 0, handle_level_irq, IRQ_TYPE_NONE);
1040 if (err) {
1041 dev_info(dev, "could not add irqchip\n");
1042 return err;
1043 }
1044
1045 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
1046 pc->irq[i] = irq_of_parse_and_map(np, i);
1047 pc->irq_group[i] = i;
Stefan Wahren37a2f8e2017-06-21 20:20:04 +02001048
1049 if (pc->irq[i] == 0)
1050 continue;
1051
Linus Walleij85ae9e52016-11-14 18:48:19 +01001052 /*
1053 * Use the same handler for all groups: this is necessary
1054 * since we use one gpiochip to cover all lines - the
1055 * irq handler then needs to figure out which group and
1056 * bank that was firing the IRQ and look up the per-group
1057 * and bank data.
1058 */
1059 gpiochip_set_chained_irqchip(&pc->gpio_chip,
1060 &bcm2835_gpio_irq_chip,
1061 pc->irq[i],
1062 bcm2835_gpio_irq_handler);
1063 }
1064
Laxman Dewangan5f276f62016-02-24 14:44:07 +05301065 pc->pctl_dev = devm_pinctrl_register(dev, &bcm2835_pinctrl_desc, pc);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001066 if (IS_ERR(pc->pctl_dev)) {
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001067 gpiochip_remove(&pc->gpio_chip);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001068 return PTR_ERR(pc->pctl_dev);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001069 }
1070
1071 pc->gpio_range = bcm2835_pinctrl_gpio_range;
1072 pc->gpio_range.base = pc->gpio_chip.base;
1073 pc->gpio_range.gc = &pc->gpio_chip;
1074 pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1075
1076 return 0;
1077}
1078
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001079static const struct of_device_id bcm2835_pinctrl_match[] = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001080 { .compatible = "brcm,bcm2835-gpio" },
1081 {}
1082};
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001083
1084static struct platform_driver bcm2835_pinctrl_driver = {
1085 .probe = bcm2835_pinctrl_probe,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001086 .driver = {
1087 .name = MODULE_NAME,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001088 .of_match_table = bcm2835_pinctrl_match,
Paul Gortmaker34f46842017-05-22 16:56:48 -04001089 .suppress_bind_attrs = true,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001090 },
1091};
Paul Gortmaker34f46842017-05-22 16:56:48 -04001092builtin_platform_driver(bcm2835_pinctrl_driver);