aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-11-12 01:50:46 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-11-12 01:50:46 -0500
commitc2d33069915d1f9b3b1dcc2199af11d4e072b037 (patch)
tree2664fd3c41c2f3223cfbb64793c2ddb8e9087ded
parent8a5dc585d50015af9c079ae2d182dc4c1cd22914 (diff)
parent993571273275bfecb5161806796eb368db234106 (diff)
Merge tag 'gpio-v3.13-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO changes from Linus Walleij: "Here is the bulk of GPIO changes for the v3.13 development cycle. I've got ACKs for the things that affect other subsystems (or it's my own subsystem, like pinctrl). Most of that pertain to an attempt from my side to consolidate and get rid of custom GPIO implementations in the ARM tree. I will continue doing this. The main change this time is the new GPIO descriptor API, background for this can be found in Corbet's summary from this january in LWN: http://lwn.net/Articles/533632/ Summary: - Merged the GPIO descriptor API from Alexandre Courbot. This is a first step toward trying to get rid of the global GPIO numberspace for the future. - Add an API so that driver can flag that a certain GPIO line is being used by a irqchip backend for generating IRQs, so that we can enforce checks, like not allowing users to switch that line to an output at runtime, since this makes no sense. Implemented corresponding calls in a few select drivers. - ACPI GPIO cleanups, refactorings and switch to using the descriptor-based interface. - Support for the TPS80036 Palmas GPIO variant. - A new driver for the Broadcom Kona GPIO SoC IP block. - Device tree support for the PCF857x driver. - A set of ARM GPIO refactorings with the goal of getting rid of a bunch of custom GPIO implementations from the arch/arm/* tree: * Move the IOP GPIO driver to the GPIO subsystem and fix all users to use the gpiolib API for accessing GPIOs. Delete the old custom GPIO implementation. * Delete the unused custom PXA GPIO implemention. * Convert all users of the IXP4 custom GPIO implementation to use gpiolib and delete the custom implementation. * Delete the custom Gemini GPIO implementation, also completely unused. - Various cleanups and renamings" * tag 'gpio-v3.13-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (85 commits) gpio: gpio-mxs: Remove unneeded dt checks gpio: pl061: don't depend on CONFIG_ARM gpio: bcm-kona: add missing .owner to struct gpio_chip gpiolib: provide a declaration of seq_file in gpio/driver.h gpiolib: include gpio/consumer.h in of_gpio.h for desc_to_gpio() gpio: provide stubs for devres gpio functions gpiolib: devres: add missing headers gpiolib: make GPIO_DEVRES depend on GPIOLIB gpiolib: devres: fix devm_gpiod_get_index() gpiolib / ACPI: document the GPIO descriptor based interface gpiolib / ACPI: allow passing GPIOF_ACTIVE_LOW for GpioInt resources gpiolib / ACPI: add ACPI support for gpiod_get_index() gpiolib / ACPI: convert to gpiod interfaces gpiolib: add gpiod_get() and gpiod_put() functions gpiolib: port of_ functions to use gpiod gpiolib: export descriptor-based GPIO interface Fixup "MAINTAINERS: GPIO-INTEL-MID: add maintainer" gpio: bcm281xx: Don't print addresses of GPIO area in probe() gpio: tegra: use new gpio_lock_as_irq() API gpio: rcar: Include linux/of.h header ...
-rw-r--r--Documentation/acpi/enumeration.txt26
-rw-r--r--Documentation/devicetree/bindings/gpio/gpio-bcm-kona.txt52
-rw-r--r--Documentation/devicetree/bindings/gpio/gpio-pcf857x.txt71
-rw-r--r--MAINTAINERS6
-rw-r--r--arch/arm/Kconfig7
-rw-r--r--arch/arm/include/asm/hardware/iop3xx-gpio.h75
-rw-r--r--arch/arm/include/asm/hardware/iop3xx.h12
-rw-r--r--arch/arm/mach-gemini/gpio.c2
-rw-r--r--arch/arm/mach-gemini/include/mach/gpio.h20
-rw-r--r--arch/arm/mach-iop32x/em7210.c2
-rw-r--r--arch/arm/mach-iop32x/glantank.c2
-rw-r--r--arch/arm/mach-iop32x/gpio-iop32x.h10
-rw-r--r--arch/arm/mach-iop32x/include/mach/gpio.h6
-rw-r--r--arch/arm/mach-iop32x/include/mach/iop32x.h1
-rw-r--r--arch/arm/mach-iop32x/iq31244.c2
-rw-r--r--arch/arm/mach-iop32x/iq80321.c2
-rw-r--r--arch/arm/mach-iop32x/n2100.c47
-rw-r--r--arch/arm/mach-iop33x/include/mach/gpio.h6
-rw-r--r--arch/arm/mach-iop33x/include/mach/iop33x.h1
-rw-r--r--arch/arm/mach-iop33x/iq80331.c7
-rw-r--r--arch/arm/mach-iop33x/iq80332.c7
-rw-r--r--arch/arm/mach-ixp4xx/common.c49
-rw-r--r--arch/arm/mach-ixp4xx/dsmg600-setup.c65
-rw-r--r--arch/arm/mach-ixp4xx/include/mach/platform.h39
-rw-r--r--arch/arm/mach-ixp4xx/ixdp425-setup.c8
-rw-r--r--arch/arm/mach-ixp4xx/nas100d-setup.c49
-rw-r--r--arch/arm/mach-ixp4xx/nslu2-setup.c17
-rw-r--r--arch/arm/mach-mmp/include/mach/gpio.h8
-rw-r--r--arch/arm/mach-pxa/include/mach/gpio.h32
-rw-r--r--arch/arm/mach-w90x900/include/mach/gpio.h30
-rw-r--r--arch/arm/plat-iop/Makefile2
-rw-r--r--drivers/gpio/Kconfig35
-rw-r--r--drivers/gpio/Makefile4
-rw-r--r--drivers/gpio/devres.c85
-rw-r--r--drivers/gpio/gpio-74x164.c5
-rw-r--r--drivers/gpio/gpio-adnp.c8
-rw-r--r--drivers/gpio/gpio-arizona.c4
-rw-r--r--drivers/gpio/gpio-bcm-kona.c640
-rw-r--r--drivers/gpio/gpio-bt8xx.c2
-rw-r--r--drivers/gpio/gpio-clps711x.c2
-rw-r--r--drivers/gpio/gpio-em.c13
-rw-r--r--drivers/gpio/gpio-ep93xx.c18
-rw-r--r--drivers/gpio/gpio-intel-mid.c471
-rw-r--r--drivers/gpio/gpio-iop.c (renamed from arch/arm/plat-iop/gpio.c)67
-rw-r--r--drivers/gpio/gpio-langwell.c397
-rw-r--r--drivers/gpio/gpio-lpc32xx.c1
-rw-r--r--drivers/gpio/gpio-lynxpoint.c19
-rw-r--r--drivers/gpio/gpio-mc33880.c3
-rw-r--r--drivers/gpio/gpio-mpc8xxx.c8
-rw-r--r--drivers/gpio/gpio-mxs.c32
-rw-r--r--drivers/gpio/gpio-omap.c20
-rw-r--r--drivers/gpio/gpio-palmas.c104
-rw-r--r--drivers/gpio/gpio-pca953x.c11
-rw-r--r--drivers/gpio/gpio-pcf857x.c105
-rw-r--r--drivers/gpio/gpio-pl061.c10
-rw-r--r--drivers/gpio/gpio-rcar.c13
-rw-r--r--drivers/gpio/gpio-stmpe.c25
-rw-r--r--drivers/gpio/gpio-tc3589x.c36
-rw-r--r--drivers/gpio/gpio-tegra.c18
-rw-r--r--drivers/gpio/gpio-twl4030.c2
-rw-r--r--drivers/gpio/gpiolib-acpi.c138
-rw-r--r--drivers/gpio/gpiolib-of.c28
-rw-r--r--drivers/gpio/gpiolib.c828
-rw-r--r--drivers/input/misc/ixp4xx-beeper.c25
-rw-r--r--drivers/pinctrl/pinctrl-coh901.c5
-rw-r--r--drivers/pinctrl/pinctrl-nomadik.c5
-rw-r--r--drivers/ptp/ptp_ixp46x.c9
-rw-r--r--drivers/staging/media/lirc/lirc_serial.c16
-rw-r--r--include/asm-generic/gpio.h225
-rw-r--r--include/linux/acpi_gpio.h31
-rw-r--r--include/linux/gpio.h57
-rw-r--r--include/linux/gpio/consumer.h253
-rw-r--r--include/linux/gpio/driver.h184
-rw-r--r--include/linux/of_gpio.h29
74 files changed, 3199 insertions, 1455 deletions
diff --git a/Documentation/acpi/enumeration.txt b/Documentation/acpi/enumeration.txt
index aca4e69121b7..b994bcb32b92 100644
--- a/Documentation/acpi/enumeration.txt
+++ b/Documentation/acpi/enumeration.txt
@@ -295,10 +295,6 @@ These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
295specifies the path to the controller. In order to use these GPIOs in Linux 295specifies the path to the controller. In order to use these GPIOs in Linux
296we need to translate them to the Linux GPIO numbers. 296we need to translate them to the Linux GPIO numbers.
297 297
298The driver can do this by including <linux/acpi_gpio.h> and then calling
299acpi_get_gpio(path, gpio). This will return the Linux GPIO number or
300negative errno if there was no translation found.
301
302In a simple case of just getting the Linux GPIO number from device 298In a simple case of just getting the Linux GPIO number from device
303resources one can use acpi_get_gpio_by_index() helper function. It takes 299resources one can use acpi_get_gpio_by_index() helper function. It takes
304pointer to the device and index of the GpioIo/GpioInt descriptor in the 300pointer to the device and index of the GpioIo/GpioInt descriptor in the
@@ -322,3 +318,25 @@ suitable to the gpiolib before passing them.
322 318
323In case of GpioInt resource an additional call to gpio_to_irq() must be 319In case of GpioInt resource an additional call to gpio_to_irq() must be
324done before calling request_irq(). 320done before calling request_irq().
321
322Note that the above API is ACPI specific and not recommended for drivers
323that need to support non-ACPI systems. The recommended way is to use
324the descriptor based GPIO interfaces. The above example looks like this
325when converted to the GPIO desc:
326
327 #include <linux/gpio/consumer.h>
328 ...
329
330 struct gpio_desc *irq_desc, *power_desc;
331
332 irq_desc = gpiod_get_index(dev, NULL, 1);
333 if (IS_ERR(irq_desc))
334 /* handle error */
335
336 power_desc = gpiod_get_index(dev, NULL, 0);
337 if (IS_ERR(power_desc))
338 /* handle error */
339
340 /* Now we can use the GPIO descriptors */
341
342See also Documentation/gpio.txt.
diff --git a/Documentation/devicetree/bindings/gpio/gpio-bcm-kona.txt b/Documentation/devicetree/bindings/gpio/gpio-bcm-kona.txt
new file mode 100644
index 000000000000..4a63bc96b687
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio-bcm-kona.txt
@@ -0,0 +1,52 @@
1Broadcom Kona Family GPIO
2=========================
3
4This GPIO driver is used in the following Broadcom SoCs:
5 BCM11130, BCM11140, BCM11351, BCM28145, BCM28155
6
7The Broadcom GPIO Controller IP can be configured prior to synthesis to
8support up to 8 banks of 32 GPIOs where each bank has its own IRQ. The
9GPIO controller only supports edge, not level, triggering of interrupts.
10
11Required properties
12-------------------
13
14- compatible: "brcm,bcm11351-gpio", "brcm,kona-gpio"
15- reg: Physical base address and length of the controller's registers.
16- interrupts: The interrupt outputs from the controller. There is one GPIO
17 interrupt per GPIO bank. The number of interrupts listed depends on the
18 number of GPIO banks on the SoC. The interrupts must be ordered by bank,
19 starting with bank 0. There is always a 1:1 mapping between banks and
20 IRQs.
21- #gpio-cells: Should be <2>. The first cell is the pin number, the second
22 cell is used to specify optional parameters:
23 - bit 0 specifies polarity (0 for normal, 1 for inverted)
24 See also "gpio-specifier" in .../devicetree/bindings/gpio/gpio.txt.
25- #interrupt-cells: Should be <2>. The first cell is the GPIO number. The
26 second cell is used to specify flags. The following subset of flags is
27 supported:
28 - trigger type (bits[1:0]):
29 1 = low-to-high edge triggered.
30 2 = high-to-low edge triggered.
31 3 = low-to-high or high-to-low edge triggered
32 Valid values are 1, 2, 3
33 See also .../devicetree/bindings/interrupt-controller/interrupts.txt.
34- gpio-controller: Marks the device node as a GPIO controller.
35- interrupt-controller: Marks the device node as an interrupt controller.
36
37Example:
38 gpio: gpio@35003000 {
39 compatible = "brcm,bcm11351-gpio", "brcm,kona-gpio";
40 reg = <0x35003000 0x800>;
41 interrupts =
42 <GIC_SPI 106 IRQ_TYPE_LEVEL_HIGH
43 GIC_SPI 115 IRQ_TYPE_LEVEL_HIGH
44 GIC_SPI 114 IRQ_TYPE_LEVEL_HIGH
45 GIC_SPI 113 IRQ_TYPE_LEVEL_HIGH
46 GIC_SPI 112 IRQ_TYPE_LEVEL_HIGH
47 GIC_SPI 111 IRQ_TYPE_LEVEL_HIGH>;
48 #gpio-cells = <2>;
49 #interrupt-cells = <2>;
50 gpio-controller;
51 interrupt-controller;
52 };
diff --git a/Documentation/devicetree/bindings/gpio/gpio-pcf857x.txt b/Documentation/devicetree/bindings/gpio/gpio-pcf857x.txt
new file mode 100644
index 000000000000..d63194a2c848
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio-pcf857x.txt
@@ -0,0 +1,71 @@
1* PCF857x-compatible I/O expanders
2
3The PCF857x-compatible chips have "quasi-bidirectional" I/O lines that can be
4driven high by a pull-up current source or driven low to ground. This combines
5the direction and output level into a single bit per line, which can't be read
6back. We can't actually know at initialization time whether a line is configured
7(a) as output and driving the signal low/high, or (b) as input and reporting a
8low/high value, without knowing the last value written since the chip came out
9of reset (if any). The only reliable solution for setting up line direction is
10thus to do it explicitly.
11
12Required Properties:
13
14 - compatible: should be one of the following.
15 - "maxim,max7328": For the Maxim MAX7378
16 - "maxim,max7329": For the Maxim MAX7329
17 - "nxp,pca8574": For the NXP PCA8574
18 - "nxp,pca8575": For the NXP PCA8575
19 - "nxp,pca9670": For the NXP PCA9670
20 - "nxp,pca9671": For the NXP PCA9671
21 - "nxp,pca9672": For the NXP PCA9672
22 - "nxp,pca9673": For the NXP PCA9673
23 - "nxp,pca9674": For the NXP PCA9674
24 - "nxp,pca9675": For the NXP PCA9675
25 - "nxp,pcf8574": For the NXP PCF8574
26 - "nxp,pcf8574a": For the NXP PCF8574A
27 - "nxp,pcf8575": For the NXP PCF8575
28 - "ti,tca9554": For the TI TCA9554
29
30 - reg: I2C slave address.
31
32 - gpio-controller: Marks the device node as a gpio controller.
33 - #gpio-cells: Should be 2. The first cell is the GPIO number and the second
34 cell specifies GPIO flags, as defined in <dt-bindings/gpio/gpio.h>. Only the
35 GPIO_ACTIVE_HIGH and GPIO_ACTIVE_LOW flags are supported.
36
37Optional Properties:
38
39 - lines-initial-states: Bitmask that specifies the initial state of each
40 line. When a bit is set to zero, the corresponding line will be initialized to
41 the input (pulled-up) state. When the bit is set to one, the line will be
42 initialized the the low-level output state. If the property is not specified
43 all lines will be initialized to the input state.
44
45 The I/O expander can detect input state changes, and thus optionally act as
46 an interrupt controller. When the expander interrupt line is connected all the
47 following properties must be set. For more information please see the
48 interrupt controller device tree bindings documentation available at
49 Documentation/devicetree/bindings/interrupt-controller/interrupts.txt.
50
51 - interrupt-controller: Identifies the node as an interrupt controller.
52 - #interrupt-cells: Number of cells to encode an interrupt source, shall be 2.
53 - interrupt-parent: phandle of the parent interrupt controller.
54 - interrupts: Interrupt specifier for the controllers interrupt.
55
56
57Please refer to gpio.txt in this directory for details of the common GPIO
58bindings used by client devices.
59
60Example: PCF8575 I/O expander node
61
62 pcf8575: gpio@20 {
63 compatible = "nxp,pcf8575";
64 reg = <0x20>;
65 interrupt-parent = <&irqpin2>;
66 interrupts = <3 0>;
67 gpio-controller;
68 #gpio-cells = <2>;
69 interrupt-controller;
70 #interrupt-cells = <2>;
71 };
diff --git a/MAINTAINERS b/MAINTAINERS
index 197e2b61fcfe..051e4dc5b70f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4440,6 +4440,12 @@ F: Documentation/networking/ixgbevf.txt
4440F: Documentation/networking/i40e.txt 4440F: Documentation/networking/i40e.txt
4441F: drivers/net/ethernet/intel/ 4441F: drivers/net/ethernet/intel/
4442 4442
4443INTEL-MID GPIO DRIVER
4444M: David Cohen <david.a.cohen@linux.intel.com>
4445L: linux-gpio@vger.kernel.org
4446S: Maintained
4447F: drivers/gpio/gpio-intel-mid.c
4448
4443INTEL PRO/WIRELESS 2100, 2200BG, 2915ABG NETWORK CONNECTION SUPPORT 4449INTEL PRO/WIRELESS 2100, 2200BG, 2915ABG NETWORK CONNECTION SUPPORT
4444M: Stanislav Yakovlev <stas.yakovlev@gmail.com> 4450M: Stanislav Yakovlev <stas.yakovlev@gmail.com>
4445L: linux-wireless@vger.kernel.org 4451L: linux-wireless@vger.kernel.org
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index aa8300333bc5..acb80708accd 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -389,7 +389,6 @@ config ARCH_GEMINI
389 select CLKSRC_MMIO 389 select CLKSRC_MMIO
390 select CPU_FA526 390 select CPU_FA526
391 select GENERIC_CLOCKEVENTS 391 select GENERIC_CLOCKEVENTS
392 select NEED_MACH_GPIO_H
393 help 392 help
394 Support for the Cortina Systems Gemini family SoCs 393 Support for the Cortina Systems Gemini family SoCs
395 394
@@ -458,7 +457,7 @@ config ARCH_IOP32X
458 depends on MMU 457 depends on MMU
459 select ARCH_REQUIRE_GPIOLIB 458 select ARCH_REQUIRE_GPIOLIB
460 select CPU_XSCALE 459 select CPU_XSCALE
461 select NEED_MACH_GPIO_H 460 select GPIO_IOP
462 select NEED_RET_TO_USER 461 select NEED_RET_TO_USER
463 select PCI 462 select PCI
464 select PLAT_IOP 463 select PLAT_IOP
@@ -471,7 +470,7 @@ config ARCH_IOP33X
471 depends on MMU 470 depends on MMU
472 select ARCH_REQUIRE_GPIOLIB 471 select ARCH_REQUIRE_GPIOLIB
473 select CPU_XSCALE 472 select CPU_XSCALE
474 select NEED_MACH_GPIO_H 473 select GPIO_IOP
475 select NEED_RET_TO_USER 474 select NEED_RET_TO_USER
476 select PCI 475 select PCI
477 select PLAT_IOP 476 select PLAT_IOP
@@ -560,7 +559,6 @@ config ARCH_MMP
560 select GPIO_PXA 559 select GPIO_PXA
561 select IRQ_DOMAIN 560 select IRQ_DOMAIN
562 select MULTI_IRQ_HANDLER 561 select MULTI_IRQ_HANDLER
563 select NEED_MACH_GPIO_H
564 select PINCTRL 562 select PINCTRL
565 select PLAT_PXA 563 select PLAT_PXA
566 select SPARSE_IRQ 564 select SPARSE_IRQ
@@ -623,7 +621,6 @@ config ARCH_PXA
623 select GPIO_PXA 621 select GPIO_PXA
624 select HAVE_IDE 622 select HAVE_IDE
625 select MULTI_IRQ_HANDLER 623 select MULTI_IRQ_HANDLER
626 select NEED_MACH_GPIO_H
627 select PLAT_PXA 624 select PLAT_PXA
628 select SPARSE_IRQ 625 select SPARSE_IRQ
629 help 626 help
diff --git a/arch/arm/include/asm/hardware/iop3xx-gpio.h b/arch/arm/include/asm/hardware/iop3xx-gpio.h
deleted file mode 100644
index 9eda7dc92ad8..000000000000
--- a/arch/arm/include/asm/hardware/iop3xx-gpio.h
+++ /dev/null
@@ -1,75 +0,0 @@
1/*
2 * arch/arm/include/asm/hardware/iop3xx-gpio.h
3 *
4 * IOP3xx GPIO wrappers
5 *
6 * Copyright (c) 2008 Arnaud Patard <arnaud.patard@rtp-net.org>
7 * Based on IXP4XX gpio.h file
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25#ifndef __ASM_ARM_HARDWARE_IOP3XX_GPIO_H
26#define __ASM_ARM_HARDWARE_IOP3XX_GPIO_H
27
28#include <mach/hardware.h>
29#include <asm-generic/gpio.h>
30
31#define __ARM_GPIOLIB_COMPLEX
32
33#define IOP3XX_N_GPIOS 8
34
35static inline int gpio_get_value(unsigned gpio)
36{
37 if (gpio > IOP3XX_N_GPIOS)
38 return __gpio_get_value(gpio);
39
40 return gpio_line_get(gpio);
41}
42
43static inline void gpio_set_value(unsigned gpio, int value)
44{
45 if (gpio > IOP3XX_N_GPIOS) {
46 __gpio_set_value(gpio, value);
47 return;
48 }
49 gpio_line_set(gpio, value);
50}
51
52static inline int gpio_cansleep(unsigned gpio)
53{
54 if (gpio < IOP3XX_N_GPIOS)
55 return 0;
56 else
57 return __gpio_cansleep(gpio);
58}
59
60/*
61 * The GPIOs are not generating any interrupt
62 * Note : manuals are not clear about this
63 */
64static inline int gpio_to_irq(int gpio)
65{
66 return -EINVAL;
67}
68
69static inline int irq_to_gpio(int gpio)
70{
71 return -EINVAL;
72}
73
74#endif
75
diff --git a/arch/arm/include/asm/hardware/iop3xx.h b/arch/arm/include/asm/hardware/iop3xx.h
index 423744bf18eb..2594a95ff19a 100644
--- a/arch/arm/include/asm/hardware/iop3xx.h
+++ b/arch/arm/include/asm/hardware/iop3xx.h
@@ -18,16 +18,9 @@
18/* 18/*
19 * IOP3XX GPIO handling 19 * IOP3XX GPIO handling
20 */ 20 */
21#define GPIO_IN 0
22#define GPIO_OUT 1
23#define GPIO_LOW 0
24#define GPIO_HIGH 1
25#define IOP3XX_GPIO_LINE(x) (x) 21#define IOP3XX_GPIO_LINE(x) (x)
26 22
27#ifndef __ASSEMBLY__ 23#ifndef __ASSEMBLY__
28extern void gpio_line_config(int line, int direction);
29extern int gpio_line_get(int line);
30extern void gpio_line_set(int line, int value);
31extern int init_atu; 24extern int init_atu;
32extern int iop3xx_get_init_atu(void); 25extern int iop3xx_get_init_atu(void);
33#endif 26#endif
@@ -168,11 +161,6 @@ extern int iop3xx_get_init_atu(void);
168/* PERCR0 DOESN'T EXIST - index from 1! */ 161/* PERCR0 DOESN'T EXIST - index from 1! */
169#define IOP3XX_PERCR0 (volatile u32 *)IOP3XX_REG_ADDR(0x0710) 162#define IOP3XX_PERCR0 (volatile u32 *)IOP3XX_REG_ADDR(0x0710)
170 163
171/* General Purpose I/O */
172#define IOP3XX_GPOE (volatile u32 *)IOP3XX_GPIO_REG(0x0000)
173#define IOP3XX_GPID (volatile u32 *)IOP3XX_GPIO_REG(0x0004)
174#define IOP3XX_GPOD (volatile u32 *)IOP3XX_GPIO_REG(0x0008)
175
176/* Timers */ 164/* Timers */
177#define IOP3XX_TU_TMR0 (volatile u32 *)IOP3XX_TIMER_REG(0x0000) 165#define IOP3XX_TU_TMR0 (volatile u32 *)IOP3XX_TIMER_REG(0x0000)
178#define IOP3XX_TU_TMR1 (volatile u32 *)IOP3XX_TIMER_REG(0x0004) 166#define IOP3XX_TU_TMR1 (volatile u32 *)IOP3XX_TIMER_REG(0x0004)
diff --git a/arch/arm/mach-gemini/gpio.c b/arch/arm/mach-gemini/gpio.c
index 70bfa571b24b..f8cb5710d6ee 100644
--- a/arch/arm/mach-gemini/gpio.c
+++ b/arch/arm/mach-gemini/gpio.c
@@ -21,9 +21,9 @@
21 21
22#include <mach/hardware.h> 22#include <mach/hardware.h>
23#include <mach/irqs.h> 23#include <mach/irqs.h>
24#include <mach/gpio.h>
25 24
26#define GPIO_BASE(x) IO_ADDRESS(GEMINI_GPIO_BASE(x)) 25#define GPIO_BASE(x) IO_ADDRESS(GEMINI_GPIO_BASE(x))
26#define irq_to_gpio(x) ((x) - GPIO_IRQ_BASE)
27 27
28/* GPIO registers definition */ 28/* GPIO registers definition */
29#define GPIO_DATA_OUT 0x0 29#define GPIO_DATA_OUT 0x0
diff --git a/arch/arm/mach-gemini/include/mach/gpio.h b/arch/arm/mach-gemini/include/mach/gpio.h
deleted file mode 100644
index 40a0527bada7..000000000000
--- a/arch/arm/mach-gemini/include/mach/gpio.h
+++ /dev/null
@@ -1,20 +0,0 @@
1/*
2 * Gemini gpiolib specific defines
3 *
4 * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef __MACH_GPIO_H__
13#define __MACH_GPIO_H__
14
15#include <mach/irqs.h>
16
17#define gpio_to_irq(x) ((x) + GPIO_IRQ_BASE)
18#define irq_to_gpio(x) ((x) - GPIO_IRQ_BASE)
19
20#endif /* __MACH_GPIO_H__ */
diff --git a/arch/arm/mach-iop32x/em7210.c b/arch/arm/mach-iop32x/em7210.c
index 31fbb6c61b25..177cd073a83b 100644
--- a/arch/arm/mach-iop32x/em7210.c
+++ b/arch/arm/mach-iop32x/em7210.c
@@ -32,6 +32,7 @@
32#include <asm/mach/time.h> 32#include <asm/mach/time.h>
33#include <asm/mach-types.h> 33#include <asm/mach-types.h>
34#include <mach/time.h> 34#include <mach/time.h>
35#include "gpio-iop32x.h"
35 36
36static void __init em7210_timer_init(void) 37static void __init em7210_timer_init(void)
37{ 38{
@@ -183,6 +184,7 @@ void em7210_power_off(void)
183 184
184static void __init em7210_init_machine(void) 185static void __init em7210_init_machine(void)
185{ 186{
187 register_iop32x_gpio();
186 platform_device_register(&em7210_serial_device); 188 platform_device_register(&em7210_serial_device);
187 platform_device_register(&iop3xx_i2c0_device); 189 platform_device_register(&iop3xx_i2c0_device);
188 platform_device_register(&iop3xx_i2c1_device); 190 platform_device_register(&iop3xx_i2c1_device);
diff --git a/arch/arm/mach-iop32x/glantank.c b/arch/arm/mach-iop32x/glantank.c
index ac304705fe68..547b2342d61a 100644
--- a/arch/arm/mach-iop32x/glantank.c
+++ b/arch/arm/mach-iop32x/glantank.c
@@ -34,6 +34,7 @@
34#include <asm/mach-types.h> 34#include <asm/mach-types.h>
35#include <asm/page.h> 35#include <asm/page.h>
36#include <mach/time.h> 36#include <mach/time.h>
37#include "gpio-iop32x.h"
37 38
38/* 39/*
39 * GLAN Tank timer tick configuration. 40 * GLAN Tank timer tick configuration.
@@ -187,6 +188,7 @@ static void glantank_power_off(void)
187 188
188static void __init glantank_init_machine(void) 189static void __init glantank_init_machine(void)
189{ 190{
191 register_iop32x_gpio();
190 platform_device_register(&iop3xx_i2c0_device); 192 platform_device_register(&iop3xx_i2c0_device);
191 platform_device_register(&iop3xx_i2c1_device); 193 platform_device_register(&iop3xx_i2c1_device);
192 platform_device_register(&glantank_flash_device); 194 platform_device_register(&glantank_flash_device);
diff --git a/arch/arm/mach-iop32x/gpio-iop32x.h b/arch/arm/mach-iop32x/gpio-iop32x.h
new file mode 100644
index 000000000000..3c7309c02029
--- /dev/null
+++ b/arch/arm/mach-iop32x/gpio-iop32x.h
@@ -0,0 +1,10 @@
1static struct resource iop32x_gpio_res[] = {
2 DEFINE_RES_MEM((IOP3XX_PERIPHERAL_PHYS_BASE + 0x07c4), 0x10),
3};
4
5static inline void register_iop32x_gpio(void)
6{
7 platform_device_register_simple("gpio-iop", 0,
8 iop32x_gpio_res,
9 ARRAY_SIZE(iop32x_gpio_res));
10}
diff --git a/arch/arm/mach-iop32x/include/mach/gpio.h b/arch/arm/mach-iop32x/include/mach/gpio.h
deleted file mode 100644
index 708f4ec9db1d..000000000000
--- a/arch/arm/mach-iop32x/include/mach/gpio.h
+++ /dev/null
@@ -1,6 +0,0 @@
1#ifndef __ASM_ARCH_IOP32X_GPIO_H
2#define __ASM_ARCH_IOP32X_GPIO_H
3
4#include <asm/hardware/iop3xx-gpio.h>
5
6#endif
diff --git a/arch/arm/mach-iop32x/include/mach/iop32x.h b/arch/arm/mach-iop32x/include/mach/iop32x.h
index 941f363aca56..56ec864ec313 100644
--- a/arch/arm/mach-iop32x/include/mach/iop32x.h
+++ b/arch/arm/mach-iop32x/include/mach/iop32x.h
@@ -19,7 +19,6 @@
19 * Peripherals that are shared between the iop32x and iop33x but 19 * Peripherals that are shared between the iop32x and iop33x but
20 * located at different addresses. 20 * located at different addresses.
21 */ 21 */
22#define IOP3XX_GPIO_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x07c4 + (reg))
23#define IOP3XX_TIMER_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x07e0 + (reg)) 22#define IOP3XX_TIMER_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x07e0 + (reg))
24 23
25#include <asm/hardware/iop3xx.h> 24#include <asm/hardware/iop3xx.h>
diff --git a/arch/arm/mach-iop32x/iq31244.c b/arch/arm/mach-iop32x/iq31244.c
index f2cd2966212d..0e1392b20d18 100644
--- a/arch/arm/mach-iop32x/iq31244.c
+++ b/arch/arm/mach-iop32x/iq31244.c
@@ -37,6 +37,7 @@
37#include <asm/page.h> 37#include <asm/page.h>
38#include <asm/pgtable.h> 38#include <asm/pgtable.h>
39#include <mach/time.h> 39#include <mach/time.h>
40#include "gpio-iop32x.h"
40 41
41/* 42/*
42 * Until March of 2007 iq31244 platforms and ep80219 platforms shared the 43 * Until March of 2007 iq31244 platforms and ep80219 platforms shared the
@@ -283,6 +284,7 @@ void ep80219_power_off(void)
283 284
284static void __init iq31244_init_machine(void) 285static void __init iq31244_init_machine(void)
285{ 286{
287 register_iop32x_gpio();
286 platform_device_register(&iop3xx_i2c0_device); 288 platform_device_register(&iop3xx_i2c0_device);
287 platform_device_register(&iop3xx_i2c1_device); 289 platform_device_register(&iop3xx_i2c1_device);
288 platform_device_register(&iq31244_flash_device); 290 platform_device_register(&iq31244_flash_device);
diff --git a/arch/arm/mach-iop32x/iq80321.c b/arch/arm/mach-iop32x/iq80321.c
index 015435de90dd..66782ff1f46a 100644
--- a/arch/arm/mach-iop32x/iq80321.c
+++ b/arch/arm/mach-iop32x/iq80321.c
@@ -33,6 +33,7 @@
33#include <asm/page.h> 33#include <asm/page.h>
34#include <asm/pgtable.h> 34#include <asm/pgtable.h>
35#include <mach/time.h> 35#include <mach/time.h>
36#include "gpio-iop32x.h"
36 37
37/* 38/*
38 * IQ80321 timer tick configuration. 39 * IQ80321 timer tick configuration.
@@ -170,6 +171,7 @@ static struct platform_device iq80321_serial_device = {
170 171
171static void __init iq80321_init_machine(void) 172static void __init iq80321_init_machine(void)
172{ 173{
174 register_iop32x_gpio();
173 platform_device_register(&iop3xx_i2c0_device); 175 platform_device_register(&iop3xx_i2c0_device);
174 platform_device_register(&iop3xx_i2c1_device); 176 platform_device_register(&iop3xx_i2c1_device);
175 platform_device_register(&iq80321_flash_device); 177 platform_device_register(&iq80321_flash_device);
diff --git a/arch/arm/mach-iop32x/n2100.c b/arch/arm/mach-iop32x/n2100.c
index 069144300b77..c1cd80ecc219 100644
--- a/arch/arm/mach-iop32x/n2100.c
+++ b/arch/arm/mach-iop32x/n2100.c
@@ -30,6 +30,7 @@
30#include <linux/platform_device.h> 30#include <linux/platform_device.h>
31#include <linux/reboot.h> 31#include <linux/reboot.h>
32#include <linux/io.h> 32#include <linux/io.h>
33#include <linux/gpio.h>
33#include <mach/hardware.h> 34#include <mach/hardware.h>
34#include <asm/irq.h> 35#include <asm/irq.h>
35#include <asm/mach/arch.h> 36#include <asm/mach/arch.h>
@@ -40,6 +41,7 @@
40#include <asm/page.h> 41#include <asm/page.h>
41#include <asm/pgtable.h> 42#include <asm/pgtable.h>
42#include <mach/time.h> 43#include <mach/time.h>
44#include "gpio-iop32x.h"
43 45
44/* 46/*
45 * N2100 timer tick configuration. 47 * N2100 timer tick configuration.
@@ -288,8 +290,14 @@ static void n2100_power_off(void)
288 290
289static void n2100_restart(enum reboot_mode mode, const char *cmd) 291static void n2100_restart(enum reboot_mode mode, const char *cmd)
290{ 292{
291 gpio_line_set(N2100_HARDWARE_RESET, GPIO_LOW); 293 int ret;
292 gpio_line_config(N2100_HARDWARE_RESET, GPIO_OUT); 294
295 ret = gpio_direction_output(N2100_HARDWARE_RESET, 0);
296 if (ret) {
297 pr_crit("could not drive reset GPIO low\n");
298 return;
299 }
300 /* Wait for reset to happen */
293 while (1) 301 while (1)
294 ; 302 ;
295} 303}
@@ -299,7 +307,7 @@ static struct timer_list power_button_poll_timer;
299 307
300static void power_button_poll(unsigned long dummy) 308static void power_button_poll(unsigned long dummy)
301{ 309{
302 if (gpio_line_get(N2100_POWER_BUTTON) == 0) { 310 if (gpio_get_value(N2100_POWER_BUTTON) == 0) {
303 ctrl_alt_del(); 311 ctrl_alt_del();
304 return; 312 return;
305 } 313 }
@@ -308,9 +316,37 @@ static void power_button_poll(unsigned long dummy)
308 add_timer(&power_button_poll_timer); 316 add_timer(&power_button_poll_timer);
309} 317}
310 318
319static int __init n2100_request_gpios(void)
320{
321 int ret;
322
323 if (!machine_is_n2100())
324 return 0;
325
326 ret = gpio_request(N2100_HARDWARE_RESET, "reset");
327 if (ret)
328 pr_err("could not request reset GPIO\n");
329
330 ret = gpio_request(N2100_POWER_BUTTON, "power");
331 if (ret)
332 pr_err("could not request power GPIO\n");
333 else {
334 ret = gpio_direction_input(N2100_POWER_BUTTON);
335 if (ret)
336 pr_err("could not set power GPIO as input\n");
337 }
338 /* Set up power button poll timer */
339 init_timer(&power_button_poll_timer);
340 power_button_poll_timer.function = power_button_poll;
341 power_button_poll_timer.expires = jiffies + (HZ / 10);
342 add_timer(&power_button_poll_timer);
343 return 0;
344}
345device_initcall(n2100_request_gpios);
311 346
312static void __init n2100_init_machine(void) 347static void __init n2100_init_machine(void)
313{ 348{
349 register_iop32x_gpio();
314 platform_device_register(&iop3xx_i2c0_device); 350 platform_device_register(&iop3xx_i2c0_device);
315 platform_device_register(&n2100_flash_device); 351 platform_device_register(&n2100_flash_device);
316 platform_device_register(&n2100_serial_device); 352 platform_device_register(&n2100_serial_device);
@@ -321,11 +357,6 @@ static void __init n2100_init_machine(void)
321 ARRAY_SIZE(n2100_i2c_devices)); 357 ARRAY_SIZE(n2100_i2c_devices));
322 358
323 pm_power_off = n2100_power_off; 359 pm_power_off = n2100_power_off;
324
325 init_timer(&power_button_poll_timer);
326 power_button_poll_timer.function = power_button_poll;
327 power_button_poll_timer.expires = jiffies + (HZ / 10);
328 add_timer(&power_button_poll_timer);
329} 360}
330 361
331MACHINE_START(N2100, "Thecus N2100") 362MACHINE_START(N2100, "Thecus N2100")
diff --git a/arch/arm/mach-iop33x/include/mach/gpio.h b/arch/arm/mach-iop33x/include/mach/gpio.h
deleted file mode 100644
index ddd55bba9bb9..000000000000
--- a/arch/arm/mach-iop33x/include/mach/gpio.h
+++ /dev/null
@@ -1,6 +0,0 @@
1#ifndef __ASM_ARCH_IOP33X_GPIO_H
2#define __ASM_ARCH_IOP33X_GPIO_H
3
4#include <asm/hardware/iop3xx-gpio.h>
5
6#endif
diff --git a/arch/arm/mach-iop33x/include/mach/iop33x.h b/arch/arm/mach-iop33x/include/mach/iop33x.h
index a89c0a234bff..c95122653094 100644
--- a/arch/arm/mach-iop33x/include/mach/iop33x.h
+++ b/arch/arm/mach-iop33x/include/mach/iop33x.h
@@ -18,7 +18,6 @@
18 * Peripherals that are shared between the iop32x and iop33x but 18 * Peripherals that are shared between the iop32x and iop33x but
19 * located at different addresses. 19 * located at different addresses.
20 */ 20 */
21#define IOP3XX_GPIO_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x1780 + (reg))
22#define IOP3XX_TIMER_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x07d0 + (reg)) 21#define IOP3XX_TIMER_REG(reg) (IOP3XX_PERIPHERAL_VIRT_BASE + 0x07d0 + (reg))
23 22
24#include <asm/hardware/iop3xx.h> 23#include <asm/hardware/iop3xx.h>
diff --git a/arch/arm/mach-iop33x/iq80331.c b/arch/arm/mach-iop33x/iq80331.c
index c43304a10fa7..e2cb65cfbe23 100644
--- a/arch/arm/mach-iop33x/iq80331.c
+++ b/arch/arm/mach-iop33x/iq80331.c
@@ -122,8 +122,15 @@ static struct platform_device iq80331_flash_device = {
122 .resource = &iq80331_flash_resource, 122 .resource = &iq80331_flash_resource,
123}; 123};
124 124
125static struct resource iq80331_gpio_res[] = {
126 DEFINE_RES_MEM((IOP3XX_PERIPHERAL_PHYS_BASE + 0x1780), 0x10),
127};
128
125static void __init iq80331_init_machine(void) 129static void __init iq80331_init_machine(void)
126{ 130{
131 platform_device_register_simple("gpio-iop", 0,
132 iq80331_gpio_res,
133 ARRAY_SIZE(iq80331_gpio_res));
127 platform_device_register(&iop3xx_i2c0_device); 134 platform_device_register(&iop3xx_i2c0_device);
128 platform_device_register(&iop3xx_i2c1_device); 135 platform_device_register(&iop3xx_i2c1_device);
129 platform_device_register(&iop33x_uart0_device); 136 platform_device_register(&iop33x_uart0_device);
diff --git a/arch/arm/mach-iop33x/iq80332.c b/arch/arm/mach-iop33x/iq80332.c
index 8192987e78e5..0b6269d94f89 100644
--- a/arch/arm/mach-iop33x/iq80332.c
+++ b/arch/arm/mach-iop33x/iq80332.c
@@ -122,8 +122,15 @@ static struct platform_device iq80332_flash_device = {
122 .resource = &iq80332_flash_resource, 122 .resource = &iq80332_flash_resource,
123}; 123};
124 124
125static struct resource iq80332_gpio_res[] = {
126 DEFINE_RES_MEM((IOP3XX_PERIPHERAL_PHYS_BASE + 0x1780), 0x10),
127};
128
125static void __init iq80332_init_machine(void) 129static void __init iq80332_init_machine(void)
126{ 130{
131 platform_device_register_simple("gpio-iop", 0,
132 iq80332_gpio_res,
133 ARRAY_SIZE(iq80332_gpio_res));
127 platform_device_register(&iop3xx_i2c0_device); 134 platform_device_register(&iop3xx_i2c0_device);
128 platform_device_register(&iop3xx_i2c1_device); 135 platform_device_register(&iop3xx_i2c1_device);
129 platform_device_register(&iop33x_uart0_device); 136 platform_device_register(&iop33x_uart0_device);
diff --git a/arch/arm/mach-ixp4xx/common.c b/arch/arm/mach-ixp4xx/common.c
index 5327decde5a0..9edaf4734fa8 100644
--- a/arch/arm/mach-ixp4xx/common.c
+++ b/arch/arm/mach-ixp4xx/common.c
@@ -81,6 +81,44 @@ void __init ixp4xx_map_io(void)
81 iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc)); 81 iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
82} 82}
83 83
84/*
85 * GPIO-functions
86 */
87/*
88 * The following converted to the real HW bits the gpio_line_config
89 */
90/* GPIO pin types */
91#define IXP4XX_GPIO_OUT 0x1
92#define IXP4XX_GPIO_IN 0x2
93
94/* GPIO signal types */
95#define IXP4XX_GPIO_LOW 0
96#define IXP4XX_GPIO_HIGH 1
97
98/* GPIO Clocks */
99#define IXP4XX_GPIO_CLK_0 14
100#define IXP4XX_GPIO_CLK_1 15
101
102static void gpio_line_config(u8 line, u32 direction)
103{
104 if (direction == IXP4XX_GPIO_IN)
105 *IXP4XX_GPIO_GPOER |= (1 << line);
106 else
107 *IXP4XX_GPIO_GPOER &= ~(1 << line);
108}
109
110static void gpio_line_get(u8 line, int *value)
111{
112 *value = (*IXP4XX_GPIO_GPINR >> line) & 0x1;
113}
114
115static void gpio_line_set(u8 line, int value)
116{
117 if (value == IXP4XX_GPIO_HIGH)
118 *IXP4XX_GPIO_GPOUTR |= (1 << line);
119 else if (value == IXP4XX_GPIO_LOW)
120 *IXP4XX_GPIO_GPOUTR &= ~(1 << line);
121}
84 122
85/************************************************************************* 123/*************************************************************************
86 * IXP4xx chipset IRQ handling 124 * IXP4xx chipset IRQ handling
@@ -117,17 +155,6 @@ static int ixp4xx_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
117 return -EINVAL; 155 return -EINVAL;
118} 156}
119 157
120int irq_to_gpio(unsigned int irq)
121{
122 int gpio = (irq < 32) ? irq2gpio[irq] : -EINVAL;
123
124 if (gpio == -1)
125 return -EINVAL;
126
127 return gpio;
128}
129EXPORT_SYMBOL(irq_to_gpio);
130
131static int ixp4xx_set_irq_type(struct irq_data *d, unsigned int type) 158static int ixp4xx_set_irq_type(struct irq_data *d, unsigned int type)
132{ 159{
133 int line = irq2gpio[d->irq]; 160 int line = irq2gpio[d->irq];
diff --git a/arch/arm/mach-ixp4xx/dsmg600-setup.c b/arch/arm/mach-ixp4xx/dsmg600-setup.c
index 63de1b3fd06b..736dc692d540 100644
--- a/arch/arm/mach-ixp4xx/dsmg600-setup.c
+++ b/arch/arm/mach-ixp4xx/dsmg600-setup.c
@@ -26,6 +26,7 @@
26#include <linux/reboot.h> 26#include <linux/reboot.h>
27#include <linux/i2c.h> 27#include <linux/i2c.h>
28#include <linux/i2c-gpio.h> 28#include <linux/i2c-gpio.h>
29#include <linux/gpio.h>
29 30
30#include <mach/hardware.h> 31#include <mach/hardware.h>
31 32
@@ -161,11 +162,8 @@ static struct platform_device *dsmg600_devices[] __initdata = {
161 162
162static void dsmg600_power_off(void) 163static void dsmg600_power_off(void)
163{ 164{
164 /* enable the pwr cntl gpio */ 165 /* enable the pwr cntl and drive it high */
165 gpio_line_config(DSMG600_PO_GPIO, IXP4XX_GPIO_OUT); 166 gpio_direction_output(DSMG600_PO_GPIO, 1);
166
167 /* poweroff */
168 gpio_line_set(DSMG600_PO_GPIO, IXP4XX_GPIO_HIGH);
169} 167}
170 168
171/* This is used to make sure the power-button pusher is serious. The button 169/* This is used to make sure the power-button pusher is serious. The button
@@ -202,7 +200,7 @@ static void dsmg600_power_handler(unsigned long data)
202 ctrl_alt_del(); 200 ctrl_alt_del();
203 201
204 /* Change the state of the power LED to "blink" */ 202 /* Change the state of the power LED to "blink" */
205 gpio_line_set(DSMG600_LED_PWR_GPIO, IXP4XX_GPIO_LOW); 203 gpio_set_value(DSMG600_LED_PWR_GPIO, 0);
206 } else { 204 } else {
207 power_button_countdown = PBUTTON_HOLDDOWN_COUNT; 205 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
208 } 206 }
@@ -228,6 +226,40 @@ static void __init dsmg600_timer_init(void)
228 ixp4xx_timer_init(); 226 ixp4xx_timer_init();
229} 227}
230 228
229static int __init dsmg600_gpio_init(void)
230{
231 if (!machine_is_dsmg600())
232 return 0;
233
234 gpio_request(DSMG600_RB_GPIO, "reset button");
235 if (request_irq(gpio_to_irq(DSMG600_RB_GPIO), &dsmg600_reset_handler,
236 IRQF_DISABLED | IRQF_TRIGGER_LOW,
237 "DSM-G600 reset button", NULL) < 0) {
238
239 printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
240 gpio_to_irq(DSMG600_RB_GPIO));
241 }
242
243 /*
244 * The power button on the D-Link DSM-G600 is on GPIO 15, but
245 * it cannot handle interrupts on that GPIO line. So we'll
246 * have to poll it with a kernel timer.
247 */
248
249 /* Make sure that the power button GPIO is set up as an input */
250 gpio_request(DSMG600_PB_GPIO, "power button");
251 gpio_direction_input(DSMG600_PB_GPIO);
252 /* Request poweroff GPIO line */
253 gpio_request(DSMG600_PO_GPIO, "power off button");
254
255 /* Set the initial value for the power button IRQ handler */
256 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
257
258 mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
259 return 0;
260}
261device_initcall(dsmg600_gpio_init);
262
231static void __init dsmg600_init(void) 263static void __init dsmg600_init(void)
232{ 264{
233 ixp4xx_sys_init(); 265 ixp4xx_sys_init();
@@ -251,27 +283,6 @@ static void __init dsmg600_init(void)
251 platform_add_devices(dsmg600_devices, ARRAY_SIZE(dsmg600_devices)); 283 platform_add_devices(dsmg600_devices, ARRAY_SIZE(dsmg600_devices));
252 284
253 pm_power_off = dsmg600_power_off; 285 pm_power_off = dsmg600_power_off;
254
255 if (request_irq(gpio_to_irq(DSMG600_RB_GPIO), &dsmg600_reset_handler,
256 IRQF_DISABLED | IRQF_TRIGGER_LOW,
257 "DSM-G600 reset button", NULL) < 0) {
258
259 printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
260 gpio_to_irq(DSMG600_RB_GPIO));
261 }
262
263 /* The power button on the D-Link DSM-G600 is on GPIO 15, but
264 * it cannot handle interrupts on that GPIO line. So we'll
265 * have to poll it with a kernel timer.
266 */
267
268 /* Make sure that the power button GPIO is set up as an input */
269 gpio_line_config(DSMG600_PB_GPIO, IXP4XX_GPIO_IN);
270
271 /* Set the initial value for the power button IRQ handler */
272 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
273
274 mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
275} 286}
276 287
277MACHINE_START(DSMG600, "D-Link DSM-G600 RevA") 288MACHINE_START(DSMG600, "D-Link DSM-G600 RevA")
diff --git a/arch/arm/mach-ixp4xx/include/mach/platform.h b/arch/arm/mach-ixp4xx/include/mach/platform.h
index 4c4c6a6f4526..75c4c6572ad0 100644
--- a/arch/arm/mach-ixp4xx/include/mach/platform.h
+++ b/arch/arm/mach-ixp4xx/include/mach/platform.h
@@ -131,44 +131,5 @@ struct pci_sys_data;
131extern int ixp4xx_setup(int nr, struct pci_sys_data *sys); 131extern int ixp4xx_setup(int nr, struct pci_sys_data *sys);
132extern struct pci_ops ixp4xx_ops; 132extern struct pci_ops ixp4xx_ops;
133 133
134/*
135 * GPIO-functions
136 */
137/*
138 * The following converted to the real HW bits the gpio_line_config
139 */
140/* GPIO pin types */
141#define IXP4XX_GPIO_OUT 0x1
142#define IXP4XX_GPIO_IN 0x2
143
144/* GPIO signal types */
145#define IXP4XX_GPIO_LOW 0
146#define IXP4XX_GPIO_HIGH 1
147
148/* GPIO Clocks */
149#define IXP4XX_GPIO_CLK_0 14
150#define IXP4XX_GPIO_CLK_1 15
151
152static inline void gpio_line_config(u8 line, u32 direction)
153{
154 if (direction == IXP4XX_GPIO_IN)
155 *IXP4XX_GPIO_GPOER |= (1 << line);
156 else
157 *IXP4XX_GPIO_GPOER &= ~(1 << line);
158}
159
160static inline void gpio_line_get(u8 line, int *value)
161{
162 *value = (*IXP4XX_GPIO_GPINR >> line) & 0x1;
163}
164
165static inline void gpio_line_set(u8 line, int value)
166{
167 if (value == IXP4XX_GPIO_HIGH)
168 *IXP4XX_GPIO_GPOUTR |= (1 << line);
169 else if (value == IXP4XX_GPIO_LOW)
170 *IXP4XX_GPIO_GPOUTR &= ~(1 << line);
171}
172
173#endif // __ASSEMBLY__ 134#endif // __ASSEMBLY__
174 135
diff --git a/arch/arm/mach-ixp4xx/ixdp425-setup.c b/arch/arm/mach-ixp4xx/ixdp425-setup.c
index 22d688b7d513..e7b8befa8729 100644
--- a/arch/arm/mach-ixp4xx/ixdp425-setup.c
+++ b/arch/arm/mach-ixp4xx/ixdp425-setup.c
@@ -20,6 +20,7 @@
20#include <linux/mtd/nand.h> 20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h> 21#include <linux/mtd/partitions.h>
22#include <linux/delay.h> 22#include <linux/delay.h>
23#include <linux/gpio.h>
23#include <asm/types.h> 24#include <asm/types.h>
24#include <asm/setup.h> 25#include <asm/setup.h>
25#include <asm/memory.h> 26#include <asm/memory.h>
@@ -80,10 +81,10 @@ ixdp425_flash_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
80 81
81 if (ctrl & NAND_CTRL_CHANGE) { 82 if (ctrl & NAND_CTRL_CHANGE) {
82 if (ctrl & NAND_NCE) { 83 if (ctrl & NAND_NCE) {
83 gpio_line_set(IXDP425_NAND_NCE_PIN, IXP4XX_GPIO_LOW); 84 gpio_set_value(IXDP425_NAND_NCE_PIN, 0);
84 udelay(5); 85 udelay(5);
85 } else 86 } else
86 gpio_line_set(IXDP425_NAND_NCE_PIN, IXP4XX_GPIO_HIGH); 87 gpio_set_value(IXDP425_NAND_NCE_PIN, 1);
87 88
88 offset = (ctrl & NAND_CLE) ? IXDP425_NAND_CMD_BYTE : 0; 89 offset = (ctrl & NAND_CLE) ? IXDP425_NAND_CMD_BYTE : 0;
89 offset |= (ctrl & NAND_ALE) ? IXDP425_NAND_ADDR_BYTE : 0; 90 offset |= (ctrl & NAND_ALE) ? IXDP425_NAND_ADDR_BYTE : 0;
@@ -227,7 +228,8 @@ static void __init ixdp425_init(void)
227 ixdp425_flash_nand_resource.start = IXP4XX_EXP_BUS_BASE(3), 228 ixdp425_flash_nand_resource.start = IXP4XX_EXP_BUS_BASE(3),
228 ixdp425_flash_nand_resource.end = IXP4XX_EXP_BUS_BASE(3) + 0x10 - 1; 229 ixdp425_flash_nand_resource.end = IXP4XX_EXP_BUS_BASE(3) + 0x10 - 1;
229 230
230 gpio_line_config(IXDP425_NAND_NCE_PIN, IXP4XX_GPIO_OUT); 231 gpio_request(IXDP425_NAND_NCE_PIN, "NAND NCE pin");
232 gpio_direction_output(IXDP425_NAND_NCE_PIN, 0);
231 233
232 /* Configure expansion bus for NAND Flash */ 234 /* Configure expansion bus for NAND Flash */
233 *IXP4XX_EXP_CS3 = IXP4XX_EXP_BUS_CS_EN | 235 *IXP4XX_EXP_CS3 = IXP4XX_EXP_BUS_CS_EN |
diff --git a/arch/arm/mach-ixp4xx/nas100d-setup.c b/arch/arm/mach-ixp4xx/nas100d-setup.c
index ed667ce9f576..507cb5233537 100644
--- a/arch/arm/mach-ixp4xx/nas100d-setup.c
+++ b/arch/arm/mach-ixp4xx/nas100d-setup.c
@@ -184,11 +184,8 @@ static void nas100d_power_off(void)
184{ 184{
185 /* This causes the box to drop the power and go dead. */ 185 /* This causes the box to drop the power and go dead. */
186 186
187 /* enable the pwr cntl gpio */ 187 /* enable the pwr cntl gpio and assert power off */
188 gpio_line_config(NAS100D_PO_GPIO, IXP4XX_GPIO_OUT); 188 gpio_direction_output(NAS100D_PO_GPIO, 1);
189
190 /* do the deed */
191 gpio_line_set(NAS100D_PO_GPIO, IXP4XX_GPIO_HIGH);
192} 189}
193 190
194/* This is used to make sure the power-button pusher is serious. The button 191/* This is used to make sure the power-button pusher is serious. The button
@@ -225,7 +222,7 @@ static void nas100d_power_handler(unsigned long data)
225 ctrl_alt_del(); 222 ctrl_alt_del();
226 223
227 /* Change the state of the power LED to "blink" */ 224 /* Change the state of the power LED to "blink" */
228 gpio_line_set(NAS100D_LED_PWR_GPIO, IXP4XX_GPIO_LOW); 225 gpio_set_value(NAS100D_LED_PWR_GPIO, 0);
229 } else { 226 } else {
230 power_button_countdown = PBUTTON_HOLDDOWN_COUNT; 227 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
231 } 228 }
@@ -242,6 +239,33 @@ static irqreturn_t nas100d_reset_handler(int irq, void *dev_id)
242 return IRQ_HANDLED; 239 return IRQ_HANDLED;
243} 240}
244 241
242static int __init nas100d_gpio_init(void)
243{
244 if (!machine_is_nas100d())
245 return 0;
246
247 /*
248 * The power button on the Iomega NAS100d is on GPIO 14, but
249 * it cannot handle interrupts on that GPIO line. So we'll
250 * have to poll it with a kernel timer.
251 */
252
253 /* Request the power off GPIO */
254 gpio_request(NAS100D_PO_GPIO, "power off");
255
256 /* Make sure that the power button GPIO is set up as an input */
257 gpio_request(NAS100D_PB_GPIO, "power button");
258 gpio_direction_input(NAS100D_PB_GPIO);
259
260 /* Set the initial value for the power button IRQ handler */
261 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
262
263 mod_timer(&nas100d_power_timer, jiffies + msecs_to_jiffies(500));
264
265 return 0;
266}
267device_initcall(nas100d_gpio_init);
268
245static void __init nas100d_init(void) 269static void __init nas100d_init(void)
246{ 270{
247 uint8_t __iomem *f; 271 uint8_t __iomem *f;
@@ -278,19 +302,6 @@ static void __init nas100d_init(void)
278 gpio_to_irq(NAS100D_RB_GPIO)); 302 gpio_to_irq(NAS100D_RB_GPIO));
279 } 303 }
280 304
281 /* The power button on the Iomega NAS100d is on GPIO 14, but
282 * it cannot handle interrupts on that GPIO line. So we'll
283 * have to poll it with a kernel timer.
284 */
285
286 /* Make sure that the power button GPIO is set up as an input */
287 gpio_line_config(NAS100D_PB_GPIO, IXP4XX_GPIO_IN);
288
289 /* Set the initial value for the power button IRQ handler */
290 power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
291
292 mod_timer(&nas100d_power_timer, jiffies + msecs_to_jiffies(500));
293
294 /* 305 /*
295 * Map in a portion of the flash and read the MAC address. 306 * Map in a portion of the flash and read the MAC address.
296 * Since it is stored in BE in the flash itself, we need to 307 * Since it is stored in BE in the flash itself, we need to
diff --git a/arch/arm/mach-ixp4xx/nslu2-setup.c b/arch/arm/mach-ixp4xx/nslu2-setup.c
index 7e55236c26ea..ba5f1cda2a9d 100644
--- a/arch/arm/mach-ixp4xx/nslu2-setup.c
+++ b/arch/arm/mach-ixp4xx/nslu2-setup.c
@@ -197,11 +197,8 @@ static void nslu2_power_off(void)
197{ 197{
198 /* This causes the box to drop the power and go dead. */ 198 /* This causes the box to drop the power and go dead. */
199 199
200 /* enable the pwr cntl gpio */ 200 /* enable the pwr cntl gpio and assert power off */
201 gpio_line_config(NSLU2_PO_GPIO, IXP4XX_GPIO_OUT); 201 gpio_direction_output(NSLU2_PO_GPIO, 1);
202
203 /* do the deed */
204 gpio_line_set(NSLU2_PO_GPIO, IXP4XX_GPIO_HIGH);
205} 202}
206 203
207static irqreturn_t nslu2_power_handler(int irq, void *dev_id) 204static irqreturn_t nslu2_power_handler(int irq, void *dev_id)
@@ -223,6 +220,16 @@ static irqreturn_t nslu2_reset_handler(int irq, void *dev_id)
223 return IRQ_HANDLED; 220 return IRQ_HANDLED;
224} 221}
225 222
223static int __init nslu2_gpio_init(void)
224{
225 if (!machine_is_nslu2())
226 return 0;
227
228 /* Request the power off GPIO */
229 return gpio_request(NSLU2_PO_GPIO, "power off");
230}
231device_initcall(nslu2_gpio_init);
232
226static void __init nslu2_timer_init(void) 233static void __init nslu2_timer_init(void)
227{ 234{
228 /* The xtal on this machine is non-standard. */ 235 /* The xtal on this machine is non-standard. */
diff --git a/arch/arm/mach-mmp/include/mach/gpio.h b/arch/arm/mach-mmp/include/mach/gpio.h
deleted file mode 100644
index 13219ebf5128..000000000000
--- a/arch/arm/mach-mmp/include/mach/gpio.h
+++ /dev/null
@@ -1,8 +0,0 @@
1#ifndef __ASM_MACH_GPIO_H
2#define __ASM_MACH_GPIO_H
3
4#include <asm-generic/gpio.h>
5
6#include <mach/cputype.h>
7
8#endif /* __ASM_MACH_GPIO_H */
diff --git a/arch/arm/mach-pxa/include/mach/gpio.h b/arch/arm/mach-pxa/include/mach/gpio.h
deleted file mode 100644
index 0248e433bc98..000000000000
--- a/arch/arm/mach-pxa/include/mach/gpio.h
+++ /dev/null
@@ -1,32 +0,0 @@
1/*
2 * arch/arm/mach-pxa/include/mach/gpio.h
3 *
4 * PXA GPIO wrappers for arch-neutral GPIO calls
5 *
6 * Written by Philipp Zabel <philipp.zabel@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#ifndef __ASM_ARCH_PXA_GPIO_H
25#define __ASM_ARCH_PXA_GPIO_H
26
27#include <asm-generic/gpio.h>
28
29#include <mach/irqs.h>
30#include <mach/hardware.h>
31
32#endif
diff --git a/arch/arm/mach-w90x900/include/mach/gpio.h b/arch/arm/mach-w90x900/include/mach/gpio.h
deleted file mode 100644
index 5385a4203277..000000000000
--- a/arch/arm/mach-w90x900/include/mach/gpio.h
+++ /dev/null
@@ -1,30 +0,0 @@
1/*
2 * linux/arch/arm/mach-w90p910/include/mach/gpio.h
3 *
4 * Generic w90p910 GPIO handling
5 *
6 * Wan ZongShun <mcuos.com@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef __ASM_ARCH_W90P910_GPIO_H
14#define __ASM_ARCH_W90P910_GPIO_H
15
16#include <mach/hardware.h>
17#include <asm/irq.h>
18
19static inline int gpio_to_irq(unsigned gpio)
20{
21 return gpio;
22}
23#define gpio_to_irq gpio_to_irq
24
25static inline int irq_to_gpio(unsigned irq)
26{
27 return irq;
28}
29
30#endif
diff --git a/arch/arm/plat-iop/Makefile b/arch/arm/plat-iop/Makefile
index a99dc15a70f7..224e56c6049b 100644
--- a/arch/arm/plat-iop/Makefile
+++ b/arch/arm/plat-iop/Makefile
@@ -5,7 +5,6 @@
5obj-y := 5obj-y :=
6 6
7# IOP32X 7# IOP32X
8obj-$(CONFIG_ARCH_IOP32X) += gpio.o
9obj-$(CONFIG_ARCH_IOP32X) += i2c.o 8obj-$(CONFIG_ARCH_IOP32X) += i2c.o
10obj-$(CONFIG_ARCH_IOP32X) += pci.o 9obj-$(CONFIG_ARCH_IOP32X) += pci.o
11obj-$(CONFIG_ARCH_IOP32X) += setup.o 10obj-$(CONFIG_ARCH_IOP32X) += setup.o
@@ -16,7 +15,6 @@ obj-$(CONFIG_ARCH_IOP32X) += pmu.o
16obj-$(CONFIG_ARCH_IOP32X) += restart.o 15obj-$(CONFIG_ARCH_IOP32X) += restart.o
17 16
18# IOP33X 17# IOP33X
19obj-$(CONFIG_ARCH_IOP33X) += gpio.o
20obj-$(CONFIG_ARCH_IOP33X) += i2c.o 18obj-$(CONFIG_ARCH_IOP33X) += i2c.o
21obj-$(CONFIG_ARCH_IOP33X) += pci.o 19obj-$(CONFIG_ARCH_IOP33X) += pci.o
22obj-$(CONFIG_ARCH_IOP33X) += setup.o 20obj-$(CONFIG_ARCH_IOP33X) += setup.o
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 661ca82814e5..0f0444475bf0 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -30,10 +30,6 @@ config ARCH_REQUIRE_GPIOLIB
30 Selecting this from the architecture code will cause the gpiolib 30 Selecting this from the architecture code will cause the gpiolib
31 code to always get built in. 31 code to always get built in.
32 32
33config GPIO_DEVRES
34 def_bool y
35 depends on HAS_IOMEM
36
37 33
38menuconfig GPIOLIB 34menuconfig GPIOLIB
39 bool "GPIO Support" 35 bool "GPIO Support"
@@ -47,6 +43,10 @@ menuconfig GPIOLIB
47 43
48if GPIOLIB 44if GPIOLIB
49 45
46config GPIO_DEVRES
47 def_bool y
48 depends on HAS_IOMEM
49
50config OF_GPIO 50config OF_GPIO
51 def_bool y 51 def_bool y
52 depends on OF 52 depends on OF
@@ -129,7 +129,7 @@ config GPIO_IT8761E
129 129
130config GPIO_EM 130config GPIO_EM
131 tristate "Emma Mobile GPIO" 131 tristate "Emma Mobile GPIO"
132 depends on ARM 132 depends on ARM && OF_GPIO
133 help 133 help
134 Say yes here to support GPIO on Renesas Emma Mobile SoCs. 134 Say yes here to support GPIO on Renesas Emma Mobile SoCs.
135 135
@@ -213,7 +213,7 @@ config GPIO_OCTEON
213 213
214config GPIO_PL061 214config GPIO_PL061
215 bool "PrimeCell PL061 GPIO support" 215 bool "PrimeCell PL061 GPIO support"
216 depends on ARM && ARM_AMBA 216 depends on ARM_AMBA
217 select GENERIC_IRQ_CHIP 217 select GENERIC_IRQ_CHIP
218 help 218 help
219 Say yes here to support the PrimeCell PL061 GPIO device 219 Say yes here to support the PrimeCell PL061 GPIO device
@@ -320,6 +320,15 @@ config GPIO_ICH
320 320
321 If unsure, say N. 321 If unsure, say N.
322 322
323config GPIO_IOP
324 tristate "Intel IOP GPIO"
325 depends on ARM && (ARCH_IOP32X || ARCH_IOP33X)
326 help
327 Say yes here to support the GPIO functionality of a number of Intel
328 IOP32X or IOP33X.
329
330 If unsure, say N.
331
323config GPIO_VX855 332config GPIO_VX855
324 tristate "VIA VX855/VX875 GPIO" 333 tristate "VIA VX855/VX875 GPIO"
325 depends on PCI 334 depends on PCI
@@ -616,12 +625,12 @@ config GPIO_AMD8111
616 625
617 If unsure, say N 626 If unsure, say N
618 627
619config GPIO_LANGWELL 628config GPIO_INTEL_MID
620 bool "Intel Langwell/Penwell GPIO support" 629 bool "Intel Mid GPIO support"
621 depends on PCI && X86 630 depends on PCI && X86
622 select IRQ_DOMAIN 631 select IRQ_DOMAIN
623 help 632 help
624 Say Y here to support Intel Langwell/Penwell GPIO. 633 Say Y here to support Intel Mid GPIO.
625 634
626config GPIO_PCH 635config GPIO_PCH
627 tristate "Intel EG20T PCH/LAPIS Semiconductor IOH(ML7223/ML7831) GPIO" 636 tristate "Intel EG20T PCH/LAPIS Semiconductor IOH(ML7223/ML7831) GPIO"
@@ -707,7 +716,7 @@ config GPIO_74X164
707comment "AC97 GPIO expanders:" 716comment "AC97 GPIO expanders:"
708 717
709config GPIO_UCB1400 718config GPIO_UCB1400
710 bool "Philips UCB1400 GPIO" 719 tristate "Philips UCB1400 GPIO"
711 depends on UCB1400_CORE 720 depends on UCB1400_CORE
712 help 721 help
713 This enables support for the Philips UCB1400 GPIO pins. 722 This enables support for the Philips UCB1400 GPIO pins.
@@ -763,6 +772,12 @@ config GPIO_MSIC
763 Enable support for GPIO on intel MSIC controllers found in 772 Enable support for GPIO on intel MSIC controllers found in
764 intel MID devices 773 intel MID devices
765 774
775config GPIO_BCM_KONA
776 bool "Broadcom Kona GPIO"
777 depends on OF_GPIO
778 help
779 Turn on GPIO support for Broadcom "Kona" chips.
780
766comment "USB GPIO expanders:" 781comment "USB GPIO expanders:"
767 782
768config GPIO_VIPERBOARD 783config GPIO_VIPERBOARD
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 2931c996e71f..7971e36b8b12 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_GPIO_ADP5520) += gpio-adp5520.o
16obj-$(CONFIG_GPIO_ADP5588) += gpio-adp5588.o 16obj-$(CONFIG_GPIO_ADP5588) += gpio-adp5588.o
17obj-$(CONFIG_GPIO_AMD8111) += gpio-amd8111.o 17obj-$(CONFIG_GPIO_AMD8111) += gpio-amd8111.o
18obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o 18obj-$(CONFIG_GPIO_ARIZONA) += gpio-arizona.o
19obj-$(CONFIG_GPIO_BCM_KONA) += gpio-bcm-kona.o
19obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o 20obj-$(CONFIG_GPIO_BT8XX) += gpio-bt8xx.o
20obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o 21obj-$(CONFIG_GPIO_CLPS711X) += gpio-clps711x.o
21obj-$(CONFIG_GPIO_CS5535) += gpio-cs5535.o 22obj-$(CONFIG_GPIO_CS5535) += gpio-cs5535.o
@@ -28,11 +29,12 @@ obj-$(CONFIG_GPIO_F7188X) += gpio-f7188x.o
28obj-$(CONFIG_GPIO_GE_FPGA) += gpio-ge.o 29obj-$(CONFIG_GPIO_GE_FPGA) += gpio-ge.o
29obj-$(CONFIG_GPIO_GRGPIO) += gpio-grgpio.o 30obj-$(CONFIG_GPIO_GRGPIO) += gpio-grgpio.o
30obj-$(CONFIG_GPIO_ICH) += gpio-ich.o 31obj-$(CONFIG_GPIO_ICH) += gpio-ich.o
32obj-$(CONFIG_GPIO_IOP) += gpio-iop.o
31obj-$(CONFIG_GPIO_IT8761E) += gpio-it8761e.o 33obj-$(CONFIG_GPIO_IT8761E) += gpio-it8761e.o
32obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o 34obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o
33obj-$(CONFIG_GPIO_KEMPLD) += gpio-kempld.o 35obj-$(CONFIG_GPIO_KEMPLD) += gpio-kempld.o
34obj-$(CONFIG_ARCH_KS8695) += gpio-ks8695.o 36obj-$(CONFIG_ARCH_KS8695) += gpio-ks8695.o
35obj-$(CONFIG_GPIO_LANGWELL) += gpio-langwell.o 37obj-$(CONFIG_GPIO_INTEL_MID) += gpio-intel-mid.o
36obj-$(CONFIG_ARCH_LPC32XX) += gpio-lpc32xx.o 38obj-$(CONFIG_ARCH_LPC32XX) += gpio-lpc32xx.o
37obj-$(CONFIG_GPIO_LYNXPOINT) += gpio-lynxpoint.o 39obj-$(CONFIG_GPIO_LYNXPOINT) += gpio-lynxpoint.o
38obj-$(CONFIG_GPIO_MAX730X) += gpio-max730x.o 40obj-$(CONFIG_GPIO_MAX730X) += gpio-max730x.o
diff --git a/drivers/gpio/devres.c b/drivers/gpio/devres.c
index 3e7812f0405e..307464fd015f 100644
--- a/drivers/gpio/devres.c
+++ b/drivers/gpio/devres.c
@@ -15,10 +15,95 @@
15 */ 15 */
16 16
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/err.h>
18#include <linux/gpio.h> 19#include <linux/gpio.h>
20#include <linux/gpio/consumer.h>
19#include <linux/device.h> 21#include <linux/device.h>
20#include <linux/gfp.h> 22#include <linux/gfp.h>
21 23
24static void devm_gpiod_release(struct device *dev, void *res)
25{
26 struct gpio_desc **desc = res;
27
28 gpiod_put(*desc);
29}
30
31static int devm_gpiod_match(struct device *dev, void *res, void *data)
32{
33 struct gpio_desc **this = res, **gpio = data;
34
35 return *this == *gpio;
36}
37
38/**
39 * devm_gpiod_get - Resource-managed gpiod_get()
40 * @dev: GPIO consumer
41 * @con_id: function within the GPIO consumer
42 *
43 * Managed gpiod_get(). GPIO descriptors returned from this function are
44 * automatically disposed on driver detach. See gpiod_get() for detailed
45 * information about behavior and return values.
46 */
47struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
48 const char *con_id)
49{
50 return devm_gpiod_get_index(dev, con_id, 0);
51}
52EXPORT_SYMBOL(devm_gpiod_get);
53
54/**
55 * devm_gpiod_get_index - Resource-managed gpiod_get_index()
56 * @dev: GPIO consumer
57 * @con_id: function within the GPIO consumer
58 * @idx: index of the GPIO to obtain in the consumer
59 *
60 * Managed gpiod_get_index(). GPIO descriptors returned from this function are
61 * automatically disposed on driver detach. See gpiod_get_index() for detailed
62 * information about behavior and return values.
63 */
64struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
65 const char *con_id,
66 unsigned int idx)
67{
68 struct gpio_desc **dr;
69 struct gpio_desc *desc;
70
71 dr = devres_alloc(devm_gpiod_release, sizeof(struct gpiod_desc *),
72 GFP_KERNEL);
73 if (!dr)
74 return ERR_PTR(-ENOMEM);
75
76 desc = gpiod_get_index(dev, con_id, idx);
77 if (IS_ERR(desc)) {
78 devres_free(dr);
79 return desc;
80 }
81
82 *dr = desc;
83 devres_add(dev, dr);
84
85 return desc;
86}
87EXPORT_SYMBOL(devm_gpiod_get_index);
88
89/**
90 * devm_gpiod_put - Resource-managed gpiod_put()
91 * @desc: GPIO descriptor to dispose of
92 *
93 * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
94 * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
95 * will be disposed of by the resource management code.
96 */
97void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
98{
99 WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
100 &desc));
101}
102EXPORT_SYMBOL(devm_gpiod_put);
103
104
105
106
22static void devm_gpio_release(struct device *dev, void *res) 107static void devm_gpio_release(struct device *dev, void *res)
23{ 108{
24 unsigned *gpio = res; 109 unsigned *gpio = res;
diff --git a/drivers/gpio/gpio-74x164.c b/drivers/gpio/gpio-74x164.c
index 5d518d5db7a0..1e04bf91328d 100644
--- a/drivers/gpio/gpio-74x164.c
+++ b/drivers/gpio/gpio-74x164.c
@@ -176,7 +176,6 @@ static int gen_74x164_probe(struct spi_device *spi)
176 return ret; 176 return ret;
177 177
178exit_destroy: 178exit_destroy:
179 spi_set_drvdata(spi, NULL);
180 mutex_destroy(&chip->lock); 179 mutex_destroy(&chip->lock);
181 return ret; 180 return ret;
182} 181}
@@ -190,8 +189,6 @@ static int gen_74x164_remove(struct spi_device *spi)
190 if (chip == NULL) 189 if (chip == NULL)
191 return -ENODEV; 190 return -ENODEV;
192 191
193 spi_set_drvdata(spi, NULL);
194
195 ret = gpiochip_remove(&chip->gpio_chip); 192 ret = gpiochip_remove(&chip->gpio_chip);
196 if (!ret) 193 if (!ret)
197 mutex_destroy(&chip->lock); 194 mutex_destroy(&chip->lock);
@@ -212,7 +209,7 @@ static struct spi_driver gen_74x164_driver = {
212 .driver = { 209 .driver = {
213 .name = "74x164", 210 .name = "74x164",
214 .owner = THIS_MODULE, 211 .owner = THIS_MODULE,
215 .of_match_table = of_match_ptr(gen_74x164_dt_ids), 212 .of_match_table = gen_74x164_dt_ids,
216 }, 213 },
217 .probe = gen_74x164_probe, 214 .probe = gen_74x164_probe,
218 .remove = gen_74x164_remove, 215 .remove = gen_74x164_remove,
diff --git a/drivers/gpio/gpio-adnp.c b/drivers/gpio/gpio-adnp.c
index c0f3fc44ab0e..b204033acaeb 100644
--- a/drivers/gpio/gpio-adnp.c
+++ b/drivers/gpio/gpio-adnp.c
@@ -325,9 +325,9 @@ static irqreturn_t adnp_irq(int irq, void *data)
325 pending &= isr & ier; 325 pending &= isr & ier;
326 326
327 for_each_set_bit(bit, &pending, 8) { 327 for_each_set_bit(bit, &pending, 8) {
328 unsigned int virq; 328 unsigned int child_irq;
329 virq = irq_find_mapping(adnp->domain, base + bit); 329 child_irq = irq_find_mapping(adnp->domain, base + bit);
330 handle_nested_irq(virq); 330 handle_nested_irq(child_irq);
331 } 331 }
332 } 332 }
333 333
@@ -594,7 +594,7 @@ static struct i2c_driver adnp_i2c_driver = {
594 .driver = { 594 .driver = {
595 .name = "gpio-adnp", 595 .name = "gpio-adnp",
596 .owner = THIS_MODULE, 596 .owner = THIS_MODULE,
597 .of_match_table = of_match_ptr(adnp_of_match), 597 .of_match_table = adnp_of_match,
598 }, 598 },
599 .probe = adnp_i2c_probe, 599 .probe = adnp_i2c_probe,
600 .remove = adnp_i2c_remove, 600 .remove = adnp_i2c_remove,
diff --git a/drivers/gpio/gpio-arizona.c b/drivers/gpio/gpio-arizona.c
index fa8b6a762761..dceb5dcf9d16 100644
--- a/drivers/gpio/gpio-arizona.c
+++ b/drivers/gpio/gpio-arizona.c
@@ -109,10 +109,14 @@ static int arizona_gpio_probe(struct platform_device *pdev)
109 arizona_gpio->arizona = arizona; 109 arizona_gpio->arizona = arizona;
110 arizona_gpio->gpio_chip = template_chip; 110 arizona_gpio->gpio_chip = template_chip;
111 arizona_gpio->gpio_chip.dev = &pdev->dev; 111 arizona_gpio->gpio_chip.dev = &pdev->dev;
112#ifdef CONFIG_OF_GPIO
113 arizona_gpio->gpio_chip.of_node = arizona->dev->of_node;
114#endif
112 115
113 switch (arizona->type) { 116 switch (arizona->type) {
114 case WM5102: 117 case WM5102:
115 case WM5110: 118 case WM5110:
119 case WM8997:
116 arizona_gpio->gpio_chip.ngpio = 5; 120 arizona_gpio->gpio_chip.ngpio = 5;
117 break; 121 break;
118 default: 122 default:
diff --git a/drivers/gpio/gpio-bcm-kona.c b/drivers/gpio/gpio-bcm-kona.c
new file mode 100644
index 000000000000..72c927dc3be1
--- /dev/null
+++ b/drivers/gpio/gpio-bcm-kona.c
@@ -0,0 +1,640 @@
1/*
2 * Copyright (C) 2012-2013 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/bitops.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/gpio.h>
18#include <linux/of_device.h>
19#include <linux/of_irq.h>
20#include <linux/module.h>
21#include <linux/irqdomain.h>
22#include <linux/irqchip/chained_irq.h>
23
24#define BCM_GPIO_PASSWD 0x00a5a501
25#define GPIO_PER_BANK 32
26#define GPIO_MAX_BANK_NUM 8
27
28#define GPIO_BANK(gpio) ((gpio) >> 5)
29#define GPIO_BIT(gpio) ((gpio) & (GPIO_PER_BANK - 1))
30
31#define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
32#define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
33#define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
34#define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
35#define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
36#define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
37#define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
38#define GPIO_CONTROL(bank) (0x00000100 + ((bank) << 2))
39#define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
40
41#define GPIO_GPPWR_OFFSET 0x00000520
42
43#define GPIO_GPCTR0_DBR_SHIFT 5
44#define GPIO_GPCTR0_DBR_MASK 0x000001e0
45
46#define GPIO_GPCTR0_ITR_SHIFT 3
47#define GPIO_GPCTR0_ITR_MASK 0x00000018
48#define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
49#define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
50#define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
51
52#define GPIO_GPCTR0_IOTR_MASK 0x00000001
53#define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
54#define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
55
56#define GPIO_GPCTR0_DB_ENABLE_MASK 0x00000100
57
58#define LOCK_CODE 0xffffffff
59#define UNLOCK_CODE 0x00000000
60
61struct bcm_kona_gpio {
62 void __iomem *reg_base;
63 int num_bank;
64 spinlock_t lock;
65 struct gpio_chip gpio_chip;
66 struct irq_domain *irq_domain;
67 struct bcm_kona_gpio_bank *banks;
68 struct platform_device *pdev;
69};
70
71struct bcm_kona_gpio_bank {
72 int id;
73 int irq;
74 /* Used in the interrupt handler */
75 struct bcm_kona_gpio *kona_gpio;
76};
77
78static inline struct bcm_kona_gpio *to_kona_gpio(struct gpio_chip *chip)
79{
80 return container_of(chip, struct bcm_kona_gpio, gpio_chip);
81}
82
83static void bcm_kona_gpio_set_lockcode_bank(void __iomem *reg_base,
84 int bank_id, int lockcode)
85{
86 writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
87 writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
88}
89
90static inline void bcm_kona_gpio_lock_bank(void __iomem *reg_base, int bank_id)
91{
92 bcm_kona_gpio_set_lockcode_bank(reg_base, bank_id, LOCK_CODE);
93}
94
95static inline void bcm_kona_gpio_unlock_bank(void __iomem *reg_base,
96 int bank_id)
97{
98 bcm_kona_gpio_set_lockcode_bank(reg_base, bank_id, UNLOCK_CODE);
99}
100
101static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
102{
103 struct bcm_kona_gpio *kona_gpio;
104 void __iomem *reg_base;
105 int bank_id = GPIO_BANK(gpio);
106 int bit = GPIO_BIT(gpio);
107 u32 val, reg_offset;
108 unsigned long flags;
109
110 kona_gpio = to_kona_gpio(chip);
111 reg_base = kona_gpio->reg_base;
112 spin_lock_irqsave(&kona_gpio->lock, flags);
113 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
114
115 /* determine the GPIO pin direction */
116 val = readl(reg_base + GPIO_CONTROL(gpio));
117 val &= GPIO_GPCTR0_IOTR_MASK;
118
119 /* this function only applies to output pin */
120 if (GPIO_GPCTR0_IOTR_CMD_INPUT == val)
121 goto out;
122
123 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
124
125 val = readl(reg_base + reg_offset);
126 val |= BIT(bit);
127 writel(val, reg_base + reg_offset);
128
129out:
130 bcm_kona_gpio_lock_bank(reg_base, bank_id);
131 spin_unlock_irqrestore(&kona_gpio->lock, flags);
132}
133
134static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
135{
136 struct bcm_kona_gpio *kona_gpio;
137 void __iomem *reg_base;
138 int bank_id = GPIO_BANK(gpio);
139 int bit = GPIO_BIT(gpio);
140 u32 val, reg_offset;
141 unsigned long flags;
142
143 kona_gpio = to_kona_gpio(chip);
144 reg_base = kona_gpio->reg_base;
145 spin_lock_irqsave(&kona_gpio->lock, flags);
146 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
147
148 /* determine the GPIO pin direction */
149 val = readl(reg_base + GPIO_CONTROL(gpio));
150 val &= GPIO_GPCTR0_IOTR_MASK;
151
152 /* read the GPIO bank status */
153 reg_offset = (GPIO_GPCTR0_IOTR_CMD_INPUT == val) ?
154 GPIO_IN_STATUS(bank_id) : GPIO_OUT_STATUS(bank_id);
155 val = readl(reg_base + reg_offset);
156
157 bcm_kona_gpio_lock_bank(reg_base, bank_id);
158 spin_unlock_irqrestore(&kona_gpio->lock, flags);
159
160 /* return the specified bit status */
161 return !!(val & bit);
162}
163
164static int bcm_kona_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
165{
166 struct bcm_kona_gpio *kona_gpio;
167 void __iomem *reg_base;
168 u32 val;
169 unsigned long flags;
170 int bank_id = GPIO_BANK(gpio);
171
172 kona_gpio = to_kona_gpio(chip);
173 reg_base = kona_gpio->reg_base;
174 spin_lock_irqsave(&kona_gpio->lock, flags);
175 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
176
177 val = readl(reg_base + GPIO_CONTROL(gpio));
178 val &= ~GPIO_GPCTR0_IOTR_MASK;
179 val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
180 writel(val, reg_base + GPIO_CONTROL(gpio));
181
182 bcm_kona_gpio_lock_bank(reg_base, bank_id);
183 spin_unlock_irqrestore(&kona_gpio->lock, flags);
184
185 return 0;
186}
187
188static int bcm_kona_gpio_direction_output(struct gpio_chip *chip,
189 unsigned gpio, int value)
190{
191 struct bcm_kona_gpio *kona_gpio;
192 void __iomem *reg_base;
193 int bank_id = GPIO_BANK(gpio);
194 int bit = GPIO_BIT(gpio);
195 u32 val, reg_offset;
196 unsigned long flags;
197
198 kona_gpio = to_kona_gpio(chip);
199 reg_base = kona_gpio->reg_base;
200 spin_lock_irqsave(&kona_gpio->lock, flags);
201 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
202
203 val = readl(reg_base + GPIO_CONTROL(gpio));
204 val &= ~GPIO_GPCTR0_IOTR_MASK;
205 val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
206 writel(val, reg_base + GPIO_CONTROL(gpio));
207 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
208
209 val = readl(reg_base + reg_offset);
210 val |= BIT(bit);
211 writel(val, reg_base + reg_offset);
212
213 bcm_kona_gpio_lock_bank(reg_base, bank_id);
214 spin_unlock_irqrestore(&kona_gpio->lock, flags);
215
216 return 0;
217}
218
219static int bcm_kona_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
220{
221 struct bcm_kona_gpio *kona_gpio;
222
223 kona_gpio = to_kona_gpio(chip);
224 if (gpio >= kona_gpio->gpio_chip.ngpio)
225 return -ENXIO;
226 return irq_create_mapping(kona_gpio->irq_domain, gpio);
227}
228
229static int bcm_kona_gpio_set_debounce(struct gpio_chip *chip, unsigned gpio,
230 unsigned debounce)
231{
232 struct bcm_kona_gpio *kona_gpio;
233 void __iomem *reg_base;
234 u32 val, res;
235 unsigned long flags;
236 int bank_id = GPIO_BANK(gpio);
237
238 kona_gpio = to_kona_gpio(chip);
239 reg_base = kona_gpio->reg_base;
240 /* debounce must be 1-128ms (or 0) */
241 if ((debounce > 0 && debounce < 1000) || debounce > 128000) {
242 dev_err(chip->dev, "Debounce value %u not in range\n",
243 debounce);
244 return -EINVAL;
245 }
246
247 /* calculate debounce bit value */
248 if (debounce != 0) {
249 /* Convert to ms */
250 debounce /= 1000;
251 /* find the MSB */
252 res = fls(debounce) - 1;
253 /* Check if MSB-1 is set (round up or down) */
254 if (res > 0 && (debounce & BIT(res - 1)))
255 res++;
256 }
257
258 /* spin lock for read-modify-write of the GPIO register */
259 spin_lock_irqsave(&kona_gpio->lock, flags);
260 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
261
262 val = readl(reg_base + GPIO_CONTROL(gpio));
263 val &= ~GPIO_GPCTR0_DBR_MASK;
264
265 if (debounce == 0) {
266 /* disable debounce */
267 val &= ~GPIO_GPCTR0_DB_ENABLE_MASK;
268 } else {
269 val |= GPIO_GPCTR0_DB_ENABLE_MASK |
270 (res << GPIO_GPCTR0_DBR_SHIFT);
271 }
272
273 writel(val, reg_base + GPIO_CONTROL(gpio));
274
275 bcm_kona_gpio_lock_bank(reg_base, bank_id);
276 spin_unlock_irqrestore(&kona_gpio->lock, flags);
277
278 return 0;
279}
280
281static struct gpio_chip template_chip = {
282 .label = "bcm-kona-gpio",
283 .owner = THIS_MODULE,
284 .direction_input = bcm_kona_gpio_direction_input,
285 .get = bcm_kona_gpio_get,
286 .direction_output = bcm_kona_gpio_direction_output,
287 .set = bcm_kona_gpio_set,
288 .set_debounce = bcm_kona_gpio_set_debounce,
289 .to_irq = bcm_kona_gpio_to_irq,
290 .base = 0,
291};
292
293static void bcm_kona_gpio_irq_ack(struct irq_data *d)
294{
295 struct bcm_kona_gpio *kona_gpio;
296 void __iomem *reg_base;
297 int gpio = d->hwirq;
298 int bank_id = GPIO_BANK(gpio);
299 int bit = GPIO_BIT(gpio);
300 u32 val;
301 unsigned long flags;
302
303 kona_gpio = irq_data_get_irq_chip_data(d);
304 reg_base = kona_gpio->reg_base;
305 spin_lock_irqsave(&kona_gpio->lock, flags);
306 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
307
308 val = readl(reg_base + GPIO_INT_STATUS(bank_id));
309 val |= BIT(bit);
310 writel(val, reg_base + GPIO_INT_STATUS(bank_id));
311
312 bcm_kona_gpio_lock_bank(reg_base, bank_id);
313 spin_unlock_irqrestore(&kona_gpio->lock, flags);
314}
315
316static void bcm_kona_gpio_irq_mask(struct irq_data *d)
317{
318 struct bcm_kona_gpio *kona_gpio;
319 void __iomem *reg_base;
320 int gpio = d->hwirq;
321 int bank_id = GPIO_BANK(gpio);
322 int bit = GPIO_BIT(gpio);
323 u32 val;
324 unsigned long flags;
325
326 kona_gpio = irq_data_get_irq_chip_data(d);
327 reg_base = kona_gpio->reg_base;
328 spin_lock_irqsave(&kona_gpio->lock, flags);
329 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
330
331 val = readl(reg_base + GPIO_INT_MASK(bank_id));
332 val |= BIT(bit);
333 writel(val, reg_base + GPIO_INT_MASK(bank_id));
334
335 bcm_kona_gpio_lock_bank(reg_base, bank_id);
336 spin_unlock_irqrestore(&kona_gpio->lock, flags);
337}
338
339static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
340{
341 struct bcm_kona_gpio *kona_gpio;
342 void __iomem *reg_base;
343 int gpio = d->hwirq;
344 int bank_id = GPIO_BANK(gpio);
345 int bit = GPIO_BIT(gpio);
346 u32 val;
347 unsigned long flags;
348
349 kona_gpio = irq_data_get_irq_chip_data(d);
350 reg_base = kona_gpio->reg_base;
351 spin_lock_irqsave(&kona_gpio->lock, flags);
352 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
353
354 val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
355 val |= BIT(bit);
356 writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
357
358 bcm_kona_gpio_lock_bank(reg_base, bank_id);
359 spin_unlock_irqrestore(&kona_gpio->lock, flags);
360}
361
362static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
363{
364 struct bcm_kona_gpio *kona_gpio;
365 void __iomem *reg_base;
366 int gpio = d->hwirq;
367 u32 lvl_type;
368 u32 val;
369 unsigned long flags;
370 int bank_id = GPIO_BANK(gpio);
371
372 kona_gpio = irq_data_get_irq_chip_data(d);
373 reg_base = kona_gpio->reg_base;
374 switch (type & IRQ_TYPE_SENSE_MASK) {
375 case IRQ_TYPE_EDGE_RISING:
376 lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
377 break;
378
379 case IRQ_TYPE_EDGE_FALLING:
380 lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
381 break;
382
383 case IRQ_TYPE_EDGE_BOTH:
384 lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
385 break;
386
387 case IRQ_TYPE_LEVEL_HIGH:
388 case IRQ_TYPE_LEVEL_LOW:
389 /* BCM GPIO doesn't support level triggering */
390 default:
391 dev_err(kona_gpio->gpio_chip.dev,
392 "Invalid BCM GPIO irq type 0x%x\n", type);
393 return -EINVAL;
394 }
395
396 spin_lock_irqsave(&kona_gpio->lock, flags);
397 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
398
399 val = readl(reg_base + GPIO_CONTROL(gpio));
400 val &= ~GPIO_GPCTR0_ITR_MASK;
401 val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
402 writel(val, reg_base + GPIO_CONTROL(gpio));
403
404 bcm_kona_gpio_lock_bank(reg_base, bank_id);
405 spin_unlock_irqrestore(&kona_gpio->lock, flags);
406
407 return 0;
408}
409
410static void bcm_kona_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
411{
412 void __iomem *reg_base;
413 int bit, bank_id;
414 unsigned long sta;
415 struct bcm_kona_gpio_bank *bank = irq_get_handler_data(irq);
416 struct irq_chip *chip = irq_desc_get_chip(desc);
417
418 chained_irq_enter(chip, desc);
419
420 /*
421 * For bank interrupts, we can't use chip_data to store the kona_gpio
422 * pointer, since GIC needs it for its own purposes. Therefore, we get
423 * our pointer from the bank structure.
424 */
425 reg_base = bank->kona_gpio->reg_base;
426 bank_id = bank->id;
427 bcm_kona_gpio_unlock_bank(reg_base, bank_id);
428
429 while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
430 (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
431 for_each_set_bit(bit, &sta, 32) {
432 int hwirq = GPIO_PER_BANK * bank_id + bit;
433 int child_irq =
434 irq_find_mapping(bank->kona_gpio->irq_domain,
435 hwirq);
436 /*
437 * Clear interrupt before handler is called so we don't
438 * miss any interrupt occurred during executing them.
439 */
440 writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
441 BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
442 /* Invoke interrupt handler */
443 generic_handle_irq(child_irq);
444 }
445 }
446
447 bcm_kona_gpio_lock_bank(reg_base, bank_id);
448
449 chained_irq_exit(chip, desc);
450}
451
452static struct irq_chip bcm_gpio_irq_chip = {
453 .name = "bcm-kona-gpio",
454 .irq_ack = bcm_kona_gpio_irq_ack,
455 .irq_mask = bcm_kona_gpio_irq_mask,
456 .irq_unmask = bcm_kona_gpio_irq_unmask,
457 .irq_set_type = bcm_kona_gpio_irq_set_type,
458};
459
460static struct __initconst of_device_id bcm_kona_gpio_of_match[] = {
461 { .compatible = "brcm,kona-gpio" },
462 {}
463};
464
465MODULE_DEVICE_TABLE(of, bcm_kona_gpio_of_match);
466
467/*
468 * This lock class tells lockdep that GPIO irqs are in a different
469 * category than their parents, so it won't report false recursion.
470 */
471static struct lock_class_key gpio_lock_class;
472
473static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
474 irq_hw_number_t hwirq)
475{
476 int ret;
477
478 ret = irq_set_chip_data(irq, d->host_data);
479 if (ret < 0)
480 return ret;
481 irq_set_lockdep_class(irq, &gpio_lock_class);
482 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
483#ifdef CONFIG_ARM
484 set_irq_flags(irq, IRQF_VALID);
485#else
486 irq_set_noprobe(irq);
487#endif
488
489 return 0;
490}
491
492static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
493{
494 irq_set_chip_and_handler(irq, NULL, NULL);
495 irq_set_chip_data(irq, NULL);
496}
497
498static struct irq_domain_ops bcm_kona_irq_ops = {
499 .map = bcm_kona_gpio_irq_map,
500 .unmap = bcm_kona_gpio_irq_unmap,
501 .xlate = irq_domain_xlate_twocell,
502};
503
504static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
505{
506 void __iomem *reg_base;
507 int i;
508
509 reg_base = kona_gpio->reg_base;
510 /* disable interrupts and clear status */
511 for (i = 0; i < kona_gpio->num_bank; i++) {
512 bcm_kona_gpio_unlock_bank(reg_base, i);
513 writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
514 writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
515 bcm_kona_gpio_lock_bank(reg_base, i);
516 }
517}
518
519static int bcm_kona_gpio_probe(struct platform_device *pdev)
520{
521 struct device *dev = &pdev->dev;
522 const struct of_device_id *match;
523 struct resource *res;
524 struct bcm_kona_gpio_bank *bank;
525 struct bcm_kona_gpio *kona_gpio;
526 struct gpio_chip *chip;
527 int ret;
528 int i;
529
530 match = of_match_device(bcm_kona_gpio_of_match, dev);
531 if (!match) {
532 dev_err(dev, "Failed to find gpio controller\n");
533 return -ENODEV;
534 }
535
536 kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
537 if (!kona_gpio)
538 return -ENOMEM;
539
540 kona_gpio->gpio_chip = template_chip;
541 chip = &kona_gpio->gpio_chip;
542 kona_gpio->num_bank = of_irq_count(dev->of_node);
543 if (kona_gpio->num_bank == 0) {
544 dev_err(dev, "Couldn't determine # GPIO banks\n");
545 return -ENOENT;
546 }
547 if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
548 dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
549 GPIO_MAX_BANK_NUM);
550 return -ENXIO;
551 }
552 kona_gpio->banks = devm_kzalloc(dev,
553 kona_gpio->num_bank *
554 sizeof(*kona_gpio->banks), GFP_KERNEL);
555 if (!kona_gpio->banks)
556 return -ENOMEM;
557
558 kona_gpio->pdev = pdev;
559 platform_set_drvdata(pdev, kona_gpio);
560 chip->of_node = dev->of_node;
561 chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
562
563 kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
564 chip->ngpio,
565 &bcm_kona_irq_ops,
566 kona_gpio);
567 if (!kona_gpio->irq_domain) {
568 dev_err(dev, "Couldn't allocate IRQ domain\n");
569 return -ENXIO;
570 }
571
572 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
573 kona_gpio->reg_base = devm_ioremap_resource(dev, res);
574 if (IS_ERR(kona_gpio->reg_base)) {
575 ret = -ENXIO;
576 goto err_irq_domain;
577 }
578
579 for (i = 0; i < kona_gpio->num_bank; i++) {
580 bank = &kona_gpio->banks[i];
581 bank->id = i;
582 bank->irq = platform_get_irq(pdev, i);
583 bank->kona_gpio = kona_gpio;
584 if (bank->irq < 0) {
585 dev_err(dev, "Couldn't get IRQ for bank %d", i);
586 ret = -ENOENT;
587 goto err_irq_domain;
588 }
589 }
590
591 dev_info(&pdev->dev, "Setting up Kona GPIO\n");
592
593 bcm_kona_gpio_reset(kona_gpio);
594
595 ret = gpiochip_add(chip);
596 if (ret < 0) {
597 dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
598 goto err_irq_domain;
599 }
600 for (i = 0; i < chip->ngpio; i++) {
601 int irq = bcm_kona_gpio_to_irq(chip, i);
602 irq_set_lockdep_class(irq, &gpio_lock_class);
603 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip,
604 handle_simple_irq);
605#ifdef CONFIG_ARM
606 set_irq_flags(irq, IRQF_VALID);
607#else
608 irq_set_noprobe(irq);
609#endif
610 }
611 for (i = 0; i < kona_gpio->num_bank; i++) {
612 bank = &kona_gpio->banks[i];
613 irq_set_chained_handler(bank->irq, bcm_kona_gpio_irq_handler);
614 irq_set_handler_data(bank->irq, bank);
615 }
616
617 spin_lock_init(&kona_gpio->lock);
618
619 return 0;
620
621err_irq_domain:
622 irq_domain_remove(kona_gpio->irq_domain);
623
624 return ret;
625}
626
627static struct platform_driver bcm_kona_gpio_driver = {
628 .driver = {
629 .name = "bcm-kona-gpio",
630 .owner = THIS_MODULE,
631 .of_match_table = bcm_kona_gpio_of_match,
632 },
633 .probe = bcm_kona_gpio_probe,
634};
635
636module_platform_driver(bcm_kona_gpio_driver);
637
638MODULE_AUTHOR("Broadcom");
639MODULE_DESCRIPTION("Broadcom Kona GPIO Driver");
640MODULE_LICENSE("GPL v2");
diff --git a/drivers/gpio/gpio-bt8xx.c b/drivers/gpio/gpio-bt8xx.c
index 8369e71ebe4f..9dfe36fd8baf 100644
--- a/drivers/gpio/gpio-bt8xx.c
+++ b/drivers/gpio/gpio-bt8xx.c
@@ -228,7 +228,6 @@ static int bt8xxgpio_probe(struct pci_dev *dev,
228err_release_mem: 228err_release_mem:
229 release_mem_region(pci_resource_start(dev, 0), 229 release_mem_region(pci_resource_start(dev, 0),
230 pci_resource_len(dev, 0)); 230 pci_resource_len(dev, 0));
231 pci_set_drvdata(dev, NULL);
232err_disable: 231err_disable:
233 pci_disable_device(dev); 232 pci_disable_device(dev);
234err_freebg: 233err_freebg:
@@ -252,7 +251,6 @@ static void bt8xxgpio_remove(struct pci_dev *pdev)
252 pci_resource_len(pdev, 0)); 251 pci_resource_len(pdev, 0));
253 pci_disable_device(pdev); 252 pci_disable_device(pdev);
254 253
255 pci_set_drvdata(pdev, NULL);
256 kfree(bg); 254 kfree(bg);
257} 255}
258 256
diff --git a/drivers/gpio/gpio-clps711x.c b/drivers/gpio/gpio-clps711x.c
index 0edaf2ce9266..0924f20fa47f 100644
--- a/drivers/gpio/gpio-clps711x.c
+++ b/drivers/gpio/gpio-clps711x.c
@@ -87,7 +87,7 @@ static struct platform_driver clps711x_gpio_driver = {
87 .driver = { 87 .driver = {
88 .name = "clps711x-gpio", 88 .name = "clps711x-gpio",
89 .owner = THIS_MODULE, 89 .owner = THIS_MODULE,
90 .of_match_table = of_match_ptr(clps711x_gpio_ids), 90 .of_match_table = clps711x_gpio_ids,
91 }, 91 },
92 .probe = clps711x_gpio_probe, 92 .probe = clps711x_gpio_probe,
93 .remove = clps711x_gpio_remove, 93 .remove = clps711x_gpio_remove,
diff --git a/drivers/gpio/gpio-em.c b/drivers/gpio/gpio-em.c
index c6e1f086efe8..ec190361bf2e 100644
--- a/drivers/gpio/gpio-em.c
+++ b/drivers/gpio/gpio-em.c
@@ -232,16 +232,16 @@ static void em_gio_free(struct gpio_chip *chip, unsigned offset)
232 em_gio_direction_input(chip, offset); 232 em_gio_direction_input(chip, offset);
233} 233}
234 234
235static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq, 235static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq,
236 irq_hw_number_t hw) 236 irq_hw_number_t hwirq)
237{ 237{
238 struct em_gio_priv *p = h->host_data; 238 struct em_gio_priv *p = h->host_data;
239 239
240 pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq); 240 pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq);
241 241
242 irq_set_chip_data(virq, h->host_data); 242 irq_set_chip_data(irq, h->host_data);
243 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq); 243 irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
244 set_irq_flags(virq, IRQF_VALID); /* kill me now */ 244 set_irq_flags(irq, IRQF_VALID); /* kill me now */
245 return 0; 245 return 0;
246} 246}
247 247
@@ -319,6 +319,7 @@ static int em_gio_probe(struct platform_device *pdev)
319 } 319 }
320 320
321 gpio_chip = &p->gpio_chip; 321 gpio_chip = &p->gpio_chip;
322 gpio_chip->of_node = pdev->dev.of_node;
322 gpio_chip->direction_input = em_gio_direction_input; 323 gpio_chip->direction_input = em_gio_direction_input;
323 gpio_chip->get = em_gio_get; 324 gpio_chip->get = em_gio_get;
324 gpio_chip->direction_output = em_gio_direction_output; 325 gpio_chip->direction_output = em_gio_direction_output;
diff --git a/drivers/gpio/gpio-ep93xx.c b/drivers/gpio/gpio-ep93xx.c
index 56b98eebe1fc..80829f3c6543 100644
--- a/drivers/gpio/gpio-ep93xx.c
+++ b/drivers/gpio/gpio-ep93xx.c
@@ -51,15 +51,15 @@ static void ep93xx_gpio_update_int_params(unsigned port)
51{ 51{
52 BUG_ON(port > 2); 52 BUG_ON(port > 2);
53 53
54 __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port])); 54 writeb_relaxed(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
55 55
56 __raw_writeb(gpio_int_type2[port], 56 writeb_relaxed(gpio_int_type2[port],
57 EP93XX_GPIO_REG(int_type2_register_offset[port])); 57 EP93XX_GPIO_REG(int_type2_register_offset[port]));
58 58
59 __raw_writeb(gpio_int_type1[port], 59 writeb_relaxed(gpio_int_type1[port],
60 EP93XX_GPIO_REG(int_type1_register_offset[port])); 60 EP93XX_GPIO_REG(int_type1_register_offset[port]));
61 61
62 __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port], 62 writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
63 EP93XX_GPIO_REG(int_en_register_offset[port])); 63 EP93XX_GPIO_REG(int_en_register_offset[port]));
64} 64}
65 65
@@ -74,7 +74,7 @@ static void ep93xx_gpio_int_debounce(unsigned int irq, bool enable)
74 else 74 else
75 gpio_int_debounce[port] &= ~port_mask; 75 gpio_int_debounce[port] &= ~port_mask;
76 76
77 __raw_writeb(gpio_int_debounce[port], 77 writeb(gpio_int_debounce[port],
78 EP93XX_GPIO_REG(int_debounce_register_offset[port])); 78 EP93XX_GPIO_REG(int_debounce_register_offset[port]));
79} 79}
80 80
@@ -83,7 +83,7 @@ static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
83 unsigned char status; 83 unsigned char status;
84 int i; 84 int i;
85 85
86 status = __raw_readb(EP93XX_GPIO_A_INT_STATUS); 86 status = readb(EP93XX_GPIO_A_INT_STATUS);
87 for (i = 0; i < 8; i++) { 87 for (i = 0; i < 8; i++) {
88 if (status & (1 << i)) { 88 if (status & (1 << i)) {
89 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i; 89 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
@@ -91,7 +91,7 @@ static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
91 } 91 }
92 } 92 }
93 93
94 status = __raw_readb(EP93XX_GPIO_B_INT_STATUS); 94 status = readb(EP93XX_GPIO_B_INT_STATUS);
95 for (i = 0; i < 8; i++) { 95 for (i = 0; i < 8; i++) {
96 if (status & (1 << i)) { 96 if (status & (1 << i)) {
97 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i; 97 int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
@@ -124,7 +124,7 @@ static void ep93xx_gpio_irq_ack(struct irq_data *d)
124 ep93xx_gpio_update_int_params(port); 124 ep93xx_gpio_update_int_params(port);
125 } 125 }
126 126
127 __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port])); 127 writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
128} 128}
129 129
130static void ep93xx_gpio_irq_mask_ack(struct irq_data *d) 130static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
@@ -139,7 +139,7 @@ static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
139 gpio_int_unmasked[port] &= ~port_mask; 139 gpio_int_unmasked[port] &= ~port_mask;
140 ep93xx_gpio_update_int_params(port); 140 ep93xx_gpio_update_int_params(port);
141 141
142 __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port])); 142 writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
143} 143}
144 144
145static void ep93xx_gpio_irq_mask(struct irq_data *d) 145static void ep93xx_gpio_irq_mask(struct irq_data *d)
diff --git a/drivers/gpio/gpio-intel-mid.c b/drivers/gpio/gpio-intel-mid.c
new file mode 100644
index 000000000000..be803af658ac
--- /dev/null
+++ b/drivers/gpio/gpio-intel-mid.c
@@ -0,0 +1,471 @@
1/*
2 * Moorestown platform Langwell chip GPIO driver
3 *
4 * Copyright (c) 2008, 2009, 2013, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/* Supports:
21 * Moorestown platform Langwell chip.
22 * Medfield platform Penwell chip.
23 * Clovertrail platform Cloverview chip.
24 * Merrifield platform Tangier chip.
25 */
26
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/platform_device.h>
30#include <linux/kernel.h>
31#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/interrupt.h>
34#include <linux/init.h>
35#include <linux/irq.h>
36#include <linux/io.h>
37#include <linux/gpio.h>
38#include <linux/slab.h>
39#include <linux/pm_runtime.h>
40#include <linux/irqdomain.h>
41
42#define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
43#define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
44
45/*
46 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
47 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
48 * registers to control them, so we only define the order here instead of a
49 * structure, to get a bit offset for a pin (use GPDR as an example):
50 *
51 * nreg = ngpio / 32;
52 * reg = offset / 32;
53 * bit = offset % 32;
54 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
55 *
56 * so the bit of reg_addr is to control pin offset's GPDR feature
57*/
58
59enum GPIO_REG {
60 GPLR = 0, /* pin level read-only */
61 GPDR, /* pin direction */
62 GPSR, /* pin set */
63 GPCR, /* pin clear */
64 GRER, /* rising edge detect */
65 GFER, /* falling edge detect */
66 GEDR, /* edge detect result */
67 GAFR, /* alt function */
68};
69
70/* intel_mid gpio driver data */
71struct intel_mid_gpio_ddata {
72 u16 ngpio; /* number of gpio pins */
73 u32 gplr_offset; /* offset of first GPLR register from base */
74 u32 flis_base; /* base address of FLIS registers */
75 u32 flis_len; /* length of FLIS registers */
76 u32 (*get_flis_offset)(int gpio);
77 u32 chip_irq_type; /* chip interrupt type */
78};
79
80struct intel_mid_gpio {
81 struct gpio_chip chip;
82 void __iomem *reg_base;
83 spinlock_t lock;
84 struct pci_dev *pdev;
85 struct irq_domain *domain;
86};
87
88#define to_intel_gpio_priv(chip) container_of(chip, struct intel_mid_gpio, chip)
89
90static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
91 enum GPIO_REG reg_type)
92{
93 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
94 unsigned nreg = chip->ngpio / 32;
95 u8 reg = offset / 32;
96
97 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
98}
99
100static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
101 enum GPIO_REG reg_type)
102{
103 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
104 unsigned nreg = chip->ngpio / 32;
105 u8 reg = offset / 16;
106
107 return priv->reg_base + reg_type * nreg * 4 + reg * 4;
108}
109
110static int intel_gpio_request(struct gpio_chip *chip, unsigned offset)
111{
112 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
113 u32 value = readl(gafr);
114 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
115
116 if (af) {
117 value &= ~(3 << shift);
118 writel(value, gafr);
119 }
120 return 0;
121}
122
123static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
124{
125 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
126
127 return readl(gplr) & BIT(offset % 32);
128}
129
130static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
131{
132 void __iomem *gpsr, *gpcr;
133
134 if (value) {
135 gpsr = gpio_reg(chip, offset, GPSR);
136 writel(BIT(offset % 32), gpsr);
137 } else {
138 gpcr = gpio_reg(chip, offset, GPCR);
139 writel(BIT(offset % 32), gpcr);
140 }
141}
142
143static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
144{
145 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
146 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
147 u32 value;
148 unsigned long flags;
149
150 if (priv->pdev)
151 pm_runtime_get(&priv->pdev->dev);
152
153 spin_lock_irqsave(&priv->lock, flags);
154 value = readl(gpdr);
155 value &= ~BIT(offset % 32);
156 writel(value, gpdr);
157 spin_unlock_irqrestore(&priv->lock, flags);
158
159 if (priv->pdev)
160 pm_runtime_put(&priv->pdev->dev);
161
162 return 0;
163}
164
165static int intel_gpio_direction_output(struct gpio_chip *chip,
166 unsigned offset, int value)
167{
168 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
169 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
170 unsigned long flags;
171
172 intel_gpio_set(chip, offset, value);
173
174 if (priv->pdev)
175 pm_runtime_get(&priv->pdev->dev);
176
177 spin_lock_irqsave(&priv->lock, flags);
178 value = readl(gpdr);
179 value |= BIT(offset % 32);
180 writel(value, gpdr);
181 spin_unlock_irqrestore(&priv->lock, flags);
182
183 if (priv->pdev)
184 pm_runtime_put(&priv->pdev->dev);
185
186 return 0;
187}
188
189static int intel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
190{
191 struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
192 return irq_create_mapping(priv->domain, offset);
193}
194
195static int intel_mid_irq_type(struct irq_data *d, unsigned type)
196{
197 struct intel_mid_gpio *priv = irq_data_get_irq_chip_data(d);
198 u32 gpio = irqd_to_hwirq(d);
199 unsigned long flags;
200 u32 value;
201 void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
202 void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
203
204 if (gpio >= priv->chip.ngpio)
205 return -EINVAL;
206
207 if (priv->pdev)
208 pm_runtime_get(&priv->pdev->dev);
209
210 spin_lock_irqsave(&priv->lock, flags);
211 if (type & IRQ_TYPE_EDGE_RISING)
212 value = readl(grer) | BIT(gpio % 32);
213 else
214 value = readl(grer) & (~BIT(gpio % 32));
215 writel(value, grer);
216
217 if (type & IRQ_TYPE_EDGE_FALLING)
218 value = readl(gfer) | BIT(gpio % 32);
219 else
220 value = readl(gfer) & (~BIT(gpio % 32));
221 writel(value, gfer);
222 spin_unlock_irqrestore(&priv->lock, flags);
223
224 if (priv->pdev)
225 pm_runtime_put(&priv->pdev->dev);
226
227 return 0;
228}
229
230static void intel_mid_irq_unmask(struct irq_data *d)
231{
232}
233
234static void intel_mid_irq_mask(struct irq_data *d)
235{
236}
237
238static struct irq_chip intel_mid_irqchip = {
239 .name = "INTEL_MID-GPIO",
240 .irq_mask = intel_mid_irq_mask,
241 .irq_unmask = intel_mid_irq_unmask,
242 .irq_set_type = intel_mid_irq_type,
243};
244
245static const struct intel_mid_gpio_ddata gpio_lincroft = {
246 .ngpio = 64,
247};
248
249static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
250 .ngpio = 96,
251 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
252};
253
254static const struct intel_mid_gpio_ddata gpio_penwell_core = {
255 .ngpio = 96,
256 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
257};
258
259static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
260 .ngpio = 96,
261 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
262};
263
264static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
265 .ngpio = 96,
266 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
267};
268
269static const struct intel_mid_gpio_ddata gpio_tangier = {
270 .ngpio = 192,
271 .gplr_offset = 4,
272 .flis_base = 0xff0c0000,
273 .flis_len = 0x8000,
274 .get_flis_offset = NULL,
275 .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
276};
277
278static DEFINE_PCI_DEVICE_TABLE(intel_gpio_ids) = {
279 {
280 /* Lincroft */
281 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
282 .driver_data = (kernel_ulong_t)&gpio_lincroft,
283 },
284 {
285 /* Penwell AON */
286 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
287 .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
288 },
289 {
290 /* Penwell Core */
291 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
292 .driver_data = (kernel_ulong_t)&gpio_penwell_core,
293 },
294 {
295 /* Cloverview Aon */
296 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
297 .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
298 },
299 {
300 /* Cloverview Core */
301 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
302 .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
303 },
304 {
305 /* Tangier */
306 PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x1199),
307 .driver_data = (kernel_ulong_t)&gpio_tangier,
308 },
309 { 0 }
310};
311MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
312
313static void intel_mid_irq_handler(unsigned irq, struct irq_desc *desc)
314{
315 struct irq_data *data = irq_desc_get_irq_data(desc);
316 struct intel_mid_gpio *priv = irq_data_get_irq_handler_data(data);
317 struct irq_chip *chip = irq_data_get_irq_chip(data);
318 u32 base, gpio, mask;
319 unsigned long pending;
320 void __iomem *gedr;
321
322 /* check GPIO controller to check which pin triggered the interrupt */
323 for (base = 0; base < priv->chip.ngpio; base += 32) {
324 gedr = gpio_reg(&priv->chip, base, GEDR);
325 while ((pending = readl(gedr))) {
326 gpio = __ffs(pending);
327 mask = BIT(gpio);
328 /* Clear before handling so we can't lose an edge */
329 writel(mask, gedr);
330 generic_handle_irq(irq_find_mapping(priv->domain,
331 base + gpio));
332 }
333 }
334
335 chip->irq_eoi(data);
336}
337
338static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
339{
340 void __iomem *reg;
341 unsigned base;
342
343 for (base = 0; base < priv->chip.ngpio; base += 32) {
344 /* Clear the rising-edge detect register */
345 reg = gpio_reg(&priv->chip, base, GRER);
346 writel(0, reg);
347 /* Clear the falling-edge detect register */
348 reg = gpio_reg(&priv->chip, base, GFER);
349 writel(0, reg);
350 /* Clear the edge detect status register */
351 reg = gpio_reg(&priv->chip, base, GEDR);
352 writel(~0, reg);
353 }
354}
355
356static int intel_gpio_irq_map(struct irq_domain *d, unsigned int irq,
357 irq_hw_number_t hwirq)
358{
359 struct intel_mid_gpio *priv = d->host_data;
360
361 irq_set_chip_and_handler_name(irq, &intel_mid_irqchip,
362 handle_simple_irq, "demux");
363 irq_set_chip_data(irq, priv);
364 irq_set_irq_type(irq, IRQ_TYPE_NONE);
365
366 return 0;
367}
368
369static const struct irq_domain_ops intel_gpio_irq_ops = {
370 .map = intel_gpio_irq_map,
371 .xlate = irq_domain_xlate_twocell,
372};
373
374static int intel_gpio_runtime_idle(struct device *dev)
375{
376 pm_schedule_suspend(dev, 500);
377 return -EBUSY;
378}
379
380static const struct dev_pm_ops intel_gpio_pm_ops = {
381 SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
382};
383
384static int intel_gpio_probe(struct pci_dev *pdev,
385 const struct pci_device_id *id)
386{
387 void __iomem *base;
388 struct intel_mid_gpio *priv;
389 u32 gpio_base;
390 u32 irq_base;
391 int retval;
392 struct intel_mid_gpio_ddata *ddata =
393 (struct intel_mid_gpio_ddata *)id->driver_data;
394
395 retval = pcim_enable_device(pdev);
396 if (retval)
397 return retval;
398
399 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
400 if (retval) {
401 dev_err(&pdev->dev, "I/O memory mapping error\n");
402 return retval;
403 }
404
405 base = pcim_iomap_table(pdev)[1];
406
407 irq_base = readl(base);
408 gpio_base = readl(sizeof(u32) + base);
409
410 /* release the IO mapping, since we already get the info from bar1 */
411 pcim_iounmap_regions(pdev, 1 << 1);
412
413 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
414 if (!priv) {
415 dev_err(&pdev->dev, "can't allocate chip data\n");
416 return -ENOMEM;
417 }
418
419 priv->reg_base = pcim_iomap_table(pdev)[0];
420 priv->chip.label = dev_name(&pdev->dev);
421 priv->chip.request = intel_gpio_request;
422 priv->chip.direction_input = intel_gpio_direction_input;
423 priv->chip.direction_output = intel_gpio_direction_output;
424 priv->chip.get = intel_gpio_get;
425 priv->chip.set = intel_gpio_set;
426 priv->chip.to_irq = intel_gpio_to_irq;
427 priv->chip.base = gpio_base;
428 priv->chip.ngpio = ddata->ngpio;
429 priv->chip.can_sleep = 0;
430 priv->pdev = pdev;
431
432 spin_lock_init(&priv->lock);
433
434 priv->domain = irq_domain_add_simple(pdev->dev.of_node, ddata->ngpio,
435 irq_base, &intel_gpio_irq_ops, priv);
436 if (!priv->domain)
437 return -ENOMEM;
438
439 pci_set_drvdata(pdev, priv);
440 retval = gpiochip_add(&priv->chip);
441 if (retval) {
442 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
443 return retval;
444 }
445
446 intel_mid_irq_init_hw(priv);
447
448 irq_set_handler_data(pdev->irq, priv);
449 irq_set_chained_handler(pdev->irq, intel_mid_irq_handler);
450
451 pm_runtime_put_noidle(&pdev->dev);
452 pm_runtime_allow(&pdev->dev);
453
454 return 0;
455}
456
457static struct pci_driver intel_gpio_driver = {
458 .name = "intel_mid_gpio",
459 .id_table = intel_gpio_ids,
460 .probe = intel_gpio_probe,
461 .driver = {
462 .pm = &intel_gpio_pm_ops,
463 },
464};
465
466static int __init intel_gpio_init(void)
467{
468 return pci_register_driver(&intel_gpio_driver);
469}
470
471device_initcall(intel_gpio_init);
diff --git a/arch/arm/plat-iop/gpio.c b/drivers/gpio/gpio-iop.c
index 697de6dc4936..c22a61be3a9c 100644
--- a/arch/arm/plat-iop/gpio.c
+++ b/drivers/gpio/gpio-iop.c
@@ -16,42 +16,61 @@
16#include <linux/errno.h> 16#include <linux/errno.h>
17#include <linux/gpio.h> 17#include <linux/gpio.h>
18#include <linux/export.h> 18#include <linux/export.h>
19#include <asm/hardware/iop3xx.h> 19#include <linux/platform_device.h>
20#include <mach/gpio.h> 20#include <linux/bitops.h>
21#include <linux/io.h>
21 22
22void gpio_line_config(int line, int direction) 23#define IOP3XX_N_GPIOS 8
24
25#define GPIO_IN 0
26#define GPIO_OUT 1
27#define GPIO_LOW 0
28#define GPIO_HIGH 1
29
30/* Memory base offset */
31static void __iomem *base;
32
33#define IOP3XX_GPIO_REG(reg) (base + (reg))
34#define IOP3XX_GPOE IOP3XX_GPIO_REG(0x0000)
35#define IOP3XX_GPID IOP3XX_GPIO_REG(0x0004)
36#define IOP3XX_GPOD IOP3XX_GPIO_REG(0x0008)
37
38static void gpio_line_config(int line, int direction)
23{ 39{
24 unsigned long flags; 40 unsigned long flags;
41 u32 val;
25 42
26 local_irq_save(flags); 43 local_irq_save(flags);
44 val = readl(IOP3XX_GPOE);
27 if (direction == GPIO_IN) { 45 if (direction == GPIO_IN) {
28 *IOP3XX_GPOE |= 1 << line; 46 val |= BIT(line);
29 } else if (direction == GPIO_OUT) { 47 } else if (direction == GPIO_OUT) {
30 *IOP3XX_GPOE &= ~(1 << line); 48 val &= ~BIT(line);
31 } 49 }
50 writel(val, IOP3XX_GPOE);
32 local_irq_restore(flags); 51 local_irq_restore(flags);
33} 52}
34EXPORT_SYMBOL(gpio_line_config);
35 53
36int gpio_line_get(int line) 54static int gpio_line_get(int line)
37{ 55{
38 return !!(*IOP3XX_GPID & (1 << line)); 56 return !!(readl(IOP3XX_GPID) & BIT(line));
39} 57}
40EXPORT_SYMBOL(gpio_line_get);
41 58
42void gpio_line_set(int line, int value) 59static void gpio_line_set(int line, int value)
43{ 60{
44 unsigned long flags; 61 unsigned long flags;
62 u32 val;
45 63
46 local_irq_save(flags); 64 local_irq_save(flags);
65 val = readl(IOP3XX_GPOD);
47 if (value == GPIO_LOW) { 66 if (value == GPIO_LOW) {
48 *IOP3XX_GPOD &= ~(1 << line); 67 val &= ~BIT(line);
49 } else if (value == GPIO_HIGH) { 68 } else if (value == GPIO_HIGH) {
50 *IOP3XX_GPOD |= 1 << line; 69 val |= BIT(line);
51 } 70 }
71 writel(val, IOP3XX_GPOD);
52 local_irq_restore(flags); 72 local_irq_restore(flags);
53} 73}
54EXPORT_SYMBOL(gpio_line_set);
55 74
56static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) 75static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
57{ 76{
@@ -86,8 +105,26 @@ static struct gpio_chip iop3xx_chip = {
86 .ngpio = IOP3XX_N_GPIOS, 105 .ngpio = IOP3XX_N_GPIOS,
87}; 106};
88 107
89static int __init iop3xx_gpio_setup(void) 108static int iop3xx_gpio_probe(struct platform_device *pdev)
90{ 109{
110 struct resource *res;
111
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 base = devm_ioremap_resource(&pdev->dev, res);
114
91 return gpiochip_add(&iop3xx_chip); 115 return gpiochip_add(&iop3xx_chip);
92} 116}
93arch_initcall(iop3xx_gpio_setup); 117
118static struct platform_driver iop3xx_gpio_driver = {
119 .driver = {
120 .name = "gpio-iop",
121 .owner = THIS_MODULE,
122 },
123 .probe = iop3xx_gpio_probe,
124};
125
126static int __init iop3xx_gpio_init(void)
127{
128 return platform_driver_register(&iop3xx_gpio_driver);
129}
130arch_initcall(iop3xx_gpio_init);
diff --git a/drivers/gpio/gpio-langwell.c b/drivers/gpio/gpio-langwell.c
deleted file mode 100644
index bfa1af1b519f..000000000000
--- a/drivers/gpio/gpio-langwell.c
+++ /dev/null
@@ -1,397 +0,0 @@
1/*
2 * Moorestown platform Langwell chip GPIO driver
3 *
4 * Copyright (c) 2008, 2009, 2013, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/* Supports:
21 * Moorestown platform Langwell chip.
22 * Medfield platform Penwell chip.
23 */
24
25#include <linux/module.h>
26#include <linux/pci.h>
27#include <linux/platform_device.h>
28#include <linux/kernel.h>
29#include <linux/delay.h>
30#include <linux/stddef.h>
31#include <linux/interrupt.h>
32#include <linux/init.h>
33#include <linux/irq.h>
34#include <linux/io.h>
35#include <linux/gpio.h>
36#include <linux/slab.h>
37#include <linux/pm_runtime.h>
38#include <linux/irqdomain.h>
39
40/*
41 * Langwell chip has 64 pins and thus there are 2 32bit registers to control
42 * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
43 * registers to control them, so we only define the order here instead of a
44 * structure, to get a bit offset for a pin (use GPDR as an example):
45 *
46 * nreg = ngpio / 32;
47 * reg = offset / 32;
48 * bit = offset % 32;
49 * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
50 *
51 * so the bit of reg_addr is to control pin offset's GPDR feature
52*/
53
54enum GPIO_REG {
55 GPLR = 0, /* pin level read-only */
56 GPDR, /* pin direction */
57 GPSR, /* pin set */
58 GPCR, /* pin clear */
59 GRER, /* rising edge detect */
60 GFER, /* falling edge detect */
61 GEDR, /* edge detect result */
62 GAFR, /* alt function */
63};
64
65struct lnw_gpio {
66 struct gpio_chip chip;
67 void __iomem *reg_base;
68 spinlock_t lock;
69 struct pci_dev *pdev;
70 struct irq_domain *domain;
71};
72
73#define to_lnw_priv(chip) container_of(chip, struct lnw_gpio, chip)
74
75static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
76 enum GPIO_REG reg_type)
77{
78 struct lnw_gpio *lnw = to_lnw_priv(chip);
79 unsigned nreg = chip->ngpio / 32;
80 u8 reg = offset / 32;
81
82 return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
83}
84
85static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
86 enum GPIO_REG reg_type)
87{
88 struct lnw_gpio *lnw = to_lnw_priv(chip);
89 unsigned nreg = chip->ngpio / 32;
90 u8 reg = offset / 16;
91
92 return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
93}
94
95static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset)
96{
97 void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
98 u32 value = readl(gafr);
99 int shift = (offset % 16) << 1, af = (value >> shift) & 3;
100
101 if (af) {
102 value &= ~(3 << shift);
103 writel(value, gafr);
104 }
105 return 0;
106}
107
108static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
109{
110 void __iomem *gplr = gpio_reg(chip, offset, GPLR);
111
112 return readl(gplr) & BIT(offset % 32);
113}
114
115static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
116{
117 void __iomem *gpsr, *gpcr;
118
119 if (value) {
120 gpsr = gpio_reg(chip, offset, GPSR);
121 writel(BIT(offset % 32), gpsr);
122 } else {
123 gpcr = gpio_reg(chip, offset, GPCR);
124 writel(BIT(offset % 32), gpcr);
125 }
126}
127
128static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
129{
130 struct lnw_gpio *lnw = to_lnw_priv(chip);
131 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
132 u32 value;
133 unsigned long flags;
134
135 if (lnw->pdev)
136 pm_runtime_get(&lnw->pdev->dev);
137
138 spin_lock_irqsave(&lnw->lock, flags);
139 value = readl(gpdr);
140 value &= ~BIT(offset % 32);
141 writel(value, gpdr);
142 spin_unlock_irqrestore(&lnw->lock, flags);
143
144 if (lnw->pdev)
145 pm_runtime_put(&lnw->pdev->dev);
146
147 return 0;
148}
149
150static int lnw_gpio_direction_output(struct gpio_chip *chip,
151 unsigned offset, int value)
152{
153 struct lnw_gpio *lnw = to_lnw_priv(chip);
154 void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
155 unsigned long flags;
156
157 lnw_gpio_set(chip, offset, value);
158
159 if (lnw->pdev)
160 pm_runtime_get(&lnw->pdev->dev);
161
162 spin_lock_irqsave(&lnw->lock, flags);
163 value = readl(gpdr);
164 value |= BIT(offset % 32);
165 writel(value, gpdr);
166 spin_unlock_irqrestore(&lnw->lock, flags);
167
168 if (lnw->pdev)
169 pm_runtime_put(&lnw->pdev->dev);
170
171 return 0;
172}
173
174static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
175{
176 struct lnw_gpio *lnw = to_lnw_priv(chip);
177 return irq_create_mapping(lnw->domain, offset);
178}
179
180static int lnw_irq_type(struct irq_data *d, unsigned type)
181{
182 struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
183 u32 gpio = irqd_to_hwirq(d);
184 unsigned long flags;
185 u32 value;
186 void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
187 void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
188
189 if (gpio >= lnw->chip.ngpio)
190 return -EINVAL;
191
192 if (lnw->pdev)
193 pm_runtime_get(&lnw->pdev->dev);
194
195 spin_lock_irqsave(&lnw->lock, flags);
196 if (type & IRQ_TYPE_EDGE_RISING)
197 value = readl(grer) | BIT(gpio % 32);
198 else
199 value = readl(grer) & (~BIT(gpio % 32));
200 writel(value, grer);
201
202 if (type & IRQ_TYPE_EDGE_FALLING)
203 value = readl(gfer) | BIT(gpio % 32);
204 else
205 value = readl(gfer) & (~BIT(gpio % 32));
206 writel(value, gfer);
207 spin_unlock_irqrestore(&lnw->lock, flags);
208
209 if (lnw->pdev)
210 pm_runtime_put(&lnw->pdev->dev);
211
212 return 0;
213}
214
215static void lnw_irq_unmask(struct irq_data *d)
216{
217}
218
219static void lnw_irq_mask(struct irq_data *d)
220{
221}
222
223static struct irq_chip lnw_irqchip = {
224 .name = "LNW-GPIO",
225 .irq_mask = lnw_irq_mask,
226 .irq_unmask = lnw_irq_unmask,
227 .irq_set_type = lnw_irq_type,
228};
229
230static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = { /* pin number */
231 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
232 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
233 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
234 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb), .driver_data = 96 },
235 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7), .driver_data = 96 },
236 { 0, }
237};
238MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
239
240static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
241{
242 struct irq_data *data = irq_desc_get_irq_data(desc);
243 struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
244 struct irq_chip *chip = irq_data_get_irq_chip(data);
245 u32 base, gpio, mask;
246 unsigned long pending;
247 void __iomem *gedr;
248
249 /* check GPIO controller to check which pin triggered the interrupt */
250 for (base = 0; base < lnw->chip.ngpio; base += 32) {
251 gedr = gpio_reg(&lnw->chip, base, GEDR);
252 while ((pending = readl(gedr))) {
253 gpio = __ffs(pending);
254 mask = BIT(gpio);
255 /* Clear before handling so we can't lose an edge */
256 writel(mask, gedr);
257 generic_handle_irq(irq_find_mapping(lnw->domain,
258 base + gpio));
259 }
260 }
261
262 chip->irq_eoi(data);
263}
264
265static void lnw_irq_init_hw(struct lnw_gpio *lnw)
266{
267 void __iomem *reg;
268 unsigned base;
269
270 for (base = 0; base < lnw->chip.ngpio; base += 32) {
271 /* Clear the rising-edge detect register */
272 reg = gpio_reg(&lnw->chip, base, GRER);
273 writel(0, reg);
274 /* Clear the falling-edge detect register */
275 reg = gpio_reg(&lnw->chip, base, GFER);
276 writel(0, reg);
277 /* Clear the edge detect status register */
278 reg = gpio_reg(&lnw->chip, base, GEDR);
279 writel(~0, reg);
280 }
281}
282
283static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
284 irq_hw_number_t hw)
285{
286 struct lnw_gpio *lnw = d->host_data;
287
288 irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
289 "demux");
290 irq_set_chip_data(virq, lnw);
291 irq_set_irq_type(virq, IRQ_TYPE_NONE);
292
293 return 0;
294}
295
296static const struct irq_domain_ops lnw_gpio_irq_ops = {
297 .map = lnw_gpio_irq_map,
298 .xlate = irq_domain_xlate_twocell,
299};
300
301static int lnw_gpio_runtime_idle(struct device *dev)
302{
303 pm_schedule_suspend(dev, 500);
304 return -EBUSY;
305}
306
307static const struct dev_pm_ops lnw_gpio_pm_ops = {
308 SET_RUNTIME_PM_OPS(NULL, NULL, lnw_gpio_runtime_idle)
309};
310
311static int lnw_gpio_probe(struct pci_dev *pdev,
312 const struct pci_device_id *id)
313{
314 void __iomem *base;
315 struct lnw_gpio *lnw;
316 u32 gpio_base;
317 u32 irq_base;
318 int retval;
319 int ngpio = id->driver_data;
320
321 retval = pcim_enable_device(pdev);
322 if (retval)
323 return retval;
324
325 retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
326 if (retval) {
327 dev_err(&pdev->dev, "I/O memory mapping error\n");
328 return retval;
329 }
330
331 base = pcim_iomap_table(pdev)[1];
332
333 irq_base = readl(base);
334 gpio_base = readl(sizeof(u32) + base);
335
336 /* release the IO mapping, since we already get the info from bar1 */
337 pcim_iounmap_regions(pdev, 1 << 1);
338
339 lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL);
340 if (!lnw) {
341 dev_err(&pdev->dev, "can't allocate chip data\n");
342 return -ENOMEM;
343 }
344
345 lnw->reg_base = pcim_iomap_table(pdev)[0];
346 lnw->chip.label = dev_name(&pdev->dev);
347 lnw->chip.request = lnw_gpio_request;
348 lnw->chip.direction_input = lnw_gpio_direction_input;
349 lnw->chip.direction_output = lnw_gpio_direction_output;
350 lnw->chip.get = lnw_gpio_get;
351 lnw->chip.set = lnw_gpio_set;
352 lnw->chip.to_irq = lnw_gpio_to_irq;
353 lnw->chip.base = gpio_base;
354 lnw->chip.ngpio = ngpio;
355 lnw->chip.can_sleep = 0;
356 lnw->pdev = pdev;
357
358 spin_lock_init(&lnw->lock);
359
360 lnw->domain = irq_domain_add_simple(pdev->dev.of_node, ngpio, irq_base,
361 &lnw_gpio_irq_ops, lnw);
362 if (!lnw->domain)
363 return -ENOMEM;
364
365 pci_set_drvdata(pdev, lnw);
366 retval = gpiochip_add(&lnw->chip);
367 if (retval) {
368 dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
369 return retval;
370 }
371
372 lnw_irq_init_hw(lnw);
373
374 irq_set_handler_data(pdev->irq, lnw);
375 irq_set_chained_handler(pdev->irq, lnw_irq_handler);
376
377 pm_runtime_put_noidle(&pdev->dev);
378 pm_runtime_allow(&pdev->dev);
379
380 return 0;
381}
382
383static struct pci_driver lnw_gpio_driver = {
384 .name = "langwell_gpio",
385 .id_table = lnw_gpio_ids,
386 .probe = lnw_gpio_probe,
387 .driver = {
388 .pm = &lnw_gpio_pm_ops,
389 },
390};
391
392static int __init lnw_gpio_init(void)
393{
394 return pci_register_driver(&lnw_gpio_driver);
395}
396
397device_initcall(lnw_gpio_init);
diff --git a/drivers/gpio/gpio-lpc32xx.c b/drivers/gpio/gpio-lpc32xx.c
index 90a80eb688a9..2d5555decf0c 100644
--- a/drivers/gpio/gpio-lpc32xx.c
+++ b/drivers/gpio/gpio-lpc32xx.c
@@ -21,6 +21,7 @@
21#include <linux/io.h> 21#include <linux/io.h>
22#include <linux/errno.h> 22#include <linux/errno.h>
23#include <linux/gpio.h> 23#include <linux/gpio.h>
24#include <linux/of.h>
24#include <linux/of_gpio.h> 25#include <linux/of_gpio.h>
25#include <linux/platform_device.h> 26#include <linux/platform_device.h>
26#include <linux/module.h> 27#include <linux/module.h>
diff --git a/drivers/gpio/gpio-lynxpoint.c b/drivers/gpio/gpio-lynxpoint.c
index 41b5913ddabe..a0804740a0b7 100644
--- a/drivers/gpio/gpio-lynxpoint.c
+++ b/drivers/gpio/gpio-lynxpoint.c
@@ -242,14 +242,13 @@ static int lp_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
242 return irq_create_mapping(lg->domain, offset); 242 return irq_create_mapping(lg->domain, offset);
243} 243}
244 244
245static void lp_gpio_irq_handler(unsigned irq, struct irq_desc *desc) 245static void lp_gpio_irq_handler(unsigned hwirq, struct irq_desc *desc)
246{ 246{
247 struct irq_data *data = irq_desc_get_irq_data(desc); 247 struct irq_data *data = irq_desc_get_irq_data(desc);
248 struct lp_gpio *lg = irq_data_get_irq_handler_data(data); 248 struct lp_gpio *lg = irq_data_get_irq_handler_data(data);
249 struct irq_chip *chip = irq_data_get_irq_chip(data); 249 struct irq_chip *chip = irq_data_get_irq_chip(data);
250 u32 base, pin, mask; 250 u32 base, pin, mask;
251 unsigned long reg, ena, pending; 251 unsigned long reg, ena, pending;
252 unsigned virq;
253 252
254 /* check from GPIO controller which pin triggered the interrupt */ 253 /* check from GPIO controller which pin triggered the interrupt */
255 for (base = 0; base < lg->chip.ngpio; base += 32) { 254 for (base = 0; base < lg->chip.ngpio; base += 32) {
@@ -257,12 +256,14 @@ static void lp_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
257 ena = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE); 256 ena = lp_gpio_reg(&lg->chip, base, LP_INT_ENABLE);
258 257
259 while ((pending = (inl(reg) & inl(ena)))) { 258 while ((pending = (inl(reg) & inl(ena)))) {
259 unsigned irq;
260
260 pin = __ffs(pending); 261 pin = __ffs(pending);
261 mask = BIT(pin); 262 mask = BIT(pin);
262 /* Clear before handling so we don't lose an edge */ 263 /* Clear before handling so we don't lose an edge */
263 outl(mask, reg); 264 outl(mask, reg);
264 virq = irq_find_mapping(lg->domain, base + pin); 265 irq = irq_find_mapping(lg->domain, base + pin);
265 generic_handle_irq(virq); 266 generic_handle_irq(irq);
266 } 267 }
267 } 268 }
268 chip->irq_eoi(data); 269 chip->irq_eoi(data);
@@ -325,15 +326,15 @@ static void lp_gpio_irq_init_hw(struct lp_gpio *lg)
325 } 326 }
326} 327}
327 328
328static int lp_gpio_irq_map(struct irq_domain *d, unsigned int virq, 329static int lp_gpio_irq_map(struct irq_domain *d, unsigned int irq,
329 irq_hw_number_t hw) 330 irq_hw_number_t hwirq)
330{ 331{
331 struct lp_gpio *lg = d->host_data; 332 struct lp_gpio *lg = d->host_data;
332 333
333 irq_set_chip_and_handler_name(virq, &lp_irqchip, handle_simple_irq, 334 irq_set_chip_and_handler_name(irq, &lp_irqchip, handle_simple_irq,
334 "demux"); 335 "demux");
335 irq_set_chip_data(virq, lg); 336 irq_set_chip_data(irq, lg);
336 irq_set_irq_type(virq, IRQ_TYPE_NONE); 337 irq_set_irq_type(irq, IRQ_TYPE_NONE);
337 338
338 return 0; 339 return 0;
339} 340}
diff --git a/drivers/gpio/gpio-mc33880.c b/drivers/gpio/gpio-mc33880.c
index 3fd2caa4a2e0..c0b7835f5136 100644
--- a/drivers/gpio/gpio-mc33880.c
+++ b/drivers/gpio/gpio-mc33880.c
@@ -142,7 +142,6 @@ static int mc33880_probe(struct spi_device *spi)
142 return ret; 142 return ret;
143 143
144exit_destroy: 144exit_destroy:
145 spi_set_drvdata(spi, NULL);
146 mutex_destroy(&mc->lock); 145 mutex_destroy(&mc->lock);
147 return ret; 146 return ret;
148} 147}
@@ -156,8 +155,6 @@ static int mc33880_remove(struct spi_device *spi)
156 if (mc == NULL) 155 if (mc == NULL)
157 return -ENODEV; 156 return -ENODEV;
158 157
159 spi_set_drvdata(spi, NULL);
160
161 ret = gpiochip_remove(&mc->chip); 158 ret = gpiochip_remove(&mc->chip);
162 if (!ret) 159 if (!ret)
163 mutex_destroy(&mc->lock); 160 mutex_destroy(&mc->lock);
diff --git a/drivers/gpio/gpio-mpc8xxx.c b/drivers/gpio/gpio-mpc8xxx.c
index a0b33a216d4a..b350649b9764 100644
--- a/drivers/gpio/gpio-mpc8xxx.c
+++ b/drivers/gpio/gpio-mpc8xxx.c
@@ -282,16 +282,16 @@ static struct irq_chip mpc8xxx_irq_chip = {
282 .irq_set_type = mpc8xxx_irq_set_type, 282 .irq_set_type = mpc8xxx_irq_set_type,
283}; 283};
284 284
285static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int virq, 285static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
286 irq_hw_number_t hw) 286 irq_hw_number_t hwirq)
287{ 287{
288 struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data; 288 struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
289 289
290 if (mpc8xxx_gc->of_dev_id_data) 290 if (mpc8xxx_gc->of_dev_id_data)
291 mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data; 291 mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data;
292 292
293 irq_set_chip_data(virq, h->host_data); 293 irq_set_chip_data(irq, h->host_data);
294 irq_set_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq); 294 irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq);
295 295
296 return 0; 296 return 0;
297} 297}
diff --git a/drivers/gpio/gpio-mxs.c b/drivers/gpio/gpio-mxs.c
index f8e6af20dfbf..532bcb336eff 100644
--- a/drivers/gpio/gpio-mxs.c
+++ b/drivers/gpio/gpio-mxs.c
@@ -254,7 +254,6 @@ static int mxs_gpio_probe(struct platform_device *pdev)
254 struct device_node *parent; 254 struct device_node *parent;
255 static void __iomem *base; 255 static void __iomem *base;
256 struct mxs_gpio_port *port; 256 struct mxs_gpio_port *port;
257 struct resource *iores = NULL;
258 int irq_base; 257 int irq_base;
259 int err; 258 int err;
260 259
@@ -262,16 +261,10 @@ static int mxs_gpio_probe(struct platform_device *pdev)
262 if (!port) 261 if (!port)
263 return -ENOMEM; 262 return -ENOMEM;
264 263
265 if (np) { 264 port->id = of_alias_get_id(np, "gpio");
266 port->id = of_alias_get_id(np, "gpio"); 265 if (port->id < 0)
267 if (port->id < 0) 266 return port->id;
268 return port->id; 267 port->devid = (enum mxs_gpio_id) of_id->data;
269 port->devid = (enum mxs_gpio_id) of_id->data;
270 } else {
271 port->id = pdev->id;
272 port->devid = pdev->id_entry->driver_data;
273 }
274
275 port->irq = platform_get_irq(pdev, 0); 268 port->irq = platform_get_irq(pdev, 0);
276 if (port->irq < 0) 269 if (port->irq < 0)
277 return port->irq; 270 return port->irq;
@@ -281,18 +274,11 @@ static int mxs_gpio_probe(struct platform_device *pdev)
281 * share the same one 274 * share the same one
282 */ 275 */
283 if (!base) { 276 if (!base) {
284 if (np) { 277 parent = of_get_parent(np);
285 parent = of_get_parent(np); 278 base = of_iomap(parent, 0);
286 base = of_iomap(parent, 0); 279 of_node_put(parent);
287 of_node_put(parent); 280 if (!base)
288 if (!base) 281 return -EADDRNOTAVAIL;
289 return -EADDRNOTAVAIL;
290 } else {
291 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
292 base = devm_ioremap_resource(&pdev->dev, iores);
293 if (IS_ERR(base))
294 return PTR_ERR(base);
295 }
296 } 282 }
297 port->base = base; 283 port->base = base;
298 284
diff --git a/drivers/gpio/gpio-omap.c b/drivers/gpio/gpio-omap.c
index 89675f862308..f319c9ffd4a8 100644
--- a/drivers/gpio/gpio-omap.c
+++ b/drivers/gpio/gpio-omap.c
@@ -514,6 +514,14 @@ static int gpio_irq_type(struct irq_data *d, unsigned type)
514 return -EINVAL; 514 return -EINVAL;
515 } 515 }
516 516
517 retval = gpio_lock_as_irq(&bank->chip, offset);
518 if (retval) {
519 dev_err(bank->dev, "unable to lock offset %d for IRQ\n",
520 offset);
521 spin_unlock_irqrestore(&bank->lock, flags);
522 return retval;
523 }
524
517 bank->irq_usage |= 1 << GPIO_INDEX(bank, gpio); 525 bank->irq_usage |= 1 << GPIO_INDEX(bank, gpio);
518 spin_unlock_irqrestore(&bank->lock, flags); 526 spin_unlock_irqrestore(&bank->lock, flags);
519 527
@@ -797,6 +805,7 @@ static void gpio_irq_shutdown(struct irq_data *d)
797 unsigned offset = GPIO_INDEX(bank, gpio); 805 unsigned offset = GPIO_INDEX(bank, gpio);
798 806
799 spin_lock_irqsave(&bank->lock, flags); 807 spin_lock_irqsave(&bank->lock, flags);
808 gpio_unlock_as_irq(&bank->chip, offset);
800 bank->irq_usage &= ~(1 << offset); 809 bank->irq_usage &= ~(1 << offset);
801 _disable_gpio_module(bank, offset); 810 _disable_gpio_module(bank, offset);
802 _reset_gpio(bank, gpio); 811 _reset_gpio(bank, gpio);
@@ -957,22 +966,13 @@ static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
957{ 966{
958 struct gpio_bank *bank; 967 struct gpio_bank *bank;
959 unsigned long flags; 968 unsigned long flags;
960 int retval = 0;
961 969
962 bank = container_of(chip, struct gpio_bank, chip); 970 bank = container_of(chip, struct gpio_bank, chip);
963 spin_lock_irqsave(&bank->lock, flags); 971 spin_lock_irqsave(&bank->lock, flags);
964
965 if (LINE_USED(bank->irq_usage, offset)) {
966 retval = -EINVAL;
967 goto exit;
968 }
969
970 bank->set_dataout(bank, offset, value); 972 bank->set_dataout(bank, offset, value);
971 _set_gpio_direction(bank, offset, 0); 973 _set_gpio_direction(bank, offset, 0);
972
973exit:
974 spin_unlock_irqrestore(&bank->lock, flags); 974 spin_unlock_irqrestore(&bank->lock, flags);
975 return retval; 975 return 0;
976} 976}
977 977
978static int gpio_debounce(struct gpio_chip *chip, unsigned offset, 978static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
diff --git a/drivers/gpio/gpio-palmas.c b/drivers/gpio/gpio-palmas.c
index 8588af0f7661..11801e986dd9 100644
--- a/drivers/gpio/gpio-palmas.c
+++ b/drivers/gpio/gpio-palmas.c
@@ -31,6 +31,10 @@ struct palmas_gpio {
31 struct palmas *palmas; 31 struct palmas *palmas;
32}; 32};
33 33
34struct palmas_device_data {
35 int ngpio;
36};
37
34static inline struct palmas_gpio *to_palmas_gpio(struct gpio_chip *chip) 38static inline struct palmas_gpio *to_palmas_gpio(struct gpio_chip *chip)
35{ 39{
36 return container_of(chip, struct palmas_gpio, gpio_chip); 40 return container_of(chip, struct palmas_gpio, gpio_chip);
@@ -42,23 +46,26 @@ static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
42 struct palmas *palmas = pg->palmas; 46 struct palmas *palmas = pg->palmas;
43 unsigned int val; 47 unsigned int val;
44 int ret; 48 int ret;
49 unsigned int reg;
50 int gpio16 = (offset/8);
51
52 offset %= 8;
53 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
45 54
46 ret = palmas_read(palmas, PALMAS_GPIO_BASE, PALMAS_GPIO_DATA_DIR, &val); 55 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
47 if (ret < 0) { 56 if (ret < 0) {
48 dev_err(gc->dev, "GPIO_DATA_DIR read failed, err = %d\n", ret); 57 dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret);
49 return ret; 58 return ret;
50 } 59 }
51 60
52 if (val & (1 << offset)) { 61 if (val & BIT(offset))
53 ret = palmas_read(palmas, PALMAS_GPIO_BASE, 62 reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
54 PALMAS_GPIO_DATA_OUT, &val); 63 else
55 } else { 64 reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
56 ret = palmas_read(palmas, PALMAS_GPIO_BASE, 65
57 PALMAS_GPIO_DATA_IN, &val); 66 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
58 }
59 if (ret < 0) { 67 if (ret < 0) {
60 dev_err(gc->dev, "GPIO_DATA_IN/OUT read failed, err = %d\n", 68 dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret);
61 ret);
62 return ret; 69 return ret;
63 } 70 }
64 return !!(val & BIT(offset)); 71 return !!(val & BIT(offset));
@@ -70,17 +77,20 @@ static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
70 struct palmas_gpio *pg = to_palmas_gpio(gc); 77 struct palmas_gpio *pg = to_palmas_gpio(gc);
71 struct palmas *palmas = pg->palmas; 78 struct palmas *palmas = pg->palmas;
72 int ret; 79 int ret;
80 unsigned int reg;
81 int gpio16 = (offset/8);
73 82
74 if (value) 83 offset %= 8;
75 ret = palmas_write(palmas, PALMAS_GPIO_BASE, 84 if (gpio16)
76 PALMAS_GPIO_SET_DATA_OUT, BIT(offset)); 85 reg = (value) ?
86 PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
77 else 87 else
78 ret = palmas_write(palmas, PALMAS_GPIO_BASE, 88 reg = (value) ?
79 PALMAS_GPIO_CLEAR_DATA_OUT, BIT(offset)); 89 PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
90
91 ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
80 if (ret < 0) 92 if (ret < 0)
81 dev_err(gc->dev, "%s write failed, err = %d\n", 93 dev_err(gc->dev, "Reg 0x%02x write failed, %d\n", reg, ret);
82 (value) ? "GPIO_SET_DATA_OUT" : "GPIO_CLEAR_DATA_OUT",
83 ret);
84} 94}
85 95
86static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset, 96static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
@@ -89,14 +99,19 @@ static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
89 struct palmas_gpio *pg = to_palmas_gpio(gc); 99 struct palmas_gpio *pg = to_palmas_gpio(gc);
90 struct palmas *palmas = pg->palmas; 100 struct palmas *palmas = pg->palmas;
91 int ret; 101 int ret;
102 unsigned int reg;
103 int gpio16 = (offset/8);
104
105 offset %= 8;
106 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
92 107
93 /* Set the initial value */ 108 /* Set the initial value */
94 palmas_gpio_set(gc, offset, value); 109 palmas_gpio_set(gc, offset, value);
95 110
96 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, 111 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
97 PALMAS_GPIO_DATA_DIR, BIT(offset), BIT(offset)); 112 BIT(offset), BIT(offset));
98 if (ret < 0) 113 if (ret < 0)
99 dev_err(gc->dev, "GPIO_DATA_DIR write failed, err = %d\n", ret); 114 dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret);
100 return ret; 115 return ret;
101} 116}
102 117
@@ -105,11 +120,15 @@ static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
105 struct palmas_gpio *pg = to_palmas_gpio(gc); 120 struct palmas_gpio *pg = to_palmas_gpio(gc);
106 struct palmas *palmas = pg->palmas; 121 struct palmas *palmas = pg->palmas;
107 int ret; 122 int ret;
123 unsigned int reg;
124 int gpio16 = (offset/8);
125
126 offset %= 8;
127 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
108 128
109 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, 129 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
110 PALMAS_GPIO_DATA_DIR, BIT(offset), 0);
111 if (ret < 0) 130 if (ret < 0)
112 dev_err(gc->dev, "GPIO_DATA_DIR write failed, err = %d\n", ret); 131 dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret);
113 return ret; 132 return ret;
114} 133}
115 134
@@ -121,12 +140,36 @@ static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
121 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset); 140 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
122} 141}
123 142
143static const struct palmas_device_data palmas_dev_data = {
144 .ngpio = 8,
145};
146
147static const struct palmas_device_data tps80036_dev_data = {
148 .ngpio = 16,
149};
150
151static struct of_device_id of_palmas_gpio_match[] = {
152 { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
153 { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
154 { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
155 { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
156 { },
157};
158MODULE_DEVICE_TABLE(of, of_palmas_gpio_match);
159
124static int palmas_gpio_probe(struct platform_device *pdev) 160static int palmas_gpio_probe(struct platform_device *pdev)
125{ 161{
126 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); 162 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
127 struct palmas_platform_data *palmas_pdata; 163 struct palmas_platform_data *palmas_pdata;
128 struct palmas_gpio *palmas_gpio; 164 struct palmas_gpio *palmas_gpio;
129 int ret; 165 int ret;
166 const struct of_device_id *match;
167 const struct palmas_device_data *dev_data;
168
169 match = of_match_device(of_palmas_gpio_match, &pdev->dev);
170 dev_data = match->data;
171 if (!dev_data)
172 dev_data = &palmas_dev_data;
130 173
131 palmas_gpio = devm_kzalloc(&pdev->dev, 174 palmas_gpio = devm_kzalloc(&pdev->dev,
132 sizeof(*palmas_gpio), GFP_KERNEL); 175 sizeof(*palmas_gpio), GFP_KERNEL);
@@ -138,7 +181,7 @@ static int palmas_gpio_probe(struct platform_device *pdev)
138 palmas_gpio->palmas = palmas; 181 palmas_gpio->palmas = palmas;
139 palmas_gpio->gpio_chip.owner = THIS_MODULE; 182 palmas_gpio->gpio_chip.owner = THIS_MODULE;
140 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev); 183 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
141 palmas_gpio->gpio_chip.ngpio = 8; 184 palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
142 palmas_gpio->gpio_chip.can_sleep = 1; 185 palmas_gpio->gpio_chip.can_sleep = 1;
143 palmas_gpio->gpio_chip.direction_input = palmas_gpio_input; 186 palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
144 palmas_gpio->gpio_chip.direction_output = palmas_gpio_output; 187 palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
@@ -172,15 +215,6 @@ static int palmas_gpio_remove(struct platform_device *pdev)
172 return gpiochip_remove(&palmas_gpio->gpio_chip); 215 return gpiochip_remove(&palmas_gpio->gpio_chip);
173} 216}
174 217
175static struct of_device_id of_palmas_gpio_match[] = {
176 { .compatible = "ti,palmas-gpio"},
177 { .compatible = "ti,tps65913-gpio"},
178 { .compatible = "ti,tps65914-gpio"},
179 { .compatible = "ti,tps80036-gpio"},
180 { },
181};
182MODULE_DEVICE_TABLE(of, of_palmas_gpio_match);
183
184static struct platform_driver palmas_gpio_driver = { 218static struct platform_driver palmas_gpio_driver = {
185 .driver.name = "palmas-gpio", 219 .driver.name = "palmas-gpio",
186 .driver.owner = THIS_MODULE, 220 .driver.owner = THIS_MODULE,
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index cdd1aa12b895..6e48c07e3d8c 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -683,17 +683,6 @@ static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
683 int ret; 683 int ret;
684 u8 val[MAX_BANK]; 684 u8 val[MAX_BANK];
685 685
686 /* Let every port in proper state, that could save power */
687 memset(val, 0, NBANK(chip));
688 pca953x_write_regs(chip, PCA957X_PUPD, val);
689 memset(val, 0xFF, NBANK(chip));
690 pca953x_write_regs(chip, PCA957X_CFG, val);
691 memset(val, 0, NBANK(chip));
692 pca953x_write_regs(chip, PCA957X_OUT, val);
693
694 ret = pca953x_read_regs(chip, PCA957X_IN, val);
695 if (ret)
696 goto out;
697 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output); 686 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
698 if (ret) 687 if (ret)
699 goto out; 688 goto out;
diff --git a/drivers/gpio/gpio-pcf857x.c b/drivers/gpio/gpio-pcf857x.c
index 9e61bb0719d0..1535686e74ea 100644
--- a/drivers/gpio/gpio-pcf857x.c
+++ b/drivers/gpio/gpio-pcf857x.c
@@ -26,9 +26,10 @@
26#include <linux/irqdomain.h> 26#include <linux/irqdomain.h>
27#include <linux/kernel.h> 27#include <linux/kernel.h>
28#include <linux/module.h> 28#include <linux/module.h>
29#include <linux/of.h>
30#include <linux/of_device.h>
29#include <linux/slab.h> 31#include <linux/slab.h>
30#include <linux/spinlock.h> 32#include <linux/spinlock.h>
31#include <linux/workqueue.h>
32 33
33 34
34static const struct i2c_device_id pcf857x_id[] = { 35static const struct i2c_device_id pcf857x_id[] = {
@@ -50,6 +51,27 @@ static const struct i2c_device_id pcf857x_id[] = {
50}; 51};
51MODULE_DEVICE_TABLE(i2c, pcf857x_id); 52MODULE_DEVICE_TABLE(i2c, pcf857x_id);
52 53
54#ifdef CONFIG_OF
55static const struct of_device_id pcf857x_of_table[] = {
56 { .compatible = "nxp,pcf8574" },
57 { .compatible = "nxp,pcf8574a" },
58 { .compatible = "nxp,pca8574" },
59 { .compatible = "nxp,pca9670" },
60 { .compatible = "nxp,pca9672" },
61 { .compatible = "nxp,pca9674" },
62 { .compatible = "nxp,pcf8575" },
63 { .compatible = "nxp,pca8575" },
64 { .compatible = "nxp,pca9671" },
65 { .compatible = "nxp,pca9673" },
66 { .compatible = "nxp,pca9675" },
67 { .compatible = "maxim,max7328" },
68 { .compatible = "maxim,max7329" },
69 { .compatible = "ti,tca9554" },
70 { }
71};
72MODULE_DEVICE_TABLE(of, pcf857x_of_table);
73#endif
74
53/* 75/*
54 * The pcf857x, pca857x, and pca967x chips only expose one read and one 76 * The pcf857x, pca857x, and pca967x chips only expose one read and one
55 * write register. Writing a "one" bit (to match the reset state) lets 77 * write register. Writing a "one" bit (to match the reset state) lets
@@ -66,12 +88,11 @@ struct pcf857x {
66 struct gpio_chip chip; 88 struct gpio_chip chip;
67 struct i2c_client *client; 89 struct i2c_client *client;
68 struct mutex lock; /* protect 'out' */ 90 struct mutex lock; /* protect 'out' */
69 struct work_struct work; /* irq demux work */
70 struct irq_domain *irq_domain; /* for irq demux */ 91 struct irq_domain *irq_domain; /* for irq demux */
71 spinlock_t slock; /* protect irq demux */ 92 spinlock_t slock; /* protect irq demux */
72 unsigned out; /* software latch */ 93 unsigned out; /* software latch */
73 unsigned status; /* current status */ 94 unsigned status; /* current status */
74 int irq; /* real irq number */ 95 unsigned irq_mapped; /* mapped gpio irqs */
75 96
76 int (*write)(struct i2c_client *client, unsigned data); 97 int (*write)(struct i2c_client *client, unsigned data);
77 int (*read)(struct i2c_client *client); 98 int (*read)(struct i2c_client *client);
@@ -164,48 +185,54 @@ static void pcf857x_set(struct gpio_chip *chip, unsigned offset, int value)
164static int pcf857x_to_irq(struct gpio_chip *chip, unsigned offset) 185static int pcf857x_to_irq(struct gpio_chip *chip, unsigned offset)
165{ 186{
166 struct pcf857x *gpio = container_of(chip, struct pcf857x, chip); 187 struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
188 int ret;
167 189
168 return irq_create_mapping(gpio->irq_domain, offset); 190 ret = irq_create_mapping(gpio->irq_domain, offset);
191 if (ret > 0)
192 gpio->irq_mapped |= (1 << offset);
193
194 return ret;
169} 195}
170 196
171static void pcf857x_irq_demux_work(struct work_struct *work) 197static irqreturn_t pcf857x_irq(int irq, void *data)
172{ 198{
173 struct pcf857x *gpio = container_of(work, 199 struct pcf857x *gpio = data;
174 struct pcf857x,
175 work);
176 unsigned long change, i, status, flags; 200 unsigned long change, i, status, flags;
177 201
178 status = gpio->read(gpio->client); 202 status = gpio->read(gpio->client);
179 203
180 spin_lock_irqsave(&gpio->slock, flags); 204 spin_lock_irqsave(&gpio->slock, flags);
181 205
182 change = gpio->status ^ status; 206 /*
207 * call the interrupt handler iff gpio is used as
208 * interrupt source, just to avoid bad irqs
209 */
210
211 change = ((gpio->status ^ status) & gpio->irq_mapped);
183 for_each_set_bit(i, &change, gpio->chip.ngpio) 212 for_each_set_bit(i, &change, gpio->chip.ngpio)
184 generic_handle_irq(irq_find_mapping(gpio->irq_domain, i)); 213 generic_handle_irq(irq_find_mapping(gpio->irq_domain, i));
185 gpio->status = status; 214 gpio->status = status;
186 215
187 spin_unlock_irqrestore(&gpio->slock, flags); 216 spin_unlock_irqrestore(&gpio->slock, flags);
188}
189
190static irqreturn_t pcf857x_irq_demux(int irq, void *data)
191{
192 struct pcf857x *gpio = data;
193
194 /*
195 * pcf857x can't read/write data here,
196 * since i2c data access might go to sleep.
197 */
198 schedule_work(&gpio->work);
199 217
200 return IRQ_HANDLED; 218 return IRQ_HANDLED;
201} 219}
202 220
203static int pcf857x_irq_domain_map(struct irq_domain *domain, unsigned int virq, 221static int pcf857x_irq_domain_map(struct irq_domain *domain, unsigned int irq,
204 irq_hw_number_t hw) 222 irq_hw_number_t hw)
205{ 223{
206 irq_set_chip_and_handler(virq, 224 struct pcf857x *gpio = domain->host_data;
225
226 irq_set_chip_and_handler(irq,
207 &dummy_irq_chip, 227 &dummy_irq_chip,
208 handle_level_irq); 228 handle_level_irq);
229#ifdef CONFIG_ARM
230 set_irq_flags(irq, IRQF_VALID);
231#else
232 irq_set_noprobe(irq);
233#endif
234 gpio->irq_mapped |= (1 << hw);
235
209 return 0; 236 return 0;
210} 237}
211 238
@@ -218,8 +245,6 @@ static void pcf857x_irq_domain_cleanup(struct pcf857x *gpio)
218 if (gpio->irq_domain) 245 if (gpio->irq_domain)
219 irq_domain_remove(gpio->irq_domain); 246 irq_domain_remove(gpio->irq_domain);
220 247
221 if (gpio->irq)
222 free_irq(gpio->irq, gpio);
223} 248}
224 249
225static int pcf857x_irq_domain_init(struct pcf857x *gpio, 250static int pcf857x_irq_domain_init(struct pcf857x *gpio,
@@ -230,20 +255,21 @@ static int pcf857x_irq_domain_init(struct pcf857x *gpio,
230 gpio->irq_domain = irq_domain_add_linear(client->dev.of_node, 255 gpio->irq_domain = irq_domain_add_linear(client->dev.of_node,
231 gpio->chip.ngpio, 256 gpio->chip.ngpio,
232 &pcf857x_irq_domain_ops, 257 &pcf857x_irq_domain_ops,
233 NULL); 258 gpio);
234 if (!gpio->irq_domain) 259 if (!gpio->irq_domain)
235 goto fail; 260 goto fail;
236 261
237 /* enable real irq */ 262 /* enable real irq */
238 status = request_irq(client->irq, pcf857x_irq_demux, 0, 263 status = devm_request_threaded_irq(&client->dev, client->irq,
239 dev_name(&client->dev), gpio); 264 NULL, pcf857x_irq, IRQF_ONESHOT |
265 IRQF_TRIGGER_FALLING,
266 dev_name(&client->dev), gpio);
267
240 if (status) 268 if (status)
241 goto fail; 269 goto fail;
242 270
243 /* enable gpio_to_irq() */ 271 /* enable gpio_to_irq() */
244 INIT_WORK(&gpio->work, pcf857x_irq_demux_work);
245 gpio->chip.to_irq = pcf857x_to_irq; 272 gpio->chip.to_irq = pcf857x_to_irq;
246 gpio->irq = client->irq;
247 273
248 return 0; 274 return 0;
249 275
@@ -257,14 +283,18 @@ fail:
257static int pcf857x_probe(struct i2c_client *client, 283static int pcf857x_probe(struct i2c_client *client,
258 const struct i2c_device_id *id) 284 const struct i2c_device_id *id)
259{ 285{
260 struct pcf857x_platform_data *pdata; 286 struct pcf857x_platform_data *pdata = dev_get_platdata(&client->dev);
287 struct device_node *np = client->dev.of_node;
261 struct pcf857x *gpio; 288 struct pcf857x *gpio;
289 unsigned int n_latch = 0;
262 int status; 290 int status;
263 291
264 pdata = dev_get_platdata(&client->dev); 292 if (IS_ENABLED(CONFIG_OF) && np)
265 if (!pdata) { 293 of_property_read_u32(np, "lines-initial-states", &n_latch);
294 else if (pdata)
295 n_latch = pdata->n_latch;
296 else
266 dev_dbg(&client->dev, "no platform data\n"); 297 dev_dbg(&client->dev, "no platform data\n");
267 }
268 298
269 /* Allocate, initialize, and register this gpio_chip. */ 299 /* Allocate, initialize, and register this gpio_chip. */
270 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL); 300 gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
@@ -357,11 +387,11 @@ static int pcf857x_probe(struct i2c_client *client,
357 * may cause transient glitching since it can't know the last value 387 * may cause transient glitching since it can't know the last value
358 * written (some pins may need to be driven low). 388 * written (some pins may need to be driven low).
359 * 389 *
360 * Using pdata->n_latch avoids that trouble. When left initialized 390 * Using n_latch avoids that trouble. When left initialized to zero,
361 * to zero, our software copy of the "latch" then matches the chip's 391 * our software copy of the "latch" then matches the chip's all-ones
362 * all-ones reset state. Otherwise it flags pins to be driven low. 392 * reset state. Otherwise it flags pins to be driven low.
363 */ 393 */
364 gpio->out = pdata ? ~pdata->n_latch : ~0; 394 gpio->out = ~n_latch;
365 gpio->status = gpio->out; 395 gpio->status = gpio->out;
366 396
367 status = gpiochip_add(&gpio->chip); 397 status = gpiochip_add(&gpio->chip);
@@ -423,6 +453,7 @@ static struct i2c_driver pcf857x_driver = {
423 .driver = { 453 .driver = {
424 .name = "pcf857x", 454 .name = "pcf857x",
425 .owner = THIS_MODULE, 455 .owner = THIS_MODULE,
456 .of_match_table = of_match_ptr(pcf857x_of_table),
426 }, 457 },
427 .probe = pcf857x_probe, 458 .probe = pcf857x_probe,
428 .remove = pcf857x_remove, 459 .remove = pcf857x_remove,
diff --git a/drivers/gpio/gpio-pl061.c b/drivers/gpio/gpio-pl061.c
index 4274e2e70ef8..f22f7f3e2e53 100644
--- a/drivers/gpio/gpio-pl061.c
+++ b/drivers/gpio/gpio-pl061.c
@@ -238,15 +238,15 @@ static struct irq_chip pl061_irqchip = {
238 .irq_set_type = pl061_irq_type, 238 .irq_set_type = pl061_irq_type,
239}; 239};
240 240
241static int pl061_irq_map(struct irq_domain *d, unsigned int virq, 241static int pl061_irq_map(struct irq_domain *d, unsigned int irq,
242 irq_hw_number_t hw) 242 irq_hw_number_t hwirq)
243{ 243{
244 struct pl061_gpio *chip = d->host_data; 244 struct pl061_gpio *chip = d->host_data;
245 245
246 irq_set_chip_and_handler_name(virq, &pl061_irqchip, handle_simple_irq, 246 irq_set_chip_and_handler_name(irq, &pl061_irqchip, handle_simple_irq,
247 "pl061"); 247 "pl061");
248 irq_set_chip_data(virq, chip); 248 irq_set_chip_data(irq, chip);
249 irq_set_irq_type(virq, IRQ_TYPE_NONE); 249 irq_set_irq_type(irq, IRQ_TYPE_NONE);
250 250
251 return 0; 251 return 0;
252} 252}
diff --git a/drivers/gpio/gpio-rcar.c b/drivers/gpio/gpio-rcar.c
index 6038966ab045..d3f15ae93bd3 100644
--- a/drivers/gpio/gpio-rcar.c
+++ b/drivers/gpio/gpio-rcar.c
@@ -22,6 +22,7 @@
22#include <linux/irq.h> 22#include <linux/irq.h>
23#include <linux/irqdomain.h> 23#include <linux/irqdomain.h>
24#include <linux/module.h> 24#include <linux/module.h>
25#include <linux/of.h>
25#include <linux/pinctrl/consumer.h> 26#include <linux/pinctrl/consumer.h>
26#include <linux/platform_data/gpio-rcar.h> 27#include <linux/platform_data/gpio-rcar.h>
27#include <linux/platform_device.h> 28#include <linux/platform_device.h>
@@ -266,16 +267,16 @@ static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
266 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset); 267 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
267} 268}
268 269
269static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int virq, 270static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
270 irq_hw_number_t hw) 271 irq_hw_number_t hwirq)
271{ 272{
272 struct gpio_rcar_priv *p = h->host_data; 273 struct gpio_rcar_priv *p = h->host_data;
273 274
274 dev_dbg(&p->pdev->dev, "map hw irq = %d, virq = %d\n", (int)hw, virq); 275 dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
275 276
276 irq_set_chip_data(virq, h->host_data); 277 irq_set_chip_data(irq, h->host_data);
277 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq); 278 irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
278 set_irq_flags(virq, IRQF_VALID); /* kill me now */ 279 set_irq_flags(irq, IRQF_VALID); /* kill me now */
279 return 0; 280 return 0;
280} 281}
281 282
diff --git a/drivers/gpio/gpio-stmpe.c b/drivers/gpio/gpio-stmpe.c
index b33bad1bb4df..2647e243d471 100644
--- a/drivers/gpio/gpio-stmpe.c
+++ b/drivers/gpio/gpio-stmpe.c
@@ -254,9 +254,10 @@ static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
254 while (stat) { 254 while (stat) {
255 int bit = __ffs(stat); 255 int bit = __ffs(stat);
256 int line = bank * 8 + bit; 256 int line = bank * 8 + bit;
257 int virq = irq_find_mapping(stmpe_gpio->domain, line); 257 int child_irq = irq_find_mapping(stmpe_gpio->domain,
258 line);
258 259
259 handle_nested_irq(virq); 260 handle_nested_irq(child_irq);
260 stat &= ~(1 << bit); 261 stat &= ~(1 << bit);
261 } 262 }
262 263
@@ -271,7 +272,7 @@ static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
271 return IRQ_HANDLED; 272 return IRQ_HANDLED;
272} 273}
273 274
274static int stmpe_gpio_irq_map(struct irq_domain *d, unsigned int virq, 275static int stmpe_gpio_irq_map(struct irq_domain *d, unsigned int irq,
275 irq_hw_number_t hwirq) 276 irq_hw_number_t hwirq)
276{ 277{
277 struct stmpe_gpio *stmpe_gpio = d->host_data; 278 struct stmpe_gpio *stmpe_gpio = d->host_data;
@@ -279,26 +280,26 @@ static int stmpe_gpio_irq_map(struct irq_domain *d, unsigned int virq,
279 if (!stmpe_gpio) 280 if (!stmpe_gpio)
280 return -EINVAL; 281 return -EINVAL;
281 282
282 irq_set_chip_data(hwirq, stmpe_gpio); 283 irq_set_chip_data(irq, stmpe_gpio);
283 irq_set_chip_and_handler(hwirq, &stmpe_gpio_irq_chip, 284 irq_set_chip_and_handler(irq, &stmpe_gpio_irq_chip,
284 handle_simple_irq); 285 handle_simple_irq);
285 irq_set_nested_thread(hwirq, 1); 286 irq_set_nested_thread(irq, 1);
286#ifdef CONFIG_ARM 287#ifdef CONFIG_ARM
287 set_irq_flags(hwirq, IRQF_VALID); 288 set_irq_flags(irq, IRQF_VALID);
288#else 289#else
289 irq_set_noprobe(hwirq); 290 irq_set_noprobe(irq);
290#endif 291#endif
291 292
292 return 0; 293 return 0;
293} 294}
294 295
295static void stmpe_gpio_irq_unmap(struct irq_domain *d, unsigned int virq) 296static void stmpe_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
296{ 297{
297#ifdef CONFIG_ARM 298#ifdef CONFIG_ARM
298 set_irq_flags(virq, 0); 299 set_irq_flags(irq, 0);
299#endif 300#endif
300 irq_set_chip_and_handler(virq, NULL, NULL); 301 irq_set_chip_and_handler(irq, NULL, NULL);
301 irq_set_chip_data(virq, NULL); 302 irq_set_chip_data(irq, NULL);
302} 303}
303 304
304static const struct irq_domain_ops stmpe_gpio_irq_simple_ops = { 305static const struct irq_domain_ops stmpe_gpio_irq_simple_ops = {
diff --git a/drivers/gpio/gpio-tc3589x.c b/drivers/gpio/gpio-tc3589x.c
index 4a5de273c230..ddb5fefaa715 100644
--- a/drivers/gpio/gpio-tc3589x.c
+++ b/drivers/gpio/gpio-tc3589x.c
@@ -96,27 +96,27 @@ static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
96} 96}
97 97
98/** 98/**
99 * tc3589x_gpio_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ 99 * tc3589x_gpio_irq_get_irq(): Map a hardware IRQ on a chip to a Linux IRQ
100 * 100 *
101 * @tc3589x_gpio: tc3589x_gpio_irq controller to operate on. 101 * @tc3589x_gpio: tc3589x_gpio_irq controller to operate on.
102 * @irq: index of the interrupt requested in the chip IRQs 102 * @irq: index of the hardware interrupt requested in the chip IRQs
103 * 103 *
104 * Useful for drivers to request their own IRQs. 104 * Useful for drivers to request their own IRQs.
105 */ 105 */
106static int tc3589x_gpio_irq_get_virq(struct tc3589x_gpio *tc3589x_gpio, 106static int tc3589x_gpio_irq_get_irq(struct tc3589x_gpio *tc3589x_gpio,
107 int irq) 107 int hwirq)
108{ 108{
109 if (!tc3589x_gpio) 109 if (!tc3589x_gpio)
110 return -EINVAL; 110 return -EINVAL;
111 111
112 return irq_create_mapping(tc3589x_gpio->domain, irq); 112 return irq_create_mapping(tc3589x_gpio->domain, hwirq);
113} 113}
114 114
115static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset) 115static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
116{ 116{
117 struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip); 117 struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
118 118
119 return tc3589x_gpio_irq_get_virq(tc3589x_gpio, offset); 119 return tc3589x_gpio_irq_get_irq(tc3589x_gpio, offset);
120} 120}
121 121
122static struct gpio_chip template_chip = { 122static struct gpio_chip template_chip = {
@@ -242,9 +242,9 @@ static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
242 while (stat) { 242 while (stat) {
243 int bit = __ffs(stat); 243 int bit = __ffs(stat);
244 int line = i * 8 + bit; 244 int line = i * 8 + bit;
245 int virq = tc3589x_gpio_irq_get_virq(tc3589x_gpio, line); 245 int irq = tc3589x_gpio_irq_get_irq(tc3589x_gpio, line);
246 246
247 handle_nested_irq(virq); 247 handle_nested_irq(irq);
248 stat &= ~(1 << bit); 248 stat &= ~(1 << bit);
249 } 249 }
250 250
@@ -254,31 +254,31 @@ static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
254 return IRQ_HANDLED; 254 return IRQ_HANDLED;
255} 255}
256 256
257static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int virq, 257static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
258 irq_hw_number_t hwirq) 258 irq_hw_number_t hwirq)
259{ 259{
260 struct tc3589x *tc3589x_gpio = d->host_data; 260 struct tc3589x *tc3589x_gpio = d->host_data;
261 261
262 irq_set_chip_data(virq, tc3589x_gpio); 262 irq_set_chip_data(irq, tc3589x_gpio);
263 irq_set_chip_and_handler(virq, &tc3589x_gpio_irq_chip, 263 irq_set_chip_and_handler(irq, &tc3589x_gpio_irq_chip,
264 handle_simple_irq); 264 handle_simple_irq);
265 irq_set_nested_thread(virq, 1); 265 irq_set_nested_thread(irq, 1);
266#ifdef CONFIG_ARM 266#ifdef CONFIG_ARM
267 set_irq_flags(virq, IRQF_VALID); 267 set_irq_flags(irq, IRQF_VALID);
268#else 268#else
269 irq_set_noprobe(virq); 269 irq_set_noprobe(irq);
270#endif 270#endif
271 271
272 return 0; 272 return 0;
273} 273}
274 274
275static void tc3589x_gpio_irq_unmap(struct irq_domain *d, unsigned int virq) 275static void tc3589x_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
276{ 276{
277#ifdef CONFIG_ARM 277#ifdef CONFIG_ARM
278 set_irq_flags(virq, 0); 278 set_irq_flags(irq, 0);
279#endif 279#endif
280 irq_set_chip_and_handler(virq, NULL, NULL); 280 irq_set_chip_and_handler(irq, NULL, NULL);
281 irq_set_chip_data(virq, NULL); 281 irq_set_chip_data(irq, NULL);
282} 282}
283 283
284static struct irq_domain_ops tc3589x_irq_ops = { 284static struct irq_domain_ops tc3589x_irq_ops = {
diff --git a/drivers/gpio/gpio-tegra.c b/drivers/gpio/gpio-tegra.c
index 9a62672f1bed..cfd3b9037bc7 100644
--- a/drivers/gpio/gpio-tegra.c
+++ b/drivers/gpio/gpio-tegra.c
@@ -75,6 +75,7 @@ struct tegra_gpio_bank {
75#endif 75#endif
76}; 76};
77 77
78static struct device *dev;
78static struct irq_domain *irq_domain; 79static struct irq_domain *irq_domain;
79static void __iomem *regs; 80static void __iomem *regs;
80static u32 tegra_gpio_bank_count; 81static u32 tegra_gpio_bank_count;
@@ -205,6 +206,7 @@ static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
205 int lvl_type; 206 int lvl_type;
206 int val; 207 int val;
207 unsigned long flags; 208 unsigned long flags;
209 int ret;
208 210
209 switch (type & IRQ_TYPE_SENSE_MASK) { 211 switch (type & IRQ_TYPE_SENSE_MASK) {
210 case IRQ_TYPE_EDGE_RISING: 212 case IRQ_TYPE_EDGE_RISING:
@@ -231,6 +233,12 @@ static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
231 return -EINVAL; 233 return -EINVAL;
232 } 234 }
233 235
236 ret = gpio_lock_as_irq(&tegra_gpio_chip, gpio);
237 if (ret) {
238 dev_err(dev, "unable to lock Tegra GPIO %d as IRQ\n", gpio);
239 return ret;
240 }
241
234 spin_lock_irqsave(&bank->lvl_lock[port], flags); 242 spin_lock_irqsave(&bank->lvl_lock[port], flags);
235 243
236 val = tegra_gpio_readl(GPIO_INT_LVL(gpio)); 244 val = tegra_gpio_readl(GPIO_INT_LVL(gpio));
@@ -251,6 +259,13 @@ static int tegra_gpio_irq_set_type(struct irq_data *d, unsigned int type)
251 return 0; 259 return 0;
252} 260}
253 261
262static void tegra_gpio_irq_shutdown(struct irq_data *d)
263{
264 int gpio = d->hwirq;
265
266 gpio_unlock_as_irq(&tegra_gpio_chip, gpio);
267}
268
254static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) 269static void tegra_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
255{ 270{
256 struct tegra_gpio_bank *bank; 271 struct tegra_gpio_bank *bank;
@@ -368,6 +383,7 @@ static struct irq_chip tegra_gpio_irq_chip = {
368 .irq_mask = tegra_gpio_irq_mask, 383 .irq_mask = tegra_gpio_irq_mask,
369 .irq_unmask = tegra_gpio_irq_unmask, 384 .irq_unmask = tegra_gpio_irq_unmask,
370 .irq_set_type = tegra_gpio_irq_set_type, 385 .irq_set_type = tegra_gpio_irq_set_type,
386 .irq_shutdown = tegra_gpio_irq_shutdown,
371#ifdef CONFIG_PM_SLEEP 387#ifdef CONFIG_PM_SLEEP
372 .irq_set_wake = tegra_gpio_irq_set_wake, 388 .irq_set_wake = tegra_gpio_irq_set_wake,
373#endif 389#endif
@@ -413,6 +429,8 @@ static int tegra_gpio_probe(struct platform_device *pdev)
413 int i; 429 int i;
414 int j; 430 int j;
415 431
432 dev = &pdev->dev;
433
416 match = of_match_device(tegra_gpio_of_match, &pdev->dev); 434 match = of_match_device(tegra_gpio_of_match, &pdev->dev);
417 if (!match) { 435 if (!match) {
418 dev_err(&pdev->dev, "Error: No device match found\n"); 436 dev_err(&pdev->dev, "Error: No device match found\n");
diff --git a/drivers/gpio/gpio-twl4030.c b/drivers/gpio/gpio-twl4030.c
index d8e4f6efcb29..0c7e891c8651 100644
--- a/drivers/gpio/gpio-twl4030.c
+++ b/drivers/gpio/gpio-twl4030.c
@@ -594,7 +594,7 @@ static struct platform_driver gpio_twl4030_driver = {
594 .driver = { 594 .driver = {
595 .name = "twl4030_gpio", 595 .name = "twl4030_gpio",
596 .owner = THIS_MODULE, 596 .owner = THIS_MODULE,
597 .of_match_table = of_match_ptr(twl_gpio_match), 597 .of_match_table = twl_gpio_match,
598 }, 598 },
599 .probe = gpio_twl4030_probe, 599 .probe = gpio_twl4030_probe,
600 .remove = gpio_twl4030_remove, 600 .remove = gpio_twl4030_remove,
diff --git a/drivers/gpio/gpiolib-acpi.c b/drivers/gpio/gpiolib-acpi.c
index 5c1ef2b3ef18..ae0ffdce8bd5 100644
--- a/drivers/gpio/gpiolib-acpi.c
+++ b/drivers/gpio/gpiolib-acpi.c
@@ -11,7 +11,7 @@
11 */ 11 */
12 12
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/gpio.h> 14#include <linux/gpio/consumer.h>
15#include <linux/export.h> 15#include <linux/export.h>
16#include <linux/acpi_gpio.h> 16#include <linux/acpi_gpio.h>
17#include <linux/acpi.h> 17#include <linux/acpi.h>
@@ -33,14 +33,15 @@ static int acpi_gpiochip_find(struct gpio_chip *gc, void *data)
33} 33}
34 34
35/** 35/**
36 * acpi_get_gpio() - Translate ACPI GPIO pin to GPIO number usable with GPIO API 36 * acpi_get_gpiod() - Translate ACPI GPIO pin to GPIO descriptor usable with GPIO API
37 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1") 37 * @path: ACPI GPIO controller full path name, (e.g. "\\_SB.GPO1")
38 * @pin: ACPI GPIO pin number (0-based, controller-relative) 38 * @pin: ACPI GPIO pin number (0-based, controller-relative)
39 * 39 *
40 * Returns GPIO number to use with Linux generic GPIO API, or errno error value 40 * Returns GPIO descriptor to use with Linux generic GPIO API, or ERR_PTR
41 * error value
41 */ 42 */
42 43
43int acpi_get_gpio(char *path, int pin) 44static struct gpio_desc *acpi_get_gpiod(char *path, int pin)
44{ 45{
45 struct gpio_chip *chip; 46 struct gpio_chip *chip;
46 acpi_handle handle; 47 acpi_handle handle;
@@ -48,18 +49,17 @@ int acpi_get_gpio(char *path, int pin)
48 49
49 status = acpi_get_handle(NULL, path, &handle); 50 status = acpi_get_handle(NULL, path, &handle);
50 if (ACPI_FAILURE(status)) 51 if (ACPI_FAILURE(status))
51 return -ENODEV; 52 return ERR_PTR(-ENODEV);
52 53
53 chip = gpiochip_find(handle, acpi_gpiochip_find); 54 chip = gpiochip_find(handle, acpi_gpiochip_find);
54 if (!chip) 55 if (!chip)
55 return -ENODEV; 56 return ERR_PTR(-ENODEV);
56 57
57 if (!gpio_is_valid(chip->base + pin)) 58 if (pin < 0 || pin > chip->ngpio)
58 return -EINVAL; 59 return ERR_PTR(-EINVAL);
59 60
60 return chip->base + pin; 61 return gpio_to_desc(chip->base + pin);
61} 62}
62EXPORT_SYMBOL_GPL(acpi_get_gpio);
63 63
64static irqreturn_t acpi_gpio_irq_handler(int irq, void *data) 64static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
65{ 65{
@@ -73,15 +73,8 @@ static irqreturn_t acpi_gpio_irq_handler(int irq, void *data)
73static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data) 73static irqreturn_t acpi_gpio_irq_handler_evt(int irq, void *data)
74{ 74{
75 struct acpi_gpio_evt_pin *evt_pin = data; 75 struct acpi_gpio_evt_pin *evt_pin = data;
76 struct acpi_object_list args;
77 union acpi_object arg;
78 76
79 arg.type = ACPI_TYPE_INTEGER; 77 acpi_execute_simple_method(evt_pin->evt_handle, NULL, evt_pin->pin);
80 arg.integer.value = evt_pin->pin;
81 args.count = 1;
82 args.pointer = &arg;
83
84 acpi_evaluate_object(evt_pin->evt_handle, NULL, &args, NULL);
85 78
86 return IRQ_HANDLED; 79 return IRQ_HANDLED;
87} 80}
@@ -201,10 +194,48 @@ void acpi_gpiochip_request_interrupts(struct gpio_chip *chip)
201} 194}
202EXPORT_SYMBOL(acpi_gpiochip_request_interrupts); 195EXPORT_SYMBOL(acpi_gpiochip_request_interrupts);
203 196
197/**
198 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
199 * @chip: gpio chip
200 *
201 * Free interrupts associated with the _EVT method for the given GPIO chip.
202 *
203 * The remaining ACPI event interrupts associated with the chip are freed
204 * automatically.
205 */
206void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
207{
208 acpi_handle handle;
209 acpi_status status;
210 struct list_head *evt_pins;
211 struct acpi_gpio_evt_pin *evt_pin, *ep;
212
213 if (!chip->dev || !chip->to_irq)
214 return;
215
216 handle = ACPI_HANDLE(chip->dev);
217 if (!handle)
218 return;
219
220 status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
221 if (ACPI_FAILURE(status))
222 return;
223
224 list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
225 devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
226 list_del(&evt_pin->node);
227 kfree(evt_pin);
228 }
229
230 acpi_detach_data(handle, acpi_gpio_evt_dh);
231 kfree(evt_pins);
232}
233EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
234
204struct acpi_gpio_lookup { 235struct acpi_gpio_lookup {
205 struct acpi_gpio_info info; 236 struct acpi_gpio_info info;
206 int index; 237 int index;
207 int gpio; 238 struct gpio_desc *desc;
208 int n; 239 int n;
209}; 240};
210 241
@@ -215,37 +246,39 @@ static int acpi_find_gpio(struct acpi_resource *ares, void *data)
215 if (ares->type != ACPI_RESOURCE_TYPE_GPIO) 246 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
216 return 1; 247 return 1;
217 248
218 if (lookup->n++ == lookup->index && lookup->gpio < 0) { 249 if (lookup->n++ == lookup->index && !lookup->desc) {
219 const struct acpi_resource_gpio *agpio = &ares->data.gpio; 250 const struct acpi_resource_gpio *agpio = &ares->data.gpio;
220 251
221 lookup->gpio = acpi_get_gpio(agpio->resource_source.string_ptr, 252 lookup->desc = acpi_get_gpiod(agpio->resource_source.string_ptr,
222 agpio->pin_table[0]); 253 agpio->pin_table[0]);
223 lookup->info.gpioint = 254 lookup->info.gpioint =
224 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT; 255 agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
256 lookup->info.active_low =
257 agpio->polarity == ACPI_ACTIVE_LOW;
225 } 258 }
226 259
227 return 1; 260 return 1;
228} 261}
229 262
230/** 263/**
231 * acpi_get_gpio_by_index() - get a GPIO number from device resources 264 * acpi_get_gpiod_by_index() - get a GPIO descriptor from device resources
232 * @dev: pointer to a device to get GPIO from 265 * @dev: pointer to a device to get GPIO from
233 * @index: index of GpioIo/GpioInt resource (starting from %0) 266 * @index: index of GpioIo/GpioInt resource (starting from %0)
234 * @info: info pointer to fill in (optional) 267 * @info: info pointer to fill in (optional)
235 * 268 *
236 * Function goes through ACPI resources for @dev and based on @index looks 269 * Function goes through ACPI resources for @dev and based on @index looks
237 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO number, 270 * up a GpioIo/GpioInt resource, translates it to the Linux GPIO descriptor,
238 * and returns it. @index matches GpioIo/GpioInt resources only so if there 271 * and returns it. @index matches GpioIo/GpioInt resources only so if there
239 * are total %3 GPIO resources, the index goes from %0 to %2. 272 * are total %3 GPIO resources, the index goes from %0 to %2.
240 * 273 *
241 * If the GPIO cannot be translated or there is an error, negative errno is 274 * If the GPIO cannot be translated or there is an error an ERR_PTR is
242 * returned. 275 * returned.
243 * 276 *
244 * Note: if the GPIO resource has multiple entries in the pin list, this 277 * Note: if the GPIO resource has multiple entries in the pin list, this
245 * function only returns the first. 278 * function only returns the first.
246 */ 279 */
247int acpi_get_gpio_by_index(struct device *dev, int index, 280struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
248 struct acpi_gpio_info *info) 281 struct acpi_gpio_info *info)
249{ 282{
250 struct acpi_gpio_lookup lookup; 283 struct acpi_gpio_lookup lookup;
251 struct list_head resource_list; 284 struct list_head resource_list;
@@ -254,65 +287,26 @@ int acpi_get_gpio_by_index(struct device *dev, int index,
254 int ret; 287 int ret;
255 288
256 if (!dev) 289 if (!dev)
257 return -EINVAL; 290 return ERR_PTR(-EINVAL);
258 291
259 handle = ACPI_HANDLE(dev); 292 handle = ACPI_HANDLE(dev);
260 if (!handle || acpi_bus_get_device(handle, &adev)) 293 if (!handle || acpi_bus_get_device(handle, &adev))
261 return -ENODEV; 294 return ERR_PTR(-ENODEV);
262 295
263 memset(&lookup, 0, sizeof(lookup)); 296 memset(&lookup, 0, sizeof(lookup));
264 lookup.index = index; 297 lookup.index = index;
265 lookup.gpio = -ENODEV;
266 298
267 INIT_LIST_HEAD(&resource_list); 299 INIT_LIST_HEAD(&resource_list);
268 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio, 300 ret = acpi_dev_get_resources(adev, &resource_list, acpi_find_gpio,
269 &lookup); 301 &lookup);
270 if (ret < 0) 302 if (ret < 0)
271 return ret; 303 return ERR_PTR(ret);
272 304
273 acpi_dev_free_resource_list(&resource_list); 305 acpi_dev_free_resource_list(&resource_list);
274 306
275 if (lookup.gpio >= 0 && info) 307 if (lookup.desc && info)
276 *info = lookup.info; 308 *info = lookup.info;
277 309
278 return lookup.gpio; 310 return lookup.desc ? lookup.desc : ERR_PTR(-ENODEV);
279} 311}
280EXPORT_SYMBOL_GPL(acpi_get_gpio_by_index); 312EXPORT_SYMBOL_GPL(acpi_get_gpiod_by_index);
281
282/**
283 * acpi_gpiochip_free_interrupts() - Free GPIO _EVT ACPI event interrupts.
284 * @chip: gpio chip
285 *
286 * Free interrupts associated with the _EVT method for the given GPIO chip.
287 *
288 * The remaining ACPI event interrupts associated with the chip are freed
289 * automatically.
290 */
291void acpi_gpiochip_free_interrupts(struct gpio_chip *chip)
292{
293 acpi_handle handle;
294 acpi_status status;
295 struct list_head *evt_pins;
296 struct acpi_gpio_evt_pin *evt_pin, *ep;
297
298 if (!chip->dev || !chip->to_irq)
299 return;
300
301 handle = ACPI_HANDLE(chip->dev);
302 if (!handle)
303 return;
304
305 status = acpi_get_data(handle, acpi_gpio_evt_dh, (void **)&evt_pins);
306 if (ACPI_FAILURE(status))
307 return;
308
309 list_for_each_entry_safe_reverse(evt_pin, ep, evt_pins, node) {
310 devm_free_irq(chip->dev, evt_pin->irq, evt_pin);
311 list_del(&evt_pin->node);
312 kfree(evt_pin);
313 }
314
315 acpi_detach_data(handle, acpi_gpio_evt_dh);
316 kfree(evt_pins);
317}
318EXPORT_SYMBOL(acpi_gpiochip_free_interrupts);
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index e78760921bd7..e0a98f581f58 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -15,19 +15,21 @@
15#include <linux/errno.h> 15#include <linux/errno.h>
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/io.h> 17#include <linux/io.h>
18#include <linux/gpio.h> 18#include <linux/gpio/consumer.h>
19#include <linux/of.h> 19#include <linux/of.h>
20#include <linux/of_address.h> 20#include <linux/of_address.h>
21#include <linux/of_gpio.h> 21#include <linux/of_gpio.h>
22#include <linux/pinctrl/pinctrl.h> 22#include <linux/pinctrl/pinctrl.h>
23#include <linux/slab.h> 23#include <linux/slab.h>
24 24
25struct gpio_desc;
26
25/* Private data structure for of_gpiochip_find_and_xlate */ 27/* Private data structure for of_gpiochip_find_and_xlate */
26struct gg_data { 28struct gg_data {
27 enum of_gpio_flags *flags; 29 enum of_gpio_flags *flags;
28 struct of_phandle_args gpiospec; 30 struct of_phandle_args gpiospec;
29 31
30 int out_gpio; 32 struct gpio_desc *out_gpio;
31}; 33};
32 34
33/* Private function for resolving node pointer to gpio_chip */ 35/* Private function for resolving node pointer to gpio_chip */
@@ -45,28 +47,31 @@ static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
45 if (ret < 0) 47 if (ret < 0)
46 return false; 48 return false;
47 49
48 gg_data->out_gpio = ret + gc->base; 50 gg_data->out_gpio = gpio_to_desc(ret + gc->base);
49 return true; 51 return true;
50} 52}
51 53
52/** 54/**
53 * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API 55 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
54 * @np: device node to get GPIO from 56 * @np: device node to get GPIO from
55 * @propname: property name containing gpio specifier(s) 57 * @propname: property name containing gpio specifier(s)
56 * @index: index of the GPIO 58 * @index: index of the GPIO
57 * @flags: a flags pointer to fill in 59 * @flags: a flags pointer to fill in
58 * 60 *
59 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 61 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
60 * value on the error condition. If @flags is not NULL the function also fills 62 * value on the error condition. If @flags is not NULL the function also fills
61 * in flags for the GPIO. 63 * in flags for the GPIO.
62 */ 64 */
63int of_get_named_gpio_flags(struct device_node *np, const char *propname, 65struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
64 int index, enum of_gpio_flags *flags) 66 const char *propname, int index, enum of_gpio_flags *flags)
65{ 67{
66 /* Return -EPROBE_DEFER to support probe() functions to be called 68 /* Return -EPROBE_DEFER to support probe() functions to be called
67 * later when the GPIO actually becomes available 69 * later when the GPIO actually becomes available
68 */ 70 */
69 struct gg_data gg_data = { .flags = flags, .out_gpio = -EPROBE_DEFER }; 71 struct gg_data gg_data = {
72 .flags = flags,
73 .out_gpio = ERR_PTR(-EPROBE_DEFER)
74 };
70 int ret; 75 int ret;
71 76
72 /* .of_xlate might decide to not fill in the flags, so clear it. */ 77 /* .of_xlate might decide to not fill in the flags, so clear it. */
@@ -78,16 +83,17 @@ int of_get_named_gpio_flags(struct device_node *np, const char *propname,
78 if (ret) { 83 if (ret) {
79 pr_debug("%s: can't parse gpios property of node '%s[%d]'\n", 84 pr_debug("%s: can't parse gpios property of node '%s[%d]'\n",
80 __func__, np->full_name, index); 85 __func__, np->full_name, index);
81 return ret; 86 return ERR_PTR(ret);
82 } 87 }
83 88
84 gpiochip_find(&gg_data, of_gpiochip_find_and_xlate); 89 gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
85 90
86 of_node_put(gg_data.gpiospec.np); 91 of_node_put(gg_data.gpiospec.np);
87 pr_debug("%s exited with status %d\n", __func__, gg_data.out_gpio); 92 pr_debug("%s exited with status %d\n", __func__,
93 PTR_RET(gg_data.out_gpio));
88 return gg_data.out_gpio; 94 return gg_data.out_gpio;
89} 95}
90EXPORT_SYMBOL(of_get_named_gpio_flags); 96EXPORT_SYMBOL(of_get_named_gpiod_flags);
91 97
92/** 98/**
93 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 99 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 4a34ca9c1768..7dd446150294 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -10,22 +10,18 @@
10#include <linux/seq_file.h> 10#include <linux/seq_file.h>
11#include <linux/gpio.h> 11#include <linux/gpio.h>
12#include <linux/of_gpio.h> 12#include <linux/of_gpio.h>
13#include <linux/acpi_gpio.h>
13#include <linux/idr.h> 14#include <linux/idr.h>
14#include <linux/slab.h> 15#include <linux/slab.h>
15 16
16#define CREATE_TRACE_POINTS 17#define CREATE_TRACE_POINTS
17#include <trace/events/gpio.h> 18#include <trace/events/gpio.h>
18 19
19/* Optional implementation infrastructure for GPIO interfaces. 20/* Implementation infrastructure for GPIO interfaces.
20 * 21 *
21 * Platforms may want to use this if they tend to use very many GPIOs 22 * The GPIO programming interface allows for inlining speed-critical
22 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. 23 * get/set operations for common cases, so that access to SOC-integrated
23 * 24 * GPIOs can sometimes cost only an instruction or two per bit.
24 * When kernel footprint or instruction count is an issue, simpler
25 * implementations may be preferred. The GPIO programming interface
26 * allows for inlining speed-critical get/set operations for common
27 * cases, so that access to SOC-integrated GPIOs can sometimes cost
28 * only an instruction or two per bit.
29 */ 25 */
30 26
31 27
@@ -57,9 +53,10 @@ struct gpio_desc {
57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 53#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */ 54#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */ 55#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */ 56#define FLAG_ACTIVE_LOW 6 /* value has active low */
61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 57#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 58#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
59#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
63 60
64#define ID_SHIFT 16 /* add new flags before this one */ 61#define ID_SHIFT 16 /* add new flags before this one */
65 62
@@ -74,34 +71,50 @@ static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74 71
75#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) 72#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76 73
74static DEFINE_MUTEX(gpio_lookup_lock);
75static LIST_HEAD(gpio_lookup_list);
77static LIST_HEAD(gpio_chips); 76static LIST_HEAD(gpio_chips);
78 77
79#ifdef CONFIG_GPIO_SYSFS 78#ifdef CONFIG_GPIO_SYSFS
80static DEFINE_IDR(dirent_idr); 79static DEFINE_IDR(dirent_idr);
81#endif 80#endif
82 81
83/*
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
86 */
87static int gpiod_request(struct gpio_desc *desc, const char *label); 82static int gpiod_request(struct gpio_desc *desc, const char *label);
88static void gpiod_free(struct gpio_desc *desc); 83static void gpiod_free(struct gpio_desc *desc);
89static int gpiod_direction_input(struct gpio_desc *desc);
90static int gpiod_direction_output(struct gpio_desc *desc, int value);
91static int gpiod_get_direction(const struct gpio_desc *desc);
92static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
93static int gpiod_get_value_cansleep(const struct gpio_desc *desc);
94static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
95static int gpiod_get_value(const struct gpio_desc *desc);
96static void gpiod_set_value(struct gpio_desc *desc, int value);
97static int gpiod_cansleep(const struct gpio_desc *desc);
98static int gpiod_to_irq(const struct gpio_desc *desc);
99static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
100static int gpiod_export_link(struct device *dev, const char *name,
101 struct gpio_desc *desc);
102static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
103static void gpiod_unexport(struct gpio_desc *desc);
104 84
85#ifdef CONFIG_DEBUG_FS
86#define gpiod_emerg(desc, fmt, ...) \
87 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
88 ##__VA_ARGS__)
89#define gpiod_crit(desc, fmt, ...) \
90 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
91 ##__VA_ARGS__)
92#define gpiod_err(desc, fmt, ...) \
93 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
94 ##__VA_ARGS__)
95#define gpiod_warn(desc, fmt, ...) \
96 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
97 ##__VA_ARGS__)
98#define gpiod_info(desc, fmt, ...) \
99 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
100 ##__VA_ARGS__)
101#define gpiod_dbg(desc, fmt, ...) \
102 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \
103 ##__VA_ARGS__)
104#else
105#define gpiod_emerg(desc, fmt, ...) \
106 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
107#define gpiod_crit(desc, fmt, ...) \
108 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109#define gpiod_err(desc, fmt, ...) \
110 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111#define gpiod_warn(desc, fmt, ...) \
112 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113#define gpiod_info(desc, fmt, ...) \
114 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115#define gpiod_dbg(desc, fmt, ...) \
116 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
117#endif
105 118
106static inline void desc_set_label(struct gpio_desc *d, const char *label) 119static inline void desc_set_label(struct gpio_desc *d, const char *label)
107{ 120{
@@ -121,23 +134,36 @@ static int gpio_chip_hwgpio(const struct gpio_desc *desc)
121/** 134/**
122 * Convert a GPIO number to its descriptor 135 * Convert a GPIO number to its descriptor
123 */ 136 */
124static struct gpio_desc *gpio_to_desc(unsigned gpio) 137struct gpio_desc *gpio_to_desc(unsigned gpio)
125{ 138{
126 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) 139 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
127 return NULL; 140 return NULL;
128 else 141 else
129 return &gpio_desc[gpio]; 142 return &gpio_desc[gpio];
130} 143}
144EXPORT_SYMBOL_GPL(gpio_to_desc);
145
146/**
147 * Convert an offset on a certain chip to a corresponding descriptor
148 */
149static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
150 unsigned int offset)
151{
152 unsigned int gpio = chip->base + offset;
153
154 return gpio_to_desc(gpio);
155}
131 156
132/** 157/**
133 * Convert a GPIO descriptor to the integer namespace. 158 * Convert a GPIO descriptor to the integer namespace.
134 * This should disappear in the future but is needed since we still 159 * This should disappear in the future but is needed since we still
135 * use GPIO numbers for error messages and sysfs nodes 160 * use GPIO numbers for error messages and sysfs nodes
136 */ 161 */
137static int desc_to_gpio(const struct gpio_desc *desc) 162int desc_to_gpio(const struct gpio_desc *desc)
138{ 163{
139 return desc - &gpio_desc[0]; 164 return desc - &gpio_desc[0];
140} 165}
166EXPORT_SYMBOL_GPL(desc_to_gpio);
141 167
142 168
143/* Warn when drivers omit gpio_request() calls -- legal but ill-advised 169/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
@@ -172,16 +198,15 @@ static int gpio_ensure_requested(struct gpio_desc *desc)
172 return 0; 198 return 0;
173} 199}
174 200
175static struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) 201/**
202 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
203 * @desc: descriptor to return the chip of
204 */
205struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
176{ 206{
177 return desc ? desc->chip : NULL; 207 return desc ? desc->chip : NULL;
178} 208}
179 209EXPORT_SYMBOL_GPL(gpiod_to_chip);
180/* caller holds gpio_lock *OR* gpio is marked as requested */
181struct gpio_chip *gpio_to_chip(unsigned gpio)
182{
183 return gpiod_to_chip(gpio_to_desc(gpio));
184}
185 210
186/* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 211/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
187static int gpiochip_find_base(int ngpio) 212static int gpiochip_find_base(int ngpio)
@@ -207,8 +232,15 @@ static int gpiochip_find_base(int ngpio)
207 } 232 }
208} 233}
209 234
210/* caller ensures gpio is valid and requested, chip->get_direction may sleep */ 235/**
211static int gpiod_get_direction(const struct gpio_desc *desc) 236 * gpiod_get_direction - return the current direction of a GPIO
237 * @desc: GPIO to get the direction of
238 *
239 * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error.
240 *
241 * This function may sleep if gpiod_cansleep() is true.
242 */
243int gpiod_get_direction(const struct gpio_desc *desc)
212{ 244{
213 struct gpio_chip *chip; 245 struct gpio_chip *chip;
214 unsigned offset; 246 unsigned offset;
@@ -234,6 +266,7 @@ static int gpiod_get_direction(const struct gpio_desc *desc)
234 } 266 }
235 return status; 267 return status;
236} 268}
269EXPORT_SYMBOL_GPL(gpiod_get_direction);
237 270
238#ifdef CONFIG_GPIO_SYSFS 271#ifdef CONFIG_GPIO_SYSFS
239 272
@@ -318,17 +351,10 @@ static ssize_t gpio_value_show(struct device *dev,
318 351
319 mutex_lock(&sysfs_lock); 352 mutex_lock(&sysfs_lock);
320 353
321 if (!test_bit(FLAG_EXPORT, &desc->flags)) { 354 if (!test_bit(FLAG_EXPORT, &desc->flags))
322 status = -EIO; 355 status = -EIO;
323 } else { 356 else
324 int value; 357 status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc));
325
326 value = !!gpiod_get_value_cansleep(desc);
327 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
328 value = !value;
329
330 status = sprintf(buf, "%d\n", value);
331 }
332 358
333 mutex_unlock(&sysfs_lock); 359 mutex_unlock(&sysfs_lock);
334 return status; 360 return status;
@@ -351,9 +377,7 @@ static ssize_t gpio_value_store(struct device *dev,
351 377
352 status = kstrtol(buf, 0, &value); 378 status = kstrtol(buf, 0, &value);
353 if (status == 0) { 379 if (status == 0) {
354 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 380 gpiod_set_value_cansleep(desc, value);
355 value = !value;
356 gpiod_set_value_cansleep(desc, value != 0);
357 status = size; 381 status = size;
358 } 382 }
359 } 383 }
@@ -395,6 +419,7 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
395 desc->flags &= ~GPIO_TRIGGER_MASK; 419 desc->flags &= ~GPIO_TRIGGER_MASK;
396 420
397 if (!gpio_flags) { 421 if (!gpio_flags) {
422 gpiod_unlock_as_irq(desc);
398 ret = 0; 423 ret = 0;
399 goto free_id; 424 goto free_id;
400 } 425 }
@@ -433,6 +458,12 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
433 if (ret < 0) 458 if (ret < 0)
434 goto free_id; 459 goto free_id;
435 460
461 ret = gpiod_lock_as_irq(desc);
462 if (ret < 0) {
463 gpiod_warn(desc, "failed to flag the GPIO for IRQ\n");
464 goto free_id;
465 }
466
436 desc->flags |= gpio_flags; 467 desc->flags |= gpio_flags;
437 return 0; 468 return 0;
438 469
@@ -736,7 +767,7 @@ static struct class gpio_class = {
736 767
737 768
738/** 769/**
739 * gpio_export - export a GPIO through sysfs 770 * gpiod_export - export a GPIO through sysfs
740 * @gpio: gpio to make available, already requested 771 * @gpio: gpio to make available, already requested
741 * @direction_may_change: true if userspace may change gpio direction 772 * @direction_may_change: true if userspace may change gpio direction
742 * Context: arch_initcall or later 773 * Context: arch_initcall or later
@@ -750,7 +781,7 @@ static struct class gpio_class = {
750 * 781 *
751 * Returns zero on success, else an error. 782 * Returns zero on success, else an error.
752 */ 783 */
753static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 784int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
754{ 785{
755 unsigned long flags; 786 unsigned long flags;
756 int status; 787 int status;
@@ -828,12 +859,7 @@ fail_unlock:
828 status); 859 status);
829 return status; 860 return status;
830} 861}
831 862EXPORT_SYMBOL_GPL(gpiod_export);
832int gpio_export(unsigned gpio, bool direction_may_change)
833{
834 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
835}
836EXPORT_SYMBOL_GPL(gpio_export);
837 863
838static int match_export(struct device *dev, const void *data) 864static int match_export(struct device *dev, const void *data)
839{ 865{
@@ -841,7 +867,7 @@ static int match_export(struct device *dev, const void *data)
841} 867}
842 868
843/** 869/**
844 * gpio_export_link - create a sysfs link to an exported GPIO node 870 * gpiod_export_link - create a sysfs link to an exported GPIO node
845 * @dev: device under which to create symlink 871 * @dev: device under which to create symlink
846 * @name: name of the symlink 872 * @name: name of the symlink
847 * @gpio: gpio to create symlink to, already exported 873 * @gpio: gpio to create symlink to, already exported
@@ -851,8 +877,8 @@ static int match_export(struct device *dev, const void *data)
851 * 877 *
852 * Returns zero on success, else an error. 878 * Returns zero on success, else an error.
853 */ 879 */
854static int gpiod_export_link(struct device *dev, const char *name, 880int gpiod_export_link(struct device *dev, const char *name,
855 struct gpio_desc *desc) 881 struct gpio_desc *desc)
856{ 882{
857 int status = -EINVAL; 883 int status = -EINVAL;
858 884
@@ -883,15 +909,10 @@ static int gpiod_export_link(struct device *dev, const char *name,
883 909
884 return status; 910 return status;
885} 911}
886 912EXPORT_SYMBOL_GPL(gpiod_export_link);
887int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
888{
889 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
890}
891EXPORT_SYMBOL_GPL(gpio_export_link);
892 913
893/** 914/**
894 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value 915 * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value
895 * @gpio: gpio to change 916 * @gpio: gpio to change
896 * @value: non-zero to use active low, i.e. inverted values 917 * @value: non-zero to use active low, i.e. inverted values
897 * 918 *
@@ -902,7 +923,7 @@ EXPORT_SYMBOL_GPL(gpio_export_link);
902 * 923 *
903 * Returns zero on success, else an error. 924 * Returns zero on success, else an error.
904 */ 925 */
905static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 926int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
906{ 927{
907 struct device *dev = NULL; 928 struct device *dev = NULL;
908 int status = -EINVAL; 929 int status = -EINVAL;
@@ -933,20 +954,15 @@ unlock:
933 954
934 return status; 955 return status;
935} 956}
936 957EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low);
937int gpio_sysfs_set_active_low(unsigned gpio, int value)
938{
939 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
940}
941EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
942 958
943/** 959/**
944 * gpio_unexport - reverse effect of gpio_export() 960 * gpiod_unexport - reverse effect of gpio_export()
945 * @gpio: gpio to make unavailable 961 * @gpio: gpio to make unavailable
946 * 962 *
947 * This is implicit on gpio_free(). 963 * This is implicit on gpio_free().
948 */ 964 */
949static void gpiod_unexport(struct gpio_desc *desc) 965void gpiod_unexport(struct gpio_desc *desc)
950{ 966{
951 int status = 0; 967 int status = 0;
952 struct device *dev = NULL; 968 struct device *dev = NULL;
@@ -979,12 +995,7 @@ static void gpiod_unexport(struct gpio_desc *desc)
979 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 995 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
980 status); 996 status);
981} 997}
982 998EXPORT_SYMBOL_GPL(gpiod_unexport);
983void gpio_unexport(unsigned gpio)
984{
985 gpiod_unexport(gpio_to_desc(gpio));
986}
987EXPORT_SYMBOL_GPL(gpio_unexport);
988 999
989static int gpiochip_export(struct gpio_chip *chip) 1000static int gpiochip_export(struct gpio_chip *chip)
990{ 1001{
@@ -1091,27 +1102,6 @@ static inline void gpiochip_unexport(struct gpio_chip *chip)
1091{ 1102{
1092} 1103}
1093 1104
1094static inline int gpiod_export(struct gpio_desc *desc,
1095 bool direction_may_change)
1096{
1097 return -ENOSYS;
1098}
1099
1100static inline int gpiod_export_link(struct device *dev, const char *name,
1101 struct gpio_desc *desc)
1102{
1103 return -ENOSYS;
1104}
1105
1106static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1107{
1108 return -ENOSYS;
1109}
1110
1111static inline void gpiod_unexport(struct gpio_desc *desc)
1112{
1113}
1114
1115#endif /* CONFIG_GPIO_SYSFS */ 1105#endif /* CONFIG_GPIO_SYSFS */
1116 1106
1117/* 1107/*
@@ -1670,7 +1660,16 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1670 * rely on gpio_request() having been called beforehand. 1660 * rely on gpio_request() having been called beforehand.
1671 */ 1661 */
1672 1662
1673static int gpiod_direction_input(struct gpio_desc *desc) 1663/**
1664 * gpiod_direction_input - set the GPIO direction to input
1665 * @desc: GPIO to set to input
1666 *
1667 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
1668 * be called safely on it.
1669 *
1670 * Return 0 in case of success, else an error code.
1671 */
1672int gpiod_direction_input(struct gpio_desc *desc)
1674{ 1673{
1675 unsigned long flags; 1674 unsigned long flags;
1676 struct gpio_chip *chip; 1675 struct gpio_chip *chip;
@@ -1684,8 +1683,9 @@ static int gpiod_direction_input(struct gpio_desc *desc)
1684 1683
1685 chip = desc->chip; 1684 chip = desc->chip;
1686 if (!chip->get || !chip->direction_input) { 1685 if (!chip->get || !chip->direction_input) {
1687 pr_warn("%s: missing get() or direction_input() operations\n", 1686 gpiod_warn(desc,
1688 __func__); 1687 "%s: missing get() or direction_input() operations\n",
1688 __func__);
1689 return -EIO; 1689 return -EIO;
1690 } 1690 }
1691 1691
@@ -1705,8 +1705,7 @@ static int gpiod_direction_input(struct gpio_desc *desc)
1705 if (status) { 1705 if (status) {
1706 status = chip->request(chip, offset); 1706 status = chip->request(chip, offset);
1707 if (status < 0) { 1707 if (status < 0) {
1708 pr_debug("GPIO-%d: chip request fail, %d\n", 1708 gpiod_dbg(desc, "chip request fail, %d\n", status);
1709 desc_to_gpio(desc), status);
1710 /* and it's not available to anyone else ... 1709 /* and it's not available to anyone else ...
1711 * gpio_request() is the fully clean solution. 1710 * gpio_request() is the fully clean solution.
1712 */ 1711 */
@@ -1724,18 +1723,22 @@ lose:
1724fail: 1723fail:
1725 spin_unlock_irqrestore(&gpio_lock, flags); 1724 spin_unlock_irqrestore(&gpio_lock, flags);
1726 if (status) 1725 if (status)
1727 pr_debug("%s: gpio-%d status %d\n", __func__, 1726 gpiod_dbg(desc, "%s status %d\n", __func__, status);
1728 desc_to_gpio(desc), status);
1729 return status; 1727 return status;
1730} 1728}
1729EXPORT_SYMBOL_GPL(gpiod_direction_input);
1731 1730
1732int gpio_direction_input(unsigned gpio) 1731/**
1733{ 1732 * gpiod_direction_output - set the GPIO direction to input
1734 return gpiod_direction_input(gpio_to_desc(gpio)); 1733 * @desc: GPIO to set to output
1735} 1734 * @value: initial output value of the GPIO
1736EXPORT_SYMBOL_GPL(gpio_direction_input); 1735 *
1737 1736 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1738static int gpiod_direction_output(struct gpio_desc *desc, int value) 1737 * be called safely on it. The initial value of the output must be specified.
1738 *
1739 * Return 0 in case of success, else an error code.
1740 */
1741int gpiod_direction_output(struct gpio_desc *desc, int value)
1739{ 1742{
1740 unsigned long flags; 1743 unsigned long flags;
1741 struct gpio_chip *chip; 1744 struct gpio_chip *chip;
@@ -1747,6 +1750,14 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
1747 return -EINVAL; 1750 return -EINVAL;
1748 } 1751 }
1749 1752
1753 /* GPIOs used for IRQs shall not be set as output */
1754 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
1755 gpiod_err(desc,
1756 "%s: tried to set a GPIO tied to an IRQ as output\n",
1757 __func__);
1758 return -EIO;
1759 }
1760
1750 /* Open drain pin should not be driven to 1 */ 1761 /* Open drain pin should not be driven to 1 */
1751 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1762 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1752 return gpiod_direction_input(desc); 1763 return gpiod_direction_input(desc);
@@ -1757,8 +1768,9 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
1757 1768
1758 chip = desc->chip; 1769 chip = desc->chip;
1759 if (!chip->set || !chip->direction_output) { 1770 if (!chip->set || !chip->direction_output) {
1760 pr_warn("%s: missing set() or direction_output() operations\n", 1771 gpiod_warn(desc,
1761 __func__); 1772 "%s: missing set() or direction_output() operations\n",
1773 __func__);
1762 return -EIO; 1774 return -EIO;
1763 } 1775 }
1764 1776
@@ -1778,8 +1790,7 @@ static int gpiod_direction_output(struct gpio_desc *desc, int value)
1778 if (status) { 1790 if (status) {
1779 status = chip->request(chip, offset); 1791 status = chip->request(chip, offset);
1780 if (status < 0) { 1792 if (status < 0) {
1781 pr_debug("GPIO-%d: chip request fail, %d\n", 1793 gpiod_dbg(desc, "chip request fail, %d\n", status);
1782 desc_to_gpio(desc), status);
1783 /* and it's not available to anyone else ... 1794 /* and it's not available to anyone else ...
1784 * gpio_request() is the fully clean solution. 1795 * gpio_request() is the fully clean solution.
1785 */ 1796 */
@@ -1797,26 +1808,20 @@ lose:
1797fail: 1808fail:
1798 spin_unlock_irqrestore(&gpio_lock, flags); 1809 spin_unlock_irqrestore(&gpio_lock, flags);
1799 if (status) 1810 if (status)
1800 pr_debug("%s: gpio-%d status %d\n", __func__, 1811 gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
1801 desc_to_gpio(desc), status);
1802 return status; 1812 return status;
1803} 1813}
1804 1814EXPORT_SYMBOL_GPL(gpiod_direction_output);
1805int gpio_direction_output(unsigned gpio, int value)
1806{
1807 return gpiod_direction_output(gpio_to_desc(gpio), value);
1808}
1809EXPORT_SYMBOL_GPL(gpio_direction_output);
1810 1815
1811/** 1816/**
1812 * gpio_set_debounce - sets @debounce time for a @gpio 1817 * gpiod_set_debounce - sets @debounce time for a @gpio
1813 * @gpio: the gpio to set debounce time 1818 * @gpio: the gpio to set debounce time
1814 * @debounce: debounce time is microseconds 1819 * @debounce: debounce time is microseconds
1815 * 1820 *
1816 * returns -ENOTSUPP if the controller does not support setting 1821 * returns -ENOTSUPP if the controller does not support setting
1817 * debounce. 1822 * debounce.
1818 */ 1823 */
1819static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 1824int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1820{ 1825{
1821 unsigned long flags; 1826 unsigned long flags;
1822 struct gpio_chip *chip; 1827 struct gpio_chip *chip;
@@ -1830,8 +1835,9 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1830 1835
1831 chip = desc->chip; 1836 chip = desc->chip;
1832 if (!chip->set || !chip->set_debounce) { 1837 if (!chip->set || !chip->set_debounce) {
1833 pr_debug("%s: missing set() or set_debounce() operations\n", 1838 gpiod_dbg(desc,
1834 __func__); 1839 "%s: missing set() or set_debounce() operations\n",
1840 __func__);
1835 return -ENOTSUPP; 1841 return -ENOTSUPP;
1836 } 1842 }
1837 1843
@@ -1853,17 +1859,23 @@ static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1853fail: 1859fail:
1854 spin_unlock_irqrestore(&gpio_lock, flags); 1860 spin_unlock_irqrestore(&gpio_lock, flags);
1855 if (status) 1861 if (status)
1856 pr_debug("%s: gpio-%d status %d\n", __func__, 1862 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1857 desc_to_gpio(desc), status);
1858 1863
1859 return status; 1864 return status;
1860} 1865}
1866EXPORT_SYMBOL_GPL(gpiod_set_debounce);
1861 1867
1862int gpio_set_debounce(unsigned gpio, unsigned debounce) 1868/**
1869 * gpiod_is_active_low - test whether a GPIO is active-low or not
1870 * @desc: the gpio descriptor to test
1871 *
1872 * Returns 1 if the GPIO is active-low, 0 otherwise.
1873 */
1874int gpiod_is_active_low(const struct gpio_desc *desc)
1863{ 1875{
1864 return gpiod_set_debounce(gpio_to_desc(gpio), debounce); 1876 return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
1865} 1877}
1866EXPORT_SYMBOL_GPL(gpio_set_debounce); 1878EXPORT_SYMBOL_GPL(gpiod_is_active_low);
1867 1879
1868/* I/O calls are only valid after configuration completed; the relevant 1880/* I/O calls are only valid after configuration completed; the relevant
1869 * "is this a valid GPIO" error checks should already have been done. 1881 * "is this a valid GPIO" error checks should already have been done.
@@ -1887,42 +1899,68 @@ EXPORT_SYMBOL_GPL(gpio_set_debounce);
1887 * that the GPIO was actually requested. 1899 * that the GPIO was actually requested.
1888 */ 1900 */
1889 1901
1890/** 1902static int _gpiod_get_raw_value(const struct gpio_desc *desc)
1891 * __gpio_get_value() - return a gpio's value
1892 * @gpio: gpio whose value will be returned
1893 * Context: any
1894 *
1895 * This is used directly or indirectly to implement gpio_get_value().
1896 * It returns the zero or nonzero value provided by the associated
1897 * gpio_chip.get() method; or zero if no such method is provided.
1898 */
1899static int gpiod_get_value(const struct gpio_desc *desc)
1900{ 1903{
1901 struct gpio_chip *chip; 1904 struct gpio_chip *chip;
1902 int value; 1905 int value;
1903 int offset; 1906 int offset;
1904 1907
1905 if (!desc)
1906 return 0;
1907 chip = desc->chip; 1908 chip = desc->chip;
1908 offset = gpio_chip_hwgpio(desc); 1909 offset = gpio_chip_hwgpio(desc);
1909 /* Should be using gpio_get_value_cansleep() */
1910 WARN_ON(chip->can_sleep);
1911 value = chip->get ? chip->get(chip, offset) : 0; 1910 value = chip->get ? chip->get(chip, offset) : 0;
1912 trace_gpio_value(desc_to_gpio(desc), 1, value); 1911 trace_gpio_value(desc_to_gpio(desc), 1, value);
1913 return value; 1912 return value;
1914} 1913}
1915 1914
1916int __gpio_get_value(unsigned gpio) 1915/**
1916 * gpiod_get_raw_value() - return a gpio's raw value
1917 * @desc: gpio whose value will be returned
1918 *
1919 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
1920 * its ACTIVE_LOW status.
1921 *
1922 * This function should be called from contexts where we cannot sleep, and will
1923 * complain if the GPIO chip functions potentially sleep.
1924 */
1925int gpiod_get_raw_value(const struct gpio_desc *desc)
1926{
1927 if (!desc)
1928 return 0;
1929 /* Should be using gpio_get_value_cansleep() */
1930 WARN_ON(desc->chip->can_sleep);
1931 return _gpiod_get_raw_value(desc);
1932}
1933EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
1934
1935/**
1936 * gpiod_get_value() - return a gpio's value
1937 * @desc: gpio whose value will be returned
1938 *
1939 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
1940 * account.
1941 *
1942 * This function should be called from contexts where we cannot sleep, and will
1943 * complain if the GPIO chip functions potentially sleep.
1944 */
1945int gpiod_get_value(const struct gpio_desc *desc)
1917{ 1946{
1918 return gpiod_get_value(gpio_to_desc(gpio)); 1947 int value;
1948 if (!desc)
1949 return 0;
1950 /* Should be using gpio_get_value_cansleep() */
1951 WARN_ON(desc->chip->can_sleep);
1952
1953 value = _gpiod_get_raw_value(desc);
1954 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1955 value = !value;
1956
1957 return value;
1919} 1958}
1920EXPORT_SYMBOL_GPL(__gpio_get_value); 1959EXPORT_SYMBOL_GPL(gpiod_get_value);
1921 1960
1922/* 1961/*
1923 * _gpio_set_open_drain_value() - Set the open drain gpio's value. 1962 * _gpio_set_open_drain_value() - Set the open drain gpio's value.
1924 * @gpio: Gpio whose state need to be set. 1963 * @desc: gpio descriptor whose state need to be set.
1925 * @chip: Gpio chip.
1926 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1964 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1927 */ 1965 */
1928static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) 1966static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
@@ -1942,14 +1980,14 @@ static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1942 } 1980 }
1943 trace_gpio_direction(desc_to_gpio(desc), value, err); 1981 trace_gpio_direction(desc_to_gpio(desc), value, err);
1944 if (err < 0) 1982 if (err < 0)
1945 pr_err("%s: Error in set_value for open drain gpio%d err %d\n", 1983 gpiod_err(desc,
1946 __func__, desc_to_gpio(desc), err); 1984 "%s: Error in set_value for open drain err %d\n",
1985 __func__, err);
1947} 1986}
1948 1987
1949/* 1988/*
1950 * _gpio_set_open_source() - Set the open source gpio's value. 1989 * _gpio_set_open_source_value() - Set the open source gpio's value.
1951 * @gpio: Gpio whose state need to be set. 1990 * @desc: gpio descriptor whose state need to be set.
1952 * @chip: Gpio chip.
1953 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1991 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1954 */ 1992 */
1955static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) 1993static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
@@ -1969,28 +2007,16 @@ static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1969 } 2007 }
1970 trace_gpio_direction(desc_to_gpio(desc), !value, err); 2008 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1971 if (err < 0) 2009 if (err < 0)
1972 pr_err("%s: Error in set_value for open source gpio%d err %d\n", 2010 gpiod_err(desc,
1973 __func__, desc_to_gpio(desc), err); 2011 "%s: Error in set_value for open source err %d\n",
2012 __func__, err);
1974} 2013}
1975 2014
1976/** 2015static void _gpiod_set_raw_value(struct gpio_desc *desc, int value)
1977 * __gpio_set_value() - assign a gpio's value
1978 * @gpio: gpio whose value will be assigned
1979 * @value: value to assign
1980 * Context: any
1981 *
1982 * This is used directly or indirectly to implement gpio_set_value().
1983 * It invokes the associated gpio_chip.set() method.
1984 */
1985static void gpiod_set_value(struct gpio_desc *desc, int value)
1986{ 2016{
1987 struct gpio_chip *chip; 2017 struct gpio_chip *chip;
1988 2018
1989 if (!desc)
1990 return;
1991 chip = desc->chip; 2019 chip = desc->chip;
1992 /* Should be using gpio_set_value_cansleep() */
1993 WARN_ON(chip->can_sleep);
1994 trace_gpio_value(desc_to_gpio(desc), 0, value); 2020 trace_gpio_value(desc_to_gpio(desc), 0, value);
1995 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2021 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1996 _gpio_set_open_drain_value(desc, value); 2022 _gpio_set_open_drain_value(desc, value);
@@ -2000,44 +2026,71 @@ static void gpiod_set_value(struct gpio_desc *desc, int value)
2000 chip->set(chip, gpio_chip_hwgpio(desc), value); 2026 chip->set(chip, gpio_chip_hwgpio(desc), value);
2001} 2027}
2002 2028
2003void __gpio_set_value(unsigned gpio, int value) 2029/**
2030 * gpiod_set_raw_value() - assign a gpio's raw value
2031 * @desc: gpio whose value will be assigned
2032 * @value: value to assign
2033 *
2034 * Set the raw value of the GPIO, i.e. the value of its physical line without
2035 * regard for its ACTIVE_LOW status.
2036 *
2037 * This function should be called from contexts where we cannot sleep, and will
2038 * complain if the GPIO chip functions potentially sleep.
2039 */
2040void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2004{ 2041{
2005 return gpiod_set_value(gpio_to_desc(gpio), value); 2042 if (!desc)
2043 return;
2044 /* Should be using gpio_set_value_cansleep() */
2045 WARN_ON(desc->chip->can_sleep);
2046 _gpiod_set_raw_value(desc, value);
2006} 2047}
2007EXPORT_SYMBOL_GPL(__gpio_set_value); 2048EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2008 2049
2009/** 2050/**
2010 * __gpio_cansleep() - report whether gpio value access will sleep 2051 * gpiod_set_value() - assign a gpio's value
2011 * @gpio: gpio in question 2052 * @desc: gpio whose value will be assigned
2012 * Context: any 2053 * @value: value to assign
2054 *
2055 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2056 * account
2013 * 2057 *
2014 * This is used directly or indirectly to implement gpio_cansleep(). It 2058 * This function should be called from contexts where we cannot sleep, and will
2015 * returns nonzero if access reading or writing the GPIO value can sleep. 2059 * complain if the GPIO chip functions potentially sleep.
2016 */ 2060 */
2017static int gpiod_cansleep(const struct gpio_desc *desc) 2061void gpiod_set_value(struct gpio_desc *desc, int value)
2018{ 2062{
2019 if (!desc) 2063 if (!desc)
2020 return 0; 2064 return;
2021 /* only call this on GPIOs that are valid! */ 2065 /* Should be using gpio_set_value_cansleep() */
2022 return desc->chip->can_sleep; 2066 WARN_ON(desc->chip->can_sleep);
2067 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2068 value = !value;
2069 _gpiod_set_raw_value(desc, value);
2023} 2070}
2071EXPORT_SYMBOL_GPL(gpiod_set_value);
2024 2072
2025int __gpio_cansleep(unsigned gpio) 2073/**
2074 * gpiod_cansleep() - report whether gpio value access may sleep
2075 * @desc: gpio to check
2076 *
2077 */
2078int gpiod_cansleep(const struct gpio_desc *desc)
2026{ 2079{
2027 return gpiod_cansleep(gpio_to_desc(gpio)); 2080 if (!desc)
2081 return 0;
2082 return desc->chip->can_sleep;
2028} 2083}
2029EXPORT_SYMBOL_GPL(__gpio_cansleep); 2084EXPORT_SYMBOL_GPL(gpiod_cansleep);
2030 2085
2031/** 2086/**
2032 * __gpio_to_irq() - return the IRQ corresponding to a GPIO 2087 * gpiod_to_irq() - return the IRQ corresponding to a GPIO
2033 * @gpio: gpio whose IRQ will be returned (already requested) 2088 * @desc: gpio whose IRQ will be returned (already requested)
2034 * Context: any
2035 * 2089 *
2036 * This is used directly or indirectly to implement gpio_to_irq(). 2090 * Return the IRQ corresponding to the passed GPIO, or an error code in case of
2037 * It returns the number of the IRQ signaled by this (input) GPIO, 2091 * error.
2038 * or a negative errno.
2039 */ 2092 */
2040static int gpiod_to_irq(const struct gpio_desc *desc) 2093int gpiod_to_irq(const struct gpio_desc *desc)
2041{ 2094{
2042 struct gpio_chip *chip; 2095 struct gpio_chip *chip;
2043 int offset; 2096 int offset;
@@ -2048,62 +2101,366 @@ static int gpiod_to_irq(const struct gpio_desc *desc)
2048 offset = gpio_chip_hwgpio(desc); 2101 offset = gpio_chip_hwgpio(desc);
2049 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; 2102 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
2050} 2103}
2104EXPORT_SYMBOL_GPL(gpiod_to_irq);
2105
2106/**
2107 * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ
2108 * @gpio: the GPIO line to lock as used for IRQ
2109 *
2110 * This is used directly by GPIO drivers that want to lock down
2111 * a certain GPIO line to be used as IRQs, for example in the
2112 * .to_irq() callback of their gpio_chip, or in the .irq_enable()
2113 * of its irq_chip implementation if the GPIO is known from that
2114 * code.
2115 */
2116int gpiod_lock_as_irq(struct gpio_desc *desc)
2117{
2118 if (!desc)
2119 return -EINVAL;
2120
2121 if (test_bit(FLAG_IS_OUT, &desc->flags)) {
2122 gpiod_err(desc,
2123 "%s: tried to flag a GPIO set as output for IRQ\n",
2124 __func__);
2125 return -EIO;
2126 }
2127
2128 set_bit(FLAG_USED_AS_IRQ, &desc->flags);
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(gpiod_lock_as_irq);
2132
2133int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
2134{
2135 return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset));
2136}
2137EXPORT_SYMBOL_GPL(gpio_lock_as_irq);
2051 2138
2052int __gpio_to_irq(unsigned gpio) 2139/**
2140 * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ
2141 * @gpio: the GPIO line to unlock from IRQ usage
2142 *
2143 * This is used directly by GPIO drivers that want to indicate
2144 * that a certain GPIO is no longer used exclusively for IRQ.
2145 */
2146void gpiod_unlock_as_irq(struct gpio_desc *desc)
2053{ 2147{
2054 return gpiod_to_irq(gpio_to_desc(gpio)); 2148 if (!desc)
2149 return;
2150
2151 clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
2055} 2152}
2056EXPORT_SYMBOL_GPL(__gpio_to_irq); 2153EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq);
2057 2154
2155void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
2156{
2157 return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset));
2158}
2159EXPORT_SYMBOL_GPL(gpio_unlock_as_irq);
2058 2160
2059/* There's no value in making it easy to inline GPIO calls that may sleep. 2161/**
2060 * Common examples include ones connected to I2C or SPI chips. 2162 * gpiod_get_raw_value_cansleep() - return a gpio's raw value
2163 * @desc: gpio whose value will be returned
2164 *
2165 * Return the GPIO's raw value, i.e. the value of the physical line disregarding
2166 * its ACTIVE_LOW status.
2167 *
2168 * This function is to be called from contexts that can sleep.
2061 */ 2169 */
2170int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2171{
2172 might_sleep_if(extra_checks);
2173 if (!desc)
2174 return 0;
2175 return _gpiod_get_raw_value(desc);
2176}
2177EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2062 2178
2063static int gpiod_get_value_cansleep(const struct gpio_desc *desc) 2179/**
2180 * gpiod_get_value_cansleep() - return a gpio's value
2181 * @desc: gpio whose value will be returned
2182 *
2183 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
2184 * account.
2185 *
2186 * This function is to be called from contexts that can sleep.
2187 */
2188int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2064{ 2189{
2065 struct gpio_chip *chip;
2066 int value; 2190 int value;
2067 int offset;
2068 2191
2069 might_sleep_if(extra_checks); 2192 might_sleep_if(extra_checks);
2070 if (!desc) 2193 if (!desc)
2071 return 0; 2194 return 0;
2072 chip = desc->chip; 2195
2073 offset = gpio_chip_hwgpio(desc); 2196 value = _gpiod_get_raw_value(desc);
2074 value = chip->get ? chip->get(chip, offset) : 0; 2197 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2075 trace_gpio_value(desc_to_gpio(desc), 1, value); 2198 value = !value;
2199
2076 return value; 2200 return value;
2077} 2201}
2202EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2078 2203
2079int gpio_get_value_cansleep(unsigned gpio) 2204/**
2205 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2206 * @desc: gpio whose value will be assigned
2207 * @value: value to assign
2208 *
2209 * Set the raw value of the GPIO, i.e. the value of its physical line without
2210 * regard for its ACTIVE_LOW status.
2211 *
2212 * This function is to be called from contexts that can sleep.
2213 */
2214void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2080{ 2215{
2081 return gpiod_get_value_cansleep(gpio_to_desc(gpio)); 2216 might_sleep_if(extra_checks);
2217 if (!desc)
2218 return;
2219 _gpiod_set_raw_value(desc, value);
2082} 2220}
2083EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 2221EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2084 2222
2085static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 2223/**
2224 * gpiod_set_value_cansleep() - assign a gpio's value
2225 * @desc: gpio whose value will be assigned
2226 * @value: value to assign
2227 *
2228 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
2229 * account
2230 *
2231 * This function is to be called from contexts that can sleep.
2232 */
2233void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2086{ 2234{
2087 struct gpio_chip *chip;
2088
2089 might_sleep_if(extra_checks); 2235 might_sleep_if(extra_checks);
2090 if (!desc) 2236 if (!desc)
2091 return; 2237 return;
2092 chip = desc->chip; 2238
2093 trace_gpio_value(desc_to_gpio(desc), 0, value); 2239 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2094 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2240 value = !value;
2095 _gpio_set_open_drain_value(desc, value); 2241 _gpiod_set_raw_value(desc, value);
2096 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2242}
2097 _gpio_set_open_source_value(desc, value); 2243EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2244
2245/**
2246 * gpiod_add_table() - register GPIO device consumers
2247 * @table: array of consumers to register
2248 * @num: number of consumers in table
2249 */
2250void gpiod_add_table(struct gpiod_lookup *table, size_t size)
2251{
2252 mutex_lock(&gpio_lookup_lock);
2253
2254 while (size--) {
2255 list_add_tail(&table->list, &gpio_lookup_list);
2256 table++;
2257 }
2258
2259 mutex_unlock(&gpio_lookup_lock);
2260}
2261
2262/*
2263 * Caller must have a acquired gpio_lookup_lock
2264 */
2265static struct gpio_chip *find_chip_by_name(const char *name)
2266{
2267 struct gpio_chip *chip = NULL;
2268
2269 list_for_each_entry(chip, &gpio_lookup_list, list) {
2270 if (chip->label == NULL)
2271 continue;
2272 if (!strcmp(chip->label, name))
2273 break;
2274 }
2275
2276 return chip;
2277}
2278
2279#ifdef CONFIG_OF
2280static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2281 unsigned int idx, unsigned long *flags)
2282{
2283 char prop_name[32]; /* 32 is max size of property name */
2284 enum of_gpio_flags of_flags;
2285 struct gpio_desc *desc;
2286
2287 if (con_id)
2288 snprintf(prop_name, 32, "%s-gpios", con_id);
2098 else 2289 else
2099 chip->set(chip, gpio_chip_hwgpio(desc), value); 2290 snprintf(prop_name, 32, "gpios");
2291
2292 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
2293 &of_flags);
2294
2295 if (IS_ERR(desc))
2296 return desc;
2297
2298 if (of_flags & OF_GPIO_ACTIVE_LOW)
2299 *flags |= GPIOF_ACTIVE_LOW;
2300
2301 return desc;
2302}
2303#else
2304static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
2305 unsigned int idx, unsigned long *flags)
2306{
2307 return ERR_PTR(-ENODEV);
2308}
2309#endif
2310
2311static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2312 unsigned int idx, unsigned long *flags)
2313{
2314 struct acpi_gpio_info info;
2315 struct gpio_desc *desc;
2316
2317 desc = acpi_get_gpiod_by_index(dev, idx, &info);
2318 if (IS_ERR(desc))
2319 return desc;
2320
2321 if (info.gpioint && info.active_low)
2322 *flags |= GPIOF_ACTIVE_LOW;
2323
2324 return desc;
2325}
2326
2327static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2328 unsigned int idx, unsigned long *flags)
2329{
2330 const char *dev_id = dev ? dev_name(dev) : NULL;
2331 struct gpio_desc *desc = ERR_PTR(-ENODEV);
2332 unsigned int match, best = 0;
2333 struct gpiod_lookup *p;
2334
2335 mutex_lock(&gpio_lookup_lock);
2336
2337 list_for_each_entry(p, &gpio_lookup_list, list) {
2338 match = 0;
2339
2340 if (p->dev_id) {
2341 if (!dev_id || strcmp(p->dev_id, dev_id))
2342 continue;
2343
2344 match += 2;
2345 }
2346
2347 if (p->con_id) {
2348 if (!con_id || strcmp(p->con_id, con_id))
2349 continue;
2350
2351 match += 1;
2352 }
2353
2354 if (p->idx != idx)
2355 continue;
2356
2357 if (match > best) {
2358 struct gpio_chip *chip;
2359
2360 chip = find_chip_by_name(p->chip_label);
2361
2362 if (!chip) {
2363 dev_warn(dev, "cannot find GPIO chip %s\n",
2364 p->chip_label);
2365 continue;
2366 }
2367
2368 if (chip->ngpio >= p->chip_hwnum) {
2369 dev_warn(dev, "GPIO chip %s has %d GPIOs\n",
2370 chip->label, chip->ngpio);
2371 continue;
2372 }
2373
2374 desc = gpio_to_desc(chip->base + p->chip_hwnum);
2375 *flags = p->flags;
2376
2377 if (match != 3)
2378 best = match;
2379 else
2380 break;
2381 }
2382 }
2383
2384 mutex_unlock(&gpio_lookup_lock);
2385
2386 return desc;
2387}
2388
2389/**
2390 * gpio_get - obtain a GPIO for a given GPIO function
2391 * @dev: GPIO consumer
2392 * @con_id: function within the GPIO consumer
2393 *
2394 * Return the GPIO descriptor corresponding to the function con_id of device
2395 * dev, or an IS_ERR() condition if an error occured.
2396 */
2397struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2398{
2399 return gpiod_get_index(dev, con_id, 0);
2400}
2401EXPORT_SYMBOL_GPL(gpiod_get);
2402
2403/**
2404 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2405 * @dev: GPIO consumer
2406 * @con_id: function within the GPIO consumer
2407 * @idx: index of the GPIO to obtain in the consumer
2408 *
2409 * This variant of gpiod_get() allows to access GPIOs other than the first
2410 * defined one for functions that define several GPIOs.
2411 *
2412 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error.
2413 */
2414struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2415 const char *con_id,
2416 unsigned int idx)
2417{
2418 struct gpio_desc *desc;
2419 int status;
2420 unsigned long flags = 0;
2421
2422 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
2423
2424 /* Using device tree? */
2425 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) {
2426 dev_dbg(dev, "using device tree for GPIO lookup\n");
2427 desc = of_find_gpio(dev, con_id, idx, &flags);
2428 } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) {
2429 dev_dbg(dev, "using ACPI for GPIO lookup\n");
2430 desc = acpi_find_gpio(dev, con_id, idx, &flags);
2431 } else {
2432 dev_dbg(dev, "using lookup tables for GPIO lookup");
2433 desc = gpiod_find(dev, con_id, idx, &flags);
2434 }
2435
2436 if (IS_ERR(desc)) {
2437 dev_warn(dev, "lookup for GPIO %s failed\n", con_id);
2438 return desc;
2439 }
2440
2441 status = gpiod_request(desc, con_id);
2442
2443 if (status < 0)
2444 return ERR_PTR(status);
2445
2446 if (flags & GPIOF_ACTIVE_LOW)
2447 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
2448
2449 return desc;
2100} 2450}
2451EXPORT_SYMBOL_GPL(gpiod_get_index);
2101 2452
2102void gpio_set_value_cansleep(unsigned gpio, int value) 2453/**
2454 * gpiod_put - dispose of a GPIO descriptor
2455 * @desc: GPIO descriptor to dispose of
2456 *
2457 * No descriptor can be used after gpiod_put() has been called on it.
2458 */
2459void gpiod_put(struct gpio_desc *desc)
2103{ 2460{
2104 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); 2461 gpiod_free(desc);
2105} 2462}
2106EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); 2463EXPORT_SYMBOL_GPL(gpiod_put);
2107 2464
2108#ifdef CONFIG_DEBUG_FS 2465#ifdef CONFIG_DEBUG_FS
2109 2466
@@ -2113,6 +2470,7 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2113 unsigned gpio = chip->base; 2470 unsigned gpio = chip->base;
2114 struct gpio_desc *gdesc = &chip->desc[0]; 2471 struct gpio_desc *gdesc = &chip->desc[0];
2115 int is_out; 2472 int is_out;
2473 int is_irq;
2116 2474
2117 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { 2475 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
2118 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) 2476 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
@@ -2120,12 +2478,14 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
2120 2478
2121 gpiod_get_direction(gdesc); 2479 gpiod_get_direction(gdesc);
2122 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); 2480 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
2123 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", 2481 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
2482 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
2124 gpio, gdesc->label, 2483 gpio, gdesc->label,
2125 is_out ? "out" : "in ", 2484 is_out ? "out" : "in ",
2126 chip->get 2485 chip->get
2127 ? (chip->get(chip, i) ? "hi" : "lo") 2486 ? (chip->get(chip, i) ? "hi" : "lo")
2128 : "? "); 2487 : "? ",
2488 is_irq ? "IRQ" : " ");
2129 seq_printf(s, "\n"); 2489 seq_printf(s, "\n");
2130 } 2490 }
2131} 2491}
diff --git a/drivers/input/misc/ixp4xx-beeper.c b/drivers/input/misc/ixp4xx-beeper.c
index f34beb228d36..17ccba88d636 100644
--- a/drivers/input/misc/ixp4xx-beeper.c
+++ b/drivers/input/misc/ixp4xx-beeper.c
@@ -20,6 +20,7 @@
20#include <linux/delay.h> 20#include <linux/delay.h>
21#include <linux/platform_device.h> 21#include <linux/platform_device.h>
22#include <linux/interrupt.h> 22#include <linux/interrupt.h>
23#include <linux/gpio.h>
23#include <mach/hardware.h> 24#include <mach/hardware.h>
24 25
25MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); 26MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
@@ -35,15 +36,12 @@ static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
35 36
36 spin_lock_irqsave(&beep_lock, flags); 37 spin_lock_irqsave(&beep_lock, flags);
37 38
38 if (count) { 39 if (count) {
39 gpio_line_config(pin, IXP4XX_GPIO_OUT); 40 gpio_direction_output(pin, 0);
40 gpio_line_set(pin, IXP4XX_GPIO_LOW);
41
42 *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE; 41 *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
43 } else { 42 } else {
44 gpio_line_config(pin, IXP4XX_GPIO_IN); 43 gpio_direction_output(pin, 1);
45 gpio_line_set(pin, IXP4XX_GPIO_HIGH); 44 gpio_direction_input(pin);
46
47 *IXP4XX_OSRT2 = 0; 45 *IXP4XX_OSRT2 = 0;
48 } 46 }
49 47
@@ -78,11 +76,13 @@ static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned
78 76
79static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id) 77static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id)
80{ 78{
79 unsigned int pin = (unsigned int) dev_id;
80
81 /* clear interrupt */ 81 /* clear interrupt */
82 *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND; 82 *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
83 83
84 /* flip the beeper output */ 84 /* flip the beeper output */
85 *IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id); 85 gpio_set_value(pin, !gpio_get_value(pin));
86 86
87 return IRQ_HANDLED; 87 return IRQ_HANDLED;
88} 88}
@@ -110,11 +110,15 @@ static int ixp4xx_spkr_probe(struct platform_device *dev)
110 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE); 110 input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
111 input_dev->event = ixp4xx_spkr_event; 111 input_dev->event = ixp4xx_spkr_event;
112 112
113 err = gpio_request(dev->id, "ixp4-beeper");
114 if (err)
115 goto err_free_device;
116
113 err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt, 117 err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt,
114 IRQF_NO_SUSPEND, "ixp4xx-beeper", 118 IRQF_NO_SUSPEND, "ixp4xx-beeper",
115 (void *) dev->id); 119 (void *) dev->id);
116 if (err) 120 if (err)
117 goto err_free_device; 121 goto err_free_gpio;
118 122
119 err = input_register_device(input_dev); 123 err = input_register_device(input_dev);
120 if (err) 124 if (err)
@@ -126,6 +130,8 @@ static int ixp4xx_spkr_probe(struct platform_device *dev)
126 130
127 err_free_irq: 131 err_free_irq:
128 free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id); 132 free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id);
133 err_free_gpio:
134 gpio_free(dev->id);
129 err_free_device: 135 err_free_device:
130 input_free_device(input_dev); 136 input_free_device(input_dev);
131 137
@@ -144,6 +150,7 @@ static int ixp4xx_spkr_remove(struct platform_device *dev)
144 ixp4xx_spkr_control(pin, 0); 150 ixp4xx_spkr_control(pin, 0);
145 151
146 free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id); 152 free_irq(IRQ_IXP4XX_TIMER2, (void *)dev->id);
153 gpio_free(dev->id);
147 154
148 return 0; 155 return 0;
149} 156}
diff --git a/drivers/pinctrl/pinctrl-coh901.c b/drivers/pinctrl/pinctrl-coh901.c
index f22a2193d949..162ac0d73739 100644
--- a/drivers/pinctrl/pinctrl-coh901.c
+++ b/drivers/pinctrl/pinctrl-coh901.c
@@ -529,6 +529,10 @@ static void u300_gpio_irq_enable(struct irq_data *d)
529 529
530 dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n", 530 dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n",
531 d->hwirq, port->name, offset); 531 d->hwirq, port->name, offset);
532 if (gpio_lock_as_irq(&gpio->chip, d->hwirq))
533 dev_err(gpio->dev,
534 "unable to lock HW IRQ %lu for IRQ\n",
535 d->hwirq);
532 local_irq_save(flags); 536 local_irq_save(flags);
533 val = readl(U300_PIN_REG(offset, ien)); 537 val = readl(U300_PIN_REG(offset, ien));
534 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien)); 538 writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
@@ -547,6 +551,7 @@ static void u300_gpio_irq_disable(struct irq_data *d)
547 val = readl(U300_PIN_REG(offset, ien)); 551 val = readl(U300_PIN_REG(offset, ien));
548 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien)); 552 writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
549 local_irq_restore(flags); 553 local_irq_restore(flags);
554 gpio_unlock_as_irq(&gpio->chip, d->hwirq);
550} 555}
551 556
552static struct irq_chip u300_gpio_irqchip = { 557static struct irq_chip u300_gpio_irqchip = {
diff --git a/drivers/pinctrl/pinctrl-nomadik.c b/drivers/pinctrl/pinctrl-nomadik.c
index d7c3ae300fa7..7111c3b59130 100644
--- a/drivers/pinctrl/pinctrl-nomadik.c
+++ b/drivers/pinctrl/pinctrl-nomadik.c
@@ -634,6 +634,10 @@ static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
634{ 634{
635 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d); 635 struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
636 636
637 if (gpio_lock_as_irq(&nmk_chip->chip, d->hwirq))
638 dev_err(nmk_chip->chip.dev,
639 "unable to lock HW IRQ %lu for IRQ\n",
640 d->hwirq);
637 clk_enable(nmk_chip->clk); 641 clk_enable(nmk_chip->clk);
638 nmk_gpio_irq_unmask(d); 642 nmk_gpio_irq_unmask(d);
639 return 0; 643 return 0;
@@ -645,6 +649,7 @@ static void nmk_gpio_irq_shutdown(struct irq_data *d)
645 649
646 nmk_gpio_irq_mask(d); 650 nmk_gpio_irq_mask(d);
647 clk_disable(nmk_chip->clk); 651 clk_disable(nmk_chip->clk);
652 gpio_unlock_as_irq(&nmk_chip->chip, d->hwirq);
648} 653}
649 654
650static struct irq_chip nmk_gpio_irq_chip = { 655static struct irq_chip nmk_gpio_irq_chip = {
diff --git a/drivers/ptp/ptp_ixp46x.c b/drivers/ptp/ptp_ixp46x.c
index d49b85164fd2..4a08727fcaf3 100644
--- a/drivers/ptp/ptp_ixp46x.c
+++ b/drivers/ptp/ptp_ixp46x.c
@@ -259,8 +259,15 @@ static struct ixp_clock ixp_clock;
259static int setup_interrupt(int gpio) 259static int setup_interrupt(int gpio)
260{ 260{
261 int irq; 261 int irq;
262 int err;
262 263
263 gpio_line_config(gpio, IXP4XX_GPIO_IN); 264 err = gpio_request(gpio, "ixp4-ptp");
265 if (err)
266 return err;
267
268 err = gpio_direction_input(gpio);
269 if (err)
270 return err;
264 271
265 irq = gpio_to_irq(gpio); 272 irq = gpio_to_irq(gpio);
266 273
diff --git a/drivers/staging/media/lirc/lirc_serial.c b/drivers/staging/media/lirc/lirc_serial.c
index af08e677b60f..f6bc4c91ab35 100644
--- a/drivers/staging/media/lirc/lirc_serial.c
+++ b/drivers/staging/media/lirc/lirc_serial.c
@@ -67,7 +67,7 @@
67#include <linux/delay.h> 67#include <linux/delay.h>
68#include <linux/poll.h> 68#include <linux/poll.h>
69#include <linux/platform_device.h> 69#include <linux/platform_device.h>
70 70#include <linux/gpio.h>
71#include <linux/io.h> 71#include <linux/io.h>
72#include <linux/irq.h> 72#include <linux/irq.h>
73#include <linux/fcntl.h> 73#include <linux/fcntl.h>
@@ -321,7 +321,7 @@ static void on(void)
321 * status LED and ground 321 * status LED and ground
322 */ 322 */
323 if (type == LIRC_NSLU2) { 323 if (type == LIRC_NSLU2) {
324 gpio_line_set(NSLU2_LED_GRN, IXP4XX_GPIO_LOW); 324 gpio_set_value(NSLU2_LED_GRN, 0);
325 return; 325 return;
326 } 326 }
327#endif 327#endif
@@ -335,7 +335,7 @@ static void off(void)
335{ 335{
336#ifdef CONFIG_LIRC_SERIAL_NSLU2 336#ifdef CONFIG_LIRC_SERIAL_NSLU2
337 if (type == LIRC_NSLU2) { 337 if (type == LIRC_NSLU2) {
338 gpio_line_set(NSLU2_LED_GRN, IXP4XX_GPIO_HIGH); 338 gpio_set_value(NSLU2_LED_GRN, 1);
339 return; 339 return;
340 } 340 }
341#endif 341#endif
@@ -839,6 +839,16 @@ static int lirc_serial_probe(struct platform_device *dev)
839{ 839{
840 int i, nlow, nhigh, result; 840 int i, nlow, nhigh, result;
841 841
842#ifdef CONFIG_LIRC_SERIAL_NSLU2
843 /* This GPIO is used for a LED on the NSLU2 */
844 result = devm_gpio_request(dev, NSLU2_LED_GRN, "lirc-serial");
845 if (result)
846 return result;
847 result = gpio_direction_output(NSLU2_LED_GRN, 0);
848 if (result)
849 return result;
850#endif
851
842 result = request_irq(irq, irq_handler, 852 result = request_irq(irq, irq_handler,
843 (share_irq ? IRQF_SHARED : 0), 853 (share_irq ? IRQF_SHARED : 0),
844 LIRC_DRIVER_NAME, (void *)&hardware); 854 LIRC_DRIVER_NAME, (void *)&hardware);
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h
index 523f40525535..a5f56a0213a7 100644
--- a/include/asm-generic/gpio.h
+++ b/include/asm-generic/gpio.h
@@ -10,6 +10,8 @@
10#ifdef CONFIG_GPIOLIB 10#ifdef CONFIG_GPIOLIB
11 11
12#include <linux/compiler.h> 12#include <linux/compiler.h>
13#include <linux/gpio/driver.h>
14#include <linux/gpio/consumer.h>
13 15
14/* Platforms may implement their GPIO interface with library code, 16/* Platforms may implement their GPIO interface with library code,
15 * at a small performance cost for non-inlined operations and some 17 * at a small performance cost for non-inlined operations and some
@@ -49,122 +51,11 @@ struct module;
49struct device_node; 51struct device_node;
50struct gpio_desc; 52struct gpio_desc;
51 53
52/** 54/* caller holds gpio_lock *OR* gpio is marked as requested */
53 * struct gpio_chip - abstract a GPIO controller 55static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
54 * @label: for diagnostics 56{
55 * @dev: optional device providing the GPIOs 57 return gpiod_to_chip(gpio_to_desc(gpio));
56 * @owner: helps prevent removal of modules exporting active GPIOs 58}
57 * @list: links gpio_chips together for traversal
58 * @request: optional hook for chip-specific activation, such as
59 * enabling module power and clock; may sleep
60 * @free: optional hook for chip-specific deactivation, such as
61 * disabling module power and clock; may sleep
62 * @get_direction: returns direction for signal "offset", 0=out, 1=in,
63 * (same as GPIOF_DIR_XXX), or negative error
64 * @direction_input: configures signal "offset" as input, or returns error
65 * @get: returns value for signal "offset"; for output signals this
66 * returns either the value actually sensed, or zero
67 * @direction_output: configures signal "offset" as output, or returns error
68 * @set_debounce: optional hook for setting debounce time for specified gpio in
69 * interrupt triggered gpio chips
70 * @set: assigns output value for signal "offset"
71 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
72 * implementation may not sleep
73 * @dbg_show: optional routine to show contents in debugfs; default code
74 * will be used when this is omitted, but custom code can show extra
75 * state (such as pullup/pulldown configuration).
76 * @base: identifies the first GPIO number handled by this chip; or, if
77 * negative during registration, requests dynamic ID allocation.
78 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
79 * handled is (base + ngpio - 1).
80 * @desc: array of ngpio descriptors. Private.
81 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
82 * must while accessing GPIO expander chips over I2C or SPI
83 * @names: if set, must be an array of strings to use as alternative
84 * names for the GPIOs in this chip. Any entry in the array
85 * may be NULL if there is no alias for the GPIO, however the
86 * array must be @ngpio entries long. A name can include a single printk
87 * format specifier for an unsigned int. It is substituted by the actual
88 * number of the gpio.
89 *
90 * A gpio_chip can help platforms abstract various sources of GPIOs so
91 * they can all be accessed through a common programing interface.
92 * Example sources would be SOC controllers, FPGAs, multifunction
93 * chips, dedicated GPIO expanders, and so on.
94 *
95 * Each chip controls a number of signals, identified in method calls
96 * by "offset" values in the range 0..(@ngpio - 1). When those signals
97 * are referenced through calls like gpio_get_value(gpio), the offset
98 * is calculated by subtracting @base from the gpio number.
99 */
100struct gpio_chip {
101 const char *label;
102 struct device *dev;
103 struct module *owner;
104 struct list_head list;
105
106 int (*request)(struct gpio_chip *chip,
107 unsigned offset);
108 void (*free)(struct gpio_chip *chip,
109 unsigned offset);
110 int (*get_direction)(struct gpio_chip *chip,
111 unsigned offset);
112 int (*direction_input)(struct gpio_chip *chip,
113 unsigned offset);
114 int (*get)(struct gpio_chip *chip,
115 unsigned offset);
116 int (*direction_output)(struct gpio_chip *chip,
117 unsigned offset, int value);
118 int (*set_debounce)(struct gpio_chip *chip,
119 unsigned offset, unsigned debounce);
120
121 void (*set)(struct gpio_chip *chip,
122 unsigned offset, int value);
123
124 int (*to_irq)(struct gpio_chip *chip,
125 unsigned offset);
126
127 void (*dbg_show)(struct seq_file *s,
128 struct gpio_chip *chip);
129 int base;
130 u16 ngpio;
131 struct gpio_desc *desc;
132 const char *const *names;
133 unsigned can_sleep:1;
134 unsigned exported:1;
135
136#if defined(CONFIG_OF_GPIO)
137 /*
138 * If CONFIG_OF is enabled, then all GPIO controllers described in the
139 * device tree automatically may have an OF translation
140 */
141 struct device_node *of_node;
142 int of_gpio_n_cells;
143 int (*of_xlate)(struct gpio_chip *gc,
144 const struct of_phandle_args *gpiospec, u32 *flags);
145#endif
146#ifdef CONFIG_PINCTRL
147 /*
148 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
149 * describe the actual pin range which they serve in an SoC. This
150 * information would be used by pinctrl subsystem to configure
151 * corresponding pins for gpio usage.
152 */
153 struct list_head pin_ranges;
154#endif
155};
156
157extern const char *gpiochip_is_requested(struct gpio_chip *chip,
158 unsigned offset);
159extern struct gpio_chip *gpio_to_chip(unsigned gpio);
160
161/* add/remove chips */
162extern int gpiochip_add(struct gpio_chip *chip);
163extern int __must_check gpiochip_remove(struct gpio_chip *chip);
164extern struct gpio_chip *gpiochip_find(void *data,
165 int (*match)(struct gpio_chip *chip,
166 void *data));
167
168 59
169/* Always use the library code for GPIO management calls, 60/* Always use the library code for GPIO management calls,
170 * or when sleeping may be involved. 61 * or when sleeping may be involved.
@@ -172,43 +63,84 @@ extern struct gpio_chip *gpiochip_find(void *data,
172extern int gpio_request(unsigned gpio, const char *label); 63extern int gpio_request(unsigned gpio, const char *label);
173extern void gpio_free(unsigned gpio); 64extern void gpio_free(unsigned gpio);
174 65
175extern int gpio_direction_input(unsigned gpio); 66static inline int gpio_direction_input(unsigned gpio)
176extern int gpio_direction_output(unsigned gpio, int value); 67{
68 return gpiod_direction_input(gpio_to_desc(gpio));
69}
70static inline int gpio_direction_output(unsigned gpio, int value)
71{
72 return gpiod_direction_output(gpio_to_desc(gpio), value);
73}
177 74
178extern int gpio_set_debounce(unsigned gpio, unsigned debounce); 75static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
76{
77 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
78}
179 79
180extern int gpio_get_value_cansleep(unsigned gpio); 80static inline int gpio_get_value_cansleep(unsigned gpio)
181extern void gpio_set_value_cansleep(unsigned gpio, int value); 81{
82 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
83}
84static inline void gpio_set_value_cansleep(unsigned gpio, int value)
85{
86 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
87}
182 88
183 89
184/* A platform's <asm/gpio.h> code may want to inline the I/O calls when 90/* A platform's <asm/gpio.h> code may want to inline the I/O calls when
185 * the GPIO is constant and refers to some always-present controller, 91 * the GPIO is constant and refers to some always-present controller,
186 * giving direct access to chip registers and tight bitbanging loops. 92 * giving direct access to chip registers and tight bitbanging loops.
187 */ 93 */
188extern int __gpio_get_value(unsigned gpio); 94static inline int __gpio_get_value(unsigned gpio)
189extern void __gpio_set_value(unsigned gpio, int value); 95{
96 return gpiod_get_raw_value(gpio_to_desc(gpio));
97}
98static inline void __gpio_set_value(unsigned gpio, int value)
99{
100 return gpiod_set_raw_value(gpio_to_desc(gpio), value);
101}
102
103static inline int __gpio_cansleep(unsigned gpio)
104{
105 return gpiod_cansleep(gpio_to_desc(gpio));
106}
190 107
191extern int __gpio_cansleep(unsigned gpio); 108static inline int __gpio_to_irq(unsigned gpio)
109{
110 return gpiod_to_irq(gpio_to_desc(gpio));
111}
192 112
193extern int __gpio_to_irq(unsigned gpio); 113extern int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
114extern void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
194 115
195extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); 116extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
196extern int gpio_request_array(const struct gpio *array, size_t num); 117extern int gpio_request_array(const struct gpio *array, size_t num);
197extern void gpio_free_array(const struct gpio *array, size_t num); 118extern void gpio_free_array(const struct gpio *array, size_t num);
198 119
199#ifdef CONFIG_GPIO_SYSFS
200
201/* 120/*
202 * A sysfs interface can be exported by individual drivers if they want, 121 * A sysfs interface can be exported by individual drivers if they want,
203 * but more typically is configured entirely from userspace. 122 * but more typically is configured entirely from userspace.
204 */ 123 */
205extern int gpio_export(unsigned gpio, bool direction_may_change); 124static inline int gpio_export(unsigned gpio, bool direction_may_change)
206extern int gpio_export_link(struct device *dev, const char *name, 125{
207 unsigned gpio); 126 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
208extern int gpio_sysfs_set_active_low(unsigned gpio, int value); 127}
209extern void gpio_unexport(unsigned gpio);
210 128
211#endif /* CONFIG_GPIO_SYSFS */ 129static inline int gpio_export_link(struct device *dev, const char *name,
130 unsigned gpio)
131{
132 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
133}
134
135static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
136{
137 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
138}
139
140static inline void gpio_unexport(unsigned gpio)
141{
142 gpiod_unexport(gpio_to_desc(gpio));
143}
212 144
213#ifdef CONFIG_PINCTRL 145#ifdef CONFIG_PINCTRL
214 146
@@ -288,31 +220,4 @@ static inline void gpio_set_value_cansleep(unsigned gpio, int value)
288 220
289#endif /* !CONFIG_GPIOLIB */ 221#endif /* !CONFIG_GPIOLIB */
290 222
291#ifndef CONFIG_GPIO_SYSFS
292
293struct device;
294
295/* sysfs support is only available with gpiolib, where it's optional */
296
297static inline int gpio_export(unsigned gpio, bool direction_may_change)
298{
299 return -ENOSYS;
300}
301
302static inline int gpio_export_link(struct device *dev, const char *name,
303 unsigned gpio)
304{
305 return -ENOSYS;
306}
307
308static inline int gpio_sysfs_set_active_low(unsigned gpio, int value)
309{
310 return -ENOSYS;
311}
312
313static inline void gpio_unexport(unsigned gpio)
314{
315}
316#endif /* CONFIG_GPIO_SYSFS */
317
318#endif /* _ASM_GENERIC_GPIO_H */ 223#endif /* _ASM_GENERIC_GPIO_H */
diff --git a/include/linux/acpi_gpio.h b/include/linux/acpi_gpio.h
index 4c120a1e0ca3..d875bc3dba3c 100644
--- a/include/linux/acpi_gpio.h
+++ b/include/linux/acpi_gpio.h
@@ -2,36 +2,35 @@
2#define _LINUX_ACPI_GPIO_H_ 2#define _LINUX_ACPI_GPIO_H_
3 3
4#include <linux/device.h> 4#include <linux/device.h>
5#include <linux/err.h>
5#include <linux/errno.h> 6#include <linux/errno.h>
6#include <linux/gpio.h> 7#include <linux/gpio.h>
8#include <linux/gpio/consumer.h>
7 9
8/** 10/**
9 * struct acpi_gpio_info - ACPI GPIO specific information 11 * struct acpi_gpio_info - ACPI GPIO specific information
10 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo 12 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
13 * @active_low: in case of @gpioint, the pin is active low
11 */ 14 */
12struct acpi_gpio_info { 15struct acpi_gpio_info {
13 bool gpioint; 16 bool gpioint;
17 bool active_low;
14}; 18};
15 19
16#ifdef CONFIG_GPIO_ACPI 20#ifdef CONFIG_GPIO_ACPI
17 21
18int acpi_get_gpio(char *path, int pin); 22struct gpio_desc *acpi_get_gpiod_by_index(struct device *dev, int index,
19int acpi_get_gpio_by_index(struct device *dev, int index, 23 struct acpi_gpio_info *info);
20 struct acpi_gpio_info *info);
21void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 24void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
22void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 25void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
23 26
24#else /* CONFIG_GPIO_ACPI */ 27#else /* CONFIG_GPIO_ACPI */
25 28
26static inline int acpi_get_gpio(char *path, int pin) 29static inline struct gpio_desc *
30acpi_get_gpiod_by_index(struct device *dev, int index,
31 struct acpi_gpio_info *info)
27{ 32{
28 return -ENODEV; 33 return ERR_PTR(-ENOSYS);
29}
30
31static inline int acpi_get_gpio_by_index(struct device *dev, int index,
32 struct acpi_gpio_info *info)
33{
34 return -ENODEV;
35} 34}
36 35
37static inline void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } 36static inline void acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
@@ -39,4 +38,14 @@ static inline void acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
39 38
40#endif /* CONFIG_GPIO_ACPI */ 39#endif /* CONFIG_GPIO_ACPI */
41 40
41static inline int acpi_get_gpio_by_index(struct device *dev, int index,
42 struct acpi_gpio_info *info)
43{
44 struct gpio_desc *desc = acpi_get_gpiod_by_index(dev, index, info);
45
46 if (IS_ERR(desc))
47 return PTR_ERR(desc);
48 return desc_to_gpio(desc);
49}
50
42#endif /* _LINUX_ACPI_GPIO_H_ */ 51#endif /* _LINUX_ACPI_GPIO_H_ */
diff --git a/include/linux/gpio.h b/include/linux/gpio.h
index b8d0e53a802f..13dfd24d01ab 100644
--- a/include/linux/gpio.h
+++ b/include/linux/gpio.h
@@ -16,14 +16,17 @@
16#define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW) 16#define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW)
17#define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH) 17#define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
18 18
19/* Gpio pin is active-low */
20#define GPIOF_ACTIVE_LOW (1 << 2)
21
19/* Gpio pin is open drain */ 22/* Gpio pin is open drain */
20#define GPIOF_OPEN_DRAIN (1 << 2) 23#define GPIOF_OPEN_DRAIN (1 << 3)
21 24
22/* Gpio pin is open source */ 25/* Gpio pin is open source */
23#define GPIOF_OPEN_SOURCE (1 << 3) 26#define GPIOF_OPEN_SOURCE (1 << 4)
24 27
25#define GPIOF_EXPORT (1 << 4) 28#define GPIOF_EXPORT (1 << 5)
26#define GPIOF_EXPORT_CHANGEABLE (1 << 5) 29#define GPIOF_EXPORT_CHANGEABLE (1 << 6)
27#define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT) 30#define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
28#define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE) 31#define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
29 32
@@ -74,6 +77,15 @@ static inline int irq_to_gpio(unsigned int irq)
74 77
75#endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */ 78#endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
76 79
80/* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
81
82struct device;
83
84int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
85int devm_gpio_request_one(struct device *dev, unsigned gpio,
86 unsigned long flags, const char *label);
87void devm_gpio_free(struct device *dev, unsigned int gpio);
88
77#else /* ! CONFIG_GPIOLIB */ 89#else /* ! CONFIG_GPIOLIB */
78 90
79#include <linux/kernel.h> 91#include <linux/kernel.h>
@@ -205,6 +217,18 @@ static inline int gpio_to_irq(unsigned gpio)
205 return -EINVAL; 217 return -EINVAL;
206} 218}
207 219
220static inline int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
221{
222 WARN_ON(1);
223 return -EINVAL;
224}
225
226static inline void gpio_unlock_as_irq(struct gpio_chip *chip,
227 unsigned int offset)
228{
229 WARN_ON(1);
230}
231
208static inline int irq_to_gpio(unsigned irq) 232static inline int irq_to_gpio(unsigned irq)
209{ 233{
210 /* irq can never have been returned from gpio_to_irq() */ 234 /* irq can never have been returned from gpio_to_irq() */
@@ -236,14 +260,25 @@ gpiochip_remove_pin_ranges(struct gpio_chip *chip)
236 WARN_ON(1); 260 WARN_ON(1);
237} 261}
238 262
239#endif /* ! CONFIG_GPIOLIB */ 263static inline int devm_gpio_request(struct device *dev, unsigned gpio,
264 const char *label)
265{
266 WARN_ON(1);
267 return -EINVAL;
268}
240 269
241struct device; 270static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
271 unsigned long flags, const char *label)
272{
273 WARN_ON(1);
274 return -EINVAL;
275}
242 276
243/* bindings for managed devices that want to request gpios */ 277static inline void devm_gpio_free(struct device *dev, unsigned int gpio)
244int devm_gpio_request(struct device *dev, unsigned gpio, const char *label); 278{
245int devm_gpio_request_one(struct device *dev, unsigned gpio, 279 WARN_ON(1);
246 unsigned long flags, const char *label); 280}
247void devm_gpio_free(struct device *dev, unsigned int gpio); 281
282#endif /* ! CONFIG_GPIOLIB */
248 283
249#endif /* __LINUX_GPIO_H */ 284#endif /* __LINUX_GPIO_H */
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
new file mode 100644
index 000000000000..4d34dbbbad4d
--- /dev/null
+++ b/include/linux/gpio/consumer.h
@@ -0,0 +1,253 @@
1#ifndef __LINUX_GPIO_CONSUMER_H
2#define __LINUX_GPIO_CONSUMER_H
3
4#include <linux/err.h>
5#include <linux/kernel.h>
6
7#ifdef CONFIG_GPIOLIB
8
9struct device;
10struct gpio_chip;
11
12/**
13 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
14 * preferable to the old integer-based handles.
15 *
16 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
17 * until the GPIO is released.
18 */
19struct gpio_desc;
20
21/* Acquire and dispose GPIOs */
22struct gpio_desc *__must_check gpiod_get(struct device *dev,
23 const char *con_id);
24struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
25 const char *con_id,
26 unsigned int idx);
27void gpiod_put(struct gpio_desc *desc);
28
29struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
30 const char *con_id);
31struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
32 const char *con_id,
33 unsigned int idx);
34void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
35
36int gpiod_get_direction(const struct gpio_desc *desc);
37int gpiod_direction_input(struct gpio_desc *desc);
38int gpiod_direction_output(struct gpio_desc *desc, int value);
39
40/* Value get/set from non-sleeping context */
41int gpiod_get_value(const struct gpio_desc *desc);
42void gpiod_set_value(struct gpio_desc *desc, int value);
43int gpiod_get_raw_value(const struct gpio_desc *desc);
44void gpiod_set_raw_value(struct gpio_desc *desc, int value);
45
46/* Value get/set from sleeping context */
47int gpiod_get_value_cansleep(const struct gpio_desc *desc);
48void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
49int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
50void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
51
52int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
53
54int gpiod_is_active_low(const struct gpio_desc *desc);
55int gpiod_cansleep(const struct gpio_desc *desc);
56
57int gpiod_to_irq(const struct gpio_desc *desc);
58
59/* Convert between the old gpio_ and new gpiod_ interfaces */
60struct gpio_desc *gpio_to_desc(unsigned gpio);
61int desc_to_gpio(const struct gpio_desc *desc);
62struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
63
64#else /* CONFIG_GPIOLIB */
65
66static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
67 const char *con_id)
68{
69 return ERR_PTR(-ENOSYS);
70}
71static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
72 const char *con_id,
73 unsigned int idx)
74{
75 return ERR_PTR(-ENOSYS);
76}
77static inline void gpiod_put(struct gpio_desc *desc)
78{
79 might_sleep();
80
81 /* GPIO can never have been requested */
82 WARN_ON(1);
83}
84
85static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
86 const char *con_id)
87{
88 return ERR_PTR(-ENOSYS);
89}
90static inline
91struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
92 const char *con_id,
93 unsigned int idx)
94{
95 return ERR_PTR(-ENOSYS);
96}
97static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
98{
99 might_sleep();
100
101 /* GPIO can never have been requested */
102 WARN_ON(1);
103}
104
105
106static inline int gpiod_get_direction(const struct gpio_desc *desc)
107{
108 /* GPIO can never have been requested */
109 WARN_ON(1);
110 return -ENOSYS;
111}
112static inline int gpiod_direction_input(struct gpio_desc *desc)
113{
114 /* GPIO can never have been requested */
115 WARN_ON(1);
116 return -ENOSYS;
117}
118static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
119{
120 /* GPIO can never have been requested */
121 WARN_ON(1);
122 return -ENOSYS;
123}
124
125
126static inline int gpiod_get_value(const struct gpio_desc *desc)
127{
128 /* GPIO can never have been requested */
129 WARN_ON(1);
130 return 0;
131}
132static inline void gpiod_set_value(struct gpio_desc *desc, int value)
133{
134 /* GPIO can never have been requested */
135 WARN_ON(1);
136}
137static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
138{
139 /* GPIO can never have been requested */
140 WARN_ON(1);
141 return 0;
142}
143static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
144{
145 /* GPIO can never have been requested */
146 WARN_ON(1);
147}
148
149static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
150{
151 /* GPIO can never have been requested */
152 WARN_ON(1);
153 return 0;
154}
155static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
156{
157 /* GPIO can never have been requested */
158 WARN_ON(1);
159}
160static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
161{
162 /* GPIO can never have been requested */
163 WARN_ON(1);
164 return 0;
165}
166static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
167 int value)
168{
169 /* GPIO can never have been requested */
170 WARN_ON(1);
171}
172
173static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
174{
175 /* GPIO can never have been requested */
176 WARN_ON(1);
177 return -ENOSYS;
178}
179
180static inline int gpiod_is_active_low(const struct gpio_desc *desc)
181{
182 /* GPIO can never have been requested */
183 WARN_ON(1);
184 return 0;
185}
186static inline int gpiod_cansleep(const struct gpio_desc *desc)
187{
188 /* GPIO can never have been requested */
189 WARN_ON(1);
190 return 0;
191}
192
193static inline int gpiod_to_irq(const struct gpio_desc *desc)
194{
195 /* GPIO can never have been requested */
196 WARN_ON(1);
197 return -EINVAL;
198}
199
200static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
201{
202 return ERR_PTR(-EINVAL);
203}
204static inline int desc_to_gpio(const struct gpio_desc *desc)
205{
206 /* GPIO can never have been requested */
207 WARN_ON(1);
208 return -EINVAL;
209}
210static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
211{
212 /* GPIO can never have been requested */
213 WARN_ON(1);
214 return ERR_PTR(-ENODEV);
215}
216
217
218#endif /* CONFIG_GPIOLIB */
219
220#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
221
222int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
223int gpiod_export_link(struct device *dev, const char *name,
224 struct gpio_desc *desc);
225int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
226void gpiod_unexport(struct gpio_desc *desc);
227
228#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
229
230static inline int gpiod_export(struct gpio_desc *desc,
231 bool direction_may_change)
232{
233 return -ENOSYS;
234}
235
236static inline int gpiod_export_link(struct device *dev, const char *name,
237 struct gpio_desc *desc)
238{
239 return -ENOSYS;
240}
241
242static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
243{
244 return -ENOSYS;
245}
246
247static inline void gpiod_unexport(struct gpio_desc *desc)
248{
249}
250
251#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
252
253#endif
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
new file mode 100644
index 000000000000..656a27efb2c8
--- /dev/null
+++ b/include/linux/gpio/driver.h
@@ -0,0 +1,184 @@
1#ifndef __LINUX_GPIO_DRIVER_H
2#define __LINUX_GPIO_DRIVER_H
3
4#include <linux/types.h>
5
6struct device;
7struct gpio_desc;
8struct seq_file;
9
10/**
11 * struct gpio_chip - abstract a GPIO controller
12 * @label: for diagnostics
13 * @dev: optional device providing the GPIOs
14 * @owner: helps prevent removal of modules exporting active GPIOs
15 * @list: links gpio_chips together for traversal
16 * @request: optional hook for chip-specific activation, such as
17 * enabling module power and clock; may sleep
18 * @free: optional hook for chip-specific deactivation, such as
19 * disabling module power and clock; may sleep
20 * @get_direction: returns direction for signal "offset", 0=out, 1=in,
21 * (same as GPIOF_DIR_XXX), or negative error
22 * @direction_input: configures signal "offset" as input, or returns error
23 * @direction_output: configures signal "offset" as output, or returns error
24 * @get: returns value for signal "offset"; for output signals this
25 * returns either the value actually sensed, or zero
26 * @set: assigns output value for signal "offset"
27 * @set_debounce: optional hook for setting debounce time for specified gpio in
28 * interrupt triggered gpio chips
29 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
30 * implementation may not sleep
31 * @dbg_show: optional routine to show contents in debugfs; default code
32 * will be used when this is omitted, but custom code can show extra
33 * state (such as pullup/pulldown configuration).
34 * @base: identifies the first GPIO number handled by this chip; or, if
35 * negative during registration, requests dynamic ID allocation.
36 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
37 * handled is (base + ngpio - 1).
38 * @desc: array of ngpio descriptors. Private.
39 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
40 * must while accessing GPIO expander chips over I2C or SPI
41 * @names: if set, must be an array of strings to use as alternative
42 * names for the GPIOs in this chip. Any entry in the array
43 * may be NULL if there is no alias for the GPIO, however the
44 * array must be @ngpio entries long. A name can include a single printk
45 * format specifier for an unsigned int. It is substituted by the actual
46 * number of the gpio.
47 *
48 * A gpio_chip can help platforms abstract various sources of GPIOs so
49 * they can all be accessed through a common programing interface.
50 * Example sources would be SOC controllers, FPGAs, multifunction
51 * chips, dedicated GPIO expanders, and so on.
52 *
53 * Each chip controls a number of signals, identified in method calls
54 * by "offset" values in the range 0..(@ngpio - 1). When those signals
55 * are referenced through calls like gpio_get_value(gpio), the offset
56 * is calculated by subtracting @base from the gpio number.
57 */
58struct gpio_chip {
59 const char *label;
60 struct device *dev;
61 struct module *owner;
62 struct list_head list;
63
64 int (*request)(struct gpio_chip *chip,
65 unsigned offset);
66 void (*free)(struct gpio_chip *chip,
67 unsigned offset);
68 int (*get_direction)(struct gpio_chip *chip,
69 unsigned offset);
70 int (*direction_input)(struct gpio_chip *chip,
71 unsigned offset);
72 int (*direction_output)(struct gpio_chip *chip,
73 unsigned offset, int value);
74 int (*get)(struct gpio_chip *chip,
75 unsigned offset);
76 void (*set)(struct gpio_chip *chip,
77 unsigned offset, int value);
78 int (*set_debounce)(struct gpio_chip *chip,
79 unsigned offset,
80 unsigned debounce);
81
82 int (*to_irq)(struct gpio_chip *chip,
83 unsigned offset);
84
85 void (*dbg_show)(struct seq_file *s,
86 struct gpio_chip *chip);
87 int base;
88 u16 ngpio;
89 struct gpio_desc *desc;
90 const char *const *names;
91 unsigned can_sleep:1;
92 unsigned exported:1;
93
94#if defined(CONFIG_OF_GPIO)
95 /*
96 * If CONFIG_OF is enabled, then all GPIO controllers described in the
97 * device tree automatically may have an OF translation
98 */
99 struct device_node *of_node;
100 int of_gpio_n_cells;
101 int (*of_xlate)(struct gpio_chip *gc,
102 const struct of_phandle_args *gpiospec, u32 *flags);
103#endif
104#ifdef CONFIG_PINCTRL
105 /*
106 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
107 * describe the actual pin range which they serve in an SoC. This
108 * information would be used by pinctrl subsystem to configure
109 * corresponding pins for gpio usage.
110 */
111 struct list_head pin_ranges;
112#endif
113};
114
115extern const char *gpiochip_is_requested(struct gpio_chip *chip,
116 unsigned offset);
117
118/* add/remove chips */
119extern int gpiochip_add(struct gpio_chip *chip);
120extern int __must_check gpiochip_remove(struct gpio_chip *chip);
121extern struct gpio_chip *gpiochip_find(void *data,
122 int (*match)(struct gpio_chip *chip, void *data));
123
124/* lock/unlock as IRQ */
125int gpiod_lock_as_irq(struct gpio_desc *desc);
126void gpiod_unlock_as_irq(struct gpio_desc *desc);
127
128/**
129 * Lookup table for associating GPIOs to specific devices and functions using
130 * platform data.
131 */
132struct gpiod_lookup {
133 struct list_head list;
134 /*
135 * name of the chip the GPIO belongs to
136 */
137 const char *chip_label;
138 /*
139 * hardware number (i.e. relative to the chip) of the GPIO
140 */
141 u16 chip_hwnum;
142 /*
143 * name of device that can claim this GPIO
144 */
145 const char *dev_id;
146 /*
147 * name of the GPIO from the device's point of view
148 */
149 const char *con_id;
150 /*
151 * index of the GPIO in case several GPIOs share the same name
152 */
153 unsigned int idx;
154 /*
155 * mask of GPIOF_* values
156 */
157 unsigned long flags;
158};
159
160/*
161 * Simple definition of a single GPIO under a con_id
162 */
163#define GPIO_LOOKUP(_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \
164 GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags)
165
166/*
167 * Use this macro if you need to have several GPIOs under the same con_id.
168 * Each GPIO needs to use a different index and can be accessed using
169 * gpiod_get_index()
170 */
171#define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \
172 _flags) \
173{ \
174 .chip_label = _chip_label, \
175 .chip_hwnum = _chip_hwnum, \
176 .dev_id = _dev_id, \
177 .con_id = _con_id, \
178 .idx = _idx, \
179 .flags = _flags, \
180}
181
182void gpiod_add_table(struct gpiod_lookup *table, size_t size);
183
184#endif
diff --git a/include/linux/of_gpio.h b/include/linux/of_gpio.h
index a83dc6f5008e..f14123a5a9df 100644
--- a/include/linux/of_gpio.h
+++ b/include/linux/of_gpio.h
@@ -19,6 +19,7 @@
19#include <linux/errno.h> 19#include <linux/errno.h>
20#include <linux/gpio.h> 20#include <linux/gpio.h>
21#include <linux/of.h> 21#include <linux/of.h>
22#include <linux/gpio/consumer.h>
22 23
23struct device_node; 24struct device_node;
24 25
@@ -47,7 +48,7 @@ static inline struct of_mm_gpio_chip *to_of_mm_gpio_chip(struct gpio_chip *gc)
47 return container_of(gc, struct of_mm_gpio_chip, gc); 48 return container_of(gc, struct of_mm_gpio_chip, gc);
48} 49}
49 50
50extern int of_get_named_gpio_flags(struct device_node *np, 51extern struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
51 const char *list_name, int index, enum of_gpio_flags *flags); 52 const char *list_name, int index, enum of_gpio_flags *flags);
52 53
53extern int of_mm_gpiochip_add(struct device_node *np, 54extern int of_mm_gpiochip_add(struct device_node *np,
@@ -62,10 +63,10 @@ extern int of_gpio_simple_xlate(struct gpio_chip *gc,
62#else /* CONFIG_OF_GPIO */ 63#else /* CONFIG_OF_GPIO */
63 64
64/* Drivers may not strictly depend on the GPIO support, so let them link. */ 65/* Drivers may not strictly depend on the GPIO support, so let them link. */
65static inline int of_get_named_gpio_flags(struct device_node *np, 66static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
66 const char *list_name, int index, enum of_gpio_flags *flags) 67 const char *list_name, int index, enum of_gpio_flags *flags)
67{ 68{
68 return -ENOSYS; 69 return ERR_PTR(-ENOSYS);
69} 70}
70 71
71static inline int of_gpio_simple_xlate(struct gpio_chip *gc, 72static inline int of_gpio_simple_xlate(struct gpio_chip *gc,
@@ -80,6 +81,18 @@ static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
80 81
81#endif /* CONFIG_OF_GPIO */ 82#endif /* CONFIG_OF_GPIO */
82 83
84static inline int of_get_named_gpio_flags(struct device_node *np,
85 const char *list_name, int index, enum of_gpio_flags *flags)
86{
87 struct gpio_desc *desc;
88 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
89
90 if (IS_ERR(desc))
91 return PTR_ERR(desc);
92 else
93 return desc_to_gpio(desc);
94}
95
83/** 96/**
84 * of_gpio_named_count() - Count GPIOs for a device 97 * of_gpio_named_count() - Count GPIOs for a device
85 * @np: device node to count GPIOs for 98 * @np: device node to count GPIOs for
@@ -117,15 +130,21 @@ static inline int of_gpio_count(struct device_node *np)
117} 130}
118 131
119/** 132/**
120 * of_get_gpio_flags() - Get a GPIO number and flags to use with GPIO API 133 * of_get_gpiod_flags() - Get a GPIO descriptor and flags to use with GPIO API
121 * @np: device node to get GPIO from 134 * @np: device node to get GPIO from
122 * @index: index of the GPIO 135 * @index: index of the GPIO
123 * @flags: a flags pointer to fill in 136 * @flags: a flags pointer to fill in
124 * 137 *
125 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 138 * Returns GPIO descriptor to use with Linux generic GPIO API, or a errno
126 * value on the error condition. If @flags is not NULL the function also fills 139 * value on the error condition. If @flags is not NULL the function also fills
127 * in flags for the GPIO. 140 * in flags for the GPIO.
128 */ 141 */
142static inline struct gpio_desc *of_get_gpiod_flags(struct device_node *np,
143 int index, enum of_gpio_flags *flags)
144{
145 return of_get_named_gpiod_flags(np, "gpios", index, flags);
146}
147
129static inline int of_get_gpio_flags(struct device_node *np, int index, 148static inline int of_get_gpio_flags(struct device_node *np, int index,
130 enum of_gpio_flags *flags) 149 enum of_gpio_flags *flags)
131{ 150{