Skip to contentSkip to conversation

Add automatic bus number support for i2c busses with device tree

PATCH Superseded Doug Anderson 2 participants 11 messages Feb 12, 2013 – Mar 21, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover Add automatic bus number support for i2c busses with device tree

This was suggested by Mark Brown in response to a patch for adding this functionality only for the s3c2410 bus:
https://lkml.org/lkml/2012/11/20/681

I have also modified the i2c-pxa driver to use this new functionality. The i2c-pxa driver changes have only been compile-tested and are just for cleanup purposes.

Changes in v3:
- Addressed Wolfram's feedback; rebased atop idr-cleanup series.

Changes in v2:
- No longer tweak pdev->id as per Sylwester Nawrocki.
- No longer add the dev ID to the adap.name. Other drivers don't include the device ID here and it doesn't make sense with dynamically (or automatically) allocated IDs.
- Use dev_name(&dev->dev) to register for the IRQ; this matches what the i2c-s3c2410.c does and handles dynamically allocated IDs.
- This change was only compile-tested (corgi_defconfig), since I don't have access to a board that uses this driver.

Doug Anderson (2):
i2c-core: dt: Pick i2c bus number from i2c alias if present
i2c: pxa: Use i2c-core to get bus number now

 drivers/i2c/busses/i2c-pxa.c | 20 ++++++++--------
 drivers/i2c/i2c-core.c       | 54 ++++++++++++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.1
#1 lore raw

PATCH v3 1/2 i2c-core: dt: Pick i2c bus number from i2c alias if present

This allows you to get the equivalent functionality of i2c_add_numbered_adapter() with all data in the device tree and no special case code in your driver. This is a common device tree technique.

For quick reference, the FDT syntax for using an alias to provide an ID looks like:

  aliases {
    i2c0 = &i2c_0;
    i2c1 = &i2c_1;

};

---
Changes in v3:
- Addressed Wolfram's feedback; rebased atop idr-cleanup series.

Changes in v2: None

 drivers/i2c/i2c-core.c | 54 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 40 insertions(+), 14 deletions(-)
