staging: xgifb: vb_setmode: make XGI_SetXG27FPBits() static
[linux/fpc-iii.git] / sound / soc / samsung / i2s.c
blobbff42bf370b9c8849874db4792150fad27328010
1 /* sound/soc/samsung/i2s.c
3 * ALSA SoC Audio Layer - Samsung I2S Controller driver
5 * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassi.brar@samsung.com>
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.
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>
19 #include <sound/soc.h>
20 #include <sound/pcm_params.h>
22 #include <plat/audio.h>
24 #include "dma.h"
25 #include "idma.h"
26 #include "i2s.h"
27 #include "i2s-regs.h"
29 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
31 struct i2s_dai {
32 /* Platform device for this DAI */
33 struct platform_device *pdev;
34 /* IOREMAP'd SFRs */
35 void __iomem *addr;
36 /* Physical base address of SFRs */
37 u32 base;
38 /* Rate of RCLK source clock */
39 unsigned long rclk_srcrate;
40 /* Frame Clock */
41 unsigned frmclk;
43 * Specifically requested RCLK,BCLK by MACHINE Driver.
44 * 0 indicates CPU driver is free to choose any value.
46 unsigned rfs, bfs;
47 /* I2S Controller's core clock */
48 struct clk *clk;
49 /* Clock for generating I2S signals */
50 struct clk *op_clk;
51 /* Array of clock names for op_clk */
52 const char **src_clk;
53 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
54 struct i2s_dai *pri_dai;
55 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
56 struct i2s_dai *sec_dai;
57 #define DAI_OPENED (1 << 0) /* Dai is opened */
58 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
59 unsigned mode;
60 /* Driver for this DAI */
61 struct snd_soc_dai_driver i2s_dai_drv;
62 /* DMA parameters */
63 struct s3c_dma_params dma_playback;
64 struct s3c_dma_params dma_capture;
65 struct s3c_dma_params idma_playback;
66 u32 quirks;
67 u32 suspend_i2smod;
68 u32 suspend_i2scon;
69 u32 suspend_i2spsr;
72 /* Lock for cross i/f checks */
73 static DEFINE_SPINLOCK(lock);
75 /* If this is the 'overlay' stereo DAI */
76 static inline bool is_secondary(struct i2s_dai *i2s)
78 return i2s->pri_dai ? true : false;
81 /* If operating in SoC-Slave mode */
82 static inline bool is_slave(struct i2s_dai *i2s)
84 return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
87 /* If this interface of the controller is transmitting data */
88 static inline bool tx_active(struct i2s_dai *i2s)
90 u32 active;
92 if (!i2s)
93 return false;
95 active = readl(i2s->addr + I2SCON);
97 if (is_secondary(i2s))
98 active &= CON_TXSDMA_ACTIVE;
99 else
100 active &= CON_TXDMA_ACTIVE;
102 return active ? true : false;
105 /* If the other interface of the controller is transmitting data */
106 static inline bool other_tx_active(struct i2s_dai *i2s)
108 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
110 return tx_active(other);
113 /* If any interface of the controller is transmitting data */
114 static inline bool any_tx_active(struct i2s_dai *i2s)
116 return tx_active(i2s) || other_tx_active(i2s);
119 /* If this interface of the controller is receiving data */
120 static inline bool rx_active(struct i2s_dai *i2s)
122 u32 active;
124 if (!i2s)
125 return false;
127 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
129 return active ? true : false;
132 /* If the other interface of the controller is receiving data */
133 static inline bool other_rx_active(struct i2s_dai *i2s)
135 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
137 return rx_active(other);
140 /* If any interface of the controller is receiving data */
141 static inline bool any_rx_active(struct i2s_dai *i2s)
143 return rx_active(i2s) || other_rx_active(i2s);
146 /* If the other DAI is transmitting or receiving data */
147 static inline bool other_active(struct i2s_dai *i2s)
149 return other_rx_active(i2s) || other_tx_active(i2s);
152 /* If this DAI is transmitting or receiving data */
153 static inline bool this_active(struct i2s_dai *i2s)
155 return tx_active(i2s) || rx_active(i2s);
158 /* If the controller is active anyway */
159 static inline bool any_active(struct i2s_dai *i2s)
161 return this_active(i2s) || other_active(i2s);
164 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
166 return snd_soc_dai_get_drvdata(dai);
169 static inline bool is_opened(struct i2s_dai *i2s)
171 if (i2s && (i2s->mode & DAI_OPENED))
172 return true;
173 else
174 return false;
177 static inline bool is_manager(struct i2s_dai *i2s)
179 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
180 return true;
181 else
182 return false;
185 /* Read RCLK of I2S (in multiples of LRCLK) */
186 static inline unsigned get_rfs(struct i2s_dai *i2s)
188 u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3;
190 switch (rfs) {
191 case 3: return 768;
192 case 2: return 384;
193 case 1: return 512;
194 default: return 256;
198 /* Write RCLK of I2S (in multiples of LRCLK) */
199 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
201 u32 mod = readl(i2s->addr + I2SMOD);
203 mod &= ~MOD_RCLK_MASK;
205 switch (rfs) {
206 case 768:
207 mod |= MOD_RCLK_768FS;
208 break;
209 case 512:
210 mod |= MOD_RCLK_512FS;
211 break;
212 case 384:
213 mod |= MOD_RCLK_384FS;
214 break;
215 default:
216 mod |= MOD_RCLK_256FS;
217 break;
220 writel(mod, i2s->addr + I2SMOD);
223 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
224 static inline unsigned get_bfs(struct i2s_dai *i2s)
226 u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3;
228 switch (bfs) {
229 case 3: return 24;
230 case 2: return 16;
231 case 1: return 48;
232 default: return 32;
236 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
237 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
239 u32 mod = readl(i2s->addr + I2SMOD);
241 mod &= ~MOD_BCLK_MASK;
243 switch (bfs) {
244 case 48:
245 mod |= MOD_BCLK_48FS;
246 break;
247 case 32:
248 mod |= MOD_BCLK_32FS;
249 break;
250 case 24:
251 mod |= MOD_BCLK_24FS;
252 break;
253 case 16:
254 mod |= MOD_BCLK_16FS;
255 break;
256 default:
257 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
258 return;
261 writel(mod, i2s->addr + I2SMOD);
264 /* Sample-Size */
265 static inline int get_blc(struct i2s_dai *i2s)
267 int blc = readl(i2s->addr + I2SMOD);
269 blc = (blc >> 13) & 0x3;
271 switch (blc) {
272 case 2: return 24;
273 case 1: return 8;
274 default: return 16;
278 /* TX Channel Control */
279 static void i2s_txctrl(struct i2s_dai *i2s, int on)
281 void __iomem *addr = i2s->addr;
282 u32 con = readl(addr + I2SCON);
283 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
285 if (on) {
286 con |= CON_ACTIVE;
287 con &= ~CON_TXCH_PAUSE;
289 if (is_secondary(i2s)) {
290 con |= CON_TXSDMA_ACTIVE;
291 con &= ~CON_TXSDMA_PAUSE;
292 } else {
293 con |= CON_TXDMA_ACTIVE;
294 con &= ~CON_TXDMA_PAUSE;
297 if (any_rx_active(i2s))
298 mod |= MOD_TXRX;
299 else
300 mod |= MOD_TXONLY;
301 } else {
302 if (is_secondary(i2s)) {
303 con |= CON_TXSDMA_PAUSE;
304 con &= ~CON_TXSDMA_ACTIVE;
305 } else {
306 con |= CON_TXDMA_PAUSE;
307 con &= ~CON_TXDMA_ACTIVE;
310 if (other_tx_active(i2s)) {
311 writel(con, addr + I2SCON);
312 return;
315 con |= CON_TXCH_PAUSE;
317 if (any_rx_active(i2s))
318 mod |= MOD_RXONLY;
319 else
320 con &= ~CON_ACTIVE;
323 writel(mod, addr + I2SMOD);
324 writel(con, addr + I2SCON);
327 /* RX Channel Control */
328 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
330 void __iomem *addr = i2s->addr;
331 u32 con = readl(addr + I2SCON);
332 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
334 if (on) {
335 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
336 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
338 if (any_tx_active(i2s))
339 mod |= MOD_TXRX;
340 else
341 mod |= MOD_RXONLY;
342 } else {
343 con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
344 con &= ~CON_RXDMA_ACTIVE;
346 if (any_tx_active(i2s))
347 mod |= MOD_TXONLY;
348 else
349 con &= ~CON_ACTIVE;
352 writel(mod, addr + I2SMOD);
353 writel(con, addr + I2SCON);
356 /* Flush FIFO of an interface */
357 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
359 void __iomem *fic;
360 u32 val;
362 if (!i2s)
363 return;
365 if (is_secondary(i2s))
366 fic = i2s->addr + I2SFICS;
367 else
368 fic = i2s->addr + I2SFIC;
370 /* Flush the FIFO */
371 writel(readl(fic) | flush, fic);
373 /* Be patient */
374 val = msecs_to_loops(1) / 1000; /* 1 usec */
375 while (--val)
376 cpu_relax();
378 writel(readl(fic) & ~flush, fic);
381 static int i2s_set_sysclk(struct snd_soc_dai *dai,
382 int clk_id, unsigned int rfs, int dir)
384 struct i2s_dai *i2s = to_info(dai);
385 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
386 u32 mod = readl(i2s->addr + I2SMOD);
388 switch (clk_id) {
389 case SAMSUNG_I2S_CDCLK:
390 /* Shouldn't matter in GATING(CLOCK_IN) mode */
391 if (dir == SND_SOC_CLOCK_IN)
392 rfs = 0;
394 if ((rfs && other->rfs && (other->rfs != rfs)) ||
395 (any_active(i2s) &&
396 (((dir == SND_SOC_CLOCK_IN)
397 && !(mod & MOD_CDCLKCON)) ||
398 ((dir == SND_SOC_CLOCK_OUT)
399 && (mod & MOD_CDCLKCON))))) {
400 dev_err(&i2s->pdev->dev,
401 "%s:%d Other DAI busy\n", __func__, __LINE__);
402 return -EAGAIN;
405 if (dir == SND_SOC_CLOCK_IN)
406 mod |= MOD_CDCLKCON;
407 else
408 mod &= ~MOD_CDCLKCON;
410 i2s->rfs = rfs;
411 break;
413 case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
414 case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
415 if ((i2s->quirks & QUIRK_NO_MUXPSR)
416 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
417 clk_id = 0;
418 else
419 clk_id = 1;
421 if (!any_active(i2s)) {
422 if (i2s->op_clk) {
423 if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
424 (!clk_id && (mod & MOD_IMS_SYSMUX))) {
425 clk_disable(i2s->op_clk);
426 clk_put(i2s->op_clk);
427 } else {
428 i2s->rclk_srcrate =
429 clk_get_rate(i2s->op_clk);
430 return 0;
434 i2s->op_clk = clk_get(&i2s->pdev->dev,
435 i2s->src_clk[clk_id]);
436 clk_enable(i2s->op_clk);
437 i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
439 /* Over-ride the other's */
440 if (other) {
441 other->op_clk = i2s->op_clk;
442 other->rclk_srcrate = i2s->rclk_srcrate;
444 } else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
445 || (clk_id && !(mod & MOD_IMS_SYSMUX))) {
446 dev_err(&i2s->pdev->dev,
447 "%s:%d Other DAI busy\n", __func__, __LINE__);
448 return -EAGAIN;
449 } else {
450 /* Call can't be on the active DAI */
451 i2s->op_clk = other->op_clk;
452 i2s->rclk_srcrate = other->rclk_srcrate;
453 return 0;
456 if (clk_id == 0)
457 mod &= ~MOD_IMS_SYSMUX;
458 else
459 mod |= MOD_IMS_SYSMUX;
460 break;
462 default:
463 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
464 return -EINVAL;
467 writel(mod, i2s->addr + I2SMOD);
469 return 0;
472 static int i2s_set_fmt(struct snd_soc_dai *dai,
473 unsigned int fmt)
475 struct i2s_dai *i2s = to_info(dai);
476 u32 mod = readl(i2s->addr + I2SMOD);
477 u32 tmp = 0;
479 /* Format is priority */
480 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
481 case SND_SOC_DAIFMT_RIGHT_J:
482 tmp |= MOD_LR_RLOW;
483 tmp |= MOD_SDF_MSB;
484 break;
485 case SND_SOC_DAIFMT_LEFT_J:
486 tmp |= MOD_LR_RLOW;
487 tmp |= MOD_SDF_LSB;
488 break;
489 case SND_SOC_DAIFMT_I2S:
490 tmp |= MOD_SDF_IIS;
491 break;
492 default:
493 dev_err(&i2s->pdev->dev, "Format not supported\n");
494 return -EINVAL;
498 * INV flag is relative to the FORMAT flag - if set it simply
499 * flips the polarity specified by the Standard
501 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
502 case SND_SOC_DAIFMT_NB_NF:
503 break;
504 case SND_SOC_DAIFMT_NB_IF:
505 if (tmp & MOD_LR_RLOW)
506 tmp &= ~MOD_LR_RLOW;
507 else
508 tmp |= MOD_LR_RLOW;
509 break;
510 default:
511 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
512 return -EINVAL;
515 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
516 case SND_SOC_DAIFMT_CBM_CFM:
517 tmp |= MOD_SLAVE;
518 break;
519 case SND_SOC_DAIFMT_CBS_CFS:
520 /* Set default source clock in Master mode */
521 if (i2s->rclk_srcrate == 0)
522 i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
523 0, SND_SOC_CLOCK_IN);
524 break;
525 default:
526 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
527 return -EINVAL;
530 if (any_active(i2s) &&
531 ((mod & (MOD_SDF_MASK | MOD_LR_RLOW
532 | MOD_SLAVE)) != tmp)) {
533 dev_err(&i2s->pdev->dev,
534 "%s:%d Other DAI busy\n", __func__, __LINE__);
535 return -EAGAIN;
538 mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
539 mod |= tmp;
540 writel(mod, i2s->addr + I2SMOD);
542 return 0;
545 static int i2s_hw_params(struct snd_pcm_substream *substream,
546 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
548 struct i2s_dai *i2s = to_info(dai);
549 u32 mod = readl(i2s->addr + I2SMOD);
551 if (!is_secondary(i2s))
552 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
554 switch (params_channels(params)) {
555 case 6:
556 mod |= MOD_DC2_EN;
557 case 4:
558 mod |= MOD_DC1_EN;
559 break;
560 case 2:
561 break;
562 default:
563 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
564 params_channels(params));
565 return -EINVAL;
568 if (is_secondary(i2s))
569 mod &= ~MOD_BLCS_MASK;
570 else
571 mod &= ~MOD_BLCP_MASK;
573 if (is_manager(i2s))
574 mod &= ~MOD_BLC_MASK;
576 switch (params_format(params)) {
577 case SNDRV_PCM_FORMAT_S8:
578 if (is_secondary(i2s))
579 mod |= MOD_BLCS_8BIT;
580 else
581 mod |= MOD_BLCP_8BIT;
582 if (is_manager(i2s))
583 mod |= MOD_BLC_8BIT;
584 break;
585 case SNDRV_PCM_FORMAT_S16_LE:
586 if (is_secondary(i2s))
587 mod |= MOD_BLCS_16BIT;
588 else
589 mod |= MOD_BLCP_16BIT;
590 if (is_manager(i2s))
591 mod |= MOD_BLC_16BIT;
592 break;
593 case SNDRV_PCM_FORMAT_S24_LE:
594 if (is_secondary(i2s))
595 mod |= MOD_BLCS_24BIT;
596 else
597 mod |= MOD_BLCP_24BIT;
598 if (is_manager(i2s))
599 mod |= MOD_BLC_24BIT;
600 break;
601 default:
602 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
603 params_format(params));
604 return -EINVAL;
606 writel(mod, i2s->addr + I2SMOD);
608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
609 snd_soc_dai_set_dma_data(dai, substream,
610 (void *)&i2s->dma_playback);
611 else
612 snd_soc_dai_set_dma_data(dai, substream,
613 (void *)&i2s->dma_capture);
615 i2s->frmclk = params_rate(params);
617 return 0;
620 /* We set constraints on the substream acc to the version of I2S */
621 static int i2s_startup(struct snd_pcm_substream *substream,
622 struct snd_soc_dai *dai)
624 struct i2s_dai *i2s = to_info(dai);
625 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
626 unsigned long flags;
628 spin_lock_irqsave(&lock, flags);
630 i2s->mode |= DAI_OPENED;
632 if (is_manager(other))
633 i2s->mode &= ~DAI_MANAGER;
634 else
635 i2s->mode |= DAI_MANAGER;
637 /* Enforce set_sysclk in Master mode */
638 i2s->rclk_srcrate = 0;
640 spin_unlock_irqrestore(&lock, flags);
642 return 0;
645 static void i2s_shutdown(struct snd_pcm_substream *substream,
646 struct snd_soc_dai *dai)
648 struct i2s_dai *i2s = to_info(dai);
649 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
650 unsigned long flags;
652 spin_lock_irqsave(&lock, flags);
654 i2s->mode &= ~DAI_OPENED;
655 i2s->mode &= ~DAI_MANAGER;
657 if (is_opened(other))
658 other->mode |= DAI_MANAGER;
660 /* Reset any constraint on RFS and BFS */
661 i2s->rfs = 0;
662 i2s->bfs = 0;
664 spin_unlock_irqrestore(&lock, flags);
666 /* Gate CDCLK by default */
667 if (!is_opened(other))
668 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
669 0, SND_SOC_CLOCK_IN);
672 static int config_setup(struct i2s_dai *i2s)
674 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
675 unsigned rfs, bfs, blc;
676 u32 psr;
678 blc = get_blc(i2s);
680 bfs = i2s->bfs;
682 if (!bfs && other)
683 bfs = other->bfs;
685 /* Select least possible multiple(2) if no constraint set */
686 if (!bfs)
687 bfs = blc * 2;
689 rfs = i2s->rfs;
691 if (!rfs && other)
692 rfs = other->rfs;
694 if ((rfs == 256 || rfs == 512) && (blc == 24)) {
695 dev_err(&i2s->pdev->dev,
696 "%d-RFS not supported for 24-blc\n", rfs);
697 return -EINVAL;
700 if (!rfs) {
701 if (bfs == 16 || bfs == 32)
702 rfs = 256;
703 else
704 rfs = 384;
707 /* If already setup and running */
708 if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
709 dev_err(&i2s->pdev->dev,
710 "%s:%d Other DAI busy\n", __func__, __LINE__);
711 return -EAGAIN;
714 /* Don't bother RFS, BFS & PSR in Slave mode */
715 if (is_slave(i2s))
716 return 0;
718 set_bfs(i2s, bfs);
719 set_rfs(i2s, rfs);
721 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
722 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
723 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
724 dev_dbg(&i2s->pdev->dev,
725 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
726 i2s->rclk_srcrate, psr, rfs, bfs);
729 return 0;
732 static int i2s_trigger(struct snd_pcm_substream *substream,
733 int cmd, struct snd_soc_dai *dai)
735 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
736 struct snd_soc_pcm_runtime *rtd = substream->private_data;
737 struct i2s_dai *i2s = to_info(rtd->cpu_dai);
738 unsigned long flags;
740 switch (cmd) {
741 case SNDRV_PCM_TRIGGER_START:
742 case SNDRV_PCM_TRIGGER_RESUME:
743 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
744 local_irq_save(flags);
746 if (config_setup(i2s)) {
747 local_irq_restore(flags);
748 return -EINVAL;
751 if (capture)
752 i2s_rxctrl(i2s, 1);
753 else
754 i2s_txctrl(i2s, 1);
756 local_irq_restore(flags);
757 break;
758 case SNDRV_PCM_TRIGGER_STOP:
759 case SNDRV_PCM_TRIGGER_SUSPEND:
760 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
761 local_irq_save(flags);
763 if (capture)
764 i2s_rxctrl(i2s, 0);
765 else
766 i2s_txctrl(i2s, 0);
768 if (capture)
769 i2s_fifo(i2s, FIC_RXFLUSH);
770 else
771 i2s_fifo(i2s, FIC_TXFLUSH);
773 local_irq_restore(flags);
774 break;
777 return 0;
780 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
781 int div_id, int div)
783 struct i2s_dai *i2s = to_info(dai);
784 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
786 switch (div_id) {
787 case SAMSUNG_I2S_DIV_BCLK:
788 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
789 || (other && other->bfs && (other->bfs != div))) {
790 dev_err(&i2s->pdev->dev,
791 "%s:%d Other DAI busy\n", __func__, __LINE__);
792 return -EAGAIN;
794 i2s->bfs = div;
795 break;
796 default:
797 dev_err(&i2s->pdev->dev,
798 "Invalid clock divider(%d)\n", div_id);
799 return -EINVAL;
802 return 0;
805 static snd_pcm_sframes_t
806 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
808 struct i2s_dai *i2s = to_info(dai);
809 u32 reg = readl(i2s->addr + I2SFIC);
810 snd_pcm_sframes_t delay;
812 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
813 delay = FIC_RXCOUNT(reg);
814 else if (is_secondary(i2s))
815 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
816 else
817 delay = FIC_TXCOUNT(reg);
819 return delay;
822 #ifdef CONFIG_PM
823 static int i2s_suspend(struct snd_soc_dai *dai)
825 struct i2s_dai *i2s = to_info(dai);
827 if (dai->active) {
828 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
829 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
830 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
833 return 0;
836 static int i2s_resume(struct snd_soc_dai *dai)
838 struct i2s_dai *i2s = to_info(dai);
840 if (dai->active) {
841 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
842 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
843 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
846 return 0;
848 #else
849 #define i2s_suspend NULL
850 #define i2s_resume NULL
851 #endif
853 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
855 struct i2s_dai *i2s = to_info(dai);
856 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
858 if (other && other->clk) /* If this is probe on secondary */
859 goto probe_exit;
861 i2s->addr = ioremap(i2s->base, 0x100);
862 if (i2s->addr == NULL) {
863 dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
864 return -ENXIO;
867 i2s->clk = clk_get(&i2s->pdev->dev, "iis");
868 if (IS_ERR(i2s->clk)) {
869 dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
870 iounmap(i2s->addr);
871 return -ENOENT;
873 clk_enable(i2s->clk);
875 if (other) {
876 other->addr = i2s->addr;
877 other->clk = i2s->clk;
880 if (i2s->quirks & QUIRK_NEED_RSTCLR)
881 writel(CON_RSTCLR, i2s->addr + I2SCON);
883 if (i2s->quirks & QUIRK_SEC_DAI)
884 idma_reg_addr_init((void *)i2s->addr,
885 i2s->sec_dai->idma_playback.dma_addr);
887 probe_exit:
888 /* Reset any constraint on RFS and BFS */
889 i2s->rfs = 0;
890 i2s->bfs = 0;
891 i2s_txctrl(i2s, 0);
892 i2s_rxctrl(i2s, 0);
893 i2s_fifo(i2s, FIC_TXFLUSH);
894 i2s_fifo(other, FIC_TXFLUSH);
895 i2s_fifo(i2s, FIC_RXFLUSH);
897 /* Gate CDCLK by default */
898 if (!is_opened(other))
899 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
900 0, SND_SOC_CLOCK_IN);
902 return 0;
905 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
907 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
908 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
910 if (!other || !other->clk) {
912 if (i2s->quirks & QUIRK_NEED_RSTCLR)
913 writel(0, i2s->addr + I2SCON);
915 clk_disable(i2s->clk);
916 clk_put(i2s->clk);
918 iounmap(i2s->addr);
921 i2s->clk = NULL;
923 return 0;
926 static struct snd_soc_dai_ops samsung_i2s_dai_ops = {
927 .trigger = i2s_trigger,
928 .hw_params = i2s_hw_params,
929 .set_fmt = i2s_set_fmt,
930 .set_clkdiv = i2s_set_clkdiv,
931 .set_sysclk = i2s_set_sysclk,
932 .startup = i2s_startup,
933 .shutdown = i2s_shutdown,
934 .delay = i2s_delay,
937 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
939 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
940 SNDRV_PCM_FMTBIT_S16_LE | \
941 SNDRV_PCM_FMTBIT_S24_LE)
943 static __devinit
944 struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
946 struct i2s_dai *i2s;
948 i2s = kzalloc(sizeof(struct i2s_dai), GFP_KERNEL);
949 if (i2s == NULL)
950 return NULL;
952 i2s->pdev = pdev;
953 i2s->pri_dai = NULL;
954 i2s->sec_dai = NULL;
955 i2s->i2s_dai_drv.symmetric_rates = 1;
956 i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
957 i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
958 i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
959 i2s->i2s_dai_drv.suspend = i2s_suspend;
960 i2s->i2s_dai_drv.resume = i2s_resume;
961 i2s->i2s_dai_drv.playback.channels_min = 2;
962 i2s->i2s_dai_drv.playback.channels_max = 2;
963 i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
964 i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
966 if (!sec) {
967 i2s->i2s_dai_drv.capture.channels_min = 2;
968 i2s->i2s_dai_drv.capture.channels_max = 2;
969 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
970 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
971 } else { /* Create a new platform_device for Secondary */
972 i2s->pdev = platform_device_register_resndata(NULL,
973 pdev->name, pdev->id + SAMSUNG_I2S_SECOFF,
974 NULL, 0, NULL, 0);
975 if (IS_ERR(i2s->pdev)) {
976 kfree(i2s);
977 return NULL;
981 /* Pre-assign snd_soc_dai_set_drvdata */
982 dev_set_drvdata(&i2s->pdev->dev, i2s);
984 return i2s;
987 static __devinit int samsung_i2s_probe(struct platform_device *pdev)
989 u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan;
990 struct i2s_dai *pri_dai, *sec_dai = NULL;
991 struct s3c_audio_pdata *i2s_pdata;
992 struct samsung_i2s *i2s_cfg;
993 struct resource *res;
994 u32 regs_base, quirks;
995 int ret = 0;
997 /* Call during Seconday interface registration */
998 if (pdev->id >= SAMSUNG_I2S_SECOFF) {
999 sec_dai = dev_get_drvdata(&pdev->dev);
1000 snd_soc_register_dai(&sec_dai->pdev->dev,
1001 &sec_dai->i2s_dai_drv);
1002 return 0;
1005 i2s_pdata = pdev->dev.platform_data;
1006 if (i2s_pdata == NULL) {
1007 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1008 return -EINVAL;
1011 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1012 if (!res) {
1013 dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n");
1014 return -ENXIO;
1016 dma_pl_chan = res->start;
1018 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1019 if (!res) {
1020 dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n");
1021 return -ENXIO;
1023 dma_cp_chan = res->start;
1025 res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1026 if (res)
1027 dma_pl_sec_chan = res->start;
1028 else
1029 dma_pl_sec_chan = 0;
1031 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1032 if (!res) {
1033 dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1034 return -ENXIO;
1037 if (!request_mem_region(res->start, resource_size(res),
1038 "samsung-i2s")) {
1039 dev_err(&pdev->dev, "Unable to request SFR region\n");
1040 return -EBUSY;
1042 regs_base = res->start;
1044 i2s_cfg = &i2s_pdata->type.i2s;
1045 quirks = i2s_cfg->quirks;
1047 pri_dai = i2s_alloc_dai(pdev, false);
1048 if (!pri_dai) {
1049 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1050 ret = -ENOMEM;
1051 goto err1;
1054 pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1055 pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1056 pri_dai->dma_playback.client =
1057 (struct s3c2410_dma_client *)&pri_dai->dma_playback;
1058 pri_dai->dma_capture.client =
1059 (struct s3c2410_dma_client *)&pri_dai->dma_capture;
1060 pri_dai->dma_playback.channel = dma_pl_chan;
1061 pri_dai->dma_capture.channel = dma_cp_chan;
1062 pri_dai->src_clk = i2s_cfg->src_clk;
1063 pri_dai->dma_playback.dma_size = 4;
1064 pri_dai->dma_capture.dma_size = 4;
1065 pri_dai->base = regs_base;
1066 pri_dai->quirks = quirks;
1068 if (quirks & QUIRK_PRI_6CHAN)
1069 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1071 if (quirks & QUIRK_SEC_DAI) {
1072 sec_dai = i2s_alloc_dai(pdev, true);
1073 if (!sec_dai) {
1074 dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1075 ret = -ENOMEM;
1076 goto err2;
1078 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1079 sec_dai->dma_playback.client =
1080 (struct s3c2410_dma_client *)&sec_dai->dma_playback;
1081 /* Use iDMA always if SysDMA not provided */
1082 sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
1083 sec_dai->src_clk = i2s_cfg->src_clk;
1084 sec_dai->dma_playback.dma_size = 4;
1085 sec_dai->base = regs_base;
1086 sec_dai->quirks = quirks;
1087 sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
1088 sec_dai->pri_dai = pri_dai;
1089 pri_dai->sec_dai = sec_dai;
1092 if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1093 dev_err(&pdev->dev, "Unable to configure gpio\n");
1094 ret = -EINVAL;
1095 goto err3;
1098 snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
1100 return 0;
1101 err3:
1102 kfree(sec_dai);
1103 err2:
1104 kfree(pri_dai);
1105 err1:
1106 release_mem_region(regs_base, resource_size(res));
1108 return ret;
1111 static __devexit int samsung_i2s_remove(struct platform_device *pdev)
1113 struct i2s_dai *i2s, *other;
1115 i2s = dev_get_drvdata(&pdev->dev);
1116 other = i2s->pri_dai ? : i2s->sec_dai;
1118 if (other) {
1119 other->pri_dai = NULL;
1120 other->sec_dai = NULL;
1121 } else {
1122 struct resource *res;
1123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1124 if (res)
1125 release_mem_region(res->start, resource_size(res));
1128 i2s->pri_dai = NULL;
1129 i2s->sec_dai = NULL;
1131 kfree(i2s);
1133 snd_soc_unregister_dai(&pdev->dev);
1135 return 0;
1138 static struct platform_driver samsung_i2s_driver = {
1139 .probe = samsung_i2s_probe,
1140 .remove = __devexit_p(samsung_i2s_remove),
1141 .driver = {
1142 .name = "samsung-i2s",
1143 .owner = THIS_MODULE,
1147 static int __init samsung_i2s_init(void)
1149 return platform_driver_register(&samsung_i2s_driver);
1151 module_init(samsung_i2s_init);
1153 static void __exit samsung_i2s_exit(void)
1155 platform_driver_unregister(&samsung_i2s_driver);
1157 module_exit(samsung_i2s_exit);
1159 /* Module information */
1160 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
1161 MODULE_DESCRIPTION("Samsung I2S Interface");
1162 MODULE_ALIAS("platform:samsung-i2s");
1163 MODULE_LICENSE("GPL");