blob: f58bcd85c07fbd8b302c0c3bcd97fd3cf6ed4d0c [file] [log] [blame]
Sascha Hauer83802222009-11-25 16:41:04 +01001/*
2 * imx-ssi.c -- ALSA Soc Audio Layer
3 *
4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
5 *
6 * This code is based on code copyrighted by Freescale,
7 * Liam Girdwood, Javier Martin and probably others.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019 * was developed with always sends GPIO status data in slot 12 which
Sascha Hauer83802222009-11-25 16:41:04 +010020 * 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.
Uwe Kleine-König25d1fbf2010-07-13 11:27:58 +020026 * Reading and writing AC97 registers is another challenge. The core
Sascha Hauer83802222009-11-25 16:41:04 +010027 * 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.
Shawn Guoa23dc692012-03-16 16:56:38 +080031 *
Sascha Hauer83802222009-11-25 16:41:04 +010032 */
33
34#include <linux/clk.h>
35#include <linux/delay.h>
36#include <linux/device.h>
37#include <linux/dma-mapping.h>
38#include <linux/init.h>
39#include <linux/interrupt.h>
40#include <linux/module.h>
41#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Sascha Hauer83802222009-11-25 16:41:04 +010043
44#include <sound/core.h>
45#include <sound/initval.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
48#include <sound/soc.h>
49
Arnd Bergmann82906b12012-08-24 15:14:29 +020050#include <linux/platform_data/asoc-imx-ssi.h>
Sascha Hauer83802222009-11-25 16:41:04 +010051
52#include "imx-ssi.h"
53
54#define SSI_SACNT_DEFAULT (SSI_SACNT_AC97EN | SSI_SACNT_FV)
55
56/*
57 * SSI Network Mode or TDM slots configuration.
58 * Should only be called when port is inactive (i.e. SSIEN = 0).
59 */
60static int imx_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
61 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
62{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000063 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(cpu_dai);
Sascha Hauer83802222009-11-25 16:41:04 +010064 u32 sccr;
65
66 sccr = readl(ssi->base + SSI_STCCR);
67 sccr &= ~SSI_STCCR_DC_MASK;
68 sccr |= SSI_STCCR_DC(slots - 1);
69 writel(sccr, ssi->base + SSI_STCCR);
70
71 sccr = readl(ssi->base + SSI_SRCCR);
72 sccr &= ~SSI_STCCR_DC_MASK;
73 sccr |= SSI_STCCR_DC(slots - 1);
74 writel(sccr, ssi->base + SSI_SRCCR);
75
76 writel(tx_mask, ssi->base + SSI_STMSK);
77 writel(rx_mask, ssi->base + SSI_SRMSK);
78
79 return 0;
80}
81
82/*
83 * SSI DAI format configuration.
84 * Should only be called when port is inactive (i.e. SSIEN = 0).
Sascha Hauer83802222009-11-25 16:41:04 +010085 */
86static int imx_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
87{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000088 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(cpu_dai);
Sascha Hauer83802222009-11-25 16:41:04 +010089 u32 strcr = 0, scr;
90
91 scr = readl(ssi->base + SSI_SCR) & ~(SSI_SCR_SYN | SSI_SCR_NET);
92
93 /* DAI mode */
94 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
95 case SND_SOC_DAIFMT_I2S:
96 /* data on rising edge of bclk, frame low 1clk before data */
97 strcr |= SSI_STCR_TFSI | SSI_STCR_TEFS | SSI_STCR_TXBIT0;
98 scr |= SSI_SCR_NET;
Eric Bénard0e796122010-05-27 10:58:54 +020099 if (ssi->flags & IMX_SSI_USE_I2S_SLAVE) {
100 scr &= ~SSI_I2S_MODE_MASK;
101 scr |= SSI_SCR_I2S_MODE_SLAVE;
102 }
Sascha Hauer83802222009-11-25 16:41:04 +0100103 break;
104 case SND_SOC_DAIFMT_LEFT_J:
105 /* data on rising edge of bclk, frame high with data */
106 strcr |= SSI_STCR_TXBIT0;
107 break;
108 case SND_SOC_DAIFMT_DSP_B:
109 /* data on rising edge of bclk, frame high with data */
Javier Martin0a934212011-03-01 15:02:06 +0100110 strcr |= SSI_STCR_TFSL | SSI_STCR_TXBIT0;
Sascha Hauer83802222009-11-25 16:41:04 +0100111 break;
112 case SND_SOC_DAIFMT_DSP_A:
113 /* data on rising edge of bclk, frame high 1clk before data */
Javier Martin5ed80a72012-02-23 15:43:18 +0100114 strcr |= SSI_STCR_TFSL | SSI_STCR_TXBIT0 | SSI_STCR_TEFS;
Sascha Hauer83802222009-11-25 16:41:04 +0100115 break;
116 }
117
118 /* DAI clock inversion */
119 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
120 case SND_SOC_DAIFMT_IB_IF:
121 strcr |= SSI_STCR_TFSI;
122 strcr &= ~SSI_STCR_TSCKP;
123 break;
124 case SND_SOC_DAIFMT_IB_NF:
125 strcr &= ~(SSI_STCR_TSCKP | SSI_STCR_TFSI);
126 break;
127 case SND_SOC_DAIFMT_NB_IF:
128 strcr |= SSI_STCR_TFSI | SSI_STCR_TSCKP;
129 break;
130 case SND_SOC_DAIFMT_NB_NF:
131 strcr &= ~SSI_STCR_TFSI;
132 strcr |= SSI_STCR_TSCKP;
133 break;
134 }
135
136 /* DAI clock master masks */
137 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Mark Brownd08a68b2010-01-11 16:56:19 +0000138 case SND_SOC_DAIFMT_CBM_CFM:
Sascha Hauer83802222009-11-25 16:41:04 +0100139 break;
Mark Brownd08a68b2010-01-11 16:56:19 +0000140 default:
141 /* Master mode not implemented, needs handling of clocks. */
142 return -EINVAL;
Sascha Hauer83802222009-11-25 16:41:04 +0100143 }
144
145 strcr |= SSI_STCR_TFEN0;
146
Eric Bénard0e796122010-05-27 10:58:54 +0200147 if (ssi->flags & IMX_SSI_NET)
148 scr |= SSI_SCR_NET;
149 if (ssi->flags & IMX_SSI_SYN)
150 scr |= SSI_SCR_SYN;
151
Sascha Hauer83802222009-11-25 16:41:04 +0100152 writel(strcr, ssi->base + SSI_STCR);
153 writel(strcr, ssi->base + SSI_SRCR);
154 writel(scr, ssi->base + SSI_SCR);
155
156 return 0;
157}
158
159/*
160 * SSI system clock configuration.
161 * Should only be called when port is inactive (i.e. SSIEN = 0).
162 */
163static int imx_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
164 int clk_id, unsigned int freq, int dir)
165{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(cpu_dai);
Sascha Hauer83802222009-11-25 16:41:04 +0100167 u32 scr;
168
169 scr = readl(ssi->base + SSI_SCR);
170
171 switch (clk_id) {
172 case IMX_SSP_SYS_CLK:
173 if (dir == SND_SOC_CLOCK_OUT)
174 scr |= SSI_SCR_SYS_CLK_EN;
175 else
176 scr &= ~SSI_SCR_SYS_CLK_EN;
177 break;
178 default:
179 return -EINVAL;
180 }
181
182 writel(scr, ssi->base + SSI_SCR);
183
184 return 0;
185}
186
187/*
188 * SSI Clock dividers
189 * Should only be called when port is inactive (i.e. SSIEN = 0).
190 */
191static int imx_ssi_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
192 int div_id, int div)
193{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000194 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(cpu_dai);
Sascha Hauer83802222009-11-25 16:41:04 +0100195 u32 stccr, srccr;
196
197 stccr = readl(ssi->base + SSI_STCCR);
198 srccr = readl(ssi->base + SSI_SRCCR);
199
200 switch (div_id) {
201 case IMX_SSI_TX_DIV_2:
202 stccr &= ~SSI_STCCR_DIV2;
203 stccr |= div;
204 break;
205 case IMX_SSI_TX_DIV_PSR:
206 stccr &= ~SSI_STCCR_PSR;
207 stccr |= div;
208 break;
209 case IMX_SSI_TX_DIV_PM:
210 stccr &= ~0xff;
211 stccr |= SSI_STCCR_PM(div);
212 break;
213 case IMX_SSI_RX_DIV_2:
214 stccr &= ~SSI_STCCR_DIV2;
215 stccr |= div;
216 break;
217 case IMX_SSI_RX_DIV_PSR:
218 stccr &= ~SSI_STCCR_PSR;
219 stccr |= div;
220 break;
221 case IMX_SSI_RX_DIV_PM:
222 stccr &= ~0xff;
223 stccr |= SSI_STCCR_PM(div);
224 break;
225 default:
226 return -EINVAL;
227 }
228
229 writel(stccr, ssi->base + SSI_STCCR);
230 writel(srccr, ssi->base + SSI_SRCCR);
231
232 return 0;
233}
234
235/*
236 * Should only be called when port is inactive (i.e. SSIEN = 0),
237 * although can be called multiple times by upper layers.
238 */
239static int imx_ssi_hw_params(struct snd_pcm_substream *substream,
240 struct snd_pcm_hw_params *params,
241 struct snd_soc_dai *cpu_dai)
242{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000243 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(cpu_dai);
Sascha Hauer83802222009-11-25 16:41:04 +0100244 u32 reg, sccr;
245
246 /* Tx/Rx config */
Lars-Peter Clausen91a38542012-02-22 10:49:05 +0100247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Sascha Hauer83802222009-11-25 16:41:04 +0100248 reg = SSI_STCCR;
Lars-Peter Clausen91a38542012-02-22 10:49:05 +0100249 else
Sascha Hauer83802222009-11-25 16:41:04 +0100250 reg = SSI_SRCCR;
Sascha Hauer83802222009-11-25 16:41:04 +0100251
Sascha Hauer70bf0432010-08-23 08:54:02 +0200252 if (ssi->flags & IMX_SSI_SYN)
253 reg = SSI_STCCR;
254
Sascha Hauer83802222009-11-25 16:41:04 +0100255 sccr = readl(ssi->base + reg) & ~SSI_STCCR_WL_MASK;
256
257 /* DAI data (word) size */
258 switch (params_format(params)) {
259 case SNDRV_PCM_FORMAT_S16_LE:
260 sccr |= SSI_SRCCR_WL(16);
261 break;
262 case SNDRV_PCM_FORMAT_S20_3LE:
263 sccr |= SSI_SRCCR_WL(20);
264 break;
265 case SNDRV_PCM_FORMAT_S24_LE:
266 sccr |= SSI_SRCCR_WL(24);
267 break;
268 }
269
270 writel(sccr, ssi->base + reg);
271
272 return 0;
273}
274
275static int imx_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
276 struct snd_soc_dai *dai)
277{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278 struct imx_ssi *ssi = snd_soc_dai_get_drvdata(dai);
Sascha Hauer83802222009-11-25 16:41:04 +0100279 unsigned int sier_bits, sier;
280 unsigned int scr;
281
282 scr = readl(ssi->base + SSI_SCR);
283 sier = readl(ssi->base + SSI_SIER);
284
285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
286 if (ssi->flags & IMX_SSI_DMA)
287 sier_bits = SSI_SIER_TDMAE;
288 else
289 sier_bits = SSI_SIER_TIE | SSI_SIER_TFE0_EN;
290 } else {
291 if (ssi->flags & IMX_SSI_DMA)
292 sier_bits = SSI_SIER_RDMAE;
293 else
294 sier_bits = SSI_SIER_RIE | SSI_SIER_RFF0_EN;
295 }
296
297 switch (cmd) {
298 case SNDRV_PCM_TRIGGER_START:
299 case SNDRV_PCM_TRIGGER_RESUME:
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
302 scr |= SSI_SCR_TE;
303 else
304 scr |= SSI_SCR_RE;
305 sier |= sier_bits;
306
307 if (++ssi->enabled == 1)
308 scr |= SSI_SCR_SSIEN;
309
310 break;
311
312 case SNDRV_PCM_TRIGGER_STOP:
313 case SNDRV_PCM_TRIGGER_SUSPEND:
314 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
316 scr &= ~SSI_SCR_TE;
317 else
318 scr &= ~SSI_SCR_RE;
319 sier &= ~sier_bits;
320
321 if (--ssi->enabled == 0)
322 scr &= ~SSI_SCR_SSIEN;
323
324 break;
325 default:
326 return -EINVAL;
327 }
328
329 if (!(ssi->flags & IMX_SSI_USE_AC97))
330 /* rx/tx are always enabled to access ac97 registers */
331 writel(scr, ssi->base + SSI_SCR);
332
333 writel(sier, ssi->base + SSI_SIER);
334
335 return 0;
336}
337
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100338static const struct snd_soc_dai_ops imx_ssi_pcm_dai_ops = {
Sascha Hauer83802222009-11-25 16:41:04 +0100339 .hw_params = imx_ssi_hw_params,
340 .set_fmt = imx_ssi_set_dai_fmt,
341 .set_clkdiv = imx_ssi_set_dai_clkdiv,
342 .set_sysclk = imx_ssi_set_dai_sysclk,
343 .set_tdm_slot = imx_ssi_set_dai_tdm_slot,
344 .trigger = imx_ssi_trigger,
345};
346
Sascha Hauerf562be52010-11-04 17:05:42 +0100347static int imx_ssi_dai_probe(struct snd_soc_dai *dai)
348{
349 struct imx_ssi *ssi = dev_get_drvdata(dai->dev);
350 uint32_t val;
351
352 snd_soc_dai_set_drvdata(dai, ssi);
353
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200354 val = SSI_SFCSR_TFWM0(ssi->dma_params_tx.maxburst) |
355 SSI_SFCSR_RFWM0(ssi->dma_params_rx.maxburst);
Sascha Hauerf562be52010-11-04 17:05:42 +0100356 writel(val, ssi->base + SSI_SFCSR);
357
Lars-Peter Clausenfc8ba7f2013-04-15 19:19:58 +0200358 /* Tx/Rx config */
359 dai->playback_dma_data = &ssi->dma_params_tx;
360 dai->capture_dma_data = &ssi->dma_params_rx;
361
Sascha Hauerf562be52010-11-04 17:05:42 +0100362 return 0;
363}
364
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000365static struct snd_soc_dai_driver imx_ssi_dai = {
Sascha Hauerf562be52010-11-04 17:05:42 +0100366 .probe = imx_ssi_dai_probe,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000367 .playback = {
Javier Martin104c2292012-09-05 21:30:10 -0300368 .channels_min = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000369 .channels_max = 2,
370 .rates = SNDRV_PCM_RATE_8000_96000,
371 .formats = SNDRV_PCM_FMTBIT_S16_LE,
372 },
373 .capture = {
Javier Martin104c2292012-09-05 21:30:10 -0300374 .channels_min = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000375 .channels_max = 2,
376 .rates = SNDRV_PCM_RATE_8000_96000,
377 .formats = SNDRV_PCM_FMTBIT_S16_LE,
378 },
379 .ops = &imx_ssi_pcm_dai_ops,
Sascha Hauer83802222009-11-25 16:41:04 +0100380};
Sascha Hauer83802222009-11-25 16:41:04 +0100381
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000382static struct snd_soc_dai_driver imx_ac97_dai = {
383 .probe = imx_ssi_dai_probe,
Sascha Hauer83802222009-11-25 16:41:04 +0100384 .ac97_control = 1,
385 .playback = {
386 .stream_name = "AC97 Playback",
387 .channels_min = 2,
388 .channels_max = 2,
Sascha Hauerc1963c32013-03-10 19:33:02 +0100389 .rates = SNDRV_PCM_RATE_8000_48000,
Sascha Hauer83802222009-11-25 16:41:04 +0100390 .formats = SNDRV_PCM_FMTBIT_S16_LE,
391 },
392 .capture = {
393 .stream_name = "AC97 Capture",
394 .channels_min = 2,
395 .channels_max = 2,
396 .rates = SNDRV_PCM_RATE_48000,
397 .formats = SNDRV_PCM_FMTBIT_S16_LE,
398 },
399 .ops = &imx_ssi_pcm_dai_ops,
400};
401
Kuninori Morimotoc22fd5e2013-03-21 03:32:15 -0700402static const struct snd_soc_component_driver imx_component = {
403 .name = DRV_NAME,
404};
405
Sascha Hauer83802222009-11-25 16:41:04 +0100406static void setup_channel_to_ac97(struct imx_ssi *imx_ssi)
407{
408 void __iomem *base = imx_ssi->base;
409
410 writel(0x0, base + SSI_SCR);
411 writel(0x0, base + SSI_STCR);
412 writel(0x0, base + SSI_SRCR);
413
414 writel(SSI_SCR_SYN | SSI_SCR_NET, base + SSI_SCR);
415
416 writel(SSI_SFCSR_RFWM0(8) |
417 SSI_SFCSR_TFWM0(8) |
418 SSI_SFCSR_RFWM1(8) |
419 SSI_SFCSR_TFWM1(8), base + SSI_SFCSR);
420
421 writel(SSI_STCCR_WL(16) | SSI_STCCR_DC(12), base + SSI_STCCR);
422 writel(SSI_STCCR_WL(16) | SSI_STCCR_DC(12), base + SSI_SRCCR);
423
424 writel(SSI_SCR_SYN | SSI_SCR_NET | SSI_SCR_SSIEN, base + SSI_SCR);
425 writel(SSI_SOR_WAIT(3), base + SSI_SOR);
426
427 writel(SSI_SCR_SYN | SSI_SCR_NET | SSI_SCR_SSIEN |
428 SSI_SCR_TE | SSI_SCR_RE,
429 base + SSI_SCR);
430
431 writel(SSI_SACNT_DEFAULT, base + SSI_SACNT);
432 writel(0xff, base + SSI_SACCDIS);
433 writel(0x300, base + SSI_SACCEN);
434}
435
436static struct imx_ssi *ac97_ssi;
437
438static void imx_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
439 unsigned short val)
440{
441 struct imx_ssi *imx_ssi = ac97_ssi;
442 void __iomem *base = imx_ssi->base;
443 unsigned int lreg;
444 unsigned int lval;
445
446 if (reg > 0x7f)
447 return;
448
449 pr_debug("%s: 0x%02x 0x%04x\n", __func__, reg, val);
450
451 lreg = reg << 12;
452 writel(lreg, base + SSI_SACADD);
453
454 lval = val << 4;
455 writel(lval , base + SSI_SACDAT);
456
457 writel(SSI_SACNT_DEFAULT | SSI_SACNT_WR, base + SSI_SACNT);
458 udelay(100);
459}
460
461static unsigned short imx_ssi_ac97_read(struct snd_ac97 *ac97,
462 unsigned short reg)
463{
464 struct imx_ssi *imx_ssi = ac97_ssi;
465 void __iomem *base = imx_ssi->base;
466
467 unsigned short val = -1;
468 unsigned int lreg;
469
470 lreg = (reg & 0x7f) << 12 ;
471 writel(lreg, base + SSI_SACADD);
472 writel(SSI_SACNT_DEFAULT | SSI_SACNT_RD, base + SSI_SACNT);
473
474 udelay(100);
475
476 val = (readl(base + SSI_SACDAT) >> 4) & 0xffff;
477
478 pr_debug("%s: 0x%02x 0x%04x\n", __func__, reg, val);
479
480 return val;
481}
482
483static void imx_ssi_ac97_reset(struct snd_ac97 *ac97)
484{
485 struct imx_ssi *imx_ssi = ac97_ssi;
486
487 if (imx_ssi->ac97_reset)
488 imx_ssi->ac97_reset(ac97);
Sascha Hauerb6e51602013-03-10 19:33:03 +0100489 /* First read sometimes fails, do a dummy read */
490 imx_ssi_ac97_read(ac97, 0);
Sascha Hauer83802222009-11-25 16:41:04 +0100491}
492
493static void imx_ssi_ac97_warm_reset(struct snd_ac97 *ac97)
494{
495 struct imx_ssi *imx_ssi = ac97_ssi;
496
497 if (imx_ssi->ac97_warm_reset)
498 imx_ssi->ac97_warm_reset(ac97);
Sascha Hauerb6e51602013-03-10 19:33:03 +0100499
500 /* First read sometimes fails, do a dummy read */
501 imx_ssi_ac97_read(ac97, 0);
Sascha Hauer83802222009-11-25 16:41:04 +0100502}
503
Mark Brownb047e1c2013-06-26 12:45:59 +0100504static struct snd_ac97_bus_ops imx_ssi_ac97_ops = {
Sascha Hauer83802222009-11-25 16:41:04 +0100505 .read = imx_ssi_ac97_read,
506 .write = imx_ssi_ac97_write,
507 .reset = imx_ssi_ac97_reset,
508 .warm_reset = imx_ssi_ac97_warm_reset
509};
Sascha Hauer83802222009-11-25 16:41:04 +0100510
Sascha Hauer83802222009-11-25 16:41:04 +0100511static int imx_ssi_probe(struct platform_device *pdev)
512{
513 struct resource *res;
514 struct imx_ssi *ssi;
515 struct imx_ssi_platform_data *pdata = pdev->dev.platform_data;
Sascha Hauer83802222009-11-25 16:41:04 +0100516 int ret = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 struct snd_soc_dai_driver *dai;
Sascha Hauer83802222009-11-25 16:41:04 +0100518
Fabio Estevamad3ab1b2012-07-27 14:32:10 -0300519 ssi = devm_kzalloc(&pdev->dev, sizeof(*ssi), GFP_KERNEL);
Sascha Hauer83802222009-11-25 16:41:04 +0100520 if (!ssi)
521 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 dev_set_drvdata(&pdev->dev, ssi);
Sascha Hauer83802222009-11-25 16:41:04 +0100523
524 if (pdata) {
525 ssi->ac97_reset = pdata->ac97_reset;
526 ssi->ac97_warm_reset = pdata->ac97_warm_reset;
527 ssi->flags = pdata->flags;
528 }
529
Sascha Hauer83802222009-11-25 16:41:04 +0100530 ssi->irq = platform_get_irq(pdev, 0);
531
Fabio Estevamad3ab1b2012-07-27 14:32:10 -0300532 ssi->clk = devm_clk_get(&pdev->dev, NULL);
Sascha Hauer83802222009-11-25 16:41:04 +0100533 if (IS_ERR(ssi->clk)) {
534 ret = PTR_ERR(ssi->clk);
535 dev_err(&pdev->dev, "Cannot get the clock: %d\n",
536 ret);
537 goto failed_clk;
538 }
Fabio Estevam41c73b62012-05-25 17:27:20 -0300539 clk_prepare_enable(ssi->clk);
Sascha Hauer83802222009-11-25 16:41:04 +0100540
541 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Redingb25b5aa2013-01-21 11:09:26 +0100542 ssi->base = devm_ioremap_resource(&pdev->dev, res);
543 if (IS_ERR(ssi->base)) {
544 ret = PTR_ERR(ssi->base);
Fabio Estevamad3ab1b2012-07-27 14:32:10 -0300545 goto failed_register;
Sascha Hauer83802222009-11-25 16:41:04 +0100546 }
547
548 if (ssi->flags & IMX_SSI_USE_AC97) {
549 if (ac97_ssi) {
Mark Browna0f1e982012-07-31 18:34:07 +0100550 dev_err(&pdev->dev, "AC'97 SSI already registered\n");
Sascha Hauer83802222009-11-25 16:41:04 +0100551 ret = -EBUSY;
Fabio Estevamad3ab1b2012-07-27 14:32:10 -0300552 goto failed_register;
Sascha Hauer83802222009-11-25 16:41:04 +0100553 }
554 ac97_ssi = ssi;
555 setup_channel_to_ac97(ssi);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000556 dai = &imx_ac97_dai;
Sascha Hauer83802222009-11-25 16:41:04 +0100557 } else
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 dai = &imx_ssi_dai;
Sascha Hauer83802222009-11-25 16:41:04 +0100559
560 writel(0x0, ssi->base + SSI_SIER);
561
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200562 ssi->dma_params_rx.addr = res->start + SSI_SRX0;
563 ssi->dma_params_tx.addr = res->start + SSI_STX0;
Sascha Hauer83802222009-11-25 16:41:04 +0100564
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200565 ssi->dma_params_tx.maxburst = 6;
566 ssi->dma_params_rx.maxburst = 4;
567
568 ssi->dma_params_tx.filter_data = &ssi->filter_data_tx;
569 ssi->dma_params_rx.filter_data = &ssi->filter_data_rx;
Javier Martin0a934212011-03-01 15:02:06 +0100570
Sascha Hauer83802222009-11-25 16:41:04 +0100571 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx0");
Lars-Peter Clausen312bb4f2013-03-22 14:12:12 +0100572 if (res) {
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200573 imx_pcm_dma_params_init_data(&ssi->filter_data_tx, res->start,
Nicolin Chen32bd8cd2013-07-25 17:41:41 +0800574 IMX_DMATYPE_SSI);
Lars-Peter Clausen312bb4f2013-03-22 14:12:12 +0100575 }
Sascha Hauer83802222009-11-25 16:41:04 +0100576
577 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx0");
Lars-Peter Clausen312bb4f2013-03-22 14:12:12 +0100578 if (res) {
Lars-Peter Clausena8909c92013-04-03 11:06:04 +0200579 imx_pcm_dma_params_init_data(&ssi->filter_data_rx, res->start,
Nicolin Chen32bd8cd2013-07-25 17:41:41 +0800580 IMX_DMATYPE_SSI);
Lars-Peter Clausen312bb4f2013-03-22 14:12:12 +0100581 }
Sascha Hauer83802222009-11-25 16:41:04 +0100582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 platform_set_drvdata(pdev, ssi);
Sascha Hauer83802222009-11-25 16:41:04 +0100584
Mark Brownb047e1c2013-06-26 12:45:59 +0100585 ret = snd_soc_set_ac97_ops(&imx_ssi_ac97_ops);
586 if (ret != 0) {
587 dev_err(&pdev->dev, "Failed to set AC'97 ops: %d\n", ret);
588 goto failed_register;
589 }
590
Kuninori Morimotoc22fd5e2013-03-21 03:32:15 -0700591 ret = snd_soc_register_component(&pdev->dev, &imx_component,
592 dai, 1);
Sascha Hauer83802222009-11-25 16:41:04 +0100593 if (ret) {
594 dev_err(&pdev->dev, "register DAI failed\n");
595 goto failed_register;
596 }
597
Markus Pargmann9051cba2013-06-20 15:20:21 +0200598 ssi->fiq_params.irq = ssi->irq;
599 ssi->fiq_params.base = ssi->base;
600 ssi->fiq_params.dma_params_rx = &ssi->dma_params_rx;
601 ssi->fiq_params.dma_params_tx = &ssi->dma_params_tx;
602
603 ret = imx_pcm_fiq_init(pdev, &ssi->fiq_params);
Shawn Guo2bf9d4b2013-04-25 11:18:49 +0800604 if (ret)
605 goto failed_pcm_fiq;
Axel Linac8f9242010-11-25 15:13:09 +0800606
Shawn Guo3b7d4632013-04-25 11:18:47 +0800607 ret = imx_pcm_dma_init(pdev);
608 if (ret)
609 goto failed_pcm_dma;
Sascha Hauer83802222009-11-25 16:41:04 +0100610
611 return 0;
612
Shawn Guo3b7d4632013-04-25 11:18:47 +0800613failed_pcm_dma:
Shawn Guo2bf9d4b2013-04-25 11:18:49 +0800614 imx_pcm_fiq_exit(pdev);
615failed_pcm_fiq:
Kuninori Morimotoc22fd5e2013-03-21 03:32:15 -0700616 snd_soc_unregister_component(&pdev->dev);
Sascha Hauer83802222009-11-25 16:41:04 +0100617failed_register:
Sascha Hauer83802222009-11-25 16:41:04 +0100618 release_mem_region(res->start, resource_size(res));
Fabio Estevam41c73b62012-05-25 17:27:20 -0300619 clk_disable_unprepare(ssi->clk);
Sascha Hauer83802222009-11-25 16:41:04 +0100620failed_clk:
Mark Brownb047e1c2013-06-26 12:45:59 +0100621 snd_soc_set_ac97_ops(NULL);
Sascha Hauer83802222009-11-25 16:41:04 +0100622
623 return ret;
624}
625
Bill Pembertona0a3d512012-12-07 09:26:16 -0500626static int imx_ssi_remove(struct platform_device *pdev)
Sascha Hauer83802222009-11-25 16:41:04 +0100627{
628 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
629 struct imx_ssi *ssi = platform_get_drvdata(pdev);
630
Shawn Guo3b7d4632013-04-25 11:18:47 +0800631 imx_pcm_dma_exit(pdev);
Shawn Guo2bf9d4b2013-04-25 11:18:49 +0800632 imx_pcm_fiq_exit(pdev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633
Kuninori Morimotoc22fd5e2013-03-21 03:32:15 -0700634 snd_soc_unregister_component(&pdev->dev);
Sascha Hauer83802222009-11-25 16:41:04 +0100635
636 if (ssi->flags & IMX_SSI_USE_AC97)
637 ac97_ssi = NULL;
638
Sascha Hauer83802222009-11-25 16:41:04 +0100639 release_mem_region(res->start, resource_size(res));
Fabio Estevam41c73b62012-05-25 17:27:20 -0300640 clk_disable_unprepare(ssi->clk);
Mark Brownb047e1c2013-06-26 12:45:59 +0100641 snd_soc_set_ac97_ops(NULL);
Sascha Hauer83802222009-11-25 16:41:04 +0100642
643 return 0;
644}
645
646static struct platform_driver imx_ssi_driver = {
647 .probe = imx_ssi_probe,
Bill Pembertona0a3d512012-12-07 09:26:16 -0500648 .remove = imx_ssi_remove,
Sascha Hauer83802222009-11-25 16:41:04 +0100649
650 .driver = {
Mark Brown205d2312010-09-30 13:46:14 -0700651 .name = "imx-ssi",
Sascha Hauer83802222009-11-25 16:41:04 +0100652 .owner = THIS_MODULE,
653 },
654};
655
Axel Lin7a24b2b2011-11-24 15:03:50 +0800656module_platform_driver(imx_ssi_driver);
Sascha Hauer83802222009-11-25 16:41:04 +0100657
658/* Module information */
659MODULE_AUTHOR("Sascha Hauer, <s.hauer@pengutronix.de>");
660MODULE_DESCRIPTION("i.MX I2S/ac97 SoC Interface");
661MODULE_LICENSE("GPL");
Arnaud Patard (Rtp)96dcabb2011-06-22 22:21:49 +0200662MODULE_ALIAS("platform:imx-ssi");