aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Vorontsov <cbouatmailru@gmail.com>2010-10-27 18:33:15 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-10-27 21:03:06 -0400
commitaeec56e331c6d2750de02ef34b305338305ca690 (patch)
tree1a9df159e4408cf1f4d9ab8451ae803de6007126
parentd0f744c8cbd19a8d07eccb15bb08e6a29c4d5192 (diff)
gpio: add driver for basic memory-mapped GPIO controllers
The basic GPIO controllers may be found in various on-board FPGA and ASIC solutions that are used to control board's switches, LEDs, chip-selects, Ethernet/USB PHY power, etc. These controllers may not provide any means of pin setup (in/out/open drain). The driver supports: - 8/16/32/64 bits registers; - GPIO controllers with clear/set registers; - GPIO controllers with a single "data" register; - Big endian bits/GPIOs ordering (mostly used on PowerPC). Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com> Reviewed-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Cc: David Brownell <david-b@pacbell.net> Cc: Samuel Ortiz <sameo@linux.intel.com>, Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--drivers/gpio/Kconfig5
-rw-r--r--drivers/gpio/Makefile1
-rw-r--r--drivers/gpio/basic_mmio_gpio.c297
-rw-r--r--include/linux/basic_mmio_gpio.h20
4 files changed, 323 insertions, 0 deletions
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 510aa2054544..e47ef94be379 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -70,6 +70,11 @@ config GPIO_MAX730X
70 70
71comment "Memory mapped GPIO expanders:" 71comment "Memory mapped GPIO expanders:"
72 72
73config GPIO_BASIC_MMIO
74 tristate "Basic memory-mapped GPIO controllers support"
75 help
76 Say yes here to support basic memory-mapped GPIO controllers.
77
73config GPIO_IT8761E 78config GPIO_IT8761E
74 tristate "IT8761E GPIO support" 79 tristate "IT8761E GPIO support"
75 depends on GPIOLIB 80 depends on GPIOLIB
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index fc6019d93720..3ff0651fd173 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -10,6 +10,7 @@ obj-$(CONFIG_GPIOLIB) += gpiolib.o
10 10
11obj-$(CONFIG_GPIO_ADP5520) += adp5520-gpio.o 11obj-$(CONFIG_GPIO_ADP5520) += adp5520-gpio.o
12obj-$(CONFIG_GPIO_ADP5588) += adp5588-gpio.o 12obj-$(CONFIG_GPIO_ADP5588) += adp5588-gpio.o
13obj-$(CONFIG_GPIO_BASIC_MMIO) += basic_mmio_gpio.o
13obj-$(CONFIG_GPIO_LANGWELL) += langwell_gpio.o 14obj-$(CONFIG_GPIO_LANGWELL) += langwell_gpio.o
14obj-$(CONFIG_GPIO_MAX730X) += max730x.o 15obj-$(CONFIG_GPIO_MAX730X) += max730x.o
15obj-$(CONFIG_GPIO_MAX7300) += max7300.o 16obj-$(CONFIG_GPIO_MAX7300) += max7300.o
diff --git a/drivers/gpio/basic_mmio_gpio.c b/drivers/gpio/basic_mmio_gpio.c
new file mode 100644
index 000000000000..3addea65894e
--- /dev/null
+++ b/drivers/gpio/basic_mmio_gpio.c
@@ -0,0 +1,297 @@
1/*
2 * Driver for basic memory-mapped GPIO controllers.
3 *
4 * Copyright 2008 MontaVista Software, Inc.
5 * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
13 * ...`` ```````..
14 * ..The simplest form of a GPIO controller that the driver supports is``
15 * `.just a single "data" register, where GPIO state can be read and/or `
16 * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
17 * `````````
18 ___
19_/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,...
20__________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO .
21o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
22 `....trivial..'~`.```.```
23 * ```````
24 * .```````~~~~`..`.``.``.
25 * . The driver supports `... ,..```.`~~~```````````````....````.``,,
26 * . big-endian notation, just`. .. A bit more sophisticated controllers ,
27 * . register the device with -be`. .with a pair of set/clear-bit registers ,
28 * `.. suffix. ```~~`````....`.` . affecting the data register and the .`
29 * ``.`.``...``` ```.. output pins are also supported.`
30 * ^^ `````.`````````.,``~``~``~~``````
31 * . ^^
32 * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
33 * .. The expectation is that in at least some cases . ,-~~~-,
34 * .this will be used with roll-your-own ASIC/FPGA .` \ /
35 * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ /
36 * ..````````......``````````` \o_
37 * |
38 * ^^ / \
39 *
40 * ...`````~~`.....``.`..........``````.`.``.```........``.
41 * ` 8, 16, 32 and 64 bits registers are supported, and``.
42 * . the number of GPIOs is determined by the width of ~
43 * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
44 * `.......````.```
45 */
46
47#include <linux/init.h>
48#include <linux/bug.h>
49#include <linux/kernel.h>
50#include <linux/module.h>
51#include <linux/spinlock.h>
52#include <linux/compiler.h>
53#include <linux/types.h>
54#include <linux/errno.h>
55#include <linux/log2.h>
56#include <linux/ioport.h>
57#include <linux/io.h>
58#include <linux/gpio.h>
59#include <linux/slab.h>
60#include <linux/platform_device.h>
61#include <linux/mod_devicetable.h>
62#include <linux/basic_mmio_gpio.h>
63
64struct bgpio_chip {
65 struct gpio_chip gc;
66 void __iomem *reg_dat;
67 void __iomem *reg_set;
68 void __iomem *reg_clr;
69
70 /* Number of bits (GPIOs): <register width> * 8. */
71 int bits;
72
73 /*
74 * Some GPIO controllers work with the big-endian bits notation,
75 * e.g. in a 8-bits register, GPIO7 is the least significant bit.
76 */
77 int big_endian_bits;
78
79 /*
80 * Used to lock bgpio_chip->data. Also, this is needed to keep
81 * shadowed and real data registers writes together.
82 */
83 spinlock_t lock;
84
85 /* Shadowed data register to clear/set bits safely. */
86 unsigned long data;
87};
88
89static struct bgpio_chip *to_bgpio_chip(struct gpio_chip *gc)
90{
91 return container_of(gc, struct bgpio_chip, gc);
92}
93
94static unsigned long bgpio_in(struct bgpio_chip *bgc)
95{
96 switch (bgc->bits) {
97 case 8:
98 return __raw_readb(bgc->reg_dat);
99 case 16:
100 return __raw_readw(bgc->reg_dat);
101 case 32:
102 return __raw_readl(bgc->reg_dat);
103#if BITS_PER_LONG >= 64
104 case 64:
105 return __raw_readq(bgc->reg_dat);
106#endif
107 }
108 return -EINVAL;
109}
110
111static void bgpio_out(struct bgpio_chip *bgc, void __iomem *reg,
112 unsigned long data)
113{
114 switch (bgc->bits) {
115 case 8:
116 __raw_writeb(data, reg);
117 return;
118 case 16:
119 __raw_writew(data, reg);
120 return;
121 case 32:
122 __raw_writel(data, reg);
123 return;
124#if BITS_PER_LONG >= 64
125 case 64:
126 __raw_writeq(data, reg);
127 return;
128#endif
129 }
130}
131
132static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
133{
134 if (bgc->big_endian_bits)
135 return 1 << (bgc->bits - 1 - pin);
136 else
137 return 1 << pin;
138}
139
140static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
141{
142 struct bgpio_chip *bgc = to_bgpio_chip(gc);
143
144 return bgpio_in(bgc) & bgpio_pin2mask(bgc, gpio);
145}
146
147static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
148{
149 struct bgpio_chip *bgc = to_bgpio_chip(gc);
150 unsigned long mask = bgpio_pin2mask(bgc, gpio);
151 unsigned long flags;
152
153 if (bgc->reg_set) {
154 if (val)
155 bgpio_out(bgc, bgc->reg_set, mask);
156 else
157 bgpio_out(bgc, bgc->reg_clr, mask);
158 return;
159