pin-control.rst 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. ===============================
  2. PINCTRL (PIN CONTROL) subsystem
  3. ===============================
  4. This document outlines the pin control subsystem in Linux
  5. This subsystem deals with:
  6. - Enumerating and naming controllable pins
  7. - Multiplexing of pins, pads, fingers (etc) see below for details
  8. - Configuration of pins, pads, fingers (etc), such as software-controlled
  9. biasing and driving mode specific pins, such as pull-up, pull-down, open drain,
  10. load capacitance etc.
  11. Top-level interface
  12. ===================
  13. Definitions:
  14. - A PIN CONTROLLER is a piece of hardware, usually a set of registers, that
  15. can control PINs. It may be able to multiplex, bias, set load capacitance,
  16. set drive strength, etc. for individual pins or groups of pins.
  17. - PINS are equal to pads, fingers, balls or whatever packaging input or
  18. output line you want to control and these are denoted by unsigned integers
  19. in the range 0..maxpin. This numberspace is local to each PIN CONTROLLER, so
  20. there may be several such number spaces in a system. This pin space may
  21. be sparse - i.e. there may be gaps in the space with numbers where no
  22. pin exists.
  23. When a PIN CONTROLLER is instantiated, it will register a descriptor to the
  24. pin control framework, and this descriptor contains an array of pin descriptors
  25. describing the pins handled by this specific pin controller.
  26. Here is an example of a PGA (Pin Grid Array) chip seen from underneath::
  27. A B C D E F G H
  28. 8 o o o o o o o o
  29. 7 o o o o o o o o
  30. 6 o o o o o o o o
  31. 5 o o o o o o o o
  32. 4 o o o o o o o o
  33. 3 o o o o o o o o
  34. 2 o o o o o o o o
  35. 1 o o o o o o o o
  36. To register a pin controller and name all the pins on this package we can do
  37. this in our driver:
  38. .. code-block:: c
  39. #include <linux/pinctrl/pinctrl.h>
  40. const struct pinctrl_pin_desc foo_pins[] = {
  41. PINCTRL_PIN(0, "A8"),
  42. PINCTRL_PIN(1, "B8"),
  43. PINCTRL_PIN(2, "C8"),
  44. ...
  45. PINCTRL_PIN(61, "F1"),
  46. PINCTRL_PIN(62, "G1"),
  47. PINCTRL_PIN(63, "H1"),
  48. };
  49. static struct pinctrl_desc foo_desc = {
  50. .name = "foo",
  51. .pins = foo_pins,
  52. .npins = ARRAY_SIZE(foo_pins),
  53. .owner = THIS_MODULE,
  54. };
  55. int __init foo_init(void)
  56. {
  57. int error;
  58. struct pinctrl_dev *pctl;
  59. error = pinctrl_register_and_init(&foo_desc, <PARENT>, NULL, &pctl);
  60. if (error)
  61. return error;
  62. return pinctrl_enable(pctl);
  63. }
  64. To enable the pinctrl subsystem and the subgroups for PINMUX and PINCONF and
  65. selected drivers, you need to select them from your machine's Kconfig entry,
  66. since these are so tightly integrated with the machines they are used on.
  67. See ``arch/arm/mach-ux500/Kconfig`` for an example.
  68. Pins usually have fancier names than this. You can find these in the datasheet
  69. for your chip. Notice that the core pinctrl.h file provides a fancy macro
  70. called ``PINCTRL_PIN()`` to create the struct entries. As you can see the pins are
  71. enumerated from 0 in the upper left corner to 63 in the lower right corner.
  72. This enumeration was arbitrarily chosen, in practice you need to think
  73. through your numbering system so that it matches the layout of registers
  74. and such things in your driver, or the code may become complicated. You must
  75. also consider matching of offsets to the GPIO ranges that may be handled by
  76. the pin controller.
  77. For a padding with 467 pads, as opposed to actual pins, the enumeration will
  78. be like this, walking around the edge of the chip, which seems to be industry
  79. standard too (all these pads had names, too)::
  80. 0 ..... 104
  81. 466 105
  82. . .
  83. . .
  84. 358 224
  85. 357 .... 225
  86. Pin groups
  87. ==========
  88. Many controllers need to deal with groups of pins, so the pin controller
  89. subsystem has a mechanism for enumerating groups of pins and retrieving the
  90. actual enumerated pins that are part of a certain group.
  91. For example, say that we have a group of pins dealing with an SPI interface
  92. on { 0, 8, 16, 24 }, and a group of pins dealing with an I2C interface on pins
  93. on { 24, 25 }.
  94. These two groups are presented to the pin control subsystem by implementing
  95. some generic ``pinctrl_ops`` like this:
  96. .. code-block:: c
  97. #include <linux/pinctrl/pinctrl.h>
  98. static const unsigned int spi0_pins[] = { 0, 8, 16, 24 };
  99. static const unsigned int i2c0_pins[] = { 24, 25 };
  100. static const struct pingroup foo_groups[] = {
  101. PINCTRL_PINGROUP("spi0_grp", spi0_pins, ARRAY_SIZE(spi0_pins)),
  102. PINCTRL_PINGROUP("i2c0_grp", i2c0_pins, ARRAY_SIZE(i2c0_pins)),
  103. };
  104. static int foo_get_groups_count(struct pinctrl_dev *pctldev)
  105. {
  106. return ARRAY_SIZE(foo_groups);
  107. }
  108. static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
  109. unsigned int selector)
  110. {
  111. return foo_groups[selector].name;
  112. }
  113. static int foo_get_group_pins(struct pinctrl_dev *pctldev,
  114. unsigned int selector,
  115. const unsigned int **pins,
  116. unsigned int *npins)
  117. {
  118. *pins = foo_groups[selector].pins;
  119. *npins = foo_groups[selector].npins;
  120. return 0;
  121. }
  122. static struct pinctrl_ops foo_pctrl_ops = {
  123. .get_groups_count = foo_get_groups_count,
  124. .get_group_name = foo_get_group_name,
  125. .get_group_pins = foo_get_group_pins,
  126. };
  127. static struct pinctrl_desc foo_desc = {
  128. ...
  129. .pctlops = &foo_pctrl_ops,
  130. };
  131. The pin control subsystem will call the ``.get_groups_count()`` function to
  132. determine the total number of legal selectors, then it will call the other functions
  133. to retrieve the name and pins of the group. Maintaining the data structure of
  134. the groups is up to the driver, this is just a simple example - in practice you
  135. may need more entries in your group structure, for example specific register
  136. ranges associated with each group and so on.
  137. Pin configuration
  138. =================
  139. Pins can sometimes be software-configured in various ways, mostly related
  140. to their electronic properties when used as inputs or outputs. For example you
  141. may be able to make an output pin high impedance (Hi-Z), or "tristate" meaning it is
  142. effectively disconnected. You may be able to connect an input pin to VDD or GND
  143. using a certain resistor value - pull up and pull down - so that the pin has a
  144. stable value when nothing is driving the rail it is connected to, or when it's
  145. unconnected.
  146. Pin configuration can be programmed by adding configuration entries into the
  147. mapping table; see section `Board/machine configuration`_ below.
  148. The format and meaning of the configuration parameter, PLATFORM_X_PULL_UP
  149. above, is entirely defined by the pin controller driver.
  150. The pin configuration driver implements callbacks for changing pin
  151. configuration in the pin controller ops like this:
  152. .. code-block:: c
  153. #include <linux/pinctrl/pinconf.h>
  154. #include <linux/pinctrl/pinctrl.h>
  155. #include "platform_x_pindefs.h"
  156. static int foo_pin_config_get(struct pinctrl_dev *pctldev,
  157. unsigned int offset,
  158. unsigned long *config)
  159. {
  160. struct my_conftype conf;
  161. /* ... Find setting for pin @ offset ... */
  162. *config = (unsigned long) conf;
  163. }
  164. static int foo_pin_config_set(struct pinctrl_dev *pctldev,
  165. unsigned int offset,
  166. unsigned long config)
  167. {
  168. struct my_conftype *conf = (struct my_conftype *) config;
  169. switch (conf) {
  170. case PLATFORM_X_PULL_UP:
  171. ...
  172. break;
  173. }
  174. }
  175. static int foo_pin_config_group_get(struct pinctrl_dev *pctldev,
  176. unsigned selector,
  177. unsigned long *config)
  178. {
  179. ...
  180. }
  181. static int foo_pin_config_group_set(struct pinctrl_dev *pctldev,
  182. unsigned selector,
  183. unsigned long config)
  184. {
  185. ...
  186. }
  187. static struct pinconf_ops foo_pconf_ops = {
  188. .pin_config_get = foo_pin_config_get,
  189. .pin_config_set = foo_pin_config_set,
  190. .pin_config_group_get = foo_pin_config_group_get,
  191. .pin_config_group_set = foo_pin_config_group_set,
  192. };
  193. /* Pin config operations are handled by some pin controller */
  194. static struct pinctrl_desc foo_desc = {
  195. ...
  196. .confops = &foo_pconf_ops,
  197. };
  198. Interaction with the GPIO subsystem
  199. ===================================
  200. The GPIO drivers may want to perform operations of various types on the same
  201. physical pins that are also registered as pin controller pins.
  202. First and foremost, the two subsystems can be used as completely orthogonal,
  203. see the section named `Pin control requests from drivers`_ and
  204. `Drivers needing both pin control and GPIOs`_ below for details. But in some
  205. situations a cross-subsystem mapping between pins and GPIOs is needed.
  206. Since the pin controller subsystem has its pinspace local to the pin controller
  207. we need a mapping so that the pin control subsystem can figure out which pin
  208. controller handles control of a certain GPIO pin. Since a single pin controller
  209. may be muxing several GPIO ranges (typically SoCs that have one set of pins,
  210. but internally several GPIO silicon blocks, each modelled as a struct
  211. gpio_chip) any number of GPIO ranges can be added to a pin controller instance
  212. like this:
  213. .. code-block:: c
  214. #include <linux/gpio/driver.h>
  215. #include <linux/pinctrl/pinctrl.h>
  216. struct gpio_chip chip_a;
  217. struct gpio_chip chip_b;
  218. static struct pinctrl_gpio_range gpio_range_a = {
  219. .name = "chip a",
  220. .id = 0,
  221. .base = 32,
  222. .pin_base = 32,
  223. .npins = 16,
  224. .gc = &chip_a,
  225. };
  226. static struct pinctrl_gpio_range gpio_range_b = {
  227. .name = "chip b",
  228. .id = 0,
  229. .base = 48,
  230. .pin_base = 64,
  231. .npins = 8,
  232. .gc = &chip_b;
  233. };
  234. int __init foo_init(void)
  235. {
  236. struct pinctrl_dev *pctl;
  237. ...
  238. pinctrl_add_gpio_range(pctl, &gpio_range_a);
  239. pinctrl_add_gpio_range(pctl, &gpio_range_b);
  240. ...
  241. }
  242. So this complex system has one pin controller handling two different
  243. GPIO chips. "chip a" has 16 pins and "chip b" has 8 pins. The "chip a" and
  244. "chip b" have different ``pin_base``, which means a start pin number of the
  245. GPIO range.
  246. The GPIO range of "chip a" starts from the GPIO base of 32 and actual
  247. pin range also starts from 32. However "chip b" has different starting
  248. offset for the GPIO range and pin range. The GPIO range of "chip b" starts
  249. from GPIO number 48, while the pin range of "chip b" starts from 64.
  250. We can convert a gpio number to actual pin number using this ``pin_base``.
  251. They are mapped in the global GPIO pin space at:
  252. chip a:
  253. - GPIO range : [32 .. 47]
  254. - pin range : [32 .. 47]
  255. chip b:
  256. - GPIO range : [48 .. 55]
  257. - pin range : [64 .. 71]
  258. The above examples assume the mapping between the GPIOs and pins is
  259. linear. If the mapping is sparse or haphazard, an array of arbitrary pin
  260. numbers can be encoded in the range like this:
  261. .. code-block:: c
  262. static const unsigned int range_pins[] = { 14, 1, 22, 17, 10, 8, 6, 2 };
  263. static struct pinctrl_gpio_range gpio_range = {
  264. .name = "chip",
  265. .id = 0,
  266. .base = 32,
  267. .pins = &range_pins,
  268. .npins = ARRAY_SIZE(range_pins),
  269. .gc = &chip,
  270. };
  271. In this case the ``pin_base`` property will be ignored. If the name of a pin
  272. group is known, the pins and npins elements of the above structure can be
  273. initialised using the function ``pinctrl_get_group_pins()``, e.g. for pin
  274. group "foo":
  275. .. code-block:: c
  276. pinctrl_get_group_pins(pctl, "foo", &gpio_range.pins, &gpio_range.npins);
  277. When GPIO-specific functions in the pin control subsystem are called, these
  278. ranges will be used to look up the appropriate pin controller by inspecting
  279. and matching the pin to the pin ranges across all controllers. When a
  280. pin controller handling the matching range is found, GPIO-specific functions
  281. will be called on that specific pin controller.
  282. For all functionalities dealing with pin biasing, pin muxing etc, the pin
  283. controller subsystem will look up the corresponding pin number from the passed
  284. in gpio number, and use the range's internals to retrieve a pin number. After
  285. that, the subsystem passes it on to the pin control driver, so the driver
  286. will get a pin number into its handled number range. Further it is also passed
  287. the range ID value, so that the pin controller knows which range it should
  288. deal with.
  289. Calling ``pinctrl_add_gpio_range()`` from pinctrl driver is DEPRECATED. Please see
  290. section 2.1 of ``Documentation/devicetree/bindings/gpio/gpio.txt`` on how to bind
  291. pinctrl and gpio drivers.
  292. PINMUX interfaces
  293. =================
  294. These calls use the pinmux_* naming prefix. No other calls should use that
  295. prefix.
  296. What is pinmuxing?
  297. ==================
  298. PINMUX, also known as padmux, ballmux, alternate functions or mission modes
  299. is a way for chip vendors producing some kind of electrical packages to use
  300. a certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive
  301. functions, depending on the application. By "application" in this context
  302. we usually mean a way of soldering or wiring the package into an electronic
  303. system, even though the framework makes it possible to also change the function
  304. at runtime.
  305. Here is an example of a PGA (Pin Grid Array) chip seen from underneath::
  306. A B C D E F G H
  307. +---+
  308. 8 | o | o o o o o o o
  309. | |
  310. 7 | o | o o o o o o o
  311. | |
  312. 6 | o | o o o o o o o
  313. +---+---+
  314. 5 | o | o | o o o o o o
  315. +---+---+ +---+
  316. 4 o o o o o o | o | o
  317. | |
  318. 3 o o o o o o | o | o
  319. | |
  320. 2 o o o o o o | o | o
  321. +-------+-------+-------+---+---+
  322. 1 | o o | o o | o o | o | o |
  323. +-------+-------+-------+---+---+
  324. This is not tetris. The game to think of is chess. Not all PGA/BGA packages
  325. are chessboard-like, big ones have "holes" in some arrangement according to
  326. different design patterns, but we're using this as a simple example. Of the
  327. pins you see some will be taken by things like a few VCC and GND to feed power
  328. to the chip, and quite a few will be taken by large ports like an external
  329. memory interface. The remaining pins will often be subject to pin multiplexing.
  330. The example 8x8 PGA package above will have pin numbers 0 through 63 assigned
  331. to its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
  332. pinctrl_register_pins() and a suitable data set as shown earlier.
  333. In this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
  334. (these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
  335. some general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
  336. be used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
  337. we cannot use the SPI port and I2C port at the same time. However in the inside
  338. of the package the silicon performing the SPI logic can alternatively be routed
  339. out on pins { G4, G3, G2, G1 }.
  340. On the bottom row at { A1, B1, C1, D1, E1, F1, G1, H1 } we have something
  341. special - it's an external MMC bus that can be 2, 4 or 8 bits wide, and it will
  342. consume 2, 4 or 8 pins respectively, so either { A1, B1 } are taken or
  343. { A1, B1, C1, D1 } or all of them. If we use all 8 bits, we cannot use the SPI
  344. port on pins { G4, G3, G2, G1 } of course.
  345. This way the silicon blocks present inside the chip can be multiplexed "muxed"
  346. out on different pin ranges. Often contemporary SoC (systems on chip) will
  347. contain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
  348. different pins by pinmux settings.
  349. Since general-purpose I/O pins (GPIO) are typically always in shortage, it is
  350. common to be able to use almost any pin as a GPIO pin if it is not currently
  351. in use by some other I/O port.
  352. Pinmux conventions
  353. ==================
  354. The purpose of the pinmux functionality in the pin controller subsystem is to
  355. abstract and provide pinmux settings to the devices you choose to instantiate
  356. in your machine configuration. It is inspired by the clk, GPIO and regulator
  357. subsystems, so devices will request their mux setting, but it's also possible
  358. to request a single pin for e.g. GPIO.
  359. The conventions are:
  360. - FUNCTIONS can be switched in and out by a driver residing with the pin
  361. control subsystem in the ``drivers/pinctrl`` directory of the kernel. The
  362. pin control driver knows the possible functions. In the example above you can
  363. identify three pinmux functions, one for spi, one for i2c and one for mmc.
  364. - FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
  365. In this case the array could be something like: { spi0, i2c0, mmc0 }
  366. for the three available functions.
  367. - FUNCTIONS have PIN GROUPS as defined on the generic level - so a certain
  368. function is *always* associated with a certain set of pin groups, could
  369. be just a single one, but could also be many. In the example above the
  370. function i2c is associated with the pins { A5, B5 }, enumerated as
  371. { 24, 25 } in the controller pin space.
  372. The Function spi is associated with pin groups { A8, A7, A6, A5 }
  373. and { G4, G3, G2, G1 }, which are enumerated as { 0, 8, 16, 24 } and
  374. { 38, 46, 54, 62 } respectively.
  375. Group names must be unique per pin controller, no two groups on the same
  376. controller may have the same name.
  377. - The combination of a FUNCTION and a PIN GROUP determine a certain function
  378. for a certain set of pins. The knowledge of the functions and pin groups
  379. and their machine-specific particulars are kept inside the pinmux driver,
  380. from the outside only the enumerators are known, and the driver core can
  381. request:
  382. - The name of a function with a certain selector (>= 0)
  383. - A list of groups associated with a certain function
  384. - That a certain group in that list to be activated for a certain function
  385. As already described above, pin groups are in turn self-descriptive, so
  386. the core will retrieve the actual pin range in a certain group from the
  387. driver.
  388. - FUNCTIONS and GROUPS on a certain PIN CONTROLLER are MAPPED to a certain
  389. device by the board file, device tree or similar machine setup configuration
  390. mechanism, similar to how regulators are connected to devices, usually by
  391. name. Defining a pin controller, function and group thus uniquely identify
  392. the set of pins to be used by a certain device. (If only one possible group
  393. of pins is available for the function, no group name need to be supplied -
  394. the core will simply select the first and only group available.)
  395. In the example case we can define that this particular machine shall
  396. use device spi0 with pinmux function fspi0 group gspi0 and i2c0 on function
  397. fi2c0 group gi2c0, on the primary pin controller, we get mappings
  398. like these:
  399. .. code-block:: c
  400. {
  401. {"map-spi0", spi0, pinctrl0, fspi0, gspi0},
  402. {"map-i2c0", i2c0, pinctrl0, fi2c0, gi2c0},
  403. }
  404. Every map must be assigned a state name, pin controller, device and
  405. function. The group is not compulsory - if it is omitted the first group
  406. presented by the driver as applicable for the function will be selected,
  407. which is useful for simple cases.
  408. It is possible to map several groups to the same combination of device,
  409. pin controller and function. This is for cases where a certain function on
  410. a certain pin controller may use different sets of pins in different
  411. configurations.
  412. - PINS for a certain FUNCTION using a certain PIN GROUP on a certain
  413. PIN CONTROLLER are provided on a first-come first-serve basis, so if some
  414. other device mux setting or GPIO pin request has already taken your physical
  415. pin, you will be denied the use of it. To get (activate) a new setting, the
  416. old one has to be put (deactivated) first.
  417. Sometimes the documentation and hardware registers will be oriented around
  418. pads (or "fingers") rather than pins - these are the soldering surfaces on the
  419. silicon inside the package, and may or may not match the actual number of
  420. pins/balls underneath the capsule. Pick some enumeration that makes sense to
  421. you. Define enumerators only for the pins you can control if that makes sense.
  422. Assumptions:
  423. We assume that the number of possible function maps to pin groups is limited by
  424. the hardware. I.e. we assume that there is no system where any function can be
  425. mapped to any pin, like in a phone exchange. So the available pin groups for
  426. a certain function will be limited to a few choices (say up to eight or so),
  427. not hundreds or any amount of choices. This is the characteristic we have found
  428. by inspecting available pinmux hardware, and a necessary assumption since we
  429. expect pinmux drivers to present *all* possible function vs pin group mappings
  430. to the subsystem.
  431. Pinmux drivers
  432. ==============
  433. The pinmux core takes care of preventing conflicts on pins and calling
  434. the pin controller driver to execute different settings.
  435. It is the responsibility of the pinmux driver to impose further restrictions
  436. (say for example infer electronic limitations due to load, etc.) to determine
  437. whether or not the requested function can actually be allowed, and in case it
  438. is possible to perform the requested mux setting, poke the hardware so that
  439. this happens.
  440. Pinmux drivers are required to supply a few callback functions, some are
  441. optional. Usually the ``.set_mux()`` function is implemented, writing values into
  442. some certain registers to activate a certain mux setting for a certain pin.
  443. A simple driver for the above example will work by setting bits 0, 1, 2, 3, 4, or 5
  444. into some register named MUX to select a certain function with a certain
  445. group of pins would work something like this:
  446. .. code-block:: c
  447. #include <linux/pinctrl/pinctrl.h>
  448. #include <linux/pinctrl/pinmux.h>
  449. static const unsigned int spi0_0_pins[] = { 0, 8, 16, 24 };
  450. static const unsigned int spi0_1_pins[] = { 38, 46, 54, 62 };
  451. static const unsigned int i2c0_pins[] = { 24, 25 };
  452. static const unsigned int mmc0_1_pins[] = { 56, 57 };
  453. static const unsigned int mmc0_2_pins[] = { 58, 59 };
  454. static const unsigned int mmc0_3_pins[] = { 60, 61, 62, 63 };
  455. static const struct pingroup foo_groups[] = {
  456. PINCTRL_PINGROUP("spi0_0_grp", spi0_0_pins, ARRAY_SIZE(spi0_0_pins)),
  457. PINCTRL_PINGROUP("spi0_1_grp", spi0_1_pins, ARRAY_SIZE(spi0_1_pins)),
  458. PINCTRL_PINGROUP("i2c0_grp", i2c0_pins, ARRAY_SIZE(i2c0_pins)),
  459. PINCTRL_PINGROUP("mmc0_1_grp", mmc0_1_pins, ARRAY_SIZE(mmc0_1_pins)),
  460. PINCTRL_PINGROUP("mmc0_2_grp", mmc0_2_pins, ARRAY_SIZE(mmc0_2_pins)),
  461. PINCTRL_PINGROUP("mmc0_3_grp", mmc0_3_pins, ARRAY_SIZE(mmc0_3_pins)),
  462. };
  463. static int foo_get_groups_count(struct pinctrl_dev *pctldev)
  464. {
  465. return ARRAY_SIZE(foo_groups);
  466. }
  467. static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
  468. unsigned int selector)
  469. {
  470. return foo_groups[selector].name;
  471. }
  472. static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
  473. const unsigned int **pins,
  474. unsigned int *npins)
  475. {
  476. *pins = foo_groups[selector].pins;
  477. *npins = foo_groups[selector].npins;
  478. return 0;
  479. }
  480. static struct pinctrl_ops foo_pctrl_ops = {
  481. .get_groups_count = foo_get_groups_count,
  482. .get_group_name = foo_get_group_name,
  483. .get_group_pins = foo_get_group_pins,
  484. };
  485. static const char * const spi0_groups[] = { "spi0_0_grp", "spi0_1_grp" };
  486. static const char * const i2c0_groups[] = { "i2c0_grp" };
  487. static const char * const mmc0_groups[] = { "mmc0_1_grp", "mmc0_2_grp", "mmc0_3_grp" };
  488. static const struct pinfunction foo_functions[] = {
  489. PINCTRL_PINFUNCTION("spi0", spi0_groups, ARRAY_SIZE(spi0_groups)),
  490. PINCTRL_PINFUNCTION("i2c0", i2c0_groups, ARRAY_SIZE(i2c0_groups)),
  491. PINCTRL_PINFUNCTION("mmc0", mmc0_groups, ARRAY_SIZE(mmc0_groups)),
  492. };
  493. static int foo_get_functions_count(struct pinctrl_dev *pctldev)
  494. {
  495. return ARRAY_SIZE(foo_functions);
  496. }
  497. static const char *foo_get_fname(struct pinctrl_dev *pctldev, unsigned int selector)
  498. {
  499. return foo_functions[selector].name;
  500. }
  501. static int foo_get_groups(struct pinctrl_dev *pctldev, unsigned int selector,
  502. const char * const **groups,
  503. unsigned int * const ngroups)
  504. {
  505. *groups = foo_functions[selector].groups;
  506. *ngroups = foo_functions[selector].ngroups;
  507. return 0;
  508. }
  509. static int foo_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  510. unsigned int group)
  511. {
  512. u8 regbit = BIT(group);
  513. writeb((readb(MUX) | regbit), MUX);
  514. return 0;
  515. }
  516. static struct pinmux_ops foo_pmxops = {
  517. .get_functions_count = foo_get_functions_count,
  518. .get_function_name = foo_get_fname,
  519. .get_function_groups = foo_get_groups,
  520. .set_mux = foo_set_mux,
  521. .strict = true,
  522. };
  523. /* Pinmux operations are handled by some pin controller */
  524. static struct pinctrl_desc foo_desc = {
  525. ...
  526. .pctlops = &foo_pctrl_ops,
  527. .pmxops = &foo_pmxops,
  528. };
  529. In the example activating muxing 0 and 2 at the same time setting bits
  530. 0 and 2, uses pin 24 in common so they would collide. All the same for
  531. the muxes 1 and 5, which have pin 62 in common.
  532. The beauty of the pinmux subsystem is that since it keeps track of all
  533. pins and who is using them, it will already have denied an impossible
  534. request like that, so the driver does not need to worry about such
  535. things - when it gets a selector passed in, the pinmux subsystem makes
  536. sure no other device or GPIO assignment is already using the selected
  537. pins. Thus bits 0 and 2, or 1 and 5 in the control register will never
  538. be set at the same time.
  539. All the above functions are mandatory to implement for a pinmux driver.
  540. Pin control interaction with the GPIO subsystem
  541. ===============================================
  542. Note that the following implies that the use case is to use a certain pin
  543. from the Linux kernel using the API in ``<linux/gpio/consumer.h>`` with gpiod_get()
  544. and similar functions. There are cases where you may be using something
  545. that your datasheet calls "GPIO mode", but actually is just an electrical
  546. configuration for a certain device. See the section below named
  547. `GPIO mode pitfalls`_ for more details on this scenario.
  548. The public pinmux API contains two functions named ``pinctrl_gpio_request()``
  549. and ``pinctrl_gpio_free()``. These two functions shall *ONLY* be called from
  550. gpiolib-based drivers as part of their ``.request()`` and ``.free()`` semantics.
  551. Likewise the ``pinctrl_gpio_direction_input()`` / ``pinctrl_gpio_direction_output()``
  552. shall only be called from within respective ``.direction_input()`` /
  553. ``.direction_output()`` gpiolib implementation.
  554. NOTE that platforms and individual drivers shall *NOT* request GPIO pins to be
  555. controlled e.g. muxed in. Instead, implement a proper gpiolib driver and have
  556. that driver request proper muxing and other control for its pins.
  557. The function list could become long, especially if you can convert every
  558. individual pin into a GPIO pin independent of any other pins, and then try
  559. the approach to define every pin as a function.
  560. In this case, the function array would become 64 entries for each GPIO
  561. setting and then the device functions.
  562. For this reason there are two functions a pin control driver can implement
  563. to enable only GPIO on an individual pin: ``.gpio_request_enable()`` and
  564. ``.gpio_disable_free()``.
  565. This function will pass in the affected GPIO range identified by the pin
  566. controller core, so you know which GPIO pins are being affected by the request
  567. operation.
  568. If your driver needs to have an indication from the framework of whether the
  569. GPIO pin shall be used for input or output you can implement the
  570. ``.gpio_set_direction()`` function. As described this shall be called from the
  571. gpiolib driver and the affected GPIO range, pin offset and desired direction
  572. will be passed along to this function.
  573. Alternatively to using these special functions, it is fully allowed to use
  574. named functions for each GPIO pin, the ``pinctrl_gpio_request()`` will attempt to
  575. obtain the function "gpioN" where "N" is the global GPIO pin number if no
  576. special GPIO-handler is registered.
  577. GPIO mode pitfalls
  578. ==================
  579. Due to the naming conventions used by hardware engineers, where "GPIO"
  580. is taken to mean different things than what the kernel does, the developer
  581. may be confused by a datasheet talking about a pin being possible to set
  582. into "GPIO mode". It appears that what hardware engineers mean with
  583. "GPIO mode" is not necessarily the use case that is implied in the kernel
  584. interface ``<linux/gpio/consumer.h>``: a pin that you grab from kernel code and then
  585. either listen for input or drive high/low to assert/deassert some
  586. external line.
  587. Rather hardware engineers think that "GPIO mode" means that you can
  588. software-control a few electrical properties of the pin that you would
  589. not be able to control if the pin was in some other mode, such as muxed in
  590. for a device.
  591. The GPIO portions of a pin and its relation to a certain pin controller
  592. configuration and muxing logic can be constructed in several ways. Here
  593. are two examples.
  594. Example **(A)**::
  595. pin config
  596. logic regs
  597. | +- SPI
  598. Physical pins --- pad --- pinmux -+- I2C
  599. | +- mmc
  600. | +- GPIO
  601. pin
  602. multiplex
  603. logic regs
  604. Here some electrical properties of the pin can be configured no matter
  605. whether the pin is used for GPIO or not. If you multiplex a GPIO onto a
  606. pin, you can also drive it high/low from "GPIO" registers.
  607. Alternatively, the pin can be controlled by a certain peripheral, while
  608. still applying desired pin config properties. GPIO functionality is thus
  609. orthogonal to any other device using the pin.
  610. In this arrangement the registers for the GPIO portions of the pin controller,
  611. or the registers for the GPIO hardware module are likely to reside in a
  612. separate memory range only intended for GPIO driving, and the register
  613. range dealing with pin config and pin multiplexing get placed into a
  614. different memory range and a separate section of the data sheet.
  615. A flag "strict" in struct pinmux_ops is available to check and deny
  616. simultaneous access to the same pin from GPIO and pin multiplexing
  617. consumers on hardware of this type. The pinctrl driver should set this flag
  618. accordingly.
  619. Example **(B)**::
  620. pin config
  621. logic regs
  622. | +- SPI
  623. Physical pins --- pad --- pinmux -+- I2C
  624. | | +- mmc
  625. | |
  626. GPIO pin
  627. multiplex
  628. logic regs
  629. In this arrangement, the GPIO functionality can always be enabled, such that
  630. e.g. a GPIO input can be used to "spy" on the SPI/I2C/MMC signal while it is
  631. pulsed out. It is likely possible to disrupt the traffic on the pin by doing
  632. wrong things on the GPIO block, as it is never really disconnected. It is
  633. possible that the GPIO, pin config and pin multiplex registers are placed into
  634. the same memory range and the same section of the data sheet, although that
  635. need not be the case.
  636. In some pin controllers, although the physical pins are designed in the same
  637. way as (B), the GPIO function still can't be enabled at the same time as the
  638. peripheral functions. So again the "strict" flag should be set, denying
  639. simultaneous activation by GPIO and other muxed in devices.
  640. From a kernel point of view, however, these are different aspects of the
  641. hardware and shall be put into different subsystems:
  642. - Registers (or fields within registers) that control electrical
  643. properties of the pin such as biasing and drive strength should be
  644. exposed through the pinctrl subsystem, as "pin configuration" settings.
  645. - Registers (or fields within registers) that control muxing of signals
  646. from various other HW blocks (e.g. I2C, MMC, or GPIO) onto pins should
  647. be exposed through the pinctrl subsystem, as mux functions.
  648. - Registers (or fields within registers) that control GPIO functionality
  649. such as setting a GPIO's output value, reading a GPIO's input value, or
  650. setting GPIO pin direction should be exposed through the GPIO subsystem,
  651. and if they also support interrupt capabilities, through the irqchip
  652. abstraction.
  653. Depending on the exact HW register design, some functions exposed by the
  654. GPIO subsystem may call into the pinctrl subsystem in order to
  655. coordinate register settings across HW modules. In particular, this may
  656. be needed for HW with separate GPIO and pin controller HW modules, where
  657. e.g. GPIO direction is determined by a register in the pin controller HW
  658. module rather than the GPIO HW module.
  659. Electrical properties of the pin such as biasing and drive strength
  660. may be placed at some pin-specific register in all cases or as part
  661. of the GPIO register in case (B) especially. This doesn't mean that such
  662. properties necessarily pertain to what the Linux kernel calls "GPIO".
  663. Example: a pin is usually muxed in to be used as a UART TX line. But during
  664. system sleep, we need to put this pin into "GPIO mode" and ground it.
  665. If you make a 1-to-1 map to the GPIO subsystem for this pin, you may start
  666. to think that you need to come up with something really complex, that the
  667. pin shall be used for UART TX and GPIO at the same time, that you will grab
  668. a pin control handle and set it to a certain state to enable UART TX to be
  669. muxed in, then twist it over to GPIO mode and use gpiod_direction_output()
  670. to drive it low during sleep, then mux it over to UART TX again when you
  671. wake up and maybe even gpiod_get() / gpiod_put() as part of this cycle. This
  672. all gets very complicated.
  673. The solution is to not think that what the datasheet calls "GPIO mode"
  674. has to be handled by the ``<linux/gpio/consumer.h>`` interface. Instead view this as
  675. a certain pin config setting. Look in e.g. ``<linux/pinctrl/pinconf-generic.h>``
  676. and you find this in the documentation:
  677. PIN_CONFIG_OUTPUT:
  678. this will configure the pin in output, use argument
  679. 1 to indicate high level, argument 0 to indicate low level.
  680. So it is perfectly possible to push a pin into "GPIO mode" and drive the
  681. line low as part of the usual pin control map. So for example your UART
  682. driver may look like this:
  683. .. code-block:: c
  684. #include <linux/pinctrl/consumer.h>
  685. struct pinctrl *pinctrl;
  686. struct pinctrl_state *pins_default;
  687. struct pinctrl_state *pins_sleep;
  688. pins_default = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_DEFAULT);
  689. pins_sleep = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_SLEEP);
  690. /* Normal mode */
  691. retval = pinctrl_select_state(pinctrl, pins_default);
  692. /* Sleep mode */
  693. retval = pinctrl_select_state(pinctrl, pins_sleep);
  694. And your machine configuration may look like this:
  695. .. code-block:: c
  696. static unsigned long uart_default_mode[] = {
  697. PIN_CONF_PACKED(PIN_CONFIG_DRIVE_PUSH_PULL, 0),
  698. };
  699. static unsigned long uart_sleep_mode[] = {
  700. PIN_CONF_PACKED(PIN_CONFIG_OUTPUT, 0),
  701. };
  702. static struct pinctrl_map pinmap[] __initdata = {
  703. PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo",
  704. "u0_group", "u0"),
  705. PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo",
  706. "UART_TX_PIN", uart_default_mode),
  707. PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo",
  708. "u0_group", "gpio-mode"),
  709. PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo",
  710. "UART_TX_PIN", uart_sleep_mode),
  711. };
  712. foo_init(void)
  713. {
  714. pinctrl_register_mappings(pinmap, ARRAY_SIZE(pinmap));
  715. }
  716. Here the pins we want to control are in the "u0_group" and there is some
  717. function called "u0" that can be enabled on this group of pins, and then
  718. everything is UART business as usual. But there is also some function
  719. named "gpio-mode" that can be mapped onto the same pins to move them into
  720. GPIO mode.
  721. This will give the desired effect without any bogus interaction with the
  722. GPIO subsystem. It is just an electrical configuration used by that device
  723. when going to sleep, it might imply that the pin is set into something the
  724. datasheet calls "GPIO mode", but that is not the point: it is still used
  725. by that UART device to control the pins that pertain to that very UART
  726. driver, putting them into modes needed by the UART. GPIO in the Linux
  727. kernel sense are just some 1-bit line, and is a different use case.
  728. How the registers are poked to attain the push or pull, and output low
  729. configuration and the muxing of the "u0" or "gpio-mode" group onto these
  730. pins is a question for the driver.
  731. Some datasheets will be more helpful and refer to the "GPIO mode" as
  732. "low power mode" rather than anything to do with GPIO. This often means
  733. the same thing electrically speaking, but in this latter case the
  734. software engineers will usually quickly identify that this is some
  735. specific muxing or configuration rather than anything related to the GPIO
  736. API.
  737. Board/machine configuration
  738. ===========================
  739. Boards and machines define how a certain complete running system is put
  740. together, including how GPIOs and devices are muxed, how regulators are
  741. constrained and how the clock tree looks. Of course pinmux settings are also
  742. part of this.
  743. A pin controller configuration for a machine looks pretty much like a simple
  744. regulator configuration, so for the example array above we want to enable i2c
  745. and spi on the second function mapping:
  746. .. code-block:: c
  747. #include <linux/pinctrl/machine.h>
  748. static const struct pinctrl_map mapping[] __initconst = {
  749. {
  750. .dev_name = "foo-spi.0",
  751. .name = PINCTRL_STATE_DEFAULT,
  752. .type = PIN_MAP_TYPE_MUX_GROUP,
  753. .ctrl_dev_name = "pinctrl-foo",
  754. .data.mux.function = "spi0",
  755. },
  756. {
  757. .dev_name = "foo-i2c.0",
  758. .name = PINCTRL_STATE_DEFAULT,
  759. .type = PIN_MAP_TYPE_MUX_GROUP,
  760. .ctrl_dev_name = "pinctrl-foo",
  761. .data.mux.function = "i2c0",
  762. },
  763. {
  764. .dev_name = "foo-mmc.0",
  765. .name = PINCTRL_STATE_DEFAULT,
  766. .type = PIN_MAP_TYPE_MUX_GROUP,
  767. .ctrl_dev_name = "pinctrl-foo",
  768. .data.mux.function = "mmc0",
  769. },
  770. };
  771. The dev_name here matches to the unique device name that can be used to look
  772. up the device struct (just like with clockdev or regulators). The function name
  773. must match a function provided by the pinmux driver handling this pin range.
  774. As you can see we may have several pin controllers on the system and thus
  775. we need to specify which one of them contains the functions we wish to map.
  776. You register this pinmux mapping to the pinmux subsystem by simply:
  777. .. code-block:: c
  778. ret = pinctrl_register_mappings(mapping, ARRAY_SIZE(mapping));
  779. Since the above construct is pretty common there is a helper macro to make
  780. it even more compact which assumes you want to use pinctrl-foo and position
  781. 0 for mapping, for example:
  782. .. code-block:: c
  783. static struct pinctrl_map mapping[] __initdata = {
  784. PIN_MAP_MUX_GROUP("foo-i2c.0", PINCTRL_STATE_DEFAULT,
  785. "pinctrl-foo", NULL, "i2c0"),
  786. };
  787. The mapping table may also contain pin configuration entries. It's common for
  788. each pin/group to have a number of configuration entries that affect it, so
  789. the table entries for configuration reference an array of config parameters
  790. and values. An example using the convenience macros is shown below:
  791. .. code-block:: c
  792. static unsigned long i2c_grp_configs[] = {
  793. FOO_PIN_DRIVEN,
  794. FOO_PIN_PULLUP,
  795. };
  796. static unsigned long i2c_pin_configs[] = {
  797. FOO_OPEN_COLLECTOR,
  798. FOO_SLEW_RATE_SLOW,
  799. };
  800. static struct pinctrl_map mapping[] __initdata = {
  801. PIN_MAP_MUX_GROUP("foo-i2c.0", PINCTRL_STATE_DEFAULT,
  802. "pinctrl-foo", "i2c0", "i2c0"),
  803. PIN_MAP_CONFIGS_GROUP("foo-i2c.0", PINCTRL_STATE_DEFAULT,
  804. "pinctrl-foo", "i2c0", i2c_grp_configs),
  805. PIN_MAP_CONFIGS_PIN("foo-i2c.0", PINCTRL_STATE_DEFAULT,
  806. "pinctrl-foo", "i2c0scl", i2c_pin_configs),
  807. PIN_MAP_CONFIGS_PIN("foo-i2c.0", PINCTRL_STATE_DEFAULT,
  808. "pinctrl-foo", "i2c0sda", i2c_pin_configs),
  809. };
  810. Finally, some devices expect the mapping table to contain certain specific
  811. named states. When running on hardware that doesn't need any pin controller
  812. configuration, the mapping table must still contain those named states, in
  813. order to explicitly indicate that the states were provided and intended to
  814. be empty. Table entry macro ``PIN_MAP_DUMMY_STATE()`` serves the purpose of defining
  815. a named state without causing any pin controller to be programmed:
  816. .. code-block:: c
  817. static struct pinctrl_map mapping[] __initdata = {
  818. PIN_MAP_DUMMY_STATE("foo-i2c.0", PINCTRL_STATE_DEFAULT),
  819. };
  820. Complex mappings
  821. ================
  822. As it is possible to map a function to different groups of pins an optional
  823. .group can be specified like this:
  824. .. code-block:: c
  825. ...
  826. {
  827. .dev_name = "foo-spi.0",
  828. .name = "spi0-pos-A",
  829. .type = PIN_MAP_TYPE_MUX_GROUP,
  830. .ctrl_dev_name = "pinctrl-foo",
  831. .function = "spi0",
  832. .group = "spi0_0_grp",
  833. },
  834. {
  835. .dev_name = "foo-spi.0",
  836. .name = "spi0-pos-B",
  837. .type = PIN_MAP_TYPE_MUX_GROUP,
  838. .ctrl_dev_name = "pinctrl-foo",
  839. .function = "spi0",
  840. .group = "spi0_1_grp",
  841. },
  842. ...
  843. This example mapping is used to switch between two positions for spi0 at
  844. runtime, as described further below under the heading `Runtime pinmuxing`_.
  845. Further it is possible for one named state to affect the muxing of several
  846. groups of pins, say for example in the mmc0 example above, where you can
  847. additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
  848. three groups for a total of 2 + 2 + 4 = 8 pins (for an 8-bit MMC bus as is the
  849. case), we define a mapping like this:
  850. .. code-block:: c
  851. ...
  852. {
  853. .dev_name = "foo-mmc.0",
  854. .name = "2bit"
  855. .type = PIN_MAP_TYPE_MUX_GROUP,
  856. .ctrl_dev_name = "pinctrl-foo",
  857. .function = "mmc0",
  858. .group = "mmc0_1_grp",
  859. },
  860. {
  861. .dev_name = "foo-mmc.0",
  862. .name = "4bit"
  863. .type = PIN_MAP_TYPE_MUX_GROUP,
  864. .ctrl_dev_name = "pinctrl-foo",
  865. .function = "mmc0",
  866. .group = "mmc0_1_grp",
  867. },
  868. {
  869. .dev_name = "foo-mmc.0",
  870. .name = "4bit"
  871. .type = PIN_MAP_TYPE_MUX_GROUP,
  872. .ctrl_dev_name = "pinctrl-foo",
  873. .function = "mmc0",
  874. .group = "mmc0_2_grp",
  875. },
  876. {
  877. .dev_name = "foo-mmc.0",
  878. .name = "8bit"
  879. .type = PIN_MAP_TYPE_MUX_GROUP,
  880. .ctrl_dev_name = "pinctrl-foo",
  881. .function = "mmc0",
  882. .group = "mmc0_1_grp",
  883. },
  884. {
  885. .dev_name = "foo-mmc.0",
  886. .name = "8bit"
  887. .type = PIN_MAP_TYPE_MUX_GROUP,
  888. .ctrl_dev_name = "pinctrl-foo",
  889. .function = "mmc0",
  890. .group = "mmc0_2_grp",
  891. },
  892. {
  893. .dev_name = "foo-mmc.0",
  894. .name = "8bit"
  895. .type = PIN_MAP_TYPE_MUX_GROUP,
  896. .ctrl_dev_name = "pinctrl-foo",
  897. .function = "mmc0",
  898. .group = "mmc0_3_grp",
  899. },
  900. ...
  901. The result of grabbing this mapping from the device with something like
  902. this (see next paragraph):
  903. .. code-block:: c
  904. p = devm_pinctrl_get(dev);
  905. s = pinctrl_lookup_state(p, "8bit");
  906. ret = pinctrl_select_state(p, s);
  907. or more simply:
  908. .. code-block:: c
  909. p = devm_pinctrl_get_select(dev, "8bit");
  910. Will be that you activate all the three bottom records in the mapping at
  911. once. Since they share the same name, pin controller device, function and
  912. device, and since we allow multiple groups to match to a single device, they
  913. all get selected, and they all get enabled and disable simultaneously by the
  914. pinmux core.
  915. Pin control requests from drivers
  916. =================================
  917. When a device driver is about to probe the device core will automatically
  918. attempt to issue ``pinctrl_get_select_default()`` on these devices.
  919. This way driver writers do not need to add any of the boilerplate code
  920. of the type found below. However when doing fine-grained state selection
  921. and not using the "default" state, you may have to do some device driver
  922. handling of the pinctrl handles and states.
  923. So if you just want to put the pins for a certain device into the default
  924. state and be done with it, there is nothing you need to do besides
  925. providing the proper mapping table. The device core will take care of
  926. the rest.
  927. Generally it is discouraged to let individual drivers get and enable pin
  928. control. So if possible, handle the pin control in platform code or some other
  929. place where you have access to all the affected struct device * pointers. In
  930. some cases where a driver needs to e.g. switch between different mux mappings
  931. at runtime this is not possible.
  932. A typical case is if a driver needs to switch bias of pins from normal
  933. operation and going to sleep, moving from the ``PINCTRL_STATE_DEFAULT`` to
  934. ``PINCTRL_STATE_SLEEP`` at runtime, re-biasing or even re-muxing pins to save
  935. current in sleep mode.
  936. A driver may request a certain control state to be activated, usually just the
  937. default state like this:
  938. .. code-block:: c
  939. #include <linux/pinctrl/consumer.h>
  940. struct foo_state {
  941. struct pinctrl *p;
  942. struct pinctrl_state *s;
  943. ...
  944. };
  945. foo_probe()
  946. {
  947. /* Allocate a state holder named "foo" etc */
  948. struct foo_state *foo = ...;
  949. foo->p = devm_pinctrl_get(&device);
  950. if (IS_ERR(foo->p)) {
  951. /* FIXME: clean up "foo" here */
  952. return PTR_ERR(foo->p);
  953. }
  954. foo->s = pinctrl_lookup_state(foo->p, PINCTRL_STATE_DEFAULT);
  955. if (IS_ERR(foo->s)) {
  956. /* FIXME: clean up "foo" here */
  957. return PTR_ERR(foo->s);
  958. }
  959. ret = pinctrl_select_state(foo->p, foo->s);
  960. if (ret < 0) {
  961. /* FIXME: clean up "foo" here */
  962. return ret;
  963. }
  964. }
  965. This get/lookup/select/put sequence can just as well be handled by bus drivers
  966. if you don't want each and every driver to handle it and you know the
  967. arrangement on your bus.
  968. The semantics of the pinctrl APIs are:
  969. - ``pinctrl_get()`` is called in process context to obtain a handle to all pinctrl
  970. information for a given client device. It will allocate a struct from the
  971. kernel memory to hold the pinmux state. All mapping table parsing or similar
  972. slow operations take place within this API.
  973. - ``devm_pinctrl_get()`` is a variant of pinctrl_get() that causes ``pinctrl_put()``
  974. to be called automatically on the retrieved pointer when the associated
  975. device is removed. It is recommended to use this function over plain
  976. ``pinctrl_get()``.
  977. - ``pinctrl_lookup_state()`` is called in process context to obtain a handle to a
  978. specific state for a client device. This operation may be slow, too.
  979. - ``pinctrl_select_state()`` programs pin controller hardware according to the
  980. definition of the state as given by the mapping table. In theory, this is a
  981. fast-path operation, since it only involved blasting some register settings
  982. into hardware. However, note that some pin controllers may have their
  983. registers on a slow/IRQ-based bus, so client devices should not assume they
  984. can call ``pinctrl_select_state()`` from non-blocking contexts.
  985. - ``pinctrl_put()`` frees all information associated with a pinctrl handle.
  986. - ``devm_pinctrl_put()`` is a variant of ``pinctrl_put()`` that may be used to
  987. explicitly destroy a pinctrl object returned by ``devm_pinctrl_get()``.
  988. However, use of this function will be rare, due to the automatic cleanup
  989. that will occur even without calling it.
  990. ``pinctrl_get()`` must be paired with a plain ``pinctrl_put()``.
  991. ``pinctrl_get()`` may not be paired with ``devm_pinctrl_put()``.
  992. ``devm_pinctrl_get()`` can optionally be paired with ``devm_pinctrl_put()``.
  993. ``devm_pinctrl_get()`` may not be paired with plain ``pinctrl_put()``.
  994. Usually the pin control core handled the get/put pair and call out to the
  995. device drivers bookkeeping operations, like checking available functions and
  996. the associated pins, whereas ``pinctrl_select_state()`` pass on to the pin controller
  997. driver which takes care of activating and/or deactivating the mux setting by
  998. quickly poking some registers.
  999. The pins are allocated for your device when you issue the ``devm_pinctrl_get()``
  1000. call, after this you should be able to see this in the debugfs listing of all
  1001. pins.
  1002. NOTE: the pinctrl system will return ``-EPROBE_DEFER`` if it cannot find the
  1003. requested pinctrl handles, for example if the pinctrl driver has not yet
  1004. registered. Thus make sure that the error path in your driver gracefully
  1005. cleans up and is ready to retry the probing later in the startup process.
  1006. Drivers needing both pin control and GPIOs
  1007. ==========================================
  1008. Again, it is discouraged to let drivers lookup and select pin control states
  1009. themselves, but again sometimes this is unavoidable.
  1010. So say that your driver is fetching its resources like this:
  1011. .. code-block:: c
  1012. #include <linux/pinctrl/consumer.h>
  1013. #include <linux/gpio/consumer.h>
  1014. struct pinctrl *pinctrl;
  1015. struct gpio_desc *gpio;
  1016. pinctrl = devm_pinctrl_get_select_default(&dev);
  1017. gpio = devm_gpiod_get(&dev, "foo");
  1018. Here we first request a certain pin state and then request GPIO "foo" to be
  1019. used. If you're using the subsystems orthogonally like this, you should
  1020. nominally always get your pinctrl handle and select the desired pinctrl
  1021. state BEFORE requesting the GPIO. This is a semantic convention to avoid
  1022. situations that can be electrically unpleasant, you will certainly want to
  1023. mux in and bias pins in a certain way before the GPIO subsystems starts to
  1024. deal with them.
  1025. The above can be hidden: using the device core, the pinctrl core may be
  1026. setting up the config and muxing for the pins right before the device is
  1027. probing, nevertheless orthogonal to the GPIO subsystem.
  1028. But there are also situations where it makes sense for the GPIO subsystem
  1029. to communicate directly with the pinctrl subsystem, using the latter as a
  1030. back-end. This is when the GPIO driver may call out to the functions
  1031. described in the section `Pin control interaction with the GPIO subsystem`_
  1032. above. This only involves per-pin multiplexing, and will be completely
  1033. hidden behind the gpiod_*() function namespace. In this case, the driver
  1034. need not interact with the pin control subsystem at all.
  1035. If a pin control driver and a GPIO driver is dealing with the same pins
  1036. and the use cases involve multiplexing, you MUST implement the pin controller
  1037. as a back-end for the GPIO driver like this, unless your hardware design
  1038. is such that the GPIO controller can override the pin controller's
  1039. multiplexing state through hardware without the need to interact with the
  1040. pin control system.
  1041. System pin control hogging
  1042. ==========================
  1043. Pin control map entries can be hogged by the core when the pin controller
  1044. is registered. This means that the core will attempt to call ``pinctrl_get()``,
  1045. ``pinctrl_lookup_state()`` and ``pinctrl_select_state()`` on it immediately after
  1046. the pin control device has been registered.
  1047. This occurs for mapping table entries where the client device name is equal
  1048. to the pin controller device name, and the state name is ``PINCTRL_STATE_DEFAULT``:
  1049. .. code-block:: c
  1050. {
  1051. .dev_name = "pinctrl-foo",
  1052. .name = PINCTRL_STATE_DEFAULT,
  1053. .type = PIN_MAP_TYPE_MUX_GROUP,
  1054. .ctrl_dev_name = "pinctrl-foo",
  1055. .function = "power_func",
  1056. },
  1057. Since it may be common to request the core to hog a few always-applicable
  1058. mux settings on the primary pin controller, there is a convenience macro for
  1059. this:
  1060. .. code-block:: c
  1061. PIN_MAP_MUX_GROUP_HOG_DEFAULT("pinctrl-foo", NULL /* group */,
  1062. "power_func")
  1063. This gives the exact same result as the above construction.
  1064. Runtime pinmuxing
  1065. =================
  1066. It is possible to mux a certain function in and out at runtime, say to move
  1067. an SPI port from one set of pins to another set of pins. Say for example for
  1068. spi0 in the example above, we expose two different groups of pins for the same
  1069. function, but with different named in the mapping as described under
  1070. "Advanced mapping" above. So that for an SPI device, we have two states named
  1071. "pos-A" and "pos-B".
  1072. This snippet first initializes a state object for both groups (in foo_probe()),
  1073. then muxes the function in the pins defined by group A, and finally muxes it in
  1074. on the pins defined by group B:
  1075. .. code-block:: c
  1076. #include <linux/pinctrl/consumer.h>
  1077. struct pinctrl *p;
  1078. struct pinctrl_state *s1, *s2;
  1079. foo_probe()
  1080. {
  1081. /* Setup */
  1082. p = devm_pinctrl_get(&device);
  1083. if (IS_ERR(p))
  1084. ...
  1085. s1 = pinctrl_lookup_state(p, "pos-A");
  1086. if (IS_ERR(s1))
  1087. ...
  1088. s2 = pinctrl_lookup_state(p, "pos-B");
  1089. if (IS_ERR(s2))
  1090. ...
  1091. }
  1092. foo_switch()
  1093. {
  1094. /* Enable on position A */
  1095. ret = pinctrl_select_state(p, s1);
  1096. if (ret < 0)
  1097. ...
  1098. ...
  1099. /* Enable on position B */
  1100. ret = pinctrl_select_state(p, s2);
  1101. if (ret < 0)
  1102. ...
  1103. ...
  1104. }
  1105. The above has to be done from process context. The reservation of the pins
  1106. will be done when the state is activated, so in effect one specific pin
  1107. can be used by different functions at different times on a running system.
  1108. Debugfs files
  1109. =============
  1110. These files are created in ``/sys/kernel/debug/pinctrl``:
  1111. - ``pinctrl-devices``: prints each pin controller device along with columns to
  1112. indicate support for pinmux and pinconf
  1113. - ``pinctrl-handles``: prints each configured pin controller handle and the
  1114. corresponding pinmux maps
  1115. - ``pinctrl-maps``: prints all pinctrl maps
  1116. A sub-directory is created inside of ``/sys/kernel/debug/pinctrl`` for each pin
  1117. controller device containing these files:
  1118. - ``pins``: prints a line for each pin registered on the pin controller. The
  1119. pinctrl driver may add additional information such as register contents.
  1120. - ``gpio-ranges``: prints ranges that map gpio lines to pins on the controller
  1121. - ``pingroups``: prints all pin groups registered on the pin controller
  1122. - ``pinconf-pins``: prints pin config settings for each pin
  1123. - ``pinconf-groups``: prints pin config settings per pin group
  1124. - ``pinmux-functions``: prints each pin function along with the pin groups that
  1125. map to the pin function
  1126. - ``pinmux-pins``: iterates through all pins and prints mux owner, gpio owner
  1127. and if the pin is a hog
  1128. - ``pinmux-select``: write to this file to activate a pin function for a group:
  1129. .. code-block:: sh
  1130. echo "<group-name function-name>" > pinmux-select