ASoC: fsi: use dmaengine helper functions
[deliverable/linux.git] / sound / soc / sh / fsi.c
1 /*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
3 *
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ssi.c
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/io.h>
19 #include <linux/scatterlist.h>
20 #include <linux/sh_dma.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <sound/soc.h>
24 #include <sound/sh_fsi.h>
25
26 /* PortA/PortB register */
27 #define REG_DO_FMT 0x0000
28 #define REG_DOFF_CTL 0x0004
29 #define REG_DOFF_ST 0x0008
30 #define REG_DI_FMT 0x000C
31 #define REG_DIFF_CTL 0x0010
32 #define REG_DIFF_ST 0x0014
33 #define REG_CKG1 0x0018
34 #define REG_CKG2 0x001C
35 #define REG_DIDT 0x0020
36 #define REG_DODT 0x0024
37 #define REG_MUTE_ST 0x0028
38 #define REG_OUT_DMAC 0x002C
39 #define REG_OUT_SEL 0x0030
40 #define REG_IN_DMAC 0x0038
41
42 /* master register */
43 #define MST_CLK_RST 0x0210
44 #define MST_SOFT_RST 0x0214
45 #define MST_FIFO_SZ 0x0218
46
47 /* core register (depend on FSI version) */
48 #define A_MST_CTLR 0x0180
49 #define B_MST_CTLR 0x01A0
50 #define CPU_INT_ST 0x01F4
51 #define CPU_IEMSK 0x01F8
52 #define CPU_IMSK 0x01FC
53 #define INT_ST 0x0200
54 #define IEMSK 0x0204
55 #define IMSK 0x0208
56
57 /* DO_FMT */
58 /* DI_FMT */
59 #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
60 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
61 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
62 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
63
64 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
65 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
66 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
67
68 #define CR_MONO (0x0 << 4)
69 #define CR_MONO_D (0x1 << 4)
70 #define CR_PCM (0x2 << 4)
71 #define CR_I2S (0x3 << 4)
72 #define CR_TDM (0x4 << 4)
73 #define CR_TDM_D (0x5 << 4)
74
75 /* OUT_DMAC */
76 /* IN_DMAC */
77 #define VDMD_MASK (0x3 << 4)
78 #define VDMD_FRONT (0x0 << 4) /* Package in front */
79 #define VDMD_BACK (0x1 << 4) /* Package in back */
80 #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
81
82 #define DMA_ON (0x1 << 0)
83
84 /* DOFF_CTL */
85 /* DIFF_CTL */
86 #define IRQ_HALF 0x00100000
87 #define FIFO_CLR 0x00000001
88
89 /* DOFF_ST */
90 #define ERR_OVER 0x00000010
91 #define ERR_UNDER 0x00000001
92 #define ST_ERR (ERR_OVER | ERR_UNDER)
93
94 /* CKG1 */
95 #define ACKMD_MASK 0x00007000
96 #define BPFMD_MASK 0x00000700
97 #define DIMD (1 << 4)
98 #define DOMD (1 << 0)
99
100 /* A/B MST_CTLR */
101 #define BP (1 << 4) /* Fix the signal of Biphase output */
102 #define SE (1 << 0) /* Fix the master clock */
103
104 /* CLK_RST */
105 #define CRB (1 << 4)
106 #define CRA (1 << 0)
107
108 /* IO SHIFT / MACRO */
109 #define BI_SHIFT 12
110 #define BO_SHIFT 8
111 #define AI_SHIFT 4
112 #define AO_SHIFT 0
113 #define AB_IO(param, shift) (param << shift)
114
115 /* SOFT_RST */
116 #define PBSR (1 << 12) /* Port B Software Reset */
117 #define PASR (1 << 8) /* Port A Software Reset */
118 #define IR (1 << 4) /* Interrupt Reset */
119 #define FSISR (1 << 0) /* Software Reset */
120
121 /* OUT_SEL (FSI2) */
122 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
123 /* 1: Biphase and serial */
124
125 /* FIFO_SZ */
126 #define FIFO_SZ_MASK 0x7
127
128 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
129
130 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
131
132 typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
133
134 /*
135 * bus options
136 *
137 * 0x000000BA
138 *
139 * A : sample widtht 16bit setting
140 * B : sample widtht 24bit setting
141 */
142
143 #define SHIFT_16DATA 0
144 #define SHIFT_24DATA 4
145
146 #define PACKAGE_24BITBUS_BACK 0
147 #define PACKAGE_24BITBUS_FRONT 1
148 #define PACKAGE_16BITBUS_STREAM 2
149
150 #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
151 #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
152
153 /*
154 * FSI driver use below type name for variable
155 *
156 * xxx_num : number of data
157 * xxx_pos : position of data
158 * xxx_capa : capacity of data
159 */
160
161 /*
162 * period/frame/sample image
163 *
164 * ex) PCM (2ch)
165 *
166 * period pos period pos
167 * [n] [n + 1]
168 * |<-------------------- period--------------------->|
169 * ==|============================================ ... =|==
170 * | |
171 * ||<----- frame ----->|<------ frame ----->| ... |
172 * |+--------------------+--------------------+- ... |
173 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
174 * |+--------------------+--------------------+- ... |
175 * ==|============================================ ... =|==
176 */
177
178 /*
179 * FSI FIFO image
180 *
181 * | |
182 * | |
183 * | [ sample ] |
184 * | [ sample ] |
185 * | [ sample ] |
186 * | [ sample ] |
187 * --> go to codecs
188 */
189
190 /*
191 * struct
192 */
193
194 struct fsi_stream_handler;
195 struct fsi_stream {
196
197 /*
198 * these are initialized by fsi_stream_init()
199 */
200 struct snd_pcm_substream *substream;
201 int fifo_sample_capa; /* sample capacity of FSI FIFO */
202 int buff_sample_capa; /* sample capacity of ALSA buffer */
203 int buff_sample_pos; /* sample position of ALSA buffer */
204 int period_samples; /* sample number / 1 period */
205 int period_pos; /* current period position */
206 int sample_width; /* sample width */
207 int uerr_num;
208 int oerr_num;
209
210 /*
211 * bus options
212 */
213 u32 bus_option;
214
215 /*
216 * thse are initialized by fsi_handler_init()
217 */
218 struct fsi_stream_handler *handler;
219 struct fsi_priv *priv;
220
221 /*
222 * these are for DMAEngine
223 */
224 struct dma_chan *chan;
225 struct sh_dmae_slave slave; /* see fsi_handler_init() */
226 struct tasklet_struct tasklet;
227 dma_addr_t dma;
228 };
229
230 struct fsi_priv {
231 void __iomem *base;
232 struct fsi_master *master;
233 struct sh_fsi_port_info *info;
234
235 struct fsi_stream playback;
236 struct fsi_stream capture;
237
238 u32 fmt;
239
240 int chan_num:16;
241 int clk_master:1;
242 int spdif:1;
243
244 long rate;
245 };
246
247 struct fsi_stream_handler {
248 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
249 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
250 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
251 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
252 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
253 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
254 int enable);
255 };
256 #define fsi_stream_handler_call(io, func, args...) \
257 (!(io) ? -ENODEV : \
258 !((io)->handler->func) ? 0 : \
259 (io)->handler->func(args))
260
261 struct fsi_core {
262 int ver;
263
264 u32 int_st;
265 u32 iemsk;
266 u32 imsk;
267 u32 a_mclk;
268 u32 b_mclk;
269 };
270
271 struct fsi_master {
272 void __iomem *base;
273 int irq;
274 struct fsi_priv fsia;
275 struct fsi_priv fsib;
276 struct fsi_core *core;
277 spinlock_t lock;
278 };
279
280 static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
281
282 /*
283 * basic read write function
284 */
285
286 static void __fsi_reg_write(u32 __iomem *reg, u32 data)
287 {
288 /* valid data area is 24bit */
289 data &= 0x00ffffff;
290
291 __raw_writel(data, reg);
292 }
293
294 static u32 __fsi_reg_read(u32 __iomem *reg)
295 {
296 return __raw_readl(reg);
297 }
298
299 static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
300 {
301 u32 val = __fsi_reg_read(reg);
302
303 val &= ~mask;
304 val |= data & mask;
305
306 __fsi_reg_write(reg, val);
307 }
308
309 #define fsi_reg_write(p, r, d)\
310 __fsi_reg_write((p->base + REG_##r), d)
311
312 #define fsi_reg_read(p, r)\
313 __fsi_reg_read((p->base + REG_##r))
314
315 #define fsi_reg_mask_set(p, r, m, d)\
316 __fsi_reg_mask_set((p->base + REG_##r), m, d)
317
318 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
319 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
320 static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
321 {
322 u32 ret;
323 unsigned long flags;
324
325 spin_lock_irqsave(&master->lock, flags);
326 ret = __fsi_reg_read(master->base + reg);
327 spin_unlock_irqrestore(&master->lock, flags);
328
329 return ret;
330 }
331
332 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
333 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
334 static void _fsi_master_mask_set(struct fsi_master *master,
335 u32 reg, u32 mask, u32 data)
336 {
337 unsigned long flags;
338
339 spin_lock_irqsave(&master->lock, flags);
340 __fsi_reg_mask_set(master->base + reg, mask, data);
341 spin_unlock_irqrestore(&master->lock, flags);
342 }
343
344 /*
345 * basic function
346 */
347 static int fsi_version(struct fsi_master *master)
348 {
349 return master->core->ver;
350 }
351
352 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
353 {
354 return fsi->master;
355 }
356
357 static int fsi_is_clk_master(struct fsi_priv *fsi)
358 {
359 return fsi->clk_master;
360 }
361
362 static int fsi_is_port_a(struct fsi_priv *fsi)
363 {
364 return fsi->master->base == fsi->base;
365 }
366
367 static int fsi_is_spdif(struct fsi_priv *fsi)
368 {
369 return fsi->spdif;
370 }
371
372 static int fsi_is_play(struct snd_pcm_substream *substream)
373 {
374 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
375 }
376
377 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
378 {
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
380
381 return rtd->cpu_dai;
382 }
383
384 static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
385 {
386 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
387
388 if (dai->id == 0)
389 return &master->fsia;
390 else
391 return &master->fsib;
392 }
393
394 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
395 {
396 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
397 }
398
399 static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
400 {
401 if (!fsi->info)
402 return NULL;
403
404 return fsi->info->set_rate;
405 }
406
407 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
408 {
409 if (!fsi->info)
410 return 0;
411
412 return fsi->info->flags;
413 }
414
415 static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
416 {
417 int is_play = fsi_stream_is_play(fsi, io);
418 int is_porta = fsi_is_port_a(fsi);
419 u32 shift;
420
421 if (is_porta)
422 shift = is_play ? AO_SHIFT : AI_SHIFT;
423 else
424 shift = is_play ? BO_SHIFT : BI_SHIFT;
425
426 return shift;
427 }
428
429 static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
430 {
431 return frames * fsi->chan_num;
432 }
433
434 static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
435 {
436 return samples / fsi->chan_num;
437 }
438
439 static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
440 struct fsi_stream *io)
441 {
442 int is_play = fsi_stream_is_play(fsi, io);
443 u32 status;
444 int frames;
445
446 status = is_play ?
447 fsi_reg_read(fsi, DOFF_ST) :
448 fsi_reg_read(fsi, DIFF_ST);
449
450 frames = 0x1ff & (status >> 8);
451
452 return fsi_frame2sample(fsi, frames);
453 }
454
455 static void fsi_count_fifo_err(struct fsi_priv *fsi)
456 {
457 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
458 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
459
460 if (ostatus & ERR_OVER)
461 fsi->playback.oerr_num++;
462
463 if (ostatus & ERR_UNDER)
464 fsi->playback.uerr_num++;
465
466 if (istatus & ERR_OVER)
467 fsi->capture.oerr_num++;
468
469 if (istatus & ERR_UNDER)
470 fsi->capture.uerr_num++;
471
472 fsi_reg_write(fsi, DOFF_ST, 0);
473 fsi_reg_write(fsi, DIFF_ST, 0);
474 }
475
476 /*
477 * fsi_stream_xx() function
478 */
479 static inline int fsi_stream_is_play(struct fsi_priv *fsi,
480 struct fsi_stream *io)
481 {
482 return &fsi->playback == io;
483 }
484
485 static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
486 struct snd_pcm_substream *substream)
487 {
488 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
489 }
490
491 static int fsi_stream_is_working(struct fsi_priv *fsi,
492 struct fsi_stream *io)
493 {
494 struct fsi_master *master = fsi_get_master(fsi);
495 unsigned long flags;
496 int ret;
497
498 spin_lock_irqsave(&master->lock, flags);
499 ret = !!(io->substream && io->substream->runtime);
500 spin_unlock_irqrestore(&master->lock, flags);
501
502 return ret;
503 }
504
505 static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
506 {
507 return io->priv;
508 }
509
510 static void fsi_stream_init(struct fsi_priv *fsi,
511 struct fsi_stream *io,
512 struct snd_pcm_substream *substream)
513 {
514 struct snd_pcm_runtime *runtime = substream->runtime;
515 struct fsi_master *master = fsi_get_master(fsi);
516 unsigned long flags;
517
518 spin_lock_irqsave(&master->lock, flags);
519 io->substream = substream;
520 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
521 io->buff_sample_pos = 0;
522 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
523 io->period_pos = 0;
524 io->sample_width = samples_to_bytes(runtime, 1);
525 io->bus_option = 0;
526 io->oerr_num = -1; /* ignore 1st err */
527 io->uerr_num = -1; /* ignore 1st err */
528 fsi_stream_handler_call(io, init, fsi, io);
529 spin_unlock_irqrestore(&master->lock, flags);
530 }
531
532 static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
533 {
534 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
535 struct fsi_master *master = fsi_get_master(fsi);
536 unsigned long flags;
537
538 spin_lock_irqsave(&master->lock, flags);
539
540 if (io->oerr_num > 0)
541 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
542
543 if (io->uerr_num > 0)
544 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
545
546 fsi_stream_handler_call(io, quit, fsi, io);
547 io->substream = NULL;
548 io->buff_sample_capa = 0;
549 io->buff_sample_pos = 0;
550 io->period_samples = 0;
551 io->period_pos = 0;
552 io->sample_width = 0;
553 io->bus_option = 0;
554 io->oerr_num = 0;
555 io->uerr_num = 0;
556 spin_unlock_irqrestore(&master->lock, flags);
557 }
558
559 static int fsi_stream_transfer(struct fsi_stream *io)
560 {
561 struct fsi_priv *fsi = fsi_stream_to_priv(io);
562 if (!fsi)
563 return -EIO;
564
565 return fsi_stream_handler_call(io, transfer, fsi, io);
566 }
567
568 #define fsi_stream_start(fsi, io)\
569 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
570
571 #define fsi_stream_stop(fsi, io)\
572 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
573
574 static int fsi_stream_probe(struct fsi_priv *fsi)
575 {
576 struct fsi_stream *io;
577 int ret1, ret2;
578
579 io = &fsi->playback;
580 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
581
582 io = &fsi->capture;
583 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
584
585 if (ret1 < 0)
586 return ret1;
587 if (ret2 < 0)
588 return ret2;
589
590 return 0;
591 }
592
593 static int fsi_stream_remove(struct fsi_priv *fsi)
594 {
595 struct fsi_stream *io;
596 int ret1, ret2;
597
598 io = &fsi->playback;
599 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
600
601 io = &fsi->capture;
602 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
603
604 if (ret1 < 0)
605 return ret1;
606 if (ret2 < 0)
607 return ret2;
608
609 return 0;
610 }
611
612 /*
613 * format/bus/dma setting
614 */
615 static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
616 u32 bus, struct device *dev)
617 {
618 struct fsi_master *master = fsi_get_master(fsi);
619 int is_play = fsi_stream_is_play(fsi, io);
620 u32 fmt = fsi->fmt;
621
622 if (fsi_version(master) >= 2) {
623 u32 dma = 0;
624
625 /*
626 * FSI2 needs DMA/Bus setting
627 */
628 switch (bus) {
629 case PACKAGE_24BITBUS_FRONT:
630 fmt |= CR_BWS_24;
631 dma |= VDMD_FRONT;
632 dev_dbg(dev, "24bit bus / package in front\n");
633 break;
634 case PACKAGE_16BITBUS_STREAM:
635 fmt |= CR_BWS_16;
636 dma |= VDMD_STREAM;
637 dev_dbg(dev, "16bit bus / stream mode\n");
638 break;
639 case PACKAGE_24BITBUS_BACK:
640 default:
641 fmt |= CR_BWS_24;
642 dma |= VDMD_BACK;
643 dev_dbg(dev, "24bit bus / package in back\n");
644 break;
645 }
646
647 if (is_play)
648 fsi_reg_write(fsi, OUT_DMAC, dma);
649 else
650 fsi_reg_write(fsi, IN_DMAC, dma);
651 }
652
653 if (is_play)
654 fsi_reg_write(fsi, DO_FMT, fmt);
655 else
656 fsi_reg_write(fsi, DI_FMT, fmt);
657 }
658
659 /*
660 * irq function
661 */
662
663 static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
664 {
665 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
666 struct fsi_master *master = fsi_get_master(fsi);
667
668 fsi_core_mask_set(master, imsk, data, data);
669 fsi_core_mask_set(master, iemsk, data, data);
670 }
671
672 static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
673 {
674 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
675 struct fsi_master *master = fsi_get_master(fsi);
676
677 fsi_core_mask_set(master, imsk, data, 0);
678 fsi_core_mask_set(master, iemsk, data, 0);
679 }
680
681 static u32 fsi_irq_get_status(struct fsi_master *master)
682 {
683 return fsi_core_read(master, int_st);
684 }
685
686 static void fsi_irq_clear_status(struct fsi_priv *fsi)
687 {
688 u32 data = 0;
689 struct fsi_master *master = fsi_get_master(fsi);
690
691 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
692 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
693
694 /* clear interrupt factor */
695 fsi_core_mask_set(master, int_st, data, 0);
696 }
697
698 /*
699 * SPDIF master clock function
700 *
701 * These functions are used later FSI2
702 */
703 static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
704 {
705 struct fsi_master *master = fsi_get_master(fsi);
706 u32 mask, val;
707
708 mask = BP | SE;
709 val = enable ? mask : 0;
710
711 fsi_is_port_a(fsi) ?
712 fsi_core_mask_set(master, a_mclk, mask, val) :
713 fsi_core_mask_set(master, b_mclk, mask, val);
714 }
715
716 /*
717 * clock function
718 */
719 static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
720 long rate, int enable)
721 {
722 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
723 int ret;
724
725 if (!set_rate)
726 return 0;
727
728 ret = set_rate(dev, rate, enable);
729 if (ret < 0) /* error */
730 return ret;
731
732 if (!enable)
733 return 0;
734
735 if (ret > 0) {
736 u32 data = 0;
737
738 switch (ret & SH_FSI_ACKMD_MASK) {
739 default:
740 /* FALL THROUGH */
741 case SH_FSI_ACKMD_512:
742 data |= (0x0 << 12);
743 break;
744 case SH_FSI_ACKMD_256:
745 data |= (0x1 << 12);
746 break;
747 case SH_FSI_ACKMD_128:
748 data |= (0x2 << 12);
749 break;
750 case SH_FSI_ACKMD_64:
751 data |= (0x3 << 12);
752 break;
753 case SH_FSI_ACKMD_32:
754 data |= (0x4 << 12);
755 break;
756 }
757
758 switch (ret & SH_FSI_BPFMD_MASK) {
759 default:
760 /* FALL THROUGH */
761 case SH_FSI_BPFMD_32:
762 data |= (0x0 << 8);
763 break;
764 case SH_FSI_BPFMD_64:
765 data |= (0x1 << 8);
766 break;
767 case SH_FSI_BPFMD_128:
768 data |= (0x2 << 8);
769 break;
770 case SH_FSI_BPFMD_256:
771 data |= (0x3 << 8);
772 break;
773 case SH_FSI_BPFMD_512:
774 data |= (0x4 << 8);
775 break;
776 case SH_FSI_BPFMD_16:
777 data |= (0x7 << 8);
778 break;
779 }
780
781 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
782 udelay(10);
783 ret = 0;
784 }
785
786 return ret;
787 }
788
789 /*
790 * pio data transfer handler
791 */
792 static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
793 {
794 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
795 int i;
796
797 if (enable_stream) {
798 /*
799 * stream mode
800 * see
801 * fsi_pio_push_init()
802 */
803 u32 *buf = (u32 *)_buf;
804
805 for (i = 0; i < samples / 2; i++)
806 fsi_reg_write(fsi, DODT, buf[i]);
807 } else {
808 /* normal mode */
809 u16 *buf = (u16 *)_buf;
810
811 for (i = 0; i < samples; i++)
812 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
813 }
814 }
815
816 static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
817 {
818 u16 *buf = (u16 *)_buf;
819 int i;
820
821 for (i = 0; i < samples; i++)
822 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
823 }
824
825 static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
826 {
827 u32 *buf = (u32 *)_buf;
828 int i;
829
830 for (i = 0; i < samples; i++)
831 fsi_reg_write(fsi, DODT, *(buf + i));
832 }
833
834 static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
835 {
836 u32 *buf = (u32 *)_buf;
837 int i;
838
839 for (i = 0; i < samples; i++)
840 *(buf + i) = fsi_reg_read(fsi, DIDT);
841 }
842
843 static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
844 {
845 struct snd_pcm_runtime *runtime = io->substream->runtime;
846
847 return runtime->dma_area +
848 samples_to_bytes(runtime, io->buff_sample_pos);
849 }
850
851 static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
852 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
853 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
854 int samples)
855 {
856 struct snd_pcm_runtime *runtime;
857 struct snd_pcm_substream *substream;
858 u8 *buf;
859 int over_period;
860
861 if (!fsi_stream_is_working(fsi, io))
862 return -EINVAL;
863
864 over_period = 0;
865 substream = io->substream;
866 runtime = substream->runtime;
867
868 /* FSI FIFO has limit.
869 * So, this driver can not send periods data at a time
870 */
871 if (io->buff_sample_pos >=
872 io->period_samples * (io->period_pos + 1)) {
873
874 over_period = 1;
875 io->period_pos = (io->period_pos + 1) % runtime->periods;
876
877 if (0 == io->period_pos)
878 io->buff_sample_pos = 0;
879 }
880
881 buf = fsi_pio_get_area(fsi, io);
882
883 switch (io->sample_width) {
884 case 2:
885 run16(fsi, buf, samples);
886 break;
887 case 4:
888 run32(fsi, buf, samples);
889 break;
890 default:
891 return -EINVAL;
892 }
893
894 /* update buff_sample_pos */
895 io->buff_sample_pos += samples;
896
897 if (over_period)
898 snd_pcm_period_elapsed(substream);
899
900 return 0;
901 }
902
903 static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
904 {
905 int sample_residues; /* samples in FSI fifo */
906 int sample_space; /* ALSA free samples space */
907 int samples;
908
909 sample_residues = fsi_get_current_fifo_samples(fsi, io);
910 sample_space = io->buff_sample_capa - io->buff_sample_pos;
911
912 samples = min(sample_residues, sample_space);
913
914 return fsi_pio_transfer(fsi, io,
915 fsi_pio_pop16,
916 fsi_pio_pop32,
917 samples);
918 }
919
920 static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
921 {
922 int sample_residues; /* ALSA residue samples */
923 int sample_space; /* FSI fifo free samples space */
924 int samples;
925
926 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
927 sample_space = io->fifo_sample_capa -
928 fsi_get_current_fifo_samples(fsi, io);
929
930 samples = min(sample_residues, sample_space);
931
932 return fsi_pio_transfer(fsi, io,
933 fsi_pio_push16,
934 fsi_pio_push32,
935 samples);
936 }
937
938 static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
939 int enable)
940 {
941 struct fsi_master *master = fsi_get_master(fsi);
942 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
943
944 if (enable)
945 fsi_irq_enable(fsi, io);
946 else
947 fsi_irq_disable(fsi, io);
948
949 if (fsi_is_clk_master(fsi))
950 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
951 }
952
953 static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
954 {
955 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
956
957 /*
958 * we can use 16bit stream mode
959 * when "playback" and "16bit data"
960 * and platform allows "stream mode"
961 * see
962 * fsi_pio_push16()
963 */
964 if (enable_stream)
965 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
966 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
967 else
968 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
969 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
970 return 0;
971 }
972
973 static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
974 {
975 /*
976 * always 24bit bus, package back when "capture"
977 */
978 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
979 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
980 return 0;
981 }
982
983 static struct fsi_stream_handler fsi_pio_push_handler = {
984 .init = fsi_pio_push_init,
985 .transfer = fsi_pio_push,
986 .start_stop = fsi_pio_start_stop,
987 };
988
989 static struct fsi_stream_handler fsi_pio_pop_handler = {
990 .init = fsi_pio_pop_init,
991 .transfer = fsi_pio_pop,
992 .start_stop = fsi_pio_start_stop,
993 };
994
995 static irqreturn_t fsi_interrupt(int irq, void *data)
996 {
997 struct fsi_master *master = data;
998 u32 int_st = fsi_irq_get_status(master);
999
1000 /* clear irq status */
1001 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1002 fsi_master_mask_set(master, SOFT_RST, IR, IR);
1003
1004 if (int_st & AB_IO(1, AO_SHIFT))
1005 fsi_stream_transfer(&master->fsia.playback);
1006 if (int_st & AB_IO(1, BO_SHIFT))
1007 fsi_stream_transfer(&master->fsib.playback);
1008 if (int_st & AB_IO(1, AI_SHIFT))
1009 fsi_stream_transfer(&master->fsia.capture);
1010 if (int_st & AB_IO(1, BI_SHIFT))
1011 fsi_stream_transfer(&master->fsib.capture);
1012
1013 fsi_count_fifo_err(&master->fsia);
1014 fsi_count_fifo_err(&master->fsib);
1015
1016 fsi_irq_clear_status(&master->fsia);
1017 fsi_irq_clear_status(&master->fsib);
1018
1019 return IRQ_HANDLED;
1020 }
1021
1022 /*
1023 * dma data transfer handler
1024 */
1025 static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1026 {
1027 struct snd_pcm_runtime *runtime = io->substream->runtime;
1028 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1029 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1030 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1031
1032 /*
1033 * 24bit data : 24bit bus / package in back
1034 * 16bit data : 16bit bus / stream mode
1035 */
1036 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1037 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1038
1039 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1040 snd_pcm_lib_buffer_bytes(io->substream), dir);
1041 return 0;
1042 }
1043
1044 static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1045 {
1046 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1047 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1048 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1049
1050 dma_unmap_single(dai->dev, io->dma,
1051 snd_pcm_lib_buffer_bytes(io->substream), dir);
1052 return 0;
1053 }
1054
1055 static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1056 {
1057 struct snd_pcm_runtime *runtime = io->substream->runtime;
1058
1059 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1060 }
1061
1062 static void fsi_dma_complete(void *data)
1063 {
1064 struct fsi_stream *io = (struct fsi_stream *)data;
1065 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1066 struct snd_pcm_runtime *runtime = io->substream->runtime;
1067 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1068 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1069 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1070
1071 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
1072 samples_to_bytes(runtime, io->period_samples), dir);
1073
1074 io->buff_sample_pos += io->period_samples;
1075 io->period_pos++;
1076
1077 if (io->period_pos >= runtime->periods) {
1078 io->period_pos = 0;
1079 io->buff_sample_pos = 0;
1080 }
1081
1082 fsi_count_fifo_err(fsi);
1083 fsi_stream_transfer(io);
1084
1085 snd_pcm_period_elapsed(io->substream);
1086 }
1087
1088 static void fsi_dma_do_tasklet(unsigned long data)
1089 {
1090 struct fsi_stream *io = (struct fsi_stream *)data;
1091 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1092 struct snd_soc_dai *dai;
1093 struct dma_async_tx_descriptor *desc;
1094 struct snd_pcm_runtime *runtime;
1095 enum dma_data_direction dir;
1096 int is_play = fsi_stream_is_play(fsi, io);
1097 int len;
1098 dma_addr_t buf;
1099
1100 if (!fsi_stream_is_working(fsi, io))
1101 return;
1102
1103 dai = fsi_get_dai(io->substream);
1104 runtime = io->substream->runtime;
1105 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1106 len = samples_to_bytes(runtime, io->period_samples);
1107 buf = fsi_dma_get_area(io);
1108
1109 dma_sync_single_for_device(dai->dev, buf, len, dir);
1110
1111 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1112 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1113 if (!desc) {
1114 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
1115 return;
1116 }
1117
1118 desc->callback = fsi_dma_complete;
1119 desc->callback_param = io;
1120
1121 if (dmaengine_submit(desc) < 0) {
1122 dev_err(dai->dev, "tx_submit() fail\n");
1123 return;
1124 }
1125
1126 dma_async_issue_pending(io->chan);
1127
1128 /*
1129 * FIXME
1130 *
1131 * In DMAEngine case, codec and FSI cannot be started simultaneously
1132 * since FSI is using tasklet.
1133 * Therefore, in capture case, probably FSI FIFO will have got
1134 * overflow error in this point.
1135 * in that case, DMA cannot start transfer until error was cleared.
1136 */
1137 if (!is_play) {
1138 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1139 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1140 fsi_reg_write(fsi, DIFF_ST, 0);
1141 }
1142 }
1143 }
1144
1145 static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1146 {
1147 struct sh_dmae_slave *slave = param;
1148
1149 chan->private = slave;
1150
1151 return true;
1152 }
1153
1154 static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1155 {
1156 tasklet_schedule(&io->tasklet);
1157
1158 return 0;
1159 }
1160
1161 static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1162 int start)
1163 {
1164 struct fsi_master *master = fsi_get_master(fsi);
1165 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1166 u32 enable = start ? DMA_ON : 0;
1167
1168 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
1169
1170 dmaengine_terminate_all(io->chan);
1171
1172 if (fsi_is_clk_master(fsi))
1173 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
1174 }
1175
1176 static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
1177 {
1178 dma_cap_mask_t mask;
1179
1180 dma_cap_zero(mask);
1181 dma_cap_set(DMA_SLAVE, mask);
1182
1183 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1184 if (!io->chan)
1185 return -EIO;
1186
1187 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1188
1189 return 0;
1190 }
1191
1192 static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1193 {
1194 tasklet_kill(&io->tasklet);
1195
1196 fsi_stream_stop(fsi, io);
1197
1198 if (io->chan)
1199 dma_release_channel(io->chan);
1200
1201 io->chan = NULL;
1202 return 0;
1203 }
1204
1205 static struct fsi_stream_handler fsi_dma_push_handler = {
1206 .init = fsi_dma_init,
1207 .quit = fsi_dma_quit,
1208 .probe = fsi_dma_probe,
1209 .transfer = fsi_dma_transfer,
1210 .remove = fsi_dma_remove,
1211 .start_stop = fsi_dma_push_start_stop,
1212 };
1213
1214 /*
1215 * dai ops
1216 */
1217 static void fsi_fifo_init(struct fsi_priv *fsi,
1218 struct fsi_stream *io,
1219 struct device *dev)
1220 {
1221 struct fsi_master *master = fsi_get_master(fsi);
1222 int is_play = fsi_stream_is_play(fsi, io);
1223 u32 shift, i;
1224 int frame_capa;
1225
1226 /* get on-chip RAM capacity */
1227 shift = fsi_master_read(master, FIFO_SZ);
1228 shift >>= fsi_get_port_shift(fsi, io);
1229 shift &= FIFO_SZ_MASK;
1230 frame_capa = 256 << shift;
1231 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1232
1233 /*
1234 * The maximum number of sample data varies depending
1235 * on the number of channels selected for the format.
1236 *
1237 * FIFOs are used in 4-channel units in 3-channel mode
1238 * and in 8-channel units in 5- to 7-channel mode
1239 * meaning that more FIFOs than the required size of DPRAM
1240 * are used.
1241 *
1242 * ex) if 256 words of DP-RAM is connected
1243 * 1 channel: 256 (256 x 1 = 256)
1244 * 2 channels: 128 (128 x 2 = 256)
1245 * 3 channels: 64 ( 64 x 3 = 192)
1246 * 4 channels: 64 ( 64 x 4 = 256)
1247 * 5 channels: 32 ( 32 x 5 = 160)
1248 * 6 channels: 32 ( 32 x 6 = 192)
1249 * 7 channels: 32 ( 32 x 7 = 224)
1250 * 8 channels: 32 ( 32 x 8 = 256)
1251 */
1252 for (i = 1; i < fsi->chan_num; i <<= 1)
1253 frame_capa >>= 1;
1254 dev_dbg(dev, "%d channel %d store\n",
1255 fsi->chan_num, frame_capa);
1256
1257 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1258
1259 /*
1260 * set interrupt generation factor
1261 * clear FIFO
1262 */
1263 if (is_play) {
1264 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1265 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1266 } else {
1267 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1268 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1269 }
1270 }
1271
1272 static int fsi_hw_startup(struct fsi_priv *fsi,
1273 struct fsi_stream *io,
1274 struct device *dev)
1275 {
1276 u32 flags = fsi_get_info_flags(fsi);
1277 u32 data = 0;
1278
1279 /* clock setting */
1280 if (fsi_is_clk_master(fsi))
1281 data = DIMD | DOMD;
1282
1283 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
1284
1285 /* clock inversion (CKG2) */
1286 data = 0;
1287 if (SH_FSI_LRM_INV & flags)
1288 data |= 1 << 12;
1289 if (SH_FSI_BRM_INV & flags)
1290 data |= 1 << 8;
1291 if (SH_FSI_LRS_INV & flags)
1292 data |= 1 << 4;
1293 if (SH_FSI_BRS_INV & flags)
1294 data |= 1 << 0;
1295
1296 fsi_reg_write(fsi, CKG2, data);
1297
1298 /* spdif ? */
1299 if (fsi_is_spdif(fsi)) {
1300 fsi_spdif_clk_ctrl(fsi, 1);
1301 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1302 }
1303
1304 /*
1305 * get bus settings
1306 */
1307 data = 0;
1308 switch (io->sample_width) {
1309 case 2:
1310 data = BUSOP_GET(16, io->bus_option);
1311 break;
1312 case 4:
1313 data = BUSOP_GET(24, io->bus_option);
1314 break;
1315 }
1316 fsi_format_bus_setup(fsi, io, data, dev);
1317
1318 /* irq clear */
1319 fsi_irq_disable(fsi, io);
1320 fsi_irq_clear_status(fsi);
1321
1322 /* fifo init */
1323 fsi_fifo_init(fsi, io, dev);
1324
1325 return 0;
1326 }
1327
1328 static void fsi_hw_shutdown(struct fsi_priv *fsi,
1329 struct device *dev)
1330 {
1331 if (fsi_is_clk_master(fsi))
1332 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
1333 }
1334
1335 static int fsi_dai_startup(struct snd_pcm_substream *substream,
1336 struct snd_soc_dai *dai)
1337 {
1338 struct fsi_priv *fsi = fsi_get_priv(substream);
1339
1340 fsi->rate = 0;
1341
1342 return 0;
1343 }
1344
1345 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1346 struct snd_soc_dai *dai)
1347 {
1348 struct fsi_priv *fsi = fsi_get_priv(substream);
1349
1350 fsi->rate = 0;
1351 }
1352
1353 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1354 struct snd_soc_dai *dai)
1355 {
1356 struct fsi_priv *fsi = fsi_get_priv(substream);
1357 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1358 int ret = 0;
1359
1360 switch (cmd) {
1361 case SNDRV_PCM_TRIGGER_START:
1362 fsi_stream_init(fsi, io, substream);
1363 fsi_hw_startup(fsi, io, dai->dev);
1364 ret = fsi_stream_transfer(io);
1365 if (0 == ret)
1366 fsi_stream_start(fsi, io);
1367 break;
1368 case SNDRV_PCM_TRIGGER_STOP:
1369 fsi_hw_shutdown(fsi, dai->dev);
1370 fsi_stream_stop(fsi, io);
1371 fsi_stream_quit(fsi, io);
1372 break;
1373 }
1374
1375 return ret;
1376 }
1377
1378 static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1379 {
1380 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1381 case SND_SOC_DAIFMT_I2S:
1382 fsi->fmt = CR_I2S;
1383 fsi->chan_num = 2;
1384 break;
1385 case SND_SOC_DAIFMT_LEFT_J:
1386 fsi->fmt = CR_PCM;
1387 fsi->chan_num = 2;
1388 break;
1389 default:
1390 return -EINVAL;
1391 }
1392
1393 return 0;
1394 }
1395
1396 static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1397 {
1398 struct fsi_master *master = fsi_get_master(fsi);
1399
1400 if (fsi_version(master) < 2)
1401 return -EINVAL;
1402
1403 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
1404 fsi->chan_num = 2;
1405 fsi->spdif = 1;
1406
1407 return 0;
1408 }
1409
1410 static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1411 {
1412 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
1413 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
1414 u32 flags = fsi_get_info_flags(fsi);
1415 int ret;
1416
1417 /* set master/slave audio interface */
1418 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1419 case SND_SOC_DAIFMT_CBM_CFM:
1420 fsi->clk_master = 1;
1421 break;
1422 case SND_SOC_DAIFMT_CBS_CFS:
1423 break;
1424 default:
1425 return -EINVAL;
1426 }
1427
1428 if (fsi_is_clk_master(fsi) && !set_rate) {
1429 dev_err(dai->dev, "platform doesn't have set_rate\n");
1430 return -EINVAL;
1431 }
1432
1433 /* set format */
1434 switch (flags & SH_FSI_FMT_MASK) {
1435 case SH_FSI_FMT_DAI:
1436 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1437 break;
1438 case SH_FSI_FMT_SPDIF:
1439 ret = fsi_set_fmt_spdif(fsi);
1440 break;
1441 default:
1442 ret = -EINVAL;
1443 }
1444
1445 return ret;
1446 }
1447
1448 static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1449 struct snd_pcm_hw_params *params,
1450 struct snd_soc_dai *dai)
1451 {
1452 struct fsi_priv *fsi = fsi_get_priv(substream);
1453 long rate = params_rate(params);
1454 int ret;
1455
1456 if (!fsi_is_clk_master(fsi))
1457 return 0;
1458
1459 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1460 if (ret < 0)
1461 return ret;
1462
1463 fsi->rate = rate;
1464
1465 return ret;
1466 }
1467
1468 static const struct snd_soc_dai_ops fsi_dai_ops = {
1469 .startup = fsi_dai_startup,
1470 .shutdown = fsi_dai_shutdown,
1471 .trigger = fsi_dai_trigger,
1472 .set_fmt = fsi_dai_set_fmt,
1473 .hw_params = fsi_dai_hw_params,
1474 };
1475
1476 /*
1477 * pcm ops
1478 */
1479
1480 static struct snd_pcm_hardware fsi_pcm_hardware = {
1481 .info = SNDRV_PCM_INFO_INTERLEAVED |
1482 SNDRV_PCM_INFO_MMAP |
1483 SNDRV_PCM_INFO_MMAP_VALID |
1484 SNDRV_PCM_INFO_PAUSE,
1485 .formats = FSI_FMTS,
1486 .rates = FSI_RATES,
1487 .rate_min = 8000,
1488 .rate_max = 192000,
1489 .channels_min = 1,
1490 .channels_max = 2,
1491 .buffer_bytes_max = 64 * 1024,
1492 .period_bytes_min = 32,
1493 .period_bytes_max = 8192,
1494 .periods_min = 1,
1495 .periods_max = 32,
1496 .fifo_size = 256,
1497 };
1498
1499 static int fsi_pcm_open(struct snd_pcm_substream *substream)
1500 {
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 int ret = 0;
1503
1504 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1505
1506 ret = snd_pcm_hw_constraint_integer(runtime,
1507 SNDRV_PCM_HW_PARAM_PERIODS);
1508
1509 return ret;
1510 }
1511
1512 static int fsi_hw_params(struct snd_pcm_substream *substream,
1513 struct snd_pcm_hw_params *hw_params)
1514 {
1515 return snd_pcm_lib_malloc_pages(substream,
1516 params_buffer_bytes(hw_params));
1517 }
1518
1519 static int fsi_hw_free(struct snd_pcm_substream *substream)
1520 {
1521 return snd_pcm_lib_free_pages(substream);
1522 }
1523
1524 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1525 {
1526 struct fsi_priv *fsi = fsi_get_priv(substream);
1527 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1528
1529 return fsi_sample2frame(fsi, io->buff_sample_pos);
1530 }
1531
1532 static struct snd_pcm_ops fsi_pcm_ops = {
1533 .open = fsi_pcm_open,
1534 .ioctl = snd_pcm_lib_ioctl,
1535 .hw_params = fsi_hw_params,
1536 .hw_free = fsi_hw_free,
1537 .pointer = fsi_pointer,
1538 };
1539
1540 /*
1541 * snd_soc_platform
1542 */
1543
1544 #define PREALLOC_BUFFER (32 * 1024)
1545 #define PREALLOC_BUFFER_MAX (32 * 1024)
1546
1547 static void fsi_pcm_free(struct snd_pcm *pcm)
1548 {
1549 snd_pcm_lib_preallocate_free_for_all(pcm);
1550 }
1551
1552 static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
1553 {
1554 struct snd_pcm *pcm = rtd->pcm;
1555
1556 /*
1557 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1558 * in MMAP mode (i.e. aplay -M)
1559 */
1560 return snd_pcm_lib_preallocate_pages_for_all(
1561 pcm,
1562 SNDRV_DMA_TYPE_CONTINUOUS,
1563 snd_dma_continuous_data(GFP_KERNEL),
1564 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1565 }
1566
1567 /*
1568 * alsa struct
1569 */
1570
1571 static struct snd_soc_dai_driver fsi_soc_dai[] = {
1572 {
1573 .name = "fsia-dai",
1574 .playback = {
1575 .rates = FSI_RATES,
1576 .formats = FSI_FMTS,
1577 .channels_min = 1,
1578 .channels_max = 8,
1579 },
1580 .capture = {
1581 .rates = FSI_RATES,
1582 .formats = FSI_FMTS,
1583 .channels_min = 1,
1584 .channels_max = 8,
1585 },
1586 .ops = &fsi_dai_ops,
1587 },
1588 {
1589 .name = "fsib-dai",
1590 .playback = {
1591 .rates = FSI_RATES,
1592 .formats = FSI_FMTS,
1593 .channels_min = 1,
1594 .channels_max = 8,
1595 },
1596 .capture = {
1597 .rates = FSI_RATES,
1598 .formats = FSI_FMTS,
1599 .channels_min = 1,
1600 .channels_max = 8,
1601 },
1602 .ops = &fsi_dai_ops,
1603 },
1604 };
1605
1606 static struct snd_soc_platform_driver fsi_soc_platform = {
1607 .ops = &fsi_pcm_ops,
1608 .pcm_new = fsi_pcm_new,
1609 .pcm_free = fsi_pcm_free,
1610 };
1611
1612 /*
1613 * platform function
1614 */
1615 static void fsi_handler_init(struct fsi_priv *fsi)
1616 {
1617 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1618 fsi->playback.priv = fsi;
1619 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1620 fsi->capture.priv = fsi;
1621
1622 if (fsi->info->tx_id) {
1623 fsi->playback.slave.slave_id = fsi->info->tx_id;
1624 fsi->playback.handler = &fsi_dma_push_handler;
1625 }
1626 }
1627
1628 static int fsi_probe(struct platform_device *pdev)
1629 {
1630 struct fsi_master *master;
1631 const struct platform_device_id *id_entry;
1632 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
1633 struct resource *res;
1634 unsigned int irq;
1635 int ret;
1636
1637 id_entry = pdev->id_entry;
1638 if (!id_entry) {
1639 dev_err(&pdev->dev, "unknown fsi device\n");
1640 return -ENODEV;
1641 }
1642
1643 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1644 irq = platform_get_irq(pdev, 0);
1645 if (!res || (int)irq <= 0) {
1646 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1647 ret = -ENODEV;
1648 goto exit;
1649 }
1650
1651 master = kzalloc(sizeof(*master), GFP_KERNEL);
1652 if (!master) {
1653 dev_err(&pdev->dev, "Could not allocate master\n");
1654 ret = -ENOMEM;
1655 goto exit;
1656 }
1657
1658 master->base = ioremap_nocache(res->start, resource_size(res));
1659 if (!master->base) {
1660 ret = -ENXIO;
1661 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1662 goto exit_kfree;
1663 }
1664
1665 /* master setting */
1666 master->irq = irq;
1667 master->core = (struct fsi_core *)id_entry->driver_data;
1668 spin_lock_init(&master->lock);
1669
1670 /* FSI A setting */
1671 master->fsia.base = master->base;
1672 master->fsia.master = master;
1673 master->fsia.info = &info->port_a;
1674 fsi_handler_init(&master->fsia);
1675 ret = fsi_stream_probe(&master->fsia);
1676 if (ret < 0) {
1677 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1678 goto exit_iounmap;
1679 }
1680
1681 /* FSI B setting */
1682 master->fsib.base = master->base + 0x40;
1683 master->fsib.master = master;
1684 master->fsib.info = &info->port_b;
1685 fsi_handler_init(&master->fsib);
1686 ret = fsi_stream_probe(&master->fsib);
1687 if (ret < 0) {
1688 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1689 goto exit_fsia;
1690 }
1691
1692 pm_runtime_enable(&pdev->dev);
1693 dev_set_drvdata(&pdev->dev, master);
1694
1695 ret = request_irq(irq, &fsi_interrupt, 0,
1696 id_entry->name, master);
1697 if (ret) {
1698 dev_err(&pdev->dev, "irq request err\n");
1699 goto exit_fsib;
1700 }
1701
1702 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
1703 if (ret < 0) {
1704 dev_err(&pdev->dev, "cannot snd soc register\n");
1705 goto exit_free_irq;
1706 }
1707
1708 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1709 ARRAY_SIZE(fsi_soc_dai));
1710 if (ret < 0) {
1711 dev_err(&pdev->dev, "cannot snd dai register\n");
1712 goto exit_snd_soc;
1713 }
1714
1715 return ret;
1716
1717 exit_snd_soc:
1718 snd_soc_unregister_platform(&pdev->dev);
1719 exit_free_irq:
1720 free_irq(irq, master);
1721 exit_fsib:
1722 fsi_stream_remove(&master->fsib);
1723 exit_fsia:
1724 fsi_stream_remove(&master->fsia);
1725 exit_iounmap:
1726 iounmap(master->base);
1727 pm_runtime_disable(&pdev->dev);
1728 exit_kfree:
1729 kfree(master);
1730 master = NULL;
1731 exit:
1732 return ret;
1733 }
1734
1735 static int fsi_remove(struct platform_device *pdev)
1736 {
1737 struct fsi_master *master;
1738
1739 master = dev_get_drvdata(&pdev->dev);
1740
1741 free_irq(master->irq, master);
1742 pm_runtime_disable(&pdev->dev);
1743
1744 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1745 snd_soc_unregister_platform(&pdev->dev);
1746
1747 fsi_stream_remove(&master->fsia);
1748 fsi_stream_remove(&master->fsib);
1749
1750 iounmap(master->base);
1751 kfree(master);
1752
1753 return 0;
1754 }
1755
1756 static void __fsi_suspend(struct fsi_priv *fsi,
1757 struct fsi_stream *io,
1758 struct device *dev)
1759 {
1760 if (!fsi_stream_is_working(fsi, io))
1761 return;
1762
1763 fsi_stream_stop(fsi, io);
1764 fsi_hw_shutdown(fsi, dev);
1765 }
1766
1767 static void __fsi_resume(struct fsi_priv *fsi,
1768 struct fsi_stream *io,
1769 struct device *dev)
1770 {
1771 if (!fsi_stream_is_working(fsi, io))
1772 return;
1773
1774 fsi_hw_startup(fsi, io, dev);
1775
1776 if (fsi_is_clk_master(fsi) && fsi->rate)
1777 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
1778
1779 fsi_stream_start(fsi, io);
1780 }
1781
1782 static int fsi_suspend(struct device *dev)
1783 {
1784 struct fsi_master *master = dev_get_drvdata(dev);
1785 struct fsi_priv *fsia = &master->fsia;
1786 struct fsi_priv *fsib = &master->fsib;
1787
1788 __fsi_suspend(fsia, &fsia->playback, dev);
1789 __fsi_suspend(fsia, &fsia->capture, dev);
1790
1791 __fsi_suspend(fsib, &fsib->playback, dev);
1792 __fsi_suspend(fsib, &fsib->capture, dev);
1793
1794 return 0;
1795 }
1796
1797 static int fsi_resume(struct device *dev)
1798 {
1799 struct fsi_master *master = dev_get_drvdata(dev);
1800 struct fsi_priv *fsia = &master->fsia;
1801 struct fsi_priv *fsib = &master->fsib;
1802
1803 __fsi_resume(fsia, &fsia->playback, dev);
1804 __fsi_resume(fsia, &fsia->capture, dev);
1805
1806 __fsi_resume(fsib, &fsib->playback, dev);
1807 __fsi_resume(fsib, &fsib->capture, dev);
1808
1809 return 0;
1810 }
1811
1812 static struct dev_pm_ops fsi_pm_ops = {
1813 .suspend = fsi_suspend,
1814 .resume = fsi_resume,
1815 };
1816
1817 static struct fsi_core fsi1_core = {
1818 .ver = 1,
1819
1820 /* Interrupt */
1821 .int_st = INT_ST,
1822 .iemsk = IEMSK,
1823 .imsk = IMSK,
1824 };
1825
1826 static struct fsi_core fsi2_core = {
1827 .ver = 2,
1828
1829 /* Interrupt */
1830 .int_st = CPU_INT_ST,
1831 .iemsk = CPU_IEMSK,
1832 .imsk = CPU_IMSK,
1833 .a_mclk = A_MST_CTLR,
1834 .b_mclk = B_MST_CTLR,
1835 };
1836
1837 static struct platform_device_id fsi_id_table[] = {
1838 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1839 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
1840 {},
1841 };
1842 MODULE_DEVICE_TABLE(platform, fsi_id_table);
1843
1844 static struct platform_driver fsi_driver = {
1845 .driver = {
1846 .name = "fsi-pcm-audio",
1847 .pm = &fsi_pm_ops,
1848 },
1849 .probe = fsi_probe,
1850 .remove = fsi_remove,
1851 .id_table = fsi_id_table,
1852 };
1853
1854 module_platform_driver(fsi_driver);
1855
1856 MODULE_LICENSE("GPL");
1857 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1858 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1859 MODULE_ALIAS("platform:fsi-pcm-audio");
This page took 0.247778 seconds and 6 git commands to generate.