i2c-core-slave.c 2.9 KB

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