18c2ecf20Sopenharmony_ciThis is a place for planning the ongoing long-term work in the GPIO
28c2ecf20Sopenharmony_cisubsystem.
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciGPIO descriptors
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciStarting with commit 79a9becda894 the GPIO subsystem embarked on a journey
88c2ecf20Sopenharmony_cito move away from the global GPIO numberspace and toward a descriptor-based
98c2ecf20Sopenharmony_ciapproach. This means that GPIO consumers, drivers and machine descriptions
108c2ecf20Sopenharmony_ciideally have no use or idea of the global GPIO numberspace that has/was
118c2ecf20Sopenharmony_ciused in the inception of the GPIO subsystem.
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciThe numberspace issue is the same as to why irq is moving away from irq
148c2ecf20Sopenharmony_cinumbers to IRQ descriptors.
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciThe underlying motivation for this is that the GPIO numberspace has become
178c2ecf20Sopenharmony_ciunmanageable: machine board files tend to become full of macros trying to
188c2ecf20Sopenharmony_ciestablish the numberspace at compile-time, making it hard to add any numbers
198c2ecf20Sopenharmony_ciin the middle (such as if you missed a pin on a chip) without the numberspace
208c2ecf20Sopenharmony_cibreaking.
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciMachine descriptions such as device tree or ACPI does not have a concept of the
238c2ecf20Sopenharmony_ciLinux GPIO number as those descriptions are external to the Linux kernel
248c2ecf20Sopenharmony_ciand treat GPIO lines as abstract entities.
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciThe runtime-assigned GPIO numberspace (what you get if you assign the GPIO
278c2ecf20Sopenharmony_cibase as -1 in struct gpio_chip) has also became unpredictable due to factors
288c2ecf20Sopenharmony_cisuch as probe ordering and the introduction of -EPROBE_DEFER making probe
298c2ecf20Sopenharmony_ciordering of independent GPIO chips essentially unpredictable, as their base
308c2ecf20Sopenharmony_cinumber will be assigned on a first come first serve basis.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciThe best way to get out of the problem is to make the global GPIO numbers
338c2ecf20Sopenharmony_ciunimportant by simply not using them. GPIO descriptors deal with this.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciWork items:
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci- Convert all GPIO device drivers to only #include <linux/gpio/driver.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci- Convert all consumer drivers to only #include <linux/gpio/consumer.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci- Convert all machine descriptors in "boardfiles" to only
428c2ecf20Sopenharmony_ci  #include <linux/gpio/machine.h>, the other option being to convert it
438c2ecf20Sopenharmony_ci  to a machine description such as device tree, ACPI or fwnode that
448c2ecf20Sopenharmony_ci  implicitly does not use global GPIO numbers.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci- When this work is complete (will require some of the items in the
478c2ecf20Sopenharmony_ci  following ongoing work as well) we can delete the old global
488c2ecf20Sopenharmony_ci  numberspace accessors from <linux/gpio.h> and eventually delete
498c2ecf20Sopenharmony_ci  <linux/gpio.h> altogether.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciGet rid of <linux/of_gpio.h>
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciThis header and helpers appeared at one point when there was no proper
558c2ecf20Sopenharmony_cidriver infrastructure for doing simpler MMIO GPIO devices and there was
568c2ecf20Sopenharmony_cino core support for parsing device tree GPIOs from the core library with
578c2ecf20Sopenharmony_cithe [devm_]gpiod_get() calls we have today that will implicitly go into
588c2ecf20Sopenharmony_cithe device tree back-end. It is legacy and should not be used in new code.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciWork items:
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci- Get rid of struct of_mm_gpio_chip altogether: use the generic  MMIO
638c2ecf20Sopenharmony_ci  GPIO for all current users (see below). Delete struct of_mm_gpio_chip,
648c2ecf20Sopenharmony_ci  to_of_mm_gpio_chip(), of_mm_gpiochip_add_data(), of_mm_gpiochip_add()
658c2ecf20Sopenharmony_ci  of_mm_gpiochip_remove() from the kernel.
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci- Change all consumer drivers that #include <linux/of_gpio.h> to
688c2ecf20Sopenharmony_ci  #include <linux/gpio/consumer.h> and stop doing custom parsing of the
698c2ecf20Sopenharmony_ci  GPIO lines from the device tree. This can be tricky and often ivolves
708c2ecf20Sopenharmony_ci  changing boardfiles, etc.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci- Pull semantics for legacy device tree (OF) GPIO lookups into
738c2ecf20Sopenharmony_ci  gpiolib-of.c: in some cases subsystems are doing custom flags and
748c2ecf20Sopenharmony_ci  lookups for polarity inversion, open drain and what not. As we now
758c2ecf20Sopenharmony_ci  handle this with generic OF bindings, pull all legacy handling into
768c2ecf20Sopenharmony_ci  gpiolib so the library API becomes narrow and deep and handle all
778c2ecf20Sopenharmony_ci  legacy bindings internally. (See e.g. commits 6953c57ab172,
788c2ecf20Sopenharmony_ci  6a537d48461d etc)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci- Delete <linux/of_gpio.h> when all the above is complete and everything
818c2ecf20Sopenharmony_ci  uses <linux/gpio/consumer.h> or <linux/gpio/driver.h> instead.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ciGet rid of <linux/gpio.h>
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciThis legacy header is a one stop shop for anything GPIO is closely tied
878c2ecf20Sopenharmony_cito the global GPIO numberspace. The endgame of the above refactorings will
888c2ecf20Sopenharmony_cibe the removal of <linux/gpio.h> and from that point only the specialized
898c2ecf20Sopenharmony_ciheaders under <linux/gpio/*.h> will be used. This requires all the above to
908c2ecf20Sopenharmony_cibe completed and is expected to take a long time.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciCollect drivers
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciCollect GPIO drivers from arch/* and other places that should be placed
968c2ecf20Sopenharmony_ciin drivers/gpio/gpio-*. Augment platforms to create platform devices or
978c2ecf20Sopenharmony_cisimilar and probe a proper driver in the gpiolib subsystem.
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciIn some cases it makes sense to create a GPIO chip from the local driver
1008c2ecf20Sopenharmony_cifor a few GPIOs. Those should stay where they are.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciAt the same time it makes sense to get rid of code duplication in existing or
1038c2ecf20Sopenharmony_cinew coming drivers. For example, gpio-ml-ioh should be incorporated into
1048c2ecf20Sopenharmony_cigpio-pch. In similar way gpio-intel-mid into gpio-pxa.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciGeneric MMIO GPIO
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciThe GPIO drivers can utilize the generic MMIO helper library in many
1108c2ecf20Sopenharmony_cicases, and the helper library should be as helpful as possible for MMIO
1118c2ecf20Sopenharmony_cidrivers. (drivers/gpio/gpio-mmio.c)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciWork items:
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci- Look over and identify any remaining easily converted drivers and
1168c2ecf20Sopenharmony_ci  dry-code conversions to MMIO GPIO for maintainers to test
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci- Expand the MMIO GPIO or write a new library for regmap-based I/O
1198c2ecf20Sopenharmony_ci  helpers for GPIO drivers on regmap that simply use offsets
1208c2ecf20Sopenharmony_ci  0..n in some register to drive GPIO lines
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci- Expand the MMIO GPIO or write a new library for port-mapped I/O
1238c2ecf20Sopenharmony_ci  helpers (x86 inb()/outb()) and convert port-mapped I/O drivers to use
1248c2ecf20Sopenharmony_ci  this with dry-coding and sending to maintainers to test
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciGPIOLIB irqchip
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciThe GPIOLIB irqchip is a helper irqchip for "simple cases" that should
1308c2ecf20Sopenharmony_citry to cover any generic kind of irqchip cascaded from a GPIO.
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci- Convert all the GPIOLIB_IRQCHIP users to pass an irqchip template,
1338c2ecf20Sopenharmony_ci  parent and flags before calling [devm_]gpiochip_add[_data]().
1348c2ecf20Sopenharmony_ci  Currently we set up the irqchip after setting up the gpiochip
1358c2ecf20Sopenharmony_ci  using gpiochip_irqchip_add() and gpiochip_set_[chained|nested]_irqchip().
1368c2ecf20Sopenharmony_ci  This is too complex, so convert all users over to just set up
1378c2ecf20Sopenharmony_ci  the irqchip before registering the gpio_chip, typical example:
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci  /* Typical state container with dynamic irqchip */
1408c2ecf20Sopenharmony_ci  struct my_gpio {
1418c2ecf20Sopenharmony_ci      struct gpio_chip gc;
1428c2ecf20Sopenharmony_ci      struct irq_chip irq;
1438c2ecf20Sopenharmony_ci  };
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci  int irq; /* from platform etc */
1468c2ecf20Sopenharmony_ci  struct my_gpio *g;
1478c2ecf20Sopenharmony_ci  struct gpio_irq_chip *girq;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci  /* Set up the irqchip dynamically */
1508c2ecf20Sopenharmony_ci  g->irq.name = "my_gpio_irq";
1518c2ecf20Sopenharmony_ci  g->irq.irq_ack = my_gpio_ack_irq;
1528c2ecf20Sopenharmony_ci  g->irq.irq_mask = my_gpio_mask_irq;
1538c2ecf20Sopenharmony_ci  g->irq.irq_unmask = my_gpio_unmask_irq;
1548c2ecf20Sopenharmony_ci  g->irq.irq_set_type = my_gpio_set_irq_type;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci  /* Get a pointer to the gpio_irq_chip */
1578c2ecf20Sopenharmony_ci  girq = &g->gc.irq;
1588c2ecf20Sopenharmony_ci  girq->chip = &g->irq;
1598c2ecf20Sopenharmony_ci  girq->parent_handler = ftgpio_gpio_irq_handler;
1608c2ecf20Sopenharmony_ci  girq->num_parents = 1;
1618c2ecf20Sopenharmony_ci  girq->parents = devm_kcalloc(dev, 1, sizeof(*girq->parents),
1628c2ecf20Sopenharmony_ci                               GFP_KERNEL);
1638c2ecf20Sopenharmony_ci  if (!girq->parents)
1648c2ecf20Sopenharmony_ci      return -ENOMEM;
1658c2ecf20Sopenharmony_ci  girq->default_type = IRQ_TYPE_NONE;
1668c2ecf20Sopenharmony_ci  girq->handler = handle_bad_irq;
1678c2ecf20Sopenharmony_ci  girq->parents[0] = irq;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci  When this is done, we will delete the old APIs for instatiating
1708c2ecf20Sopenharmony_ci  GPIOLIB_IRQCHIP and simplify the code.
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci- Look over and identify any remaining easily converted drivers and
1738c2ecf20Sopenharmony_ci  dry-code conversions to gpiolib irqchip for maintainers to test
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci- Drop gpiochip_set_chained_irqchip() when all the chained irqchips
1768c2ecf20Sopenharmony_ci  have been converted to the above infrastructure.
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci- Add more infrastructure to make it possible to also pass a threaded
1798c2ecf20Sopenharmony_ci  irqchip in struct gpio_irq_chip.
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci- Drop gpiochip_irqchip_add_nested() when all the chained irqchips
1828c2ecf20Sopenharmony_ci  have been converted to the above infrastructure.
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciIncrease integration with pin control
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciThere are already ways to use pin control as back-end for GPIO and
1888c2ecf20Sopenharmony_ciit may make sense to bring these subsystems closer. One reason for
1898c2ecf20Sopenharmony_cicreating pin control as its own subsystem was that we could avoid any
1908c2ecf20Sopenharmony_ciuse of the global GPIO numbers. Once the above is complete, it may
1918c2ecf20Sopenharmony_cimake sense to simply join the subsystems into one and make pin
1928c2ecf20Sopenharmony_cimultiplexing, pin configuration, GPIO, etc selectable options in one
1938c2ecf20Sopenharmony_ciand the same pin control and GPIO subsystem.
194