diff mbox series

i3c: Allow OF-alias-based persistent bus numbering

Message ID 20230405094149.1513209-1-jk@codeconstruct.com.au (mailing list archive)
State Accepted
Headers show
Series i3c: Allow OF-alias-based persistent bus numbering | expand

Commit Message

Jeremy Kerr April 5, 2023, 9:41 a.m. UTC
Parse the /aliases node to assign any fixed bus numbers, as is done with
the i2c subsystem. Numbering for non-aliased busses will start after the
highest fixed bus number.

This allows an alias node such as:

    aliases {
        i3c0 = &bus_a,
	i3c4 = &bus_b,
    };

to set the numbering for a set of i3c controllers:

    /* fixed-numbered bus, assigned "i3c-0" */
    bus_a: i3c-master {
    };

    /* another fixed-numbered bus, assigned "i3c-4" */
    bus_b: i3c-master {
    };

    /* dynamic-numbered bus, likely assigned "i3c-5" */
    bus_c: i3c-master {
    };

If no i3c device aliases are present, the numbering will stay as-is,
starting from 0.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
---
 drivers/i3c/master.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

Comments

Ben Dooks April 5, 2023, 10:34 a.m. UTC | #1
On 05/04/2023 10:41, Jeremy Kerr wrote:
> Parse the /aliases node to assign any fixed bus numbers, as is done with
> the i2c subsystem. Numbering for non-aliased busses will start after the
> highest fixed bus number.
> 
> This allows an alias node such as:
> 
>      aliases {
>          i3c0 = &bus_a,
> 	i3c4 = &bus_b,
>      };
> 
> to set the numbering for a set of i3c controllers:
> 
>      /* fixed-numbered bus, assigned "i3c-0" */
>      bus_a: i3c-master {
>      };
> 
>      /* another fixed-numbered bus, assigned "i3c-4" */
>      bus_b: i3c-master {
>      };
> 
>      /* dynamic-numbered bus, likely assigned "i3c-5" */
>      bus_c: i3c-master {
>      };
> 
> If no i3c device aliases are present, the numbering will stay as-is,
> starting from 0.
> 
> Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
> ---
>   drivers/i3c/master.c | 30 +++++++++++++++++++++++++-----
>   1 file changed, 25 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index af8b9ebaec34..6a3169e4606e 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
> @@ -21,6 +21,7 @@
>   
>   static DEFINE_IDR(i3c_bus_idr);
>   static DEFINE_MUTEX(i3c_core_lock);
> +static int __i3c_first_dynamic_bus_num;
>   
>   /**
>    * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
> @@ -419,9 +420,9 @@ static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
>   	mutex_unlock(&i3c_core_lock);
>   }
>   
> -static int i3c_bus_init(struct i3c_bus *i3cbus)
> +static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)

would it be worth passing the struct i3c_master_controller through
this in case other info is needed at a later date? Does ACPI have
anything similar?

>   {
> -	int ret;
> +	int ret, start, end, id = -1;
>   
>   	init_rwsem(&i3cbus->lock);
>   	INIT_LIST_HEAD(&i3cbus->devs.i2c);
> @@ -429,8 +430,19 @@ static int i3c_bus_init(struct i3c_bus *i3cbus)
>   	i3c_bus_init_addrslots(i3cbus);
>   	i3cbus->mode = I3C_BUS_MODE_PURE;
>   
> +	if (np)
> +		id = of_alias_get_id(np, "i3c");
> +
>   	mutex_lock(&i3c_core_lock);
> -	ret = idr_alloc(&i3c_bus_idr, i3cbus, 0, 0, GFP_KERNEL);
> +	if (id >= 0) {
> +		start = id;
> +		end = start + 1;
> +	} else {
> +		start = __i3c_first_dynamic_bus_num;
> +		end = 0;
> +	}
> +
> +	ret = idr_alloc(&i3c_bus_idr, i3cbus, start, end, GFP_KERNEL);
>   	mutex_unlock(&i3c_core_lock);
>   
>   	if (ret < 0)
> @@ -2618,7 +2630,7 @@ int i3c_master_register(struct i3c_master_controller *master,
>   	INIT_LIST_HEAD(&master->boardinfo.i2c);
>   	INIT_LIST_HEAD(&master->boardinfo.i3c);
>   
> -	ret = i3c_bus_init(i3cbus);
> +	ret = i3c_bus_init(i3cbus, master->dev.of_node);
>   	if (ret)
>   		return ret;
>   
> @@ -2846,8 +2858,16 @@ void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
>   
>   static int __init i3c_init(void)
>   {
> -	int res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
> +	int res;
> +
> +	res = of_alias_get_highest_id("i3c");
> +	if (res >= 0) {
> +		mutex_lock(&i3c_core_lock);
> +		__i3c_first_dynamic_bus_num = res + 1;
> +		mutex_unlock(&i3c_core_lock);
> +	}
>   
> +	res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
>   	if (res)
>   		return res;
>
Jeremy Kerr April 5, 2023, 12:40 p.m. UTC | #2
Hi Ben,

> > @@ -419,9 +420,9 @@ static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
> >         mutex_unlock(&i3c_core_lock);
> >   }
> >   
> > -static int i3c_bus_init(struct i3c_bus *i3cbus)
> > +static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)
> 
> would it be worth passing the struct i3c_master_controller through
> this in case other info is needed at a later date? Does ACPI have
> anything similar?

We could certainly add other platform enumeration data in future, but I
don't know enough about ACPI device discovery to cater for everything at
this stage.

I'm just passing the OF node because that's all we need now; we can
certainly extend once we have a concrete pattern for other platforms -
this is just a static function, so it'll be easy to update.

If we want to follow i2c: for non-OF-based static bus numbering, it's up
to whatever is instantiating the i2c bus device to provide an id for
i2c_add_numbered_adapter(). We don't have any users for that in i3c at
present though, so it's probably a bit premature to add an equivalent
API.

Cheers,


Jeremy
diff mbox series

Patch

diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index af8b9ebaec34..6a3169e4606e 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -21,6 +21,7 @@ 
 
 static DEFINE_IDR(i3c_bus_idr);
 static DEFINE_MUTEX(i3c_core_lock);
+static int __i3c_first_dynamic_bus_num;
 
 /**
  * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
@@ -419,9 +420,9 @@  static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
 	mutex_unlock(&i3c_core_lock);
 }
 
-static int i3c_bus_init(struct i3c_bus *i3cbus)
+static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)
 {
-	int ret;
+	int ret, start, end, id = -1;
 
 	init_rwsem(&i3cbus->lock);
 	INIT_LIST_HEAD(&i3cbus->devs.i2c);
@@ -429,8 +430,19 @@  static int i3c_bus_init(struct i3c_bus *i3cbus)
 	i3c_bus_init_addrslots(i3cbus);
 	i3cbus->mode = I3C_BUS_MODE_PURE;
 
+	if (np)
+		id = of_alias_get_id(np, "i3c");
+
 	mutex_lock(&i3c_core_lock);
-	ret = idr_alloc(&i3c_bus_idr, i3cbus, 0, 0, GFP_KERNEL);
+	if (id >= 0) {
+		start = id;
+		end = start + 1;
+	} else {
+		start = __i3c_first_dynamic_bus_num;
+		end = 0;
+	}
+
+	ret = idr_alloc(&i3c_bus_idr, i3cbus, start, end, GFP_KERNEL);
 	mutex_unlock(&i3c_core_lock);
 
 	if (ret < 0)
@@ -2618,7 +2630,7 @@  int i3c_master_register(struct i3c_master_controller *master,
 	INIT_LIST_HEAD(&master->boardinfo.i2c);
 	INIT_LIST_HEAD(&master->boardinfo.i3c);
 
-	ret = i3c_bus_init(i3cbus);
+	ret = i3c_bus_init(i3cbus, master->dev.of_node);
 	if (ret)
 		return ret;
 
@@ -2846,8 +2858,16 @@  void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
 
 static int __init i3c_init(void)
 {
-	int res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
+	int res;
+
+	res = of_alias_get_highest_id("i3c");
+	if (res >= 0) {
+		mutex_lock(&i3c_core_lock);
+		__i3c_first_dynamic_bus_num = res + 1;
+		mutex_unlock(&i3c_core_lock);
+	}
 
+	res = bus_register_notifier(&i2c_bus_type, &i2cdev_notifier);
 	if (res)
 		return res;