core.c 46 KB

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