core.c 57 KB

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