31 #include <linux/kernel.h>
48 #include <linux/module.h>
54 #define DRIVER_NAME "mxs-spi"
57 #define SSP_TIMEOUT 10000
59 #define SG_MAXLEN 0xff00
78 if (bits_per_word != 8) {
79 dev_err(&dev->
dev,
"%s, unsupported bits_per_word=%d\n",
80 __func__, bits_per_word);
88 dev_err(&dev->
dev,
"Cannot continue with zero clock\n");
107 static int mxs_spi_setup(
struct spi_device *dev)
117 err = mxs_spi_setup_transfer(dev,
NULL);
120 "Failed to setup transfer, error = %d\n", err);
126 static uint32_t mxs_spi_cs_to_reg(
unsigned cs)
146 static void mxs_spi_set_cs(
struct mxs_spi *spi,
unsigned cs)
154 select = mxs_spi_cs_to_reg(cs);
158 static inline void mxs_spi_enable(
struct mxs_spi *spi)
168 static inline void mxs_spi_disable(
struct mxs_spi *spi)
199 static void mxs_ssp_dma_irq_callback(
void *
param)
208 dev_err(ssp->
dev,
"%s[%i] CTRL1=%08x STATUS=%08x\n",
215 static int mxs_spi_txrx_dma(
struct mxs_spi *spi,
int cs,
216 unsigned char *
buf,
int len,
221 const bool vmalloced_buf = is_vmalloc_addr(buf);
227 struct page *vm_page;
252 for (sg_count = 0; sg_count < sgs; sg_count++) {
253 min =
min(len, desc_len);
256 if ((sg_count + 1 == sgs) && *last)
285 desc = dmaengine_prep_slave_sg(ssp->
dmach,
292 "Failed to get PIO reg. write descriptor.\n");
297 desc = dmaengine_prep_slave_sg(ssp->
dmach,
304 "Failed to get DMA data write descriptor.\n");
314 desc->
callback = mxs_ssp_dma_irq_callback;
318 dmaengine_submit(desc);
319 dma_async_issue_pending(ssp->
dmach);
326 dmaengine_terminate_all(ssp->
dmach);
333 while (--sg_count >= 0) {
344 static int mxs_spi_txrx_pio(
struct mxs_spi *spi,
int cs,
345 unsigned char *buf,
int len,
346 int *first,
int *last,
int write)
353 mxs_spi_set_cs(spi, cs);
356 if (*last && len == 0)
357 mxs_spi_disable(spi);
407 static int mxs_spi_transfer_one(
struct spi_master *master,
410 struct mxs_spi *spi = spi_master_get_devdata(master);
419 cs = m->
spi->chip_select;
423 status = mxs_spi_setup_transfer(m->
spi, t);
433 "Cannot send and receive simultaneously\n");
453 status = mxs_spi_txrx_pio(spi, cs,
455 t->
len, &first, &last, 1);
457 status = mxs_spi_txrx_pio(spi, cs,
466 status = mxs_spi_txrx_dma(spi, cs,
470 status = mxs_spi_txrx_dma(spi, cs,
506 { .compatible =
"fsl,imx23-spi", .data = (
void *)
IMX23_SSP, },
507 { .compatible =
"fsl,imx28-spi", .data = (
void *)
IMX28_SSP, },
525 int ret = 0, irq_err, irq_dma;
533 const int clk_freq_default = 160000000;
538 if (!iores || irq_err < 0 || irq_dma < 0)
545 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
547 return PTR_ERR(pinctrl);
559 ret = of_property_read_u32(np,
"fsl,ssp-dma-channel",
563 "Failed to get DMA channel\n");
567 ret = of_property_read_u32(np,
"clock-frequency",
570 clk_freq = clk_freq_default;
575 devid = pdev->
id_entry->driver_data;
576 dma_channel = dmares->
start;
577 clk_freq = clk_freq_default;
585 master->
setup = mxs_spi_setup;
588 master->
dev.of_node = np;
591 spi = spi_master_get_devdata(master);
599 init_completion(&spi->
c);
601 ret = devm_request_irq(&pdev->
dev, irq_err, mxs_ssp_irq_handler, 0,
604 goto out_master_free;
612 goto out_master_free;
615 clk_prepare_enable(ssp->
clk);
621 platform_set_drvdata(pdev, master);
625 dev_err(&pdev->
dev,
"Cannot register SPI master, %d\n", ret);
633 clk_disable_unprepare(ssp->
clk);
635 spi_master_put(master);
645 master = spi_master_get(platform_get_drvdata(pdev));
646 spi = spi_master_get_devdata(master);
653 clk_disable_unprepare(ssp->
clk);
655 spi_master_put(master);
661 .probe = mxs_spi_probe,
666 .of_match_table = mxs_spi_dt_ids,