linux-renesas-soc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wolfram Sang <wsa+renesas@sang-engineering.com>
To: linux-i2c@vger.kernel.org
Cc: linux-renesas-soc@vger.kernel.org,
	Luca Ceresoli <luca@lucaceresoli.net>,
	Kieran Bingham <kieran@ksquared.org.uk>,
	Jacopo Mondi <jacopo@jmondi.org>,
	Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	Vladimir Zapolskiy <vz@mleia.com>,
	Wolfram Sang <wsa+renesas@sang-engineering.com>
Subject: [RFC PATCH 1/5] i2c: core: refactor scanning for a client
Date: Tue, 31 Dec 2019 17:13:56 +0100	[thread overview]
Message-ID: <20191231161400.1688-2-wsa+renesas@sang-engineering.com> (raw)
In-Reply-To: <20191231161400.1688-1-wsa+renesas@sang-engineering.com>

There is a pattern to check for existence of a client which is copied in
i2c_detect_address() and i2c_new_scanned_device():

1) check if address is valid
2) check if address is already registered
3) send a message and check the reponse

Because this pattern will be needed a third time soon, refactor it into
its own function.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 drivers/i2c/i2c-core-base.c | 57 ++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 32 deletions(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index a1eb28a3cc54..20a726dc78db 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -2108,29 +2108,39 @@ static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
 	return err >= 0;
 }
 
-static int i2c_detect_address(struct i2c_client *temp_client,
-			      struct i2c_driver *driver)
+static int i2c_scan_for_client(struct i2c_adapter *adap, unsigned short addr,
+			    int (*probe)(struct i2c_adapter *adap, unsigned short addr))
 {
-	struct i2c_board_info info;
-	struct i2c_adapter *adapter = temp_client->adapter;
-	int addr = temp_client->addr;
 	int err;
 
 	/* Make sure the address is valid */
 	err = i2c_check_7bit_addr_validity_strict(addr);
-	if (err) {
-		dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
-			 addr);
+	if (WARN(err, "Invalid probe address 0x%02x\n", addr))
 		return err;
-	}
 
 	/* Skip if already in use (7 bit, no need to encode flags) */
-	if (i2c_check_addr_busy(adapter, addr))
-		return 0;
+	if (i2c_check_addr_busy(adap, addr))
+		return -EBUSY;
 
 	/* Make sure there is something at this address */
-	if (!i2c_default_probe(adapter, addr))
-		return 0;
+	if (!probe(adap, addr))
+		return -ENODEV;
+
+	return 0;
+}
+
+static int i2c_detect_address(struct i2c_client *temp_client,
+			      struct i2c_driver *driver)
+{
+	struct i2c_board_info info;
+	struct i2c_adapter *adapter = temp_client->adapter;
+	int addr = temp_client->addr;
+	int err;
+
+	/* Only report broken addresses, busy addresses are no error */
+	err = i2c_scan_for_client(adapter, addr, i2c_default_probe);
+	if (err < 0)
+		return err == -EINVAL ? -EINVAL : 0;
 
 	/* Finally call the custom detection function */
 	memset(&info, 0, sizeof(struct i2c_board_info));
@@ -2232,26 +2242,9 @@ i2c_new_scanned_device(struct i2c_adapter *adap,
 	if (!probe)
 		probe = i2c_default_probe;
 
-	for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
-		/* Check address validity */
-		if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
-			dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
-				 addr_list[i]);
-			continue;
-		}
-
-		/* Check address availability (7 bit, no need to encode flags) */
-		if (i2c_check_addr_busy(adap, addr_list[i])) {
-			dev_dbg(&adap->dev,
-				"Address 0x%02x already in use, not probing\n",
-				addr_list[i]);
-			continue;
-		}
-
-		/* Test address responsiveness */
-		if (probe(adap, addr_list[i]))
+	for (i = 0; addr_list[i] != I2C_CLIENT_END; i++)
+		if (i2c_scan_for_client(adap, addr_list[i], probe) == 0)
 			break;
-	}
 
 	if (addr_list[i] == I2C_CLIENT_END) {
 		dev_dbg(&adap->dev, "Probing failed, no device found\n");
-- 
2.20.1


  reply	other threads:[~2019-12-31 16:14 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-31 16:13 [RFC PATCH 0/5] i2c: implement mechanism to retrieve an alias device Wolfram Sang
2019-12-31 16:13 ` Wolfram Sang [this message]
2020-01-01 16:45   ` [RFC PATCH 1/5] i2c: core: refactor scanning for a client Laurent Pinchart
2020-01-07  9:26   ` Kieran Bingham
2020-01-07  9:53     ` Geert Uytterhoeven
2020-01-07  9:58       ` Kieran Bingham
2020-01-07 10:25       ` Wolfram Sang
2020-01-07 10:36         ` Geert Uytterhoeven
2020-01-07 11:23           ` Wolfram Sang
2020-01-07 15:03             ` Luca Ceresoli
2020-01-07 16:45               ` Wolfram Sang
2020-01-07 16:52                 ` Kieran Bingham
2019-12-31 16:13 ` [RFC PATCH 2/5] i2c: core: add new variant to check " Wolfram Sang
2020-01-01 16:49   ` Laurent Pinchart
2020-01-07  9:42   ` Kieran Bingham
2019-12-31 16:13 ` [RFC PATCH 3/5] i2c: core: add function to request an alias Wolfram Sang
2020-01-01 16:55   ` Laurent Pinchart
2020-01-02 18:58     ` Luca Ceresoli
2020-01-02 21:13       ` Wolfram Sang
2020-01-02 22:27         ` Luca Ceresoli
2020-01-03  0:10           ` Laurent Pinchart
2020-01-07 15:03             ` Luca Ceresoli
2020-01-07 17:13               ` Laurent Pinchart
2020-01-08 13:27                 ` Wolfram Sang
2020-01-08 13:31                   ` Laurent Pinchart
2020-01-08 13:38                     ` Wolfram Sang
2020-01-08 13:22               ` Wolfram Sang
2020-01-08 13:19             ` Wolfram Sang
2020-01-08 13:29               ` Geert Uytterhoeven
2020-01-08 13:34               ` Laurent Pinchart
2020-01-02 21:03     ` Wolfram Sang
2020-01-21  9:05     ` Peter Rosin
2020-01-07  9:40   ` Kieran Bingham
2020-01-07 17:11     ` Laurent Pinchart
2020-01-07 17:14       ` Kieran Bingham
2020-01-08 13:35         ` Wolfram Sang
2020-01-08 13:36           ` Laurent Pinchart
2019-12-31 16:13 ` [RFC PATCH 4/5] i2c: core: add simple caching to the 'alias' scanning Wolfram Sang
2020-01-07  9:59   ` Kieran Bingham
2020-01-21  9:22   ` Peter Rosin
2019-12-31 16:14 ` [RFC PATCH 5/5] simple test case for the I2C alias functionality Wolfram Sang
2019-12-31 16:27 ` [RFC PATCH 0/5] i2c: implement mechanism to retrieve an alias device Wolfram Sang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191231161400.1688-2-wsa+renesas@sang-engineering.com \
    --to=wsa+renesas@sang-engineering.com \
    --cc=jacopo@jmondi.org \
    --cc=kieran@ksquared.org.uk \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=luca@lucaceresoli.net \
    --cc=vz@mleia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).