aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-11-14 20:23:44 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2017-11-14 20:23:44 -0500
commit6aa2f9441f1ef21f10c41f45e6453b135e9cd736 (patch)
tree334e67c4693eddff47a098b9afad63ca2ccfcd55
parente37e0ee0190034a059c9faea8adfb4982fb24ddd (diff)
parent24f0966c3e3f52a96e888504d60810d9df5b2d42 (diff)
Merge tag 'gpio-v4.15-1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio
Pull GPIO updates from Linus Walleij: "This is the bulk of GPIO changes for the v4.15 kernel cycle: Core: - Fix the semantics of raw GPIO to actually be raw. No inversion semantics as before, but also no open draining, and allow the raw operations to affect lines used for interrupts as the caller supposedly knows what they are doing if they are getting the big hammer. - Rewrote the __inner_function() notation calls to names that make more sense. I just find this kind of code disturbing. - Drop the .irq_base() field from the gpiochip since now all IRQs are mapped dynamically. This is nice. - Support for .get_multiple() in the core driver API. This allows us to read several GPIO lines with a single register read. This has high value for some usecases: it can be used to create oscilloscopes and signal analyzers and other things that rely on reading several lines at exactly the same instant. Also a generally nice optimization. This uses the new assign_bit() macro from the bitops lib that was ACKed by Andrew Morton and is implemented for two drivers, one of them being the generic MMIO driver so everyone using that will be able to benefit from this. - Do not allow requests of Open Drain and Open Source setting of a GPIO line simultaneously. If the hardware actually supports enabling both at the same time the electrical result would be disastrous. - A new interrupt chip core helper. This will be helpful to deal with "banked" GPIOs, which means GPIO controllers with several logical blocks of GPIO inside them. This is several gpiochips per device in the device model, in contrast to the case when there is a 1-to-1 relationship between a device and a gpiochip. New drivers: - Maxim MAX3191x industrial serializer, a very interesting piece of professional I/O hardware. - Uniphier GPIO driver. This is the GPIO block from the recent Socionext (ex Fujitsu and Panasonic) platform. - Tegra 186 driver. This is based on the new banked GPIO infrastructure. Other improvements: - Some documentation improvements. - Wakeup support for the DesignWare DWAPB GPIO controller. - Reset line support on the DesignWare DWAPB GPIO controller. - Several non-critical bug fixes and improvements for the Broadcom BRCMSTB driver. - Misc non-critical bug fixes like exotic errorpaths, removal of dead code etc. - Explicit comments on fall-through switch() statements" * tag 'gpio-v4.15-1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (65 commits) gpio: tegra186: Remove tegra186_gpio_lock_class gpio: rcar: Add r8a77995 (R-Car D3) support pinctrl: bcm2835: Fix some merge fallout gpio: Fix undefined lock_dep_class gpio: Automatically add lockdep keys gpio: Introduce struct gpio_irq_chip.first gpio: Disambiguate struct gpio_irq_chip.nested gpio: Add Tegra186 support gpio: Export gpiochip_irq_{map,unmap}() gpio: Implement tighter IRQ chip integration gpio: Move lock_key into struct gpio_irq_chip gpio: Move irq_valid_mask into struct gpio_irq_chip gpio: Move irq_nested into struct gpio_irq_chip gpio: Move irq_chained_parent to struct gpio_irq_chip gpio: Move irq_default_type to struct gpio_irq_chip gpio: Move irq_handler to struct gpio_irq_chip gpio: Move irqdomain into struct gpio_irq_chip gpio: Move irqchip into struct gpio_irq_chip gpio: Introduce struct gpio_irq_chip pinctrl: armada-37xx: remove unused variable ...
-rw-r--r--Documentation/ABI/obsolete/sysfs-gpio2
-rw-r--r--Documentation/devicetree/bindings/common-properties.txt26
-rw-r--r--Documentation/devicetree/bindings/gpio/gpio-max3191x.txt59
-rw-r--r--Documentation/devicetree/bindings/gpio/gpio-uniphier.txt52
-rw-r--r--Documentation/devicetree/bindings/gpio/renesas,gpio-rcar.txt2
-rw-r--r--Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt1
-rw-r--r--Documentation/gpio/consumer.txt63
-rw-r--r--Documentation/gpio/driver.txt6
-rw-r--r--MAINTAINERS9
-rw-r--r--drivers/bcma/driver_gpio.c2
-rw-r--r--drivers/gpio/Kconfig32
-rw-r--r--drivers/gpio/Makefile3
-rw-r--r--drivers/gpio/gpio-104-dio-48e.c2
-rw-r--r--drivers/gpio/gpio-104-idi-48.c2
-rw-r--r--drivers/gpio/gpio-104-idio-16.c2
-rw-r--r--drivers/gpio/gpio-adnp.c31
-rw-r--r--drivers/gpio/gpio-altera.c4
-rw-r--r--drivers/gpio/gpio-aspeed.c9
-rw-r--r--drivers/gpio/gpio-ath79.c3
-rw-r--r--drivers/gpio/gpio-brcmstb.c422
-rw-r--r--drivers/gpio/gpio-crystalcove.c2
-rw-r--r--drivers/gpio/gpio-dln2.c2
-rw-r--r--drivers/gpio/gpio-dwapb.c36
-rw-r--r--drivers/gpio/gpio-ftgpio010.c2
-rw-r--r--drivers/gpio/gpio-grgpio.c6
-rw-r--r--drivers/gpio/gpio-ingenic.c2
-rw-r--r--drivers/gpio/gpio-intel-mid.c2
-rw-r--r--drivers/gpio/gpio-loongson1.c7
-rw-r--r--drivers/gpio/gpio-lynxpoint.c2
-rw-r--r--drivers/gpio/gpio-max3191x.c492
-rw-r--r--drivers/gpio/gpio-max732x.c2
-rw-r--r--drivers/gpio/gpio-mb86s7x.c12
-rw-r--r--drivers/gpio/gpio-merrifield.c2
-rw-r--r--drivers/gpio/gpio-mmio.c130
-rw-r--r--drivers/gpio/gpio-mpc8xxx.c23
-rw-r--r--drivers/gpio/gpio-omap.c2
-rw-r--r--drivers/gpio/gpio-pca953x.c2
-rw-r--r--drivers/gpio/gpio-pcf857x.c2
-rw-r--r--drivers/gpio/gpio-pci-idio-16.c2
-rw-r--r--drivers/gpio/gpio-pl061.c2
-rw-r--r--drivers/gpio/gpio-rcar.c24
-rw-r--r--drivers/gpio/gpio-reg.c4
-rw-r--r--drivers/gpio/gpio-stmpe.c10
-rw-r--r--drivers/gpio/gpio-tb10x.c3
-rw-r--r--drivers/gpio/gpio-tc3589x.c2
-rw-r--r--drivers/gpio/gpio-tegra186.c620
-rw-r--r--drivers/gpio/gpio-thunderx.c13
-rw-r--r--drivers/gpio/gpio-uniphier.c508
-rw-r--r--drivers/gpio/gpio-vf610.c2
-rw-r--r--drivers/gpio/gpio-wcove.c2
-rw-r--r--drivers/gpio/gpio-ws16c48.c2
-rw-r--r--drivers/gpio/gpio-xgene-sb.c15
-rw-r--r--drivers/gpio/gpio-xlp.c2
-rw-r--r--drivers/gpio/gpio-zx.c2
-rw-r--r--drivers/gpio/gpio-zynq.c2
-rw-r--r--drivers/gpio/gpiolib-of.c4
-rw-r--r--drivers/gpio/gpiolib.c582
-rw-r--r--drivers/gpio/gpiolib.h6
-rw-r--r--drivers/md/dm-mpath.c22
-rw-r--r--drivers/pinctrl/bcm/pinctrl-bcm2835.c4
-rw-r--r--drivers/pinctrl/bcm/pinctrl-iproc-gpio.c2
-rw-r--r--drivers/pinctrl/intel/pinctrl-baytrail.c6
-rw-r--r--drivers/pinctrl/intel/pinctrl-cherryview.c6
-rw-r--r--drivers/pinctrl/intel/pinctrl-intel.c2
-rw-r--r--drivers/pinctrl/mvebu/pinctrl-armada-37xx.c6
-rw-r--r--drivers/pinctrl/nomadik/pinctrl-nomadik.c4
-rw-r--r--drivers/pinctrl/pinctrl-amd.c2
-rw-r--r--drivers/pinctrl/pinctrl-at91.c2
-rw-r--r--drivers/pinctrl/pinctrl-coh901.c2
-rw-r--r--drivers/pinctrl/pinctrl-mcp23s08.c2
-rw-r--r--drivers/pinctrl/pinctrl-oxnas.c2
-rw-r--r--drivers/pinctrl/pinctrl-pic32.c2
-rw-r--r--drivers/pinctrl/pinctrl-pistachio.c2
-rw-r--r--drivers/pinctrl/pinctrl-st.c2
-rw-r--r--drivers/pinctrl/pinctrl-sx150x.c2
-rw-r--r--drivers/pinctrl/qcom/pinctrl-msm.c2
-rw-r--r--drivers/pinctrl/sirf/pinctrl-atlas7.c2
-rw-r--r--drivers/pinctrl/sirf/pinctrl-sirf.c2
-rw-r--r--drivers/pinctrl/spear/pinctrl-plgpio.c2
-rw-r--r--drivers/platform/x86/intel_int0002_vgpio.c6
-rw-r--r--include/dt-bindings/gpio/gpio.h2
-rw-r--r--include/dt-bindings/gpio/uniphier-gpio.h18
-rw-r--r--include/linux/bitops.h24
-rw-r--r--include/linux/gpio/consumer.h43
-rw-r--r--include/linux/gpio/driver.h215
-rw-r--r--include/linux/gpio/machine.h2
-rw-r--r--include/linux/of_gpio.h2
-rw-r--r--tools/gpio/gpio-utils.c17
88 files changed, 3158 insertions, 516 deletions
diff --git a/Documentation/ABI/obsolete/sysfs-gpio b/Documentation/ABI/obsolete/sysfs-gpio
index 867c1fab20e2..32513dc2eec9 100644
--- a/Documentation/ABI/obsolete/sysfs-gpio
+++ b/Documentation/ABI/obsolete/sysfs-gpio
@@ -11,7 +11,7 @@ Description:
11 Kernel code may export it for complete or partial access. 11 Kernel code may export it for complete or partial access.
12 12
13 GPIOs are identified as they are inside the kernel, using integers in 13 GPIOs are identified as they are inside the kernel, using integers in
14 the range 0..INT_MAX. See Documentation/gpio.txt for more information. 14 the range 0..INT_MAX. See Documentation/gpio/gpio.txt for more information.
15 15
16 /sys/class/gpio 16 /sys/class/gpio
17 /export ... asks the kernel to export a GPIO to userspace 17 /export ... asks the kernel to export a GPIO to userspace
diff --git a/Documentation/devicetree/bindings/common-properties.txt b/Documentation/devicetree/bindings/common-properties.txt
index 697714f8d75c..a3448bfa1c82 100644
--- a/Documentation/devicetree/bindings/common-properties.txt
+++ b/Documentation/devicetree/bindings/common-properties.txt
@@ -1,4 +1,8 @@
1Common properties 1Common properties
2=================
3
4Endianness
5----------
2 6
3The Devicetree Specification does not define any properties related to hardware 7The Devicetree Specification does not define any properties related to hardware
4byteswapping, but endianness issues show up frequently in porting Linux to 8byteswapping, but endianness issues show up frequently in porting Linux to
@@ -58,3 +62,25 @@ dev: dev@40031000 {
58 ... 62 ...
59 little-endian; 63 little-endian;
60}; 64};
65
66Daisy-chained devices
67---------------------
68
69Many serially-attached GPIO and IIO devices are daisy-chainable. To the
70host controller, a daisy-chain appears as a single device, but the number
71of inputs and outputs it provides is the sum of inputs and outputs provided
72by all of its devices. The driver needs to know how many devices the
73daisy-chain comprises to determine the amount of data exchanged, how many
74inputs and outputs to register and so on.
75
76Optional properties:
77 - #daisy-chained-devices: Number of devices in the daisy-chain (default is 1).
78
79Example:
80gpio@0 {
81 compatible = "name";
82 reg = <0>;
83 gpio-controller;
84 #gpio-cells = <2>;
85 #daisy-chained-devices = <3>;
86};
diff --git a/Documentation/devicetree/bindings/gpio/gpio-max3191x.txt b/Documentation/devicetree/bindings/gpio/gpio-max3191x.txt
new file mode 100644
index 000000000000..b3a6444b8f45
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio-max3191x.txt
@@ -0,0 +1,59 @@
1GPIO driver for Maxim MAX3191x industrial serializer
2
3Required properties:
4 - compatible: Must be one of:
5 "maxim,max31910"
6 "maxim,max31911"
7 "maxim,max31912"
8 "maxim,max31913"
9 "maxim,max31953"
10 "maxim,max31963"
11 - reg: Chip select number.
12 - gpio-controller: Marks the device node as a GPIO controller.
13 - #gpio-cells: Should be two. For consumer use see gpio.txt.
14
15Optional properties:
16 - #daisy-chained-devices:
17 Number of chips in the daisy-chain (default is 1).
18 - maxim,modesel-gpios: GPIO pins to configure modesel of each chip.
19 The number of GPIOs must equal "#daisy-chained-devices"
20 (if each chip is driven by a separate pin) or 1
21 (if all chips are wired to the same pin).
22 - maxim,fault-gpios: GPIO pins to read fault of each chip.
23 The number of GPIOs must equal "#daisy-chained-devices"
24 or 1.
25 - maxim,db0-gpios: GPIO pins to configure debounce of each chip.
26 The number of GPIOs must equal "#daisy-chained-devices"
27 or 1.
28 - maxim,db1-gpios: GPIO pins to configure debounce of each chip.
29 The number of GPIOs must equal "maxim,db0-gpios".
30 - maxim,modesel-8bit: Boolean whether the modesel pin of the chips is
31 pulled high (8-bit mode). Use this if the modesel pin
32 is hardwired and consequently "maxim,modesel-gpios"
33 cannot be specified. By default if neither this nor
34 "maxim,modesel-gpios" is given, the driver assumes
35 that modesel is pulled low (16-bit mode).
36 - maxim,ignore-undervoltage:
37 Boolean whether to ignore undervoltage alarms signaled
38 by the "maxim,fault-gpios" or by the status byte
39 (in 16-bit mode). Use this if the chips are powered
40 through 5VOUT instead of VCC24V, in which case they
41 will constantly signal undervoltage.
42
43For other required and optional properties of SPI slave nodes please refer to
44../spi/spi-bus.txt.
45
46Example:
47 gpio@0 {
48 compatible = "maxim,max31913";
49 reg = <0>;
50 gpio-controller;
51 #gpio-cells = <2>;
52
53 maxim,modesel-gpios = <&gpio2 23>;
54 maxim,fault-gpios = <&gpio2 24 GPIO_ACTIVE_LOW>;
55 maxim,db0-gpios = <&gpio2 25>;
56 maxim,db1-gpios = <&gpio2 26>;
57
58 spi-max-frequency = <25000000>;
59 };
diff --git a/Documentation/devicetree/bindings/gpio/gpio-uniphier.txt b/Documentation/devicetree/bindings/gpio/gpio-uniphier.txt
new file mode 100644
index 000000000000..fed9158dd913
--- /dev/null
+++ b/Documentation/devicetree/bindings/gpio/gpio-uniphier.txt
@@ -0,0 +1,52 @@
1UniPhier GPIO controller
2
3Required properties:
4- compatible: Should be "socionext,uniphier-gpio".
5- reg: Specifies offset and length of the register set for the device.
6- gpio-controller: Marks the device node as a GPIO controller.
7- #gpio-cells: Should be 2. The first cell is the pin number and the second
8 cell is used to specify optional parameters.
9- interrupt-parent: Specifies the parent interrupt controller.
10- interrupt-controller: Marks the device node as an interrupt controller.
11- #interrupt-cells: Should be 2. The first cell defines the interrupt number.
12 The second cell bits[3:0] is used to specify trigger type as follows:
13 1 = low-to-high edge triggered
14 2 = high-to-low edge triggered
15 4 = active high level-sensitive
16 8 = active low level-sensitive
17 Valid combinations are 1, 2, 3, 4, 8.
18- ngpios: Specifies the number of GPIO lines.
19- gpio-ranges: Mapping to pin controller pins (as described in gpio.txt)
20- socionext,interrupt-ranges: Specifies an interrupt number mapping between
21 this GPIO controller and its interrupt parent, in the form of arbitrary
22 number of <child-interrupt-base parent-interrupt-base length> triplets.
23
24Optional properties:
25- gpio-ranges-group-names: Used for named gpio ranges (as described in gpio.txt)
26
27Example:
28 gpio: gpio@55000000 {
29 compatible = "socionext,uniphier-gpio";
30 reg = <0x55000000 0x200>;
31 interrupt-parent = <&aidet>;
32 interrupt-controller;
33 #interrupt-cells = <2>;
34 gpio-controller;
35 #gpio-cells = <2>;
36 gpio-ranges = <&pinctrl 0 0 0>;
37 gpio-ranges-group-names = "gpio_range";
38 ngpios = <248>;
39 socionext,interrupt-ranges = <0 48 16>, <16 154 5>, <21 217 3>;
40 };
41
42Consumer Example:
43
44 sdhci0_pwrseq {
45 compatible = "mmc-pwrseq-emmc";
46 reset-gpios = <&gpio UNIPHIER_GPIO_PORT(29, 4) GPIO_ACTIVE_LOW>;
47 };
48
49Please note UNIPHIER_GPIO_PORT(29, 4) represents PORT294 in the SoC document.
50Unfortunately, only the one's place is octal in the port numbering. (That is,
51PORT 8, 9, 18, 19, 28, 29, ... are missing.) UNIPHIER_GPIO_PORT() is a helper
52macro to calculate 29 * 8 + 4.
diff --git a/Documentation/devicetree/bindings/gpio/renesas,gpio-rcar.txt b/Documentation/devicetree/bindings/gpio/renesas,gpio-rcar.txt
index 51c86f69995e..a7ac460ad657 100644
--- a/Documentation/devicetree/bindings/gpio/renesas,gpio-rcar.txt
+++ b/Documentation/devicetree/bindings/gpio/renesas,gpio-rcar.txt
@@ -14,6 +14,8 @@ Required Properties:
14 - "renesas,gpio-r8a7794": for R8A7794 (R-Car E2) compatible GPIO controller. 14 - "renesas,gpio-r8a7794": for R8A7794 (R-Car E2) compatible GPIO controller.
15 - "renesas,gpio-r8a7795": for R8A7795 (R-Car H3) compatible GPIO controller. 15 - "renesas,gpio-r8a7795": for R8A7795 (R-Car H3) compatible GPIO controller.
16 - "renesas,gpio-r8a7796": for R8A7796 (R-Car M3-W) compatible GPIO controller. 16 - "renesas,gpio-r8a7796": for R8A7796 (R-Car M3-W) compatible GPIO controller.
17 - "renesas,gpio-r8a77970": for R8A77970 (R-Car V3M) compatible GPIO controller.
18 - "renesas,gpio-r8a77995": for R8A77995 (R-Car D3) compatible GPIO controller.
17 - "renesas,rcar-gen1-gpio": for a generic R-Car Gen1 GPIO controller. 19 - "renesas,rcar-gen1-gpio": for a generic R-Car Gen1 GPIO controller.
18 - "renesas,rcar-gen2-gpio": for a generic R-Car Gen2 or RZ/G1 GPIO controller. 20 - "renesas,rcar-gen2-gpio": for a generic R-Car Gen2 or RZ/G1 GPIO controller.
19 - "renesas,rcar-gen3-gpio": for a generic R-Car Gen3 GPIO controller. 21 - "renesas,rcar-gen3-gpio": for a generic R-Car Gen3 GPIO controller.
diff --git a/Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt b/Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt
index 4d6c8cdc8586..4a75da7051bd 100644
--- a/Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt
+++ b/Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt
@@ -29,6 +29,7 @@ controller.
29- interrupts : The interrupt to the parent controller raised when GPIOs 29- interrupts : The interrupt to the parent controller raised when GPIOs
30 generate the interrupts. 30 generate the interrupts.
31- snps,nr-gpios : The number of pins in the port, a single cell. 31- snps,nr-gpios : The number of pins in the port, a single cell.
32- resets : Reset line for the controller.
32 33
33Example: 34Example:
34 35
diff --git a/Documentation/gpio/consumer.txt b/Documentation/gpio/consumer.txt
index 912568baabb9..63e1bd1d88e3 100644
--- a/Documentation/gpio/consumer.txt
+++ b/Documentation/gpio/consumer.txt
@@ -10,14 +10,30 @@ Guidelines for GPIOs consumers
10============================== 10==============================
11 11
12Drivers that can't work without standard GPIO calls should have Kconfig entries 12Drivers that can't work without standard GPIO calls should have Kconfig entries
13that depend on GPIOLIB. The functions that allow a driver to obtain and use 13that depend on GPIOLIB or select GPIOLIB. The functions that allow a driver to
14GPIOs are available by including the following file: 14obtain and use GPIOs are available by including the following file:
15 15
16 #include <linux/gpio/consumer.h> 16 #include <linux/gpio/consumer.h>
17 17
18There are static inline stubs for all functions in the header file in the case
19where GPIOLIB is disabled. When these stubs are called they will emit
20warnings. These stubs are used for two use cases:
21
22- Simple compile coverage with e.g. COMPILE_TEST - it does not matter that
23 the current platform does not enable or select GPIOLIB because we are not
24 going to execute the system anyway.
25
26- Truly optional GPIOLIB support - where the driver does not really make use
27 of the GPIOs on certain compile-time configurations for certain systems, but
28 will use it under other compile-time configurations. In this case the
29 consumer must make sure not to call into these functions, or the user will
30 be met with console warnings that may be perceived as intimidating.
31
18All the functions that work with the descriptor-based GPIO interface are 32All the functions that work with the descriptor-based GPIO interface are
19prefixed with gpiod_. The gpio_ prefix is used for the legacy interface. No 33prefixed with gpiod_. The gpio_ prefix is used for the legacy interface. No
20other function in the kernel should use these prefixes. 34other function in the kernel should use these prefixes. The use of the legacy
35functions is strongly discouraged, new code should use <linux/gpio/consumer.h>
36and descriptors exclusively.
21 37
22 38
23Obtaining and Disposing GPIOs 39Obtaining and Disposing GPIOs
@@ -279,9 +295,22 @@ as possible, especially by drivers which should not care about the actual
279physical line level and worry about the logical value instead. 295physical line level and worry about the logical value instead.
280 296
281 297
282Set multiple GPIO outputs with a single function call 298Access multiple GPIOs with a single function call
283----------------------------------------------------- 299-------------------------------------------------
284The following functions set the output values of an array of GPIOs: 300The following functions get or set the values of an array of GPIOs:
301
302 int gpiod_get_array_value(unsigned int array_size,
303 struct gpio_desc **desc_array,
304 int *value_array);
305 int gpiod_get_raw_array_value(unsigned int array_size,
306 struct gpio_desc **desc_array,
307 int *value_array);
308 int gpiod_get_array_value_cansleep(unsigned int array_size,
309 struct gpio_desc **desc_array,
310 int *value_array);
311 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
312 struct gpio_desc **desc_array,
313 int *value_array);
285 314
286 void gpiod_set_array_value(unsigned int array_size, 315 void gpiod_set_array_value(unsigned int array_size,
287 struct gpio_desc **desc_array, 316 struct gpio_desc **desc_array,
@@ -296,34 +325,40 @@ The following functions set the output values of an array of GPIOs:
296 struct gpio_desc **desc_array, 325 struct gpio_desc **desc_array,
297 int *value_array) 326 int *value_array)
298 327
299The array can be an arbitrary set of GPIOs. The functions will try to set 328The array can be an arbitrary set of GPIOs. The functions will try to access
300GPIOs belonging to the same bank or chip simultaneously if supported by the 329GPIOs belonging to the same bank or chip simultaneously if supported by the
301corresponding chip driver. In that case a significantly improved performance 330corresponding chip driver. In that case a significantly improved performance
302can be expected. If simultaneous setting is not possible the GPIOs will be set 331can be expected. If simultaneous access is not possible the GPIOs will be
303sequentially. 332accessed sequentially.
304 333
305The gpiod_set_array() functions take three arguments: 334The functions take three arguments:
306 * array_size - the number of array elements 335 * array_size - the number of array elements
307 * desc_array - an array of GPIO descriptors 336 * desc_array - an array of GPIO descriptors
308 * value_array - an array of values to assign to the GPIOs 337 * value_array - an array to store the GPIOs' values (get) or
338 an array of values to assign to the GPIOs (set)
309 339
310The descriptor array can be obtained using the gpiod_get_array() function 340The descriptor array can be obtained using the gpiod_get_array() function
311or one of its variants. If the group of descriptors returned by that function 341or one of its variants. If the group of descriptors returned by that function
312matches the desired group of GPIOs, those GPIOs can be set by simply using 342matches the desired group of GPIOs, those GPIOs can be accessed by simply using
313the struct gpio_descs returned by gpiod_get_array(): 343the struct gpio_descs returned by gpiod_get_array():
314 344
315 struct gpio_descs *my_gpio_descs = gpiod_get_array(...); 345 struct gpio_descs *my_gpio_descs = gpiod_get_array(...);
316 gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc, 346 gpiod_set_array_value(my_gpio_descs->ndescs, my_gpio_descs->desc,
317 my_gpio_values); 347 my_gpio_values);
318 348
319It is also possible to set a completely arbitrary array of descriptors. The 349It is also possible to access a completely arbitrary array of descriptors. The
320descriptors may be obtained using any combination of gpiod_get() and 350descriptors may be obtained using any combination of gpiod_get() and
321gpiod_get_array(). Afterwards the array of descriptors has to be setup 351gpiod_get_array(). Afterwards the array of descriptors has to be setup
322manually before it can be used with gpiod_set_array(). 352manually before it can be passed to one of the above functions.
323 353
324Note that for optimal performance GPIOs belonging to the same chip should be 354Note that for optimal performance GPIOs belonging to the same chip should be
325contiguous within the array of descriptors. 355contiguous within the array of descriptors.
326 356
357The return value of gpiod_get_array_value() and its variants is 0 on success
358or negative on error. Note the difference to gpiod_get_value(), which returns
3590 or 1 on success to convey the GPIO value. With the array functions, the GPIO
360values are stored in value_array rather than passed back as return value.
361
327 362
328GPIOs mapped to IRQs 363GPIOs mapped to IRQs
329-------------------- 364--------------------
diff --git a/Documentation/gpio/driver.txt b/Documentation/gpio/driver.txt
index fc1d2f83564d..d8de1c7de85a 100644
--- a/Documentation/gpio/driver.txt
+++ b/Documentation/gpio/driver.txt
@@ -254,7 +254,7 @@ GPIO irqchips usually fall in one of two categories:
254 static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank) 254 static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
255 unsigned long wa_lock_flags; 255 unsigned long wa_lock_flags;
256 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags); 256 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
257 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain, bit)); 257 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain, bit));
258 raw_spin_unlock_irqrestore(&bank->wa_lock, wa_lock_flags); 258 raw_spin_unlock_irqrestore(&bank->wa_lock, wa_lock_flags);
259 259
260* GENERIC CHAINED GPIO irqchips: these are the same as "CHAINED GPIO irqchips", 260* GENERIC CHAINED GPIO irqchips: these are the same as "CHAINED GPIO irqchips",
@@ -313,8 +313,8 @@ symbol:
313 mark all the child IRQs as having the other IRQ as parent. 313 mark all the child IRQs as having the other IRQ as parent.
314 314
315If there is a need to exclude certain GPIOs from the IRQ domain, you can 315If there is a need to exclude certain GPIOs from the IRQ domain, you can
316set .irq_need_valid_mask of the gpiochip before gpiochip_add_data() is 316set .irq.need_valid_mask of the gpiochip before gpiochip_add_data() is
317called. This allocates an .irq_valid_mask with as many bits set as there 317called. This allocates an .irq.valid_mask with as many bits set as there
318are GPIOs in the chip. Drivers can exclude GPIOs by clearing bits from this 318are GPIOs in the chip. Drivers can exclude GPIOs by clearing bits from this
319mask. The mask must be filled in before gpiochip_irqchip_add() or 319mask. The mask must be filled in before gpiochip_irqchip_add() or
320gpiochip_irqchip_add_nested() is called. 320gpiochip_irqchip_add_nested() is called.
diff --git a/MAINTAINERS b/MAINTAINERS
index 2a121c4c990d..bb8eba5e9e4f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2029,6 +2029,7 @@ M: Masahiro Yamada <yamada.masahiro@socionext.com>
2029L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) 2029L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
2030T: git git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-uniphier.git 2030T: git git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-uniphier.git
2031S: Maintained 2031S: Maintained
2032F: Documentation/devicetree/bindings/gpio/gpio-uniphier.txt
2032F: arch/arm/boot/dts/uniphier* 2033F: arch/arm/boot/dts/uniphier*
2033F: arch/arm/include/asm/hardware/cache-uniphier.h 2034F: arch/arm/include/asm/hardware/cache-uniphier.h
2034F: arch/arm/mach-uniphier/ 2035F: arch/arm/mach-uniphier/
@@ -2036,6 +2037,7 @@ F: arch/arm/mm/cache-uniphier.c
2036F: arch/arm64/boot/dts/socionext/ 2037F: arch/arm64/boot/dts/socionext/
2037F: drivers/bus/uniphier-system-bus.c 2038F: drivers/bus/uniphier-system-bus.c
2038F: drivers/clk/uniphier/ 2039F: drivers/clk/uniphier/
2040F: drivers/gpio/gpio-uniphier.c
2039F: drivers/i2c/busses/i2c-uniphier* 2041F: drivers/i2c/busses/i2c-uniphier*
2040F: drivers/irqchip/irq-uniphier-aidet.c 2042F: drivers/irqchip/irq-uniphier-aidet.c
2041F: drivers/pinctrl/uniphier/ 2043F: drivers/pinctrl/uniphier/
@@ -12945,6 +12947,13 @@ F: arch/arc/plat-axs10x
12945F: arch/arc/boot/dts/ax* 12947F: arch/arc/boot/dts/ax*
12946F: Documentation/devicetree/bindings/arc/axs10* 12948F: Documentation/devicetree/bindings/arc/axs10*
12947 12949
12950SYNOPSYS DESIGNWARE APB GPIO DRIVER
12951M: Hoan Tran <hotran@apm.com>
12952L: linux-gpio@vger.kernel.org
12953S: Maintained
12954F: drivers/gpio/gpio-dwapb.c
12955F: Documentation/devicetree/bindings/gpio/snps-dwapb-gpio.txt
12956
12948SYNOPSYS DESIGNWARE DMAC DRIVER 12957SYNOPSYS DESIGNWARE DMAC DRIVER
12949M: Viresh Kumar <vireshk@kernel.org> 12958M: Viresh Kumar <vireshk@kernel.org>
12950R: Andy Shevchenko <andriy.shevchenko@linux.intel.com> 12959R: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
diff --git a/drivers/bcma/driver_gpio.c b/drivers/bcma/driver_gpio.c
index 982d5781d3ce..2c0ffb77d738 100644
--- a/drivers/bcma/driver_gpio.c
+++ b/drivers/bcma/driver_gpio.c
@@ -113,7 +113,7 @@ static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
113 return IRQ_NONE; 113 return IRQ_NONE;
114 114
115 for_each_set_bit(gpio, &irqs, gc->ngpio) 115 for_each_set_bit(gpio, &irqs, gc->ngpio)
116 generic_handle_irq(irq_find_mapping(gc->irqdomain, gpio)); 116 generic_handle_irq(irq_find_mapping(gc->irq.domain, gpio));
117 bcma_chipco_gpio_polarity(cc, irqs, val & irqs); 117 bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
118 118
119 return IRQ_HANDLED; 119 return IRQ_HANDLED;
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 3f80f167ed56..29fc15423299 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -139,7 +139,7 @@ config GPIO_BRCMSTB
139 default y if (ARCH_BRCMSTB || BMIPS_GENERIC) 139 default y if (ARCH_BRCMSTB || BMIPS_GENERIC)
140 depends on OF_GPIO && (ARCH_BRCMSTB || BMIPS_GENERIC || COMPILE_TEST) 140 depends on OF_GPIO && (ARCH_BRCMSTB || BMIPS_GENERIC || COMPILE_TEST)
141 select GPIO_GENERIC 141 select GPIO_GENERIC
142 select GPIOLIB_IRQCHIP 142 select IRQ_DOMAIN
143 help 143 help
144 Say yes here to enable GPIO support for Broadcom STB (BCM7XXX) SoCs. 144 Say yes here to enable GPIO support for Broadcom STB (BCM7XXX) SoCs.
145 145
@@ -286,8 +286,7 @@ config GPIO_LYNXPOINT
286 Requires ACPI device enumeration code to set up a platform device. 286 Requires ACPI device enumeration code to set up a platform device.
287 287
288config GPIO_MB86S7X 288config GPIO_MB86S7X
289 bool "GPIO support for Fujitsu MB86S7x Platforms" 289 tristate "GPIO support for Fujitsu MB86S7x Platforms"
290 depends on ARCH_MB86S7X || COMPILE_TEST
291 help 290 help
292 Say yes here to support the GPIO controller in Fujitsu MB86S70 SoCs. 291 Say yes here to support the GPIO controller in Fujitsu MB86S70 SoCs.
293 292
@@ -442,6 +441,15 @@ config GPIO_TEGRA
442 help 441 help
443 Say yes here to support GPIO pins on NVIDIA Tegra SoCs. 442 Say yes here to support GPIO pins on NVIDIA Tegra SoCs.
444 443
444config GPIO_TEGRA186
445 tristate "NVIDIA Tegra186 GPIO support"
446 default ARCH_TEGRA_186_SOC
447 depends on ARCH_TEGRA_186_SOC || COMPILE_TEST
448 depends on OF_GPIO
449 select GPIOLIB_IRQCHIP
450 help
451 Say yes here to support GPIO pins on NVIDIA Tegra186 SoCs.
452
445config GPIO_TS4800 453config GPIO_TS4800
446 tristate "TS-4800 DIO blocks and compatibles" 454 tristate "TS-4800 DIO blocks and compatibles"
447 depends on OF_GPIO 455 depends on OF_GPIO
@@ -475,6 +483,14 @@ config GPIO_TZ1090_PDC
475 help 483 help
476 Say yes here to support Toumaz Xenif TZ1090 PDC GPIOs. 484 Say yes here to support Toumaz Xenif TZ1090 PDC GPIOs.
477 485
486config GPIO_UNIPHIER
487 tristate "UniPhier GPIO support"
488 depends on ARCH_UNIPHIER || COMPILE_TEST
489 depends on OF_GPIO
490 select IRQ_DOMAIN_HIERARCHY
491 help
492 Say yes here to support UniPhier GPIOs.
493
478config GPIO_VF610 494config GPIO_VF610
479 def_bool y 495 def_bool y
480 depends on ARCH_MXC && SOC_VF610 496 depends on ARCH_MXC && SOC_VF610
@@ -1256,6 +1272,16 @@ config GPIO_74X164
1256 shift registers. This driver can be used to provide access 1272 shift registers. This driver can be used to provide access
1257 to more gpio outputs. 1273 to more gpio outputs.
1258 1274
1275config GPIO_MAX3191X
1276 tristate "Maxim MAX3191x industrial serializer"
1277 select CRC8
1278 help
1279 GPIO driver for Maxim MAX31910, MAX31911, MAX31912, MAX31913,
1280 MAX31953 and MAX31963 industrial serializer, a daisy-chainable
1281 chip to make 8 digital 24V inputs available via SPI. Supports
1282 CRC checksums to guard against electromagnetic interference,
1283 as well as undervoltage and overtemperature detection.
1284
1259config GPIO_MAX7301 1285config GPIO_MAX7301
1260 tristate "Maxim MAX7301 GPIO expander" 1286 tristate "Maxim MAX7301 GPIO expander"
1261 select GPIO_MAX730X 1287 select GPIO_MAX730X
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 8a2dfba3b231..4bc24febb889 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -70,6 +70,7 @@ obj-$(CONFIG_ARCH_LPC32XX) += gpio-lpc32xx.o
70obj-$(CONFIG_GPIO_LP873X) += gpio-lp873x.o 70obj-$(CONFIG_GPIO_LP873X) += gpio-lp873x.o
71obj-$(CONFIG_GPIO_LP87565) += gpio-lp87565.o 71obj-$(CONFIG_GPIO_LP87565) += gpio-lp87565.o
72obj-$(CONFIG_GPIO_LYNXPOINT) += gpio-lynxpoint.o 72obj-$(CONFIG_GPIO_LYNXPOINT) += gpio-lynxpoint.o
73obj-$(CONFIG_GPIO_MAX3191X) += gpio-max3191x.o
73obj-$(CONFIG_GPIO_MAX730X) += gpio-max730x.o 74obj-$(CONFIG_GPIO_MAX730X) += gpio-max730x.o
74obj-$(CONFIG_GPIO_MAX7300) += gpio-max7300.o 75obj-$(CONFIG_GPIO_MAX7300) += gpio-max7300.o
75obj-$(CONFIG_GPIO_MAX7301) += gpio-max7301.o 76obj-$(CONFIG_GPIO_MAX7301) += gpio-max7301.o
@@ -114,6 +115,7 @@ obj-$(CONFIG_GPIO_SYSCON) += gpio-syscon.o
114obj-$(CONFIG_GPIO_TB10X) += gpio-tb10x.o 115obj-$(CONFIG_GPIO_TB10X) += gpio-tb10x.o
115obj-$(CONFIG_GPIO_TC3589X) += gpio-tc3589x.o 116obj-$(CONFIG_GPIO_TC3589X) += gpio-tc3589x.o
116obj-$(CONFIG_GPIO_TEGRA) += gpio-tegra.o 117obj-$(CONFIG_GPIO_TEGRA) += gpio-tegra.o
118obj-$(CONFIG_GPIO_TEGRA186) += gpio-tegra186.o
117obj-$(CONFIG_GPIO_THUNDERX) += gpio-thunderx.o 119obj-$(CONFIG_GPIO_THUNDERX) += gpio-thunderx.o
118obj-$(CONFIG_GPIO_TIMBERDALE) += gpio-timberdale.o 120obj-$(CONFIG_GPIO_TIMBERDALE) += gpio-timberdale.o
119obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o 121obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o
@@ -132,6 +134,7 @@ obj-$(CONFIG_GPIO_TWL6040) += gpio-twl6040.o
132obj-$(CONFIG_GPIO_TZ1090) += gpio-tz1090.o 134obj-$(CONFIG_GPIO_TZ1090) += gpio-tz1090.o
133obj-$(CONFIG_GPIO_TZ1090_PDC) += gpio-tz1090-pdc.o 135obj-$(CONFIG_GPIO_TZ1090_PDC) += gpio-tz1090-pdc.o
134obj-$(CONFIG_GPIO_UCB1400) += gpio-ucb1400.o 136obj-$(CONFIG_GPIO_UCB1400) += gpio-ucb1400.o
137obj-$(CONFIG_GPIO_UNIPHIER) += gpio-uniphier.o
135obj-$(CONFIG_GPIO_VF610) += gpio-vf610.o 138obj-$(CONFIG_GPIO_VF610) += gpio-vf610.o
136obj-$(CONFIG_GPIO_VIPERBOARD) += gpio-viperboard.o 139obj-$(CONFIG_GPIO_VIPERBOARD) += gpio-viperboard.o
137obj-$(CONFIG_GPIO_VR41XX) += gpio-vr41xx.o 140obj-$(CONFIG_GPIO_VR41XX) += gpio-vr41xx.o
diff --git a/drivers/gpio/gpio-104-dio-48e.c b/drivers/gpio/gpio-104-dio-48e.c
index 598e209efa2d..bab3b94c5cbc 100644
--- a/drivers/gpio/gpio-104-dio-48e.c
+++ b/drivers/gpio/gpio-104-dio-48e.c
@@ -326,7 +326,7 @@ static irqreturn_t dio48e_irq_handler(int irq, void *dev_id)
326 unsigned long gpio; 326 unsigned long gpio;
327 327
328 for_each_set_bit(gpio, &irq_mask, 2) 328 for_each_set_bit(gpio, &irq_mask, 2)
329 generic_handle_irq(irq_find_mapping(chip->irqdomain, 329 generic_handle_irq(irq_find_mapping(chip->irq.domain,
330 19 + gpio*24)); 330 19 + gpio*24));
331 331
332 raw_spin_lock(&dio48egpio->lock); 332 raw_spin_lock(&dio48egpio->lock);
diff --git a/drivers/gpio/gpio-104-idi-48.c b/drivers/gpio/gpio-104-idi-48.c
index 51f046e29ff7..add859d59766 100644
--- a/drivers/gpio/gpio-104-idi-48.c
+++ b/drivers/gpio/gpio-104-idi-48.c
@@ -209,7 +209,7 @@ static irqreturn_t idi_48_irq_handler(int irq, void *dev_id)
209 for_each_set_bit(bit_num, &irq_mask, 8) { 209 for_each_set_bit(bit_num, &irq_mask, 8) {
210 gpio = bit_num + boundary * 8; 210 gpio = bit_num + boundary * 8;
211 211
212 generic_handle_irq(irq_find_mapping(chip->irqdomain, 212 generic_handle_irq(irq_find_mapping(chip->irq.domain,
213 gpio)); 213 gpio));
214 } 214 }
215 } 215 }
diff --git a/drivers/gpio/gpio-104-idio-16.c b/drivers/gpio/gpio-104-idio-16.c
index ec2ce34ff473..2f16638a0589 100644
--- a/drivers/gpio/gpio-104-idio-16.c
+++ b/drivers/gpio/gpio-104-idio-16.c
@@ -199,7 +199,7 @@ static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
199 int gpio; 199 int gpio;
200 200
201 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio) 201 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
202 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio)); 202 generic_handle_irq(irq_find_mapping(chip->irq.domain, gpio));
203 203
204 raw_spin_lock(&idio16gpio->lock); 204 raw_spin_lock(&idio16gpio->lock);
205 205
diff --git a/drivers/gpio/gpio-adnp.c b/drivers/gpio/gpio-adnp.c
index 89863ea25de1..44c09904daa6 100644
--- a/drivers/gpio/gpio-adnp.c
+++ b/drivers/gpio/gpio-adnp.c
@@ -192,28 +192,20 @@ static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
192 mutex_lock(&adnp->i2c_lock); 192 mutex_lock(&adnp->i2c_lock);
193 193
194 err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr); 194 err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr);
195 if (err < 0) { 195 if (err < 0)
196 mutex_unlock(&adnp->i2c_lock); 196 goto unlock;
197 return;
198 }
199 197
200 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr); 198 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr);
201 if (err < 0) { 199 if (err < 0)
202 mutex_unlock(&adnp->i2c_lock); 200 goto unlock;
203 return;
204 }
205 201
206 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier); 202 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
207 if (err < 0) { 203 if (err < 0)
208 mutex_unlock(&adnp->i2c_lock); 204 goto unlock;
209 return;
210 }
211 205
212 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr); 206 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
213 if (err < 0) { 207 if (err < 0)
214 mutex_unlock(&adnp->i2c_lock); 208 goto unlock;
215 return;
216 }
217 209
218 mutex_unlock(&adnp->i2c_lock); 210 mutex_unlock(&adnp->i2c_lock);
219 211
@@ -240,6 +232,11 @@ static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
240 direction, level, interrupt, pending); 232 direction, level, interrupt, pending);
241 } 233 }
242 } 234 }
235
236 return;
237
238unlock:
239 mutex_unlock(&adnp->i2c_lock);
243} 240}
244 241
245static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios) 242static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios)
@@ -323,7 +320,7 @@ static irqreturn_t adnp_irq(int irq, void *data)
323 320
324 for_each_set_bit(bit, &pending, 8) { 321 for_each_set_bit(bit, &pending, 8) {
325 unsigned int child_irq; 322 unsigned int child_irq;
326 child_irq = irq_find_mapping(adnp->gpio.irqdomain, 323 child_irq = irq_find_mapping(adnp->gpio.irq.domain,
327 base + bit); 324 base + bit);
328 handle_nested_irq(child_irq); 325 handle_nested_irq(child_irq);
329 } 326 }
diff --git a/drivers/gpio/gpio-altera.c b/drivers/gpio/gpio-altera.c
index ccc02ed65b3c..8e76d390e653 100644
--- a/drivers/gpio/gpio-altera.c
+++ b/drivers/gpio/gpio-altera.c
@@ -211,7 +211,7 @@ static void altera_gpio_irq_edge_handler(struct irq_desc *desc)
211 altera_gc = gpiochip_get_data(irq_desc_get_handler_data(desc)); 211 altera_gc = gpiochip_get_data(irq_desc_get_handler_data(desc));
212 chip = irq_desc_get_chip(desc); 212 chip = irq_desc_get_chip(desc);
213 mm_gc = &altera_gc->mmchip; 213 mm_gc = &altera_gc->mmchip;
214 irqdomain = altera_gc->mmchip.gc.irqdomain; 214 irqdomain = altera_gc->mmchip.gc.irq.domain;
215 215
216 chained_irq_enter(chip, desc); 216 chained_irq_enter(chip, desc);
217 217
@@ -239,7 +239,7 @@ static void altera_gpio_irq_leveL_high_handler(struct irq_desc *desc)
239 altera_gc = gpiochip_get_data(irq_desc_get_handler_data(desc)); 239 altera_gc = gpiochip_get_data(irq_desc_get_handler_data(desc));
240 chip = irq_desc_get_chip(desc); 240 chip = irq_desc_get_chip(desc);
241 mm_gc = &altera_gc->mmchip; 241 mm_gc = &altera_gc->mmchip;
242 irqdomain = altera_gc->mmchip.gc.irqdomain; 242 irqdomain = altera_gc->mmchip.gc.irq.domain;
243 243
244 chained_irq_enter(chip, desc); 244 chained_irq_enter(chip, desc);
245 245
diff --git a/drivers/gpio/gpio-aspeed.c b/drivers/gpio/gpio-aspeed.c
index bfc53995064a..8781817d9003 100644
--- a/drivers/gpio/gpio-aspeed.c
+++ b/drivers/gpio/gpio-aspeed.c
@@ -411,13 +411,16 @@ static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
411 switch (type & IRQ_TYPE_SENSE_MASK) { 411 switch (type & IRQ_TYPE_SENSE_MASK) {
412 case IRQ_TYPE_EDGE_BOTH: 412 case IRQ_TYPE_EDGE_BOTH:
413 type2 |= bit; 413 type2 |= bit;
414 /* fall through */
414 case IRQ_TYPE_EDGE_RISING: 415 case IRQ_TYPE_EDGE_RISING:
415 type0 |= bit; 416 type0 |= bit;
417 /* fall through */
416 case IRQ_TYPE_EDGE_FALLING: 418 case IRQ_TYPE_EDGE_FALLING:
417 handler = handle_edge_irq; 419 handler = handle_edge_irq;
418 break; 420 break;
419 case IRQ_TYPE_LEVEL_HIGH: 421 case IRQ_TYPE_LEVEL_HIGH:
420 type0 |= bit; 422 type0 |= bit;
423 /* fall through */
421 case IRQ_TYPE_LEVEL_LOW: 424 case IRQ_TYPE_LEVEL_LOW:
422 type1 |= bit; 425 type1 |= bit;
423 handler = handle_level_irq; 426 handler = handle_level_irq;
@@ -466,7 +469,7 @@ static void aspeed_gpio_irq_handler(struct irq_desc *desc)
466 reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS)); 469 reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
467 470
468 for_each_set_bit(p, &reg, 32) { 471 for_each_set_bit(p, &reg, 32) {
469 girq = irq_find_mapping(gc->irqdomain, i * 32 + p); 472 girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
470 generic_handle_irq(girq); 473 generic_handle_irq(girq);
471 } 474 }
472 475
@@ -498,7 +501,7 @@ static void set_irq_valid_mask(struct aspeed_gpio *gpio)
498 if (i >= gpio->config->nr_gpios) 501 if (i >= gpio->config->nr_gpios)
499 break; 502 break;
500 503
501 clear_bit(i, gpio->chip.irq_valid_mask); 504 clear_bit(i, gpio->chip.irq.valid_mask);
502 } 505 }
503 506
504 props++; 507 props++;
@@ -853,7 +856,7 @@ static int __init aspeed_gpio_probe(struct platform_device *pdev)
853 gpio->chip.set_config = aspeed_gpio_set_config; 856 gpio->chip.set_config = aspeed_gpio_set_config;
854 gpio->chip.label = dev_name(&pdev->dev); 857 gpio->chip.label = dev_name(&pdev->dev);
855 gpio->chip.base = -1; 858 gpio->chip.base = -1;
856 gpio->chip.irq_need_valid_mask = true; 859 gpio->chip.irq.need_valid_mask = true;
857 860
858 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); 861 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
859 if (rc < 0) 862 if (rc < 0)
diff --git a/drivers/gpio/gpio-ath79.c b/drivers/gpio/gpio-ath79.c
index f33d4a5fe671..5fad89dfab7e 100644
--- a/drivers/gpio/gpio-ath79.c
+++ b/drivers/gpio/gpio-ath79.c
@@ -132,6 +132,7 @@ static int ath79_gpio_irq_set_type(struct irq_data *data,
132 132
133 case IRQ_TYPE_LEVEL_HIGH: 133 case IRQ_TYPE_LEVEL_HIGH:
134 polarity |= mask; 134 polarity |= mask;
135 /* fall through */
135 case IRQ_TYPE_LEVEL_LOW: 136 case IRQ_TYPE_LEVEL_LOW:
136 type |= mask; 137 type |= mask;
137 break; 138 break;
@@ -208,7 +209,7 @@ static void ath79_gpio_irq_handler(struct irq_desc *desc)
208 if (pending) { 209 if (pending) {
209 for_each_set_bit(irq, &pending, gc->ngpio) 210 for_each_set_bit(irq, &pending, gc->ngpio)
210 generic_handle_irq( 211 generic_handle_irq(
211 irq_linear_revmap(gc->irqdomain, irq)); 212 irq_linear_revmap(gc->irq.domain, irq));
212 } 213 }
213 214
214 chained_irq_exit(irqchip, desc); 215 chained_irq_exit(irqchip, desc);
diff --git a/drivers/gpio/gpio-brcmstb.c b/drivers/gpio/gpio-brcmstb.c
index dd0308cc8bb0..545d43a587b7 100644
--- a/drivers/gpio/gpio-brcmstb.c
+++ b/drivers/gpio/gpio-brcmstb.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (C) 2015 Broadcom Corporation 2 * Copyright (C) 2015-2017 Broadcom
3 * 3 *
4 * This program is free software; you can redistribute it and/or 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 5 * modify it under the terms of the GNU General Public License as
@@ -19,17 +19,30 @@
19#include <linux/irqdomain.h> 19#include <linux/irqdomain.h>
20#include <linux/irqchip/chained_irq.h> 20#include <linux/irqchip/chained_irq.h>
21#include <linux/interrupt.h> 21#include <linux/interrupt.h>
22#include <linux/reboot.h> 22#include <linux/bitops.h>
23 23
24#define GIO_BANK_SIZE 0x20 24enum gio_reg_index {
25#define GIO_ODEN(bank) (((bank) * GIO_BANK_SIZE) + 0x00) 25 GIO_REG_ODEN = 0,
26#define GIO_DATA(bank) (((bank) * GIO_BANK_SIZE) + 0x04) 26 GIO_REG_DATA,
27#define GIO_IODIR(bank) (((bank) * GIO_BANK_SIZE) + 0x08) 27 GIO_REG_IODIR,
28#define GIO_EC(bank) (((bank) * GIO_BANK_SIZE) + 0x0c) 28 GIO_REG_EC,
29#define GIO_EI(bank) (((bank) * GIO_BANK_SIZE) + 0x10) 29 GIO_REG_EI,
30#define GIO_MASK(bank) (((bank) * GIO_BANK_SIZE) + 0x14) 30 GIO_REG_MASK,
31#define GIO_LEVEL(bank) (((bank) * GIO_BANK_SIZE) + 0x18) 31 GIO_REG_LEVEL,
32#define GIO_STAT(bank) (((bank) * GIO_BANK_SIZE) + 0x1c) 32 GIO_REG_STAT,
33 NUMBER_OF_GIO_REGISTERS
34};
35
36#define GIO_BANK_SIZE (NUMBER_OF_GIO_REGISTERS * sizeof(u32))
37#define GIO_BANK_OFF(bank, off) (((bank) * GIO_BANK_SIZE) + (off * sizeof(u32)))
38#define GIO_ODEN(bank) GIO_BANK_OFF(bank, GIO_REG_ODEN)
39#define GIO_DATA(bank) GIO_BANK_OFF(bank, GIO_REG_DATA)
40#define GIO_IODIR(bank) GIO_BANK_OFF(bank, GIO_REG_IODIR)
41#define GIO_EC(bank) GIO_BANK_OFF(bank, GIO_REG_EC)
42#define GIO_EI(bank) GIO_BANK_OFF(bank, GIO_REG_EI)
43#define GIO_MASK(bank) GIO_BANK_OFF(bank, GIO_REG_MASK)
44#define GIO_LEVEL(bank) GIO_BANK_OFF(bank, GIO_REG_LEVEL)
45#define GIO_STAT(bank) GIO_BANK_OFF(bank, GIO_REG_STAT)
33 46
34struct brcmstb_gpio_bank { 47struct brcmstb_gpio_bank {
35 struct list_head node; 48 struct list_head node;
@@ -37,21 +50,23 @@ struct brcmstb_gpio_bank {
37 struct gpio_chip gc; 50 struct gpio_chip gc;
38 struct brcmstb_gpio_priv *parent_priv; 51 struct brcmstb_gpio_priv *parent_priv;
39 u32 width; 52 u32 width;
40 struct irq_chip irq_chip; 53 u32 wake_active;
54 u32 saved_regs[GIO_REG_STAT]; /* Don't save and restore GIO_REG_STAT */
41}; 55};
42 56
43struct brcmstb_gpio_priv { 57struct brcmstb_gpio_priv {
44 struct list_head bank_list; 58 struct list_head bank_list;
45 void __iomem *reg_base; 59 void __iomem *reg_base;
46 struct platform_device *pdev; 60 struct platform_device *pdev;
61 struct irq_domain *irq_domain;
62 struct irq_chip irq_chip;
47 int parent_irq; 63 int parent_irq;
48 int gpio_base; 64 int gpio_base;
49 bool can_wake; 65 int num_gpios;
50 int parent_wake_irq; 66 int parent_wake_irq;
51 struct notifier_block reboot_notifier;
52}; 67};
53 68
54#define MAX_GPIO_PER_BANK 32 69#define MAX_GPIO_PER_BANK 32
55#define GPIO_BANK(gpio) ((gpio) >> 5) 70#define GPIO_BANK(gpio) ((gpio) >> 5)
56/* assumes MAX_GPIO_PER_BANK is a multiple of 2 */ 71/* assumes MAX_GPIO_PER_BANK is a multiple of 2 */
57#define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1)) 72#define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1))
@@ -63,12 +78,40 @@ brcmstb_gpio_gc_to_priv(struct gpio_chip *gc)
63 return bank->parent_priv; 78 return bank->parent_priv;
64} 79}
65 80
81static unsigned long
82__brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank)
83{
84 void __iomem *reg_base = bank->parent_priv->reg_base;
85
86 return bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) &
87 bank->gc.read_reg(reg_base + GIO_MASK(bank->id));
88}
89
90static unsigned long
91brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank)
92{
93 unsigned long status;
94 unsigned long flags;
95
96 spin_lock_irqsave(&bank->gc.bgpio_lock, flags);
97 status = __brcmstb_gpio_get_active_irqs(bank);
98 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
99
100 return status;
101}
102
103static int brcmstb_gpio_hwirq_to_offset(irq_hw_number_t hwirq,
104 struct brcmstb_gpio_bank *bank)
105{
106 return hwirq - (bank->gc.base - bank->parent_priv->gpio_base);
107}
108
66static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank, 109static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
67 unsigned int offset, bool enable) 110 unsigned int hwirq, bool enable)
68{ 111{
69 struct gpio_chip *gc = &bank->gc; 112 struct gpio_chip *gc = &bank->gc;
70 struct brcmstb_gpio_priv *priv = bank->parent_priv; 113 struct brcmstb_gpio_priv *priv = bank->parent_priv;
71 u32 mask = gc->pin2mask(gc, offset); 114 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(hwirq, bank));
72 u32 imask; 115 u32 imask;
73 unsigned long flags; 116 unsigned long flags;
74 117
@@ -82,6 +125,17 @@ static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank,
82 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 125 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
83} 126}
84 127
128static int brcmstb_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
129{
130 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
131 /* gc_offset is relative to this gpio_chip; want real offset */
132 int hwirq = offset + (gc->base - priv->gpio_base);
133
134 if (hwirq >= priv->num_gpios)
135 return -ENXIO;
136 return irq_create_mapping(priv->irq_domain, hwirq);
137}
138
85/* -------------------- IRQ chip functions -------------------- */ 139/* -------------------- IRQ chip functions -------------------- */
86 140
87static void brcmstb_gpio_irq_mask(struct irq_data *d) 141static void brcmstb_gpio_irq_mask(struct irq_data *d)
@@ -100,12 +154,22 @@ static void brcmstb_gpio_irq_unmask(struct irq_data *d)
100 brcmstb_gpio_set_imask(bank, d->hwirq, true); 154 brcmstb_gpio_set_imask(bank, d->hwirq, true);
101} 155}
102 156
157static void brcmstb_gpio_irq_ack(struct irq_data *d)
158{
159 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
160 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
161 struct brcmstb_gpio_priv *priv = bank->parent_priv;
162 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
163
164 gc->write_reg(priv->reg_base + GIO_STAT(bank->id), mask);
165}
166
103static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type) 167static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
104{ 168{
105 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 169 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
106 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); 170 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
107 struct brcmstb_gpio_priv *priv = bank->parent_priv; 171 struct brcmstb_gpio_priv *priv = bank->parent_priv;
108 u32 mask = BIT(d->hwirq); 172 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
109 u32 edge_insensitive, iedge_insensitive; 173 u32 edge_insensitive, iedge_insensitive;
110 u32 edge_config, iedge_config; 174 u32 edge_config, iedge_config;
111 u32 level, ilevel; 175 u32 level, ilevel;
@@ -113,13 +177,13 @@ static int brcmstb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
113 177
114 switch (type) { 178 switch (type) {
115 case IRQ_TYPE_LEVEL_LOW: 179 case IRQ_TYPE_LEVEL_LOW:
116 level = 0; 180 level = mask;
117 edge_config = 0; 181 edge_config = 0;
118 edge_insensitive = 0; 182 edge_insensitive = 0;
119 break; 183 break;
120 case IRQ_TYPE_LEVEL_HIGH: 184 case IRQ_TYPE_LEVEL_HIGH:
121 level = mask; 185 level = mask;
122 edge_config = 0; 186 edge_config = mask;
123 edge_insensitive = 0; 187 edge_insensitive = 0;
124 break; 188 break;
125 case IRQ_TYPE_EDGE_FALLING: 189 case IRQ_TYPE_EDGE_FALLING:
@@ -166,11 +230,6 @@ static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
166{ 230{
167 int ret = 0; 231 int ret = 0;
168 232
169 /*
170 * Only enable wake IRQ once for however many hwirqs can wake
171 * since they all use the same wake IRQ. Mask will be set
172 * up appropriately thanks to IRQCHIP_MASK_ON_SUSPEND flag.
173 */
174 if (enable) 233 if (enable)
175 ret = enable_irq_wake(priv->parent_wake_irq); 234 ret = enable_irq_wake(priv->parent_wake_irq);
176 else 235 else
@@ -184,7 +243,18 @@ static int brcmstb_gpio_priv_set_wake(struct brcmstb_gpio_priv *priv,
184static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable) 243static int brcmstb_gpio_irq_set_wake(struct irq_data *d, unsigned int enable)
185{ 244{
186 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 245 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
187 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc); 246 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc);
247 struct brcmstb_gpio_priv *priv = bank->parent_priv;
248 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank));
249
250 /*
251 * Do not do anything specific for now, suspend/resume callbacks will
252 * configure the interrupt mask appropriately
253 */
254 if (enable)
255 bank->wake_active |= mask;
256 else
257 bank->wake_active &= ~mask;
188 258
189 return brcmstb_gpio_priv_set_wake(priv, enable); 259 return brcmstb_gpio_priv_set_wake(priv, enable);
190} 260}
@@ -195,43 +265,36 @@ static irqreturn_t brcmstb_gpio_wake_irq_handler(int irq, void *data)
195 265
196 if (!priv || irq != priv->parent_wake_irq) 266 if (!priv || irq != priv->parent_wake_irq)
197 return IRQ_NONE; 267 return IRQ_NONE;
198 pm_wakeup_event(&priv->pdev->dev, 0); 268
269 /* Nothing to do */
199 return IRQ_HANDLED; 270 return IRQ_HANDLED;
200} 271}
201 272
202static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank) 273static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank)
203{ 274{
204 struct brcmstb_gpio_priv *priv = bank->parent_priv; 275 struct brcmstb_gpio_priv *priv = bank->parent_priv;
205 struct irq_domain *irq_domain = bank->gc.irqdomain; 276 struct irq_domain *domain = priv->irq_domain;
206 void __iomem *reg_base = priv->reg_base; 277 int hwbase = bank->gc.base - priv->gpio_base;
207 unsigned long status; 278 unsigned long status;
208 unsigned long flags;
209 279
210 spin_lock_irqsave(&bank->gc.bgpio_lock, flags); 280 while ((status = brcmstb_gpio_get_active_irqs(bank))) {
211 while ((status = bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) & 281 unsigned int irq, offset;
212 bank->gc.read_reg(reg_base + GIO_MASK(bank->id)))) { 282
213 int bit; 283 for_each_set_bit(offset, &status, 32) {
214 284 if (offset >= bank->width)
215 for_each_set_bit(bit, &status, 32) {
216 u32 stat = bank->gc.read_reg(reg_base +
217 GIO_STAT(bank->id));
218 if (bit >= bank->width)
219 dev_warn(&priv->pdev->dev, 285 dev_warn(&priv->pdev->dev,
220 "IRQ for invalid GPIO (bank=%d, offset=%d)\n", 286 "IRQ for invalid GPIO (bank=%d, offset=%d)\n",
221 bank->id, bit); 287 bank->id, offset);
222 bank->gc.write_reg(reg_base + GIO_STAT(bank->id), 288 irq = irq_linear_revmap(domain, hwbase + offset);
223 stat | BIT(bit)); 289 generic_handle_irq(irq);
224 generic_handle_irq(irq_find_mapping(irq_domain, bit));
225 } 290 }
226 } 291 }
227 spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags);
228} 292}
229 293
230/* Each UPG GIO block has one IRQ for all banks */ 294/* Each UPG GIO block has one IRQ for all banks */
231static void brcmstb_gpio_irq_handler(struct irq_desc *desc) 295static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
232{ 296{
233 struct gpio_chip *gc = irq_desc_get_handler_data(desc); 297 struct brcmstb_gpio_priv *priv = irq_desc_get_handler_data(desc);
234 struct brcmstb_gpio_priv *priv = brcmstb_gpio_gc_to_priv(gc);
235 struct irq_chip *chip = irq_desc_get_chip(desc); 298 struct irq_chip *chip = irq_desc_get_chip(desc);
236 struct brcmstb_gpio_bank *bank; 299 struct brcmstb_gpio_bank *bank;
237 300
@@ -244,19 +307,63 @@ static void brcmstb_gpio_irq_handler(struct irq_desc *desc)
244 chained_irq_exit(chip, desc); 307 chained_irq_exit(chip, desc);
245} 308}
246 309
247static int brcmstb_gpio_reboot(struct notifier_block *nb, 310static struct brcmstb_gpio_bank *brcmstb_gpio_hwirq_to_bank(
248 unsigned long action, void *data) 311 struct brcmstb_gpio_priv *priv, irq_hw_number_t hwirq)
249{ 312{
250 struct brcmstb_gpio_priv *priv = 313 struct brcmstb_gpio_bank *bank;
251 container_of(nb, struct brcmstb_gpio_priv, reboot_notifier); 314 int i = 0;
252 315
253 /* Enable GPIO for S5 cold boot */ 316 /* banks are in descending order */
254 if (action == SYS_POWER_OFF) 317 list_for_each_entry_reverse(bank, &priv->bank_list, node) {
255 brcmstb_gpio_priv_set_wake(priv, 1); 318 i += bank->gc.ngpio;
319 if (hwirq < i)
320 return bank;
321 }
322 return NULL;
323}
324
325/*
326 * This lock class tells lockdep that GPIO irqs are in a different
327 * category than their parents, so it won't report false recursion.
328 */
329static struct lock_class_key brcmstb_gpio_irq_lock_class;
256 330
257 return NOTIFY_DONE; 331
332static int brcmstb_gpio_irq_map(struct irq_domain *d, unsigned int irq,
333 irq_hw_number_t hwirq)
334{
335 struct brcmstb_gpio_priv *priv = d->host_data;
336 struct brcmstb_gpio_bank *bank =
337 brcmstb_gpio_hwirq_to_bank(priv, hwirq);
338 struct platform_device *pdev = priv->pdev;
339 int ret;
340
341 if (!bank)
342 return -EINVAL;
343
344 dev_dbg(&pdev->dev, "Mapping irq %d for gpio line %d (bank %d)\n",
345 irq, (int)hwirq, bank->id);
346 ret = irq_set_chip_data(irq, &bank->gc);
347 if (ret < 0)
348 return ret;
349 irq_set_lockdep_class(irq, &brcmstb_gpio_irq_lock_class);
350 irq_set_chip_and_handler(irq, &priv->irq_chip, handle_level_irq);
351 irq_set_noprobe(irq);
352 return 0;
258} 353}
259 354
355static void brcmstb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
356{
357 irq_set_chip_and_handler(irq, NULL, NULL);
358 irq_set_chip_data(irq, NULL);
359}
360
361static const struct irq_domain_ops brcmstb_gpio_irq_domain_ops = {
362 .map = brcmstb_gpio_irq_map,
363 .unmap = brcmstb_gpio_irq_unmap,
364 .xlate = irq_domain_xlate_twocell,
365};
366
260/* Make sure that the number of banks matches up between properties */ 367/* Make sure that the number of banks matches up between properties */
261static int brcmstb_gpio_sanity_check_banks(struct device *dev, 368static int brcmstb_gpio_sanity_check_banks(struct device *dev,
262 struct device_node *np, struct resource *res) 369 struct device_node *np, struct resource *res)
@@ -278,13 +385,25 @@ static int brcmstb_gpio_remove(struct platform_device *pdev)
278{ 385{
279 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev); 386 struct brcmstb_gpio_priv *priv = platform_get_drvdata(pdev);
280 struct brcmstb_gpio_bank *bank; 387 struct brcmstb_gpio_bank *bank;
281 int ret = 0; 388 int offset, ret = 0, virq;
282 389
283 if (!priv) { 390 if (!priv) {
284 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__); 391 dev_err(&pdev->dev, "called %s without drvdata!\n", __func__);
285 return -EFAULT; 392 return -EFAULT;
286 } 393 }
287 394
395 if (priv->parent_irq > 0)
396 irq_set_chained_handler_and_data(priv->parent_irq, NULL, NULL);
397
398 /* Remove all IRQ mappings and delete the domain */
399 if (priv->irq_domain) {
400 for (offset = 0; offset < priv->num_gpios; offset++) {
401 virq = irq_find_mapping(priv->irq_domain, offset);
402 irq_dispose_mapping(virq);
403 }
404 irq_domain_remove(priv->irq_domain);
405 }
406
288 /* 407 /*
289 * You can lose return values below, but we report all errors, and it's 408 * You can lose return values below, but we report all errors, and it's
290 * more important to actually perform all of the steps. 409 * more important to actually perform all of the steps.
@@ -292,12 +411,6 @@ static int brcmstb_gpio_remove(struct platform_device *pdev)
292 list_for_each_entry(bank, &priv->bank_list, node) 411 list_for_each_entry(bank, &priv->bank_list, node)
293 gpiochip_remove(&bank->gc); 412 gpiochip_remove(&bank->gc);
294 413
295 if (priv->reboot_notifier.notifier_call) {
296 ret = unregister_reboot_notifier(&priv->reboot_notifier);
297 if (ret)
298 dev_err(&pdev->dev,
299 "failed to unregister reboot notifier\n");
300 }
301 return ret; 414 return ret;
302} 415}
303 416
@@ -332,66 +445,163 @@ static int brcmstb_gpio_of_xlate(struct gpio_chip *gc,
332 return offset; 445 return offset;
333} 446}
334 447
335/* Before calling, must have bank->parent_irq set and gpiochip registered */ 448/* priv->parent_irq and priv->num_gpios must be set before calling */
336static int brcmstb_gpio_irq_setup(struct platform_device *pdev, 449static int brcmstb_gpio_irq_setup(struct platform_device *pdev,
337 struct brcmstb_gpio_bank *bank) 450 struct brcmstb_gpio_priv *priv)
338{ 451{
339 struct brcmstb_gpio_priv *priv = bank->parent_priv;
340 struct device *dev = &pdev->dev; 452 struct device *dev = &pdev->dev;
341 struct device_node *np = dev->of_node; 453 struct device_node *np = dev->of_node;
342 int err; 454 int err;
343 455
344 bank->irq_chip.name = dev_name(dev); 456 priv->irq_domain =
345 bank->irq_chip.irq_mask = brcmstb_gpio_irq_mask; 457 irq_domain_add_linear(np, priv->num_gpios,
346 bank->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask; 458 &brcmstb_gpio_irq_domain_ops,
347 bank->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type; 459 priv);
348 460 if (!priv->irq_domain) {
349 /* Ensures that all non-wakeup IRQs are disabled at suspend */ 461 dev_err(dev, "Couldn't allocate IRQ domain\n");
350 bank->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND; 462 return -ENXIO;
463 }
351 464
352 if (IS_ENABLED(CONFIG_PM_SLEEP) && !priv->can_wake && 465 if (of_property_read_bool(np, "wakeup-source")) {
353 of_property_read_bool(np, "wakeup-source")) {
354 priv->parent_wake_irq = platform_get_irq(pdev, 1); 466 priv->parent_wake_irq = platform_get_irq(pdev, 1);
355 if (priv->parent_wake_irq < 0) { 467 if (priv->parent_wake_irq < 0) {
468 priv->parent_wake_irq = 0;
356 dev_warn(dev, 469 dev_warn(dev,
357 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep"); 470 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep");
358 } else { 471 } else {
359 /* 472 /*
360 * Set wakeup capability before requesting wakeup 473 * Set wakeup capability so we can process boot-time
361 * interrupt, so we can process boot-time "wakeups" 474 * "wakeups" (e.g., from S5 cold boot)
362 * (e.g., from S5 cold boot)
363 */ 475 */
364 device_set_wakeup_capable(dev, true); 476 device_set_wakeup_capable(dev, true);
365 device_wakeup_enable(dev); 477 device_wakeup_enable(dev);
366 err = devm_request_irq(dev, priv->parent_wake_irq, 478 err = devm_request_irq(dev, priv->parent_wake_irq,
367 brcmstb_gpio_wake_irq_handler, 0, 479 brcmstb_gpio_wake_irq_handler,
368 "brcmstb-gpio-wake", priv); 480 IRQF_SHARED,
481 "brcmstb-gpio-wake", priv);
369 482
370 if (err < 0) { 483 if (err < 0) {
371 dev_err(dev, "Couldn't request wake IRQ"); 484 dev_err(dev, "Couldn't request wake IRQ");
372 return err; 485 goto out_free_domain;
373 } 486 }
374
375 priv->reboot_notifier.notifier_call =
376 brcmstb_gpio_reboot;
377 register_reboot_notifier(&priv->reboot_notifier);
378 priv->can_wake = true;
379 } 487 }
380 } 488 }
381 489
382 if (priv->can_wake) 490 priv->irq_chip.name = dev_name(dev);
383 bank->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake; 491 priv->irq_chip.irq_disable = brcmstb_gpio_irq_mask;
492 priv->irq_chip.irq_mask = brcmstb_gpio_irq_mask;
493 priv->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask;
494 priv->irq_chip.irq_ack = brcmstb_gpio_irq_ack;
495 priv->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type;
496
497 if (priv->parent_wake_irq)
498 priv->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake;
499
500 irq_set_chained_handler_and_data(priv->parent_irq,
501 brcmstb_gpio_irq_handler, priv);
502 irq_set_status_flags(priv->parent_irq, IRQ_DISABLE_UNLAZY);
503
504 return 0;
505
506out_free_domain:
507 irq_domain_remove(priv->irq_domain);
508
509 return err;
510}
511
512static void brcmstb_gpio_bank_save(struct brcmstb_gpio_priv *priv,
513 struct brcmstb_gpio_bank *bank)
514{
515 struct gpio_chip *gc = &bank->gc;
516 unsigned int i;
517
518 for (i = 0; i < GIO_REG_STAT; i++)
519 bank->saved_regs[i] = gc->read_reg(priv->reg_base +
520 GIO_BANK_OFF(bank->id, i));
521}
522
523static void brcmstb_gpio_quiesce(struct device *dev, bool save)
524{
525 struct brcmstb_gpio_priv *priv = dev_get_drvdata(dev);
526 struct brcmstb_gpio_bank *bank;
527 struct gpio_chip *gc;
528 u32 imask;
529
530 /* disable non-wake interrupt */
531 if (priv->parent_irq >= 0)
532 disable_irq(priv->parent_irq);
533
534 list_for_each_entry(bank, &priv->bank_list, node) {
535 gc = &bank->gc;
536
537 if (save)
538 brcmstb_gpio_bank_save(priv, bank);
539
540 /* Unmask GPIOs which have been flagged as wake-up sources */
541 if (priv->parent_wake_irq)
542 imask = bank->wake_active;
543 else
544 imask = 0;
545 gc->write_reg(priv->reg_base + GIO_MASK(bank->id),
546 imask);
547 }
548}
549
550static void brcmstb_gpio_shutdown(struct platform_device *pdev)
551{
552 /* Enable GPIO for S5 cold boot */
553 brcmstb_gpio_quiesce(&pdev->dev, false);
554}
555
556#ifdef CONFIG_PM_SLEEP
557static void brcmstb_gpio_bank_restore(struct brcmstb_gpio_priv *priv,
558 struct brcmstb_gpio_bank *bank)
559{
560 struct gpio_chip *gc = &bank->gc;
561 unsigned int i;
562
563 for (i = 0; i < GIO_REG_STAT; i++)
564 gc->write_reg(priv->reg_base + GIO_BANK_OFF(bank->id, i),
565 bank->saved_regs[i]);
566}
567
568static int brcmstb_gpio_suspend(struct device *dev)
569{
570 brcmstb_gpio_quiesce(dev, true);
571 return 0;
572}
573
574static int brcmstb_gpio_resume(struct device *dev)
575{
576 struct brcmstb_gpio_priv *priv = dev_get_drvdata(dev);
577 struct brcmstb_gpio_bank *bank;
578 bool need_wakeup_event = false;
579
580 list_for_each_entry(bank, &priv->bank_list, node) {
581 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
582 brcmstb_gpio_bank_restore(priv, bank);
583 }
584
585 if (priv->parent_wake_irq && need_wakeup_event)
586 pm_wakeup_event(dev, 0);
384 587
385 err = gpiochip_irqchip_add(&bank->gc, &bank->irq_chip, 0, 588 /* enable non-wake interrupt */
386 handle_simple_irq, IRQ_TYPE_NONE); 589 if (priv->parent_irq >= 0)
387 if (err) 590 enable_irq(priv->parent_irq);
388 return err;
389 gpiochip_set_chained_irqchip(&bank->gc, &bank->irq_chip,
390 priv->parent_irq, brcmstb_gpio_irq_handler);
391 591
392 return 0; 592 return 0;
393} 593}
394 594
595#else
596#define brcmstb_gpio_suspend NULL
597#define brcmstb_gpio_resume NULL
598#endif /* CONFIG_PM_SLEEP */
599
600static const struct dev_pm_ops brcmstb_gpio_pm_ops = {
601 .suspend_noirq = brcmstb_gpio_suspend,
602 .resume_noirq = brcmstb_gpio_resume,
603};
604
395static int brcmstb_gpio_probe(struct platform_device *pdev) 605static int brcmstb_gpio_probe(struct platform_device *pdev)
396{ 606{
397 struct device *dev = &pdev->dev; 607 struct device *dev = &pdev->dev;
@@ -406,6 +616,7 @@ static int brcmstb_gpio_probe(struct platform_device *pdev)
406 int err; 616 int err;
407 static int gpio_base; 617 static int gpio_base;
408 unsigned long flags = 0; 618 unsigned long flags = 0;
619 bool need_wakeup_event = false;
409 620
410 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 621 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
411 if (!priv) 622 if (!priv)
@@ -485,16 +696,23 @@ static int brcmstb_gpio_probe(struct platform_device *pdev)
485 gc->of_node = np; 696 gc->of_node = np;
486 gc->owner = THIS_MODULE; 697 gc->owner = THIS_MODULE;
487 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node); 698 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", dev->of_node);
699 if (!gc->label) {
700 err = -ENOMEM;
701 goto fail;
702 }
488 gc->base = gpio_base; 703 gc->base = gpio_base;
489 gc->of_gpio_n_cells = 2; 704 gc->of_gpio_n_cells = 2;
490 gc->of_xlate = brcmstb_gpio_of_xlate; 705 gc->of_xlate = brcmstb_gpio_of_xlate;
491 /* not all ngpio lines are valid, will use bank width later */ 706 /* not all ngpio lines are valid, will use bank width later */
492 gc->ngpio = MAX_GPIO_PER_BANK; 707 gc->ngpio = MAX_GPIO_PER_BANK;
708 if (priv->parent_irq > 0)
709 gc->to_irq = brcmstb_gpio_to_irq;
493 710
494 /* 711 /*
495 * Mask all interrupts by default, since wakeup interrupts may 712 * Mask all interrupts by default, since wakeup interrupts may
496 * be retained from S5 cold boot 713 * be retained from S5 cold boot
497 */ 714 */
715 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank);
498 gc->write_reg(reg_base + GIO_MASK(bank->id), 0); 716 gc->write_reg(reg_base + GIO_MASK(bank->id), 0);
499 717
500 err = gpiochip_add_data(gc, bank); 718 err = gpiochip_add_data(gc, bank);
@@ -505,12 +723,6 @@ static int brcmstb_gpio_probe(struct platform_device *pdev)
505 } 723 }
506 gpio_base += gc->ngpio; 724 gpio_base += gc->ngpio;
507 725
508 if (priv->parent_irq > 0) {
509 err = brcmstb_gpio_irq_setup(pdev, bank);
510 if (err)
511 goto fail;
512 }
513
514 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id, 726 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id,
515 gc->base, gc->ngpio, bank->width); 727 gc->base, gc->ngpio, bank->width);
516 728
@@ -520,9 +732,19 @@ static int brcmstb_gpio_probe(struct platform_device *pdev)
520 num_banks++; 732 num_banks++;
521 } 733 }
522 734
735 priv->num_gpios = gpio_base - priv->gpio_base;
736 if (priv->parent_irq > 0) {
737 err = brcmstb_gpio_irq_setup(pdev, priv);
738 if (err)
739 goto fail;
740 }
741
523 dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n", 742 dev_info(dev, "Registered %d banks (GPIO(s): %d-%d)\n",
524 num_banks, priv->gpio_base, gpio_base - 1); 743 num_banks, priv->gpio_base, gpio_base - 1);
525 744
745 if (priv->parent_wake_irq && need_wakeup_event)
746 pm_wakeup_event(dev, 0);
747
526 return 0; 748 return 0;
527 749
528fail: 750fail:
@@ -541,9 +763,11 @@ static struct platform_driver brcmstb_gpio_driver = {
541 .driver = { 763 .driver = {
542 .name = "brcmstb-gpio", 764 .name = "brcmstb-gpio",
543 .of_match_table = brcmstb_gpio_of_match, 765 .of_match_table = brcmstb_gpio_of_match,
766 .pm = &brcmstb_gpio_pm_ops,
544 }, 767 },
545 .probe = brcmstb_gpio_probe, 768 .probe = brcmstb_gpio_probe,
546 .remove = brcmstb_gpio_remove, 769 .remove = brcmstb_gpio_remove,
770 .shutdown = brcmstb_gpio_shutdown,
547}; 771};
548module_platform_driver(brcmstb_gpio_driver); 772module_platform_driver(brcmstb_gpio_driver);
549 773
diff --git a/drivers/gpio/gpio-crystalcove.c b/drivers/gpio/gpio-crystalcove.c
index e60156ec0c18..b6f0f729656c 100644
--- a/drivers/gpio/gpio-crystalcove.c
+++ b/drivers/gpio/gpio-crystalcove.c
@@ -295,7 +295,7 @@ static irqreturn_t crystalcove_gpio_irq_handler(int irq, void *data)
295 295
296 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) { 296 for (gpio = 0; gpio < CRYSTALCOVE_GPIO_NUM; gpio++) {
297 if (pending & BIT(gpio)) { 297 if (pending & BIT(gpio)) {
298 virq = irq_find_mapping(cg->chip.irqdomain, gpio); 298 virq = irq_find_mapping(cg->chip.irq.domain, gpio);
299 handle_nested_irq(virq); 299 handle_nested_irq(virq);
300 } 300 }
301 } 301 }
diff --git a/drivers/gpio/gpio-dln2.c b/drivers/gpio/gpio-dln2.c
index aecb847166f5..1dada68b9a27 100644
--- a/drivers/gpio/gpio-dln2.c
+++ b/drivers/gpio/gpio-dln2.c
@@ -420,7 +420,7 @@ static void dln2_gpio_event(struct platform_device *pdev, u16 echo,
420 return; 420 return;
421 } 421 }
422 422
423 irq = irq_find_mapping(dln2->gpio.irqdomain, pin); 423 irq = irq_find_mapping(dln2->gpio.irq.domain, pin);
424 if (!irq) { 424 if (!irq) {
425 dev_err(dln2->gpio.parent, "pin %d not mapped to IRQ\n", pin); 425 dev_err(dln2->gpio.parent, "pin %d not mapped to IRQ\n", pin);
426 return; 426 return;
diff --git a/drivers/gpio/gpio-dwapb.c b/drivers/gpio/gpio-dwapb.c
index c07ada9c7af6..6730c6642ce3 100644
--- a/drivers/gpio/gpio-dwapb.c
+++ b/drivers/gpio/gpio-dwapb.c
@@ -25,6 +25,7 @@
25#include <linux/of_irq.h> 25#include <linux/of_irq.h>
26#include <linux/platform_device.h> 26#include <linux/platform_device.h>
27#include <linux/property.h> 27#include <linux/property.h>
28#include <linux/reset.h>
28#include <linux/spinlock.h> 29#include <linux/spinlock.h>
29#include <linux/platform_data/gpio-dwapb.h> 30#include <linux/platform_data/gpio-dwapb.h>
30#include <linux/slab.h> 31#include <linux/slab.h>
@@ -77,6 +78,7 @@ struct dwapb_context {
77 u32 int_type; 78 u32 int_type;
78 u32 int_pol; 79 u32 int_pol;
79 u32 int_deb; 80 u32 int_deb;
81 u32 wake_en;
80}; 82};
81#endif 83#endif
82 84
@@ -97,6 +99,7 @@ struct dwapb_gpio {
97 unsigned int nr_ports; 99 unsigned int nr_ports;
98 struct irq_domain *domain; 100 struct irq_domain *domain;
99 unsigned int flags; 101 unsigned int flags;
102 struct reset_control *rst;
100}; 103};
101 104
102static inline u32 gpio_reg_v2_convert(unsigned int offset) 105static inline u32 gpio_reg_v2_convert(unsigned int offset)
@@ -295,13 +298,29 @@ static int dwapb_irq_set_type(struct irq_data *d, u32 type)
295 return 0; 298 return 0;
296} 299}
297 300
301#ifdef CONFIG_PM_SLEEP
302static int dwapb_irq_set_wake(struct irq_data *d, unsigned int enable)
303{
304 struct irq_chip_generic *igc = irq_data_get_irq_chip_data(d);
305 struct dwapb_gpio *gpio = igc->private;
306 struct dwapb_context *ctx = gpio->ports[0].ctx;
307
308 if (enable)
309 ctx->wake_en |= BIT(d->hwirq);
310 else
311 ctx->wake_en &= ~BIT(d->hwirq);
312
313 return 0;
314}
315#endif
316
298static int dwapb_gpio_set_debounce(struct gpio_chip *gc, 317static int dwapb_gpio_set_debounce(struct gpio_chip *gc,
299 unsigned offset, unsigned debounce) 318 unsigned offset, unsigned debounce)
300{ 319{
301 struct dwapb_gpio_port *port = gpiochip_get_data(gc); 320 struct dwapb_gpio_port *port = gpiochip_get_data(gc);
302 struct dwapb_gpio *gpio = port->gpio; 321 struct dwapb_gpio *gpio = port->gpio;
303 unsigned long flags, val_deb; 322 unsigned long flags, val_deb;
304 unsigned long mask = gc->pin2mask(gc, offset); 323 unsigned long mask = BIT(offset);
305 324
306 spin_lock_irqsave(&gc->bgpio_lock, flags); 325 spin_lock_irqsave(&gc->bgpio_lock, flags);
307 326
@@ -385,6 +404,9 @@ static void dwapb_configure_irqs(struct dwapb_gpio *gpio,
385 ct->chip.irq_disable = dwapb_irq_disable; 404 ct->chip.irq_disable = dwapb_irq_disable;
386 ct->chip.irq_request_resources = dwapb_irq_reqres; 405 ct->chip.irq_request_resources = dwapb_irq_reqres;
387 ct->chip.irq_release_resources = dwapb_irq_relres; 406 ct->chip.irq_release_resources = dwapb_irq_relres;
407#ifdef CONFIG_PM_SLEEP
408 ct->chip.irq_set_wake = dwapb_irq_set_wake;
409#endif
388 ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI); 410 ct->regs.ack = gpio_reg_convert(gpio, GPIO_PORTA_EOI);
389 ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK); 411 ct->regs.mask = gpio_reg_convert(gpio, GPIO_INTMASK);
390 ct->type = IRQ_TYPE_LEVEL_MASK; 412 ct->type = IRQ_TYPE_LEVEL_MASK;
@@ -460,7 +482,7 @@ static int dwapb_gpio_add_port(struct dwapb_gpio *gpio,
460 (pp->idx * GPIO_SWPORT_DDR_SIZE); 482 (pp->idx * GPIO_SWPORT_DDR_SIZE);
461 483
462 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout, 484 err = bgpio_init(&port->gc, gpio->dev, 4, dat, set, NULL, dirout,
463 NULL, false); 485 NULL, 0);
464 if (err) { 486 if (err) {
465 dev_err(gpio->dev, "failed to init gpio chip for port%d\n", 487 dev_err(gpio->dev, "failed to init gpio chip for port%d\n",
466 port->idx); 488 port->idx);
@@ -609,6 +631,12 @@ static int dwapb_gpio_probe(struct platform_device *pdev)
609 gpio->dev = &pdev->dev; 631 gpio->dev = &pdev->dev;
610 gpio->nr_ports = pdata->nports; 632 gpio->nr_ports = pdata->nports;
611 633
634 gpio->rst = devm_reset_control_get_optional_shared(dev, NULL);
635 if (IS_ERR(gpio->rst))
636 return PTR_ERR(gpio->rst);
637
638 reset_control_deassert(gpio->rst);
639
612 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports, 640 gpio->ports = devm_kcalloc(&pdev->dev, gpio->nr_ports,
613 sizeof(*gpio->ports), GFP_KERNEL); 641 sizeof(*gpio->ports), GFP_KERNEL);
614 if (!gpio->ports) 642 if (!gpio->ports)
@@ -660,6 +688,7 @@ static int dwapb_gpio_remove(struct platform_device *pdev)
660 688
661 dwapb_gpio_unregister(gpio); 689 dwapb_gpio_unregister(gpio);
662 dwapb_irq_teardown(gpio); 690 dwapb_irq_teardown(gpio);
691 reset_control_assert(gpio->rst);
663 692
664 return 0; 693 return 0;
665} 694}
@@ -699,7 +728,8 @@ static int dwapb_gpio_suspend(struct device *dev)
699 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE); 728 ctx->int_deb = dwapb_read(gpio, GPIO_PORTA_DEBOUNCE);
700 729
701 /* Mask out interrupts */ 730 /* Mask out interrupts */
702 dwapb_write(gpio, GPIO_INTMASK, 0xffffffff); 731 dwapb_write(gpio, GPIO_INTMASK,
732 0xffffffff & ~ctx->wake_en);
703 } 733 }
704 } 734 }
705 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 735 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
diff --git a/drivers/gpio/gpio-ftgpio010.c b/drivers/gpio/gpio-ftgpio010.c
index 139f73d3f4ba..7b3394fdc624 100644
--- a/drivers/gpio/gpio-ftgpio010.c
+++ b/drivers/gpio/gpio-ftgpio010.c
@@ -150,7 +150,7 @@ static void ftgpio_gpio_irq_handler(struct irq_desc *desc)
150 stat = readl(g->base + GPIO_INT_STAT); 150 stat = readl(g->base + GPIO_INT_STAT);
151 if (stat) 151 if (stat)
152 for_each_set_bit(offset, &stat, gc->ngpio) 152 for_each_set_bit(offset, &stat, gc->ngpio)
153 generic_handle_irq(irq_find_mapping(gc->irqdomain, 153 generic_handle_irq(irq_find_mapping(gc->irq.domain,
154 offset)); 154 offset));
155 155
156 chained_irq_exit(irqchip, desc); 156 chained_irq_exit(irqchip, desc);
diff --git a/drivers/gpio/gpio-grgpio.c b/drivers/gpio/gpio-grgpio.c
index 6544a16ab02e..e2fc561f4315 100644
--- a/drivers/gpio/gpio-grgpio.c
+++ b/drivers/gpio/gpio-grgpio.c
@@ -35,6 +35,7 @@
35#include <linux/interrupt.h> 35#include <linux/interrupt.h>
36#include <linux/irq.h> 36#include <linux/irq.h>
37#include <linux/irqdomain.h> 37#include <linux/irqdomain.h>
38#include <linux/bitops.h>
38 39
39#define GRGPIO_MAX_NGPIO 32 40#define GRGPIO_MAX_NGPIO 32
40 41
@@ -96,12 +97,11 @@ static void grgpio_set_imask(struct grgpio_priv *priv, unsigned int offset,
96 int val) 97 int val)
97{ 98{
98 struct gpio_chip *gc = &priv->gc; 99 struct gpio_chip *gc = &priv->gc;
99 unsigned long mask = gc->pin2mask(gc, offset);
100 100
101 if (val) 101 if (val)
102 priv->imask |= mask; 102 priv->imask |= BIT(offset);
103 else 103 else
104 priv->imask &= ~mask; 104 priv->imask &= ~BIT(offset);
105 gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask); 105 gc->write_reg(priv->regs + GRGPIO_IMASK, priv->imask);
106} 106}
107 107
diff --git a/drivers/gpio/gpio-ingenic.c b/drivers/gpio/gpio-ingenic.c
index 254780730b95..15fb2bc796a8 100644
--- a/drivers/gpio/gpio-ingenic.c
+++ b/drivers/gpio/gpio-ingenic.c
@@ -242,7 +242,7 @@ static void ingenic_gpio_irq_handler(struct irq_desc *desc)
242 flag = gpio_ingenic_read_reg(jzgc, JZ4740_GPIO_FLAG); 242 flag = gpio_ingenic_read_reg(jzgc, JZ4740_GPIO_FLAG);
243 243
244 for_each_set_bit(i, &flag, 32) 244 for_each_set_bit(i, &flag, 32)
245 generic_handle_irq(irq_linear_revmap(gc->irqdomain, i)); 245 generic_handle_irq(irq_linear_revmap(gc->irq.domain, i));
246 chained_irq_exit(irq_chip, desc); 246 chained_irq_exit(irq_chip, desc);
247} 247}
248 248
diff --git a/drivers/gpio/gpio-intel-mid.c b/drivers/gpio/gpio-intel-mid.c
index b76ecee82c3f..629575ea46a0 100644
--- a/drivers/gpio/gpio-intel-mid.c
+++ b/drivers/gpio/gpio-intel-mid.c
@@ -295,7 +295,7 @@ static void intel_mid_irq_handler(struct irq_desc *desc)
295 mask = BIT(gpio); 295 mask = BIT(gpio);
296 /* Clear before handling so we can't lose an edge */ 296 /* Clear before handling so we can't lose an edge */
297 writel(mask, gedr); 297 writel(mask, gedr);
298 generic_handle_irq(irq_find_mapping(gc->irqdomain, 298 generic_handle_irq(irq_find_mapping(gc->irq.domain,
299 base + gpio)); 299 base + gpio));
300 } 300 }
301 } 301 }
diff --git a/drivers/gpio/gpio-loongson1.c b/drivers/gpio/gpio-loongson1.c
index 72b64039241a..fca84ccac35c 100644
--- a/drivers/gpio/gpio-loongson1.c
+++ b/drivers/gpio/gpio-loongson1.c
@@ -11,6 +11,7 @@
11#include <linux/module.h> 11#include <linux/module.h>
12#include <linux/gpio/driver.h> 12#include <linux/gpio/driver.h>
13#include <linux/platform_device.h> 13#include <linux/platform_device.h>
14#include <linux/bitops.h>
14 15
15/* Loongson 1 GPIO Register Definitions */ 16/* Loongson 1 GPIO Register Definitions */
16#define GPIO_CFG 0x0 17#define GPIO_CFG 0x0
@@ -22,11 +23,10 @@ static void __iomem *gpio_reg_base;
22 23
23static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset) 24static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
24{ 25{
25 unsigned long pinmask = gc->pin2mask(gc, offset);
26 unsigned long flags; 26 unsigned long flags;
27 27
28 spin_lock_irqsave(&gc->bgpio_lock, flags); 28 spin_lock_irqsave(&gc->bgpio_lock, flags);
29 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | pinmask, 29 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) | BIT(offset),
30 gpio_reg_base + GPIO_CFG); 30 gpio_reg_base + GPIO_CFG);
31 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 31 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
32 32
@@ -35,11 +35,10 @@ static int ls1x_gpio_request(struct gpio_chip *gc, unsigned int offset)
35 35
36static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset) 36static void ls1x_gpio_free(struct gpio_chip *gc, unsigned int offset)
37{ 37{
38 unsigned long pinmask = gc->pin2mask(gc, offset);
39 unsigned long flags; 38 unsigned long flags;
40 39
41 spin_lock_irqsave(&gc->bgpio_lock, flags); 40 spin_lock_irqsave(&gc->bgpio_lock, flags);
42 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~pinmask, 41 __raw_writel(__raw_readl(gpio_reg_base + GPIO_CFG) & ~BIT(offset),
43 gpio_reg_base + GPIO_CFG); 42 gpio_reg_base + GPIO_CFG);
44 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 43 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
45} 44}
diff --git a/drivers/gpio/gpio-lynxpoint.c b/drivers/gpio/gpio-lynxpoint.c
index fbd393b46ce0..1e557b10d73e 100644
--- a/drivers/gpio/gpio-lynxpoint.c
+++ b/drivers/gpio/gpio-lynxpoint.c
@@ -255,7 +255,7 @@ static void lp_gpio_irq_handler(struct irq_desc *desc)
255 mask = BIT(pin); 255 mask = BIT(pin);
256 /* Clear before handling so we don't lose an edge */ 256 /* Clear before handling so we don't lose an edge */
257 outl(mask, reg); 257 outl(mask, reg);
258 irq = irq_find_mapping(lg->chip.irqdomain, base + pin); 258 irq = irq_find_mapping(lg->chip.irq.domain, base + pin);
259 generic_handle_irq(irq); 259 generic_handle_irq(irq);
260 } 260 }
261 } 261 }
diff --git a/drivers/gpio/gpio-max3191x.c b/drivers/gpio/gpio-max3191x.c
new file mode 100644
index 000000000000..f74b1072e84b
--- /dev/null
+++ b/drivers/gpio/gpio-max3191x.c
@@ -0,0 +1,492 @@
1/*
2 * gpio-max3191x.c - GPIO driver for Maxim MAX3191x industrial serializer
3 *
4 * Copyright (C) 2017 KUNBUS GmbH
5 *
6 * The MAX3191x makes 8 digital 24V inputs available via SPI.
7 * Multiple chips can be daisy-chained, the spec does not impose
8 * a limit on the number of chips and neither does this driver.
9 *
10 * Either of two modes is selectable: In 8-bit mode, only the state
11 * of the inputs is clocked out to achieve high readout speeds;
12 * In 16-bit mode, an additional status byte is clocked out with
13 * a CRC and indicator bits for undervoltage and overtemperature.
14 * The driver returns an error instead of potentially bogus data
15 * if any of these fault conditions occur. However it does allow
16 * readout of non-faulting chips in the same daisy-chain.
17 *
18 * MAX3191x supports four debounce settings and the driver is
19 * capable of configuring these differently for each chip in the
20 * daisy-chain.
21 *
22 * If the chips are hardwired to 8-bit mode ("modesel" pulled high),
23 * gpio-pisosr.c can be used alternatively to this driver.
24 *
25 * https://datasheets.maximintegrated.com/en/ds/MAX31910.pdf
26 * https://datasheets.maximintegrated.com/en/ds/MAX31911.pdf
27 * https://datasheets.maximintegrated.com/en/ds/MAX31912.pdf
28 * https://datasheets.maximintegrated.com/en/ds/MAX31913.pdf
29 * https://datasheets.maximintegrated.com/en/ds/MAX31953-MAX31963.pdf
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License (version 2) as
33 * published by the Free Software Foundation.
34 */
35
36#include <linux/bitmap.h>
37#include <linux/crc8.h>
38#include <linux/gpio/consumer.h>
39#include <linux/gpio/driver.h>
40#include <linux/module.h>
41#include <linux/spi/spi.h>
42
43enum max3191x_mode {
44 STATUS_BYTE_ENABLED,
45 STATUS_BYTE_DISABLED,
46};
47
48/**
49 * struct max3191x_chip - max3191x daisy-chain
50 * @gpio: GPIO controller struct
51 * @lock: protects read sequences
52 * @nchips: number of chips in the daisy-chain
53 * @mode: current mode, 0 for 16-bit, 1 for 8-bit;
54 * for simplicity, all chips in the daisy-chain are assumed
55 * to use the same mode
56 * @modesel_pins: GPIO pins to configure modesel of each chip
57 * @fault_pins: GPIO pins to detect fault of each chip
58 * @db0_pins: GPIO pins to configure debounce of each chip
59 * @db1_pins: GPIO pins to configure debounce of each chip
60 * @mesg: SPI message to perform a readout
61 * @xfer: SPI transfer used by @mesg
62 * @crc_error: bitmap signaling CRC error for each chip
63 * @overtemp: bitmap signaling overtemperature alarm for each chip
64 * @undervolt1: bitmap signaling undervoltage alarm for each chip
65 * @undervolt2: bitmap signaling undervoltage warning for each chip
66 * @fault: bitmap signaling assertion of @fault_pins for each chip
67 * @ignore_uv: whether to ignore undervoltage alarms;
68 * set by a device property if the chips are powered through
69 * 5VOUT instead of VCC24V, in which case they will constantly
70 * signal undervoltage;
71 * for simplicity, all chips in the daisy-chain are assumed
72 * to be powered the same way
73 */
74struct max3191x_chip {
75 struct gpio_chip gpio;
76 struct mutex lock;
77 u32 nchips;
78 enum max3191x_mode mode;
79 struct gpio_descs *modesel_pins;
80 struct gpio_descs *fault_pins;
81 struct gpio_descs *db0_pins;
82 struct gpio_descs *db1_pins;
83 struct spi_message mesg;
84 struct spi_transfer xfer;
85 unsigned long *crc_error;
86 unsigned long *overtemp;
87 unsigned long *undervolt1;
88 unsigned long *undervolt2;
89 unsigned long *fault;
90 bool ignore_uv;
91};
92
93#define MAX3191X_NGPIO 8
94#define MAX3191X_CRC8_POLYNOMIAL 0xa8 /* (x^5) + x^4 + x^2 + x^0 */
95
96DECLARE_CRC8_TABLE(max3191x_crc8);
97
98static int max3191x_get_direction(struct gpio_chip *gpio, unsigned int offset)
99{
100 return 1; /* always in */
101}
102
103static int max3191x_direction_input(struct gpio_chip *gpio, unsigned int offset)
104{
105 return 0;
106}
107
108static int max3191x_direction_output(struct gpio_chip *gpio,
109 unsigned int offset, int value)
110{
111 return -EINVAL;
112}
113
114static void max3191x_set(struct gpio_chip *gpio, unsigned int offset, int value)
115{ }
116
117static void max3191x_set_multiple(struct gpio_chip *gpio, unsigned long *mask,
118 unsigned long *bits)
119{ }
120
121static unsigned int max3191x_wordlen(struct max3191x_chip *max3191x)
122{
123 return max3191x->mode == STATUS_BYTE_ENABLED ? 2 : 1;
124}
125
126static int max3191x_readout_locked(struct max3191x_chip *max3191x)
127{
128 struct device *dev = max3191x->gpio.parent;
129 struct spi_device *spi = to_spi_device(dev);
130 int val, i, ot = 0, uv1 = 0;
131
132 val = spi_sync(spi, &max3191x->mesg);
133 if (val) {
134 dev_err_ratelimited(dev, "SPI receive error %d\n", val);
135 return val;
136 }
137
138 for (i = 0; i < max3191x->nchips; i++) {
139 if (max3191x->mode == STATUS_BYTE_ENABLED) {
140 u8 in = ((u8 *)max3191x->xfer.rx_buf)[i * 2];
141 u8 status = ((u8 *)max3191x->xfer.rx_buf)[i * 2 + 1];
142
143 val = (status & 0xf8) != crc8(max3191x_crc8, &in, 1, 0);
144 __assign_bit(i, max3191x->crc_error, val);
145 if (val)
146 dev_err_ratelimited(dev,
147 "chip %d: CRC error\n", i);
148
149 ot = (status >> 1) & 1;
150 __assign_bit(i, max3191x->overtemp, ot);
151 if (ot)
152 dev_err_ratelimited(dev,
153 "chip %d: overtemperature\n", i);
154
155 if (!max3191x->ignore_uv) {
156 uv1 = !((status >> 2) & 1);
157 __assign_bit(i, max3191x->undervolt1, uv1);
158 if (uv1)
159 dev_err_ratelimited(dev,
160 "chip %d: undervoltage\n", i);
161
162 val = !(status & 1);
163 __assign_bit(i, max3191x->undervolt2, val);
164 if (val && !uv1)
165 dev_warn_ratelimited(dev,
166 "chip %d: voltage warn\n", i);
167 }
168 }
169
170 if (max3191x->fault_pins && !max3191x->ignore_uv) {
171 /* fault pin shared by all chips or per chip */
172 struct gpio_desc *fault_pin =
173 (max3191x->fault_pins->ndescs == 1)
174 ? max3191x->fault_pins->desc[0]
175 : max3191x->fault_pins->desc[i];
176
177 val = gpiod_get_value_cansleep(fault_pin);
178 if (val < 0) {
179 dev_err_ratelimited(dev,
180 "GPIO read error %d\n", val);
181 return val;
182 }
183 __assign_bit(i, max3191x->fault, val);
184 if (val && !uv1 && !ot)
185 dev_err_ratelimited(dev,
186 "chip %d: fault\n", i);
187 }
188 }
189
190 return 0;
191}
192
193static bool max3191x_chip_is_faulting(struct max3191x_chip *max3191x,
194 unsigned int chipnum)
195{
196 /* without status byte the only diagnostic is the fault pin */
197 if (!max3191x->ignore_uv && test_bit(chipnum, max3191x->fault))
198 return true;
199
200 if (max3191x->mode == STATUS_BYTE_DISABLED)
201 return false;
202
203 return test_bit(chipnum, max3191x->crc_error) ||
204 test_bit(chipnum, max3191x->overtemp) ||
205 (!max3191x->ignore_uv &&
206 test_bit(chipnum, max3191x->undervolt1));
207}
208
209static int max3191x_get(struct gpio_chip *gpio, unsigned int offset)
210{
211 struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
212 int ret, chipnum, wordlen = max3191x_wordlen(max3191x);
213 u8 in;
214
215 mutex_lock(&max3191x->lock);
216 ret = max3191x_readout_locked(max3191x);
217 if (ret)
218 goto out_unlock;
219
220 chipnum = offset / MAX3191X_NGPIO;
221 if (max3191x_chip_is_faulting(max3191x, chipnum)) {
222 ret = -EIO;
223 goto out_unlock;
224 }
225
226 in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen];
227 ret = (in >> (offset % MAX3191X_NGPIO)) & 1;
228
229out_unlock:
230 mutex_unlock(&max3191x->lock);
231 return ret;
232}
233
234static int max3191x_get_multiple(struct gpio_chip *gpio, unsigned long *mask,
235 unsigned long *bits)
236{
237 struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
238 int ret, bit = 0, wordlen = max3191x_wordlen(max3191x);
239
240 mutex_lock(&max3191x->lock);
241 ret = max3191x_readout_locked(max3191x);
242 if (ret)
243 goto out_unlock;
244
245 while ((bit = find_next_bit(mask, gpio->ngpio, bit)) != gpio->ngpio) {
246 unsigned int chipnum = bit / MAX3191X_NGPIO;
247 unsigned long in, shift, index;
248
249 if (max3191x_chip_is_faulting(max3191x, chipnum)) {
250 ret = -EIO;
251 goto out_unlock;
252 }
253
254 in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen];
255 shift = round_down(bit % BITS_PER_LONG, MAX3191X_NGPIO);
256 index = bit / BITS_PER_LONG;
257 bits[index] &= ~(mask[index] & (0xff << shift));
258 bits[index] |= mask[index] & (in << shift); /* copy bits */
259
260 bit = (chipnum + 1) * MAX3191X_NGPIO; /* go to next chip */
261 }
262
263out_unlock:
264 mutex_unlock(&max3191x->lock);
265 return ret;
266}
267
268static int max3191x_set_config(struct gpio_chip *gpio, unsigned int offset,
269 unsigned long config)
270{
271 struct max3191x_chip *max3191x = gpiochip_get_data(gpio);
272 u32 debounce, chipnum, db0_val, db1_val;
273
274 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
275 return -ENOTSUPP;
276
277 if (!max3191x->db0_pins || !max3191x->db1_pins)
278 return -EINVAL;
279
280 debounce = pinconf_to_config_argument(config);
281 switch (debounce) {
282 case 0:
283 db0_val = 0;
284 db1_val = 0;
285 break;
286 case 1 ... 25:
287 db0_val = 0;
288 db1_val = 1;
289 break;
290 case 26 ... 750:
291 db0_val = 1;
292 db1_val = 0;
293 break;
294 case 751 ... 3000:
295 db0_val = 1;
296 db1_val = 1;
297 break;
298 default:
299 return -EINVAL;
300 }
301
302 if (max3191x->db0_pins->ndescs == 1)
303 chipnum = 0; /* all chips use the same pair of debounce pins */
304 else
305 chipnum = offset / MAX3191X_NGPIO; /* per chip debounce pins */
306
307 mutex_lock(&max3191x->lock);
308 gpiod_set_value_cansleep(max3191x->db0_pins->desc[chipnum], db0_val);
309 gpiod_set_value_cansleep(max3191x->db1_pins->desc[chipnum], db1_val);
310 mutex_unlock(&max3191x->lock);
311 return 0;
312}
313
314static void gpiod_set_array_single_value_cansleep(unsigned int ndescs,
315 struct gpio_desc **desc,
316 int value)
317{
318 int i, values[ndescs];
319
320 for (i = 0; i < ndescs; i++)
321 values[i] = value;
322
323 gpiod_set_array_value_cansleep(ndescs, desc, values);
324}
325
326static struct gpio_descs *devm_gpiod_get_array_optional_count(
327 struct device *dev, const char *con_id,
328 enum gpiod_flags flags, unsigned int expected)
329{
330 struct gpio_descs *descs;
331 int found = gpiod_count(dev, con_id);
332
333 if (found == -ENOENT)
334 return NULL;
335
336 if (found != expected && found != 1) {
337 dev_err(dev, "ignoring %s-gpios: found %d, expected %u or 1\n",
338 con_id, found, expected);
339 return NULL;
340 }
341
342 descs = devm_gpiod_get_array_optional(dev, con_id, flags);
343
344 if (IS_ERR(descs)) {
345 dev_err(dev, "failed to get %s-gpios: %ld\n",
346 con_id, PTR_ERR(descs));
347 return NULL;
348 }
349
350 return descs;
351}
352
353static int max3191x_probe(struct spi_device *spi)
354{
355 struct device *dev = &spi->dev;
356 struct max3191x_chip *max3191x;
357 int n, ret;
358
359 max3191x = devm_kzalloc(dev, sizeof(*max3191x), GFP_KERNEL);
360 if (!max3191x)
361 return -ENOMEM;
362 spi_set_drvdata(spi, max3191x);
363
364 max3191x->nchips = 1;
365 device_property_read_u32(dev, "#daisy-chained-devices",
366 &max3191x->nchips);
367
368 n = BITS_TO_LONGS(max3191x->nchips);
369 max3191x->crc_error = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
370 max3191x->undervolt1 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
371 max3191x->undervolt2 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
372 max3191x->overtemp = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
373 max3191x->fault = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL);
374 max3191x->xfer.rx_buf = devm_kcalloc(dev, max3191x->nchips,
375 2, GFP_KERNEL);
376 if (!max3191x->crc_error || !max3191x->undervolt1 ||
377 !max3191x->overtemp || !max3191x->undervolt2 ||
378 !max3191x->fault || !max3191x->xfer.rx_buf)
379 return -ENOMEM;
380
381 max3191x->modesel_pins = devm_gpiod_get_array_optional_count(dev,
382 "maxim,modesel", GPIOD_ASIS, max3191x->nchips);
383 max3191x->fault_pins = devm_gpiod_get_array_optional_count(dev,
384 "maxim,fault", GPIOD_IN, max3191x->nchips);
385 max3191x->db0_pins = devm_gpiod_get_array_optional_count(dev,
386 "maxim,db0", GPIOD_OUT_LOW, max3191x->nchips);
387 max3191x->db1_pins = devm_gpiod_get_array_optional_count(dev,
388 "maxim,db1", GPIOD_OUT_LOW, max3191x->nchips);
389
390 max3191x->mode = device_property_read_bool(dev, "maxim,modesel-8bit")
391 ? STATUS_BYTE_DISABLED : STATUS_BYTE_ENABLED;
392 if (max3191x->modesel_pins)
393 gpiod_set_array_single_value_cansleep(
394 max3191x->modesel_pins->ndescs,
395 max3191x->modesel_pins->desc, max3191x->mode);
396
397 max3191x->ignore_uv = device_property_read_bool(dev,
398 "maxim,ignore-undervoltage");
399
400 if (max3191x->db0_pins && max3191x->db1_pins &&
401 max3191x->db0_pins->ndescs != max3191x->db1_pins->ndescs) {
402 dev_err(dev, "ignoring maxim,db*-gpios: array len mismatch\n");
403 devm_gpiod_put_array(dev, max3191x->db0_pins);
404 devm_gpiod_put_array(dev, max3191x->db1_pins);
405 max3191x->db0_pins = NULL;
406 max3191x->db1_pins = NULL;
407 }
408
409 max3191x->xfer.len = max3191x->nchips * max3191x_wordlen(max3191x);
410 spi_message_init_with_transfers(&max3191x->mesg, &max3191x->xfer, 1);
411
412 max3191x->gpio.label = spi->modalias;
413 max3191x->gpio.owner = THIS_MODULE;
414 max3191x->gpio.parent = dev;
415 max3191x->gpio.base = -1;
416 max3191x->gpio.ngpio = max3191x->nchips * MAX3191X_NGPIO;
417 max3191x->gpio.can_sleep = true;
418
419 max3191x->gpio.get_direction = max3191x_get_direction;
420 max3191x->gpio.direction_input = max3191x_direction_input;
421 max3191x->gpio.direction_output = max3191x_direction_output;
422 max3191x->gpio.set = max3191x_set;
423 max3191x->gpio.set_multiple = max3191x_set_multiple;
424 max3191x->gpio.get = max3191x_get;
425 max3191x->gpio.get_multiple = max3191x_get_multiple;
426 max3191x->gpio.set_config = max3191x_set_config;
427
428 mutex_init(&max3191x->lock);
429
430 ret = gpiochip_add_data(&max3191x->gpio, max3191x);
431 if (ret) {
432 mutex_destroy(&max3191x->lock);
433 return ret;
434 }
435
436 return 0;
437}
438
439static int max3191x_remove(struct spi_device *spi)
440{
441 struct max3191x_chip *max3191x = spi_get_drvdata(spi);
442
443 gpiochip_remove(&max3191x->gpio);
444 mutex_destroy(&max3191x->lock);
445
446 return 0;
447}
448
449static int __init max3191x_register_driver(struct spi_driver *sdrv)
450{
451 crc8_populate_msb(max3191x_crc8, MAX3191X_CRC8_POLYNOMIAL);
452 return spi_register_driver(sdrv);
453}
454
455#ifdef CONFIG_OF
456static const struct of_device_id max3191x_of_id[] = {
457 { .compatible = "maxim,max31910" },
458 { .compatible = "maxim,max31911" },
459 { .compatible = "maxim,max31912" },
460 { .compatible = "maxim,max31913" },
461 { .compatible = "maxim,max31953" },
462 { .compatible = "maxim,max31963" },
463 { }
464};
465MODULE_DEVICE_TABLE(of, max3191x_of_id);
466#endif
467
468static const struct spi_device_id max3191x_spi_id[] = {
469 { "max31910" },
470 { "max31911" },
471 { "max31912" },
472 { "max31913" },
473 { "max31953" },
474 { "max31963" },
475 { }
476};
477MODULE_DEVICE_TABLE(spi, max3191x_spi_id);
478
479static struct spi_driver max3191x_driver = {
480 .driver = {
481 .name = "max3191x",
482 .of_match_table = of_match_ptr(max3191x_of_id),
483 },
484 .probe = max3191x_probe,
485 .remove = max3191x_remove,
486 .id_table = max3191x_spi_id,
487};
488module_driver(max3191x_driver, max3191x_register_driver, spi_unregister_driver);
489
490MODULE_AUTHOR("Lukas Wunner <lukas@wunner.de>");
491MODULE_DESCRIPTION("GPIO driver for Maxim MAX3191x industrial serializer");
492MODULE_LICENSE("GPL v2");
diff --git a/drivers/gpio/gpio-max732x.c b/drivers/gpio/gpio-max732x.c
index 7f4d26ce5f23..c04fae1ba32a 100644
--- a/drivers/gpio/gpio-max732x.c
+++ b/drivers/gpio/gpio-max732x.c
@@ -486,7 +486,7 @@ static irqreturn_t max732x_irq_handler(int irq, void *devid)
486 486
487 do { 487 do {
488 level = __ffs(pending); 488 level = __ffs(pending);
489 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain, 489 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
490 level)); 490 level));
491 491
492 pending &= ~(1 << level); 492 pending &= ~(1 << level);
diff --git a/drivers/gpio/gpio-mb86s7x.c b/drivers/gpio/gpio-mb86s7x.c
index 94d772677ed6..3134c0d2bfe4 100644
--- a/drivers/gpio/gpio-mb86s7x.c
+++ b/drivers/gpio/gpio-mb86s7x.c
@@ -17,6 +17,7 @@
17#include <linux/io.h> 17#include <linux/io.h>
18#include <linux/init.h> 18#include <linux/init.h>
19#include <linux/clk.h> 19#include <linux/clk.h>
20#include <linux/module.h>
20#include <linux/err.h> 21#include <linux/err.h>
21#include <linux/errno.h> 22#include <linux/errno.h>
22#include <linux/ioport.h> 23#include <linux/ioport.h>
@@ -52,11 +53,6 @@ static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
52 spin_lock_irqsave(&gchip->lock, flags); 53 spin_lock_irqsave(&gchip->lock, flags);
53 54
54 val = readl(gchip->base + PFR(gpio)); 55 val = readl(gchip->base + PFR(gpio));
55 if (!(val & OFFSET(gpio))) {
56 spin_unlock_irqrestore(&gchip->lock, flags);
57 return -EINVAL;
58 }
59
60 val &= ~OFFSET(gpio); 56 val &= ~OFFSET(gpio);
61 writel(val, gchip->base + PFR(gpio)); 57 writel(val, gchip->base + PFR(gpio));
62 58
@@ -209,6 +205,7 @@ static const struct of_device_id mb86s70_gpio_dt_ids[] = {
209 { .compatible = "fujitsu,mb86s70-gpio" }, 205 { .compatible = "fujitsu,mb86s70-gpio" },
210 { /* sentinel */ } 206 { /* sentinel */ }
211}; 207};
208MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
212 209
213static struct platform_driver mb86s70_gpio_driver = { 210static struct platform_driver mb86s70_gpio_driver = {
214 .driver = { 211 .driver = {
@@ -218,5 +215,8 @@ static struct platform_driver mb86s70_gpio_driver = {
218 .probe = mb86s70_gpio_probe, 215 .probe = mb86s70_gpio_probe,
219 .remove = mb86s70_gpio_remove, 216 .remove = mb86s70_gpio_remove,
220}; 217};
218module_platform_driver(mb86s70_gpio_driver);
221 219
222builtin_platform_driver(mb86s70_gpio_driver); 220MODULE_DESCRIPTION("MB86S7x GPIO Driver");
221MODULE_ALIAS("platform:mb86s70-gpio");
222MODULE_LICENSE("GPL");
diff --git a/drivers/gpio/gpio-merrifield.c b/drivers/gpio/gpio-merrifield.c
index ec8560298805..dd67a31ac337 100644
--- a/drivers/gpio/gpio-merrifield.c
+++ b/drivers/gpio/gpio-merrifield.c
@@ -357,7 +357,7 @@ static void mrfld_irq_handler(struct irq_desc *desc)
357 for_each_set_bit(gpio, &pending, 32) { 357 for_each_set_bit(gpio, &pending, 32) {
358 unsigned int irq; 358 unsigned int irq;
359 359
360 irq = irq_find_mapping(gc->irqdomain, base + gpio); 360 irq = irq_find_mapping(gc->irq.domain, base + gpio);
361 generic_handle_irq(irq); 361 generic_handle_irq(irq);
362 } 362 }
363 } 363 }
diff --git a/drivers/gpio/gpio-mmio.c b/drivers/gpio/gpio-mmio.c
index f7da40e46c55..f9042bcc27a4 100644
--- a/drivers/gpio/gpio-mmio.c
+++ b/drivers/gpio/gpio-mmio.c
@@ -126,20 +126,16 @@ static unsigned long bgpio_read32be(void __iomem *reg)
126 return ioread32be(reg); 126 return ioread32be(reg);
127} 127}
128 128
129static unsigned long bgpio_pin2mask(struct gpio_chip *gc, unsigned int pin) 129static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line)
130{ 130{
131 return BIT(pin); 131 if (gc->be_bits)
132} 132 return BIT(gc->bgpio_bits - 1 - line);
133 133 return BIT(line);
134static unsigned long bgpio_pin2mask_be(struct gpio_chip *gc,
135 unsigned int pin)
136{
137 return BIT(gc->bgpio_bits - 1 - pin);
138} 134}
139 135
140static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio) 136static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
141{ 137{
142 unsigned long pinmask = gc->pin2mask(gc, gpio); 138 unsigned long pinmask = bgpio_line2mask(gc, gpio);
143 139
144 if (gc->bgpio_dir & pinmask) 140 if (gc->bgpio_dir & pinmask)
145 return !!(gc->read_reg(gc->reg_set) & pinmask); 141 return !!(gc->read_reg(gc->reg_set) & pinmask);
@@ -147,9 +143,76 @@ static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
147 return !!(gc->read_reg(gc->reg_dat) & pinmask); 143 return !!(gc->read_reg(gc->reg_dat) & pinmask);
148} 144}
149 145
146/*
147 * This assumes that the bits in the GPIO register are in native endianness.
148 * We only assign the function pointer if we have that.
149 */
150static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
151 unsigned long *bits)
152{
153 unsigned long get_mask = 0;
154 unsigned long set_mask = 0;
155 int bit = 0;
156
157 while ((bit = find_next_bit(mask, gc->ngpio, bit)) != gc->ngpio) {
158 if (gc->bgpio_dir & BIT(bit))
159 set_mask |= BIT(bit);
160 else
161 get_mask |= BIT(bit);
162 }
163
164 if (set_mask)
165 *bits |= gc->read_reg(gc->reg_set) & set_mask;
166 if (get_mask)
167 *bits |= gc->read_reg(gc->reg_dat) & get_mask;
168
169 return 0;
170}
171
150static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) 172static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
151{ 173{
152 return !!(gc->read_reg(gc->reg_dat) & gc->pin2mask(gc, gpio)); 174 return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio));
175}
176
177/*
178 * This only works if the bits in the GPIO register are in native endianness.
179 * It is dirt simple and fast in this case. (Also the most common case.)
180 */
181static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask,
182 unsigned long *bits)
183{
184
185 *bits = gc->read_reg(gc->reg_dat) & *mask;
186 return 0;
187}
188
189/*
190 * With big endian mirrored bit order it becomes more tedious.
191 */
192static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask,
193 unsigned long *bits)
194{
195 unsigned long readmask = 0;
196 unsigned long val;
197 int bit;
198
199 /* Create a mirrored mask */
200 bit = 0;
201 while ((bit = find_next_bit(mask, gc->ngpio, bit)) != gc->ngpio)
202 readmask |= bgpio_line2mask(gc, bit);
203
204 /* Read the register */
205 val = gc->read_reg(gc->reg_dat) & readmask;
206
207 /*
208 * Mirror the result into the "bits" result, this will give line 0
209 * in bit 0 ... line 31 in bit 31 for a 32bit register.
210 */
211 bit = 0;
212 while ((bit = find_next_bit(&val, gc->ngpio, bit)) != gc->ngpio)
213 *bits |= bgpio_line2mask(gc, bit);
214
215 return 0;
153} 216}
154 217
155static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val) 218static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
@@ -158,7 +221,7 @@ static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
158 221
159static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 222static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
160{ 223{
161 unsigned long mask = gc->pin2mask(gc, gpio); 224 unsigned long mask = bgpio_line2mask(gc, gpio);
162 unsigned long flags; 225 unsigned long flags;
163 226
164 spin_lock_irqsave(&gc->bgpio_lock, flags); 227 spin_lock_irqsave(&gc->bgpio_lock, flags);
@@ -176,7 +239,7 @@ static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
176static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, 239static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
177 int val) 240 int val)
178{ 241{
179 unsigned long mask = gc->pin2mask(gc, gpio); 242 unsigned long mask = bgpio_line2mask(gc, gpio);
180 243
181 if (val) 244 if (val)
182 gc->write_reg(gc->reg_set, mask); 245 gc->write_reg(gc->reg_set, mask);
@@ -186,7 +249,7 @@ static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
186 249
187static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) 250static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
188{ 251{
189 unsigned long mask = gc->pin2mask(gc, gpio); 252 unsigned long mask = bgpio_line2mask(gc, gpio);
190 unsigned long flags; 253 unsigned long flags;
191 254
192 spin_lock_irqsave(&gc->bgpio_lock, flags); 255 spin_lock_irqsave(&gc->bgpio_lock, flags);
@@ -216,9 +279,9 @@ static void bgpio_multiple_get_masks(struct gpio_chip *gc,
216 break; 279 break;
217 if (__test_and_clear_bit(i, mask)) { 280 if (__test_and_clear_bit(i, mask)) {
218 if (test_bit(i, bits)) 281 if (test_bit(i, bits))
219 *set_mask |= gc->pin2mask(gc, i); 282 *set_mask |= bgpio_line2mask(gc, i);
220 else 283 else
221 *clear_mask |= gc->pin2mask(gc, i); 284 *clear_mask |= bgpio_line2mask(gc, i);
222 } 285 }
223 } 286 }
224} 287}
@@ -294,7 +357,7 @@ static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
294 357
295 spin_lock_irqsave(&gc->bgpio_lock, flags); 358 spin_lock_irqsave(&gc->bgpio_lock, flags);
296 359
297 gc->bgpio_dir &= ~gc->pin2mask(gc, gpio); 360 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
298 gc->write_reg(gc->reg_dir, gc->bgpio_dir); 361 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
299 362
300 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 363 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
@@ -305,7 +368,7 @@ static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
305static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) 368static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
306{ 369{
307 /* Return 0 if output, 1 of input */ 370 /* Return 0 if output, 1 of input */
308 return !(gc->read_reg(gc->reg_dir) & gc->pin2mask(gc, gpio)); 371 return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
309} 372}
310 373
311static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 374static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
@@ -316,7 +379,7 @@ static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
316 379
317 spin_lock_irqsave(&gc->bgpio_lock, flags); 380 spin_lock_irqsave(&gc->bgpio_lock, flags);
318 381
319 gc->bgpio_dir |= gc->pin2mask(gc, gpio); 382 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
320 gc->write_reg(gc->reg_dir, gc->bgpio_dir); 383 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
321 384
322 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 385 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
@@ -330,7 +393,7 @@ static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
330 393
331 spin_lock_irqsave(&gc->bgpio_lock, flags); 394 spin_lock_irqsave(&gc->bgpio_lock, flags);
332 395
333 gc->bgpio_dir |= gc->pin2mask(gc, gpio); 396 gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
334 gc->write_reg(gc->reg_dir, gc->bgpio_dir); 397 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
335 398
336 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 399 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
@@ -346,7 +409,7 @@ static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
346 409
347 spin_lock_irqsave(&gc->bgpio_lock, flags); 410 spin_lock_irqsave(&gc->bgpio_lock, flags);
348 411
349 gc->bgpio_dir &= ~gc->pin2mask(gc, gpio); 412 gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
350 gc->write_reg(gc->reg_dir, gc->bgpio_dir); 413 gc->write_reg(gc->reg_dir, gc->bgpio_dir);
351 414
352 spin_unlock_irqrestore(&gc->bgpio_lock, flags); 415 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
@@ -357,12 +420,11 @@ static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
357static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio) 420static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio)
358{ 421{
359 /* Return 0 if output, 1 if input */ 422 /* Return 0 if output, 1 if input */
360 return !!(gc->read_reg(gc->reg_dir) & gc->pin2mask(gc, gpio)); 423 return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
361} 424}
362 425
363static int bgpio_setup_accessors(struct device *dev, 426static int bgpio_setup_accessors(struct device *dev,
364 struct gpio_chip *gc, 427 struct gpio_chip *gc,
365 bool bit_be,
366 bool byte_be) 428 bool byte_be)
367{ 429{
368 430
@@ -406,8 +468,6 @@ static int bgpio_setup_accessors(struct device *dev,
406 return -EINVAL; 468 return -EINVAL;
407 } 469 }
408 470
409 gc->pin2mask = bit_be ? bgpio_pin2mask_be : bgpio_pin2mask;
410
411 return 0; 471 return 0;
412} 472}
413 473
@@ -462,10 +522,24 @@ static int bgpio_setup_io(struct gpio_chip *gc,
462 } 522 }
463 523
464 if (!(flags & BGPIOF_UNREADABLE_REG_SET) && 524 if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
465 (flags & BGPIOF_READ_OUTPUT_REG_SET)) 525 (flags & BGPIOF_READ_OUTPUT_REG_SET)) {
466 gc->get = bgpio_get_set; 526 gc->get = bgpio_get_set;
467 else 527 if (!gc->be_bits)
528 gc->get_multiple = bgpio_get_set_multiple;
529 /*
530 * We deliberately avoid assigning the ->get_multiple() call
531 * for big endian mirrored registers which are ALSO reflecting
532 * their value in the set register when used as output. It is
533 * simply too much complexity, let the GPIO core fall back to
534 * reading each line individually in that fringe case.
535 */
536 } else {
468 gc->get = bgpio_get; 537 gc->get = bgpio_get;
538 if (gc->be_bits)
539 gc->get_multiple = bgpio_get_multiple_be;
540 else
541 gc->get_multiple = bgpio_get_multiple;
542 }
469 543
470 return 0; 544 return 0;
471} 545}
@@ -526,13 +600,13 @@ int bgpio_init(struct gpio_chip *gc, struct device *dev,
526 gc->base = -1; 600 gc->base = -1;
527 gc->ngpio = gc->bgpio_bits; 601 gc->ngpio = gc->bgpio_bits;
528 gc->request = bgpio_request; 602 gc->request = bgpio_request;
603 gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN);
529 604
530 ret = bgpio_setup_io(gc, dat, set, clr, flags); 605 ret = bgpio_setup_io(gc, dat, set, clr, flags);
531 if (ret) 606 if (ret)
532 return ret; 607 return ret;
533 608
534 ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN, 609 ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
535 flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
536 if (ret) 610 if (ret)
537 return ret; 611 return ret;
538 612
diff --git a/drivers/gpio/gpio-mpc8xxx.c b/drivers/gpio/gpio-mpc8xxx.c
index 8c93dec498fa..c8673a5d9412 100644
--- a/drivers/gpio/gpio-mpc8xxx.c
+++ b/drivers/gpio/gpio-mpc8xxx.c
@@ -21,6 +21,7 @@
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/irq.h> 22#include <linux/irq.h>
23#include <linux/gpio/driver.h> 23#include <linux/gpio/driver.h>
24#include <linux/bitops.h>
24 25
25#define MPC8XXX_GPIO_PINS 32 26#define MPC8XXX_GPIO_PINS 32
26 27
@@ -44,6 +45,16 @@ struct mpc8xxx_gpio_chip {
44 unsigned int irqn; 45 unsigned int irqn;
45}; 46};
46 47
48/*
49 * This hardware has a big endian bit assignment such that GPIO line 0 is
50 * connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
51 * This inline helper give the right bitmask for a certain line.
52 */
53static inline u32 mpc_pin2mask(unsigned int offset)
54{
55 return BIT(31 - offset);
56}
57
47/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs 58/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
48 * defined as output cannot be determined by reading GPDAT register, 59 * defined as output cannot be determined by reading GPDAT register,
49 * so we use shadow data register instead. The status of input pins 60 * so we use shadow data register instead. The status of input pins
@@ -59,7 +70,7 @@ static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
59 val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask; 70 val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
60 out_shadow = gc->bgpio_data & out_mask; 71 out_shadow = gc->bgpio_data & out_mask;
61 72
62 return !!((val | out_shadow) & gc->pin2mask(gc, gpio)); 73 return !!((val | out_shadow) & mpc_pin2mask(gpio));
63} 74}
64 75
65static int mpc5121_gpio_dir_out(struct gpio_chip *gc, 76static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
@@ -120,7 +131,7 @@ static void mpc8xxx_irq_unmask(struct irq_data *d)
120 131
121 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 132 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
122 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR) 133 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
123 | gc->pin2mask(gc, irqd_to_hwirq(d))); 134 | mpc_pin2mask(irqd_to_hwirq(d)));
124 135
125 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); 136 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
126} 137}
@@ -135,7 +146,7 @@ static void mpc8xxx_irq_mask(struct irq_data *d)
135 146
136 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 147 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
137 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR) 148 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
138 & ~(gc->pin2mask(gc, irqd_to_hwirq(d)))); 149 & ~mpc_pin2mask(irqd_to_hwirq(d)));
139 150
140 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); 151 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
141} 152}
@@ -146,7 +157,7 @@ static void mpc8xxx_irq_ack(struct irq_data *d)
146 struct gpio_chip *gc = &mpc8xxx_gc->gc; 157 struct gpio_chip *gc = &mpc8xxx_gc->gc;
147 158
148 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 159 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
149 gc->pin2mask(gc, irqd_to_hwirq(d))); 160 mpc_pin2mask(irqd_to_hwirq(d)));
150} 161}
151 162
152static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type) 163static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
@@ -160,7 +171,7 @@ static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
160 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); 171 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
161 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR, 172 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
162 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR) 173 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
163 | gc->pin2mask(gc, irqd_to_hwirq(d))); 174 | mpc_pin2mask(irqd_to_hwirq(d)));
164 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); 175 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
165 break; 176 break;
166 177
@@ -168,7 +179,7 @@ static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
168 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags); 179 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
169 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR, 180 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
170 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR) 181 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
171 & ~(gc->pin2mask(gc, irqd_to_hwirq(d)))); 182 & ~mpc_pin2mask(irqd_to_hwirq(d)));
172 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags); 183 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
173 break; 184 break;
174 185
diff --git a/drivers/gpio/gpio-omap.c b/drivers/gpio/gpio-omap.c
index 3233b72b6828..e136d666f1e5 100644
--- a/drivers/gpio/gpio-omap.c
+++ b/drivers/gpio/gpio-omap.c
@@ -737,7 +737,7 @@ static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
737 737
738 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags); 738 raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);
739 739
740 generic_handle_irq(irq_find_mapping(bank->chip.irqdomain, 740 generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
741 bit)); 741 bit));
742 742
743 raw_spin_unlock_irqrestore(&bank->wa_lock, 743 raw_spin_unlock_irqrestore(&bank->wa_lock,
diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c
index 1b9dbf691ae7..babb7bd2ba59 100644
--- a/drivers/gpio/gpio-pca953x.c
+++ b/drivers/gpio/gpio-pca953x.c
@@ -608,7 +608,7 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)
608 for (i = 0; i < NBANK(chip); i++) { 608 for (i = 0; i < NBANK(chip); i++) {
609 while (pending[i]) { 609 while (pending[i]) {
610 level = __ffs(pending[i]); 610 level = __ffs(pending[i]);
611 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain, 611 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
612 level + (BANK_SZ * i))); 612 level + (BANK_SZ * i)));
613 pending[i] &= ~(1 << level); 613 pending[i] &= ~(1 << level);
614 nhandled++; 614 nhandled++;
diff --git a/drivers/gpio/gpio-pcf857x.c b/drivers/gpio/gpio-pcf857x.c
index a4fd78b9c0e4..38fbb420c6cd 100644
--- a/drivers/gpio/gpio-pcf857x.c
+++ b/drivers/gpio/gpio-pcf857x.c
@@ -196,7 +196,7 @@ static irqreturn_t pcf857x_irq(int irq, void *data)
196 mutex_unlock(&gpio->lock); 196 mutex_unlock(&gpio->lock);
197 197
198 for_each_set_bit(i, &change, gpio->chip.ngpio) 198 for_each_set_bit(i, &change, gpio->chip.ngpio)
199 handle_nested_irq(irq_find_mapping(gpio->chip.irqdomain, i)); 199 handle_nested_irq(irq_find_mapping(gpio->chip.irq.domain, i));
200 200
201 return IRQ_HANDLED; 201 return IRQ_HANDLED;
202} 202}
diff --git a/drivers/gpio/gpio-pci-idio-16.c b/drivers/gpio/gpio-pci-idio-16.c
index 7de4f6a2cb49..57d1b7fbf07b 100644
--- a/drivers/gpio/gpio-pci-idio-16.c
+++ b/drivers/gpio/gpio-pci-idio-16.c
@@ -240,7 +240,7 @@ static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
240 return IRQ_NONE; 240 return IRQ_NONE;
241 241
242 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio) 242 for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
243 generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio)); 243 generic_handle_irq(irq_find_mapping(chip->irq.domain, gpio));
244 244
245 raw_spin_lock(&idio16gpio->lock); 245 raw_spin_lock(&idio16gpio->lock);
246 246
diff --git a/drivers/gpio/gpio-pl061.c b/drivers/gpio/gpio-pl061.c
index 6aaaab79c205..b70974cb9ef1 100644
--- a/drivers/gpio/gpio-pl061.c
+++ b/drivers/gpio/gpio-pl061.c
@@ -221,7 +221,7 @@ static void pl061_irq_handler(struct irq_desc *desc)
221 pending = readb(pl061->base + GPIOMIS); 221 pending = readb(pl061->base + GPIOMIS);
222 if (pending) { 222 if (pending) {
223 for_each_set_bit(offset, &pending, PL061_GPIO_NR) 223 for_each_set_bit(offset, &pending, PL061_GPIO_NR)
224 generic_handle_irq(irq_find_mapping(gc->irqdomain, 224 generic_handle_irq(irq_find_mapping(gc->irq.domain,
225 offset)); 225 offset));
226 } 226 }
227 227
diff --git a/drivers/gpio/gpio-rcar.c b/drivers/gpio/gpio-rcar.c
index 1f0871553fd2..0ea998a3e357 100644
--- a/drivers/gpio/gpio-rcar.c
+++ b/drivers/gpio/gpio-rcar.c
@@ -24,6 +24,7 @@
24#include <linux/irq.h> 24#include <linux/irq.h>
25#include <linux/module.h> 25#include <linux/module.h>
26#include <linux/of.h> 26#include <linux/of.h>
27#include <linux/of_device.h>
27#include <linux/pinctrl/consumer.h> 28#include <linux/pinctrl/consumer.h>
28#include <linux/platform_device.h> 29#include <linux/platform_device.h>
29#include <linux/pm_runtime.h> 30#include <linux/pm_runtime.h>
@@ -206,7 +207,7 @@ static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
206 gpio_rcar_read(p, INTMSK))) { 207 gpio_rcar_read(p, INTMSK))) {
207 offset = __ffs(pending); 208 offset = __ffs(pending);
208 gpio_rcar_write(p, INTCLR, BIT(offset)); 209 gpio_rcar_write(p, INTCLR, BIT(offset));
209 generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain, 210 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
210 offset)); 211 offset));
211 irqs_handled++; 212 irqs_handled++;
212 } 213 }
@@ -393,16 +394,11 @@ MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
393static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins) 394static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
394{ 395{
395 struct device_node *np = p->pdev->dev.of_node; 396 struct device_node *np = p->pdev->dev.of_node;
396 const struct of_device_id *match;
397 const struct gpio_rcar_info *info; 397 const struct gpio_rcar_info *info;
398 struct of_phandle_args args; 398 struct of_phandle_args args;
399 int ret; 399 int ret;
400 400
401 match = of_match_node(gpio_rcar_of_table, np); 401 info = of_device_get_match_data(&p->pdev->dev);
402 if (!match)
403 return -EINVAL;
404
405 info = match->data;
406 402
407 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args); 403 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
408 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK; 404 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
@@ -456,19 +452,17 @@ static int gpio_rcar_probe(struct platform_device *pdev)
456 452
457 pm_runtime_enable(dev); 453 pm_runtime_enable(dev);
458 454
459 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 455 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
461 456 if (!irq) {
462 if (!io || !irq) { 457 dev_err(dev, "missing IRQ\n");
463 dev_err(dev, "missing IRQ or IOMEM\n");
464 ret = -EINVAL; 458 ret = -EINVAL;
465 goto err0; 459 goto err0;
466 } 460 }
467 461
468 p->base = devm_ioremap_nocache(dev, io->start, resource_size(io)); 462 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
469 if (!p->base) { 463 p->base = devm_ioremap_resource(dev, io);
470 dev_err(dev, "failed to remap I/O memory\n"); 464 if (IS_ERR(p->base)) {
471 ret = -ENXIO; 465 ret = PTR_ERR(p->base);
472 goto err0; 466 goto err0;
473 } 467 }
474 468
diff --git a/drivers/gpio/gpio-reg.c b/drivers/gpio/gpio-reg.c
index e85903eddc68..23e771dba4c1 100644
--- a/drivers/gpio/gpio-reg.c
+++ b/drivers/gpio/gpio-reg.c
@@ -103,8 +103,8 @@ static int gpio_reg_to_irq(struct gpio_chip *gc, unsigned offset)
103 struct gpio_reg *r = to_gpio_reg(gc); 103 struct gpio_reg *r = to_gpio_reg(gc);
104 int irq = r->irqs[offset]; 104 int irq = r->irqs[offset];
105 105
106 if (irq >= 0 && r->irqdomain) 106 if (irq >= 0 && r->irq.domain)
107 irq = irq_find_mapping(r->irqdomain, irq); 107 irq = irq_find_mapping(r->irq.domain, irq);
108 108
109 return irq; 109 return irq;
110} 110}
diff --git a/drivers/gpio/gpio-stmpe.c b/drivers/gpio/gpio-stmpe.c
index 16cbc5702865..e6e5cca624a7 100644
--- a/drivers/gpio/gpio-stmpe.c
+++ b/drivers/gpio/gpio-stmpe.c
@@ -299,7 +299,7 @@ static void stmpe_dbg_show_one(struct seq_file *s,
299 if (ret < 0) 299 if (ret < 0)
300 return; 300 return;
301 edge_det = !!(ret & mask); 301 edge_det = !!(ret & mask);
302 302 /* fall through */
303 case STMPE1801: 303 case STMPE1801:
304 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank]; 304 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
305 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank]; 305 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
@@ -312,7 +312,7 @@ static void stmpe_dbg_show_one(struct seq_file *s,
312 if (ret < 0) 312 if (ret < 0)
313 return; 313 return;
314 fall = !!(ret & mask); 314 fall = !!(ret & mask);
315 315 /* fall through */
316 case STMPE801: 316 case STMPE801:
317 case STMPE1600: 317 case STMPE1600:
318 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank]; 318 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
@@ -397,7 +397,7 @@ static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
397 while (stat) { 397 while (stat) {
398 int bit = __ffs(stat); 398 int bit = __ffs(stat);
399 int line = bank * 8 + bit; 399 int line = bank * 8 + bit;
400 int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain, 400 int child_irq = irq_find_mapping(stmpe_gpio->chip.irq.domain,
401 line); 401 line);
402 402
403 handle_nested_irq(child_irq); 403 handle_nested_irq(child_irq);
@@ -451,7 +451,7 @@ static int stmpe_gpio_probe(struct platform_device *pdev)
451 of_property_read_u32(np, "st,norequest-mask", 451 of_property_read_u32(np, "st,norequest-mask",
452 &stmpe_gpio->norequest_mask); 452 &stmpe_gpio->norequest_mask);
453 if (stmpe_gpio->norequest_mask) 453 if (stmpe_gpio->norequest_mask)
454 stmpe_gpio->chip.irq_need_valid_mask = true; 454 stmpe_gpio->chip.irq.need_valid_mask = true;
455 455
456 if (irq < 0) 456 if (irq < 0)
457 dev_info(&pdev->dev, 457 dev_info(&pdev->dev,
@@ -482,7 +482,7 @@ static int stmpe_gpio_probe(struct platform_device *pdev)
482 /* Forbid unused lines to be mapped as IRQs */ 482 /* Forbid unused lines to be mapped as IRQs */
483 for (i = 0; i < sizeof(u32); i++) 483 for (i = 0; i < sizeof(u32); i++)
484 if (stmpe_gpio->norequest_mask & BIT(i)) 484 if (stmpe_gpio->norequest_mask & BIT(i))
485 clear_bit(i, stmpe_gpio->chip.irq_valid_mask); 485 clear_bit(i, stmpe_gpio->chip.irq.valid_mask);
486 } 486 }
487 ret = gpiochip_irqchip_add_nested(&stmpe_gpio->chip, 487 ret = gpiochip_irqchip_add_nested(&stmpe_gpio->chip,
488 &stmpe_gpio_irq_chip, 488 &stmpe_gpio_irq_chip,
diff --git a/drivers/gpio/gpio-tb10x.c b/drivers/gpio/gpio-tb10x.c
index 091ffaaec635..ac6f2a9841e5 100644
--- a/drivers/gpio/gpio-tb10x.c
+++ b/drivers/gpio/gpio-tb10x.c
@@ -193,6 +193,9 @@ static int tb10x_gpio_probe(struct platform_device *pdev)
193 193
194 tb10x_gpio->gc.label = 194 tb10x_gpio->gc.label =
195 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOF", pdev->dev.of_node); 195 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOF", pdev->dev.of_node);
196 if (!tb10x_gpio->gc.label)
197 return -ENOMEM;
198
196 tb10x_gpio->gc.parent = &pdev->dev; 199 tb10x_gpio->gc.parent = &pdev->dev;
197 tb10x_gpio->gc.owner = THIS_MODULE; 200 tb10x_gpio->gc.owner = THIS_MODULE;
198 tb10x_gpio->gc.direction_input = tb10x_gpio_direction_in; 201 tb10x_gpio->gc.direction_input = tb10x_gpio_direction_in;
diff --git a/drivers/gpio/gpio-tc3589x.c b/drivers/gpio/gpio-tc3589x.c
index 433b45ef332e..91a8ef8e7f3f 100644
--- a/drivers/gpio/gpio-tc3589x.c
+++ b/drivers/gpio/gpio-tc3589x.c
@@ -268,7 +268,7 @@ static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
268 while (stat) { 268 while (stat) {
269 int bit = __ffs(stat); 269 int bit = __ffs(stat);
270 int line = i * 8 + bit; 270 int line = i * 8 + bit;
271 int irq = irq_find_mapping(tc3589x_gpio->chip.irqdomain, 271 int irq = irq_find_mapping(tc3589x_gpio->chip.irq.domain,
272 line); 272 line);
273 273
274 handle_nested_irq(irq); 274 handle_nested_irq(irq);
diff --git a/drivers/gpio/gpio-tegra186.c b/drivers/gpio/gpio-tegra186.c
new file mode 100644
index 000000000000..7f1aa4c21e0d
--- /dev/null
+++ b/drivers/gpio/gpio-tegra186.c
@@ -0,0 +1,620 @@
1/*
2 * Copyright (c) 2016-2017 NVIDIA Corporation
3 *
4 * Author: Thierry Reding <treding@nvidia.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 */
10
11#include <linux/gpio/driver.h>
12#include <linux/interrupt.h>
13#include <linux/irq.h>
14#include <linux/module.h>
15#include <linux/of_device.h>
16#include <linux/platform_device.h>
17
18#include <dt-bindings/gpio/tegra186-gpio.h>
19
20#define TEGRA186_GPIO_ENABLE_CONFIG 0x00
21#define TEGRA186_GPIO_ENABLE_CONFIG_ENABLE BIT(0)
22#define TEGRA186_GPIO_ENABLE_CONFIG_OUT BIT(1)
23#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_NONE (0x0 << 2)
24#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL (0x1 << 2)
25#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE (0x2 << 2)
26#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE (0x3 << 2)
27#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK (0x3 << 2)
28#define TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL BIT(4)
29#define TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT BIT(6)
30
31#define TEGRA186_GPIO_DEBOUNCE_CONTROL 0x04
32#define TEGRA186_GPIO_DEBOUNCE_CONTROL_THRESHOLD(x) ((x) & 0xff)
33
34#define TEGRA186_GPIO_INPUT 0x08
35#define TEGRA186_GPIO_INPUT_HIGH BIT(0)
36
37#define TEGRA186_GPIO_OUTPUT_CONTROL 0x0c
38#define TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED BIT(0)
39
40#define TEGRA186_GPIO_OUTPUT_VALUE 0x10
41#define TEGRA186_GPIO_OUTPUT_VALUE_HIGH BIT(0)
42
43#define TEGRA186_GPIO_INTERRUPT_CLEAR 0x14
44
45#define TEGRA186_GPIO_INTERRUPT_STATUS(x) (0x100 + (x) * 4)
46
47struct tegra_gpio_port {
48 const char *name;
49 unsigned int offset;
50 unsigned int pins;
51 unsigned int irq;
52};
53
54struct tegra_gpio_soc {
55 const struct tegra_gpio_port *ports;
56 unsigned int num_ports;
57 const char *name;
58};
59
60struct tegra_gpio {
61 struct gpio_chip gpio;
62 struct irq_chip intc;
63 unsigned int num_irq;
64 unsigned int *irq;
65
66 const struct tegra_gpio_soc *soc;
67
68 void __iomem *base;
69};
70
71static const struct tegra_gpio_port *
72tegra186_gpio_get_port(struct tegra_gpio *gpio, unsigned int *pin)
73{
74 unsigned int start = 0, i;
75
76 for (i = 0; i < gpio->soc->num_ports; i++) {
77 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
78
79 if (*pin >= start && *pin < start + port->pins) {
80 *pin -= start;
81 return port;
82 }
83
84 start += port->pins;
85 }
86
87 return NULL;
88}
89
90static void __iomem *tegra186_gpio_get_base(struct tegra_gpio *gpio,
91 unsigned int pin)
92{
93 const struct tegra_gpio_port *port;
94
95 port = tegra186_gpio_get_port(gpio, &pin);
96 if (!port)
97 return NULL;
98
99 return gpio->base + port->offset + pin * 0x20;
100}
101
102static int tegra186_gpio_get_direction(struct gpio_chip *chip,
103 unsigned int offset)
104{
105 struct tegra_gpio *gpio = gpiochip_get_data(chip);
106 void __iomem *base;
107 u32 value;
108
109 base = tegra186_gpio_get_base(gpio, offset);
110 if (WARN_ON(base == NULL))
111 return -ENODEV;
112
113 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
114 if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
115 return 0;
116
117 return 1;
118}
119
120static int tegra186_gpio_direction_input(struct gpio_chip *chip,
121 unsigned int offset)
122{
123 struct tegra_gpio *gpio = gpiochip_get_data(chip);
124 void __iomem *base;
125 u32 value;
126
127 base = tegra186_gpio_get_base(gpio, offset);
128 if (WARN_ON(base == NULL))
129 return -ENODEV;
130
131 value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
132 value |= TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
133 writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
134
135 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
136 value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
137 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_OUT;
138 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
139
140 return 0;
141}
142
143static int tegra186_gpio_direction_output(struct gpio_chip *chip,
144 unsigned int offset, int level)
145{
146 struct tegra_gpio *gpio = gpiochip_get_data(chip);
147 void __iomem *base;
148 u32 value;
149
150 /* configure output level first */
151 chip->set(chip, offset, level);
152
153 base = tegra186_gpio_get_base(gpio, offset);
154 if (WARN_ON(base == NULL))
155 return -EINVAL;
156
157 /* set the direction */
158 value = readl(base + TEGRA186_GPIO_OUTPUT_CONTROL);
159 value &= ~TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED;
160 writel(value, base + TEGRA186_GPIO_OUTPUT_CONTROL);
161
162 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
163 value |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE;
164 value |= TEGRA186_GPIO_ENABLE_CONFIG_OUT;
165 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
166
167 return 0;
168}
169
170static int tegra186_gpio_get(struct gpio_chip *chip, unsigned int offset)
171{
172 struct tegra_gpio *gpio = gpiochip_get_data(chip);
173 void __iomem *base;
174 u32 value;
175
176 base = tegra186_gpio_get_base(gpio, offset);
177 if (WARN_ON(base == NULL))
178 return -ENODEV;
179
180 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
181 if (value & TEGRA186_GPIO_ENABLE_CONFIG_OUT)
182 value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
183 else
184 value = readl(base + TEGRA186_GPIO_INPUT);
185
186 return value & BIT(0);
187}
188
189static void tegra186_gpio_set(struct gpio_chip *chip, unsigned int offset,
190 int level)
191{
192 struct tegra_gpio *gpio = gpiochip_get_data(chip);
193 void __iomem *base;
194 u32 value;
195
196 base = tegra186_gpio_get_base(gpio, offset);
197 if (WARN_ON(base == NULL))
198 return;
199
200 value = readl(base + TEGRA186_GPIO_OUTPUT_VALUE);
201 if (level == 0)
202 value &= ~TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
203 else
204 value |= TEGRA186_GPIO_OUTPUT_VALUE_HIGH;
205
206 writel(value, base + TEGRA186_GPIO_OUTPUT_VALUE);
207}
208
209static int tegra186_gpio_of_xlate(struct gpio_chip *chip,
210 const struct of_phandle_args *spec,
211 u32 *flags)
212{
213 struct tegra_gpio *gpio = gpiochip_get_data(chip);
214 unsigned int port, pin, i, offset = 0;
215
216 if (WARN_ON(chip->of_gpio_n_cells < 2))
217 return -EINVAL;
218
219 if (WARN_ON(spec->args_count < chip->of_gpio_n_cells))
220 return -EINVAL;
221
222 port = spec->args[0] / 8;
223 pin = spec->args[0] % 8;
224
225 if (port >= gpio->soc->num_ports) {
226 dev_err(chip->parent, "invalid port number: %u\n", port);
227 return -EINVAL;
228 }
229
230 for (i = 0; i < port; i++)
231 offset += gpio->soc->ports[i].pins;
232
233 if (flags)
234 *flags = spec->args[1];
235
236 return offset + pin;
237}
238
239static void tegra186_irq_ack(struct irq_data *data)
240{
241 struct tegra_gpio *gpio = irq_data_get_irq_chip_data(data);
242 void __iomem *base;
243
244 base = tegra186_gpio_get_base(gpio, data->hwirq);
245 if (WARN_ON(base == NULL))
246 return;
247
248 writel(1, base + TEGRA186_GPIO_INTERRUPT_CLEAR);
249}
250
251static void tegra186_irq_mask(struct irq_data *data)
252{
253 struct tegra_gpio *gpio = irq_data_get_irq_chip_data(data);
254 void __iomem *base;
255 u32 value;
256
257 base = tegra186_gpio_get_base(gpio, data->hwirq);
258 if (WARN_ON(base == NULL))
259 return;
260
261 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
262 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
263 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
264}
265
266static void tegra186_irq_unmask(struct irq_data *data)
267{
268 struct tegra_gpio *gpio = irq_data_get_irq_chip_data(data);
269 void __iomem *base;
270 u32 value;
271
272 base = tegra186_gpio_get_base(gpio, data->hwirq);
273 if (WARN_ON(base == NULL))
274 return;
275
276 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
277 value |= TEGRA186_GPIO_ENABLE_CONFIG_INTERRUPT;
278 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
279}
280
281static int tegra186_irq_set_type(struct irq_data *data, unsigned int flow)
282{
283 struct tegra_gpio *gpio = irq_data_get_irq_chip_data(data);
284 void __iomem *base;
285 u32 value;
286
287 base = tegra186_gpio_get_base(gpio, data->hwirq);
288 if (WARN_ON(base == NULL))
289 return -ENODEV;
290
291 value = readl(base + TEGRA186_GPIO_ENABLE_CONFIG);
292 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_MASK;
293 value &= ~TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
294
295 switch (flow & IRQ_TYPE_SENSE_MASK) {
296 case IRQ_TYPE_NONE:
297 break;
298
299 case IRQ_TYPE_EDGE_RISING:
300 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
301 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
302 break;
303
304 case IRQ_TYPE_EDGE_FALLING:
305 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_SINGLE_EDGE;
306 break;
307
308 case IRQ_TYPE_EDGE_BOTH:
309 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_DOUBLE_EDGE;
310 break;
311
312 case IRQ_TYPE_LEVEL_HIGH:
313 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
314 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_LEVEL;
315 break;
316
317 case IRQ_TYPE_LEVEL_LOW:
318 value |= TEGRA186_GPIO_ENABLE_CONFIG_TRIGGER_TYPE_LEVEL;
319 break;
320
321 default:
322 return -EINVAL;
323 }
324
325 writel(value, base + TEGRA186_GPIO_ENABLE_CONFIG);
326
327 if ((flow & IRQ_TYPE_EDGE_BOTH) == 0)
328 irq_set_handler_locked(data, handle_level_irq);
329 else
330 irq_set_handler_locked(data, handle_edge_irq);
331
332 return 0;
333}
334
335static void tegra186_gpio_irq(struct irq_desc *desc)
336{
337 struct tegra_gpio *gpio = irq_desc_get_handler_data(desc);
338 struct irq_domain *domain = gpio->gpio.irq.domain;
339 struct irq_chip *chip = irq_desc_get_chip(desc);
340 unsigned int parent = irq_desc_get_irq(desc);
341 unsigned int i, offset = 0;
342
343 chained_irq_enter(chip, desc);
344
345 for (i = 0; i < gpio->soc->num_ports; i++) {
346 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
347 void __iomem *base = gpio->base + port->offset;
348 unsigned int pin, irq;
349 unsigned long value;
350
351 /* skip ports that are not associated with this controller */
352 if (parent != gpio->irq[port->irq])
353 goto skip;
354
355 value = readl(base + TEGRA186_GPIO_INTERRUPT_STATUS(1));
356
357 for_each_set_bit(pin, &value, port->pins) {
358 irq = irq_find_mapping(domain, offset + pin);
359 if (WARN_ON(irq == 0))
360 continue;
361
362 generic_handle_irq(irq);
363 }
364
365skip:
366 offset += port->pins;
367 }
368
369 chained_irq_exit(chip, desc);
370}
371
372static int tegra186_gpio_irq_domain_xlate(struct irq_domain *domain,
373 struct device_node *np,
374 const u32 *spec, unsigned int size,
375 unsigned long *hwirq,
376 unsigned int *type)
377{
378 struct tegra_gpio *gpio = gpiochip_get_data(domain->host_data);
379 unsigned int port, pin, i, offset = 0;
380
381 if (size < 2)
382 return -EINVAL;
383
384 port = spec[0] / 8;
385 pin = spec[0] % 8;
386
387 if (port >= gpio->soc->num_ports) {
388 dev_err(gpio->gpio.parent, "invalid port number: %u\n", port);
389 return -EINVAL;
390 }
391
392 for (i = 0; i < port; i++)
393 offset += gpio->soc->ports[i].pins;
394
395 *type = spec[1] & IRQ_TYPE_SENSE_MASK;
396 *hwirq = offset + pin;
397
398 return 0;
399}
400
401static const struct irq_domain_ops tegra186_gpio_irq_domain_ops = {
402 .map = gpiochip_irq_map,
403 .unmap = gpiochip_irq_unmap,
404 .xlate = tegra186_gpio_irq_domain_xlate,
405};
406
407static int tegra186_gpio_probe(struct platform_device *pdev)
408{
409 unsigned int i, j, offset;
410 struct gpio_irq_chip *irq;
411 struct tegra_gpio *gpio;
412 struct resource *res;
413 char **names;
414 int err;
415
416 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
417 if (!gpio)
418 return -ENOMEM;
419
420 gpio->soc = of_device_get_match_data(&pdev->dev);
421
422 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gpio");
423 gpio->base = devm_ioremap_resource(&pdev->dev, res);
424 if (IS_ERR(gpio->base))
425 return PTR_ERR(gpio->base);
426
427 err = platform_irq_count(pdev);
428 if (err < 0)
429 return err;
430
431 gpio->num_irq = err;
432
433 gpio->irq = devm_kcalloc(&pdev->dev, gpio->num_irq, sizeof(*gpio->irq),
434 GFP_KERNEL);
435 if (!gpio->irq)
436 return -ENOMEM;
437
438 for (i = 0; i < gpio->num_irq; i++) {
439 err = platform_get_irq(pdev, i);
440 if (err < 0)
441 return err;
442
443 gpio->irq[i] = err;
444 }
445
446 gpio->gpio.label = gpio->soc->name;
447 gpio->gpio.parent = &pdev->dev;
448
449 gpio->gpio.get_direction = tegra186_gpio_get_direction;
450 gpio->gpio.direction_input = tegra186_gpio_direction_input;
451 gpio->gpio.direction_output = tegra186_gpio_direction_output;
452 gpio->gpio.get = tegra186_gpio_get,
453 gpio->gpio.set = tegra186_gpio_set;
454
455 gpio->gpio.base = -1;
456
457 for (i = 0; i < gpio->soc->num_ports; i++)
458 gpio->gpio.ngpio += gpio->soc->ports[i].pins;
459
460 names = devm_kcalloc(gpio->gpio.parent, gpio->gpio.ngpio,
461 sizeof(*names), GFP_KERNEL);
462 if (!names)
463 return -ENOMEM;
464
465 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
466 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
467 char *name;
468
469 for (j = 0; j < port->pins; j++) {
470 name = devm_kasprintf(gpio->gpio.parent, GFP_KERNEL,
471 "P%s.%02x", port->name, j);
472 if (!name)
473 return -ENOMEM;
474
475 names[offset + j] = name;
476 }
477
478 offset += port->pins;
479 }
480
481 gpio->gpio.names = (const char * const *)names;
482
483 gpio->gpio.of_node = pdev->dev.of_node;
484 gpio->gpio.of_gpio_n_cells = 2;
485 gpio->gpio.of_xlate = tegra186_gpio_of_xlate;
486
487 gpio->intc.name = pdev->dev.of_node->name;
488 gpio->intc.irq_ack = tegra186_irq_ack;
489 gpio->intc.irq_mask = tegra186_irq_mask;
490 gpio->intc.irq_unmask = tegra186_irq_unmask;
491 gpio->intc.irq_set_type = tegra186_irq_set_type;
492
493 irq = &gpio->gpio.irq;
494 irq->chip = &gpio->intc;
495 irq->domain_ops = &tegra186_gpio_irq_domain_ops;
496 irq->handler = handle_simple_irq;
497 irq->default_type = IRQ_TYPE_NONE;
498 irq->parent_handler = tegra186_gpio_irq;
499 irq->parent_handler_data = gpio;
500 irq->num_parents = gpio->num_irq;
501 irq->parents = gpio->irq;
502
503 irq->map = devm_kcalloc(&pdev->dev, gpio->gpio.ngpio,
504 sizeof(*irq->map), GFP_KERNEL);
505 if (!irq->map)
506 return -ENOMEM;
507
508 for (i = 0, offset = 0; i < gpio->soc->num_ports; i++) {
509 const struct tegra_gpio_port *port = &gpio->soc->ports[i];
510
511 for (j = 0; j < port->pins; j++)
512 irq->map[offset + j] = irq->parents[port->irq];
513
514 offset += port->pins;
515 }
516
517 platform_set_drvdata(pdev, gpio);
518
519 err = devm_gpiochip_add_data(&pdev->dev, &gpio->gpio, gpio);
520 if (err < 0)
521 return err;
522
523 return 0;
524}
525
526static int tegra186_gpio_remove(struct platform_device *pdev)
527{
528 return 0;
529}
530
531#define TEGRA_MAIN_GPIO_PORT(port, base, count, controller) \
532 [TEGRA_MAIN_GPIO_PORT_##port] = { \
533 .name = #port, \
534 .offset = base, \
535 .pins = count, \
536 .irq = controller, \
537 }
538
539static const struct tegra_gpio_port tegra186_main_ports[] = {
540 TEGRA_MAIN_GPIO_PORT( A, 0x2000, 7, 2),
541 TEGRA_MAIN_GPIO_PORT( B, 0x3000, 7, 3),
542 TEGRA_MAIN_GPIO_PORT( C, 0x3200, 7, 3),
543 TEGRA_MAIN_GPIO_PORT( D, 0x3400, 6, 3),
544 TEGRA_MAIN_GPIO_PORT( E, 0x2200, 8, 2),
545 TEGRA_MAIN_GPIO_PORT( F, 0x2400, 6, 2),
546 TEGRA_MAIN_GPIO_PORT( G, 0x4200, 6, 4),
547 TEGRA_MAIN_GPIO_PORT( H, 0x1000, 7, 1),
548 TEGRA_MAIN_GPIO_PORT( I, 0x0800, 8, 0),
549 TEGRA_MAIN_GPIO_PORT( J, 0x5000, 8, 5),
550 TEGRA_MAIN_GPIO_PORT( K, 0x5200, 1, 5),
551 TEGRA_MAIN_GPIO_PORT( L, 0x1200, 8, 1),
552 TEGRA_MAIN_GPIO_PORT( M, 0x5600, 6, 5),
553 TEGRA_MAIN_GPIO_PORT( N, 0x0000, 7, 0),
554 TEGRA_MAIN_GPIO_PORT( O, 0x0200, 4, 0),
555 TEGRA_MAIN_GPIO_PORT( P, 0x4000, 7, 4),
556 TEGRA_MAIN_GPIO_PORT( Q, 0x0400, 6, 0),
557 TEGRA_MAIN_GPIO_PORT( R, 0x0a00, 6, 0),
558 TEGRA_MAIN_GPIO_PORT( T, 0x0600, 4, 0),
559 TEGRA_MAIN_GPIO_PORT( X, 0x1400, 8, 1),
560 TEGRA_MAIN_GPIO_PORT( Y, 0x1600, 7, 1),
561 TEGRA_MAIN_GPIO_PORT(BB, 0x2600, 2, 2),
562 TEGRA_MAIN_GPIO_PORT(CC, 0x5400, 4, 5),
563};
564
565static const struct tegra_gpio_soc tegra186_main_soc = {
566 .num_ports = ARRAY_SIZE(tegra186_main_ports),
567 .ports = tegra186_main_ports,
568 .name = "tegra186-gpio",
569};
570
571#define TEGRA_AON_GPIO_PORT(port, base, count, controller) \
572 [TEGRA_AON_GPIO_PORT_##port] = { \
573 .name = #port, \
574 .offset = base, \
575 .pins = count, \
576 .irq = controller, \
577 }
578
579static const struct tegra_gpio_port tegra186_aon_ports[] = {
580 TEGRA_AON_GPIO_PORT( S, 0x0200, 5, 0),
581 TEGRA_AON_GPIO_PORT( U, 0x0400, 6, 0),
582 TEGRA_AON_GPIO_PORT( V, 0x0800, 8, 0),
583 TEGRA_AON_GPIO_PORT( W, 0x0a00, 8, 0),
584 TEGRA_AON_GPIO_PORT( Z, 0x0e00, 4, 0),
585 TEGRA_AON_GPIO_PORT(AA, 0x0c00, 8, 0),
586 TEGRA_AON_GPIO_PORT(EE, 0x0600, 3, 0),
587 TEGRA_AON_GPIO_PORT(FF, 0x0000, 5, 0),
588};
589
590static const struct tegra_gpio_soc tegra186_aon_soc = {
591 .num_ports = ARRAY_SIZE(tegra186_aon_ports),
592 .ports = tegra186_aon_ports,
593 .name = "tegra186-gpio-aon",
594};
595
596static const struct of_device_id tegra186_gpio_of_match[] = {
597 {
598 .compatible = "nvidia,tegra186-gpio",
599 .data = &tegra186_main_soc
600 }, {
601 .compatible = "nvidia,tegra186-gpio-aon",
602 .data = &tegra186_aon_soc
603 }, {
604 /* sentinel */
605 }
606};
607
608static struct platform_driver tegra186_gpio_driver = {
609 .driver = {
610 .name = "tegra186-gpio",
611 .of_match_table = tegra186_gpio_of_match,
612 },
613 .probe = tegra186_gpio_probe,
614 .remove = tegra186_gpio_remove,
615};
616module_platform_driver(tegra186_gpio_driver);
617
618MODULE_DESCRIPTION("NVIDIA Tegra186 GPIO controller driver");
619MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
620MODULE_LICENSE("GPL v2");
diff --git a/drivers/gpio/gpio-thunderx.c b/drivers/gpio/gpio-thunderx.c
index 57efb251f9c4..b5adb79a631a 100644
--- a/drivers/gpio/gpio-thunderx.c
+++ b/drivers/gpio/gpio-thunderx.c
@@ -417,18 +417,6 @@ static struct irq_chip thunderx_gpio_irq_chip = {
417 .flags = IRQCHIP_SET_TYPE_MASKED 417 .flags = IRQCHIP_SET_TYPE_MASKED
418}; 418};
419 419
420static int thunderx_gpio_irq_map(struct irq_domain *d, unsigned int irq,
421 irq_hw_number_t hwirq)
422{
423 struct thunderx_gpio *txgpio = d->host_data;
424
425 if (hwirq >= txgpio->chip.ngpio)
426 return -EINVAL;
427 if (!thunderx_gpio_is_gpio_nowarn(txgpio, hwirq))
428 return -EPERM;
429 return 0;
430}
431
432static int thunderx_gpio_irq_translate(struct irq_domain *d, 420static int thunderx_gpio_irq_translate(struct irq_domain *d,
433 struct irq_fwspec *fwspec, 421 struct irq_fwspec *fwspec,
434 irq_hw_number_t *hwirq, 422 irq_hw_number_t *hwirq,
@@ -455,7 +443,6 @@ static int thunderx_gpio_irq_alloc(struct irq_domain *d, unsigned int virq,
455} 443}
456 444
457static const struct irq_domain_ops thunderx_gpio_irqd_ops = { 445static const struct irq_domain_ops thunderx_gpio_irqd_ops = {
458 .map = thunderx_gpio_irq_map,
459 .alloc = thunderx_gpio_irq_alloc, 446 .alloc = thunderx_gpio_irq_alloc,
460 .translate = thunderx_gpio_irq_translate 447 .translate = thunderx_gpio_irq_translate
461}; 448};
diff --git a/drivers/gpio/gpio-uniphier.c b/drivers/gpio/gpio-uniphier.c
new file mode 100644
index 000000000000..016d7427ebfa
--- /dev/null
+++ b/drivers/gpio/gpio-uniphier.c
@@ -0,0 +1,508 @@
1/*
2 * Copyright (C) 2017 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/bitops.h>
16#include <linux/gpio/driver.h>
17#include <linux/irq.h>
18#include <linux/irqdomain.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/of_irq.h>
23#include <linux/platform_device.h>
24#include <linux/spinlock.h>
25#include <dt-bindings/gpio/uniphier-gpio.h>
26
27#define UNIPHIER_GPIO_BANK_MASK \
28 GENMASK((UNIPHIER_GPIO_LINES_PER_BANK) - 1, 0)
29
30#define UNIPHIER_GPIO_IRQ_MAX_NUM 24
31
32#define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
33#define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
34#define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
35#define UNIPHIER_GPIO_IRQ_MODE 0x94 /* irq mode (1: both edge) */
36#define UNIPHIER_GPIO_IRQ_FLT_EN 0x98 /* noise filter enable */
37#define UNIPHIER_GPIO_IRQ_FLT_CYC 0x9c /* noise filter clock cycle */
38
39struct uniphier_gpio_priv {
40 struct gpio_chip chip;
41 struct irq_chip irq_chip;
42 struct irq_domain *domain;
43 void __iomem *regs;
44 spinlock_t lock;
45 u32 saved_vals[0];
46};
47
48static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
49{
50 unsigned int reg;
51
52 reg = (bank + 1) * 8;
53
54 /*
55 * Unfortunately, the GPIO port registers are not contiguous because
56 * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
57 */
58 if (reg >= UNIPHIER_GPIO_IRQ_EN)
59 reg += 0x10;
60
61 return reg;
62}
63
64static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
65 unsigned int *bank, u32 *mask)
66{
67 *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
68 *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
69}
70
71static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
72 unsigned int reg, u32 mask, u32 val)
73{
74 unsigned long flags;
75 u32 tmp;
76
77 spin_lock_irqsave(&priv->lock, flags);
78 tmp = readl(priv->regs + reg);
79 tmp &= ~mask;
80 tmp |= mask & val;
81 writel(tmp, priv->regs + reg);
82 spin_unlock_irqrestore(&priv->lock, flags);
83}
84
85static void uniphier_gpio_bank_write(struct gpio_chip *chip, unsigned int bank,
86 unsigned int reg, u32 mask, u32 val)
87{
88 struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
89
90 if (!mask)
91 return;
92
93 uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
94 mask, val);
95}
96
97static void uniphier_gpio_offset_write(struct gpio_chip *chip,
98 unsigned int offset, unsigned int reg,
99 int val)
100{
101 unsigned int bank;
102 u32 mask;
103
104 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
105
106 uniphier_gpio_bank_write(chip, bank, reg, mask, val ? mask : 0);
107}
108
109static int uniphier_gpio_offset_read(struct gpio_chip *chip,
110 unsigned int offset, unsigned int reg)
111{
112 struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
113 unsigned int bank, reg_offset;
114 u32 mask;
115
116 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
117 reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
118
119 return !!(readl(priv->regs + reg_offset) & mask);
120}
121
122static int uniphier_gpio_get_direction(struct gpio_chip *chip,
123 unsigned int offset)
124{
125 return uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DIR);
126}
127
128static int uniphier_gpio_direction_input(struct gpio_chip *chip,
129 unsigned int offset)
130{
131 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 1);
132
133 return 0;
134}
135
136static int uniphier_gpio_direction_output(struct gpio_chip *chip,
137 unsigned int offset, int val)
138{
139 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
140 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 0);
141
142 return 0;
143}
144
145static int uniphier_gpio_get(struct gpio_chip *chip, unsigned int offset)
146{
147 return uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DATA);
148}
149
150static void uniphier_gpio_set(struct gpio_chip *chip,
151 unsigned int offset, int val)
152{
153 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
154}
155
156static void uniphier_gpio_set_multiple(struct gpio_chip *chip,
157 unsigned long *mask, unsigned long *bits)
158{
159 unsigned int bank, shift, bank_mask, bank_bits;
160 int i;
161
162 for (i = 0; i < chip->ngpio; i += UNIPHIER_GPIO_LINES_PER_BANK) {
163 bank = i / UNIPHIER_GPIO_LINES_PER_BANK;
164 shift = i % BITS_PER_LONG;
165 bank_mask = (mask[BIT_WORD(i)] >> shift) &
166 UNIPHIER_GPIO_BANK_MASK;
167 bank_bits = bits[BIT_WORD(i)] >> shift;
168
169 uniphier_gpio_bank_write(chip, bank, UNIPHIER_GPIO_PORT_DATA,
170 bank_mask, bank_bits);
171 }
172}
173
174static int uniphier_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
175{
176 struct irq_fwspec fwspec;
177
178 if (offset < UNIPHIER_GPIO_IRQ_OFFSET)
179 return -ENXIO;
180
181 fwspec.fwnode = of_node_to_fwnode(chip->parent->of_node);
182 fwspec.param_count = 2;
183 fwspec.param[0] = offset - UNIPHIER_GPIO_IRQ_OFFSET;
184 fwspec.param[1] = IRQ_TYPE_NONE;
185
186 return irq_create_fwspec_mapping(&fwspec);
187}
188
189static void uniphier_gpio_irq_mask(struct irq_data *data)
190{
191 struct uniphier_gpio_priv *priv = data->chip_data;
192 u32 mask = BIT(data->hwirq);
193
194 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, 0);
195
196 return irq_chip_mask_parent(data);
197}
198
199static void uniphier_gpio_irq_unmask(struct irq_data *data)
200{
201 struct uniphier_gpio_priv *priv = data->chip_data;
202 u32 mask = BIT(data->hwirq);
203
204 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, mask);
205
206 return irq_chip_unmask_parent(data);
207}
208
209static int uniphier_gpio_irq_set_type(struct irq_data *data, unsigned int type)
210{
211 struct uniphier_gpio_priv *priv = data->chip_data;
212 u32 mask = BIT(data->hwirq);
213 u32 val = 0;
214
215 if (type == IRQ_TYPE_EDGE_BOTH) {
216 val = mask;
217 type = IRQ_TYPE_EDGE_FALLING;
218 }
219
220 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_MODE, mask, val);
221 /* To enable both edge detection, the noise filter must be enabled. */
222 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_FLT_EN, mask, val);
223
224 return irq_chip_set_type_parent(data, type);
225}
226
227static int uniphier_gpio_irq_get_parent_hwirq(struct uniphier_gpio_priv *priv,
228 unsigned int hwirq)
229{
230 struct device_node *np = priv->chip.parent->of_node;
231 const __be32 *range;
232 u32 base, parent_base, size;
233 int len;
234
235 range = of_get_property(np, "socionext,interrupt-ranges", &len);
236 if (!range)
237 return -EINVAL;
238
239 len /= sizeof(*range);
240
241 for (; len >= 3; len -= 3) {
242 base = be32_to_cpu(*range++);
243 parent_base = be32_to_cpu(*range++);
244 size = be32_to_cpu(*range++);
245
246 if (base <= hwirq && hwirq < base + size)
247 return hwirq - base + parent_base;
248 }
249
250 return -ENOENT;
251}
252
253static int uniphier_gpio_irq_domain_translate(struct irq_domain *domain,
254 struct irq_fwspec *fwspec,
255 unsigned long *out_hwirq,
256 unsigned int *out_type)
257{
258 if (WARN_ON(fwspec->param_count < 2))
259 return -EINVAL;
260
261 *out_hwirq = fwspec->param[0];
262 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
263
264 return 0;
265}
266
267static int uniphier_gpio_irq_domain_alloc(struct irq_domain *domain,
268 unsigned int virq,
269 unsigned int nr_irqs, void *arg)
270{
271 struct uniphier_gpio_priv *priv = domain->host_data;
272 struct irq_fwspec parent_fwspec;
273 irq_hw_number_t hwirq;
274 unsigned int type;
275 int ret;
276
277 if (WARN_ON(nr_irqs != 1))
278 return -EINVAL;
279
280 ret = uniphier_gpio_irq_domain_translate(domain, arg, &hwirq, &type);
281 if (ret)
282 return ret;
283
284 ret = uniphier_gpio_irq_get_parent_hwirq(priv, hwirq);
285 if (ret < 0)
286 return ret;
287
288 /* parent is UniPhier AIDET */
289 parent_fwspec.fwnode = domain->parent->fwnode;
290 parent_fwspec.param_count = 2;
291 parent_fwspec.param[0] = ret;
292 parent_fwspec.param[1] = (type == IRQ_TYPE_EDGE_BOTH) ?
293 IRQ_TYPE_EDGE_FALLING : type;
294
295 ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
296 &priv->irq_chip, priv);
297 if (ret)
298 return ret;
299
300 return irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
301}
302
303static int uniphier_gpio_irq_domain_activate(struct irq_domain *domain,
304 struct irq_data *data, bool early)
305{
306 struct uniphier_gpio_priv *priv = domain->host_data;
307 struct gpio_chip *chip = &priv->chip;
308
309 gpiochip_lock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
310 return 0;
311}
312
313static void uniphier_gpio_irq_domain_deactivate(struct irq_domain *domain,
314 struct irq_data *data)
315{
316 struct uniphier_gpio_priv *priv = domain->host_data;
317 struct gpio_chip *chip = &priv->chip;
318
319 gpiochip_unlock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
320}
321
322static const struct irq_domain_ops uniphier_gpio_irq_domain_ops = {
323 .alloc = uniphier_gpio_irq_domain_alloc,
324 .free = irq_domain_free_irqs_common,
325 .activate = uniphier_gpio_irq_domain_activate,
326 .deactivate = uniphier_gpio_irq_domain_deactivate,
327 .translate = uniphier_gpio_irq_domain_translate,
328};
329
330static void uniphier_gpio_hw_init(struct uniphier_gpio_priv *priv)
331{
332 /*
333 * Due to the hardware design, the noise filter must be enabled to
334 * detect both edge interrupts. This filter is intended to remove the
335 * noise from the irq lines. It does not work for GPIO input, so GPIO
336 * debounce is not supported. Unfortunately, the filter period is
337 * shared among all irq lines. Just choose a sensible period here.
338 */
339 writel(0xff, priv->regs + UNIPHIER_GPIO_IRQ_FLT_CYC);
340}
341
342static unsigned int uniphier_gpio_get_nbanks(unsigned int ngpio)
343{
344 return DIV_ROUND_UP(ngpio, UNIPHIER_GPIO_LINES_PER_BANK);
345}
346
347static int uniphier_gpio_probe(struct platform_device *pdev)
348{
349 struct device *dev = &pdev->dev;
350 struct device_node *parent_np;
351 struct irq_domain *parent_domain;
352 struct uniphier_gpio_priv *priv;
353 struct gpio_chip *chip;
354 struct irq_chip *irq_chip;
355 struct resource *regs;
356 unsigned int nregs;
357 u32 ngpios;
358 int ret;
359
360 parent_np = of_irq_find_parent(dev->of_node);
361 if (!parent_np)
362 return -ENXIO;
363
364 parent_domain = irq_find_host(parent_np);
365 of_node_put(parent_np);
366 if (!parent_domain)
367 return -EPROBE_DEFER;
368
369 ret = of_property_read_u32(dev->of_node, "ngpios", &ngpios);
370 if (ret)
371 return ret;
372
373 nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
374 priv = devm_kzalloc(dev,
375 sizeof(*priv) + sizeof(priv->saved_vals[0]) * nregs,
376 GFP_KERNEL);
377 if (!priv)
378 return -ENOMEM;
379
380 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
381 priv->regs = devm_ioremap_resource(dev, regs);
382 if (IS_ERR(priv->regs))
383 return PTR_ERR(priv->regs);
384
385 spin_lock_init(&priv->lock);
386
387 chip = &priv->chip;
388 chip->label = dev_name(dev);
389 chip->parent = dev;
390 chip->request = gpiochip_generic_request;
391 chip->free = gpiochip_generic_free;
392 chip->get_direction = uniphier_gpio_get_direction;
393 chip->direction_input = uniphier_gpio_direction_input;
394 chip->direction_output = uniphier_gpio_direction_output;
395 chip->get = uniphier_gpio_get;
396 chip->set = uniphier_gpio_set;
397 chip->set_multiple = uniphier_gpio_set_multiple;
398 chip->to_irq = uniphier_gpio_to_irq;
399 chip->base = -1;
400 chip->ngpio = ngpios;
401
402 irq_chip = &priv->irq_chip;
403 irq_chip->name = dev_name(dev);
404 irq_chip->irq_mask = uniphier_gpio_irq_mask;
405 irq_chip->irq_unmask = uniphier_gpio_irq_unmask;
406 irq_chip->irq_eoi = irq_chip_eoi_parent;
407 irq_chip->irq_set_affinity = irq_chip_set_affinity_parent;
408 irq_chip->irq_set_type = uniphier_gpio_irq_set_type;
409
410 uniphier_gpio_hw_init(priv);
411
412 ret = devm_gpiochip_add_data(dev, chip, priv);
413 if (ret)
414 return ret;
415
416 priv->domain = irq_domain_create_hierarchy(
417 parent_domain, 0,
418 UNIPHIER_GPIO_IRQ_MAX_NUM,
419 of_node_to_fwnode(dev->of_node),
420 &uniphier_gpio_irq_domain_ops, priv);
421 if (!priv->domain)
422 return -ENOMEM;
423
424 platform_set_drvdata(pdev, priv);
425
426 return 0;
427}
428
429static int uniphier_gpio_remove(struct platform_device *pdev)
430{
431 struct uniphier_gpio_priv *priv = platform_get_drvdata(pdev);
432
433 irq_domain_remove(priv->domain);
434
435 return 0;
436}
437
438static int __maybe_unused uniphier_gpio_suspend(struct device *dev)
439{
440 struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
441 unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
442 u32 *val = priv->saved_vals;
443 unsigned int reg;
444 int i;
445
446 for (i = 0; i < nbanks; i++) {
447 reg = uniphier_gpio_bank_to_reg(i);
448
449 *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
450 *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
451 }
452
453 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_EN);
454 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_MODE);
455 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
456
457 return 0;
458}
459
460static int __maybe_unused uniphier_gpio_resume(struct device *dev)
461{
462 struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
463 unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
464 const u32 *val = priv->saved_vals;
465 unsigned int reg;
466 int i;
467
468 for (i = 0; i < nbanks; i++) {
469 reg = uniphier_gpio_bank_to_reg(i);
470
471 writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
472 writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
473 }
474
475 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_EN);
476 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_MODE);
477 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
478
479 uniphier_gpio_hw_init(priv);
480
481 return 0;
482}
483
484static const struct dev_pm_ops uniphier_gpio_pm_ops = {
485 SET_LATE_SYSTEM_SLEEP_PM_OPS(uniphier_gpio_suspend,
486 uniphier_gpio_resume)
487};
488
489static const struct of_device_id uniphier_gpio_match[] = {
490 { .compatible = "socionext,uniphier-gpio" },
491 { /* sentinel */ }
492};
493MODULE_DEVICE_TABLE(of, uniphier_gpio_match);
494
495static struct platform_driver uniphier_gpio_driver = {
496 .probe = uniphier_gpio_probe,
497 .remove = uniphier_gpio_remove,
498 .driver = {
499 .name = "uniphier-gpio",
500 .of_match_table = uniphier_gpio_match,
501 .pm = &uniphier_gpio_pm_ops,
502 },
503};
504module_platform_driver(uniphier_gpio_driver);
505
506MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
507MODULE_DESCRIPTION("UniPhier GPIO driver");
508MODULE_LICENSE("GPL");
diff --git a/drivers/gpio/gpio-vf610.c b/drivers/gpio/gpio-vf610.c
index cbe9e06861de..4610cc2938ad 100644
--- a/drivers/gpio/gpio-vf610.c
+++ b/drivers/gpio/gpio-vf610.c
@@ -160,7 +160,7 @@ static void vf610_gpio_irq_handler(struct irq_desc *desc)
160 for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) { 160 for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
161 vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR); 161 vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
162 162
163 generic_handle_irq(irq_find_mapping(port->gc.irqdomain, pin)); 163 generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin));
164 } 164 }
165 165
166 chained_irq_exit(chip, desc); 166 chained_irq_exit(chip, desc);
diff --git a/drivers/gpio/gpio-wcove.c b/drivers/gpio/gpio-wcove.c
index 85341eab795d..dde7c6aecbb5 100644
--- a/drivers/gpio/gpio-wcove.c
+++ b/drivers/gpio/gpio-wcove.c
@@ -350,7 +350,7 @@ static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
350 offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0; 350 offset = (gpio > GROUP0_NR_IRQS) ? 1 : 0;
351 mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) : 351 mask = (offset == 1) ? BIT(gpio - GROUP0_NR_IRQS) :
352 BIT(gpio); 352 BIT(gpio);
353 virq = irq_find_mapping(wg->chip.irqdomain, gpio); 353 virq = irq_find_mapping(wg->chip.irq.domain, gpio);
354 handle_nested_irq(virq); 354 handle_nested_irq(virq);
355 regmap_update_bits(wg->regmap, IRQ_STATUS_BASE + offset, 355 regmap_update_bits(wg->regmap, IRQ_STATUS_BASE + offset,
356 mask, mask); 356 mask, mask);
diff --git a/drivers/gpio/gpio-ws16c48.c b/drivers/gpio/gpio-ws16c48.c
index 5037974ac063..746648244bf3 100644
--- a/drivers/gpio/gpio-ws16c48.c
+++ b/drivers/gpio/gpio-ws16c48.c
@@ -332,7 +332,7 @@ static irqreturn_t ws16c48_irq_handler(int irq, void *dev_id)
332 int_id = inb(ws16c48gpio->base + 8 + port); 332 int_id = inb(ws16c48gpio->base + 8 + port);
333 for_each_set_bit(gpio, &int_id, 8) 333 for_each_set_bit(gpio, &int_id, 8)
334 generic_handle_irq(irq_find_mapping( 334 generic_handle_irq(irq_find_mapping(
335 chip->irqdomain, gpio + 8*port)); 335 chip->irq.domain, gpio + 8*port));
336 } 336 }
337 337
338 int_pending = inb(ws16c48gpio->base + 6) & 0x7; 338 int_pending = inb(ws16c48gpio->base + 6) & 0x7;
diff --git a/drivers/gpio/gpio-xgene-sb.c b/drivers/gpio/gpio-xgene-sb.c
index b5843fe6a44d..2313af82fad3 100644
--- a/drivers/gpio/gpio-xgene-sb.c
+++ b/drivers/gpio/gpio-xgene-sb.c
@@ -130,10 +130,7 @@ static int xgene_gpio_sb_to_irq(struct gpio_chip *gc, u32 gpio)
130 (gpio > HWIRQ_TO_GPIO(priv, priv->nirq))) 130 (gpio > HWIRQ_TO_GPIO(priv, priv->nirq)))
131 return -ENXIO; 131 return -ENXIO;
132 132
133 if (gc->parent->of_node) 133 fwspec.fwnode = gc->parent->fwnode;
134 fwspec.fwnode = of_node_to_fwnode(gc->parent->of_node);
135 else
136 fwspec.fwnode = gc->parent->fwnode;
137 fwspec.param_count = 2; 134 fwspec.param_count = 2;
138 fwspec.param[0] = GPIO_TO_HWIRQ(priv, gpio); 135 fwspec.param[0] = GPIO_TO_HWIRQ(priv, gpio);
139 fwspec.param[1] = IRQ_TYPE_NONE; 136 fwspec.param[1] = IRQ_TYPE_NONE;
@@ -233,7 +230,6 @@ static int xgene_gpio_sb_probe(struct platform_device *pdev)
233 struct resource *res; 230 struct resource *res;
234 void __iomem *regs; 231 void __iomem *regs;
235 struct irq_domain *parent_domain = NULL; 232 struct irq_domain *parent_domain = NULL;
236 struct fwnode_handle *fwnode;
237 u32 val32; 233 u32 val32;
238 234
239 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 235 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
@@ -287,18 +283,13 @@ static int xgene_gpio_sb_probe(struct platform_device *pdev)
287 283
288 platform_set_drvdata(pdev, priv); 284 platform_set_drvdata(pdev, priv);
289 285
290 if (pdev->dev.of_node)
291 fwnode = of_node_to_fwnode(pdev->dev.of_node);
292 else
293 fwnode = pdev->dev.fwnode;
294
295 priv->irq_domain = irq_domain_create_hierarchy(parent_domain, 286 priv->irq_domain = irq_domain_create_hierarchy(parent_domain,
296 0, priv->nirq, fwnode, 287 0, priv->nirq, pdev->dev.fwnode,
297 &xgene_gpio_sb_domain_ops, priv); 288 &xgene_gpio_sb_domain_ops, priv);
298 if (!priv->irq_domain) 289 if (!priv->irq_domain)
299 return -ENODEV; 290 return -ENODEV;
300 291
301 priv->gc.irqdomain = priv->irq_domain; 292 priv->gc.irq.domain = priv->irq_domain;
302 293
303 ret = devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv); 294 ret = devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
304 if (ret) { 295 if (ret) {
diff --git a/drivers/gpio/gpio-xlp.c b/drivers/gpio/gpio-xlp.c
index d857e1d8e731..e74bd43a6974 100644
--- a/drivers/gpio/gpio-xlp.c
+++ b/drivers/gpio/gpio-xlp.c
@@ -225,7 +225,7 @@ static void xlp_gpio_generic_handler(struct irq_desc *desc)
225 225
226 if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ)) 226 if (gpio_stat & BIT(gpio % XLP_GPIO_REGSZ))
227 generic_handle_irq(irq_find_mapping( 227 generic_handle_irq(irq_find_mapping(
228 priv->chip.irqdomain, gpio)); 228 priv->chip.irq.domain, gpio));
229 } 229 }
230 chained_irq_exit(irqchip, desc); 230 chained_irq_exit(irqchip, desc);
231} 231}
diff --git a/drivers/gpio/gpio-zx.c b/drivers/gpio/gpio-zx.c
index be3a87da8438..5eacad9b2692 100644
--- a/drivers/gpio/gpio-zx.c
+++ b/drivers/gpio/gpio-zx.c
@@ -170,7 +170,7 @@ static void zx_irq_handler(struct irq_desc *desc)
170 writew_relaxed(pending, chip->base + ZX_GPIO_IC); 170 writew_relaxed(pending, chip->base + ZX_GPIO_IC);
171 if (pending) { 171 if (pending) {
172 for_each_set_bit(offset, &pending, ZX_GPIO_NR) 172 for_each_set_bit(offset, &pending, ZX_GPIO_NR)
173 generic_handle_irq(irq_find_mapping(gc->irqdomain, 173 generic_handle_irq(irq_find_mapping(gc->irq.domain,
174 offset)); 174 offset));
175 } 175 }
176 176
diff --git a/drivers/gpio/gpio-zynq.c b/drivers/gpio/gpio-zynq.c
index b3cc948a2d8b..75ee877e5cd5 100644
--- a/drivers/gpio/gpio-zynq.c
+++ b/drivers/gpio/gpio-zynq.c
@@ -562,7 +562,7 @@ static void zynq_gpio_handle_bank_irq(struct zynq_gpio *gpio,
562 unsigned long pending) 562 unsigned long pending)
563{ 563{
564 unsigned int bank_offset = gpio->p_data->bank_min[bank_num]; 564 unsigned int bank_offset = gpio->p_data->bank_min[bank_num];
565 struct irq_domain *irqdomain = gpio->chip.irqdomain; 565 struct irq_domain *irqdomain = gpio->chip.irq.domain;
566 int offset; 566 int offset;
567 567
568 if (!pending) 568 if (!pending)
diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c
index bfcd20699ec8..e0d59e61b52f 100644
--- a/drivers/gpio/gpiolib-of.c
+++ b/drivers/gpio/gpiolib-of.c
@@ -153,8 +153,8 @@ struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
153 *flags |= GPIO_OPEN_SOURCE; 153 *flags |= GPIO_OPEN_SOURCE;
154 } 154 }
155 155
156 if (of_flags & OF_GPIO_SLEEP_MAY_LOOSE_VALUE) 156 if (of_flags & OF_GPIO_SLEEP_MAY_LOSE_VALUE)
157 *flags |= GPIO_SLEEP_MAY_LOOSE_VALUE; 157 *flags |= GPIO_SLEEP_MAY_LOSE_VALUE;
158 158
159 return desc; 159 return desc;
160} 160}
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index eb80dac4e26a..86dcd02cf602 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -72,6 +72,8 @@ static LIST_HEAD(gpio_lookup_list);
72LIST_HEAD(gpio_devices); 72LIST_HEAD(gpio_devices);
73 73
74static void gpiochip_free_hogs(struct gpio_chip *chip); 74static void gpiochip_free_hogs(struct gpio_chip *chip);
75static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
76 struct lock_class_key *key);
75static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); 77static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
76static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); 78static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
77static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); 79static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
@@ -365,28 +367,28 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd,
365 struct linehandle_state *lh = filep->private_data; 367 struct linehandle_state *lh = filep->private_data;
366 void __user *ip = (void __user *)arg; 368 void __user *ip = (void __user *)arg;
367 struct gpiohandle_data ghd; 369 struct gpiohandle_data ghd;
370 int vals[GPIOHANDLES_MAX];
368 int i; 371 int i;
369 372
370 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { 373 if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
371 int val; 374 /* TODO: check if descriptors are really input */
375 int ret = gpiod_get_array_value_complex(false,
376 true,
377 lh->numdescs,
378 lh->descs,
379 vals);
380 if (ret)
381 return ret;
372 382
373 memset(&ghd, 0, sizeof(ghd)); 383 memset(&ghd, 0, sizeof(ghd));
374 384 for (i = 0; i < lh->numdescs; i++)
375 /* TODO: check if descriptors are really input */ 385 ghd.values[i] = vals[i];
376 for (i = 0; i < lh->numdescs; i++) {
377 val = gpiod_get_value_cansleep(lh->descs[i]);
378 if (val < 0)
379 return val;
380 ghd.values[i] = val;
381 }
382 386
383 if (copy_to_user(ip, &ghd, sizeof(ghd))) 387 if (copy_to_user(ip, &ghd, sizeof(ghd)))
384 return -EFAULT; 388 return -EFAULT;
385 389
386 return 0; 390 return 0;
387 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { 391 } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
388 int vals[GPIOHANDLES_MAX];
389
390 /* TODO: check if descriptors are really output */ 392 /* TODO: check if descriptors are really output */
391 if (copy_from_user(&ghd, ip, sizeof(ghd))) 393 if (copy_from_user(&ghd, ip, sizeof(ghd)))
392 return -EFAULT; 394 return -EFAULT;
@@ -444,12 +446,25 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
444 struct linehandle_state *lh; 446 struct linehandle_state *lh;
445 struct file *file; 447 struct file *file;
446 int fd, i, ret; 448 int fd, i, ret;
449 u32 lflags;
447 450
448 if (copy_from_user(&handlereq, ip, sizeof(handlereq))) 451 if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
449 return -EFAULT; 452 return -EFAULT;
450 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) 453 if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
451 return -EINVAL; 454 return -EINVAL;
452 455
456 lflags = handlereq.flags;
457
458 /* Return an error if an unknown flag is set */
459 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
460 return -EINVAL;
461
462 /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
463 if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
464 ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
465 (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
466 return -EINVAL;
467
453 lh = kzalloc(sizeof(*lh), GFP_KERNEL); 468 lh = kzalloc(sizeof(*lh), GFP_KERNEL);
454 if (!lh) 469 if (!lh)
455 return -ENOMEM; 470 return -ENOMEM;
@@ -470,7 +485,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
470 /* Request each GPIO */ 485 /* Request each GPIO */
471 for (i = 0; i < handlereq.lines; i++) { 486 for (i = 0; i < handlereq.lines; i++) {
472 u32 offset = handlereq.lineoffsets[i]; 487 u32 offset = handlereq.lineoffsets[i];
473 u32 lflags = handlereq.flags;
474 struct gpio_desc *desc; 488 struct gpio_desc *desc;
475 489
476 if (offset >= gdev->ngpio) { 490 if (offset >= gdev->ngpio) {
@@ -478,12 +492,6 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
478 goto out_free_descs; 492 goto out_free_descs;
479 } 493 }
480 494
481 /* Return an error if a unknown flag is set */
482 if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) {
483 ret = -EINVAL;
484 goto out_free_descs;
485 }
486
487 desc = &gdev->descs[offset]; 495 desc = &gdev->descs[offset];
488 ret = gpiod_request(desc, lh->label); 496 ret = gpiod_request(desc, lh->label);
489 if (ret) 497 if (ret)
@@ -1091,30 +1099,8 @@ static void gpiochip_setup_devs(void)
1091 } 1099 }
1092} 1100}
1093 1101
1094/** 1102int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
1095 * gpiochip_add_data() - register a gpio_chip 1103 struct lock_class_key *key)
1096 * @chip: the chip to register, with chip->base initialized
1097 * @data: driver-private data associated with this chip
1098 *
1099 * Context: potentially before irqs will work
1100 *
1101 * When gpiochip_add_data() is called very early during boot, so that GPIOs
1102 * can be freely used, the chip->parent device must be registered before
1103 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
1104 * for GPIOs will fail rudely.
1105 *
1106 * gpiochip_add_data() must only be called after gpiolib initialization,
1107 * ie after core_initcall().
1108 *
1109 * If chip->base is negative, this requests dynamic assignment of
1110 * a range of valid GPIOs.
1111 *
1112 * Returns:
1113 * A negative errno if the chip can't be registered, such as because the
1114 * chip->base is invalid or already associated with a different chip.
1115 * Otherwise it returns zero as a success code.
1116 */
1117int gpiochip_add_data(struct gpio_chip *chip, void *data)
1118{ 1104{
1119 unsigned long flags; 1105 unsigned long flags;
1120 int status = 0; 1106 int status = 0;
@@ -1260,6 +1246,10 @@ int gpiochip_add_data(struct gpio_chip *chip, void *data)
1260 if (status) 1246 if (status)
1261 goto err_remove_from_list; 1247 goto err_remove_from_list;
1262 1248
1249 status = gpiochip_add_irqchip(chip, key);
1250 if (status)
1251 goto err_remove_chip;
1252
1263 status = of_gpiochip_add(chip); 1253 status = of_gpiochip_add(chip);
1264 if (status) 1254 if (status)
1265 goto err_remove_chip; 1255 goto err_remove_chip;
@@ -1303,7 +1293,7 @@ err_free_gdev:
1303 kfree(gdev); 1293 kfree(gdev);
1304 return status; 1294 return status;
1305} 1295}
1306EXPORT_SYMBOL_GPL(gpiochip_add_data); 1296EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
1307 1297
1308/** 1298/**
1309 * gpiochip_get_data() - get per-subdriver data for the chip 1299 * gpiochip_get_data() - get per-subdriver data for the chip
@@ -1498,33 +1488,33 @@ static struct gpio_chip *find_chip_by_name(const char *name)
1498 1488
1499static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) 1489static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1500{ 1490{
1501 if (!gpiochip->irq_need_valid_mask) 1491 if (!gpiochip->irq.need_valid_mask)
1502 return 0; 1492 return 0;
1503 1493
1504 gpiochip->irq_valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio), 1494 gpiochip->irq.valid_mask = kcalloc(BITS_TO_LONGS(gpiochip->ngpio),
1505 sizeof(long), GFP_KERNEL); 1495 sizeof(long), GFP_KERNEL);
1506 if (!gpiochip->irq_valid_mask) 1496 if (!gpiochip->irq.valid_mask)
1507 return -ENOMEM; 1497 return -ENOMEM;
1508 1498
1509 /* Assume by default all GPIOs are valid */ 1499 /* Assume by default all GPIOs are valid */
1510 bitmap_fill(gpiochip->irq_valid_mask, gpiochip->ngpio); 1500 bitmap_fill(gpiochip->irq.valid_mask, gpiochip->ngpio);
1511 1501
1512 return 0; 1502 return 0;
1513} 1503}
1514 1504
1515static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) 1505static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
1516{ 1506{
1517 kfree(gpiochip->irq_valid_mask); 1507 kfree(gpiochip->irq.valid_mask);
1518 gpiochip->irq_valid_mask = NULL; 1508 gpiochip->irq.valid_mask = NULL;
1519} 1509}
1520 1510
1521static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, 1511static bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
1522 unsigned int offset) 1512 unsigned int offset)
1523{ 1513{
1524 /* No mask means all valid */ 1514 /* No mask means all valid */
1525 if (likely(!gpiochip->irq_valid_mask)) 1515 if (likely(!gpiochip->irq.valid_mask))
1526 return true; 1516 return true;
1527 return test_bit(offset, gpiochip->irq_valid_mask); 1517 return test_bit(offset, gpiochip->irq.valid_mask);
1528} 1518}
1529 1519
1530/** 1520/**
@@ -1544,7 +1534,7 @@ static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1544{ 1534{
1545 unsigned int offset; 1535 unsigned int offset;
1546 1536
1547 if (!gpiochip->irqdomain) { 1537 if (!gpiochip->irq.domain) {
1548 chip_err(gpiochip, "called %s before setting up irqchip\n", 1538 chip_err(gpiochip, "called %s before setting up irqchip\n",
1549 __func__); 1539 __func__);
1550 return; 1540 return;
@@ -1564,14 +1554,15 @@ static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip,
1564 irq_set_chained_handler_and_data(parent_irq, parent_handler, 1554 irq_set_chained_handler_and_data(parent_irq, parent_handler,
1565 gpiochip); 1555 gpiochip);
1566 1556
1567 gpiochip->irq_chained_parent = parent_irq; 1557 gpiochip->irq.parents = &parent_irq;
1558 gpiochip->irq.num_parents = 1;
1568 } 1559 }
1569 1560
1570 /* Set the parent IRQ for all affected IRQs */ 1561 /* Set the parent IRQ for all affected IRQs */
1571 for (offset = 0; offset < gpiochip->ngpio; offset++) { 1562 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1572 if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) 1563 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1573 continue; 1564 continue;
1574 irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), 1565 irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset),
1575 parent_irq); 1566 parent_irq);
1576 } 1567 }
1577} 1568}
@@ -1591,6 +1582,11 @@ void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
1591 unsigned int parent_irq, 1582 unsigned int parent_irq,
1592 irq_flow_handler_t parent_handler) 1583 irq_flow_handler_t parent_handler)
1593{ 1584{
1585 if (gpiochip->irq.threaded) {
1586 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
1587 return;
1588 }
1589
1594 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, 1590 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1595 parent_handler); 1591 parent_handler);
1596} 1592}
@@ -1607,10 +1603,6 @@ void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
1607 struct irq_chip *irqchip, 1603 struct irq_chip *irqchip,
1608 unsigned int parent_irq) 1604 unsigned int parent_irq)
1609{ 1605{
1610 if (!gpiochip->irq_nested) {
1611 chip_err(gpiochip, "tried to nest a chained gpiochip\n");
1612 return;
1613 }
1614 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, 1606 gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq,
1615 NULL); 1607 NULL);
1616} 1608}
@@ -1626,10 +1618,11 @@ EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
1626 * gpiochip by assigning the gpiochip as chip data, and using the irqchip 1618 * gpiochip by assigning the gpiochip as chip data, and using the irqchip
1627 * stored inside the gpiochip. 1619 * stored inside the gpiochip.
1628 */ 1620 */
1629static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, 1621int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1630 irq_hw_number_t hwirq) 1622 irq_hw_number_t hwirq)
1631{ 1623{
1632 struct gpio_chip *chip = d->host_data; 1624 struct gpio_chip *chip = d->host_data;
1625 int err = 0;
1633 1626
1634 if (!gpiochip_irqchip_irq_valid(chip, hwirq)) 1627 if (!gpiochip_irqchip_irq_valid(chip, hwirq))
1635 return -ENXIO; 1628 return -ENXIO;
@@ -1639,32 +1632,42 @@ static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
1639 * This lock class tells lockdep that GPIO irqs are in a different 1632 * This lock class tells lockdep that GPIO irqs are in a different
1640 * category than their parents, so it won't report false recursion. 1633 * category than their parents, so it won't report false recursion.
1641 */ 1634 */
1642 irq_set_lockdep_class(irq, chip->lock_key); 1635 irq_set_lockdep_class(irq, chip->irq.lock_key);
1643 irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); 1636 irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
1644 /* Chips that use nested thread handlers have them marked */ 1637 /* Chips that use nested thread handlers have them marked */
1645 if (chip->irq_nested) 1638 if (chip->irq.threaded)
1646 irq_set_nested_thread(irq, 1); 1639 irq_set_nested_thread(irq, 1);
1647 irq_set_noprobe(irq); 1640 irq_set_noprobe(irq);
1648 1641
1642 if (chip->irq.num_parents == 1)
1643 err = irq_set_parent(irq, chip->irq.parents[0]);
1644 else if (chip->irq.map)
1645 err = irq_set_parent(irq, chip->irq.map[hwirq]);
1646
1647 if (err < 0)
1648 return err;
1649
1649 /* 1650 /*
1650 * No set-up of the hardware will happen if IRQ_TYPE_NONE 1651 * No set-up of the hardware will happen if IRQ_TYPE_NONE
1651 * is passed as default type. 1652 * is passed as default type.
1652 */ 1653 */
1653 if (chip->irq_default_type != IRQ_TYPE_NONE) 1654 if (chip->irq.default_type != IRQ_TYPE_NONE)
1654 irq_set_irq_type(irq, chip->irq_default_type); 1655 irq_set_irq_type(irq, chip->irq.default_type);
1655 1656
1656 return 0; 1657 return 0;
1657} 1658}
1659EXPORT_SYMBOL_GPL(gpiochip_irq_map);
1658 1660
1659static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) 1661void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
1660{ 1662{
1661 struct gpio_chip *chip = d->host_data; 1663 struct gpio_chip *chip = d->host_data;
1662 1664
1663 if (chip->irq_nested) 1665 if (chip->irq.threaded)
1664 irq_set_nested_thread(irq, 0); 1666 irq_set_nested_thread(irq, 0);
1665 irq_set_chip_and_handler(irq, NULL, NULL); 1667 irq_set_chip_and_handler(irq, NULL, NULL);
1666 irq_set_chip_data(irq, NULL); 1668 irq_set_chip_data(irq, NULL);
1667} 1669}
1670EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
1668 1671
1669static const struct irq_domain_ops gpiochip_domain_ops = { 1672static const struct irq_domain_ops gpiochip_domain_ops = {
1670 .map = gpiochip_irq_map, 1673 .map = gpiochip_irq_map,
@@ -1702,7 +1705,94 @@ static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
1702{ 1705{
1703 if (!gpiochip_irqchip_irq_valid(chip, offset)) 1706 if (!gpiochip_irqchip_irq_valid(chip, offset))
1704 return -ENXIO; 1707 return -ENXIO;
1705 return irq_create_mapping(chip->irqdomain, offset); 1708
1709 return irq_create_mapping(chip->irq.domain, offset);
1710}
1711
1712/**
1713 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
1714 * @gpiochip: the GPIO chip to add the IRQ chip to
1715 * @lock_key: lockdep class
1716 */
1717static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1718 struct lock_class_key *lock_key)
1719{
1720 struct irq_chip *irqchip = gpiochip->irq.chip;
1721 const struct irq_domain_ops *ops;
1722 struct device_node *np;
1723 unsigned int type;
1724 unsigned int i;
1725
1726 if (!irqchip)
1727 return 0;
1728
1729 if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
1730 chip_err(gpiochip, "you cannot have chained interrupts on a "
1731 "chip that may sleep\n");
1732 return -EINVAL;
1733 }
1734
1735 np = gpiochip->gpiodev->dev.of_node;
1736 type = gpiochip->irq.default_type;
1737
1738 /*
1739 * Specifying a default trigger is a terrible idea if DT or ACPI is
1740 * used to configure the interrupts, as you may end up with
1741 * conflicting triggers. Tell the user, and reset to NONE.
1742 */
1743 if (WARN(np && type != IRQ_TYPE_NONE,
1744 "%s: Ignoring %u default trigger\n", np->full_name, type))
1745 type = IRQ_TYPE_NONE;
1746
1747 if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
1748 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
1749 "Ignoring %u default trigger\n", type);
1750 type = IRQ_TYPE_NONE;
1751 }
1752
1753 gpiochip->to_irq = gpiochip_to_irq;
1754 gpiochip->irq.default_type = type;
1755 gpiochip->irq.lock_key = lock_key;
1756
1757 if (gpiochip->irq.domain_ops)
1758 ops = gpiochip->irq.domain_ops;
1759 else
1760 ops = &gpiochip_domain_ops;
1761
1762 gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio,
1763 gpiochip->irq.first,
1764 ops, gpiochip);
1765 if (!gpiochip->irq.domain)
1766 return -EINVAL;
1767
1768 /*
1769 * It is possible for a driver to override this, but only if the
1770 * alternative functions are both implemented.
1771 */
1772 if (!irqchip->irq_request_resources &&
1773 !irqchip->irq_release_resources) {
1774 irqchip->irq_request_resources = gpiochip_irq_reqres;
1775 irqchip->irq_release_resources = gpiochip_irq_relres;
1776 }
1777
1778 if (gpiochip->irq.parent_handler) {
1779 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
1780
1781 for (i = 0; i < gpiochip->irq.num_parents; i++) {
1782 /*
1783 * The parent IRQ chip is already using the chip_data
1784 * for this IRQ chip, so our callbacks simply use the
1785 * handler_data.
1786 */
1787 irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
1788 gpiochip->irq.parent_handler,
1789 data);
1790 }
1791 }
1792
1793 acpi_gpiochip_request_interrupts(gpiochip);
1794
1795 return 0;
1706} 1796}
1707 1797
1708/** 1798/**
@@ -1717,26 +1807,34 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1717 1807
1718 acpi_gpiochip_free_interrupts(gpiochip); 1808 acpi_gpiochip_free_interrupts(gpiochip);
1719 1809
1720 if (gpiochip->irq_chained_parent) { 1810 if (gpiochip->irq.chip && gpiochip->irq.parent_handler) {
1721 irq_set_chained_handler(gpiochip->irq_chained_parent, NULL); 1811 struct gpio_irq_chip *irq = &gpiochip->irq;
1722 irq_set_handler_data(gpiochip->irq_chained_parent, NULL); 1812 unsigned int i;
1813
1814 for (i = 0; i < irq->num_parents; i++)
1815 irq_set_chained_handler_and_data(irq->parents[i],
1816 NULL, NULL);
1723 } 1817 }
1724 1818
1725 /* Remove all IRQ mappings and delete the domain */ 1819 /* Remove all IRQ mappings and delete the domain */
1726 if (gpiochip->irqdomain) { 1820 if (gpiochip->irq.domain) {
1821 unsigned int irq;
1822
1727 for (offset = 0; offset < gpiochip->ngpio; offset++) { 1823 for (offset = 0; offset < gpiochip->ngpio; offset++) {
1728 if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) 1824 if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
1729 continue; 1825 continue;
1730 irq_dispose_mapping( 1826
1731 irq_find_mapping(gpiochip->irqdomain, offset)); 1827 irq = irq_find_mapping(gpiochip->irq.domain, offset);
1828 irq_dispose_mapping(irq);
1732 } 1829 }
1733 irq_domain_remove(gpiochip->irqdomain); 1830
1831 irq_domain_remove(gpiochip->irq.domain);
1734 } 1832 }
1735 1833
1736 if (gpiochip->irqchip) { 1834 if (gpiochip->irq.chip) {
1737 gpiochip->irqchip->irq_request_resources = NULL; 1835 gpiochip->irq.chip->irq_request_resources = NULL;
1738 gpiochip->irqchip->irq_release_resources = NULL; 1836 gpiochip->irq.chip->irq_release_resources = NULL;
1739 gpiochip->irqchip = NULL; 1837 gpiochip->irq.chip = NULL;
1740 } 1838 }
1741 1839
1742 gpiochip_irqchip_free_valid_mask(gpiochip); 1840 gpiochip_irqchip_free_valid_mask(gpiochip);
@@ -1751,8 +1849,7 @@ static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
1751 * @handler: the irq handler to use (often a predefined irq core function) 1849 * @handler: the irq handler to use (often a predefined irq core function)
1752 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE 1850 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
1753 * to have the core avoid setting up any default type in the hardware. 1851 * to have the core avoid setting up any default type in the hardware.
1754 * @nested: whether this is a nested irqchip calling handle_nested_irq() 1852 * @threaded: whether this irqchip uses a nested thread handler
1755 * in its IRQ handler
1756 * @lock_key: lockdep class 1853 * @lock_key: lockdep class
1757 * 1854 *
1758 * This function closely associates a certain irqchip with a certain 1855 * This function closely associates a certain irqchip with a certain
@@ -1774,7 +1871,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1774 unsigned int first_irq, 1871 unsigned int first_irq,
1775 irq_flow_handler_t handler, 1872 irq_flow_handler_t handler,
1776 unsigned int type, 1873 unsigned int type,
1777 bool nested, 1874 bool threaded,
1778 struct lock_class_key *lock_key) 1875 struct lock_class_key *lock_key)
1779{ 1876{
1780 struct device_node *of_node; 1877 struct device_node *of_node;
@@ -1786,7 +1883,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1786 pr_err("missing gpiochip .dev parent pointer\n"); 1883 pr_err("missing gpiochip .dev parent pointer\n");
1787 return -EINVAL; 1884 return -EINVAL;
1788 } 1885 }
1789 gpiochip->irq_nested = nested; 1886 gpiochip->irq.threaded = threaded;
1790 of_node = gpiochip->parent->of_node; 1887 of_node = gpiochip->parent->of_node;
1791#ifdef CONFIG_OF_GPIO 1888#ifdef CONFIG_OF_GPIO
1792 /* 1889 /*
@@ -1811,16 +1908,16 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
1811 type = IRQ_TYPE_NONE; 1908 type = IRQ_TYPE_NONE;
1812 } 1909 }
1813 1910
1814 gpiochip->irqchip = irqchip; 1911 gpiochip->irq.chip = irqchip;
1815 gpiochip->irq_handler = handler; 1912 gpiochip->irq.handler = handler;
1816 gpiochip->irq_default_type = type; 1913 gpiochip->irq.default_type = type;
1817 gpiochip->to_irq = gpiochip_to_irq; 1914 gpiochip->to_irq = gpiochip_to_irq;
1818 gpiochip->lock_key = lock_key; 1915 gpiochip->irq.lock_key = lock_key;
1819 gpiochip->irqdomain = irq_domain_add_simple(of_node, 1916 gpiochip->irq.domain = irq_domain_add_simple(of_node,
1820 gpiochip->ngpio, first_irq, 1917 gpiochip->ngpio, first_irq,
1821 &gpiochip_domain_ops, gpiochip); 1918 &gpiochip_domain_ops, gpiochip);
1822 if (!gpiochip->irqdomain) { 1919 if (!gpiochip->irq.domain) {
1823 gpiochip->irqchip = NULL; 1920 gpiochip->irq.chip = NULL;
1824 return -EINVAL; 1921 return -EINVAL;
1825 } 1922 }
1826 1923
@@ -1842,6 +1939,12 @@ EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
1842 1939
1843#else /* CONFIG_GPIOLIB_IRQCHIP */ 1940#else /* CONFIG_GPIOLIB_IRQCHIP */
1844 1941
1942static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
1943 struct lock_class_key *key)
1944{
1945 return 0;
1946}
1947
1845static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} 1948static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
1846static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) 1949static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
1847{ 1950{
@@ -2013,7 +2116,7 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
2013 * on each other, and help provide better diagnostics in debugfs. 2116 * on each other, and help provide better diagnostics in debugfs.
2014 * They're called even less than the "set direction" calls. 2117 * They're called even less than the "set direction" calls.
2015 */ 2118 */
2016static int __gpiod_request(struct gpio_desc *desc, const char *label) 2119static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
2017{ 2120{
2018 struct gpio_chip *chip = desc->gdev->chip; 2121 struct gpio_chip *chip = desc->gdev->chip;
2019 int status; 2122 int status;
@@ -2106,7 +2209,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label)
2106 gdev = desc->gdev; 2209 gdev = desc->gdev;
2107 2210
2108 if (try_module_get(gdev->owner)) { 2211 if (try_module_get(gdev->owner)) {
2109 status = __gpiod_request(desc, label); 2212 status = gpiod_request_commit(desc, label);
2110 if (status < 0) 2213 if (status < 0)
2111 module_put(gdev->owner); 2214 module_put(gdev->owner);
2112 else 2215 else
@@ -2119,7 +2222,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label)
2119 return status; 2222 return status;
2120} 2223}
2121 2224
2122static bool __gpiod_free(struct gpio_desc *desc) 2225static bool gpiod_free_commit(struct gpio_desc *desc)
2123{ 2226{
2124 bool ret = false; 2227 bool ret = false;
2125 unsigned long flags; 2228 unsigned long flags;
@@ -2154,7 +2257,7 @@ static bool __gpiod_free(struct gpio_desc *desc)
2154 2257
2155void gpiod_free(struct gpio_desc *desc) 2258void gpiod_free(struct gpio_desc *desc)
2156{ 2259{
2157 if (desc && desc->gdev && __gpiod_free(desc)) { 2260 if (desc && desc->gdev && gpiod_free_commit(desc)) {
2158 module_put(desc->gdev->owner); 2261 module_put(desc->gdev->owner);
2159 put_device(&desc->gdev->dev); 2262 put_device(&desc->gdev->dev);
2160 } else { 2263 } else {
@@ -2217,7 +2320,7 @@ struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
2217 return desc; 2320 return desc;
2218 } 2321 }
2219 2322
2220 err = __gpiod_request(desc, label); 2323 err = gpiod_request_commit(desc, label);
2221 if (err < 0) 2324 if (err < 0)
2222 return ERR_PTR(err); 2325 return ERR_PTR(err);
2223 2326
@@ -2235,7 +2338,7 @@ EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
2235void gpiochip_free_own_desc(struct gpio_desc *desc) 2338void gpiochip_free_own_desc(struct gpio_desc *desc)
2236{ 2339{
2237 if (desc) 2340 if (desc)
2238 __gpiod_free(desc); 2341 gpiod_free_commit(desc);
2239} 2342}
2240EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); 2343EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
2241 2344
@@ -2291,44 +2394,12 @@ static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset,
2291 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; 2394 return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
2292} 2395}
2293 2396
2294static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) 2397static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
2295{ 2398{
2296 struct gpio_chip *gc = desc->gdev->chip; 2399 struct gpio_chip *gc = desc->gdev->chip;
2297 int val = !!value; 2400 int val = !!value;
2298 int ret; 2401 int ret;
2299 2402
2300 /* GPIOs used for IRQs shall not be set as output */
2301 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2302 gpiod_err(desc,
2303 "%s: tried to set a GPIO tied to an IRQ as output\n",
2304 __func__);
2305 return -EIO;
2306 }
2307
2308 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2309 /* First see if we can enable open drain in hardware */
2310 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2311 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2312 if (!ret)
2313 goto set_output_value;
2314 /* Emulate open drain by not actively driving the line high */
2315 if (val)
2316 return gpiod_direction_input(desc);
2317 }
2318 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2319 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2320 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2321 if (!ret)
2322 goto set_output_value;
2323 /* Emulate open source by not actively driving the line low */
2324 if (!val)
2325 return gpiod_direction_input(desc);
2326 } else {
2327 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2328 PIN_CONFIG_DRIVE_PUSH_PULL);
2329 }
2330
2331set_output_value:
2332 if (!gc->set || !gc->direction_output) { 2403 if (!gc->set || !gc->direction_output) {
2333 gpiod_warn(desc, 2404 gpiod_warn(desc,
2334 "%s: missing set() or direction_output() operations\n", 2405 "%s: missing set() or direction_output() operations\n",
@@ -2358,7 +2429,7 @@ set_output_value:
2358int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 2429int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
2359{ 2430{
2360 VALIDATE_DESC(desc); 2431 VALIDATE_DESC(desc);
2361 return _gpiod_direction_output_raw(desc, value); 2432 return gpiod_direction_output_raw_commit(desc, value);
2362} 2433}
2363EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); 2434EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2364 2435
@@ -2376,12 +2447,48 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
2376 */ 2447 */
2377int gpiod_direction_output(struct gpio_desc *desc, int value) 2448int gpiod_direction_output(struct gpio_desc *desc, int value)
2378{ 2449{
2450 struct gpio_chip *gc = desc->gdev->chip;
2451 int ret;
2452
2379 VALIDATE_DESC(desc); 2453 VALIDATE_DESC(desc);
2380 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2454 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2381 value = !value; 2455 value = !value;
2382 else 2456 else
2383 value = !!value; 2457 value = !!value;
2384 return _gpiod_direction_output_raw(desc, value); 2458
2459 /* GPIOs used for IRQs shall not be set as output */
2460 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
2461 gpiod_err(desc,
2462 "%s: tried to set a GPIO tied to an IRQ as output\n",
2463 __func__);
2464 return -EIO;
2465 }
2466
2467 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
2468 /* First see if we can enable open drain in hardware */
2469 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2470 PIN_CONFIG_DRIVE_OPEN_DRAIN);
2471 if (!ret)
2472 goto set_output_value;
2473 /* Emulate open drain by not actively driving the line high */
2474 if (value)
2475 return gpiod_direction_input(desc);
2476 }
2477 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
2478 ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2479 PIN_CONFIG_DRIVE_OPEN_SOURCE);
2480 if (!ret)
2481 goto set_output_value;
2482 /* Emulate open source by not actively driving the line low */
2483 if (!value)
2484 return gpiod_direction_input(desc);
2485 } else {
2486 gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
2487 PIN_CONFIG_DRIVE_PUSH_PULL);
2488 }
2489
2490set_output_value:
2491 return gpiod_direction_output_raw_commit(desc, value);
2385} 2492}
2386EXPORT_SYMBOL_GPL(gpiod_direction_output); 2493EXPORT_SYMBOL_GPL(gpiod_direction_output);
2387 2494
@@ -2448,7 +2555,7 @@ EXPORT_SYMBOL_GPL(gpiod_is_active_low);
2448 * that the GPIO was actually requested. 2555 * that the GPIO was actually requested.
2449 */ 2556 */
2450 2557
2451static int _gpiod_get_raw_value(const struct gpio_desc *desc) 2558static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
2452{ 2559{
2453 struct gpio_chip *chip; 2560 struct gpio_chip *chip;
2454 int offset; 2561 int offset;
@@ -2462,6 +2569,71 @@ static int _gpiod_get_raw_value(const struct gpio_desc *desc)
2462 return value; 2569 return value;
2463} 2570}
2464 2571
2572static int gpio_chip_get_multiple(struct gpio_chip *chip,
2573 unsigned long *mask, unsigned long *bits)
2574{
2575 if (chip->get_multiple) {
2576 return chip->get_multiple(chip, mask, bits);
2577 } else if (chip->get) {
2578 int i, value;
2579
2580 for_each_set_bit(i, mask, chip->ngpio) {
2581 value = chip->get(chip, i);
2582 if (value < 0)
2583 return value;
2584 __assign_bit(i, bits, value);
2585 }
2586 return 0;
2587 }
2588 return -EIO;
2589}
2590
2591int gpiod_get_array_value_complex(bool raw, bool can_sleep,
2592 unsigned int array_size,
2593 struct gpio_desc **desc_array,
2594 int *value_array)
2595{
2596 int i = 0;
2597
2598 while (i < array_size) {
2599 struct gpio_chip *chip = desc_array[i]->gdev->chip;
2600 unsigned long mask[BITS_TO_LONGS(chip->ngpio)];
2601 unsigned long bits[BITS_TO_LONGS(chip->ngpio)];
2602 int first, j, ret;
2603
2604 if (!can_sleep)
2605 WARN_ON(chip->can_sleep);
2606
2607 /* collect all inputs belonging to the same chip */
2608 first = i;
2609 memset(mask, 0, sizeof(mask));
2610 do {
2611 const struct gpio_desc *desc = desc_array[i];
2612 int hwgpio = gpio_chip_hwgpio(desc);
2613
2614 __set_bit(hwgpio, mask);
2615 i++;
2616 } while ((i < array_size) &&
2617 (desc_array[i]->gdev->chip == chip));
2618
2619 ret = gpio_chip_get_multiple(chip, mask, bits);
2620 if (ret)
2621 return ret;
2622
2623 for (j = first; j < i; j++) {
2624 const struct gpio_desc *desc = desc_array[j];
2625 int hwgpio = gpio_chip_hwgpio(desc);
2626 int value = test_bit(hwgpio, bits);
2627
2628 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2629 value = !value;
2630 value_array[j] = value;
2631 trace_gpio_value(desc_to_gpio(desc), 1, value);
2632 }
2633 }
2634 return 0;
2635}
2636
2465/** 2637/**
2466 * gpiod_get_raw_value() - return a gpio's raw value 2638 * gpiod_get_raw_value() - return a gpio's raw value
2467 * @desc: gpio whose value will be returned 2639 * @desc: gpio whose value will be returned
@@ -2477,7 +2649,7 @@ int gpiod_get_raw_value(const struct gpio_desc *desc)
2477 VALIDATE_DESC(desc); 2649 VALIDATE_DESC(desc);
2478 /* Should be using gpio_get_value_cansleep() */ 2650 /* Should be using gpio_get_value_cansleep() */
2479 WARN_ON(desc->gdev->chip->can_sleep); 2651 WARN_ON(desc->gdev->chip->can_sleep);
2480 return _gpiod_get_raw_value(desc); 2652 return gpiod_get_raw_value_commit(desc);
2481} 2653}
2482EXPORT_SYMBOL_GPL(gpiod_get_raw_value); 2654EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
2483 2655
@@ -2499,7 +2671,7 @@ int gpiod_get_value(const struct gpio_desc *desc)
2499 /* Should be using gpio_get_value_cansleep() */ 2671 /* Should be using gpio_get_value_cansleep() */
2500 WARN_ON(desc->gdev->chip->can_sleep); 2672 WARN_ON(desc->gdev->chip->can_sleep);
2501 2673
2502 value = _gpiod_get_raw_value(desc); 2674 value = gpiod_get_raw_value_commit(desc);
2503 if (value < 0) 2675 if (value < 0)
2504 return value; 2676 return value;
2505 2677
@@ -2510,12 +2682,57 @@ int gpiod_get_value(const struct gpio_desc *desc)
2510} 2682}
2511EXPORT_SYMBOL_GPL(gpiod_get_value); 2683EXPORT_SYMBOL_GPL(gpiod_get_value);
2512 2684
2685/**
2686 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
2687 * @array_size: number of elements in the descriptor / value arrays
2688 * @desc_array: array of GPIO descriptors whose values will be read
2689 * @value_array: array to store the read values
2690 *
2691 * Read the raw values of the GPIOs, i.e. the values of the physical lines
2692 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
2693 * else an error code.
2694 *
2695 * This function should be called from contexts where we cannot sleep,
2696 * and it will complain if the GPIO chip functions potentially sleep.
2697 */
2698int gpiod_get_raw_array_value(unsigned int array_size,
2699 struct gpio_desc **desc_array, int *value_array)
2700{
2701 if (!desc_array)
2702 return -EINVAL;
2703 return gpiod_get_array_value_complex(true, false, array_size,
2704 desc_array, value_array);
2705}
2706EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
2707
2708/**
2709 * gpiod_get_array_value() - read values from an array of GPIOs
2710 * @array_size: number of elements in the descriptor / value arrays
2711 * @desc_array: array of GPIO descriptors whose values will be read
2712 * @value_array: array to store the read values
2713 *
2714 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
2715 * into account. Return 0 in case of success, else an error code.
2716 *
2717 * This function should be called from contexts where we cannot sleep,
2718 * and it will complain if the GPIO chip functions potentially sleep.
2719 */
2720int gpiod_get_array_value(unsigned int array_size,
2721 struct gpio_desc **desc_array, int *value_array)
2722{
2723 if (!desc_array)
2724 return -EINVAL;
2725 return gpiod_get_array_value_complex(false, false, array_size,
2726 desc_array, value_array);
2727}
2728EXPORT_SYMBOL_GPL(gpiod_get_array_value);
2729
2513/* 2730/*
2514 * _gpio_set_open_drain_value() - Set the open drain gpio's value. 2731 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
2515 * @desc: gpio descriptor whose state need to be set. 2732 * @desc: gpio descriptor whose state need to be set.
2516 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 2733 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2517 */ 2734 */
2518static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) 2735static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
2519{ 2736{
2520 int err = 0; 2737 int err = 0;
2521 struct gpio_chip *chip = desc->gdev->chip; 2738 struct gpio_chip *chip = desc->gdev->chip;
@@ -2542,7 +2759,7 @@ static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value)
2542 * @desc: gpio descriptor whose state need to be set. 2759 * @desc: gpio descriptor whose state need to be set.
2543 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 2760 * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
2544 */ 2761 */
2545static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) 2762static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
2546{ 2763{
2547 int err = 0; 2764 int err = 0;
2548 struct gpio_chip *chip = desc->gdev->chip; 2765 struct gpio_chip *chip = desc->gdev->chip;
@@ -2564,18 +2781,13 @@ static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value)
2564 __func__, err); 2781 __func__, err);
2565} 2782}
2566 2783
2567static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) 2784static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
2568{ 2785{
2569 struct gpio_chip *chip; 2786 struct gpio_chip *chip;
2570 2787
2571 chip = desc->gdev->chip; 2788 chip = desc->gdev->chip;
2572 trace_gpio_value(desc_to_gpio(desc), 0, value); 2789 trace_gpio_value(desc_to_gpio(desc), 0, value);
2573 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2790 chip->set(chip, gpio_chip_hwgpio(desc), value);
2574 _gpio_set_open_drain_value(desc, value);
2575 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2576 _gpio_set_open_source_value(desc, value);
2577 else
2578 chip->set(chip, gpio_chip_hwgpio(desc), value);
2579} 2791}
2580 2792
2581/* 2793/*
@@ -2630,10 +2842,10 @@ void gpiod_set_array_value_complex(bool raw, bool can_sleep,
2630 * collect all normal outputs belonging to the same chip 2842 * collect all normal outputs belonging to the same chip
2631 * open drain and open source outputs are set individually 2843 * open drain and open source outputs are set individually
2632 */ 2844 */
2633 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 2845 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
2634 _gpio_set_open_drain_value(desc, value); 2846 gpio_set_open_drain_value_commit(desc, value);
2635 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { 2847 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
2636 _gpio_set_open_source_value(desc, value); 2848 gpio_set_open_source_value_commit(desc, value);
2637 } else { 2849 } else {
2638 __set_bit(hwgpio, mask); 2850 __set_bit(hwgpio, mask);
2639 if (value) 2851 if (value)
@@ -2667,7 +2879,7 @@ void gpiod_set_raw_value(struct gpio_desc *desc, int value)
2667 VALIDATE_DESC_VOID(desc); 2879 VALIDATE_DESC_VOID(desc);
2668 /* Should be using gpiod_set_value_cansleep() */ 2880 /* Should be using gpiod_set_value_cansleep() */
2669 WARN_ON(desc->gdev->chip->can_sleep); 2881 WARN_ON(desc->gdev->chip->can_sleep);
2670 _gpiod_set_raw_value(desc, value); 2882 gpiod_set_raw_value_commit(desc, value);
2671} 2883}
2672EXPORT_SYMBOL_GPL(gpiod_set_raw_value); 2884EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2673 2885
@@ -2676,8 +2888,8 @@ EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
2676 * @desc: gpio whose value will be assigned 2888 * @desc: gpio whose value will be assigned
2677 * @value: value to assign 2889 * @value: value to assign
2678 * 2890 *
2679 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 2891 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
2680 * account 2892 * OPEN_DRAIN and OPEN_SOURCE flags into account.
2681 * 2893 *
2682 * This function should be called from contexts where we cannot sleep, and will 2894 * This function should be called from contexts where we cannot sleep, and will
2683 * complain if the GPIO chip functions potentially sleep. 2895 * complain if the GPIO chip functions potentially sleep.
@@ -2689,7 +2901,12 @@ void gpiod_set_value(struct gpio_desc *desc, int value)
2689 WARN_ON(desc->gdev->chip->can_sleep); 2901 WARN_ON(desc->gdev->chip->can_sleep);
2690 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2902 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2691 value = !value; 2903 value = !value;
2692 _gpiod_set_raw_value(desc, value); 2904 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
2905 gpio_set_open_drain_value_commit(desc, value);
2906 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
2907 gpio_set_open_source_value_commit(desc, value);
2908 else
2909 gpiod_set_raw_value_commit(desc, value);
2693} 2910}
2694EXPORT_SYMBOL_GPL(gpiod_set_value); 2911EXPORT_SYMBOL_GPL(gpiod_set_value);
2695 2912
@@ -2890,7 +3107,7 @@ bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
2890 if (offset >= chip->ngpio) 3107 if (offset >= chip->ngpio)
2891 return false; 3108 return false;
2892 3109
2893 return !test_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, 3110 return !test_bit(FLAG_SLEEP_MAY_LOSE_VALUE,
2894 &chip->gpiodev->descs[offset].flags); 3111 &chip->gpiodev->descs[offset].flags);
2895} 3112}
2896EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); 3113EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
@@ -2908,7 +3125,7 @@ int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
2908{ 3125{
2909 might_sleep_if(extra_checks); 3126 might_sleep_if(extra_checks);
2910 VALIDATE_DESC(desc); 3127 VALIDATE_DESC(desc);
2911 return _gpiod_get_raw_value(desc); 3128 return gpiod_get_raw_value_commit(desc);
2912} 3129}
2913EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); 3130EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
2914 3131
@@ -2927,7 +3144,7 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2927 3144
2928 might_sleep_if(extra_checks); 3145 might_sleep_if(extra_checks);
2929 VALIDATE_DESC(desc); 3146 VALIDATE_DESC(desc);
2930 value = _gpiod_get_raw_value(desc); 3147 value = gpiod_get_raw_value_commit(desc);
2931 if (value < 0) 3148 if (value < 0)
2932 return value; 3149 return value;
2933 3150
@@ -2939,6 +3156,53 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc)
2939EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); 3156EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
2940 3157
2941/** 3158/**
3159 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
3160 * @array_size: number of elements in the descriptor / value arrays
3161 * @desc_array: array of GPIO descriptors whose values will be read
3162 * @value_array: array to store the read values
3163 *
3164 * Read the raw values of the GPIOs, i.e. the values of the physical lines
3165 * without regard for their ACTIVE_LOW status. Return 0 in case of success,
3166 * else an error code.
3167 *
3168 * This function is to be called from contexts that can sleep.
3169 */
3170int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
3171 struct gpio_desc **desc_array,
3172 int *value_array)
3173{
3174 might_sleep_if(extra_checks);
3175 if (!desc_array)
3176 return -EINVAL;
3177 return gpiod_get_array_value_complex(true, true, array_size,
3178 desc_array, value_array);
3179}
3180EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
3181
3182/**
3183 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
3184 * @array_size: number of elements in the descriptor / value arrays
3185 * @desc_array: array of GPIO descriptors whose values will be read
3186 * @value_array: array to store the read values
3187 *
3188 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
3189 * into account. Return 0 in case of success, else an error code.
3190 *
3191 * This function is to be called from contexts that can sleep.
3192 */
3193int gpiod_get_array_value_cansleep(unsigned int array_size,
3194 struct gpio_desc **desc_array,
3195 int *value_array)
3196{
3197 might_sleep_if(extra_checks);
3198 if (!desc_array)
3199 return -EINVAL;
3200 return gpiod_get_array_value_complex(false, true, array_size,
3201 desc_array, value_array);
3202}
3203EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
3204
3205/**
2942 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value 3206 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
2943 * @desc: gpio whose value will be assigned 3207 * @desc: gpio whose value will be assigned
2944 * @value: value to assign 3208 * @value: value to assign
@@ -2952,7 +3216,7 @@ void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
2952{ 3216{
2953 might_sleep_if(extra_checks); 3217 might_sleep_if(extra_checks);
2954 VALIDATE_DESC_VOID(desc); 3218 VALIDATE_DESC_VOID(desc);
2955 _gpiod_set_raw_value(desc, value); 3219 gpiod_set_raw_value_commit(desc, value);
2956} 3220}
2957EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); 3221EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
2958 3222
@@ -2972,7 +3236,7 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2972 VALIDATE_DESC_VOID(desc); 3236 VALIDATE_DESC_VOID(desc);
2973 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3237 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
2974 value = !value; 3238 value = !value;
2975 _gpiod_set_raw_value(desc, value); 3239 gpiod_set_raw_value_commit(desc, value);
2976} 3240}
2977EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 3241EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2978 3242
@@ -3268,8 +3532,8 @@ int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
3268 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 3532 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
3269 if (lflags & GPIO_OPEN_SOURCE) 3533 if (lflags & GPIO_OPEN_SOURCE)
3270 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 3534 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
3271 if (lflags & GPIO_SLEEP_MAY_LOOSE_VALUE) 3535 if (lflags & GPIO_SLEEP_MAY_LOSE_VALUE)
3272 set_bit(FLAG_SLEEP_MAY_LOOSE_VALUE, &desc->flags); 3536 set_bit(FLAG_SLEEP_MAY_LOSE_VALUE, &desc->flags);
3273 3537
3274 /* No particular flag request, return here... */ 3538 /* No particular flag request, return here... */
3275 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { 3539 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
index d003ccb12781..af48322839c3 100644
--- a/drivers/gpio/gpiolib.h
+++ b/drivers/gpio/gpiolib.h
@@ -180,6 +180,10 @@ static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
180#endif 180#endif
181 181
182struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); 182struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
183int gpiod_get_array_value_complex(bool raw, bool can_sleep,
184 unsigned int array_size,
185 struct gpio_desc **desc_array,
186 int *value_array);
183void gpiod_set_array_value_complex(bool raw, bool can_sleep, 187void gpiod_set_array_value_complex(bool raw, bool can_sleep,
184 unsigned int array_size, 188 unsigned int array_size,
185 struct gpio_desc **desc_array, 189 struct gpio_desc **desc_array,
@@ -201,7 +205,7 @@ struct gpio_desc {
201#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 205#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
202#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 206#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
203#define FLAG_IS_HOGGED 11 /* GPIO is hogged */ 207#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
204#define FLAG_SLEEP_MAY_LOOSE_VALUE 12 /* GPIO may loose value in sleep */ 208#define FLAG_SLEEP_MAY_LOSE_VALUE 12 /* GPIO may lose value in sleep */
205 209
206 /* Connection label */ 210 /* Connection label */
207 const char *label; 211 const char *label;
diff --git a/drivers/md/dm-mpath.c b/drivers/md/dm-mpath.c
index 3f88c9d32f7e..204606bef9e2 100644
--- a/drivers/md/dm-mpath.c
+++ b/drivers/md/dm-mpath.c
@@ -641,14 +641,6 @@ static void process_queued_bios(struct work_struct *work)
641 blk_finish_plug(&plug); 641 blk_finish_plug(&plug);
642} 642}
643 643
644static void assign_bit(bool value, long nr, unsigned long *addr)
645{
646 if (value)
647 set_bit(nr, addr);
648 else
649 clear_bit(nr, addr);
650}
651
652/* 644/*
653 * If we run out of usable paths, should we queue I/O or error it? 645 * If we run out of usable paths, should we queue I/O or error it?
654 */ 646 */
@@ -658,11 +650,11 @@ static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
658 unsigned long flags; 650 unsigned long flags;
659 651
660 spin_lock_irqsave(&m->lock, flags); 652 spin_lock_irqsave(&m->lock, flags);
661 assign_bit((save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) || 653 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
662 (!save_old_value && queue_if_no_path), 654 (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
663 MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 655 (!save_old_value && queue_if_no_path));
664 assign_bit(queue_if_no_path || dm_noflush_suspending(m->ti), 656 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
665 MPATHF_QUEUE_IF_NO_PATH, &m->flags); 657 queue_if_no_path || dm_noflush_suspending(m->ti));
666 spin_unlock_irqrestore(&m->lock, flags); 658 spin_unlock_irqrestore(&m->lock, flags);
667 659
668 if (!queue_if_no_path) { 660 if (!queue_if_no_path) {
@@ -1588,8 +1580,8 @@ static void multipath_resume(struct dm_target *ti)
1588 unsigned long flags; 1580 unsigned long flags;
1589 1581
1590 spin_lock_irqsave(&m->lock, flags); 1582 spin_lock_irqsave(&m->lock, flags);
1591 assign_bit(test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags), 1583 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1592 MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1584 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
1593 spin_unlock_irqrestore(&m->lock, flags); 1585 spin_unlock_irqrestore(&m->lock, flags);
1594} 1586}
1595 1587
diff --git a/drivers/pinctrl/bcm/pinctrl-bcm2835.c b/drivers/pinctrl/bcm/pinctrl-bcm2835.c
index ff782445dfb7..785c366fd6d6 100644
--- a/drivers/pinctrl/bcm/pinctrl-bcm2835.c
+++ b/drivers/pinctrl/bcm/pinctrl-bcm2835.c
@@ -379,7 +379,7 @@ static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
379 events &= pc->enabled_irq_map[bank]; 379 events &= pc->enabled_irq_map[bank];
380 for_each_set_bit(offset, &events, 32) { 380 for_each_set_bit(offset, &events, 32) {
381 gpio = (32 * bank) + offset; 381 gpio = (32 * bank) + offset;
382 generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irqdomain, 382 generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irq.domain,
383 gpio)); 383 gpio));
384 } 384 }
385} 385}
@@ -661,7 +661,7 @@ static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
661 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset); 661 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
662 const char *fname = bcm2835_functions[fsel]; 662 const char *fname = bcm2835_functions[fsel];
663 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset); 663 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
664 int irq = irq_find_mapping(chip->irqdomain, offset); 664 int irq = irq_find_mapping(chip->irq.domain, offset);
665 665
666 seq_printf(s, "function %s in %s; irq %d (%s)", 666 seq_printf(s, "function %s in %s; irq %d (%s)",
667 fname, value ? "hi" : "lo", 667 fname, value ? "hi" : "lo",
diff --git a/drivers/pinctrl/bcm/pinctrl-iproc-gpio.c b/drivers/pinctrl/bcm/pinctrl-iproc-gpio.c
index 85a8c97d9dfe..b93f62dc8733 100644
--- a/drivers/pinctrl/bcm/pinctrl-iproc-gpio.c
+++ b/drivers/pinctrl/bcm/pinctrl-iproc-gpio.c
@@ -172,7 +172,7 @@ static void iproc_gpio_irq_handler(struct irq_desc *desc)
172 172
173 for_each_set_bit(bit, &val, NGPIOS_PER_BANK) { 173 for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
174 unsigned pin = NGPIOS_PER_BANK * i + bit; 174 unsigned pin = NGPIOS_PER_BANK * i + bit;
175 int child_irq = irq_find_mapping(gc->irqdomain, pin); 175 int child_irq = irq_find_mapping(gc->irq.domain, pin);
176 176
177 /* 177 /*
178 * Clear the interrupt before invoking the 178 * Clear the interrupt before invoking the
diff --git a/drivers/pinctrl/intel/pinctrl-baytrail.c b/drivers/pinctrl/intel/pinctrl-baytrail.c
index 0f3a02495aeb..9c1ca29c60b7 100644
--- a/drivers/pinctrl/intel/pinctrl-baytrail.c
+++ b/drivers/pinctrl/intel/pinctrl-baytrail.c
@@ -1627,7 +1627,7 @@ static void byt_gpio_irq_handler(struct irq_desc *desc)
1627 pending = readl(reg); 1627 pending = readl(reg);
1628 raw_spin_unlock(&vg->lock); 1628 raw_spin_unlock(&vg->lock);
1629 for_each_set_bit(pin, &pending, 32) { 1629 for_each_set_bit(pin, &pending, 32) {
1630 virq = irq_find_mapping(vg->chip.irqdomain, base + pin); 1630 virq = irq_find_mapping(vg->chip.irq.domain, base + pin);
1631 generic_handle_irq(virq); 1631 generic_handle_irq(virq);
1632 } 1632 }
1633 } 1633 }
@@ -1660,7 +1660,7 @@ static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
1660 1660
1661 value = readl(reg); 1661 value = readl(reg);
1662 if (value & BYT_DIRECT_IRQ_EN) { 1662 if (value & BYT_DIRECT_IRQ_EN) {
1663 clear_bit(i, gc->irq_valid_mask); 1663 clear_bit(i, gc->irq.valid_mask);
1664 dev_dbg(dev, "excluding GPIO %d from IRQ domain\n", i); 1664 dev_dbg(dev, "excluding GPIO %d from IRQ domain\n", i);
1665 } else if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i)) { 1665 } else if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i)) {
1666 byt_gpio_clear_triggering(vg, i); 1666 byt_gpio_clear_triggering(vg, i);
@@ -1703,7 +1703,7 @@ static int byt_gpio_probe(struct byt_gpio *vg)
1703 gc->can_sleep = false; 1703 gc->can_sleep = false;
1704 gc->parent = &vg->pdev->dev; 1704 gc->parent = &vg->pdev->dev;
1705 gc->ngpio = vg->soc_data->npins; 1705 gc->ngpio = vg->soc_data->npins;
1706 gc->irq_need_valid_mask = true; 1706 gc->irq.need_valid_mask = true;
1707 1707
1708#ifdef CONFIG_PM_SLEEP 1708#ifdef CONFIG_PM_SLEEP
1709 vg->saved_context = devm_kcalloc(&vg->pdev->dev, gc->ngpio, 1709 vg->saved_context = devm_kcalloc(&vg->pdev->dev, gc->ngpio,
diff --git a/drivers/pinctrl/intel/pinctrl-cherryview.c b/drivers/pinctrl/intel/pinctrl-cherryview.c
index fadbca907c7c..bc2bb6b328ab 100644
--- a/drivers/pinctrl/intel/pinctrl-cherryview.c
+++ b/drivers/pinctrl/intel/pinctrl-cherryview.c
@@ -1523,7 +1523,7 @@ static void chv_gpio_irq_handler(struct irq_desc *desc)
1523 unsigned irq, offset; 1523 unsigned irq, offset;
1524 1524
1525 offset = pctrl->intr_lines[intr_line]; 1525 offset = pctrl->intr_lines[intr_line];
1526 irq = irq_find_mapping(gc->irqdomain, offset); 1526 irq = irq_find_mapping(gc->irq.domain, offset);
1527 generic_handle_irq(irq); 1527 generic_handle_irq(irq);
1528 } 1528 }
1529 1529
@@ -1585,7 +1585,7 @@ static int chv_gpio_probe(struct chv_pinctrl *pctrl, int irq)
1585 chip->label = dev_name(pctrl->dev); 1585 chip->label = dev_name(pctrl->dev);
1586 chip->parent = pctrl->dev; 1586 chip->parent = pctrl->dev;
1587 chip->base = -1; 1587 chip->base = -1;
1588 chip->irq_need_valid_mask = need_valid_mask; 1588 chip->irq.need_valid_mask = need_valid_mask;
1589 1589
1590 ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl); 1590 ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
1591 if (ret) { 1591 if (ret) {
@@ -1617,7 +1617,7 @@ static int chv_gpio_probe(struct chv_pinctrl *pctrl, int irq)
1617 intsel >>= CHV_PADCTRL0_INTSEL_SHIFT; 1617 intsel >>= CHV_PADCTRL0_INTSEL_SHIFT;
1618 1618
1619 if (need_valid_mask && intsel >= pctrl->community->nirqs) 1619 if (need_valid_mask && intsel >= pctrl->community->nirqs)
1620 clear_bit(i, chip->irq_valid_mask); 1620 clear_bit(i, chip->irq.valid_mask);
1621 } 1621 }
1622 1622
1623 /* Clear all interrupts */ 1623 /* Clear all interrupts */
diff --git a/drivers/pinctrl/intel/pinctrl-intel.c b/drivers/pinctrl/intel/pinctrl-intel.c
index 71df0f70b61f..ffda27bfd133 100644
--- a/drivers/pinctrl/intel/pinctrl-intel.c
+++ b/drivers/pinctrl/intel/pinctrl-intel.c
@@ -1005,7 +1005,7 @@ static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1005 if (padno >= community->npins) 1005 if (padno >= community->npins)
1006 break; 1006 break;
1007 1007
1008 irq = irq_find_mapping(gc->irqdomain, 1008 irq = irq_find_mapping(gc->irq.domain,
1009 community->pin_base + padno); 1009 community->pin_base + padno);
1010 generic_handle_irq(irq); 1010 generic_handle_irq(irq);
1011 1011
diff --git a/drivers/pinctrl/mvebu/pinctrl-armada-37xx.c b/drivers/pinctrl/mvebu/pinctrl-armada-37xx.c
index 71b944748304..d754a9b10e19 100644
--- a/drivers/pinctrl/mvebu/pinctrl-armada-37xx.c
+++ b/drivers/pinctrl/mvebu/pinctrl-armada-37xx.c
@@ -592,7 +592,7 @@ static void armada_37xx_irq_handler(struct irq_desc *desc)
592 struct gpio_chip *gc = irq_desc_get_handler_data(desc); 592 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
593 struct irq_chip *chip = irq_desc_get_chip(desc); 593 struct irq_chip *chip = irq_desc_get_chip(desc);
594 struct armada_37xx_pinctrl *info = gpiochip_get_data(gc); 594 struct armada_37xx_pinctrl *info = gpiochip_get_data(gc);
595 struct irq_domain *d = gc->irqdomain; 595 struct irq_domain *d = gc->irq.domain;
596 int i; 596 int i;
597 597
598 chained_irq_enter(chip, desc); 598 chained_irq_enter(chip, desc);
@@ -626,15 +626,13 @@ static void armada_37xx_irq_handler(struct irq_desc *desc)
626 626
627static unsigned int armada_37xx_irq_startup(struct irq_data *d) 627static unsigned int armada_37xx_irq_startup(struct irq_data *d)
628{ 628{
629 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
630 int irq = d->hwirq - chip->irq_base;
631 /* 629 /*
632 * The mask field is a "precomputed bitmask for accessing the 630 * The mask field is a "precomputed bitmask for accessing the
633 * chip registers" which was introduced for the generic 631 * chip registers" which was introduced for the generic
634 * irqchip framework. As we don't use this framework, we can 632 * irqchip framework. As we don't use this framework, we can
635 * reuse this field for our own usage. 633 * reuse this field for our own usage.
636 */ 634 */
637 d->mask = BIT(irq % GPIO_PER_REG); 635 d->mask = BIT(d->hwirq % GPIO_PER_REG);
638 636
639 armada_37xx_irq_unmask(d); 637 armada_37xx_irq_unmask(d);
640 638
diff --git a/drivers/pinctrl/nomadik/pinctrl-nomadik.c b/drivers/pinctrl/nomadik/pinctrl-nomadik.c
index a53f1a9b1ed2..f0e7a8c114b2 100644
--- a/drivers/pinctrl/nomadik/pinctrl-nomadik.c
+++ b/drivers/pinctrl/nomadik/pinctrl-nomadik.c
@@ -413,7 +413,7 @@ nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
413 u32 falling = nmk_chip->fimsc & BIT(offset); 413 u32 falling = nmk_chip->fimsc & BIT(offset);
414 u32 rising = nmk_chip->rimsc & BIT(offset); 414 u32 rising = nmk_chip->rimsc & BIT(offset);
415 int gpio = nmk_chip->chip.base + offset; 415 int gpio = nmk_chip->chip.base + offset;
416 int irq = irq_find_mapping(nmk_chip->chip.irqdomain, offset); 416 int irq = irq_find_mapping(nmk_chip->chip.irq.domain, offset);
417 struct irq_data *d = irq_get_irq_data(irq); 417 struct irq_data *d = irq_get_irq_data(irq);
418 418
419 if (!rising && !falling) 419 if (!rising && !falling)
@@ -815,7 +815,7 @@ static void __nmk_gpio_irq_handler(struct irq_desc *desc, u32 status)
815 while (status) { 815 while (status) {
816 int bit = __ffs(status); 816 int bit = __ffs(status);
817 817
818 generic_handle_irq(irq_find_mapping(chip->irqdomain, bit)); 818 generic_handle_irq(irq_find_mapping(chip->irq.domain, bit));
819 status &= ~BIT(bit); 819 status &= ~BIT(bit);
820 } 820 }
821 821
diff --git a/drivers/pinctrl/pinctrl-amd.c b/drivers/pinctrl/pinctrl-amd.c
index 433af328d981..8ed2a90a8d44 100644
--- a/drivers/pinctrl/pinctrl-amd.c
+++ b/drivers/pinctrl/pinctrl-amd.c
@@ -532,7 +532,7 @@ static irqreturn_t amd_gpio_irq_handler(int irq, void *dev_id)
532 regval = readl(regs + i); 532 regval = readl(regs + i);
533 if (!(regval & PIN_IRQ_PENDING)) 533 if (!(regval & PIN_IRQ_PENDING))
534 continue; 534 continue;
535 irq = irq_find_mapping(gc->irqdomain, irqnr + i); 535 irq = irq_find_mapping(gc->irq.domain, irqnr + i);
536 generic_handle_irq(irq); 536 generic_handle_irq(irq);
537 537
538 /* Clear interrupt. 538 /* Clear interrupt.
diff --git a/drivers/pinctrl/pinctrl-at91.c b/drivers/pinctrl/pinctrl-at91.c
index 569bc28cb909..03492e3c09fa 100644
--- a/drivers/pinctrl/pinctrl-at91.c
+++ b/drivers/pinctrl/pinctrl-at91.c
@@ -1603,7 +1603,7 @@ static void gpio_irq_handler(struct irq_desc *desc)
1603 1603
1604 for_each_set_bit(n, &isr, BITS_PER_LONG) { 1604 for_each_set_bit(n, &isr, BITS_PER_LONG) {
1605 generic_handle_irq(irq_find_mapping( 1605 generic_handle_irq(irq_find_mapping(
1606 gpio_chip->irqdomain, n)); 1606 gpio_chip->irq.domain, n));
1607 } 1607 }
1608 } 1608 }
1609 chained_irq_exit(chip, desc); 1609 chained_irq_exit(chip, desc);
diff --git a/drivers/pinctrl/pinctrl-coh901.c b/drivers/pinctrl/pinctrl-coh901.c
index ac155e7d3412..7939b178c6ae 100644
--- a/drivers/pinctrl/pinctrl-coh901.c
+++ b/drivers/pinctrl/pinctrl-coh901.c
@@ -517,7 +517,7 @@ static void u300_gpio_irq_handler(struct irq_desc *desc)
517 517
518 for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) { 518 for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
519 int offset = pinoffset + irqoffset; 519 int offset = pinoffset + irqoffset;
520 int pin_irq = irq_find_mapping(chip->irqdomain, offset); 520 int pin_irq = irq_find_mapping(chip->irq.domain, offset);
521 521
522 dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n", 522 dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
523 pin_irq, offset); 523 pin_irq, offset);
diff --git a/drivers/pinctrl/pinctrl-mcp23s08.c b/drivers/pinctrl/pinctrl-mcp23s08.c
index 9c950bbf07ba..59c899c205fb 100644
--- a/drivers/pinctrl/pinctrl-mcp23s08.c
+++ b/drivers/pinctrl/pinctrl-mcp23s08.c
@@ -537,7 +537,7 @@ static irqreturn_t mcp23s08_irq(int irq, void *data)
537 ((gpio_bit_changed || intcap_changed) && 537 ((gpio_bit_changed || intcap_changed) &&
538 (BIT(i) & mcp->irq_fall) && !gpio_set) || 538 (BIT(i) & mcp->irq_fall) && !gpio_set) ||
539 defval_changed) { 539 defval_changed) {
540 child_irq = irq_find_mapping(mcp->chip.irqdomain, i); 540 child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
541 handle_nested_irq(child_irq); 541 handle_nested_irq(child_irq);
542 } 542 }
543 } 543 }
diff --git a/drivers/pinctrl/pinctrl-oxnas.c b/drivers/pinctrl/pinctrl-oxnas.c
index 494ec9a7573a..53ec22a51f5c 100644
--- a/drivers/pinctrl/pinctrl-oxnas.c
+++ b/drivers/pinctrl/pinctrl-oxnas.c
@@ -1064,7 +1064,7 @@ static void oxnas_gpio_irq_handler(struct irq_desc *desc)
1064 stat = readl(bank->reg_base + IRQ_PENDING); 1064 stat = readl(bank->reg_base + IRQ_PENDING);
1065 1065
1066 for_each_set_bit(pin, &stat, BITS_PER_LONG) 1066 for_each_set_bit(pin, &stat, BITS_PER_LONG)
1067 generic_handle_irq(irq_linear_revmap(gc->irqdomain, pin)); 1067 generic_handle_irq(irq_linear_revmap(gc->irq.domain, pin));
1068 1068
1069 chained_irq_exit(chip, desc); 1069 chained_irq_exit(chip, desc);
1070} 1070}
diff --git a/drivers/pinctrl/pinctrl-pic32.c b/drivers/pinctrl/pinctrl-pic32.c
index 31ceb958b3fe..96390228d388 100644
--- a/drivers/pinctrl/pinctrl-pic32.c
+++ b/drivers/pinctrl/pinctrl-pic32.c
@@ -2106,7 +2106,7 @@ static void pic32_gpio_irq_handler(struct irq_desc *desc)
2106 pending = pic32_gpio_get_pending(gc, stat); 2106 pending = pic32_gpio_get_pending(gc, stat);
2107 2107
2108 for_each_set_bit(pin, &pending, BITS_PER_LONG) 2108 for_each_set_bit(pin, &pending, BITS_PER_LONG)
2109 generic_handle_irq(irq_linear_revmap(gc->irqdomain, pin)); 2109 generic_handle_irq(irq_linear_revmap(gc->irq.domain, pin));
2110 2110
2111 chained_irq_exit(chip, desc); 2111 chained_irq_exit(chip, desc);
2112} 2112}
diff --git a/drivers/pinctrl/pinctrl-pistachio.c b/drivers/pinctrl/pinctrl-pistachio.c
index 55375b1b3cc8..302190d1558d 100644
--- a/drivers/pinctrl/pinctrl-pistachio.c
+++ b/drivers/pinctrl/pinctrl-pistachio.c
@@ -1307,7 +1307,7 @@ static void pistachio_gpio_irq_handler(struct irq_desc *desc)
1307 pending = gpio_readl(bank, GPIO_INTERRUPT_STATUS) & 1307 pending = gpio_readl(bank, GPIO_INTERRUPT_STATUS) &
1308 gpio_readl(bank, GPIO_INTERRUPT_EN); 1308 gpio_readl(bank, GPIO_INTERRUPT_EN);
1309 for_each_set_bit(pin, &pending, 16) 1309 for_each_set_bit(pin, &pending, 16)
1310 generic_handle_irq(irq_linear_revmap(gc->irqdomain, pin)); 1310 generic_handle_irq(irq_linear_revmap(gc->irq.domain, pin));
1311 chained_irq_exit(chip, desc); 1311 chained_irq_exit(chip, desc);
1312} 1312}
1313 1313
diff --git a/drivers/pinctrl/pinctrl-st.c b/drivers/pinctrl/pinctrl-st.c
index a5205b94b2e6..2081c67667a8 100644
--- a/drivers/pinctrl/pinctrl-st.c
+++ b/drivers/pinctrl/pinctrl-st.c
@@ -1408,7 +1408,7 @@ static void __gpio_irq_handler(struct st_gpio_bank *bank)
1408 continue; 1408 continue;
1409 } 1409 }
1410 1410
1411 generic_handle_irq(irq_find_mapping(bank->gpio_chip.irqdomain, n)); 1411 generic_handle_irq(irq_find_mapping(bank->gpio_chip.irq.domain, n));
1412 } 1412 }
1413 } 1413 }
1414} 1414}
diff --git a/drivers/pinctrl/pinctrl-sx150x.c b/drivers/pinctrl/pinctrl-sx150x.c
index 7450f5118445..7db4f6a6eb17 100644
--- a/drivers/pinctrl/pinctrl-sx150x.c
+++ b/drivers/pinctrl/pinctrl-sx150x.c
@@ -561,7 +561,7 @@ static irqreturn_t sx150x_irq_thread_fn(int irq, void *dev_id)
561 561
562 status = val; 562 status = val;
563 for_each_set_bit(n, &status, pctl->data->ngpios) 563 for_each_set_bit(n, &status, pctl->data->ngpios)
564 handle_nested_irq(irq_find_mapping(pctl->gpio.irqdomain, n)); 564 handle_nested_irq(irq_find_mapping(pctl->gpio.irq.domain, n));
565 565
566 return IRQ_HANDLED; 566 return IRQ_HANDLED;
567} 567}
diff --git a/drivers/pinctrl/qcom/pinctrl-msm.c b/drivers/pinctrl/qcom/pinctrl-msm.c
index ff491da64dab..7a960590ecaa 100644
--- a/drivers/pinctrl/qcom/pinctrl-msm.c
+++ b/drivers/pinctrl/qcom/pinctrl-msm.c
@@ -795,7 +795,7 @@ static void msm_gpio_irq_handler(struct irq_desc *desc)
795 g = &pctrl->soc->groups[i]; 795 g = &pctrl->soc->groups[i];
796 val = readl(pctrl->regs + g->intr_status_reg); 796 val = readl(pctrl->regs + g->intr_status_reg);
797 if (val & BIT(g->intr_status_bit)) { 797 if (val & BIT(g->intr_status_bit)) {
798 irq_pin = irq_find_mapping(gc->irqdomain, i); 798 irq_pin = irq_find_mapping(gc->irq.domain, i);
799 generic_handle_irq(irq_pin); 799 generic_handle_irq(irq_pin);
800 handled++; 800 handled++;
801 } 801 }
diff --git a/drivers/pinctrl/sirf/pinctrl-atlas7.c b/drivers/pinctrl/sirf/pinctrl-atlas7.c
index 4db9323251e3..f5cef6e5fa3e 100644
--- a/drivers/pinctrl/sirf/pinctrl-atlas7.c
+++ b/drivers/pinctrl/sirf/pinctrl-atlas7.c
@@ -5820,7 +5820,7 @@ static void atlas7_gpio_handle_irq(struct irq_desc *desc)
5820 __func__, gc->label, 5820 __func__, gc->label,
5821 bank->gpio_offset + pin_in_bank); 5821 bank->gpio_offset + pin_in_bank);
5822 generic_handle_irq( 5822 generic_handle_irq(
5823 irq_find_mapping(gc->irqdomain, 5823 irq_find_mapping(gc->irq.domain,
5824 bank->gpio_offset + pin_in_bank)); 5824 bank->gpio_offset + pin_in_bank));
5825 } 5825 }
5826 5826
diff --git a/drivers/pinctrl/sirf/pinctrl-sirf.c b/drivers/pinctrl/sirf/pinctrl-sirf.c
index d3ef05973901..8b14a1f1e671 100644
--- a/drivers/pinctrl/sirf/pinctrl-sirf.c
+++ b/drivers/pinctrl/sirf/pinctrl-sirf.c
@@ -587,7 +587,7 @@ static void sirfsoc_gpio_handle_irq(struct irq_desc *desc)
587 if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) { 587 if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) {
588 pr_debug("%s: gpio id %d idx %d happens\n", 588 pr_debug("%s: gpio id %d idx %d happens\n",
589 __func__, bank->id, idx); 589 __func__, bank->id, idx);
590 generic_handle_irq(irq_find_mapping(gc->irqdomain, idx + 590 generic_handle_irq(irq_find_mapping(gc->irq.domain, idx +
591 bank->id * SIRFSOC_GPIO_BANK_SIZE)); 591 bank->id * SIRFSOC_GPIO_BANK_SIZE));
592 } 592 }
593 593
diff --git a/drivers/pinctrl/spear/pinctrl-plgpio.c b/drivers/pinctrl/spear/pinctrl-plgpio.c
index cf6d68c7345b..72ae6bccee55 100644
--- a/drivers/pinctrl/spear/pinctrl-plgpio.c
+++ b/drivers/pinctrl/spear/pinctrl-plgpio.c
@@ -401,7 +401,7 @@ static void plgpio_irq_handler(struct irq_desc *desc)
401 /* get correct irq line number */ 401 /* get correct irq line number */
402 pin = i * MAX_GPIO_PER_REG + pin; 402 pin = i * MAX_GPIO_PER_REG + pin;
403 generic_handle_irq( 403 generic_handle_irq(
404 irq_find_mapping(gc->irqdomain, pin)); 404 irq_find_mapping(gc->irq.domain, pin));
405 } 405 }
406 } 406 }
407 chained_irq_exit(irqchip, desc); 407 chained_irq_exit(irqchip, desc);
diff --git a/drivers/platform/x86/intel_int0002_vgpio.c b/drivers/platform/x86/intel_int0002_vgpio.c
index 92dc230ef5b2..f7b67e898abc 100644
--- a/drivers/platform/x86/intel_int0002_vgpio.c
+++ b/drivers/platform/x86/intel_int0002_vgpio.c
@@ -119,7 +119,7 @@ static irqreturn_t int0002_irq(int irq, void *data)
119 if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT)) 119 if (!(gpe_sts_reg & GPE0A_PME_B0_STS_BIT))
120 return IRQ_NONE; 120 return IRQ_NONE;
121 121
122 generic_handle_irq(irq_find_mapping(chip->irqdomain, 122 generic_handle_irq(irq_find_mapping(chip->irq.domain,
123 GPE0A_PME_B0_VIRT_GPIO_PIN)); 123 GPE0A_PME_B0_VIRT_GPIO_PIN));
124 124
125 pm_system_wakeup(); 125 pm_system_wakeup();
@@ -165,7 +165,7 @@ static int int0002_probe(struct platform_device *pdev)
165 chip->direction_output = int0002_gpio_direction_output; 165 chip->direction_output = int0002_gpio_direction_output;
166 chip->base = -1; 166 chip->base = -1;
167 chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1; 167 chip->ngpio = GPE0A_PME_B0_VIRT_GPIO_PIN + 1;
168 chip->irq_need_valid_mask = true; 168 chip->irq.need_valid_mask = true;
169 169
170 ret = devm_gpiochip_add_data(&pdev->dev, chip, NULL); 170 ret = devm_gpiochip_add_data(&pdev->dev, chip, NULL);
171 if (ret) { 171 if (ret) {
@@ -173,7 +173,7 @@ static int int0002_probe(struct platform_device *pdev)
173 return ret; 173 return ret;
174 } 174 }
175 175
176 bitmap_clear(chip->irq_valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN); 176 bitmap_clear(chip->irq.valid_mask, 0, GPE0A_PME_B0_VIRT_GPIO_PIN);
177 177
178 /* 178 /*
179 * We manually request the irq here instead of passing a flow-handler 179 * We manually request the irq here instead of passing a flow-handler
diff --git a/include/dt-bindings/gpio/gpio.h b/include/dt-bindings/gpio/gpio.h
index 32dd58a3d13c..dd549ff04295 100644
--- a/include/dt-bindings/gpio/gpio.h
+++ b/include/dt-bindings/gpio/gpio.h
@@ -31,6 +31,6 @@
31 31
32/* Bit 3 express GPIO suspend/resume persistence */ 32/* Bit 3 express GPIO suspend/resume persistence */
33#define GPIO_SLEEP_MAINTAIN_VALUE 0 33#define GPIO_SLEEP_MAINTAIN_VALUE 0
34#define GPIO_SLEEP_MAY_LOOSE_VALUE 8 34#define GPIO_SLEEP_MAY_LOSE_VALUE 8
35 35
36#endif 36#endif
diff --git a/include/dt-bindings/gpio/uniphier-gpio.h b/include/dt-bindings/gpio/uniphier-gpio.h
new file mode 100644
index 000000000000..9f0ad174f61c
--- /dev/null
+++ b/include/dt-bindings/gpio/uniphier-gpio.h
@@ -0,0 +1,18 @@
1/*
2 * Copyright (C) 2017 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
4 */
5
6#ifndef _DT_BINDINGS_GPIO_UNIPHIER_H
7#define _DT_BINDINGS_GPIO_UNIPHIER_H
8
9#define UNIPHIER_GPIO_LINES_PER_BANK 8
10
11#define UNIPHIER_GPIO_IRQ_OFFSET ((UNIPHIER_GPIO_LINES_PER_BANK) * 15)
12
13#define UNIPHIER_GPIO_PORT(bank, line) \
14 ((UNIPHIER_GPIO_LINES_PER_BANK) * (bank) + (line))
15
16#define UNIPHIER_GPIO_IRQ(n) ((UNIPHIER_GPIO_IRQ_OFFSET) + (n))
17
18#endif /* _DT_BINDINGS_GPIO_UNIPHIER_H */
diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index c537ac7435ad..4cac4e1a72ff 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -228,6 +228,30 @@ static inline unsigned long __ffs64(u64 word)
228 return __ffs((unsigned long)word); 228 return __ffs((unsigned long)word);
229} 229}
230 230
231/**
232 * assign_bit - Assign value to a bit in memory
233 * @nr: the bit to set
234 * @addr: the address to start counting from
235 * @value: the value to assign
236 */
237static __always_inline void assign_bit(long nr, volatile unsigned long *addr,
238 bool value)
239{
240 if (value)
241 set_bit(nr, addr);
242 else
243 clear_bit(nr, addr);
244}
245
246static __always_inline void __assign_bit(long nr, volatile unsigned long *addr,
247 bool value)
248{
249 if (value)
250 __set_bit(nr, addr);
251 else
252 __clear_bit(nr, addr);
253}
254
231#ifdef __KERNEL__ 255#ifdef __KERNEL__
232 256
233#ifndef set_mask_bits 257#ifndef set_mask_bits
diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index c4a350d83578..6d236656aaef 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -100,10 +100,15 @@ int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
100 100
101/* Value get/set from non-sleeping context */ 101/* Value get/set from non-sleeping context */
102int gpiod_get_value(const struct gpio_desc *desc); 102int gpiod_get_value(const struct gpio_desc *desc);
103int gpiod_get_array_value(unsigned int array_size,
104 struct gpio_desc **desc_array, int *value_array);
103void gpiod_set_value(struct gpio_desc *desc, int value); 105void gpiod_set_value(struct gpio_desc *desc, int value);
104void gpiod_set_array_value(unsigned int array_size, 106void gpiod_set_array_value(unsigned int array_size,
105 struct gpio_desc **desc_array, int *value_array); 107 struct gpio_desc **desc_array, int *value_array);
106int gpiod_get_raw_value(const struct gpio_desc *desc); 108int gpiod_get_raw_value(const struct gpio_desc *desc);
109int gpiod_get_raw_array_value(unsigned int array_size,
110 struct gpio_desc **desc_array,
111 int *value_array);
107void gpiod_set_raw_value(struct gpio_desc *desc, int value); 112void gpiod_set_raw_value(struct gpio_desc *desc, int value);
108void gpiod_set_raw_array_value(unsigned int array_size, 113void gpiod_set_raw_array_value(unsigned int array_size,
109 struct gpio_desc **desc_array, 114 struct gpio_desc **desc_array,
@@ -111,11 +116,17 @@ void gpiod_set_raw_array_value(unsigned int array_size,
111 116
112/* Value get/set from sleeping context */ 117/* Value get/set from sleeping context */
113int gpiod_get_value_cansleep(const struct gpio_desc *desc); 118int gpiod_get_value_cansleep(const struct gpio_desc *desc);
119int gpiod_get_array_value_cansleep(unsigned int array_size,
120 struct gpio_desc **desc_array,
121 int *value_array);
114void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 122void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
115void gpiod_set_array_value_cansleep(unsigned int array_size, 123void gpiod_set_array_value_cansleep(unsigned int array_size,
116 struct gpio_desc **desc_array, 124 struct gpio_desc **desc_array,
117 int *value_array); 125 int *value_array);
118int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 126int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
127int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
128 struct gpio_desc **desc_array,
129 int *value_array);
119void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 130void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
120void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 131void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
121 struct gpio_desc **desc_array, 132 struct gpio_desc **desc_array,
@@ -306,6 +317,14 @@ static inline int gpiod_get_value(const struct gpio_desc *desc)
306 WARN_ON(1); 317 WARN_ON(1);
307 return 0; 318 return 0;
308} 319}
320static inline int gpiod_get_array_value(unsigned int array_size,
321 struct gpio_desc **desc_array,
322 int *value_array)
323{
324 /* GPIO can never have been requested */
325 WARN_ON(1);
326 return 0;
327}
309static inline void gpiod_set_value(struct gpio_desc *desc, int value) 328static inline void gpiod_set_value(struct gpio_desc *desc, int value)
310{ 329{
311 /* GPIO can never have been requested */ 330 /* GPIO can never have been requested */
@@ -324,6 +343,14 @@ static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
324 WARN_ON(1); 343 WARN_ON(1);
325 return 0; 344 return 0;
326} 345}
346static inline int gpiod_get_raw_array_value(unsigned int array_size,
347 struct gpio_desc **desc_array,
348 int *value_array)
349{
350 /* GPIO can never have been requested */
351 WARN_ON(1);
352 return 0;
353}
327static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 354static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
328{ 355{
329 /* GPIO can never have been requested */ 356 /* GPIO can never have been requested */
@@ -343,6 +370,14 @@ static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
343 WARN_ON(1); 370 WARN_ON(1);
344 return 0; 371 return 0;
345} 372}
373static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
374 struct gpio_desc **desc_array,
375 int *value_array)
376{
377 /* GPIO can never have been requested */
378 WARN_ON(1);
379 return 0;
380}
346static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 381static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
347{ 382{
348 /* GPIO can never have been requested */ 383 /* GPIO can never have been requested */
@@ -361,6 +396,14 @@ static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
361 WARN_ON(1); 396 WARN_ON(1);
362 return 0; 397 return 0;
363} 398}
399static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
400 struct gpio_desc **desc_array,
401 int *value_array)
402{
403 /* GPIO can never have been requested */
404 WARN_ON(1);
405 return 0;
406}
364static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 407static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
365 int value) 408 int value)
366{ 409{
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 424e5139ff10..55e672592fa9 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -20,6 +20,131 @@ struct module;
20 20
21#ifdef CONFIG_GPIOLIB 21#ifdef CONFIG_GPIOLIB
22 22
23#ifdef CONFIG_GPIOLIB_IRQCHIP
24/**
25 * struct gpio_irq_chip - GPIO interrupt controller
26 */
27struct gpio_irq_chip {
28 /**
29 * @chip:
30 *
31 * GPIO IRQ chip implementation, provided by GPIO driver.
32 */
33 struct irq_chip *chip;
34
35 /**
36 * @domain:
37 *
38 * Interrupt translation domain; responsible for mapping between GPIO
39 * hwirq number and Linux IRQ number.
40 */
41 struct irq_domain *domain;
42
43 /**
44 * @domain_ops:
45 *
46 * Table of interrupt domain operations for this IRQ chip.
47 */
48 const struct irq_domain_ops *domain_ops;
49
50 /**
51 * @handler:
52 *
53 * The IRQ handler to use (often a predefined IRQ core function) for
54 * GPIO IRQs, provided by GPIO driver.
55 */
56 irq_flow_handler_t handler;
57
58 /**
59 * @default_type:
60 *
61 * Default IRQ triggering type applied during GPIO driver
62 * initialization, provided by GPIO driver.
63 */
64 unsigned int default_type;
65
66 /**
67 * @lock_key:
68 *
69 * Per GPIO IRQ chip lockdep class.
70 */
71 struct lock_class_key *lock_key;
72
73 /**
74 * @parent_handler:
75 *
76 * The interrupt handler for the GPIO chip's parent interrupts, may be
77 * NULL if the parent interrupts are nested rather than cascaded.
78 */
79 irq_flow_handler_t parent_handler;
80
81 /**
82 * @parent_handler_data:
83 *
84 * Data associated, and passed to, the handler for the parent
85 * interrupt.
86 */
87 void *parent_handler_data;
88
89 /**
90 * @num_parents:
91 *
92 * The number of interrupt parents of a GPIO chip.
93 */
94 unsigned int num_parents;
95
96 /**
97 * @parents:
98 *
99 * A list of interrupt parents of a GPIO chip. This is owned by the
100 * driver, so the core will only reference this list, not modify it.
101 */
102 unsigned int *parents;
103
104 /**
105 * @map:
106 *
107 * A list of interrupt parents for each line of a GPIO chip.
108 */
109 unsigned int *map;
110
111 /**
112 * @threaded:
113 *
114 * True if set the interrupt handling uses nested threads.
115 */
116 bool threaded;
117
118 /**
119 * @need_valid_mask:
120 *
121 * If set core allocates @valid_mask with all bits set to one.
122 */
123 bool need_valid_mask;
124
125 /**
126 * @valid_mask:
127 *
128 * If not %NULL holds bitmask of GPIOs which are valid to be included
129 * in IRQ domain of the chip.
130 */
131 unsigned long *valid_mask;
132
133 /**
134 * @first:
135 *
136 * Required for static IRQ allocation. If set, irq_domain_add_simple()
137 * will allocate and map all IRQs during initialization.
138 */
139 unsigned int first;
140};
141
142static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip)
143{
144 return container_of(chip, struct gpio_irq_chip, chip);
145}
146#endif
147
23/** 148/**
24 * struct gpio_chip - abstract a GPIO controller 149 * struct gpio_chip - abstract a GPIO controller
25 * @label: a functional name for the GPIO device, such as a part 150 * @label: a functional name for the GPIO device, such as a part
@@ -36,6 +161,8 @@ struct module;
36 * @direction_input: configures signal "offset" as input, or returns error 161 * @direction_input: configures signal "offset" as input, or returns error
37 * @direction_output: configures signal "offset" as output, or returns error 162 * @direction_output: configures signal "offset" as output, or returns error
38 * @get: returns value for signal "offset", 0=low, 1=high, or negative error 163 * @get: returns value for signal "offset", 0=low, 1=high, or negative error
164 * @get_multiple: reads values for multiple signals defined by "mask" and
165 * stores them in "bits", returns 0 on success or negative error
39 * @set: assigns output value for signal "offset" 166 * @set: assigns output value for signal "offset"
40 * @set_multiple: assigns output values for multiple signals defined by "mask" 167 * @set_multiple: assigns output values for multiple signals defined by "mask"
41 * @set_config: optional hook for all kinds of settings. Uses the same 168 * @set_config: optional hook for all kinds of settings. Uses the same
@@ -66,9 +193,9 @@ struct module;
66 * registers. 193 * registers.
67 * @read_reg: reader function for generic GPIO 194 * @read_reg: reader function for generic GPIO
68 * @write_reg: writer function for generic GPIO 195 * @write_reg: writer function for generic GPIO
69 * @pin2mask: some generic GPIO controllers work with the big-endian bits 196 * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
70 * notation, e.g. in a 8-bits register, GPIO7 is the least significant 197 * line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
71 * bit. This callback assigns the right bit mask. 198 * generic GPIO core. It is for internal housekeeping only.
72 * @reg_dat: data (in) register for generic GPIO 199 * @reg_dat: data (in) register for generic GPIO
73 * @reg_set: output set register (out=high) for generic GPIO 200 * @reg_set: output set register (out=high) for generic GPIO
74 * @reg_clr: output clear register (out=low) for generic GPIO 201 * @reg_clr: output clear register (out=low) for generic GPIO
@@ -81,23 +208,6 @@ struct module;
81 * safely. 208 * safely.
82 * @bgpio_dir: shadowed direction register for generic GPIO to clear/set 209 * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
83 * direction safely. 210 * direction safely.
84 * @irqchip: GPIO IRQ chip impl, provided by GPIO driver
85 * @irqdomain: Interrupt translation domain; responsible for mapping
86 * between GPIO hwirq number and linux irq number
87 * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated)
88 * @irq_handler: the irq handler to use (often a predefined irq core function)
89 * for GPIO IRQs, provided by GPIO driver
90 * @irq_default_type: default IRQ triggering type applied during GPIO driver
91 * initialization, provided by GPIO driver
92 * @irq_chained_parent: GPIO IRQ chip parent/bank linux irq number,
93 * provided by GPIO driver for chained interrupt (not for nested
94 * interrupts).
95 * @irq_nested: True if set the interrupt handling is nested.
96 * @irq_need_valid_mask: If set core allocates @irq_valid_mask with all
97 * bits set to one
98 * @irq_valid_mask: If not %NULL holds bitmask of GPIOs which are valid to
99 * be included in IRQ domain of the chip
100 * @lock_key: per GPIO IRQ chip lockdep class
101 * 211 *
102 * A gpio_chip can help platforms abstract various sources of GPIOs so 212 * A gpio_chip can help platforms abstract various sources of GPIOs so
103 * they can all be accessed through a common programing interface. 213 * they can all be accessed through a common programing interface.
@@ -127,6 +237,9 @@ struct gpio_chip {
127 unsigned offset, int value); 237 unsigned offset, int value);
128 int (*get)(struct gpio_chip *chip, 238 int (*get)(struct gpio_chip *chip,
129 unsigned offset); 239 unsigned offset);
240 int (*get_multiple)(struct gpio_chip *chip,
241 unsigned long *mask,
242 unsigned long *bits);
130 void (*set)(struct gpio_chip *chip, 243 void (*set)(struct gpio_chip *chip,
131 unsigned offset, int value); 244 unsigned offset, int value);
132 void (*set_multiple)(struct gpio_chip *chip, 245 void (*set_multiple)(struct gpio_chip *chip,
@@ -148,7 +261,7 @@ struct gpio_chip {
148#if IS_ENABLED(CONFIG_GPIO_GENERIC) 261#if IS_ENABLED(CONFIG_GPIO_GENERIC)
149 unsigned long (*read_reg)(void __iomem *reg); 262 unsigned long (*read_reg)(void __iomem *reg);
150 void (*write_reg)(void __iomem *reg, unsigned long data); 263 void (*write_reg)(void __iomem *reg, unsigned long data);
151 unsigned long (*pin2mask)(struct gpio_chip *gc, unsigned int pin); 264 bool be_bits;
152 void __iomem *reg_dat; 265 void __iomem *reg_dat;
153 void __iomem *reg_set; 266 void __iomem *reg_set;
154 void __iomem *reg_clr; 267 void __iomem *reg_clr;
@@ -164,16 +277,14 @@ struct gpio_chip {
164 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib 277 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
165 * to handle IRQs for most practical cases. 278 * to handle IRQs for most practical cases.
166 */ 279 */
167 struct irq_chip *irqchip; 280
168 struct irq_domain *irqdomain; 281 /**
169 unsigned int irq_base; 282 * @irq:
170 irq_flow_handler_t irq_handler; 283 *
171 unsigned int irq_default_type; 284 * Integrates interrupt chip functionality with the GPIO chip. Can be
172 unsigned int irq_chained_parent; 285 * used to handle IRQs for most practical cases.
173 bool irq_nested; 286 */
174 bool irq_need_valid_mask; 287 struct gpio_irq_chip irq;
175 unsigned long *irq_valid_mask;
176 struct lock_class_key *lock_key;
177#endif 288#endif
178 289
179#if defined(CONFIG_OF_GPIO) 290#if defined(CONFIG_OF_GPIO)
@@ -211,7 +322,41 @@ extern const char *gpiochip_is_requested(struct gpio_chip *chip,
211 unsigned offset); 322 unsigned offset);
212 323
213/* add/remove chips */ 324/* add/remove chips */
214extern int gpiochip_add_data(struct gpio_chip *chip, void *data); 325extern int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
326 struct lock_class_key *lock_key);
327
328/**
329 * gpiochip_add_data() - register a gpio_chip
330 * @chip: the chip to register, with chip->base initialized
331 * @data: driver-private data associated with this chip
332 *
333 * Context: potentially before irqs will work
334 *
335 * When gpiochip_add_data() is called very early during boot, so that GPIOs
336 * can be freely used, the chip->parent device must be registered before
337 * the gpio framework's arch_initcall(). Otherwise sysfs initialization
338 * for GPIOs will fail rudely.
339 *
340 * gpiochip_add_data() must only be called after gpiolib initialization,
341 * ie after core_initcall().
342 *
343 * If chip->base is negative, this requests dynamic assignment of
344 * a range of valid GPIOs.
345 *
346 * Returns:
347 * A negative errno if the chip can't be registered, such as because the
348 * chip->base is invalid or already associated with a different chip.
349 * Otherwise it returns zero as a success code.
350 */
351#ifdef CONFIG_LOCKDEP
352#define gpiochip_add_data(chip, data) ({ \
353 static struct lock_class_key key; \
354 gpiochip_add_data_with_key(chip, data, &key); \
355 })
356#else
357#define gpiochip_add_data(chip, data) gpiochip_add_data_with_key(chip, data, NULL)
358#endif
359
215static inline int gpiochip_add(struct gpio_chip *chip) 360static inline int gpiochip_add(struct gpio_chip *chip)
216{ 361{
217 return gpiochip_add_data(chip, NULL); 362 return gpiochip_add_data(chip, NULL);
@@ -265,6 +410,10 @@ int bgpio_init(struct gpio_chip *gc, struct device *dev,
265 410
266#ifdef CONFIG_GPIOLIB_IRQCHIP 411#ifdef CONFIG_GPIOLIB_IRQCHIP
267 412
413int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
414 irq_hw_number_t hwirq);
415void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
416
268void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, 417void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
269 struct irq_chip *irqchip, 418 struct irq_chip *irqchip,
270 unsigned int parent_irq, 419 unsigned int parent_irq,
@@ -279,7 +428,7 @@ int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
279 unsigned int first_irq, 428 unsigned int first_irq,
280 irq_flow_handler_t handler, 429 irq_flow_handler_t handler,
281 unsigned int type, 430 unsigned int type,
282 bool nested, 431 bool threaded,
283 struct lock_class_key *lock_key); 432 struct lock_class_key *lock_key);
284 433
285#ifdef CONFIG_LOCKDEP 434#ifdef CONFIG_LOCKDEP
diff --git a/include/linux/gpio/machine.h b/include/linux/gpio/machine.h
index b7225369e568..846be7c69a52 100644
--- a/include/linux/gpio/machine.h
+++ b/include/linux/gpio/machine.h
@@ -11,7 +11,7 @@ enum gpio_lookup_flags {
11 GPIO_OPEN_DRAIN = (1 << 1), 11 GPIO_OPEN_DRAIN = (1 << 1),
12 GPIO_OPEN_SOURCE = (1 << 2), 12 GPIO_OPEN_SOURCE = (1 << 2),
13 GPIO_SLEEP_MAINTAIN_VALUE = (0 << 3), 13 GPIO_SLEEP_MAINTAIN_VALUE = (0 << 3),
14 GPIO_SLEEP_MAY_LOOSE_VALUE = (1 << 3), 14 GPIO_SLEEP_MAY_LOSE_VALUE = (1 << 3),
15}; 15};
16 16
17/** 17/**
diff --git a/include/linux/of_gpio.h b/include/linux/of_gpio.h
index ca10f43564de..1fe205582111 100644
--- a/include/linux/of_gpio.h
+++ b/include/linux/of_gpio.h
@@ -31,7 +31,7 @@ enum of_gpio_flags {
31 OF_GPIO_ACTIVE_LOW = 0x1, 31 OF_GPIO_ACTIVE_LOW = 0x1,
32 OF_GPIO_SINGLE_ENDED = 0x2, 32 OF_GPIO_SINGLE_ENDED = 0x2,
33 OF_GPIO_OPEN_DRAIN = 0x4, 33 OF_GPIO_OPEN_DRAIN = 0x4,
34 OF_GPIO_SLEEP_MAY_LOOSE_VALUE = 0x8, 34 OF_GPIO_SLEEP_MAY_LOSE_VALUE = 0x8,
35}; 35};
36 36
37#ifdef CONFIG_OF_GPIO 37#ifdef CONFIG_OF_GPIO
diff --git a/tools/gpio/gpio-utils.c b/tools/gpio/gpio-utils.c
index b86a32d90d88..cf7e2f3419ee 100644
--- a/tools/gpio/gpio-utils.c
+++ b/tools/gpio/gpio-utils.c
@@ -76,7 +76,8 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
76 fd = open(chrdev_name, 0); 76 fd = open(chrdev_name, 0);
77 if (fd == -1) { 77 if (fd == -1) {
78 ret = -errno; 78 ret = -errno;
79 fprintf(stderr, "Failed to open %s\n", chrdev_name); 79 fprintf(stderr, "Failed to open %s, %s\n",
80 chrdev_name, strerror(errno));
80 goto exit_close_error; 81 goto exit_close_error;
81 } 82 }
82 83
@@ -92,8 +93,8 @@ int gpiotools_request_linehandle(const char *device_name, unsigned int *lines,
92 ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req); 93 ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
93 if (ret == -1) { 94 if (ret == -1) {
94 ret = -errno; 95 ret = -errno;
95 fprintf(stderr, "Failed to issue GET LINEHANDLE IOCTL (%d)\n", 96 fprintf(stderr, "Failed to issue %s (%d), %s\n",
96 ret); 97 "GPIO_GET_LINEHANDLE_IOCTL", ret, strerror(errno));
97 } 98 }
98 99
99exit_close_error: 100exit_close_error:
@@ -118,8 +119,9 @@ int gpiotools_set_values(const int fd, struct gpiohandle_data *data)
118 ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data); 119 ret = ioctl(fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, data);
119 if (ret == -1) { 120 if (ret == -1) {
120 ret = -errno; 121 ret = -errno;
121 fprintf(stderr, "Failed to issue %s (%d)\n", 122 fprintf(stderr, "Failed to issue %s (%d), %s\n",
122 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret); 123 "GPIOHANDLE_SET_LINE_VALUES_IOCTL", ret,
124 strerror(errno));
123 } 125 }
124 126
125 return ret; 127 return ret;
@@ -141,8 +143,9 @@ int gpiotools_get_values(const int fd, struct gpiohandle_data *data)
141 ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data); 143 ret = ioctl(fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, data);
142 if (ret == -1) { 144 if (ret == -1) {
143 ret = -errno; 145 ret = -errno;
144 fprintf(stderr, "Failed to issue %s (%d)\n", 146 fprintf(stderr, "Failed to issue %s (%d), %s\n",
145 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret); 147 "GPIOHANDLE_GET_LINE_VALUES_IOCTL", ret,
148 strerror(errno));
146 } 149 }
147 150
148 return ret; 151 return ret;