sandbox.c 5.0 KB

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