blob: 64f65910a7d7bc1995ba05dde49698c149d17df6 [file] [log] [blame]
Timur Tabi17467f22008-01-11 18:15:26 +01001/*
2 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
3 *
4 * Author: Timur Tabi <timur@freescale.com>
5 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00006 * 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.
Timur Tabi17467f22008-01-11 18:15:26 +010011 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/interrupt.h>
16#include <linux/device.h>
17#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000019#include <linux/of_platform.h>
Timur Tabi17467f22008-01-11 18:15:26 +010020
Timur Tabi17467f22008-01-11 18:15:26 +010021#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/initval.h>
25#include <sound/soc.h>
26
27#include <asm/immap_86xx.h>
28
29#include "fsl_ssi.h"
30
31/**
32 * FSLSSI_I2S_RATES: sample rates supported by the I2S
33 *
34 * This driver currently only supports the SSI running in I2S slave mode,
35 * which means the codec determines the sample rate. Therefore, we tell
36 * ALSA that we support all rates and let the codec driver decide what rates
37 * are really supported.
38 */
39#define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
40 SNDRV_PCM_RATE_CONTINUOUS)
41
42/**
43 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
44 *
45 * This driver currently only supports the SSI running in I2S slave mode.
46 *
47 * The SSI has a limitation in that the samples must be in the same byte
48 * order as the host CPU. This is because when multiple bytes are written
49 * to the STX register, the bytes and bits must be written in the same
50 * order. The STX is a shift register, so all the bits need to be aligned
51 * (bit-endianness must match byte-endianness). Processors typically write
52 * the bits within a byte in the same order that the bytes of a word are
53 * written in. So if the host CPU is big-endian, then only big-endian
54 * samples will be written to STX properly.
55 */
56#ifdef __BIG_ENDIAN
57#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
58 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
59 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
60#else
61#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
62 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
63 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
64#endif
65
Timur Tabid5a908b2009-03-26 11:42:38 -050066/* SIER bitflag of interrupts to enable */
67#define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
68 CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
69 CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
70 CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
71 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
72
Timur Tabi17467f22008-01-11 18:15:26 +010073/**
74 * fsl_ssi_private: per-SSI private data
75 *
Timur Tabi17467f22008-01-11 18:15:26 +010076 * @ssi: pointer to the SSI's registers
77 * @ssi_phys: physical address of the SSI registers
78 * @irq: IRQ of this SSI
Timur Tabibe41e942008-07-28 17:04:39 -050079 * @first_stream: pointer to the stream that was opened first
80 * @second_stream: pointer to second stream
Timur Tabi17467f22008-01-11 18:15:26 +010081 * @playback: the number of playback streams opened
82 * @capture: the number of capture streams opened
Timur Tabia454dad2009-03-05 17:23:37 -060083 * @asynchronous: 0=synchronous mode, 1=asynchronous mode
Timur Tabi17467f22008-01-11 18:15:26 +010084 * @cpu_dai: the CPU DAI for this device
85 * @dev_attr: the sysfs device attribute structure
86 * @stats: SSI statistics
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000087 * @name: name for this device
Timur Tabi17467f22008-01-11 18:15:26 +010088 */
89struct fsl_ssi_private {
Timur Tabi17467f22008-01-11 18:15:26 +010090 struct ccsr_ssi __iomem *ssi;
91 dma_addr_t ssi_phys;
92 unsigned int irq;
Timur Tabibe41e942008-07-28 17:04:39 -050093 struct snd_pcm_substream *first_stream;
94 struct snd_pcm_substream *second_stream;
Timur Tabi17467f22008-01-11 18:15:26 +010095 unsigned int playback;
96 unsigned int capture;
Timur Tabia454dad2009-03-05 17:23:37 -060097 int asynchronous;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000098 struct snd_soc_dai_driver cpu_dai_drv;
Timur Tabi17467f22008-01-11 18:15:26 +010099 struct device_attribute dev_attr;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000100 struct platform_device *pdev;
Timur Tabi17467f22008-01-11 18:15:26 +0100101
102 struct {
103 unsigned int rfrc;
104 unsigned int tfrc;
105 unsigned int cmdau;
106 unsigned int cmddu;
107 unsigned int rxt;
108 unsigned int rdr1;
109 unsigned int rdr0;
110 unsigned int tde1;
111 unsigned int tde0;
112 unsigned int roe1;
113 unsigned int roe0;
114 unsigned int tue1;
115 unsigned int tue0;
116 unsigned int tfs;
117 unsigned int rfs;
118 unsigned int tls;
119 unsigned int rls;
120 unsigned int rff1;
121 unsigned int rff0;
122 unsigned int tfe1;
123 unsigned int tfe0;
124 } stats;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000125
126 char name[1];
Timur Tabi17467f22008-01-11 18:15:26 +0100127};
128
129/**
130 * fsl_ssi_isr: SSI interrupt handler
131 *
132 * Although it's possible to use the interrupt handler to send and receive
133 * data to/from the SSI, we use the DMA instead. Programming is more
134 * complicated, but the performance is much better.
135 *
136 * This interrupt handler is used only to gather statistics.
137 *
138 * @irq: IRQ of the SSI device
139 * @dev_id: pointer to the ssi_private structure for this SSI device
140 */
141static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
142{
143 struct fsl_ssi_private *ssi_private = dev_id;
144 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
145 irqreturn_t ret = IRQ_NONE;
146 __be32 sisr;
147 __be32 sisr2 = 0;
148
149 /* We got an interrupt, so read the status register to see what we
150 were interrupted for. We mask it with the Interrupt Enable register
151 so that we only check for events that we're interested in.
152 */
Timur Tabid5a908b2009-03-26 11:42:38 -0500153 sisr = in_be32(&ssi->sisr) & SIER_FLAGS;
Timur Tabi17467f22008-01-11 18:15:26 +0100154
155 if (sisr & CCSR_SSI_SISR_RFRC) {
156 ssi_private->stats.rfrc++;
157 sisr2 |= CCSR_SSI_SISR_RFRC;
158 ret = IRQ_HANDLED;
159 }
160
161 if (sisr & CCSR_SSI_SISR_TFRC) {
162 ssi_private->stats.tfrc++;
163 sisr2 |= CCSR_SSI_SISR_TFRC;
164 ret = IRQ_HANDLED;
165 }
166
167 if (sisr & CCSR_SSI_SISR_CMDAU) {
168 ssi_private->stats.cmdau++;
169 ret = IRQ_HANDLED;
170 }
171
172 if (sisr & CCSR_SSI_SISR_CMDDU) {
173 ssi_private->stats.cmddu++;
174 ret = IRQ_HANDLED;
175 }
176
177 if (sisr & CCSR_SSI_SISR_RXT) {
178 ssi_private->stats.rxt++;
179 ret = IRQ_HANDLED;
180 }
181
182 if (sisr & CCSR_SSI_SISR_RDR1) {
183 ssi_private->stats.rdr1++;
184 ret = IRQ_HANDLED;
185 }
186
187 if (sisr & CCSR_SSI_SISR_RDR0) {
188 ssi_private->stats.rdr0++;
189 ret = IRQ_HANDLED;
190 }
191
192 if (sisr & CCSR_SSI_SISR_TDE1) {
193 ssi_private->stats.tde1++;
194 ret = IRQ_HANDLED;
195 }
196
197 if (sisr & CCSR_SSI_SISR_TDE0) {
198 ssi_private->stats.tde0++;
199 ret = IRQ_HANDLED;
200 }
201
202 if (sisr & CCSR_SSI_SISR_ROE1) {
203 ssi_private->stats.roe1++;
204 sisr2 |= CCSR_SSI_SISR_ROE1;
205 ret = IRQ_HANDLED;
206 }
207
208 if (sisr & CCSR_SSI_SISR_ROE0) {
209 ssi_private->stats.roe0++;
210 sisr2 |= CCSR_SSI_SISR_ROE0;
211 ret = IRQ_HANDLED;
212 }
213
214 if (sisr & CCSR_SSI_SISR_TUE1) {
215 ssi_private->stats.tue1++;
216 sisr2 |= CCSR_SSI_SISR_TUE1;
217 ret = IRQ_HANDLED;
218 }
219
220 if (sisr & CCSR_SSI_SISR_TUE0) {
221 ssi_private->stats.tue0++;
222 sisr2 |= CCSR_SSI_SISR_TUE0;
223 ret = IRQ_HANDLED;
224 }
225
226 if (sisr & CCSR_SSI_SISR_TFS) {
227 ssi_private->stats.tfs++;
228 ret = IRQ_HANDLED;
229 }
230
231 if (sisr & CCSR_SSI_SISR_RFS) {
232 ssi_private->stats.rfs++;
233 ret = IRQ_HANDLED;
234 }
235
236 if (sisr & CCSR_SSI_SISR_TLS) {
237 ssi_private->stats.tls++;
238 ret = IRQ_HANDLED;
239 }
240
241 if (sisr & CCSR_SSI_SISR_RLS) {
242 ssi_private->stats.rls++;
243 ret = IRQ_HANDLED;
244 }
245
246 if (sisr & CCSR_SSI_SISR_RFF1) {
247 ssi_private->stats.rff1++;
248 ret = IRQ_HANDLED;
249 }
250
251 if (sisr & CCSR_SSI_SISR_RFF0) {
252 ssi_private->stats.rff0++;
253 ret = IRQ_HANDLED;
254 }
255
256 if (sisr & CCSR_SSI_SISR_TFE1) {
257 ssi_private->stats.tfe1++;
258 ret = IRQ_HANDLED;
259 }
260
261 if (sisr & CCSR_SSI_SISR_TFE0) {
262 ssi_private->stats.tfe0++;
263 ret = IRQ_HANDLED;
264 }
265
266 /* Clear the bits that we set */
267 if (sisr2)
268 out_be32(&ssi->sisr, sisr2);
269
270 return ret;
271}
272
273/**
274 * fsl_ssi_startup: create a new substream
275 *
276 * This is the first function called when a stream is opened.
277 *
278 * If this is the first stream open, then grab the IRQ and program most of
279 * the SSI registers.
280 */
Mark Browndee89c42008-11-18 22:11:38 +0000281static int fsl_ssi_startup(struct snd_pcm_substream *substream,
282 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100283{
284 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000285 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100286
287 /*
288 * If this is the first stream opened, then request the IRQ
289 * and initialize the SSI registers.
290 */
291 if (!ssi_private->playback && !ssi_private->capture) {
292 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
293 int ret;
294
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000295 /* The 'name' should not have any slashes in it. */
Timur Tabi17467f22008-01-11 18:15:26 +0100296 ret = request_irq(ssi_private->irq, fsl_ssi_isr, 0,
297 ssi_private->name, ssi_private);
298 if (ret < 0) {
299 dev_err(substream->pcm->card->dev,
300 "could not claim irq %u\n", ssi_private->irq);
301 return ret;
302 }
303
304 /*
305 * Section 16.5 of the MPC8610 reference manual says that the
306 * SSI needs to be disabled before updating the registers we set
307 * here.
308 */
309 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
310
311 /*
312 * Program the SSI into I2S Slave Non-Network Synchronous mode.
313 * Also enable the transmit and receive FIFO.
314 *
315 * FIXME: Little-endian samples require a different shift dir
316 */
Timur Tabia454dad2009-03-05 17:23:37 -0600317 clrsetbits_be32(&ssi->scr,
318 CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
319 CCSR_SSI_SCR_TFR_CLK_DIS | CCSR_SSI_SCR_I2S_MODE_SLAVE
320 | (ssi_private->asynchronous ? 0 : CCSR_SSI_SCR_SYN));
Timur Tabi17467f22008-01-11 18:15:26 +0100321
322 out_be32(&ssi->stcr,
323 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
324 CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
325 CCSR_SSI_STCR_TSCKP);
326
327 out_be32(&ssi->srcr,
328 CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
329 CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
330 CCSR_SSI_SRCR_RSCKP);
331
332 /*
333 * The DC and PM bits are only used if the SSI is the clock
334 * master.
335 */
336
337 /* 4. Enable the interrupts and DMA requests */
Timur Tabid5a908b2009-03-26 11:42:38 -0500338 out_be32(&ssi->sier, SIER_FLAGS);
Timur Tabi17467f22008-01-11 18:15:26 +0100339
340 /*
341 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We
342 * don't use FIFO 1. Since the SSI only supports stereo, the
343 * watermark should never be an odd number.
344 */
345 out_be32(&ssi->sfcsr,
346 CCSR_SSI_SFCSR_TFWM0(6) | CCSR_SSI_SFCSR_RFWM0(2));
347
348 /*
349 * We keep the SSI disabled because if we enable it, then the
350 * DMA controller will start. It's not supposed to start until
351 * the SCR.TE (or SCR.RE) bit is set, but it does anyway. The
352 * DMA controller will transfer one "BWC" of data (i.e. the
353 * amount of data that the MR.BWC bits are set to). The reason
354 * this is bad is because at this point, the PCM driver has not
355 * finished initializing the DMA controller.
356 */
357 }
358
Timur Tabibe41e942008-07-28 17:04:39 -0500359 if (!ssi_private->first_stream)
360 ssi_private->first_stream = substream;
361 else {
362 /* This is the second stream open, so we need to impose sample
363 * rate and maybe sample size constraints. Note that this can
364 * cause a race condition if the second stream is opened before
365 * the first stream is fully initialized.
366 *
367 * We provide some protection by checking to make sure the first
368 * stream is initialized, but it's not perfect. ALSA sometimes
369 * re-initializes the driver with a different sample rate or
370 * size. If the second stream is opened before the first stream
371 * has received its final parameters, then the second stream may
372 * be constrained to the wrong sample rate or size.
373 *
374 * FIXME: This code does not handle opening and closing streams
375 * repeatedly. If you open two streams and then close the first
376 * one, you may not be able to open another stream until you
377 * close the second one as well.
378 */
379 struct snd_pcm_runtime *first_runtime =
380 ssi_private->first_stream->runtime;
381
Mark Brown08d15f02009-05-23 10:41:05 +0100382 if (!first_runtime->sample_bits) {
Timur Tabibe41e942008-07-28 17:04:39 -0500383 dev_err(substream->pcm->card->dev,
Mark Brown08d15f02009-05-23 10:41:05 +0100384 "set sample size in %s stream first\n",
Timur Tabibe41e942008-07-28 17:04:39 -0500385 substream->stream == SNDRV_PCM_STREAM_PLAYBACK
386 ? "capture" : "playback");
387 return -EAGAIN;
388 }
389
Timur Tabia454dad2009-03-05 17:23:37 -0600390 /* If we're in synchronous mode, then we need to constrain
391 * the sample size as well. We don't support independent sample
392 * rates in asynchronous mode.
393 */
394 if (!ssi_private->asynchronous)
395 snd_pcm_hw_constraint_minmax(substream->runtime,
396 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
397 first_runtime->sample_bits,
398 first_runtime->sample_bits);
Timur Tabibe41e942008-07-28 17:04:39 -0500399
400 ssi_private->second_stream = substream;
401 }
402
Timur Tabi17467f22008-01-11 18:15:26 +0100403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 ssi_private->playback++;
405
406 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
407 ssi_private->capture++;
408
409 return 0;
410}
411
412/**
Timur Tabi85ef2372009-02-05 17:56:02 -0600413 * fsl_ssi_hw_params - program the sample size
Timur Tabi17467f22008-01-11 18:15:26 +0100414 *
415 * Most of the SSI registers have been programmed in the startup function,
416 * but the word length must be programmed here. Unfortunately, programming
417 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
418 * cause a problem with supporting simultaneous playback and capture. If
419 * the SSI is already playing a stream, then that stream may be temporarily
420 * stopped when you start capture.
421 *
422 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
423 * clock master.
424 */
Timur Tabi85ef2372009-02-05 17:56:02 -0600425static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
426 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100427{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000428 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100429
Timur Tabibe41e942008-07-28 17:04:39 -0500430 if (substream == ssi_private->first_stream) {
Timur Tabi85ef2372009-02-05 17:56:02 -0600431 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
432 unsigned int sample_size =
433 snd_pcm_format_width(params_format(hw_params));
Timur Tabia454dad2009-03-05 17:23:37 -0600434 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
Timur Tabi17467f22008-01-11 18:15:26 +0100435
Timur Tabibe41e942008-07-28 17:04:39 -0500436 /* The SSI should always be disabled at this points (SSIEN=0) */
Timur Tabi17467f22008-01-11 18:15:26 +0100437
Timur Tabibe41e942008-07-28 17:04:39 -0500438 /* In synchronous mode, the SSI uses STCCR for capture */
Timur Tabia454dad2009-03-05 17:23:37 -0600439 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
440 !ssi_private->asynchronous)
441 clrsetbits_be32(&ssi->stccr,
442 CCSR_SSI_SxCCR_WL_MASK, wl);
443 else
444 clrsetbits_be32(&ssi->srccr,
445 CCSR_SSI_SxCCR_WL_MASK, wl);
Timur Tabibe41e942008-07-28 17:04:39 -0500446 }
Timur Tabi17467f22008-01-11 18:15:26 +0100447
448 return 0;
449}
450
451/**
452 * fsl_ssi_trigger: start and stop the DMA transfer.
453 *
454 * This function is called by ALSA to start, stop, pause, and resume the DMA
455 * transfer of data.
456 *
457 * The DMA channel is in external master start and pause mode, which
458 * means the SSI completely controls the flow of data.
459 */
Mark Browndee89c42008-11-18 22:11:38 +0000460static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
461 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100462{
463 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100465 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
466
467 switch (cmd) {
468 case SNDRV_PCM_TRIGGER_START:
Timur Tabi3a638ff2009-03-06 18:39:34 -0600469 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
Timur Tabi17467f22008-01-11 18:15:26 +0100470 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Timur Tabia4d11fe2009-03-25 18:20:37 -0500471 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Timur Tabibe41e942008-07-28 17:04:39 -0500472 setbits32(&ssi->scr,
473 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
Timur Tabia4d11fe2009-03-25 18:20:37 -0500474 else
Timur Tabibe41e942008-07-28 17:04:39 -0500475 setbits32(&ssi->scr,
476 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
Timur Tabi17467f22008-01-11 18:15:26 +0100477 break;
478
479 case SNDRV_PCM_TRIGGER_STOP:
Timur Tabi17467f22008-01-11 18:15:26 +0100480 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
481 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
482 clrbits32(&ssi->scr, CCSR_SSI_SCR_TE);
483 else
484 clrbits32(&ssi->scr, CCSR_SSI_SCR_RE);
485 break;
486
487 default:
488 return -EINVAL;
489 }
490
491 return 0;
492}
493
494/**
495 * fsl_ssi_shutdown: shutdown the SSI
496 *
497 * Shutdown the SSI if there are no other substreams open.
498 */
Mark Browndee89c42008-11-18 22:11:38 +0000499static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
500 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100501{
502 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000503 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100504
505 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
506 ssi_private->playback--;
507
508 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
509 ssi_private->capture--;
510
Timur Tabibe41e942008-07-28 17:04:39 -0500511 if (ssi_private->first_stream == substream)
512 ssi_private->first_stream = ssi_private->second_stream;
513
514 ssi_private->second_stream = NULL;
515
Timur Tabi17467f22008-01-11 18:15:26 +0100516 /*
517 * If this is the last active substream, disable the SSI and release
518 * the IRQ.
519 */
520 if (!ssi_private->playback && !ssi_private->capture) {
521 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
522
523 clrbits32(&ssi->scr, CCSR_SSI_SCR_SSIEN);
524
525 free_irq(ssi_private->irq, ssi_private);
526 }
527}
528
Eric Miao6335d052009-03-03 09:41:00 +0800529static struct snd_soc_dai_ops fsl_ssi_dai_ops = {
530 .startup = fsl_ssi_startup,
531 .hw_params = fsl_ssi_hw_params,
532 .shutdown = fsl_ssi_shutdown,
533 .trigger = fsl_ssi_trigger,
Eric Miao6335d052009-03-03 09:41:00 +0800534};
535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536/* Template for the CPU dai driver structure */
537static struct snd_soc_dai_driver fsl_ssi_dai_template = {
Timur Tabi17467f22008-01-11 18:15:26 +0100538 .playback = {
539 /* The SSI does not support monaural audio. */
540 .channels_min = 2,
541 .channels_max = 2,
542 .rates = FSLSSI_I2S_RATES,
543 .formats = FSLSSI_I2S_FORMATS,
544 },
545 .capture = {
546 .channels_min = 2,
547 .channels_max = 2,
548 .rates = FSLSSI_I2S_RATES,
549 .formats = FSLSSI_I2S_FORMATS,
550 },
Eric Miao6335d052009-03-03 09:41:00 +0800551 .ops = &fsl_ssi_dai_ops,
Timur Tabi17467f22008-01-11 18:15:26 +0100552};
553
Timur Tabid5a908b2009-03-26 11:42:38 -0500554/* Show the statistics of a flag only if its interrupt is enabled. The
555 * compiler will optimze this code to a no-op if the interrupt is not
556 * enabled.
557 */
558#define SIER_SHOW(flag, name) \
559 do { \
560 if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \
561 length += sprintf(buf + length, #name "=%u\n", \
562 ssi_private->stats.name); \
563 } while (0)
564
565
Timur Tabi17467f22008-01-11 18:15:26 +0100566/**
567 * fsl_sysfs_ssi_show: display SSI statistics
568 *
Timur Tabid5a908b2009-03-26 11:42:38 -0500569 * Display the statistics for the current SSI device. To avoid confusion,
570 * we only show those counts that are enabled.
Timur Tabi17467f22008-01-11 18:15:26 +0100571 */
572static ssize_t fsl_sysfs_ssi_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
575 struct fsl_ssi_private *ssi_private =
Timur Tabid5a908b2009-03-26 11:42:38 -0500576 container_of(attr, struct fsl_ssi_private, dev_attr);
577 ssize_t length = 0;
Timur Tabi17467f22008-01-11 18:15:26 +0100578
Timur Tabid5a908b2009-03-26 11:42:38 -0500579 SIER_SHOW(RFRC_EN, rfrc);
580 SIER_SHOW(TFRC_EN, tfrc);
581 SIER_SHOW(CMDAU_EN, cmdau);
582 SIER_SHOW(CMDDU_EN, cmddu);
583 SIER_SHOW(RXT_EN, rxt);
584 SIER_SHOW(RDR1_EN, rdr1);
585 SIER_SHOW(RDR0_EN, rdr0);
586 SIER_SHOW(TDE1_EN, tde1);
587 SIER_SHOW(TDE0_EN, tde0);
588 SIER_SHOW(ROE1_EN, roe1);
589 SIER_SHOW(ROE0_EN, roe0);
590 SIER_SHOW(TUE1_EN, tue1);
591 SIER_SHOW(TUE0_EN, tue0);
592 SIER_SHOW(TFS_EN, tfs);
593 SIER_SHOW(RFS_EN, rfs);
594 SIER_SHOW(TLS_EN, tls);
595 SIER_SHOW(RLS_EN, rls);
596 SIER_SHOW(RFF1_EN, rff1);
597 SIER_SHOW(RFF0_EN, rff0);
598 SIER_SHOW(TFE1_EN, tfe1);
599 SIER_SHOW(TFE0_EN, tfe0);
Timur Tabi17467f22008-01-11 18:15:26 +0100600
601 return length;
602}
603
604/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 * Make every character in a string lower-case
Timur Tabi17467f22008-01-11 18:15:26 +0100606 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000607static void make_lowercase(char *s)
Timur Tabi17467f22008-01-11 18:15:26 +0100608{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 char *p = s;
610 char c;
611
612 while ((c = *p)) {
613 if ((c >= 'A') && (c <= 'Z'))
614 *p = c + ('a' - 'A');
615 p++;
616 }
617}
618
619static int __devinit fsl_ssi_probe(struct of_device *of_dev,
620 const struct of_device_id *match)
621{
Timur Tabi17467f22008-01-11 18:15:26 +0100622 struct fsl_ssi_private *ssi_private;
623 int ret = 0;
624 struct device_attribute *dev_attr;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000625 struct device_node *np = of_dev->dev.of_node;
626 const char *p, *sprop;
627 struct resource res;
628 char name[64];
Timur Tabi17467f22008-01-11 18:15:26 +0100629
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 /* We are only interested in SSIs with a codec phandle in them, so let's
631 * make sure this SSI has one.
632 */
633 if (!of_get_property(np, "codec-handle", NULL))
634 return -ENODEV;
635
636 /* We only support the SSI in "I2S Slave" mode */
637 sprop = of_get_property(np, "fsl,mode", NULL);
638 if (!sprop || strcmp(sprop, "i2s-slave")) {
639 dev_notice(&of_dev->dev, "mode %s is unsupported\n", sprop);
640 return -ENODEV;
Timur Tabi17467f22008-01-11 18:15:26 +0100641 }
Timur Tabi17467f22008-01-11 18:15:26 +0100642
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 /* The DAI name is the last part of the full name of the node. */
644 p = strrchr(np->full_name, '/') + 1;
645 ssi_private = kzalloc(sizeof(struct fsl_ssi_private) + strlen(p),
646 GFP_KERNEL);
647 if (!ssi_private) {
648 dev_err(&of_dev->dev, "could not allocate DAI object\n");
649 return -ENOMEM;
650 }
Timur Tabi17467f22008-01-11 18:15:26 +0100651
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000652 strcpy(ssi_private->name, p);
Timur Tabi17467f22008-01-11 18:15:26 +0100653
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 /* Initialize this copy of the CPU DAI driver structure */
655 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
656 sizeof(fsl_ssi_dai_template));
657 ssi_private->cpu_dai_drv.name = ssi_private->name;
658
659 /* Get the addresses and IRQ */
660 ret = of_address_to_resource(np, 0, &res);
661 if (ret) {
662 dev_err(&of_dev->dev, "could not determine device resources\n");
663 kfree(ssi_private);
664 return ret;
665 }
666 ssi_private->ssi = ioremap(res.start, 1 + res.end - res.start);
667 ssi_private->ssi_phys = res.start;
668 ssi_private->irq = irq_of_parse_and_map(np, 0);
669
670 /* Are the RX and the TX clocks locked? */
671 if (of_find_property(np, "fsl,ssi-asynchronous", NULL))
672 ssi_private->asynchronous = 1;
673 else
674 ssi_private->cpu_dai_drv.symmetric_rates = 1;
Timur Tabi17467f22008-01-11 18:15:26 +0100675
676 /* Initialize the the device_attribute structure */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000677 dev_attr = &ssi_private->dev_attr;
678 dev_attr->attr.name = "statistics";
Timur Tabi17467f22008-01-11 18:15:26 +0100679 dev_attr->attr.mode = S_IRUGO;
680 dev_attr->show = fsl_sysfs_ssi_show;
681
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 ret = device_create_file(&of_dev->dev, dev_attr);
Timur Tabi17467f22008-01-11 18:15:26 +0100683 if (ret) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000684 dev_err(&of_dev->dev, "could not create sysfs %s file\n",
Timur Tabi17467f22008-01-11 18:15:26 +0100685 ssi_private->dev_attr.attr.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000686 kfree(ssi_private);
687 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100688 }
689
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000690 /* Register with ASoC */
691 dev_set_drvdata(&of_dev->dev, ssi_private);
Mark Brown3f4b7832008-12-03 19:26:35 +0000692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000693 ret = snd_soc_register_dai(&of_dev->dev, &ssi_private->cpu_dai_drv);
Mark Brown3f4b7832008-12-03 19:26:35 +0000694 if (ret != 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000695 dev_err(&of_dev->dev, "failed to register DAI: %d\n", ret);
696 kfree(ssi_private);
697 return ret;
Mark Brown3f4b7832008-12-03 19:26:35 +0000698 }
Timur Tabi17467f22008-01-11 18:15:26 +0100699
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700 /* Trigger the machine driver's probe function. The platform driver
701 * name of the machine driver is taken from the /model property of the
702 * device tree. We also pass the address of the CPU DAI driver
703 * structure.
704 */
705 sprop = of_get_property(of_find_node_by_path("/"), "model", NULL);
706 /* Sometimes the model name has a "fsl," prefix, so we strip that. */
707 p = strrchr(sprop, ',');
708 if (p)
709 sprop = p + 1;
710 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
711 make_lowercase(name);
712
713 ssi_private->pdev =
714 platform_device_register_data(&of_dev->dev, name, 0, NULL, 0);
715 if (IS_ERR(ssi_private->pdev)) {
716 ret = PTR_ERR(ssi_private->pdev);
717 dev_err(&of_dev->dev, "failed to register platform: %d\n", ret);
718 kfree(ssi_private);
719 return ret;
720 }
721
722 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +0100723}
Timur Tabi17467f22008-01-11 18:15:26 +0100724
725/**
Liam Girdwood8cf7b2b2008-07-07 16:08:00 +0100726 * fsl_ssi_destroy_dai: destroy the snd_soc_dai object
Timur Tabi17467f22008-01-11 18:15:26 +0100727 *
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 * This function undoes the operations of fsl_ssi_probe()
Timur Tabi17467f22008-01-11 18:15:26 +0100729 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000730static int fsl_ssi_remove(struct of_device *of_dev)
Timur Tabi17467f22008-01-11 18:15:26 +0100731{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&of_dev->dev);
Timur Tabi17467f22008-01-11 18:15:26 +0100733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 platform_device_unregister(ssi_private->pdev);
735 snd_soc_unregister_dai(&of_dev->dev);
736 device_remove_file(&of_dev->dev, &ssi_private->dev_attr);
Mark Brown3f4b7832008-12-03 19:26:35 +0000737
Timur Tabi17467f22008-01-11 18:15:26 +0100738 kfree(ssi_private);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 dev_set_drvdata(&of_dev->dev, NULL);
740
741 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +0100742}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000743
744static const struct of_device_id fsl_ssi_ids[] = {
745 { .compatible = "fsl,mpc8610-ssi", },
746 {}
747};
748MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
749
750static struct of_platform_driver fsl_ssi_driver = {
751 .driver = {
752 .name = "fsl-ssi-dai",
753 .owner = THIS_MODULE,
754 .of_match_table = fsl_ssi_ids,
755 },
756 .probe = fsl_ssi_probe,
757 .remove = fsl_ssi_remove,
758};
Timur Tabi17467f22008-01-11 18:15:26 +0100759
Timur Tabia454dad2009-03-05 17:23:37 -0600760static int __init fsl_ssi_init(void)
761{
762 printk(KERN_INFO "Freescale Synchronous Serial Interface (SSI) ASoC Driver\n");
763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 return of_register_platform_driver(&fsl_ssi_driver);
Timur Tabia454dad2009-03-05 17:23:37 -0600765}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000766
767static void __exit fsl_ssi_exit(void)
768{
769 of_unregister_platform_driver(&fsl_ssi_driver);
770}
771
Timur Tabia454dad2009-03-05 17:23:37 -0600772module_init(fsl_ssi_init);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773module_exit(fsl_ssi_exit);
Timur Tabia454dad2009-03-05 17:23:37 -0600774
Timur Tabi17467f22008-01-11 18:15:26 +0100775MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
776MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777MODULE_LICENSE("GPL v2");