gpio-uclass.c 30 KB

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