gpiolib-of.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * OF helpers for the GPIO API
  4. *
  5. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  6. *
  7. * Author: Anton Vorontsov <[email protected]>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/slab.h>
  20. #include <linux/gpio/machine.h>
  21. #include "gpiolib.h"
  22. #include "gpiolib-of.h"
  23. /**
  24. * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
  25. * @dev: Consuming device
  26. * @con_id: Function within the GPIO consumer
  27. *
  28. * Some elder GPIO controllers need special quirks. Currently we handle
  29. * the Freescale and PPC GPIO controller with bindings that doesn't use the
  30. * established "cs-gpios" for chip selects but instead rely on
  31. * "gpios" for the chip select lines. If we detect this, we redirect
  32. * the counting of "cs-gpios" to count "gpios" transparent to the
  33. * driver.
  34. */
  35. static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
  36. {
  37. struct device_node *np = dev->of_node;
  38. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  39. return 0;
  40. if (!con_id || strcmp(con_id, "cs"))
  41. return 0;
  42. if (!of_device_is_compatible(np, "fsl,spi") &&
  43. !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
  44. !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
  45. return 0;
  46. return of_gpio_named_count(np, "gpios");
  47. }
  48. /*
  49. * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
  50. *
  51. * FIXME: get rid of those external users by converting them to GPIO
  52. * descriptors and let them all use gpiod_count()
  53. */
  54. int of_gpio_get_count(struct device *dev, const char *con_id)
  55. {
  56. int ret;
  57. char propname[32];
  58. unsigned int i;
  59. ret = of_gpio_spi_cs_get_count(dev, con_id);
  60. if (ret > 0)
  61. return ret;
  62. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  63. if (con_id)
  64. snprintf(propname, sizeof(propname), "%s-%s",
  65. con_id, gpio_suffixes[i]);
  66. else
  67. snprintf(propname, sizeof(propname), "%s",
  68. gpio_suffixes[i]);
  69. ret = of_gpio_named_count(dev->of_node, propname);
  70. if (ret > 0)
  71. break;
  72. }
  73. return ret ? ret : -ENOENT;
  74. }
  75. static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
  76. {
  77. struct of_phandle_args *gpiospec = data;
  78. return chip->gpiodev->dev.of_node == gpiospec->np &&
  79. chip->of_xlate &&
  80. chip->of_xlate(chip, gpiospec, NULL) >= 0;
  81. }
  82. static struct gpio_chip *of_find_gpiochip_by_xlate(
  83. struct of_phandle_args *gpiospec)
  84. {
  85. return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
  86. }
  87. static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
  88. struct of_phandle_args *gpiospec,
  89. enum of_gpio_flags *flags)
  90. {
  91. int ret;
  92. if (chip->of_gpio_n_cells != gpiospec->args_count)
  93. return ERR_PTR(-EINVAL);
  94. ret = chip->of_xlate(chip, gpiospec, flags);
  95. if (ret < 0)
  96. return ERR_PTR(ret);
  97. return gpiochip_get_desc(chip, ret);
  98. }
  99. /**
  100. * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
  101. * to set the .valid_mask
  102. * @gc: the target gpio_chip
  103. *
  104. * Return: true if the valid mask needs to be set
  105. */
  106. bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
  107. {
  108. int size;
  109. const struct device_node *np = gc->of_node;
  110. size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
  111. if (size > 0 && size % 2 == 0)
  112. return true;
  113. return false;
  114. }
  115. static void of_gpio_flags_quirks(const struct device_node *np,
  116. const char *propname,
  117. enum of_gpio_flags *flags,
  118. int index)
  119. {
  120. /*
  121. * Some GPIO fixed regulator quirks.
  122. * Note that active low is the default.
  123. */
  124. if (IS_ENABLED(CONFIG_REGULATOR) &&
  125. (of_device_is_compatible(np, "regulator-fixed") ||
  126. of_device_is_compatible(np, "reg-fixed-voltage") ||
  127. (!(strcmp(propname, "enable-gpio") &&
  128. strcmp(propname, "enable-gpios")) &&
  129. of_device_is_compatible(np, "regulator-gpio")))) {
  130. bool active_low = !of_property_read_bool(np,
  131. "enable-active-high");
  132. /*
  133. * The regulator GPIO handles are specified such that the
  134. * presence or absence of "enable-active-high" solely controls
  135. * the polarity of the GPIO line. Any phandle flags must
  136. * be actively ignored.
  137. */
  138. if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
  139. pr_warn("%s GPIO handle specifies active low - ignored\n",
  140. of_node_full_name(np));
  141. *flags &= ~OF_GPIO_ACTIVE_LOW;
  142. }
  143. if (active_low)
  144. *flags |= OF_GPIO_ACTIVE_LOW;
  145. }
  146. /*
  147. * Legacy open drain handling for fixed voltage regulators.
  148. */
  149. if (IS_ENABLED(CONFIG_REGULATOR) &&
  150. of_device_is_compatible(np, "reg-fixed-voltage") &&
  151. of_property_read_bool(np, "gpio-open-drain")) {
  152. *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
  153. pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
  154. of_node_full_name(np));
  155. }
  156. /*
  157. * Legacy handling of SPI active high chip select. If we have a
  158. * property named "cs-gpios" we need to inspect the child node
  159. * to determine if the flags should have inverted semantics.
  160. */
  161. if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
  162. of_property_read_bool(np, "cs-gpios")) {
  163. struct device_node *child;
  164. u32 cs;
  165. int ret;
  166. for_each_child_of_node(np, child) {
  167. ret = of_property_read_u32(child, "reg", &cs);
  168. if (ret)
  169. continue;
  170. if (cs == index) {
  171. /*
  172. * SPI children have active low chip selects
  173. * by default. This can be specified negatively
  174. * by just omitting "spi-cs-high" in the
  175. * device node, or actively by tagging on
  176. * GPIO_ACTIVE_LOW as flag in the device
  177. * tree. If the line is simultaneously
  178. * tagged as active low in the device tree
  179. * and has the "spi-cs-high" set, we get a
  180. * conflict and the "spi-cs-high" flag will
  181. * take precedence.
  182. */
  183. if (of_property_read_bool(child, "spi-cs-high")) {
  184. if (*flags & OF_GPIO_ACTIVE_LOW) {
  185. pr_warn("%s GPIO handle specifies active low - ignored\n",
  186. of_node_full_name(child));
  187. *flags &= ~OF_GPIO_ACTIVE_LOW;
  188. }
  189. } else {
  190. if (!(*flags & OF_GPIO_ACTIVE_LOW))
  191. pr_info("%s enforce active low on chipselect handle\n",
  192. of_node_full_name(child));
  193. *flags |= OF_GPIO_ACTIVE_LOW;
  194. }
  195. of_node_put(child);
  196. break;
  197. }
  198. }
  199. }
  200. /* Legacy handling of stmmac's active-low PHY reset line */
  201. if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
  202. !strcmp(propname, "snps,reset-gpio") &&
  203. of_property_read_bool(np, "snps,reset-active-low"))
  204. *flags |= OF_GPIO_ACTIVE_LOW;
  205. }
  206. /**
  207. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  208. * @np: device node to get GPIO from
  209. * @propname: property name containing gpio specifier(s)
  210. * @index: index of the GPIO
  211. * @flags: a flags pointer to fill in
  212. *
  213. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  214. * value on the error condition. If @flags is not NULL the function also fills
  215. * in flags for the GPIO.
  216. */
  217. static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
  218. const char *propname, int index, enum of_gpio_flags *flags)
  219. {
  220. struct of_phandle_args gpiospec;
  221. struct gpio_chip *chip;
  222. struct gpio_desc *desc;
  223. int ret;
  224. ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
  225. &gpiospec);
  226. if (ret) {
  227. pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
  228. __func__, propname, np, index);
  229. return ERR_PTR(ret);
  230. }
  231. chip = of_find_gpiochip_by_xlate(&gpiospec);
  232. if (!chip) {
  233. desc = ERR_PTR(-EPROBE_DEFER);
  234. goto out;
  235. }
  236. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
  237. if (IS_ERR(desc))
  238. goto out;
  239. if (flags)
  240. of_gpio_flags_quirks(np, propname, flags, index);
  241. pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
  242. __func__, propname, np, index,
  243. PTR_ERR_OR_ZERO(desc));
  244. out:
  245. of_node_put(gpiospec.np);
  246. return desc;
  247. }
  248. int of_get_named_gpio_flags(const struct device_node *np, const char *list_name,
  249. int index, enum of_gpio_flags *flags)
  250. {
  251. struct gpio_desc *desc;
  252. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  253. if (IS_ERR(desc))
  254. return PTR_ERR(desc);
  255. else
  256. return desc_to_gpio(desc);
  257. }
  258. EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
  259. /* Converts gpio_lookup_flags into bitmask of GPIO_* values */
  260. static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags)
  261. {
  262. unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  263. if (flags & OF_GPIO_ACTIVE_LOW)
  264. lflags |= GPIO_ACTIVE_LOW;
  265. if (flags & OF_GPIO_SINGLE_ENDED) {
  266. if (flags & OF_GPIO_OPEN_DRAIN)
  267. lflags |= GPIO_OPEN_DRAIN;
  268. else
  269. lflags |= GPIO_OPEN_SOURCE;
  270. }
  271. if (flags & OF_GPIO_TRANSITORY)
  272. lflags |= GPIO_TRANSITORY;
  273. if (flags & OF_GPIO_PULL_UP)
  274. lflags |= GPIO_PULL_UP;
  275. if (flags & OF_GPIO_PULL_DOWN)
  276. lflags |= GPIO_PULL_DOWN;
  277. if (flags & OF_GPIO_PULL_DISABLE)
  278. lflags |= GPIO_PULL_DISABLE;
  279. return lflags;
  280. }
  281. /**
  282. * gpiod_get_from_of_node() - obtain a GPIO from an OF node
  283. * @node: handle of the OF node
  284. * @propname: name of the DT property representing the GPIO
  285. * @index: index of the GPIO to obtain for the consumer
  286. * @dflags: GPIO initialization flags
  287. * @label: label to attach to the requested GPIO
  288. *
  289. * Returns:
  290. * On successful request the GPIO pin is configured in accordance with
  291. * provided @dflags.
  292. *
  293. * In case of error an ERR_PTR() is returned.
  294. */
  295. struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
  296. const char *propname, int index,
  297. enum gpiod_flags dflags,
  298. const char *label)
  299. {
  300. unsigned long lflags;
  301. struct gpio_desc *desc;
  302. enum of_gpio_flags of_flags;
  303. int ret;
  304. desc = of_get_named_gpiod_flags(node, propname, index, &of_flags);
  305. if (!desc || IS_ERR(desc))
  306. return desc;
  307. ret = gpiod_request(desc, label);
  308. if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
  309. return desc;
  310. if (ret)
  311. return ERR_PTR(ret);
  312. lflags = of_convert_gpio_flags(of_flags);
  313. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  314. if (ret < 0) {
  315. gpiod_put(desc);
  316. return ERR_PTR(ret);
  317. }
  318. return desc;
  319. }
  320. EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
  321. /*
  322. * The SPI GPIO bindings happened before we managed to establish that GPIO
  323. * properties should be named "foo-gpios" so we have this special kludge for
  324. * them.
  325. */
  326. static struct gpio_desc *of_find_spi_gpio(struct device_node *np,
  327. const char *con_id,
  328. unsigned int idx,
  329. enum of_gpio_flags *of_flags)
  330. {
  331. char prop_name[32]; /* 32 is max size of property name */
  332. /*
  333. * Hopefully the compiler stubs the rest of the function if this
  334. * is false.
  335. */
  336. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  337. return ERR_PTR(-ENOENT);
  338. /* Allow this specifically for "spi-gpio" devices */
  339. if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
  340. return ERR_PTR(-ENOENT);
  341. /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
  342. snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
  343. return of_get_named_gpiod_flags(np, prop_name, idx, of_flags);
  344. }
  345. /*
  346. * The old Freescale bindings use simply "gpios" as name for the chip select
  347. * lines rather than "cs-gpios" like all other SPI hardware. Account for this
  348. * with a special quirk.
  349. */
  350. static struct gpio_desc *of_find_spi_cs_gpio(struct device_node *np,
  351. const char *con_id,
  352. unsigned int idx,
  353. enum of_gpio_flags *of_flags)
  354. {
  355. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  356. return ERR_PTR(-ENOENT);
  357. /* Allow this specifically for Freescale and PPC devices */
  358. if (!of_device_is_compatible(np, "fsl,spi") &&
  359. !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
  360. !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
  361. return ERR_PTR(-ENOENT);
  362. /* Allow only if asking for "cs-gpios" */
  363. if (!con_id || strcmp(con_id, "cs"))
  364. return ERR_PTR(-ENOENT);
  365. /*
  366. * While all other SPI controllers use "cs-gpios" the Freescale
  367. * uses just "gpios" so translate to that when "cs-gpios" is
  368. * requested.
  369. */
  370. return of_get_named_gpiod_flags(np, "gpios", idx, of_flags);
  371. }
  372. /*
  373. * Some regulator bindings happened before we managed to establish that GPIO
  374. * properties should be named "foo-gpios" so we have this special kludge for
  375. * them.
  376. */
  377. static struct gpio_desc *of_find_regulator_gpio(struct device_node *np,
  378. const char *con_id,
  379. unsigned int idx,
  380. enum of_gpio_flags *of_flags)
  381. {
  382. /* These are the connection IDs we accept as legacy GPIO phandles */
  383. const char *whitelist[] = {
  384. "wlf,ldoena", /* Arizona */
  385. "wlf,ldo1ena", /* WM8994 */
  386. "wlf,ldo2ena", /* WM8994 */
  387. };
  388. int i;
  389. if (!IS_ENABLED(CONFIG_REGULATOR))
  390. return ERR_PTR(-ENOENT);
  391. if (!con_id)
  392. return ERR_PTR(-ENOENT);
  393. i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
  394. if (i < 0)
  395. return ERR_PTR(-ENOENT);
  396. return of_get_named_gpiod_flags(np, con_id, idx, of_flags);
  397. }
  398. static struct gpio_desc *of_find_arizona_gpio(struct device_node *np,
  399. const char *con_id,
  400. unsigned int idx,
  401. enum of_gpio_flags *of_flags)
  402. {
  403. if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
  404. return ERR_PTR(-ENOENT);
  405. if (!con_id || strcmp(con_id, "wlf,reset"))
  406. return ERR_PTR(-ENOENT);
  407. return of_get_named_gpiod_flags(np, con_id, idx, of_flags);
  408. }
  409. static struct gpio_desc *of_find_usb_gpio(struct device_node *np,
  410. const char *con_id,
  411. unsigned int idx,
  412. enum of_gpio_flags *of_flags)
  413. {
  414. /*
  415. * Currently this USB quirk is only for the Fairchild FUSB302 host
  416. * which is using an undocumented DT GPIO line named "fcs,int_n"
  417. * without the compulsory "-gpios" suffix.
  418. */
  419. if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
  420. return ERR_PTR(-ENOENT);
  421. if (!con_id || strcmp(con_id, "fcs,int_n"))
  422. return ERR_PTR(-ENOENT);
  423. return of_get_named_gpiod_flags(np, con_id, idx, of_flags);
  424. }
  425. typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np,
  426. const char *con_id,
  427. unsigned int idx,
  428. enum of_gpio_flags *of_flags);
  429. static const of_find_gpio_quirk of_find_gpio_quirks[] = {
  430. of_find_spi_gpio,
  431. of_find_spi_cs_gpio,
  432. of_find_regulator_gpio,
  433. of_find_arizona_gpio,
  434. of_find_usb_gpio,
  435. NULL
  436. };
  437. struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  438. unsigned int idx, unsigned long *flags)
  439. {
  440. char prop_name[32]; /* 32 is max size of property name */
  441. enum of_gpio_flags of_flags;
  442. const of_find_gpio_quirk *q;
  443. struct gpio_desc *desc;
  444. unsigned int i;
  445. /* Try GPIO property "foo-gpios" and "foo-gpio" */
  446. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  447. if (con_id)
  448. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  449. gpio_suffixes[i]);
  450. else
  451. snprintf(prop_name, sizeof(prop_name), "%s",
  452. gpio_suffixes[i]);
  453. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  454. &of_flags);
  455. if (!gpiod_not_found(desc))
  456. break;
  457. }
  458. /* Properly named GPIO was not found, try workarounds */
  459. for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++)
  460. desc = (*q)(dev->of_node, con_id, idx, &of_flags);
  461. if (IS_ERR(desc))
  462. return desc;
  463. *flags = of_convert_gpio_flags(of_flags);
  464. return desc;
  465. }
  466. /**
  467. * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  468. * @np: device node to get GPIO from
  469. * @chip: GPIO chip whose hog is parsed
  470. * @idx: Index of the GPIO to parse
  471. * @name: GPIO line name
  472. * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
  473. * of_find_gpio() or of_parse_own_gpio()
  474. * @dflags: gpiod_flags - optional GPIO initialization flags
  475. *
  476. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  477. * value on the error condition.
  478. */
  479. static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
  480. struct gpio_chip *chip,
  481. unsigned int idx, const char **name,
  482. unsigned long *lflags,
  483. enum gpiod_flags *dflags)
  484. {
  485. struct device_node *chip_np;
  486. enum of_gpio_flags xlate_flags;
  487. struct of_phandle_args gpiospec;
  488. struct gpio_desc *desc;
  489. unsigned int i;
  490. u32 tmp;
  491. int ret;
  492. chip_np = chip->of_node;
  493. if (!chip_np)
  494. return ERR_PTR(-EINVAL);
  495. xlate_flags = 0;
  496. *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  497. *dflags = GPIOD_ASIS;
  498. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  499. if (ret)
  500. return ERR_PTR(ret);
  501. gpiospec.np = chip_np;
  502. gpiospec.args_count = tmp;
  503. for (i = 0; i < tmp; i++) {
  504. ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
  505. &gpiospec.args[i]);
  506. if (ret)
  507. return ERR_PTR(ret);
  508. }
  509. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
  510. if (IS_ERR(desc))
  511. return desc;
  512. *lflags = of_convert_gpio_flags(xlate_flags);
  513. if (of_property_read_bool(np, "input"))
  514. *dflags |= GPIOD_IN;
  515. else if (of_property_read_bool(np, "output-low"))
  516. *dflags |= GPIOD_OUT_LOW;
  517. else if (of_property_read_bool(np, "output-high"))
  518. *dflags |= GPIOD_OUT_HIGH;
  519. else {
  520. pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
  521. desc_to_gpio(desc), np);
  522. return ERR_PTR(-EINVAL);
  523. }
  524. if (name && of_property_read_string(np, "line-name", name))
  525. *name = np->name;
  526. return desc;
  527. }
  528. /**
  529. * of_gpiochip_add_hog - Add all hogs in a hog device node
  530. * @chip: gpio chip to act on
  531. * @hog: device node describing the hogs
  532. *
  533. * Returns error if it fails otherwise 0 on success.
  534. */
  535. static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
  536. {
  537. enum gpiod_flags dflags;
  538. struct gpio_desc *desc;
  539. unsigned long lflags;
  540. const char *name;
  541. unsigned int i;
  542. int ret;
  543. for (i = 0;; i++) {
  544. desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
  545. if (IS_ERR(desc))
  546. break;
  547. ret = gpiod_hog(desc, name, lflags, dflags);
  548. if (ret < 0)
  549. return ret;
  550. #ifdef CONFIG_OF_DYNAMIC
  551. desc->hog = hog;
  552. #endif
  553. }
  554. return 0;
  555. }
  556. /**
  557. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  558. * @chip: gpio chip to act on
  559. *
  560. * This is only used by of_gpiochip_add to request/set GPIO initial
  561. * configuration.
  562. * It returns error if it fails otherwise 0 on success.
  563. */
  564. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  565. {
  566. struct device_node *np;
  567. int ret;
  568. for_each_available_child_of_node(chip->of_node, np) {
  569. if (!of_property_read_bool(np, "gpio-hog"))
  570. continue;
  571. ret = of_gpiochip_add_hog(chip, np);
  572. if (ret < 0) {
  573. of_node_put(np);
  574. return ret;
  575. }
  576. of_node_set_flag(np, OF_POPULATED);
  577. }
  578. return 0;
  579. }
  580. #ifdef CONFIG_OF_DYNAMIC
  581. /**
  582. * of_gpiochip_remove_hog - Remove all hogs in a hog device node
  583. * @chip: gpio chip to act on
  584. * @hog: device node describing the hogs
  585. */
  586. static void of_gpiochip_remove_hog(struct gpio_chip *chip,
  587. struct device_node *hog)
  588. {
  589. struct gpio_desc *desc;
  590. for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
  591. if (desc->hog == hog)
  592. gpiochip_free_own_desc(desc);
  593. }
  594. static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
  595. {
  596. return device_match_of_node(&chip->gpiodev->dev, data);
  597. }
  598. static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
  599. {
  600. return gpiochip_find(np, of_gpiochip_match_node);
  601. }
  602. static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
  603. void *arg)
  604. {
  605. struct of_reconfig_data *rd = arg;
  606. struct gpio_chip *chip;
  607. int ret;
  608. /*
  609. * This only supports adding and removing complete gpio-hog nodes.
  610. * Modifying an existing gpio-hog node is not supported (except for
  611. * changing its "status" property, which is treated the same as
  612. * addition/removal).
  613. */
  614. switch (of_reconfig_get_state_change(action, arg)) {
  615. case OF_RECONFIG_CHANGE_ADD:
  616. if (!of_property_read_bool(rd->dn, "gpio-hog"))
  617. return NOTIFY_OK; /* not for us */
  618. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
  619. return NOTIFY_OK;
  620. chip = of_find_gpiochip_by_node(rd->dn->parent);
  621. if (chip == NULL)
  622. return NOTIFY_OK; /* not for us */
  623. ret = of_gpiochip_add_hog(chip, rd->dn);
  624. if (ret < 0) {
  625. pr_err("%s: failed to add hogs for %pOF\n", __func__,
  626. rd->dn);
  627. of_node_clear_flag(rd->dn, OF_POPULATED);
  628. return notifier_from_errno(ret);
  629. }
  630. break;
  631. case OF_RECONFIG_CHANGE_REMOVE:
  632. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  633. return NOTIFY_OK; /* already depopulated */
  634. chip = of_find_gpiochip_by_node(rd->dn->parent);
  635. if (chip == NULL)
  636. return NOTIFY_OK; /* not for us */
  637. of_gpiochip_remove_hog(chip, rd->dn);
  638. of_node_clear_flag(rd->dn, OF_POPULATED);
  639. break;
  640. }
  641. return NOTIFY_OK;
  642. }
  643. struct notifier_block gpio_of_notifier = {
  644. .notifier_call = of_gpio_notify,
  645. };
  646. #endif /* CONFIG_OF_DYNAMIC */
  647. /**
  648. * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
  649. * @gc: pointer to the gpio_chip structure
  650. * @gpiospec: GPIO specifier as found in the device tree
  651. * @flags: a flags pointer to fill in
  652. *
  653. * This is simple translation function, suitable for the most 1:1 mapped
  654. * GPIO chips. This function performs only one sanity check: whether GPIO
  655. * is less than ngpios (that is specified in the gpio_chip).
  656. */
  657. static int of_gpio_simple_xlate(struct gpio_chip *gc,
  658. const struct of_phandle_args *gpiospec,
  659. u32 *flags)
  660. {
  661. /*
  662. * We're discouraging gpio_cells < 2, since that way you'll have to
  663. * write your own xlate function (that will have to retrieve the GPIO
  664. * number and the flags from a single gpio cell -- this is possible,
  665. * but not recommended).
  666. */
  667. if (gc->of_gpio_n_cells < 2) {
  668. WARN_ON(1);
  669. return -EINVAL;
  670. }
  671. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  672. return -EINVAL;
  673. if (gpiospec->args[0] >= gc->ngpio)
  674. return -EINVAL;
  675. if (flags)
  676. *flags = gpiospec->args[1];
  677. return gpiospec->args[0];
  678. }
  679. /**
  680. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  681. * @np: device node of the GPIO chip
  682. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  683. * @data: driver data to store in the struct gpio_chip
  684. *
  685. * To use this function you should allocate and fill mm_gc with:
  686. *
  687. * 1) In the gpio_chip structure:
  688. * - all the callbacks
  689. * - of_gpio_n_cells
  690. * - of_xlate callback (optional)
  691. *
  692. * 3) In the of_mm_gpio_chip structure:
  693. * - save_regs callback (optional)
  694. *
  695. * If succeeded, this function will map bank's memory and will
  696. * do all necessary work for you. Then you'll able to use .regs
  697. * to manage GPIOs from the callbacks.
  698. */
  699. int of_mm_gpiochip_add_data(struct device_node *np,
  700. struct of_mm_gpio_chip *mm_gc,
  701. void *data)
  702. {
  703. int ret = -ENOMEM;
  704. struct gpio_chip *gc = &mm_gc->gc;
  705. gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
  706. if (!gc->label)
  707. goto err0;
  708. mm_gc->regs = of_iomap(np, 0);
  709. if (!mm_gc->regs)
  710. goto err1;
  711. gc->base = -1;
  712. if (mm_gc->save_regs)
  713. mm_gc->save_regs(mm_gc);
  714. of_node_put(mm_gc->gc.of_node);
  715. mm_gc->gc.of_node = of_node_get(np);
  716. ret = gpiochip_add_data(gc, data);
  717. if (ret)
  718. goto err2;
  719. return 0;
  720. err2:
  721. of_node_put(np);
  722. iounmap(mm_gc->regs);
  723. err1:
  724. kfree(gc->label);
  725. err0:
  726. pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
  727. return ret;
  728. }
  729. EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
  730. /**
  731. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  732. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  733. */
  734. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  735. {
  736. struct gpio_chip *gc = &mm_gc->gc;
  737. if (!mm_gc)
  738. return;
  739. gpiochip_remove(gc);
  740. iounmap(mm_gc->regs);
  741. kfree(gc->label);
  742. }
  743. EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
  744. static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
  745. {
  746. int len, i;
  747. u32 start, count;
  748. struct device_node *np = chip->of_node;
  749. len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
  750. if (len < 0 || len % 2 != 0)
  751. return;
  752. for (i = 0; i < len; i += 2) {
  753. of_property_read_u32_index(np, "gpio-reserved-ranges",
  754. i, &start);
  755. of_property_read_u32_index(np, "gpio-reserved-ranges",
  756. i + 1, &count);
  757. if (start >= chip->ngpio || start + count > chip->ngpio)
  758. continue;
  759. bitmap_clear(chip->valid_mask, start, count);
  760. }
  761. };
  762. #ifdef CONFIG_PINCTRL
  763. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  764. {
  765. struct device_node *np = chip->of_node;
  766. struct of_phandle_args pinspec;
  767. struct pinctrl_dev *pctldev;
  768. int index = 0, ret;
  769. const char *name;
  770. static const char group_names_propname[] = "gpio-ranges-group-names";
  771. struct property *group_names;
  772. if (!np)
  773. return 0;
  774. if (!of_property_read_bool(np, "gpio-ranges") &&
  775. chip->of_gpio_ranges_fallback) {
  776. return chip->of_gpio_ranges_fallback(chip, np);
  777. }
  778. group_names = of_find_property(np, group_names_propname, NULL);
  779. for (;; index++) {
  780. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  781. index, &pinspec);
  782. if (ret)
  783. break;
  784. pctldev = of_pinctrl_get(pinspec.np);
  785. of_node_put(pinspec.np);
  786. if (!pctldev)
  787. return -EPROBE_DEFER;
  788. if (pinspec.args[2]) {
  789. if (group_names) {
  790. of_property_read_string_index(np,
  791. group_names_propname,
  792. index, &name);
  793. if (strlen(name)) {
  794. pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
  795. np);
  796. break;
  797. }
  798. }
  799. /* npins != 0: linear range */
  800. ret = gpiochip_add_pin_range(chip,
  801. pinctrl_dev_get_devname(pctldev),
  802. pinspec.args[0],
  803. pinspec.args[1],
  804. pinspec.args[2]);
  805. if (ret)
  806. return ret;
  807. } else {
  808. /* npins == 0: special range */
  809. if (pinspec.args[1]) {
  810. pr_err("%pOF: Illegal gpio-range format.\n",
  811. np);
  812. break;
  813. }
  814. if (!group_names) {
  815. pr_err("%pOF: GPIO group range requested but no %s property.\n",
  816. np, group_names_propname);
  817. break;
  818. }
  819. ret = of_property_read_string_index(np,
  820. group_names_propname,
  821. index, &name);
  822. if (ret)
  823. break;
  824. if (!strlen(name)) {
  825. pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
  826. np);
  827. break;
  828. }
  829. ret = gpiochip_add_pingroup_range(chip, pctldev,
  830. pinspec.args[0], name);
  831. if (ret)
  832. return ret;
  833. }
  834. }
  835. return 0;
  836. }
  837. #else
  838. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  839. #endif
  840. int of_gpiochip_add(struct gpio_chip *chip)
  841. {
  842. int ret;
  843. if (!chip->of_node)
  844. return 0;
  845. if (!chip->of_xlate) {
  846. chip->of_gpio_n_cells = 2;
  847. chip->of_xlate = of_gpio_simple_xlate;
  848. }
  849. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  850. return -EINVAL;
  851. of_gpiochip_init_valid_mask(chip);
  852. ret = of_gpiochip_add_pin_range(chip);
  853. if (ret)
  854. return ret;
  855. of_node_get(chip->of_node);
  856. ret = of_gpiochip_scan_gpios(chip);
  857. if (ret)
  858. of_node_put(chip->of_node);
  859. return ret;
  860. }
  861. void of_gpiochip_remove(struct gpio_chip *chip)
  862. {
  863. of_node_put(chip->of_node);
  864. }
  865. void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
  866. {
  867. /* Set default OF node to parent's one if present */
  868. if (gc->parent)
  869. gdev->dev.of_node = gc->parent->of_node;
  870. if (gc->fwnode)
  871. gc->of_node = to_of_node(gc->fwnode);
  872. /* If the gpiochip has an assigned OF node this takes precedence */
  873. if (gc->of_node)
  874. gdev->dev.of_node = gc->of_node;
  875. else
  876. gc->of_node = gdev->dev.of_node;
  877. }