pinctrl-bcm283x.c 3.8 KB

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