i2c-core-slave.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core slave support code
  4. *
  5. * Copyright (C) 2014 by Wolfram Sang <[email protected]>
  6. */
  7. #include <dt-bindings/i2c/i2c.h>
  8. #include <linux/acpi.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/of.h>
  13. #include "i2c-core.h"
  14. #define CREATE_TRACE_POINTS
  15. #include <trace/events/i2c_slave.h>
  16. int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
  17. {
  18. int ret;
  19. if (WARN(IS_ERR_OR_NULL(client) || !slave_cb, "insufficient data\n"))
  20. return -EINVAL;
  21. if (!(client->flags & I2C_CLIENT_SLAVE))
  22. dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
  23. __func__);
  24. if (!(client->flags & I2C_CLIENT_TEN)) {
  25. /* Enforce stricter address checking */
  26. ret = i2c_check_7bit_addr_validity_strict(client->addr);
  27. if (ret) {
  28. dev_err(&client->dev, "%s: invalid address\n", __func__);
  29. return ret;
  30. }
  31. }
  32. if (!client->adapter->algo->reg_slave) {
  33. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  34. return -EOPNOTSUPP;
  35. }
  36. client->slave_cb = slave_cb;
  37. i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  38. ret = client->adapter->algo->reg_slave(client);
  39. i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  40. if (ret) {
  41. client->slave_cb = NULL;
  42. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  43. }
  44. return ret;
  45. }
  46. EXPORT_SYMBOL_GPL(i2c_slave_register);
  47. int i2c_slave_unregister(struct i2c_client *client)
  48. {
  49. int ret;
  50. if (IS_ERR_OR_NULL(client))
  51. return -EINVAL;
  52. if (!client->adapter->algo->unreg_slave) {
  53. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  54. return -EOPNOTSUPP;
  55. }
  56. i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  57. ret = client->adapter->algo->unreg_slave(client);
  58. i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  59. if (ret == 0)
  60. client->slave_cb = NULL;
  61. else
  62. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL_GPL(i2c_slave_unregister);
  66. int i2c_slave_event(struct i2c_client *client,
  67. enum i2c_slave_event event, u8 *val)
  68. {
  69. int ret = client->slave_cb(client, event, val);
  70. if (trace_i2c_slave_enabled())
  71. trace_i2c_slave(client, event, val, ret);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL_GPL(i2c_slave_event);
  75. /**
  76. * i2c_detect_slave_mode - detect operation mode
  77. * @dev: The device owning the bus
  78. *
  79. * This checks the device nodes for an I2C slave by checking the address
  80. * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
  81. * flag this means the device is configured to act as a I2C slave and it will
  82. * be listening at that address.
  83. *
  84. * Returns true if an I2C own slave address is detected, otherwise returns
  85. * false.
  86. */
  87. bool i2c_detect_slave_mode(struct device *dev)
  88. {
  89. if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
  90. struct device_node *child;
  91. u32 reg;
  92. for_each_child_of_node(dev->of_node, child) {
  93. of_property_read_u32(child, "reg", &reg);
  94. if (reg & I2C_OWN_SLAVE_ADDRESS) {
  95. of_node_put(child);
  96. return true;
  97. }
  98. }
  99. } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
  100. dev_dbg(dev, "ACPI slave is not supported yet\n");
  101. }
  102. return false;
  103. }
  104. EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);