core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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. void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev,
  289. struct pinctrl_gpio_range *ranges,
  290. unsigned nranges)
  291. {
  292. int i;
  293. for (i = 0; i < nranges; i++)
  294. pinctrl_add_gpio_range(pctldev, &ranges[i]);
  295. }
  296. EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges);
  297. /**
  298. * pinctrl_get_group_selector() - returns the group selector for a group
  299. * @pctldev: the pin controller handling the group
  300. * @pin_group: the pin group to look up
  301. */
  302. int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
  303. const char *pin_group)
  304. {
  305. const struct pinctrl_ops *pctlops = pctldev->desc->pctlops;
  306. unsigned ngroups = pctlops->get_groups_count(pctldev);
  307. unsigned group_selector = 0;
  308. while (group_selector < ngroups) {
  309. const char *gname = pctlops->get_group_name(pctldev,
  310. group_selector);
  311. if (!strcmp(gname, pin_group)) {
  312. dev_dbg(pctldev->dev,
  313. "found group selector %u for %s\n",
  314. group_selector,
  315. pin_group);
  316. return group_selector;
  317. }
  318. group_selector++;
  319. }
  320. dev_err(pctldev->dev, "does not have pin group %s\n",
  321. pin_group);
  322. return -EINVAL;
  323. }
  324. /**
  325. * pinctrl_request_gpio() - request a single pin to be used in as GPIO
  326. * @gpio: the GPIO pin number from the GPIO subsystem number space
  327. *
  328. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  329. * as part of their gpio_request() semantics, platforms and individual drivers
  330. * shall *NOT* request GPIO pins to be muxed in.
  331. */
  332. int pinctrl_request_gpio(unsigned gpio)
  333. {
  334. struct pinctrl_dev *pctldev;
  335. struct pinctrl_gpio_range *range;
  336. int ret;
  337. int pin;
  338. mutex_lock(&pinctrl_mutex);
  339. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  340. if (ret) {
  341. mutex_unlock(&pinctrl_mutex);
  342. return ret;
  343. }
  344. /* Convert to the pin controllers number space */
  345. pin = gpio - range->base + range->pin_base;
  346. ret = pinmux_request_gpio(pctldev, range, pin, gpio);
  347. mutex_unlock(&pinctrl_mutex);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
  351. /**
  352. * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
  353. * @gpio: the GPIO pin number from the GPIO subsystem number space
  354. *
  355. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  356. * as part of their gpio_free() semantics, platforms and individual drivers
  357. * shall *NOT* request GPIO pins to be muxed out.
  358. */
  359. void pinctrl_free_gpio(unsigned gpio)
  360. {
  361. struct pinctrl_dev *pctldev;
  362. struct pinctrl_gpio_range *range;
  363. int ret;
  364. int pin;
  365. mutex_lock(&pinctrl_mutex);
  366. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  367. if (ret) {
  368. mutex_unlock(&pinctrl_mutex);
  369. return;
  370. }
  371. /* Convert to the pin controllers number space */
  372. pin = gpio - range->base + range->pin_base;
  373. pinmux_free_gpio(pctldev, pin, range);
  374. mutex_unlock(&pinctrl_mutex);
  375. }
  376. EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
  377. static int pinctrl_gpio_direction(unsigned gpio, bool input)
  378. {
  379. struct pinctrl_dev *pctldev;
  380. struct pinctrl_gpio_range *range;
  381. int ret;
  382. int pin;
  383. ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
  384. if (ret)
  385. return ret;
  386. /* Convert to the pin controllers number space */
  387. pin = gpio - range->base + range->pin_base;
  388. return pinmux_gpio_direction(pctldev, range, pin, input);
  389. }
  390. /**
  391. * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
  392. * @gpio: the GPIO pin number from the GPIO subsystem number space
  393. *
  394. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  395. * as part of their gpio_direction_input() semantics, platforms and individual
  396. * drivers shall *NOT* touch pin control GPIO calls.
  397. */
  398. int pinctrl_gpio_direction_input(unsigned gpio)
  399. {
  400. int ret;
  401. mutex_lock(&pinctrl_mutex);
  402. ret = pinctrl_gpio_direction(gpio, true);
  403. mutex_unlock(&pinctrl_mutex);
  404. return ret;
  405. }
  406. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
  407. /**
  408. * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
  409. * @gpio: the GPIO pin number from the GPIO subsystem number space
  410. *
  411. * This function should *ONLY* be used from gpiolib-based GPIO drivers,
  412. * as part of their gpio_direction_output() semantics, platforms and individual
  413. * drivers shall *NOT* touch pin control GPIO calls.
  414. */
  415. int pinctrl_gpio_direction_output(unsigned gpio)
  416. {
  417. int ret;
  418. mutex_lock(&pinctrl_mutex);
  419. ret = pinctrl_gpio_direction(gpio, false);
  420. mutex_unlock(&pinctrl_mutex);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
  424. static struct pinctrl_state *find_state(struct pinctrl *p,
  425. const char *name)
  426. {
  427. struct pinctrl_state *state;
  428. list_for_each_entry(state, &p->states, node)
  429. if (!strcmp(state->name, name))
  430. return state;
  431. return NULL;
  432. }
  433. static struct pinctrl_state *create_state(struct pinctrl *p,
  434. const char *name)
  435. {
  436. struct pinctrl_state *state;
  437. state = kzalloc(sizeof(*state), GFP_KERNEL);
  438. if (state == NULL) {
  439. dev_err(p->dev,
  440. "failed to alloc struct pinctrl_state\n");
  441. return ERR_PTR(-ENOMEM);
  442. }
  443. state->name = name;
  444. INIT_LIST_HEAD(&state->settings);
  445. list_add_tail(&state->node, &p->states);
  446. return state;
  447. }
  448. static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
  449. {
  450. struct pinctrl_state *state;
  451. struct pinctrl_setting *setting;
  452. int ret;
  453. state = find_state(p, map->name);
  454. if (!state)
  455. state = create_state(p, map->name);
  456. if (IS_ERR(state))
  457. return PTR_ERR(state);
  458. if (map->type == PIN_MAP_TYPE_DUMMY_STATE)
  459. return 0;
  460. setting = kzalloc(sizeof(*setting), GFP_KERNEL);
  461. if (setting == NULL) {
  462. dev_err(p->dev,
  463. "failed to alloc struct pinctrl_setting\n");
  464. return -ENOMEM;
  465. }
  466. setting->type = map->type;
  467. setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
  468. if (setting->pctldev == NULL) {
  469. dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe",
  470. map->ctrl_dev_name);
  471. kfree(setting);
  472. /*
  473. * OK let us guess that the driver is not there yet, and
  474. * let's defer obtaining this pinctrl handle to later...
  475. */
  476. return -EPROBE_DEFER;
  477. }
  478. switch (map->type) {
  479. case PIN_MAP_TYPE_MUX_GROUP:
  480. ret = pinmux_map_to_setting(map, setting);
  481. break;
  482. case PIN_MAP_TYPE_CONFIGS_PIN:
  483. case PIN_MAP_TYPE_CONFIGS_GROUP:
  484. ret = pinconf_map_to_setting(map, setting);
  485. break;
  486. default:
  487. ret = -EINVAL;
  488. break;
  489. }
  490. if (ret < 0) {
  491. kfree(setting);
  492. return ret;
  493. }
  494. list_add_tail(&setting->node, &state->settings);
  495. return 0;
  496. }
  497. static struct pinctrl *find_pinctrl(struct device *dev)
  498. {
  499. struct pinctrl *p;
  500. list_for_each_entry(p, &pinctrl_list, node)
  501. if (p->dev == dev)
  502. return p;
  503. return NULL;
  504. }
  505. static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
  506. static struct pinctrl *create_pinctrl(struct device *dev)
  507. {
  508. struct pinctrl *p;
  509. const char *devname;
  510. struct pinctrl_maps *maps_node;
  511. int i;
  512. struct pinctrl_map const *map;
  513. int ret;
  514. /*
  515. * create the state cookie holder struct pinctrl for each
  516. * mapping, this is what consumers will get when requesting
  517. * a pin control handle with pinctrl_get()
  518. */
  519. p = kzalloc(sizeof(*p), GFP_KERNEL);
  520. if (p == NULL) {
  521. dev_err(dev, "failed to alloc struct pinctrl\n");
  522. return ERR_PTR(-ENOMEM);
  523. }
  524. p->dev = dev;
  525. INIT_LIST_HEAD(&p->states);
  526. INIT_LIST_HEAD(&p->dt_maps);
  527. ret = pinctrl_dt_to_map(p);
  528. if (ret < 0) {
  529. kfree(p);
  530. return ERR_PTR(ret);
  531. }
  532. devname = dev_name(dev);
  533. /* Iterate over the pin control maps to locate the right ones */
  534. for_each_maps(maps_node, i, map) {
  535. /* Map must be for this device */
  536. if (strcmp(map->dev_name, devname))
  537. continue;
  538. ret = add_setting(p, map);
  539. if (ret < 0) {
  540. pinctrl_put_locked(p, false);
  541. return ERR_PTR(ret);
  542. }
  543. }
  544. /* Add the pinmux to the global list */
  545. list_add_tail(&p->node, &pinctrl_list);
  546. return p;
  547. }
  548. static struct pinctrl *pinctrl_get_locked(struct device *dev)
  549. {
  550. struct pinctrl *p;
  551. if (WARN_ON(!dev))
  552. return ERR_PTR(-EINVAL);
  553. p = find_pinctrl(dev);
  554. if (p != NULL)
  555. return ERR_PTR(-EBUSY);
  556. p = create_pinctrl(dev);
  557. if (IS_ERR(p))
  558. return p;
  559. return p;
  560. }
  561. /**
  562. * pinctrl_get() - retrieves the pinctrl handle for a device
  563. * @dev: the device to obtain the handle for
  564. */
  565. struct pinctrl *pinctrl_get(struct device *dev)
  566. {
  567. struct pinctrl *p;
  568. mutex_lock(&pinctrl_mutex);
  569. p = pinctrl_get_locked(dev);
  570. mutex_unlock(&pinctrl_mutex);
  571. return p;
  572. }
  573. EXPORT_SYMBOL_GPL(pinctrl_get);
  574. static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
  575. {
  576. struct pinctrl_state *state, *n1;
  577. struct pinctrl_setting *setting, *n2;
  578. list_for_each_entry_safe(state, n1, &p->states, node) {
  579. list_for_each_entry_safe(setting, n2, &state->settings, node) {
  580. switch (setting->type) {
  581. case PIN_MAP_TYPE_MUX_GROUP:
  582. if (state == p->state)
  583. pinmux_disable_setting(setting);
  584. pinmux_free_setting(setting);
  585. break;
  586. case PIN_MAP_TYPE_CONFIGS_PIN:
  587. case PIN_MAP_TYPE_CONFIGS_GROUP:
  588. pinconf_free_setting(setting);
  589. break;
  590. default:
  591. break;
  592. }
  593. list_del(&setting->node);
  594. kfree(setting);
  595. }
  596. list_del(&state->node);
  597. kfree(state);
  598. }
  599. pinctrl_dt_free_maps(p);
  600. if (inlist)
  601. list_del(&p->node);
  602. kfree(p);
  603. }
  604. /**
  605. * pinctrl_put() - release a previously claimed pinctrl handle
  606. * @p: the pinctrl handle to release
  607. */
  608. void pinctrl_put(struct pinctrl *p)
  609. {
  610. mutex_lock(&pinctrl_mutex);
  611. pinctrl_put_locked(p, true);
  612. mutex_unlock(&pinctrl_mutex);
  613. }
  614. EXPORT_SYMBOL_GPL(pinctrl_put);
  615. static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
  616. const char *name)
  617. {
  618. struct pinctrl_state *state;
  619. state = find_state(p, name);
  620. if (!state) {
  621. if (pinctrl_dummy_state) {
  622. /* create dummy state */
  623. dev_dbg(p->dev, "using pinctrl dummy state (%s)\n",
  624. name);
  625. state = create_state(p, name);
  626. if (IS_ERR(state))
  627. return state;
  628. } else {
  629. return ERR_PTR(-ENODEV);
  630. }
  631. }
  632. return state;
  633. }
  634. /**
  635. * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
  636. * @p: the pinctrl handle to retrieve the state from
  637. * @name: the state name to retrieve
  638. */
  639. struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
  640. {
  641. struct pinctrl_state *s;
  642. mutex_lock(&pinctrl_mutex);
  643. s = pinctrl_lookup_state_locked(p, name);
  644. mutex_unlock(&pinctrl_mutex);
  645. return s;
  646. }
  647. EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
  648. static int pinctrl_select_state_locked(struct pinctrl *p,
  649. struct pinctrl_state *state)
  650. {
  651. struct pinctrl_setting *setting, *setting2;
  652. int ret;
  653. if (p->state == state)
  654. return 0;
  655. if (p->state) {
  656. /*
  657. * The set of groups with a mux configuration in the old state
  658. * may not be identical to the set of groups with a mux setting
  659. * in the new state. While this might be unusual, it's entirely
  660. * possible for the "user"-supplied mapping table to be written
  661. * that way. For each group that was configured in the old state
  662. * but not in the new state, this code puts that group into a
  663. * safe/disabled state.
  664. */
  665. list_for_each_entry(setting, &p->state->settings, node) {
  666. bool found = false;
  667. if (setting->type != PIN_MAP_TYPE_MUX_GROUP)
  668. continue;
  669. list_for_each_entry(setting2, &state->settings, node) {
  670. if (setting2->type != PIN_MAP_TYPE_MUX_GROUP)
  671. continue;
  672. if (setting2->data.mux.group ==
  673. setting->data.mux.group) {
  674. found = true;
  675. break;
  676. }
  677. }
  678. if (!found)
  679. pinmux_disable_setting(setting);
  680. }
  681. }
  682. p->state = state;
  683. /* Apply all the settings for the new state */
  684. list_for_each_entry(setting, &state->settings, node) {
  685. switch (setting->type) {
  686. case PIN_MAP_TYPE_MUX_GROUP:
  687. ret = pinmux_enable_setting(setting);
  688. break;
  689. case PIN_MAP_TYPE_CONFIGS_PIN:
  690. case PIN_MAP_TYPE_CONFIGS_GROUP:
  691. ret = pinconf_apply_setting(setting);
  692. break;
  693. default:
  694. ret = -EINVAL;
  695. break;
  696. }
  697. if (ret < 0) {
  698. /* FIXME: Difficult to return to prev state */
  699. return ret;
  700. }
  701. }
  702. return 0;
  703. }
  704. /**
  705. * pinctrl_select() - select/activate/program a pinctrl state to HW
  706. * @p: the pinctrl handle for the device that requests configuratio
  707. * @state: the state handle to select/activate/program
  708. */
  709. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  710. {
  711. int ret;
  712. mutex_lock(&pinctrl_mutex);
  713. ret = pinctrl_select_state_locked(p, state);
  714. mutex_unlock(&pinctrl_mutex);
  715. return ret;
  716. }
  717. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  718. static void devm_pinctrl_release(struct device *dev, void *res)
  719. {
  720. pinctrl_put(*(struct pinctrl **)res);
  721. }
  722. /**
  723. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  724. * @dev: the device to obtain the handle for
  725. *
  726. * If there is a need to explicitly destroy the returned struct pinctrl,
  727. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  728. */
  729. struct pinctrl *devm_pinctrl_get(struct device *dev)
  730. {
  731. struct pinctrl **ptr, *p;
  732. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  733. if (!ptr)
  734. return ERR_PTR(-ENOMEM);
  735. p = pinctrl_get(dev);
  736. if (!IS_ERR(p)) {
  737. *ptr = p;
  738. devres_add(dev, ptr);
  739. } else {
  740. devres_free(ptr);
  741. }
  742. return p;
  743. }
  744. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  745. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  746. {
  747. struct pinctrl **p = res;
  748. return *p == data;
  749. }
  750. /**
  751. * devm_pinctrl_put() - Resource managed pinctrl_put()
  752. * @p: the pinctrl handle to release
  753. *
  754. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  755. * this function will not need to be called and the resource management
  756. * code will ensure that the resource is freed.
  757. */
  758. void devm_pinctrl_put(struct pinctrl *p)
  759. {
  760. WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
  761. devm_pinctrl_match, p));
  762. pinctrl_put(p);
  763. }
  764. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  765. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  766. bool dup, bool locked)
  767. {
  768. int i, ret;
  769. struct pinctrl_maps *maps_node;
  770. pr_debug("add %d pinmux maps\n", num_maps);
  771. /* First sanity check the new mapping */
  772. for (i = 0; i < num_maps; i++) {
  773. if (!maps[i].dev_name) {
  774. pr_err("failed to register map %s (%d): no device given\n",
  775. maps[i].name, i);
  776. return -EINVAL;
  777. }
  778. if (!maps[i].name) {
  779. pr_err("failed to register map %d: no map name given\n",
  780. i);
  781. return -EINVAL;
  782. }
  783. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  784. !maps[i].ctrl_dev_name) {
  785. pr_err("failed to register map %s (%d): no pin control device given\n",
  786. maps[i].name, i);
  787. return -EINVAL;
  788. }
  789. switch (maps[i].type) {
  790. case PIN_MAP_TYPE_DUMMY_STATE:
  791. break;
  792. case PIN_MAP_TYPE_MUX_GROUP:
  793. ret = pinmux_validate_map(&maps[i], i);
  794. if (ret < 0)
  795. return ret;
  796. break;
  797. case PIN_MAP_TYPE_CONFIGS_PIN:
  798. case PIN_MAP_TYPE_CONFIGS_GROUP:
  799. ret = pinconf_validate_map(&maps[i], i);
  800. if (ret < 0)
  801. return ret;
  802. break;
  803. default:
  804. pr_err("failed to register map %s (%d): invalid type given\n",
  805. maps[i].name, i);
  806. return -EINVAL;
  807. }
  808. }
  809. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  810. if (!maps_node) {
  811. pr_err("failed to alloc struct pinctrl_maps\n");
  812. return -ENOMEM;
  813. }
  814. maps_node->num_maps = num_maps;
  815. if (dup) {
  816. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  817. GFP_KERNEL);
  818. if (!maps_node->maps) {
  819. pr_err("failed to duplicate mapping table\n");
  820. kfree(maps_node);
  821. return -ENOMEM;
  822. }
  823. } else {
  824. maps_node->maps = maps;
  825. }
  826. if (!locked)
  827. mutex_lock(&pinctrl_mutex);
  828. list_add_tail(&maps_node->node, &pinctrl_maps);
  829. if (!locked)
  830. mutex_unlock(&pinctrl_mutex);
  831. return 0;
  832. }
  833. /**
  834. * pinctrl_register_mappings() - register a set of pin controller mappings
  835. * @maps: the pincontrol mappings table to register. This should probably be
  836. * marked with __initdata so it can be discarded after boot. This
  837. * function will perform a shallow copy for the mapping entries.
  838. * @num_maps: the number of maps in the mapping table
  839. */
  840. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  841. unsigned num_maps)
  842. {
  843. return pinctrl_register_map(maps, num_maps, true, false);
  844. }
  845. void pinctrl_unregister_map(struct pinctrl_map const *map)
  846. {
  847. struct pinctrl_maps *maps_node;
  848. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  849. if (maps_node->maps == map) {
  850. list_del(&maps_node->node);
  851. return;
  852. }
  853. }
  854. }
  855. #ifdef CONFIG_DEBUG_FS
  856. static int pinctrl_pins_show(struct seq_file *s, void *what)
  857. {
  858. struct pinctrl_dev *pctldev = s->private;
  859. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  860. unsigned i, pin;
  861. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  862. mutex_lock(&pinctrl_mutex);
  863. /* The pin number can be retrived from the pin controller descriptor */
  864. for (i = 0; i < pctldev->desc->npins; i++) {
  865. struct pin_desc *desc;
  866. pin = pctldev->desc->pins[i].number;
  867. desc = pin_desc_get(pctldev, pin);
  868. /* Pin space may be sparse */
  869. if (desc == NULL)
  870. continue;
  871. seq_printf(s, "pin %d (%s) ", pin,
  872. desc->name ? desc->name : "unnamed");
  873. /* Driver-specific info per pin */
  874. if (ops->pin_dbg_show)
  875. ops->pin_dbg_show(pctldev, s, pin);
  876. seq_puts(s, "\n");
  877. }
  878. mutex_unlock(&pinctrl_mutex);
  879. return 0;
  880. }
  881. static int pinctrl_groups_show(struct seq_file *s, void *what)
  882. {
  883. struct pinctrl_dev *pctldev = s->private;
  884. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  885. unsigned ngroups, selector = 0;
  886. ngroups = ops->get_groups_count(pctldev);
  887. mutex_lock(&pinctrl_mutex);
  888. seq_puts(s, "registered pin groups:\n");
  889. while (selector < ngroups) {
  890. const unsigned *pins;
  891. unsigned num_pins;
  892. const char *gname = ops->get_group_name(pctldev, selector);
  893. const char *pname;
  894. int ret;
  895. int i;
  896. ret = ops->get_group_pins(pctldev, selector,
  897. &pins, &num_pins);
  898. if (ret)
  899. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  900. gname);
  901. else {
  902. seq_printf(s, "group: %s\n", gname);
  903. for (i = 0; i < num_pins; i++) {
  904. pname = pin_get_name(pctldev, pins[i]);
  905. if (WARN_ON(!pname))
  906. return -EINVAL;
  907. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  908. }
  909. seq_puts(s, "\n");
  910. }
  911. selector++;
  912. }
  913. mutex_unlock(&pinctrl_mutex);
  914. return 0;
  915. }
  916. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  917. {
  918. struct pinctrl_dev *pctldev = s->private;
  919. struct pinctrl_gpio_range *range = NULL;
  920. seq_puts(s, "GPIO ranges handled:\n");
  921. mutex_lock(&pinctrl_mutex);
  922. /* Loop over the ranges */
  923. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  924. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  925. range->id, range->name,
  926. range->base, (range->base + range->npins - 1),
  927. range->pin_base,
  928. (range->pin_base + range->npins - 1));
  929. }
  930. mutex_unlock(&pinctrl_mutex);
  931. return 0;
  932. }
  933. static int pinctrl_devices_show(struct seq_file *s, void *what)
  934. {
  935. struct pinctrl_dev *pctldev;
  936. seq_puts(s, "name [pinmux] [pinconf]\n");
  937. mutex_lock(&pinctrl_mutex);
  938. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  939. seq_printf(s, "%s ", pctldev->desc->name);
  940. if (pctldev->desc->pmxops)
  941. seq_puts(s, "yes ");
  942. else
  943. seq_puts(s, "no ");
  944. if (pctldev->desc->confops)
  945. seq_puts(s, "yes");
  946. else
  947. seq_puts(s, "no");
  948. seq_puts(s, "\n");
  949. }
  950. mutex_unlock(&pinctrl_mutex);
  951. return 0;
  952. }
  953. static inline const char *map_type(enum pinctrl_map_type type)
  954. {
  955. static const char * const names[] = {
  956. "INVALID",
  957. "DUMMY_STATE",
  958. "MUX_GROUP",
  959. "CONFIGS_PIN",
  960. "CONFIGS_GROUP",
  961. };
  962. if (type >= ARRAY_SIZE(names))
  963. return "UNKNOWN";
  964. return names[type];
  965. }
  966. static int pinctrl_maps_show(struct seq_file *s, void *what)
  967. {
  968. struct pinctrl_maps *maps_node;
  969. int i;
  970. struct pinctrl_map const *map;
  971. seq_puts(s, "Pinctrl maps:\n");
  972. mutex_lock(&pinctrl_mutex);
  973. for_each_maps(maps_node, i, map) {
  974. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  975. map->dev_name, map->name, map_type(map->type),
  976. map->type);
  977. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  978. seq_printf(s, "controlling device %s\n",
  979. map->ctrl_dev_name);
  980. switch (map->type) {
  981. case PIN_MAP_TYPE_MUX_GROUP:
  982. pinmux_show_map(s, map);
  983. break;
  984. case PIN_MAP_TYPE_CONFIGS_PIN:
  985. case PIN_MAP_TYPE_CONFIGS_GROUP:
  986. pinconf_show_map(s, map);
  987. break;
  988. default:
  989. break;
  990. }
  991. seq_printf(s, "\n");
  992. }
  993. mutex_unlock(&pinctrl_mutex);
  994. return 0;
  995. }
  996. static int pinctrl_show(struct seq_file *s, void *what)
  997. {
  998. struct pinctrl *p;
  999. struct pinctrl_state *state;
  1000. struct pinctrl_setting *setting;
  1001. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1002. mutex_lock(&pinctrl_mutex);
  1003. list_for_each_entry(p, &pinctrl_list, node) {
  1004. seq_printf(s, "device: %s current state: %s\n",
  1005. dev_name(p->dev),
  1006. p->state ? p->state->name : "none");
  1007. list_for_each_entry(state, &p->states, node) {
  1008. seq_printf(s, " state: %s\n", state->name);
  1009. list_for_each_entry(setting, &state->settings, node) {
  1010. struct pinctrl_dev *pctldev = setting->pctldev;
  1011. seq_printf(s, " type: %s controller %s ",
  1012. map_type(setting->type),
  1013. pinctrl_dev_get_name(pctldev));
  1014. switch (setting->type) {
  1015. case PIN_MAP_TYPE_MUX_GROUP:
  1016. pinmux_show_setting(s, setting);
  1017. break;
  1018. case PIN_MAP_TYPE_CONFIGS_PIN:
  1019. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1020. pinconf_show_setting(s, setting);
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. mutex_unlock(&pinctrl_mutex);
  1029. return 0;
  1030. }
  1031. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1032. {
  1033. return single_open(file, pinctrl_pins_show, inode->i_private);
  1034. }
  1035. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1036. {
  1037. return single_open(file, pinctrl_groups_show, inode->i_private);
  1038. }
  1039. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1040. {
  1041. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1042. }
  1043. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1044. {
  1045. return single_open(file, pinctrl_devices_show, NULL);
  1046. }
  1047. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1048. {
  1049. return single_open(file, pinctrl_maps_show, NULL);
  1050. }
  1051. static int pinctrl_open(struct inode *inode, struct file *file)
  1052. {
  1053. return single_open(file, pinctrl_show, NULL);
  1054. }
  1055. static const struct file_operations pinctrl_pins_ops = {
  1056. .open = pinctrl_pins_open,
  1057. .read = seq_read,
  1058. .llseek = seq_lseek,
  1059. .release = single_release,
  1060. };
  1061. static const struct file_operations pinctrl_groups_ops = {
  1062. .open = pinctrl_groups_open,
  1063. .read = seq_read,
  1064. .llseek = seq_lseek,
  1065. .release = single_release,
  1066. };
  1067. static const struct file_operations pinctrl_gpioranges_ops = {
  1068. .open = pinctrl_gpioranges_open,
  1069. .read = seq_read,
  1070. .llseek = seq_lseek,
  1071. .release = single_release,
  1072. };
  1073. static const struct file_operations pinctrl_devices_ops = {
  1074. .open = pinctrl_devices_open,
  1075. .read = seq_read,
  1076. .llseek = seq_lseek,
  1077. .release = single_release,
  1078. };
  1079. static const struct file_operations pinctrl_maps_ops = {
  1080. .open = pinctrl_maps_open,
  1081. .read = seq_read,
  1082. .llseek = seq_lseek,
  1083. .release = single_release,
  1084. };
  1085. static const struct file_operations pinctrl_ops = {
  1086. .open = pinctrl_open,
  1087. .read = seq_read,
  1088. .llseek = seq_lseek,
  1089. .release = single_release,
  1090. };
  1091. static struct dentry *debugfs_root;
  1092. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1093. {
  1094. struct dentry *device_root;
  1095. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1096. debugfs_root);
  1097. pctldev->device_root = device_root;
  1098. if (IS_ERR(device_root) || !device_root) {
  1099. pr_warn("failed to create debugfs directory for %s\n",
  1100. dev_name(pctldev->dev));
  1101. return;
  1102. }
  1103. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1104. device_root, pctldev, &pinctrl_pins_ops);
  1105. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1106. device_root, pctldev, &pinctrl_groups_ops);
  1107. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1108. device_root, pctldev, &pinctrl_gpioranges_ops);
  1109. pinmux_init_device_debugfs(device_root, pctldev);
  1110. pinconf_init_device_debugfs(device_root, pctldev);
  1111. }
  1112. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1113. {
  1114. debugfs_remove_recursive(pctldev->device_root);
  1115. }
  1116. static void pinctrl_init_debugfs(void)
  1117. {
  1118. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1119. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1120. pr_warn("failed to create debugfs directory\n");
  1121. debugfs_root = NULL;
  1122. return;
  1123. }
  1124. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1125. debugfs_root, NULL, &pinctrl_devices_ops);
  1126. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1127. debugfs_root, NULL, &pinctrl_maps_ops);
  1128. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1129. debugfs_root, NULL, &pinctrl_ops);
  1130. }
  1131. #else /* CONFIG_DEBUG_FS */
  1132. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1133. {
  1134. }
  1135. static void pinctrl_init_debugfs(void)
  1136. {
  1137. }
  1138. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1139. {
  1140. }
  1141. #endif
  1142. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1143. {
  1144. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1145. if (!ops ||
  1146. !ops->get_groups_count ||
  1147. !ops->get_group_name ||
  1148. !ops->get_group_pins)
  1149. return -EINVAL;
  1150. if (ops->dt_node_to_map && !ops->dt_free_map)
  1151. return -EINVAL;
  1152. return 0;
  1153. }
  1154. /**
  1155. * pinctrl_register() - register a pin controller device
  1156. * @pctldesc: descriptor for this pin controller
  1157. * @dev: parent device for this pin controller
  1158. * @driver_data: private pin controller data for this pin controller
  1159. */
  1160. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1161. struct device *dev, void *driver_data)
  1162. {
  1163. struct pinctrl_dev *pctldev;
  1164. int ret;
  1165. if (!pctldesc)
  1166. return NULL;
  1167. if (!pctldesc->name)
  1168. return NULL;
  1169. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1170. if (pctldev == NULL) {
  1171. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1172. return NULL;
  1173. }
  1174. /* Initialize pin control device struct */
  1175. pctldev->owner = pctldesc->owner;
  1176. pctldev->desc = pctldesc;
  1177. pctldev->driver_data = driver_data;
  1178. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1179. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1180. pctldev->dev = dev;
  1181. /* check core ops for sanity */
  1182. if (pinctrl_check_ops(pctldev)) {
  1183. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1184. goto out_err;
  1185. }
  1186. /* If we're implementing pinmuxing, check the ops for sanity */
  1187. if (pctldesc->pmxops) {
  1188. if (pinmux_check_ops(pctldev))
  1189. goto out_err;
  1190. }
  1191. /* If we're implementing pinconfig, check the ops for sanity */
  1192. if (pctldesc->confops) {
  1193. if (pinconf_check_ops(pctldev))
  1194. goto out_err;
  1195. }
  1196. /* Register all the pins */
  1197. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1198. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1199. if (ret) {
  1200. dev_err(dev, "error during pin registration\n");
  1201. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1202. pctldesc->npins);
  1203. goto out_err;
  1204. }
  1205. mutex_lock(&pinctrl_mutex);
  1206. list_add_tail(&pctldev->node, &pinctrldev_list);
  1207. pctldev->p = pinctrl_get_locked(pctldev->dev);
  1208. if (!IS_ERR(pctldev->p)) {
  1209. struct pinctrl_state *s =
  1210. pinctrl_lookup_state_locked(pctldev->p,
  1211. PINCTRL_STATE_DEFAULT);
  1212. if (IS_ERR(s)) {
  1213. dev_dbg(dev, "failed to lookup the default state\n");
  1214. } else {
  1215. if (pinctrl_select_state_locked(pctldev->p, s))
  1216. dev_err(dev,
  1217. "failed to select default state\n");
  1218. }
  1219. }
  1220. mutex_unlock(&pinctrl_mutex);
  1221. pinctrl_init_device_debugfs(pctldev);
  1222. return pctldev;
  1223. out_err:
  1224. kfree(pctldev);
  1225. return NULL;
  1226. }
  1227. EXPORT_SYMBOL_GPL(pinctrl_register);
  1228. /**
  1229. * pinctrl_unregister() - unregister pinmux
  1230. * @pctldev: pin controller to unregister
  1231. *
  1232. * Called by pinmux drivers to unregister a pinmux.
  1233. */
  1234. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1235. {
  1236. struct pinctrl_gpio_range *range, *n;
  1237. if (pctldev == NULL)
  1238. return;
  1239. pinctrl_remove_device_debugfs(pctldev);
  1240. mutex_lock(&pinctrl_mutex);
  1241. if (!IS_ERR(pctldev->p))
  1242. pinctrl_put_locked(pctldev->p, true);
  1243. /* TODO: check that no pinmuxes are still active? */
  1244. list_del(&pctldev->node);
  1245. /* Destroy descriptor tree */
  1246. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1247. pctldev->desc->npins);
  1248. /* remove gpio ranges map */
  1249. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1250. list_del(&range->node);
  1251. kfree(pctldev);
  1252. mutex_unlock(&pinctrl_mutex);
  1253. }
  1254. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1255. static int __init pinctrl_init(void)
  1256. {
  1257. pr_info("initialized pinctrl subsystem\n");
  1258. pinctrl_init_debugfs();
  1259. return 0;
  1260. }
  1261. /* init early since many drivers really need to initialized pinmux early */
  1262. core_initcall(pinctrl_init);