gpiolib-of.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 <avorontsov@ru.mvista.com>
  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. 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(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(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(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. /**
  260. * gpiod_get_from_of_node() - obtain a GPIO from an OF node
  261. * @node: handle of the OF node
  262. * @propname: name of the DT property representing the GPIO
  263. * @index: index of the GPIO to obtain for the consumer
  264. * @dflags: GPIO initialization flags
  265. * @label: label to attach to the requested GPIO
  266. *
  267. * Returns:
  268. * On successful request the GPIO pin is configured in accordance with
  269. * provided @dflags.
  270. *
  271. * In case of error an ERR_PTR() is returned.
  272. */
  273. struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
  274. const char *propname, int index,
  275. enum gpiod_flags dflags,
  276. const char *label)
  277. {
  278. unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  279. struct gpio_desc *desc;
  280. enum of_gpio_flags flags;
  281. bool active_low = false;
  282. bool single_ended = false;
  283. bool open_drain = false;
  284. bool transitory = false;
  285. int ret;
  286. desc = of_get_named_gpiod_flags(node, propname,
  287. index, &flags);
  288. if (!desc || IS_ERR(desc)) {
  289. return desc;
  290. }
  291. active_low = flags & OF_GPIO_ACTIVE_LOW;
  292. single_ended = flags & OF_GPIO_SINGLE_ENDED;
  293. open_drain = flags & OF_GPIO_OPEN_DRAIN;
  294. transitory = flags & OF_GPIO_TRANSITORY;
  295. ret = gpiod_request(desc, label);
  296. if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
  297. return desc;
  298. if (ret)
  299. return ERR_PTR(ret);
  300. if (active_low)
  301. lflags |= GPIO_ACTIVE_LOW;
  302. if (single_ended) {
  303. if (open_drain)
  304. lflags |= GPIO_OPEN_DRAIN;
  305. else
  306. lflags |= GPIO_OPEN_SOURCE;
  307. }
  308. if (transitory)
  309. lflags |= GPIO_TRANSITORY;
  310. if (flags & OF_GPIO_PULL_UP)
  311. lflags |= GPIO_PULL_UP;
  312. if (flags & OF_GPIO_PULL_DOWN)
  313. lflags |= GPIO_PULL_DOWN;
  314. ret = gpiod_configure_flags(desc, propname, lflags, dflags);
  315. if (ret < 0) {
  316. gpiod_put(desc);
  317. return ERR_PTR(ret);
  318. }
  319. return desc;
  320. }
  321. EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
  322. /*
  323. * The SPI GPIO bindings happened before we managed to establish that GPIO
  324. * properties should be named "foo-gpios" so we have this special kludge for
  325. * them.
  326. */
  327. static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
  328. enum of_gpio_flags *of_flags)
  329. {
  330. char prop_name[32]; /* 32 is max size of property name */
  331. struct device_node *np = dev->of_node;
  332. struct gpio_desc *desc;
  333. /*
  334. * Hopefully the compiler stubs the rest of the function if this
  335. * is false.
  336. */
  337. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  338. return ERR_PTR(-ENOENT);
  339. /* Allow this specifically for "spi-gpio" devices */
  340. if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
  341. return ERR_PTR(-ENOENT);
  342. /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
  343. snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
  344. desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
  345. return desc;
  346. }
  347. /*
  348. * The old Freescale bindings use simply "gpios" as name for the chip select
  349. * lines rather than "cs-gpios" like all other SPI hardware. Account for this
  350. * with a special quirk.
  351. */
  352. static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
  353. const char *con_id,
  354. unsigned int idx,
  355. unsigned long *flags)
  356. {
  357. struct device_node *np = dev->of_node;
  358. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  359. return ERR_PTR(-ENOENT);
  360. /* Allow this specifically for Freescale and PPC devices */
  361. if (!of_device_is_compatible(np, "fsl,spi") &&
  362. !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
  363. !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
  364. return ERR_PTR(-ENOENT);
  365. /* Allow only if asking for "cs-gpios" */
  366. if (!con_id || strcmp(con_id, "cs"))
  367. return ERR_PTR(-ENOENT);
  368. /*
  369. * While all other SPI controllers use "cs-gpios" the Freescale
  370. * uses just "gpios" so translate to that when "cs-gpios" is
  371. * requested.
  372. */
  373. return of_find_gpio(dev, NULL, idx, flags);
  374. }
  375. /*
  376. * Some regulator bindings happened before we managed to establish that GPIO
  377. * properties should be named "foo-gpios" so we have this special kludge for
  378. * them.
  379. */
  380. static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
  381. enum of_gpio_flags *of_flags)
  382. {
  383. /* These are the connection IDs we accept as legacy GPIO phandles */
  384. const char *whitelist[] = {
  385. "wlf,ldoena", /* Arizona */
  386. "wlf,ldo1ena", /* WM8994 */
  387. "wlf,ldo2ena", /* WM8994 */
  388. };
  389. struct device_node *np = dev->of_node;
  390. struct gpio_desc *desc;
  391. int i;
  392. if (!IS_ENABLED(CONFIG_REGULATOR))
  393. return ERR_PTR(-ENOENT);
  394. if (!con_id)
  395. return ERR_PTR(-ENOENT);
  396. i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
  397. if (i < 0)
  398. return ERR_PTR(-ENOENT);
  399. desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
  400. return desc;
  401. }
  402. static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
  403. const char *con_id,
  404. enum of_gpio_flags *of_flags)
  405. {
  406. if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
  407. return ERR_PTR(-ENOENT);
  408. if (!con_id || strcmp(con_id, "wlf,reset"))
  409. return ERR_PTR(-ENOENT);
  410. return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
  411. }
  412. static struct gpio_desc *of_find_usb_gpio(struct device *dev,
  413. const char *con_id,
  414. enum of_gpio_flags *of_flags)
  415. {
  416. /*
  417. * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
  418. * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
  419. * suffix.
  420. */
  421. if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
  422. return ERR_PTR(-ENOENT);
  423. if (!con_id || strcmp(con_id, "fcs,int_n"))
  424. return ERR_PTR(-ENOENT);
  425. return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
  426. }
  427. struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  428. unsigned int idx, unsigned long *flags)
  429. {
  430. char prop_name[32]; /* 32 is max size of property name */
  431. enum of_gpio_flags of_flags;
  432. struct gpio_desc *desc;
  433. unsigned int i;
  434. /* Try GPIO property "foo-gpios" and "foo-gpio" */
  435. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  436. if (con_id)
  437. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  438. gpio_suffixes[i]);
  439. else
  440. snprintf(prop_name, sizeof(prop_name), "%s",
  441. gpio_suffixes[i]);
  442. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  443. &of_flags);
  444. if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
  445. break;
  446. }
  447. if (PTR_ERR(desc) == -ENOENT) {
  448. /* Special handling for SPI GPIOs if used */
  449. desc = of_find_spi_gpio(dev, con_id, &of_flags);
  450. }
  451. if (PTR_ERR(desc) == -ENOENT) {
  452. /* This quirk looks up flags and all */
  453. desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
  454. if (!IS_ERR(desc))
  455. return desc;
  456. }
  457. if (PTR_ERR(desc) == -ENOENT) {
  458. /* Special handling for regulator GPIOs if used */
  459. desc = of_find_regulator_gpio(dev, con_id, &of_flags);
  460. }
  461. if (PTR_ERR(desc) == -ENOENT)
  462. desc = of_find_arizona_gpio(dev, con_id, &of_flags);
  463. if (PTR_ERR(desc) == -ENOENT)
  464. desc = of_find_usb_gpio(dev, con_id, &of_flags);
  465. if (IS_ERR(desc))
  466. return desc;
  467. if (of_flags & OF_GPIO_ACTIVE_LOW)
  468. *flags |= GPIO_ACTIVE_LOW;
  469. if (of_flags & OF_GPIO_SINGLE_ENDED) {
  470. if (of_flags & OF_GPIO_OPEN_DRAIN)
  471. *flags |= GPIO_OPEN_DRAIN;
  472. else
  473. *flags |= GPIO_OPEN_SOURCE;
  474. }
  475. if (of_flags & OF_GPIO_TRANSITORY)
  476. *flags |= GPIO_TRANSITORY;
  477. if (of_flags & OF_GPIO_PULL_UP)
  478. *flags |= GPIO_PULL_UP;
  479. if (of_flags & OF_GPIO_PULL_DOWN)
  480. *flags |= GPIO_PULL_DOWN;
  481. return desc;
  482. }
  483. /**
  484. * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  485. * @np: device node to get GPIO from
  486. * @chip: GPIO chip whose hog is parsed
  487. * @idx: Index of the GPIO to parse
  488. * @name: GPIO line name
  489. * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
  490. * of_find_gpio() or of_parse_own_gpio()
  491. * @dflags: gpiod_flags - optional GPIO initialization flags
  492. *
  493. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  494. * value on the error condition.
  495. */
  496. static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
  497. struct gpio_chip *chip,
  498. unsigned int idx, const char **name,
  499. unsigned long *lflags,
  500. enum gpiod_flags *dflags)
  501. {
  502. struct device_node *chip_np;
  503. enum of_gpio_flags xlate_flags;
  504. struct of_phandle_args gpiospec;
  505. struct gpio_desc *desc;
  506. unsigned int i;
  507. u32 tmp;
  508. int ret;
  509. chip_np = chip->of_node;
  510. if (!chip_np)
  511. return ERR_PTR(-EINVAL);
  512. xlate_flags = 0;
  513. *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
  514. *dflags = 0;
  515. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  516. if (ret)
  517. return ERR_PTR(ret);
  518. gpiospec.np = chip_np;
  519. gpiospec.args_count = tmp;
  520. for (i = 0; i < tmp; i++) {
  521. ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
  522. &gpiospec.args[i]);
  523. if (ret)
  524. return ERR_PTR(ret);
  525. }
  526. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
  527. if (IS_ERR(desc))
  528. return desc;
  529. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  530. *lflags |= GPIO_ACTIVE_LOW;
  531. if (xlate_flags & OF_GPIO_TRANSITORY)
  532. *lflags |= GPIO_TRANSITORY;
  533. if (xlate_flags & OF_GPIO_PULL_UP)
  534. *lflags |= GPIO_PULL_UP;
  535. if (xlate_flags & OF_GPIO_PULL_DOWN)
  536. *lflags |= GPIO_PULL_DOWN;
  537. if (of_property_read_bool(np, "input"))
  538. *dflags |= GPIOD_IN;
  539. else if (of_property_read_bool(np, "output-low"))
  540. *dflags |= GPIOD_OUT_LOW;
  541. else if (of_property_read_bool(np, "output-high"))
  542. *dflags |= GPIOD_OUT_HIGH;
  543. else {
  544. pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
  545. desc_to_gpio(desc), np);
  546. return ERR_PTR(-EINVAL);
  547. }
  548. if (name && of_property_read_string(np, "line-name", name))
  549. *name = np->name;
  550. return desc;
  551. }
  552. /**
  553. * of_gpiochip_add_hog - Add all hogs in a hog device node
  554. * @chip: gpio chip to act on
  555. * @hog: device node describing the hogs
  556. *
  557. * Returns error if it fails otherwise 0 on success.
  558. */
  559. static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
  560. {
  561. enum gpiod_flags dflags;
  562. struct gpio_desc *desc;
  563. unsigned long lflags;
  564. const char *name;
  565. unsigned int i;
  566. int ret;
  567. for (i = 0;; i++) {
  568. desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
  569. if (IS_ERR(desc))
  570. break;
  571. ret = gpiod_hog(desc, name, lflags, dflags);
  572. if (ret < 0)
  573. return ret;
  574. #ifdef CONFIG_OF_DYNAMIC
  575. desc->hog = hog;
  576. #endif
  577. }
  578. return 0;
  579. }
  580. /**
  581. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  582. * @chip: gpio chip to act on
  583. *
  584. * This is only used by of_gpiochip_add to request/set GPIO initial
  585. * configuration.
  586. * It returns error if it fails otherwise 0 on success.
  587. */
  588. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  589. {
  590. struct device_node *np;
  591. int ret;
  592. for_each_available_child_of_node(chip->of_node, np) {
  593. if (!of_property_read_bool(np, "gpio-hog"))
  594. continue;
  595. ret = of_gpiochip_add_hog(chip, np);
  596. if (ret < 0) {
  597. of_node_put(np);
  598. return ret;
  599. }
  600. of_node_set_flag(np, OF_POPULATED);
  601. }
  602. return 0;
  603. }
  604. #ifdef CONFIG_OF_DYNAMIC
  605. /**
  606. * of_gpiochip_remove_hog - Remove all hogs in a hog device node
  607. * @chip: gpio chip to act on
  608. * @hog: device node describing the hogs
  609. */
  610. static void of_gpiochip_remove_hog(struct gpio_chip *chip,
  611. struct device_node *hog)
  612. {
  613. struct gpio_desc *descs = chip->gpiodev->descs;
  614. unsigned int i;
  615. for (i = 0; i < chip->ngpio; i++) {
  616. if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) &&
  617. descs[i].hog == hog)
  618. gpiochip_free_own_desc(&descs[i]);
  619. }
  620. }
  621. static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
  622. {
  623. return chip->gpiodev->dev.of_node == data;
  624. }
  625. static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
  626. {
  627. return gpiochip_find(np, of_gpiochip_match_node);
  628. }
  629. static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
  630. void *arg)
  631. {
  632. struct of_reconfig_data *rd = arg;
  633. struct gpio_chip *chip;
  634. int ret;
  635. /*
  636. * This only supports adding and removing complete gpio-hog nodes.
  637. * Modifying an existing gpio-hog node is not supported (except for
  638. * changing its "status" property, which is treated the same as
  639. * addition/removal).
  640. */
  641. switch (of_reconfig_get_state_change(action, arg)) {
  642. case OF_RECONFIG_CHANGE_ADD:
  643. if (!of_property_read_bool(rd->dn, "gpio-hog"))
  644. return NOTIFY_OK; /* not for us */
  645. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
  646. return NOTIFY_OK;
  647. chip = of_find_gpiochip_by_node(rd->dn->parent);
  648. if (chip == NULL)
  649. return NOTIFY_OK; /* not for us */
  650. ret = of_gpiochip_add_hog(chip, rd->dn);
  651. if (ret < 0) {
  652. pr_err("%s: failed to add hogs for %pOF\n", __func__,
  653. rd->dn);
  654. of_node_clear_flag(rd->dn, OF_POPULATED);
  655. return notifier_from_errno(ret);
  656. }
  657. break;
  658. case OF_RECONFIG_CHANGE_REMOVE:
  659. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  660. return NOTIFY_OK; /* already depopulated */
  661. chip = of_find_gpiochip_by_node(rd->dn->parent);
  662. if (chip == NULL)
  663. return NOTIFY_OK; /* not for us */
  664. of_gpiochip_remove_hog(chip, rd->dn);
  665. of_node_clear_flag(rd->dn, OF_POPULATED);
  666. break;
  667. }
  668. return NOTIFY_OK;
  669. }
  670. struct notifier_block gpio_of_notifier = {
  671. .notifier_call = of_gpio_notify,
  672. };
  673. #endif /* CONFIG_OF_DYNAMIC */
  674. /**
  675. * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
  676. * @gc: pointer to the gpio_chip structure
  677. * @gpiospec: GPIO specifier as found in the device tree
  678. * @flags: a flags pointer to fill in
  679. *
  680. * This is simple translation function, suitable for the most 1:1 mapped
  681. * GPIO chips. This function performs only one sanity check: whether GPIO
  682. * is less than ngpios (that is specified in the gpio_chip).
  683. */
  684. static int of_gpio_simple_xlate(struct gpio_chip *gc,
  685. const struct of_phandle_args *gpiospec,
  686. u32 *flags)
  687. {
  688. /*
  689. * We're discouraging gpio_cells < 2, since that way you'll have to
  690. * write your own xlate function (that will have to retrieve the GPIO
  691. * number and the flags from a single gpio cell -- this is possible,
  692. * but not recommended).
  693. */
  694. if (gc->of_gpio_n_cells < 2) {
  695. WARN_ON(1);
  696. return -EINVAL;
  697. }
  698. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  699. return -EINVAL;
  700. if (gpiospec->args[0] >= gc->ngpio)
  701. return -EINVAL;
  702. if (flags)
  703. *flags = gpiospec->args[1];
  704. return gpiospec->args[0];
  705. }
  706. /**
  707. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  708. * @np: device node of the GPIO chip
  709. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  710. * @data: driver data to store in the struct gpio_chip
  711. *
  712. * To use this function you should allocate and fill mm_gc with:
  713. *
  714. * 1) In the gpio_chip structure:
  715. * - all the callbacks
  716. * - of_gpio_n_cells
  717. * - of_xlate callback (optional)
  718. *
  719. * 3) In the of_mm_gpio_chip structure:
  720. * - save_regs callback (optional)
  721. *
  722. * If succeeded, this function will map bank's memory and will
  723. * do all necessary work for you. Then you'll able to use .regs
  724. * to manage GPIOs from the callbacks.
  725. */
  726. int of_mm_gpiochip_add_data(struct device_node *np,
  727. struct of_mm_gpio_chip *mm_gc,
  728. void *data)
  729. {
  730. int ret = -ENOMEM;
  731. struct gpio_chip *gc = &mm_gc->gc;
  732. gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
  733. if (!gc->label)
  734. goto err0;
  735. mm_gc->regs = of_iomap(np, 0);
  736. if (!mm_gc->regs)
  737. goto err1;
  738. gc->base = -1;
  739. if (mm_gc->save_regs)
  740. mm_gc->save_regs(mm_gc);
  741. mm_gc->gc.of_node = np;
  742. ret = gpiochip_add_data(gc, data);
  743. if (ret)
  744. goto err2;
  745. return 0;
  746. err2:
  747. iounmap(mm_gc->regs);
  748. err1:
  749. kfree(gc->label);
  750. err0:
  751. pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
  752. return ret;
  753. }
  754. EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
  755. /**
  756. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  757. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  758. */
  759. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  760. {
  761. struct gpio_chip *gc = &mm_gc->gc;
  762. if (!mm_gc)
  763. return;
  764. gpiochip_remove(gc);
  765. iounmap(mm_gc->regs);
  766. kfree(gc->label);
  767. }
  768. EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
  769. static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
  770. {
  771. int len, i;
  772. u32 start, count;
  773. struct device_node *np = chip->of_node;
  774. len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
  775. if (len < 0 || len % 2 != 0)
  776. return;
  777. for (i = 0; i < len; i += 2) {
  778. of_property_read_u32_index(np, "gpio-reserved-ranges",
  779. i, &start);
  780. of_property_read_u32_index(np, "gpio-reserved-ranges",
  781. i + 1, &count);
  782. if (start >= chip->ngpio || start + count >= chip->ngpio)
  783. continue;
  784. bitmap_clear(chip->valid_mask, start, count);
  785. }
  786. };
  787. #ifdef CONFIG_PINCTRL
  788. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  789. {
  790. struct device_node *np = chip->of_node;
  791. struct of_phandle_args pinspec;
  792. struct pinctrl_dev *pctldev;
  793. int index = 0, ret;
  794. const char *name;
  795. static const char group_names_propname[] = "gpio-ranges-group-names";
  796. struct property *group_names;
  797. if (!np)
  798. return 0;
  799. group_names = of_find_property(np, group_names_propname, NULL);
  800. for (;; index++) {
  801. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  802. index, &pinspec);
  803. if (ret)
  804. break;
  805. pctldev = of_pinctrl_get(pinspec.np);
  806. of_node_put(pinspec.np);
  807. if (!pctldev)
  808. return -EPROBE_DEFER;
  809. if (pinspec.args[2]) {
  810. if (group_names) {
  811. of_property_read_string_index(np,
  812. group_names_propname,
  813. index, &name);
  814. if (strlen(name)) {
  815. pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
  816. np);
  817. break;
  818. }
  819. }
  820. /* npins != 0: linear range */
  821. ret = gpiochip_add_pin_range(chip,
  822. pinctrl_dev_get_devname(pctldev),
  823. pinspec.args[0],
  824. pinspec.args[1],
  825. pinspec.args[2]);
  826. if (ret)
  827. return ret;
  828. } else {
  829. /* npins == 0: special range */
  830. if (pinspec.args[1]) {
  831. pr_err("%pOF: Illegal gpio-range format.\n",
  832. np);
  833. break;
  834. }
  835. if (!group_names) {
  836. pr_err("%pOF: GPIO group range requested but no %s property.\n",
  837. np, group_names_propname);
  838. break;
  839. }
  840. ret = of_property_read_string_index(np,
  841. group_names_propname,
  842. index, &name);
  843. if (ret)
  844. break;
  845. if (!strlen(name)) {
  846. pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
  847. np);
  848. break;
  849. }
  850. ret = gpiochip_add_pingroup_range(chip, pctldev,
  851. pinspec.args[0], name);
  852. if (ret)
  853. return ret;
  854. }
  855. }
  856. return 0;
  857. }
  858. #else
  859. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  860. #endif
  861. int of_gpiochip_add(struct gpio_chip *chip)
  862. {
  863. int ret;
  864. if (!chip->of_node)
  865. return 0;
  866. if (!chip->of_xlate) {
  867. chip->of_gpio_n_cells = 2;
  868. chip->of_xlate = of_gpio_simple_xlate;
  869. }
  870. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  871. return -EINVAL;
  872. of_gpiochip_init_valid_mask(chip);
  873. ret = of_gpiochip_add_pin_range(chip);
  874. if (ret)
  875. return ret;
  876. of_node_get(chip->of_node);
  877. ret = of_gpiochip_scan_gpios(chip);
  878. if (ret)
  879. of_node_put(chip->of_node);
  880. return ret;
  881. }
  882. void of_gpiochip_remove(struct gpio_chip *chip)
  883. {
  884. of_node_put(chip->of_node);
  885. }
  886. void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
  887. {
  888. /* If the gpiochip has an assigned OF node this takes precedence */
  889. if (gc->of_node)
  890. gdev->dev.of_node = gc->of_node;
  891. else
  892. gc->of_node = gdev->dev.of_node;
  893. if (gdev->dev.of_node)
  894. gdev->dev.fwnode = of_fwnode_handle(gdev->dev.of_node);
  895. }