core.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /*
  2. * Core driver for the pin control subsystem
  3. *
  4. * Copyright (C) 2011-2012 ST-Ericsson SA
  5. * Written on behalf of Linaro for ST-Ericsson
  6. * Based on bits of regulator core, gpio core and clk core
  7. *
  8. * Author: Linus Walleij <linus.walleij@linaro.org>
  9. *
  10. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  11. *
  12. * License terms: GNU General Public License (GPL) version 2
  13. */
  14. #define pr_fmt(fmt) "pinctrl core: " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/kref.h>
  17. #include <linux/export.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/machine.h>
  29. #ifdef CONFIG_GPIOLIB
  30. #include <asm-generic/gpio.h>
  31. #endif
  32. #include "core.h"
  33. #include "devicetree.h"
  34. #include "pinmux.h"
  35. #include "pinconf.h"
  36. static bool pinctrl_dummy_state;
  37. /* Mutex taken to protect pinctrl_list */
  38. static DEFINE_MUTEX(pinctrl_list_mutex);
  39. /* Mutex taken to protect pinctrl_maps */
  40. DEFINE_MUTEX(pinctrl_maps_mutex);
  41. /* Mutex taken to protect pinctrldev_list */
  42. static DEFINE_MUTEX(pinctrldev_list_mutex);
  43. /* Global list of pin control devices (struct pinctrl_dev) */
  44. static LIST_HEAD(pinctrldev_list);
  45. /* List of pin controller handles (struct pinctrl) */
  46. static LIST_HEAD(pinctrl_list);
  47. /* List of pinctrl maps (struct pinctrl_maps) */
  48. LIST_HEAD(pinctrl_maps);
  49. /**
  50. * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
  51. *
  52. * Usually this function is called by platforms without pinctrl driver support
  53. * but run with some shared drivers using pinctrl APIs.
  54. * After calling this function, the pinctrl core will return successfully
  55. * with creating a dummy state for the driver to keep going smoothly.
  56. */
  57. void pinctrl_provide_dummies(void)
  58. {
  59. pinctrl_dummy_state = true;
  60. }
  61. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  62. {
  63. /* We're not allowed to register devices without name */
  64. return pctldev->desc->name;
  65. }
  66. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  67. const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
  68. {
  69. return dev_name(pctldev->dev);
  70. }
  71. EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
  72. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  73. {
  74. return pctldev->driver_data;
  75. }
  76. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  77. /**
  78. * get_pinctrl_dev_from_devname() - look up pin controller device
  79. * @devname: the name of a device instance, as returned by dev_name()
  80. *
  81. * Looks up a pin control device matching a certain device name or pure device
  82. * pointer, the pure device pointer will take precedence.
  83. */
  84. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  85. {
  86. struct pinctrl_dev *pctldev = NULL;
  87. if (!devname)
  88. return NULL;
  89. mutex_lock(&pinctrldev_list_mutex);
  90. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  91. if (!strcmp(dev_name(pctldev->dev), devname)) {
  92. /* Matched on device name */
  93. mutex_unlock(&pinctrldev_list_mutex);
  94. return pctldev;
  95. }
  96. }
  97. mutex_unlock(&pinctrldev_list_mutex);
  98. return NULL;
  99. }
  100. struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
  101. {
  102. struct pinctrl_dev *pctldev;
  103. mutex_lock(&pinctrldev_list_mutex);
  104. list_for_each_entry(pctldev, &pinctrldev_list, node)
  105. if (pctldev->dev->of_node == np) {
  106. mutex_unlock(&pinctrldev_list_mutex);
  107. return pctldev;
  108. }
  109. mutex_unlock(&pinctrldev_list_mutex);
  110. return NULL;
  111. }
  112. /**
  113. * pin_get_from_name() - look up a pin number from a name
  114. * @pctldev: the pin control device to lookup the pin on
  115. * @name: the name of the pin to look up
  116. */
  117. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  118. {
  119. unsigned i, pin;
  120. /* The pin number can be retrived from the pin controller descriptor */
  121. for (i = 0; i < pctldev->desc->npins; i++) {
  122. struct pin_desc *desc;
  123. pin = pctldev->desc->pins[i].number;
  124. desc = pin_desc_get(pctldev, pin);
  125. /* Pin space may be sparse */
  126. if (desc == NULL)
  127. continue;
  128. if (desc->name && !strcmp(name, desc->name))
  129. return pin;
  130. }
  131. return -EINVAL;
  132. }
  133. /**
  134. * pin_get_name_from_id() - look up a pin name from a pin id
  135. * @pctldev: the pin control device to lookup the pin on
  136. * @name: the name of the pin to look up
  137. */
  138. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
  139. {
  140. const struct pin_desc *desc;
  141. desc = pin_desc_get(pctldev, pin);
  142. if (desc == NULL) {
  143. dev_err(pctldev->dev, "failed to get pin(%d) name\n",
  144. pin);
  145. return NULL;
  146. }
  147. return desc->name;
  148. }
  149. /**
  150. * pin_is_valid() - check if pin exists on controller
  151. * @pctldev: the pin control device to check the pin on
  152. * @pin: pin to check, use the local pin controller index number
  153. *
  154. * This tells us whether a certain pin exist on a certain pin controller or
  155. * not. Pin lists may be sparse, so some pins may not exist.
  156. */
  157. bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
  158. {
  159. struct pin_desc *pindesc;
  160. if (pin < 0)
  161. return false;
  162. mutex_lock(&pctldev->mutex);
  163. pindesc = pin_desc_get(pctldev, pin);
  164. mutex_unlock(&pctldev->mutex);
  165. return pindesc != NULL;
  166. }
  167. EXPORT_SYMBOL_GPL(pin_is_valid);
  168. /* Deletes a range of pin descriptors */
  169. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  170. const struct pinctrl_pin_desc *pins,
  171. unsigned num_pins)
  172. {
  173. int i;
  174. for (i = 0; i < num_pins; i++) {
  175. struct pin_desc *pindesc;
  176. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  177. pins[i].number);
  178. if (pindesc != NULL) {
  179. radix_tree_delete(&pctldev->pin_desc_tree,
  180. pins[i].number);
  181. if (pindesc->dynamic_name)
  182. kfree(pindesc->name);
  183. }
  184. kfree(pindesc);
  185. }
  186. }
  187. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  188. unsigned number, const char *name)
  189. {
  190. struct pin_desc *pindesc;
  191. pindesc = pin_desc_get(pctldev, number);
  192. if (pindesc != NULL) {
  193. pr_err("pin %d already registered on %s\n", number,
  194. pctldev->desc->name);
  195. return -EINVAL;
  196. }
  197. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  198. if (pindesc == NULL) {
  199. dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
  200. return -ENOMEM;
  201. }
  202. /* Set owner */
  203. pindesc->pctldev = pctldev;
  204. /* Copy basic pin info */
  205. if (name) {
  206. pindesc->name = name;
  207. } else {
  208. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
  209. if (pindesc->name == NULL) {
  210. kfree(pindesc);
  211. return -ENOMEM;
  212. }
  213. pindesc->dynamic_name = true;
  214. }
  215. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
  216. pr_debug("registered pin %d (%s) on %s\n",
  217. number, pindesc->name, pctldev->desc->name);
  218. return 0;
  219. }
  220. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  221. struct pinctrl_pin_desc const *pins,
  222. unsigned num_descs)
  223. {
  224. unsigned i;
  225. int ret = 0;
  226. for (i = 0; i < num_descs; i++) {
  227. ret = pinctrl_register_one_pin(pctldev,
  228. pins[i].number, pins[i].name);
  229. if (ret)
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. /**
  235. * gpio_to_pin() - GPIO range GPIO number to pin number translation
  236. * @range: GPIO range used for the translation
  237. * @gpio: gpio pin to translate to a pin number
  238. *
  239. * Finds the pin number for a given GPIO using the specified GPIO range
  240. * as a base for translation. The distinction between linear GPIO ranges
  241. * and pin list based GPIO ranges is managed correctly by this function.
  242. *
  243. * This function assumes the gpio is part of the specified GPIO range, use
  244. * only after making sure this is the case (e.g. by calling it on the
  245. * result of successful pinctrl_get_device_gpio_range calls)!
  246. */
  247. static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
  248. unsigned int gpio)
  249. {
  250. unsigned int offset = gpio - range->base;
  251. if (range->pins)
  252. return range->pins[offset];
  253. else
  254. return range->pin_base + offset;
  255. }
  256. /**
  257. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  258. * @pctldev: pin controller device to check
  259. * @gpio: gpio pin to check taken from the global GPIO pin space
  260. *
  261. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  262. * controller, return the range or NULL
  263. */
  264. static struct pinctrl_gpio_range *
  265. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  266. {
  267. struct pinctrl_gpio_range *range = NULL;
  268. mutex_lock(&pctldev->mutex);
  269. /* Loop over the ranges */
  270. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  271. /* Check if we're in the valid range */
  272. if (gpio >= range->base &&
  273. gpio < range->base + range->npins) {
  274. mutex_unlock(&pctldev->mutex);
  275. return range;
  276. }
  277. }
  278. mutex_unlock(&pctldev->mutex);
  279. return NULL;
  280. }
  281. /**
  282. * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
  283. * the same GPIO chip are in range
  284. * @gpio: gpio pin to check taken from the global GPIO pin space
  285. *
  286. * This function is complement of pinctrl_match_gpio_range(). If the return
  287. * value of pinctrl_match_gpio_range() is NULL, this function could be used
  288. * to check whether pinctrl device is ready or not. Maybe some GPIO pins
  289. * of the same GPIO chip don't have back-end pinctrl interface.
  290. * If the return value is true, it means that pinctrl device is ready & the
  291. * certain GPIO pin doesn't have back-end pinctrl device. If the return value
  292. * is false, it means that pinctrl device may not be ready.
  293. */
  294. #ifdef CONFIG_GPIOLIB
  295. static bool pinctrl_ready_for_gpio_range(unsigned gpio)
  296. {
  297. struct pinctrl_dev *pctldev;
  298. struct pinctrl_gpio_range *range = NULL;
  299. struct gpio_chip *chip = gpio_to_chip(gpio);
  300. mutex_lock(&pinctrldev_list_mutex);
  301. /* Loop over the pin controllers */
  302. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  303. /* Loop over the ranges */
  304. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  305. /* Check if any gpio range overlapped with gpio chip */
  306. if (range->base + range->npins - 1 < chip->base ||
  307. range->base > chip->base + chip->ngpio - 1)
  308. continue;
  309. mutex_unlock(&pinctrldev_list_mutex);
  310. return true;
  311. }
  312. }
  313. mutex_unlock(&pinctrldev_list_mutex);
  314. return false;
  315. }
  316. #else
  317. static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
  318. #endif
  319. /**
  320. * pinctrl_get_device_gpio_range() - find device for GPIO range
  321. * @gpio: the pin to locate the pin controller for
  322. * @outdev: the pin control device if found
  323. * @outrange: the GPIO range if found
  324. *
  325. * Find the pin controller handling a certain GPIO pin from the pinspace of
  326. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  327. * -EPROBE_DEFER if the GPIO range could not be found in any device since it
  328. * may still have not been registered.
  329. */
  330. static int pinctrl_get_device_gpio_range(unsigned gpio,
  331. struct pinctrl_dev **outdev,
  332. struct pinctrl_gpio_range **outrange)
  333. {
  334. struct pinctrl_dev *pctldev = NULL;
  335. /* Loop over the pin controllers */
  336. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  337. struct pinctrl_gpio_range *range;
  338. range = pinctrl_match_gpio_range(pctldev, gpio);
  339. if (range != NULL) {
  340. *outdev = pctldev;
  341. *outrange = range;
  342. return 0;
  343. }
  344. }
  345. return -EPROBE_DEFER;
  346. }
  347. /**
  348. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  349. * @pctldev: pin controller device to add the range to
  350. * @range: the GPIO range to add
  351. *
  352. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  353. * this to register handled ranges after registering your pin controller.
  354. */
  355. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  356. struct pinctrl_gpio_range *range)
  357. {
  358. mutex_lock(&pctldev->mutex);
  359. list_add_tail(&range->node, &pctldev->gpio_ranges);
  360. mutex_unlock(&pctldev->mutex);
  361. }
  362. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  363. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  364. struct pinctrl_gpio_range *ranges,
  365. unsigned nranges)
  366. {
  367. int i;
  368. for (i = 0; i < nranges; i++)
  369. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  370. }
  371. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  372. struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
  373. struct pinctrl_gpio_range *range)
  374. {
  375. struct pinctrl_dev *pctldev;
  376. pctldev = get_pinctrl_dev_from_devname(devname);
  377. /*
  378. * If we can't find this device, let's assume that is because
  379. * it has not probed yet, so the driver trying to register this
  380. * range need to defer probing.
  381. */
  382. if (!pctldev) {
  383. return ERR_PTR(-EPROBE_DEFER);
  384. }
  385. pinctrl_add_gpio_range(pctldev, range);
  386. return pctldev;
  387. }
  388. EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
  389. /**
  390. * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
  391. * @pctldev: the pin controller device to look in
  392. * @pin: a controller-local number to find the range for
  393. */
  394. struct pinctrl_gpio_range *
  395. pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
  396. unsigned int pin)
  397. {
  398. struct pinctrl_gpio_range *range;
  399. mutex_lock(&pctldev->mutex);
  400. /* Loop over the ranges */
  401. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  402. /* Check if we're in the valid range */
  403. if (range->pins) {
  404. int a;
  405. for (a = 0; a < range->npins; a++) {
  406. if (range->pins[a] == pin)
  407. goto out;
  408. }
  409. } else if (pin >= range->pin_base &&
  410. pin < range->pin_base + range->npins)
  411. goto out;
  412. }
  413. range = NULL;
  414. out:
  415. mutex_unlock(&pctldev->mutex);
  416. return range;
  417. }
  418. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
  419. /**
  420. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  421. * @pctldev: pin controller device to remove the range from
  422. * @range: the GPIO range to remove
  423. */
  424. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  425. struct pinctrl_gpio_range *range)
  426. {
  427. mutex_lock(&pctldev->mutex);
  428. list_del(&range->node);
  429. mutex_unlock(&pctldev->mutex);
  430. }
  431. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  432. /**
  433. * pinctrl_get_group_selector() - returns the group selector for a group
  434. * @pctldev: the pin controller handling the group
  435. * @pin_group: the pin group to look up
  436. */
  437. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  438. const char *pin_group)
  439. {
  440. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  441. unsigned ngroups = pctlops->get_groups_count(pctldev);
  442. unsigned group_selector = 0;
  443. while (group_selector < ngroups) {
  444. const char *gname = pctlops->get_group_name(pctldev,
  445. group_selector);
  446. if (!strcmp(gname, pin_group)) {
  447. dev_dbg(pctldev->dev,
  448. "found group selector %u for %s\n",
  449. group_selector,
  450. pin_group);
  451. return group_selector;
  452. }
  453. group_selector++;
  454. }
  455. dev_err(pctldev->dev, "does not have pin group %s\n",
  456. pin_group);
  457. return -EINVAL;
  458. }
  459. /**
  460. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  461. * @gpio: the GPIO pin number from the GPIO subsystem number space
  462. *
  463. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  464. * as part of their gpio_request() semantics, platforms and individual drivers
  465. * shall *NOT* request GPIO pins to be muxed in.
  466. */
  467. int pinctrl_request_gpio(unsigned gpio)
  468. {
  469. struct pinctrl_dev *pctldev;
  470. struct pinctrl_gpio_range *range;
  471. int ret;
  472. int pin;
  473. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  474. if (ret) {
  475. if (pinctrl_ready_for_gpio_range(gpio))
  476. ret = 0;
  477. return ret;
  478. }
  479. /* Convert to the pin controllers number space */
  480. pin = gpio_to_pin(range, gpio);
  481. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  482. return ret;
  483. }
  484. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  485. /**
  486. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  487. * @gpio: the GPIO pin number from the GPIO subsystem number space
  488. *
  489. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  490. * as part of their gpio_free() semantics, platforms and individual drivers
  491. * shall *NOT* request GPIO pins to be muxed out.
  492. */
  493. void pinctrl_free_gpio(unsigned gpio)
  494. {
  495. struct pinctrl_dev *pctldev;
  496. struct pinctrl_gpio_range *range;
  497. int ret;
  498. int pin;
  499. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  500. if (ret) {
  501. return;
  502. }
  503. mutex_lock(&pctldev->mutex);
  504. /* Convert to the pin controllers number space */
  505. pin = gpio_to_pin(range, gpio);
  506. pinmux_free_gpio(pctldev, pin, range);
  507. mutex_unlock(&pctldev->mutex);
  508. }
  509. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  510. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  511. {
  512. struct pinctrl_dev *pctldev;
  513. struct pinctrl_gpio_range *range;
  514. int ret;
  515. int pin;
  516. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  517. if (ret) {
  518. return ret;
  519. }
  520. mutex_lock(&pctldev->mutex);
  521. /* Convert to the pin controllers number space */
  522. pin = gpio_to_pin(range, gpio);
  523. ret = pinmux_gpio_direction(pctldev, range, pin, input);
  524. mutex_unlock(&pctldev->mutex);
  525. return ret;
  526. }
  527. /**
  528. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  529. * @gpio: the GPIO pin number from the GPIO subsystem number space
  530. *
  531. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  532. * as part of their gpio_direction_input() semantics, platforms and individual
  533. * drivers shall *NOT* touch pin control GPIO calls.
  534. */
  535. int pinctrl_gpio_direction_input(unsigned gpio)
  536. {
  537. return pinctrl_gpio_direction(gpio, true);
  538. }
  539. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  540. /**
  541. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  542. * @gpio: the GPIO pin number from the GPIO subsystem number space
  543. *
  544. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  545. * as part of their gpio_direction_output() semantics, platforms and individual
  546. * drivers shall *NOT* touch pin control GPIO calls.
  547. */
  548. int pinctrl_gpio_direction_output(unsigned gpio)
  549. {
  550. return pinctrl_gpio_direction(gpio, false);
  551. }
  552. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  553. static struct pinctrl_state *find_state(struct pinctrl *p,
  554. const char *name)
  555. {
  556. struct pinctrl_state *state;
  557. list_for_each_entry(state, &p->states, node)
  558. if (!strcmp(state->name, name))
  559. return state;
  560. return NULL;
  561. }
  562. static struct pinctrl_state *create_state(struct pinctrl *p,
  563. const char *name)
  564. {
  565. struct pinctrl_state *state;
  566. state = kzalloc(sizeof(*state), GFP_KERNEL);
  567. if (state == NULL) {
  568. dev_err(p->dev,
  569. "failed to alloc struct pinctrl_state\n");
  570. return ERR_PTR(-ENOMEM);
  571. }
  572. state->name = name;
  573. INIT_LIST_HEAD(&state->settings);
  574. list_add_tail(&state->node, &p->states);
  575. return state;
  576. }
  577. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
  578. {
  579. struct pinctrl_state *state;
  580. struct pinctrl_setting *setting;
  581. int ret;
  582. state = find_state(p, map->name);
  583. if (!state)
  584. state = create_state(p, map->name);
  585. if (IS_ERR(state))
  586. return PTR_ERR(state);
  587. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  588. return 0;
  589. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  590. if (setting == NULL) {
  591. dev_err(p->dev,
  592. "failed to alloc struct pinctrl_setting\n");
  593. return -ENOMEM;
  594. }
  595. setting->type = map->type;
  596. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  597. if (setting->pctldev == NULL) {
  598. kfree(setting);
  599. /* Do not defer probing of hogs (circular loop) */
  600. if (!strcmp(map->ctrl_dev_name, map->dev_name))
  601. return -ENODEV;
  602. /*
  603. * OK let us guess that the driver is not there yet, and
  604. * let's defer obtaining this pinctrl handle to later...
  605. */
  606. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  607. map->ctrl_dev_name);
  608. return -EPROBE_DEFER;
  609. }
  610. setting->dev_name = map->dev_name;
  611. switch (map->type) {
  612. case PIN_MAP_TYPE_MUX_GROUP:
  613. ret = pinmux_map_to_setting(map, setting);
  614. break;
  615. case PIN_MAP_TYPE_CONFIGS_PIN:
  616. case PIN_MAP_TYPE_CONFIGS_GROUP:
  617. ret = pinconf_map_to_setting(map, setting);
  618. break;
  619. default:
  620. ret = -EINVAL;
  621. break;
  622. }
  623. if (ret < 0) {
  624. kfree(setting);
  625. return ret;
  626. }
  627. list_add_tail(&setting->node, &state->settings);
  628. return 0;
  629. }
  630. static struct pinctrl *find_pinctrl(struct device *dev)
  631. {
  632. struct pinctrl *p;
  633. mutex_lock(&pinctrl_list_mutex);
  634. list_for_each_entry(p, &pinctrl_list, node)
  635. if (p->dev == dev) {
  636. mutex_unlock(&pinctrl_list_mutex);
  637. return p;
  638. }
  639. mutex_unlock(&pinctrl_list_mutex);
  640. return NULL;
  641. }
  642. static void pinctrl_free(struct pinctrl *p, bool inlist);
  643. static struct pinctrl *create_pinctrl(struct device *dev)
  644. {
  645. struct pinctrl *p;
  646. const char *devname;
  647. struct pinctrl_maps *maps_node;
  648. int i;
  649. struct pinctrl_map const *map;
  650. int ret;
  651. /*
  652. * create the state cookie holder struct pinctrl for each
  653. * mapping, this is what consumers will get when requesting
  654. * a pin control handle with pinctrl_get()
  655. */
  656. p = kzalloc(sizeof(*p), GFP_KERNEL);
  657. if (p == NULL) {
  658. dev_err(dev, "failed to alloc struct pinctrl\n");
  659. return ERR_PTR(-ENOMEM);
  660. }
  661. p->dev = dev;
  662. INIT_LIST_HEAD(&p->states);
  663. INIT_LIST_HEAD(&p->dt_maps);
  664. ret = pinctrl_dt_to_map(p);
  665. if (ret < 0) {
  666. kfree(p);
  667. return ERR_PTR(ret);
  668. }
  669. devname = dev_name(dev);
  670. mutex_lock(&pinctrl_maps_mutex);
  671. /* Iterate over the pin control maps to locate the right ones */
  672. for_each_maps(maps_node, i, map) {
  673. /* Map must be for this device */
  674. if (strcmp(map->dev_name, devname))
  675. continue;
  676. ret = add_setting(p, map);
  677. /*
  678. * At this point the adding of a setting may:
  679. *
  680. * - Defer, if the pinctrl device is not yet available
  681. * - Fail, if the pinctrl device is not yet available,
  682. * AND the setting is a hog. We cannot defer that, since
  683. * the hog will kick in immediately after the device
  684. * is registered.
  685. *
  686. * If the error returned was not -EPROBE_DEFER then we
  687. * accumulate the errors to see if we end up with
  688. * an -EPROBE_DEFER later, as that is the worst case.
  689. */
  690. if (ret == -EPROBE_DEFER) {
  691. pinctrl_free(p, false);
  692. mutex_unlock(&pinctrl_maps_mutex);
  693. return ERR_PTR(ret);
  694. }
  695. }
  696. mutex_unlock(&pinctrl_maps_mutex);
  697. if (ret < 0) {
  698. /* If some other error than deferral occured, return here */
  699. pinctrl_free(p, false);
  700. return ERR_PTR(ret);
  701. }
  702. kref_init(&p->users);
  703. /* Add the pinctrl handle to the global list */
  704. list_add_tail(&p->node, &pinctrl_list);
  705. return p;
  706. }
  707. /**
  708. * pinctrl_get() - retrieves the pinctrl handle for a device
  709. * @dev: the device to obtain the handle for
  710. */
  711. struct pinctrl *pinctrl_get(struct device *dev)
  712. {
  713. struct pinctrl *p;
  714. if (WARN_ON(!dev))
  715. return ERR_PTR(-EINVAL);
  716. /*
  717. * See if somebody else (such as the device core) has already
  718. * obtained a handle to the pinctrl for this device. In that case,
  719. * return another pointer to it.
  720. */
  721. p = find_pinctrl(dev);
  722. if (p != NULL) {
  723. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
  724. kref_get(&p->users);
  725. return p;
  726. }
  727. return create_pinctrl(dev);
  728. }
  729. EXPORT_SYMBOL_GPL(pinctrl_get);
  730. static void pinctrl_free_setting(bool disable_setting,
  731. struct pinctrl_setting *setting)
  732. {
  733. switch (setting->type) {
  734. case PIN_MAP_TYPE_MUX_GROUP:
  735. if (disable_setting)
  736. pinmux_disable_setting(setting);
  737. pinmux_free_setting(setting);
  738. break;
  739. case PIN_MAP_TYPE_CONFIGS_PIN:
  740. case PIN_MAP_TYPE_CONFIGS_GROUP:
  741. pinconf_free_setting(setting);
  742. break;
  743. default:
  744. break;
  745. }
  746. }
  747. static void pinctrl_free(struct pinctrl *p, bool inlist)
  748. {
  749. struct pinctrl_state *state, *n1;
  750. struct pinctrl_setting *setting, *n2;
  751. mutex_lock(&pinctrl_list_mutex);
  752. list_for_each_entry_safe(state, n1, &p->states, node) {
  753. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  754. pinctrl_free_setting(state == p->state, setting);
  755. list_del(&setting->node);
  756. kfree(setting);
  757. }
  758. list_del(&state->node);
  759. kfree(state);
  760. }
  761. pinctrl_dt_free_maps(p);
  762. if (inlist)
  763. list_del(&p->node);
  764. kfree(p);
  765. mutex_unlock(&pinctrl_list_mutex);
  766. }
  767. /**
  768. * pinctrl_release() - release the pinctrl handle
  769. * @kref: the kref in the pinctrl being released
  770. */
  771. static void pinctrl_release(struct kref *kref)
  772. {
  773. struct pinctrl *p = container_of(kref, struct pinctrl, users);
  774. pinctrl_free(p, true);
  775. }
  776. /**
  777. * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
  778. * @p: the pinctrl handle to release
  779. */
  780. void pinctrl_put(struct pinctrl *p)
  781. {
  782. kref_put(&p->users, pinctrl_release);
  783. }
  784. EXPORT_SYMBOL_GPL(pinctrl_put);
  785. /**
  786. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  787. * @p: the pinctrl handle to retrieve the state from
  788. * @name: the state name to retrieve
  789. */
  790. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
  791. const char *name)
  792. {
  793. struct pinctrl_state *state;
  794. state = find_state(p, name);
  795. if (!state) {
  796. if (pinctrl_dummy_state) {
  797. /* create dummy state */
  798. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  799. name);
  800. state = create_state(p, name);
  801. } else
  802. state = ERR_PTR(-ENODEV);
  803. }
  804. return state;
  805. }
  806. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  807. /**
  808. * pinctrl_select_state() - select/activate/program a pinctrl state to HW
  809. * @p: the pinctrl handle for the device that requests configuration
  810. * @state: the state handle to select/activate/program
  811. */
  812. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  813. {
  814. struct pinctrl_setting *setting, *setting2;
  815. struct pinctrl_state *old_state = p->state;
  816. int ret;
  817. if (p->state == state)
  818. return 0;
  819. if (p->state) {
  820. /*
  821. * The set of groups with a mux configuration in the old state
  822. * may not be identical to the set of groups with a mux setting
  823. * in the new state. While this might be unusual, it's entirely
  824. * possible for the "user"-supplied mapping table to be written
  825. * that way. For each group that was configured in the old state
  826. * but not in the new state, this code puts that group into a
  827. * safe/disabled state.
  828. */
  829. list_for_each_entry(setting, &p->state->settings, node) {
  830. bool found = false;
  831. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  832. continue;
  833. list_for_each_entry(setting2, &state->settings, node) {
  834. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  835. continue;
  836. if (setting2->data.mux.group ==
  837. setting->data.mux.group) {
  838. found = true;
  839. break;
  840. }
  841. }
  842. if (!found)
  843. pinmux_disable_setting(setting);
  844. }
  845. }
  846. p->state = NULL;
  847. /* Apply all the settings for the new state */
  848. list_for_each_entry(setting, &state->settings, node) {
  849. switch (setting->type) {
  850. case PIN_MAP_TYPE_MUX_GROUP:
  851. ret = pinmux_enable_setting(setting);
  852. break;
  853. case PIN_MAP_TYPE_CONFIGS_PIN:
  854. case PIN_MAP_TYPE_CONFIGS_GROUP:
  855. ret = pinconf_apply_setting(setting);
  856. break;
  857. default:
  858. ret = -EINVAL;
  859. break;
  860. }
  861. if (ret < 0) {
  862. goto unapply_new_state;
  863. }
  864. }
  865. p->state = state;
  866. return 0;
  867. unapply_new_state:
  868. dev_err(p->dev, "Error applying setting, reverse things back\n");
  869. list_for_each_entry(setting2, &state->settings, node) {
  870. if (&setting2->node == &setting->node)
  871. break;
  872. /*
  873. * All we can do here is pinmux_disable_setting.
  874. * That means that some pins are muxed differently now
  875. * than they were before applying the setting (We can't
  876. * "unmux a pin"!), but it's not a big deal since the pins
  877. * are free to be muxed by another apply_setting.
  878. */
  879. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
  880. pinmux_disable_setting(setting2);
  881. }
  882. /* There's no infinite recursive loop here because p->state is NULL */
  883. if (old_state)
  884. pinctrl_select_state(p, old_state);
  885. return ret;
  886. }
  887. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  888. static void devm_pinctrl_release(struct device *dev, void *res)
  889. {
  890. pinctrl_put(*(struct pinctrl **)res);
  891. }
  892. /**
  893. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  894. * @dev: the device to obtain the handle for
  895. *
  896. * If there is a need to explicitly destroy the returned struct pinctrl,
  897. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  898. */
  899. struct pinctrl *devm_pinctrl_get(struct device *dev)
  900. {
  901. struct pinctrl **ptr, *p;
  902. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  903. if (!ptr)
  904. return ERR_PTR(-ENOMEM);
  905. p = pinctrl_get(dev);
  906. if (!IS_ERR(p)) {
  907. *ptr = p;
  908. devres_add(dev, ptr);
  909. } else {
  910. devres_free(ptr);
  911. }
  912. return p;
  913. }
  914. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  915. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  916. {
  917. struct pinctrl **p = res;
  918. return *p == data;
  919. }
  920. /**
  921. * devm_pinctrl_put() - Resource managed pinctrl_put()
  922. * @p: the pinctrl handle to release
  923. *
  924. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  925. * this function will not need to be called and the resource management
  926. * code will ensure that the resource is freed.
  927. */
  928. void devm_pinctrl_put(struct pinctrl *p)
  929. {
  930. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  931. devm_pinctrl_match, p));
  932. }
  933. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  934. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  935. bool dup, bool locked)
  936. {
  937. int i, ret;
  938. struct pinctrl_maps *maps_node;
  939. pr_debug("add %d pinmux maps\n", num_maps);
  940. /* First sanity check the new mapping */
  941. for (i = 0; i < num_maps; i++) {
  942. if (!maps[i].dev_name) {
  943. pr_err("failed to register map %s (%d): no device given\n",
  944. maps[i].name, i);
  945. return -EINVAL;
  946. }
  947. if (!maps[i].name) {
  948. pr_err("failed to register map %d: no map name given\n",
  949. i);
  950. return -EINVAL;
  951. }
  952. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  953. !maps[i].ctrl_dev_name) {
  954. pr_err("failed to register map %s (%d): no pin control device given\n",
  955. maps[i].name, i);
  956. return -EINVAL;
  957. }
  958. switch (maps[i].type) {
  959. case PIN_MAP_TYPE_DUMMY_STATE:
  960. break;
  961. case PIN_MAP_TYPE_MUX_GROUP:
  962. ret = pinmux_validate_map(&maps[i], i);
  963. if (ret < 0)
  964. return ret;
  965. break;
  966. case PIN_MAP_TYPE_CONFIGS_PIN:
  967. case PIN_MAP_TYPE_CONFIGS_GROUP:
  968. ret = pinconf_validate_map(&maps[i], i);
  969. if (ret < 0)
  970. return ret;
  971. break;
  972. default:
  973. pr_err("failed to register map %s (%d): invalid type given\n",
  974. maps[i].name, i);
  975. return -EINVAL;
  976. }
  977. }
  978. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  979. if (!maps_node) {
  980. pr_err("failed to alloc struct pinctrl_maps\n");
  981. return -ENOMEM;
  982. }
  983. maps_node->num_maps = num_maps;
  984. if (dup) {
  985. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  986. GFP_KERNEL);
  987. if (!maps_node->maps) {
  988. pr_err("failed to duplicate mapping table\n");
  989. kfree(maps_node);
  990. return -ENOMEM;
  991. }
  992. } else {
  993. maps_node->maps = maps;
  994. }
  995. if (!locked)
  996. mutex_lock(&pinctrl_maps_mutex);
  997. list_add_tail(&maps_node->node, &pinctrl_maps);
  998. if (!locked)
  999. mutex_unlock(&pinctrl_maps_mutex);
  1000. return 0;
  1001. }
  1002. /**
  1003. * pinctrl_register_mappings() - register a set of pin controller mappings
  1004. * @maps: the pincontrol mappings table to register. This should probably be
  1005. * marked with __initdata so it can be discarded after boot. This
  1006. * function will perform a shallow copy for the mapping entries.
  1007. * @num_maps: the number of maps in the mapping table
  1008. */
  1009. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  1010. unsigned num_maps)
  1011. {
  1012. return pinctrl_register_map(maps, num_maps, true, false);
  1013. }
  1014. void pinctrl_unregister_map(struct pinctrl_map const *map)
  1015. {
  1016. struct pinctrl_maps *maps_node;
  1017. mutex_lock(&pinctrl_maps_mutex);
  1018. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  1019. if (maps_node->maps == map) {
  1020. list_del(&maps_node->node);
  1021. mutex_unlock(&pinctrl_maps_mutex);
  1022. return;
  1023. }
  1024. }
  1025. mutex_unlock(&pinctrl_maps_mutex);
  1026. }
  1027. /**
  1028. * pinctrl_force_sleep() - turn a given controller device into sleep state
  1029. * @pctldev: pin controller device
  1030. */
  1031. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  1032. {
  1033. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1034. return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1038. /**
  1039. * pinctrl_force_default() - turn a given controller device into default state
  1040. * @pctldev: pin controller device
  1041. */
  1042. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1043. {
  1044. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1045. return pinctrl_select_state(pctldev->p, pctldev->hog_default);
  1046. return 0;
  1047. }
  1048. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1049. #ifdef CONFIG_PM
  1050. /**
  1051. * pinctrl_pm_select_default_state() - select default pinctrl state for PM
  1052. * @dev: device to select default state for
  1053. */
  1054. int pinctrl_pm_select_default_state(struct device *dev)
  1055. {
  1056. struct dev_pin_info *pins = dev->pins;
  1057. int ret;
  1058. if (!pins)
  1059. return 0;
  1060. if (IS_ERR(pins->default_state))
  1061. return 0; /* No default state */
  1062. ret = pinctrl_select_state(pins->p, pins->default_state);
  1063. if (ret)
  1064. dev_err(dev, "failed to activate default pinctrl state\n");
  1065. return ret;
  1066. }
  1067. EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
  1068. /**
  1069. * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
  1070. * @dev: device to select sleep state for
  1071. */
  1072. int pinctrl_pm_select_sleep_state(struct device *dev)
  1073. {
  1074. struct dev_pin_info *pins = dev->pins;
  1075. int ret;
  1076. if (!pins)
  1077. return 0;
  1078. if (IS_ERR(pins->sleep_state))
  1079. return 0; /* No sleep state */
  1080. ret = pinctrl_select_state(pins->p, pins->sleep_state);
  1081. if (ret)
  1082. dev_err(dev, "failed to activate pinctrl sleep state\n");
  1083. return ret;
  1084. }
  1085. EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
  1086. /**
  1087. * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
  1088. * @dev: device to select idle state for
  1089. */
  1090. int pinctrl_pm_select_idle_state(struct device *dev)
  1091. {
  1092. struct dev_pin_info *pins = dev->pins;
  1093. int ret;
  1094. if (!pins)
  1095. return 0;
  1096. if (IS_ERR(pins->idle_state))
  1097. return 0; /* No idle state */
  1098. ret = pinctrl_select_state(pins->p, pins->idle_state);
  1099. if (ret)
  1100. dev_err(dev, "failed to activate pinctrl idle state\n");
  1101. return ret;
  1102. }
  1103. EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
  1104. #endif
  1105. #ifdef CONFIG_DEBUG_FS
  1106. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1107. {
  1108. struct pinctrl_dev *pctldev = s->private;
  1109. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1110. unsigned i, pin;
  1111. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1112. mutex_lock(&pctldev->mutex);
  1113. /* The pin number can be retrived from the pin controller descriptor */
  1114. for (i = 0; i < pctldev->desc->npins; i++) {
  1115. struct pin_desc *desc;
  1116. pin = pctldev->desc->pins[i].number;
  1117. desc = pin_desc_get(pctldev, pin);
  1118. /* Pin space may be sparse */
  1119. if (desc == NULL)
  1120. continue;
  1121. seq_printf(s, "pin %d (%s) ", pin,
  1122. desc->name ? desc->name : "unnamed");
  1123. /* Driver-specific info per pin */
  1124. if (ops->pin_dbg_show)
  1125. ops->pin_dbg_show(pctldev, s, pin);
  1126. seq_puts(s, "\n");
  1127. }
  1128. mutex_unlock(&pctldev->mutex);
  1129. return 0;
  1130. }
  1131. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1132. {
  1133. struct pinctrl_dev *pctldev = s->private;
  1134. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1135. unsigned ngroups, selector = 0;
  1136. mutex_lock(&pctldev->mutex);
  1137. ngroups = ops->get_groups_count(pctldev);
  1138. seq_puts(s, "registered pin groups:\n");
  1139. while (selector < ngroups) {
  1140. const unsigned *pins;
  1141. unsigned num_pins;
  1142. const char *gname = ops->get_group_name(pctldev, selector);
  1143. const char *pname;
  1144. int ret;
  1145. int i;
  1146. ret = ops->get_group_pins(pctldev, selector,
  1147. &pins, &num_pins);
  1148. if (ret)
  1149. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1150. gname);
  1151. else {
  1152. seq_printf(s, "group: %s\n", gname);
  1153. for (i = 0; i < num_pins; i++) {
  1154. pname = pin_get_name(pctldev, pins[i]);
  1155. if (WARN_ON(!pname)) {
  1156. mutex_unlock(&pctldev->mutex);
  1157. return -EINVAL;
  1158. }
  1159. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1160. }
  1161. seq_puts(s, "\n");
  1162. }
  1163. selector++;
  1164. }
  1165. mutex_unlock(&pctldev->mutex);
  1166. return 0;
  1167. }
  1168. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1169. {
  1170. struct pinctrl_dev *pctldev = s->private;
  1171. struct pinctrl_gpio_range *range = NULL;
  1172. seq_puts(s, "GPIO ranges handled:\n");
  1173. mutex_lock(&pctldev->mutex);
  1174. /* Loop over the ranges */
  1175. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1176. if (range->pins) {
  1177. int a;
  1178. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
  1179. range->id, range->name,
  1180. range->base, (range->base + range->npins - 1));
  1181. for (a = 0; a < range->npins - 1; a++)
  1182. seq_printf(s, "%u, ", range->pins[a]);
  1183. seq_printf(s, "%u}\n", range->pins[a]);
  1184. }
  1185. else
  1186. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1187. range->id, range->name,
  1188. range->base, (range->base + range->npins - 1),
  1189. range->pin_base,
  1190. (range->pin_base + range->npins - 1));
  1191. }
  1192. mutex_unlock(&pctldev->mutex);
  1193. return 0;
  1194. }
  1195. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1196. {
  1197. struct pinctrl_dev *pctldev;
  1198. seq_puts(s, "name [pinmux] [pinconf]\n");
  1199. mutex_lock(&pinctrldev_list_mutex);
  1200. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1201. seq_printf(s, "%s ", pctldev->desc->name);
  1202. if (pctldev->desc->pmxops)
  1203. seq_puts(s, "yes ");
  1204. else
  1205. seq_puts(s, "no ");
  1206. if (pctldev->desc->confops)
  1207. seq_puts(s, "yes");
  1208. else
  1209. seq_puts(s, "no");
  1210. seq_puts(s, "\n");
  1211. }
  1212. mutex_unlock(&pinctrldev_list_mutex);
  1213. return 0;
  1214. }
  1215. static inline const char *map_type(enum pinctrl_map_type type)
  1216. {
  1217. static const char * const names[] = {
  1218. "INVALID",
  1219. "DUMMY_STATE",
  1220. "MUX_GROUP",
  1221. "CONFIGS_PIN",
  1222. "CONFIGS_GROUP",
  1223. };
  1224. if (type >= ARRAY_SIZE(names))
  1225. return "UNKNOWN";
  1226. return names[type];
  1227. }
  1228. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1229. {
  1230. struct pinctrl_maps *maps_node;
  1231. int i;
  1232. struct pinctrl_map const *map;
  1233. seq_puts(s, "Pinctrl maps:\n");
  1234. mutex_lock(&pinctrl_maps_mutex);
  1235. for_each_maps(maps_node, i, map) {
  1236. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1237. map->dev_name, map->name, map_type(map->type),
  1238. map->type);
  1239. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1240. seq_printf(s, "controlling device %s\n",
  1241. map->ctrl_dev_name);
  1242. switch (map->type) {
  1243. case PIN_MAP_TYPE_MUX_GROUP:
  1244. pinmux_show_map(s, map);
  1245. break;
  1246. case PIN_MAP_TYPE_CONFIGS_PIN:
  1247. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1248. pinconf_show_map(s, map);
  1249. break;
  1250. default:
  1251. break;
  1252. }
  1253. seq_printf(s, "\n");
  1254. }
  1255. mutex_unlock(&pinctrl_maps_mutex);
  1256. return 0;
  1257. }
  1258. static int pinctrl_show(struct seq_file *s, void *what)
  1259. {
  1260. struct pinctrl *p;
  1261. struct pinctrl_state *state;
  1262. struct pinctrl_setting *setting;
  1263. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1264. mutex_lock(&pinctrl_list_mutex);
  1265. list_for_each_entry(p, &pinctrl_list, node) {
  1266. seq_printf(s, "device: %s current state: %s\n",
  1267. dev_name(p->dev),
  1268. p->state ? p->state->name : "none");
  1269. list_for_each_entry(state, &p->states, node) {
  1270. seq_printf(s, " state: %s\n", state->name);
  1271. list_for_each_entry(setting, &state->settings, node) {
  1272. struct pinctrl_dev *pctldev = setting->pctldev;
  1273. seq_printf(s, " type: %s controller %s ",
  1274. map_type(setting->type),
  1275. pinctrl_dev_get_name(pctldev));
  1276. switch (setting->type) {
  1277. case PIN_MAP_TYPE_MUX_GROUP:
  1278. pinmux_show_setting(s, setting);
  1279. break;
  1280. case PIN_MAP_TYPE_CONFIGS_PIN:
  1281. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1282. pinconf_show_setting(s, setting);
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. mutex_unlock(&pinctrl_list_mutex);
  1291. return 0;
  1292. }
  1293. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1294. {
  1295. return single_open(file, pinctrl_pins_show, inode->i_private);
  1296. }
  1297. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1298. {
  1299. return single_open(file, pinctrl_groups_show, inode->i_private);
  1300. }
  1301. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1302. {
  1303. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1304. }
  1305. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1306. {
  1307. return single_open(file, pinctrl_devices_show, NULL);
  1308. }
  1309. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1310. {
  1311. return single_open(file, pinctrl_maps_show, NULL);
  1312. }
  1313. static int pinctrl_open(struct inode *inode, struct file *file)
  1314. {
  1315. return single_open(file, pinctrl_show, NULL);
  1316. }
  1317. static const struct file_operations pinctrl_pins_ops = {
  1318. .open = pinctrl_pins_open,
  1319. .read = seq_read,
  1320. .llseek = seq_lseek,
  1321. .release = single_release,
  1322. };
  1323. static const struct file_operations pinctrl_groups_ops = {
  1324. .open = pinctrl_groups_open,
  1325. .read = seq_read,
  1326. .llseek = seq_lseek,
  1327. .release = single_release,
  1328. };
  1329. static const struct file_operations pinctrl_gpioranges_ops = {
  1330. .open = pinctrl_gpioranges_open,
  1331. .read = seq_read,
  1332. .llseek = seq_lseek,
  1333. .release = single_release,
  1334. };
  1335. static const struct file_operations pinctrl_devices_ops = {
  1336. .open = pinctrl_devices_open,
  1337. .read = seq_read,
  1338. .llseek = seq_lseek,
  1339. .release = single_release,
  1340. };
  1341. static const struct file_operations pinctrl_maps_ops = {
  1342. .open = pinctrl_maps_open,
  1343. .read = seq_read,
  1344. .llseek = seq_lseek,
  1345. .release = single_release,
  1346. };
  1347. static const struct file_operations pinctrl_ops = {
  1348. .open = pinctrl_open,
  1349. .read = seq_read,
  1350. .llseek = seq_lseek,
  1351. .release = single_release,
  1352. };
  1353. static struct dentry *debugfs_root;
  1354. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1355. {
  1356. struct dentry *device_root;
  1357. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1358. debugfs_root);
  1359. pctldev->device_root = device_root;
  1360. if (IS_ERR(device_root) || !device_root) {
  1361. pr_warn("failed to create debugfs directory for %s\n",
  1362. dev_name(pctldev->dev));
  1363. return;
  1364. }
  1365. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1366. device_root, pctldev, &pinctrl_pins_ops);
  1367. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1368. device_root, pctldev, &pinctrl_groups_ops);
  1369. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1370. device_root, pctldev, &pinctrl_gpioranges_ops);
  1371. pinmux_init_device_debugfs(device_root, pctldev);
  1372. pinconf_init_device_debugfs(device_root, pctldev);
  1373. }
  1374. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1375. {
  1376. debugfs_remove_recursive(pctldev->device_root);
  1377. }
  1378. static void pinctrl_init_debugfs(void)
  1379. {
  1380. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1381. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1382. pr_warn("failed to create debugfs directory\n");
  1383. debugfs_root = NULL;
  1384. return;
  1385. }
  1386. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1387. debugfs_root, NULL, &pinctrl_devices_ops);
  1388. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1389. debugfs_root, NULL, &pinctrl_maps_ops);
  1390. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1391. debugfs_root, NULL, &pinctrl_ops);
  1392. }
  1393. #else /* CONFIG_DEBUG_FS */
  1394. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1395. {
  1396. }
  1397. static void pinctrl_init_debugfs(void)
  1398. {
  1399. }
  1400. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1401. {
  1402. }
  1403. #endif
  1404. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1405. {
  1406. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1407. if (!ops ||
  1408. !ops->get_groups_count ||
  1409. !ops->get_group_name ||
  1410. !ops->get_group_pins)
  1411. return -EINVAL;
  1412. if (ops->dt_node_to_map && !ops->dt_free_map)
  1413. return -EINVAL;
  1414. return 0;
  1415. }
  1416. /**
  1417. * pinctrl_register() - register a pin controller device
  1418. * @pctldesc: descriptor for this pin controller
  1419. * @dev: parent device for this pin controller
  1420. * @driver_data: private pin controller data for this pin controller
  1421. */
  1422. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1423. struct device *dev, void *driver_data)
  1424. {
  1425. struct pinctrl_dev *pctldev;
  1426. int ret;
  1427. if (!pctldesc)
  1428. return NULL;
  1429. if (!pctldesc->name)
  1430. return NULL;
  1431. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1432. if (pctldev == NULL) {
  1433. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1434. return NULL;
  1435. }
  1436. /* Initialize pin control device struct */
  1437. pctldev->owner = pctldesc->owner;
  1438. pctldev->desc = pctldesc;
  1439. pctldev->driver_data = driver_data;
  1440. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1441. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1442. pctldev->dev = dev;
  1443. mutex_init(&pctldev->mutex);
  1444. /* check core ops for sanity */
  1445. if (pinctrl_check_ops(pctldev)) {
  1446. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1447. goto out_err;
  1448. }
  1449. /* If we're implementing pinmuxing, check the ops for sanity */
  1450. if (pctldesc->pmxops) {
  1451. if (pinmux_check_ops(pctldev))
  1452. goto out_err;
  1453. }
  1454. /* If we're implementing pinconfig, check the ops for sanity */
  1455. if (pctldesc->confops) {
  1456. if (pinconf_check_ops(pctldev))
  1457. goto out_err;
  1458. }
  1459. /* Register all the pins */
  1460. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1461. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1462. if (ret) {
  1463. dev_err(dev, "error during pin registration\n");
  1464. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1465. pctldesc->npins);
  1466. goto out_err;
  1467. }
  1468. mutex_lock(&pinctrldev_list_mutex);
  1469. list_add_tail(&pctldev->node, &pinctrldev_list);
  1470. mutex_unlock(&pinctrldev_list_mutex);
  1471. pctldev->p = pinctrl_get(pctldev->dev);
  1472. if (!IS_ERR(pctldev->p)) {
  1473. pctldev->hog_default =
  1474. pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
  1475. if (IS_ERR(pctldev->hog_default)) {
  1476. dev_dbg(dev, "failed to lookup the default state\n");
  1477. } else {
  1478. if (pinctrl_select_state(pctldev->p,
  1479. pctldev->hog_default))
  1480. dev_err(dev,
  1481. "failed to select default state\n");
  1482. }
  1483. pctldev->hog_sleep =
  1484. pinctrl_lookup_state(pctldev->p,
  1485. PINCTRL_STATE_SLEEP);
  1486. if (IS_ERR(pctldev->hog_sleep))
  1487. dev_dbg(dev, "failed to lookup the sleep state\n");
  1488. }
  1489. pinctrl_init_device_debugfs(pctldev);
  1490. return pctldev;
  1491. out_err:
  1492. mutex_destroy(&pctldev->mutex);
  1493. kfree(pctldev);
  1494. return NULL;
  1495. }
  1496. EXPORT_SYMBOL_GPL(pinctrl_register);
  1497. /**
  1498. * pinctrl_unregister() - unregister pinmux
  1499. * @pctldev: pin controller to unregister
  1500. *
  1501. * Called by pinmux drivers to unregister a pinmux.
  1502. */
  1503. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1504. {
  1505. struct pinctrl_gpio_range *range, *n;
  1506. if (pctldev == NULL)
  1507. return;
  1508. mutex_lock(&pinctrldev_list_mutex);
  1509. mutex_lock(&pctldev->mutex);
  1510. pinctrl_remove_device_debugfs(pctldev);
  1511. if (!IS_ERR(pctldev->p))
  1512. pinctrl_put(pctldev->p);
  1513. /* TODO: check that no pinmuxes are still active? */
  1514. list_del(&pctldev->node);
  1515. /* Destroy descriptor tree */
  1516. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1517. pctldev->desc->npins);
  1518. /* remove gpio ranges map */
  1519. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1520. list_del(&range->node);
  1521. mutex_unlock(&pctldev->mutex);
  1522. mutex_destroy(&pctldev->mutex);
  1523. kfree(pctldev);
  1524. mutex_unlock(&pinctrldev_list_mutex);
  1525. }
  1526. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1527. static int __init pinctrl_init(void)
  1528. {
  1529. pr_info("initialized pinctrl subsystem\n");
  1530. pinctrl_init_debugfs();
  1531. return 0;
  1532. }
  1533. /* init early since many drivers really need to initialized pinmux early */
  1534. core_initcall(pinctrl_init);