core.c 28 KB

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