pinctrl.txt 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. PINCTRL (PIN CONTROL) subsystem
  2. This document outlines the pin control subsystem in Linux
  3. This subsystem deals with:
  4. - Enumerating and naming controllable pins
  5. - Multiplexing of pins, pads, fingers (etc) see below for details
  6. The intention is to also deal with:
  7. - Software-controlled biasing and driving mode specific pins, such as
  8. pull-up/down, open drain etc, load capacitance configuration when controlled
  9. by software, etc.
  10. Top-level interface
  11. ===================
  12. Definition of PIN CONTROLLER:
  13. - A pin controller is a piece of hardware, usually a set of registers, that
  14. can control PINs. It may be able to multiplex, bias, set load capacitance,
  15. set drive strength etc for individual pins or groups of pins.
  16. Definition of PIN:
  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. #include <linux/pinctrl/pinctrl.h>
  39. const struct pinctrl_pin_desc foo_pins[] = {
  40. PINCTRL_PIN(0, "A8"),
  41. PINCTRL_PIN(1, "B8"),
  42. PINCTRL_PIN(2, "C8"),
  43. ...
  44. PINCTRL_PIN(61, "F1"),
  45. PINCTRL_PIN(62, "G1"),
  46. PINCTRL_PIN(63, "H1"),
  47. };
  48. static struct pinctrl_desc foo_desc = {
  49. .name = "foo",
  50. .pins = foo_pins,
  51. .npins = ARRAY_SIZE(foo_pins),
  52. .maxpin = 63,
  53. .owner = THIS_MODULE,
  54. };
  55. int __init foo_probe(void)
  56. {
  57. struct pinctrl_dev *pctl;
  58. pctl = pinctrl_register(&foo_desc, <PARENT>, NULL);
  59. if (IS_ERR(pctl))
  60. pr_err("could not register foo pin driver\n");
  61. }
  62. Pins usually have fancier names than this. You can find these in the dataheet
  63. for your chip. Notice that the core pinctrl.h file provides a fancy macro
  64. called PINCTRL_PIN() to create the struct entries. As you can see I enumerated
  65. the pins from 0 in the upper left corner to 63 in the lower right corner.
  66. This enumeration was arbitrarily chosen, in practice you need to think
  67. through your numbering system so that it matches the layout of registers
  68. and such things in your driver, or the code may become complicated. You must
  69. also consider matching of offsets to the GPIO ranges that may be handled by
  70. the pin controller.
  71. For a padring with 467 pads, as opposed to actual pins, I used an enumeration
  72. like this, walking around the edge of the chip, which seems to be industry
  73. standard too (all these pads had names, too):
  74. 0 ..... 104
  75. 466 105
  76. . .
  77. . .
  78. 358 224
  79. 357 .... 225
  80. Pin groups
  81. ==========
  82. Many controllers need to deal with groups of pins, so the pin controller
  83. subsystem has a mechanism for enumerating groups of pins and retrieving the
  84. actual enumerated pins that are part of a certain group.
  85. For example, say that we have a group of pins dealing with an SPI interface
  86. on { 0, 8, 16, 24 }, and a group of pins dealing with an I2C interface on pins
  87. on { 24, 25 }.
  88. These two groups are presented to the pin control subsystem by implementing
  89. some generic pinctrl_ops like this:
  90. #include <linux/pinctrl/pinctrl.h>
  91. struct foo_group {
  92. const char *name;
  93. const unsigned int *pins;
  94. const unsigned num_pins;
  95. };
  96. static const unsigned int spi0_pins[] = { 0, 8, 16, 24 };
  97. static const unsigned int i2c0_pins[] = { 24, 25 };
  98. static const struct foo_group foo_groups[] = {
  99. {
  100. .name = "spi0_grp",
  101. .pins = spi0_pins,
  102. .num_pins = ARRAY_SIZE(spi0_pins),
  103. },
  104. {
  105. .name = "i2c0_grp",
  106. .pins = i2c0_pins,
  107. .num_pins = ARRAY_SIZE(i2c0_pins),
  108. },
  109. };
  110. static int foo_list_groups(struct pinctrl_dev *pctldev, unsigned selector)
  111. {
  112. if (selector >= ARRAY_SIZE(foo_groups))
  113. return -EINVAL;
  114. return 0;
  115. }
  116. static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
  117. unsigned selector)
  118. {
  119. return foo_groups[selector].name;
  120. }
  121. static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  122. unsigned ** const pins,
  123. unsigned * const num_pins)
  124. {
  125. *pins = (unsigned *) foo_groups[selector].pins;
  126. *num_pins = foo_groups[selector].num_pins;
  127. return 0;
  128. }
  129. static struct pinctrl_ops foo_pctrl_ops = {
  130. .list_groups = foo_list_groups,
  131. .get_group_name = foo_get_group_name,
  132. .get_group_pins = foo_get_group_pins,
  133. };
  134. static struct pinctrl_desc foo_desc = {
  135. ...
  136. .pctlops = &foo_pctrl_ops,
  137. };
  138. The pin control subsystem will call the .list_groups() function repeatedly
  139. beginning on 0 until it returns non-zero to determine legal selectors, then
  140. it will call the other functions to retrieve the name and pins of the group.
  141. Maintaining the data structure of the groups is up to the driver, this is
  142. just a simple example - in practice you may need more entries in your group
  143. structure, for example specific register ranges associated with each group
  144. and so on.
  145. Interaction with the GPIO subsystem
  146. ===================================
  147. The GPIO drivers may want to perform operations of various types on the same
  148. physical pins that are also registered as pin controller pins.
  149. Since the pin controller subsystem have its pinspace local to the pin
  150. controller we need a mapping so that the pin control subsystem can figure out
  151. which pin controller handles control of a certain GPIO pin. Since a single
  152. pin controller may be muxing several GPIO ranges (typically SoCs that have
  153. one set of pins but internally several GPIO silicon blocks, each modeled as
  154. a struct gpio_chip) any number of GPIO ranges can be added to a pin controller
  155. instance like this:
  156. struct gpio_chip chip_a;
  157. struct gpio_chip chip_b;
  158. static struct pinctrl_gpio_range gpio_range_a = {
  159. .name = "chip a",
  160. .id = 0,
  161. .base = 32,
  162. .pin_base = 32,
  163. .npins = 16,
  164. .gc = &chip_a;
  165. };
  166. static struct pinctrl_gpio_range gpio_range_b = {
  167. .name = "chip b",
  168. .id = 0,
  169. .base = 48,
  170. .pin_base = 64,
  171. .npins = 8,
  172. .gc = &chip_b;
  173. };
  174. {
  175. struct pinctrl_dev *pctl;
  176. ...
  177. pinctrl_add_gpio_range(pctl, &gpio_range_a);
  178. pinctrl_add_gpio_range(pctl, &gpio_range_b);
  179. }
  180. So this complex system has one pin controller handling two different
  181. GPIO chips. "chip a" has 16 pins and "chip b" has 8 pins. The "chip a" and
  182. "chip b" have different .pin_base, which means a start pin number of the
  183. GPIO range.
  184. The GPIO range of "chip a" starts from the GPIO base of 32 and actual
  185. pin range also starts from 32. However "chip b" has different starting
  186. offset for the GPIO range and pin range. The GPIO range of "chip b" starts
  187. from GPIO number 48, while the pin range of "chip b" starts from 64.
  188. We can convert a gpio number to actual pin number using this "pin_base".
  189. They are mapped in the global GPIO pin space at:
  190. chip a:
  191. - GPIO range : [32 .. 47]
  192. - pin range : [32 .. 47]
  193. chip b:
  194. - GPIO range : [48 .. 55]
  195. - pin range : [64 .. 71]
  196. When GPIO-specific functions in the pin control subsystem are called, these
  197. ranges will be used to look up the appropriate pin controller by inspecting
  198. and matching the pin to the pin ranges across all controllers. When a
  199. pin controller handling the matching range is found, GPIO-specific functions
  200. will be called on that specific pin controller.
  201. For all functionalities dealing with pin biasing, pin muxing etc, the pin
  202. controller subsystem will subtract the range's .base offset from the passed
  203. in gpio number, and add the ranges's .pin_base offset to retrive a pin number.
  204. After that, the subsystem passes it on to the pin control driver, so the driver
  205. will get an pin number into its handled number range. Further it is also passed
  206. the range ID value, so that the pin controller knows which range it should
  207. deal with.
  208. PINMUX interfaces
  209. =================
  210. These calls use the pinmux_* naming prefix. No other calls should use that
  211. prefix.
  212. What is pinmuxing?
  213. ==================
  214. PINMUX, also known as padmux, ballmux, alternate functions or mission modes
  215. is a way for chip vendors producing some kind of electrical packages to use
  216. a certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive
  217. functions, depending on the application. By "application" in this context
  218. we usually mean a way of soldering or wiring the package into an electronic
  219. system, even though the framework makes it possible to also change the function
  220. at runtime.
  221. Here is an example of a PGA (Pin Grid Array) chip seen from underneath:
  222. A B C D E F G H
  223. +---+
  224. 8 | o | o o o o o o o
  225. | |
  226. 7 | o | o o o o o o o
  227. | |
  228. 6 | o | o o o o o o o
  229. +---+---+
  230. 5 | o | o | o o o o o o
  231. +---+---+ +---+
  232. 4 o o o o o o | o | o
  233. | |
  234. 3 o o o o o o | o | o
  235. | |
  236. 2 o o o o o o | o | o
  237. +-------+-------+-------+---+---+
  238. 1 | o o | o o | o o | o | o |
  239. +-------+-------+-------+---+---+
  240. This is not tetris. The game to think of is chess. Not all PGA/BGA packages
  241. are chessboard-like, big ones have "holes" in some arrangement according to
  242. different design patterns, but we're using this as a simple example. Of the
  243. pins you see some will be taken by things like a few VCC and GND to feed power
  244. to the chip, and quite a few will be taken by large ports like an external
  245. memory interface. The remaining pins will often be subject to pin multiplexing.
  246. The example 8x8 PGA package above will have pin numbers 0 thru 63 assigned to
  247. its physical pins. It will name the pins { A1, A2, A3 ... H6, H7, H8 } using
  248. pinctrl_register_pins() and a suitable data set as shown earlier.
  249. In this 8x8 BGA package the pins { A8, A7, A6, A5 } can be used as an SPI port
  250. (these are four pins: CLK, RXD, TXD, FRM). In that case, pin B5 can be used as
  251. some general-purpose GPIO pin. However, in another setting, pins { A5, B5 } can
  252. be used as an I2C port (these are just two pins: SCL, SDA). Needless to say,
  253. we cannot use the SPI port and I2C port at the same time. However in the inside
  254. of the package the silicon performing the SPI logic can alternatively be routed
  255. out on pins { G4, G3, G2, G1 }.
  256. On the botton row at { A1, B1, C1, D1, E1, F1, G1, H1 } we have something
  257. special - it's an external MMC bus that can be 2, 4 or 8 bits wide, and it will
  258. consume 2, 4 or 8 pins respectively, so either { A1, B1 } are taken or
  259. { A1, B1, C1, D1 } or all of them. If we use all 8 bits, we cannot use the SPI
  260. port on pins { G4, G3, G2, G1 } of course.
  261. This way the silicon blocks present inside the chip can be multiplexed "muxed"
  262. out on different pin ranges. Often contemporary SoC (systems on chip) will
  263. contain several I2C, SPI, SDIO/MMC, etc silicon blocks that can be routed to
  264. different pins by pinmux settings.
  265. Since general-purpose I/O pins (GPIO) are typically always in shortage, it is
  266. common to be able to use almost any pin as a GPIO pin if it is not currently
  267. in use by some other I/O port.
  268. Pinmux conventions
  269. ==================
  270. The purpose of the pinmux functionality in the pin controller subsystem is to
  271. abstract and provide pinmux settings to the devices you choose to instantiate
  272. in your machine configuration. It is inspired by the clk, GPIO and regulator
  273. subsystems, so devices will request their mux setting, but it's also possible
  274. to request a single pin for e.g. GPIO.
  275. Definitions:
  276. - FUNCTIONS can be switched in and out by a driver residing with the pin
  277. control subsystem in the drivers/pinctrl/* directory of the kernel. The
  278. pin control driver knows the possible functions. In the example above you can
  279. identify three pinmux functions, one for spi, one for i2c and one for mmc.
  280. - FUNCTIONS are assumed to be enumerable from zero in a one-dimensional array.
  281. In this case the array could be something like: { spi0, i2c0, mmc0 }
  282. for the three available functions.
  283. - FUNCTIONS have PIN GROUPS as defined on the generic level - so a certain
  284. function is *always* associated with a certain set of pin groups, could
  285. be just a single one, but could also be many. In the example above the
  286. function i2c is associated with the pins { A5, B5 }, enumerated as
  287. { 24, 25 } in the controller pin space.
  288. The Function spi is associated with pin groups { A8, A7, A6, A5 }
  289. and { G4, G3, G2, G1 }, which are enumerated as { 0, 8, 16, 24 } and
  290. { 38, 46, 54, 62 } respectively.
  291. Group names must be unique per pin controller, no two groups on the same
  292. controller may have the same name.
  293. - The combination of a FUNCTION and a PIN GROUP determine a certain function
  294. for a certain set of pins. The knowledge of the functions and pin groups
  295. and their machine-specific particulars are kept inside the pinmux driver,
  296. from the outside only the enumerators are known, and the driver core can:
  297. - Request the name of a function with a certain selector (>= 0)
  298. - A list of groups associated with a certain function
  299. - Request that a certain group in that list to be activated for a certain
  300. function
  301. As already described above, pin groups are in turn self-descriptive, so
  302. the core will retrieve the actual pin range in a certain group from the
  303. driver.
  304. - FUNCTIONS and GROUPS on a certain PIN CONTROLLER are MAPPED to a certain
  305. device by the board file, device tree or similar machine setup configuration
  306. mechanism, similar to how regulators are connected to devices, usually by
  307. name. Defining a pin controller, function and group thus uniquely identify
  308. the set of pins to be used by a certain device. (If only one possible group
  309. of pins is available for the function, no group name need to be supplied -
  310. the core will simply select the first and only group available.)
  311. In the example case we can define that this particular machine shall
  312. use device spi0 with pinmux function fspi0 group gspi0 and i2c0 on function
  313. fi2c0 group gi2c0, on the primary pin controller, we get mappings
  314. like these:
  315. {
  316. {"map-spi0", spi0, pinctrl0, fspi0, gspi0},
  317. {"map-i2c0", i2c0, pinctrl0, fi2c0, gi2c0}
  318. }
  319. Every map must be assigned a symbolic name, pin controller and function.
  320. The group is not compulsory - if it is omitted the first group presented by
  321. the driver as applicable for the function will be selected, which is
  322. useful for simple cases.
  323. The device name is present in map entries tied to specific devices. Maps
  324. without device names are referred to as SYSTEM pinmuxes, such as can be taken
  325. by the machine implementation on boot and not tied to any specific device.
  326. It is possible to map several groups to the same combination of device,
  327. pin controller and function. This is for cases where a certain function on
  328. a certain pin controller may use different sets of pins in different
  329. configurations.
  330. - PINS for a certain FUNCTION using a certain PIN GROUP on a certain
  331. PIN CONTROLLER are provided on a first-come first-serve basis, so if some
  332. other device mux setting or GPIO pin request has already taken your physical
  333. pin, you will be denied the use of it. To get (activate) a new setting, the
  334. old one has to be put (deactivated) first.
  335. Sometimes the documentation and hardware registers will be oriented around
  336. pads (or "fingers") rather than pins - these are the soldering surfaces on the
  337. silicon inside the package, and may or may not match the actual number of
  338. pins/balls underneath the capsule. Pick some enumeration that makes sense to
  339. you. Define enumerators only for the pins you can control if that makes sense.
  340. Assumptions:
  341. We assume that the number of possible function maps to pin groups is limited by
  342. the hardware. I.e. we assume that there is no system where any function can be
  343. mapped to any pin, like in a phone exchange. So the available pins groups for
  344. a certain function will be limited to a few choices (say up to eight or so),
  345. not hundreds or any amount of choices. This is the characteristic we have found
  346. by inspecting available pinmux hardware, and a necessary assumption since we
  347. expect pinmux drivers to present *all* possible function vs pin group mappings
  348. to the subsystem.
  349. Pinmux drivers
  350. ==============
  351. The pinmux core takes care of preventing conflicts on pins and calling
  352. the pin controller driver to execute different settings.
  353. It is the responsibility of the pinmux driver to impose further restrictions
  354. (say for example infer electronic limitations due to load etc) to determine
  355. whether or not the requested function can actually be allowed, and in case it
  356. is possible to perform the requested mux setting, poke the hardware so that
  357. this happens.
  358. Pinmux drivers are required to supply a few callback functions, some are
  359. optional. Usually the enable() and disable() functions are implemented,
  360. writing values into some certain registers to activate a certain mux setting
  361. for a certain pin.
  362. A simple driver for the above example will work by setting bits 0, 1, 2, 3 or 4
  363. into some register named MUX to select a certain function with a certain
  364. group of pins would work something like this:
  365. #include <linux/pinctrl/pinctrl.h>
  366. #include <linux/pinctrl/pinmux.h>
  367. struct foo_group {
  368. const char *name;
  369. const unsigned int *pins;
  370. const unsigned num_pins;
  371. };
  372. static const unsigned spi0_0_pins[] = { 0, 8, 16, 24 };
  373. static const unsigned spi0_1_pins[] = { 38, 46, 54, 62 };
  374. static const unsigned i2c0_pins[] = { 24, 25 };
  375. static const unsigned mmc0_1_pins[] = { 56, 57 };
  376. static const unsigned mmc0_2_pins[] = { 58, 59 };
  377. static const unsigned mmc0_3_pins[] = { 60, 61, 62, 63 };
  378. static const struct foo_group foo_groups[] = {
  379. {
  380. .name = "spi0_0_grp",
  381. .pins = spi0_0_pins,
  382. .num_pins = ARRAY_SIZE(spi0_0_pins),
  383. },
  384. {
  385. .name = "spi0_1_grp",
  386. .pins = spi0_1_pins,
  387. .num_pins = ARRAY_SIZE(spi0_1_pins),
  388. },
  389. {
  390. .name = "i2c0_grp",
  391. .pins = i2c0_pins,
  392. .num_pins = ARRAY_SIZE(i2c0_pins),
  393. },
  394. {
  395. .name = "mmc0_1_grp",
  396. .pins = mmc0_1_pins,
  397. .num_pins = ARRAY_SIZE(mmc0_1_pins),
  398. },
  399. {
  400. .name = "mmc0_2_grp",
  401. .pins = mmc0_2_pins,
  402. .num_pins = ARRAY_SIZE(mmc0_2_pins),
  403. },
  404. {
  405. .name = "mmc0_3_grp",
  406. .pins = mmc0_3_pins,
  407. .num_pins = ARRAY_SIZE(mmc0_3_pins),
  408. },
  409. };
  410. static int foo_list_groups(struct pinctrl_dev *pctldev, unsigned selector)
  411. {
  412. if (selector >= ARRAY_SIZE(foo_groups))
  413. return -EINVAL;
  414. return 0;
  415. }
  416. static const char *foo_get_group_name(struct pinctrl_dev *pctldev,
  417. unsigned selector)
  418. {
  419. return foo_groups[selector].name;
  420. }
  421. static int foo_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  422. unsigned ** const pins,
  423. unsigned * const num_pins)
  424. {
  425. *pins = (unsigned *) foo_groups[selector].pins;
  426. *num_pins = foo_groups[selector].num_pins;
  427. return 0;
  428. }
  429. static struct pinctrl_ops foo_pctrl_ops = {
  430. .list_groups = foo_list_groups,
  431. .get_group_name = foo_get_group_name,
  432. .get_group_pins = foo_get_group_pins,
  433. };
  434. struct foo_pmx_func {
  435. const char *name;
  436. const char * const *groups;
  437. const unsigned num_groups;
  438. };
  439. static const char * const spi0_groups[] = { "spi0_1_grp" };
  440. static const char * const i2c0_groups[] = { "i2c0_grp" };
  441. static const char * const mmc0_groups[] = { "mmc0_1_grp", "mmc0_2_grp",
  442. "mmc0_3_grp" };
  443. static const struct foo_pmx_func foo_functions[] = {
  444. {
  445. .name = "spi0",
  446. .groups = spi0_groups,
  447. .num_groups = ARRAY_SIZE(spi0_groups),
  448. },
  449. {
  450. .name = "i2c0",
  451. .groups = i2c0_groups,
  452. .num_groups = ARRAY_SIZE(i2c0_groups),
  453. },
  454. {
  455. .name = "mmc0",
  456. .groups = mmc0_groups,
  457. .num_groups = ARRAY_SIZE(mmc0_groups),
  458. },
  459. };
  460. int foo_list_funcs(struct pinctrl_dev *pctldev, unsigned selector)
  461. {
  462. if (selector >= ARRAY_SIZE(foo_functions))
  463. return -EINVAL;
  464. return 0;
  465. }
  466. const char *foo_get_fname(struct pinctrl_dev *pctldev, unsigned selector)
  467. {
  468. return foo_functions[selector].name;
  469. }
  470. static int foo_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  471. const char * const **groups,
  472. unsigned * const num_groups)
  473. {
  474. *groups = foo_functions[selector].groups;
  475. *num_groups = foo_functions[selector].num_groups;
  476. return 0;
  477. }
  478. int foo_enable(struct pinctrl_dev *pctldev, unsigned selector,
  479. unsigned group)
  480. {
  481. u8 regbit = (1 << selector + group);
  482. writeb((readb(MUX)|regbit), MUX)
  483. return 0;
  484. }
  485. void foo_disable(struct pinctrl_dev *pctldev, unsigned selector,
  486. unsigned group)
  487. {
  488. u8 regbit = (1 << selector + group);
  489. writeb((readb(MUX) & ~(regbit)), MUX)
  490. return 0;
  491. }
  492. struct pinmux_ops foo_pmxops = {
  493. .list_functions = foo_list_funcs,
  494. .get_function_name = foo_get_fname,
  495. .get_function_groups = foo_get_groups,
  496. .enable = foo_enable,
  497. .disable = foo_disable,
  498. };
  499. /* Pinmux operations are handled by some pin controller */
  500. static struct pinctrl_desc foo_desc = {
  501. ...
  502. .pctlops = &foo_pctrl_ops,
  503. .pmxops = &foo_pmxops,
  504. };
  505. In the example activating muxing 0 and 1 at the same time setting bits
  506. 0 and 1, uses one pin in common so they would collide.
  507. The beauty of the pinmux subsystem is that since it keeps track of all
  508. pins and who is using them, it will already have denied an impossible
  509. request like that, so the driver does not need to worry about such
  510. things - when it gets a selector passed in, the pinmux subsystem makes
  511. sure no other device or GPIO assignment is already using the selected
  512. pins. Thus bits 0 and 1 in the control register will never be set at the
  513. same time.
  514. All the above functions are mandatory to implement for a pinmux driver.
  515. Pinmux interaction with the GPIO subsystem
  516. ==========================================
  517. The public pinmux API contains two functions named pinmux_request_gpio()
  518. and pinmux_free_gpio(). These two functions shall *ONLY* be called from
  519. gpiolib-based drivers as part of their gpio_request() and
  520. gpio_free() semantics. Likewise the pinmux_gpio_direction_[input|output]
  521. shall only be called from within respective gpio_direction_[input|output]
  522. gpiolib implementation.
  523. NOTE that platforms and individual drivers shall *NOT* request GPIO pins to be
  524. muxed in. Instead, implement a proper gpiolib driver and have that driver
  525. request proper muxing for its pins.
  526. The function list could become long, especially if you can convert every
  527. individual pin into a GPIO pin independent of any other pins, and then try
  528. the approach to define every pin as a function.
  529. In this case, the function array would become 64 entries for each GPIO
  530. setting and then the device functions.
  531. For this reason there are two functions a pinmux driver can implement
  532. to enable only GPIO on an individual pin: .gpio_request_enable() and
  533. .gpio_disable_free().
  534. This function will pass in the affected GPIO range identified by the pin
  535. controller core, so you know which GPIO pins are being affected by the request
  536. operation.
  537. If your driver needs to have an indication from the framework of whether the
  538. GPIO pin shall be used for input or output you can implement the
  539. .gpio_set_direction() function. As described this shall be called from the
  540. gpiolib driver and the affected GPIO range, pin offset and desired direction
  541. will be passed along to this function.
  542. Alternatively to using these special functions, it is fully allowed to use
  543. named functions for each GPIO pin, the pinmux_request_gpio() will attempt to
  544. obtain the function "gpioN" where "N" is the global GPIO pin number if no
  545. special GPIO-handler is registered.
  546. Pinmux board/machine configuration
  547. ==================================
  548. Boards and machines define how a certain complete running system is put
  549. together, including how GPIOs and devices are muxed, how regulators are
  550. constrained and how the clock tree looks. Of course pinmux settings are also
  551. part of this.
  552. A pinmux config for a machine looks pretty much like a simple regulator
  553. configuration, so for the example array above we want to enable i2c and
  554. spi on the second function mapping:
  555. #include <linux/pinctrl/machine.h>
  556. static const struct pinmux_map __initdata pmx_mapping[] = {
  557. {
  558. .ctrl_dev_name = "pinctrl.0",
  559. .function = "spi0",
  560. .dev_name = "foo-spi.0",
  561. },
  562. {
  563. .ctrl_dev_name = "pinctrl.0",
  564. .function = "i2c0",
  565. .dev_name = "foo-i2c.0",
  566. },
  567. {
  568. .ctrl_dev_name = "pinctrl.0",
  569. .function = "mmc0",
  570. .dev_name = "foo-mmc.0",
  571. },
  572. };
  573. The dev_name here matches to the unique device name that can be used to look
  574. up the device struct (just like with clockdev or regulators). The function name
  575. must match a function provided by the pinmux driver handling this pin range.
  576. As you can see we may have several pin controllers on the system and thus
  577. we need to specify which one of them that contain the functions we wish
  578. to map. The map can also use struct device * directly, so there is no
  579. inherent need to use strings to specify .dev_name or .ctrl_dev_name, these
  580. are for the situation where you do not have a handle to the struct device *,
  581. for example if they are not yet instantiated or cumbersome to obtain.
  582. You register this pinmux mapping to the pinmux subsystem by simply:
  583. ret = pinmux_register_mappings(pmx_mapping, ARRAY_SIZE(pmx_mapping));
  584. Since the above construct is pretty common there is a helper macro to make
  585. it even more compact which assumes you want to use pinctrl.0 and position
  586. 0 for mapping, for example:
  587. static struct pinmux_map __initdata pmx_mapping[] = {
  588. PINMUX_MAP_PRIMARY("I2CMAP", "i2c0", "foo-i2c.0"),
  589. };
  590. Complex mappings
  591. ================
  592. As it is possible to map a function to different groups of pins an optional
  593. .group can be specified like this:
  594. ...
  595. {
  596. .name = "spi0-pos-A",
  597. .ctrl_dev_name = "pinctrl.0",
  598. .function = "spi0",
  599. .group = "spi0_0_grp",
  600. .dev_name = "foo-spi.0",
  601. },
  602. {
  603. .name = "spi0-pos-B",
  604. .ctrl_dev_name = "pinctrl.0",
  605. .function = "spi0",
  606. .group = "spi0_1_grp",
  607. .dev_name = "foo-spi.0",
  608. },
  609. ...
  610. This example mapping is used to switch between two positions for spi0 at
  611. runtime, as described further below under the heading "Runtime pinmuxing".
  612. Further it is possible to match several groups of pins to the same function
  613. for a single device, say for example in the mmc0 example above, where you can
  614. additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
  615. three groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the
  616. case), we define a mapping like this:
  617. ...
  618. {
  619. .name "2bit"
  620. .ctrl_dev_name = "pinctrl.0",
  621. .function = "mmc0",
  622. .group = "mmc0_1_grp",
  623. .dev_name = "foo-mmc.0",
  624. },
  625. {
  626. .name "4bit"
  627. .ctrl_dev_name = "pinctrl.0",
  628. .function = "mmc0",
  629. .group = "mmc0_1_grp",
  630. .dev_name = "foo-mmc.0",
  631. },
  632. {
  633. .name "4bit"
  634. .ctrl_dev_name = "pinctrl.0",
  635. .function = "mmc0",
  636. .group = "mmc0_2_grp",
  637. .dev_name = "foo-mmc.0",
  638. },
  639. {
  640. .name "8bit"
  641. .ctrl_dev_name = "pinctrl.0",
  642. .function = "mmc0",
  643. .group = "mmc0_1_grp",
  644. .dev_name = "foo-mmc.0",
  645. },
  646. {
  647. .name "8bit"
  648. .ctrl_dev_name = "pinctrl.0",
  649. .function = "mmc0",
  650. .group = "mmc0_2_grp",
  651. .dev_name = "foo-mmc.0",
  652. },
  653. {
  654. .name "8bit"
  655. .ctrl_dev_name = "pinctrl.0",
  656. .function = "mmc0",
  657. .group = "mmc0_3_grp",
  658. .dev_name = "foo-mmc.0",
  659. },
  660. ...
  661. The result of grabbing this mapping from the device with something like
  662. this (see next paragraph):
  663. pmx = pinmux_get(&device, "8bit");
  664. Will be that you activate all the three bottom records in the mapping at
  665. once. Since they share the same name, pin controller device, funcion and
  666. device, and since we allow multiple groups to match to a single device, they
  667. all get selected, and they all get enabled and disable simultaneously by the
  668. pinmux core.
  669. Pinmux requests from drivers
  670. ============================
  671. Generally it is discouraged to let individual drivers get and enable pinmuxes.
  672. So if possible, handle the pinmuxes in platform code or some other place where
  673. you have access to all the affected struct device * pointers. In some cases
  674. where a driver needs to switch between different mux mappings at runtime
  675. this is not possible.
  676. A driver may request a certain mux to be activated, usually just the default
  677. mux like this:
  678. #include <linux/pinctrl/pinmux.h>
  679. struct foo_state {
  680. struct pinmux *pmx;
  681. ...
  682. };
  683. foo_probe()
  684. {
  685. /* Allocate a state holder named "state" etc */
  686. struct pinmux pmx;
  687. pmx = pinmux_get(&device, NULL);
  688. if IS_ERR(pmx)
  689. return PTR_ERR(pmx);
  690. pinmux_enable(pmx);
  691. state->pmx = pmx;
  692. }
  693. foo_remove()
  694. {
  695. pinmux_disable(state->pmx);
  696. pinmux_put(state->pmx);
  697. }
  698. If you want to grab a specific mux mapping and not just the first one found for
  699. this device you can specify a specific mapping name, for example in the above
  700. example the second i2c0 setting: pinmux_get(&device, "spi0-pos-B");
  701. This get/enable/disable/put sequence can just as well be handled by bus drivers
  702. if you don't want each and every driver to handle it and you know the
  703. arrangement on your bus.
  704. The semantics of the get/enable respective disable/put is as follows:
  705. - pinmux_get() is called in process context to reserve the pins affected with
  706. a certain mapping and set up the pinmux core and the driver. It will allocate
  707. a struct from the kernel memory to hold the pinmux state.
  708. - pinmux_enable()/pinmux_disable() is quick and can be called from fastpath
  709. (irq context) when you quickly want to set up/tear down the hardware muxing
  710. when running a device driver. Usually it will just poke some values into a
  711. register.
  712. - pinmux_disable() is called in process context to tear down the pin requests
  713. and release the state holder struct for the mux setting.
  714. Usually the pinmux core handled the get/put pair and call out to the device
  715. drivers bookkeeping operations, like checking available functions and the
  716. associated pins, whereas the enable/disable pass on to the pin controller
  717. driver which takes care of activating and/or deactivating the mux setting by
  718. quickly poking some registers.
  719. The pins are allocated for your device when you issue the pinmux_get() call,
  720. after this you should be able to see this in the debugfs listing of all pins.
  721. System pinmux hogging
  722. =====================
  723. A system pinmux map entry, i.e. a pinmux setting that does not have a device
  724. associated with it, can be hogged by the core when the pin controller is
  725. registered. This means that the core will attempt to call pinmux_get() and
  726. pinmux_enable() on it immediately after the pin control device has been
  727. registered.
  728. This is enabled by simply setting the .hog_on_boot field in the map to true,
  729. like this:
  730. {
  731. .name "POWERMAP"
  732. .ctrl_dev_name = "pinctrl.0",
  733. .function = "power_func",
  734. .hog_on_boot = true,
  735. },
  736. Since it may be common to request the core to hog a few always-applicable
  737. mux settings on the primary pin controller, there is a convenience macro for
  738. this:
  739. PINMUX_MAP_PRIMARY_SYS_HOG("POWERMAP", "power_func")
  740. This gives the exact same result as the above construction.
  741. Runtime pinmuxing
  742. =================
  743. It is possible to mux a certain function in and out at runtime, say to move
  744. an SPI port from one set of pins to another set of pins. Say for example for
  745. spi0 in the example above, we expose two different groups of pins for the same
  746. function, but with different named in the mapping as described under
  747. "Advanced mapping" above. So we have two mappings named "spi0-pos-A" and
  748. "spi0-pos-B".
  749. This snippet first muxes the function in the pins defined by group A, enables
  750. it, disables and releases it, and muxes it in on the pins defined by group B:
  751. foo_switch()
  752. {
  753. struct pinmux pmx;
  754. /* Enable on position A */
  755. pmx = pinmux_get(&device, "spi0-pos-A");
  756. if IS_ERR(pmx)
  757. return PTR_ERR(pmx);
  758. pinmux_enable(pmx);
  759. /* This releases the pins again */
  760. pinmux_disable(pmx);
  761. pinmux_put(pmx);
  762. /* Enable on position B */
  763. pmx = pinmux_get(&device, "spi0-pos-B");
  764. if IS_ERR(pmx)
  765. return PTR_ERR(pmx);
  766. pinmux_enable(pmx);
  767. ...
  768. }
  769. The above has to be done from process context.