]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/soc/samsung/i2s.c
ASoC: SAMSUNG: Move I2S common register definition
[mv-sheeva.git] / sound / soc / samsung / i2s.c
1 /* sound/soc/samsung/i2s.c
2  *
3  * ALSA SoC Audio Layer - Samsung I2S Controller driver
4  *
5  * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6  *      Jaswinder Singh <jassi.brar@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/clk.h>
16 #include <linux/io.h>
17
18 #include <sound/soc.h>
19 #include <sound/pcm_params.h>
20
21 #include <plat/audio.h>
22
23 #include "dma.h"
24 #include "i2s.h"
25 #include "i2s-regs.h"
26
27 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
28
29 struct i2s_dai {
30         /* Platform device for this DAI */
31         struct platform_device *pdev;
32         /* IOREMAP'd SFRs */
33         void __iomem    *addr;
34         /* Physical base address of SFRs */
35         u32     base;
36         /* Rate of RCLK source clock */
37         unsigned long rclk_srcrate;
38         /* Frame Clock */
39         unsigned frmclk;
40         /*
41          * Specifically requested RCLK,BCLK by MACHINE Driver.
42          * 0 indicates CPU driver is free to choose any value.
43          */
44         unsigned rfs, bfs;
45         /* I2S Controller's core clock */
46         struct clk *clk;
47         /* Clock for generating I2S signals */
48         struct clk *op_clk;
49         /* Array of clock names for op_clk */
50         const char **src_clk;
51         /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
52         struct i2s_dai *pri_dai;
53         /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
54         struct i2s_dai *sec_dai;
55 #define DAI_OPENED      (1 << 0) /* Dai is opened */
56 #define DAI_MANAGER     (1 << 1) /* Dai is the manager */
57         unsigned mode;
58         /* Driver for this DAI */
59         struct snd_soc_dai_driver i2s_dai_drv;
60         /* DMA parameters */
61         struct s3c_dma_params dma_playback;
62         struct s3c_dma_params dma_capture;
63         u32     quirks;
64         u32     suspend_i2smod;
65         u32     suspend_i2scon;
66         u32     suspend_i2spsr;
67 };
68
69 /* Lock for cross i/f checks */
70 static DEFINE_SPINLOCK(lock);
71
72 /* If this is the 'overlay' stereo DAI */
73 static inline bool is_secondary(struct i2s_dai *i2s)
74 {
75         return i2s->pri_dai ? true : false;
76 }
77
78 /* If operating in SoC-Slave mode */
79 static inline bool is_slave(struct i2s_dai *i2s)
80 {
81         return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
82 }
83
84 /* If this interface of the controller is transmitting data */
85 static inline bool tx_active(struct i2s_dai *i2s)
86 {
87         u32 active;
88
89         if (!i2s)
90                 return false;
91
92         active = readl(i2s->addr + I2SCON);
93
94         if (is_secondary(i2s))
95                 active &= CON_TXSDMA_ACTIVE;
96         else
97                 active &= CON_TXDMA_ACTIVE;
98
99         return active ? true : false;
100 }
101
102 /* If the other interface of the controller is transmitting data */
103 static inline bool other_tx_active(struct i2s_dai *i2s)
104 {
105         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
106
107         return tx_active(other);
108 }
109
110 /* If any interface of the controller is transmitting data */
111 static inline bool any_tx_active(struct i2s_dai *i2s)
112 {
113         return tx_active(i2s) || other_tx_active(i2s);
114 }
115
116 /* If this interface of the controller is receiving data */
117 static inline bool rx_active(struct i2s_dai *i2s)
118 {
119         u32 active;
120
121         if (!i2s)
122                 return false;
123
124         active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
125
126         return active ? true : false;
127 }
128
129 /* If the other interface of the controller is receiving data */
130 static inline bool other_rx_active(struct i2s_dai *i2s)
131 {
132         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
133
134         return rx_active(other);
135 }
136
137 /* If any interface of the controller is receiving data */
138 static inline bool any_rx_active(struct i2s_dai *i2s)
139 {
140         return rx_active(i2s) || other_rx_active(i2s);
141 }
142
143 /* If the other DAI is transmitting or receiving data */
144 static inline bool other_active(struct i2s_dai *i2s)
145 {
146         return other_rx_active(i2s) || other_tx_active(i2s);
147 }
148
149 /* If this DAI is transmitting or receiving data */
150 static inline bool this_active(struct i2s_dai *i2s)
151 {
152         return tx_active(i2s) || rx_active(i2s);
153 }
154
155 /* If the controller is active anyway */
156 static inline bool any_active(struct i2s_dai *i2s)
157 {
158         return this_active(i2s) || other_active(i2s);
159 }
160
161 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
162 {
163         return snd_soc_dai_get_drvdata(dai);
164 }
165
166 static inline bool is_opened(struct i2s_dai *i2s)
167 {
168         if (i2s && (i2s->mode & DAI_OPENED))
169                 return true;
170         else
171                 return false;
172 }
173
174 static inline bool is_manager(struct i2s_dai *i2s)
175 {
176         if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
177                 return true;
178         else
179                 return false;
180 }
181
182 /* Read RCLK of I2S (in multiples of LRCLK) */
183 static inline unsigned get_rfs(struct i2s_dai *i2s)
184 {
185         u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3;
186
187         switch (rfs) {
188         case 3: return 768;
189         case 2: return 384;
190         case 1: return 512;
191         default: return 256;
192         }
193 }
194
195 /* Write RCLK of I2S (in multiples of LRCLK) */
196 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
197 {
198         u32 mod = readl(i2s->addr + I2SMOD);
199
200         mod &= ~MOD_RCLK_MASK;
201
202         switch (rfs) {
203         case 768:
204                 mod |= MOD_RCLK_768FS;
205                 break;
206         case 512:
207                 mod |= MOD_RCLK_512FS;
208                 break;
209         case 384:
210                 mod |= MOD_RCLK_384FS;
211                 break;
212         default:
213                 mod |= MOD_RCLK_256FS;
214                 break;
215         }
216
217         writel(mod, i2s->addr + I2SMOD);
218 }
219
220 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
221 static inline unsigned get_bfs(struct i2s_dai *i2s)
222 {
223         u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3;
224
225         switch (bfs) {
226         case 3: return 24;
227         case 2: return 16;
228         case 1: return 48;
229         default: return 32;
230         }
231 }
232
233 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
234 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
235 {
236         u32 mod = readl(i2s->addr + I2SMOD);
237
238         mod &= ~MOD_BCLK_MASK;
239
240         switch (bfs) {
241         case 48:
242                 mod |= MOD_BCLK_48FS;
243                 break;
244         case 32:
245                 mod |= MOD_BCLK_32FS;
246                 break;
247         case 24:
248                 mod |= MOD_BCLK_24FS;
249                 break;
250         case 16:
251                 mod |= MOD_BCLK_16FS;
252                 break;
253         default:
254                 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
255                 return;
256         }
257
258         writel(mod, i2s->addr + I2SMOD);
259 }
260
261 /* Sample-Size */
262 static inline int get_blc(struct i2s_dai *i2s)
263 {
264         int blc = readl(i2s->addr + I2SMOD);
265
266         blc = (blc >> 13) & 0x3;
267
268         switch (blc) {
269         case 2: return 24;
270         case 1: return 8;
271         default: return 16;
272         }
273 }
274
275 /* TX Channel Control */
276 static void i2s_txctrl(struct i2s_dai *i2s, int on)
277 {
278         void __iomem *addr = i2s->addr;
279         u32 con = readl(addr + I2SCON);
280         u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
281
282         if (on) {
283                 con |= CON_ACTIVE;
284                 con &= ~CON_TXCH_PAUSE;
285
286                 if (is_secondary(i2s)) {
287                         con |= CON_TXSDMA_ACTIVE;
288                         con &= ~CON_TXSDMA_PAUSE;
289                 } else {
290                         con |= CON_TXDMA_ACTIVE;
291                         con &= ~CON_TXDMA_PAUSE;
292                 }
293
294                 if (any_rx_active(i2s))
295                         mod |= MOD_TXRX;
296                 else
297                         mod |= MOD_TXONLY;
298         } else {
299                 if (is_secondary(i2s)) {
300                         con |=  CON_TXSDMA_PAUSE;
301                         con &= ~CON_TXSDMA_ACTIVE;
302                 } else {
303                         con |=  CON_TXDMA_PAUSE;
304                         con &= ~CON_TXDMA_ACTIVE;
305                 }
306
307                 if (other_tx_active(i2s)) {
308                         writel(con, addr + I2SCON);
309                         return;
310                 }
311
312                 con |=  CON_TXCH_PAUSE;
313
314                 if (any_rx_active(i2s))
315                         mod |= MOD_RXONLY;
316                 else
317                         con &= ~CON_ACTIVE;
318         }
319
320         writel(mod, addr + I2SMOD);
321         writel(con, addr + I2SCON);
322 }
323
324 /* RX Channel Control */
325 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
326 {
327         void __iomem *addr = i2s->addr;
328         u32 con = readl(addr + I2SCON);
329         u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
330
331         if (on) {
332                 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
333                 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
334
335                 if (any_tx_active(i2s))
336                         mod |= MOD_TXRX;
337                 else
338                         mod |= MOD_RXONLY;
339         } else {
340                 con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
341                 con &= ~CON_RXDMA_ACTIVE;
342
343                 if (any_tx_active(i2s))
344                         mod |= MOD_TXONLY;
345                 else
346                         con &= ~CON_ACTIVE;
347         }
348
349         writel(mod, addr + I2SMOD);
350         writel(con, addr + I2SCON);
351 }
352
353 /* Flush FIFO of an interface */
354 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
355 {
356         void __iomem *fic;
357         u32 val;
358
359         if (!i2s)
360                 return;
361
362         if (is_secondary(i2s))
363                 fic = i2s->addr + I2SFICS;
364         else
365                 fic = i2s->addr + I2SFIC;
366
367         /* Flush the FIFO */
368         writel(readl(fic) | flush, fic);
369
370         /* Be patient */
371         val = msecs_to_loops(1) / 1000; /* 1 usec */
372         while (--val)
373                 cpu_relax();
374
375         writel(readl(fic) & ~flush, fic);
376 }
377
378 static int i2s_set_sysclk(struct snd_soc_dai *dai,
379           int clk_id, unsigned int rfs, int dir)
380 {
381         struct i2s_dai *i2s = to_info(dai);
382         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
383         u32 mod = readl(i2s->addr + I2SMOD);
384
385         switch (clk_id) {
386         case SAMSUNG_I2S_CDCLK:
387                 /* Shouldn't matter in GATING(CLOCK_IN) mode */
388                 if (dir == SND_SOC_CLOCK_IN)
389                         rfs = 0;
390
391                 if ((rfs && other->rfs && (other->rfs != rfs)) ||
392                                 (any_active(i2s) &&
393                                 (((dir == SND_SOC_CLOCK_IN)
394                                         && !(mod & MOD_CDCLKCON)) ||
395                                 ((dir == SND_SOC_CLOCK_OUT)
396                                         && (mod & MOD_CDCLKCON))))) {
397                         dev_err(&i2s->pdev->dev,
398                                 "%s:%d Other DAI busy\n", __func__, __LINE__);
399                         return -EAGAIN;
400                 }
401
402                 if (dir == SND_SOC_CLOCK_IN)
403                         mod |= MOD_CDCLKCON;
404                 else
405                         mod &= ~MOD_CDCLKCON;
406
407                 i2s->rfs = rfs;
408                 break;
409
410         case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
411         case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
412                 if ((i2s->quirks & QUIRK_NO_MUXPSR)
413                                 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
414                         clk_id = 0;
415                 else
416                         clk_id = 1;
417
418                 if (!any_active(i2s)) {
419                         if (i2s->op_clk) {
420                                 if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
421                                         (!clk_id && (mod & MOD_IMS_SYSMUX))) {
422                                         clk_disable(i2s->op_clk);
423                                         clk_put(i2s->op_clk);
424                                 } else {
425                                         i2s->rclk_srcrate =
426                                                 clk_get_rate(i2s->op_clk);
427                                         return 0;
428                                 }
429                         }
430
431                         i2s->op_clk = clk_get(&i2s->pdev->dev,
432                                                 i2s->src_clk[clk_id]);
433                         clk_enable(i2s->op_clk);
434                         i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
435
436                         /* Over-ride the other's */
437                         if (other) {
438                                 other->op_clk = i2s->op_clk;
439                                 other->rclk_srcrate = i2s->rclk_srcrate;
440                         }
441                 } else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
442                                 || (clk_id && !(mod & MOD_IMS_SYSMUX))) {
443                         dev_err(&i2s->pdev->dev,
444                                 "%s:%d Other DAI busy\n", __func__, __LINE__);
445                         return -EAGAIN;
446                 } else {
447                         /* Call can't be on the active DAI */
448                         i2s->op_clk = other->op_clk;
449                         i2s->rclk_srcrate = other->rclk_srcrate;
450                         return 0;
451                 }
452
453                 if (clk_id == 0)
454                         mod &= ~MOD_IMS_SYSMUX;
455                 else
456                         mod |= MOD_IMS_SYSMUX;
457                 break;
458
459         default:
460                 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
461                 return -EINVAL;
462         }
463
464         writel(mod, i2s->addr + I2SMOD);
465
466         return 0;
467 }
468
469 static int i2s_set_fmt(struct snd_soc_dai *dai,
470         unsigned int fmt)
471 {
472         struct i2s_dai *i2s = to_info(dai);
473         u32 mod = readl(i2s->addr + I2SMOD);
474         u32 tmp = 0;
475
476         /* Format is priority */
477         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
478         case SND_SOC_DAIFMT_RIGHT_J:
479                 tmp |= MOD_LR_RLOW;
480                 tmp |= MOD_SDF_MSB;
481                 break;
482         case SND_SOC_DAIFMT_LEFT_J:
483                 tmp |= MOD_LR_RLOW;
484                 tmp |= MOD_SDF_LSB;
485                 break;
486         case SND_SOC_DAIFMT_I2S:
487                 tmp |= MOD_SDF_IIS;
488                 break;
489         default:
490                 dev_err(&i2s->pdev->dev, "Format not supported\n");
491                 return -EINVAL;
492         }
493
494         /*
495          * INV flag is relative to the FORMAT flag - if set it simply
496          * flips the polarity specified by the Standard
497          */
498         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
499         case SND_SOC_DAIFMT_NB_NF:
500                 break;
501         case SND_SOC_DAIFMT_NB_IF:
502                 if (tmp & MOD_LR_RLOW)
503                         tmp &= ~MOD_LR_RLOW;
504                 else
505                         tmp |= MOD_LR_RLOW;
506                 break;
507         default:
508                 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
509                 return -EINVAL;
510         }
511
512         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
513         case SND_SOC_DAIFMT_CBM_CFM:
514                 tmp |= MOD_SLAVE;
515                 break;
516         case SND_SOC_DAIFMT_CBS_CFS:
517                 /* Set default source clock in Master mode */
518                 if (i2s->rclk_srcrate == 0)
519                         i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
520                                                         0, SND_SOC_CLOCK_IN);
521                 break;
522         default:
523                 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
524                 return -EINVAL;
525         }
526
527         if (any_active(i2s) &&
528                         ((mod & (MOD_SDF_MASK | MOD_LR_RLOW
529                                 | MOD_SLAVE)) != tmp)) {
530                 dev_err(&i2s->pdev->dev,
531                                 "%s:%d Other DAI busy\n", __func__, __LINE__);
532                 return -EAGAIN;
533         }
534
535         mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
536         mod |= tmp;
537         writel(mod, i2s->addr + I2SMOD);
538
539         return 0;
540 }
541
542 static int i2s_hw_params(struct snd_pcm_substream *substream,
543         struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
544 {
545         struct i2s_dai *i2s = to_info(dai);
546         u32 mod = readl(i2s->addr + I2SMOD);
547
548         if (!is_secondary(i2s))
549                 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
550
551         switch (params_channels(params)) {
552         case 6:
553                 mod |= MOD_DC2_EN;
554         case 4:
555                 mod |= MOD_DC1_EN;
556                 break;
557         case 2:
558                 break;
559         default:
560                 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
561                                 params_channels(params));
562                 return -EINVAL;
563         }
564
565         if (is_secondary(i2s))
566                 mod &= ~MOD_BLCS_MASK;
567         else
568                 mod &= ~MOD_BLCP_MASK;
569
570         if (is_manager(i2s))
571                 mod &= ~MOD_BLC_MASK;
572
573         switch (params_format(params)) {
574         case SNDRV_PCM_FORMAT_S8:
575                 if (is_secondary(i2s))
576                         mod |= MOD_BLCS_8BIT;
577                 else
578                         mod |= MOD_BLCP_8BIT;
579                 if (is_manager(i2s))
580                         mod |= MOD_BLC_8BIT;
581                 break;
582         case SNDRV_PCM_FORMAT_S16_LE:
583                 if (is_secondary(i2s))
584                         mod |= MOD_BLCS_16BIT;
585                 else
586                         mod |= MOD_BLCP_16BIT;
587                 if (is_manager(i2s))
588                         mod |= MOD_BLC_16BIT;
589                 break;
590         case SNDRV_PCM_FORMAT_S24_LE:
591                 if (is_secondary(i2s))
592                         mod |= MOD_BLCS_24BIT;
593                 else
594                         mod |= MOD_BLCP_24BIT;
595                 if (is_manager(i2s))
596                         mod |= MOD_BLC_24BIT;
597                 break;
598         default:
599                 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
600                                 params_format(params));
601                 return -EINVAL;
602         }
603         writel(mod, i2s->addr + I2SMOD);
604
605         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
606                 snd_soc_dai_set_dma_data(dai, substream,
607                         (void *)&i2s->dma_playback);
608         else
609                 snd_soc_dai_set_dma_data(dai, substream,
610                         (void *)&i2s->dma_capture);
611
612         i2s->frmclk = params_rate(params);
613
614         return 0;
615 }
616
617 /* We set constraints on the substream acc to the version of I2S */
618 static int i2s_startup(struct snd_pcm_substream *substream,
619           struct snd_soc_dai *dai)
620 {
621         struct i2s_dai *i2s = to_info(dai);
622         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
623         unsigned long flags;
624
625         spin_lock_irqsave(&lock, flags);
626
627         i2s->mode |= DAI_OPENED;
628
629         if (is_manager(other))
630                 i2s->mode &= ~DAI_MANAGER;
631         else
632                 i2s->mode |= DAI_MANAGER;
633
634         /* Enforce set_sysclk in Master mode */
635         i2s->rclk_srcrate = 0;
636
637         spin_unlock_irqrestore(&lock, flags);
638
639         return 0;
640 }
641
642 static void i2s_shutdown(struct snd_pcm_substream *substream,
643         struct snd_soc_dai *dai)
644 {
645         struct i2s_dai *i2s = to_info(dai);
646         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
647         unsigned long flags;
648
649         spin_lock_irqsave(&lock, flags);
650
651         i2s->mode &= ~DAI_OPENED;
652         i2s->mode &= ~DAI_MANAGER;
653
654         if (is_opened(other))
655                 other->mode |= DAI_MANAGER;
656
657         /* Reset any constraint on RFS and BFS */
658         i2s->rfs = 0;
659         i2s->bfs = 0;
660
661         spin_unlock_irqrestore(&lock, flags);
662
663         /* Gate CDCLK by default */
664         if (!is_opened(other))
665                 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
666                                 0, SND_SOC_CLOCK_IN);
667 }
668
669 static int config_setup(struct i2s_dai *i2s)
670 {
671         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
672         unsigned rfs, bfs, blc;
673         u32 psr;
674
675         blc = get_blc(i2s);
676
677         bfs = i2s->bfs;
678
679         if (!bfs && other)
680                 bfs = other->bfs;
681
682         /* Select least possible multiple(2) if no constraint set */
683         if (!bfs)
684                 bfs = blc * 2;
685
686         rfs = i2s->rfs;
687
688         if (!rfs && other)
689                 rfs = other->rfs;
690
691         if ((rfs == 256 || rfs == 512) && (blc == 24)) {
692                 dev_err(&i2s->pdev->dev,
693                         "%d-RFS not supported for 24-blc\n", rfs);
694                 return -EINVAL;
695         }
696
697         if (!rfs) {
698                 if (bfs == 16 || bfs == 32)
699                         rfs = 256;
700                 else
701                         rfs = 384;
702         }
703
704         /* If already setup and running */
705         if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
706                 dev_err(&i2s->pdev->dev,
707                                 "%s:%d Other DAI busy\n", __func__, __LINE__);
708                 return -EAGAIN;
709         }
710
711         /* Don't bother RFS, BFS & PSR in Slave mode */
712         if (is_slave(i2s))
713                 return 0;
714
715         set_bfs(i2s, bfs);
716         set_rfs(i2s, rfs);
717
718         if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
719                 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
720                 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
721                 dev_dbg(&i2s->pdev->dev,
722                         "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
723                                 i2s->rclk_srcrate, psr, rfs, bfs);
724         }
725
726         return 0;
727 }
728
729 static int i2s_trigger(struct snd_pcm_substream *substream,
730         int cmd, struct snd_soc_dai *dai)
731 {
732         int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
733         struct snd_soc_pcm_runtime *rtd = substream->private_data;
734         struct i2s_dai *i2s = to_info(rtd->cpu_dai);
735         unsigned long flags;
736
737         switch (cmd) {
738         case SNDRV_PCM_TRIGGER_START:
739         case SNDRV_PCM_TRIGGER_RESUME:
740         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
741                 local_irq_save(flags);
742
743                 if (config_setup(i2s)) {
744                         local_irq_restore(flags);
745                         return -EINVAL;
746                 }
747
748                 if (capture)
749                         i2s_rxctrl(i2s, 1);
750                 else
751                         i2s_txctrl(i2s, 1);
752
753                 local_irq_restore(flags);
754                 break;
755         case SNDRV_PCM_TRIGGER_STOP:
756         case SNDRV_PCM_TRIGGER_SUSPEND:
757         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
758                 local_irq_save(flags);
759
760                 if (capture)
761                         i2s_rxctrl(i2s, 0);
762                 else
763                         i2s_txctrl(i2s, 0);
764
765                 if (capture)
766                         i2s_fifo(i2s, FIC_RXFLUSH);
767                 else
768                         i2s_fifo(i2s, FIC_TXFLUSH);
769
770                 local_irq_restore(flags);
771                 break;
772         }
773
774         return 0;
775 }
776
777 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
778         int div_id, int div)
779 {
780         struct i2s_dai *i2s = to_info(dai);
781         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
782
783         switch (div_id) {
784         case SAMSUNG_I2S_DIV_BCLK:
785                 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
786                         || (other && other->bfs && (other->bfs != div))) {
787                         dev_err(&i2s->pdev->dev,
788                                 "%s:%d Other DAI busy\n", __func__, __LINE__);
789                         return -EAGAIN;
790                 }
791                 i2s->bfs = div;
792                 break;
793         default:
794                 dev_err(&i2s->pdev->dev,
795                         "Invalid clock divider(%d)\n", div_id);
796                 return -EINVAL;
797         }
798
799         return 0;
800 }
801
802 static snd_pcm_sframes_t
803 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
804 {
805         struct i2s_dai *i2s = to_info(dai);
806         u32 reg = readl(i2s->addr + I2SFIC);
807         snd_pcm_sframes_t delay;
808
809         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
810                 delay = FIC_RXCOUNT(reg);
811         else if (is_secondary(i2s))
812                 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
813         else
814                 delay = FIC_TXCOUNT(reg);
815
816         return delay;
817 }
818
819 #ifdef CONFIG_PM
820 static int i2s_suspend(struct snd_soc_dai *dai)
821 {
822         struct i2s_dai *i2s = to_info(dai);
823
824         if (dai->active) {
825                 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
826                 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
827                 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
828         }
829
830         return 0;
831 }
832
833 static int i2s_resume(struct snd_soc_dai *dai)
834 {
835         struct i2s_dai *i2s = to_info(dai);
836
837         if (dai->active) {
838                 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
839                 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
840                 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
841         }
842
843         return 0;
844 }
845 #else
846 #define i2s_suspend NULL
847 #define i2s_resume  NULL
848 #endif
849
850 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
851 {
852         struct i2s_dai *i2s = to_info(dai);
853         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
854
855         if (other && other->clk) /* If this is probe on secondary */
856                 goto probe_exit;
857
858         i2s->addr = ioremap(i2s->base, 0x100);
859         if (i2s->addr == NULL) {
860                 dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
861                 return -ENXIO;
862         }
863
864         i2s->clk = clk_get(&i2s->pdev->dev, "iis");
865         if (IS_ERR(i2s->clk)) {
866                 dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
867                 iounmap(i2s->addr);
868                 return -ENOENT;
869         }
870         clk_enable(i2s->clk);
871
872         if (other) {
873                 other->addr = i2s->addr;
874                 other->clk = i2s->clk;
875         }
876
877         if (i2s->quirks & QUIRK_NEED_RSTCLR)
878                 writel(CON_RSTCLR, i2s->addr + I2SCON);
879
880 probe_exit:
881         /* Reset any constraint on RFS and BFS */
882         i2s->rfs = 0;
883         i2s->bfs = 0;
884         i2s_txctrl(i2s, 0);
885         i2s_rxctrl(i2s, 0);
886         i2s_fifo(i2s, FIC_TXFLUSH);
887         i2s_fifo(other, FIC_TXFLUSH);
888         i2s_fifo(i2s, FIC_RXFLUSH);
889
890         /* Gate CDCLK by default */
891         if (!is_opened(other))
892                 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
893                                 0, SND_SOC_CLOCK_IN);
894
895         return 0;
896 }
897
898 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
899 {
900         struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
901         struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
902
903         if (!other || !other->clk) {
904
905                 if (i2s->quirks & QUIRK_NEED_RSTCLR)
906                         writel(0, i2s->addr + I2SCON);
907
908                 clk_disable(i2s->clk);
909                 clk_put(i2s->clk);
910
911                 iounmap(i2s->addr);
912         }
913
914         i2s->clk = NULL;
915
916         return 0;
917 }
918
919 static struct snd_soc_dai_ops samsung_i2s_dai_ops = {
920         .trigger = i2s_trigger,
921         .hw_params = i2s_hw_params,
922         .set_fmt = i2s_set_fmt,
923         .set_clkdiv = i2s_set_clkdiv,
924         .set_sysclk = i2s_set_sysclk,
925         .startup = i2s_startup,
926         .shutdown = i2s_shutdown,
927         .delay = i2s_delay,
928 };
929
930 #define SAMSUNG_I2S_RATES       SNDRV_PCM_RATE_8000_96000
931
932 #define SAMSUNG_I2S_FMTS        (SNDRV_PCM_FMTBIT_S8 | \
933                                         SNDRV_PCM_FMTBIT_S16_LE | \
934                                         SNDRV_PCM_FMTBIT_S24_LE)
935
936 static __devinit
937 struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
938 {
939         struct i2s_dai *i2s;
940
941         i2s = kzalloc(sizeof(struct i2s_dai), GFP_KERNEL);
942         if (i2s == NULL)
943                 return NULL;
944
945         i2s->pdev = pdev;
946         i2s->pri_dai = NULL;
947         i2s->sec_dai = NULL;
948         i2s->i2s_dai_drv.symmetric_rates = 1;
949         i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
950         i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
951         i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
952         i2s->i2s_dai_drv.suspend = i2s_suspend;
953         i2s->i2s_dai_drv.resume = i2s_resume;
954         i2s->i2s_dai_drv.playback.channels_min = 2;
955         i2s->i2s_dai_drv.playback.channels_max = 2;
956         i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
957         i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
958
959         if (!sec) {
960                 i2s->i2s_dai_drv.capture.channels_min = 2;
961                 i2s->i2s_dai_drv.capture.channels_max = 2;
962                 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
963                 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
964         } else {        /* Create a new platform_device for Secondary */
965                 i2s->pdev = platform_device_register_resndata(NULL,
966                                 pdev->name, pdev->id + SAMSUNG_I2S_SECOFF,
967                                 NULL, 0, NULL, 0);
968                 if (IS_ERR(i2s->pdev)) {
969                         kfree(i2s);
970                         return NULL;
971                 }
972         }
973
974         /* Pre-assign snd_soc_dai_set_drvdata */
975         dev_set_drvdata(&i2s->pdev->dev, i2s);
976
977         return i2s;
978 }
979
980 static __devinit int samsung_i2s_probe(struct platform_device *pdev)
981 {
982         u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan;
983         struct i2s_dai *pri_dai, *sec_dai = NULL;
984         struct s3c_audio_pdata *i2s_pdata;
985         struct samsung_i2s *i2s_cfg;
986         struct resource *res;
987         u32 regs_base, quirks;
988         int ret = 0;
989
990         /* Call during Seconday interface registration */
991         if (pdev->id >= SAMSUNG_I2S_SECOFF) {
992                 sec_dai = dev_get_drvdata(&pdev->dev);
993                 snd_soc_register_dai(&sec_dai->pdev->dev,
994                         &sec_dai->i2s_dai_drv);
995                 return 0;
996         }
997
998         i2s_pdata = pdev->dev.platform_data;
999         if (i2s_pdata == NULL) {
1000                 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1001                 return -EINVAL;
1002         }
1003
1004         res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1005         if (!res) {
1006                 dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n");
1007                 return -ENXIO;
1008         }
1009         dma_pl_chan = res->start;
1010
1011         res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1012         if (!res) {
1013                 dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n");
1014                 return -ENXIO;
1015         }
1016         dma_cp_chan = res->start;
1017
1018         res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1019         if (res)
1020                 dma_pl_sec_chan = res->start;
1021         else
1022                 dma_pl_sec_chan = 0;
1023
1024         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1025         if (!res) {
1026                 dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1027                 return -ENXIO;
1028         }
1029
1030         if (!request_mem_region(res->start, resource_size(res),
1031                                                         "samsung-i2s")) {
1032                 dev_err(&pdev->dev, "Unable to request SFR region\n");
1033                 return -EBUSY;
1034         }
1035         regs_base = res->start;
1036
1037         i2s_cfg = &i2s_pdata->type.i2s;
1038         quirks = i2s_cfg->quirks;
1039
1040         pri_dai = i2s_alloc_dai(pdev, false);
1041         if (!pri_dai) {
1042                 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1043                 ret = -ENOMEM;
1044                 goto err1;
1045         }
1046
1047         pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1048         pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1049         pri_dai->dma_playback.client =
1050                 (struct s3c2410_dma_client *)&pri_dai->dma_playback;
1051         pri_dai->dma_capture.client =
1052                 (struct s3c2410_dma_client *)&pri_dai->dma_capture;
1053         pri_dai->dma_playback.channel = dma_pl_chan;
1054         pri_dai->dma_capture.channel = dma_cp_chan;
1055         pri_dai->src_clk = i2s_cfg->src_clk;
1056         pri_dai->dma_playback.dma_size = 4;
1057         pri_dai->dma_capture.dma_size = 4;
1058         pri_dai->base = regs_base;
1059         pri_dai->quirks = quirks;
1060
1061         if (quirks & QUIRK_PRI_6CHAN)
1062                 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1063
1064         if (quirks & QUIRK_SEC_DAI) {
1065                 sec_dai = i2s_alloc_dai(pdev, true);
1066                 if (!sec_dai) {
1067                         dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1068                         ret = -ENOMEM;
1069                         goto err2;
1070                 }
1071                 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1072                 sec_dai->dma_playback.client =
1073                         (struct s3c2410_dma_client *)&sec_dai->dma_playback;
1074                 /* Use iDMA always if SysDMA not provided */
1075                 sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
1076                 sec_dai->src_clk = i2s_cfg->src_clk;
1077                 sec_dai->dma_playback.dma_size = 4;
1078                 sec_dai->base = regs_base;
1079                 sec_dai->quirks = quirks;
1080                 sec_dai->pri_dai = pri_dai;
1081                 pri_dai->sec_dai = sec_dai;
1082         }
1083
1084         if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1085                 dev_err(&pdev->dev, "Unable to configure gpio\n");
1086                 ret = -EINVAL;
1087                 goto err3;
1088         }
1089
1090         snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
1091
1092         return 0;
1093 err3:
1094         kfree(sec_dai);
1095 err2:
1096         kfree(pri_dai);
1097 err1:
1098         release_mem_region(regs_base, resource_size(res));
1099
1100         return ret;
1101 }
1102
1103 static __devexit int samsung_i2s_remove(struct platform_device *pdev)
1104 {
1105         struct i2s_dai *i2s, *other;
1106
1107         i2s = dev_get_drvdata(&pdev->dev);
1108         other = i2s->pri_dai ? : i2s->sec_dai;
1109
1110         if (other) {
1111                 other->pri_dai = NULL;
1112                 other->sec_dai = NULL;
1113         } else {
1114                 struct resource *res;
1115                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1116                 if (res)
1117                         release_mem_region(res->start, resource_size(res));
1118         }
1119
1120         i2s->pri_dai = NULL;
1121         i2s->sec_dai = NULL;
1122
1123         kfree(i2s);
1124
1125         snd_soc_unregister_dai(&pdev->dev);
1126
1127         return 0;
1128 }
1129
1130 static struct platform_driver samsung_i2s_driver = {
1131         .probe  = samsung_i2s_probe,
1132         .remove = samsung_i2s_remove,
1133         .driver = {
1134                 .name = "samsung-i2s",
1135                 .owner = THIS_MODULE,
1136         },
1137 };
1138
1139 static int __init samsung_i2s_init(void)
1140 {
1141         return platform_driver_register(&samsung_i2s_driver);
1142 }
1143 module_init(samsung_i2s_init);
1144
1145 static void __exit samsung_i2s_exit(void)
1146 {
1147         platform_driver_unregister(&samsung_i2s_driver);
1148 }
1149 module_exit(samsung_i2s_exit);
1150
1151 /* Module information */
1152 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
1153 MODULE_DESCRIPTION("Samsung I2S Interface");
1154 MODULE_ALIAS("platform:samsung-i2s");
1155 MODULE_LICENSE("GPL");