pinctrl-apple.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/device-internal.h>
  8. #include <dm/pinctrl.h>
  9. #include <dt-bindings/pinctrl/apple.h>
  10. #include <asm/io.h>
  11. #include <asm-generic/gpio.h>
  12. #include <linux/bitfield.h>
  13. struct apple_pinctrl_priv {
  14. void *base;
  15. int pin_count;
  16. };
  17. #define REG_GPIO(x) (4 * (x))
  18. #define REG_GPIO_DATA BIT(0)
  19. #define REG_GPIO_MODE GENMASK(3, 1)
  20. #define REG_GPIO_OUT 1
  21. #define REG_GPIO_PERIPH GENMASK(6, 5)
  22. #define REG_GPIO_INPUT_ENABLE BIT(9)
  23. static void apple_pinctrl_config_pin(struct apple_pinctrl_priv *priv,
  24. unsigned pin, u32 clr, u32 set)
  25. {
  26. unsigned reg = REG_GPIO(pin);
  27. u32 old, new;
  28. old = readl(priv->base + REG_GPIO(pin));
  29. new = (old & ~clr) | set;
  30. writel(new, priv->base + reg);
  31. }
  32. static int apple_gpio_get_value(struct udevice *dev, unsigned offset)
  33. {
  34. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  35. return !!(readl(priv->base + REG_GPIO(offset)) & REG_GPIO_DATA);
  36. }
  37. static int apple_gpio_set_value(struct udevice *dev, unsigned offset,
  38. int value)
  39. {
  40. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  41. apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA,
  42. value ? REG_GPIO_DATA : 0);
  43. return 0;
  44. }
  45. static int apple_gpio_get_direction(struct udevice *dev, unsigned offset)
  46. {
  47. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  48. u32 reg = readl(priv->base + REG_GPIO(offset));
  49. if (FIELD_GET(REG_GPIO_MODE, reg) == REG_GPIO_OUT)
  50. return GPIOF_OUTPUT;
  51. else
  52. return GPIOF_INPUT;
  53. }
  54. static int apple_gpio_direction_input(struct udevice *dev, unsigned offset)
  55. {
  56. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  57. apple_pinctrl_config_pin(priv, offset,
  58. REG_GPIO_PERIPH | REG_GPIO_MODE,
  59. REG_GPIO_INPUT_ENABLE);
  60. return 0;
  61. }
  62. static int apple_gpio_direction_output(struct udevice *dev, unsigned offset,
  63. int value)
  64. {
  65. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  66. u32 set = (value ? REG_GPIO_DATA : 0);
  67. apple_pinctrl_config_pin(priv, offset, REG_GPIO_DATA |
  68. REG_GPIO_PERIPH | REG_GPIO_MODE,
  69. set | FIELD_PREP(REG_GPIO_MODE, REG_GPIO_OUT));
  70. return 0;
  71. }
  72. static int apple_gpio_probe(struct udevice *dev)
  73. {
  74. struct apple_pinctrl_priv *priv = dev_get_priv(dev->parent);
  75. struct gpio_dev_priv *uc_priv;
  76. uc_priv = dev_get_uclass_priv(dev);
  77. uc_priv->bank_name = "gpio";
  78. uc_priv->gpio_count = priv->pin_count;
  79. return 0;
  80. }
  81. static struct dm_gpio_ops apple_gpio_ops = {
  82. .get_value = apple_gpio_get_value,
  83. .set_value = apple_gpio_set_value,
  84. .get_function = apple_gpio_get_direction,
  85. .direction_input = apple_gpio_direction_input,
  86. .direction_output = apple_gpio_direction_output,
  87. };
  88. static struct driver apple_gpio_driver = {
  89. .name = "apple_gpio",
  90. .id = UCLASS_GPIO,
  91. .probe = apple_gpio_probe,
  92. .ops = &apple_gpio_ops,
  93. };
  94. static int apple_pinctrl_get_pins_count(struct udevice *dev)
  95. {
  96. struct apple_pinctrl_priv *priv = dev_get_priv(dev);
  97. return priv->pin_count;
  98. }
  99. static const char *apple_pinctrl_get_pin_name(struct udevice *dev,
  100. unsigned selector)
  101. {
  102. static char pin_name[PINNAME_SIZE];
  103. snprintf(pin_name, PINNAME_SIZE, "pin%d", selector);
  104. return pin_name;
  105. }
  106. static int apple_pinctrl_get_pin_muxing(struct udevice *dev, unsigned selector,
  107. char *buf, int size)
  108. {
  109. struct apple_pinctrl_priv *priv = dev_get_priv(dev);
  110. if (readl(priv->base + REG_GPIO(selector)) & REG_GPIO_PERIPH)
  111. strncpy(buf, "periph", size);
  112. else
  113. strncpy(buf, "gpio", size);
  114. return 0;
  115. }
  116. static int apple_pinctrl_pinmux_set(struct udevice *dev, unsigned pin_selector,
  117. unsigned func_selector)
  118. {
  119. struct apple_pinctrl_priv *priv = dev_get_priv(dev);
  120. apple_pinctrl_config_pin(priv, pin_selector,
  121. REG_GPIO_DATA | REG_GPIO_MODE,
  122. FIELD_PREP(REG_GPIO_PERIPH, func_selector) |
  123. REG_GPIO_INPUT_ENABLE);
  124. return 0;
  125. }
  126. static int apple_pinctrl_pinmux_property_set(struct udevice *dev,
  127. u32 pinmux_group)
  128. {
  129. unsigned pin_selector = APPLE_PIN(pinmux_group);
  130. unsigned func_selector = APPLE_FUNC(pinmux_group);
  131. int ret;
  132. ret = apple_pinctrl_pinmux_set(dev, pin_selector, func_selector);
  133. return ret ? ret : pin_selector;
  134. }
  135. static int apple_pinctrl_probe(struct udevice *dev)
  136. {
  137. struct apple_pinctrl_priv *priv = dev_get_priv(dev);
  138. struct ofnode_phandle_args args;
  139. struct udevice *child;
  140. priv->base = dev_read_addr_ptr(dev);
  141. if (!priv->base)
  142. return -EINVAL;
  143. if (!dev_read_phandle_with_args(dev, "gpio-ranges",
  144. NULL, 3, 0, &args))
  145. priv->pin_count = args.args[2];
  146. device_bind(dev, &apple_gpio_driver, "apple_gpio", NULL,
  147. dev_ofnode(dev), &child);
  148. return 0;
  149. }
  150. static struct pinctrl_ops apple_pinctrl_ops = {
  151. .set_state = pinctrl_generic_set_state,
  152. .get_pins_count = apple_pinctrl_get_pins_count,
  153. .get_pin_name = apple_pinctrl_get_pin_name,
  154. .pinmux_set = apple_pinctrl_pinmux_set,
  155. .pinmux_property_set = apple_pinctrl_pinmux_property_set,
  156. .get_pin_muxing = apple_pinctrl_get_pin_muxing,
  157. };
  158. static const struct udevice_id apple_pinctrl_ids[] = {
  159. { .compatible = "apple,pinctrl" },
  160. { /* sentinel */ }
  161. };
  162. U_BOOT_DRIVER(pinctrl_apple) = {
  163. .name = "apple_pinctrl",
  164. .id = UCLASS_PINCTRL,
  165. .of_match = apple_pinctrl_ids,
  166. .priv_auto = sizeof(struct apple_pinctrl_priv),
  167. .ops = &apple_pinctrl_ops,
  168. .probe = apple_pinctrl_probe,
  169. };