i2c-mxv7.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2012 Boundary Devices Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <asm/arch/clock.h>
  9. #include <asm/arch/imx-regs.h>
  10. #include <asm/errno.h>
  11. #include <asm/gpio.h>
  12. #include <asm/imx-common/mxc_i2c.h>
  13. #include <watchdog.h>
  14. static int force_idle_bus(void *priv)
  15. {
  16. int i;
  17. int sda, scl;
  18. ulong elapsed, start_time;
  19. struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
  20. int ret = 0;
  21. gpio_direction_input(p->sda.gp);
  22. gpio_direction_input(p->scl.gp);
  23. imx_iomux_v3_setup_pad(p->sda.gpio_mode);
  24. imx_iomux_v3_setup_pad(p->scl.gpio_mode);
  25. sda = gpio_get_value(p->sda.gp);
  26. scl = gpio_get_value(p->scl.gp);
  27. if ((sda & scl) == 1)
  28. goto exit; /* Bus is idle already */
  29. printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
  30. sda, scl, p->sda.gp, p->scl.gp);
  31. /* Send high and low on the SCL line */
  32. for (i = 0; i < 9; i++) {
  33. gpio_direction_output(p->scl.gp, 0);
  34. udelay(50);
  35. gpio_direction_input(p->scl.gp);
  36. udelay(50);
  37. }
  38. start_time = get_timer(0);
  39. for (;;) {
  40. sda = gpio_get_value(p->sda.gp);
  41. scl = gpio_get_value(p->scl.gp);
  42. if ((sda & scl) == 1)
  43. break;
  44. WATCHDOG_RESET();
  45. elapsed = get_timer(start_time);
  46. if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
  47. ret = -EBUSY;
  48. printf("%s: failed to clear bus, sda=%d scl=%d\n",
  49. __func__, sda, scl);
  50. break;
  51. }
  52. }
  53. exit:
  54. imx_iomux_v3_setup_pad(p->sda.i2c_mode);
  55. imx_iomux_v3_setup_pad(p->scl.i2c_mode);
  56. return ret;
  57. }
  58. static void * const i2c_bases[] = {
  59. (void *)I2C1_BASE_ADDR,
  60. (void *)I2C2_BASE_ADDR,
  61. #ifdef I2C3_BASE_ADDR
  62. (void *)I2C3_BASE_ADDR,
  63. #endif
  64. };
  65. /* i2c_index can be from 0 - 2 */
  66. int setup_i2c(unsigned i2c_index, int speed, int slave_addr,
  67. struct i2c_pads_info *p)
  68. {
  69. char name[9];
  70. int ret;
  71. if (i2c_index >= ARRAY_SIZE(i2c_bases))
  72. return -EINVAL;
  73. snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index);
  74. ret = gpio_request(p->sda.gp, name);
  75. if (ret)
  76. return ret;
  77. snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index);
  78. ret = gpio_request(p->scl.gp, name);
  79. if (ret)
  80. goto err_req;
  81. /* Enable i2c clock */
  82. ret = enable_i2c_clk(1, i2c_index);
  83. if (ret)
  84. goto err_clk;
  85. /* Make sure bus is idle */
  86. ret = force_idle_bus(p);
  87. if (ret)
  88. goto err_idle;
  89. bus_i2c_init(i2c_bases[i2c_index], speed, slave_addr,
  90. force_idle_bus, p);
  91. return 0;
  92. err_idle:
  93. err_clk:
  94. gpio_free(p->scl.gp);
  95. err_req:
  96. gpio_free(p->sda.gp);
  97. return ret;
  98. }