pinctrl.txt 31 KB

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