core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. /* We should have atleast one map, right */
  492. if (!num_maps) {
  493. pr_err("could not find any mux maps for device %s, ID %s\n",
  494. devname ? devname : "(anonymous)",
  495. name ? name : "(undefined)");
  496. kfree(p);
  497. return ERR_PTR(-EINVAL);
  498. }
  499. pr_debug("found %u mux maps for device %s, UD %s\n",
  500. num_maps,
  501. devname ? devname : "(anonymous)",
  502. name ? name : "(undefined)");
  503. /* Add the pinmux to the global list */
  504. mutex_lock(&pinctrl_list_mutex);
  505. list_add_tail(&p->node, &pinctrl_list);
  506. mutex_unlock(&pinctrl_list_mutex);
  507. return p;
  508. }
  509. /**
  510. * pinctrl_get() - retrieves the pin controller handle for a certain device
  511. * @dev: the device to get the pin controller handle for
  512. * @name: an optional specific control mapping name or NULL, the name is only
  513. * needed if you want to have more than one mapping per device, or if you
  514. * need an anonymous pin control (not tied to any specific device)
  515. */
  516. struct pinctrl *pinctrl_get(struct device *dev, const char *name)
  517. {
  518. struct pinctrl *p;
  519. mutex_lock(&pinctrl_maps_mutex);
  520. p = pinctrl_get_locked(dev, name);
  521. mutex_unlock(&pinctrl_maps_mutex);
  522. return p;
  523. }
  524. EXPORT_SYMBOL_GPL(pinctrl_get);
  525. /**
  526. * pinctrl_put() - release a previously claimed pin control handle
  527. * @p: a pin control handle previously claimed by pinctrl_get()
  528. */
  529. void pinctrl_put(struct pinctrl *p)
  530. {
  531. if (p == NULL)
  532. return;
  533. mutex_lock(&p->mutex);
  534. if (p->usecount)
  535. pr_warn("releasing pin control handle with active users!\n");
  536. /* Free the groups and all acquired pins */
  537. pinmux_put(p);
  538. mutex_unlock(&p->mutex);
  539. /* Remove from list */
  540. mutex_lock(&pinctrl_list_mutex);
  541. list_del(&p->node);
  542. mutex_unlock(&pinctrl_list_mutex);
  543. kfree(p);
  544. }
  545. EXPORT_SYMBOL_GPL(pinctrl_put);
  546. /**
  547. * pinctrl_enable() - enable a certain pin controller setting
  548. * @p: the pin control handle to enable, previously claimed by pinctrl_get()
  549. */
  550. int pinctrl_enable(struct pinctrl *p)
  551. {
  552. int ret = 0;
  553. if (p == NULL)
  554. return -EINVAL;
  555. mutex_lock(&p->mutex);
  556. if (p->usecount++ == 0) {
  557. ret = pinmux_enable(p);
  558. if (ret)
  559. p->usecount--;
  560. }
  561. mutex_unlock(&p->mutex);
  562. return ret;
  563. }
  564. EXPORT_SYMBOL_GPL(pinctrl_enable);
  565. /**
  566. * pinctrl_disable() - disable a certain pin control setting
  567. * @p: the pin control handle to disable, previously claimed by pinctrl_get()
  568. */
  569. void pinctrl_disable(struct pinctrl *p)
  570. {
  571. if (p == NULL)
  572. return;
  573. mutex_lock(&p->mutex);
  574. if (--p->usecount == 0) {
  575. pinmux_disable(p);
  576. }
  577. mutex_unlock(&p->mutex);
  578. }
  579. EXPORT_SYMBOL_GPL(pinctrl_disable);
  580. /**
  581. * pinctrl_register_mappings() - register a set of pin controller mappings
  582. * @maps: the pincontrol mappings table to register. This should probably be
  583. * marked with __initdata so it can be discarded after boot. This
  584. * function will perform a shallow copy for the mapping entries.
  585. * @num_maps: the number of maps in the mapping table
  586. */
  587. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  588. unsigned num_maps)
  589. {
  590. int i;
  591. struct pinctrl_maps *maps_node;
  592. pr_debug("add %d pinmux maps\n", num_maps);
  593. /* First sanity check the new mapping */
  594. for (i = 0; i < num_maps; i++) {
  595. if (!maps[i].name) {
  596. pr_err("failed to register map %d: no map name given\n",
  597. i);
  598. return -EINVAL;
  599. }
  600. if (!maps[i].ctrl_dev_name) {
  601. pr_err("failed to register map %s (%d): no pin control device given\n",
  602. maps[i].name, i);
  603. return -EINVAL;
  604. }
  605. if (!maps[i].function) {
  606. pr_err("failed to register map %s (%d): no function ID given\n",
  607. maps[i].name, i);
  608. return -EINVAL;
  609. }
  610. if (!maps[i].dev_name)
  611. pr_debug("add system map %s function %s with no device\n",
  612. maps[i].name,
  613. maps[i].function);
  614. else
  615. pr_debug("register map %s, function %s\n",
  616. maps[i].name,
  617. maps[i].function);
  618. }
  619. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  620. if (!maps_node) {
  621. pr_err("failed to alloc struct pinctrl_maps\n");
  622. return -ENOMEM;
  623. }
  624. maps_node->num_maps = num_maps;
  625. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, GFP_KERNEL);
  626. if (!maps_node->maps) {
  627. kfree(maps_node);
  628. return -ENOMEM;
  629. }
  630. mutex_lock(&pinctrl_maps_mutex);
  631. list_add_tail(&maps_node->node, &pinctrl_maps);
  632. mutex_unlock(&pinctrl_maps_mutex);
  633. return 0;
  634. }
  635. /* Hog a single map entry and add to the hoglist */
  636. static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
  637. struct pinctrl_map const *map)
  638. {
  639. struct pinctrl_hog *hog;
  640. struct pinctrl *p;
  641. int ret;
  642. hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
  643. if (!hog)
  644. return -ENOMEM;
  645. p = pinctrl_get_locked(pctldev->dev, map->name);
  646. if (IS_ERR(p)) {
  647. kfree(hog);
  648. dev_err(pctldev->dev,
  649. "could not get the %s pin control mapping for hogging\n",
  650. map->name);
  651. return PTR_ERR(p);
  652. }
  653. ret = pinctrl_enable(p);
  654. if (ret) {
  655. pinctrl_put(p);
  656. kfree(hog);
  657. dev_err(pctldev->dev,
  658. "could not enable the %s pin control mapping for hogging\n",
  659. map->name);
  660. return ret;
  661. }
  662. hog->map = map;
  663. hog->p = p;
  664. dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
  665. map->function);
  666. mutex_lock(&pctldev->pinctrl_hogs_lock);
  667. list_add_tail(&hog->node, &pctldev->pinctrl_hogs);
  668. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  669. return 0;
  670. }
  671. /**
  672. * pinctrl_hog_maps() - hog specific map entries on controller device
  673. * @pctldev: the pin control device to hog entries on
  674. *
  675. * When the pin controllers are registered, there may be some specific pinmux
  676. * map entries that need to be hogged, i.e. get+enabled until the system shuts
  677. * down.
  678. */
  679. static int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
  680. {
  681. struct device *dev = pctldev->dev;
  682. const char *devname = dev_name(dev);
  683. int ret;
  684. struct pinctrl_maps *maps_node;
  685. int i;
  686. struct pinctrl_map const *map;
  687. INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
  688. mutex_init(&pctldev->pinctrl_hogs_lock);
  689. mutex_lock(&pinctrl_maps_mutex);
  690. for_each_maps(maps_node, i, map) {
  691. if (!strcmp(map->ctrl_dev_name, devname) &&
  692. !strcmp(map->dev_name, devname)) {
  693. /* OK time to hog! */
  694. ret = pinctrl_hog_map(pctldev, map);
  695. if (ret) {
  696. mutex_unlock(&pinctrl_maps_mutex);
  697. return ret;
  698. }
  699. }
  700. }
  701. mutex_unlock(&pinctrl_maps_mutex);
  702. return 0;
  703. }
  704. /**
  705. * pinctrl_unhog_maps() - unhog specific map entries on controller device
  706. * @pctldev: the pin control device to unhog entries on
  707. */
  708. static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
  709. {
  710. struct list_head *node, *tmp;
  711. mutex_lock(&pctldev->pinctrl_hogs_lock);
  712. list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
  713. struct pinctrl_hog *hog =
  714. list_entry(node, struct pinctrl_hog, node);
  715. pinctrl_disable(hog->p);
  716. pinctrl_put(hog->p);
  717. list_del(node);
  718. kfree(hog);
  719. }
  720. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  721. }
  722. #ifdef CONFIG_DEBUG_FS
  723. static int pinctrl_pins_show(struct seq_file *s, void *what)
  724. {
  725. struct pinctrl_dev *pctldev = s->private;
  726. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  727. unsigned i, pin;
  728. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  729. /* The pin number can be retrived from the pin controller descriptor */
  730. for (i = 0; i < pctldev->desc->npins; i++) {
  731. struct pin_desc *desc;
  732. pin = pctldev->desc->pins[i].number;
  733. desc = pin_desc_get(pctldev, pin);
  734. /* Pin space may be sparse */
  735. if (desc == NULL)
  736. continue;
  737. seq_printf(s, "pin %d (%s) ", pin,
  738. desc->name ? desc->name : "unnamed");
  739. /* Driver-specific info per pin */
  740. if (ops->pin_dbg_show)
  741. ops->pin_dbg_show(pctldev, s, pin);
  742. seq_puts(s, "\n");
  743. }
  744. return 0;
  745. }
  746. static int pinctrl_groups_show(struct seq_file *s, void *what)
  747. {
  748. struct pinctrl_dev *pctldev = s->private;
  749. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  750. unsigned selector = 0;
  751. /* No grouping */
  752. if (!ops)
  753. return 0;
  754. seq_puts(s, "registered pin groups:\n");
  755. while (ops->list_groups(pctldev, selector) >= 0) {
  756. const unsigned *pins;
  757. unsigned num_pins;
  758. const char *gname = ops->get_group_name(pctldev, selector);
  759. int ret;
  760. int i;
  761. ret = ops->get_group_pins(pctldev, selector,
  762. &pins, &num_pins);
  763. if (ret)
  764. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  765. gname);
  766. else {
  767. seq_printf(s, "group: %s, pins = [ ", gname);
  768. for (i = 0; i < num_pins; i++)
  769. seq_printf(s, "%d ", pins[i]);
  770. seq_puts(s, "]\n");
  771. }
  772. selector++;
  773. }
  774. return 0;
  775. }
  776. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  777. {
  778. struct pinctrl_dev *pctldev = s->private;
  779. struct pinctrl_gpio_range *range = NULL;
  780. seq_puts(s, "GPIO ranges handled:\n");
  781. /* Loop over the ranges */
  782. mutex_lock(&pctldev->gpio_ranges_lock);
  783. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  784. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  785. range->id, range->name,
  786. range->base, (range->base + range->npins - 1),
  787. range->pin_base,
  788. (range->pin_base + range->npins - 1));
  789. }
  790. mutex_unlock(&pctldev->gpio_ranges_lock);
  791. return 0;
  792. }
  793. static int pinctrl_maps_show(struct seq_file *s, void *what)
  794. {
  795. struct pinctrl_maps *maps_node;
  796. int i;
  797. struct pinctrl_map const *map;
  798. seq_puts(s, "Pinctrl maps:\n");
  799. mutex_lock(&pinctrl_maps_mutex);
  800. for_each_maps(maps_node, i, map) {
  801. seq_printf(s, "%s:\n", map->name);
  802. if (map->dev_name)
  803. seq_printf(s, " device: %s\n",
  804. map->dev_name);
  805. else
  806. seq_printf(s, " SYSTEM MUX\n");
  807. seq_printf(s, " controlling device %s\n",
  808. map->ctrl_dev_name);
  809. seq_printf(s, " function: %s\n", map->function);
  810. seq_printf(s, " group: %s\n", map->group ? map->group :
  811. "(default)");
  812. }
  813. mutex_unlock(&pinctrl_maps_mutex);
  814. return 0;
  815. }
  816. static int pinmux_hogs_show(struct seq_file *s, void *what)
  817. {
  818. struct pinctrl_dev *pctldev = s->private;
  819. struct pinctrl_hog *hog;
  820. seq_puts(s, "Pin control map hogs held by device\n");
  821. list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
  822. seq_printf(s, "%s\n", hog->map->name);
  823. return 0;
  824. }
  825. static int pinctrl_devices_show(struct seq_file *s, void *what)
  826. {
  827. struct pinctrl_dev *pctldev;
  828. seq_puts(s, "name [pinmux] [pinconf]\n");
  829. mutex_lock(&pinctrldev_list_mutex);
  830. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  831. seq_printf(s, "%s ", pctldev->desc->name);
  832. if (pctldev->desc->pmxops)
  833. seq_puts(s, "yes ");
  834. else
  835. seq_puts(s, "no ");
  836. if (pctldev->desc->confops)
  837. seq_puts(s, "yes");
  838. else
  839. seq_puts(s, "no");
  840. seq_puts(s, "\n");
  841. }
  842. mutex_unlock(&pinctrldev_list_mutex);
  843. return 0;
  844. }
  845. static int pinctrl_show(struct seq_file *s, void *what)
  846. {
  847. struct pinctrl *p;
  848. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  849. list_for_each_entry(p, &pinctrl_list, node) {
  850. struct pinctrl_dev *pctldev = p->pctldev;
  851. if (!pctldev) {
  852. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  853. continue;
  854. }
  855. seq_printf(s, "device: %s",
  856. pinctrl_dev_get_name(p->pctldev));
  857. pinmux_dbg_show(s, p);
  858. seq_printf(s, " users: %u map-> %s\n",
  859. p->usecount,
  860. p->dev ? dev_name(p->dev) : "(system)");
  861. }
  862. return 0;
  863. }
  864. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  865. {
  866. return single_open(file, pinctrl_pins_show, inode->i_private);
  867. }
  868. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  869. {
  870. return single_open(file, pinctrl_groups_show, inode->i_private);
  871. }
  872. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  873. {
  874. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  875. }
  876. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  877. {
  878. return single_open(file, pinctrl_maps_show, inode->i_private);
  879. }
  880. static int pinmux_hogs_open(struct inode *inode, struct file *file)
  881. {
  882. return single_open(file, pinmux_hogs_show, inode->i_private);
  883. }
  884. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  885. {
  886. return single_open(file, pinctrl_devices_show, NULL);
  887. }
  888. static int pinctrl_open(struct inode *inode, struct file *file)
  889. {
  890. return single_open(file, pinctrl_show, NULL);
  891. }
  892. static const struct file_operations pinctrl_pins_ops = {
  893. .open = pinctrl_pins_open,
  894. .read = seq_read,
  895. .llseek = seq_lseek,
  896. .release = single_release,
  897. };
  898. static const struct file_operations pinctrl_groups_ops = {
  899. .open = pinctrl_groups_open,
  900. .read = seq_read,
  901. .llseek = seq_lseek,
  902. .release = single_release,
  903. };
  904. static const struct file_operations pinctrl_gpioranges_ops = {
  905. .open = pinctrl_gpioranges_open,
  906. .read = seq_read,
  907. .llseek = seq_lseek,
  908. .release = single_release,
  909. };
  910. static const struct file_operations pinctrl_maps_ops = {
  911. .open = pinctrl_maps_open,
  912. .read = seq_read,
  913. .llseek = seq_lseek,
  914. .release = single_release,
  915. };
  916. static const struct file_operations pinmux_hogs_ops = {
  917. .open = pinmux_hogs_open,
  918. .read = seq_read,
  919. .llseek = seq_lseek,
  920. .release = single_release,
  921. };
  922. static const struct file_operations pinctrl_devices_ops = {
  923. .open = pinctrl_devices_open,
  924. .read = seq_read,
  925. .llseek = seq_lseek,
  926. .release = single_release,
  927. };
  928. static const struct file_operations pinctrl_ops = {
  929. .open = pinctrl_open,
  930. .read = seq_read,
  931. .llseek = seq_lseek,
  932. .release = single_release,
  933. };
  934. static struct dentry *debugfs_root;
  935. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  936. {
  937. struct dentry *device_root;
  938. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  939. debugfs_root);
  940. pctldev->device_root = device_root;
  941. if (IS_ERR(device_root) || !device_root) {
  942. pr_warn("failed to create debugfs directory for %s\n",
  943. dev_name(pctldev->dev));
  944. return;
  945. }
  946. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  947. device_root, pctldev, &pinctrl_pins_ops);
  948. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  949. device_root, pctldev, &pinctrl_groups_ops);
  950. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  951. device_root, pctldev, &pinctrl_gpioranges_ops);
  952. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  953. device_root, pctldev, &pinctrl_maps_ops);
  954. debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
  955. device_root, pctldev, &pinmux_hogs_ops);
  956. pinmux_init_device_debugfs(device_root, pctldev);
  957. pinconf_init_device_debugfs(device_root, pctldev);
  958. }
  959. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  960. {
  961. debugfs_remove_recursive(pctldev->device_root);
  962. }
  963. static void pinctrl_init_debugfs(void)
  964. {
  965. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  966. if (IS_ERR(debugfs_root) || !debugfs_root) {
  967. pr_warn("failed to create debugfs directory\n");
  968. debugfs_root = NULL;
  969. return;
  970. }
  971. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  972. debugfs_root, NULL, &pinctrl_devices_ops);
  973. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  974. debugfs_root, NULL, &pinctrl_ops);
  975. }
  976. #else /* CONFIG_DEBUG_FS */
  977. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  978. {
  979. }
  980. static void pinctrl_init_debugfs(void)
  981. {
  982. }
  983. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  984. {
  985. }
  986. #endif
  987. /**
  988. * pinctrl_register() - register a pin controller device
  989. * @pctldesc: descriptor for this pin controller
  990. * @dev: parent device for this pin controller
  991. * @driver_data: private pin controller data for this pin controller
  992. */
  993. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  994. struct device *dev, void *driver_data)
  995. {
  996. struct pinctrl_dev *pctldev;
  997. int ret;
  998. if (pctldesc == NULL)
  999. return NULL;
  1000. if (pctldesc->name == NULL)
  1001. return NULL;
  1002. pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
  1003. if (pctldev == NULL)
  1004. return NULL;
  1005. /* Initialize pin control device struct */
  1006. pctldev->owner = pctldesc->owner;
  1007. pctldev->desc = pctldesc;
  1008. pctldev->driver_data = driver_data;
  1009. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1010. spin_lock_init(&pctldev->pin_desc_tree_lock);
  1011. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1012. mutex_init(&pctldev->gpio_ranges_lock);
  1013. pctldev->dev = dev;
  1014. /* If we're implementing pinmuxing, check the ops for sanity */
  1015. if (pctldesc->pmxops) {
  1016. ret = pinmux_check_ops(pctldev);
  1017. if (ret) {
  1018. pr_err("%s pinmux ops lacks necessary functions\n",
  1019. pctldesc->name);
  1020. goto out_err;
  1021. }
  1022. }
  1023. /* If we're implementing pinconfig, check the ops for sanity */
  1024. if (pctldesc->confops) {
  1025. ret = pinconf_check_ops(pctldev);
  1026. if (ret) {
  1027. pr_err("%s pin config ops lacks necessary functions\n",
  1028. pctldesc->name);
  1029. goto out_err;
  1030. }
  1031. }
  1032. /* Register all the pins */
  1033. pr_debug("try to register %d pins on %s...\n",
  1034. pctldesc->npins, pctldesc->name);
  1035. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1036. if (ret) {
  1037. pr_err("error during pin registration\n");
  1038. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1039. pctldesc->npins);
  1040. goto out_err;
  1041. }
  1042. pinctrl_init_device_debugfs(pctldev);
  1043. mutex_lock(&pinctrldev_list_mutex);
  1044. list_add_tail(&pctldev->node, &pinctrldev_list);
  1045. mutex_unlock(&pinctrldev_list_mutex);
  1046. pinctrl_hog_maps(pctldev);
  1047. return pctldev;
  1048. out_err:
  1049. kfree(pctldev);
  1050. return NULL;
  1051. }
  1052. EXPORT_SYMBOL_GPL(pinctrl_register);
  1053. /**
  1054. * pinctrl_unregister() - unregister pinmux
  1055. * @pctldev: pin controller to unregister
  1056. *
  1057. * Called by pinmux drivers to unregister a pinmux.
  1058. */
  1059. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1060. {
  1061. if (pctldev == NULL)
  1062. return;
  1063. pinctrl_remove_device_debugfs(pctldev);
  1064. pinctrl_unhog_maps(pctldev);
  1065. /* TODO: check that no pinmuxes are still active? */
  1066. mutex_lock(&pinctrldev_list_mutex);
  1067. list_del(&pctldev->node);
  1068. mutex_unlock(&pinctrldev_list_mutex);
  1069. /* Destroy descriptor tree */
  1070. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1071. pctldev->desc->npins);
  1072. kfree(pctldev);
  1073. }
  1074. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1075. static int __init pinctrl_init(void)
  1076. {
  1077. pr_info("initialized pinctrl subsystem\n");
  1078. pinctrl_init_debugfs();
  1079. return 0;
  1080. }
  1081. /* init early since many drivers really need to initialized pinmux early */
  1082. core_initcall(pinctrl_init);