gpio-uclass.c 33 KB

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