gpio-uclass.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/device_compat.h>
  8. #include <dm/device-internal.h>
  9. #include <dm/lists.h>
  10. #include <dm/uclass-internal.h>
  11. #include <dt-bindings/gpio/gpio.h>
  12. #include <errno.h>
  13. #include <fdtdec.h>
  14. #include <malloc.h>
  15. #include <asm/gpio.h>
  16. #include <linux/bug.h>
  17. #include <linux/ctype.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /**
  20. * gpio_desc_init() - Initialize the GPIO descriptor
  21. *
  22. * @desc: GPIO descriptor to initialize
  23. * @dev: GPIO device
  24. * @offset: Offset of device GPIO
  25. */
  26. static void gpio_desc_init(struct gpio_desc *desc,
  27. struct udevice *dev,
  28. uint offset)
  29. {
  30. desc->dev = dev;
  31. desc->offset = offset;
  32. desc->flags = 0;
  33. }
  34. /**
  35. * gpio_to_device() - Convert global GPIO number to device, number
  36. *
  37. * Convert the GPIO number to an entry in the list of GPIOs
  38. * or GPIO blocks registered with the GPIO controller. Returns
  39. * entry on success, NULL on error.
  40. *
  41. * @gpio: The numeric representation of the GPIO
  42. * @desc: Returns description (desc->flags will always be 0)
  43. * @return 0 if found, -ENOENT if not found
  44. */
  45. static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
  46. {
  47. struct gpio_dev_priv *uc_priv;
  48. struct udevice *dev;
  49. int ret;
  50. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  51. dev;
  52. ret = uclass_next_device(&dev)) {
  53. uc_priv = dev_get_uclass_priv(dev);
  54. if (gpio >= uc_priv->gpio_base &&
  55. gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
  56. gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
  57. return 0;
  58. }
  59. }
  60. /* No such GPIO */
  61. return ret ? ret : -ENOENT;
  62. }
  63. int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
  64. {
  65. struct gpio_dev_priv *uc_priv = NULL;
  66. struct udevice *dev;
  67. ulong offset;
  68. int numeric;
  69. int ret;
  70. numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
  71. for (ret = uclass_first_device(UCLASS_GPIO, &dev);
  72. dev;
  73. ret = uclass_next_device(&dev)) {
  74. int len;
  75. uc_priv = dev_get_uclass_priv(dev);
  76. if (numeric != -1) {
  77. offset = numeric - uc_priv->gpio_base;
  78. /* Allow GPIOs to be numbered from 0 */
  79. if (offset < uc_priv->gpio_count)
  80. break;
  81. }
  82. len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
  83. if (!strncasecmp(name, uc_priv->bank_name, len)) {
  84. if (!strict_strtoul(name + len, 10, &offset))
  85. break;
  86. }
  87. }
  88. if (!dev)
  89. return ret ? ret : -EINVAL;
  90. gpio_desc_init(desc, dev, offset);
  91. return 0;
  92. }
  93. int gpio_lookup_name(const char *name, struct udevice **devp,
  94. unsigned int *offsetp, unsigned int *gpiop)
  95. {
  96. struct gpio_desc desc;
  97. int ret;
  98. if (devp)
  99. *devp = NULL;
  100. ret = dm_gpio_lookup_name(name, &desc);
  101. if (ret)
  102. return ret;
  103. if (devp)
  104. *devp = desc.dev;
  105. if (offsetp)
  106. *offsetp = desc.offset;
  107. if (gpiop) {
  108. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
  109. *gpiop = uc_priv->gpio_base + desc.offset;
  110. }
  111. return 0;
  112. }
  113. int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
  114. struct ofnode_phandle_args *args)
  115. {
  116. if (args->args_count < 1)
  117. return -EINVAL;
  118. desc->offset = args->args[0];
  119. if (args->args_count < 2)
  120. return 0;
  121. desc->flags = 0;
  122. if (args->args[1] & GPIO_ACTIVE_LOW)
  123. desc->flags |= GPIOD_ACTIVE_LOW;
  124. /*
  125. * need to test 2 bits for gpio output binding:
  126. * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
  127. * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
  128. */
  129. if (args->args[1] & GPIO_SINGLE_ENDED) {
  130. if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
  131. desc->flags |= GPIOD_OPEN_DRAIN;
  132. else
  133. desc->flags |= GPIOD_OPEN_SOURCE;
  134. }
  135. if (args->args[1] & GPIO_PULL_UP)
  136. desc->flags |= GPIOD_PULL_UP;
  137. if (args->args[1] & GPIO_PULL_DOWN)
  138. desc->flags |= GPIOD_PULL_DOWN;
  139. return 0;
  140. }
  141. static int gpio_find_and_xlate(struct gpio_desc *desc,
  142. struct ofnode_phandle_args *args)
  143. {
  144. struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
  145. if (ops->xlate)
  146. return ops->xlate(desc->dev, desc, args);
  147. else
  148. return gpio_xlate_offs_flags(desc->dev, desc, args);
  149. }
  150. #if defined(CONFIG_GPIO_HOG)
  151. struct gpio_hog_priv {
  152. struct gpio_desc gpiod;
  153. };
  154. struct gpio_hog_data {
  155. int gpiod_flags;
  156. int value;
  157. u32 val[2];
  158. };
  159. static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
  160. {
  161. struct gpio_hog_data *plat = dev_get_platdata(dev);
  162. const char *nodename;
  163. int ret;
  164. plat->value = 0;
  165. if (dev_read_bool(dev, "input")) {
  166. plat->gpiod_flags = GPIOD_IS_IN;
  167. } else if (dev_read_bool(dev, "output-high")) {
  168. plat->value = 1;
  169. plat->gpiod_flags = GPIOD_IS_OUT;
  170. } else if (dev_read_bool(dev, "output-low")) {
  171. plat->gpiod_flags = GPIOD_IS_OUT;
  172. } else {
  173. printf("%s: missing gpio-hog state.\n", __func__);
  174. return -EINVAL;
  175. }
  176. ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
  177. if (ret) {
  178. printf("%s: wrong gpios property, 2 values needed %d\n",
  179. __func__, ret);
  180. return ret;
  181. }
  182. nodename = dev_read_string(dev, "line-name");
  183. if (nodename)
  184. device_set_name(dev, nodename);
  185. return 0;
  186. }
  187. static int gpio_hog_probe(struct udevice *dev)
  188. {
  189. struct gpio_hog_data *plat = dev_get_platdata(dev);
  190. struct gpio_hog_priv *priv = dev_get_priv(dev);
  191. int ret;
  192. ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
  193. plat->val[0], plat->gpiod_flags,
  194. plat->val[1], &priv->gpiod);
  195. if (ret < 0) {
  196. debug("%s: node %s could not get gpio.\n", __func__,
  197. dev->name);
  198. return ret;
  199. }
  200. if (plat->gpiod_flags == GPIOD_IS_OUT) {
  201. ret = dm_gpio_set_value(&priv->gpiod, plat->value);
  202. if (ret < 0) {
  203. debug("%s: node %s could not set gpio.\n", __func__,
  204. dev->name);
  205. return ret;
  206. }
  207. }
  208. return 0;
  209. }
  210. int gpio_hog_probe_all(void)
  211. {
  212. struct udevice *dev;
  213. int ret;
  214. int retval = 0;
  215. for (uclass_first_device(UCLASS_NOP, &dev);
  216. dev;
  217. uclass_find_next_device(&dev)) {
  218. if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
  219. ret = device_probe(dev);
  220. if (ret) {
  221. printf("Failed to probe device %s err: %d\n",
  222. dev->name, ret);
  223. retval = ret;
  224. }
  225. }
  226. }
  227. return retval;
  228. }
  229. int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
  230. {
  231. struct udevice *dev;
  232. *desc = NULL;
  233. gpio_hog_probe_all();
  234. if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
  235. struct gpio_hog_priv *priv = dev_get_priv(dev);
  236. *desc = &priv->gpiod;
  237. return 0;
  238. }
  239. return -ENODEV;
  240. }
  241. U_BOOT_DRIVER(gpio_hog) = {
  242. .name = "gpio_hog",
  243. .id = UCLASS_NOP,
  244. .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
  245. .probe = gpio_hog_probe,
  246. .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
  247. .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
  248. };
  249. #else
  250. int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
  251. {
  252. return 0;
  253. }
  254. #endif
  255. int dm_gpio_request(struct gpio_desc *desc, const char *label)
  256. {
  257. struct udevice *dev = desc->dev;
  258. struct gpio_dev_priv *uc_priv;
  259. char *str;
  260. int ret;
  261. uc_priv = dev_get_uclass_priv(dev);
  262. if (uc_priv->name[desc->offset])
  263. return -EBUSY;
  264. str = strdup(label);
  265. if (!str)
  266. return -ENOMEM;
  267. if (gpio_get_ops(dev)->request) {
  268. ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
  269. if (ret) {
  270. free(str);
  271. return ret;
  272. }
  273. }
  274. uc_priv->name[desc->offset] = str;
  275. return 0;
  276. }
  277. static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
  278. {
  279. #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
  280. va_list args;
  281. char buf[40];
  282. va_start(args, fmt);
  283. vscnprintf(buf, sizeof(buf), fmt, args);
  284. va_end(args);
  285. return dm_gpio_request(desc, buf);
  286. #else
  287. return dm_gpio_request(desc, fmt);
  288. #endif
  289. }
  290. /**
  291. * gpio_request() - [COMPAT] Request GPIO
  292. * gpio: GPIO number
  293. * label: Name for the requested GPIO
  294. *
  295. * The label is copied and allocated so the caller does not need to keep
  296. * the pointer around.
  297. *
  298. * This function implements the API that's compatible with current
  299. * GPIO API used in U-Boot. The request is forwarded to particular
  300. * GPIO driver. Returns 0 on success, negative value on error.
  301. */
  302. int gpio_request(unsigned gpio, const char *label)
  303. {
  304. struct gpio_desc desc;
  305. int ret;
  306. ret = gpio_to_device(gpio, &desc);
  307. if (ret)
  308. return ret;
  309. return dm_gpio_request(&desc, label);
  310. }
  311. /**
  312. * gpio_requestf() - [COMPAT] Request GPIO
  313. * @gpio: GPIO number
  314. * @fmt: Format string for the requested GPIO
  315. * @...: Arguments for the printf() format string
  316. *
  317. * This function implements the API that's compatible with current
  318. * GPIO API used in U-Boot. The request is forwarded to particular
  319. * GPIO driver. Returns 0 on success, negative value on error.
  320. */
  321. int gpio_requestf(unsigned gpio, const char *fmt, ...)
  322. {
  323. #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
  324. va_list args;
  325. char buf[40];
  326. va_start(args, fmt);
  327. vscnprintf(buf, sizeof(buf), fmt, args);
  328. va_end(args);
  329. return gpio_request(gpio, buf);
  330. #else
  331. return gpio_request(gpio, fmt);
  332. #endif
  333. }
  334. int _dm_gpio_free(struct udevice *dev, uint offset)
  335. {
  336. struct gpio_dev_priv *uc_priv;
  337. int ret;
  338. uc_priv = dev_get_uclass_priv(dev);
  339. if (!uc_priv->name[offset])
  340. return -ENXIO;
  341. if (gpio_get_ops(dev)->rfree) {
  342. ret = gpio_get_ops(dev)->rfree(dev, offset);
  343. if (ret)
  344. return ret;
  345. }
  346. free(uc_priv->name[offset]);
  347. uc_priv->name[offset] = NULL;
  348. return 0;
  349. }
  350. /**
  351. * gpio_free() - [COMPAT] Relinquish GPIO
  352. * gpio: GPIO number
  353. *
  354. * This function implements the API that's compatible with current
  355. * GPIO API used in U-Boot. The request is forwarded to particular
  356. * GPIO driver. Returns 0 on success, negative value on error.
  357. */
  358. int gpio_free(unsigned gpio)
  359. {
  360. struct gpio_desc desc;
  361. int ret;
  362. ret = gpio_to_device(gpio, &desc);
  363. if (ret)
  364. return ret;
  365. return _dm_gpio_free(desc.dev, desc.offset);
  366. }
  367. static int check_reserved(const struct gpio_desc *desc, const char *func)
  368. {
  369. struct gpio_dev_priv *uc_priv;
  370. if (!dm_gpio_is_valid(desc))
  371. return -ENOENT;
  372. uc_priv = dev_get_uclass_priv(desc->dev);
  373. if (!uc_priv->name[desc->offset]) {
  374. printf("%s: %s: error: gpio %s%d not reserved\n",
  375. desc->dev->name, func,
  376. uc_priv->bank_name ? uc_priv->bank_name : "",
  377. desc->offset);
  378. return -EBUSY;
  379. }
  380. return 0;
  381. }
  382. /**
  383. * gpio_direction_input() - [COMPAT] Set GPIO direction to input
  384. * gpio: GPIO number
  385. *
  386. * This function implements the API that's compatible with current
  387. * GPIO API used in U-Boot. The request is forwarded to particular
  388. * GPIO driver. Returns 0 on success, negative value on error.
  389. */
  390. int gpio_direction_input(unsigned gpio)
  391. {
  392. struct gpio_desc desc;
  393. int ret;
  394. ret = gpio_to_device(gpio, &desc);
  395. if (ret)
  396. return ret;
  397. ret = check_reserved(&desc, "dir_input");
  398. if (ret)
  399. return ret;
  400. return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
  401. }
  402. /**
  403. * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
  404. * gpio: GPIO number
  405. * value: Logical value to be set on the GPIO pin
  406. *
  407. * This function implements the API that's compatible with current
  408. * GPIO API used in U-Boot. The request is forwarded to particular
  409. * GPIO driver. Returns 0 on success, negative value on error.
  410. */
  411. int gpio_direction_output(unsigned gpio, int value)
  412. {
  413. struct gpio_desc desc;
  414. int ret;
  415. ret = gpio_to_device(gpio, &desc);
  416. if (ret)
  417. return ret;
  418. ret = check_reserved(&desc, "dir_output");
  419. if (ret)
  420. return ret;
  421. return gpio_get_ops(desc.dev)->direction_output(desc.dev,
  422. desc.offset, value);
  423. }
  424. static int _gpio_get_value(const struct gpio_desc *desc)
  425. {
  426. int value;
  427. value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
  428. return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
  429. }
  430. int dm_gpio_get_value(const struct gpio_desc *desc)
  431. {
  432. int ret;
  433. ret = check_reserved(desc, "get_value");
  434. if (ret)
  435. return ret;
  436. return _gpio_get_value(desc);
  437. }
  438. int dm_gpio_set_value(const struct gpio_desc *desc, int value)
  439. {
  440. int ret;
  441. ret = check_reserved(desc, "set_value");
  442. if (ret)
  443. return ret;
  444. if (desc->flags & GPIOD_ACTIVE_LOW)
  445. value = !value;
  446. /*
  447. * Emulate open drain by not actively driving the line high or
  448. * Emulate open source by not actively driving the line low
  449. */
  450. if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
  451. (desc->flags & GPIOD_OPEN_SOURCE && !value))
  452. return gpio_get_ops(desc->dev)->direction_input(desc->dev,
  453. desc->offset);
  454. else if (desc->flags & GPIOD_OPEN_DRAIN ||
  455. desc->flags & GPIOD_OPEN_SOURCE)
  456. return gpio_get_ops(desc->dev)->direction_output(desc->dev,
  457. desc->offset,
  458. value);
  459. gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
  460. return 0;
  461. }
  462. /* check dir flags invalid configuration */
  463. static int check_dir_flags(ulong flags)
  464. {
  465. if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
  466. log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
  467. __func__, flags);
  468. return -EINVAL;
  469. }
  470. if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
  471. log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
  472. __func__, flags);
  473. return -EINVAL;
  474. }
  475. if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
  476. log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
  477. __func__, flags);
  478. return -EINVAL;
  479. }
  480. return 0;
  481. }
  482. static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
  483. {
  484. struct udevice *dev = desc->dev;
  485. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  486. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  487. int ret = 0;
  488. ret = check_dir_flags(flags);
  489. if (ret) {
  490. dev_dbg(dev,
  491. "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
  492. desc->dev->name,
  493. uc_priv->bank_name ? uc_priv->bank_name : "",
  494. desc->offset, flags);
  495. return ret;
  496. }
  497. /* GPIOD_ are directly managed by driver in set_dir_flags*/
  498. if (ops->set_dir_flags) {
  499. ret = ops->set_dir_flags(dev, desc->offset, flags);
  500. } else {
  501. if (flags & GPIOD_IS_OUT) {
  502. ret = ops->direction_output(dev, desc->offset,
  503. GPIOD_FLAGS_OUTPUT(flags));
  504. } else if (flags & GPIOD_IS_IN) {
  505. ret = ops->direction_input(dev, desc->offset);
  506. }
  507. }
  508. return ret;
  509. }
  510. int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
  511. {
  512. int ret;
  513. ret = check_reserved(desc, "set_dir_flags");
  514. if (ret)
  515. return ret;
  516. /* combine the requested flags (for IN/OUT) and the descriptor flags */
  517. flags |= desc->flags;
  518. ret = _dm_gpio_set_dir_flags(desc, flags);
  519. /* update the descriptor flags */
  520. if (ret)
  521. desc->flags = flags;
  522. return ret;
  523. }
  524. int dm_gpio_set_dir(struct gpio_desc *desc)
  525. {
  526. int ret;
  527. ret = check_reserved(desc, "set_dir");
  528. if (ret)
  529. return ret;
  530. return _dm_gpio_set_dir_flags(desc, desc->flags);
  531. }
  532. int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
  533. {
  534. struct udevice *dev = desc->dev;
  535. int ret, value;
  536. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  537. ulong dir_flags;
  538. ret = check_reserved(desc, "get_dir_flags");
  539. if (ret)
  540. return ret;
  541. /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
  542. if (ops->get_dir_flags) {
  543. ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
  544. if (ret)
  545. return ret;
  546. /* GPIOD_ACTIVE_LOW is saved in desc->flags */
  547. value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
  548. if (desc->flags & GPIOD_ACTIVE_LOW)
  549. value = !value;
  550. dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
  551. dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
  552. if (value)
  553. dir_flags |= GPIOD_IS_OUT_ACTIVE;
  554. } else {
  555. dir_flags = desc->flags;
  556. /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
  557. dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
  558. if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
  559. dir_flags |= GPIOD_IS_OUT_ACTIVE;
  560. }
  561. *flags = dir_flags;
  562. return 0;
  563. }
  564. /**
  565. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  566. * gpio: GPIO number
  567. *
  568. * This function implements the API that's compatible with current
  569. * GPIO API used in U-Boot. The request is forwarded to particular
  570. * GPIO driver. Returns the value of the GPIO pin, or negative value
  571. * on error.
  572. */
  573. int gpio_get_value(unsigned gpio)
  574. {
  575. int ret;
  576. struct gpio_desc desc;
  577. ret = gpio_to_device(gpio, &desc);
  578. if (ret)
  579. return ret;
  580. return dm_gpio_get_value(&desc);
  581. }
  582. /**
  583. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  584. * gpio: GPIO number
  585. * value: Logical value to be set on the GPIO pin.
  586. *
  587. * This function implements the API that's compatible with current
  588. * GPIO API used in U-Boot. The request is forwarded to particular
  589. * GPIO driver. Returns 0 on success, negative value on error.
  590. */
  591. int gpio_set_value(unsigned gpio, int value)
  592. {
  593. struct gpio_desc desc;
  594. int ret;
  595. ret = gpio_to_device(gpio, &desc);
  596. if (ret)
  597. return ret;
  598. return dm_gpio_set_value(&desc, value);
  599. }
  600. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  601. {
  602. struct gpio_dev_priv *priv;
  603. /* Must be called on an active device */
  604. priv = dev_get_uclass_priv(dev);
  605. assert(priv);
  606. *bit_count = priv->gpio_count;
  607. return priv->bank_name;
  608. }
  609. static const char * const gpio_function[GPIOF_COUNT] = {
  610. "input",
  611. "output",
  612. "unused",
  613. "unknown",
  614. "func",
  615. };
  616. static int get_function(struct udevice *dev, int offset, bool skip_unused,
  617. const char **namep)
  618. {
  619. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  620. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  621. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  622. if (!device_active(dev))
  623. return -ENODEV;
  624. if (offset < 0 || offset >= uc_priv->gpio_count)
  625. return -EINVAL;
  626. if (namep)
  627. *namep = uc_priv->name[offset];
  628. if (skip_unused && !uc_priv->name[offset])
  629. return GPIOF_UNUSED;
  630. if (ops->get_function) {
  631. int ret;
  632. ret = ops->get_function(dev, offset);
  633. if (ret < 0)
  634. return ret;
  635. if (ret >= ARRAY_SIZE(gpio_function))
  636. return -ENODATA;
  637. return ret;
  638. }
  639. return GPIOF_UNKNOWN;
  640. }
  641. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  642. {
  643. return get_function(dev, offset, true, namep);
  644. }
  645. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  646. {
  647. return get_function(dev, offset, false, namep);
  648. }
  649. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  650. {
  651. struct dm_gpio_ops *ops = gpio_get_ops(dev);
  652. struct gpio_dev_priv *priv;
  653. char *str = buf;
  654. int func;
  655. int ret;
  656. int len;
  657. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  658. *buf = 0;
  659. priv = dev_get_uclass_priv(dev);
  660. ret = gpio_get_raw_function(dev, offset, NULL);
  661. if (ret < 0)
  662. return ret;
  663. func = ret;
  664. len = snprintf(str, buffsize, "%s%d: %s",
  665. priv->bank_name ? priv->bank_name : "",
  666. offset, gpio_function[func]);
  667. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  668. func == GPIOF_UNUSED) {
  669. const char *label;
  670. bool used;
  671. ret = ops->get_value(dev, offset);
  672. if (ret < 0)
  673. return ret;
  674. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  675. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  676. ret,
  677. used ? 'x' : ' ',
  678. used ? " " : "",
  679. label ? label : "");
  680. }
  681. return 0;
  682. }
  683. int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
  684. {
  685. int i, ret;
  686. int gpio;
  687. for (i = 0; i < 32; i++) {
  688. gpio = gpio_num_array[i];
  689. if (gpio == -1)
  690. break;
  691. ret = gpio_requestf(gpio, fmt, i);
  692. if (ret)
  693. goto err;
  694. ret = gpio_direction_input(gpio);
  695. if (ret) {
  696. gpio_free(gpio);
  697. goto err;
  698. }
  699. }
  700. return 0;
  701. err:
  702. for (i--; i >= 0; i--)
  703. gpio_free(gpio_num_array[i]);
  704. return ret;
  705. }
  706. /*
  707. * get a number comprised of multiple GPIO values. gpio_num_array points to
  708. * the array of gpio pin numbers to scan, terminated by -1.
  709. */
  710. int gpio_get_values_as_int(const int *gpio_list)
  711. {
  712. int gpio;
  713. unsigned bitmask = 1;
  714. unsigned vector = 0;
  715. int ret;
  716. while (bitmask &&
  717. ((gpio = *gpio_list++) != -1)) {
  718. ret = gpio_get_value(gpio);
  719. if (ret < 0)
  720. return ret;
  721. else if (ret)
  722. vector |= bitmask;
  723. bitmask <<= 1;
  724. }
  725. return vector;
  726. }
  727. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
  728. {
  729. unsigned bitmask = 1;
  730. unsigned vector = 0;
  731. int ret, i;
  732. for (i = 0; i < count; i++) {
  733. ret = dm_gpio_get_value(&desc_list[i]);
  734. if (ret < 0)
  735. return ret;
  736. else if (ret)
  737. vector |= bitmask;
  738. bitmask <<= 1;
  739. }
  740. return vector;
  741. }
  742. /**
  743. * gpio_request_tail: common work for requesting a gpio.
  744. *
  745. * ret: return value from previous work in function which calls
  746. * this function.
  747. * This seems bogus (why calling this function instead not
  748. * calling it and end caller function instead?).
  749. * Because on error in caller function we want to set some
  750. * default values in gpio desc and have a common error
  751. * debug message, which provides this function.
  752. * nodename: Name of node for which gpio gets requested
  753. * used for gpio label name.
  754. * args: pointer to output arguments structure
  755. * list_name: Name of GPIO list
  756. * used for gpio label name.
  757. * index: gpio index in gpio list
  758. * used for gpio label name.
  759. * desc: pointer to gpio descriptor, filled from this
  760. * function.
  761. * flags: gpio flags to use.
  762. * add_index: should index added to gpio label name
  763. * gpio_dev: pointer to gpio device from which the gpio
  764. * will be requested. If NULL try to get the
  765. * gpio device with uclass_get_device_by_ofnode()
  766. *
  767. * return: In error case this function sets default values in
  768. * gpio descriptor, also emmits a debug message.
  769. * On success it returns 0 else the error code from
  770. * function calls, or the error code passed through
  771. * ret to this function.
  772. *
  773. */
  774. static int gpio_request_tail(int ret, const char *nodename,
  775. struct ofnode_phandle_args *args,
  776. const char *list_name, int index,
  777. struct gpio_desc *desc, int flags,
  778. bool add_index, struct udevice *gpio_dev)
  779. {
  780. gpio_desc_init(desc, gpio_dev, 0);
  781. if (ret)
  782. goto err;
  783. if (!desc->dev) {
  784. ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
  785. &desc->dev);
  786. if (ret) {
  787. debug("%s: uclass_get_device_by_ofnode failed\n",
  788. __func__);
  789. goto err;
  790. }
  791. }
  792. ret = gpio_find_and_xlate(desc, args);
  793. if (ret) {
  794. debug("%s: gpio_find_and_xlate failed\n", __func__);
  795. goto err;
  796. }
  797. ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
  798. nodename, list_name, index);
  799. if (ret) {
  800. debug("%s: dm_gpio_requestf failed\n", __func__);
  801. goto err;
  802. }
  803. ret = dm_gpio_set_dir_flags(desc, flags);
  804. if (ret) {
  805. debug("%s: dm_gpio_set_dir failed\n", __func__);
  806. goto err;
  807. }
  808. return 0;
  809. err:
  810. debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
  811. __func__, nodename, list_name, index, ret);
  812. return ret;
  813. }
  814. static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
  815. int index, struct gpio_desc *desc,
  816. int flags, bool add_index)
  817. {
  818. struct ofnode_phandle_args args;
  819. int ret;
  820. ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
  821. index, &args);
  822. return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
  823. index, desc, flags, add_index, NULL);
  824. }
  825. int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
  826. struct gpio_desc *desc, int flags)
  827. {
  828. return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
  829. index > 0);
  830. }
  831. int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
  832. struct gpio_desc *desc, int flags)
  833. {
  834. struct ofnode_phandle_args args;
  835. ofnode node;
  836. int ret;
  837. ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
  838. index, &args);
  839. node = dev_ofnode(dev);
  840. return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
  841. index, desc, flags, index > 0, NULL);
  842. }
  843. int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
  844. struct gpio_desc *desc, int max_count,
  845. int flags)
  846. {
  847. int count;
  848. int ret;
  849. for (count = 0; count < max_count; count++) {
  850. ret = _gpio_request_by_name_nodev(node, list_name, count,
  851. &desc[count], flags, true);
  852. if (ret == -ENOENT)
  853. break;
  854. else if (ret)
  855. goto err;
  856. }
  857. /* We ran out of GPIOs in the list */
  858. return count;
  859. err:
  860. gpio_free_list_nodev(desc, count - 1);
  861. return ret;
  862. }
  863. int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
  864. struct gpio_desc *desc, int max_count,
  865. int flags)
  866. {
  867. /*
  868. * This isn't ideal since we don't use dev->name in the debug()
  869. * calls in gpio_request_by_name(), but we can do this until
  870. * gpio_request_list_by_name_nodev() can be dropped.
  871. */
  872. return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
  873. max_count, flags);
  874. }
  875. int gpio_get_list_count(struct udevice *dev, const char *list_name)
  876. {
  877. int ret;
  878. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
  879. list_name, "#gpio-cells", 0, -1,
  880. NULL);
  881. if (ret) {
  882. debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
  883. __func__, dev->name, list_name, ret);
  884. }
  885. return ret;
  886. }
  887. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
  888. {
  889. /* For now, we don't do any checking of dev */
  890. return _dm_gpio_free(desc->dev, desc->offset);
  891. }
  892. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
  893. {
  894. int i;
  895. /* For now, we don't do any checking of dev */
  896. for (i = 0; i < count; i++)
  897. dm_gpio_free(dev, &desc[i]);
  898. return 0;
  899. }
  900. int gpio_free_list_nodev(struct gpio_desc *desc, int count)
  901. {
  902. return gpio_free_list(NULL, desc, count);
  903. }
  904. /* We need to renumber the GPIOs when any driver is probed/removed */
  905. static int gpio_renumber(struct udevice *removed_dev)
  906. {
  907. struct gpio_dev_priv *uc_priv;
  908. struct udevice *dev;
  909. struct uclass *uc;
  910. unsigned base;
  911. int ret;
  912. ret = uclass_get(UCLASS_GPIO, &uc);
  913. if (ret)
  914. return ret;
  915. /* Ensure that we have a base for each bank */
  916. base = 0;
  917. uclass_foreach_dev(dev, uc) {
  918. if (device_active(dev) && dev != removed_dev) {
  919. uc_priv = dev_get_uclass_priv(dev);
  920. uc_priv->gpio_base = base;
  921. base += uc_priv->gpio_count;
  922. }
  923. }
  924. return 0;
  925. }
  926. int gpio_get_number(const struct gpio_desc *desc)
  927. {
  928. struct udevice *dev = desc->dev;
  929. struct gpio_dev_priv *uc_priv;
  930. if (!dev)
  931. return -1;
  932. uc_priv = dev->uclass_priv;
  933. return uc_priv->gpio_base + desc->offset;
  934. }
  935. static int gpio_post_probe(struct udevice *dev)
  936. {
  937. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  938. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  939. if (!uc_priv->name)
  940. return -ENOMEM;
  941. return gpio_renumber(NULL);
  942. }
  943. static int gpio_pre_remove(struct udevice *dev)
  944. {
  945. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  946. int i;
  947. for (i = 0; i < uc_priv->gpio_count; i++) {
  948. if (uc_priv->name[i])
  949. free(uc_priv->name[i]);
  950. }
  951. free(uc_priv->name);
  952. return gpio_renumber(dev);
  953. }
  954. int gpio_dev_request_index(struct udevice *dev, const char *nodename,
  955. char *list_name, int index, int flags,
  956. int dtflags, struct gpio_desc *desc)
  957. {
  958. struct ofnode_phandle_args args;
  959. args.node = ofnode_null();
  960. args.args_count = 2;
  961. args.args[0] = index;
  962. args.args[1] = dtflags;
  963. return gpio_request_tail(0, nodename, &args, list_name, index, desc,
  964. flags, 0, dev);
  965. }
  966. static int gpio_post_bind(struct udevice *dev)
  967. {
  968. struct udevice *child;
  969. ofnode node;
  970. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  971. struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
  972. static int reloc_done;
  973. if (!reloc_done) {
  974. if (ops->request)
  975. ops->request += gd->reloc_off;
  976. if (ops->rfree)
  977. ops->rfree += gd->reloc_off;
  978. if (ops->direction_input)
  979. ops->direction_input += gd->reloc_off;
  980. if (ops->direction_output)
  981. ops->direction_output += gd->reloc_off;
  982. if (ops->get_value)
  983. ops->get_value += gd->reloc_off;
  984. if (ops->set_value)
  985. ops->set_value += gd->reloc_off;
  986. if (ops->get_function)
  987. ops->get_function += gd->reloc_off;
  988. if (ops->xlate)
  989. ops->xlate += gd->reloc_off;
  990. if (ops->set_dir_flags)
  991. ops->set_dir_flags += gd->reloc_off;
  992. if (ops->get_dir_flags)
  993. ops->get_dir_flags += gd->reloc_off;
  994. reloc_done++;
  995. }
  996. #endif
  997. if (IS_ENABLED(CONFIG_GPIO_HOG)) {
  998. dev_for_each_subnode(node, dev) {
  999. if (ofnode_read_bool(node, "gpio-hog")) {
  1000. const char *name = ofnode_get_name(node);
  1001. int ret;
  1002. ret = device_bind_driver_to_node(dev,
  1003. "gpio_hog",
  1004. name, node,
  1005. &child);
  1006. if (ret)
  1007. return ret;
  1008. }
  1009. }
  1010. }
  1011. return 0;
  1012. }
  1013. UCLASS_DRIVER(gpio) = {
  1014. .id = UCLASS_GPIO,
  1015. .name = "gpio",
  1016. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1017. .post_probe = gpio_post_probe,
  1018. .post_bind = gpio_post_bind,
  1019. .pre_remove = gpio_pre_remove,
  1020. .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
  1021. };