i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "omap-pm.h"
  25. #include "prm.h"
  26. #include "common.h"
  27. #include "mux.h"
  28. #include "i2c.h"
  29. /* In register I2C_CON, Bit 15 is the I2C enable bit */
  30. #define I2C_EN BIT(15)
  31. #define OMAP2_I2C_CON_OFFSET 0x24
  32. #define OMAP4_I2C_CON_OFFSET 0xA4
  33. #define MAX_OMAP_I2C_HWMOD_NAME_LEN 16
  34. static void __init omap2_i2c_mux_pins(int bus_id)
  35. {
  36. char mux_name[sizeof("i2c2_scl.i2c2_scl")];
  37. /* First I2C bus is not muxable */
  38. if (bus_id == 1)
  39. return;
  40. sprintf(mux_name, "i2c%i_scl.i2c%i_scl", bus_id, bus_id);
  41. omap_mux_init_signal(mux_name, OMAP_PIN_INPUT);
  42. sprintf(mux_name, "i2c%i_sda.i2c%i_sda", bus_id, bus_id);
  43. omap_mux_init_signal(mux_name, OMAP_PIN_INPUT);
  44. }
  45. /**
  46. * omap_i2c_reset - reset the omap i2c module.
  47. * @oh: struct omap_hwmod *
  48. *
  49. * The i2c moudle in omap2, omap3 had a special sequence to reset. The
  50. * sequence is:
  51. * - Disable the I2C.
  52. * - Write to SOFTRESET bit.
  53. * - Enable the I2C.
  54. * - Poll on the RESETDONE bit.
  55. * The sequence is implemented in below function. This is called for 2420,
  56. * 2430 and omap3.
  57. */
  58. int omap_i2c_reset(struct omap_hwmod *oh)
  59. {
  60. u32 v;
  61. u16 i2c_con;
  62. int c = 0;
  63. if (oh->class->rev == OMAP_I2C_IP_VERSION_2) {
  64. i2c_con = OMAP4_I2C_CON_OFFSET;
  65. } else if (oh->class->rev == OMAP_I2C_IP_VERSION_1) {
  66. i2c_con = OMAP2_I2C_CON_OFFSET;
  67. } else {
  68. WARN(1, "Cannot reset I2C block %s: unsupported revision\n",
  69. oh->name);
  70. return -EINVAL;
  71. }
  72. /* Disable I2C */
  73. v = omap_hwmod_read(oh, i2c_con);
  74. v &= ~I2C_EN;
  75. omap_hwmod_write(v, oh, i2c_con);
  76. /* Write to the SOFTRESET bit */
  77. omap_hwmod_softreset(oh);
  78. /* Enable I2C */
  79. v = omap_hwmod_read(oh, i2c_con);
  80. v |= I2C_EN;
  81. omap_hwmod_write(v, oh, i2c_con);
  82. /* Poll on RESETDONE bit */
  83. omap_test_timeout((omap_hwmod_read(oh,
  84. oh->class->sysc->syss_offs)
  85. & SYSS_RESETDONE_MASK),
  86. MAX_MODULE_SOFTRESET_WAIT, c);
  87. if (c == MAX_MODULE_SOFTRESET_WAIT)
  88. pr_warn("%s: %s: softreset failed (waited %d usec)\n",
  89. __func__, oh->name, MAX_MODULE_SOFTRESET_WAIT);
  90. else
  91. pr_debug("%s: %s: softreset in %d usec\n", __func__,
  92. oh->name, c);
  93. return 0;
  94. }
  95. static int __init omap_i2c_nr_ports(void)
  96. {
  97. int ports = 0;
  98. if (cpu_is_omap24xx())
  99. ports = 2;
  100. else if (cpu_is_omap34xx())
  101. ports = 3;
  102. else if (cpu_is_omap44xx())
  103. ports = 4;
  104. return ports;
  105. }
  106. /*
  107. * XXX This function is a temporary compatibility wrapper - only
  108. * needed until the I2C driver can be converted to call
  109. * omap_pm_set_max_dev_wakeup_lat() and handle a return code.
  110. */
  111. static void omap_pm_set_max_mpu_wakeup_lat_compat(struct device *dev, long t)
  112. {
  113. omap_pm_set_max_mpu_wakeup_lat(dev, t);
  114. }
  115. static const char name[] = "omap_i2c";
  116. int __init omap_i2c_add_bus(struct omap_i2c_bus_platform_data *i2c_pdata,
  117. int bus_id)
  118. {
  119. int l;
  120. struct omap_hwmod *oh;
  121. struct platform_device *pdev;
  122. char oh_name[MAX_OMAP_I2C_HWMOD_NAME_LEN];
  123. struct omap_i2c_bus_platform_data *pdata;
  124. struct omap_i2c_dev_attr *dev_attr;
  125. if (bus_id > omap_i2c_nr_ports())
  126. return -EINVAL;
  127. omap2_i2c_mux_pins(bus_id);
  128. l = snprintf(oh_name, MAX_OMAP_I2C_HWMOD_NAME_LEN, "i2c%d", bus_id);
  129. WARN(l >= MAX_OMAP_I2C_HWMOD_NAME_LEN,
  130. "String buffer overflow in I2C%d device setup\n", bus_id);
  131. oh = omap_hwmod_lookup(oh_name);
  132. if (!oh) {
  133. pr_err("Could not look up %s\n", oh_name);
  134. return -EEXIST;
  135. }
  136. pdata = i2c_pdata;
  137. /*
  138. * pass the hwmod class's CPU-specific knowledge of I2C IP revision in
  139. * use, and functionality implementation flags, up to the OMAP I2C
  140. * driver via platform data
  141. */
  142. pdata->rev = oh->class->rev;
  143. dev_attr = (struct omap_i2c_dev_attr *)oh->dev_attr;
  144. pdata->flags = dev_attr->flags;
  145. /*
  146. * When waiting for completion of a i2c transfer, we need to
  147. * set a wake up latency constraint for the MPU. This is to
  148. * ensure quick enough wakeup from idle, when transfer
  149. * completes.
  150. * Only omap3 has support for constraints
  151. */
  152. if (cpu_is_omap34xx())
  153. pdata->set_mpu_wkup_lat = omap_pm_set_max_mpu_wakeup_lat_compat;
  154. pdev = omap_device_build(name, bus_id, oh, pdata,
  155. sizeof(struct omap_i2c_bus_platform_data));
  156. WARN(IS_ERR(pdev), "Could not build omap_device for %s\n", name);
  157. return PTR_ERR_OR_ZERO(pdev);
  158. }
  159. static int __init omap_i2c_cmdline(void)
  160. {
  161. return omap_register_i2c_bus_cmdline();
  162. }
  163. omap_subsys_initcall(omap_i2c_cmdline);