gpio-uclass.c 17 KB

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