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