i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/cpu.h>
  34. #include "i2c.h"
  35. #define OMAP_I2C_MAX_CONTROLLERS 4
  36. static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
  37. #define OMAP_I2C_CMDLINE_SETUP (BIT(31))
  38. static int __init omap_i2c_nr_ports(void)
  39. {
  40. int ports = 0;
  41. if (cpu_class_is_omap1())
  42. ports = 1;
  43. else if (cpu_is_omap24xx())
  44. ports = 2;
  45. else if (cpu_is_omap34xx())
  46. ports = 3;
  47. else if (cpu_is_omap44xx())
  48. ports = 4;
  49. return ports;
  50. }
  51. /**
  52. * omap_i2c_bus_setup - Process command line options for the I2C bus speed
  53. * @str: String of options
  54. *
  55. * This function allow to override the default I2C bus speed for given I2C
  56. * bus with a command line option.
  57. *
  58. * Format: i2c_bus=bus_id,clkrate (in kHz)
  59. *
  60. * Returns 1 on success, 0 otherwise.
  61. */
  62. static int __init omap_i2c_bus_setup(char *str)
  63. {
  64. int ports;
  65. int ints[3];
  66. ports = omap_i2c_nr_ports();
  67. get_options(str, 3, ints);
  68. if (ints[0] < 2 || ints[1] < 1 || ints[1] > ports)
  69. return 0;
  70. i2c_pdata[ints[1] - 1].clkrate = ints[2];
  71. i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
  72. return 1;
  73. }
  74. __setup("i2c_bus=", omap_i2c_bus_setup);
  75. /*
  76. * Register busses defined in command line but that are not registered with
  77. * omap_register_i2c_bus from board initialization code.
  78. */
  79. static int __init omap_register_i2c_bus_cmdline(void)
  80. {
  81. int i, err = 0;
  82. for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
  83. if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
  84. i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  85. err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
  86. if (err)
  87. goto out;
  88. }
  89. out:
  90. return err;
  91. }
  92. subsys_initcall(omap_register_i2c_bus_cmdline);
  93. /**
  94. * omap_register_i2c_bus - register I2C bus with device descriptors
  95. * @bus_id: bus id counting from number 1
  96. * @clkrate: clock rate of the bus in kHz
  97. * @info: pointer into I2C device descriptor table or NULL
  98. * @len: number of descriptors in the table
  99. *
  100. * Returns 0 on success or an error code.
  101. */
  102. int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
  103. struct i2c_board_info const *info,
  104. unsigned len)
  105. {
  106. int err;
  107. BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports());
  108. if (info) {
  109. err = i2c_register_board_info(bus_id, info, len);
  110. if (err)
  111. return err;
  112. }
  113. if (!i2c_pdata[bus_id - 1].clkrate)
  114. i2c_pdata[bus_id - 1].clkrate = clkrate;
  115. i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
  116. return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
  117. }