core.c 37 KB

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