linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] i2c: of: brush up the code a little
@ 2018-01-18 12:11 Wolfram Sang
  2018-01-18 12:11 ` [PATCH 1/5] i2c: of: change log level of failed device creation Wolfram Sang
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

While debugging a refcounting problem with OF device nodes, I found a few small
issues in the code. This series addresses them.

Tested on a Renesas Salvator-XS (R-Car H3).

Looking forward for comments. Scheduled for 4.17.

@Lixin Wang: you were working on this code recently, so I added you to CC in
case you are interested in the changes.

Kind regards,

   Wolfram


Wolfram Sang (5):
  i2c: of: change log level of failed device creation
  i2c: of: make ref counting more visible
  i2c: of: rename variable to meet expectations
  i2c: of: remove duplicated check for valid address
  i2c: of: simplify reading the "reg" property

 drivers/i2c/i2c-core-of.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)

-- 
2.11.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] i2c: of: change log level of failed device creation
  2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
@ 2018-01-18 12:11 ` Wolfram Sang
  2018-01-18 12:11 ` [PATCH 2/5] i2c: of: make ref counting more visible Wolfram Sang
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

If we cannot create a device, this is an error, not a warning. Fix the
log level.

Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/i2c-core-of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index 8d474bb1dc1575..b652579ed04e66 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -103,7 +103,7 @@ void of_i2c_register_devices(struct i2c_adapter *adap)
 
 		client = of_i2c_register_device(adap, node);
 		if (IS_ERR(client)) {
-			dev_warn(&adap->dev,
+			dev_err(&adap->dev,
 				 "Failed to create I2C device for %pOF\n",
 				 node);
 			of_node_clear_flag(node, OF_POPULATED);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/5] i2c: of: make ref counting more visible
  2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
  2018-01-18 12:11 ` [PATCH 1/5] i2c: of: change log level of failed device creation Wolfram Sang
@ 2018-01-18 12:11 ` Wolfram Sang
  2018-01-18 12:11 ` [PATCH 3/5] i2c: of: rename variable to meet expectations Wolfram Sang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

When debugging a ref counting problem, I overlooked this snipplet a few
times. Might be taste, but I think the new location is visually easier
recognizable.

Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/i2c-core-of.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index b652579ed04e66..5e2d55b3daacca 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -64,8 +64,8 @@ static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 	}
 
 	info.addr = addr;
-	info.of_node = of_node_get(node);
 	info.archdata = &dev_ad;
+	info.of_node = of_node_get(node);
 
 	if (of_property_read_bool(node, "host-notify"))
 		info.flags |= I2C_CLIENT_HOST_NOTIFY;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/5] i2c: of: rename variable to meet expectations
  2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
  2018-01-18 12:11 ` [PATCH 1/5] i2c: of: change log level of failed device creation Wolfram Sang
  2018-01-18 12:11 ` [PATCH 2/5] i2c: of: make ref counting more visible Wolfram Sang
