pinctrl.txt 34 KB

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