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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/dmaengine.h>
32 #include <linux/omap-dma.h>
33 #include <linux/platform_device.h>
34 #include <linux/err.h>
35 #include <linux/clk.h>
37 #include <linux/slab.h>
38 #include <linux/pm_runtime.h>
40 #include <linux/of_device.h>
41 #include <linux/gcd.h>
43 #include <linux/spi/spi.h>
45 #include <linux/platform_data/spi-omap2-mcspi.h>
47 #define OMAP2_MCSPI_MAX_FREQ 48000000
48 #define OMAP2_MCSPI_MAX_FIFODEPTH 64
49 #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
50 #define SPI_AUTOSUSPEND_TIMEOUT 2000
52 #define OMAP2_MCSPI_REVISION 0x00
53 #define OMAP2_MCSPI_SYSSTATUS 0x14
54 #define OMAP2_MCSPI_IRQSTATUS 0x18
55 #define OMAP2_MCSPI_IRQENABLE 0x1c
56 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
57 #define OMAP2_MCSPI_SYST 0x24
58 #define OMAP2_MCSPI_MODULCTRL 0x28
59 #define OMAP2_MCSPI_XFERLEVEL 0x7c
61 /* per-channel banks, 0x14 bytes each, first is: */
62 #define OMAP2_MCSPI_CHCONF0 0x2c
63 #define OMAP2_MCSPI_CHSTAT0 0x30
64 #define OMAP2_MCSPI_CHCTRL0 0x34
65 #define OMAP2_MCSPI_TX0 0x38
66 #define OMAP2_MCSPI_RX0 0x3c
68 /* per-register bitmasks: */
69 #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
71 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
72 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
73 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
75 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
76 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
77 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
78 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
79 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
80 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
81 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
82 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
83 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
84 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
85 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
86 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
87 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
88 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
89 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
90 #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
91 #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
93 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
94 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
95 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
96 #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
98 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
100 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
102 /* We have 2 DMA channels per CS, one for RX and one for TX */
103 struct omap2_mcspi_dma
{
104 struct dma_chan
*dma_tx
;
105 struct dma_chan
*dma_rx
;
110 struct completion dma_tx_completion
;
111 struct completion dma_rx_completion
;
113 char dma_rx_ch_name
[14];
114 char dma_tx_ch_name
[14];
117 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
118 * cache operations; better heuristics consider wordsize and bitrate.
120 #define DMA_MIN_BYTES 160
124 * Used for context save and restore, structure members to be updated whenever
125 * corresponding registers are modified.
127 struct omap2_mcspi_regs
{
134 struct spi_master
*master
;
135 /* Virtual base address of the controller */
138 /* SPI1 has 4 channels, while SPI2 has 2 */
139 struct omap2_mcspi_dma
*dma_channels
;
141 struct omap2_mcspi_regs ctx
;
143 unsigned int pin_dir
:1;
146 struct omap2_mcspi_cs
{
150 struct list_head node
;
151 /* Context save and restore shadow register */
155 static inline void mcspi_write_reg(struct spi_master
*master
,
158 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
160 __raw_writel(val
, mcspi
->base
+ idx
);
163 static inline u32
mcspi_read_reg(struct spi_master
*master
, int idx
)
165 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
167 return __raw_readl(mcspi
->base
+ idx
);
170 static inline void mcspi_write_cs_reg(const struct spi_device
*spi
,
173 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
175 __raw_writel(val
, cs
->base
+ idx
);
178 static inline u32
mcspi_read_cs_reg(const struct spi_device
*spi
, int idx
)
180 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
182 return __raw_readl(cs
->base
+ idx
);
185 static inline u32
mcspi_cached_chconf0(const struct spi_device
*spi
)
187 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
192 static inline void mcspi_write_chconf0(const struct spi_device
*spi
, u32 val
)
194 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
197 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
, val
);
198 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCONF0
);
201 static inline int mcspi_bytes_per_word(int word_len
)
205 else if (word_len
<= 16)
207 else /* word_len <= 32 */
211 static void omap2_mcspi_set_dma_req(const struct spi_device
*spi
,
212 int is_read
, int enable
)
216 l
= mcspi_cached_chconf0(spi
);
218 if (is_read
) /* 1 is read, 0 write */
219 rw
= OMAP2_MCSPI_CHCONF_DMAR
;
221 rw
= OMAP2_MCSPI_CHCONF_DMAW
;
228 mcspi_write_chconf0(spi
, l
);
231 static void omap2_mcspi_set_enable(const struct spi_device
*spi
, int enable
)
235 l
= enable
? OMAP2_MCSPI_CHCTRL_EN
: 0;
236 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, l
);
237 /* Flash post-writes */
238 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
);
241 static void omap2_mcspi_force_cs(struct spi_device
*spi
, int cs_active
)
245 l
= mcspi_cached_chconf0(spi
);
247 l
|= OMAP2_MCSPI_CHCONF_FORCE
;
249 l
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
251 mcspi_write_chconf0(spi
, l
);
254 static void omap2_mcspi_set_master_mode(struct spi_master
*master
)
256 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
257 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
261 * Setup when switching from (reset default) slave mode
262 * to single-channel master mode
264 l
= mcspi_read_reg(master
, OMAP2_MCSPI_MODULCTRL
);
265 l
&= ~(OMAP2_MCSPI_MODULCTRL_STEST
| OMAP2_MCSPI_MODULCTRL_MS
);
266 l
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
267 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, l
);
272 static void omap2_mcspi_set_fifo(const struct spi_device
*spi
,
273 struct spi_transfer
*t
, int enable
)
275 struct spi_master
*master
= spi
->master
;
276 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
277 struct omap2_mcspi
*mcspi
;
279 int fifo_depth
, bytes_per_word
;
280 u32 chconf
, xferlevel
;
282 mcspi
= spi_master_get_devdata(master
);
284 chconf
= mcspi_cached_chconf0(spi
);
286 bytes_per_word
= mcspi_bytes_per_word(cs
->word_len
);
287 if (t
->len
% bytes_per_word
!= 0)
290 fifo_depth
= gcd(t
->len
, OMAP2_MCSPI_MAX_FIFODEPTH
);
291 if (fifo_depth
< 2 || fifo_depth
% bytes_per_word
!= 0)
294 wcnt
= t
->len
/ bytes_per_word
;
295 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
298 xferlevel
= wcnt
<< 16;
299 if (t
->rx_buf
!= NULL
) {
300 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
301 xferlevel
|= (fifo_depth
- 1) << 8;
303 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
304 xferlevel
|= fifo_depth
- 1;
307 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
308 mcspi_write_chconf0(spi
, chconf
);
309 mcspi
->fifo_depth
= fifo_depth
;
315 if (t
->rx_buf
!= NULL
)
316 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
318 if (t
->tx_buf
!= NULL
)
319 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
321 mcspi_write_chconf0(spi
, chconf
);
322 mcspi
->fifo_depth
= 0;
325 static void omap2_mcspi_restore_ctx(struct omap2_mcspi
*mcspi
)
327 struct spi_master
*spi_cntrl
= mcspi
->master
;
328 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
329 struct omap2_mcspi_cs
*cs
;
331 /* McSPI: context restore */
332 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
333 mcspi_write_reg(spi_cntrl
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
335 list_for_each_entry(cs
, &ctx
->cs
, node
)
336 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
339 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
341 unsigned long timeout
;
343 timeout
= jiffies
+ msecs_to_jiffies(1000);
344 while (!(__raw_readl(reg
) & bit
)) {
345 if (time_after(jiffies
, timeout
)) {
346 if (!(__raw_readl(reg
) & bit
))
356 static void omap2_mcspi_rx_callback(void *data
)
358 struct spi_device
*spi
= data
;
359 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
360 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
362 /* We must disable the DMA RX request */
363 omap2_mcspi_set_dma_req(spi
, 1, 0);
365 complete(&mcspi_dma
->dma_rx_completion
);
368 static void omap2_mcspi_tx_callback(void *data
)
370 struct spi_device
*spi
= data
;
371 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
372 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
374 /* We must disable the DMA TX request */
375 omap2_mcspi_set_dma_req(spi
, 0, 0);
377 complete(&mcspi_dma
->dma_tx_completion
);
380 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
381 struct spi_transfer
*xfer
,
382 struct dma_slave_config cfg
)
384 struct omap2_mcspi
*mcspi
;
385 struct omap2_mcspi_dma
*mcspi_dma
;
388 mcspi
= spi_master_get_devdata(spi
->master
);
389 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
392 if (mcspi_dma
->dma_tx
) {
393 struct dma_async_tx_descriptor
*tx
;
394 struct scatterlist sg
;
396 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
398 sg_init_table(&sg
, 1);
399 sg_dma_address(&sg
) = xfer
->tx_dma
;
400 sg_dma_len(&sg
) = xfer
->len
;
402 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, &sg
, 1,
403 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
405 tx
->callback
= omap2_mcspi_tx_callback
;
406 tx
->callback_param
= spi
;
407 dmaengine_submit(tx
);
409 /* FIXME: fall back to PIO? */
412 dma_async_issue_pending(mcspi_dma
->dma_tx
);
413 omap2_mcspi_set_dma_req(spi
, 0, 1);
418 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
419 struct dma_slave_config cfg
,
422 struct omap2_mcspi
*mcspi
;
423 struct omap2_mcspi_dma
*mcspi_dma
;
424 unsigned int count
, dma_count
;
427 int word_len
, element_count
;
428 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
429 mcspi
= spi_master_get_devdata(spi
->master
);
430 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
432 dma_count
= xfer
->len
;
434 if (mcspi
->fifo_depth
== 0)
437 word_len
= cs
->word_len
;
438 l
= mcspi_cached_chconf0(spi
);
441 element_count
= count
;
442 else if (word_len
<= 16)
443 element_count
= count
>> 1;
444 else /* word_len <= 32 */
445 element_count
= count
>> 2;
447 if (mcspi_dma
->dma_rx
) {
448 struct dma_async_tx_descriptor
*tx
;
449 struct scatterlist sg
;
451 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
453 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
456 sg_init_table(&sg
, 1);
457 sg_dma_address(&sg
) = xfer
->rx_dma
;
458 sg_dma_len(&sg
) = dma_count
;
460 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
, &sg
, 1,
461 DMA_DEV_TO_MEM
, DMA_PREP_INTERRUPT
|
464 tx
->callback
= omap2_mcspi_rx_callback
;
465 tx
->callback_param
= spi
;
466 dmaengine_submit(tx
);
468 /* FIXME: fall back to PIO? */
472 dma_async_issue_pending(mcspi_dma
->dma_rx
);
473 omap2_mcspi_set_dma_req(spi
, 1, 1);
475 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
476 dma_unmap_single(mcspi
->dev
, xfer
->rx_dma
, count
,
479 if (mcspi
->fifo_depth
> 0)
482 omap2_mcspi_set_enable(spi
, 0);
484 elements
= element_count
- 1;
486 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
489 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
490 & OMAP2_MCSPI_CHSTAT_RXS
)) {
493 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
495 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
496 else if (word_len
<= 16)
497 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
498 else /* word_len <= 32 */
499 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
501 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
502 dev_err(&spi
->dev
, "DMA RX penultimate word empty");
503 count
-= (bytes_per_word
<< 1);
504 omap2_mcspi_set_enable(spi
, 1);
508 if (likely(mcspi_read_cs_reg(spi
, OMAP2_MCSPI_CHSTAT0
)
509 & OMAP2_MCSPI_CHSTAT_RXS
)) {
512 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
514 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
515 else if (word_len
<= 16)
516 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
517 else /* word_len <= 32 */
518 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
520 dev_err(&spi
->dev
, "DMA RX last word empty");
521 count
-= mcspi_bytes_per_word(word_len
);
523 omap2_mcspi_set_enable(spi
, 1);
528 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
530 struct omap2_mcspi
*mcspi
;
531 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
532 struct omap2_mcspi_dma
*mcspi_dma
;
537 struct dma_slave_config cfg
;
538 enum dma_slave_buswidth width
;
541 void __iomem
*chstat_reg
;
542 void __iomem
*irqstat_reg
;
545 mcspi
= spi_master_get_devdata(spi
->master
);
546 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
547 l
= mcspi_cached_chconf0(spi
);
550 if (cs
->word_len
<= 8) {
551 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
553 } else if (cs
->word_len
<= 16) {
554 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
557 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
564 if (mcspi
->fifo_depth
> 0) {
565 if (count
> mcspi
->fifo_depth
)
566 burst
= mcspi
->fifo_depth
/ es
;
571 memset(&cfg
, 0, sizeof(cfg
));
572 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
573 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
574 cfg
.src_addr_width
= width
;
575 cfg
.dst_addr_width
= width
;
576 cfg
.src_maxburst
= burst
;
577 cfg
.dst_maxburst
= burst
;
583 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
586 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
589 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
590 dma_unmap_single(mcspi
->dev
, xfer
->tx_dma
, xfer
->len
,
593 if (mcspi
->fifo_depth
> 0) {
594 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
596 if (mcspi_wait_for_reg_bit(irqstat_reg
,
597 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
598 dev_err(&spi
->dev
, "EOW timed out\n");
600 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
601 OMAP2_MCSPI_IRQSTATUS_EOW
);
604 /* for TX_ONLY mode, be sure all words have shifted out */
606 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
607 if (mcspi
->fifo_depth
> 0) {
608 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
609 OMAP2_MCSPI_CHSTAT_TXFFE
);
611 dev_err(&spi
->dev
, "TXFFE timed out\n");
613 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
614 OMAP2_MCSPI_CHSTAT_TXS
);
616 dev_err(&spi
->dev
, "TXS timed out\n");
619 (mcspi_wait_for_reg_bit(chstat_reg
,
620 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
621 dev_err(&spi
->dev
, "EOT timed out\n");
628 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
630 struct omap2_mcspi
*mcspi
;
631 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
632 unsigned int count
, c
;
634 void __iomem
*base
= cs
->base
;
635 void __iomem
*tx_reg
;
636 void __iomem
*rx_reg
;
637 void __iomem
*chstat_reg
;
640 mcspi
= spi_master_get_devdata(spi
->master
);
643 word_len
= cs
->word_len
;
645 l
= mcspi_cached_chconf0(spi
);
647 /* We store the pre-calculated register addresses on stack to speed
648 * up the transfer loop. */
649 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
650 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
651 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
653 if (c
< (word_len
>>3))
666 if (mcspi_wait_for_reg_bit(chstat_reg
,
667 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
668 dev_err(&spi
->dev
, "TXS timed out\n");
671 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
673 __raw_writel(*tx
++, tx_reg
);
676 if (mcspi_wait_for_reg_bit(chstat_reg
,
677 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
678 dev_err(&spi
->dev
, "RXS timed out\n");
682 if (c
== 1 && tx
== NULL
&&
683 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
684 omap2_mcspi_set_enable(spi
, 0);
685 *rx
++ = __raw_readl(rx_reg
);
686 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
687 word_len
, *(rx
- 1));
688 if (mcspi_wait_for_reg_bit(chstat_reg
,
689 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
695 } else if (c
== 0 && tx
== NULL
) {
696 omap2_mcspi_set_enable(spi
, 0);
699 *rx
++ = __raw_readl(rx_reg
);
700 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
701 word_len
, *(rx
- 1));
704 } else if (word_len
<= 16) {
713 if (mcspi_wait_for_reg_bit(chstat_reg
,
714 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
715 dev_err(&spi
->dev
, "TXS timed out\n");
718 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
720 __raw_writel(*tx
++, tx_reg
);
723 if (mcspi_wait_for_reg_bit(chstat_reg
,
724 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
725 dev_err(&spi
->dev
, "RXS timed out\n");
729 if (c
== 2 && tx
== NULL
&&
730 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
731 omap2_mcspi_set_enable(spi
, 0);
732 *rx
++ = __raw_readl(rx_reg
);
733 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
734 word_len
, *(rx
- 1));
735 if (mcspi_wait_for_reg_bit(chstat_reg
,
736 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
742 } else if (c
== 0 && tx
== NULL
) {
743 omap2_mcspi_set_enable(spi
, 0);
746 *rx
++ = __raw_readl(rx_reg
);
747 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
748 word_len
, *(rx
- 1));
751 } else if (word_len
<= 32) {
760 if (mcspi_wait_for_reg_bit(chstat_reg
,
761 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
762 dev_err(&spi
->dev
, "TXS timed out\n");
765 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
767 __raw_writel(*tx
++, tx_reg
);
770 if (mcspi_wait_for_reg_bit(chstat_reg
,
771 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
772 dev_err(&spi
->dev
, "RXS timed out\n");
776 if (c
== 4 && tx
== NULL
&&
777 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
778 omap2_mcspi_set_enable(spi
, 0);
779 *rx
++ = __raw_readl(rx_reg
);
780 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
781 word_len
, *(rx
- 1));
782 if (mcspi_wait_for_reg_bit(chstat_reg
,
783 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
789 } else if (c
== 0 && tx
== NULL
) {
790 omap2_mcspi_set_enable(spi
, 0);
793 *rx
++ = __raw_readl(rx_reg
);
794 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
795 word_len
, *(rx
- 1));
800 /* for TX_ONLY mode, be sure all words have shifted out */
801 if (xfer
->rx_buf
== NULL
) {
802 if (mcspi_wait_for_reg_bit(chstat_reg
,
803 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
804 dev_err(&spi
->dev
, "TXS timed out\n");
805 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
806 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
807 dev_err(&spi
->dev
, "EOT timed out\n");
809 /* disable chan to purge rx datas received in TX_ONLY transfer,
810 * otherwise these rx datas will affect the direct following
813 omap2_mcspi_set_enable(spi
, 0);
816 omap2_mcspi_set_enable(spi
, 1);
820 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
824 for (div
= 0; div
< 15; div
++)
825 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
831 /* called only when no transfer is active to this device */
832 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
833 struct spi_transfer
*t
)
835 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
836 struct omap2_mcspi
*mcspi
;
837 struct spi_master
*spi_cntrl
;
839 u8 word_len
= spi
->bits_per_word
;
840 u32 speed_hz
= spi
->max_speed_hz
;
842 mcspi
= spi_master_get_devdata(spi
->master
);
843 spi_cntrl
= mcspi
->master
;
845 if (t
!= NULL
&& t
->bits_per_word
)
846 word_len
= t
->bits_per_word
;
848 cs
->word_len
= word_len
;
850 if (t
&& t
->speed_hz
)
851 speed_hz
= t
->speed_hz
;
853 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
854 div
= omap2_mcspi_calc_divisor(speed_hz
);
856 l
= mcspi_cached_chconf0(spi
);
858 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
859 * REVISIT: this controller could support SPI_3WIRE mode.
861 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
862 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
863 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
864 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
866 l
|= OMAP2_MCSPI_CHCONF_IS
;
867 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
868 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
872 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
873 l
|= (word_len
- 1) << 7;
875 /* set chipselect polarity; manage with FORCE */
876 if (!(spi
->mode
& SPI_CS_HIGH
))
877 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
879 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
881 /* set clock divisor */
882 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
885 /* set SPI mode 0..3 */
886 if (spi
->mode
& SPI_CPOL
)
887 l
|= OMAP2_MCSPI_CHCONF_POL
;
889 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
890 if (spi
->mode
& SPI_CPHA
)
891 l
|= OMAP2_MCSPI_CHCONF_PHA
;
893 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
895 mcspi_write_chconf0(spi
, l
);
897 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
898 OMAP2_MCSPI_MAX_FREQ
>> div
,
899 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
900 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
906 * Note that we currently allow DMA only if we get a channel
907 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
909 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
911 struct spi_master
*master
= spi
->master
;
912 struct omap2_mcspi
*mcspi
;
913 struct omap2_mcspi_dma
*mcspi_dma
;
917 mcspi
= spi_master_get_devdata(master
);
918 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
920 init_completion(&mcspi_dma
->dma_rx_completion
);
921 init_completion(&mcspi_dma
->dma_tx_completion
);
924 dma_cap_set(DMA_SLAVE
, mask
);
925 sig
= mcspi_dma
->dma_rx_sync_dev
;
928 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
930 mcspi_dma
->dma_rx_ch_name
);
931 if (!mcspi_dma
->dma_rx
)
934 sig
= mcspi_dma
->dma_tx_sync_dev
;
936 dma_request_slave_channel_compat(mask
, omap_dma_filter_fn
,
938 mcspi_dma
->dma_tx_ch_name
);
940 if (!mcspi_dma
->dma_tx
) {
941 dma_release_channel(mcspi_dma
->dma_rx
);
942 mcspi_dma
->dma_rx
= NULL
;
949 dev_warn(&spi
->dev
, "not using DMA for McSPI\n");
953 static int omap2_mcspi_setup(struct spi_device
*spi
)
956 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
957 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
958 struct omap2_mcspi_dma
*mcspi_dma
;
959 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
961 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
964 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
967 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
968 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
970 spi
->controller_state
= cs
;
971 /* Link this to context save list */
972 list_add_tail(&cs
->node
, &ctx
->cs
);
975 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
976 ret
= omap2_mcspi_request_dma(spi
);
977 if (ret
< 0 && ret
!= -EAGAIN
)
981 ret
= pm_runtime_get_sync(mcspi
->dev
);
985 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
986 pm_runtime_mark_last_busy(mcspi
->dev
);
987 pm_runtime_put_autosuspend(mcspi
->dev
);
992 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
994 struct omap2_mcspi
*mcspi
;
995 struct omap2_mcspi_dma
*mcspi_dma
;
996 struct omap2_mcspi_cs
*cs
;
998 mcspi
= spi_master_get_devdata(spi
->master
);
1000 if (spi
->controller_state
) {
1001 /* Unlink controller state from context save list */
1002 cs
= spi
->controller_state
;
1003 list_del(&cs
->node
);
1008 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1009 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1011 if (mcspi_dma
->dma_rx
) {
1012 dma_release_channel(mcspi_dma
->dma_rx
);
1013 mcspi_dma
->dma_rx
= NULL
;
1015 if (mcspi_dma
->dma_tx
) {
1016 dma_release_channel(mcspi_dma
->dma_tx
);
1017 mcspi_dma
->dma_tx
= NULL
;
1022 static void omap2_mcspi_work(struct omap2_mcspi
*mcspi
, struct spi_message
*m
)
1025 /* We only enable one channel at a time -- the one whose message is
1026 * -- although this controller would gladly
1027 * arbitrate among multiple channels. This corresponds to "single
1028 * channel" master mode. As a side effect, we need to manage the
1029 * chipselect with the FORCE bit ... CS != channel enable.
1032 struct spi_device
*spi
;
1033 struct spi_transfer
*t
= NULL
;
1034 struct spi_master
*master
;
1035 struct omap2_mcspi_dma
*mcspi_dma
;
1037 struct omap2_mcspi_cs
*cs
;
1038 struct omap2_mcspi_device_config
*cd
;
1039 int par_override
= 0;
1044 master
= spi
->master
;
1045 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1046 cs
= spi
->controller_state
;
1047 cd
= spi
->controller_data
;
1049 omap2_mcspi_set_enable(spi
, 0);
1050 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
1051 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
1055 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
1057 status
= omap2_mcspi_setup_transfer(spi
, t
);
1060 if (!t
->speed_hz
&& !t
->bits_per_word
)
1063 if (cd
&& cd
->cs_per_word
) {
1064 chconf
= mcspi
->ctx
.modulctrl
;
1065 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1066 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1067 mcspi
->ctx
.modulctrl
=
1068 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1073 omap2_mcspi_force_cs(spi
, 1);
1077 chconf
= mcspi_cached_chconf0(spi
);
1078 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1079 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1081 if (t
->tx_buf
== NULL
)
1082 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1083 else if (t
->rx_buf
== NULL
)
1084 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1086 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1087 /* Turbo mode is for more than one word */
1088 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1089 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1092 mcspi_write_chconf0(spi
, chconf
);
1097 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1098 (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
))
1099 omap2_mcspi_set_fifo(spi
, t
, 1);
1101 omap2_mcspi_set_enable(spi
, 1);
1103 /* RX_ONLY mode needs dummy data in TX reg */
1104 if (t
->tx_buf
== NULL
)
1105 __raw_writel(0, cs
->base
1108 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1109 (m
->is_dma_mapped
|| t
->len
>= DMA_MIN_BYTES
))
1110 count
= omap2_mcspi_txrx_dma(spi
, t
);
1112 count
= omap2_mcspi_txrx_pio(spi
, t
);
1113 m
->actual_length
+= count
;
1115 if (count
!= t
->len
) {
1122 udelay(t
->delay_usecs
);
1124 /* ignore the "leave it on after last xfer" hint */
1126 omap2_mcspi_force_cs(spi
, 0);
1130 omap2_mcspi_set_enable(spi
, 0);
1132 if (mcspi
->fifo_depth
> 0)
1133 omap2_mcspi_set_fifo(spi
, t
, 0);
1135 /* Restore defaults if they were overriden */
1138 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1142 omap2_mcspi_force_cs(spi
, 0);
1144 if (cd
&& cd
->cs_per_word
) {
1145 chconf
= mcspi
->ctx
.modulctrl
;
1146 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1147 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1148 mcspi
->ctx
.modulctrl
=
1149 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1152 omap2_mcspi_set_enable(spi
, 0);
1154 if (mcspi
->fifo_depth
> 0 && t
)
1155 omap2_mcspi_set_fifo(spi
, t
, 0);
1160 static int omap2_mcspi_transfer_one_message(struct spi_master
*master
,
1161 struct spi_message
*m
)
1163 struct spi_device
*spi
;
1164 struct omap2_mcspi
*mcspi
;
1165 struct omap2_mcspi_dma
*mcspi_dma
;
1166 struct spi_transfer
*t
;
1169 mcspi
= spi_master_get_devdata(master
);
1170 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1171 m
->actual_length
= 0;
1174 /* reject invalid messages and transfers */
1175 if (list_empty(&m
->transfers
))
1177 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
1178 const void *tx_buf
= t
->tx_buf
;
1179 void *rx_buf
= t
->rx_buf
;
1180 unsigned len
= t
->len
;
1182 if (t
->speed_hz
> OMAP2_MCSPI_MAX_FREQ
1183 || (len
&& !(rx_buf
|| tx_buf
))) {
1184 dev_dbg(mcspi
->dev
, "transfer: %d Hz, %d %s%s, %d bpw\n",
1192 if (t
->speed_hz
&& t
->speed_hz
< (OMAP2_MCSPI_MAX_FREQ
>> 15)) {
1193 dev_dbg(mcspi
->dev
, "speed_hz %d below minimum %d Hz\n",
1195 OMAP2_MCSPI_MAX_FREQ
>> 15);
1199 if (m
->is_dma_mapped
|| len
< DMA_MIN_BYTES
)
1202 if (mcspi_dma
->dma_tx
&& tx_buf
!= NULL
) {
1203 t
->tx_dma
= dma_map_single(mcspi
->dev
, (void *) tx_buf
,
1204 len
, DMA_TO_DEVICE
);
1205 if (dma_mapping_error(mcspi
->dev
, t
->tx_dma
)) {
1206 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1211 if (mcspi_dma
->dma_rx
&& rx_buf
!= NULL
) {
1212 t
->rx_dma
= dma_map_single(mcspi
->dev
, rx_buf
, t
->len
,
1214 if (dma_mapping_error(mcspi
->dev
, t
->rx_dma
)) {
1215 dev_dbg(mcspi
->dev
, "dma %cX %d bytes error\n",
1218 dma_unmap_single(mcspi
->dev
, t
->tx_dma
,
1219 len
, DMA_TO_DEVICE
);
1225 omap2_mcspi_work(mcspi
, m
);
1226 spi_finalize_current_message(master
);
1230 static int omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1232 struct spi_master
*master
= mcspi
->master
;
1233 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1236 ret
= pm_runtime_get_sync(mcspi
->dev
);
1240 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
1241 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
1242 ctx
->wakeupenable
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1244 omap2_mcspi_set_master_mode(master
);
1245 pm_runtime_mark_last_busy(mcspi
->dev
);
1246 pm_runtime_put_autosuspend(mcspi
->dev
);
1250 static int omap_mcspi_runtime_resume(struct device
*dev
)
1252 struct omap2_mcspi
*mcspi
;
1253 struct spi_master
*master
;
1255 master
= dev_get_drvdata(dev
);
1256 mcspi
= spi_master_get_devdata(master
);
1257 omap2_mcspi_restore_ctx(mcspi
);
1262 static struct omap2_mcspi_platform_config omap2_pdata
= {
1266 static struct omap2_mcspi_platform_config omap4_pdata
= {
1267 .regs_offset
= OMAP4_MCSPI_REG_OFFSET
,
1270 static const struct of_device_id omap_mcspi_of_match
[] = {
1272 .compatible
= "ti,omap2-mcspi",
1273 .data
= &omap2_pdata
,
1276 .compatible
= "ti,omap4-mcspi",
1277 .data
= &omap4_pdata
,
1281 MODULE_DEVICE_TABLE(of
, omap_mcspi_of_match
);
1283 static int omap2_mcspi_probe(struct platform_device
*pdev
)
1285 struct spi_master
*master
;
1286 const struct omap2_mcspi_platform_config
*pdata
;
1287 struct omap2_mcspi
*mcspi
;
1290 u32 regs_offset
= 0;
1291 static int bus_num
= 1;
1292 struct device_node
*node
= pdev
->dev
.of_node
;
1293 const struct of_device_id
*match
;
1295 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1296 if (master
== NULL
) {
1297 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1301 /* the spi->mode bits understood by this driver: */
1302 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1303 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1304 master
->setup
= omap2_mcspi_setup
;
1305 master
->auto_runtime_pm
= true;
1306 master
->transfer_one_message
= omap2_mcspi_transfer_one_message
;
1307 master
->cleanup
= omap2_mcspi_cleanup
;
1308 master
->dev
.of_node
= node
;
1310 platform_set_drvdata(pdev
, master
);
1312 mcspi
= spi_master_get_devdata(master
);
1313 mcspi
->master
= master
;
1315 match
= of_match_device(omap_mcspi_of_match
, &pdev
->dev
);
1317 u32 num_cs
= 1; /* default number of chipselect */
1318 pdata
= match
->data
;
1320 of_property_read_u32(node
, "ti,spi-num-cs", &num_cs
);
1321 master
->num_chipselect
= num_cs
;
1322 master
->bus_num
= bus_num
++;
1323 if (of_get_property(node
, "ti,pindir-d0-out-d1-in", NULL
))
1324 mcspi
->pin_dir
= MCSPI_PINDIR_D0_OUT_D1_IN
;
1326 pdata
= dev_get_platdata(&pdev
->dev
);
1327 master
->num_chipselect
= pdata
->num_cs
;
1329 master
->bus_num
= pdev
->id
;
1330 mcspi
->pin_dir
= pdata
->pin_dir
;
1332 regs_offset
= pdata
->regs_offset
;
1334 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1340 r
->start
+= regs_offset
;
1341 r
->end
+= regs_offset
;
1342 mcspi
->phys
= r
->start
;
1344 mcspi
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1345 if (IS_ERR(mcspi
->base
)) {
1346 status
= PTR_ERR(mcspi
->base
);
1350 mcspi
->dev
= &pdev
->dev
;
1352 INIT_LIST_HEAD(&mcspi
->ctx
.cs
);
1354 mcspi
->dma_channels
= kcalloc(master
->num_chipselect
,
1355 sizeof(struct omap2_mcspi_dma
),
1358 if (mcspi
->dma_channels
== NULL
)
1361 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1362 char *dma_rx_ch_name
= mcspi
->dma_channels
[i
].dma_rx_ch_name
;
1363 char *dma_tx_ch_name
= mcspi
->dma_channels
[i
].dma_tx_ch_name
;
1364 struct resource
*dma_res
;
1366 sprintf(dma_rx_ch_name
, "rx%d", i
);
1367 if (!pdev
->dev
.of_node
) {
1369 platform_get_resource_byname(pdev
,
1374 "cannot get DMA RX channel\n");
1379 mcspi
->dma_channels
[i
].dma_rx_sync_dev
=
1382 sprintf(dma_tx_ch_name
, "tx%d", i
);
1383 if (!pdev
->dev
.of_node
) {
1385 platform_get_resource_byname(pdev
,
1390 "cannot get DMA TX channel\n");
1395 mcspi
->dma_channels
[i
].dma_tx_sync_dev
=
1403 pm_runtime_use_autosuspend(&pdev
->dev
);
1404 pm_runtime_set_autosuspend_delay(&pdev
->dev
, SPI_AUTOSUSPEND_TIMEOUT
);
1405 pm_runtime_enable(&pdev
->dev
);
1407 status
= omap2_mcspi_master_setup(mcspi
);
1411 status
= spi_register_master(master
);
1418 pm_runtime_disable(&pdev
->dev
);
1420 kfree(mcspi
->dma_channels
);
1422 spi_master_put(master
);
1426 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1428 struct spi_master
*master
;
1429 struct omap2_mcspi
*mcspi
;
1430 struct omap2_mcspi_dma
*dma_channels
;
1432 master
= platform_get_drvdata(pdev
);
1433 mcspi
= spi_master_get_devdata(master
);
1434 dma_channels
= mcspi
->dma_channels
;
1436 pm_runtime_put_sync(mcspi
->dev
);
1437 pm_runtime_disable(&pdev
->dev
);
1439 spi_unregister_master(master
);
1440 kfree(dma_channels
);
1445 /* work with hotplug and coldplug */
1446 MODULE_ALIAS("platform:omap2_mcspi");
1448 #ifdef CONFIG_SUSPEND
1450 * When SPI wake up from off-mode, CS is in activate state. If it was in
1451 * unactive state when driver was suspend, then force it to unactive state at
1454 static int omap2_mcspi_resume(struct device
*dev
)
1456 struct spi_master
*master
= dev_get_drvdata(dev
);
1457 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1458 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1459 struct omap2_mcspi_cs
*cs
;
1461 pm_runtime_get_sync(mcspi
->dev
);
1462 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1463 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1465 * We need to toggle CS state for OMAP take this
1466 * change in account.
1468 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1469 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1470 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1471 __raw_writel(cs
->chconf0
, cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1474 pm_runtime_mark_last_busy(mcspi
->dev
);
1475 pm_runtime_put_autosuspend(mcspi
->dev
);
1479 #define omap2_mcspi_resume NULL
1482 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1483 .resume
= omap2_mcspi_resume
,
1484 .runtime_resume
= omap_mcspi_runtime_resume
,
1487 static struct platform_driver omap2_mcspi_driver
= {
1489 .name
= "omap2_mcspi",
1490 .owner
= THIS_MODULE
,
1491 .pm
= &omap2_mcspi_pm_ops
,
1492 .of_match_table
= omap_mcspi_of_match
,
1494 .probe
= omap2_mcspi_probe
,
1495 .remove
= omap2_mcspi_remove
,
1498 module_platform_driver(omap2_mcspi_driver
);
1499 MODULE_LICENSE("GPL");