sandbox.c 6.0 KB

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