blob: bc19849053a55332fee6438fd8eb18aeda1d05fb [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>
Mark Brown9c72a042014-04-15 12:02:02 +010042#include <linux/of.h>
Shawn Guodfa1a102012-03-16 16:56:42 +080043#include <linux/of_address.h>
44#include <linux/of_irq.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000045#include <linux/of_platform.h>
Timur Tabi17467f22008-01-11 18:15:26 +010046
Timur Tabi17467f22008-01-11 18:15:26 +010047#include <sound/core.h>
48#include <sound/pcm.h>
49#include <sound/pcm_params.h>
50#include <sound/initval.h>
51#include <sound/soc.h>
Lars-Peter Clausena8909c92013-04-03 11:06:04 +020052#include <sound/dmaengine_pcm.h>
Timur Tabi17467f22008-01-11 18:15:26 +010053
Timur Tabi17467f22008-01-11 18:15:26 +010054#include "fsl_ssi.h"
Shawn Guo09ce1112012-03-16 16:56:43 +080055#include "imx-pcm.h"
Timur Tabi17467f22008-01-11 18:15:26 +010056
57/**
58 * FSLSSI_I2S_RATES: sample rates supported by the I2S
59 *
60 * This driver currently only supports the SSI running in I2S slave mode,
61 * which means the codec determines the sample rate. Therefore, we tell
62 * ALSA that we support all rates and let the codec driver decide what rates
63 * are really supported.
64 */
Lars-Peter Clausen24710c92014-01-11 10:24:41 +010065#define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
Timur Tabi17467f22008-01-11 18:15:26 +010066
67/**
68 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
69 *
Timur Tabi17467f22008-01-11 18:15:26 +010070 * The SSI has a limitation in that the samples must be in the same byte
71 * order as the host CPU. This is because when multiple bytes are written
72 * to the STX register, the bytes and bits must be written in the same
73 * order. The STX is a shift register, so all the bits need to be aligned
74 * (bit-endianness must match byte-endianness). Processors typically write
75 * the bits within a byte in the same order that the bytes of a word are
76 * written in. So if the host CPU is big-endian, then only big-endian
77 * samples will be written to STX properly.
78 */
79#ifdef __BIG_ENDIAN
80#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
81 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
82 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
83#else
84#define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
85 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
86 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
87#endif
88
Markus Pargmann9368acc2013-12-20 14:11:29 +010089#define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
90 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
91 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
92#define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
93 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
94 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
Markus Pargmannc1953bf2013-12-20 14:11:30 +010095
96enum fsl_ssi_type {
97 FSL_SSI_MCP8610,
98 FSL_SSI_MX21,
Markus Pargmann0888efd2013-12-20 14:11:31 +010099 FSL_SSI_MX35,
Markus Pargmannc1953bf2013-12-20 14:11:30 +0100100 FSL_SSI_MX51,
101};
102
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100103struct fsl_ssi_reg_val {
104 u32 sier;
105 u32 srcr;
106 u32 stcr;
107 u32 scr;
108};
109
110struct fsl_ssi_rxtx_reg_val {
111 struct fsl_ssi_reg_val rx;
112 struct fsl_ssi_reg_val tx;
113};
Markus Pargmann43248122014-05-27 10:24:25 +0200114static const struct regmap_config fsl_ssi_regconfig = {
115 .max_register = CCSR_SSI_SACCDIS,
116 .reg_bits = 32,
117 .val_bits = 32,
118 .reg_stride = 4,
119 .val_format_endian = REGMAP_ENDIAN_NATIVE,
120};
Timur Tabid5a908b2009-03-26 11:42:38 -0500121
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200122struct fsl_ssi_soc_data {
123 bool imx;
124 bool offline_config;
125 u32 sisr_write_mask;
126};
127
Timur Tabi17467f22008-01-11 18:15:26 +0100128/**
129 * fsl_ssi_private: per-SSI private data
130 *
Markus Pargmann43248122014-05-27 10:24:25 +0200131 * @reg: Pointer to the regmap registers
Timur Tabi17467f22008-01-11 18:15:26 +0100132 * @irq: IRQ of this SSI
Markus Pargmann737a6b42014-05-27 10:24:24 +0200133 * @cpu_dai_drv: CPU DAI driver for this device
134 *
135 * @dai_fmt: DAI configuration this device is currently used with
136 * @i2s_mode: i2s and network mode configuration of the device. Is used to
137 * switch between normal and i2s/network mode
138 * mode depending on the number of channels
139 * @use_dma: DMA is used or FIQ with stream filter
140 * @use_dual_fifo: DMA with support for both FIFOs used
141 * @fifo_deph: Depth of the SSI FIFOs
142 * @rxtx_reg_val: Specific register settings for receive/transmit configuration
143 *
144 * @clk: SSI clock
145 * @baudclk: SSI baud clock for master mode
146 * @baudclk_streams: Active streams that are using baudclk
147 * @bitclk_freq: bitclock frequency set by .set_dai_sysclk
148 *
149 * @dma_params_tx: DMA transmit parameters
150 * @dma_params_rx: DMA receive parameters
151 * @ssi_phys: physical address of the SSI registers
152 *
153 * @fiq_params: FIQ stream filtering parameters
154 *
155 * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
156 *
157 * @dbg_stats: Debugging statistics
158 *
159 * @soc: SoC specifc data
Timur Tabi17467f22008-01-11 18:15:26 +0100160 */
161struct fsl_ssi_private {
Markus Pargmann43248122014-05-27 10:24:25 +0200162 struct regmap *regs;
Timur Tabi17467f22008-01-11 18:15:26 +0100163 unsigned int irq;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000164 struct snd_soc_dai_driver cpu_dai_drv;
Timur Tabi17467f22008-01-11 18:15:26 +0100165
Markus Pargmann737a6b42014-05-27 10:24:24 +0200166 unsigned int dai_fmt;
167 u8 i2s_mode;
Markus Pargmannde623ec2013-07-27 13:31:53 +0200168 bool use_dma;
Nicolin Chen0da9e552013-11-13 22:55:26 +0800169 bool use_dual_fifo;
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +0800170 bool has_ipg_clk_name;
Markus Pargmann737a6b42014-05-27 10:24:24 +0200171 unsigned int fifo_depth;
172 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
173
Shawn Guo95cd98f2012-03-29 10:53:41 +0800174 struct clk *clk;
Markus Pargmann737a6b42014-05-27 10:24:24 +0200175 struct clk *baudclk;
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200176 unsigned int baudclk_streams;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200177 unsigned int bitclk_freq;
Markus Pargmann737a6b42014-05-27 10:24:24 +0200178
179 /* DMA params */
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200180 struct snd_dmaengine_dai_dma_data dma_params_tx;
181 struct snd_dmaengine_dai_dma_data dma_params_rx;
Markus Pargmann737a6b42014-05-27 10:24:24 +0200182 dma_addr_t ssi_phys;
183
184 /* params for non-dma FIQ stream filtered mode */
Markus Pargmannde623ec2013-07-27 13:31:53 +0200185 struct imx_pcm_fiq_params fiq_params;
Markus Pargmann737a6b42014-05-27 10:24:24 +0200186
187 /* Used when using fsl-ssi as sound-card. This is only used by ppc and
188 * should be replaced with simple-sound-card. */
189 struct platform_device *pdev;
Shawn Guo09ce1112012-03-16 16:56:43 +0800190
Markus Pargmannf138e622014-04-28 12:54:43 +0200191 struct fsl_ssi_dbg dbg_stats;
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200192
193 const struct fsl_ssi_soc_data *soc;
Timur Tabi17467f22008-01-11 18:15:26 +0100194};
195
Markus Pargmann171d6832014-04-28 12:54:48 +0200196/*
197 * imx51 and later SoCs have a slightly different IP that allows the
198 * SSI configuration while the SSI unit is running.
199 *
200 * More important, it is necessary on those SoCs to configure the
201 * sperate TX/RX DMA bits just before starting the stream
202 * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
203 * sends any DMA requests to the SDMA unit, otherwise it is not defined
204 * how the SDMA unit handles the DMA request.
205 *
206 * SDMA units are present on devices starting at imx35 but the imx35
207 * reference manual states that the DMA bits should not be changed
208 * while the SSI unit is running (SSIEN). So we support the necessary
209 * online configuration of fsl-ssi starting at imx51.
210 */
Markus Pargmann171d6832014-04-28 12:54:48 +0200211
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200212static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
213 .imx = false,
214 .offline_config = true,
215 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
216 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
217 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
218};
219
220static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
221 .imx = true,
222 .offline_config = true,
223 .sisr_write_mask = 0,
224};
225
226static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
227 .imx = true,
228 .offline_config = true,
229 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
230 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
231 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
232};
233
234static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
235 .imx = true,
236 .offline_config = false,
237 .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
238 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
239};
240
241static const struct of_device_id fsl_ssi_ids[] = {
242 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
243 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
244 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
245 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
246 {}
247};
248MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
249
250static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
251{
252 return !!(ssi_private->dai_fmt & SND_SOC_DAIFMT_AC97);
Markus Pargmann171d6832014-04-28 12:54:48 +0200253}
254
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200255static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
256{
257 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
258 SND_SOC_DAIFMT_CBS_CFS;
259}
260
Fabio Falzoicf4f7fc2014-08-04 17:08:07 +0200261static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
262{
263 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
264 SND_SOC_DAIFMT_CBM_CFS;
265}
Timur Tabi17467f22008-01-11 18:15:26 +0100266/**
267 * fsl_ssi_isr: SSI interrupt handler
268 *
269 * Although it's possible to use the interrupt handler to send and receive
270 * data to/from the SSI, we use the DMA instead. Programming is more
271 * complicated, but the performance is much better.
272 *
273 * This interrupt handler is used only to gather statistics.
274 *
275 * @irq: IRQ of the SSI device
276 * @dev_id: pointer to the ssi_private structure for this SSI device
277 */
278static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
279{
280 struct fsl_ssi_private *ssi_private = dev_id;
Markus Pargmann43248122014-05-27 10:24:25 +0200281 struct regmap *regs = ssi_private->regs;
Timur Tabi17467f22008-01-11 18:15:26 +0100282 __be32 sisr;
Markus Pargmann0888efd2013-12-20 14:11:31 +0100283 __be32 sisr2;
Timur Tabi17467f22008-01-11 18:15:26 +0100284
285 /* We got an interrupt, so read the status register to see what we
286 were interrupted for. We mask it with the Interrupt Enable register
287 so that we only check for events that we're interested in.
288 */
Markus Pargmann43248122014-05-27 10:24:25 +0200289 regmap_read(regs, CCSR_SSI_SISR, &sisr);
Timur Tabi17467f22008-01-11 18:15:26 +0100290
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200291 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
Timur Tabi17467f22008-01-11 18:15:26 +0100292 /* Clear the bits that we set */
293 if (sisr2)
Markus Pargmann43248122014-05-27 10:24:25 +0200294 regmap_write(regs, CCSR_SSI_SISR, sisr2);
Timur Tabi17467f22008-01-11 18:15:26 +0100295
Markus Pargmannf138e622014-04-28 12:54:43 +0200296 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
297
298 return IRQ_HANDLED;
Timur Tabi17467f22008-01-11 18:15:26 +0100299}
300
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100301/*
302 * Enable/Disable all rx/tx config flags at once.
303 */
304static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
305 bool enable)
306{
Markus Pargmann43248122014-05-27 10:24:25 +0200307 struct regmap *regs = ssi_private->regs;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100308 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
309
310 if (enable) {
Markus Pargmann43248122014-05-27 10:24:25 +0200311 regmap_update_bits(regs, CCSR_SSI_SIER,
312 vals->rx.sier | vals->tx.sier,
313 vals->rx.sier | vals->tx.sier);
314 regmap_update_bits(regs, CCSR_SSI_SRCR,
315 vals->rx.srcr | vals->tx.srcr,
316 vals->rx.srcr | vals->tx.srcr);
317 regmap_update_bits(regs, CCSR_SSI_STCR,
318 vals->rx.stcr | vals->tx.stcr,
319 vals->rx.stcr | vals->tx.stcr);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100320 } else {
Markus Pargmann43248122014-05-27 10:24:25 +0200321 regmap_update_bits(regs, CCSR_SSI_SRCR,
322 vals->rx.srcr | vals->tx.srcr, 0);
323 regmap_update_bits(regs, CCSR_SSI_STCR,
324 vals->rx.stcr | vals->tx.stcr, 0);
325 regmap_update_bits(regs, CCSR_SSI_SIER,
326 vals->rx.sier | vals->tx.sier, 0);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100327 }
328}
329
330/*
Markus Pargmann65c961c2014-04-28 12:54:42 +0200331 * Calculate the bits that have to be disabled for the current stream that is
332 * getting disabled. This keeps the bits enabled that are necessary for the
333 * second stream to work if 'stream_active' is true.
334 *
335 * Detailed calculation:
336 * These are the values that need to be active after disabling. For non-active
337 * second stream, this is 0:
338 * vals_stream * !!stream_active
339 *
340 * The following computes the overall differences between the setup for the
341 * to-disable stream and the active stream, a simple XOR:
342 * vals_disable ^ (vals_stream * !!(stream_active))
343 *
344 * The full expression adds a mask on all values we care about
345 */
346#define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
347 ((vals_disable) & \
348 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
349
350/*
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100351 * Enable/Disable a ssi configuration. You have to pass either
352 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
353 */
354static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
355 struct fsl_ssi_reg_val *vals)
356{
Markus Pargmann43248122014-05-27 10:24:25 +0200357 struct regmap *regs = ssi_private->regs;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100358 struct fsl_ssi_reg_val *avals;
Markus Pargmann43248122014-05-27 10:24:25 +0200359 int nr_active_streams;
360 u32 scr_val;
Markus Pargmann65c961c2014-04-28 12:54:42 +0200361 int keep_active;
362
Markus Pargmann43248122014-05-27 10:24:25 +0200363 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
364
365 nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
366 !!(scr_val & CCSR_SSI_SCR_RE);
367
Markus Pargmann65c961c2014-04-28 12:54:42 +0200368 if (nr_active_streams - 1 > 0)
369 keep_active = 1;
370 else
371 keep_active = 0;
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100372
373 /* Find the other direction values rx or tx which we do not want to
374 * modify */
375 if (&ssi_private->rxtx_reg_val.rx == vals)
376 avals = &ssi_private->rxtx_reg_val.tx;
377 else
378 avals = &ssi_private->rxtx_reg_val.rx;
379
380 /* If vals should be disabled, start with disabling the unit */
381 if (!enable) {
Markus Pargmann65c961c2014-04-28 12:54:42 +0200382 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
383 keep_active);
Markus Pargmann43248122014-05-27 10:24:25 +0200384 regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100385 }
386
387 /*
388 * We are running on a SoC which does not support online SSI
389 * reconfiguration, so we have to enable all necessary flags at once
390 * even if we do not use them later (capture and playback configuration)
391 */
Sascha Hauerfcdbade2014-05-27 10:24:18 +0200392 if (ssi_private->soc->offline_config) {
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100393 if ((enable && !nr_active_streams) ||
Markus Pargmann65c961c2014-04-28 12:54:42 +0200394 (!enable && !keep_active))
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100395 fsl_ssi_rxtx_config(ssi_private, enable);
396
397 goto config_done;
398 }
399
400 /*
401 * Configure single direction units while the SSI unit is running
402 * (online configuration)
403 */
404 if (enable) {
Markus Pargmann43248122014-05-27 10:24:25 +0200405 regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
406 regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
407 regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100408 } else {
409 u32 sier;
410 u32 srcr;
411 u32 stcr;
412
413 /*
414 * Disabling the necessary flags for one of rx/tx while the
415 * other stream is active is a little bit more difficult. We
416 * have to disable only those flags that differ between both
417 * streams (rx XOR tx) and that are set in the stream that is
418 * disabled now. Otherwise we could alter flags of the other
419 * stream
420 */
421
422 /* These assignments are simply vals without bits set in avals*/
Markus Pargmann65c961c2014-04-28 12:54:42 +0200423 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
424 keep_active);
425 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
426 keep_active);
427 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
428 keep_active);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100429
Markus Pargmann43248122014-05-27 10:24:25 +0200430 regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
431 regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
432 regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100433 }
434
435config_done:
436 /* Enabling of subunits is done after configuration */
437 if (enable)
Markus Pargmann43248122014-05-27 10:24:25 +0200438 regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
Markus Pargmann4e6ec0d2013-12-20 14:11:33 +0100439}
440
441
442static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
443{
444 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
445}
446
447static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
448{
449 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
450}
451
Markus Pargmann6de83872013-12-20 14:11:34 +0100452/*
453 * Setup rx/tx register values used to enable/disable the streams. These will
454 * be used later in fsl_ssi_config to setup the streams without the need to
455 * check for all different SSI modes.
456 */
457static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
458{
459 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
460
461 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
462 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
463 reg->rx.scr = 0;
464 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
465 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
466 reg->tx.scr = 0;
467
Markus Pargmann171d6832014-04-28 12:54:48 +0200468 if (!fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmann6de83872013-12-20 14:11:34 +0100469 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
470 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
471 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
472 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
473 }
474
475 if (ssi_private->use_dma) {
476 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
477 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
478 } else {
479 reg->rx.sier |= CCSR_SSI_SIER_RIE;
480 reg->tx.sier |= CCSR_SSI_SIER_TIE;
481 }
482
483 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
484 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
485}
486
Markus Pargmannd8764642013-11-20 10:04:15 +0100487static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
488{
Markus Pargmann43248122014-05-27 10:24:25 +0200489 struct regmap *regs = ssi_private->regs;
Markus Pargmannd8764642013-11-20 10:04:15 +0100490
491 /*
492 * Setup the clock control register
493 */
Markus Pargmann43248122014-05-27 10:24:25 +0200494 regmap_write(regs, CCSR_SSI_STCCR,
495 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
496 regmap_write(regs, CCSR_SSI_SRCCR,
497 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
Markus Pargmannd8764642013-11-20 10:04:15 +0100498
499 /*
500 * Enable AC97 mode and startup the SSI
501 */
Markus Pargmann43248122014-05-27 10:24:25 +0200502 regmap_write(regs, CCSR_SSI_SACNT,
503 CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
504 regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
505 regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
Markus Pargmannd8764642013-11-20 10:04:15 +0100506
507 /*
508 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
509 * codec before a stream is started.
510 */
Markus Pargmann43248122014-05-27 10:24:25 +0200511 regmap_update_bits(regs, CCSR_SSI_SCR,
512 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
513 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
Markus Pargmannd8764642013-11-20 10:04:15 +0100514
Markus Pargmann43248122014-05-27 10:24:25 +0200515 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
Markus Pargmannd8764642013-11-20 10:04:15 +0100516}
517
Timur Tabi17467f22008-01-11 18:15:26 +0100518/**
519 * fsl_ssi_startup: create a new substream
520 *
521 * This is the first function called when a stream is opened.
522 *
523 * If this is the first stream open, then grab the IRQ and program most of
524 * the SSI registers.
525 */
Mark Browndee89c42008-11-18 22:11:38 +0000526static int fsl_ssi_startup(struct snd_pcm_substream *substream,
527 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100528{
529 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Timur Tabi5e538ec2011-09-13 12:59:37 -0500530 struct fsl_ssi_private *ssi_private =
531 snd_soc_dai_get_drvdata(rtd->cpu_dai);
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +0800532 int ret;
533
534 ret = clk_prepare_enable(ssi_private->clk);
535 if (ret)
536 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +0100537
Nicolin Chen0da9e552013-11-13 22:55:26 +0800538 /* When using dual fifo mode, it is safer to ensure an even period
539 * size. If appearing to an odd number while DMA always starts its
540 * task from fifo0, fifo1 would be neglected at the end of each
541 * period. But SSI would still access fifo1 with an invalid data.
542 */
543 if (ssi_private->use_dual_fifo)
544 snd_pcm_hw_constraint_step(substream->runtime, 0,
545 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
546
Timur Tabi17467f22008-01-11 18:15:26 +0100547 return 0;
548}
549
550/**
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +0800551 * fsl_ssi_shutdown: shutdown the SSI
552 *
553 */
554static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
555 struct snd_soc_dai *dai)
556{
557 struct snd_soc_pcm_runtime *rtd = substream->private_data;
558 struct fsl_ssi_private *ssi_private =
559 snd_soc_dai_get_drvdata(rtd->cpu_dai);
560
561 clk_disable_unprepare(ssi_private->clk);
562
563}
564
565/**
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200566 * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
Sascha Haueree9daad2014-04-28 12:54:52 +0200567 *
568 * Note: This function can be only called when using SSI as DAI master
569 *
570 * Quick instruction for parameters:
571 * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
572 * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
573 */
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200574static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
575 struct snd_soc_dai *cpu_dai,
576 struct snd_pcm_hw_params *hw_params)
Sascha Haueree9daad2014-04-28 12:54:52 +0200577{
578 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Markus Pargmann43248122014-05-27 10:24:25 +0200579 struct regmap *regs = ssi_private->regs;
Sascha Haueree9daad2014-04-28 12:54:52 +0200580 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
581 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
Sascha Hauerd8ced472014-05-27 10:24:21 +0200582 unsigned long clkrate, baudrate, tmprate;
Sascha Haueree9daad2014-04-28 12:54:52 +0200583 u64 sub, savesub = 100000;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200584 unsigned int freq;
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200585 bool baudclk_is_used;
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200586
587 /* Prefer the explicitly set bitclock frequency */
588 if (ssi_private->bitclk_freq)
589 freq = ssi_private->bitclk_freq;
590 else
591 freq = params_channels(hw_params) * 32 * params_rate(hw_params);
Sascha Haueree9daad2014-04-28 12:54:52 +0200592
593 /* Don't apply it to any non-baudclk circumstance */
594 if (IS_ERR(ssi_private->baudclk))
595 return -EINVAL;
596
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200597 baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
598
Sascha Haueree9daad2014-04-28 12:54:52 +0200599 /* It should be already enough to divide clock by setting pm alone */
600 psr = 0;
601 div2 = 0;
602
603 factor = (div2 + 1) * (7 * psr + 1) * 2;
604
605 for (i = 0; i < 255; i++) {
606 /* The bclk rate must be smaller than 1/5 sysclk rate */
607 if (factor * (i + 1) < 5)
608 continue;
609
610 tmprate = freq * factor * (i + 2);
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200611
612 if (baudclk_is_used)
613 clkrate = clk_get_rate(ssi_private->baudclk);
614 else
615 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
Sascha Haueree9daad2014-04-28 12:54:52 +0200616
Timur Tabiacf2c602014-06-13 07:42:40 -0500617 clkrate /= factor;
618 afreq = clkrate / (i + 1);
Sascha Haueree9daad2014-04-28 12:54:52 +0200619
620 if (freq == afreq)
621 sub = 0;
622 else if (freq / afreq == 1)
623 sub = freq - afreq;
624 else if (afreq / freq == 1)
625 sub = afreq - freq;
626 else
627 continue;
628
629 /* Calculate the fraction */
630 sub *= 100000;
631 do_div(sub, freq);
632
633 if (sub < savesub) {
634 baudrate = tmprate;
635 savesub = sub;
636 pm = i;
637 }
638
639 /* We are lucky */
640 if (savesub == 0)
641 break;
642 }
643
644 /* No proper pm found if it is still remaining the initial value */
645 if (pm == 999) {
646 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
647 return -EINVAL;
648 }
649
650 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
651 (psr ? CCSR_SSI_SxCCR_PSR : 0);
652 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
653 CCSR_SSI_SxCCR_PSR;
654
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200655 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
Markus Pargmann43248122014-05-27 10:24:25 +0200656 regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
Sascha Haueree9daad2014-04-28 12:54:52 +0200657 else
Markus Pargmann43248122014-05-27 10:24:25 +0200658 regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
Sascha Haueree9daad2014-04-28 12:54:52 +0200659
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200660 if (!baudclk_is_used) {
Sascha Haueree9daad2014-04-28 12:54:52 +0200661 ret = clk_set_rate(ssi_private->baudclk, baudrate);
662 if (ret) {
Sascha Haueree9daad2014-04-28 12:54:52 +0200663 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
664 return -EINVAL;
665 }
Sascha Haueree9daad2014-04-28 12:54:52 +0200666 }
Sascha Haueree9daad2014-04-28 12:54:52 +0200667
668 return 0;
669}
670
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200671static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
672 int clk_id, unsigned int freq, int dir)
673{
674 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
675
676 ssi_private->bitclk_freq = freq;
677
678 return 0;
679}
680
Sascha Haueree9daad2014-04-28 12:54:52 +0200681/**
Timur Tabi85ef2372009-02-05 17:56:02 -0600682 * fsl_ssi_hw_params - program the sample size
Timur Tabi17467f22008-01-11 18:15:26 +0100683 *
684 * Most of the SSI registers have been programmed in the startup function,
685 * but the word length must be programmed here. Unfortunately, programming
686 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
687 * cause a problem with supporting simultaneous playback and capture. If
688 * the SSI is already playing a stream, then that stream may be temporarily
689 * stopped when you start capture.
690 *
691 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
692 * clock master.
693 */
Timur Tabi85ef2372009-02-05 17:56:02 -0600694static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
695 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
Timur Tabi17467f22008-01-11 18:15:26 +0100696{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Markus Pargmann43248122014-05-27 10:24:25 +0200698 struct regmap *regs = ssi_private->regs;
Nicolin Chen2924a992013-12-02 23:29:03 +0800699 unsigned int channels = params_channels(hw_params);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500700 unsigned int sample_size =
701 snd_pcm_format_width(params_format(hw_params));
702 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200703 int ret;
Markus Pargmann43248122014-05-27 10:24:25 +0200704 u32 scr_val;
705 int enabled;
706
707 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
708 enabled = scr_val & CCSR_SSI_SCR_SSIEN;
Timur Tabi17467f22008-01-11 18:15:26 +0100709
Timur Tabi5e538ec2011-09-13 12:59:37 -0500710 /*
711 * If we're in synchronous mode, and the SSI is already enabled,
712 * then STCCR is already set properly.
713 */
714 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
715 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +0100716
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200717 if (fsl_ssi_is_i2s_master(ssi_private)) {
718 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
719 if (ret)
720 return ret;
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200721
722 /* Do not enable the clock if it is already enabled */
723 if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
724 ret = clk_prepare_enable(ssi_private->baudclk);
725 if (ret)
726 return ret;
727
728 ssi_private->baudclk_streams |= BIT(substream->stream);
729 }
Sascha Hauer8dd51e22014-05-27 10:24:20 +0200730 }
731
Fabio Falzoicf4f7fc2014-08-04 17:08:07 +0200732 if (!fsl_ssi_is_ac97(ssi_private)) {
733 u8 i2smode;
734 /*
735 * Switch to normal net mode in order to have a frame sync
736 * signal every 32 bits instead of 16 bits
737 */
738 if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
739 i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
740 CCSR_SSI_SCR_NET;
741 else
742 i2smode = ssi_private->i2s_mode;
743
744 regmap_update_bits(regs, CCSR_SSI_SCR,
745 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
746 channels == 1 ? 0 : i2smode);
747 }
748
Timur Tabi5e538ec2011-09-13 12:59:37 -0500749 /*
750 * FIXME: The documentation says that SxCCR[WL] should not be
751 * modified while the SSI is enabled. The only time this can
752 * happen is if we're trying to do simultaneous playback and
753 * capture in asynchronous mode. Unfortunately, I have been enable
754 * to get that to work at all on the P1022DS. Therefore, we don't
755 * bother to disable/enable the SSI when setting SxCCR[WL], because
756 * the SSI will stop anyway. Maybe one day, this will get fixed.
757 */
Timur Tabi17467f22008-01-11 18:15:26 +0100758
Timur Tabi5e538ec2011-09-13 12:59:37 -0500759 /* In synchronous mode, the SSI uses STCCR for capture */
760 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
761 ssi_private->cpu_dai_drv.symmetric_rates)
Markus Pargmann43248122014-05-27 10:24:25 +0200762 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
763 wl);
Timur Tabi5e538ec2011-09-13 12:59:37 -0500764 else
Markus Pargmann43248122014-05-27 10:24:25 +0200765 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
766 wl);
Timur Tabi17467f22008-01-11 18:15:26 +0100767
768 return 0;
769}
770
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200771static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
772 struct snd_soc_dai *cpu_dai)
773{
774 struct snd_soc_pcm_runtime *rtd = substream->private_data;
775 struct fsl_ssi_private *ssi_private =
776 snd_soc_dai_get_drvdata(rtd->cpu_dai);
777
778 if (fsl_ssi_is_i2s_master(ssi_private) &&
779 ssi_private->baudclk_streams & BIT(substream->stream)) {
780 clk_disable_unprepare(ssi_private->baudclk);
781 ssi_private->baudclk_streams &= ~BIT(substream->stream);
782 }
783
784 return 0;
785}
786
Michael Trimarchi85151462014-09-18 20:38:09 +0200787static int _fsl_ssi_set_dai_fmt(struct device *dev,
788 struct fsl_ssi_private *ssi_private,
789 unsigned int fmt)
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800790{
Markus Pargmann43248122014-05-27 10:24:25 +0200791 struct regmap *regs = ssi_private->regs;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800792 u32 strcr = 0, stcr, srcr, scr, mask;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100793 u8 wm;
794
Markus Pargmann171d6832014-04-28 12:54:48 +0200795 ssi_private->dai_fmt = fmt;
796
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200797 if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
Michael Trimarchi85151462014-09-18 20:38:09 +0200798 dev_err(dev, "baudclk is missing which is necessary for master mode\n");
Markus Pargmannd429d8e2014-05-27 10:24:23 +0200799 return -EINVAL;
800 }
801
Markus Pargmann2b0db992014-03-15 13:44:09 +0100802 fsl_ssi_setup_reg_vals(ssi_private);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800803
Markus Pargmann43248122014-05-27 10:24:25 +0200804 regmap_read(regs, CCSR_SSI_SCR, &scr);
805 scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
Markus Pargmann50489472014-04-28 12:54:51 +0200806 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800807
808 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
809 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
810 CCSR_SSI_STCR_TEFS;
Markus Pargmann43248122014-05-27 10:24:25 +0200811 regmap_read(regs, CCSR_SSI_STCR, &stcr);
812 regmap_read(regs, CCSR_SSI_SRCR, &srcr);
813 stcr &= ~mask;
814 srcr &= ~mask;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800815
Markus Pargmann07a28db2014-03-15 13:44:10 +0100816 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800817 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
818 case SND_SOC_DAIFMT_I2S:
819 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Fabio Falzoicf4f7fc2014-08-04 17:08:07 +0200820 case SND_SOC_DAIFMT_CBM_CFS:
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800821 case SND_SOC_DAIFMT_CBS_CFS:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100822 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
Markus Pargmann43248122014-05-27 10:24:25 +0200823 regmap_update_bits(regs, CCSR_SSI_STCCR,
824 CCSR_SSI_SxCCR_DC_MASK,
825 CCSR_SSI_SxCCR_DC(2));
826 regmap_update_bits(regs, CCSR_SSI_SRCCR,
827 CCSR_SSI_SxCCR_DC_MASK,
828 CCSR_SSI_SxCCR_DC(2));
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800829 break;
830 case SND_SOC_DAIFMT_CBM_CFM:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100831 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800832 break;
833 default:
834 return -EINVAL;
835 }
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800836
837 /* Data on rising edge of bclk, frame low, 1clk before data */
838 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
839 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
840 break;
841 case SND_SOC_DAIFMT_LEFT_J:
842 /* Data on rising edge of bclk, frame high */
843 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
844 break;
845 case SND_SOC_DAIFMT_DSP_A:
846 /* Data on rising edge of bclk, frame high, 1clk before data */
847 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
848 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
849 break;
850 case SND_SOC_DAIFMT_DSP_B:
851 /* Data on rising edge of bclk, frame high */
852 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
853 CCSR_SSI_STCR_TXBIT0;
854 break;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100855 case SND_SOC_DAIFMT_AC97:
Markus Pargmann07a28db2014-03-15 13:44:10 +0100856 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
Markus Pargmann2b0db992014-03-15 13:44:09 +0100857 break;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800858 default:
859 return -EINVAL;
860 }
Markus Pargmann2b0db992014-03-15 13:44:09 +0100861 scr |= ssi_private->i2s_mode;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800862
863 /* DAI clock inversion */
864 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
865 case SND_SOC_DAIFMT_NB_NF:
866 /* Nothing to do for both normal cases */
867 break;
868 case SND_SOC_DAIFMT_IB_NF:
869 /* Invert bit clock */
870 strcr ^= CCSR_SSI_STCR_TSCKP;
871 break;
872 case SND_SOC_DAIFMT_NB_IF:
873 /* Invert frame clock */
874 strcr ^= CCSR_SSI_STCR_TFSI;
875 break;
876 case SND_SOC_DAIFMT_IB_IF:
877 /* Invert both clocks */
878 strcr ^= CCSR_SSI_STCR_TSCKP;
879 strcr ^= CCSR_SSI_STCR_TFSI;
880 break;
881 default:
882 return -EINVAL;
883 }
884
885 /* DAI clock master masks */
886 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
887 case SND_SOC_DAIFMT_CBS_CFS:
888 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
889 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
890 break;
891 case SND_SOC_DAIFMT_CBM_CFM:
892 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
893 break;
Fabio Falzoicf4f7fc2014-08-04 17:08:07 +0200894 case SND_SOC_DAIFMT_CBM_CFS:
895 strcr &= ~CCSR_SSI_STCR_TXDIR;
896 strcr |= CCSR_SSI_STCR_TFDIR;
897 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
898 break;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800899 default:
900 return -EINVAL;
901 }
902
903 stcr |= strcr;
904 srcr |= strcr;
905
906 if (ssi_private->cpu_dai_drv.symmetric_rates) {
907 /* Need to clear RXDIR when using SYNC mode */
908 srcr &= ~CCSR_SSI_SRCR_RXDIR;
909 scr |= CCSR_SSI_SCR_SYN;
910 }
911
Markus Pargmann43248122014-05-27 10:24:25 +0200912 regmap_write(regs, CCSR_SSI_STCR, stcr);
913 regmap_write(regs, CCSR_SSI_SRCR, srcr);
914 regmap_write(regs, CCSR_SSI_SCR, scr);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800915
Markus Pargmann2b0db992014-03-15 13:44:09 +0100916 /*
917 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
918 * use FIFO 1. We program the transmit water to signal a DMA transfer
919 * if there are only two (or fewer) elements left in the FIFO. Two
920 * elements equals one frame (left channel, right channel). This value,
921 * however, depends on the depth of the transmit buffer.
922 *
923 * We set the watermark on the same level as the DMA burstsize. For
924 * fiq it is probably better to use the biggest possible watermark
925 * size.
926 */
927 if (ssi_private->use_dma)
928 wm = ssi_private->fifo_depth - 2;
929 else
930 wm = ssi_private->fifo_depth;
931
Markus Pargmann43248122014-05-27 10:24:25 +0200932 regmap_write(regs, CCSR_SSI_SFCSR,
933 CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
934 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
Markus Pargmann2b0db992014-03-15 13:44:09 +0100935
936 if (ssi_private->use_dual_fifo) {
Markus Pargmann43248122014-05-27 10:24:25 +0200937 regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
Markus Pargmann2b0db992014-03-15 13:44:09 +0100938 CCSR_SSI_SRCR_RFEN1);
Markus Pargmann43248122014-05-27 10:24:25 +0200939 regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
Markus Pargmann2b0db992014-03-15 13:44:09 +0100940 CCSR_SSI_STCR_TFEN1);
Markus Pargmann43248122014-05-27 10:24:25 +0200941 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
Markus Pargmann2b0db992014-03-15 13:44:09 +0100942 CCSR_SSI_SCR_TCH_EN);
943 }
944
945 if (fmt & SND_SOC_DAIFMT_AC97)
946 fsl_ssi_setup_ac97(ssi_private);
947
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800948 return 0;
Markus Pargmann85e59af22014-05-27 10:24:19 +0200949
950}
951
952/**
953 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
954 */
955static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
956{
957 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
958
Michael Trimarchi85151462014-09-18 20:38:09 +0200959 return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800960}
961
962/**
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800963 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
964 *
965 * Note: This function can be only called when using SSI as DAI master
966 */
967static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
968 u32 rx_mask, int slots, int slot_width)
969{
970 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
Markus Pargmann43248122014-05-27 10:24:25 +0200971 struct regmap *regs = ssi_private->regs;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800972 u32 val;
973
974 /* The slot number should be >= 2 if using Network mode or I2S mode */
Markus Pargmann43248122014-05-27 10:24:25 +0200975 regmap_read(regs, CCSR_SSI_SCR, &val);
976 val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800977 if (val && slots < 2) {
978 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
979 return -EINVAL;
980 }
981
Markus Pargmann43248122014-05-27 10:24:25 +0200982 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800983 CCSR_SSI_SxCCR_DC(slots));
Markus Pargmann43248122014-05-27 10:24:25 +0200984 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800985 CCSR_SSI_SxCCR_DC(slots));
986
987 /* The register SxMSKs needs SSI to provide essential clock due to
988 * hardware design. So we here temporarily enable SSI to set them.
989 */
Markus Pargmann43248122014-05-27 10:24:25 +0200990 regmap_read(regs, CCSR_SSI_SCR, &val);
991 val &= CCSR_SSI_SCR_SSIEN;
992 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
993 CCSR_SSI_SCR_SSIEN);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800994
Markus Pargmann43248122014-05-27 10:24:25 +0200995 regmap_write(regs, CCSR_SSI_STMSK, tx_mask);
996 regmap_write(regs, CCSR_SSI_SRMSK, rx_mask);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800997
Markus Pargmann43248122014-05-27 10:24:25 +0200998 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
Nicolin Chenaafa85e2013-12-12 18:44:45 +0800999
1000 return 0;
1001}
1002
1003/**
Timur Tabi17467f22008-01-11 18:15:26 +01001004 * fsl_ssi_trigger: start and stop the DMA transfer.
1005 *
1006 * This function is called by ALSA to start, stop, pause, and resume the DMA
1007 * transfer of data.
1008 *
1009 * The DMA channel is in external master start and pause mode, which
1010 * means the SSI completely controls the flow of data.
1011 */
Mark Browndee89c42008-11-18 22:11:38 +00001012static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1013 struct snd_soc_dai *dai)
Timur Tabi17467f22008-01-11 18:15:26 +01001014{
1015 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001016 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Markus Pargmann43248122014-05-27 10:24:25 +02001017 struct regmap *regs = ssi_private->regs;
Michael Grzeschik9b443e32013-08-19 17:06:00 +02001018
Timur Tabi17467f22008-01-11 18:15:26 +01001019 switch (cmd) {
1020 case SNDRV_PCM_TRIGGER_START:
Fabio Estevamb20e53a2014-05-23 02:38:56 -03001021 case SNDRV_PCM_TRIGGER_RESUME:
Timur Tabi17467f22008-01-11 18:15:26 +01001022 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Timur Tabia4d11fe2009-03-25 18:20:37 -05001023 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +01001024 fsl_ssi_tx_config(ssi_private, true);
Timur Tabia4d11fe2009-03-25 18:20:37 -05001025 else
Markus Pargmann6de83872013-12-20 14:11:34 +01001026 fsl_ssi_rx_config(ssi_private, true);
Timur Tabi17467f22008-01-11 18:15:26 +01001027 break;
1028
1029 case SNDRV_PCM_TRIGGER_STOP:
Fabio Estevamb20e53a2014-05-23 02:38:56 -03001030 case SNDRV_PCM_TRIGGER_SUSPEND:
Timur Tabi17467f22008-01-11 18:15:26 +01001031 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1032 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann6de83872013-12-20 14:11:34 +01001033 fsl_ssi_tx_config(ssi_private, false);
Timur Tabi17467f22008-01-11 18:15:26 +01001034 else
Markus Pargmann6de83872013-12-20 14:11:34 +01001035 fsl_ssi_rx_config(ssi_private, false);
Timur Tabi17467f22008-01-11 18:15:26 +01001036 break;
1037
1038 default:
1039 return -EINVAL;
1040 }
1041
Markus Pargmann171d6832014-04-28 12:54:48 +02001042 if (fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmanna5a7ee72013-12-20 14:11:35 +01001043 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Markus Pargmann43248122014-05-27 10:24:25 +02001044 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
Markus Pargmanna5a7ee72013-12-20 14:11:35 +01001045 else
Markus Pargmann43248122014-05-27 10:24:25 +02001046 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
Markus Pargmanna5a7ee72013-12-20 14:11:35 +01001047 }
Michael Grzeschik9b443e32013-08-19 17:06:00 +02001048
Timur Tabi17467f22008-01-11 18:15:26 +01001049 return 0;
1050}
1051
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +02001052static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1053{
1054 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
1055
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001056 if (ssi_private->soc->imx && ssi_private->use_dma) {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +02001057 dai->playback_dma_data = &ssi_private->dma_params_tx;
1058 dai->capture_dma_data = &ssi_private->dma_params_rx;
1059 }
1060
1061 return 0;
1062}
1063
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001064static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +08001065 .startup = fsl_ssi_startup,
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001066 .shutdown = fsl_ssi_shutdown,
Eric Miao6335d052009-03-03 09:41:00 +08001067 .hw_params = fsl_ssi_hw_params,
Markus Pargmannd429d8e2014-05-27 10:24:23 +02001068 .hw_free = fsl_ssi_hw_free,
Nicolin Chenaafa85e2013-12-12 18:44:45 +08001069 .set_fmt = fsl_ssi_set_dai_fmt,
1070 .set_sysclk = fsl_ssi_set_dai_sysclk,
1071 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
Eric Miao6335d052009-03-03 09:41:00 +08001072 .trigger = fsl_ssi_trigger,
Eric Miao6335d052009-03-03 09:41:00 +08001073};
1074
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001075/* Template for the CPU dai driver structure */
1076static struct snd_soc_dai_driver fsl_ssi_dai_template = {
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +02001077 .probe = fsl_ssi_dai_probe,
Timur Tabi17467f22008-01-11 18:15:26 +01001078 .playback = {
Nicolin Chene3655002014-07-30 11:10:29 +08001079 .stream_name = "CPU-Playback",
Nicolin Chen2924a992013-12-02 23:29:03 +08001080 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +01001081 .channels_max = 2,
1082 .rates = FSLSSI_I2S_RATES,
1083 .formats = FSLSSI_I2S_FORMATS,
1084 },
1085 .capture = {
Nicolin Chene3655002014-07-30 11:10:29 +08001086 .stream_name = "CPU-Capture",
Nicolin Chen2924a992013-12-02 23:29:03 +08001087 .channels_min = 1,
Timur Tabi17467f22008-01-11 18:15:26 +01001088 .channels_max = 2,
1089 .rates = FSLSSI_I2S_RATES,
1090 .formats = FSLSSI_I2S_FORMATS,
1091 },
Eric Miao6335d052009-03-03 09:41:00 +08001092 .ops = &fsl_ssi_dai_ops,
Timur Tabi17467f22008-01-11 18:15:26 +01001093};
1094
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001095static const struct snd_soc_component_driver fsl_ssi_component = {
1096 .name = "fsl-ssi",
1097};
1098
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001099static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1100 .ac97_control = 1,
1101 .playback = {
1102 .stream_name = "AC97 Playback",
1103 .channels_min = 2,
1104 .channels_max = 2,
1105 .rates = SNDRV_PCM_RATE_8000_48000,
1106 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1107 },
1108 .capture = {
1109 .stream_name = "AC97 Capture",
1110 .channels_min = 2,
1111 .channels_max = 2,
1112 .rates = SNDRV_PCM_RATE_48000,
1113 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1114 },
Markus Pargmanna5a7ee72013-12-20 14:11:35 +01001115 .ops = &fsl_ssi_dai_ops,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001116};
1117
1118
1119static struct fsl_ssi_private *fsl_ac97_data;
1120
Sachin Kamata851a2b2013-09-13 15:22:17 +05301121static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001122 unsigned short val)
1123{
Markus Pargmann43248122014-05-27 10:24:25 +02001124 struct regmap *regs = fsl_ac97_data->regs;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001125 unsigned int lreg;
1126 unsigned int lval;
1127
1128 if (reg > 0x7f)
1129 return;
1130
1131
1132 lreg = reg << 12;
Markus Pargmann43248122014-05-27 10:24:25 +02001133 regmap_write(regs, CCSR_SSI_SACADD, lreg);
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001134
1135 lval = val << 4;
Markus Pargmann43248122014-05-27 10:24:25 +02001136 regmap_write(regs, CCSR_SSI_SACDAT, lval);
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001137
Markus Pargmann43248122014-05-27 10:24:25 +02001138 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001139 CCSR_SSI_SACNT_WR);
1140 udelay(100);
1141}
1142
Sachin Kamata851a2b2013-09-13 15:22:17 +05301143static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001144 unsigned short reg)
1145{
Markus Pargmann43248122014-05-27 10:24:25 +02001146 struct regmap *regs = fsl_ac97_data->regs;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001147
1148 unsigned short val = -1;
Markus Pargmann43248122014-05-27 10:24:25 +02001149 u32 reg_val;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001150 unsigned int lreg;
1151
1152 lreg = (reg & 0x7f) << 12;
Markus Pargmann43248122014-05-27 10:24:25 +02001153 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1154 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001155 CCSR_SSI_SACNT_RD);
1156
1157 udelay(100);
1158
Markus Pargmann43248122014-05-27 10:24:25 +02001159 regmap_read(regs, CCSR_SSI_SACDAT, &reg_val);
1160 val = (reg_val >> 4) & 0xffff;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001161
1162 return val;
1163}
1164
1165static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1166 .read = fsl_ssi_ac97_read,
1167 .write = fsl_ssi_ac97_write,
1168};
1169
Timur Tabi17467f22008-01-11 18:15:26 +01001170/**
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001171 * Make every character in a string lower-case
Timur Tabi17467f22008-01-11 18:15:26 +01001172 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001173static void make_lowercase(char *s)
Timur Tabi17467f22008-01-11 18:15:26 +01001174{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001175 char *p = s;
1176 char c;
1177
1178 while ((c = *p)) {
1179 if ((c >= 'A') && (c <= 'Z'))
1180 *p = c + ('a' - 'A');
1181 p++;
1182 }
1183}
1184
Markus Pargmann49da09e2014-04-28 12:54:45 +02001185static int fsl_ssi_imx_probe(struct platform_device *pdev,
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001186 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001187{
1188 struct device_node *np = pdev->dev.of_node;
Markus Pargmanned0f16042014-04-28 12:54:46 +02001189 u32 dmas[4];
Markus Pargmann49da09e2014-04-28 12:54:45 +02001190 int ret;
1191
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001192 if (ssi_private->has_ipg_clk_name)
1193 ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
1194 else
1195 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001196 if (IS_ERR(ssi_private->clk)) {
1197 ret = PTR_ERR(ssi_private->clk);
1198 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1199 return ret;
1200 }
1201
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001202 if (!ssi_private->has_ipg_clk_name) {
1203 ret = clk_prepare_enable(ssi_private->clk);
1204 if (ret) {
1205 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1206 return ret;
1207 }
Markus Pargmann49da09e2014-04-28 12:54:45 +02001208 }
1209
1210 /* For those SLAVE implementations, we ingore non-baudclk cases
1211 * and, instead, abandon MASTER mode that needs baud clock.
1212 */
1213 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1214 if (IS_ERR(ssi_private->baudclk))
1215 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1216 PTR_ERR(ssi_private->baudclk));
Markus Pargmann49da09e2014-04-28 12:54:45 +02001217
1218 /*
1219 * We have burstsize be "fifo_depth - 2" to match the SSI
1220 * watermark setting in fsl_ssi_startup().
1221 */
1222 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1223 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
Markus Pargmann43248122014-05-27 10:24:25 +02001224 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
1225 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
Markus Pargmann49da09e2014-04-28 12:54:45 +02001226
Markus Pargmanned0f16042014-04-28 12:54:46 +02001227 ret = !of_property_read_u32_array(np, "dmas", dmas, 4);
1228 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
Markus Pargmann49da09e2014-04-28 12:54:45 +02001229 ssi_private->use_dual_fifo = true;
1230 /* When using dual fifo mode, we need to keep watermark
1231 * as even numbers due to dma script limitation.
1232 */
1233 ssi_private->dma_params_tx.maxburst &= ~0x1;
1234 ssi_private->dma_params_rx.maxburst &= ~0x1;
1235 }
1236
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001237 if (!ssi_private->use_dma) {
1238
1239 /*
1240 * Some boards use an incompatible codec. To get it
1241 * working, we are using imx-fiq-pcm-audio, that
1242 * can handle those codecs. DMA is not possible in this
1243 * situation.
1244 */
1245
1246 ssi_private->fiq_params.irq = ssi_private->irq;
1247 ssi_private->fiq_params.base = iomem;
1248 ssi_private->fiq_params.dma_params_rx =
1249 &ssi_private->dma_params_rx;
1250 ssi_private->fiq_params.dma_params_tx =
1251 &ssi_private->dma_params_tx;
1252
1253 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1254 if (ret)
1255 goto error_pcm;
1256 } else {
1257 ret = imx_pcm_dma_init(pdev);
1258 if (ret)
1259 goto error_pcm;
1260 }
1261
Markus Pargmann49da09e2014-04-28 12:54:45 +02001262 return 0;
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001263
1264error_pcm:
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001265
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001266 if (!ssi_private->has_ipg_clk_name)
1267 clk_disable_unprepare(ssi_private->clk);
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001268 return ret;
Markus Pargmann49da09e2014-04-28 12:54:45 +02001269}
1270
1271static void fsl_ssi_imx_clean(struct platform_device *pdev,
1272 struct fsl_ssi_private *ssi_private)
1273{
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001274 if (!ssi_private->use_dma)
1275 imx_pcm_fiq_exit(pdev);
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001276 if (!ssi_private->has_ipg_clk_name)
1277 clk_disable_unprepare(ssi_private->clk);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001278}
1279
Bill Pembertona0a3d512012-12-07 09:26:16 -05001280static int fsl_ssi_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001281{
Timur Tabi17467f22008-01-11 18:15:26 +01001282 struct fsl_ssi_private *ssi_private;
1283 int ret = 0;
Timur Tabi38fec722010-08-19 15:26:58 -05001284 struct device_node *np = pdev->dev.of_node;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001285 const struct of_device_id *of_id;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286 const char *p, *sprop;
Timur Tabi8e9d8692010-08-06 12:16:12 -05001287 const uint32_t *iprop;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001288 struct resource res;
Markus Pargmann43248122014-05-27 10:24:25 +02001289 void __iomem *iomem;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001290 char name[64];
Timur Tabi17467f22008-01-11 18:15:26 +01001291
Timur Tabiff713342010-08-04 17:51:08 -05001292 /* SSIs that are not connected on the board should have a
1293 * status = "disabled"
1294 * property in their device tree nodes.
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001295 */
Timur Tabiff713342010-08-04 17:51:08 -05001296 if (!of_device_is_available(np))
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001297 return -ENODEV;
1298
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001299 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001300 if (!of_id || !of_id->data)
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001301 return -EINVAL;
Markus Pargmannc1953bf2013-12-20 14:11:30 +01001302
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001303 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1304 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001305 if (!ssi_private) {
Timur Tabi38fec722010-08-19 15:26:58 -05001306 dev_err(&pdev->dev, "could not allocate DAI object\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001307 return -ENOMEM;
1308 }
Timur Tabi17467f22008-01-11 18:15:26 +01001309
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001310 ssi_private->soc = of_id->data;
1311
Markus Pargmann85e59af22014-05-27 10:24:19 +02001312 sprop = of_get_property(np, "fsl,mode", NULL);
1313 if (sprop) {
1314 if (!strcmp(sprop, "ac97-slave"))
1315 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
Markus Pargmann85e59af22014-05-27 10:24:19 +02001316 }
1317
Markus Pargmannde623ec2013-07-27 13:31:53 +02001318 ssi_private->use_dma = !of_property_read_bool(np,
1319 "fsl,fiq-stream-filter");
1320
Markus Pargmann85e59af22014-05-27 10:24:19 +02001321 if (fsl_ssi_is_ac97(ssi_private)) {
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001322 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1323 sizeof(fsl_ssi_ac97_dai));
1324
1325 fsl_ac97_data = ssi_private;
Markus Pargmanncd7f0292013-08-19 17:05:58 +02001326
1327 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1328 } else {
1329 /* Initialize this copy of the CPU DAI driver structure */
1330 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1331 sizeof(fsl_ssi_dai_template));
1332 }
Markus Pargmann2a1d1022014-04-28 12:54:44 +02001333 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001334
1335 /* Get the addresses and IRQ */
1336 ret = of_address_to_resource(np, 0, &res);
1337 if (ret) {
Timur Tabi38fec722010-08-19 15:26:58 -05001338 dev_err(&pdev->dev, "could not determine device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001339 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001340 }
Markus Pargmann43248122014-05-27 10:24:25 +02001341 ssi_private->ssi_phys = res.start;
1342
1343 iomem = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
1344 if (!iomem) {
Timur Tabi147dfe92011-06-08 15:02:55 -05001345 dev_err(&pdev->dev, "could not map device resources\n");
Fabio Estevamb0a47472013-07-17 02:00:38 -03001346 return -ENOMEM;
Timur Tabi147dfe92011-06-08 15:02:55 -05001347 }
Markus Pargmann43248122014-05-27 10:24:25 +02001348
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001349 ret = of_property_match_string(np, "clock-names", "ipg");
1350 if (ret < 0) {
1351 ssi_private->has_ipg_clk_name = false;
1352 ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
Markus Pargmann43248122014-05-27 10:24:25 +02001353 &fsl_ssi_regconfig);
Shengjiu Wangf4a43ca2014-09-16 10:13:16 +08001354 } else {
1355 ssi_private->has_ipg_clk_name = true;
1356 ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
1357 "ipg", iomem, &fsl_ssi_regconfig);
1358 }
Markus Pargmann43248122014-05-27 10:24:25 +02001359 if (IS_ERR(ssi_private->regs)) {
1360 dev_err(&pdev->dev, "Failed to init register map\n");
1361 return PTR_ERR(ssi_private->regs);
1362 }
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001363
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001364 ssi_private->irq = irq_of_parse_and_map(np, 0);
Chen Gangd60336e2013-09-23 11:36:21 +08001365 if (!ssi_private->irq) {
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001366 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
Fabio Estevamb0a47472013-07-17 02:00:38 -03001367 return -ENXIO;
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001368 }
1369
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001370 /* Are the RX and the TX clocks locked? */
Nicolin Chen07a94832013-12-03 18:38:07 +08001371 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001372 ssi_private->cpu_dai_drv.symmetric_rates = 1;
Nicolin Chen07a94832013-12-03 18:38:07 +08001373 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1374 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1375 }
Timur Tabi17467f22008-01-11 18:15:26 +01001376
Timur Tabi8e9d8692010-08-06 12:16:12 -05001377 /* Determine the FIFO depth. */
1378 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1379 if (iprop)
Timur Tabi147dfe92011-06-08 15:02:55 -05001380 ssi_private->fifo_depth = be32_to_cpup(iprop);
Timur Tabi8e9d8692010-08-06 12:16:12 -05001381 else
1382 /* Older 8610 DTs didn't have the fifo-depth property */
1383 ssi_private->fifo_depth = 8;
1384
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001385 dev_set_drvdata(&pdev->dev, ssi_private);
1386
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001387 if (ssi_private->soc->imx) {
Markus Pargmann43248122014-05-27 10:24:25 +02001388 ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001389 if (ret)
Fabio Estevamb0a47472013-07-17 02:00:38 -03001390 goto error_irqmap;
Markus Pargmann0888efd2013-12-20 14:11:31 +01001391 }
1392
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001393 ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1394 &ssi_private->cpu_dai_drv, 1);
1395 if (ret) {
1396 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1397 goto error_asoc_register;
1398 }
1399
Markus Pargmann0888efd2013-12-20 14:11:31 +01001400 if (ssi_private->use_dma) {
Michael Grzeschikf0377082013-08-19 17:06:01 +02001401 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
Markus Pargmann171d6832014-04-28 12:54:48 +02001402 fsl_ssi_isr, 0, dev_name(&pdev->dev),
Michael Grzeschikf0377082013-08-19 17:06:01 +02001403 ssi_private);
1404 if (ret < 0) {
1405 dev_err(&pdev->dev, "could not claim irq %u\n",
1406 ssi_private->irq);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001407 goto error_irq;
Michael Grzeschikf0377082013-08-19 17:06:01 +02001408 }
Shawn Guo09ce1112012-03-16 16:56:43 +08001409 }
1410
Markus Pargmannf138e622014-04-28 12:54:43 +02001411 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001412 if (ret)
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001413 goto error_asoc_register;
Shawn Guo09ce1112012-03-16 16:56:43 +08001414
1415 /*
1416 * If codec-handle property is missing from SSI node, we assume
1417 * that the machine driver uses new binding which does not require
1418 * SSI driver to trigger machine driver's probe.
1419 */
Markus Pargmann171d6832014-04-28 12:54:48 +02001420 if (!of_get_property(np, "codec-handle", NULL))
Shawn Guo09ce1112012-03-16 16:56:43 +08001421 goto done;
Shawn Guo09ce1112012-03-16 16:56:43 +08001422
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 /* Trigger the machine driver's probe function. The platform driver
Shawn Guo2b81ec62012-03-09 00:59:46 +08001424 * name of the machine driver is taken from /compatible property of the
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 * device tree. We also pass the address of the CPU DAI driver
1426 * structure.
1427 */
Shawn Guo2b81ec62012-03-09 00:59:46 +08001428 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1429 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001430 p = strrchr(sprop, ',');
1431 if (p)
1432 sprop = p + 1;
1433 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1434 make_lowercase(name);
1435
1436 ssi_private->pdev =
Timur Tabi38fec722010-08-19 15:26:58 -05001437 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001438 if (IS_ERR(ssi_private->pdev)) {
1439 ret = PTR_ERR(ssi_private->pdev);
Timur Tabi38fec722010-08-19 15:26:58 -05001440 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001441 goto error_sound_card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001442 }
1443
Shawn Guo09ce1112012-03-16 16:56:43 +08001444done:
Markus Pargmann85e59af22014-05-27 10:24:19 +02001445 if (ssi_private->dai_fmt)
Michael Trimarchi85151462014-09-18 20:38:09 +02001446 _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
1447 ssi_private->dai_fmt);
Markus Pargmann85e59af22014-05-27 10:24:19 +02001448
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001449 return 0;
Timur Tabi87a06322010-08-03 17:55:28 -05001450
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001451error_sound_card:
Markus Pargmannf138e622014-04-28 12:54:43 +02001452 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001453
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001454error_irq:
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001455 snd_soc_unregister_component(&pdev->dev);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001456
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001457error_asoc_register:
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001458 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001459 fsl_ssi_imx_clean(pdev, ssi_private);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001460
1461error_irqmap:
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001462 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001463 irq_dispose_mapping(ssi_private->irq);
Timur Tabi1fab6ca2011-08-16 18:47:45 -04001464
Timur Tabi87a06322010-08-03 17:55:28 -05001465 return ret;
Timur Tabi17467f22008-01-11 18:15:26 +01001466}
Timur Tabi17467f22008-01-11 18:15:26 +01001467
Timur Tabi38fec722010-08-19 15:26:58 -05001468static int fsl_ssi_remove(struct platform_device *pdev)
Timur Tabi17467f22008-01-11 18:15:26 +01001469{
Timur Tabi38fec722010-08-19 15:26:58 -05001470 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
Timur Tabi17467f22008-01-11 18:15:26 +01001471
Markus Pargmannf138e622014-04-28 12:54:43 +02001472 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
Markus Pargmann9368acc2013-12-20 14:11:29 +01001473
Markus Pargmann171d6832014-04-28 12:54:48 +02001474 if (ssi_private->pdev)
Shawn Guo09ce1112012-03-16 16:56:43 +08001475 platform_device_unregister(ssi_private->pdev);
Kuninori Morimoto3580aa12013-03-21 03:32:04 -07001476 snd_soc_unregister_component(&pdev->dev);
Markus Pargmann49da09e2014-04-28 12:54:45 +02001477
Sascha Hauerfcdbade2014-05-27 10:24:18 +02001478 if (ssi_private->soc->imx)
Markus Pargmann49da09e2014-04-28 12:54:45 +02001479 fsl_ssi_imx_clean(pdev, ssi_private);
1480
Markus Pargmann4d9b7922014-04-28 12:54:47 +02001481 if (ssi_private->use_dma)
Markus Pargmann2841be92013-12-20 14:11:28 +01001482 irq_dispose_mapping(ssi_private->irq);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001483
1484 return 0;
Timur Tabi17467f22008-01-11 18:15:26 +01001485}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001486
Grant Likelyf07eb222011-02-22 21:05:04 -07001487static struct platform_driver fsl_ssi_driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488 .driver = {
1489 .name = "fsl-ssi-dai",
1490 .owner = THIS_MODULE,
1491 .of_match_table = fsl_ssi_ids,
1492 },
1493 .probe = fsl_ssi_probe,
1494 .remove = fsl_ssi_remove,
1495};
Timur Tabi17467f22008-01-11 18:15:26 +01001496
Axel Linba0a7e02011-11-25 10:10:55 +08001497module_platform_driver(fsl_ssi_driver);
Timur Tabia454dad2009-03-05 17:23:37 -06001498
Fabio Estevamf3142802013-07-20 16:16:01 -03001499MODULE_ALIAS("platform:fsl-ssi-dai");
Timur Tabi17467f22008-01-11 18:15:26 +01001500MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1501MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502MODULE_LICENSE("GPL v2");