About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Documentation / gpio / board.txt




Custom Search

Based on kernel version 4.1. Page generated on 2015-06-28 12:12 EST.

1	GPIO Mappings
2	=============
3	
4	This document explains how GPIOs can be assigned to given devices and functions.
5	Note that it only applies to the new descriptor-based interface. For a
6	description of the deprecated integer-based GPIO interface please refer to
7	gpio-legacy.txt (actually, there is no real mapping possible with the old
8	interface; you just fetch an integer from somewhere and request the
9	corresponding GPIO.
10	
11	Platforms that make use of GPIOs must select ARCH_REQUIRE_GPIOLIB (if GPIO usage
12	is mandatory) or ARCH_WANT_OPTIONAL_GPIOLIB (if GPIO support can be omitted) in
13	their Kconfig. Then, how GPIOs are mapped depends on what the platform uses to
14	describe its hardware layout. Currently, mappings can be defined through device
15	tree, ACPI, and platform data.
16	
17	Device Tree
18	-----------
19	GPIOs can easily be mapped to devices and functions in the device tree. The
20	exact way to do it depends on the GPIO controller providing the GPIOs, see the
21	device tree bindings for your controller.
22	
23	GPIOs mappings are defined in the consumer device's node, in a property named
24	<function>-gpios, where <function> is the function the driver will request
25	through gpiod_get(). For example:
26	
27		foo_device {
28			compatible = "acme,foo";
29			...
30			led-gpios = <&gpio 15 GPIO_ACTIVE_HIGH>, /* red */
31				    <&gpio 16 GPIO_ACTIVE_HIGH>, /* green */
32				    <&gpio 17 GPIO_ACTIVE_HIGH>; /* blue */
33	
34			power-gpios = <&gpio 1 GPIO_ACTIVE_LOW>;
35		};
36	
37	This property will make GPIOs 15, 16 and 17 available to the driver under the
38	"led" function, and GPIO 1 as the "power" GPIO:
39	
40		struct gpio_desc *red, *green, *blue, *power;
41	
42		red = gpiod_get_index(dev, "led", 0);
43		green = gpiod_get_index(dev, "led", 1);
44		blue = gpiod_get_index(dev, "led", 2);
45	
46		power = gpiod_get(dev, "power");
47	
48	The led GPIOs will be active-high, while the power GPIO will be active-low (i.e.
49	gpiod_is_active_low(power) will be true).
50	
51	ACPI
52	----
53	ACPI also supports function names for GPIOs in a similar fashion to DT.
54	The above DT example can be converted to an equivalent ACPI description
55	with the help of _DSD (Device Specific Data), introduced in ACPI 5.1:
56	
57		Device (FOO) {
58			Name (_CRS, ResourceTemplate () {
59				GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
60					"\\_SB.GPI0") {15} // red
61				GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
62					"\\_SB.GPI0") {16} // green
63				GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
64					"\\_SB.GPI0") {17} // blue
65				GpioIo (Exclusive, ..., IoRestrictionOutputOnly,
66					"\\_SB.GPI0") {1} // power
67			})
68	
69			Name (_DSD, Package () {
70				ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
71				Package () {
72					Package () {
73						"led-gpios",
74						Package () {
75							^FOO, 0, 0, 1,
76							^FOO, 1, 0, 1,
77							^FOO, 2, 0, 1,
78						}
79					},
80					Package () {
81						"power-gpios",
82						Package () {^FOO, 3, 0, 0},
83					},
84				}
85			})
86		}
87	
88	For more information about the ACPI GPIO bindings see
89	Documentation/acpi/gpio-properties.txt.
90	
91	Platform Data
92	-------------
93	Finally, GPIOs can be bound to devices and functions using platform data. Board
94	files that desire to do so need to include the following header:
95	
96		#include <linux/gpio/machine.h>
97	
98	GPIOs are mapped by the means of tables of lookups, containing instances of the
99	gpiod_lookup structure. Two macros are defined to help declaring such mappings:
100	
101		GPIO_LOOKUP(chip_label, chip_hwnum, dev_id, con_id, flags)
102		GPIO_LOOKUP_IDX(chip_label, chip_hwnum, dev_id, con_id, idx, flags)
103	
104	where
105	
106	  - chip_label is the label of the gpiod_chip instance providing the GPIO
107	  - chip_hwnum is the hardware number of the GPIO within the chip
108	  - dev_id is the identifier of the device that will make use of this GPIO. It
109		can be NULL, in which case it will be matched for calls to gpiod_get()
110		with a NULL device.
111	  - con_id is the name of the GPIO function from the device point of view. It
112		can be NULL, in which case it will match any function.
113	  - idx is the index of the GPIO within the function.
114	  - flags is defined to specify the following properties:
115		* GPIOF_ACTIVE_LOW	- to configure the GPIO as active-low
116		* GPIOF_OPEN_DRAIN	- GPIO pin is open drain type.
117		* GPIOF_OPEN_SOURCE	- GPIO pin is open source type.
118	
119	In the future, these flags might be extended to support more properties.
120	
121	Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0.
122	
123	A lookup table can then be defined as follows, with an empty entry defining its
124	end:
125	
126	struct gpiod_lookup_table gpios_table = {
127		.dev_id = "foo.0",
128		.table = {
129			GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
130			GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH),
131			GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH),
132			GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
133			{ },
134		},
135	};
136	
137	And the table can be added by the board code as follows:
138	
139		gpiod_add_lookup_table(&gpios_table);
140	
141	The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:
142	
143		struct gpio_desc *red, *green, *blue, *power;
144	
145		red = gpiod_get_index(dev, "led", 0);
146		green = gpiod_get_index(dev, "led", 1);
147		blue = gpiod_get_index(dev, "led", 2);
148	
149		power = gpiod_get(dev, "power");
150		gpiod_direction_output(power, 1);
151	
152	Since the "power" GPIO is mapped as active-low, its actual signal will be 0
153	after this code. Contrary to the legacy integer GPIO interface, the active-low
154	property is handled during mapping and is thus transparent to GPIO consumers.
Hide Line Numbers
About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Information is copyright its respective author. All material is available from the Linux Kernel Source distributed under a GPL License. This page is provided as a free service by mjmwired.net.