ASoC: mxs-saif: Store saif state
[deliverable/linux.git] / sound / soc / mxs / mxs-saif.c
CommitLineData
2a24f2ce
DA
1/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
08641c7c
SG
21#include <linux/of.h>
22#include <linux/of_device.h>
2a24f2ce
DA
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/dma-mapping.h>
26#include <linux/clk.h>
7c9e6150 27#include <linux/clk-provider.h>
2a24f2ce 28#include <linux/delay.h>
76067540 29#include <linux/time.h>
2a24f2ce
DA
30#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
2a24f2ce
DA
34
35#include "mxs-saif.h"
36
114fe75d
SG
37#define MXS_SET_ADDR 0x4
38#define MXS_CLR_ADDR 0x8
39
2a24f2ce
DA
40static struct mxs_saif *mxs_saif[2];
41
76067540
DA
42/*
43 * SAIF is a little different with other normal SOC DAIs on clock using.
44 *
45 * For MXS, two SAIF modules are instantiated on-chip.
46 * Each SAIF has a set of clock pins and can be operating in master
47 * mode simultaneously if they are connected to different off-chip codecs.
48 * Also, one of the two SAIFs can master or drive the clock pins while the
49 * other SAIF, in slave mode, receives clocking from the master SAIF.
50 * This also means that both SAIFs must operate at the same sample rate.
51 *
52 * We abstract this as each saif has a master, the master could be
53 * himself or other saifs. In the generic saif driver, saif does not need
54 * to know the different clkmux. Saif only needs to know who is his master
55 * and operating his master to generate the proper clock rate for him.
56 * The master id is provided in mach-specific layer according to different
57 * clkmux setting.
58 */
59
2a24f2ce
DA
60static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
61 int clk_id, unsigned int freq, int dir)
62{
63 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
64
65 switch (clk_id) {
66 case MXS_SAIF_MCLK:
67 saif->mclk = freq;
68 break;
69 default:
70 return -EINVAL;
71 }
72 return 0;
73}
74
76067540
DA
75/*
76 * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
77 * is provided by other SAIF, we provide a interface here to get its master
78 * from its master_id.
79 * Note that the master could be himself.
80 */
81static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
82{
83 return mxs_saif[saif->master_id];
84}
85
2a24f2ce
DA
86/*
87 * Set SAIF clock and MCLK
88 */
89static int mxs_saif_set_clk(struct mxs_saif *saif,
90 unsigned int mclk,
91 unsigned int rate)
92{
93 u32 scr;
94 int ret;
76067540 95 struct mxs_saif *master_saif;
2a24f2ce 96
76067540
DA
97 dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
98
99 /* Set master saif to generate proper clock */
100 master_saif = mxs_saif_get_master(saif);
101 if (!master_saif)
102 return -EINVAL;
103
104 dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
105
106 /* Checking if can playback and capture simutaneously */
107 if (master_saif->ongoing && rate != master_saif->cur_rate) {
108 dev_err(saif->dev,
109 "can not change clock, master saif%d(rate %d) is ongoing\n",
110 master_saif->id, master_saif->cur_rate);
111 return -EINVAL;
112 }
113
114 scr = __raw_readl(master_saif->base + SAIF_CTRL);
2a24f2ce
DA
115 scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
116 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
117
118 /*
119 * Set SAIF clock
120 *
121 * The SAIF clock should be either 384*fs or 512*fs.
122 * If MCLK is used, the SAIF clk ratio need to match mclk ratio.
123 * For 32x mclk, set saif clk as 512*fs.
124 * For 48x mclk, set saif clk as 384*fs.
125 *
126 * If MCLK is not used, we just set saif clk to 512*fs.
127 */
6b35f924
FE
128 clk_prepare_enable(master_saif->clk);
129
76067540 130 if (master_saif->mclk_in_use) {
2a24f2ce
DA
131 if (mclk % 32 == 0) {
132 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
76067540 133 ret = clk_set_rate(master_saif->clk, 512 * rate);
2a24f2ce
DA
134 } else if (mclk % 48 == 0) {
135 scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
76067540 136 ret = clk_set_rate(master_saif->clk, 384 * rate);
2a24f2ce
DA
137 } else {
138 /* SAIF MCLK should be either 32x or 48x */
6b35f924 139 clk_disable_unprepare(master_saif->clk);
2a24f2ce
DA
140 return -EINVAL;
141 }
142 } else {
76067540 143 ret = clk_set_rate(master_saif->clk, 512 * rate);
2a24f2ce
DA
144 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
145 }
146
6b35f924
FE
147 clk_disable_unprepare(master_saif->clk);
148
2a24f2ce
DA
149 if (ret)
150 return ret;
151
76067540
DA
152 master_saif->cur_rate = rate;
153
154 if (!master_saif->mclk_in_use) {
155 __raw_writel(scr, master_saif->base + SAIF_CTRL);
2a24f2ce
DA
156 return 0;
157 }
158
159 /*
160 * Program the over-sample rate for MCLK output
161 *
162 * The available MCLK range is 32x, 48x... 512x. The rate
163 * could be from 8kHz to 192kH.
164 */
165 switch (mclk / rate) {
166 case 32:
167 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
168 break;
169 case 64:
170 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
171 break;
172 case 128:
173 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
174 break;
175 case 256:
176 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
177 break;
178 case 512:
179 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
180 break;
181 case 48:
182 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
183 break;
184 case 96:
185 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
186 break;
187 case 192:
188 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
189 break;
190 case 384:
191 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
192 break;
193 default:
194 return -EINVAL;
195 }
196
76067540 197 __raw_writel(scr, master_saif->base + SAIF_CTRL);
2a24f2ce
DA
198
199 return 0;
200}
201
202/*
203 * Put and disable MCLK.
204 */
205int mxs_saif_put_mclk(unsigned int saif_id)
206{
207 struct mxs_saif *saif = mxs_saif[saif_id];
208 u32 stat;
209
210 if (!saif)
211 return -EINVAL;
212
213 stat = __raw_readl(saif->base + SAIF_STAT);
214 if (stat & BM_SAIF_STAT_BUSY) {
215 dev_err(saif->dev, "error: busy\n");
216 return -EBUSY;
217 }
218
67939b22 219 clk_disable_unprepare(saif->clk);
2a24f2ce
DA
220
221 /* disable MCLK output */
222 __raw_writel(BM_SAIF_CTRL_CLKGATE,
223 saif->base + SAIF_CTRL + MXS_SET_ADDR);
224 __raw_writel(BM_SAIF_CTRL_RUN,
225 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
226
227 saif->mclk_in_use = 0;
228 return 0;
229}
cf7d0f09 230EXPORT_SYMBOL_GPL(mxs_saif_put_mclk);
2a24f2ce
DA
231
232/*
233 * Get MCLK and set clock rate, then enable it
234 *
235 * This interface is used for codecs who are using MCLK provided
236 * by saif.
237 */
238int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
239 unsigned int rate)
240{
241 struct mxs_saif *saif = mxs_saif[saif_id];
242 u32 stat;
243 int ret;
76067540 244 struct mxs_saif *master_saif;
2a24f2ce
DA
245
246 if (!saif)
247 return -EINVAL;
248
bbe8ff5e
DA
249 /* Clear Reset */
250 __raw_writel(BM_SAIF_CTRL_SFTRST,
251 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
252
253 /* FIXME: need clear clk gate for register r/w */
254 __raw_writel(BM_SAIF_CTRL_CLKGATE,
255 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
256
76067540
DA
257 master_saif = mxs_saif_get_master(saif);
258 if (saif != master_saif) {
259 dev_err(saif->dev, "can not get mclk from a non-master saif\n");
260 return -EINVAL;
261 }
262
2a24f2ce
DA
263 stat = __raw_readl(saif->base + SAIF_STAT);
264 if (stat & BM_SAIF_STAT_BUSY) {
265 dev_err(saif->dev, "error: busy\n");
266 return -EBUSY;
267 }
268
2a24f2ce
DA
269 saif->mclk_in_use = 1;
270 ret = mxs_saif_set_clk(saif, mclk, rate);
271 if (ret)
272 return ret;
273
67939b22 274 ret = clk_prepare_enable(saif->clk);
2a24f2ce
DA
275 if (ret)
276 return ret;
277
278 /* enable MCLK output */
2a24f2ce
DA
279 __raw_writel(BM_SAIF_CTRL_RUN,
280 saif->base + SAIF_CTRL + MXS_SET_ADDR);
281
282 return 0;
283}
cf7d0f09 284EXPORT_SYMBOL_GPL(mxs_saif_get_mclk);
2a24f2ce
DA
285
286/*
287 * SAIF DAI format configuration.
288 * Should only be called when port is inactive.
289 */
290static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
291{
292 u32 scr, stat;
293 u32 scr0;
294 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
295
296 stat = __raw_readl(saif->base + SAIF_STAT);
297 if (stat & BM_SAIF_STAT_BUSY) {
298 dev_err(cpu_dai->dev, "error: busy\n");
299 return -EBUSY;
300 }
301
302 scr0 = __raw_readl(saif->base + SAIF_CTRL);
303 scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
304 & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
305 scr = 0;
306
307 /* DAI mode */
308 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
309 case SND_SOC_DAIFMT_I2S:
310 /* data frame low 1clk before data */
311 scr |= BM_SAIF_CTRL_DELAY;
312 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
313 break;
314 case SND_SOC_DAIFMT_LEFT_J:
315 /* data frame high with data */
316 scr &= ~BM_SAIF_CTRL_DELAY;
317 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
318 scr &= ~BM_SAIF_CTRL_JUSTIFY;
319 break;
320 default:
321 return -EINVAL;
322 }
323
324 /* DAI clock inversion */
325 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
326 case SND_SOC_DAIFMT_IB_IF:
327 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
328 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
329 break;
330 case SND_SOC_DAIFMT_IB_NF:
331 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
332 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
333 break;
334 case SND_SOC_DAIFMT_NB_IF:
335 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
336 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
337 break;
338 case SND_SOC_DAIFMT_NB_NF:
339 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
340 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
341 break;
342 }
343
344 /*
345 * Note: We simply just support master mode since SAIF TX can only
346 * work as master.
76067540
DA
347 * Here the master is relative to codec side.
348 * Saif internally could be slave when working on EXTMASTER mode.
349 * We just hide this to machine driver.
2a24f2ce
DA
350 */
351 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
352 case SND_SOC_DAIFMT_CBS_CFS:
76067540
DA
353 if (saif->id == saif->master_id)
354 scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
355 else
356 scr |= BM_SAIF_CTRL_SLAVE_MODE;
357
2a24f2ce
DA
358 __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
359 break;
360 default:
361 return -EINVAL;
362 }
363
364 return 0;
365}
366
367static int mxs_saif_startup(struct snd_pcm_substream *substream,
368 struct snd_soc_dai *cpu_dai)
369{
370 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
2a24f2ce
DA
371
372 /* clear error status to 0 for each re-open */
373 saif->fifo_underrun = 0;
374 saif->fifo_overrun = 0;
375
376 /* Clear Reset for normal operations */
377 __raw_writel(BM_SAIF_CTRL_SFTRST,
378 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
379
bbe8ff5e
DA
380 /* clear clock gate */
381 __raw_writel(BM_SAIF_CTRL_CLKGATE,
382 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
383
2a24f2ce
DA
384 return 0;
385}
386
387/*
388 * Should only be called when port is inactive.
389 * although can be called multiple times by upper layers.
390 */
391static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
392 struct snd_pcm_hw_params *params,
393 struct snd_soc_dai *cpu_dai)
394{
395 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
c2e1d907 396 struct mxs_saif *master_saif;
2a24f2ce
DA
397 u32 scr, stat;
398 int ret;
399
c2e1d907
DA
400 master_saif = mxs_saif_get_master(saif);
401 if (!master_saif)
402 return -EINVAL;
403
2a24f2ce
DA
404 /* mclk should already be set */
405 if (!saif->mclk && saif->mclk_in_use) {
406 dev_err(cpu_dai->dev, "set mclk first\n");
407 return -EINVAL;
408 }
409
410 stat = __raw_readl(saif->base + SAIF_STAT);
411 if (stat & BM_SAIF_STAT_BUSY) {
412 dev_err(cpu_dai->dev, "error: busy\n");
413 return -EBUSY;
414 }
415
416 /*
417 * Set saif clk based on sample rate.
418 * If mclk is used, we also set mclk, if not, saif->mclk is
419 * default 0, means not used.
420 */
421 ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
422 if (ret) {
423 dev_err(cpu_dai->dev, "unable to get proper clk\n");
424 return ret;
425 }
426
c2e1d907
DA
427 /* prepare clk in hw_param, enable in trigger */
428 clk_prepare(saif->clk);
d0ba4c01
DA
429 if (saif != master_saif) {
430 /*
431 * Set an initial clock rate for the saif internal logic to work
432 * properly. This is important when working in EXTMASTER mode
433 * that uses the other saif's BITCLK&LRCLK but it still needs a
434 * basic clock which should be fast enough for the internal
435 * logic.
436 */
437 clk_enable(saif->clk);
438 ret = clk_set_rate(saif->clk, 24000000);
439 clk_disable(saif->clk);
440 if (ret)
441 return ret;
442
c2e1d907 443 clk_prepare(master_saif->clk);
d0ba4c01 444 }
c2e1d907 445
2a24f2ce
DA
446 scr = __raw_readl(saif->base + SAIF_CTRL);
447
448 scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
449 scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
450 switch (params_format(params)) {
451 case SNDRV_PCM_FORMAT_S16_LE:
452 scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
453 break;
454 case SNDRV_PCM_FORMAT_S20_3LE:
455 scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
456 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
457 break;
458 case SNDRV_PCM_FORMAT_S24_LE:
459 scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
460 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
461 break;
462 default:
463 return -EINVAL;
464 }
465
466 /* Tx/Rx config */
467 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
468 /* enable TX mode */
469 scr &= ~BM_SAIF_CTRL_READ_MODE;
470 } else {
471 /* enable RX mode */
472 scr |= BM_SAIF_CTRL_READ_MODE;
473 }
474
475 __raw_writel(scr, saif->base + SAIF_CTRL);
476 return 0;
477}
478
479static int mxs_saif_prepare(struct snd_pcm_substream *substream,
480 struct snd_soc_dai *cpu_dai)
481{
482 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
483
2a24f2ce
DA
484 /* enable FIFO error irqs */
485 __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
486 saif->base + SAIF_CTRL + MXS_SET_ADDR);
487
488 return 0;
489}
490
491static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
492 struct snd_soc_dai *cpu_dai)
493{
494 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
76067540
DA
495 struct mxs_saif *master_saif;
496 u32 delay;
497
498 master_saif = mxs_saif_get_master(saif);
499 if (!master_saif)
500 return -EINVAL;
2a24f2ce
DA
501
502 switch (cmd) {
503 case SNDRV_PCM_TRIGGER_START:
504 case SNDRV_PCM_TRIGGER_RESUME:
505 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
88cf632a
MP
506 if (saif->state == MXS_SAIF_STATE_RUNNING)
507 return 0;
508
2a24f2ce
DA
509 dev_dbg(cpu_dai->dev, "start\n");
510
76067540
DA
511 clk_enable(master_saif->clk);
512 if (!master_saif->mclk_in_use)
513 __raw_writel(BM_SAIF_CTRL_RUN,
514 master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
515
516 /*
517 * If the saif's master is not himself, we also need to enable
518 * itself clk for its internal basic logic to work.
519 */
520 if (saif != master_saif) {
521 clk_enable(saif->clk);
2a24f2ce
DA
522 __raw_writel(BM_SAIF_CTRL_RUN,
523 saif->base + SAIF_CTRL + MXS_SET_ADDR);
76067540 524 }
2a24f2ce
DA
525
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
527 /*
f55f1475
FE
528 * write data to saif data register to trigger
529 * the transfer.
530 * For 24-bit format the 32-bit FIFO register stores
531 * only one channel, so we need to write twice.
532 * This is also safe for the other non 24-bit formats.
2a24f2ce
DA
533 */
534 __raw_writel(0, saif->base + SAIF_DATA);
f55f1475 535 __raw_writel(0, saif->base + SAIF_DATA);
2a24f2ce
DA
536 } else {
537 /*
f55f1475
FE
538 * read data from saif data register to trigger
539 * the receive.
540 * For 24-bit format the 32-bit FIFO register stores
541 * only one channel, so we need to read twice.
542 * This is also safe for the other non 24-bit formats.
2a24f2ce
DA
543 */
544 __raw_readl(saif->base + SAIF_DATA);
f55f1475 545 __raw_readl(saif->base + SAIF_DATA);
2a24f2ce
DA
546 }
547
76067540 548 master_saif->ongoing = 1;
88cf632a 549 saif->state = MXS_SAIF_STATE_RUNNING;
76067540
DA
550
551 dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
2a24f2ce
DA
552 __raw_readl(saif->base + SAIF_CTRL),
553 __raw_readl(saif->base + SAIF_STAT));
554
76067540
DA
555 dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
556 __raw_readl(master_saif->base + SAIF_CTRL),
557 __raw_readl(master_saif->base + SAIF_STAT));
2a24f2ce
DA
558 break;
559 case SNDRV_PCM_TRIGGER_SUSPEND:
560 case SNDRV_PCM_TRIGGER_STOP:
561 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
88cf632a
MP
562 if (saif->state == MXS_SAIF_STATE_STOPPED)
563 return 0;
564
2a24f2ce
DA
565 dev_dbg(cpu_dai->dev, "stop\n");
566
76067540
DA
567 /* wait a while for the current sample to complete */
568 delay = USEC_PER_SEC / master_saif->cur_rate;
569
570 if (!master_saif->mclk_in_use) {
571 __raw_writel(BM_SAIF_CTRL_RUN,
572 master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
573 udelay(delay);
574 }
575 clk_disable(master_saif->clk);
576
577 if (saif != master_saif) {
2a24f2ce
DA
578 __raw_writel(BM_SAIF_CTRL_RUN,
579 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
76067540
DA
580 udelay(delay);
581 clk_disable(saif->clk);
582 }
583
584 master_saif->ongoing = 0;
88cf632a 585 saif->state = MXS_SAIF_STATE_STOPPED;
2a24f2ce
DA
586
587 break;
588 default:
589 return -EINVAL;
590 }
591
592 return 0;
593}
594
595#define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
596#define MXS_SAIF_FORMATS \
597 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
598 SNDRV_PCM_FMTBIT_S24_LE)
599
85e7652d 600static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
2a24f2ce
DA
601 .startup = mxs_saif_startup,
602 .trigger = mxs_saif_trigger,
603 .prepare = mxs_saif_prepare,
604 .hw_params = mxs_saif_hw_params,
605 .set_sysclk = mxs_saif_set_dai_sysclk,
606 .set_fmt = mxs_saif_set_dai_fmt,
607};
608
609static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
610{
611 struct mxs_saif *saif = dev_get_drvdata(dai->dev);
612
613 snd_soc_dai_set_drvdata(dai, saif);
614
615 return 0;
616}
617
618static struct snd_soc_dai_driver mxs_saif_dai = {
619 .name = "mxs-saif",
620 .probe = mxs_saif_dai_probe,
621 .playback = {
622 .channels_min = 2,
623 .channels_max = 2,
624 .rates = MXS_SAIF_RATES,
625 .formats = MXS_SAIF_FORMATS,
626 },
627 .capture = {
628 .channels_min = 2,
629 .channels_max = 2,
630 .rates = MXS_SAIF_RATES,
631 .formats = MXS_SAIF_FORMATS,
632 },
633 .ops = &mxs_saif_dai_ops,
634};
635
026240bb
KM
636static const struct snd_soc_component_driver mxs_saif_component = {
637 .name = "mxs-saif",
638};
639
2a24f2ce
DA
640static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
641{
642 struct mxs_saif *saif = dev_id;
643 unsigned int stat;
644
645 stat = __raw_readl(saif->base + SAIF_STAT);
646 if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
647 BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
648 return IRQ_NONE;
649
650 if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
651 dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
652 __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
653 saif->base + SAIF_STAT + MXS_CLR_ADDR);
654 }
655
656 if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
657 dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
658 __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
659 saif->base + SAIF_STAT + MXS_CLR_ADDR);
660 }
661
662 dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
663 __raw_readl(saif->base + SAIF_CTRL),
664 __raw_readl(saif->base + SAIF_STAT));
665
666 return IRQ_HANDLED;
667}
668
7c9e6150
SG
669static int mxs_saif_mclk_init(struct platform_device *pdev)
670{
671 struct mxs_saif *saif = platform_get_drvdata(pdev);
672 struct device_node *np = pdev->dev.of_node;
673 struct clk *clk;
674 int ret;
675
676 clk = clk_register_divider(&pdev->dev, "mxs_saif_mclk",
677 __clk_get_name(saif->clk), 0,
678 saif->base + SAIF_CTRL,
679 BP_SAIF_CTRL_BITCLK_MULT_RATE, 3,
680 0, NULL);
681 if (IS_ERR(clk)) {
682 ret = PTR_ERR(clk);
683 if (ret == -EEXIST)
684 return 0;
685 dev_err(&pdev->dev, "failed to register mclk: %d\n", ret);
686 return PTR_ERR(clk);
687 }
688
689 ret = of_clk_add_provider(np, of_clk_src_simple_get, clk);
690 if (ret)
691 return ret;
692
693 return 0;
694}
695
fd582736 696static int mxs_saif_probe(struct platform_device *pdev)
2a24f2ce 697{
08641c7c 698 struct device_node *np = pdev->dev.of_node;
62477adf 699 struct resource *iores;
2a24f2ce
DA
700 struct mxs_saif *saif;
701 int ret = 0;
4498a3ca 702 struct device_node *master;
2a24f2ce 703
4498a3ca 704 if (!np)
0bb98ba2
JL
705 return -EINVAL;
706
830eb876 707 saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
2a24f2ce
DA
708 if (!saif)
709 return -ENOMEM;
710
324a7fb0
FE
711 ret = of_alias_get_id(np, "saif");
712 if (ret < 0)
713 return ret;
714 else
715 saif->id = ret;
716
4498a3ca
FE
717 /*
718 * If there is no "fsl,saif-master" phandle, it's a saif
719 * master. Otherwise, it's a slave and its phandle points
720 * to the master.
721 */
722 master = of_parse_phandle(np, "fsl,saif-master", 0);
723 if (!master) {
724 saif->master_id = saif->id;
77882580 725 } else {
324a7fb0
FE
726 ret = of_alias_get_id(master, "saif");
727 if (ret < 0)
728 return ret;
729 else
730 saif->master_id = ret;
08641c7c
SG
731 }
732
324a7fb0 733 if (saif->master_id >= ARRAY_SIZE(mxs_saif)) {
08641c7c
SG
734 dev_err(&pdev->dev, "get wrong master id\n");
735 return -EINVAL;
76067540 736 }
2a24f2ce 737
08641c7c
SG
738 mxs_saif[saif->id] = saif;
739
730963f8 740 saif->clk = devm_clk_get(&pdev->dev, NULL);
2a24f2ce
DA
741 if (IS_ERR(saif->clk)) {
742 ret = PTR_ERR(saif->clk);
743 dev_err(&pdev->dev, "Cannot get the clock: %d\n",
744 ret);
830eb876 745 return ret;
2a24f2ce
DA
746 }
747
226d0f22 748 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2a24f2ce 749
b25b5aa0
TR
750 saif->base = devm_ioremap_resource(&pdev->dev, iores);
751 if (IS_ERR(saif->base))
752 return PTR_ERR(saif->base);
2a24f2ce 753
2a24f2ce
DA
754 saif->irq = platform_get_irq(pdev, 0);
755 if (saif->irq < 0) {
756 ret = saif->irq;
757 dev_err(&pdev->dev, "failed to get irq resource: %d\n",
758 ret);
730963f8 759 return ret;
2a24f2ce
DA
760 }
761
762 saif->dev = &pdev->dev;
830eb876
JL
763 ret = devm_request_irq(&pdev->dev, saif->irq, mxs_saif_irq, 0,
764 "mxs-saif", saif);
2a24f2ce
DA
765 if (ret) {
766 dev_err(&pdev->dev, "failed to request irq\n");
730963f8 767 return ret;
2a24f2ce
DA
768 }
769
2a24f2ce
DA
770 platform_set_drvdata(pdev, saif);
771
7c9e6150
SG
772 /* We only support saif0 being tx and clock master */
773 if (saif->id == 0) {
774 ret = mxs_saif_mclk_init(pdev);
775 if (ret)
776 dev_warn(&pdev->dev, "failed to init clocks\n");
777 }
778
026240bb
KM
779 ret = snd_soc_register_component(&pdev->dev, &mxs_saif_component,
780 &mxs_saif_dai, 1);
2a24f2ce
DA
781 if (ret) {
782 dev_err(&pdev->dev, "register DAI failed\n");
730963f8 783 return ret;
2a24f2ce
DA
784 }
785
4da3fe78 786 ret = mxs_pcm_platform_register(&pdev->dev);
2a24f2ce 787 if (ret) {
4da3fe78
SG
788 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
789 goto failed_pdev_alloc;
2a24f2ce
DA
790 }
791
792 return 0;
793
2a24f2ce 794failed_pdev_alloc:
026240bb 795 snd_soc_unregister_component(&pdev->dev);
2a24f2ce
DA
796
797 return ret;
798}
799
fd582736 800static int mxs_saif_remove(struct platform_device *pdev)
2a24f2ce 801{
4da3fe78 802 mxs_pcm_platform_unregister(&pdev->dev);
026240bb 803 snd_soc_unregister_component(&pdev->dev);
2a24f2ce
DA
804
805 return 0;
806}
807
08641c7c
SG
808static const struct of_device_id mxs_saif_dt_ids[] = {
809 { .compatible = "fsl,imx28-saif", },
810 { /* sentinel */ }
811};
812MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
813
2a24f2ce
DA
814static struct platform_driver mxs_saif_driver = {
815 .probe = mxs_saif_probe,
fd582736 816 .remove = mxs_saif_remove,
2a24f2ce
DA
817
818 .driver = {
819 .name = "mxs-saif",
820 .owner = THIS_MODULE,
08641c7c 821 .of_match_table = mxs_saif_dt_ids,
2a24f2ce
DA
822 },
823};
824
85aa0960 825module_platform_driver(mxs_saif_driver);
2a24f2ce 826
2a24f2ce
DA
827MODULE_AUTHOR("Freescale Semiconductor, Inc.");
828MODULE_DESCRIPTION("MXS ASoC SAIF driver");
829MODULE_LICENSE("GPL");
9f4c3f1c 830MODULE_ALIAS("platform:mxs-saif");
This page took 0.134946 seconds and 5 git commands to generate.