xref: /openbmc/linux/sound/soc/fsl/fsl_ssi.c (revision 4cff79e9)
1 /*
2  * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
3  *
4  * Author: Timur Tabi <timur@freescale.com>
5  *
6  * Copyright 2007-2010 Freescale Semiconductor, Inc.
7  *
8  * This file is licensed under the terms of the GNU General Public License
9  * version 2.  This program is licensed "as is" without any warranty of any
10  * kind, whether express or implied.
11  *
12  *
13  * Some notes why imx-pcm-fiq is used instead of DMA on some boards:
14  *
15  * The i.MX SSI core has some nasty limitations in AC97 mode. While most
16  * sane processor vendors have a FIFO per AC97 slot, the i.MX has only
17  * one FIFO which combines all valid receive slots. We cannot even select
18  * which slots we want to receive. The WM9712 with which this driver
19  * was developed with always sends GPIO status data in slot 12 which
20  * we receive in our (PCM-) data stream. The only chance we have is to
21  * manually skip this data in the FIQ handler. With sampling rates different
22  * from 48000Hz not every frame has valid receive data, so the ratio
23  * between pcm data and GPIO status data changes. Our FIQ handler is not
24  * able to handle this, hence this driver only works with 48000Hz sampling
25  * rate.
26  * Reading and writing AC97 registers is another challenge. The core
27  * provides us status bits when the read register is updated with *another*
28  * value. When we read the same register two times (and the register still
29  * contains the same value) these status bits are not set. We work
30  * around this by not polling these bits but only wait a fixed delay.
31  */
32 
33 #include <linux/init.h>
34 #include <linux/io.h>
35 #include <linux/module.h>
36 #include <linux/interrupt.h>
37 #include <linux/clk.h>
38 #include <linux/ctype.h>
39 #include <linux/device.h>
40 #include <linux/delay.h>
41 #include <linux/mutex.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/of.h>
45 #include <linux/of_address.h>
46 #include <linux/of_irq.h>
47 #include <linux/of_platform.h>
48 
49 #include <sound/core.h>
50 #include <sound/pcm.h>
51 #include <sound/pcm_params.h>
52 #include <sound/initval.h>
53 #include <sound/soc.h>
54 #include <sound/dmaengine_pcm.h>
55 
56 #include "fsl_ssi.h"
57 #include "imx-pcm.h"
58 
59 /* Define RX and TX to index ssi->regvals array; Can be 0 or 1 only */
60 #define RX 0
61 #define TX 1
62 
63 /**
64  * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
65  *
66  * The SSI has a limitation in that the samples must be in the same byte
67  * order as the host CPU.  This is because when multiple bytes are written
68  * to the STX register, the bytes and bits must be written in the same
69  * order.  The STX is a shift register, so all the bits need to be aligned
70  * (bit-endianness must match byte-endianness).  Processors typically write
71  * the bits within a byte in the same order that the bytes of a word are
72  * written in.  So if the host CPU is big-endian, then only big-endian
73  * samples will be written to STX properly.
74  */
75 #ifdef __BIG_ENDIAN
76 #define FSLSSI_I2S_FORMATS \
77 	(SNDRV_PCM_FMTBIT_S8 | \
78 	 SNDRV_PCM_FMTBIT_S16_BE | \
79 	 SNDRV_PCM_FMTBIT_S18_3BE | \
80 	 SNDRV_PCM_FMTBIT_S20_3BE | \
81 	 SNDRV_PCM_FMTBIT_S24_3BE | \
82 	 SNDRV_PCM_FMTBIT_S24_BE)
83 #else
84 #define FSLSSI_I2S_FORMATS \
85 	(SNDRV_PCM_FMTBIT_S8 | \
86 	 SNDRV_PCM_FMTBIT_S16_LE | \
87 	 SNDRV_PCM_FMTBIT_S18_3LE | \
88 	 SNDRV_PCM_FMTBIT_S20_3LE | \
89 	 SNDRV_PCM_FMTBIT_S24_3LE | \
90 	 SNDRV_PCM_FMTBIT_S24_LE)
91 #endif
92 
93 /*
94  * In AC97 mode, TXDIR bit is forced to 0 and TFDIR bit is forced to 1:
95  *  - SSI inputs external bit clock and outputs frame sync clock -- CBM_CFS
96  *  - Also have NB_NF to mark these two clocks will not be inverted
97  */
98 #define FSLSSI_AC97_DAIFMT \
99 	(SND_SOC_DAIFMT_AC97 | \
100 	 SND_SOC_DAIFMT_CBM_CFS | \
101 	 SND_SOC_DAIFMT_NB_NF)
102 
103 #define FSLSSI_SIER_DBG_RX_FLAGS \
104 	(SSI_SIER_RFF0_EN | \
105 	 SSI_SIER_RLS_EN | \
106 	 SSI_SIER_RFS_EN | \
107 	 SSI_SIER_ROE0_EN | \
108 	 SSI_SIER_RFRC_EN)
109 #define FSLSSI_SIER_DBG_TX_FLAGS \
110 	(SSI_SIER_TFE0_EN | \
111 	 SSI_SIER_TLS_EN | \
112 	 SSI_SIER_TFS_EN | \
113 	 SSI_SIER_TUE0_EN | \
114 	 SSI_SIER_TFRC_EN)
115 
116 enum fsl_ssi_type {
117 	FSL_SSI_MCP8610,
118 	FSL_SSI_MX21,
119 	FSL_SSI_MX35,
120 	FSL_SSI_MX51,
121 };
122 
123 struct fsl_ssi_regvals {
124 	u32 sier;
125 	u32 srcr;
126 	u32 stcr;
127 	u32 scr;
128 };
129 
130 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
131 {
132 	switch (reg) {
133 	case REG_SSI_SACCEN:
134 	case REG_SSI_SACCDIS:
135 		return false;
136 	default:
137 		return true;
138 	}
139 }
140 
141 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
142 {
143 	switch (reg) {
144 	case REG_SSI_STX0:
145 	case REG_SSI_STX1:
146 	case REG_SSI_SRX0:
147 	case REG_SSI_SRX1:
148 	case REG_SSI_SISR:
149 	case REG_SSI_SFCSR:
150 	case REG_SSI_SACNT:
151 	case REG_SSI_SACADD:
152 	case REG_SSI_SACDAT:
153 	case REG_SSI_SATAG:
154 	case REG_SSI_SACCST:
155 	case REG_SSI_SOR:
156 		return true;
157 	default:
158 		return false;
159 	}
160 }
161 
162 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
163 {
164 	switch (reg) {
165 	case REG_SSI_SRX0:
166 	case REG_SSI_SRX1:
167 	case REG_SSI_SISR:
168 	case REG_SSI_SACADD:
169 	case REG_SSI_SACDAT:
170 	case REG_SSI_SATAG:
171 		return true;
172 	default:
173 		return false;
174 	}
175 }
176 
177 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
178 {
179 	switch (reg) {
180 	case REG_SSI_SRX0:
181 	case REG_SSI_SRX1:
182 	case REG_SSI_SACCST:
183 		return false;
184 	default:
185 		return true;
186 	}
187 }
188 
189 static const struct regmap_config fsl_ssi_regconfig = {
190 	.max_register = REG_SSI_SACCDIS,
191 	.reg_bits = 32,
192 	.val_bits = 32,
193 	.reg_stride = 4,
194 	.val_format_endian = REGMAP_ENDIAN_NATIVE,
195 	.num_reg_defaults_raw = REG_SSI_SACCDIS / sizeof(uint32_t) + 1,
196 	.readable_reg = fsl_ssi_readable_reg,
197 	.volatile_reg = fsl_ssi_volatile_reg,
198 	.precious_reg = fsl_ssi_precious_reg,
199 	.writeable_reg = fsl_ssi_writeable_reg,
200 	.cache_type = REGCACHE_FLAT,
201 };
202 
203 struct fsl_ssi_soc_data {
204 	bool imx;
205 	bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
206 	bool offline_config;
207 	u32 sisr_write_mask;
208 };
209 
210 /**
211  * fsl_ssi: per-SSI private data
212  *
213  * @regs: Pointer to the regmap registers
214  * @irq: IRQ of this SSI
215  * @cpu_dai_drv: CPU DAI driver for this device
216  *
217  * @dai_fmt: DAI configuration this device is currently used with
218  * @streams: Mask of current active streams: BIT(TX) and BIT(RX)
219  * @i2s_net: I2S and Network mode configurations of SCR register
220  *           (this is the initial settings based on the DAI format)
221  * @synchronous: Use synchronous mode - both of TX and RX use STCK and SFCK
222  * @use_dma: DMA is used or FIQ with stream filter
223  * @use_dual_fifo: DMA with support for dual FIFO mode
224  * @has_ipg_clk_name: If "ipg" is in the clock name list of device tree
225  * @fifo_depth: Depth of the SSI FIFOs
226  * @slot_width: Width of each DAI slot
227  * @slots: Number of slots
228  * @regvals: Specific RX/TX register settings
229  *
230  * @clk: Clock source to access register
231  * @baudclk: Clock source to generate bit and frame-sync clocks
232  * @baudclk_streams: Active streams that are using baudclk
233  *
234  * @regcache_sfcsr: Cache sfcsr register value during suspend and resume
235  * @regcache_sacnt: Cache sacnt register value during suspend and resume
236  *
237  * @dma_params_tx: DMA transmit parameters
238  * @dma_params_rx: DMA receive parameters
239  * @ssi_phys: physical address of the SSI registers
240  *
241  * @fiq_params: FIQ stream filtering parameters
242  *
243  * @card_pdev: Platform_device pointer to register a sound card for PowerPC or
244  *             to register a CODEC platform device for AC97
245  * @card_name: Platform_device name to register a sound card for PowerPC or
246  *             to register a CODEC platform device for AC97
247  * @card_idx: The index of SSI to register a sound card for PowerPC or
248  *            to register a CODEC platform device for AC97
249  *
250  * @dbg_stats: Debugging statistics
251  *
252  * @soc: SoC specific data
253  * @dev: Pointer to &pdev->dev
254  *
255  * @fifo_watermark: The FIFO watermark setting. Notifies DMA when there are
256  *                  @fifo_watermark or fewer words in TX fifo or
257  *                  @fifo_watermark or more empty words in RX fifo.
258  * @dma_maxburst: Max number of words to transfer in one go. So far,
259  *                this is always the same as fifo_watermark.
260  *
261  * @ac97_reg_lock: Mutex lock to serialize AC97 register access operations
262  */
263 struct fsl_ssi {
264 	struct regmap *regs;
265 	int irq;
266 	struct snd_soc_dai_driver cpu_dai_drv;
267 
268 	unsigned int dai_fmt;
269 	u8 streams;
270 	u8 i2s_net;
271 	bool synchronous;
272 	bool use_dma;
273 	bool use_dual_fifo;
274 	bool has_ipg_clk_name;
275 	unsigned int fifo_depth;
276 	unsigned int slot_width;
277 	unsigned int slots;
278 	struct fsl_ssi_regvals regvals[2];
279 
280 	struct clk *clk;
281 	struct clk *baudclk;
282 	unsigned int baudclk_streams;
283 
284 	u32 regcache_sfcsr;
285 	u32 regcache_sacnt;
286 
287 	struct snd_dmaengine_dai_dma_data dma_params_tx;
288 	struct snd_dmaengine_dai_dma_data dma_params_rx;
289 	dma_addr_t ssi_phys;
290 
291 	struct imx_pcm_fiq_params fiq_params;
292 
293 	struct platform_device *card_pdev;
294 	char card_name[32];
295 	u32 card_idx;
296 
297 	struct fsl_ssi_dbg dbg_stats;
298 
299 	const struct fsl_ssi_soc_data *soc;
300 	struct device *dev;
301 
302 	u32 fifo_watermark;
303 	u32 dma_maxburst;
304 
305 	struct mutex ac97_reg_lock;
306 };
307 
308 /*
309  * SoC specific data
310  *
311  * Notes:
312  * 1) SSI in earlier SoCS has critical bits in control registers that
313  *    cannot be changed after SSI starts running -- a software reset
314  *    (set SSIEN to 0) is required to change their values. So adding
315  *    an offline_config flag for these SoCs.
316  * 2) SDMA is available since imx35. However, imx35 does not support
317  *    DMA bits changing when SSI is running, so set offline_config.
318  * 3) imx51 and later versions support register configurations when
319  *    SSI is running (SSIEN); For these versions, DMA needs to be
320  *    configured before SSI sends DMA request to avoid an undefined
321  *    DMA request on the SDMA side.
322  */
323 
324 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
325 	.imx = false,
326 	.offline_config = true,
327 	.sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
328 			   SSI_SISR_ROE0 | SSI_SISR_ROE1 |
329 			   SSI_SISR_TUE0 | SSI_SISR_TUE1,
330 };
331 
332 static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
333 	.imx = true,
334 	.imx21regs = true,
335 	.offline_config = true,
336 	.sisr_write_mask = 0,
337 };
338 
339 static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
340 	.imx = true,
341 	.offline_config = true,
342 	.sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
343 			   SSI_SISR_ROE0 | SSI_SISR_ROE1 |
344 			   SSI_SISR_TUE0 | SSI_SISR_TUE1,
345 };
346 
347 static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
348 	.imx = true,
349 	.offline_config = false,
350 	.sisr_write_mask = SSI_SISR_ROE0 | SSI_SISR_ROE1 |
351 			   SSI_SISR_TUE0 | SSI_SISR_TUE1,
352 };
353 
354 static const struct of_device_id fsl_ssi_ids[] = {
355 	{ .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
356 	{ .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
357 	{ .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
358 	{ .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
359 	{}
360 };
361 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
362 
363 static bool fsl_ssi_is_ac97(struct fsl_ssi *ssi)
364 {
365 	return (ssi->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
366 		SND_SOC_DAIFMT_AC97;
367 }
368 
369 static bool fsl_ssi_is_i2s_master(struct fsl_ssi *ssi)
370 {
371 	return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
372 		SND_SOC_DAIFMT_CBS_CFS;
373 }
374 
375 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi *ssi)
376 {
377 	return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
378 		SND_SOC_DAIFMT_CBM_CFS;
379 }
380 
381 /**
382  * Interrupt handler to gather states
383  */
384 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
385 {
386 	struct fsl_ssi *ssi = dev_id;
387 	struct regmap *regs = ssi->regs;
388 	__be32 sisr;
389 	__be32 sisr2;
390 
391 	regmap_read(regs, REG_SSI_SISR, &sisr);
392 
393 	sisr2 = sisr & ssi->soc->sisr_write_mask;
394 	/* Clear the bits that we set */
395 	if (sisr2)
396 		regmap_write(regs, REG_SSI_SISR, sisr2);
397 
398 	fsl_ssi_dbg_isr(&ssi->dbg_stats, sisr);
399 
400 	return IRQ_HANDLED;
401 }
402 
403 /**
404  * Set SCR, SIER, STCR and SRCR registers with cached values in regvals
405  *
406  * Notes:
407  * 1) For offline_config SoCs, enable all necessary bits of both streams
408  *    when 1st stream starts, even if the opposite stream will not start
409  * 2) It also clears FIFO before setting regvals; SOR is safe to set online
410  */
411 static void fsl_ssi_config_enable(struct fsl_ssi *ssi, bool tx)
412 {
413 	struct fsl_ssi_regvals *vals = ssi->regvals;
414 	int dir = tx ? TX : RX;
415 	u32 sier, srcr, stcr;
416 
417 	/* Clear dirty data in the FIFO; It also prevents channel slipping */
418 	regmap_update_bits(ssi->regs, REG_SSI_SOR,
419 			   SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
420 
421 	/*
422 	 * On offline_config SoCs, SxCR and SIER are already configured when
423 	 * the previous stream started. So skip all SxCR and SIER settings
424 	 * to prevent online reconfigurations, then jump to set SCR directly
425 	 */
426 	if (ssi->soc->offline_config && ssi->streams)
427 		goto enable_scr;
428 
429 	if (ssi->soc->offline_config) {
430 		/*
431 		 * Online reconfiguration not supported, so enable all bits for
432 		 * both streams at once to avoid necessity of reconfigurations
433 		 */
434 		srcr = vals[RX].srcr | vals[TX].srcr;
435 		stcr = vals[RX].stcr | vals[TX].stcr;
436 		sier = vals[RX].sier | vals[TX].sier;
437 	} else {
438 		/* Otherwise, only set bits for the current stream */
439 		srcr = vals[dir].srcr;
440 		stcr = vals[dir].stcr;
441 		sier = vals[dir].sier;
442 	}
443 
444 	/* Configure SRCR, STCR and SIER at once */
445 	regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, srcr);
446 	regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, stcr);
447 	regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, sier);
448 
449 enable_scr:
450 	/*
451 	 * Start DMA before setting TE to avoid FIFO underrun
452 	 * which may cause a channel slip or a channel swap
453 	 *
454 	 * TODO: FIQ cases might also need this upon testing
455 	 */
456 	if (ssi->use_dma && tx) {
457 		int try = 100;
458 		u32 sfcsr;
459 
460 		/* Enable SSI first to send TX DMA request */
461 		regmap_update_bits(ssi->regs, REG_SSI_SCR,
462 				   SSI_SCR_SSIEN, SSI_SCR_SSIEN);
463 
464 		/* Busy wait until TX FIFO not empty -- DMA working */
465 		do {
466 			regmap_read(ssi->regs, REG_SSI_SFCSR, &sfcsr);
467 			if (SSI_SFCSR_TFCNT0(sfcsr))
468 				break;
469 		} while (--try);
470 
471 		/* FIFO still empty -- something might be wrong */
472 		if (!SSI_SFCSR_TFCNT0(sfcsr))
473 			dev_warn(ssi->dev, "Timeout waiting TX FIFO filling\n");
474 	}
475 	/* Enable all remaining bits in SCR */
476 	regmap_update_bits(ssi->regs, REG_SSI_SCR,
477 			   vals[dir].scr, vals[dir].scr);
478 
479 	/* Log the enabled stream to the mask */
480 	ssi->streams |= BIT(dir);
481 }
482 
483 /**
484  * Exclude bits that are used by the opposite stream
485  *
486  * When both streams are active, disabling some bits for the current stream
487  * might break the other stream if these bits are used by it.
488  *
489  * @vals : regvals of the current stream
490  * @avals: regvals of the opposite stream
491  * @aactive: active state of the opposite stream
492  *
493  *  1) XOR vals and avals to get the differences if the other stream is active;
494  *     Otherwise, return current vals if the other stream is not active
495  *  2) AND the result of 1) with the current vals
496  */
497 #define _ssi_xor_shared_bits(vals, avals, aactive) \
498 	((vals) ^ ((avals) * (aactive)))
499 
500 #define ssi_excl_shared_bits(vals, avals, aactive) \
501 	((vals) & _ssi_xor_shared_bits(vals, avals, aactive))
502 
503 /**
504  * Unset SCR, SIER, STCR and SRCR registers with cached values in regvals
505  *
506  * Notes:
507  * 1) For offline_config SoCs, to avoid online reconfigurations, disable all
508  *    bits of both streams at once when the last stream is abort to end
509  * 2) It also clears FIFO after unsetting regvals; SOR is safe to set online
510  */
511 static void fsl_ssi_config_disable(struct fsl_ssi *ssi, bool tx)
512 {
513 	struct fsl_ssi_regvals *vals, *avals;
514 	u32 sier, srcr, stcr, scr;
515 	int adir = tx ? RX : TX;
516 	int dir = tx ? TX : RX;
517 	bool aactive;
518 
519 	/* Check if the opposite stream is active */
520 	aactive = ssi->streams & BIT(adir);
521 
522 	vals = &ssi->regvals[dir];
523 
524 	/* Get regvals of the opposite stream to keep opposite stream safe */
525 	avals = &ssi->regvals[adir];
526 
527 	/*
528 	 * To keep the other stream safe, exclude shared bits between
529 	 * both streams, and get safe bits to disable current stream
530 	 */
531 	scr = ssi_excl_shared_bits(vals->scr, avals->scr, aactive);
532 
533 	/* Disable safe bits of SCR register for the current stream */
534 	regmap_update_bits(ssi->regs, REG_SSI_SCR, scr, 0);
535 
536 	/* Log the disabled stream to the mask */
537 	ssi->streams &= ~BIT(dir);
538 
539 	/*
540 	 * On offline_config SoCs, if the other stream is active, skip
541 	 * SxCR and SIER settings to prevent online reconfigurations
542 	 */
543 	if (ssi->soc->offline_config && aactive)
544 		goto fifo_clear;
545 
546 	if (ssi->soc->offline_config) {
547 		/* Now there is only current stream active, disable all bits */
548 		srcr = vals->srcr | avals->srcr;
549 		stcr = vals->stcr | avals->stcr;
550 		sier = vals->sier | avals->sier;
551 	} else {
552 		/*
553 		 * To keep the other stream safe, exclude shared bits between
554 		 * both streams, and get safe bits to disable current stream
555 		 */
556 		sier = ssi_excl_shared_bits(vals->sier, avals->sier, aactive);
557 		srcr = ssi_excl_shared_bits(vals->srcr, avals->srcr, aactive);
558 		stcr = ssi_excl_shared_bits(vals->stcr, avals->stcr, aactive);
559 	}
560 
561 	/* Clear configurations of SRCR, STCR and SIER at once */
562 	regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, 0);
563 	regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, 0);
564 	regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, 0);
565 
566 fifo_clear:
567 	/* Clear remaining data in the FIFO */
568 	regmap_update_bits(ssi->regs, REG_SSI_SOR,
569 			   SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
570 }
571 
572 static void fsl_ssi_tx_ac97_saccst_setup(struct fsl_ssi *ssi)
573 {
574 	struct regmap *regs = ssi->regs;
575 
576 	/* no SACC{ST,EN,DIS} regs on imx21-class SSI */
577 	if (!ssi->soc->imx21regs) {
578 		/* Disable all channel slots */
579 		regmap_write(regs, REG_SSI_SACCDIS, 0xff);
580 		/* Enable slots 3 & 4 -- PCM Playback Left & Right channels */
581 		regmap_write(regs, REG_SSI_SACCEN, 0x300);
582 	}
583 }
584 
585 /**
586  * Cache critical bits of SIER, SRCR, STCR and SCR to later set them safely
587  */
588 static void fsl_ssi_setup_regvals(struct fsl_ssi *ssi)
589 {
590 	struct fsl_ssi_regvals *vals = ssi->regvals;
591 
592 	vals[RX].sier = SSI_SIER_RFF0_EN | FSLSSI_SIER_DBG_RX_FLAGS;
593 	vals[RX].srcr = SSI_SRCR_RFEN0;
594 	vals[RX].scr = SSI_SCR_SSIEN | SSI_SCR_RE;
595 	vals[TX].sier = SSI_SIER_TFE0_EN | FSLSSI_SIER_DBG_TX_FLAGS;
596 	vals[TX].stcr = SSI_STCR_TFEN0;
597 	vals[TX].scr = SSI_SCR_SSIEN | SSI_SCR_TE;
598 
599 	/* AC97 has already enabled SSIEN, RE and TE, so ignore them */
600 	if (fsl_ssi_is_ac97(ssi))
601 		vals[RX].scr = vals[TX].scr = 0;
602 
603 	if (ssi->use_dual_fifo) {
604 		vals[RX].srcr |= SSI_SRCR_RFEN1;
605 		vals[TX].stcr |= SSI_STCR_TFEN1;
606 	}
607 
608 	if (ssi->use_dma) {
609 		vals[RX].sier |= SSI_SIER_RDMAE;
610 		vals[TX].sier |= SSI_SIER_TDMAE;
611 	} else {
612 		vals[RX].sier |= SSI_SIER_RIE;
613 		vals[TX].sier |= SSI_SIER_TIE;
614 	}
615 }
616 
617 static void fsl_ssi_setup_ac97(struct fsl_ssi *ssi)
618 {
619 	struct regmap *regs = ssi->regs;
620 
621 	/* Setup the clock control register */
622 	regmap_write(regs, REG_SSI_STCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
623 	regmap_write(regs, REG_SSI_SRCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
624 
625 	/* Enable AC97 mode and startup the SSI */
626 	regmap_write(regs, REG_SSI_SACNT, SSI_SACNT_AC97EN | SSI_SACNT_FV);
627 
628 	/* AC97 has to communicate with codec before starting a stream */
629 	regmap_update_bits(regs, REG_SSI_SCR,
630 			   SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE,
631 			   SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE);
632 
633 	regmap_write(regs, REG_SSI_SOR, SSI_SOR_WAIT(3));
634 }
635 
636 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
637 			   struct snd_soc_dai *dai)
638 {
639 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
640 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
641 	int ret;
642 
643 	ret = clk_prepare_enable(ssi->clk);
644 	if (ret)
645 		return ret;
646 
647 	/*
648 	 * When using dual fifo mode, it is safer to ensure an even period
649 	 * size. If appearing to an odd number while DMA always starts its
650 	 * task from fifo0, fifo1 would be neglected at the end of each
651 	 * period. But SSI would still access fifo1 with an invalid data.
652 	 */
653 	if (ssi->use_dual_fifo)
654 		snd_pcm_hw_constraint_step(substream->runtime, 0,
655 					   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
656 
657 	return 0;
658 }
659 
660 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
661 			     struct snd_soc_dai *dai)
662 {
663 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
664 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
665 
666 	clk_disable_unprepare(ssi->clk);
667 }
668 
669 /**
670  * Configure Digital Audio Interface bit clock
671  *
672  * Note: This function can be only called when using SSI as DAI master
673  *
674  * Quick instruction for parameters:
675  * freq: Output BCLK frequency = samplerate * slots * slot_width
676  *       (In 2-channel I2S Master mode, slot_width is fixed 32)
677  */
678 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
679 			    struct snd_soc_dai *dai,
680 			    struct snd_pcm_hw_params *hw_params)
681 {
682 	bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
683 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
684 	struct regmap *regs = ssi->regs;
685 	u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
686 	unsigned long clkrate, baudrate, tmprate;
687 	unsigned int slots = params_channels(hw_params);
688 	unsigned int slot_width = 32;
689 	u64 sub, savesub = 100000;
690 	unsigned int freq;
691 	bool baudclk_is_used;
692 	int ret;
693 
694 	/* Override slots and slot_width if being specifically set... */
695 	if (ssi->slots)
696 		slots = ssi->slots;
697 	/* ...but keep 32 bits if slots is 2 -- I2S Master mode */
698 	if (ssi->slot_width && slots != 2)
699 		slot_width = ssi->slot_width;
700 
701 	/* Generate bit clock based on the slot number and slot width */
702 	freq = slots * slot_width * params_rate(hw_params);
703 
704 	/* Don't apply it to any non-baudclk circumstance */
705 	if (IS_ERR(ssi->baudclk))
706 		return -EINVAL;
707 
708 	/*
709 	 * Hardware limitation: The bclk rate must be
710 	 * never greater than 1/5 IPG clock rate
711 	 */
712 	if (freq * 5 > clk_get_rate(ssi->clk)) {
713 		dev_err(dai->dev, "bitclk > ipgclk / 5\n");
714 		return -EINVAL;
715 	}
716 
717 	baudclk_is_used = ssi->baudclk_streams & ~(BIT(substream->stream));
718 
719 	/* It should be already enough to divide clock by setting pm alone */
720 	psr = 0;
721 	div2 = 0;
722 
723 	factor = (div2 + 1) * (7 * psr + 1) * 2;
724 
725 	for (i = 0; i < 255; i++) {
726 		tmprate = freq * factor * (i + 1);
727 
728 		if (baudclk_is_used)
729 			clkrate = clk_get_rate(ssi->baudclk);
730 		else
731 			clkrate = clk_round_rate(ssi->baudclk, tmprate);
732 
733 		clkrate /= factor;
734 		afreq = clkrate / (i + 1);
735 
736 		if (freq == afreq)
737 			sub = 0;
738 		else if (freq / afreq == 1)
739 			sub = freq - afreq;
740 		else if (afreq / freq == 1)
741 			sub = afreq - freq;
742 		else
743 			continue;
744 
745 		/* Calculate the fraction */
746 		sub *= 100000;
747 		do_div(sub, freq);
748 
749 		if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
750 			baudrate = tmprate;
751 			savesub = sub;
752 			pm = i;
753 		}
754 
755 		/* We are lucky */
756 		if (savesub == 0)
757 			break;
758 	}
759 
760 	/* No proper pm found if it is still remaining the initial value */
761 	if (pm == 999) {
762 		dev_err(dai->dev, "failed to handle the required sysclk\n");
763 		return -EINVAL;
764 	}
765 
766 	stccr = SSI_SxCCR_PM(pm + 1) | (div2 ? SSI_SxCCR_DIV2 : 0) |
767 		(psr ? SSI_SxCCR_PSR : 0);
768 	mask = SSI_SxCCR_PM_MASK | SSI_SxCCR_DIV2 | SSI_SxCCR_PSR;
769 
770 	/* STCCR is used for RX in synchronous mode */
771 	tx2 = tx || ssi->synchronous;
772 	regmap_update_bits(regs, REG_SSI_SxCCR(tx2), mask, stccr);
773 
774 	if (!baudclk_is_used) {
775 		ret = clk_set_rate(ssi->baudclk, baudrate);
776 		if (ret) {
777 			dev_err(dai->dev, "failed to set baudclk rate\n");
778 			return -EINVAL;
779 		}
780 	}
781 
782 	return 0;
783 }
784 
785 /**
786  * Configure SSI based on PCM hardware parameters
787  *
788  * Notes:
789  * 1) SxCCR.WL bits are critical bits that require SSI to be temporarily
790  *    disabled on offline_config SoCs. Even for online configurable SoCs
791  *    running in synchronous mode (both TX and RX use STCCR), it is not
792  *    safe to re-configure them when both two streams start running.
793  * 2) SxCCR.PM, SxCCR.DIV2 and SxCCR.PSR bits will be configured in the
794  *    fsl_ssi_set_bclk() if SSI is the DAI clock master.
795  */
796 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
797 			     struct snd_pcm_hw_params *hw_params,
798 			     struct snd_soc_dai *dai)
799 {
800 	bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
801 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
802 	struct regmap *regs = ssi->regs;
803 	unsigned int channels = params_channels(hw_params);
804 	unsigned int sample_size = params_width(hw_params);
805 	u32 wl = SSI_SxCCR_WL(sample_size);
806 	int ret;
807 
808 	/*
809 	 * SSI is properly configured if it is enabled and running in
810 	 * the synchronous mode; Note that AC97 mode is an exception
811 	 * that should set separate configurations for STCCR and SRCCR
812 	 * despite running in the synchronous mode.
813 	 */
814 	if (ssi->streams && ssi->synchronous)
815 		return 0;
816 
817 	if (fsl_ssi_is_i2s_master(ssi)) {
818 		ret = fsl_ssi_set_bclk(substream, dai, hw_params);
819 		if (ret)
820 			return ret;
821 
822 		/* Do not enable the clock if it is already enabled */
823 		if (!(ssi->baudclk_streams & BIT(substream->stream))) {
824 			ret = clk_prepare_enable(ssi->baudclk);
825 			if (ret)
826 				return ret;
827 
828 			ssi->baudclk_streams |= BIT(substream->stream);
829 		}
830 	}
831 
832 	if (!fsl_ssi_is_ac97(ssi)) {
833 		/*
834 		 * Keep the ssi->i2s_net intact while having a local variable
835 		 * to override settings for special use cases. Otherwise, the
836 		 * ssi->i2s_net will lose the settings for regular use cases.
837 		 */
838 		u8 i2s_net = ssi->i2s_net;
839 
840 		/* Normal + Network mode to send 16-bit data in 32-bit frames */
841 		if (fsl_ssi_is_i2s_cbm_cfs(ssi) && sample_size == 16)
842 			i2s_net = SSI_SCR_I2S_MODE_NORMAL | SSI_SCR_NET;
843 
844 		/* Use Normal mode to send mono data at 1st slot of 2 slots */
845 		if (channels == 1)
846 			i2s_net = SSI_SCR_I2S_MODE_NORMAL;
847 
848 		regmap_update_bits(regs, REG_SSI_SCR,
849 				   SSI_SCR_I2S_NET_MASK, i2s_net);
850 	}
851 
852 	/* In synchronous mode, the SSI uses STCCR for capture */
853 	tx2 = tx || ssi->synchronous;
854 	regmap_update_bits(regs, REG_SSI_SxCCR(tx2), SSI_SxCCR_WL_MASK, wl);
855 
856 	return 0;
857 }
858 
859 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
860 			   struct snd_soc_dai *dai)
861 {
862 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
863 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
864 
865 	if (fsl_ssi_is_i2s_master(ssi) &&
866 	    ssi->baudclk_streams & BIT(substream->stream)) {
867 		clk_disable_unprepare(ssi->baudclk);
868 		ssi->baudclk_streams &= ~BIT(substream->stream);
869 	}
870 
871 	return 0;
872 }
873 
874 static int _fsl_ssi_set_dai_fmt(struct fsl_ssi *ssi, unsigned int fmt)
875 {
876 	u32 strcr = 0, scr = 0, stcr, srcr, mask;
877 
878 	ssi->dai_fmt = fmt;
879 
880 	/* Synchronize frame sync clock for TE to avoid data slipping */
881 	scr |= SSI_SCR_SYNC_TX_FS;
882 
883 	/* Set to default shifting settings: LSB_ALIGNED */
884 	strcr |= SSI_STCR_TXBIT0;
885 
886 	/* Use Network mode as default */
887 	ssi->i2s_net = SSI_SCR_NET;
888 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
889 	case SND_SOC_DAIFMT_I2S:
890 		switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
891 		case SND_SOC_DAIFMT_CBS_CFS:
892 			if (IS_ERR(ssi->baudclk)) {
893 				dev_err(ssi->dev,
894 					"missing baudclk for master mode\n");
895 				return -EINVAL;
896 			}
897 			/* fall through */
898 		case SND_SOC_DAIFMT_CBM_CFS:
899 			ssi->i2s_net |= SSI_SCR_I2S_MODE_MASTER;
900 			break;
901 		case SND_SOC_DAIFMT_CBM_CFM:
902 			ssi->i2s_net |= SSI_SCR_I2S_MODE_SLAVE;
903 			break;
904 		default:
905 			return -EINVAL;
906 		}
907 
908 		regmap_update_bits(ssi->regs, REG_SSI_STCCR,
909 				   SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2));
910 		regmap_update_bits(ssi->regs, REG_SSI_SRCCR,
911 				   SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2));
912 
913 		/* Data on rising edge of bclk, frame low, 1clk before data */
914 		strcr |= SSI_STCR_TFSI | SSI_STCR_TSCKP | SSI_STCR_TEFS;
915 		break;
916 	case SND_SOC_DAIFMT_LEFT_J:
917 		/* Data on rising edge of bclk, frame high */
918 		strcr |= SSI_STCR_TSCKP;
919 		break;
920 	case SND_SOC_DAIFMT_DSP_A:
921 		/* Data on rising edge of bclk, frame high, 1clk before data */
922 		strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP | SSI_STCR_TEFS;
923 		break;
924 	case SND_SOC_DAIFMT_DSP_B:
925 		/* Data on rising edge of bclk, frame high */
926 		strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP;
927 		break;
928 	case SND_SOC_DAIFMT_AC97:
929 		/* Data on falling edge of bclk, frame high, 1clk before data */
930 		strcr |= SSI_STCR_TEFS;
931 		break;
932 	default:
933 		return -EINVAL;
934 	}
935 
936 	scr |= ssi->i2s_net;
937 
938 	/* DAI clock inversion */
939 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
940 	case SND_SOC_DAIFMT_NB_NF:
941 		/* Nothing to do for both normal cases */
942 		break;
943 	case SND_SOC_DAIFMT_IB_NF:
944 		/* Invert bit clock */
945 		strcr ^= SSI_STCR_TSCKP;
946 		break;
947 	case SND_SOC_DAIFMT_NB_IF:
948 		/* Invert frame clock */
949 		strcr ^= SSI_STCR_TFSI;
950 		break;
951 	case SND_SOC_DAIFMT_IB_IF:
952 		/* Invert both clocks */
953 		strcr ^= SSI_STCR_TSCKP;
954 		strcr ^= SSI_STCR_TFSI;
955 		break;
956 	default:
957 		return -EINVAL;
958 	}
959 
960 	/* DAI clock master masks */
961 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
962 	case SND_SOC_DAIFMT_CBS_CFS:
963 		/* Output bit and frame sync clocks */
964 		strcr |= SSI_STCR_TFDIR | SSI_STCR_TXDIR;
965 		scr |= SSI_SCR_SYS_CLK_EN;
966 		break;
967 	case SND_SOC_DAIFMT_CBM_CFM:
968 		/* Input bit or frame sync clocks */
969 		break;
970 	case SND_SOC_DAIFMT_CBM_CFS:
971 		/* Input bit clock but output frame sync clock */
972 		strcr |= SSI_STCR_TFDIR;
973 		break;
974 	default:
975 		return -EINVAL;
976 	}
977 
978 	stcr = strcr;
979 	srcr = strcr;
980 
981 	/* Set SYN mode and clear RXDIR bit when using SYN or AC97 mode */
982 	if (ssi->synchronous || fsl_ssi_is_ac97(ssi)) {
983 		srcr &= ~SSI_SRCR_RXDIR;
984 		scr |= SSI_SCR_SYN;
985 	}
986 
987 	mask = SSI_STCR_TFDIR | SSI_STCR_TXDIR | SSI_STCR_TSCKP |
988 	       SSI_STCR_TFSL | SSI_STCR_TFSI | SSI_STCR_TEFS | SSI_STCR_TXBIT0;
989 
990 	regmap_update_bits(ssi->regs, REG_SSI_STCR, mask, stcr);
991 	regmap_update_bits(ssi->regs, REG_SSI_SRCR, mask, srcr);
992 
993 	mask = SSI_SCR_SYNC_TX_FS | SSI_SCR_I2S_MODE_MASK |
994 	       SSI_SCR_SYS_CLK_EN | SSI_SCR_SYN;
995 	regmap_update_bits(ssi->regs, REG_SSI_SCR, mask, scr);
996 
997 	return 0;
998 }
999 
1000 /**
1001  * Configure Digital Audio Interface (DAI) Format
1002  */
1003 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1004 {
1005 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1006 
1007 	/* AC97 configured DAIFMT earlier in the probe() */
1008 	if (fsl_ssi_is_ac97(ssi))
1009 		return 0;
1010 
1011 	return _fsl_ssi_set_dai_fmt(ssi, fmt);
1012 }
1013 
1014 /**
1015  * Set TDM slot number and slot width
1016  */
1017 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
1018 				    u32 rx_mask, int slots, int slot_width)
1019 {
1020 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1021 	struct regmap *regs = ssi->regs;
1022 	u32 val;
1023 
1024 	/* The word length should be 8, 10, 12, 16, 18, 20, 22 or 24 */
1025 	if (slot_width & 1 || slot_width < 8 || slot_width > 24) {
1026 		dev_err(dai->dev, "invalid slot width: %d\n", slot_width);
1027 		return -EINVAL;
1028 	}
1029 
1030 	/* The slot number should be >= 2 if using Network mode or I2S mode */
1031 	if (ssi->i2s_net && slots < 2) {
1032 		dev_err(dai->dev, "slot number should be >= 2 in I2S or NET\n");
1033 		return -EINVAL;
1034 	}
1035 
1036 	regmap_update_bits(regs, REG_SSI_STCCR,
1037 			   SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
1038 	regmap_update_bits(regs, REG_SSI_SRCCR,
1039 			   SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
1040 
1041 	/* Save the SCR register value */
1042 	regmap_read(regs, REG_SSI_SCR, &val);
1043 	/* Temporarily enable SSI to allow SxMSKs to be configurable */
1044 	regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, SSI_SCR_SSIEN);
1045 
1046 	regmap_write(regs, REG_SSI_STMSK, ~tx_mask);
1047 	regmap_write(regs, REG_SSI_SRMSK, ~rx_mask);
1048 
1049 	/* Restore the value of SSIEN bit */
1050 	regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, val);
1051 
1052 	ssi->slot_width = slot_width;
1053 	ssi->slots = slots;
1054 
1055 	return 0;
1056 }
1057 
1058 /**
1059  * Start or stop SSI and corresponding DMA transaction.
1060  *
1061  * The DMA channel is in external master start and pause mode, which
1062  * means the SSI completely controls the flow of data.
1063  */
1064 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1065 			   struct snd_soc_dai *dai)
1066 {
1067 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1068 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1069 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1070 
1071 	switch (cmd) {
1072 	case SNDRV_PCM_TRIGGER_START:
1073 	case SNDRV_PCM_TRIGGER_RESUME:
1074 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1075 		/*
1076 		 * SACCST might be modified via AC Link by a CODEC if it sends
1077 		 * extra bits in their SLOTREQ requests, which'll accidentally
1078 		 * send valid data to slots other than normal playback slots.
1079 		 *
1080 		 * To be safe, configure SACCST right before TX starts.
1081 		 */
1082 		if (tx && fsl_ssi_is_ac97(ssi))
1083 			fsl_ssi_tx_ac97_saccst_setup(ssi);
1084 		fsl_ssi_config_enable(ssi, tx);
1085 		break;
1086 
1087 	case SNDRV_PCM_TRIGGER_STOP:
1088 	case SNDRV_PCM_TRIGGER_SUSPEND:
1089 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1090 		fsl_ssi_config_disable(ssi, tx);
1091 		break;
1092 
1093 	default:
1094 		return -EINVAL;
1095 	}
1096 
1097 	return 0;
1098 }
1099 
1100 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1101 {
1102 	struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1103 
1104 	if (ssi->soc->imx && ssi->use_dma)
1105 		snd_soc_dai_init_dma_data(dai, &ssi->dma_params_tx,
1106 					  &ssi->dma_params_rx);
1107 
1108 	return 0;
1109 }
1110 
1111 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
1112 	.startup = fsl_ssi_startup,
1113 	.shutdown = fsl_ssi_shutdown,
1114 	.hw_params = fsl_ssi_hw_params,
1115 	.hw_free = fsl_ssi_hw_free,
1116 	.set_fmt = fsl_ssi_set_dai_fmt,
1117 	.set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
1118 	.trigger = fsl_ssi_trigger,
1119 };
1120 
1121 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
1122 	.probe = fsl_ssi_dai_probe,
1123 	.playback = {
1124 		.stream_name = "CPU-Playback",
1125 		.channels_min = 1,
1126 		.channels_max = 32,
1127 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
1128 		.formats = FSLSSI_I2S_FORMATS,
1129 	},
1130 	.capture = {
1131 		.stream_name = "CPU-Capture",
1132 		.channels_min = 1,
1133 		.channels_max = 32,
1134 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
1135 		.formats = FSLSSI_I2S_FORMATS,
1136 	},
1137 	.ops = &fsl_ssi_dai_ops,
1138 };
1139 
1140 static const struct snd_soc_component_driver fsl_ssi_component = {
1141 	.name = "fsl-ssi",
1142 };
1143 
1144 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1145 	.bus_control = true,
1146 	.symmetric_channels = 1,
1147 	.probe = fsl_ssi_dai_probe,
1148 	.playback = {
1149 		.stream_name = "AC97 Playback",
1150 		.channels_min = 2,
1151 		.channels_max = 2,
1152 		.rates = SNDRV_PCM_RATE_8000_48000,
1153 		.formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S20,
1154 	},
1155 	.capture = {
1156 		.stream_name = "AC97 Capture",
1157 		.channels_min = 2,
1158 		.channels_max = 2,
1159 		.rates = SNDRV_PCM_RATE_48000,
1160 		/* 16-bit capture is broken (errata ERR003778) */
1161 		.formats = SNDRV_PCM_FMTBIT_S20,
1162 	},
1163 	.ops = &fsl_ssi_dai_ops,
1164 };
1165 
1166 static struct fsl_ssi *fsl_ac97_data;
1167 
1168 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1169 			       unsigned short val)
1170 {
1171 	struct regmap *regs = fsl_ac97_data->regs;
1172 	unsigned int lreg;
1173 	unsigned int lval;
1174 	int ret;
1175 
1176 	if (reg > 0x7f)
1177 		return;
1178 
1179 	mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1180 
1181 	ret = clk_prepare_enable(fsl_ac97_data->clk);
1182 	if (ret) {
1183 		pr_err("ac97 write clk_prepare_enable failed: %d\n",
1184 			ret);
1185 		goto ret_unlock;
1186 	}
1187 
1188 	lreg = reg <<  12;
1189 	regmap_write(regs, REG_SSI_SACADD, lreg);
1190 
1191 	lval = val << 4;
1192 	regmap_write(regs, REG_SSI_SACDAT, lval);
1193 
1194 	regmap_update_bits(regs, REG_SSI_SACNT,
1195 			   SSI_SACNT_RDWR_MASK, SSI_SACNT_WR);
1196 	udelay(100);
1197 
1198 	clk_disable_unprepare(fsl_ac97_data->clk);
1199 
1200 ret_unlock:
1201 	mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
1202 }
1203 
1204 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1205 					unsigned short reg)
1206 {
1207 	struct regmap *regs = fsl_ac97_data->regs;
1208 	unsigned short val = 0;
1209 	u32 reg_val;
1210 	unsigned int lreg;
1211 	int ret;
1212 
1213 	mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1214 
1215 	ret = clk_prepare_enable(fsl_ac97_data->clk);
1216 	if (ret) {
1217 		pr_err("ac97 read clk_prepare_enable failed: %d\n", ret);
1218 		goto ret_unlock;
1219 	}
1220 
1221 	lreg = (reg & 0x7f) <<  12;
1222 	regmap_write(regs, REG_SSI_SACADD, lreg);
1223 	regmap_update_bits(regs, REG_SSI_SACNT,
1224 			   SSI_SACNT_RDWR_MASK, SSI_SACNT_RD);
1225 
1226 	udelay(100);
1227 
1228 	regmap_read(regs, REG_SSI_SACDAT, &reg_val);
1229 	val = (reg_val >> 4) & 0xffff;
1230 
1231 	clk_disable_unprepare(fsl_ac97_data->clk);
1232 
1233 ret_unlock:
1234 	mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
1235 	return val;
1236 }
1237 
1238 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1239 	.read = fsl_ssi_ac97_read,
1240 	.write = fsl_ssi_ac97_write,
1241 };
1242 
1243 /**
1244  * Initialize SSI registers
1245  */
1246 static int fsl_ssi_hw_init(struct fsl_ssi *ssi)
1247 {
1248 	u32 wm = ssi->fifo_watermark;
1249 
1250 	/* Initialize regvals */
1251 	fsl_ssi_setup_regvals(ssi);
1252 
1253 	/* Set watermarks */
1254 	regmap_write(ssi->regs, REG_SSI_SFCSR,
1255 		     SSI_SFCSR_TFWM0(wm) | SSI_SFCSR_RFWM0(wm) |
1256 		     SSI_SFCSR_TFWM1(wm) | SSI_SFCSR_RFWM1(wm));
1257 
1258 	/* Enable Dual FIFO mode */
1259 	if (ssi->use_dual_fifo)
1260 		regmap_update_bits(ssi->regs, REG_SSI_SCR,
1261 				   SSI_SCR_TCH_EN, SSI_SCR_TCH_EN);
1262 
1263 	/* AC97 should start earlier to communicate with CODECs */
1264 	if (fsl_ssi_is_ac97(ssi)) {
1265 		_fsl_ssi_set_dai_fmt(ssi, ssi->dai_fmt);
1266 		fsl_ssi_setup_ac97(ssi);
1267 	}
1268 
1269 	return 0;
1270 }
1271 
1272 /**
1273  * Clear SSI registers
1274  */
1275 static void fsl_ssi_hw_clean(struct fsl_ssi *ssi)
1276 {
1277 	/* Disable registers for AC97 */
1278 	if (fsl_ssi_is_ac97(ssi)) {
1279 		/* Disable TE and RE bits first */
1280 		regmap_update_bits(ssi->regs, REG_SSI_SCR,
1281 				   SSI_SCR_TE | SSI_SCR_RE, 0);
1282 		/* Disable AC97 mode */
1283 		regmap_write(ssi->regs, REG_SSI_SACNT, 0);
1284 		/* Unset WAIT bits */
1285 		regmap_write(ssi->regs, REG_SSI_SOR, 0);
1286 		/* Disable SSI -- software reset */
1287 		regmap_update_bits(ssi->regs, REG_SSI_SCR, SSI_SCR_SSIEN, 0);
1288 	}
1289 }
1290 /**
1291  * Make every character in a string lower-case
1292  */
1293 static void make_lowercase(char *s)
1294 {
1295 	if (!s)
1296 		return;
1297 	for (; *s; s++)
1298 		*s = tolower(*s);
1299 }
1300 
1301 static int fsl_ssi_imx_probe(struct platform_device *pdev,
1302 			     struct fsl_ssi *ssi, void __iomem *iomem)
1303 {
1304 	struct device *dev = &pdev->dev;
1305 	int ret;
1306 
1307 	/* Backward compatible for a DT without ipg clock name assigned */
1308 	if (ssi->has_ipg_clk_name)
1309 		ssi->clk = devm_clk_get(dev, "ipg");
1310 	else
1311 		ssi->clk = devm_clk_get(dev, NULL);
1312 	if (IS_ERR(ssi->clk)) {
1313 		ret = PTR_ERR(ssi->clk);
1314 		dev_err(dev, "failed to get clock: %d\n", ret);
1315 		return ret;
1316 	}
1317 
1318 	/* Enable the clock since regmap will not handle it in this case */
1319 	if (!ssi->has_ipg_clk_name) {
1320 		ret = clk_prepare_enable(ssi->clk);
1321 		if (ret) {
1322 			dev_err(dev, "clk_prepare_enable failed: %d\n", ret);
1323 			return ret;
1324 		}
1325 	}
1326 
1327 	/* Do not error out for slave cases that live without a baud clock */
1328 	ssi->baudclk = devm_clk_get(dev, "baud");
1329 	if (IS_ERR(ssi->baudclk))
1330 		dev_dbg(dev, "failed to get baud clock: %ld\n",
1331 			 PTR_ERR(ssi->baudclk));
1332 
1333 	ssi->dma_params_tx.maxburst = ssi->dma_maxburst;
1334 	ssi->dma_params_rx.maxburst = ssi->dma_maxburst;
1335 	ssi->dma_params_tx.addr = ssi->ssi_phys + REG_SSI_STX0;
1336 	ssi->dma_params_rx.addr = ssi->ssi_phys + REG_SSI_SRX0;
1337 
1338 	/* Use even numbers to avoid channel swap due to SDMA script design */
1339 	if (ssi->use_dual_fifo) {
1340 		ssi->dma_params_tx.maxburst &= ~0x1;
1341 		ssi->dma_params_rx.maxburst &= ~0x1;
1342 	}
1343 
1344 	if (!ssi->use_dma) {
1345 		/*
1346 		 * Some boards use an incompatible codec. Use imx-fiq-pcm-audio
1347 		 * to get it working, as DMA is not possible in this situation.
1348 		 */
1349 		ssi->fiq_params.irq = ssi->irq;
1350 		ssi->fiq_params.base = iomem;
1351 		ssi->fiq_params.dma_params_rx = &ssi->dma_params_rx;
1352 		ssi->fiq_params.dma_params_tx = &ssi->dma_params_tx;
1353 
1354 		ret = imx_pcm_fiq_init(pdev, &ssi->fiq_params);
1355 		if (ret)
1356 			goto error_pcm;
1357 	} else {
1358 		ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
1359 		if (ret)
1360 			goto error_pcm;
1361 	}
1362 
1363 	return 0;
1364 
1365 error_pcm:
1366 	if (!ssi->has_ipg_clk_name)
1367 		clk_disable_unprepare(ssi->clk);
1368 
1369 	return ret;
1370 }
1371 
1372 static void fsl_ssi_imx_clean(struct platform_device *pdev, struct fsl_ssi *ssi)
1373 {
1374 	if (!ssi->use_dma)
1375 		imx_pcm_fiq_exit(pdev);
1376 	if (!ssi->has_ipg_clk_name)
1377 		clk_disable_unprepare(ssi->clk);
1378 }
1379 
1380 static int fsl_ssi_probe_from_dt(struct fsl_ssi *ssi)
1381 {
1382 	struct device *dev = ssi->dev;
1383 	struct device_node *np = dev->of_node;
1384 	const struct of_device_id *of_id;
1385 	const char *p, *sprop;
1386 	const __be32 *iprop;
1387 	u32 dmas[4];
1388 	int ret;
1389 
1390 	of_id = of_match_device(fsl_ssi_ids, dev);
1391 	if (!of_id || !of_id->data)
1392 		return -EINVAL;
1393 
1394 	ssi->soc = of_id->data;
1395 
1396 	ret = of_property_match_string(np, "clock-names", "ipg");
1397 	/* Get error code if not found */
1398 	ssi->has_ipg_clk_name = ret >= 0;
1399 
1400 	/* Check if being used in AC97 mode */
1401 	sprop = of_get_property(np, "fsl,mode", NULL);
1402 	if (sprop && !strcmp(sprop, "ac97-slave")) {
1403 		ssi->dai_fmt = FSLSSI_AC97_DAIFMT;
1404 
1405 		ret = of_property_read_u32(np, "cell-index", &ssi->card_idx);
1406 		if (ret) {
1407 			dev_err(dev, "failed to get SSI index property\n");
1408 			return -EINVAL;
1409 		}
1410 		strcpy(ssi->card_name, "ac97-codec");
1411 	} else if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1412 		/*
1413 		 * In synchronous mode, STCK and STFS ports are used by RX
1414 		 * as well. So the software should limit the sample rates,
1415 		 * sample bits and channels to be symmetric.
1416 		 *
1417 		 * This is exclusive with FSLSSI_AC97_FORMATS as AC97 runs
1418 		 * in the SSI synchronous mode however it does not have to
1419 		 * limit symmetric sample rates and sample bits.
1420 		 */
1421 		ssi->synchronous = true;
1422 	}
1423 
1424 	/* Select DMA or FIQ */
1425 	ssi->use_dma = !of_property_read_bool(np, "fsl,fiq-stream-filter");
1426 
1427 	/* Fetch FIFO depth; Set to 8 for older DT without this property */
1428 	iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1429 	if (iprop)
1430 		ssi->fifo_depth = be32_to_cpup(iprop);
1431 	else
1432 		ssi->fifo_depth = 8;
1433 
1434 	/* Use dual FIFO mode depending on the support from SDMA script */
1435 	ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1436 	if (ssi->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL)
1437 		ssi->use_dual_fifo = true;
1438 
1439 	/*
1440 	 * Backward compatible for older bindings by manually triggering the
1441 	 * machine driver's probe(). Use /compatible property, including the
1442 	 * address of CPU DAI driver structure, as the name of machine driver
1443 	 *
1444 	 * If card_name is set by AC97 earlier, bypass here since it uses a
1445 	 * different name to register the device.
1446 	 */
1447 	if (!ssi->card_name[0] && of_get_property(np, "codec-handle", NULL)) {
1448 		sprop = of_get_property(of_find_node_by_path("/"),
1449 					"compatible", NULL);
1450 		/* Strip "fsl," in the compatible name if applicable */
1451 		p = strrchr(sprop, ',');
1452 		if (p)
1453 			sprop = p + 1;
1454 		snprintf(ssi->card_name, sizeof(ssi->card_name),
1455 			 "snd-soc-%s", sprop);
1456 		make_lowercase(ssi->card_name);
1457 		ssi->card_idx = 0;
1458 	}
1459 
1460 	return 0;
1461 }
1462 
1463 static int fsl_ssi_probe(struct platform_device *pdev)
1464 {
1465 	struct regmap_config regconfig = fsl_ssi_regconfig;
1466 	struct device *dev = &pdev->dev;
1467 	struct fsl_ssi *ssi;
1468 	struct resource *res;
1469 	void __iomem *iomem;
1470 	int ret = 0;
1471 
1472 	ssi = devm_kzalloc(dev, sizeof(*ssi), GFP_KERNEL);
1473 	if (!ssi)
1474 		return -ENOMEM;
1475 
1476 	ssi->dev = dev;
1477 
1478 	/* Probe from DT */
1479 	ret = fsl_ssi_probe_from_dt(ssi);
1480 	if (ret)
1481 		return ret;
1482 
1483 	if (fsl_ssi_is_ac97(ssi)) {
1484 		memcpy(&ssi->cpu_dai_drv, &fsl_ssi_ac97_dai,
1485 		       sizeof(fsl_ssi_ac97_dai));
1486 		fsl_ac97_data = ssi;
1487 	} else {
1488 		memcpy(&ssi->cpu_dai_drv, &fsl_ssi_dai_template,
1489 		       sizeof(fsl_ssi_dai_template));
1490 	}
1491 	ssi->cpu_dai_drv.name = dev_name(dev);
1492 
1493 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1494 	iomem = devm_ioremap_resource(dev, res);
1495 	if (IS_ERR(iomem))
1496 		return PTR_ERR(iomem);
1497 	ssi->ssi_phys = res->start;
1498 
1499 	if (ssi->soc->imx21regs) {
1500 		/* No SACC{ST,EN,DIS} regs in imx21-class SSI */
1501 		regconfig.max_register = REG_SSI_SRMSK;
1502 		regconfig.num_reg_defaults_raw =
1503 			REG_SSI_SRMSK / sizeof(uint32_t) + 1;
1504 	}
1505 
1506 	if (ssi->has_ipg_clk_name)
1507 		ssi->regs = devm_regmap_init_mmio_clk(dev, "ipg", iomem,
1508 						      &regconfig);
1509 	else
1510 		ssi->regs = devm_regmap_init_mmio(dev, iomem, &regconfig);
1511 	if (IS_ERR(ssi->regs)) {
1512 		dev_err(dev, "failed to init register map\n");
1513 		return PTR_ERR(ssi->regs);
1514 	}
1515 
1516 	ssi->irq = platform_get_irq(pdev, 0);
1517 	if (ssi->irq < 0) {
1518 		dev_err(dev, "no irq for node %s\n", pdev->name);
1519 		return ssi->irq;
1520 	}
1521 
1522 	/* Set software limitations for synchronous mode except AC97 */
1523 	if (ssi->synchronous && !fsl_ssi_is_ac97(ssi)) {
1524 		ssi->cpu_dai_drv.symmetric_rates = 1;
1525 		ssi->cpu_dai_drv.symmetric_channels = 1;
1526 		ssi->cpu_dai_drv.symmetric_samplebits = 1;
1527 	}
1528 
1529 	/*
1530 	 * Configure TX and RX DMA watermarks -- when to send a DMA request
1531 	 *
1532 	 * Values should be tested to avoid FIFO under/over run. Set maxburst
1533 	 * to fifo_watermark to maxiumize DMA transaction to reduce overhead.
1534 	 */
1535 	switch (ssi->fifo_depth) {
1536 	case 15:
1537 		/*
1538 		 * Set to 8 as a balanced configuration -- When TX FIFO has 8
1539 		 * empty slots, send a DMA request to fill these 8 slots. The
1540 		 * remaining 7 slots should be able to allow DMA to finish the
1541 		 * transaction before TX FIFO underruns; Same applies to RX.
1542 		 *
1543 		 * Tested with cases running at 48kHz @ 16 bits x 16 channels
1544 		 */
1545 		ssi->fifo_watermark = 8;
1546 		ssi->dma_maxburst = 8;
1547 		break;
1548 	case 8:
1549 	default:
1550 		/* Safely use old watermark configurations for older chips */
1551 		ssi->fifo_watermark = ssi->fifo_depth - 2;
1552 		ssi->dma_maxburst = ssi->fifo_depth - 2;
1553 		break;
1554 	}
1555 
1556 	dev_set_drvdata(dev, ssi);
1557 
1558 	if (ssi->soc->imx) {
1559 		ret = fsl_ssi_imx_probe(pdev, ssi, iomem);
1560 		if (ret)
1561 			return ret;
1562 	}
1563 
1564 	if (fsl_ssi_is_ac97(ssi)) {
1565 		mutex_init(&ssi->ac97_reg_lock);
1566 		ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1567 		if (ret) {
1568 			dev_err(dev, "failed to set AC'97 ops\n");
1569 			goto error_ac97_ops;
1570 		}
1571 	}
1572 
1573 	ret = devm_snd_soc_register_component(dev, &fsl_ssi_component,
1574 					      &ssi->cpu_dai_drv, 1);
1575 	if (ret) {
1576 		dev_err(dev, "failed to register DAI: %d\n", ret);
1577 		goto error_asoc_register;
1578 	}
1579 
1580 	if (ssi->use_dma) {
1581 		ret = devm_request_irq(dev, ssi->irq, fsl_ssi_isr, 0,
1582 				       dev_name(dev), ssi);
1583 		if (ret < 0) {
1584 			dev_err(dev, "failed to claim irq %u\n", ssi->irq);
1585 			goto error_asoc_register;
1586 		}
1587 	}
1588 
1589 	ret = fsl_ssi_debugfs_create(&ssi->dbg_stats, dev);
1590 	if (ret)
1591 		goto error_asoc_register;
1592 
1593 	/* Initially configures SSI registers */
1594 	fsl_ssi_hw_init(ssi);
1595 
1596 	/* Register a platform device for older bindings or AC97 */
1597 	if (ssi->card_name[0]) {
1598 		struct device *parent = dev;
1599 		/*
1600 		 * Do not set SSI dev as the parent of AC97 CODEC device since
1601 		 * it does not have a DT node. Otherwise ASoC core will assume
1602 		 * CODEC has the same DT node as the SSI, so it may bypass the
1603 		 * dai_probe() of SSI and then cause NULL DMA data pointers.
1604 		 */
1605 		if (fsl_ssi_is_ac97(ssi))
1606 			parent = NULL;
1607 
1608 		ssi->card_pdev = platform_device_register_data(parent,
1609 				ssi->card_name, ssi->card_idx, NULL, 0);
1610 		if (IS_ERR(ssi->card_pdev)) {
1611 			ret = PTR_ERR(ssi->card_pdev);
1612 			dev_err(dev, "failed to register %s: %d\n",
1613 				ssi->card_name, ret);
1614 			goto error_sound_card;
1615 		}
1616 	}
1617 
1618 	return 0;
1619 
1620 error_sound_card:
1621 	fsl_ssi_debugfs_remove(&ssi->dbg_stats);
1622 error_asoc_register:
1623 	if (fsl_ssi_is_ac97(ssi))
1624 		snd_soc_set_ac97_ops(NULL);
1625 error_ac97_ops:
1626 	if (fsl_ssi_is_ac97(ssi))
1627 		mutex_destroy(&ssi->ac97_reg_lock);
1628 
1629 	if (ssi->soc->imx)
1630 		fsl_ssi_imx_clean(pdev, ssi);
1631 
1632 	return ret;
1633 }
1634 
1635 static int fsl_ssi_remove(struct platform_device *pdev)
1636 {
1637 	struct fsl_ssi *ssi = dev_get_drvdata(&pdev->dev);
1638 
1639 	fsl_ssi_debugfs_remove(&ssi->dbg_stats);
1640 
1641 	if (ssi->card_pdev)
1642 		platform_device_unregister(ssi->card_pdev);
1643 
1644 	/* Clean up SSI registers */
1645 	fsl_ssi_hw_clean(ssi);
1646 
1647 	if (ssi->soc->imx)
1648 		fsl_ssi_imx_clean(pdev, ssi);
1649 
1650 	if (fsl_ssi_is_ac97(ssi)) {
1651 		snd_soc_set_ac97_ops(NULL);
1652 		mutex_destroy(&ssi->ac97_reg_lock);
1653 	}
1654 
1655 	return 0;
1656 }
1657 
1658 #ifdef CONFIG_PM_SLEEP
1659 static int fsl_ssi_suspend(struct device *dev)
1660 {
1661 	struct fsl_ssi *ssi = dev_get_drvdata(dev);
1662 	struct regmap *regs = ssi->regs;
1663 
1664 	regmap_read(regs, REG_SSI_SFCSR, &ssi->regcache_sfcsr);
1665 	regmap_read(regs, REG_SSI_SACNT, &ssi->regcache_sacnt);
1666 
1667 	regcache_cache_only(regs, true);
1668 	regcache_mark_dirty(regs);
1669 
1670 	return 0;
1671 }
1672 
1673 static int fsl_ssi_resume(struct device *dev)
1674 {
1675 	struct fsl_ssi *ssi = dev_get_drvdata(dev);
1676 	struct regmap *regs = ssi->regs;
1677 
1678 	regcache_cache_only(regs, false);
1679 
1680 	regmap_update_bits(regs, REG_SSI_SFCSR,
1681 			   SSI_SFCSR_RFWM1_MASK | SSI_SFCSR_TFWM1_MASK |
1682 			   SSI_SFCSR_RFWM0_MASK | SSI_SFCSR_TFWM0_MASK,
1683 			   ssi->regcache_sfcsr);
1684 	regmap_write(regs, REG_SSI_SACNT, ssi->regcache_sacnt);
1685 
1686 	return regcache_sync(regs);
1687 }
1688 #endif /* CONFIG_PM_SLEEP */
1689 
1690 static const struct dev_pm_ops fsl_ssi_pm = {
1691 	SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1692 };
1693 
1694 static struct platform_driver fsl_ssi_driver = {
1695 	.driver = {
1696 		.name = "fsl-ssi-dai",
1697 		.of_match_table = fsl_ssi_ids,
1698 		.pm = &fsl_ssi_pm,
1699 	},
1700 	.probe = fsl_ssi_probe,
1701 	.remove = fsl_ssi_remove,
1702 };
1703 
1704 module_platform_driver(fsl_ssi_driver);
1705 
1706 MODULE_ALIAS("platform:fsl-ssi-dai");
1707 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1708 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1709 MODULE_LICENSE("GPL v2");
1710