ASoC: fsi: Add FIFO size calculate
[firefly-linux-kernel-4.4.55.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/init.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/list.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/io.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
26 #include <sound/pcm_params.h>
27 #include <sound/sh_fsi.h>
28 #include <asm/atomic.h>
29
30 #define DO_FMT          0x0000
31 #define DOFF_CTL        0x0004
32 #define DOFF_ST         0x0008
33 #define DI_FMT          0x000C
34 #define DIFF_CTL        0x0010
35 #define DIFF_ST         0x0014
36 #define CKG1            0x0018
37 #define CKG2            0x001C
38 #define DIDT            0x0020
39 #define DODT            0x0024
40 #define MUTE_ST         0x0028
41 #define REG_END         MUTE_ST
42
43 #define INT_ST          0x0200
44 #define IEMSK           0x0204
45 #define IMSK            0x0208
46 #define MUTE            0x020C
47 #define CLK_RST         0x0210
48 #define SOFT_RST        0x0214
49 #define FIFO_SZ         0x0218
50 #define MREG_START      INT_ST
51 #define MREG_END        FIFO_SZ
52
53 /* DO_FMT */
54 /* DI_FMT */
55 #define CR_FMT(param) ((param) << 4)
56 # define CR_MONO        0x0
57 # define CR_MONO_D      0x1
58 # define CR_PCM         0x2
59 # define CR_I2S         0x3
60 # define CR_TDM         0x4
61 # define CR_TDM_D       0x5
62
63 /* DOFF_CTL */
64 /* DIFF_CTL */
65 #define IRQ_HALF        0x00100000
66 #define FIFO_CLR        0x00000001
67
68 /* DOFF_ST */
69 #define ERR_OVER        0x00000010
70 #define ERR_UNDER       0x00000001
71 #define ST_ERR          (ERR_OVER | ERR_UNDER)
72
73 /* CLK_RST */
74 #define B_CLK           0x00000010
75 #define A_CLK           0x00000001
76
77 /* INT_ST */
78 #define INT_B_IN        (1 << 12)
79 #define INT_B_OUT       (1 << 8)
80 #define INT_A_IN        (1 << 4)
81 #define INT_A_OUT       (1 << 0)
82
83 /* SOFT_RST */
84 #define PBSR            (1 << 12) /* Port B Software Reset */
85 #define PASR            (1 <<  8) /* Port A Software Reset */
86 #define IR              (1 <<  4) /* Interrupt Reset */
87 #define FSISR           (1 <<  0) /* Software Reset */
88
89 /* FIFO_SZ */
90 #define OUT_SZ_MASK     0x7
91 #define BO_SZ_SHIFT     8
92 #define AO_SZ_SHIFT     0
93
94 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
95
96 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
97
98 /************************************************************************
99
100
101                 struct
102
103
104 ************************************************************************/
105 struct fsi_priv {
106         void __iomem *base;
107         struct snd_pcm_substream *substream;
108         struct fsi_master *master;
109
110         int fifo_max;
111         int chan;
112
113         int byte_offset;
114         int period_len;
115         int buffer_len;
116         int periods;
117 };
118
119 struct fsi_master {
120         void __iomem *base;
121         int irq;
122         struct fsi_priv fsia;
123         struct fsi_priv fsib;
124         struct sh_fsi_platform_info *info;
125         spinlock_t lock;
126 };
127
128 /************************************************************************
129
130
131                 basic read write function
132
133
134 ************************************************************************/
135 static void __fsi_reg_write(u32 reg, u32 data)
136 {
137         /* valid data area is 24bit */
138         data &= 0x00ffffff;
139
140         __raw_writel(data, reg);
141 }
142
143 static u32 __fsi_reg_read(u32 reg)
144 {
145         return __raw_readl(reg);
146 }
147
148 static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
149 {
150         u32 val = __fsi_reg_read(reg);
151
152         val &= ~mask;
153         val |= data & mask;
154
155         __fsi_reg_write(reg, val);
156 }
157
158 static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
159 {
160         if (reg > REG_END)
161                 return;
162
163         __fsi_reg_write((u32)(fsi->base + reg), data);
164 }
165
166 static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
167 {
168         if (reg > REG_END)
169                 return 0;
170
171         return __fsi_reg_read((u32)(fsi->base + reg));
172 }
173
174 static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
175 {
176         if (reg > REG_END)
177                 return;
178
179         __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
180 }
181
182 static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
183 {
184         unsigned long flags;
185
186         if ((reg < MREG_START) ||
187             (reg > MREG_END))
188                 return;
189
190         spin_lock_irqsave(&master->lock, flags);
191         __fsi_reg_write((u32)(master->base + reg), data);
192         spin_unlock_irqrestore(&master->lock, flags);
193 }
194
195 static u32 fsi_master_read(struct fsi_master *master, u32 reg)
196 {
197         u32 ret;
198         unsigned long flags;
199
200         if ((reg < MREG_START) ||
201             (reg > MREG_END))
202                 return 0;
203
204         spin_lock_irqsave(&master->lock, flags);
205         ret = __fsi_reg_read((u32)(master->base + reg));
206         spin_unlock_irqrestore(&master->lock, flags);
207
208         return ret;
209 }
210
211 static void fsi_master_mask_set(struct fsi_master *master,
212                                u32 reg, u32 mask, u32 data)
213 {
214         unsigned long flags;
215
216         if ((reg < MREG_START) ||
217             (reg > MREG_END))
218                 return;
219
220         spin_lock_irqsave(&master->lock, flags);
221         __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
222         spin_unlock_irqrestore(&master->lock, flags);
223 }
224
225 /************************************************************************
226
227
228                 basic function
229
230
231 ************************************************************************/
232 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
233 {
234         return fsi->master;
235 }
236
237 static int fsi_is_port_a(struct fsi_priv *fsi)
238 {
239         return fsi->master->base == fsi->base;
240 }
241
242 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
243 {
244         struct snd_soc_pcm_runtime *rtd = substream->private_data;
245         struct snd_soc_dai_link *machine = rtd->dai;
246
247         return  machine->cpu_dai;
248 }
249
250 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
251 {
252         struct snd_soc_dai *dai = fsi_get_dai(substream);
253
254         return dai->private_data;
255 }
256
257 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
258 {
259         int is_porta = fsi_is_port_a(fsi);
260         struct fsi_master *master = fsi_get_master(fsi);
261
262         return is_porta ? master->info->porta_flags :
263                 master->info->portb_flags;
264 }
265
266 static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
267 {
268         u32 mode;
269         u32 flags = fsi_get_info_flags(fsi);
270
271         mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
272
273         /* return
274          * 1 : master mode
275          * 0 : slave mode
276          */
277
278         return (mode & flags) != mode;
279 }
280
281 static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
282 {
283         int is_porta = fsi_is_port_a(fsi);
284         u32 data;
285
286         if (is_porta)
287                 data = is_play ? (1 << 0) : (1 << 4);
288         else
289                 data = is_play ? (1 << 8) : (1 << 12);
290
291         return data;
292 }
293
294 static void fsi_stream_push(struct fsi_priv *fsi,
295                             struct snd_pcm_substream *substream,
296                             u32 buffer_len,
297                             u32 period_len)
298 {
299         fsi->substream          = substream;
300         fsi->buffer_len         = buffer_len;
301         fsi->period_len         = period_len;
302         fsi->byte_offset        = 0;
303         fsi->periods            = 0;
304 }
305
306 static void fsi_stream_pop(struct fsi_priv *fsi)
307 {
308         fsi->substream          = NULL;
309         fsi->buffer_len         = 0;
310         fsi->period_len         = 0;
311         fsi->byte_offset        = 0;
312         fsi->periods            = 0;
313 }
314
315 static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
316 {
317         u32 status;
318         u32 reg = is_play ? DOFF_ST : DIFF_ST;
319         int residue;
320
321         status = fsi_reg_read(fsi, reg);
322         residue = 0x1ff & (status >> 8);
323         residue *= fsi->chan;
324
325         return residue;
326 }
327
328 /************************************************************************
329
330
331                 irq function
332
333
334 ************************************************************************/
335 static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
336 {
337         u32 data = fsi_port_ab_io_bit(fsi, is_play);
338         struct fsi_master *master = fsi_get_master(fsi);
339
340         fsi_master_mask_set(master, IMSK,  data, data);
341         fsi_master_mask_set(master, IEMSK, data, data);
342 }
343
344 static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
345 {
346         u32 data = fsi_port_ab_io_bit(fsi, is_play);
347         struct fsi_master *master = fsi_get_master(fsi);
348
349         fsi_master_mask_set(master, IMSK,  data, 0);
350         fsi_master_mask_set(master, IEMSK, data, 0);
351 }
352
353 static u32 fsi_irq_get_status(struct fsi_master *master)
354 {
355         return fsi_master_read(master, INT_ST);
356 }
357
358 static void fsi_irq_clear_all_status(struct fsi_master *master)
359 {
360         fsi_master_write(master, INT_ST, 0x0000000);
361 }
362
363 static void fsi_irq_clear_status(struct fsi_priv *fsi)
364 {
365         u32 data = 0;
366         struct fsi_master *master = fsi_get_master(fsi);
367
368         data |= fsi_port_ab_io_bit(fsi, 0);
369         data |= fsi_port_ab_io_bit(fsi, 1);
370
371         /* clear interrupt factor */
372         fsi_master_mask_set(master, INT_ST, data, 0);
373 }
374
375 /************************************************************************
376
377
378                 ctrl function
379
380
381 ************************************************************************/
382 static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
383 {
384         u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
385         struct fsi_master *master = fsi_get_master(fsi);
386
387         if (enable)
388                 fsi_master_mask_set(master, CLK_RST, val, val);
389         else
390                 fsi_master_mask_set(master, CLK_RST, val, 0);
391 }
392
393 static void fsi_fifo_init(struct fsi_priv *fsi,
394                           int is_play,
395                           struct snd_soc_dai *dai)
396 {
397         struct fsi_master *master = fsi_get_master(fsi);
398         u32 ctrl, shift, i;
399
400         /* get on-chip RAM capacity */
401         shift = fsi_master_read(master, FIFO_SZ);
402         shift >>= fsi_is_port_a(fsi) ? AO_SZ_SHIFT : BO_SZ_SHIFT;
403         shift &= OUT_SZ_MASK;
404         fsi->fifo_max = 256 << shift;
405         dev_dbg(dai->dev, "fifo = %d words\n", fsi->fifo_max);
406
407         /*
408          * The maximum number of sample data varies depending
409          * on the number of channels selected for the format.
410          *
411          * FIFOs are used in 4-channel units in 3-channel mode
412          * and in 8-channel units in 5- to 7-channel mode
413          * meaning that more FIFOs than the required size of DPRAM
414          * are used.
415          *
416          * ex) if 256 words of DP-RAM is connected
417          * 1 channel:  256 (256 x 1 = 256)
418          * 2 channels: 128 (128 x 2 = 256)
419          * 3 channels:  64 ( 64 x 3 = 192)
420          * 4 channels:  64 ( 64 x 4 = 256)
421          * 5 channels:  32 ( 32 x 5 = 160)
422          * 6 channels:  32 ( 32 x 6 = 192)
423          * 7 channels:  32 ( 32 x 7 = 224)
424          * 8 channels:  32 ( 32 x 8 = 256)
425          */
426         for (i = 1; i < fsi->chan; i <<= 1)
427                 fsi->fifo_max >>= 1;
428         dev_dbg(dai->dev, "%d channel %d store\n", fsi->chan, fsi->fifo_max);
429
430         ctrl = is_play ? DOFF_CTL : DIFF_CTL;
431
432         /* set interrupt generation factor */
433         fsi_reg_write(fsi, ctrl, IRQ_HALF);
434
435         /* clear FIFO */
436         fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
437 }
438
439 static void fsi_soft_all_reset(struct fsi_master *master)
440 {
441         /* port AB reset */
442         fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
443         mdelay(10);
444
445         /* soft reset */
446         fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
447         fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
448         mdelay(10);
449 }
450
451 /* playback interrupt */
452 static int fsi_data_push(struct fsi_priv *fsi, int startup)
453 {
454         struct snd_pcm_runtime *runtime;
455         struct snd_pcm_substream *substream = NULL;
456         u32 status;
457         int send;
458         int fifo_free;
459         int width;
460         u8 *start;
461         int i, over_period;
462
463         if (!fsi                        ||
464             !fsi->substream             ||
465             !fsi->substream->runtime)
466                 return -EINVAL;
467
468         over_period     = 0;
469         substream       = fsi->substream;
470         runtime         = substream->runtime;
471
472         /* FSI FIFO has limit.
473          * So, this driver can not send periods data at a time
474          */
475         if (fsi->byte_offset >=
476             fsi->period_len * (fsi->periods + 1)) {
477
478                 over_period = 1;
479                 fsi->periods = (fsi->periods + 1) % runtime->periods;
480
481                 if (0 == fsi->periods)
482                         fsi->byte_offset = 0;
483         }
484
485         /* get 1 channel data width */
486         width = frames_to_bytes(runtime, 1) / fsi->chan;
487
488         /* get send size for alsa */
489         send = (fsi->buffer_len - fsi->byte_offset) / width;
490
491         /*  get FIFO free size */
492         fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
493
494         /* size check */
495         if (fifo_free < send)
496                 send = fifo_free;
497
498         start = runtime->dma_area;
499         start += fsi->byte_offset;
500
501         switch (width) {
502         case 2:
503                 for (i = 0; i < send; i++)
504                         fsi_reg_write(fsi, DODT,
505                                       ((u32)*((u16 *)start + i) << 8));
506                 break;
507         case 4:
508                 for (i = 0; i < send; i++)
509                         fsi_reg_write(fsi, DODT, *((u32 *)start + i));
510                 break;
511         default:
512                 return -EINVAL;
513         }
514
515         fsi->byte_offset += send * width;
516
517         status = fsi_reg_read(fsi, DOFF_ST);
518         if (!startup) {
519                 struct snd_soc_dai *dai = fsi_get_dai(substream);
520
521                 if (status & ERR_OVER)
522                         dev_err(dai->dev, "over run\n");
523                 if (status & ERR_UNDER)
524                         dev_err(dai->dev, "under run\n");
525         }
526         fsi_reg_write(fsi, DOFF_ST, 0);
527
528         fsi_irq_enable(fsi, 1);
529
530         if (over_period)
531                 snd_pcm_period_elapsed(substream);
532
533         return 0;
534 }
535
536 static int fsi_data_pop(struct fsi_priv *fsi, int startup)
537 {
538         struct snd_pcm_runtime *runtime;
539         struct snd_pcm_substream *substream = NULL;
540         u32 status;
541         int free;
542         int fifo_fill;
543         int width;
544         u8 *start;
545         int i, over_period;
546
547         if (!fsi                        ||
548             !fsi->substream             ||
549             !fsi->substream->runtime)
550                 return -EINVAL;
551
552         over_period     = 0;
553         substream       = fsi->substream;
554         runtime         = substream->runtime;
555
556         /* FSI FIFO has limit.
557          * So, this driver can not send periods data at a time
558          */
559         if (fsi->byte_offset >=
560             fsi->period_len * (fsi->periods + 1)) {
561
562                 over_period = 1;
563                 fsi->periods = (fsi->periods + 1) % runtime->periods;
564
565                 if (0 == fsi->periods)
566                         fsi->byte_offset = 0;
567         }
568
569         /* get 1 channel data width */
570         width = frames_to_bytes(runtime, 1) / fsi->chan;
571
572         /* get free space for alsa */
573         free = (fsi->buffer_len - fsi->byte_offset) / width;
574
575         /* get recv size */
576         fifo_fill = fsi_get_fifo_residue(fsi, 0);
577
578         if (free < fifo_fill)
579                 fifo_fill = free;
580
581         start = runtime->dma_area;
582         start += fsi->byte_offset;
583
584         switch (width) {
585         case 2:
586                 for (i = 0; i < fifo_fill; i++)
587                         *((u16 *)start + i) =
588                                 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
589                 break;
590         case 4:
591                 for (i = 0; i < fifo_fill; i++)
592                         *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
593                 break;
594         default:
595                 return -EINVAL;
596         }
597
598         fsi->byte_offset += fifo_fill * width;
599
600         status = fsi_reg_read(fsi, DIFF_ST);
601         if (!startup) {
602                 struct snd_soc_dai *dai = fsi_get_dai(substream);
603
604                 if (status & ERR_OVER)
605                         dev_err(dai->dev, "over run\n");
606                 if (status & ERR_UNDER)
607                         dev_err(dai->dev, "under run\n");
608         }
609         fsi_reg_write(fsi, DIFF_ST, 0);
610
611         fsi_irq_enable(fsi, 0);
612
613         if (over_period)
614                 snd_pcm_period_elapsed(substream);
615
616         return 0;
617 }
618
619 static irqreturn_t fsi_interrupt(int irq, void *data)
620 {
621         struct fsi_master *master = data;
622         u32 int_st = fsi_irq_get_status(master);
623
624         /* clear irq status */
625         fsi_master_mask_set(master, SOFT_RST, IR, 0);
626         fsi_master_mask_set(master, SOFT_RST, IR, IR);
627
628         if (int_st & INT_A_OUT)
629                 fsi_data_push(&master->fsia, 0);
630         if (int_st & INT_B_OUT)
631                 fsi_data_push(&master->fsib, 0);
632         if (int_st & INT_A_IN)
633                 fsi_data_pop(&master->fsia, 0);
634         if (int_st & INT_B_IN)
635                 fsi_data_pop(&master->fsib, 0);
636
637         fsi_irq_clear_all_status(master);
638
639         return IRQ_HANDLED;
640 }
641
642 /************************************************************************
643
644
645                 dai ops
646
647
648 ************************************************************************/
649 static int fsi_dai_startup(struct snd_pcm_substream *substream,
650                            struct snd_soc_dai *dai)
651 {
652         struct fsi_priv *fsi = fsi_get_priv(substream);
653         const char *msg;
654         u32 flags = fsi_get_info_flags(fsi);
655         u32 fmt;
656         u32 reg;
657         u32 data;
658         int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
659         int is_master;
660         int ret = 0;
661
662         pm_runtime_get_sync(dai->dev);
663
664         /* CKG1 */
665         data = is_play ? (1 << 0) : (1 << 4);
666         is_master = fsi_is_master_mode(fsi, is_play);
667         if (is_master)
668                 fsi_reg_mask_set(fsi, CKG1, data, data);
669         else
670                 fsi_reg_mask_set(fsi, CKG1, data, 0);
671
672         /* clock inversion (CKG2) */
673         data = 0;
674         switch (SH_FSI_INVERSION_MASK & flags) {
675         case SH_FSI_LRM_INV:
676                 data = 1 << 12;
677                 break;
678         case SH_FSI_BRM_INV:
679                 data = 1 << 8;
680                 break;
681         case SH_FSI_LRS_INV:
682                 data = 1 << 4;
683                 break;
684         case SH_FSI_BRS_INV:
685                 data = 1 << 0;
686                 break;
687         }
688         fsi_reg_write(fsi, CKG2, data);
689
690         /* do fmt, di fmt */
691         data = 0;
692         reg = is_play ? DO_FMT : DI_FMT;
693         fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
694         switch (fmt) {
695         case SH_FSI_FMT_MONO:
696                 msg = "MONO";
697                 data = CR_FMT(CR_MONO);
698                 fsi->chan = 1;
699                 break;
700         case SH_FSI_FMT_MONO_DELAY:
701                 msg = "MONO Delay";
702                 data = CR_FMT(CR_MONO_D);
703                 fsi->chan = 1;
704                 break;
705         case SH_FSI_FMT_PCM:
706                 msg = "PCM";
707                 data = CR_FMT(CR_PCM);
708                 fsi->chan = 2;
709                 break;
710         case SH_FSI_FMT_I2S:
711                 msg = "I2S";
712                 data = CR_FMT(CR_I2S);
713                 fsi->chan = 2;
714                 break;
715         case SH_FSI_FMT_TDM:
716                 msg = "TDM";
717                 data = CR_FMT(CR_TDM) | (fsi->chan - 1);
718                 fsi->chan = is_play ?
719                         SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
720                 break;
721         case SH_FSI_FMT_TDM_DELAY:
722                 msg = "TDM Delay";
723                 data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
724                 fsi->chan = is_play ?
725                         SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
726                 break;
727         default:
728                 dev_err(dai->dev, "unknown format.\n");
729                 return -EINVAL;
730         }
731         fsi_reg_write(fsi, reg, data);
732
733         /*
734          * clear clk reset if master mode
735          */
736         if (is_master)
737                 fsi_clk_ctrl(fsi, 1);
738
739         /* irq clear */
740         fsi_irq_disable(fsi, is_play);
741         fsi_irq_clear_status(fsi);
742
743         /* fifo init */
744         fsi_fifo_init(fsi, is_play, dai);
745
746         return ret;
747 }
748
749 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
750                              struct snd_soc_dai *dai)
751 {
752         struct fsi_priv *fsi = fsi_get_priv(substream);
753         int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
754
755         fsi_irq_disable(fsi, is_play);
756         fsi_clk_ctrl(fsi, 0);
757
758         pm_runtime_put_sync(dai->dev);
759 }
760
761 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
762                            struct snd_soc_dai *dai)
763 {
764         struct fsi_priv *fsi = fsi_get_priv(substream);
765         struct snd_pcm_runtime *runtime = substream->runtime;
766         int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
767         int ret = 0;
768
769         switch (cmd) {
770         case SNDRV_PCM_TRIGGER_START:
771                 fsi_stream_push(fsi, substream,
772                                 frames_to_bytes(runtime, runtime->buffer_size),
773                                 frames_to_bytes(runtime, runtime->period_size));
774                 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
775                 break;
776         case SNDRV_PCM_TRIGGER_STOP:
777                 fsi_irq_disable(fsi, is_play);
778                 fsi_stream_pop(fsi);
779                 break;
780         }
781
782         return ret;
783 }
784
785 static struct snd_soc_dai_ops fsi_dai_ops = {
786         .startup        = fsi_dai_startup,
787         .shutdown       = fsi_dai_shutdown,
788         .trigger        = fsi_dai_trigger,
789 };
790
791 /************************************************************************
792
793
794                 pcm ops
795
796
797 ************************************************************************/
798 static struct snd_pcm_hardware fsi_pcm_hardware = {
799         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
800                         SNDRV_PCM_INFO_MMAP             |
801                         SNDRV_PCM_INFO_MMAP_VALID       |
802                         SNDRV_PCM_INFO_PAUSE,
803         .formats                = FSI_FMTS,
804         .rates                  = FSI_RATES,
805         .rate_min               = 8000,
806         .rate_max               = 192000,
807         .channels_min           = 1,
808         .channels_max           = 2,
809         .buffer_bytes_max       = 64 * 1024,
810         .period_bytes_min       = 32,
811         .period_bytes_max       = 8192,
812         .periods_min            = 1,
813         .periods_max            = 32,
814         .fifo_size              = 256,
815 };
816
817 static int fsi_pcm_open(struct snd_pcm_substream *substream)
818 {
819         struct snd_pcm_runtime *runtime = substream->runtime;
820         int ret = 0;
821
822         snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
823
824         ret = snd_pcm_hw_constraint_integer(runtime,
825                                             SNDRV_PCM_HW_PARAM_PERIODS);
826
827         return ret;
828 }
829
830 static int fsi_hw_params(struct snd_pcm_substream *substream,
831                          struct snd_pcm_hw_params *hw_params)
832 {
833         return snd_pcm_lib_malloc_pages(substream,
834                                         params_buffer_bytes(hw_params));
835 }
836
837 static int fsi_hw_free(struct snd_pcm_substream *substream)
838 {
839         return snd_pcm_lib_free_pages(substream);
840 }
841
842 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
843 {
844         struct snd_pcm_runtime *runtime = substream->runtime;
845         struct fsi_priv *fsi = fsi_get_priv(substream);
846         long location;
847
848         location = (fsi->byte_offset - 1);
849         if (location < 0)
850                 location = 0;
851
852         return bytes_to_frames(runtime, location);
853 }
854
855 static struct snd_pcm_ops fsi_pcm_ops = {
856         .open           = fsi_pcm_open,
857         .ioctl          = snd_pcm_lib_ioctl,
858         .hw_params      = fsi_hw_params,
859         .hw_free        = fsi_hw_free,
860         .pointer        = fsi_pointer,
861 };
862
863 /************************************************************************
864
865
866                 snd_soc_platform
867
868
869 ************************************************************************/
870 #define PREALLOC_BUFFER         (32 * 1024)
871 #define PREALLOC_BUFFER_MAX     (32 * 1024)
872
873 static void fsi_pcm_free(struct snd_pcm *pcm)
874 {
875         snd_pcm_lib_preallocate_free_for_all(pcm);
876 }
877
878 static int fsi_pcm_new(struct snd_card *card,
879                        struct snd_soc_dai *dai,
880                        struct snd_pcm *pcm)
881 {
882         /*
883          * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
884          * in MMAP mode (i.e. aplay -M)
885          */
886         return snd_pcm_lib_preallocate_pages_for_all(
887                 pcm,
888                 SNDRV_DMA_TYPE_CONTINUOUS,
889                 snd_dma_continuous_data(GFP_KERNEL),
890                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
891 }
892
893 /************************************************************************
894
895
896                 alsa struct
897
898
899 ************************************************************************/
900 struct snd_soc_dai fsi_soc_dai[] = {
901         {
902                 .name                   = "FSIA",
903                 .id                     = 0,
904                 .playback = {
905                         .rates          = FSI_RATES,
906                         .formats        = FSI_FMTS,
907                         .channels_min   = 1,
908                         .channels_max   = 8,
909                 },
910                 .capture = {
911                         .rates          = FSI_RATES,
912                         .formats        = FSI_FMTS,
913                         .channels_min   = 1,
914                         .channels_max   = 8,
915                 },
916                 .ops = &fsi_dai_ops,
917         },
918         {
919                 .name                   = "FSIB",
920                 .id                     = 1,
921                 .playback = {
922                         .rates          = FSI_RATES,
923                         .formats        = FSI_FMTS,
924                         .channels_min   = 1,
925                         .channels_max   = 8,
926                 },
927                 .capture = {
928                         .rates          = FSI_RATES,
929                         .formats        = FSI_FMTS,
930                         .channels_min   = 1,
931                         .channels_max   = 8,
932                 },
933                 .ops = &fsi_dai_ops,
934         },
935 };
936 EXPORT_SYMBOL_GPL(fsi_soc_dai);
937
938 struct snd_soc_platform fsi_soc_platform = {
939         .name           = "fsi-pcm",
940         .pcm_ops        = &fsi_pcm_ops,
941         .pcm_new        = fsi_pcm_new,
942         .pcm_free       = fsi_pcm_free,
943 };
944 EXPORT_SYMBOL_GPL(fsi_soc_platform);
945
946 /************************************************************************
947
948
949                 platform function
950
951
952 ************************************************************************/
953 static int fsi_probe(struct platform_device *pdev)
954 {
955         struct fsi_master *master;
956         struct resource *res;
957         unsigned int irq;
958         int ret;
959
960         if (0 != pdev->id) {
961                 dev_err(&pdev->dev, "current fsi support id 0 only now\n");
962                 return -ENODEV;
963         }
964
965         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
966         irq = platform_get_irq(pdev, 0);
967         if (!res || (int)irq <= 0) {
968                 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
969                 ret = -ENODEV;
970                 goto exit;
971         }
972
973         master = kzalloc(sizeof(*master), GFP_KERNEL);
974         if (!master) {
975                 dev_err(&pdev->dev, "Could not allocate master\n");
976                 ret = -ENOMEM;
977                 goto exit;
978         }
979
980         master->base = ioremap_nocache(res->start, resource_size(res));
981         if (!master->base) {
982                 ret = -ENXIO;
983                 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
984                 goto exit_kfree;
985         }
986
987         master->irq             = irq;
988         master->info            = pdev->dev.platform_data;
989         master->fsia.base       = master->base;
990         master->fsia.master     = master;
991         master->fsib.base       = master->base + 0x40;
992         master->fsib.master     = master;
993         spin_lock_init(&master->lock);
994
995         pm_runtime_enable(&pdev->dev);
996         pm_runtime_resume(&pdev->dev);
997
998         fsi_soc_dai[0].dev              = &pdev->dev;
999         fsi_soc_dai[0].private_data     = &master->fsia;
1000         fsi_soc_dai[1].dev              = &pdev->dev;
1001         fsi_soc_dai[1].private_data     = &master->fsib;
1002
1003         fsi_soft_all_reset(master);
1004
1005         ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
1006         if (ret) {
1007                 dev_err(&pdev->dev, "irq request err\n");
1008                 goto exit_iounmap;
1009         }
1010
1011         ret = snd_soc_register_platform(&fsi_soc_platform);
1012         if (ret < 0) {
1013                 dev_err(&pdev->dev, "cannot snd soc register\n");
1014                 goto exit_free_irq;
1015         }
1016
1017         return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
1018
1019 exit_free_irq:
1020         free_irq(irq, master);
1021 exit_iounmap:
1022         iounmap(master->base);
1023         pm_runtime_disable(&pdev->dev);
1024 exit_kfree:
1025         kfree(master);
1026         master = NULL;
1027 exit:
1028         return ret;
1029 }
1030
1031 static int fsi_remove(struct platform_device *pdev)
1032 {
1033         struct fsi_master *master;
1034
1035         master = fsi_get_master(fsi_soc_dai[0].private_data);
1036
1037         snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
1038         snd_soc_unregister_platform(&fsi_soc_platform);
1039
1040         pm_runtime_disable(&pdev->dev);
1041
1042         free_irq(master->irq, master);
1043
1044         iounmap(master->base);
1045         kfree(master);
1046
1047         fsi_soc_dai[0].dev              = NULL;
1048         fsi_soc_dai[0].private_data     = NULL;
1049         fsi_soc_dai[1].dev              = NULL;
1050         fsi_soc_dai[1].private_data     = NULL;
1051
1052         return 0;
1053 }
1054
1055 static int fsi_runtime_nop(struct device *dev)
1056 {
1057         /* Runtime PM callback shared between ->runtime_suspend()
1058          * and ->runtime_resume(). Simply returns success.
1059          *
1060          * This driver re-initializes all registers after
1061          * pm_runtime_get_sync() anyway so there is no need
1062          * to save and restore registers here.
1063          */
1064         return 0;
1065 }
1066
1067 static struct dev_pm_ops fsi_pm_ops = {
1068         .runtime_suspend        = fsi_runtime_nop,
1069         .runtime_resume         = fsi_runtime_nop,
1070 };
1071
1072 static struct platform_driver fsi_driver = {
1073         .driver         = {
1074                 .name   = "sh_fsi",
1075                 .pm     = &fsi_pm_ops,
1076         },
1077         .probe          = fsi_probe,
1078         .remove         = fsi_remove,
1079 };
1080
1081 static int __init fsi_mobile_init(void)
1082 {
1083         return platform_driver_register(&fsi_driver);
1084 }
1085
1086 static void __exit fsi_mobile_exit(void)
1087 {
1088         platform_driver_unregister(&fsi_driver);
1089 }
1090 module_init(fsi_mobile_init);
1091 module_exit(fsi_mobile_exit);
1092
1093 MODULE_LICENSE("GPL");
1094 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1095 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");