i2c-core-of.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. int of_i2c_get_board_info(struct device *dev, struct device_node *node,
  23. struct i2c_board_info *info)
  24. {
  25. u32 addr;
  26. int ret;
  27. memset(info, 0, sizeof(*info));
  28. if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
  29. dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
  30. return -EINVAL;
  31. }
  32. ret = of_property_read_u32(node, "reg", &addr);
  33. if (ret) {
  34. dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
  35. return ret;
  36. }
  37. if (addr & I2C_TEN_BIT_ADDRESS) {
  38. addr &= ~I2C_TEN_BIT_ADDRESS;
  39. info->flags |= I2C_CLIENT_TEN;
  40. }
  41. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  42. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  43. info->flags |= I2C_CLIENT_SLAVE;
  44. }
  45. info->addr = addr;
  46. info->of_node = node;
  47. if (of_property_read_bool(node, "host-notify"))
  48. info->flags |= I2C_CLIENT_HOST_NOTIFY;
  49. if (of_get_property(node, "wakeup-source", NULL))
  50. info->flags |= I2C_CLIENT_WAKE;
  51. return 0;
  52. }
  53. EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
  54. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  55. struct device_node *node)
  56. {
  57. struct i2c_client *client;
  58. struct i2c_board_info info;
  59. int ret;
  60. dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
  61. ret = of_i2c_get_board_info(&adap->dev, node, &info);
  62. if (ret)
  63. return ERR_PTR(ret);
  64. client = i2c_new_device(adap, &info);
  65. if (!client) {
  66. dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. return client;
  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_err(&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. static int of_dev_or_parent_node_match(struct device *dev, void *data)
  100. {
  101. if (dev->of_node == data)
  102. return 1;
  103. if (dev->parent)
  104. return dev->parent->of_node == data;
  105. return 0;
  106. }
  107. /* must call put_device() when done with returned i2c_client device */
  108. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  109. {
  110. struct device *dev;
  111. struct i2c_client *client;
  112. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  113. if (!dev)
  114. return NULL;
  115. client = i2c_verify_client(dev);
  116. if (!client)
  117. put_device(dev);
  118. return client;
  119. }
  120. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  121. /* must call put_device() when done with returned i2c_adapter device */
  122. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  123. {
  124. struct device *dev;
  125. struct i2c_adapter *adapter;
  126. dev = bus_find_device(&i2c_bus_type, NULL, node,
  127. of_dev_or_parent_node_match);
  128. if (!dev)
  129. return NULL;
  130. adapter = i2c_verify_adapter(dev);
  131. if (!adapter)
  132. put_device(dev);
  133. return adapter;
  134. }
  135. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  136. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  137. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  138. {
  139. struct i2c_adapter *adapter;
  140. adapter = of_find_i2c_adapter_by_node(node);
  141. if (!adapter)
  142. return NULL;
  143. if (!try_module_get(adapter->owner)) {
  144. put_device(&adapter->dev);
  145. adapter = NULL;
  146. }
  147. return adapter;
  148. }
  149. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  150. static const struct of_device_id*
  151. i2c_of_match_device_sysfs(const struct of_device_id *matches,
  152. struct i2c_client *client)
  153. {
  154. const char *name;
  155. for (; matches->compatible[0]; matches++) {
  156. /*
  157. * Adding devices through the i2c sysfs interface provides us
  158. * a string to match which may be compatible with the device
  159. * tree compatible strings, however with no actual of_node the
  160. * of_match_device() will not match
  161. */
  162. if (sysfs_streq(client->name, matches->compatible))
  163. return matches;
  164. name = strchr(matches->compatible, ',');
  165. if (!name)
  166. name = matches->compatible;
  167. else
  168. name++;
  169. if (sysfs_streq(client->name, name))
  170. return matches;
  171. }
  172. return NULL;
  173. }
  174. const struct of_device_id
  175. *i2c_of_match_device(const struct of_device_id *matches,
  176. struct i2c_client *client)
  177. {
  178. const struct of_device_id *match;
  179. if (!(client && matches))
  180. return NULL;
  181. match = of_match_device(matches, &client->dev);
  182. if (match)
  183. return match;
  184. return i2c_of_match_device_sysfs(matches, client);
  185. }
  186. EXPORT_SYMBOL_GPL(i2c_of_match_device);
  187. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  188. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  189. void *arg)
  190. {
  191. struct of_reconfig_data *rd = arg;
  192. struct i2c_adapter *adap;
  193. struct i2c_client *client;
  194. switch (of_reconfig_get_state_change(action, rd)) {
  195. case OF_RECONFIG_CHANGE_ADD:
  196. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  197. if (adap == NULL)
  198. return NOTIFY_OK; /* not for us */
  199. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  200. put_device(&adap->dev);
  201. return NOTIFY_OK;
  202. }
  203. client = of_i2c_register_device(adap, rd->dn);
  204. if (IS_ERR(client)) {
  205. dev_err(&adap->dev, "failed to create client for '%pOF'\n",
  206. rd->dn);
  207. put_device(&adap->dev);
  208. of_node_clear_flag(rd->dn, OF_POPULATED);
  209. return notifier_from_errno(PTR_ERR(client));
  210. }
  211. put_device(&adap->dev);
  212. break;
  213. case OF_RECONFIG_CHANGE_REMOVE:
  214. /* already depopulated? */
  215. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  216. return NOTIFY_OK;
  217. /* find our device by node */
  218. client = of_find_i2c_device_by_node(rd->dn);
  219. if (client == NULL)
  220. return NOTIFY_OK; /* no? not meant for us */
  221. /* unregister takes one ref away */
  222. i2c_unregister_device(client);
  223. /* and put the reference of the find */
  224. put_device(&client->dev);
  225. break;
  226. }
  227. return NOTIFY_OK;
  228. }
  229. struct notifier_block i2c_of_notifier = {
  230. .notifier_call = of_i2c_notify,
  231. };
  232. #endif /* CONFIG_OF_DYNAMIC */