Skip to content

Commit

Permalink
spi: Replace all spi->chip_select and spi->cs_gpiod references with f…
Browse files Browse the repository at this point in the history
…unction call

Supporting multi-cs in spi drivers would require the chip_select & cs_gpiod
members of struct spi_device to be an array. But changing the type of these
members to array would break the spi driver functionality. To make the
transition smoother introduced four new APIs to get/set the
spi->chip_select & spi->cs_gpiod and replaced all spi->chip_select and
spi->cs_gpiod references with get or set API calls.
While adding multi-cs support in further patches the chip_select & cs_gpiod
members of the spi_device structure would be converted to arrays & the
"idx" parameter of the APIs would be used as array index i.e.,
spi->chip_select[idx] & spi->cs_gpiod[idx] respectively.

Signed-off-by: Amit Kumar Mahapatra <[email protected]>
Acked-by: Heiko Stuebner <[email protected]> # Rockchip drivers
Reviewed-by: Michal Simek <[email protected]>
Reviewed-by: Cédric Le Goater <[email protected]> # Aspeed driver
Reviewed-by: Dhruva Gole <[email protected]> # SPI Cadence QSPI
Reviewed-by: Patrice Chotard <[email protected]> # spi-stm32-qspi
Acked-by: William Zhang <[email protected]> # bcm63xx-hsspi driver
Reviewed-by: Serge Semin <[email protected]> # DW SSI part
Link: https://lore.kernel.org/r/167847070432.26.15076794204368669839@mailman-core.alsa-project.org
Signed-off-by: Mark Brown <[email protected]>
  • Loading branch information
