Fabio Estevam | 4c23e48 | 2018-05-02 16:18:28 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Freescale MXS SPI master driver |
| 4 | // |
| 5 | // Copyright 2012 DENX Software Engineering, GmbH. |
| 6 | // Copyright 2012 Freescale Semiconductor, Inc. |
| 7 | // Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved. |
| 8 | // |
| 9 | // Rework and transition to new API by: |
| 10 | // Marek Vasut <marex@denx.de> |
| 11 | // |
| 12 | // Based on previous attempt by: |
| 13 | // Fabio Estevam <fabio.estevam@freescale.com> |
| 14 | // |
| 15 | // Based on code from U-Boot bootloader by: |
| 16 | // Marek Vasut <marex@denx.de> |
| 17 | // |
| 18 | // Based on spi-stmp.c, which is: |
| 19 | // Author: Dmitry Pervushin <dimka@embeddedalley.com> |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 20 | |
| 21 | #include <linux/kernel.h> |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 22 | #include <linux/ioport.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/of_gpio.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | #include <linux/dmaengine.h> |
| 31 | #include <linux/highmem.h> |
| 32 | #include <linux/clk.h> |
| 33 | #include <linux/err.h> |
| 34 | #include <linux/completion.h> |
| 35 | #include <linux/gpio.h> |
| 36 | #include <linux/regulator/consumer.h> |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 38 | #include <linux/module.h> |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 39 | #include <linux/stmp_device.h> |
| 40 | #include <linux/spi/spi.h> |
| 41 | #include <linux/spi/mxs-spi.h> |
| 42 | |
| 43 | #define DRIVER_NAME "mxs-spi" |
| 44 | |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 45 | /* Use 10S timeout for very long transfers, it should suffice. */ |
| 46 | #define SSP_TIMEOUT 10000 |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 47 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 48 | #define SG_MAXLEN 0xff00 |
| 49 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Flags for txrx functions. More efficient that using an argument register for |
| 52 | * each one. |
| 53 | */ |
| 54 | #define TXRX_WRITE (1<<0) /* This is a write */ |
| 55 | #define TXRX_DEASSERT_CS (1<<1) /* De-assert CS at end of txrx */ |
| 56 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 57 | struct mxs_spi { |
| 58 | struct mxs_ssp ssp; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 59 | struct completion c; |
Trent Piepho | a560943 | 2013-10-01 13:15:47 -0700 | [diff] [blame] | 60 | unsigned int sck; /* Rate requested (vs actual) */ |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static int mxs_spi_setup_transfer(struct spi_device *dev, |
Trent Piepho | aa9e0c6 | 2013-10-01 13:15:40 -0700 | [diff] [blame] | 64 | const struct spi_transfer *t) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 65 | { |
| 66 | struct mxs_spi *spi = spi_master_get_devdata(dev->master); |
| 67 | struct mxs_ssp *ssp = &spi->ssp; |
Trent Piepho | aa9e0c6 | 2013-10-01 13:15:40 -0700 | [diff] [blame] | 68 | const unsigned int hz = min(dev->max_speed_hz, t->speed_hz); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 69 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 70 | if (hz == 0) { |
Trent Piepho | aa9e0c6 | 2013-10-01 13:15:40 -0700 | [diff] [blame] | 71 | dev_err(&dev->dev, "SPI clock rate of zero not allowed\n"); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | } |
| 74 | |
Trent Piepho | a560943 | 2013-10-01 13:15:47 -0700 | [diff] [blame] | 75 | if (hz != spi->sck) { |
| 76 | mxs_ssp_set_clk_rate(ssp, hz); |
| 77 | /* |
| 78 | * Save requested rate, hz, rather than the actual rate, |
Michael Heimpold | a44619c | 2014-10-02 23:10:22 +0200 | [diff] [blame] | 79 | * ssp->clk_rate. Otherwise we would set the rate every transfer |
Trent Piepho | a560943 | 2013-10-01 13:15:47 -0700 | [diff] [blame] | 80 | * when the actual rate is not quite the same as requested rate. |
| 81 | */ |
| 82 | spi->sck = hz; |
| 83 | /* |
| 84 | * Perhaps we should return an error if the actual clock is |
| 85 | * nowhere close to what was requested? |
| 86 | */ |
| 87 | } |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 88 | |
Trent Piepho | 58f46e4 | 2013-10-01 13:14:25 -0700 | [diff] [blame] | 89 | writel(BM_SSP_CTRL0_LOCK_CS, |
| 90 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 91 | |
| 92 | writel(BF_SSP_CTRL1_SSP_MODE(BV_SSP_CTRL1_SSP_MODE__SPI) | |
Trent Piepho | aa9e0c6 | 2013-10-01 13:15:40 -0700 | [diff] [blame] | 93 | BF_SSP_CTRL1_WORD_LENGTH(BV_SSP_CTRL1_WORD_LENGTH__EIGHT_BITS) | |
| 94 | ((dev->mode & SPI_CPOL) ? BM_SSP_CTRL1_POLARITY : 0) | |
| 95 | ((dev->mode & SPI_CPHA) ? BM_SSP_CTRL1_PHASE : 0), |
| 96 | ssp->base + HW_SSP_CTRL1(ssp)); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 97 | |
| 98 | writel(0x0, ssp->base + HW_SSP_CMD0); |
| 99 | writel(0x0, ssp->base + HW_SSP_CMD1); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Trent Piepho | 42e182f | 2013-10-01 13:15:54 -0700 | [diff] [blame] | 104 | static u32 mxs_spi_cs_to_reg(unsigned cs) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 105 | { |
Trent Piepho | 42e182f | 2013-10-01 13:15:54 -0700 | [diff] [blame] | 106 | u32 select = 0; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * i.MX28 Datasheet: 17.10.1: HW_SSP_CTRL0 |
| 110 | * |
| 111 | * The bits BM_SSP_CTRL0_WAIT_FOR_CMD and BM_SSP_CTRL0_WAIT_FOR_IRQ |
| 112 | * in HW_SSP_CTRL0 register do have multiple usage, please refer to |
| 113 | * the datasheet for further details. In SPI mode, they are used to |
| 114 | * toggle the chip-select lines (nCS pins). |
| 115 | */ |
| 116 | if (cs & 1) |
| 117 | select |= BM_SSP_CTRL0_WAIT_FOR_CMD; |
| 118 | if (cs & 2) |
| 119 | select |= BM_SSP_CTRL0_WAIT_FOR_IRQ; |
| 120 | |
| 121 | return select; |
| 122 | } |
| 123 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 124 | static int mxs_ssp_wait(struct mxs_spi *spi, int offset, int mask, bool set) |
| 125 | { |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 126 | const unsigned long timeout = jiffies + msecs_to_jiffies(SSP_TIMEOUT); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 127 | struct mxs_ssp *ssp = &spi->ssp; |
Trent Piepho | 42e182f | 2013-10-01 13:15:54 -0700 | [diff] [blame] | 128 | u32 reg; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 129 | |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 130 | do { |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 131 | reg = readl_relaxed(ssp->base + offset); |
| 132 | |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 133 | if (!set) |
| 134 | reg = ~reg; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 135 | |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 136 | reg &= mask; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 137 | |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 138 | if (reg == mask) |
| 139 | return 0; |
| 140 | } while (time_before(jiffies, timeout)); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 141 | |
Marek Vasut | f13639d | 2012-09-04 04:40:18 +0200 | [diff] [blame] | 142 | return -ETIMEDOUT; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 145 | static void mxs_ssp_dma_irq_callback(void *param) |
| 146 | { |
| 147 | struct mxs_spi *spi = param; |
Jingoo Han | a7fa321 | 2014-09-02 11:50:48 +0900 | [diff] [blame] | 148 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 149 | complete(&spi->c); |
| 150 | } |
| 151 | |
| 152 | static irqreturn_t mxs_ssp_irq_handler(int irq, void *dev_id) |
| 153 | { |
| 154 | struct mxs_ssp *ssp = dev_id; |
Jingoo Han | a7fa321 | 2014-09-02 11:50:48 +0900 | [diff] [blame] | 155 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 156 | dev_err(ssp->dev, "%s[%i] CTRL1=%08x STATUS=%08x\n", |
| 157 | __func__, __LINE__, |
| 158 | readl(ssp->base + HW_SSP_CTRL1(ssp)), |
| 159 | readl(ssp->base + HW_SSP_STATUS(ssp))); |
| 160 | return IRQ_HANDLED; |
| 161 | } |
| 162 | |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 163 | static int mxs_spi_txrx_dma(struct mxs_spi *spi, |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 164 | unsigned char *buf, int len, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 165 | unsigned int flags) |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 166 | { |
| 167 | struct mxs_ssp *ssp = &spi->ssp; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 168 | struct dma_async_tx_descriptor *desc = NULL; |
| 169 | const bool vmalloced_buf = is_vmalloc_addr(buf); |
| 170 | const int desc_len = vmalloced_buf ? PAGE_SIZE : SG_MAXLEN; |
| 171 | const int sgs = DIV_ROUND_UP(len, desc_len); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 172 | int sg_count; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 173 | int min, ret; |
Trent Piepho | 42e182f | 2013-10-01 13:15:54 -0700 | [diff] [blame] | 174 | u32 ctrl0; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 175 | struct page *vm_page; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 176 | struct { |
Trent Piepho | 42e182f | 2013-10-01 13:15:54 -0700 | [diff] [blame] | 177 | u32 pio[4]; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 178 | struct scatterlist sg; |
| 179 | } *dma_xfer; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 180 | |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 181 | if (!len) |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 182 | return -EINVAL; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 183 | |
Jingoo Han | a7fa321 | 2014-09-02 11:50:48 +0900 | [diff] [blame] | 184 | dma_xfer = kcalloc(sgs, sizeof(*dma_xfer), GFP_KERNEL); |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 185 | if (!dma_xfer) |
| 186 | return -ENOMEM; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 187 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 188 | reinit_completion(&spi->c); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 189 | |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 190 | /* Chip select was already programmed into CTRL0 */ |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 191 | ctrl0 = readl(ssp->base + HW_SSP_CTRL0); |
Trent Piepho | df23286 | 2013-10-01 13:14:57 -0700 | [diff] [blame] | 192 | ctrl0 &= ~(BM_SSP_CTRL0_XFER_COUNT | BM_SSP_CTRL0_IGNORE_CRC | |
| 193 | BM_SSP_CTRL0_READ); |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 194 | ctrl0 |= BM_SSP_CTRL0_DATA_XFER; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 195 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 196 | if (!(flags & TXRX_WRITE)) |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 197 | ctrl0 |= BM_SSP_CTRL0_READ; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 198 | |
| 199 | /* Queue the DMA data transfer. */ |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 200 | for (sg_count = 0; sg_count < sgs; sg_count++) { |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 201 | /* Prepare the transfer descriptor. */ |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 202 | min = min(len, desc_len); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 203 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 204 | /* |
| 205 | * De-assert CS on last segment if flag is set (i.e., no more |
| 206 | * transfers will follow) |
| 207 | */ |
| 208 | if ((sg_count + 1 == sgs) && (flags & TXRX_DEASSERT_CS)) |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 209 | ctrl0 |= BM_SSP_CTRL0_IGNORE_CRC; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 210 | |
Juha Lumme | ba486a2 | 2012-12-26 14:48:51 +0900 | [diff] [blame] | 211 | if (ssp->devid == IMX23_SSP) { |
| 212 | ctrl0 &= ~BM_SSP_CTRL0_XFER_COUNT; |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 213 | ctrl0 |= min; |
Juha Lumme | ba486a2 | 2012-12-26 14:48:51 +0900 | [diff] [blame] | 214 | } |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 215 | |
| 216 | dma_xfer[sg_count].pio[0] = ctrl0; |
| 217 | dma_xfer[sg_count].pio[3] = min; |
| 218 | |
| 219 | if (vmalloced_buf) { |
| 220 | vm_page = vmalloc_to_page(buf); |
| 221 | if (!vm_page) { |
| 222 | ret = -ENOMEM; |
| 223 | goto err_vmalloc; |
| 224 | } |
Charles Keepax | 9e8987a | 2014-11-17 09:14:32 +0000 | [diff] [blame] | 225 | |
| 226 | sg_init_table(&dma_xfer[sg_count].sg, 1); |
| 227 | sg_set_page(&dma_xfer[sg_count].sg, vm_page, |
| 228 | min, offset_in_page(buf)); |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 229 | } else { |
Charles Keepax | 9e8987a | 2014-11-17 09:14:32 +0000 | [diff] [blame] | 230 | sg_init_one(&dma_xfer[sg_count].sg, buf, min); |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 233 | ret = dma_map_sg(ssp->dev, &dma_xfer[sg_count].sg, 1, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 234 | (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 235 | |
| 236 | len -= min; |
| 237 | buf += min; |
| 238 | |
| 239 | /* Queue the PIO register write transfer. */ |
| 240 | desc = dmaengine_prep_slave_sg(ssp->dmach, |
| 241 | (struct scatterlist *)dma_xfer[sg_count].pio, |
| 242 | (ssp->devid == IMX23_SSP) ? 1 : 4, |
| 243 | DMA_TRANS_NONE, |
| 244 | sg_count ? DMA_PREP_INTERRUPT : 0); |
| 245 | if (!desc) { |
| 246 | dev_err(ssp->dev, |
| 247 | "Failed to get PIO reg. write descriptor.\n"); |
| 248 | ret = -EINVAL; |
| 249 | goto err_mapped; |
| 250 | } |
| 251 | |
| 252 | desc = dmaengine_prep_slave_sg(ssp->dmach, |
| 253 | &dma_xfer[sg_count].sg, 1, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 254 | (flags & TXRX_WRITE) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM, |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 255 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 256 | |
| 257 | if (!desc) { |
| 258 | dev_err(ssp->dev, |
| 259 | "Failed to get DMA data write descriptor.\n"); |
| 260 | ret = -EINVAL; |
| 261 | goto err_mapped; |
| 262 | } |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * The last descriptor must have this callback, |
| 267 | * to finish the DMA transaction. |
| 268 | */ |
| 269 | desc->callback = mxs_ssp_dma_irq_callback; |
| 270 | desc->callback_param = spi; |
| 271 | |
| 272 | /* Start the transfer. */ |
| 273 | dmaengine_submit(desc); |
| 274 | dma_async_issue_pending(ssp->dmach); |
| 275 | |
Nicholas Mc Guire | f223469 | 2015-02-05 09:47:06 -0500 | [diff] [blame] | 276 | if (!wait_for_completion_timeout(&spi->c, |
| 277 | msecs_to_jiffies(SSP_TIMEOUT))) { |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 278 | dev_err(ssp->dev, "DMA transfer timeout\n"); |
| 279 | ret = -ETIMEDOUT; |
Marek Vasut | 4496846 | 2012-10-14 04:32:56 +0200 | [diff] [blame] | 280 | dmaengine_terminate_all(ssp->dmach); |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 281 | goto err_vmalloc; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | ret = 0; |
| 285 | |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 286 | err_vmalloc: |
| 287 | while (--sg_count >= 0) { |
| 288 | err_mapped: |
| 289 | dma_unmap_sg(ssp->dev, &dma_xfer[sg_count].sg, 1, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 290 | (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Marek Vasut | 010b481 | 2012-09-04 04:40:15 +0200 | [diff] [blame] | 293 | kfree(dma_xfer); |
| 294 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 295 | return ret; |
| 296 | } |
| 297 | |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 298 | static int mxs_spi_txrx_pio(struct mxs_spi *spi, |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 299 | unsigned char *buf, int len, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 300 | unsigned int flags) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 301 | { |
| 302 | struct mxs_ssp *ssp = &spi->ssp; |
| 303 | |
Trent Piepho | 75e73fa | 2013-10-01 13:14:39 -0700 | [diff] [blame] | 304 | writel(BM_SSP_CTRL0_IGNORE_CRC, |
| 305 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 306 | |
| 307 | while (len--) { |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 308 | if (len == 0 && (flags & TXRX_DEASSERT_CS)) |
Trent Piepho | f5bc738 | 2013-10-01 13:14:32 -0700 | [diff] [blame] | 309 | writel(BM_SSP_CTRL0_IGNORE_CRC, |
| 310 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 311 | |
| 312 | if (ssp->devid == IMX23_SSP) { |
| 313 | writel(BM_SSP_CTRL0_XFER_COUNT, |
| 314 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
| 315 | writel(1, |
| 316 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
| 317 | } else { |
| 318 | writel(1, ssp->base + HW_SSP_XFER_SIZE); |
| 319 | } |
| 320 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 321 | if (flags & TXRX_WRITE) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 322 | writel(BM_SSP_CTRL0_READ, |
| 323 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
| 324 | else |
| 325 | writel(BM_SSP_CTRL0_READ, |
| 326 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
| 327 | |
| 328 | writel(BM_SSP_CTRL0_RUN, |
| 329 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
| 330 | |
| 331 | if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 1)) |
| 332 | return -ETIMEDOUT; |
| 333 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 334 | if (flags & TXRX_WRITE) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 335 | writel(*buf, ssp->base + HW_SSP_DATA(ssp)); |
| 336 | |
| 337 | writel(BM_SSP_CTRL0_DATA_XFER, |
| 338 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
| 339 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 340 | if (!(flags & TXRX_WRITE)) { |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 341 | if (mxs_ssp_wait(spi, HW_SSP_STATUS(ssp), |
| 342 | BM_SSP_STATUS_FIFO_EMPTY, 0)) |
| 343 | return -ETIMEDOUT; |
| 344 | |
| 345 | *buf = (readl(ssp->base + HW_SSP_DATA(ssp)) & 0xff); |
| 346 | } |
| 347 | |
| 348 | if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 0)) |
| 349 | return -ETIMEDOUT; |
| 350 | |
| 351 | buf++; |
| 352 | } |
| 353 | |
| 354 | if (len <= 0) |
| 355 | return 0; |
| 356 | |
| 357 | return -ETIMEDOUT; |
| 358 | } |
| 359 | |
| 360 | static int mxs_spi_transfer_one(struct spi_master *master, |
| 361 | struct spi_message *m) |
| 362 | { |
| 363 | struct mxs_spi *spi = spi_master_get_devdata(master); |
| 364 | struct mxs_ssp *ssp = &spi->ssp; |
Axel Lin | 9a7da6c | 2014-02-05 17:47:59 +0800 | [diff] [blame] | 365 | struct spi_transfer *t; |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 366 | unsigned int flag; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 367 | int status = 0; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 368 | |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 369 | /* Program CS register bits here, it will be used for all transfers. */ |
| 370 | writel(BM_SSP_CTRL0_WAIT_FOR_CMD | BM_SSP_CTRL0_WAIT_FOR_IRQ, |
| 371 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR); |
| 372 | writel(mxs_spi_cs_to_reg(m->spi->chip_select), |
| 373 | ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 374 | |
Axel Lin | 9a7da6c | 2014-02-05 17:47:59 +0800 | [diff] [blame] | 375 | list_for_each_entry(t, &m->transfers, transfer_list) { |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 376 | |
| 377 | status = mxs_spi_setup_transfer(m->spi, t); |
| 378 | if (status) |
| 379 | break; |
| 380 | |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 381 | /* De-assert on last transfer, inverted by cs_change flag */ |
| 382 | flag = (&t->transfer_list == m->transfers.prev) ^ t->cs_change ? |
| 383 | TXRX_DEASSERT_CS : 0; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 384 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 385 | /* |
| 386 | * Small blocks can be transfered via PIO. |
| 387 | * Measured by empiric means: |
| 388 | * |
| 389 | * dd if=/dev/mtdblock0 of=/dev/null bs=1024k count=1 |
| 390 | * |
| 391 | * DMA only: 2.164808 seconds, 473.0KB/s |
| 392 | * Combined: 1.676276 seconds, 610.9KB/s |
| 393 | */ |
Marek Vasut | 727c10e | 2012-09-04 04:40:17 +0200 | [diff] [blame] | 394 | if (t->len < 32) { |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 395 | writel(BM_SSP_CTRL1_DMA_ENABLE, |
| 396 | ssp->base + HW_SSP_CTRL1(ssp) + |
| 397 | STMP_OFFSET_REG_CLR); |
| 398 | |
| 399 | if (t->tx_buf) |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 400 | status = mxs_spi_txrx_pio(spi, |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 401 | (void *)t->tx_buf, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 402 | t->len, flag | TXRX_WRITE); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 403 | if (t->rx_buf) |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 404 | status = mxs_spi_txrx_pio(spi, |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 405 | t->rx_buf, t->len, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 406 | flag); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 407 | } else { |
| 408 | writel(BM_SSP_CTRL1_DMA_ENABLE, |
| 409 | ssp->base + HW_SSP_CTRL1(ssp) + |
| 410 | STMP_OFFSET_REG_SET); |
| 411 | |
| 412 | if (t->tx_buf) |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 413 | status = mxs_spi_txrx_dma(spi, |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 414 | (void *)t->tx_buf, t->len, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 415 | flag | TXRX_WRITE); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 416 | if (t->rx_buf) |
Trent Piepho | 0b782f7 | 2013-10-01 13:15:04 -0700 | [diff] [blame] | 417 | status = mxs_spi_txrx_dma(spi, |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 418 | t->rx_buf, t->len, |
Trent Piepho | 28cad12 | 2013-10-01 13:14:50 -0700 | [diff] [blame] | 419 | flag); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 420 | } |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 421 | |
Marek Vasut | c895db0 | 2012-08-24 04:34:18 +0200 | [diff] [blame] | 422 | if (status) { |
| 423 | stmp_reset_block(ssp->base); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 424 | break; |
Marek Vasut | c895db0 | 2012-08-24 04:34:18 +0200 | [diff] [blame] | 425 | } |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 426 | |
Marek Vasut | 204e706 | 2012-09-04 04:40:16 +0200 | [diff] [blame] | 427 | m->actual_length += t->len; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Marek Vasut | d856f1eb | 2012-10-14 04:32:55 +0200 | [diff] [blame] | 430 | m->status = status; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 431 | spi_finalize_current_message(master); |
| 432 | |
| 433 | return status; |
| 434 | } |
| 435 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 436 | static int mxs_spi_runtime_suspend(struct device *dev) |
| 437 | { |
| 438 | struct spi_master *master = dev_get_drvdata(dev); |
| 439 | struct mxs_spi *spi = spi_master_get_devdata(master); |
| 440 | struct mxs_ssp *ssp = &spi->ssp; |
| 441 | int ret; |
| 442 | |
| 443 | clk_disable_unprepare(ssp->clk); |
| 444 | |
| 445 | ret = pinctrl_pm_select_idle_state(dev); |
| 446 | if (ret) { |
| 447 | int ret2 = clk_prepare_enable(ssp->clk); |
| 448 | |
| 449 | if (ret2) |
| 450 | dev_warn(dev, "Failed to reenable clock after failing pinctrl request (pinctrl: %d, clk: %d)\n", |
| 451 | ret, ret2); |
| 452 | } |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | static int mxs_spi_runtime_resume(struct device *dev) |
| 458 | { |
| 459 | struct spi_master *master = dev_get_drvdata(dev); |
| 460 | struct mxs_spi *spi = spi_master_get_devdata(master); |
| 461 | struct mxs_ssp *ssp = &spi->ssp; |
| 462 | int ret; |
| 463 | |
| 464 | ret = pinctrl_pm_select_default_state(dev); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | |
| 468 | ret = clk_prepare_enable(ssp->clk); |
| 469 | if (ret) |
| 470 | pinctrl_pm_select_idle_state(dev); |
| 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static int __maybe_unused mxs_spi_suspend(struct device *dev) |
| 476 | { |
| 477 | struct spi_master *master = dev_get_drvdata(dev); |
| 478 | int ret; |
| 479 | |
| 480 | ret = spi_master_suspend(master); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
| 484 | if (!pm_runtime_suspended(dev)) |
| 485 | return mxs_spi_runtime_suspend(dev); |
| 486 | else |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int __maybe_unused mxs_spi_resume(struct device *dev) |
| 491 | { |
| 492 | struct spi_master *master = dev_get_drvdata(dev); |
| 493 | int ret; |
| 494 | |
| 495 | if (!pm_runtime_suspended(dev)) |
| 496 | ret = mxs_spi_runtime_resume(dev); |
| 497 | else |
| 498 | ret = 0; |
| 499 | if (ret) |
| 500 | return ret; |
| 501 | |
| 502 | ret = spi_master_resume(master); |
| 503 | if (ret < 0 && !pm_runtime_suspended(dev)) |
| 504 | mxs_spi_runtime_suspend(dev); |
| 505 | |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static const struct dev_pm_ops mxs_spi_pm = { |
| 510 | SET_RUNTIME_PM_OPS(mxs_spi_runtime_suspend, |
| 511 | mxs_spi_runtime_resume, NULL) |
| 512 | SET_SYSTEM_SLEEP_PM_OPS(mxs_spi_suspend, mxs_spi_resume) |
| 513 | }; |
| 514 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 515 | static const struct of_device_id mxs_spi_dt_ids[] = { |
| 516 | { .compatible = "fsl,imx23-spi", .data = (void *) IMX23_SSP, }, |
| 517 | { .compatible = "fsl,imx28-spi", .data = (void *) IMX28_SSP, }, |
| 518 | { /* sentinel */ } |
| 519 | }; |
| 520 | MODULE_DEVICE_TABLE(of, mxs_spi_dt_ids); |
| 521 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 522 | static int mxs_spi_probe(struct platform_device *pdev) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 523 | { |
| 524 | const struct of_device_id *of_id = |
| 525 | of_match_device(mxs_spi_dt_ids, &pdev->dev); |
| 526 | struct device_node *np = pdev->dev.of_node; |
| 527 | struct spi_master *master; |
| 528 | struct mxs_spi *spi; |
| 529 | struct mxs_ssp *ssp; |
Shawn Guo | 26aafa7 | 2013-02-26 11:07:32 +0800 | [diff] [blame] | 530 | struct resource *iores; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 531 | struct clk *clk; |
| 532 | void __iomem *base; |
Shawn Guo | 26aafa7 | 2013-02-26 11:07:32 +0800 | [diff] [blame] | 533 | int devid, clk_freq; |
| 534 | int ret = 0, irq_err; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 535 | |
Marek Vasut | e64d07a | 2012-08-22 22:38:35 +0200 | [diff] [blame] | 536 | /* |
| 537 | * Default clock speed for the SPI core. 160MHz seems to |
| 538 | * work reasonably well with most SPI flashes, so use this |
| 539 | * as a default. Override with "clock-frequency" DT prop. |
| 540 | */ |
| 541 | const int clk_freq_default = 160000000; |
| 542 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 543 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 544 | irq_err = platform_get_irq(pdev, 0); |
Fabio Estevam | 796305a | 2013-07-21 22:29:54 -0300 | [diff] [blame] | 545 | if (irq_err < 0) |
Fabio Estevam | cdd1945 | 2014-02-14 01:19:21 -0200 | [diff] [blame] | 546 | return irq_err; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 547 | |
Thierry Reding | b0ee560 | 2013-01-21 11:09:18 +0100 | [diff] [blame] | 548 | base = devm_ioremap_resource(&pdev->dev, iores); |
| 549 | if (IS_ERR(base)) |
| 550 | return PTR_ERR(base); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 551 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 552 | clk = devm_clk_get(&pdev->dev, NULL); |
| 553 | if (IS_ERR(clk)) |
| 554 | return PTR_ERR(clk); |
| 555 | |
Shawn Guo | 26aafa7 | 2013-02-26 11:07:32 +0800 | [diff] [blame] | 556 | devid = (enum mxs_ssp_id) of_id->data; |
| 557 | ret = of_property_read_u32(np, "clock-frequency", |
| 558 | &clk_freq); |
| 559 | if (ret) |
Marek Vasut | e64d07a | 2012-08-22 22:38:35 +0200 | [diff] [blame] | 560 | clk_freq = clk_freq_default; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 561 | |
| 562 | master = spi_alloc_master(&pdev->dev, sizeof(*spi)); |
| 563 | if (!master) |
| 564 | return -ENOMEM; |
| 565 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 566 | platform_set_drvdata(pdev, master); |
| 567 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 568 | master->transfer_one_message = mxs_spi_transfer_one; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 569 | master->bits_per_word_mask = SPI_BPW_MASK(8); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 570 | master->mode_bits = SPI_CPOL | SPI_CPHA; |
| 571 | master->num_chipselect = 3; |
| 572 | master->dev.of_node = np; |
| 573 | master->flags = SPI_MASTER_HALF_DUPLEX; |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 574 | master->auto_runtime_pm = true; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 575 | |
| 576 | spi = spi_master_get_devdata(master); |
| 577 | ssp = &spi->ssp; |
| 578 | ssp->dev = &pdev->dev; |
| 579 | ssp->clk = clk; |
| 580 | ssp->base = base; |
| 581 | ssp->devid = devid; |
| 582 | |
Marek Vasut | 41682e0 | 2012-08-24 04:56:27 +0200 | [diff] [blame] | 583 | init_completion(&spi->c); |
| 584 | |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 585 | ret = devm_request_irq(&pdev->dev, irq_err, mxs_ssp_irq_handler, 0, |
Fabio Estevam | 617100c | 2014-11-10 17:25:24 -0200 | [diff] [blame] | 586 | dev_name(&pdev->dev), ssp); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 587 | if (ret) |
| 588 | goto out_master_free; |
| 589 | |
Shawn Guo | 26aafa7 | 2013-02-26 11:07:32 +0800 | [diff] [blame] | 590 | ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx"); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 591 | if (!ssp->dmach) { |
| 592 | dev_err(ssp->dev, "Failed to request DMA\n"); |
Wei Yongjun | 58ad60b | 2013-04-03 21:06:40 +0800 | [diff] [blame] | 593 | ret = -ENODEV; |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 594 | goto out_master_free; |
| 595 | } |
| 596 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 597 | pm_runtime_enable(ssp->dev); |
| 598 | if (!pm_runtime_enabled(ssp->dev)) { |
| 599 | ret = mxs_spi_runtime_resume(ssp->dev); |
| 600 | if (ret < 0) { |
| 601 | dev_err(ssp->dev, "runtime resume failed\n"); |
| 602 | goto out_dma_release; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | ret = pm_runtime_get_sync(ssp->dev); |
| 607 | if (ret < 0) { |
| 608 | dev_err(ssp->dev, "runtime_get_sync failed\n"); |
| 609 | goto out_pm_runtime_disable; |
| 610 | } |
Fabio Estevam | 9c4a39a | 2013-07-10 00:16:28 -0300 | [diff] [blame] | 611 | |
Marek Vasut | e64d07a | 2012-08-22 22:38:35 +0200 | [diff] [blame] | 612 | clk_set_rate(ssp->clk, clk_freq); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 613 | |
Fabio Estevam | 8498bce | 2013-07-10 00:16:29 -0300 | [diff] [blame] | 614 | ret = stmp_reset_block(ssp->base); |
| 615 | if (ret) |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 616 | goto out_pm_runtime_put; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 617 | |
Jingoo Han | 33e195a | 2013-09-24 13:32:56 +0900 | [diff] [blame] | 618 | ret = devm_spi_register_master(&pdev->dev, master); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 619 | if (ret) { |
| 620 | dev_err(&pdev->dev, "Cannot register SPI master, %d\n", ret); |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 621 | goto out_pm_runtime_put; |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 624 | pm_runtime_put(ssp->dev); |
| 625 | |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 626 | return 0; |
| 627 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 628 | out_pm_runtime_put: |
| 629 | pm_runtime_put(ssp->dev); |
| 630 | out_pm_runtime_disable: |
| 631 | pm_runtime_disable(ssp->dev); |
Fabio Estevam | 9c4a39a | 2013-07-10 00:16:28 -0300 | [diff] [blame] | 632 | out_dma_release: |
Fabio Estevam | e11933f | 2013-07-10 00:16:27 -0300 | [diff] [blame] | 633 | dma_release_channel(ssp->dmach); |
Marek Vasut | 474afc0 | 2012-08-03 17:26:13 +0200 | [diff] [blame] | 634 | out_master_free: |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 635 | spi_master_put(master); |
| 636 | return ret; |
| 637 | } |
| 638 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 639 | static int mxs_spi_remove(struct platform_device *pdev) |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 640 | { |
| 641 | struct spi_master *master; |
| 642 | struct mxs_spi *spi; |
| 643 | struct mxs_ssp *ssp; |
| 644 | |
Wei Yongjun | e322ce9 | 2013-11-15 15:50:31 +0800 | [diff] [blame] | 645 | master = platform_get_drvdata(pdev); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 646 | spi = spi_master_get_devdata(master); |
| 647 | ssp = &spi->ssp; |
| 648 | |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 649 | pm_runtime_disable(&pdev->dev); |
| 650 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 651 | mxs_spi_runtime_suspend(&pdev->dev); |
| 652 | |
Fabio Estevam | e11933f | 2013-07-10 00:16:27 -0300 | [diff] [blame] | 653 | dma_release_channel(ssp->dmach); |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static struct platform_driver mxs_spi_driver = { |
| 659 | .probe = mxs_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 660 | .remove = mxs_spi_remove, |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 661 | .driver = { |
| 662 | .name = DRIVER_NAME, |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 663 | .of_match_table = mxs_spi_dt_ids, |
Uwe Kleine-König | b7969ca | 2017-09-27 17:39:22 +0200 | [diff] [blame] | 664 | .pm = &mxs_spi_pm, |
Marek Vasut | 646781d3 | 2012-08-03 17:26:11 +0200 | [diff] [blame] | 665 | }, |
| 666 | }; |
| 667 | |
| 668 | module_platform_driver(mxs_spi_driver); |
| 669 | |
| 670 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
| 671 | MODULE_DESCRIPTION("MXS SPI master driver"); |
| 672 | MODULE_LICENSE("GPL"); |
| 673 | MODULE_ALIAS("platform:mxs-spi"); |