blob: b3033d9108b215f18e3f3bcf9e7f9dbf51e059af [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 struct clk *baudclk;
Shawn Guo95cd98f2012-03-29 10:53:41 +0800167 struct clk *clk;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200168 unsigned int bitclk_freq;
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
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200239static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
240{
241 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
242 SND_SOC_DAIFMT_CBS_CFS;
243}
244
Timur Tabi17467f22008-01-11 18:15:26 +0100245/**
246 * fsl_ssi_isr: SSI interrupt handler
247 *
248 * Although it's possible to use the interrupt handler to send and receive
249 * data to/from the SSI, we use the DMA instead. Programming is more
250 * complicated, but the performance is much better.
251 *
252 * This interrupt handler is used only to gather statistics.
253 *
254 * @irq: IRQ of the SSI device
255 * @dev_id: pointer to the ssi_private structure for this SSI device
256 */
257static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
258{
259 struct fsl_ssi_private *ssi_private = dev_id;
260 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
Timur Tabi17467f22008-01-11 18:15:26 +0100261 __be32 sisr;
Markus Pargmann0888efd2013-12-20 14:11:31 +0100262 __be32 sisr2;
Timur Tabi17467f22008-01-11 18:15:26 +0100263
264 /* We got an interrupt, so read the status register to see what we
265 were interrupted for. We mask it with the Interrupt Enable register
266 so that we only check for events that we're interested in.
267 */
Markus Pargmannf138e622014-04-28 12:54:43 +0200268 sisr = read_ssi(&ssi->sisr);
Timur Tabi17467f22008-01-11 18:15:26 +0100269
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200270 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
Timur Tabi17467f22008-01-11 18:15:26 +0100271 /* Clear the bits that we set */
272 if (sisr2)
Shawn Guodfa1a102012-03-16 16:56:42 +0800273 write_ssi(sisr2, &ssi->sisr);
Timur Tabi17467f22008-01-11 18:15:26 +0100274
Markus Pargmannf138e622014-04-28 12:54:43 +0200275 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
276
277 return IRQ_HANDLED;
Timur Tabi17467f22008-01-11 18:15:26 +0100278}
279
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100280/*
281 * Enable/Disable all rx/tx config flags at once.
282 */
283static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
284 bool enable)
285{
286 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
287 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
288
289 if (enable) {
290 write_ssi_mask(&ssi->sier, 0, vals->rx.sier | vals->tx.sier);
291 write_ssi_mask(&ssi->srcr, 0, vals->rx.srcr | vals->tx.srcr);
292 write_ssi_mask(&ssi->stcr, 0, vals->rx.stcr | vals->tx.stcr);
293 } else {
294 write_ssi_mask(&ssi->srcr, vals->rx.srcr | vals->tx.srcr, 0);
295 write_ssi_mask(&ssi->stcr, vals->rx.stcr | vals->tx.stcr, 0);
296 write_ssi_mask(&ssi->sier, vals->rx.sier | vals->tx.sier, 0);
297 }
298}
299
300/*
Markus Pargmann65c961c2014-04-28 12:54:42 +0200301 * Calculate the bits that have to be disabled for the current stream that is
302 * getting disabled. This keeps the bits enabled that are necessary for the
303 * second stream to work if 'stream_active' is true.
304 *
305 * Detailed calculation:
306 * These are the values that need to be active after disabling. For non-active
307 * second stream, this is 0:
308 * vals_stream * !!stream_active
309 *
310 * The following computes the overall differences between the setup for the
311 * to-disable stream and the active stream, a simple XOR:
312 * vals_disable ^ (vals_stream * !!(stream_active))
313 *
314 * The full expression adds a mask on all values we care about
315 */
316#define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
317 ((vals_disable) & \
318 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
319
320/*
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100321 * Enable/Disable a ssi configuration. You have to pass either
322 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
323 */
324static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
325 struct fsl_ssi_reg_val *vals)
326{
327 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
328 struct fsl_ssi_reg_val *avals;
329 u32 scr_val = read_ssi(&ssi->scr);
330 int nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
331 !!(scr_val & CCSR_SSI_SCR_RE);
Markus Pargmann65c961c2014-04-28 12:54:42 +0200332 int keep_active;
333
334 if (nr_active_streams - 1 > 0)
335 keep_active = 1;
336 else
337 keep_active = 0;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100338
339 /* Find the other direction values rx or tx which we do not want to
340 * modify */
341 if (&ssi_private->rxtx_reg_val.rx == vals)
342 avals = &ssi_private->rxtx_reg_val.tx;
343 else
344 avals = &ssi_private->rxtx_reg_val.rx;
345
346 /* If vals should be disabled, start with disabling the unit */
347 if (!enable) {
Markus Pargmann65c961c2014-04-28 12:54:42 +0200348 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
349 keep_active);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100350 write_ssi_mask(&ssi->scr, scr, 0);
351 }
352
353 /*
354 * We are running on a SoC which does not support online SSI
355 * reconfiguration, so we have to enable all necessary flags at once
356 * even if we do not use them later (capture and playback configuration)
357 */
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200358 if (ssi_private->soc->offline_config) {
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100359 if ((enable && !nr_active_streams) ||
Markus Pargmann65c961c2014-04-28 12:54:42 +0200360 (!enable && !keep_active))
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100361 fsl_ssi_rxtx_config(ssi_private, enable);
362
363 goto config_done;
364 }
365
366 /*
367 * Configure single direction units while the SSI unit is running
368 * (online configuration)
369 */
370 if (enable) {
371 write_ssi_mask(&ssi->sier, 0, vals->sier);
372 write_ssi_mask(&ssi->srcr, 0, vals->srcr);
373 write_ssi_mask(&ssi->stcr, 0, vals->stcr);
374 } else {
375 u32 sier;
376 u32 srcr;
377 u32 stcr;
378
379 /*
380 * Disabling the necessary flags for one of rx/tx while the
381 * other stream is active is a little bit more difficult. We
382 * have to disable only those flags that differ between both
383 * streams (rx XOR tx) and that are set in the stream that is
384 * disabled now. Otherwise we could alter flags of the other
385 * stream
386 */
387
388 /* These assignments are simply vals without bits set in avals*/
Markus Pargmann65c961c2014-04-28 12:54:42 +0200389 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
390 keep_active);
391 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
392 keep_active);
393 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
394 keep_active);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100395
396 write_ssi_mask(&ssi->srcr, srcr, 0);
397 write_ssi_mask(&ssi->stcr, stcr, 0);
398 write_ssi_mask(&ssi->sier, sier, 0);
399 }
400
401config_done:
402 /* Enabling of subunits is done after configuration */
403 if (enable)
404 write_ssi_mask(&ssi->scr, 0, vals->scr);
405}
406
407
408static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
409{
410 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
411}
412
413static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
414{
415 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
416}
417
Markus Pargmann6de83872013-12-20 14:11:34 +0100418/*
419 * Setup rx/tx register values used to enable/disable the streams. These will
420 * be used later in fsl_ssi_config to setup the streams without the need to
421 * check for all different SSI modes.
422 */
423static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
424{
425 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
426
427 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
428 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
429 reg->rx.scr = 0;
430 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
431 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
432 reg->tx.scr = 0;
433
Markus Pargmann171d6832014-04-28 12:54:48 +0200434 if (!fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmann6de83872013-12-20 14:11:34 +0100435 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
436 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
437 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
438 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
439 }
440
441 if (ssi_private->use_dma) {
442 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
443 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
444 } else {
445 reg->rx.sier |= CCSR_SSI_SIER_RIE;
446 reg->tx.sier |= CCSR_SSI_SIER_TIE;
447 }
448
449 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
450 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
451}
452
Markus Pargmannd8764642013-11-20 10:04:15 +0100453static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
454{
455 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
456
457 /*
458 * Setup the clock control register
459 */
460 write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
461 &ssi->stccr);
462 write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
463 &ssi->srccr);
464
465 /*
466 * Enable AC97 mode and startup the SSI
467 */
468 write_ssi(CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV,
469 &ssi->sacnt);
470 write_ssi(0xff, &ssi->saccdis);
471 write_ssi(0x300, &ssi->saccen);
472
473 /*
474 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
475 * codec before a stream is started.
476 */
477 write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN |
478 CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
479
480 write_ssi(CCSR_SSI_SOR_WAIT(3), &ssi->sor);
481}
482
Timur Tabi17467f22008-01-11 18:15:26 +0100483/**
484 * fsl_ssi_startup: create a new substream
485 *
486 * This is the first function called when a stream is opened.
487 *
488 * If this is the first stream open, then grab the IRQ and program most of
489 * the SSI registers.
490 */
Mark Browndee89c42008-11-18 22:11:38 +0000491static int fsl_ssi_startup(struct snd_pcm_substream *substream,
492 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100493{
494 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Timur Tabi5e538ec2011-09-13 12:59:37 -0500495 struct fsl_ssi_private *ssi_private =
496 snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100497
Sascha Hauerd8ced472014-05-27 10:24:21 +0200498 if (!dai->active && !fsl_ssi_is_ac97(ssi_private))
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800499 ssi_private->baudclk_locked = false;
Timur Tabibe41e942008-07-28 17:04:39 -0500500
Nicolin Chen0da9e552013-11-13 22:55:26 +0800501 /* When using dual fifo mode, it is safer to ensure an even period
502 * size. If appearing to an odd number while DMA always starts its
503 * task from fifo0, fifo1 would be neglected at the end of each
504 * period. But SSI would still access fifo1 with an invalid data.
505 */
506 if (ssi_private->use_dual_fifo)
507 snd_pcm_hw_constraint_step(substream->runtime, 0,
508 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
509
Timur Tabi17467f22008-01-11 18:15:26 +0100510 return 0;
511}
512
513/**
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200514 * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
Sascha Haueree9daad2014-04-28 12:54:52 +0200515 *
516 * Note: This function can be only called when using SSI as DAI master
517 *
518 * Quick instruction for parameters:
519 * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
520 * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
521 */
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200522static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
523 struct snd_soc_dai *cpu_dai,
524 struct snd_pcm_hw_params *hw_params)
Sascha Haueree9daad2014-04-28 12:54:52 +0200525{
526 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
527 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
528 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
529 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
Sascha Hauerd8ced472014-05-27 10:24:21 +0200530 unsigned long clkrate, baudrate, tmprate;
Sascha Haueree9daad2014-04-28 12:54:52 +0200531 u64 sub, savesub = 100000;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200532 unsigned int freq;
533
534 /* Prefer the explicitly set bitclock frequency */
535 if (ssi_private->bitclk_freq)
536 freq = ssi_private->bitclk_freq;
537 else
538 freq = params_channels(hw_params) * 32 * params_rate(hw_params);
Sascha Haueree9daad2014-04-28 12:54:52 +0200539
540 /* Don't apply it to any non-baudclk circumstance */
541 if (IS_ERR(ssi_private->baudclk))
542 return -EINVAL;
543
544 /* It should be already enough to divide clock by setting pm alone */
545 psr = 0;
546 div2 = 0;
547
548 factor = (div2 + 1) * (7 * psr + 1) * 2;
549
550 for (i = 0; i < 255; i++) {
551 /* The bclk rate must be smaller than 1/5 sysclk rate */
552 if (factor * (i + 1) < 5)
553 continue;
554
555 tmprate = freq * factor * (i + 2);
556 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
557
558 do_div(clkrate, factor);
559 afreq = (u32)clkrate / (i + 1);
560
561 if (freq == afreq)
562 sub = 0;
563 else if (freq / afreq == 1)
564 sub = freq - afreq;
565 else if (afreq / freq == 1)
566 sub = afreq - freq;
567 else
568 continue;
569
570 /* Calculate the fraction */
571 sub *= 100000;
572 do_div(sub, freq);
573
574 if (sub < savesub) {
575 baudrate = tmprate;
576 savesub = sub;
577 pm = i;
578 }
579
580 /* We are lucky */
581 if (savesub == 0)
582 break;
583 }
584
585 /* No proper pm found if it is still remaining the initial value */
586 if (pm == 999) {
587 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
588 return -EINVAL;
589 }
590
591 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
592 (psr ? CCSR_SSI_SxCCR_PSR : 0);
593 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
594 CCSR_SSI_SxCCR_PSR;
595
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
Sascha Haueree9daad2014-04-28 12:54:52 +0200597 write_ssi_mask(&ssi->stccr, mask, stccr);
598 else
599 write_ssi_mask(&ssi->srccr, mask, stccr);
600
Sascha Haueree9daad2014-04-28 12:54:52 +0200601 if (!ssi_private->baudclk_locked) {
602 ret = clk_set_rate(ssi_private->baudclk, baudrate);
603 if (ret) {
Sascha Haueree9daad2014-04-28 12:54:52 +0200604 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
605 return -EINVAL;
606 }
607 ssi_private->baudclk_locked = true;
608 }
Sascha Haueree9daad2014-04-28 12:54:52 +0200609
610 return 0;
611}
612
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200613static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
614 int clk_id, unsigned int freq, int dir)
615{
616 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
617
618 ssi_private->bitclk_freq = freq;
619
620 return 0;
621}
622
Sascha Haueree9daad2014-04-28 12:54:52 +0200623/**
Timur Tabi85ef2372009-02-05 17:56:02 -0600624 * fsl_ssi_hw_params - program the sample size
Timur Tabi17467f22008-01-11 18:15:26 +0100625 *
626 * Most of the SSI registers have been programmed in the startup function,
627 * but the word length must be programmed here. Unfortunately, programming
628 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
629 * cause a problem with supporting simultaneous playback and capture. If
630 * the SSI is already playing a stream, then that stream may be temporarily
631 * stopped when you start capture.
632 *
633 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
634 * clock master.
635 */
Timur Tabi85ef2372009-02-05 17:56:02 -0600636static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
637 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100638{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500640 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
Nicolin Chen2924a992013-12-02 23:29:03 +0800641 unsigned int channels = params_channels(hw_params);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500642 unsigned int sample_size =
643 snd_pcm_format_width(params_format(hw_params));
644 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
Shawn Guodfa1a102012-03-16 16:56:42 +0800645 int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200646 int ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100647
Timur Tabi5e538ec2011-09-13 12:59:37 -0500648 /*
649 * If we're in synchronous mode, and the SSI is already enabled,
650 * then STCCR is already set properly.
651 */
652 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
653 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +0100654
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200655 if (fsl_ssi_is_i2s_master(ssi_private)) {
656 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
657 if (ret)
658 return ret;
659 }
660
Timur Tabi5e538ec2011-09-13 12:59:37 -0500661 /*
662 * FIXME: The documentation says that SxCCR[WL] should not be
663 * modified while the SSI is enabled. The only time this can
664 * happen is if we're trying to do simultaneous playback and
665 * capture in asynchronous mode. Unfortunately, I have been enable
666 * to get that to work at all on the P1022DS. Therefore, we don't
667 * bother to disable/enable the SSI when setting SxCCR[WL], because
668 * the SSI will stop anyway. Maybe one day, this will get fixed.
669 */
Timur Tabi17467f22008-01-11 18:15:26 +0100670
Timur Tabi5e538ec2011-09-13 12:59:37 -0500671 /* In synchronous mode, the SSI uses STCCR for capture */
672 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
673 ssi_private->cpu_dai_drv.symmetric_rates)
Shawn Guodfa1a102012-03-16 16:56:42 +0800674 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500675 else
Shawn Guodfa1a102012-03-16 16:56:42 +0800676 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
Timur Tabi17467f22008-01-11 18:15:26 +0100677
Markus Pargmann171d6832014-04-28 12:54:48 +0200678 if (!fsl_ssi_is_ac97(ssi_private))
Nicolin Chen2924a992013-12-02 23:29:03 +0800679 write_ssi_mask(&ssi->scr,
680 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
681 channels == 1 ? 0 : ssi_private->i2s_mode);
682
Timur Tabi17467f22008-01-11 18:15:26 +0100683 return 0;
684}
685
Markus Pargmann85e59af2014-05-27 10:24:19 +0200686static int _fsl_ssi_set_dai_fmt(struct fsl_ssi_private *ssi_private,
687 unsigned int fmt)
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800688{
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800689 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
690 u32 strcr = 0, stcr, srcr, scr, mask;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100691 u8 wm;
692
Markus Pargmann171d6832014-04-28 12:54:48 +0200693 ssi_private->dai_fmt = fmt;
694
Markus Pargmann2b0db992014-03-15 13:44:09 +0100695 fsl_ssi_setup_reg_vals(ssi_private);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800696
697 scr = read_ssi(&ssi->scr) & ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
Markus Pargmann50489472014-04-28 12:54:51 +0200698 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800699
700 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
701 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
702 CCSR_SSI_STCR_TEFS;
703 stcr = read_ssi(&ssi->stcr) & ~mask;
704 srcr = read_ssi(&ssi->srcr) & ~mask;
705
Markus Pargmann07a28db2014-03-15 13:44:10 +0100706 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800707 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
708 case SND_SOC_DAIFMT_I2S:
709 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
710 case SND_SOC_DAIFMT_CBS_CFS:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100711 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800712 break;
713 case SND_SOC_DAIFMT_CBM_CFM:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100714 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800715 break;
716 default:
717 return -EINVAL;
718 }
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800719
720 /* Data on rising edge of bclk, frame low, 1clk before data */
721 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
722 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
723 break;
724 case SND_SOC_DAIFMT_LEFT_J:
725 /* Data on rising edge of bclk, frame high */
726 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
727 break;
728 case SND_SOC_DAIFMT_DSP_A:
729 /* Data on rising edge of bclk, frame high, 1clk before data */
730 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
731 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
732 break;
733 case SND_SOC_DAIFMT_DSP_B:
734 /* Data on rising edge of bclk, frame high */
735 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
736 CCSR_SSI_STCR_TXBIT0;
737 break;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100738 case SND_SOC_DAIFMT_AC97:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100739 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100740 break;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800741 default:
742 return -EINVAL;
743 }
Markus Pargmann2b0db992014-03-15 13:44:09 +0100744 scr |= ssi_private->i2s_mode;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800745
746 /* DAI clock inversion */
747 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
748 case SND_SOC_DAIFMT_NB_NF:
749 /* Nothing to do for both normal cases */
750 break;
751 case SND_SOC_DAIFMT_IB_NF:
752 /* Invert bit clock */
753 strcr ^= CCSR_SSI_STCR_TSCKP;
754 break;
755 case SND_SOC_DAIFMT_NB_IF:
756 /* Invert frame clock */
757 strcr ^= CCSR_SSI_STCR_TFSI;
758 break;
759 case SND_SOC_DAIFMT_IB_IF:
760 /* Invert both clocks */
761 strcr ^= CCSR_SSI_STCR_TSCKP;
762 strcr ^= CCSR_SSI_STCR_TFSI;
763 break;
764 default:
765 return -EINVAL;
766 }
767
768 /* DAI clock master masks */
769 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
770 case SND_SOC_DAIFMT_CBS_CFS:
771 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
772 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
773 break;
774 case SND_SOC_DAIFMT_CBM_CFM:
775 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
776 break;
777 default:
778 return -EINVAL;
779 }
780
781 stcr |= strcr;
782 srcr |= strcr;
783
784 if (ssi_private->cpu_dai_drv.symmetric_rates) {
785 /* Need to clear RXDIR when using SYNC mode */
786 srcr &= ~CCSR_SSI_SRCR_RXDIR;
787 scr |= CCSR_SSI_SCR_SYN;
788 }
789
790 write_ssi(stcr, &ssi->stcr);
791 write_ssi(srcr, &ssi->srcr);
792 write_ssi(scr, &ssi->scr);
793
Markus Pargmann2b0db992014-03-15 13:44:09 +0100794 /*
795 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
796 * use FIFO 1. We program the transmit water to signal a DMA transfer
797 * if there are only two (or fewer) elements left in the FIFO. Two
798 * elements equals one frame (left channel, right channel). This value,
799 * however, depends on the depth of the transmit buffer.
800 *
801 * We set the watermark on the same level as the DMA burstsize. For
802 * fiq it is probably better to use the biggest possible watermark
803 * size.
804 */
805 if (ssi_private->use_dma)
806 wm = ssi_private->fifo_depth - 2;
807 else
808 wm = ssi_private->fifo_depth;
809
810 write_ssi(CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
811 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm),
812 &ssi->sfcsr);
813
814 if (ssi_private->use_dual_fifo) {
815 write_ssi_mask(&ssi->srcr, CCSR_SSI_SRCR_RFEN1,
816 CCSR_SSI_SRCR_RFEN1);
817 write_ssi_mask(&ssi->stcr, CCSR_SSI_STCR_TFEN1,
818 CCSR_SSI_STCR_TFEN1);
819 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TCH_EN,
820 CCSR_SSI_SCR_TCH_EN);
821 }
822
823 if (fmt & SND_SOC_DAIFMT_AC97)
824 fsl_ssi_setup_ac97(ssi_private);
825
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800826 return 0;
Markus Pargmann85e59af2014-05-27 10:24:19 +0200827
828}
829
830/**
831 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
832 */
833static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
834{
835 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
836
837 return _fsl_ssi_set_dai_fmt(ssi_private, fmt);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800838}
839
840/**
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800841 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
842 *
843 * Note: This function can be only called when using SSI as DAI master
844 */
845static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
846 u32 rx_mask, int slots, int slot_width)
847{
848 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
849 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
850 u32 val;
851
852 /* The slot number should be >= 2 if using Network mode or I2S mode */
853 val = read_ssi(&ssi->scr) & (CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET);
854 if (val && slots < 2) {
855 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
856 return -EINVAL;
857 }
858
859 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_DC_MASK,
860 CCSR_SSI_SxCCR_DC(slots));
861 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_DC_MASK,
862 CCSR_SSI_SxCCR_DC(slots));
863
864 /* The register SxMSKs needs SSI to provide essential clock due to
865 * hardware design. So we here temporarily enable SSI to set them.
866 */
867 val = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
868 write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN);
869
870 write_ssi(tx_mask, &ssi->stmsk);
871 write_ssi(rx_mask, &ssi->srmsk);
872
873 write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, val);
874
875 return 0;
876}
877
878/**
Timur Tabi17467f22008-01-11 18:15:26 +0100879 * fsl_ssi_trigger: start and stop the DMA transfer.
880 *
881 * This function is called by ALSA to start, stop, pause, and resume the DMA
882 * transfer of data.
883 *
884 * The DMA channel is in external master start and pause mode, which
885 * means the SSI completely controls the flow of data.
886 */
Mark Browndee89c42008-11-18 22:11:38 +0000887static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
888 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100889{
890 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Timur Tabi17467f22008-01-11 18:15:26 +0100892 struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
Michael Grzeschik9b443e32013-08-19 17:06:00 +0200893
Timur Tabi17467f22008-01-11 18:15:26 +0100894 switch (cmd) {
895 case SNDRV_PCM_TRIGGER_START:
Fabio Estevamb20e53a2014-05-23 02:38:56 -0300896 case SNDRV_PCM_TRIGGER_RESUME:
Timur Tabi17467f22008-01-11 18:15:26 +0100897 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Timur Tabia4d11fe2009-03-25 18:20:37 -0500898 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +0100899 fsl_ssi_tx_config(ssi_private, true);
Timur Tabia4d11fe2009-03-25 18:20:37 -0500900 else
Markus Pargmann6de83872013-12-20 14:11:34 +0100901 fsl_ssi_rx_config(ssi_private, true);
Timur Tabi17467f22008-01-11 18:15:26 +0100902 break;
903
904 case SNDRV_PCM_TRIGGER_STOP:
Fabio Estevamb20e53a2014-05-23 02:38:56 -0300905 case SNDRV_PCM_TRIGGER_SUSPEND:
Timur Tabi17467f22008-01-11 18:15:26 +0100906 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
907 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +0100908 fsl_ssi_tx_config(ssi_private, false);
Timur Tabi17467f22008-01-11 18:15:26 +0100909 else
Markus Pargmann6de83872013-12-20 14:11:34 +0100910 fsl_ssi_rx_config(ssi_private, false);
Nicolin Chenb2c119b2013-07-10 18:43:54 +0800911
Markus Pargmann171d6832014-04-28 12:54:48 +0200912 if (!fsl_ssi_is_ac97(ssi_private) && (read_ssi(&ssi->scr) &
Sascha Hauerd8ced472014-05-27 10:24:21 +0200913 (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0)
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800914 ssi_private->baudclk_locked = false;
Sascha Hauerd8ced472014-05-27 10:24:21 +0200915
Timur Tabi17467f22008-01-11 18:15:26 +0100916 break;
917
918 default:
919 return -EINVAL;
920 }
921
Markus Pargmann171d6832014-04-28 12:54:48 +0200922 if (fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmanna5a7ee72013-12-20 14:11:35 +0100923 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
924 write_ssi(CCSR_SSI_SOR_TX_CLR, &ssi->sor);
925 else
926 write_ssi(CCSR_SSI_SOR_RX_CLR, &ssi->sor);
927 }
Michael Grzeschik9b443e32013-08-19 17:06:00 +0200928
Timur Tabi17467f22008-01-11 18:15:26 +0100929 return 0;
930}
931
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200932static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
933{
934 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
935
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200936 if (ssi_private->soc->imx && ssi_private->use_dma) {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200937 dai->playback_dma_data = &ssi_private->dma_params_tx;
938 dai->capture_dma_data = &ssi_private->dma_params_rx;
939 }
940
941 return 0;
942}
943
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100944static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800945 .startup = fsl_ssi_startup,
946 .hw_params = fsl_ssi_hw_params,
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800947 .set_fmt = fsl_ssi_set_dai_fmt,
948 .set_sysclk = fsl_ssi_set_dai_sysclk,
949 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
Eric Miao6335d052009-03-03 09:41:00 +0800950 .trigger = fsl_ssi_trigger,
Eric Miao6335d052009-03-03 09:41:00 +0800951};
952
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953/* Template for the CPU dai driver structure */
954static struct snd_soc_dai_driver fsl_ssi_dai_template = {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200955 .probe = fsl_ssi_dai_probe,
Timur Tabi17467f22008-01-11 18:15:26 +0100956 .playback = {
Nicolin Chen2924a992013-12-02 23:29:03 +0800957 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +0100958 .channels_max = 2,
959 .rates = FSLSSI_I2S_RATES,
960 .formats = FSLSSI_I2S_FORMATS,
961 },
962 .capture = {
Nicolin Chen2924a992013-12-02 23:29:03 +0800963 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +0100964 .channels_max = 2,
965 .rates = FSLSSI_I2S_RATES,
966 .formats = FSLSSI_I2S_FORMATS,
967 },
Eric Miao6335d052009-03-03 09:41:00 +0800968 .ops = &fsl_ssi_dai_ops,
Timur Tabi17467f22008-01-11 18:15:26 +0100969};
970
Kuninori Morimoto3580aa12013-03-21 03:32:04 -0700971static const struct snd_soc_component_driver fsl_ssi_component = {
972 .name = "fsl-ssi",
973};
974
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200975static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
976 .ac97_control = 1,
977 .playback = {
978 .stream_name = "AC97 Playback",
979 .channels_min = 2,
980 .channels_max = 2,
981 .rates = SNDRV_PCM_RATE_8000_48000,
982 .formats = SNDRV_PCM_FMTBIT_S16_LE,
983 },
984 .capture = {
985 .stream_name = "AC97 Capture",
986 .channels_min = 2,
987 .channels_max = 2,
988 .rates = SNDRV_PCM_RATE_48000,
989 .formats = SNDRV_PCM_FMTBIT_S16_LE,
990 },
Markus Pargmanna5a7ee72013-12-20 14:11:35 +0100991 .ops = &fsl_ssi_dai_ops,
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200992};
993
994
995static struct fsl_ssi_private *fsl_ac97_data;
996
Sachin Kamata851a2b2013-09-13 15:22:17 +0530997static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
Markus Pargmanncd7f0292013-08-19 17:05:58 +0200998 unsigned short val)
999{
1000 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
1001 unsigned int lreg;
1002 unsigned int lval;
1003
1004 if (reg > 0x7f)
1005 return;
1006
1007
1008 lreg = reg << 12;
1009 write_ssi(lreg, &ssi->sacadd);
1010
1011 lval = val << 4;
1012 write_ssi(lval , &ssi->sacdat);
1013
1014 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1015 CCSR_SSI_SACNT_WR);
1016 udelay(100);
1017}
1018
Sachin Kamata851a2b2013-09-13 15:22:17 +05301019static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001020 unsigned short reg)
1021{
1022 struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
1023
1024 unsigned short val = -1;
1025 unsigned int lreg;
1026
1027 lreg = (reg & 0x7f) << 12;
1028 write_ssi(lreg, &ssi->sacadd);
1029 write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1030 CCSR_SSI_SACNT_RD);
1031
1032 udelay(100);
1033
1034 val = (read_ssi(&ssi->sacdat) >> 4) & 0xffff;
1035
1036 return val;
1037}
1038
1039static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1040 .read = fsl_ssi_ac97_read,
1041 .write = fsl_ssi_ac97_write,
1042};
1043
Timur Tabi17467f22008-01-11 18:15:26 +01001044/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 * Make every character in a string lower-case
Timur Tabi17467f22008-01-11 18:15:26 +01001046 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047static void make_lowercase(char *s)
Timur Tabi17467f22008-01-11 18:15:26 +01001048{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001049 char *p = s;
1050 char c;
1051
1052 while ((c = *p)) {
1053 if ((c >= 'A') && (c <= 'Z'))
1054 *p = c + ('a' - 'A');
1055 p++;
1056 }
1057}
1058
Markus Pargmann49da09e2014-04-28 12:54:45 +02001059static int fsl_ssi_imx_probe(struct platform_device *pdev,
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001060 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001061{
1062 struct device_node *np = pdev->dev.of_node;
Markus Pargmanned0f16042014-04-28 12:54:46 +02001063 u32 dmas[4];
Markus Pargmann49da09e2014-04-28 12:54:45 +02001064 int ret;
1065
1066 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1067 if (IS_ERR(ssi_private->clk)) {
1068 ret = PTR_ERR(ssi_private->clk);
1069 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1070 return ret;
1071 }
1072
1073 ret = clk_prepare_enable(ssi_private->clk);
1074 if (ret) {
1075 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1076 return ret;
1077 }
1078
1079 /* For those SLAVE implementations, we ingore non-baudclk cases
1080 * and, instead, abandon MASTER mode that needs baud clock.
1081 */
1082 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1083 if (IS_ERR(ssi_private->baudclk))
1084 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1085 PTR_ERR(ssi_private->baudclk));
1086 else
1087 clk_prepare_enable(ssi_private->baudclk);
1088
1089 /*
1090 * We have burstsize be "fifo_depth - 2" to match the SSI
1091 * watermark setting in fsl_ssi_startup().
1092 */
1093 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1094 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1095 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys +
1096 offsetof(struct ccsr_ssi, stx0);
1097 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys +
1098 offsetof(struct ccsr_ssi, srx0);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001099
Markus Pargmanned0f16042014-04-28 12:54:46 +02001100 ret = !of_property_read_u32_array(np, "dmas", dmas, 4);
1101 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
Markus Pargmann49da09e2014-04-28 12:54:45 +02001102 ssi_private->use_dual_fifo = true;
1103 /* When using dual fifo mode, we need to keep watermark
1104 * as even numbers due to dma script limitation.
1105 */
1106 ssi_private->dma_params_tx.maxburst &= ~0x1;
1107 ssi_private->dma_params_rx.maxburst &= ~0x1;
1108 }
1109
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001110 if (!ssi_private->use_dma) {
1111
1112 /*
1113 * Some boards use an incompatible codec. To get it
1114 * working, we are using imx-fiq-pcm-audio, that
1115 * can handle those codecs. DMA is not possible in this
1116 * situation.
1117 */
1118
1119 ssi_private->fiq_params.irq = ssi_private->irq;
1120 ssi_private->fiq_params.base = iomem;
1121 ssi_private->fiq_params.dma_params_rx =
1122 &ssi_private->dma_params_rx;
1123 ssi_private->fiq_params.dma_params_tx =
1124 &ssi_private->dma_params_tx;
1125
1126 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1127 if (ret)
1128 goto error_pcm;
1129 } else {
1130 ret = imx_pcm_dma_init(pdev);
1131 if (ret)
1132 goto error_pcm;
1133 }
1134
Markus Pargmann49da09e2014-04-28 12:54:45 +02001135 return 0;
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001136
1137error_pcm:
1138 if (!IS_ERR(ssi_private->baudclk))
1139 clk_disable_unprepare(ssi_private->baudclk);
1140
1141 clk_disable_unprepare(ssi_private->clk);
1142
1143 return ret;
Markus Pargmann49da09e2014-04-28 12:54:45 +02001144}
1145
1146static void fsl_ssi_imx_clean(struct platform_device *pdev,
1147 struct fsl_ssi_private *ssi_private)
1148{
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001149 if (!ssi_private->use_dma)
1150 imx_pcm_fiq_exit(pdev);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001151 if (!IS_ERR(ssi_private->baudclk))
1152 clk_disable_unprepare(ssi_private->baudclk);
1153 clk_disable_unprepare(ssi_private->clk);
1154}
1155
Bill Pembertona0a3d512012-12-07 09:26:16 -05001156static int fsl_ssi_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001157{
Timur Tabi17467f22008-01-11 18:15:26 +01001158 struct fsl_ssi_private *ssi_private;
1159 int ret = 0;
Timur Tabi38fec722010-08-19 15:26:58 -05001160 struct device_node *np = pdev->dev.of_node;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001161 const struct of_device_id *of_id;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001162 const char *p, *sprop;
Timur Tabi8e9d8692010-08-06 12:16:12 -05001163 const uint32_t *iprop;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001164 struct resource res;
1165 char name[64];
Timur Tabi17467f22008-01-11 18:15:26 +01001166
Timur Tabiff713342010-08-04 17:51:08 -05001167 /* SSIs that are not connected on the board should have a
1168 * status = "disabled"
1169 * property in their device tree nodes.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001170 */
Timur Tabiff713342010-08-04 17:51:08 -05001171 if (!of_device_is_available(np))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001172 return -ENODEV;
1173
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001174 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001175 if (!of_id || !of_id->data)
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001176 return -EINVAL;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001177
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001178 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1179 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001180 if (!ssi_private) {
Timur Tabi38fec722010-08-19 15:26:58 -05001181 dev_err(&pdev->dev, "could not allocate DAI object\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001182 return -ENOMEM;
1183 }
Timur Tabi17467f22008-01-11 18:15:26 +01001184
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001185 ssi_private->soc = of_id->data;
1186
Markus Pargmann85e59af2014-05-27 10:24:19 +02001187 sprop = of_get_property(np, "fsl,mode", NULL);
1188 if (sprop) {
1189 if (!strcmp(sprop, "ac97-slave"))
1190 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
1191 else if (!strcmp(sprop, "i2s-slave"))
1192 ssi_private->dai_fmt = SND_SOC_DAIFMT_I2S |
1193 SND_SOC_DAIFMT_CBM_CFM;
1194 }
1195
Markus Pargmannde623ec2013-07-27 13:31:53 +02001196 ssi_private->use_dma = !of_property_read_bool(np,
1197 "fsl,fiq-stream-filter");
1198
Markus Pargmann85e59af2014-05-27 10:24:19 +02001199 if (fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001200 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1201 sizeof(fsl_ssi_ac97_dai));
1202
1203 fsl_ac97_data = ssi_private;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001204
1205 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1206 } else {
1207 /* Initialize this copy of the CPU DAI driver structure */
1208 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1209 sizeof(fsl_ssi_dai_template));
1210 }
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001211 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212
1213 /* Get the addresses and IRQ */
1214 ret = of_address_to_resource(np, 0, &res);
1215 if (ret) {
Timur Tabi38fec722010-08-19 15:26:58 -05001216 dev_err(&pdev->dev, "could not determine device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001217 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001218 }
Timur Tabi147dfe92011-06-08 15:02:55 -05001219 ssi_private->ssi = of_iomap(np, 0);
1220 if (!ssi_private->ssi) {
1221 dev_err(&pdev->dev, "could not map device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001222 return -ENOMEM;
Timur Tabi147dfe92011-06-08 15:02:55 -05001223 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001224 ssi_private->ssi_phys = res.start;
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001225
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001226 ssi_private->irq = irq_of_parse_and_map(np, 0);
Chen Gangd60336e2013-09-23 11:36:21 +08001227 if (!ssi_private->irq) {
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001228 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
Fabio Estevamb0a47472013-07-17 02:00:38 -03001229 return -ENXIO;
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001230 }
1231
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001232 /* Are the RX and the TX clocks locked? */
Nicolin Chen07a94832013-12-03 18:38:07 +08001233 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001234 ssi_private->cpu_dai_drv.symmetric_rates = 1;
Nicolin Chen07a94832013-12-03 18:38:07 +08001235 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1236 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1237 }
Timur Tabi17467f22008-01-11 18:15:26 +01001238
Timur Tabi8e9d8692010-08-06 12:16:12 -05001239 /* Determine the FIFO depth. */
1240 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1241 if (iprop)
Timur Tabi147dfe92011-06-08 15:02:55 -05001242 ssi_private->fifo_depth = be32_to_cpup(iprop);
Timur Tabi8e9d8692010-08-06 12:16:12 -05001243 else
1244 /* Older 8610 DTs didn't have the fifo-depth property */
1245 ssi_private->fifo_depth = 8;
1246
Nicolin Chenaafa85e2013-12-12 18:44:45 +08001247 ssi_private->baudclk_locked = false;
Nicolin Chenaafa85e2013-12-12 18:44:45 +08001248
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001249 dev_set_drvdata(&pdev->dev, ssi_private);
1250
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001251 if (ssi_private->soc->imx) {
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001252 ret = fsl_ssi_imx_probe(pdev, ssi_private, ssi_private->ssi);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001253 if (ret)
Fabio Estevamb0a47472013-07-17 02:00:38 -03001254 goto error_irqmap;
Markus Pargmann0888efd2013-12-20 14:11:31 +01001255 }
1256
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001257 ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1258 &ssi_private->cpu_dai_drv, 1);
1259 if (ret) {
1260 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1261 goto error_asoc_register;
1262 }
1263
Markus Pargmann0888efd2013-12-20 14:11:31 +01001264 if (ssi_private->use_dma) {
Michael Grzeschikf0377082013-08-19 17:06:01 +02001265 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
Markus Pargmann171d6832014-04-28 12:54:48 +02001266 fsl_ssi_isr, 0, dev_name(&pdev->dev),
Michael Grzeschikf0377082013-08-19 17:06:01 +02001267 ssi_private);
1268 if (ret < 0) {
1269 dev_err(&pdev->dev, "could not claim irq %u\n",
1270 ssi_private->irq);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001271 goto error_irq;
Michael Grzeschikf0377082013-08-19 17:06:01 +02001272 }
Shawn Guo09ce1112012-03-16 16:56:43 +08001273 }
1274
Markus Pargmannf138e622014-04-28 12:54:43 +02001275 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001276 if (ret)
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001277 goto error_asoc_register;
Shawn Guo09ce1112012-03-16 16:56:43 +08001278
1279 /*
1280 * If codec-handle property is missing from SSI node, we assume
1281 * that the machine driver uses new binding which does not require
1282 * SSI driver to trigger machine driver's probe.
1283 */
Markus Pargmann171d6832014-04-28 12:54:48 +02001284 if (!of_get_property(np, "codec-handle", NULL))
Shawn Guo09ce1112012-03-16 16:56:43 +08001285 goto done;
Shawn Guo09ce1112012-03-16 16:56:43 +08001286
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001287 /* Trigger the machine driver's probe function. The platform driver
Shawn Guo2b81ec62012-03-09 00:59:46 +08001288 * name of the machine driver is taken from /compatible property of the
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001289 * device tree. We also pass the address of the CPU DAI driver
1290 * structure.
1291 */
Shawn Guo2b81ec62012-03-09 00:59:46 +08001292 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1293 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001294 p = strrchr(sprop, ',');
1295 if (p)
1296 sprop = p + 1;
1297 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1298 make_lowercase(name);
1299
1300 ssi_private->pdev =
Timur Tabi38fec722010-08-19 15:26:58 -05001301 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001302 if (IS_ERR(ssi_private->pdev)) {
1303 ret = PTR_ERR(ssi_private->pdev);
Timur Tabi38fec722010-08-19 15:26:58 -05001304 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001305 goto error_sound_card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001306 }
1307
Shawn Guo09ce1112012-03-16 16:56:43 +08001308done:
Markus Pargmann85e59af2014-05-27 10:24:19 +02001309 if (ssi_private->dai_fmt)
1310 _fsl_ssi_set_dai_fmt(ssi_private, ssi_private->dai_fmt);
1311
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001312 return 0;
Timur Tabi87a06322010-08-03 17:55:28 -05001313
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001314error_sound_card:
Markus Pargmannf138e622014-04-28 12:54:43 +02001315 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001316
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001317error_irq:
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001318 snd_soc_unregister_component(&pdev->dev);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001319
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001320error_asoc_register:
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001321 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001322 fsl_ssi_imx_clean(pdev, ssi_private);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001323
1324error_irqmap:
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001325 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001326 irq_dispose_mapping(ssi_private->irq);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001327
Timur Tabi87a06322010-08-03 17:55:28 -05001328 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +01001329}
Timur Tabi17467f22008-01-11 18:15:26 +01001330
Timur Tabi38fec722010-08-19 15:26:58 -05001331static int fsl_ssi_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +01001332{
Timur Tabi38fec722010-08-19 15:26:58 -05001333 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
Timur Tabi17467f22008-01-11 18:15:26 +01001334
Markus Pargmannf138e622014-04-28 12:54:43 +02001335 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001336
Markus Pargmann171d6832014-04-28 12:54:48 +02001337 if (ssi_private->pdev)
Shawn Guo09ce1112012-03-16 16:56:43 +08001338 platform_device_unregister(ssi_private->pdev);
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001339 snd_soc_unregister_component(&pdev->dev);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001340
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001341 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001342 fsl_ssi_imx_clean(pdev, ssi_private);
1343
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001344 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001345 irq_dispose_mapping(ssi_private->irq);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001346
1347 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +01001348}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001349
Grant Likelyf07eb222011-02-22 21:05:04 -07001350static struct platform_driver fsl_ssi_driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001351 .driver = {
1352 .name = "fsl-ssi-dai",
1353 .owner = THIS_MODULE,
1354 .of_match_table = fsl_ssi_ids,
1355 },
1356 .probe = fsl_ssi_probe,
1357 .remove = fsl_ssi_remove,
1358};
Timur Tabi17467f22008-01-11 18:15:26 +01001359
Axel Linba0a7e02011-11-25 10:10:55 +08001360module_platform_driver(fsl_ssi_driver);
Timur Tabia454dad2009-03-05 17:23:37 -06001361
Fabio Estevamf3142802013-07-20 16:16:01 -03001362MODULE_ALIAS("platform:fsl-ssi-dai");
Timur Tabi17467f22008-01-11 18:15:26 +01001363MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1364MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001365MODULE_LICENSE("GPL v2");