drivers/i2c/i2c-core.c +40 -14
index 0b599f2..5204818 100644
@@ -921,13 +921,41 @@ out_list:
 }
 
 /**
+ * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
+ * @adap: the adapter to register (with adap->nr initialized)
+ * Context: can sleep
+ *
+ * See i2c_add_numbered_adapter() for details.
+ */
+static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
+{
+	int	id;
+
+	/* Handled by wrappers */
+	if (WARN_ON(adap->nr == -1))
+		return -EINVAL;
+
+	if (adap->nr & ~MAX_IDR_MASK)
+		return -EINVAL;
+
+	mutex_lock(&core_lock);
+	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
+		       GFP_KERNEL);
+	mutex_unlock(&core_lock);
+	if (id < 0)
+		return id == -ENOSPC ? -EBUSY : id;
+	return i2c_register_adapter(adap);
+}
+
+/**
  * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  * @adapter: the adapter to add
  * Context: can sleep
  *
  * This routine is used to declare an I2C adapter when its bus number
- * doesn't matter.  Examples: for I2C adapters dynamically added by
- * USB links or PCI plugin cards.
+ * doesn't matter or when its bus number is specified by an dt alias.
+ * Examples of bases when the bus number doesn't matter: I2C adapters
+ * dynamically added by USB links or PCI plugin cards.
  *
  * When this returns zero, a new bus number was allocated and stored
  * in adap->nr, and the specified adapter became available for clients.
@@ -935,8 +963,17 @@ out_list:
  */
 int i2c_add_adapter(struct i2c_adapter *adapter)
 {
+	struct device *dev = &adapter->dev;
 	int res;
 
+	if (dev->of_node) {
+		res = of_alias_get_id(dev->of_node, "i2c");
+		if (res >= 0) {
+			adapter->nr = res;
+			return __i2c_add_numbered_adapter(adapter);
+		}
+	}
+
 	mutex_lock(&core_lock);
 	res = idr_alloc(&i2c_adapter_idr, adapter,
 			__i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
@@ -974,20 +1011,9 @@ EXPORT_SYMBOL(i2c_add_adapter);
  */
 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
 {
-	int	id;
-
 	if (adap->nr == -1) /* -1 means dynamically assign bus id */
 		return i2c_add_adapter(adap);
-	if (adap->nr & ~MAX_IDR_MASK)
-		return -EINVAL;
-
-	mutex_lock(&core_lock);
-	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
-		       GFP_KERNEL);
-	mutex_unlock(&core_lock);
-	if (id < 0)
-		return id == -ENOSPC ? -EBUSY : id;
-	return i2c_register_adapter(adap);
+	return __i2c_add_numbered_adapter(adap);
 }
 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
 
Signature · 1 line
1.8.1
#2 lore raw

PATCH v3 2/2 i2c: pxa: Use i2c-core to get bus number now

In reply to Doug Anderson

The commit: "i2c-core: dt: Pick i2c bus number from i2c alias if present" adds support for automatically picking the bus number based on the alias ID. Remove the now unnecessary code from i2c-pxa that did the same thing.

---
Changes in v3: None
Changes in v2:
- No longer tweak pdev->id as per Sylwester Nawrocki.
- No longer add the dev ID to the adap.name. Other drivers don't include the device ID here and it doesn't make sense with dynamically (or automatically) allocated IDs.
- Use dev_name(&dev->dev) to register for the IRQ; this matches what the i2c-s3c2410.c does and handles dynamically allocated IDs.
- This change was only compile-tested (corgi_defconfig), since I don't have access to a board that uses this driver.

 drivers/i2c/busses/i2c-pxa.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
drivers/i2c/busses/i2c-pxa.c +9 -11
index 1034d93..705cc9f 100644
@@ -1053,16 +1053,13 @@ static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
 	struct device_node *np = pdev->dev.of_node;
 	const struct of_device_id *of_id =
 			of_match_device(i2c_pxa_dt_ids, &pdev->dev);
-	int ret;
 
 	if (!of_id)
 		return 1;
-	ret = of_alias_get_id(np, "i2c");
-	if (ret < 0) {
-		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
-		return ret;
-	}
-	pdev->id = ret;
+
+	/* For device tree we always use the dynamic or alias-assigned ID */
+	i2c->adap.nr = -1;
+
 	if (of_get_property(np, "mrvl,i2c-polling", NULL))
 		i2c->use_pio = 1;
 	if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
@@ -1100,6 +1097,9 @@ static int i2c_pxa_probe(struct platform_device *dev)
 		goto emalloc;
 	}
 
+	/* Default adapter num to device id; i2c_pxa_probe_dt can override. */
+	i2c->adap.nr = dev->id;
+
 	ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
 	if (ret > 0)
 		ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
@@ -1124,9 +1124,7 @@ static int i2c_pxa_probe(struct platform_device *dev)
 	spin_lock_init(&i2c->lock);
 	init_waitqueue_head(&i2c->wait);
 
-	i2c->adap.nr = dev->id;
-	snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
-		 i2c->adap.nr);
+	strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
 
 	i2c->clk = clk_get(&dev->dev, NULL);
 	if (IS_ERR(i2c->clk)) {
@@ -1169,7 +1167,7 @@ static int i2c_pxa_probe(struct platform_device *dev)
 	} else {
 		i2c->adap.algo = &i2c_pxa_algorithm;
 		ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
-				  i2c->adap.name, i2c);
+				  dev_name(&dev->dev), i2c);
 		if (ret)
 			goto ereqirq;
 	}
Signature · 1 line
1.8.1
#3 lore raw
In reply to Doug Anderson

Wolfram,

Quoted 21 lines · Doug Anderson

This was suggested by Mark Brown in response to a patch for adding this functionality only for the s3c2410 bus:
https://lkml.org/lkml/2012/11/20/681

I have also modified the i2c-pxa driver to use this new functionality. The i2c-pxa driver changes have only been compile-tested and are just for cleanup purposes.

Changes in v3:
- Addressed Wolfram's feedback; rebased atop idr-cleanup series.

Changes in v2:
- No longer tweak pdev->id as per Sylwester Nawrocki.
- No longer add the dev ID to the adap.name. Other drivers don't include the device ID here and it doesn't make sense with dynamically (or automatically) allocated IDs.
- Use dev_name(&dev->dev) to register for the IRQ; this matches what the i2c-s3c2410.c does and handles dynamically allocated IDs.
- This change was only compile-tested (corgi_defconfig), since I don't have access to a board that uses this driver.

Quoted 7 lines

Doug Anderson (2):
i2c-core: dt: Pick i2c bus number from i2c alias if present
i2c: pxa: Use i2c-core to get bus number now

 drivers/i2c/busses/i2c-pxa.c | 20 ++++++++--------
 drivers/i2c/i2c-core.c       | 54 ++++++++++++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 25 deletions(-)

Is there anything else needed for this patch series? It's been hanging around for quite a while and it would be nice to see it land. If you're waiting for additional acks or reviews I'll ask around and see if I can get them...

Thanks! :)

-Doug

#4 lore raw
Quoted 5 lines
Quoted 5 lines

Doug Anderson (2):
i2c-core: dt: Pick i2c bus number from i2c alias if present
i2c: pxa: Use i2c-core to get bus number now

Quoted 8 lines
Quoted 3 lines
 drivers/i2c/busses/i2c-pxa.c | 20 ++++++++--------
 drivers/i2c/i2c-core.c       | 54 ++++++++++++++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 25 deletions(-)

Is there anything else needed for this patch series? It's been hanging around for quite a while and it would be nice to see it land. If you're waiting for additional acks or reviews I'll ask around and see if I can get them...

Regarding patch 1, I was waiting for the idr changes to hit mainline. They are mainline now, but since the removal of MAX_IDR_MASK your patch doesn't apply anymore :( Can you rebase and retest, please? I'd like to get it into 3.9, still.

For patch 2, some Tested-by would be nice; but it will probably make it into my for-next somewhen after rc1 is out.

Thanks,

Wolfram

#5 lore raw

Wolfram,

Quoted 5 lines · Wolfram Sang

Regarding patch 1, I was waiting for the idr changes to hit mainline. They are mainline now, but since the removal of MAX_IDR_MASK your patch doesn't apply anymore :( Can you rebase and retest, please? I'd like to get it into 3.9, still.

Yes, I'll rebase/retest tomorrow. I'll let you make the call between 3.9 and 3.10. Obviously I'd love to see it in 3.9 but I'm OK with either. I was just hoping not to find out at the end of the 3.10 cycle that it needs fixes and need to wait for 3.11. I wanted to make sure all my ducks were in a row. ;-)

Quoted 2 lines

For patch 2, some Tested-by would be nice; but it will probably make it into my for-next somewhen after rc1 is out.

I have no urgent need for patch 2 to land but it seemed nice to tidy up the pxa driver and I felt it was my obligation to post it together with patch set #1 (plus it's always nice to remove code). Putting it into your for-next seems like a very good solution. That gives people some time to test it and make sure it works for them.

Thanks!

-Doug

#6 lore raw

v4 cover Add automatic bus number support for i2c busses with device tree

In reply to Doug Anderson

This was suggested by Mark Brown in response to a patch for adding this functionality only for the s3c2410 bus:
https://lkml.org/lkml/2012/11/20/681

I have also modified the i2c-pxa driver to use this new functionality. The i2c-pxa driver changes have only been compile-tested and are just for cleanup purposes.

Changes in v4:
- Rebased atop the removal of MAX_IDR_MASK.

Changes in v3:
- Addressed Wolfram's feedback; rebased atop idr-cleanup series.

Changes in v2:
- No longer tweak pdev->id as per Sylwester Nawrocki.
- No longer add the dev ID to the adap.name. Other drivers don't include the device ID here and it doesn't make sense with dynamically (or automatically) allocated IDs.
- Use dev_name(&dev->dev) to register for the IRQ; this matches what the i2c-s3c2410.c does and handles dynamically allocated IDs.
- This change was only compile-tested (corgi_defconfig), since I don't have access to a board that uses this driver.

