Skip to contentSkip to conversation

gpio: driver for PrimeCell PL061 GPIO controller

PATCH Superseded Baruch Siach 2 participants 3 messages May 19, 2009 – May 31, 2009 GPIO

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: driver for PrimeCell PL061 GPIO controller

---
 drivers/gpio/Kconfig       |    5 +
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/pl061.c       |  336 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/gpio/pl061.h |   18 +++
 4 files changed, 360 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/pl061.c
 create mode 100644 include/linux/gpio/pl061.h
drivers/gpio/Kconfig +5 -0
index edb0253..30d7948 100644
@@ -67,6 +67,11 @@ config GPIO_SYSFS
 
 comment "Memory mapped GPIO expanders:"
 
+config GPIO_PL061
+	bool "PrimeCell PL061 GPIO support"
+	help
+	  Say yes here to support the PrimeCell PL061 GPIO device
+
 config GPIO_XILINX
 	bool "Xilinx GPIO support"
 	depends on PPC_OF
drivers/gpio/Makefile +1 -0
index 49ac64e..ef90203 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_GPIO_MAX732X)	+= max732x.o
 obj-$(CONFIG_GPIO_MCP23S08)	+= mcp23s08.o
 obj-$(CONFIG_GPIO_PCA953X)	+= pca953x.o
 obj-$(CONFIG_GPIO_PCF857X)	+= pcf857x.o
+obj-$(CONFIG_GPIO_PL061)	+= pl061.o
 obj-$(CONFIG_GPIO_TWL4030)	+= twl4030-gpio.o
 obj-$(CONFIG_GPIO_XILINX)	+= xilinx_gpio.o
 obj-$(CONFIG_GPIO_BT8XX)	+= bt8xxgpio.o
drivers/gpio/pl061.c new file +336 -0

336 lines not on this page · raw message

include/linux/gpio/pl061.h new file +18 -0
new file mode 100644
index 0000000..9e18fa3
@@ -0,0 +1,18 @@
+/* platform data for the PL061 GPIO driver */
+
+#define GPIODIR 0x400
+#define GPIOIS  0x404
+#define GPIOIBE 0x408
+#define GPIOIEV 0x40C
+#define GPIOIE  0x410
+#define GPIORIS 0x414
+#define GPIOMIS 0x418
+#define GPIOIC  0x41C
+
+struct pl061_platform_data {
+	/* number of the first GPIO */
+	unsigned	gpio_base;
+	u32		(*pending_irqs)(int irq);
+	u8		directions;	/* startup directions, 1: out, 0: in */
+	u8		values;		/* startup values */
+};
Signature · 1 line
1.6.2.4
#1 lore raw
Quoted 2 lines · On Tue, 19 May 2009 10:35:29 +0300 Baruch Siach <[email protected]> wrote:

Patch looks nice and simple. Was there really nothing you can think of for a changelog? Like, what's a "PrimeCell PL061 GPIO"? What hardware would one find it on? Is it possible that a "PrimeCell PL061 GPIO" can appear on any CPU architecture at all, or is it specific to arm/x86/whatever?

etc.

drivers/gpio/Kconfig config GPIO_SYSFS · line 74

Quoted 11 lines
drivers/gpio/Kconfig +5 -0
@@ -67,6 +67,11 @@ config GPIO_SYSFS
 
 comment "Memory mapped GPIO expanders:"
 
+config GPIO_PL061
+	bool "PrimeCell PL061 GPIO support"
+	help
+	  Say yes here to support the PrimeCell PL061 GPIO device
+

hm, so gpio drivers can't be loaded as modules?

Quoted 25 lines

...

drivers/gpio/pl061.c new file +19 -0
@@ -0,0 +1,336 @@
+/*
+ *  linux/drivers/gpio/pl061.c
+ *
+ *  Copyright (C) 2008, 2009 Provigent Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
+ *
+ * Data sheet: ARM DDI 0190B, September 2000
+ */
+#include <linux/spinlock.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/io.h>
+#include <linux/irq.h>

