i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Helper module for board specific I2C bus registration
  3. *
  4. * Copyright (C) 2009 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include "soc.h"
  22. #include "omap_hwmod.h"
  23. #include "omap_device.h"
  24. #include "prm.h"
  25. #include "common.h"
  26. #include "mux.h"
  27. #include "i2c.h"
  28. /* In register I2C_CON, Bit 15 is the I2C enable bit */
  29. #define I2C_EN BIT(15)
  30. #define OMAP2_I2C_CON_OFFSET 0x24
  31. #define OMAP4_I2C_CON_OFFSET 0xA4
  32. #define MAX_OMAP_I2C_HWMOD_NAME_LEN 16
  33. static void __init omap2_i2c_mux_pins(int bus_id)
  34. {
  35. char mux_name[sizeof("i2c2_scl.i2c2_scl")];
  36. /* First I2C bus is not muxable */
  37. if (bus_id == 1)
  38. return;
  39. sprintf(mux_name, "i2c%i_scl.i2c%i_scl", bus_id, bus_id);
  40. omap_mux_init_signal(mux_name, OMAP_PIN_INPUT);
  41. sprintf(mux_name, "i2c%i_sda.i2c%i_sda", bus_id, bus_id);
  42. omap_mux_init_signal(mux_name, OMAP_PIN_INPUT);
  43. }
  44. /**
  45. * omap_i2c_reset - reset the omap i2c module.
  46. * @oh: struct omap_hwmod *
  47. *
  48. * The i2c moudle in omap2, omap3 had a special sequence to reset. The
  49. * sequence is:
  50. * - Disable the I2C.
  51. * - Write to SOFTRESET bit.
  52. * - Enable the I2C.
  53. * - Poll on the RESETDONE bit.
  54. * The sequence is implemented in below function. This is called for 2420,
  55. * 2430 and omap3.
  56. */
  57. int omap_i2c_reset(struct omap_hwmod *oh)
  58. {
  59. u32 v;
  60. u16 i2c_con;
  61. int c = 0;
  62. if (oh->class->rev == OMAP_I2C_IP_VERSION_2) {
  63. i2c_con = OMAP4_I2C_CON_OFFSET;
  64. } else if (oh->class->rev == OMAP_I2C_IP_VERSION_1) {
  65. i2c_con = OMAP2_I2C_CON_OFFSET;
  66. } else {
  67. WARN(1, "Cannot reset I2C block %s: unsupported revision\n",
  68. oh->name);
  69. return -EINVAL;
  70. }
  71. /* Disable I2C */
  72. v = omap_hwmod_read(oh, i2c_con);
  73. v &= ~I2C_EN;
  74. omap_hwmod_write(v, oh, i2c_con);
  75. /* Write to the SOFTRESET bit */
  76. omap_hwmod_softreset(oh);
  77. /* Enable I2C */
  78. v = omap_hwmod_read(oh, i2c_con);
  79. v |= I2C_EN;
  80. omap_hwmod_write(v, oh, i2c_con);
  81. /* Poll on RESETDONE bit */
  82. omap_test_timeout((omap_hwmod_read(oh,
  83. oh->class->sysc->syss_offs)
  84. & SYSS_RESETDONE_MASK),
  85. MAX_MODULE_SOFTRESET_WAIT, c);
  86. if (c == MAX_MODULE_SOFTRESET_WAIT)
  87. pr_warning("%s: %s: softreset failed (waited %d usec)\n",
  88. __func__, oh->name, MAX_MODULE_SOFTRESET_WAIT);
  89. else
  90. pr_debug("%s: %s: softreset in %d usec\n", __func__,
  91. oh->name, c);
  92. return 0;
  93. }
  94. static int __init omap_i2c_nr_ports(void)
  95. {
  96. int ports = 0;
  97. if (cpu_is_omap24xx())
  98. ports = 2;
  99. else if (cpu_is_omap34xx())
  100. ports = 3;
  101. else if (cpu_is_omap44xx())
  102. ports = 4;
  103. return ports;
  104. }
  105. static const char name[] = "omap_i2c";
  106. int __init omap_i2c_add_bus(struct omap_i2c_bus_platform_data *i2c_pdata,
  107. int bus_id)
  108. {
  109. int l;
  110. struct omap_hwmod *oh;
  111. struct platform_device *pdev;
  112. char oh_name[MAX_OMAP_I2C_HWMOD_NAME_LEN];
  113. struct omap_i2c_bus_platform_data *pdata;
  114. struct omap_i2c_dev_attr *dev_attr;
  115. if (bus_id > omap_i2c_nr_ports())
  116. return -EINVAL;
  117. omap2_i2c_mux_pins(bus_id);
  118. l = snprintf(oh_name, MAX_OMAP_I2C_HWMOD_NAME_LEN, "i2c%d", bus_id);
  119. WARN(l >= MAX_OMAP_I2C_HWMOD_NAME_LEN,
  120. "String buffer overflow in I2C%d device setup\n", bus_id);
  121. oh = omap_hwmod_lookup(oh_name);
  122. if (!oh) {
  123. pr_err("Could not look up %s\n", oh_name);
  124. return -EEXIST;
  125. }
  126. pdata = i2c_pdata;
  127. /*
  128. * pass the hwmod class's CPU-specific knowledge of I2C IP revision in
  129. * use, and functionality implementation flags, up to the OMAP I2C
  130. * driver via platform data
  131. */
  132. pdata->rev = oh->class->rev;
  133. dev_attr = (struct omap_i2c_dev_attr *)oh->dev_attr;
  134. pdata->flags = dev_attr->flags;
  135. pdev = omap_device_build(name, bus_id, oh, pdata,
  136. sizeof(struct omap_i2c_bus_platform_data),
  137. NULL, 0, 0);
  138. WARN(IS_ERR(pdev), "Could not build omap_device for %s\n", name);
  139. return PTR_RET(pdev);
  140. }