vybrid_gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2015
  3. * Bhuvanchandra DV, Toradex, Inc.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <asm/gpio.h>
  12. #include <asm/imx-common/iomux-v3.h>
  13. #include <asm/io.h>
  14. #include <malloc.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. struct vybrid_gpios {
  17. unsigned int chip;
  18. struct vybrid_gpio_regs *reg;
  19. };
  20. static int vybrid_gpio_direction_input(struct udevice *dev, unsigned gpio)
  21. {
  22. const struct vybrid_gpios *gpios = dev_get_priv(dev);
  23. gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
  24. imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_IN);
  25. return 0;
  26. }
  27. static int vybrid_gpio_direction_output(struct udevice *dev, unsigned gpio,
  28. int value)
  29. {
  30. const struct vybrid_gpios *gpios = dev_get_priv(dev);
  31. gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
  32. gpio_set_value(gpio, value);
  33. imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_OUT);
  34. return 0;
  35. }
  36. static int vybrid_gpio_get_value(struct udevice *dev, unsigned gpio)
  37. {
  38. const struct vybrid_gpios *gpios = dev_get_priv(dev);
  39. return ((readl(&gpios->reg->gpio_pdir) & (1 << gpio))) ? 1 : 0;
  40. }
  41. static int vybrid_gpio_set_value(struct udevice *dev, unsigned gpio,
  42. int value)
  43. {
  44. const struct vybrid_gpios *gpios = dev_get_priv(dev);
  45. if (value)
  46. writel((1 << gpio), &gpios->reg->gpio_psor);
  47. else
  48. writel((1 << gpio), &gpios->reg->gpio_pcor);
  49. return 0;
  50. }
  51. static int vybrid_gpio_get_function(struct udevice *dev, unsigned gpio)
  52. {
  53. const struct vybrid_gpios *gpios = dev_get_priv(dev);
  54. u32 g_state = 0;
  55. gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
  56. imx_iomux_gpio_get_function(gpio, &g_state);
  57. if (((g_state & (0x07 << PAD_MUX_MODE_SHIFT)) >> PAD_MUX_MODE_SHIFT) > 0)
  58. return GPIOF_FUNC;
  59. if (g_state & PAD_CTL_OBE_ENABLE)
  60. return GPIOF_OUTPUT;
  61. if (g_state & PAD_CTL_IBE_ENABLE)
  62. return GPIOF_INPUT;
  63. if (!(g_state & PAD_CTL_OBE_IBE_ENABLE))
  64. return GPIOF_UNUSED;
  65. return GPIOF_UNKNOWN;
  66. }
  67. static const struct dm_gpio_ops gpio_vybrid_ops = {
  68. .direction_input = vybrid_gpio_direction_input,
  69. .direction_output = vybrid_gpio_direction_output,
  70. .get_value = vybrid_gpio_get_value,
  71. .set_value = vybrid_gpio_set_value,
  72. .get_function = vybrid_gpio_get_function,
  73. };
  74. static int vybrid_gpio_probe(struct udevice *dev)
  75. {
  76. struct vybrid_gpios *gpios = dev_get_priv(dev);
  77. struct vybrid_gpio_platdata *plat = dev_get_platdata(dev);
  78. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  79. uc_priv->bank_name = plat->port_name;
  80. uc_priv->gpio_count = VYBRID_GPIO_COUNT;
  81. gpios->reg = (struct vybrid_gpio_regs *)plat->base;
  82. gpios->chip = plat->chip;
  83. return 0;
  84. }
  85. static int vybrid_gpio_bind(struct udevice *dev)
  86. {
  87. struct vybrid_gpio_platdata *plat = dev->platdata;
  88. fdt_addr_t base_addr;
  89. if (plat)
  90. return 0;
  91. base_addr = dev_get_addr(dev);
  92. if (base_addr == FDT_ADDR_T_NONE)
  93. return -ENODEV;
  94. /*
  95. * TODO:
  96. * When every board is converted to driver model and DT is
  97. * supported, this can be done by auto-alloc feature, but
  98. * not using calloc to alloc memory for platdata.
  99. */
  100. plat = calloc(1, sizeof(*plat));
  101. if (!plat)
  102. return -ENOMEM;
  103. plat->base = base_addr;
  104. plat->chip = dev->req_seq;
  105. plat->port_name = fdt_get_name(gd->fdt_blob, dev->of_offset, NULL);
  106. dev->platdata = plat;
  107. return 0;
  108. }
  109. static const struct udevice_id vybrid_gpio_ids[] = {
  110. { .compatible = "fsl,vf610-gpio" },
  111. { }
  112. };
  113. U_BOOT_DRIVER(gpio_vybrid) = {
  114. .name = "gpio_vybrid",
  115. .id = UCLASS_GPIO,
  116. .ops = &gpio_vybrid_ops,
  117. .probe = vybrid_gpio_probe,
  118. .priv_auto_alloc_size = sizeof(struct vybrid_gpios),
  119. .of_match = vybrid_gpio_ids,
  120. .bind = vybrid_gpio_bind,
  121. };