i2c-core-of.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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->of_node = node;
  44. info->fwnode = of_fwnode_handle(node);
  45. if (of_property_read_bool(node, "host-notify"))
  46. info->flags |= I2C_CLIENT_HOST_NOTIFY;
  47. if (of_property_read_bool(node, "wakeup-source"))
  48. info->flags |= I2C_CLIENT_WAKE;
  49. return 0;
  50. }
  51. EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
  52. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  53. struct device_node *node)
  54. {
  55. struct i2c_client *client;
  56. struct i2c_board_info info;
  57. int ret;
  58. dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
  59. ret = of_i2c_get_board_info(&adap->dev, node, &info);
  60. if (ret)
  61. return ERR_PTR(ret);
  62. client = i2c_new_client_device(adap, &info);
  63. if (IS_ERR(client))
  64. dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
  65. return client;
  66. }
  67. void of_i2c_register_devices(struct i2c_adapter *adap)
  68. {
  69. struct device_node *bus, *node;
  70. struct i2c_client *client;
  71. /* Only register child devices if the adapter has a node pointer set */
  72. if (!adap->dev.of_node)
  73. return;
  74. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  75. bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  76. if (!bus)
  77. bus = of_node_get(adap->dev.of_node);
  78. for_each_available_child_of_node(bus, node) {
  79. if (of_node_test_and_set_flag(node, OF_POPULATED))
  80. continue;
  81. client = of_i2c_register_device(adap, node);
  82. if (IS_ERR(client)) {
  83. dev_err(&adap->dev,
  84. "Failed to create I2C device for %pOF\n",
  85. node);
  86. of_node_clear_flag(node, OF_POPULATED);
  87. }
  88. }
  89. of_node_put(bus);
  90. }
  91. static const struct of_device_id*
  92. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  93. struct i2c_client *client)
  94. {
  95. const char *name;
  96. for (; matches->compatible[0]; matches++) {
  97. /*
  98. * Adding devices through the i2c sysfs interface provides us
  99. * a string to match which may be compatible with the device
  100. * tree compatible strings, however with no actual of_node the
  101. * of_match_device() will not match
  102. */
  103. if (sysfs_streq(client->name, matches->compatible))
  104. return matches;
  105. name = strchr(matches->compatible, ',');
  106. if (!name)
  107. name = matches->compatible;
  108. else
  109. name++;
  110. if (sysfs_streq(client->name, name))
  111. return matches;
  112. }
  113. return NULL;
  114. }
  115. const struct of_device_id
  116. *i2c_of_match_device(const struct of_device_id *matches,
  117. struct i2c_client *client)
  118. {
  119. const struct of_device_id *match;
  120. if (!(client && matches))
  121. return NULL;
  122. match = of_match_device(matches, &client->dev);
  123. if (match)
  124. return match;
  125. return i2c_of_match_device_sysfs(matches, client);
  126. }
  127. EXPORT_SYMBOL_GPL(i2c_of_match_device);
  128. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  129. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  130. void *arg)
  131. {
  132. struct of_reconfig_data *rd = arg;
  133. struct i2c_adapter *adap;
  134. struct i2c_client *client;
  135. switch (of_reconfig_get_state_change(action, rd)) {
  136. case OF_RECONFIG_CHANGE_ADD:
  137. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  138. if (adap == NULL)
  139. return NOTIFY_OK; /* not for us */
  140. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  141. put_device(&adap->dev);
  142. return NOTIFY_OK;
  143. }
  144. /*
  145. * Clear the flag before adding the device so that fw_devlink
  146. * doesn't skip adding consumers to this device.
  147. */
  148. rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
  149. client = of_i2c_register_device(adap, rd->dn);
  150. if (IS_ERR(client)) {
  151. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  152. rd->dn);
  153. put_device(&adap->dev);
  154. of_node_clear_flag(rd->dn, OF_POPULATED);
  155. return notifier_from_errno(PTR_ERR(client));
  156. }
  157. put_device(&adap->dev);
  158. break;
  159. case OF_RECONFIG_CHANGE_REMOVE:
  160. /* already depopulated? */
  161. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  162. return NOTIFY_OK;
  163. /* find our device by node */
  164. client = of_find_i2c_device_by_node(rd->dn);
  165. if (client == NULL)
  166. return NOTIFY_OK; /* no? not meant for us */
  167. /* unregister takes one ref away */
  168. i2c_unregister_device(client);
  169. /* and put the reference of the find */
  170. put_device(&client->dev);
  171. break;
  172. }
  173. return NOTIFY_OK;
  174. }
  175. struct notifier_block i2c_of_notifier = {
  176. .notifier_call = of_i2c_notify,
  177. };
  178. #endif /* CONFIG_OF_DYNAMIC */