blob: 136ccaf53df8d88cc5cc409f01cea24b40c50edd [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>
Matheus Castello0de70492018-04-30 20:42:13 -040039#include <linux/pinctrl/pinconf-generic.h>
Simon Arlotte1b2dc72012-09-27 22:10:11 -060040#include <linux/platform_device.h>
41#include <linux/seq_file.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/types.h>
Matheus Castello0de70492018-04-30 20:42:13 -040045#include <dt-bindings/pinctrl/bcm2835.h>
Simon Arlotte1b2dc72012-09-27 22:10:11 -060046
47#define MODULE_NAME "pinctrl-bcm2835"
48#define BCM2835_NUM_GPIOS 54
49#define BCM2835_NUM_BANKS 2
Phil Elwell00445b52015-02-24 13:40:50 +000050#define BCM2835_NUM_IRQS 3
Simon Arlotte1b2dc72012-09-27 22:10:11 -060051
52#define BCM2835_PIN_BITMAP_SZ \
53 DIV_ROUND_UP(BCM2835_NUM_GPIOS, sizeof(unsigned long) * 8)
54
55/* GPIO register offsets */
56#define GPFSEL0 0x0 /* Function Select */
57#define GPSET0 0x1c /* Pin Output Set */
58#define GPCLR0 0x28 /* Pin Output Clear */
59#define GPLEV0 0x34 /* Pin Level */
60#define GPEDS0 0x40 /* Pin Event Detect Status */
61#define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
62#define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
63#define GPHEN0 0x64 /* Pin High Detect Enable */
64#define GPLEN0 0x70 /* Pin Low Detect Enable */
65#define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
66#define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
67#define GPPUD 0x94 /* Pin Pull-up/down Enable */
68#define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
69
70#define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
71#define FSEL_SHIFT(p) (((p) % 10) * 3)
72#define GPIO_REG_OFFSET(p) ((p) / 32)
73#define GPIO_REG_SHIFT(p) ((p) % 32)
74
75enum bcm2835_pinconf_param {
76 /* argument: bcm2835_pinconf_pull */
Matheus Castello0de70492018-04-30 20:42:13 -040077 BCM2835_PINCONF_PARAM_PULL = (PIN_CONFIG_END + 1),
Simon Arlotte1b2dc72012-09-27 22:10:11 -060078};
79
Simon Arlotte1b2dc72012-09-27 22:10:11 -060080struct bcm2835_pinctrl {
81 struct device *dev;
82 void __iomem *base;
Phil Elwell00445b52015-02-24 13:40:50 +000083 int irq[BCM2835_NUM_IRQS];
Simon Arlotte1b2dc72012-09-27 22:10:11 -060084
85 /* note: locking assumes each bank will have its own unsigned long */
86 unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
87 unsigned int irq_type[BCM2835_NUM_GPIOS];
88
89 struct pinctrl_dev *pctl_dev;
Simon Arlotte1b2dc72012-09-27 22:10:11 -060090 struct gpio_chip gpio_chip;
91 struct pinctrl_gpio_range gpio_range;
92
Simon Arlotte1b2dc72012-09-27 22:10:11 -060093 spinlock_t irq_lock[BCM2835_NUM_BANKS];
94};
95
Simon Arlotte1b2dc72012-09-27 22:10:11 -060096/* pins are just named GPIO0..GPIO53 */
97#define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
Sachin Kamate8ed9122013-06-18 14:34:24 +053098static struct pinctrl_pin_desc bcm2835_gpio_pins[] = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -060099 BCM2835_GPIO_PIN(0),
100 BCM2835_GPIO_PIN(1),
101 BCM2835_GPIO_PIN(2),
102 BCM2835_GPIO_PIN(3),
103 BCM2835_GPIO_PIN(4),
104 BCM2835_GPIO_PIN(5),
105 BCM2835_GPIO_PIN(6),
106 BCM2835_GPIO_PIN(7),
107 BCM2835_GPIO_PIN(8),
108 BCM2835_GPIO_PIN(9),
109 BCM2835_GPIO_PIN(10),
110 BCM2835_GPIO_PIN(11),
111 BCM2835_GPIO_PIN(12),
112 BCM2835_GPIO_PIN(13),
113 BCM2835_GPIO_PIN(14),
114 BCM2835_GPIO_PIN(15),
115 BCM2835_GPIO_PIN(16),
116 BCM2835_GPIO_PIN(17),
117 BCM2835_GPIO_PIN(18),
118 BCM2835_GPIO_PIN(19),
119 BCM2835_GPIO_PIN(20),
120 BCM2835_GPIO_PIN(21),
121 BCM2835_GPIO_PIN(22),
122 BCM2835_GPIO_PIN(23),
123 BCM2835_GPIO_PIN(24),
124 BCM2835_GPIO_PIN(25),
125 BCM2835_GPIO_PIN(26),
126 BCM2835_GPIO_PIN(27),
127 BCM2835_GPIO_PIN(28),
128 BCM2835_GPIO_PIN(29),
129 BCM2835_GPIO_PIN(30),
130 BCM2835_GPIO_PIN(31),
131 BCM2835_GPIO_PIN(32),
132 BCM2835_GPIO_PIN(33),
133 BCM2835_GPIO_PIN(34),
134 BCM2835_GPIO_PIN(35),
135 BCM2835_GPIO_PIN(36),
136 BCM2835_GPIO_PIN(37),
137 BCM2835_GPIO_PIN(38),
138 BCM2835_GPIO_PIN(39),
139 BCM2835_GPIO_PIN(40),
140 BCM2835_GPIO_PIN(41),
141 BCM2835_GPIO_PIN(42),
142 BCM2835_GPIO_PIN(43),
143 BCM2835_GPIO_PIN(44),
144 BCM2835_GPIO_PIN(45),
145 BCM2835_GPIO_PIN(46),
146 BCM2835_GPIO_PIN(47),
147 BCM2835_GPIO_PIN(48),
148 BCM2835_GPIO_PIN(49),
149 BCM2835_GPIO_PIN(50),
150 BCM2835_GPIO_PIN(51),
151 BCM2835_GPIO_PIN(52),
152 BCM2835_GPIO_PIN(53),
153};
154
155/* one pin per group */
156static const char * const bcm2835_gpio_groups[] = {
157 "gpio0",
158 "gpio1",
159 "gpio2",
160 "gpio3",
161 "gpio4",
162 "gpio5",
163 "gpio6",
164 "gpio7",
165 "gpio8",
166 "gpio9",
167 "gpio10",
168 "gpio11",
169 "gpio12",
170 "gpio13",
171 "gpio14",
172 "gpio15",
173 "gpio16",
174 "gpio17",
175 "gpio18",
176 "gpio19",
177 "gpio20",
178 "gpio21",
179 "gpio22",
180 "gpio23",
181 "gpio24",
182 "gpio25",
183 "gpio26",
184 "gpio27",
185 "gpio28",
186 "gpio29",
187 "gpio30",
188 "gpio31",
189 "gpio32",
190 "gpio33",
191 "gpio34",
192 "gpio35",
193 "gpio36",
194 "gpio37",
195 "gpio38",
196 "gpio39",
197 "gpio40",
198 "gpio41",
199 "gpio42",
200 "gpio43",
201 "gpio44",
202 "gpio45",
203 "gpio46",
204 "gpio47",
205 "gpio48",
206 "gpio49",
207 "gpio50",
208 "gpio51",
209 "gpio52",
210 "gpio53",
211};
212
213enum bcm2835_fsel {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600214 BCM2835_FSEL_COUNT = 8,
215 BCM2835_FSEL_MASK = 0x7,
216};
217
218static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
219 [BCM2835_FSEL_GPIO_IN] = "gpio_in",
220 [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
221 [BCM2835_FSEL_ALT0] = "alt0",
222 [BCM2835_FSEL_ALT1] = "alt1",
223 [BCM2835_FSEL_ALT2] = "alt2",
224 [BCM2835_FSEL_ALT3] = "alt3",
225 [BCM2835_FSEL_ALT4] = "alt4",
226 [BCM2835_FSEL_ALT5] = "alt5",
227};
228
229static const char * const irq_type_names[] = {
230 [IRQ_TYPE_NONE] = "none",
231 [IRQ_TYPE_EDGE_RISING] = "edge-rising",
232 [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
233 [IRQ_TYPE_EDGE_BOTH] = "edge-both",
234 [IRQ_TYPE_LEVEL_HIGH] = "level-high",
235 [IRQ_TYPE_LEVEL_LOW] = "level-low",
236};
237
238static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
239{
240 return readl(pc->base + reg);
241}
242
243static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
244 u32 val)
245{
246 writel(val, pc->base + reg);
247}
248
249static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
250 unsigned bit)
251{
252 reg += GPIO_REG_OFFSET(bit) * 4;
253 return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
254}
255
256/* note NOT a read/modify/write cycle */
257static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
258 unsigned reg, unsigned bit)
259{
260 reg += GPIO_REG_OFFSET(bit) * 4;
261 bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
262}
263
264static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
265 struct bcm2835_pinctrl *pc, unsigned pin)
266{
267 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
268 enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
269
270 dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
271 bcm2835_functions[status]);
272
273 return status;
274}
275
276static inline void bcm2835_pinctrl_fsel_set(
277 struct bcm2835_pinctrl *pc, unsigned pin,
278 enum bcm2835_fsel fsel)
279{
280 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
281 enum bcm2835_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
282
283 dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
284 bcm2835_functions[cur]);
285
286 if (cur == fsel)
287 return;
288
289 if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
290 /* always transition through GPIO_IN */
291 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
292 val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
293
294 dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
295 bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
296 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
297 }
298
299 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
300 val |= fsel << FSEL_SHIFT(pin);
301
302 dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
303 bcm2835_functions[fsel]);
304 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
305}
306
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600307static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
308{
309 return pinctrl_gpio_direction_input(chip->base + offset);
310}
311
312static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
313{
Linus Walleije19a5f72015-12-08 22:01:00 +0100314 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600315
316 return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
317}
318
Stefan Wahren20b3d2a2016-03-28 14:58:24 +0000319static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
320{
321 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
322 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
323
324 /* Alternative function doesn't clearly provide a direction */
325 if (fsel > BCM2835_FSEL_GPIO_OUT)
326 return -EINVAL;
327
328 return (fsel == BCM2835_FSEL_GPIO_IN);
329}
330
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600331static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
332{
Linus Walleije19a5f72015-12-08 22:01:00 +0100333 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600334
335 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
336}
337
Stefan Wahren4c02cba2015-11-19 00:32:27 +0000338static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
339 unsigned offset, int value)
340{
341 bcm2835_gpio_set(chip, offset, value);
342 return pinctrl_gpio_direction_output(chip->base + offset);
343}
344
Gustavo A. R. Silva531bcf72017-07-11 13:03:39 -0500345static const struct gpio_chip bcm2835_gpio_chip = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600346 .label = MODULE_NAME,
347 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200348 .request = gpiochip_generic_request,
349 .free = gpiochip_generic_free,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600350 .direction_input = bcm2835_gpio_direction_input,
351 .direction_output = bcm2835_gpio_direction_output,
Stefan Wahren20b3d2a2016-03-28 14:58:24 +0000352 .get_direction = bcm2835_gpio_get_direction,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600353 .get = bcm2835_gpio_get,
354 .set = bcm2835_gpio_set,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600355 .base = -1,
356 .ngpio = BCM2835_NUM_GPIOS,
Linus Walleij9fb1f392013-12-04 14:42:46 +0100357 .can_sleep = false,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600358};
359
Linus Walleij85ae9e52016-11-14 18:48:19 +0100360static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
361 unsigned int bank, u32 mask)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600362{
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600363 unsigned long events;
364 unsigned offset;
365 unsigned gpio;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600366
367 events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
Phil Elwell00445b52015-02-24 13:40:50 +0000368 events &= mask;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600369 events &= pc->enabled_irq_map[bank];
370 for_each_set_bit(offset, &events, 32) {
371 gpio = (32 * bank) + offset;
Linus Walleij9e9355b2017-11-09 09:36:07 +0100372 generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irq.domain,
Linus Walleij85ae9e52016-11-14 18:48:19 +0100373 gpio));
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600374 }
Phil Elwell00445b52015-02-24 13:40:50 +0000375}
376
Linus Walleij85ae9e52016-11-14 18:48:19 +0100377static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
Phil Elwell00445b52015-02-24 13:40:50 +0000378{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100379 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
380 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
381 struct irq_chip *host_chip = irq_desc_get_chip(desc);
382 int irq = irq_desc_get_irq(desc);
383 int group;
384 int i;
Phil Elwell00445b52015-02-24 13:40:50 +0000385
Linus Walleij85ae9e52016-11-14 18:48:19 +0100386 for (i = 0; i < ARRAY_SIZE(pc->irq); i++) {
387 if (pc->irq[i] == irq) {
Thierry Reding0d885e92017-07-20 18:59:12 +0200388 group = i;
Linus Walleij85ae9e52016-11-14 18:48:19 +0100389 break;
390 }
391 }
392 /* This should not happen, every IRQ has a bank */
393 if (i == ARRAY_SIZE(pc->irq))
394 BUG();
395
396 chained_irq_enter(host_chip, desc);
397
398 switch (group) {
Phil Elwell00445b52015-02-24 13:40:50 +0000399 case 0: /* IRQ0 covers GPIOs 0-27 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100400 bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
Phil Elwell00445b52015-02-24 13:40:50 +0000401 break;
402 case 1: /* IRQ1 covers GPIOs 28-45 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100403 bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
404 bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
Phil Elwell00445b52015-02-24 13:40:50 +0000405 break;
406 case 2: /* IRQ2 covers GPIOs 46-53 */
Linus Walleij85ae9e52016-11-14 18:48:19 +0100407 bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
Phil Elwell00445b52015-02-24 13:40:50 +0000408 break;
409 }
410
Linus Walleij85ae9e52016-11-14 18:48:19 +0100411 chained_irq_exit(host_chip, desc);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600412}
413
414static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
415 unsigned reg, unsigned offset, bool enable)
416{
417 u32 value;
418 reg += GPIO_REG_OFFSET(offset) * 4;
419 value = bcm2835_gpio_rd(pc, reg);
420 if (enable)
421 value |= BIT(GPIO_REG_SHIFT(offset));
422 else
423 value &= ~(BIT(GPIO_REG_SHIFT(offset)));
424 bcm2835_gpio_wr(pc, reg, value);
425}
426
427/* fast path for IRQ handler */
428static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
429 unsigned offset, bool enable)
430{
431 switch (pc->irq_type[offset]) {
432 case IRQ_TYPE_EDGE_RISING:
433 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
434 break;
435
436 case IRQ_TYPE_EDGE_FALLING:
437 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
438 break;
439
440 case IRQ_TYPE_EDGE_BOTH:
441 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
442 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
443 break;
444
445 case IRQ_TYPE_LEVEL_HIGH:
446 __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
447 break;
448
449 case IRQ_TYPE_LEVEL_LOW:
450 __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
451 break;
452 }
453}
454
455static void bcm2835_gpio_irq_enable(struct irq_data *data)
456{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100457 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
458 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600459 unsigned gpio = irqd_to_hwirq(data);
460 unsigned offset = GPIO_REG_SHIFT(gpio);
461 unsigned bank = GPIO_REG_OFFSET(gpio);
462 unsigned long flags;
463
464 spin_lock_irqsave(&pc->irq_lock[bank], flags);
465 set_bit(offset, &pc->enabled_irq_map[bank]);
466 bcm2835_gpio_irq_config(pc, gpio, true);
467 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
468}
469
470static void bcm2835_gpio_irq_disable(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 bcm2835_gpio_irq_config(pc, gpio, false);
Jonathan Bell714b1dd2015-06-30 12:35:39 +0100481 /* Clear events that were latched prior to clearing event sources */
482 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600483 clear_bit(offset, &pc->enabled_irq_map[bank]);
484 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
485}
486
487static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
488 unsigned offset, unsigned int type)
489{
490 switch (type) {
491 case IRQ_TYPE_NONE:
492 case IRQ_TYPE_EDGE_RISING:
493 case IRQ_TYPE_EDGE_FALLING:
494 case IRQ_TYPE_EDGE_BOTH:
495 case IRQ_TYPE_LEVEL_HIGH:
496 case IRQ_TYPE_LEVEL_LOW:
497 pc->irq_type[offset] = type;
498 break;
499
500 default:
501 return -EINVAL;
502 }
503 return 0;
504}
505
506/* slower path for reconfiguring IRQ type */
507static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
508 unsigned offset, unsigned int type)
509{
510 switch (type) {
511 case IRQ_TYPE_NONE:
512 if (pc->irq_type[offset] != type) {
513 bcm2835_gpio_irq_config(pc, offset, false);
514 pc->irq_type[offset] = type;
515 }
516 break;
517
518 case IRQ_TYPE_EDGE_RISING:
519 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
520 /* RISING already enabled, disable FALLING */
521 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
522 bcm2835_gpio_irq_config(pc, offset, false);
523 pc->irq_type[offset] = type;
524 } else if (pc->irq_type[offset] != type) {
525 bcm2835_gpio_irq_config(pc, offset, false);
526 pc->irq_type[offset] = type;
527 bcm2835_gpio_irq_config(pc, offset, true);
528 }
529 break;
530
531 case IRQ_TYPE_EDGE_FALLING:
532 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
533 /* FALLING already enabled, disable RISING */
534 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
535 bcm2835_gpio_irq_config(pc, offset, false);
536 pc->irq_type[offset] = type;
537 } else if (pc->irq_type[offset] != type) {
538 bcm2835_gpio_irq_config(pc, offset, false);
539 pc->irq_type[offset] = type;
540 bcm2835_gpio_irq_config(pc, offset, true);
541 }
542 break;
543
544 case IRQ_TYPE_EDGE_BOTH:
545 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
546 /* RISING already enabled, enable FALLING too */
547 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
548 bcm2835_gpio_irq_config(pc, offset, true);
549 pc->irq_type[offset] = type;
550 } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
551 /* FALLING already enabled, enable RISING too */
552 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
553 bcm2835_gpio_irq_config(pc, offset, true);
554 pc->irq_type[offset] = type;
555 } else if (pc->irq_type[offset] != type) {
556 bcm2835_gpio_irq_config(pc, offset, false);
557 pc->irq_type[offset] = type;
558 bcm2835_gpio_irq_config(pc, offset, true);
559 }
560 break;
561
562 case IRQ_TYPE_LEVEL_HIGH:
563 case IRQ_TYPE_LEVEL_LOW:
564 if (pc->irq_type[offset] != type) {
565 bcm2835_gpio_irq_config(pc, offset, false);
566 pc->irq_type[offset] = type;
567 bcm2835_gpio_irq_config(pc, offset, true);
568 }
569 break;
570
571 default:
572 return -EINVAL;
573 }
574 return 0;
575}
576
577static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
578{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100579 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
580 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600581 unsigned gpio = irqd_to_hwirq(data);
582 unsigned offset = GPIO_REG_SHIFT(gpio);
583 unsigned bank = GPIO_REG_OFFSET(gpio);
584 unsigned long flags;
585 int ret;
586
587 spin_lock_irqsave(&pc->irq_lock[bank], flags);
588
589 if (test_bit(offset, &pc->enabled_irq_map[bank]))
590 ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
591 else
592 ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
593
Charles Keepaxb8a19382015-04-07 11:43:45 +0100594 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixner1aa74fd2015-06-23 15:52:41 +0200595 irq_set_handler_locked(data, handle_edge_irq);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100596 else
Thomas Gleixner1aa74fd2015-06-23 15:52:41 +0200597 irq_set_handler_locked(data, handle_level_irq);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100598
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600599 spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
600
601 return ret;
602}
603
Charles Keepaxb8a19382015-04-07 11:43:45 +0100604static void bcm2835_gpio_irq_ack(struct irq_data *data)
605{
Linus Walleij85ae9e52016-11-14 18:48:19 +0100606 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
607 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
Charles Keepaxb8a19382015-04-07 11:43:45 +0100608 unsigned gpio = irqd_to_hwirq(data);
609
610 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
611}
612
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600613static struct irq_chip bcm2835_gpio_irq_chip = {
614 .name = MODULE_NAME,
615 .irq_enable = bcm2835_gpio_irq_enable,
616 .irq_disable = bcm2835_gpio_irq_disable,
617 .irq_set_type = bcm2835_gpio_irq_set_type,
Charles Keepaxb8a19382015-04-07 11:43:45 +0100618 .irq_ack = bcm2835_gpio_irq_ack,
619 .irq_mask = bcm2835_gpio_irq_disable,
620 .irq_unmask = bcm2835_gpio_irq_enable,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600621};
622
623static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
624{
625 return ARRAY_SIZE(bcm2835_gpio_groups);
626}
627
628static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
629 unsigned selector)
630{
631 return bcm2835_gpio_groups[selector];
632}
633
634static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
635 unsigned selector,
636 const unsigned **pins,
637 unsigned *num_pins)
638{
639 *pins = &bcm2835_gpio_pins[selector].number;
640 *num_pins = 1;
641
642 return 0;
643}
644
645static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
646 struct seq_file *s,
647 unsigned offset)
648{
649 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
Linus Walleij85ae9e52016-11-14 18:48:19 +0100650 struct gpio_chip *chip = &pc->gpio_chip;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600651 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
652 const char *fname = bcm2835_functions[fsel];
653 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100654 int irq = irq_find_mapping(chip->irq.domain, offset);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600655
656 seq_printf(s, "function %s in %s; irq %d (%s)",
657 fname, value ? "hi" : "lo",
658 irq, irq_type_names[pc->irq_type[offset]]);
659}
660
661static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
662 struct pinctrl_map *maps, unsigned num_maps)
663{
664 int i;
665
666 for (i = 0; i < num_maps; i++)
667 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
668 kfree(maps[i].data.configs.configs);
669
670 kfree(maps);
671}
672
673static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
674 struct device_node *np, u32 pin, u32 fnum,
675 struct pinctrl_map **maps)
676{
677 struct pinctrl_map *map = *maps;
678
679 if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
Rob Herringf5292d02017-07-18 16:43:23 -0500680 dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600681 return -EINVAL;
682 }
683
684 map->type = PIN_MAP_TYPE_MUX_GROUP;
685 map->data.mux.group = bcm2835_gpio_groups[pin];
686 map->data.mux.function = bcm2835_functions[fnum];
687 (*maps)++;
688
689 return 0;
690}
691
692static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
693 struct device_node *np, u32 pin, u32 pull,
694 struct pinctrl_map **maps)
695{
696 struct pinctrl_map *map = *maps;
697 unsigned long *configs;
698
699 if (pull > 2) {
Rob Herringf5292d02017-07-18 16:43:23 -0500700 dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600701 return -EINVAL;
702 }
703
704 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
705 if (!configs)
706 return -ENOMEM;
Matheus Castello0de70492018-04-30 20:42:13 -0400707 configs[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL, pull);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600708
709 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
710 map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
711 map->data.configs.configs = configs;
712 map->data.configs.num_configs = 1;
713 (*maps)++;
714
715 return 0;
716}
717
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600718static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
719 struct device_node *np,
Matheus Castello0de70492018-04-30 20:42:13 -0400720 struct pinctrl_map **map, unsigned int *num_maps)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600721{
722 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
723 struct property *pins, *funcs, *pulls;
724 int num_pins, num_funcs, num_pulls, maps_per_pin;
725 struct pinctrl_map *maps, *cur_map;
726 int i, err;
727 u32 pin, func, pull;
728
Matheus Castello0de70492018-04-30 20:42:13 -0400729 /* Check for generic binding in this node */
730 err = pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
731 if (err || *num_maps)
732 return err;
733
734 /* Generic binding did not find anything continue with legacy parse */
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600735 pins = of_find_property(np, "brcm,pins", NULL);
736 if (!pins) {
Rob Herringf5292d02017-07-18 16:43:23 -0500737 dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600738 return -EINVAL;
739 }
740
741 funcs = of_find_property(np, "brcm,function", NULL);
742 pulls = of_find_property(np, "brcm,pull", NULL);
743
744 if (!funcs && !pulls) {
745 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500746 "%pOF: neither brcm,function nor brcm,pull specified\n",
747 np);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600748 return -EINVAL;
749 }
750
751 num_pins = pins->length / 4;
752 num_funcs = funcs ? (funcs->length / 4) : 0;
753 num_pulls = pulls ? (pulls->length / 4) : 0;
754
755 if (num_funcs > 1 && num_funcs != num_pins) {
756 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500757 "%pOF: brcm,function must have 1 or %d entries\n",
758 np, num_pins);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600759 return -EINVAL;
760 }
761
762 if (num_pulls > 1 && num_pulls != num_pins) {
763 dev_err(pc->dev,
Rob Herringf5292d02017-07-18 16:43:23 -0500764 "%pOF: brcm,pull must have 1 or %d entries\n",
765 np, num_pins);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600766 return -EINVAL;
767 }
768
769 maps_per_pin = 0;
770 if (num_funcs)
771 maps_per_pin++;
772 if (num_pulls)
773 maps_per_pin++;
774 cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
775 GFP_KERNEL);
776 if (!maps)
777 return -ENOMEM;
778
779 for (i = 0; i < num_pins; i++) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000780 err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
781 if (err)
782 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600783 if (pin >= ARRAY_SIZE(bcm2835_gpio_pins)) {
Rob Herringf5292d02017-07-18 16:43:23 -0500784 dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
785 np, pin);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600786 err = -EINVAL;
787 goto out;
788 }
789
790 if (num_funcs) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000791 err = of_property_read_u32_index(np, "brcm,function",
792 (num_funcs > 1) ? i : 0, &func);
793 if (err)
794 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600795 err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
796 func, &cur_map);
797 if (err)
798 goto out;
799 }
800 if (num_pulls) {
Stephen Warrence63d6d2013-03-28 05:09:57 +0000801 err = of_property_read_u32_index(np, "brcm,pull",
Phil Elwell2c7e3302016-02-29 17:30:08 -0800802 (num_pulls > 1) ? i : 0, &pull);
Stephen Warrence63d6d2013-03-28 05:09:57 +0000803 if (err)
804 goto out;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600805 err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
806 pull, &cur_map);
807 if (err)
808 goto out;
809 }
810 }
811
812 *map = maps;
813 *num_maps = num_pins * maps_per_pin;
814
815 return 0;
816
817out:
Stefan Wahren53653c62015-12-21 00:44:04 +0000818 bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600819 return err;
820}
821
Laurent Pinchart022ab142013-02-16 10:25:07 +0100822static const struct pinctrl_ops bcm2835_pctl_ops = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600823 .get_groups_count = bcm2835_pctl_get_groups_count,
824 .get_group_name = bcm2835_pctl_get_group_name,
825 .get_group_pins = bcm2835_pctl_get_group_pins,
826 .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
827 .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
828 .dt_free_map = bcm2835_pctl_dt_free_map,
829};
830
Phil Elwellccca1ad2016-05-06 12:32:47 +0100831static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
832 unsigned offset)
833{
834 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
835
836 /* disable by setting to GPIO_IN */
837 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
838 return 0;
839}
840
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600841static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
842{
843 return BCM2835_FSEL_COUNT;
844}
845
846static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
847 unsigned selector)
848{
849 return bcm2835_functions[selector];
850}
851
852static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
853 unsigned selector,
854 const char * const **groups,
855 unsigned * const num_groups)
856{
857 /* every pin can do every function */
858 *groups = bcm2835_gpio_groups;
859 *num_groups = ARRAY_SIZE(bcm2835_gpio_groups);
860
861 return 0;
862}
863
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200864static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600865 unsigned func_selector,
866 unsigned group_selector)
867{
868 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
869
870 bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
871
872 return 0;
873}
874
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600875static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
876 struct pinctrl_gpio_range *range,
877 unsigned offset)
878{
879 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
880
881 /* disable by setting to GPIO_IN */
882 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
883}
884
885static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
886 struct pinctrl_gpio_range *range,
887 unsigned offset,
888 bool input)
889{
890 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
891 enum bcm2835_fsel fsel = input ?
892 BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
893
894 bcm2835_pinctrl_fsel_set(pc, offset, fsel);
895
896 return 0;
897}
898
Laurent Pinchart022ab142013-02-16 10:25:07 +0100899static const struct pinmux_ops bcm2835_pmx_ops = {
Phil Elwellccca1ad2016-05-06 12:32:47 +0100900 .free = bcm2835_pmx_free,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600901 .get_functions_count = bcm2835_pmx_get_functions_count,
902 .get_function_name = bcm2835_pmx_get_function_name,
903 .get_function_groups = bcm2835_pmx_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200904 .set_mux = bcm2835_pmx_set,
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600905 .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
906 .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
907};
908
909static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
910 unsigned pin, unsigned long *config)
911{
912 /* No way to read back config in HW */
913 return -ENOTSUPP;
914}
915
Matheus Castello0de70492018-04-30 20:42:13 -0400916static void bcm2835_pull_config_set(struct bcm2835_pinctrl *pc,
917 unsigned int pin, unsigned int arg)
918{
919 u32 off, bit;
920
921 off = GPIO_REG_OFFSET(pin);
922 bit = GPIO_REG_SHIFT(pin);
923
924 bcm2835_gpio_wr(pc, GPPUD, arg & 3);
925 /*
926 * BCM2835 datasheet say to wait 150 cycles, but not of what.
927 * But the VideoCore firmware delay for this operation
928 * based nearly on the same amount of VPU cycles and this clock
929 * runs at 250 MHz.
930 */
931 udelay(1);
932 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
933 udelay(1);
934 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
935}
936
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600937static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
Matheus Castello0de70492018-04-30 20:42:13 -0400938 unsigned int pin, unsigned long *configs,
939 unsigned int num_configs)
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600940{
941 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
Matheus Castello0de70492018-04-30 20:42:13 -0400942 u32 param, arg;
Sherman Yin03b054e2013-08-27 11:32:12 -0700943 int i;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600944
Sherman Yin03b054e2013-08-27 11:32:12 -0700945 for (i = 0; i < num_configs; i++) {
Matheus Castello0de70492018-04-30 20:42:13 -0400946 param = pinconf_to_config_param(configs[i]);
947 arg = pinconf_to_config_argument(configs[i]);
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600948
Matheus Castello0de70492018-04-30 20:42:13 -0400949 switch (param) {
950 /* Set legacy brcm,pull */
951 case BCM2835_PINCONF_PARAM_PULL:
952 bcm2835_pull_config_set(pc, pin, arg);
953 break;
954
955 /* Set pull generic bindings */
956 case PIN_CONFIG_BIAS_DISABLE:
957 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_OFF);
958 break;
959
960 case PIN_CONFIG_BIAS_PULL_DOWN:
961 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_DOWN);
962 break;
963
964 case PIN_CONFIG_BIAS_PULL_UP:
965 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_UP);
966 break;
967
Matheus Castello90b60552018-04-30 20:42:14 -0400968 /* Set output-high or output-low */
969 case PIN_CONFIG_OUTPUT:
970 bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
971 break;
972
Matheus Castello0de70492018-04-30 20:42:13 -0400973 default:
Sherman Yin03b054e2013-08-27 11:32:12 -0700974 return -EINVAL;
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600975
Matheus Castello0de70492018-04-30 20:42:13 -0400976 } /* switch param type */
Sherman Yin03b054e2013-08-27 11:32:12 -0700977 } /* for each config */
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600978
979 return 0;
980}
981
Laurent Pinchart022ab142013-02-16 10:25:07 +0100982static const struct pinconf_ops bcm2835_pinconf_ops = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600983 .pin_config_get = bcm2835_pinconf_get,
984 .pin_config_set = bcm2835_pinconf_set,
985};
986
987static struct pinctrl_desc bcm2835_pinctrl_desc = {
988 .name = MODULE_NAME,
989 .pins = bcm2835_gpio_pins,
990 .npins = ARRAY_SIZE(bcm2835_gpio_pins),
991 .pctlops = &bcm2835_pctl_ops,
992 .pmxops = &bcm2835_pmx_ops,
993 .confops = &bcm2835_pinconf_ops,
994 .owner = THIS_MODULE,
995};
996
Bill Pemberton84db00b2012-11-19 13:25:13 -0500997static struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -0600998 .name = MODULE_NAME,
999 .npins = BCM2835_NUM_GPIOS,
1000};
1001
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001002static int bcm2835_pinctrl_probe(struct platform_device *pdev)
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001003{
1004 struct device *dev = &pdev->dev;
1005 struct device_node *np = dev->of_node;
1006 struct bcm2835_pinctrl *pc;
1007 struct resource iomem;
1008 int err, i;
1009 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2835_NUM_GPIOS);
1010 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2835_NUM_GPIOS);
1011
1012 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
1013 if (!pc)
1014 return -ENOMEM;
1015
1016 platform_set_drvdata(pdev, pc);
1017 pc->dev = dev;
1018
1019 err = of_address_to_resource(np, 0, &iomem);
1020 if (err) {
1021 dev_err(dev, "could not get IO memory\n");
1022 return err;
1023 }
1024
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001025 pc->base = devm_ioremap_resource(dev, &iomem);
1026 if (IS_ERR(pc->base))
1027 return PTR_ERR(pc->base);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001028
1029 pc->gpio_chip = bcm2835_gpio_chip;
Linus Walleij58383c782015-11-04 09:56:26 +01001030 pc->gpio_chip.parent = dev;
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001031 pc->gpio_chip.of_node = np;
1032
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001033 for (i = 0; i < BCM2835_NUM_BANKS; i++) {
1034 unsigned long events;
1035 unsigned offset;
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001036
1037 /* clear event detection flags */
1038 bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
1039 bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
1040 bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
1041 bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
1042 bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
1043 bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
1044
1045 /* clear all the events */
1046 events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
1047 for_each_set_bit(offset, &events, 32)
1048 bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
1049
Phil Elwell00445b52015-02-24 13:40:50 +00001050 spin_lock_init(&pc->irq_lock[i]);
1051 }
1052
Linus Walleije19a5f72015-12-08 22:01:00 +01001053 err = gpiochip_add_data(&pc->gpio_chip, pc);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001054 if (err) {
1055 dev_err(dev, "could not add GPIO chip\n");
1056 return err;
1057 }
1058
Linus Walleij85ae9e52016-11-14 18:48:19 +01001059 err = gpiochip_irqchip_add(&pc->gpio_chip, &bcm2835_gpio_irq_chip,
1060 0, handle_level_irq, IRQ_TYPE_NONE);
1061 if (err) {
1062 dev_info(dev, "could not add irqchip\n");
1063 return err;
1064 }
1065
1066 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
1067 pc->irq[i] = irq_of_parse_and_map(np, i);
Stefan Wahren37a2f8e2017-06-21 20:20:04 +02001068
1069 if (pc->irq[i] == 0)
1070 continue;
1071
Linus Walleij85ae9e52016-11-14 18:48:19 +01001072 /*
1073 * Use the same handler for all groups: this is necessary
1074 * since we use one gpiochip to cover all lines - the
1075 * irq handler then needs to figure out which group and
1076 * bank that was firing the IRQ and look up the per-group
1077 * and bank data.
1078 */
1079 gpiochip_set_chained_irqchip(&pc->gpio_chip,
1080 &bcm2835_gpio_irq_chip,
1081 pc->irq[i],
1082 bcm2835_gpio_irq_handler);
1083 }
1084
Laxman Dewangan5f276f62016-02-24 14:44:07 +05301085 pc->pctl_dev = devm_pinctrl_register(dev, &bcm2835_pinctrl_desc, pc);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001086 if (IS_ERR(pc->pctl_dev)) {
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001087 gpiochip_remove(&pc->gpio_chip);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001088 return PTR_ERR(pc->pctl_dev);
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001089 }
1090
1091 pc->gpio_range = bcm2835_pinctrl_gpio_range;
1092 pc->gpio_range.base = pc->gpio_chip.base;
1093 pc->gpio_range.gc = &pc->gpio_chip;
1094 pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1095
1096 return 0;
1097}
1098
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001099static const struct of_device_id bcm2835_pinctrl_match[] = {
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001100 { .compatible = "brcm,bcm2835-gpio" },
1101 {}
1102};
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001103
1104static struct platform_driver bcm2835_pinctrl_driver = {
1105 .probe = bcm2835_pinctrl_probe,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001106 .driver = {
1107 .name = MODULE_NAME,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001108 .of_match_table = bcm2835_pinctrl_match,
Paul Gortmaker34f46842017-05-22 16:56:48 -04001109 .suppress_bind_attrs = true,
Simon Arlotte1b2dc72012-09-27 22:10:11 -06001110 },
1111};
Paul Gortmaker34f46842017-05-22 16:56:48 -04001112builtin_platform_driver(bcm2835_pinctrl_driver);