Skip to content

Commit

Permalink
pinctrl: mcp23s08: Use for_each_set_bit() and hweight_long()
Browse files Browse the repository at this point in the history
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>
Link: https://lore.kernel.org/r/20200407173849.43628-8-andriy.shevchenko@linux.intel.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
  • Loading branch information
Andy Shevchenko authored and Linus Walleij committed Apr 16, 2020
1 parent 1ac30db commit 7b04aaa
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions drivers/pinctrl/pinctrl-mcp23s08.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -940,43 +941,37 @@ 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)
type = (int)(uintptr_t)match;
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);
Expand All @@ -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);
Expand Down

0 comments on commit 7b04aaa

Please sign in to comment.