12646b90dSLinus WalleijThis is a place for planning the ongoing long-term work in the GPIO 22646b90dSLinus Walleijsubsystem. 32646b90dSLinus Walleij 45ceb3536SBartosz Golaszewski=============================================================================== 52646b90dSLinus Walleij 62646b90dSLinus WalleijGPIO descriptors 72646b90dSLinus Walleij 82646b90dSLinus WalleijStarting with commit 79a9becda894 the GPIO subsystem embarked on a journey 93abda79aSKieran Binghamto move away from the global GPIO numberspace and toward a descriptor-based 102646b90dSLinus Walleijapproach. This means that GPIO consumers, drivers and machine descriptions 112646b90dSLinus Walleijideally have no use or idea of the global GPIO numberspace that has/was 122646b90dSLinus Walleijused in the inception of the GPIO subsystem. 132646b90dSLinus Walleij 1497082890SLinus WalleijThe numberspace issue is the same as to why irq is moving away from irq 1597082890SLinus Walleijnumbers to IRQ descriptors. 1697082890SLinus Walleij 1797082890SLinus WalleijThe underlying motivation for this is that the GPIO numberspace has become 1897082890SLinus Walleijunmanageable: machine board files tend to become full of macros trying to 1997082890SLinus Walleijestablish the numberspace at compile-time, making it hard to add any numbers 2097082890SLinus Walleijin the middle (such as if you missed a pin on a chip) without the numberspace 2197082890SLinus Walleijbreaking. 2297082890SLinus Walleij 2397082890SLinus WalleijMachine descriptions such as device tree or ACPI does not have a concept of the 2497082890SLinus WalleijLinux GPIO number as those descriptions are external to the Linux kernel 2597082890SLinus Walleijand treat GPIO lines as abstract entities. 2697082890SLinus Walleij 2797082890SLinus WalleijThe runtime-assigned GPIO numberspace (what you get if you assign the GPIO 2897082890SLinus Walleijbase as -1 in struct gpio_chip) has also became unpredictable due to factors 2997082890SLinus Walleijsuch as probe ordering and the introduction of -EPROBE_DEFER making probe 3097082890SLinus Walleijordering of independent GPIO chips essentially unpredictable, as their base 3197082890SLinus Walleijnumber will be assigned on a first come first serve basis. 3297082890SLinus Walleij 3397082890SLinus WalleijThe best way to get out of the problem is to make the global GPIO numbers 3497082890SLinus Walleijunimportant by simply not using them. GPIO descriptors deal with this. 3597082890SLinus Walleij 362646b90dSLinus WalleijWork items: 372646b90dSLinus Walleij 382646b90dSLinus Walleij- Convert all GPIO device drivers to only #include <linux/gpio/driver.h> 392646b90dSLinus Walleij 402646b90dSLinus Walleij- Convert all consumer drivers to only #include <linux/gpio/consumer.h> 412646b90dSLinus Walleij 422646b90dSLinus Walleij- Convert all machine descriptors in "boardfiles" to only 432646b90dSLinus Walleij #include <linux/gpio/machine.h>, the other option being to convert it 442646b90dSLinus Walleij to a machine description such as device tree, ACPI or fwnode that 452646b90dSLinus Walleij implicitly does not use global GPIO numbers. 462646b90dSLinus Walleij 47833c086fSAhmad Fatoum- Fix drivers to not read back struct gpio_chip::base. Some drivers do 48833c086fSAhmad Fatoum that and would be broken by attempts to poison it or make it dynamic. 49833c086fSAhmad Fatoum Example in AT91 pinctrl driver: 50833c086fSAhmad Fatoum https://lore.kernel.org/all/1d00c056-3d61-4c22-bedd-3bae0bf1ddc4@pengutronix.de/ 51833c086fSAhmad Fatoum This particular driver is also DT-only, so with the above fixed, the 52833c086fSAhmad Fatoum base can be made dynamic (set to -1) if CONFIG_GPIO_SYSFS is disabled. 53833c086fSAhmad Fatoum 542646b90dSLinus Walleij- When this work is complete (will require some of the items in the 552646b90dSLinus Walleij following ongoing work as well) we can delete the old global 562646b90dSLinus Walleij numberspace accessors from <linux/gpio.h> and eventually delete 572646b90dSLinus Walleij <linux/gpio.h> altogether. 582646b90dSLinus Walleij 595ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 602646b90dSLinus Walleij 612646b90dSLinus WalleijGet rid of <linux/of_gpio.h> 622646b90dSLinus Walleij 632646b90dSLinus WalleijThis header and helpers appeared at one point when there was no proper 642646b90dSLinus Walleijdriver infrastructure for doing simpler MMIO GPIO devices and there was 652646b90dSLinus Walleijno core support for parsing device tree GPIOs from the core library with 662646b90dSLinus Walleijthe [devm_]gpiod_get() calls we have today that will implicitly go into 6797082890SLinus Walleijthe device tree back-end. It is legacy and should not be used in new code. 682646b90dSLinus Walleij 692646b90dSLinus WalleijWork items: 702646b90dSLinus Walleij 712646b90dSLinus Walleij- Change all consumer drivers that #include <linux/of_gpio.h> to 722646b90dSLinus Walleij #include <linux/gpio/consumer.h> and stop doing custom parsing of the 73f53ab435SShivam Chaudhary GPIO lines from the device tree. This can be tricky and often involves 742646b90dSLinus Walleij changing board files, etc. 752646b90dSLinus Walleij 762646b90dSLinus Walleij- Pull semantics for legacy device tree (OF) GPIO lookups into 772646b90dSLinus Walleij gpiolib-of.c: in some cases subsystems are doing custom flags and 782646b90dSLinus Walleij lookups for polarity inversion, open drain and what not. As we now 792646b90dSLinus Walleij handle this with generic OF bindings, pull all legacy handling into 802646b90dSLinus Walleij gpiolib so the library API becomes narrow and deep and handle all 812646b90dSLinus Walleij legacy bindings internally. (See e.g. commits 6953c57ab172, 822646b90dSLinus Walleij 6a537d48461d etc) 832646b90dSLinus Walleij 842646b90dSLinus Walleij- Delete <linux/of_gpio.h> when all the above is complete and everything 852646b90dSLinus Walleij uses <linux/gpio/consumer.h> or <linux/gpio/driver.h> instead. 862646b90dSLinus Walleij 875ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 882646b90dSLinus Walleij 892646b90dSLinus WalleijCollect drivers 902646b90dSLinus Walleij 912646b90dSLinus WalleijCollect GPIO drivers from arch/* and other places that should be placed 922646b90dSLinus Walleijin drivers/gpio/gpio-*. Augment platforms to create platform devices or 932646b90dSLinus Walleijsimilar and probe a proper driver in the gpiolib subsystem. 942646b90dSLinus Walleij 952646b90dSLinus WalleijIn some cases it makes sense to create a GPIO chip from the local driver 962646b90dSLinus Walleijfor a few GPIOs. Those should stay where they are. 972646b90dSLinus Walleij 9885a94ff8SAndy ShevchenkoAt the same time it makes sense to get rid of code duplication in existing or 9985a94ff8SAndy Shevchenkonew coming drivers. For example, gpio-ml-ioh should be incorporated into 1005f7582aaSAndy Shevchenkogpio-pch. 10185a94ff8SAndy Shevchenko 1025ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 1032646b90dSLinus Walleij 1042646b90dSLinus WalleijGeneric MMIO GPIO 1052646b90dSLinus Walleij 1062646b90dSLinus WalleijThe GPIO drivers can utilize the generic MMIO helper library in many 1072646b90dSLinus Walleijcases, and the helper library should be as helpful as possible for MMIO 1082646b90dSLinus Walleijdrivers. (drivers/gpio/gpio-mmio.c) 1092646b90dSLinus Walleij 1102646b90dSLinus WalleijWork items: 1112646b90dSLinus Walleij 1122646b90dSLinus Walleij- Look over and identify any remaining easily converted drivers and 1132646b90dSLinus Walleij dry-code conversions to MMIO GPIO for maintainers to test 1142646b90dSLinus Walleij 11541c4616bSLinus Walleij- Expand the MMIO GPIO or write a new library for regmap-based I/O 11641c4616bSLinus Walleij helpers for GPIO drivers on regmap that simply use offsets 11741c4616bSLinus Walleij 0..n in some register to drive GPIO lines 11841c4616bSLinus Walleij 1192646b90dSLinus Walleij- Expand the MMIO GPIO or write a new library for port-mapped I/O 1202646b90dSLinus Walleij helpers (x86 inb()/outb()) and convert port-mapped I/O drivers to use 1212646b90dSLinus Walleij this with dry-coding and sending to maintainers to test 1222646b90dSLinus Walleij 1235ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 1242646b90dSLinus Walleij 125c8a51f03SAndy ShevchenkoGeneric regmap GPIO 126c8a51f03SAndy Shevchenko 127c8a51f03SAndy ShevchenkoIn the very similar way to Generic MMIO GPIO convert the users which can 128c8a51f03SAndy Shevchenkotake advantage of using regmap over direct IO accessors. Note, even in 129c8a51f03SAndy ShevchenkoMMIO case the regmap MMIO with gpio-regmap.c is preferable over gpio-mmio.c. 130c8a51f03SAndy Shevchenko 1315ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 132c8a51f03SAndy Shevchenko 1332646b90dSLinus WalleijGPIOLIB irqchip 1342646b90dSLinus Walleij 1352646b90dSLinus WalleijThe GPIOLIB irqchip is a helper irqchip for "simple cases" that should 1362646b90dSLinus Walleijtry to cover any generic kind of irqchip cascaded from a GPIO. 1372646b90dSLinus Walleij 1382646b90dSLinus Walleij- Look over and identify any remaining easily converted drivers and 1392646b90dSLinus Walleij dry-code conversions to gpiolib irqchip for maintainers to test 1402646b90dSLinus Walleij 1415ceb3536SBartosz Golaszewski------------------------------------------------------------------------------- 1422646b90dSLinus Walleij 143afefc326SMarc ZyngierMoving over to immutable irq_chip structures 144afefc326SMarc Zyngier 145afefc326SMarc ZyngierMost of the gpio chips implementing interrupt support rely on gpiolib 146afefc326SMarc Zyngierintercepting some of the irq_chip callbacks, preventing the structures 147afefc326SMarc Zyngierfrom being made read-only and forcing duplication of structures that 148afefc326SMarc Zyngiershould otherwise be unique. 149afefc326SMarc Zyngier 150afefc326SMarc ZyngierThe solution is to call into the gpiolib code when needed (resource 151afefc326SMarc Zyngiermanagement, enable/disable or unmask/mask callbacks), and to let the 152afefc326SMarc Zyngiercore code know about that by exposing a flag (IRQCHIP_IMMUTABLE) in 153afefc326SMarc Zyngierthe irq_chip structure. The irq_chip structure can then be made unique 154afefc326SMarc Zyngierand const. 155afefc326SMarc Zyngier 156afefc326SMarc ZyngierA small number of drivers have been converted (pl061, tegra186, msm, 157afefc326SMarc Zyngieramd, apple), and can be used as examples of how to proceed with this 158afefc326SMarc Zyngierconversion. Note that drivers using the generic irqchip framework 159afefc326SMarc Zyngiercannot be converted yet, but watch this space! 1609ff2443bSBartosz Golaszewski 1619ff2443bSBartosz Golaszewski------------------------------------------------------------------------------- 1629ff2443bSBartosz Golaszewski 163*0c0438d4SBartosz GolaszewskiRemove legacy sysfs features 164af54a2fbSBartosz Golaszewski 165*0c0438d4SBartosz GolaszewskiWe have two parallel per-chip class devices and per-exported-line attribute 166*0c0438d4SBartosz Golaszewskigroups in sysfs. One is using the obsolete global GPIO numberspace and the 167*0c0438d4SBartosz Golaszewskisecond relies on hardware offsets of pins within the chip. Remove the former 168*0c0438d4SBartosz Golaszewskionce user-space has switched to using the latter. 1692de1cf17SBartosz Golaszewski 1702de1cf17SBartosz Golaszewski------------------------------------------------------------------------------- 1712de1cf17SBartosz Golaszewski 1722de1cf17SBartosz GolaszewskiRemove GPIOD_FLAGS_BIT_NONEXCLUSIVE 1732de1cf17SBartosz Golaszewski 1742de1cf17SBartosz GolaszewskiGPIOs in the linux kernel are meant to be an exclusive resource. This means 1752de1cf17SBartosz Golaszewskithat the GPIO descriptors (the software representation of the hardware concept) 1762de1cf17SBartosz Golaszewskiare not reference counted and - in general - only one user at a time can 1772de1cf17SBartosz Golaszewskirequest a GPIO line and control its settings. The consumer API is designed 1782de1cf17SBartosz Golaszewskiaround full control of the line's state as evidenced by the fact that, for 1792de1cf17SBartosz Golaszewskiinstance, gpiod_set_value() does indeed drive the line as requested, instead 1802de1cf17SBartosz Golaszewskiof bumping an enable counter of some sort. 1812de1cf17SBartosz Golaszewski 1822de1cf17SBartosz GolaszewskiA problematic use-case for GPIOs is when two consumers want to use the same 1832de1cf17SBartosz Golaszewskidescriptor independently. An example of such a user is the regulator subsystem 1842de1cf17SBartosz Golaszewskiwhich may instantiate several struct regulator_dev instances containing 1852de1cf17SBartosz Golaszewskia struct device but using the same enable GPIO line. 1862de1cf17SBartosz Golaszewski 1872de1cf17SBartosz GolaszewskiA workaround was introduced in the form of the GPIOD_FLAGS_BIT_NONEXCLUSIVE 1882de1cf17SBartosz Golaszewskiflag but its implementation is problematic: it does not provide any 1892de1cf17SBartosz Golaszewskisynchronization of usage nor did it introduce any enable count meaning the 1902de1cf17SBartosz Golaszewskinon-exclusive users of the same descriptor will in fact "fight" for the 1912de1cf17SBartosz Golaszewskicontrol over it. This flag should be removed and replaced with a better 1922de1cf17SBartosz Golaszewskisolution, possibly based on the new power sequencing subsystem. 1932de1cf17SBartosz Golaszewski 1942de1cf17SBartosz Golaszewski------------------------------------------------------------------------------- 1952de1cf17SBartosz Golaszewski 1962de1cf17SBartosz GolaszewskiRemove devm_gpiod_unhinge() 1972de1cf17SBartosz Golaszewski 1982de1cf17SBartosz Golaszewskidevm_gpiod_unhinge() is provided as a way to transfer the ownership of managed 1992de1cf17SBartosz Golaszewskienable GPIOs to the regulator core. Rather than doing that however, we should 2002de1cf17SBartosz Golaszewskimake it possible for the regulator subsystem to deal with GPIO resources the 2012de1cf17SBartosz Golaszewskilifetime of which it doesn't control as logically, a GPIO obtained by a caller 2022de1cf17SBartosz Golaszewskishould also be freed by it. 203