xref: /openbmc/linux/sound/soc/samsung/i2s.c (revision 47ebd031)
1 // SPDX-License-Identifier: GPL-2.0
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 #include <dt-bindings/sound/samsung-i2s.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.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 #define SAMSUNG_I2S_ID_PRIMARY		1
33 #define SAMSUNG_I2S_ID_SECONDARY	2
34 
35 struct samsung_i2s_variant_regs {
36 	unsigned int	bfs_off;
37 	unsigned int	rfs_off;
38 	unsigned int	sdf_off;
39 	unsigned int	txr_off;
40 	unsigned int	rclksrc_off;
41 	unsigned int	mss_off;
42 	unsigned int	cdclkcon_off;
43 	unsigned int	lrp_off;
44 	unsigned int	bfs_mask;
45 	unsigned int	rfs_mask;
46 	unsigned int	ftx0cnt_off;
47 };
48 
49 struct samsung_i2s_dai_data {
50 	u32 quirks;
51 	unsigned int pcm_rates;
52 	const struct samsung_i2s_variant_regs *i2s_variant_regs;
53 	void (*fixup_early)(struct snd_pcm_substream *substream,
54 					struct snd_soc_dai *dai);
55 	void (*fixup_late)(struct snd_pcm_substream *substream,
56 					struct snd_soc_dai *dai);
57 };
58 
59 struct i2s_dai {
60 	/* Platform device for this DAI */
61 	struct platform_device *pdev;
62 
63 	/* Frame clock */
64 	unsigned frmclk;
65 	/*
66 	 * Specifically requested RCLK, BCLK by machine driver.
67 	 * 0 indicates CPU driver is free to choose any value.
68 	 */
69 	unsigned rfs, bfs;
70 	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
71 	struct i2s_dai *pri_dai;
72 	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
73 	struct i2s_dai *sec_dai;
74 
75 #define DAI_OPENED	(1 << 0) /* DAI is opened */
76 #define DAI_MANAGER	(1 << 1) /* DAI is the manager */
77 	unsigned mode;
78 
79 	/* Driver for this DAI */
80 	struct snd_soc_dai_driver *drv;
81 
82 	/* DMA parameters */
83 	struct snd_dmaengine_dai_dma_data dma_playback;
84 	struct snd_dmaengine_dai_dma_data dma_capture;
85 	struct snd_dmaengine_dai_dma_data idma_playback;
86 	dma_filter_fn filter;
87 
88 	struct samsung_i2s_priv *priv;
89 };
90 
91 struct samsung_i2s_priv {
92 	struct platform_device *pdev;
93 	struct platform_device *pdev_sec;
94 
95 	/* Lock for cross interface checks */
96 	spinlock_t pcm_lock;
97 
98 	/* CPU DAIs and their corresponding drivers */
99 	struct i2s_dai *dai;
100 	struct snd_soc_dai_driver *dai_drv;
101 	int num_dais;
102 
103 	/* The I2S controller's core clock */
104 	struct clk *clk;
105 
106 	/* Clock for generating I2S signals */
107 	struct clk *op_clk;
108 
109 	/* Rate of RCLK source clock */
110 	unsigned long rclk_srcrate;
111 
112 	/* Cache of selected I2S registers for system suspend */
113 	u32 suspend_i2smod;
114 	u32 suspend_i2scon;
115 	u32 suspend_i2spsr;
116 
117 	const struct samsung_i2s_variant_regs *variant_regs;
118 	void (*fixup_early)(struct snd_pcm_substream *substream,
119 						struct snd_soc_dai *dai);
120 	void (*fixup_late)(struct snd_pcm_substream *substream,
121 						struct snd_soc_dai *dai);
122 	u32 quirks;
123 
124 	/* The clock provider's data */
125 	struct clk *clk_table[3];
126 	struct clk_onecell_data clk_data;
127 
128 	/* Spinlock protecting member fields below */
129 	spinlock_t lock;
130 
131 	/* Memory mapped SFR region */
132 	void __iomem *addr;
133 
134 	/* A flag indicating the I2S slave mode operation */
135 	bool slave_mode;
136 };
137 
138 /* Returns true if this is the 'overlay' stereo DAI */
139 static inline bool is_secondary(struct i2s_dai *i2s)
140 {
141 	return i2s->drv->id == SAMSUNG_I2S_ID_SECONDARY;
142 }
143 
144 /* If this interface of the controller is transmitting data */
145 static inline bool tx_active(struct i2s_dai *i2s)
146 {
147 	u32 active;
148 
149 	if (!i2s)
150 		return false;
151 
152 	active = readl(i2s->priv->addr + I2SCON);
153 
154 	if (is_secondary(i2s))
155 		active &= CON_TXSDMA_ACTIVE;
156 	else
157 		active &= CON_TXDMA_ACTIVE;
158 
159 	return active ? true : false;
160 }
161 
162 /* Return pointer to the other DAI */
163 static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
164 {
165 	return i2s->pri_dai ? : i2s->sec_dai;
166 }
167 
168 /* If the other interface of the controller is transmitting data */
169 static inline bool other_tx_active(struct i2s_dai *i2s)
170 {
171 	struct i2s_dai *other = get_other_dai(i2s);
172 
173 	return tx_active(other);
174 }
175 
176 /* If any interface of the controller is transmitting data */
177 static inline bool any_tx_active(struct i2s_dai *i2s)
178 {
179 	return tx_active(i2s) || other_tx_active(i2s);
180 }
181 
182 /* If this interface of the controller is receiving data */
183 static inline bool rx_active(struct i2s_dai *i2s)
184 {
185 	u32 active;
186 
187 	if (!i2s)
188 		return false;
189 
190 	active = readl(i2s->priv->addr + I2SCON) & CON_RXDMA_ACTIVE;
191 
192 	return active ? true : false;
193 }
194 
195 /* If the other interface of the controller is receiving data */
196 static inline bool other_rx_active(struct i2s_dai *i2s)
197 {
198 	struct i2s_dai *other = get_other_dai(i2s);
199 
200 	return rx_active(other);
201 }
202 
203 /* If any interface of the controller is receiving data */
204 static inline bool any_rx_active(struct i2s_dai *i2s)
205 {
206 	return rx_active(i2s) || other_rx_active(i2s);
207 }
208 
209 /* If the other DAI is transmitting or receiving data */
210 static inline bool other_active(struct i2s_dai *i2s)
211 {
212 	return other_rx_active(i2s) || other_tx_active(i2s);
213 }
214 
215 /* If this DAI is transmitting or receiving data */
216 static inline bool this_active(struct i2s_dai *i2s)
217 {
218 	return tx_active(i2s) || rx_active(i2s);
219 }
220 
221 /* If the controller is active anyway */
222 static inline bool any_active(struct i2s_dai *i2s)
223 {
224 	return this_active(i2s) || other_active(i2s);
225 }
226 
227 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
228 {
229 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
230 
231 	return &priv->dai[dai->id - 1];
232 }
233 
234 static inline bool is_opened(struct i2s_dai *i2s)
235 {
236 	if (i2s && (i2s->mode & DAI_OPENED))
237 		return true;
238 	else
239 		return false;
240 }
241 
242 static inline bool is_manager(struct i2s_dai *i2s)
243 {
244 	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
245 		return true;
246 	else
247 		return false;
248 }
249 
250 /* Read RCLK of I2S (in multiples of LRCLK) */
251 static inline unsigned get_rfs(struct i2s_dai *i2s)
252 {
253 	struct samsung_i2s_priv *priv = i2s->priv;
254 	u32 rfs;
255 
256 	rfs = readl(priv->addr + I2SMOD) >> priv->variant_regs->rfs_off;
257 	rfs &= priv->variant_regs->rfs_mask;
258 
259 	switch (rfs) {
260 	case 7: return 192;
261 	case 6: return 96;
262 	case 5: return 128;
263 	case 4: return 64;
264 	case 3:	return 768;
265 	case 2: return 384;
266 	case 1:	return 512;
267 	default: return 256;
268 	}
269 }
270 
271 /* Write RCLK of I2S (in multiples of LRCLK) */
272 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
273 {
274 	struct samsung_i2s_priv *priv = i2s->priv;
275 	u32 mod = readl(priv->addr + I2SMOD);
276 	int rfs_shift = priv->variant_regs->rfs_off;
277 
278 	mod &= ~(priv->variant_regs->rfs_mask << rfs_shift);
279 
280 	switch (rfs) {
281 	case 192:
282 		mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
283 		break;
284 	case 96:
285 		mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
286 		break;
287 	case 128:
288 		mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
289 		break;
290 	case 64:
291 		mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
292 		break;
293 	case 768:
294 		mod |= (MOD_RCLK_768FS << rfs_shift);
295 		break;
296 	case 512:
297 		mod |= (MOD_RCLK_512FS << rfs_shift);
298 		break;
299 	case 384:
300 		mod |= (MOD_RCLK_384FS << rfs_shift);
301 		break;
302 	default:
303 		mod |= (MOD_RCLK_256FS << rfs_shift);
304 		break;
305 	}
306 
307 	writel(mod, priv->addr + I2SMOD);
308 }
309 
310 /* Read bit-clock of I2S (in multiples of LRCLK) */
311 static inline unsigned get_bfs(struct i2s_dai *i2s)
312 {
313 	struct samsung_i2s_priv *priv = i2s->priv;
314 	u32 bfs;
315 
316 	bfs = readl(priv->addr + I2SMOD) >> priv->variant_regs->bfs_off;
317 	bfs &= priv->variant_regs->bfs_mask;
318 
319 	switch (bfs) {
320 	case 8: return 256;
321 	case 7: return 192;
322 	case 6: return 128;
323 	case 5: return 96;
324 	case 4: return 64;
325 	case 3: return 24;
326 	case 2: return 16;
327 	case 1:	return 48;
328 	default: return 32;
329 	}
330 }
331 
332 /* Write bit-clock of I2S (in multiples of LRCLK) */
333 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
334 {
335 	struct samsung_i2s_priv *priv = i2s->priv;
336 	u32 mod = readl(priv->addr + I2SMOD);
337 	int tdm = priv->quirks & QUIRK_SUPPORTS_TDM;
338 	int bfs_shift = priv->variant_regs->bfs_off;
339 
340 	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
341 	if (!tdm && bfs > 48) {
342 		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
343 		return;
344 	}
345 
346 	mod &= ~(priv->variant_regs->bfs_mask << bfs_shift);
347 
348 	switch (bfs) {
349 	case 48:
350 		mod |= (MOD_BCLK_48FS << bfs_shift);
351 		break;
352 	case 32:
353 		mod |= (MOD_BCLK_32FS << bfs_shift);
354 		break;
355 	case 24:
356 		mod |= (MOD_BCLK_24FS << bfs_shift);
357 		break;
358 	case 16:
359 		mod |= (MOD_BCLK_16FS << bfs_shift);
360 		break;
361 	case 64:
362 		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
363 		break;
364 	case 96:
365 		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
366 		break;
367 	case 128:
368 		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
369 		break;
370 	case 192:
371 		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
372 		break;
373 	case 256:
374 		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
375 		break;
376 	default:
377 		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
378 		return;
379 	}
380 
381 	writel(mod, priv->addr + I2SMOD);
382 }
383 
384 /* Sample size */
385 static inline int get_blc(struct i2s_dai *i2s)
386 {
387 	int blc = readl(i2s->priv->addr + I2SMOD);
388 
389 	blc = (blc >> 13) & 0x3;
390 
391 	switch (blc) {
392 	case 2: return 24;
393 	case 1:	return 8;
394 	default: return 16;
395 	}
396 }
397 
398 /* TX channel control */
399 static void i2s_txctrl(struct i2s_dai *i2s, int on)
400 {
401 	struct samsung_i2s_priv *priv = i2s->priv;
402 	void __iomem *addr = priv->addr;
403 	int txr_off = priv->variant_regs->txr_off;
404 	u32 con = readl(addr + I2SCON);
405 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
406 
407 	if (on) {
408 		con |= CON_ACTIVE;
409 		con &= ~CON_TXCH_PAUSE;
410 
411 		if (is_secondary(i2s)) {
412 			con |= CON_TXSDMA_ACTIVE;
413 			con &= ~CON_TXSDMA_PAUSE;
414 		} else {
415 			con |= CON_TXDMA_ACTIVE;
416 			con &= ~CON_TXDMA_PAUSE;
417 		}
418 
419 		if (any_rx_active(i2s))
420 			mod |= 2 << txr_off;
421 		else
422 			mod |= 0 << txr_off;
423 	} else {
424 		if (is_secondary(i2s)) {
425 			con |=  CON_TXSDMA_PAUSE;
426 			con &= ~CON_TXSDMA_ACTIVE;
427 		} else {
428 			con |=  CON_TXDMA_PAUSE;
429 			con &= ~CON_TXDMA_ACTIVE;
430 		}
431 
432 		if (other_tx_active(i2s)) {
433 			writel(con, addr + I2SCON);
434 			return;
435 		}
436 
437 		con |=  CON_TXCH_PAUSE;
438 
439 		if (any_rx_active(i2s))
440 			mod |= 1 << txr_off;
441 		else
442 			con &= ~CON_ACTIVE;
443 	}
444 
445 	writel(mod, addr + I2SMOD);
446 	writel(con, addr + I2SCON);
447 }
448 
449 /* RX Channel Control */
450 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
451 {
452 	struct samsung_i2s_priv *priv = i2s->priv;
453 	void __iomem *addr = priv->addr;
454 	int txr_off = priv->variant_regs->txr_off;
455 	u32 con = readl(addr + I2SCON);
456 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
457 
458 	if (on) {
459 		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
460 		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
461 
462 		if (any_tx_active(i2s))
463 			mod |= 2 << txr_off;
464 		else
465 			mod |= 1 << txr_off;
466 	} else {
467 		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
468 		con &= ~CON_RXDMA_ACTIVE;
469 
470 		if (any_tx_active(i2s))
471 			mod |= 0 << txr_off;
472 		else
473 			con &= ~CON_ACTIVE;
474 	}
475 
476 	writel(mod, addr + I2SMOD);
477 	writel(con, addr + I2SCON);
478 }
479 
480 /* Flush FIFO of an interface */
481 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
482 {
483 	void __iomem *fic;
484 	u32 val;
485 
486 	if (!i2s)
487 		return;
488 
489 	if (is_secondary(i2s))
490 		fic = i2s->priv->addr + I2SFICS;
491 	else
492 		fic = i2s->priv->addr + I2SFIC;
493 
494 	/* Flush the FIFO */
495 	writel(readl(fic) | flush, fic);
496 
497 	/* Be patient */
498 	val = msecs_to_loops(1) / 1000; /* 1 usec */
499 	while (--val)
500 		cpu_relax();
501 
502 	writel(readl(fic) & ~flush, fic);
503 }
504 
505 static int i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int rfs,
506 			  int dir)
507 {
508 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
509 	struct i2s_dai *i2s = to_info(dai);
510 	struct i2s_dai *other = get_other_dai(i2s);
511 	const struct samsung_i2s_variant_regs *i2s_regs = priv->variant_regs;
512 	unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
513 	unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
514 	u32 mod, mask, val = 0;
515 	unsigned long flags;
516 	int ret = 0;
517 
518 	pm_runtime_get_sync(dai->dev);
519 
520 	spin_lock_irqsave(&priv->lock, flags);
521 	mod = readl(priv->addr + I2SMOD);
522 	spin_unlock_irqrestore(&priv->lock, flags);
523 
524 	switch (clk_id) {
525 	case SAMSUNG_I2S_OPCLK:
526 		mask = MOD_OPCLK_MASK;
527 		val = (dir << MOD_OPCLK_SHIFT) & MOD_OPCLK_MASK;
528 		break;
529 	case SAMSUNG_I2S_CDCLK:
530 		mask = 1 << i2s_regs->cdclkcon_off;
531 		/* Shouldn't matter in GATING(CLOCK_IN) mode */
532 		if (dir == SND_SOC_CLOCK_IN)
533 			rfs = 0;
534 
535 		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
536 				(any_active(i2s) &&
537 				(((dir == SND_SOC_CLOCK_IN)
538 					&& !(mod & cdcon_mask)) ||
539 				((dir == SND_SOC_CLOCK_OUT)
540 					&& (mod & cdcon_mask))))) {
541 			dev_err(&i2s->pdev->dev,
542 				"%s:%d Other DAI busy\n", __func__, __LINE__);
543 			ret = -EAGAIN;
544 			goto err;
545 		}
546 
547 		if (dir == SND_SOC_CLOCK_IN)
548 			val = 1 << i2s_regs->cdclkcon_off;
549 
550 		i2s->rfs = rfs;
551 		break;
552 
553 	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
554 	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
555 		mask = 1 << i2s_regs->rclksrc_off;
556 
557 		if ((priv->quirks & QUIRK_NO_MUXPSR)
558 				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
559 			clk_id = 0;
560 		else
561 			clk_id = 1;
562 
563 		if (!any_active(i2s)) {
564 			if (priv->op_clk && !IS_ERR(priv->op_clk)) {
565 				if ((clk_id && !(mod & rsrc_mask)) ||
566 					(!clk_id && (mod & rsrc_mask))) {
567 					clk_disable_unprepare(priv->op_clk);
568 					clk_put(priv->op_clk);
569 				} else {
570 					priv->rclk_srcrate =
571 						clk_get_rate(priv->op_clk);
572 					goto done;
573 				}
574 			}
575 
576 			if (clk_id)
577 				priv->op_clk = clk_get(&i2s->pdev->dev,
578 						"i2s_opclk1");
579 			else
580 				priv->op_clk = clk_get(&i2s->pdev->dev,
581 						"i2s_opclk0");
582 
583 			if (WARN_ON(IS_ERR(priv->op_clk))) {
584 				ret = PTR_ERR(priv->op_clk);
585 				priv->op_clk = NULL;
586 				goto err;
587 			}
588 
589 			ret = clk_prepare_enable(priv->op_clk);
590 			if (ret) {
591 				clk_put(priv->op_clk);
592 				priv->op_clk = NULL;
593 				goto err;
594 			}
595 			priv->rclk_srcrate = clk_get_rate(priv->op_clk);
596 
597 		} else if ((!clk_id && (mod & rsrc_mask))
598 				|| (clk_id && !(mod & rsrc_mask))) {
599 			dev_err(&i2s->pdev->dev,
600 				"%s:%d Other DAI busy\n", __func__, __LINE__);
601 			ret = -EAGAIN;
602 			goto err;
603 		} else {
604 			/* Call can't be on the active DAI */
605 			goto done;
606 		}
607 
608 		if (clk_id == 1)
609 			val = 1 << i2s_regs->rclksrc_off;
610 		break;
611 	default:
612 		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
613 		ret = -EINVAL;
614 		goto err;
615 	}
616 
617 	spin_lock_irqsave(&priv->lock, flags);
618 	mod = readl(priv->addr + I2SMOD);
619 	mod = (mod & ~mask) | val;
620 	writel(mod, priv->addr + I2SMOD);
621 	spin_unlock_irqrestore(&priv->lock, flags);
622 done:
623 	pm_runtime_put(dai->dev);
624 
625 	return 0;
626 err:
627 	pm_runtime_put(dai->dev);
628 	return ret;
629 }
630 
631 static int i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
632 {
633 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
634 	struct i2s_dai *i2s = to_info(dai);
635 	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
636 	u32 mod, tmp = 0;
637 	unsigned long flags;
638 
639 	lrp_shift = priv->variant_regs->lrp_off;
640 	sdf_shift = priv->variant_regs->sdf_off;
641 	mod_slave = 1 << priv->variant_regs->mss_off;
642 
643 	sdf_mask = MOD_SDF_MASK << sdf_shift;
644 	lrp_rlow = MOD_LR_RLOW << lrp_shift;
645 
646 	/* Format is priority */
647 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
648 	case SND_SOC_DAIFMT_RIGHT_J:
649 		tmp |= lrp_rlow;
650 		tmp |= (MOD_SDF_MSB << sdf_shift);
651 		break;
652 	case SND_SOC_DAIFMT_LEFT_J:
653 		tmp |= lrp_rlow;
654 		tmp |= (MOD_SDF_LSB << sdf_shift);
655 		break;
656 	case SND_SOC_DAIFMT_I2S:
657 		tmp |= (MOD_SDF_IIS << sdf_shift);
658 		break;
659 	default:
660 		dev_err(&i2s->pdev->dev, "Format not supported\n");
661 		return -EINVAL;
662 	}
663 
664 	/*
665 	 * INV flag is relative to the FORMAT flag - if set it simply
666 	 * flips the polarity specified by the Standard
667 	 */
668 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
669 	case SND_SOC_DAIFMT_NB_NF:
670 		break;
671 	case SND_SOC_DAIFMT_NB_IF:
672 		if (tmp & lrp_rlow)
673 			tmp &= ~lrp_rlow;
674 		else
675 			tmp |= lrp_rlow;
676 		break;
677 	default:
678 		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
679 		return -EINVAL;
680 	}
681 
682 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
683 	case SND_SOC_DAIFMT_BC_FC:
684 		tmp |= mod_slave;
685 		break;
686 	case SND_SOC_DAIFMT_BP_FP:
687 		/*
688 		 * Set default source clock in Master mode, only when the
689 		 * CLK_I2S_RCLK_SRC clock is not exposed so we ensure any
690 		 * clock configuration assigned in DT is not overwritten.
691 		 */
692 		if (priv->rclk_srcrate == 0 && priv->clk_data.clks == NULL)
693 			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
694 							0, SND_SOC_CLOCK_IN);
695 		break;
696 	default:
697 		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
698 		return -EINVAL;
699 	}
700 
701 	pm_runtime_get_sync(dai->dev);
702 	spin_lock_irqsave(&priv->lock, flags);
703 	mod = readl(priv->addr + I2SMOD);
704 	/*
705 	 * Don't change the I2S mode if any controller is active on this
706 	 * channel.
707 	 */
708 	if (any_active(i2s) &&
709 		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
710 		spin_unlock_irqrestore(&priv->lock, flags);
711 		pm_runtime_put(dai->dev);
712 		dev_err(&i2s->pdev->dev,
713 				"%s:%d Other DAI busy\n", __func__, __LINE__);
714 		return -EAGAIN;
715 	}
716 
717 	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
718 	mod |= tmp;
719 	writel(mod, priv->addr + I2SMOD);
720 	priv->slave_mode = (mod & mod_slave);
721 	spin_unlock_irqrestore(&priv->lock, flags);
722 	pm_runtime_put(dai->dev);
723 
724 	return 0;
725 }
726 
727 static int i2s_hw_params(struct snd_pcm_substream *substream,
728 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
729 {
730 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
731 	struct i2s_dai *i2s = to_info(dai);
732 	u32 mod, mask = 0, val = 0;
733 	struct clk *rclksrc;
734 	unsigned long flags;
735 
736 	WARN_ON(!pm_runtime_active(dai->dev));
737 
738 	if (!is_secondary(i2s))
739 		mask |= (MOD_DC2_EN | MOD_DC1_EN);
740 
741 	switch (params_channels(params)) {
742 	case 6:
743 		val |= MOD_DC2_EN;
744 		fallthrough;
745 	case 4:
746 		val |= MOD_DC1_EN;
747 		break;
748 	case 2:
749 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
750 			i2s->dma_playback.addr_width = 4;
751 		else
752 			i2s->dma_capture.addr_width = 4;
753 		break;
754 	case 1:
755 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
756 			i2s->dma_playback.addr_width = 2;
757 		else
758 			i2s->dma_capture.addr_width = 2;
759 
760 		break;
761 	default:
762 		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
763 				params_channels(params));
764 		return -EINVAL;
765 	}
766 
767 	if (is_secondary(i2s))
768 		mask |= MOD_BLCS_MASK;
769 	else
770 		mask |= MOD_BLCP_MASK;
771 
772 	if (is_manager(i2s))
773 		mask |= MOD_BLC_MASK;
774 
775 	switch (params_width(params)) {
776 	case 8:
777 		if (is_secondary(i2s))
778 			val |= MOD_BLCS_8BIT;
779 		else
780 			val |= MOD_BLCP_8BIT;
781 		if (is_manager(i2s))
782 			val |= MOD_BLC_8BIT;
783 		break;
784 	case 16:
785 		if (is_secondary(i2s))
786 			val |= MOD_BLCS_16BIT;
787 		else
788 			val |= MOD_BLCP_16BIT;
789 		if (is_manager(i2s))
790 			val |= MOD_BLC_16BIT;
791 		break;
792 	case 24:
793 		if (is_secondary(i2s))
794 			val |= MOD_BLCS_24BIT;
795 		else
796 			val |= MOD_BLCP_24BIT;
797 		if (is_manager(i2s))
798 			val |= MOD_BLC_24BIT;
799 		break;
800 	default:
801 		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
802 				params_format(params));
803 		return -EINVAL;
804 	}
805 
806 	spin_lock_irqsave(&priv->lock, flags);
807 	mod = readl(priv->addr + I2SMOD);
808 	mod = (mod & ~mask) | val;
809 	writel(mod, priv->addr + I2SMOD);
810 	spin_unlock_irqrestore(&priv->lock, flags);
811 
812 	snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
813 
814 	i2s->frmclk = params_rate(params);
815 
816 	rclksrc = priv->clk_table[CLK_I2S_RCLK_SRC];
817 	if (rclksrc && !IS_ERR(rclksrc))
818 		priv->rclk_srcrate = clk_get_rate(rclksrc);
819 
820 	return 0;
821 }
822 
823 /* We set constraints on the substream according to the version of I2S */
824 static int i2s_startup(struct snd_pcm_substream *substream,
825 	  struct snd_soc_dai *dai)
826 {
827 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
828 	struct i2s_dai *i2s = to_info(dai);
829 	struct i2s_dai *other = get_other_dai(i2s);
830 	unsigned long flags;
831 
832 	pm_runtime_get_sync(dai->dev);
833 
834 	spin_lock_irqsave(&priv->pcm_lock, flags);
835 
836 	i2s->mode |= DAI_OPENED;
837 
838 	if (is_manager(other))
839 		i2s->mode &= ~DAI_MANAGER;
840 	else
841 		i2s->mode |= DAI_MANAGER;
842 
843 	if (!any_active(i2s) && (priv->quirks & QUIRK_NEED_RSTCLR))
844 		writel(CON_RSTCLR, i2s->priv->addr + I2SCON);
845 
846 	spin_unlock_irqrestore(&priv->pcm_lock, flags);
847 
848 	return 0;
849 }
850 
851 static void i2s_shutdown(struct snd_pcm_substream *substream,
852 	struct snd_soc_dai *dai)
853 {
854 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
855 	struct i2s_dai *i2s = to_info(dai);
856 	struct i2s_dai *other = get_other_dai(i2s);
857 	unsigned long flags;
858 
859 	spin_lock_irqsave(&priv->pcm_lock, flags);
860 
861 	i2s->mode &= ~DAI_OPENED;
862 	i2s->mode &= ~DAI_MANAGER;
863 
864 	if (is_opened(other))
865 		other->mode |= DAI_MANAGER;
866 
867 	/* Reset any constraint on RFS and BFS */
868 	i2s->rfs = 0;
869 	i2s->bfs = 0;
870 
871 	spin_unlock_irqrestore(&priv->pcm_lock, flags);
872 
873 	pm_runtime_put(dai->dev);
874 }
875 
876 static int config_setup(struct i2s_dai *i2s)
877 {
878 	struct samsung_i2s_priv *priv = i2s->priv;
879 	struct i2s_dai *other = get_other_dai(i2s);
880 	unsigned rfs, bfs, blc;
881 	u32 psr;
882 
883 	blc = get_blc(i2s);
884 
885 	bfs = i2s->bfs;
886 
887 	if (!bfs && other)
888 		bfs = other->bfs;
889 
890 	/* Select least possible multiple(2) if no constraint set */
891 	if (!bfs)
892 		bfs = blc * 2;
893 
894 	rfs = i2s->rfs;
895 
896 	if (!rfs && other)
897 		rfs = other->rfs;
898 
899 	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
900 		dev_err(&i2s->pdev->dev,
901 			"%d-RFS not supported for 24-blc\n", rfs);
902 		return -EINVAL;
903 	}
904 
905 	if (!rfs) {
906 		if (bfs == 16 || bfs == 32)
907 			rfs = 256;
908 		else
909 			rfs = 384;
910 	}
911 
912 	/* If already setup and running */
913 	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
914 		dev_err(&i2s->pdev->dev,
915 				"%s:%d Other DAI busy\n", __func__, __LINE__);
916 		return -EAGAIN;
917 	}
918 
919 	set_bfs(i2s, bfs);
920 	set_rfs(i2s, rfs);
921 
922 	/* Don't bother with PSR in Slave mode */
923 	if (priv->slave_mode)
924 		return 0;
925 
926 	if (!(priv->quirks & QUIRK_NO_MUXPSR)) {
927 		psr = priv->rclk_srcrate / i2s->frmclk / rfs;
928 		writel(((psr - 1) << 8) | PSR_PSREN, priv->addr + I2SPSR);
929 		dev_dbg(&i2s->pdev->dev,
930 			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
931 				priv->rclk_srcrate, psr, rfs, bfs);
932 	}
933 
934 	return 0;
935 }
936 
937 static int i2s_trigger(struct snd_pcm_substream *substream,
938 	int cmd, struct snd_soc_dai *dai)
939 {
940 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
941 	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
942 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
943 	struct i2s_dai *i2s = to_info(asoc_rtd_to_cpu(rtd, 0));
944 	unsigned long flags;
945 
946 	switch (cmd) {
947 	case SNDRV_PCM_TRIGGER_START:
948 	case SNDRV_PCM_TRIGGER_RESUME:
949 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
950 		pm_runtime_get_sync(dai->dev);
951 
952 		if (priv->fixup_early)
953 			priv->fixup_early(substream, dai);
954 
955 		spin_lock_irqsave(&priv->lock, flags);
956 
957 		if (config_setup(i2s)) {
958 			spin_unlock_irqrestore(&priv->lock, flags);
959 			return -EINVAL;
960 		}
961 
962 		if (priv->fixup_late)
963 			priv->fixup_late(substream, dai);
964 
965 		if (capture)
966 			i2s_rxctrl(i2s, 1);
967 		else
968 			i2s_txctrl(i2s, 1);
969 
970 		spin_unlock_irqrestore(&priv->lock, flags);
971 		break;
972 	case SNDRV_PCM_TRIGGER_STOP:
973 	case SNDRV_PCM_TRIGGER_SUSPEND:
974 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
975 		spin_lock_irqsave(&priv->lock, flags);
976 
977 		if (capture) {
978 			i2s_rxctrl(i2s, 0);
979 			i2s_fifo(i2s, FIC_RXFLUSH);
980 		} else {
981 			i2s_txctrl(i2s, 0);
982 			i2s_fifo(i2s, FIC_TXFLUSH);
983 		}
984 
985 		spin_unlock_irqrestore(&priv->lock, flags);
986 		pm_runtime_put(dai->dev);
987 		break;
988 	}
989 
990 	return 0;
991 }
992 
993 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
994 	int div_id, int div)
995 {
996 	struct i2s_dai *i2s = to_info(dai);
997 	struct i2s_dai *other = get_other_dai(i2s);
998 
999 	switch (div_id) {
1000 	case SAMSUNG_I2S_DIV_BCLK:
1001 		pm_runtime_get_sync(dai->dev);
1002 		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
1003 			|| (other && other->bfs && (other->bfs != div))) {
1004 			pm_runtime_put(dai->dev);
1005 			dev_err(&i2s->pdev->dev,
1006 				"%s:%d Other DAI busy\n", __func__, __LINE__);
1007 			return -EAGAIN;
1008 		}
1009 		i2s->bfs = div;
1010 		pm_runtime_put(dai->dev);
1011 		break;
1012 	default:
1013 		dev_err(&i2s->pdev->dev,
1014 			"Invalid clock divider(%d)\n", div_id);
1015 		return -EINVAL;
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 static snd_pcm_sframes_t
1022 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
1023 {
1024 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
1025 	struct i2s_dai *i2s = to_info(dai);
1026 	u32 reg = readl(priv->addr + I2SFIC);
1027 	snd_pcm_sframes_t delay;
1028 
1029 	WARN_ON(!pm_runtime_active(dai->dev));
1030 
1031 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1032 		delay = FIC_RXCOUNT(reg);
1033 	else if (is_secondary(i2s))
1034 		delay = FICS_TXCOUNT(readl(priv->addr + I2SFICS));
1035 	else
1036 		delay = (reg >> priv->variant_regs->ftx0cnt_off) & 0x7f;
1037 
1038 	return delay;
1039 }
1040 
1041 #ifdef CONFIG_PM
1042 static int i2s_suspend(struct snd_soc_component *component)
1043 {
1044 	return pm_runtime_force_suspend(component->dev);
1045 }
1046 
1047 static int i2s_resume(struct snd_soc_component *component)
1048 {
1049 	return pm_runtime_force_resume(component->dev);
1050 }
1051 #else
1052 #define i2s_suspend NULL
1053 #define i2s_resume  NULL
1054 #endif
1055 
1056 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
1057 {
1058 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
1059 	struct i2s_dai *i2s = to_info(dai);
1060 	struct i2s_dai *other = get_other_dai(i2s);
1061 	unsigned long flags;
1062 
1063 	pm_runtime_get_sync(dai->dev);
1064 
1065 	if (is_secondary(i2s)) {
1066 		/* If this is probe on the secondary DAI */
1067 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, NULL);
1068 	} else {
1069 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback,
1070 					  &i2s->dma_capture);
1071 
1072 		if (priv->quirks & QUIRK_NEED_RSTCLR)
1073 			writel(CON_RSTCLR, priv->addr + I2SCON);
1074 
1075 		if (priv->quirks & QUIRK_SUPPORTS_IDMA)
1076 			idma_reg_addr_init(priv->addr,
1077 					   other->idma_playback.addr);
1078 	}
1079 
1080 	/* Reset any constraint on RFS and BFS */
1081 	i2s->rfs = 0;
1082 	i2s->bfs = 0;
1083 
1084 	spin_lock_irqsave(&priv->lock, flags);
1085 	i2s_txctrl(i2s, 0);
1086 	i2s_rxctrl(i2s, 0);
1087 	i2s_fifo(i2s, FIC_TXFLUSH);
1088 	i2s_fifo(other, FIC_TXFLUSH);
1089 	i2s_fifo(i2s, FIC_RXFLUSH);
1090 	spin_unlock_irqrestore(&priv->lock, flags);
1091 
1092 	/* Gate CDCLK by default */
1093 	if (!is_opened(other))
1094 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
1095 				0, SND_SOC_CLOCK_IN);
1096 	pm_runtime_put(dai->dev);
1097 
1098 	return 0;
1099 }
1100 
1101 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
1102 {
1103 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
1104 	struct i2s_dai *i2s = to_info(dai);
1105 	unsigned long flags;
1106 
1107 	pm_runtime_get_sync(dai->dev);
1108 
1109 	if (!is_secondary(i2s)) {
1110 		if (priv->quirks & QUIRK_NEED_RSTCLR) {
1111 			spin_lock_irqsave(&priv->lock, flags);
1112 			writel(0, priv->addr + I2SCON);
1113 			spin_unlock_irqrestore(&priv->lock, flags);
1114 		}
1115 	}
1116 
1117 	pm_runtime_put(dai->dev);
1118 
1119 	return 0;
1120 }
1121 
1122 static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1123 	.trigger = i2s_trigger,
1124 	.hw_params = i2s_hw_params,
1125 	.set_fmt = i2s_set_fmt,
1126 	.set_clkdiv = i2s_set_clkdiv,
1127 	.set_sysclk = i2s_set_sysclk,
1128 	.startup = i2s_startup,
1129 	.shutdown = i2s_shutdown,
1130 	.delay = i2s_delay,
1131 };
1132 
1133 static const struct snd_soc_dapm_widget samsung_i2s_widgets[] = {
1134 	/* Backend DAI  */
1135 	SND_SOC_DAPM_AIF_OUT("Mixer DAI TX", NULL, 0, SND_SOC_NOPM, 0, 0),
1136 	SND_SOC_DAPM_AIF_IN("Mixer DAI RX", NULL, 0, SND_SOC_NOPM, 0, 0),
1137 
1138 	/* Playback Mixer */
1139 	SND_SOC_DAPM_MIXER("Playback Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1140 };
1141 
1142 static const struct snd_soc_dapm_route samsung_i2s_dapm_routes[] = {
1143 	{ "Playback Mixer", NULL, "Primary Playback" },
1144 	{ "Playback Mixer", NULL, "Secondary Playback" },
1145 
1146 	{ "Mixer DAI TX", NULL, "Playback Mixer" },
1147 	{ "Primary Capture", NULL, "Mixer DAI RX" },
1148 };
1149 
1150 static const struct snd_soc_component_driver samsung_i2s_component = {
1151 	.name = "samsung-i2s",
1152 
1153 	.dapm_widgets = samsung_i2s_widgets,
1154 	.num_dapm_widgets = ARRAY_SIZE(samsung_i2s_widgets),
1155 
1156 	.dapm_routes = samsung_i2s_dapm_routes,
1157 	.num_dapm_routes = ARRAY_SIZE(samsung_i2s_dapm_routes),
1158 
1159 	.suspend = i2s_suspend,
1160 	.resume = i2s_resume,
1161 
1162 	.legacy_dai_naming = 1,
1163 };
1164 
1165 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
1166 			  SNDRV_PCM_FMTBIT_S24_LE)
1167 
1168 static int i2s_alloc_dais(struct samsung_i2s_priv *priv,
1169 			  const struct samsung_i2s_dai_data *i2s_dai_data,
1170 			  int num_dais)
1171 {
1172 	static const char *dai_names[] = { "samsung-i2s", "samsung-i2s-sec" };
1173 	static const char *stream_names[] = { "Primary Playback",
1174 					      "Secondary Playback" };
1175 	struct snd_soc_dai_driver *dai_drv;
1176 	int i;
1177 
1178 	priv->dai = devm_kcalloc(&priv->pdev->dev, num_dais,
1179 				     sizeof(struct i2s_dai), GFP_KERNEL);
1180 	if (!priv->dai)
1181 		return -ENOMEM;
1182 
1183 	priv->dai_drv = devm_kcalloc(&priv->pdev->dev, num_dais,
1184 				     sizeof(*dai_drv), GFP_KERNEL);
1185 	if (!priv->dai_drv)
1186 		return -ENOMEM;
1187 
1188 	for (i = 0; i < num_dais; i++) {
1189 		dai_drv = &priv->dai_drv[i];
1190 
1191 		dai_drv->probe = samsung_i2s_dai_probe;
1192 		dai_drv->remove = samsung_i2s_dai_remove;
1193 
1194 		dai_drv->symmetric_rate = 1;
1195 		dai_drv->ops = &samsung_i2s_dai_ops;
1196 
1197 		dai_drv->playback.channels_min = 1;
1198 		dai_drv->playback.channels_max = 2;
1199 		dai_drv->playback.rates = i2s_dai_data->pcm_rates;
1200 		dai_drv->playback.formats = SAMSUNG_I2S_FMTS;
1201 		dai_drv->playback.stream_name = stream_names[i];
1202 
1203 		dai_drv->id = i + 1;
1204 		dai_drv->name = dai_names[i];
1205 
1206 		priv->dai[i].drv = &priv->dai_drv[i];
1207 		priv->dai[i].pdev = priv->pdev;
1208 	}
1209 
1210 	/* Initialize capture only for the primary DAI */
1211 	dai_drv = &priv->dai_drv[SAMSUNG_I2S_ID_PRIMARY - 1];
1212 
1213 	dai_drv->capture.channels_min = 1;
1214 	dai_drv->capture.channels_max = 2;
1215 	dai_drv->capture.rates = i2s_dai_data->pcm_rates;
1216 	dai_drv->capture.formats = SAMSUNG_I2S_FMTS;
1217 	dai_drv->capture.stream_name = "Primary Capture";
1218 
1219 	return 0;
1220 }
1221 
1222 #ifdef CONFIG_PM
1223 static int i2s_runtime_suspend(struct device *dev)
1224 {
1225 	struct samsung_i2s_priv *priv = dev_get_drvdata(dev);
1226 
1227 	priv->suspend_i2smod = readl(priv->addr + I2SMOD);
1228 	priv->suspend_i2scon = readl(priv->addr + I2SCON);
1229 	priv->suspend_i2spsr = readl(priv->addr + I2SPSR);
1230 
1231 	clk_disable_unprepare(priv->op_clk);
1232 	clk_disable_unprepare(priv->clk);
1233 
1234 	return 0;
1235 }
1236 
1237 static int i2s_runtime_resume(struct device *dev)
1238 {
1239 	struct samsung_i2s_priv *priv = dev_get_drvdata(dev);
1240 	int ret;
1241 
1242 	ret = clk_prepare_enable(priv->clk);
1243 	if (ret)
1244 		return ret;
1245 
1246 	if (priv->op_clk) {
1247 		ret = clk_prepare_enable(priv->op_clk);
1248 		if (ret) {
1249 			clk_disable_unprepare(priv->clk);
1250 			return ret;
1251 		}
1252 	}
1253 
1254 	writel(priv->suspend_i2scon, priv->addr + I2SCON);
1255 	writel(priv->suspend_i2smod, priv->addr + I2SMOD);
1256 	writel(priv->suspend_i2spsr, priv->addr + I2SPSR);
1257 
1258 	return 0;
1259 }
1260 #endif /* CONFIG_PM */
1261 
1262 static void i2s_unregister_clocks(struct samsung_i2s_priv *priv)
1263 {
1264 	int i;
1265 
1266 	for (i = 0; i < priv->clk_data.clk_num; i++) {
1267 		if (!IS_ERR(priv->clk_table[i]))
1268 			clk_unregister(priv->clk_table[i]);
1269 	}
1270 }
1271 
1272 static void i2s_unregister_clock_provider(struct samsung_i2s_priv *priv)
1273 {
1274 	of_clk_del_provider(priv->pdev->dev.of_node);
1275 	i2s_unregister_clocks(priv);
1276 }
1277 
1278 
1279 static int i2s_register_clock_provider(struct samsung_i2s_priv *priv)
1280 {
1281 
1282 	const char * const i2s_clk_desc[] = { "cdclk", "rclk_src", "prescaler" };
1283 	const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
1284 	const char *p_names[2] = { NULL };
1285 	struct device *dev = &priv->pdev->dev;
1286 	const struct samsung_i2s_variant_regs *reg_info = priv->variant_regs;
1287 	const char *i2s_clk_name[ARRAY_SIZE(i2s_clk_desc)];
1288 	struct clk *rclksrc;
1289 	int ret, i;
1290 
1291 	/* Register the clock provider only if it's expected in the DTB */
1292 	if (!of_find_property(dev->of_node, "#clock-cells", NULL))
1293 		return 0;
1294 
1295 	/* Get the RCLKSRC mux clock parent clock names */
1296 	for (i = 0; i < ARRAY_SIZE(p_names); i++) {
1297 		rclksrc = clk_get(dev, clk_name[i]);
1298 		if (IS_ERR(rclksrc))
1299 			continue;
1300 		p_names[i] = __clk_get_name(rclksrc);
1301 		clk_put(rclksrc);
1302 	}
1303 
1304 	for (i = 0; i < ARRAY_SIZE(i2s_clk_desc); i++) {
1305 		i2s_clk_name[i] = devm_kasprintf(dev, GFP_KERNEL, "%s_%s",
1306 						dev_name(dev), i2s_clk_desc[i]);
1307 		if (!i2s_clk_name[i])
1308 			return -ENOMEM;
1309 	}
1310 
1311 	if (!(priv->quirks & QUIRK_NO_MUXPSR)) {
1312 		/* Activate the prescaler */
1313 		u32 val = readl(priv->addr + I2SPSR);
1314 		writel(val | PSR_PSREN, priv->addr + I2SPSR);
1315 
1316 		priv->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(dev,
1317 				i2s_clk_name[CLK_I2S_RCLK_SRC], p_names,
1318 				ARRAY_SIZE(p_names),
1319 				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1320 				priv->addr + I2SMOD, reg_info->rclksrc_off,
1321 				1, 0, &priv->lock);
1322 
1323 		priv->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(dev,
1324 				i2s_clk_name[CLK_I2S_RCLK_PSR],
1325 				i2s_clk_name[CLK_I2S_RCLK_SRC],
1326 				CLK_SET_RATE_PARENT,
1327 				priv->addr + I2SPSR, 8, 6, 0, &priv->lock);
1328 
1329 		p_names[0] = i2s_clk_name[CLK_I2S_RCLK_PSR];
1330 		priv->clk_data.clk_num = 2;
1331 	}
1332 
1333 	priv->clk_table[CLK_I2S_CDCLK] = clk_register_gate(dev,
1334 				i2s_clk_name[CLK_I2S_CDCLK], p_names[0],
1335 				CLK_SET_RATE_PARENT,
1336 				priv->addr + I2SMOD, reg_info->cdclkcon_off,
1337 				CLK_GATE_SET_TO_DISABLE, &priv->lock);
1338 
1339 	priv->clk_data.clk_num += 1;
1340 	priv->clk_data.clks = priv->clk_table;
1341 
1342 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1343 				  &priv->clk_data);
1344 	if (ret < 0) {
1345 		dev_err(dev, "failed to add clock provider: %d\n", ret);
1346 		i2s_unregister_clocks(priv);
1347 	}
1348 
1349 	return ret;
1350 }
1351 
1352 /* Create platform device for the secondary PCM */
1353 static int i2s_create_secondary_device(struct samsung_i2s_priv *priv)
1354 {
1355 	struct platform_device *pdev_sec;
1356 	const char *devname;
1357 	int ret;
1358 
1359 	devname = devm_kasprintf(&priv->pdev->dev, GFP_KERNEL, "%s-sec",
1360 				 dev_name(&priv->pdev->dev));
1361 	if (!devname)
1362 		return -ENOMEM;
1363 
1364 	pdev_sec = platform_device_alloc(devname, -1);
1365 	if (!pdev_sec)
1366 		return -ENOMEM;
1367 
1368 	pdev_sec->driver_override = kstrdup("samsung-i2s", GFP_KERNEL);
1369 	if (!pdev_sec->driver_override) {
1370 		platform_device_put(pdev_sec);
1371 		return -ENOMEM;
1372 	}
1373 
1374 	ret = platform_device_add(pdev_sec);
1375 	if (ret < 0) {
1376 		platform_device_put(pdev_sec);
1377 		return ret;
1378 	}
1379 
1380 	ret = device_attach(&pdev_sec->dev);
1381 	if (ret <= 0) {
1382 		platform_device_unregister(priv->pdev_sec);
1383 		dev_info(&pdev_sec->dev, "device_attach() failed\n");
1384 		return ret;
1385 	}
1386 
1387 	priv->pdev_sec = pdev_sec;
1388 
1389 	return 0;
1390 }
1391 
1392 static void i2s_delete_secondary_device(struct samsung_i2s_priv *priv)
1393 {
1394 	platform_device_unregister(priv->pdev_sec);
1395 	priv->pdev_sec = NULL;
1396 }
1397 
1398 static int samsung_i2s_probe(struct platform_device *pdev)
1399 {
1400 	struct i2s_dai *pri_dai, *sec_dai = NULL;
1401 	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
1402 	u32 regs_base, idma_addr = 0;
1403 	struct device_node *np = pdev->dev.of_node;
1404 	const struct samsung_i2s_dai_data *i2s_dai_data;
1405 	const struct platform_device_id *id;
1406 	struct samsung_i2s_priv *priv;
1407 	struct resource *res;
1408 	int num_dais, ret;
1409 
1410 	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
1411 		i2s_dai_data = of_device_get_match_data(&pdev->dev);
1412 	} else {
1413 		id = platform_get_device_id(pdev);
1414 
1415 		/* Nothing to do if it is the secondary device probe */
1416 		if (!id)
1417 			return 0;
1418 
1419 		i2s_dai_data = (struct samsung_i2s_dai_data *)id->driver_data;
1420 	}
1421 
1422 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1423 	if (!priv)
1424 		return -ENOMEM;
1425 
1426 	if (np) {
1427 		priv->quirks = i2s_dai_data->quirks;
1428 		priv->fixup_early = i2s_dai_data->fixup_early;
1429 		priv->fixup_late = i2s_dai_data->fixup_late;
1430 	} else {
1431 		if (!i2s_pdata) {
1432 			dev_err(&pdev->dev, "Missing platform data\n");
1433 			return -EINVAL;
1434 		}
1435 		priv->quirks = i2s_pdata->type.quirks;
1436 	}
1437 
1438 	num_dais = (priv->quirks & QUIRK_SEC_DAI) ? 2 : 1;
1439 	priv->pdev = pdev;
1440 	priv->variant_regs = i2s_dai_data->i2s_variant_regs;
1441 
1442 	ret = i2s_alloc_dais(priv, i2s_dai_data, num_dais);
1443 	if (ret < 0)
1444 		return ret;
1445 
1446 	pri_dai = &priv->dai[SAMSUNG_I2S_ID_PRIMARY - 1];
1447 
1448 	spin_lock_init(&priv->lock);
1449 	spin_lock_init(&priv->pcm_lock);
1450 
1451 	if (!np) {
1452 		pri_dai->dma_playback.filter_data = i2s_pdata->dma_playback;
1453 		pri_dai->dma_capture.filter_data = i2s_pdata->dma_capture;
1454 		pri_dai->filter = i2s_pdata->dma_filter;
1455 
1456 		idma_addr = i2s_pdata->type.idma_addr;
1457 	} else {
1458 		if (of_property_read_u32(np, "samsung,idma-addr",
1459 					 &idma_addr)) {
1460 			if (priv->quirks & QUIRK_SUPPORTS_IDMA) {
1461 				dev_info(&pdev->dev, "idma address is not"\
1462 						"specified");
1463 			}
1464 		}
1465 	}
1466 
1467 	priv->addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1468 	if (IS_ERR(priv->addr))
1469 		return PTR_ERR(priv->addr);
1470 
1471 	regs_base = res->start;
1472 
1473 	priv->clk = devm_clk_get(&pdev->dev, "iis");
1474 	if (IS_ERR(priv->clk)) {
1475 		dev_err(&pdev->dev, "Failed to get iis clock\n");
1476 		return PTR_ERR(priv->clk);
1477 	}
1478 
1479 	ret = clk_prepare_enable(priv->clk);
1480 	if (ret != 0) {
1481 		dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
1482 		return ret;
1483 	}
1484 	pri_dai->dma_playback.addr = regs_base + I2STXD;
1485 	pri_dai->dma_capture.addr = regs_base + I2SRXD;
1486 	pri_dai->dma_playback.chan_name = "tx";
1487 	pri_dai->dma_capture.chan_name = "rx";
1488 	pri_dai->dma_playback.addr_width = 4;
1489 	pri_dai->dma_capture.addr_width = 4;
1490 	pri_dai->priv = priv;
1491 
1492 	if (priv->quirks & QUIRK_PRI_6CHAN)
1493 		pri_dai->drv->playback.channels_max = 6;
1494 
1495 	ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter,
1496 						 "tx", "rx", NULL);
1497 	if (ret < 0)
1498 		goto err_disable_clk;
1499 
1500 	if (priv->quirks & QUIRK_SEC_DAI) {
1501 		sec_dai = &priv->dai[SAMSUNG_I2S_ID_SECONDARY - 1];
1502 
1503 		sec_dai->dma_playback.addr = regs_base + I2STXDS;
1504 		sec_dai->dma_playback.chan_name = "tx-sec";
1505 
1506 		if (!np) {
1507 			sec_dai->dma_playback.filter_data = i2s_pdata->dma_play_sec;
1508 			sec_dai->filter = i2s_pdata->dma_filter;
1509 		}
1510 
1511 		sec_dai->dma_playback.addr_width = 4;
1512 		sec_dai->idma_playback.addr = idma_addr;
1513 		sec_dai->pri_dai = pri_dai;
1514 		sec_dai->priv = priv;
1515 		pri_dai->sec_dai = sec_dai;
1516 
1517 		ret = i2s_create_secondary_device(priv);
1518 		if (ret < 0)
1519 			goto err_disable_clk;
1520 
1521 		ret = samsung_asoc_dma_platform_register(&priv->pdev_sec->dev,
1522 						sec_dai->filter, "tx-sec", NULL,
1523 						&pdev->dev);
1524 		if (ret < 0)
1525 			goto err_del_sec;
1526 
1527 	}
1528 
1529 	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1530 		dev_err(&pdev->dev, "Unable to configure gpio\n");
1531 		ret = -EINVAL;
1532 		goto err_del_sec;
1533 	}
1534 
1535 	dev_set_drvdata(&pdev->dev, priv);
1536 
1537 	ret = devm_snd_soc_register_component(&pdev->dev,
1538 					&samsung_i2s_component,
1539 					priv->dai_drv, num_dais);
1540 	if (ret < 0)
1541 		goto err_del_sec;
1542 
1543 	pm_runtime_set_active(&pdev->dev);
1544 	pm_runtime_enable(&pdev->dev);
1545 
1546 	ret = i2s_register_clock_provider(priv);
1547 	if (ret < 0)
1548 		goto err_disable_pm;
1549 
1550 	priv->op_clk = clk_get_parent(priv->clk_table[CLK_I2S_RCLK_SRC]);
1551 
1552 	return 0;
1553 
1554 err_disable_pm:
1555 	pm_runtime_disable(&pdev->dev);
1556 err_del_sec:
1557 	i2s_delete_secondary_device(priv);
1558 err_disable_clk:
1559 	clk_disable_unprepare(priv->clk);
1560 	return ret;
1561 }
1562 
1563 static int samsung_i2s_remove(struct platform_device *pdev)
1564 {
1565 	struct samsung_i2s_priv *priv = dev_get_drvdata(&pdev->dev);
1566 
1567 	/* The secondary device has no driver data assigned */
1568 	if (!priv)
1569 		return 0;
1570 
1571 	pm_runtime_get_sync(&pdev->dev);
1572 	pm_runtime_disable(&pdev->dev);
1573 
1574 	i2s_unregister_clock_provider(priv);
1575 	i2s_delete_secondary_device(priv);
1576 	clk_disable_unprepare(priv->clk);
1577 
1578 	pm_runtime_put_noidle(&pdev->dev);
1579 
1580 	return 0;
1581 }
1582 
1583 static void fsd_i2s_fixup_early(struct snd_pcm_substream *substream,
1584 		struct snd_soc_dai *dai)
1585 {
1586 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1587 	struct i2s_dai *i2s = to_info(asoc_rtd_to_cpu(rtd, 0));
1588 	struct i2s_dai *other = get_other_dai(i2s);
1589 
1590 	if (!is_opened(other)) {
1591 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 0, SND_SOC_CLOCK_OUT);
1592 		i2s_set_sysclk(dai, SAMSUNG_I2S_OPCLK, 0, MOD_OPCLK_PCLK);
1593 	}
1594 }
1595 
1596 static void fsd_i2s_fixup_late(struct snd_pcm_substream *substream,
1597 		struct snd_soc_dai *dai)
1598 {
1599 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1600 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
1601 	struct i2s_dai *i2s = to_info(asoc_rtd_to_cpu(rtd, 0));
1602 	struct i2s_dai *other = get_other_dai(i2s);
1603 
1604 	if (!is_opened(other))
1605 		writel(PSR_PSVAL(2) | PSR_PSREN, priv->addr + I2SPSR);
1606 }
1607 
1608 static const struct samsung_i2s_variant_regs i2sv3_regs = {
1609 	.bfs_off = 1,
1610 	.rfs_off = 3,
1611 	.sdf_off = 5,
1612 	.txr_off = 8,
1613 	.rclksrc_off = 10,
1614 	.mss_off = 11,
1615 	.cdclkcon_off = 12,
1616 	.lrp_off = 7,
1617 	.bfs_mask = 0x3,
1618 	.rfs_mask = 0x3,
1619 	.ftx0cnt_off = 8,
1620 };
1621 
1622 static const struct samsung_i2s_variant_regs i2sv6_regs = {
1623 	.bfs_off = 0,
1624 	.rfs_off = 4,
1625 	.sdf_off = 6,
1626 	.txr_off = 8,
1627 	.rclksrc_off = 10,
1628 	.mss_off = 11,
1629 	.cdclkcon_off = 12,
1630 	.lrp_off = 15,
1631 	.bfs_mask = 0xf,
1632 	.rfs_mask = 0x3,
1633 	.ftx0cnt_off = 8,
1634 };
1635 
1636 static const struct samsung_i2s_variant_regs i2sv7_regs = {
1637 	.bfs_off = 0,
1638 	.rfs_off = 4,
1639 	.sdf_off = 7,
1640 	.txr_off = 9,
1641 	.rclksrc_off = 11,
1642 	.mss_off = 12,
1643 	.cdclkcon_off = 22,
1644 	.lrp_off = 15,
1645 	.bfs_mask = 0xf,
1646 	.rfs_mask = 0x7,
1647 	.ftx0cnt_off = 0,
1648 };
1649 
1650 static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1651 	.bfs_off = 0,
1652 	.rfs_off = 3,
1653 	.sdf_off = 6,
1654 	.txr_off = 8,
1655 	.rclksrc_off = 10,
1656 	.mss_off = 11,
1657 	.cdclkcon_off = 12,
1658 	.lrp_off = 15,
1659 	.bfs_mask = 0x7,
1660 	.rfs_mask = 0x7,
1661 	.ftx0cnt_off = 8,
1662 };
1663 
1664 static const struct samsung_i2s_dai_data i2sv3_dai_type = {
1665 	.quirks = QUIRK_NO_MUXPSR,
1666 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1667 	.i2s_variant_regs = &i2sv3_regs,
1668 };
1669 
1670 static const struct samsung_i2s_dai_data i2sv5_dai_type __maybe_unused = {
1671 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1672 			QUIRK_SUPPORTS_IDMA,
1673 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1674 	.i2s_variant_regs = &i2sv3_regs,
1675 };
1676 
1677 static const struct samsung_i2s_dai_data i2sv6_dai_type __maybe_unused = {
1678 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1679 			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1680 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1681 	.i2s_variant_regs = &i2sv6_regs,
1682 };
1683 
1684 static const struct samsung_i2s_dai_data i2sv7_dai_type __maybe_unused = {
1685 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1686 			QUIRK_SUPPORTS_TDM,
1687 	.pcm_rates = SNDRV_PCM_RATE_8000_192000,
1688 	.i2s_variant_regs = &i2sv7_regs,
1689 };
1690 
1691 static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 __maybe_unused = {
1692 	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1693 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1694 	.i2s_variant_regs = &i2sv5_i2s1_regs,
1695 };
1696 
1697 static const struct samsung_i2s_dai_data fsd_dai_type __maybe_unused = {
1698 	.quirks = QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR | QUIRK_SUPPORTS_TDM,
1699 	.pcm_rates = SNDRV_PCM_RATE_8000_192000,
1700 	.i2s_variant_regs = &i2sv7_regs,
1701 	.fixup_early = fsd_i2s_fixup_early,
1702 	.fixup_late = fsd_i2s_fixup_late,
1703 };
1704 
1705 static const struct platform_device_id samsung_i2s_driver_ids[] = {
1706 	{
1707 		.name           = "samsung-i2s",
1708 		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
1709 	},
1710 	{},
1711 };
1712 MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
1713 
1714 #ifdef CONFIG_OF
1715 static const struct of_device_id exynos_i2s_match[] = {
1716 	{
1717 		.compatible = "samsung,s3c6410-i2s",
1718 		.data = &i2sv3_dai_type,
1719 	}, {
1720 		.compatible = "samsung,s5pv210-i2s",
1721 		.data = &i2sv5_dai_type,
1722 	}, {
1723 		.compatible = "samsung,exynos5420-i2s",
1724 		.data = &i2sv6_dai_type,
1725 	}, {
1726 		.compatible = "samsung,exynos7-i2s",
1727 		.data = &i2sv7_dai_type,
1728 	}, {
1729 		.compatible = "samsung,exynos7-i2s1",
1730 		.data = &i2sv5_dai_type_i2s1,
1731 	}, {
1732 		.compatible = "tesla,fsd-i2s",
1733 		.data = &fsd_dai_type,
1734 	},
1735 	{},
1736 };
1737 MODULE_DEVICE_TABLE(of, exynos_i2s_match);
1738 #endif
1739 
1740 static const struct dev_pm_ops samsung_i2s_pm = {
1741 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
1742 				i2s_runtime_resume, NULL)
1743 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1744 				     pm_runtime_force_resume)
1745 };
1746 
1747 static struct platform_driver samsung_i2s_driver = {
1748 	.probe  = samsung_i2s_probe,
1749 	.remove = samsung_i2s_remove,
1750 	.id_table = samsung_i2s_driver_ids,
1751 	.driver = {
1752 		.name = "samsung-i2s",
1753 		.of_match_table = of_match_ptr(exynos_i2s_match),
1754 		.pm = &samsung_i2s_pm,
1755 	},
1756 };
1757 
1758 module_platform_driver(samsung_i2s_driver);
1759 
1760 /* Module information */
1761 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1762 MODULE_DESCRIPTION("Samsung I2S Interface");
1763 MODULE_ALIAS("platform:samsung-i2s");
1764 MODULE_LICENSE("GPL");
1765