core.c 32 KB

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