gpio-uclass.c 20 KB

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