@ 2018-01-18 12:11 ` Wolfram Sang
  2018-01-18 12:11 ` [PATCH 4/5] i2c: of: remove duplicated check for valid address Wolfram Sang
  2018-01-18 12:11 ` [PATCH 5/5] i2c: of: simplify reading the "reg" property Wolfram Sang
  4 siblings, 0 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

'result' is mostly used in the kernel as int for functions returning
errno on failure. Here it is a pointer to the client struct, so let's
call it this way (as the parent function does, too).

Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/i2c-core-of.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index 5e2d55b3daacca..4b573ee4a8203a 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -25,7 +25,7 @@
 static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 						 struct device_node *node)
 {
-	struct i2c_client *result;
+	struct i2c_client *client;
 	struct i2c_board_info info = {};
 	struct dev_archdata dev_ad = {};
 	const __be32 *addr_be;
@@ -73,13 +73,13 @@ static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 	if (of_get_property(node, "wakeup-source", NULL))
 		info.flags |= I2C_CLIENT_WAKE;
 
-	result = i2c_new_device(adap, &info);
-	if (result == NULL) {
+	client = i2c_new_device(adap, &info);
+	if (!client) {
 		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
 		of_node_put(node);
 		return ERR_PTR(-EINVAL);
 	}
-	return result;
+	return client;
 }
 
 void of_i2c_register_devices(struct i2c_adapter *adap)
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/5] i2c: of: remove duplicated check for valid address
  2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
                   ` (2 preceding siblings ...)
  2018-01-18 12:11 ` [PATCH 3/5] i2c: of: rename variable to meet expectations Wolfram Sang
@ 2018-01-18 12:11 ` Wolfram Sang
  2018-01-18 12:11 ` [PATCH 5/5] i2c: of: simplify reading the "reg" property Wolfram Sang
  4 siblings, 0 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

The very same check is done when calling i2c_new_device(). Remove it
here to avoid code duplication.

Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/i2c-core-of.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index 4b573ee4a8203a..bbfff3f61b1fdb 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -57,12 +57,6 @@ static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 		info.flags |= I2C_CLIENT_SLAVE;
 	}
 
-	if (i2c_check_addr_validity(addr, info.flags)) {
-		dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
-			addr, node);
-		return ERR_PTR(-EINVAL);
-	}
-
 	info.addr = addr;
 	info.archdata = &dev_ad;
 	info.of_node = of_node_get(node);
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/5] i2c: of: simplify reading the "reg" property
  2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
                   ` (3 preceding siblings ...)
  2018-01-18 12:11 ` [PATCH 4/5] i2c: of: remove duplicated check for valid address Wolfram Sang
@ 2018-01-18 12:11 ` Wolfram Sang
  4 siblings, 0 replies; 6+ messages in thread
From: Wolfram Sang @ 2018-01-18 12:11 UTC (permalink / raw)
  To: linux-i2c; +Cc: Lixin Wang, Wolfram Sang

of_get_property() is a bit cumbersome to use. Replace it with the newer
of_property_read_u32() for more readable code.

Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
---
 drivers/i2c/i2c-core-of.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/drivers/i2c/i2c-core-of.c b/drivers/i2c/i2c-core-of.c
index bbfff3f61b1fdb..c405270a98b4f0 100644
--- a/drivers/i2c/i2c-core-of.c
+++ b/drivers/i2c/i2c-core-of.c
@@ -4,7 +4,7 @@
  * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
  * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  *
- * Copyright (C) 2013 Wolfram Sang <wsa@the-dreams.de>
+ * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -28,9 +28,8 @@ static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 	struct i2c_client *client;
 	struct i2c_board_info info = {};
 	struct dev_archdata dev_ad = {};
-	const __be32 *addr_be;
 	u32 addr;
-	int len;
+	int ret;
 
 	dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
 
@@ -40,13 +39,12 @@ static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 		return ERR_PTR(-EINVAL);
 	}
 
-	addr_be = of_get_property(node, "reg", &len);
-	if (!addr_be || (len < sizeof(*addr_be))) {
+	ret = of_property_read_u32(node, "reg", &addr);
+	if (ret) {
 		dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
-		return ERR_PTR(-EINVAL);
+		return ERR_PTR(ret);
 	}
 
-	addr = be32_to_cpup(addr_be);
 	if (addr & I2C_TEN_BIT_ADDRESS) {
 		addr &= ~I2C_TEN_BIT_ADDRESS;
 		info.flags |= I2C_CLIENT_TEN;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-01-18 12:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-18 12:11 [PATCH 0/5] i2c: of: brush up the code a little Wolfram Sang
2018-01-18 12:11 ` [PATCH 1/5] i2c: of: change log level of failed device creation Wolfram Sang
2018-01-18 12:11 ` [PATCH 2/5] i2c: of: make ref counting more visible Wolfram Sang
2018-01-18 12:11 ` [PATCH 3/5] i2c: of: rename variable to meet expectations Wolfram Sang
2018-01-18 12:11 ` [PATCH 4/5] i2c: of: remove duplicated check for valid address Wolfram Sang
2018-01-18 12:11 ` [PATCH 5/5] i2c: of: simplify reading the "reg" property Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).