core.c 43 KB

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