i2c.c 3.9 KB

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