1157ce8f3SChen-Yu Tsai // SPDX-License-Identifier: GPL-2.0-or-later 2157ce8f3SChen-Yu Tsai /* 3157ce8f3SChen-Yu Tsai * Linux I2C core OF component prober code 4157ce8f3SChen-Yu Tsai * 5157ce8f3SChen-Yu Tsai * Copyright (C) 2024 Google LLC 6157ce8f3SChen-Yu Tsai */ 7157ce8f3SChen-Yu Tsai 8157ce8f3SChen-Yu Tsai #include <linux/cleanup.h> 9*89726114SChen-Yu Tsai #include <linux/delay.h> 10157ce8f3SChen-Yu Tsai #include <linux/device.h> 11157ce8f3SChen-Yu Tsai #include <linux/dev_printk.h> 12157ce8f3SChen-Yu Tsai #include <linux/err.h> 13157ce8f3SChen-Yu Tsai #include <linux/i2c.h> 14157ce8f3SChen-Yu Tsai #include <linux/i2c-of-prober.h> 15157ce8f3SChen-Yu Tsai #include <linux/module.h> 16157ce8f3SChen-Yu Tsai #include <linux/of.h> 17*89726114SChen-Yu Tsai #include <linux/regulator/consumer.h> 18157ce8f3SChen-Yu Tsai #include <linux/slab.h> 19157ce8f3SChen-Yu Tsai #include <linux/stddef.h> 20157ce8f3SChen-Yu Tsai 21157ce8f3SChen-Yu Tsai /* 22157ce8f3SChen-Yu Tsai * Some devices, such as Google Hana Chromebooks, are produced by multiple 23157ce8f3SChen-Yu Tsai * vendors each using their preferred components. Such components are all 24157ce8f3SChen-Yu Tsai * in the device tree. Instead of having all of them enabled and having each 25157ce8f3SChen-Yu Tsai * driver separately try and probe its device while fighting over shared 26157ce8f3SChen-Yu Tsai * resources, they can be marked as "fail-needs-probe" and have a prober 27157ce8f3SChen-Yu Tsai * figure out which one is actually used beforehand. 28157ce8f3SChen-Yu Tsai * 29157ce8f3SChen-Yu Tsai * This prober assumes such drop-in parts are on the same I2C bus, have 30157ce8f3SChen-Yu Tsai * non-conflicting addresses, and can be directly probed by seeing which 31157ce8f3SChen-Yu Tsai * address responds. 32157ce8f3SChen-Yu Tsai * 33157ce8f3SChen-Yu Tsai * TODO: 34157ce8f3SChen-Yu Tsai * - Support handling common GPIOs. 35157ce8f3SChen-Yu Tsai * - Support I2C muxes 36157ce8f3SChen-Yu Tsai */ 37157ce8f3SChen-Yu Tsai 38157ce8f3SChen-Yu Tsai static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type) 39157ce8f3SChen-Yu Tsai { 40157ce8f3SChen-Yu Tsai struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type); 41157ce8f3SChen-Yu Tsai if (!node) { 42157ce8f3SChen-Yu Tsai dev_err(dev, "Could not find %s device node\n", type); 43157ce8f3SChen-Yu Tsai return NULL; 44157ce8f3SChen-Yu Tsai } 45157ce8f3SChen-Yu Tsai 46157ce8f3SChen-Yu Tsai struct device_node *i2c_node __free(device_node) = of_get_parent(node); 47157ce8f3SChen-Yu Tsai if (!of_node_name_eq(i2c_node, "i2c")) { 48157ce8f3SChen-Yu Tsai dev_err(dev, "%s device isn't on I2C bus\n", type); 49157ce8f3SChen-Yu Tsai return NULL; 50157ce8f3SChen-Yu Tsai } 51157ce8f3SChen-Yu Tsai 52157ce8f3SChen-Yu Tsai if (!of_device_is_available(i2c_node)) { 53157ce8f3SChen-Yu Tsai dev_err(dev, "I2C controller not available\n"); 54157ce8f3SChen-Yu Tsai return NULL; 55157ce8f3SChen-Yu Tsai } 56157ce8f3SChen-Yu Tsai 57157ce8f3SChen-Yu Tsai return no_free_ptr(i2c_node); 58157ce8f3SChen-Yu Tsai } 59157ce8f3SChen-Yu Tsai 60157ce8f3SChen-Yu Tsai static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) 61157ce8f3SChen-Yu Tsai { 62157ce8f3SChen-Yu Tsai int ret; 63157ce8f3SChen-Yu Tsai 64157ce8f3SChen-Yu Tsai dev_dbg(dev, "Enabling %pOF\n", node); 65157ce8f3SChen-Yu Tsai 66157ce8f3SChen-Yu Tsai struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); 67157ce8f3SChen-Yu Tsai if (!ocs) 68157ce8f3SChen-Yu Tsai return -ENOMEM; 69157ce8f3SChen-Yu Tsai 70157ce8f3SChen-Yu Tsai of_changeset_init(ocs); 71157ce8f3SChen-Yu Tsai ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); 72157ce8f3SChen-Yu Tsai if (ret) 73157ce8f3SChen-Yu Tsai return ret; 74157ce8f3SChen-Yu Tsai 75157ce8f3SChen-Yu Tsai ret = of_changeset_apply(ocs); 76157ce8f3SChen-Yu Tsai if (ret) { 77157ce8f3SChen-Yu Tsai /* ocs needs to be explicitly cleaned up before being freed. */ 78157ce8f3SChen-Yu Tsai of_changeset_destroy(ocs); 79157ce8f3SChen-Yu Tsai } else { 80157ce8f3SChen-Yu Tsai /* 81157ce8f3SChen-Yu Tsai * ocs is intentionally kept around as it needs to 82157ce8f3SChen-Yu Tsai * exist as long as the change is applied. 83157ce8f3SChen-Yu Tsai */ 84157ce8f3SChen-Yu Tsai void *ptr __always_unused = no_free_ptr(ocs); 85157ce8f3SChen-Yu Tsai } 86157ce8f3SChen-Yu Tsai 87157ce8f3SChen-Yu Tsai return ret; 88157ce8f3SChen-Yu Tsai } 89157ce8f3SChen-Yu Tsai 90157ce8f3SChen-Yu Tsai static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops; 91157ce8f3SChen-Yu Tsai 92157ce8f3SChen-Yu Tsai /** 93157ce8f3SChen-Yu Tsai * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus 94157ce8f3SChen-Yu Tsai * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages. 95157ce8f3SChen-Yu Tsai * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options 96157ce8f3SChen-Yu Tsai * for the prober. 97157ce8f3SChen-Yu Tsai * @ctx: Context data for callbacks. 98157ce8f3SChen-Yu Tsai * 99157ce8f3SChen-Yu Tsai * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type) 100157ce8f3SChen-Yu Tsai * on the same I2C bus that have their status marked as "fail-needs-probe". 101157ce8f3SChen-Yu Tsai * 102157ce8f3SChen-Yu Tsai * Assumes that across the entire device tree the only instances of nodes 103157ce8f3SChen-Yu Tsai * with "type" prefixed node names (not including the address portion) are 104157ce8f3SChen-Yu Tsai * the ones that need handling for second source components. In other words, 105157ce8f3SChen-Yu Tsai * if "type" is "touchscreen", then all device nodes named "touchscreen*" 106157ce8f3SChen-Yu Tsai * are the ones that need probing. There cannot be another "touchscreen*" 107157ce8f3SChen-Yu Tsai * node that is already enabled. 108157ce8f3SChen-Yu Tsai * 109157ce8f3SChen-Yu Tsai * Assumes that for each "type" of component, only one actually exists. In 110157ce8f3SChen-Yu Tsai * other words, only one matching and existing device will be enabled. 111157ce8f3SChen-Yu Tsai * 112157ce8f3SChen-Yu Tsai * Context: Process context only. Does non-atomic I2C transfers. 113157ce8f3SChen-Yu Tsai * Should only be used from a driver probe function, as the function 114157ce8f3SChen-Yu Tsai * can return -EPROBE_DEFER if the I2C adapter or other resources 115157ce8f3SChen-Yu Tsai * are unavailable. 116157ce8f3SChen-Yu Tsai * Return: 0 on success or no-op, error code otherwise. 117157ce8f3SChen-Yu Tsai * A no-op can happen when it seems like the device tree already 118157ce8f3SChen-Yu Tsai * has components of the type to be probed already enabled. This 119157ce8f3SChen-Yu Tsai * can happen when the device tree had not been updated to mark 120157ce8f3SChen-Yu Tsai * the status of the to-be-probed components as "fail-needs-probe". 121157ce8f3SChen-Yu Tsai * Or this function was already run with the same parameters and 122157ce8f3SChen-Yu Tsai * succeeded in enabling a component. The latter could happen if 123157ce8f3SChen-Yu Tsai * the user had multiple types of components to probe, and one of 124157ce8f3SChen-Yu Tsai * them down the list caused a deferred probe. This is expected 125157ce8f3SChen-Yu Tsai * behavior. 126157ce8f3SChen-Yu Tsai */ 127157ce8f3SChen-Yu Tsai int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) 128157ce8f3SChen-Yu Tsai { 129157ce8f3SChen-Yu Tsai const struct i2c_of_probe_ops *ops; 130157ce8f3SChen-Yu Tsai const char *type; 131157ce8f3SChen-Yu Tsai struct i2c_adapter *i2c; 132157ce8f3SChen-Yu Tsai int ret; 133157ce8f3SChen-Yu Tsai 134157ce8f3SChen-Yu Tsai ops = cfg->ops ?: &i2c_of_probe_dummy_ops; 135157ce8f3SChen-Yu Tsai type = cfg->type; 136157ce8f3SChen-Yu Tsai 137157ce8f3SChen-Yu Tsai struct device_node *i2c_node __free(device_node) = i2c_of_probe_get_i2c_node(dev, type); 138157ce8f3SChen-Yu Tsai if (!i2c_node) 139157ce8f3SChen-Yu Tsai return -ENODEV; 140157ce8f3SChen-Yu Tsai 141157ce8f3SChen-Yu Tsai /* 142157ce8f3SChen-Yu Tsai * If any devices of the given "type" are already enabled then this function is a no-op. 143157ce8f3SChen-Yu Tsai * Either the device tree hasn't been modified to work with this probe function, or the 144157ce8f3SChen-Yu Tsai * function had already run before and enabled some component. 145157ce8f3SChen-Yu Tsai */ 146157ce8f3SChen-Yu Tsai for_each_child_of_node_with_prefix(i2c_node, node, type) 147157ce8f3SChen-Yu Tsai if (of_device_is_available(node)) 148157ce8f3SChen-Yu Tsai return 0; 149157ce8f3SChen-Yu Tsai 150157ce8f3SChen-Yu Tsai i2c = of_get_i2c_adapter_by_node(i2c_node); 151157ce8f3SChen-Yu Tsai if (!i2c) 152157ce8f3SChen-Yu Tsai return dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); 153157ce8f3SChen-Yu Tsai 154157ce8f3SChen-Yu Tsai /* Grab and enable resources */ 155157ce8f3SChen-Yu Tsai ret = 0; 156157ce8f3SChen-Yu Tsai if (ops->enable) 157157ce8f3SChen-Yu Tsai ret = ops->enable(dev, i2c_node, ctx); 158157ce8f3SChen-Yu Tsai if (ret) 159157ce8f3SChen-Yu Tsai goto out_put_i2c_adapter; 160157ce8f3SChen-Yu Tsai 161157ce8f3SChen-Yu Tsai for_each_child_of_node_with_prefix(i2c_node, node, type) { 162157ce8f3SChen-Yu Tsai union i2c_smbus_data data; 163157ce8f3SChen-Yu Tsai u32 addr; 164157ce8f3SChen-Yu Tsai 165157ce8f3SChen-Yu Tsai if (of_property_read_u32(node, "reg", &addr)) 166157ce8f3SChen-Yu Tsai continue; 167157ce8f3SChen-Yu Tsai if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) 168157ce8f3SChen-Yu Tsai continue; 169157ce8f3SChen-Yu Tsai 170157ce8f3SChen-Yu Tsai /* Found a device that is responding */ 171157ce8f3SChen-Yu Tsai if (ops->cleanup_early) 172157ce8f3SChen-Yu Tsai ops->cleanup_early(dev, ctx); 173157ce8f3SChen-Yu Tsai ret = i2c_of_probe_enable_node(dev, node); 174157ce8f3SChen-Yu Tsai break; 175157ce8f3SChen-Yu Tsai } 176157ce8f3SChen-Yu Tsai 177157ce8f3SChen-Yu Tsai if (ops->cleanup) 178157ce8f3SChen-Yu Tsai ops->cleanup(dev, ctx); 179157ce8f3SChen-Yu Tsai out_put_i2c_adapter: 180157ce8f3SChen-Yu Tsai i2c_put_adapter(i2c); 181157ce8f3SChen-Yu Tsai 182157ce8f3SChen-Yu Tsai return ret; 183157ce8f3SChen-Yu Tsai } 184157ce8f3SChen-Yu Tsai EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); 185*89726114SChen-Yu Tsai 186*89726114SChen-Yu Tsai static int i2c_of_probe_simple_get_supply(struct device *dev, struct device_node *node, 187*89726114SChen-Yu Tsai struct i2c_of_probe_simple_ctx *ctx) 188*89726114SChen-Yu Tsai { 189*89726114SChen-Yu Tsai const char *supply_name; 190*89726114SChen-Yu Tsai struct regulator *supply; 191*89726114SChen-Yu Tsai 192*89726114SChen-Yu Tsai /* 193*89726114SChen-Yu Tsai * It's entirely possible for the component's device node to not have the 194*89726114SChen-Yu Tsai * regulator supplies. While it does not make sense from a hardware perspective, 195*89726114SChen-Yu Tsai * the supplies could be always on or otherwise not modeled in the device tree, 196*89726114SChen-Yu Tsai * but the device would still work. 197*89726114SChen-Yu Tsai */ 198*89726114SChen-Yu Tsai supply_name = ctx->opts->supply_name; 199*89726114SChen-Yu Tsai if (!supply_name) 200*89726114SChen-Yu Tsai return 0; 201*89726114SChen-Yu Tsai 202*89726114SChen-Yu Tsai supply = of_regulator_get_optional(dev, node, supply_name); 203*89726114SChen-Yu Tsai if (IS_ERR(supply)) { 204*89726114SChen-Yu Tsai return dev_err_probe(dev, PTR_ERR(supply), 205*89726114SChen-Yu Tsai "Failed to get regulator supply \"%s\" from %pOF\n", 206*89726114SChen-Yu Tsai supply_name, node); 207*89726114SChen-Yu Tsai } 208*89726114SChen-Yu Tsai 209*89726114SChen-Yu Tsai ctx->supply = supply; 210*89726114SChen-Yu Tsai 211*89726114SChen-Yu Tsai return 0; 212*89726114SChen-Yu Tsai } 213*89726114SChen-Yu Tsai 214*89726114SChen-Yu Tsai static void i2c_of_probe_simple_put_supply(struct i2c_of_probe_simple_ctx *ctx) 215*89726114SChen-Yu Tsai { 216*89726114SChen-Yu Tsai regulator_put(ctx->supply); 217*89726114SChen-Yu Tsai ctx->supply = NULL; 218*89726114SChen-Yu Tsai } 219*89726114SChen-Yu Tsai 220*89726114SChen-Yu Tsai static int i2c_of_probe_simple_enable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx) 221*89726114SChen-Yu Tsai { 222*89726114SChen-Yu Tsai int ret; 223*89726114SChen-Yu Tsai 224*89726114SChen-Yu Tsai if (!ctx->supply) 225*89726114SChen-Yu Tsai return 0; 226*89726114SChen-Yu Tsai 227*89726114SChen-Yu Tsai dev_dbg(dev, "Enabling regulator supply \"%s\"\n", ctx->opts->supply_name); 228*89726114SChen-Yu Tsai 229*89726114SChen-Yu Tsai ret = regulator_enable(ctx->supply); 230*89726114SChen-Yu Tsai if (ret) 231*89726114SChen-Yu Tsai return ret; 232*89726114SChen-Yu Tsai 233*89726114SChen-Yu Tsai if (ctx->opts->post_power_on_delay_ms) 234*89726114SChen-Yu Tsai msleep(ctx->opts->post_power_on_delay_ms); 235*89726114SChen-Yu Tsai 236*89726114SChen-Yu Tsai return 0; 237*89726114SChen-Yu Tsai } 238*89726114SChen-Yu Tsai 239*89726114SChen-Yu Tsai static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c_of_probe_simple_ctx *ctx) 240*89726114SChen-Yu Tsai { 241*89726114SChen-Yu Tsai if (!ctx->supply) 242*89726114SChen-Yu Tsai return; 243*89726114SChen-Yu Tsai 244*89726114SChen-Yu Tsai dev_dbg(dev, "Disabling regulator supply \"%s\"\n", ctx->opts->supply_name); 245*89726114SChen-Yu Tsai 246*89726114SChen-Yu Tsai regulator_disable(ctx->supply); 247*89726114SChen-Yu Tsai } 248*89726114SChen-Yu Tsai 249*89726114SChen-Yu Tsai /** 250*89726114SChen-Yu Tsai * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources 251*89726114SChen-Yu Tsai * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages 252*89726114SChen-Yu Tsai * @bus_node: Pointer to the &struct device_node of the I2C adapter. 253*89726114SChen-Yu Tsai * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 254*89726114SChen-Yu Tsai * 255*89726114SChen-Yu Tsai * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply. 256*89726114SChen-Yu Tsai * If a regulator supply was found, enable that regulator. 257*89726114SChen-Yu Tsai * 258*89726114SChen-Yu Tsai * Return: %0 on success or no-op, or a negative error number on failure. 259*89726114SChen-Yu Tsai */ 260*89726114SChen-Yu Tsai int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data) 261*89726114SChen-Yu Tsai { 262*89726114SChen-Yu Tsai struct i2c_of_probe_simple_ctx *ctx = data; 263*89726114SChen-Yu Tsai struct device_node *node; 264*89726114SChen-Yu Tsai const char *compat; 265*89726114SChen-Yu Tsai int ret; 266*89726114SChen-Yu Tsai 267*89726114SChen-Yu Tsai dev_dbg(dev, "Requesting resources for components under I2C bus %pOF\n", bus_node); 268*89726114SChen-Yu Tsai 269*89726114SChen-Yu Tsai if (!ctx || !ctx->opts) 270*89726114SChen-Yu Tsai return -EINVAL; 271*89726114SChen-Yu Tsai 272*89726114SChen-Yu Tsai compat = ctx->opts->res_node_compatible; 273*89726114SChen-Yu Tsai if (!compat) 274*89726114SChen-Yu Tsai return -EINVAL; 275*89726114SChen-Yu Tsai 276*89726114SChen-Yu Tsai node = of_get_compatible_child(bus_node, compat); 277*89726114SChen-Yu Tsai if (!node) 278*89726114SChen-Yu Tsai return dev_err_probe(dev, -ENODEV, "No device compatible with \"%s\" found\n", 279*89726114SChen-Yu Tsai compat); 280*89726114SChen-Yu Tsai 281*89726114SChen-Yu Tsai ret = i2c_of_probe_simple_get_supply(dev, node, ctx); 282*89726114SChen-Yu Tsai if (ret) 283*89726114SChen-Yu Tsai goto out_put_node; 284*89726114SChen-Yu Tsai 285*89726114SChen-Yu Tsai ret = i2c_of_probe_simple_enable_regulator(dev, ctx); 286*89726114SChen-Yu Tsai if (ret) 287*89726114SChen-Yu Tsai goto out_put_supply; 288*89726114SChen-Yu Tsai 289*89726114SChen-Yu Tsai return 0; 290*89726114SChen-Yu Tsai 291*89726114SChen-Yu Tsai out_put_supply: 292*89726114SChen-Yu Tsai i2c_of_probe_simple_put_supply(ctx); 293*89726114SChen-Yu Tsai out_put_node: 294*89726114SChen-Yu Tsai of_node_put(node); 295*89726114SChen-Yu Tsai return ret; 296*89726114SChen-Yu Tsai } 297*89726114SChen-Yu Tsai EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); 298*89726114SChen-Yu Tsai 299*89726114SChen-Yu Tsai /** 300*89726114SChen-Yu Tsai * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers 301*89726114SChen-Yu Tsai * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages 302*89726114SChen-Yu Tsai * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 303*89726114SChen-Yu Tsai * 304*89726114SChen-Yu Tsai * * If a regulator supply was found, disable that regulator and release it. 305*89726114SChen-Yu Tsai */ 306*89726114SChen-Yu Tsai void i2c_of_probe_simple_cleanup(struct device *dev, void *data) 307*89726114SChen-Yu Tsai { 308*89726114SChen-Yu Tsai struct i2c_of_probe_simple_ctx *ctx = data; 309*89726114SChen-Yu Tsai 310*89726114SChen-Yu Tsai i2c_of_probe_simple_disable_regulator(dev, ctx); 311*89726114SChen-Yu Tsai i2c_of_probe_simple_put_supply(ctx); 312*89726114SChen-Yu Tsai } 313*89726114SChen-Yu Tsai EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_OF_PROBER); 314*89726114SChen-Yu Tsai 315*89726114SChen-Yu Tsai struct i2c_of_probe_ops i2c_of_probe_simple_ops = { 316*89726114SChen-Yu Tsai .enable = i2c_of_probe_simple_enable, 317*89726114SChen-Yu Tsai .cleanup = i2c_of_probe_simple_cleanup, 318*89726114SChen-Yu Tsai }; 319*89726114SChen-Yu Tsai EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER); 320