core.c 45 KB

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