i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 <linux/slab.h>
  30. #include <linux/err.h>
  31. #include <linux/clk.h>
  32. #include <mach/irqs.h>
  33. #include <plat/mux.h>
  34. #include <plat/i2c.h>
  35. #include <plat/omap-pm.h>
  36. #include <plat/omap_device.h>
  37. #define OMAP_I2C_SIZE 0x3f
  38. #define OMAP1_I2C_BASE 0xfffb3800
  39. static const char name[] = "omap_i2c";
  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. };
  53. #define I2C_DEV_BUILDER(bus_id, res, data) \
  54. { \
  55. .id = (bus_id), \
  56. .name = name, \
  57. .num_resources = ARRAY_SIZE(res), \
  58. .resource = (res), \
  59. .dev = { \
  60. .platform_data = (data), \
  61. }, \
  62. }
  63. #define MAX_OMAP_I2C_HWMOD_NAME_LEN 16
  64. #define OMAP_I2C_MAX_CONTROLLERS 4
  65. static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
  66. static struct platform_device omap_i2c_devices[] = {
  67. I2C_DEV_BUILDER(1, i2c_resources[0], &i2c_pdata[0]),
  68. };
  69. #define OMAP_I2C_CMDLINE_SETUP (BIT(31))
  70. static int __init omap_i2c_nr_ports(void)
  71. {
  72. int ports = 0;
  73. if (cpu_class_is_omap1())
  74. ports = 1;
  75. else if (cpu_is_omap24xx())
  76. ports = 2;
  77. else if (cpu_is_omap34xx())
  78. ports = 3;
  79. else if (cpu_is_omap44xx())
  80. ports = 4;
  81. return ports;
  82. }
  83. static inline int omap1_i2c_add_bus(int bus_id)
  84. {
  85. struct platform_device *pdev;
  86. struct omap_i2c_bus_platform_data *pdata;
  87. struct resource *res;
  88. omap1_i2c_mux_pins(bus_id);
  89. pdev = &omap_i2c_devices[bus_id - 1];
  90. res = pdev->resource;
  91. res[0].start = OMAP1_I2C_BASE;
  92. res[0].end = res[0].start + OMAP_I2C_SIZE;
  93. res[1].start = INT_I2C;
  94. pdata = &i2c_pdata[bus_id - 1];
  95. /* all OMAP1 have IP version 1 register set */
  96. pdata->rev = OMAP_I2C_IP_VERSION_1;
  97. /* all OMAP1 I2C are implemented like this */
  98. pdata->flags = OMAP_I2C_FLAG_NO_FIFO |
  99. OMAP_I2C_FLAG_SIMPLE_CLOCK |
  100. OMAP_I2C_FLAG_16BIT_DATA_REG |
  101. OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK;
  102. /* how the cpu bus is wired up differs for 7xx only */
  103. if (cpu_is_omap7xx())
  104. pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_1;
  105. else
  106. pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_2;
  107. return platform_device_register(pdev);
  108. }
  109. #ifdef CONFIG_ARCH_OMAP2PLUS
  110. /*
  111. * XXX This function is a temporary compatibility wrapper - only
  112. * needed until the I2C driver can be converted to call
  113. * omap_pm_set_max_dev_wakeup_lat() and handle a return code.
  114. */
  115. static void omap_pm_set_max_mpu_wakeup_lat_compat(struct device *dev, long t)
  116. {
  117. omap_pm_set_max_mpu_wakeup_lat(dev, t);
  118. }
  119. static inline int omap2_i2c_add_bus(int bus_id)
  120. {
  121. int l;
  122. struct omap_hwmod *oh;
  123. struct platform_device *pdev;
  124. char oh_name[MAX_OMAP_I2C_HWMOD_NAME_LEN];
  125. struct omap_i2c_bus_platform_data *pdata;
  126. struct omap_i2c_dev_attr *dev_attr;
  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[bus_id - 1];
  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. NULL, 0, 0);
  157. WARN(IS_ERR(pdev), "Could not build omap_device for %s\n", name);
  158. return PTR_RET(pdev);
  159. }
  160. #else
  161. static inline int omap2_i2c_add_bus(int bus_id)
  162. {
  163. return 0;
  164. }
  165. #endif
  166. static int __init omap_i2c_add_bus(int bus_id)
  167. {
  168. if (cpu_class_is_omap1())
  169. return omap1_i2c_add_bus(bus_id);
  170. else
  171. return omap2_i2c_add_bus(bus_id);
  172. }
  173. /**
  174. * omap_i2c_bus_setup - Process command line options for the I2C bus speed
  175. * @str: String of options
  176. *
  177. * This function allow to override the default I2C bus speed for given I2C
  178. * bus with a command line option.
  179. *
  180. * Format: i2c_bus=bus_id,clkrate (in kHz)
  181. *
  182. * Returns 1 on success, 0 otherwise.
  183. */
  184. static int __init omap_i2c_bus_setup(char *str)
  185. {
  186. int ports;
  187. int ints[3];
  188. ports = omap_i2c_nr_ports();
  189. get_options(str, 3, ints);
  190. if (ints[0] < 2 || ints[1] < 1 || ints[1] > ports)
  191. return 0;
  192. i2c_pdata[ints[1] - 1].clkrate = ints[2];
  193. i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
  194. return 1;
  195. }
  196. __setup("i2c_bus=", omap_i2c_bus_setup);
  197. /*
  198. * Register busses defined in command line but that are not registered with
  199. * omap_register_i2c_bus from board initialization code.
  200. */
  201. static int __init omap_register_i2c_bus_cmdline(void)
  202. {
  203. int i, err = 0;
  204. for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
  205. if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
  206. i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  207. err = omap_i2c_add_bus(i + 1);
  208. if (err)
  209. goto out;
  210. }
  211. out:
  212. return err;
  213. }
  214. subsys_initcall(omap_register_i2c_bus_cmdline);
  215. /**
  216. * omap_register_i2c_bus - register I2C bus with device descriptors
  217. * @bus_id: bus id counting from number 1
  218. * @clkrate: clock rate of the bus in kHz
  219. * @info: pointer into I2C device descriptor table or NULL
  220. * @len: number of descriptors in the table
  221. *
  222. * Returns 0 on success or an error code.
  223. */
  224. int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
  225. struct i2c_board_info const *info,
  226. unsigned len)
  227. {
  228. int err;
  229. BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports());
  230. if (info) {
  231. err = i2c_register_board_info(bus_id, info, len);
  232. if (err)
  233. return err;
  234. }
  235. if (!i2c_pdata[bus_id - 1].clkrate)
  236. i2c_pdata[bus_id - 1].clkrate = clkrate;
  237. i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  238. return omap_i2c_add_bus(bus_id);
  239. }