core.c 40 KB

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