Lines Matching +full:cdr +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 - 2014 Allwinner Tech
7 * Maxime Ripard <maxime.ripard@free-electrons.com>
92 return readl(sspi->base_addr + reg); in sun4i_spi_read()
97 writel(value, sspi->base_addr + reg); in sun4i_spi_write()
138 while (len--) { in sun4i_spi_drain_fifo()
139 byte = readb(sspi->base_addr + SUN4I_RXDATA_REG); in sun4i_spi_drain_fifo()
140 if (sspi->rx_buf) in sun4i_spi_drain_fifo()
141 *sspi->rx_buf++ = byte; in sun4i_spi_drain_fifo()
151 cnt = SUN4I_FIFO_DEPTH - sun4i_spi_get_tx_fifo_count(sspi); in sun4i_spi_fill_fifo()
153 len = min3(len, (int)cnt, sspi->len); in sun4i_spi_fill_fifo()
155 while (len--) { in sun4i_spi_fill_fifo()
156 byte = sspi->tx_buf ? *sspi->tx_buf++ : 0; in sun4i_spi_fill_fifo()
157 writeb(byte, sspi->base_addr + SUN4I_TXDATA_REG); in sun4i_spi_fill_fifo()
158 sspi->len--; in sun4i_spi_fill_fifo()
164 struct sun4i_spi *sspi = spi_controller_get_devdata(spi->controller); in sun4i_spi_set_cs()
191 if (spi->mode & SPI_CS_HIGH) in sun4i_spi_set_cs()
201 return SUN4I_MAX_XFER_SIZE - 1; in sun4i_spi_max_transfer_size()
217 if (tfr->len > SUN4I_MAX_XFER_SIZE) in sun4i_spi_transfer_one()
218 return -EMSGSIZE; in sun4i_spi_transfer_one()
220 if (tfr->tx_buf && tfr->len >= SUN4I_MAX_XFER_SIZE) in sun4i_spi_transfer_one()
221 return -EMSGSIZE; in sun4i_spi_transfer_one()
223 reinit_completion(&sspi->done); in sun4i_spi_transfer_one()
224 sspi->tx_buf = tfr->tx_buf; in sun4i_spi_transfer_one()
225 sspi->rx_buf = tfr->rx_buf; in sun4i_spi_transfer_one()
226 sspi->len = tfr->len; in sun4i_spi_transfer_one()
242 if (spi->mode & SPI_CPOL) in sun4i_spi_transfer_one()
247 if (spi->mode & SPI_CPHA) in sun4i_spi_transfer_one()
252 if (spi->mode & SPI_LSB_FIRST) in sun4i_spi_transfer_one()
262 if (sspi->rx_buf) in sun4i_spi_transfer_one()
270 mclk_rate = clk_get_rate(sspi->mclk); in sun4i_spi_transfer_one()
271 if (mclk_rate < (2 * tfr->speed_hz)) { in sun4i_spi_transfer_one()
272 clk_set_rate(sspi->mclk, 2 * tfr->speed_hz); in sun4i_spi_transfer_one()
273 mclk_rate = clk_get_rate(sspi->mclk); in sun4i_spi_transfer_one()
281 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1)) in sun4i_spi_transfer_one()
283 * SPI_CLK = MOD_CLK / (2 * (cdr + 1)) in sun4i_spi_transfer_one()
290 div = mclk_rate / (2 * tfr->speed_hz); in sun4i_spi_transfer_one()
293 div--; in sun4i_spi_transfer_one()
297 div = ilog2(mclk_rate) - ilog2(tfr->speed_hz); in sun4i_spi_transfer_one()
304 if (sspi->tx_buf) in sun4i_spi_transfer_one()
305 tx_len = tfr->len; in sun4i_spi_transfer_one()
308 sun4i_spi_write(sspi, SUN4I_BURST_CNT_REG, SUN4I_BURST_CNT(tfr->len)); in sun4i_spi_transfer_one()
316 sun4i_spi_fill_fifo(sspi, SUN4I_FIFO_DEPTH - 1); in sun4i_spi_transfer_one()
329 tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U); in sun4i_spi_transfer_one()
331 time_left = wait_for_completion_timeout(&sspi->done, in sun4i_spi_transfer_one()
335 dev_warn(&host->dev, in sun4i_spi_transfer_one()
337 dev_name(&spi->dev), tfr->len, tfr->speed_hz, in sun4i_spi_transfer_one()
338 jiffies_to_msecs(end - start), tx_time); in sun4i_spi_transfer_one()
339 ret = -ETIMEDOUT; in sun4i_spi_transfer_one()
359 complete(&sspi->done); in sun4i_spi_handler()
375 if (!sspi->len) in sun4i_spi_handler()
379 /* Only clear the interrupt _after_ re-seeding the FIFO */ in sun4i_spi_handler()
394 ret = clk_prepare_enable(sspi->hclk); in sun4i_spi_runtime_resume()
400 ret = clk_prepare_enable(sspi->mclk); in sun4i_spi_runtime_resume()
412 clk_disable_unprepare(sspi->hclk); in sun4i_spi_runtime_resume()
422 clk_disable_unprepare(sspi->mclk); in sun4i_spi_runtime_suspend()
423 clk_disable_unprepare(sspi->hclk); in sun4i_spi_runtime_suspend()
434 host = spi_alloc_host(&pdev->dev, sizeof(struct sun4i_spi)); in sun4i_spi_probe()
436 dev_err(&pdev->dev, "Unable to allocate SPI Host\n"); in sun4i_spi_probe()
437 return -ENOMEM; in sun4i_spi_probe()
443 sspi->base_addr = devm_platform_ioremap_resource(pdev, 0); in sun4i_spi_probe()
444 if (IS_ERR(sspi->base_addr)) { in sun4i_spi_probe()
445 ret = PTR_ERR(sspi->base_addr); in sun4i_spi_probe()
451 ret = -ENXIO; in sun4i_spi_probe()
455 ret = devm_request_irq(&pdev->dev, irq, sun4i_spi_handler, in sun4i_spi_probe()
456 0, "sun4i-spi", sspi); in sun4i_spi_probe()
458 dev_err(&pdev->dev, "Cannot request IRQ\n"); in sun4i_spi_probe()
462 sspi->host = host; in sun4i_spi_probe()
463 host->max_speed_hz = 100 * 1000 * 1000; in sun4i_spi_probe()
464 host->min_speed_hz = 3 * 1000; in sun4i_spi_probe()
465 host->set_cs = sun4i_spi_set_cs; in sun4i_spi_probe()
466 host->transfer_one = sun4i_spi_transfer_one; in sun4i_spi_probe()
467 host->num_chipselect = 4; in sun4i_spi_probe()
468 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; in sun4i_spi_probe()
469 host->bits_per_word_mask = SPI_BPW_MASK(8); in sun4i_spi_probe()
470 host->dev.of_node = pdev->dev.of_node; in sun4i_spi_probe()
471 host->auto_runtime_pm = true; in sun4i_spi_probe()
472 host->max_transfer_size = sun4i_spi_max_transfer_size; in sun4i_spi_probe()
474 sspi->hclk = devm_clk_get(&pdev->dev, "ahb"); in sun4i_spi_probe()
475 if (IS_ERR(sspi->hclk)) { in sun4i_spi_probe()
476 dev_err(&pdev->dev, "Unable to acquire AHB clock\n"); in sun4i_spi_probe()
477 ret = PTR_ERR(sspi->hclk); in sun4i_spi_probe()
481 sspi->mclk = devm_clk_get(&pdev->dev, "mod"); in sun4i_spi_probe()
482 if (IS_ERR(sspi->mclk)) { in sun4i_spi_probe()
483 dev_err(&pdev->dev, "Unable to acquire module clock\n"); in sun4i_spi_probe()
484 ret = PTR_ERR(sspi->mclk); in sun4i_spi_probe()
488 init_completion(&sspi->done); in sun4i_spi_probe()
491 * This wake-up/shutdown pattern is to be able to have the in sun4i_spi_probe()
494 ret = sun4i_spi_runtime_resume(&pdev->dev); in sun4i_spi_probe()
496 dev_err(&pdev->dev, "Couldn't resume the device\n"); in sun4i_spi_probe()
500 pm_runtime_set_active(&pdev->dev); in sun4i_spi_probe()
501 pm_runtime_enable(&pdev->dev); in sun4i_spi_probe()
502 pm_runtime_idle(&pdev->dev); in sun4i_spi_probe()
504 ret = devm_spi_register_controller(&pdev->dev, host); in sun4i_spi_probe()
506 dev_err(&pdev->dev, "cannot register SPI host\n"); in sun4i_spi_probe()
513 pm_runtime_disable(&pdev->dev); in sun4i_spi_probe()
514 sun4i_spi_runtime_suspend(&pdev->dev); in sun4i_spi_probe()
522 pm_runtime_force_suspend(&pdev->dev); in sun4i_spi_remove()
526 { .compatible = "allwinner,sun4i-a10-spi", },
540 .name = "sun4i-spi",
548 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");