core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 a dev name */
  402. if (WARN_ON(!dev))
  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. /*
  438. * If we're looking for a specific named map, this must match,
  439. * else we loop and look for the next.
  440. */
  441. if (name != NULL) {
  442. if (map->name == NULL)
  443. continue;
  444. if (strcmp(map->name, name))
  445. continue;
  446. }
  447. ret = pinmux_apply_muxmap(pctldev, p, dev, devname, map);
  448. if (ret) {
  449. kfree(p);
  450. return ERR_PTR(ret);
  451. }
  452. num_maps++;
  453. }
  454. /*
  455. * This may be perfectly legitimate. An IP block may get re-used
  456. * across SoCs. Not all of those SoCs may need pinmux settings for the
  457. * IP block, e.g. if one SoC dedicates pins to that function but
  458. * another doesn't. The driver won't know this, and will always
  459. * attempt to set up the pinmux. The mapping table defines whether any
  460. * HW programming is actually needed.
  461. */
  462. if (!num_maps)
  463. dev_info(dev, "zero maps found for mapping %s\n", name);
  464. dev_dbg(dev, "found %u maps for device %s state %s\n",
  465. num_maps, devname, name ? name : "(undefined)");
  466. /* Add the pinmux to the global list */
  467. mutex_lock(&pinctrl_list_mutex);
  468. list_add_tail(&p->node, &pinctrl_list);
  469. mutex_unlock(&pinctrl_list_mutex);
  470. return p;
  471. }
  472. /**
  473. * pinctrl_get() - retrieves the pin controller handle for a certain device
  474. * @dev: the device to get the pin controller handle for
  475. * @name: an optional specific control mapping name or NULL, the name is only
  476. * needed if you want to have more than one mapping per device, or if you
  477. * need an anonymous pin control (not tied to any specific device)
  478. */
  479. struct pinctrl *pinctrl_get(struct device *dev, const char *name)
  480. {
  481. struct pinctrl *p;
  482. mutex_lock(&pinctrl_maps_mutex);
  483. p = pinctrl_get_locked(dev, name);
  484. mutex_unlock(&pinctrl_maps_mutex);
  485. return p;
  486. }
  487. EXPORT_SYMBOL_GPL(pinctrl_get);
  488. /**
  489. * pinctrl_put() - release a previously claimed pin control handle
  490. * @p: a pin control handle previously claimed by pinctrl_get()
  491. */
  492. void pinctrl_put(struct pinctrl *p)
  493. {
  494. if (p == NULL)
  495. return;
  496. mutex_lock(&p->mutex);
  497. if (p->usecount)
  498. pr_warn("releasing pin control handle with active users!\n");
  499. /* Free the groups and all acquired pins */
  500. pinmux_put(p);
  501. mutex_unlock(&p->mutex);
  502. /* Remove from list */
  503. mutex_lock(&pinctrl_list_mutex);
  504. list_del(&p->node);
  505. mutex_unlock(&pinctrl_list_mutex);
  506. kfree(p);
  507. }
  508. EXPORT_SYMBOL_GPL(pinctrl_put);
  509. /**
  510. * pinctrl_enable() - enable a certain pin controller setting
  511. * @p: the pin control handle to enable, previously claimed by pinctrl_get()
  512. */
  513. int pinctrl_enable(struct pinctrl *p)
  514. {
  515. int ret = 0;
  516. if (p == NULL)
  517. return -EINVAL;
  518. mutex_lock(&p->mutex);
  519. if (p->usecount++ == 0) {
  520. ret = pinmux_enable(p);
  521. if (ret)
  522. p->usecount--;
  523. }
  524. mutex_unlock(&p->mutex);
  525. return ret;
  526. }
  527. EXPORT_SYMBOL_GPL(pinctrl_enable);
  528. /**
  529. * pinctrl_disable() - disable a certain pin control setting
  530. * @p: the pin control handle to disable, previously claimed by pinctrl_get()
  531. */
  532. void pinctrl_disable(struct pinctrl *p)
  533. {
  534. if (p == NULL)
  535. return;
  536. mutex_lock(&p->mutex);
  537. if (--p->usecount == 0) {
  538. pinmux_disable(p);
  539. }
  540. mutex_unlock(&p->mutex);
  541. }
  542. EXPORT_SYMBOL_GPL(pinctrl_disable);
  543. /**
  544. * pinctrl_register_mappings() - register a set of pin controller mappings
  545. * @maps: the pincontrol mappings table to register. This should probably be
  546. * marked with __initdata so it can be discarded after boot. This
  547. * function will perform a shallow copy for the mapping entries.
  548. * @num_maps: the number of maps in the mapping table
  549. */
  550. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  551. unsigned num_maps)
  552. {
  553. int i;
  554. struct pinctrl_maps *maps_node;
  555. pr_debug("add %d pinmux maps\n", num_maps);
  556. /* First sanity check the new mapping */
  557. for (i = 0; i < num_maps; i++) {
  558. if (!maps[i].name) {
  559. pr_err("failed to register map %d: no map name given\n",
  560. i);
  561. return -EINVAL;
  562. }
  563. if (!maps[i].ctrl_dev_name) {
  564. pr_err("failed to register map %s (%d): no pin control device given\n",
  565. maps[i].name, i);
  566. return -EINVAL;
  567. }
  568. if (!maps[i].function) {
  569. pr_err("failed to register map %s (%d): no function ID given\n",
  570. maps[i].name, i);
  571. return -EINVAL;
  572. }
  573. if (!maps[i].dev_name) {
  574. pr_err("failed to register map %s (%d): no device given\n",
  575. maps[i].name, i);
  576. return -EINVAL;
  577. }
  578. }
  579. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  580. if (!maps_node) {
  581. pr_err("failed to alloc struct pinctrl_maps\n");
  582. return -ENOMEM;
  583. }
  584. maps_node->num_maps = num_maps;
  585. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
  586. if (!maps_node->maps) {
  587. pr_err("failed to duplicate mapping table\n");
  588. kfree(maps_node);
  589. return -ENOMEM;
  590. }
  591. mutex_lock(&pinctrl_maps_mutex);
  592. list_add_tail(&maps_node->node, &pinctrl_maps);
  593. mutex_unlock(&pinctrl_maps_mutex);
  594. return 0;
  595. }
  596. #ifdef CONFIG_DEBUG_FS
  597. static int pinctrl_pins_show(struct seq_file *s, void *what)
  598. {
  599. struct pinctrl_dev *pctldev = s->private;
  600. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  601. unsigned i, pin;
  602. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  603. /* The pin number can be retrived from the pin controller descriptor */
  604. for (i = 0; i < pctldev->desc->npins; i++) {
  605. struct pin_desc *desc;
  606. pin = pctldev->desc->pins[i].number;
  607. desc = pin_desc_get(pctldev, pin);
  608. /* Pin space may be sparse */
  609. if (desc == NULL)
  610. continue;
  611. seq_printf(s, "pin %d (%s) ", pin,
  612. desc->name ? desc->name : "unnamed");
  613. /* Driver-specific info per pin */
  614. if (ops->pin_dbg_show)
  615. ops->pin_dbg_show(pctldev, s, pin);
  616. seq_puts(s, "\n");
  617. }
  618. return 0;
  619. }
  620. static int pinctrl_groups_show(struct seq_file *s, void *what)
  621. {
  622. struct pinctrl_dev *pctldev = s->private;
  623. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  624. unsigned selector = 0;
  625. /* No grouping */
  626. if (!ops)
  627. return 0;
  628. seq_puts(s, "registered pin groups:\n");
  629. while (ops->list_groups(pctldev, selector) >= 0) {
  630. const unsigned *pins;
  631. unsigned num_pins;
  632. const char *gname = ops->get_group_name(pctldev, selector);
  633. int ret;
  634. int i;
  635. ret = ops->get_group_pins(pctldev, selector,
  636. &pins, &num_pins);
  637. if (ret)
  638. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  639. gname);
  640. else {
  641. seq_printf(s, "group: %s, pins = [ ", gname);
  642. for (i = 0; i < num_pins; i++)
  643. seq_printf(s, "%d ", pins[i]);
  644. seq_puts(s, "]\n");
  645. }
  646. selector++;
  647. }
  648. return 0;
  649. }
  650. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  651. {
  652. struct pinctrl_dev *pctldev = s->private;
  653. struct pinctrl_gpio_range *range = NULL;
  654. seq_puts(s, "GPIO ranges handled:\n");
  655. /* Loop over the ranges */
  656. mutex_lock(&pctldev->gpio_ranges_lock);
  657. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  658. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  659. range->id, range->name,
  660. range->base, (range->base + range->npins - 1),
  661. range->pin_base,
  662. (range->pin_base + range->npins - 1));
  663. }
  664. mutex_unlock(&pctldev->gpio_ranges_lock);
  665. return 0;
  666. }
  667. static int pinctrl_devices_show(struct seq_file *s, void *what)
  668. {
  669. struct pinctrl_dev *pctldev;
  670. seq_puts(s, "name [pinmux] [pinconf]\n");
  671. mutex_lock(&pinctrldev_list_mutex);
  672. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  673. seq_printf(s, "%s ", pctldev->desc->name);
  674. if (pctldev->desc->pmxops)
  675. seq_puts(s, "yes ");
  676. else
  677. seq_puts(s, "no ");
  678. if (pctldev->desc->confops)
  679. seq_puts(s, "yes");
  680. else
  681. seq_puts(s, "no");
  682. seq_puts(s, "\n");
  683. }
  684. mutex_unlock(&pinctrldev_list_mutex);
  685. return 0;
  686. }
  687. static int pinctrl_maps_show(struct seq_file *s, void *what)
  688. {
  689. struct pinctrl_maps *maps_node;
  690. int i;
  691. struct pinctrl_map const *map;
  692. seq_puts(s, "Pinctrl maps:\n");
  693. mutex_lock(&pinctrl_maps_mutex);
  694. for_each_maps(maps_node, i, map) {
  695. seq_printf(s, "%s:\n", map->name);
  696. seq_printf(s, " device: %s\n", map->dev_name);
  697. seq_printf(s, " controlling device %s\n", map->ctrl_dev_name);
  698. seq_printf(s, " function: %s\n", map->function);
  699. seq_printf(s, " group: %s\n", map->group ? map->group :
  700. "(default)");
  701. }
  702. mutex_unlock(&pinctrl_maps_mutex);
  703. return 0;
  704. }
  705. static int pinctrl_show(struct seq_file *s, void *what)
  706. {
  707. struct pinctrl *p;
  708. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  709. list_for_each_entry(p, &pinctrl_list, node) {
  710. struct pinctrl_dev *pctldev = p->pctldev;
  711. if (!pctldev) {
  712. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  713. continue;
  714. }
  715. seq_printf(s, "device: %s",
  716. pinctrl_dev_get_name(p->pctldev));
  717. pinmux_dbg_show(s, p);
  718. seq_printf(s, " users: %u map-> %s\n",
  719. p->usecount,
  720. p->dev ? dev_name(p->dev) : "(system)");
  721. }
  722. return 0;
  723. }
  724. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  725. {
  726. return single_open(file, pinctrl_pins_show, inode->i_private);
  727. }
  728. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  729. {
  730. return single_open(file, pinctrl_groups_show, inode->i_private);
  731. }
  732. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  733. {
  734. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  735. }
  736. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  737. {
  738. return single_open(file, pinctrl_devices_show, NULL);
  739. }
  740. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  741. {
  742. return single_open(file, pinctrl_maps_show, NULL);
  743. }
  744. static int pinctrl_open(struct inode *inode, struct file *file)
  745. {
  746. return single_open(file, pinctrl_show, NULL);
  747. }
  748. static const struct file_operations pinctrl_pins_ops = {
  749. .open = pinctrl_pins_open,
  750. .read = seq_read,
  751. .llseek = seq_lseek,
  752. .release = single_release,
  753. };
  754. static const struct file_operations pinctrl_groups_ops = {
  755. .open = pinctrl_groups_open,
  756. .read = seq_read,
  757. .llseek = seq_lseek,
  758. .release = single_release,
  759. };
  760. static const struct file_operations pinctrl_gpioranges_ops = {
  761. .open = pinctrl_gpioranges_open,
  762. .read = seq_read,
  763. .llseek = seq_lseek,
  764. .release = single_release,
  765. };
  766. static const struct file_operations pinctrl_devices_ops = {
  767. .open = pinctrl_devices_open,
  768. .read = seq_read,
  769. .llseek = seq_lseek,
  770. .release = single_release,
  771. };
  772. static const struct file_operations pinctrl_maps_ops = {
  773. .open = pinctrl_maps_open,
  774. .read = seq_read,
  775. .llseek = seq_lseek,
  776. .release = single_release,
  777. };
  778. static const struct file_operations pinctrl_ops = {
  779. .open = pinctrl_open,
  780. .read = seq_read,
  781. .llseek = seq_lseek,
  782. .release = single_release,
  783. };
  784. static struct dentry *debugfs_root;
  785. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  786. {
  787. struct dentry *device_root;
  788. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  789. debugfs_root);
  790. pctldev->device_root = device_root;
  791. if (IS_ERR(device_root) || !device_root) {
  792. pr_warn("failed to create debugfs directory for %s\n",
  793. dev_name(pctldev->dev));
  794. return;
  795. }
  796. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  797. device_root, pctldev, &pinctrl_pins_ops);
  798. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  799. device_root, pctldev, &pinctrl_groups_ops);
  800. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  801. device_root, pctldev, &pinctrl_gpioranges_ops);
  802. pinmux_init_device_debugfs(device_root, pctldev);
  803. pinconf_init_device_debugfs(device_root, pctldev);
  804. }
  805. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  806. {
  807. debugfs_remove_recursive(pctldev->device_root);
  808. }
  809. static void pinctrl_init_debugfs(void)
  810. {
  811. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  812. if (IS_ERR(debugfs_root) || !debugfs_root) {
  813. pr_warn("failed to create debugfs directory\n");
  814. debugfs_root = NULL;
  815. return;
  816. }
  817. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  818. debugfs_root, NULL, &pinctrl_devices_ops);
  819. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  820. debugfs_root, NULL, &pinctrl_maps_ops);
  821. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  822. debugfs_root, NULL, &pinctrl_ops);
  823. }
  824. #else /* CONFIG_DEBUG_FS */
  825. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  826. {
  827. }
  828. static void pinctrl_init_debugfs(void)
  829. {
  830. }
  831. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  832. {
  833. }
  834. #endif
  835. /**
  836. * pinctrl_register() - register a pin controller device
  837. * @pctldesc: descriptor for this pin controller
  838. * @dev: parent device for this pin controller
  839. * @driver_data: private pin controller data for this pin controller
  840. */
  841. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  842. struct device *dev, void *driver_data)
  843. {
  844. struct pinctrl_dev *pctldev;
  845. int ret;
  846. if (pctldesc == NULL)
  847. return NULL;
  848. if (pctldesc->name == NULL)
  849. return NULL;
  850. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  851. if (pctldev == NULL) {
  852. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  853. return NULL;
  854. }
  855. /* Initialize pin control device struct */
  856. pctldev->owner = pctldesc->owner;
  857. pctldev->desc = pctldesc;
  858. pctldev->driver_data = driver_data;
  859. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  860. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  861. mutex_init(&pctldev->gpio_ranges_lock);
  862. pctldev->dev = dev;
  863. /* If we're implementing pinmuxing, check the ops for sanity */
  864. if (pctldesc->pmxops) {
  865. ret = pinmux_check_ops(pctldev);
  866. if (ret) {
  867. pr_err("%s pinmux ops lacks necessary functions\n",
  868. pctldesc->name);
  869. goto out_err;
  870. }
  871. }
  872. /* If we're implementing pinconfig, check the ops for sanity */
  873. if (pctldesc->confops) {
  874. ret = pinconf_check_ops(pctldev);
  875. if (ret) {
  876. pr_err("%s pin config ops lacks necessary functions\n",
  877. pctldesc->name);
  878. goto out_err;
  879. }
  880. }
  881. /* Register all the pins */
  882. pr_debug("try to register %d pins on %s...\n",
  883. pctldesc->npins, pctldesc->name);
  884. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  885. if (ret) {
  886. pr_err("error during pin registration\n");
  887. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  888. pctldesc->npins);
  889. goto out_err;
  890. }
  891. mutex_lock(&pinctrldev_list_mutex);
  892. list_add_tail(&pctldev->node, &pinctrldev_list);
  893. mutex_unlock(&pinctrldev_list_mutex);
  894. pctldev->p = pinctrl_get(pctldev->dev, PINCTRL_STATE_DEFAULT);
  895. if (!IS_ERR(pctldev->p))
  896. pinctrl_enable(pctldev->p);
  897. pinctrl_init_device_debugfs(pctldev);
  898. return pctldev;
  899. out_err:
  900. kfree(pctldev);
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL_GPL(pinctrl_register);
  904. /**
  905. * pinctrl_unregister() - unregister pinmux
  906. * @pctldev: pin controller to unregister
  907. *
  908. * Called by pinmux drivers to unregister a pinmux.
  909. */
  910. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  911. {
  912. if (pctldev == NULL)
  913. return;
  914. pinctrl_remove_device_debugfs(pctldev);
  915. if (!IS_ERR(pctldev->p)) {
  916. pinctrl_disable(pctldev->p);
  917. pinctrl_put(pctldev->p);
  918. }
  919. /* TODO: check that no pinmuxes are still active? */
  920. mutex_lock(&pinctrldev_list_mutex);
  921. list_del(&pctldev->node);
  922. mutex_unlock(&pinctrldev_list_mutex);
  923. /* Destroy descriptor tree */
  924. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  925. pctldev->desc->npins);
  926. kfree(pctldev);
  927. }
  928. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  929. static int __init pinctrl_init(void)
  930. {
  931. pr_info("initialized pinctrl subsystem\n");
  932. pinctrl_init_debugfs();
  933. return 0;
  934. }
  935. /* init early since many drivers really need to initialized pinmux early */
  936. core_initcall(pinctrl_init);