spi-mxs.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * Freescale MXS SPI master driver
 *
 * Copyright 2012 DENX Software Engineering, GmbH.
 * Copyright 2012 Freescale Semiconductor, Inc.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 *
 * Rework and transition to new API by:
 * Marek Vasut <marex@denx.de>
 *
 * Based on previous attempt by:
 * Fabio Estevam <fabio.estevam@freescale.com>
 *
 * Based on code from U-Boot bootloader by:
 * Marek Vasut <marex@denx.de>
 *
 * Based on spi-stmp.c, which is:
 * Author: Dmitry Pervushin <dimka@embeddedalley.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/highmem.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/completion.h>
#include <linux/gpio.h>
#include <linux/regulator/consumer.h>
47
#include <linux/pm_runtime.h>
48 49 50 51 52 53 54
#include <linux/module.h>
#include <linux/stmp_device.h>
#include <linux/spi/spi.h>
#include <linux/spi/mxs-spi.h>

#define DRIVER_NAME		"mxs-spi"

55 56
/* Use 10S timeout for very long transfers, it should suffice. */
#define SSP_TIMEOUT		10000
57

58 59
#define SG_MAXLEN		0xff00

60 61 62 63 64 65 66
/*
 * Flags for txrx functions.  More efficient that using an argument register for
 * each one.
 */
#define TXRX_WRITE		(1<<0)	/* This is a write */
#define TXRX_DEASSERT_CS	(1<<1)	/* De-assert CS at end of txrx */

67 68
struct mxs_spi {
	struct mxs_ssp		ssp;
69
	struct completion	c;
70
	unsigned int		sck;	/* Rate requested (vs actual) */
71 72 73
};

static int mxs_spi_setup_transfer(struct spi_device *dev,
74
				  const struct spi_transfer *t)
