Message ID | 20240911072751.365361-7-wenst@chromium.org (mailing list archive) |
---|---|
State | Superseded, archived |
Headers | show |
Series | platform/chrome: Introduce DT hardware prober | expand |
On Wed, Sep 11, 2024 at 03:27:44PM +0800, Chen-Yu Tsai wrote: > Some devices are designed and manufactured with some components having > multiple drop-in replacement options. These components are often > connected to the mainboard via ribbon cables, having the same signals > and pin assignments across all options. These may include the display > panel and touchscreen on laptops and tablets, and the trackpad on > laptops. Sometimes which component option is used in a particular device > can be detected by some firmware provided identifier, other times that > information is not available, and the kernel has to try to probe each > device. > > This change attempts to make the "probe each device" case cleaner. The > current approach is to have all options added and enabled in the device > tree. The kernel would then bind each device and run each driver's probe > function. This works, but has been broken before due to the introduction > of asynchronous probing, causing multiple instances requesting "shared" > resources, such as pinmuxes, GPIO pins, interrupt lines, at the same > time, with only one instance succeeding. Work arounds for these include > moving the pinmux to the parent I2C controller, using GPIO hogs or > pinmux settings to keep the GPIO pins in some fixed configuration, and > requesting the interrupt line very late. Such configurations can be seen > on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based > Lenovo Thinkpad 13S. > > Instead of this delicate dance between drivers and device tree quirks, > this change introduces a simple I2C component probe. function For a > given class of devices on the same I2C bus, it will go through all of > them, doing a simple I2C read transfer and see which one of them responds. > It will then enable the device that responds. > > This requires some minor modifications in the existing device tree. The > status for all the device nodes for the component options must be set > to "failed-needs-probe". This makes it clear that some mechanism is > needed to enable one of them, and also prevents the prober and device > drivers running at the same time. ... > +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) > +{ > + int ret; > + dev_info(dev, "Enabling %pOF\n", node); Is it important to be on INFO level? > + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); > + if (!ocs) > + return -ENOMEM; > + > + of_changeset_init(ocs); > + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); > + if (ret) > + return ret; > + > + ret = of_changeset_apply(ocs); > + if (ret) { > + /* ocs needs to be explicitly cleaned up before being freed. */ > + of_changeset_destroy(ocs); > + } else { > + /* > + * ocs is intentionally kept around as it needs to > + * exist as long as the change is applied. > + */ > + void *ptr __always_unused = no_free_ptr(ocs); > + } > + > + return ret; > +} ... > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > +{ > + const struct i2c_of_probe_ops *ops; > + const char *type; > + struct device_node *i2c_node; > + struct i2c_adapter *i2c; > + int ret; > + > + if (!cfg) > + return -EINVAL; > + > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > + type = cfg->type; > + > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); struct device_node *i2c_node __free(of_node_put) = i2c_...; > + if (IS_ERR(i2c_node)) > + return PTR_ERR(i2c_node); > + > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > + if (!of_device_is_available(node)) > + continue; > + > + /* > + * Device tree has component already enabled. Either the > + * device tree isn't supported or we already probed once. > + */ > + ret = 0; Shouldn't you drop reference count for "node"? (See also below) > + goto out_put_i2c_node; > + } > + > + i2c = of_get_i2c_adapter_by_node(i2c_node); > + if (!i2c) { > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > + goto out_put_i2c_node; > + } > + > + /* Grab resources */ > + ret = 0; > + if (ops->get_resources) > + ret = ops->get_resources(dev, i2c_node, ctx); > + if (ret) > + goto out_put_i2c_adapter; > + > + /* Enable resources */ > + if (ops->enable) > + ret = ops->enable(dev, ctx); > + if (ret) > + goto out_release_resources; > + > + ret = 0; > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > + union i2c_smbus_data data; > + u32 addr; > + > + if (of_property_read_u32(node, "reg", &addr)) > + continue; > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > + continue; > + > + /* Found a device that is responding */ > + if (ops->free_resources_early) > + ops->free_resources_early(ctx); > + ret = i2c_of_probe_enable_node(dev, node); Hmm... Is "node" reference count left bumped up for a reason? > + break; > + } > + > + if (ops->cleanup) > + ops->cleanup(dev, ctx); > +out_release_resources: > + if (ops->free_resources_late) > + ops->free_resources_late(ctx); > +out_put_i2c_adapter: > + i2c_put_adapter(i2c); > +out_put_i2c_node: > + of_node_put(i2c_node); > + > + return ret; > +} ... > +/* > + * i2c-of-prober.h - definitions for the Linux I2C OF component prober Please avoid putting filenames inside files. In the possible future event of file renaming this may become a burden and sometimes even forgotten. > + * Copyright (C) 2024 Google LLC > + */ > + > +#ifndef _LINUX_I2C_OF_PROBER_H > +#define _LINUX_I2C_OF_PROBER_H > +#if IS_ENABLED(CONFIG_OF_DYNAMIC) Do you really need to hide data types with this? Wouldn't be enough to hide APIs only? > +struct device; > +struct device_node; > + > +/** > + * struct i2c_of_probe_ops - I2C OF component prober callbacks > + * > + * A set of callbacks to be used by i2c_of_probe_component(). > + * > + * All callbacks are optional. Callbacks are called only once per run, and are > + * used in the order they are defined in this structure. > + * > + * All callbacks that have return values shall return %0 on success, > + * or a negative error number on failure. > + * > + * The @dev parameter passed to the callbacks is the same as @dev passed to > + * i2c_of_probe_component(). It should only be used for dev_printk() calls > + * and nothing else, especially not managed device resource (devres) APIs. > + */ > +struct i2c_of_probe_ops { > + /** @get_resources: Retrieve resources for components. */ > + int (*get_resources)(struct device *dev, struct device_node *bus_node, void *data); > + > + /** @free_resources_early: Release exclusive resources prior to enabling component. */ > + void (*free_resources_early)(void *data); > + > + /** > + * @enable: Enable resources so that the components respond to probes. > + * > + * Resources should be reverted to their initial state before returning if this fails. > + */ > + int (*enable)(struct device *dev, void *data); > + > + /** > + * @cleanup: Opposite of @enable to balance refcounts after probing. > + * > + * Can not operate on resources already freed in @free_resources_early. > + */ > + int (*cleanup)(struct device *dev, void *data); > + > + /** > + * @free_resources_late: Release all resources, including those that would have > + * been released by @free_resources_early. > + */ > + void (*free_resources_late)(void *data); > +}; > + > +/** > + * struct i2c_of_probe_cfg - I2C OF component prober configuration > + * @ops: Callbacks for the prober to use. > + * @type: A string to match the device node name prefix to probe for. > + */ > +struct i2c_of_probe_cfg { > + const struct i2c_of_probe_ops *ops; > + const char *type; > +}; > + > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx); > + > +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ > + > +#endif /* _LINUX_I2C_OF_PROBER_H */
Hi, On Wed, Sep 11, 2024 at 12:28 AM Chen-Yu Tsai <wenst@chromium.org> wrote: > > Some devices are designed and manufactured with some components having > multiple drop-in replacement options. These components are often > connected to the mainboard via ribbon cables, having the same signals > and pin assignments across all options. These may include the display > panel and touchscreen on laptops and tablets, and the trackpad on > laptops. Sometimes which component option is used in a particular device > can be detected by some firmware provided identifier, other times that > information is not available, and the kernel has to try to probe each > device. > > This change attempts to make the "probe each device" case cleaner. The > current approach is to have all options added and enabled in the device > tree. The kernel would then bind each device and run each driver's probe > function. This works, but has been broken before due to the introduction > of asynchronous probing, causing multiple instances requesting "shared" > resources, such as pinmuxes, GPIO pins, interrupt lines, at the same > time, with only one instance succeeding. Work arounds for these include > moving the pinmux to the parent I2C controller, using GPIO hogs or > pinmux settings to keep the GPIO pins in some fixed configuration, and > requesting the interrupt line very late. Such configurations can be seen > on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based > Lenovo Thinkpad 13S. > > Instead of this delicate dance between drivers and device tree quirks, > this change introduces a simple I2C component probe. function For a s/probe. function/probe function./ > +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) > +{ > + int ret; > + > + dev_info(dev, "Enabling %pOF\n", node); > + > + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); > + if (!ocs) > + return -ENOMEM; I guess the kernel lets you mix code and declarations now? I'm still used to all declarations being together but maybe I'm old school... I would have put the "dev_info" below the allocation... > +/** > + * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus > + * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages. > + * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options > + * for the prober. > + * @ctx: Context data for callbacks. > + * > + * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type) > + * on the same I2C bus that have their status marked as "fail". I may have missed it, but originally this was ones marked "fail-needs-probe", right? Now it tries all types of fail? > + * Assumes that across the entire device tree the only instances of nodes > + * prefixed with "type" are the ones that need handling for second source > + * components. In other words, if "type" is "touchscreen", then all device > + * nodes named "touchscreen*" are the ones that need probing. There cannot "touchscreen*" implies that it can have an arbitrary suffix. Can it? ...or can it just have a unit address? > + * be another "touchscreen" node that is already enabled. > + * > + * Assumes that for each "type" of component, only one actually exists. In > + * other words, only one matching and existing device will be enabled. > + * > + * Context: Process context only. Does non-atomic I2C transfers. > + * Should only be used from a driver probe function, as the function > + * can return -EPROBE_DEFER if the I2C adapter or other resources > + * are unavailable. > + * Return: 0 on success or no-op, error code otherwise. > + * A no-op can happen when it seems like the device tree already > + * has components of the type to be probed already enabled. This > + * can happen when the device tree had not been updated to mark > + * the status of the to-be-probed components as "fail". Or this > + * function was already run with the same parameters and succeeded > + * in enabling a component. The latter could happen if the user s/latter/later > + * had multiple types of components to probe, and one of them down > + * the list caused a deferred probe. This is expected behavior. > + */ > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > +{ > + const struct i2c_of_probe_ops *ops; > + const char *type; > + struct device_node *i2c_node; > + struct i2c_adapter *i2c; > + int ret; > + > + if (!cfg) > + return -EINVAL; Drop extra check of "!cfg". In general kernel conventions don't check for NULL pointers passed by caller unless it's an expected case. You don't check for a NULL "dev" and you shouldn't need to check for a NULL "cfg". They are both simply required parameters. There are a few other places in the patch series where it feels like there are extra arg checks that aren't really needed... > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > + type = cfg->type; > + > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > + if (IS_ERR(i2c_node)) > + return PTR_ERR(i2c_node); > + > + for_each_child_of_node_with_prefix(i2c_node, node, type) { I wouldn't object to a comment before this for loop: /* If any devices of this type are already enabled then the function is a noop */ ...or it could be a helper function. > + if (!of_device_is_available(node)) > + continue; > + > + /* > + * Device tree has component already enabled. Either the > + * device tree isn't supported or we already probed once. > + */ > + ret = 0; > + goto out_put_i2c_node; > + } > + > + i2c = of_get_i2c_adapter_by_node(i2c_node); > + if (!i2c) { > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > + goto out_put_i2c_node; > + } > + > + /* Grab resources */ > + ret = 0; > + if (ops->get_resources) > + ret = ops->get_resources(dev, i2c_node, ctx); > + if (ret) > + goto out_put_i2c_adapter; > + > + /* Enable resources */ > + if (ops->enable) > + ret = ops->enable(dev, ctx); > + if (ret) > + goto out_release_resources; I won't insist, but a part of me wonders whether we should just combine "get_resources" and "enable" and then combine "cleanup" and "free_resources_late". They are always paired one after another and I'm having a hard time seeing why they need to be separate. It's not like you'll ever get the resources and then enable/disable multiple times. > +/** > + * struct i2c_of_probe_ops - I2C OF component prober callbacks > + * > + * A set of callbacks to be used by i2c_of_probe_component(). > + * > + * All callbacks are optional. Callbacks are called only once per run, and are > + * used in the order they are defined in this structure. > + * > + * All callbacks that have return values shall return %0 on success, > + * or a negative error number on failure. > + * > + * The @dev parameter passed to the callbacks is the same as @dev passed to > + * i2c_of_probe_component(). It should only be used for dev_printk() calls > + * and nothing else, especially not managed device resource (devres) APIs. > + */ > +struct i2c_of_probe_ops { > + /** @get_resources: Retrieve resources for components. */ > + int (*get_resources)(struct device *dev, struct device_node *bus_node, void *data); > + > + /** @free_resources_early: Release exclusive resources prior to enabling component. */ > + void (*free_resources_early)(void *data); It would be good if the doc here mentioned what happened if no components were found and thus nothing was enabled. Is the function still called? It looks like "no" and "cleanup" is in charge of cleaning in this case. Feels like the docs need to be more explicit.
On 24-09-11 15:27, Chen-Yu Tsai wrote: > Some devices are designed and manufactured with some components having > multiple drop-in replacement options. These components are often > connected to the mainboard via ribbon cables, having the same signals > and pin assignments across all options. These may include the display > panel and touchscreen on laptops and tablets, and the trackpad on > laptops. Sometimes which component option is used in a particular device > can be detected by some firmware provided identifier, other times that > information is not available, and the kernel has to try to probe each > device. > > This change attempts to make the "probe each device" case cleaner. The > current approach is to have all options added and enabled in the device > tree. The kernel would then bind each device and run each driver's probe > function. This works, but has been broken before due to the introduction > of asynchronous probing, causing multiple instances requesting "shared" > resources, such as pinmuxes, GPIO pins, interrupt lines, at the same > time, with only one instance succeeding. Work arounds for these include > moving the pinmux to the parent I2C controller, using GPIO hogs or > pinmux settings to keep the GPIO pins in some fixed configuration, and > requesting the interrupt line very late. Such configurations can be seen > on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based > Lenovo Thinkpad 13S. > > Instead of this delicate dance between drivers and device tree quirks, > this change introduces a simple I2C component probe. function For a > given class of devices on the same I2C bus, it will go through all of > them, doing a simple I2C read transfer and see which one of them responds. > It will then enable the device that responds. > > This requires some minor modifications in the existing device tree. The > status for all the device nodes for the component options must be set > to "failed-needs-probe". This makes it clear that some mechanism is Wrong status name ("failed-needs-probe"), "fail-needs-probe". This is minor, but it confused me as I went through patchset first time, since there are different name in different patches. > needed to enable one of them, and also prevents the prober and device > drivers running at the same time. > > Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
On Fri, Sep 13, 2024 at 12:25 PM Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > > On Wed, Sep 11, 2024 at 03:27:44PM +0800, Chen-Yu Tsai wrote: > > Some devices are designed and manufactured with some components having > > multiple drop-in replacement options. These components are often > > connected to the mainboard via ribbon cables, having the same signals > > and pin assignments across all options. These may include the display > > panel and touchscreen on laptops and tablets, and the trackpad on > > laptops. Sometimes which component option is used in a particular device > > can be detected by some firmware provided identifier, other times that > > information is not available, and the kernel has to try to probe each > > device. > > > > This change attempts to make the "probe each device" case cleaner. The > > current approach is to have all options added and enabled in the device > > tree. The kernel would then bind each device and run each driver's probe > > function. This works, but has been broken before due to the introduction > > of asynchronous probing, causing multiple instances requesting "shared" > > resources, such as pinmuxes, GPIO pins, interrupt lines, at the same > > time, with only one instance succeeding. Work arounds for these include > > moving the pinmux to the parent I2C controller, using GPIO hogs or > > pinmux settings to keep the GPIO pins in some fixed configuration, and > > requesting the interrupt line very late. Such configurations can be seen > > on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based > > Lenovo Thinkpad 13S. > > > > Instead of this delicate dance between drivers and device tree quirks, > > this change introduces a simple I2C component probe. function For a > > given class of devices on the same I2C bus, it will go through all of > > them, doing a simple I2C read transfer and see which one of them responds. > > It will then enable the device that responds. > > > > This requires some minor modifications in the existing device tree. The > > status for all the device nodes for the component options must be set > > to "failed-needs-probe". This makes it clear that some mechanism is > > needed to enable one of them, and also prevents the prober and device > > drivers running at the same time. > > ... > > > +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) > > +{ > > + int ret; > > > + dev_info(dev, "Enabling %pOF\n", node); > > Is it important to be on INFO level? Not really. > > + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); > > + if (!ocs) > > + return -ENOMEM; > > + > > + of_changeset_init(ocs); > > + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); > > + if (ret) > > + return ret; > > + > > + ret = of_changeset_apply(ocs); > > + if (ret) { > > + /* ocs needs to be explicitly cleaned up before being freed. */ > > + of_changeset_destroy(ocs); > > + } else { > > + /* > > + * ocs is intentionally kept around as it needs to > > + * exist as long as the change is applied. > > + */ > > + void *ptr __always_unused = no_free_ptr(ocs); > > + } > > + > > + return ret; > > +} > > ... > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > +{ > > + const struct i2c_of_probe_ops *ops; > > + const char *type; > > + struct device_node *i2c_node; > > + struct i2c_adapter *i2c; > > + int ret; > > + > > + if (!cfg) > > + return -EINVAL; > > + > > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > > + type = cfg->type; > > + > > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > > > struct device_node *i2c_node __free(of_node_put) = > i2c_...; cleanup.h says to not mix the two styles (scoped vs goto). I was trying to follow that, though I realize now that with the scoped loops it probably doesn't help. I'll revert back to having __free(). > > + if (IS_ERR(i2c_node)) > > + return PTR_ERR(i2c_node); > > + > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > + if (!of_device_is_available(node)) > > + continue; > > + > > + /* > > + * Device tree has component already enabled. Either the > > + * device tree isn't supported or we already probed once. > > + */ > > + ret = 0; > > Shouldn't you drop reference count for "node"? (See also below) This for-each loop the "scoped". It just doesn't have the prefix anymore. I believe you asked if the prefix could be dropped and then Rob agreed. > > + goto out_put_i2c_node; > > + } > > + > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > + if (!i2c) { > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > + goto out_put_i2c_node; > > + } > > + > > + /* Grab resources */ > > + ret = 0; > > + if (ops->get_resources) > > + ret = ops->get_resources(dev, i2c_node, ctx); > > + if (ret) > > + goto out_put_i2c_adapter; > > + > > + /* Enable resources */ > > + if (ops->enable) > > + ret = ops->enable(dev, ctx); > > + if (ret) > > + goto out_release_resources; > > + > > + ret = 0; > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > + union i2c_smbus_data data; > > + u32 addr; > > + > > + if (of_property_read_u32(node, "reg", &addr)) > > + continue; > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > + continue; > > + > > + /* Found a device that is responding */ > > + if (ops->free_resources_early) > > + ops->free_resources_early(ctx); > > + ret = i2c_of_probe_enable_node(dev, node); > > Hmm... Is "node" reference count left bumped up for a reason? Same as above. > > + break; > > + } > > + > > + if (ops->cleanup) > > + ops->cleanup(dev, ctx); > > +out_release_resources: > > + if (ops->free_resources_late) > > + ops->free_resources_late(ctx); > > +out_put_i2c_adapter: > > + i2c_put_adapter(i2c); > > +out_put_i2c_node: > > + of_node_put(i2c_node); > > + > > + return ret; > > +} > > ... > > > +/* > > + * i2c-of-prober.h - definitions for the Linux I2C OF component prober > > Please avoid putting filenames inside files. In the possible future event of > file renaming this may become a burden and sometimes even forgotten. Ack. > > + * Copyright (C) 2024 Google LLC > > + */ > > + > > +#ifndef _LINUX_I2C_OF_PROBER_H > > +#define _LINUX_I2C_OF_PROBER_H > > > +#if IS_ENABLED(CONFIG_OF_DYNAMIC) > > Do you really need to hide data types with this? Wouldn't be enough to hide > APIs only? Ack. Will move the data types outside. Thanks ChenYu > > +struct device; > > +struct device_node; > > + > > +/** > > + * struct i2c_of_probe_ops - I2C OF component prober callbacks > > + * > > + * A set of callbacks to be used by i2c_of_probe_component(). > > + * > > + * All callbacks are optional. Callbacks are called only once per run, and are > > + * used in the order they are defined in this structure. > > + * > > + * All callbacks that have return values shall return %0 on success, > > + * or a negative error number on failure. > > + * > > + * The @dev parameter passed to the callbacks is the same as @dev passed to > > + * i2c_of_probe_component(). It should only be used for dev_printk() calls > > + * and nothing else, especially not managed device resource (devres) APIs. > > + */ > > +struct i2c_of_probe_ops { > > + /** @get_resources: Retrieve resources for components. */ > > + int (*get_resources)(struct device *dev, struct device_node *bus_node, void *data); > > + > > + /** @free_resources_early: Release exclusive resources prior to enabling component. */ > > + void (*free_resources_early)(void *data); > > + > > + /** > > + * @enable: Enable resources so that the components respond to probes. > > + * > > + * Resources should be reverted to their initial state before returning if this fails. > > + */ > > + int (*enable)(struct device *dev, void *data); > > + > > + /** > > + * @cleanup: Opposite of @enable to balance refcounts after probing. > > + * > > + * Can not operate on resources already freed in @free_resources_early. > > + */ > > + int (*cleanup)(struct device *dev, void *data); > > + > > + /** > > + * @free_resources_late: Release all resources, including those that would have > > + * been released by @free_resources_early. > > + */ > > + void (*free_resources_late)(void *data); > > +}; > > + > > +/** > > + * struct i2c_of_probe_cfg - I2C OF component prober configuration > > + * @ops: Callbacks for the prober to use. > > + * @type: A string to match the device node name prefix to probe for. > > + */ > > +struct i2c_of_probe_cfg { > > + const struct i2c_of_probe_ops *ops; > > + const char *type; > > +}; > > + > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx); > > + > > +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ > > + > > +#endif /* _LINUX_I2C_OF_PROBER_H */ > > -- > With Best Regards, > Andy Shevchenko > >
On Sat, Sep 14, 2024 at 1:44 AM Doug Anderson <dianders@chromium.org> wrote: > > Hi, > > On Wed, Sep 11, 2024 at 12:28 AM Chen-Yu Tsai <wenst@chromium.org> wrote: > > > > Some devices are designed and manufactured with some components having > > multiple drop-in replacement options. These components are often > > connected to the mainboard via ribbon cables, having the same signals > > and pin assignments across all options. These may include the display > > panel and touchscreen on laptops and tablets, and the trackpad on > > laptops. Sometimes which component option is used in a particular device > > can be detected by some firmware provided identifier, other times that > > information is not available, and the kernel has to try to probe each > > device. > > > > This change attempts to make the "probe each device" case cleaner. The > > current approach is to have all options added and enabled in the device > > tree. The kernel would then bind each device and run each driver's probe > > function. This works, but has been broken before due to the introduction > > of asynchronous probing, causing multiple instances requesting "shared" > > resources, such as pinmuxes, GPIO pins, interrupt lines, at the same > > time, with only one instance succeeding. Work arounds for these include > > moving the pinmux to the parent I2C controller, using GPIO hogs or > > pinmux settings to keep the GPIO pins in some fixed configuration, and > > requesting the interrupt line very late. Such configurations can be seen > > on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based > > Lenovo Thinkpad 13S. > > > > Instead of this delicate dance between drivers and device tree quirks, > > this change introduces a simple I2C component probe. function For a > > s/probe. function/probe function./ Ack. > > +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) > > +{ > > + int ret; > > + > > + dev_info(dev, "Enabling %pOF\n", node); > > + > > + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); > > + if (!ocs) > > + return -ENOMEM; > > I guess the kernel lets you mix code and declarations now? I'm still > used to all declarations being together but maybe I'm old school... I > would have put the "dev_info" below the allocation... AFAIK this is an exception. Excerpt from include/linux/cleanup.h: When the unwind order matters it requires that variables be defined mid-function scope rather than at the top of the file. and Given that the "__free(...) = NULL" pattern for variables defined at the top of the function poses this potential interdependency problem the recommendation is to always define and assign variables in one statement and not group variable definitions at the top of the function when __free() is used. > > > +/** > > + * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus > > + * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages. > > + * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options > > + * for the prober. > > + * @ctx: Context data for callbacks. > > + * > > + * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type) > > + * on the same I2C bus that have their status marked as "fail". > > I may have missed it, but originally this was ones marked > "fail-needs-probe", right? Now it tries all types of fail? > > > > + * Assumes that across the entire device tree the only instances of nodes > > + * prefixed with "type" are the ones that need handling for second source > > + * components. In other words, if "type" is "touchscreen", then all device > > + * nodes named "touchscreen*" are the ones that need probing. There cannot > > "touchscreen*" implies that it can have an arbitrary suffix. Can it? That is the idea. The use case is for components that have conflicting addresses and need special probing. Such device nodes obviously can't have the same node name. This is planned but not implemented in this series. > ...or can it just have a unit address? IIUC in DT jargon the "node name" does not include the address. The name including the address is the "full name". > > + * be another "touchscreen" node that is already enabled. > > + * > > + * Assumes that for each "type" of component, only one actually exists. In > > + * other words, only one matching and existing device will be enabled. > > + * > > + * Context: Process context only. Does non-atomic I2C transfers. > > + * Should only be used from a driver probe function, as the function > > + * can return -EPROBE_DEFER if the I2C adapter or other resources > > + * are unavailable. > > + * Return: 0 on success or no-op, error code otherwise. > > + * A no-op can happen when it seems like the device tree already > > + * has components of the type to be probed already enabled. This > > + * can happen when the device tree had not been updated to mark > > + * the status of the to-be-probed components as "fail". Or this > > + * function was already run with the same parameters and succeeded > > + * in enabling a component. The latter could happen if the user > > s/latter/later Are you sure? > > + * had multiple types of components to probe, and one of them down > > + * the list caused a deferred probe. This is expected behavior. > > + */ > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > +{ > > + const struct i2c_of_probe_ops *ops; > > + const char *type; > > + struct device_node *i2c_node; > > + struct i2c_adapter *i2c; > > + int ret; > > + > > + if (!cfg) > > + return -EINVAL; > > Drop extra check of "!cfg". In general kernel conventions don't check > for NULL pointers passed by caller unless it's an expected case. You > don't check for a NULL "dev" and you shouldn't need to check for a > NULL "cfg". They are both simply required parameters. "dev" is only passed to dev_printk(), and that can handle "dev" being NULL. Same can't be said for "cfg". I don't know what the preference is though. Crashing is probably not the nicest thing, even if it only happens to developers. > There are a few other places in the patch series where it feels like > there are extra arg checks that aren't really needed... > > > > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > > + type = cfg->type; > > + > > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > > + if (IS_ERR(i2c_node)) > > + return PTR_ERR(i2c_node); > > + > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > I wouldn't object to a comment before this for loop: > > /* If any devices of this type are already enabled then the function > is a noop */ > > ...or it could be a helper function. That is what the commeet within the loop is trying to say. I'll move it before the loop and incorporate your words. The loop can then be rewritten to return early. > > + if (!of_device_is_available(node)) > > + continue; > > + > > + /* > > + * Device tree has component already enabled. Either the > > + * device tree isn't supported or we already probed once. > > + */ > > + ret = 0; > > + goto out_put_i2c_node; > > + } > > + > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > + if (!i2c) { > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > + goto out_put_i2c_node; > > + } > > + > > + /* Grab resources */ > > + ret = 0; > > + if (ops->get_resources) > > + ret = ops->get_resources(dev, i2c_node, ctx); > > + if (ret) > > + goto out_put_i2c_adapter; > > + > > + /* Enable resources */ > > + if (ops->enable) > > + ret = ops->enable(dev, ctx); > > + if (ret) > > + goto out_release_resources; > > I won't insist, but a part of me wonders whether we should just > combine "get_resources" and "enable" and then combine "cleanup" and > "free_resources_late". They are always paired one after another and > I'm having a hard time seeing why they need to be separate. It's not > like you'll ever get the resources and then enable/disable multiple > times. Maybe. The structure was carried over from the original non-callback version. I think it's easier to reason about if they are kept separate, especially since the outgoing path is slightly different when no working component is found and one of the callbacks ends up not getting called. > > +/** > > + * struct i2c_of_probe_ops - I2C OF component prober callbacks > > + * > > + * A set of callbacks to be used by i2c_of_probe_component(). > > + * > > + * All callbacks are optional. Callbacks are called only once per run, and are > > + * used in the order they are defined in this structure. > > + * > > + * All callbacks that have return values shall return %0 on success, > > + * or a negative error number on failure. > > + * > > + * The @dev parameter passed to the callbacks is the same as @dev passed to > > + * i2c_of_probe_component(). It should only be used for dev_printk() calls > > + * and nothing else, especially not managed device resource (devres) APIs. > > + */ > > +struct i2c_of_probe_ops { > > + /** @get_resources: Retrieve resources for components. */ > > + int (*get_resources)(struct device *dev, struct device_node *bus_node, void *data); > > + > > + /** @free_resources_early: Release exclusive resources prior to enabling component. */ > > + void (*free_resources_early)(void *data); > > It would be good if the doc here mentioned what happened if no > components were found and thus nothing was enabled. Is the function > still called? It looks like "no" and "cleanup" is in charge of > cleaning in this case. Feels like the docs need to be more explicit. Ack. Thanks ChenYu
On Fri, Sep 13, 2024 at 04:43:04PM -0700, Doug Anderson wrote: > On Wed, Sep 11, 2024 at 12:28 AM Chen-Yu Tsai <wenst@chromium.org> wrote: ... > > +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) > > +{ > > + int ret; > > + > > + dev_info(dev, "Enabling %pOF\n", node); > > + > > + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); > > + if (!ocs) > > + return -ENOMEM; > > I guess the kernel lets you mix code and declarations now? I'm still > used to all declarations being together but maybe I'm old school... I > would have put the "dev_info" below the allocation... In general yes, but we don't allow it everywhere for everything, we have two exceptions: 1) for-loops; 2) __free() RAII.
On Sun, Sep 15, 2024 at 12:44:13PM +0200, Chen-Yu Tsai wrote: > On Fri, Sep 13, 2024 at 12:25 PM Andy Shevchenko > <andriy.shevchenko@linux.intel.com> wrote: > > On Wed, Sep 11, 2024 at 03:27:44PM +0800, Chen-Yu Tsai wrote: ... > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > > +{ > > > + const struct i2c_of_probe_ops *ops; > > > + const char *type; > > > + struct device_node *i2c_node; > > > + struct i2c_adapter *i2c; > > > + int ret; > > > + > > > + if (!cfg) > > > + return -EINVAL; > > > + > > > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > > > + type = cfg->type; > > > + > > > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > > > > > > struct device_node *i2c_node __free(of_node_put) = > > i2c_...; > > cleanup.h says to not mix the two styles (scoped vs goto). I was trying > to follow that, though I realize now that with the scoped loops it > probably doesn't help. > > I'll revert back to having __free(). > > > > + if (IS_ERR(i2c_node)) > > > + return PTR_ERR(i2c_node); > > > + > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > + if (!of_device_is_available(node)) > > > + continue; > > > + > > > + /* > > > + * Device tree has component already enabled. Either the > > > + * device tree isn't supported or we already probed once. > > > + */ > > > + ret = 0; > > > > Shouldn't you drop reference count for "node"? (See also below) > > This for-each loop the "scoped". It just doesn't have the prefix anymore. > I believe you asked if the prefix could be dropped and then Rob agreed. Hmm... I have looked into the implementation and I haven't found the evidence that this is anyhow scoped. Can you point out what I have missed? > > > + goto out_put_i2c_node; > > > + } > > > + > > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > > + if (!i2c) { > > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > > + goto out_put_i2c_node; > > > + } > > > + > > > + /* Grab resources */ > > > + ret = 0; > > > + if (ops->get_resources) > > > + ret = ops->get_resources(dev, i2c_node, ctx); > > > + if (ret) > > > + goto out_put_i2c_adapter; > > > + > > > + /* Enable resources */ > > > + if (ops->enable) > > > + ret = ops->enable(dev, ctx); > > > + if (ret) > > > + goto out_release_resources; > > > + > > > + ret = 0; > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > + union i2c_smbus_data data; > > > + u32 addr; > > > + > > > + if (of_property_read_u32(node, "reg", &addr)) > > > + continue; > > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > > + continue; > > > + > > > + /* Found a device that is responding */ > > > + if (ops->free_resources_early) > > > + ops->free_resources_early(ctx); > > > + ret = i2c_of_probe_enable_node(dev, node); > > > > Hmm... Is "node" reference count left bumped up for a reason? > > Same as above. Same as above. > > > + break; > > > + } > > > + > > > + if (ops->cleanup) > > > + ops->cleanup(dev, ctx); > > > +out_release_resources: > > > + if (ops->free_resources_late) > > > + ops->free_resources_late(ctx); > > > +out_put_i2c_adapter: > > > + i2c_put_adapter(i2c); > > > +out_put_i2c_node: > > > + of_node_put(i2c_node); > > > + > > > + return ret; > > > +}
On Mon, 16 Sep 2024 13:36:14 +0300 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > On Sun, Sep 15, 2024 at 12:44:13PM +0200, Chen-Yu Tsai wrote: > > On Fri, Sep 13, 2024 at 12:25 PM Andy Shevchenko > > <andriy.shevchenko@linux.intel.com> wrote: > > > On Wed, Sep 11, 2024 at 03:27:44PM +0800, Chen-Yu Tsai wrote: > > ... > > > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > > > +{ > > > > + const struct i2c_of_probe_ops *ops; > > > > + const char *type; > > > > + struct device_node *i2c_node; > > > > + struct i2c_adapter *i2c; > > > > + int ret; > > > > + > > > > + if (!cfg) > > > > + return -EINVAL; > > > > + > > > > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > > > > + type = cfg->type; > > > > + > > > > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > > > > > > > > > struct device_node *i2c_node __free(of_node_put) = > > > i2c_...; > > > > cleanup.h says to not mix the two styles (scoped vs goto). I was trying > > to follow that, though I realize now that with the scoped loops it > > probably doesn't help. The problem pattern is (IIUC) if (x) goto bob; struct device_node *i2c_node __free(of_node_put) = i2c_.... bob: return ret; So a goto that jumps over registration of a cleanup function. Jonathan > > > > I'll revert back to having __free(). > > > > > > + if (IS_ERR(i2c_node)) > > > > + return PTR_ERR(i2c_node); > > > > + > > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > > + if (!of_device_is_available(node)) > > > > + continue; > > > > + > > > > + /* > > > > + * Device tree has component already enabled. Either the > > > > + * device tree isn't supported or we already probed once. > > > > + */ > > > > + ret = 0; > > > > > > Shouldn't you drop reference count for "node"? (See also below) > > > > This for-each loop the "scoped". It just doesn't have the prefix anymore. > > I believe you asked if the prefix could be dropped and then Rob agreed. > > Hmm... I have looked into the implementation and I haven't found the evidence > that this is anyhow scoped. Can you point out what I have missed? > > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > > > + if (!i2c) { > > > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + /* Grab resources */ > > > > + ret = 0; > > > > + if (ops->get_resources) > > > > + ret = ops->get_resources(dev, i2c_node, ctx); > > > > + if (ret) > > > > + goto out_put_i2c_adapter; > > > > + > > > > + /* Enable resources */ > > > > + if (ops->enable) > > > > + ret = ops->enable(dev, ctx); > > > > + if (ret) > > > > + goto out_release_resources; > > > > + > > > > + ret = 0; > > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > > + union i2c_smbus_data data; > > > > + u32 addr; > > > > + > > > > + if (of_property_read_u32(node, "reg", &addr)) > > > > + continue; > > > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > > > + continue; > > > > + > > > > + /* Found a device that is responding */ > > > > + if (ops->free_resources_early) > > > > + ops->free_resources_early(ctx); > > > > + ret = i2c_of_probe_enable_node(dev, node); > > > > > > Hmm... Is "node" reference count left bumped up for a reason? > > > > Same as above. > > Same as above. > > > > > + break; > > > > + } > > > > + > > > > + if (ops->cleanup) > > > > + ops->cleanup(dev, ctx); > > > > +out_release_resources: > > > > + if (ops->free_resources_late) > > > > + ops->free_resources_late(ctx); > > > > +out_put_i2c_adapter: > > > > + i2c_put_adapter(i2c); > > > > +out_put_i2c_node: > > > > + of_node_put(i2c_node); > > > > + > > > > + return ret; > > > > +} >
Hi, On Sun, Sep 15, 2024 at 4:32 AM Chen-Yu Tsai <wenst@chromium.org> wrote: > > > > + * Assumes that across the entire device tree the only instances of nodes > > > + * prefixed with "type" are the ones that need handling for second source > > > + * components. In other words, if "type" is "touchscreen", then all device > > > + * nodes named "touchscreen*" are the ones that need probing. There cannot > > > > "touchscreen*" implies that it can have an arbitrary suffix. Can it? > > That is the idea. The use case is for components that have conflicting > addresses and need special probing. Such device nodes obviously can't > have the same node name. This is planned but not implemented in this > series. Maybe "touchscreen@*" instead of "touchscreen*" if I'm understanding correctly. > > > + * be another "touchscreen" node that is already enabled. > > > + * > > > + * Assumes that for each "type" of component, only one actually exists. In > > > + * other words, only one matching and existing device will be enabled. > > > + * > > > + * Context: Process context only. Does non-atomic I2C transfers. > > > + * Should only be used from a driver probe function, as the function > > > + * can return -EPROBE_DEFER if the I2C adapter or other resources > > > + * are unavailable. > > > + * Return: 0 on success or no-op, error code otherwise. > > > + * A no-op can happen when it seems like the device tree already > > > + * has components of the type to be probed already enabled. This > > > + * can happen when the device tree had not been updated to mark > > > + * the status of the to-be-probed components as "fail". Or this > > > + * function was already run with the same parameters and succeeded > > > + * in enabling a component. The latter could happen if the user > > > > s/latter/later > > Are you sure? No. latter looked weird to me and I searched quickly and thought I was right. With a more full search looks like you're right. > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > > +{ > > > + const struct i2c_of_probe_ops *ops; > > > + const char *type; > > > + struct device_node *i2c_node; > > > + struct i2c_adapter *i2c; > > > + int ret; > > > + > > > + if (!cfg) > > > + return -EINVAL; > > > > Drop extra check of "!cfg". In general kernel conventions don't check > > for NULL pointers passed by caller unless it's an expected case. You > > don't check for a NULL "dev" and you shouldn't need to check for a > > NULL "cfg". They are both simply required parameters. > > "dev" is only passed to dev_printk(), and that can handle "dev" being > NULL. Same can't be said for "cfg". > > I don't know what the preference is though. Crashing is probably not the > nicest thing, even if it only happens to developers. Honestly as a developer I'd prefer the crash. It points out the exact line where I had an invalid NULL. Returning an error code means I've got to compile/boot several more times to track down where the error code is coming from. I'm fairly certain that the kernel convention is to only check things for NULL if it's part of the API to accept NULL or if the value can be NULL due to untrusted data. If the only way it can be NULL is due to buggy code elsewhere in the kernel then you should omit the error checks. > > > + if (!of_device_is_available(node)) > > > + continue; > > > + > > > + /* > > > + * Device tree has component already enabled. Either the > > > + * device tree isn't supported or we already probed once. > > > + */ > > > + ret = 0; > > > + goto out_put_i2c_node; > > > + } > > > + > > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > > + if (!i2c) { > > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > > + goto out_put_i2c_node; > > > + } > > > + > > > + /* Grab resources */ > > > + ret = 0; > > > + if (ops->get_resources) > > > + ret = ops->get_resources(dev, i2c_node, ctx); > > > + if (ret) > > > + goto out_put_i2c_adapter; > > > + > > > + /* Enable resources */ > > > + if (ops->enable) > > > + ret = ops->enable(dev, ctx); > > > + if (ret) > > > + goto out_release_resources; > > > > I won't insist, but a part of me wonders whether we should just > > combine "get_resources" and "enable" and then combine "cleanup" and > > "free_resources_late". They are always paired one after another and > > I'm having a hard time seeing why they need to be separate. It's not > > like you'll ever get the resources and then enable/disable multiple > > times. > > Maybe. The structure was carried over from the original non-callback > version. I think it's easier to reason about if they are kept separate, > especially since the outgoing path is slightly different when no working > component is found and one of the callbacks ends up not getting called. Actually, both of the outgoing callbacks are always called. It's only the 3rd callback (the "early" one) that's called sometimes. I won't insist on combining them, but I still feel like combining them would be better. I'd be interested in other opinions, though. -Doug
On Mon, Sep 16, 2024 at 4:16 PM Doug Anderson <dianders@chromium.org> wrote: > > Hi, > > On Sun, Sep 15, 2024 at 4:32 AM Chen-Yu Tsai <wenst@chromium.org> wrote: > > > > > > + * Assumes that across the entire device tree the only instances of nodes > > > > + * prefixed with "type" are the ones that need handling for second source > > > > + * components. In other words, if "type" is "touchscreen", then all device > > > > + * nodes named "touchscreen*" are the ones that need probing. There cannot > > > > > > "touchscreen*" implies that it can have an arbitrary suffix. Can it? > > > > That is the idea. The use case is for components that have conflicting > > addresses and need special probing. Such device nodes obviously can't > > have the same node name. This is planned but not implemented in this > > series. > > Maybe "touchscreen@*" instead of "touchscreen*" if I'm understanding correctly. Then it would be "touchscreen*@*". > > > > + * be another "touchscreen" node that is already enabled. > > > > + * > > > > + * Assumes that for each "type" of component, only one actually exists. In > > > > + * other words, only one matching and existing device will be enabled. > > > > + * > > > > + * Context: Process context only. Does non-atomic I2C transfers. > > > > + * Should only be used from a driver probe function, as the function > > > > + * can return -EPROBE_DEFER if the I2C adapter or other resources > > > > + * are unavailable. > > > > + * Return: 0 on success or no-op, error code otherwise. > > > > + * A no-op can happen when it seems like the device tree already > > > > + * has components of the type to be probed already enabled. This > > > > + * can happen when the device tree had not been updated to mark > > > > + * the status of the to-be-probed components as "fail". Or this > > > > + * function was already run with the same parameters and succeeded > > > > + * in enabling a component. The latter could happen if the user > > > > > > s/latter/later > > > > Are you sure? > > No. latter looked weird to me and I searched quickly and thought I was > right. With a more full search looks like you're right. > > > > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > > > +{ > > > > + const struct i2c_of_probe_ops *ops; > > > > + const char *type; > > > > + struct device_node *i2c_node; > > > > + struct i2c_adapter *i2c; > > > > + int ret; > > > > + > > > > + if (!cfg) > > > > + return -EINVAL; > > > > > > Drop extra check of "!cfg". In general kernel conventions don't check > > > for NULL pointers passed by caller unless it's an expected case. You > > > don't check for a NULL "dev" and you shouldn't need to check for a > > > NULL "cfg". They are both simply required parameters. > > > > "dev" is only passed to dev_printk(), and that can handle "dev" being > > NULL. Same can't be said for "cfg". > > > > I don't know what the preference is though. Crashing is probably not the > > nicest thing, even if it only happens to developers. > > Honestly as a developer I'd prefer the crash. It points out the exact > line where I had an invalid NULL. Returning an error code means I've > got to compile/boot several more times to track down where the error > code is coming from. > > I'm fairly certain that the kernel convention is to only check things > for NULL if it's part of the API to accept NULL or if the value can be > NULL due to untrusted data. If the only way it can be NULL is due to > buggy code elsewhere in the kernel then you should omit the error > checks. Make sense. > > > > + if (!of_device_is_available(node)) > > > > + continue; > > > > + > > > > + /* > > > > + * Device tree has component already enabled. Either the > > > > + * device tree isn't supported or we already probed once. > > > > + */ > > > > + ret = 0; > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > > > + if (!i2c) { > > > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + /* Grab resources */ > > > > + ret = 0; > > > > + if (ops->get_resources) > > > > + ret = ops->get_resources(dev, i2c_node, ctx); > > > > + if (ret) > > > > + goto out_put_i2c_adapter; > > > > + > > > > + /* Enable resources */ > > > > + if (ops->enable) > > > > + ret = ops->enable(dev, ctx); > > > > + if (ret) > > > > + goto out_release_resources; > > > > > > I won't insist, but a part of me wonders whether we should just > > > combine "get_resources" and "enable" and then combine "cleanup" and > > > "free_resources_late". They are always paired one after another and > > > I'm having a hard time seeing why they need to be separate. It's not > > > like you'll ever get the resources and then enable/disable multiple > > > times. > > > > Maybe. The structure was carried over from the original non-callback > > version. I think it's easier to reason about if they are kept separate, > > especially since the outgoing path is slightly different when no working > > component is found and one of the callbacks ends up not getting called. > > Actually, both of the outgoing callbacks are always called. It's only > the 3rd callback (the "early" one) that's called sometimes. So IIRC do "get + enable" and "cleanup + release_late", and leave "release_early" alone? ChenYu > I won't insist on combining them, but I still feel like combining them > would be better. I'd be interested in other opinions, though. > > > -Doug >
On Mon, Sep 16, 2024 at 12:36 PM Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > > On Sun, Sep 15, 2024 at 12:44:13PM +0200, Chen-Yu Tsai wrote: > > On Fri, Sep 13, 2024 at 12:25 PM Andy Shevchenko > > <andriy.shevchenko@linux.intel.com> wrote: > > > On Wed, Sep 11, 2024 at 03:27:44PM +0800, Chen-Yu Tsai wrote: > > ... > > > > > +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) > > > > +{ > > > > + const struct i2c_of_probe_ops *ops; > > > > + const char *type; > > > > + struct device_node *i2c_node; > > > > + struct i2c_adapter *i2c; > > > > + int ret; > > > > + > > > > + if (!cfg) > > > > + return -EINVAL; > > > > + > > > > + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; > > > > + type = cfg->type; > > > > + > > > > + i2c_node = i2c_of_probe_get_i2c_node(dev, type); > > > > > > > > > struct device_node *i2c_node __free(of_node_put) = > > > i2c_...; > > > > cleanup.h says to not mix the two styles (scoped vs goto). I was trying > > to follow that, though I realize now that with the scoped loops it > > probably doesn't help. > > > > I'll revert back to having __free(). > > > > > > + if (IS_ERR(i2c_node)) > > > > + return PTR_ERR(i2c_node); > > > > + > > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > > + if (!of_device_is_available(node)) > > > > + continue; > > > > + > > > > + /* > > > > + * Device tree has component already enabled. Either the > > > > + * device tree isn't supported or we already probed once. > > > > + */ > > > > + ret = 0; > > > > > > Shouldn't you drop reference count for "node"? (See also below) > > > > This for-each loop the "scoped". It just doesn't have the prefix anymore. > > I believe you asked if the prefix could be dropped and then Rob agreed. > > Hmm... I have looked into the implementation and I haven't found the evidence > that this is anyhow scoped. Can you point out what I have missed? From patch 2: +#define for_each_child_of_node_with_prefix(parent, child, prefix) \ + for (struct device_node *child __free(device_node) = \ ^^^^^^^^^^^^^^^^^^^^^^^^^ scoped here + of_get_next_child_with_prefix(parent, NULL, prefix); \ + child != NULL; \ + child = of_get_next_child_with_prefix(parent, child, prefix)) + "node", or "child" in this snippet is scoped within the for loop. ChenYu > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + i2c = of_get_i2c_adapter_by_node(i2c_node); > > > > + if (!i2c) { > > > > + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); > > > > + goto out_put_i2c_node; > > > > + } > > > > + > > > > + /* Grab resources */ > > > > + ret = 0; > > > > + if (ops->get_resources) > > > > + ret = ops->get_resources(dev, i2c_node, ctx); > > > > + if (ret) > > > > + goto out_put_i2c_adapter; > > > > + > > > > + /* Enable resources */ > > > > + if (ops->enable) > > > > + ret = ops->enable(dev, ctx); > > > > + if (ret) > > > > + goto out_release_resources; > > > > + > > > > + ret = 0; > > > > + for_each_child_of_node_with_prefix(i2c_node, node, type) { > > > > + union i2c_smbus_data data; > > > > + u32 addr; > > > > + > > > > + if (of_property_read_u32(node, "reg", &addr)) > > > > + continue; > > > > + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) > > > > + continue; > > > > + > > > > + /* Found a device that is responding */ > > > > + if (ops->free_resources_early) > > > > + ops->free_resources_early(ctx); > > > > + ret = i2c_of_probe_enable_node(dev, node); > > > > > > Hmm... Is "node" reference count left bumped up for a reason? > > > > Same as above. > > Same as above. > > > > > + break; > > > > + } > > > > + > > > > + if (ops->cleanup) > > > > + ops->cleanup(dev, ctx); > > > > +out_release_resources: > > > > + if (ops->free_resources_late) > > > > + ops->free_resources_late(ctx); > > > > +out_put_i2c_adapter: > > > > + i2c_put_adapter(i2c); > > > > +out_put_i2c_node: > > > > + of_node_put(i2c_node); > > > > + > > > > + return ret; > > > > +} > > -- > With Best Regards, > Andy Shevchenko > >
On Mon, Sep 16, 2024 at 04:59:59PM +0200, Chen-Yu Tsai wrote: > On Mon, Sep 16, 2024 at 12:36 PM Andy Shevchenko > <andriy.shevchenko@linux.intel.com> wrote: > > On Sun, Sep 15, 2024 at 12:44:13PM +0200, Chen-Yu Tsai wrote: ... > > Hmm... I have looked into the implementation and I haven't found the evidence > > that this is anyhow scoped. Can you point out what I have missed? > > From patch 2: > > +#define for_each_child_of_node_with_prefix(parent, child, prefix) \ > + for (struct device_node *child __free(device_node) = \ > > ^^^^^^^^^^^^^^^^^^^^^^^^^ scoped here > > + of_get_next_child_with_prefix(parent, NULL, prefix); \ > + child != NULL; \ > + child = of_get_next_child_with_prefix(parent, child, prefix)) > + > > "node", or "child" in this snippet is scoped within the for loop. Ah, nice, that's what I missed, thanks!
diff --git a/MAINTAINERS b/MAINTAINERS index 927f81c12543..d88646380786 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10645,6 +10645,14 @@ S: Maintained F: Documentation/devicetree/bindings/i2c/marvell,mv64xxx-i2c.yaml F: drivers/i2c/busses/i2c-mv64xxx.c +I2C OF COMPONENT PROBER +M: Chen-Yu Tsai <wenst@chromium.org> +L: linux-i2c@vger.kernel.org +L: devicetree@vger.kernel.org +S: Maintained +F: drivers/i2c/i2c-core-of-prober.c +F: include/linux-i2c-of-prober.h + I2C OVER PARALLEL PORT M: Jean Delvare <jdelvare@suse.com> L: linux-i2c@vger.kernel.org diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index f12d6b10a85e..c539cdc1e305 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile @@ -9,6 +9,7 @@ i2c-core-objs := i2c-core-base.o i2c-core-smbus.o i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o i2c-core-$(CONFIG_OF) += i2c-core-of.o +i2c-core-$(CONFIG_OF_DYNAMIC) += i2c-core-of-prober.o obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o diff --git a/drivers/i2c/i2c-core-of-prober.c b/drivers/i2c/i2c-core-of-prober.c new file mode 100644 index 000000000000..62ff2f4b6177 --- /dev/null +++ b/drivers/i2c/i2c-core-of-prober.c @@ -0,0 +1,195 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Linux I2C core OF component prober code + * + * Copyright (C) 2024 Google LLC + */ + +#include <linux/cleanup.h> +#include <linux/device.h> +#include <linux/dev_printk.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/i2c-of-prober.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/slab.h> + +/* + * Some devices, such as Google Hana Chromebooks, are produced by multiple + * vendors each using their preferred components. Such components are all + * in the device tree. Instead of having all of them enabled and having each + * driver separately try and probe its device while fighting over shared + * resources, they can be marked as "fail-needs-probe" and have a prober + * figure out which one is actually used beforehand. + * + * This prober assumes such drop-in parts are on the same I2C bus, have + * non-conflicting addresses, and can be directly probed by seeing which + * address responds. + * + * TODO: + * - Support handling common regulators. + * - Support handling common GPIOs. + * - Support I2C muxes + */ + +static struct device_node *i2c_of_probe_get_i2c_node(struct device *dev, const char *type) +{ + struct device_node *node __free(device_node) = of_find_node_by_name(NULL, type); + if (!node) + return dev_err_ptr_probe(dev, -ENODEV, "Could not find %s device node\n", type); + + struct device_node *i2c_node __free(device_node) = of_get_parent(node); + if (!of_node_name_eq(i2c_node, "i2c")) + return dev_err_ptr_probe(dev, -EINVAL, "%s device isn't on I2C bus\n", type); + + if (!of_device_is_available(i2c_node)) + return dev_err_ptr_probe(dev, -ENODEV, "I2C controller not available\n"); + + return no_free_ptr(i2c_node); +} + +static int i2c_of_probe_enable_node(struct device *dev, struct device_node *node) +{ + int ret; + + dev_info(dev, "Enabling %pOF\n", node); + + struct of_changeset *ocs __free(kfree) = kzalloc(sizeof(*ocs), GFP_KERNEL); + if (!ocs) + return -ENOMEM; + + of_changeset_init(ocs); + ret = of_changeset_update_prop_string(ocs, node, "status", "okay"); + if (ret) + return ret; + + ret = of_changeset_apply(ocs); + if (ret) { + /* ocs needs to be explicitly cleaned up before being freed. */ + of_changeset_destroy(ocs); + } else { + /* + * ocs is intentionally kept around as it needs to + * exist as long as the change is applied. + */ + void *ptr __always_unused = no_free_ptr(ocs); + } + + return ret; +} + +static const struct i2c_of_probe_ops i2c_of_probe_dummy_ops; + +/** + * i2c_of_probe_component() - probe for devices of "type" on the same i2c bus + * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages. + * @cfg: Pointer to the &struct i2c_of_probe_cfg containing callbacks and other options + * for the prober. + * @ctx: Context data for callbacks. + * + * Probe for possible I2C components of the same "type" (&i2c_of_probe_cfg->type) + * on the same I2C bus that have their status marked as "fail". + * + * Assumes that across the entire device tree the only instances of nodes + * prefixed with "type" are the ones that need handling for second source + * components. In other words, if "type" is "touchscreen", then all device + * nodes named "touchscreen*" are the ones that need probing. There cannot + * be another "touchscreen" node that is already enabled. + * + * Assumes that for each "type" of component, only one actually exists. In + * other words, only one matching and existing device will be enabled. + * + * Context: Process context only. Does non-atomic I2C transfers. + * Should only be used from a driver probe function, as the function + * can return -EPROBE_DEFER if the I2C adapter or other resources + * are unavailable. + * Return: 0 on success or no-op, error code otherwise. + * A no-op can happen when it seems like the device tree already + * has components of the type to be probed already enabled. This + * can happen when the device tree had not been updated to mark + * the status of the to-be-probed components as "fail". Or this + * function was already run with the same parameters and succeeded + * in enabling a component. The latter could happen if the user + * had multiple types of components to probe, and one of them down + * the list caused a deferred probe. This is expected behavior. + */ +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx) +{ + const struct i2c_of_probe_ops *ops; + const char *type; + struct device_node *i2c_node; + struct i2c_adapter *i2c; + int ret; + + if (!cfg) + return -EINVAL; + + ops = cfg->ops ?: &i2c_of_probe_dummy_ops; + type = cfg->type; + + i2c_node = i2c_of_probe_get_i2c_node(dev, type); + if (IS_ERR(i2c_node)) + return PTR_ERR(i2c_node); + + for_each_child_of_node_with_prefix(i2c_node, node, type) { + if (!of_device_is_available(node)) + continue; + + /* + * Device tree has component already enabled. Either the + * device tree isn't supported or we already probed once. + */ + ret = 0; + goto out_put_i2c_node; + } + + i2c = of_get_i2c_adapter_by_node(i2c_node); + if (!i2c) { + ret = dev_err_probe(dev, -EPROBE_DEFER, "Couldn't get I2C adapter\n"); + goto out_put_i2c_node; + } + + /* Grab resources */ + ret = 0; + if (ops->get_resources) + ret = ops->get_resources(dev, i2c_node, ctx); + if (ret) + goto out_put_i2c_adapter; + + /* Enable resources */ + if (ops->enable) + ret = ops->enable(dev, ctx); + if (ret) + goto out_release_resources; + + ret = 0; + for_each_child_of_node_with_prefix(i2c_node, node, type) { + union i2c_smbus_data data; + u32 addr; + + if (of_property_read_u32(node, "reg", &addr)) + continue; + if (i2c_smbus_xfer(i2c, addr, 0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0) + continue; + + /* Found a device that is responding */ + if (ops->free_resources_early) + ops->free_resources_early(ctx); + ret = i2c_of_probe_enable_node(dev, node); + break; + } + + if (ops->cleanup) + ops->cleanup(dev, ctx); +out_release_resources: + if (ops->free_resources_late) + ops->free_resources_late(ctx); +out_put_i2c_adapter: + i2c_put_adapter(i2c); +out_put_i2c_node: + of_node_put(i2c_node); + + return ret; +} +EXPORT_SYMBOL_NS_GPL(i2c_of_probe_component, I2C_OF_PROBER); diff --git a/include/linux/i2c-of-prober.h b/include/linux/i2c-of-prober.h new file mode 100644 index 000000000000..0f94e7c94310 --- /dev/null +++ b/include/linux/i2c-of-prober.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * i2c-of-prober.h - definitions for the Linux I2C OF component prober + * + * Copyright (C) 2024 Google LLC + */ + +#ifndef _LINUX_I2C_OF_PROBER_H +#define _LINUX_I2C_OF_PROBER_H + +#if IS_ENABLED(CONFIG_OF_DYNAMIC) + +struct device; +struct device_node; + +/** + * struct i2c_of_probe_ops - I2C OF component prober callbacks + * + * A set of callbacks to be used by i2c_of_probe_component(). + * + * All callbacks are optional. Callbacks are called only once per run, and are + * used in the order they are defined in this structure. + * + * All callbacks that have return values shall return %0 on success, + * or a negative error number on failure. + * + * The @dev parameter passed to the callbacks is the same as @dev passed to + * i2c_of_probe_component(). It should only be used for dev_printk() calls + * and nothing else, especially not managed device resource (devres) APIs. + */ +struct i2c_of_probe_ops { + /** @get_resources: Retrieve resources for components. */ + int (*get_resources)(struct device *dev, struct device_node *bus_node, void *data); + + /** @free_resources_early: Release exclusive resources prior to enabling component. */ + void (*free_resources_early)(void *data); + + /** + * @enable: Enable resources so that the components respond to probes. + * + * Resources should be reverted to their initial state before returning if this fails. + */ + int (*enable)(struct device *dev, void *data); + + /** + * @cleanup: Opposite of @enable to balance refcounts after probing. + * + * Can not operate on resources already freed in @free_resources_early. + */ + int (*cleanup)(struct device *dev, void *data); + + /** + * @free_resources_late: Release all resources, including those that would have + * been released by @free_resources_early. + */ + void (*free_resources_late)(void *data); +}; + +/** + * struct i2c_of_probe_cfg - I2C OF component prober configuration + * @ops: Callbacks for the prober to use. + * @type: A string to match the device node name prefix to probe for. + */ +struct i2c_of_probe_cfg { + const struct i2c_of_probe_ops *ops; + const char *type; +}; + +int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx); + +#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ + +#endif /* _LINUX_I2C_OF_PROBER_H */
Some devices are designed and manufactured with some components having multiple drop-in replacement options. These components are often connected to the mainboard via ribbon cables, having the same signals and pin assignments across all options. These may include the display panel and touchscreen on laptops and tablets, and the trackpad on laptops. Sometimes which component option is used in a particular device can be detected by some firmware provided identifier, other times that information is not available, and the kernel has to try to probe each device. This change attempts to make the "probe each device" case cleaner. The current approach is to have all options added and enabled in the device tree. The kernel would then bind each device and run each driver's probe function. This works, but has been broken before due to the introduction of asynchronous probing, causing multiple instances requesting "shared" resources, such as pinmuxes, GPIO pins, interrupt lines, at the same time, with only one instance succeeding. Work arounds for these include moving the pinmux to the parent I2C controller, using GPIO hogs or pinmux settings to keep the GPIO pins in some fixed configuration, and requesting the interrupt line very late. Such configurations can be seen on the MT8183 Krane Chromebook tablets, and the Qualcomm sc8280xp-based Lenovo Thinkpad 13S. Instead of this delicate dance between drivers and device tree quirks, this change introduces a simple I2C component probe. function For a given class of devices on the same I2C bus, it will go through all of them, doing a simple I2C read transfer and see which one of them responds. It will then enable the device that responds. This requires some minor modifications in the existing device tree. The status for all the device nodes for the component options must be set to "failed-needs-probe". This makes it clear that some mechanism is needed to enable one of them, and also prevents the prober and device drivers running at the same time. Signed-off-by: Chen-Yu Tsai <wenst@chromium.org> --- Changes since v6: - Correctly replaced for_each_child_of_node_scoped() with for_each_child_of_node_with_prefix() - Added namespace for exported symbol - Made the probe function a framework with hooks - Split out a new header file - Added MAINTAINERS entry - Reworded kernel-doc - Dropped usage of __free from i2c_of_probe_component() since error path cleanup is needed anyway Changes since v5: - Fixed indent in Makefile - Split regulator and GPIO TODO items - Reversed final conditional in i2c_of_probe_enable_node() Changes since v4: - Split code into helper functions - Use scoped helpers and __free() to reduce error path Changes since v3: - Complete kernel-doc - Return different error if I2C controller is disabled - Expand comment to explain assumptions and constraints - Split for-loop finding target node and operations on target node - Add missing i2c_put_adapter() - Move prober code to separate file Rob also asked why there was a limitation of "exactly one touchscreen will be enabled across the whole tree". The use case this prober currently targets is a component on consumer electronics (tablet or laptop) being swapped out due to cost or supply reasons. Designs with multiple components of the same type are pretty rare. The way the next patch is written also assumes this for efficiency reasons. Changes since v2: - New patch split out from "of: Introduce hardware prober driver" - Addressed Rob's comments - Move i2c prober to i2c subsystem - Use of_node_is_available() to check if node is enabled. - Use OF changeset API to update status property - Addressed Andy's comments - Probe function now accepts "struct device *dev" instead to reduce line length and dereferences - Move "ret = 0" to just before for_each_child_of_node(i2c_node, node) --- MAINTAINERS | 8 ++ drivers/i2c/Makefile | 1 + drivers/i2c/i2c-core-of-prober.c | 195 +++++++++++++++++++++++++++++++ include/linux/i2c-of-prober.h | 73 ++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 drivers/i2c/i2c-core-of-prober.c create mode 100644 include/linux/i2c-of-prober.h