[PATCH v4 2/5] i2c: mux: add support for per channel bus frequency
Peter Rosin
peda at axentia.se
Thu Feb 12 07:02:16 PST 2026
Hi!
2026-01-28 at 10:54, Marcus Folkesson wrote:
> There may be several reasons why you may need to use a certain speed
> on an I2C bus. E.g.
>
> - When several devices are attached to the bus, the speed must be
> selected according to the slowest device.
>
> - Electrical conditions may limit the usuable speed on the bus for
> different reasons.
>
> With an I2C multiplexer, it is possible to group the attached devices
> after their preferred speed by e.g. put all "slow" devices on a separate
> channel on the multiplexer.
>
> Consider the following topology:
>
> .----------. 100kHz .--------.
> .--------. 400kHz | |--------| dev D1 |
> | root |--+-----| I2C MUX | '--------'
> '--------' | | |--. 400kHz .--------.
> | '----------' '-------| dev D2 |
> | .--------. '--------'
> '--| dev D3 |
> '--------'
>
> One requirement with this design is that a multiplexer may only use the
> same or lower bus speed as its parent.
> Otherwise, if the multiplexer would have to increase the bus frequency,
> then all siblings (D3 in this case) would run into a clock speed it may
> not support.
>
> The bus frequency for each channel is set in the devicetree. As the
> i2c-mux bindings import the i2c-controller schema, the clock-frequency
> property is already allowed.
> If no clock-frequency property is set, the channel inherit their parent
> bus speed.
>
> The following example uses dt bindings to illustrate the topology above:
>
> i2c {
> clock-frequency = <400000>;
>
> i2c-mux {
> i2c at 0 {
> clock-frequency = <100000>;
>
> D1 {
> ...
> };
> };
>
> i2c at 1 {
> D2 {
> ...
> };
> };
> };
>
> D3 {
> ...
> }
> };
>
> Signed-off-by: Marcus Folkesson <marcus.folkesson at gmail.com>
> ---
> drivers/i2c/i2c-mux.c | 116 ++++++++++++++++++++++++++++++++++++++++++++------
> 1 file changed, 104 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
> index d59644e50f14..b7974161be4a 100644
> --- a/drivers/i2c/i2c-mux.c
> +++ b/drivers/i2c/i2c-mux.c
> @@ -36,6 +36,72 @@ struct i2c_mux_priv {
> u32 chan_id;
> };
>
> +static int i2c_mux_select_chan(struct i2c_adapter *adap, u32 chan_id)
> +{
> + struct i2c_mux_priv *priv = adap->algo_data;
> + struct i2c_mux_core *muxc = priv->muxc;
> + struct i2c_adapter *parent = muxc->parent;
> + struct i2c_adapter *root;
> + int ret;
> +
> + if (priv->adap.clock_hz && priv->adap.clock_hz != parent->clock_hz) {
> + root = i2c_root_adapter(&adap->dev);
> +
> + /* if we are parent-locked and the root adapter is our parent,
> + * we already have the lock we need. Otherwise take the bus lock for the root
> + * adapter before changing bus clock.
> + */
The assumptions made for the "otherwise" case is wrong, I think.
Consider e.g. the case where we are parent-locket, our parent is
another parent-locked mux and our grand-parent is the root adapter.
In that case we also have all the locks we need. Trying to grab
them again will be a deadlock.
The more correct approach is to do the parent walk in search of
the first ancestor mux that is not parent-locked and then call
i2c_lock_bus(ancestor->parent, I2C_LOCK_ROOT_ADAPTER);
for it.
Again, I think. Famous last words. Handwaving...
Cheers,
Peter
> + if ((root != parent && !muxc->mux_locked) || muxc->mux_locked)
> + i2c_lock_bus(parent, I2C_LOCK_ROOT_ADAPTER);
> +
> + ret = i2c_adapter_set_clk_freq(root, priv->adap.clock_hz);
> +
> + if ((root != parent && !muxc->mux_locked) || muxc->mux_locked)
> + i2c_unlock_bus(parent, I2C_LOCK_ROOT_ADAPTER);
> +
> + if (ret < 0) {
> + dev_err(&adap->dev,
> + "Failed to set clock frequency %dHz on root adapter %s: %d\n",
> + priv->adap.clock_hz, root->name, ret);
> +
> + return ret;
> + }
> + }
> +
> + return muxc->select(muxc, priv->chan_id);
> +}
More information about the linux-arm-kernel
mailing list