core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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/export.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/radix-tree.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/mutex.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/machine.h>
  30. #include "core.h"
  31. #include "pinmux.h"
  32. #include "pinconf.h"
  33. /**
  34. * struct pinctrl_maps - a list item containing part of the mapping table
  35. * @node: mapping table list node
  36. * @maps: array of mapping table entries
  37. * @num_maps: the number of entries in @maps
  38. */
  39. struct pinctrl_maps {
  40. struct list_head node;
  41. struct pinctrl_map const *maps;
  42. unsigned num_maps;
  43. };
  44. /**
  45. * struct pinctrl_hog - a list item to stash control hogs
  46. * @node: pin control hog list node
  47. * @map: map entry responsible for this hogging
  48. * @pmx: the pin control hogged by this item
  49. */
  50. struct pinctrl_hog {
  51. struct list_head node;
  52. struct pinctrl_map const *map;
  53. struct pinctrl *p;
  54. };
  55. /* Global list of pin control devices */
  56. static DEFINE_MUTEX(pinctrldev_list_mutex);
  57. static LIST_HEAD(pinctrldev_list);
  58. /* List of pin controller handles */
  59. static DEFINE_MUTEX(pinctrl_list_mutex);
  60. static LIST_HEAD(pinctrl_list);
  61. /* Global pinctrl maps */
  62. static DEFINE_MUTEX(pinctrl_maps_mutex);
  63. static LIST_HEAD(pinctrl_maps);
  64. #define for_each_maps(_maps_node_, _i_, _map_) \
  65. list_for_each_entry(_maps_node_, &pinctrl_maps, node) \
  66. for (_i_ = 0, _map_ = &_maps_node_->maps[_i_]; \
  67. _i_ < _maps_node_->num_maps; \
  68. i++, _map_ = &_maps_node_->maps[_i_])
  69. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  70. {
  71. /* We're not allowed to register devices without name */
  72. return pctldev->desc->name;
  73. }
  74. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  75. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  76. {
  77. return pctldev->driver_data;
  78. }
  79. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  80. /**
  81. * get_pinctrl_dev_from_devname() - look up pin controller device
  82. * @devname: the name of a device instance, as returned by dev_name()
  83. *
  84. * Looks up a pin control device matching a certain device name or pure device
  85. * pointer, the pure device pointer will take precedence.
  86. */
  87. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  88. {
  89. struct pinctrl_dev *pctldev = NULL;
  90. bool found = false;
  91. if (!devname)
  92. return NULL;
  93. mutex_lock(&pinctrldev_list_mutex);
  94. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  95. if (!strcmp(dev_name(pctldev->dev), devname)) {
  96. /* Matched on device name */
  97. found = true;
  98. break;
  99. }
  100. }
  101. mutex_unlock(&pinctrldev_list_mutex);
  102. return found ? pctldev : NULL;
  103. }
  104. struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev, unsigned int pin)
  105. {
  106. struct pin_desc *pindesc;
  107. unsigned long flags;
  108. spin_lock_irqsave(&pctldev->pin_desc_tree_lock, flags);
  109. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, pin);
  110. spin_unlock_irqrestore(&pctldev->pin_desc_tree_lock, flags);
  111. return pindesc;
  112. }
  113. /**
  114. * pin_get_from_name() - look up a pin number from a name
  115. * @pctldev: the pin control device to lookup the pin on
  116. * @name: the name of the pin to look up
  117. */
  118. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  119. {
  120. unsigned i, pin;
  121. /* The pin number can be retrived from the pin controller descriptor */
  122. for (i = 0; i < pctldev->desc->npins; i++) {
  123. struct pin_desc *desc;
  124. pin = pctldev->desc->pins[i].number;
  125. desc = pin_desc_get(pctldev, pin);
  126. /* Pin space may be sparse */
  127. if (desc == NULL)
  128. continue;
  129. if (desc->name && !strcmp(name, desc->name))
  130. return pin;
  131. }
  132. return -EINVAL;
  133. }
  134. /**
  135. * pin_is_valid() - check if pin exists on controller
  136. * @pctldev: the pin control device to check the pin on
  137. * @pin: pin to check, use the local pin controller index number
  138. *
  139. * This tells us whether a certain pin exist on a certain pin controller or
  140. * not. Pin lists may be sparse, so some pins may not exist.
  141. */
  142. bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
  143. {
  144. struct pin_desc *pindesc;
  145. if (pin < 0)
  146. return false;
  147. pindesc = pin_desc_get(pctldev, pin);
  148. if (pindesc == NULL)
  149. return false;
  150. return true;
  151. }
  152. EXPORT_SYMBOL_GPL(pin_is_valid);
  153. /* Deletes a range of pin descriptors */
  154. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  155. const struct pinctrl_pin_desc *pins,
  156. unsigned num_pins)
  157. {
  158. int i;
  159. spin_lock(&pctldev->pin_desc_tree_lock);
  160. for (i = 0; i < num_pins; i++) {
  161. struct pin_desc *pindesc;
  162. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  163. pins[i].number);
  164. if (pindesc != NULL) {
  165. radix_tree_delete(&pctldev->pin_desc_tree,
  166. pins[i].number);
  167. if (pindesc->dynamic_name)
  168. kfree(pindesc->name);
  169. }
  170. kfree(pindesc);
  171. }
  172. spin_unlock(&pctldev->pin_desc_tree_lock);
  173. }
  174. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  175. unsigned number, const char *name)
  176. {
  177. struct pin_desc *pindesc;
  178. pindesc = pin_desc_get(pctldev, number);
  179. if (pindesc != NULL) {
  180. pr_err("pin %d already registered on %s\n", number,
  181. pctldev->desc->name);
  182. return -EINVAL;
  183. }
  184. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  185. if (pindesc == NULL)
  186. return -ENOMEM;
  187. spin_lock_init(&pindesc->lock);
  188. /* Set owner */
  189. pindesc->pctldev = pctldev;
  190. /* Copy basic pin info */
  191. if (name) {
  192. pindesc->name = name;
  193. } else {
  194. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
  195. if (pindesc->name == NULL)
  196. return -ENOMEM;
  197. pindesc->dynamic_name = true;
  198. }
  199. spin_lock(&pctldev->pin_desc_tree_lock);
  200. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
  201. spin_unlock(&pctldev->pin_desc_tree_lock);
  202. pr_debug("registered pin %d (%s) on %s\n",
  203. number, pindesc->name, pctldev->desc->name);
  204. return 0;
  205. }
  206. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  207. struct pinctrl_pin_desc const *pins,
  208. unsigned num_descs)
  209. {
  210. unsigned i;
  211. int ret = 0;
  212. for (i = 0; i < num_descs; i++) {
  213. ret = pinctrl_register_one_pin(pctldev,
  214. pins[i].number, pins[i].name);
  215. if (ret)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  222. * @pctldev: pin controller device to check
  223. * @gpio: gpio pin to check taken from the global GPIO pin space
  224. *
  225. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  226. * controller, return the range or NULL
  227. */
  228. static struct pinctrl_gpio_range *
  229. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  230. {
  231. struct pinctrl_gpio_range *range = NULL;
  232. /* Loop over the ranges */
  233. mutex_lock(&pctldev->gpio_ranges_lock);
  234. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  235. /* Check if we're in the valid range */
  236. if (gpio >= range->base &&
  237. gpio < range->base + range->npins) {
  238. mutex_unlock(&pctldev->gpio_ranges_lock);
  239. return range;
  240. }
  241. }
  242. mutex_unlock(&pctldev->gpio_ranges_lock);
  243. return NULL;
  244. }
  245. /**
  246. * pinctrl_get_device_gpio_range() - find device for GPIO range
  247. * @gpio: the pin to locate the pin controller for
  248. * @outdev: the pin control device if found
  249. * @outrange: the GPIO range if found
  250. *
  251. * Find the pin controller handling a certain GPIO pin from the pinspace of
  252. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  253. * negative if the GPIO range could not be found in any device.
  254. */
  255. static int pinctrl_get_device_gpio_range(unsigned gpio,
  256. struct pinctrl_dev **outdev,
  257. struct pinctrl_gpio_range **outrange)
  258. {
  259. struct pinctrl_dev *pctldev = NULL;
  260. /* Loop over the pin controllers */
  261. mutex_lock(&pinctrldev_list_mutex);
  262. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  263. struct pinctrl_gpio_range *range;
  264. range = pinctrl_match_gpio_range(pctldev, gpio);
  265. if (range != NULL) {
  266. *outdev = pctldev;
  267. *outrange = range;
  268. mutex_unlock(&pinctrldev_list_mutex);
  269. return 0;
  270. }
  271. }
  272. mutex_unlock(&pinctrldev_list_mutex);
  273. return -EINVAL;
  274. }
  275. /**
  276. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  277. * @pctldev: pin controller device to add the range to
  278. * @range: the GPIO range to add
  279. *
  280. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  281. * this to register handled ranges after registering your pin controller.
  282. */
  283. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  284. struct pinctrl_gpio_range *range)
  285. {
  286. mutex_lock(&pctldev->gpio_ranges_lock);
  287. list_add_tail(&range->node, &pctldev->gpio_ranges);
  288. mutex_unlock(&pctldev->gpio_ranges_lock);
  289. }
  290. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  291. /**
  292. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  293. * @pctldev: pin controller device to remove the range from
  294. * @range: the GPIO range to remove
  295. */
  296. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  297. struct pinctrl_gpio_range *range)
  298. {
  299. mutex_lock(&pctldev->gpio_ranges_lock);
  300. list_del(&range->node);
  301. mutex_unlock(&pctldev->gpio_ranges_lock);
  302. }
  303. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  304. /**
  305. * pinctrl_get_group_selector() - returns the group selector for a group
  306. * @pctldev: the pin controller handling the group
  307. * @pin_group: the pin group to look up
  308. */
  309. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  310. const char *pin_group)
  311. {
  312. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  313. unsigned group_selector = 0;
  314. while (pctlops->list_groups(pctldev, group_selector) >= 0) {
  315. const char *gname = pctlops->get_group_name(pctldev,
  316. group_selector);
  317. if (!strcmp(gname, pin_group)) {
  318. dev_dbg(pctldev->dev,
  319. "found group selector %u for %s\n",
  320. group_selector,
  321. pin_group);
  322. return group_selector;
  323. }
  324. group_selector++;
  325. }
  326. dev_err(pctldev->dev, "does not have pin group %s\n",
  327. pin_group);
  328. return -EINVAL;
  329. }
  330. /**
  331. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  332. * @gpio: the GPIO pin number from the GPIO subsystem number space
  333. *
  334. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  335. * as part of their gpio_request() semantics, platforms and individual drivers
  336. * shall *NOT* request GPIO pins to be muxed in.
  337. */
  338. int pinctrl_request_gpio(unsigned gpio)
  339. {
  340. struct pinctrl_dev *pctldev;
  341. struct pinctrl_gpio_range *range;
  342. int ret;
  343. int pin;
  344. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  345. if (ret)
  346. return -EINVAL;
  347. /* Convert to the pin controllers number space */
  348. pin = gpio - range->base + range->pin_base;
  349. return pinmux_request_gpio(pctldev, range, pin, gpio);
  350. }
  351. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  352. /**
  353. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  354. * @gpio: the GPIO pin number from the GPIO subsystem number space
  355. *
  356. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  357. * as part of their gpio_free() semantics, platforms and individual drivers
  358. * shall *NOT* request GPIO pins to be muxed out.
  359. */
  360. void pinctrl_free_gpio(unsigned gpio)
  361. {
  362. struct pinctrl_dev *pctldev;
  363. struct pinctrl_gpio_range *range;
  364. int ret;
  365. int pin;
  366. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  367. if (ret)
  368. return;
  369. /* Convert to the pin controllers number space */
  370. pin = gpio - range->base + range->pin_base;
  371. return pinmux_free_gpio(pctldev, pin, range);
  372. }
  373. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  374. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  375. {
  376. struct pinctrl_dev *pctldev;
  377. struct pinctrl_gpio_range *range;
  378. int ret;
  379. int pin;
  380. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  381. if (ret)
  382. return ret;
  383. /* Convert to the pin controllers number space */
  384. pin = gpio - range->base + range->pin_base;
  385. return pinmux_gpio_direction(pctldev, range, pin, input);
  386. }
  387. /**
  388. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  389. * @gpio: the GPIO pin number from the GPIO subsystem number space
  390. *
  391. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  392. * as part of their gpio_direction_input() semantics, platforms and individual
  393. * drivers shall *NOT* touch pin control GPIO calls.
  394. */
  395. int pinctrl_gpio_direction_input(unsigned gpio)
  396. {
  397. return pinctrl_gpio_direction(gpio, true);
  398. }
  399. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  400. /**
  401. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  402. * @gpio: the GPIO pin number from the GPIO subsystem number space
  403. *
  404. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  405. * as part of their gpio_direction_output() semantics, platforms and individual
  406. * drivers shall *NOT* touch pin control GPIO calls.
  407. */
  408. int pinctrl_gpio_direction_output(unsigned gpio)
  409. {
  410. return pinctrl_gpio_direction(gpio, false);
  411. }
  412. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  413. static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
  414. {
  415. struct pinctrl_dev *pctldev = NULL;
  416. const char *devname = NULL;
  417. struct pinctrl *p;
  418. bool found_map;
  419. unsigned num_maps = 0;
  420. int ret = -ENODEV;
  421. struct pinctrl_maps *maps_node;
  422. int i;
  423. struct pinctrl_map const *map;
  424. /* We must have dev or ID or both */
  425. if (!dev && !name)
  426. return ERR_PTR(-EINVAL);
  427. if (dev)
  428. devname = dev_name(dev);
  429. pr_debug("get pin control handle %s for device %s\n", name,
  430. devname ? devname : "(none)");
  431. /*
  432. * create the state cookie holder struct pinctrl for each
  433. * mapping, this is what consumers will get when requesting
  434. * a pin control handle with pinctrl_get()
  435. */
  436. p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
  437. if (p == NULL)
  438. return ERR_PTR(-ENOMEM);
  439. mutex_init(&p->mutex);
  440. pinmux_init_pinctrl_handle(p);
  441. /* Iterate over the pin control maps to locate the right ones */
  442. for_each_maps(maps_node, i, map) {
  443. found_map = false;
  444. /*
  445. * First, try to find the pctldev given in the map
  446. */
  447. pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  448. if (!pctldev) {
  449. pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
  450. map->function);
  451. pr_warning("given pinctrl device name: %s",
  452. map->ctrl_dev_name);
  453. /* Continue to check the other mappings anyway... */
  454. continue;
  455. }
  456. pr_debug("in map, found pctldev %s to handle function %s",
  457. dev_name(pctldev->dev), map->function);
  458. /*
  459. * If we're looking for a specific named map, this must match,
  460. * else we loop and look for the next.
  461. */
  462. if (name != NULL) {
  463. if (map->name == NULL)
  464. continue;
  465. if (strcmp(map->name, name))
  466. continue;
  467. }
  468. /*
  469. * This is for the case where no device name is given, we
  470. * already know that the function name matches from above
  471. * code.
  472. */
  473. if (!map->dev_name && (name != NULL))
  474. found_map = true;
  475. /* If the mapping has a device set up it must match */
  476. if (map->dev_name &&
  477. (!devname || !strcmp(map->dev_name, devname)))
  478. /* MATCH! */
  479. found_map = true;
  480. /* If this map is applicable, then apply it */
  481. if (found_map) {
  482. ret = pinmux_apply_muxmap(pctldev, p, dev,
  483. devname, map);
  484. if (ret) {
  485. kfree(p);
  486. return ERR_PTR(ret);
  487. }
  488. num_maps++;
  489. }
  490. }
  491. /*
  492. * This may be perfectly legitimate. An IP block may get re-used
  493. * across SoCs. Not all of those SoCs may need pinmux settings for the
  494. * IP block, e.g. if one SoC dedicates pins to that function but
  495. * another doesn't. The driver won't know this, and will always
  496. * attempt to set up the pinmux. The mapping table defines whether any
  497. * HW programming is actually needed.
  498. */
  499. if (!num_maps)
  500. dev_info(dev, "zero maps found for mapping %s\n", name);
  501. pr_debug("found %u mux maps for device %s, UD %s\n",
  502. num_maps,
  503. devname ? devname : "(anonymous)",
  504. name ? name : "(undefined)");
  505. /* Add the pinmux to the global list */
  506. mutex_lock(&pinctrl_list_mutex);
  507. list_add_tail(&p->node, &pinctrl_list);
  508. mutex_unlock(&pinctrl_list_mutex);
  509. return p;
  510. }
  511. /**
  512. * pinctrl_get() - retrieves the pin controller handle for a certain device
  513. * @dev: the device to get the pin controller handle for
  514. * @name: an optional specific control mapping name or NULL, the name is only
  515. * needed if you want to have more than one mapping per device, or if you
  516. * need an anonymous pin control (not tied to any specific device)
  517. */
  518. struct pinctrl *pinctrl_get(struct device *dev, const char *name)
  519. {
  520. struct pinctrl *p;
  521. mutex_lock(&pinctrl_maps_mutex);
  522. p = pinctrl_get_locked(dev, name);
  523. mutex_unlock(&pinctrl_maps_mutex);
  524. return p;
  525. }
  526. EXPORT_SYMBOL_GPL(pinctrl_get);
  527. /**
  528. * pinctrl_put() - release a previously claimed pin control handle
  529. * @p: a pin control handle previously claimed by pinctrl_get()
  530. */
  531. void pinctrl_put(struct pinctrl *p)
  532. {
  533. if (p == NULL)
  534. return;
  535. mutex_lock(&p->mutex);
  536. if (p->usecount)
  537. pr_warn("releasing pin control handle with active users!\n");
  538. /* Free the groups and all acquired pins */
  539. pinmux_put(p);
  540. mutex_unlock(&p->mutex);
  541. /* Remove from list */
  542. mutex_lock(&pinctrl_list_mutex);
  543. list_del(&p->node);
  544. mutex_unlock(&pinctrl_list_mutex);
  545. kfree(p);
  546. }
  547. EXPORT_SYMBOL_GPL(pinctrl_put);
  548. /**
  549. * pinctrl_enable() - enable a certain pin controller setting
  550. * @p: the pin control handle to enable, previously claimed by pinctrl_get()
  551. */
  552. int pinctrl_enable(struct pinctrl *p)
  553. {
  554. int ret = 0;
  555. if (p == NULL)
  556. return -EINVAL;
  557. mutex_lock(&p->mutex);
  558. if (p->usecount++ == 0) {
  559. ret = pinmux_enable(p);
  560. if (ret)
  561. p->usecount--;
  562. }
  563. mutex_unlock(&p->mutex);
  564. return ret;
  565. }
  566. EXPORT_SYMBOL_GPL(pinctrl_enable);
  567. /**
  568. * pinctrl_disable() - disable a certain pin control setting
  569. * @p: the pin control handle to disable, previously claimed by pinctrl_get()
  570. */
  571. void pinctrl_disable(struct pinctrl *p)
  572. {
  573. if (p == NULL)
  574. return;
  575. mutex_lock(&p->mutex);
  576. if (--p->usecount == 0) {
  577. pinmux_disable(p);
  578. }
  579. mutex_unlock(&p->mutex);
  580. }
  581. EXPORT_SYMBOL_GPL(pinctrl_disable);
  582. /**
  583. * pinctrl_register_mappings() - register a set of pin controller mappings
  584. * @maps: the pincontrol mappings table to register. This should probably be
  585. * marked with __initdata so it can be discarded after boot. This
  586. * function will perform a shallow copy for the mapping entries.
  587. * @num_maps: the number of maps in the mapping table
  588. */
  589. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  590. unsigned num_maps)
  591. {
  592. int i;
  593. struct pinctrl_maps *maps_node;
  594. pr_debug("add %d pinmux maps\n", num_maps);
  595. /* First sanity check the new mapping */
  596. for (i = 0; i < num_maps; i++) {
  597. if (!maps[i].name) {
  598. pr_err("failed to register map %d: no map name given\n",
  599. i);
  600. return -EINVAL;
  601. }
  602. if (!maps[i].ctrl_dev_name) {
  603. pr_err("failed to register map %s (%d): no pin control device given\n",
  604. maps[i].name, i);
  605. return -EINVAL;
  606. }
  607. if (!maps[i].function) {
  608. pr_err("failed to register map %s (%d): no function ID given\n",
  609. maps[i].name, i);
  610. return -EINVAL;
  611. }
  612. if (!maps[i].dev_name)
  613. pr_debug("add system map %s function %s with no device\n",
  614. maps[i].name,
  615. maps[i].function);
  616. else
  617. pr_debug("register map %s, function %s\n",
  618. maps[i].name,
  619. maps[i].function);
  620. }
  621. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  622. if (!maps_node) {
  623. pr_err("failed to alloc struct pinctrl_maps\n");
  624. return -ENOMEM;
  625. }
  626. maps_node->num_maps = num_maps;
  627. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
  628. if (!maps_node->maps) {
  629. kfree(maps_node);
  630. return -ENOMEM;
  631. }
  632. mutex_lock(&pinctrl_maps_mutex);
  633. list_add_tail(&maps_node->node, &pinctrl_maps);
  634. mutex_unlock(&pinctrl_maps_mutex);
  635. return 0;
  636. }
  637. /* Hog a single map entry and add to the hoglist */
  638. static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
  639. struct pinctrl_map const *map)
  640. {
  641. struct pinctrl_hog *hog;
  642. struct pinctrl *p;
  643. int ret;
  644. hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
  645. if (!hog)
  646. return -ENOMEM;
  647. p = pinctrl_get_locked(pctldev->dev, map->name);
  648. if (IS_ERR(p)) {
  649. kfree(hog);
  650. dev_err(pctldev->dev,
  651. "could not get the %s pin control mapping for hogging\n",
  652. map->name);
  653. return PTR_ERR(p);
  654. }
  655. ret = pinctrl_enable(p);
  656. if (ret) {
  657. pinctrl_put(p);
  658. kfree(hog);
  659. dev_err(pctldev->dev,
  660. "could not enable the %s pin control mapping for hogging\n",
  661. map->name);
  662. return ret;
  663. }
  664. hog->map = map;
  665. hog->p = p;
  666. dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
  667. map->function);
  668. mutex_lock(&pctldev->pinctrl_hogs_lock);
  669. list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
  670. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  671. return 0;
  672. }
  673. /**
  674. * pinctrl_hog_maps() - hog specific map entries on controller device
  675. * @pctldev: the pin control device to hog entries on
  676. *
  677. * When the pin controllers are registered, there may be some specific pinmux
  678. * map entries that need to be hogged, i.e. get+enabled until the system shuts
  679. * down.
  680. */
  681. static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
  682. {
  683. struct device *dev = pctldev->dev;
  684. const char *devname = dev_name(dev);
  685. int ret;
  686. struct pinctrl_maps *maps_node;
  687. int i;
  688. struct pinctrl_map const *map;
  689. INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
  690. mutex_init(&pctldev->pinctrl_hogs_lock);
  691. mutex_lock(&pinctrl_maps_mutex);
  692. for_each_maps(maps_node, i, map) {
  693. if (!strcmp(map->ctrl_dev_name, devname) &&
  694. !strcmp(map->dev_name, devname)) {
  695. /* OK time to hog! */
  696. ret = pinctrl_hog_map(pctldev, map);
  697. if (ret) {
  698. mutex_unlock(&pinctrl_maps_mutex);
  699. return ret;
  700. }
  701. }
  702. }
  703. mutex_unlock(&pinctrl_maps_mutex);
  704. return 0;
  705. }
  706. /**
  707. * pinctrl_unhog_maps() - unhog specific map entries on controller device
  708. * @pctldev: the pin control device to unhog entries on
  709. */
  710. static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
  711. {
  712. struct list_head *node, *tmp;
  713. mutex_lock(&pctldev->pinctrl_hogs_lock);
  714. list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
  715. struct pinctrl_hog *hog =
  716. list_entry(node, struct pinctrl_hog, node);
  717. pinctrl_disable(hog->p);
  718. pinctrl_put(hog->p);
  719. list_del(node);
  720. kfree(hog);
  721. }
  722. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  723. }
  724. #ifdef CONFIG_DEBUG_FS
  725. static int pinctrl_pins_show(struct seq_file *s, void *what)
  726. {
  727. struct pinctrl_dev *pctldev = s->private;
  728. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  729. unsigned i, pin;
  730. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  731. /* The pin number can be retrived from the pin controller descriptor */
  732. for (i = 0; i < pctldev->desc->npins; i++) {
  733. struct pin_desc *desc;
  734. pin = pctldev->desc->pins[i].number;
  735. desc = pin_desc_get(pctldev, pin);
  736. /* Pin space may be sparse */
  737. if (desc == NULL)
  738. continue;
  739. seq_printf(s, "pin %d (%s) ", pin,
  740. desc->name ? desc->name : "unnamed");
  741. /* Driver-specific info per pin */
  742. if (ops->pin_dbg_show)
  743. ops->pin_dbg_show(pctldev, s, pin);
  744. seq_puts(s, "\n");
  745. }
  746. return 0;
  747. }
  748. static int pinctrl_groups_show(struct seq_file *s, void *what)
  749. {
  750. struct pinctrl_dev *pctldev = s->private;
  751. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  752. unsigned selector = 0;
  753. /* No grouping */
  754. if (!ops)
  755. return 0;
  756. seq_puts(s, "registered pin groups:\n");
  757. while (ops->list_groups(pctldev, selector) >= 0) {
  758. const unsigned *pins;
  759. unsigned num_pins;
  760. const char *gname = ops->get_group_name(pctldev, selector);
  761. int ret;
  762. int i;
  763. ret = ops->get_group_pins(pctldev, selector,
  764. &pins, &num_pins);
  765. if (ret)
  766. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  767. gname);
  768. else {
  769. seq_printf(s, "group: %s, pins = [ ", gname);
  770. for (i = 0; i < num_pins; i++)
  771. seq_printf(s, "%d ", pins[i]);
  772. seq_puts(s, "]\n");
  773. }
  774. selector++;
  775. }
  776. return 0;
  777. }
  778. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  779. {
  780. struct pinctrl_dev *pctldev = s->private;
  781. struct pinctrl_gpio_range *range = NULL;
  782. seq_puts(s, "GPIO ranges handled:\n");
  783. /* Loop over the ranges */
  784. mutex_lock(&pctldev->gpio_ranges_lock);
  785. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  786. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  787. range->id, range->name,
  788. range->base, (range->base + range->npins - 1),
  789. range->pin_base,
  790. (range->pin_base + range->npins - 1));
  791. }
  792. mutex_unlock(&pctldev->gpio_ranges_lock);
  793. return 0;
  794. }
  795. static int pinctrl_maps_show(struct seq_file *s, void *what)
  796. {
  797. struct pinctrl_maps *maps_node;
  798. int i;
  799. struct pinctrl_map const *map;
  800. seq_puts(s, "Pinctrl maps:\n");
  801. mutex_lock(&pinctrl_maps_mutex);
  802. for_each_maps(maps_node, i, map) {
  803. seq_printf(s, "%s:\n", map->name);
  804. if (map->dev_name)
  805. seq_printf(s, " device: %s\n",
  806. map->dev_name);
  807. else
  808. seq_printf(s, " SYSTEM MUX\n");
  809. seq_printf(s, " controlling device %s\n",
  810. map->ctrl_dev_name);
  811. seq_printf(s, " function: %s\n", map->function);
  812. seq_printf(s, " group: %s\n", map->group ? map->group :
  813. "(default)");
  814. }
  815. mutex_unlock(&pinctrl_maps_mutex);
  816. return 0;
  817. }
  818. static int pinmux_hogs_show(struct seq_file *s, void *what)
  819. {
  820. struct pinctrl_dev *pctldev = s->private;
  821. struct pinctrl_hog *hog;
  822. seq_puts(s, "Pin control map hogs held by device\n");
  823. list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
  824. seq_printf(s, "%s\n", hog->map->name);
  825. return 0;
  826. }
  827. static int pinctrl_devices_show(struct seq_file *s, void *what)
  828. {
  829. struct pinctrl_dev *pctldev;
  830. seq_puts(s, "name [pinmux] [pinconf]\n");
  831. mutex_lock(&pinctrldev_list_mutex);
  832. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  833. seq_printf(s, "%s ", pctldev->desc->name);
  834. if (pctldev->desc->pmxops)
  835. seq_puts(s, "yes ");
  836. else
  837. seq_puts(s, "no ");
  838. if (pctldev->desc->confops)
  839. seq_puts(s, "yes");
  840. else
  841. seq_puts(s, "no");
  842. seq_puts(s, "\n");
  843. }
  844. mutex_unlock(&pinctrldev_list_mutex);
  845. return 0;
  846. }
  847. static int pinctrl_show(struct seq_file *s, void *what)
  848. {
  849. struct pinctrl *p;
  850. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  851. list_for_each_entry(p, &pinctrl_list, node) {
  852. struct pinctrl_dev *pctldev = p->pctldev;
  853. if (!pctldev) {
  854. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  855. continue;
  856. }
  857. seq_printf(s, "device: %s",
  858. pinctrl_dev_get_name(p->pctldev));
  859. pinmux_dbg_show(s, p);
  860. seq_printf(s, " users: %u map-> %s\n",
  861. p->usecount,
  862. p->dev ? dev_name(p->dev) : "(system)");
  863. }
  864. return 0;
  865. }
  866. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  867. {
  868. return single_open(file, pinctrl_pins_show, inode->i_private);
  869. }
  870. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  871. {
  872. return single_open(file, pinctrl_groups_show, inode->i_private);
  873. }
  874. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  875. {
  876. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  877. }
  878. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  879. {
  880. return single_open(file, pinctrl_maps_show, inode->i_private);
  881. }
  882. static int pinmux_hogs_open(struct inode *inode, struct file *file)
  883. {
  884. return single_open(file, pinmux_hogs_show, inode->i_private);
  885. }
  886. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  887. {
  888. return single_open(file, pinctrl_devices_show, NULL);
  889. }
  890. static int pinctrl_open(struct inode *inode, struct file *file)
  891. {
  892. return single_open(file, pinctrl_show, NULL);
  893. }
  894. static const struct file_operations pinctrl_pins_ops = {
  895. .open = pinctrl_pins_open,
  896. .read = seq_read,
  897. .llseek = seq_lseek,
  898. .release = single_release,
  899. };
  900. static const struct file_operations pinctrl_groups_ops = {
  901. .open = pinctrl_groups_open,
  902. .read = seq_read,
  903. .llseek = seq_lseek,
  904. .release = single_release,
  905. };
  906. static const struct file_operations pinctrl_gpioranges_ops = {
  907. .open = pinctrl_gpioranges_open,
  908. .read = seq_read,
  909. .llseek = seq_lseek,
  910. .release = single_release,
  911. };
  912. static const struct file_operations pinctrl_maps_ops = {
  913. .open = pinctrl_maps_open,
  914. .read = seq_read,
  915. .llseek = seq_lseek,
  916. .release = single_release,
  917. };
  918. static const struct file_operations pinmux_hogs_ops = {
  919. .open = pinmux_hogs_open,
  920. .read = seq_read,
  921. .llseek = seq_lseek,
  922. .release = single_release,
  923. };
  924. static const struct file_operations pinctrl_devices_ops = {
  925. .open = pinctrl_devices_open,
  926. .read = seq_read,
  927. .llseek = seq_lseek,
  928. .release = single_release,
  929. };
  930. static const struct file_operations pinctrl_ops = {
  931. .open = pinctrl_open,
  932. .read = seq_read,
  933. .llseek = seq_lseek,
  934. .release = single_release,
  935. };
  936. static struct dentry *debugfs_root;
  937. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  938. {
  939. struct dentry *device_root;
  940. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  941. debugfs_root);
  942. pctldev->device_root = device_root;
  943. if (IS_ERR(device_root) || !device_root) {
  944. pr_warn("failed to create debugfs directory for %s\n",
  945. dev_name(pctldev->dev));
  946. return;
  947. }
  948. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  949. device_root, pctldev, &pinctrl_pins_ops);
  950. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  951. device_root, pctldev, &pinctrl_groups_ops);
  952. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  953. device_root, pctldev, &pinctrl_gpioranges_ops);
  954. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  955. device_root, pctldev, &pinctrl_maps_ops);
  956. debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
  957. device_root, pctldev, &pinmux_hogs_ops);
  958. pinmux_init_device_debugfs(device_root, pctldev);
  959. pinconf_init_device_debugfs(device_root, pctldev);
  960. }
  961. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  962. {
  963. debugfs_remove_recursive(pctldev->device_root);
  964. }
  965. static void pinctrl_init_debugfs(void)
  966. {
  967. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  968. if (IS_ERR(debugfs_root) || !debugfs_root) {
  969. pr_warn("failed to create debugfs directory\n");
  970. debugfs_root = NULL;
  971. return;
  972. }
  973. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  974. debugfs_root, NULL, &pinctrl_devices_ops);
  975. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  976. debugfs_root, NULL, &pinctrl_ops);
  977. }
  978. #else /* CONFIG_DEBUG_FS */
  979. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  980. {
  981. }
  982. static void pinctrl_init_debugfs(void)
  983. {
  984. }
  985. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  986. {
  987. }
  988. #endif
  989. /**
  990. * pinctrl_register() - register a pin controller device
  991. * @pctldesc: descriptor for this pin controller
  992. * @dev: parent device for this pin controller
  993. * @driver_data: private pin controller data for this pin controller
  994. */
  995. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  996. struct device *dev, void *driver_data)
  997. {
  998. struct pinctrl_dev *pctldev;
  999. int ret;
  1000. if (pctldesc == NULL)
  1001. return NULL;
  1002. if (pctldesc->name == NULL)
  1003. return NULL;
  1004. pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
  1005. if (pctldev == NULL)
  1006. return NULL;
  1007. /* Initialize pin control device struct */
  1008. pctldev->owner = pctldesc->owner;
  1009. pctldev->desc = pctldesc;
  1010. pctldev->driver_data = driver_data;
  1011. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1012. spin_lock_init(&pctldev->pin_desc_tree_lock);
  1013. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1014. mutex_init(&pctldev->gpio_ranges_lock);
  1015. pctldev->dev = dev;
  1016. /* If we're implementing pinmuxing, check the ops for sanity */
  1017. if (pctldesc->pmxops) {
  1018. ret = pinmux_check_ops(pctldev);
  1019. if (ret) {
  1020. pr_err("%s pinmux ops lacks necessary functions\n",
  1021. pctldesc->name);
  1022. goto out_err;
  1023. }
  1024. }
  1025. /* If we're implementing pinconfig, check the ops for sanity */
  1026. if (pctldesc->confops) {
  1027. ret = pinconf_check_ops(pctldev);
  1028. if (ret) {
  1029. pr_err("%s pin config ops lacks necessary functions\n",
  1030. pctldesc->name);
  1031. goto out_err;
  1032. }
  1033. }
  1034. /* Register all the pins */
  1035. pr_debug("try to register %d pins on %s...\n",
  1036. pctldesc->npins, pctldesc->name);
  1037. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1038. if (ret) {
  1039. pr_err("error during pin registration\n");
  1040. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1041. pctldesc->npins);
  1042. goto out_err;
  1043. }
  1044. pinctrl_init_device_debugfs(pctldev);
  1045. mutex_lock(&pinctrldev_list_mutex);
  1046. list_add_tail(&pctldev->node, &pinctrldev_list);
  1047. mutex_unlock(&pinctrldev_list_mutex);
  1048. pinctrl_hog_maps(pctldev);
  1049. return pctldev;
  1050. out_err:
  1051. kfree(pctldev);
  1052. return NULL;
  1053. }
  1054. EXPORT_SYMBOL_GPL(pinctrl_register);
  1055. /**
  1056. * pinctrl_unregister() - unregister pinmux
  1057. * @pctldev: pin controller to unregister
  1058. *
  1059. * Called by pinmux drivers to unregister a pinmux.
  1060. */
  1061. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1062. {
  1063. if (pctldev == NULL)
  1064. return;
  1065. pinctrl_remove_device_debugfs(pctldev);
  1066. pinctrl_unhog_maps(pctldev);
  1067. /* TODO: check that no pinmuxes are still active? */
  1068. mutex_lock(&pinctrldev_list_mutex);
  1069. list_del(&pctldev->node);
  1070. mutex_unlock(&pinctrldev_list_mutex);
  1071. /* Destroy descriptor tree */
  1072. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1073. pctldev->desc->npins);
  1074. kfree(pctldev);
  1075. }
  1076. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1077. static int __init pinctrl_init(void)
  1078. {
  1079. pr_info("initialized pinctrl subsystem\n");
  1080. pinctrl_init_debugfs();
  1081. return 0;
  1082. }
  1083. /* init early since many drivers really need to initialized pinmux early */
  1084. core_initcall(pinctrl_init);