]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/soc/fsl/fsl_ssi.c
ASoC: fsl-ssi: make fsl,mode property optional
[mirror_ubuntu-bionic-kernel.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>
17467f23
TT
38#include <linux/device.h>
39#include <linux/delay.h>
5a0e3ad6 40#include <linux/slab.h>
aafa85e7 41#include <linux/spinlock.h>
dfa1a107
SG
42#include <linux/of_address.h>
43#include <linux/of_irq.h>
f0fba2ad 44#include <linux/of_platform.h>
17467f23 45
17467f23
TT
46#include <sound/core.h>
47#include <sound/pcm.h>
48#include <sound/pcm_params.h>
49#include <sound/initval.h>
50#include <sound/soc.h>
a8909c9b 51#include <sound/dmaengine_pcm.h>
17467f23 52
17467f23 53#include "fsl_ssi.h"
09ce1111 54#include "imx-pcm.h"
17467f23 55
dfa1a107
SG
56#ifdef PPC
57#define read_ssi(addr) in_be32(addr)
58#define write_ssi(val, addr) out_be32(addr, val)
59#define write_ssi_mask(addr, clear, set) clrsetbits_be32(addr, clear, set)
0a9eaa39 60#else
dfa1a107
SG
61#define read_ssi(addr) readl(addr)
62#define write_ssi(val, addr) writel(val, addr)
63/*
64 * FIXME: Proper locking should be added at write_ssi_mask caller level
65 * to ensure this register read/modify/write sequence is race free.
66 */
67static inline void write_ssi_mask(u32 __iomem *addr, u32 clear, u32 set)
68{
69 u32 val = readl(addr);
70 val = (val & ~clear) | set;
71 writel(val, addr);
72}
73#endif
74
17467f23
TT
75/**
76 * FSLSSI_I2S_RATES: sample rates supported by the I2S
77 *
78 * This driver currently only supports the SSI running in I2S slave mode,
79 * which means the codec determines the sample rate. Therefore, we tell
80 * ALSA that we support all rates and let the codec driver decide what rates
81 * are really supported.
82 */
24710c97 83#define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
17467f23
TT
84
85/**
86 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
87 *
88 * This driver currently only supports the SSI running in I2S slave mode.
89 *
90 * The SSI has a limitation in that the samples must be in the same byte
91 * order as the host CPU. This is because when multiple bytes are written
92 * to the STX register, the bytes and bits must be written in the same
93 * order. The STX is a shift register, so all the bits need to be aligned
94 * (bit-endianness must match byte-endianness). Processors typically write
95 * the bits within a byte in the same order that the bytes of a word are
96 * written in. So if the host CPU is big-endian, then only big-endian
97 * samples will be written to STX properly.
98 */
99#ifdef __BIG_ENDIAN
100#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
101 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
102 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
103#else
104#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
105 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
106 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
107#endif
108
9368acc4
MP
109#define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
110 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
111 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
112#define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
113 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
114 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
c1953bfe
MP
115
116enum fsl_ssi_type {
117 FSL_SSI_MCP8610,
118 FSL_SSI_MX21,
0888efd1 119 FSL_SSI_MX35,
c1953bfe
MP
120 FSL_SSI_MX51,
121};
122
4e6ec0d9
MP
123struct fsl_ssi_reg_val {
124 u32 sier;
125 u32 srcr;
126 u32 stcr;
127 u32 scr;
128};
129
130struct fsl_ssi_rxtx_reg_val {
131 struct fsl_ssi_reg_val rx;
132 struct fsl_ssi_reg_val tx;
133};
d5a908b2 134
fcdbadef
SH
135struct fsl_ssi_soc_data {
136 bool imx;
137 bool offline_config;
138 u32 sisr_write_mask;
139};
140
17467f23
TT
141/**
142 * fsl_ssi_private: per-SSI private data
143 *
17467f23
TT
144 * @ssi: pointer to the SSI's registers
145 * @ssi_phys: physical address of the SSI registers
146 * @irq: IRQ of this SSI
17467f23
TT
147 * @playback: the number of playback streams opened
148 * @capture: the number of capture streams opened
149 * @cpu_dai: the CPU DAI for this device
150 * @dev_attr: the sysfs device attribute structure
151 * @stats: SSI statistics
152 */
153struct fsl_ssi_private {
17467f23
TT
154 struct ccsr_ssi __iomem *ssi;
155 dma_addr_t ssi_phys;
156 unsigned int irq;
8e9d8690 157 unsigned int fifo_depth;
f0fba2ad 158 struct snd_soc_dai_driver cpu_dai_drv;
f0fba2ad 159 struct platform_device *pdev;
171d683d 160 unsigned int dai_fmt;
17467f23 161
de623ece 162 bool use_dma;
aafa85e7 163 bool baudclk_locked;
0da9e55e 164 bool use_dual_fifo;
2924a998 165 u8 i2s_mode;
aafa85e7
NC
166 spinlock_t baudclk_lock;
167 struct clk *baudclk;
95cd98f9 168 struct clk *clk;
a8909c9b
LPC
169 struct snd_dmaengine_dai_dma_data dma_params_tx;
170 struct snd_dmaengine_dai_dma_data dma_params_rx;
de623ece 171 struct imx_pcm_fiq_params fiq_params;
4e6ec0d9
MP
172 /* Register values for rx/tx configuration */
173 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
09ce1111 174
f138e621 175 struct fsl_ssi_dbg dbg_stats;
17467f23 176
fcdbadef 177 const struct fsl_ssi_soc_data *soc;
c1953bfe 178};
171d683d
MP
179
180/*
181 * imx51 and later SoCs have a slightly different IP that allows the
182 * SSI configuration while the SSI unit is running.
183 *
184 * More important, it is necessary on those SoCs to configure the
185 * sperate TX/RX DMA bits just before starting the stream
186 * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
187 * sends any DMA requests to the SDMA unit, otherwise it is not defined
188 * how the SDMA unit handles the DMA request.
189 *
190 * SDMA units are present on devices starting at imx35 but the imx35
191 * reference manual states that the DMA bits should not be changed
192 * while the SSI unit is running (SSIEN). So we support the necessary
193 * online configuration of fsl-ssi starting at imx51.
194 */
171d683d 195
fcdbadef
SH
196static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
197 .imx = false,
198 .offline_config = true,
199 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
200 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
201 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
202};
203
204static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
205 .imx = true,
206 .offline_config = true,
207 .sisr_write_mask = 0,
208};
209
210static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
211 .imx = true,
212 .offline_config = true,
213 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
214 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
215 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
216};
217
218static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
219 .imx = true,
220 .offline_config = false,
221 .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
222 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
223};
224
225static const struct of_device_id fsl_ssi_ids[] = {
226 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
227 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
228 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
229 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
230 {}
231};
232MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
233
234static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
235{
236 return !!(ssi_private->dai_fmt & SND_SOC_DAIFMT_AC97);
171d683d
MP
237}
238
17467f23
TT
239/**
240 * fsl_ssi_isr: SSI interrupt handler
241 *
242 * Although it's possible to use the interrupt handler to send and receive
243 * data to/from the SSI, we use the DMA instead. Programming is more
244 * complicated, but the performance is much better.
245 *
246 * This interrupt handler is used only to gather statistics.
247 *
248 * @irq: IRQ of the SSI device
249 * @dev_id: pointer to the ssi_private structure for this SSI device
250 */
251static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
252{
253 struct fsl_ssi_private *ssi_private = dev_id;
254 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
17467f23 255 __be32 sisr;
0888efd1 256 __be32 sisr2;
17467f23
TT
257
258 /* We got an interrupt, so read the status register to see what we
259 were interrupted for. We mask it with the Interrupt Enable register
260 so that we only check for events that we're interested in.
261 */
f138e621 262 sisr = read_ssi(&ssi->sisr);
17467f23 263
fcdbadef 264 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
17467f23
TT
265 /* Clear the bits that we set */
266 if (sisr2)
dfa1a107 267 write_ssi(sisr2, &ssi->sisr);
17467f23 268
f138e621 269 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
9368acc4 270
f138e621 271 return IRQ_HANDLED;
9368acc4
MP
272}
273
4e6ec0d9
MP
274/*
275 * Enable/Disable all rx/tx config flags at once.
276 */
277static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
278 bool enable)
279{
280 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
281 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
282
283 if (enable) {
284 write_ssi_mask(&ssi->sier, 0, vals->rx.sier | vals->tx.sier);
285 write_ssi_mask(&ssi->srcr, 0, vals->rx.srcr | vals->tx.srcr);
286 write_ssi_mask(&ssi->stcr, 0, vals->rx.stcr | vals->tx.stcr);
287 } else {
288 write_ssi_mask(&ssi->srcr, vals->rx.srcr | vals->tx.srcr, 0);
289 write_ssi_mask(&ssi->stcr, vals->rx.stcr | vals->tx.stcr, 0);
290 write_ssi_mask(&ssi->sier, vals->rx.sier | vals->tx.sier, 0);
291 }
292}
293
65c961cc
MP
294/*
295 * Calculate the bits that have to be disabled for the current stream that is
296 * getting disabled. This keeps the bits enabled that are necessary for the
297 * second stream to work if 'stream_active' is true.
298 *
299 * Detailed calculation:
300 * These are the values that need to be active after disabling. For non-active
301 * second stream, this is 0:
302 * vals_stream * !!stream_active
303 *
304 * The following computes the overall differences between the setup for the
305 * to-disable stream and the active stream, a simple XOR:
306 * vals_disable ^ (vals_stream * !!(stream_active))
307 *
308 * The full expression adds a mask on all values we care about
309 */
310#define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
311 ((vals_disable) & \
312 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
313
4e6ec0d9
MP
314/*
315 * Enable/Disable a ssi configuration. You have to pass either
316 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
317 */
318static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
319 struct fsl_ssi_reg_val *vals)
320{
321 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
322 struct fsl_ssi_reg_val *avals;
323 u32 scr_val = read_ssi(&ssi->scr);
324 int nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
325 !!(scr_val & CCSR_SSI_SCR_RE);
65c961cc
MP
326 int keep_active;
327
328 if (nr_active_streams - 1 > 0)
329 keep_active = 1;
330 else
331 keep_active = 0;
4e6ec0d9
MP
332
333 /* Find the other direction values rx or tx which we do not want to
334 * modify */
335 if (&ssi_private->rxtx_reg_val.rx == vals)
336 avals = &ssi_private->rxtx_reg_val.tx;
337 else
338 avals = &ssi_private->rxtx_reg_val.rx;
339
340 /* If vals should be disabled, start with disabling the unit */
341 if (!enable) {
65c961cc
MP
342 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
343 keep_active);
4e6ec0d9
MP
344 write_ssi_mask(&ssi->scr, scr, 0);
345 }
346
347 /*
348 * We are running on a SoC which does not support online SSI
349 * reconfiguration, so we have to enable all necessary flags at once
350 * even if we do not use them later (capture and playback configuration)
351 */
fcdbadef 352 if (ssi_private->soc->offline_config) {
4e6ec0d9 353 if ((enable && !nr_active_streams) ||
65c961cc 354 (!enable && !keep_active))
4e6ec0d9
MP
355 fsl_ssi_rxtx_config(ssi_private, enable);
356
357 goto config_done;
358 }
359
360 /*
361 * Configure single direction units while the SSI unit is running
362 * (online configuration)
363 */
364 if (enable) {
365 write_ssi_mask(&ssi->sier, 0, vals->sier);
366 write_ssi_mask(&ssi->srcr, 0, vals->srcr);
367 write_ssi_mask(&ssi->stcr, 0, vals->stcr);
368 } else {
369 u32 sier;
370 u32 srcr;
371 u32 stcr;
372
373 /*
374 * Disabling the necessary flags for one of rx/tx while the
375 * other stream is active is a little bit more difficult. We
376 * have to disable only those flags that differ between both
377 * streams (rx XOR tx) and that are set in the stream that is
378 * disabled now. Otherwise we could alter flags of the other
379 * stream
380 */
381
382 /* These assignments are simply vals without bits set in avals*/
65c961cc
MP
383 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
384 keep_active);
385 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
386 keep_active);
387 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
388 keep_active);
4e6ec0d9
MP
389
390 write_ssi_mask(&ssi->srcr, srcr, 0);
391 write_ssi_mask(&ssi->stcr, stcr, 0);
392 write_ssi_mask(&ssi->sier, sier, 0);
393 }
394
395config_done:
396 /* Enabling of subunits is done after configuration */
397 if (enable)
398 write_ssi_mask(&ssi->scr, 0, vals->scr);
399}
400
401
402static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
403{
404 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
405}
406
407static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
408{
409 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
410}
411
6de83879
MP
412/*
413 * Setup rx/tx register values used to enable/disable the streams. These will
414 * be used later in fsl_ssi_config to setup the streams without the need to
415 * check for all different SSI modes.
416 */
417static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
418{
419 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
420
421 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
422 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
423 reg->rx.scr = 0;
424 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
425 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
426 reg->tx.scr = 0;
427
171d683d 428 if (!fsl_ssi_is_ac97(ssi_private)) {
6de83879
MP
429 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
430 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
431 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
432 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
433 }
434
435 if (ssi_private->use_dma) {
436 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
437 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
438 } else {
439 reg->rx.sier |= CCSR_SSI_SIER_RIE;
440 reg->tx.sier |= CCSR_SSI_SIER_TIE;
441 }
442
443 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
444 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
445}
446
d8764646
MP
447static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
448{
449 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
450
451 /*
452 * Setup the clock control register
453 */
454 write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
455 &ssi->stccr);
456 write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
457 &ssi->srccr);
458
459 /*
460 * Enable AC97 mode and startup the SSI
461 */
462 write_ssi(CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV,
463 &ssi->sacnt);
464 write_ssi(0xff, &ssi->saccdis);
465 write_ssi(0x300, &ssi->saccen);
466
467 /*
468 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
469 * codec before a stream is started.
470 */
471 write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN |
472 CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
473
474 write_ssi(CCSR_SSI_SOR_WAIT(3), &ssi->sor);
475}
476
17467f23
TT
477/**
478 * fsl_ssi_startup: create a new substream
479 *
480 * This is the first function called when a stream is opened.
481 *
482 * If this is the first stream open, then grab the IRQ and program most of
483 * the SSI registers.
484 */
dee89c4d
MB
485static int fsl_ssi_startup(struct snd_pcm_substream *substream,
486 struct snd_soc_dai *dai)
17467f23
TT
487{
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
5e538eca
TT
489 struct fsl_ssi_private *ssi_private =
490 snd_soc_dai_get_drvdata(rtd->cpu_dai);
aafa85e7 491 unsigned long flags;
17467f23 492
171d683d 493 if (!dai->active && !fsl_ssi_is_ac97(ssi_private)) {
aafa85e7
NC
494 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
495 ssi_private->baudclk_locked = false;
496 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
497 }
be41e941 498
0da9e55e
NC
499 /* When using dual fifo mode, it is safer to ensure an even period
500 * size. If appearing to an odd number while DMA always starts its
501 * task from fifo0, fifo1 would be neglected at the end of each
502 * period. But SSI would still access fifo1 with an invalid data.
503 */
504 if (ssi_private->use_dual_fifo)
505 snd_pcm_hw_constraint_step(substream->runtime, 0,
506 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
507
17467f23
TT
508 return 0;
509}
510
ee9daad4
SH
511/**
512 * fsl_ssi_set_dai_sysclk - configure Digital Audio Interface bit clock
513 *
514 * Note: This function can be only called when using SSI as DAI master
515 *
516 * Quick instruction for parameters:
517 * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
518 * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
519 */
520static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
521 int clk_id, unsigned int freq, int dir)
522{
523 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
524 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
525 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
526 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
527 unsigned long flags, clkrate, baudrate, tmprate;
528 u64 sub, savesub = 100000;
529
530 /* Don't apply it to any non-baudclk circumstance */
531 if (IS_ERR(ssi_private->baudclk))
532 return -EINVAL;
533
534 /* It should be already enough to divide clock by setting pm alone */
535 psr = 0;
536 div2 = 0;
537
538 factor = (div2 + 1) * (7 * psr + 1) * 2;
539
540 for (i = 0; i < 255; i++) {
541 /* The bclk rate must be smaller than 1/5 sysclk rate */
542 if (factor * (i + 1) < 5)
543 continue;
544
545 tmprate = freq * factor * (i + 2);
546 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
547
548 do_div(clkrate, factor);
549 afreq = (u32)clkrate / (i + 1);
550
551 if (freq == afreq)
552 sub = 0;
553 else if (freq / afreq == 1)
554 sub = freq - afreq;
555 else if (afreq / freq == 1)
556 sub = afreq - freq;
557 else
558 continue;
559
560 /* Calculate the fraction */
561 sub *= 100000;
562 do_div(sub, freq);
563
564 if (sub < savesub) {
565 baudrate = tmprate;
566 savesub = sub;
567 pm = i;
568 }
569
570 /* We are lucky */
571 if (savesub == 0)
572 break;
573 }
574
575 /* No proper pm found if it is still remaining the initial value */
576 if (pm == 999) {
577 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
578 return -EINVAL;
579 }
580
581 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
582 (psr ? CCSR_SSI_SxCCR_PSR : 0);
583 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
584 CCSR_SSI_SxCCR_PSR;
585
586 if (dir == SND_SOC_CLOCK_OUT || synchronous)
587 write_ssi_mask(&ssi->stccr, mask, stccr);
588 else
589 write_ssi_mask(&ssi->srccr, mask, stccr);
590
591 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
592 if (!ssi_private->baudclk_locked) {
593 ret = clk_set_rate(ssi_private->baudclk, baudrate);
594 if (ret) {
595 spin_unlock_irqrestore(&ssi_private->baudclk_lock,
596 flags);
597 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
598 return -EINVAL;
599 }
600 ssi_private->baudclk_locked = true;
601 }
602 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
603
604 return 0;
605}
606
17467f23 607/**
85ef2375 608 * fsl_ssi_hw_params - program the sample size
17467f23
TT
609 *
610 * Most of the SSI registers have been programmed in the startup function,
611 * but the word length must be programmed here. Unfortunately, programming
612 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
613 * cause a problem with supporting simultaneous playback and capture. If
614 * the SSI is already playing a stream, then that stream may be temporarily
615 * stopped when you start capture.
616 *
617 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
618 * clock master.
619 */
85ef2375
TT
620static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
621 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
17467f23 622{
f0fba2ad 623 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
5e538eca 624 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
2924a998 625 unsigned int channels = params_channels(hw_params);
5e538eca
TT
626 unsigned int sample_size =
627 snd_pcm_format_width(params_format(hw_params));
628 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
dfa1a107 629 int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
17467f23 630
5e538eca
TT
631 /*
632 * If we're in synchronous mode, and the SSI is already enabled,
633 * then STCCR is already set properly.
634 */
635 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
636 return 0;
17467f23 637
5e538eca
TT
638 /*
639 * FIXME: The documentation says that SxCCR[WL] should not be
640 * modified while the SSI is enabled. The only time this can
641 * happen is if we're trying to do simultaneous playback and
642 * capture in asynchronous mode. Unfortunately, I have been enable
643 * to get that to work at all on the P1022DS. Therefore, we don't
644 * bother to disable/enable the SSI when setting SxCCR[WL], because
645 * the SSI will stop anyway. Maybe one day, this will get fixed.
646 */
17467f23 647
5e538eca
TT
648 /* In synchronous mode, the SSI uses STCCR for capture */
649 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
650 ssi_private->cpu_dai_drv.symmetric_rates)
dfa1a107 651 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
5e538eca 652 else
dfa1a107 653 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
17467f23 654
171d683d 655 if (!fsl_ssi_is_ac97(ssi_private))
2924a998
NC
656 write_ssi_mask(&ssi->scr,
657 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
658 channels == 1 ? 0 : ssi_private->i2s_mode);
659
17467f23
TT
660 return 0;
661}
662
85e59af2
MP
663static int _fsl_ssi_set_dai_fmt(struct fsl_ssi_private *ssi_private,
664 unsigned int fmt)
aafa85e7 665{
aafa85e7
NC
666 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
667 u32 strcr = 0, stcr, srcr, scr, mask;
2b0db996
MP
668 u8 wm;
669
171d683d
MP
670 ssi_private->dai_fmt = fmt;
671
2b0db996 672 fsl_ssi_setup_reg_vals(ssi_private);
aafa85e7
NC
673
674 scr = read_ssi(&ssi->scr) & ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
50489479 675 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
aafa85e7
NC
676
677 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
678 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
679 CCSR_SSI_STCR_TEFS;
680 stcr = read_ssi(&ssi->stcr) & ~mask;
681 srcr = read_ssi(&ssi->srcr) & ~mask;
682
07a28dbe 683 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
aafa85e7
NC
684 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
685 case SND_SOC_DAIFMT_I2S:
686 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
687 case SND_SOC_DAIFMT_CBS_CFS:
07a28dbe 688 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
aafa85e7
NC
689 break;
690 case SND_SOC_DAIFMT_CBM_CFM:
07a28dbe 691 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
aafa85e7
NC
692 break;
693 default:
694 return -EINVAL;
695 }
aafa85e7
NC
696
697 /* Data on rising edge of bclk, frame low, 1clk before data */
698 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
699 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
700 break;
701 case SND_SOC_DAIFMT_LEFT_J:
702 /* Data on rising edge of bclk, frame high */
703 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
704 break;
705 case SND_SOC_DAIFMT_DSP_A:
706 /* Data on rising edge of bclk, frame high, 1clk before data */
707 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
708 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
709 break;
710 case SND_SOC_DAIFMT_DSP_B:
711 /* Data on rising edge of bclk, frame high */
712 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
713 CCSR_SSI_STCR_TXBIT0;
714 break;
2b0db996 715 case SND_SOC_DAIFMT_AC97:
07a28dbe 716 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
2b0db996 717 break;
aafa85e7
NC
718 default:
719 return -EINVAL;
720 }
2b0db996 721 scr |= ssi_private->i2s_mode;
aafa85e7
NC
722
723 /* DAI clock inversion */
724 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
725 case SND_SOC_DAIFMT_NB_NF:
726 /* Nothing to do for both normal cases */
727 break;
728 case SND_SOC_DAIFMT_IB_NF:
729 /* Invert bit clock */
730 strcr ^= CCSR_SSI_STCR_TSCKP;
731 break;
732 case SND_SOC_DAIFMT_NB_IF:
733 /* Invert frame clock */
734 strcr ^= CCSR_SSI_STCR_TFSI;
735 break;
736 case SND_SOC_DAIFMT_IB_IF:
737 /* Invert both clocks */
738 strcr ^= CCSR_SSI_STCR_TSCKP;
739 strcr ^= CCSR_SSI_STCR_TFSI;
740 break;
741 default:
742 return -EINVAL;
743 }
744
745 /* DAI clock master masks */
746 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
747 case SND_SOC_DAIFMT_CBS_CFS:
748 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
749 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
750 break;
751 case SND_SOC_DAIFMT_CBM_CFM:
752 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
753 break;
754 default:
755 return -EINVAL;
756 }
757
758 stcr |= strcr;
759 srcr |= strcr;
760
761 if (ssi_private->cpu_dai_drv.symmetric_rates) {
762 /* Need to clear RXDIR when using SYNC mode */
763 srcr &= ~CCSR_SSI_SRCR_RXDIR;
764 scr |= CCSR_SSI_SCR_SYN;
765 }
766
767 write_ssi(stcr, &ssi->stcr);
768 write_ssi(srcr, &ssi->srcr);
769 write_ssi(scr, &ssi->scr);
770
2b0db996
MP
771 /*
772 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
773 * use FIFO 1. We program the transmit water to signal a DMA transfer
774 * if there are only two (or fewer) elements left in the FIFO. Two
775 * elements equals one frame (left channel, right channel). This value,
776 * however, depends on the depth of the transmit buffer.
777 *
778 * We set the watermark on the same level as the DMA burstsize. For
779 * fiq it is probably better to use the biggest possible watermark
780 * size.
781 */
782 if (ssi_private->use_dma)
783 wm = ssi_private->fifo_depth - 2;
784 else
785 wm = ssi_private->fifo_depth;
786
787 write_ssi(CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
788 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm),
789 &ssi->sfcsr);
790
791 if (ssi_private->use_dual_fifo) {
792 write_ssi_mask(&ssi->srcr, CCSR_SSI_SRCR_RFEN1,
793 CCSR_SSI_SRCR_RFEN1);
794 write_ssi_mask(&ssi->stcr, CCSR_SSI_STCR_TFEN1,
795 CCSR_SSI_STCR_TFEN1);
796 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TCH_EN,
797 CCSR_SSI_SCR_TCH_EN);
798 }
799
800 if (fmt & SND_SOC_DAIFMT_AC97)
801 fsl_ssi_setup_ac97(ssi_private);
802
aafa85e7 803 return 0;
85e59af2
MP
804
805}
806
807/**
808 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
809 */
810static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
811{
812 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
813
814 return _fsl_ssi_set_dai_fmt(ssi_private, fmt);
aafa85e7
NC
815}
816
aafa85e7
NC
817/**
818 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
819 *
820 * Note: This function can be only called when using SSI as DAI master
821 */
822static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
823 u32 rx_mask, int slots, int slot_width)
824{
825 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
826 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
827 u32 val;
828
829 /* The slot number should be >= 2 if using Network mode or I2S mode */
830 val = read_ssi(&ssi->scr) & (CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET);
831 if (val && slots < 2) {
832 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
833 return -EINVAL;
834 }
835
836 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_DC_MASK,
837 CCSR_SSI_SxCCR_DC(slots));
838 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_DC_MASK,
839 CCSR_SSI_SxCCR_DC(slots));
840
841 /* The register SxMSKs needs SSI to provide essential clock due to
842 * hardware design. So we here temporarily enable SSI to set them.
843 */
844 val = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
845 write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN);
846
847 write_ssi(tx_mask, &ssi->stmsk);
848 write_ssi(rx_mask, &ssi->srmsk);
849
850 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, val);
851
852 return 0;
853}
854
17467f23
TT
855/**
856 * fsl_ssi_trigger: start and stop the DMA transfer.
857 *
858 * This function is called by ALSA to start, stop, pause, and resume the DMA
859 * transfer of data.
860 *
861 * The DMA channel is in external master start and pause mode, which
862 * means the SSI completely controls the flow of data.
863 */
dee89c4d
MB
864static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
865 struct snd_soc_dai *dai)
17467f23
TT
866{
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 868 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
17467f23 869 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
aafa85e7 870 unsigned long flags;
9b443e3d 871
17467f23
TT
872 switch (cmd) {
873 case SNDRV_PCM_TRIGGER_START:
b20e53a8 874 case SNDRV_PCM_TRIGGER_RESUME:
17467f23 875 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
a4d11fe5 876 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6de83879 877 fsl_ssi_tx_config(ssi_private, true);
a4d11fe5 878 else
6de83879 879 fsl_ssi_rx_config(ssi_private, true);
17467f23
TT
880 break;
881
882 case SNDRV_PCM_TRIGGER_STOP:
b20e53a8 883 case SNDRV_PCM_TRIGGER_SUSPEND:
17467f23
TT
884 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
885 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
6de83879 886 fsl_ssi_tx_config(ssi_private, false);
17467f23 887 else
6de83879 888 fsl_ssi_rx_config(ssi_private, false);
b2c119b0 889
171d683d 890 if (!fsl_ssi_is_ac97(ssi_private) && (read_ssi(&ssi->scr) &
aafa85e7 891 (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0) {
aafa85e7
NC
892 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
893 ssi_private->baudclk_locked = false;
894 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
895 }
17467f23
TT
896 break;
897
898 default:
899 return -EINVAL;
900 }
901
171d683d 902 if (fsl_ssi_is_ac97(ssi_private)) {
a5a7ee7c
MP
903 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
904 write_ssi(CCSR_SSI_SOR_TX_CLR, &ssi->sor);
905 else
906 write_ssi(CCSR_SSI_SOR_RX_CLR, &ssi->sor);
907 }
9b443e3d 908
17467f23
TT
909 return 0;
910}
911
fc8ba7f9
LPC
912static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
913{
914 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
915
fcdbadef 916 if (ssi_private->soc->imx && ssi_private->use_dma) {
fc8ba7f9
LPC
917 dai->playback_dma_data = &ssi_private->dma_params_tx;
918 dai->capture_dma_data = &ssi_private->dma_params_rx;
919 }
920
921 return 0;
922}
923
85e7652d 924static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
6335d055
EM
925 .startup = fsl_ssi_startup,
926 .hw_params = fsl_ssi_hw_params,
aafa85e7
NC
927 .set_fmt = fsl_ssi_set_dai_fmt,
928 .set_sysclk = fsl_ssi_set_dai_sysclk,
929 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
6335d055 930 .trigger = fsl_ssi_trigger,
6335d055
EM
931};
932
f0fba2ad
LG
933/* Template for the CPU dai driver structure */
934static struct snd_soc_dai_driver fsl_ssi_dai_template = {
fc8ba7f9 935 .probe = fsl_ssi_dai_probe,
17467f23 936 .playback = {
2924a998 937 .channels_min = 1,
17467f23
TT
938 .channels_max = 2,
939 .rates = FSLSSI_I2S_RATES,
940 .formats = FSLSSI_I2S_FORMATS,
941 },
942 .capture = {
2924a998 943 .channels_min = 1,
17467f23
TT
944 .channels_max = 2,
945 .rates = FSLSSI_I2S_RATES,
946 .formats = FSLSSI_I2S_FORMATS,
947 },
6335d055 948 .ops = &fsl_ssi_dai_ops,
17467f23
TT
949};
950
3580aa10
KM
951static const struct snd_soc_component_driver fsl_ssi_component = {
952 .name = "fsl-ssi",
953};
954
cd7f0295
MP
955static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
956 .ac97_control = 1,
957 .playback = {
958 .stream_name = "AC97 Playback",
959 .channels_min = 2,
960 .channels_max = 2,
961 .rates = SNDRV_PCM_RATE_8000_48000,
962 .formats = SNDRV_PCM_FMTBIT_S16_LE,
963 },
964 .capture = {
965 .stream_name = "AC97 Capture",
966 .channels_min = 2,
967 .channels_max = 2,
968 .rates = SNDRV_PCM_RATE_48000,
969 .formats = SNDRV_PCM_FMTBIT_S16_LE,
970 },
a5a7ee7c 971 .ops = &fsl_ssi_dai_ops,
cd7f0295
MP
972};
973
974
975static struct fsl_ssi_private *fsl_ac97_data;
976
a851a2bb 977static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
cd7f0295
MP
978 unsigned short val)
979{
980 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
981 unsigned int lreg;
982 unsigned int lval;
983
984 if (reg > 0x7f)
985 return;
986
987
988 lreg = reg << 12;
989 write_ssi(lreg, &ssi->sacadd);
990
991 lval = val << 4;
992 write_ssi(lval , &ssi->sacdat);
993
994 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
995 CCSR_SSI_SACNT_WR);
996 udelay(100);
997}
998
a851a2bb 999static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
cd7f0295
MP
1000 unsigned short reg)
1001{
1002 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
1003
1004 unsigned short val = -1;
1005 unsigned int lreg;
1006
1007 lreg = (reg & 0x7f) << 12;
1008 write_ssi(lreg, &ssi->sacadd);
1009 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1010 CCSR_SSI_SACNT_RD);
1011
1012 udelay(100);
1013
1014 val = (read_ssi(&ssi->sacdat) >> 4) & 0xffff;
1015
1016 return val;
1017}
1018
1019static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1020 .read = fsl_ssi_ac97_read,
1021 .write = fsl_ssi_ac97_write,
1022};
1023
17467f23 1024/**
f0fba2ad 1025 * Make every character in a string lower-case
17467f23 1026 */
f0fba2ad
LG
1027static void make_lowercase(char *s)
1028{
1029 char *p = s;
1030 char c;
1031
1032 while ((c = *p)) {
1033 if ((c >= 'A') && (c <= 'Z'))
1034 *p = c + ('a' - 'A');
1035 p++;
1036 }
1037}
1038
49da09e2 1039static int fsl_ssi_imx_probe(struct platform_device *pdev,
4d9b7926 1040 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
49da09e2
MP
1041{
1042 struct device_node *np = pdev->dev.of_node;
ed0f1604 1043 u32 dmas[4];
49da09e2
MP
1044 int ret;
1045
1046 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1047 if (IS_ERR(ssi_private->clk)) {
1048 ret = PTR_ERR(ssi_private->clk);
1049 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1050 return ret;
1051 }
1052
1053 ret = clk_prepare_enable(ssi_private->clk);
1054 if (ret) {
1055 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1056 return ret;
1057 }
1058
1059 /* For those SLAVE implementations, we ingore non-baudclk cases
1060 * and, instead, abandon MASTER mode that needs baud clock.
1061 */
1062 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1063 if (IS_ERR(ssi_private->baudclk))
1064 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1065 PTR_ERR(ssi_private->baudclk));
1066 else
1067 clk_prepare_enable(ssi_private->baudclk);
1068
1069 /*
1070 * We have burstsize be "fifo_depth - 2" to match the SSI
1071 * watermark setting in fsl_ssi_startup().
1072 */
1073 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1074 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1075 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys +
1076 offsetof(struct ccsr_ssi, stx0);
1077 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys +
1078 offsetof(struct ccsr_ssi, srx0);
49da09e2 1079
ed0f1604
MP
1080 ret = !of_property_read_u32_array(np, "dmas", dmas, 4);
1081 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
49da09e2
MP
1082 ssi_private->use_dual_fifo = true;
1083 /* When using dual fifo mode, we need to keep watermark
1084 * as even numbers due to dma script limitation.
1085 */
1086 ssi_private->dma_params_tx.maxburst &= ~0x1;
1087 ssi_private->dma_params_rx.maxburst &= ~0x1;
1088 }
1089
4d9b7926
MP
1090 if (!ssi_private->use_dma) {
1091
1092 /*
1093 * Some boards use an incompatible codec. To get it
1094 * working, we are using imx-fiq-pcm-audio, that
1095 * can handle those codecs. DMA is not possible in this
1096 * situation.
1097 */
1098
1099 ssi_private->fiq_params.irq = ssi_private->irq;
1100 ssi_private->fiq_params.base = iomem;
1101 ssi_private->fiq_params.dma_params_rx =
1102 &ssi_private->dma_params_rx;
1103 ssi_private->fiq_params.dma_params_tx =
1104 &ssi_private->dma_params_tx;
1105
1106 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1107 if (ret)
1108 goto error_pcm;
1109 } else {
1110 ret = imx_pcm_dma_init(pdev);
1111 if (ret)
1112 goto error_pcm;
1113 }
1114
49da09e2 1115 return 0;
4d9b7926
MP
1116
1117error_pcm:
1118 if (!IS_ERR(ssi_private->baudclk))
1119 clk_disable_unprepare(ssi_private->baudclk);
1120
1121 clk_disable_unprepare(ssi_private->clk);
1122
1123 return ret;
49da09e2
MP
1124}
1125
1126static void fsl_ssi_imx_clean(struct platform_device *pdev,
1127 struct fsl_ssi_private *ssi_private)
1128{
4d9b7926
MP
1129 if (!ssi_private->use_dma)
1130 imx_pcm_fiq_exit(pdev);
49da09e2
MP
1131 if (!IS_ERR(ssi_private->baudclk))
1132 clk_disable_unprepare(ssi_private->baudclk);
1133 clk_disable_unprepare(ssi_private->clk);
1134}
1135
a0a3d518 1136static int fsl_ssi_probe(struct platform_device *pdev)
17467f23 1137{
17467f23
TT
1138 struct fsl_ssi_private *ssi_private;
1139 int ret = 0;
38fec727 1140 struct device_node *np = pdev->dev.of_node;
c1953bfe 1141 const struct of_device_id *of_id;
f0fba2ad 1142 const char *p, *sprop;
8e9d8690 1143 const uint32_t *iprop;
f0fba2ad
LG
1144 struct resource res;
1145 char name[64];
17467f23 1146
ff71334a
TT
1147 /* SSIs that are not connected on the board should have a
1148 * status = "disabled"
1149 * property in their device tree nodes.
f0fba2ad 1150 */
ff71334a 1151 if (!of_device_is_available(np))
f0fba2ad
LG
1152 return -ENODEV;
1153
c1953bfe 1154 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
fcdbadef 1155 if (!of_id || !of_id->data)
c1953bfe 1156 return -EINVAL;
c1953bfe 1157
2a1d102d
MP
1158 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1159 GFP_KERNEL);
17467f23 1160 if (!ssi_private) {
38fec727 1161 dev_err(&pdev->dev, "could not allocate DAI object\n");
f0fba2ad 1162 return -ENOMEM;
17467f23 1163 }
17467f23 1164
fcdbadef
SH
1165 ssi_private->soc = of_id->data;
1166
85e59af2
MP
1167 sprop = of_get_property(np, "fsl,mode", NULL);
1168 if (sprop) {
1169 if (!strcmp(sprop, "ac97-slave"))
1170 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
1171 else if (!strcmp(sprop, "i2s-slave"))
1172 ssi_private->dai_fmt = SND_SOC_DAIFMT_I2S |
1173 SND_SOC_DAIFMT_CBM_CFM;
1174 }
1175
de623ece
MP
1176 ssi_private->use_dma = !of_property_read_bool(np,
1177 "fsl,fiq-stream-filter");
1178
85e59af2 1179 if (fsl_ssi_is_ac97(ssi_private)) {
cd7f0295
MP
1180 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1181 sizeof(fsl_ssi_ac97_dai));
1182
1183 fsl_ac97_data = ssi_private;
cd7f0295
MP
1184
1185 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1186 } else {
1187 /* Initialize this copy of the CPU DAI driver structure */
1188 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1189 sizeof(fsl_ssi_dai_template));
1190 }
2a1d102d 1191 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
f0fba2ad
LG
1192
1193 /* Get the addresses and IRQ */
1194 ret = of_address_to_resource(np, 0, &res);
1195 if (ret) {
38fec727 1196 dev_err(&pdev->dev, "could not determine device resources\n");
b0a4747a 1197 return ret;
f0fba2ad 1198 }
147dfe90
TT
1199 ssi_private->ssi = of_iomap(np, 0);
1200 if (!ssi_private->ssi) {
1201 dev_err(&pdev->dev, "could not map device resources\n");
b0a4747a 1202 return -ENOMEM;
147dfe90 1203 }
f0fba2ad 1204 ssi_private->ssi_phys = res.start;
1fab6caf 1205
f0fba2ad 1206 ssi_private->irq = irq_of_parse_and_map(np, 0);
d60336e2 1207 if (!ssi_private->irq) {
1fab6caf 1208 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
b0a4747a 1209 return -ENXIO;
1fab6caf
TT
1210 }
1211
f0fba2ad 1212 /* Are the RX and the TX clocks locked? */
07a9483a 1213 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
f0fba2ad 1214 ssi_private->cpu_dai_drv.symmetric_rates = 1;
07a9483a
NC
1215 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1216 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1217 }
17467f23 1218
8e9d8690
TT
1219 /* Determine the FIFO depth. */
1220 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1221 if (iprop)
147dfe90 1222 ssi_private->fifo_depth = be32_to_cpup(iprop);
8e9d8690
TT
1223 else
1224 /* Older 8610 DTs didn't have the fifo-depth property */
1225 ssi_private->fifo_depth = 8;
1226
aafa85e7
NC
1227 ssi_private->baudclk_locked = false;
1228 spin_lock_init(&ssi_private->baudclk_lock);
1229
4d9b7926
MP
1230 dev_set_drvdata(&pdev->dev, ssi_private);
1231
fcdbadef 1232 if (ssi_private->soc->imx) {
4d9b7926 1233 ret = fsl_ssi_imx_probe(pdev, ssi_private, ssi_private->ssi);
49da09e2 1234 if (ret)
b0a4747a 1235 goto error_irqmap;
0888efd1
MP
1236 }
1237
4d9b7926
MP
1238 ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1239 &ssi_private->cpu_dai_drv, 1);
1240 if (ret) {
1241 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1242 goto error_asoc_register;
1243 }
1244
0888efd1 1245 if (ssi_private->use_dma) {
f0377086 1246 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
171d683d 1247 fsl_ssi_isr, 0, dev_name(&pdev->dev),
f0377086
MG
1248 ssi_private);
1249 if (ret < 0) {
1250 dev_err(&pdev->dev, "could not claim irq %u\n",
1251 ssi_private->irq);
49da09e2 1252 goto error_irq;
f0377086 1253 }
09ce1111
SG
1254 }
1255
f138e621 1256 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
9368acc4 1257 if (ret)
4d9b7926 1258 goto error_asoc_register;
09ce1111
SG
1259
1260 /*
1261 * If codec-handle property is missing from SSI node, we assume
1262 * that the machine driver uses new binding which does not require
1263 * SSI driver to trigger machine driver's probe.
1264 */
171d683d 1265 if (!of_get_property(np, "codec-handle", NULL))
09ce1111 1266 goto done;
09ce1111 1267
f0fba2ad 1268 /* Trigger the machine driver's probe function. The platform driver
2b81ec69 1269 * name of the machine driver is taken from /compatible property of the
f0fba2ad
LG
1270 * device tree. We also pass the address of the CPU DAI driver
1271 * structure.
1272 */
2b81ec69
SG
1273 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1274 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
f0fba2ad
LG
1275 p = strrchr(sprop, ',');
1276 if (p)
1277 sprop = p + 1;
1278 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1279 make_lowercase(name);
1280
1281 ssi_private->pdev =
38fec727 1282 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
f0fba2ad
LG
1283 if (IS_ERR(ssi_private->pdev)) {
1284 ret = PTR_ERR(ssi_private->pdev);
38fec727 1285 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
4d9b7926 1286 goto error_sound_card;
3f4b783c 1287 }
17467f23 1288
09ce1111 1289done:
85e59af2
MP
1290 if (ssi_private->dai_fmt)
1291 _fsl_ssi_set_dai_fmt(ssi_private, ssi_private->dai_fmt);
1292
f0fba2ad 1293 return 0;
87a0632b 1294
4d9b7926 1295error_sound_card:
f138e621 1296 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
9368acc4 1297
4d9b7926 1298error_irq:
3580aa10 1299 snd_soc_unregister_component(&pdev->dev);
1fab6caf 1300
4d9b7926 1301error_asoc_register:
fcdbadef 1302 if (ssi_private->soc->imx)
49da09e2 1303 fsl_ssi_imx_clean(pdev, ssi_private);
1fab6caf
TT
1304
1305error_irqmap:
4d9b7926 1306 if (ssi_private->use_dma)
2841be9a 1307 irq_dispose_mapping(ssi_private->irq);
1fab6caf 1308
87a0632b 1309 return ret;
17467f23 1310}
17467f23 1311
38fec727 1312static int fsl_ssi_remove(struct platform_device *pdev)
17467f23 1313{
38fec727 1314 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
17467f23 1315
f138e621 1316 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
9368acc4 1317
171d683d 1318 if (ssi_private->pdev)
09ce1111 1319 platform_device_unregister(ssi_private->pdev);
3580aa10 1320 snd_soc_unregister_component(&pdev->dev);
49da09e2 1321
fcdbadef 1322 if (ssi_private->soc->imx)
49da09e2
MP
1323 fsl_ssi_imx_clean(pdev, ssi_private);
1324
4d9b7926 1325 if (ssi_private->use_dma)
2841be9a 1326 irq_dispose_mapping(ssi_private->irq);
f0fba2ad
LG
1327
1328 return 0;
17467f23 1329}
f0fba2ad 1330
f07eb223 1331static struct platform_driver fsl_ssi_driver = {
f0fba2ad
LG
1332 .driver = {
1333 .name = "fsl-ssi-dai",
1334 .owner = THIS_MODULE,
1335 .of_match_table = fsl_ssi_ids,
1336 },
1337 .probe = fsl_ssi_probe,
1338 .remove = fsl_ssi_remove,
1339};
17467f23 1340
ba0a7e02 1341module_platform_driver(fsl_ssi_driver);
a454dad1 1342
f3142807 1343MODULE_ALIAS("platform:fsl-ssi-dai");
17467f23
TT
1344MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1345MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
f0fba2ad 1346MODULE_LICENSE("GPL v2");