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