Doug Anderson (2):
i2c-core: dt: Pick i2c bus number from i2c alias if present
i2c: pxa: Use i2c-core to get bus number now

 drivers/i2c/busses/i2c-pxa.c | 20 ++++++++----------
 drivers/i2c/i2c-core.c       | 49 +++++++++++++++++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 23 deletions(-)
Signature · 1 line
1.8.1.3
#7 lore raw

PATCH v4 1/2 i2c-core: dt: Pick i2c bus number from i2c alias if present

This allows you to get the equivalent functionality of i2c_add_numbered_adapter() with all data in the device tree and no special case code in your driver. This is a common device tree technique.

For quick reference, the FDT syntax for using an alias to provide an ID looks like:

  aliases {
    i2c0 = &i2c_0;
    i2c1 = &i2c_1;

};

---
Changes in v4:
- Rebased atop the removal of MAX_IDR_MASK.

Changes in v3:
- Addressed Wolfram's feedback; rebased atop idr-cleanup series.

Changes in v2: None

 drivers/i2c/i2c-core.c | 49 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 12 deletions(-)
drivers/i2c/i2c-core.c +37 -12
index 991d38d..66c5252 100644
@@ -921,13 +921,38 @@ out_list:
 }
 
 /**
+ * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
+ * @adap: the adapter to register (with adap->nr initialized)
+ * Context: can sleep
+ *
+ * See i2c_add_numbered_adapter() for details.
+ */
+static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
+{
+	int	id;
+
+	/* Handled by wrappers */
+	if (WARN_ON(adap->nr == -1))
+		return -EINVAL;
+
+	mutex_lock(&core_lock);
+	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
+		       GFP_KERNEL);
+	mutex_unlock(&core_lock);
+	if (id < 0)
+		return id == -ENOSPC ? -EBUSY : id;
+	return i2c_register_adapter(adap);
+}
+
+/**
  * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  * @adapter: the adapter to add
  * Context: can sleep
  *
  * This routine is used to declare an I2C adapter when its bus number
- * doesn't matter.  Examples: for I2C adapters dynamically added by
- * USB links or PCI plugin cards.
+ * doesn't matter or when its bus number is specified by an dt alias.
+ * Examples of bases when the bus number doesn't matter: I2C adapters
+ * dynamically added by USB links or PCI plugin cards.
  *
  * When this returns zero, a new bus number was allocated and stored
  * in adap->nr, and the specified adapter became available for clients.
@@ -935,8 +960,17 @@ out_list:
  */
 int i2c_add_adapter(struct i2c_adapter *adapter)
 {
+	struct device *dev = &adapter->dev;
 	int id;
 
+	if (dev->of_node) {
+		id = of_alias_get_id(dev->of_node, "i2c");
+		if (id >= 0) {
+			adapter->nr = id;
+			return __i2c_add_numbered_adapter(adapter);
+		}
+	}
+
 	mutex_lock(&core_lock);
 	id = idr_alloc(&i2c_adapter_idr, adapter,
 		       __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
@@ -975,18 +1009,9 @@ EXPORT_SYMBOL(i2c_add_adapter);
  */
 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
 {
-	int	id;
-
 	if (adap->nr == -1) /* -1 means dynamically assign bus id */
 		return i2c_add_adapter(adap);
-
-	mutex_lock(&core_lock);
-	id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
-		       GFP_KERNEL);
-	mutex_unlock(&core_lock);
-	if (id < 0)
-		return id == -ENOSPC ? -EBUSY : id;
-	return i2c_register_adapter(adap);
+	return __i2c_add_numbered_adapter(adap);
 }
 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
 
Signature · 1 line
1.8.1.3
#9 lore raw

PATCH v4 2/2 i2c: pxa: Use i2c-core to get bus number now

In reply to Doug Anderson

The commit: "i2c-core: dt: Pick i2c bus number from i2c alias if present" adds support for automatically picking the bus number based on the alias ID. Remove the now unnecessary code from i2c-pxa that did the same thing.

---
Changes in v4: None
Changes in v3: None
Changes in v2:
- No longer tweak pdev->id as per Sylwester Nawrocki.
- No longer add the dev ID to the adap.name. Other drivers don't include the device ID here and it doesn't make sense with dynamically (or automatically) allocated IDs.
- Use dev_name(&dev->dev) to register for the IRQ; this matches what the i2c-s3c2410.c does and handles dynamically allocated IDs.
- This change was only compile-tested (corgi_defconfig), since I don't have access to a board that uses this driver.

 drivers/i2c/busses/i2c-pxa.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
drivers/i2c/busses/i2c-pxa.c +9 -11
index 1e88e8d..ea6d45d 100644
@@ -1053,16 +1053,13 @@ static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
 	struct device_node *np = pdev->dev.of_node;
 	const struct of_device_id *of_id =
 			of_match_device(i2c_pxa_dt_ids, &pdev->dev);
-	int ret;
 
 	if (!of_id)
 		return 1;
-	ret = of_alias_get_id(np, "i2c");
-	if (ret < 0) {
-		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
-		return ret;
-	}
-	pdev->id = ret;
+
+	/* For device tree we always use the dynamic or alias-assigned ID */
+	i2c->adap.nr = -1;
+
 	if (of_get_property(np, "mrvl,i2c-polling", NULL))
 		i2c->use_pio = 1;
 	if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
@@ -1100,6 +1097,9 @@ static int i2c_pxa_probe(struct platform_device *dev)
 		goto emalloc;
 	}
 
