1
2
3
4
5
6
7
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/device.h>
12#include <linux/dma-mapping.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18
19#include <sound/core.h>
20#include <sound/dmaengine_pcm.h>
21#include <sound/initval.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25
26#include <asm/fiq.h>
27
28#include <linux/platform_data/asoc-imx-ssi.h>
29
30#include "imx-ssi.h"
31#include "imx-pcm.h"
32
33struct imx_pcm_runtime_data {
34 unsigned int period;
35 int periods;
36 unsigned long offset;
37 struct hrtimer hrt;
38 int poll_time_ns;
39 struct snd_pcm_substream *substream;
40 atomic_t playing;
41 atomic_t capturing;
42};
43
44static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
45{
46 struct imx_pcm_runtime_data *iprtd =
47 container_of(hrt, struct imx_pcm_runtime_data, hrt);
48 struct snd_pcm_substream *substream = iprtd->substream;
49 struct pt_regs regs;
50
51 if (!atomic_read(&iprtd->playing) && !atomic_read(&iprtd->capturing))
52 return HRTIMER_NORESTART;
53
54 get_fiq_regs(®s);
55
56 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
57 iprtd->offset = regs.ARM_r8 & 0xffff;
58 else
59 iprtd->offset = regs.ARM_r9 & 0xffff;
60
61 snd_pcm_period_elapsed(substream);
62
63 hrtimer_forward_now(hrt, ns_to_ktime(iprtd->poll_time_ns));
64
65 return HRTIMER_RESTART;
66}
67
68static struct fiq_handler fh = {
69 .name = DRV_NAME,
70};
71
72static int snd_imx_pcm_hw_params(struct snd_soc_component *component,
73 struct snd_pcm_substream *substream,
74 struct snd_pcm_hw_params *params)
75{
76 struct snd_pcm_runtime *runtime = substream->runtime;
77 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
78
79 iprtd->periods = params_periods(params);
80 iprtd->period = params_period_bytes(params);
81 iprtd->offset = 0;
82 iprtd->poll_time_ns = 1000000000 / params_rate(params) *
83 params_period_size(params);
84 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
85
86 return 0;
87}
88
89static int snd_imx_pcm_prepare(struct snd_soc_component *component,
90 struct snd_pcm_substream *substream)
91{
92 struct snd_pcm_runtime *runtime = substream->runtime;
93 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
94 struct pt_regs regs;
95
96 get_fiq_regs(®s);
97 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
98 regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16;
99 else
100 regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16;
101
102 set_fiq_regs(®s);
103
104 return 0;
105}
106
107static int imx_pcm_fiq;
108
109static int snd_imx_pcm_trigger(struct snd_soc_component *component,
110 struct snd_pcm_substream *substream, int cmd)
111{
112 struct snd_pcm_runtime *runtime = substream->runtime;
113 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
114
115 switch (cmd) {
116 case SNDRV_PCM_TRIGGER_START:
117 case SNDRV_PCM_TRIGGER_RESUME:
118 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
119 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
120 atomic_set(&iprtd->playing, 1);
121 else
122 atomic_set(&iprtd->capturing, 1);
123 hrtimer_start(&iprtd->hrt, ns_to_ktime(iprtd->poll_time_ns),
124 HRTIMER_MODE_REL);
125 enable_fiq(imx_pcm_fiq);
126 break;
127
128 case SNDRV_PCM_TRIGGER_STOP:
129 case SNDRV_PCM_TRIGGER_SUSPEND:
130 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
131 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
132 atomic_set(&iprtd->playing, 0);
133 else
134 atomic_set(&iprtd->capturing, 0);
135 if (!atomic_read(&iprtd->playing) &&
136 !atomic_read(&iprtd->capturing))
137 disable_fiq(imx_pcm_fiq);
138 break;
139
140 default:
141 return -EINVAL;
142 }
143
144 return 0;
145}
146
147static snd_pcm_uframes_t
148snd_imx_pcm_pointer(struct snd_soc_component *component,
149 struct snd_pcm_substream *substream)
150{
151 struct snd_pcm_runtime *runtime = substream->runtime;
152 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
153
154 return bytes_to_frames(substream->runtime, iprtd->offset);
155}
156
157static const struct snd_pcm_hardware snd_imx_hardware = {
158 .info = SNDRV_PCM_INFO_INTERLEAVED |
159 SNDRV_PCM_INFO_BLOCK_TRANSFER |
160 SNDRV_PCM_INFO_MMAP |
161 SNDRV_PCM_INFO_MMAP_VALID |
162 SNDRV_PCM_INFO_PAUSE |
163 SNDRV_PCM_INFO_RESUME,
164 .formats = SNDRV_PCM_FMTBIT_S16_LE,
165 .buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
166 .period_bytes_min = 128,
167 .period_bytes_max = 16 * 1024,
168 .periods_min = 4,
169 .periods_max = 255,
170 .fifo_size = 0,
171};
172
173static int snd_imx_open(struct snd_soc_component *component,
174 struct snd_pcm_substream *substream)
175{
176 struct snd_pcm_runtime *runtime = substream->runtime;
177 struct imx_pcm_runtime_data *iprtd;
178 int ret;
179
180 iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
181 if (iprtd == NULL)
182 return -ENOMEM;
183 runtime->private_data = iprtd;
184
185 iprtd->substream = substream;
186
187 atomic_set(&iprtd->playing, 0);
188 atomic_set(&iprtd->capturing, 0);
189 hrtimer_init(&iprtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
190 iprtd->hrt.function = snd_hrtimer_callback;
191
192 ret = snd_pcm_hw_constraint_integer(substream->runtime,
193 SNDRV_PCM_HW_PARAM_PERIODS);
194 if (ret < 0) {
195 kfree(iprtd);
196 return ret;
197 }
198
199 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
200 return 0;
201}
202
203static int snd_imx_close(struct snd_soc_component *component,
204 struct snd_pcm_substream *substream)
205{
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 struct imx_pcm_runtime_data *iprtd = runtime->private_data;
208
209 hrtimer_cancel(&iprtd->hrt);
210
211 kfree(iprtd);
212
213 return 0;
214}
215
216static int snd_imx_pcm_mmap(struct snd_soc_component *component,
217 struct snd_pcm_substream *substream,
218 struct vm_area_struct *vma)
219{
220 struct snd_pcm_runtime *runtime = substream->runtime;
221 int ret;
222
223 ret = dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
224 runtime->dma_addr, runtime->dma_bytes);
225
226 pr_debug("%s: ret: %d %p %pad 0x%08zx\n", __func__, ret,
227 runtime->dma_area,
228 &runtime->dma_addr,
229 runtime->dma_bytes);
230 return ret;
231}
232
233static int imx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
234{
235 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
236 struct snd_dma_buffer *buf = &substream->dma_buffer;
237 size_t size = IMX_SSI_DMABUF_SIZE;
238
239 buf->dev.type = SNDRV_DMA_TYPE_DEV;
240 buf->dev.dev = pcm->card->dev;
241 buf->private_data = NULL;
242 buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
243 if (!buf->area)
244 return -ENOMEM;
245 buf->bytes = size;
246
247 return 0;
248}
249
250static int imx_pcm_new(struct snd_soc_pcm_runtime *rtd)
251{
252 struct snd_card *card = rtd->card->snd_card;
253 struct snd_pcm *pcm = rtd->pcm;
254 int ret;
255
256 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
257 if (ret)
258 return ret;
259
260 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
261 ret = imx_pcm_preallocate_dma_buffer(pcm,
262 SNDRV_PCM_STREAM_PLAYBACK);
263 if (ret)
264 return ret;
265 }
266
267 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
268 ret = imx_pcm_preallocate_dma_buffer(pcm,
269 SNDRV_PCM_STREAM_CAPTURE);
270 if (ret)
271 return ret;
272 }
273
274 return 0;
275}
276
277static int ssi_irq;
278
279static int snd_imx_pcm_new(struct snd_soc_component *component,
280 struct snd_soc_pcm_runtime *rtd)
281{
282 struct snd_pcm *pcm = rtd->pcm;
283 struct snd_pcm_substream *substream;
284 int ret;
285
286 ret = imx_pcm_new(rtd);
287 if (ret)
288 return ret;
289
290 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
291 if (substream) {
292 struct snd_dma_buffer *buf = &substream->dma_buffer;
293
294 imx_ssi_fiq_tx_buffer = (unsigned long)buf->area;
295 }
296
297 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
298 if (substream) {
299 struct snd_dma_buffer *buf = &substream->dma_buffer;
300
301 imx_ssi_fiq_rx_buffer = (unsigned long)buf->area;
302 }
303
304 set_fiq_handler(&imx_ssi_fiq_start,
305 &imx_ssi_fiq_end - &imx_ssi_fiq_start);
306
307 return 0;
308}
309
310static void imx_pcm_free(struct snd_pcm *pcm)
311{
312 struct snd_pcm_substream *substream;
313 struct snd_dma_buffer *buf;
314 int stream;
315
316 for (stream = 0; stream < 2; stream++) {
317 substream = pcm->streams[stream].substream;
318 if (!substream)
319 continue;
320
321 buf = &substream->dma_buffer;
322 if (!buf->area)
323 continue;
324
325 dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
326 buf->area = NULL;
327 }
328}
329
330static void snd_imx_pcm_free(struct snd_soc_component *component,
331 struct snd_pcm *pcm)
332{
333 mxc_set_irq_fiq(ssi_irq, 0);
334 release_fiq(&fh);
335 imx_pcm_free(pcm);
336}
337
338static const struct snd_soc_component_driver imx_soc_component_fiq = {
339 .open = snd_imx_open,
340 .close = snd_imx_close,
341 .hw_params = snd_imx_pcm_hw_params,
342 .prepare = snd_imx_pcm_prepare,
343 .trigger = snd_imx_pcm_trigger,
344 .pointer = snd_imx_pcm_pointer,
345 .mmap = snd_imx_pcm_mmap,
346 .pcm_construct = snd_imx_pcm_new,
347 .pcm_destruct = snd_imx_pcm_free,
348};
349
350int imx_pcm_fiq_init(struct platform_device *pdev,
351 struct imx_pcm_fiq_params *params)
352{
353 int ret;
354
355 ret = claim_fiq(&fh);
356 if (ret) {
357 dev_err(&pdev->dev, "failed to claim fiq: %d", ret);
358 return ret;
359 }
360
361 mxc_set_irq_fiq(params->irq, 1);
362 ssi_irq = params->irq;
363
364 imx_pcm_fiq = params->irq;
365
366 imx_ssi_fiq_base = (unsigned long)params->base;
367
368 params->dma_params_tx->maxburst = 4;
369 params->dma_params_rx->maxburst = 6;
370
371 ret = devm_snd_soc_register_component(&pdev->dev, &imx_soc_component_fiq,
372 NULL, 0);
373 if (ret)
374 goto failed_register;
375
376 return 0;
377
378failed_register:
379 mxc_set_irq_fiq(ssi_irq, 0);
380 release_fiq(&fh);
381
382 return ret;
383}
384EXPORT_SYMBOL_GPL(imx_pcm_fiq_init);
385
386void imx_pcm_fiq_exit(struct platform_device *pdev)
387{
388}
389EXPORT_SYMBOL_GPL(imx_pcm_fiq_exit);
390
391MODULE_LICENSE("GPL");
392