i2c.c 3.7 KB

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