1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4 * Copyright (C) 2013, 2021 Intel Corporation
7 #include <linux/acpi.h>
8 #include <linux/atomic.h>
9 #include <linux/bitops.h>
10 #include <linux/bug.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/dmaengine.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
20 #include <linux/ioport.h>
21 #include <linux/math64.h>
22 #include <linux/minmax.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
31 #include <linux/spi/spi.h>
33 #include "spi-pxa2xx.h"
35 MODULE_AUTHOR("Stephen Street");
36 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
37 MODULE_LICENSE("GPL");
38 MODULE_ALIAS("platform:pxa2xx-spi");
40 #define TIMOUT_DFLT 1000
43 * For testing SSCR1 changes that require SSP restart, basically
44 * everything except the service and interrupt enables, the PXA270 developer
45 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
46 * list, but the PXA255 developer manual says all bits without really meaning
47 * the service and interrupt enables.
49 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
50 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
51 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
52 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
53 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
54 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
56 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
57 | QUARK_X1000_SSCR1_EFWR \
58 | QUARK_X1000_SSCR1_RFT \
59 | QUARK_X1000_SSCR1_TFT \
60 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
62 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
63 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
64 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
65 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
66 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
67 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
73 u16 lpss_rx_threshold;
74 u16 lpss_tx_threshold;
77 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
78 #define LPSS_CS_CONTROL_SW_MODE BIT(0)
79 #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
80 #define LPSS_CAPS_CS_EN_SHIFT 9
81 #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
83 #define LPSS_PRIV_CLOCK_GATE 0x38
84 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
85 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
88 /* LPSS offset from drv_data->ioaddr */
90 /* Register offsets from drv_data->lpss_base or -1 */
99 /* Chip select control */
100 unsigned cs_sel_shift;
101 unsigned cs_sel_mask;
104 unsigned cs_clk_stays_gated : 1;
107 /* Keep these sorted with enum pxa_ssp_type */
108 static const struct lpss_config lpss_platforms[] = {
114 .reg_capabilities = -1,
116 .tx_threshold_lo = 160,
117 .tx_threshold_hi = 224,
124 .reg_capabilities = -1,
126 .tx_threshold_lo = 160,
127 .tx_threshold_hi = 224,
134 .reg_capabilities = -1,
136 .tx_threshold_lo = 160,
137 .tx_threshold_hi = 224,
139 .cs_sel_mask = 1 << 2,
147 .reg_capabilities = -1,
149 .tx_threshold_lo = 32,
150 .tx_threshold_hi = 56,
157 .reg_capabilities = 0xfc,
159 .tx_threshold_lo = 16,
160 .tx_threshold_hi = 48,
162 .cs_sel_mask = 3 << 8,
163 .cs_clk_stays_gated = true,
170 .reg_capabilities = 0xfc,
172 .tx_threshold_lo = 32,
173 .tx_threshold_hi = 56,
175 .cs_sel_mask = 3 << 8,
176 .cs_clk_stays_gated = true,
180 static inline const struct lpss_config
181 *lpss_get_config(const struct driver_data *drv_data)
183 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
186 static bool is_lpss_ssp(const struct driver_data *drv_data)
188 switch (drv_data->ssp_type) {
201 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
203 return drv_data->ssp_type == QUARK_X1000_SSP;
206 static bool is_mmp2_ssp(const struct driver_data *drv_data)
208 return drv_data->ssp_type == MMP2_SSP;
211 static bool is_mrfld_ssp(const struct driver_data *drv_data)
213 return drv_data->ssp_type == MRFLD_SSP;
216 static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value)
218 if ((pxa2xx_spi_read(drv_data, reg) & mask) != value)
219 pxa2xx_spi_write(drv_data, reg, value & mask);
222 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
224 switch (drv_data->ssp_type) {
225 case QUARK_X1000_SSP:
226 return QUARK_X1000_SSCR1_CHANGE_MASK;
228 return CE4100_SSCR1_CHANGE_MASK;
230 return SSCR1_CHANGE_MASK;
235 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
237 switch (drv_data->ssp_type) {
238 case QUARK_X1000_SSP:
239 return RX_THRESH_QUARK_X1000_DFLT;
241 return RX_THRESH_CE4100_DFLT;
243 return RX_THRESH_DFLT;
247 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
251 switch (drv_data->ssp_type) {
252 case QUARK_X1000_SSP:
253 mask = QUARK_X1000_SSSR_TFL_MASK;
256 mask = CE4100_SSSR_TFL_MASK;
259 mask = SSSR_TFL_MASK;
263 return read_SSSR_bits(drv_data, mask) == mask;
266 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
271 switch (drv_data->ssp_type) {
272 case QUARK_X1000_SSP:
273 mask = QUARK_X1000_SSCR1_RFT;
276 mask = CE4100_SSCR1_RFT;
285 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
286 u32 *sccr1_reg, u32 threshold)
288 switch (drv_data->ssp_type) {
289 case QUARK_X1000_SSP:
290 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
293 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
296 *sccr1_reg |= SSCR1_RxTresh(threshold);
301 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
302 u32 clk_div, u8 bits)
304 switch (drv_data->ssp_type) {
305 case QUARK_X1000_SSP:
307 | QUARK_X1000_SSCR0_Motorola
308 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits);
312 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
313 | (bits > 16 ? SSCR0_EDSS : 0);
318 * Read and write LPSS SSP private registers. Caller must first check that
319 * is_lpss_ssp() returns true before these can be called.
321 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
323 WARN_ON(!drv_data->lpss_base);
324 return readl(drv_data->lpss_base + offset);
327 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
328 unsigned offset, u32 value)
330 WARN_ON(!drv_data->lpss_base);
331 writel(value, drv_data->lpss_base + offset);
335 * lpss_ssp_setup - perform LPSS SSP specific setup
336 * @drv_data: pointer to the driver private data
338 * Perform LPSS SSP specific setup. This function must be called first if
339 * one is going to use LPSS SSP private registers.
341 static void lpss_ssp_setup(struct driver_data *drv_data)
343 const struct lpss_config *config;
346 config = lpss_get_config(drv_data);
347 drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset;
349 /* Enable software chip select control */
350 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
351 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
352 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
353 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
355 /* Enable multiblock DMA transfers */
356 if (drv_data->controller_info->enable_dma) {
357 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
359 if (config->reg_general >= 0) {
360 value = __lpss_ssp_read_priv(drv_data,
361 config->reg_general);
362 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
363 __lpss_ssp_write_priv(drv_data,
364 config->reg_general, value);
369 static void lpss_ssp_select_cs(struct spi_device *spi,
370 const struct lpss_config *config)
372 struct driver_data *drv_data =
373 spi_controller_get_devdata(spi->controller);
376 if (!config->cs_sel_mask)
379 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
381 cs = spi_get_chipselect(spi, 0);
382 cs <<= config->cs_sel_shift;
383 if (cs != (value & config->cs_sel_mask)) {
385 * When switching another chip select output active the
386 * output must be selected first and wait 2 ssp_clk cycles
387 * before changing state to active. Otherwise a short
388 * glitch will occur on the previous chip select since
389 * output select is latched but state control is not.
391 value &= ~config->cs_sel_mask;
393 __lpss_ssp_write_priv(drv_data,
394 config->reg_cs_ctrl, value);
396 (drv_data->controller->max_speed_hz / 2));
400 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
402 struct driver_data *drv_data =
403 spi_controller_get_devdata(spi->controller);
404 const struct lpss_config *config;
407 config = lpss_get_config(drv_data);
410 lpss_ssp_select_cs(spi, config);
412 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
414 value &= ~LPSS_CS_CONTROL_CS_HIGH;
416 value |= LPSS_CS_CONTROL_CS_HIGH;
417 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
418 if (config->cs_clk_stays_gated) {
422 * Changing CS alone when dynamic clock gating is on won't
423 * actually flip CS at that time. This ruins SPI transfers
424 * that specify delays, or have no data. Toggle the clock mode
425 * to force on briefly to poke the CS pin to move.
427 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
428 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
429 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
431 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
432 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
436 static void cs_assert(struct spi_device *spi)
438 struct driver_data *drv_data =
439 spi_controller_get_devdata(spi->controller);
441 if (drv_data->ssp_type == CE4100_SSP) {
442 pxa2xx_spi_write(drv_data, SSSR, spi_get_chipselect(spi, 0));
446 if (is_lpss_ssp(drv_data))
447 lpss_ssp_cs_control(spi, true);
450 static void cs_deassert(struct spi_device *spi)
452 struct driver_data *drv_data =
453 spi_controller_get_devdata(spi->controller);
454 unsigned long timeout;
456 if (drv_data->ssp_type == CE4100_SSP)
459 /* Wait until SSP becomes idle before deasserting the CS */
460 timeout = jiffies + msecs_to_jiffies(10);
461 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
462 !time_after(jiffies, timeout))
465 if (is_lpss_ssp(drv_data))
466 lpss_ssp_cs_control(spi, false);
469 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
477 int pxa2xx_spi_flush(struct driver_data *drv_data)
479 unsigned long limit = loops_per_jiffy << 1;
482 while (read_SSSR_bits(drv_data, SSSR_RNE))
483 pxa2xx_spi_read(drv_data, SSDR);
484 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
485 write_SSSR_CS(drv_data, SSSR_ROR);
490 static void pxa2xx_spi_off(struct driver_data *drv_data)
492 /* On MMP, disabling SSE seems to corrupt the Rx FIFO */
493 if (is_mmp2_ssp(drv_data))
496 pxa_ssp_disable(drv_data->ssp);
499 static int null_writer(struct driver_data *drv_data)
501 u8 n_bytes = drv_data->n_bytes;
503 if (pxa2xx_spi_txfifo_full(drv_data)
504 || (drv_data->tx == drv_data->tx_end))
507 pxa2xx_spi_write(drv_data, SSDR, 0);
508 drv_data->tx += n_bytes;
513 static int null_reader(struct driver_data *drv_data)
515 u8 n_bytes = drv_data->n_bytes;
517 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
518 pxa2xx_spi_read(drv_data, SSDR);
519 drv_data->rx += n_bytes;
522 return drv_data->rx == drv_data->rx_end;
525 static int u8_writer(struct driver_data *drv_data)
527 if (pxa2xx_spi_txfifo_full(drv_data)
528 || (drv_data->tx == drv_data->tx_end))
531 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
537 static int u8_reader(struct driver_data *drv_data)
539 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
540 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
544 return drv_data->rx == drv_data->rx_end;
547 static int u16_writer(struct driver_data *drv_data)
549 if (pxa2xx_spi_txfifo_full(drv_data)
550 || (drv_data->tx == drv_data->tx_end))
553 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
559 static int u16_reader(struct driver_data *drv_data)
561 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
562 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
566 return drv_data->rx == drv_data->rx_end;
569 static int u32_writer(struct driver_data *drv_data)
571 if (pxa2xx_spi_txfifo_full(drv_data)
572 || (drv_data->tx == drv_data->tx_end))
575 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
581 static int u32_reader(struct driver_data *drv_data)
583 while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) {
584 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
588 return drv_data->rx == drv_data->rx_end;
591 static void reset_sccr1(struct driver_data *drv_data)
593 u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold;
594 struct chip_data *chip;
596 if (drv_data->controller->cur_msg) {
597 chip = spi_get_ctldata(drv_data->controller->cur_msg->spi);
598 threshold = chip->threshold;
603 switch (drv_data->ssp_type) {
604 case QUARK_X1000_SSP:
605 mask |= QUARK_X1000_SSCR1_RFT;
608 mask |= CE4100_SSCR1_RFT;
615 pxa2xx_spi_update(drv_data, SSCR1, mask, threshold);
618 static void int_stop_and_reset(struct driver_data *drv_data)
620 /* Clear and disable interrupts */
621 write_SSSR_CS(drv_data, drv_data->clear_sr);
622 reset_sccr1(drv_data);
623 if (pxa25x_ssp_comp(drv_data))
626 pxa2xx_spi_write(drv_data, SSTO, 0);
629 static void int_error_stop(struct driver_data *drv_data, const char *msg, int err)
631 int_stop_and_reset(drv_data);
632 pxa2xx_spi_flush(drv_data);
633 pxa2xx_spi_off(drv_data);
635 dev_err(drv_data->ssp->dev, "%s\n", msg);
637 drv_data->controller->cur_msg->status = err;
638 spi_finalize_current_transfer(drv_data->controller);
641 static void int_transfer_complete(struct driver_data *drv_data)
643 int_stop_and_reset(drv_data);
645 spi_finalize_current_transfer(drv_data->controller);
648 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
652 irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr);
653 if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE))
654 irq_status &= ~SSSR_TFS;
656 if (irq_status & SSSR_ROR) {
657 int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO);
661 if (irq_status & SSSR_TUR) {
662 int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO);
666 if (irq_status & SSSR_TINT) {
667 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
668 if (drv_data->read(drv_data)) {
669 int_transfer_complete(drv_data);
674 /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */
676 if (drv_data->read(drv_data)) {
677 int_transfer_complete(drv_data);
680 } while (drv_data->write(drv_data));
682 if (drv_data->read(drv_data)) {
683 int_transfer_complete(drv_data);
687 if (drv_data->tx == drv_data->tx_end) {
691 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
692 sccr1_reg &= ~SSCR1_TIE;
695 * PXA25x_SSP has no timeout, set up Rx threshold for
696 * the remaining Rx bytes.
698 if (pxa25x_ssp_comp(drv_data)) {
701 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
703 bytes_left = drv_data->rx_end - drv_data->rx;
704 switch (drv_data->n_bytes) {
713 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
714 if (rx_thre > bytes_left)
715 rx_thre = bytes_left;
717 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
719 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
722 /* We did something */
726 static void handle_bad_msg(struct driver_data *drv_data)
728 int_stop_and_reset(drv_data);
729 pxa2xx_spi_off(drv_data);
731 dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n");
734 static irqreturn_t ssp_int(int irq, void *dev_id)
736 struct driver_data *drv_data = dev_id;
738 u32 mask = drv_data->mask_sr;
742 * The IRQ might be shared with other peripherals so we must first
743 * check that are we RPM suspended or not. If we are we assume that
744 * the IRQ was not for us (we shouldn't be RPM suspended when the
745 * interrupt is enabled).
747 if (pm_runtime_suspended(drv_data->ssp->dev))
751 * If the device is not yet in RPM suspended state and we get an
752 * interrupt that is meant for another device, check if status bits
753 * are all set to one. That means that the device is already
756 status = pxa2xx_spi_read(drv_data, SSSR);
760 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
762 /* Ignore possible writes if we don't need to write */
763 if (!(sccr1_reg & SSCR1_TIE))
766 /* Ignore RX timeout interrupt if it is disabled */
767 if (!(sccr1_reg & SSCR1_TINTE))
770 if (!(status & mask))
773 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
774 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
776 if (!drv_data->controller->cur_msg) {
777 handle_bad_msg(drv_data);
782 return drv_data->transfer_handler(drv_data);
786 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
787 * input frequency by fractions of 2^24. It also has a divider by 5.
789 * There are formulas to get baud rate value for given input frequency and
790 * divider parameters, such as DDS_CLK_RATE and SCR:
794 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
795 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
797 * DDS_CLK_RATE either 2^n or 2^n / 5.
798 * SCR is in range 0 .. 255
800 * Divisor = 5^i * 2^j * 2 * k
801 * i = [0, 1] i = 1 iff j = 0 or j > 3
802 * j = [0, 23] j = 0 iff i = 1
804 * Special case: j = 0, i = 1: Divisor = 2 / 5
806 * Accordingly to the specification the recommended values for DDS_CLK_RATE
808 * Case 1: 2^n, n = [0, 23]
809 * Case 2: 2^24 * 2 / 5 (0x666666)
810 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
812 * In all cases the lowest possible value is better.
814 * The function calculates parameters for all cases and chooses the one closest
815 * to the asked baud rate.
817 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
819 unsigned long xtal = 200000000;
820 unsigned long fref = xtal / 2; /* mandatory division by 2,
823 unsigned long fref1 = fref / 2; /* case 1 */
824 unsigned long fref2 = fref * 2 / 5; /* case 2 */
826 unsigned long q, q1, q2;
832 /* Set initial value for DDS_CLK_RATE */
833 mul = (1 << 24) >> 1;
835 /* Calculate initial quot */
836 q1 = DIV_ROUND_UP(fref1, rate);
838 /* Scale q1 if it's too big */
840 /* Scale q1 to range [1, 512] */
841 scale = fls_long(q1 - 1);
847 /* Round the result if we have a remainder */
851 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
856 /* Get the remainder */
857 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
861 q2 = DIV_ROUND_UP(fref2, rate);
862 r2 = abs(fref2 / q2 - rate);
865 * Choose the best between two: less remainder we have the better. We
866 * can't go case 2 if q2 is greater than 256 since SCR register can
867 * hold only values 0 .. 255.
869 if (r2 >= r1 || q2 > 256) {
870 /* case 1 is better */
874 /* case 2 is better */
877 mul = (1 << 24) * 2 / 5;
880 /* Check case 3 only if the divisor is big enough */
881 if (fref / rate >= 80) {
885 /* Calculate initial quot */
886 q1 = DIV_ROUND_UP(fref, rate);
889 /* Get the remainder */
890 fssp = (u64)fref * m;
891 do_div(fssp, 1 << 24);
892 r1 = abs(fssp - rate);
894 /* Choose this one if it suits better */
896 /* case 3 is better */
906 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
908 unsigned long ssp_clk = drv_data->controller->max_speed_hz;
909 const struct ssp_device *ssp = drv_data->ssp;
911 rate = min_t(int, ssp_clk, rate);
914 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
915 * that the SSP transmission rate can be greater than the device rate.
917 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
918 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
920 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
923 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
926 struct chip_data *chip =
927 spi_get_ctldata(drv_data->controller->cur_msg->spi);
928 unsigned int clk_div;
930 switch (drv_data->ssp_type) {
931 case QUARK_X1000_SSP:
932 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
935 clk_div = ssp_get_clk_div(drv_data, rate);
941 static bool pxa2xx_spi_can_dma(struct spi_controller *controller,
942 struct spi_device *spi,
943 struct spi_transfer *xfer)
945 struct driver_data *drv_data = spi_controller_get_devdata(controller);
947 return drv_data->controller_info->enable_dma &&
948 xfer->len <= MAX_DMA_LEN &&
949 xfer->len >= drv_data->controller_info->dma_burst_size;
952 static int pxa2xx_spi_transfer_one(struct spi_controller *controller,
953 struct spi_device *spi,
954 struct spi_transfer *transfer)
956 struct driver_data *drv_data = spi_controller_get_devdata(controller);
957 struct chip_data *chip = spi_get_ctldata(spi);
958 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
968 /* Check if we can DMA this transfer */
969 if (transfer->len > MAX_DMA_LEN && drv_data->controller_info->enable_dma) {
970 /* Warn ... we force this to PIO mode */
971 dev_warn_ratelimited(&spi->dev,
972 "DMA disabled for transfer length %u greater than %d\n",
973 transfer->len, MAX_DMA_LEN);
976 /* Setup the transfer state based on the type of transfer */
977 if (pxa2xx_spi_flush(drv_data) == 0) {
978 dev_err(&spi->dev, "Flush failed\n");
981 drv_data->tx = (void *)transfer->tx_buf;
982 drv_data->tx_end = drv_data->tx + transfer->len;
983 drv_data->rx = transfer->rx_buf;
984 drv_data->rx_end = drv_data->rx + transfer->len;
986 /* Change speed and bit per word on a per transfer */
987 bits = transfer->bits_per_word;
988 speed = transfer->speed_hz;
990 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
993 drv_data->n_bytes = 1;
994 drv_data->read = drv_data->rx ? u8_reader : null_reader;
995 drv_data->write = drv_data->tx ? u8_writer : null_writer;
996 } else if (bits <= 16) {
997 drv_data->n_bytes = 2;
998 drv_data->read = drv_data->rx ? u16_reader : null_reader;
999 drv_data->write = drv_data->tx ? u16_writer : null_writer;
1000 } else if (bits <= 32) {
1001 drv_data->n_bytes = 4;
1002 drv_data->read = drv_data->rx ? u32_reader : null_reader;
1003 drv_data->write = drv_data->tx ? u32_writer : null_writer;
1006 dma_thresh = SSCR1_RxTresh(RX_THRESH_DFLT) | SSCR1_TxTresh(TX_THRESH_DFLT);
1007 dma_mapped = controller->can_dma &&
1008 controller->can_dma(controller, spi, transfer) &&
1009 controller->cur_msg_mapped;
1012 /* Ensure we have the correct interrupt handler */
1013 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1015 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1019 /* Clear status and start DMA engine */
1020 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1021 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1023 pxa2xx_spi_dma_start(drv_data);
1025 /* Ensure we have the correct interrupt handler */
1026 drv_data->transfer_handler = interrupt_transfer;
1029 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1030 write_SSSR_CS(drv_data, drv_data->clear_sr);
1033 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1034 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1035 if (!pxa25x_ssp_comp(drv_data))
1036 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1037 controller->max_speed_hz
1038 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1039 dma_mapped ? "DMA" : "PIO");
1041 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1042 controller->max_speed_hz / 2
1043 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1044 dma_mapped ? "DMA" : "PIO");
1046 if (is_lpss_ssp(drv_data)) {
1047 pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold);
1048 pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold);
1051 if (is_mrfld_ssp(drv_data)) {
1052 u32 mask = SFIFOTT_RFT | SFIFOTT_TFT;
1055 thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold);
1056 thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold);
1058 pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh);
1061 if (is_quark_x1000_ssp(drv_data))
1062 pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate);
1065 if (!is_mmp2_ssp(drv_data))
1066 pxa_ssp_disable(drv_data->ssp);
1068 if (!pxa25x_ssp_comp(drv_data))
1069 pxa2xx_spi_write(drv_data, SSTO, TIMOUT_DFLT);
1071 /* First set CR1 without interrupt and service enables */
1072 pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1);
1074 /* See if we need to reload the configuration registers */
1075 pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0);
1077 /* Restart the SSP */
1078 pxa_ssp_enable(drv_data->ssp);
1080 if (is_mmp2_ssp(drv_data)) {
1081 u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8;
1084 /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */
1085 dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level);
1086 if (tx_level > transfer->len)
1087 tx_level = transfer->len;
1088 drv_data->tx += tx_level;
1092 if (spi_controller_is_target(controller)) {
1093 while (drv_data->write(drv_data))
1095 if (drv_data->gpiod_ready) {
1096 gpiod_set_value(drv_data->gpiod_ready, 1);
1098 gpiod_set_value(drv_data->gpiod_ready, 0);
1103 * Release the data by enabling service requests and interrupts,
1104 * without changing any mode bits.
1106 pxa2xx_spi_write(drv_data, SSCR1, cr1);
1111 static int pxa2xx_spi_target_abort(struct spi_controller *controller)
1113 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1115 int_error_stop(drv_data, "transfer aborted", -EINTR);
1120 static void pxa2xx_spi_handle_err(struct spi_controller *controller,
1121 struct spi_message *msg)
1123 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1125 int_stop_and_reset(drv_data);
1127 /* Disable the SSP */
1128 pxa2xx_spi_off(drv_data);
1131 * Stop the DMA if running. Note DMA callback handler may have unset
1132 * the dma_running already, which is fine as stopping is not needed
1133 * then but we shouldn't rely this flag for anything else than
1134 * stopping. For instance to differentiate between PIO and DMA
1137 if (atomic_read(&drv_data->dma_running))
1138 pxa2xx_spi_dma_stop(drv_data);
1141 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller)
1143 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1145 /* Disable the SSP now */
1146 pxa2xx_spi_off(drv_data);
1151 static int setup(struct spi_device *spi)
1153 struct chip_data *chip;
1154 const struct lpss_config *config;
1155 struct driver_data *drv_data =
1156 spi_controller_get_devdata(spi->controller);
1157 uint tx_thres, tx_hi_thres, rx_thres;
1159 switch (drv_data->ssp_type) {
1160 case QUARK_X1000_SSP:
1161 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1163 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1166 tx_thres = TX_THRESH_MRFLD_DFLT;
1168 rx_thres = RX_THRESH_MRFLD_DFLT;
1171 tx_thres = TX_THRESH_CE4100_DFLT;
1173 rx_thres = RX_THRESH_CE4100_DFLT;
1181 config = lpss_get_config(drv_data);
1182 tx_thres = config->tx_threshold_lo;
1183 tx_hi_thres = config->tx_threshold_hi;
1184 rx_thres = config->rx_threshold;
1188 if (spi_controller_is_target(drv_data->controller)) {
1192 tx_thres = TX_THRESH_DFLT;
1193 rx_thres = RX_THRESH_DFLT;
1198 if (drv_data->ssp_type == CE4100_SSP) {
1199 if (spi_get_chipselect(spi, 0) > 4) {
1200 dev_err(&spi->dev, "failed setup: cs number must not be > 4.\n");
1205 /* Only allocate on the first setup */
1206 chip = spi_get_ctldata(spi);
1208 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1214 if (spi_controller_is_target(drv_data->controller)) {
1215 chip->cr1 |= SSCR1_SCFR;
1216 chip->cr1 |= SSCR1_SCLKDIR;
1217 chip->cr1 |= SSCR1_SFRMDIR;
1218 chip->cr1 |= SSCR1_SPH;
1221 if (is_lpss_ssp(drv_data)) {
1222 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1223 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
1224 SSITF_TxHiThresh(tx_hi_thres);
1227 if (is_mrfld_ssp(drv_data)) {
1228 chip->lpss_rx_threshold = rx_thres;
1229 chip->lpss_tx_threshold = tx_thres;
1232 switch (drv_data->ssp_type) {
1233 case QUARK_X1000_SSP:
1234 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1235 & QUARK_X1000_SSCR1_RFT)
1236 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1237 & QUARK_X1000_SSCR1_TFT);
1240 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1241 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1244 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1245 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1249 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1250 chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) |
1251 ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0);
1253 if (spi->mode & SPI_LOOP)
1254 chip->cr1 |= SSCR1_LBM;
1256 spi_set_ctldata(spi, chip);
1261 static void cleanup(struct spi_device *spi)
1263 struct chip_data *chip = spi_get_ctldata(spi);
1268 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1270 return param == chan->device->dev;
1274 pxa2xx_spi_init_ssp(struct platform_device *pdev, struct ssp_device *ssp, enum pxa_ssp_type type)
1276 struct device *dev = &pdev->dev;
1277 struct resource *res;
1281 ssp->mmio_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1282 if (IS_ERR(ssp->mmio_base))
1283 return PTR_ERR(ssp->mmio_base);
1285 ssp->phys_base = res->start;
1287 ssp->clk = devm_clk_get(dev, NULL);
1288 if (IS_ERR(ssp->clk))
1289 return PTR_ERR(ssp->clk);
1291 ssp->irq = platform_get_irq(pdev, 0);
1298 status = acpi_dev_uid_to_integer(ACPI_COMPANION(dev), &uid);
1307 static struct pxa2xx_spi_controller *
1308 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1310 struct pxa2xx_spi_controller *pdata;
1311 struct device *dev = &pdev->dev;
1312 struct device *parent = dev->parent;
1313 enum pxa_ssp_type type = SSP_UNDEFINED;
1314 struct ssp_device *ssp = NULL;
1320 is_lpss_priv = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpss_priv");
1322 match = device_get_match_data(dev);
1324 type = (uintptr_t)match;
1325 else if (is_lpss_priv) {
1328 status = device_property_read_u32(dev, "intel,spi-pxa2xx-type", &value);
1330 return ERR_PTR(status);
1332 type = (enum pxa_ssp_type)value;
1334 ssp = pxa_ssp_request(pdev->id, pdev->name);
1341 /* Validate the SSP type correctness */
1342 if (!(type > SSP_UNDEFINED && type < SSP_MAX))
1343 return ERR_PTR(-EINVAL);
1345 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1347 return ERR_PTR(-ENOMEM);
1349 /* Platforms with iDMA 64-bit */
1351 pdata->tx_param = parent;
1352 pdata->rx_param = parent;
1353 pdata->dma_filter = pxa2xx_spi_idma_filter;
1356 /* Read number of chip select pins, if provided */
1357 device_property_read_u32(dev, "num-cs", &num_cs);
1359 pdata->num_chipselect = num_cs;
1360 pdata->is_target = device_property_read_bool(dev, "spi-slave");
1361 pdata->enable_dma = true;
1362 pdata->dma_burst_size = 1;
1364 /* If SSP has been already enumerated, use it */
1368 status = pxa2xx_spi_init_ssp(pdev, &pdata->ssp, type);
1370 return ERR_PTR(status);
1375 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller,
1378 struct driver_data *drv_data = spi_controller_get_devdata(controller);
1380 if (has_acpi_companion(drv_data->ssp->dev)) {
1381 switch (drv_data->ssp_type) {
1383 * For Atoms the ACPI DeviceSelection used by the Windows
1384 * driver starts from 1 instead of 0 so translate it here
1385 * to match what Linux expects.
1399 static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi)
1404 static int pxa2xx_spi_probe(struct platform_device *pdev)
1406 struct device *dev = &pdev->dev;
1407 struct pxa2xx_spi_controller *platform_info;
1408 struct spi_controller *controller;
1409 struct driver_data *drv_data;
1410 struct ssp_device *ssp;
1411 const struct lpss_config *config;
1415 platform_info = dev_get_platdata(dev);
1416 if (!platform_info) {
1417 platform_info = pxa2xx_spi_init_pdata(pdev);
1418 if (IS_ERR(platform_info))
1419 return dev_err_probe(dev, PTR_ERR(platform_info), "missing platform data\n");
1421 dev_dbg(dev, "DMA burst size set to %u\n", platform_info->dma_burst_size);
1423 ssp = pxa_ssp_request(pdev->id, pdev->name);
1425 ssp = &platform_info->ssp;
1427 if (!ssp->mmio_base)
1428 return dev_err_probe(dev, -ENODEV, "failed to get SSP\n");
1430 if (platform_info->is_target)
1431 controller = devm_spi_alloc_target(dev, sizeof(*drv_data));
1433 controller = devm_spi_alloc_host(dev, sizeof(*drv_data));
1436 status = dev_err_probe(dev, -ENOMEM, "cannot alloc spi_controller\n");
1437 goto out_error_controller_alloc;
1439 drv_data = spi_controller_get_devdata(controller);
1440 drv_data->controller = controller;
1441 drv_data->controller_info = platform_info;
1442 drv_data->ssp = ssp;
1444 device_set_node(&controller->dev, dev_fwnode(dev));
1446 /* The spi->mode bits understood by this driver: */
1447 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1449 controller->bus_num = ssp->port_id;
1450 controller->dma_alignment = DMA_ALIGNMENT;
1451 controller->cleanup = cleanup;
1452 controller->setup = setup;
1453 controller->set_cs = pxa2xx_spi_set_cs;
1454 controller->transfer_one = pxa2xx_spi_transfer_one;
1455 controller->target_abort = pxa2xx_spi_target_abort;
1456 controller->handle_err = pxa2xx_spi_handle_err;
1457 controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1458 controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1459 controller->auto_runtime_pm = true;
1460 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1462 drv_data->ssp_type = ssp->type;
1464 if (pxa25x_ssp_comp(drv_data)) {
1465 switch (drv_data->ssp_type) {
1466 case QUARK_X1000_SSP:
1467 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1470 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1474 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1475 drv_data->dma_cr1 = 0;
1476 drv_data->clear_sr = SSSR_ROR;
1477 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1479 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1480 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1481 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1482 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1483 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
1484 | SSSR_ROR | SSSR_TUR;
1487 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1490 dev_err_probe(dev, status, "cannot get IRQ %d\n", ssp->irq);
1491 goto out_error_controller_alloc;
1494 /* Setup DMA if requested */
1495 if (platform_info->enable_dma) {
1496 status = pxa2xx_spi_dma_setup(drv_data);
1498 dev_warn(dev, "no DMA channels available, using PIO\n");
1499 platform_info->enable_dma = false;
1501 controller->can_dma = pxa2xx_spi_can_dma;
1502 controller->max_dma_len = MAX_DMA_LEN;
1503 controller->max_transfer_size =
1504 pxa2xx_spi_max_dma_transfer_size;
1508 /* Enable SOC clock */
1509 status = clk_prepare_enable(ssp->clk);
1511 goto out_error_dma_irq_alloc;
1513 controller->max_speed_hz = clk_get_rate(ssp->clk);
1515 * Set minimum speed for all other platforms than Intel Quark which is
1516 * able do under 1 Hz transfers.
1518 if (!pxa25x_ssp_comp(drv_data))
1519 controller->min_speed_hz =
1520 DIV_ROUND_UP(controller->max_speed_hz, 4096);
1521 else if (!is_quark_x1000_ssp(drv_data))
1522 controller->min_speed_hz =
1523 DIV_ROUND_UP(controller->max_speed_hz, 512);
1525 pxa_ssp_disable(ssp);
1527 /* Load default SSP configuration */
1528 switch (drv_data->ssp_type) {
1529 case QUARK_X1000_SSP:
1530 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1531 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1532 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1534 /* Using the Motorola SPI protocol and use 8 bit frame */
1535 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1536 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1539 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1540 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1541 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1542 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1543 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1547 if (spi_controller_is_target(controller)) {
1555 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1556 SSCR1_TxTresh(TX_THRESH_DFLT);
1558 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1559 tmp = SSCR0_Motorola | SSCR0_DataSize(8);
1560 if (!spi_controller_is_target(controller))
1561 tmp |= SSCR0_SCR(2);
1562 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1566 if (!pxa25x_ssp_comp(drv_data))
1567 pxa2xx_spi_write(drv_data, SSTO, 0);
1569 if (!is_quark_x1000_ssp(drv_data))
1570 pxa2xx_spi_write(drv_data, SSPSP, 0);
1572 if (is_lpss_ssp(drv_data)) {
1573 lpss_ssp_setup(drv_data);
1574 config = lpss_get_config(drv_data);
1575 if (config->reg_capabilities >= 0) {
1576 tmp = __lpss_ssp_read_priv(drv_data,
1577 config->reg_capabilities);
1578 tmp &= LPSS_CAPS_CS_EN_MASK;
1579 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1580 platform_info->num_chipselect = ffz(tmp);
1581 } else if (config->cs_num) {
1582 platform_info->num_chipselect = config->cs_num;
1585 controller->num_chipselect = platform_info->num_chipselect;
1586 controller->use_gpio_descriptors = true;
1588 if (platform_info->is_target) {
1589 drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
1590 "ready", GPIOD_OUT_LOW);
1591 if (IS_ERR(drv_data->gpiod_ready)) {
1592 status = PTR_ERR(drv_data->gpiod_ready);
1593 goto out_error_clock_enabled;
1597 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1598 pm_runtime_use_autosuspend(&pdev->dev);
1599 pm_runtime_set_active(&pdev->dev);
1600 pm_runtime_enable(&pdev->dev);
1602 /* Register with the SPI framework */
1603 platform_set_drvdata(pdev, drv_data);
1604 status = spi_register_controller(controller);
1606 dev_err_probe(dev, status, "problem registering SPI controller\n");
1607 goto out_error_pm_runtime_enabled;
1612 out_error_pm_runtime_enabled:
1613 pm_runtime_disable(&pdev->dev);
1615 out_error_clock_enabled:
1616 clk_disable_unprepare(ssp->clk);
1618 out_error_dma_irq_alloc:
1619 pxa2xx_spi_dma_release(drv_data);
1620 free_irq(ssp->irq, drv_data);
1622 out_error_controller_alloc:
1627 static void pxa2xx_spi_remove(struct platform_device *pdev)
1629 struct driver_data *drv_data = platform_get_drvdata(pdev);
1630 struct ssp_device *ssp = drv_data->ssp;
1632 pm_runtime_get_sync(&pdev->dev);
1634 spi_unregister_controller(drv_data->controller);
1636 /* Disable the SSP at the peripheral and SOC level */
1637 pxa_ssp_disable(ssp);
1638 clk_disable_unprepare(ssp->clk);
1641 if (drv_data->controller_info->enable_dma)
1642 pxa2xx_spi_dma_release(drv_data);
1644 pm_runtime_put_noidle(&pdev->dev);
1645 pm_runtime_disable(&pdev->dev);
1648 free_irq(ssp->irq, drv_data);
1654 static int pxa2xx_spi_suspend(struct device *dev)
1656 struct driver_data *drv_data = dev_get_drvdata(dev);
1657 struct ssp_device *ssp = drv_data->ssp;
1660 status = spi_controller_suspend(drv_data->controller);
1664 pxa_ssp_disable(ssp);
1666 if (!pm_runtime_suspended(dev))
1667 clk_disable_unprepare(ssp->clk);
1672 static int pxa2xx_spi_resume(struct device *dev)
1674 struct driver_data *drv_data = dev_get_drvdata(dev);
1675 struct ssp_device *ssp = drv_data->ssp;
1678 /* Enable the SSP clock */
1679 if (!pm_runtime_suspended(dev)) {
1680 status = clk_prepare_enable(ssp->clk);
1685 /* Start the queue running */
1686 return spi_controller_resume(drv_data->controller);
1689 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1691 struct driver_data *drv_data = dev_get_drvdata(dev);
1693 clk_disable_unprepare(drv_data->ssp->clk);
1697 static int pxa2xx_spi_runtime_resume(struct device *dev)
1699 struct driver_data *drv_data = dev_get_drvdata(dev);
1701 return clk_prepare_enable(drv_data->ssp->clk);
1704 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1705 SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1706 RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, pxa2xx_spi_runtime_resume, NULL)
1709 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1710 { "80860F0E", LPSS_BYT_SSP },
1711 { "8086228E", LPSS_BSW_SSP },
1712 { "INT33C0", LPSS_LPT_SSP },
1713 { "INT33C1", LPSS_LPT_SSP },
1714 { "INT3430", LPSS_LPT_SSP },
1715 { "INT3431", LPSS_LPT_SSP },
1718 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1720 static const struct of_device_id pxa2xx_spi_of_match[] = {
1721 { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP },
1724 MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match);
1726 static struct platform_driver driver = {
1728 .name = "pxa2xx-spi",
1729 .pm = pm_ptr(&pxa2xx_spi_pm_ops),
1730 .acpi_match_table = pxa2xx_spi_acpi_match,
1731 .of_match_table = pxa2xx_spi_of_match,
1733 .probe = pxa2xx_spi_probe,
1734 .remove_new = pxa2xx_spi_remove,
1737 static int __init pxa2xx_spi_init(void)
1739 return platform_driver_register(&driver);
1741 subsys_initcall(pxa2xx_spi_init);
1743 static void __exit pxa2xx_spi_exit(void)
1745 platform_driver_unregister(&driver);
1747 module_exit(pxa2xx_spi_exit);
1749 MODULE_SOFTDEP("pre: dw_dmac");