1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2019 Spreadtrum Communications Inc.
4 #include <linux/errno.h>
5 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
11 #include <linux/platform_device.h>
12 #include <linux/spinlock.h>
14 #include "sprd-mcdt.h"
16 /* MCDT registers definition */
17 #define MCDT_CH0_TXD 0x0
18 #define MCDT_CH0_RXD 0x28
19 #define MCDT_DAC0_WTMK 0x60
20 #define MCDT_ADC0_WTMK 0x88
21 #define MCDT_DMA_EN 0xb0
23 #define MCDT_INT_EN0 0xb4
24 #define MCDT_INT_EN1 0xb8
25 #define MCDT_INT_EN2 0xbc
27 #define MCDT_INT_CLR0 0xc0
28 #define MCDT_INT_CLR1 0xc4
29 #define MCDT_INT_CLR2 0xc8
31 #define MCDT_INT_RAW1 0xcc
32 #define MCDT_INT_RAW2 0xd0
33 #define MCDT_INT_RAW3 0xd4
35 #define MCDT_INT_MSK1 0xd8
36 #define MCDT_INT_MSK2 0xdc
37 #define MCDT_INT_MSK3 0xe0
39 #define MCDT_DAC0_FIFO_ADDR_ST 0xe4
40 #define MCDT_ADC0_FIFO_ADDR_ST 0xe8
42 #define MCDT_CH_FIFO_ST0 0x134
43 #define MCDT_CH_FIFO_ST1 0x138
44 #define MCDT_CH_FIFO_ST2 0x13c
46 #define MCDT_INT_MSK_CFG0 0x140
47 #define MCDT_INT_MSK_CFG1 0x144
49 #define MCDT_DMA_CFG0 0x148
50 #define MCDT_FIFO_CLR 0x14c
51 #define MCDT_DMA_CFG1 0x150
52 #define MCDT_DMA_CFG2 0x154
53 #define MCDT_DMA_CFG3 0x158
54 #define MCDT_DMA_CFG4 0x15c
55 #define MCDT_DMA_CFG5 0x160
57 /* Channel water mark definition */
58 #define MCDT_CH_FIFO_AE_SHIFT 16
59 #define MCDT_CH_FIFO_AE_MASK GENMASK(24, 16)
60 #define MCDT_CH_FIFO_AF_MASK GENMASK(8, 0)
62 /* DMA channel select definition */
63 #define MCDT_DMA_CH0_SEL_MASK GENMASK(3, 0)
64 #define MCDT_DMA_CH0_SEL_SHIFT 0
65 #define MCDT_DMA_CH1_SEL_MASK GENMASK(7, 4)
66 #define MCDT_DMA_CH1_SEL_SHIFT 4
67 #define MCDT_DMA_CH2_SEL_MASK GENMASK(11, 8)
68 #define MCDT_DMA_CH2_SEL_SHIFT 8
69 #define MCDT_DMA_CH3_SEL_MASK GENMASK(15, 12)
70 #define MCDT_DMA_CH3_SEL_SHIFT 12
71 #define MCDT_DMA_CH4_SEL_MASK GENMASK(19, 16)
72 #define MCDT_DMA_CH4_SEL_SHIFT 16
73 #define MCDT_DAC_DMA_SHIFT 16
75 /* DMA channel ACK select definition */
76 #define MCDT_DMA_ACK_SEL_MASK GENMASK(3, 0)
78 /* Channel FIFO definition */
79 #define MCDT_CH_FIFO_ADDR_SHIFT 16
80 #define MCDT_CH_FIFO_ADDR_MASK GENMASK(9, 0)
81 #define MCDT_ADC_FIFO_SHIFT 16
82 #define MCDT_FIFO_LENGTH 512
84 #define MCDT_ADC_CHANNEL_NUM 10
85 #define MCDT_DAC_CHANNEL_NUM 10
86 #define MCDT_CHANNEL_NUM (MCDT_ADC_CHANNEL_NUM + MCDT_DAC_CHANNEL_NUM)
88 enum sprd_mcdt_fifo_int
{
97 enum sprd_mcdt_fifo_sts
{
98 MCDT_ADC_FIFO_REAL_FULL
,
99 MCDT_ADC_FIFO_REAL_EMPTY
,
102 MCDT_DAC_FIFO_REAL_FULL
,
103 MCDT_DAC_FIFO_REAL_EMPTY
,
108 struct sprd_mcdt_dev
{
112 struct sprd_mcdt_chan chan
[MCDT_CHANNEL_NUM
];
115 static LIST_HEAD(sprd_mcdt_chan_list
);
116 static DEFINE_MUTEX(sprd_mcdt_list_mutex
);
118 static void sprd_mcdt_update(struct sprd_mcdt_dev
*mcdt
, u32 reg
, u32 val
,
121 u32 orig
= readl_relaxed(mcdt
->base
+ reg
);
124 tmp
= (orig
& ~mask
) | val
;
125 writel_relaxed(tmp
, mcdt
->base
+ reg
);
128 static void sprd_mcdt_dac_set_watermark(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
131 u32 reg
= MCDT_DAC0_WTMK
+ channel
* 4;
133 (empty
<< MCDT_CH_FIFO_AE_SHIFT
) & MCDT_CH_FIFO_AE_MASK
;
135 water_mark
|= full
& MCDT_CH_FIFO_AF_MASK
;
136 sprd_mcdt_update(mcdt
, reg
, water_mark
,
137 MCDT_CH_FIFO_AE_MASK
| MCDT_CH_FIFO_AF_MASK
);
140 static void sprd_mcdt_adc_set_watermark(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
143 u32 reg
= MCDT_ADC0_WTMK
+ channel
* 4;
145 (empty
<< MCDT_CH_FIFO_AE_SHIFT
) & MCDT_CH_FIFO_AE_MASK
;
147 water_mark
|= full
& MCDT_CH_FIFO_AF_MASK
;
148 sprd_mcdt_update(mcdt
, reg
, water_mark
,
149 MCDT_CH_FIFO_AE_MASK
| MCDT_CH_FIFO_AF_MASK
);
152 static void sprd_mcdt_dac_dma_enable(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
155 u32 shift
= MCDT_DAC_DMA_SHIFT
+ channel
;
158 sprd_mcdt_update(mcdt
, MCDT_DMA_EN
, BIT(shift
), BIT(shift
));
160 sprd_mcdt_update(mcdt
, MCDT_DMA_EN
, 0, BIT(shift
));
163 static void sprd_mcdt_adc_dma_enable(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
167 sprd_mcdt_update(mcdt
, MCDT_DMA_EN
, BIT(channel
), BIT(channel
));
169 sprd_mcdt_update(mcdt
, MCDT_DMA_EN
, 0, BIT(channel
));
172 static void sprd_mcdt_ap_int_enable(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
176 sprd_mcdt_update(mcdt
, MCDT_INT_MSK_CFG0
, BIT(channel
),
179 sprd_mcdt_update(mcdt
, MCDT_INT_MSK_CFG0
, 0, BIT(channel
));
182 static void sprd_mcdt_dac_write_fifo(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
185 u32 reg
= MCDT_CH0_TXD
+ channel
* 4;
187 writel_relaxed(val
, mcdt
->base
+ reg
);
190 static void sprd_mcdt_adc_read_fifo(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
193 u32 reg
= MCDT_CH0_RXD
+ channel
* 4;
195 *val
= readl_relaxed(mcdt
->base
+ reg
);
198 static void sprd_mcdt_dac_dma_chn_select(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
199 enum sprd_mcdt_dma_chan dma_chan
)
202 case SPRD_MCDT_DMA_CH0
:
203 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG0
,
204 channel
<< MCDT_DMA_CH0_SEL_SHIFT
,
205 MCDT_DMA_CH0_SEL_MASK
);
208 case SPRD_MCDT_DMA_CH1
:
209 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG0
,
210 channel
<< MCDT_DMA_CH1_SEL_SHIFT
,
211 MCDT_DMA_CH1_SEL_MASK
);
214 case SPRD_MCDT_DMA_CH2
:
215 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG0
,
216 channel
<< MCDT_DMA_CH2_SEL_SHIFT
,
217 MCDT_DMA_CH2_SEL_MASK
);
220 case SPRD_MCDT_DMA_CH3
:
221 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG0
,
222 channel
<< MCDT_DMA_CH3_SEL_SHIFT
,
223 MCDT_DMA_CH3_SEL_MASK
);
226 case SPRD_MCDT_DMA_CH4
:
227 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG0
,
228 channel
<< MCDT_DMA_CH4_SEL_SHIFT
,
229 MCDT_DMA_CH4_SEL_MASK
);
234 static void sprd_mcdt_adc_dma_chn_select(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
235 enum sprd_mcdt_dma_chan dma_chan
)
238 case SPRD_MCDT_DMA_CH0
:
239 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG1
,
240 channel
<< MCDT_DMA_CH0_SEL_SHIFT
,
241 MCDT_DMA_CH0_SEL_MASK
);
244 case SPRD_MCDT_DMA_CH1
:
245 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG1
,
246 channel
<< MCDT_DMA_CH1_SEL_SHIFT
,
247 MCDT_DMA_CH1_SEL_MASK
);
250 case SPRD_MCDT_DMA_CH2
:
251 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG1
,
252 channel
<< MCDT_DMA_CH2_SEL_SHIFT
,
253 MCDT_DMA_CH2_SEL_MASK
);
256 case SPRD_MCDT_DMA_CH3
:
257 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG1
,
258 channel
<< MCDT_DMA_CH3_SEL_SHIFT
,
259 MCDT_DMA_CH3_SEL_MASK
);
262 case SPRD_MCDT_DMA_CH4
:
263 sprd_mcdt_update(mcdt
, MCDT_DMA_CFG1
,
264 channel
<< MCDT_DMA_CH4_SEL_SHIFT
,
265 MCDT_DMA_CH4_SEL_MASK
);
270 static u32
sprd_mcdt_dma_ack_shift(u8 channel
)
295 static void sprd_mcdt_dac_dma_ack_select(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
296 enum sprd_mcdt_dma_chan dma_chan
)
298 u32 reg
, shift
= sprd_mcdt_dma_ack_shift(channel
), ack
= dma_chan
;
313 sprd_mcdt_update(mcdt
, reg
, ack
<< shift
,
314 MCDT_DMA_ACK_SEL_MASK
<< shift
);
317 static void sprd_mcdt_adc_dma_ack_select(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
318 enum sprd_mcdt_dma_chan dma_chan
)
320 u32 reg
, shift
= sprd_mcdt_dma_ack_shift(channel
), ack
= dma_chan
;
335 sprd_mcdt_update(mcdt
, reg
, ack
<< shift
,
336 MCDT_DMA_ACK_SEL_MASK
<< shift
);
339 static bool sprd_mcdt_chan_fifo_sts(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
340 enum sprd_mcdt_fifo_sts fifo_sts
)
346 reg
= MCDT_CH_FIFO_ST0
;
349 reg
= MCDT_CH_FIFO_ST1
;
352 reg
= MCDT_CH_FIFO_ST2
;
368 shift
= 8 + fifo_sts
;
373 shift
= 16 + fifo_sts
;
378 shift
= 24 + fifo_sts
;
385 return !!(readl_relaxed(mcdt
->base
+ reg
) & BIT(shift
));
388 static void sprd_mcdt_dac_fifo_clear(struct sprd_mcdt_dev
*mcdt
, u8 channel
)
390 sprd_mcdt_update(mcdt
, MCDT_FIFO_CLR
, BIT(channel
), BIT(channel
));
393 static void sprd_mcdt_adc_fifo_clear(struct sprd_mcdt_dev
*mcdt
, u8 channel
)
395 u32 shift
= MCDT_ADC_FIFO_SHIFT
+ channel
;
397 sprd_mcdt_update(mcdt
, MCDT_FIFO_CLR
, BIT(shift
), BIT(shift
));
400 static u32
sprd_mcdt_dac_fifo_avail(struct sprd_mcdt_dev
*mcdt
, u8 channel
)
402 u32 reg
= MCDT_DAC0_FIFO_ADDR_ST
+ channel
* 8;
403 u32 r_addr
= (readl_relaxed(mcdt
->base
+ reg
) >>
404 MCDT_CH_FIFO_ADDR_SHIFT
) & MCDT_CH_FIFO_ADDR_MASK
;
405 u32 w_addr
= readl_relaxed(mcdt
->base
+ reg
) & MCDT_CH_FIFO_ADDR_MASK
;
407 if (w_addr
>= r_addr
)
408 return 4 * (MCDT_FIFO_LENGTH
- w_addr
+ r_addr
);
410 return 4 * (r_addr
- w_addr
);
413 static u32
sprd_mcdt_adc_fifo_avail(struct sprd_mcdt_dev
*mcdt
, u8 channel
)
415 u32 reg
= MCDT_ADC0_FIFO_ADDR_ST
+ channel
* 8;
416 u32 r_addr
= (readl_relaxed(mcdt
->base
+ reg
) >>
417 MCDT_CH_FIFO_ADDR_SHIFT
) & MCDT_CH_FIFO_ADDR_MASK
;
418 u32 w_addr
= readl_relaxed(mcdt
->base
+ reg
) & MCDT_CH_FIFO_ADDR_MASK
;
420 if (w_addr
>= r_addr
)
421 return 4 * (w_addr
- r_addr
);
423 return 4 * (MCDT_FIFO_LENGTH
- r_addr
+ w_addr
);
426 static u32
sprd_mcdt_int_type_shift(u8 channel
,
427 enum sprd_mcdt_fifo_int int_type
)
442 return 16 + int_type
;
446 return 24 + int_type
;
453 static void sprd_mcdt_chan_int_en(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
454 enum sprd_mcdt_fifo_int int_type
, bool enable
)
456 u32 reg
, shift
= sprd_mcdt_int_type_shift(channel
, int_type
);
473 sprd_mcdt_update(mcdt
, reg
, BIT(shift
), BIT(shift
));
475 sprd_mcdt_update(mcdt
, reg
, 0, BIT(shift
));
478 static void sprd_mcdt_chan_int_clear(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
479 enum sprd_mcdt_fifo_int int_type
)
481 u32 reg
, shift
= sprd_mcdt_int_type_shift(channel
, int_type
);
497 sprd_mcdt_update(mcdt
, reg
, BIT(shift
), BIT(shift
));
500 static bool sprd_mcdt_chan_int_sts(struct sprd_mcdt_dev
*mcdt
, u8 channel
,
501 enum sprd_mcdt_fifo_int int_type
)
503 u32 reg
, shift
= sprd_mcdt_int_type_shift(channel
, int_type
);
519 return !!(readl_relaxed(mcdt
->base
+ reg
) & BIT(shift
));
522 static irqreturn_t
sprd_mcdt_irq_handler(int irq
, void *dev_id
)
524 struct sprd_mcdt_dev
*mcdt
= (struct sprd_mcdt_dev
*)dev_id
;
527 spin_lock(&mcdt
->lock
);
529 for (i
= 0; i
< MCDT_ADC_CHANNEL_NUM
; i
++) {
530 if (sprd_mcdt_chan_int_sts(mcdt
, i
, MCDT_ADC_FIFO_AF_INT
)) {
531 struct sprd_mcdt_chan
*chan
= &mcdt
->chan
[i
];
533 sprd_mcdt_chan_int_clear(mcdt
, i
, MCDT_ADC_FIFO_AF_INT
);
535 chan
->cb
->notify(chan
->cb
->data
);
539 for (i
= 0; i
< MCDT_DAC_CHANNEL_NUM
; i
++) {
540 if (sprd_mcdt_chan_int_sts(mcdt
, i
, MCDT_DAC_FIFO_AE_INT
)) {
541 struct sprd_mcdt_chan
*chan
=
542 &mcdt
->chan
[i
+ MCDT_ADC_CHANNEL_NUM
];
544 sprd_mcdt_chan_int_clear(mcdt
, i
, MCDT_DAC_FIFO_AE_INT
);
546 chan
->cb
->notify(chan
->cb
->data
);
550 spin_unlock(&mcdt
->lock
);
556 * sprd_mcdt_chan_write - write data to the MCDT channel's fifo
557 * @chan: the MCDT channel
558 * @tx_buf: send buffer
561 * Note: We can not write data to the channel fifo when enabling the DMA mode,
562 * otherwise the channel fifo data will be invalid.
564 * If there are not enough space of the channel fifo, it will return errors
567 * Returns 0 on success, or an appropriate error code on failure.
569 int sprd_mcdt_chan_write(struct sprd_mcdt_chan
*chan
, char *tx_buf
, u32 size
)
571 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
573 int avail
, i
= 0, words
= size
/ 4;
574 u32
*buf
= (u32
*)tx_buf
;
576 spin_lock_irqsave(&mcdt
->lock
, flags
);
578 if (chan
->dma_enable
) {
580 "Can not write data when DMA mode enabled\n");
581 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
585 if (sprd_mcdt_chan_fifo_sts(mcdt
, chan
->id
, MCDT_DAC_FIFO_REAL_FULL
)) {
586 dev_err(mcdt
->dev
, "Channel fifo is full now\n");
587 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
591 avail
= sprd_mcdt_dac_fifo_avail(mcdt
, chan
->id
);
594 "Data size is larger than the available fifo size\n");
595 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
600 sprd_mcdt_dac_write_fifo(mcdt
, chan
->id
, *buf
++);
602 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
605 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_write
);
608 * sprd_mcdt_chan_read - read data from the MCDT channel's fifo
609 * @chan: the MCDT channel
610 * @rx_buf: receive buffer
613 * Note: We can not read data from the channel fifo when enabling the DMA mode,
614 * otherwise the reading data will be invalid.
616 * Usually user need start to read data once receiving the fifo full interrupt.
618 * Returns data size of reading successfully, or an error code on failure.
620 int sprd_mcdt_chan_read(struct sprd_mcdt_chan
*chan
, char *rx_buf
, u32 size
)
622 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
624 int i
= 0, avail
, words
= size
/ 4;
625 u32
*buf
= (u32
*)rx_buf
;
627 spin_lock_irqsave(&mcdt
->lock
, flags
);
629 if (chan
->dma_enable
) {
630 dev_err(mcdt
->dev
, "Can not read data when DMA mode enabled\n");
631 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
635 if (sprd_mcdt_chan_fifo_sts(mcdt
, chan
->id
, MCDT_ADC_FIFO_REAL_EMPTY
)) {
636 dev_err(mcdt
->dev
, "Channel fifo is empty\n");
637 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
641 avail
= sprd_mcdt_adc_fifo_avail(mcdt
, chan
->id
);
646 sprd_mcdt_adc_read_fifo(mcdt
, chan
->id
, buf
++);
648 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
651 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_read
);
654 * sprd_mcdt_chan_int_enable - enable the interrupt mode for the MCDT channel
655 * @chan: the MCDT channel
656 * @water_mark: water mark to trigger a interrupt
657 * @cb: callback when a interrupt happened
659 * Now it only can enable fifo almost full interrupt for ADC channel and fifo
660 * almost empty interrupt for DAC channel. Morevoer for interrupt mode, user
661 * should use sprd_mcdt_chan_read() or sprd_mcdt_chan_write() to read or write
664 * For ADC channel, user can start to read data once receiving one fifo full
665 * interrupt. For DAC channel, user can start to write data once receiving one
666 * fifo empty interrupt or just call sprd_mcdt_chan_write() to write data
669 * Returns 0 on success, or an error code on failure.
671 int sprd_mcdt_chan_int_enable(struct sprd_mcdt_chan
*chan
, u32 water_mark
,
672 struct sprd_mcdt_chan_callback
*cb
)
674 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
678 spin_lock_irqsave(&mcdt
->lock
, flags
);
680 if (chan
->dma_enable
|| chan
->int_enable
) {
681 dev_err(mcdt
->dev
, "Failed to set interrupt mode.\n");
682 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
686 switch (chan
->type
) {
687 case SPRD_MCDT_ADC_CHAN
:
688 sprd_mcdt_adc_fifo_clear(mcdt
, chan
->id
);
689 sprd_mcdt_adc_set_watermark(mcdt
, chan
->id
, water_mark
,
690 MCDT_FIFO_LENGTH
- 1);
691 sprd_mcdt_chan_int_en(mcdt
, chan
->id
,
692 MCDT_ADC_FIFO_AF_INT
, true);
693 sprd_mcdt_ap_int_enable(mcdt
, chan
->id
, true);
696 case SPRD_MCDT_DAC_CHAN
:
697 sprd_mcdt_dac_fifo_clear(mcdt
, chan
->id
);
698 sprd_mcdt_dac_set_watermark(mcdt
, chan
->id
,
699 MCDT_FIFO_LENGTH
- 1, water_mark
);
700 sprd_mcdt_chan_int_en(mcdt
, chan
->id
,
701 MCDT_DAC_FIFO_AE_INT
, true);
702 sprd_mcdt_ap_int_enable(mcdt
, chan
->id
, true);
706 dev_err(mcdt
->dev
, "Unsupported channel type\n");
712 chan
->int_enable
= true;
715 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
719 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_int_enable
);
722 * sprd_mcdt_chan_int_disable - disable the interrupt mode for the MCDT channel
723 * @chan: the MCDT channel
725 void sprd_mcdt_chan_int_disable(struct sprd_mcdt_chan
*chan
)
727 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
730 spin_lock_irqsave(&mcdt
->lock
, flags
);
732 if (!chan
->int_enable
) {
733 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
737 switch (chan
->type
) {
738 case SPRD_MCDT_ADC_CHAN
:
739 sprd_mcdt_chan_int_en(mcdt
, chan
->id
,
740 MCDT_ADC_FIFO_AF_INT
, false);
741 sprd_mcdt_chan_int_clear(mcdt
, chan
->id
, MCDT_ADC_FIFO_AF_INT
);
742 sprd_mcdt_ap_int_enable(mcdt
, chan
->id
, false);
745 case SPRD_MCDT_DAC_CHAN
:
746 sprd_mcdt_chan_int_en(mcdt
, chan
->id
,
747 MCDT_DAC_FIFO_AE_INT
, false);
748 sprd_mcdt_chan_int_clear(mcdt
, chan
->id
, MCDT_DAC_FIFO_AE_INT
);
749 sprd_mcdt_ap_int_enable(mcdt
, chan
->id
, false);
756 chan
->int_enable
= false;
757 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
759 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_int_disable
);
762 * sprd_mcdt_chan_dma_enable - enable the DMA mode for the MCDT channel
763 * @chan: the MCDT channel
764 * @dma_chan: specify which DMA channel will be used for this MCDT channel
765 * @water_mark: water mark to trigger a DMA request
767 * Enable the DMA mode for the MCDT channel, that means we can use DMA to
768 * transfer data to the channel fifo and do not need reading/writing data
771 * Returns 0 on success, or an error code on failure.
773 int sprd_mcdt_chan_dma_enable(struct sprd_mcdt_chan
*chan
,
774 enum sprd_mcdt_dma_chan dma_chan
,
777 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
781 spin_lock_irqsave(&mcdt
->lock
, flags
);
783 if (chan
->dma_enable
|| chan
->int_enable
||
784 dma_chan
> SPRD_MCDT_DMA_CH4
) {
785 dev_err(mcdt
->dev
, "Failed to set DMA mode\n");
786 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
790 switch (chan
->type
) {
791 case SPRD_MCDT_ADC_CHAN
:
792 sprd_mcdt_adc_fifo_clear(mcdt
, chan
->id
);
793 sprd_mcdt_adc_set_watermark(mcdt
, chan
->id
,
794 water_mark
, MCDT_FIFO_LENGTH
- 1);
795 sprd_mcdt_adc_dma_enable(mcdt
, chan
->id
, true);
796 sprd_mcdt_adc_dma_chn_select(mcdt
, chan
->id
, dma_chan
);
797 sprd_mcdt_adc_dma_ack_select(mcdt
, chan
->id
, dma_chan
);
800 case SPRD_MCDT_DAC_CHAN
:
801 sprd_mcdt_dac_fifo_clear(mcdt
, chan
->id
);
802 sprd_mcdt_dac_set_watermark(mcdt
, chan
->id
,
803 MCDT_FIFO_LENGTH
- 1, water_mark
);
804 sprd_mcdt_dac_dma_enable(mcdt
, chan
->id
, true);
805 sprd_mcdt_dac_dma_chn_select(mcdt
, chan
->id
, dma_chan
);
806 sprd_mcdt_dac_dma_ack_select(mcdt
, chan
->id
, dma_chan
);
810 dev_err(mcdt
->dev
, "Unsupported channel type\n");
815 chan
->dma_enable
= true;
817 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
821 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_dma_enable
);
824 * sprd_mcdt_chan_dma_disable - disable the DMA mode for the MCDT channel
825 * @chan: the MCDT channel
827 void sprd_mcdt_chan_dma_disable(struct sprd_mcdt_chan
*chan
)
829 struct sprd_mcdt_dev
*mcdt
= chan
->mcdt
;
832 spin_lock_irqsave(&mcdt
->lock
, flags
);
834 if (!chan
->dma_enable
) {
835 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
839 switch (chan
->type
) {
840 case SPRD_MCDT_ADC_CHAN
:
841 sprd_mcdt_adc_dma_enable(mcdt
, chan
->id
, false);
842 sprd_mcdt_adc_fifo_clear(mcdt
, chan
->id
);
845 case SPRD_MCDT_DAC_CHAN
:
846 sprd_mcdt_dac_dma_enable(mcdt
, chan
->id
, false);
847 sprd_mcdt_dac_fifo_clear(mcdt
, chan
->id
);
854 chan
->dma_enable
= false;
855 spin_unlock_irqrestore(&mcdt
->lock
, flags
);
857 EXPORT_SYMBOL_GPL(sprd_mcdt_chan_dma_disable
);
860 * sprd_mcdt_request_chan - request one MCDT channel
861 * @channel: channel id
862 * @type: channel type, it can be one ADC channel or DAC channel
864 * Rreturn NULL if no available channel.
866 struct sprd_mcdt_chan
*sprd_mcdt_request_chan(u8 channel
,
867 enum sprd_mcdt_channel_type type
)
869 struct sprd_mcdt_chan
*temp
, *chan
= NULL
;
871 mutex_lock(&sprd_mcdt_list_mutex
);
873 list_for_each_entry(temp
, &sprd_mcdt_chan_list
, list
) {
874 if (temp
->type
== type
&& temp
->id
== channel
) {
881 list_del(&chan
->list
);
883 mutex_unlock(&sprd_mcdt_list_mutex
);
887 EXPORT_SYMBOL_GPL(sprd_mcdt_request_chan
);
890 * sprd_mcdt_free_chan - free one MCDT channel
891 * @chan: the channel to be freed
893 void sprd_mcdt_free_chan(struct sprd_mcdt_chan
*chan
)
895 struct sprd_mcdt_chan
*temp
;
897 sprd_mcdt_chan_dma_disable(chan
);
898 sprd_mcdt_chan_int_disable(chan
);
900 mutex_lock(&sprd_mcdt_list_mutex
);
902 list_for_each_entry(temp
, &sprd_mcdt_chan_list
, list
) {
904 mutex_unlock(&sprd_mcdt_list_mutex
);
909 list_add_tail(&chan
->list
, &sprd_mcdt_chan_list
);
910 mutex_unlock(&sprd_mcdt_list_mutex
);
912 EXPORT_SYMBOL_GPL(sprd_mcdt_free_chan
);
914 static void sprd_mcdt_init_chans(struct sprd_mcdt_dev
*mcdt
,
915 struct resource
*res
)
919 for (i
= 0; i
< MCDT_CHANNEL_NUM
; i
++) {
920 struct sprd_mcdt_chan
*chan
= &mcdt
->chan
[i
];
922 if (i
< MCDT_ADC_CHANNEL_NUM
) {
924 chan
->type
= SPRD_MCDT_ADC_CHAN
;
925 chan
->fifo_phys
= res
->start
+ MCDT_CH0_RXD
+ i
* 4;
927 chan
->id
= i
- MCDT_ADC_CHANNEL_NUM
;
928 chan
->type
= SPRD_MCDT_DAC_CHAN
;
929 chan
->fifo_phys
= res
->start
+ MCDT_CH0_TXD
+
930 (i
- MCDT_ADC_CHANNEL_NUM
) * 4;
934 INIT_LIST_HEAD(&chan
->list
);
936 mutex_lock(&sprd_mcdt_list_mutex
);
937 list_add_tail(&chan
->list
, &sprd_mcdt_chan_list
);
938 mutex_unlock(&sprd_mcdt_list_mutex
);
942 static int sprd_mcdt_probe(struct platform_device
*pdev
)
944 struct sprd_mcdt_dev
*mcdt
;
945 struct resource
*res
;
948 mcdt
= devm_kzalloc(&pdev
->dev
, sizeof(*mcdt
), GFP_KERNEL
);
952 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
953 mcdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
954 if (IS_ERR(mcdt
->base
))
955 return PTR_ERR(mcdt
->base
);
957 mcdt
->dev
= &pdev
->dev
;
958 spin_lock_init(&mcdt
->lock
);
959 platform_set_drvdata(pdev
, mcdt
);
961 irq
= platform_get_irq(pdev
, 0);
965 ret
= devm_request_irq(&pdev
->dev
, irq
, sprd_mcdt_irq_handler
,
966 0, "sprd-mcdt", mcdt
);
968 dev_err(&pdev
->dev
, "Failed to request MCDT IRQ\n");
972 sprd_mcdt_init_chans(mcdt
, res
);
977 static int sprd_mcdt_remove(struct platform_device
*pdev
)
979 struct sprd_mcdt_chan
*chan
, *temp
;
981 mutex_lock(&sprd_mcdt_list_mutex
);
983 list_for_each_entry_safe(chan
, temp
, &sprd_mcdt_chan_list
, list
)
984 list_del(&chan
->list
);
986 mutex_unlock(&sprd_mcdt_list_mutex
);
991 static const struct of_device_id sprd_mcdt_of_match
[] = {
992 { .compatible
= "sprd,sc9860-mcdt", },
995 MODULE_DEVICE_TABLE(of
, sprd_mcdt_of_match
);
997 static struct platform_driver sprd_mcdt_driver
= {
998 .probe
= sprd_mcdt_probe
,
999 .remove
= sprd_mcdt_remove
,
1001 .name
= "sprd-mcdt",
1002 .of_match_table
= sprd_mcdt_of_match
,
1006 module_platform_driver(sprd_mcdt_driver
);
1008 MODULE_DESCRIPTION("Spreadtrum Multi-Channel Data Transfer Driver");
1009 MODULE_LICENSE("GPL v2");