linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Linus Walleij <linus.walleij@linaro.org>, linux-gpio@vger.kernel.org
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: [PATCH v1 8/9] pinctrl: mcp23s08: Use for_each_set_bit() and hweight_long()
Date: Tue,  7 Apr 2020 20:38:48 +0300	[thread overview]
Message-ID: <20200407173849.43628-8-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20200407173849.43628-1-andriy.shevchenko@linux.intel.com>

Here is a simplification of SPI code by using for_each_set_bit() and
hweight_long() library functions.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/pinctrl/pinctrl-mcp23s08.c | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-mcp23s08.c b/drivers/pinctrl/pinctrl-mcp23s08.c
index 330c2203e0f2..ea8decc36d50 100644
--- a/drivers/pinctrl/pinctrl-mcp23s08.c
+++ b/drivers/pinctrl/pinctrl-mcp23s08.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* MCP23S08 SPI/I2C GPIO driver */
 
+#include <linux/bitops.h>
 #include <linux/kernel.h>
 #include <linux/device.h>
 #include <linux/mutex.h>
@@ -940,13 +941,14 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
 static int mcp23s08_probe(struct spi_device *spi)
 {
 	struct device *dev = &spi->dev;
+	unsigned long spi_present_mask;
 	const void *match;
+	int chips;
+	u32 v;
 	unsigned			addr;
-	int				chips = 0;
 	struct mcp23s08_driver_data	*data;
 	int				status, type;
 	unsigned			ngpio = 0;
-	u32				spi_present_mask;
 
 	match = device_get_match_data(dev);
 	if (match)
@@ -954,29 +956,22 @@ static int mcp23s08_probe(struct spi_device *spi)
 	else
 		type = spi_get_device_id(spi)->driver_data;
 
-	status = device_property_read_u32(&spi->dev,
-			"microchip,spi-present-mask", &spi_present_mask);
+	status = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
 	if (status) {
-		status = device_property_read_u32(&spi->dev,
-				"mcp,spi-present-mask", &spi_present_mask);
+		status = device_property_read_u32(dev, "mcp,spi-present-mask", &v);
 		if (status) {
 			dev_err(&spi->dev, "missing spi-present-mask");
 			return status;
 		}
 	}
+	spi_present_mask = v;
 
-	if (!spi_present_mask || spi_present_mask > 0xff) {
+	if (!spi_present_mask || spi_present_mask >= BIT(MCP_MAX_DEV_PER_CS)) {
 		dev_err(&spi->dev, "invalid spi-present-mask");
 		return -ENODEV;
 	}
 
-	for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
-		if (spi_present_mask & BIT(addr))
-			chips++;
-	}
-
-	if (!chips)
-		return -ENODEV;
+	chips = hweight_long(spi_present_mask);
 
 	data = devm_kzalloc(&spi->dev,
 			    struct_size(data, chip, chips), GFP_KERNEL);
@@ -985,11 +980,8 @@ static int mcp23s08_probe(struct spi_device *spi)
 
 	spi_set_drvdata(spi, data);
 
-	for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
-		if (!(spi_present_mask & BIT(addr)))
-			continue;
-		chips--;
-		data->mcp[addr] = &data->chip[chips];
+	for_each_set_bit(addr, &spi_present_mask, MCP_MAX_DEV_PER_CS) {
+		data->mcp[addr] = &data->chip[--chips];
 		data->mcp[addr]->irq = spi->irq;
 
 		status = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type);
-- 
2.25.1


  parent reply	other threads:[~2020-04-07 17:38 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-07 17:38 [PATCH v1 1/9] pinctrl: mcp23s08: Get rid of legacy platform data Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 2/9] pinctrl: mcp23s08: Deduplicate IRQ chip filling Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 3/9] pinctrl: mcp23s08: Consolidate SPI and I²C code Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 4/9] pinctrl: mcp23s08: Drop unused parameter in mcp23s08_probe_one() Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 5/9] pinctrl: mcp23s08: Refactor mcp23s08_spi_regmap_init() Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 6/9] pinctrl: mcp23s08: Propagate error code from device_property_read_u32() Andy Shevchenko
2020-04-07 17:38 ` [PATCH v1 7/9] pinctrl: mcp23s08: Make use of device properties Andy Shevchenko
2020-04-07 17:38 ` Andy Shevchenko [this message]
2020-04-07 17:38 ` [PATCH v1 9/9] pinctrl: mcp23s08: Split to three parts: core, I²C, SPI Andy Shevchenko
2020-04-16 10:35 ` [PATCH v1 1/9] pinctrl: mcp23s08: Get rid of legacy platform data Linus Walleij
2020-10-09  9:15   ` Martin Hundebøll
2020-10-09 14:02     ` Andy Shevchenko
2020-10-09 16:02       ` Andy Shevchenko
2021-09-16 12:51 ` Florian Eckert
2021-09-22  6:36   ` Andy Shevchenko
2021-09-22  7:53     ` Andy Shevchenko
2021-09-23 14:17       ` Add a SSDT ACPI description to recognize my I2C device connected via SMBus Florian Eckert
2021-09-23 16:24         ` Mika Westerberg
2021-09-23 20:26         ` Andy Shevchenko
2021-09-23 20:29           ` Andy Shevchenko
2021-09-29 22:40           ` Florian Eckert
2021-09-30  6:15             ` Andy Shevchenko
2021-09-30  6:19               ` Andy Shevchenko

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=20200407173849.43628-8-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    /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).