pinctl.rst 51 KB

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