Re: [RFC PATCH v2 2/7] of: Introduce hardware prober driver

From: Chen-Yu Tsai
Date: Tue Nov 14 2023 - 03:26:53 EST


On Fri, Nov 10, 2023 at 1:54 AM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> On Thu, Nov 09, 2023 at 06:05:59PM +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 prober. For any 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-xxx". 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.
>
> ...
>
> > +config HW_PROBER
>
> config OF_HW_PROBER // or anything with explicit OF
>
> Don't give a false impression that this is something that may works without
> OF support.

Ack.

> ...
>
> > + bool "Hardware Prober driver"
>
> Ditto.

Ack.

> ...
>
> > +/*
> > + * hw_prober.c - Hardware prober driver
>
> Do not include filename into the file itself.

Ack.

> > + *
> > + * Copyright (c) 2023 Google LLC
> > + */
>
> ...
>
> > + node = of_find_node_by_name(NULL, node_name);
> > + if (!node)
> > + return dev_err_probe(&pdev->dev, -ENODEV, "Could not find %s device node\n",
> > + node_name);
>
> With
>
> struct device *dev = &pdev->dev;
>
> this and other lines can be made neater.

Ack.

> ...
>
>
> For better maintenance it's good to have ret assignment be placed here
>
> ret = 0;

Ack.

> > + for_each_child_of_node(i2c_node, node) {
> > + struct property *prop;
> > + union i2c_smbus_data data;
> > + u32 addr;
> > +
> > + if (!of_node_name_prefix(node, node_name))
> > + continue;
> > + 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;
> > +
> > + dev_info(&pdev->dev, "Enabling %pOF\n", node);
> > +
> > + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
> > + if (!prop) {
> > + ret = -ENOMEM;
> > + of_node_put(node);
> > + break;
> > + }
> > +
> > + prop->name = "status";
> > + prop->length = 5;
> > + prop->value = "okay";
> > +
> > + /* Found a device that is responding */
> > + ret = of_update_property(node, prop);
> > + if (ret)
> > + kfree(prop);
> > +
> > + of_node_put(node);
> > + break;
> > + }
>
> ...
>
> > +static const struct hw_prober_entry hw_prober_platforms[] = {
> > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "touchscreen" },
> > + { .compatible = "google,hana", .prober = i2c_component_prober, .data = "trackpad" },
> > +};
>
> Why can't OF ID table be used for this?

My intent was to have this accept a probe function, which may take an extra
data argument. So either a new structure like the one here, or use OF ID table,
and then another layer with a struct holding the prober and extra data pointer.

I'm guessing this will change since Rob thinks the next patch that adds a
different prober doesn't belong here.

> ...
>
> > + for (int i = 0; i < ARRAY_SIZE(hw_prober_platforms); i++)
>
> unsigned?

Ack.

> > + if (of_machine_is_compatible(hw_prober_platforms[i].compatible)) {
> > + int ret;
> > +
> > + ret = hw_prober_platforms[i].prober(pdev, hw_prober_platforms[i].data);
> > + if (ret)
> > + return ret;
> > + }
>
> ...
>
> > + pdev = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
>
> -1 is defined in the header, use that definition.

Ack.

> > + if (!IS_ERR(pdev))
> > + return 0;
> > +
> > + platform_driver_unregister(&hw_prober_driver);
> > +
> > + return PTR_ERR(pdev);
>
> Can you use standard pattern, i.e. checking for the _error_ condition?

Ack.


Thanks
ChenYu