i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. return platform_device_register(pdev);
  96. }
  97. #ifdef CONFIG_ARCH_OMAP2PLUS
  98. /*
  99. * XXX This function is a temporary compatibility wrapper - only
  100. * needed until the I2C driver can be converted to call
  101. * omap_pm_set_max_dev_wakeup_lat() and handle a return code.
  102. */
  103. static void omap_pm_set_max_mpu_wakeup_lat_compat(struct device *dev, long t)
  104. {
  105. omap_pm_set_max_mpu_wakeup_lat(dev, t);
  106. }
  107. static struct omap_device_pm_latency omap_i2c_latency[] = {
  108. [0] = {
  109. .deactivate_func = omap_device_idle_hwmods,
  110. .activate_func = omap_device_enable_hwmods,
  111. .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
  112. },
  113. };
  114. static inline int omap2_i2c_add_bus(int bus_id)
  115. {
  116. int l;
  117. struct omap_hwmod *oh;
  118. struct omap_device *od;
  119. char oh_name[MAX_OMAP_I2C_HWMOD_NAME_LEN];
  120. struct omap_i2c_bus_platform_data *pdata;
  121. omap2_i2c_mux_pins(bus_id);
  122. l = snprintf(oh_name, MAX_OMAP_I2C_HWMOD_NAME_LEN, "i2c%d", bus_id);
  123. WARN(l >= MAX_OMAP_I2C_HWMOD_NAME_LEN,
  124. "String buffer overflow in I2C%d device setup\n", bus_id);
  125. oh = omap_hwmod_lookup(oh_name);
  126. if (!oh) {
  127. pr_err("Could not look up %s\n", oh_name);
  128. return -EEXIST;
  129. }
  130. pdata = &i2c_pdata[bus_id - 1];
  131. /*
  132. * When waiting for completion of a i2c transfer, we need to
  133. * set a wake up latency constraint for the MPU. This is to
  134. * ensure quick enough wakeup from idle, when transfer
  135. * completes.
  136. * Only omap3 has support for constraints
  137. */
  138. if (cpu_is_omap34xx())
  139. pdata->set_mpu_wkup_lat = omap_pm_set_max_mpu_wakeup_lat_compat;
  140. od = omap_device_build(name, bus_id, oh, pdata,
  141. sizeof(struct omap_i2c_bus_platform_data),
  142. omap_i2c_latency, ARRAY_SIZE(omap_i2c_latency), 0);
  143. WARN(IS_ERR(od), "Could not build omap_device for %s\n", name);
  144. return PTR_ERR(od);
  145. }
  146. #else
  147. static inline int omap2_i2c_add_bus(int bus_id)
  148. {
  149. return 0;
  150. }
  151. #endif
  152. static int __init omap_i2c_add_bus(int bus_id)
  153. {
  154. if (cpu_class_is_omap1())
  155. return omap1_i2c_add_bus(bus_id);
  156. else
  157. return omap2_i2c_add_bus(bus_id);
  158. }
  159. /**
  160. * omap_i2c_bus_setup - Process command line options for the I2C bus speed
  161. * @str: String of options
  162. *
  163. * This function allow to override the default I2C bus speed for given I2C
  164. * bus with a command line option.
  165. *
  166. * Format: i2c_bus=bus_id,clkrate (in kHz)
  167. *
  168. * Returns 1 on success, 0 otherwise.
  169. */
  170. static int __init omap_i2c_bus_setup(char *str)
  171. {
  172. int ports;
  173. int ints[3];
  174. ports = omap_i2c_nr_ports();
  175. get_options(str, 3, ints);
  176. if (ints[0] < 2 || ints[1] < 1 || ints[1] > ports)
  177. return 0;
  178. i2c_pdata[ints[1] - 1].clkrate = ints[2];
  179. i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
  180. return 1;
  181. }
  182. __setup("i2c_bus=", omap_i2c_bus_setup);
  183. /*
  184. * Register busses defined in command line but that are not registered with
  185. * omap_register_i2c_bus from board initialization code.
  186. */
  187. static int __init omap_register_i2c_bus_cmdline(void)
  188. {
  189. int i, err = 0;
  190. for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
  191. if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
  192. i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  193. err = omap_i2c_add_bus(i + 1);
  194. if (err)
  195. goto out;
  196. }
  197. out:
  198. return err;
  199. }
  200. subsys_initcall(omap_register_i2c_bus_cmdline);
  201. /**
  202. * omap_register_i2c_bus - register I2C bus with device descriptors
  203. * @bus_id: bus id counting from number 1
  204. * @clkrate: clock rate of the bus in kHz
  205. * @info: pointer into I2C device descriptor table or NULL
  206. * @len: number of descriptors in the table
  207. *
  208. * Returns 0 on success or an error code.
  209. */
  210. int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
  211. struct i2c_board_info const *info,
  212. unsigned len)
  213. {
  214. int err;
  215. BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports());
  216. if (info) {
  217. err = i2c_register_board_info(bus_id, info, len);
  218. if (err)
  219. return err;
  220. }
  221. if (!i2c_pdata[bus_id - 1].clkrate)
  222. i2c_pdata[bus_id - 1].clkrate = clkrate;
  223. i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  224. return omap_i2c_add_bus(bus_id);
  225. }