i2c-core-of.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 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 *result;
  26. struct i2c_board_info info = {};
  27. struct dev_archdata dev_ad = {};
  28. const __be32 *addr_be;
  29. u32 addr;
  30. int len;
  31. dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
  32. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  33. dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
  34. node);
  35. return ERR_PTR(-EINVAL);
  36. }
  37. addr_be = of_get_property(node, "reg", &len);
  38. if (!addr_be || (len < sizeof(*addr_be))) {
  39. dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
  40. return ERR_PTR(-EINVAL);
  41. }
  42. addr = be32_to_cpup(addr_be);
  43. if (addr & I2C_TEN_BIT_ADDRESS) {
  44. addr &= ~I2C_TEN_BIT_ADDRESS;
  45. info.flags |= I2C_CLIENT_TEN;
  46. }
  47. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  48. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  49. info.flags |= I2C_CLIENT_SLAVE;
  50. }
  51. if (i2c_check_addr_validity(addr, info.flags)) {
  52. dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
  53. addr, node);
  54. return ERR_PTR(-EINVAL);
  55. }
  56. info.addr = addr;
  57. info.of_node = of_node_get(node);
  58. info.archdata = &dev_ad;
  59. if (of_property_read_bool(node, "host-notify"))
  60. info.flags |= I2C_CLIENT_HOST_NOTIFY;
  61. if (of_get_property(node, "wakeup-source", NULL))
  62. info.flags |= I2C_CLIENT_WAKE;
  63. result = i2c_new_device(adap, &info);
  64. if (result == NULL) {
  65. dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
  66. of_node_put(node);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. return result;
  70. }
  71. void of_i2c_register_devices(struct i2c_adapter *adap)
  72. {
  73. struct device_node *bus, *node;
  74. struct i2c_client *client;
  75. /* Only register child devices if the adapter has a node pointer set */
  76. if (!adap->dev.of_node)
  77. return;
  78. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  79. bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  80. if (!bus)
  81. bus = of_node_get(adap->dev.of_node);
  82. for_each_available_child_of_node(bus, node) {
  83. if (of_node_test_and_set_flag(node, OF_POPULATED))
  84. continue;
  85. client = of_i2c_register_device(adap, node);
  86. if (IS_ERR(client)) {
  87. dev_warn(&adap->dev,
  88. "Failed to create I2C device for %pOF\n",
  89. node);
  90. of_node_clear_flag(node, OF_POPULATED);
  91. }
  92. }
  93. of_node_put(bus);
  94. }
  95. static int of_dev_node_match(struct device *dev, void *data)
  96. {
  97. return dev->of_node == data;
  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(&i2c_bus_type, NULL, node, of_dev_node_match);
  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, of_dev_node_match);
  119. if (!dev)
  120. return NULL;
  121. adapter = i2c_verify_adapter(dev);
  122. if (!adapter)
  123. put_device(dev);
  124. return adapter;
  125. }
  126. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  127. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  128. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  129. {
  130. struct i2c_adapter *adapter;
  131. adapter = of_find_i2c_adapter_by_node(node);
  132. if (!adapter)
  133. return NULL;
  134. if (!try_module_get(adapter->owner)) {
  135. put_device(&adapter->dev);
  136. adapter = NULL;
  137. }
  138. return adapter;
  139. }
  140. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  141. static const struct of_device_id*
  142. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  143. struct i2c_client *client)
  144. {
  145. const char *name;
  146. for (; matches->compatible[0]; matches++) {
  147. /*
  148. * Adding devices through the i2c sysfs interface provides us
  149. * a string to match which may be compatible with the device
  150. * tree compatible strings, however with no actual of_node the
  151. * of_match_device() will not match
  152. */
  153. if (sysfs_streq(client->name, matches->compatible))
  154. return matches;
  155. name = strchr(matches->compatible, ',');
  156. if (!name)
  157. name = matches->compatible;
  158. else
  159. name++;
  160. if (sysfs_streq(client->name, name))
  161. return matches;
  162. }
  163. return NULL;
  164. }
  165. const struct of_device_id
  166. *i2c_of_match_device(const struct of_device_id *matches,
  167. struct i2c_client *client)
  168. {
  169. const struct of_device_id *match;
  170. if (!(client && matches))
  171. return NULL;
  172. match = of_match_device(matches, &client->dev);
  173. if (match)
  174. return match;
  175. return i2c_of_match_device_sysfs(matches, client);
  176. }
  177. EXPORT_SYMBOL_GPL(i2c_of_match_device);
  178. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  179. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  180. void *arg)
  181. {
  182. struct of_reconfig_data *rd = arg;
  183. struct i2c_adapter *adap;
  184. struct i2c_client *client;
  185. switch (of_reconfig_get_state_change(action, rd)) {
  186. case OF_RECONFIG_CHANGE_ADD:
  187. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  188. if (adap == NULL)
  189. return NOTIFY_OK; /* not for us */
  190. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  191. put_device(&adap->dev);
  192. return NOTIFY_OK;
  193. }
  194. client = of_i2c_register_device(adap, rd->dn);
  195. if (IS_ERR(client)) {
  196. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  197. rd->dn);
  198. put_device(&adap->dev);
  199. of_node_clear_flag(rd->dn, OF_POPULATED);
  200. return notifier_from_errno(PTR_ERR(client));
  201. }
  202. put_device(&adap->dev);
  203. break;
  204. case OF_RECONFIG_CHANGE_REMOVE:
  205. /* already depopulated? */
  206. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  207. return NOTIFY_OK;
  208. /* find our device by node */
  209. client = of_find_i2c_device_by_node(rd->dn);
  210. if (client == NULL)
  211. return NOTIFY_OK; /* no? not meant for us */
  212. /* unregister takes one ref away */
  213. i2c_unregister_device(client);
  214. /* and put the reference of the find */
  215. put_device(&client->dev);
  216. break;
  217. }
  218. return NOTIFY_OK;
  219. }
  220. struct notifier_block i2c_of_notifier = {
  221. .notifier_call = of_i2c_notify,
  222. };
  223. #endif /* CONFIG_OF_DYNAMIC */