Amit Kumar Mahapatra via Alsa-devel authored and broonie committed Mar 11, 2023
1 parent 21d19e6 commit 9e264f3
Show file tree
Hide file tree
Showing 92 changed files with 333 additions and 328 deletions.
2 changes: 1 addition & 1 deletion drivers/spi/spi-altera-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void altera_spi_set_cs(struct spi_device *spi, bool is_high)
altr_spi_writel(hw, ALTERA_SPI_TARGET_SEL, 0);
} else {
altr_spi_writel(hw, ALTERA_SPI_TARGET_SEL,
BIT(spi->chip_select));
BIT(spi_get_chipselect(spi, 0)));
hw->imr |= ALTERA_SPI_CONTROL_SSO_MSK;
altr_spi_writel(hw, ALTERA_SPI_CONTROL, hw->imr);
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi-amd.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
case AMD_SPI_V1:
break;
case AMD_SPI_V2:
amd_spi_clear_chip(amd_spi, message->spi->chip_select);
amd_spi_clear_chip(amd_spi, spi_get_chipselect(message->spi, 0));
break;
default:
return -ENODEV;
Expand All @@ -364,7 +364,7 @@ static int amd_spi_master_transfer(struct spi_master *master,
struct amd_spi *amd_spi = spi_master_get_devdata(master);
struct spi_device *spi = msg->spi;

amd_spi_select_chip(amd_spi, spi->chip_select);
amd_spi_select_chip(amd_spi, spi_get_chipselect(spi, 0));

/*
* Extract spi_transfers from the spi message and
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-ar934x.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static int ar934x_spi_transfer_one_message(struct spi_controller *ctlr,
iowrite32(reg, sp->base + AR934X_SPI_DATAOUT);
}

reg = AR934X_SPI_SHIFT_VAL(spi->chip_select, term,
reg = AR934X_SPI_SHIFT_VAL(spi_get_chipselect(spi, 0), term,
trx_cur * 8);
iowrite32(reg, sp->base + AR934X_SPI_REG_SHIFT_CTRL);
stat = readl_poll_timeout(
Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi-armada-3700.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
struct a3700_spi *a3700_spi = spi_controller_get_devdata(spi->controller);

if (!enable)
a3700_spi_activate_cs(a3700_spi, spi->chip_select);
a3700_spi_activate_cs(a3700_spi, spi_get_chipselect(spi, 0));
else
a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
a3700_spi_deactivate_cs(a3700_spi, spi_get_chipselect(spi, 0));
}

static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
Expand Down
13 changes: 7 additions & 6 deletions drivers/spi/spi-aspeed-smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static const struct aspeed_spi_data ast2400_spi_data;
static int do_aspeed_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
{
struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->master);
struct aspeed_spi_chip *chip = &aspi->chips[mem->spi->chip_select];
struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(mem->spi, 0)];
u32 addr_mode, addr_mode_backup;
u32 ctl_val;
int ret = 0;
Expand Down Expand Up @@ -377,7 +377,8 @@ static const char *aspeed_spi_get_name(struct spi_mem *mem)
struct aspeed_spi *aspi = spi_controller_get_devdata(mem->spi->master);
struct device *dev = aspi->dev;

return devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev), mem->spi->chip_select);
return devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
spi_get_chipselect(mem->spi, 0));
}

struct aspeed_spi_window {
Expand Down Expand Up @@ -553,7 +554,7 @@ static int aspeed_spi_do_calibration(struct aspeed_spi_chip *chip);
static int aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
{
struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->master);
struct aspeed_spi_chip *chip = &aspi->chips[desc->mem->spi->chip_select];
struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];
struct spi_mem_op *op = &desc->info.op_tmpl;
u32 ctl_val;
int ret = 0;
Expand Down Expand Up @@ -620,7 +621,7 @@ static ssize_t aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc *desc,
u64 offset, size_t len, void *buf)
{
struct aspeed_spi *aspi = spi_controller_get_devdata(desc->mem->spi->master);
struct aspeed_spi_chip *chip = &aspi->chips[desc->mem->spi->chip_select];
struct aspeed_spi_chip *chip = &aspi->chips[spi_get_chipselect(desc->mem->spi, 0)];

/* Switch to USER command mode if mapping window is too small */
if (chip->ahb_window_size < offset + len) {
Expand Down Expand Up @@ -670,7 +671,7 @@ static int aspeed_spi_setup(struct spi_device *spi)
{
struct aspeed_spi *aspi = spi_controller_get_devdata(spi->master);
const struct aspeed_spi_data *data = aspi->data;
unsigned int cs = spi->chip_select;
unsigned int cs = spi_get_chipselect(spi, 0);
struct aspeed_spi_chip *chip = &aspi->chips[cs];

chip->aspi = aspi;
Expand All @@ -697,7 +698,7 @@ static int aspeed_spi_setup(struct spi_device *spi)
static void aspeed_spi_cleanup(struct spi_device *spi)
{
struct aspeed_spi *aspi = spi_controller_get_devdata(spi->master);
unsigned int cs = spi->chip_select;
unsigned int cs = spi_get_chipselect(spi, 0);

aspeed_spi_chip_enable(aspi, cs, false);

Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-at91-usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static int at91_usart_spi_setup(struct spi_device *spi)

dev_dbg(&spi->dev,
"setup: bpw %u mode 0x%x -> mr %d %08x\n",
spi->bits_per_word, spi->mode, spi->chip_select, mr);
spi->bits_per_word, spi->mode, spi_get_chipselect(spi, 0), mr);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi-ath79.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
{
struct ath79_spi *sp = ath79_spidev_to_sp(spi);
int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
u32 cs_bit = AR71XX_SPI_IOC_CS(spi_get_chipselect(spi, 0));

if (cs_high)
sp->ioc_base |= cs_bit;
Expand Down Expand Up @@ -140,7 +140,7 @@ static int ath79_exec_mem_op(struct spi_mem *mem,
struct ath79_spi *sp = ath79_spidev_to_sp(mem->spi);

/* Ensures that reading is performed on device connected to hardware cs0 */
if (mem->spi->chip_select || mem->spi->cs_gpiod)
if (spi_get_chipselect(mem->spi, 0) || spi_get_csgpiod(mem->spi, 0))
return -ENOTSUPP;

/* Only use for fast-read op. */
Expand Down
26 changes: 13 additions & 13 deletions drivers/spi/spi-atmel.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
int chip_select;
u32 mr;

if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
chip_select = as->native_cs_for_gpio;
else
chip_select = spi->chip_select;
chip_select = spi_get_chipselect(spi, 0);

if (atmel_spi_is_v2(as)) {
spi_writel(as, CSR0 + 4 * chip_select, asd->csr);
Expand Down Expand Up @@ -378,10 +378,10 @@ static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
int chip_select;
u32 mr;

if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
chip_select = as->native_cs_for_gpio;
else
chip_select = spi->chip_select;
chip_select = spi_get_chipselect(spi, 0);

/* only deactivate *this* device; sometimes transfers to
* another device may be active when this routine is called.
Expand All @@ -394,7 +394,7 @@ static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)

dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr);

if (!spi->cs_gpiod)
if (!spi_get_csgpiod(spi, 0))
spi_writel(as, CR, SPI_BIT(LASTXFER));
}

Expand Down Expand Up @@ -800,10 +800,10 @@ static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
unsigned long bus_hz;
int chip_select;

if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
chip_select = as->native_cs_for_gpio;
else
chip_select = spi->chip_select;
chip_select = spi_get_chipselect(spi, 0);

/* v1 chips start out at half the peripheral bus speed. */
bus_hz = as->spi_clk;
Expand Down Expand Up @@ -1189,7 +1189,7 @@ static int atmel_spi_setup(struct spi_device *spi)
as = spi_controller_get_devdata(spi->controller);

/* see notes above re chipselect */
if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH)) {
if (!spi_get_csgpiod(spi, 0) && (spi->mode & SPI_CS_HIGH)) {
dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n");
return -EINVAL;
}
Expand All @@ -1201,24 +1201,24 @@ static int atmel_spi_setup(struct spi_device *spi)
*/
initialize_native_cs_for_gpio(as);

if (spi->cs_gpiod && as->native_cs_free) {
if (spi_get_csgpiod(spi, 0) && as->native_cs_free) {
dev_err(&spi->dev,
"No native CS available to support this GPIO CS\n");
return -EBUSY;
}

if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
chip_select = as->native_cs_for_gpio;
else
chip_select = spi->chip_select;
chip_select = spi_get_chipselect(spi, 0);

csr = SPI_BF(BITS, bits - 8);
if (spi->mode & SPI_CPOL)
csr |= SPI_BIT(CPOL);
if (!(spi->mode & SPI_CPHA))
csr |= SPI_BIT(NCPHA);

if (!spi->cs_gpiod)
if (!spi_get_csgpiod(spi, 0))
csr |= SPI_BIT(CSAAT);
csr |= SPI_BF(DLYBS, 0);

Expand All @@ -1244,7 +1244,7 @@ static int atmel_spi_setup(struct spi_device *spi)

dev_dbg(&spi->dev,
"setup: bpw %u mode 0x%x -> csr%d %08x\n",
bits, spi->mode, spi->chip_select, csr);
bits, spi->mode, spi_get_chipselect(spi, 0), csr);

if (!atmel_spi_is_v2(as))
spi_writel(as, CSR0 + 4 * chip_select, csr);
Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi-au1550.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static void au1550_spi_chipsel(struct spi_device *spi, int value)
switch (value) {
case BITBANG_CS_INACTIVE:
if (hw->pdata->deactivate_cs)
hw->pdata->deactivate_cs(hw->pdata, spi->chip_select,
hw->pdata->deactivate_cs(hw->pdata, spi_get_chipselect(spi, 0),
cspol);
break;

Expand Down Expand Up @@ -211,7 +211,7 @@ static void au1550_spi_chipsel(struct spi_device *spi, int value)
} while ((stat & PSC_SPISTAT_DR) == 0);

if (hw->pdata->activate_cs)
hw->pdata->activate_cs(hw->pdata, spi->chip_select,
hw->pdata->activate_cs(hw->pdata, spi_get_chipselect(spi, 0),
cspol);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/spi-axi-spi-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static void spi_engine_gen_cs(struct spi_engine_program *p, bool dry,
unsigned int mask = 0xff;

if (assert)
mask ^= BIT(spi->chip_select);
mask ^= BIT(spi_get_chipselect(spi, 0));

spi_engine_program_add_cmd(p, dry, SPI_ENGINE_CMD_ASSERT(1, mask));
}
Expand Down
10 changes: 5 additions & 5 deletions drivers/spi/spi-bcm-qspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi)
if (has_bspi(qspi))
mspi_cdram &= ~1;
else
mspi_cdram |= (~(1 << spi->chip_select) &
mspi_cdram |= (~(1 << spi_get_chipselect(spi, 0)) &
MSPI_CDRAM_PCS);

write_cdram_slot(qspi, slot, mspi_cdram);
Expand Down Expand Up @@ -1046,8 +1046,8 @@ static int bcm_qspi_bspi_exec_mem_op(struct spi_device *spi,
return -EIO;

from = op->addr.val;
if (!spi->cs_gpiod)
bcm_qspi_chip_select(qspi, spi->chip_select);
if (!spi_get_csgpiod(spi, 0))
bcm_qspi_chip_select(qspi, spi_get_chipselect(spi, 0));
bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);

/*
Expand Down Expand Up @@ -1126,8 +1126,8 @@ static int bcm_qspi_transfer_one(struct spi_master *master,
int slots;
unsigned long timeo = msecs_to_jiffies(100);

if (!spi->cs_gpiod)
bcm_qspi_chip_select(qspi, spi->chip_select);
if (!spi_get_csgpiod(spi, 0))
bcm_qspi_chip_select(qspi, spi_get_chipselect(spi, 0));
qspi->trans_pos.trans = trans;
qspi->trans_pos.byte = 0;

Expand Down
19 changes: 10 additions & 9 deletions drivers/spi/spi-bcm2835.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,9 @@ static int bcm2835_spi_setup(struct spi_device *spi)
* The SPI core has successfully requested the CS GPIO line from the
* device tree, so we are done.
*/
if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
return 0;
if (spi->chip_select > 1) {
if (spi_get_chipselect(spi, 0) > 1) {
/* error in the case of native CS requested with CS > 1
* officially there is a CS2, but it is not documented
* which GPIO is connected with that...
Expand All @@ -1301,18 +1301,19 @@ static int bcm2835_spi_setup(struct spi_device *spi)
if (!chip)
return 0;

spi->cs_gpiod = gpiochip_request_own_desc(chip, 8 - spi->chip_select,
DRV_NAME,
GPIO_LOOKUP_FLAGS_DEFAULT,
GPIOD_OUT_LOW);
if (IS_ERR(spi->cs_gpiod)) {
ret = PTR_ERR(spi->cs_gpiod);
spi_set_csgpiod(spi, 0, gpiochip_request_own_desc(chip,
8 - (spi_get_chipselect(spi, 0)),
DRV_NAME,
GPIO_LOOKUP_FLAGS_DEFAULT,
GPIOD_OUT_LOW));
if (IS_ERR(spi_get_csgpiod(spi, 0))) {
ret = PTR_ERR(spi_get_csgpiod(spi, 0));
goto err_cleanup;
}

/* and set up the "mode" and level */
dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
spi->chip_select);
spi_get_chipselect(spi, 0));

return 0;

Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi-bcm2835aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static int bcm2835aux_spi_setup(struct spi_device *spi)
if (spi->mode & SPI_NO_CS)
return 0;

if (spi->cs_gpiod)
if (spi_get_csgpiod(spi, 0))
return 0;

/* for dt-backwards compatibility: only support native on CS0
Expand All @@ -465,7 +465,7 @@ static int bcm2835aux_spi_setup(struct spi_device *spi)
dev_warn(&spi->dev,
"Native CS is not supported - please configure cs-gpio in device-tree\n");

if (spi->chip_select == 0)
if (spi_get_chipselect(spi, 0) == 0)
return 0;

dev_warn(&spi->dev, "Native CS is not working for cs > 0\n");
Expand Down
Loading

0 comments on commit 9e264f3

Please sign in to comment.