blob: bff42bf370b9c8849874db4792150fad27328010 [file] [log] [blame]
Jassi Brar5033f432010-11-22 15:37:25 +09001/* sound/soc/samsung/i2s.c
Jassi Brar1c7ac012010-11-22 15:36:59 +09002 *
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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040017#include <linux/module.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090018
Jassi Brar1c7ac012010-11-22 15:36:59 +090019#include <sound/soc.h>
Seungwhan Youn0378b6a2011-01-11 07:26:06 +090020#include <sound/pcm_params.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090021
22#include <plat/audio.h>
23
24#include "dma.h"
Sangbeom Kim61100f42011-07-20 17:07:12 +090025#include "idma.h"
Jassi Brar1c7ac012010-11-22 15:36:59 +090026#include "i2s.h"
Sangbeom Kim172a4532011-06-20 16:36:18 +090027#include "i2s-regs.h"
Jassi Brar1c7ac012010-11-22 15:36:59 +090028
29#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
30
31struct 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;
42 /*
43 * Specifically requested RCLK,BCLK by MACHINE Driver.
44 * 0 indicates CPU driver is free to choose any value.
45 */
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;
Sangbeom Kim61100f42011-07-20 17:07:12 +090065 struct s3c_dma_params idma_playback;
Jassi Brar1c7ac012010-11-22 15:36:59 +090066 u32 quirks;
67 u32 suspend_i2smod;
68 u32 suspend_i2scon;
69 u32 suspend_i2spsr;
70};
71
72/* Lock for cross i/f checks */
73static DEFINE_SPINLOCK(lock);
74
75/* If this is the 'overlay' stereo DAI */
76static inline bool is_secondary(struct i2s_dai *i2s)
77{
78 return i2s->pri_dai ? true : false;
79}
80
81/* If operating in SoC-Slave mode */
82static inline bool is_slave(struct i2s_dai *i2s)
83{
84 return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
85}
86
87/* If this interface of the controller is transmitting data */
88static inline bool tx_active(struct i2s_dai *i2s)
89{
90 u32 active;
91
92 if (!i2s)
93 return false;
94
Sangbeom Kim33195502011-06-10 10:36:54 +090095 active = readl(i2s->addr + I2SCON);
Jassi Brar1c7ac012010-11-22 15:36:59 +090096
97 if (is_secondary(i2s))
98 active &= CON_TXSDMA_ACTIVE;
99 else
100 active &= CON_TXDMA_ACTIVE;
101
102 return active ? true : false;
103}
104
105/* If the other interface of the controller is transmitting data */
106static inline bool other_tx_active(struct i2s_dai *i2s)
107{
108 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
109
110 return tx_active(other);
111}
112
113/* If any interface of the controller is transmitting data */
114static inline bool any_tx_active(struct i2s_dai *i2s)
115{
116 return tx_active(i2s) || other_tx_active(i2s);
117}
118
119/* If this interface of the controller is receiving data */
120static inline bool rx_active(struct i2s_dai *i2s)
121{
122 u32 active;
123
124 if (!i2s)
125 return false;
126
Sangbeom Kim33195502011-06-10 10:36:54 +0900127 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900128
129 return active ? true : false;
130}
131
132/* If the other interface of the controller is receiving data */
133static inline bool other_rx_active(struct i2s_dai *i2s)
134{
135 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
136
137 return rx_active(other);
138}
139
140/* If any interface of the controller is receiving data */
141static inline bool any_rx_active(struct i2s_dai *i2s)
142{
143 return rx_active(i2s) || other_rx_active(i2s);
144}
145
146/* If the other DAI is transmitting or receiving data */
147static inline bool other_active(struct i2s_dai *i2s)
148{
149 return other_rx_active(i2s) || other_tx_active(i2s);
150}
151
152/* If this DAI is transmitting or receiving data */
153static inline bool this_active(struct i2s_dai *i2s)
154{
155 return tx_active(i2s) || rx_active(i2s);
156}
157
158/* If the controller is active anyway */
159static inline bool any_active(struct i2s_dai *i2s)
160{
161 return this_active(i2s) || other_active(i2s);
162}
163
164static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
165{
166 return snd_soc_dai_get_drvdata(dai);
167}
168
169static inline bool is_opened(struct i2s_dai *i2s)
170{
171 if (i2s && (i2s->mode & DAI_OPENED))
172 return true;
173 else
174 return false;
175}
176
177static inline bool is_manager(struct i2s_dai *i2s)
178{
179 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
180 return true;
181 else
182 return false;
183}
184
185/* Read RCLK of I2S (in multiples of LRCLK) */
186static inline unsigned get_rfs(struct i2s_dai *i2s)
187{
188 u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3;
189
190 switch (rfs) {
191 case 3: return 768;
192 case 2: return 384;
193 case 1: return 512;
194 default: return 256;
195 }
196}
197
198/* Write RCLK of I2S (in multiples of LRCLK) */
199static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
200{
201 u32 mod = readl(i2s->addr + I2SMOD);
202
203 mod &= ~MOD_RCLK_MASK;
204
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;
218 }
219
220 writel(mod, i2s->addr + I2SMOD);
221}
222
223/* Read Bit-Clock of I2S (in multiples of LRCLK) */
224static inline unsigned get_bfs(struct i2s_dai *i2s)
225{
226 u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3;
227
228 switch (bfs) {
229 case 3: return 24;
230 case 2: return 16;
231 case 1: return 48;
232 default: return 32;
233 }
234}
235
236/* Write Bit-Clock of I2S (in multiples of LRCLK) */
237static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
238{
239 u32 mod = readl(i2s->addr + I2SMOD);
240
241 mod &= ~MOD_BCLK_MASK;
242
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;
259 }
260
261 writel(mod, i2s->addr + I2SMOD);
262}
263
264/* Sample-Size */
265static inline int get_blc(struct i2s_dai *i2s)
266{
267 int blc = readl(i2s->addr + I2SMOD);
268
269 blc = (blc >> 13) & 0x3;
270
271 switch (blc) {
272 case 2: return 24;
273 case 1: return 8;
274 default: return 16;
275 }
276}
277
278/* TX Channel Control */
279static void i2s_txctrl(struct i2s_dai *i2s, int on)
280{
281 void __iomem *addr = i2s->addr;
282 u32 con = readl(addr + I2SCON);
283 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
284
285 if (on) {
286 con |= CON_ACTIVE;
287 con &= ~CON_TXCH_PAUSE;
288
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;
295 }
296
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;
308 }
309
310 if (other_tx_active(i2s)) {
311 writel(con, addr + I2SCON);
312 return;
313 }
314
315 con |= CON_TXCH_PAUSE;
316
317 if (any_rx_active(i2s))
318 mod |= MOD_RXONLY;
319 else
320 con &= ~CON_ACTIVE;
321 }
322
323 writel(mod, addr + I2SMOD);
324 writel(con, addr + I2SCON);
325}
326
327/* RX Channel Control */
328static void i2s_rxctrl(struct i2s_dai *i2s, int on)
329{
330 void __iomem *addr = i2s->addr;
331 u32 con = readl(addr + I2SCON);
332 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
333
334 if (on) {
335 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
336 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
337
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;
345
346 if (any_tx_active(i2s))
347 mod |= MOD_TXONLY;
348 else
349 con &= ~CON_ACTIVE;
350 }
351
352 writel(mod, addr + I2SMOD);
353 writel(con, addr + I2SCON);
354}
355
356/* Flush FIFO of an interface */
357static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
358{
359 void __iomem *fic;
360 u32 val;
361
362 if (!i2s)
363 return;
364
365 if (is_secondary(i2s))
366 fic = i2s->addr + I2SFICS;
367 else
368 fic = i2s->addr + I2SFIC;
369
370 /* Flush the FIFO */
371 writel(readl(fic) | flush, fic);
372
373 /* Be patient */
374 val = msecs_to_loops(1) / 1000; /* 1 usec */
375 while (--val)
376 cpu_relax();
377
378 writel(readl(fic) & ~flush, fic);
379}
380
381static int i2s_set_sysclk(struct snd_soc_dai *dai,
382 int clk_id, unsigned int rfs, int dir)
383{
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);
387
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;
393
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;
403 }
404
405 if (dir == SND_SOC_CLOCK_IN)
406 mod |= MOD_CDCLKCON;
407 else
408 mod &= ~MOD_CDCLKCON;
409
410 i2s->rfs = rfs;
411 break;
412
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;
420
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 {
Jassi Brar6ce534a2010-12-20 11:05:46 +0900428 i2s->rclk_srcrate =
429 clk_get_rate(i2s->op_clk);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900430 return 0;
431 }
432 }
433
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);
438
439 /* Over-ride the other's */
440 if (other) {
441 other->op_clk = i2s->op_clk;
442 other->rclk_srcrate = i2s->rclk_srcrate;
443 }
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;
454 }
455
456 if (clk_id == 0)
457 mod &= ~MOD_IMS_SYSMUX;
458 else
459 mod |= MOD_IMS_SYSMUX;
460 break;
461
462 default:
463 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
464 return -EINVAL;
465 }
466
467 writel(mod, i2s->addr + I2SMOD);
468
469 return 0;
470}
471
472static int i2s_set_fmt(struct snd_soc_dai *dai,
473 unsigned int fmt)
474{
475 struct i2s_dai *i2s = to_info(dai);
476 u32 mod = readl(i2s->addr + I2SMOD);
477 u32 tmp = 0;
478
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;
495 }
496
497 /*
498 * INV flag is relative to the FORMAT flag - if set it simply
499 * flips the polarity specified by the Standard
500 */
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;
513 }
514
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;
528 }
529
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;
536 }
537
538 mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
539 mod |= tmp;
540 writel(mod, i2s->addr + I2SMOD);
541
542 return 0;
543}
544
545static int i2s_hw_params(struct snd_pcm_substream *substream,
546 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
547{
548 struct i2s_dai *i2s = to_info(dai);
549 u32 mod = readl(i2s->addr + I2SMOD);
550
551 if (!is_secondary(i2s))
552 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
553
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;
566 }
567
568 if (is_secondary(i2s))
569 mod &= ~MOD_BLCS_MASK;
570 else
571 mod &= ~MOD_BLCP_MASK;
572
573 if (is_manager(i2s))
574 mod &= ~MOD_BLC_MASK;
575
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;
605 }
606 writel(mod, i2s->addr + I2SMOD);
607
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);
614
615 i2s->frmclk = params_rate(params);
616
617 return 0;
618}
619
620/* We set constraints on the substream acc to the version of I2S */
621static int i2s_startup(struct snd_pcm_substream *substream,
622 struct snd_soc_dai *dai)
623{
624 struct i2s_dai *i2s = to_info(dai);
625 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
626 unsigned long flags;
627
628 spin_lock_irqsave(&lock, flags);
629
630 i2s->mode |= DAI_OPENED;
631
632 if (is_manager(other))
633 i2s->mode &= ~DAI_MANAGER;
634 else
635 i2s->mode |= DAI_MANAGER;
636
637 /* Enforce set_sysclk in Master mode */
638 i2s->rclk_srcrate = 0;
639
640 spin_unlock_irqrestore(&lock, flags);
641
642 return 0;
643}
644
645static void i2s_shutdown(struct snd_pcm_substream *substream,
646 struct snd_soc_dai *dai)
647{
648 struct i2s_dai *i2s = to_info(dai);
649 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
650 unsigned long flags;
651
652 spin_lock_irqsave(&lock, flags);
653
654 i2s->mode &= ~DAI_OPENED;
655 i2s->mode &= ~DAI_MANAGER;
656
657 if (is_opened(other))
658 other->mode |= DAI_MANAGER;
659
660 /* Reset any constraint on RFS and BFS */
661 i2s->rfs = 0;
662 i2s->bfs = 0;
663
664 spin_unlock_irqrestore(&lock, flags);
665
666 /* Gate CDCLK by default */
667 if (!is_opened(other))
668 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
669 0, SND_SOC_CLOCK_IN);
670}
671
672static int config_setup(struct i2s_dai *i2s)
673{
674 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
675 unsigned rfs, bfs, blc;
676 u32 psr;
677
678 blc = get_blc(i2s);
679
680 bfs = i2s->bfs;
681
682 if (!bfs && other)
683 bfs = other->bfs;
684
685 /* Select least possible multiple(2) if no constraint set */
686 if (!bfs)
687 bfs = blc * 2;
688
689 rfs = i2s->rfs;
690
691 if (!rfs && other)
692 rfs = other->rfs;
693
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;
698 }
699
700 if (!rfs) {
701 if (bfs == 16 || bfs == 32)
702 rfs = 256;
703 else
704 rfs = 384;
705 }
706
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;
712 }
713
714 /* Don't bother RFS, BFS & PSR in Slave mode */
715 if (is_slave(i2s))
716 return 0;
717
718 set_bfs(i2s, bfs);
719 set_rfs(i2s, rfs);
720
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);
727 }
728
729 return 0;
730}
731
732static int i2s_trigger(struct snd_pcm_substream *substream,
733 int cmd, struct snd_soc_dai *dai)
734{
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;
739
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);
745
Jassi Brar1c7ac012010-11-22 15:36:59 +0900746 if (config_setup(i2s)) {
747 local_irq_restore(flags);
748 return -EINVAL;
749 }
750
751 if (capture)
752 i2s_rxctrl(i2s, 1);
753 else
754 i2s_txctrl(i2s, 1);
755
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);
762
763 if (capture)
764 i2s_rxctrl(i2s, 0);
765 else
766 i2s_txctrl(i2s, 0);
767
Jassi Brar775bc972010-12-20 11:05:47 +0900768 if (capture)
769 i2s_fifo(i2s, FIC_RXFLUSH);
770 else
771 i2s_fifo(i2s, FIC_TXFLUSH);
772
Jassi Brar1c7ac012010-11-22 15:36:59 +0900773 local_irq_restore(flags);
774 break;
775 }
776
777 return 0;
778}
779
780static int i2s_set_clkdiv(struct snd_soc_dai *dai,
781 int div_id, int div)
782{
783 struct i2s_dai *i2s = to_info(dai);
784 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
785
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;
793 }
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;
800 }
801
802 return 0;
803}
804
805static snd_pcm_sframes_t
806i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
807{
808 struct i2s_dai *i2s = to_info(dai);
809 u32 reg = readl(i2s->addr + I2SFIC);
810 snd_pcm_sframes_t delay;
811
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);
818
819 return delay;
820}
821
822#ifdef CONFIG_PM
823static int i2s_suspend(struct snd_soc_dai *dai)
824{
825 struct i2s_dai *i2s = to_info(dai);
826
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);
831 }
832
833 return 0;
834}
835
836static int i2s_resume(struct snd_soc_dai *dai)
837{
838 struct i2s_dai *i2s = to_info(dai);
839
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);
844 }
845
846 return 0;
847}
848#else
849#define i2s_suspend NULL
850#define i2s_resume NULL
851#endif
852
853static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
854{
855 struct i2s_dai *i2s = to_info(dai);
856 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
857
858 if (other && other->clk) /* If this is probe on secondary */
859 goto probe_exit;
860
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;
865 }
866
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;
872 }
873 clk_enable(i2s->clk);
874
875 if (other) {
876 other->addr = i2s->addr;
877 other->clk = i2s->clk;
878 }
879
880 if (i2s->quirks & QUIRK_NEED_RSTCLR)
881 writel(CON_RSTCLR, i2s->addr + I2SCON);
882
Sangbeom Kim61100f42011-07-20 17:07:12 +0900883 if (i2s->quirks & QUIRK_SEC_DAI)
884 idma_reg_addr_init((void *)i2s->addr,
885 i2s->sec_dai->idma_playback.dma_addr);
886
Jassi Brar1c7ac012010-11-22 15:36:59 +0900887probe_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);
896
897 /* Gate CDCLK by default */
898 if (!is_opened(other))
899 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
900 0, SND_SOC_CLOCK_IN);
901
902 return 0;
903}
904
905static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
906{
907 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
908 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
909
910 if (!other || !other->clk) {
911
912 if (i2s->quirks & QUIRK_NEED_RSTCLR)
913 writel(0, i2s->addr + I2SCON);
914
915 clk_disable(i2s->clk);
916 clk_put(i2s->clk);
917
918 iounmap(i2s->addr);
919 }
920
921 i2s->clk = NULL;
922
923 return 0;
924}
925
926static 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,
935};
936
937#define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
938
939#define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
940 SNDRV_PCM_FMTBIT_S16_LE | \
941 SNDRV_PCM_FMTBIT_S24_LE)
942
943static __devinit
944struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
945{
946 struct i2s_dai *i2s;
947
948 i2s = kzalloc(sizeof(struct i2s_dai), GFP_KERNEL);
949 if (i2s == NULL)
950 return NULL;
951
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;
965
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;
978 }
979 }
980
981 /* Pre-assign snd_soc_dai_set_drvdata */
982 dev_set_drvdata(&i2s->pdev->dev, i2s);
983
984 return i2s;
985}
986
987static __devinit int samsung_i2s_probe(struct platform_device *pdev)
988{
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;
996
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;
1003 }
1004
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;
1009 }
1010
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;
1015 }
1016 dma_pl_chan = res->start;
1017
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;
1022 }
1023 dma_cp_chan = res->start;
1024
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;
1030
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;
1035 }
1036
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;
1041 }
1042 regs_base = res->start;
1043
1044 i2s_cfg = &i2s_pdata->type.i2s;
1045 quirks = i2s_cfg->quirks;
1046
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;
1052 }
1053
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;
1067
1068 if (quirks & QUIRK_PRI_6CHAN)
1069 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1070
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;
1077 }
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;
Sangbeom Kim61100f42011-07-20 17:07:12 +09001087 sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001088 sec_dai->pri_dai = pri_dai;
1089 pri_dai->sec_dai = sec_dai;
1090 }
1091
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;
1096 }
1097
1098 snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
1099
1100 return 0;
1101err3:
1102 kfree(sec_dai);
1103err2:
1104 kfree(pri_dai);
1105err1:
1106 release_mem_region(regs_base, resource_size(res));
1107
1108 return ret;
1109}
1110
1111static __devexit int samsung_i2s_remove(struct platform_device *pdev)
1112{
1113 struct i2s_dai *i2s, *other;
1114
1115 i2s = dev_get_drvdata(&pdev->dev);
1116 other = i2s->pri_dai ? : i2s->sec_dai;
1117
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));
1126 }
1127
1128 i2s->pri_dai = NULL;
1129 i2s->sec_dai = NULL;
1130
1131 kfree(i2s);
1132
1133 snd_soc_unregister_dai(&pdev->dev);
1134
1135 return 0;
1136}
1137
1138static struct platform_driver samsung_i2s_driver = {
1139 .probe = samsung_i2s_probe,
Axel Linc4c58392011-10-02 11:20:13 +08001140 .remove = __devexit_p(samsung_i2s_remove),
Jassi Brar1c7ac012010-11-22 15:36:59 +09001141 .driver = {
1142 .name = "samsung-i2s",
1143 .owner = THIS_MODULE,
1144 },
1145};
1146
1147static int __init samsung_i2s_init(void)
1148{
1149 return platform_driver_register(&samsung_i2s_driver);
1150}
1151module_init(samsung_i2s_init);
1152
1153static void __exit samsung_i2s_exit(void)
1154{
1155 platform_driver_unregister(&samsung_i2s_driver);
1156}
1157module_exit(samsung_i2s_exit);
1158
1159/* Module information */
1160MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
1161MODULE_DESCRIPTION("Samsung I2S Interface");
1162MODULE_ALIAS("platform:samsung-i2s");
1163MODULE_LICENSE("GPL");