drivers/gpio/pl061.c line 29

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+#include <linux/bitops.h>
+#include <linux/workqueue.h>
+#include <linux/gpio.h>
+#include <linux/gpio/pl061.h>
+
+#define PL061_GPIO_NR	8
+
+struct pl061_gpio {
+	spinlock_t		lock;
+	spinlock_t		irq_lock;

It's unclear (to me) why this has two different locks. Perhaps only one was needed. If we indeed need two locks then please add code comments which explain the role of each one. And the nesting rules if appropriate.

Quoted 9 lines
drivers/gpio/pl061.c new file +9 -0
+	void __iomem		*base;
+	struct gpio_chip	gc;
+	struct work_struct	gpio_free_work;
+	DECLARE_BITMAP(gpios_to_free, PL061_GPIO_NR);
+};
+
+static u32 (*pl061_pending_irq)(int irq);
+
+static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)

drivers/gpio/pl061.c line 48

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+{
+	struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
+	unsigned long flags;
+	unsigned char gpiodir;
+
+	if (offset >= gc->ngpio)
+		return -EINVAL;
+
+	spin_lock_irqsave(&chip->lock, flags);
+	gpiodir = readb (chip->base + GPIODIR);

Please pass this patch (and indeed, all patches) through scripts/checkpatch.pl and consider the resulting output.

Quoted 9 lines
drivers/gpio/pl061.c new file +7 -0
+	gpiodir &= ~(1 << offset);
+	writeb(gpiodir, chip->base + GPIODIR);
+	spin_unlock_irqrestore(&chip->lock, flags);
+
+	return 0;
+}
+
 
...

drivers/gpio/pl061.c line 116

Quoted 10 lines
drivers/gpio/pl061.c new file +9 -0
 
+static void pl061_irq_shutdown(unsigned irq)
+{
+	struct pl061_gpio *chip = get_irq_chip_data(irq);
+	int offset = irq_to_gpio(irq) - chip->gc.base;
+
+	pl061_irq_disable(irq);
+	set_bit(offset, chip->gpios_to_free);
+	schedule_work(&chip->gpio_free_work);
+}

It's usually a bug to add a schedule_work() and no corresponding flush_work()/cancel_work_sync()/etc.

Because the callback might still be pending after module removal, device shutdown, etc.

If this is for some reason not a bug, I'd suggest that code somments be added explaining why.

Quoted 7 lines

...

