i2c-core-slave.c 3.0 KB

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