1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/debugfs.h>
14#include <linux/dma-mapping.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/timer.h>
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
24#include <sound/soc-dai.h>
25
26#include "cygnus-ssp.h"
27
28
29
30#define INTH_R5F_STATUS_OFFSET 0x040
31#define INTH_R5F_CLEAR_OFFSET 0x048
32#define INTH_R5F_MASK_SET_OFFSET 0x050
33#define INTH_R5F_MASK_CLEAR_OFFSET 0x054
34
35#define BF_REARM_FREE_MARK_OFFSET 0x344
36#define BF_REARM_FULL_MARK_OFFSET 0x348
37
38
39
40#define SRC_RBUF_0_RDADDR_OFFSET 0x500
41#define SRC_RBUF_1_RDADDR_OFFSET 0x518
42#define SRC_RBUF_2_RDADDR_OFFSET 0x530
43#define SRC_RBUF_3_RDADDR_OFFSET 0x548
44#define SRC_RBUF_4_RDADDR_OFFSET 0x560
45#define SRC_RBUF_5_RDADDR_OFFSET 0x578
46#define SRC_RBUF_6_RDADDR_OFFSET 0x590
47
48
49#define SRC_RBUF_0_WRADDR_OFFSET 0x504
50#define SRC_RBUF_1_WRADDR_OFFSET 0x51c
51#define SRC_RBUF_2_WRADDR_OFFSET 0x534
52#define SRC_RBUF_3_WRADDR_OFFSET 0x54c
53#define SRC_RBUF_4_WRADDR_OFFSET 0x564
54#define SRC_RBUF_5_WRADDR_OFFSET 0x57c
55#define SRC_RBUF_6_WRADDR_OFFSET 0x594
56
57
58#define SRC_RBUF_0_BASEADDR_OFFSET 0x508
59#define SRC_RBUF_1_BASEADDR_OFFSET 0x520
60#define SRC_RBUF_2_BASEADDR_OFFSET 0x538
61#define SRC_RBUF_3_BASEADDR_OFFSET 0x550
62#define SRC_RBUF_4_BASEADDR_OFFSET 0x568
63#define SRC_RBUF_5_BASEADDR_OFFSET 0x580
64#define SRC_RBUF_6_BASEADDR_OFFSET 0x598
65
66
67#define SRC_RBUF_0_ENDADDR_OFFSET 0x50c
68#define SRC_RBUF_1_ENDADDR_OFFSET 0x524
69#define SRC_RBUF_2_ENDADDR_OFFSET 0x53c
70#define SRC_RBUF_3_ENDADDR_OFFSET 0x554
71#define SRC_RBUF_4_ENDADDR_OFFSET 0x56c
72#define SRC_RBUF_5_ENDADDR_OFFSET 0x584
73#define SRC_RBUF_6_ENDADDR_OFFSET 0x59c
74
75
76#define SRC_RBUF_0_FREE_MARK_OFFSET 0x510
77#define SRC_RBUF_1_FREE_MARK_OFFSET 0x528
78#define SRC_RBUF_2_FREE_MARK_OFFSET 0x540
79#define SRC_RBUF_3_FREE_MARK_OFFSET 0x558
80#define SRC_RBUF_4_FREE_MARK_OFFSET 0x570
81#define SRC_RBUF_5_FREE_MARK_OFFSET 0x588
82#define SRC_RBUF_6_FREE_MARK_OFFSET 0x5a0
83
84
85#define DST_RBUF_0_RDADDR_OFFSET 0x5c0
86#define DST_RBUF_1_RDADDR_OFFSET 0x5d8
87#define DST_RBUF_2_RDADDR_OFFSET 0x5f0
88#define DST_RBUF_3_RDADDR_OFFSET 0x608
89#define DST_RBUF_4_RDADDR_OFFSET 0x620
90#define DST_RBUF_5_RDADDR_OFFSET 0x638
91
92
93#define DST_RBUF_0_WRADDR_OFFSET 0x5c4
94#define DST_RBUF_1_WRADDR_OFFSET 0x5dc
95#define DST_RBUF_2_WRADDR_OFFSET 0x5f4
96#define DST_RBUF_3_WRADDR_OFFSET 0x60c
97#define DST_RBUF_4_WRADDR_OFFSET 0x624
98#define DST_RBUF_5_WRADDR_OFFSET 0x63c
99
100
101#define DST_RBUF_0_BASEADDR_OFFSET 0x5c8
102#define DST_RBUF_1_BASEADDR_OFFSET 0x5e0
103#define DST_RBUF_2_BASEADDR_OFFSET 0x5f8
104#define DST_RBUF_3_BASEADDR_OFFSET 0x610
105#define DST_RBUF_4_BASEADDR_OFFSET 0x628
106#define DST_RBUF_5_BASEADDR_OFFSET 0x640
107
108
109#define DST_RBUF_0_ENDADDR_OFFSET 0x5cc
110#define DST_RBUF_1_ENDADDR_OFFSET 0x5e4
111#define DST_RBUF_2_ENDADDR_OFFSET 0x5fc
112#define DST_RBUF_3_ENDADDR_OFFSET 0x614
113#define DST_RBUF_4_ENDADDR_OFFSET 0x62c
114#define DST_RBUF_5_ENDADDR_OFFSET 0x644
115
116
117#define DST_RBUF_0_FULL_MARK_OFFSET 0x5d0
118#define DST_RBUF_1_FULL_MARK_OFFSET 0x5e8
119#define DST_RBUF_2_FULL_MARK_OFFSET 0x600
120#define DST_RBUF_3_FULL_MARK_OFFSET 0x618
121#define DST_RBUF_4_FULL_MARK_OFFSET 0x630
122#define DST_RBUF_5_FULL_MARK_OFFSET 0x648
123
124
125
126
127#define ESR0_STATUS_OFFSET 0x900
128#define ESR1_STATUS_OFFSET 0x918
129#define ESR2_STATUS_OFFSET 0x930
130#define ESR3_STATUS_OFFSET 0x948
131#define ESR4_STATUS_OFFSET 0x960
132
133
134#define ESR0_STATUS_CLR_OFFSET 0x908
135#define ESR1_STATUS_CLR_OFFSET 0x920
136#define ESR2_STATUS_CLR_OFFSET 0x938
137#define ESR3_STATUS_CLR_OFFSET 0x950
138#define ESR4_STATUS_CLR_OFFSET 0x968
139
140
141#define ESR0_MASK_STATUS_OFFSET 0x90c
142#define ESR1_MASK_STATUS_OFFSET 0x924
143#define ESR2_MASK_STATUS_OFFSET 0x93c
144#define ESR3_MASK_STATUS_OFFSET 0x954
145#define ESR4_MASK_STATUS_OFFSET 0x96c
146
147
148#define ESR0_MASK_SET_OFFSET 0x910
149#define ESR1_MASK_SET_OFFSET 0x928
150#define ESR2_MASK_SET_OFFSET 0x940
151#define ESR3_MASK_SET_OFFSET 0x958
152#define ESR4_MASK_SET_OFFSET 0x970
153
154
155#define ESR0_MASK_CLR_OFFSET 0x914
156#define ESR1_MASK_CLR_OFFSET 0x92c
157#define ESR2_MASK_CLR_OFFSET 0x944
158#define ESR3_MASK_CLR_OFFSET 0x95c
159#define ESR4_MASK_CLR_OFFSET 0x974
160
161
162#define R5F_ESR0_SHIFT 0
163#define R5F_ESR1_SHIFT 1
164#define R5F_ESR2_SHIFT 2
165#define R5F_ESR3_SHIFT 3
166#define R5F_ESR4_SHIFT 4
167
168
169
170#define ANY_PLAYBACK_IRQ (BIT(R5F_ESR0_SHIFT) | \
171 BIT(R5F_ESR1_SHIFT) | \
172 BIT(R5F_ESR3_SHIFT))
173
174
175#define ANY_CAPTURE_IRQ (BIT(R5F_ESR2_SHIFT) | BIT(R5F_ESR4_SHIFT))
176
177
178
179
180
181#define PERIOD_BYTES_MIN 0x100
182
183static const struct snd_pcm_hardware cygnus_pcm_hw = {
184 .info = SNDRV_PCM_INFO_MMAP |
185 SNDRV_PCM_INFO_MMAP_VALID |
186 SNDRV_PCM_INFO_INTERLEAVED,
187 .formats = SNDRV_PCM_FMTBIT_S16_LE |
188 SNDRV_PCM_FMTBIT_S32_LE,
189
190
191 .period_bytes_min = PERIOD_BYTES_MIN,
192 .period_bytes_max = 0x10000,
193
194
195 .periods_min = 2,
196 .periods_max = 8,
197
198
199
200
201
202 .buffer_bytes_max = 4 * 0x8000,
203};
204
205static u64 cygnus_dma_dmamask = DMA_BIT_MASK(32);
206
207static struct cygnus_aio_port *cygnus_dai_get_dma_data(
208 struct snd_pcm_substream *substream)
209{
210 struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
211
212 return snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(soc_runtime, 0), substream);
213}
214
215static void ringbuf_set_initial(void __iomem *audio_io,
216 struct ringbuf_regs *p_rbuf,
217 bool is_playback,
218 u32 start,
219 u32 periodsize,
220 u32 bufsize)
221{
222 u32 initial_rd;
223 u32 initial_wr;
224 u32 end;
225 u32 fmark_val;
226
227 p_rbuf->period_bytes = periodsize;
228 p_rbuf->buf_size = bufsize;
229
230 if (is_playback) {
231
232 initial_rd = start;
233 initial_wr = initial_rd ^ BIT(31);
234 } else {
235
236 initial_wr = start;
237 initial_rd = initial_wr;
238 }
239
240 end = start + bufsize - 1;
241
242
243
244
245
246
247 fmark_val = periodsize - PERIOD_BYTES_MIN;
248
249 writel(start, audio_io + p_rbuf->baseaddr);
250 writel(end, audio_io + p_rbuf->endaddr);
251 writel(fmark_val, audio_io + p_rbuf->fmark);
252 writel(initial_rd, audio_io + p_rbuf->rdaddr);
253 writel(initial_wr, audio_io + p_rbuf->wraddr);
254}
255
256static int configure_ringbuf_regs(struct snd_pcm_substream *substream)
257{
258 struct cygnus_aio_port *aio;
259 struct ringbuf_regs *p_rbuf;
260 int status = 0;
261
262 aio = cygnus_dai_get_dma_data(substream);
263
264
265 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
266 p_rbuf = &aio->play_rb_regs;
267
268 switch (aio->portnum) {
269 case 0:
270 *p_rbuf = RINGBUF_REG_PLAYBACK(0);
271 break;
272 case 1:
273 *p_rbuf = RINGBUF_REG_PLAYBACK(2);
274 break;
275 case 2:
276 *p_rbuf = RINGBUF_REG_PLAYBACK(4);
277 break;
278 case 3:
279 *p_rbuf = RINGBUF_REG_PLAYBACK(6);
280 break;
281 default:
282 status = -EINVAL;
283 }
284 } else {
285 p_rbuf = &aio->capture_rb_regs;
286
287 switch (aio->portnum) {
288 case 0:
289 *p_rbuf = RINGBUF_REG_CAPTURE(0);
290 break;
291 case 1:
292 *p_rbuf = RINGBUF_REG_CAPTURE(2);
293 break;
294 case 2:
295 *p_rbuf = RINGBUF_REG_CAPTURE(4);
296 break;
297 default:
298 status = -EINVAL;
299 }
300 }
301
302 return status;
303}
304
305static struct ringbuf_regs *get_ringbuf(struct snd_pcm_substream *substream)
306{
307 struct cygnus_aio_port *aio;
308 struct ringbuf_regs *p_rbuf = NULL;
309
310 aio = cygnus_dai_get_dma_data(substream);
311
312 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
313 p_rbuf = &aio->play_rb_regs;
314 else
315 p_rbuf = &aio->capture_rb_regs;
316
317 return p_rbuf;
318}
319
320static void enable_intr(struct snd_pcm_substream *substream)
321{
322 struct cygnus_aio_port *aio;
323 u32 clear_mask;
324
325 aio = cygnus_dai_get_dma_data(substream);
326
327
328 clear_mask = BIT(aio->portnum);
329
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331
332 writel(clear_mask, aio->cygaud->audio + ESR0_STATUS_CLR_OFFSET);
333 writel(clear_mask, aio->cygaud->audio + ESR1_STATUS_CLR_OFFSET);
334 writel(clear_mask, aio->cygaud->audio + ESR3_STATUS_CLR_OFFSET);
335
336 writel(clear_mask, aio->cygaud->audio + ESR0_MASK_CLR_OFFSET);
337 writel(clear_mask, aio->cygaud->audio + ESR1_MASK_CLR_OFFSET);
338 writel(clear_mask, aio->cygaud->audio + ESR3_MASK_CLR_OFFSET);
339
340 writel(ANY_PLAYBACK_IRQ,
341 aio->cygaud->audio + INTH_R5F_MASK_CLEAR_OFFSET);
342 } else {
343 writel(clear_mask, aio->cygaud->audio + ESR2_STATUS_CLR_OFFSET);
344 writel(clear_mask, aio->cygaud->audio + ESR4_STATUS_CLR_OFFSET);
345 writel(clear_mask, aio->cygaud->audio + ESR2_MASK_CLR_OFFSET);
346 writel(clear_mask, aio->cygaud->audio + ESR4_MASK_CLR_OFFSET);
347
348 writel(ANY_CAPTURE_IRQ,
349 aio->cygaud->audio + INTH_R5F_MASK_CLEAR_OFFSET);
350 }
351
352}
353
354static void disable_intr(struct snd_pcm_substream *substream)
355{
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct cygnus_aio_port *aio;
358 u32 set_mask;
359
360 aio = cygnus_dai_get_dma_data(substream);
361
362 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s on port %d\n", __func__, aio->portnum);
363
364
365 set_mask = BIT(aio->portnum);
366
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368
369 writel(set_mask, aio->cygaud->audio + ESR0_MASK_SET_OFFSET);
370 writel(set_mask, aio->cygaud->audio + ESR1_MASK_SET_OFFSET);
371 writel(set_mask, aio->cygaud->audio + ESR3_MASK_SET_OFFSET);
372 } else {
373 writel(set_mask, aio->cygaud->audio + ESR2_MASK_SET_OFFSET);
374 writel(set_mask, aio->cygaud->audio + ESR4_MASK_SET_OFFSET);
375 }
376
377}
378
379static int cygnus_pcm_trigger(struct snd_soc_component *component,
380 struct snd_pcm_substream *substream, int cmd)
381{
382 int ret = 0;
383
384 switch (cmd) {
385 case SNDRV_PCM_TRIGGER_START:
386 case SNDRV_PCM_TRIGGER_RESUME:
387 enable_intr(substream);
388 break;
389
390 case SNDRV_PCM_TRIGGER_STOP:
391 case SNDRV_PCM_TRIGGER_SUSPEND:
392 disable_intr(substream);
393 break;
394 default:
395 ret = -EINVAL;
396 }
397
398 return ret;
399}
400
401static void cygnus_pcm_period_elapsed(struct snd_pcm_substream *substream)
402{
403 struct cygnus_aio_port *aio;
404 struct ringbuf_regs *p_rbuf = NULL;
405 u32 regval;
406
407 aio = cygnus_dai_get_dma_data(substream);
408
409 p_rbuf = get_ringbuf(substream);
410
411
412
413
414
415 snd_pcm_period_elapsed(substream);
416
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
418
419 regval = readl(aio->cygaud->audio + p_rbuf->rdaddr);
420 regval = regval ^ BIT(31);
421 writel(regval, aio->cygaud->audio + p_rbuf->wraddr);
422 } else {
423
424 regval = readl(aio->cygaud->audio + p_rbuf->wraddr);
425 writel(regval, aio->cygaud->audio + p_rbuf->rdaddr);
426 }
427}
428
429
430
431
432
433
434
435
436static void handle_playback_irq(struct cygnus_audio *cygaud)
437{
438 void __iomem *audio_io;
439 u32 port;
440 u32 esr_status0, esr_status1, esr_status3;
441
442 audio_io = cygaud->audio;
443
444
445
446
447
448
449
450 esr_status0 = readl(audio_io + ESR0_STATUS_OFFSET);
451 esr_status0 &= ~readl(audio_io + ESR0_MASK_STATUS_OFFSET);
452 esr_status1 = readl(audio_io + ESR1_STATUS_OFFSET);
453 esr_status1 &= ~readl(audio_io + ESR1_MASK_STATUS_OFFSET);
454 esr_status3 = readl(audio_io + ESR3_STATUS_OFFSET);
455 esr_status3 &= ~readl(audio_io + ESR3_MASK_STATUS_OFFSET);
456
457 for (port = 0; port < CYGNUS_MAX_PLAYBACK_PORTS; port++) {
458 u32 esrmask = BIT(port);
459
460
461
462
463
464
465
466
467 if ((esrmask & esr_status1) || (esrmask & esr_status0)) {
468 dev_dbg(cygaud->dev,
469 "Underrun: esr0=0x%x, esr1=0x%x esr3=0x%x\n",
470 esr_status0, esr_status1, esr_status3);
471 }
472
473
474
475
476
477 if (esrmask & esr_status3) {
478 struct snd_pcm_substream *playstr;
479
480 playstr = cygaud->portinfo[port].play_stream;
481 cygnus_pcm_period_elapsed(playstr);
482 }
483 }
484
485
486 writel(esr_status0, audio_io + ESR0_STATUS_CLR_OFFSET);
487 writel(esr_status1, audio_io + ESR1_STATUS_CLR_OFFSET);
488 writel(esr_status3, audio_io + ESR3_STATUS_CLR_OFFSET);
489
490 writel(esr_status3, audio_io + BF_REARM_FREE_MARK_OFFSET);
491}
492
493
494
495
496
497
498
499static void handle_capture_irq(struct cygnus_audio *cygaud)
500{
501 void __iomem *audio_io;
502 u32 port;
503 u32 esr_status2, esr_status4;
504
505 audio_io = cygaud->audio;
506
507
508
509
510
511
512
513 esr_status2 = readl(audio_io + ESR2_STATUS_OFFSET);
514 esr_status2 &= ~readl(audio_io + ESR2_MASK_STATUS_OFFSET);
515 esr_status4 = readl(audio_io + ESR4_STATUS_OFFSET);
516 esr_status4 &= ~readl(audio_io + ESR4_MASK_STATUS_OFFSET);
517
518 for (port = 0; port < CYGNUS_MAX_CAPTURE_PORTS; port++) {
519 u32 esrmask = BIT(port);
520
521
522
523
524
525
526
527
528 if (esrmask & esr_status2)
529 dev_dbg(cygaud->dev,
530 "Overflow: esr2=0x%x\n", esr_status2);
531
532 if (esrmask & esr_status4) {
533 struct snd_pcm_substream *capstr;
534
535 capstr = cygaud->portinfo[port].capture_stream;
536 cygnus_pcm_period_elapsed(capstr);
537 }
538 }
539
540 writel(esr_status2, audio_io + ESR2_STATUS_CLR_OFFSET);
541 writel(esr_status4, audio_io + ESR4_STATUS_CLR_OFFSET);
542
543 writel(esr_status4, audio_io + BF_REARM_FULL_MARK_OFFSET);
544}
545
546static irqreturn_t cygnus_dma_irq(int irq, void *data)
547{
548 u32 r5_status;
549 struct cygnus_audio *cygaud = data;
550
551
552
553
554
555
556
557
558
559 r5_status = readl(cygaud->audio + INTH_R5F_STATUS_OFFSET);
560
561 if (!(r5_status & (ANY_PLAYBACK_IRQ | ANY_CAPTURE_IRQ)))
562 return IRQ_NONE;
563
564
565 if (ANY_PLAYBACK_IRQ & r5_status) {
566 handle_playback_irq(cygaud);
567 writel(ANY_PLAYBACK_IRQ & r5_status,
568 cygaud->audio + INTH_R5F_CLEAR_OFFSET);
569 }
570
571
572 if (ANY_CAPTURE_IRQ & r5_status) {
573 handle_capture_irq(cygaud);
574 writel(ANY_CAPTURE_IRQ & r5_status,
575 cygaud->audio + INTH_R5F_CLEAR_OFFSET);
576 }
577
578 return IRQ_HANDLED;
579}
580
581static int cygnus_pcm_open(struct snd_soc_component *component,
582 struct snd_pcm_substream *substream)
583{
584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
585 struct snd_pcm_runtime *runtime = substream->runtime;
586 struct cygnus_aio_port *aio;
587 int ret;
588
589 aio = cygnus_dai_get_dma_data(substream);
590 if (!aio)
591 return -ENODEV;
592
593 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s port %d\n", __func__, aio->portnum);
594
595 snd_soc_set_runtime_hwparams(substream, &cygnus_pcm_hw);
596
597 ret = snd_pcm_hw_constraint_step(runtime, 0,
598 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, PERIOD_BYTES_MIN);
599 if (ret < 0)
600 return ret;
601
602 ret = snd_pcm_hw_constraint_step(runtime, 0,
603 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, PERIOD_BYTES_MIN);
604 if (ret < 0)
605 return ret;
606
607
608
609
610 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
611 aio->play_stream = substream;
612 else
613 aio->capture_stream = substream;
614
615 return 0;
616}
617
618static int cygnus_pcm_close(struct snd_soc_component *component,
619 struct snd_pcm_substream *substream)
620{
621 struct snd_soc_pcm_runtime *rtd = substream->private_data;
622 struct cygnus_aio_port *aio;
623
624 aio = cygnus_dai_get_dma_data(substream);
625
626 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s port %d\n", __func__, aio->portnum);
627
628 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
629 aio->play_stream = NULL;
630 else
631 aio->capture_stream = NULL;
632
633 if (!aio->play_stream && !aio->capture_stream)
634 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "freed port %d\n", aio->portnum);
635
636 return 0;
637}
638
639static int cygnus_pcm_hw_params(struct snd_soc_component *component,
640 struct snd_pcm_substream *substream,
641 struct snd_pcm_hw_params *params)
642{
643 struct snd_soc_pcm_runtime *rtd = substream->private_data;
644 struct snd_pcm_runtime *runtime = substream->runtime;
645 struct cygnus_aio_port *aio;
646
647 aio = cygnus_dai_get_dma_data(substream);
648 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s port %d\n", __func__, aio->portnum);
649
650 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
651 runtime->dma_bytes = params_buffer_bytes(params);
652
653 return 0;
654}
655
656static int cygnus_pcm_hw_free(struct snd_soc_component *component,
657 struct snd_pcm_substream *substream)
658{
659 struct snd_soc_pcm_runtime *rtd = substream->private_data;
660 struct cygnus_aio_port *aio;
661
662 aio = cygnus_dai_get_dma_data(substream);
663 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s port %d\n", __func__, aio->portnum);
664
665 snd_pcm_set_runtime_buffer(substream, NULL);
666 return 0;
667}
668
669static int cygnus_pcm_prepare(struct snd_soc_component *component,
670 struct snd_pcm_substream *substream)
671{
672 struct snd_soc_pcm_runtime *rtd = substream->private_data;
673 struct snd_pcm_runtime *runtime = substream->runtime;
674 struct cygnus_aio_port *aio;
675 unsigned long bufsize, periodsize;
676 bool is_play;
677 u32 start;
678 struct ringbuf_regs *p_rbuf = NULL;
679
680 aio = cygnus_dai_get_dma_data(substream);
681 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s port %d\n", __func__, aio->portnum);
682
683 bufsize = snd_pcm_lib_buffer_bytes(substream);
684 periodsize = snd_pcm_lib_period_bytes(substream);
685
686 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s (buf_size %lu) (period_size %lu)\n",
687 __func__, bufsize, periodsize);
688
689 configure_ringbuf_regs(substream);
690
691 p_rbuf = get_ringbuf(substream);
692
693 start = runtime->dma_addr;
694
695 is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 1 : 0;
696
697 ringbuf_set_initial(aio->cygaud->audio, p_rbuf, is_play, start,
698 periodsize, bufsize);
699
700 return 0;
701}
702
703static snd_pcm_uframes_t cygnus_pcm_pointer(struct snd_soc_component *component,
704 struct snd_pcm_substream *substream)
705{
706 struct cygnus_aio_port *aio;
707 unsigned int res = 0, cur = 0, base = 0;
708 struct ringbuf_regs *p_rbuf = NULL;
709
710 aio = cygnus_dai_get_dma_data(substream);
711
712
713
714
715
716 p_rbuf = get_ringbuf(substream);
717 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
718 cur = readl(aio->cygaud->audio + p_rbuf->rdaddr);
719 else
720 cur = readl(aio->cygaud->audio + p_rbuf->wraddr);
721
722 base = readl(aio->cygaud->audio + p_rbuf->baseaddr);
723
724
725
726
727
728 res = (cur & 0x7fffffff) - (base & 0x7fffffff);
729
730 return bytes_to_frames(substream->runtime, res);
731}
732
733static int cygnus_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
734{
735 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
736 struct snd_soc_pcm_runtime *rtd = substream->private_data;
737 struct snd_dma_buffer *buf = &substream->dma_buffer;
738 size_t size;
739
740 size = cygnus_pcm_hw.buffer_bytes_max;
741
742 buf->dev.type = SNDRV_DMA_TYPE_DEV;
743 buf->dev.dev = pcm->card->dev;
744 buf->private_data = NULL;
745 buf->area = dma_alloc_coherent(pcm->card->dev, size,
746 &buf->addr, GFP_KERNEL);
747
748 dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "%s: size 0x%zx @ %pK\n",
749 __func__, size, buf->area);
750
751 if (!buf->area) {
752 dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "%s: dma_alloc failed\n", __func__);
753 return -ENOMEM;
754 }
755 buf->bytes = size;
756
757 return 0;
758}
759
760static void cygnus_dma_free_dma_buffers(struct snd_soc_component *component,
761 struct snd_pcm *pcm)
762{
763 struct snd_pcm_substream *substream;
764 struct snd_dma_buffer *buf;
765
766 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
767 if (substream) {
768 buf = &substream->dma_buffer;
769 if (buf->area) {
770 dma_free_coherent(pcm->card->dev, buf->bytes,
771 buf->area, buf->addr);
772 buf->area = NULL;
773 }
774 }
775
776 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
777 if (substream) {
778 buf = &substream->dma_buffer;
779 if (buf->area) {
780 dma_free_coherent(pcm->card->dev, buf->bytes,
781 buf->area, buf->addr);
782 buf->area = NULL;
783 }
784 }
785}
786
787static int cygnus_dma_new(struct snd_soc_component *component,
788 struct snd_soc_pcm_runtime *rtd)
789{
790 struct snd_card *card = rtd->card->snd_card;
791 struct snd_pcm *pcm = rtd->pcm;
792 int ret;
793
794 if (!card->dev->dma_mask)
795 card->dev->dma_mask = &cygnus_dma_dmamask;
796 if (!card->dev->coherent_dma_mask)
797 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
798
799 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
800 ret = cygnus_pcm_preallocate_dma_buffer(pcm,
801 SNDRV_PCM_STREAM_PLAYBACK);
802 if (ret)
803 return ret;
804 }
805
806 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
807 ret = cygnus_pcm_preallocate_dma_buffer(pcm,
808 SNDRV_PCM_STREAM_CAPTURE);
809 if (ret) {
810 cygnus_dma_free_dma_buffers(component, pcm);
811 return ret;
812 }
813 }
814
815 return 0;
816}
817
818static struct snd_soc_component_driver cygnus_soc_platform = {
819 .open = cygnus_pcm_open,
820 .close = cygnus_pcm_close,
821 .hw_params = cygnus_pcm_hw_params,
822 .hw_free = cygnus_pcm_hw_free,
823 .prepare = cygnus_pcm_prepare,
824 .trigger = cygnus_pcm_trigger,
825 .pointer = cygnus_pcm_pointer,
826 .pcm_construct = cygnus_dma_new,
827 .pcm_destruct = cygnus_dma_free_dma_buffers,
828};
829
830int cygnus_soc_platform_register(struct device *dev,
831 struct cygnus_audio *cygaud)
832{
833 int rc = 0;
834
835 dev_dbg(dev, "%s Enter\n", __func__);
836
837 rc = devm_request_irq(dev, cygaud->irq_num, cygnus_dma_irq,
838 IRQF_SHARED, "cygnus-audio", cygaud);
839 if (rc) {
840 dev_err(dev, "%s request_irq error %d\n", __func__, rc);
841 return rc;
842 }
843
844 rc = devm_snd_soc_register_component(dev, &cygnus_soc_platform,
845 NULL, 0);
846 if (rc) {
847 dev_err(dev, "%s failed\n", __func__);
848 return rc;
849 }
850
851 return 0;
852}
853
854int cygnus_soc_platform_unregister(struct device *dev)
855{
856 return 0;
857}
858
859MODULE_LICENSE("GPL v2");
860MODULE_AUTHOR("Broadcom");
861MODULE_DESCRIPTION("Cygnus ASoC PCM module");
862