Skip to content

Commit c21cde6

Browse files
committed
gpio: document interaction with other subsystems
Now I am very fed up with people reinventing kernel wheels in userspace "just because they can" (read, sysfs). Put in a angry blurb in sysfs doc and put in a new file with pointers to other subsystem drivers utilizing GPIOs. Signed-off-by: Linus Walleij <[email protected]>
1 parent bcae888 commit c21cde6

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

Documentation/gpio/00-INDEX

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ consumer.txt
66
- How to obtain and use GPIOs in a driver
77
driver.txt
88
- How to write a GPIO driver
9+
drivers-on-gpio.txt:
10+
- Drivers in other subsystems that can use GPIO to provide more
11+
complex functionality.
912
board.txt
1013
- How to assign GPIOs to a consumer device and a function
1114
sysfs.txt
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Subsystem drivers using GPIO
2+
============================
3+
4+
Note that standard kernel drivers exist for common GPIO tasks and will provide
5+
the right in-kernel and userspace APIs/ABIs for the job, and that these
6+
drivers can quite easily interconnect with other kernel subsystems using
7+
hardware descriptions such as device tree or ACPI:
8+
9+
- leds-gpio: drivers/leds/leds-gpio.c will handle LEDs connected to GPIO
10+
lines, giving you the LED sysfs interface
11+
12+
- ledtrig-gpio: drivers/leds/trigger/ledtrig-gpio.c will provide a LED trigger,
13+
i.e. a LED will turn on/off in response to a GPIO line going high or low
14+
(and that LED may in turn use the leds-gpio as per above).
15+
16+
- gpio-keys: drivers/input/keyboard/gpio_keys.c is used when your GPIO line
17+
can generate interrupts in response to a key press. Also supports debounce.
18+
19+
- gpio-keys-polled: drivers/input/keyboard/gpio_keys_polled.c is used when your
20+
GPIO line cannot generate interrupts, so it needs to be periodically polled
21+
by a timer.
22+
23+
- gpio_mouse: drivers/input/mouse/gpio_mouse.c is used to provide a mouse with
24+
up to three buttons by simply using GPIOs and no mouse port. You can cut the
25+
mouse cable and connect the wires to GPIO lines or solder a mouse connector
26+
to the lines for a more permanent solution of this type.
27+
28+
- gpio-beeper: drivers/input/misc/gpio-beeper.c is used to provide a beep from
29+
an external speaker connected to a GPIO line.
30+
31+
- gpio-tilt-polled: drivers/input/misc/gpio_tilt_polled.c provides tilt
32+
detection switches using GPIO, which is useful for your homebrewn pinball
33+
machine if for nothing else. It can detect different tilt angles of the
34+
monitored object.
35+
36+
- extcon-gpio: drivers/extcon/extcon-gpio.c is used when you need to read an
37+
external connector status, such as a headset line for an audio driver or an
38+
HDMI connector. It will provide a better userspace sysfs interface than GPIO.
39+
40+
- restart-gpio: drivers/power/gpio-restart.c is used to restart/reboot the
41+
system by pulling a GPIO line and will register a restart handler so
42+
userspace can issue the right system call to restart the system.
43+
44+
- poweroff-gpio: drivers/power/gpio-poweroff.c is used to power the system down
45+
by pulling a GPIO line and will register a pm_power_off() callback so that
46+
userspace can issue the right system call to power down the system.
47+
48+
- gpio-gate-clock: drivers/clk/clk-gpio-gate.c is used to control a gated clock
49+
(off/on) that uses a GPIO, and integrated with the clock subsystem.
50+
51+
- i2c-gpio: drivers/i2c/busses/i2c-gpio.c is used to drive an I2C bus
52+
(two wires, SDA and SCL lines) by hammering (bitbang) two GPIO lines. It will
53+
appear as any other I2C bus to the system and makes it possible to connect
54+
drivers for the I2C devices on the bus like any other I2C bus driver.
55+
56+
- spi_gpio: drivers/spi/spi-gpio.c is used to drive an SPI bus (variable number
57+
of wires, atleast SCK and optionally MISO, MOSI and chip select lines) using
58+
GPIO hammering (bitbang). It will appear as any other SPI bus on the system
59+
and makes it possible to connect drivers for SPI devices on the bus like
60+
any other SPI bus driver. For example any MMC/SD card can then be connected
61+
to this SPI by using the mmc_spi host from the MMC/SD card subsystem.
62+
63+
- w1-gpio: drivers/w1/masters/w1-gpio.c is used to drive a one-wire bus using
64+
a GPIO line, integrating with the W1 subsystem and handling devices on
65+
the bus like any other W1 device.
66+
67+
- gpio-fan: drivers/hwmon/gpio-fan.c is used to control a fan for cooling the
68+
system, connected to a GPIO line (and optionally a GPIO alarm line),
69+
presenting all the right in-kernel and sysfs interfaces to make your system
70+
not overheat.
71+
72+
- gpio-regulator: drivers/regulator/gpio-regulator.c is used to control a
73+
regulator providing a certain voltage by pulling a GPIO line, integrating
74+
with the regulator subsystem and giving you all the right interfaces.
75+
76+
- gpio-wdt: drivers/watchdog/gpio_wdt.c is used to provide a watchdog timer
77+
that will periodically "ping" a hardware connected to a GPIO line by toggling
78+
it from 1-to-0-to-1. If that hardware does not recieve its "ping"
79+
periodically, it will reset the system.
80+
81+
- gpio-nand: drivers/mtd/nand/gpio.c is used to connect a NAND flash chip to
82+
a set of simple GPIO lines: RDY, NCE, ALE, CLE, NWP. It interacts with the
83+
NAND flash MTD subsystem and provides chip access and partition parsing like
84+
any other NAND driving hardware.
85+
86+
Apart from this there are special GPIO drivers in subsystems like MMC/SD to
87+
read card detect and write protect GPIO lines, and in the TTY serial subsystem
88+
to emulate MCTRL (modem control) signals CTS/RTS by using two GPIO lines. The
89+
MTD NOR flash has add-ons for extra GPIO lines too, though the address bus is
90+
usually connected directly to the flash.
91+
92+
Use those instead of talking directly to the GPIOs using sysfs; they integrate
93+
with kernel frameworks better than your userspace code could. Needless to say,
94+
just using the apropriate kernel drivers will simplify and speed up your
95+
embedded hacking in particular by providing ready-made components.

Documentation/gpio/sysfs.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ userspace GPIO can be used to determine system configuration data that
2020
standard kernels won't know about. And for some tasks, simple userspace
2121
GPIO drivers could be all that the system really needs.
2222

23-
Note that standard kernel drivers exist for common "LEDs and Buttons"
24-
GPIO tasks: "leds-gpio" and "gpio_keys", respectively. Use those
25-
instead of talking directly to the GPIOs; they integrate with kernel
26-
frameworks better than your userspace code could.
27-
23+
DO NOT ABUSE SYFS TO CONTROL HARDWARE THAT HAS PROPER KERNEL DRIVERS.
24+
PLEASE READ THE DOCUMENT NAMED "drivers-on-gpio.txt" IN THIS DOCUMENTATION
25+
DIRECTORY TO AVOID REINVENTING KERNEL WHEELS IN USERSPACE. I MEAN IT.
26+
REALLY.
2827

2928
Paths in Sysfs
3029
--------------

0 commit comments

Comments
 (0)