xref: /linux/sound/soc/ti/davinci-mcasp.c (revision 37744feebc086908fd89760650f458ab19071750)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ALSA SoC McASP Audio Layer for TI DAVINCI processor
4  *
5  * Multi-channel Audio Serial Port Driver
6  *
7  * Author: Nirmal Pandey <n-pandey@ti.com>,
8  *         Suresh Rajashekara <suresh.r@ti.com>
9  *         Steve Chen <schen@.mvista.com>
10  *
11  * Copyright:   (C) 2009 MontaVista Software, Inc., <source@mvista.com>
12  * Copyright:   (C) 2009  Texas Instruments, India
13  */
14 
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/io.h>
21 #include <linux/clk.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/of.h>
24 #include <linux/of_platform.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_data/davinci_asp.h>
27 #include <linux/math64.h>
28 #include <linux/bitmap.h>
29 #include <linux/gpio/driver.h>
30 
31 #include <sound/asoundef.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/initval.h>
36 #include <sound/soc.h>
37 #include <sound/dmaengine_pcm.h>
38 
39 #include "edma-pcm.h"
40 #include "sdma-pcm.h"
41 #include "udma-pcm.h"
42 #include "davinci-mcasp.h"
43 
44 #define MCASP_MAX_AFIFO_DEPTH	64
45 
46 #ifdef CONFIG_PM
47 static u32 context_regs[] = {
48 	DAVINCI_MCASP_TXFMCTL_REG,
49 	DAVINCI_MCASP_RXFMCTL_REG,
50 	DAVINCI_MCASP_TXFMT_REG,
51 	DAVINCI_MCASP_RXFMT_REG,
52 	DAVINCI_MCASP_ACLKXCTL_REG,
53 	DAVINCI_MCASP_ACLKRCTL_REG,
54 	DAVINCI_MCASP_AHCLKXCTL_REG,
55 	DAVINCI_MCASP_AHCLKRCTL_REG,
56 	DAVINCI_MCASP_PDIR_REG,
57 	DAVINCI_MCASP_PFUNC_REG,
58 	DAVINCI_MCASP_RXMASK_REG,
59 	DAVINCI_MCASP_TXMASK_REG,
60 	DAVINCI_MCASP_RXTDM_REG,
61 	DAVINCI_MCASP_TXTDM_REG,
62 };
63 
64 struct davinci_mcasp_context {
65 	u32	config_regs[ARRAY_SIZE(context_regs)];
66 	u32	afifo_regs[2]; /* for read/write fifo control registers */
67 	u32	*xrsr_regs; /* for serializer configuration */
68 	bool	pm_state;
69 };
70 #endif
71 
72 struct davinci_mcasp_ruledata {
73 	struct davinci_mcasp *mcasp;
74 	int serializers;
75 };
76 
77 struct davinci_mcasp {
78 	struct snd_dmaengine_dai_dma_data dma_data[2];
79 	void __iomem *base;
80 	u32 fifo_base;
81 	struct device *dev;
82 	struct snd_pcm_substream *substreams[2];
83 	unsigned int dai_fmt;
84 
85 	/* McASP specific data */
86 	int	tdm_slots;
87 	u32	tdm_mask[2];
88 	int	slot_width;
89 	u8	op_mode;
90 	u8	dismod;
91 	u8	num_serializer;
92 	u8	*serial_dir;
93 	u8	version;
94 	u8	bclk_div;
95 	int	streams;
96 	u32	irq_request[2];
97 	int	dma_request[2];
98 
99 	int	sysclk_freq;
100 	bool	bclk_master;
101 	u32	auxclk_fs_ratio;
102 
103 	unsigned long pdir; /* Pin direction bitfield */
104 
105 	/* McASP FIFO related */
106 	u8	txnumevt;
107 	u8	rxnumevt;
108 
109 	bool	dat_port;
110 
111 	/* Used for comstraint setting on the second stream */
112 	u32	channels;
113 	int	max_format_width;
114 	u8	active_serializers[2];
115 
116 #ifdef CONFIG_GPIOLIB
117 	struct gpio_chip gpio_chip;
118 #endif
119 
120 #ifdef CONFIG_PM
121 	struct davinci_mcasp_context context;
122 #endif
123 
124 	struct davinci_mcasp_ruledata ruledata[2];
125 	struct snd_pcm_hw_constraint_list chconstr[2];
126 };
127 
128 static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
129 				  u32 val)
130 {
131 	void __iomem *reg = mcasp->base + offset;
132 	__raw_writel(__raw_readl(reg) | val, reg);
133 }
134 
135 static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
136 				  u32 val)
137 {
138 	void __iomem *reg = mcasp->base + offset;
139 	__raw_writel((__raw_readl(reg) & ~(val)), reg);
140 }
141 
142 static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
143 				  u32 val, u32 mask)
144 {
145 	void __iomem *reg = mcasp->base + offset;
146 	__raw_writel((__raw_readl(reg) & ~mask) | val, reg);
147 }
148 
149 static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
150 				 u32 val)
151 {
152 	__raw_writel(val, mcasp->base + offset);
153 }
154 
155 static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
156 {
157 	return (u32)__raw_readl(mcasp->base + offset);
158 }
159 
160 static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
161 {
162 	int i = 0;
163 
164 	mcasp_set_bits(mcasp, ctl_reg, val);
165 
166 	/* programming GBLCTL needs to read back from GBLCTL and verfiy */
167 	/* loop count is to avoid the lock-up */
168 	for (i = 0; i < 1000; i++) {
169 		if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
170 			break;
171 	}
172 
173 	if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
174 		printk(KERN_ERR "GBLCTL write error\n");
175 }
176 
177 static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
178 {
179 	u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
180 	u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
181 
182 	return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
183 }
184 
185 static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable)
186 {
187 	u32 bit = PIN_BIT_AMUTE;
188 
189 	for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) {
190 		if (enable)
191 			mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
192 		else
193 			mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
194 	}
195 }
196 
197 static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable)
198 {
199 	u32 bit;
200 
201 	for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AMUTE) {
202 		if (enable)
203 			mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
204 		else
205 			mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
206 	}
207 }
208 
209 static void mcasp_start_rx(struct davinci_mcasp *mcasp)
210 {
211 	if (mcasp->rxnumevt) {	/* enable FIFO */
212 		u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
213 
214 		mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
215 		mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
216 	}
217 
218 	/* Start clocks */
219 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
220 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
221 	/*
222 	 * When ASYNC == 0 the transmit and receive sections operate
223 	 * synchronously from the transmit clock and frame sync. We need to make
224 	 * sure that the TX signlas are enabled when starting reception.
225 	 */
226 	if (mcasp_is_synchronous(mcasp)) {
227 		mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
228 		mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
229 		mcasp_set_clk_pdir(mcasp, true);
230 	}
231 
232 	/* Activate serializer(s) */
233 	mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
234 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
235 	/* Release RX state machine */
236 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
237 	/* Release Frame Sync generator */
238 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
239 	if (mcasp_is_synchronous(mcasp))
240 		mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
241 
242 	/* enable receive IRQs */
243 	mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
244 		       mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
245 }
246 
247 static void mcasp_start_tx(struct davinci_mcasp *mcasp)
248 {
249 	u32 cnt;
250 
251 	if (mcasp->txnumevt) {	/* enable FIFO */
252 		u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
253 
254 		mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
255 		mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
256 	}
257 
258 	/* Start clocks */
259 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
260 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
261 	mcasp_set_clk_pdir(mcasp, true);
262 
263 	/* Activate serializer(s) */
264 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
265 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
266 
267 	/* wait for XDATA to be cleared */
268 	cnt = 0;
269 	while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
270 	       (cnt < 100000))
271 		cnt++;
272 
273 	mcasp_set_axr_pdir(mcasp, true);
274 
275 	/* Release TX state machine */
276 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
277 	/* Release Frame Sync generator */
278 	mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
279 
280 	/* enable transmit IRQs */
281 	mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
282 		       mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
283 }
284 
285 static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
286 {
287 	mcasp->streams++;
288 
289 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
290 		mcasp_start_tx(mcasp);
291 	else
292 		mcasp_start_rx(mcasp);
293 }
294 
295 static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
296 {
297 	/* disable IRQ sources */
298 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
299 		       mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
300 
301 	/*
302 	 * In synchronous mode stop the TX clocks if no other stream is
303 	 * running
304 	 */
305 	if (mcasp_is_synchronous(mcasp) && !mcasp->streams) {
306 		mcasp_set_clk_pdir(mcasp, false);
307 		mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
308 	}
309 
310 	mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
311 	mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
312 
313 	if (mcasp->rxnumevt) {	/* disable FIFO */
314 		u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
315 
316 		mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
317 	}
318 }
319 
320 static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
321 {
322 	u32 val = 0;
323 
324 	/* disable IRQ sources */
325 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
326 		       mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
327 
328 	/*
329 	 * In synchronous mode keep TX clocks running if the capture stream is
330 	 * still running.
331 	 */
332 	if (mcasp_is_synchronous(mcasp) && mcasp->streams)
333 		val =  TXHCLKRST | TXCLKRST | TXFSRST;
334 	else
335 		mcasp_set_clk_pdir(mcasp, false);
336 
337 
338 	mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
339 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
340 
341 	if (mcasp->txnumevt) {	/* disable FIFO */
342 		u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
343 
344 		mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
345 	}
346 
347 	mcasp_set_axr_pdir(mcasp, false);
348 }
349 
350 static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
351 {
352 	mcasp->streams--;
353 
354 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
355 		mcasp_stop_tx(mcasp);
356 	else
357 		mcasp_stop_rx(mcasp);
358 }
359 
360 static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
361 {
362 	struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
363 	struct snd_pcm_substream *substream;
364 	u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
365 	u32 handled_mask = 0;
366 	u32 stat;
367 
368 	stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
369 	if (stat & XUNDRN & irq_mask) {
370 		dev_warn(mcasp->dev, "Transmit buffer underflow\n");
371 		handled_mask |= XUNDRN;
372 
373 		substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
374 		if (substream)
375 			snd_pcm_stop_xrun(substream);
376 	}
377 
378 	if (!handled_mask)
379 		dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
380 			 stat);
381 
382 	if (stat & XRERR)
383 		handled_mask |= XRERR;
384 
385 	/* Ack the handled event only */
386 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
387 
388 	return IRQ_RETVAL(handled_mask);
389 }
390 
391 static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
392 {
393 	struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
394 	struct snd_pcm_substream *substream;
395 	u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
396 	u32 handled_mask = 0;
397 	u32 stat;
398 
399 	stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
400 	if (stat & ROVRN & irq_mask) {
401 		dev_warn(mcasp->dev, "Receive buffer overflow\n");
402 		handled_mask |= ROVRN;
403 
404 		substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
405 		if (substream)
406 			snd_pcm_stop_xrun(substream);
407 	}
408 
409 	if (!handled_mask)
410 		dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
411 			 stat);
412 
413 	if (stat & XRERR)
414 		handled_mask |= XRERR;
415 
416 	/* Ack the handled event only */
417 	mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
418 
419 	return IRQ_RETVAL(handled_mask);
420 }
421 
422 static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
423 {
424 	struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
425 	irqreturn_t ret = IRQ_NONE;
426 
427 	if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
428 		ret = davinci_mcasp_tx_irq_handler(irq, data);
429 
430 	if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
431 		ret |= davinci_mcasp_rx_irq_handler(irq, data);
432 
433 	return ret;
434 }
435 
436 static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
437 					 unsigned int fmt)
438 {
439 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
440 	int ret = 0;
441 	u32 data_delay;
442 	bool fs_pol_rising;
443 	bool inv_fs = false;
444 
445 	if (!fmt)
446 		return 0;
447 
448 	pm_runtime_get_sync(mcasp->dev);
449 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
450 	case SND_SOC_DAIFMT_DSP_A:
451 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
452 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
453 		/* 1st data bit occur one ACLK cycle after the frame sync */
454 		data_delay = 1;
455 		break;
456 	case SND_SOC_DAIFMT_DSP_B:
457 	case SND_SOC_DAIFMT_AC97:
458 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
459 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
460 		/* No delay after FS */
461 		data_delay = 0;
462 		break;
463 	case SND_SOC_DAIFMT_I2S:
464 		/* configure a full-word SYNC pulse (LRCLK) */
465 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
466 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
467 		/* 1st data bit occur one ACLK cycle after the frame sync */
468 		data_delay = 1;
469 		/* FS need to be inverted */
470 		inv_fs = true;
471 		break;
472 	case SND_SOC_DAIFMT_RIGHT_J:
473 	case SND_SOC_DAIFMT_LEFT_J:
474 		/* configure a full-word SYNC pulse (LRCLK) */
475 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
476 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
477 		/* No delay after FS */
478 		data_delay = 0;
479 		break;
480 	default:
481 		ret = -EINVAL;
482 		goto out;
483 	}
484 
485 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
486 		       FSXDLY(3));
487 	mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
488 		       FSRDLY(3));
489 
490 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
491 	case SND_SOC_DAIFMT_CBS_CFS:
492 		/* codec is clock and frame slave */
493 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
494 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
495 
496 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
497 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
498 
499 		/* BCLK */
500 		set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
501 		set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
502 		/* Frame Sync */
503 		set_bit(PIN_BIT_AFSX, &mcasp->pdir);
504 		set_bit(PIN_BIT_AFSR, &mcasp->pdir);
505 
506 		mcasp->bclk_master = 1;
507 		break;
508 	case SND_SOC_DAIFMT_CBS_CFM:
509 		/* codec is clock slave and frame master */
510 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
511 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
512 
513 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
514 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
515 
516 		/* BCLK */
517 		set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
518 		set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
519 		/* Frame Sync */
520 		clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
521 		clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
522 
523 		mcasp->bclk_master = 1;
524 		break;
525 	case SND_SOC_DAIFMT_CBM_CFS:
526 		/* codec is clock master and frame slave */
527 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
528 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
529 
530 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
531 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
532 
533 		/* BCLK */
534 		clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
535 		clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
536 		/* Frame Sync */
537 		set_bit(PIN_BIT_AFSX, &mcasp->pdir);
538 		set_bit(PIN_BIT_AFSR, &mcasp->pdir);
539 
540 		mcasp->bclk_master = 0;
541 		break;
542 	case SND_SOC_DAIFMT_CBM_CFM:
543 		/* codec is clock and frame master */
544 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
545 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
546 
547 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
548 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
549 
550 		/* BCLK */
551 		clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
552 		clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
553 		/* Frame Sync */
554 		clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
555 		clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
556 
557 		mcasp->bclk_master = 0;
558 		break;
559 	default:
560 		ret = -EINVAL;
561 		goto out;
562 	}
563 
564 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
565 	case SND_SOC_DAIFMT_IB_NF:
566 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
567 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
568 		fs_pol_rising = true;
569 		break;
570 	case SND_SOC_DAIFMT_NB_IF:
571 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
572 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
573 		fs_pol_rising = false;
574 		break;
575 	case SND_SOC_DAIFMT_IB_IF:
576 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
577 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
578 		fs_pol_rising = false;
579 		break;
580 	case SND_SOC_DAIFMT_NB_NF:
581 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
582 		mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
583 		fs_pol_rising = true;
584 		break;
585 	default:
586 		ret = -EINVAL;
587 		goto out;
588 	}
589 
590 	if (inv_fs)
591 		fs_pol_rising = !fs_pol_rising;
592 
593 	if (fs_pol_rising) {
594 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
595 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
596 	} else {
597 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
598 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
599 	}
600 
601 	mcasp->dai_fmt = fmt;
602 out:
603 	pm_runtime_put(mcasp->dev);
604 	return ret;
605 }
606 
607 static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
608 				      int div, bool explicit)
609 {
610 	pm_runtime_get_sync(mcasp->dev);
611 	switch (div_id) {
612 	case MCASP_CLKDIV_AUXCLK:			/* MCLK divider */
613 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
614 			       AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
615 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
616 			       AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
617 		break;
618 
619 	case MCASP_CLKDIV_BCLK:			/* BCLK divider */
620 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
621 			       ACLKXDIV(div - 1), ACLKXDIV_MASK);
622 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
623 			       ACLKRDIV(div - 1), ACLKRDIV_MASK);
624 		if (explicit)
625 			mcasp->bclk_div = div;
626 		break;
627 
628 	case MCASP_CLKDIV_BCLK_FS_RATIO:
629 		/*
630 		 * BCLK/LRCLK ratio descries how many bit-clock cycles
631 		 * fit into one frame. The clock ratio is given for a
632 		 * full period of data (for I2S format both left and
633 		 * right channels), so it has to be divided by number
634 		 * of tdm-slots (for I2S - divided by 2).
635 		 * Instead of storing this ratio, we calculate a new
636 		 * tdm_slot width by dividing the the ratio by the
637 		 * number of configured tdm slots.
638 		 */
639 		mcasp->slot_width = div / mcasp->tdm_slots;
640 		if (div % mcasp->tdm_slots)
641 			dev_warn(mcasp->dev,
642 				 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
643 				 __func__, div, mcasp->tdm_slots);
644 		break;
645 
646 	default:
647 		return -EINVAL;
648 	}
649 
650 	pm_runtime_put(mcasp->dev);
651 	return 0;
652 }
653 
654 static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
655 				    int div)
656 {
657 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
658 
659 	return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
660 }
661 
662 static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
663 				    unsigned int freq, int dir)
664 {
665 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
666 
667 	pm_runtime_get_sync(mcasp->dev);
668 
669 	if (dir == SND_SOC_CLOCK_IN) {
670 		switch (clk_id) {
671 		case MCASP_CLK_HCLK_AHCLK:
672 			mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
673 				       AHCLKXE);
674 			mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
675 				       AHCLKRE);
676 			clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
677 			break;
678 		case MCASP_CLK_HCLK_AUXCLK:
679 			mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
680 				       AHCLKXE);
681 			mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
682 				       AHCLKRE);
683 			set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
684 			break;
685 		default:
686 			dev_err(mcasp->dev, "Invalid clk id: %d\n", clk_id);
687 			goto out;
688 		}
689 	} else {
690 		/* Select AUXCLK as HCLK */
691 		mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
692 		mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
693 		set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
694 	}
695 	/*
696 	 * When AHCLK X/R is selected to be output it means that the HCLK is
697 	 * the same clock - coming via AUXCLK.
698 	 */
699 	mcasp->sysclk_freq = freq;
700 out:
701 	pm_runtime_put(mcasp->dev);
702 	return 0;
703 }
704 
705 /* All serializers must have equal number of channels */
706 static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
707 				       int serializers)
708 {
709 	struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
710 	unsigned int *list = (unsigned int *) cl->list;
711 	int slots = mcasp->tdm_slots;
712 	int i, count = 0;
713 
714 	if (mcasp->tdm_mask[stream])
715 		slots = hweight32(mcasp->tdm_mask[stream]);
716 
717 	for (i = 1; i <= slots; i++)
718 		list[count++] = i;
719 
720 	for (i = 2; i <= serializers; i++)
721 		list[count++] = i*slots;
722 
723 	cl->count = count;
724 
725 	return 0;
726 }
727 
728 static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
729 {
730 	int rx_serializers = 0, tx_serializers = 0, ret, i;
731 
732 	for (i = 0; i < mcasp->num_serializer; i++)
733 		if (mcasp->serial_dir[i] == TX_MODE)
734 			tx_serializers++;
735 		else if (mcasp->serial_dir[i] == RX_MODE)
736 			rx_serializers++;
737 
738 	ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
739 					  tx_serializers);
740 	if (ret)
741 		return ret;
742 
743 	ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
744 					  rx_serializers);
745 
746 	return ret;
747 }
748 
749 
750 static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
751 				      unsigned int tx_mask,
752 				      unsigned int rx_mask,
753 				      int slots, int slot_width)
754 {
755 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
756 
757 	dev_dbg(mcasp->dev,
758 		 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
759 		 __func__, tx_mask, rx_mask, slots, slot_width);
760 
761 	if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
762 		dev_err(mcasp->dev,
763 			"Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
764 			tx_mask, rx_mask, slots);
765 		return -EINVAL;
766 	}
767 
768 	if (slot_width &&
769 	    (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
770 		dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
771 			__func__, slot_width);
772 		return -EINVAL;
773 	}
774 
775 	mcasp->tdm_slots = slots;
776 	mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
777 	mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
778 	mcasp->slot_width = slot_width;
779 
780 	return davinci_mcasp_set_ch_constraints(mcasp);
781 }
782 
783 static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
784 				       int sample_width)
785 {
786 	u32 fmt;
787 	u32 tx_rotate, rx_rotate, slot_width;
788 	u32 mask = (1ULL << sample_width) - 1;
789 
790 	if (mcasp->slot_width)
791 		slot_width = mcasp->slot_width;
792 	else if (mcasp->max_format_width)
793 		slot_width = mcasp->max_format_width;
794 	else
795 		slot_width = sample_width;
796 	/*
797 	 * TX rotation:
798 	 * right aligned formats: rotate w/ slot_width
799 	 * left aligned formats: rotate w/ sample_width
800 	 *
801 	 * RX rotation:
802 	 * right aligned formats: no rotation needed
803 	 * left aligned formats: rotate w/ (slot_width - sample_width)
804 	 */
805 	if ((mcasp->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
806 	    SND_SOC_DAIFMT_RIGHT_J) {
807 		tx_rotate = (slot_width / 4) & 0x7;
808 		rx_rotate = 0;
809 	} else {
810 		tx_rotate = (sample_width / 4) & 0x7;
811 		rx_rotate = (slot_width - sample_width) / 4;
812 	}
813 
814 	/* mapping of the XSSZ bit-field as described in the datasheet */
815 	fmt = (slot_width >> 1) - 1;
816 
817 	if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
818 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
819 			       RXSSZ(0x0F));
820 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
821 			       TXSSZ(0x0F));
822 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
823 			       TXROT(7));
824 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
825 			       RXROT(7));
826 		mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
827 	}
828 
829 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
830 
831 	return 0;
832 }
833 
834 static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
835 				 int period_words, int channels)
836 {
837 	struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
838 	int i;
839 	u8 tx_ser = 0;
840 	u8 rx_ser = 0;
841 	u8 slots = mcasp->tdm_slots;
842 	u8 max_active_serializers = (channels + slots - 1) / slots;
843 	u8 max_rx_serializers, max_tx_serializers;
844 	int active_serializers, numevt;
845 	u32 reg;
846 	/* Default configuration */
847 	if (mcasp->version < MCASP_VERSION_3)
848 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
849 
850 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
851 		mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
852 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
853 		max_tx_serializers = max_active_serializers;
854 		max_rx_serializers =
855 			mcasp->active_serializers[SNDRV_PCM_STREAM_CAPTURE];
856 	} else {
857 		mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
858 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
859 		max_tx_serializers =
860 			mcasp->active_serializers[SNDRV_PCM_STREAM_PLAYBACK];
861 		max_rx_serializers = max_active_serializers;
862 	}
863 
864 	for (i = 0; i < mcasp->num_serializer; i++) {
865 		mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
866 			       mcasp->serial_dir[i]);
867 		if (mcasp->serial_dir[i] == TX_MODE &&
868 					tx_ser < max_tx_serializers) {
869 			mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
870 				       mcasp->dismod, DISMOD_MASK);
871 			set_bit(PIN_BIT_AXR(i), &mcasp->pdir);
872 			tx_ser++;
873 		} else if (mcasp->serial_dir[i] == RX_MODE &&
874 					rx_ser < max_rx_serializers) {
875 			clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
876 			rx_ser++;
877 		} else {
878 			/* Inactive or unused pin, set it to inactive */
879 			mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
880 				       SRMOD_INACTIVE, SRMOD_MASK);
881 			/* If unused, set DISMOD for the pin */
882 			if (mcasp->serial_dir[i] != INACTIVE_MODE)
883 				mcasp_mod_bits(mcasp,
884 					       DAVINCI_MCASP_XRSRCTL_REG(i),
885 					       mcasp->dismod, DISMOD_MASK);
886 			clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
887 		}
888 	}
889 
890 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
891 		active_serializers = tx_ser;
892 		numevt = mcasp->txnumevt;
893 		reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
894 	} else {
895 		active_serializers = rx_ser;
896 		numevt = mcasp->rxnumevt;
897 		reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
898 	}
899 
900 	if (active_serializers < max_active_serializers) {
901 		dev_warn(mcasp->dev, "stream has more channels (%d) than are "
902 			 "enabled in mcasp (%d)\n", channels,
903 			 active_serializers * slots);
904 		return -EINVAL;
905 	}
906 
907 	/* AFIFO is not in use */
908 	if (!numevt) {
909 		/* Configure the burst size for platform drivers */
910 		if (active_serializers > 1) {
911 			/*
912 			 * If more than one serializers are in use we have one
913 			 * DMA request to provide data for all serializers.
914 			 * For example if three serializers are enabled the DMA
915 			 * need to transfer three words per DMA request.
916 			 */
917 			dma_data->maxburst = active_serializers;
918 		} else {
919 			dma_data->maxburst = 0;
920 		}
921 
922 		goto out;
923 	}
924 
925 	if (period_words % active_serializers) {
926 		dev_err(mcasp->dev, "Invalid combination of period words and "
927 			"active serializers: %d, %d\n", period_words,
928 			active_serializers);
929 		return -EINVAL;
930 	}
931 
932 	/*
933 	 * Calculate the optimal AFIFO depth for platform side:
934 	 * The number of words for numevt need to be in steps of active
935 	 * serializers.
936 	 */
937 	numevt = (numevt / active_serializers) * active_serializers;
938 
939 	while (period_words % numevt && numevt > 0)
940 		numevt -= active_serializers;
941 	if (numevt <= 0)
942 		numevt = active_serializers;
943 
944 	mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
945 	mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
946 
947 	/* Configure the burst size for platform drivers */
948 	if (numevt == 1)
949 		numevt = 0;
950 	dma_data->maxburst = numevt;
951 
952 out:
953 	mcasp->active_serializers[stream] = active_serializers;
954 
955 	return 0;
956 }
957 
958 static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
959 			      int channels)
960 {
961 	int i, active_slots;
962 	int total_slots;
963 	int active_serializers;
964 	u32 mask = 0;
965 	u32 busel = 0;
966 
967 	total_slots = mcasp->tdm_slots;
968 
969 	/*
970 	 * If more than one serializer is needed, then use them with
971 	 * all the specified tdm_slots. Otherwise, one serializer can
972 	 * cope with the transaction using just as many slots as there
973 	 * are channels in the stream.
974 	 */
975 	if (mcasp->tdm_mask[stream]) {
976 		active_slots = hweight32(mcasp->tdm_mask[stream]);
977 		active_serializers = (channels + active_slots - 1) /
978 			active_slots;
979 		if (active_serializers == 1)
980 			active_slots = channels;
981 		for (i = 0; i < total_slots; i++) {
982 			if ((1 << i) & mcasp->tdm_mask[stream]) {
983 				mask |= (1 << i);
984 				if (--active_slots <= 0)
985 					break;
986 			}
987 		}
988 	} else {
989 		active_serializers = (channels + total_slots - 1) / total_slots;
990 		if (active_serializers == 1)
991 			active_slots = channels;
992 		else
993 			active_slots = total_slots;
994 
995 		for (i = 0; i < active_slots; i++)
996 			mask |= (1 << i);
997 	}
998 
999 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
1000 
1001 	if (!mcasp->dat_port)
1002 		busel = TXSEL;
1003 
1004 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1005 		mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
1006 		mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
1007 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1008 			       FSXMOD(total_slots), FSXMOD(0x1FF));
1009 	} else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
1010 		mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
1011 		mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
1012 		mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
1013 			       FSRMOD(total_slots), FSRMOD(0x1FF));
1014 		/*
1015 		 * If McASP is set to be TX/RX synchronous and the playback is
1016 		 * not running already we need to configure the TX slots in
1017 		 * order to have correct FSX on the bus
1018 		 */
1019 		if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
1020 			mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
1021 				       FSXMOD(total_slots), FSXMOD(0x1FF));
1022 	}
1023 
1024 	return 0;
1025 }
1026 
1027 /* S/PDIF */
1028 static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
1029 			      unsigned int rate)
1030 {
1031 	u32 cs_value = 0;
1032 	u8 *cs_bytes = (u8*) &cs_value;
1033 
1034 	/* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1035 	   and LSB first */
1036 	mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
1037 
1038 	/* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
1039 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
1040 
1041 	/* Set the TX tdm : for all the slots */
1042 	mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
1043 
1044 	/* Set the TX clock controls : div = 1 and internal */
1045 	mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
1046 
1047 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
1048 
1049 	/* Only 44100 and 48000 are valid, both have the same setting */
1050 	mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
1051 
1052 	/* Enable the DIT */
1053 	mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
1054 
1055 	/* Set S/PDIF channel status bits */
1056 	cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
1057 	cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
1058 
1059 	switch (rate) {
1060 	case 22050:
1061 		cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
1062 		break;
1063 	case 24000:
1064 		cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
1065 		break;
1066 	case 32000:
1067 		cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
1068 		break;
1069 	case 44100:
1070 		cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
1071 		break;
1072 	case 48000:
1073 		cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
1074 		break;
1075 	case 88200:
1076 		cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
1077 		break;
1078 	case 96000:
1079 		cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
1080 		break;
1081 	case 176400:
1082 		cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
1083 		break;
1084 	case 192000:
1085 		cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
1086 		break;
1087 	default:
1088 		printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
1089 		return -EINVAL;
1090 	}
1091 
1092 	mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
1093 	mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
1094 
1095 	return 0;
1096 }
1097 
1098 static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
1099 				      unsigned int sysclk_freq,
1100 				      unsigned int bclk_freq, bool set)
1101 {
1102 	u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1103 	int div = sysclk_freq / bclk_freq;
1104 	int rem = sysclk_freq % bclk_freq;
1105 	int error_ppm;
1106 	int aux_div = 1;
1107 
1108 	if (div > (ACLKXDIV_MASK + 1)) {
1109 		if (reg & AHCLKXE) {
1110 			aux_div = div / (ACLKXDIV_MASK + 1);
1111 			if (div % (ACLKXDIV_MASK + 1))
1112 				aux_div++;
1113 
1114 			sysclk_freq /= aux_div;
1115 			div = sysclk_freq / bclk_freq;
1116 			rem = sysclk_freq % bclk_freq;
1117 		} else if (set) {
1118 			dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1119 				 sysclk_freq);
1120 		}
1121 	}
1122 
1123 	if (rem != 0) {
1124 		if (div == 0 ||
1125 		    ((sysclk_freq / div) - bclk_freq) >
1126 		    (bclk_freq - (sysclk_freq / (div+1)))) {
1127 			div++;
1128 			rem = rem - bclk_freq;
1129 		}
1130 	}
1131 	error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1132 		     (int)bclk_freq)) / div - 1000000;
1133 
1134 	if (set) {
1135 		if (error_ppm)
1136 			dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1137 				 error_ppm);
1138 
1139 		__davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
1140 		if (reg & AHCLKXE)
1141 			__davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1142 						   aux_div, 0);
1143 	}
1144 
1145 	return error_ppm;
1146 }
1147 
1148 static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp)
1149 {
1150 	if (!mcasp->txnumevt)
1151 		return 0;
1152 
1153 	return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET);
1154 }
1155 
1156 static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp)
1157 {
1158 	if (!mcasp->rxnumevt)
1159 		return 0;
1160 
1161 	return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET);
1162 }
1163 
1164 static snd_pcm_sframes_t davinci_mcasp_delay(
1165 			struct snd_pcm_substream *substream,
1166 			struct snd_soc_dai *cpu_dai)
1167 {
1168 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1169 	u32 fifo_use;
1170 
1171 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1172 		fifo_use = davinci_mcasp_tx_delay(mcasp);
1173 	else
1174 		fifo_use = davinci_mcasp_rx_delay(mcasp);
1175 
1176 	/*
1177 	 * Divide the used locations with the channel count to get the
1178 	 * FIFO usage in samples (don't care about partial samples in the
1179 	 * buffer).
1180 	 */
1181 	return fifo_use / substream->runtime->channels;
1182 }
1183 
1184 static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1185 					struct snd_pcm_hw_params *params,
1186 					struct snd_soc_dai *cpu_dai)
1187 {
1188 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1189 	int word_length;
1190 	int channels = params_channels(params);
1191 	int period_size = params_period_size(params);
1192 	int ret;
1193 
1194 	switch (params_format(params)) {
1195 	case SNDRV_PCM_FORMAT_U8:
1196 	case SNDRV_PCM_FORMAT_S8:
1197 		word_length = 8;
1198 		break;
1199 
1200 	case SNDRV_PCM_FORMAT_U16_LE:
1201 	case SNDRV_PCM_FORMAT_S16_LE:
1202 		word_length = 16;
1203 		break;
1204 
1205 	case SNDRV_PCM_FORMAT_U24_3LE:
1206 	case SNDRV_PCM_FORMAT_S24_3LE:
1207 		word_length = 24;
1208 		break;
1209 
1210 	case SNDRV_PCM_FORMAT_U24_LE:
1211 	case SNDRV_PCM_FORMAT_S24_LE:
1212 		word_length = 24;
1213 		break;
1214 
1215 	case SNDRV_PCM_FORMAT_U32_LE:
1216 	case SNDRV_PCM_FORMAT_S32_LE:
1217 		word_length = 32;
1218 		break;
1219 
1220 	default:
1221 		printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1222 		return -EINVAL;
1223 	}
1224 
1225 	ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1226 	if (ret)
1227 		return ret;
1228 
1229 	/*
1230 	 * If mcasp is BCLK master, and a BCLK divider was not provided by
1231 	 * the machine driver, we need to calculate the ratio.
1232 	 */
1233 	if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1234 		int slots = mcasp->tdm_slots;
1235 		int rate = params_rate(params);
1236 		int sbits = params_width(params);
1237 
1238 		if (mcasp->slot_width)
1239 			sbits = mcasp->slot_width;
1240 
1241 		davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq,
1242 					   rate * sbits * slots, true);
1243 	}
1244 
1245 	ret = mcasp_common_hw_param(mcasp, substream->stream,
1246 				    period_size * channels, channels);
1247 	if (ret)
1248 		return ret;
1249 
1250 	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1251 		ret = mcasp_dit_hw_param(mcasp, params_rate(params));
1252 	else
1253 		ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1254 					 channels);
1255 
1256 	if (ret)
1257 		return ret;
1258 
1259 	davinci_config_channel_size(mcasp, word_length);
1260 
1261 	if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1262 		mcasp->channels = channels;
1263 		if (!mcasp->max_format_width)
1264 			mcasp->max_format_width = word_length;
1265 	}
1266 
1267 	return 0;
1268 }
1269 
1270 static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1271 				     int cmd, struct snd_soc_dai *cpu_dai)
1272 {
1273 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1274 	int ret = 0;
1275 
1276 	switch (cmd) {
1277 	case SNDRV_PCM_TRIGGER_RESUME:
1278 	case SNDRV_PCM_TRIGGER_START:
1279 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1280 		davinci_mcasp_start(mcasp, substream->stream);
1281 		break;
1282 	case SNDRV_PCM_TRIGGER_SUSPEND:
1283 	case SNDRV_PCM_TRIGGER_STOP:
1284 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1285 		davinci_mcasp_stop(mcasp, substream->stream);
1286 		break;
1287 
1288 	default:
1289 		ret = -EINVAL;
1290 	}
1291 
1292 	return ret;
1293 }
1294 
1295 static int davinci_mcasp_hw_rule_slot_width(struct snd_pcm_hw_params *params,
1296 					    struct snd_pcm_hw_rule *rule)
1297 {
1298 	struct davinci_mcasp_ruledata *rd = rule->private;
1299 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1300 	struct snd_mask nfmt;
1301 	int i, slot_width;
1302 
1303 	snd_mask_none(&nfmt);
1304 	slot_width = rd->mcasp->slot_width;
1305 
1306 	for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1307 		if (snd_mask_test(fmt, i)) {
1308 			if (snd_pcm_format_width(i) <= slot_width) {
1309 				snd_mask_set(&nfmt, i);
1310 			}
1311 		}
1312 	}
1313 
1314 	return snd_mask_refine(fmt, &nfmt);
1315 }
1316 
1317 static int davinci_mcasp_hw_rule_format_width(struct snd_pcm_hw_params *params,
1318 					      struct snd_pcm_hw_rule *rule)
1319 {
1320 	struct davinci_mcasp_ruledata *rd = rule->private;
1321 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1322 	struct snd_mask nfmt;
1323 	int i, format_width;
1324 
1325 	snd_mask_none(&nfmt);
1326 	format_width = rd->mcasp->max_format_width;
1327 
1328 	for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1329 		if (snd_mask_test(fmt, i)) {
1330 			if (snd_pcm_format_width(i) == format_width) {
1331 				snd_mask_set(&nfmt, i);
1332 			}
1333 		}
1334 	}
1335 
1336 	return snd_mask_refine(fmt, &nfmt);
1337 }
1338 
1339 static const unsigned int davinci_mcasp_dai_rates[] = {
1340 	8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1341 	88200, 96000, 176400, 192000,
1342 };
1343 
1344 #define DAVINCI_MAX_RATE_ERROR_PPM 1000
1345 
1346 static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1347 				      struct snd_pcm_hw_rule *rule)
1348 {
1349 	struct davinci_mcasp_ruledata *rd = rule->private;
1350 	struct snd_interval *ri =
1351 		hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1352 	int sbits = params_width(params);
1353 	int slots = rd->mcasp->tdm_slots;
1354 	struct snd_interval range;
1355 	int i;
1356 
1357 	if (rd->mcasp->slot_width)
1358 		sbits = rd->mcasp->slot_width;
1359 
1360 	snd_interval_any(&range);
1361 	range.empty = 1;
1362 
1363 	for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
1364 		if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
1365 			uint bclk_freq = sbits * slots *
1366 					 davinci_mcasp_dai_rates[i];
1367 			unsigned int sysclk_freq;
1368 			int ppm;
1369 
1370 			if (rd->mcasp->auxclk_fs_ratio)
1371 				sysclk_freq =  davinci_mcasp_dai_rates[i] *
1372 					       rd->mcasp->auxclk_fs_ratio;
1373 			else
1374 				sysclk_freq = rd->mcasp->sysclk_freq;
1375 
1376 			ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1377 							 bclk_freq, false);
1378 			if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1379 				if (range.empty) {
1380 					range.min = davinci_mcasp_dai_rates[i];
1381 					range.empty = 0;
1382 				}
1383 				range.max = davinci_mcasp_dai_rates[i];
1384 			}
1385 		}
1386 	}
1387 
1388 	dev_dbg(rd->mcasp->dev,
1389 		"Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1390 		ri->min, ri->max, range.min, range.max, sbits, slots);
1391 
1392 	return snd_interval_refine(hw_param_interval(params, rule->var),
1393 				   &range);
1394 }
1395 
1396 static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1397 					struct snd_pcm_hw_rule *rule)
1398 {
1399 	struct davinci_mcasp_ruledata *rd = rule->private;
1400 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1401 	struct snd_mask nfmt;
1402 	int rate = params_rate(params);
1403 	int slots = rd->mcasp->tdm_slots;
1404 	int i, count = 0;
1405 
1406 	snd_mask_none(&nfmt);
1407 
1408 	for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
1409 		if (snd_mask_test(fmt, i)) {
1410 			uint sbits = snd_pcm_format_width(i);
1411 			unsigned int sysclk_freq;
1412 			int ppm;
1413 
1414 			if (rd->mcasp->auxclk_fs_ratio)
1415 				sysclk_freq =  rate *
1416 					       rd->mcasp->auxclk_fs_ratio;
1417 			else
1418 				sysclk_freq = rd->mcasp->sysclk_freq;
1419 
1420 			if (rd->mcasp->slot_width)
1421 				sbits = rd->mcasp->slot_width;
1422 
1423 			ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1424 							 sbits * slots * rate,
1425 							 false);
1426 			if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1427 				snd_mask_set(&nfmt, i);
1428 				count++;
1429 			}
1430 		}
1431 	}
1432 	dev_dbg(rd->mcasp->dev,
1433 		"%d possible sample format for %d Hz and %d tdm slots\n",
1434 		count, rate, slots);
1435 
1436 	return snd_mask_refine(fmt, &nfmt);
1437 }
1438 
1439 static int davinci_mcasp_hw_rule_min_periodsize(
1440 		struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1441 {
1442 	struct snd_interval *period_size = hw_param_interval(params,
1443 						SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1444 	struct snd_interval frames;
1445 
1446 	snd_interval_any(&frames);
1447 	frames.min = 64;
1448 	frames.integer = 1;
1449 
1450 	return snd_interval_refine(period_size, &frames);
1451 }
1452 
1453 static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1454 				 struct snd_soc_dai *cpu_dai)
1455 {
1456 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1457 	struct davinci_mcasp_ruledata *ruledata =
1458 					&mcasp->ruledata[substream->stream];
1459 	u32 max_channels = 0;
1460 	int i, dir, ret;
1461 	int tdm_slots = mcasp->tdm_slots;
1462 
1463 	/* Do not allow more then one stream per direction */
1464 	if (mcasp->substreams[substream->stream])
1465 		return -EBUSY;
1466 
1467 	mcasp->substreams[substream->stream] = substream;
1468 
1469 	if (mcasp->tdm_mask[substream->stream])
1470 		tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1471 
1472 	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1473 		return 0;
1474 
1475 	/*
1476 	 * Limit the maximum allowed channels for the first stream:
1477 	 * number of serializers for the direction * tdm slots per serializer
1478 	 */
1479 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1480 		dir = TX_MODE;
1481 	else
1482 		dir = RX_MODE;
1483 
1484 	for (i = 0; i < mcasp->num_serializer; i++) {
1485 		if (mcasp->serial_dir[i] == dir)
1486 			max_channels++;
1487 	}
1488 	ruledata->serializers = max_channels;
1489 	ruledata->mcasp = mcasp;
1490 	max_channels *= tdm_slots;
1491 	/*
1492 	 * If the already active stream has less channels than the calculated
1493 	 * limit based on the seirializers * tdm_slots, and only one serializer
1494 	 * is in use we need to use that as a constraint for the second stream.
1495 	 * Otherwise (first stream or less allowed channels or more than one
1496 	 * serializer in use) we use the calculated constraint.
1497 	 */
1498 	if (mcasp->channels && mcasp->channels < max_channels &&
1499 	    ruledata->serializers == 1)
1500 		max_channels = mcasp->channels;
1501 	/*
1502 	 * But we can always allow channels upto the amount of
1503 	 * the available tdm_slots.
1504 	 */
1505 	if (max_channels < tdm_slots)
1506 		max_channels = tdm_slots;
1507 
1508 	snd_pcm_hw_constraint_minmax(substream->runtime,
1509 				     SNDRV_PCM_HW_PARAM_CHANNELS,
1510 				     0, max_channels);
1511 
1512 	snd_pcm_hw_constraint_list(substream->runtime,
1513 				   0, SNDRV_PCM_HW_PARAM_CHANNELS,
1514 				   &mcasp->chconstr[substream->stream]);
1515 
1516 	if (mcasp->max_format_width) {
1517 		/*
1518 		 * Only allow formats which require same amount of bits on the
1519 		 * bus as the currently running stream
1520 		 */
1521 		ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1522 					  SNDRV_PCM_HW_PARAM_FORMAT,
1523 					  davinci_mcasp_hw_rule_format_width,
1524 					  ruledata,
1525 					  SNDRV_PCM_HW_PARAM_FORMAT, -1);
1526 		if (ret)
1527 			return ret;
1528 	}
1529 	else if (mcasp->slot_width) {
1530 		/* Only allow formats require <= slot_width bits on the bus */
1531 		ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1532 					  SNDRV_PCM_HW_PARAM_FORMAT,
1533 					  davinci_mcasp_hw_rule_slot_width,
1534 					  ruledata,
1535 					  SNDRV_PCM_HW_PARAM_FORMAT, -1);
1536 		if (ret)
1537 			return ret;
1538 	}
1539 
1540 	/*
1541 	 * If we rely on implicit BCLK divider setting we should
1542 	 * set constraints based on what we can provide.
1543 	 */
1544 	if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1545 		ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1546 					  SNDRV_PCM_HW_PARAM_RATE,
1547 					  davinci_mcasp_hw_rule_rate,
1548 					  ruledata,
1549 					  SNDRV_PCM_HW_PARAM_FORMAT, -1);
1550 		if (ret)
1551 			return ret;
1552 		ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1553 					  SNDRV_PCM_HW_PARAM_FORMAT,
1554 					  davinci_mcasp_hw_rule_format,
1555 					  ruledata,
1556 					  SNDRV_PCM_HW_PARAM_RATE, -1);
1557 		if (ret)
1558 			return ret;
1559 	}
1560 
1561 	snd_pcm_hw_rule_add(substream->runtime, 0,
1562 			    SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1563 			    davinci_mcasp_hw_rule_min_periodsize, NULL,
1564 			    SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1565 
1566 	return 0;
1567 }
1568 
1569 static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1570 				   struct snd_soc_dai *cpu_dai)
1571 {
1572 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1573 
1574 	mcasp->substreams[substream->stream] = NULL;
1575 	mcasp->active_serializers[substream->stream] = 0;
1576 
1577 	if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1578 		return;
1579 
1580 	if (!cpu_dai->active) {
1581 		mcasp->channels = 0;
1582 		mcasp->max_format_width = 0;
1583 	}
1584 }
1585 
1586 static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
1587 	.startup	= davinci_mcasp_startup,
1588 	.shutdown	= davinci_mcasp_shutdown,
1589 	.trigger	= davinci_mcasp_trigger,
1590 	.delay		= davinci_mcasp_delay,
1591 	.hw_params	= davinci_mcasp_hw_params,
1592 	.set_fmt	= davinci_mcasp_set_dai_fmt,
1593 	.set_clkdiv	= davinci_mcasp_set_clkdiv,
1594 	.set_sysclk	= davinci_mcasp_set_sysclk,
1595 	.set_tdm_slot	= davinci_mcasp_set_tdm_slot,
1596 };
1597 
1598 static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1599 {
1600 	struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1601 
1602 	dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1603 	dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
1604 
1605 	return 0;
1606 }
1607 
1608 #define DAVINCI_MCASP_RATES	SNDRV_PCM_RATE_8000_192000
1609 
1610 #define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1611 				SNDRV_PCM_FMTBIT_U8 | \
1612 				SNDRV_PCM_FMTBIT_S16_LE | \
1613 				SNDRV_PCM_FMTBIT_U16_LE | \
1614 				SNDRV_PCM_FMTBIT_S24_LE | \
1615 				SNDRV_PCM_FMTBIT_U24_LE | \
1616 				SNDRV_PCM_FMTBIT_S24_3LE | \
1617 				SNDRV_PCM_FMTBIT_U24_3LE | \
1618 				SNDRV_PCM_FMTBIT_S32_LE | \
1619 				SNDRV_PCM_FMTBIT_U32_LE)
1620 
1621 static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
1622 	{
1623 		.name		= "davinci-mcasp.0",
1624 		.probe		= davinci_mcasp_dai_probe,
1625 		.playback	= {
1626 			.channels_min	= 1,
1627 			.channels_max	= 32 * 16,
1628 			.rates 		= DAVINCI_MCASP_RATES,
1629 			.formats	= DAVINCI_MCASP_PCM_FMTS,
1630 		},
1631 		.capture 	= {
1632 			.channels_min 	= 1,
1633 			.channels_max	= 32 * 16,
1634 			.rates 		= DAVINCI_MCASP_RATES,
1635 			.formats	= DAVINCI_MCASP_PCM_FMTS,
1636 		},
1637 		.ops 		= &davinci_mcasp_dai_ops,
1638 
1639 		.symmetric_rates	= 1,
1640 	},
1641 	{
1642 		.name		= "davinci-mcasp.1",
1643 		.probe		= davinci_mcasp_dai_probe,
1644 		.playback 	= {
1645 			.channels_min	= 1,
1646 			.channels_max	= 384,
1647 			.rates		= DAVINCI_MCASP_RATES,
1648 			.formats	= DAVINCI_MCASP_PCM_FMTS,
1649 		},
1650 		.ops 		= &davinci_mcasp_dai_ops,
1651 	},
1652 
1653 };
1654 
1655 static const struct snd_soc_component_driver davinci_mcasp_component = {
1656 	.name		= "davinci-mcasp",
1657 };
1658 
1659 /* Some HW specific values and defaults. The rest is filled in from DT. */
1660 static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
1661 	.tx_dma_offset = 0x400,
1662 	.rx_dma_offset = 0x400,
1663 	.version = MCASP_VERSION_1,
1664 };
1665 
1666 static struct davinci_mcasp_pdata da830_mcasp_pdata = {
1667 	.tx_dma_offset = 0x2000,
1668 	.rx_dma_offset = 0x2000,
1669 	.version = MCASP_VERSION_2,
1670 };
1671 
1672 static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
1673 	.tx_dma_offset = 0,
1674 	.rx_dma_offset = 0,
1675 	.version = MCASP_VERSION_3,
1676 };
1677 
1678 static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
1679 	/* The CFG port offset will be calculated if it is needed */
1680 	.tx_dma_offset = 0,
1681 	.rx_dma_offset = 0,
1682 	.version = MCASP_VERSION_4,
1683 };
1684 
1685 static const struct of_device_id mcasp_dt_ids[] = {
1686 	{
1687 		.compatible = "ti,dm646x-mcasp-audio",
1688 		.data = &dm646x_mcasp_pdata,
1689 	},
1690 	{
1691 		.compatible = "ti,da830-mcasp-audio",
1692 		.data = &da830_mcasp_pdata,
1693 	},
1694 	{
1695 		.compatible = "ti,am33xx-mcasp-audio",
1696 		.data = &am33xx_mcasp_pdata,
1697 	},
1698 	{
1699 		.compatible = "ti,dra7-mcasp-audio",
1700 		.data = &dra7_mcasp_pdata,
1701 	},
1702 	{ /* sentinel */ }
1703 };
1704 MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1705 
1706 static int mcasp_reparent_fck(struct platform_device *pdev)
1707 {
1708 	struct device_node *node = pdev->dev.of_node;
1709 	struct clk *gfclk, *parent_clk;
1710 	const char *parent_name;
1711 	int ret;
1712 
1713 	if (!node)
1714 		return 0;
1715 
1716 	parent_name = of_get_property(node, "fck_parent", NULL);
1717 	if (!parent_name)
1718 		return 0;
1719 
1720 	dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1721 
1722 	gfclk = clk_get(&pdev->dev, "fck");
1723 	if (IS_ERR(gfclk)) {
1724 		dev_err(&pdev->dev, "failed to get fck\n");
1725 		return PTR_ERR(gfclk);
1726 	}
1727 
1728 	parent_clk = clk_get(NULL, parent_name);
1729 	if (IS_ERR(parent_clk)) {
1730 		dev_err(&pdev->dev, "failed to get parent clock\n");
1731 		ret = PTR_ERR(parent_clk);
1732 		goto err1;
1733 	}
1734 
1735 	ret = clk_set_parent(gfclk, parent_clk);
1736 	if (ret) {
1737 		dev_err(&pdev->dev, "failed to reparent fck\n");
1738 		goto err2;
1739 	}
1740 
1741 err2:
1742 	clk_put(parent_clk);
1743 err1:
1744 	clk_put(gfclk);
1745 	return ret;
1746 }
1747 
1748 static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
1749 						struct platform_device *pdev)
1750 {
1751 	struct device_node *np = pdev->dev.of_node;
1752 	struct davinci_mcasp_pdata *pdata = NULL;
1753 	const struct of_device_id *match =
1754 			of_match_device(mcasp_dt_ids, &pdev->dev);
1755 	struct of_phandle_args dma_spec;
1756 
1757 	const u32 *of_serial_dir32;
1758 	u32 val;
1759 	int i, ret = 0;
1760 
1761 	if (pdev->dev.platform_data) {
1762 		pdata = pdev->dev.platform_data;
1763 		pdata->dismod = DISMOD_LOW;
1764 		return pdata;
1765 	} else if (match) {
1766 		pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1767 				     GFP_KERNEL);
1768 		if (!pdata)
1769 			return NULL;
1770 	} else {
1771 		/* control shouldn't reach here. something is wrong */
1772 		ret = -EINVAL;
1773 		goto nodata;
1774 	}
1775 
1776 	ret = of_property_read_u32(np, "op-mode", &val);
1777 	if (ret >= 0)
1778 		pdata->op_mode = val;
1779 
1780 	ret = of_property_read_u32(np, "tdm-slots", &val);
1781 	if (ret >= 0) {
1782 		if (val < 2 || val > 32) {
1783 			dev_err(&pdev->dev,
1784 				"tdm-slots must be in rage [2-32]\n");
1785 			ret = -EINVAL;
1786 			goto nodata;
1787 		}
1788 
1789 		pdata->tdm_slots = val;
1790 	}
1791 
1792 	of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1793 	val /= sizeof(u32);
1794 	if (of_serial_dir32) {
1795 		u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1796 						 (sizeof(*of_serial_dir) * val),
1797 						 GFP_KERNEL);
1798 		if (!of_serial_dir) {
1799 			ret = -ENOMEM;
1800 			goto nodata;
1801 		}
1802 
1803 		for (i = 0; i < val; i++)
1804 			of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1805 
1806 		pdata->num_serializer = val;
1807 		pdata->serial_dir = of_serial_dir;
1808 	}
1809 
1810 	ret = of_property_match_string(np, "dma-names", "tx");
1811 	if (ret < 0)
1812 		goto nodata;
1813 
1814 	ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1815 					 &dma_spec);
1816 	if (ret < 0)
1817 		goto nodata;
1818 
1819 	pdata->tx_dma_channel = dma_spec.args[0];
1820 
1821 	/* RX is not valid in DIT mode */
1822 	if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1823 		ret = of_property_match_string(np, "dma-names", "rx");
1824 		if (ret < 0)
1825 			goto nodata;
1826 
1827 		ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1828 						 &dma_spec);
1829 		if (ret < 0)
1830 			goto nodata;
1831 
1832 		pdata->rx_dma_channel = dma_spec.args[0];
1833 	}
1834 
1835 	ret = of_property_read_u32(np, "tx-num-evt", &val);
1836 	if (ret >= 0)
1837 		pdata->txnumevt = val;
1838 
1839 	ret = of_property_read_u32(np, "rx-num-evt", &val);
1840 	if (ret >= 0)
1841 		pdata->rxnumevt = val;
1842 
1843 	ret = of_property_read_u32(np, "sram-size-playback", &val);
1844 	if (ret >= 0)
1845 		pdata->sram_size_playback = val;
1846 
1847 	ret = of_property_read_u32(np, "sram-size-capture", &val);
1848 	if (ret >= 0)
1849 		pdata->sram_size_capture = val;
1850 
1851 	ret = of_property_read_u32(np, "dismod", &val);
1852 	if (ret >= 0) {
1853 		if (val == 0 || val == 2 || val == 3) {
1854 			pdata->dismod = DISMOD_VAL(val);
1855 		} else {
1856 			dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val);
1857 			pdata->dismod = DISMOD_LOW;
1858 		}
1859 	} else {
1860 		pdata->dismod = DISMOD_LOW;
1861 	}
1862 
1863 	return  pdata;
1864 
1865 nodata:
1866 	if (ret < 0) {
1867 		dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1868 			ret);
1869 		pdata = NULL;
1870 	}
1871 	return  pdata;
1872 }
1873 
1874 enum {
1875 	PCM_EDMA,
1876 	PCM_SDMA,
1877 	PCM_UDMA,
1878 };
1879 static const char *sdma_prefix = "ti,omap";
1880 
1881 static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1882 {
1883 	struct dma_chan *chan;
1884 	const char *tmp;
1885 	int ret = PCM_EDMA;
1886 
1887 	if (!mcasp->dev->of_node)
1888 		return PCM_EDMA;
1889 
1890 	tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
1891 	chan = dma_request_chan(mcasp->dev, tmp);
1892 	if (IS_ERR(chan)) {
1893 		if (PTR_ERR(chan) != -EPROBE_DEFER)
1894 			dev_err(mcasp->dev,
1895 				"Can't verify DMA configuration (%ld)\n",
1896 				PTR_ERR(chan));
1897 		return PTR_ERR(chan);
1898 	}
1899 	if (WARN_ON(!chan->device || !chan->device->dev))
1900 		return -EINVAL;
1901 
1902 	if (chan->device->dev->of_node)
1903 		ret = of_property_read_string(chan->device->dev->of_node,
1904 					      "compatible", &tmp);
1905 	else
1906 		dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1907 
1908 	dma_release_channel(chan);
1909 	if (ret)
1910 		return ret;
1911 
1912 	dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1913 	if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1914 		return PCM_SDMA;
1915 	else if (strstr(tmp, "udmap"))
1916 		return PCM_UDMA;
1917 
1918 	return PCM_EDMA;
1919 }
1920 
1921 static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1922 {
1923 	int i;
1924 	u32 offset = 0;
1925 
1926 	if (pdata->version != MCASP_VERSION_4)
1927 		return pdata->tx_dma_offset;
1928 
1929 	for (i = 0; i < pdata->num_serializer; i++) {
1930 		if (pdata->serial_dir[i] == TX_MODE) {
1931 			if (!offset) {
1932 				offset = DAVINCI_MCASP_TXBUF_REG(i);
1933 			} else {
1934 				pr_err("%s: Only one serializer allowed!\n",
1935 				       __func__);
1936 				break;
1937 			}
1938 		}
1939 	}
1940 
1941 	return offset;
1942 }
1943 
1944 static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1945 {
1946 	int i;
1947 	u32 offset = 0;
1948 
1949 	if (pdata->version != MCASP_VERSION_4)
1950 		return pdata->rx_dma_offset;
1951 
1952 	for (i = 0; i < pdata->num_serializer; i++) {
1953 		if (pdata->serial_dir[i] == RX_MODE) {
1954 			if (!offset) {
1955 				offset = DAVINCI_MCASP_RXBUF_REG(i);
1956 			} else {
1957 				pr_err("%s: Only one serializer allowed!\n",
1958 				       __func__);
1959 				break;
1960 			}
1961 		}
1962 	}
1963 
1964 	return offset;
1965 }
1966 
1967 #ifdef CONFIG_GPIOLIB
1968 static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset)
1969 {
1970 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1971 
1972 	if (mcasp->num_serializer && offset < mcasp->num_serializer &&
1973 	    mcasp->serial_dir[offset] != INACTIVE_MODE) {
1974 		dev_err(mcasp->dev, "AXR%u pin is  used for audio\n", offset);
1975 		return -EBUSY;
1976 	}
1977 
1978 	/* Do not change the PIN yet */
1979 
1980 	return pm_runtime_get_sync(mcasp->dev);
1981 }
1982 
1983 static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset)
1984 {
1985 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1986 
1987 	/* Set the direction to input */
1988 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
1989 
1990 	/* Set the pin as McASP pin */
1991 	mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
1992 
1993 	pm_runtime_put_sync(mcasp->dev);
1994 }
1995 
1996 static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip,
1997 					    unsigned offset, int value)
1998 {
1999 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2000 	u32 val;
2001 
2002 	if (value)
2003 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2004 	else
2005 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2006 
2007 	val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2008 	if (!(val & BIT(offset))) {
2009 		/* Set the pin as GPIO pin */
2010 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2011 
2012 		/* Set the direction to output */
2013 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset,
2020 				  int value)
2021 {
2022 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2023 
2024 	if (value)
2025 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2026 	else
2027 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
2028 }
2029 
2030 static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip,
2031 					   unsigned offset)
2032 {
2033 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2034 	u32 val;
2035 
2036 	val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
2037 	if (!(val & BIT(offset))) {
2038 		/* Set the direction to input */
2039 		mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
2040 
2041 		/* Set the pin as GPIO pin */
2042 		mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
2043 	}
2044 
2045 	return 0;
2046 }
2047 
2048 static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset)
2049 {
2050 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2051 	u32 val;
2052 
2053 	val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG);
2054 	if (val & BIT(offset))
2055 		return 1;
2056 
2057 	return 0;
2058 }
2059 
2060 static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip,
2061 					    unsigned offset)
2062 {
2063 	struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
2064 	u32 val;
2065 
2066 	val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG);
2067 	if (val & BIT(offset))
2068 		return 0;
2069 
2070 	return 1;
2071 }
2072 
2073 static const struct gpio_chip davinci_mcasp_template_chip = {
2074 	.owner			= THIS_MODULE,
2075 	.request		= davinci_mcasp_gpio_request,
2076 	.free			= davinci_mcasp_gpio_free,
2077 	.direction_output	= davinci_mcasp_gpio_direction_out,
2078 	.set			= davinci_mcasp_gpio_set,
2079 	.direction_input	= davinci_mcasp_gpio_direction_in,
2080 	.get			= davinci_mcasp_gpio_get,
2081 	.get_direction		= davinci_mcasp_gpio_get_direction,
2082 	.base			= -1,
2083 	.ngpio			= 32,
2084 };
2085 
2086 static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2087 {
2088 	if (!of_property_read_bool(mcasp->dev->of_node, "gpio-controller"))
2089 		return 0;
2090 
2091 	mcasp->gpio_chip = davinci_mcasp_template_chip;
2092 	mcasp->gpio_chip.label = dev_name(mcasp->dev);
2093 	mcasp->gpio_chip.parent = mcasp->dev;
2094 #ifdef CONFIG_OF_GPIO
2095 	mcasp->gpio_chip.of_node = mcasp->dev->of_node;
2096 #endif
2097 
2098 	return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp);
2099 }
2100 
2101 #else /* CONFIG_GPIOLIB */
2102 static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2103 {
2104 	return 0;
2105 }
2106 #endif /* CONFIG_GPIOLIB */
2107 
2108 static int davinci_mcasp_get_dt_params(struct davinci_mcasp *mcasp)
2109 {
2110 	struct device_node *np = mcasp->dev->of_node;
2111 	int ret;
2112 	u32 val;
2113 
2114 	if (!np)
2115 		return 0;
2116 
2117 	ret = of_property_read_u32(np, "auxclk-fs-ratio", &val);
2118 	if (ret >= 0)
2119 		mcasp->auxclk_fs_ratio = val;
2120 
2121 	return 0;
2122 }
2123 
2124 static int davinci_mcasp_probe(struct platform_device *pdev)
2125 {
2126 	struct snd_dmaengine_dai_dma_data *dma_data;
2127 	struct resource *mem, *res, *dat;
2128 	struct davinci_mcasp_pdata *pdata;
2129 	struct davinci_mcasp *mcasp;
2130 	char *irq_name;
2131 	int *dma;
2132 	int irq;
2133 	int ret;
2134 
2135 	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
2136 		dev_err(&pdev->dev, "No platform data supplied\n");
2137 		return -EINVAL;
2138 	}
2139 
2140 	mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
2141 			   GFP_KERNEL);
2142 	if (!mcasp)
2143 		return	-ENOMEM;
2144 
2145 	pdata = davinci_mcasp_set_pdata_from_of(pdev);
2146 	if (!pdata) {
2147 		dev_err(&pdev->dev, "no platform data\n");
2148 		return -EINVAL;
2149 	}
2150 
2151 	mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
2152 	if (!mem) {
2153 		dev_warn(mcasp->dev,
2154 			 "\"mpu\" mem resource not found, using index 0\n");
2155 		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2156 		if (!mem) {
2157 			dev_err(&pdev->dev, "no mem resource?\n");
2158 			return -ENODEV;
2159 		}
2160 	}
2161 
2162 	mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
2163 	if (IS_ERR(mcasp->base))
2164 		return PTR_ERR(mcasp->base);
2165 
2166 	pm_runtime_enable(&pdev->dev);
2167 
2168 	mcasp->op_mode = pdata->op_mode;
2169 	/* sanity check for tdm slots parameter */
2170 	if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
2171 		if (pdata->tdm_slots < 2) {
2172 			dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2173 				pdata->tdm_slots);
2174 			mcasp->tdm_slots = 2;
2175 		} else if (pdata->tdm_slots > 32) {
2176 			dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2177 				pdata->tdm_slots);
2178 			mcasp->tdm_slots = 32;
2179 		} else {
2180 			mcasp->tdm_slots = pdata->tdm_slots;
2181 		}
2182 	}
2183 
2184 	mcasp->num_serializer = pdata->num_serializer;
2185 #ifdef CONFIG_PM
2186 	mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
2187 					mcasp->num_serializer, sizeof(u32),
2188 					GFP_KERNEL);
2189 	if (!mcasp->context.xrsr_regs) {
2190 		ret = -ENOMEM;
2191 		goto err;
2192 	}
2193 #endif
2194 	mcasp->serial_dir = pdata->serial_dir;
2195 	mcasp->version = pdata->version;
2196 	mcasp->txnumevt = pdata->txnumevt;
2197 	mcasp->rxnumevt = pdata->rxnumevt;
2198 	mcasp->dismod = pdata->dismod;
2199 
2200 	mcasp->dev = &pdev->dev;
2201 
2202 	irq = platform_get_irq_byname(pdev, "common");
2203 	if (irq >= 0) {
2204 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
2205 					  dev_name(&pdev->dev));
2206 		if (!irq_name) {
2207 			ret = -ENOMEM;
2208 			goto err;
2209 		}
2210 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2211 						davinci_mcasp_common_irq_handler,
2212 						IRQF_ONESHOT | IRQF_SHARED,
2213 						irq_name, mcasp);
2214 		if (ret) {
2215 			dev_err(&pdev->dev, "common IRQ request failed\n");
2216 			goto err;
2217 		}
2218 
2219 		mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2220 		mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2221 	}
2222 
2223 	irq = platform_get_irq_byname(pdev, "rx");
2224 	if (irq >= 0) {
2225 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
2226 					  dev_name(&pdev->dev));
2227 		if (!irq_name) {
2228 			ret = -ENOMEM;
2229 			goto err;
2230 		}
2231 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2232 						davinci_mcasp_rx_irq_handler,
2233 						IRQF_ONESHOT, irq_name, mcasp);
2234 		if (ret) {
2235 			dev_err(&pdev->dev, "RX IRQ request failed\n");
2236 			goto err;
2237 		}
2238 
2239 		mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2240 	}
2241 
2242 	irq = platform_get_irq_byname(pdev, "tx");
2243 	if (irq >= 0) {
2244 		irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
2245 					  dev_name(&pdev->dev));
2246 		if (!irq_name) {
2247 			ret = -ENOMEM;
2248 			goto err;
2249 		}
2250 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2251 						davinci_mcasp_tx_irq_handler,
2252 						IRQF_ONESHOT, irq_name, mcasp);
2253 		if (ret) {
2254 			dev_err(&pdev->dev, "TX IRQ request failed\n");
2255 			goto err;
2256 		}
2257 
2258 		mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2259 	}
2260 
2261 	dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
2262 	if (dat)
2263 		mcasp->dat_port = true;
2264 
2265 	dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
2266 	if (dat)
2267 		dma_data->addr = dat->start;
2268 	else
2269 		dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata);
2270 
2271 	dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
2272 	res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
2273 	if (res)
2274 		*dma = res->start;
2275 	else
2276 		*dma = pdata->tx_dma_channel;
2277 
2278 	/* dmaengine filter data for DT and non-DT boot */
2279 	if (pdev->dev.of_node)
2280 		dma_data->filter_data = "tx";
2281 	else
2282 		dma_data->filter_data = dma;
2283 
2284 	/* RX is not valid in DIT mode */
2285 	if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
2286 		dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
2287 		if (dat)
2288 			dma_data->addr = dat->start;
2289 		else
2290 			dma_data->addr =
2291 				mem->start + davinci_mcasp_rxdma_offset(pdata);
2292 
2293 		dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
2294 		res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
2295 		if (res)
2296 			*dma = res->start;
2297 		else
2298 			*dma = pdata->rx_dma_channel;
2299 
2300 		/* dmaengine filter data for DT and non-DT boot */
2301 		if (pdev->dev.of_node)
2302 			dma_data->filter_data = "rx";
2303 		else
2304 			dma_data->filter_data = dma;
2305 	}
2306 
2307 	if (mcasp->version < MCASP_VERSION_3) {
2308 		mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
2309 		/* dma_params->dma_addr is pointing to the data port address */
2310 		mcasp->dat_port = true;
2311 	} else {
2312 		mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2313 	}
2314 
2315 	/* Allocate memory for long enough list for all possible
2316 	 * scenarios. Maximum number tdm slots is 32 and there cannot
2317 	 * be more serializers than given in the configuration.  The
2318 	 * serializer directions could be taken into account, but it
2319 	 * would make code much more complex and save only couple of
2320 	 * bytes.
2321 	 */
2322 	mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
2323 		devm_kcalloc(mcasp->dev,
2324 			     32 + mcasp->num_serializer - 1,
2325 			     sizeof(unsigned int),
2326 			     GFP_KERNEL);
2327 
2328 	mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
2329 		devm_kcalloc(mcasp->dev,
2330 			     32 + mcasp->num_serializer - 1,
2331 			     sizeof(unsigned int),
2332 			     GFP_KERNEL);
2333 
2334 	if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
2335 	    !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2336 		ret = -ENOMEM;
2337 		goto err;
2338 	}
2339 
2340 	ret = davinci_mcasp_set_ch_constraints(mcasp);
2341 	if (ret)
2342 		goto err;
2343 
2344 	dev_set_drvdata(&pdev->dev, mcasp);
2345 
2346 	mcasp_reparent_fck(pdev);
2347 
2348 	/* All PINS as McASP */
2349 	pm_runtime_get_sync(mcasp->dev);
2350 	mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
2351 	pm_runtime_put(mcasp->dev);
2352 
2353 	ret = davinci_mcasp_init_gpiochip(mcasp);
2354 	if (ret)
2355 		goto err;
2356 
2357 	ret = davinci_mcasp_get_dt_params(mcasp);
2358 	if (ret)
2359 		return -EINVAL;
2360 
2361 	ret = devm_snd_soc_register_component(&pdev->dev,
2362 					&davinci_mcasp_component,
2363 					&davinci_mcasp_dai[pdata->op_mode], 1);
2364 
2365 	if (ret != 0)
2366 		goto err;
2367 
2368 	ret = davinci_mcasp_get_dma_type(mcasp);
2369 	switch (ret) {
2370 	case PCM_EDMA:
2371 		ret = edma_pcm_platform_register(&pdev->dev);
2372 		break;
2373 	case PCM_SDMA:
2374 		ret = sdma_pcm_platform_register(&pdev->dev, "tx", "rx");
2375 		break;
2376 	case PCM_UDMA:
2377 		ret = udma_pcm_platform_register(&pdev->dev);
2378 		break;
2379 	default:
2380 		dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2381 	case -EPROBE_DEFER:
2382 		goto err;
2383 		break;
2384 	}
2385 
2386 	if (ret) {
2387 		dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
2388 		goto err;
2389 	}
2390 
2391 	return 0;
2392 
2393 err:
2394 	pm_runtime_disable(&pdev->dev);
2395 	return ret;
2396 }
2397 
2398 static int davinci_mcasp_remove(struct platform_device *pdev)
2399 {
2400 	pm_runtime_disable(&pdev->dev);
2401 
2402 	return 0;
2403 }
2404 
2405 #ifdef CONFIG_PM
2406 static int davinci_mcasp_runtime_suspend(struct device *dev)
2407 {
2408 	struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2409 	struct davinci_mcasp_context *context = &mcasp->context;
2410 	u32 reg;
2411 	int i;
2412 
2413 	for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2414 		context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
2415 
2416 	if (mcasp->txnumevt) {
2417 		reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2418 		context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
2419 	}
2420 	if (mcasp->rxnumevt) {
2421 		reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2422 		context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
2423 	}
2424 
2425 	for (i = 0; i < mcasp->num_serializer; i++)
2426 		context->xrsr_regs[i] = mcasp_get_reg(mcasp,
2427 						DAVINCI_MCASP_XRSRCTL_REG(i));
2428 
2429 	return 0;
2430 }
2431 
2432 static int davinci_mcasp_runtime_resume(struct device *dev)
2433 {
2434 	struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2435 	struct davinci_mcasp_context *context = &mcasp->context;
2436 	u32 reg;
2437 	int i;
2438 
2439 	for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2440 		mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
2441 
2442 	if (mcasp->txnumevt) {
2443 		reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2444 		mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
2445 	}
2446 	if (mcasp->rxnumevt) {
2447 		reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2448 		mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
2449 	}
2450 
2451 	for (i = 0; i < mcasp->num_serializer; i++)
2452 		mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
2453 			      context->xrsr_regs[i]);
2454 
2455 	return 0;
2456 }
2457 
2458 #endif
2459 
2460 static const struct dev_pm_ops davinci_mcasp_pm_ops = {
2461 	SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend,
2462 			   davinci_mcasp_runtime_resume,
2463 			   NULL)
2464 };
2465 
2466 static struct platform_driver davinci_mcasp_driver = {
2467 	.probe		= davinci_mcasp_probe,
2468 	.remove		= davinci_mcasp_remove,
2469 	.driver		= {
2470 		.name	= "davinci-mcasp",
2471 		.pm     = &davinci_mcasp_pm_ops,
2472 		.of_match_table = mcasp_dt_ids,
2473 	},
2474 };
2475 
2476 module_platform_driver(davinci_mcasp_driver);
2477 
2478 MODULE_AUTHOR("Steve Chen");
2479 MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2480 MODULE_LICENSE("GPL");
2481