Skip to content

Commit d065f41

Browse files
ruehlchrisbroonie
authored andcommitted
spi: spi-rockchip: add support for spi slave mode
Add support for spi slave mode in spi-rockchip. The register map has an entry for it. If spi-slave is set in dts, set this corresponding bit and add to mode_bits the SPI_NO_CS, allow slave mode without explicit CS use. Slave abort function had been added. Signed-off-by: Chris Ruehl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent d66571a commit d065f41

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

drivers/spi/spi-rockchip.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ struct rockchip_spi {
183183
u8 rsd;
184184

185185
bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
186+
187+
bool slave_abort;
186188
};
187189

188190
static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
@@ -359,7 +361,7 @@ static void rockchip_spi_dma_rxcb(void *data)
359361
struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
360362
int state = atomic_fetch_andnot(RXDMA, &rs->state);
361363

362-
if (state & TXDMA)
364+
if (state & TXDMA && !rs->slave_abort)
363365
return;
364366

365367
spi_enable_chip(rs, false);
@@ -372,7 +374,7 @@ static void rockchip_spi_dma_txcb(void *data)
372374
struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
373375
int state = atomic_fetch_andnot(TXDMA, &rs->state);
374376

375-
if (state & RXDMA)
377+
if (state & RXDMA && !rs->slave_abort)
376378
return;
377379

378380
/* Wait until the FIFO data completely. */
@@ -457,7 +459,7 @@ static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
457459

458460
static void rockchip_spi_config(struct rockchip_spi *rs,
459461
struct spi_device *spi, struct spi_transfer *xfer,
460-
bool use_dma)
462+
bool use_dma, bool slave_mode)
461463
{
462464
u32 cr0 = CR0_FRF_SPI << CR0_FRF_OFFSET
463465
| CR0_BHT_8BIT << CR0_BHT_OFFSET
@@ -466,6 +468,10 @@ static void rockchip_spi_config(struct rockchip_spi *rs,
466468
u32 cr1;
467469
u32 dmacr = 0;
468470

471+
if (slave_mode)
472+
cr0 |= CR0_OPM_SLAVE << CR0_OPM_OFFSET;
473+
rs->slave_abort = false;
474+
469475
cr0 |= rs->rsd << CR0_RSD_OFFSET;
470476
cr0 |= (spi->mode & 0x3U) << CR0_SCPH_OFFSET;
471477
if (spi->mode & SPI_LSB_FIRST)
@@ -535,6 +541,16 @@ static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
535541
return ROCKCHIP_SPI_MAX_TRANLEN;
536542
}
537543

544+
static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
545+
{
546+
struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
547+
548+
rs->slave_abort = true;
549+
complete(&ctlr->xfer_completion);
550+
551+
return 0;
552+
}
553+
538554
static int rockchip_spi_transfer_one(
539555
struct spi_controller *ctlr,
540556
struct spi_device *spi,
@@ -560,7 +576,7 @@ static int rockchip_spi_transfer_one(
560576

561577
use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
562578

563-
rockchip_spi_config(rs, spi, xfer, use_dma);
579+
rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
564580

565581
if (use_dma)
566582
return rockchip_spi_prepare_dma(rs, ctlr, xfer);
@@ -588,15 +604,26 @@ static int rockchip_spi_probe(struct platform_device *pdev)
588604
struct rockchip_spi *rs;
589605
struct spi_controller *ctlr;
590606
struct resource *mem;
607+
struct device_node *np = pdev->dev.of_node;
591608
u32 rsd_nsecs;
609+
bool slave_mode;
610+
611+
slave_mode = of_property_read_bool(np, "spi-slave");
612+
613+
if (slave_mode)
614+
ctlr = spi_alloc_slave(&pdev->dev,
615+
sizeof(struct rockchip_spi));
616+
else
617+
ctlr = spi_alloc_master(&pdev->dev,
618+
sizeof(struct rockchip_spi));
592619

593-
ctlr = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
594620
if (!ctlr)
595621
return -ENOMEM;
596622

597623
platform_set_drvdata(pdev, ctlr);
598624

599625
rs = spi_controller_get_devdata(ctlr);
626+
ctlr->slave = slave_mode;
600627

601628
/* Get basic io resource and map it */
602629
mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -676,6 +703,12 @@ static int rockchip_spi_probe(struct platform_device *pdev)
676703
ctlr->auto_runtime_pm = true;
677704
ctlr->bus_num = pdev->id;
678705
ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
706+
if (slave_mode) {
707+
ctlr->mode_bits |= SPI_NO_CS;
708+
ctlr->slave_abort = rockchip_spi_slave_abort;
709+
} else {
710+
ctlr->flags = SPI_MASTER_GPIO_SS;
711+
}
679712
ctlr->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
680713
ctlr->dev.of_node = pdev->dev.of_node;
681714
ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
@@ -686,7 +719,6 @@ static int rockchip_spi_probe(struct platform_device *pdev)
686719
ctlr->transfer_one = rockchip_spi_transfer_one;
687720
ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
688721
ctlr->handle_err = rockchip_spi_handle_err;
689-
ctlr->flags = SPI_MASTER_GPIO_SS;
690722

691723
ctlr->dma_tx = dma_request_chan(rs->dev, "tx");
692724
if (IS_ERR(ctlr->dma_tx)) {

0 commit comments

Comments
 (0)