gpio-uclass.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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_set_dir_flags(struct gpio_desc *desc, ulong flags)
  311. {
  312. struct udevice *dev = desc->dev;
  313. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  314. int ret;
  315. ret = check_reserved(desc, "set_dir");
  316. if (ret)
  317. return ret;
  318. if (flags & GPIOD_IS_OUT) {
  319. int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
  320. if (flags & GPIOD_ACTIVE_LOW)
  321. value = !value;
  322. ret = ops->direction_output(dev, desc->offset, value);
  323. } else if (flags & GPIOD_IS_IN) {
  324. ret = ops->direction_input(dev, desc->offset);
  325. }
  326. if (ret)
  327. return ret;
  328. /*
  329. * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
  330. * futures
  331. */
  332. desc->flags = flags;
  333. return 0;
  334. }
  335. int dm_gpio_set_dir(struct gpio_desc *desc)
  336. {
  337. return dm_gpio_set_dir_flags(desc, desc->flags);
  338. }
  339. /**
  340. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  341. * gpio: GPIO number
  342. *
  343. * This function implements the API that's compatible with current
  344. * GPIO API used in U-Boot. The request is forwarded to particular
  345. * GPIO driver. Returns the value of the GPIO pin, or negative value
  346. * on error.
  347. */
  348. int gpio_get_value(unsigned gpio)
  349. {
  350. int ret;
  351. struct gpio_desc desc;
  352. ret = gpio_to_device(gpio, &desc);
  353. if (ret)
  354. return ret;
  355. return dm_gpio_get_value(&desc);
  356. }
  357. /**
  358. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  359. * gpio: GPIO number
  360. * value: Logical value to be set on the GPIO pin.
  361. *
  362. * This function implements the API that's compatible with current
  363. * GPIO API used in U-Boot. The request is forwarded to particular
  364. * GPIO driver. Returns 0 on success, negative value on error.
  365. */
  366. int gpio_set_value(unsigned gpio, int value)
  367. {
  368. struct gpio_desc desc;
  369. int ret;
  370. ret = gpio_to_device(gpio, &desc);
  371. if (ret)
  372. return ret;
  373. return dm_gpio_set_value(&desc, value);
  374. }
  375. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  376. {
  377. struct gpio_dev_priv *priv;
  378. /* Must be called on an active device */
  379. priv = dev_get_uclass_priv(dev);
  380. assert(priv);
  381. *bit_count = priv->gpio_count;
  382. return priv->bank_name;
  383. }
  384. static const char * const gpio_function[GPIOF_COUNT] = {
  385. "input",
  386. "output",
  387. "unused",
  388. "unknown",
  389. "func",
  390. };
  391. int get_function(struct udevice *dev, int offset, bool skip_unused,
  392. const char **namep)
  393. {
  394. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  395. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  396. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  397. if (!device_active(dev))
  398. return -ENODEV;
  399. if (offset < 0 || offset >= uc_priv->gpio_count)
  400. return -EINVAL;
  401. if (namep)
  402. *namep = uc_priv->name[offset];
  403. if (skip_unused && !uc_priv->name[offset])
  404. return GPIOF_UNUSED;
  405. if (ops->get_function) {
  406. int ret;
  407. ret = ops->get_function(dev, offset);
  408. if (ret < 0)
  409. return ret;
  410. if (ret >= ARRAY_SIZE(gpio_function))
  411. return -ENODATA;
  412. return ret;
  413. }
  414. return GPIOF_UNKNOWN;
  415. }
  416. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  417. {
  418. return get_function(dev, offset, true, namep);
  419. }
  420. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  421. {
  422. return get_function(dev, offset, false, namep);
  423. }
  424. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  425. {
  426. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  427. struct gpio_dev_priv *priv;
  428. char *str = buf;
  429. int func;
  430. int ret;
  431. int len;
  432. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  433. *buf = 0;
  434. priv = dev_get_uclass_priv(dev);
  435. ret = gpio_get_raw_function(dev, offset, NULL);
  436. if (ret < 0)
  437. return ret;
  438. func = ret;
  439. len = snprintf(str, buffsize, "%s%d: %s",
  440. priv->bank_name ? priv->bank_name : "",
  441. offset, gpio_function[func]);
  442. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  443. func == GPIOF_UNUSED) {
  444. const char *label;
  445. bool used;
  446. ret = ops->get_value(dev, offset);
  447. if (ret < 0)
  448. return ret;
  449. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  450. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  451. ret,
  452. used ? 'x' : ' ',
  453. used ? " " : "",
  454. label ? label : "");
  455. }
  456. return 0;
  457. }
  458. int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
  459. {
  460. int i, ret;
  461. int gpio;
  462. for (i = 0; i < 32; i++) {
  463. gpio = gpio_num_array[i];
  464. if (gpio == -1)
  465. break;
  466. ret = gpio_requestf(gpio, fmt, i);
  467. if (ret)
  468. goto err;
  469. ret = gpio_direction_input(gpio);
  470. if (ret) {
  471. gpio_free(gpio);
  472. goto err;
  473. }
  474. }
  475. return 0;
  476. err:
  477. for (i--; i >= 0; i--)
  478. gpio_free(gpio_num_array[i]);
  479. return ret;
  480. }
  481. /*
  482. * get a number comprised of multiple GPIO values. gpio_num_array points to
  483. * the array of gpio pin numbers to scan, terminated by -1.
  484. */
  485. int gpio_get_values_as_int(const int *gpio_list)
  486. {
  487. int gpio;
  488. unsigned bitmask = 1;
  489. unsigned vector = 0;
  490. int ret;
  491. while (bitmask &&
  492. ((gpio = *gpio_list++) != -1)) {
  493. ret = gpio_get_value(gpio);
  494. if (ret < 0)
  495. return ret;
  496. else if (ret)
  497. vector |= bitmask;
  498. bitmask <<= 1;
  499. }
  500. return vector;
  501. }
  502. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
  503. {
  504. unsigned bitmask = 1;
  505. unsigned vector = 0;
  506. int ret, i;
  507. for (i = 0; i < count; i++) {
  508. ret = dm_gpio_get_value(&desc_list[i]);
  509. if (ret < 0)
  510. return ret;
  511. else if (ret)
  512. vector |= bitmask;
  513. bitmask <<= 1;
  514. }
  515. return vector;
  516. }
  517. static int _gpio_request_by_name_nodev(const void *blob, int node,
  518. const char *list_name, int index,
  519. struct gpio_desc *desc, int flags,
  520. bool add_index)
  521. {
  522. struct fdtdec_phandle_args args;
  523. int ret;
  524. desc->dev = NULL;
  525. desc->offset = 0;
  526. desc->flags = 0;
  527. ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
  528. "#gpio-cells", 0, index, &args);
  529. if (ret) {
  530. debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
  531. goto err;
  532. }
  533. ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
  534. &desc->dev);
  535. if (ret) {
  536. debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
  537. goto err;
  538. }
  539. ret = gpio_find_and_xlate(desc, &args);
  540. if (ret) {
  541. debug("%s: gpio_find_and_xlate failed\n", __func__);
  542. goto err;
  543. }
  544. ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
  545. fdt_get_name(blob, node, NULL),
  546. list_name, index);
  547. if (ret) {
  548. debug("%s: dm_gpio_requestf failed\n", __func__);
  549. goto err;
  550. }
  551. ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
  552. if (ret) {
  553. debug("%s: dm_gpio_set_dir failed\n", __func__);
  554. goto err;
  555. }
  556. return 0;
  557. err:
  558. debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
  559. __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
  560. return ret;
  561. }
  562. int gpio_request_by_name_nodev(const void *blob, int node,
  563. const char *list_name, int index,
  564. struct gpio_desc *desc, int flags)
  565. {
  566. return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
  567. flags, index > 0);
  568. }
  569. int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
  570. struct gpio_desc *desc, int flags)
  571. {
  572. /*
  573. * This isn't ideal since we don't use dev->name in the debug()
  574. * calls in gpio_request_by_name(), but we can do this until
  575. * gpio_request_by_name_nodev() can be dropped.
  576. */
  577. return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
  578. list_name, index, desc, flags);
  579. }
  580. int gpio_request_list_by_name_nodev(const void *blob, int node,
  581. const char *list_name,
  582. struct gpio_desc *desc, int max_count,
  583. int flags)
  584. {
  585. int count;
  586. int ret;
  587. for (count = 0; count < max_count; count++) {
  588. ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
  589. &desc[count], flags, true);
  590. if (ret == -ENOENT)
  591. break;
  592. else if (ret)
  593. goto err;
  594. }
  595. /* We ran out of GPIOs in the list */
  596. return count;
  597. err:
  598. gpio_free_list_nodev(desc, count - 1);
  599. return ret;
  600. }
  601. int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
  602. struct gpio_desc *desc, int max_count,
  603. int flags)
  604. {
  605. /*
  606. * This isn't ideal since we don't use dev->name in the debug()
  607. * calls in gpio_request_by_name(), but we can do this until
  608. * gpio_request_list_by_name_nodev() can be dropped.
  609. */
  610. return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
  611. list_name, desc, max_count,
  612. flags);
  613. }
  614. int gpio_get_list_count(struct udevice *dev, const char *list_name)
  615. {
  616. int ret;
  617. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
  618. list_name, "#gpio-cells", 0, -1,
  619. NULL);
  620. if (ret) {
  621. debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
  622. __func__, dev->name, list_name, ret);
  623. }
  624. return ret;
  625. }
  626. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
  627. {
  628. /* For now, we don't do any checking of dev */
  629. return _dm_gpio_free(desc->dev, desc->offset);
  630. }
  631. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
  632. {
  633. int i;
  634. /* For now, we don't do any checking of dev */
  635. for (i = 0; i < count; i++)
  636. dm_gpio_free(dev, &desc[i]);
  637. return 0;
  638. }
  639. int gpio_free_list_nodev(struct gpio_desc *desc, int count)
  640. {
  641. return gpio_free_list(NULL, desc, count);
  642. }
  643. /* We need to renumber the GPIOs when any driver is probed/removed */
  644. static int gpio_renumber(struct udevice *removed_dev)
  645. {
  646. struct gpio_dev_priv *uc_priv;
  647. struct udevice *dev;
  648. struct uclass *uc;
  649. unsigned base;
  650. int ret;
  651. ret = uclass_get(UCLASS_GPIO, &uc);
  652. if (ret)
  653. return ret;
  654. /* Ensure that we have a base for each bank */
  655. base = 0;
  656. uclass_foreach_dev(dev, uc) {
  657. if (device_active(dev) && dev != removed_dev) {
  658. uc_priv = dev_get_uclass_priv(dev);
  659. uc_priv->gpio_base = base;
  660. base += uc_priv->gpio_count;
  661. }
  662. }
  663. return 0;
  664. }
  665. int gpio_get_number(const struct gpio_desc *desc)
  666. {
  667. struct udevice *dev = desc->dev;
  668. struct gpio_dev_priv *uc_priv;
  669. if (!dev)
  670. return -1;
  671. uc_priv = dev->uclass_priv;
  672. return uc_priv->gpio_base + desc->offset;
  673. }
  674. static int gpio_post_probe(struct udevice *dev)
  675. {
  676. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  677. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  678. if (!uc_priv->name)
  679. return -ENOMEM;
  680. return gpio_renumber(NULL);
  681. }
  682. static int gpio_pre_remove(struct udevice *dev)
  683. {
  684. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  685. int i;
  686. for (i = 0; i < uc_priv->gpio_count; i++) {
  687. if (uc_priv->name[i])
  688. free(uc_priv->name[i]);
  689. }
  690. free(uc_priv->name);
  691. return gpio_renumber(dev);
  692. }
  693. UCLASS_DRIVER(gpio) = {
  694. .id = UCLASS_GPIO,
  695. .name = "gpio",
  696. .flags = DM_UC_FLAG_SEQ_ALIAS,
  697. .post_probe = gpio_post_probe,
  698. .pre_remove = gpio_pre_remove,
  699. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  700. };