i2c-core-of.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core OF support code
  4. *
  5. * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
  6. * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  7. *
  8. * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
  9. */
  10. #include <dt-bindings/i2c/i2c.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/sysfs.h>
  18. #include "i2c-core.h"
  19. int of_i2c_get_board_info(struct device *dev, struct device_node *node,
  20. struct i2c_board_info *info)
  21. {
  22. u32 addr;
  23. int ret;
  24. memset(info, 0, sizeof(*info));
  25. if (of_alias_from_compatible(node, info->type, sizeof(info->type)) < 0) {
  26. dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
  27. return -EINVAL;
  28. }
  29. ret = of_property_read_u32(node, "reg", &addr);
  30. if (ret) {
  31. dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
  32. return ret;
  33. }
  34. if (addr & I2C_TEN_BIT_ADDRESS) {
  35. addr &= ~I2C_TEN_BIT_ADDRESS;
  36. info->flags |= I2C_CLIENT_TEN;
  37. }
  38. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  39. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  40. info->flags |= I2C_CLIENT_SLAVE;
  41. }
  42. info->addr = addr;
  43. info->fwnode = of_fwnode_handle(node);
  44. if (of_property_read_bool(node, "host-notify"))
  45. info->flags |= I2C_CLIENT_HOST_NOTIFY;
  46. if (of_property_read_bool(node, "wakeup-source"))
  47. info->flags |= I2C_CLIENT_WAKE;
  48. return 0;
  49. }
  50. EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
  51. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  52. struct device_node *node)
  53. {
  54. struct i2c_client *client;
  55. struct i2c_board_info info;
  56. int ret;
  57. dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
  58. ret = of_i2c_get_board_info(&adap->dev, node, &info);
  59. if (ret)
  60. return ERR_PTR(ret);
  61. client = i2c_new_client_device(adap, &info);
  62. if (IS_ERR(client))
  63. dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
  64. return client;
  65. }
  66. void of_i2c_register_devices(struct i2c_adapter *adap)
  67. {
  68. struct device_node *bus, *node;
  69. struct i2c_client *client;
  70. /* Only register child devices if the adapter has a node pointer set */
  71. if (!adap->dev.of_node)
  72. return;
  73. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  74. bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  75. if (!bus)
  76. bus = of_node_get(adap->dev.of_node);
  77. for_each_available_child_of_node(bus, node) {
  78. if (of_node_test_and_set_flag(node, OF_POPULATED))
  79. continue;
  80. client = of_i2c_register_device(adap, node);
  81. if (IS_ERR(client)) {
  82. dev_err(&adap->dev,
  83. "Failed to create I2C device for %pOF\n",
  84. node);
  85. of_node_clear_flag(node, OF_POPULATED);
  86. }
  87. }
  88. of_node_put(bus);
  89. }
  90. static const struct of_device_id*
  91. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  92. struct i2c_client *client)
  93. {
  94. const char *name;
  95. for (; matches->compatible[0]; matches++) {
  96. /*
  97. * Adding devices through the i2c sysfs interface provides us
  98. * a string to match which may be compatible with the device
  99. * tree compatible strings, however with no actual of_node the
  100. * of_match_device() will not match
  101. */
  102. if (sysfs_streq(client->name, matches->compatible))
  103. return matches;
  104. name = strchr(matches->compatible, ',');
  105. if (!name)
  106. name = matches->compatible;
  107. else
  108. name++;
  109. if (sysfs_streq(client->name, name))
  110. return matches;
  111. }
  112. return NULL;
  113. }
  114. const struct of_device_id
  115. *i2c_of_match_device(const struct of_device_id *matches,
  116. struct i2c_client *client)
  117. {
  118. const struct of_device_id *match;
  119. if (!(client && matches))
  120. return NULL;
  121. match = of_match_device(matches, &client->dev);
  122. if (match)
  123. return match;
  124. return i2c_of_match_device_sysfs(matches, client);
  125. }
  126. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  127. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  128. void *arg)
  129. {
  130. struct of_reconfig_data *rd = arg;
  131. struct i2c_adapter *adap;
  132. struct i2c_client *client;
  133. switch (of_reconfig_get_state_change(action, rd)) {
  134. case OF_RECONFIG_CHANGE_ADD:
  135. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  136. if (adap == NULL)
  137. return NOTIFY_OK; /* not for us */
  138. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  139. put_device(&adap->dev);
  140. return NOTIFY_OK;
  141. }
  142. /*
  143. * Clear the flag before adding the device so that fw_devlink
  144. * doesn't skip adding consumers to this device.
  145. */
  146. rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
  147. client = of_i2c_register_device(adap, rd->dn);
  148. if (IS_ERR(client)) {
  149. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  150. rd->dn);
  151. put_device(&adap->dev);
  152. of_node_clear_flag(rd->dn, OF_POPULATED);
  153. return notifier_from_errno(PTR_ERR(client));
  154. }
  155. put_device(&adap->dev);
  156. break;
  157. case OF_RECONFIG_CHANGE_REMOVE:
  158. /* already depopulated? */
  159. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  160. return NOTIFY_OK;
  161. /* find our device by node */
  162. client = of_find_i2c_device_by_node(rd->dn);
  163. if (client == NULL)
  164. return NOTIFY_OK; /* no? not meant for us */
  165. /* unregister takes one ref away */
  166. i2c_unregister_device(client);
  167. /* and put the reference of the find */
  168. put_device(&client->dev);
  169. break;
  170. }
  171. return NOTIFY_OK;
  172. }
  173. struct notifier_block i2c_of_notifier = {
  174. .notifier_call = of_i2c_notify,
  175. };
  176. #endif /* CONFIG_OF_DYNAMIC */