gpio-uclass.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dt-bindings/gpio/gpio.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <malloc.h>
  12. #include <asm/gpio.h>
  13. #include <linux/bug.h>
  14. #include <linux/ctype.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /**
  17. * gpio_to_device() - Convert global GPIO number to device, number
  18. *
  19. * Convert the GPIO number to an entry in the list of GPIOs
  20. * or GPIO blocks registered with the GPIO controller. Returns
  21. * entry on success, NULL on error.
  22. *
  23. * @gpio: The numeric representation of the GPIO
  24. * @desc: Returns description (desc->flags will always be 0)
  25. * @return 0 if found, -ENOENT if not found
  26. */
  27. static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
  28. {
  29. struct gpio_dev_priv *uc_priv;
  30. struct udevice *dev;
  31. int ret;
  32. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  33. dev;
  34. ret = uclass_next_device(&dev)) {
  35. uc_priv = dev_get_uclass_priv(dev);
  36. if (gpio >= uc_priv->gpio_base &&
  37. gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
  38. desc->dev = dev;
  39. desc->offset = gpio - uc_priv->gpio_base;
  40. desc->flags = 0;
  41. return 0;
  42. }
  43. }
  44. /* No such GPIO */
  45. return ret ? ret : -ENOENT;
  46. }
  47. int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
  48. {
  49. struct gpio_dev_priv *uc_priv = NULL;
  50. struct udevice *dev;
  51. ulong offset;
  52. int numeric;
  53. int ret;
  54. numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
  55. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  56. dev;
  57. ret = uclass_next_device(&dev)) {
  58. int len;
  59. uc_priv = dev_get_uclass_priv(dev);
  60. if (numeric != -1) {
  61. offset = numeric - uc_priv->gpio_base;
  62. /* Allow GPIOs to be numbered from 0 */
  63. if (offset < uc_priv->gpio_count)
  64. break;
  65. }
  66. len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
  67. if (!strncasecmp(name, uc_priv->bank_name, len)) {
  68. if (!strict_strtoul(name + len, 10, &offset))
  69. break;
  70. }
  71. }
  72. if (!dev)
  73. return ret ? ret : -EINVAL;
  74. desc->dev = dev;
  75. desc->offset = offset;
  76. return 0;
  77. }
  78. int gpio_lookup_name(const char *name, struct udevice **devp,
  79. unsigned int *offsetp, unsigned int *gpiop)
  80. {
  81. struct gpio_desc desc;
  82. int ret;
  83. if (devp)
  84. *devp = NULL;
  85. ret = dm_gpio_lookup_name(name, &desc);
  86. if (ret)
  87. return ret;
  88. if (devp)
  89. *devp = desc.dev;
  90. if (offsetp)
  91. *offsetp = desc.offset;
  92. if (gpiop) {
  93. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
  94. *gpiop = uc_priv->gpio_base + desc.offset;
  95. }
  96. return 0;
  97. }
  98. int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
  99. struct ofnode_phandle_args *args)
  100. {
  101. if (args->args_count < 1)
  102. return -EINVAL;
  103. desc->offset = args->args[0];
  104. if (args->args_count < 2)
  105. return 0;
  106. if (args->args[1] & GPIO_ACTIVE_LOW)
  107. desc->flags = GPIOD_ACTIVE_LOW;
  108. return 0;
  109. }
  110. static int gpio_find_and_xlate(struct gpio_desc *desc,
  111. struct ofnode_phandle_args *args)
  112. {
  113. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  114. if (ops->xlate)
  115. return ops->xlate(desc->dev, desc, args);
  116. else
  117. return gpio_xlate_offs_flags(desc->dev, desc, args);
  118. }
  119. int dm_gpio_request(struct gpio_desc *desc, const char *label)
  120. {
  121. struct udevice *dev = desc->dev;
  122. struct gpio_dev_priv *uc_priv;
  123. char *str;
  124. int ret;
  125. uc_priv = dev_get_uclass_priv(dev);
  126. if (uc_priv->name[desc->offset])
  127. return -EBUSY;
  128. str = strdup(label);
  129. if (!str)
  130. return -ENOMEM;
  131. if (gpio_get_ops(dev)->request) {
  132. ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
  133. if (ret) {
  134. free(str);
  135. return ret;
  136. }
  137. }
  138. uc_priv->name[desc->offset] = str;
  139. return 0;
  140. }
  141. static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
  142. {
  143. #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
  144. va_list args;
  145. char buf[40];
  146. va_start(args, fmt);
  147. vscnprintf(buf, sizeof(buf), fmt, args);
  148. va_end(args);
  149. return dm_gpio_request(desc, buf);
  150. #else
  151. return dm_gpio_request(desc, fmt);
  152. #endif
  153. }
  154. /**
  155. * gpio_request() - [COMPAT] Request GPIO
  156. * gpio: GPIO number
  157. * label: Name for the requested GPIO
  158. *
  159. * The label is copied and allocated so the caller does not need to keep
  160. * the pointer around.
  161. *
  162. * This function implements the API that's compatible with current
  163. * GPIO API used in U-Boot. The request is forwarded to particular
  164. * GPIO driver. Returns 0 on success, negative value on error.
  165. */
  166. int gpio_request(unsigned gpio, const char *label)
  167. {
  168. struct gpio_desc desc;
  169. int ret;
  170. ret = gpio_to_device(gpio, &desc);
  171. if (ret)
  172. return ret;
  173. return dm_gpio_request(&desc, label);
  174. }
  175. /**
  176. * gpio_requestf() - [COMPAT] Request GPIO
  177. * @gpio: GPIO number
  178. * @fmt: Format string for the requested GPIO
  179. * @...: Arguments for the printf() format string
  180. *
  181. * This function implements the API that's compatible with current
  182. * GPIO API used in U-Boot. The request is forwarded to particular
  183. * GPIO driver. Returns 0 on success, negative value on error.
  184. */
  185. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  186. {
  187. #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
  188. va_list args;
  189. char buf[40];
  190. va_start(args, fmt);
  191. vscnprintf(buf, sizeof(buf), fmt, args);
  192. va_end(args);
  193. return gpio_request(gpio, buf);
  194. #else
  195. return gpio_request(gpio, fmt);
  196. #endif
  197. }
  198. int _dm_gpio_free(struct udevice *dev, uint offset)
  199. {
  200. struct gpio_dev_priv *uc_priv;
  201. int ret;
  202. uc_priv = dev_get_uclass_priv(dev);
  203. if (!uc_priv->name[offset])
  204. return -ENXIO;
  205. if (gpio_get_ops(dev)->free) {
  206. ret = gpio_get_ops(dev)->free(dev, offset);
  207. if (ret)
  208. return ret;
  209. }
  210. free(uc_priv->name[offset]);
  211. uc_priv->name[offset] = NULL;
  212. return 0;
  213. }
  214. /**
  215. * gpio_free() - [COMPAT] Relinquish GPIO
  216. * gpio: GPIO number
  217. *
  218. * This function implements the API that's compatible with current
  219. * GPIO API used in U-Boot. The request is forwarded to particular
  220. * GPIO driver. Returns 0 on success, negative value on error.
  221. */
  222. int gpio_free(unsigned gpio)
  223. {
  224. struct gpio_desc desc;
  225. int ret;
  226. ret = gpio_to_device(gpio, &desc);
  227. if (ret)
  228. return ret;
  229. return _dm_gpio_free(desc.dev, desc.offset);
  230. }
  231. static int check_reserved(const struct gpio_desc *desc, const char *func)
  232. {
  233. struct gpio_dev_priv *uc_priv;
  234. if (!dm_gpio_is_valid(desc))
  235. return -ENOENT;
  236. uc_priv = dev_get_uclass_priv(desc->dev);
  237. if (!uc_priv->name[desc->offset]) {
  238. printf("%s: %s: error: gpio %s%d not reserved\n",
  239. desc->dev->name, func,
  240. uc_priv->bank_name ? uc_priv->bank_name : "",
  241. desc->offset);
  242. return -EBUSY;
  243. }
  244. return 0;
  245. }
  246. /**
  247. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  248. * gpio: GPIO number
  249. *
  250. * This function implements the API that's compatible with current
  251. * GPIO API used in U-Boot. The request is forwarded to particular
  252. * GPIO driver. Returns 0 on success, negative value on error.
  253. */
  254. int gpio_direction_input(unsigned gpio)
  255. {
  256. struct gpio_desc desc;
  257. int ret;
  258. ret = gpio_to_device(gpio, &desc);
  259. if (ret)
  260. return ret;
  261. ret = check_reserved(&desc, "dir_input");
  262. if (ret)
  263. return ret;
  264. return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
  265. }
  266. /**
  267. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  268. * gpio: GPIO number
  269. * value: Logical value to be set on the GPIO pin
  270. *
  271. * This function implements the API that's compatible with current
  272. * GPIO API used in U-Boot. The request is forwarded to particular
  273. * GPIO driver. Returns 0 on success, negative value on error.
  274. */
  275. int gpio_direction_output(unsigned gpio, int value)
  276. {
  277. struct gpio_desc desc;
  278. int ret;
  279. ret = gpio_to_device(gpio, &desc);
  280. if (ret)
  281. return ret;
  282. ret = check_reserved(&desc, "dir_output");
  283. if (ret)
  284. return ret;
  285. return gpio_get_ops(desc.dev)->direction_output(desc.dev,
  286. desc.offset, value);
  287. }
  288. int dm_gpio_get_value(const struct gpio_desc *desc)
  289. {
  290. int value;
  291. int ret;
  292. ret = check_reserved(desc, "get_value");
  293. if (ret)
  294. return ret;
  295. value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
  296. return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
  297. }
  298. int dm_gpio_set_value(const struct gpio_desc *desc, int value)
  299. {
  300. int ret;
  301. ret = check_reserved(desc, "set_value");
  302. if (ret)
  303. return ret;
  304. if (desc->flags & GPIOD_ACTIVE_LOW)
  305. value = !value;
  306. gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
  307. return 0;
  308. }
  309. int dm_gpio_get_open_drain(struct gpio_desc *desc)
  310. {
  311. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  312. int ret;
  313. ret = check_reserved(desc, "get_open_drain");
  314. if (ret)
  315. return ret;
  316. if (ops->set_open_drain)
  317. return ops->get_open_drain(desc->dev, desc->offset);
  318. else
  319. return -ENOSYS;
  320. }
  321. int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
  322. {
  323. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  324. int ret;
  325. ret = check_reserved(desc, "set_open_drain");
  326. if (ret)
  327. return ret;
  328. if (ops->set_open_drain)
  329. ret = ops->set_open_drain(desc->dev, desc->offset, value);
  330. else
  331. return 0; /* feature not supported -> ignore setting */
  332. return ret;
  333. }
  334. int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
  335. {
  336. struct udevice *dev = desc->dev;
  337. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  338. int ret;
  339. ret = check_reserved(desc, "set_dir");
  340. if (ret)
  341. return ret;
  342. if (flags & GPIOD_IS_OUT) {
  343. int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
  344. if (flags & GPIOD_ACTIVE_LOW)
  345. value = !value;
  346. ret = ops->direction_output(dev, desc->offset, value);
  347. } else if (flags & GPIOD_IS_IN) {
  348. ret = ops->direction_input(dev, desc->offset);
  349. }
  350. if (ret)
  351. return ret;
  352. /*
  353. * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
  354. * futures
  355. */
  356. desc->flags = flags;
  357. return 0;
  358. }
  359. int dm_gpio_set_dir(struct gpio_desc *desc)
  360. {
  361. return dm_gpio_set_dir_flags(desc, desc->flags);
  362. }
  363. /**
  364. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  365. * gpio: GPIO number
  366. *
  367. * This function implements the API that's compatible with current
  368. * GPIO API used in U-Boot. The request is forwarded to particular
  369. * GPIO driver. Returns the value of the GPIO pin, or negative value
  370. * on error.
  371. */
  372. int gpio_get_value(unsigned gpio)
  373. {
  374. int ret;
  375. struct gpio_desc desc;
  376. ret = gpio_to_device(gpio, &desc);
  377. if (ret)
  378. return ret;
  379. return dm_gpio_get_value(&desc);
  380. }
  381. /**
  382. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  383. * gpio: GPIO number
  384. * value: Logical value to be set on the GPIO pin.
  385. *
  386. * This function implements the API that's compatible with current
  387. * GPIO API used in U-Boot. The request is forwarded to particular
  388. * GPIO driver. Returns 0 on success, negative value on error.
  389. */
  390. int gpio_set_value(unsigned gpio, int value)
  391. {
  392. struct gpio_desc desc;
  393. int ret;
  394. ret = gpio_to_device(gpio, &desc);
  395. if (ret)
  396. return ret;
  397. return dm_gpio_set_value(&desc, value);
  398. }
  399. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  400. {
  401. struct gpio_dev_priv *priv;
  402. /* Must be called on an active device */
  403. priv = dev_get_uclass_priv(dev);
  404. assert(priv);
  405. *bit_count = priv->gpio_count;
  406. return priv->bank_name;
  407. }
  408. static const char * const gpio_function[GPIOF_COUNT] = {
  409. "input",
  410. "output",
  411. "unused",
  412. "unknown",
  413. "func",
  414. };
  415. static int get_function(struct udevice *dev, int offset, bool skip_unused,
  416. const char **namep)
  417. {
  418. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  419. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  420. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  421. if (!device_active(dev))
  422. return -ENODEV;
  423. if (offset < 0 || offset >= uc_priv->gpio_count)
  424. return -EINVAL;
  425. if (namep)
  426. *namep = uc_priv->name[offset];
  427. if (skip_unused && !uc_priv->name[offset])
  428. return GPIOF_UNUSED;
  429. if (ops->get_function) {
  430. int ret;
  431. ret = ops->get_function(dev, offset);
  432. if (ret < 0)
  433. return ret;
  434. if (ret >= ARRAY_SIZE(gpio_function))
  435. return -ENODATA;
  436. return ret;
  437. }
  438. return GPIOF_UNKNOWN;
  439. }
  440. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  441. {
  442. return get_function(dev, offset, true, namep);
  443. }
  444. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  445. {
  446. return get_function(dev, offset, false, namep);
  447. }
  448. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  449. {
  450. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  451. struct gpio_dev_priv *priv;
  452. char *str = buf;
  453. int func;
  454. int ret;
  455. int len;
  456. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  457. *buf = 0;
  458. priv = dev_get_uclass_priv(dev);
  459. ret = gpio_get_raw_function(dev, offset, NULL);
  460. if (ret < 0)
  461. return ret;
  462. func = ret;
  463. len = snprintf(str, buffsize, "%s%d: %s",
  464. priv->bank_name ? priv->bank_name : "",
  465. offset, gpio_function[func]);
  466. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  467. func == GPIOF_UNUSED) {
  468. const char *label;
  469. bool used;
  470. ret = ops->get_value(dev, offset);
  471. if (ret < 0)
  472. return ret;
  473. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  474. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  475. ret,
  476. used ? 'x' : ' ',
  477. used ? " " : "",
  478. label ? label : "");
  479. }
  480. return 0;
  481. }
  482. int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
  483. {
  484. int i, ret;
  485. int gpio;
  486. for (i = 0; i < 32; i++) {
  487. gpio = gpio_num_array[i];
  488. if (gpio == -1)
  489. break;
  490. ret = gpio_requestf(gpio, fmt, i);
  491. if (ret)
  492. goto err;
  493. ret = gpio_direction_input(gpio);
  494. if (ret) {
  495. gpio_free(gpio);
  496. goto err;
  497. }
  498. }
  499. return 0;
  500. err:
  501. for (i--; i >= 0; i--)
  502. gpio_free(gpio_num_array[i]);
  503. return ret;
  504. }
  505. /*
  506. * get a number comprised of multiple GPIO values. gpio_num_array points to
  507. * the array of gpio pin numbers to scan, terminated by -1.
  508. */
  509. int gpio_get_values_as_int(const int *gpio_list)
  510. {
  511. int gpio;
  512. unsigned bitmask = 1;
  513. unsigned vector = 0;
  514. int ret;
  515. while (bitmask &&
  516. ((gpio = *gpio_list++) != -1)) {
  517. ret = gpio_get_value(gpio);
  518. if (ret < 0)
  519. return ret;
  520. else if (ret)
  521. vector |= bitmask;
  522. bitmask <<= 1;
  523. }
  524. return vector;
  525. }
  526. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
  527. {
  528. unsigned bitmask = 1;
  529. unsigned vector = 0;
  530. int ret, i;
  531. for (i = 0; i < count; i++) {
  532. ret = dm_gpio_get_value(&desc_list[i]);
  533. if (ret < 0)
  534. return ret;
  535. else if (ret)
  536. vector |= bitmask;
  537. bitmask <<= 1;
  538. }
  539. return vector;
  540. }
  541. static int gpio_request_tail(int ret, ofnode node,
  542. struct ofnode_phandle_args *args,
  543. const char *list_name, int index,
  544. struct gpio_desc *desc, int flags, bool add_index)
  545. {
  546. desc->dev = NULL;
  547. desc->offset = 0;
  548. desc->flags = 0;
  549. if (ret)
  550. goto err;
  551. ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
  552. &desc->dev);
  553. if (ret) {
  554. debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
  555. goto err;
  556. }
  557. ret = gpio_find_and_xlate(desc, args);
  558. if (ret) {
  559. debug("%s: gpio_find_and_xlate failed\n", __func__);
  560. goto err;
  561. }
  562. ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
  563. ofnode_get_name(node),
  564. list_name, index);
  565. if (ret) {
  566. debug("%s: dm_gpio_requestf failed\n", __func__);
  567. goto err;
  568. }
  569. ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
  570. if (ret) {
  571. debug("%s: dm_gpio_set_dir failed\n", __func__);
  572. goto err;
  573. }
  574. return 0;
  575. err:
  576. debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
  577. __func__, ofnode_get_name(node), list_name, index, ret);
  578. return ret;
  579. }
  580. static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
  581. int index, struct gpio_desc *desc,
  582. int flags, bool add_index)
  583. {
  584. struct ofnode_phandle_args args;
  585. int ret;
  586. ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
  587. index, &args);
  588. return gpio_request_tail(ret, node, &args, list_name, index, desc,
  589. flags, add_index);
  590. }
  591. int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
  592. struct gpio_desc *desc, int flags)
  593. {
  594. return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
  595. index > 0);
  596. }
  597. int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
  598. struct gpio_desc *desc, int flags)
  599. {
  600. struct ofnode_phandle_args args;
  601. int ret;
  602. ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
  603. index, &args);
  604. return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
  605. index, desc, flags, index > 0);
  606. }
  607. int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
  608. struct gpio_desc *desc, int max_count,
  609. int flags)
  610. {
  611. int count;
  612. int ret;
  613. for (count = 0; count < max_count; count++) {
  614. ret = _gpio_request_by_name_nodev(node, list_name, count,
  615. &desc[count], flags, true);
  616. if (ret == -ENOENT)
  617. break;
  618. else if (ret)
  619. goto err;
  620. }
  621. /* We ran out of GPIOs in the list */
  622. return count;
  623. err:
  624. gpio_free_list_nodev(desc, count - 1);
  625. return ret;
  626. }
  627. int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
  628. struct gpio_desc *desc, int max_count,
  629. int flags)
  630. {
  631. /*
  632. * This isn't ideal since we don't use dev->name in the debug()
  633. * calls in gpio_request_by_name(), but we can do this until
  634. * gpio_request_list_by_name_nodev() can be dropped.
  635. */
  636. return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
  637. max_count, flags);
  638. }
  639. int gpio_get_list_count(struct udevice *dev, const char *list_name)
  640. {
  641. int ret;
  642. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
  643. list_name, "#gpio-cells", 0, -1,
  644. NULL);
  645. if (ret) {
  646. debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
  647. __func__, dev->name, list_name, ret);
  648. }
  649. return ret;
  650. }
  651. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
  652. {
  653. /* For now, we don't do any checking of dev */
  654. return _dm_gpio_free(desc->dev, desc->offset);
  655. }
  656. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
  657. {
  658. int i;
  659. /* For now, we don't do any checking of dev */
  660. for (i = 0; i < count; i++)
  661. dm_gpio_free(dev, &desc[i]);
  662. return 0;
  663. }
  664. int gpio_free_list_nodev(struct gpio_desc *desc, int count)
  665. {
  666. return gpio_free_list(NULL, desc, count);
  667. }
  668. /* We need to renumber the GPIOs when any driver is probed/removed */
  669. static int gpio_renumber(struct udevice *removed_dev)
  670. {
  671. struct gpio_dev_priv *uc_priv;
  672. struct udevice *dev;
  673. struct uclass *uc;
  674. unsigned base;
  675. int ret;
  676. ret = uclass_get(UCLASS_GPIO, &uc);
  677. if (ret)
  678. return ret;
  679. /* Ensure that we have a base for each bank */
  680. base = 0;
  681. uclass_foreach_dev(dev, uc) {
  682. if (device_active(dev) && dev != removed_dev) {
  683. uc_priv = dev_get_uclass_priv(dev);
  684. uc_priv->gpio_base = base;
  685. base += uc_priv->gpio_count;
  686. }
  687. }
  688. return 0;
  689. }
  690. int gpio_get_number(const struct gpio_desc *desc)
  691. {
  692. struct udevice *dev = desc->dev;
  693. struct gpio_dev_priv *uc_priv;
  694. if (!dev)
  695. return -1;
  696. uc_priv = dev->uclass_priv;
  697. return uc_priv->gpio_base + desc->offset;
  698. }
  699. static int gpio_post_probe(struct udevice *dev)
  700. {
  701. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  702. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  703. if (!uc_priv->name)
  704. return -ENOMEM;
  705. return gpio_renumber(NULL);
  706. }
  707. static int gpio_pre_remove(struct udevice *dev)
  708. {
  709. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  710. int i;
  711. for (i = 0; i < uc_priv->gpio_count; i++) {
  712. if (uc_priv->name[i])
  713. free(uc_priv->name[i]);
  714. }
  715. free(uc_priv->name);
  716. return gpio_renumber(dev);
  717. }
  718. UCLASS_DRIVER(gpio) = {
  719. .id = UCLASS_GPIO,
  720. .name = "gpio",
  721. .flags = DM_UC_FLAG_SEQ_ALIAS,
  722. .post_probe = gpio_post_probe,
  723. .pre_remove = gpio_pre_remove,
  724. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  725. };