xref: /openbmc/linux/sound/soc/samsung/i2s.c (revision d0b73b48)
1 /* sound/soc/samsung/i2s.c
2  *
3  * ALSA SoC Audio Layer - Samsung I2S Controller driver
4  *
5  * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6  *	Jaswinder Singh <jassisinghbrar@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/clk.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 
20 #include <sound/soc.h>
21 #include <sound/pcm_params.h>
22 
23 #include <linux/platform_data/asoc-s3c.h>
24 
25 #include "dma.h"
26 #include "idma.h"
27 #include "i2s.h"
28 #include "i2s-regs.h"
29 
30 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
31 
32 struct 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 	/* 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;
64 	struct s3c_dma_params idma_playback;
65 	u32	quirks;
66 	u32	suspend_i2smod;
67 	u32	suspend_i2scon;
68 	u32	suspend_i2spsr;
69 };
70 
71 /* Lock for cross i/f checks */
72 static DEFINE_SPINLOCK(lock);
73 
74 /* If this is the 'overlay' stereo DAI */
75 static 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 */
81 static 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 */
87 static inline bool tx_active(struct i2s_dai *i2s)
88 {
89 	u32 active;
90 
91 	if (!i2s)
92 		return false;
93 
94 	active = readl(i2s->addr + I2SCON);
95 
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 */
105 static 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 */
113 static 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 */
119 static inline bool rx_active(struct i2s_dai *i2s)
120 {
121 	u32 active;
122 
123 	if (!i2s)
124 		return false;
125 
126 	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
127 
128 	return active ? true : false;
129 }
130 
131 /* If the other interface of the controller is receiving data */
132 static 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 */
140 static 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 */
146 static 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 */
152 static 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 */
158 static inline bool any_active(struct i2s_dai *i2s)
159 {
160 	return this_active(i2s) || other_active(i2s);
161 }
162 
163 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
164 {
165 	return snd_soc_dai_get_drvdata(dai);
166 }
167 
168 static 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 
176 static 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) */
185 static 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) */
198 static 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) */
223 static 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) */
236 static 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 */
264 static 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 */
278 static 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 */
327 static 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 */
356 static 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 
380 static 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))) {
424 					clk_disable_unprepare(i2s->op_clk);
425 					clk_put(i2s->op_clk);
426 				} else {
427 					i2s->rclk_srcrate =
428 						clk_get_rate(i2s->op_clk);
429 					return 0;
430 				}
431 			}
432 
433 			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");
439 			clk_prepare_enable(i2s->op_clk);
440 			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 
475 static 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 
548 static 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:
564 		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 
575 		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 */
635 static 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 
659 static 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 
686 static 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 
746 static 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 
760 		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 
777 		if (capture) {
778 			i2s_rxctrl(i2s, 0);
779 			i2s_fifo(i2s, FIC_RXFLUSH);
780 		} else {
781 			i2s_txctrl(i2s, 0);
782 			i2s_fifo(i2s, FIC_TXFLUSH);
783 		}
784 
785 		local_irq_restore(flags);
786 		break;
787 	}
788 
789 	return 0;
790 }
791 
792 static 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 
817 static snd_pcm_sframes_t
818 i2s_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
835 static 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 
848 static 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 
865 static 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 	}
885 	clk_prepare_enable(i2s->clk);
886 
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 
895 	if (i2s->quirks & QUIRK_SEC_DAI)
896 		idma_reg_addr_init(i2s->addr,
897 					i2s->sec_dai->idma_playback.dma_addr);
898 
899 probe_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 
917 static 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 
927 		clk_disable_unprepare(i2s->clk);
928 		clk_put(i2s->clk);
929 
930 		iounmap(i2s->addr);
931 	}
932 
933 	i2s->clk = NULL;
934 
935 	return 0;
936 }
937 
938 static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
939 	.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 
955 static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
956 {
957 	struct i2s_dai *i2s;
958 
959 	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
960 	if (i2s == NULL)
961 		return NULL;
962 
963 	i2s->pdev = pdev;
964 	i2s->pri_dai = NULL;
965 	i2s->sec_dai = NULL;
966 	i2s->i2s_dai_drv.symmetric_rates = 1;
967 	i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
968 	i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
969 	i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
970 	i2s->i2s_dai_drv.suspend = i2s_suspend;
971 	i2s->i2s_dai_drv.resume = i2s_resume;
972 	i2s->i2s_dai_drv.playback.channels_min = 2;
973 	i2s->i2s_dai_drv.playback.channels_max = 2;
974 	i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
975 	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
976 
977 	if (!sec) {
978 		i2s->i2s_dai_drv.capture.channels_min = 1;
979 		i2s->i2s_dai_drv.capture.channels_max = 2;
980 		i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
981 		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
982 	} else {	/* Create a new platform_device for Secondary */
983 		i2s->pdev = platform_device_register_resndata(NULL,
984 				pdev->name, pdev->id + SAMSUNG_I2S_SECOFF,
985 				NULL, 0, NULL, 0);
986 		if (IS_ERR(i2s->pdev))
987 			return NULL;
988 	}
989 
990 	/* Pre-assign snd_soc_dai_set_drvdata */
991 	dev_set_drvdata(&i2s->pdev->dev, i2s);
992 
993 	return i2s;
994 }
995 
996 static int samsung_i2s_probe(struct platform_device *pdev)
997 {
998 	u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan;
999 	struct i2s_dai *pri_dai, *sec_dai = NULL;
1000 	struct s3c_audio_pdata *i2s_pdata;
1001 	struct samsung_i2s *i2s_cfg;
1002 	struct resource *res;
1003 	u32 regs_base, quirks;
1004 	int ret = 0;
1005 
1006 	/* Call during Seconday interface registration */
1007 	if (pdev->id >= SAMSUNG_I2S_SECOFF) {
1008 		sec_dai = dev_get_drvdata(&pdev->dev);
1009 		snd_soc_register_dai(&sec_dai->pdev->dev,
1010 			&sec_dai->i2s_dai_drv);
1011 		asoc_dma_platform_register(&pdev->dev);
1012 		return 0;
1013 	}
1014 
1015 	i2s_pdata = pdev->dev.platform_data;
1016 	if (i2s_pdata == NULL) {
1017 		dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1018 		return -EINVAL;
1019 	}
1020 
1021 	res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1022 	if (!res) {
1023 		dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n");
1024 		return -ENXIO;
1025 	}
1026 	dma_pl_chan = res->start;
1027 
1028 	res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1029 	if (!res) {
1030 		dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n");
1031 		return -ENXIO;
1032 	}
1033 	dma_cp_chan = res->start;
1034 
1035 	res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1036 	if (res)
1037 		dma_pl_sec_chan = res->start;
1038 	else
1039 		dma_pl_sec_chan = 0;
1040 
1041 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1042 	if (!res) {
1043 		dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1044 		return -ENXIO;
1045 	}
1046 
1047 	if (!request_mem_region(res->start, resource_size(res),
1048 							"samsung-i2s")) {
1049 		dev_err(&pdev->dev, "Unable to request SFR region\n");
1050 		return -EBUSY;
1051 	}
1052 	regs_base = res->start;
1053 
1054 	i2s_cfg = &i2s_pdata->type.i2s;
1055 	quirks = i2s_cfg->quirks;
1056 
1057 	pri_dai = i2s_alloc_dai(pdev, false);
1058 	if (!pri_dai) {
1059 		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1060 		ret = -ENOMEM;
1061 		goto err;
1062 	}
1063 
1064 	pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1065 	pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1066 	pri_dai->dma_playback.client =
1067 		(struct s3c2410_dma_client *)&pri_dai->dma_playback;
1068 	pri_dai->dma_capture.client =
1069 		(struct s3c2410_dma_client *)&pri_dai->dma_capture;
1070 	pri_dai->dma_playback.channel = dma_pl_chan;
1071 	pri_dai->dma_capture.channel = dma_cp_chan;
1072 	pri_dai->dma_playback.dma_size = 4;
1073 	pri_dai->dma_capture.dma_size = 4;
1074 	pri_dai->base = regs_base;
1075 	pri_dai->quirks = quirks;
1076 
1077 	if (quirks & QUIRK_PRI_6CHAN)
1078 		pri_dai->i2s_dai_drv.playback.channels_max = 6;
1079 
1080 	if (quirks & QUIRK_SEC_DAI) {
1081 		sec_dai = i2s_alloc_dai(pdev, true);
1082 		if (!sec_dai) {
1083 			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1084 			ret = -ENOMEM;
1085 			goto err;
1086 		}
1087 		sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1088 		sec_dai->dma_playback.client =
1089 			(struct s3c2410_dma_client *)&sec_dai->dma_playback;
1090 		/* Use iDMA always if SysDMA not provided */
1091 		sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
1092 		sec_dai->dma_playback.dma_size = 4;
1093 		sec_dai->base = regs_base;
1094 		sec_dai->quirks = quirks;
1095 		sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
1096 		sec_dai->pri_dai = pri_dai;
1097 		pri_dai->sec_dai = sec_dai;
1098 	}
1099 
1100 	if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1101 		dev_err(&pdev->dev, "Unable to configure gpio\n");
1102 		ret = -EINVAL;
1103 		goto err;
1104 	}
1105 
1106 	snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
1107 
1108 	pm_runtime_enable(&pdev->dev);
1109 
1110 	asoc_dma_platform_register(&pdev->dev);
1111 
1112 	return 0;
1113 err:
1114 	release_mem_region(regs_base, resource_size(res));
1115 
1116 	return ret;
1117 }
1118 
1119 static int samsung_i2s_remove(struct platform_device *pdev)
1120 {
1121 	struct i2s_dai *i2s, *other;
1122 	struct resource *res;
1123 
1124 	i2s = dev_get_drvdata(&pdev->dev);
1125 	other = i2s->pri_dai ? : i2s->sec_dai;
1126 
1127 	if (other) {
1128 		other->pri_dai = NULL;
1129 		other->sec_dai = NULL;
1130 	} else {
1131 		pm_runtime_disable(&pdev->dev);
1132 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1133 		if (res)
1134 			release_mem_region(res->start, resource_size(res));
1135 	}
1136 
1137 	i2s->pri_dai = NULL;
1138 	i2s->sec_dai = NULL;
1139 
1140 	asoc_dma_platform_unregister(&pdev->dev);
1141 	snd_soc_unregister_dai(&pdev->dev);
1142 
1143 	return 0;
1144 }
1145 
1146 static struct platform_driver samsung_i2s_driver = {
1147 	.probe  = samsung_i2s_probe,
1148 	.remove = samsung_i2s_remove,
1149 	.driver = {
1150 		.name = "samsung-i2s",
1151 		.owner = THIS_MODULE,
1152 	},
1153 };
1154 
1155 module_platform_driver(samsung_i2s_driver);
1156 
1157 /* Module information */
1158 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1159 MODULE_DESCRIPTION("Samsung I2S Interface");
1160 MODULE_ALIAS("platform:samsung-i2s");
1161 MODULE_LICENSE("GPL");
1162