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 <wsa@sang-engineering.com>
  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 <linux/property.h>
  14. #include "i2c-core.h"
  15. #define CREATE_TRACE_POINTS
  16. #include <trace/events/i2c_slave.h>
  17. int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
  18. {
  19. int ret;
  20. if (WARN(IS_ERR_OR_NULL(client) || !slave_cb, "insufficient data\n"))
  21. return -EINVAL;
  22. if (!(client->flags & I2C_CLIENT_SLAVE))
  23. dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
  24. __func__);
  25. if (!(client->flags & I2C_CLIENT_TEN)) {
  26. /* Enforce stricter address checking */
  27. ret = i2c_check_7bit_addr_validity_strict(client->addr);
  28. if (ret) {
  29. dev_err(&client->dev, "%s: invalid address\n", __func__);
  30. return ret;
  31. }
  32. }
  33. if (!client->adapter->algo->reg_slave) {
  34. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  35. return -EOPNOTSUPP;
  36. }
  37. client->slave_cb = slave_cb;
  38. i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  39. ret = client->adapter->algo->reg_slave(client);
  40. i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  41. if (ret) {
  42. client->slave_cb = NULL;
  43. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  44. }
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(i2c_slave_register);
  48. int i2c_slave_unregister(struct i2c_client *client)
  49. {
  50. int ret;
  51. if (IS_ERR_OR_NULL(client))
  52. return -EINVAL;
  53. if (!client->adapter->algo->unreg_slave) {
  54. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  55. return -EOPNOTSUPP;
  56. }
  57. i2c_lock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  58. ret = client->adapter->algo->unreg_slave(client);
  59. i2c_unlock_bus(client->adapter, I2C_LOCK_ROOT_ADAPTER);
  60. if (ret == 0)
  61. client->slave_cb = NULL;
  62. else
  63. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  64. return ret;
  65. }
  66. EXPORT_SYMBOL_GPL(i2c_slave_unregister);
  67. int i2c_slave_event(struct i2c_client *client,
  68. enum i2c_slave_event event, u8 *val)
  69. {
  70. int ret = client->slave_cb(client, event, val);
  71. if (trace_i2c_slave_enabled())
  72. trace_i2c_slave(client, event, val, ret);
  73. return ret;
  74. }
  75. EXPORT_SYMBOL_GPL(i2c_slave_event);
  76. /**
  77. * i2c_detect_slave_mode - detect operation mode
  78. * @dev: The device owning the bus
  79. *
  80. * This checks the device nodes for an I2C slave by checking the address
  81. * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
  82. * flag this means the device is configured to act as a I2C slave and it will
  83. * be listening at that address.
  84. *
  85. * Returns true if an I2C own slave address is detected, otherwise returns
  86. * false.
  87. */
  88. bool i2c_detect_slave_mode(struct device *dev)
  89. {
  90. struct fwnode_handle *fwnode = dev_fwnode(dev);
  91. if (is_of_node(fwnode)) {
  92. u32 reg;
  93. fwnode_for_each_child_node_scoped(fwnode, child) {
  94. fwnode_property_read_u32(child, "reg", &reg);
  95. if (reg & I2C_OWN_SLAVE_ADDRESS)
  96. return true;
  97. }
  98. } else if (is_acpi_device_node(fwnode)) {
  99. dev_dbg(dev, "ACPI slave is not supported yet\n");
  100. }
  101. return false;
  102. }
  103. EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);