ASoC: fsl_ssi: call _fsl_ssi_set_dai_fmt() just once in AC'97 mode
[linux-2.6-block.git] / sound / soc / fsl / fsl_ssi.c
CommitLineData
17467f23
TT
1/*
2 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
f0fba2ad
LG
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.
de623ece
MP
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.
17467f23
TT
31 */
32
33#include <linux/init.h>
dfa1a107 34#include <linux/io.h>
17467f23
TT
35#include <linux/module.h>
36#include <linux/interrupt.h>
95cd98f9 37#include <linux/clk.h>
c6682fed 38#include <linux/ctype.h>
17467f23
TT
39#include <linux/device.h>
40#include <linux/delay.h>
b880b805 41#include <linux/mutex.h>
5a0e3ad6 42#include <linux/slab.h>
aafa85e7 43#include <linux/spinlock.h>
9c72a04c 44#include <linux/of.h>
dfa1a107
SG
45#include <linux/of_address.h>
46#include <linux/of_irq.h>
f0fba2ad 47#include <linux/of_platform.h>
17467f23 48
17467f23
TT
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>
a8909c9b 54#include <sound/dmaengine_pcm.h>
17467f23 55
17467f23 56#include "fsl_ssi.h"
09ce1111 57#include "imx-pcm.h"
17467f23 58
17467f23
TT
59/**
60 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
61 *
17467f23
TT
62 * The SSI has a limitation in that the samples must be in the same byte
63 * order as the host CPU. This is because when multiple bytes are written
64 * to the STX register, the bytes and bits must be written in the same
65 * order. The STX is a shift register, so all the bits need to be aligned
66 * (bit-endianness must match byte-endianness). Processors typically write
67 * the bits within a byte in the same order that the bytes of a word are
68 * written in. So if the host CPU is big-endian, then only big-endian
69 * samples will be written to STX properly.
70 */
71#ifdef __BIG_ENDIAN
72#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
73 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
74 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
75#else
76#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
77 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
78 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
79#endif
80
9368acc4
MP
81#define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
82 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
83 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
84#define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
85 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
86 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
c1953bfe
MP
87
88enum fsl_ssi_type {
89 FSL_SSI_MCP8610,
90 FSL_SSI_MX21,
0888efd1 91 FSL_SSI_MX35,
c1953bfe
MP
92 FSL_SSI_MX51,
93};
94
4e6ec0d9
MP
95struct fsl_ssi_reg_val {
96 u32 sier;
97 u32 srcr;
98 u32 stcr;
99 u32 scr;
100};
101
102struct fsl_ssi_rxtx_reg_val {
103 struct fsl_ssi_reg_val rx;
104 struct fsl_ssi_reg_val tx;
105};
05cf2379 106
05cf2379
ZW
107static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
108{
109 switch (reg) {
110 case CCSR_SSI_SACCEN:
111 case CCSR_SSI_SACCDIS:
112 return false;
113 default:
114 return true;
115 }
116}
117
118static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
119{
120 switch (reg) {
121 case CCSR_SSI_STX0:
122 case CCSR_SSI_STX1:
123 case CCSR_SSI_SRX0:
124 case CCSR_SSI_SRX1:
125 case CCSR_SSI_SISR:
126 case CCSR_SSI_SFCSR:
3f1c241f 127 case CCSR_SSI_SACNT:
05cf2379
ZW
128 case CCSR_SSI_SACADD:
129 case CCSR_SSI_SACDAT:
130 case CCSR_SSI_SATAG:
131 case CCSR_SSI_SACCST:
3cc6185b 132 case CCSR_SSI_SOR:
05cf2379
ZW
133 return true;
134 default:
135 return false;
136 }
137}
138
f51e3d53
MS
139static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
140{
141 switch (reg) {
142 case CCSR_SSI_SRX0:
143 case CCSR_SSI_SRX1:
144 case CCSR_SSI_SISR:
145 case CCSR_SSI_SACADD:
146 case CCSR_SSI_SACDAT:
147 case CCSR_SSI_SATAG:
148 return true;
149 default:
150 return false;
151 }
152}
153
05cf2379
ZW
154static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
155{
156 switch (reg) {
157 case CCSR_SSI_SRX0:
158 case CCSR_SSI_SRX1:
159 case CCSR_SSI_SACCST:
160 return false;
161 default:
162 return true;
163 }
164}
165
43248122
MP
166static const struct regmap_config fsl_ssi_regconfig = {
167 .max_register = CCSR_SSI_SACCDIS,
168 .reg_bits = 32,
169 .val_bits = 32,
170 .reg_stride = 4,
171 .val_format_endian = REGMAP_ENDIAN_NATIVE,
f26b3b2a 172 .num_reg_defaults_raw = CCSR_SSI_SACCDIS / sizeof(uint32_t) + 1,
05cf2379
ZW
173 .readable_reg = fsl_ssi_readable_reg,
174 .volatile_reg = fsl_ssi_volatile_reg,
f51e3d53 175 .precious_reg = fsl_ssi_precious_reg,
05cf2379 176 .writeable_reg = fsl_ssi_writeable_reg,
bfcf928d 177 .cache_type = REGCACHE_FLAT,
43248122 178};
d5a908b2 179
fcdbadef
SH
180struct fsl_ssi_soc_data {
181 bool imx;
6139b1b1 182 bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
fcdbadef
SH
183 bool offline_config;
184 u32 sisr_write_mask;
185};
186
17467f23
TT
187/**
188 * fsl_ssi_private: per-SSI private data
189 *
43248122 190 * @reg: Pointer to the regmap registers
17467f23 191 * @irq: IRQ of this SSI
737a6b41
MP
192 * @cpu_dai_drv: CPU DAI driver for this device
193 *
194 * @dai_fmt: DAI configuration this device is currently used with
195 * @i2s_mode: i2s and network mode configuration of the device. Is used to
196 * switch between normal and i2s/network mode
197 * mode depending on the number of channels
198 * @use_dma: DMA is used or FIQ with stream filter
199 * @use_dual_fifo: DMA with support for both FIFOs used
200 * @fifo_deph: Depth of the SSI FIFOs
b0a7043d
NC
201 * @slot_width: width of each DAI slot
202 * @slots: number of slots
737a6b41
MP
203 * @rxtx_reg_val: Specific register settings for receive/transmit configuration
204 *
205 * @clk: SSI clock
206 * @baudclk: SSI baud clock for master mode
207 * @baudclk_streams: Active streams that are using baudclk
737a6b41
MP
208 *
209 * @dma_params_tx: DMA transmit parameters
210 * @dma_params_rx: DMA receive parameters
211 * @ssi_phys: physical address of the SSI registers
212 *
213 * @fiq_params: FIQ stream filtering parameters
214 *
215 * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
216 *
217 * @dbg_stats: Debugging statistics
218 *
dcfcf2c2 219 * @soc: SoC specific data
4ee437fb
CC
220 *
221 * @fifo_watermark: the FIFO watermark setting. Notifies DMA when
222 * there are @fifo_watermark or fewer words in TX fifo or
223 * @fifo_watermark or more empty words in RX fifo.
224 * @dma_maxburst: max number of words to transfer in one go. So far,
225 * this is always the same as fifo_watermark.
17467f23
TT
226 */
227struct fsl_ssi_private {
43248122 228 struct regmap *regs;
9e446ad5 229 int irq;
f0fba2ad 230 struct snd_soc_dai_driver cpu_dai_drv;
17467f23 231
737a6b41
MP
232 unsigned int dai_fmt;
233 u8 i2s_mode;
de623ece 234 bool use_dma;
0da9e55e 235 bool use_dual_fifo;
f4a43cab 236 bool has_ipg_clk_name;
737a6b41 237 unsigned int fifo_depth;
b0a7043d
NC
238 unsigned int slot_width;
239 unsigned int slots;
737a6b41
MP
240 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
241
95cd98f9 242 struct clk *clk;
737a6b41 243 struct clk *baudclk;
d429d8e3 244 unsigned int baudclk_streams;
737a6b41 245
3f1c241f 246 /* regcache for volatile regs */
05cf2379 247 u32 regcache_sfcsr;
3f1c241f 248 u32 regcache_sacnt;
05cf2379 249
737a6b41 250 /* DMA params */
a8909c9b
LPC
251 struct snd_dmaengine_dai_dma_data dma_params_tx;
252 struct snd_dmaengine_dai_dma_data dma_params_rx;
737a6b41
MP
253 dma_addr_t ssi_phys;
254
255 /* params for non-dma FIQ stream filtered mode */
de623ece 256 struct imx_pcm_fiq_params fiq_params;
737a6b41
MP
257
258 /* Used when using fsl-ssi as sound-card. This is only used by ppc and
259 * should be replaced with simple-sound-card. */
260 struct platform_device *pdev;
09ce1111 261
f138e621 262 struct fsl_ssi_dbg dbg_stats;
17467f23 263
fcdbadef 264 const struct fsl_ssi_soc_data *soc;
0096b693 265 struct device *dev;
4ee437fb
CC
266
267 u32 fifo_watermark;
268 u32 dma_maxburst;
b880b805
MS
269
270 struct mutex ac97_reg_lock;
c1953bfe 271};
171d683d
MP
272
273/*
274 * imx51 and later SoCs have a slightly different IP that allows the
275 * SSI configuration while the SSI unit is running.
276 *
277 * More important, it is necessary on those SoCs to configure the
278 * sperate TX/RX DMA bits just before starting the stream
279 * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
280 * sends any DMA requests to the SDMA unit, otherwise it is not defined
281 * how the SDMA unit handles the DMA request.
282 *
283 * SDMA units are present on devices starting at imx35 but the imx35
284 * reference manual states that the DMA bits should not be changed
285 * while the SSI unit is running (SSIEN). So we support the necessary
286 * online configuration of fsl-ssi starting at imx51.
287 */
171d683d 288
fcdbadef
SH
289static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
290 .imx = false,
291 .offline_config = true,
292 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
293 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
294 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
295};
296
297static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
298 .imx = true,
6139b1b1 299 .imx21regs = true,
fcdbadef
SH
300 .offline_config = true,
301 .sisr_write_mask = 0,
302};
303
304static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
305 .imx = true,
306 .offline_config = true,
307 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
308 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
309 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
310};
311
312static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
313 .imx = true,
314 .offline_config = false,
315 .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
316 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
317};
318
319static const struct of_device_id fsl_ssi_ids[] = {
320 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
321 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
322 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
323 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
324 {}
325};
326MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
327
328static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
329{
5b64c173
AT
330 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
331 SND_SOC_DAIFMT_AC97;
171d683d
MP
332}
333
8dd51e23
SH
334static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
335{
336 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
337 SND_SOC_DAIFMT_CBS_CFS;
338}
339
cf4f7fc3
FF
340static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
341{
342 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
343 SND_SOC_DAIFMT_CBM_CFS;
344}
17467f23
TT
345/**
346 * fsl_ssi_isr: SSI interrupt handler
347 *
348 * Although it's possible to use the interrupt handler to send and receive
349 * data to/from the SSI, we use the DMA instead. Programming is more
350 * complicated, but the performance is much better.
351 *
352 * This interrupt handler is used only to gather statistics.
353 *
354 * @irq: IRQ of the SSI device
355 * @dev_id: pointer to the ssi_private structure for this SSI device
356 */
357static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
358{
359 struct fsl_ssi_private *ssi_private = dev_id;
43248122 360 struct regmap *regs = ssi_private->regs;
17467f23 361 __be32 sisr;
0888efd1 362 __be32 sisr2;
17467f23
TT
363
364 /* We got an interrupt, so read the status register to see what we
365 were interrupted for. We mask it with the Interrupt Enable register
366 so that we only check for events that we're interested in.
367 */
43248122 368 regmap_read(regs, CCSR_SSI_SISR, &sisr);
17467f23 369
fcdbadef 370 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
17467f23
TT
371 /* Clear the bits that we set */
372 if (sisr2)
43248122 373 regmap_write(regs, CCSR_SSI_SISR, sisr2);
17467f23 374
f138e621 375 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
9368acc4 376
f138e621 377 return IRQ_HANDLED;
9368acc4
MP
378}
379
4e6ec0d9
MP
380/*
381 * Enable/Disable all rx/tx config flags at once.
382 */
383static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
384 bool enable)
385{
43248122 386 struct regmap *regs = ssi_private->regs;
4e6ec0d9
MP
387 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
388
389 if (enable) {
43248122
MP
390 regmap_update_bits(regs, CCSR_SSI_SIER,
391 vals->rx.sier | vals->tx.sier,
392 vals->rx.sier | vals->tx.sier);
393 regmap_update_bits(regs, CCSR_SSI_SRCR,
394 vals->rx.srcr | vals->tx.srcr,
395 vals->rx.srcr | vals->tx.srcr);
396 regmap_update_bits(regs, CCSR_SSI_STCR,
397 vals->rx.stcr | vals->tx.stcr,
398 vals->rx.stcr | vals->tx.stcr);
4e6ec0d9 399 } else {
43248122
MP
400 regmap_update_bits(regs, CCSR_SSI_SRCR,
401 vals->rx.srcr | vals->tx.srcr, 0);
402 regmap_update_bits(regs, CCSR_SSI_STCR,
403 vals->rx.stcr | vals->tx.stcr, 0);
404 regmap_update_bits(regs, CCSR_SSI_SIER,
405 vals->rx.sier | vals->tx.sier, 0);
4e6ec0d9
MP
406 }
407}
408
027db2e1
AM
409/*
410 * Clear RX or TX FIFO to remove samples from the previous
411 * stream session which may be still present in the FIFO and
412 * may introduce bad samples and/or channel slipping.
413 *
414 * Note: The SOR is not documented in recent IMX datasheet, but
415 * is described in IMX51 reference manual at section 56.3.3.15.
416 */
417static void fsl_ssi_fifo_clear(struct fsl_ssi_private *ssi_private,
418 bool is_rx)
419{
420 if (is_rx) {
421 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
422 CCSR_SSI_SOR_RX_CLR, CCSR_SSI_SOR_RX_CLR);
423 } else {
424 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
425 CCSR_SSI_SOR_TX_CLR, CCSR_SSI_SOR_TX_CLR);
426 }
427}
428
65c961cc
MP
429/*
430 * Calculate the bits that have to be disabled for the current stream that is
431 * getting disabled. This keeps the bits enabled that are necessary for the
432 * second stream to work if 'stream_active' is true.
433 *
434 * Detailed calculation:
435 * These are the values that need to be active after disabling. For non-active
436 * second stream, this is 0:
437 * vals_stream * !!stream_active
438 *
439 * The following computes the overall differences between the setup for the
440 * to-disable stream and the active stream, a simple XOR:
441 * vals_disable ^ (vals_stream * !!(stream_active))
442 *
443 * The full expression adds a mask on all values we care about
444 */
445#define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
446 ((vals_disable) & \
447 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
448
4e6ec0d9
MP
449/*
450 * Enable/Disable a ssi configuration. You have to pass either
451 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
452 */
453static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
454 struct fsl_ssi_reg_val *vals)
455{
43248122 456 struct regmap *regs = ssi_private->regs;
4e6ec0d9 457 struct fsl_ssi_reg_val *avals;
43248122
MP
458 int nr_active_streams;
459 u32 scr_val;
65c961cc
MP
460 int keep_active;
461
43248122
MP
462 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
463
464 nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
465 !!(scr_val & CCSR_SSI_SCR_RE);
466
65c961cc
MP
467 if (nr_active_streams - 1 > 0)
468 keep_active = 1;
469 else
470 keep_active = 0;
4e6ec0d9
MP
471
472 /* Find the other direction values rx or tx which we do not want to
473 * modify */
474 if (&ssi_private->rxtx_reg_val.rx == vals)
475 avals = &ssi_private->rxtx_reg_val.tx;
476 else
477 avals = &ssi_private->rxtx_reg_val.rx;
478
479 /* If vals should be disabled, start with disabling the unit */
480 if (!enable) {
65c961cc
MP
481 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
482 keep_active);
43248122 483 regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
4e6ec0d9
MP
484 }
485
486 /*
487 * We are running on a SoC which does not support online SSI
488 * reconfiguration, so we have to enable all necessary flags at once
489 * even if we do not use them later (capture and playback configuration)
490 */
fcdbadef 491 if (ssi_private->soc->offline_config) {
4e6ec0d9 492 if ((enable && !nr_active_streams) ||
65c961cc 493 (!enable && !keep_active))
4e6ec0d9
MP
494 fsl_ssi_rxtx_config(ssi_private, enable);
495
496 goto config_done;
497 }
498
499 /*
500 * Configure single direction units while the SSI unit is running
501 * (online configuration)
502 */
503 if (enable) {
027db2e1
AM
504 fsl_ssi_fifo_clear(ssi_private, vals->scr & CCSR_SSI_SCR_RE);
505
43248122
MP
506 regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
507 regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
d9f2a202 508 regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
4e6ec0d9
MP
509 } else {
510 u32 sier;
511 u32 srcr;
512 u32 stcr;
513
514 /*
515 * Disabling the necessary flags for one of rx/tx while the
516 * other stream is active is a little bit more difficult. We
517 * have to disable only those flags that differ between both
518 * streams (rx XOR tx) and that are set in the stream that is
519 * disabled now. Otherwise we could alter flags of the other
520 * stream
521 */
522
523 /* These assignments are simply vals without bits set in avals*/
65c961cc
MP
524 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
525 keep_active);
526 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
527 keep_active);
528 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
529 keep_active);
4e6ec0d9 530
43248122
MP
531 regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
532 regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
533 regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
4e6ec0d9
MP
534 }
535
536config_done:
537 /* Enabling of subunits is done after configuration */
61fcf10a
AM
538 if (enable) {
539 if (ssi_private->use_dma && (vals->scr & CCSR_SSI_SCR_TE)) {
540 /*
541 * Be sure the Tx FIFO is filled when TE is set.
542 * Otherwise, there are some chances to start the
543 * playback with some void samples inserted first,
544 * generating a channel slip.
545 *
546 * First, SSIEN must be set, to let the FIFO be filled.
547 *
548 * Notes:
549 * - Limit this fix to the DMA case until FIQ cases can
550 * be tested.
551 * - Limit the length of the busy loop to not lock the
552 * system too long, even if 1-2 loops are sufficient
553 * in general.
554 */
555 int i;
556 int max_loop = 100;
557 regmap_update_bits(regs, CCSR_SSI_SCR,
558 CCSR_SSI_SCR_SSIEN, CCSR_SSI_SCR_SSIEN);
559 for (i = 0; i < max_loop; i++) {
560 u32 sfcsr;
561 regmap_read(regs, CCSR_SSI_SFCSR, &sfcsr);
562 if (CCSR_SSI_SFCSR_TFCNT0(sfcsr))
563 break;
564 }
565 if (i == max_loop) {
566 dev_err(ssi_private->dev,
567 "Timeout waiting TX FIFO filling\n");
568 }
569 }
43248122 570 regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
61fcf10a 571 }
4e6ec0d9
MP
572}
573
574
575static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
576{
577 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
578}
579
580static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
581{
582 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
583}
584
6de83879
MP
585/*
586 * Setup rx/tx register values used to enable/disable the streams. These will
587 * be used later in fsl_ssi_config to setup the streams without the need to
588 * check for all different SSI modes.
589 */
590static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
591{
592 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
593
594 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
595 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
596 reg->rx.scr = 0;
597 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
598 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
599 reg->tx.scr = 0;
600
171d683d 601 if (!fsl_ssi_is_ac97(ssi_private)) {
6de83879 602 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
6de83879 603 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
6de83879
MP
604 }
605
606 if (ssi_private->use_dma) {
607 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
608 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
609 } else {
610 reg->rx.sier |= CCSR_SSI_SIER_RIE;
611 reg->tx.sier |= CCSR_SSI_SIER_TIE;
612 }
613
614 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
615 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
616}
617
d8764646
MP
618static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
619{
43248122 620 struct regmap *regs = ssi_private->regs;
d8764646
MP
621
622 /*
623 * Setup the clock control register
624 */
43248122
MP
625 regmap_write(regs, CCSR_SSI_STCCR,
626 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
627 regmap_write(regs, CCSR_SSI_SRCCR,
628 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
d8764646
MP
629
630 /*
631 * Enable AC97 mode and startup the SSI
632 */
43248122
MP
633 regmap_write(regs, CCSR_SSI_SACNT,
634 CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
6139b1b1
MS
635
636 /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
637 if (!ssi_private->soc->imx21regs) {
638 regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
639 regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
640 }
d8764646
MP
641
642 /*
643 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
644 * codec before a stream is started.
645 */
43248122
MP
646 regmap_update_bits(regs, CCSR_SSI_SCR,
647 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
648 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
d8764646 649
43248122 650 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
d8764646
MP
651}
652
17467f23
TT
653/**
654 * fsl_ssi_startup: create a new substream
655 *
656 * This is the first function called when a stream is opened.
657 *
658 * If this is the first stream open, then grab the IRQ and program most of
659 * the SSI registers.
660 */
dee89c4d
MB
661static int fsl_ssi_startup(struct snd_pcm_substream *substream,
662 struct snd_soc_dai *dai)
17467f23
TT
663{
664 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5e538eca
TT
665 struct fsl_ssi_private *ssi_private =
666 snd_soc_dai_get_drvdata(rtd->cpu_dai);
f4a43cab
SW
667 int ret;
668
669 ret = clk_prepare_enable(ssi_private->clk);
670 if (ret)
671 return ret;
17467f23 672
0da9e55e
NC
673 /* When using dual fifo mode, it is safer to ensure an even period
674 * size. If appearing to an odd number while DMA always starts its
675 * task from fifo0, fifo1 would be neglected at the end of each
676 * period. But SSI would still access fifo1 with an invalid data.
677 */
678 if (ssi_private->use_dual_fifo)
679 snd_pcm_hw_constraint_step(substream->runtime, 0,
680 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
681
17467f23
TT
682 return 0;
683}
684
f4a43cab
SW
685/**
686 * fsl_ssi_shutdown: shutdown the SSI
687 *
688 */
689static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
690 struct snd_soc_dai *dai)
691{
692 struct snd_soc_pcm_runtime *rtd = substream->private_data;
693 struct fsl_ssi_private *ssi_private =
694 snd_soc_dai_get_drvdata(rtd->cpu_dai);
695
696 clk_disable_unprepare(ssi_private->clk);
697
698}
699
ee9daad4 700/**
8dd51e23 701 * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
ee9daad4
SH
702 *
703 * Note: This function can be only called when using SSI as DAI master
704 *
705 * Quick instruction for parameters:
b0a7043d
NC
706 * freq: Output BCLK frequency = samplerate * slots * slot_width
707 * (In 2-channel I2S Master mode, slot_width is fixed 32)
ee9daad4 708 */
8dd51e23
SH
709static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
710 struct snd_soc_dai *cpu_dai,
711 struct snd_pcm_hw_params *hw_params)
ee9daad4
SH
712{
713 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
43248122 714 struct regmap *regs = ssi_private->regs;
ee9daad4
SH
715 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
716 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
d8ced479 717 unsigned long clkrate, baudrate, tmprate;
b0a7043d
NC
718 unsigned int slots = params_channels(hw_params);
719 unsigned int slot_width = 32;
ee9daad4 720 u64 sub, savesub = 100000;
8dd51e23 721 unsigned int freq;
d429d8e3 722 bool baudclk_is_used;
8dd51e23 723
b0a7043d
NC
724 /* Override slots and slot_width if being specifically set... */
725 if (ssi_private->slots)
726 slots = ssi_private->slots;
727 /* ...but keep 32 bits if slots is 2 -- I2S Master mode */
728 if (ssi_private->slot_width && slots != 2)
729 slot_width = ssi_private->slot_width;
730
731 /* Generate bit clock based on the slot number and slot width */
732 freq = slots * slot_width * params_rate(hw_params);
ee9daad4
SH
733
734 /* Don't apply it to any non-baudclk circumstance */
735 if (IS_ERR(ssi_private->baudclk))
736 return -EINVAL;
737
e09745f2
AM
738 /*
739 * Hardware limitation: The bclk rate must be
740 * never greater than 1/5 IPG clock rate
741 */
742 if (freq * 5 > clk_get_rate(ssi_private->clk)) {
743 dev_err(cpu_dai->dev, "bitclk > ipgclk/5\n");
744 return -EINVAL;
745 }
746
d429d8e3
MP
747 baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
748
ee9daad4
SH
749 /* It should be already enough to divide clock by setting pm alone */
750 psr = 0;
751 div2 = 0;
752
753 factor = (div2 + 1) * (7 * psr + 1) * 2;
754
755 for (i = 0; i < 255; i++) {
6c8ca30e 756 tmprate = freq * factor * (i + 1);
d429d8e3
MP
757
758 if (baudclk_is_used)
759 clkrate = clk_get_rate(ssi_private->baudclk);
760 else
761 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
ee9daad4 762
acf2c60a
TT
763 clkrate /= factor;
764 afreq = clkrate / (i + 1);
ee9daad4
SH
765
766 if (freq == afreq)
767 sub = 0;
768 else if (freq / afreq == 1)
769 sub = freq - afreq;
770 else if (afreq / freq == 1)
771 sub = afreq - freq;
772 else
773 continue;
774
775 /* Calculate the fraction */
776 sub *= 100000;
777 do_div(sub, freq);
778
ebac95a9 779 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
ee9daad4
SH
780 baudrate = tmprate;
781 savesub = sub;
782 pm = i;
783 }
784
785 /* We are lucky */
786 if (savesub == 0)
787 break;
788 }
789
790 /* No proper pm found if it is still remaining the initial value */
791 if (pm == 999) {
792 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
793 return -EINVAL;
794 }
795
796 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
797 (psr ? CCSR_SSI_SxCCR_PSR : 0);
798 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
799 CCSR_SSI_SxCCR_PSR;
800
8dd51e23 801 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
43248122 802 regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
ee9daad4 803 else
43248122 804 regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
ee9daad4 805
d429d8e3 806 if (!baudclk_is_used) {
ee9daad4
SH
807 ret = clk_set_rate(ssi_private->baudclk, baudrate);
808 if (ret) {
ee9daad4
SH
809 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
810 return -EINVAL;
811 }
ee9daad4 812 }
ee9daad4
SH
813
814 return 0;
815}
816
17467f23 817/**
85ef2375 818 * fsl_ssi_hw_params - program the sample size
17467f23
TT
819 *
820 * Most of the SSI registers have been programmed in the startup function,
821 * but the word length must be programmed here. Unfortunately, programming
822 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
823 * cause a problem with supporting simultaneous playback and capture. If
824 * the SSI is already playing a stream, then that stream may be temporarily
825 * stopped when you start capture.
826 *
827 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
828 * clock master.
829 */
85ef2375
TT
830static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
831 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
17467f23 832{
f0fba2ad 833 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
43248122 834 struct regmap *regs = ssi_private->regs;
2924a998 835 unsigned int channels = params_channels(hw_params);
4ca73043 836 unsigned int sample_size = params_width(hw_params);
5e538eca 837 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
8dd51e23 838 int ret;
43248122
MP
839 u32 scr_val;
840 int enabled;
841
842 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
843 enabled = scr_val & CCSR_SSI_SCR_SSIEN;
17467f23 844
5e538eca
TT
845 /*
846 * If we're in synchronous mode, and the SSI is already enabled,
847 * then STCCR is already set properly.
848 */
849 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
850 return 0;
17467f23 851
8dd51e23
SH
852 if (fsl_ssi_is_i2s_master(ssi_private)) {
853 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
854 if (ret)
855 return ret;
d429d8e3
MP
856
857 /* Do not enable the clock if it is already enabled */
858 if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
859 ret = clk_prepare_enable(ssi_private->baudclk);
860 if (ret)
861 return ret;
862
863 ssi_private->baudclk_streams |= BIT(substream->stream);
864 }
8dd51e23
SH
865 }
866
cf4f7fc3
FF
867 if (!fsl_ssi_is_ac97(ssi_private)) {
868 u8 i2smode;
869 /*
870 * Switch to normal net mode in order to have a frame sync
871 * signal every 32 bits instead of 16 bits
872 */
873 if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
874 i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
875 CCSR_SSI_SCR_NET;
876 else
877 i2smode = ssi_private->i2s_mode;
878
879 regmap_update_bits(regs, CCSR_SSI_SCR,
880 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
881 channels == 1 ? 0 : i2smode);
882 }
883
5e538eca
TT
884 /*
885 * FIXME: The documentation says that SxCCR[WL] should not be
886 * modified while the SSI is enabled. The only time this can
887 * happen is if we're trying to do simultaneous playback and
888 * capture in asynchronous mode. Unfortunately, I have been enable
889 * to get that to work at all on the P1022DS. Therefore, we don't
890 * bother to disable/enable the SSI when setting SxCCR[WL], because
891 * the SSI will stop anyway. Maybe one day, this will get fixed.
892 */
17467f23 893
5e538eca
TT
894 /* In synchronous mode, the SSI uses STCCR for capture */
895 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
896 ssi_private->cpu_dai_drv.symmetric_rates)
43248122
MP
897 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
898 wl);
5e538eca 899 else
43248122
MP
900 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
901 wl);
17467f23
TT
902
903 return 0;
904}
905
d429d8e3
MP
906static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
907 struct snd_soc_dai *cpu_dai)
908{
909 struct snd_soc_pcm_runtime *rtd = substream->private_data;
910 struct fsl_ssi_private *ssi_private =
911 snd_soc_dai_get_drvdata(rtd->cpu_dai);
912
913 if (fsl_ssi_is_i2s_master(ssi_private) &&
914 ssi_private->baudclk_streams & BIT(substream->stream)) {
915 clk_disable_unprepare(ssi_private->baudclk);
916 ssi_private->baudclk_streams &= ~BIT(substream->stream);
917 }
918
919 return 0;
920}
921
85151461
MT
922static int _fsl_ssi_set_dai_fmt(struct device *dev,
923 struct fsl_ssi_private *ssi_private,
924 unsigned int fmt)
aafa85e7 925{
43248122 926 struct regmap *regs = ssi_private->regs;
aafa85e7 927 u32 strcr = 0, stcr, srcr, scr, mask;
2b0db996
MP
928 u8 wm;
929
171d683d
MP
930 ssi_private->dai_fmt = fmt;
931
d429d8e3 932 if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
85151461 933 dev_err(dev, "baudclk is missing which is necessary for master mode\n");
d429d8e3
MP
934 return -EINVAL;
935 }
936
2b0db996 937 fsl_ssi_setup_reg_vals(ssi_private);
aafa85e7 938
43248122
MP
939 regmap_read(regs, CCSR_SSI_SCR, &scr);
940 scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
50489479 941 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
aafa85e7
NC
942
943 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
944 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
945 CCSR_SSI_STCR_TEFS;
43248122
MP
946 regmap_read(regs, CCSR_SSI_STCR, &stcr);
947 regmap_read(regs, CCSR_SSI_SRCR, &srcr);
948 stcr &= ~mask;
949 srcr &= ~mask;
aafa85e7 950
07a28dbe 951 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
aafa85e7
NC
952 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
953 case SND_SOC_DAIFMT_I2S:
4f14f5c1
AS
954 regmap_update_bits(regs, CCSR_SSI_STCCR,
955 CCSR_SSI_SxCCR_DC_MASK,
956 CCSR_SSI_SxCCR_DC(2));
957 regmap_update_bits(regs, CCSR_SSI_SRCCR,
958 CCSR_SSI_SxCCR_DC_MASK,
959 CCSR_SSI_SxCCR_DC(2));
aafa85e7 960 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
cf4f7fc3 961 case SND_SOC_DAIFMT_CBM_CFS:
aafa85e7 962 case SND_SOC_DAIFMT_CBS_CFS:
07a28dbe 963 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
aafa85e7
NC
964 break;
965 case SND_SOC_DAIFMT_CBM_CFM:
07a28dbe 966 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
aafa85e7
NC
967 break;
968 default:
969 return -EINVAL;
970 }
aafa85e7
NC
971
972 /* Data on rising edge of bclk, frame low, 1clk before data */
973 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
974 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
975 break;
976 case SND_SOC_DAIFMT_LEFT_J:
977 /* Data on rising edge of bclk, frame high */
978 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
979 break;
980 case SND_SOC_DAIFMT_DSP_A:
981 /* Data on rising edge of bclk, frame high, 1clk before data */
982 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
983 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
984 break;
985 case SND_SOC_DAIFMT_DSP_B:
986 /* Data on rising edge of bclk, frame high */
987 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
988 CCSR_SSI_STCR_TXBIT0;
989 break;
2b0db996 990 case SND_SOC_DAIFMT_AC97:
07a28dbe 991 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
2b0db996 992 break;
aafa85e7
NC
993 default:
994 return -EINVAL;
995 }
2b0db996 996 scr |= ssi_private->i2s_mode;
aafa85e7
NC
997
998 /* DAI clock inversion */
999 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1000 case SND_SOC_DAIFMT_NB_NF:
1001 /* Nothing to do for both normal cases */
1002 break;
1003 case SND_SOC_DAIFMT_IB_NF:
1004 /* Invert bit clock */
1005 strcr ^= CCSR_SSI_STCR_TSCKP;
1006 break;
1007 case SND_SOC_DAIFMT_NB_IF:
1008 /* Invert frame clock */
1009 strcr ^= CCSR_SSI_STCR_TFSI;
1010 break;
1011 case SND_SOC_DAIFMT_IB_IF:
1012 /* Invert both clocks */
1013 strcr ^= CCSR_SSI_STCR_TSCKP;
1014 strcr ^= CCSR_SSI_STCR_TFSI;
1015 break;
1016 default:
1017 return -EINVAL;
1018 }
1019
1020 /* DAI clock master masks */
1021 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1022 case SND_SOC_DAIFMT_CBS_CFS:
1023 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
1024 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
1025 break;
1026 case SND_SOC_DAIFMT_CBM_CFM:
1027 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1028 break;
cf4f7fc3
FF
1029 case SND_SOC_DAIFMT_CBM_CFS:
1030 strcr &= ~CCSR_SSI_STCR_TXDIR;
1031 strcr |= CCSR_SSI_STCR_TFDIR;
1032 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1033 break;
aafa85e7 1034 default:
dce0332c
MS
1035 if (!fsl_ssi_is_ac97(ssi_private))
1036 return -EINVAL;
aafa85e7
NC
1037 }
1038
1039 stcr |= strcr;
1040 srcr |= strcr;
1041
dce0332c
MS
1042 if (ssi_private->cpu_dai_drv.symmetric_rates
1043 || fsl_ssi_is_ac97(ssi_private)) {
1044 /* Need to clear RXDIR when using SYNC or AC97 mode */
aafa85e7
NC
1045 srcr &= ~CCSR_SSI_SRCR_RXDIR;
1046 scr |= CCSR_SSI_SCR_SYN;
1047 }
1048
43248122
MP
1049 regmap_write(regs, CCSR_SSI_STCR, stcr);
1050 regmap_write(regs, CCSR_SSI_SRCR, srcr);
1051 regmap_write(regs, CCSR_SSI_SCR, scr);
aafa85e7 1052
4ee437fb 1053 wm = ssi_private->fifo_watermark;
2b0db996 1054
43248122
MP
1055 regmap_write(regs, CCSR_SSI_SFCSR,
1056 CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
1057 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
2b0db996
MP
1058
1059 if (ssi_private->use_dual_fifo) {
43248122 1060 regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
2b0db996 1061 CCSR_SSI_SRCR_RFEN1);
43248122 1062 regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
2b0db996 1063 CCSR_SSI_STCR_TFEN1);
43248122 1064 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
2b0db996
MP
1065 CCSR_SSI_SCR_TCH_EN);
1066 }
1067
5b64c173 1068 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_AC97)
2b0db996
MP
1069 fsl_ssi_setup_ac97(ssi_private);
1070
aafa85e7 1071 return 0;
85e59af2
MP
1072
1073}
1074
1075/**
1076 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
1077 */
1078static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
1079{
1080 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1081
c997a92a
MS
1082 if (fsl_ssi_is_ac97(ssi_private))
1083 return 0;
1084
85151461 1085 return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
aafa85e7
NC
1086}
1087
aafa85e7
NC
1088/**
1089 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
1090 *
1091 * Note: This function can be only called when using SSI as DAI master
1092 */
1093static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
1094 u32 rx_mask, int slots, int slot_width)
1095{
1096 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
43248122 1097 struct regmap *regs = ssi_private->regs;
aafa85e7
NC
1098 u32 val;
1099
b0a7043d
NC
1100 /* The word length should be 8, 10, 12, 16, 18, 20, 22 or 24 */
1101 if (slot_width & 1 || slot_width < 8 || slot_width > 24) {
1102 dev_err(cpu_dai->dev, "invalid slot width: %d\n", slot_width);
1103 return -EINVAL;
1104 }
1105
aafa85e7 1106 /* The slot number should be >= 2 if using Network mode or I2S mode */
43248122
MP
1107 regmap_read(regs, CCSR_SSI_SCR, &val);
1108 val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
aafa85e7
NC
1109 if (val && slots < 2) {
1110 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
1111 return -EINVAL;
1112 }
1113
43248122 1114 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
aafa85e7 1115 CCSR_SSI_SxCCR_DC(slots));
43248122 1116 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
aafa85e7
NC
1117 CCSR_SSI_SxCCR_DC(slots));
1118
1119 /* The register SxMSKs needs SSI to provide essential clock due to
1120 * hardware design. So we here temporarily enable SSI to set them.
1121 */
43248122
MP
1122 regmap_read(regs, CCSR_SSI_SCR, &val);
1123 val &= CCSR_SSI_SCR_SSIEN;
1124 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
1125 CCSR_SSI_SCR_SSIEN);
aafa85e7 1126
d0077aaf
LPC
1127 regmap_write(regs, CCSR_SSI_STMSK, ~tx_mask);
1128 regmap_write(regs, CCSR_SSI_SRMSK, ~rx_mask);
aafa85e7 1129
43248122 1130 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
aafa85e7 1131
b0a7043d
NC
1132 ssi_private->slot_width = slot_width;
1133 ssi_private->slots = slots;
1134
aafa85e7
NC
1135 return 0;
1136}
1137
17467f23
TT
1138/**
1139 * fsl_ssi_trigger: start and stop the DMA transfer.
1140 *
1141 * This function is called by ALSA to start, stop, pause, and resume the DMA
1142 * transfer of data.
1143 *
1144 * The DMA channel is in external master start and pause mode, which
1145 * means the SSI completely controls the flow of data.
1146 */
dee89c4d
MB
1147static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1148 struct snd_soc_dai *dai)
17467f23
TT
1149{
1150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 1151 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
43248122 1152 struct regmap *regs = ssi_private->regs;
9b443e3d 1153
17467f23
TT
1154 switch (cmd) {
1155 case SNDRV_PCM_TRIGGER_START:
b20e53a8 1156 case SNDRV_PCM_TRIGGER_RESUME:
17467f23 1157 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
a4d11fe5 1158 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6de83879 1159 fsl_ssi_tx_config(ssi_private, true);
a4d11fe5 1160 else
6de83879 1161 fsl_ssi_rx_config(ssi_private, true);
17467f23
TT
1162 break;
1163
1164 case SNDRV_PCM_TRIGGER_STOP:
b20e53a8 1165 case SNDRV_PCM_TRIGGER_SUSPEND:
17467f23
TT
1166 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6de83879 1168 fsl_ssi_tx_config(ssi_private, false);
17467f23 1169 else
6de83879 1170 fsl_ssi_rx_config(ssi_private, false);
17467f23
TT
1171 break;
1172
1173 default:
1174 return -EINVAL;
1175 }
1176
171d683d 1177 if (fsl_ssi_is_ac97(ssi_private)) {
a5a7ee7c 1178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
43248122 1179 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
a5a7ee7c 1180 else
43248122 1181 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
a5a7ee7c 1182 }
9b443e3d 1183
17467f23
TT
1184 return 0;
1185}
1186
fc8ba7f9
LPC
1187static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1188{
1189 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
1190
fcdbadef 1191 if (ssi_private->soc->imx && ssi_private->use_dma) {
fc8ba7f9
LPC
1192 dai->playback_dma_data = &ssi_private->dma_params_tx;
1193 dai->capture_dma_data = &ssi_private->dma_params_rx;
1194 }
1195
1196 return 0;
1197}
1198
85e7652d 1199static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
6335d055 1200 .startup = fsl_ssi_startup,
f4a43cab 1201 .shutdown = fsl_ssi_shutdown,
6335d055 1202 .hw_params = fsl_ssi_hw_params,
d429d8e3 1203 .hw_free = fsl_ssi_hw_free,
aafa85e7 1204 .set_fmt = fsl_ssi_set_dai_fmt,
aafa85e7 1205 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
6335d055 1206 .trigger = fsl_ssi_trigger,
6335d055
EM
1207};
1208
f0fba2ad
LG
1209/* Template for the CPU dai driver structure */
1210static struct snd_soc_dai_driver fsl_ssi_dai_template = {
fc8ba7f9 1211 .probe = fsl_ssi_dai_probe,
17467f23 1212 .playback = {
e3655004 1213 .stream_name = "CPU-Playback",
2924a998 1214 .channels_min = 1,
48a260ee 1215 .channels_max = 32,
58055677 1216 .rates = SNDRV_PCM_RATE_CONTINUOUS,
17467f23
TT
1217 .formats = FSLSSI_I2S_FORMATS,
1218 },
1219 .capture = {
e3655004 1220 .stream_name = "CPU-Capture",
2924a998 1221 .channels_min = 1,
48a260ee 1222 .channels_max = 32,
58055677 1223 .rates = SNDRV_PCM_RATE_CONTINUOUS,
17467f23
TT
1224 .formats = FSLSSI_I2S_FORMATS,
1225 },
6335d055 1226 .ops = &fsl_ssi_dai_ops,
17467f23
TT
1227};
1228
3580aa10
KM
1229static const struct snd_soc_component_driver fsl_ssi_component = {
1230 .name = "fsl-ssi",
1231};
1232
cd7f0295 1233static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
bc263214 1234 .bus_control = true,
793e3e9e 1235 .probe = fsl_ssi_dai_probe,
cd7f0295
MP
1236 .playback = {
1237 .stream_name = "AC97 Playback",
1238 .channels_min = 2,
1239 .channels_max = 2,
1240 .rates = SNDRV_PCM_RATE_8000_48000,
1241 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1242 },
1243 .capture = {
1244 .stream_name = "AC97 Capture",
1245 .channels_min = 2,
1246 .channels_max = 2,
1247 .rates = SNDRV_PCM_RATE_48000,
1248 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1249 },
a5a7ee7c 1250 .ops = &fsl_ssi_dai_ops,
cd7f0295
MP
1251};
1252
1253
1254static struct fsl_ssi_private *fsl_ac97_data;
1255
a851a2bb 1256static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
cd7f0295
MP
1257 unsigned short val)
1258{
43248122 1259 struct regmap *regs = fsl_ac97_data->regs;
cd7f0295
MP
1260 unsigned int lreg;
1261 unsigned int lval;
8277df3c 1262 int ret;
cd7f0295
MP
1263
1264 if (reg > 0x7f)
1265 return;
1266
b880b805
MS
1267 mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1268
8277df3c
MS
1269 ret = clk_prepare_enable(fsl_ac97_data->clk);
1270 if (ret) {
1271 pr_err("ac97 write clk_prepare_enable failed: %d\n",
1272 ret);
b880b805 1273 goto ret_unlock;
8277df3c 1274 }
cd7f0295
MP
1275
1276 lreg = reg << 12;
43248122 1277 regmap_write(regs, CCSR_SSI_SACADD, lreg);
cd7f0295
MP
1278
1279 lval = val << 4;
43248122 1280 regmap_write(regs, CCSR_SSI_SACDAT, lval);
cd7f0295 1281
43248122 1282 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
cd7f0295
MP
1283 CCSR_SSI_SACNT_WR);
1284 udelay(100);
8277df3c
MS
1285
1286 clk_disable_unprepare(fsl_ac97_data->clk);
b880b805
MS
1287
1288ret_unlock:
1289 mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
cd7f0295
MP
1290}
1291
a851a2bb 1292static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
cd7f0295
MP
1293 unsigned short reg)
1294{
43248122 1295 struct regmap *regs = fsl_ac97_data->regs;
cd7f0295 1296
b880b805 1297 unsigned short val = 0;
43248122 1298 u32 reg_val;
cd7f0295 1299 unsigned int lreg;
8277df3c
MS
1300 int ret;
1301
b880b805
MS
1302 mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1303
8277df3c
MS
1304 ret = clk_prepare_enable(fsl_ac97_data->clk);
1305 if (ret) {
1306 pr_err("ac97 read clk_prepare_enable failed: %d\n",
1307 ret);
b880b805 1308 goto ret_unlock;
8277df3c 1309 }
cd7f0295
MP
1310
1311 lreg = (reg & 0x7f) << 12;
43248122
MP
1312 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1313 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
cd7f0295
MP
1314 CCSR_SSI_SACNT_RD);
1315
1316 udelay(100);
1317
43248122
MP
1318 regmap_read(regs, CCSR_SSI_SACDAT, &reg_val);
1319 val = (reg_val >> 4) & 0xffff;
cd7f0295 1320
8277df3c
MS
1321 clk_disable_unprepare(fsl_ac97_data->clk);
1322
b880b805
MS
1323ret_unlock:
1324 mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
cd7f0295
MP
1325 return val;
1326}
1327
1328static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1329 .read = fsl_ssi_ac97_read,
1330 .write = fsl_ssi_ac97_write,
1331};
1332
17467f23 1333/**
f0fba2ad 1334 * Make every character in a string lower-case
17467f23 1335 */
f0fba2ad
LG
1336static void make_lowercase(char *s)
1337{
c6682fed
FE
1338 if (!s)
1339 return;
1340 for (; *s; s++)
1341 *s = tolower(*s);
f0fba2ad
LG
1342}
1343
49da09e2 1344static int fsl_ssi_imx_probe(struct platform_device *pdev,
4d9b7926 1345 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
49da09e2
MP
1346{
1347 struct device_node *np = pdev->dev.of_node;
ed0f1604 1348 u32 dmas[4];
49da09e2
MP
1349 int ret;
1350
f4a43cab
SW
1351 if (ssi_private->has_ipg_clk_name)
1352 ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
1353 else
1354 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
49da09e2
MP
1355 if (IS_ERR(ssi_private->clk)) {
1356 ret = PTR_ERR(ssi_private->clk);
1357 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1358 return ret;
1359 }
1360
f4a43cab
SW
1361 if (!ssi_private->has_ipg_clk_name) {
1362 ret = clk_prepare_enable(ssi_private->clk);
1363 if (ret) {
1364 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1365 return ret;
1366 }
49da09e2
MP
1367 }
1368
dcfcf2c2 1369 /* For those SLAVE implementations, we ignore non-baudclk cases
49da09e2
MP
1370 * and, instead, abandon MASTER mode that needs baud clock.
1371 */
1372 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1373 if (IS_ERR(ssi_private->baudclk))
1374 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1375 PTR_ERR(ssi_private->baudclk));
49da09e2 1376
4ee437fb
CC
1377 ssi_private->dma_params_tx.maxburst = ssi_private->dma_maxburst;
1378 ssi_private->dma_params_rx.maxburst = ssi_private->dma_maxburst;
43248122
MP
1379 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
1380 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
49da09e2 1381
90aff15b 1382 ret = of_property_read_u32_array(np, "dmas", dmas, 4);
ed0f1604 1383 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
49da09e2
MP
1384 ssi_private->use_dual_fifo = true;
1385 /* When using dual fifo mode, we need to keep watermark
1386 * as even numbers due to dma script limitation.
1387 */
1388 ssi_private->dma_params_tx.maxburst &= ~0x1;
1389 ssi_private->dma_params_rx.maxburst &= ~0x1;
1390 }
1391
4d9b7926
MP
1392 if (!ssi_private->use_dma) {
1393
1394 /*
1395 * Some boards use an incompatible codec. To get it
1396 * working, we are using imx-fiq-pcm-audio, that
1397 * can handle those codecs. DMA is not possible in this
1398 * situation.
1399 */
1400
1401 ssi_private->fiq_params.irq = ssi_private->irq;
1402 ssi_private->fiq_params.base = iomem;
1403 ssi_private->fiq_params.dma_params_rx =
1404 &ssi_private->dma_params_rx;
1405 ssi_private->fiq_params.dma_params_tx =
1406 &ssi_private->dma_params_tx;
1407
1408 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1409 if (ret)
1410 goto error_pcm;
1411 } else {
0d69e0dd 1412 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
4d9b7926
MP
1413 if (ret)
1414 goto error_pcm;
1415 }
1416
49da09e2 1417 return 0;
4d9b7926
MP
1418
1419error_pcm:
4d9b7926 1420
f4a43cab
SW
1421 if (!ssi_private->has_ipg_clk_name)
1422 clk_disable_unprepare(ssi_private->clk);
4d9b7926 1423 return ret;
49da09e2
MP
1424}
1425
1426static void fsl_ssi_imx_clean(struct platform_device *pdev,
1427 struct fsl_ssi_private *ssi_private)
1428{
4d9b7926
MP
1429 if (!ssi_private->use_dma)
1430 imx_pcm_fiq_exit(pdev);
f4a43cab
SW
1431 if (!ssi_private->has_ipg_clk_name)
1432 clk_disable_unprepare(ssi_private->clk);
49da09e2
MP
1433}
1434
a0a3d518 1435static int fsl_ssi_probe(struct platform_device *pdev)
17467f23 1436{
17467f23
TT
1437 struct fsl_ssi_private *ssi_private;
1438 int ret = 0;
38fec727 1439 struct device_node *np = pdev->dev.of_node;
c1953bfe 1440 const struct of_device_id *of_id;
f0fba2ad 1441 const char *p, *sprop;
8e9d8690 1442 const uint32_t *iprop;
ca264189 1443 struct resource *res;
43248122 1444 void __iomem *iomem;
f0fba2ad 1445 char name[64];
6139b1b1 1446 struct regmap_config regconfig = fsl_ssi_regconfig;
17467f23 1447
c1953bfe 1448 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
fcdbadef 1449 if (!of_id || !of_id->data)
c1953bfe 1450 return -EINVAL;
c1953bfe 1451
2a1d102d
MP
1452 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1453 GFP_KERNEL);
443be77e 1454 if (!ssi_private)
f0fba2ad 1455 return -ENOMEM;
17467f23 1456
fcdbadef 1457 ssi_private->soc = of_id->data;
0096b693 1458 ssi_private->dev = &pdev->dev;
fcdbadef 1459
85e59af2
MP
1460 sprop = of_get_property(np, "fsl,mode", NULL);
1461 if (sprop) {
1462 if (!strcmp(sprop, "ac97-slave"))
1463 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
85e59af2
MP
1464 }
1465
de623ece
MP
1466 ssi_private->use_dma = !of_property_read_bool(np,
1467 "fsl,fiq-stream-filter");
1468
85e59af2 1469 if (fsl_ssi_is_ac97(ssi_private)) {
cd7f0295
MP
1470 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1471 sizeof(fsl_ssi_ac97_dai));
1472
1473 fsl_ac97_data = ssi_private;
cd7f0295
MP
1474 } else {
1475 /* Initialize this copy of the CPU DAI driver structure */
1476 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1477 sizeof(fsl_ssi_dai_template));
1478 }
2a1d102d 1479 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
f0fba2ad 1480
ca264189
FE
1481 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1482 iomem = devm_ioremap_resource(&pdev->dev, res);
1483 if (IS_ERR(iomem))
1484 return PTR_ERR(iomem);
1485 ssi_private->ssi_phys = res->start;
43248122 1486
6139b1b1
MS
1487 if (ssi_private->soc->imx21regs) {
1488 /*
1489 * According to datasheet imx21-class SSI
1490 * don't have SACC{ST,EN,DIS} regs.
1491 */
1492 regconfig.max_register = CCSR_SSI_SRMSK;
f26b3b2a
MB
1493 regconfig.num_reg_defaults_raw =
1494 CCSR_SSI_SRMSK / sizeof(uint32_t) + 1;
6139b1b1
MS
1495 }
1496
f4a43cab
SW
1497 ret = of_property_match_string(np, "clock-names", "ipg");
1498 if (ret < 0) {
1499 ssi_private->has_ipg_clk_name = false;
1500 ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
6139b1b1 1501 &regconfig);
f4a43cab
SW
1502 } else {
1503 ssi_private->has_ipg_clk_name = true;
1504 ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
6139b1b1 1505 "ipg", iomem, &regconfig);
f4a43cab 1506 }
43248122
MP
1507 if (IS_ERR(ssi_private->regs)) {
1508 dev_err(&pdev->dev, "Failed to init register map\n");
1509 return PTR_ERR(ssi_private->regs);
1510 }
1fab6caf 1511
2ffa5310 1512 ssi_private->irq = platform_get_irq(pdev, 0);
28ecc0b6 1513 if (ssi_private->irq < 0) {
0c123250 1514 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
64aa5f58 1515 return ssi_private->irq;
1fab6caf
TT
1516 }
1517
f0fba2ad 1518 /* Are the RX and the TX clocks locked? */
07a9483a 1519 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
06cb3736
MS
1520 if (!fsl_ssi_is_ac97(ssi_private))
1521 ssi_private->cpu_dai_drv.symmetric_rates = 1;
1522
07a9483a
NC
1523 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1524 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1525 }
17467f23 1526
8e9d8690
TT
1527 /* Determine the FIFO depth. */
1528 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1529 if (iprop)
147dfe90 1530 ssi_private->fifo_depth = be32_to_cpup(iprop);
8e9d8690
TT
1531 else
1532 /* Older 8610 DTs didn't have the fifo-depth property */
1533 ssi_private->fifo_depth = 8;
1534
4ee437fb
CC
1535 /*
1536 * Set the watermark for transmit FIFO 0 and receive FIFO 0. We don't
1537 * use FIFO 1 but set the watermark appropriately nontheless.
1538 * We program the transmit water to signal a DMA transfer
1539 * if there are N elements left in the FIFO. For chips with 15-deep
1540 * FIFOs, set watermark to 8. This allows the SSI to operate at a
1541 * high data rate without channel slipping. Behavior is unchanged
1542 * for the older chips with a fifo depth of only 8. A value of 4
1543 * might be appropriate for the older chips, but is left at
1544 * fifo_depth-2 until sombody has a chance to test.
1545 *
1546 * We set the watermark on the same level as the DMA burstsize. For
1547 * fiq it is probably better to use the biggest possible watermark
1548 * size.
1549 */
1550 switch (ssi_private->fifo_depth) {
1551 case 15:
1552 /*
1553 * 2 samples is not enough when running at high data
1554 * rates (like 48kHz @ 16 bits/channel, 16 channels)
1555 * 8 seems to split things evenly and leave enough time
1556 * for the DMA to fill the FIFO before it's over/under
1557 * run.
1558 */
1559 ssi_private->fifo_watermark = 8;
1560 ssi_private->dma_maxburst = 8;
1561 break;
1562 case 8:
1563 default:
1564 /*
1565 * maintain old behavior for older chips.
1566 * Keeping it the same because I don't have an older
1567 * board to test with.
1568 * I suspect this could be changed to be something to
1569 * leave some more space in the fifo.
1570 */
1571 ssi_private->fifo_watermark = ssi_private->fifo_depth - 2;
1572 ssi_private->dma_maxburst = ssi_private->fifo_depth - 2;
1573 break;
1574 }
1575
4d9b7926
MP
1576 dev_set_drvdata(&pdev->dev, ssi_private);
1577
fcdbadef 1578 if (ssi_private->soc->imx) {
43248122 1579 ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
49da09e2 1580 if (ret)
2ffa5310 1581 return ret;
0888efd1
MP
1582 }
1583
695b78b5 1584 if (fsl_ssi_is_ac97(ssi_private)) {
b880b805 1585 mutex_init(&ssi_private->ac97_reg_lock);
695b78b5
MS
1586 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1587 if (ret) {
1588 dev_err(&pdev->dev, "could not set AC'97 ops\n");
1589 goto error_ac97_ops;
1590 }
1591 }
1592
299e7e97
FE
1593 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1594 &ssi_private->cpu_dai_drv, 1);
4d9b7926
MP
1595 if (ret) {
1596 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1597 goto error_asoc_register;
1598 }
1599
0888efd1 1600 if (ssi_private->use_dma) {
f0377086 1601 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
171d683d 1602 fsl_ssi_isr, 0, dev_name(&pdev->dev),
f0377086
MG
1603 ssi_private);
1604 if (ret < 0) {
1605 dev_err(&pdev->dev, "could not claim irq %u\n",
1606 ssi_private->irq);
299e7e97 1607 goto error_asoc_register;
f0377086 1608 }
09ce1111
SG
1609 }
1610
f138e621 1611 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
9368acc4 1612 if (ret)
299e7e97 1613 goto error_asoc_register;
09ce1111
SG
1614
1615 /*
1616 * If codec-handle property is missing from SSI node, we assume
1617 * that the machine driver uses new binding which does not require
1618 * SSI driver to trigger machine driver's probe.
1619 */
171d683d 1620 if (!of_get_property(np, "codec-handle", NULL))
09ce1111 1621 goto done;
09ce1111 1622
f0fba2ad 1623 /* Trigger the machine driver's probe function. The platform driver
2b81ec69 1624 * name of the machine driver is taken from /compatible property of the
f0fba2ad
LG
1625 * device tree. We also pass the address of the CPU DAI driver
1626 * structure.
1627 */
2b81ec69
SG
1628 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1629 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
f0fba2ad
LG
1630 p = strrchr(sprop, ',');
1631 if (p)
1632 sprop = p + 1;
1633 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1634 make_lowercase(name);
1635
1636 ssi_private->pdev =
38fec727 1637 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
f0fba2ad
LG
1638 if (IS_ERR(ssi_private->pdev)) {
1639 ret = PTR_ERR(ssi_private->pdev);
38fec727 1640 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
4d9b7926 1641 goto error_sound_card;
3f4b783c 1642 }
17467f23 1643
09ce1111 1644done:
85e59af2 1645 if (ssi_private->dai_fmt)
85151461
MT
1646 _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
1647 ssi_private->dai_fmt);
85e59af2 1648
8ed0c842
MS
1649 if (fsl_ssi_is_ac97(ssi_private)) {
1650 u32 ssi_idx;
1651
1652 ret = of_property_read_u32(np, "cell-index", &ssi_idx);
1653 if (ret) {
1654 dev_err(&pdev->dev, "cannot get SSI index property\n");
1655 goto error_sound_card;
1656 }
1657
1658 ssi_private->pdev =
1659 platform_device_register_data(NULL,
1660 "ac97-codec", ssi_idx, NULL, 0);
1661 if (IS_ERR(ssi_private->pdev)) {
1662 ret = PTR_ERR(ssi_private->pdev);
1663 dev_err(&pdev->dev,
1664 "failed to register AC97 codec platform: %d\n",
1665 ret);
1666 goto error_sound_card;
1667 }
1668 }
1669
f0fba2ad 1670 return 0;
87a0632b 1671
4d9b7926 1672error_sound_card:
f138e621 1673 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
9368acc4 1674
4d9b7926 1675error_asoc_register:
695b78b5
MS
1676 if (fsl_ssi_is_ac97(ssi_private))
1677 snd_soc_set_ac97_ops(NULL);
1678
1679error_ac97_ops:
b880b805
MS
1680 if (fsl_ssi_is_ac97(ssi_private))
1681 mutex_destroy(&ssi_private->ac97_reg_lock);
1682
fcdbadef 1683 if (ssi_private->soc->imx)
49da09e2 1684 fsl_ssi_imx_clean(pdev, ssi_private);
1fab6caf 1685
87a0632b 1686 return ret;
17467f23 1687}
17467f23 1688
38fec727 1689static int fsl_ssi_remove(struct platform_device *pdev)
17467f23 1690{
38fec727 1691 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
17467f23 1692
f138e621 1693 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
9368acc4 1694
171d683d 1695 if (ssi_private->pdev)
09ce1111 1696 platform_device_unregister(ssi_private->pdev);
49da09e2 1697
fcdbadef 1698 if (ssi_private->soc->imx)
49da09e2
MP
1699 fsl_ssi_imx_clean(pdev, ssi_private);
1700
b880b805 1701 if (fsl_ssi_is_ac97(ssi_private)) {
04143d61 1702 snd_soc_set_ac97_ops(NULL);
b880b805
MS
1703 mutex_destroy(&ssi_private->ac97_reg_lock);
1704 }
04143d61 1705
f0fba2ad 1706 return 0;
17467f23 1707}
f0fba2ad 1708
05cf2379
ZW
1709#ifdef CONFIG_PM_SLEEP
1710static int fsl_ssi_suspend(struct device *dev)
1711{
1712 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1713 struct regmap *regs = ssi_private->regs;
1714
1715 regmap_read(regs, CCSR_SSI_SFCSR,
1716 &ssi_private->regcache_sfcsr);
3f1c241f
MS
1717 regmap_read(regs, CCSR_SSI_SACNT,
1718 &ssi_private->regcache_sacnt);
05cf2379
ZW
1719
1720 regcache_cache_only(regs, true);
1721 regcache_mark_dirty(regs);
1722
1723 return 0;
1724}
1725
1726static int fsl_ssi_resume(struct device *dev)
1727{
1728 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1729 struct regmap *regs = ssi_private->regs;
1730
1731 regcache_cache_only(regs, false);
1732
1733 regmap_update_bits(regs, CCSR_SSI_SFCSR,
1734 CCSR_SSI_SFCSR_RFWM1_MASK | CCSR_SSI_SFCSR_TFWM1_MASK |
1735 CCSR_SSI_SFCSR_RFWM0_MASK | CCSR_SSI_SFCSR_TFWM0_MASK,
1736 ssi_private->regcache_sfcsr);
3f1c241f
MS
1737 regmap_write(regs, CCSR_SSI_SACNT,
1738 ssi_private->regcache_sacnt);
05cf2379
ZW
1739
1740 return regcache_sync(regs);
1741}
1742#endif /* CONFIG_PM_SLEEP */
1743
1744static const struct dev_pm_ops fsl_ssi_pm = {
1745 SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1746};
1747
f07eb223 1748static struct platform_driver fsl_ssi_driver = {
f0fba2ad
LG
1749 .driver = {
1750 .name = "fsl-ssi-dai",
f0fba2ad 1751 .of_match_table = fsl_ssi_ids,
05cf2379 1752 .pm = &fsl_ssi_pm,
f0fba2ad
LG
1753 },
1754 .probe = fsl_ssi_probe,
1755 .remove = fsl_ssi_remove,
1756};
17467f23 1757
ba0a7e02 1758module_platform_driver(fsl_ssi_driver);
a454dad1 1759
f3142807 1760MODULE_ALIAS("platform:fsl-ssi-dai");
17467f23
TT
1761MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1762MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
f0fba2ad 1763MODULE_LICENSE("GPL v2");