core.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 (map->ctrl_dev_name &&
  692. !strcmp(map->ctrl_dev_name, devname) &&
  693. !strcmp(map->dev_name, devname)) {
  694. /* OK time to hog! */
  695. ret = pinctrl_hog_map(pctldev, map);
  696. if (ret) {
  697. mutex_unlock(&pinctrl_maps_mutex);
  698. return ret;
  699. }
  700. }
  701. }
  702. mutex_unlock(&pinctrl_maps_mutex);
  703. return 0;
  704. }
  705. /**
  706. * pinctrl_unhog_maps() - unhog specific map entries on controller device
  707. * @pctldev: the pin control device to unhog entries on
  708. */
  709. static void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
  710. {
  711. struct list_head *node, *tmp;
  712. mutex_lock(&pctldev->pinctrl_hogs_lock);
  713. list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
  714. struct pinctrl_hog *hog =
  715. list_entry(node, struct pinctrl_hog, node);
  716. pinctrl_disable(hog->p);
  717. pinctrl_put(hog->p);
  718. list_del(node);
  719. kfree(hog);
  720. }
  721. mutex_unlock(&pctldev->pinctrl_hogs_lock);
  722. }
  723. #ifdef CONFIG_DEBUG_FS
  724. static int pinctrl_pins_show(struct seq_file *s, void *what)
  725. {
  726. struct pinctrl_dev *pctldev = s->private;
  727. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  728. unsigned i, pin;
  729. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  730. /* The pin number can be retrived from the pin controller descriptor */
  731. for (i = 0; i < pctldev->desc->npins; i++) {
  732. struct pin_desc *desc;
  733. pin = pctldev->desc->pins[i].number;
  734. desc = pin_desc_get(pctldev, pin);
  735. /* Pin space may be sparse */
  736. if (desc == NULL)
  737. continue;
  738. seq_printf(s, "pin %d (%s) ", pin,
  739. desc->name ? desc->name : "unnamed");
  740. /* Driver-specific info per pin */
  741. if (ops->pin_dbg_show)
  742. ops->pin_dbg_show(pctldev, s, pin);
  743. seq_puts(s, "\n");
  744. }
  745. return 0;
  746. }
  747. static int pinctrl_groups_show(struct seq_file *s, void *what)
  748. {
  749. struct pinctrl_dev *pctldev = s->private;
  750. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  751. unsigned selector = 0;
  752. /* No grouping */
  753. if (!ops)
  754. return 0;
  755. seq_puts(s, "registered pin groups:\n");
  756. while (ops->list_groups(pctldev, selector) >= 0) {
  757. const unsigned *pins;
  758. unsigned num_pins;
  759. const char *gname = ops->get_group_name(pctldev, selector);
  760. int ret;
  761. int i;
  762. ret = ops->get_group_pins(pctldev, selector,
  763. &pins, &num_pins);
  764. if (ret)
  765. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  766. gname);
  767. else {
  768. seq_printf(s, "group: %s, pins = [ ", gname);
  769. for (i = 0; i < num_pins; i++)
  770. seq_printf(s, "%d ", pins[i]);
  771. seq_puts(s, "]\n");
  772. }
  773. selector++;
  774. }
  775. return 0;
  776. }
  777. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  778. {
  779. struct pinctrl_dev *pctldev = s->private;
  780. struct pinctrl_gpio_range *range = NULL;
  781. seq_puts(s, "GPIO ranges handled:\n");
  782. /* Loop over the ranges */
  783. mutex_lock(&pctldev->gpio_ranges_lock);
  784. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  785. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  786. range->id, range->name,
  787. range->base, (range->base + range->npins - 1),
  788. range->pin_base,
  789. (range->pin_base + range->npins - 1));
  790. }
  791. mutex_unlock(&pctldev->gpio_ranges_lock);
  792. return 0;
  793. }
  794. static int pinctrl_maps_show(struct seq_file *s, void *what)
  795. {
  796. struct pinctrl_maps *maps_node;
  797. int i;
  798. struct pinctrl_map const *map;
  799. seq_puts(s, "Pinctrl maps:\n");
  800. mutex_lock(&pinctrl_maps_mutex);
  801. for_each_maps(maps_node, i, map) {
  802. seq_printf(s, "%s:\n", map->name);
  803. if (map->dev_name)
  804. seq_printf(s, " device: %s\n",
  805. map->dev_name);
  806. else
  807. seq_printf(s, " SYSTEM MUX\n");
  808. seq_printf(s, " controlling device %s\n",
  809. map->ctrl_dev_name);
  810. seq_printf(s, " function: %s\n", map->function);
  811. seq_printf(s, " group: %s\n", map->group ? map->group :
  812. "(default)");
  813. }
  814. mutex_unlock(&pinctrl_maps_mutex);
  815. return 0;
  816. }
  817. static int pinmux_hogs_show(struct seq_file *s, void *what)
  818. {
  819. struct pinctrl_dev *pctldev = s->private;
  820. struct pinctrl_hog *hog;
  821. seq_puts(s, "Pin control map hogs held by device\n");
  822. list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
  823. seq_printf(s, "%s\n", hog->map->name);
  824. return 0;
  825. }
  826. static int pinctrl_devices_show(struct seq_file *s, void *what)
  827. {
  828. struct pinctrl_dev *pctldev;
  829. seq_puts(s, "name [pinmux] [pinconf]\n");
  830. mutex_lock(&pinctrldev_list_mutex);
  831. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  832. seq_printf(s, "%s ", pctldev->desc->name);
  833. if (pctldev->desc->pmxops)
  834. seq_puts(s, "yes ");
  835. else
  836. seq_puts(s, "no ");
  837. if (pctldev->desc->confops)
  838. seq_puts(s, "yes");
  839. else
  840. seq_puts(s, "no");
  841. seq_puts(s, "\n");
  842. }
  843. mutex_unlock(&pinctrldev_list_mutex);
  844. return 0;
  845. }
  846. static int pinctrl_show(struct seq_file *s, void *what)
  847. {
  848. struct pinctrl *p;
  849. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  850. list_for_each_entry(p, &pinctrl_list, node) {
  851. struct pinctrl_dev *pctldev = p->pctldev;
  852. if (!pctldev) {
  853. seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
  854. continue;
  855. }
  856. seq_printf(s, "device: %s",
  857. pinctrl_dev_get_name(p->pctldev));
  858. pinmux_dbg_show(s, p);
  859. seq_printf(s, " users: %u map-> %s\n",
  860. p->usecount,
  861. p->dev ? dev_name(p->dev) : "(system)");
  862. }
  863. return 0;
  864. }
  865. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  866. {
  867. return single_open(file, pinctrl_pins_show, inode->i_private);
  868. }
  869. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  870. {
  871. return single_open(file, pinctrl_groups_show, inode->i_private);
  872. }
  873. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  874. {
  875. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  876. }
  877. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  878. {
  879. return single_open(file, pinctrl_maps_show, inode->i_private);
  880. }
  881. static int pinmux_hogs_open(struct inode *inode, struct file *file)
  882. {
  883. return single_open(file, pinmux_hogs_show, inode->i_private);
  884. }
  885. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  886. {
  887. return single_open(file, pinctrl_devices_show, NULL);
  888. }
  889. static int pinctrl_open(struct inode *inode, struct file *file)
  890. {
  891. return single_open(file, pinctrl_show, NULL);
  892. }
  893. static const struct file_operations pinctrl_pins_ops = {
  894. .open = pinctrl_pins_open,
  895. .read = seq_read,
  896. .llseek = seq_lseek,
  897. .release = single_release,
  898. };
  899. static const struct file_operations pinctrl_groups_ops = {
  900. .open = pinctrl_groups_open,
  901. .read = seq_read,
  902. .llseek = seq_lseek,
  903. .release = single_release,
  904. };
  905. static const struct file_operations pinctrl_gpioranges_ops = {
  906. .open = pinctrl_gpioranges_open,
  907. .read = seq_read,
  908. .llseek = seq_lseek,
  909. .release = single_release,
  910. };
  911. static const struct file_operations pinctrl_maps_ops = {
  912. .open = pinctrl_maps_open,
  913. .read = seq_read,
  914. .llseek = seq_lseek,
  915. .release = single_release,
  916. };
  917. static const struct file_operations pinmux_hogs_ops = {
  918. .open = pinmux_hogs_open,
  919. .read = seq_read,
  920. .llseek = seq_lseek,
  921. .release = single_release,
  922. };
  923. static const struct file_operations pinctrl_devices_ops = {
  924. .open = pinctrl_devices_open,
  925. .read = seq_read,
  926. .llseek = seq_lseek,
  927. .release = single_release,
  928. };
  929. static const struct file_operations pinctrl_ops = {
  930. .open = pinctrl_open,
  931. .read = seq_read,
  932. .llseek = seq_lseek,
  933. .release = single_release,
  934. };
  935. static struct dentry *debugfs_root;
  936. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  937. {
  938. struct dentry *device_root;
  939. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  940. debugfs_root);
  941. pctldev->device_root = device_root;
  942. if (IS_ERR(device_root) || !device_root) {
  943. pr_warn("failed to create debugfs directory for %s\n",
  944. dev_name(pctldev->dev));
  945. return;
  946. }
  947. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  948. device_root, pctldev, &pinctrl_pins_ops);
  949. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  950. device_root, pctldev, &pinctrl_groups_ops);
  951. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  952. device_root, pctldev, &pinctrl_gpioranges_ops);
  953. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  954. device_root, pctldev, &pinctrl_maps_ops);
  955. debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
  956. device_root, pctldev, &pinmux_hogs_ops);
  957. pinmux_init_device_debugfs(device_root, pctldev);
  958. pinconf_init_device_debugfs(device_root, pctldev);
  959. }
  960. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  961. {
  962. debugfs_remove_recursive(pctldev->device_root);
  963. }
  964. static void pinctrl_init_debugfs(void)
  965. {
  966. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  967. if (IS_ERR(debugfs_root) || !debugfs_root) {
  968. pr_warn("failed to create debugfs directory\n");
  969. debugfs_root = NULL;
  970. return;
  971. }
  972. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  973. debugfs_root, NULL, &pinctrl_devices_ops);
  974. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  975. debugfs_root, NULL, &pinctrl_ops);
  976. }
  977. #else /* CONFIG_DEBUG_FS */
  978. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  979. {
  980. }
  981. static void pinctrl_init_debugfs(void)
  982. {
  983. }
  984. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  985. {
  986. }
  987. #endif
  988. /**
  989. * pinctrl_register() - register a pin controller device
  990. * @pctldesc: descriptor for this pin controller
  991. * @dev: parent device for this pin controller
  992. * @driver_data: private pin controller data for this pin controller
  993. */
  994. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  995. struct device *dev, void *driver_data)
  996. {
  997. struct pinctrl_dev *pctldev;
  998. int ret;
  999. if (pctldesc == NULL)
  1000. return NULL;
  1001. if (pctldesc->name == NULL)
  1002. return NULL;
  1003. pctldev = kzalloc(sizeof(struct pinctrl_dev), GFP_KERNEL);
  1004. if (pctldev == NULL)
  1005. return NULL;
  1006. /* Initialize pin control device struct */
  1007. pctldev->owner = pctldesc->owner;
  1008. pctldev->desc = pctldesc;
  1009. pctldev->driver_data = driver_data;
  1010. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1011. spin_lock_init(&pctldev->pin_desc_tree_lock);
  1012. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1013. mutex_init(&pctldev->gpio_ranges_lock);
  1014. pctldev->dev = dev;
  1015. /* If we're implementing pinmuxing, check the ops for sanity */
  1016. if (pctldesc->pmxops) {
  1017. ret = pinmux_check_ops(pctldev);
  1018. if (ret) {
  1019. pr_err("%s pinmux ops lacks necessary functions\n",
  1020. pctldesc->name);
  1021. goto out_err;
  1022. }
  1023. }
  1024. /* If we're implementing pinconfig, check the ops for sanity */
  1025. if (pctldesc->confops) {
  1026. ret = pinconf_check_ops(pctldev);
  1027. if (ret) {
  1028. pr_err("%s pin config ops lacks necessary functions\n",
  1029. pctldesc->name);
  1030. goto out_err;
  1031. }
  1032. }
  1033. /* Register all the pins */
  1034. pr_debug("try to register %d pins on %s...\n",
  1035. pctldesc->npins, pctldesc->name);
  1036. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1037. if (ret) {
  1038. pr_err("error during pin registration\n");
  1039. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1040. pctldesc->npins);
  1041. goto out_err;
  1042. }
  1043. pinctrl_init_device_debugfs(pctldev);
  1044. mutex_lock(&pinctrldev_list_mutex);
  1045. list_add_tail(&pctldev->node, &pinctrldev_list);
  1046. mutex_unlock(&pinctrldev_list_mutex);
  1047. pinctrl_hog_maps(pctldev);
  1048. return pctldev;
  1049. out_err:
  1050. kfree(pctldev);
  1051. return NULL;
  1052. }
  1053. EXPORT_SYMBOL_GPL(pinctrl_register);
  1054. /**
  1055. * pinctrl_unregister() - unregister pinmux
  1056. * @pctldev: pin controller to unregister
  1057. *
  1058. * Called by pinmux drivers to unregister a pinmux.
  1059. */
  1060. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1061. {
  1062. if (pctldev == NULL)
  1063. return;
  1064. pinctrl_remove_device_debugfs(pctldev);
  1065. pinctrl_unhog_maps(pctldev);
  1066. /* TODO: check that no pinmuxes are still active? */
  1067. mutex_lock(&pinctrldev_list_mutex);
  1068. list_del(&pctldev->node);
  1069. mutex_unlock(&pinctrldev_list_mutex);
  1070. /* Destroy descriptor tree */
  1071. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1072. pctldev->desc->npins);
  1073. kfree(pctldev);
  1074. }
  1075. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1076. static int __init pinctrl_init(void)
  1077. {
  1078. pr_info("initialized pinctrl subsystem\n");
  1079. pinctrl_init_debugfs();
  1080. return 0;
  1081. }
  1082. /* init early since many drivers really need to initialized pinmux early */
  1083. core_initcall(pinctrl_init);