core.c 40 KB

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