+	/* Default adapter num to device id; i2c_pxa_probe_dt can override. */
+	i2c->adap.nr = dev->id;
+
 	ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
 	if (ret > 0)
 		ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
@@ -1124,9 +1124,7 @@ static int i2c_pxa_probe(struct platform_device *dev)
 	spin_lock_init(&i2c->lock);
 	init_waitqueue_head(&i2c->wait);
 
-	i2c->adap.nr = dev->id;
-	snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
-		 i2c->adap.nr);
+	strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
 
 	i2c->clk = clk_get(&dev->dev, NULL);
 	if (IS_ERR(i2c->clk)) {
@@ -1169,7 +1167,7 @@ static int i2c_pxa_probe(struct platform_device *dev)
 	} else {
 		i2c->adap.algo = &i2c_pxa_algorithm;
 		ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
-				  i2c->adap.name, i2c);
+				  dev_name(&dev->dev), i2c);
 		if (ret)
 			goto ereqirq;
 	}
Signature · 1 line
1.8.1.3
Applied
#8 lore raw

Re: [PATCH v4 1/2] i2c-core: dt: Pick i2c bus number from i2c alias if present

In reply to Doug Anderson
Quoted 5 lines · Doug Anderson

This allows you to get the equivalent functionality of i2c_add_numbered_adapter() with all data in the device tree and no special case code in your driver. This is a common device tree technique.

Quoted 8 lines

For quick reference, the FDT syntax for using an alias to provide an ID looks like:

  aliases {
    i2c0 = &i2c_0;
    i2c1 = &i2c_1;

};

Thanks, applied. I'll try to get it into 3.9.

drivers/i2c/i2c-core.c out_list: · line 936

Quoted 3 lines
drivers/i2c/i2c-core.c +3 -0
+	/* Handled by wrappers */
+	if (WARN_ON(adap->nr == -1))
+		return -EINVAL;

I removed this check, though. We know our callers, all static.

Thanks,

Wolfram

Applied
#10 lore raw

Re: [PATCH v4 2/2] i2c: pxa: Use i2c-core to get bus number now

In reply to Doug Anderson
Quoted 6 lines · Doug Anderson

The commit: "i2c-core: dt: Pick i2c bus number from i2c alias if present" adds support for automatically picking the bus number based on the alias ID. Remove the now unnecessary code from i2c-pxa that did the same thing.

Thanks, applied for-next.