^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) Using GPIO Lines in Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) The Linux kernel exists to abstract and present hardware to users. GPIO lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) as such are normally not user facing abstractions. The most obvious, natural
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) and preferred way to use GPIO lines is to let kernel hardware drivers deal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) with them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) For examples of already existing generic drivers that will also be good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) examples for any other kernel drivers you want to author, refer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) :doc:`drivers-on-gpio`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) For any kind of mass produced system you want to support, such as servers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) laptops, phones, tablets, routers, and any consumer or office or business goods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) using appropriate kernel drivers is paramount. Submit your code for inclusion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) in the upstream Linux kernel when you feel it is mature enough and you will get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) help to refine it, see :doc:`../../process/submitting-patches`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) In Linux GPIO lines also have a userspace ABI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) The userspace ABI is intended for one-off deployments. Examples are prototypes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) factory lines, maker community projects, workshop specimen, production tools,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) industrial automation, PLC-type use cases, door controllers, in short a piece
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) of specialized equipment that is not produced by the numbers, requiring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) operators to have a deep knowledge of the equipment and knows about the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) software-hardware interface to be set up. They should not have a natural fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) to any existing kernel subsystem and not be a good fit for an operating system,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) because of not being reusable or abstract enough, or involving a lot of non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) computer hardware related policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) Applications that have a good reason to use the industrial I/O (IIO) subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) from userspace will likely be a good fit for using GPIO lines from userspace as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) Do not under any circumstances abuse the GPIO userspace ABI to cut corners in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) any product development projects. If you use it for prototyping, then do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) productify the prototype: rewrite it using proper kernel drivers. Do not under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) any circumstances deploy any uniform products using GPIO from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) The userspace ABI is a character device for each GPIO hardware unit (GPIO chip).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) These devices will appear on the system as ``/dev/gpiochip0`` thru
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ``/dev/gpiochipN``. Examples of how to directly use the userspace ABI can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) found in the kernel tree ``tools/gpio`` subdirectory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) For structured and managed applications, we recommend that you make use of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) libgpiod_ library. This provides helper abstractions, command line utlities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) and arbitration for multiple simultaneous consumers on the same GPIO chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .. _libgpiod: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/