core.c 42 KB

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