i2c-core-of-prober.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core OF component prober code
  4. *
  5. * Copyright (C) 2024 Google LLC
  6. */
  7. #include <linux/cleanup.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/dev_printk.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-of-prober.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. #include <linux/stddef.h>
  20. /*
  21. * Some devices, such as Google Hana Chromebooks, are produced by multiple
  22. * vendors each using their preferred components. Such components are all
  23. * in the device tree. Instead of having all of them enabled and having each
  24. * driver separately try and probe its device while fighting over shared
  25. * resources, they can be marked as "fail-needs-probe" and have a prober
  26. * figure out which one is actually used beforehand.
  27. *
  28. * This prober assumes such drop-in parts are on the same I2C bus, have
  29. * non-conflicting addresses, and can be directly probed by seeing which
  30. * address responds.
  31. *
  32. * TODO:
  33. * - Support I2C muxes
  34. */
  35. static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type)
  36. {
  37. struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type);
  38. if (!node) {
  39. dev_err(dev, "Could not find %s device node\n", type);
  40. return NULL;
  41. }
  42. struct device_node *i2c_node __free(device_node) = of_get_parent(node);
  43. if (!of_node_name_eq(i2c_node, "i2c")) {
  44. dev_err(dev, "%s device isn't on I2C bus\n", type);
  45. return NULL;
  46. }
  47. if (!of_device_is_available(i2c_node)) {
  48. dev_err(dev, "I2C controller not available\n");
  49. return NULL;
  50. }
  51. return no_free_ptr(i2c_node);
  52. }
  53. static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node)
  54. {
  55. int ret;
  56. dev_dbg(dev, "Enabling %pOF\n", node);
  57. struct of_changeset *ocs __free(kfree) = kzalloc_obj(*ocs);
  58. if (!ocs)
  59. return -ENOMEM;
  60. of_changeset_init(ocs);
  61. ret = of_changeset_update_prop_string(ocs, node, "status", "okay");
  62. if (ret)
  63. return ret;
  64. ret = of_changeset_apply(ocs);
  65. if (ret) {
  66. /* ocs needs to be explicitly cleaned up before being freed. */
  67. of_changeset_destroy(ocs);
  68. } else {
  69. /*
  70. * ocs is intentionally kept around as it needs to
  71. * exist as long as the change is applied.
  72. */
  73. void *ptr __always_unused = no_free_ptr(ocs);
  74. }
  75. return ret;
  76. }
  77. static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops;
  78. /**
  79. * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus
  80. * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages.
  81. * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options
  82. * for the prober.
  83. * @ctx: Context data for callbacks.
  84. *
  85. * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type)
  86. * on the same I2C bus that have their status marked as "fail-needs-probe".
  87. *
  88. * Assumes that across the entire device tree the only instances of nodes
  89. * with "type" prefixed node names (not including the address portion) are
  90. * the ones that need handling for second source components. In other words,
  91. * if "type" is "touchscreen", then all device nodes named "touchscreen*"
  92. * are the ones that need probing. There cannot be another "touchscreen*"
  93. * node that is already enabled.
  94. *
  95. * Assumes that for each "type" of component, only one actually exists. In
  96. * other words, only one matching and existing device will be enabled.
  97. *
  98. * Context: Process context only. Does non-atomic I2C transfers.
  99. * Should only be used from a driver probe function, as the function
  100. * can return -EPROBE_DEFER if the I2C adapter or other resources
  101. * are unavailable.
  102. * Return: 0 on success or no-op, error code otherwise.
  103. * A no-op can happen when it seems like the device tree already
  104. * has components of the type to be probed already enabled. This
  105. * can happen when the device tree had not been updated to mark
  106. * the status of the to-be-probed components as "fail-needs-probe".
  107. * Or this function was already run with the same parameters and
  108. * succeeded in enabling a component. The latter could happen if
  109. * the user had multiple types of components to probe, and one of
  110. * them down the list caused a deferred probe. This is expected
  111. * behavior.
  112. */
  113. int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx)
  114. {
  115. const struct i2c_of_probe_ops *ops;
  116. const char *type;
  117. struct i2c_adapter *i2c;
  118. int ret;
  119. ops = cfg->ops ?: &i2c_of_probe_dummy_ops;
  120. type = cfg->type;
  121. struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type);
  122. if (!i2c_node)
  123. return -ENODEV;
  124. /*
  125. * If any devices of the given "type" are already enabled then this function is a no-op.
  126. * Either the device tree hasn't been modified to work with this probe function, or the
  127. * function had already run before and enabled some component.
  128. */
  129. for_each_child_of_node_with_prefix(i2c_node, node, type)
  130. if (of_device_is_available(node))
  131. return 0;
  132. i2c = of_get_i2c_adapter_by_node(i2c_node);
  133. if (!i2c)
  134. return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n");
  135. /* Grab and enable resources */
  136. ret = 0;
  137. if (ops->enable)
  138. ret = ops->enable(dev, i2c_node, ctx);
  139. if (ret)
  140. goto out_put_i2c_adapter;
  141. for_each_child_of_node_with_prefix(i2c_node, node, type) {
  142. union i2c_smbus_data data;
  143. u32 addr;
  144. if (of_property_read_u32(node, "reg", &addr))
  145. continue;
  146. if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0)
  147. continue;
  148. /* Found a device that is responding */
  149. if (ops->cleanup_early)
  150. ops->cleanup_early(dev, ctx);
  151. ret = i2c_of_probe_enable_node(dev, node);
  152. break;
  153. }
  154. if (ops->cleanup)
  155. ops->cleanup(dev, ctx);
  156. out_put_i2c_adapter:
  157. i2c_put_adapter(i2c);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, "I2C_OF_PROBER");
  161. static int i2c_of_probe_simple_get_supply(struct device *dev, struct device_node *node,
  162. struct i2c_of_probe_simple_ctx *ctx)
  163. {
  164. const char *supply_name;
  165. struct regulator *supply;
  166. /*
  167. * It's entirely possible for the component's device node to not have the
  168. * regulator supplies. While it does not make sense from a hardware perspective,
  169. * the supplies could be always on or otherwise not modeled in the device tree,
  170. * but the device would still work.
  171. */
  172. supply_name = ctx->opts->supply_name;
  173. if (!supply_name)
  174. return 0;
  175. supply = of_regulator_get_optional(dev, node, supply_name);
  176. if (IS_ERR(supply)) {
  177. return dev_err_probe(dev, PTR_ERR(supply),
  178. "Failed to get regulator supply \"%s\" from %pOF\n",
  179. supply_name, node);
  180. }
  181. ctx->supply = supply;
  182. return 0;
  183. }
  184. static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx *ctx)
  185. {
  186. regulator_put(ctx->supply);
  187. ctx->supply = NULL;
  188. }
  189. static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
  190. {
  191. int ret;
  192. if (!ctx->supply)
  193. return 0;
  194. dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name);
  195. ret = regulator_enable(ctx->supply);
  196. if (ret)
  197. return ret;
  198. if (ctx->opts->post_power_on_delay_ms)
  199. msleep(ctx->opts->post_power_on_delay_ms);
  200. return 0;
  201. }
  202. static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
  203. {
  204. if (!ctx->supply)
  205. return;
  206. dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_name);
  207. regulator_disable(ctx->supply);
  208. }
  209. static int i2c_of_probe_simple_get_gpiod(struct device *dev, struct device_node *node,
  210. struct i2c_of_probe_simple_ctx *ctx)
  211. {
  212. struct fwnode_handle *fwnode = of_fwnode_handle(node);
  213. struct gpio_desc *gpiod;
  214. const char *con_id;
  215. /* NULL signals no GPIO needed */
  216. if (!ctx->opts->gpio_name)
  217. return 0;
  218. /* An empty string signals an unnamed GPIO */
  219. if (!ctx->opts->gpio_name[0])
  220. con_id = NULL;
  221. else
  222. con_id = ctx->opts->gpio_name;
  223. gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober");
  224. if (IS_ERR(gpiod))
  225. return PTR_ERR(gpiod);
  226. ctx->gpiod = gpiod;
  227. return 0;
  228. }
  229. static void i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx *ctx)
  230. {
  231. gpiod_put(ctx->gpiod);
  232. ctx->gpiod = NULL;
  233. }
  234. static int i2c_of_probe_simple_set_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
  235. {
  236. int ret;
  237. if (!ctx->gpiod)
  238. return 0;
  239. dev_dbg(dev, "Configuring GPIO\n");
  240. ret = gpiod_direction_output(ctx->gpiod, ctx->opts->gpio_assert_to_enable);
  241. if (ret)
  242. return ret;
  243. if (ctx->opts->post_gpio_config_delay_ms)
  244. msleep(ctx->opts->post_gpio_config_delay_ms);
  245. return 0;
  246. }
  247. static void i2c_of_probe_simple_disable_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
  248. {
  249. gpiod_set_value(ctx->gpiod, !ctx->opts->gpio_assert_to_enable);
  250. }
  251. /**
  252. * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources
  253. * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
  254. * @bus_node: Pointer to the &struct device_node of the I2C adapter.
  255. * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
  256. *
  257. * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply.
  258. * If &i2c_of_probe_simple_opts->gpio_name is given, request the named GPIO. Or if it is
  259. * the empty string, request the unnamed GPIO.
  260. * If a regulator supply was found, enable that regulator.
  261. * If a GPIO line was found, configure the GPIO line to output and set value
  262. * according to given options.
  263. *
  264. * Return: %0 on success or no-op, or a negative error number on failure.
  265. */
  266. int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data)
  267. {
  268. struct i2c_of_probe_simple_ctx *ctx = data;
  269. struct device_node *node;
  270. const char *compat;
  271. int ret;
  272. dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", bus_node);
  273. if (!ctx || !ctx->opts)
  274. return -EINVAL;
  275. compat = ctx->opts->res_node_compatible;
  276. if (!compat)
  277. return -EINVAL;
  278. node = of_get_compatible_child(bus_node, compat);
  279. if (!node)
  280. return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" found\n",
  281. compat);
  282. ret = i2c_of_probe_simple_get_supply(dev, node, ctx);
  283. if (ret)
  284. goto out_put_node;
  285. ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx);
  286. if (ret)
  287. goto out_put_supply;
  288. ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
  289. if (ret)
  290. goto out_put_gpiod;
  291. ret = i2c_of_probe_simple_set_gpio(dev, ctx);
  292. if (ret)
  293. goto out_disable_regulator;
  294. return 0;
  295. out_disable_regulator:
  296. i2c_of_probe_simple_disable_regulator(dev, ctx);
  297. out_put_gpiod:
  298. i2c_of_probe_simple_put_gpiod(ctx);
  299. out_put_supply:
  300. i2c_of_probe_simple_put_supply(ctx);
  301. out_put_node:
  302. of_node_put(node);
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, "I2C_OF_PROBER");
  306. /**
  307. * i2c_of_probe_simple_cleanup_early - \
  308. * Simple helper for I2C OF prober to release GPIOs before component is enabled
  309. * @dev: Pointer to the &struct device of the caller; unused.
  310. * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
  311. *
  312. * GPIO descriptors are exclusive and have to be released before the
  313. * actual driver probes so that the latter can acquire them.
  314. */
  315. void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data)
  316. {
  317. struct i2c_of_probe_simple_ctx *ctx = data;
  318. i2c_of_probe_simple_put_gpiod(ctx);
  319. }
  320. EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup_early, "I2C_OF_PROBER");
  321. /**
  322. * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers
  323. * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
  324. * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
  325. *
  326. * * If a GPIO line was found and not yet released, set its value to the opposite of that
  327. * set in i2c_of_probe_simple_enable() and release it.
  328. * * If a regulator supply was found, disable that regulator and release it.
  329. */
  330. void i2c_of_probe_simple_cleanup(struct device *dev, void *data)
  331. {
  332. struct i2c_of_probe_simple_ctx *ctx = data;
  333. /* GPIO operations here are no-ops if i2c_of_probe_simple_cleanup_early was called. */
  334. i2c_of_probe_simple_disable_gpio(dev, ctx);
  335. i2c_of_probe_simple_put_gpiod(ctx);
  336. i2c_of_probe_simple_disable_regulator(dev, ctx);
  337. i2c_of_probe_simple_put_supply(ctx);
  338. }
  339. EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, "I2C_OF_PROBER");
  340. struct i2c_of_probe_ops i2c_of_probe_simple_ops = {
  341. .enable = i2c_of_probe_simple_enable,
  342. .cleanup_early = i2c_of_probe_simple_cleanup_early,
  343. .cleanup = i2c_of_probe_simple_cleanup,
  344. };
  345. EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, "I2C_OF_PROBER");