gpio-uclass.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 >= 0 && 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,
  99. struct gpio_desc *desc,
  100. struct fdtdec_phandle_args *args)
  101. {
  102. if (args->args_count < 1)
  103. return -EINVAL;
  104. desc->offset = args->args[0];
  105. if (args->args_count < 2)
  106. return 0;
  107. if (args->args[1] & GPIO_ACTIVE_LOW)
  108. desc->flags = GPIOD_ACTIVE_LOW;
  109. return 0;
  110. }
  111. static int gpio_find_and_xlate(struct gpio_desc *desc,
  112. struct fdtdec_phandle_args *args)
  113. {
  114. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  115. if (ops->xlate)
  116. return ops->xlate(desc->dev, desc, args);
  117. else
  118. return gpio_xlate_offs_flags(desc->dev, desc, args);
  119. }
  120. int dm_gpio_request(struct gpio_desc *desc, const char *label)
  121. {
  122. struct udevice *dev = desc->dev;
  123. struct gpio_dev_priv *uc_priv;
  124. char *str;
  125. int ret;
  126. uc_priv = dev_get_uclass_priv(dev);
  127. if (uc_priv->name[desc->offset])
  128. return -EBUSY;
  129. str = strdup(label);
  130. if (!str)
  131. return -ENOMEM;
  132. if (gpio_get_ops(dev)->request) {
  133. ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
  134. if (ret) {
  135. free(str);
  136. return ret;
  137. }
  138. }
  139. uc_priv->name[desc->offset] = str;
  140. return 0;
  141. }
  142. static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
  143. {
  144. #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
  145. va_list args;
  146. char buf[40];
  147. va_start(args, fmt);
  148. vscnprintf(buf, sizeof(buf), fmt, args);
  149. va_end(args);
  150. return dm_gpio_request(desc, buf);
  151. #else
  152. return dm_gpio_request(desc, fmt);
  153. #endif
  154. }
  155. /**
  156. * gpio_request() - [COMPAT] Request GPIO
  157. * gpio: GPIO number
  158. * label: Name for the requested GPIO
  159. *
  160. * The label is copied and allocated so the caller does not need to keep
  161. * the pointer around.
  162. *
  163. * This function implements the API that's compatible with current
  164. * GPIO API used in U-Boot. The request is forwarded to particular
  165. * GPIO driver. Returns 0 on success, negative value on error.
  166. */
  167. int gpio_request(unsigned gpio, const char *label)
  168. {
  169. struct gpio_desc desc;
  170. int ret;
  171. ret = gpio_to_device(gpio, &desc);
  172. if (ret)
  173. return ret;
  174. return dm_gpio_request(&desc, label);
  175. }
  176. /**
  177. * gpio_requestf() - [COMPAT] Request GPIO
  178. * @gpio: GPIO number
  179. * @fmt: Format string for the requested GPIO
  180. * @...: Arguments for the printf() format string
  181. *
  182. * This function implements the API that's compatible with current
  183. * GPIO API used in U-Boot. The request is forwarded to particular
  184. * GPIO driver. Returns 0 on success, negative value on error.
  185. */
  186. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  187. {
  188. #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
  189. va_list args;
  190. char buf[40];
  191. va_start(args, fmt);
  192. vscnprintf(buf, sizeof(buf), fmt, args);
  193. va_end(args);
  194. return gpio_request(gpio, buf);
  195. #else
  196. return gpio_request(gpio, fmt);
  197. #endif
  198. }
  199. int _dm_gpio_free(struct udevice *dev, uint offset)
  200. {
  201. struct gpio_dev_priv *uc_priv;
  202. int ret;
  203. uc_priv = dev_get_uclass_priv(dev);
  204. if (!uc_priv->name[offset])
  205. return -ENXIO;
  206. if (gpio_get_ops(dev)->free) {
  207. ret = gpio_get_ops(dev)->free(dev, offset);
  208. if (ret)
  209. return ret;
  210. }
  211. free(uc_priv->name[offset]);
  212. uc_priv->name[offset] = NULL;
  213. return 0;
  214. }
  215. /**
  216. * gpio_free() - [COMPAT] Relinquish GPIO
  217. * gpio: GPIO number
  218. *
  219. * This function implements the API that's compatible with current
  220. * GPIO API used in U-Boot. The request is forwarded to particular
  221. * GPIO driver. Returns 0 on success, negative value on error.
  222. */
  223. int gpio_free(unsigned gpio)
  224. {
  225. struct gpio_desc desc;
  226. int ret;
  227. ret = gpio_to_device(gpio, &desc);
  228. if (ret)
  229. return ret;
  230. return _dm_gpio_free(desc.dev, desc.offset);
  231. }
  232. static int check_reserved(const struct gpio_desc *desc, const char *func)
  233. {
  234. struct gpio_dev_priv *uc_priv;
  235. if (!dm_gpio_is_valid(desc))
  236. return -ENOENT;
  237. uc_priv = dev_get_uclass_priv(desc->dev);
  238. if (!uc_priv->name[desc->offset]) {
  239. printf("%s: %s: error: gpio %s%d not reserved\n",
  240. desc->dev->name, func,
  241. uc_priv->bank_name ? uc_priv->bank_name : "",
  242. desc->offset);
  243. return -EBUSY;
  244. }
  245. return 0;
  246. }
  247. /**
  248. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  249. * gpio: GPIO number
  250. *
  251. * This function implements the API that's compatible with current
  252. * GPIO API used in U-Boot. The request is forwarded to particular
  253. * GPIO driver. Returns 0 on success, negative value on error.
  254. */
  255. int gpio_direction_input(unsigned gpio)
  256. {
  257. struct gpio_desc desc;
  258. int ret;
  259. ret = gpio_to_device(gpio, &desc);
  260. if (ret)
  261. return ret;
  262. ret = check_reserved(&desc, "dir_input");
  263. if (ret)
  264. return ret;
  265. return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
  266. }
  267. /**
  268. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  269. * gpio: GPIO number
  270. * value: Logical value to be set on the GPIO pin
  271. *
  272. * This function implements the API that's compatible with current
  273. * GPIO API used in U-Boot. The request is forwarded to particular
  274. * GPIO driver. Returns 0 on success, negative value on error.
  275. */
  276. int gpio_direction_output(unsigned gpio, int value)
  277. {
  278. struct gpio_desc desc;
  279. int ret;
  280. ret = gpio_to_device(gpio, &desc);
  281. if (ret)
  282. return ret;
  283. ret = check_reserved(&desc, "dir_output");
  284. if (ret)
  285. return ret;
  286. return gpio_get_ops(desc.dev)->direction_output(desc.dev,
  287. desc.offset, value);
  288. }
  289. int dm_gpio_get_value(const struct gpio_desc *desc)
  290. {
  291. int value;
  292. int ret;
  293. ret = check_reserved(desc, "get_value");
  294. if (ret)
  295. return ret;
  296. value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
  297. return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
  298. }
  299. int dm_gpio_set_value(const struct gpio_desc *desc, int value)
  300. {
  301. int ret;
  302. ret = check_reserved(desc, "set_value");
  303. if (ret)
  304. return ret;
  305. if (desc->flags & GPIOD_ACTIVE_LOW)
  306. value = !value;
  307. gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
  308. return 0;
  309. }
  310. int dm_gpio_get_open_drain(struct gpio_desc *desc)
  311. {
  312. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  313. int ret;
  314. ret = check_reserved(desc, "get_open_drain");
  315. if (ret)
  316. return ret;
  317. if (ops->set_open_drain)
  318. return ops->get_open_drain(desc->dev, desc->offset);
  319. else
  320. return -ENOSYS;
  321. }
  322. int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
  323. {
  324. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  325. int ret;
  326. ret = check_reserved(desc, "set_open_drain");
  327. if (ret)
  328. return ret;
  329. if (ops->set_open_drain)
  330. ret = ops->set_open_drain(desc->dev, desc->offset, value);
  331. else
  332. return 0; /* feature not supported -> ignore setting */
  333. return ret;
  334. }
  335. int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
  336. {
  337. struct udevice *dev = desc->dev;
  338. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  339. int ret;
  340. ret = check_reserved(desc, "set_dir");
  341. if (ret)
  342. return ret;
  343. if (flags & GPIOD_IS_OUT) {
  344. int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
  345. if (flags & GPIOD_ACTIVE_LOW)
  346. value = !value;
  347. ret = ops->direction_output(dev, desc->offset, value);
  348. } else if (flags & GPIOD_IS_IN) {
  349. ret = ops->direction_input(dev, desc->offset);
  350. }
  351. if (ret)
  352. return ret;
  353. /*
  354. * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
  355. * futures
  356. */
  357. desc->flags = flags;
  358. return 0;
  359. }
  360. int dm_gpio_set_dir(struct gpio_desc *desc)
  361. {
  362. return dm_gpio_set_dir_flags(desc, desc->flags);
  363. }
  364. /**
  365. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  366. * gpio: GPIO number
  367. *
  368. * This function implements the API that's compatible with current
  369. * GPIO API used in U-Boot. The request is forwarded to particular
  370. * GPIO driver. Returns the value of the GPIO pin, or negative value
  371. * on error.
  372. */
  373. int gpio_get_value(unsigned gpio)
  374. {
  375. int ret;
  376. struct gpio_desc desc;
  377. ret = gpio_to_device(gpio, &desc);
  378. if (ret)
  379. return ret;
  380. return dm_gpio_get_value(&desc);
  381. }
  382. /**
  383. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  384. * gpio: GPIO number
  385. * value: Logical value to be set on the GPIO pin.
  386. *
  387. * This function implements the API that's compatible with current
  388. * GPIO API used in U-Boot. The request is forwarded to particular
  389. * GPIO driver. Returns 0 on success, negative value on error.
  390. */
  391. int gpio_set_value(unsigned gpio, int value)
  392. {
  393. struct gpio_desc desc;
  394. int ret;
  395. ret = gpio_to_device(gpio, &desc);
  396. if (ret)
  397. return ret;
  398. return dm_gpio_set_value(&desc, value);
  399. }
  400. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  401. {
  402. struct gpio_dev_priv *priv;
  403. /* Must be called on an active device */
  404. priv = dev_get_uclass_priv(dev);
  405. assert(priv);
  406. *bit_count = priv->gpio_count;
  407. return priv->bank_name;
  408. }
  409. static const char * const gpio_function[GPIOF_COUNT] = {
  410. "input",
  411. "output",
  412. "unused",
  413. "unknown",
  414. "func",
  415. };
  416. int get_function(struct udevice *dev, int offset, bool skip_unused,
  417. const char **namep)
  418. {
  419. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  420. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  421. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  422. if (!device_active(dev))
  423. return -ENODEV;
  424. if (offset < 0 || offset >= uc_priv->gpio_count)
  425. return -EINVAL;
  426. if (namep)
  427. *namep = uc_priv->name[offset];
  428. if (skip_unused && !uc_priv->name[offset])
  429. return GPIOF_UNUSED;
  430. if (ops->get_function) {
  431. int ret;
  432. ret = ops->get_function(dev, offset);
  433. if (ret < 0)
  434. return ret;
  435. if (ret >= ARRAY_SIZE(gpio_function))
  436. return -ENODATA;
  437. return ret;
  438. }
  439. return GPIOF_UNKNOWN;
  440. }
  441. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  442. {
  443. return get_function(dev, offset, true, namep);
  444. }
  445. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  446. {
  447. return get_function(dev, offset, false, namep);
  448. }
  449. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  450. {
  451. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  452. struct gpio_dev_priv *priv;
  453. char *str = buf;
  454. int func;
  455. int ret;
  456. int len;
  457. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  458. *buf = 0;
  459. priv = dev_get_uclass_priv(dev);
  460. ret = gpio_get_raw_function(dev, offset, NULL);
  461. if (ret < 0)
  462. return ret;
  463. func = ret;
  464. len = snprintf(str, buffsize, "%s%d: %s",
  465. priv->bank_name ? priv->bank_name : "",
  466. offset, gpio_function[func]);
  467. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  468. func == GPIOF_UNUSED) {
  469. const char *label;
  470. bool used;
  471. ret = ops->get_value(dev, offset);
  472. if (ret < 0)
  473. return ret;
  474. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  475. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  476. ret,
  477. used ? 'x' : ' ',
  478. used ? " " : "",
  479. label ? label : "");
  480. }
  481. return 0;
  482. }
  483. int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
  484. {
  485. int i, ret;
  486. int gpio;
  487. for (i = 0; i < 32; i++) {
  488. gpio = gpio_num_array[i];
  489. if (gpio == -1)
  490. break;
  491. ret = gpio_requestf(gpio, fmt, i);
  492. if (ret)
  493. goto err;
  494. ret = gpio_direction_input(gpio);
  495. if (ret) {
  496. gpio_free(gpio);
  497. goto err;
  498. }
  499. }
  500. return 0;
  501. err:
  502. for (i--; i >= 0; i--)
  503. gpio_free(gpio_num_array[i]);
  504. return ret;
  505. }
  506. /*
  507. * get a number comprised of multiple GPIO values. gpio_num_array points to
  508. * the array of gpio pin numbers to scan, terminated by -1.
  509. */
  510. int gpio_get_values_as_int(const int *gpio_list)
  511. {
  512. int gpio;
  513. unsigned bitmask = 1;
  514. unsigned vector = 0;
  515. int ret;
  516. while (bitmask &&
  517. ((gpio = *gpio_list++) != -1)) {
  518. ret = gpio_get_value(gpio);
  519. if (ret < 0)
  520. return ret;
  521. else if (ret)
  522. vector |= bitmask;
  523. bitmask <<= 1;
  524. }
  525. return vector;
  526. }
  527. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
  528. {
  529. unsigned bitmask = 1;
  530. unsigned vector = 0;
  531. int ret, i;
  532. for (i = 0; i < count; i++) {
  533. ret = dm_gpio_get_value(&desc_list[i]);
  534. if (ret < 0)
  535. return ret;
  536. else if (ret)
  537. vector |= bitmask;
  538. bitmask <<= 1;
  539. }
  540. return vector;
  541. }
  542. static int _gpio_request_by_name_nodev(const void *blob, int node,
  543. const char *list_name, int index,
  544. struct gpio_desc *desc, int flags,
  545. bool add_index)
  546. {
  547. struct fdtdec_phandle_args args;
  548. int ret;
  549. desc->dev = NULL;
  550. desc->offset = 0;
  551. desc->flags = 0;
  552. ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
  553. "#gpio-cells", 0, index, &args);
  554. if (ret) {
  555. debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
  556. goto err;
  557. }
  558. ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
  559. &desc->dev);
  560. if (ret) {
  561. debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
  562. goto err;
  563. }
  564. ret = gpio_find_and_xlate(desc, &args);
  565. if (ret) {
  566. debug("%s: gpio_find_and_xlate failed\n", __func__);
  567. goto err;
  568. }
  569. ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
  570. fdt_get_name(blob, node, NULL),
  571. list_name, index);
  572. if (ret) {
  573. debug("%s: dm_gpio_requestf failed\n", __func__);
  574. goto err;
  575. }
  576. ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
  577. if (ret) {
  578. debug("%s: dm_gpio_set_dir failed\n", __func__);
  579. goto err;
  580. }
  581. return 0;
  582. err:
  583. debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
  584. __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
  585. return ret;
  586. }
  587. int gpio_request_by_name_nodev(const void *blob, int node,
  588. const char *list_name, int index,
  589. struct gpio_desc *desc, int flags)
  590. {
  591. return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
  592. flags, index > 0);
  593. }
  594. int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
  595. struct gpio_desc *desc, int flags)
  596. {
  597. /*
  598. * This isn't ideal since we don't use dev->name in the debug()
  599. * calls in gpio_request_by_name(), but we can do this until
  600. * gpio_request_by_name_nodev() can be dropped.
  601. */
  602. return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
  603. list_name, index, desc, flags);
  604. }
  605. int gpio_request_list_by_name_nodev(const void *blob, int node,
  606. 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(blob, 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(gd->fdt_blob, dev->of_offset,
  636. list_name, desc, max_count,
  637. 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,
  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. };