drivers/gpio/pl061.c new file +4 -0
+static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
+{
+	desc->chip->ack(irq);
+	while (1) {

drivers/gpio/pl061.c line 216

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+		unsigned long pending;
+		int gpio;
+
+		pending = pl061_pending_irq(irq);
+		if (pending == 0)
+			break;
+
+		for_each_bit(gpio, &pending, BITS_PER_LONG) {
+			generic_handle_irq(gpio_to_irq(gpio));
+		}

The braces are superfluous, but checkpatch misses this.

Quoted 8 lines
drivers/gpio/pl061.c new file +8 -0
+	}
+	desc->chip->unmask(irq);
+}
+
+static void pl061_gpio_free(struct work_struct *work)
+{
+	struct pl061_gpio *chip = container_of(work, struct pl061_gpio,
+			gpio_free_work);

drivers/gpio/pl061.c line 234

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+	int offset;
+
+	while (!bitmap_empty(chip->gpios_to_free, PL061_GPIO_NR)) {
+		for_each_bit(offset, chip->gpios_to_free, PL061_GPIO_NR) {
+			int gpio = offset + chip->gc.base;
+
+			if (test_and_clear_bit(offset, chip->gpios_to_free))
+				gpio_free(gpio);
+		}
+	}

Does this function need to do multiple passes over gpios_to_free like this? It seems unnecessary.

If it is indeed needed then please add a comment explaining what's going on.

Quoted 23 lines
drivers/gpio/pl061.c new file +23 -0
+}
+
+static int __init pl061_probe(struct platform_device *dev)
+{
+	struct pl061_platform_data *pdata;
+	struct pl061_gpio *chip;
+	struct resource *r;
+	int ret, irq, i;
+
+	pdata = dev->dev.platform_data;
+	if (pdata == NULL)
+		return -ENODEV;
+
+	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
+	if (chip == NULL)
+		return -ENOMEM;
+
+	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
+	if (r == NULL) {
+		ret = -ENODEV;
+		goto free_mem;
+	}
+

drivers/gpio/pl061.c line 267

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+	chip->base = ioremap(r->start, r->end - r->start + 1);
+	if (chip->base == NULL) {
+		ret = -ENOMEM;
+		goto free_mem;
+	}
+
+	spin_lock_init(&chip->lock);
+	spin_lock_init(&chip->irq_lock);
+
+	bitmap_zero(chip->gpios_to_free, PL061_GPIO_NR);

Well. kzalloc() already did that.

Quoted 55 lines
drivers/gpio/pl061.c new file +52 -0
+	INIT_WORK(&chip->gpio_free_work, pl061_gpio_free);
+
+	chip->gc.direction_input = pl061_direction_input;
+	chip->gc.direction_output = pl061_direction_output;
+	chip->gc.get = pl061_get_value;
+	chip->gc.set = pl061_set_value;
+	chip->gc.base = pdata->gpio_base;
+	chip->gc.ngpio = PL061_GPIO_NR;
+	chip->gc.label = dev->name;
+	chip->gc.dev = &dev->dev;
+	chip->gc.owner = THIS_MODULE;
+
+	ret = gpiochip_add(&chip->gc);
+	if (ret)
+		goto iounmap;
+
+	/*
+	 * irq_chip support
+	 */
+	writeb(0, chip->base + GPIOIE); /* disable irqs */
+	irq = platform_get_irq(dev, 0);
+	if (irq < 0) {
+		ret = -ENODEV;
+		goto iounmap;
+	}
+	set_irq_chained_handler(irq, pl061_irq_handler);
+	pl061_pending_irq = pdata->pending_irqs;
+
+	for (i = 0; i < PL061_GPIO_NR; i++) {
+		if (pdata->directions & (1 << i))
+			pl061_direction_output(&chip->gc, i,
+					pdata->values & (1 << i));
+		else
+			pl061_direction_input(&chip->gc, i);
+
+		set_irq_chip(gpio_to_irq(i+pdata->gpio_base), &pl061_irqchip);
+		set_irq_handler(gpio_to_irq(i+pdata->gpio_base),
+				handle_simple_irq);
+		set_irq_flags(gpio_to_irq(i+pdata->gpio_base), IRQF_VALID);
+		set_irq_chip_data(gpio_to_irq(i+pdata->gpio_base), chip);
+	}
+
+	return 0;
+
+iounmap:
+	iounmap(chip->base);
+free_mem:
+	kfree(chip);
+
+	return ret;
+}
+
 
...
#2 lore raw

Hi Andrew,

Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Tue, 19 May 2009 10:35:29 +0300 Baruch Siach <[email protected]> wrote:
Quoted 7 lines

Patch looks nice and simple. Was there really nothing you can think of for a changelog? Like, what's a "PrimeCell PL061 GPIO"? What hardware would one find it on? Is it possible that a "PrimeCell PL061 GPIO" can appear on any CPU architecture at all, or is it specific to arm/x86/whatever?

etc.

OK. I'll add a changelog.

Quoted 6 lines
Quoted 6 lines
drivers/gpio/Kconfig +0 -0
@@ -67,6 +67,11 @@ config GPIO_SYSFS
 
 comment "Memory mapped GPIO expanders:"
 
Quoted 7 lines

drivers/gpio/Kconfig config GPIO_SYSFS · line 74

Quoted 5 lines
drivers/gpio/Kconfig +5 -0
+config GPIO_PL061
+	bool "PrimeCell PL061 GPIO support"
+	help
+	  Say yes here to support the PrimeCell PL061 GPIO device
+

hm, so gpio drivers can't be loaded as modules?

I guess gpio drives can be loaded as modules, but this is also an interrupt controller. Is there a safe way to unload an interrupt controller? Is there an example of a driver doing this?

Quoted 31 lines
Quoted 31 lines

...

