gpio-uclass.c 18 KB

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