core.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Core driver for the pin control subsystem
  4. *
  5. * Copyright (C) 2011-2012 ST-Ericsson SA
  6. * Written on behalf of Linaro for ST-Ericsson
  7. * Based on bits of regulator core, gpio core and clk core
  8. *
  9. * Author: Linus Walleij <linus.walleij@linaro.org>
  10. *
  11. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  12. */
  13. #define pr_fmt(fmt) "pinctrl core: " fmt
  14. #include <linux/array_size.h>
  15. #include <linux/cleanup.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/export.h>
  20. #include <linux/init.h>
  21. #include <linux/kref.h>
  22. #include <linux/list.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/gpio.h>
  26. #include <linux/gpio/driver.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/pinctrl/devinfo.h>
  29. #include <linux/pinctrl/machine.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include "core.h"
  32. #include "devicetree.h"
  33. #include "pinconf.h"
  34. #include "pinmux.h"
  35. static bool pinctrl_dummy_state;
  36. /* Mutex taken to protect pinctrl_list */
  37. static DEFINE_MUTEX(pinctrl_list_mutex);
  38. /* Mutex taken to protect pinctrl_maps */
  39. DEFINE_MUTEX(pinctrl_maps_mutex);
  40. /* Mutex taken to protect pinctrldev_list */
  41. static DEFINE_MUTEX(pinctrldev_list_mutex);
  42. /* Global list of pin control devices (struct pinctrl_dev) */
  43. static LIST_HEAD(pinctrldev_list);
  44. /* List of pin controller handles (struct pinctrl) */
  45. static LIST_HEAD(pinctrl_list);
  46. /* List of pinctrl maps (struct pinctrl_maps) */
  47. LIST_HEAD(pinctrl_maps);
  48. /**
  49. * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support
  50. *
  51. * Usually this function is called by platforms without pinctrl driver support
  52. * but run with some shared drivers using pinctrl APIs.
  53. * After calling this function, the pinctrl core will return successfully
  54. * with creating a dummy state for the driver to keep going smoothly.
  55. */
  56. void pinctrl_provide_dummies(void)
  57. {
  58. pinctrl_dummy_state = true;
  59. }
  60. const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
  61. {
  62. /* We're not allowed to register devices without name */
  63. return pctldev->desc->name;
  64. }
  65. EXPORT_SYMBOL_GPL(pinctrl_dev_get_name);
  66. const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev)
  67. {
  68. return dev_name(pctldev->dev);
  69. }
  70. EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname);
  71. void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev)
  72. {
  73. return pctldev->driver_data;
  74. }
  75. EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata);
  76. /**
  77. * get_pinctrl_dev_from_devname() - look up pin controller device
  78. * @devname: the name of a device instance, as returned by dev_name()
  79. *
  80. * Looks up a pin control device matching a certain device name or pure device
  81. * pointer, the pure device pointer will take precedence.
  82. */
  83. struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
  84. {
  85. struct pinctrl_dev *pctldev;
  86. if (!devname)
  87. return NULL;
  88. mutex_lock(&pinctrldev_list_mutex);
  89. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  90. if (!strcmp(dev_name(pctldev->dev), devname)) {
  91. /* Matched on device name */
  92. mutex_unlock(&pinctrldev_list_mutex);
  93. return pctldev;
  94. }
  95. }
  96. mutex_unlock(&pinctrldev_list_mutex);
  97. return NULL;
  98. }
  99. struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
  100. {
  101. struct pinctrl_dev *pctldev;
  102. mutex_lock(&pinctrldev_list_mutex);
  103. list_for_each_entry(pctldev, &pinctrldev_list, node)
  104. if (device_match_of_node(pctldev->dev, np)) {
  105. mutex_unlock(&pinctrldev_list_mutex);
  106. return pctldev;
  107. }
  108. mutex_unlock(&pinctrldev_list_mutex);
  109. return NULL;
  110. }
  111. /**
  112. * pin_get_from_name() - look up a pin number from a name
  113. * @pctldev: the pin control device to lookup the pin on
  114. * @name: the name of the pin to look up
  115. */
  116. int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name)
  117. {
  118. unsigned int i, pin;
  119. /* The pin number can be retrived from the pin controller descriptor */
  120. for (i = 0; i < pctldev->desc->npins; i++) {
  121. struct pin_desc *desc;
  122. pin = pctldev->desc->pins[i].number;
  123. desc = pin_desc_get(pctldev, pin);
  124. /* Pin space may be sparse */
  125. if (desc && !strcmp(name, desc->name))
  126. return pin;
  127. }
  128. return -EINVAL;
  129. }
  130. /**
  131. * pin_get_name() - look up a pin name from a pin id
  132. * @pctldev: the pin control device to lookup the pin on
  133. * @pin: pin number/id to look up
  134. */
  135. const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin)
  136. {
  137. const struct pin_desc *desc;
  138. desc = pin_desc_get(pctldev, pin);
  139. if (!desc) {
  140. dev_err(pctldev->dev, "failed to get pin(%d) name\n",
  141. pin);
  142. return NULL;
  143. }
  144. return desc->name;
  145. }
  146. EXPORT_SYMBOL_GPL(pin_get_name);
  147. /* Deletes a range of pin descriptors */
  148. static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev,
  149. const struct pinctrl_pin_desc *pins,
  150. unsigned int num_pins)
  151. {
  152. int i;
  153. for (i = 0; i < num_pins; i++) {
  154. struct pin_desc *pindesc;
  155. pindesc = radix_tree_lookup(&pctldev->pin_desc_tree,
  156. pins[i].number);
  157. if (pindesc) {
  158. radix_tree_delete(&pctldev->pin_desc_tree,
  159. pins[i].number);
  160. if (pindesc->dynamic_name)
  161. kfree(pindesc->name);
  162. }
  163. kfree(pindesc);
  164. }
  165. }
  166. static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
  167. const struct pinctrl_pin_desc *pin)
  168. {
  169. struct pin_desc *pindesc;
  170. int error;
  171. pindesc = pin_desc_get(pctldev, pin->number);
  172. if (pindesc) {
  173. dev_err(pctldev->dev, "pin %d already registered\n",
  174. pin->number);
  175. return -EINVAL;
  176. }
  177. pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
  178. if (!pindesc)
  179. return -ENOMEM;
  180. /* Set owner */
  181. pindesc->pctldev = pctldev;
  182. #ifdef CONFIG_PINMUX
  183. mutex_init(&pindesc->mux_lock);
  184. #endif
  185. /* Copy basic pin info */
  186. if (pin->name) {
  187. pindesc->name = pin->name;
  188. } else {
  189. pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
  190. if (!pindesc->name) {
  191. error = -ENOMEM;
  192. goto failed;
  193. }
  194. pindesc->dynamic_name = true;
  195. }
  196. pindesc->drv_data = pin->drv_data;
  197. error = radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
  198. if (error)
  199. goto failed;
  200. pr_debug("registered pin %d (%s) on %s\n",
  201. pin->number, pindesc->name, pctldev->desc->name);
  202. return 0;
  203. failed:
  204. kfree(pindesc);
  205. return error;
  206. }
  207. static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
  208. const struct pinctrl_pin_desc *pins,
  209. unsigned int num_descs)
  210. {
  211. unsigned int i;
  212. int ret = 0;
  213. for (i = 0; i < num_descs; i++) {
  214. ret = pinctrl_register_one_pin(pctldev, &pins[i]);
  215. if (ret)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * gpio_to_pin() - GPIO range GPIO number to pin number translation
  222. * @range: GPIO range used for the translation
  223. * @gc: GPIO chip structure from the GPIO subsystem
  224. * @offset: hardware offset of the GPIO relative to the controller
  225. *
  226. * Finds the pin number for a given GPIO using the specified GPIO range
  227. * as a base for translation. The distinction between linear GPIO ranges
  228. * and pin list based GPIO ranges is managed correctly by this function.
  229. *
  230. * This function assumes the gpio is part of the specified GPIO range, use
  231. * only after making sure this is the case (e.g. by calling it on the
  232. * result of successful pinctrl_get_device_gpio_range calls)!
  233. */
  234. static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
  235. struct gpio_chip *gc, unsigned int offset)
  236. {
  237. unsigned int pin = gc->base + offset - range->base;
  238. if (range->pins)
  239. return range->pins[pin];
  240. else
  241. return range->pin_base + pin;
  242. }
  243. /**
  244. * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
  245. * @pctldev: pin controller device to check
  246. * @gc: GPIO chip structure from the GPIO subsystem
  247. * @offset: hardware offset of the GPIO relative to the controller
  248. *
  249. * Tries to match a GPIO pin number to the ranges handled by a certain pin
  250. * controller, return the range or NULL
  251. */
  252. static struct pinctrl_gpio_range *
  253. pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc,
  254. unsigned int offset)
  255. {
  256. struct pinctrl_gpio_range *range;
  257. mutex_lock(&pctldev->mutex);
  258. /* Loop over the ranges */
  259. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  260. /* Check if we're in the valid range */
  261. if ((gc->base + offset) >= range->base &&
  262. (gc->base + offset) < range->base + range->npins) {
  263. mutex_unlock(&pctldev->mutex);
  264. return range;
  265. }
  266. }
  267. mutex_unlock(&pctldev->mutex);
  268. return NULL;
  269. }
  270. /**
  271. * pinctrl_ready_for_gpio_range() - check if other GPIO pins of
  272. * the same GPIO chip are in range
  273. * @gc: GPIO chip structure from the GPIO subsystem
  274. * @offset: hardware offset of the GPIO relative to the controller
  275. *
  276. * This function is complement of pinctrl_match_gpio_range(). If the return
  277. * value of pinctrl_match_gpio_range() is NULL, this function could be used
  278. * to check whether pinctrl device is ready or not. Maybe some GPIO pins
  279. * of the same GPIO chip don't have back-end pinctrl interface.
  280. * If the return value is true, it means that pinctrl device is ready & the
  281. * certain GPIO pin doesn't have back-end pinctrl device. If the return value
  282. * is false, it means that pinctrl device may not be ready.
  283. */
  284. #ifdef CONFIG_GPIOLIB
  285. static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc,
  286. unsigned int offset)
  287. {
  288. struct pinctrl_dev *pctldev;
  289. struct pinctrl_gpio_range *range = NULL;
  290. mutex_lock(&pinctrldev_list_mutex);
  291. /* Loop over the pin controllers */
  292. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  293. /* Loop over the ranges */
  294. mutex_lock(&pctldev->mutex);
  295. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  296. /* Check if any gpio range overlapped with gpio chip */
  297. if (range->base + range->npins - 1 < gc->base ||
  298. range->base > gc->base + gc->ngpio - 1)
  299. continue;
  300. mutex_unlock(&pctldev->mutex);
  301. mutex_unlock(&pinctrldev_list_mutex);
  302. return true;
  303. }
  304. mutex_unlock(&pctldev->mutex);
  305. }
  306. mutex_unlock(&pinctrldev_list_mutex);
  307. return false;
  308. }
  309. #else
  310. static inline bool
  311. pinctrl_ready_for_gpio_range(struct gpio_chip *gc, unsigned int offset)
  312. {
  313. return true;
  314. }
  315. #endif
  316. /**
  317. * pinctrl_get_device_gpio_range() - find device for GPIO range
  318. * @gc: GPIO chip structure from the GPIO subsystem
  319. * @offset: hardware offset of the GPIO relative to the controller
  320. * @outdev: the pin control device if found
  321. * @outrange: the GPIO range if found
  322. *
  323. * Find the pin controller handling a certain GPIO pin from the pinspace of
  324. * the GPIO subsystem, return the device and the matching GPIO range. Returns
  325. * -EPROBE_DEFER if the GPIO range could not be found in any device since it
  326. * may still have not been registered.
  327. */
  328. static int pinctrl_get_device_gpio_range(struct gpio_chip *gc,
  329. unsigned int offset,
  330. struct pinctrl_dev **outdev,
  331. struct pinctrl_gpio_range **outrange)
  332. {
  333. struct pinctrl_dev *pctldev;
  334. mutex_lock(&pinctrldev_list_mutex);
  335. /* Loop over the pin controllers */
  336. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  337. struct pinctrl_gpio_range *range;
  338. range = pinctrl_match_gpio_range(pctldev, gc, offset);
  339. if (range) {
  340. *outdev = pctldev;
  341. *outrange = range;
  342. mutex_unlock(&pinctrldev_list_mutex);
  343. return 0;
  344. }
  345. }
  346. mutex_unlock(&pinctrldev_list_mutex);
  347. return -EPROBE_DEFER;
  348. }
  349. /**
  350. * pinctrl_add_gpio_range() - register a GPIO range for a controller
  351. * @pctldev: pin controller device to add the range to
  352. * @range: the GPIO range to add
  353. *
  354. * DEPRECATED: Don't use this function in new code. See section 2 of
  355. * Documentation/devicetree/bindings/gpio/gpio.txt on how to bind pinctrl and
  356. * gpio drivers.
  357. *
  358. * This adds a range of GPIOs to be handled by a certain pin controller. Call
  359. * this to register handled ranges after registering your pin controller.
  360. */
  361. void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
  362. struct pinctrl_gpio_range *range)
  363. {
  364. mutex_lock(&pctldev->mutex);
  365. list_add_tail(&range->node, &pctldev->gpio_ranges);
  366. mutex_unlock(&pctldev->mutex);
  367. }
  368. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range);
  369. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  370. struct pinctrl_gpio_range *ranges,
  371. unsigned int nranges)
  372. {
  373. int i;
  374. for (i = 0; i < nranges; i++)
  375. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  376. }
  377. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  378. struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname,
  379. struct pinctrl_gpio_range *range)
  380. {
  381. struct pinctrl_dev *pctldev;
  382. pctldev = get_pinctrl_dev_from_devname(devname);
  383. /*
  384. * If we can't find this device, let's assume that is because
  385. * it has not probed yet, so the driver trying to register this
  386. * range need to defer probing.
  387. */
  388. if (!pctldev)
  389. return ERR_PTR(-EPROBE_DEFER);
  390. pinctrl_add_gpio_range(pctldev, range);
  391. return pctldev;
  392. }
  393. EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range);
  394. int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group,
  395. const unsigned int **pins, unsigned int *num_pins)
  396. {
  397. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  398. int gs;
  399. if (!pctlops->get_group_pins)
  400. return -EINVAL;
  401. gs = pinctrl_get_group_selector(pctldev, pin_group);
  402. if (gs < 0)
  403. return gs;
  404. return pctlops->get_group_pins(pctldev, gs, pins, num_pins);
  405. }
  406. EXPORT_SYMBOL_GPL(pinctrl_get_group_pins);
  407. struct pinctrl_gpio_range *
  408. pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev,
  409. unsigned int pin)
  410. {
  411. struct pinctrl_gpio_range *range;
  412. /* Loop over the ranges */
  413. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  414. /* Check if we're in the valid range */
  415. if (range->pins) {
  416. int a;
  417. for (a = 0; a < range->npins; a++) {
  418. if (range->pins[a] == pin)
  419. return range;
  420. }
  421. } else if (pin >= range->pin_base &&
  422. pin < range->pin_base + range->npins)
  423. return range;
  424. }
  425. return NULL;
  426. }
  427. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock);
  428. /**
  429. * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin
  430. * @pctldev: the pin controller device to look in
  431. * @pin: a controller-local number to find the range for
  432. */
  433. struct pinctrl_gpio_range *
  434. pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev,
  435. unsigned int pin)
  436. {
  437. struct pinctrl_gpio_range *range;
  438. mutex_lock(&pctldev->mutex);
  439. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  440. mutex_unlock(&pctldev->mutex);
  441. return range;
  442. }
  443. EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin);
  444. /**
  445. * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller
  446. * @pctldev: pin controller device to remove the range from
  447. * @range: the GPIO range to remove
  448. */
  449. void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev,
  450. struct pinctrl_gpio_range *range)
  451. {
  452. mutex_lock(&pctldev->mutex);
  453. list_del(&range->node);
  454. mutex_unlock(&pctldev->mutex);
  455. }
  456. EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range);
  457. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  458. /**
  459. * pinctrl_generic_get_group_count() - returns the number of pin groups
  460. * @pctldev: pin controller device
  461. */
  462. int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev)
  463. {
  464. return pctldev->num_groups;
  465. }
  466. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count);
  467. /**
  468. * pinctrl_generic_get_group_name() - returns the name of a pin group
  469. * @pctldev: pin controller device
  470. * @selector: group number
  471. */
  472. const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev,
  473. unsigned int selector)
  474. {
  475. struct group_desc *group;
  476. group = radix_tree_lookup(&pctldev->pin_group_tree,
  477. selector);
  478. if (!group)
  479. return NULL;
  480. return group->grp.name;
  481. }
  482. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name);
  483. /**
  484. * pinctrl_generic_get_group_pins() - gets the pin group pins
  485. * @pctldev: pin controller device
  486. * @selector: group number
  487. * @pins: pins in the group
  488. * @num_pins: number of pins in the group
  489. */
  490. int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev,
  491. unsigned int selector,
  492. const unsigned int **pins,
  493. unsigned int *num_pins)
  494. {
  495. struct group_desc *group;
  496. group = radix_tree_lookup(&pctldev->pin_group_tree,
  497. selector);
  498. if (!group) {
  499. dev_err(pctldev->dev, "%s could not find pingroup%i\n",
  500. __func__, selector);
  501. return -EINVAL;
  502. }
  503. *pins = group->grp.pins;
  504. *num_pins = group->grp.npins;
  505. return 0;
  506. }
  507. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins);
  508. /**
  509. * pinctrl_generic_get_group() - returns a pin group based on the number
  510. * @pctldev: pin controller device
  511. * @selector: group number
  512. */
  513. struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev,
  514. unsigned int selector)
  515. {
  516. struct group_desc *group;
  517. group = radix_tree_lookup(&pctldev->pin_group_tree,
  518. selector);
  519. if (!group)
  520. return NULL;
  521. return group;
  522. }
  523. EXPORT_SYMBOL_GPL(pinctrl_generic_get_group);
  524. static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev,
  525. const char *function)
  526. {
  527. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  528. int ngroups = ops->get_groups_count(pctldev);
  529. int selector = 0;
  530. /* See if this pctldev has this group */
  531. while (selector < ngroups) {
  532. const char *gname = ops->get_group_name(pctldev, selector);
  533. if (gname && !strcmp(function, gname))
  534. return selector;
  535. selector++;
  536. }
  537. return -EINVAL;
  538. }
  539. /**
  540. * pinctrl_generic_add_group() - adds a new pin group
  541. * @pctldev: pin controller device
  542. * @name: name of the pin group
  543. * @pins: pins in the pin group
  544. * @num_pins: number of pins in the pin group
  545. * @data: pin controller driver specific data
  546. *
  547. * Note that the caller must take care of locking.
  548. */
  549. int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name,
  550. const unsigned int *pins, int num_pins, void *data)
  551. {
  552. struct group_desc *group;
  553. int selector, error;
  554. if (!name)
  555. return -EINVAL;
  556. selector = pinctrl_generic_group_name_to_selector(pctldev, name);
  557. if (selector >= 0)
  558. return selector;
  559. selector = pctldev->num_groups;
  560. group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL);
  561. if (!group)
  562. return -ENOMEM;
  563. *group = PINCTRL_GROUP_DESC(name, pins, num_pins, data);
  564. error = radix_tree_insert(&pctldev->pin_group_tree, selector, group);
  565. if (error)
  566. return error;
  567. pctldev->num_groups++;
  568. return selector;
  569. }
  570. EXPORT_SYMBOL_GPL(pinctrl_generic_add_group);
  571. /**
  572. * pinctrl_generic_remove_group() - removes a numbered pin group
  573. * @pctldev: pin controller device
  574. * @selector: group number
  575. *
  576. * Note that the caller must take care of locking.
  577. */
  578. int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
  579. unsigned int selector)
  580. {
  581. struct group_desc *group;
  582. group = radix_tree_lookup(&pctldev->pin_group_tree,
  583. selector);
  584. if (!group)
  585. return -ENOENT;
  586. radix_tree_delete(&pctldev->pin_group_tree, selector);
  587. devm_kfree(pctldev->dev, group);
  588. pctldev->num_groups--;
  589. return 0;
  590. }
  591. EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group);
  592. /**
  593. * pinctrl_generic_free_groups() - removes all pin groups
  594. * @pctldev: pin controller device
  595. *
  596. * Note that the caller must take care of locking. The pinctrl groups
  597. * are allocated with devm_kzalloc() so no need to free them here.
  598. */
  599. static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
  600. {
  601. struct radix_tree_iter iter;
  602. void __rcu **slot;
  603. radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0)
  604. radix_tree_delete(&pctldev->pin_group_tree, iter.index);
  605. pctldev->num_groups = 0;
  606. }
  607. #else
  608. static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev)
  609. {
  610. }
  611. #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */
  612. /**
  613. * pinctrl_get_group_selector() - returns the group selector for a group
  614. * @pctldev: the pin controller handling the group
  615. * @pin_group: the pin group to look up
  616. */
  617. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  618. const char *pin_group)
  619. {
  620. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  621. unsigned int ngroups = pctlops->get_groups_count(pctldev);
  622. unsigned int group_selector = 0;
  623. while (group_selector < ngroups) {
  624. const char *gname = pctlops->get_group_name(pctldev,
  625. group_selector);
  626. if (gname && !strcmp(gname, pin_group)) {
  627. dev_dbg(pctldev->dev,
  628. "found group selector %u for %s\n",
  629. group_selector,
  630. pin_group);
  631. return group_selector;
  632. }
  633. group_selector++;
  634. }
  635. dev_err(pctldev->dev, "does not have pin group %s\n",
  636. pin_group);
  637. return -EINVAL;
  638. }
  639. bool pinctrl_gpio_can_use_line(struct gpio_chip *gc, unsigned int offset)
  640. {
  641. struct pinctrl_dev *pctldev;
  642. struct pinctrl_gpio_range *range;
  643. bool result;
  644. int pin;
  645. /*
  646. * Try to obtain GPIO range, if it fails
  647. * we're probably dealing with GPIO driver
  648. * without a backing pin controller - bail out.
  649. */
  650. if (pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range))
  651. return true;
  652. mutex_lock(&pctldev->mutex);
  653. /* Convert to the pin controllers number space */
  654. pin = gpio_to_pin(range, gc, offset);
  655. result = pinmux_can_be_used_for_gpio(pctldev, pin);
  656. mutex_unlock(&pctldev->mutex);
  657. return result;
  658. }
  659. EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
  660. /**
  661. * pinctrl_gpio_request() - request a single pin to be used as GPIO
  662. * @gc: GPIO chip structure from the GPIO subsystem
  663. * @offset: hardware offset of the GPIO relative to the controller
  664. *
  665. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  666. * as part of their gpio_request() semantics, platforms and individual drivers
  667. * shall *NOT* request GPIO pins to be muxed in.
  668. */
  669. int pinctrl_gpio_request(struct gpio_chip *gc, unsigned int offset)
  670. {
  671. struct pinctrl_gpio_range *range;
  672. struct pinctrl_dev *pctldev;
  673. int ret, pin;
  674. ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
  675. if (ret) {
  676. if (pinctrl_ready_for_gpio_range(gc, offset))
  677. ret = 0;
  678. return ret;
  679. }
  680. mutex_lock(&pctldev->mutex);
  681. /* Convert to the pin controllers number space */
  682. pin = gpio_to_pin(range, gc, offset);
  683. ret = pinmux_request_gpio(pctldev, range, pin, gc->base + offset);
  684. mutex_unlock(&pctldev->mutex);
  685. return ret;
  686. }
  687. EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
  688. /**
  689. * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO
  690. * @gc: GPIO chip structure from the GPIO subsystem
  691. * @offset: hardware offset of the GPIO relative to the controller
  692. *
  693. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  694. * as part of their gpio_request() semantics, platforms and individual drivers
  695. * shall *NOT* request GPIO pins to be muxed in.
  696. */
  697. void pinctrl_gpio_free(struct gpio_chip *gc, unsigned int offset)
  698. {
  699. struct pinctrl_gpio_range *range;
  700. struct pinctrl_dev *pctldev;
  701. int ret, pin;
  702. ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
  703. if (ret)
  704. return;
  705. mutex_lock(&pctldev->mutex);
  706. /* Convert to the pin controllers number space */
  707. pin = gpio_to_pin(range, gc, offset);
  708. pinmux_free_gpio(pctldev, pin, range);
  709. mutex_unlock(&pctldev->mutex);
  710. }
  711. EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
  712. static int pinctrl_gpio_direction(struct gpio_chip *gc, unsigned int offset,
  713. bool input)
  714. {
  715. struct pinctrl_dev *pctldev;
  716. struct pinctrl_gpio_range *range;
  717. int ret;
  718. int pin;
  719. ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
  720. if (ret) {
  721. return ret;
  722. }
  723. mutex_lock(&pctldev->mutex);
  724. /* Convert to the pin controllers number space */
  725. pin = gpio_to_pin(range, gc, offset);
  726. ret = pinmux_gpio_direction(pctldev, range, pin, input);
  727. mutex_unlock(&pctldev->mutex);
  728. return ret;
  729. }
  730. /**
  731. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  732. * @gc: GPIO chip structure from the GPIO subsystem
  733. * @offset: hardware offset of the GPIO relative to the controller
  734. *
  735. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  736. * as part of their gpio_direction_input() semantics, platforms and individual
  737. * drivers shall *NOT* touch pin control GPIO calls.
  738. */
  739. int pinctrl_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
  740. {
  741. return pinctrl_gpio_direction(gc, offset, true);
  742. }
  743. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  744. /**
  745. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  746. * @gc: GPIO chip structure from the GPIO subsystem
  747. * @offset: hardware offset of the GPIO relative to the controller
  748. *
  749. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  750. * as part of their gpio_direction_output() semantics, platforms and individual
  751. * drivers shall *NOT* touch pin control GPIO calls.
  752. */
  753. int pinctrl_gpio_direction_output(struct gpio_chip *gc, unsigned int offset)
  754. {
  755. return pinctrl_gpio_direction(gc, offset, false);
  756. }
  757. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  758. /**
  759. * pinctrl_gpio_set_config() - Apply config to given GPIO pin
  760. * @gc: GPIO chip structure from the GPIO subsystem
  761. * @offset: hardware offset of the GPIO relative to the controller
  762. * @config: the configuration to apply to the GPIO
  763. *
  764. * This function should *ONLY* be used from gpiolib-based GPIO drivers, if
  765. * they need to call the underlying pin controller to change GPIO config
  766. * (for example set debounce time).
  767. */
  768. int pinctrl_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  769. unsigned long config)
  770. {
  771. unsigned long configs[] = { config };
  772. struct pinctrl_gpio_range *range;
  773. struct pinctrl_dev *pctldev;
  774. int ret, pin;
  775. ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
  776. if (ret)
  777. return ret;
  778. mutex_lock(&pctldev->mutex);
  779. pin = gpio_to_pin(range, gc, offset);
  780. ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
  781. mutex_unlock(&pctldev->mutex);
  782. return ret;
  783. }
  784. EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
  785. static struct pinctrl_state *find_state(struct pinctrl *p,
  786. const char *name)
  787. {
  788. struct pinctrl_state *state;
  789. list_for_each_entry(state, &p->states, node)
  790. if (!strcmp(state->name, name))
  791. return state;
  792. return NULL;
  793. }
  794. static struct pinctrl_state *create_state(struct pinctrl *p,
  795. const char *name)
  796. {
  797. struct pinctrl_state *state;
  798. state = kzalloc(sizeof(*state), GFP_KERNEL);
  799. if (!state)
  800. return ERR_PTR(-ENOMEM);
  801. state->name = name;
  802. INIT_LIST_HEAD(&state->settings);
  803. list_add_tail(&state->node, &p->states);
  804. return state;
  805. }
  806. static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev,
  807. const struct pinctrl_map *map)
  808. {
  809. struct pinctrl_state *state;
  810. struct pinctrl_setting *setting;
  811. int ret;
  812. state = find_state(p, map->name);
  813. if (!state)
  814. state = create_state(p, map->name);
  815. if (IS_ERR(state))
  816. return PTR_ERR(state);
  817. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  818. return 0;
  819. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  820. if (!setting)
  821. return -ENOMEM;
  822. setting->type = map->type;
  823. if (pctldev)
  824. setting->pctldev = pctldev;
  825. else
  826. setting->pctldev =
  827. get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  828. if (!setting->pctldev) {
  829. kfree(setting);
  830. /* Do not defer probing of hogs (circular loop) */
  831. if (!strcmp(map->ctrl_dev_name, map->dev_name))
  832. return -ENODEV;
  833. /*
  834. * OK let us guess that the driver is not there yet, and
  835. * let's defer obtaining this pinctrl handle to later...
  836. */
  837. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  838. map->ctrl_dev_name);
  839. return -EPROBE_DEFER;
  840. }
  841. setting->dev_name = map->dev_name;
  842. switch (map->type) {
  843. case PIN_MAP_TYPE_MUX_GROUP:
  844. ret = pinmux_map_to_setting(map, setting);
  845. break;
  846. case PIN_MAP_TYPE_CONFIGS_PIN:
  847. case PIN_MAP_TYPE_CONFIGS_GROUP:
  848. ret = pinconf_map_to_setting(map, setting);
  849. break;
  850. default:
  851. ret = -EINVAL;
  852. break;
  853. }
  854. if (ret < 0) {
  855. kfree(setting);
  856. return ret;
  857. }
  858. list_add_tail(&setting->node, &state->settings);
  859. return 0;
  860. }
  861. static struct pinctrl *find_pinctrl(struct device *dev)
  862. {
  863. struct pinctrl *p;
  864. mutex_lock(&pinctrl_list_mutex);
  865. list_for_each_entry(p, &pinctrl_list, node)
  866. if (p->dev == dev) {
  867. mutex_unlock(&pinctrl_list_mutex);
  868. return p;
  869. }
  870. mutex_unlock(&pinctrl_list_mutex);
  871. return NULL;
  872. }
  873. static void pinctrl_free(struct pinctrl *p, bool inlist);
  874. static struct pinctrl *create_pinctrl(struct device *dev,
  875. struct pinctrl_dev *pctldev)
  876. {
  877. struct pinctrl *p;
  878. const char *devname;
  879. struct pinctrl_maps *maps_node;
  880. const struct pinctrl_map *map;
  881. int ret;
  882. /*
  883. * create the state cookie holder struct pinctrl for each
  884. * mapping, this is what consumers will get when requesting
  885. * a pin control handle with pinctrl_get()
  886. */
  887. p = kzalloc(sizeof(*p), GFP_KERNEL);
  888. if (!p)
  889. return ERR_PTR(-ENOMEM);
  890. p->dev = dev;
  891. INIT_LIST_HEAD(&p->states);
  892. INIT_LIST_HEAD(&p->dt_maps);
  893. ret = pinctrl_dt_to_map(p, pctldev);
  894. if (ret < 0) {
  895. kfree(p);
  896. return ERR_PTR(ret);
  897. }
  898. devname = dev_name(dev);
  899. mutex_lock(&pinctrl_maps_mutex);
  900. /* Iterate over the pin control maps to locate the right ones */
  901. for_each_pin_map(maps_node, map) {
  902. /* Map must be for this device */
  903. if (strcmp(map->dev_name, devname))
  904. continue;
  905. /*
  906. * If pctldev is not null, we are claiming hog for it,
  907. * that means, setting that is served by pctldev by itself.
  908. *
  909. * Thus we must skip map that is for this device but is served
  910. * by other device.
  911. */
  912. if (pctldev &&
  913. strcmp(dev_name(pctldev->dev), map->ctrl_dev_name))
  914. continue;
  915. ret = add_setting(p, pctldev, map);
  916. /*
  917. * At this point the adding of a setting may:
  918. *
  919. * - Defer, if the pinctrl device is not yet available
  920. * - Fail, if the pinctrl device is not yet available,
  921. * AND the setting is a hog. We cannot defer that, since
  922. * the hog will kick in immediately after the device
  923. * is registered.
  924. *
  925. * If the error returned was not -EPROBE_DEFER then we
  926. * accumulate the errors to see if we end up with
  927. * an -EPROBE_DEFER later, as that is the worst case.
  928. */
  929. if (ret == -EPROBE_DEFER) {
  930. mutex_unlock(&pinctrl_maps_mutex);
  931. pinctrl_free(p, false);
  932. return ERR_PTR(ret);
  933. }
  934. }
  935. mutex_unlock(&pinctrl_maps_mutex);
  936. if (ret < 0) {
  937. /* If some other error than deferral occurred, return here */
  938. pinctrl_free(p, false);
  939. return ERR_PTR(ret);
  940. }
  941. kref_init(&p->users);
  942. /* Add the pinctrl handle to the global list */
  943. mutex_lock(&pinctrl_list_mutex);
  944. list_add_tail(&p->node, &pinctrl_list);
  945. mutex_unlock(&pinctrl_list_mutex);
  946. return p;
  947. }
  948. /**
  949. * pinctrl_get() - retrieves the pinctrl handle for a device
  950. * @dev: the device to obtain the handle for
  951. */
  952. struct pinctrl *pinctrl_get(struct device *dev)
  953. {
  954. struct pinctrl *p;
  955. if (WARN_ON(!dev))
  956. return ERR_PTR(-EINVAL);
  957. /*
  958. * See if somebody else (such as the device core) has already
  959. * obtained a handle to the pinctrl for this device. In that case,
  960. * return another pointer to it.
  961. */
  962. p = find_pinctrl(dev);
  963. if (p) {
  964. dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n");
  965. kref_get(&p->users);
  966. return p;
  967. }
  968. return create_pinctrl(dev, NULL);
  969. }
  970. EXPORT_SYMBOL_GPL(pinctrl_get);
  971. static void pinctrl_free_setting(bool disable_setting,
  972. struct pinctrl_setting *setting)
  973. {
  974. switch (setting->type) {
  975. case PIN_MAP_TYPE_MUX_GROUP:
  976. if (disable_setting)
  977. pinmux_disable_setting(setting);
  978. pinmux_free_setting(setting);
  979. break;
  980. case PIN_MAP_TYPE_CONFIGS_PIN:
  981. case PIN_MAP_TYPE_CONFIGS_GROUP:
  982. pinconf_free_setting(setting);
  983. break;
  984. default:
  985. break;
  986. }
  987. }
  988. static void pinctrl_free(struct pinctrl *p, bool inlist)
  989. {
  990. struct pinctrl_state *state, *n1;
  991. struct pinctrl_setting *setting, *n2;
  992. mutex_lock(&pinctrl_list_mutex);
  993. list_for_each_entry_safe(state, n1, &p->states, node) {
  994. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  995. pinctrl_free_setting(state == p->state, setting);
  996. list_del(&setting->node);
  997. kfree(setting);
  998. }
  999. list_del(&state->node);
  1000. kfree(state);
  1001. }
  1002. pinctrl_dt_free_maps(p);
  1003. if (inlist)
  1004. list_del(&p->node);
  1005. kfree(p);
  1006. mutex_unlock(&pinctrl_list_mutex);
  1007. }
  1008. /**
  1009. * pinctrl_release() - release the pinctrl handle
  1010. * @kref: the kref in the pinctrl being released
  1011. */
  1012. static void pinctrl_release(struct kref *kref)
  1013. {
  1014. struct pinctrl *p = container_of(kref, struct pinctrl, users);
  1015. pinctrl_free(p, true);
  1016. }
  1017. /**
  1018. * pinctrl_put() - decrease use count on a previously claimed pinctrl handle
  1019. * @p: the pinctrl handle to release
  1020. */
  1021. void pinctrl_put(struct pinctrl *p)
  1022. {
  1023. kref_put(&p->users, pinctrl_release);
  1024. }
  1025. EXPORT_SYMBOL_GPL(pinctrl_put);
  1026. /**
  1027. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  1028. * @p: the pinctrl handle to retrieve the state from
  1029. * @name: the state name to retrieve
  1030. */
  1031. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p,
  1032. const char *name)
  1033. {
  1034. struct pinctrl_state *state;
  1035. state = find_state(p, name);
  1036. if (!state) {
  1037. if (pinctrl_dummy_state) {
  1038. /* create dummy state */
  1039. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  1040. name);
  1041. state = create_state(p, name);
  1042. } else
  1043. state = ERR_PTR(-ENODEV);
  1044. }
  1045. return state;
  1046. }
  1047. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  1048. static void pinctrl_link_add(struct pinctrl_dev *pctldev,
  1049. struct device *consumer)
  1050. {
  1051. if (pctldev->desc->link_consumers)
  1052. device_link_add(consumer, pctldev->dev,
  1053. DL_FLAG_PM_RUNTIME |
  1054. DL_FLAG_AUTOREMOVE_CONSUMER);
  1055. }
  1056. /**
  1057. * pinctrl_commit_state() - select/activate/program a pinctrl state to HW
  1058. * @p: the pinctrl handle for the device that requests configuration
  1059. * @state: the state handle to select/activate/program
  1060. */
  1061. static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state)
  1062. {
  1063. struct pinctrl_setting *setting, *setting2;
  1064. struct pinctrl_state *old_state = READ_ONCE(p->state);
  1065. int ret;
  1066. if (old_state) {
  1067. /*
  1068. * For each pinmux setting in the old state, forget SW's record
  1069. * of mux owner for that pingroup. Any pingroups which are
  1070. * still owned by the new state will be re-acquired by the call
  1071. * to pinmux_enable_setting() in the loop below.
  1072. */
  1073. list_for_each_entry(setting, &old_state->settings, node) {
  1074. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  1075. continue;
  1076. pinmux_disable_setting(setting);
  1077. }
  1078. }
  1079. p->state = NULL;
  1080. /* Apply all the settings for the new state - pinmux first */
  1081. list_for_each_entry(setting, &state->settings, node) {
  1082. switch (setting->type) {
  1083. case PIN_MAP_TYPE_MUX_GROUP:
  1084. ret = pinmux_enable_setting(setting);
  1085. break;
  1086. case PIN_MAP_TYPE_CONFIGS_PIN:
  1087. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1088. ret = 0;
  1089. break;
  1090. default:
  1091. ret = -EINVAL;
  1092. break;
  1093. }
  1094. if (ret < 0)
  1095. goto unapply_new_state;
  1096. /* Do not link hogs (circular dependency) */
  1097. if (p != setting->pctldev->p)
  1098. pinctrl_link_add(setting->pctldev, p->dev);
  1099. }
  1100. /* Apply all the settings for the new state - pinconf after */
  1101. list_for_each_entry(setting, &state->settings, node) {
  1102. switch (setting->type) {
  1103. case PIN_MAP_TYPE_MUX_GROUP:
  1104. ret = 0;
  1105. break;
  1106. case PIN_MAP_TYPE_CONFIGS_PIN:
  1107. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1108. ret = pinconf_apply_setting(setting);
  1109. break;
  1110. default:
  1111. ret = -EINVAL;
  1112. break;
  1113. }
  1114. if (ret < 0) {
  1115. goto unapply_new_state;
  1116. }
  1117. /* Do not link hogs (circular dependency) */
  1118. if (p != setting->pctldev->p)
  1119. pinctrl_link_add(setting->pctldev, p->dev);
  1120. }
  1121. p->state = state;
  1122. return 0;
  1123. unapply_new_state:
  1124. dev_err(p->dev, "Error applying setting, reverse things back\n");
  1125. list_for_each_entry(setting2, &state->settings, node) {
  1126. if (&setting2->node == &setting->node)
  1127. break;
  1128. /*
  1129. * All we can do here is pinmux_disable_setting.
  1130. * That means that some pins are muxed differently now
  1131. * than they were before applying the setting (We can't
  1132. * "unmux a pin"!), but it's not a big deal since the pins
  1133. * are free to be muxed by another apply_setting.
  1134. */
  1135. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
  1136. pinmux_disable_setting(setting2);
  1137. }
  1138. /* There's no infinite recursive loop here because p->state is NULL */
  1139. if (old_state)
  1140. pinctrl_select_state(p, old_state);
  1141. return ret;
  1142. }
  1143. /**
  1144. * pinctrl_select_state() - select/activate/program a pinctrl state to HW
  1145. * @p: the pinctrl handle for the device that requests configuration
  1146. * @state: the state handle to select/activate/program
  1147. */
  1148. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  1149. {
  1150. if (p->state == state)
  1151. return 0;
  1152. return pinctrl_commit_state(p, state);
  1153. }
  1154. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  1155. static void devm_pinctrl_release(struct device *dev, void *res)
  1156. {
  1157. pinctrl_put(*(struct pinctrl **)res);
  1158. }
  1159. /**
  1160. * devm_pinctrl_get() - Resource managed pinctrl_get()
  1161. * @dev: the device to obtain the handle for
  1162. *
  1163. * If there is a need to explicitly destroy the returned struct pinctrl,
  1164. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  1165. */
  1166. struct pinctrl *devm_pinctrl_get(struct device *dev)
  1167. {
  1168. struct pinctrl **ptr, *p;
  1169. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  1170. if (!ptr)
  1171. return ERR_PTR(-ENOMEM);
  1172. p = pinctrl_get(dev);
  1173. if (!IS_ERR(p)) {
  1174. *ptr = p;
  1175. devres_add(dev, ptr);
  1176. } else {
  1177. devres_free(ptr);
  1178. }
  1179. return p;
  1180. }
  1181. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  1182. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  1183. {
  1184. struct pinctrl **p = res;
  1185. return *p == data;
  1186. }
  1187. /**
  1188. * devm_pinctrl_put() - Resource managed pinctrl_put()
  1189. * @p: the pinctrl handle to release
  1190. *
  1191. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  1192. * this function will not need to be called and the resource management
  1193. * code will ensure that the resource is freed.
  1194. */
  1195. void devm_pinctrl_put(struct pinctrl *p)
  1196. {
  1197. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  1198. devm_pinctrl_match, p));
  1199. }
  1200. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  1201. /**
  1202. * pinctrl_register_mappings() - register a set of pin controller mappings
  1203. * @maps: the pincontrol mappings table to register. Note the pinctrl-core
  1204. * keeps a reference to the passed in maps, so they should _not_ be
  1205. * marked with __initdata.
  1206. * @num_maps: the number of maps in the mapping table
  1207. */
  1208. int pinctrl_register_mappings(const struct pinctrl_map *maps,
  1209. unsigned int num_maps)
  1210. {
  1211. int i, ret;
  1212. struct pinctrl_maps *maps_node;
  1213. pr_debug("add %u pinctrl maps\n", num_maps);
  1214. /* First sanity check the new mapping */
  1215. for (i = 0; i < num_maps; i++) {
  1216. if (!maps[i].dev_name) {
  1217. pr_err("failed to register map %s (%d): no device given\n",
  1218. maps[i].name, i);
  1219. return -EINVAL;
  1220. }
  1221. if (!maps[i].name) {
  1222. pr_err("failed to register map %d: no map name given\n",
  1223. i);
  1224. return -EINVAL;
  1225. }
  1226. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  1227. !maps[i].ctrl_dev_name) {
  1228. pr_err("failed to register map %s (%d): no pin control device given\n",
  1229. maps[i].name, i);
  1230. return -EINVAL;
  1231. }
  1232. switch (maps[i].type) {
  1233. case PIN_MAP_TYPE_DUMMY_STATE:
  1234. break;
  1235. case PIN_MAP_TYPE_MUX_GROUP:
  1236. ret = pinmux_validate_map(&maps[i], i);
  1237. if (ret < 0)
  1238. return ret;
  1239. break;
  1240. case PIN_MAP_TYPE_CONFIGS_PIN:
  1241. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1242. ret = pinconf_validate_map(&maps[i], i);
  1243. if (ret < 0)
  1244. return ret;
  1245. break;
  1246. default:
  1247. pr_err("failed to register map %s (%d): invalid type given\n",
  1248. maps[i].name, i);
  1249. return -EINVAL;
  1250. }
  1251. }
  1252. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  1253. if (!maps_node)
  1254. return -ENOMEM;
  1255. maps_node->maps = maps;
  1256. maps_node->num_maps = num_maps;
  1257. mutex_lock(&pinctrl_maps_mutex);
  1258. list_add_tail(&maps_node->node, &pinctrl_maps);
  1259. mutex_unlock(&pinctrl_maps_mutex);
  1260. return 0;
  1261. }
  1262. EXPORT_SYMBOL_GPL(pinctrl_register_mappings);
  1263. /**
  1264. * pinctrl_unregister_mappings() - unregister a set of pin controller mappings
  1265. * @map: the pincontrol mappings table passed to pinctrl_register_mappings()
  1266. * when registering the mappings.
  1267. */
  1268. void pinctrl_unregister_mappings(const struct pinctrl_map *map)
  1269. {
  1270. struct pinctrl_maps *maps_node;
  1271. mutex_lock(&pinctrl_maps_mutex);
  1272. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  1273. if (maps_node->maps == map) {
  1274. list_del(&maps_node->node);
  1275. kfree(maps_node);
  1276. mutex_unlock(&pinctrl_maps_mutex);
  1277. return;
  1278. }
  1279. }
  1280. mutex_unlock(&pinctrl_maps_mutex);
  1281. }
  1282. EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings);
  1283. /**
  1284. * pinctrl_force_sleep() - turn a given controller device into sleep state
  1285. * @pctldev: pin controller device
  1286. */
  1287. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  1288. {
  1289. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1290. return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep);
  1291. return 0;
  1292. }
  1293. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1294. /**
  1295. * pinctrl_force_default() - turn a given controller device into default state
  1296. * @pctldev: pin controller device
  1297. */
  1298. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1299. {
  1300. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1301. return pinctrl_commit_state(pctldev->p, pctldev->hog_default);
  1302. return 0;
  1303. }
  1304. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1305. /**
  1306. * pinctrl_init_done() - tell pinctrl probe is done
  1307. *
  1308. * We'll use this time to switch the pins from "init" to "default" unless the
  1309. * driver selected some other state.
  1310. *
  1311. * @dev: device to that's done probing
  1312. */
  1313. int pinctrl_init_done(struct device *dev)
  1314. {
  1315. struct dev_pin_info *pins = dev->pins;
  1316. int ret;
  1317. if (!pins)
  1318. return 0;
  1319. if (IS_ERR(pins->init_state))
  1320. return 0; /* No such state */
  1321. if (pins->p->state != pins->init_state)
  1322. return 0; /* Not at init anyway */
  1323. if (IS_ERR(pins->default_state))
  1324. return 0; /* No default state */
  1325. ret = pinctrl_select_state(pins->p, pins->default_state);
  1326. if (ret)
  1327. dev_err(dev, "failed to activate default pinctrl state\n");
  1328. return ret;
  1329. }
  1330. static int pinctrl_select_bound_state(struct device *dev,
  1331. struct pinctrl_state *state)
  1332. {
  1333. struct dev_pin_info *pins = dev->pins;
  1334. int ret;
  1335. if (IS_ERR(state))
  1336. return 0; /* No such state */
  1337. ret = pinctrl_select_state(pins->p, state);
  1338. if (ret)
  1339. dev_err(dev, "failed to activate pinctrl state %s\n",
  1340. state->name);
  1341. return ret;
  1342. }
  1343. /**
  1344. * pinctrl_select_default_state() - select default pinctrl state
  1345. * @dev: device to select default state for
  1346. */
  1347. int pinctrl_select_default_state(struct device *dev)
  1348. {
  1349. if (!dev->pins)
  1350. return 0;
  1351. return pinctrl_select_bound_state(dev, dev->pins->default_state);
  1352. }
  1353. EXPORT_SYMBOL_GPL(pinctrl_select_default_state);
  1354. #ifdef CONFIG_PM
  1355. /**
  1356. * pinctrl_pm_select_default_state() - select default pinctrl state for PM
  1357. * @dev: device to select default state for
  1358. */
  1359. int pinctrl_pm_select_default_state(struct device *dev)
  1360. {
  1361. return pinctrl_select_default_state(dev);
  1362. }
  1363. EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state);
  1364. /**
  1365. * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM
  1366. * @dev: device to select sleep state for
  1367. */
  1368. int pinctrl_pm_select_sleep_state(struct device *dev)
  1369. {
  1370. if (!dev->pins)
  1371. return 0;
  1372. return pinctrl_select_bound_state(dev, dev->pins->sleep_state);
  1373. }
  1374. EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state);
  1375. /**
  1376. * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM
  1377. * @dev: device to select idle state for
  1378. */
  1379. int pinctrl_pm_select_idle_state(struct device *dev)
  1380. {
  1381. if (!dev->pins)
  1382. return 0;
  1383. return pinctrl_select_bound_state(dev, dev->pins->idle_state);
  1384. }
  1385. EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state);
  1386. #endif
  1387. #ifdef CONFIG_DEBUG_FS
  1388. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1389. {
  1390. struct pinctrl_dev *pctldev = s->private;
  1391. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1392. unsigned int i, pin;
  1393. #ifdef CONFIG_GPIOLIB
  1394. struct gpio_device *gdev = NULL;
  1395. struct pinctrl_gpio_range *range;
  1396. int gpio_num;
  1397. #endif
  1398. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1399. mutex_lock(&pctldev->mutex);
  1400. /* The pin number can be retrived from the pin controller descriptor */
  1401. for (i = 0; i < pctldev->desc->npins; i++) {
  1402. struct pin_desc *desc;
  1403. pin = pctldev->desc->pins[i].number;
  1404. desc = pin_desc_get(pctldev, pin);
  1405. /* Pin space may be sparse */
  1406. if (!desc)
  1407. continue;
  1408. seq_printf(s, "pin %d (%s) ", pin, desc->name);
  1409. #ifdef CONFIG_GPIOLIB
  1410. gdev = NULL;
  1411. gpio_num = -1;
  1412. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1413. if (range->pins != NULL) {
  1414. for (int i = 0; i < range->npins; ++i) {
  1415. if (range->pins[i] == pin) {
  1416. gpio_num = range->base + i;
  1417. break;
  1418. }
  1419. }
  1420. } else if ((pin >= range->pin_base) &&
  1421. (pin < (range->pin_base + range->npins))) {
  1422. gpio_num =
  1423. range->base + (pin - range->pin_base);
  1424. }
  1425. if (gpio_num != -1)
  1426. break;
  1427. }
  1428. if (gpio_num >= 0)
  1429. /*
  1430. * FIXME: gpio_num comes from the global GPIO numberspace.
  1431. * we need to get rid of the range->base eventually and
  1432. * get the descriptor directly from the gpio_chip.
  1433. */
  1434. gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num));
  1435. if (gdev)
  1436. seq_printf(s, "%u:%s ",
  1437. gpio_num - gpio_device_get_base(gdev),
  1438. gpio_device_get_label(gdev));
  1439. else
  1440. seq_puts(s, "0:? ");
  1441. #endif
  1442. /* Driver-specific info per pin */
  1443. if (ops->pin_dbg_show)
  1444. ops->pin_dbg_show(pctldev, s, pin);
  1445. seq_puts(s, "\n");
  1446. }
  1447. mutex_unlock(&pctldev->mutex);
  1448. return 0;
  1449. }
  1450. DEFINE_SHOW_ATTRIBUTE(pinctrl_pins);
  1451. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1452. {
  1453. struct pinctrl_dev *pctldev = s->private;
  1454. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1455. unsigned int ngroups, selector = 0;
  1456. mutex_lock(&pctldev->mutex);
  1457. ngroups = ops->get_groups_count(pctldev);
  1458. seq_puts(s, "registered pin groups:\n");
  1459. while (selector < ngroups) {
  1460. const unsigned int *pins = NULL;
  1461. unsigned int num_pins = 0;
  1462. const char *gname = ops->get_group_name(pctldev, selector);
  1463. const char *pname;
  1464. int ret = 0;
  1465. int i;
  1466. if (ops->get_group_pins)
  1467. ret = ops->get_group_pins(pctldev, selector,
  1468. &pins, &num_pins);
  1469. if (ret)
  1470. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1471. gname);
  1472. else {
  1473. seq_printf(s, "group: %s\n", gname);
  1474. for (i = 0; i < num_pins; i++) {
  1475. pname = pin_get_name(pctldev, pins[i]);
  1476. if (WARN_ON(!pname)) {
  1477. mutex_unlock(&pctldev->mutex);
  1478. return -EINVAL;
  1479. }
  1480. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1481. }
  1482. seq_puts(s, "\n");
  1483. }
  1484. selector++;
  1485. }
  1486. mutex_unlock(&pctldev->mutex);
  1487. return 0;
  1488. }
  1489. DEFINE_SHOW_ATTRIBUTE(pinctrl_groups);
  1490. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1491. {
  1492. struct pinctrl_dev *pctldev = s->private;
  1493. struct pinctrl_gpio_range *range;
  1494. seq_puts(s, "GPIO ranges handled:\n");
  1495. mutex_lock(&pctldev->mutex);
  1496. /* Loop over the ranges */
  1497. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1498. if (range->pins) {
  1499. int a;
  1500. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {",
  1501. range->id, range->name,
  1502. range->base, (range->base + range->npins - 1));
  1503. for (a = 0; a < range->npins - 1; a++)
  1504. seq_printf(s, "%u, ", range->pins[a]);
  1505. seq_printf(s, "%u}\n", range->pins[a]);
  1506. }
  1507. else
  1508. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1509. range->id, range->name,
  1510. range->base, (range->base + range->npins - 1),
  1511. range->pin_base,
  1512. (range->pin_base + range->npins - 1));
  1513. }
  1514. mutex_unlock(&pctldev->mutex);
  1515. return 0;
  1516. }
  1517. DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges);
  1518. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1519. {
  1520. struct pinctrl_dev *pctldev;
  1521. seq_puts(s, "name [pinmux] [pinconf]\n");
  1522. mutex_lock(&pinctrldev_list_mutex);
  1523. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1524. seq_printf(s, "%s ", pctldev->desc->name);
  1525. if (pctldev->desc->pmxops)
  1526. seq_puts(s, "yes ");
  1527. else
  1528. seq_puts(s, "no ");
  1529. if (pctldev->desc->confops)
  1530. seq_puts(s, "yes");
  1531. else
  1532. seq_puts(s, "no");
  1533. seq_puts(s, "\n");
  1534. }
  1535. mutex_unlock(&pinctrldev_list_mutex);
  1536. return 0;
  1537. }
  1538. DEFINE_SHOW_ATTRIBUTE(pinctrl_devices);
  1539. static inline const char *map_type(enum pinctrl_map_type type)
  1540. {
  1541. static const char * const names[] = {
  1542. "INVALID",
  1543. "DUMMY_STATE",
  1544. "MUX_GROUP",
  1545. "CONFIGS_PIN",
  1546. "CONFIGS_GROUP",
  1547. };
  1548. if (type >= ARRAY_SIZE(names))
  1549. return "UNKNOWN";
  1550. return names[type];
  1551. }
  1552. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1553. {
  1554. struct pinctrl_maps *maps_node;
  1555. const struct pinctrl_map *map;
  1556. seq_puts(s, "Pinctrl maps:\n");
  1557. mutex_lock(&pinctrl_maps_mutex);
  1558. for_each_pin_map(maps_node, map) {
  1559. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1560. map->dev_name, map->name, map_type(map->type),
  1561. map->type);
  1562. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1563. seq_printf(s, "controlling device %s\n",
  1564. map->ctrl_dev_name);
  1565. switch (map->type) {
  1566. case PIN_MAP_TYPE_MUX_GROUP:
  1567. pinmux_show_map(s, map);
  1568. break;
  1569. case PIN_MAP_TYPE_CONFIGS_PIN:
  1570. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1571. pinconf_show_map(s, map);
  1572. break;
  1573. default:
  1574. break;
  1575. }
  1576. seq_putc(s, '\n');
  1577. }
  1578. mutex_unlock(&pinctrl_maps_mutex);
  1579. return 0;
  1580. }
  1581. DEFINE_SHOW_ATTRIBUTE(pinctrl_maps);
  1582. static int pinctrl_show(struct seq_file *s, void *what)
  1583. {
  1584. struct pinctrl *p;
  1585. struct pinctrl_state *state;
  1586. struct pinctrl_setting *setting;
  1587. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1588. mutex_lock(&pinctrl_list_mutex);
  1589. list_for_each_entry(p, &pinctrl_list, node) {
  1590. seq_printf(s, "device: %s current state: %s\n",
  1591. dev_name(p->dev),
  1592. p->state ? p->state->name : "none");
  1593. list_for_each_entry(state, &p->states, node) {
  1594. seq_printf(s, " state: %s\n", state->name);
  1595. list_for_each_entry(setting, &state->settings, node) {
  1596. struct pinctrl_dev *pctldev = setting->pctldev;
  1597. seq_printf(s, " type: %s controller %s ",
  1598. map_type(setting->type),
  1599. pinctrl_dev_get_name(pctldev));
  1600. switch (setting->type) {
  1601. case PIN_MAP_TYPE_MUX_GROUP:
  1602. pinmux_show_setting(s, setting);
  1603. break;
  1604. case PIN_MAP_TYPE_CONFIGS_PIN:
  1605. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1606. pinconf_show_setting(s, setting);
  1607. break;
  1608. default:
  1609. break;
  1610. }
  1611. }
  1612. }
  1613. }
  1614. mutex_unlock(&pinctrl_list_mutex);
  1615. return 0;
  1616. }
  1617. DEFINE_SHOW_ATTRIBUTE(pinctrl);
  1618. static struct dentry *debugfs_root;
  1619. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1620. {
  1621. struct dentry *device_root;
  1622. const char *debugfs_name;
  1623. if (pctldev->desc->name &&
  1624. strcmp(dev_name(pctldev->dev), pctldev->desc->name)) {
  1625. debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL,
  1626. "%s-%s", dev_name(pctldev->dev),
  1627. pctldev->desc->name);
  1628. if (!debugfs_name) {
  1629. pr_warn("failed to determine debugfs dir name for %s\n",
  1630. dev_name(pctldev->dev));
  1631. return;
  1632. }
  1633. } else {
  1634. debugfs_name = dev_name(pctldev->dev);
  1635. }
  1636. device_root = debugfs_create_dir(debugfs_name, debugfs_root);
  1637. pctldev->device_root = device_root;
  1638. if (IS_ERR(device_root) || !device_root) {
  1639. pr_warn("failed to create debugfs directory for %s\n",
  1640. dev_name(pctldev->dev));
  1641. return;
  1642. }
  1643. debugfs_create_file("pins", 0444,
  1644. device_root, pctldev, &pinctrl_pins_fops);
  1645. debugfs_create_file("pingroups", 0444,
  1646. device_root, pctldev, &pinctrl_groups_fops);
  1647. debugfs_create_file("gpio-ranges", 0444,
  1648. device_root, pctldev, &pinctrl_gpioranges_fops);
  1649. if (pctldev->desc->pmxops)
  1650. pinmux_init_device_debugfs(device_root, pctldev);
  1651. if (pctldev->desc->confops)
  1652. pinconf_init_device_debugfs(device_root, pctldev);
  1653. }
  1654. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1655. {
  1656. debugfs_remove_recursive(pctldev->device_root);
  1657. }
  1658. static void pinctrl_init_debugfs(void)
  1659. {
  1660. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1661. if (IS_ERR(debugfs_root)) {
  1662. pr_warn("failed to create debugfs directory\n");
  1663. debugfs_root = NULL;
  1664. return;
  1665. }
  1666. debugfs_create_file("pinctrl-devices", 0444,
  1667. debugfs_root, NULL, &pinctrl_devices_fops);
  1668. debugfs_create_file("pinctrl-maps", 0444,
  1669. debugfs_root, NULL, &pinctrl_maps_fops);
  1670. debugfs_create_file("pinctrl-handles", 0444,
  1671. debugfs_root, NULL, &pinctrl_fops);
  1672. }
  1673. #else /* CONFIG_DEBUG_FS */
  1674. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1675. {
  1676. }
  1677. static void pinctrl_init_debugfs(void)
  1678. {
  1679. }
  1680. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1681. {
  1682. }
  1683. #endif
  1684. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1685. {
  1686. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1687. if (!ops ||
  1688. !ops->get_groups_count ||
  1689. !ops->get_group_name)
  1690. return -EINVAL;
  1691. return 0;
  1692. }
  1693. /**
  1694. * pinctrl_init_controller() - init a pin controller device
  1695. * @pctldesc: descriptor for this pin controller
  1696. * @dev: parent device for this pin controller
  1697. * @driver_data: private pin controller data for this pin controller
  1698. */
  1699. static struct pinctrl_dev *
  1700. pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
  1701. void *driver_data)
  1702. {
  1703. struct pinctrl_dev *pctldev;
  1704. int ret;
  1705. if (!pctldesc)
  1706. return ERR_PTR(-EINVAL);
  1707. if (!pctldesc->name)
  1708. return ERR_PTR(-EINVAL);
  1709. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1710. if (!pctldev)
  1711. return ERR_PTR(-ENOMEM);
  1712. /* Initialize pin control device struct */
  1713. pctldev->owner = pctldesc->owner;
  1714. pctldev->desc = pctldesc;
  1715. pctldev->driver_data = driver_data;
  1716. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1717. #ifdef CONFIG_GENERIC_PINCTRL_GROUPS
  1718. INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL);
  1719. #endif
  1720. #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS
  1721. INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL);
  1722. #endif
  1723. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1724. INIT_LIST_HEAD(&pctldev->node);
  1725. pctldev->dev = dev;
  1726. mutex_init(&pctldev->mutex);
  1727. /* check core ops for sanity */
  1728. ret = pinctrl_check_ops(pctldev);
  1729. if (ret) {
  1730. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1731. goto out_err;
  1732. }
  1733. /* If we're implementing pinmuxing, check the ops for sanity */
  1734. if (pctldesc->pmxops) {
  1735. ret = pinmux_check_ops(pctldev);
  1736. if (ret)
  1737. goto out_err;
  1738. }
  1739. /* If we're implementing pinconfig, check the ops for sanity */
  1740. if (pctldesc->confops) {
  1741. ret = pinconf_check_ops(pctldev);
  1742. if (ret)
  1743. goto out_err;
  1744. }
  1745. /* Register all the pins */
  1746. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1747. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1748. if (ret) {
  1749. dev_err(dev, "error during pin registration\n");
  1750. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1751. pctldesc->npins);
  1752. goto out_err;
  1753. }
  1754. return pctldev;
  1755. out_err:
  1756. mutex_destroy(&pctldev->mutex);
  1757. kfree(pctldev);
  1758. return ERR_PTR(ret);
  1759. }
  1760. static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, struct pinctrl_desc *pctldesc)
  1761. {
  1762. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1763. pctldesc->npins);
  1764. mutex_destroy(&pctldev->mutex);
  1765. kfree(pctldev);
  1766. }
  1767. static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev)
  1768. {
  1769. pctldev->p = create_pinctrl(pctldev->dev, pctldev);
  1770. if (PTR_ERR(pctldev->p) == -ENODEV) {
  1771. dev_dbg(pctldev->dev, "no hogs found\n");
  1772. return 0;
  1773. }
  1774. if (IS_ERR(pctldev->p)) {
  1775. dev_err(pctldev->dev, "error claiming hogs: %li\n",
  1776. PTR_ERR(pctldev->p));
  1777. return PTR_ERR(pctldev->p);
  1778. }
  1779. pctldev->hog_default =
  1780. pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT);
  1781. if (IS_ERR(pctldev->hog_default)) {
  1782. dev_dbg(pctldev->dev,
  1783. "failed to lookup the default state\n");
  1784. } else {
  1785. if (pinctrl_select_state(pctldev->p,
  1786. pctldev->hog_default))
  1787. dev_err(pctldev->dev,
  1788. "failed to select default state\n");
  1789. }
  1790. pctldev->hog_sleep =
  1791. pinctrl_lookup_state(pctldev->p,
  1792. PINCTRL_STATE_SLEEP);
  1793. if (IS_ERR(pctldev->hog_sleep))
  1794. dev_dbg(pctldev->dev,
  1795. "failed to lookup the sleep state\n");
  1796. return 0;
  1797. }
  1798. int pinctrl_enable(struct pinctrl_dev *pctldev)
  1799. {
  1800. int error;
  1801. error = pinctrl_claim_hogs(pctldev);
  1802. if (error) {
  1803. dev_err(pctldev->dev, "could not claim hogs: %i\n", error);
  1804. return error;
  1805. }
  1806. mutex_lock(&pinctrldev_list_mutex);
  1807. list_add_tail(&pctldev->node, &pinctrldev_list);
  1808. mutex_unlock(&pinctrldev_list_mutex);
  1809. pinctrl_init_device_debugfs(pctldev);
  1810. return 0;
  1811. }
  1812. EXPORT_SYMBOL_GPL(pinctrl_enable);
  1813. /**
  1814. * pinctrl_register() - register a pin controller device
  1815. * @pctldesc: descriptor for this pin controller
  1816. * @dev: parent device for this pin controller
  1817. * @driver_data: private pin controller data for this pin controller
  1818. *
  1819. * Note that pinctrl_register() is known to have problems as the pin
  1820. * controller driver functions are called before the driver has a
  1821. * struct pinctrl_dev handle. To avoid issues later on, please use the
  1822. * new pinctrl_register_and_init() below instead.
  1823. */
  1824. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1825. struct device *dev, void *driver_data)
  1826. {
  1827. struct pinctrl_dev *pctldev;
  1828. int error;
  1829. pctldev = pinctrl_init_controller(pctldesc, dev, driver_data);
  1830. if (IS_ERR(pctldev))
  1831. return pctldev;
  1832. error = pinctrl_enable(pctldev);
  1833. if (error) {
  1834. pinctrl_uninit_controller(pctldev, pctldesc);
  1835. return ERR_PTR(error);
  1836. }
  1837. return pctldev;
  1838. }
  1839. EXPORT_SYMBOL_GPL(pinctrl_register);
  1840. /**
  1841. * pinctrl_register_and_init() - register and init pin controller device
  1842. * @pctldesc: descriptor for this pin controller
  1843. * @dev: parent device for this pin controller
  1844. * @driver_data: private pin controller data for this pin controller
  1845. * @pctldev: pin controller device
  1846. *
  1847. * Note that pinctrl_enable() still needs to be manually called after
  1848. * this once the driver is ready.
  1849. */
  1850. int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
  1851. struct device *dev, void *driver_data,
  1852. struct pinctrl_dev **pctldev)
  1853. {
  1854. struct pinctrl_dev *p;
  1855. p = pinctrl_init_controller(pctldesc, dev, driver_data);
  1856. if (IS_ERR(p))
  1857. return PTR_ERR(p);
  1858. /*
  1859. * We have pinctrl_start() call functions in the pin controller
  1860. * driver with create_pinctrl() for at least dt_node_to_map(). So
  1861. * let's make sure pctldev is properly initialized for the
  1862. * pin controller driver before we do anything.
  1863. */
  1864. *pctldev = p;
  1865. return 0;
  1866. }
  1867. EXPORT_SYMBOL_GPL(pinctrl_register_and_init);
  1868. /**
  1869. * pinctrl_unregister() - unregister pinmux
  1870. * @pctldev: pin controller to unregister
  1871. *
  1872. * Called by pinmux drivers to unregister a pinmux.
  1873. */
  1874. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1875. {
  1876. struct pinctrl_gpio_range *range, *n;
  1877. if (!pctldev)
  1878. return;
  1879. mutex_lock(&pctldev->mutex);
  1880. pinctrl_remove_device_debugfs(pctldev);
  1881. mutex_unlock(&pctldev->mutex);
  1882. if (!IS_ERR_OR_NULL(pctldev->p))
  1883. pinctrl_put(pctldev->p);
  1884. mutex_lock(&pinctrldev_list_mutex);
  1885. mutex_lock(&pctldev->mutex);
  1886. /* TODO: check that no pinmuxes are still active? */
  1887. list_del(&pctldev->node);
  1888. pinmux_generic_free_functions(pctldev);
  1889. pinctrl_generic_free_groups(pctldev);
  1890. /* Destroy descriptor tree */
  1891. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1892. pctldev->desc->npins);
  1893. /* remove gpio ranges map */
  1894. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1895. list_del(&range->node);
  1896. mutex_unlock(&pctldev->mutex);
  1897. mutex_destroy(&pctldev->mutex);
  1898. kfree(pctldev);
  1899. mutex_unlock(&pinctrldev_list_mutex);
  1900. }
  1901. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1902. static void devm_pinctrl_dev_release(struct device *dev, void *res)
  1903. {
  1904. struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
  1905. pinctrl_unregister(pctldev);
  1906. }
  1907. static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
  1908. {
  1909. struct pctldev **r = res;
  1910. if (WARN_ON(!r || !*r))
  1911. return 0;
  1912. return *r == data;
  1913. }
  1914. /**
  1915. * devm_pinctrl_register() - Resource managed version of pinctrl_register().
  1916. * @dev: parent device for this pin controller
  1917. * @pctldesc: descriptor for this pin controller
  1918. * @driver_data: private pin controller data for this pin controller
  1919. *
  1920. * Returns an error pointer if pincontrol register failed. Otherwise
  1921. * it returns valid pinctrl handle.
  1922. *
  1923. * The pinctrl device will be automatically released when the device is unbound.
  1924. */
  1925. struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
  1926. struct pinctrl_desc *pctldesc,
  1927. void *driver_data)
  1928. {
  1929. struct pinctrl_dev **ptr, *pctldev;
  1930. ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
  1931. if (!ptr)
  1932. return ERR_PTR(-ENOMEM);
  1933. pctldev = pinctrl_register(pctldesc, dev, driver_data);
  1934. if (IS_ERR(pctldev)) {
  1935. devres_free(ptr);
  1936. return pctldev;
  1937. }
  1938. *ptr = pctldev;
  1939. devres_add(dev, ptr);
  1940. return pctldev;
  1941. }
  1942. EXPORT_SYMBOL_GPL(devm_pinctrl_register);
  1943. /**
  1944. * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init
  1945. * @dev: parent device for this pin controller
  1946. * @pctldesc: descriptor for this pin controller
  1947. * @driver_data: private pin controller data for this pin controller
  1948. * @pctldev: pin controller device
  1949. *
  1950. * Returns zero on success or an error number on failure.
  1951. *
  1952. * The pinctrl device will be automatically released when the device is unbound.
  1953. */
  1954. int devm_pinctrl_register_and_init(struct device *dev,
  1955. struct pinctrl_desc *pctldesc,
  1956. void *driver_data,
  1957. struct pinctrl_dev **pctldev)
  1958. {
  1959. struct pinctrl_dev **ptr;
  1960. int error;
  1961. ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
  1962. if (!ptr)
  1963. return -ENOMEM;
  1964. error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
  1965. if (error) {
  1966. devres_free(ptr);
  1967. return error;
  1968. }
  1969. *ptr = *pctldev;
  1970. devres_add(dev, ptr);
  1971. return 0;
  1972. }
  1973. EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init);
  1974. /**
  1975. * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
  1976. * @dev: device for which resource was allocated
  1977. * @pctldev: the pinctrl device to unregister.
  1978. */
  1979. void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
  1980. {
  1981. WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
  1982. devm_pinctrl_dev_match, pctldev));
  1983. }
  1984. EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
  1985. static int __init pinctrl_init(void)
  1986. {
  1987. pr_info("initialized pinctrl subsystem\n");
  1988. pinctrl_init_debugfs();
  1989. return 0;
  1990. }
  1991. /* init early since many drivers really need to initialized pinmux early */
  1992. core_initcall(pinctrl_init);