i2c.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * linux/arch/arm/plat-omap/i2c.c
  3. *
  4. * Helper module for board specific I2C bus registration
  5. *
  6. * Copyright (C) 2007 Nokia Corporation.
  7. *
  8. * Contact: Jarkko Nikula <jhnikula@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-omap.h>
  29. #include <mach/irqs.h>
  30. #include <plat/mux.h>
  31. #include <plat/i2c.h>
  32. #include <plat/omap-pm.h>
  33. #define OMAP_I2C_SIZE 0x3f
  34. #define OMAP1_I2C_BASE 0xfffb3800
  35. #define OMAP2_I2C_BASE1 0x48070000
  36. #define OMAP2_I2C_BASE2 0x48072000
  37. #define OMAP2_I2C_BASE3 0x48060000
  38. #define OMAP4_I2C_BASE4 0x48350000
  39. static const char name[] = "i2c_omap";
  40. #define I2C_RESOURCE_BUILDER(base, irq) \
  41. { \
  42. .start = (base), \
  43. .end = (base) + OMAP_I2C_SIZE, \
  44. .flags = IORESOURCE_MEM, \
  45. }, \
  46. { \
  47. .start = (irq), \
  48. .flags = IORESOURCE_IRQ, \
  49. },
  50. static struct resource i2c_resources[][2] = {
  51. { I2C_RESOURCE_BUILDER(0, 0) },
  52. #if defined(CONFIG_ARCH_OMAP2PLUS)
  53. { I2C_RESOURCE_BUILDER(OMAP2_I2C_BASE2, 0) },
  54. #endif
  55. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  56. { I2C_RESOURCE_BUILDER(OMAP2_I2C_BASE3, 0) },
  57. #endif
  58. #if defined(CONFIG_ARCH_OMAP4)
  59. { I2C_RESOURCE_BUILDER(OMAP4_I2C_BASE4, 0) },
  60. #endif
  61. };
  62. #define I2C_DEV_BUILDER(bus_id, res, data) \
  63. { \
  64. .id = (bus_id), \
  65. .name = name, \
  66. .num_resources = ARRAY_SIZE(res), \
  67. .resource = (res), \
  68. .dev = { \
  69. .platform_data = (data), \
  70. }, \
  71. }
  72. static struct omap_i2c_bus_platform_data i2c_pdata[ARRAY_SIZE(i2c_resources)];
  73. static struct platform_device omap_i2c_devices[] = {
  74. I2C_DEV_BUILDER(1, i2c_resources[0], &i2c_pdata[0]),
  75. #if defined(CONFIG_ARCH_OMAP2PLUS)
  76. I2C_DEV_BUILDER(2, i2c_resources[1], &i2c_pdata[1]),
  77. #endif
  78. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  79. I2C_DEV_BUILDER(3, i2c_resources[2], &i2c_pdata[2]),
  80. #endif
  81. #if defined(CONFIG_ARCH_OMAP4)
  82. I2C_DEV_BUILDER(4, i2c_resources[3], &i2c_pdata[3]),
  83. #endif
  84. };
  85. #define OMAP_I2C_CMDLINE_SETUP (BIT(31))
  86. static int __init omap_i2c_nr_ports(void)
  87. {
  88. int ports = 0;
  89. if (cpu_class_is_omap1())
  90. ports = 1;
  91. else if (cpu_is_omap24xx())
  92. ports = 2;
  93. else if (cpu_is_omap34xx())
  94. ports = 3;
  95. else if (cpu_is_omap44xx())
  96. ports = 4;
  97. return ports;
  98. }
  99. /* Shared between omap2 and 3 */
  100. static resource_size_t omap2_i2c_irq[3] __initdata = {
  101. INT_24XX_I2C1_IRQ,
  102. INT_24XX_I2C2_IRQ,
  103. INT_34XX_I2C3_IRQ,
  104. };
  105. static resource_size_t omap4_i2c_irq[4] __initdata = {
  106. OMAP44XX_IRQ_I2C1,
  107. OMAP44XX_IRQ_I2C2,
  108. OMAP44XX_IRQ_I2C3,
  109. OMAP44XX_IRQ_I2C4,
  110. };
  111. static inline int omap1_i2c_add_bus(struct platform_device *pdev, int bus_id)
  112. {
  113. struct omap_i2c_bus_platform_data *pd;
  114. struct resource *res;
  115. pd = pdev->dev.platform_data;
  116. res = pdev->resource;
  117. res[0].start = OMAP1_I2C_BASE;
  118. res[0].end = res[0].start + OMAP_I2C_SIZE;
  119. res[1].start = INT_I2C;
  120. omap1_i2c_mux_pins(bus_id);
  121. return platform_device_register(pdev);
  122. }
  123. /*
  124. * XXX This function is a temporary compatibility wrapper - only
  125. * needed until the I2C driver can be converted to call
  126. * omap_pm_set_max_dev_wakeup_lat() and handle a return code.
  127. */
  128. static void omap_pm_set_max_mpu_wakeup_lat_compat(struct device *dev, long t)
  129. {
  130. omap_pm_set_max_mpu_wakeup_lat(dev, t);
  131. }
  132. static inline int omap2_i2c_add_bus(struct platform_device *pdev, int bus_id)
  133. {
  134. struct resource *res;
  135. resource_size_t *irq;
  136. res = pdev->resource;
  137. if (!cpu_is_omap44xx())
  138. irq = omap2_i2c_irq;
  139. else
  140. irq = omap4_i2c_irq;
  141. if (bus_id == 1) {
  142. res[0].start = OMAP2_I2C_BASE1;
  143. res[0].end = res[0].start + OMAP_I2C_SIZE;
  144. }
  145. res[1].start = irq[bus_id - 1];
  146. omap2_i2c_mux_pins(bus_id);
  147. /*
  148. * When waiting for completion of a i2c transfer, we need to
  149. * set a wake up latency constraint for the MPU. This is to
  150. * ensure quick enough wakeup from idle, when transfer
  151. * completes.
  152. */
  153. if (cpu_is_omap34xx()) {
  154. struct omap_i2c_bus_platform_data *pd;
  155. pd = pdev->dev.platform_data;
  156. pd->set_mpu_wkup_lat = omap_pm_set_max_mpu_wakeup_lat_compat;
  157. }
  158. return platform_device_register(pdev);
  159. }
  160. static int __init omap_i2c_add_bus(int bus_id)
  161. {
  162. struct platform_device *pdev;
  163. pdev = &omap_i2c_devices[bus_id - 1];
  164. if (cpu_class_is_omap1())
  165. return omap1_i2c_add_bus(pdev, bus_id);
  166. else
  167. return omap2_i2c_add_bus(pdev, bus_id);
  168. }
  169. /**
  170. * omap_i2c_bus_setup - Process command line options for the I2C bus speed
  171. * @str: String of options
  172. *
  173. * This function allow to override the default I2C bus speed for given I2C
  174. * bus with a command line option.
  175. *
  176. * Format: i2c_bus=bus_id,clkrate (in kHz)
  177. *
  178. * Returns 1 on success, 0 otherwise.
  179. */
  180. static int __init omap_i2c_bus_setup(char *str)
  181. {
  182. int ports;
  183. int ints[3];
  184. ports = omap_i2c_nr_ports();
  185. get_options(str, 3, ints);
  186. if (ints[0] < 2 || ints[1] < 1 || ints[1] > ports)
  187. return 0;
  188. i2c_pdata[ints[1] - 1].clkrate = ints[2];
  189. i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
  190. return 1;
  191. }
  192. __setup("i2c_bus=", omap_i2c_bus_setup);
  193. /*
  194. * Register busses defined in command line but that are not registered with
  195. * omap_register_i2c_bus from board initialization code.
  196. */
  197. static int __init omap_register_i2c_bus_cmdline(void)
  198. {
  199. int i, err = 0;
  200. for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
  201. if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
  202. i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  203. err = omap_i2c_add_bus(i + 1);
  204. if (err)
  205. goto out;
  206. }
  207. out:
  208. return err;
  209. }
  210. subsys_initcall(omap_register_i2c_bus_cmdline);
  211. /**
  212. * omap_register_i2c_bus - register I2C bus with device descriptors
  213. * @bus_id: bus id counting from number 1
  214. * @clkrate: clock rate of the bus in kHz
  215. * @info: pointer into I2C device descriptor table or NULL
  216. * @len: number of descriptors in the table
  217. *
  218. * Returns 0 on success or an error code.
  219. */
  220. int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
  221. struct i2c_board_info const *info,
  222. unsigned len)
  223. {
  224. int err;
  225. BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports());
  226. if (info) {
  227. err = i2c_register_board_info(bus_id, info, len);
  228. if (err)
  229. return err;
  230. }
  231. if (!i2c_pdata[bus_id - 1].clkrate)
  232. i2c_pdata[bus_id - 1].clkrate = clkrate;
  233. i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  234. return omap_i2c_add_bus(bus_id);
  235. }