vybrid_gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015
  4. * Bhuvanchandra DV, Toradex, Inc.
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <asm/global_data.h>
  11. #include <asm/gpio.h>
  12. #include <asm/mach-imx/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_plat *plat = dev_get_plat(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_odata_to_plat(struct udevice *dev)
  86. {
  87. struct vybrid_gpio_plat *plat = dev_get_plat(dev);
  88. fdt_addr_t base_addr;
  89. base_addr = dev_read_addr(dev);
  90. if (base_addr == FDT_ADDR_T_NONE)
  91. return -EINVAL;
  92. plat->base = base_addr;
  93. plat->chip = dev_seq(dev);
  94. plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
  95. return 0;
  96. }
  97. static const struct udevice_id vybrid_gpio_ids[] = {
  98. { .compatible = "fsl,vf610-gpio" },
  99. { }
  100. };
  101. U_BOOT_DRIVER(gpio_vybrid) = {
  102. .name = "gpio_vybrid",
  103. .id = UCLASS_GPIO,
  104. .ops = &gpio_vybrid_ops,
  105. .of_match = vybrid_gpio_ids,
  106. .of_to_plat = vybrid_gpio_odata_to_plat,
  107. .probe = vybrid_gpio_probe,
  108. .priv_auto = sizeof(struct vybrid_gpios),
  109. .plat_auto = sizeof(struct vybrid_gpio_plat),
  110. };