gpio-uclass.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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) ? simple_strtoul(name, NULL, 10) : -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_gpio_set_dir(struct gpio_desc *desc)
  606. {
  607. int ret;
  608. ret = check_reserved(desc, "set_dir");
  609. if (ret)
  610. return ret;
  611. return _dm_gpio_set_flags(desc, desc->flags);
  612. }
  613. int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
  614. ulong set)
  615. {
  616. int ret;
  617. int i;
  618. for (i = 0; i < count; i++) {
  619. ret = dm_gpio_clrset_flags(&desc[i], clr, set);
  620. if (ret)
  621. return log_ret(ret);
  622. }
  623. return 0;
  624. }
  625. int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
  626. {
  627. struct udevice *dev = desc->dev;
  628. int ret, value;
  629. const struct dm_gpio_ops *ops = gpio_get_ops(dev);
  630. ulong flags;
  631. ret = check_reserved(desc, "get_flags");
  632. if (ret)
  633. return ret;
  634. /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
  635. if (ops->get_flags) {
  636. ret = ops->get_flags(dev, desc->offset, &flags);
  637. if (ret)
  638. return ret;
  639. /* GPIOD_ACTIVE_LOW is saved in desc->flags */
  640. value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
  641. if (desc->flags & GPIOD_ACTIVE_LOW)
  642. value = !value;
  643. flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
  644. flags |= (desc->flags & GPIOD_ACTIVE_LOW);
  645. if (value)
  646. flags |= GPIOD_IS_OUT_ACTIVE;
  647. } else {
  648. flags = desc->flags;
  649. /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
  650. flags &= ~GPIOD_IS_OUT_ACTIVE;
  651. if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
  652. flags |= GPIOD_IS_OUT_ACTIVE;
  653. }
  654. *flagsp = flags;
  655. return 0;
  656. }
  657. /**
  658. * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
  659. * gpio: GPIO number
  660. *
  661. * This function implements the API that's compatible with current
  662. * GPIO API used in U-Boot. The request is forwarded to particular
  663. * GPIO driver. Returns the value of the GPIO pin, or negative value
  664. * on error.
  665. */
  666. int gpio_get_value(unsigned gpio)
  667. {
  668. int ret;
  669. struct gpio_desc desc;
  670. ret = gpio_to_device(gpio, &desc);
  671. if (ret)
  672. return ret;
  673. return dm_gpio_get_value(&desc);
  674. }
  675. /**
  676. * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
  677. * gpio: GPIO number
  678. * value: Logical value to be set on the GPIO pin.
  679. *
  680. * This function implements the API that's compatible with current
  681. * GPIO API used in U-Boot. The request is forwarded to particular
  682. * GPIO driver. Returns 0 on success, negative value on error.
  683. */
  684. int gpio_set_value(unsigned gpio, int value)
  685. {
  686. struct gpio_desc desc;
  687. int ret;
  688. ret = gpio_to_device(gpio, &desc);
  689. if (ret)
  690. return ret;
  691. return dm_gpio_set_value(&desc, value);
  692. }
  693. const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
  694. {
  695. struct gpio_dev_priv *priv;
  696. /* Must be called on an active device */
  697. priv = dev_get_uclass_priv(dev);
  698. assert(priv);
  699. *bit_count = priv->gpio_count;
  700. return priv->bank_name;
  701. }
  702. static const char * const gpio_function[GPIOF_COUNT] = {
  703. "input",
  704. "output",
  705. "unused",
  706. "unknown",
  707. "func",
  708. };
  709. static int get_function(struct udevice *dev, int offset, bool skip_unused,
  710. const char **namep)
  711. {
  712. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  713. const struct dm_gpio_ops *ops = gpio_get_ops(dev);
  714. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  715. if (!device_active(dev))
  716. return -ENODEV;
  717. if (offset < 0 || offset >= uc_priv->gpio_count)
  718. return -EINVAL;
  719. if (namep)
  720. *namep = uc_priv->name[offset];
  721. if (skip_unused && !uc_priv->name[offset])
  722. return GPIOF_UNUSED;
  723. if (ops->get_function) {
  724. int ret;
  725. ret = ops->get_function(dev, offset);
  726. if (ret < 0)
  727. return ret;
  728. if (ret >= ARRAY_SIZE(gpio_function))
  729. return -ENODATA;
  730. return ret;
  731. }
  732. return GPIOF_UNKNOWN;
  733. }
  734. int gpio_get_function(struct udevice *dev, int offset, const char **namep)
  735. {
  736. return get_function(dev, offset, true, namep);
  737. }
  738. int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
  739. {
  740. return get_function(dev, offset, false, namep);
  741. }
  742. int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
  743. {
  744. const struct dm_gpio_ops *ops = gpio_get_ops(dev);
  745. struct gpio_dev_priv *priv;
  746. char *str = buf;
  747. int func;
  748. int ret;
  749. int len;
  750. BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
  751. *buf = 0;
  752. priv = dev_get_uclass_priv(dev);
  753. ret = gpio_get_raw_function(dev, offset, NULL);
  754. if (ret < 0)
  755. return ret;
  756. func = ret;
  757. len = snprintf(str, buffsize, "%s%d: %s",
  758. priv->bank_name ? priv->bank_name : "",
  759. offset, gpio_function[func]);
  760. if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
  761. func == GPIOF_UNUSED) {
  762. const char *label;
  763. bool used;
  764. ret = ops->get_value(dev, offset);
  765. if (ret < 0)
  766. return ret;
  767. used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
  768. snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
  769. ret,
  770. used ? 'x' : ' ',
  771. used ? " " : "",
  772. label ? label : "");
  773. }
  774. return 0;
  775. }
  776. #if CONFIG_IS_ENABLED(ACPIGEN)
  777. int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
  778. {
  779. const struct dm_gpio_ops *ops;
  780. memset(gpio, '\0', sizeof(*gpio));
  781. if (!dm_gpio_is_valid(desc)) {
  782. /* Indicate that the GPIO is not valid */
  783. gpio->pin_count = 0;
  784. gpio->pins[0] = 0;
  785. return -EINVAL;
  786. }
  787. ops = gpio_get_ops(desc->dev);
  788. if (!ops->get_acpi)
  789. return -ENOSYS;
  790. return ops->get_acpi(desc, gpio);
  791. }
  792. #endif
  793. int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
  794. {
  795. int i, ret;
  796. int gpio;
  797. for (i = 0; i < 32; i++) {
  798. gpio = gpio_num_array[i];
  799. if (gpio == -1)
  800. break;
  801. ret = gpio_requestf(gpio, fmt, i);
  802. if (ret)
  803. goto err;
  804. ret = gpio_direction_input(gpio);
  805. if (ret) {
  806. gpio_free(gpio);
  807. goto err;
  808. }
  809. }
  810. return 0;
  811. err:
  812. for (i--; i >= 0; i--)
  813. gpio_free(gpio_num_array[i]);
  814. return ret;
  815. }
  816. /*
  817. * get a number comprised of multiple GPIO values. gpio_num_array points to
  818. * the array of gpio pin numbers to scan, terminated by -1.
  819. */
  820. int gpio_get_values_as_int(const int *gpio_list)
  821. {
  822. int gpio;
  823. unsigned bitmask = 1;
  824. unsigned vector = 0;
  825. int ret;
  826. while (bitmask &&
  827. ((gpio = *gpio_list++) != -1)) {
  828. ret = gpio_get_value(gpio);
  829. if (ret < 0)
  830. return ret;
  831. else if (ret)
  832. vector |= bitmask;
  833. bitmask <<= 1;
  834. }
  835. return vector;
  836. }
  837. int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
  838. {
  839. unsigned bitmask = 1;
  840. unsigned vector = 0;
  841. int ret, i;
  842. for (i = 0; i < count; i++) {
  843. ret = dm_gpio_get_value(&desc_list[i]);
  844. if (ret < 0)
  845. return ret;
  846. else if (ret)
  847. vector |= bitmask;
  848. bitmask <<= 1;
  849. }
  850. return vector;
  851. }
  852. int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
  853. int count)
  854. {
  855. static const char tristate[] = "01z";
  856. enum {
  857. PULLUP,
  858. PULLDOWN,
  859. NUM_OPTIONS,
  860. };
  861. int vals[NUM_OPTIONS];
  862. uint mask;
  863. uint vector = 0;
  864. int ret, i;
  865. /*
  866. * Limit to 19 digits which should be plenty. This avoids overflow of a
  867. * 32-bit int
  868. */
  869. assert(count < 20);
  870. for (i = 0; i < NUM_OPTIONS; i++) {
  871. uint flags = GPIOD_IS_IN;
  872. flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
  873. ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
  874. flags);
  875. if (ret)
  876. return log_msg_ret("pu", ret);
  877. /* Give the lines time to settle */
  878. udelay(10);
  879. ret = dm_gpio_get_values_as_int(desc_list, count);
  880. if (ret < 0)
  881. return log_msg_ret("get1", ret);
  882. vals[i] = ret;
  883. }
  884. log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
  885. for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
  886. uint pd = vals[PULLDOWN] & mask ? 1 : 0;
  887. uint pu = vals[PULLUP] & mask ? 1 : 0;
  888. uint digit;
  889. /*
  890. * Get value with internal pulldown active. If this is 1 then
  891. * there is a stronger external pullup, which we call 1. If not
  892. * then call it 0.
  893. *
  894. * If the values differ then the pin is floating so we call
  895. * this a 2.
  896. */
  897. if (pu == pd)
  898. digit = pd;
  899. else
  900. digit = 2;
  901. log_debug("%c ", tristate[digit]);
  902. vector = 3 * vector + digit;
  903. }
  904. log_debug("vector=%d\n", vector);
  905. return vector;
  906. }
  907. /**
  908. * gpio_request_tail: common work for requesting a gpio.
  909. *
  910. * ret: return value from previous work in function which calls
  911. * this function.
  912. * This seems bogus (why calling this function instead not
  913. * calling it and end caller function instead?).
  914. * Because on error in caller function we want to set some
  915. * default values in gpio desc and have a common error
  916. * debug message, which provides this function.
  917. * nodename: Name of node for which gpio gets requested
  918. * used for gpio label name.
  919. * args: pointer to output arguments structure
  920. * list_name: Name of GPIO list
  921. * used for gpio label name.
  922. * index: gpio index in gpio list
  923. * used for gpio label name.
  924. * desc: pointer to gpio descriptor, filled from this
  925. * function.
  926. * flags: gpio flags to use.
  927. * add_index: should index added to gpio label name
  928. * gpio_dev: pointer to gpio device from which the gpio
  929. * will be requested. If NULL try to get the
  930. * gpio device with uclass_get_device_by_ofnode()
  931. *
  932. * return: In error case this function sets default values in
  933. * gpio descriptor, also emmits a debug message.
  934. * On success it returns 0 else the error code from
  935. * function calls, or the error code passed through
  936. * ret to this function.
  937. *
  938. */
  939. static int gpio_request_tail(int ret, const char *nodename,
  940. struct ofnode_phandle_args *args,
  941. const char *list_name, int index,
  942. struct gpio_desc *desc, int flags,
  943. bool add_index, struct udevice *gpio_dev)
  944. {
  945. gpio_desc_init(desc, gpio_dev, 0);
  946. if (ret)
  947. goto err;
  948. if (!desc->dev) {
  949. ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
  950. &desc->dev);
  951. if (ret) {
  952. debug("%s: uclass_get_device_by_ofnode failed\n",
  953. __func__);
  954. goto err;
  955. }
  956. }
  957. ret = gpio_find_and_xlate(desc, args);
  958. if (ret) {
  959. debug("%s: gpio_find_and_xlate failed\n", __func__);
  960. goto err;
  961. }
  962. ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
  963. nodename, list_name, index);
  964. if (ret) {
  965. debug("%s: dm_gpio_requestf failed\n", __func__);
  966. goto err;
  967. }
  968. /* Keep any direction flags provided by the devicetree */
  969. ret = dm_gpio_set_dir_flags(desc,
  970. flags | (desc->flags & GPIOD_MASK_DIR));
  971. if (ret) {
  972. debug("%s: dm_gpio_set_dir failed\n", __func__);
  973. goto err;
  974. }
  975. return 0;
  976. err:
  977. debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
  978. __func__, nodename, list_name, index, ret);
  979. return ret;
  980. }
  981. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  982. static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
  983. int index, struct gpio_desc *desc,
  984. int flags, bool add_index)
  985. {
  986. struct ofnode_phandle_args args;
  987. int ret;
  988. ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
  989. index, &args);
  990. return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
  991. index, desc, flags, add_index, NULL);
  992. }
  993. int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
  994. struct gpio_desc *desc, int flags)
  995. {
  996. return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
  997. index > 0);
  998. }
  999. int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
  1000. struct gpio_desc *desc, int flags)
  1001. {
  1002. struct ofnode_phandle_args args;
  1003. ofnode node;
  1004. int ret;
  1005. ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
  1006. index, &args);
  1007. node = dev_ofnode(dev);
  1008. return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
  1009. index, desc, flags, index > 0, NULL);
  1010. }
  1011. int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
  1012. struct gpio_desc *desc, int max_count,
  1013. int flags)
  1014. {
  1015. int count;
  1016. int ret;
  1017. for (count = 0; count < max_count; count++) {
  1018. ret = _gpio_request_by_name_nodev(node, list_name, count,
  1019. &desc[count], flags, true);
  1020. if (ret == -ENOENT)
  1021. break;
  1022. else if (ret)
  1023. goto err;
  1024. }
  1025. /* We ran out of GPIOs in the list */
  1026. return count;
  1027. err:
  1028. gpio_free_list_nodev(desc, count - 1);
  1029. return ret;
  1030. }
  1031. int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
  1032. struct gpio_desc *desc, int max_count,
  1033. int flags)
  1034. {
  1035. /*
  1036. * This isn't ideal since we don't use dev->name in the debug()
  1037. * calls in gpio_request_by_name(), but we can do this until
  1038. * gpio_request_list_by_name_nodev() can be dropped.
  1039. */
  1040. return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
  1041. max_count, flags);
  1042. }
  1043. int gpio_get_list_count(struct udevice *dev, const char *list_name)
  1044. {
  1045. int ret;
  1046. ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
  1047. NULL);
  1048. if (ret) {
  1049. debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
  1050. __func__, dev->name, list_name, ret);
  1051. }
  1052. return ret;
  1053. }
  1054. #endif /* OF_PLATDATA */
  1055. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
  1056. {
  1057. /* For now, we don't do any checking of dev */
  1058. return _dm_gpio_free(desc->dev, desc->offset);
  1059. }
  1060. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
  1061. {
  1062. int i;
  1063. /* For now, we don't do any checking of dev */
  1064. for (i = 0; i < count; i++)
  1065. dm_gpio_free(dev, &desc[i]);
  1066. return 0;
  1067. }
  1068. int gpio_free_list_nodev(struct gpio_desc *desc, int count)
  1069. {
  1070. return gpio_free_list(NULL, desc, count);
  1071. }
  1072. /* We need to renumber the GPIOs when any driver is probed/removed */
  1073. static int gpio_renumber(struct udevice *removed_dev)
  1074. {
  1075. struct gpio_dev_priv *uc_priv;
  1076. struct udevice *dev;
  1077. struct uclass *uc;
  1078. unsigned base;
  1079. int ret;
  1080. ret = uclass_get(UCLASS_GPIO, &uc);
  1081. if (ret)
  1082. return ret;
  1083. /* Ensure that we have a base for each bank */
  1084. base = 0;
  1085. uclass_foreach_dev(dev, uc) {
  1086. if (device_active(dev) && dev != removed_dev) {
  1087. uc_priv = dev_get_uclass_priv(dev);
  1088. uc_priv->gpio_base = base;
  1089. base += uc_priv->gpio_count;
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. int gpio_get_number(const struct gpio_desc *desc)
  1095. {
  1096. struct udevice *dev = desc->dev;
  1097. struct gpio_dev_priv *uc_priv;
  1098. if (!dev)
  1099. return -1;
  1100. uc_priv = dev_get_uclass_priv(dev);
  1101. return uc_priv->gpio_base + desc->offset;
  1102. }
  1103. static int gpio_post_probe(struct udevice *dev)
  1104. {
  1105. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  1106. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  1107. if (!uc_priv->name)
  1108. return -ENOMEM;
  1109. return gpio_renumber(NULL);
  1110. }
  1111. static int gpio_pre_remove(struct udevice *dev)
  1112. {
  1113. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  1114. int i;
  1115. for (i = 0; i < uc_priv->gpio_count; i++) {
  1116. if (uc_priv->name[i])
  1117. free(uc_priv->name[i]);
  1118. }
  1119. free(uc_priv->name);
  1120. return gpio_renumber(dev);
  1121. }
  1122. int gpio_dev_request_index(struct udevice *dev, const char *nodename,
  1123. char *list_name, int index, int flags,
  1124. int dtflags, struct gpio_desc *desc)
  1125. {
  1126. struct ofnode_phandle_args args;
  1127. args.node = ofnode_null();
  1128. args.args_count = 2;
  1129. args.args[0] = index;
  1130. args.args[1] = dtflags;
  1131. return gpio_request_tail(0, nodename, &args, list_name, index, desc,
  1132. flags, 0, dev);
  1133. }
  1134. static void devm_gpiod_release(struct udevice *dev, void *res)
  1135. {
  1136. dm_gpio_free(dev, res);
  1137. }
  1138. static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
  1139. {
  1140. return res == data;
  1141. }
  1142. struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
  1143. unsigned int index, int flags)
  1144. {
  1145. int rc;
  1146. struct gpio_desc *desc;
  1147. char *propname;
  1148. static const char suffix[] = "-gpios";
  1149. propname = malloc(strlen(id) + sizeof(suffix));
  1150. if (!propname) {
  1151. rc = -ENOMEM;
  1152. goto end;
  1153. }
  1154. strcpy(propname, id);
  1155. strcat(propname, suffix);
  1156. desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
  1157. __GFP_ZERO);
  1158. if (unlikely(!desc)) {
  1159. rc = -ENOMEM;
  1160. goto end;
  1161. }
  1162. rc = gpio_request_by_name(dev, propname, index, desc, flags);
  1163. end:
  1164. if (propname)
  1165. free(propname);
  1166. if (rc)
  1167. return ERR_PTR(rc);
  1168. devres_add(dev, desc);
  1169. return desc;
  1170. }
  1171. struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
  1172. const char *id,
  1173. unsigned int index,
  1174. int flags)
  1175. {
  1176. struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
  1177. if (IS_ERR(desc))
  1178. return NULL;
  1179. return desc;
  1180. }
  1181. void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
  1182. {
  1183. int rc;
  1184. rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
  1185. WARN_ON(rc);
  1186. }
  1187. static int gpio_post_bind(struct udevice *dev)
  1188. {
  1189. struct udevice *child;
  1190. ofnode node;
  1191. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  1192. struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
  1193. static int reloc_done;
  1194. if (!reloc_done) {
  1195. if (ops->request)
  1196. ops->request += gd->reloc_off;
  1197. if (ops->rfree)
  1198. ops->rfree += gd->reloc_off;
  1199. if (ops->direction_input)
  1200. ops->direction_input += gd->reloc_off;
  1201. if (ops->direction_output)
  1202. ops->direction_output += gd->reloc_off;
  1203. if (ops->get_value)
  1204. ops->get_value += gd->reloc_off;
  1205. if (ops->set_value)
  1206. ops->set_value += gd->reloc_off;
  1207. if (ops->get_function)
  1208. ops->get_function += gd->reloc_off;
  1209. if (ops->xlate)
  1210. ops->xlate += gd->reloc_off;
  1211. if (ops->set_flags)
  1212. ops->set_flags += gd->reloc_off;
  1213. if (ops->get_flags)
  1214. ops->get_flags += gd->reloc_off;
  1215. reloc_done++;
  1216. }
  1217. #endif
  1218. if (IS_ENABLED(CONFIG_GPIO_HOG)) {
  1219. dev_for_each_subnode(node, dev) {
  1220. if (ofnode_read_bool(node, "gpio-hog")) {
  1221. const char *name = ofnode_get_name(node);
  1222. int ret;
  1223. ret = device_bind_driver_to_node(dev,
  1224. "gpio_hog",
  1225. name, node,
  1226. &child);
  1227. if (ret)
  1228. return ret;
  1229. }
  1230. }
  1231. }
  1232. return 0;
  1233. }
  1234. UCLASS_DRIVER(gpio) = {
  1235. .id = UCLASS_GPIO,
  1236. .name = "gpio",
  1237. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1238. .post_probe = gpio_post_probe,
  1239. .post_bind = gpio_post_bind,
  1240. .pre_remove = gpio_pre_remove,
  1241. .per_device_auto = sizeof(struct gpio_dev_priv),
  1242. };