Skip to content

Commit 23495b8

Browse files
sergey-suloevVincent-FK
authored andcommitted
spi: sun6i: use completion provided by SPI core
As long as the completion is already provided by the SPI core then there is no need to waste extra-memory on this. Also a waiting function was added to avoid code duplication. Changes in v2: 1) Fixed issue with passing an invalid argument into devm_request_irq() function. Signed-off-by: Sergey Suloev <ssuloev@orpaltech.com>
1 parent 6839cad commit 23495b8

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

drivers/spi/spi-sun6i.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ struct sun6i_spi {
9999
struct clk *mclk;
100100
struct reset_control *rstc;
101101

102-
struct completion done;
103-
104102
const u8 *tx_buf;
105103
u8 *rx_buf;
106104
int len;
@@ -246,6 +244,30 @@ static int sun6i_spi_prepare_message(struct spi_master *master,
246244
return 0;
247245
}
248246

247+
static int sun6i_spi_wait_for_transfer(struct spi_device *spi,
248+
struct spi_transfer *tfr)
249+
{
250+
struct spi_master *master = spi->master;
251+
unsigned int start, end, tx_time;
252+
unsigned int timeout;
253+
254+
/* smart wait for completion */
255+
tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
256+
start = jiffies;
257+
timeout = wait_for_completion_timeout(&master->xfer_completion,
258+
msecs_to_jiffies(tx_time));
259+
end = jiffies;
260+
if (!timeout) {
261+
dev_warn(&master->dev,
262+
"%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
263+
dev_name(&spi->dev), tfr->len, tfr->speed_hz,
264+
jiffies_to_msecs(end - start), tx_time);
265+
return -ETIMEDOUT;
266+
}
267+
268+
return 0;
269+
}
270+
249271
static int sun6i_spi_transfer_one(struct spi_master *master,
250272
struct spi_device *spi,
251273
struct spi_transfer *tfr)
@@ -266,7 +288,6 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
266288
if (tfr->len > sspi->fifo_depth)
267289
return -EMSGSIZE;
268290

269-
reinit_completion(&sspi->done);
270291
sspi->tx_buf = tfr->tx_buf;
271292
sspi->rx_buf = tfr->rx_buf;
272293
sspi->len = tfr->len;
@@ -346,29 +367,18 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
346367
reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
347368
sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
348369

349-
tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
350-
start = jiffies;
351-
timeout = wait_for_completion_timeout(&sspi->done,
352-
msecs_to_jiffies(tx_time));
353-
end = jiffies;
354-
if (!timeout) {
355-
dev_warn(&master->dev,
356-
"%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
357-
dev_name(&spi->dev), tfr->len, tfr->speed_hz,
358-
jiffies_to_msecs(end - start), tx_time);
359-
ret = -ETIMEDOUT;
360-
goto out;
361-
}
370+
/* Wait for completion */
371+
ret = sun6i_spi_wait_for_transfer(spi, tfr);
362372

363-
out:
364373
sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
365374

366375
return ret;
367376
}
368377

369378
static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
370379
{
371-
struct sun6i_spi *sspi = dev_id;
380+
struct spi_master *master = dev_id;
381+
struct sun6i_spi *sspi = spi_master_get_devdata(master);
372382
u32 status;
373383

374384
status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
@@ -377,7 +387,7 @@ static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
377387
if (status & SUN6I_INT_CTL_TC) {
378388
sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
379389
sun6i_spi_drain_fifo(sspi, sspi->fifo_depth);
380-
complete(&sspi->done);
390+
spi_finalize_current_transfer(master);
381391
return IRQ_HANDLED;
382392
}
383393

@@ -476,7 +486,7 @@ static int sun6i_spi_probe(struct platform_device *pdev)
476486
}
477487

478488
ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
479-
0, dev_name(&pdev->dev), sspi);
489+
0, dev_name(&pdev->dev), master);
480490
if (ret) {
481491
dev_err(&pdev->dev, "Cannot request IRQ\n");
482492
goto err_free_master;
@@ -498,8 +508,6 @@ static int sun6i_spi_probe(struct platform_device *pdev)
498508
goto err_free_master;
499509
}
500510

501-
init_completion(&sspi->done);
502-
503511
sspi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
504512
if (IS_ERR(sspi->rstc)) {
505513
dev_err(&pdev->dev, "Couldn't get reset controller\n");

0 commit comments

Comments
 (0)