Lines Matching +full:zynq +full:- +full:gpio +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2008 - 2014 Xilinx, Inc.
7 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
12 #include <linux/gpio/consumer.h>
23 #define CDNS_SPI_NAME "cdns-spi"
61 * SPI Configuration Register - Baud rate and slave select
68 #define CDNS_SPI_BAUD_DIV_MIN 1 /* Baud rate divisor minimum */
102 * struct cdns_spi - This definition defines spi driver instance
131 return readl_relaxed(xspi->regs + offset); in cdns_spi_read()
136 writel_relaxed(val, xspi->regs + offset); in cdns_spi_write()
140 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
145 * to 1 and size of the word to be transferred as 8 bit.
154 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
170 * cdns_spi_chipselect - Select or deselect the chip select line
172 * @is_high: Select(0) or deselect (1) the chip select line
176 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_chipselect()
187 if (!(xspi->is_decoded_cs)) in cdns_spi_chipselect()
188 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) << in cdns_spi_chipselect()
192 ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) & in cdns_spi_chipselect()
200 * cdns_spi_config_clock_mode - Sets clock polarity and phase
207 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_mode()
215 if (spi->mode & SPI_CPHA) in cdns_spi_config_clock_mode()
217 if (spi->mode & SPI_CPOL) in cdns_spi_config_clock_mode()
234 * cdns_spi_config_clock_freq - Sets clock frequency
250 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_freq()
254 frequency = xspi->clk_rate; in cdns_spi_config_clock_freq()
259 if (xspi->speed_hz != transfer->speed_hz) { in cdns_spi_config_clock_freq()
260 /* first valid value is 1 */ in cdns_spi_config_clock_freq()
263 (frequency / (2 << baud_rate_val)) > transfer->speed_hz) in cdns_spi_config_clock_freq()
269 xspi->speed_hz = frequency / (2 << baud_rate_val); in cdns_spi_config_clock_freq()
275 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
288 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_setup_transfer()
292 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", in cdns_spi_setup_transfer()
293 __func__, spi->mode, spi->bits_per_word, in cdns_spi_setup_transfer()
294 xspi->speed_hz); in cdns_spi_setup_transfer()
300 * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
308 (xspi->tx_bytes > 0)) { in cdns_spi_fill_tx_fifo()
317 if (xspi->txbuf) in cdns_spi_fill_tx_fifo()
318 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); in cdns_spi_fill_tx_fifo()
322 xspi->tx_bytes--; in cdns_spi_fill_tx_fifo()
328 * cdns_spi_irq - Interrupt service routine of the SPI controller
337 * transferred is non-zero.
354 * transferred is non-zero in cdns_spi_irq()
362 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
369 if (xspi->rxbuf) in cdns_spi_irq()
370 *xspi->rxbuf++ = data; in cdns_spi_irq()
372 xspi->rx_bytes--; in cdns_spi_irq()
373 trans_cnt--; in cdns_spi_irq()
376 if (xspi->tx_bytes) { in cdns_spi_irq()
394 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
399 * cdns_transfer_one - Initiates the SPI transfer
416 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
417 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
418 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
419 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
426 return transfer->len; in cdns_transfer_one()
430 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
448 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
466 * cdns_spi_probe - Probe method for the SPI driver
480 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
482 return -ENOMEM; in cdns_spi_probe()
485 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
488 xspi->regs = devm_platform_ioremap_resource(pdev, 0); in cdns_spi_probe()
489 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
490 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
494 xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_spi_probe()
495 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
496 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
497 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
501 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_spi_probe()
502 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
503 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
504 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
508 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_probe()
510 dev_err(&pdev->dev, "Unable to enable APB clock.\n"); in cdns_spi_probe()
514 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_probe()
516 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_spi_probe()
520 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
521 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
522 pm_runtime_get_noresume(&pdev->dev); in cdns_spi_probe()
523 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
524 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
526 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
528 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
530 master->num_chipselect = num_cs; in cdns_spi_probe()
532 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
533 &xspi->is_decoded_cs); in cdns_spi_probe()
535 xspi->is_decoded_cs = 0; in cdns_spi_probe()
542 ret = -ENXIO; in cdns_spi_probe()
546 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
547 0, pdev->name, master); in cdns_spi_probe()
549 ret = -ENXIO; in cdns_spi_probe()
550 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
554 master->use_gpio_descriptors = true; in cdns_spi_probe()
555 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
556 master->prepare_message = cdns_prepare_message; in cdns_spi_probe()
557 master->transfer_one = cdns_transfer_one; in cdns_spi_probe()
558 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
559 master->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
560 master->auto_runtime_pm = true; in cdns_spi_probe()
561 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in cdns_spi_probe()
563 xspi->clk_rate = clk_get_rate(xspi->ref_clk); in cdns_spi_probe()
565 master->max_speed_hz = xspi->clk_rate / 4; in cdns_spi_probe()
566 xspi->speed_hz = master->max_speed_hz; in cdns_spi_probe()
568 master->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
570 pm_runtime_mark_last_busy(&pdev->dev); in cdns_spi_probe()
571 pm_runtime_put_autosuspend(&pdev->dev); in cdns_spi_probe()
575 dev_err(&pdev->dev, "spi_register_master failed\n"); in cdns_spi_probe()
582 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
583 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
584 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_probe()
586 clk_disable_unprepare(xspi->pclk); in cdns_spi_probe()
593 * cdns_spi_remove - Remove method for the SPI driver
609 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_remove()
610 clk_disable_unprepare(xspi->pclk); in cdns_spi_remove()
611 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
612 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
620 * cdns_spi_suspend - Suspend method for the SPI driver
636 * cdns_spi_resume - Resume method for the SPI driver
653 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
666 ret = clk_prepare_enable(xspi->pclk); in cnds_runtime_resume()
672 ret = clk_prepare_enable(xspi->ref_clk); in cnds_runtime_resume()
675 clk_disable_unprepare(xspi->pclk); in cnds_runtime_resume()
682 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
694 clk_disable_unprepare(xspi->ref_clk); in cnds_runtime_suspend()
695 clk_disable_unprepare(xspi->pclk); in cnds_runtime_suspend()
707 { .compatible = "xlnx,zynq-spi-r1p6" },
708 { .compatible = "cdns,spi-r1p6" },
713 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */