14e6fdaf1SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
24e6fdaf1SKuninori Morimoto //
34e6fdaf1SKuninori Morimoto // siu_pcm.c - ALSA driver for Renesas SH7343, SH7722 SIU peripheral.
44e6fdaf1SKuninori Morimoto //
54e6fdaf1SKuninori Morimoto // Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
64e6fdaf1SKuninori Morimoto // Copyright (C) 2006 Carlos Munoz <carlos@kenati.com>
74e6fdaf1SKuninori Morimoto
8895d4509SGuennadi Liakhovetski #include <linux/delay.h>
9895d4509SGuennadi Liakhovetski #include <linux/dma-mapping.h>
10895d4509SGuennadi Liakhovetski #include <linux/dmaengine.h>
11895d4509SGuennadi Liakhovetski #include <linux/interrupt.h>
12895d4509SGuennadi Liakhovetski #include <linux/module.h>
13895d4509SGuennadi Liakhovetski #include <linux/platform_device.h>
14895d4509SGuennadi Liakhovetski
15895d4509SGuennadi Liakhovetski #include <sound/control.h>
16895d4509SGuennadi Liakhovetski #include <sound/core.h>
17895d4509SGuennadi Liakhovetski #include <sound/pcm.h>
18895d4509SGuennadi Liakhovetski #include <sound/pcm_params.h>
190d911baeSJarkko Nikula #include <sound/soc.h>
20895d4509SGuennadi Liakhovetski
21895d4509SGuennadi Liakhovetski #include <asm/siu.h>
22895d4509SGuennadi Liakhovetski
23895d4509SGuennadi Liakhovetski #include "siu.h"
24895d4509SGuennadi Liakhovetski
25be3eabe9SKuninori Morimoto #define DRV_NAME "siu-i2s"
26895d4509SGuennadi Liakhovetski #define GET_MAX_PERIODS(buf_bytes, period_bytes) \
27895d4509SGuennadi Liakhovetski ((buf_bytes) / (period_bytes))
28895d4509SGuennadi Liakhovetski #define PERIOD_OFFSET(buf_addr, period_num, period_bytes) \
29895d4509SGuennadi Liakhovetski ((buf_addr) + ((period_num) * (period_bytes)))
30895d4509SGuennadi Liakhovetski
31895d4509SGuennadi Liakhovetski #define RWF_STM_RD 0x01 /* Read in progress */
32895d4509SGuennadi Liakhovetski #define RWF_STM_WT 0x02 /* Write in progress */
33895d4509SGuennadi Liakhovetski
34895d4509SGuennadi Liakhovetski struct siu_port *siu_ports[SIU_PORT_NUM];
35895d4509SGuennadi Liakhovetski
36895d4509SGuennadi Liakhovetski /* transfersize is number of u32 dma transfers per period */
siu_pcm_stmwrite_stop(struct siu_port * port_info)37895d4509SGuennadi Liakhovetski static int siu_pcm_stmwrite_stop(struct siu_port *port_info)
38895d4509SGuennadi Liakhovetski {
39f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
40895d4509SGuennadi Liakhovetski u32 __iomem *base = info->reg;
41895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->playback;
42895d4509SGuennadi Liakhovetski u32 stfifo;
43895d4509SGuennadi Liakhovetski
44895d4509SGuennadi Liakhovetski if (!siu_stream->rw_flg)
45895d4509SGuennadi Liakhovetski return -EPERM;
46895d4509SGuennadi Liakhovetski
47895d4509SGuennadi Liakhovetski /* output FIFO disable */
48895d4509SGuennadi Liakhovetski stfifo = siu_read32(base + SIU_STFIFO);
49895d4509SGuennadi Liakhovetski siu_write32(base + SIU_STFIFO, stfifo & ~0x0c180c18);
50895d4509SGuennadi Liakhovetski pr_debug("%s: STFIFO %x -> %x\n", __func__,
51895d4509SGuennadi Liakhovetski stfifo, stfifo & ~0x0c180c18);
52895d4509SGuennadi Liakhovetski
53895d4509SGuennadi Liakhovetski /* during stmwrite clear */
54895d4509SGuennadi Liakhovetski siu_stream->rw_flg = 0;
55895d4509SGuennadi Liakhovetski
56895d4509SGuennadi Liakhovetski return 0;
57895d4509SGuennadi Liakhovetski }
58895d4509SGuennadi Liakhovetski
siu_pcm_stmwrite_start(struct siu_port * port_info)59895d4509SGuennadi Liakhovetski static int siu_pcm_stmwrite_start(struct siu_port *port_info)
60895d4509SGuennadi Liakhovetski {
61895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->playback;
62895d4509SGuennadi Liakhovetski
63895d4509SGuennadi Liakhovetski if (siu_stream->rw_flg)
64895d4509SGuennadi Liakhovetski return -EPERM;
65895d4509SGuennadi Liakhovetski
66895d4509SGuennadi Liakhovetski /* Current period in buffer */
67895d4509SGuennadi Liakhovetski port_info->playback.cur_period = 0;
68895d4509SGuennadi Liakhovetski
69895d4509SGuennadi Liakhovetski /* during stmwrite flag set */
70895d4509SGuennadi Liakhovetski siu_stream->rw_flg = RWF_STM_WT;
71895d4509SGuennadi Liakhovetski
72895d4509SGuennadi Liakhovetski /* DMA transfer start */
73d668e640STakashi Iwai queue_work(system_highpri_wq, &siu_stream->work);
74895d4509SGuennadi Liakhovetski
75895d4509SGuennadi Liakhovetski return 0;
76895d4509SGuennadi Liakhovetski }
77895d4509SGuennadi Liakhovetski
siu_dma_tx_complete(void * arg)78895d4509SGuennadi Liakhovetski static void siu_dma_tx_complete(void *arg)
79895d4509SGuennadi Liakhovetski {
80895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = arg;
81895d4509SGuennadi Liakhovetski
82895d4509SGuennadi Liakhovetski if (!siu_stream->rw_flg)
83895d4509SGuennadi Liakhovetski return;
84895d4509SGuennadi Liakhovetski
85895d4509SGuennadi Liakhovetski /* Update completed period count */
86895d4509SGuennadi Liakhovetski if (++siu_stream->cur_period >=
87895d4509SGuennadi Liakhovetski GET_MAX_PERIODS(siu_stream->buf_bytes,
88895d4509SGuennadi Liakhovetski siu_stream->period_bytes))
89895d4509SGuennadi Liakhovetski siu_stream->cur_period = 0;
90895d4509SGuennadi Liakhovetski
91895d4509SGuennadi Liakhovetski pr_debug("%s: done period #%d (%u/%u bytes), cookie %d\n",
92895d4509SGuennadi Liakhovetski __func__, siu_stream->cur_period,
93895d4509SGuennadi Liakhovetski siu_stream->cur_period * siu_stream->period_bytes,
94895d4509SGuennadi Liakhovetski siu_stream->buf_bytes, siu_stream->cookie);
95895d4509SGuennadi Liakhovetski
96d668e640STakashi Iwai queue_work(system_highpri_wq, &siu_stream->work);
97895d4509SGuennadi Liakhovetski
98895d4509SGuennadi Liakhovetski /* Notify alsa: a period is done */
99895d4509SGuennadi Liakhovetski snd_pcm_period_elapsed(siu_stream->substream);
100895d4509SGuennadi Liakhovetski }
101895d4509SGuennadi Liakhovetski
siu_pcm_wr_set(struct siu_port * port_info,dma_addr_t buff,u32 size)102895d4509SGuennadi Liakhovetski static int siu_pcm_wr_set(struct siu_port *port_info,
103895d4509SGuennadi Liakhovetski dma_addr_t buff, u32 size)
104895d4509SGuennadi Liakhovetski {
105f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
106895d4509SGuennadi Liakhovetski u32 __iomem *base = info->reg;
107895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->playback;
108895d4509SGuennadi Liakhovetski struct snd_pcm_substream *substream = siu_stream->substream;
109895d4509SGuennadi Liakhovetski struct device *dev = substream->pcm->card->dev;
110895d4509SGuennadi Liakhovetski struct dma_async_tx_descriptor *desc;
111895d4509SGuennadi Liakhovetski dma_cookie_t cookie;
112895d4509SGuennadi Liakhovetski struct scatterlist sg;
113895d4509SGuennadi Liakhovetski u32 stfifo;
114895d4509SGuennadi Liakhovetski
115895d4509SGuennadi Liakhovetski sg_init_table(&sg, 1);
116895d4509SGuennadi Liakhovetski sg_set_page(&sg, pfn_to_page(PFN_DOWN(buff)),
117895d4509SGuennadi Liakhovetski size, offset_in_page(buff));
1187b42176aSMarkus Pietrek sg_dma_len(&sg) = size;
119895d4509SGuennadi Liakhovetski sg_dma_address(&sg) = buff;
120895d4509SGuennadi Liakhovetski
12116052827SAlexandre Bounine desc = dmaengine_prep_slave_sg(siu_stream->chan,
12235e16581SVinod Koul &sg, 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
123895d4509SGuennadi Liakhovetski if (!desc) {
124895d4509SGuennadi Liakhovetski dev_err(dev, "Failed to allocate a dma descriptor\n");
125895d4509SGuennadi Liakhovetski return -ENOMEM;
126895d4509SGuennadi Liakhovetski }
127895d4509SGuennadi Liakhovetski
128895d4509SGuennadi Liakhovetski desc->callback = siu_dma_tx_complete;
129895d4509SGuennadi Liakhovetski desc->callback_param = siu_stream;
1305d0ecb0eSLars-Peter Clausen cookie = dmaengine_submit(desc);
131895d4509SGuennadi Liakhovetski if (cookie < 0) {
132895d4509SGuennadi Liakhovetski dev_err(dev, "Failed to submit a dma transfer\n");
133895d4509SGuennadi Liakhovetski return cookie;
134895d4509SGuennadi Liakhovetski }
135895d4509SGuennadi Liakhovetski
136895d4509SGuennadi Liakhovetski siu_stream->tx_desc = desc;
137895d4509SGuennadi Liakhovetski siu_stream->cookie = cookie;
138895d4509SGuennadi Liakhovetski
139895d4509SGuennadi Liakhovetski dma_async_issue_pending(siu_stream->chan);
140895d4509SGuennadi Liakhovetski
141895d4509SGuennadi Liakhovetski /* only output FIFO enable */
142895d4509SGuennadi Liakhovetski stfifo = siu_read32(base + SIU_STFIFO);
143895d4509SGuennadi Liakhovetski siu_write32(base + SIU_STFIFO, stfifo | (port_info->stfifo & 0x0c180c18));
144895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__,
145895d4509SGuennadi Liakhovetski stfifo, stfifo | (port_info->stfifo & 0x0c180c18));
146895d4509SGuennadi Liakhovetski
147895d4509SGuennadi Liakhovetski return 0;
148895d4509SGuennadi Liakhovetski }
149895d4509SGuennadi Liakhovetski
siu_pcm_rd_set(struct siu_port * port_info,dma_addr_t buff,size_t size)150895d4509SGuennadi Liakhovetski static int siu_pcm_rd_set(struct siu_port *port_info,
151895d4509SGuennadi Liakhovetski dma_addr_t buff, size_t size)
152895d4509SGuennadi Liakhovetski {
153f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
154895d4509SGuennadi Liakhovetski u32 __iomem *base = info->reg;
155895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->capture;
156895d4509SGuennadi Liakhovetski struct snd_pcm_substream *substream = siu_stream->substream;
157895d4509SGuennadi Liakhovetski struct device *dev = substream->pcm->card->dev;
158895d4509SGuennadi Liakhovetski struct dma_async_tx_descriptor *desc;
159895d4509SGuennadi Liakhovetski dma_cookie_t cookie;
160895d4509SGuennadi Liakhovetski struct scatterlist sg;
161895d4509SGuennadi Liakhovetski u32 stfifo;
162895d4509SGuennadi Liakhovetski
163895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: %u@%llx\n", __func__, size, (unsigned long long)buff);
164895d4509SGuennadi Liakhovetski
165895d4509SGuennadi Liakhovetski sg_init_table(&sg, 1);
166895d4509SGuennadi Liakhovetski sg_set_page(&sg, pfn_to_page(PFN_DOWN(buff)),
167895d4509SGuennadi Liakhovetski size, offset_in_page(buff));
1687b42176aSMarkus Pietrek sg_dma_len(&sg) = size;
169895d4509SGuennadi Liakhovetski sg_dma_address(&sg) = buff;
170895d4509SGuennadi Liakhovetski
17116052827SAlexandre Bounine desc = dmaengine_prep_slave_sg(siu_stream->chan,
17235e16581SVinod Koul &sg, 1, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
173895d4509SGuennadi Liakhovetski if (!desc) {
174895d4509SGuennadi Liakhovetski dev_err(dev, "Failed to allocate dma descriptor\n");
175895d4509SGuennadi Liakhovetski return -ENOMEM;
176895d4509SGuennadi Liakhovetski }
177895d4509SGuennadi Liakhovetski
178895d4509SGuennadi Liakhovetski desc->callback = siu_dma_tx_complete;
179895d4509SGuennadi Liakhovetski desc->callback_param = siu_stream;
1805d0ecb0eSLars-Peter Clausen cookie = dmaengine_submit(desc);
181895d4509SGuennadi Liakhovetski if (cookie < 0) {
182895d4509SGuennadi Liakhovetski dev_err(dev, "Failed to submit dma descriptor\n");
183895d4509SGuennadi Liakhovetski return cookie;
184895d4509SGuennadi Liakhovetski }
185895d4509SGuennadi Liakhovetski
186895d4509SGuennadi Liakhovetski siu_stream->tx_desc = desc;
187895d4509SGuennadi Liakhovetski siu_stream->cookie = cookie;
188895d4509SGuennadi Liakhovetski
189895d4509SGuennadi Liakhovetski dma_async_issue_pending(siu_stream->chan);
190895d4509SGuennadi Liakhovetski
191895d4509SGuennadi Liakhovetski /* only input FIFO enable */
192895d4509SGuennadi Liakhovetski stfifo = siu_read32(base + SIU_STFIFO);
193895d4509SGuennadi Liakhovetski siu_write32(base + SIU_STFIFO, siu_read32(base + SIU_STFIFO) |
194895d4509SGuennadi Liakhovetski (port_info->stfifo & 0x13071307));
195895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__,
196895d4509SGuennadi Liakhovetski stfifo, stfifo | (port_info->stfifo & 0x13071307));
197895d4509SGuennadi Liakhovetski
198895d4509SGuennadi Liakhovetski return 0;
199895d4509SGuennadi Liakhovetski }
200895d4509SGuennadi Liakhovetski
siu_io_work(struct work_struct * work)201d668e640STakashi Iwai static void siu_io_work(struct work_struct *work)
202895d4509SGuennadi Liakhovetski {
203d668e640STakashi Iwai struct siu_stream *siu_stream = container_of(work, struct siu_stream,
204d668e640STakashi Iwai work);
205895d4509SGuennadi Liakhovetski struct snd_pcm_substream *substream = siu_stream->substream;
206895d4509SGuennadi Liakhovetski struct device *dev = substream->pcm->card->dev;
207895d4509SGuennadi Liakhovetski struct snd_pcm_runtime *rt = substream->runtime;
208895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(substream);
209895d4509SGuennadi Liakhovetski
210895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: flags %x\n", __func__, siu_stream->rw_flg);
211895d4509SGuennadi Liakhovetski
212895d4509SGuennadi Liakhovetski if (!siu_stream->rw_flg) {
213895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: stream inactive\n", __func__);
214895d4509SGuennadi Liakhovetski return;
215895d4509SGuennadi Liakhovetski }
216895d4509SGuennadi Liakhovetski
217895d4509SGuennadi Liakhovetski if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
218895d4509SGuennadi Liakhovetski dma_addr_t buff;
219895d4509SGuennadi Liakhovetski size_t count;
220895d4509SGuennadi Liakhovetski
221895d4509SGuennadi Liakhovetski buff = (dma_addr_t)PERIOD_OFFSET(rt->dma_addr,
222895d4509SGuennadi Liakhovetski siu_stream->cur_period,
223895d4509SGuennadi Liakhovetski siu_stream->period_bytes);
224895d4509SGuennadi Liakhovetski count = siu_stream->period_bytes;
225895d4509SGuennadi Liakhovetski
226895d4509SGuennadi Liakhovetski /* DMA transfer start */
227895d4509SGuennadi Liakhovetski siu_pcm_rd_set(port_info, buff, count);
228895d4509SGuennadi Liakhovetski } else {
229895d4509SGuennadi Liakhovetski siu_pcm_wr_set(port_info,
230895d4509SGuennadi Liakhovetski (dma_addr_t)PERIOD_OFFSET(rt->dma_addr,
231895d4509SGuennadi Liakhovetski siu_stream->cur_period,
232895d4509SGuennadi Liakhovetski siu_stream->period_bytes),
233895d4509SGuennadi Liakhovetski siu_stream->period_bytes);
234895d4509SGuennadi Liakhovetski }
235895d4509SGuennadi Liakhovetski }
236895d4509SGuennadi Liakhovetski
237895d4509SGuennadi Liakhovetski /* Capture */
siu_pcm_stmread_start(struct siu_port * port_info)238895d4509SGuennadi Liakhovetski static int siu_pcm_stmread_start(struct siu_port *port_info)
239895d4509SGuennadi Liakhovetski {
240895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->capture;
241895d4509SGuennadi Liakhovetski
242895d4509SGuennadi Liakhovetski if (siu_stream->xfer_cnt > 0x1000000)
243895d4509SGuennadi Liakhovetski return -EINVAL;
244895d4509SGuennadi Liakhovetski if (siu_stream->rw_flg)
245895d4509SGuennadi Liakhovetski return -EPERM;
246895d4509SGuennadi Liakhovetski
247895d4509SGuennadi Liakhovetski /* Current period in buffer */
248895d4509SGuennadi Liakhovetski siu_stream->cur_period = 0;
249895d4509SGuennadi Liakhovetski
250895d4509SGuennadi Liakhovetski /* during stmread flag set */
251895d4509SGuennadi Liakhovetski siu_stream->rw_flg = RWF_STM_RD;
252895d4509SGuennadi Liakhovetski
253d668e640STakashi Iwai queue_work(system_highpri_wq, &siu_stream->work);
254895d4509SGuennadi Liakhovetski
255895d4509SGuennadi Liakhovetski return 0;
256895d4509SGuennadi Liakhovetski }
257895d4509SGuennadi Liakhovetski
siu_pcm_stmread_stop(struct siu_port * port_info)258895d4509SGuennadi Liakhovetski static int siu_pcm_stmread_stop(struct siu_port *port_info)
259895d4509SGuennadi Liakhovetski {
260f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
261895d4509SGuennadi Liakhovetski u32 __iomem *base = info->reg;
262895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream = &port_info->capture;
263895d4509SGuennadi Liakhovetski struct device *dev = siu_stream->substream->pcm->card->dev;
264895d4509SGuennadi Liakhovetski u32 stfifo;
265895d4509SGuennadi Liakhovetski
266895d4509SGuennadi Liakhovetski if (!siu_stream->rw_flg)
267895d4509SGuennadi Liakhovetski return -EPERM;
268895d4509SGuennadi Liakhovetski
269895d4509SGuennadi Liakhovetski /* input FIFO disable */
270895d4509SGuennadi Liakhovetski stfifo = siu_read32(base + SIU_STFIFO);
271895d4509SGuennadi Liakhovetski siu_write32(base + SIU_STFIFO, stfifo & ~0x13071307);
272895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__,
273895d4509SGuennadi Liakhovetski stfifo, stfifo & ~0x13071307);
274895d4509SGuennadi Liakhovetski
275895d4509SGuennadi Liakhovetski /* during stmread flag clear */
276895d4509SGuennadi Liakhovetski siu_stream->rw_flg = 0;
277895d4509SGuennadi Liakhovetski
278895d4509SGuennadi Liakhovetski return 0;
279895d4509SGuennadi Liakhovetski }
280895d4509SGuennadi Liakhovetski
filter(struct dma_chan * chan,void * secondary)281f363459fSKuninori Morimoto static bool filter(struct dma_chan *chan, void *secondary)
282895d4509SGuennadi Liakhovetski {
283f363459fSKuninori Morimoto struct sh_dmae_slave *param = secondary;
284895d4509SGuennadi Liakhovetski
285f363459fSKuninori Morimoto pr_debug("%s: secondary ID %d\n", __func__, param->shdma_slave.slave_id);
286895d4509SGuennadi Liakhovetski
2876f3de8bfSGuennadi Liakhovetski chan->private = ¶m->shdma_slave;
288895d4509SGuennadi Liakhovetski return true;
289895d4509SGuennadi Liakhovetski }
290895d4509SGuennadi Liakhovetski
siu_pcm_open(struct snd_soc_component * component,struct snd_pcm_substream * ss)291932eaf7cSKuninori Morimoto static int siu_pcm_open(struct snd_soc_component *component,
292932eaf7cSKuninori Morimoto struct snd_pcm_substream *ss)
293895d4509SGuennadi Liakhovetski {
294895d4509SGuennadi Liakhovetski /* Playback / Capture */
295be3eabe9SKuninori Morimoto struct siu_platform *pdata = component->dev->platform_data;
296f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
297895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(ss);
298895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream;
299895d4509SGuennadi Liakhovetski u32 port = info->port_id;
300895d4509SGuennadi Liakhovetski struct device *dev = ss->pcm->card->dev;
301895d4509SGuennadi Liakhovetski dma_cap_mask_t mask;
302895d4509SGuennadi Liakhovetski struct sh_dmae_slave *param;
303895d4509SGuennadi Liakhovetski
304895d4509SGuennadi Liakhovetski dma_cap_zero(mask);
305895d4509SGuennadi Liakhovetski dma_cap_set(DMA_SLAVE, mask);
306895d4509SGuennadi Liakhovetski
307895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s, port=%d@%p\n", __func__, port, port_info);
308895d4509SGuennadi Liakhovetski
309895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) {
310895d4509SGuennadi Liakhovetski siu_stream = &port_info->playback;
311895d4509SGuennadi Liakhovetski param = &siu_stream->param;
3126f3de8bfSGuennadi Liakhovetski param->shdma_slave.slave_id = port ? pdata->dma_slave_tx_b :
31310440af1SGuennadi Liakhovetski pdata->dma_slave_tx_a;
314895d4509SGuennadi Liakhovetski } else {
315895d4509SGuennadi Liakhovetski siu_stream = &port_info->capture;
316895d4509SGuennadi Liakhovetski param = &siu_stream->param;
3176f3de8bfSGuennadi Liakhovetski param->shdma_slave.slave_id = port ? pdata->dma_slave_rx_b :
31810440af1SGuennadi Liakhovetski pdata->dma_slave_rx_a;
319895d4509SGuennadi Liakhovetski }
320895d4509SGuennadi Liakhovetski
321895d4509SGuennadi Liakhovetski /* Get DMA channel */
322895d4509SGuennadi Liakhovetski siu_stream->chan = dma_request_channel(mask, filter, param);
323895d4509SGuennadi Liakhovetski if (!siu_stream->chan) {
324895d4509SGuennadi Liakhovetski dev_err(dev, "DMA channel allocation failed!\n");
325895d4509SGuennadi Liakhovetski return -EBUSY;
326895d4509SGuennadi Liakhovetski }
327895d4509SGuennadi Liakhovetski
328895d4509SGuennadi Liakhovetski siu_stream->substream = ss;
329895d4509SGuennadi Liakhovetski
330895d4509SGuennadi Liakhovetski return 0;
331895d4509SGuennadi Liakhovetski }
332895d4509SGuennadi Liakhovetski
siu_pcm_close(struct snd_soc_component * component,struct snd_pcm_substream * ss)333932eaf7cSKuninori Morimoto static int siu_pcm_close(struct snd_soc_component *component,
334932eaf7cSKuninori Morimoto struct snd_pcm_substream *ss)
335895d4509SGuennadi Liakhovetski {
336f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
337895d4509SGuennadi Liakhovetski struct device *dev = ss->pcm->card->dev;
338895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(ss);
339895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream;
340895d4509SGuennadi Liakhovetski
341895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: port=%d\n", __func__, info->port_id);
342895d4509SGuennadi Liakhovetski
343895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
344895d4509SGuennadi Liakhovetski siu_stream = &port_info->playback;
345895d4509SGuennadi Liakhovetski else
346895d4509SGuennadi Liakhovetski siu_stream = &port_info->capture;
347895d4509SGuennadi Liakhovetski
348895d4509SGuennadi Liakhovetski dma_release_channel(siu_stream->chan);
349895d4509SGuennadi Liakhovetski siu_stream->chan = NULL;
350895d4509SGuennadi Liakhovetski
351895d4509SGuennadi Liakhovetski siu_stream->substream = NULL;
352895d4509SGuennadi Liakhovetski
353895d4509SGuennadi Liakhovetski return 0;
354895d4509SGuennadi Liakhovetski }
355895d4509SGuennadi Liakhovetski
siu_pcm_prepare(struct snd_soc_component * component,struct snd_pcm_substream * ss)356932eaf7cSKuninori Morimoto static int siu_pcm_prepare(struct snd_soc_component *component,
357932eaf7cSKuninori Morimoto struct snd_pcm_substream *ss)
358895d4509SGuennadi Liakhovetski {
359f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
360895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(ss);
361895d4509SGuennadi Liakhovetski struct device *dev = ss->pcm->card->dev;
362eeff1df1SPierre-Louis Bossart struct snd_pcm_runtime *rt;
363895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream;
364895d4509SGuennadi Liakhovetski snd_pcm_sframes_t xfer_cnt;
365895d4509SGuennadi Liakhovetski
366895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
367895d4509SGuennadi Liakhovetski siu_stream = &port_info->playback;
368895d4509SGuennadi Liakhovetski else
369895d4509SGuennadi Liakhovetski siu_stream = &port_info->capture;
370895d4509SGuennadi Liakhovetski
371895d4509SGuennadi Liakhovetski rt = siu_stream->substream->runtime;
372895d4509SGuennadi Liakhovetski
373895d4509SGuennadi Liakhovetski siu_stream->buf_bytes = snd_pcm_lib_buffer_bytes(ss);
374895d4509SGuennadi Liakhovetski siu_stream->period_bytes = snd_pcm_lib_period_bytes(ss);
375895d4509SGuennadi Liakhovetski
376895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: port=%d, %d channels, period=%u bytes\n", __func__,
377895d4509SGuennadi Liakhovetski info->port_id, rt->channels, siu_stream->period_bytes);
378895d4509SGuennadi Liakhovetski
379895d4509SGuennadi Liakhovetski /* We only support buffers that are multiples of the period */
380895d4509SGuennadi Liakhovetski if (siu_stream->buf_bytes % siu_stream->period_bytes) {
381895d4509SGuennadi Liakhovetski dev_err(dev, "%s() - buffer=%d not multiple of period=%d\n",
382895d4509SGuennadi Liakhovetski __func__, siu_stream->buf_bytes,
383895d4509SGuennadi Liakhovetski siu_stream->period_bytes);
384895d4509SGuennadi Liakhovetski return -EINVAL;
385895d4509SGuennadi Liakhovetski }
386895d4509SGuennadi Liakhovetski
387895d4509SGuennadi Liakhovetski xfer_cnt = bytes_to_frames(rt, siu_stream->period_bytes);
388895d4509SGuennadi Liakhovetski if (!xfer_cnt || xfer_cnt > 0x1000000)
389895d4509SGuennadi Liakhovetski return -EINVAL;
390895d4509SGuennadi Liakhovetski
391895d4509SGuennadi Liakhovetski siu_stream->format = rt->format;
392895d4509SGuennadi Liakhovetski siu_stream->xfer_cnt = xfer_cnt;
393895d4509SGuennadi Liakhovetski
394895d4509SGuennadi Liakhovetski dev_dbg(dev, "port=%d buf=%lx buf_bytes=%d period_bytes=%d "
395895d4509SGuennadi Liakhovetski "format=%d channels=%d xfer_cnt=%d\n", info->port_id,
396895d4509SGuennadi Liakhovetski (unsigned long)rt->dma_addr, siu_stream->buf_bytes,
397895d4509SGuennadi Liakhovetski siu_stream->period_bytes,
398895d4509SGuennadi Liakhovetski siu_stream->format, rt->channels, (int)xfer_cnt);
399895d4509SGuennadi Liakhovetski
400895d4509SGuennadi Liakhovetski return 0;
401895d4509SGuennadi Liakhovetski }
402895d4509SGuennadi Liakhovetski
siu_pcm_trigger(struct snd_soc_component * component,struct snd_pcm_substream * ss,int cmd)403932eaf7cSKuninori Morimoto static int siu_pcm_trigger(struct snd_soc_component *component,
404932eaf7cSKuninori Morimoto struct snd_pcm_substream *ss, int cmd)
405895d4509SGuennadi Liakhovetski {
406f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
407895d4509SGuennadi Liakhovetski struct device *dev = ss->pcm->card->dev;
408895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(ss);
409895d4509SGuennadi Liakhovetski int ret;
410895d4509SGuennadi Liakhovetski
411895d4509SGuennadi Liakhovetski dev_dbg(dev, "%s: port=%d@%p, cmd=%d\n", __func__,
412895d4509SGuennadi Liakhovetski info->port_id, port_info, cmd);
413895d4509SGuennadi Liakhovetski
414895d4509SGuennadi Liakhovetski switch (cmd) {
415895d4509SGuennadi Liakhovetski case SNDRV_PCM_TRIGGER_START:
416895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
417895d4509SGuennadi Liakhovetski ret = siu_pcm_stmwrite_start(port_info);
418895d4509SGuennadi Liakhovetski else
419895d4509SGuennadi Liakhovetski ret = siu_pcm_stmread_start(port_info);
420895d4509SGuennadi Liakhovetski
421895d4509SGuennadi Liakhovetski if (ret < 0)
422895d4509SGuennadi Liakhovetski dev_warn(dev, "%s: start failed on port=%d\n",
423895d4509SGuennadi Liakhovetski __func__, info->port_id);
424895d4509SGuennadi Liakhovetski
425895d4509SGuennadi Liakhovetski break;
426895d4509SGuennadi Liakhovetski case SNDRV_PCM_TRIGGER_STOP:
427895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
428895d4509SGuennadi Liakhovetski siu_pcm_stmwrite_stop(port_info);
429895d4509SGuennadi Liakhovetski else
430895d4509SGuennadi Liakhovetski siu_pcm_stmread_stop(port_info);
431895d4509SGuennadi Liakhovetski ret = 0;
432895d4509SGuennadi Liakhovetski
433895d4509SGuennadi Liakhovetski break;
434895d4509SGuennadi Liakhovetski default:
435895d4509SGuennadi Liakhovetski dev_err(dev, "%s() unsupported cmd=%d\n", __func__, cmd);
436895d4509SGuennadi Liakhovetski ret = -EINVAL;
437895d4509SGuennadi Liakhovetski }
438895d4509SGuennadi Liakhovetski
439895d4509SGuennadi Liakhovetski return ret;
440895d4509SGuennadi Liakhovetski }
441895d4509SGuennadi Liakhovetski
442895d4509SGuennadi Liakhovetski /*
443895d4509SGuennadi Liakhovetski * So far only resolution of one period is supported, subject to extending the
444895d4509SGuennadi Liakhovetski * dmangine API
445895d4509SGuennadi Liakhovetski */
446932eaf7cSKuninori Morimoto static snd_pcm_uframes_t
siu_pcm_pointer_dma(struct snd_soc_component * component,struct snd_pcm_substream * ss)447932eaf7cSKuninori Morimoto siu_pcm_pointer_dma(struct snd_soc_component *component,
448932eaf7cSKuninori Morimoto struct snd_pcm_substream *ss)
449895d4509SGuennadi Liakhovetski {
450895d4509SGuennadi Liakhovetski struct device *dev = ss->pcm->card->dev;
451f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
452895d4509SGuennadi Liakhovetski u32 __iomem *base = info->reg;
453895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_port_info(ss);
454895d4509SGuennadi Liakhovetski struct snd_pcm_runtime *rt = ss->runtime;
455895d4509SGuennadi Liakhovetski size_t ptr;
456895d4509SGuennadi Liakhovetski struct siu_stream *siu_stream;
457895d4509SGuennadi Liakhovetski
458895d4509SGuennadi Liakhovetski if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK)
459895d4509SGuennadi Liakhovetski siu_stream = &port_info->playback;
460895d4509SGuennadi Liakhovetski else
461895d4509SGuennadi Liakhovetski siu_stream = &port_info->capture;
462895d4509SGuennadi Liakhovetski
463895d4509SGuennadi Liakhovetski /*
464895d4509SGuennadi Liakhovetski * ptr is the offset into the buffer where the dma is currently at. We
465895d4509SGuennadi Liakhovetski * check if the dma buffer has just wrapped.
466895d4509SGuennadi Liakhovetski */
467895d4509SGuennadi Liakhovetski ptr = PERIOD_OFFSET(rt->dma_addr,
468895d4509SGuennadi Liakhovetski siu_stream->cur_period,
469895d4509SGuennadi Liakhovetski siu_stream->period_bytes) - rt->dma_addr;
470895d4509SGuennadi Liakhovetski
471895d4509SGuennadi Liakhovetski dev_dbg(dev,
472895d4509SGuennadi Liakhovetski "%s: port=%d, events %x, FSTS %x, xferred %u/%u, cookie %d\n",
473895d4509SGuennadi Liakhovetski __func__, info->port_id, siu_read32(base + SIU_EVNTC),
474895d4509SGuennadi Liakhovetski siu_read32(base + SIU_SBFSTS), ptr, siu_stream->buf_bytes,
475895d4509SGuennadi Liakhovetski siu_stream->cookie);
476895d4509SGuennadi Liakhovetski
477895d4509SGuennadi Liakhovetski if (ptr >= siu_stream->buf_bytes)
478895d4509SGuennadi Liakhovetski ptr = 0;
479895d4509SGuennadi Liakhovetski
480895d4509SGuennadi Liakhovetski return bytes_to_frames(ss->runtime, ptr);
481895d4509SGuennadi Liakhovetski }
482895d4509SGuennadi Liakhovetski
siu_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)483932eaf7cSKuninori Morimoto static int siu_pcm_new(struct snd_soc_component *component,
484932eaf7cSKuninori Morimoto struct snd_soc_pcm_runtime *rtd)
485895d4509SGuennadi Liakhovetski {
486895d4509SGuennadi Liakhovetski /* card->dev == socdev->dev, see snd_soc_new_pcms() */
487552d1ef6SLiam Girdwood struct snd_card *card = rtd->card->snd_card;
488552d1ef6SLiam Girdwood struct snd_pcm *pcm = rtd->pcm;
489f0fba2adSLiam Girdwood struct siu_info *info = siu_i2s_data;
490895d4509SGuennadi Liakhovetski struct platform_device *pdev = to_platform_device(card->dev);
491895d4509SGuennadi Liakhovetski int ret;
492895d4509SGuennadi Liakhovetski int i;
493895d4509SGuennadi Liakhovetski
494895d4509SGuennadi Liakhovetski /* pdev->id selects between SIUA and SIUB */
495895d4509SGuennadi Liakhovetski if (pdev->id < 0 || pdev->id >= SIU_PORT_NUM)
496895d4509SGuennadi Liakhovetski return -EINVAL;
497895d4509SGuennadi Liakhovetski
498895d4509SGuennadi Liakhovetski info->port_id = pdev->id;
499895d4509SGuennadi Liakhovetski
500895d4509SGuennadi Liakhovetski /*
501895d4509SGuennadi Liakhovetski * While the siu has 2 ports, only one port can be on at a time (only 1
502895d4509SGuennadi Liakhovetski * SPB). So far all the boards using the siu had only one of the ports
503895d4509SGuennadi Liakhovetski * wired to a codec. To simplify things, we only register one port with
504895d4509SGuennadi Liakhovetski * alsa. In case both ports are needed, it should be changed here
505895d4509SGuennadi Liakhovetski */
506895d4509SGuennadi Liakhovetski for (i = pdev->id; i < pdev->id + 1; i++) {
507895d4509SGuennadi Liakhovetski struct siu_port **port_info = &siu_ports[i];
508895d4509SGuennadi Liakhovetski
509895d4509SGuennadi Liakhovetski ret = siu_init_port(i, port_info, card);
510895d4509SGuennadi Liakhovetski if (ret < 0)
511895d4509SGuennadi Liakhovetski return ret;
512895d4509SGuennadi Liakhovetski
5132c7c9630STakashi Iwai snd_pcm_set_managed_buffer_all(pcm,
5148f74ae39STakashi Iwai SNDRV_DMA_TYPE_DEV, card->dev,
515895d4509SGuennadi Liakhovetski SIU_BUFFER_BYTES_MAX, SIU_BUFFER_BYTES_MAX);
516895d4509SGuennadi Liakhovetski
517895d4509SGuennadi Liakhovetski (*port_info)->pcm = pcm;
518895d4509SGuennadi Liakhovetski
519d668e640STakashi Iwai /* IO works */
520d668e640STakashi Iwai INIT_WORK(&(*port_info)->playback.work, siu_io_work);
521d668e640STakashi Iwai INIT_WORK(&(*port_info)->capture.work, siu_io_work);
522895d4509SGuennadi Liakhovetski }
523895d4509SGuennadi Liakhovetski
524895d4509SGuennadi Liakhovetski dev_info(card->dev, "SuperH SIU driver initialized.\n");
525895d4509SGuennadi Liakhovetski return 0;
526895d4509SGuennadi Liakhovetski }
527895d4509SGuennadi Liakhovetski
siu_pcm_free(struct snd_soc_component * component,struct snd_pcm * pcm)528932eaf7cSKuninori Morimoto static void siu_pcm_free(struct snd_soc_component *component,
529932eaf7cSKuninori Morimoto struct snd_pcm *pcm)
530895d4509SGuennadi Liakhovetski {
531895d4509SGuennadi Liakhovetski struct platform_device *pdev = to_platform_device(pcm->card->dev);
532895d4509SGuennadi Liakhovetski struct siu_port *port_info = siu_ports[pdev->id];
533895d4509SGuennadi Liakhovetski
534d668e640STakashi Iwai cancel_work_sync(&port_info->capture.work);
535d668e640STakashi Iwai cancel_work_sync(&port_info->playback.work);
536895d4509SGuennadi Liakhovetski
537895d4509SGuennadi Liakhovetski siu_free_port(port_info);
538895d4509SGuennadi Liakhovetski
539895d4509SGuennadi Liakhovetski dev_dbg(pcm->card->dev, "%s\n", __func__);
540895d4509SGuennadi Liakhovetski }
541895d4509SGuennadi Liakhovetski
542ae07f5c7STakashi Iwai const struct snd_soc_component_driver siu_component = {
543932eaf7cSKuninori Morimoto .name = DRV_NAME,
544895d4509SGuennadi Liakhovetski .open = siu_pcm_open,
545895d4509SGuennadi Liakhovetski .close = siu_pcm_close,
546895d4509SGuennadi Liakhovetski .prepare = siu_pcm_prepare,
547895d4509SGuennadi Liakhovetski .trigger = siu_pcm_trigger,
548895d4509SGuennadi Liakhovetski .pointer = siu_pcm_pointer_dma,
549932eaf7cSKuninori Morimoto .pcm_construct = siu_pcm_new,
550932eaf7cSKuninori Morimoto .pcm_destruct = siu_pcm_free,
551*f712ff57SCharles Keepax .legacy_dai_naming = 1,
552895d4509SGuennadi Liakhovetski };
553be3eabe9SKuninori Morimoto EXPORT_SYMBOL_GPL(siu_component);
554