75 76 77
{
	struct mxs_spi *spi = spi_master_get_devdata(dev->master);
	struct mxs_ssp *ssp = &spi->ssp;
78
	const unsigned int hz = min(dev->max_speed_hz, t->speed_hz);
79 80

	if (hz == 0) {
81
		dev_err(&dev->dev, "SPI clock rate of zero not allowed\n");
82 83 84
		return -EINVAL;
	}

85 86 87 88
	if (hz != spi->sck) {
		mxs_ssp_set_clk_rate(ssp, hz);
		/*
		 * Save requested rate, hz, rather than the actual rate,
89
		 * ssp->clk_rate.  Otherwise we would set the rate every transfer
90 91 92 93 94 95 96 97
		 * when the actual rate is not quite the same as requested rate.
		 */
		spi->sck = hz;
		/*
		 * Perhaps we should return an error if the actual clock is
		 * nowhere close to what was requested?
		 */
	}
98

T
Trent Piepho 已提交
99 100
	writel(BM_SSP_CTRL0_LOCK_CS,
		ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
101 102

	writel(BF_SSP_CTRL1_SSP_MODE(BV_SSP_CTRL1_SSP_MODE__SPI) |
103 104 105 106
	       BF_SSP_CTRL1_WORD_LENGTH(BV_SSP_CTRL1_WORD_LENGTH__EIGHT_BITS) |
	       ((dev->mode & SPI_CPOL) ? BM_SSP_CTRL1_POLARITY : 0) |
	       ((dev->mode & SPI_CPHA) ? BM_SSP_CTRL1_PHASE : 0),
	       ssp->base + HW_SSP_CTRL1(ssp));
107 108 109 110 111 112 113

	writel(0x0, ssp->base + HW_SSP_CMD0);
	writel(0x0, ssp->base + HW_SSP_CMD1);

	return 0;
}

114
static u32 mxs_spi_cs_to_reg(unsigned cs)
115
{
116
	u32 select = 0;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

	/*
	 * i.MX28 Datasheet: 17.10.1: HW_SSP_CTRL0
	 *
	 * The bits BM_SSP_CTRL0_WAIT_FOR_CMD and BM_SSP_CTRL0_WAIT_FOR_IRQ
	 * in HW_SSP_CTRL0 register do have multiple usage, please refer to
	 * the datasheet for further details. In SPI mode, they are used to
	 * toggle the chip-select lines (nCS pins).
	 */
	if (cs & 1)
		select |= BM_SSP_CTRL0_WAIT_FOR_CMD;
	if (cs & 2)
		select |= BM_SSP_CTRL0_WAIT_FOR_IRQ;

	return select;
}

static int mxs_ssp_wait(struct mxs_spi *spi, int offset, int mask, bool set)
{
136
	const unsigned long timeout = jiffies + msecs_to_jiffies(SSP_TIMEOUT);
137
	struct mxs_ssp *ssp = &spi->ssp;
138
	u32 reg;
139

140
	do {
141 142
		reg = readl_relaxed(ssp->base + offset);

143 144
		if (!set)
			reg = ~reg;
145

146
		reg &= mask;
147

148 149 150
		if (reg == mask)
			return 0;
	} while (time_before(jiffies, timeout));
151

152
	return -ETIMEDOUT;
153 154
}

155 156 157
static void mxs_ssp_dma_irq_callback(void *param)
{
	struct mxs_spi *spi = param;
J
Jingoo Han 已提交
158

159 160 161 162 163 164
	complete(&spi->c);
}

static irqreturn_t mxs_ssp_irq_handler(int irq, void *dev_id)
{
	struct mxs_ssp *ssp = dev_id;
J
Jingoo Han 已提交
165

166 167 168 169 170 171 172
	dev_err(ssp->dev, "%s[%i] CTRL1=%08x STATUS=%08x\n",
		__func__, __LINE__,
		readl(ssp->base + HW_SSP_CTRL1(ssp)),
		readl(ssp->base + HW_SSP_STATUS(ssp)));
	return IRQ_HANDLED;
}

173
static int mxs_spi_txrx_dma(struct mxs_spi *spi,
174
			    unsigned char *buf, int len,
175
			    unsigned int flags)
176 177
{
	struct mxs_ssp *ssp = &spi->ssp;
178 179 180 181
	struct dma_async_tx_descriptor *desc = NULL;
	const bool vmalloced_buf = is_vmalloc_addr(buf);
	const int desc_len = vmalloced_buf ? PAGE_SIZE : SG_MAXLEN;
	const int sgs = DIV_ROUND_UP(len, desc_len);
182
	int sg_count;
183
	int min, ret;
184
	u32 ctrl0;
185 186
	struct page *vm_page;
	struct {
187
		u32			pio[4];
188 189 190 191
		struct scatterlist	sg;
	} *dma_xfer;

	if (!len)
192
		return -EINVAL;
193

J
Jingoo Han 已提交
194
	dma_xfer = kcalloc(sgs, sizeof(*dma_xfer), GFP_KERNEL);
195 196
	if (!dma_xfer)
		return -ENOMEM;
197

198
	reinit_completion(&spi->c);
199

200
	/* Chip select was already programmed into CTRL0 */
201
	ctrl0 = readl(ssp->base + HW_SSP_CTRL0);
202 203
	ctrl0 &= ~(BM_SSP_CTRL0_XFER_COUNT | BM_SSP_CTRL0_IGNORE_CRC |
		 BM_SSP_CTRL0_READ);
204
	ctrl0 |= BM_SSP_CTRL0_DATA_XFER;
205

206
	if (!(flags & TXRX_WRITE))
207
		ctrl0 |= BM_SSP_CTRL0_READ;
208 209

	/* Queue the DMA data transfer. */
210
	for (sg_count = 0; sg_count < sgs; sg_count++) {
211
		/* Prepare the transfer descriptor. */
212 213
		min = min(len, desc_len);

214 215 216 217 218
		/*
		 * De-assert CS on last segment if flag is set (i.e., no more
		 * transfers will follow)
		 */
		if ((sg_count + 1 == sgs) && (flags & TXRX_DEASSERT_CS))
219 220
			ctrl0 |= BM_SSP_CTRL0_IGNORE_CRC;

221 222
		if (ssp->devid == IMX23_SSP) {
			ctrl0 &= ~BM_SSP_CTRL0_XFER_COUNT;
223
			ctrl0 |= min;
224
		}
225 226 227 228 229 230 231 232 233 234

		dma_xfer[sg_count].pio[0] = ctrl0;
		dma_xfer[sg_count].pio[3] = min;

		if (vmalloced_buf) {
			vm_page = vmalloc_to_page(buf);
			if (!vm_page) {
				ret = -ENOMEM;
				goto err_vmalloc;
			}
235 236 237 238

			sg_init_table(&dma_xfer[sg_count].sg, 1);
			sg_set_page(&dma_xfer[sg_count].sg, vm_page,
				    min, offset_in_page(buf));
239
		} else {
240
			sg_init_one(&dma_xfer[sg_count].sg, buf, min);
241 242 243
		}

		ret = dma_map_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
244
			(flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

		len -= min;
		buf += min;

		/* Queue the PIO register write transfer. */
		desc = dmaengine_prep_slave_sg(ssp->dmach,
				(struct scatterlist *)dma_xfer[sg_count].pio,
				(ssp->devid == IMX23_SSP) ? 1 : 4,
				DMA_TRANS_NONE,
				sg_count ? DMA_PREP_INTERRUPT : 0);
		if (!desc) {
			dev_err(ssp->dev,
				"Failed to get PIO reg. write descriptor.\n");
			ret = -EINVAL;
			goto err_mapped;
		}

		desc = dmaengine_prep_slave_sg(ssp->dmach,
				&dma_xfer[sg_count].sg, 1,
264
				(flags & TXRX_WRITE) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
265 266 267 268 269 270 271 272
				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);

		if (!desc) {
			dev_err(ssp->dev,
				"Failed to get DMA data write descriptor.\n");
			ret = -EINVAL;
			goto err_mapped;
		}
273 274 275 276 277 278 279 280 281 282 283 284 285
	}

	/*
	 * The last descriptor must have this callback,
	 * to finish the DMA transaction.
	 */
	desc->callback = mxs_ssp_dma_irq_callback;
	desc->callback_param = spi;

	/* Start the transfer. */
	dmaengine_submit(desc);
	dma_async_issue_pending(ssp->dmach);

286 287
	if (!wait_for_completion_timeout(&spi->c,
					 msecs_to_jiffies(SSP_TIMEOUT))) {
288 289
		dev_err(ssp->dev, "DMA transfer timeout\n");
		ret = -ETIMEDOUT;
290
		dmaengine_terminate_all(ssp->dmach);
291
		goto err_vmalloc;
292 293 294 295
	}

	ret = 0;

296 297 298 299
err_vmalloc:
	while (--sg_count >= 0) {
err_mapped:
		dma_unmap_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
300
			(flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
301 302
	}

303 304
	kfree(dma_xfer);

305 306 307
	return ret;
}

308
static int mxs_spi_txrx_pio(struct mxs_spi *spi,
309
			    unsigned char *buf, int len,
310
			    unsigned int flags)
311 312 313
{
	struct mxs_ssp *ssp = &spi->ssp;

314 315
	writel(BM_SSP_CTRL0_IGNORE_CRC,
	       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
316 317

	while (len--) {
318
		if (len == 0 && (flags & TXRX_DEASSERT_CS))
319 320
			writel(BM_SSP_CTRL0_IGNORE_CRC,
			       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
321 322 323 324 325 326 327 328 329 330

		if (ssp->devid == IMX23_SSP) {
			writel(BM_SSP_CTRL0_XFER_COUNT,
				ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
			writel(1,
				ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
		} else {
			writel(1, ssp->base + HW_SSP_XFER_SIZE);
		}

331
		if (flags & TXRX_WRITE)
332 333 334 335 336 337 338 339 340 341 342 343
			writel(BM_SSP_CTRL0_READ,
				ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
		else
			writel(BM_SSP_CTRL0_READ,
				ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);

		writel(BM_SSP_CTRL0_RUN,
				ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);

		if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 1))
			return -ETIMEDOUT;

344
		if (flags & TXRX_WRITE)
345 346 347 348 349
			writel(*buf, ssp->base + HW_SSP_DATA(ssp));

		writel(BM_SSP_CTRL0_DATA_XFER,
			     ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);

350
		if (!(flags & TXRX_WRITE)) {
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
			if (mxs_ssp_wait(spi, HW_SSP_STATUS(ssp),
						BM_SSP_STATUS_FIFO_EMPTY, 0))
				return -ETIMEDOUT;

			*buf = (readl(ssp->base + HW_SSP_DATA(ssp)) & 0xff);
		}

		if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 0))
			return -ETIMEDOUT;

		buf++;
	}

	if (len <= 0)
		return 0;

	return -ETIMEDOUT;
}

static int mxs_spi_transfer_one(struct spi_master *master,
				struct spi_message *m)
{
	struct mxs_spi *spi = spi_master_get_devdata(master);
	struct mxs_ssp *ssp = &spi->ssp;
375
	struct spi_transfer *t;
376
	unsigned int flag;
377 378
	int status = 0;

379 380 381 382 383
	/* Program CS register bits here, it will be used for all transfers. */
	writel(BM_SSP_CTRL0_WAIT_FOR_CMD | BM_SSP_CTRL0_WAIT_FOR_IRQ,
	       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
	writel(mxs_spi_cs_to_reg(m->spi->chip_select),
	       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
384

385
	list_for_each_entry(t, &m->transfers, transfer_list) {
386 387 388 389 390

		status = mxs_spi_setup_transfer(m->spi, t);
		if (status)
			break;

391 392 393
		/* De-assert on last transfer, inverted by cs_change flag */
		flag = (&t->transfer_list == m->transfers.prev) ^ t->cs_change ?
		       TXRX_DEASSERT_CS : 0;
394

395 396 397 398 399 400 401 402 403
		/*
		 * Small blocks can be transfered via PIO.
		 * Measured by empiric means:
		 *
		 * dd if=/dev/mtdblock0 of=/dev/null bs=1024k count=1
		 *
		 * DMA only: 2.164808 seconds, 473.0KB/s
		 * Combined: 1.676276 seconds, 610.9KB/s
		 */
404
		if (t->len < 32) {
405 406 407 408 409
			writel(BM_SSP_CTRL1_DMA_ENABLE,
				ssp->base + HW_SSP_CTRL1(ssp) +
				STMP_OFFSET_REG_CLR);

			if (t->tx_buf)
410
				status = mxs_spi_txrx_pio(spi,
411
						(void *)t->tx_buf,
412
						t->len, flag | TXRX_WRITE);
413
			if (t->rx_buf)
414
				status = mxs_spi_txrx_pio(spi,
415
						t->rx_buf, t->len,
416
						flag);
417 418 419 420 421 422
		} else {
			writel(BM_SSP_CTRL1_DMA_ENABLE,
				ssp->base + HW_SSP_CTRL1(ssp) +
				STMP_OFFSET_REG_SET);

			if (t->tx_buf)
423
				status = mxs_spi_txrx_dma(spi,
424
						(void *)t->tx_buf, t->len,
425
						flag | TXRX_WRITE);
426
			if (t->rx_buf)
427
				status = mxs_spi_txrx_dma(spi,
428
						t->rx_buf, t->len,
429
						flag);
430
		}
431

432 433
		if (status) {
			stmp_reset_block(ssp->base);
434
			break;
435
		}
436

437
		m->actual_length += t->len;
438 439
	}

440
	m->status = status;
441 442 443 444 445
	spi_finalize_current_message(master);

	return status;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
static int mxs_spi_runtime_suspend(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	struct mxs_spi *spi = spi_master_get_devdata(master);
	struct mxs_ssp *ssp = &spi->ssp;
	int ret;

	clk_disable_unprepare(ssp->clk);

	ret = pinctrl_pm_select_idle_state(dev);
	if (ret) {
		int ret2 = clk_prepare_enable(ssp->clk);

		if (ret2)
			dev_warn(dev, "Failed to reenable clock after failing pinctrl request (pinctrl: %d, clk: %d)\n",
				 ret, ret2);
	}

	return ret;
}

static int mxs_spi_runtime_resume(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	struct mxs_spi *spi = spi_master_get_devdata(master);
	struct mxs_ssp *ssp = &spi->ssp;
	int ret;

	ret = pinctrl_pm_select_default_state(dev);
	if (ret)
		return ret;

	ret = clk_prepare_enable(ssp->clk);
	if (ret)
		pinctrl_pm_select_idle_state(dev);

	return ret;
}

static int __maybe_unused mxs_spi_suspend(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	int ret;

	ret = spi_master_suspend(master);
	if (ret)
		return ret;

	if (!pm_runtime_suspended(dev))
		return mxs_spi_runtime_suspend(dev);
	else
		return 0;
}

static int __maybe_unused mxs_spi_resume(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	int ret;

	if (!pm_runtime_suspended(dev))
		ret = mxs_spi_runtime_resume(dev);
	else
		ret = 0;
	if (ret)
		return ret;

	ret = spi_master_resume(master);
	if (ret < 0 && !pm_runtime_suspended(dev))
		mxs_spi_runtime_suspend(dev);

	return ret;
}

static const struct dev_pm_ops mxs_spi_pm = {
	SET_RUNTIME_PM_OPS(mxs_spi_runtime_suspend,
			   mxs_spi_runtime_resume, NULL)
	SET_SYSTEM_SLEEP_PM_OPS(mxs_spi_suspend, mxs_spi_resume)
};

525 526 527 528 529 530 531
static const struct of_device_id mxs_spi_dt_ids[] = {
	{ .compatible = "fsl,imx23-spi", .data = (void *) IMX23_SSP, },
	{ .compatible = "fsl,imx28-spi", .data = (void *) IMX28_SSP, },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mxs_spi_dt_ids);

532
static int mxs_spi_probe(struct platform_device *pdev)
533 534 535 536 537 538 539
{
	const struct of_device_id *of_id =
			of_match_device(mxs_spi_dt_ids, &pdev->dev);
	struct device_node *np = pdev->dev.of_node;
	struct spi_master *master;
	struct mxs_spi *spi;
	struct mxs_ssp *ssp;
540
	struct resource *iores;
541 542
	struct clk *clk;
	void __iomem *base;
543 544
	int devid, clk_freq;
	int ret = 0, irq_err;
545

546 547 548 549 550 551 552
	/*
	 * Default clock speed for the SPI core. 160MHz seems to
	 * work reasonably well with most SPI flashes, so use this
	 * as a default. Override with "clock-frequency" DT prop.
	 */
	const int clk_freq_default = 160000000;

553
	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
554
	irq_err = platform_get_irq(pdev, 0);
555
	if (irq_err < 0)
556
		return irq_err;
557

558 559 560
	base = devm_ioremap_resource(&pdev->dev, iores);
	if (IS_ERR(base))
		return PTR_ERR(base);
561 562 563 564 565

	clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(clk))
		return PTR_ERR(clk);

566 567 568 569
	devid = (enum mxs_ssp_id) of_id->data;
	ret = of_property_read_u32(np, "clock-frequency",
				   &clk_freq);
	if (ret)
570
		clk_freq = clk_freq_default;
571 572 573 574 575

	master = spi_alloc_master(&pdev->dev, sizeof(*spi));
	if (!master)
		return -ENOMEM;

576 577
	platform_set_drvdata(pdev, master);

578
	master->transfer_one_message = mxs_spi_transfer_one;
579
	master->bits_per_word_mask = SPI_BPW_MASK(8);
580 581 582 583
	master->mode_bits = SPI_CPOL | SPI_CPHA;
	master->num_chipselect = 3;
	master->dev.of_node = np;
	master->flags = SPI_MASTER_HALF_DUPLEX;
584
	master->auto_runtime_pm = true;
585 586 587 588 589 590 591

	spi = spi_master_get_devdata(master);
	ssp = &spi->ssp;
	ssp->dev = &pdev->dev;
	ssp->clk = clk;
	ssp->base = base;
	ssp->devid = devid;
592

593 594
	init_completion(&spi->c);

595
	ret = devm_request_irq(&pdev->dev, irq_err, mxs_ssp_irq_handler, 0,
596
			       dev_name(&pdev->dev), ssp);
597 598 599
	if (ret)
		goto out_master_free;

600
	ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx");
601 602
	if (!ssp->dmach) {
		dev_err(ssp->dev, "Failed to request DMA\n");
603
		ret = -ENODEV;
604 605
		goto out_master_free;
	}
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620
	pm_runtime_enable(ssp->dev);
	if (!pm_runtime_enabled(ssp->dev)) {
		ret = mxs_spi_runtime_resume(ssp->dev);
		if (ret < 0) {
			dev_err(ssp->dev, "runtime resume failed\n");
			goto out_dma_release;
		}
	}

	ret = pm_runtime_get_sync(ssp->dev);
	if (ret < 0) {
		dev_err(ssp->dev, "runtime_get_sync failed\n");
		goto out_pm_runtime_disable;
	}
621

622
	clk_set_rate(ssp->clk, clk_freq);
623

624 625
	ret = stmp_reset_block(ssp->base);
	if (ret)
626
		goto out_pm_runtime_put;
627

628
	ret = devm_spi_register_master(&pdev->dev, master);
629 630
	if (ret) {
		dev_err(&pdev->dev, "Cannot register SPI master, %d\n", ret);
631
		goto out_pm_runtime_put;
632 633
	}

634 635
	pm_runtime_put(ssp->dev);

636 637
	return 0;

638 639 640 641
out_pm_runtime_put:
	pm_runtime_put(ssp->dev);
out_pm_runtime_disable:
	pm_runtime_disable(ssp->dev);
642
out_dma_release:
643
	dma_release_channel(ssp->dmach);
644
out_master_free:
645 646 647 648
	spi_master_put(master);
	return ret;
}

649
static int mxs_spi_remove(struct platform_device *pdev)
650 651 652 653 654
{
	struct spi_master *master;
	struct mxs_spi *spi;
	struct mxs_ssp *ssp;

655
	master = platform_get_drvdata(pdev);
656 657 658
	spi = spi_master_get_devdata(master);
	ssp = &spi->ssp;

659 660 661 662
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		mxs_spi_runtime_suspend(&pdev->dev);

663
	dma_release_channel(ssp->dmach);
664 665 666 667 668 669

	return 0;
}

static struct platform_driver mxs_spi_driver = {
	.probe	= mxs_spi_probe,
670
	.remove	= mxs_spi_remove,
671 672 673
	.driver	= {
		.name	= DRIVER_NAME,
		.of_match_table = mxs_spi_dt_ids,
674
		.pm = &mxs_spi_pm,
675 676 677 678 679 680 681 682 683
	},
};

module_platform_driver(mxs_spi_driver);

MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
MODULE_DESCRIPTION("MXS SPI master driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:mxs-spi");