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
, 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 wcnt
= t
->len
/ bytes_per_word
;
319 if (wcnt
> OMAP2_MCSPI_MAX_FIFOWCNT
)
322 xferlevel
= wcnt
<< 16;
323 if (t
->rx_buf
!= NULL
) {
324 chconf
|= OMAP2_MCSPI_CHCONF_FFER
;
325 xferlevel
|= (bytes_per_word
- 1) << 8;
328 if (t
->tx_buf
!= NULL
) {
329 chconf
|= OMAP2_MCSPI_CHCONF_FFET
;
330 xferlevel
|= bytes_per_word
- 1;
333 mcspi_write_reg(master
, OMAP2_MCSPI_XFERLEVEL
, xferlevel
);
334 mcspi_write_chconf0(spi
, chconf
);
335 mcspi
->fifo_depth
= max_fifo_depth
;
341 if (t
->rx_buf
!= NULL
)
342 chconf
&= ~OMAP2_MCSPI_CHCONF_FFER
;
344 if (t
->tx_buf
!= NULL
)
345 chconf
&= ~OMAP2_MCSPI_CHCONF_FFET
;
347 mcspi_write_chconf0(spi
, chconf
);
348 mcspi
->fifo_depth
= 0;
351 static int mcspi_wait_for_reg_bit(void __iomem
*reg
, unsigned long bit
)
353 unsigned long timeout
;
355 timeout
= jiffies
+ msecs_to_jiffies(1000);
356 while (!(readl_relaxed(reg
) & bit
)) {
357 if (time_after(jiffies
, timeout
)) {
358 if (!(readl_relaxed(reg
) & bit
))
368 static void omap2_mcspi_rx_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 RX request */
375 omap2_mcspi_set_dma_req(spi
, 1, 0);
377 complete(&mcspi_dma
->dma_rx_completion
);
380 static void omap2_mcspi_tx_callback(void *data
)
382 struct spi_device
*spi
= data
;
383 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
384 struct omap2_mcspi_dma
*mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
386 /* We must disable the DMA TX request */
387 omap2_mcspi_set_dma_req(spi
, 0, 0);
389 complete(&mcspi_dma
->dma_tx_completion
);
392 static void omap2_mcspi_tx_dma(struct spi_device
*spi
,
393 struct spi_transfer
*xfer
,
394 struct dma_slave_config cfg
)
396 struct omap2_mcspi
*mcspi
;
397 struct omap2_mcspi_dma
*mcspi_dma
;
399 mcspi
= spi_master_get_devdata(spi
->master
);
400 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
402 if (mcspi_dma
->dma_tx
) {
403 struct dma_async_tx_descriptor
*tx
;
405 dmaengine_slave_config(mcspi_dma
->dma_tx
, &cfg
);
407 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_tx
, xfer
->tx_sg
.sgl
,
410 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
412 tx
->callback
= omap2_mcspi_tx_callback
;
413 tx
->callback_param
= spi
;
414 dmaengine_submit(tx
);
416 /* FIXME: fall back to PIO? */
419 dma_async_issue_pending(mcspi_dma
->dma_tx
);
420 omap2_mcspi_set_dma_req(spi
, 0, 1);
425 omap2_mcspi_rx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
,
426 struct dma_slave_config cfg
,
429 struct omap2_mcspi
*mcspi
;
430 struct omap2_mcspi_dma
*mcspi_dma
;
431 unsigned int count
, transfer_reduction
= 0;
432 struct scatterlist
*sg_out
[2];
433 int nb_sizes
= 0, out_mapped_nents
[2], ret
, x
;
437 int word_len
, element_count
;
438 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
439 void __iomem
*chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
441 mcspi
= spi_master_get_devdata(spi
->master
);
442 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
446 * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
447 * it mentions reducing DMA transfer length by one element in master
450 if (mcspi
->fifo_depth
== 0)
451 transfer_reduction
= es
;
453 word_len
= cs
->word_len
;
454 l
= mcspi_cached_chconf0(spi
);
457 element_count
= count
;
458 else if (word_len
<= 16)
459 element_count
= count
>> 1;
460 else /* word_len <= 32 */
461 element_count
= count
>> 2;
463 if (mcspi_dma
->dma_rx
) {
464 struct dma_async_tx_descriptor
*tx
;
466 dmaengine_slave_config(mcspi_dma
->dma_rx
, &cfg
);
469 * Reduce DMA transfer length by one more if McSPI is
470 * configured in turbo mode.
472 if ((l
& OMAP2_MCSPI_CHCONF_TURBO
) && mcspi
->fifo_depth
== 0)
473 transfer_reduction
+= es
;
475 if (transfer_reduction
) {
476 /* Split sgl into two. The second sgl won't be used. */
477 sizes
[0] = count
- transfer_reduction
;
478 sizes
[1] = transfer_reduction
;
482 * Don't bother splitting the sgl. This essentially
483 * clones the original sgl.
489 ret
= sg_split(xfer
->rx_sg
.sgl
, xfer
->rx_sg
.nents
,
492 sg_out
, out_mapped_nents
,
496 dev_err(&spi
->dev
, "sg_split failed\n");
500 tx
= dmaengine_prep_slave_sg(mcspi_dma
->dma_rx
,
504 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
506 tx
->callback
= omap2_mcspi_rx_callback
;
507 tx
->callback_param
= spi
;
508 dmaengine_submit(tx
);
510 /* FIXME: fall back to PIO? */
514 dma_async_issue_pending(mcspi_dma
->dma_rx
);
515 omap2_mcspi_set_dma_req(spi
, 1, 1);
517 wait_for_completion(&mcspi_dma
->dma_rx_completion
);
519 for (x
= 0; x
< nb_sizes
; x
++)
522 if (mcspi
->fifo_depth
> 0)
526 * Due to the DMA transfer length reduction the missing bytes must
527 * be read manually to receive all of the expected data.
529 omap2_mcspi_set_enable(spi
, 0);
531 elements
= element_count
- 1;
533 if (l
& OMAP2_MCSPI_CHCONF_TURBO
) {
536 if (!mcspi_wait_for_reg_bit(chstat_reg
,
537 OMAP2_MCSPI_CHSTAT_RXS
)) {
540 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
542 ((u8
*)xfer
->rx_buf
)[elements
++] = w
;
543 else if (word_len
<= 16)
544 ((u16
*)xfer
->rx_buf
)[elements
++] = w
;
545 else /* word_len <= 32 */
546 ((u32
*)xfer
->rx_buf
)[elements
++] = w
;
548 int bytes_per_word
= mcspi_bytes_per_word(word_len
);
549 dev_err(&spi
->dev
, "DMA RX penultimate word empty\n");
550 count
-= (bytes_per_word
<< 1);
551 omap2_mcspi_set_enable(spi
, 1);
555 if (!mcspi_wait_for_reg_bit(chstat_reg
, OMAP2_MCSPI_CHSTAT_RXS
)) {
558 w
= mcspi_read_cs_reg(spi
, OMAP2_MCSPI_RX0
);
560 ((u8
*)xfer
->rx_buf
)[elements
] = w
;
561 else if (word_len
<= 16)
562 ((u16
*)xfer
->rx_buf
)[elements
] = w
;
563 else /* word_len <= 32 */
564 ((u32
*)xfer
->rx_buf
)[elements
] = w
;
566 dev_err(&spi
->dev
, "DMA RX last word empty\n");
567 count
-= mcspi_bytes_per_word(word_len
);
569 omap2_mcspi_set_enable(spi
, 1);
574 omap2_mcspi_txrx_dma(struct spi_device
*spi
, struct spi_transfer
*xfer
)
576 struct omap2_mcspi
*mcspi
;
577 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
578 struct omap2_mcspi_dma
*mcspi_dma
;
582 struct dma_slave_config cfg
;
583 enum dma_slave_buswidth width
;
585 void __iomem
*chstat_reg
;
586 void __iomem
*irqstat_reg
;
589 mcspi
= spi_master_get_devdata(spi
->master
);
590 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
592 if (cs
->word_len
<= 8) {
593 width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
595 } else if (cs
->word_len
<= 16) {
596 width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
599 width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
605 memset(&cfg
, 0, sizeof(cfg
));
606 cfg
.src_addr
= cs
->phys
+ OMAP2_MCSPI_RX0
;
607 cfg
.dst_addr
= cs
->phys
+ OMAP2_MCSPI_TX0
;
608 cfg
.src_addr_width
= width
;
609 cfg
.dst_addr_width
= width
;
610 cfg
.src_maxburst
= 1;
611 cfg
.dst_maxburst
= 1;
617 omap2_mcspi_tx_dma(spi
, xfer
, cfg
);
620 count
= omap2_mcspi_rx_dma(spi
, xfer
, cfg
, es
);
623 wait_for_completion(&mcspi_dma
->dma_tx_completion
);
625 if (mcspi
->fifo_depth
> 0) {
626 irqstat_reg
= mcspi
->base
+ OMAP2_MCSPI_IRQSTATUS
;
628 if (mcspi_wait_for_reg_bit(irqstat_reg
,
629 OMAP2_MCSPI_IRQSTATUS_EOW
) < 0)
630 dev_err(&spi
->dev
, "EOW timed out\n");
632 mcspi_write_reg(mcspi
->master
, OMAP2_MCSPI_IRQSTATUS
,
633 OMAP2_MCSPI_IRQSTATUS_EOW
);
636 /* for TX_ONLY mode, be sure all words have shifted out */
638 chstat_reg
= cs
->base
+ OMAP2_MCSPI_CHSTAT0
;
639 if (mcspi
->fifo_depth
> 0) {
640 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
641 OMAP2_MCSPI_CHSTAT_TXFFE
);
643 dev_err(&spi
->dev
, "TXFFE timed out\n");
645 wait_res
= mcspi_wait_for_reg_bit(chstat_reg
,
646 OMAP2_MCSPI_CHSTAT_TXS
);
648 dev_err(&spi
->dev
, "TXS timed out\n");
651 (mcspi_wait_for_reg_bit(chstat_reg
,
652 OMAP2_MCSPI_CHSTAT_EOT
) < 0))
653 dev_err(&spi
->dev
, "EOT timed out\n");
660 omap2_mcspi_txrx_pio(struct spi_device
*spi
, struct spi_transfer
*xfer
)
662 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
663 unsigned int count
, c
;
665 void __iomem
*base
= cs
->base
;
666 void __iomem
*tx_reg
;
667 void __iomem
*rx_reg
;
668 void __iomem
*chstat_reg
;
673 word_len
= cs
->word_len
;
675 l
= mcspi_cached_chconf0(spi
);
677 /* We store the pre-calculated register addresses on stack to speed
678 * up the transfer loop. */
679 tx_reg
= base
+ OMAP2_MCSPI_TX0
;
680 rx_reg
= base
+ OMAP2_MCSPI_RX0
;
681 chstat_reg
= base
+ OMAP2_MCSPI_CHSTAT0
;
683 if (c
< (word_len
>>3))
696 if (mcspi_wait_for_reg_bit(chstat_reg
,
697 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
698 dev_err(&spi
->dev
, "TXS timed out\n");
701 dev_vdbg(&spi
->dev
, "write-%d %02x\n",
703 writel_relaxed(*tx
++, tx_reg
);
706 if (mcspi_wait_for_reg_bit(chstat_reg
,
707 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
708 dev_err(&spi
->dev
, "RXS timed out\n");
712 if (c
== 1 && tx
== NULL
&&
713 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
714 omap2_mcspi_set_enable(spi
, 0);
715 *rx
++ = readl_relaxed(rx_reg
);
716 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
717 word_len
, *(rx
- 1));
718 if (mcspi_wait_for_reg_bit(chstat_reg
,
719 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
725 } else if (c
== 0 && tx
== NULL
) {
726 omap2_mcspi_set_enable(spi
, 0);
729 *rx
++ = readl_relaxed(rx_reg
);
730 dev_vdbg(&spi
->dev
, "read-%d %02x\n",
731 word_len
, *(rx
- 1));
734 } else if (word_len
<= 16) {
743 if (mcspi_wait_for_reg_bit(chstat_reg
,
744 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
745 dev_err(&spi
->dev
, "TXS timed out\n");
748 dev_vdbg(&spi
->dev
, "write-%d %04x\n",
750 writel_relaxed(*tx
++, tx_reg
);
753 if (mcspi_wait_for_reg_bit(chstat_reg
,
754 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
755 dev_err(&spi
->dev
, "RXS timed out\n");
759 if (c
== 2 && tx
== NULL
&&
760 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
761 omap2_mcspi_set_enable(spi
, 0);
762 *rx
++ = readl_relaxed(rx_reg
);
763 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
764 word_len
, *(rx
- 1));
765 if (mcspi_wait_for_reg_bit(chstat_reg
,
766 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
772 } else if (c
== 0 && tx
== NULL
) {
773 omap2_mcspi_set_enable(spi
, 0);
776 *rx
++ = readl_relaxed(rx_reg
);
777 dev_vdbg(&spi
->dev
, "read-%d %04x\n",
778 word_len
, *(rx
- 1));
781 } else if (word_len
<= 32) {
790 if (mcspi_wait_for_reg_bit(chstat_reg
,
791 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
792 dev_err(&spi
->dev
, "TXS timed out\n");
795 dev_vdbg(&spi
->dev
, "write-%d %08x\n",
797 writel_relaxed(*tx
++, tx_reg
);
800 if (mcspi_wait_for_reg_bit(chstat_reg
,
801 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
802 dev_err(&spi
->dev
, "RXS timed out\n");
806 if (c
== 4 && tx
== NULL
&&
807 (l
& OMAP2_MCSPI_CHCONF_TURBO
)) {
808 omap2_mcspi_set_enable(spi
, 0);
809 *rx
++ = readl_relaxed(rx_reg
);
810 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
811 word_len
, *(rx
- 1));
812 if (mcspi_wait_for_reg_bit(chstat_reg
,
813 OMAP2_MCSPI_CHSTAT_RXS
) < 0) {
819 } else if (c
== 0 && tx
== NULL
) {
820 omap2_mcspi_set_enable(spi
, 0);
823 *rx
++ = readl_relaxed(rx_reg
);
824 dev_vdbg(&spi
->dev
, "read-%d %08x\n",
825 word_len
, *(rx
- 1));
830 /* for TX_ONLY mode, be sure all words have shifted out */
831 if (xfer
->rx_buf
== NULL
) {
832 if (mcspi_wait_for_reg_bit(chstat_reg
,
833 OMAP2_MCSPI_CHSTAT_TXS
) < 0) {
834 dev_err(&spi
->dev
, "TXS timed out\n");
835 } else if (mcspi_wait_for_reg_bit(chstat_reg
,
836 OMAP2_MCSPI_CHSTAT_EOT
) < 0)
837 dev_err(&spi
->dev
, "EOT timed out\n");
839 /* disable chan to purge rx datas received in TX_ONLY transfer,
840 * otherwise these rx datas will affect the direct following
843 omap2_mcspi_set_enable(spi
, 0);
846 omap2_mcspi_set_enable(spi
, 1);
850 static u32
omap2_mcspi_calc_divisor(u32 speed_hz
)
854 for (div
= 0; div
< 15; div
++)
855 if (speed_hz
>= (OMAP2_MCSPI_MAX_FREQ
>> div
))
861 /* called only when no transfer is active to this device */
862 static int omap2_mcspi_setup_transfer(struct spi_device
*spi
,
863 struct spi_transfer
*t
)
865 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
866 struct omap2_mcspi
*mcspi
;
867 u32 l
= 0, clkd
= 0, div
, extclk
= 0, clkg
= 0;
868 u8 word_len
= spi
->bits_per_word
;
869 u32 speed_hz
= spi
->max_speed_hz
;
871 mcspi
= spi_master_get_devdata(spi
->master
);
873 if (t
!= NULL
&& t
->bits_per_word
)
874 word_len
= t
->bits_per_word
;
876 cs
->word_len
= word_len
;
878 if (t
&& t
->speed_hz
)
879 speed_hz
= t
->speed_hz
;
881 speed_hz
= min_t(u32
, speed_hz
, OMAP2_MCSPI_MAX_FREQ
);
882 if (speed_hz
< (OMAP2_MCSPI_MAX_FREQ
/ OMAP2_MCSPI_MAX_DIVIDER
)) {
883 clkd
= omap2_mcspi_calc_divisor(speed_hz
);
884 speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> clkd
;
887 div
= (OMAP2_MCSPI_MAX_FREQ
+ speed_hz
- 1) / speed_hz
;
888 speed_hz
= OMAP2_MCSPI_MAX_FREQ
/ div
;
889 clkd
= (div
- 1) & 0xf;
890 extclk
= (div
- 1) >> 4;
891 clkg
= OMAP2_MCSPI_CHCONF_CLKG
;
894 l
= mcspi_cached_chconf0(spi
);
896 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
897 * REVISIT: this controller could support SPI_3WIRE mode.
899 if (mcspi
->pin_dir
== MCSPI_PINDIR_D0_IN_D1_OUT
) {
900 l
&= ~OMAP2_MCSPI_CHCONF_IS
;
901 l
&= ~OMAP2_MCSPI_CHCONF_DPE1
;
902 l
|= OMAP2_MCSPI_CHCONF_DPE0
;
904 l
|= OMAP2_MCSPI_CHCONF_IS
;
905 l
|= OMAP2_MCSPI_CHCONF_DPE1
;
906 l
&= ~OMAP2_MCSPI_CHCONF_DPE0
;
910 l
&= ~OMAP2_MCSPI_CHCONF_WL_MASK
;
911 l
|= (word_len
- 1) << 7;
913 /* set chipselect polarity; manage with FORCE */
914 if (!(spi
->mode
& SPI_CS_HIGH
))
915 l
|= OMAP2_MCSPI_CHCONF_EPOL
; /* active-low; normal */
917 l
&= ~OMAP2_MCSPI_CHCONF_EPOL
;
919 /* set clock divisor */
920 l
&= ~OMAP2_MCSPI_CHCONF_CLKD_MASK
;
923 /* set clock granularity */
924 l
&= ~OMAP2_MCSPI_CHCONF_CLKG
;
927 cs
->chctrl0
&= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK
;
928 cs
->chctrl0
|= extclk
<< 8;
929 mcspi_write_cs_reg(spi
, OMAP2_MCSPI_CHCTRL0
, cs
->chctrl0
);
932 /* set SPI mode 0..3 */
933 if (spi
->mode
& SPI_CPOL
)
934 l
|= OMAP2_MCSPI_CHCONF_POL
;
936 l
&= ~OMAP2_MCSPI_CHCONF_POL
;
937 if (spi
->mode
& SPI_CPHA
)
938 l
|= OMAP2_MCSPI_CHCONF_PHA
;
940 l
&= ~OMAP2_MCSPI_CHCONF_PHA
;
942 mcspi_write_chconf0(spi
, l
);
944 cs
->mode
= spi
->mode
;
946 dev_dbg(&spi
->dev
, "setup: speed %d, sample %s edge, clk %s\n",
948 (spi
->mode
& SPI_CPHA
) ? "trailing" : "leading",
949 (spi
->mode
& SPI_CPOL
) ? "inverted" : "normal");
955 * Note that we currently allow DMA only if we get a channel
956 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
958 static int omap2_mcspi_request_dma(struct spi_device
*spi
)
960 struct spi_master
*master
= spi
->master
;
961 struct omap2_mcspi
*mcspi
;
962 struct omap2_mcspi_dma
*mcspi_dma
;
965 mcspi
= spi_master_get_devdata(master
);
966 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
968 init_completion(&mcspi_dma
->dma_rx_completion
);
969 init_completion(&mcspi_dma
->dma_tx_completion
);
971 mcspi_dma
->dma_rx
= dma_request_chan(&master
->dev
,
972 mcspi_dma
->dma_rx_ch_name
);
973 if (IS_ERR(mcspi_dma
->dma_rx
)) {
974 ret
= PTR_ERR(mcspi_dma
->dma_rx
);
975 mcspi_dma
->dma_rx
= NULL
;
979 mcspi_dma
->dma_tx
= dma_request_chan(&master
->dev
,
980 mcspi_dma
->dma_tx_ch_name
);
981 if (IS_ERR(mcspi_dma
->dma_tx
)) {
982 ret
= PTR_ERR(mcspi_dma
->dma_tx
);
983 mcspi_dma
->dma_tx
= NULL
;
984 dma_release_channel(mcspi_dma
->dma_rx
);
985 mcspi_dma
->dma_rx
= NULL
;
992 static int omap2_mcspi_setup(struct spi_device
*spi
)
995 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(spi
->master
);
996 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
997 struct omap2_mcspi_dma
*mcspi_dma
;
998 struct omap2_mcspi_cs
*cs
= spi
->controller_state
;
1000 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1003 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
1006 cs
->base
= mcspi
->base
+ spi
->chip_select
* 0x14;
1007 cs
->phys
= mcspi
->phys
+ spi
->chip_select
* 0x14;
1011 spi
->controller_state
= cs
;
1012 /* Link this to context save list */
1013 list_add_tail(&cs
->node
, &ctx
->cs
);
1015 if (gpio_is_valid(spi
->cs_gpio
)) {
1016 ret
= gpio_request(spi
->cs_gpio
, dev_name(&spi
->dev
));
1018 dev_err(&spi
->dev
, "failed to request gpio\n");
1021 gpio_direction_output(spi
->cs_gpio
,
1022 !(spi
->mode
& SPI_CS_HIGH
));
1026 if (!mcspi_dma
->dma_rx
|| !mcspi_dma
->dma_tx
) {
1027 ret
= omap2_mcspi_request_dma(spi
);
1029 dev_warn(&spi
->dev
, "not using DMA for McSPI (%d)\n",
1033 ret
= pm_runtime_get_sync(mcspi
->dev
);
1035 pm_runtime_put_noidle(mcspi
->dev
);
1040 ret
= omap2_mcspi_setup_transfer(spi
, NULL
);
1041 pm_runtime_mark_last_busy(mcspi
->dev
);
1042 pm_runtime_put_autosuspend(mcspi
->dev
);
1047 static void omap2_mcspi_cleanup(struct spi_device
*spi
)
1049 struct omap2_mcspi
*mcspi
;
1050 struct omap2_mcspi_dma
*mcspi_dma
;
1051 struct omap2_mcspi_cs
*cs
;
1053 mcspi
= spi_master_get_devdata(spi
->master
);
1055 if (spi
->controller_state
) {
1056 /* Unlink controller state from context save list */
1057 cs
= spi
->controller_state
;
1058 list_del(&cs
->node
);
1063 if (spi
->chip_select
< spi
->master
->num_chipselect
) {
1064 mcspi_dma
= &mcspi
->dma_channels
[spi
->chip_select
];
1066 if (mcspi_dma
->dma_rx
) {
1067 dma_release_channel(mcspi_dma
->dma_rx
);
1068 mcspi_dma
->dma_rx
= NULL
;
1070 if (mcspi_dma
->dma_tx
) {
1071 dma_release_channel(mcspi_dma
->dma_tx
);
1072 mcspi_dma
->dma_tx
= NULL
;
1076 if (gpio_is_valid(spi
->cs_gpio
))
1077 gpio_free(spi
->cs_gpio
);
1080 static int omap2_mcspi_transfer_one(struct spi_master
*master
,
1081 struct spi_device
*spi
,
1082 struct spi_transfer
*t
)
1085 /* We only enable one channel at a time -- the one whose message is
1086 * -- although this controller would gladly
1087 * arbitrate among multiple channels. This corresponds to "single
1088 * channel" master mode. As a side effect, we need to manage the
1089 * chipselect with the FORCE bit ... CS != channel enable.
1092 struct omap2_mcspi
*mcspi
;
1093 struct omap2_mcspi_dma
*mcspi_dma
;
1094 struct omap2_mcspi_cs
*cs
;
1095 struct omap2_mcspi_device_config
*cd
;
1096 int par_override
= 0;
1100 mcspi
= spi_master_get_devdata(master
);
1101 mcspi_dma
= mcspi
->dma_channels
+ spi
->chip_select
;
1102 cs
= spi
->controller_state
;
1103 cd
= spi
->controller_data
;
1106 * The slave driver could have changed spi->mode in which case
1107 * it will be different from cs->mode (the current hardware setup).
1108 * If so, set par_override (even though its not a parity issue) so
1109 * omap2_mcspi_setup_transfer will be called to configure the hardware
1110 * with the correct mode on the first iteration of the loop below.
1112 if (spi
->mode
!= cs
->mode
)
1115 omap2_mcspi_set_enable(spi
, 0);
1117 if (gpio_is_valid(spi
->cs_gpio
))
1118 omap2_mcspi_set_cs(spi
, spi
->mode
& SPI_CS_HIGH
);
1121 (t
->speed_hz
!= spi
->max_speed_hz
) ||
1122 (t
->bits_per_word
!= spi
->bits_per_word
)) {
1124 status
= omap2_mcspi_setup_transfer(spi
, t
);
1127 if (t
->speed_hz
== spi
->max_speed_hz
&&
1128 t
->bits_per_word
== spi
->bits_per_word
)
1131 if (cd
&& cd
->cs_per_word
) {
1132 chconf
= mcspi
->ctx
.modulctrl
;
1133 chconf
&= ~OMAP2_MCSPI_MODULCTRL_SINGLE
;
1134 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1135 mcspi
->ctx
.modulctrl
=
1136 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1139 chconf
= mcspi_cached_chconf0(spi
);
1140 chconf
&= ~OMAP2_MCSPI_CHCONF_TRM_MASK
;
1141 chconf
&= ~OMAP2_MCSPI_CHCONF_TURBO
;
1143 if (t
->tx_buf
== NULL
)
1144 chconf
|= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY
;
1145 else if (t
->rx_buf
== NULL
)
1146 chconf
|= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY
;
1148 if (cd
&& cd
->turbo_mode
&& t
->tx_buf
== NULL
) {
1149 /* Turbo mode is for more than one word */
1150 if (t
->len
> ((cs
->word_len
+ 7) >> 3))
1151 chconf
|= OMAP2_MCSPI_CHCONF_TURBO
;
1154 mcspi_write_chconf0(spi
, chconf
);
1159 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1160 master
->cur_msg_mapped
&&
1161 master
->can_dma(master
, spi
, t
))
1162 omap2_mcspi_set_fifo(spi
, t
, 1);
1164 omap2_mcspi_set_enable(spi
, 1);
1166 /* RX_ONLY mode needs dummy data in TX reg */
1167 if (t
->tx_buf
== NULL
)
1168 writel_relaxed(0, cs
->base
1171 if ((mcspi_dma
->dma_rx
&& mcspi_dma
->dma_tx
) &&
1172 master
->cur_msg_mapped
&&
1173 master
->can_dma(master
, spi
, t
))
1174 count
= omap2_mcspi_txrx_dma(spi
, t
);
1176 count
= omap2_mcspi_txrx_pio(spi
, t
);
1178 if (count
!= t
->len
) {
1184 omap2_mcspi_set_enable(spi
, 0);
1186 if (mcspi
->fifo_depth
> 0)
1187 omap2_mcspi_set_fifo(spi
, t
, 0);
1190 /* Restore defaults if they were overriden */
1193 status
= omap2_mcspi_setup_transfer(spi
, NULL
);
1196 if (cd
&& cd
->cs_per_word
) {
1197 chconf
= mcspi
->ctx
.modulctrl
;
1198 chconf
|= OMAP2_MCSPI_MODULCTRL_SINGLE
;
1199 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, chconf
);
1200 mcspi
->ctx
.modulctrl
=
1201 mcspi_read_cs_reg(spi
, OMAP2_MCSPI_MODULCTRL
);
1204 omap2_mcspi_set_enable(spi
, 0);
1206 if (gpio_is_valid(spi
->cs_gpio
))
1207 omap2_mcspi_set_cs(spi
, !(spi
->mode
& SPI_CS_HIGH
));
1209 if (mcspi
->fifo_depth
> 0 && t
)
1210 omap2_mcspi_set_fifo(spi
, t
, 0);
1215 static int omap2_mcspi_prepare_message(struct spi_master
*master
,
1216 struct spi_message
*msg
)
1218 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1219 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1220 struct omap2_mcspi_cs
*cs
;
1222 /* Only a single channel can have the FORCE bit enabled
1223 * in its chconf0 register.
1224 * Scan all channels and disable them except the current one.
1225 * A FORCE can remain from a last transfer having cs_change enabled
1227 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1228 if (msg
->spi
->controller_state
== cs
)
1231 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
)) {
1232 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1233 writel_relaxed(cs
->chconf0
,
1234 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1235 readl_relaxed(cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1242 static bool omap2_mcspi_can_dma(struct spi_master
*master
,
1243 struct spi_device
*spi
,
1244 struct spi_transfer
*xfer
)
1246 return (xfer
->len
>= DMA_MIN_BYTES
);
1249 static int omap2_mcspi_master_setup(struct omap2_mcspi
*mcspi
)
1251 struct spi_master
*master
= mcspi
->master
;
1252 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1255 ret
= pm_runtime_get_sync(mcspi
->dev
);
1257 pm_runtime_put_noidle(mcspi
->dev
);
1262 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
,
1263 OMAP2_MCSPI_WAKEUPENABLE_WKEN
);
1264 ctx
->wakeupenable
= OMAP2_MCSPI_WAKEUPENABLE_WKEN
;
1266 omap2_mcspi_set_master_mode(master
);
1267 pm_runtime_mark_last_busy(mcspi
->dev
);
1268 pm_runtime_put_autosuspend(mcspi
->dev
);
1273 * When SPI wake up from off-mode, CS is in activate state. If it was in
1274 * inactive state when driver was suspend, then force it to inactive state at
1277 static int omap_mcspi_runtime_resume(struct device
*dev
)
1279 struct spi_master
*master
= dev_get_drvdata(dev
);
1280 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1281 struct omap2_mcspi_regs
*ctx
= &mcspi
->ctx
;
1282 struct omap2_mcspi_cs
*cs
;
1284 /* McSPI: context restore */
1285 mcspi_write_reg(master
, OMAP2_MCSPI_MODULCTRL
, ctx
->modulctrl
);
1286 mcspi_write_reg(master
, OMAP2_MCSPI_WAKEUPENABLE
, ctx
->wakeupenable
);
1288 list_for_each_entry(cs
, &ctx
->cs
, node
) {
1290 * We need to toggle CS state for OMAP take this
1291 * change in account.
1293 if ((cs
->chconf0
& OMAP2_MCSPI_CHCONF_FORCE
) == 0) {
1294 cs
->chconf0
|= OMAP2_MCSPI_CHCONF_FORCE
;
1295 writel_relaxed(cs
->chconf0
,
1296 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1297 cs
->chconf0
&= ~OMAP2_MCSPI_CHCONF_FORCE
;
1298 writel_relaxed(cs
->chconf0
,
1299 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1301 writel_relaxed(cs
->chconf0
,
1302 cs
->base
+ OMAP2_MCSPI_CHCONF0
);
1309 static struct omap2_mcspi_platform_config omap2_pdata
= {
1313 static struct omap2_mcspi_platform_config omap4_pdata
= {
1314 .regs_offset
= OMAP4_MCSPI_REG_OFFSET
,
1317 static const struct of_device_id omap_mcspi_of_match
[] = {
1319 .compatible
= "ti,omap2-mcspi",
1320 .data
= &omap2_pdata
,
1323 .compatible
= "ti,omap4-mcspi",
1324 .data
= &omap4_pdata
,
1328 MODULE_DEVICE_TABLE(of
, omap_mcspi_of_match
);
1330 static int omap2_mcspi_probe(struct platform_device
*pdev
)
1332 struct spi_master
*master
;
1333 const struct omap2_mcspi_platform_config
*pdata
;
1334 struct omap2_mcspi
*mcspi
;
1337 u32 regs_offset
= 0;
1338 struct device_node
*node
= pdev
->dev
.of_node
;
1339 const struct of_device_id
*match
;
1341 master
= spi_alloc_master(&pdev
->dev
, sizeof *mcspi
);
1342 if (master
== NULL
) {
1343 dev_dbg(&pdev
->dev
, "master allocation failed\n");
1347 /* the spi->mode bits understood by this driver: */
1348 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1349 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1350 master
->setup
= omap2_mcspi_setup
;
1351 master
->auto_runtime_pm
= true;
1352 master
->prepare_message
= omap2_mcspi_prepare_message
;
1353 master
->can_dma
= omap2_mcspi_can_dma
;
1354 master
->transfer_one
= omap2_mcspi_transfer_one
;
1355 master
->set_cs
= omap2_mcspi_set_cs
;
1356 master
->cleanup
= omap2_mcspi_cleanup
;
1357 master
->dev
.of_node
= node
;
1358 master
->max_speed_hz
= OMAP2_MCSPI_MAX_FREQ
;
1359 master
->min_speed_hz
= OMAP2_MCSPI_MAX_FREQ
>> 15;
1361 platform_set_drvdata(pdev
, master
);
1363 mcspi
= spi_master_get_devdata(master
);
1364 mcspi
->master
= master
;
1366 match
= of_match_device(omap_mcspi_of_match
, &pdev
->dev
);
1368 u32 num_cs
= 1; /* default number of chipselect */
1369 pdata
= match
->data
;
1371 of_property_read_u32(node
, "ti,spi-num-cs", &num_cs
);
1372 master
->num_chipselect
= num_cs
;
1373 if (of_get_property(node
, "ti,pindir-d0-out-d1-in", NULL
))
1374 mcspi
->pin_dir
= MCSPI_PINDIR_D0_OUT_D1_IN
;
1376 pdata
= dev_get_platdata(&pdev
->dev
);
1377 master
->num_chipselect
= pdata
->num_cs
;
1378 mcspi
->pin_dir
= pdata
->pin_dir
;
1380 regs_offset
= pdata
->regs_offset
;
1382 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1383 mcspi
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1384 if (IS_ERR(mcspi
->base
)) {
1385 status
= PTR_ERR(mcspi
->base
);
1388 mcspi
->phys
= r
->start
+ regs_offset
;
1389 mcspi
->base
+= regs_offset
;
1391 mcspi
->dev
= &pdev
->dev
;
1393 INIT_LIST_HEAD(&mcspi
->ctx
.cs
);
1395 mcspi
->dma_channels
= devm_kcalloc(&pdev
->dev
, master
->num_chipselect
,
1396 sizeof(struct omap2_mcspi_dma
),
1398 if (mcspi
->dma_channels
== NULL
) {
1403 for (i
= 0; i
< master
->num_chipselect
; i
++) {
1404 sprintf(mcspi
->dma_channels
[i
].dma_rx_ch_name
, "rx%d", i
);
1405 sprintf(mcspi
->dma_channels
[i
].dma_tx_ch_name
, "tx%d", i
);
1408 pm_runtime_use_autosuspend(&pdev
->dev
);
1409 pm_runtime_set_autosuspend_delay(&pdev
->dev
, SPI_AUTOSUSPEND_TIMEOUT
);
1410 pm_runtime_enable(&pdev
->dev
);
1412 status
= omap2_mcspi_master_setup(mcspi
);
1416 status
= devm_spi_register_master(&pdev
->dev
, master
);
1423 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1424 pm_runtime_put_sync(&pdev
->dev
);
1425 pm_runtime_disable(&pdev
->dev
);
1427 spi_master_put(master
);
1431 static int omap2_mcspi_remove(struct platform_device
*pdev
)
1433 struct spi_master
*master
= platform_get_drvdata(pdev
);
1434 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1436 pm_runtime_dont_use_autosuspend(mcspi
->dev
);
1437 pm_runtime_put_sync(mcspi
->dev
);
1438 pm_runtime_disable(&pdev
->dev
);
1443 /* work with hotplug and coldplug */
1444 MODULE_ALIAS("platform:omap2_mcspi");
1446 static int __maybe_unused
omap2_mcspi_suspend(struct device
*dev
)
1448 struct spi_master
*master
= dev_get_drvdata(dev
);
1449 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1452 error
= pinctrl_pm_select_sleep_state(dev
);
1454 dev_warn(mcspi
->dev
, "%s: failed to set pins: %i\n",
1457 error
= spi_master_suspend(master
);
1459 dev_warn(mcspi
->dev
, "%s: master suspend failed: %i\n",
1462 return pm_runtime_force_suspend(dev
);
1465 static int __maybe_unused
omap2_mcspi_resume(struct device
*dev
)
1467 struct spi_master
*master
= dev_get_drvdata(dev
);
1468 struct omap2_mcspi
*mcspi
= spi_master_get_devdata(master
);
1471 error
= pinctrl_pm_select_default_state(dev
);
1473 dev_warn(mcspi
->dev
, "%s: failed to set pins: %i\n",
1476 error
= spi_master_resume(master
);
1478 dev_warn(mcspi
->dev
, "%s: master resume failed: %i\n",
1481 return pm_runtime_force_resume(dev
);
1484 static const struct dev_pm_ops omap2_mcspi_pm_ops
= {
1485 SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend
,
1487 .runtime_resume
= omap_mcspi_runtime_resume
,
1490 static struct platform_driver omap2_mcspi_driver
= {
1492 .name
= "omap2_mcspi",
1493 .pm
= &omap2_mcspi_pm_ops
,
1494 .of_match_table
= omap_mcspi_of_match
,
1496 .probe
= omap2_mcspi_probe
,
1497 .remove
= omap2_mcspi_remove
,
1500 module_platform_driver(omap2_mcspi_driver
);
1501 MODULE_LICENSE("GPL");