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