gpio-uclass.c 29 KB

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