i2c-core-of.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core OF support code
  4. *
  5. * Copyright (C) 2008 Jochen Friedrich <[email protected]>
  6. * based on a previous patch from Jon Smirl <[email protected]>
  7. *
  8. * Copyright (C) 2013, 2018 Wolfram Sang <[email protected]>
  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_modalias_node(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_get_property(node, "wakeup-source", NULL))
  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 int of_dev_or_parent_node_match(struct device *dev, const void *data)
  92. {
  93. if (dev->of_node == data)
  94. return 1;
  95. if (dev->parent)
  96. return dev->parent->of_node == data;
  97. return 0;
  98. }
  99. /* must call put_device() when done with returned i2c_client device */
  100. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  101. {
  102. struct device *dev;
  103. struct i2c_client *client;
  104. dev = bus_find_device_by_of_node(&i2c_bus_type, node);
  105. if (!dev)
  106. return NULL;
  107. client = i2c_verify_client(dev);
  108. if (!client)
  109. put_device(dev);
  110. return client;
  111. }
  112. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  113. /* must call put_device() when done with returned i2c_adapter device */
  114. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  115. {
  116. struct device *dev;
  117. struct i2c_adapter *adapter;
  118. dev = bus_find_device(&i2c_bus_type, NULL, node,
  119. of_dev_or_parent_node_match);
  120. if (!dev)
  121. return NULL;
  122. adapter = i2c_verify_adapter(dev);
  123. if (!adapter)
  124. put_device(dev);
  125. return adapter;
  126. }
  127. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  128. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  129. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  130. {
  131. struct i2c_adapter *adapter;
  132. adapter = of_find_i2c_adapter_by_node(node);
  133. if (!adapter)
  134. return NULL;
  135. if (!try_module_get(adapter->owner)) {
  136. put_device(&adapter->dev);
  137. adapter = NULL;
  138. }
  139. return adapter;
  140. }
  141. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  142. static const struct of_device_id*
  143. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  144. struct i2c_client *client)
  145. {
  146. const char *name;
  147. for (; matches->compatible[0]; matches++) {
  148. /*
  149. * Adding devices through the i2c sysfs interface provides us
  150. * a string to match which may be compatible with the device
  151. * tree compatible strings, however with no actual of_node the
  152. * of_match_device() will not match
  153. */
  154. if (sysfs_streq(client->name, matches->compatible))
  155. return matches;
  156. name = strchr(matches->compatible, ',');
  157. if (!name)
  158. name = matches->compatible;
  159. else
  160. name++;
  161. if (sysfs_streq(client->name, name))
  162. return matches;
  163. }
  164. return NULL;
  165. }
  166. const struct of_device_id
  167. *i2c_of_match_device(const struct of_device_id *matches,
  168. struct i2c_client *client)
  169. {
  170. const struct of_device_id *match;
  171. if (!(client && matches))
  172. return NULL;
  173. match = of_match_device(matches, &client->dev);
  174. if (match)
  175. return match;
  176. return i2c_of_match_device_sysfs(matches, client);
  177. }
  178. EXPORT_SYMBOL_GPL(i2c_of_match_device);
  179. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  180. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  181. void *arg)
  182. {
  183. struct of_reconfig_data *rd = arg;
  184. struct i2c_adapter *adap;
  185. struct i2c_client *client;
  186. switch (of_reconfig_get_state_change(action, rd)) {
  187. case OF_RECONFIG_CHANGE_ADD:
  188. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  189. if (adap == NULL)
  190. return NOTIFY_OK; /* not for us */
  191. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  192. put_device(&adap->dev);
  193. return NOTIFY_OK;
  194. }
  195. /*
  196. * Clear the flag before adding the device so that fw_devlink
  197. * doesn't skip adding consumers to this device.
  198. */
  199. rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
  200. client = of_i2c_register_device(adap, rd->dn);
  201. if (IS_ERR(client)) {
  202. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  203. rd->dn);
  204. put_device(&adap->dev);
  205. of_node_clear_flag(rd->dn, OF_POPULATED);
  206. return notifier_from_errno(PTR_ERR(client));
  207. }
  208. put_device(&adap->dev);
  209. break;
  210. case OF_RECONFIG_CHANGE_REMOVE:
  211. /* already depopulated? */
  212. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  213. return NOTIFY_OK;
  214. /* find our device by node */
  215. client = of_find_i2c_device_by_node(rd->dn);
  216. if (client == NULL)
  217. return NOTIFY_OK; /* no? not meant for us */
  218. /* unregister takes one ref away */
  219. i2c_unregister_device(client);
  220. /* and put the reference of the find */
  221. put_device(&client->dev);
  222. break;
  223. }
  224. return NOTIFY_OK;
  225. }
  226. struct notifier_block i2c_of_notifier = {
  227. .notifier_call = of_i2c_notify,
  228. };
  229. #endif /* CONFIG_OF_DYNAMIC */