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