i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <linux/i2c.h>
  22. #include <linux/i2c-omap.h>
  23. #include <mach/mux.h>
  24. #include "soc.h"
  25. #include <plat/i2c.h>
  26. #define OMAP_I2C_SIZE 0x3f
  27. #define OMAP1_I2C_BASE 0xfffb3800
  28. static const char name[] = "omap_i2c";
  29. static struct resource i2c_resources[2] = {
  30. };
  31. static struct platform_device omap_i2c_devices[1] = {
  32. };
  33. static void __init omap1_i2c_mux_pins(int bus_id)
  34. {
  35. if (cpu_is_omap7xx()) {
  36. omap_cfg_reg(I2C_7XX_SDA);
  37. omap_cfg_reg(I2C_7XX_SCL);
  38. } else {
  39. omap_cfg_reg(I2C_SDA);
  40. omap_cfg_reg(I2C_SCL);
  41. }
  42. }
  43. int __init omap_i2c_add_bus(struct omap_i2c_bus_platform_data *pdata,
  44. int bus_id)
  45. {
  46. struct platform_device *pdev;
  47. struct resource *res;
  48. if (bus_id > 1)
  49. return -EINVAL;
  50. omap1_i2c_mux_pins(bus_id);
  51. pdev = &omap_i2c_devices[bus_id - 1];
  52. pdev->id = bus_id;
  53. pdev->name = name;
  54. pdev->num_resources = ARRAY_SIZE(i2c_resources);
  55. res = i2c_resources;
  56. res[0].start = OMAP1_I2C_BASE;
  57. res[0].end = res[0].start + OMAP_I2C_SIZE;
  58. res[0].flags = IORESOURCE_MEM;
  59. res[1].start = INT_I2C;
  60. res[1].flags = IORESOURCE_IRQ;
  61. pdev->resource = res;
  62. /* all OMAP1 have IP version 1 register set */
  63. pdata->rev = OMAP_I2C_IP_VERSION_1;
  64. /* all OMAP1 I2C are implemented like this */
  65. pdata->flags = OMAP_I2C_FLAG_NO_FIFO |
  66. OMAP_I2C_FLAG_SIMPLE_CLOCK |
  67. OMAP_I2C_FLAG_16BIT_DATA_REG |
  68. OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK;
  69. /* how the cpu bus is wired up differs for 7xx only */
  70. if (cpu_is_omap7xx())
  71. pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_1;
  72. else
  73. pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_2;
  74. pdev->dev.platform_data = pdata;
  75. return platform_device_register(pdev);
  76. }
  77. #define OMAP_I2C_MAX_CONTROLLERS 4
  78. static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
  79. #define OMAP_I2C_CMDLINE_SETUP (BIT(31))
  80. /**
  81. * omap_i2c_bus_setup - Process command line options for the I2C bus speed
  82. * @str: String of options
  83. *
  84. * This function allow to override the default I2C bus speed for given I2C
  85. * bus with a command line option.
  86. *
  87. * Format: i2c_bus=bus_id,clkrate (in kHz)
  88. *
  89. * Returns 1 on success, 0 otherwise.
  90. */
  91. static int __init omap_i2c_bus_setup(char *str)
  92. {
  93. int ints[3];
  94. get_options(str, 3, ints);
  95. if (ints[0] < 2 || ints[1] < 1 ||
  96. ints[1] > OMAP_I2C_MAX_CONTROLLERS)
  97. return 0;
  98. i2c_pdata[ints[1] - 1].clkrate = ints[2];
  99. i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
  100. return 1;
  101. }
  102. __setup("i2c_bus=", omap_i2c_bus_setup);
  103. /*
  104. * Register busses defined in command line but that are not registered with
  105. * omap_register_i2c_bus from board initialization code.
  106. */
  107. int __init omap_register_i2c_bus_cmdline(void)
  108. {
  109. int i, err = 0;
  110. for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
  111. if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
  112. i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  113. err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
  114. if (err)
  115. goto out;
  116. }
  117. out:
  118. return err;
  119. }
  120. /**
  121. * omap_register_i2c_bus - register I2C bus with device descriptors
  122. * @bus_id: bus id counting from number 1
  123. * @clkrate: clock rate of the bus in kHz
  124. * @info: pointer into I2C device descriptor table or NULL
  125. * @len: number of descriptors in the table
  126. *
  127. * Returns 0 on success or an error code.
  128. */
  129. int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
  130. struct i2c_board_info const *info,
  131. unsigned len)
  132. {
  133. int err;
  134. BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
  135. if (info) {
  136. err = i2c_register_board_info(bus_id, info, len);
  137. if (err)
  138. return err;
  139. }
  140. if (!i2c_pdata[bus_id - 1].clkrate)
  141. i2c_pdata[bus_id - 1].clkrate = clkrate;
  142. i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  143. return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
  144. }
  145. static int __init omap_i2c_cmdline(void)
  146. {
  147. return omap_register_i2c_bus_cmdline();
  148. }
  149. subsys_initcall(omap_i2c_cmdline);