i2c-core-of.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Linux I2C core OF support code
  3. *
  4. * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
  5. * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  6. *
  7. * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. #include <dt-bindings/i2c/i2c.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/i2c.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include "i2c-core.h"
  22. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  23. struct device_node *node)
  24. {
  25. struct i2c_client *client;
  26. struct i2c_board_info info = {};
  27. struct dev_archdata dev_ad = {};
  28. u32 addr;
  29. int ret;
  30. dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
  31. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  32. dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
  33. node);
  34. return ERR_PTR(-EINVAL);
  35. }
  36. ret = of_property_read_u32(node, "reg", &addr);
  37. if (ret) {
  38. dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
  39. return ERR_PTR(ret);
  40. }
  41. if (addr & I2C_TEN_BIT_ADDRESS) {
  42. addr &= ~I2C_TEN_BIT_ADDRESS;
  43. info.flags |= I2C_CLIENT_TEN;
  44. }
  45. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  46. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  47. info.flags |= I2C_CLIENT_SLAVE;
  48. }
  49. info.addr = addr;
  50. info.archdata = &dev_ad;
  51. info.of_node = of_node_get(node);
  52. if (of_property_read_bool(node, "host-notify"))
  53. info.flags |= I2C_CLIENT_HOST_NOTIFY;
  54. if (of_get_property(node, "wakeup-source", NULL))
  55. info.flags |= I2C_CLIENT_WAKE;
  56. client = i2c_new_device(adap, &info);
  57. if (!client) {
  58. dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
  59. of_node_put(node);
  60. return ERR_PTR(-EINVAL);
  61. }
  62. return client;
  63. }
  64. void of_i2c_register_devices(struct i2c_adapter *adap)
  65. {
  66. struct device_node *bus, *node;
  67. struct i2c_client *client;
  68. /* Only register child devices if the adapter has a node pointer set */
  69. if (!adap->dev.of_node)
  70. return;
  71. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  72. bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  73. if (!bus)
  74. bus = of_node_get(adap->dev.of_node);
  75. for_each_available_child_of_node(bus, node) {
  76. if (of_node_test_and_set_flag(node, OF_POPULATED))
  77. continue;
  78. client = of_i2c_register_device(adap, node);
  79. if (IS_ERR(client)) {
  80. dev_err(&adap->dev,
  81. "Failed to create I2C device for %pOF\n",
  82. node);
  83. of_node_clear_flag(node, OF_POPULATED);
  84. }
  85. }
  86. of_node_put(bus);
  87. }
  88. static int of_dev_node_match(struct device *dev, void *data)
  89. {
  90. return dev->of_node == data;
  91. }
  92. /* must call put_device() when done with returned i2c_client device */
  93. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  94. {
  95. struct device *dev;
  96. struct i2c_client *client;
  97. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  98. if (!dev)
  99. return NULL;
  100. client = i2c_verify_client(dev);
  101. if (!client)
  102. put_device(dev);
  103. return client;
  104. }
  105. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  106. /* must call put_device() when done with returned i2c_adapter device */
  107. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  108. {
  109. struct device *dev;
  110. struct i2c_adapter *adapter;
  111. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  112. if (!dev)
  113. return NULL;
  114. adapter = i2c_verify_adapter(dev);
  115. if (!adapter)
  116. put_device(dev);
  117. return adapter;
  118. }
  119. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  120. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  121. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  122. {
  123. struct i2c_adapter *adapter;
  124. adapter = of_find_i2c_adapter_by_node(node);
  125. if (!adapter)
  126. return NULL;
  127. if (!try_module_get(adapter->owner)) {
  128. put_device(&adapter->dev);
  129. adapter = NULL;
  130. }
  131. return adapter;
  132. }
  133. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  134. static const struct of_device_id*
  135. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  136. struct i2c_client *client)
  137. {
  138. const char *name;
  139. for (; matches->compatible[0]; matches++) {
  140. /*
  141. * Adding devices through the i2c sysfs interface provides us
  142. * a string to match which may be compatible with the device
  143. * tree compatible strings, however with no actual of_node the
  144. * of_match_device() will not match
  145. */
  146. if (sysfs_streq(client->name, matches->compatible))
  147. return matches;
  148. name = strchr(matches->compatible, ',');
  149. if (!name)
  150. name = matches->compatible;
  151. else
  152. name++;
  153. if (sysfs_streq(client->name, name))
  154. return matches;
  155. }
  156. return NULL;
  157. }
  158. const struct of_device_id
  159. *i2c_of_match_device(const struct of_device_id *matches,
  160. struct i2c_client *client)
  161. {
  162. const struct of_device_id *match;
  163. if (!(client && matches))
  164. return NULL;
  165. match = of_match_device(matches, &client->dev);
  166. if (match)
  167. return match;
  168. return i2c_of_match_device_sysfs(matches, client);
  169. }
  170. EXPORT_SYMBOL_GPL(i2c_of_match_device);
  171. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  172. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  173. void *arg)
  174. {
  175. struct of_reconfig_data *rd = arg;
  176. struct i2c_adapter *adap;
  177. struct i2c_client *client;
  178. switch (of_reconfig_get_state_change(action, rd)) {
  179. case OF_RECONFIG_CHANGE_ADD:
  180. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  181. if (adap == NULL)
  182. return NOTIFY_OK; /* not for us */
  183. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  184. put_device(&adap->dev);
  185. return NOTIFY_OK;
  186. }
  187. client = of_i2c_register_device(adap, rd->dn);
  188. put_device(&adap->dev);
  189. if (IS_ERR(client)) {
  190. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  191. rd->dn);
  192. of_node_clear_flag(rd->dn, OF_POPULATED);
  193. return notifier_from_errno(PTR_ERR(client));
  194. }
  195. break;
  196. case OF_RECONFIG_CHANGE_REMOVE:
  197. /* already depopulated? */
  198. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  199. return NOTIFY_OK;
  200. /* find our device by node */
  201. client = of_find_i2c_device_by_node(rd->dn);
  202. if (client == NULL)
  203. return NOTIFY_OK; /* no? not meant for us */
  204. /* unregister takes one ref away */
  205. i2c_unregister_device(client);
  206. /* and put the reference of the find */
  207. put_device(&client->dev);
  208. break;
  209. }
  210. return NOTIFY_OK;
  211. }
  212. struct notifier_block i2c_of_notifier = {
  213. .notifier_call = of_i2c_notify,
  214. };
  215. #endif /* CONFIG_OF_DYNAMIC */