imx_rgpio2p.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2016 Freescale Semiconductor, Inc.
  3. *
  4. * RGPIO2P driver for the Freescale i.MX7ULP.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <asm/gpio.h>
  13. #include <asm/io.h>
  14. #include <malloc.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. enum imx_rgpio2p_direction {
  17. IMX_RGPIO2P_DIRECTION_IN,
  18. IMX_RGPIO2P_DIRECTION_OUT,
  19. };
  20. #define GPIO_PER_BANK 32
  21. struct imx_rgpio2p_data {
  22. struct gpio_regs *regs;
  23. };
  24. struct imx_rgpio2p_plat {
  25. int bank_index;
  26. struct gpio_regs *regs;
  27. };
  28. static int imx_rgpio2p_is_output(struct gpio_regs *regs, int offset)
  29. {
  30. u32 val;
  31. val = readl(&regs->gpio_pddr);
  32. return val & (1 << offset) ? 1 : 0;
  33. }
  34. static void imx_rgpio2p_bank_direction(struct gpio_regs *regs, int offset,
  35. enum imx_rgpio2p_direction direction)
  36. {
  37. u32 l;
  38. l = readl(&regs->gpio_pddr);
  39. switch (direction) {
  40. case IMX_RGPIO2P_DIRECTION_OUT:
  41. l |= 1 << offset;
  42. break;
  43. case IMX_RGPIO2P_DIRECTION_IN:
  44. l &= ~(1 << offset);
  45. }
  46. writel(l, &regs->gpio_pddr);
  47. }
  48. static void imx_rgpio2p_bank_set_value(struct gpio_regs *regs, int offset,
  49. int value)
  50. {
  51. if (value)
  52. writel((1 << offset), &regs->gpio_psor);
  53. else
  54. writel((1 << offset), &regs->gpio_pcor);
  55. }
  56. static int imx_rgpio2p_bank_get_value(struct gpio_regs *regs, int offset)
  57. {
  58. return (readl(&regs->gpio_pdir) >> offset) & 0x01;
  59. }
  60. static int imx_rgpio2p_direction_input(struct udevice *dev, unsigned offset)
  61. {
  62. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  63. /* Configure GPIO direction as input. */
  64. imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_IN);
  65. return 0;
  66. }
  67. static int imx_rgpio2p_direction_output(struct udevice *dev, unsigned offset,
  68. int value)
  69. {
  70. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  71. /* Configure GPIO output value. */
  72. imx_rgpio2p_bank_set_value(bank->regs, offset, value);
  73. /* Configure GPIO direction as output. */
  74. imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_OUT);
  75. return 0;
  76. }
  77. static int imx_rgpio2p_get_value(struct udevice *dev, unsigned offset)
  78. {
  79. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  80. return imx_rgpio2p_bank_get_value(bank->regs, offset);
  81. }
  82. static int imx_rgpio2p_set_value(struct udevice *dev, unsigned offset,
  83. int value)
  84. {
  85. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  86. imx_rgpio2p_bank_set_value(bank->regs, offset, value);
  87. return 0;
  88. }
  89. static int imx_rgpio2p_get_function(struct udevice *dev, unsigned offset)
  90. {
  91. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  92. /* GPIOF_FUNC is not implemented yet */
  93. if (imx_rgpio2p_is_output(bank->regs, offset))
  94. return GPIOF_OUTPUT;
  95. else
  96. return GPIOF_INPUT;
  97. }
  98. static const struct dm_gpio_ops imx_rgpio2p_ops = {
  99. .direction_input = imx_rgpio2p_direction_input,
  100. .direction_output = imx_rgpio2p_direction_output,
  101. .get_value = imx_rgpio2p_get_value,
  102. .set_value = imx_rgpio2p_set_value,
  103. .get_function = imx_rgpio2p_get_function,
  104. };
  105. static int imx_rgpio2p_probe(struct udevice *dev)
  106. {
  107. struct imx_rgpio2p_data *bank = dev_get_priv(dev);
  108. struct imx_rgpio2p_plat *plat = dev_get_platdata(dev);
  109. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  110. int banknum;
  111. char name[18], *str;
  112. banknum = plat->bank_index;
  113. sprintf(name, "GPIO%d_", banknum + 1);
  114. str = strdup(name);
  115. if (!str)
  116. return -ENOMEM;
  117. uc_priv->bank_name = str;
  118. uc_priv->gpio_count = GPIO_PER_BANK;
  119. bank->regs = plat->regs;
  120. return 0;
  121. }
  122. static int imx_rgpio2p_bind(struct udevice *dev)
  123. {
  124. struct imx_rgpio2p_plat *plat = dev->platdata;
  125. fdt_addr_t addr;
  126. /*
  127. * If platdata already exsits, directly return.
  128. * Actually only when DT is not supported, platdata
  129. * is statically initialized in U_BOOT_DEVICES.Here
  130. * will return.
  131. */
  132. if (plat)
  133. return 0;
  134. addr = devfdt_get_addr_index(dev, 1);
  135. if (addr == FDT_ADDR_T_NONE)
  136. return -ENODEV;
  137. /*
  138. * TODO:
  139. * When every board is converted to driver model and DT is supported,
  140. * this can be done by auto-alloc feature, but not using calloc
  141. * to alloc memory for platdata.
  142. */
  143. plat = calloc(1, sizeof(*plat));
  144. if (!plat)
  145. return -ENOMEM;
  146. plat->regs = (struct gpio_regs *)addr;
  147. plat->bank_index = dev->req_seq;
  148. dev->platdata = plat;
  149. return 0;
  150. }
  151. static const struct udevice_id imx_rgpio2p_ids[] = {
  152. { .compatible = "fsl,imx7ulp-gpio" },
  153. { }
  154. };
  155. U_BOOT_DRIVER(imx_rgpio2p) = {
  156. .name = "imx_rgpio2p",
  157. .id = UCLASS_GPIO,
  158. .ops = &imx_rgpio2p_ops,
  159. .probe = imx_rgpio2p_probe,
  160. .priv_auto_alloc_size = sizeof(struct imx_rgpio2p_plat),
  161. .of_match = imx_rgpio2p_ids,
  162. .bind = imx_rgpio2p_bind,
  163. };
  164. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  165. static const struct imx_rgpio2p_plat imx_plat[] = {
  166. { 0, (struct gpio_regs *)RGPIO2P_GPIO1_BASE_ADDR },
  167. { 1, (struct gpio_regs *)RGPIO2P_GPIO2_BASE_ADDR },
  168. { 2, (struct gpio_regs *)RGPIO2P_GPIO3_BASE_ADDR },
  169. { 3, (struct gpio_regs *)RGPIO2P_GPIO4_BASE_ADDR },
  170. { 4, (struct gpio_regs *)RGPIO2P_GPIO5_BASE_ADDR },
  171. { 5, (struct gpio_regs *)RGPIO2P_GPIO6_BASE_ADDR },
  172. };
  173. U_BOOT_DEVICES(imx_rgpio2ps) = {
  174. { "imx_rgpio2p", &imx_plat[0] },
  175. { "imx_rgpio2p", &imx_plat[1] },
  176. { "imx_rgpio2p", &imx_plat[2] },
  177. { "imx_rgpio2p", &imx_plat[3] },
  178. { "imx_rgpio2p", &imx_plat[4] },
  179. { "imx_rgpio2p", &imx_plat[5] },
  180. };
  181. #endif