Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'gpio-updates-for-v6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux

Pull gpio updates from Bartosz Golaszewski:
"We have two new drivers, an assortment of updates and cleanups to many
others, and first part of the big rework of the core GPIOLIB that's
currently underway.

Add to that some code shrink in the character device module and
updates to DT bindings and that's pretty much it.

Core GPIOLIB:
- protect the global list of GPIO devices with a read-write semaphore
as it is rarely modified but can be traversed by multiple readers
at once
- remove GPIO devices from the global list when they are
*unregistered* and not when they are *released* (which only happens
when the last reference is dropped) as this may lead to a
successful lookup of an unregistered device
- remove the unnecessary "extra_checks" switch
- rename functions that are called with a lock taken
- remove duplicate includes

Character device handling:
- use locking guards to reduce the code size
- allocate the big linereq structure using the more suitable
kvzalloc()
- redulce the size of critical sections
- improve documentation
- move the debounce_period_us field out of struct gpio_desc

New drivers:
- Nuvoton NPCM SGPIO driver for BMC NPCM7xx/NPCM8xx
- Realtek DHC (Digital Home Center) SoC GPIO driver

Driver improvements:
- replace gpiochip_is_requested() with a safer alternative in the
form of gpiochip_dup_line_label() as the former returns a pointer
to a string that can be deleted
- implement the dbg_show() callback in gpio-sim
- improve the coding style for local variables by removing
unnecessary tabs
- use generic device properties instead of OF variants in gpio-mmio
- use the preferred coding style for __free() in gpio-mockup
- reuse PM ops from the gpio-tangier in gpio-elkhartlake
- rework PM and use cleanup helpers in gpio-tangier
- fix the EIC configuration in gpio-pmic-eic-sprd
- remove the unneeded call to platform_set_drvdata() in gpio-sifive
- use generic GPIO helpers for driver callbacks in gpio-dwapb
- add clock support on certain pins of gpio-ixp4xx
- don't use the core-specific DEBUG_GPIO switch in drivers
- kerneldoc improvements

DT bindings:
- add bindings for the new Realtek and Nuvoton devices
- allow gpio-ranges in gpio-dwapb
- support GPIO hogs in gpio-rockchip
- describe the label property in gpio-zynqmp-modepin

Other:
- header cleanups
- forward declarations cleanups"

* tag 'gpio-updates-for-v6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux: (55 commits)
gpiolib: replace the GPIO device mutex with a read-write semaphore
gpiolib: remove the GPIO device from the list when it's unregistered
gpio: nuvoton: Add Nuvoton NPCM sgpio driver
dt-bindings: gpio: add NPCM sgpio driver bindings
gpio: rtd: Add support for Realtek DHC(Digital Home Center) RTD SoCs
dt-bindings: gpio: realtek: Add realtek,rtd-gpio
gpio: pmic-eic-sprd: Configure the bit corresponding to the EIC through offset
gpio: dwapb: Use generic request, free and set_config
gpio: sysfs: drop tabs from local variable declarations
gpiolib: drop tabs from local variable declarations
gpiolib: remove extra_checks
gpio: tps65219: don't use CONFIG_DEBUG_GPIO
gpiolib: cdev: replace locking wrappers for gpio_device with guards
gpiolib: cdev: replace locking wrappers for config_mutex with guards
gpiolib: cdev: allocate linereq using kvzalloc()
gpiolib: cdev: include overflow.h
gpiolib: cdev: reduce locking in gpio_desc_to_lineinfo()
gpiolib: cdev: improve documentation of get/set values
gpiolib: cdev: fully adopt guard() and scoped_guard()
gpiolib: remove debounce_period_us from struct gpio_desc
...