drivers/gpio/pl061.c new file +26 -0
@@ -0,0 +1,336 @@
+/*
+ *  linux/drivers/gpio/pl061.c
+ *
+ *  Copyright (C) 2008, 2009 Provigent Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
+ *
+ * Data sheet: ARM DDI 0190B, September 2000
+ */
+#include <linux/spinlock.h>
+#include <linux/errno.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/bitops.h>
+#include <linux/workqueue.h>
+#include <linux/gpio.h>
+#include <linux/gpio/pl061.h>
+
+#define PL061_GPIO_NR	8
+
Quoted 8 lines

drivers/gpio/pl061.c line 29

Quoted 3 lines
drivers/gpio/pl061.c new file +3 -0
+struct pl061_gpio {
+	spinlock_t		lock;
+	spinlock_t		irq_lock;

It's unclear (to me) why this has two different locks. Perhaps only one was needed. If we indeed need two locks then please add code comments which explain the role of each one. And the nesting rules if appropriate.

The first lock protects the GPIO related hardware registers. The irq_lock protects the interrupt handling registers. They are mostly independent from each other. I'll add a comment explaining this.

Quoted 14 lines
Quoted 14 lines
drivers/gpio/pl061.c new file +14 -0
+	void __iomem		*base;
+	struct gpio_chip	gc;
+	struct work_struct	gpio_free_work;
+	DECLARE_BITMAP(gpios_to_free, PL061_GPIO_NR);
+};
+
+static u32 (*pl061_pending_irq)(int irq);
+
+static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
+{
+	struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
+	unsigned long flags;
+	unsigned char gpiodir;
+
Quoted 8 lines

drivers/gpio/pl061.c line 48

Quoted 5 lines
drivers/gpio/pl061.c new file +5 -0
+	if (offset >= gc->ngpio)
+		return -EINVAL;
+
+	spin_lock_irqsave(&chip->lock, flags);
+	gpiodir = readb (chip->base + GPIODIR);

Please pass this patch (and indeed, all patches) through scripts/checkpatch.pl and consider the resulting output.

OK.

Quoted 23 lines
Quoted 9 lines
drivers/gpio/pl061.c new file +7 -0
+	gpiodir &= ~(1 << offset);
+	writeb(gpiodir, chip->base + GPIODIR);
+	spin_unlock_irqrestore(&chip->lock, flags);
+
+	return 0;
+}
+
 
...

drivers/gpio/pl061.c line 116

Quoted 10 lines
drivers/gpio/pl061.c new file +9 -0
 
+static void pl061_irq_shutdown(unsigned irq)
+{
+	struct pl061_gpio *chip = get_irq_chip_data(irq);
+	int offset = irq_to_gpio(irq) - chip->gc.base;
+
+	pl061_irq_disable(irq);
+	set_bit(offset, chip->gpios_to_free);
+	schedule_work(&chip->gpio_free_work);
+}

It's usually a bug to add a schedule_work() and no corresponding flush_work()/cancel_work_sync()/etc.

Quoted 5 lines

Because the callback might still be pending after module removal, device shutdown, etc.

If this is for some reason not a bug, I'd suggest that code somments be added explaining why.

Is this still a bug when the driver can't be removed?

Quoted 18 lines
Quoted 6 lines

...

