|
| 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 | + |
| 8 | +#include <linux/cleanup.h> |
| 9 | +#include <linux/device.h> |
| 10 | +#include <linux/dev_printk.h> |
| 11 | +#include <linux/err.h> |
| 12 | +#include <linux/i2c.h> |
| 13 | +#include <linux/i2c-of-prober.h> |
| 14 | +#include <linux/module.h> |
| 15 | +#include <linux/of.h> |
| 16 | +#include <linux/slab.h> |
| 17 | +#include <linux/stddef.h> |
| 18 | + |
| 19 | +/* |
| 20 | + * Some devices, such as Google Hana Chromebooks, are produced by multiple |
| 21 | + * vendors each using their preferred components. Such components are all |
| 22 | + * in the device tree. Instead of having all of them enabled and having each |
| 23 | + * driver separately try and probe its device while fighting over shared |
| 24 | + * resources, they can be marked as "fail-needs-probe" and have a prober |
| 25 | + * figure out which one is actually used beforehand. |
| 26 | + * |
| 27 | + * This prober assumes such drop-in parts are on the same I2C bus, have |
| 28 | + * non-conflicting addresses, and can be directly probed by seeing which |
| 29 | + * address responds. |
| 30 | + * |
| 31 | + * TODO: |
| 32 | + * - Support handling common regulators. |
| 33 | + * - Support handling common GPIOs. |
| 34 | + * - Support I2C muxes |
| 35 | + */ |
| 36 | + |
| 37 | +static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type) |
| 38 | +{ |
| 39 | + struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type); |
| 40 | + if (!node) { |
| 41 | + dev_err(dev, "Could not find %s device node\n", type); |
| 42 | + return NULL; |
| 43 | + } |
| 44 | + |
| 45 | + struct device_node *i2c_node __free(device_node) = of_get_parent(node); |
| 46 | + if (!of_node_name_eq(i2c_node, "i2c")) { |
| 47 | + dev_err(dev, "%s device isn't on I2C bus\n", type); |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + |
| 51 | + if (!of_device_is_available(i2c_node)) { |
| 52 | + dev_err(dev, "I2C controller not available\n"); |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + |
| 56 | + return no_free_ptr(i2c_node); |
| 57 | +} |
| 58 | + |
| 59 | +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) |
| 60 | +{ |
| 61 | + int ret; |
| 62 | + |
| 63 | + dev_dbg(dev, "Enabling %pOF\n", node); |
| 64 | + |
| 65 | + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); |
| 66 | + if (!ocs) |
| 67 | + return -ENOMEM; |
| 68 | + |
| 69 | + of_changeset_init(ocs); |
| 70 | + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); |
| 71 | + if (ret) |
| 72 | + return ret; |
| 73 | + |
| 74 | + ret = of_changeset_apply(ocs); |
| 75 | + if (ret) { |
| 76 | + /* ocs needs to be explicitly cleaned up before being freed. */ |
| 77 | + of_changeset_destroy(ocs); |
| 78 | + } else { |
| 79 | + /* |
| 80 | + * ocs is intentionally kept around as it needs to |
| 81 | + * exist as long as the change is applied. |
| 82 | + */ |
| 83 | + void *ptr __always_unused = no_free_ptr(ocs); |
| 84 | + } |
| 85 | + |
| 86 | + return ret; |
| 87 | +} |
| 88 | + |
| 89 | +static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops; |
| 90 | + |
| 91 | +/** |
| 92 | + * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus |
| 93 | + * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages. |
| 94 | + * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options |
| 95 | + * for the prober. |
| 96 | + * @ctx: Context data for callbacks. |
| 97 | + * |
| 98 | + * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type) |
| 99 | + * on the same I2C bus that have their status marked as "fail-needs-probe". |
| 100 | + * |
| 101 | + * Assumes that across the entire device tree the only instances of nodes |
| 102 | + * with "type" prefixed node names (not including the address portion) are |
| 103 | + * the ones that need handling for second source components. In other words, |
| 104 | + * if "type" is "touchscreen", then all device nodes named "touchscreen*" |
| 105 | + * are the ones that need probing. There cannot be another "touchscreen*" |
| 106 | + * node that is already enabled. |
| 107 | + * |
| 108 | + * Assumes that for each "type" of component, only one actually exists. In |
| 109 | + * other words, only one matching and existing device will be enabled. |
| 110 | + * |
| 111 | + * Context: Process context only. Does non-atomic I2C transfers. |
| 112 | + * Should only be used from a driver probe function, as the function |
| 113 | + * can return -EPROBE_DEFER if the I2C adapter or other resources |
| 114 | + * are unavailable. |
| 115 | + * Return: 0 on success or no-op, error code otherwise. |
| 116 | + * A no-op can happen when it seems like the device tree already |
| 117 | + * has components of the type to be probed already enabled. This |
| 118 | + * can happen when the device tree had not been updated to mark |
| 119 | + * the status of the to-be-probed components as "fail-needs-probe". |
| 120 | + * Or this function was already run with the same parameters and |
| 121 | + * succeeded in enabling a component. The latter could happen if |
| 122 | + * the user had multiple types of components to probe, and one of |
| 123 | + * them down the list caused a deferred probe. This is expected |
| 124 | + * behavior. |
| 125 | + */ |
| 126 | +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) |
| 127 | +{ |
| 128 | + const struct i2c_of_probe_ops *ops; |
| 129 | + const char *type; |
| 130 | + struct i2c_adapter *i2c; |
| 131 | + int ret; |
| 132 | + |
| 133 | + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; |
| 134 | + type = cfg->type; |
| 135 | + |
| 136 | + struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type); |
| 137 | + if (!i2c_node) |
| 138 | + return -ENODEV; |
| 139 | + |
| 140 | + /* |
| 141 | + * If any devices of the given "type" are already enabled then this function is a no-op. |
| 142 | + * Either the device tree hasn't been modified to work with this probe function, or the |
| 143 | + * function had already run before and enabled some component. |
| 144 | + */ |
| 145 | + for_each_child_of_node_with_prefix(i2c_node, node, type) |
| 146 | + if (of_device_is_available(node)) |
| 147 | + return 0; |
| 148 | + |
| 149 | + i2c = of_get_i2c_adapter_by_node(i2c_node); |
| 150 | + if (!i2c) |
| 151 | + return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); |
| 152 | + |
| 153 | + /* Grab and enable resources */ |
| 154 | + ret = 0; |
| 155 | + if (ops->enable) |
| 156 | + ret = ops->enable(dev, i2c_node, ctx); |
| 157 | + if (ret) |
| 158 | + goto out_put_i2c_adapter; |
| 159 | + |
| 160 | + for_each_child_of_node_with_prefix(i2c_node, node, type) { |
| 161 | + union i2c_smbus_data data; |
| 162 | + u32 addr; |
| 163 | + |
| 164 | + if (of_property_read_u32(node, "reg", &addr)) |
| 165 | + continue; |
| 166 | + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) |
| 167 | + continue; |
| 168 | + |
| 169 | + /* Found a device that is responding */ |
| 170 | + if (ops->cleanup_early) |
| 171 | + ops->cleanup_early(dev, ctx); |
| 172 | + ret = i2c_of_probe_enable_node(dev, node); |
| 173 | + break; |
| 174 | + } |
| 175 | + |
| 176 | + if (ops->cleanup) |
| 177 | + ops->cleanup(dev, ctx); |
| 178 | +out_put_i2c_adapter: |
| 179 | + i2c_put_adapter(i2c); |
| 180 | + |
| 181 | + return ret; |
| 182 | +} |
| 183 | +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); |
0 commit comments