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