1 From 0dcdc1b4040137f2e273f85355f8bcc431a83ca6 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/148] 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 | 25 ++
40 arch/arm/mach-bcm2708/bcm2708_gpio.c | 426 ++++++++++++++++++++++++++++++
41 arch/arm/mach-bcm2708/include/mach/gpio.h | 17 ++
42 arch/arm/mach-bcm2709/bcm2709.c | 25 ++
43 include/linux/platform_data/bcm2708.h | 23 ++
44 7 files changed, 525 insertions(+)
45 create mode 100644 arch/arm/mach-bcm2708/bcm2708_gpio.c
46 create mode 100644 arch/arm/mach-bcm2708/include/mach/gpio.h
47 create mode 100644 include/linux/platform_data/bcm2708.h
49 --- a/arch/arm/mach-bcm2708/Kconfig
50 +++ b/arch/arm/mach-bcm2708/Kconfig
51 @@ -20,6 +20,14 @@ config BCM2708_DT
53 Enable Device Tree support for BCM2708
56 + bool "BCM2708 gpio support"
57 + depends on MACH_BCM2708
58 + select ARCH_REQUIRE_GPIOLIB
61 + Include support for the Broadcom(R) BCM2708 gpio.
63 config BCM2708_NOL2CACHE
64 bool "Videocore L2 cache disable"
65 depends on MACH_BCM2708
66 --- a/arch/arm/mach-bcm2708/Makefile
67 +++ b/arch/arm/mach-bcm2708/Makefile
71 obj-$(CONFIG_MACH_BCM2708) += bcm2708.o armctrl.o
72 +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
73 --- a/arch/arm/mach-bcm2708/bcm2708.c
74 +++ b/arch/arm/mach-bcm2708/bcm2708.c
75 @@ -298,6 +298,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 int __init bcm_register_device(struct platform_device *pdev)
108 +++ b/arch/arm/mach-bcm2708/bcm2708_gpio.c
111 + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
113 + * Copyright (C) 2010 Broadcom
115 + * This program is free software; you can redistribute it and/or modify
116 + * it under the terms of the GNU General Public License version 2 as
117 + * published by the Free Software Foundation.
121 +#include <linux/spinlock.h>
122 +#include <linux/module.h>
123 +#include <linux/delay.h>
124 +#include <linux/list.h>
125 +#include <linux/io.h>
126 +#include <linux/irq.h>
127 +#include <linux/interrupt.h>
128 +#include <linux/slab.h>
129 +#include <mach/gpio.h>
130 +#include <linux/gpio.h>
131 +#include <linux/platform_device.h>
132 +#include <mach/platform.h>
133 +#include <linux/pinctrl/consumer.h>
135 +#include <linux/platform_data/bcm2708.h>
137 +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
138 +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
139 +#define BCM_GPIO_USE_IRQ 1
141 +#define GPIOFSEL(x) (0x00+(x)*4)
142 +#define GPIOSET(x) (0x1c+(x)*4)
143 +#define GPIOCLR(x) (0x28+(x)*4)
144 +#define GPIOLEV(x) (0x34+(x)*4)
145 +#define GPIOEDS(x) (0x40+(x)*4)
146 +#define GPIOREN(x) (0x4c+(x)*4)
147 +#define GPIOFEN(x) (0x58+(x)*4)
148 +#define GPIOHEN(x) (0x64+(x)*4)
149 +#define GPIOLEN(x) (0x70+(x)*4)
150 +#define GPIOAREN(x) (0x7c+(x)*4)
151 +#define GPIOAFEN(x) (0x88+(x)*4)
152 +#define GPIOUD(x) (0x94+(x)*4)
153 +#define GPIOUDCLK(x) (0x98+(x)*4)
155 +#define GPIO_BANKS 2
157 +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
158 + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
159 + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
160 + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
163 + /* Each of the two spinlocks protects a different set of hardware
164 + * regiters and data structurs. This decouples the code of the IRQ from
165 + * the GPIO code. This also makes the case of a GPIO routine call from
166 + * the IRQ code simpler.
168 +static DEFINE_SPINLOCK(lock); /* GPIO registers */
170 +struct bcm2708_gpio {
171 + struct list_head list;
172 + void __iomem *base;
173 + struct gpio_chip gc;
174 + unsigned long rising[(BCM2708_NR_GPIOS + 31) / 32];
175 + unsigned long falling[(BCM2708_NR_GPIOS + 31) / 32];
176 + unsigned long high[(BCM2708_NR_GPIOS + 31) / 32];
177 + unsigned long low[(BCM2708_NR_GPIOS + 31) / 32];
180 +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
183 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
184 + unsigned long flags;
186 + unsigned gpio_bank = offset / 10;
187 + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
189 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
190 + if (offset >= BCM2708_NR_GPIOS)
193 + spin_lock_irqsave(&lock, flags);
195 + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
196 + gpiodir &= ~(7 << gpio_field_offset);
197 + gpiodir |= function << gpio_field_offset;
198 + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
199 + spin_unlock_irqrestore(&lock, flags);
200 + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
205 +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
207 + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
210 +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
211 +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
215 + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
217 + bcm2708_gpio_set(gc, offset, value);
221 +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
223 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
224 + unsigned gpio_bank = offset / 32;
225 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
228 + if (offset >= BCM2708_NR_GPIOS)
230 + lev = readl(gpio->base + GPIOLEV(gpio_bank));
231 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
232 + return 0x1 & (lev >> gpio_field_offset);
235 +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
237 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
238 + unsigned gpio_bank = offset / 32;
239 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
240 +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
241 + if (offset >= BCM2708_NR_GPIOS)
244 + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
246 + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
249 +/**********************
250 + * extension to configure pullups
252 +int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
253 + bcm2708_gpio_pull_t value)
255 + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
256 + unsigned gpio_bank = offset / 32;
257 + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
259 + if (offset >= BCM2708_NR_GPIOS)
263 + case BCM2708_PULL_UP:
264 + writel(2, gpio->base + GPIOUD(0));
266 + case BCM2708_PULL_DOWN:
267 + writel(1, gpio->base + GPIOUD(0));
269 + case BCM2708_PULL_OFF:
270 + writel(0, gpio->base + GPIOUD(0));
275 + writel(1 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
277 + writel(0, gpio->base + GPIOUD(0));
278 + writel(0 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
282 +EXPORT_SYMBOL(bcm2708_gpio_setpull);
284 +/*************************************************************************************************************************
288 +#if BCM_GPIO_USE_IRQ
290 +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
292 + return gpio_to_irq(gpio);
295 +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
297 + unsigned irq = d->irq;
298 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
299 + unsigned gn = irq_to_gpio(irq);
300 + unsigned gb = gn / 32;
301 + unsigned go = gn % 32;
303 + gpio->rising[gb] &= ~(1 << go);
304 + gpio->falling[gb] &= ~(1 << go);
305 + gpio->high[gb] &= ~(1 << go);
306 + gpio->low[gb] &= ~(1 << go);
308 + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
311 + if (type & IRQ_TYPE_EDGE_RISING)
312 + gpio->rising[gb] |= (1 << go);
313 + if (type & IRQ_TYPE_EDGE_FALLING)
314 + gpio->falling[gb] |= (1 << go);
315 + if (type & IRQ_TYPE_LEVEL_HIGH)
316 + gpio->high[gb] |= (1 << go);
317 + if (type & IRQ_TYPE_LEVEL_LOW)
318 + gpio->low[gb] |= (1 << go);
322 +static void bcm2708_gpio_irq_mask(struct irq_data *d)
324 + unsigned irq = d->irq;
325 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
326 + unsigned gn = irq_to_gpio(irq);
327 + unsigned gb = gn / 32;
328 + unsigned long rising = readl(gpio->base + GPIOREN(gb));
329 + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
330 + unsigned long high = readl(gpio->base + GPIOHEN(gb));
331 + unsigned long low = readl(gpio->base + GPIOLEN(gb));
335 + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
336 + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
337 + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
338 + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
341 +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
343 + unsigned irq = d->irq;
344 + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
345 + unsigned gn = irq_to_gpio(irq);
346 + unsigned gb = gn / 32;
347 + unsigned go = gn % 32;
348 + unsigned long rising = readl(gpio->base + GPIOREN(gb));
349 + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
350 + unsigned long high = readl(gpio->base + GPIOHEN(gb));
351 + unsigned long low = readl(gpio->base + GPIOLEN(gb));
353 + if (gpio->rising[gb] & (1 << go)) {
354 + writel(rising | (1 << go), gpio->base + GPIOREN(gb));
356 + writel(rising & ~(1 << go), gpio->base + GPIOREN(gb));
359 + if (gpio->falling[gb] & (1 << go)) {
360 + writel(falling | (1 << go), gpio->base + GPIOFEN(gb));
362 + writel(falling & ~(1 << go), gpio->base + GPIOFEN(gb));
365 + if (gpio->high[gb] & (1 << go)) {
366 + writel(high | (1 << go), gpio->base + GPIOHEN(gb));
368 + writel(high & ~(1 << go), gpio->base + GPIOHEN(gb));
371 + if (gpio->low[gb] & (1 << go)) {
372 + writel(low | (1 << go), gpio->base + GPIOLEN(gb));
374 + writel(low & ~(1 << go), gpio->base + GPIOLEN(gb));
378 +static struct irq_chip bcm2708_irqchip = {
380 + .irq_enable = bcm2708_gpio_irq_unmask,
381 + .irq_disable = bcm2708_gpio_irq_mask,
382 + .irq_unmask = bcm2708_gpio_irq_unmask,
383 + .irq_mask = bcm2708_gpio_irq_mask,
384 + .irq_set_type = bcm2708_gpio_irq_set_type,
387 +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
389 + unsigned long edsr;
393 + unsigned level_bits;
394 + struct bcm2708_gpio *gpio_data = dev_id;
396 + for (bank = 0; bank < GPIO_BANKS; bank++) {
397 + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
398 + level_bits = gpio_data->high[bank] | gpio_data->low[bank];
400 + for_each_set_bit(i, &edsr, 32) {
401 + gpio = i + bank * 32;
402 + /* ack edge triggered IRQs immediately */
403 + if (!(level_bits & (1<<i)))
405 + __io_address(GPIO_BASE) + GPIOEDS(bank));
406 + generic_handle_irq(gpio_to_irq(gpio));
407 + /* ack level triggered IRQ after handling them */
408 + if (level_bits & (1<<i))
410 + __io_address(GPIO_BASE) + GPIOEDS(bank));
413 + return IRQ_HANDLED;
416 +static struct irqaction bcm2708_gpio_irq = {
417 + .name = "BCM2708 GPIO catchall handler",
418 + .flags = IRQF_TIMER | IRQF_IRQPOLL,
419 + .handler = bcm2708_gpio_interrupt,
422 +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
426 + ucb->gc.to_irq = bcm2708_gpio_to_irq;
428 + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
429 + irq_set_chip_data(irq, ucb);
430 + irq_set_chip_and_handler(irq, &bcm2708_irqchip,
431 + handle_simple_irq);
432 + set_irq_flags(irq, IRQF_VALID);
435 + bcm2708_gpio_irq.dev_id = ucb;
436 + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
441 +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
445 +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
447 +static int bcm2708_gpio_probe(struct platform_device *dev)
449 + struct bcm2708_gpio *ucb;
450 + struct resource *res;
454 + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
456 + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
458 + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
459 + "mailbox memory\n");
464 + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
466 + platform_set_drvdata(dev, ucb);
467 + ucb->base = __io_address(GPIO_BASE);
469 + ucb->gc.label = "bcm2708_gpio";
471 + ucb->gc.ngpio = BCM2708_NR_GPIOS;
472 + ucb->gc.owner = THIS_MODULE;
474 + ucb->gc.direction_input = bcm2708_gpio_dir_in;
475 + ucb->gc.direction_output = bcm2708_gpio_dir_out;
476 + ucb->gc.get = bcm2708_gpio_get;
477 + ucb->gc.set = bcm2708_gpio_set;
478 + ucb->gc.can_sleep = 0;
480 + for (bank = 0; bank < GPIO_BANKS; bank++) {
481 + writel(0, ucb->base + GPIOREN(bank));
482 + writel(0, ucb->base + GPIOFEN(bank));
483 + writel(0, ucb->base + GPIOHEN(bank));
484 + writel(0, ucb->base + GPIOLEN(bank));
485 + writel(0, ucb->base + GPIOAREN(bank));
486 + writel(0, ucb->base + GPIOAFEN(bank));
487 + writel(~0, ucb->base + GPIOEDS(bank));
490 + bcm2708_gpio_irq_init(ucb);
492 + err = gpiochip_add(&ucb->gc);
499 +static int bcm2708_gpio_remove(struct platform_device *dev)
502 + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
504 + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
506 + gpiochip_remove(&ucb->gc);
508 + platform_set_drvdata(dev, NULL);
514 +static struct platform_driver bcm2708_gpio_driver = {
515 + .probe = bcm2708_gpio_probe,
516 + .remove = bcm2708_gpio_remove,
518 + .name = "bcm2708_gpio"},
521 +static int __init bcm2708_gpio_init(void)
523 + return platform_driver_register(&bcm2708_gpio_driver);
526 +static void __exit bcm2708_gpio_exit(void)
528 + platform_driver_unregister(&bcm2708_gpio_driver);
531 +module_init(bcm2708_gpio_init);
532 +module_exit(bcm2708_gpio_exit);
534 +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
535 +MODULE_LICENSE("GPL");
537 +++ b/arch/arm/mach-bcm2708/include/mach/gpio.h
540 + * arch/arm/mach-bcm2708/include/mach/gpio.h
542 + * This file is licensed under the terms of the GNU General Public
543 + * License version 2. This program is licensed "as is" without any
544 + * warranty of any kind, whether express or implied.
547 +#ifndef __ASM_ARCH_GPIO_H
548 +#define __ASM_ARCH_GPIO_H
550 +#define BCM2708_NR_GPIOS 54 // number of gpio lines
552 +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
553 +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
556 --- a/arch/arm/mach-bcm2709/bcm2709.c
557 +++ b/arch/arm/mach-bcm2709/bcm2709.c
558 @@ -329,6 +329,31 @@ static struct platform_device bcm2708_vc
562 +#ifdef CONFIG_BCM2708_GPIO
563 +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
565 +static struct resource bcm2708_gpio_resources[] = {
566 + [0] = { /* general purpose I/O */
567 + .start = GPIO_BASE,
568 + .end = GPIO_BASE + SZ_4K - 1,
569 + .flags = IORESOURCE_MEM,
573 +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
575 +static struct platform_device bcm2708_gpio_device = {
576 + .name = BCM_GPIO_DRIVER_NAME,
577 + .id = -1, /* only one VideoCore I/O area */
578 + .resource = bcm2708_gpio_resources,
579 + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
581 + .dma_mask = &gpio_dmamask,
582 + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
587 int __init bcm_register_device(struct platform_device *pdev)
591 +++ b/include/linux/platform_data/bcm2708.h
594 + * include/linux/platform_data/bcm2708.h
596 + * This program is free software; you can redistribute it and/or modify
597 + * it under the terms of the GNU General Public License version 2 as
598 + * published by the Free Software Foundation.
600 + * (C) 2014 Julian Scheel <julian@jusst.de>
603 +#ifndef __BCM2708_H_
604 +#define __BCM2708_H_
610 +} bcm2708_gpio_pull_t;
612 +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
613 + bcm2708_gpio_pull_t value);