1 From 4f339b429583965a8eb7c23474414d0730db1215 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 8 Oct 2014 18:50:05 +0100
4 Subject: [PATCH 002/114] Add bcm2708_gpio driver
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 bcm2708: Add extension to configure internal pulls
10 The bcm2708 gpio controller supports internal pulls to be used as pull-up,
11 pull-down or being entirely disabled. As it can be useful for a driver to
12 change the pull configuration from it's default pull-down state, add an
13 extension which allows configuring the pull per gpio.
15 Signed-off-by: Julian Scheel <julian@jusst.de>
17 bcm2708-gpio: Revert the use of pinctrl_request_gpio
19 In non-DT systems, pinctrl_request_gpio always fails causing
20 "requests probe deferral" messages. In DT systems, it isn't useful
21 because the reference counting is independent of the normal pinctrl
24 gpio: Only clear the currently occurring interrupt. Avoids losing interrupts
28 bcm2708_gpio: Avoid calling irq_unmask for all interrupts
30 When setting up the interrupts, specify that the handle_simple_irq
31 handler should be used. This leaves interrupt acknowledgement to
32 the caller, and prevents irq_unmask from being called for all
37 arch/arm/mach-bcm2708/Kconfig | 8 +
38 arch/arm/mach-bcm2708/Makefile | 1 +
39 arch/arm/mach-bcm2708/bcm2708.c | 28 ++
40 arch/arm/mach-bcm2708/bcm2708_gpio.c | 426 ++++++++++++++++++++++++++++++
41 arch/arm/mach-bcm2708/include/mach/gpio.h | 17 ++
42 include/linux/platform_data/bcm2708.h | 23 ++
43 6 files changed, 503 insertions(+)
44 create mode 100644 arch/arm/mach-bcm2708/bcm2708_gpio.c
45 create mode 100644 arch/arm/mach-bcm2708/include/mach/gpio.h
46 create mode 100644 include/linux/platform_data/bcm2708.h
48 --- a/arch/arm/mach-bcm2708/Kconfig
49 +++ b/arch/arm/mach-bcm2708/Kconfig
50 @@ -9,6 +9,14 @@ config MACH_BCM2708
52 Include support for the Broadcom(R) BCM2708 platform.
55 + bool "BCM2708 gpio support"
56 + depends on MACH_BCM2708
57 + select ARCH_REQUIRE_GPIOLIB
60 + Include support for the Broadcom(R) BCM2708 gpio.
63 bool "Videocore Memory"
64 depends on MACH_BCM2708
65 --- a/arch/arm/mach-bcm2708/Makefile
66 +++ b/arch/arm/mach-bcm2708/Makefile
70 obj-$(CONFIG_MACH_BCM2708) += clock.o bcm2708.o armctrl.o vcio.o power.o dma.o
71 +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
72 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
73 --- a/arch/arm/mach-bcm2708/bcm2708.c
74 +++ b/arch/arm/mach-bcm2708/bcm2708.c
75 @@ -331,6 +331,31 @@ static struct platform_device bcm2708_vc
79 +#ifdef CONFIG_BCM2708_GPIO
80 +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
82 +static struct resource bcm2708_gpio_resources[] = {
83 + [0] = { /* general purpose I/O */
85 + .end = GPIO_BASE + SZ_4K - 1,
86 + .flags = IORESOURCE_MEM,
90 +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
92 +static struct platform_device bcm2708_gpio_device = {
93 + .name = BCM_GPIO_DRIVER_NAME,
94 + .id = -1, /* only one VideoCore I/O area */
95 + .resource = bcm2708_gpio_resources,
96 + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
98 + .dma_mask = &gpio_dmamask,
99 + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
104 static struct resource bcm2708_systemtimer_resources[] = {
105 [0] = { /* system timer access */
107 @@ -473,6 +498,9 @@ void __init bcm2708_init(void)
109 bcm_register_device(&bcm2708_dmaman_device);
110 bcm_register_device(&bcm2708_vcio_device);
111 +#ifdef CONFIG_BCM2708_GPIO
112 + bcm_register_device(&bcm2708_gpio_device);
114 bcm_register_device(&bcm2708_systemtimer_device);
115 bcm_register_device(&bcm2708_fb_device);
116 bcm_register_device(&bcm2708_usb_device);
118 +++ b/arch/arm/mach-bcm2708/bcm2708_gpio.c
121 + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
123 + * Copyright (C) 2010 Broadcom
125 + * This program is free software; you can redistribute it and/or modify
126 + * it under the terms of the GNU General Public License version 2 as
127 + * published by the Free Software Foundation.
131 +#include <linux/spinlock.h>
132 +#include <linux/module.h>
133 +#include <linux/delay.h>
134 +#include <linux/list.h>
135 +#include <linux/io.h>
136 +#include <linux/irq.h>
137 +#include <linux/interrupt.h>
138 +#include <linux/slab.h>
139 +#include <mach/gpio.h>
140 +#include <linux/gpio.h>
141 +#include <linux/platform_device.h>
142 +#include <mach/platform.h>
143 +#include <linux/pinctrl/consumer.h>
145 +#include <linux/platform_data/bcm2708.h>
147 +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
148 +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
149 +#define BCM_GPIO_USE_IRQ 1
151 +#define GPIOFSEL(x) (0x00+(x)*4)
152 +#define GPIOSET(x) (0x1c+(x)*4)
153 +#define GPIOCLR(x) (0x28+(x)*4)
154 +#define GPIOLEV(x) (0x34+(x)*4)
155 +#define GPIOEDS(x) (0x40+(x)*4)
156 +#define GPIOREN(x) (0x4c+(x)*4)
157 +#define GPIOFEN(x) (0x58+(x)*4)
158 +#define GPIOHEN(x) (0x64+(x)*4)
159 +#define GPIOLEN(x) (0x70+(x)*4)
160 +#define GPIOAREN(x) (0x7c+(x)*4)
161 +#define GPIOAFEN(x) (0x88+(x)*4)
162 +#define GPIOUD(x) (0x94+(x)*4)
163 +#define GPIOUDCLK(x) (0x98+(x)*4)
165 +#define GPIO_BANKS 2
167 +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
168 + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
169 + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
170 + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
173 + /* Each of the two spinlocks protects a different set of hardware
174 + * regiters and data structurs. This decouples the code of the IRQ from
175 + * the GPIO code. This also makes the case of a GPIO routine call from
176 + * the IRQ code simpler.
178 +static DEFINE_SPINLOCK(lock); /* GPIO registers */
180 +struct bcm2708_gpio {
181 + struct list_head list;
182 + void __iomem *base;
183 + struct gpio_chip gc;
184 + unsigned long rising[(BCM2708_NR_GPIOS + 31) / 32];
185 + unsigned long falling[(BCM2708_NR_GPIOS + 31) / 32];
186 + unsigned long high[(BCM2708_NR_GPIOS + 31) / 32];
187 + unsigned long low[(BCM2708_NR_GPIOS + 31) / 32];
190 +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
193 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
194 + unsigned long flags;
196 + unsigned gpio_bank = offset / 10;
197 + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
199 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
200 + if (offset >= BCM2708_NR_GPIOS)
203 + spin_lock_irqsave(&lock, flags);
205 + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
206 + gpiodir &= ~(7 << gpio_field_offset);
207 + gpiodir |= function << gpio_field_offset;
208 + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
209 + spin_unlock_irqrestore(&lock, flags);
210 + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
215 +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
217 + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
220 +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
221 +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
225 + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
227 + bcm2708_gpio_set(gc, offset, value);
231 +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
233 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
234 + unsigned gpio_bank = offset / 32;
235 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
238 + if (offset >= BCM2708_NR_GPIOS)
240 + lev = readl(gpio->base + GPIOLEV(gpio_bank));
241 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
242 + return 0x1 & (lev >> gpio_field_offset);
245 +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
247 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
248 + unsigned gpio_bank = offset / 32;
249 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
250 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
251 + if (offset >= BCM2708_NR_GPIOS)
254 + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
256 + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
259 +/**********************
260 + * extension to configure pullups
262 +int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
263 + bcm2708_gpio_pull_t value)
265 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
266 + unsigned gpio_bank = offset / 32;
267 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
269 + if (offset >= BCM2708_NR_GPIOS)
273 + case BCM2708_PULL_UP:
274 + writel(2, gpio->base + GPIOUD(0));
276 + case BCM2708_PULL_DOWN:
277 + writel(1, gpio->base + GPIOUD(0));
279 + case BCM2708_PULL_OFF:
280 + writel(0, gpio->base + GPIOUD(0));
285 + writel(1 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
287 + writel(0, gpio->base + GPIOUD(0));
288 + writel(0 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
292 +EXPORT_SYMBOL(bcm2708_gpio_setpull);
294 +/*************************************************************************************************************************
298 +#if BCM_GPIO_USE_IRQ
300 +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
302 + return gpio_to_irq(gpio);
305 +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
307 + unsigned irq = d->irq;
308 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
309 + unsigned gn = irq_to_gpio(irq);
310 + unsigned gb = gn / 32;
311 + unsigned go = gn % 32;
313 + gpio->rising[gb] &= ~(1 << go);
314 + gpio->falling[gb] &= ~(1 << go);
315 + gpio->high[gb] &= ~(1 << go);
316 + gpio->low[gb] &= ~(1 << go);
318 + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
321 + if (type & IRQ_TYPE_EDGE_RISING)
322 + gpio->rising[gb] |= (1 << go);
323 + if (type & IRQ_TYPE_EDGE_FALLING)
324 + gpio->falling[gb] |= (1 << go);
325 + if (type & IRQ_TYPE_LEVEL_HIGH)
326 + gpio->high[gb] |= (1 << go);
327 + if (type & IRQ_TYPE_LEVEL_LOW)
328 + gpio->low[gb] |= (1 << go);
332 +static void bcm2708_gpio_irq_mask(struct irq_data *d)
334 + unsigned irq = d->irq;
335 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
336 + unsigned gn = irq_to_gpio(irq);
337 + unsigned gb = gn / 32;
338 + unsigned long rising = readl(gpio->base + GPIOREN(gb));
339 + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
340 + unsigned long high = readl(gpio->base + GPIOHEN(gb));
341 + unsigned long low = readl(gpio->base + GPIOLEN(gb));
345 + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
346 + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
347 + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
348 + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
351 +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
353 + unsigned irq = d->irq;
354 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
355 + unsigned gn = irq_to_gpio(irq);
356 + unsigned gb = gn / 32;
357 + unsigned go = gn % 32;
358 + unsigned long rising = readl(gpio->base + GPIOREN(gb));
359 + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
360 + unsigned long high = readl(gpio->base + GPIOHEN(gb));
361 + unsigned long low = readl(gpio->base + GPIOLEN(gb));
363 + if (gpio->rising[gb] & (1 << go)) {
364 + writel(rising | (1 << go), gpio->base + GPIOREN(gb));
366 + writel(rising & ~(1 << go), gpio->base + GPIOREN(gb));
369 + if (gpio->falling[gb] & (1 << go)) {
370 + writel(falling | (1 << go), gpio->base + GPIOFEN(gb));
372 + writel(falling & ~(1 << go), gpio->base + GPIOFEN(gb));
375 + if (gpio->high[gb] & (1 << go)) {
376 + writel(high | (1 << go), gpio->base + GPIOHEN(gb));
378 + writel(high & ~(1 << go), gpio->base + GPIOHEN(gb));
381 + if (gpio->low[gb] & (1 << go)) {
382 + writel(low | (1 << go), gpio->base + GPIOLEN(gb));
384 + writel(low & ~(1 << go), gpio->base + GPIOLEN(gb));
388 +static struct irq_chip bcm2708_irqchip = {
390 + .irq_enable = bcm2708_gpio_irq_unmask,
391 + .irq_disable = bcm2708_gpio_irq_mask,
392 + .irq_unmask = bcm2708_gpio_irq_unmask,
393 + .irq_mask = bcm2708_gpio_irq_mask,
394 + .irq_set_type = bcm2708_gpio_irq_set_type,
397 +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
399 + unsigned long edsr;
403 + unsigned level_bits;
404 + struct bcm2708_gpio *gpio_data = dev_id;
406 + for (bank = 0; bank < GPIO_BANKS; bank++) {
407 + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
408 + level_bits = gpio_data->high[bank] | gpio_data->low[bank];
410 + for_each_set_bit(i, &edsr, 32) {
411 + gpio = i + bank * 32;
412 + /* ack edge triggered IRQs immediately */
413 + if (!(level_bits & (1<<i)))
415 + __io_address(GPIO_BASE) + GPIOEDS(bank));
416 + generic_handle_irq(gpio_to_irq(gpio));
417 + /* ack level triggered IRQ after handling them */
418 + if (level_bits & (1<<i))
420 + __io_address(GPIO_BASE) + GPIOEDS(bank));
423 + return IRQ_HANDLED;
426 +static struct irqaction bcm2708_gpio_irq = {
427 + .name = "BCM2708 GPIO catchall handler",
428 + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
429 + .handler = bcm2708_gpio_interrupt,
432 +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
436 + ucb->gc.to_irq = bcm2708_gpio_to_irq;
438 + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
439 + irq_set_chip_data(irq, ucb);
440 + irq_set_chip_and_handler(irq, &bcm2708_irqchip,
441 + handle_simple_irq);
442 + set_irq_flags(irq, IRQF_VALID);
445 + bcm2708_gpio_irq.dev_id = ucb;
446 + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
451 +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
455 +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
457 +static int bcm2708_gpio_probe(struct platform_device *dev)
459 + struct bcm2708_gpio *ucb;
460 + struct resource *res;
464 + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
466 + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
468 + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
469 + "mailbox memory\n");
474 + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
476 + platform_set_drvdata(dev, ucb);
477 + ucb->base = __io_address(GPIO_BASE);
479 + ucb->gc.label = "bcm2708_gpio";
481 + ucb->gc.ngpio = BCM2708_NR_GPIOS;
482 + ucb->gc.owner = THIS_MODULE;
484 + ucb->gc.direction_input = bcm2708_gpio_dir_in;
485 + ucb->gc.direction_output = bcm2708_gpio_dir_out;
486 + ucb->gc.get = bcm2708_gpio_get;
487 + ucb->gc.set = bcm2708_gpio_set;
488 + ucb->gc.can_sleep = 0;
490 + for (bank = 0; bank < GPIO_BANKS; bank++) {
491 + writel(0, ucb->base + GPIOREN(bank));
492 + writel(0, ucb->base + GPIOFEN(bank));
493 + writel(0, ucb->base + GPIOHEN(bank));
494 + writel(0, ucb->base + GPIOLEN(bank));
495 + writel(0, ucb->base + GPIOAREN(bank));
496 + writel(0, ucb->base + GPIOAFEN(bank));
497 + writel(~0, ucb->base + GPIOEDS(bank));
500 + bcm2708_gpio_irq_init(ucb);
502 + err = gpiochip_add(&ucb->gc);
509 +static int bcm2708_gpio_remove(struct platform_device *dev)
512 + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
514 + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
516 + gpiochip_remove(&ucb->gc);
518 + platform_set_drvdata(dev, NULL);
524 +static struct platform_driver bcm2708_gpio_driver = {
525 + .probe = bcm2708_gpio_probe,
526 + .remove = bcm2708_gpio_remove,
528 + .name = "bcm2708_gpio"},
531 +static int __init bcm2708_gpio_init(void)
533 + return platform_driver_register(&bcm2708_gpio_driver);
536 +static void __exit bcm2708_gpio_exit(void)
538 + platform_driver_unregister(&bcm2708_gpio_driver);
541 +module_init(bcm2708_gpio_init);
542 +module_exit(bcm2708_gpio_exit);
544 +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
545 +MODULE_LICENSE("GPL");
547 +++ b/arch/arm/mach-bcm2708/include/mach/gpio.h
550 + * arch/arm/mach-bcm2708/include/mach/gpio.h
552 + * This file is licensed under the terms of the GNU General Public
553 + * License version 2. This program is licensed "as is" without any
554 + * warranty of any kind, whether express or implied.
557 +#ifndef __ASM_ARCH_GPIO_H
558 +#define __ASM_ARCH_GPIO_H
560 +#define BCM2708_NR_GPIOS 54 // number of gpio lines
562 +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
563 +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
567 +++ b/include/linux/platform_data/bcm2708.h
570 + * include/linux/platform_data/bcm2708.h
572 + * This program is free software; you can redistribute it and/or modify
573 + * it under the terms of the GNU General Public License version 2 as
574 + * published by the Free Software Foundation.
576 + * (C) 2014 Julian Scheel <julian@jusst.de>
579 +#ifndef __BCM2708_H_
580 +#define __BCM2708_H_
586 +} bcm2708_gpio_pull_t;
588 +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
589 + bcm2708_gpio_pull_t value);