Lines Matching +full:spi +full:- +full:clk

1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for AT91 USART Controllers as SPI
9 #include <linux/clk.h>
12 #include <linux/dma-direction.h>
22 #include <linux/spi/spi.h>
70 readl_relaxed((port)->regs + US_##reg)
72 writel_relaxed((value), (port)->regs + US_##reg)
75 readb_relaxed((port)->regs + US_##reg)
77 writeb_relaxed((value), (port)->regs + US_##reg)
84 struct clk *clk; member
110 aus->current_rx_remaining_bytes = 0; in dma_callback()
111 complete(&aus->xfer_completion); in dma_callback()
115 struct spi_device *spi, in at91_usart_spi_can_dma() argument
120 return aus->use_dma && xfer->len >= US_DMA_MIN_BYTES; in at91_usart_spi_can_dma()
127 struct device *dev = &aus->mpdev->dev; in at91_usart_spi_configure_dma()
128 phys_addr_t phybase = aus->phybase; in at91_usart_spi_configure_dma()
135 ctlr->dma_tx = dma_request_chan(dev, "tx"); in at91_usart_spi_configure_dma()
136 if (IS_ERR_OR_NULL(ctlr->dma_tx)) { in at91_usart_spi_configure_dma()
137 if (IS_ERR(ctlr->dma_tx)) { in at91_usart_spi_configure_dma()
138 err = PTR_ERR(ctlr->dma_tx); in at91_usart_spi_configure_dma()
143 "DMA TX channel not available, SPI unable to use DMA\n"); in at91_usart_spi_configure_dma()
144 err = -EBUSY; in at91_usart_spi_configure_dma()
148 ctlr->dma_rx = dma_request_chan(dev, "rx"); in at91_usart_spi_configure_dma()
149 if (IS_ERR_OR_NULL(ctlr->dma_rx)) { in at91_usart_spi_configure_dma()
150 if (IS_ERR(ctlr->dma_rx)) { in at91_usart_spi_configure_dma()
151 err = PTR_ERR(ctlr->dma_rx); in at91_usart_spi_configure_dma()
156 "DMA RX channel not available, SPI unable to use DMA\n"); in at91_usart_spi_configure_dma()
157 err = -EBUSY; in at91_usart_spi_configure_dma()
170 if (dmaengine_slave_config(ctlr->dma_rx, &slave_config)) { in at91_usart_spi_configure_dma()
171 dev_err(&ctlr->dev, in at91_usart_spi_configure_dma()
173 err = -EINVAL; in at91_usart_spi_configure_dma()
178 if (dmaengine_slave_config(ctlr->dma_tx, &slave_config)) { in at91_usart_spi_configure_dma()
179 dev_err(&ctlr->dev, in at91_usart_spi_configure_dma()
181 err = -EINVAL; in at91_usart_spi_configure_dma()
185 aus->use_dma = true; in at91_usart_spi_configure_dma()
189 if (!IS_ERR_OR_NULL(ctlr->dma_tx)) in at91_usart_spi_configure_dma()
190 dma_release_channel(ctlr->dma_tx); in at91_usart_spi_configure_dma()
191 if (!IS_ERR_OR_NULL(ctlr->dma_rx)) in at91_usart_spi_configure_dma()
192 dma_release_channel(ctlr->dma_rx); in at91_usart_spi_configure_dma()
193 ctlr->dma_tx = NULL; in at91_usart_spi_configure_dma()
194 ctlr->dma_rx = NULL; in at91_usart_spi_configure_dma()
202 if (ctlr->dma_rx) in at91_usart_spi_release_dma()
203 dma_release_channel(ctlr->dma_rx); in at91_usart_spi_release_dma()
204 if (ctlr->dma_tx) in at91_usart_spi_release_dma()
205 dma_release_channel(ctlr->dma_tx); in at91_usart_spi_release_dma()
210 if (ctlr->dma_rx) in at91_usart_spi_stop_dma()
211 dmaengine_terminate_all(ctlr->dma_rx); in at91_usart_spi_stop_dma()
212 if (ctlr->dma_tx) in at91_usart_spi_stop_dma()
213 dmaengine_terminate_all(ctlr->dma_tx); in at91_usart_spi_stop_dma()
220 struct dma_chan *rxchan = ctlr->dma_rx; in at91_usart_spi_dma_transfer()
221 struct dma_chan *txchan = ctlr->dma_tx; in at91_usart_spi_dma_transfer()
230 xfer->rx_sg.sgl, in at91_usart_spi_dma_transfer()
231 xfer->rx_sg.nents, in at91_usart_spi_dma_transfer()
239 xfer->tx_sg.sgl, in at91_usart_spi_dma_transfer()
240 xfer->tx_sg.nents, in at91_usart_spi_dma_transfer()
247 rxdesc->callback = dma_callback; in at91_usart_spi_dma_transfer()
248 rxdesc->callback_param = ctlr; in at91_usart_spi_dma_transfer()
250 cookie = rxdesc->tx_submit(rxdesc); in at91_usart_spi_dma_transfer()
254 cookie = txdesc->tx_submit(txdesc); in at91_usart_spi_dma_transfer()
258 rxchan->device->device_issue_pending(rxchan); in at91_usart_spi_dma_transfer()
259 txchan->device->device_issue_pending(txchan); in at91_usart_spi_dma_transfer()
268 return -ENOMEM; in at91_usart_spi_dma_transfer()
273 return wait_for_completion_timeout(&aus->xfer_completion, in at91_usart_spi_dma_timeout()
279 return aus->status & US_IR_TXRDY; in at91_usart_spi_tx_ready()
284 return aus->status & US_IR_RXRDY; in at91_usart_spi_rx_ready()
289 return aus->status & US_IR_OVRE; in at91_usart_spi_check_overrun()
294 aus->status = at91_usart_spi_readl(aus, CSR); in at91_usart_spi_read_status()
295 return aus->status; in at91_usart_spi_read_status()
300 unsigned int len = aus->current_transfer->len; in at91_usart_spi_tx()
301 unsigned int remaining = aus->current_tx_remaining_bytes; in at91_usart_spi_tx()
302 const u8 *tx_buf = aus->current_transfer->tx_buf; in at91_usart_spi_tx()
308 at91_usart_spi_writeb(aus, THR, tx_buf[len - remaining]); in at91_usart_spi_tx()
309 aus->current_tx_remaining_bytes--; in at91_usart_spi_tx()
315 int len = aus->current_transfer->len; in at91_usart_spi_rx()
316 int remaining = aus->current_rx_remaining_bytes; in at91_usart_spi_rx()
317 u8 *rx_buf = aus->current_transfer->rx_buf; in at91_usart_spi_rx()
322 rx_buf[len - remaining] = at91_usart_spi_readb(aus, RHR); in at91_usart_spi_rx()
323 aus->current_rx_remaining_bytes--; in at91_usart_spi_rx()
331 DIV_ROUND_UP(aus->spi_clk, xfer->speed_hz)); in at91_usart_spi_set_xfer_speed()
339 spin_lock(&aus->lock); in at91_usart_spi_interrupt()
343 aus->xfer_failed = true; in at91_usart_spi_interrupt()
345 spin_unlock(&aus->lock); in at91_usart_spi_interrupt()
351 spin_unlock(&aus->lock); in at91_usart_spi_interrupt()
355 spin_unlock(&aus->lock); in at91_usart_spi_interrupt()
360 static int at91_usart_spi_setup(struct spi_device *spi) in at91_usart_spi_setup() argument
362 struct at91_usart_spi *aus = spi_master_get_devdata(spi->controller); in at91_usart_spi_setup()
363 u32 *ausd = spi->controller_state; in at91_usart_spi_setup()
366 if (spi->mode & SPI_CPOL) in at91_usart_spi_setup()
371 if (spi->mode & SPI_CPHA) in at91_usart_spi_setup()
376 if (spi->mode & SPI_LOOP) in at91_usart_spi_setup()
384 return -ENOMEM; in at91_usart_spi_setup()
386 spi->controller_state = ausd; in at91_usart_spi_setup()
391 dev_dbg(&spi->dev, in at91_usart_spi_setup()
392 "setup: bpw %u mode 0x%x -> mr %d %08x\n", in at91_usart_spi_setup()
393 spi->bits_per_word, spi->mode, spi->chip_select, mr); in at91_usart_spi_setup()
399 struct spi_device *spi, in at91_usart_spi_transfer_one() argument
407 aus->xfer_failed = false; in at91_usart_spi_transfer_one()
408 aus->current_transfer = xfer; in at91_usart_spi_transfer_one()
409 aus->current_tx_remaining_bytes = xfer->len; in at91_usart_spi_transfer_one()
410 aus->current_rx_remaining_bytes = xfer->len; in at91_usart_spi_transfer_one()
412 while ((aus->current_tx_remaining_bytes || in at91_usart_spi_transfer_one()
413 aus->current_rx_remaining_bytes) && !aus->xfer_failed) { in at91_usart_spi_transfer_one()
414 reinit_completion(&aus->xfer_completion); in at91_usart_spi_transfer_one()
415 if (at91_usart_spi_can_dma(ctlr, spi, xfer) && in at91_usart_spi_transfer_one()
424 dev_err(&spi->dev, "DMA transfer timeout\n"); in at91_usart_spi_transfer_one()
425 return -EIO; in at91_usart_spi_transfer_one()
427 aus->current_tx_remaining_bytes = 0; in at91_usart_spi_transfer_one()
436 if (aus->xfer_failed) { in at91_usart_spi_transfer_one()
437 dev_err(aus->dev, "Overrun!\n"); in at91_usart_spi_transfer_one()
438 return -EIO; in at91_usart_spi_transfer_one()
448 struct spi_device *spi = message->spi; in at91_usart_spi_prepare_message() local
449 u32 *ausd = spi->controller_state; in at91_usart_spi_prepare_message()
469 static void at91_usart_spi_cleanup(struct spi_device *spi) in at91_usart_spi_cleanup() argument
471 struct at91_usart_spi_device *ausd = spi->controller_state; in at91_usart_spi_cleanup()
473 spi->controller_state = NULL; in at91_usart_spi_cleanup()
487 cs_gpios = devm_gpiod_get_array_optional(&pdev->dev, "cs", GPIOD_OUT_LOW); in at91_usart_gpio_setup()
500 struct clk *clk; in at91_usart_spi_probe() local
504 regs = platform_get_resource(to_platform_device(pdev->dev.parent), in at91_usart_spi_probe()
507 return -EINVAL; in at91_usart_spi_probe()
509 irq = platform_get_irq(to_platform_device(pdev->dev.parent), 0); in at91_usart_spi_probe()
513 clk = devm_clk_get(pdev->dev.parent, "usart"); in at91_usart_spi_probe()
514 if (IS_ERR(clk)) in at91_usart_spi_probe()
515 return PTR_ERR(clk); in at91_usart_spi_probe()
517 ret = -ENOMEM; in at91_usart_spi_probe()
518 controller = spi_alloc_master(&pdev->dev, sizeof(*aus)); in at91_usart_spi_probe()
526 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH; in at91_usart_spi_probe()
527 controller->dev.of_node = pdev->dev.parent->of_node; in at91_usart_spi_probe()
528 controller->bits_per_word_mask = SPI_BPW_MASK(8); in at91_usart_spi_probe()
529 controller->setup = at91_usart_spi_setup; in at91_usart_spi_probe()
530 controller->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX; in at91_usart_spi_probe()
531 controller->transfer_one = at91_usart_spi_transfer_one; in at91_usart_spi_probe()
532 controller->prepare_message = at91_usart_spi_prepare_message; in at91_usart_spi_probe()
533 controller->unprepare_message = at91_usart_spi_unprepare_message; in at91_usart_spi_probe()
534 controller->can_dma = at91_usart_spi_can_dma; in at91_usart_spi_probe()
535 controller->cleanup = at91_usart_spi_cleanup; in at91_usart_spi_probe()
536 controller->max_speed_hz = DIV_ROUND_UP(clk_get_rate(clk), in at91_usart_spi_probe()
538 controller->min_speed_hz = DIV_ROUND_UP(clk_get_rate(clk), in at91_usart_spi_probe()
544 aus->dev = &pdev->dev; in at91_usart_spi_probe()
545 aus->regs = devm_ioremap_resource(&pdev->dev, regs); in at91_usart_spi_probe()
546 if (IS_ERR(aus->regs)) { in at91_usart_spi_probe()
547 ret = PTR_ERR(aus->regs); in at91_usart_spi_probe()
551 aus->irq = irq; in at91_usart_spi_probe()
552 aus->clk = clk; in at91_usart_spi_probe()
554 ret = devm_request_irq(&pdev->dev, irq, at91_usart_spi_interrupt, 0, in at91_usart_spi_probe()
555 dev_name(&pdev->dev), controller); in at91_usart_spi_probe()
559 ret = clk_prepare_enable(clk); in at91_usart_spi_probe()
563 aus->spi_clk = clk_get_rate(clk); in at91_usart_spi_probe()
566 aus->phybase = regs->start; in at91_usart_spi_probe()
568 aus->mpdev = to_platform_device(pdev->dev.parent); in at91_usart_spi_probe()
574 spin_lock_init(&aus->lock); in at91_usart_spi_probe()
575 init_completion(&aus->xfer_completion); in at91_usart_spi_probe()
577 ret = devm_spi_register_master(&pdev->dev, controller); in at91_usart_spi_probe()
581 dev_info(&pdev->dev, in at91_usart_spi_probe()
582 "AT91 USART SPI Controller version 0x%x at %pa (irq %d)\n", in at91_usart_spi_probe()
584 &regs->start, irq); in at91_usart_spi_probe()
591 clk_disable_unprepare(clk); in at91_usart_spi_probe()
602 clk_disable_unprepare(aus->clk); in at91_usart_spi_runtime_suspend()
615 return clk_prepare_enable(aus->clk); in at91_usart_spi_runtime_resume()
656 clk_disable_unprepare(aus->clk); in at91_usart_spi_remove()
678 MODULE_DESCRIPTION("Microchip AT91 USART SPI Controller driver");