18c2ecf20Sopenharmony_ci=========================
28c2ecf20Sopenharmony_ciUsing GPIO Lines in Linux
38c2ecf20Sopenharmony_ci=========================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciThe Linux kernel exists to abstract and present hardware to users. GPIO lines
68c2ecf20Sopenharmony_cias such are normally not user facing abstractions. The most obvious, natural
78c2ecf20Sopenharmony_ciand preferred way to use GPIO lines is to let kernel hardware drivers deal
88c2ecf20Sopenharmony_ciwith them.
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciFor examples of already existing generic drivers that will also be good
118c2ecf20Sopenharmony_ciexamples for any other kernel drivers you want to author, refer to
128c2ecf20Sopenharmony_ci:doc:`drivers-on-gpio`
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciFor any kind of mass produced system you want to support, such as servers,
158c2ecf20Sopenharmony_cilaptops, phones, tablets, routers, and any consumer or office or business goods
168c2ecf20Sopenharmony_ciusing appropriate kernel drivers is paramount. Submit your code for inclusion
178c2ecf20Sopenharmony_ciin the upstream Linux kernel when you feel it is mature enough and you will get
188c2ecf20Sopenharmony_cihelp to refine it, see :doc:`../../process/submitting-patches`.
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciIn Linux GPIO lines also have a userspace ABI.
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciThe userspace ABI is intended for one-off deployments. Examples are prototypes,
238c2ecf20Sopenharmony_cifactory lines, maker community projects, workshop specimen, production tools,
248c2ecf20Sopenharmony_ciindustrial automation, PLC-type use cases, door controllers, in short a piece
258c2ecf20Sopenharmony_ciof specialized equipment that is not produced by the numbers, requiring
268c2ecf20Sopenharmony_cioperators to have a deep knowledge of the equipment and knows about the
278c2ecf20Sopenharmony_cisoftware-hardware interface to be set up. They should not have a natural fit
288c2ecf20Sopenharmony_cito any existing kernel subsystem and not be a good fit for an operating system,
298c2ecf20Sopenharmony_cibecause of not being reusable or abstract enough, or involving a lot of non
308c2ecf20Sopenharmony_cicomputer hardware related policy.
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciApplications that have a good reason to use the industrial I/O (IIO) subsystem
338c2ecf20Sopenharmony_cifrom userspace will likely be a good fit for using GPIO lines from userspace as
348c2ecf20Sopenharmony_ciwell.
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciDo not under any circumstances abuse the GPIO userspace ABI to cut corners in
378c2ecf20Sopenharmony_ciany product development projects. If you use it for prototyping, then do not
388c2ecf20Sopenharmony_ciproductify the prototype: rewrite it using proper kernel drivers. Do not under
398c2ecf20Sopenharmony_ciany circumstances deploy any uniform products using GPIO from userspace.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciThe userspace ABI is a character device for each GPIO hardware unit (GPIO chip).
428c2ecf20Sopenharmony_ciThese devices will appear on the system as ``/dev/gpiochip0`` thru
438c2ecf20Sopenharmony_ci``/dev/gpiochipN``. Examples of how to directly use the userspace ABI can be
448c2ecf20Sopenharmony_cifound in the kernel tree ``tools/gpio`` subdirectory.
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciFor structured and managed applications, we recommend that you make use of the
478c2ecf20Sopenharmony_cilibgpiod_ library. This provides helper abstractions, command line utlities
488c2ecf20Sopenharmony_ciand arbitration for multiple simultaneous consumers on the same GPIO chip.
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci.. _libgpiod: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/
51