2 * OMAP2 McSPI controller driver
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
6 * Juha Yrj�l� <juha.yrjola@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/dmaengine.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/platform_device.h>
28 #include <linux/err.h>
29 #include <linux/clk.h>
31 #include <linux/slab.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/of_device.h>
35 #include <linux/gcd.h>
37 #include <linux/spi/spi.h>
38 #include <linux/gpio.h>
40 #include <linux/platform_data/spi-omap2-mcspi.h>
42 #define OMAP2_MCSPI_MAX_FREQ 48000000
43 #define OMAP2_MCSPI_MAX_DIVIDER 4096
44 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
45 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
46 #define SPI_AUTOSUSPEND_TIMEOUT 2000
48 #define OMAP2_MCSPI_REVISION 0x00
49 #define OMAP2_MCSPI_SYSSTATUS 0x14
50 #define OMAP2_MCSPI_IRQSTATUS 0x18
51 #define OMAP2_MCSPI_IRQENABLE 0x1c
52 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
53 #define OMAP2_MCSPI_SYST 0x24
54 #define OMAP2_MCSPI_MODULCTRL 0x28
55 #define OMAP2_MCSPI_XFERLEVEL 0x7c
57 /* per-channel banks, 0x14 bytes each, first is: */
58 #define OMAP2_MCSPI_CHCONF0 0x2c
59 #define OMAP2_MCSPI_CHSTAT0 0x30
60 #define OMAP2_MCSPI_CHCTRL0 0x34
61 #define OMAP2_MCSPI_TX0 0x38
62 #define OMAP2_MCSPI_RX0 0x3c
64 /* per-register bitmasks: */
65 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
67 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
68 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
69 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
71 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
72 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
73 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
74 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
75 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
76 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
77 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
78 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
79 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
80 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
81 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
82 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
83 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
84 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
85 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
86 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
87 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
88 #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
90 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
91 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
92 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
93 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
95 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
96 #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
98 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
100 /* We have 2 DMA channels per CS, one for RX and one for TX */
101 struct omap2_mcspi_dma
{
102 struct dma_chan
*dma_tx
;
103 struct dma_chan
*dma_rx
;
105 struct completion dma_tx_completion
;
106 struct completion dma_rx_completion
;
108 char dma_rx_ch_name
[14];
109 char dma_tx_ch_name
[14];
112 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
113 * cache operations; better heuristics consider wordsize and bitrate.
115 #define DMA_MIN_BYTES 160
119 * Used for context save and restore, structure members to be updated whenever
120 * corresponding registers are modified.
122 struct omap2_mcspi_regs
{
129 struct spi_master
*master
;
130 /* Virtual base address of the controller */
133 /* SPI1 has 4 channels, while SPI2 has 2 */
134 struct omap2_mcspi_dma
*dma_channels
;
136 struct omap2_mcspi_regs ctx
;
138 unsigned int pin_dir
:1;
141 struct omap2_mcspi_cs
{
146 struct list_head node
;
147 /* Context save and restore shadow register */
148 u32 chconf0
, chctrl0
;
151 static inline void mcspi_write_reg(struct spi_master
*master
,
154 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
156 writel_relaxed(val
, mcspi
->base
+ idx
);
159 static inline u32
mcspi_read_reg(struct spi_master
*master
, int idx
)
161 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
163 return readl_relaxed(mcspi
->base
+ idx
);
166 static inline void mcspi_write_cs_reg(const struct spi_device
*spi
,
169 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
171 writel_relaxed(val
, cs
->base
+ idx
);
174 static inline u32
mcspi_read_cs_reg(const struct spi_device
*spi
, int idx
)
176 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
178 return readl_relaxed(cs
->base
+ idx
);
181 static inline u32
mcspi_cached_chconf0(const struct spi_device
*spi
)
183 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
188 static inline void mcspi_write_chconf0(const struct spi_device
*spi
, u32 val
)
190 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
193 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, val
);
194 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
197 static inline int mcspi_bytes_per_word(int word_len
)
201 else if (word_len
<= 16)
203 else /* word_len <= 32 */
207 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
208 int is_read
, int enable
)
212 l
= mcspi_cached_chconf0(spi
);
214 if (is_read
) /* 1 is read, 0 write */
215 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
217 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
224 mcspi_write_chconf0(spi
, l
);
227 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
229 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
234 l
|= OMAP2_MCSPI_CHCTRL_EN
;
236 l
&= ~OMAP2_MCSPI_CHCTRL_EN
;
238 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
239 /* Flash post-writes */
240 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
243 static void omap2_mcspi_set_cs(struct spi_device
*spi
, bool enable
)
245 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
248 /* The controller handles the inverted chip selects
249 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
250 * the inversion from the core spi_set_cs function.
252 if (spi
->mode
& SPI_CS_HIGH
)
255 if (spi
->controller_state
) {
256 int err
= pm_runtime_get_sync(mcspi
->dev
);
258 pm_runtime_put_noidle(mcspi
->dev
);
259 dev_err(mcspi
->dev
, "failed to get sync: %d\n", err
);
263 l
= mcspi_cached_chconf0(spi
);
266 l
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
268 l
|= OMAP2_MCSPI_CHCONF_FORCE
;
270 mcspi_write_chconf0(spi
, l
);
272 pm_runtime_mark_last_busy(mcspi
->dev
);
273 pm_runtime_put_autosuspend(mcspi
->dev
);
277 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
279 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
280 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
284 * Setup when switching from (reset default) slave mode
285 * to single-channel master mode
287 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
288 l
&= ~(OMAP2_MCSPI_MODULCTRL_STEST
| OMAP2_MCSPI_MODULCTRL_MS
);
289 l
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
290 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
295 static void omap2_mcspi_set_fifo(const struct spi_device
*spi
,
296 struct spi_transfer
*t
, int enable
)
298 struct spi_master
*master
= spi
->master
;
299 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
300 struct omap2_mcspi
*mcspi
;
302 int max_fifo_depth
, fifo_depth
, bytes_per_word
;
303 u32 chconf
, xferlevel
;
305 mcspi
= spi_master_get_devdata(master
);
307 chconf
= mcspi_cached_chconf0(spi
);
309 bytes_per_word
= mcspi_bytes_per_word(cs
->word_len
);
310 if (t
->len
% bytes_per_word
!= 0)
313 if (t
->rx_buf
!= NULL
&& t
->tx_buf
!= NULL
)
314 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
/ 2;
316 max_fifo_depth
= OMAP2_MCSPI_MAX_FIFODEPTH
;
318 fifo_depth
= gcd(t
->len
, max_fifo_depth
);
319 if (fifo_depth
< 2 || fifo_depth
% bytes_per_word
!= 0)
322 wcnt
= t
->len
/ bytes_per_word
;
323 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
326 xferlevel
= wcnt
<< 16;
327 if (t
->rx_buf
!= NULL
) {
328 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
329 xferlevel
|= (fifo_depth
- 1) << 8;
331 if (t
->tx_buf
!= NULL
) {
332 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
333 xferlevel
|= fifo_depth
- 1;
336 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
337 mcspi_write_chconf0(spi
, chconf
);
338 mcspi
->fifo_depth
= fifo_depth
;
344 if (t
->rx_buf
!= NULL
)
345 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
347 if (t
->tx_buf
!= NULL
)
348 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
350 mcspi_write_chconf0(spi
, chconf
);
351 mcspi
->fifo_depth
= 0;
354 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
356 unsigned long timeout
;
358 timeout
= jiffies
+ msecs_to_jiffies(1000);
359 while (!(readl_relaxed(reg
) & bit
)) {
360 if (time_after(jiffies
, timeout
)) {
361 if (!(readl_relaxed(reg
) & bit
))
371 static void omap2_mcspi_rx_callback(void *data
)
373 struct spi_device
*spi
= data
;
374 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
375 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
377 /* We must disable the DMA RX request */
378 omap2_mcspi_set_dma_req(spi
, 1, 0);
380 complete(&mcspi_dma
->dma_rx_completion
);
383 static void omap2_mcspi_tx_callback(void *data
)
385 struct spi_device
*spi
= data
;
386 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
387 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
389 /* We must disable the DMA TX request */
390 omap2_mcspi_set_dma_req(spi
, 0, 0);
392 complete(&mcspi_dma
->dma_tx_completion
);
395 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
396 struct spi_transfer
*xfer
,
397 struct dma_slave_config cfg
)
399 struct omap2_mcspi
*mcspi
;
400 struct omap2_mcspi_dma
*mcspi_dma
;
402 mcspi
= spi_master_get_devdata(spi
->master
);
403 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
405 if (mcspi_dma
->dma_tx
) {
406 struct dma_async_tx_descriptor
*tx
;
408 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
410 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, xfer
->tx_sg
.sgl
,
413 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
415 tx
->callback
= omap2_mcspi_tx_callback
;
416 tx
->callback_param
= spi
;
417 dmaengine_submit(tx
);
419 /* FIXME: fall back to PIO? */
422 dma_async_issue_pending(mcspi_dma
->dma_tx
);
423 omap2_mcspi_set_dma_req(spi
, 0, 1);
428 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
429 struct dma_slave_config cfg
,
432 struct omap2_mcspi
*mcspi
;
433 struct omap2_mcspi_dma
*mcspi_dma
;
434 unsigned int count
, transfer_reduction
= 0;
435 struct scatterlist
*sg_out
[2];
436 int nb_sizes
= 0, out_mapped_nents
[2], ret
, x
;
440 int word_len
, element_count
;
441 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
442 void __iomem
*chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
444 mcspi
= spi_master_get_devdata(spi
->master
);
445 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
449 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
450 * it mentions reducing DMA transfer length by one element in master
453 if (mcspi
->fifo_depth
== 0)
454 transfer_reduction
= es
;
456 word_len
= cs
->word_len
;
457 l
= mcspi_cached_chconf0(spi
);
460 element_count
= count
;
461 else if (word_len
<= 16)
462 element_count
= count
>> 1;
463 else /* word_len <= 32 */
464 element_count
= count
>> 2;
466 if (mcspi_dma
->dma_rx
) {
467 struct dma_async_tx_descriptor
*tx
;
469 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
472 * Reduce DMA transfer length by one more if McSPI is
473 * configured in turbo mode.
475 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
476 transfer_reduction
+= es
;
478 if (transfer_reduction
) {
479 /* Split sgl into two. The second sgl won't be used. */
480 sizes
[0] = count
- transfer_reduction
;
481 sizes
[1] = transfer_reduction
;
485 * Don't bother splitting the sgl. This essentially
486 * clones the original sgl.
492 ret
= sg_split(xfer
->rx_sg
.sgl
, xfer
->rx_sg
.nents
,
495 sg_out
, out_mapped_nents
,
499 dev_err(&spi
->dev
, "sg_split failed\n");
503 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
,
507 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
509 tx
->callback
= omap2_mcspi_rx_callback
;
510 tx
->callback_param
= spi
;
511 dmaengine_submit(tx
);
513 /* FIXME: fall back to PIO? */
517 dma_async_issue_pending(mcspi_dma
->dma_rx
);
518 omap2_mcspi_set_dma_req(spi
, 1, 1);
520 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
522 for (x
= 0; x
< nb_sizes
; x
++)
525 if (mcspi
->fifo_depth
> 0)
529 * Due to the DMA transfer length reduction the missing bytes must
530 * be read manually to receive all of the expected data.
532 omap2_mcspi_set_enable(spi
, 0);
534 elements
= element_count
- 1;
536 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
539 if (!mcspi_wait_for_reg_bit(chstat_reg
,
540 OMAP2_MCSPI_CHSTAT_RXS
)) {
543 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
545 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
546 else if (word_len
<= 16)
547 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
548 else /* word_len <= 32 */
549 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
551 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
552 dev_err(&spi
->dev
, "DMA RX penultimate word empty\n");
553 count
-= (bytes_per_word
<< 1);
554 omap2_mcspi_set_enable(spi
, 1);
558 if (!mcspi_wait_for_reg_bit(chstat_reg
, OMAP2_MCSPI_CHSTAT_RXS
)) {
561 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
563 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
564 else if (word_len
<= 16)
565 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
566 else /* word_len <= 32 */
567 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
569 dev_err(&spi
->dev
, "DMA RX last word empty\n");
570 count
-= mcspi_bytes_per_word(word_len
);
572 omap2_mcspi_set_enable(spi
, 1);
577 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
579 struct omap2_mcspi
*mcspi
;
580 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
581 struct omap2_mcspi_dma
*mcspi_dma
;
585 struct dma_slave_config cfg
;
586 enum dma_slave_buswidth width
;
589 void __iomem
*chstat_reg
;
590 void __iomem
*irqstat_reg
;
593 mcspi
= spi_master_get_devdata(spi
->master
);
594 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
596 if (cs
->word_len
<= 8) {
597 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
599 } else if (cs
->word_len
<= 16) {
600 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
603 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
610 if (mcspi
->fifo_depth
> 0) {
611 if (count
> mcspi
->fifo_depth
)
612 burst
= mcspi
->fifo_depth
/ es
;
617 memset(&cfg
, 0, sizeof(cfg
));
618 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
619 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
620 cfg
.src_addr_width
= width
;
621 cfg
.dst_addr_width
= width
;
622 cfg
.src_maxburst
= burst
;
623 cfg
.dst_maxburst
= burst
;
629 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
632 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
635 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
637 if (mcspi
->fifo_depth
> 0) {
638 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
640 if (mcspi_wait_for_reg_bit(irqstat_reg
,
641 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
642 dev_err(&spi
->dev
, "EOW timed out\n");
644 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
645 OMAP2_MCSPI_IRQSTATUS_EOW
);
648 /* for TX_ONLY mode, be sure all words have shifted out */
650 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
651 if (mcspi
->fifo_depth
> 0) {
652 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
653 OMAP2_MCSPI_CHSTAT_TXFFE
);
655 dev_err(&spi
->dev
, "TXFFE timed out\n");
657 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
658 OMAP2_MCSPI_CHSTAT_TXS
);
660 dev_err(&spi
->dev
, "TXS timed out\n");
663 (mcspi_wait_for_reg_bit(chstat_reg
,
664 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
665 dev_err(&spi
->dev
, "EOT timed out\n");
672 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
674 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
675 unsigned int count
, c
;
677 void __iomem
*base
= cs
->base
;
678 void __iomem
*tx_reg
;
679 void __iomem
*rx_reg
;
680 void __iomem
*chstat_reg
;
685 word_len
= cs
->word_len
;
687 l
= mcspi_cached_chconf0(spi
);
689 /* We store the pre-calculated register addresses on stack to speed
690 * up the transfer loop. */
691 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
692 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
693 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
695 if (c
< (word_len
>>3))
708 if (mcspi_wait_for_reg_bit(chstat_reg
,
709 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
710 dev_err(&spi
->dev
, "TXS timed out\n");
713 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
715 writel_relaxed(*tx
++, tx_reg
);
718 if (mcspi_wait_for_reg_bit(chstat_reg
,
719 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
720 dev_err(&spi
->dev
, "RXS timed out\n");
724 if (c
== 1 && tx
== NULL
&&
725 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
726 omap2_mcspi_set_enable(spi
, 0);
727 *rx
++ = readl_relaxed(rx_reg
);
728 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
729 word_len
, *(rx
- 1));
730 if (mcspi_wait_for_reg_bit(chstat_reg
,
731 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
737 } else if (c
== 0 && tx
== NULL
) {
738 omap2_mcspi_set_enable(spi
, 0);
741 *rx
++ = readl_relaxed(rx_reg
);
742 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
743 word_len
, *(rx
- 1));
746 } else if (word_len
<= 16) {
755 if (mcspi_wait_for_reg_bit(chstat_reg
,
756 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
757 dev_err(&spi
->dev
, "TXS timed out\n");
760 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
762 writel_relaxed(*tx
++, tx_reg
);
765 if (mcspi_wait_for_reg_bit(chstat_reg
,
766 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
767 dev_err(&spi
->dev
, "RXS timed out\n");
771 if (c
== 2 && tx
== NULL
&&
772 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
773 omap2_mcspi_set_enable(spi
, 0);
774 *rx
++ = readl_relaxed(rx_reg
);
775 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
776 word_len
, *(rx
- 1));
777 if (mcspi_wait_for_reg_bit(chstat_reg
,
778 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
784 } else if (c
== 0 && tx
== NULL
) {
785 omap2_mcspi_set_enable(spi
, 0);
788 *rx
++ = readl_relaxed(rx_reg
);
789 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
790 word_len
, *(rx
- 1));
793 } else if (word_len
<= 32) {
802 if (mcspi_wait_for_reg_bit(chstat_reg
,
803 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
804 dev_err(&spi
->dev
, "TXS timed out\n");
807 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
809 writel_relaxed(*tx
++, tx_reg
);
812 if (mcspi_wait_for_reg_bit(chstat_reg
,
813 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
814 dev_err(&spi
->dev
, "RXS timed out\n");
818 if (c
== 4 && tx
== NULL
&&
819 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
820 omap2_mcspi_set_enable(spi
, 0);
821 *rx
++ = readl_relaxed(rx_reg
);
822 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
823 word_len
, *(rx
- 1));
824 if (mcspi_wait_for_reg_bit(chstat_reg
,
825 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
831 } else if (c
== 0 && tx
== NULL
) {
832 omap2_mcspi_set_enable(spi
, 0);
835 *rx
++ = readl_relaxed(rx_reg
);
836 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
837 word_len
, *(rx
- 1));
842 /* for TX_ONLY mode, be sure all words have shifted out */
843 if (xfer
->rx_buf
== NULL
) {
844 if (mcspi_wait_for_reg_bit(chstat_reg
,
845 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
846 dev_err(&spi
->dev
, "TXS timed out\n");
847 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
848 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
849 dev_err(&spi
->dev
, "EOT timed out\n");
851 /* disable chan to purge rx datas received in TX_ONLY transfer,
852 * otherwise these rx datas will affect the direct following
855 omap2_mcspi_set_enable(spi
, 0);
858 omap2_mcspi_set_enable(spi
, 1);
862 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
866 for (div
= 0; div
< 15; div
++)
867 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
873 /* called only when no transfer is active to this device */
874 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
875 struct spi_transfer
*t
)
877 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
878 struct omap2_mcspi
*mcspi
;
879 u32 l
= 0, clkd
= 0, div
, extclk
= 0, clkg
= 0;
880 u8 word_len
= spi
->bits_per_word
;
881 u32 speed_hz
= spi
->max_speed_hz
;
883 mcspi
= spi_master_get_devdata(spi
->master
);
885 if (t
!= NULL
&& t
->bits_per_word
)
886 word_len
= t
->bits_per_word
;
888 cs
->word_len
= word_len
;
890 if (t
&& t
->speed_hz
)
891 speed_hz
= t
->speed_hz
;
893 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
894 if (speed_hz
< (OMAP2_MCSPI_MAX_FREQ
/ OMAP2_MCSPI_MAX_DIVIDER
)) {
895 clkd
= omap2_mcspi_calc_divisor(speed_hz
);
896 speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> clkd
;
899 div
= (OMAP2_MCSPI_MAX_FREQ
+ speed_hz
- 1) / speed_hz
;
900 speed_hz
= OMAP2_MCSPI_MAX_FREQ
/ div
;
901 clkd
= (div
- 1) & 0xf;
902 extclk
= (div
- 1) >> 4;
903 clkg
= OMAP2_MCSPI_CHCONF_CLKG
;
906 l
= mcspi_cached_chconf0(spi
);
908 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
909 * REVISIT: this controller could support SPI_3WIRE mode.
911 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
912 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
913 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
914 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
916 l
|= OMAP2_MCSPI_CHCONF_IS
;
917 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
918 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
922 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
923 l
|= (word_len
- 1) << 7;
925 /* set chipselect polarity; manage with FORCE */
926 if (!(spi
->mode
& SPI_CS_HIGH
))
927 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
929 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
931 /* set clock divisor */
932 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
935 /* set clock granularity */
936 l
&= ~OMAP2_MCSPI_CHCONF_CLKG
;
939 cs
->chctrl0
&= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK
;
940 cs
->chctrl0
|= extclk
<< 8;
941 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
944 /* set SPI mode 0..3 */
945 if (spi
->mode
& SPI_CPOL
)
946 l
|= OMAP2_MCSPI_CHCONF_POL
;
948 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
949 if (spi
->mode
& SPI_CPHA
)
950 l
|= OMAP2_MCSPI_CHCONF_PHA
;
952 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
954 mcspi_write_chconf0(spi
, l
);
956 cs
->mode
= spi
->mode
;
958 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
960 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
961 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
967 * Note that we currently allow DMA only if we get a channel
968 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
970 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
972 struct spi_master
*master
= spi
->master
;
973 struct omap2_mcspi
*mcspi
;
974 struct omap2_mcspi_dma
*mcspi_dma
;
977 mcspi
= spi_master_get_devdata(master
);
978 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
980 init_completion(&mcspi_dma
->dma_rx_completion
);
981 init_completion(&mcspi_dma
->dma_tx_completion
);
983 mcspi_dma
->dma_rx
= dma_request_chan(&master
->dev
,
984 mcspi_dma
->dma_rx_ch_name
);
985 if (IS_ERR(mcspi_dma
->dma_rx
)) {
986 ret
= PTR_ERR(mcspi_dma
->dma_rx
);
987 mcspi_dma
->dma_rx
= NULL
;
991 mcspi_dma
->dma_tx
= dma_request_chan(&master
->dev
,
992 mcspi_dma
->dma_tx_ch_name
);
993 if (IS_ERR(mcspi_dma
->dma_tx
)) {
994 ret
= PTR_ERR(mcspi_dma
->dma_tx
);
995 mcspi_dma
->dma_tx
= NULL
;
996 dma_release_channel(mcspi_dma
->dma_rx
);
997 mcspi_dma
->dma_rx
= NULL
;
1004 static int omap2_mcspi_setup(struct spi_device
*spi
)
1007 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
1008 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1009 struct omap2_mcspi_dma
*mcspi_dma
;
1010 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
1012 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1015 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
1018 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
1019 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
1023 spi
->controller_state
= cs
;
1024 /* Link this to context save list */
1025 list_add_tail(&cs
->node
, &ctx
->cs
);
1027 if (gpio_is_valid(spi
->cs_gpio
)) {
1028 ret
= gpio_request(spi
->cs_gpio
, dev_name(&spi
->dev
));
1030 dev_err(&spi
->dev
, "failed to request gpio\n");
1033 gpio_direction_output(spi
->cs_gpio
,
1034 !(spi
->mode
& SPI_CS_HIGH
));
1038 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
1039 ret
= omap2_mcspi_request_dma(spi
);
1041 dev_warn(&spi
->dev
, "not using DMA for McSPI (%d)\n",
1045 ret
= pm_runtime_get_sync(mcspi
->dev
);
1047 pm_runtime_put_noidle(mcspi
->dev
);
1052 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
1053 pm_runtime_mark_last_busy(mcspi
->dev
);
1054 pm_runtime_put_autosuspend(mcspi
->dev
);
1059 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
1061 struct omap2_mcspi
*mcspi
;
1062 struct omap2_mcspi_dma
*mcspi_dma
;
1063 struct omap2_mcspi_cs
*cs
;
1065 mcspi
= spi_master_get_devdata(spi
->master
);
1067 if (spi
->controller_state
) {
1068 /* Unlink controller state from context save list */
1069 cs
= spi
->controller_state
;
1070 list_del(&cs
->node
);
1075 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1076 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1078 if (mcspi_dma
->dma_rx
) {
1079 dma_release_channel(mcspi_dma
->dma_rx
);
1080 mcspi_dma
->dma_rx
= NULL
;
1082 if (mcspi_dma
->dma_tx
) {
1083 dma_release_channel(mcspi_dma
->dma_tx
);
1084 mcspi_dma
->dma_tx
= NULL
;
1088 if (gpio_is_valid(spi
->cs_gpio
))
1089 gpio_free(spi
->cs_gpio
);
1092 static int omap2_mcspi_transfer_one(struct spi_master
*master
,
1093 struct spi_device
*spi
,
1094 struct spi_transfer
*t
)
1097 /* We only enable one channel at a time -- the one whose message is
1098 * -- although this controller would gladly
1099 * arbitrate among multiple channels. This corresponds to "single
1100 * channel" master mode. As a side effect, we need to manage the
1101 * chipselect with the FORCE bit ... CS != channel enable.
1104 struct omap2_mcspi
*mcspi
;
1105 struct omap2_mcspi_dma
*mcspi_dma
;
1106 struct omap2_mcspi_cs
*cs
;
1107 struct omap2_mcspi_device_config
*cd
;
1108 int par_override
= 0;
1112 mcspi
= spi_master_get_devdata(master
);
1113 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1114 cs
= spi
->controller_state
;
1115 cd
= spi
->controller_data
;
1118 * The slave driver could have changed spi->mode in which case
1119 * it will be different from cs->mode (the current hardware setup).
1120 * If so, set par_override (even though its not a parity issue) so
1121 * omap2_mcspi_setup_transfer will be called to configure the hardware
1122 * with the correct mode on the first iteration of the loop below.
1124 if (spi
->mode
!= cs
->mode
)
1127 omap2_mcspi_set_enable(spi
, 0);
1129 if (gpio_is_valid(spi
->cs_gpio
))
1130 omap2_mcspi_set_cs(spi
, spi
->mode
& SPI_CS_HIGH
);
1133 (t
->speed_hz
!= spi
->max_speed_hz
) ||
1134 (t
->bits_per_word
!= spi
->bits_per_word
)) {
1136 status
= omap2_mcspi_setup_transfer(spi
, t
);
1139 if (t
->speed_hz
== spi
->max_speed_hz
&&
1140 t
->bits_per_word
== spi
->bits_per_word
)
1143 if (cd
&& cd
->cs_per_word
) {
1144 chconf
= mcspi
->ctx
.modulctrl
;
1145 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1146 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1147 mcspi
->ctx
.modulctrl
=
1148 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1151 chconf
= mcspi_cached_chconf0(spi
);
1152 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1153 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1155 if (t
->tx_buf
== NULL
)
1156 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1157 else if (t
->rx_buf
== NULL
)
1158 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1160 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1161 /* Turbo mode is for more than one word */
1162 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1163 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1166 mcspi_write_chconf0(spi
, chconf
);
1171 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1172 master
->cur_msg_mapped
&&
1173 master
->can_dma(master
, spi
, t
))
1174 omap2_mcspi_set_fifo(spi
, t
, 1);
1176 omap2_mcspi_set_enable(spi
, 1);
1178 /* RX_ONLY mode needs dummy data in TX reg */
1179 if (t
->tx_buf
== NULL
)
1180 writel_relaxed(0, cs
->base
1183 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1184 master
->cur_msg_mapped
&&
1185 master
->can_dma(master
, spi
, t
))
1186 count
= omap2_mcspi_txrx_dma(spi
, t
);
1188 count
= omap2_mcspi_txrx_pio(spi
, t
);
1190 if (count
!= t
->len
) {
1196 omap2_mcspi_set_enable(spi
, 0);
1198 if (mcspi
->fifo_depth
> 0)
1199 omap2_mcspi_set_fifo(spi
, t
, 0);
1202 /* Restore defaults if they were overriden */
1205 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1208 if (cd
&& cd
->cs_per_word
) {
1209 chconf
= mcspi
->ctx
.modulctrl
;
1210 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1211 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1212 mcspi
->ctx
.modulctrl
=
1213 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1216 omap2_mcspi_set_enable(spi
, 0);
1218 if (gpio_is_valid(spi
->cs_gpio
))
1219 omap2_mcspi_set_cs(spi
, !(spi
->mode
& SPI_CS_HIGH
));
1221 if (mcspi
->fifo_depth
> 0 && t
)
1222 omap2_mcspi_set_fifo(spi
, t
, 0);
1227 static int omap2_mcspi_prepare_message(struct spi_master
*master
,
1228 struct spi_message
*msg
)
1230 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1231 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1232 struct omap2_mcspi_cs
*cs
;
1234 /* Only a single channel can have the FORCE bit enabled
1235 * in its chconf0 register.
1236 * Scan all channels and disable them except the current one.
1237 * A FORCE can remain from a last transfer having cs_change enabled
1239 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1240 if (msg
->spi
->controller_state
== cs
)
1243 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
)) {
1244 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1245 writel_relaxed(cs
->chconf0
,
1246 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1247 readl_relaxed(cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1254 static bool omap2_mcspi_can_dma(struct spi_master
*master
,
1255 struct spi_device
*spi
,
1256 struct spi_transfer
*xfer
)
1258 return (xfer
->len
>= DMA_MIN_BYTES
);
1261 static int omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1263 struct spi_master
*master
= mcspi
->master
;
1264 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1267 ret
= pm_runtime_get_sync(mcspi
->dev
);
1269 pm_runtime_put_noidle(mcspi
->dev
);
1274 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
1275 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
1276 ctx
->wakeupenable
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1278 omap2_mcspi_set_master_mode(master
);
1279 pm_runtime_mark_last_busy(mcspi
->dev
);
1280 pm_runtime_put_autosuspend(mcspi
->dev
);
1285 * When SPI wake up from off-mode, CS is in activate state. If it was in
1286 * inactive state when driver was suspend, then force it to inactive state at
1289 static int omap_mcspi_runtime_resume(struct device
*dev
)
1291 struct spi_master
*master
= dev_get_drvdata(dev
);
1292 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1293 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1294 struct omap2_mcspi_cs
*cs
;
1296 /* McSPI: context restore */
1297 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
1298 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
1300 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1302 * We need to toggle CS state for OMAP take this
1303 * change in account.
1305 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1306 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1307 writel_relaxed(cs
->chconf0
,
1308 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1309 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1310 writel_relaxed(cs
->chconf0
,
1311 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1313 writel_relaxed(cs
->chconf0
,
1314 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1321 static struct omap2_mcspi_platform_config omap2_pdata
= {
1325 static struct omap2_mcspi_platform_config omap4_pdata
= {
1326 .regs_offset
= OMAP4_MCSPI_REG_OFFSET
,
1329 static const struct of_device_id omap_mcspi_of_match
[] = {
1331 .compatible
= "ti,omap2-mcspi",
1332 .data
= &omap2_pdata
,
1335 .compatible
= "ti,omap4-mcspi",
1336 .data
= &omap4_pdata
,
1340 MODULE_DEVICE_TABLE(of
, omap_mcspi_of_match
);
1342 static int omap2_mcspi_probe(struct platform_device
*pdev
)
1344 struct spi_master
*master
;
1345 const struct omap2_mcspi_platform_config
*pdata
;
1346 struct omap2_mcspi
*mcspi
;
1349 u32 regs_offset
= 0;
1350 struct device_node
*node
= pdev
->dev
.of_node
;
1351 const struct of_device_id
*match
;
1353 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1354 if (master
== NULL
) {
1355 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1359 /* the spi->mode bits understood by this driver: */
1360 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1361 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1362 master
->setup
= omap2_mcspi_setup
;
1363 master
->auto_runtime_pm
= true;
1364 master
->prepare_message
= omap2_mcspi_prepare_message
;
1365 master
->can_dma
= omap2_mcspi_can_dma
;
1366 master
->transfer_one
= omap2_mcspi_transfer_one
;
1367 master
->set_cs
= omap2_mcspi_set_cs
;
1368 master
->cleanup
= omap2_mcspi_cleanup
;
1369 master
->dev
.of_node
= node
;
1370 master
->max_speed_hz
= OMAP2_MCSPI_MAX_FREQ
;
1371 master
->min_speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> 15;
1373 platform_set_drvdata(pdev
, master
);
1375 mcspi
= spi_master_get_devdata(master
);
1376 mcspi
->master
= master
;
1378 match
= of_match_device(omap_mcspi_of_match
, &pdev
->dev
);
1380 u32 num_cs
= 1; /* default number of chipselect */
1381 pdata
= match
->data
;
1383 of_property_read_u32(node
, "ti,spi-num-cs", &num_cs
);
1384 master
->num_chipselect
= num_cs
;
1385 if (of_get_property(node
, "ti,pindir-d0-out-d1-in", NULL
))
1386 mcspi
->pin_dir
= MCSPI_PINDIR_D0_OUT_D1_IN
;
1388 pdata
= dev_get_platdata(&pdev
->dev
);
1389 master
->num_chipselect
= pdata
->num_cs
;
1390 mcspi
->pin_dir
= pdata
->pin_dir
;
1392 regs_offset
= pdata
->regs_offset
;
1394 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1395 mcspi
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1396 if (IS_ERR(mcspi
->base
)) {
1397 status
= PTR_ERR(mcspi
->base
);
1400 mcspi
->phys
= r
->start
+ regs_offset
;
1401 mcspi
->base
+= regs_offset
;
1403 mcspi
->dev
= &pdev
->dev
;
1405 INIT_LIST_HEAD(&mcspi
->ctx
.cs
);
1407 mcspi
->dma_channels
= devm_kcalloc(&pdev
->dev
, master
->num_chipselect
,
1408 sizeof(struct omap2_mcspi_dma
),
1410 if (mcspi
->dma_channels
== NULL
) {
1415 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1416 sprintf(mcspi
->dma_channels
[i
].dma_rx_ch_name
, "rx%d", i
);
1417 sprintf(mcspi
->dma_channels
[i
].dma_tx_ch_name
, "tx%d", i
);
1420 pm_runtime_use_autosuspend(&pdev
->dev
);
1421 pm_runtime_set_autosuspend_delay(&pdev
->dev
, SPI_AUTOSUSPEND_TIMEOUT
);
1422 pm_runtime_enable(&pdev
->dev
);
1424 status
= omap2_mcspi_master_setup(mcspi
);
1428 status
= devm_spi_register_master(&pdev
->dev
, master
);
1435 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1436 pm_runtime_put_sync(&pdev
->dev
);
1437 pm_runtime_disable(&pdev
->dev
);
1439 spi_master_put(master
);
1443 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1445 struct spi_master
*master
= platform_get_drvdata(pdev
);
1446 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1448 pm_runtime_dont_use_autosuspend(mcspi
->dev
);
1449 pm_runtime_put_sync(mcspi
->dev
);
1450 pm_runtime_disable(&pdev
->dev
);
1455 /* work with hotplug and coldplug */
1456 MODULE_ALIAS("platform:omap2_mcspi");
1458 #ifdef CONFIG_SUSPEND
1459 static int omap2_mcspi_suspend_noirq(struct device
*dev
)
1461 return pinctrl_pm_select_sleep_state(dev
);
1464 static int omap2_mcspi_resume_noirq(struct device
*dev
)
1466 struct spi_master
*master
= dev_get_drvdata(dev
);
1467 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1470 error
= pinctrl_pm_select_default_state(dev
);
1472 dev_warn(mcspi
->dev
, "%s: failed to set pins: %i\n",
1479 #define omap2_mcspi_suspend_noirq NULL
1480 #define omap2_mcspi_resume_noirq NULL
1483 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1484 .suspend_noirq
= omap2_mcspi_suspend_noirq
,
1485 .resume_noirq
= omap2_mcspi_resume_noirq
,
1486 .runtime_resume
= omap_mcspi_runtime_resume
,
1489 static struct platform_driver omap2_mcspi_driver
= {
1491 .name
= "omap2_mcspi",
1492 .pm
= &omap2_mcspi_pm_ops
,
1493 .of_match_table
= omap_mcspi_of_match
,
1495 .probe
= omap2_mcspi_probe
,
1496 .remove
= omap2_mcspi_remove
,
1499 module_platform_driver(omap2_mcspi_driver
);
1500 MODULE_LICENSE("GPL");