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