+2185 -702
+1 -1
Documentation/devicetree/bindings/gpio/brcm,brcmstb-gpio.yaml
··· 72 72 - reg 73 73 - gpio-controller 74 74 - "#gpio-cells" 75 - - "brcm,gpio-bank-widths" 75 + - brcm,gpio-bank-widths 76 76 77 77 additionalProperties: false 78 78
+87
Documentation/devicetree/bindings/gpio/nuvoton,sgpio.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/gpio/nuvoton,sgpio.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Nuvoton SGPIO controller 8 + 9 + maintainers: 10 + - Jim LIU <JJLIU0@nuvoton.com> 11 + 12 + description: | 13 + This SGPIO controller is for NUVOTON NPCM7xx and NPCM8xx SoC and detailed 14 + information is in the NPCM7XX/8XX SERIAL I/O EXPANSION INTERFACE section. 15 + Nuvoton NPCM7xx SGPIO module is combines a serial to parallel IC (HC595) 16 + and a parallel to serial IC (HC165). 17 + Clock is a division of the APB3 clock. 18 + This interface has 4 pins (D_out , D_in, S_CLK, LDSH). 19 + NPCM7xx/NPCM8xx have two sgpio modules. Each module can support up 20 + to 64 output pins, and up to 64 input pins, the pin is only for GPI or GPO. 21 + GPIO pins can be programmed to support the following options 22 + - Support interrupt option for each input port and various interrupt 23 + sensitivity options (level-high, level-low, edge-high, edge-low) 24 + - ngpios is number of nuvoton,input-ngpios GPIO lines and nuvoton,output-ngpios GPIO lines. 25 + nuvoton,input-ngpios GPIO lines is only for GPI. 26 + nuvoton,output-ngpios GPIO lines is only for GPO. 27 + 28 + properties: 29 + compatible: 30 + enum: 31 + - nuvoton,npcm750-sgpio 32 + - nuvoton,npcm845-sgpio 33 + 34 + reg: 35 + maxItems: 1 36 + 37 + gpio-controller: true 38 + 39 + '#gpio-cells': 40 + const: 2 41 + 42 + interrupts: 43 + maxItems: 1 44 + 45 + clocks: 46 + maxItems: 1 47 + 48 + nuvoton,input-ngpios: 49 + $ref: /schemas/types.yaml#/definitions/uint32 50 + description: 51 + The numbers of GPIO's exposed. GPIO lines are only for GPI. 52 + minimum: 0 53 + maximum: 64 54 + 55 + nuvoton,output-ngpios: 56 + $ref: /schemas/types.yaml#/definitions/uint32 57 + description: 58 + The numbers of GPIO's exposed. GPIO lines are only for GPO. 59 + minimum: 0 60 + maximum: 64 61 + 62 + required: 63 + - compatible 64 + - reg 65 + - gpio-controller 66 + - '#gpio-cells' 67 + - interrupts 68 + - nuvoton,input-ngpios 69 + - nuvoton,output-ngpios 70 + - clocks 71 + 72 + additionalProperties: false 73 + 74 + examples: 75 + - | 76 + #include <dt-bindings/clock/nuvoton,npcm7xx-clock.h> 77 + #include <dt-bindings/interrupt-controller/arm-gic.h> 78 + gpio8: gpio@101000 { 79 + compatible = "nuvoton,npcm750-sgpio"; 80 + reg = <0x101000 0x200>; 81 + clocks = <&clk NPCM7XX_CLK_APB3>; 82 + interrupts = <GIC_SPI 19 IRQ_TYPE_LEVEL_HIGH>; 83 + gpio-controller; 84 + #gpio-cells = <2>; 85 + nuvoton,input-ngpios = <64>; 86 + nuvoton,output-ngpios = <64>; 87 + };
+69
Documentation/devicetree/bindings/gpio/realtek,rtd-gpio.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + # Copyright 2023 Realtek Semiconductor Corporation 3 + %YAML 1.2 4 + --- 5 + $id: http://devicetree.org/schemas/gpio/realtek,rtd-gpio.yaml# 6 + $schema: http://devicetree.org/meta-schemas/core.yaml# 7 + 8 + title: Realtek DHC GPIO controller 9 + 10 + maintainers: 11 + - Tzuyi Chang <tychang@realtek.com> 12 + 13 + description: 14 + The GPIO controller is designed for the Realtek DHC (Digital Home Center) 15 + RTD series SoC family, which are high-definition media processor SoCs. 16 + 17 + properties: 18 + compatible: 19 + enum: 20 + - realtek,rtd1295-misc-gpio 21 + - realtek,rtd1295-iso-gpio 22 + - realtek,rtd1315e-iso-gpio 23 + - realtek,rtd1319-iso-gpio 24 + - realtek,rtd1319d-iso-gpio 25 + - realtek,rtd1395-iso-gpio 26 + - realtek,rtd1619-iso-gpio 27 + - realtek,rtd1619b-iso-gpio 28 + 29 + reg: 30 + items: 31 + - description: GPIO controller registers 32 + - description: GPIO interrupt registers 33 + 34 + interrupts: 35 + items: 36 + - description: Interrupt number of the assert GPIO interrupt, which is 37 + triggered when there is a rising edge. 38 + - description: Interrupt number of the deassert GPIO interrupt, which is 39 + triggered when there is a falling edge. 40 + 41 + gpio-ranges: true 42 + 43 + gpio-controller: true 44 + 45 + "#gpio-cells": 46 + const: 2 47 + 48 + required: 49 + - compatible 50 + - reg 51 + - interrupts 52 + - gpio-ranges 53 + - gpio-controller 54 + - "#gpio-cells" 55 + 56 + additionalProperties: false 57 + 58 + examples: 59 + - | 60 + gpio@100 { 61 + compatible = "realtek,rtd1319d-iso-gpio"; 62 + reg = <0x100 0x100>, 63 + <0x0 0xb0>; 64 + interrupt-parent = <&iso_irq_mux>; 65 + interrupts = <19>, <20>; 66 + gpio-ranges = <&pinctrl 0 0 82>; 67 + gpio-controller; 68 + #gpio-cells = <2>; 69 + };
+7
Documentation/devicetree/bindings/gpio/rockchip,gpio-bank.yaml
··· 41 41 "#interrupt-cells": 42 42 const: 2 43 43 44 + patternProperties: 45 + "^.+-hog(-[0-9]+)?$": 46 + type: object 47 + 48 + required: 49 + - gpio-hog 50 + 44 51 required: 45 52 - compatible 46 53 - reg
+2
Documentation/devicetree/bindings/gpio/snps,dw-apb-gpio.yaml
··· 65 65 minItems: 1 66 66 maxItems: 32 67 67 68 + gpio-ranges: true 69 + 68 70 ngpios: 69 71 default: 32 70 72 minimum: 1
+3
Documentation/devicetree/bindings/gpio/xlnx,zynqmp-gpio-modepin.yaml
··· 23 23 "#gpio-cells": 24 24 const: 2 25 25 26 + label: true 27 + 26 28 required: 27 29 - compatible 28 30 - gpio-controller ··· 39 37 compatible = "xlnx,zynqmp-gpio-modepin"; 40 38 gpio-controller; 41 39 #gpio-cells = <2>; 40 + label = "modepin"; 42 41 }; 43 42 }; 44 43
+20
drivers/gpio/Kconfig
··· 478 478 select GPIO_GENERIC 479 479 select GENERIC_IRQ_CHIP 480 480 481 + config GPIO_NPCM_SGPIO 482 + bool "Nuvoton SGPIO support" 483 + depends on ARCH_NPCM || COMPILE_TEST 484 + select GPIOLIB_IRQCHIP 485 + help 486 + Say Y here to support Nuvoton NPCM7XX/NPCM8XX SGPIO functionality. 487 + 481 488 config GPIO_OCTEON 482 489 tristate "Cavium OCTEON GPIO" 483 490 depends on CAVIUM_OCTEON_SOC ··· 559 552 default ARCH_ROCKCHIP 560 553 help 561 554 Say yes here to support GPIO on Rockchip SoCs. 555 + 556 + config GPIO_RTD 557 + tristate "Realtek DHC GPIO support" 558 + depends on ARCH_REALTEK 559 + default y 560 + select GPIOLIB_IRQCHIP 561 + help 562 + This option enables support for GPIOs found on Realtek DHC(Digital 563 + Home Center) SoCs family, including RTD1295, RTD1315E, RTD1319, 564 + RTD1319D, RTD1395, RTD1619 and RTD1619B. 565 + 566 + Say yes here to support GPIO functionality and GPIO interrupt on 567 + Realtek DHC SoCs. 562 568 563 569 config GPIO_SAMA5D2_PIOBU 564 570 tristate "SAMA5D2 PIOBU GPIO support"
+2
drivers/gpio/Makefile
··· 116 116 obj-$(CONFIG_GPIO_MVEBU) += gpio-mvebu.o 117 117 obj-$(CONFIG_GPIO_MXC) += gpio-mxc.o 118 118 obj-$(CONFIG_GPIO_MXS) += gpio-mxs.o 119 + obj-$(CONFIG_GPIO_NPCM_SGPIO) += gpio-npcm-sgpio.o 119 120 obj-$(CONFIG_GPIO_OCTEON) += gpio-octeon.o 120 121 obj-$(CONFIG_GPIO_OMAP) += gpio-omap.o 121 122 obj-$(CONFIG_GPIO_PALMAS) += gpio-palmas.o ··· 138 137 obj-$(CONFIG_GPIO_REALTEK_OTTO) += gpio-realtek-otto.o 139 138 obj-$(CONFIG_GPIO_REG) += gpio-reg.o 140 139 obj-$(CONFIG_GPIO_ROCKCHIP) += gpio-rockchip.o 140 + obj-$(CONFIG_GPIO_RTD) += gpio-rtd.o 141 141 obj-$(CONFIG_ARCH_SA1100) += gpio-sa1100.o 142 142 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o 143 143 obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o
+9 -4
drivers/gpio/gpio-dwapb.c
··· 416 416 { 417 417 u32 debounce; 418 418 419 - if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) 420 - return -ENOTSUPP; 419 + if (pinconf_to_config_param(config) == PIN_CONFIG_INPUT_DEBOUNCE) { 420 + debounce = pinconf_to_config_argument(config); 421 + return dwapb_gpio_set_debounce(gc, offset, debounce); 422 + } 421 423 422 - debounce = pinconf_to_config_argument(config); 423 - return dwapb_gpio_set_debounce(gc, offset, debounce); 424 + return gpiochip_generic_config(gc, offset, config); 424 425 } 425 426 426 427 static int dwapb_convert_irqs(struct dwapb_gpio_port_irqchip *pirq, ··· 531 530 port->gc.fwnode = pp->fwnode; 532 531 port->gc.ngpio = pp->ngpio; 533 532 port->gc.base = pp->gpio_base; 533 + port->gc.request = gpiochip_generic_request; 534 + port->gc.free = gpiochip_generic_free; 534 535 535 536 /* Only port A support debounce */ 536 537 if (pp->idx == 0) 537 538 port->gc.set_config = dwapb_gpio_set_config; 539 + else 540 + port->gc.set_config = gpiochip_generic_config; 538 541 539 542 /* Only port A can provide interrupts in all configurations of the IP */ 540 543 if (pp->idx == 0)
+1 -13
drivers/gpio/gpio-elkhartlake.c
··· 55 55 return 0; 56 56 } 57 57 58 - static int ehl_gpio_suspend(struct device *dev) 59 - { 60 - return tng_gpio_suspend(dev); 61 - } 62 - 63 - static int ehl_gpio_resume(struct device *dev) 64 - { 65 - return tng_gpio_resume(dev); 66 - } 67 - 68 - static DEFINE_SIMPLE_DEV_PM_OPS(ehl_gpio_pm_ops, ehl_gpio_suspend, ehl_gpio_resume); 69 - 70 58 static const struct platform_device_id ehl_gpio_ids[] = { 71 59 { "gpio-elkhartlake" }, 72 60 { } ··· 64 76 static struct platform_driver ehl_gpio_driver = { 65 77 .driver = { 66 78 .name = "gpio-elkhartlake", 67 - .pm = pm_sleep_ptr(&ehl_gpio_pm_ops), 79 + .pm = pm_sleep_ptr(&tng_gpio_pm_ops), 68 80 }, 69 81 .probe = ehl_gpio_probe, 70 82 .id_table = ehl_gpio_ids,
+50 -1
drivers/gpio/gpio-ixp4xx.c
··· 38 38 #define IXP4XX_GPIO_STYLE_MASK GENMASK(2, 0) 39 39 #define IXP4XX_GPIO_STYLE_SIZE 3 40 40 41 + /* 42 + * Clock output control register defines. 43 + */ 44 + #define IXP4XX_GPCLK_CLK0DC_SHIFT 0 45 + #define IXP4XX_GPCLK_CLK0TC_SHIFT 4 46 + #define IXP4XX_GPCLK_CLK0_MASK GENMASK(7, 0) 47 + #define IXP4XX_GPCLK_MUX14 BIT(8) 48 + #define IXP4XX_GPCLK_CLK1DC_SHIFT 16 49 + #define IXP4XX_GPCLK_CLK1TC_SHIFT 20 50 + #define IXP4XX_GPCLK_CLK1_MASK GENMASK(23, 16) 51 + #define IXP4XX_GPCLK_MUX15 BIT(24) 52 + 41 53 /** 42 54 * struct ixp4xx_gpio - IXP4 GPIO state container 43 55 * @dev: containing device for this instance ··· 214 202 struct ixp4xx_gpio *g; 215 203 struct gpio_irq_chip *girq; 216 204 struct device_node *irq_parent; 205 + bool clk_14, clk_15; 206 + u32 val; 217 207 int ret; 218 208 219 209 g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL); ··· 240 226 g->fwnode = of_node_to_fwnode(np); 241 227 242 228 /* 229 + * If either clock output is enabled explicitly in the device tree 230 + * we take full control of the clock by masking off all bits for 231 + * the clock control and selectively enabling them. Otherwise 232 + * we leave the hardware default settings. 233 + * 234 + * Enable clock outputs with default timings of requested clock. 235 + * If you need control over TC and DC, add these to the device 236 + * tree bindings and use them here. 237 + */ 238 + clk_14 = of_property_read_bool(np, "intel,ixp4xx-gpio14-clkout"); 239 + clk_15 = of_property_read_bool(np, "intel,ixp4xx-gpio15-clkout"); 240 + 241 + /* 243 242 * Make sure GPIO 14 and 15 are NOT used as clocks but GPIO on 244 243 * specific machines. 245 244 */ 246 245 if (of_machine_is_compatible("dlink,dsm-g600-a") || 247 246 of_machine_is_compatible("iom,nas-100d")) 248 - __raw_writel(0x0, g->base + IXP4XX_REG_GPCLK); 247 + val = 0; 248 + else { 249 + val = __raw_readl(g->base + IXP4XX_REG_GPCLK); 250 + 251 + if (clk_14 || clk_15) { 252 + val &= ~(IXP4XX_GPCLK_MUX14 | IXP4XX_GPCLK_MUX15); 253 + val &= ~IXP4XX_GPCLK_CLK0_MASK; 254 + val &= ~IXP4XX_GPCLK_CLK1_MASK; 255 + if (clk_14) { 256 + /* IXP4XX_GPCLK_CLK0DC implicit low */ 257 + val |= (1 << IXP4XX_GPCLK_CLK0TC_SHIFT); 258 + val |= IXP4XX_GPCLK_MUX14; 259 + } 260 + 261 + if (clk_15) { 262 + /* IXP4XX_GPCLK_CLK1DC implicit low */ 263 + val |= (1 << IXP4XX_GPCLK_CLK1TC_SHIFT); 264 + val |= IXP4XX_GPCLK_MUX15; 265 + } 266 + } 267 + } 268 + 269 + __raw_writel(val, g->base + IXP4XX_REG_GPCLK); 249 270 250 271 /* 251 272 * This is a very special big-endian ARM issue: when the IXP4xx is
+1 -1
drivers/gpio/gpio-max730x.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - /** 2 + /* 3 3 * Copyright (C) 2006 Juergen Beisert, Pengutronix 4 4 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix 5 5 * Copyright (C) 2009 Wolfram Sang, Pengutronix
+20 -33
drivers/gpio/gpio-mmio.c
··· 40 40 * `.......````.``` 41 41 */ 42 42 43 - #include <linux/init.h> 44 - #include <linux/err.h> 45 - #include <linux/bug.h> 46 - #include <linux/kernel.h> 47 - #include <linux/module.h> 48 - #include <linux/spinlock.h> 49 - #include <linux/compiler.h> 50 - #include <linux/types.h> 51 - #include <linux/errno.h> 52 - #include <linux/log2.h> 53 - #include <linux/ioport.h> 54 - #include <linux/io.h> 55 - #include <linux/gpio/driver.h> 56 - #include <linux/slab.h> 57 43 #include <linux/bitops.h> 44 + #include <linux/compiler.h> 45 + #include <linux/err.h> 46 + #include <linux/init.h> 47 + #include <linux/io.h> 48 + #include <linux/ioport.h> 49 + #include <linux/log2.h> 50 + #include <linux/mod_devicetable.h> 51 + #include <linux/module.h> 58 52 #include <linux/platform_device.h> 59 53 #include <linux/property.h> 60 - #include <linux/mod_devicetable.h> 61 - #include <linux/of.h> 54 + #include <linux/slab.h> 55 + #include <linux/spinlock.h> 56 + #include <linux/types.h> 57 + 58 + #include <linux/gpio/driver.h> 62 59 63 60 #include "gpiolib.h" 64 61 ··· 685 688 return devm_ioremap_resource(&pdev->dev, r); 686 689 } 687 690 688 - #ifdef CONFIG_OF 689 691 static const struct of_device_id bgpio_of_match[] = { 690 692 { .compatible = "brcm,bcm6345-gpio" }, 691 693 { .compatible = "wd,mbl-gpio" }, ··· 693 697 }; 694 698 MODULE_DEVICE_TABLE(of, bgpio_of_match); 695 699 696 - static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 697 - unsigned long *flags) 700 + static struct bgpio_pdata *bgpio_parse_fw(struct device *dev, unsigned long *flags) 698 701 { 699 702 struct bgpio_pdata *pdata; 700 703 701 - if (!pdev->dev.of_node) 704 + if (!dev_fwnode(dev)) 702 705 return NULL; 703 706 704 - pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata), 705 - GFP_KERNEL); 707 + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 706 708 if (!pdata) 707 709 return ERR_PTR(-ENOMEM); 708 710 709 711 pdata->base = -1; 710 712 711 - if (of_device_is_big_endian(pdev->dev.of_node)) 713 + if (device_is_big_endian(dev)) 712 714 *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER; 713 715 714 - if (of_property_read_bool(pdev->dev.of_node, "no-output")) 716 + if (device_property_read_bool(dev, "no-output")) 715 717 *flags |= BGPIOF_NO_OUTPUT; 716 718 717 719 return pdata; 718 720 } 719 - #else 720 - static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 721 - unsigned long *flags) 722 - { 723 - return NULL; 724 - } 725 - #endif /* CONFIG_OF */ 726 721 727 722 static int bgpio_pdev_probe(struct platform_device *pdev) 728 723 { ··· 730 743 struct gpio_chip *gc; 731 744 struct bgpio_pdata *pdata; 732 745 733 - pdata = bgpio_parse_dt(pdev, &flags); 746 + pdata = bgpio_parse_fw(dev, &flags); 734 747 if (IS_ERR(pdata)) 735 748 return PTR_ERR(pdata); 736 749 ··· 801 814 static struct platform_driver bgpio_driver = { 802 815 .driver = { 803 816 .name = "basic-mmio-gpio", 804 - .of_match_table = of_match_ptr(bgpio_of_match), 817 + .of_match_table = bgpio_of_match, 805 818 }, 806 819 .id_table = bgpio_id_table, 807 820 .probe = bgpio_pdev_probe,
+1 -2
drivers/gpio/gpio-mockup.c
··· 354 354 static void gpio_mockup_debugfs_setup(struct device *dev, 355 355 struct gpio_mockup_chip *chip) 356 356 { 357 - struct device *child __free(put_device) = NULL; 358 357 struct gpio_mockup_dbgfs_private *priv; 359 358 struct gpio_chip *gc; 360 359 const char *devname; ··· 366 367 * There can only be a single GPIO device per platform device in 367 368 * gpio-mockup so using device_find_any_child() is OK. 368 369 */ 369 - child = device_find_any_child(dev); 370 + struct device *child __free(put_device) = device_find_any_child(dev); 370 371 if (!child) 371 372 return; 372 373
+619
drivers/gpio/gpio-npcm-sgpio.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Nuvoton NPCM Serial GPIO Driver 4 + * 5 + * Copyright (C) 2021 Nuvoton Technologies 6 + */ 7 + 8 + #include <linux/bitfield.h> 9 + #include <linux/clk.h> 10 + #include <linux/gpio/driver.h> 11 + #include <linux/hashtable.h> 12 + #include <linux/init.h> 13 + #include <linux/io.h> 14 + #include <linux/module.h> 15 + #include <linux/platform_device.h> 16 + #include <linux/spinlock.h> 17 + #include <linux/string.h> 18 + #include <linux/units.h> 19 + 20 + #define MAX_NR_HW_SGPIO 64 21 + 22 + #define NPCM_IOXCFG1 0x2A 23 + #define NPCM_IOXCFG1_SFT_CLK GENMASK(3, 0) 24 + #define NPCM_IOXCFG1_SCLK_POL BIT(4) 25 + #define NPCM_IOXCFG1_LDSH_POL BIT(5) 26 + 27 + #define NPCM_IOXCTS 0x28 28 + #define NPCM_IOXCTS_IOXIF_EN BIT(7) 29 + #define NPCM_IOXCTS_RD_MODE GENMASK(2, 1) 30 + #define NPCM_IOXCTS_RD_MODE_PERIODIC BIT(2) 31 + 32 + #define NPCM_IOXCFG2 0x2B 33 + #define NPCM_IOXCFG2_PORT GENMASK(3, 0) 34 + 35 + #define NPCM_IXOEVCFG_MASK GENMASK(1, 0) 36 + #define NPCM_IXOEVCFG_FALLING BIT(1) 37 + #define NPCM_IXOEVCFG_RISING BIT(0) 38 + #define NPCM_IXOEVCFG_BOTH (NPCM_IXOEVCFG_FALLING | NPCM_IXOEVCFG_RISING) 39 + 40 + #define NPCM_CLK_MHZ (8 * HZ_PER_MHZ) 41 + #define NPCM_750_OPT 6 42 + #define NPCM_845_OPT 5 43 + 44 + #define GPIO_BANK(x) ((x) / 8) 45 + #define GPIO_BIT(x) ((x) % 8) 46 + 47 + /* 48 + * Select the frequency of shift clock. 49 + * The shift clock is a division of the APB clock. 50 + */ 51 + struct npcm_clk_cfg { 52 + unsigned int *sft_clk; 53 + unsigned int *clk_sel; 54 + unsigned int cfg_opt; 55 + }; 56 + 57 + struct npcm_sgpio { 58 + struct gpio_chip chip; 59 + struct clk *pclk; 60 + struct irq_chip intc; 61 + raw_spinlock_t lock; 62 + 63 + void __iomem *base; 64 + int irq; 65 + u8 nin_sgpio; 66 + u8 nout_sgpio; 67 + u8 in_port; 68 + u8 out_port; 69 + u8 int_type[MAX_NR_HW_SGPIO]; 70 + }; 71 + 72 + struct npcm_sgpio_bank { 73 + u8 rdata_reg; 74 + u8 wdata_reg; 75 + u8 event_config; 76 + u8 event_status; 77 + }; 78 + 79 + enum npcm_sgpio_reg { 80 + READ_DATA, 81 + WRITE_DATA, 82 + EVENT_CFG, 83 + EVENT_STS, 84 + }; 85 + 86 + static const struct npcm_sgpio_bank npcm_sgpio_banks[] = { 87 + { 88 + .wdata_reg = 0x00, 89 + .rdata_reg = 0x08, 90 + .event_config = 0x10, 91 + .event_status = 0x20, 92 + }, 93 + { 94 + .wdata_reg = 0x01, 95 + .rdata_reg = 0x09, 96 + .event_config = 0x12, 97 + .event_status = 0x21, 98 + }, 99 + { 100 + .wdata_reg = 0x02, 101 + .rdata_reg = 0x0a, 102 + .event_config = 0x14, 103 + .event_status = 0x22, 104 + }, 105 + { 106 + .wdata_reg = 0x03, 107 + .rdata_reg = 0x0b, 108 + .event_config = 0x16, 109 + .event_status = 0x23, 110 + }, 111 + { 112 + .wdata_reg = 0x04, 113 + .rdata_reg = 0x0c, 114 + .event_config = 0x18, 115 + .event_status = 0x24, 116 + }, 117 + { 118 + .wdata_reg = 0x05, 119 + .rdata_reg = 0x0d, 120 + .event_config = 0x1a, 121 + .event_status = 0x25, 122 + }, 123 + { 124 + .wdata_reg = 0x06, 125 + .rdata_reg = 0x0e, 126 + .event_config = 0x1c, 127 + .event_status = 0x26, 128 + }, 129 + { 130 + .wdata_reg = 0x07, 131 + .rdata_reg = 0x0f, 132 + .event_config = 0x1e, 133 + .event_status = 0x27, 134 + }, 135 + }; 136 + 137 + static void __iomem *bank_reg(struct npcm_sgpio *gpio, 138 + const struct npcm_sgpio_bank *bank, 139 + const enum npcm_sgpio_reg reg) 140 + { 141 + switch (reg) { 142 + case READ_DATA: 143 + return gpio->base + bank->rdata_reg; 144 + case WRITE_DATA: 145 + return gpio->base + bank->wdata_reg; 146 + case EVENT_CFG: 147 + return gpio->base + bank->event_config; 148 + case EVENT_STS: 149 + return gpio->base + bank->event_status; 150 + default: 151 + /* actually if code runs to here, it's an error case */ 152 + dev_WARN(gpio->chip.parent, "Getting here is an error condition"); 153 + return NULL; 154 + } 155 + } 156 + 157 + static const struct npcm_sgpio_bank *offset_to_bank(unsigned int offset) 158 + { 159 + unsigned int bank = GPIO_BANK(offset); 160 + 161 + return &npcm_sgpio_banks[bank]; 162 + } 163 + 164 + static void npcm_sgpio_irqd_to_data(struct irq_data *d, 165 + struct npcm_sgpio **gpio, 166 + const struct npcm_sgpio_bank **bank, 167 + u8 *bit, unsigned int *offset) 168 + { 169 + struct npcm_sgpio *internal; 170 + 171 + *offset = irqd_to_hwirq(d); 172 + internal = irq_data_get_irq_chip_data(d); 173 + 174 + *gpio = internal; 175 + *offset -= internal->nout_sgpio; 176 + *bank = offset_to_bank(*offset); 177 + *bit = GPIO_BIT(*offset); 178 + } 179 + 180 + static int npcm_sgpio_init_port(struct npcm_sgpio *gpio) 181 + { 182 + u8 in_port, out_port, set_port, reg; 183 + 184 + in_port = GPIO_BANK(gpio->nin_sgpio); 185 + if (GPIO_BIT(gpio->nin_sgpio) > 0) 186 + in_port += 1; 187 + 188 + out_port = GPIO_BANK(gpio->nout_sgpio); 189 + if (GPIO_BIT(gpio->nout_sgpio) > 0) 190 + out_port += 1; 191 + 192 + gpio->in_port = in_port; 193 + gpio->out_port = out_port; 194 + set_port = (out_port & NPCM_IOXCFG2_PORT) << 4 | 195 + (in_port & NPCM_IOXCFG2_PORT); 196 + iowrite8(set_port, gpio->base + NPCM_IOXCFG2); 197 + 198 + reg = ioread8(gpio->base + NPCM_IOXCFG2); 199 + 200 + return reg == set_port ? 0 : -EINVAL; 201 + 202 + } 203 + 204 + static int npcm_sgpio_dir_in(struct gpio_chip *gc, unsigned int offset) 205 + { 206 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 207 + 208 + return offset < gpio->nout_sgpio ? -EINVAL : 0; 209 + 210 + } 211 + 212 + static int npcm_sgpio_dir_out(struct gpio_chip *gc, unsigned int offset, int val) 213 + { 214 + gc->set(gc, offset, val); 215 + 216 + return 0; 217 + } 218 + 219 + static int npcm_sgpio_get_direction(struct gpio_chip *gc, unsigned int offset) 220 + { 221 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 222 + 223 + if (offset < gpio->nout_sgpio) 224 + return GPIO_LINE_DIRECTION_OUT; 225 + 226 + return GPIO_LINE_DIRECTION_IN; 227 + } 228 + 229 + static void npcm_sgpio_set(struct gpio_chip *gc, unsigned int offset, int val) 230 + { 231 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 232 + const struct npcm_sgpio_bank *bank = offset_to_bank(offset); 233 + void __iomem *addr; 234 + u8 reg = 0; 235 + 236 + addr = bank_reg(gpio, bank, WRITE_DATA); 237 + reg = ioread8(addr); 238 + 239 + if (val) 240 + reg |= BIT(GPIO_BIT(offset)); 241 + else 242 + reg &= ~BIT(GPIO_BIT(offset)); 243 + 244 + iowrite8(reg, addr); 245 + } 246 + 247 + static int npcm_sgpio_get(struct gpio_chip *gc, unsigned int offset) 248 + { 249 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 250 + const struct npcm_sgpio_bank *bank; 251 + void __iomem *addr; 252 + u8 reg; 253 + 254 + if (offset < gpio->nout_sgpio) { 255 + bank = offset_to_bank(offset); 256 + addr = bank_reg(gpio, bank, WRITE_DATA); 257 + } else { 258 + offset -= gpio->nout_sgpio; 259 + bank = offset_to_bank(offset); 260 + addr = bank_reg(gpio, bank, READ_DATA); 261 + } 262 + 263 + reg = ioread8(addr); 264 + 265 + return !!(reg & BIT(GPIO_BIT(offset))); 266 + } 267 + 268 + static void npcm_sgpio_setup_enable(struct npcm_sgpio *gpio, bool enable) 269 + { 270 + u8 reg; 271 + 272 + reg = ioread8(gpio->base + NPCM_IOXCTS); 273 + reg = (reg & ~NPCM_IOXCTS_RD_MODE) | NPCM_IOXCTS_RD_MODE_PERIODIC; 274 + 275 + if (enable) 276 + reg |= NPCM_IOXCTS_IOXIF_EN; 277 + else 278 + reg &= ~NPCM_IOXCTS_IOXIF_EN; 279 + 280 + iowrite8(reg, gpio->base + NPCM_IOXCTS); 281 + } 282 + 283 + static int npcm_sgpio_setup_clk(struct npcm_sgpio *gpio, 284 + const struct npcm_clk_cfg *clk_cfg) 285 + { 286 + unsigned long apb_freq; 287 + u32 val; 288 + u8 tmp; 289 + int i; 290 + 291 + apb_freq = clk_get_rate(gpio->pclk); 292 + tmp = ioread8(gpio->base + NPCM_IOXCFG1) & ~NPCM_IOXCFG1_SFT_CLK; 293 + 294 + for (i = clk_cfg->cfg_opt-1; i > 0; i--) { 295 + val = apb_freq / clk_cfg->sft_clk[i]; 296 + if (NPCM_CLK_MHZ > val) { 297 + iowrite8(clk_cfg->clk_sel[i] | tmp, 298 + gpio->base + NPCM_IOXCFG1); 299 + return 0; 300 + } 301 + } 302 + 303 + return -EINVAL; 304 + } 305 + 306 + static void npcm_sgpio_irq_init_valid_mask(struct gpio_chip *gc, 307 + unsigned long *valid_mask, 308 + unsigned int ngpios) 309 + { 310 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 311 + 312 + /* input GPIOs in the high range */ 313 + bitmap_set(valid_mask, gpio->nout_sgpio, gpio->nin_sgpio); 314 + bitmap_clear(valid_mask, 0, gpio->nout_sgpio); 315 + } 316 + 317 + static void npcm_sgpio_irq_set_mask(struct irq_data *d, bool set) 318 + { 319 + const struct npcm_sgpio_bank *bank; 320 + struct npcm_sgpio *gpio; 321 + unsigned long flags; 322 + void __iomem *addr; 323 + unsigned int offset; 324 + u16 reg, type; 325 + u8 bit; 326 + 327 + npcm_sgpio_irqd_to_data(d, &gpio, &bank, &bit, &offset); 328 + addr = bank_reg(gpio, bank, EVENT_CFG); 329 + 330 + reg = ioread16(addr); 331 + if (set) { 332 + reg &= ~(NPCM_IXOEVCFG_MASK << (bit * 2)); 333 + } else { 334 + type = gpio->int_type[offset]; 335 + reg |= (type << (bit * 2)); 336 + } 337 + 338 + raw_spin_lock_irqsave(&gpio->lock, flags); 339 + 340 + npcm_sgpio_setup_enable(gpio, false); 341 + 342 + iowrite16(reg, addr); 343 + 344 + npcm_sgpio_setup_enable(gpio, true); 345 + 346 + addr = bank_reg(gpio, bank, EVENT_STS); 347 + reg = ioread8(addr); 348 + reg |= BIT(bit); 349 + iowrite8(reg, addr); 350 + 351 + raw_spin_unlock_irqrestore(&gpio->lock, flags); 352 + } 353 + 354 + static void npcm_sgpio_irq_ack(struct irq_data *d) 355 + { 356 + const struct npcm_sgpio_bank *bank; 357 + struct npcm_sgpio *gpio; 358 + unsigned long flags; 359 + void __iomem *status_addr; 360 + unsigned int offset; 361 + u8 bit; 362 + 363 + npcm_sgpio_irqd_to_data(d, &gpio, &bank, &bit, &offset); 364 + status_addr = bank_reg(gpio, bank, EVENT_STS); 365 + raw_spin_lock_irqsave(&gpio->lock, flags); 366 + iowrite8(BIT(bit), status_addr); 367 + raw_spin_unlock_irqrestore(&gpio->lock, flags); 368 + } 369 + 370 + static void npcm_sgpio_irq_mask(struct irq_data *d) 371 + { 372 + npcm_sgpio_irq_set_mask(d, true); 373 + } 374 + 375 + static void npcm_sgpio_irq_unmask(struct irq_data *d) 376 + { 377 + npcm_sgpio_irq_set_mask(d, false); 378 + } 379 + 380 + static int npcm_sgpio_set_type(struct irq_data *d, unsigned int type) 381 + { 382 + const struct npcm_sgpio_bank *bank; 383 + irq_flow_handler_t handler; 384 + struct npcm_sgpio *gpio; 385 + unsigned long flags; 386 + void __iomem *addr; 387 + unsigned int offset; 388 + u16 reg, val; 389 + u8 bit; 390 + 391 + npcm_sgpio_irqd_to_data(d, &gpio, &bank, &bit, &offset); 392 + 393 + switch (type & IRQ_TYPE_SENSE_MASK) { 394 + case IRQ_TYPE_EDGE_BOTH: 395 + val = NPCM_IXOEVCFG_BOTH; 396 + break; 397 + case IRQ_TYPE_EDGE_RISING: 398 + case IRQ_TYPE_LEVEL_HIGH: 399 + val = NPCM_IXOEVCFG_RISING; 400 + break; 401 + case IRQ_TYPE_EDGE_FALLING: 402 + case IRQ_TYPE_LEVEL_LOW: 403 + val = NPCM_IXOEVCFG_FALLING; 404 + break; 405 + default: 406 + return -EINVAL; 407 + } 408 + 409 + if (type & IRQ_TYPE_LEVEL_MASK) 410 + handler = handle_level_irq; 411 + else 412 + handler = handle_edge_irq; 413 + 414 + gpio->int_type[offset] = val; 415 + 416 + raw_spin_lock_irqsave(&gpio->lock, flags); 417 + npcm_sgpio_setup_enable(gpio, false); 418 + addr = bank_reg(gpio, bank, EVENT_CFG); 419 + reg = ioread16(addr); 420 + 421 + reg |= (val << (bit * 2)); 422 + 423 + iowrite16(reg, addr); 424 + npcm_sgpio_setup_enable(gpio, true); 425 + raw_spin_unlock_irqrestore(&gpio->lock, flags); 426 + 427 + irq_set_handler_locked(d, handler); 428 + 429 + return 0; 430 + } 431 + 432 + static void npcm_sgpio_irq_handler(struct irq_desc *desc) 433 + { 434 + struct gpio_chip *gc = irq_desc_get_handler_data(desc); 435 + struct irq_chip *ic = irq_desc_get_chip(desc); 436 + struct npcm_sgpio *gpio = gpiochip_get_data(gc); 437 + unsigned int i, j, girq; 438 + unsigned long reg; 439 + 440 + chained_irq_enter(ic, desc); 441 + 442 + for (i = 0; i < ARRAY_SIZE(npcm_sgpio_banks); i++) { 443 + const struct npcm_sgpio_bank *bank = &npcm_sgpio_banks[i]; 444 + 445 + reg = ioread8(bank_reg(gpio, bank, EVENT_STS)); 446 + for_each_set_bit(j, &reg, 8) { 447 + girq = irq_find_mapping(gc->irq.domain, 448 + i * 8 + gpio->nout_sgpio + j); 449 + generic_handle_domain_irq(gc->irq.domain, girq); 450 + } 451 + } 452 + 453 + chained_irq_exit(ic, desc); 454 + } 455 + 456 + static const struct irq_chip sgpio_irq_chip = { 457 + .name = "sgpio-irq", 458 + .irq_ack = npcm_sgpio_irq_ack, 459 + .irq_mask = npcm_sgpio_irq_mask, 460 + .irq_unmask = npcm_sgpio_irq_unmask, 461 + .irq_set_type = npcm_sgpio_set_type, 462 + .flags = IRQCHIP_IMMUTABLE | IRQCHIP_MASK_ON_SUSPEND, 463 + GPIOCHIP_IRQ_RESOURCE_HELPERS, 464 + }; 465 + 466 + static int npcm_sgpio_setup_irqs(struct npcm_sgpio *gpio, 467 + struct platform_device *pdev) 468 + { 469 + int rc, i; 470 + struct gpio_irq_chip *irq; 471 + 472 + rc = platform_get_irq(pdev, 0); 473 + if (rc < 0) 474 + return rc; 475 + 476 + gpio->irq = rc; 477 + 478 + npcm_sgpio_setup_enable(gpio, false); 479 + 480 + /* Disable IRQ and clear Interrupt status registers for all SGPIO Pins. */ 481 + for (i = 0; i < ARRAY_SIZE(npcm_sgpio_banks); i++) { 482 + const struct npcm_sgpio_bank *bank = &npcm_sgpio_banks[i]; 483 + 484 + iowrite16(0, bank_reg(gpio, bank, EVENT_CFG)); 485 + iowrite8(0xff, bank_reg(gpio, bank, EVENT_STS)); 486 + } 487 + 488 + irq = &gpio->chip.irq; 489 + gpio_irq_chip_set_chip(irq, &sgpio_irq_chip); 490 + irq->init_valid_mask = npcm_sgpio_irq_init_valid_mask; 491 + irq->handler = handle_bad_irq; 492 + irq->default_type = IRQ_TYPE_NONE; 493 + irq->parent_handler = npcm_sgpio_irq_handler; 494 + irq->parent_handler_data = gpio; 495 + irq->parents = &gpio->irq; 496 + irq->num_parents = 1; 497 + 498 + return 0; 499 + } 500 + 501 + static int npcm_sgpio_probe(struct platform_device *pdev) 502 + { 503 + struct npcm_sgpio *gpio; 504 + const struct npcm_clk_cfg *clk_cfg; 505 + int rc; 506 + u32 nin_gpios, nout_gpios; 507 + 508 + gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); 509 + if (!gpio) 510 + return -ENOMEM; 511 + 512 + gpio->base = devm_platform_ioremap_resource(pdev, 0); 513 + if (IS_ERR(gpio->base)) 514 + return PTR_ERR(gpio->base); 515 + 516 + clk_cfg = device_get_match_data(&pdev->dev); 517 + if (!clk_cfg) 518 + return -EINVAL; 519 + 520 + rc = device_property_read_u32(&pdev->dev, "nuvoton,input-ngpios", 521 + &nin_gpios); 522 + if (rc < 0) 523 + return dev_err_probe(&pdev->dev, rc, "Could not read ngpios property\n"); 524 + 525 + rc = device_property_read_u32(&pdev->dev, "nuvoton,output-ngpios", 526 + &nout_gpios); 527 + if (rc < 0) 528 + return dev_err_probe(&pdev->dev, rc, "Could not read ngpios property\n"); 529 + 530 + gpio->nin_sgpio = nin_gpios; 531 + gpio->nout_sgpio = nout_gpios; 532 + if (gpio->nin_sgpio > MAX_NR_HW_SGPIO || 533 + gpio->nout_sgpio > MAX_NR_HW_SGPIO) 534 + return dev_err_probe(&pdev->dev, -EINVAL, "Number of GPIOs exceeds the maximum of %d: input: %d output: %d\n", MAX_NR_HW_SGPIO, nin_gpios, nout_gpios); 535 + 536 + gpio->pclk = devm_clk_get(&pdev->dev, NULL); 537 + if (IS_ERR(gpio->pclk)) 538 + return dev_err_probe(&pdev->dev, PTR_ERR(gpio->pclk), "Could not get pclk\n"); 539 + 540 + rc = npcm_sgpio_setup_clk(gpio, clk_cfg); 541 + if (rc < 0) 542 + return dev_err_probe(&pdev->dev, rc, "Failed to setup clock\n"); 543 + 544 + raw_spin_lock_init(&gpio->lock); 545 + gpio->chip.parent = &pdev->dev; 546 + gpio->chip.ngpio = gpio->nin_sgpio + gpio->nout_sgpio; 547 + gpio->chip.direction_input = npcm_sgpio_dir_in; 548 + gpio->chip.direction_output = npcm_sgpio_dir_out; 549 + gpio->chip.get_direction = npcm_sgpio_get_direction; 550 + gpio->chip.get = npcm_sgpio_get; 551 + gpio->chip.set = npcm_sgpio_set; 552 + gpio->chip.label = dev_name(&pdev->dev); 553 + gpio->chip.base = -1; 554 + 555 + rc = npcm_sgpio_init_port(gpio); 556 + if (rc < 0) 557 + return rc; 558 + 559 + rc = npcm_sgpio_setup_irqs(gpio, pdev); 560 + if (rc < 0) 561 + return rc; 562 + 563 + rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); 564 + if (rc) 565 + return dev_err_probe(&pdev->dev, rc, "GPIO registering failed\n"); 566 + 567 + npcm_sgpio_setup_enable(gpio, true); 568 + 569 + return 0; 570 + } 571 + 572 + static unsigned int npcm750_SFT_CLK[NPCM_750_OPT] = { 573 + 1024, 32, 8, 4, 3, 2, 574 + }; 575 + 576 + static unsigned int npcm750_CLK_SEL[NPCM_750_OPT] = { 577 + 0x00, 0x05, 0x07, 0x0C, 0x0D, 0x0E, 578 + }; 579 + 580 + static unsigned int npcm845_SFT_CLK[NPCM_845_OPT] = { 581 + 1024, 32, 16, 8, 4, 582 + }; 583 + 584 + static unsigned int npcm845_CLK_SEL[NPCM_845_OPT] = { 585 + 0x00, 0x05, 0x06, 0x07, 0x0C, 586 + }; 587 + 588 + static struct npcm_clk_cfg npcm750_sgpio_pdata = { 589 + .sft_clk = npcm750_SFT_CLK, 590 + .clk_sel = npcm750_CLK_SEL, 591 + .cfg_opt = NPCM_750_OPT, 592 + }; 593 + 594 + static const struct npcm_clk_cfg npcm845_sgpio_pdata = { 595 + .sft_clk = npcm845_SFT_CLK, 596 + .clk_sel = npcm845_CLK_SEL, 597 + .cfg_opt = NPCM_845_OPT, 598 + }; 599 + 600 + static const struct of_device_id npcm_sgpio_of_table[] = { 601 + { .compatible = "nuvoton,npcm750-sgpio", .data = &npcm750_sgpio_pdata, }, 602 + { .compatible = "nuvoton,npcm845-sgpio", .data = &npcm845_sgpio_pdata, }, 603 + {} 604 + }; 605 + MODULE_DEVICE_TABLE(of, npcm_sgpio_of_table); 606 + 607 + static struct platform_driver npcm_sgpio_driver = { 608 + .driver = { 609 + .name = KBUILD_MODNAME, 610 + .of_match_table = npcm_sgpio_of_table, 611 + }, 612 + .probe = npcm_sgpio_probe, 613 + }; 614 + module_platform_driver(npcm_sgpio_driver); 615 + 616 + MODULE_AUTHOR("Jim Liu <jjliu0@nuvoton.com>"); 617 + MODULE_AUTHOR("Joseph Liu <kwliu@nuvoton.com>"); 618 + MODULE_DESCRIPTION("Nuvoton NPCM Serial GPIO Driver"); 619 + MODULE_LICENSE("GPL v2");
+10 -9
drivers/gpio/gpio-pmic-eic-sprd.c
··· 151 151 struct sprd_pmic_eic *pmic_eic = gpiochip_get_data(chip); 152 152 u32 offset = irqd_to_hwirq(data); 153 153 154 - pmic_eic->reg[REG_IE] = 0; 155 - pmic_eic->reg[REG_TRIG] = 0; 154 + pmic_eic->reg[REG_IE] &= ~BIT(offset); 155 + pmic_eic->reg[REG_TRIG] &= ~BIT(offset); 156 156 157 157 gpiochip_disable_irq(chip, offset); 158 158 } ··· 165 165 166 166 gpiochip_enable_irq(chip, offset); 167 167 168 - pmic_eic->reg[REG_IE] = 1; 169 - pmic_eic->reg[REG_TRIG] = 1; 168 + pmic_eic->reg[REG_IE] |= BIT(offset); 169 + pmic_eic->reg[REG_TRIG] |= BIT(offset); 170 170 } 171 171 172 172 static int sprd_pmic_eic_irq_set_type(struct irq_data *data, ··· 174 174 { 175 175 struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 176 176 struct sprd_pmic_eic *pmic_eic = gpiochip_get_data(chip); 177 + u32 offset = irqd_to_hwirq(data); 177 178 178 179 switch (flow_type) { 179 180 case IRQ_TYPE_LEVEL_HIGH: 180 - pmic_eic->reg[REG_IEV] = 1; 181 + pmic_eic->reg[REG_IEV] |= BIT(offset); 181 182 break; 182 183 case IRQ_TYPE_LEVEL_LOW: 183 - pmic_eic->reg[REG_IEV] = 0; 184 + pmic_eic->reg[REG_IEV] &= ~BIT(offset); 184 185 break; 185 186 case IRQ_TYPE_EDGE_RISING: 186 187 case IRQ_TYPE_EDGE_FALLING: ··· 223 222 sprd_pmic_eic_update(chip, offset, SPRD_PMIC_EIC_IEV, 1); 224 223 } else { 225 224 sprd_pmic_eic_update(chip, offset, SPRD_PMIC_EIC_IEV, 226 - pmic_eic->reg[REG_IEV]); 225 + !!(pmic_eic->reg[REG_IEV] & BIT(offset))); 227 226 } 228 227 229 228 /* Set irq unmask */ 230 229 sprd_pmic_eic_update(chip, offset, SPRD_PMIC_EIC_IE, 231 - pmic_eic->reg[REG_IE]); 230 + !!(pmic_eic->reg[REG_IE] & BIT(offset))); 232 231 /* Generate trigger start pulse for debounce EIC */ 233 232 sprd_pmic_eic_update(chip, offset, SPRD_PMIC_EIC_TRIG, 234 - pmic_eic->reg[REG_TRIG]); 233 + !!(pmic_eic->reg[REG_TRIG] & BIT(offset))); 235 234 236 235 mutex_unlock(&pmic_eic->buslock); 237 236 }
+604
drivers/gpio/gpio-rtd.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Realtek DHC gpio driver 4 + * 5 + * Copyright (c) 2023 Realtek Semiconductor Corp. 6 + */ 7 + 8 + #include <linux/bitops.h> 9 + #include <linux/cleanup.h> 10 + #include <linux/gpio/driver.h> 11 + #include <linux/interrupt.h> 12 + #include <linux/irqchip.h> 13 + #include <linux/irqchip/chained_irq.h> 14 + #include <linux/irqdomain.h> 15 + #include <linux/module.h> 16 + #include <linux/platform_device.h> 17 + #include <linux/property.h> 18 + #include <linux/spinlock.h> 19 + #include <linux/types.h> 20 + 21 + #define RTD_GPIO_DEBOUNCE_1US 0 22 + #define RTD_GPIO_DEBOUNCE_10US 1 23 + #define RTD_GPIO_DEBOUNCE_100US 2 24 + #define RTD_GPIO_DEBOUNCE_1MS 3 25 + #define RTD_GPIO_DEBOUNCE_10MS 4 26 + #define RTD_GPIO_DEBOUNCE_20MS 5 27 + #define RTD_GPIO_DEBOUNCE_30MS 6 28 + 29 + /** 30 + * struct rtd_gpio_info - Specific GPIO register information 31 + * @name: GPIO device name 32 + * @gpio_base: GPIO base number 33 + * @num_gpios: The number of GPIOs 34 + * @dir_offset: Offset for GPIO direction registers 35 + * @dato_offset: Offset for GPIO data output registers 36 + * @dati_offset: Offset for GPIO data input registers 37 + * @ie_offset: Offset for GPIO interrupt enable registers 38 + * @dp_offset: Offset for GPIO detection polarity registers 39 + * @gpa_offset: Offset for GPIO assert interrupt status registers 40 + * @gpda_offset: Offset for GPIO deassert interrupt status registers 41 + * @deb_offset: Offset for GPIO debounce registers 42 + * @deb_val: Register values representing the GPIO debounce time 43 + * @get_deb_setval: Used to get the corresponding value for setting the debounce register 44 + */ 45 + struct rtd_gpio_info { 46 + const char *name; 47 + unsigned int gpio_base; 48 + unsigned int num_gpios; 49 + u8 *dir_offset; 50 + u8 *dato_offset; 51 + u8 *dati_offset; 52 + u8 *ie_offset; 53 + u8 *dp_offset; 54 + u8 *gpa_offset; 55 + u8 *gpda_offset; 56 + u8 *deb_offset; 57 + u8 *deb_val; 58 + u8 (*get_deb_setval)(const struct rtd_gpio_info *info, 59 + unsigned int offset, u8 deb_index, 60 + u8 *reg_offset, u8 *shift); 61 + }; 62 + 63 + struct rtd_gpio { 64 + struct gpio_chip gpio_chip; 65 + const struct rtd_gpio_info *info; 66 + void __iomem *base; 67 + void __iomem *irq_base; 68 + unsigned int irqs[2]; 69 + raw_spinlock_t lock; 70 + }; 71 + 72 + static u8 rtd_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset, 73 + u8 deb_index, u8 *reg_offset, u8 *shift) 74 + { 75 + *reg_offset = info->deb_offset[offset / 8]; 76 + *shift = (offset % 8) * 4; 77 + return info->deb_val[deb_index]; 78 + } 79 + 80 + static u8 rtd1295_misc_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset, 81 + u8 deb_index, u8 *reg_offset, u8 *shift) 82 + { 83 + *reg_offset = info->deb_offset[0]; 84 + *shift = (offset % 8) * 4; 85 + return info->deb_val[deb_index]; 86 + } 87 + 88 + static u8 rtd1295_iso_gpio_get_deb_setval(const struct rtd_gpio_info *info, unsigned int offset, 89 + u8 deb_index, u8 *reg_offset, u8 *shift) 90 + { 91 + *reg_offset = info->deb_offset[0]; 92 + *shift = 0; 93 + return info->deb_val[deb_index]; 94 + } 95 + 96 + static const struct rtd_gpio_info rtd_iso_gpio_info = { 97 + .name = "rtd_iso_gpio", 98 + .gpio_base = 0, 99 + .num_gpios = 82, 100 + .dir_offset = (u8 []){ 0x0, 0x18, 0x2c }, 101 + .dato_offset = (u8 []){ 0x4, 0x1c, 0x30 }, 102 + .dati_offset = (u8 []){ 0x8, 0x20, 0x34 }, 103 + .ie_offset = (u8 []){ 0xc, 0x24, 0x38 }, 104 + .dp_offset = (u8 []){ 0x10, 0x28, 0x3c }, 105 + .gpa_offset = (u8 []){ 0x8, 0xe0, 0x90 }, 106 + .gpda_offset = (u8 []){ 0xc, 0xe4, 0x94 }, 107 + .deb_offset = (u8 []){ 0x44, 0x48, 0x4c, 0x50, 0x54, 0x58, 0x5c, 108 + 0x60, 0x64, 0x68, 0x6c }, 109 + .deb_val = (u8 []){ 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 }, 110 + .get_deb_setval = rtd_gpio_get_deb_setval, 111 + }; 112 + 113 + static const struct rtd_gpio_info rtd1619_iso_gpio_info = { 114 + .name = "rtd1619_iso_gpio", 115 + .gpio_base = 0, 116 + .num_gpios = 86, 117 + .dir_offset = (u8 []){ 0x0, 0x18, 0x2c }, 118 + .dato_offset = (u8 []){ 0x4, 0x1c, 0x30 }, 119 + .dati_offset = (u8 []){ 0x8, 0x20, 0x34 }, 120 + .ie_offset = (u8 []){ 0xc, 0x24, 0x38 }, 121 + .dp_offset = (u8 []){ 0x10, 0x28, 0x3c }, 122 + .gpa_offset = (u8 []){ 0x8, 0xe0, 0x90 }, 123 + .gpda_offset = (u8 []){ 0xc, 0xe4, 0x94 }, 124 + .deb_offset = (u8 []){ 0x44, 0x48, 0x4c, 0x50, 0x54, 0x58, 0x5c, 125 + 0x60, 0x64, 0x68, 0x6c }, 126 + .deb_val = (u8 []){ 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 }, 127 + .get_deb_setval = rtd_gpio_get_deb_setval, 128 + }; 129 + 130 + static const struct rtd_gpio_info rtd1395_iso_gpio_info = { 131 + .name = "rtd1395_iso_gpio", 132 + .gpio_base = 0, 133 + .num_gpios = 57, 134 + .dir_offset = (u8 []){ 0x0, 0x18 }, 135 + .dato_offset = (u8 []){ 0x4, 0x1c }, 136 + .dati_offset = (u8 []){ 0x8, 0x20 }, 137 + .ie_offset = (u8 []){ 0xc, 0x24 }, 138 + .dp_offset = (u8 []){ 0x10, 0x28 }, 139 + .gpa_offset = (u8 []){ 0x8, 0xe0 }, 140 + .gpda_offset = (u8 []){ 0xc, 0xe4 }, 141 + .deb_offset = (u8 []){ 0x30, 0x34, 0x38, 0x3c, 0x40, 0x44, 0x48, 0x4c }, 142 + .deb_val = (u8 []){ 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 }, 143 + .get_deb_setval = rtd_gpio_get_deb_setval, 144 + }; 145 + 146 + static const struct rtd_gpio_info rtd1295_misc_gpio_info = { 147 + .name = "rtd1295_misc_gpio", 148 + .gpio_base = 0, 149 + .num_gpios = 101, 150 + .dir_offset = (u8 []){ 0x0, 0x4, 0x8, 0xc }, 151 + .dato_offset = (u8 []){ 0x10, 0x14, 0x18, 0x1c }, 152 + .dati_offset = (u8 []){ 0x20, 0x24, 0x28, 0x2c }, 153 + .ie_offset = (u8 []){ 0x30, 0x34, 0x38, 0x3c }, 154 + .dp_offset = (u8 []){ 0x40, 0x44, 0x48, 0x4c }, 155 + .gpa_offset = (u8 []){ 0x40, 0x44, 0xa4, 0xb8 }, 156 + .gpda_offset = (u8 []){ 0x54, 0x58, 0xa8, 0xbc}, 157 + .deb_offset = (u8 []){ 0x50 }, 158 + .deb_val = (u8 []){ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 }, 159 + .get_deb_setval = rtd1295_misc_gpio_get_deb_setval, 160 + }; 161 + 162 + static const struct rtd_gpio_info rtd1295_iso_gpio_info = { 163 + .name = "rtd1295_iso_gpio", 164 + .gpio_base = 101, 165 + .num_gpios = 35, 166 + .dir_offset = (u8 []){ 0x0, 0x18 }, 167 + .dato_offset = (u8 []){ 0x4, 0x1c }, 168 + .dati_offset = (u8 []){ 0x8, 0x20 }, 169 + .ie_offset = (u8 []){ 0xc, 0x24 }, 170 + .dp_offset = (u8 []){ 0x10, 0x28 }, 171 + .gpa_offset = (u8 []){ 0x8, 0xe0 }, 172 + .gpda_offset = (u8 []){ 0xc, 0xe4 }, 173 + .deb_offset = (u8 []){ 0x14 }, 174 + .deb_val = (u8 []){ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 }, 175 + .get_deb_setval = rtd1295_iso_gpio_get_deb_setval, 176 + }; 177 + 178 + static int rtd_gpio_dir_offset(struct rtd_gpio *data, unsigned int offset) 179 + { 180 + return data->info->dir_offset[offset / 32]; 181 + } 182 + 183 + static int rtd_gpio_dato_offset(struct rtd_gpio *data, unsigned int offset) 184 + { 185 + return data->info->dato_offset[offset / 32]; 186 + } 187 + 188 + static int rtd_gpio_dati_offset(struct rtd_gpio *data, unsigned int offset) 189 + { 190 + return data->info->dati_offset[offset / 32]; 191 + } 192 + 193 + static int rtd_gpio_ie_offset(struct rtd_gpio *data, unsigned int offset) 194 + { 195 + return data->info->ie_offset[offset / 32]; 196 + } 197 + 198 + static int rtd_gpio_dp_offset(struct rtd_gpio *data, unsigned int offset) 199 + { 200 + return data->info->dp_offset[offset / 32]; 201 + } 202 + 203 + 204 + static int rtd_gpio_gpa_offset(struct rtd_gpio *data, unsigned int offset) 205 + { 206 + /* Each GPIO assert interrupt status register contains 31 GPIOs. */ 207 + return data->info->gpa_offset[offset / 31]; 208 + } 209 + 210 + static int rtd_gpio_gpda_offset(struct rtd_gpio *data, unsigned int offset) 211 + { 212 + /* Each GPIO deassert interrupt status register contains 31 GPIOs. */ 213 + return data->info->gpda_offset[offset / 31]; 214 + } 215 + 216 + static int rtd_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset, 217 + unsigned int debounce) 218 + { 219 + struct rtd_gpio *data = gpiochip_get_data(chip); 220 + u8 deb_val, deb_index, reg_offset, shift; 221 + unsigned int write_en; 222 + u32 val; 223 + 224 + switch (debounce) { 225 + case 1: 226 + deb_index = RTD_GPIO_DEBOUNCE_1US; 227 + break; 228 + case 10: 229 + deb_index = RTD_GPIO_DEBOUNCE_10US; 230 + break; 231 + case 100: 232 + deb_index = RTD_GPIO_DEBOUNCE_100US; 233 + break; 234 + case 1000: 235 + deb_index = RTD_GPIO_DEBOUNCE_1MS; 236 + break; 237 + case 10000: 238 + deb_index = RTD_GPIO_DEBOUNCE_10MS; 239 + break; 240 + case 20000: 241 + deb_index = RTD_GPIO_DEBOUNCE_20MS; 242 + break; 243 + case 30000: 244 + deb_index = RTD_GPIO_DEBOUNCE_30MS; 245 + break; 246 + default: 247 + return -ENOTSUPP; 248 + } 249 + 250 + deb_val = data->info->get_deb_setval(data->info, offset, deb_index, &reg_offset, &shift); 251 + write_en = BIT(shift + 3); 252 + val = (deb_val << shift) | write_en; 253 + 254 + guard(raw_spinlock_irqsave)(&data->lock); 255 + writel_relaxed(val, data->base + reg_offset); 256 + 257 + return 0; 258 + } 259 + 260 + static int rtd_gpio_set_config(struct gpio_chip *chip, unsigned int offset, 261 + unsigned long config) 262 + { 263 + int debounce; 264 + 265 + switch (pinconf_to_config_param(config)) { 266 + case PIN_CONFIG_BIAS_DISABLE: 267 + case PIN_CONFIG_BIAS_PULL_UP: 268 + case PIN_CONFIG_BIAS_PULL_DOWN: 269 + return gpiochip_generic_config(chip, offset, config); 270 + case PIN_CONFIG_INPUT_DEBOUNCE: 271 + debounce = pinconf_to_config_argument(config); 272 + return rtd_gpio_set_debounce(chip, offset, debounce); 273 + default: 274 + return -ENOTSUPP; 275 + } 276 + } 277 + 278 + static void rtd_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) 279 + { 280 + struct rtd_gpio *data = gpiochip_get_data(chip); 281 + u32 mask = BIT(offset % 32); 282 + int dato_reg_offset; 283 + u32 val; 284 + 285 + dato_reg_offset = rtd_gpio_dato_offset(data, offset); 286 + 287 + guard(raw_spinlock_irqsave)(&data->lock); 288 + 289 + val = readl_relaxed(data->base + dato_reg_offset); 290 + if (value) 291 + val |= mask; 292 + else 293 + val &= ~mask; 294 + writel_relaxed(val, data->base + dato_reg_offset); 295 + } 296 + 297 + static int rtd_gpio_get(struct gpio_chip *chip, unsigned int offset) 298 + { 299 + struct rtd_gpio *data = gpiochip_get_data(chip); 300 + int dato_reg_offset = rtd_gpio_dato_offset(data, offset); 301 + int dati_reg_offset = rtd_gpio_dati_offset(data, offset); 302 + int dir_reg_offset = rtd_gpio_dir_offset(data, offset); 303 + int dat_reg_offset; 304 + u32 val; 305 + 306 + guard(raw_spinlock_irqsave)(&data->lock); 307 + 308 + val = readl_relaxed(data->base + dir_reg_offset); 309 + dat_reg_offset = (val & BIT(offset % 32)) ? dato_reg_offset : dati_reg_offset; 310 + val = readl_relaxed(data->base + dat_reg_offset); 311 + 312 + return !!(val & BIT(offset % 32)); 313 + } 314 + 315 + static int rtd_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 316 + { 317 + struct rtd_gpio *data = gpiochip_get_data(chip); 318 + int reg_offset; 319 + u32 val; 320 + 321 + reg_offset = rtd_gpio_dir_offset(data, offset); 322 + val = readl_relaxed(data->base + reg_offset); 323 + if (val & BIT(offset % 32)) 324 + return GPIO_LINE_DIRECTION_OUT; 325 + 326 + return GPIO_LINE_DIRECTION_IN; 327 + } 328 + 329 + static int rtd_gpio_set_direction(struct gpio_chip *chip, unsigned int offset, bool out) 330 + { 331 + struct rtd_gpio *data = gpiochip_get_data(chip); 332 + u32 mask = BIT(offset % 32); 333 + int reg_offset; 334 + u32 val; 335 + 336 + reg_offset = rtd_gpio_dir_offset(data, offset); 337 + 338 + guard(raw_spinlock_irqsave)(&data->lock); 339 + 340 + val = readl_relaxed(data->base + reg_offset); 341 + if (out) 342 + val |= mask; 343 + else 344 + val &= ~mask; 345 + writel_relaxed(val, data->base + reg_offset); 346 + 347 + return 0; 348 + } 349 + 350 + static int rtd_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) 351 + { 352 + return rtd_gpio_set_direction(chip, offset, false); 353 + } 354 + 355 + static int rtd_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, int value) 356 + { 357 + rtd_gpio_set(chip, offset, value); 358 + 359 + return rtd_gpio_set_direction(chip, offset, true); 360 + } 361 + 362 + static bool rtd_gpio_check_ie(struct rtd_gpio *data, int irq) 363 + { 364 + int mask = BIT(irq % 32); 365 + int ie_reg_offset; 366 + u32 enable; 367 + 368 + ie_reg_offset = rtd_gpio_ie_offset(data, irq); 369 + enable = readl_relaxed(data->base + ie_reg_offset); 370 + 371 + return enable & mask; 372 + } 373 + 374 + static void rtd_gpio_irq_handle(struct irq_desc *desc) 375 + { 376 + int (*get_reg_offset)(struct rtd_gpio *gpio, unsigned int offset); 377 + struct rtd_gpio *data = irq_desc_get_handler_data(desc); 378 + struct irq_domain *domain = data->gpio_chip.irq.domain; 379 + struct irq_chip *chip = irq_desc_get_chip(desc); 380 + unsigned int irq = irq_desc_get_irq(desc); 381 + unsigned long status; 382 + int reg_offset, i, j; 383 + unsigned int hwirq; 384 + 385 + if (irq == data->irqs[0]) 386 + get_reg_offset = &rtd_gpio_gpa_offset; 387 + else if (irq == data->irqs[1]) 388 + get_reg_offset = &rtd_gpio_gpda_offset; 389 + 390 + chained_irq_enter(chip, desc); 391 + 392 + /* Each GPIO interrupt status register contains 31 GPIOs. */ 393 + for (i = 0; i < data->info->num_gpios; i += 31) { 394 + reg_offset = get_reg_offset(data, i); 395 + 396 + /* 397 + * Bit 0 is the write_en bit, bit 0 to 31 corresponds to 31 GPIOs. 398 + * When bit 0 is set to 0, write 1 to the other bits to clear the status. 399 + * When bit 0 is set to 1, write 1 to the other bits to set the status. 400 + */ 401 + status = readl_relaxed(data->irq_base + reg_offset); 402 + status &= ~BIT(0); 403 + writel_relaxed(status, data->irq_base + reg_offset); 404 + 405 + for_each_set_bit(j, &status, 32) { 406 + hwirq = i + j - 1; 407 + if (rtd_gpio_check_ie(data, hwirq)) { 408 + int girq = irq_find_mapping(domain, hwirq); 409 + u32 irq_type = irq_get_trigger_type(girq); 410 + 411 + if ((irq == data->irqs[1]) && (irq_type != IRQ_TYPE_EDGE_BOTH)) 412 + break; 413 + generic_handle_domain_irq(domain, hwirq); 414 + } 415 + } 416 + } 417 + 418 + chained_irq_exit(chip, desc); 419 + } 420 + 421 + static void rtd_gpio_enable_irq(struct irq_data *d) 422 + { 423 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 424 + struct rtd_gpio *data = gpiochip_get_data(gc); 425 + irq_hw_number_t hwirq = irqd_to_hwirq(d); 426 + 427 + /* Bit 0 is write_en and bit 1 to 31 is correspond to 31 GPIOs. */ 428 + u32 clr_mask = BIT(hwirq % 31) << 1; 429 + 430 + u32 ie_mask = BIT(hwirq % 32); 431 + int gpda_reg_offset; 432 + int gpa_reg_offset; 433 + int ie_reg_offset; 434 + u32 val; 435 + 436 + ie_reg_offset = rtd_gpio_ie_offset(data, hwirq); 437 + gpa_reg_offset = rtd_gpio_gpa_offset(data, hwirq); 438 + gpda_reg_offset = rtd_gpio_gpda_offset(data, hwirq); 439 + 440 + gpiochip_enable_irq(gc, hwirq); 441 + 442 + guard(raw_spinlock_irqsave)(&data->lock); 443 + 444 + writel_relaxed(clr_mask, data->irq_base + gpa_reg_offset); 445 + writel_relaxed(clr_mask, data->irq_base + gpda_reg_offset); 446 + 447 + val = readl_relaxed(data->base + ie_reg_offset); 448 + val |= ie_mask; 449 + writel_relaxed(val, data->base + ie_reg_offset); 450 + } 451 + 452 + static void rtd_gpio_disable_irq(struct irq_data *d) 453 + { 454 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 455 + struct rtd_gpio *data = gpiochip_get_data(gc); 456 + irq_hw_number_t hwirq = irqd_to_hwirq(d); 457 + u32 ie_mask = BIT(hwirq % 32); 458 + int ie_reg_offset; 459 + u32 val; 460 + 461 + ie_reg_offset = rtd_gpio_ie_offset(data, hwirq); 462 + 463 + scoped_guard(raw_spinlock_irqsave, &data->lock) { 464 + val = readl_relaxed(data->base + ie_reg_offset); 465 + val &= ~ie_mask; 466 + writel_relaxed(val, data->base + ie_reg_offset); 467 + } 468 + 469 + gpiochip_disable_irq(gc, hwirq); 470 + } 471 + 472 + static int rtd_gpio_irq_set_type(struct irq_data *d, unsigned int type) 473 + { 474 + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 475 + struct rtd_gpio *data = gpiochip_get_data(gc); 476 + irq_hw_number_t hwirq = irqd_to_hwirq(d); 477 + u32 mask = BIT(hwirq % 32); 478 + int dp_reg_offset; 479 + bool polarity; 480 + u32 val; 481 + 482 + dp_reg_offset = rtd_gpio_dp_offset(data, hwirq); 483 + 484 + switch (type & IRQ_TYPE_SENSE_MASK) { 485 + case IRQ_TYPE_EDGE_RISING: 486 + polarity = 1; 487 + break; 488 + 489 + case IRQ_TYPE_EDGE_FALLING: 490 + polarity = 0; 491 + break; 492 + 493 + case IRQ_TYPE_EDGE_BOTH: 494 + polarity = 1; 495 + break; 496 + 497 + default: 498 + return -EINVAL; 499 + } 500 + 501 + scoped_guard(raw_spinlock_irqsave, &data->lock) { 502 + val = readl_relaxed(data->base + dp_reg_offset); 503 + if (polarity) 504 + val |= mask; 505 + else 506 + val &= ~mask; 507 + writel_relaxed(val, data->base + dp_reg_offset); 508 + } 509 + 510 + irq_set_handler_locked(d, handle_simple_irq); 511 + 512 + return 0; 513 + } 514 + 515 + static const struct irq_chip rtd_gpio_irq_chip = { 516 + .name = "rtd-gpio", 517 + .irq_enable = rtd_gpio_enable_irq, 518 + .irq_disable = rtd_gpio_disable_irq, 519 + .irq_set_type = rtd_gpio_irq_set_type, 520 + .flags = IRQCHIP_IMMUTABLE, 521 + }; 522 + 523 + static int rtd_gpio_probe(struct platform_device *pdev) 524 + { 525 + struct device *dev = &pdev->dev; 526 + struct gpio_irq_chip *irq_chip; 527 + struct rtd_gpio *data; 528 + 529 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 530 + if (!data) 531 + return -ENOMEM; 532 + 533 + data->irqs[0] = platform_get_irq(pdev, 0); 534 + if (data->irqs[0] < 0) 535 + return data->irqs[0]; 536 + 537 + data->irqs[1] = platform_get_irq(pdev, 1); 538 + if (data->irqs[1] < 0) 539 + return data->irqs[1]; 540 + 541 + data->info = device_get_match_data(dev); 542 + if (!data->info) 543 + return -EINVAL; 544 + 545 + raw_spin_lock_init(&data->lock); 546 + 547 + data->base = devm_platform_ioremap_resource(pdev, 0); 548 + if (IS_ERR(data->base)) 549 + return PTR_ERR(data->base); 550 + 551 + data->irq_base = devm_platform_ioremap_resource(pdev, 1); 552 + if (IS_ERR(data->irq_base)) 553 + return PTR_ERR(data->irq_base); 554 + 555 + data->gpio_chip.label = dev_name(dev); 556 + data->gpio_chip.base = -1; 557 + data->gpio_chip.ngpio = data->info->num_gpios; 558 + data->gpio_chip.request = gpiochip_generic_request; 559 + data->gpio_chip.free = gpiochip_generic_free; 560 + data->gpio_chip.get_direction = rtd_gpio_get_direction; 561 + data->gpio_chip.direction_input = rtd_gpio_direction_input; 562 + data->gpio_chip.direction_output = rtd_gpio_direction_output; 563 + data->gpio_chip.set = rtd_gpio_set; 564 + data->gpio_chip.get = rtd_gpio_get; 565 + data->gpio_chip.set_config = rtd_gpio_set_config; 566 + data->gpio_chip.parent = dev; 567 + 568 + irq_chip = &data->gpio_chip.irq; 569 + irq_chip->handler = handle_bad_irq; 570 + irq_chip->default_type = IRQ_TYPE_NONE; 571 + irq_chip->parent_handler = rtd_gpio_irq_handle; 572 + irq_chip->parent_handler_data = data; 573 + irq_chip->num_parents = 2; 574 + irq_chip->parents = data->irqs; 575 + 576 + gpio_irq_chip_set_chip(irq_chip, &rtd_gpio_irq_chip); 577 + 578 + return devm_gpiochip_add_data(dev, &data->gpio_chip, data); 579 + } 580 + 581 + static const struct of_device_id rtd_gpio_of_matches[] = { 582 + { .compatible = "realtek,rtd1295-misc-gpio", .data = &rtd1295_misc_gpio_info }, 583 + { .compatible = "realtek,rtd1295-iso-gpio", .data = &rtd1295_iso_gpio_info }, 584 + { .compatible = "realtek,rtd1395-iso-gpio", .data = &rtd1395_iso_gpio_info }, 585 + { .compatible = "realtek,rtd1619-iso-gpio", .data = &rtd1619_iso_gpio_info }, 586 + { .compatible = "realtek,rtd1319-iso-gpio", .data = &rtd_iso_gpio_info }, 587 + { .compatible = "realtek,rtd1619b-iso-gpio", .data = &rtd_iso_gpio_info }, 588 + { .compatible = "realtek,rtd1319d-iso-gpio", .data = &rtd_iso_gpio_info }, 589 + { .compatible = "realtek,rtd1315e-iso-gpio", .data = &rtd_iso_gpio_info }, 590 + { } 591 + }; 592 + MODULE_DEVICE_TABLE(of, rtd_gpio_of_matches); 593 + 594 + static struct platform_driver rtd_gpio_platform_driver = { 595 + .driver = { 596 + .name = "gpio-rtd", 597 + .of_match_table = rtd_gpio_of_matches, 598 + }, 599 + .probe = rtd_gpio_probe, 600 + }; 601 + module_platform_driver(rtd_gpio_platform_driver); 602 + 603 + MODULE_DESCRIPTION("Realtek DHC SoC gpio driver"); 604 + MODULE_LICENSE("GPL v2");
-1
drivers/gpio/gpio-sifive.c
··· 250 250 girq->handler = handle_bad_irq; 251 251 girq->default_type = IRQ_TYPE_NONE; 252 252 253 - platform_set_drvdata(pdev, chip); 254 253 return gpiochip_add_data(&chip->gc, chip); 255 254 } 256 255
+23 -1
drivers/gpio/gpio-sim.c
··· 20 20 #include <linux/interrupt.h> 21 21 #include <linux/irq.h> 22 22 #include <linux/irq_sim.h> 23 + #include <linux/kernel.h> 23 24 #include <linux/list.h> 24 25 #include <linux/minmax.h> 25 26 #include <linux/mod_devicetable.h> ··· 29 28 #include <linux/notifier.h> 30 29 #include <linux/platform_device.h> 31 30 #include <linux/property.h> 31 + #include <linux/seq_file.h> 32 32 #include <linux/slab.h> 33 33 #include <linux/string.h> 34 34 #include <linux/string_helpers.h> ··· 224 222 !!test_bit(offset, chip->pull_map)); 225 223 __clear_bit(offset, chip->request_map); 226 224 } 225 + } 226 + 227 + static void gpio_sim_dbg_show(struct seq_file *seq, struct gpio_chip *gc) 228 + { 229 + struct gpio_sim_chip *chip = gpiochip_get_data(gc); 230 + const char *label; 231 + int i; 232 + 233 + guard(mutex)(&chip->lock); 234 + 235 + for_each_requested_gpio(gc, i, label) 236 + seq_printf(seq, " gpio-%-3d (%s) %s,%s\n", 237 + gc->base + i, 238 + label, 239 + test_bit(i, chip->direction_map) ? "input" : 240 + test_bit(i, chip->value_map) ? "output-high" : 241 + "output-low", 242 + test_bit(i, chip->pull_map) ? "pull-up" : 243 + "pull-down"); 227 244 } 228 245 229 246 static ssize_t gpio_sim_sysfs_val_show(struct device *dev, ··· 481 460 gc->to_irq = gpio_sim_to_irq; 482 461 gc->request = gpio_sim_request; 483 462 gc->free = gpio_sim_free; 463 + gc->dbg_show = PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), gpio_sim_dbg_show); 484 464 gc->can_sleep = true; 485 465 486 466 ret = devm_gpiochip_add_data(dev, gc, chip); ··· 1568 1546 } 1569 1547 module_exit(gpio_sim_exit); 1570 1548 1571 - MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl"); 1549 + MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); 1572 1550 MODULE_DESCRIPTION("GPIO Simulator Module"); 1573 1551 MODULE_LICENSE("GPL");
+5 -1
drivers/gpio/gpio-stmpe.c
··· 5 5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson 6 6 */ 7 7 8 + #include <linux/cleanup.h> 8 9 #include <linux/init.h> 9 10 #include <linux/platform_device.h> 10 11 #include <linux/slab.h> ··· 256 255 { 257 256 struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc); 258 257 struct stmpe *stmpe = stmpe_gpio->stmpe; 259 - const char *label = gpiochip_is_requested(gc, offset); 260 258 bool val = !!stmpe_gpio_get(gc, offset); 261 259 u8 bank = offset / 8; 262 260 u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank]; 263 261 u8 mask = BIT(offset % 8); 264 262 int ret; 265 263 u8 dir; 264 + 265 + char *label __free(kfree) = gpiochip_dup_line_label(gc, offset); 266 + if (IS_ERR(label)) 267 + return; 266 268 267 269 ret = stmpe_reg_read(stmpe, dir_reg); 268 270 if (ret < 0)
+19 -44
drivers/gpio/gpio-tangier.c
··· 10 10 */ 11 11 12 12 #include <linux/bitops.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/device.h> 14 15 #include <linux/errno.h> 15 16 #include <linux/export.h> ··· 20 19 #include <linux/math.h> 21 20 #include <linux/module.h> 22 21 #include <linux/pinctrl/pinconf-generic.h> 22 + #include <linux/pm.h> 23 23 #include <linux/spinlock.h> 24 24 #include <linux/string_helpers.h> 25 25 #include <linux/types.h> ··· 93 91 static void tng_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) 94 92 { 95 93 struct tng_gpio *priv = gpiochip_get_data(chip); 96 - unsigned long flags; 97 94 void __iomem *reg; 98 95 u8 shift; 99 96 100 97 reg = gpio_reg_and_bit(chip, offset, value ? GPSR : GPCR, &shift); 101 98 102 - raw_spin_lock_irqsave(&priv->lock, flags); 99 + guard(raw_spinlock_irqsave)(&priv->lock); 103 100 104 101 writel(BIT(shift), reg); 105 - 106 - raw_spin_unlock_irqrestore(&priv->lock, flags); 107 102 } 108 103 109 104 static int tng_gpio_direction_input(struct gpio_chip *chip, unsigned int offset) 110 105 { 111 106 struct tng_gpio *priv = gpiochip_get_data(chip); 112 - unsigned long flags; 113 107 void __iomem *gpdr; 114 108 u32 value; 115 109 u8 shift; 116 110 117 111 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift); 118 112 119 - raw_spin_lock_irqsave(&priv->lock, flags); 113 + guard(raw_spinlock_irqsave)(&priv->lock); 120 114 121 115 value = readl(gpdr); 122 116 value &= ~BIT(shift); 123 117 writel(value, gpdr); 124 - 125 - raw_spin_unlock_irqrestore(&priv->lock, flags); 126 118 127 119 return 0; 128 120 } ··· 125 129 int value) 126 130 { 127 131 struct tng_gpio *priv = gpiochip_get_data(chip); 128 - unsigned long flags; 129 132 void __iomem *gpdr; 130 133 u8 shift; 131 134 132 135 gpdr = gpio_reg_and_bit(chip, offset, GPDR, &shift); 133 136 tng_gpio_set(chip, offset, value); 134 137 135 - raw_spin_lock_irqsave(&priv->lock, flags); 138 + guard(raw_spinlock_irqsave)(&priv->lock); 136 139 137 140 value = readl(gpdr); 138 141 value |= BIT(shift); 139 142 writel(value, gpdr); 140 - 141 - raw_spin_unlock_irqrestore(&priv->lock, flags); 142 143 143 144 return 0; 144 145 } ··· 157 164 unsigned int debounce) 158 165 { 159 166 struct tng_gpio *priv = gpiochip_get_data(chip); 160 - unsigned long flags; 161 167 void __iomem *gfbr; 162 168 u32 value; 163 169 u8 shift; 164 170 165 171 gfbr = gpio_reg_and_bit(chip, offset, GFBR, &shift); 166 172 167 - raw_spin_lock_irqsave(&priv->lock, flags); 173 + guard(raw_spinlock_irqsave)(&priv->lock); 168 174 169 175 value = readl(gfbr); 170 176 if (debounce) ··· 171 179 else 172 180 value |= BIT(shift); 173 181 writel(value, gfbr); 174 - 175 - raw_spin_unlock_irqrestore(&priv->lock, flags); 176 182 177 183 return 0; 178 184 } ··· 197 207 { 198 208 struct tng_gpio *priv = irq_data_get_irq_chip_data(d); 199 209 irq_hw_number_t gpio = irqd_to_hwirq(d); 200 - unsigned long flags; 201 210 void __iomem *gisr; 202 211 u8 shift; 203 212 204 213 gisr = gpio_reg_and_bit(&priv->chip, gpio, GISR, &shift); 205 214 206 - raw_spin_lock_irqsave(&priv->lock, flags); 215 + guard(raw_spinlock_irqsave)(&priv->lock); 216 + 207 217 writel(BIT(shift), gisr); 208 - raw_spin_unlock_irqrestore(&priv->lock, flags); 209 218 } 210 219 211 220 static void tng_irq_unmask_mask(struct tng_gpio *priv, u32 gpio, bool unmask) 212 221 { 213 - unsigned long flags; 214 222 void __iomem *gimr; 215 223 u32 value; 216 224 u8 shift; 217 225 218 226 gimr = gpio_reg_and_bit(&priv->chip, gpio, GIMR, &shift); 219 227 220 - raw_spin_lock_irqsave(&priv->lock, flags); 228 + guard(raw_spinlock_irqsave)(&priv->lock); 221 229 222 230 value = readl(gimr); 223 231 if (unmask) ··· 223 235 else 224 236 value &= ~BIT(shift); 225 237 writel(value, gimr); 226 - 227 - raw_spin_unlock_irqrestore(&priv->lock, flags); 228 238 } 229 239 230 240 static void tng_irq_mask(struct irq_data *d) ··· 253 267 void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR); 254 268 void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR); 255 269 u8 shift = gpio % 32; 256 - unsigned long flags; 257 270 u32 value; 258 271 259 - raw_spin_lock_irqsave(&priv->lock, flags); 272 + guard(raw_spinlock_irqsave)(&priv->lock); 260 273 261 274 value = readl(grer); 262 275 if (type & IRQ_TYPE_EDGE_RISING) ··· 296 311 irq_set_handler_locked(d, handle_edge_irq); 297 312 } 298 313 299 - raw_spin_unlock_irqrestore(&priv->lock, flags); 300 - 301 314 return 0; 302 315 } 303 316 ··· 307 324 void __iomem *gwmr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwmr); 308 325 void __iomem *gwsr = gpio_reg(&priv->chip, gpio, priv->wake_regs.gwsr); 309 326 u8 shift = gpio % 32; 310 - unsigned long flags; 311 327 u32 value; 312 328 313 - raw_spin_lock_irqsave(&priv->lock, flags); 329 + dev_dbg(priv->dev, "%s wake for gpio %lu\n", str_enable_disable(on), gpio); 330 + 331 + guard(raw_spinlock_irqsave)(&priv->lock); 314 332 315 333 /* Clear the existing wake status */ 316 334 writel(BIT(shift), gwsr); ··· 323 339 value &= ~BIT(shift); 324 340 writel(value, gwmr); 325 341 326 - raw_spin_unlock_irqrestore(&priv->lock, flags); 327 - 328 - dev_dbg(priv->dev, "%s wake for gpio %lu\n", str_enable_disable(on), gpio); 329 342 return 0; 330 343 } 331 344 ··· 458 477 } 459 478 EXPORT_SYMBOL_NS_GPL(devm_tng_gpio_probe, GPIO_TANGIER); 460 479 461 - int tng_gpio_suspend(struct device *dev) 480 + static int tng_gpio_suspend(struct device *dev) 462 481 { 463 482 struct tng_gpio *priv = dev_get_drvdata(dev); 464 483 struct tng_gpio_context *ctx = priv->ctx; 465 - unsigned long flags; 466 484 unsigned int base; 467 485 468 - raw_spin_lock_irqsave(&priv->lock, flags); 486 + guard(raw_spinlock_irqsave)(&priv->lock); 469 487 470 488 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) { 471 489 /* GPLR is RO, values read will be restored using GPSR */ ··· 478 498 ctx->gwmr = readl(gpio_reg(&priv->chip, base, priv->wake_regs.gwmr)); 479 499 } 480 500 481 - raw_spin_unlock_irqrestore(&priv->lock, flags); 482 - 483 501 return 0; 484 502 } 485 - EXPORT_SYMBOL_NS_GPL(tng_gpio_suspend, GPIO_TANGIER); 486 503 487 - int tng_gpio_resume(struct device *dev) 504 + static int tng_gpio_resume(struct device *dev) 488 505 { 489 506 struct tng_gpio *priv = dev_get_drvdata(dev); 490 507 struct tng_gpio_context *ctx = priv->ctx; 491 - unsigned long flags; 492 508 unsigned int base; 493 509 494 - raw_spin_lock_irqsave(&priv->lock, flags); 510 + guard(raw_spinlock_irqsave)(&priv->lock); 495 511 496 512 for (base = 0; base < priv->chip.ngpio; base += 32, ctx++) { 497 513 /* GPLR is RO, values read will be restored using GPSR */ ··· 501 525 writel(ctx->gwmr, gpio_reg(&priv->chip, base, priv->wake_regs.gwmr)); 502 526 } 503 527 504 - raw_spin_unlock_irqrestore(&priv->lock, flags); 505 - 506 528 return 0; 507 529 } 508 - EXPORT_SYMBOL_NS_GPL(tng_gpio_resume, GPIO_TANGIER); 530 + 531 + EXPORT_NS_GPL_SIMPLE_DEV_PM_OPS(tng_gpio_pm_ops, tng_gpio_suspend, tng_gpio_resume, GPIO_TANGIER); 509 532 510 533 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>"); 511 534 MODULE_AUTHOR("Pandith N <pandith.n@intel.com>");
+2 -2
drivers/gpio/gpio-tangier.h
··· 13 13 #define _GPIO_TANGIER_H_ 14 14 15 15 #include <linux/gpio/driver.h> 16 + #include <linux/pm.h> 16 17 #include <linux/spinlock_types.h> 17 18 #include <linux/types.h> 18 19 ··· 112 111 113 112 int devm_tng_gpio_probe(struct device *dev, struct tng_gpio *gpio); 114 113 115 - int tng_gpio_suspend(struct device *dev); 116 - int tng_gpio_resume(struct device *dev); 114 + extern const struct dev_pm_ops tng_gpio_pm_ops; 117 115 118 116 #endif /* _GPIO_TANGIER_H_ */
+9 -9
drivers/gpio/gpio-tps65219.c
··· 96 96 * Below can be used for test purpose only. 97 97 */ 98 98 99 - if (IS_ENABLED(CONFIG_DEBUG_GPIO)) { 100 - int ret = regmap_update_bits(gpio->tps->regmap, TPS65219_REG_MFP_1_CONFIG, 101 - TPS65219_GPIO0_DIR_MASK, direction); 102 - if (ret) { 103 - dev_err(dev, 104 - "GPIO DEBUG enabled: Fail to change direction to %u for GPIO%d.\n", 105 - direction, offset); 106 - return ret; 107 - } 99 + #if 0 100 + int ret = regmap_update_bits(gpio->tps->regmap, TPS65219_REG_MFP_1_CONFIG, 101 + TPS65219_GPIO0_DIR_MASK, direction); 102 + if (ret) { 103 + dev_err(dev, 104 + "GPIO DEBUG enabled: Fail to change direction to %u for GPIO%d.\n", 105 + direction, offset); 106 + return ret; 108 107 } 108 + #endif 109 109 110 110 dev_err(dev, 111 111 "GPIO%d direction set by NVM, change to %u failed, not allowed by specification\n",
+9 -5
drivers/gpio/gpio-wm831x.c
··· 8 8 * 9 9 */ 10 10 11 + #include <linux/cleanup.h> 11 12 #include <linux/kernel.h> 12 13 #include <linux/slab.h> 13 14 #include <linux/module.h> ··· 161 160 for (i = 0; i < chip->ngpio; i++) { 162 161 int gpio = i + chip->base; 163 162 int reg; 164 - const char *label, *pull, *powerdomain; 163 + const char *pull, *powerdomain; 165 164 166 165 /* We report the GPIO even if it's not requested since 167 166 * we're also reporting things like alternate 168 167 * functions which apply even when the GPIO is not in 169 168 * use as a GPIO. 170 169 */ 171 - label = gpiochip_is_requested(chip, i); 172 - if (!label) 173 - label = "Unrequested"; 170 + char *label __free(kfree) = gpiochip_dup_line_label(chip, i); 171 + if (IS_ERR(label)) { 172 + dev_err(wm831x->dev, "Failed to duplicate label\n"); 173 + continue; 174 + } 174 175 175 - seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); 176 + seq_printf(s, " gpio-%-3d (%-20.20s) ", 177 + gpio, label ?: "Unrequested"); 176 178 177 179 reg = wm831x_reg_read(wm831x, WM831X_GPIO1_CONTROL + i); 178 180 if (reg < 0) {
+8 -5
drivers/gpio/gpio-wm8994.c
··· 8 8 * 9 9 */ 10 10 11 + #include <linux/cleanup.h> 11 12 #include <linux/kernel.h> 12 13 #include <linux/slab.h> 13 14 #include <linux/module.h> ··· 194 193 for (i = 0; i < chip->ngpio; i++) { 195 194 int gpio = i + chip->base; 196 195 int reg; 197 - const char *label; 198 196 199 197 /* We report the GPIO even if it's not requested since 200 198 * we're also reporting things like alternate 201 199 * functions which apply even when the GPIO is not in 202 200 * use as a GPIO. 203 201 */ 204 - label = gpiochip_is_requested(chip, i); 205 - if (!label) 206 - label = "Unrequested"; 202 + char *label __free(kfree) = gpiochip_dup_line_label(chip, i); 203 + if (IS_ERR(label)) { 204 + dev_err(wm8994->dev, "Failed to duplicate label\n"); 205 + continue; 206 + } 207 207 208 - seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, label); 208 + seq_printf(s, " gpio-%-3d (%-20.20s) ", gpio, 209 + label ?: "Unrequested"); 209 210 210 211 reg = wm8994_reg_read(wm8994, WM8994_GPIO_1 + i); 211 212 if (reg < 0) {
-1
drivers/gpio/gpio-xilinx.c
··· 52 52 * @dir: GPIO direction shadow register 53 53 * @gpio_lock: Lock used for synchronization 54 54 * @irq: IRQ used by GPIO device 55 - * @irqchip: IRQ chip 56 55 * @enable: GPIO IRQ enable/disable bitfield 57 56 * @rising_edge: GPIO IRQ rising edge enable/disable bitfield 58 57 * @falling_edge: GPIO IRQ falling edge enable/disable bitfield
+326 -341
drivers/gpio/gpiolib-cdev.c
··· 5 5 #include <linux/bitmap.h> 6 6 #include <linux/build_bug.h> 7 7 #include <linux/cdev.h> 8 + #include <linux/cleanup.h> 8 9 #include <linux/compat.h> 9 10 #include <linux/compiler.h> 10 11 #include <linux/device.h> ··· 20 19 #include <linux/kfifo.h> 21 20 #include <linux/module.h> 22 21 #include <linux/mutex.h> 22 + #include <linux/overflow.h> 23 23 #include <linux/pinctrl/consumer.h> 24 24 #include <linux/poll.h> 25 + #include <linux/rbtree.h> 26 + #include <linux/rwsem.h> 25 27 #include <linux/seq_file.h> 26 28 #include <linux/spinlock.h> 27 29 #include <linux/timekeeping.h> ··· 65 61 typedef long (*ioctl_fn)(struct file *, unsigned int, unsigned long); 66 62 typedef ssize_t (*read_fn)(struct file *, char __user *, 67 63 size_t count, loff_t *); 68 - 69 - static __poll_t call_poll_locked(struct file *file, 70 - struct poll_table_struct *wait, 71 - struct gpio_device *gdev, poll_fn func) 72 - { 73 - __poll_t ret; 74 - 75 - down_read(&gdev->sem); 76 - ret = func(file, wait); 77 - up_read(&gdev->sem); 78 - 79 - return ret; 80 - } 81 - 82 - static long call_ioctl_locked(struct file *file, unsigned int cmd, 83 - unsigned long arg, struct gpio_device *gdev, 84 - ioctl_fn func) 85 - { 86 - long ret; 87 - 88 - down_read(&gdev->sem); 89 - ret = func(file, cmd, arg); 90 - up_read(&gdev->sem); 91 - 92 - return ret; 93 - } 94 - 95 - static ssize_t call_read_locked(struct file *file, char __user *buf, 96 - size_t count, loff_t *f_ps, 97 - struct gpio_device *gdev, read_fn func) 98 - { 99 - ssize_t ret; 100 - 101 - down_read(&gdev->sem); 102 - ret = func(file, buf, count, f_ps); 103 - up_read(&gdev->sem); 104 - 105 - return ret; 106 - } 107 64 108 65 /* 109 66 * GPIO line handle management ··· 200 235 return 0; 201 236 } 202 237 203 - static long linehandle_ioctl_unlocked(struct file *file, unsigned int cmd, 204 - unsigned long arg) 238 + static long linehandle_ioctl(struct file *file, unsigned int cmd, 239 + unsigned long arg) 205 240 { 206 241 struct linehandle_state *lh = file->private_data; 207 242 void __user *ip = (void __user *)arg; ··· 209 244 DECLARE_BITMAP(vals, GPIOHANDLES_MAX); 210 245 unsigned int i; 211 246 int ret; 247 + 248 + guard(rwsem_read)(&lh->gdev->sem); 212 249 213 250 if (!lh->gdev->chip) 214 251 return -ENODEV; ··· 259 292 default: 260 293 return -EINVAL; 261 294 } 262 - } 263 - 264 - static long linehandle_ioctl(struct file *file, unsigned int cmd, 265 - unsigned long arg) 266 - { 267 - struct linehandle_state *lh = file->private_data; 268 - 269 - return call_ioctl_locked(file, cmd, arg, lh->gdev, 270 - linehandle_ioctl_unlocked); 271 295 } 272 296 273 297 #ifdef CONFIG_COMPAT ··· 419 461 420 462 /** 421 463 * struct line - contains the state of a requested line 464 + * @node: to store the object in supinfo_tree if supplemental 422 465 * @desc: the GPIO descriptor for this line. 423 466 * @req: the corresponding line request 424 467 * @irq: the interrupt triggered in response to events on this GPIO ··· 432 473 * @line_seqno: the seqno for the current edge event in the sequence of 433 474 * events for this line. 434 475 * @work: the worker that implements software debouncing 476 + * @debounce_period_us: the debounce period in microseconds 435 477 * @sw_debounced: flag indicating if the software debouncer is active 436 478 * @level: the current debounced physical level of the line 437 479 * @hdesc: the Hardware Timestamp Engine (HTE) descriptor ··· 441 481 * @last_seqno: the last sequence number before debounce period expires 442 482 */ 443 483 struct line { 484 + struct rb_node node; 444 485 struct gpio_desc *desc; 445 486 /* 446 487 * -- edge detector specific fields -- ··· 476 515 */ 477 516 struct delayed_work work; 478 517 /* 518 + * debounce_period_us is accessed by debounce_irq_handler() and 519 + * process_hw_ts() which are disabled when modified by 520 + * debounce_setup(), edge_detector_setup() or edge_detector_stop() 521 + * or can live with a stale version when updated by 522 + * edge_detector_update(). 523 + * The modifying functions are themselves mutually exclusive. 524 + */ 525 + unsigned int debounce_period_us; 526 + /* 479 527 * sw_debounce is accessed by linereq_set_config(), which is the 480 528 * only setter, and linereq_get_values(), which can live with a 481 529 * slightly stale value. ··· 516 546 #endif /* CONFIG_HTE */ 517 547 }; 518 548 549 + /* 550 + * a rbtree of the struct lines containing supplemental info. 551 + * Used to populate gpio_v2_line_info with cdev specific fields not contained 552 + * in the struct gpio_desc. 553 + * A line is determined to contain supplemental information by 554 + * line_has_supinfo(). 555 + */ 556 + static struct rb_root supinfo_tree = RB_ROOT; 557 + /* covers supinfo_tree */ 558 + static DEFINE_SPINLOCK(supinfo_lock); 559 + 519 560 /** 520 561 * struct linereq - contains the state of a userspace line request 521 562 * @gdev: the GPIO device the line request pertains to ··· 540 559 * this line request. Note that this is not used when @num_lines is 1, as 541 560 * the line_seqno is then the same and is cheaper to calculate. 542 561 * @config_mutex: mutex for serializing ioctl() calls to ensure consistency 543 - * of configuration, particularly multi-step accesses to desc flags. 562 + * of configuration, particularly multi-step accesses to desc flags and 563 + * changes to supinfo status. 544 564 * @lines: the lines held by this line request, with @num_lines elements. 545 565 */ 546 566 struct linereq { ··· 556 574 struct mutex config_mutex; 557 575 struct line lines[] __counted_by(num_lines); 558 576 }; 577 + 578 + static void supinfo_insert(struct line *line) 579 + { 580 + struct rb_node **new = &(supinfo_tree.rb_node), *parent = NULL; 581 + struct line *entry; 582 + 583 + guard(spinlock)(&supinfo_lock); 584 + 585 + while (*new) { 586 + entry = container_of(*new, struct line, node); 587 + 588 + parent = *new; 589 + if (line->desc < entry->desc) { 590 + new = &((*new)->rb_left); 591 + } else if (line->desc > entry->desc) { 592 + new = &((*new)->rb_right); 593 + } else { 594 + /* this should never happen */ 595 + WARN(1, "duplicate line inserted"); 596 + return; 597 + } 598 + } 599 + 600 + rb_link_node(&line->node, parent, new); 601 + rb_insert_color(&line->node, &supinfo_tree); 602 + } 603 + 604 + static void supinfo_erase(struct line *line) 605 + { 606 + guard(spinlock)(&supinfo_lock); 607 + 608 + rb_erase(&line->node, &supinfo_tree); 609 + } 610 + 611 + static struct line *supinfo_find(struct gpio_desc *desc) 612 + { 613 + struct rb_node *node = supinfo_tree.rb_node; 614 + struct line *line; 615 + 616 + while (node) { 617 + line = container_of(node, struct line, node); 618 + if (desc < line->desc) 619 + node = node->rb_left; 620 + else if (desc > line->desc) 621 + node = node->rb_right; 622 + else 623 + return line; 624 + } 625 + return NULL; 626 + } 627 + 628 + static void supinfo_to_lineinfo(struct gpio_desc *desc, 629 + struct gpio_v2_line_info *info) 630 + { 631 + struct gpio_v2_line_attribute *attr; 632 + struct line *line; 633 + 634 + guard(spinlock)(&supinfo_lock); 635 + 636 + line = supinfo_find(desc); 637 + if (!line) 638 + return; 639 + 640 + attr = &info->attrs[info->num_attrs]; 641 + attr->id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 642 + attr->debounce_period_us = READ_ONCE(line->debounce_period_us); 643 + info->num_attrs++; 644 + } 645 + 646 + static inline bool line_has_supinfo(struct line *line) 647 + { 648 + return READ_ONCE(line->debounce_period_us); 649 + } 650 + 651 + /* 652 + * Checks line_has_supinfo() before and after the change to avoid unnecessary 653 + * supinfo_tree access. 654 + * Called indirectly by linereq_create() or linereq_set_config() so line 655 + * is already protected from concurrent changes. 656 + */ 657 + static void line_set_debounce_period(struct line *line, 658 + unsigned int debounce_period_us) 659 + { 660 + bool was_suppl = line_has_supinfo(line); 661 + 662 + WRITE_ONCE(line->debounce_period_us, debounce_period_us); 663 + 664 + /* if supinfo status is unchanged then we're done */ 665 + if (line_has_supinfo(line) == was_suppl) 666 + return; 667 + 668 + /* supinfo status has changed, so update the tree */ 669 + if (was_suppl) 670 + supinfo_erase(line); 671 + else 672 + supinfo_insert(line); 673 + } 559 674 560 675 #define GPIO_V2_LINE_BIAS_FLAGS \ 561 676 (GPIO_V2_LINE_FLAG_BIAS_PULL_UP | \ ··· 704 625 { 705 626 bool overflow = false; 706 627 707 - spin_lock(&lr->wait.lock); 708 - if (kfifo_is_full(&lr->events)) { 709 - overflow = true; 710 - kfifo_skip(&lr->events); 628 + scoped_guard(spinlock, &lr->wait.lock) { 629 + if (kfifo_is_full(&lr->events)) { 630 + overflow = true; 631 + kfifo_skip(&lr->events); 632 + } 633 + kfifo_in(&lr->events, le, 1); 711 634 } 712 - kfifo_in(&lr->events, le, 1); 713 - spin_unlock(&lr->wait.lock); 714 635 if (!overflow) 715 636 wake_up_poll(&lr->wait, EPOLLIN); 716 637 else ··· 802 723 line->total_discard_seq++; 803 724 line->last_seqno = ts->seq; 804 725 mod_delayed_work(system_wq, &line->work, 805 - usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 726 + usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); 806 727 } else { 807 728 if (unlikely(ts->seq < line->line_seqno)) 808 729 return HTE_CB_HANDLED; ··· 943 864 struct line *line = p; 944 865 945 866 mod_delayed_work(system_wq, &line->work, 946 - usecs_to_jiffies(READ_ONCE(line->desc->debounce_period_us))); 867 + usecs_to_jiffies(READ_ONCE(line->debounce_period_us))); 947 868 948 869 return IRQ_HANDLED; 949 870 } ··· 1025 946 /* try hardware */ 1026 947 ret = gpiod_set_debounce(line->desc, debounce_period_us); 1027 948 if (!ret) { 1028 - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 949 + line_set_debounce_period(line, debounce_period_us); 1029 950 return ret; 1030 951 } 1031 952 if (ret != -ENOTSUPP) ··· 1104 1025 cancel_delayed_work_sync(&line->work); 1105 1026 WRITE_ONCE(line->sw_debounced, 0); 1106 1027 WRITE_ONCE(line->edflags, 0); 1107 - if (line->desc) 1108 - WRITE_ONCE(line->desc->debounce_period_us, 0); 1028 + line_set_debounce_period(line, 0); 1109 1029 /* do not change line->level - see comment in debounced_value() */ 1110 1030 } 1111 1031 ··· 1129 1051 ret = debounce_setup(line, debounce_period_us); 1130 1052 if (ret) 1131 1053 return ret; 1132 - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 1054 + line_set_debounce_period(line, debounce_period_us); 1133 1055 } 1134 1056 1135 1057 /* detection disabled or sw debouncer will provide edge detection */ ··· 1171 1093 gpio_v2_line_config_debounce_period(lc, line_idx); 1172 1094 1173 1095 if ((active_edflags == edflags) && 1174 - (READ_ONCE(line->desc->debounce_period_us) == debounce_period_us)) 1096 + (READ_ONCE(line->debounce_period_us) == debounce_period_us)) 1175 1097 return 0; 1176 1098 1177 1099 /* sw debounced and still will be...*/ 1178 1100 if (debounce_period_us && READ_ONCE(line->sw_debounced)) { 1179 - WRITE_ONCE(line->desc->debounce_period_us, debounce_period_us); 1101 + line_set_debounce_period(line, debounce_period_us); 1180 1102 return 0; 1181 1103 } 1182 1104 ··· 1350 1272 if (copy_from_user(&lv, ip, sizeof(lv))) 1351 1273 return -EFAULT; 1352 1274 1275 + /* 1276 + * gpiod_get_array_value_complex() requires compacted desc and val 1277 + * arrays, rather than the sparse ones in lv. 1278 + * Calculation of num_get and construction of the desc array is 1279 + * optimized to avoid allocation for the desc array for the common 1280 + * num_get == 1 case. 1281 + */ 1282 + /* scan requested lines to calculate the subset to get */ 1353 1283 for (num_get = 0, i = 0; i < lr->num_lines; i++) { 1354 1284 if (lv.mask & BIT_ULL(i)) { 1355 1285 num_get++; 1286 + /* capture desc for the num_get == 1 case */ 1356 1287 descs = &lr->lines[i].desc; 1357 1288 } 1358 1289 } ··· 1370 1283 return -EINVAL; 1371 1284 1372 1285 if (num_get != 1) { 1286 + /* build compacted desc array */ 1373 1287 descs = kmalloc_array(num_get, sizeof(*descs), GFP_KERNEL); 1374 1288 if (!descs) 1375 1289 return -ENOMEM; ··· 1391 1303 1392 1304 lv.bits = 0; 1393 1305 for (didx = 0, i = 0; i < lr->num_lines; i++) { 1306 + /* unpack compacted vals for the response */ 1394 1307 if (lv.mask & BIT_ULL(i)) { 1395 1308 if (lr->lines[i].sw_debounced) 1396 1309 val = debounced_value(&lr->lines[i]); ··· 1409 1320 return 0; 1410 1321 } 1411 1322 1412 - static long linereq_set_values_unlocked(struct linereq *lr, 1413 - struct gpio_v2_line_values *lv) 1323 + static long linereq_set_values(struct linereq *lr, void __user *ip) 1414 1324 { 1415 1325 DECLARE_BITMAP(vals, GPIO_V2_LINES_MAX); 1326 + struct gpio_v2_line_values lv; 1416 1327 struct gpio_desc **descs; 1417 1328 unsigned int i, didx, num_set; 1418 1329 int ret; 1419 1330 1331 + if (copy_from_user(&lv, ip, sizeof(lv))) 1332 + return -EFAULT; 1333 + 1334 + guard(mutex)(&lr->config_mutex); 1335 + 1336 + /* 1337 + * gpiod_set_array_value_complex() requires compacted desc and val 1338 + * arrays, rather than the sparse ones in lv. 1339 + * Calculation of num_set and construction of the descs and vals arrays 1340 + * is optimized to minimize scanning the lv->mask, and to avoid 1341 + * allocation for the desc array for the common num_set == 1 case. 1342 + */ 1420 1343 bitmap_zero(vals, GPIO_V2_LINES_MAX); 1344 + /* scan requested lines to determine the subset to be set */ 1421 1345 for (num_set = 0, i = 0; i < lr->num_lines; i++) { 1422 - if (lv->mask & BIT_ULL(i)) { 1346 + if (lv.mask & BIT_ULL(i)) { 1347 + /* setting inputs is not allowed */ 1423 1348 if (!test_bit(FLAG_IS_OUT, &lr->lines[i].desc->flags)) 1424 1349 return -EPERM; 1425 - if (lv->bits & BIT_ULL(i)) 1350 + /* add to compacted values */ 1351 + if (lv.bits & BIT_ULL(i)) 1426 1352 __set_bit(num_set, vals); 1427 1353 num_set++; 1354 + /* capture desc for the num_set == 1 case */ 1428 1355 descs = &lr->lines[i].desc; 1429 1356 } 1430 1357 } ··· 1448 1343 return -EINVAL; 1449 1344 1450 1345 if (num_set != 1) { 1451 - /* build compacted desc array and values */ 1346 + /* build compacted desc array */ 1452 1347 descs = kmalloc_array(num_set, sizeof(*descs), GFP_KERNEL); 1453 1348 if (!descs) 1454 1349 return -ENOMEM; 1455 1350 for (didx = 0, i = 0; i < lr->num_lines; i++) { 1456 - if (lv->mask & BIT_ULL(i)) { 1351 + if (lv.mask & BIT_ULL(i)) { 1457 1352 descs[didx] = lr->lines[i].desc; 1458 1353 didx++; 1459 1354 } ··· 1467 1362 return ret; 1468 1363 } 1469 1364 1470 - static long linereq_set_values(struct linereq *lr, void __user *ip) 1365 + static long linereq_set_config(struct linereq *lr, void __user *ip) 1471 1366 { 1472 - struct gpio_v2_line_values lv; 1473 - int ret; 1474 - 1475 - if (copy_from_user(&lv, ip, sizeof(lv))) 1476 - return -EFAULT; 1477 - 1478 - mutex_lock(&lr->config_mutex); 1479 - 1480 - ret = linereq_set_values_unlocked(lr, &lv); 1481 - 1482 - mutex_unlock(&lr->config_mutex); 1483 - 1484 - return ret; 1485 - } 1486 - 1487 - static long linereq_set_config_unlocked(struct linereq *lr, 1488 - struct gpio_v2_line_config *lc) 1489 - { 1367 + struct gpio_v2_line_config lc; 1490 1368 struct gpio_desc *desc; 1491 1369 struct line *line; 1492 1370 unsigned int i; 1493 1371 u64 flags, edflags; 1494 1372 int ret; 1495 1373 1374 + if (copy_from_user(&lc, ip, sizeof(lc))) 1375 + return -EFAULT; 1376 + 1377 + ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1378 + if (ret) 1379 + return ret; 1380 + 1381 + guard(mutex)(&lr->config_mutex); 1382 + 1496 1383 for (i = 0; i < lr->num_lines; i++) { 1497 1384 line = &lr->lines[i]; 1498 1385 desc = lr->lines[i].desc; 1499 - flags = gpio_v2_line_config_flags(lc, i); 1386 + flags = gpio_v2_line_config_flags(&lc, i); 1500 1387 gpio_v2_line_config_flags_to_desc_flags(flags, &desc->flags); 1501 1388 edflags = flags & GPIO_V2_LINE_EDGE_DETECTOR_FLAGS; 1502 1389 /* ··· 1496 1399 * or output, else the line will be treated "as is". 1497 1400 */ 1498 1401 if (flags & GPIO_V2_LINE_FLAG_OUTPUT) { 1499 - int val = gpio_v2_line_config_output_value(lc, i); 1402 + int val = gpio_v2_line_config_output_value(&lc, i); 1500 1403 1501 1404 edge_detector_stop(line); 1502 1405 ret = gpiod_direction_output(desc, val); ··· 1507 1410 if (ret) 1508 1411 return ret; 1509 1412 1510 - ret = edge_detector_update(line, lc, i, edflags); 1413 + ret = edge_detector_update(line, &lc, i, edflags); 1511 1414 if (ret) 1512 1415 return ret; 1513 1416 } ··· 1519 1422 return 0; 1520 1423 } 1521 1424 1522 - static long linereq_set_config(struct linereq *lr, void __user *ip) 1523 - { 1524 - struct gpio_v2_line_config lc; 1525 - int ret; 1526 - 1527 - if (copy_from_user(&lc, ip, sizeof(lc))) 1528 - return -EFAULT; 1529 - 1530 - ret = gpio_v2_line_config_validate(&lc, lr->num_lines); 1531 - if (ret) 1532 - return ret; 1533 - 1534 - mutex_lock(&lr->config_mutex); 1535 - 1536 - ret = linereq_set_config_unlocked(lr, &lc); 1537 - 1538 - mutex_unlock(&lr->config_mutex); 1539 - 1540 - return ret; 1541 - } 1542 - 1543 - static long linereq_ioctl_unlocked(struct file *file, unsigned int cmd, 1544 - unsigned long arg) 1425 + static long linereq_ioctl(struct file *file, unsigned int cmd, 1426 + unsigned long arg) 1545 1427 { 1546 1428 struct linereq *lr = file->private_data; 1547 1429 void __user *ip = (void __user *)arg; 1430 + 1431 + guard(rwsem_read)(&lr->gdev->sem); 1548 1432 1549 1433 if (!lr->gdev->chip) 1550 1434 return -ENODEV; ··· 1542 1464 } 1543 1465 } 1544 1466 1545 - static long linereq_ioctl(struct file *file, unsigned int cmd, 1546 - unsigned long arg) 1547 - { 1548 - struct linereq *lr = file->private_data; 1549 - 1550 - return call_ioctl_locked(file, cmd, arg, lr->gdev, 1551 - linereq_ioctl_unlocked); 1552 - } 1553 - 1554 1467 #ifdef CONFIG_COMPAT 1555 1468 static long linereq_ioctl_compat(struct file *file, unsigned int cmd, 1556 1469 unsigned long arg) ··· 1550 1481 } 1551 1482 #endif 1552 1483 1553 - static __poll_t linereq_poll_unlocked(struct file *file, 1554 - struct poll_table_struct *wait) 1484 + static __poll_t linereq_poll(struct file *file, 1485 + struct poll_table_struct *wait) 1555 1486 { 1556 1487 struct linereq *lr = file->private_data; 1557 1488 __poll_t events = 0; 1489 + 1490 + guard(rwsem_read)(&lr->gdev->sem); 1558 1491 1559 1492 if (!lr->gdev->chip) 1560 1493 return EPOLLHUP | EPOLLERR; ··· 1570 1499 return events; 1571 1500 } 1572 1501 1573 - static __poll_t linereq_poll(struct file *file, 1574 - struct poll_table_struct *wait) 1575 - { 1576 - struct linereq *lr = file->private_data; 1577 - 1578 - return call_poll_locked(file, wait, lr->gdev, linereq_poll_unlocked); 1579 - } 1580 - 1581 - static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, 1582 - size_t count, loff_t *f_ps) 1502 + static ssize_t linereq_read(struct file *file, char __user *buf, 1503 + size_t count, loff_t *f_ps) 1583 1504 { 1584 1505 struct linereq *lr = file->private_data; 1585 1506 struct gpio_v2_line_event le; 1586 1507 ssize_t bytes_read = 0; 1587 1508 int ret; 1509 + 1510 + guard(rwsem_read)(&lr->gdev->sem); 1588 1511 1589 1512 if (!lr->gdev->chip) 1590 1513 return -ENODEV; ··· 1587 1522 return -EINVAL; 1588 1523 1589 1524 do { 1590 - spin_lock(&lr->wait.lock); 1591 - if (kfifo_is_empty(&lr->events)) { 1592 - if (bytes_read) { 1593 - spin_unlock(&lr->wait.lock); 1594 - return bytes_read; 1525 + scoped_guard(spinlock, &lr->wait.lock) { 1526 + if (kfifo_is_empty(&lr->events)) { 1527 + if (bytes_read) 1528 + return bytes_read; 1529 + 1530 + if (file->f_flags & O_NONBLOCK) 1531 + return -EAGAIN; 1532 + 1533 + ret = wait_event_interruptible_locked(lr->wait, 1534 + !kfifo_is_empty(&lr->events)); 1535 + if (ret) 1536 + return ret; 1595 1537 } 1596 1538 1597 - if (file->f_flags & O_NONBLOCK) { 1598 - spin_unlock(&lr->wait.lock); 1599 - return -EAGAIN; 1600 - } 1601 - 1602 - ret = wait_event_interruptible_locked(lr->wait, 1603 - !kfifo_is_empty(&lr->events)); 1604 - if (ret) { 1605 - spin_unlock(&lr->wait.lock); 1606 - return ret; 1607 - } 1539 + ret = kfifo_out(&lr->events, &le, 1); 1608 1540 } 1609 - 1610 - ret = kfifo_out(&lr->events, &le, 1); 1611 - spin_unlock(&lr->wait.lock); 1612 1541 if (ret != 1) { 1613 1542 /* 1614 1543 * This should never happen - we were holding the ··· 1621 1562 return bytes_read; 1622 1563 } 1623 1564 1624 - static ssize_t linereq_read(struct file *file, char __user *buf, 1625 - size_t count, loff_t *f_ps) 1626 - { 1627 - struct linereq *lr = file->private_data; 1628 - 1629 - return call_read_locked(file, buf, count, f_ps, lr->gdev, 1630 - linereq_read_unlocked); 1631 - } 1632 - 1633 1565 static void linereq_free(struct linereq *lr) 1634 1566 { 1567 + struct line *line; 1635 1568 unsigned int i; 1636 1569 1637 1570 if (lr->device_unregistered_nb.notifier_call) ··· 1631 1580 &lr->device_unregistered_nb); 1632 1581 1633 1582 for (i = 0; i < lr->num_lines; i++) { 1634 - if (lr->lines[i].desc) { 1635 - edge_detector_stop(&lr->lines[i]); 1636 - gpiod_free(lr->lines[i].desc); 1637 - } 1583 + line = &lr->lines[i]; 1584 + if (!line->desc) 1585 + continue; 1586 + 1587 + edge_detector_stop(line); 1588 + if (line_has_supinfo(line)) 1589 + supinfo_erase(line); 1590 + gpiod_free(line->desc); 1638 1591 } 1639 1592 kfifo_free(&lr->events); 1640 1593 kfree(lr->label); 1641 1594 gpio_device_put(lr->gdev); 1642 - kfree(lr); 1595 + kvfree(lr); 1643 1596 } 1644 1597 1645 1598 static int linereq_release(struct inode *inode, struct file *file) ··· 1708 1653 if (ret) 1709 1654 return ret; 1710 1655 1711 - lr = kzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 1656 + lr = kvzalloc(struct_size(lr, lines, ulr.num_lines), GFP_KERNEL); 1712 1657 if (!lr) 1713 1658 return -ENOMEM; 1714 1659 lr->num_lines = ulr.num_lines; ··· 1873 1818 (GPIOEVENT_REQUEST_RISING_EDGE | \ 1874 1819 GPIOEVENT_REQUEST_FALLING_EDGE) 1875 1820 1876 - static __poll_t lineevent_poll_unlocked(struct file *file, 1877 - struct poll_table_struct *wait) 1821 + static __poll_t lineevent_poll(struct file *file, 1822 + struct poll_table_struct *wait) 1878 1823 { 1879 1824 struct lineevent_state *le = file->private_data; 1880 1825 __poll_t events = 0; 1826 + 1827 + guard(rwsem_read)(&le->gdev->sem); 1881 1828 1882 1829 if (!le->gdev->chip) 1883 1830 return EPOLLHUP | EPOLLERR; ··· 1890 1833 events = EPOLLIN | EPOLLRDNORM; 1891 1834 1892 1835 return events; 1893 - } 1894 - 1895 - static __poll_t lineevent_poll(struct file *file, 1896 - struct poll_table_struct *wait) 1897 - { 1898 - struct lineevent_state *le = file->private_data; 1899 - 1900 - return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked); 1901 1836 } 1902 1837 1903 1838 static int lineevent_unregistered_notify(struct notifier_block *nb, ··· 1908 1859 u32 id; 1909 1860 }; 1910 1861 1911 - static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, 1912 - size_t count, loff_t *f_ps) 1862 + static ssize_t lineevent_read(struct file *file, char __user *buf, 1863 + size_t count, loff_t *f_ps) 1913 1864 { 1914 1865 struct lineevent_state *le = file->private_data; 1915 1866 struct gpioevent_data ge; 1916 1867 ssize_t bytes_read = 0; 1917 1868 ssize_t ge_size; 1918 1869 int ret; 1870 + 1871 + guard(rwsem_read)(&le->gdev->sem); 1919 1872 1920 1873 if (!le->gdev->chip) 1921 1874 return -ENODEV; ··· 1939 1888 return -EINVAL; 1940 1889 1941 1890 do { 1942 - spin_lock(&le->wait.lock); 1943 - if (kfifo_is_empty(&le->events)) { 1944 - if (bytes_read) { 1945 - spin_unlock(&le->wait.lock); 1946 - return bytes_read; 1891 + scoped_guard(spinlock, &le->wait.lock) { 1892 + if (kfifo_is_empty(&le->events)) { 1893 + if (bytes_read) 1894 + return bytes_read; 1895 + 1896 + if (file->f_flags & O_NONBLOCK) 1897 + return -EAGAIN; 1898 + 1899 + ret = wait_event_interruptible_locked(le->wait, 1900 + !kfifo_is_empty(&le->events)); 1901 + if (ret) 1902 + return ret; 1947 1903 } 1948 1904 1949 - if (file->f_flags & O_NONBLOCK) { 1950 - spin_unlock(&le->wait.lock); 1951 - return -EAGAIN; 1952 - } 1953 - 1954 - ret = wait_event_interruptible_locked(le->wait, 1955 - !kfifo_is_empty(&le->events)); 1956 - if (ret) { 1957 - spin_unlock(&le->wait.lock); 1958 - return ret; 1959 - } 1905 + ret = kfifo_out(&le->events, &ge, 1); 1960 1906 } 1961 - 1962 - ret = kfifo_out(&le->events, &ge, 1); 1963 - spin_unlock(&le->wait.lock); 1964 1907 if (ret != 1) { 1965 1908 /* 1966 1909 * This should never happen - we were holding the lock ··· 1971 1926 } while (count >= bytes_read + ge_size); 1972 1927 1973 1928 return bytes_read; 1974 - } 1975 - 1976 - static ssize_t lineevent_read(struct file *file, char __user *buf, 1977 - size_t count, loff_t *f_ps) 1978 - { 1979 - struct lineevent_state *le = file->private_data; 1980 - 1981 - return call_read_locked(file, buf, count, f_ps, le->gdev, 1982 - lineevent_read_unlocked); 1983 1929 } 1984 1930 1985 1931 static void lineevent_free(struct lineevent_state *le) ··· 1993 1957 return 0; 1994 1958 } 1995 1959 1996 - static long lineevent_ioctl_unlocked(struct file *file, unsigned int cmd, 1997 - unsigned long arg) 1960 + static long lineevent_ioctl(struct file *file, unsigned int cmd, 1961 + unsigned long arg) 1998 1962 { 1999 1963 struct lineevent_state *le = file->private_data; 2000 1964 void __user *ip = (void __user *)arg; 2001 1965 struct gpiohandle_data ghd; 1966 + 1967 + guard(rwsem_read)(&le->gdev->sem); 2002 1968 2003 1969 if (!le->gdev->chip) 2004 1970 return -ENODEV; ··· 2025 1987 return 0; 2026 1988 } 2027 1989 return -EINVAL; 2028 - } 2029 - 2030 - static long lineevent_ioctl(struct file *file, unsigned int cmd, 2031 - unsigned long arg) 2032 - { 2033 - struct lineevent_state *le = file->private_data; 2034 - 2035 - return call_ioctl_locked(file, cmd, arg, le->gdev, 2036 - lineevent_ioctl_unlocked); 2037 1990 } 2038 1991 2039 1992 #ifdef CONFIG_COMPAT ··· 2301 2272 struct gpio_v2_line_info *info) 2302 2273 { 2303 2274 struct gpio_chip *gc = desc->gdev->chip; 2304 - bool ok_for_pinctrl; 2305 - unsigned long flags; 2306 - u32 debounce_period_us; 2307 - unsigned int num_attrs = 0; 2275 + unsigned long dflags; 2308 2276 2309 2277 memset(info, 0, sizeof(*info)); 2310 2278 info->offset = gpio_chip_hwgpio(desc); 2311 2279 2312 - /* 2313 - * This function takes a mutex so we must check this before taking 2314 - * the spinlock. 2315 - * 2316 - * FIXME: find a non-racy way to retrieve this information. Maybe a 2317 - * lock common to both frameworks? 2318 - */ 2319 - ok_for_pinctrl = pinctrl_gpio_can_use_line(gc, info->offset); 2280 + scoped_guard(spinlock_irqsave, &gpio_lock) { 2281 + if (desc->name) 2282 + strscpy(info->name, desc->name, sizeof(info->name)); 2320 2283 2321 - spin_lock_irqsave(&gpio_lock, flags); 2284 + if (desc->label) 2285 + strscpy(info->consumer, desc->label, 2286 + sizeof(info->consumer)); 2322 2287 2323 - if (desc->name) 2324 - strscpy(info->name, desc->name, sizeof(info->name)); 2325 - 2326 - if (desc->label) 2327 - strscpy(info->consumer, desc->label, sizeof(info->consumer)); 2288 + dflags = READ_ONCE(desc->flags); 2289 + } 2328 2290 2329 2291 /* 2330 - * Userspace only need to know that the kernel is using this GPIO so 2331 - * it can't use it. 2292 + * Userspace only need know that the kernel is using this GPIO so it 2293 + * can't use it. 2294 + * The calculation of the used flag is slightly racy, as it may read 2295 + * desc, gc and pinctrl state without a lock covering all three at 2296 + * once. Worst case if the line is in transition and the calculation 2297 + * is inconsistent then it looks to the user like they performed the 2298 + * read on the other side of the transition - but that can always 2299 + * happen. 2300 + * The definitive test that a line is available to userspace is to 2301 + * request it. 2332 2302 */ 2333 - info->flags = 0; 2334 - if (test_bit(FLAG_REQUESTED, &desc->flags) || 2335 - test_bit(FLAG_IS_HOGGED, &desc->flags) || 2336 - test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 2337 - test_bit(FLAG_EXPORT, &desc->flags) || 2338 - test_bit(FLAG_SYSFS, &desc->flags) || 2303 + if (test_bit(FLAG_REQUESTED, &dflags) || 2304 + test_bit(FLAG_IS_HOGGED, &dflags) || 2305 + test_bit(FLAG_USED_AS_IRQ, &dflags) || 2306 + test_bit(FLAG_EXPORT, &dflags) || 2307 + test_bit(FLAG_SYSFS, &dflags) || 2339 2308 !gpiochip_line_is_valid(gc, info->offset) || 2340 - !ok_for_pinctrl) 2309 + !pinctrl_gpio_can_use_line(gc, info->offset)) 2341 2310 info->flags |= GPIO_V2_LINE_FLAG_USED; 2342 2311 2343 - if (test_bit(FLAG_IS_OUT, &desc->flags)) 2312 + if (test_bit(FLAG_IS_OUT, &dflags)) 2344 2313 info->flags |= GPIO_V2_LINE_FLAG_OUTPUT; 2345 2314 else 2346 2315 info->flags |= GPIO_V2_LINE_FLAG_INPUT; 2347 2316 2348 - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2317 + if (test_bit(FLAG_ACTIVE_LOW, &dflags)) 2349 2318 info->flags |= GPIO_V2_LINE_FLAG_ACTIVE_LOW; 2350 2319 2351 - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 2320 + if (test_bit(FLAG_OPEN_DRAIN, &dflags)) 2352 2321 info->flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN; 2353 - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 2322 + if (test_bit(FLAG_OPEN_SOURCE, &dflags)) 2354 2323 info->flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE; 2355 2324 2356 - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2325 + if (test_bit(FLAG_BIAS_DISABLE, &dflags)) 2357 2326 info->flags |= GPIO_V2_LINE_FLAG_BIAS_DISABLED; 2358 - if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2327 + if (test_bit(FLAG_PULL_DOWN, &dflags)) 2359 2328 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN; 2360 - if (test_bit(FLAG_PULL_UP, &desc->flags)) 2329 + if (test_bit(FLAG_PULL_UP, &dflags)) 2361 2330 info->flags |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP; 2362 2331 2363 - if (test_bit(FLAG_EDGE_RISING, &desc->flags)) 2332 + if (test_bit(FLAG_EDGE_RISING, &dflags)) 2364 2333 info->flags |= GPIO_V2_LINE_FLAG_EDGE_RISING; 2365 - if (test_bit(FLAG_EDGE_FALLING, &desc->flags)) 2334 + if (test_bit(FLAG_EDGE_FALLING, &dflags)) 2366 2335 info->flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING; 2367 2336 2368 - if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &desc->flags)) 2337 + if (test_bit(FLAG_EVENT_CLOCK_REALTIME, &dflags)) 2369 2338 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME; 2370 - else if (test_bit(FLAG_EVENT_CLOCK_HTE, &desc->flags)) 2339 + else if (test_bit(FLAG_EVENT_CLOCK_HTE, &dflags)) 2371 2340 info->flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE; 2372 - 2373 - debounce_period_us = READ_ONCE(desc->debounce_period_us); 2374 - if (debounce_period_us) { 2375 - info->attrs[num_attrs].id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE; 2376 - info->attrs[num_attrs].debounce_period_us = debounce_period_us; 2377 - num_attrs++; 2378 - } 2379 - info->num_attrs = num_attrs; 2380 - 2381 - spin_unlock_irqrestore(&gpio_lock, flags); 2382 2341 } 2383 2342 2384 2343 struct gpio_chardev_data { ··· 2472 2455 return -EBUSY; 2473 2456 } 2474 2457 gpio_desc_to_lineinfo(desc, &lineinfo); 2458 + supinfo_to_lineinfo(desc, &lineinfo); 2475 2459 2476 2460 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) { 2477 2461 if (watch) ··· 2499 2481 return 0; 2500 2482 } 2501 2483 2502 - static long gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg) 2484 + /* 2485 + * gpio_ioctl() - ioctl handler for the GPIO chardev 2486 + */ 2487 + static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2503 2488 { 2504 2489 struct gpio_chardev_data *cdev = file->private_data; 2505 2490 struct gpio_device *gdev = cdev->gdev; 2506 2491 void __user *ip = (void __user *)arg; 2492 + 2493 + guard(rwsem_read)(&gdev->sem); 2507 2494 2508 2495 /* We fail any subsequent ioctl():s when the chip is gone */ 2509 2496 if (!gdev->chip) ··· 2541 2518 } 2542 2519 } 2543 2520 2544 - /* 2545 - * gpio_ioctl() - ioctl handler for the GPIO chardev 2546 - */ 2547 - static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 2548 - { 2549 - struct gpio_chardev_data *cdev = file->private_data; 2550 - 2551 - return call_ioctl_locked(file, cmd, arg, cdev->gdev, 2552 - gpio_ioctl_unlocked); 2553 - } 2554 - 2555 2521 #ifdef CONFIG_COMPAT 2556 2522 static long gpio_ioctl_compat(struct file *file, unsigned int cmd, 2557 2523 unsigned long arg) ··· 2565 2553 chg.event_type = action; 2566 2554 chg.timestamp_ns = ktime_get_ns(); 2567 2555 gpio_desc_to_lineinfo(desc, &chg.info); 2556 + supinfo_to_lineinfo(desc, &chg.info); 2568 2557 2569 2558 ret = kfifo_in_spinlocked(&cdev->events, &chg, 1, &cdev->wait.lock); 2570 2559 if (ret) ··· 2588 2575 return NOTIFY_OK; 2589 2576 } 2590 2577 2591 - static __poll_t lineinfo_watch_poll_unlocked(struct file *file, 2592 - struct poll_table_struct *pollt) 2578 + static __poll_t lineinfo_watch_poll(struct file *file, 2579 + struct poll_table_struct *pollt) 2593 2580 { 2594 2581 struct gpio_chardev_data *cdev = file->private_data; 2595 2582 __poll_t events = 0; 2583 + 2584 + guard(rwsem_read)(&cdev->gdev->sem); 2596 2585 2597 2586 if (!cdev->gdev->chip) 2598 2587 return EPOLLHUP | EPOLLERR; ··· 2608 2593 return events; 2609 2594 } 2610 2595 2611 - static __poll_t lineinfo_watch_poll(struct file *file, 2612 - struct poll_table_struct *pollt) 2613 - { 2614 - struct gpio_chardev_data *cdev = file->private_data; 2615 - 2616 - return call_poll_locked(file, pollt, cdev->gdev, 2617 - lineinfo_watch_poll_unlocked); 2618 - } 2619 - 2620 - static ssize_t lineinfo_watch_read_unlocked(struct file *file, char __user *buf, 2621 - size_t count, loff_t *off) 2596 + static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2597 + size_t count, loff_t *off) 2622 2598 { 2623 2599 struct gpio_chardev_data *cdev = file->private_data; 2624 2600 struct gpio_v2_line_info_changed event; 2625 2601 ssize_t bytes_read = 0; 2626 2602 int ret; 2627 2603 size_t event_size; 2604 + 2605 + guard(rwsem_read)(&cdev->gdev->sem); 2628 2606 2629 2607 if (!cdev->gdev->chip) 2630 2608 return -ENODEV; ··· 2629 2621 #endif 2630 2622 2631 2623 do { 2632 - spin_lock(&cdev->wait.lock); 2633 - if (kfifo_is_empty(&cdev->events)) { 2634 - if (bytes_read) { 2635 - spin_unlock(&cdev->wait.lock); 2636 - return bytes_read; 2637 - } 2624 + scoped_guard(spinlock, &cdev->wait.lock) { 2625 + if (kfifo_is_empty(&cdev->events)) { 2626 + if (bytes_read) 2627 + return bytes_read; 2638 2628 2639 - if (file->f_flags & O_NONBLOCK) { 2640 - spin_unlock(&cdev->wait.lock); 2641 - return -EAGAIN; 2642 - } 2629 + if (file->f_flags & O_NONBLOCK) 2630 + return -EAGAIN; 2643 2631 2644 - ret = wait_event_interruptible_locked(cdev->wait, 2645 - !kfifo_is_empty(&cdev->events)); 2646 - if (ret) { 2647 - spin_unlock(&cdev->wait.lock); 2648 - return ret; 2632 + ret = wait_event_interruptible_locked(cdev->wait, 2633 + !kfifo_is_empty(&cdev->events)); 2634 + if (ret) 2635 + return ret; 2649 2636 } 2650 - } 2651 2637 #ifdef CONFIG_GPIO_CDEV_V1 2652 - /* must be after kfifo check so watch_abi_version is set */ 2653 - if (atomic_read(&cdev->watch_abi_version) == 2) 2654 - event_size = sizeof(struct gpio_v2_line_info_changed); 2655 - else 2656 - event_size = sizeof(struct gpioline_info_changed); 2657 - if (count < event_size) { 2658 - spin_unlock(&cdev->wait.lock); 2659 - return -EINVAL; 2660 - } 2638 + /* must be after kfifo check so watch_abi_version is set */ 2639 + if (atomic_read(&cdev->watch_abi_version) == 2) 2640 + event_size = sizeof(struct gpio_v2_line_info_changed); 2641 + else 2642 + event_size = sizeof(struct gpioline_info_changed); 2643 + if (count < event_size) 2644 + return -EINVAL; 2661 2645 #endif 2662 - ret = kfifo_out(&cdev->events, &event, 1); 2663 - spin_unlock(&cdev->wait.lock); 2646 + ret = kfifo_out(&cdev->events, &event, 1); 2647 + } 2664 2648 if (ret != 1) { 2665 2649 ret = -EIO; 2666 2650 break; ··· 2681 2681 return bytes_read; 2682 2682 } 2683 2683 2684 - static ssize_t lineinfo_watch_read(struct file *file, char __user *buf, 2685 - size_t count, loff_t *off) 2686 - { 2687 - struct gpio_chardev_data *cdev = file->private_data; 2688 - 2689 - return call_read_locked(file, buf, count, off, cdev->gdev, 2690 - lineinfo_watch_read_unlocked); 2691 - } 2692 - 2693 2684 /** 2694 2685 * gpio_chrdev_open() - open the chardev for ioctl operations 2695 2686 * @inode: inode for this chardev ··· 2694 2703 struct gpio_chardev_data *cdev; 2695 2704 int ret = -ENOMEM; 2696 2705 2697 - down_read(&gdev->sem); 2706 + guard(rwsem_read)(&gdev->sem); 2698 2707 2699 2708 /* Fail on open if the backing gpiochip is gone */ 2700 - if (!gdev->chip) { 2701 - ret = -ENODEV; 2702 - goto out_unlock; 2703 - } 2709 + if (!gdev->chip) 2710 + return -ENODEV; 2704 2711 2705 2712 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 2706 2713 if (!cdev) 2707 - goto out_unlock; 2714 + return -ENODEV; 2708 2715 2709 2716 cdev->watched_lines = bitmap_zalloc(gdev->chip->ngpio, GFP_KERNEL); 2710 2717 if (!cdev->watched_lines) ··· 2731 2742 if (ret) 2732 2743 goto out_unregister_device_notifier; 2733 2744 2734 - up_read(&gdev->sem); 2735 - 2736 2745 return ret; 2737 2746 2738 2747 out_unregister_device_notifier: ··· 2744 2757 bitmap_free(cdev->watched_lines); 2745 2758 out_free_cdev: 2746 2759 kfree(cdev); 2747 - out_unlock: 2748 - up_read(&gdev->sem); 2749 2760 return ret; 2750 2761 } 2751 2762
+53 -57
drivers/gpio/gpiolib-sysfs.c
··· 165 165 /* Caller holds gpiod-data mutex. */ 166 166 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags) 167 167 { 168 - struct gpiod_data *data = dev_get_drvdata(dev); 169 - struct gpio_desc *desc = data->desc; 170 - unsigned long irq_flags; 171 - int ret; 168 + struct gpiod_data *data = dev_get_drvdata(dev); 169 + struct gpio_desc *desc = data->desc; 170 + unsigned long irq_flags; 171 + int ret; 172 172 173 173 data->irq = gpiod_to_irq(desc); 174 174 if (data->irq < 0) ··· 259 259 struct device_attribute *attr, const char *buf, size_t size) 260 260 { 261 261 struct gpiod_data *data = dev_get_drvdata(dev); 262 - ssize_t status = size; 262 + ssize_t status = size; 263 263 int flags; 264 264 265 265 flags = sysfs_match_string(trigger_names, buf); ··· 292 292 /* Caller holds gpiod-data mutex. */ 293 293 static int gpio_sysfs_set_active_low(struct device *dev, int value) 294 294 { 295 - struct gpiod_data *data = dev_get_drvdata(dev); 296 - struct gpio_desc *desc = data->desc; 297 - int status = 0; 298 - unsigned int flags = data->irq_flags; 295 + struct gpiod_data *data = dev_get_drvdata(dev); 296 + unsigned int flags = data->irq_flags; 297 + struct gpio_desc *desc = data->desc; 298 + int status = 0; 299 + 299 300 300 301 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) 301 302 return 0; ··· 332 331 static ssize_t active_low_store(struct device *dev, 333 332 struct device_attribute *attr, const char *buf, size_t size) 334 333 { 335 - struct gpiod_data *data = dev_get_drvdata(dev); 336 - ssize_t status; 337 - long value; 334 + struct gpiod_data *data = dev_get_drvdata(dev); 335 + ssize_t status; 336 + long value; 338 337 339 338 status = kstrtol(buf, 0, &value); 340 339 if (status) ··· 400 399 static ssize_t base_show(struct device *dev, 401 400 struct device_attribute *attr, char *buf) 402 401 { 403 - const struct gpio_chip *chip = dev_get_drvdata(dev); 402 + const struct gpio_chip *chip = dev_get_drvdata(dev); 404 403 405 404 return sysfs_emit(buf, "%d\n", chip->base); 406 405 } ··· 409 408 static ssize_t label_show(struct device *dev, 410 409 struct device_attribute *attr, char *buf) 411 410 { 412 - const struct gpio_chip *chip = dev_get_drvdata(dev); 411 + const struct gpio_chip *chip = dev_get_drvdata(dev); 413 412 414 413 return sysfs_emit(buf, "%s\n", chip->label ?: ""); 415 414 } ··· 418 417 static ssize_t ngpio_show(struct device *dev, 419 418 struct device_attribute *attr, char *buf) 420 419 { 421 - const struct gpio_chip *chip = dev_get_drvdata(dev); 420 + const struct gpio_chip *chip = dev_get_drvdata(dev); 422 421 423 422 return sysfs_emit(buf, "%u\n", chip->ngpio); 424 423 } ··· 442 441 const struct class_attribute *attr, 443 442 const char *buf, size_t len) 444 443 { 445 - long gpio; 446 - struct gpio_desc *desc; 447 - int status; 448 - struct gpio_chip *gc; 449 - int offset; 444 + struct gpio_desc *desc; 445 + struct gpio_chip *gc; 446 + int status, offset; 447 + long gpio; 450 448 451 449 status = kstrtol(buf, 0, &gpio); 452 450 if (status < 0) ··· 496 496 const struct class_attribute *attr, 497 497 const char *buf, size_t len) 498 498 { 499 - long gpio; 500 - struct gpio_desc *desc; 501 - int status; 499 + struct gpio_desc *desc; 500 + int status; 501 + long gpio; 502 502 503 503 status = kstrtol(buf, 0, &gpio); 504 504 if (status < 0) ··· 559 559 */ 560 560 int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 561 561 { 562 - struct gpio_chip *chip; 563 - struct gpio_device *gdev; 564 - struct gpiod_data *data; 565 - unsigned long flags; 566 - int status; 567 - const char *ioname = NULL; 568 - struct device *dev; 569 - int offset; 562 + const char *ioname = NULL; 563 + struct gpio_device *gdev; 564 + struct gpiod_data *data; 565 + struct gpio_chip *chip; 566 + unsigned long flags; 567 + struct device *dev; 568 + int status, offset; 570 569 571 570 /* can't export until sysfs is available ... */ 572 571 if (!class_is_registered(&gpio_class)) { ··· 732 733 733 734 int gpiochip_sysfs_register(struct gpio_device *gdev) 734 735 { 735 - struct device *dev; 736 - struct device *parent; 737 736 struct gpio_chip *chip = gdev->chip; 737 + struct device *parent; 738 + struct device *dev; 738 739 739 740 /* 740 741 * Many systems add gpio chips for SOC support very early, ··· 768 769 return 0; 769 770 } 770 771 772 + int gpiochip_sysfs_register_all(void) 773 + { 774 + struct gpio_device *gdev; 775 + int ret; 776 + 777 + guard(rwsem_read)(&gpio_devices_sem); 778 + 779 + list_for_each_entry(gdev, &gpio_devices, list) { 780 + if (gdev->mockdev) 781 + continue; 782 + 783 + ret = gpiochip_sysfs_register(gdev); 784 + if (ret) 785 + return ret; 786 + } 787 + 788 + return 0; 789 + } 790 + 771 791 void gpiochip_sysfs_unregister(struct gpio_device *gdev) 772 792 { 773 793 struct gpio_desc *desc; ··· 811 793 812 794 static int __init gpiolib_sysfs_init(void) 813 795 { 814 - int status; 815 - unsigned long flags; 816 - struct gpio_device *gdev; 796 + int status; 817 797 818 798 status = class_register(&gpio_class); 819 799 if (status < 0) ··· 823 807 * We run before arch_initcall() so chip->dev nodes can have 824 808 * registered, and so arch_initcall() can always gpiod_export(). 825 809 */ 826 - spin_lock_irqsave(&gpio_lock, flags); 827 - list_for_each_entry(gdev, &gpio_devices, list) { 828 - if (gdev->mockdev) 829 - continue; 830 - 831 - /* 832 - * TODO we yield gpio_lock here because 833 - * gpiochip_sysfs_register() acquires a mutex. This is unsafe 834 - * and needs to be fixed. 835 - * 836 - * Also it would be nice to use gpio_device_find() here so we 837 - * can keep gpio_chips local to gpiolib.c, but the yield of 838 - * gpio_lock prevents us from doing this. 839 - */ 840 - spin_unlock_irqrestore(&gpio_lock, flags); 841 - status = gpiochip_sysfs_register(gdev); 842 - spin_lock_irqsave(&gpio_lock, flags); 843 - } 844 - spin_unlock_irqrestore(&gpio_lock, flags); 845 - 846 - return status; 810 + return gpiochip_sysfs_register_all(); 847 811 } 848 812 postcore_initcall(gpiolib_sysfs_init);
+8 -2
drivers/gpio/gpiolib-sysfs.h
··· 3 3 #ifndef GPIOLIB_SYSFS_H 4 4 #define GPIOLIB_SYSFS_H 5 5 6 - #ifdef CONFIG_GPIO_SYSFS 7 - 8 6 struct gpio_device; 9 7 8 + #ifdef CONFIG_GPIO_SYSFS 9 + 10 10 int gpiochip_sysfs_register(struct gpio_device *gdev); 11 + int gpiochip_sysfs_register_all(void); 11 12 void gpiochip_sysfs_unregister(struct gpio_device *gdev); 12 13 13 14 #else 14 15 15 16 static inline int gpiochip_sysfs_register(struct gpio_device *gdev) 17 + { 18 + return 0; 19 + } 20 + 21 + static inline int gpiochip_sysfs_register_all(void) 16 22 { 17 23 return 0; 18 24 }
+135 -139
drivers/gpio/gpiolib.c
··· 2 2 3 3 #include <linux/acpi.h> 4 4 #include <linux/bitmap.h> 5 + #include <linux/cleanup.h> 5 6 #include <linux/compat.h> 6 7 #include <linux/debugfs.h> 7 8 #include <linux/device.h> ··· 16 15 #include <linux/kernel.h> 17 16 #include <linux/list.h> 18 17 #include <linux/module.h> 18 + #include <linux/mutex.h> 19 19 #include <linux/of.h> 20 20 #include <linux/pinctrl/consumer.h> 21 21 #include <linux/seq_file.h> ··· 46 44 * get/set operations for common cases, so that access to SOC-integrated 47 45 * GPIOs can sometimes cost only an instruction or two per bit. 48 46 */ 49 - 50 - 51 - /* When debugging, extend minimal trust to callers and platform code. 52 - * Also emit diagnostic messages that may help initial bringup, when 53 - * board setup or driver bugs are most common. 54 - * 55 - * Otherwise, minimize overhead in what may be bitbanging codepaths. 56 - */ 57 - #ifdef DEBUG 58 - #define extra_checks 1 59 - #else 60 - #define extra_checks 0 61 - #endif 62 47 63 48 /* Device and char device-related information */ 64 49 static DEFINE_IDA(gpio_ida); ··· 83 94 84 95 static DEFINE_MUTEX(gpio_lookup_lock); 85 96 static LIST_HEAD(gpio_lookup_list); 97 + 86 98 LIST_HEAD(gpio_devices); 99 + DECLARE_RWSEM(gpio_devices_sem); 87 100 88 101 static DEFINE_MUTEX(gpio_machine_hogs_mutex); 89 102 static LIST_HEAD(gpio_machine_hogs); ··· 117 126 struct gpio_desc *gpio_to_desc(unsigned gpio) 118 127 { 119 128 struct gpio_device *gdev; 120 - unsigned long flags; 121 129 122 - spin_lock_irqsave(&gpio_lock, flags); 123 - 124 - list_for_each_entry(gdev, &gpio_devices, list) { 125 - if (gdev->base <= gpio && 126 - gdev->base + gdev->ngpio > gpio) { 127 - spin_unlock_irqrestore(&gpio_lock, flags); 128 - return &gdev->descs[gpio - gdev->base]; 130 + scoped_guard(rwsem_read, &gpio_devices_sem) { 131 + list_for_each_entry(gdev, &gpio_devices, list) { 132 + if (gdev->base <= gpio && 133 + gdev->base + gdev->ngpio > gpio) 134 + return &gdev->descs[gpio - gdev->base]; 129 135 } 130 136 } 131 - 132 - spin_unlock_irqrestore(&gpio_lock, flags); 133 137 134 138 if (!gpio_is_valid(gpio)) 135 139 pr_warn("invalid GPIO %d\n", gpio); ··· 241 255 EXPORT_SYMBOL_GPL(gpio_device_get_base); 242 256 243 257 /** 258 + * gpio_device_get_label() - Get the label of this GPIO device 259 + * @gdev: GPIO device 260 + * 261 + * Returns: 262 + * Pointer to the string containing the GPIO device label. The string's 263 + * lifetime is tied to that of the underlying GPIO device. 264 + */ 265 + const char *gpio_device_get_label(struct gpio_device *gdev) 266 + { 267 + return gdev->label; 268 + } 269 + EXPORT_SYMBOL(gpio_device_get_label); 270 + 271 + /** 244 272 * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device 245 273 * @gdev: GPIO device 246 274 * ··· 276 276 EXPORT_SYMBOL_GPL(gpio_device_get_chip); 277 277 278 278 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 279 - static int gpiochip_find_base(int ngpio) 279 + static int gpiochip_find_base_unlocked(int ngpio) 280 280 { 281 281 struct gpio_device *gdev; 282 282 int base = GPIO_DYNAMIC_BASE; ··· 349 349 * Return -EBUSY if the new chip overlaps with some other chip's integer 350 350 * space. 351 351 */ 352 - static int gpiodev_add_to_list(struct gpio_device *gdev) 352 + static int gpiodev_add_to_list_unlocked(struct gpio_device *gdev) 353 353 { 354 354 struct gpio_device *prev, *next; 355 355 ··· 398 398 static struct gpio_desc *gpio_name_to_desc(const char * const name) 399 399 { 400 400 struct gpio_device *gdev; 401 - unsigned long flags; 402 401 403 402 if (!name) 404 403 return NULL; 405 404 406 - spin_lock_irqsave(&gpio_lock, flags); 405 + guard(rwsem_read)(&gpio_devices_sem); 407 406 408 407 list_for_each_entry(gdev, &gpio_devices, list) { 409 408 struct gpio_desc *desc; 410 409 411 410 for_each_gpio_desc(gdev->chip, desc) { 412 - if (desc->name && !strcmp(desc->name, name)) { 413 - spin_unlock_irqrestore(&gpio_lock, flags); 411 + if (desc->name && !strcmp(desc->name, name)) 414 412 return desc; 415 - } 416 413 } 417 414 } 418 - 419 - spin_unlock_irqrestore(&gpio_lock, flags); 420 415 421 416 return NULL; 422 417 } ··· 650 655 static void gpiodev_release(struct device *dev) 651 656 { 652 657 struct gpio_device *gdev = to_gpio_device(dev); 653 - unsigned long flags; 654 - 655 - spin_lock_irqsave(&gpio_lock, flags); 656 - list_del(&gdev->list); 657 - spin_unlock_irqrestore(&gpio_lock, flags); 658 658 659 659 ida_free(&gpio_ida, gdev->id); 660 660 kfree_const(gdev->label); ··· 807 817 struct lock_class_key *request_key) 808 818 { 809 819 struct gpio_device *gdev; 810 - unsigned long flags; 811 820 unsigned int i; 812 821 int base = 0; 813 822 int ret = 0; ··· 871 882 872 883 gdev->ngpio = gc->ngpio; 873 884 874 - spin_lock_irqsave(&gpio_lock, flags); 885 + scoped_guard(rwsem_write, &gpio_devices_sem) { 886 + /* 887 + * TODO: this allocates a Linux GPIO number base in the global 888 + * GPIO numberspace for this chip. In the long run we want to 889 + * get *rid* of this numberspace and use only descriptors, but 890 + * it may be a pipe dream. It will not happen before we get rid 891 + * of the sysfs interface anyways. 892 + */ 893 + base = gc->base; 875 894 876 - /* 877 - * TODO: this allocates a Linux GPIO number base in the global 878 - * GPIO numberspace for this chip. In the long run we want to 879 - * get *rid* of this numberspace and use only descriptors, but 880 - * it may be a pipe dream. It will not happen before we get rid 881 - * of the sysfs interface anyways. 882 - */ 883 - base = gc->base; 884 - if (base < 0) { 885 - base = gpiochip_find_base(gc->ngpio); 886 895 if (base < 0) { 887 - spin_unlock_irqrestore(&gpio_lock, flags); 888 - ret = base; 889 - base = 0; 896 + base = gpiochip_find_base_unlocked(gc->ngpio); 897 + if (base < 0) { 898 + ret = base; 899 + base = 0; 900 + goto err_free_label; 901 + } 902 + /* 903 + * TODO: it should not be necessary to reflect the assigned 904 + * base outside of the GPIO subsystem. Go over drivers and 905 + * see if anyone makes use of this, else drop this and assign 906 + * a poison instead. 907 + */ 908 + gc->base = base; 909 + } else { 910 + dev_warn(&gdev->dev, 911 + "Static allocation of GPIO base is deprecated, use dynamic allocation.\n"); 912 + } 913 + gdev->base = base; 914 + 915 + ret = gpiodev_add_to_list_unlocked(gdev); 916 + if (ret) { 917 + chip_err(gc, "GPIO integer space overlap, cannot add chip\n"); 890 918 goto err_free_label; 891 919 } 892 - /* 893 - * TODO: it should not be necessary to reflect the assigned 894 - * base outside of the GPIO subsystem. Go over drivers and 895 - * see if anyone makes use of this, else drop this and assign 896 - * a poison instead. 897 - */ 898 - gc->base = base; 899 - } else { 900 - dev_warn(&gdev->dev, 901 - "Static allocation of GPIO base is deprecated, use dynamic allocation.\n"); 920 + 921 + for (i = 0; i < gc->ngpio; i++) 922 + gdev->descs[i].gdev = gdev; 902 923 } 903 - gdev->base = base; 904 - 905 - ret = gpiodev_add_to_list(gdev); 906 - if (ret) { 907 - spin_unlock_irqrestore(&gpio_lock, flags); 908 - chip_err(gc, "GPIO integer space overlap, cannot add chip\n"); 909 - goto err_free_label; 910 - } 911 - 912 - for (i = 0; i < gc->ngpio; i++) 913 - gdev->descs[i].gdev = gdev; 914 - 915 - spin_unlock_irqrestore(&gpio_lock, flags); 916 924 917 925 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->line_state_notifier); 918 926 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->device_notifier); ··· 1001 1015 goto err_print_message; 1002 1016 } 1003 1017 err_remove_from_list: 1004 - spin_lock_irqsave(&gpio_lock, flags); 1005 - list_del(&gdev->list); 1006 - spin_unlock_irqrestore(&gpio_lock, flags); 1018 + scoped_guard(rwsem_write, &gpio_devices_sem) 1019 + list_del(&gdev->list); 1007 1020 err_free_label: 1008 1021 kfree_const(gdev->label); 1009 1022 err_free_descs: ··· 1033 1048 void gpiochip_remove(struct gpio_chip *gc) 1034 1049 { 1035 1050 struct gpio_device *gdev = gc->gpiodev; 1036 - unsigned long flags; 1037 - unsigned int i; 1051 + unsigned long flags; 1052 + unsigned int i; 1038 1053 1039 1054 down_write(&gdev->sem); 1040 1055 ··· 1056 1071 1057 1072 spin_lock_irqsave(&gpio_lock, flags); 1058 1073 for (i = 0; i < gdev->ngpio; i++) { 1059 - if (gpiochip_is_requested(gc, i)) 1074 + if (test_bit(FLAG_REQUESTED, &gdev->descs[i].flags)) 1060 1075 break; 1061 1076 } 1062 1077 spin_unlock_irqrestore(&gpio_lock, flags); ··· 1064 1079 if (i != gdev->ngpio) 1065 1080 dev_crit(&gdev->dev, 1066 1081 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); 1082 + 1083 + scoped_guard(rwsem_write, &gpio_devices_sem) 1084 + list_del(&gdev->list); 1067 1085 1068 1086 /* 1069 1087 * The gpiochip side puts its use of the device to rest here: ··· 1114 1126 */ 1115 1127 might_sleep(); 1116 1128 1117 - guard(spinlock_irqsave)(&gpio_lock); 1129 + guard(rwsem_read)(&gpio_devices_sem); 1118 1130 1119 1131 list_for_each_entry(gdev, &gpio_devices, list) { 1120 1132 if (gdev->chip && match(gdev->chip, data)) ··· 2173 2185 */ 2174 2186 static int gpiod_request_commit(struct gpio_desc *desc, const char *label) 2175 2187 { 2176 - struct gpio_chip *gc = desc->gdev->chip; 2177 - int ret; 2178 - unsigned long flags; 2179 - unsigned offset; 2188 + struct gpio_chip *gc = desc->gdev->chip; 2189 + unsigned long flags; 2190 + unsigned int offset; 2191 + int ret; 2180 2192 2181 2193 if (label) { 2182 2194 label = kstrdup_const(label, GFP_KERNEL); ··· 2288 2300 2289 2301 static bool gpiod_free_commit(struct gpio_desc *desc) 2290 2302 { 2291 - bool ret = false; 2292 - unsigned long flags; 2293 - struct gpio_chip *gc; 2303 + struct gpio_chip *gc; 2304 + unsigned long flags; 2305 + bool ret = false; 2294 2306 2295 2307 might_sleep(); 2296 2308 ··· 2319 2331 #ifdef CONFIG_OF_DYNAMIC 2320 2332 desc->hog = NULL; 2321 2333 #endif 2322 - #ifdef CONFIG_GPIO_CDEV 2323 - WRITE_ONCE(desc->debounce_period_us, 0); 2324 - #endif 2325 2334 ret = true; 2326 2335 } 2327 2336 ··· 2338 2353 return; 2339 2354 2340 2355 if (!gpiod_free_commit(desc)) 2341 - WARN_ON(extra_checks); 2356 + WARN_ON(1); 2342 2357 2343 2358 module_put(desc->gdev->owner); 2344 2359 gpio_device_put(desc->gdev); 2345 2360 } 2346 2361 2347 2362 /** 2348 - * gpiochip_is_requested - return string iff signal was requested 2349 - * @gc: controller managing the signal 2350 - * @offset: of signal within controller's 0..(ngpio - 1) range 2363 + * gpiochip_dup_line_label - Get a copy of the consumer label. 2364 + * @gc: GPIO chip controlling this line. 2365 + * @offset: Hardware offset of the line. 2351 2366 * 2352 - * Returns NULL if the GPIO is not currently requested, else a string. 2353 - * The string returned is the label passed to gpio_request(); if none has been 2354 - * passed it is a meaningless, non-NULL constant. 2367 + * Returns: 2368 + * Pointer to a copy of the consumer label if the line is requested or NULL 2369 + * if it's not. If a valid pointer was returned, it must be freed using 2370 + * kfree(). In case of a memory allocation error, the function returns %ENOMEM. 2355 2371 * 2356 - * This function is for use by GPIO controller drivers. The label can 2357 - * help with diagnostics, and knowing that the signal is used as a GPIO 2358 - * can help avoid accidentally multiplexing it to another controller. 2372 + * Must not be called from atomic context. 2359 2373 */ 2360 - const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset) 2374 + char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset) 2361 2375 { 2362 2376 struct gpio_desc *desc; 2377 + char *label; 2363 2378 2364 2379 desc = gpiochip_get_desc(gc, offset); 2365 2380 if (IS_ERR(desc)) 2366 2381 return NULL; 2367 2382 2368 - if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) 2383 + guard(spinlock_irqsave)(&gpio_lock); 2384 + 2385 + if (!test_bit(FLAG_REQUESTED, &desc->flags)) 2369 2386 return NULL; 2370 - return desc->label; 2387 + 2388 + /* 2389 + * FIXME: Once we mark gpiod_direction_input/output() and 2390 + * gpiod_get_direction() with might_sleep(), we'll be able to protect 2391 + * the GPIO descriptors with mutex (while value setting operations will 2392 + * become lockless). 2393 + * 2394 + * Until this happens, this allocation needs to be atomic. 2395 + */ 2396 + label = kstrdup(desc->label, GFP_ATOMIC); 2397 + if (!label) 2398 + return ERR_PTR(-ENOMEM); 2399 + 2400 + return label; 2371 2401 } 2372 - EXPORT_SYMBOL_GPL(gpiochip_is_requested); 2402 + EXPORT_SYMBOL_GPL(gpiochip_dup_line_label); 2373 2403 2374 2404 /** 2375 2405 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor ··· 2564 2564 */ 2565 2565 int gpiod_direction_input(struct gpio_desc *desc) 2566 2566 { 2567 - struct gpio_chip *gc; 2568 - int ret = 0; 2567 + struct gpio_chip *gc; 2568 + int ret = 0; 2569 2569 2570 2570 VALIDATE_DESC(desc); 2571 2571 gc = desc->gdev->chip; ··· 2914 2914 2915 2915 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) 2916 2916 { 2917 - struct gpio_chip *gc; 2917 + struct gpio_chip *gc; 2918 2918 int value; 2919 2919 2920 2920 gc = desc->gdev->chip; ··· 3209 3209 3210 3210 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) 3211 3211 { 3212 - struct gpio_chip *gc; 3212 + struct gpio_chip *gc; 3213 3213 3214 3214 gc = desc->gdev->chip; 3215 3215 trace_gpio_value(desc_to_gpio(desc), 0, value); ··· 3716 3716 */ 3717 3717 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 3718 3718 { 3719 - might_sleep_if(extra_checks); 3719 + might_sleep(); 3720 3720 VALIDATE_DESC(desc); 3721 3721 return gpiod_get_raw_value_commit(desc); 3722 3722 } ··· 3735 3735 { 3736 3736 int value; 3737 3737 3738 - might_sleep_if(extra_checks); 3738 + might_sleep(); 3739 3739 VALIDATE_DESC(desc); 3740 3740 value = gpiod_get_raw_value_commit(desc); 3741 3741 if (value < 0) ··· 3766 3766 struct gpio_array *array_info, 3767 3767 unsigned long *value_bitmap) 3768 3768 { 3769 - might_sleep_if(extra_checks); 3769 + might_sleep(); 3770 3770 if (!desc_array) 3771 3771 return -EINVAL; 3772 3772 return gpiod_get_array_value_complex(true, true, array_size, ··· 3792 3792 struct gpio_array *array_info, 3793 3793 unsigned long *value_bitmap) 3794 3794 { 3795 - might_sleep_if(extra_checks); 3795 + might_sleep(); 3796 3796 if (!desc_array) 3797 3797 return -EINVAL; 3798 3798 return gpiod_get_array_value_complex(false, true, array_size, ··· 3813 3813 */ 3814 3814 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) 3815 3815 { 3816 - might_sleep_if(extra_checks); 3816 + might_sleep(); 3817 3817 VALIDATE_DESC_VOID(desc); 3818 3818 gpiod_set_raw_value_commit(desc, value); 3819 3819 } ··· 3831 3831 */ 3832 3832 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 3833 3833 { 3834 - might_sleep_if(extra_checks); 3834 + might_sleep(); 3835 3835 VALIDATE_DESC_VOID(desc); 3836 3836 gpiod_set_value_nocheck(desc, value); 3837 3837 } ··· 3854 3854 struct gpio_array *array_info, 3855 3855 unsigned long *value_bitmap) 3856 3856 { 3857 - might_sleep_if(extra_checks); 3857 + might_sleep(); 3858 3858 if (!desc_array) 3859 3859 return -EINVAL; 3860 3860 return gpiod_set_array_value_complex(true, true, array_size, desc_array, ··· 3896 3896 struct gpio_array *array_info, 3897 3897 unsigned long *value_bitmap) 3898 3898 { 3899 - might_sleep_if(extra_checks); 3899 + might_sleep(); 3900 3900 if (!desc_array) 3901 3901 return -EINVAL; 3902 3902 return gpiod_set_array_value_complex(false, true, array_size, ··· 4696 4696 4697 4697 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) 4698 4698 { 4699 - struct gpio_chip *gc = gdev->chip; 4700 - struct gpio_desc *desc; 4701 - unsigned gpio = gdev->base; 4702 - int value; 4703 - bool is_out; 4704 - bool is_irq; 4705 - bool active_low; 4699 + struct gpio_chip *gc = gdev->chip; 4700 + bool active_low, is_irq, is_out; 4701 + unsigned int gpio = gdev->base; 4702 + struct gpio_desc *desc; 4703 + int value; 4706 4704 4707 4705 for_each_gpio_desc(gc, desc) { 4708 4706 if (test_bit(FLAG_REQUESTED, &desc->flags)) { ··· 4725 4727 4726 4728 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) 4727 4729 { 4728 - unsigned long flags; 4729 4730 struct gpio_device *gdev = NULL; 4730 4731 loff_t index = *pos; 4731 4732 4732 4733 s->private = ""; 4733 4734 4734 - spin_lock_irqsave(&gpio_lock, flags); 4735 - list_for_each_entry(gdev, &gpio_devices, list) 4736 - if (index-- == 0) { 4737 - spin_unlock_irqrestore(&gpio_lock, flags); 4735 + guard(rwsem_read)(&gpio_devices_sem); 4736 + 4737 + list_for_each_entry(gdev, &gpio_devices, list) { 4738 + if (index-- == 0) 4738 4739 return gdev; 4739 - } 4740 - spin_unlock_irqrestore(&gpio_lock, flags); 4740 + } 4741 4741 4742 4742 return NULL; 4743 4743 } 4744 4744 4745 4745 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) 4746 4746 { 4747 - unsigned long flags; 4748 4747 struct gpio_device *gdev = v; 4749 4748 void *ret = NULL; 4750 4749 4751 - spin_lock_irqsave(&gpio_lock, flags); 4752 - if (list_is_last(&gdev->list, &gpio_devices)) 4753 - ret = NULL; 4754 - else 4755 - ret = list_first_entry(&gdev->list, struct gpio_device, list); 4756 - spin_unlock_irqrestore(&gpio_lock, flags); 4750 + scoped_guard(rwsem_read, &gpio_devices_sem) { 4751 + if (list_is_last(&gdev->list, &gpio_devices)) 4752 + ret = NULL; 4753 + else 4754 + ret = list_first_entry(&gdev->list, struct gpio_device, 4755 + list); 4756 + } 4757 4757 4758 4758 s->private = "\n"; 4759 4759 ++*pos;
+2 -5
drivers/gpio/gpiolib.h
··· 15 15 #include <linux/gpio/consumer.h> /* for enum gpiod_flags */ 16 16 #include <linux/gpio/driver.h> 17 17 #include <linux/module.h> 18 + #include <linux/mutex.h> 18 19 #include <linux/notifier.h> 19 20 #include <linux/rwsem.h> 20 21 ··· 137 136 138 137 extern spinlock_t gpio_lock; 139 138 extern struct list_head gpio_devices; 139 + extern struct rw_semaphore gpio_devices_sem; 140 140 141 141 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action); 142 142 ··· 149 147 * @label: Name of the consumer 150 148 * @name: Line name 151 149 * @hog: Pointer to the device node that hogs this line (if any) 152 - * @debounce_period_us: Debounce period in microseconds 153 150 * 154 151 * These are obtained using gpiod_get() and are preferable to the old 155 152 * integer-based handles. ··· 185 184 const char *name; 186 185 #ifdef CONFIG_OF_DYNAMIC 187 186 struct device_node *hog; 188 - #endif 189 - #ifdef CONFIG_GPIO_CDEV 190 - /* debounce period in microseconds */ 191 - unsigned int debounce_period_us; 192 187 #endif 193 188 }; 194 189
+6 -5
drivers/pinctrl/intel/pinctrl-baytrail.c
··· 9 9 #include <linux/acpi.h> 10 10 #include <linux/array_size.h> 11 11 #include <linux/bitops.h> 12 + #include <linux/cleanup.h> 12 13 #include <linux/gpio/driver.h> 13 14 #include <linux/init.h> 14 15 #include <linux/interrupt.h> ··· 1174 1173 const char *pull_str = NULL; 1175 1174 const char *pull = NULL; 1176 1175 unsigned long flags; 1177 - const char *label; 1178 1176 unsigned int pin; 1179 1177 1180 1178 pin = vg->soc->pins[i].number; ··· 1200 1200 seq_printf(s, "Pin %i: can't retrieve community\n", pin); 1201 1201 continue; 1202 1202 } 1203 - label = gpiochip_is_requested(chip, i); 1204 - if (!label) 1205 - label = "Unrequested"; 1203 + 1204 + char *label __free(kfree) = gpiochip_dup_line_label(chip, i); 1205 + if (IS_ERR(label)) 1206 + continue; 1206 1207 1207 1208 switch (conf0 & BYT_PULL_ASSIGN_MASK) { 1208 1209 case BYT_PULL_ASSIGN_UP: ··· 1232 1231 seq_printf(s, 1233 1232 " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s", 1234 1233 pin, 1235 - label, 1234 + label ?: "Unrequested", 1236 1235 val & BYT_INPUT_EN ? " " : "in", 1237 1236 val & BYT_OUTPUT_EN ? " " : "out", 1238 1237 str_hi_lo(val & BYT_LEVEL),
+7 -2
drivers/pinctrl/nomadik/pinctrl-abx500.c
··· 6 6 * 7 7 * Driver allows to use AxB5xx unused pins to be used as GPIO 8 8 */ 9 + 9 10 #include <linux/bitops.h> 11 + #include <linux/cleanup.h> 10 12 #include <linux/err.h> 11 13 #include <linux/gpio/driver.h> 12 14 #include <linux/init.h> ··· 455 453 unsigned offset, unsigned gpio) 456 454 { 457 455 struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev); 458 - const char *label = gpiochip_is_requested(chip, offset - 1); 459 456 u8 gpio_offset = offset - 1; 460 457 int mode = -1; 461 458 bool is_out; 462 459 bool pd; 463 - int ret; 460 + int ret = -ENOMEM; 464 461 465 462 const char *modes[] = { 466 463 [ABX500_DEFAULT] = "default", ··· 474 473 [ABX500_GPIO_PULL_NONE + 1] = "pull none", 475 474 [ABX500_GPIO_PULL_UP] = "pull up", 476 475 }; 476 + 477 + char *label __free(kfree) = gpiochip_dup_line_label(chip, offset - 1); 478 + if (IS_ERR(label)) 479 + goto out; 477 480 478 481 ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, 479 482 gpio_offset, &is_out);
+5 -1
drivers/pinctrl/nomadik/pinctrl-nomadik.c
··· 8 8 * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org> 9 9 */ 10 10 #include <linux/bitops.h> 11 + #include <linux/cleanup.h> 11 12 #include <linux/clk.h> 12 13 #include <linux/device.h> 13 14 #include <linux/err.h> ··· 918 917 struct pinctrl_dev *pctldev, struct gpio_chip *chip, 919 918 unsigned offset, unsigned gpio) 920 919 { 921 - const char *label = gpiochip_is_requested(chip, offset); 922 920 struct nmk_gpio_chip *nmk_chip = gpiochip_get_data(chip); 923 921 int mode; 924 922 bool is_out; ··· 933 933 [NMK_GPIO_ALT_C+3] = "altC3", 934 934 [NMK_GPIO_ALT_C+4] = "altC4", 935 935 }; 936 + 937 + char *label = gpiochip_dup_line_label(chip, offset); 938 + if (IS_ERR(label)) 939 + return; 936 940 937 941 clk_enable(nmk_chip->clk); 938 942 is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & BIT(offset));
+5 -5
drivers/pinctrl/sunplus/sppctl.c
··· 4 4 * Copyright (C) Sunplus Tech / Tibbo Tech. 5 5 */ 6 6 7 + #include <linux/cleanup.h> 7 8 #include <linux/bitfield.h> 8 9 #include <linux/device.h> 9 10 #include <linux/err.h> ··· 501 500 502 501 static void sppctl_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) 503 502 { 504 - const char *label; 505 503 int i; 506 504 507 505 for (i = 0; i < chip->ngpio; i++) { 508 - label = gpiochip_is_requested(chip, i); 509 - if (!label) 510 - label = ""; 506 + char *label __free(kfree) = gpiochip_dup_line_label(chip, i); 507 + if (IS_ERR(label)) 508 + continue; 511 509 512 510 seq_printf(s, " gpio-%03d (%-16.16s | %-16.16s)", i + chip->base, 513 - chip->names[i], label); 511 + chip->names[i], label ?: ""); 514 512 seq_printf(s, " %c", sppctl_gpio_get_direction(chip, i) ? 'I' : 'O'); 515 513 seq_printf(s, ":%d", sppctl_gpio_get(chip, i)); 516 514 seq_printf(s, " %s", sppctl_first_get(chip, i) ? "gpi" : "mux");
+31 -12
include/linux/gpio/driver.h
··· 531 531 #endif /* CONFIG_OF_GPIO */ 532 532 }; 533 533 534 - const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned int offset); 534 + char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset); 535 + 536 + 537 + struct _gpiochip_for_each_data { 538 + const char **label; 539 + unsigned int *i; 540 + }; 541 + 542 + DEFINE_CLASS(_gpiochip_for_each_data, 543 + struct _gpiochip_for_each_data, 544 + if (*_T.label) kfree(*_T.label), 545 + ({ 546 + struct _gpiochip_for_each_data _data = { label, i }; 547 + *_data.i = 0; 548 + _data; 549 + }), 550 + const char **label, int *i) 535 551 536 552 /** 537 553 * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range 538 - * @chip: the chip to query 539 - * @i: loop variable 540 - * @base: first GPIO in the range 541 - * @size: amount of GPIOs to check starting from @base 542 - * @label: label of current GPIO 554 + * @_chip: the chip to query 555 + * @_i: loop variable 556 + * @_base: first GPIO in the range 557 + * @_size: amount of GPIOs to check starting from @base 558 + * @_label: label of current GPIO 543 559 */ 544 - #define for_each_requested_gpio_in_range(chip, i, base, size, label) \ 545 - for (i = 0; i < size; i++) \ 546 - if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else 560 + #define for_each_requested_gpio_in_range(_chip, _i, _base, _size, _label) \ 561 + for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \ 562 + *_data.i < _size; \ 563 + (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \ 564 + if ((*_data.label = \ 565 + gpiochip_dup_line_label(_chip, _base + *_data.i)) == NULL) {} \ 566 + else if (IS_ERR(*_data.label)) {} \ 567 + else 547 568 548 569 /* Iterates over all requested GPIO of the given @chip */ 549 570 #define for_each_requested_gpio(chip, i, label) \ ··· 722 701 #else 723 702 724 703 #include <asm/bug.h> 725 - #include <asm/errno.h> 726 704 727 705 static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc, 728 706 struct irq_domain *domain) ··· 806 786 807 787 /* struct gpio_device getters */ 808 788 int gpio_device_get_base(struct gpio_device *gdev); 789 + const char *gpio_device_get_label(struct gpio_device *gdev); 809 790 810 791 #else /* CONFIG_GPIOLIB */ 811 - 812 - #include <linux/err.h> 813 792 814 793 #include <asm/bug.h> 815 794
+26
include/linux/property.h
··· 80 80 81 81 bool fwnode_device_is_available(const struct fwnode_handle *fwnode); 82 82 83 + static inline bool fwnode_device_is_big_endian(const struct fwnode_handle *fwnode) 84 + { 85 + if (fwnode_property_present(fwnode, "big-endian")) 86 + return true; 87 + if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && 88 + fwnode_property_present(fwnode, "native-endian")) 89 + return true; 90 + return false; 91 + } 92 + 83 93 static inline 84 94 bool fwnode_device_is_compatible(const struct fwnode_handle *fwnode, const char *compat) 85 95 { 86 96 return fwnode_property_match_string(fwnode, "compatible", compat) >= 0; 97 + } 98 + 99 + /** 100 + * device_is_big_endian - check if a device has BE registers 101 + * @dev: Pointer to the struct device 102 + * 103 + * Returns: true if the device has a "big-endian" property, or if the kernel 104 + * was compiled for BE *and* the device has a "native-endian" property. 105 + * Returns false otherwise. 106 + * 107 + * Callers would nominally use ioread32be/iowrite32be if 108 + * device_is_big_endian() == true, or readl/writel otherwise. 109 + */ 110 + static inline bool device_is_big_endian(const struct device *dev) 111 + { 112 + return fwnode_device_is_big_endian(dev_fwnode(dev)); 87 113 } 88 114 89 115 /**