core.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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. /*
  829. * All we can do here is pinmux_disable_setting.
  830. * That means that some pins are muxed differently now
  831. * than they were before applying the setting (We can't
  832. * "unmux a pin"!), but it's not a big deal since the pins
  833. * are free to be muxed by another apply_setting.
  834. */
  835. if (setting2->type == PIN_MAP_TYPE_MUX_GROUP)
  836. pinmux_disable_setting(setting2);
  837. }
  838. /* There's no infinite recursive loop here because p->state is NULL */
  839. if (old_state)
  840. pinctrl_select_state_locked(p, old_state);
  841. return ret;
  842. }
  843. /**
  844. * pinctrl_select() - select/activate/program a pinctrl state to HW
  845. * @p: the pinctrl handle for the device that requests configuratio
  846. * @state: the state handle to select/activate/program
  847. */
  848. int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
  849. {
  850. int ret;
  851. mutex_lock(&pinctrl_mutex);
  852. ret = pinctrl_select_state_locked(p, state);
  853. mutex_unlock(&pinctrl_mutex);
  854. return ret;
  855. }
  856. EXPORT_SYMBOL_GPL(pinctrl_select_state);
  857. static void devm_pinctrl_release(struct device *dev, void *res)
  858. {
  859. pinctrl_put(*(struct pinctrl **)res);
  860. }
  861. /**
  862. * struct devm_pinctrl_get() - Resource managed pinctrl_get()
  863. * @dev: the device to obtain the handle for
  864. *
  865. * If there is a need to explicitly destroy the returned struct pinctrl,
  866. * devm_pinctrl_put() should be used, rather than plain pinctrl_put().
  867. */
  868. struct pinctrl *devm_pinctrl_get(struct device *dev)
  869. {
  870. struct pinctrl **ptr, *p;
  871. ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
  872. if (!ptr)
  873. return ERR_PTR(-ENOMEM);
  874. p = pinctrl_get(dev);
  875. if (!IS_ERR(p)) {
  876. *ptr = p;
  877. devres_add(dev, ptr);
  878. } else {
  879. devres_free(ptr);
  880. }
  881. return p;
  882. }
  883. EXPORT_SYMBOL_GPL(devm_pinctrl_get);
  884. static int devm_pinctrl_match(struct device *dev, void *res, void *data)
  885. {
  886. struct pinctrl **p = res;
  887. return *p == data;
  888. }
  889. /**
  890. * devm_pinctrl_put() - Resource managed pinctrl_put()
  891. * @p: the pinctrl handle to release
  892. *
  893. * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
  894. * this function will not need to be called and the resource management
  895. * code will ensure that the resource is freed.
  896. */
  897. void devm_pinctrl_put(struct pinctrl *p)
  898. {
  899. WARN_ON(devres_release(p->dev, devm_pinctrl_release,
  900. devm_pinctrl_match, p));
  901. }
  902. EXPORT_SYMBOL_GPL(devm_pinctrl_put);
  903. int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
  904. bool dup, bool locked)
  905. {
  906. int i, ret;
  907. struct pinctrl_maps *maps_node;
  908. pr_debug("add %d pinmux maps\n", num_maps);
  909. /* First sanity check the new mapping */
  910. for (i = 0; i < num_maps; i++) {
  911. if (!maps[i].dev_name) {
  912. pr_err("failed to register map %s (%d): no device given\n",
  913. maps[i].name, i);
  914. return -EINVAL;
  915. }
  916. if (!maps[i].name) {
  917. pr_err("failed to register map %d: no map name given\n",
  918. i);
  919. return -EINVAL;
  920. }
  921. if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE &&
  922. !maps[i].ctrl_dev_name) {
  923. pr_err("failed to register map %s (%d): no pin control device given\n",
  924. maps[i].name, i);
  925. return -EINVAL;
  926. }
  927. switch (maps[i].type) {
  928. case PIN_MAP_TYPE_DUMMY_STATE:
  929. break;
  930. case PIN_MAP_TYPE_MUX_GROUP:
  931. ret = pinmux_validate_map(&maps[i], i);
  932. if (ret < 0)
  933. return ret;
  934. break;
  935. case PIN_MAP_TYPE_CONFIGS_PIN:
  936. case PIN_MAP_TYPE_CONFIGS_GROUP:
  937. ret = pinconf_validate_map(&maps[i], i);
  938. if (ret < 0)
  939. return ret;
  940. break;
  941. default:
  942. pr_err("failed to register map %s (%d): invalid type given\n",
  943. maps[i].name, i);
  944. return -EINVAL;
  945. }
  946. }
  947. maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL);
  948. if (!maps_node) {
  949. pr_err("failed to alloc struct pinctrl_maps\n");
  950. return -ENOMEM;
  951. }
  952. maps_node->num_maps = num_maps;
  953. if (dup) {
  954. maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps,
  955. GFP_KERNEL);
  956. if (!maps_node->maps) {
  957. pr_err("failed to duplicate mapping table\n");
  958. kfree(maps_node);
  959. return -ENOMEM;
  960. }
  961. } else {
  962. maps_node->maps = maps;
  963. }
  964. if (!locked)
  965. mutex_lock(&pinctrl_mutex);
  966. list_add_tail(&maps_node->node, &pinctrl_maps);
  967. if (!locked)
  968. mutex_unlock(&pinctrl_mutex);
  969. return 0;
  970. }
  971. /**
  972. * pinctrl_register_mappings() - register a set of pin controller mappings
  973. * @maps: the pincontrol mappings table to register. This should probably be
  974. * marked with __initdata so it can be discarded after boot. This
  975. * function will perform a shallow copy for the mapping entries.
  976. * @num_maps: the number of maps in the mapping table
  977. */
  978. int pinctrl_register_mappings(struct pinctrl_map const *maps,
  979. unsigned num_maps)
  980. {
  981. return pinctrl_register_map(maps, num_maps, true, false);
  982. }
  983. void pinctrl_unregister_map(struct pinctrl_map const *map)
  984. {
  985. struct pinctrl_maps *maps_node;
  986. list_for_each_entry(maps_node, &pinctrl_maps, node) {
  987. if (maps_node->maps == map) {
  988. list_del(&maps_node->node);
  989. return;
  990. }
  991. }
  992. }
  993. /**
  994. * pinctrl_force_sleep() - turn a given controller device into sleep state
  995. * @pctldev: pin controller device
  996. */
  997. int pinctrl_force_sleep(struct pinctrl_dev *pctldev)
  998. {
  999. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep))
  1000. return pinctrl_select_state(pctldev->p, pctldev->hog_sleep);
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL_GPL(pinctrl_force_sleep);
  1004. /**
  1005. * pinctrl_force_default() - turn a given controller device into default state
  1006. * @pctldev: pin controller device
  1007. */
  1008. int pinctrl_force_default(struct pinctrl_dev *pctldev)
  1009. {
  1010. if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default))
  1011. return pinctrl_select_state(pctldev->p, pctldev->hog_default);
  1012. return 0;
  1013. }
  1014. EXPORT_SYMBOL_GPL(pinctrl_force_default);
  1015. #ifdef CONFIG_DEBUG_FS
  1016. static int pinctrl_pins_show(struct seq_file *s, void *what)
  1017. {
  1018. struct pinctrl_dev *pctldev = s->private;
  1019. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1020. unsigned i, pin;
  1021. seq_printf(s, "registered pins: %d\n", pctldev->desc->npins);
  1022. mutex_lock(&pinctrl_mutex);
  1023. /* The pin number can be retrived from the pin controller descriptor */
  1024. for (i = 0; i < pctldev->desc->npins; i++) {
  1025. struct pin_desc *desc;
  1026. pin = pctldev->desc->pins[i].number;
  1027. desc = pin_desc_get(pctldev, pin);
  1028. /* Pin space may be sparse */
  1029. if (desc == NULL)
  1030. continue;
  1031. seq_printf(s, "pin %d (%s) ", pin,
  1032. desc->name ? desc->name : "unnamed");
  1033. /* Driver-specific info per pin */
  1034. if (ops->pin_dbg_show)
  1035. ops->pin_dbg_show(pctldev, s, pin);
  1036. seq_puts(s, "\n");
  1037. }
  1038. mutex_unlock(&pinctrl_mutex);
  1039. return 0;
  1040. }
  1041. static int pinctrl_groups_show(struct seq_file *s, void *what)
  1042. {
  1043. struct pinctrl_dev *pctldev = s->private;
  1044. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1045. unsigned ngroups, selector = 0;
  1046. ngroups = ops->get_groups_count(pctldev);
  1047. mutex_lock(&pinctrl_mutex);
  1048. seq_puts(s, "registered pin groups:\n");
  1049. while (selector < ngroups) {
  1050. const unsigned *pins;
  1051. unsigned num_pins;
  1052. const char *gname = ops->get_group_name(pctldev, selector);
  1053. const char *pname;
  1054. int ret;
  1055. int i;
  1056. ret = ops->get_group_pins(pctldev, selector,
  1057. &pins, &num_pins);
  1058. if (ret)
  1059. seq_printf(s, "%s [ERROR GETTING PINS]\n",
  1060. gname);
  1061. else {
  1062. seq_printf(s, "group: %s\n", gname);
  1063. for (i = 0; i < num_pins; i++) {
  1064. pname = pin_get_name(pctldev, pins[i]);
  1065. if (WARN_ON(!pname)) {
  1066. mutex_unlock(&pinctrl_mutex);
  1067. return -EINVAL;
  1068. }
  1069. seq_printf(s, "pin %d (%s)\n", pins[i], pname);
  1070. }
  1071. seq_puts(s, "\n");
  1072. }
  1073. selector++;
  1074. }
  1075. mutex_unlock(&pinctrl_mutex);
  1076. return 0;
  1077. }
  1078. static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
  1079. {
  1080. struct pinctrl_dev *pctldev = s->private;
  1081. struct pinctrl_gpio_range *range = NULL;
  1082. seq_puts(s, "GPIO ranges handled:\n");
  1083. mutex_lock(&pinctrl_mutex);
  1084. /* Loop over the ranges */
  1085. list_for_each_entry(range, &pctldev->gpio_ranges, node) {
  1086. seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n",
  1087. range->id, range->name,
  1088. range->base, (range->base + range->npins - 1),
  1089. range->pin_base,
  1090. (range->pin_base + range->npins - 1));
  1091. }
  1092. mutex_unlock(&pinctrl_mutex);
  1093. return 0;
  1094. }
  1095. static int pinctrl_devices_show(struct seq_file *s, void *what)
  1096. {
  1097. struct pinctrl_dev *pctldev;
  1098. seq_puts(s, "name [pinmux] [pinconf]\n");
  1099. mutex_lock(&pinctrl_mutex);
  1100. list_for_each_entry(pctldev, &pinctrldev_list, node) {
  1101. seq_printf(s, "%s ", pctldev->desc->name);
  1102. if (pctldev->desc->pmxops)
  1103. seq_puts(s, "yes ");
  1104. else
  1105. seq_puts(s, "no ");
  1106. if (pctldev->desc->confops)
  1107. seq_puts(s, "yes");
  1108. else
  1109. seq_puts(s, "no");
  1110. seq_puts(s, "\n");
  1111. }
  1112. mutex_unlock(&pinctrl_mutex);
  1113. return 0;
  1114. }
  1115. static inline const char *map_type(enum pinctrl_map_type type)
  1116. {
  1117. static const char * const names[] = {
  1118. "INVALID",
  1119. "DUMMY_STATE",
  1120. "MUX_GROUP",
  1121. "CONFIGS_PIN",
  1122. "CONFIGS_GROUP",
  1123. };
  1124. if (type >= ARRAY_SIZE(names))
  1125. return "UNKNOWN";
  1126. return names[type];
  1127. }
  1128. static int pinctrl_maps_show(struct seq_file *s, void *what)
  1129. {
  1130. struct pinctrl_maps *maps_node;
  1131. int i;
  1132. struct pinctrl_map const *map;
  1133. seq_puts(s, "Pinctrl maps:\n");
  1134. mutex_lock(&pinctrl_mutex);
  1135. for_each_maps(maps_node, i, map) {
  1136. seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n",
  1137. map->dev_name, map->name, map_type(map->type),
  1138. map->type);
  1139. if (map->type != PIN_MAP_TYPE_DUMMY_STATE)
  1140. seq_printf(s, "controlling device %s\n",
  1141. map->ctrl_dev_name);
  1142. switch (map->type) {
  1143. case PIN_MAP_TYPE_MUX_GROUP:
  1144. pinmux_show_map(s, map);
  1145. break;
  1146. case PIN_MAP_TYPE_CONFIGS_PIN:
  1147. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1148. pinconf_show_map(s, map);
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. seq_printf(s, "\n");
  1154. }
  1155. mutex_unlock(&pinctrl_mutex);
  1156. return 0;
  1157. }
  1158. static int pinctrl_show(struct seq_file *s, void *what)
  1159. {
  1160. struct pinctrl *p;
  1161. struct pinctrl_state *state;
  1162. struct pinctrl_setting *setting;
  1163. seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
  1164. mutex_lock(&pinctrl_mutex);
  1165. list_for_each_entry(p, &pinctrl_list, node) {
  1166. seq_printf(s, "device: %s current state: %s\n",
  1167. dev_name(p->dev),
  1168. p->state ? p->state->name : "none");
  1169. list_for_each_entry(state, &p->states, node) {
  1170. seq_printf(s, " state: %s\n", state->name);
  1171. list_for_each_entry(setting, &state->settings, node) {
  1172. struct pinctrl_dev *pctldev = setting->pctldev;
  1173. seq_printf(s, " type: %s controller %s ",
  1174. map_type(setting->type),
  1175. pinctrl_dev_get_name(pctldev));
  1176. switch (setting->type) {
  1177. case PIN_MAP_TYPE_MUX_GROUP:
  1178. pinmux_show_setting(s, setting);
  1179. break;
  1180. case PIN_MAP_TYPE_CONFIGS_PIN:
  1181. case PIN_MAP_TYPE_CONFIGS_GROUP:
  1182. pinconf_show_setting(s, setting);
  1183. break;
  1184. default:
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. mutex_unlock(&pinctrl_mutex);
  1191. return 0;
  1192. }
  1193. static int pinctrl_pins_open(struct inode *inode, struct file *file)
  1194. {
  1195. return single_open(file, pinctrl_pins_show, inode->i_private);
  1196. }
  1197. static int pinctrl_groups_open(struct inode *inode, struct file *file)
  1198. {
  1199. return single_open(file, pinctrl_groups_show, inode->i_private);
  1200. }
  1201. static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
  1202. {
  1203. return single_open(file, pinctrl_gpioranges_show, inode->i_private);
  1204. }
  1205. static int pinctrl_devices_open(struct inode *inode, struct file *file)
  1206. {
  1207. return single_open(file, pinctrl_devices_show, NULL);
  1208. }
  1209. static int pinctrl_maps_open(struct inode *inode, struct file *file)
  1210. {
  1211. return single_open(file, pinctrl_maps_show, NULL);
  1212. }
  1213. static int pinctrl_open(struct inode *inode, struct file *file)
  1214. {
  1215. return single_open(file, pinctrl_show, NULL);
  1216. }
  1217. static const struct file_operations pinctrl_pins_ops = {
  1218. .open = pinctrl_pins_open,
  1219. .read = seq_read,
  1220. .llseek = seq_lseek,
  1221. .release = single_release,
  1222. };
  1223. static const struct file_operations pinctrl_groups_ops = {
  1224. .open = pinctrl_groups_open,
  1225. .read = seq_read,
  1226. .llseek = seq_lseek,
  1227. .release = single_release,
  1228. };
  1229. static const struct file_operations pinctrl_gpioranges_ops = {
  1230. .open = pinctrl_gpioranges_open,
  1231. .read = seq_read,
  1232. .llseek = seq_lseek,
  1233. .release = single_release,
  1234. };
  1235. static const struct file_operations pinctrl_devices_ops = {
  1236. .open = pinctrl_devices_open,
  1237. .read = seq_read,
  1238. .llseek = seq_lseek,
  1239. .release = single_release,
  1240. };
  1241. static const struct file_operations pinctrl_maps_ops = {
  1242. .open = pinctrl_maps_open,
  1243. .read = seq_read,
  1244. .llseek = seq_lseek,
  1245. .release = single_release,
  1246. };
  1247. static const struct file_operations pinctrl_ops = {
  1248. .open = pinctrl_open,
  1249. .read = seq_read,
  1250. .llseek = seq_lseek,
  1251. .release = single_release,
  1252. };
  1253. static struct dentry *debugfs_root;
  1254. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1255. {
  1256. struct dentry *device_root;
  1257. device_root = debugfs_create_dir(dev_name(pctldev->dev),
  1258. debugfs_root);
  1259. pctldev->device_root = device_root;
  1260. if (IS_ERR(device_root) || !device_root) {
  1261. pr_warn("failed to create debugfs directory for %s\n",
  1262. dev_name(pctldev->dev));
  1263. return;
  1264. }
  1265. debugfs_create_file("pins", S_IFREG | S_IRUGO,
  1266. device_root, pctldev, &pinctrl_pins_ops);
  1267. debugfs_create_file("pingroups", S_IFREG | S_IRUGO,
  1268. device_root, pctldev, &pinctrl_groups_ops);
  1269. debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
  1270. device_root, pctldev, &pinctrl_gpioranges_ops);
  1271. pinmux_init_device_debugfs(device_root, pctldev);
  1272. pinconf_init_device_debugfs(device_root, pctldev);
  1273. }
  1274. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1275. {
  1276. debugfs_remove_recursive(pctldev->device_root);
  1277. }
  1278. static void pinctrl_init_debugfs(void)
  1279. {
  1280. debugfs_root = debugfs_create_dir("pinctrl", NULL);
  1281. if (IS_ERR(debugfs_root) || !debugfs_root) {
  1282. pr_warn("failed to create debugfs directory\n");
  1283. debugfs_root = NULL;
  1284. return;
  1285. }
  1286. debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
  1287. debugfs_root, NULL, &pinctrl_devices_ops);
  1288. debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
  1289. debugfs_root, NULL, &pinctrl_maps_ops);
  1290. debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
  1291. debugfs_root, NULL, &pinctrl_ops);
  1292. }
  1293. #else /* CONFIG_DEBUG_FS */
  1294. static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
  1295. {
  1296. }
  1297. static void pinctrl_init_debugfs(void)
  1298. {
  1299. }
  1300. static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev)
  1301. {
  1302. }
  1303. #endif
  1304. static int pinctrl_check_ops(struct pinctrl_dev *pctldev)
  1305. {
  1306. const struct pinctrl_ops *ops = pctldev->desc->pctlops;
  1307. if (!ops ||
  1308. !ops->get_groups_count ||
  1309. !ops->get_group_name ||
  1310. !ops->get_group_pins)
  1311. return -EINVAL;
  1312. if (ops->dt_node_to_map && !ops->dt_free_map)
  1313. return -EINVAL;
  1314. return 0;
  1315. }
  1316. /**
  1317. * pinctrl_register() - register a pin controller device
  1318. * @pctldesc: descriptor for this pin controller
  1319. * @dev: parent device for this pin controller
  1320. * @driver_data: private pin controller data for this pin controller
  1321. */
  1322. struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
  1323. struct device *dev, void *driver_data)
  1324. {
  1325. struct pinctrl_dev *pctldev;
  1326. int ret;
  1327. if (!pctldesc)
  1328. return NULL;
  1329. if (!pctldesc->name)
  1330. return NULL;
  1331. pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
  1332. if (pctldev == NULL) {
  1333. dev_err(dev, "failed to alloc struct pinctrl_dev\n");
  1334. return NULL;
  1335. }
  1336. /* Initialize pin control device struct */
  1337. pctldev->owner = pctldesc->owner;
  1338. pctldev->desc = pctldesc;
  1339. pctldev->driver_data = driver_data;
  1340. INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
  1341. INIT_LIST_HEAD(&pctldev->gpio_ranges);
  1342. pctldev->dev = dev;
  1343. /* check core ops for sanity */
  1344. if (pinctrl_check_ops(pctldev)) {
  1345. dev_err(dev, "pinctrl ops lacks necessary functions\n");
  1346. goto out_err;
  1347. }
  1348. /* If we're implementing pinmuxing, check the ops for sanity */
  1349. if (pctldesc->pmxops) {
  1350. if (pinmux_check_ops(pctldev))
  1351. goto out_err;
  1352. }
  1353. /* If we're implementing pinconfig, check the ops for sanity */
  1354. if (pctldesc->confops) {
  1355. if (pinconf_check_ops(pctldev))
  1356. goto out_err;
  1357. }
  1358. /* Register all the pins */
  1359. dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins);
  1360. ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
  1361. if (ret) {
  1362. dev_err(dev, "error during pin registration\n");
  1363. pinctrl_free_pindescs(pctldev, pctldesc->pins,
  1364. pctldesc->npins);
  1365. goto out_err;
  1366. }
  1367. mutex_lock(&pinctrl_mutex);
  1368. list_add_tail(&pctldev->node, &pinctrldev_list);
  1369. pctldev->p = pinctrl_get_locked(pctldev->dev);
  1370. if (!IS_ERR(pctldev->p)) {
  1371. pctldev->hog_default =
  1372. pinctrl_lookup_state_locked(pctldev->p,
  1373. PINCTRL_STATE_DEFAULT);
  1374. if (IS_ERR(pctldev->hog_default)) {
  1375. dev_dbg(dev, "failed to lookup the default state\n");
  1376. } else {
  1377. if (pinctrl_select_state_locked(pctldev->p,
  1378. pctldev->hog_default))
  1379. dev_err(dev,
  1380. "failed to select default state\n");
  1381. }
  1382. pctldev->hog_sleep =
  1383. pinctrl_lookup_state_locked(pctldev->p,
  1384. PINCTRL_STATE_SLEEP);
  1385. if (IS_ERR(pctldev->hog_sleep))
  1386. dev_dbg(dev, "failed to lookup the sleep state\n");
  1387. }
  1388. mutex_unlock(&pinctrl_mutex);
  1389. pinctrl_init_device_debugfs(pctldev);
  1390. return pctldev;
  1391. out_err:
  1392. kfree(pctldev);
  1393. return NULL;
  1394. }
  1395. EXPORT_SYMBOL_GPL(pinctrl_register);
  1396. /**
  1397. * pinctrl_unregister() - unregister pinmux
  1398. * @pctldev: pin controller to unregister
  1399. *
  1400. * Called by pinmux drivers to unregister a pinmux.
  1401. */
  1402. void pinctrl_unregister(struct pinctrl_dev *pctldev)
  1403. {
  1404. struct pinctrl_gpio_range *range, *n;
  1405. if (pctldev == NULL)
  1406. return;
  1407. pinctrl_remove_device_debugfs(pctldev);
  1408. mutex_lock(&pinctrl_mutex);
  1409. if (!IS_ERR(pctldev->p))
  1410. pinctrl_put_locked(pctldev->p, true);
  1411. /* TODO: check that no pinmuxes are still active? */
  1412. list_del(&pctldev->node);
  1413. /* Destroy descriptor tree */
  1414. pinctrl_free_pindescs(pctldev, pctldev->desc->pins,
  1415. pctldev->desc->npins);
  1416. /* remove gpio ranges map */
  1417. list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node)
  1418. list_del(&range->node);
  1419. kfree(pctldev);
  1420. mutex_unlock(&pinctrl_mutex);
  1421. }
  1422. EXPORT_SYMBOL_GPL(pinctrl_unregister);
  1423. static int __init pinctrl_init(void)
  1424. {
  1425. pr_info("initialized pinctrl subsystem\n");
  1426. pinctrl_init_debugfs();
  1427. return 0;
  1428. }
  1429. /* init early since many drivers really need to initialized pinmux early */
  1430. core_initcall(pinctrl_init);