pinctrl-bcm283x.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Alexander Graf <agraf@suse.de>
  4. *
  5. * Based on drivers/pinctrl/mvebu/pinctrl-mvebu.c and
  6. * drivers/gpio/bcm2835_gpio.c
  7. *
  8. * This driver gets instantiated by the GPIO driver, because both devices
  9. * share the same device node.
  10. * https://spdx.org/licenses
  11. */
  12. #include <common.h>
  13. #include <config.h>
  14. #include <errno.h>
  15. #include <dm.h>
  16. #include <log.h>
  17. #include <dm/pinctrl.h>
  18. #include <dm/root.h>
  19. #include <dm/device-internal.h>
  20. #include <dm/lists.h>
  21. #include <asm/system.h>
  22. #include <asm/io.h>
  23. #include <asm/gpio.h>
  24. struct bcm283x_pinctrl_priv {
  25. u32 *base_reg;
  26. };
  27. #define MAX_PINS_PER_BANK 16
  28. static void bcm2835_gpio_set_func_id(struct udevice *dev, unsigned int gpio,
  29. int func)
  30. {
  31. struct bcm283x_pinctrl_priv *priv = dev_get_priv(dev);
  32. int reg_offset;
  33. int field_offset;
  34. reg_offset = BCM2835_GPIO_FSEL_BANK(gpio);
  35. field_offset = BCM2835_GPIO_FSEL_SHIFT(gpio);
  36. clrsetbits_le32(&priv->base_reg[reg_offset],
  37. BCM2835_GPIO_FSEL_MASK << field_offset,
  38. (func & BCM2835_GPIO_FSEL_MASK) << field_offset);
  39. }
  40. static int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned int gpio)
  41. {
  42. struct bcm283x_pinctrl_priv *priv = dev_get_priv(dev);
  43. u32 val;
  44. val = readl(&priv->base_reg[BCM2835_GPIO_FSEL_BANK(gpio)]);
  45. return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
  46. }
  47. /*
  48. * bcm283x_pinctrl_set_state: configure pin functions.
  49. * @dev: the pinctrl device to be configured.
  50. * @config: the state to be configured.
  51. * @return: 0 in success
  52. */
  53. int bcm283x_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  54. {
  55. u32 pin_arr[MAX_PINS_PER_BANK];
  56. u32 function;
  57. int i, len, pin_count = 0;
  58. if (!dev_read_prop(config, "brcm,pins", &len) || !len ||
  59. len & 0x3 || dev_read_u32_array(config, "brcm,pins", pin_arr,
  60. len / sizeof(u32))) {
  61. debug("Failed reading pins array for pinconfig %s (%d)\n",
  62. config->name, len);
  63. return -EINVAL;
  64. }
  65. pin_count = len / sizeof(u32);
  66. function = dev_read_u32_default(config, "brcm,function", -1);
  67. if (function < 0) {
  68. debug("Failed reading function for pinconfig %s (%d)\n",
  69. config->name, function);
  70. return -EINVAL;
  71. }
  72. for (i = 0; i < pin_count; i++)
  73. bcm2835_gpio_set_func_id(dev, pin_arr[i], function);
  74. return 0;
  75. }
  76. static int bcm283x_pinctrl_get_gpio_mux(struct udevice *dev, int banknum,
  77. int index)
  78. {
  79. if (banknum != 0)
  80. return -EINVAL;
  81. return bcm2835_gpio_get_func_id(dev, index);
  82. }
  83. static const struct udevice_id bcm2835_pinctrl_id[] = {
  84. {.compatible = "brcm,bcm2835-gpio"},
  85. {.compatible = "brcm,bcm2711-gpio"},
  86. {}
  87. };
  88. int bcm283x_pinctl_ofdata_to_platdata(struct udevice *dev)
  89. {
  90. struct bcm283x_pinctrl_priv *priv;
  91. priv = dev_get_priv(dev);
  92. priv->base_reg = dev_read_addr_ptr(dev);
  93. if (!priv->base_reg) {
  94. debug("%s: Failed to get base address\n", __func__);
  95. return -EINVAL;
  96. }
  97. return 0;
  98. }
  99. int bcm283x_pinctl_probe(struct udevice *dev)
  100. {
  101. int ret;
  102. struct udevice *pdev;
  103. /* Create GPIO device as well */
  104. ret = device_bind(dev, lists_driver_lookup_name("gpio_bcm2835"),
  105. "gpio_bcm2835", NULL, dev_of_offset(dev), &pdev);
  106. if (ret) {
  107. /*
  108. * While we really want the pinctrl driver to work to make
  109. * devices go where they should go, the GPIO controller is
  110. * not quite as crucial as it's only rarely used, so don't
  111. * fail here.
  112. */
  113. printf("Failed to bind GPIO driver\n");
  114. }
  115. return 0;
  116. }
  117. static struct pinctrl_ops bcm283x_pinctrl_ops = {
  118. .set_state = bcm283x_pinctrl_set_state,
  119. .get_gpio_mux = bcm283x_pinctrl_get_gpio_mux,
  120. };
  121. U_BOOT_DRIVER(pinctrl_bcm283x) = {
  122. .name = "bcm283x_pinctrl",
  123. .id = UCLASS_PINCTRL,
  124. .of_match = of_match_ptr(bcm2835_pinctrl_id),
  125. .ofdata_to_platdata = bcm283x_pinctl_ofdata_to_platdata,
  126. .priv_auto_alloc_size = sizeof(struct bcm283x_pinctrl_priv),
  127. .ops = &bcm283x_pinctrl_ops,
  128. .probe = bcm283x_pinctl_probe,
  129. #if CONFIG_IS_ENABLED(OF_BOARD)
  130. .flags = DM_FLAG_PRE_RELOC,
  131. #endif
  132. };