spi-mxs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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>
  20. #include <linux/kernel.h>
  21. #include <linux/ioport.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/highmem.h>
  31. #include <linux/clk.h>
  32. #include <linux/err.h>
  33. #include <linux/completion.h>
  34. #include <linux/gpio.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/module.h>
  38. #include <linux/stmp_device.h>
  39. #include <linux/spi/spi.h>
  40. #include <linux/spi/mxs-spi.h>
  41. #define DRIVER_NAME "mxs-spi"
  42. /* Use 10S timeout for very long transfers, it should suffice. */
  43. #define SSP_TIMEOUT 10000
  44. #define SG_MAXLEN 0xff00
  45. /*
  46. * Flags for txrx functions. More efficient that using an argument register for
  47. * each one.
  48. */
  49. #define TXRX_WRITE (1<<0) /* This is a write */
  50. #define TXRX_DEASSERT_CS (1<<1) /* De-assert CS at end of txrx */
  51. struct mxs_spi {
  52. struct mxs_ssp ssp;
  53. struct completion c;
  54. unsigned int sck; /* Rate requested (vs actual) */
  55. };
  56. static int mxs_spi_setup_transfer(struct spi_device *dev,
  57. const struct spi_transfer *t)
  58. {
  59. struct mxs_spi *spi = spi_master_get_devdata(dev->master);
  60. struct mxs_ssp *ssp = &spi->ssp;
  61. const unsigned int hz = min(dev->max_speed_hz, t->speed_hz);
  62. if (hz == 0) {
  63. dev_err(&dev->dev, "SPI clock rate of zero not allowed\n");
  64. return -EINVAL;
  65. }
  66. if (hz != spi->sck) {
  67. mxs_ssp_set_clk_rate(ssp, hz);
  68. /*
  69. * Save requested rate, hz, rather than the actual rate,
  70. * ssp->clk_rate. Otherwise we would set the rate every transfer
  71. * when the actual rate is not quite the same as requested rate.
  72. */
  73. spi->sck = hz;
  74. /*
  75. * Perhaps we should return an error if the actual clock is
  76. * nowhere close to what was requested?
  77. */
  78. }
  79. writel(BM_SSP_CTRL0_LOCK_CS,
  80. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  81. writel(BF_SSP_CTRL1_SSP_MODE(BV_SSP_CTRL1_SSP_MODE__SPI) |
  82. BF_SSP_CTRL1_WORD_LENGTH(BV_SSP_CTRL1_WORD_LENGTH__EIGHT_BITS) |
  83. ((dev->mode & SPI_CPOL) ? BM_SSP_CTRL1_POLARITY : 0) |
  84. ((dev->mode & SPI_CPHA) ? BM_SSP_CTRL1_PHASE : 0),
  85. ssp->base + HW_SSP_CTRL1(ssp));
  86. writel(0x0, ssp->base + HW_SSP_CMD0);
  87. writel(0x0, ssp->base + HW_SSP_CMD1);
  88. return 0;
  89. }
  90. static u32 mxs_spi_cs_to_reg(unsigned cs)
  91. {
  92. u32 select = 0;
  93. /*
  94. * i.MX28 Datasheet: 17.10.1: HW_SSP_CTRL0
  95. *
  96. * The bits BM_SSP_CTRL0_WAIT_FOR_CMD and BM_SSP_CTRL0_WAIT_FOR_IRQ
  97. * in HW_SSP_CTRL0 register do have multiple usage, please refer to
  98. * the datasheet for further details. In SPI mode, they are used to
  99. * toggle the chip-select lines (nCS pins).
  100. */
  101. if (cs & 1)
  102. select |= BM_SSP_CTRL0_WAIT_FOR_CMD;
  103. if (cs & 2)
  104. select |= BM_SSP_CTRL0_WAIT_FOR_IRQ;
  105. return select;
  106. }
  107. static int mxs_ssp_wait(struct mxs_spi *spi, int offset, int mask, bool set)
  108. {
  109. const unsigned long timeout = jiffies + msecs_to_jiffies(SSP_TIMEOUT);
  110. struct mxs_ssp *ssp = &spi->ssp;
  111. u32 reg;
  112. do {
  113. reg = readl_relaxed(ssp->base + offset);
  114. if (!set)
  115. reg = ~reg;
  116. reg &= mask;
  117. if (reg == mask)
  118. return 0;
  119. } while (time_before(jiffies, timeout));
  120. return -ETIMEDOUT;
  121. }
  122. static void mxs_ssp_dma_irq_callback(void *param)
  123. {
  124. struct mxs_spi *spi = param;
  125. complete(&spi->c);
  126. }
  127. static irqreturn_t mxs_ssp_irq_handler(int irq, void *dev_id)
  128. {
  129. struct mxs_ssp *ssp = dev_id;
  130. dev_err(ssp->dev, "%s[%i] CTRL1=%08x STATUS=%08x\n",
  131. __func__, __LINE__,
  132. readl(ssp->base + HW_SSP_CTRL1(ssp)),
  133. readl(ssp->base + HW_SSP_STATUS(ssp)));
  134. return IRQ_HANDLED;
  135. }
  136. static int mxs_spi_txrx_dma(struct mxs_spi *spi,
  137. unsigned char *buf, int len,
  138. unsigned int flags)
  139. {
  140. struct mxs_ssp *ssp = &spi->ssp;
  141. struct dma_async_tx_descriptor *desc = NULL;
  142. const bool vmalloced_buf = is_vmalloc_addr(buf);
  143. const int desc_len = vmalloced_buf ? PAGE_SIZE : SG_MAXLEN;
  144. const int sgs = DIV_ROUND_UP(len, desc_len);
  145. int sg_count;
  146. int min, ret;
  147. u32 ctrl0;
  148. struct page *vm_page;
  149. struct {
  150. u32 pio[4];
  151. struct scatterlist sg;
  152. } *dma_xfer;
  153. if (!len)
  154. return -EINVAL;
  155. dma_xfer = kcalloc(sgs, sizeof(*dma_xfer), GFP_KERNEL);
  156. if (!dma_xfer)
  157. return -ENOMEM;
  158. reinit_completion(&spi->c);
  159. /* Chip select was already programmed into CTRL0 */
  160. ctrl0 = readl(ssp->base + HW_SSP_CTRL0);
  161. ctrl0 &= ~(BM_SSP_CTRL0_XFER_COUNT | BM_SSP_CTRL0_IGNORE_CRC |
  162. BM_SSP_CTRL0_READ);
  163. ctrl0 |= BM_SSP_CTRL0_DATA_XFER;
  164. if (!(flags & TXRX_WRITE))
  165. ctrl0 |= BM_SSP_CTRL0_READ;
  166. /* Queue the DMA data transfer. */
  167. for (sg_count = 0; sg_count < sgs; sg_count++) {
  168. /* Prepare the transfer descriptor. */
  169. min = min(len, desc_len);
  170. /*
  171. * De-assert CS on last segment if flag is set (i.e., no more
  172. * transfers will follow)
  173. */
  174. if ((sg_count + 1 == sgs) && (flags & TXRX_DEASSERT_CS))
  175. ctrl0 |= BM_SSP_CTRL0_IGNORE_CRC;
  176. if (ssp->devid == IMX23_SSP) {
  177. ctrl0 &= ~BM_SSP_CTRL0_XFER_COUNT;
  178. ctrl0 |= min;
  179. }
  180. dma_xfer[sg_count].pio[0] = ctrl0;
  181. dma_xfer[sg_count].pio[3] = min;
  182. if (vmalloced_buf) {
  183. vm_page = vmalloc_to_page(buf);
  184. if (!vm_page) {
  185. ret = -ENOMEM;
  186. goto err_vmalloc;
  187. }
  188. sg_init_table(&dma_xfer[sg_count].sg, 1);
  189. sg_set_page(&dma_xfer[sg_count].sg, vm_page,
  190. min, offset_in_page(buf));
  191. } else {
  192. sg_init_one(&dma_xfer[sg_count].sg, buf, min);
  193. }
  194. ret = dma_map_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
  195. (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  196. len -= min;
  197. buf += min;
  198. /* Queue the PIO register write transfer. */
  199. desc = dmaengine_prep_slave_sg(ssp->dmach,
  200. (struct scatterlist *)dma_xfer[sg_count].pio,
  201. (ssp->devid == IMX23_SSP) ? 1 : 4,
  202. DMA_TRANS_NONE,
  203. sg_count ? DMA_PREP_INTERRUPT : 0);
  204. if (!desc) {
  205. dev_err(ssp->dev,
  206. "Failed to get PIO reg. write descriptor.\n");
  207. ret = -EINVAL;
  208. goto err_mapped;
  209. }
  210. desc = dmaengine_prep_slave_sg(ssp->dmach,
  211. &dma_xfer[sg_count].sg, 1,
  212. (flags & TXRX_WRITE) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  213. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  214. if (!desc) {
  215. dev_err(ssp->dev,
  216. "Failed to get DMA data write descriptor.\n");
  217. ret = -EINVAL;
  218. goto err_mapped;
  219. }
  220. }
  221. /*
  222. * The last descriptor must have this callback,
  223. * to finish the DMA transaction.
  224. */
  225. desc->callback = mxs_ssp_dma_irq_callback;
  226. desc->callback_param = spi;
  227. /* Start the transfer. */
  228. dmaengine_submit(desc);
  229. dma_async_issue_pending(ssp->dmach);
  230. if (!wait_for_completion_timeout(&spi->c,
  231. msecs_to_jiffies(SSP_TIMEOUT))) {
  232. dev_err(ssp->dev, "DMA transfer timeout\n");
  233. ret = -ETIMEDOUT;
  234. dmaengine_terminate_all(ssp->dmach);
  235. goto err_vmalloc;
  236. }
  237. ret = 0;
  238. err_vmalloc:
  239. while (--sg_count >= 0) {
  240. err_mapped:
  241. dma_unmap_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
  242. (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  243. }
  244. kfree(dma_xfer);
  245. return ret;
  246. }
  247. static int mxs_spi_txrx_pio(struct mxs_spi *spi,
  248. unsigned char *buf, int len,
  249. unsigned int flags)
  250. {
  251. struct mxs_ssp *ssp = &spi->ssp;
  252. writel(BM_SSP_CTRL0_IGNORE_CRC,
  253. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  254. while (len--) {
  255. if (len == 0 && (flags & TXRX_DEASSERT_CS))
  256. writel(BM_SSP_CTRL0_IGNORE_CRC,
  257. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  258. if (ssp->devid == IMX23_SSP) {
  259. writel(BM_SSP_CTRL0_XFER_COUNT,
  260. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  261. writel(1,
  262. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  263. } else {
  264. writel(1, ssp->base + HW_SSP_XFER_SIZE);
  265. }
  266. if (flags & TXRX_WRITE)
  267. writel(BM_SSP_CTRL0_READ,
  268. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  269. else
  270. writel(BM_SSP_CTRL0_READ,
  271. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  272. writel(BM_SSP_CTRL0_RUN,
  273. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  274. if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 1))
  275. return -ETIMEDOUT;
  276. if (flags & TXRX_WRITE)
  277. writel(*buf, ssp->base + HW_SSP_DATA(ssp));
  278. writel(BM_SSP_CTRL0_DATA_XFER,
  279. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  280. if (!(flags & TXRX_WRITE)) {
  281. if (mxs_ssp_wait(spi, HW_SSP_STATUS(ssp),
  282. BM_SSP_STATUS_FIFO_EMPTY, 0))
  283. return -ETIMEDOUT;
  284. *buf = (readl(ssp->base + HW_SSP_DATA(ssp)) & 0xff);
  285. }
  286. if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 0))
  287. return -ETIMEDOUT;
  288. buf++;
  289. }
  290. if (len <= 0)
  291. return 0;
  292. return -ETIMEDOUT;
  293. }
  294. static int mxs_spi_transfer_one(struct spi_master *master,
  295. struct spi_message *m)
  296. {
  297. struct mxs_spi *spi = spi_master_get_devdata(master);
  298. struct mxs_ssp *ssp = &spi->ssp;
  299. struct spi_transfer *t;
  300. unsigned int flag;
  301. int status = 0;
  302. /* Program CS register bits here, it will be used for all transfers. */
  303. writel(BM_SSP_CTRL0_WAIT_FOR_CMD | BM_SSP_CTRL0_WAIT_FOR_IRQ,
  304. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  305. writel(mxs_spi_cs_to_reg(m->spi->chip_select),
  306. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  307. list_for_each_entry(t, &m->transfers, transfer_list) {
  308. status = mxs_spi_setup_transfer(m->spi, t);
  309. if (status)
  310. break;
  311. /* De-assert on last transfer, inverted by cs_change flag */
  312. flag = (&t->transfer_list == m->transfers.prev) ^ t->cs_change ?
  313. TXRX_DEASSERT_CS : 0;
  314. /*
  315. * Small blocks can be transfered via PIO.
  316. * Measured by empiric means:
  317. *
  318. * dd if=/dev/mtdblock0 of=/dev/null bs=1024k count=1
  319. *
  320. * DMA only: 2.164808 seconds, 473.0KB/s
  321. * Combined: 1.676276 seconds, 610.9KB/s
  322. */
  323. if (t->len < 32) {
  324. writel(BM_SSP_CTRL1_DMA_ENABLE,
  325. ssp->base + HW_SSP_CTRL1(ssp) +
  326. STMP_OFFSET_REG_CLR);
  327. if (t->tx_buf)
  328. status = mxs_spi_txrx_pio(spi,
  329. (void *)t->tx_buf,
  330. t->len, flag | TXRX_WRITE);
  331. if (t->rx_buf)
  332. status = mxs_spi_txrx_pio(spi,
  333. t->rx_buf, t->len,
  334. flag);
  335. } else {
  336. writel(BM_SSP_CTRL1_DMA_ENABLE,
  337. ssp->base + HW_SSP_CTRL1(ssp) +
  338. STMP_OFFSET_REG_SET);
  339. if (t->tx_buf)
  340. status = mxs_spi_txrx_dma(spi,
  341. (void *)t->tx_buf, t->len,
  342. flag | TXRX_WRITE);
  343. if (t->rx_buf)
  344. status = mxs_spi_txrx_dma(spi,
  345. t->rx_buf, t->len,
  346. flag);
  347. }
  348. if (status) {
  349. stmp_reset_block(ssp->base);
  350. break;
  351. }
  352. m->actual_length += t->len;
  353. }
  354. m->status = status;
  355. spi_finalize_current_message(master);
  356. return status;
  357. }
  358. static int mxs_spi_runtime_suspend(struct device *dev)
  359. {
  360. struct spi_master *master = dev_get_drvdata(dev);
  361. struct mxs_spi *spi = spi_master_get_devdata(master);
  362. struct mxs_ssp *ssp = &spi->ssp;
  363. int ret;
  364. clk_disable_unprepare(ssp->clk);
  365. ret = pinctrl_pm_select_idle_state(dev);
  366. if (ret) {
  367. int ret2 = clk_prepare_enable(ssp->clk);
  368. if (ret2)
  369. dev_warn(dev, "Failed to reenable clock after failing pinctrl request (pinctrl: %d, clk: %d)\n",
  370. ret, ret2);
  371. }
  372. return ret;
  373. }
  374. static int mxs_spi_runtime_resume(struct device *dev)
  375. {
  376. struct spi_master *master = dev_get_drvdata(dev);
  377. struct mxs_spi *spi = spi_master_get_devdata(master);
  378. struct mxs_ssp *ssp = &spi->ssp;
  379. int ret;
  380. ret = pinctrl_pm_select_default_state(dev);
  381. if (ret)
  382. return ret;
  383. ret = clk_prepare_enable(ssp->clk);
  384. if (ret)
  385. pinctrl_pm_select_idle_state(dev);
  386. return ret;
  387. }
  388. static int __maybe_unused mxs_spi_suspend(struct device *dev)
  389. {
  390. struct spi_master *master = dev_get_drvdata(dev);
  391. int ret;
  392. ret = spi_master_suspend(master);
  393. if (ret)
  394. return ret;
  395. if (!pm_runtime_suspended(dev))
  396. return mxs_spi_runtime_suspend(dev);
  397. else
  398. return 0;
  399. }
  400. static int __maybe_unused mxs_spi_resume(struct device *dev)
  401. {
  402. struct spi_master *master = dev_get_drvdata(dev);
  403. int ret;
  404. if (!pm_runtime_suspended(dev))
  405. ret = mxs_spi_runtime_resume(dev);
  406. else
  407. ret = 0;
  408. if (ret)
  409. return ret;
  410. ret = spi_master_resume(master);
  411. if (ret < 0 && !pm_runtime_suspended(dev))
  412. mxs_spi_runtime_suspend(dev);
  413. return ret;
  414. }
  415. static const struct dev_pm_ops mxs_spi_pm = {
  416. SET_RUNTIME_PM_OPS(mxs_spi_runtime_suspend,
  417. mxs_spi_runtime_resume, NULL)
  418. SET_SYSTEM_SLEEP_PM_OPS(mxs_spi_suspend, mxs_spi_resume)
  419. };
  420. static const struct of_device_id mxs_spi_dt_ids[] = {
  421. { .compatible = "fsl,imx23-spi", .data = (void *) IMX23_SSP, },
  422. { .compatible = "fsl,imx28-spi", .data = (void *) IMX28_SSP, },
  423. { /* sentinel */ }
  424. };
  425. MODULE_DEVICE_TABLE(of, mxs_spi_dt_ids);
  426. static int mxs_spi_probe(struct platform_device *pdev)
  427. {
  428. const struct of_device_id *of_id =
  429. of_match_device(mxs_spi_dt_ids, &pdev->dev);
  430. struct device_node *np = pdev->dev.of_node;
  431. struct spi_master *master;
  432. struct mxs_spi *spi;
  433. struct mxs_ssp *ssp;
  434. struct resource *iores;
  435. struct clk *clk;
  436. void __iomem *base;
  437. int devid, clk_freq;
  438. int ret = 0, irq_err;
  439. /*
  440. * Default clock speed for the SPI core. 160MHz seems to
  441. * work reasonably well with most SPI flashes, so use this
  442. * as a default. Override with "clock-frequency" DT prop.
  443. */
  444. const int clk_freq_default = 160000000;
  445. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  446. irq_err = platform_get_irq(pdev, 0);
  447. if (irq_err < 0)
  448. return irq_err;
  449. base = devm_ioremap_resource(&pdev->dev, iores);
  450. if (IS_ERR(base))
  451. return PTR_ERR(base);
  452. clk = devm_clk_get(&pdev->dev, NULL);
  453. if (IS_ERR(clk))
  454. return PTR_ERR(clk);
  455. devid = (enum mxs_ssp_id) of_id->data;
  456. ret = of_property_read_u32(np, "clock-frequency",
  457. &clk_freq);
  458. if (ret)
  459. clk_freq = clk_freq_default;
  460. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  461. if (!master)
  462. return -ENOMEM;
  463. platform_set_drvdata(pdev, master);
  464. master->transfer_one_message = mxs_spi_transfer_one;
  465. master->bits_per_word_mask = SPI_BPW_MASK(8);
  466. master->mode_bits = SPI_CPOL | SPI_CPHA;
  467. master->num_chipselect = 3;
  468. master->dev.of_node = np;
  469. master->flags = SPI_MASTER_HALF_DUPLEX;
  470. master->auto_runtime_pm = true;
  471. spi = spi_master_get_devdata(master);
  472. ssp = &spi->ssp;
  473. ssp->dev = &pdev->dev;
  474. ssp->clk = clk;
  475. ssp->base = base;
  476. ssp->devid = devid;
  477. init_completion(&spi->c);
  478. ret = devm_request_irq(&pdev->dev, irq_err, mxs_ssp_irq_handler, 0,
  479. dev_name(&pdev->dev), ssp);
  480. if (ret)
  481. goto out_master_free;
  482. ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx");
  483. if (!ssp->dmach) {
  484. dev_err(ssp->dev, "Failed to request DMA\n");
  485. ret = -ENODEV;
  486. goto out_master_free;
  487. }
  488. pm_runtime_enable(ssp->dev);
  489. if (!pm_runtime_enabled(ssp->dev)) {
  490. ret = mxs_spi_runtime_resume(ssp->dev);
  491. if (ret < 0) {
  492. dev_err(ssp->dev, "runtime resume failed\n");
  493. goto out_dma_release;
  494. }
  495. }
  496. ret = pm_runtime_get_sync(ssp->dev);
  497. if (ret < 0) {
  498. dev_err(ssp->dev, "runtime_get_sync failed\n");
  499. goto out_pm_runtime_disable;
  500. }
  501. clk_set_rate(ssp->clk, clk_freq);
  502. ret = stmp_reset_block(ssp->base);
  503. if (ret)
  504. goto out_pm_runtime_put;
  505. ret = devm_spi_register_master(&pdev->dev, master);
  506. if (ret) {
  507. dev_err(&pdev->dev, "Cannot register SPI master, %d\n", ret);
  508. goto out_pm_runtime_put;
  509. }
  510. pm_runtime_put(ssp->dev);
  511. return 0;
  512. out_pm_runtime_put:
  513. pm_runtime_put(ssp->dev);
  514. out_pm_runtime_disable:
  515. pm_runtime_disable(ssp->dev);
  516. out_dma_release:
  517. dma_release_channel(ssp->dmach);
  518. out_master_free:
  519. spi_master_put(master);
  520. return ret;
  521. }
  522. static int mxs_spi_remove(struct platform_device *pdev)
  523. {
  524. struct spi_master *master;
  525. struct mxs_spi *spi;
  526. struct mxs_ssp *ssp;
  527. master = platform_get_drvdata(pdev);
  528. spi = spi_master_get_devdata(master);
  529. ssp = &spi->ssp;
  530. pm_runtime_disable(&pdev->dev);
  531. if (!pm_runtime_status_suspended(&pdev->dev))
  532. mxs_spi_runtime_suspend(&pdev->dev);
  533. dma_release_channel(ssp->dmach);
  534. return 0;
  535. }
  536. static struct platform_driver mxs_spi_driver = {
  537. .probe = mxs_spi_probe,
  538. .remove = mxs_spi_remove,
  539. .driver = {
  540. .name = DRIVER_NAME,
  541. .of_match_table = mxs_spi_dt_ids,
  542. .pm = &mxs_spi_pm,
  543. },
  544. };
  545. module_platform_driver(mxs_spi_driver);
  546. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  547. MODULE_DESCRIPTION("MXS SPI master driver");
  548. MODULE_LICENSE("GPL");
  549. MODULE_ALIAS("platform:mxs-spi");