drivers/gpio/pl061.c new file +4 -0
+static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
+{
+	desc->chip->ack(irq);
+	while (1) {

drivers/gpio/pl061.c line 216

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+		unsigned long pending;
+		int gpio;
+
+		pending = pl061_pending_irq(irq);
+		if (pending == 0)
+			break;
+
+		for_each_bit(gpio, &pending, BITS_PER_LONG) {
+			generic_handle_irq(gpio_to_irq(gpio));
+		}

The braces are superfluous, but checkpatch misses this.

OK.

Quoted 22 lines
Quoted 8 lines
drivers/gpio/pl061.c new file +8 -0
+	}
+	desc->chip->unmask(irq);
+}
+
+static void pl061_gpio_free(struct work_struct *work)
+{
+	struct pl061_gpio *chip = container_of(work, struct pl061_gpio,
+			gpio_free_work);

drivers/gpio/pl061.c line 234

Quoted 10 lines
drivers/gpio/pl061.c new file +10 -0
+	int offset;
+
+	while (!bitmap_empty(chip->gpios_to_free, PL061_GPIO_NR)) {
+		for_each_bit(offset, chip->gpios_to_free, PL061_GPIO_NR) {
+			int gpio = offset + chip->gc.base;
+
+			if (test_and_clear_bit(offset, chip->gpios_to_free))
+				gpio_free(gpio);
+		}
+	}

Does this function need to do multiple passes over gpios_to_free like this? It seems unnecessary.

Quoted 2 lines

If it is indeed needed then please add a comment explaining what's going on.

It's probably not needed. I'll remove this.

Quoted 27 lines
Quoted 27 lines
drivers/gpio/pl061.c new file +27 -0
+}
+
+static int __init pl061_probe(struct platform_device *dev)
+{
+	struct pl061_platform_data *pdata;
+	struct pl061_gpio *chip;
+	struct resource *r;
+	int ret, irq, i;
+
+	pdata = dev->dev.platform_data;
+	if (pdata == NULL)
+		return -ENODEV;
+
+	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
+	if (chip == NULL)
+		return -ENOMEM;
+
+	r = platform_get_resource(dev, IORESOURCE_MEM, 0);
+	if (r == NULL) {
+		ret = -ENODEV;
+		goto free_mem;
+	}
+
+	chip->base = ioremap(r->start, r->end - r->start + 1);
+	if (chip->base == NULL) {
+		ret = -ENOMEM;
+		goto free_mem;
Quoted 8 lines

drivers/gpio/pl061.c line 267

Quoted 6 lines
drivers/gpio/pl061.c new file +6 -0
+	}
+
+	spin_lock_init(&chip->lock);
+	spin_lock_init(&chip->irq_lock);
+
+	bitmap_zero(chip->gpios_to_free, PL061_GPIO_NR);

Well. kzalloc() already did that.

OK.

Quoted 53 lines
Quoted 53 lines
drivers/gpio/pl061.c new file +52 -0
+	INIT_WORK(&chip->gpio_free_work, pl061_gpio_free);
+
+	chip->gc.direction_input = pl061_direction_input;
+	chip->gc.direction_output = pl061_direction_output;
+	chip->gc.get = pl061_get_value;
+	chip->gc.set = pl061_set_value;
+	chip->gc.base = pdata->gpio_base;
+	chip->gc.ngpio = PL061_GPIO_NR;
+	chip->gc.label = dev->name;
+	chip->gc.dev = &dev->dev;
+	chip->gc.owner = THIS_MODULE;
+
+	ret = gpiochip_add(&chip->gc);
+	if (ret)
+		goto iounmap;
+
+	/*
+	 * irq_chip support
+	 */
+	writeb(0, chip->base + GPIOIE); /* disable irqs */
+	irq = platform_get_irq(dev, 0);
+	if (irq < 0) {
+		ret = -ENODEV;
+		goto iounmap;
+	}
+	set_irq_chained_handler(irq, pl061_irq_handler);
+	pl061_pending_irq = pdata->pending_irqs;
+
+	for (i = 0; i < PL061_GPIO_NR; i++) {
+		if (pdata->directions & (1 << i))
+			pl061_direction_output(&chip->gc, i,
+					pdata->values & (1 << i));
+		else
+			pl061_direction_input(&chip->gc, i);
+
+		set_irq_chip(gpio_to_irq(i+pdata->gpio_base), &pl061_irqchip);
+		set_irq_handler(gpio_to_irq(i+pdata->gpio_base),
+				handle_simple_irq);
+		set_irq_flags(gpio_to_irq(i+pdata->gpio_base), IRQF_VALID);
+		set_irq_chip_data(gpio_to_irq(i+pdata->gpio_base), chip);
+	}
+
+	return 0;
+
+iounmap:
+	iounmap(chip->base);
+free_mem:
+	kfree(chip);
+
+	return ret;
+}
+
Quoted 1 line
Quoted 1 line

...

baruch

Signature · 3 lines
                                                     ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - [email protected] - tel: +972.2.679.5364, http://www.tkos.co.il -