core.c 42 KB

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