core.c 36 KB

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