sandbox.c 4.5 KB

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