sandbox.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <fdtdec.h>
  8. #include <malloc.h>
  9. #include <asm/gpio.h>
  10. #include <dm/of.h>
  11. #include <dt-bindings/gpio/gpio.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. /* Flags for each GPIO */
  14. #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
  15. #define GPIOF_HIGH (1 << 1) /* Currently set high */
  16. #define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
  17. struct gpio_state {
  18. const char *label; /* label given by requester */
  19. u8 flags; /* flags (GPIOF_...) */
  20. };
  21. /* Access routines for GPIO state */
  22. static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
  23. {
  24. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  25. struct gpio_state *state = dev_get_priv(dev);
  26. if (offset >= uc_priv->gpio_count) {
  27. static u8 invalid_flags;
  28. printf("sandbox_gpio: error: invalid gpio %u\n", offset);
  29. return &invalid_flags;
  30. }
  31. return &state[offset].flags;
  32. }
  33. static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
  34. {
  35. return (*get_gpio_flags(dev, offset) & flag) != 0;
  36. }
  37. static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
  38. int value)
  39. {
  40. u8 *gpio = get_gpio_flags(dev, offset);
  41. if (value)
  42. *gpio |= flag;
  43. else
  44. *gpio &= ~flag;
  45. return 0;
  46. }
  47. /*
  48. * Back-channel sandbox-internal-only access to GPIO state
  49. */
  50. int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
  51. {
  52. if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
  53. debug("sandbox_gpio: get_value on output gpio %u\n", offset);
  54. return get_gpio_flag(dev, offset, GPIOF_HIGH);
  55. }
  56. int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  57. {
  58. return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
  59. }
  60. int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
  61. {
  62. return get_gpio_flag(dev, offset, GPIOF_ODR);
  63. }
  64. int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
  65. {
  66. return set_gpio_flag(dev, offset, GPIOF_ODR, value);
  67. }
  68. int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
  69. {
  70. return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
  71. }
  72. int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
  73. {
  74. return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
  75. }
  76. /*
  77. * These functions implement the public interface within U-Boot
  78. */
  79. /* set GPIO port 'offset' as an input */
  80. static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
  81. {
  82. debug("%s: offset:%u\n", __func__, offset);
  83. return sandbox_gpio_set_direction(dev, offset, 0);
  84. }
  85. /* set GPIO port 'offset' as an output, with polarity 'value' */
  86. static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
  87. int value)
  88. {
  89. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  90. return sandbox_gpio_set_direction(dev, offset, 1) |
  91. sandbox_gpio_set_value(dev, offset, value);
  92. }
  93. /* read GPIO IN value of port 'offset' */
  94. static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
  95. {
  96. debug("%s: offset:%u\n", __func__, offset);
  97. return sandbox_gpio_get_value(dev, offset);
  98. }
  99. /* write GPIO OUT value to port 'offset' */
  100. static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  101. {
  102. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  103. if (!sandbox_gpio_get_direction(dev, offset)) {
  104. printf("sandbox_gpio: error: set_value on input gpio %u\n",
  105. offset);
  106. return -1;
  107. }
  108. return sandbox_gpio_set_value(dev, offset, value);
  109. }
  110. /* read GPIO ODR value of port 'offset' */
  111. static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
  112. {
  113. debug("%s: offset:%u\n", __func__, offset);
  114. return sandbox_gpio_get_open_drain(dev, offset);
  115. }
  116. /* write GPIO ODR value to port 'offset' */
  117. static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
  118. {
  119. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  120. if (!sandbox_gpio_get_direction(dev, offset)) {
  121. printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
  122. offset);
  123. return -1;
  124. }
  125. return sandbox_gpio_set_open_drain(dev, offset, value);
  126. }
  127. static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
  128. {
  129. if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
  130. return GPIOF_OUTPUT;
  131. return GPIOF_INPUT;
  132. }
  133. static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
  134. struct ofnode_phandle_args *args)
  135. {
  136. desc->offset = args->args[0];
  137. if (args->args_count < 2)
  138. return 0;
  139. if (args->args[1] & GPIO_ACTIVE_LOW)
  140. desc->flags |= GPIOD_ACTIVE_LOW;
  141. if (args->args[1] & 2)
  142. desc->flags |= GPIOD_IS_IN;
  143. if (args->args[1] & 4)
  144. desc->flags |= GPIOD_IS_OUT;
  145. if (args->args[1] & 8)
  146. desc->flags |= GPIOD_IS_OUT_ACTIVE;
  147. return 0;
  148. }
  149. static const struct dm_gpio_ops gpio_sandbox_ops = {
  150. .direction_input = sb_gpio_direction_input,
  151. .direction_output = sb_gpio_direction_output,
  152. .get_value = sb_gpio_get_value,
  153. .set_value = sb_gpio_set_value,
  154. .get_open_drain = sb_gpio_get_open_drain,
  155. .set_open_drain = sb_gpio_set_open_drain,
  156. .get_function = sb_gpio_get_function,
  157. .xlate = sb_gpio_xlate,
  158. };
  159. static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
  160. {
  161. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  162. uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
  163. 0);
  164. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  165. return 0;
  166. }
  167. static int gpio_sandbox_probe(struct udevice *dev)
  168. {
  169. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  170. if (!dev_of_valid(dev))
  171. /* Tell the uclass how many GPIOs we have */
  172. uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
  173. dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
  174. return 0;
  175. }
  176. static int gpio_sandbox_remove(struct udevice *dev)
  177. {
  178. free(dev->priv);
  179. return 0;
  180. }
  181. static const struct udevice_id sandbox_gpio_ids[] = {
  182. { .compatible = "sandbox,gpio" },
  183. { }
  184. };
  185. U_BOOT_DRIVER(gpio_sandbox) = {
  186. .name = "gpio_sandbox",
  187. .id = UCLASS_GPIO,
  188. .of_match = sandbox_gpio_ids,
  189. .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
  190. .probe = gpio_sandbox_probe,
  191. .remove = gpio_sandbox_remove,
  192. .ops = &gpio_sandbox_ops,
  193. };