blob: fa650112925a07e1e39af1a919a4f6f528fb1360 [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.
Markus Pargmannde623ec2013-07-27 13:31:53 +020011 *
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.
Timur Tabi17467f22008-01-11 18:15:26 +010031 */
32
33#include <linux/init.h>
Shawn Guodfa1a102012-03-16 16:56:42 +080034#include <linux/io.h>
Timur Tabi17467f22008-01-11 18:15:26 +010035#include <linux/module.h>
36#include <linux/interrupt.h>
Shawn Guo95cd98f2012-03-29 10:53:41 +080037#include <linux/clk.h>
Timur Tabi17467f22008-01-11 18:15:26 +010038#include <linux/device.h>
39#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nicolin Chenaafa85e2013-12-12 18:44:45 +080041#include <linux/spinlock.h>
Shawn Guodfa1a102012-03-16 16:56:42 +080042#include <linux/of_address.h>
43#include <linux/of_irq.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000044#include <linux/of_platform.h>
Timur Tabi17467f22008-01-11 18:15:26 +010045
Timur Tabi17467f22008-01-11 18:15:26 +010046#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>
Lars-Peter Clausena8909c92013-04-03 11:06:04 +020051#include <sound/dmaengine_pcm.h>
Timur Tabi17467f22008-01-11 18:15:26 +010052
Timur Tabi17467f22008-01-11 18:15:26 +010053#include "fsl_ssi.h"
Shawn Guo09ce1112012-03-16 16:56:43 +080054#include "imx-pcm.h"
Timur Tabi17467f22008-01-11 18:15:26 +010055
Shawn Guodfa1a102012-03-16 16:56:42 +080056#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)
Mark Brown0a9eaa32013-07-19 11:40:13 +010060#else
Shawn Guodfa1a102012-03-16 16:56:42 +080061#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
Timur Tabi17467f22008-01-11 18:15:26 +010075/**
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 */
Lars-Peter Clausen24710c92014-01-11 10:24:41 +010083#define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
Timur Tabi17467f22008-01-11 18:15:26 +010084
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
Markus Pargmann9368acc2013-12-20 14:11:29 +0100109#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)
Markus Pargmannc1953bf2013-12-20 14:11:30 +0100115
116enum fsl_ssi_type {
117 FSL_SSI_MCP8610,
118 FSL_SSI_MX21,
Markus Pargmann0888efd2013-12-20 14:11:31 +0100119 FSL_SSI_MX35,
Markus Pargmannc1953bf2013-12-20 14:11:30 +0100120 FSL_SSI_MX51,
121};
122
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100123struct 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};
Timur Tabid5a908b2009-03-26 11:42:38 -0500134
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200135struct fsl_ssi_soc_data {
136 bool imx;
137 bool offline_config;
138 u32 sisr_write_mask;
139};
140
Timur Tabi17467f22008-01-11 18:15:26 +0100141/**
142 * fsl_ssi_private: per-SSI private data
143 *
Timur Tabi17467f22008-01-11 18:15:26 +0100144 * @ssi: pointer to the SSI's registers
145 * @ssi_phys: physical address of the SSI registers
146 * @irq: IRQ of this SSI
Timur Tabi17467f22008-01-11 18:15:26 +0100147 * @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 {
Timur Tabi17467f22008-01-11 18:15:26 +0100154 struct ccsr_ssi __iomem *ssi;
155 dma_addr_t ssi_phys;
156 unsigned int irq;
Timur Tabi8e9d8692010-08-06 12:16:12 -0500157 unsigned int fifo_depth;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000158 struct snd_soc_dai_driver cpu_dai_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 struct platform_device *pdev;
Markus Pargmann171d6832014-04-28 12:54:48 +0200160 unsigned int dai_fmt;
Timur Tabi17467f22008-01-11 18:15:26 +0100161
Markus Pargmannde623ec2013-07-27 13:31:53 +0200162 bool use_dma;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800163 bool baudclk_locked;
Nicolin Chen0da9e552013-11-13 22:55:26 +0800164 bool use_dual_fifo;
Nicolin Chen2924a992013-12-02 23:29:03 +0800165 u8 i2s_mode;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800166 spinlock_t baudclk_lock;
167 struct clk *baudclk;
Shawn Guo95cd98f2012-03-29 10:53:41 +0800168 struct clk *clk;
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200169 struct snd_dmaengine_dai_dma_data dma_params_tx;
170 struct snd_dmaengine_dai_dma_data dma_params_rx;
Markus Pargmannde623ec2013-07-27 13:31:53 +0200171 struct imx_pcm_fiq_params fiq_params;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100172 /* Register values for rx/tx configuration */
173 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
Shawn Guo09ce1112012-03-16 16:56:43 +0800174
Markus Pargmannf138e622014-04-28 12:54:43 +0200175 struct fsl_ssi_dbg dbg_stats;
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200176
177 const struct fsl_ssi_soc_data *soc;
Timur Tabi17467f22008-01-11 18:15:26 +0100178};
179
Markus Pargmann171d6832014-04-28 12:54:48 +0200180/*
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 */
Markus Pargmann171d6832014-04-28 12:54:48 +0200195
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200196static 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);
Markus Pargmann171d6832014-04-28 12:54:48 +0200237}
238
Timur Tabi17467f22008-01-11 18:15:26 +0100239/**
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;
Timur Tabi17467f22008-01-11 18:15:26 +0100255 __be32 sisr;
Markus Pargmann0888efd2013-12-20 14:11:31 +0100256 __be32 sisr2;
Timur Tabi17467f22008-01-11 18:15:26 +0100257
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 */
Markus Pargmannf138e622014-04-28 12:54:43 +0200262 sisr = read_ssi(&ssi->sisr);
Timur Tabi17467f22008-01-11 18:15:26 +0100263
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200264 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
Timur Tabi17467f22008-01-11 18:15:26 +0100265 /* Clear the bits that we set */
266 if (sisr2)
Shawn Guodfa1a102012-03-16 16:56:42 +0800267 write_ssi(sisr2, &ssi->sisr);
Timur Tabi17467f22008-01-11 18:15:26 +0100268
Markus Pargmannf138e622014-04-28 12:54:43 +0200269 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
270
271 return IRQ_HANDLED;
Timur Tabi17467f22008-01-11 18:15:26 +0100272}
273
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100274/*
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
294/*
Markus Pargmann65c961c2014-04-28 12:54:42 +0200295 * 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
314/*
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100315 * 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);
Markus Pargmann65c961c2014-04-28 12:54:42 +0200326 int keep_active;
327
328 if (nr_active_streams - 1 > 0)
329 keep_active = 1;
330 else
331 keep_active = 0;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100332
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) {
Markus Pargmann65c961c2014-04-28 12:54:42 +0200342 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
343 keep_active);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100344 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 */
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200352 if (ssi_private->soc->offline_config) {
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100353 if ((enable && !nr_active_streams) ||
Markus Pargmann65c961c2014-04-28 12:54:42 +0200354 (!enable && !keep_active))
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100355 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*/
Markus Pargmann65c961c2014-04-28 12:54:42 +0200383 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);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100389
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
Markus Pargmann6de83872013-12-20 14:11:34 +0100412/*
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
Markus Pargmann171d6832014-04-28 12:54:48 +0200428 if (!fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmann6de83872013-12-20 14:11:34 +0100429 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
Markus Pargmannd8764642013-11-20 10:04:15 +0100447static 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
Timur Tabi17467f22008-01-11 18:15:26 +0100477/**
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 */
Mark Browndee89c42008-11-18 22:11:38 +0000485static int fsl_ssi_startup(struct snd_pcm_substream *substream,
486 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100487{
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Timur Tabi5e538ec2011-09-13 12:59:37 -0500489 struct fsl_ssi_private *ssi_private =
490 snd_soc_dai_get_drvdata(rtd->cpu_dai);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800491 unsigned long flags;
Timur Tabi17467f22008-01-11 18:15:26 +0100492
Markus Pargmann171d6832014-04-28 12:54:48 +0200493 if (!dai->active && !fsl_ssi_is_ac97(ssi_private)) {
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800494 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
495 ssi_private->baudclk_locked = false;
496 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
497 }
Timur Tabibe41e942008-07-28 17:04:39 -0500498
Nicolin Chen0da9e552013-11-13 22:55:26 +0800499 /* 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
Timur Tabi17467f22008-01-11 18:15:26 +0100508 return 0;
509}
510
511/**
Sascha Haueree9daad2014-04-28 12:54:52 +0200512 * 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
607/**
Timur Tabi85ef2372009-02-05 17:56:02 -0600608 * fsl_ssi_hw_params - program the sample size
Timur Tabi17467f22008-01-11 18:15:26 +0100609 *
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 */
Timur Tabi85ef2372009-02-05 17:56:02 -0600620static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
621 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100622{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500624 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
Nicolin Chen2924a992013-12-02 23:29:03 +0800625 unsigned int channels = params_channels(hw_params);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500626 unsigned int sample_size =
627 snd_pcm_format_width(params_format(hw_params));
628 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
Shawn Guodfa1a102012-03-16 16:56:42 +0800629 int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
Timur Tabi17467f22008-01-11 18:15:26 +0100630
Timur Tabi5e538ec2011-09-13 12:59:37 -0500631 /*
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;
Timur Tabi17467f22008-01-11 18:15:26 +0100637
Timur Tabi5e538ec2011-09-13 12:59:37 -0500638 /*
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 */
Timur Tabi17467f22008-01-11 18:15:26 +0100647
Timur Tabi5e538ec2011-09-13 12:59:37 -0500648 /* 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)
Shawn Guodfa1a102012-03-16 16:56:42 +0800651 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500652 else
Shawn Guodfa1a102012-03-16 16:56:42 +0800653 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
Timur Tabi17467f22008-01-11 18:15:26 +0100654
Markus Pargmann171d6832014-04-28 12:54:48 +0200655 if (!fsl_ssi_is_ac97(ssi_private))
Nicolin Chen2924a992013-12-02 23:29:03 +0800656 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
Timur Tabi17467f22008-01-11 18:15:26 +0100660 return 0;
661}
662
663/**
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800664 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
665 */
666static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
667{
668 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
669 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
670 u32 strcr = 0, stcr, srcr, scr, mask;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100671 u8 wm;
672
Markus Pargmann171d6832014-04-28 12:54:48 +0200673 ssi_private->dai_fmt = fmt;
674
Markus Pargmann2b0db992014-03-15 13:44:09 +0100675 fsl_ssi_setup_reg_vals(ssi_private);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800676
677 scr = read_ssi(&ssi->scr) & ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
Markus Pargmann50489472014-04-28 12:54:51 +0200678 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800679
680 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
681 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
682 CCSR_SSI_STCR_TEFS;
683 stcr = read_ssi(&ssi->stcr) & ~mask;
684 srcr = read_ssi(&ssi->srcr) & ~mask;
685
Markus Pargmann07a28db2014-03-15 13:44:10 +0100686 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800687 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
688 case SND_SOC_DAIFMT_I2S:
689 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
690 case SND_SOC_DAIFMT_CBS_CFS:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100691 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800692 break;
693 case SND_SOC_DAIFMT_CBM_CFM:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100694 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800695 break;
696 default:
697 return -EINVAL;
698 }
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800699
700 /* Data on rising edge of bclk, frame low, 1clk before data */
701 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
702 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
703 break;
704 case SND_SOC_DAIFMT_LEFT_J:
705 /* Data on rising edge of bclk, frame high */
706 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
707 break;
708 case SND_SOC_DAIFMT_DSP_A:
709 /* Data on rising edge of bclk, frame high, 1clk before data */
710 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
711 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
712 break;
713 case SND_SOC_DAIFMT_DSP_B:
714 /* Data on rising edge of bclk, frame high */
715 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
716 CCSR_SSI_STCR_TXBIT0;
717 break;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100718 case SND_SOC_DAIFMT_AC97:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100719 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100720 break;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800721 default:
722 return -EINVAL;
723 }
Markus Pargmann2b0db992014-03-15 13:44:09 +0100724 scr |= ssi_private->i2s_mode;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800725
726 /* DAI clock inversion */
727 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
728 case SND_SOC_DAIFMT_NB_NF:
729 /* Nothing to do for both normal cases */
730 break;
731 case SND_SOC_DAIFMT_IB_NF:
732 /* Invert bit clock */
733 strcr ^= CCSR_SSI_STCR_TSCKP;
734 break;
735 case SND_SOC_DAIFMT_NB_IF:
736 /* Invert frame clock */
737 strcr ^= CCSR_SSI_STCR_TFSI;
738 break;
739 case SND_SOC_DAIFMT_IB_IF:
740 /* Invert both clocks */
741 strcr ^= CCSR_SSI_STCR_TSCKP;
742 strcr ^= CCSR_SSI_STCR_TFSI;
743 break;
744 default:
745 return -EINVAL;
746 }
747
748 /* DAI clock master masks */
749 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
750 case SND_SOC_DAIFMT_CBS_CFS:
751 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
752 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
753 break;
754 case SND_SOC_DAIFMT_CBM_CFM:
755 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
756 break;
757 default:
758 return -EINVAL;
759 }
760
761 stcr |= strcr;
762 srcr |= strcr;
763
764 if (ssi_private->cpu_dai_drv.symmetric_rates) {
765 /* Need to clear RXDIR when using SYNC mode */
766 srcr &= ~CCSR_SSI_SRCR_RXDIR;
767 scr |= CCSR_SSI_SCR_SYN;
768 }
769
770 write_ssi(stcr, &ssi->stcr);
771 write_ssi(srcr, &ssi->srcr);
772 write_ssi(scr, &ssi->scr);
773
Markus Pargmann2b0db992014-03-15 13:44:09 +0100774 /*
775 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
776 * use FIFO 1. We program the transmit water to signal a DMA transfer
777 * if there are only two (or fewer) elements left in the FIFO. Two
778 * elements equals one frame (left channel, right channel). This value,
779 * however, depends on the depth of the transmit buffer.
780 *
781 * We set the watermark on the same level as the DMA burstsize. For
782 * fiq it is probably better to use the biggest possible watermark
783 * size.
784 */
785 if (ssi_private->use_dma)
786 wm = ssi_private->fifo_depth - 2;
787 else
788 wm = ssi_private->fifo_depth;
789
790 write_ssi(CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
791 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm),
792 &ssi->sfcsr);
793
794 if (ssi_private->use_dual_fifo) {
795 write_ssi_mask(&ssi->srcr, CCSR_SSI_SRCR_RFEN1,
796 CCSR_SSI_SRCR_RFEN1);
797 write_ssi_mask(&ssi->stcr, CCSR_SSI_STCR_TFEN1,
798 CCSR_SSI_STCR_TFEN1);
799 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TCH_EN,
800 CCSR_SSI_SCR_TCH_EN);
801 }
802
803 if (fmt & SND_SOC_DAIFMT_AC97)
804 fsl_ssi_setup_ac97(ssi_private);
805
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800806 return 0;
807}
808
809/**
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800810 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
811 *
812 * Note: This function can be only called when using SSI as DAI master
813 */
814static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
815 u32 rx_mask, int slots, int slot_width)
816{
817 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
818 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
819 u32 val;
820
821 /* The slot number should be >= 2 if using Network mode or I2S mode */
822 val = read_ssi(&ssi->scr) & (CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET);
823 if (val && slots < 2) {
824 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
825 return -EINVAL;
826 }
827
828 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_DC_MASK,
829 CCSR_SSI_SxCCR_DC(slots));
830 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_DC_MASK,
831 CCSR_SSI_SxCCR_DC(slots));
832
833 /* The register SxMSKs needs SSI to provide essential clock due to
834 * hardware design. So we here temporarily enable SSI to set them.
835 */
836 val = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
837 write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN);
838
839 write_ssi(tx_mask, &ssi->stmsk);
840 write_ssi(rx_mask, &ssi->srmsk);
841
842 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, val);
843
844 return 0;
845}
846
847/**
Timur Tabi17467f22008-01-11 18:15:26 +0100848 * fsl_ssi_trigger: start and stop the DMA transfer.
849 *
850 * This function is called by ALSA to start, stop, pause, and resume the DMA
851 * transfer of data.
852 *
853 * The DMA channel is in external master start and pause mode, which
854 * means the SSI completely controls the flow of data.
855 */
Mark Browndee89c42008-11-18 22:11:38 +0000856static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
857 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100858{
859 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000860 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100861 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800862 unsigned long flags;
Michael Grzeschik9b443e32013-08-19 17:06:00 +0200863
Timur Tabi17467f22008-01-11 18:15:26 +0100864 switch (cmd) {
865 case SNDRV_PCM_TRIGGER_START:
Fabio Estevamb20e53a2014-05-23 02:38:56 -0300866 case SNDRV_PCM_TRIGGER_RESUME:
Timur Tabi17467f22008-01-11 18:15:26 +0100867 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Timur Tabia4d11fe2009-03-25 18:20:37 -0500868 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +0100869 fsl_ssi_tx_config(ssi_private, true);
Timur Tabia4d11fe2009-03-25 18:20:37 -0500870 else
Markus Pargmann6de83872013-12-20 14:11:34 +0100871 fsl_ssi_rx_config(ssi_private, true);
Timur Tabi17467f22008-01-11 18:15:26 +0100872 break;
873
874 case SNDRV_PCM_TRIGGER_STOP:
Fabio Estevamb20e53a2014-05-23 02:38:56 -0300875 case SNDRV_PCM_TRIGGER_SUSPEND:
Timur Tabi17467f22008-01-11 18:15:26 +0100876 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
877 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +0100878 fsl_ssi_tx_config(ssi_private, false);
Timur Tabi17467f22008-01-11 18:15:26 +0100879 else
Markus Pargmann6de83872013-12-20 14:11:34 +0100880 fsl_ssi_rx_config(ssi_private, false);
Nicolin Chenb2c119b2013-07-10 18:43:54 +0800881
Markus Pargmann171d6832014-04-28 12:54:48 +0200882 if (!fsl_ssi_is_ac97(ssi_private) && (read_ssi(&ssi->scr) &
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800883 (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0) {
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800884 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
885 ssi_private->baudclk_locked = false;
886 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
887 }
Timur Tabi17467f22008-01-11 18:15:26 +0100888 break;
889
890 default:
891 return -EINVAL;
892 }
893
Markus Pargmann171d6832014-04-28 12:54:48 +0200894 if (fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmanna5a7ee72013-12-20 14:11:35 +0100895 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
896 write_ssi(CCSR_SSI_SOR_TX_CLR, &ssi->sor);
897 else
898 write_ssi(CCSR_SSI_SOR_RX_CLR, &ssi->sor);
899 }
Michael Grzeschik9b443e32013-08-19 17:06:00 +0200900
Timur Tabi17467f22008-01-11 18:15:26 +0100901 return 0;
902}
903
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200904static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
905{
906 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
907
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200908 if (ssi_private->soc->imx && ssi_private->use_dma) {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200909 dai->playback_dma_data = &ssi_private->dma_params_tx;
910 dai->capture_dma_data = &ssi_private->dma_params_rx;
911 }
912
913 return 0;
914}
915
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100916static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800917 .startup = fsl_ssi_startup,
918 .hw_params = fsl_ssi_hw_params,
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800919 .set_fmt = fsl_ssi_set_dai_fmt,
920 .set_sysclk = fsl_ssi_set_dai_sysclk,
921 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
Eric Miao6335d052009-03-03 09:41:00 +0800922 .trigger = fsl_ssi_trigger,
Eric Miao6335d052009-03-03 09:41:00 +0800923};
924
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000925/* Template for the CPU dai driver structure */
926static struct snd_soc_dai_driver fsl_ssi_dai_template = {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200927 .probe = fsl_ssi_dai_probe,
Timur Tabi17467f22008-01-11 18:15:26 +0100928 .playback = {
Nicolin Chen2924a992013-12-02 23:29:03 +0800929 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +0100930 .channels_max = 2,
931 .rates = FSLSSI_I2S_RATES,
932 .formats = FSLSSI_I2S_FORMATS,
933 },
934 .capture = {
Nicolin Chen2924a992013-12-02 23:29:03 +0800935 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +0100936 .channels_max = 2,
937 .rates = FSLSSI_I2S_RATES,
938 .formats = FSLSSI_I2S_FORMATS,
939 },
Eric Miao6335d052009-03-03 09:41:00 +0800940 .ops = &fsl_ssi_dai_ops,
Timur Tabi17467f22008-01-11 18:15:26 +0100941};
942
Kuninori Morimoto3580aa12013-03-21 03:32:04 -0700943static const struct snd_soc_component_driver fsl_ssi_component = {
944 .name = "fsl-ssi",
945};
946
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200947static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
948 .ac97_control = 1,
949 .playback = {
950 .stream_name = "AC97 Playback",
951 .channels_min = 2,
952 .channels_max = 2,
953 .rates = SNDRV_PCM_RATE_8000_48000,
954 .formats = SNDRV_PCM_FMTBIT_S16_LE,
955 },
956 .capture = {
957 .stream_name = "AC97 Capture",
958 .channels_min = 2,
959 .channels_max = 2,
960 .rates = SNDRV_PCM_RATE_48000,
961 .formats = SNDRV_PCM_FMTBIT_S16_LE,
962 },
Markus Pargmanna5a7ee72013-12-20 14:11:35 +0100963 .ops = &fsl_ssi_dai_ops,
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200964};
965
966
967static struct fsl_ssi_private *fsl_ac97_data;
968
Sachin Kamata851a2b2013-09-13 15:22:17 +0530969static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200970 unsigned short val)
971{
972 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
973 unsigned int lreg;
974 unsigned int lval;
975
976 if (reg > 0x7f)
977 return;
978
979
980 lreg = reg << 12;
981 write_ssi(lreg, &ssi->sacadd);
982
983 lval = val << 4;
984 write_ssi(lval , &ssi->sacdat);
985
986 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
987 CCSR_SSI_SACNT_WR);
988 udelay(100);
989}
990
Sachin Kamata851a2b2013-09-13 15:22:17 +0530991static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200992 unsigned short reg)
993{
994 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
995
996 unsigned short val = -1;
997 unsigned int lreg;
998
999 lreg = (reg & 0x7f) << 12;
1000 write_ssi(lreg, &ssi->sacadd);
1001 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1002 CCSR_SSI_SACNT_RD);
1003
1004 udelay(100);
1005
1006 val = (read_ssi(&ssi->sacdat) >> 4) & 0xffff;
1007
1008 return val;
1009}
1010
1011static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1012 .read = fsl_ssi_ac97_read,
1013 .write = fsl_ssi_ac97_write,
1014};
1015
Timur Tabi17467f22008-01-11 18:15:26 +01001016/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017 * Make every character in a string lower-case
Timur Tabi17467f22008-01-11 18:15:26 +01001018 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019static void make_lowercase(char *s)
Timur Tabi17467f22008-01-11 18:15:26 +01001020{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001021 char *p = s;
1022 char c;
1023
1024 while ((c = *p)) {
1025 if ((c >= 'A') && (c <= 'Z'))
1026 *p = c + ('a' - 'A');
1027 p++;
1028 }
1029}
1030
Markus Pargmann49da09e2014-04-28 12:54:45 +02001031static int fsl_ssi_imx_probe(struct platform_device *pdev,
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001032 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001033{
1034 struct device_node *np = pdev->dev.of_node;
Markus Pargmanned0f16042014-04-28 12:54:46 +02001035 u32 dmas[4];
Markus Pargmann49da09e2014-04-28 12:54:45 +02001036 int ret;
1037
1038 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1039 if (IS_ERR(ssi_private->clk)) {
1040 ret = PTR_ERR(ssi_private->clk);
1041 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1042 return ret;
1043 }
1044
1045 ret = clk_prepare_enable(ssi_private->clk);
1046 if (ret) {
1047 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1048 return ret;
1049 }
1050
1051 /* For those SLAVE implementations, we ingore non-baudclk cases
1052 * and, instead, abandon MASTER mode that needs baud clock.
1053 */
1054 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1055 if (IS_ERR(ssi_private->baudclk))
1056 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1057 PTR_ERR(ssi_private->baudclk));
1058 else
1059 clk_prepare_enable(ssi_private->baudclk);
1060
1061 /*
1062 * We have burstsize be "fifo_depth - 2" to match the SSI
1063 * watermark setting in fsl_ssi_startup().
1064 */
1065 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1066 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1067 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys +
1068 offsetof(struct ccsr_ssi, stx0);
1069 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys +
1070 offsetof(struct ccsr_ssi, srx0);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001071
Markus Pargmanned0f16042014-04-28 12:54:46 +02001072 ret = !of_property_read_u32_array(np, "dmas", dmas, 4);
1073 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
Markus Pargmann49da09e2014-04-28 12:54:45 +02001074 ssi_private->use_dual_fifo = true;
1075 /* When using dual fifo mode, we need to keep watermark
1076 * as even numbers due to dma script limitation.
1077 */
1078 ssi_private->dma_params_tx.maxburst &= ~0x1;
1079 ssi_private->dma_params_rx.maxburst &= ~0x1;
1080 }
1081
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001082 if (!ssi_private->use_dma) {
1083
1084 /*
1085 * Some boards use an incompatible codec. To get it
1086 * working, we are using imx-fiq-pcm-audio, that
1087 * can handle those codecs. DMA is not possible in this
1088 * situation.
1089 */
1090
1091 ssi_private->fiq_params.irq = ssi_private->irq;
1092 ssi_private->fiq_params.base = iomem;
1093 ssi_private->fiq_params.dma_params_rx =
1094 &ssi_private->dma_params_rx;
1095 ssi_private->fiq_params.dma_params_tx =
1096 &ssi_private->dma_params_tx;
1097
1098 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1099 if (ret)
1100 goto error_pcm;
1101 } else {
1102 ret = imx_pcm_dma_init(pdev);
1103 if (ret)
1104 goto error_pcm;
1105 }
1106
Markus Pargmann49da09e2014-04-28 12:54:45 +02001107 return 0;
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001108
1109error_pcm:
1110 if (!IS_ERR(ssi_private->baudclk))
1111 clk_disable_unprepare(ssi_private->baudclk);
1112
1113 clk_disable_unprepare(ssi_private->clk);
1114
1115 return ret;
Markus Pargmann49da09e2014-04-28 12:54:45 +02001116}
1117
1118static void fsl_ssi_imx_clean(struct platform_device *pdev,
1119 struct fsl_ssi_private *ssi_private)
1120{
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001121 if (!ssi_private->use_dma)
1122 imx_pcm_fiq_exit(pdev);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001123 if (!IS_ERR(ssi_private->baudclk))
1124 clk_disable_unprepare(ssi_private->baudclk);
1125 clk_disable_unprepare(ssi_private->clk);
1126}
1127
Bill Pembertona0a3d512012-12-07 09:26:16 -05001128static int fsl_ssi_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001129{
Timur Tabi17467f22008-01-11 18:15:26 +01001130 struct fsl_ssi_private *ssi_private;
1131 int ret = 0;
Timur Tabi38fec722010-08-19 15:26:58 -05001132 struct device_node *np = pdev->dev.of_node;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001133 const struct of_device_id *of_id;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134 const char *p, *sprop;
Timur Tabi8e9d8692010-08-06 12:16:12 -05001135 const uint32_t *iprop;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 struct resource res;
1137 char name[64];
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001138 bool ac97 = false;
Timur Tabi17467f22008-01-11 18:15:26 +01001139
Timur Tabiff713342010-08-04 17:51:08 -05001140 /* SSIs that are not connected on the board should have a
1141 * status = "disabled"
1142 * property in their device tree nodes.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001143 */
Timur Tabiff713342010-08-04 17:51:08 -05001144 if (!of_device_is_available(np))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001145 return -ENODEV;
1146
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001147 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001148 if (!of_id || !of_id->data)
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001149 return -EINVAL;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001150
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001151 sprop = of_get_property(np, "fsl,mode", NULL);
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001152 if (!sprop) {
1153 dev_err(&pdev->dev, "fsl,mode property is necessary\n");
1154 return -EINVAL;
1155 }
Fabio Estevamae1f8ce2014-01-20 17:35:40 -02001156 if (!strcmp(sprop, "ac97-slave"))
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001157 ac97 = true;
Timur Tabi17467f22008-01-11 18:15:26 +01001158
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001159 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1160 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001161 if (!ssi_private) {
Timur Tabi38fec722010-08-19 15:26:58 -05001162 dev_err(&pdev->dev, "could not allocate DAI object\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001163 return -ENOMEM;
1164 }
Timur Tabi17467f22008-01-11 18:15:26 +01001165
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001166 ssi_private->soc = of_id->data;
1167
Markus Pargmannde623ec2013-07-27 13:31:53 +02001168 ssi_private->use_dma = !of_property_read_bool(np,
1169 "fsl,fiq-stream-filter");
1170
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001171 if (ac97) {
1172 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1173 sizeof(fsl_ssi_ac97_dai));
1174
1175 fsl_ac97_data = ssi_private;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001176
1177 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1178 } else {
1179 /* Initialize this copy of the CPU DAI driver structure */
1180 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1181 sizeof(fsl_ssi_dai_template));
1182 }
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001183 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001184
1185 /* Get the addresses and IRQ */
1186 ret = of_address_to_resource(np, 0, &res);
1187 if (ret) {
Timur Tabi38fec722010-08-19 15:26:58 -05001188 dev_err(&pdev->dev, "could not determine device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001189 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001190 }
Timur Tabi147dfe92011-06-08 15:02:55 -05001191 ssi_private->ssi = of_iomap(np, 0);
1192 if (!ssi_private->ssi) {
1193 dev_err(&pdev->dev, "could not map device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001194 return -ENOMEM;
Timur Tabi147dfe92011-06-08 15:02:55 -05001195 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001196 ssi_private->ssi_phys = res.start;
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001197
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001198 ssi_private->irq = irq_of_parse_and_map(np, 0);
Chen Gangd60336e2013-09-23 11:36:21 +08001199 if (!ssi_private->irq) {
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001200 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
Fabio Estevamb0a47472013-07-17 02:00:38 -03001201 return -ENXIO;
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001202 }
1203
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001204 /* Are the RX and the TX clocks locked? */
Nicolin Chen07a94832013-12-03 18:38:07 +08001205 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001206 ssi_private->cpu_dai_drv.symmetric_rates = 1;
Nicolin Chen07a94832013-12-03 18:38:07 +08001207 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1208 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1209 }
Timur Tabi17467f22008-01-11 18:15:26 +01001210
Timur Tabi8e9d8692010-08-06 12:16:12 -05001211 /* Determine the FIFO depth. */
1212 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1213 if (iprop)
Timur Tabi147dfe92011-06-08 15:02:55 -05001214 ssi_private->fifo_depth = be32_to_cpup(iprop);
Timur Tabi8e9d8692010-08-06 12:16:12 -05001215 else
1216 /* Older 8610 DTs didn't have the fifo-depth property */
1217 ssi_private->fifo_depth = 8;
1218
Nicolin Chenaafa85e2013-12-12 18:44:45 +08001219 ssi_private->baudclk_locked = false;
1220 spin_lock_init(&ssi_private->baudclk_lock);
1221
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001222 dev_set_drvdata(&pdev->dev, ssi_private);
1223
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001224 if (ssi_private->soc->imx) {
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001225 ret = fsl_ssi_imx_probe(pdev, ssi_private, ssi_private->ssi);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001226 if (ret)
Fabio Estevamb0a47472013-07-17 02:00:38 -03001227 goto error_irqmap;
Markus Pargmann0888efd2013-12-20 14:11:31 +01001228 }
1229
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001230 ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1231 &ssi_private->cpu_dai_drv, 1);
1232 if (ret) {
1233 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1234 goto error_asoc_register;
1235 }
1236
Markus Pargmann0888efd2013-12-20 14:11:31 +01001237 if (ssi_private->use_dma) {
Michael Grzeschikf0377082013-08-19 17:06:01 +02001238 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
Markus Pargmann171d6832014-04-28 12:54:48 +02001239 fsl_ssi_isr, 0, dev_name(&pdev->dev),
Michael Grzeschikf0377082013-08-19 17:06:01 +02001240 ssi_private);
1241 if (ret < 0) {
1242 dev_err(&pdev->dev, "could not claim irq %u\n",
1243 ssi_private->irq);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001244 goto error_irq;
Michael Grzeschikf0377082013-08-19 17:06:01 +02001245 }
Shawn Guo09ce1112012-03-16 16:56:43 +08001246 }
1247
Markus Pargmannf138e622014-04-28 12:54:43 +02001248 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001249 if (ret)
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001250 goto error_asoc_register;
Shawn Guo09ce1112012-03-16 16:56:43 +08001251
1252 /*
1253 * If codec-handle property is missing from SSI node, we assume
1254 * that the machine driver uses new binding which does not require
1255 * SSI driver to trigger machine driver's probe.
1256 */
Markus Pargmann171d6832014-04-28 12:54:48 +02001257 if (!of_get_property(np, "codec-handle", NULL))
Shawn Guo09ce1112012-03-16 16:56:43 +08001258 goto done;
Shawn Guo09ce1112012-03-16 16:56:43 +08001259
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001260 /* Trigger the machine driver's probe function. The platform driver
Shawn Guo2b81ec62012-03-09 00:59:46 +08001261 * name of the machine driver is taken from /compatible property of the
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001262 * device tree. We also pass the address of the CPU DAI driver
1263 * structure.
1264 */
Shawn Guo2b81ec62012-03-09 00:59:46 +08001265 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1266 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001267 p = strrchr(sprop, ',');
1268 if (p)
1269 sprop = p + 1;
1270 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1271 make_lowercase(name);
1272
1273 ssi_private->pdev =
Timur Tabi38fec722010-08-19 15:26:58 -05001274 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001275 if (IS_ERR(ssi_private->pdev)) {
1276 ret = PTR_ERR(ssi_private->pdev);
Timur Tabi38fec722010-08-19 15:26:58 -05001277 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001278 goto error_sound_card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001279 }
1280
Shawn Guo09ce1112012-03-16 16:56:43 +08001281done:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001282 return 0;
Timur Tabi87a06322010-08-03 17:55:28 -05001283
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001284error_sound_card:
Markus Pargmannf138e622014-04-28 12:54:43 +02001285 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001286
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001287error_irq:
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001288 snd_soc_unregister_component(&pdev->dev);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001289
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001290error_asoc_register:
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001291 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001292 fsl_ssi_imx_clean(pdev, ssi_private);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001293
1294error_irqmap:
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001295 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001296 irq_dispose_mapping(ssi_private->irq);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001297
Timur Tabi87a06322010-08-03 17:55:28 -05001298 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +01001299}
Timur Tabi17467f22008-01-11 18:15:26 +01001300
Timur Tabi38fec722010-08-19 15:26:58 -05001301static int fsl_ssi_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +01001302{
Timur Tabi38fec722010-08-19 15:26:58 -05001303 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
Timur Tabi17467f22008-01-11 18:15:26 +01001304
Markus Pargmannf138e622014-04-28 12:54:43 +02001305 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001306
Markus Pargmann171d6832014-04-28 12:54:48 +02001307 if (ssi_private->pdev)
Shawn Guo09ce1112012-03-16 16:56:43 +08001308 platform_device_unregister(ssi_private->pdev);
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001309 snd_soc_unregister_component(&pdev->dev);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001310
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001311 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001312 fsl_ssi_imx_clean(pdev, ssi_private);
1313
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001314 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001315 irq_dispose_mapping(ssi_private->irq);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001316
1317 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +01001318}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001319
Grant Likelyf07eb222011-02-22 21:05:04 -07001320static struct platform_driver fsl_ssi_driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001321 .driver = {
1322 .name = "fsl-ssi-dai",
1323 .owner = THIS_MODULE,
1324 .of_match_table = fsl_ssi_ids,
1325 },
1326 .probe = fsl_ssi_probe,
1327 .remove = fsl_ssi_remove,
1328};
Timur Tabi17467f22008-01-11 18:15:26 +01001329
Axel Linba0a7e02011-11-25 10:10:55 +08001330module_platform_driver(fsl_ssi_driver);
Timur Tabia454dad2009-03-05 17:23:37 -06001331
Fabio Estevamf3142802013-07-20 16:16:01 -03001332MODULE_ALIAS("platform:fsl-ssi-dai");
Timur Tabi17467f22008-01-11 18:15:26 +01001333MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1334MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001335MODULE_LICENSE("GPL v2");