Commit 7b04aaaf authored by Andy Shevchenko's avatar Andy Shevchenko Committed by Linus Walleij

pinctrl: mcp23s08: Use for_each_set_bit() and hweight_long()

Here is a simplification of SPI code by using for_each_set_bit() and
hweight_long() library functions.
Signed-off-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20200407173849.43628-8-andriy.shevchenko@linux.intel.comSigned-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 1ac30db2
// SPDX-License-Identifier: GPL-2.0-only // SPDX-License-Identifier: GPL-2.0-only
/* MCP23S08 SPI/I2C GPIO driver */ /* MCP23S08 SPI/I2C GPIO driver */
#include <linux/bitops.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/mutex.h> #include <linux/mutex.h>
...@@ -940,13 +941,14 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev, ...@@ -940,13 +941,14 @@ static int mcp23s08_spi_regmap_init(struct mcp23s08 *mcp, struct device *dev,
static int mcp23s08_probe(struct spi_device *spi) static int mcp23s08_probe(struct spi_device *spi)
{ {
struct device *dev = &spi->dev; struct device *dev = &spi->dev;
unsigned long spi_present_mask;
const void *match; const void *match;
int chips;
u32 v;
unsigned addr; unsigned addr;
int chips = 0;
struct mcp23s08_driver_data *data; struct mcp23s08_driver_data *data;
int status, type; int status, type;
unsigned ngpio = 0; unsigned ngpio = 0;
u32 spi_present_mask;
match = device_get_match_data(dev); match = device_get_match_data(dev);
if (match) if (match)
...@@ -954,29 +956,22 @@ static int mcp23s08_probe(struct spi_device *spi) ...@@ -954,29 +956,22 @@ static int mcp23s08_probe(struct spi_device *spi)
else else
type = spi_get_device_id(spi)->driver_data; type = spi_get_device_id(spi)->driver_data;
status = device_property_read_u32(&spi->dev, status = device_property_read_u32(dev, "microchip,spi-present-mask", &v);
"microchip,spi-present-mask", &spi_present_mask);
if (status) { if (status) {
status = device_property_read_u32(&spi->dev, status = device_property_read_u32(dev, "mcp,spi-present-mask", &v);
"mcp,spi-present-mask", &spi_present_mask);
if (status) { if (status) {
dev_err(&spi->dev, "missing spi-present-mask"); dev_err(&spi->dev, "missing spi-present-mask");
return status; 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"); dev_err(&spi->dev, "invalid spi-present-mask");
return -ENODEV; return -ENODEV;
} }
for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { chips = hweight_long(spi_present_mask);
if (spi_present_mask & BIT(addr))
chips++;
}
if (!chips)
return -ENODEV;
data = devm_kzalloc(&spi->dev, data = devm_kzalloc(&spi->dev,
struct_size(data, chip, chips), GFP_KERNEL); struct_size(data, chip, chips), GFP_KERNEL);
...@@ -985,11 +980,8 @@ static int mcp23s08_probe(struct spi_device *spi) ...@@ -985,11 +980,8 @@ static int mcp23s08_probe(struct spi_device *spi)
spi_set_drvdata(spi, data); spi_set_drvdata(spi, data);
for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) { for_each_set_bit(addr, &spi_present_mask, MCP_MAX_DEV_PER_CS) {
if (!(spi_present_mask & BIT(addr))) data->mcp[addr] = &data->chip[--chips];
continue;
chips--;
data->mcp[addr] = &data->chip[chips];
data->mcp[addr]->irq = spi->irq; data->mcp[addr]->irq = spi->irq;
status = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type); status = mcp23s08_spi_regmap_init(data->mcp[addr], dev, addr, type);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment