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