i2c.c 6.3 KB

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