gpio-uclass.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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_plat(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_plat(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 = sizeof(struct gpio_hog_priv),
  294. .plat_auto = 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 = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
  943. NULL);
  944. if (ret) {
  945. debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
  946. __func__, dev->name, list_name, ret);
  947. }
  948. return ret;
  949. }
  950. int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
  951. {
  952. /* For now, we don't do any checking of dev */
  953. return _dm_gpio_free(desc->dev, desc->offset);
  954. }
  955. int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
  956. {
  957. int i;
  958. /* For now, we don't do any checking of dev */
  959. for (i = 0; i < count; i++)
  960. dm_gpio_free(dev, &desc[i]);
  961. return 0;
  962. }
  963. int gpio_free_list_nodev(struct gpio_desc *desc, int count)
  964. {
  965. return gpio_free_list(NULL, desc, count);
  966. }
  967. /* We need to renumber the GPIOs when any driver is probed/removed */
  968. static int gpio_renumber(struct udevice *removed_dev)
  969. {
  970. struct gpio_dev_priv *uc_priv;
  971. struct udevice *dev;
  972. struct uclass *uc;
  973. unsigned base;
  974. int ret;
  975. ret = uclass_get(UCLASS_GPIO, &uc);
  976. if (ret)
  977. return ret;
  978. /* Ensure that we have a base for each bank */
  979. base = 0;
  980. uclass_foreach_dev(dev, uc) {
  981. if (device_active(dev) && dev != removed_dev) {
  982. uc_priv = dev_get_uclass_priv(dev);
  983. uc_priv->gpio_base = base;
  984. base += uc_priv->gpio_count;
  985. }
  986. }
  987. return 0;
  988. }
  989. int gpio_get_number(const struct gpio_desc *desc)
  990. {
  991. struct udevice *dev = desc->dev;
  992. struct gpio_dev_priv *uc_priv;
  993. if (!dev)
  994. return -1;
  995. uc_priv = dev->uclass_priv;
  996. return uc_priv->gpio_base + desc->offset;
  997. }
  998. static int gpio_post_probe(struct udevice *dev)
  999. {
  1000. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  1001. uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
  1002. if (!uc_priv->name)
  1003. return -ENOMEM;
  1004. return gpio_renumber(NULL);
  1005. }
  1006. static int gpio_pre_remove(struct udevice *dev)
  1007. {
  1008. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  1009. int i;
  1010. for (i = 0; i < uc_priv->gpio_count; i++) {
  1011. if (uc_priv->name[i])
  1012. free(uc_priv->name[i]);
  1013. }
  1014. free(uc_priv->name);
  1015. return gpio_renumber(dev);
  1016. }
  1017. int gpio_dev_request_index(struct udevice *dev, const char *nodename,
  1018. char *list_name, int index, int flags,
  1019. int dtflags, struct gpio_desc *desc)
  1020. {
  1021. struct ofnode_phandle_args args;
  1022. args.node = ofnode_null();
  1023. args.args_count = 2;
  1024. args.args[0] = index;
  1025. args.args[1] = dtflags;
  1026. return gpio_request_tail(0, nodename, &args, list_name, index, desc,
  1027. flags, 0, dev);
  1028. }
  1029. static void devm_gpiod_release(struct udevice *dev, void *res)
  1030. {
  1031. dm_gpio_free(dev, res);
  1032. }
  1033. static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
  1034. {
  1035. return res == data;
  1036. }
  1037. struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
  1038. unsigned int index, int flags)
  1039. {
  1040. int rc;
  1041. struct gpio_desc *desc;
  1042. char *propname;
  1043. static const char suffix[] = "-gpios";
  1044. propname = malloc(strlen(id) + sizeof(suffix));
  1045. if (!propname) {
  1046. rc = -ENOMEM;
  1047. goto end;
  1048. }
  1049. strcpy(propname, id);
  1050. strcat(propname, suffix);
  1051. desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
  1052. __GFP_ZERO);
  1053. if (unlikely(!desc)) {
  1054. rc = -ENOMEM;
  1055. goto end;
  1056. }
  1057. rc = gpio_request_by_name(dev, propname, index, desc, flags);
  1058. end:
  1059. if (propname)
  1060. free(propname);
  1061. if (rc)
  1062. return ERR_PTR(rc);
  1063. devres_add(dev, desc);
  1064. return desc;
  1065. }
  1066. struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
  1067. const char *id,
  1068. unsigned int index,
  1069. int flags)
  1070. {
  1071. struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
  1072. if (IS_ERR(desc))
  1073. return NULL;
  1074. return desc;
  1075. }
  1076. void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
  1077. {
  1078. int rc;
  1079. rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
  1080. WARN_ON(rc);
  1081. }
  1082. static int gpio_post_bind(struct udevice *dev)
  1083. {
  1084. struct udevice *child;
  1085. ofnode node;
  1086. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  1087. struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
  1088. static int reloc_done;
  1089. if (!reloc_done) {
  1090. if (ops->request)
  1091. ops->request += gd->reloc_off;
  1092. if (ops->rfree)
  1093. ops->rfree += gd->reloc_off;
  1094. if (ops->direction_input)
  1095. ops->direction_input += gd->reloc_off;
  1096. if (ops->direction_output)
  1097. ops->direction_output += gd->reloc_off;
  1098. if (ops->get_value)
  1099. ops->get_value += gd->reloc_off;
  1100. if (ops->set_value)
  1101. ops->set_value += gd->reloc_off;
  1102. if (ops->get_function)
  1103. ops->get_function += gd->reloc_off;
  1104. if (ops->xlate)
  1105. ops->xlate += gd->reloc_off;
  1106. if (ops->set_dir_flags)
  1107. ops->set_dir_flags += gd->reloc_off;
  1108. if (ops->get_dir_flags)
  1109. ops->get_dir_flags += gd->reloc_off;
  1110. reloc_done++;
  1111. }
  1112. #endif
  1113. if (IS_ENABLED(CONFIG_GPIO_HOG)) {
  1114. dev_for_each_subnode(node, dev) {
  1115. if (ofnode_read_bool(node, "gpio-hog")) {
  1116. const char *name = ofnode_get_name(node);
  1117. int ret;
  1118. ret = device_bind_driver_to_node(dev,
  1119. "gpio_hog",
  1120. name, node,
  1121. &child);
  1122. if (ret)
  1123. return ret;
  1124. }
  1125. }
  1126. }
  1127. return 0;
  1128. }
  1129. UCLASS_DRIVER(gpio) = {
  1130. .id = UCLASS_GPIO,
  1131. .name = "gpio",
  1132. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1133. .post_probe = gpio_post_probe,
  1134. .post_bind = gpio_post_bind,
  1135. .pre_remove = gpio_pre_remove,
  1136. .per_device_auto = sizeof(struct gpio_dev_priv),
  1137. };