core.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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/kref.h>
  17. #include <linux/export.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/list.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/machine.h>
  29. #ifdef CONFIG_GPIOLIB
  30. #include <asm-generic/gpio.h>
  31. #endif
  32. #include "core.h"
  33. #include "devicetree.h"
  34. #include "pinmux.h"
  35. #include "pinconf.h"
  36. static bool pinctrl_dummy_state;
  37. /* Mutex taken to protect pinctrl_list */
  38. static DEFINE_MUTEX(pinctrl_list_mutex);
  39. /* Mutex taken to protect pinctrl_maps */
  40. DEFINE_MUTEX(pinctrl_maps_mutex);
  41. /* Mutex taken to protect pinctrldev_list */
  42. static DEFINE_MUTEX(pinctrldev_list_mutex);
  43. /* Global list of pin control devices (struct pinctrl_dev) */
  44. static LIST_HEAD(pinctrldev_list);
  45. /* List of pin controller handles (struct pinctrl) */
  46. static LIST_HEAD(pinctrl_list);
  47. /* List of pinctrl maps (struct pinctrl_maps) */
  48. LIST_HEAD(pinctrl_maps);
  49. /**
  50. * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
  51. *
  52. * Usually this function is called by platforms without pinctrl driver support
  53. * but run with some shared drivers using pinctrl APIs.
  54. * After calling this function, the pinctrl core will return successfully
  55. * with creating a dummy state for the driver to keep going smoothly.
  56. */
  57. void pinctrl_provide_dummies(void)
  58. {
  59. pinctrl_dummy_state = true;
  60. }
  61. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  62. {
  63. /* We're not allowed to register devices without name */
  64. return pctldev->desc->name;
  65. }
  66. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  67. const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
  68. {
  69. return dev_name(pctldev->dev);
  70. }
  71. EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
  72. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  73. {
  74. return pctldev->driver_data;
  75. }
  76. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  77. /**
  78. * get_pinctrl_dev_from_devname() - look up pin controller device
  79. * @devname: the name of a device instance, as returned by dev_name()
  80. *
  81. * Looks up a pin control device matching a certain device name or pure device
  82. * pointer, the pure device pointer will take precedence.
  83. */
  84. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  85. {
  86. struct pinctrl_dev *pctldev = NULL;
  87. if (!devname)
  88. return NULL;
  89. mutex_lock(&pinctrldev_list_mutex);
  90. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  91. if (!strcmp(dev_name(pctldev->dev), devname)) {
  92. /* Matched on device name */
  93. mutex_unlock(&pinctrldev_list_mutex);
  94. return pctldev;
  95. }
  96. }
  97. mutex_unlock(&pinctrldev_list_mutex);
  98. return NULL;
  99. }
  100. struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
  101. {
  102. struct pinctrl_dev *pctldev;
  103. mutex_lock(&pinctrldev_list_mutex);
  104. list_for_each_entry(pctldev, &pinctrldev_list, node)
  105. if (pctldev->dev->of_node == np) {
  106. mutex_unlock(&pinctrldev_list_mutex);
  107. return pctldev;
  108. }
  109. mutex_unlock(&pinctrldev_list_mutex);
  110. return NULL;
  111. }
  112. /**
  113. * pin_get_from_name() - look up a pin number from a name
  114. * @pctldev: the pin control device to lookup the pin on
  115. * @name: the name of the pin to look up
  116. */
  117. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  118. {
  119. unsigned i, pin;
  120. /* The pin number can be retrived from the pin controller descriptor */
  121. for (i = 0; i < pctldev->desc->npins; i++) {
  122. struct pin_desc *desc;
  123. pin = pctldev->desc->pins[i].number;
  124. desc = pin_desc_get(pctldev, pin);
  125. /* Pin space may be sparse */
  126. if (desc && !strcmp(name, desc->name))
  127. return pin;
  128. }
  129. return -EINVAL;
  130. }
  131. /**
  132. * pin_get_name_from_id() - look up a pin name from a pin id
  133. * @pctldev: the pin control device to lookup the pin on
  134. * @name: the name of the pin to look up
  135. */
  136. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin)
  137. {
  138. const struct pin_desc *desc;
  139. desc = pin_desc_get(pctldev, pin);
  140. if (desc == NULL) {
  141. dev_err(pctldev->dev, "failed to get pin(%d) name\n",
  142. pin);
  143. return NULL;
  144. }
  145. return desc->name;
  146. }
  147. /**
  148. * pin_is_valid() - check if pin exists on controller
  149. * @pctldev: the pin control device to check the pin on
  150. * @pin: pin to check, use the local pin controller index number
  151. *
  152. * This tells us whether a certain pin exist on a certain pin controller or
  153. * not. Pin lists may be sparse, so some pins may not exist.
  154. */
  155. bool pin_is_valid(struct pinctrl_dev *pctldev, int pin)
  156. {
  157. struct pin_desc *pindesc;
  158. if (pin < 0)
  159. return false;
  160. mutex_lock(&pctldev->mutex);
  161. pindesc = pin_desc_get(pctldev, pin);
  162. mutex_unlock(&pctldev->mutex);
  163. return pindesc != NULL;
  164. }
  165. EXPORT_SYMBOL_GPL(pin_is_valid);
  166. /* Deletes a range of pin descriptors */
  167. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  168. const struct pinctrl_pin_desc *pins,
  169. unsigned num_pins)
  170. {
  171. int i;
  172. for (i = 0; i < num_pins; i++) {
  173. struct pin_desc *pindesc;
  174. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  175. pins[i].number);
  176. if (pindesc != NULL) {
  177. radix_tree_delete(&pctldev->pin_desc_tree,
  178. pins[i].number);
  179. if (pindesc->dynamic_name)
  180. kfree(pindesc->name);
  181. }
  182. kfree(pindesc);
  183. }
  184. }
  185. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  186. unsigned number, const char *name)
  187. {
  188. struct pin_desc *pindesc;
  189. pindesc = pin_desc_get(pctldev, number);
  190. if (pindesc != NULL) {
  191. pr_err("pin %d already registered on %s\n", number,
  192. pctldev->desc->name);
  193. return -EINVAL;
  194. }
  195. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  196. if (pindesc == NULL) {
  197. dev_err(pctldev->dev, "failed to alloc struct pin_desc\n");
  198. return -ENOMEM;
  199. }
  200. /* Set owner */
  201. pindesc->pctldev = pctldev;
  202. /* Copy basic pin info */
  203. if (name) {
  204. pindesc->name = name;
  205. } else {
  206. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number);
  207. if (pindesc->name == NULL) {
  208. kfree(pindesc);
  209. return -ENOMEM;
  210. }
  211. pindesc->dynamic_name = true;
  212. }
  213. radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc);
  214. pr_debug("registered pin %d (%s) on %s\n",
  215. number, pindesc->name, pctldev->desc->name);
  216. return 0;
  217. }
  218. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  219. struct pinctrl_pin_desc const *pins,
  220. unsigned num_descs)
  221. {
  222. unsigned i;
  223. int ret = 0;
  224. for (i = 0; i < num_descs; i++) {
  225. ret = pinctrl_register_one_pin(pctldev,
  226. pins[i].number, pins[i].name);
  227. if (ret)
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. /**
  233. * gpio_to_pin() - GPIO range GPIO number to pin number translation
  234. * @range: GPIO range used for the translation
  235. * @gpio: gpio pin to translate to a pin number
  236. *
  237. * Finds the pin number for a given GPIO using the specified GPIO range
  238. * as a base for translation. The distinction between linear GPIO ranges
  239. * and pin list based GPIO ranges is managed correctly by this function.
  240. *
  241. * This function assumes the gpio is part of the specified GPIO range, use
  242. * only after making sure this is the case (e.g. by calling it on the
  243. * result of successful pinctrl_get_device_gpio_range calls)!
  244. */
  245. static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
  246. unsigned int gpio)
  247. {
  248. unsigned int offset = gpio - range->base;
  249. if (range->pins)
  250. return range->pins[offset];
  251. else
  252. return range->pin_base + offset;
  253. }
  254. /**
  255. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  256. * @pctldev: pin controller device to check
  257. * @gpio: gpio pin to check taken from the global GPIO pin space
  258. *
  259. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  260. * controller, return the range or NULL
  261. */
  262. static struct pinctrl_gpio_range *
  263. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
  264. {
  265. struct pinctrl_gpio_range *range = NULL;
  266. mutex_lock(&pctldev->mutex);
  267. /* Loop over the ranges */
  268. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  269. /* Check if we're in the valid range */
  270. if (gpio >= range->base &&
  271. gpio < range->base + range->npins) {
  272. mutex_unlock(&pctldev->mutex);
  273. return range;
  274. }
  275. }
  276. mutex_unlock(&pctldev->mutex);
  277. return NULL;
  278. }
  279. /**
  280. * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
  281. * the same GPIO chip are in range
  282. * @gpio: gpio pin to check taken from the global GPIO pin space
  283. *
  284. * This function is complement of pinctrl_match_gpio_range(). If the return
  285. * value of pinctrl_match_gpio_range() is NULL, this function could be used
  286. * to check whether pinctrl device is ready or not. Maybe some GPIO pins
  287. * of the same GPIO chip don't have back-end pinctrl interface.
  288. * If the return value is true, it means that pinctrl device is ready & the
  289. * certain GPIO pin doesn't have back-end pinctrl device. If the return value
  290. * is false, it means that pinctrl device may not be ready.
  291. */
  292. #ifdef CONFIG_GPIOLIB
  293. static bool pinctrl_ready_for_gpio_range(unsigned gpio)
  294. {
  295. struct pinctrl_dev *pctldev;
  296. struct pinctrl_gpio_range *range = NULL;
  297. struct gpio_chip *chip = gpio_to_chip(gpio);
  298. mutex_lock(&pinctrldev_list_mutex);
  299. /* Loop over the pin controllers */
  300. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  301. /* Loop over the ranges */
  302. mutex_lock(&pctldev->mutex);
  303. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  304. /* Check if any gpio range overlapped with gpio chip */
  305. if (range->base + range->npins - 1 < chip->base ||
  306. range->base > chip->base + chip->ngpio - 1)
  307. continue;
  308. mutex_unlock(&pctldev->mutex);
  309. mutex_unlock(&pinctrldev_list_mutex);
  310. return true;
  311. }
  312. mutex_unlock(&pctldev->mutex);
  313. }
  314. mutex_unlock(&pinctrldev_list_mutex);
  315. return false;
  316. }
  317. #else
  318. static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
  319. #endif
  320. /**
  321. * pinctrl_get_device_gpio_range() - find device for GPIO range
  322. * @gpio: the pin to locate the pin controller for
  323. * @outdev: the pin control device if found
  324. * @outrange: the GPIO range if found
  325. *
  326. * Find the pin controller handling a certain GPIO pin from the pinspace of
  327. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  328. * -EPROBE_DEFER if the GPIO range could not be found in any device since it
  329. * may still have not been registered.
  330. */
  331. static int pinctrl_get_device_gpio_range(unsigned gpio,
  332. struct pinctrl_dev **outdev,
  333. struct pinctrl_gpio_range **outrange)
  334. {
  335. struct pinctrl_dev *pctldev = NULL;
  336. mutex_lock(&pinctrldev_list_mutex);
  337. /* Loop over the pin controllers */
  338. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  339. struct pinctrl_gpio_range *range;
  340. range = pinctrl_match_gpio_range(pctldev, gpio);
  341. if (range != NULL) {
  342. *outdev = pctldev;
  343. *outrange = range;
  344. mutex_unlock(&pinctrldev_list_mutex);
  345. return 0;
  346. }
  347. }
  348. mutex_unlock(&pinctrldev_list_mutex);
  349. return -EPROBE_DEFER;
  350. }
  351. /**
  352. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  353. * @pctldev: pin controller device to add the range to
  354. * @range: the GPIO range to add
  355. *
  356. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  357. * this to register handled ranges after registering your pin controller.
  358. */
  359. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  360. struct pinctrl_gpio_range *range)
  361. {
  362. mutex_lock(&pctldev->mutex);
  363. list_add_tail(&range->node, &pctldev->gpio_ranges);
  364. mutex_unlock(&pctldev->mutex);
  365. }
  366. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  367. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  368. struct pinctrl_gpio_range *ranges,
  369. unsigned nranges)
  370. {
  371. int i;
  372. for (i = 0; i < nranges; i++)
  373. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  374. }
  375. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  376. struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
  377. struct pinctrl_gpio_range *range)
  378. {
  379. struct pinctrl_dev *pctldev;
  380. pctldev = get_pinctrl_dev_from_devname(devname);
  381. /*
  382. * If we can't find this device, let's assume that is because
  383. * it has not probed yet, so the driver trying to register this
  384. * range need to defer probing.
  385. */
  386. if (!pctldev) {
  387. return ERR_PTR(-EPROBE_DEFER);
  388. }
  389. pinctrl_add_gpio_range(pctldev, range);
  390. return pctldev;
  391. }
  392. EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
  393. /**
  394. * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
  395. * @pctldev: the pin controller device to look in
  396. * @pin: a controller-local number to find the range for
  397. */
  398. struct pinctrl_gpio_range *
  399. pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
  400. unsigned int pin)
  401. {
  402. struct pinctrl_gpio_range *range;
  403. mutex_lock(&pctldev->mutex);
  404. /* Loop over the ranges */
  405. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  406. /* Check if we're in the valid range */
  407. if (range->pins) {
  408. int a;
  409. for (a = 0; a < range->npins; a++) {
  410. if (range->pins[a] == pin)
  411. goto out;
  412. }
  413. } else if (pin >= range->pin_base &&
  414. pin < range->pin_base + range->npins)
  415. goto out;
  416. }
  417. range = NULL;
  418. out:
  419. mutex_unlock(&pctldev->mutex);
  420. return range;
  421. }
  422. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
  423. /**
  424. * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller
  425. * @pctldev: pin controller device to remove the range from
  426. * @range: the GPIO range to remove
  427. */
  428. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  429. struct pinctrl_gpio_range *range)
  430. {
  431. mutex_lock(&pctldev->mutex);
  432. list_del(&range->node);
  433. mutex_unlock(&pctldev->mutex);
  434. }
  435. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  436. /**
  437. * pinctrl_get_group_selector() - returns the group selector for a group
  438. * @pctldev: the pin controller handling the group
  439. * @pin_group: the pin group to look up
  440. */
  441. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  442. const char *pin_group)
  443. {
  444. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  445. unsigned ngroups = pctlops->get_groups_count(pctldev);
  446. unsigned group_selector = 0;
  447. while (group_selector < ngroups) {
  448. const char *gname = pctlops->get_group_name(pctldev,
  449. group_selector);
  450. if (!strcmp(gname, pin_group)) {
  451. dev_dbg(pctldev->dev,
  452. "found group selector %u for %s\n",
  453. group_selector,
  454. pin_group);
  455. return group_selector;
  456. }
  457. group_selector++;
  458. }
  459. dev_err(pctldev->dev, "does not have pin group %s\n",
  460. pin_group);
  461. return -EINVAL;
  462. }
  463. /**
  464. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  465. * @gpio: the GPIO pin number from the GPIO subsystem number space
  466. *
  467. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  468. * as part of their gpio_request() semantics, platforms and individual drivers
  469. * shall *NOT* request GPIO pins to be muxed in.
  470. */
  471. int pinctrl_request_gpio(unsigned gpio)
  472. {
  473. struct pinctrl_dev *pctldev;
  474. struct pinctrl_gpio_range *range;
  475. int ret;
  476. int pin;
  477. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  478. if (ret) {
  479. if (pinctrl_ready_for_gpio_range(gpio))
  480. ret = 0;
  481. return ret;
  482. }
  483. mutex_lock(&pctldev->mutex);
  484. /* Convert to the pin controllers number space */
  485. pin = gpio_to_pin(range, gpio);
  486. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  487. mutex_unlock(&pctldev->mutex);
  488. return ret;
  489. }
  490. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  491. /**
  492. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  493. * @gpio: the GPIO pin number from the GPIO subsystem number space
  494. *
  495. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  496. * as part of their gpio_free() semantics, platforms and individual drivers
  497. * shall *NOT* request GPIO pins to be muxed out.
  498. */
  499. void pinctrl_free_gpio(unsigned gpio)
  500. {
  501. struct pinctrl_dev *pctldev;
  502. struct pinctrl_gpio_range *range;
  503. int ret;
  504. int pin;
  505. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  506. if (ret) {
  507. return;
  508. }
  509. mutex_lock(&pctldev->mutex);
  510. /* Convert to the pin controllers number space */
  511. pin = gpio_to_pin(range, gpio);
  512. pinmux_free_gpio(pctldev, pin, range);
  513. mutex_unlock(&pctldev->mutex);
  514. }
  515. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  516. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  517. {
  518. struct pinctrl_dev *pctldev;
  519. struct pinctrl_gpio_range *range;
  520. int ret;
  521. int pin;
  522. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  523. if (ret) {
  524. return ret;
  525. }
  526. mutex_lock(&pctldev->mutex);
  527. /* Convert to the pin controllers number space */
  528. pin = gpio_to_pin(range, gpio);
  529. ret = pinmux_gpio_direction(pctldev, range, pin, input);
  530. mutex_unlock(&pctldev->mutex);
  531. return ret;
  532. }
  533. /**
  534. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  535. * @gpio: the GPIO pin number from the GPIO subsystem number space
  536. *
  537. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  538. * as part of their gpio_direction_input() semantics, platforms and individual
  539. * drivers shall *NOT* touch pin control GPIO calls.
  540. */
  541. int pinctrl_gpio_direction_input(unsigned gpio)
  542. {
  543. return pinctrl_gpio_direction(gpio, true);
  544. }
  545. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  546. /**
  547. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  548. * @gpio: the GPIO pin number from the GPIO subsystem number space
  549. *
  550. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  551. * as part of their gpio_direction_output() semantics, platforms and individual
  552. * drivers shall *NOT* touch pin control GPIO calls.
  553. */
  554. int pinctrl_gpio_direction_output(unsigned gpio)
  555. {
  556. return pinctrl_gpio_direction(gpio, false);
  557. }
  558. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  559. static struct pinctrl_state *find_state(struct pinctrl *p,
  560. const char *name)
  561. {
  562. struct pinctrl_state *state;
  563. list_for_each_entry(state, &p->states, node)
  564. if (!strcmp(state->name, name))
  565. return state;
  566. return NULL;
  567. }
  568. static struct pinctrl_state *create_state(struct pinctrl *p,
  569. const char *name)
  570. {
  571. struct pinctrl_state *state;
  572. state = kzalloc(sizeof(*state), GFP_KERNEL);
  573. if (state == NULL) {
  574. dev_err(p->dev,
  575. "failed to alloc struct pinctrl_state\n");
  576. return ERR_PTR(-ENOMEM);
  577. }
  578. state->name = name;
  579. INIT_LIST_HEAD(&state->settings);
  580. list_add_tail(&state->node, &p->states);
  581. return state;
  582. }
  583. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
  584. {
  585. struct pinctrl_state *state;
  586. struct pinctrl_setting *setting;
  587. int ret;
  588. state = find_state(p, map->name);
  589. if (!state)
  590. state = create_state(p, map->name);
  591. if (IS_ERR(state))
  592. return PTR_ERR(state);
  593. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  594. return 0;
  595. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  596. if (setting == NULL) {
  597. dev_err(p->dev,
  598. "failed to alloc struct pinctrl_setting\n");
  599. return -ENOMEM;
  600. }
  601. setting->type = map->type;
  602. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  603. if (setting->pctldev == NULL) {
  604. kfree(setting);
  605. /* Do not defer probing of hogs (circular loop) */
  606. if (!strcmp(map->ctrl_dev_name, map->dev_name))
  607. return -ENODEV;
  608. /*
  609. * OK let us guess that the driver is not there yet, and
  610. * let's defer obtaining this pinctrl handle to later...
  611. */
  612. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  613. map->ctrl_dev_name);
  614. return -EPROBE_DEFER;
  615. }
  616. setting->dev_name = map->dev_name;
  617. switch (map->type) {
  618. case PIN_MAP_TYPE_MUX_GROUP:
  619. ret = pinmux_map_to_setting(map, setting);
  620. break;
  621. case PIN_MAP_TYPE_CONFIGS_PIN:
  622. case PIN_MAP_TYPE_CONFIGS_GROUP:
  623. ret = pinconf_map_to_setting(map, setting);
  624. break;
  625. default:
  626. ret = -EINVAL;
  627. break;
  628. }
  629. if (ret < 0) {
  630. kfree(setting);
  631. return ret;
  632. }
  633. list_add_tail(&setting->node, &state->settings);
  634. return 0;
  635. }
  636. static struct pinctrl *find_pinctrl(struct device *dev)
  637. {
  638. struct pinctrl *p;
  639. mutex_lock(&pinctrl_list_mutex);
  640. list_for_each_entry(p, &pinctrl_list, node)
  641. if (p->dev == dev) {
  642. mutex_unlock(&pinctrl_list_mutex);
  643. return p;
  644. }
  645. mutex_unlock(&pinctrl_list_mutex);
  646. return NULL;
  647. }
  648. static void pinctrl_free(struct pinctrl *p, bool inlist);
  649. static struct pinctrl *create_pinctrl(struct device *dev)
  650. {
  651. struct pinctrl *p;
  652. const char *devname;
  653. struct pinctrl_maps *maps_node;
  654. int i;
  655. struct pinctrl_map const *map;
  656. int ret;
  657. /*
  658. * create the state cookie holder struct pinctrl for each
  659. * mapping, this is what consumers will get when requesting
  660. * a pin control handle with pinctrl_get()
  661. */
  662. p = kzalloc(sizeof(*p), GFP_KERNEL);
  663. if (p == NULL) {
  664. dev_err(dev, "failed to alloc struct pinctrl\n");
  665. return ERR_PTR(-ENOMEM);
  666. }
  667. p->dev = dev;
  668. INIT_LIST_HEAD(&p->states);
  669. INIT_LIST_HEAD(&p->dt_maps);
  670. ret = pinctrl_dt_to_map(p);
  671. if (ret < 0) {
  672. kfree(p);
  673. return ERR_PTR(ret);
  674. }
  675. devname = dev_name(dev);
  676. mutex_lock(&pinctrl_maps_mutex);
  677. /* Iterate over the pin control maps to locate the right ones */
  678. for_each_maps(maps_node, i, map) {
  679. /* Map must be for this device */
  680. if (strcmp(map->dev_name, devname))
  681. continue;
  682. ret = add_setting(p, map);
  683. /*
  684. * At this point the adding of a setting may:
  685. *
  686. * - Defer, if the pinctrl device is not yet available
  687. * - Fail, if the pinctrl device is not yet available,
  688. * AND the setting is a hog. We cannot defer that, since
  689. * the hog will kick in immediately after the device
  690. * is registered.
  691. *
  692. * If the error returned was not -EPROBE_DEFER then we
  693. * accumulate the errors to see if we end up with
  694. * an -EPROBE_DEFER later, as that is the worst case.
  695. */
  696. if (ret == -EPROBE_DEFER) {
  697. pinctrl_free(p, false);
  698. mutex_unlock(&pinctrl_maps_mutex);
  699. return ERR_PTR(ret);
  700. }
  701. }
  702. mutex_unlock(&pinctrl_maps_mutex);
  703. if (ret < 0) {
  704. /* If some other error than deferral occured, return here */
  705. pinctrl_free(p, false);
  706. return ERR_PTR(ret);
  707. }
  708. kref_init(&p->users);
  709. /* Add the pinctrl handle to the global list */
  710. list_add_tail(&p->node, &pinctrl_list);
  711. return p;
  712. }
  713. /**
  714. * pinctrl_get() - retrieves the pinctrl handle for a device
  715. * @dev: the device to obtain the handle for
  716. */
  717. struct pinctrl *pinctrl_get(struct device *dev)
  718. {
  719. struct pinctrl *p;
  720. if (WARN_ON(!dev))
  721. return ERR_PTR(-EINVAL);
  722. /*
  723. * See if somebody else (such as the device core) has already
  724. * obtained a handle to the pinctrl for this device. In that case,
  725. * return another pointer to it.
  726. */
  727. p = find_pinctrl(dev);
  728. if (p != NULL) {
  729. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
  730. kref_get(&p->users);
  731. return p;
  732. }
  733. return create_pinctrl(dev);
  734. }
  735. EXPORT_SYMBOL_GPL(pinctrl_get);
  736. static void pinctrl_free_setting(bool disable_setting,
  737. struct pinctrl_setting *setting)
  738. {
  739. switch (setting->type) {
  740. case PIN_MAP_TYPE_MUX_GROUP:
  741. if (disable_setting)
  742. pinmux_disable_setting(setting);
  743. pinmux_free_setting(setting);
  744. break;
  745. case PIN_MAP_TYPE_CONFIGS_PIN:
  746. case PIN_MAP_TYPE_CONFIGS_GROUP:
  747. pinconf_free_setting(setting);
  748. break;
  749. default:
  750. break;
  751. }
  752. }
  753. static void pinctrl_free(struct pinctrl *p, bool inlist)
  754. {
  755. struct pinctrl_state *state, *n1;
  756. struct pinctrl_setting *setting, *n2;
  757. mutex_lock(&pinctrl_list_mutex);
  758. list_for_each_entry_safe(state, n1, &p->states, node) {
  759. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  760. pinctrl_free_setting(state == p->state, setting);
  761. list_del(&setting->node);
  762. kfree(setting);
  763. }
  764. list_del(&state->node);
  765. kfree(state);
  766. }
  767. pinctrl_dt_free_maps(p);
  768. if (inlist)
  769. list_del(&p->node);
  770. kfree(p);
  771. mutex_unlock(&pinctrl_list_mutex);
  772. }
  773. /**
  774. * pinctrl_release() - release the pinctrl handle
  775. * @kref: the kref in the pinctrl being released
  776. */
  777. static void pinctrl_release(struct kref *kref)
  778. {
  779. struct pinctrl *p = container_of(kref, struct pinctrl, users);
  780. pinctrl_free(p, true);
  781. }
  782. /**
  783. * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
  784. * @p: the pinctrl handle to release
  785. */
  786. void pinctrl_put(struct pinctrl *p)
  787. {
  788. kref_put(&p->users, pinctrl_release);
  789. }
  790. EXPORT_SYMBOL_GPL(pinctrl_put);
  791. /**
  792. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  793. * @p: the pinctrl handle to retrieve the state from
  794. * @name: the state name to retrieve
  795. */
  796. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
  797. const char *name)
  798. {
  799. struct pinctrl_state *state;
  800. state = find_state(p, name);
  801. if (!state) {
  802. if (pinctrl_dummy_state) {
  803. /* create dummy state */
  804. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  805. name);
  806. state = create_state(p, name);
  807. } else
  808. state = ERR_PTR(-ENODEV);
  809. }
  810. return state;
  811. }
  812. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  813. /**
  814. * pinctrl_select_state() - select/activate/program a pinctrl state to HW
  815. * @p: the pinctrl handle for the device that requests configuration
  816. * @state: the state handle to select/activate/program
  817. */
  818. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  819. {
  820. struct pinctrl_setting *setting, *setting2;
  821. struct pinctrl_state *old_state = p->state;
  822. int ret;
  823. if (p->state == state)
  824. return 0;
  825. if (p->state) {
  826. /*
  827. * The set of groups with a mux configuration in the old state
  828. * may not be identical to the set of groups with a mux setting
  829. * in the new state. While this might be unusual, it's entirely
  830. * possible for the "user"-supplied mapping table to be written
  831. * that way. For each group that was configured in the old state
  832. * but not in the new state, this code puts that group into a
  833. * safe/disabled state.
  834. */
  835. list_for_each_entry(setting, &p->state->settings, node) {
  836. bool found = false;
  837. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  838. continue;
  839. list_for_each_entry(setting2, &state->settings, node) {
  840. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  841. continue;
  842. if (setting2->data.mux.group ==
  843. setting->data.mux.group) {
  844. found = true;
  845. break;
  846. }
  847. }
  848. if (!found)
  849. pinmux_disable_setting(setting);
  850. }
  851. }
  852. p->state = NULL;
  853. /* Apply all the settings for the new state */
  854. list_for_each_entry(setting, &state->settings, node) {
  855. switch (setting->type) {
  856. case PIN_MAP_TYPE_MUX_GROUP:
  857. ret = pinmux_enable_setting(setting);
  858. break;
  859. case PIN_MAP_TYPE_CONFIGS_PIN:
  860. case PIN_MAP_TYPE_CONFIGS_GROUP:
  861. ret = pinconf_apply_setting(setting);
  862. break;
  863. default:
  864. ret = -EINVAL;
  865. break;
  866. }
  867. if (ret < 0) {
  868. goto unapply_new_state;
  869. }
  870. }
  871. p->state = state;
  872. return 0;
  873. unapply_new_state:
  874. dev_err(p->dev, "Error applying setting, reverse things back\n");
  875. list_for_each_entry(setting2, &state->settings, node) {
  876. if (&setting2->node == &setting->node)
  877. break;
  878. /*
  879. * All we can do here is pinmux_disable_setting.
  880. * That means that some pins are muxed differently now
  881. * than they were before applying the setting (We can't
  882. * "unmux a pin"!), but it's not a big deal since the pins
  883. * are free to be muxed by another apply_setting.
  884. */
  885. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
  886. pinmux_disable_setting(setting2);
  887. }
  888. /* There's no infinite recursive loop here because p->state is NULL */
  889. if (old_state)
  890. pinctrl_select_state(p, old_state);
  891. return ret;
  892. }
  893. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  894. static void devm_pinctrl_release(struct device *dev, void *res)
  895. {
  896. pinctrl_put(*(struct pinctrl **)res);
  897. }
  898. /**
  899. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  900. * @dev: the device to obtain the handle for
  901. *
  902. * If there is a need to explicitly destroy the returned struct pinctrl,
  903. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  904. */
  905. struct pinctrl *devm_pinctrl_get(struct device *dev)
  906. {
  907. struct pinctrl **ptr, *p;
  908. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  909. if (!ptr)
  910. return ERR_PTR(-ENOMEM);
  911. p = pinctrl_get(dev);
  912. if (!IS_ERR(p)) {
  913. *ptr = p;
  914. devres_add(dev, ptr);
  915. } else {
  916. devres_free(ptr);
  917. }
  918. return p;
  919. }
  920. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  921. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  922. {
  923. struct pinctrl **p = res;
  924. return *p == data;
  925. }
  926. /**
  927. * devm_pinctrl_put() - Resource managed pinctrl_put()
  928. * @p: the pinctrl handle to release
  929. *
  930. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  931. * this function will not need to be called and the resource management
  932. * code will ensure that the resource is freed.
  933. */
  934. void devm_pinctrl_put(struct pinctrl *p)
  935. {
  936. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  937. devm_pinctrl_match, p));
  938. }
  939. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  940. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  941. bool dup, bool locked)
  942. {
  943. int i, ret;
  944. struct pinctrl_maps *maps_node;
  945. pr_debug("add %d pinmux maps\n", num_maps);
  946. /* First sanity check the new mapping */
  947. for (i = 0; i < num_maps; i++) {
  948. if (!maps[i].dev_name) {
  949. pr_err("failed to register map %s (%d): no device given\n",
  950. maps[i].name, i);
  951. return -EINVAL;
  952. }
  953. if (!maps[i].name) {
  954. pr_err("failed to register map %d: no map name given\n",
  955. i);
  956. return -EINVAL;
  957. }
  958. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  959. !maps[i].ctrl_dev_name) {
  960. pr_err("failed to register map %s (%d): no pin control device given\n",
  961. maps[i].name, i);
  962. return -EINVAL;
  963. }
  964. switch (maps[i].type) {
  965. case PIN_MAP_TYPE_DUMMY_STATE:
  966. break;
  967. case PIN_MAP_TYPE_MUX_GROUP:
  968. ret = pinmux_validate_map(&maps[i], i);
  969. if (ret < 0)
  970. return ret;
  971. break;
  972. case PIN_MAP_TYPE_CONFIGS_PIN:
  973. case PIN_MAP_TYPE_CONFIGS_GROUP:
  974. ret = pinconf_validate_map(&maps[i], i);
  975. if (ret < 0)
  976. return ret;
  977. break;
  978. default:
  979. pr_err("failed to register map %s (%d): invalid type given\n",
  980. maps[i].name, i);
  981. return -EINVAL;
  982. }
  983. }
  984. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  985. if (!maps_node) {
  986. pr_err("failed to alloc struct pinctrl_maps\n");
  987. return -ENOMEM;
  988. }
  989. maps_node->num_maps = num_maps;
  990. if (dup) {
  991. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  992. GFP_KERNEL);
  993. if (!maps_node->maps) {
  994. pr_err("failed to duplicate mapping table\n");
  995. kfree(maps_node);
  996. return -ENOMEM;
  997. }
  998. } else {
  999. maps_node->maps = maps;
  1000. }
  1001. if (!locked)
  1002. mutex_lock(&pinctrl_maps_mutex);
  1003. list_add_tail(&maps_node->node, &pinctrl_maps);
  1004. if (!locked)
  1005. mutex_unlock(&pinctrl_maps_mutex);
  1006. return 0;
  1007. }
  1008. /**
  1009. * pinctrl_register_mappings() - register a set of pin controller mappings
  1010. * @maps: the pincontrol mappings table to register. This should probably be
  1011. * marked with __initdata so it can be discarded after boot. This
  1012. * function will perform a shallow copy for the mapping entries.
  1013. * @num_maps: the number of maps in the mapping table
  1014. */
  1015. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  1016. unsigned num_maps)
  1017. {
  1018. return pinctrl_register_map(maps, num_maps, true, false);
  1019. }
  1020. void pinctrl_unregister_map(struct pinctrl_map const *map)
  1021. {
  1022. struct pinctrl_maps *maps_node;
  1023. mutex_lock(&pinctrl_maps_mutex);
  1024. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  1025. if (maps_node->maps == map) {
  1026. list_del(&maps_node->node);
  1027. kfree(maps_node);
  1028. mutex_unlock(&pinctrl_maps_mutex);
  1029. return;
  1030. }
  1031. }
  1032. mutex_unlock(&pinctrl_maps_mutex);
  1033. }
  1034. /**
  1035. * pinctrl_force_sleep() - turn a given controller device into sleep state
  1036. * @pctldev: pin controller device
  1037. */
  1038. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  1039. {
  1040. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1041. return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1045. /**
  1046. * pinctrl_force_default() - turn a given controller device into default state
  1047. * @pctldev: pin controller device
  1048. */
  1049. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1050. {
  1051. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1052. return pinctrl_select_state(pctldev->p, pctldev->hog_default);
  1053. return 0;
  1054. }
  1055. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1056. #ifdef CONFIG_PM
  1057. /**
  1058. * pinctrl_pm_select_state() - select pinctrl state for PM
  1059. * @dev: device to select default state for
  1060. * @state: state to set
  1061. */
  1062. static int pinctrl_pm_select_state(struct device *dev,
  1063. struct pinctrl_state *state)
  1064. {
  1065. struct dev_pin_info *pins = dev->pins;
  1066. int ret;
  1067. if (IS_ERR(state))
  1068. return 0; /* No such state */
  1069. ret = pinctrl_select_state(pins->p, state);
  1070. if (ret)
  1071. dev_err(dev, "failed to activate pinctrl state %s\n",
  1072. state->name);
  1073. return ret;
  1074. }
  1075. /**
  1076. * pinctrl_pm_select_default_state() - select default pinctrl state for PM
  1077. * @dev: device to select default state for
  1078. */
  1079. int pinctrl_pm_select_default_state(struct device *dev)
  1080. {
  1081. if (!dev->pins)
  1082. return 0;
  1083. return pinctrl_pm_select_state(dev, dev->pins->default_state);
  1084. }
  1085. EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
  1086. /**
  1087. * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
  1088. * @dev: device to select sleep state for
  1089. */
  1090. int pinctrl_pm_select_sleep_state(struct device *dev)
  1091. {
  1092. if (!dev->pins)
  1093. return 0;
  1094. return pinctrl_pm_select_state(dev, dev->pins->sleep_state);
  1095. }
  1096. EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
  1097. /**
  1098. * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
  1099. * @dev: device to select idle state for
  1100. */
  1101. int pinctrl_pm_select_idle_state(struct device *dev)
  1102. {
  1103. if (!dev->pins)
  1104. return 0;
  1105. return pinctrl_pm_select_state(dev, dev->pins->idle_state);
  1106. }
  1107. EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
  1108. #endif
  1109. #ifdef CONFIG_DEBUG_FS
  1110. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1111. {
  1112. struct pinctrl_dev *pctldev = s->private;
  1113. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1114. unsigned i, pin;
  1115. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1116. mutex_lock(&pctldev->mutex);
  1117. /* The pin number can be retrived from the pin controller descriptor */
  1118. for (i = 0; i < pctldev->desc->npins; i++) {
  1119. struct pin_desc *desc;
  1120. pin = pctldev->desc->pins[i].number;
  1121. desc = pin_desc_get(pctldev, pin);
  1122. /* Pin space may be sparse */
  1123. if (desc == NULL)
  1124. continue;
  1125. seq_printf(s, "pin %d (%s) ", pin,
  1126. desc->name ? desc->name : "unnamed");
  1127. /* Driver-specific info per pin */
  1128. if (ops->pin_dbg_show)
  1129. ops->pin_dbg_show(pctldev, s, pin);
  1130. seq_puts(s, "\n");
  1131. }
  1132. mutex_unlock(&pctldev->mutex);
  1133. return 0;
  1134. }
  1135. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1136. {
  1137. struct pinctrl_dev *pctldev = s->private;
  1138. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1139. unsigned ngroups, selector = 0;
  1140. mutex_lock(&pctldev->mutex);
  1141. ngroups = ops->get_groups_count(pctldev);
  1142. seq_puts(s, "registered pin groups:\n");
  1143. while (selector < ngroups) {
  1144. const unsigned *pins;
  1145. unsigned num_pins;
  1146. const char *gname = ops->get_group_name(pctldev, selector);
  1147. const char *pname;
  1148. int ret;
  1149. int i;
  1150. ret = ops->get_group_pins(pctldev, selector,
  1151. &pins, &num_pins);
  1152. if (ret)
  1153. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1154. gname);
  1155. else {
  1156. seq_printf(s, "group: %s\n", gname);
  1157. for (i = 0; i < num_pins; i++) {
  1158. pname = pin_get_name(pctldev, pins[i]);
  1159. if (WARN_ON(!pname)) {
  1160. mutex_unlock(&pctldev->mutex);
  1161. return -EINVAL;
  1162. }
  1163. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1164. }
  1165. seq_puts(s, "\n");
  1166. }
  1167. selector++;
  1168. }
  1169. mutex_unlock(&pctldev->mutex);
  1170. return 0;
  1171. }
  1172. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1173. {
  1174. struct pinctrl_dev *pctldev = s->private;
  1175. struct pinctrl_gpio_range *range = NULL;
  1176. seq_puts(s, "GPIO ranges handled:\n");
  1177. mutex_lock(&pctldev->mutex);
  1178. /* Loop over the ranges */
  1179. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1180. if (range->pins) {
  1181. int a;
  1182. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
  1183. range->id, range->name,
  1184. range->base, (range->base + range->npins - 1));
  1185. for (a = 0; a < range->npins - 1; a++)
  1186. seq_printf(s, "%u, ", range->pins[a]);
  1187. seq_printf(s, "%u}\n", range->pins[a]);
  1188. }
  1189. else
  1190. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1191. range->id, range->name,
  1192. range->base, (range->base + range->npins - 1),
  1193. range->pin_base,
  1194. (range->pin_base + range->npins - 1));
  1195. }
  1196. mutex_unlock(&pctldev->mutex);
  1197. return 0;
  1198. }
  1199. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1200. {
  1201. struct pinctrl_dev *pctldev;
  1202. seq_puts(s, "name [pinmux] [pinconf]\n");
  1203. mutex_lock(&pinctrldev_list_mutex);
  1204. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1205. seq_printf(s, "%s ", pctldev->desc->name);
  1206. if (pctldev->desc->pmxops)
  1207. seq_puts(s, "yes ");
  1208. else
  1209. seq_puts(s, "no ");
  1210. if (pctldev->desc->confops)
  1211. seq_puts(s, "yes");
  1212. else
  1213. seq_puts(s, "no");
  1214. seq_puts(s, "\n");
  1215. }
  1216. mutex_unlock(&pinctrldev_list_mutex);
  1217. return 0;
  1218. }
  1219. static inline const char *map_type(enum pinctrl_map_type type)
  1220. {
  1221. static const char * const names[] = {
  1222. "INVALID",
  1223. "DUMMY_STATE",
  1224. "MUX_GROUP",
  1225. "CONFIGS_PIN",
  1226. "CONFIGS_GROUP",
  1227. };
  1228. if (type >= ARRAY_SIZE(names))
  1229. return "UNKNOWN";
  1230. return names[type];
  1231. }
  1232. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1233. {
  1234. struct pinctrl_maps *maps_node;
  1235. int i;
  1236. struct pinctrl_map const *map;
  1237. seq_puts(s, "Pinctrl maps:\n");
  1238. mutex_lock(&pinctrl_maps_mutex);
  1239. for_each_maps(maps_node, i, map) {
  1240. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1241. map->dev_name, map->name, map_type(map->type),
  1242. map->type);
  1243. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1244. seq_printf(s, "controlling device %s\n",
  1245. map->ctrl_dev_name);
  1246. switch (map->type) {
  1247. case PIN_MAP_TYPE_MUX_GROUP:
  1248. pinmux_show_map(s, map);
  1249. break;
  1250. case PIN_MAP_TYPE_CONFIGS_PIN:
  1251. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1252. pinconf_show_map(s, map);
  1253. break;
  1254. default:
  1255. break;
  1256. }
  1257. seq_printf(s, "\n");
  1258. }
  1259. mutex_unlock(&pinctrl_maps_mutex);
  1260. return 0;
  1261. }
  1262. static int pinctrl_show(struct seq_file *s, void *what)
  1263. {
  1264. struct pinctrl *p;
  1265. struct pinctrl_state *state;
  1266. struct pinctrl_setting *setting;
  1267. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1268. mutex_lock(&pinctrl_list_mutex);
  1269. list_for_each_entry(p, &pinctrl_list, node) {
  1270. seq_printf(s, "device: %s current state: %s\n",
  1271. dev_name(p->dev),
  1272. p->state ? p->state->name : "none");
  1273. list_for_each_entry(state, &p->states, node) {
  1274. seq_printf(s, " state: %s\n", state->name);
  1275. list_for_each_entry(setting, &state->settings, node) {
  1276. struct pinctrl_dev *pctldev = setting->pctldev;
  1277. seq_printf(s, " type: %s controller %s ",
  1278. map_type(setting->type),
  1279. pinctrl_dev_get_name(pctldev));
  1280. switch (setting->type) {
  1281. case PIN_MAP_TYPE_MUX_GROUP:
  1282. pinmux_show_setting(s, setting);
  1283. break;
  1284. case PIN_MAP_TYPE_CONFIGS_PIN:
  1285. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1286. pinconf_show_setting(s, setting);
  1287. break;
  1288. default:
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. mutex_unlock(&pinctrl_list_mutex);
  1295. return 0;
  1296. }
  1297. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1298. {
  1299. return single_open(file, pinctrl_pins_show, inode->i_private);
  1300. }
  1301. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1302. {
  1303. return single_open(file, pinctrl_groups_show, inode->i_private);
  1304. }
  1305. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1306. {
  1307. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1308. }
  1309. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1310. {
  1311. return single_open(file, pinctrl_devices_show, NULL);
  1312. }
  1313. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1314. {
  1315. return single_open(file, pinctrl_maps_show, NULL);
  1316. }
  1317. static int pinctrl_open(struct inode *inode, struct file *file)
  1318. {
  1319. return single_open(file, pinctrl_show, NULL);
  1320. }
  1321. static const struct file_operations pinctrl_pins_ops = {
  1322. .open = pinctrl_pins_open,
  1323. .read = seq_read,
  1324. .llseek = seq_lseek,
  1325. .release = single_release,
  1326. };
  1327. static const struct file_operations pinctrl_groups_ops = {
  1328. .open = pinctrl_groups_open,
  1329. .read = seq_read,
  1330. .llseek = seq_lseek,
  1331. .release = single_release,
  1332. };
  1333. static const struct file_operations pinctrl_gpioranges_ops = {
  1334. .open = pinctrl_gpioranges_open,
  1335. .read = seq_read,
  1336. .llseek = seq_lseek,
  1337. .release = single_release,
  1338. };
  1339. static const struct file_operations pinctrl_devices_ops = {
  1340. .open = pinctrl_devices_open,
  1341. .read = seq_read,
  1342. .llseek = seq_lseek,
  1343. .release = single_release,
  1344. };
  1345. static const struct file_operations pinctrl_maps_ops = {
  1346. .open = pinctrl_maps_open,
  1347. .read = seq_read,
  1348. .llseek = seq_lseek,
  1349. .release = single_release,
  1350. };
  1351. static const struct file_operations pinctrl_ops = {
  1352. .open = pinctrl_open,
  1353. .read = seq_read,
  1354. .llseek = seq_lseek,
  1355. .release = single_release,
  1356. };
  1357. static struct dentry *debugfs_root;
  1358. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1359. {
  1360. struct dentry *device_root;
  1361. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1362. debugfs_root);
  1363. pctldev->device_root = device_root;
  1364. if (IS_ERR(device_root) || !device_root) {
  1365. pr_warn("failed to create debugfs directory for %s\n",
  1366. dev_name(pctldev->dev));
  1367. return;
  1368. }
  1369. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1370. device_root, pctldev, &pinctrl_pins_ops);
  1371. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1372. device_root, pctldev, &pinctrl_groups_ops);
  1373. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1374. device_root, pctldev, &pinctrl_gpioranges_ops);
  1375. pinmux_init_device_debugfs(device_root, pctldev);
  1376. pinconf_init_device_debugfs(device_root, pctldev);
  1377. }
  1378. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1379. {
  1380. debugfs_remove_recursive(pctldev->device_root);
  1381. }
  1382. static void pinctrl_init_debugfs(void)
  1383. {
  1384. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1385. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1386. pr_warn("failed to create debugfs directory\n");
  1387. debugfs_root = NULL;
  1388. return;
  1389. }
  1390. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1391. debugfs_root, NULL, &pinctrl_devices_ops);
  1392. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1393. debugfs_root, NULL, &pinctrl_maps_ops);
  1394. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1395. debugfs_root, NULL, &pinctrl_ops);
  1396. }
  1397. #else /* CONFIG_DEBUG_FS */
  1398. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1399. {
  1400. }
  1401. static void pinctrl_init_debugfs(void)
  1402. {
  1403. }
  1404. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1405. {
  1406. }
  1407. #endif
  1408. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1409. {
  1410. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1411. if (!ops ||
  1412. !ops->get_groups_count ||
  1413. !ops->get_group_name ||
  1414. !ops->get_group_pins)
  1415. return -EINVAL;
  1416. if (ops->dt_node_to_map && !ops->dt_free_map)
  1417. return -EINVAL;
  1418. return 0;
  1419. }
  1420. /**
  1421. * pinctrl_register() - register a pin controller device
  1422. * @pctldesc: descriptor for this pin controller
  1423. * @dev: parent device for this pin controller
  1424. * @driver_data: private pin controller data for this pin controller
  1425. */
  1426. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1427. struct device *dev, void *driver_data)
  1428. {
  1429. struct pinctrl_dev *pctldev;
  1430. int ret;
  1431. if (!pctldesc)
  1432. return NULL;
  1433. if (!pctldesc->name)
  1434. return NULL;
  1435. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1436. if (pctldev == NULL) {
  1437. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1438. return NULL;
  1439. }
  1440. /* Initialize pin control device struct */
  1441. pctldev->owner = pctldesc->owner;
  1442. pctldev->desc = pctldesc;
  1443. pctldev->driver_data = driver_data;
  1444. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1445. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1446. pctldev->dev = dev;
  1447. mutex_init(&pctldev->mutex);
  1448. /* check core ops for sanity */
  1449. if (pinctrl_check_ops(pctldev)) {
  1450. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1451. goto out_err;
  1452. }
  1453. /* If we're implementing pinmuxing, check the ops for sanity */
  1454. if (pctldesc->pmxops) {
  1455. if (pinmux_check_ops(pctldev))
  1456. goto out_err;
  1457. }
  1458. /* If we're implementing pinconfig, check the ops for sanity */
  1459. if (pctldesc->confops) {
  1460. if (pinconf_check_ops(pctldev))
  1461. goto out_err;
  1462. }
  1463. /* Register all the pins */
  1464. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1465. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1466. if (ret) {
  1467. dev_err(dev, "error during pin registration\n");
  1468. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1469. pctldesc->npins);
  1470. goto out_err;
  1471. }
  1472. mutex_lock(&pinctrldev_list_mutex);
  1473. list_add_tail(&pctldev->node, &pinctrldev_list);
  1474. mutex_unlock(&pinctrldev_list_mutex);
  1475. pctldev->p = pinctrl_get(pctldev->dev);
  1476. if (!IS_ERR(pctldev->p)) {
  1477. pctldev->hog_default =
  1478. pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
  1479. if (IS_ERR(pctldev->hog_default)) {
  1480. dev_dbg(dev, "failed to lookup the default state\n");
  1481. } else {
  1482. if (pinctrl_select_state(pctldev->p,
  1483. pctldev->hog_default))
  1484. dev_err(dev,
  1485. "failed to select default state\n");
  1486. }
  1487. pctldev->hog_sleep =
  1488. pinctrl_lookup_state(pctldev->p,
  1489. PINCTRL_STATE_SLEEP);
  1490. if (IS_ERR(pctldev->hog_sleep))
  1491. dev_dbg(dev, "failed to lookup the sleep state\n");
  1492. }
  1493. pinctrl_init_device_debugfs(pctldev);
  1494. return pctldev;
  1495. out_err:
  1496. mutex_destroy(&pctldev->mutex);
  1497. kfree(pctldev);
  1498. return NULL;
  1499. }
  1500. EXPORT_SYMBOL_GPL(pinctrl_register);
  1501. /**
  1502. * pinctrl_unregister() - unregister pinmux
  1503. * @pctldev: pin controller to unregister
  1504. *
  1505. * Called by pinmux drivers to unregister a pinmux.
  1506. */
  1507. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1508. {
  1509. struct pinctrl_gpio_range *range, *n;
  1510. if (pctldev == NULL)
  1511. return;
  1512. mutex_lock(&pinctrldev_list_mutex);
  1513. mutex_lock(&pctldev->mutex);
  1514. pinctrl_remove_device_debugfs(pctldev);
  1515. if (!IS_ERR(pctldev->p))
  1516. pinctrl_put(pctldev->p);
  1517. /* TODO: check that no pinmuxes are still active? */
  1518. list_del(&pctldev->node);
  1519. /* Destroy descriptor tree */
  1520. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1521. pctldev->desc->npins);
  1522. /* remove gpio ranges map */
  1523. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1524. list_del(&range->node);
  1525. mutex_unlock(&pctldev->mutex);
  1526. mutex_destroy(&pctldev->mutex);
  1527. kfree(pctldev);
  1528. mutex_unlock(&pinctrldev_list_mutex);
  1529. }
  1530. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1531. static int __init pinctrl_init(void)
  1532. {
  1533. pr_info("initialized pinctrl subsystem\n");
  1534. pinctrl_init_debugfs();
  1535. return 0;
  1536. }
  1537. /* init early since many drivers really need to initialized pinmux early */
  1538. core_initcall(pinctrl_init);