blob: 10c264738a0b904f3d027642619517fd46996b08 [file] [log] [blame]
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001/*
2 * ALSA SoC McASP Audio Layer for TI DAVINCI processor
3 *
4 * Multi-channel Audio Serial Port Driver
5 *
6 * Author: Nirmal Pandey <n-pandey@ti.com>,
7 * Suresh Rajashekara <suresh.r@ti.com>
8 * Steve Chen <schen@.mvista.com>
9 *
10 * Copyright: (C) 2009 MontaVista Software, Inc., <source@mvista.com>
11 * Copyright: (C) 2009 Texas Instruments, India
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040022#include <linux/delay.h>
23#include <linux/io.h>
Peter Ujfalusiae726e92013-11-14 11:35:35 +020024#include <linux/clk.h>
Hebbar, Gururaja10884342012-08-08 20:40:32 +053025#include <linux/pm_runtime.h>
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +053026#include <linux/of.h>
27#include <linux/of_platform.h>
28#include <linux/of_device.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040029
Daniel Mack64792852014-03-27 11:27:40 +010030#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/initval.h>
35#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020036#include <sound/dmaengine_pcm.h>
Jyri Sarha87c19362014-05-26 11:51:14 +030037#include <sound/omap-pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040038
39#include "davinci-pcm.h"
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +030040#include "edma-pcm.h"
Chaithrika U Sb67f4482009-06-05 06:28:40 -040041#include "davinci-mcasp.h"
42
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +030043#define MCASP_MAX_AFIFO_DEPTH 64
44
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030045static u32 context_regs[] = {
46 DAVINCI_MCASP_TXFMCTL_REG,
47 DAVINCI_MCASP_RXFMCTL_REG,
48 DAVINCI_MCASP_TXFMT_REG,
49 DAVINCI_MCASP_RXFMT_REG,
50 DAVINCI_MCASP_ACLKXCTL_REG,
51 DAVINCI_MCASP_ACLKRCTL_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030052 DAVINCI_MCASP_AHCLKXCTL_REG,
53 DAVINCI_MCASP_AHCLKRCTL_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030054 DAVINCI_MCASP_PDIR_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030055 DAVINCI_MCASP_RXMASK_REG,
56 DAVINCI_MCASP_TXMASK_REG,
57 DAVINCI_MCASP_RXTDM_REG,
58 DAVINCI_MCASP_TXTDM_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030059};
60
Peter Ujfalusi790bb942014-02-03 14:51:52 +020061struct davinci_mcasp_context {
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030062 u32 config_regs[ARRAY_SIZE(context_regs)];
Peter Ujfalusif114ce62014-10-01 16:02:12 +030063 u32 afifo_regs[2]; /* for read/write fifo control registers */
64 u32 *xrsr_regs; /* for serializer configuration */
Peter Ujfalusi790bb942014-02-03 14:51:52 +020065};
66
Peter Ujfalusi70091a32013-11-14 11:35:29 +020067struct davinci_mcasp {
Peter Ujfalusi21400a72013-11-14 11:35:26 +020068 struct davinci_pcm_dma_params dma_params[2];
Peter Ujfalusi453c4992013-11-14 11:35:34 +020069 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020070 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020071 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020072 struct device *dev;
73
74 /* McASP specific data */
75 int tdm_slots;
76 u8 op_mode;
77 u8 num_serializer;
78 u8 *serial_dir;
79 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020080 u8 bclk_div;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020081 u16 bclk_lrclk_ratio;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020082 int streams;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020083
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020084 int sysclk_freq;
85 bool bclk_master;
86
Peter Ujfalusi21400a72013-11-14 11:35:26 +020087 /* McASP FIFO related */
88 u8 txnumevt;
89 u8 rxnumevt;
90
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +020091 bool dat_port;
92
Peter Ujfalusi21400a72013-11-14 11:35:26 +020093#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi790bb942014-02-03 14:51:52 +020094 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020095#endif
96};
97
Peter Ujfalusif68205a2013-11-14 11:35:36 +020098static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
99 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400100{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200101 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400102 __raw_writel(__raw_readl(reg) | val, reg);
103}
104
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200105static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
106 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400107{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200108 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400109 __raw_writel((__raw_readl(reg) & ~(val)), reg);
110}
111
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200112static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
113 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400114{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200115 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400116 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
117}
118
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200119static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
120 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400121{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200122 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400123}
124
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200125static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400126{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200127 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400128}
129
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200130static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400131{
132 int i = 0;
133
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200134 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400135
136 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
137 /* loop count is to avoid the lock-up */
138 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200139 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400140 break;
141 }
142
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200143 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400144 printk(KERN_ERR "GBLCTL write error\n");
145}
146
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200147static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
148{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200149 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
150 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200151
152 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
153}
154
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200155static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400156{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200157 if (mcasp->rxnumevt) { /* enable FIFO */
158 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
159
160 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
161 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
162 }
163
Peter Ujfalusi44982732014-10-29 13:55:45 +0200164 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200165 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
166 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200167 /*
168 * When ASYNC == 0 the transmit and receive sections operate
169 * synchronously from the transmit clock and frame sync. We need to make
170 * sure that the TX signlas are enabled when starting reception.
171 */
172 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200173 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
174 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200175 }
176
Peter Ujfalusi44982732014-10-29 13:55:45 +0200177 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200178 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200179 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200180 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200181 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200182 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200183 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200184 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400185}
186
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200187static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400188{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400189 u32 cnt;
190
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200191 if (mcasp->txnumevt) { /* enable FIFO */
192 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
193
194 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
195 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
196 }
197
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200198 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200199 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
200 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200201 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200202 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400203
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200204 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400205 cnt = 0;
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200206 while (!(mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) &
207 ~XRDATA) && (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400208 cnt++;
209
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200210 /* Release TX state machine */
211 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
212 /* Release Frame Sync generator */
213 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400214}
215
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200216static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400217{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200218 mcasp->streams++;
219
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200220 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200221 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200222 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200223 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400224}
225
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200226static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400227{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200228 /*
229 * In synchronous mode stop the TX clocks if no other stream is
230 * running
231 */
232 if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200233 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200234
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200235 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
236 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200237
238 if (mcasp->rxnumevt) { /* disable FIFO */
239 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
240
241 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
242 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400243}
244
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200245static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400246{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200247 u32 val = 0;
248
249 /*
250 * In synchronous mode keep TX clocks running if the capture stream is
251 * still running.
252 */
253 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
254 val = TXHCLKRST | TXCLKRST | TXFSRST;
255
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200256 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
257 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200258
259 if (mcasp->txnumevt) { /* disable FIFO */
260 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
261
262 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
263 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400264}
265
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200266static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400267{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200268 mcasp->streams--;
269
Peter Ujfalusi03808662014-10-29 13:55:46 +0200270 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200271 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200272 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200273 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400274}
275
276static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
277 unsigned int fmt)
278{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200279 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200280 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300281 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300282 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300283 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400284
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200285 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200286 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300287 case SND_SOC_DAIFMT_DSP_A:
288 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
289 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300290 /* 1st data bit occur one ACLK cycle after the frame sync */
291 data_delay = 1;
292 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200293 case SND_SOC_DAIFMT_DSP_B:
294 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200295 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
296 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300297 /* No delay after FS */
298 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200299 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300300 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200301 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200302 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
303 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300304 /* 1st data bit occur one ACLK cycle after the frame sync */
305 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300306 /* FS need to be inverted */
307 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200308 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300309 case SND_SOC_DAIFMT_LEFT_J:
310 /* configure a full-word SYNC pulse (LRCLK) */
311 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
312 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
313 /* No delay after FS */
314 data_delay = 0;
315 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300316 default:
317 ret = -EINVAL;
318 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200319 }
320
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300321 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
322 FSXDLY(3));
323 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
324 FSRDLY(3));
325
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400326 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
327 case SND_SOC_DAIFMT_CBS_CFS:
328 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200329 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
330 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400331
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200332 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
333 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400334
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200335 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
336 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200337 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400338 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400339 case SND_SOC_DAIFMT_CBM_CFS:
340 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200341 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
342 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400343
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200344 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
345 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400346
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200347 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
348 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200349 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400350 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400351 case SND_SOC_DAIFMT_CBM_CFM:
352 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200353 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
354 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400355
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200356 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
357 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400358
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200359 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
360 ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200361 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400362 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400363 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200364 ret = -EINVAL;
365 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400366 }
367
368 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
369 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200370 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300371 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300372 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400373 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400374 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200375 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300376 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300377 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400378 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400379 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200380 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300381 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300382 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400383 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400384 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200385 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200386 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300387 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400388 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400389 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200390 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300391 goto out;
392 }
393
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300394 if (inv_fs)
395 fs_pol_rising = !fs_pol_rising;
396
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300397 if (fs_pol_rising) {
398 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
399 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
400 } else {
401 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
402 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400403 }
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200404out:
405 pm_runtime_put_sync(mcasp->dev);
406 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400407}
408
Jyri Sarha88135432014-08-06 16:47:16 +0300409static int __davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
410 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200411{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200412 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200413
414 switch (div_id) {
415 case 0: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200416 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200417 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200418 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200419 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
420 break;
421
422 case 1: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200423 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200424 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200425 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200426 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300427 if (explicit)
428 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200429 break;
430
Daniel Mack1b3bc062012-12-05 18:20:38 +0100431 case 2: /* BCLK/LRCLK ratio */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200432 mcasp->bclk_lrclk_ratio = div;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100433 break;
434
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200435 default:
436 return -EINVAL;
437 }
438
439 return 0;
440}
441
Jyri Sarha88135432014-08-06 16:47:16 +0300442static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
443 int div)
444{
445 return __davinci_mcasp_set_clkdiv(dai, div_id, div, 1);
446}
447
Daniel Mack5b66aa22012-10-04 15:08:41 +0200448static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
449 unsigned int freq, int dir)
450{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200451 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200452
453 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200454 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
455 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
456 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200457 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200458 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
459 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
460 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200461 }
462
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200463 mcasp->sysclk_freq = freq;
464
Daniel Mack5b66aa22012-10-04 15:08:41 +0200465 return 0;
466}
467
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200468static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Daniel Mackba764b32012-12-05 18:20:37 +0100469 int word_length)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400470{
Daniel Mackba764b32012-12-05 18:20:37 +0100471 u32 fmt;
Daniel Mack79671892013-05-16 15:25:01 +0200472 u32 tx_rotate = (word_length / 4) & 0x7;
Daniel Mackba764b32012-12-05 18:20:37 +0100473 u32 mask = (1ULL << word_length) - 1;
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300474 /*
475 * For captured data we should not rotate, inversion and masking is
476 * enoguh to get the data to the right position:
477 * Format data from bus after reverse (XRBUF)
478 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
479 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
480 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
481 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
482 */
483 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400484
Daniel Mack1b3bc062012-12-05 18:20:38 +0100485 /*
486 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
487 * callback, take it into account here. That allows us to for example
488 * send 32 bits per channel to the codec, while only 16 of them carry
489 * audio payload.
Michal Bachratyd486fea2013-04-19 15:28:44 +0200490 * The clock ratio is given for a full period of data (for I2S format
491 * both left and right channels), so it has to be divided by number of
492 * tdm-slots (for I2S - divided by 2).
Daniel Mack1b3bc062012-12-05 18:20:38 +0100493 */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200494 if (mcasp->bclk_lrclk_ratio)
495 word_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100496
Daniel Mackba764b32012-12-05 18:20:37 +0100497 /* mapping of the XSSZ bit-field as described in the datasheet */
498 fmt = (word_length >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400499
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200500 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200501 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
502 RXSSZ(0x0F));
503 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
504 TXSSZ(0x0F));
505 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
506 TXROT(7));
507 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
508 RXROT(7));
509 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200510 }
511
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200512 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400513
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400514 return 0;
515}
516
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200517static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300518 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400519{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300520 struct davinci_pcm_dma_params *dma_params = &mcasp->dma_params[stream];
521 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400522 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400523 u8 tx_ser = 0;
524 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200525 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100526 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300527 int active_serializers, numevt, n;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200528 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400529 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300530 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200531 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400532
533 /* All PINS as McASP */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200534 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400535
536 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200537 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
538 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400539 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200540 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
541 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400542 }
543
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200544 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200545 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
546 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200547 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100548 tx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200549 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400550 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200551 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100552 rx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200553 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400554 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100555 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200556 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
557 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400558 }
559 }
560
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300561 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
562 active_serializers = tx_ser;
563 numevt = mcasp->txnumevt;
564 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
565 } else {
566 active_serializers = rx_ser;
567 numevt = mcasp->rxnumevt;
568 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
569 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100570
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300571 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200572 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300573 "enabled in mcasp (%d)\n", channels,
574 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100575 return -EINVAL;
576 }
577
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300578 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300579 if (!numevt) {
580 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300581 if (active_serializers > 1) {
582 /*
583 * If more than one serializers are in use we have one
584 * DMA request to provide data for all serializers.
585 * For example if three serializers are enabled the DMA
586 * need to transfer three words per DMA request.
587 */
588 dma_params->fifo_level = active_serializers;
589 dma_data->maxburst = active_serializers;
590 } else {
591 dma_params->fifo_level = 0;
592 dma_data->maxburst = 0;
593 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300594 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300595 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400596
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300597 if (period_words % active_serializers) {
598 dev_err(mcasp->dev, "Invalid combination of period words and "
599 "active serializers: %d, %d\n", period_words,
600 active_serializers);
601 return -EINVAL;
602 }
603
604 /*
605 * Calculate the optimal AFIFO depth for platform side:
606 * The number of words for numevt need to be in steps of active
607 * serializers.
608 */
609 n = numevt % active_serializers;
610 if (n)
611 numevt += (active_serializers - n);
612 while (period_words % numevt && numevt > 0)
613 numevt -= active_serializers;
614 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300615 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400616
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300617 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
618 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100619
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300620 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300621 if (numevt == 1)
622 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300623 dma_params->fifo_level = numevt;
624 dma_data->maxburst = numevt;
625
Michal Bachraty2952b272013-02-28 16:07:08 +0100626 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400627}
628
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200629static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400630{
631 int i, active_slots;
632 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200633 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400634
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +0200635 active_slots = mcasp->tdm_slots;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400636 for (i = 0; i < active_slots; i++)
637 mask |= (1 << i);
638
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200639 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400640
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200641 if (!mcasp->dat_port)
642 busel = TXSEL;
643
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200644 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
645 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
646 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +0200647 FSXMOD(active_slots), FSXMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400648
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200649 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
650 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
651 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +0200652 FSRMOD(active_slots), FSRMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400653
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200654 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400655}
656
657/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100658static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
659 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400660{
Daniel Mack64792852014-03-27 11:27:40 +0100661 u32 cs_value = 0;
662 u8 *cs_bytes = (u8*) &cs_value;
663
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400664 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
665 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200666 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400667
668 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200669 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400670
671 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200672 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400673
674 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200675 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400676
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200677 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400678
679 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200680 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400681
682 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200683 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200684
Daniel Mack64792852014-03-27 11:27:40 +0100685 /* Set S/PDIF channel status bits */
686 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
687 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
688
689 switch (rate) {
690 case 22050:
691 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
692 break;
693 case 24000:
694 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
695 break;
696 case 32000:
697 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
698 break;
699 case 44100:
700 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
701 break;
702 case 48000:
703 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
704 break;
705 case 88200:
706 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
707 break;
708 case 96000:
709 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
710 break;
711 case 176400:
712 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
713 break;
714 case 192000:
715 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
716 break;
717 default:
718 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
719 return -EINVAL;
720 }
721
722 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
723 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
724
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200725 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400726}
727
728static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
729 struct snd_pcm_hw_params *params,
730 struct snd_soc_dai *cpu_dai)
731{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200732 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400733 struct davinci_pcm_dma_params *dma_params =
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200734 &mcasp->dma_params[substream->stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400735 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +0200736 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300737 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200738 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200739
Daniel Mack82675252014-07-16 14:04:41 +0200740 /*
741 * If mcasp is BCLK master, and a BCLK divider was not provided by
742 * the machine driver, we need to calculate the ratio.
743 */
744 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200745 unsigned int bclk_freq = snd_soc_params_to_bclk(params);
Jyri Sarha09298782014-06-13 12:50:00 +0300746 unsigned int div = mcasp->sysclk_freq / bclk_freq;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200747 if (mcasp->sysclk_freq % bclk_freq != 0) {
Jyri Sarha09298782014-06-13 12:50:00 +0300748 if (((mcasp->sysclk_freq / div) - bclk_freq) >
749 (bclk_freq - (mcasp->sysclk_freq / (div+1))))
750 div++;
751 dev_warn(mcasp->dev,
752 "Inaccurate BCLK: %u Hz / %u != %u Hz\n",
753 mcasp->sysclk_freq, div, bclk_freq);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200754 }
Jyri Sarha88135432014-08-06 16:47:16 +0300755 __davinci_mcasp_set_clkdiv(cpu_dai, 1, div, 0);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200756 }
757
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300758 ret = mcasp_common_hw_param(mcasp, substream->stream,
759 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +0200760 if (ret)
761 return ret;
762
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200763 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +0100764 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400765 else
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200766 ret = mcasp_i2s_hw_param(mcasp, substream->stream);
767
768 if (ret)
769 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400770
771 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400772 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400773 case SNDRV_PCM_FORMAT_S8:
774 dma_params->data_type = 1;
Daniel Mackba764b32012-12-05 18:20:37 +0100775 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400776 break;
777
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400778 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400779 case SNDRV_PCM_FORMAT_S16_LE:
780 dma_params->data_type = 2;
Daniel Mackba764b32012-12-05 18:20:37 +0100781 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400782 break;
783
Daniel Mack21eb24d2012-10-09 09:35:16 +0200784 case SNDRV_PCM_FORMAT_U24_3LE:
785 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mack21eb24d2012-10-09 09:35:16 +0200786 dma_params->data_type = 3;
Daniel Mackba764b32012-12-05 18:20:37 +0100787 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +0200788 break;
789
Daniel Mack6b7fa012012-10-09 11:56:40 +0200790 case SNDRV_PCM_FORMAT_U24_LE:
791 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +0300792 dma_params->data_type = 4;
793 word_length = 24;
794 break;
795
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400796 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400797 case SNDRV_PCM_FORMAT_S32_LE:
798 dma_params->data_type = 4;
Daniel Mackba764b32012-12-05 18:20:37 +0100799 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400800 break;
801
802 default:
803 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
804 return -EINVAL;
805 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400806
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300807 if (mcasp->version == MCASP_VERSION_2 && !dma_params->fifo_level)
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400808 dma_params->acnt = 4;
809 else
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400810 dma_params->acnt = dma_params->data_type;
811
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200812 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400813
814 return 0;
815}
816
817static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
818 int cmd, struct snd_soc_dai *cpu_dai)
819{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200820 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400821 int ret = 0;
822
823 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400824 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +0530825 case SNDRV_PCM_TRIGGER_START:
826 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200827 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400828 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400829 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530830 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400831 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200832 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400833 break;
834
835 default:
836 ret = -EINVAL;
837 }
838
839 return ret;
840}
841
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100842static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400843 .trigger = davinci_mcasp_trigger,
844 .hw_params = davinci_mcasp_hw_params,
845 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200846 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +0200847 .set_sysclk = davinci_mcasp_set_sysclk,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400848};
849
Peter Ujfalusid5902f62014-04-01 15:55:07 +0300850static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
851{
852 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
853
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +0300854 if (mcasp->version >= MCASP_VERSION_3) {
Peter Ujfalusid5902f62014-04-01 15:55:07 +0300855 /* Using dmaengine PCM */
856 dai->playback_dma_data =
857 &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
858 dai->capture_dma_data =
859 &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
860 } else {
861 /* Using davinci-pcm */
862 dai->playback_dma_data = mcasp->dma_params;
863 dai->capture_dma_data = mcasp->dma_params;
864 }
865
866 return 0;
867}
868
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200869#ifdef CONFIG_PM_SLEEP
870static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
871{
872 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200873 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +0300874 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +0300875 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200876
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +0300877 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
878 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200879
Peter Ujfalusif114ce62014-10-01 16:02:12 +0300880 if (mcasp->txnumevt) {
881 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
882 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
883 }
884 if (mcasp->rxnumevt) {
885 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
886 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
887 }
888
889 for (i = 0; i < mcasp->num_serializer; i++)
890 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
891 DAVINCI_MCASP_XRSRCTL_REG(i));
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200892
893 return 0;
894}
895
896static int davinci_mcasp_resume(struct snd_soc_dai *dai)
897{
898 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200899 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +0300900 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +0300901 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200902
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +0300903 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
904 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200905
Peter Ujfalusif114ce62014-10-01 16:02:12 +0300906 if (mcasp->txnumevt) {
907 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
908 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
909 }
910 if (mcasp->rxnumevt) {
911 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
912 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
913 }
914
915 for (i = 0; i < mcasp->num_serializer; i++)
916 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
917 context->xrsr_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200918
919 return 0;
920}
921#else
922#define davinci_mcasp_suspend NULL
923#define davinci_mcasp_resume NULL
924#endif
925
Peter Ujfalusied29cd52013-11-14 11:35:22 +0200926#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
927
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400928#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
929 SNDRV_PCM_FMTBIT_U8 | \
930 SNDRV_PCM_FMTBIT_S16_LE | \
931 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +0200932 SNDRV_PCM_FMTBIT_S24_LE | \
933 SNDRV_PCM_FMTBIT_U24_LE | \
934 SNDRV_PCM_FMTBIT_S24_3LE | \
935 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400936 SNDRV_PCM_FMTBIT_S32_LE | \
937 SNDRV_PCM_FMTBIT_U32_LE)
938
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000939static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400940 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f62014-04-01 15:55:07 +0300942 .probe = davinci_mcasp_dai_probe,
Peter Ujfalusi135014a2014-01-30 15:21:32 +0200943 .suspend = davinci_mcasp_suspend,
944 .resume = davinci_mcasp_resume,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400945 .playback = {
946 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100947 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400948 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400949 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400950 },
951 .capture = {
952 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100953 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400954 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400955 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400956 },
957 .ops = &davinci_mcasp_dai_ops,
958
959 },
960 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +0200961 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f62014-04-01 15:55:07 +0300962 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400963 .playback = {
964 .channels_min = 1,
965 .channels_max = 384,
966 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400967 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400968 },
969 .ops = &davinci_mcasp_dai_ops,
970 },
971
972};
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400973
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -0700974static const struct snd_soc_component_driver davinci_mcasp_component = {
975 .name = "davinci-mcasp",
976};
977
Jyri Sarha256ba182013-10-18 18:37:42 +0300978/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +0200979static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +0300980 .tx_dma_offset = 0x400,
981 .rx_dma_offset = 0x400,
982 .asp_chan_q = EVENTQ_0,
983 .version = MCASP_VERSION_1,
984};
985
Peter Ujfalusid1debaf2014-02-03 14:51:51 +0200986static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +0300987 .tx_dma_offset = 0x2000,
988 .rx_dma_offset = 0x2000,
989 .asp_chan_q = EVENTQ_0,
990 .version = MCASP_VERSION_2,
991};
992
Peter Ujfalusid1debaf2014-02-03 14:51:51 +0200993static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +0300994 .tx_dma_offset = 0,
995 .rx_dma_offset = 0,
996 .asp_chan_q = EVENTQ_0,
997 .version = MCASP_VERSION_3,
998};
999
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001000static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001001 .tx_dma_offset = 0x200,
1002 .rx_dma_offset = 0x284,
1003 .asp_chan_q = EVENTQ_0,
1004 .version = MCASP_VERSION_4,
1005};
1006
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301007static const struct of_device_id mcasp_dt_ids[] = {
1008 {
1009 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001010 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301011 },
1012 {
1013 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001014 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301015 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301016 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001017 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001018 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301019 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001020 {
1021 .compatible = "ti,dra7-mcasp-audio",
1022 .data = &dra7_mcasp_pdata,
1023 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301024 { /* sentinel */ }
1025};
1026MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1027
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001028static int mcasp_reparent_fck(struct platform_device *pdev)
1029{
1030 struct device_node *node = pdev->dev.of_node;
1031 struct clk *gfclk, *parent_clk;
1032 const char *parent_name;
1033 int ret;
1034
1035 if (!node)
1036 return 0;
1037
1038 parent_name = of_get_property(node, "fck_parent", NULL);
1039 if (!parent_name)
1040 return 0;
1041
1042 gfclk = clk_get(&pdev->dev, "fck");
1043 if (IS_ERR(gfclk)) {
1044 dev_err(&pdev->dev, "failed to get fck\n");
1045 return PTR_ERR(gfclk);
1046 }
1047
1048 parent_clk = clk_get(NULL, parent_name);
1049 if (IS_ERR(parent_clk)) {
1050 dev_err(&pdev->dev, "failed to get parent clock\n");
1051 ret = PTR_ERR(parent_clk);
1052 goto err1;
1053 }
1054
1055 ret = clk_set_parent(gfclk, parent_clk);
1056 if (ret) {
1057 dev_err(&pdev->dev, "failed to reparent fck\n");
1058 goto err2;
1059 }
1060
1061err2:
1062 clk_put(parent_clk);
1063err1:
1064 clk_put(gfclk);
1065 return ret;
1066}
1067
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001068static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301069 struct platform_device *pdev)
1070{
1071 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001072 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301073 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301074 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001075 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301076
1077 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301078 u32 val;
1079 int i, ret = 0;
1080
1081 if (pdev->dev.platform_data) {
1082 pdata = pdev->dev.platform_data;
1083 return pdata;
1084 } else if (match) {
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001085 pdata = (struct davinci_mcasp_pdata*) match->data;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301086 } else {
1087 /* control shouldn't reach here. something is wrong */
1088 ret = -EINVAL;
1089 goto nodata;
1090 }
1091
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301092 ret = of_property_read_u32(np, "op-mode", &val);
1093 if (ret >= 0)
1094 pdata->op_mode = val;
1095
1096 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001097 if (ret >= 0) {
1098 if (val < 2 || val > 32) {
1099 dev_err(&pdev->dev,
1100 "tdm-slots must be in rage [2-32]\n");
1101 ret = -EINVAL;
1102 goto nodata;
1103 }
1104
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301105 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001106 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301107
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301108 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1109 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301110 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001111 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1112 (sizeof(*of_serial_dir) * val),
1113 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301114 if (!of_serial_dir) {
1115 ret = -ENOMEM;
1116 goto nodata;
1117 }
1118
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001119 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301120 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1121
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001122 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301123 pdata->serial_dir = of_serial_dir;
1124 }
1125
Jyri Sarha4023fe62013-10-18 18:37:43 +03001126 ret = of_property_match_string(np, "dma-names", "tx");
1127 if (ret < 0)
1128 goto nodata;
1129
1130 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1131 &dma_spec);
1132 if (ret < 0)
1133 goto nodata;
1134
1135 pdata->tx_dma_channel = dma_spec.args[0];
1136
1137 ret = of_property_match_string(np, "dma-names", "rx");
1138 if (ret < 0)
1139 goto nodata;
1140
1141 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1142 &dma_spec);
1143 if (ret < 0)
1144 goto nodata;
1145
1146 pdata->rx_dma_channel = dma_spec.args[0];
1147
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301148 ret = of_property_read_u32(np, "tx-num-evt", &val);
1149 if (ret >= 0)
1150 pdata->txnumevt = val;
1151
1152 ret = of_property_read_u32(np, "rx-num-evt", &val);
1153 if (ret >= 0)
1154 pdata->rxnumevt = val;
1155
1156 ret = of_property_read_u32(np, "sram-size-playback", &val);
1157 if (ret >= 0)
1158 pdata->sram_size_playback = val;
1159
1160 ret = of_property_read_u32(np, "sram-size-capture", &val);
1161 if (ret >= 0)
1162 pdata->sram_size_capture = val;
1163
1164 return pdata;
1165
1166nodata:
1167 if (ret < 0) {
1168 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1169 ret);
1170 pdata = NULL;
1171 }
1172 return pdata;
1173}
1174
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001175static int davinci_mcasp_probe(struct platform_device *pdev)
1176{
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001177 struct davinci_pcm_dma_params *dma_params;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001178 struct snd_dmaengine_dai_dma_data *dma_data;
Jyri Sarha256ba182013-10-18 18:37:42 +03001179 struct resource *mem, *ioarea, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001180 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001181 struct davinci_mcasp *mcasp;
Julia Lawall96d31e22011-12-29 17:51:21 +01001182 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001183
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301184 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
1185 dev_err(&pdev->dev, "No platform data supplied\n");
1186 return -EINVAL;
1187 }
1188
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001189 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01001190 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001191 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001192 return -ENOMEM;
1193
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301194 pdata = davinci_mcasp_set_pdata_from_of(pdev);
1195 if (!pdata) {
1196 dev_err(&pdev->dev, "no platform data\n");
1197 return -EINVAL;
1198 }
1199
Jyri Sarha256ba182013-10-18 18:37:42 +03001200 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001201 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001202 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03001203 "\"mpu\" mem resource not found, using index 0\n");
1204 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1205 if (!mem) {
1206 dev_err(&pdev->dev, "no mem resource?\n");
1207 return -ENODEV;
1208 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001209 }
1210
Julia Lawall96d31e22011-12-29 17:51:21 +01001211 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
Vaibhav Bediad852f4462011-02-09 18:39:52 +05301212 resource_size(mem), pdev->name);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001213 if (!ioarea) {
1214 dev_err(&pdev->dev, "Audio region already claimed\n");
Julia Lawall96d31e22011-12-29 17:51:21 +01001215 return -EBUSY;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001216 }
1217
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301218 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001219
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301220 ret = pm_runtime_get_sync(&pdev->dev);
1221 if (IS_ERR_VALUE(ret)) {
1222 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
1223 return ret;
1224 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001225
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001226 mcasp->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
1227 if (!mcasp->base) {
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301228 dev_err(&pdev->dev, "ioremap failed\n");
1229 ret = -ENOMEM;
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001230 goto err;
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301231 }
1232
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001233 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02001234 /* sanity check for tdm slots parameter */
1235 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1236 if (pdata->tdm_slots < 2) {
1237 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1238 pdata->tdm_slots);
1239 mcasp->tdm_slots = 2;
1240 } else if (pdata->tdm_slots > 32) {
1241 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1242 pdata->tdm_slots);
1243 mcasp->tdm_slots = 32;
1244 } else {
1245 mcasp->tdm_slots = pdata->tdm_slots;
1246 }
1247 }
1248
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001249 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001250#ifdef CONFIG_PM_SLEEP
1251 mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
1252 sizeof(u32) * mcasp->num_serializer,
1253 GFP_KERNEL);
1254#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001255 mcasp->serial_dir = pdata->serial_dir;
1256 mcasp->version = pdata->version;
1257 mcasp->txnumevt = pdata->txnumevt;
1258 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02001259
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001260 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001261
Jyri Sarha256ba182013-10-18 18:37:42 +03001262 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001263 if (dat)
1264 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03001265
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001266 dma_params = &mcasp->dma_params[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001267 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001268 dma_params->asp_chan_q = pdata->asp_chan_q;
1269 dma_params->ram_chan_q = pdata->ram_chan_q;
1270 dma_params->sram_pool = pdata->sram_pool;
1271 dma_params->sram_size = pdata->sram_size_playback;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001272 if (dat)
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001273 dma_params->dma_addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001274 else
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001275 dma_params->dma_addr = mem->start + pdata->tx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001276
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001277 /* Unconditional dmaengine stuff */
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001278 dma_data->addr = dma_params->dma_addr;
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001279
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001280 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001281 if (res)
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001282 dma_params->channel = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001283 else
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001284 dma_params->channel = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07001285
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001286 /* dmaengine filter data for DT and non-DT boot */
1287 if (pdev->dev.of_node)
1288 dma_data->filter_data = "tx";
1289 else
1290 dma_data->filter_data = &dma_params->channel;
1291
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001292 dma_params = &mcasp->dma_params[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001293 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001294 dma_params->asp_chan_q = pdata->asp_chan_q;
1295 dma_params->ram_chan_q = pdata->ram_chan_q;
1296 dma_params->sram_pool = pdata->sram_pool;
1297 dma_params->sram_size = pdata->sram_size_capture;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001298 if (dat)
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001299 dma_params->dma_addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001300 else
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001301 dma_params->dma_addr = mem->start + pdata->rx_dma_offset;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001302
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001303 /* Unconditional dmaengine stuff */
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001304 dma_data->addr = dma_params->dma_addr;
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001305
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001306 if (mcasp->version < MCASP_VERSION_3) {
1307 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001308 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001309 mcasp->dat_port = true;
1310 } else {
1311 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
1312 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001313
1314 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001315 if (res)
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001316 dma_params->channel = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001317 else
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001318 dma_params->channel = pdata->rx_dma_channel;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001319
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001320 /* dmaengine filter data for DT and non-DT boot */
1321 if (pdev->dev.of_node)
1322 dma_data->filter_data = "rx";
1323 else
1324 dma_data->filter_data = &dma_params->channel;
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001325
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001326 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001327
1328 mcasp_reparent_fck(pdev);
1329
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001330 ret = devm_snd_soc_register_component(&pdev->dev,
1331 &davinci_mcasp_component,
1332 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001333
1334 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001335 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301336
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001337 switch (mcasp->version) {
Jyri Sarha7f28f352014-06-13 12:49:59 +03001338#if IS_BUILTIN(CONFIG_SND_DAVINCI_SOC) || \
1339 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1340 IS_MODULE(CONFIG_SND_DAVINCI_SOC))
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001341 case MCASP_VERSION_1:
1342 case MCASP_VERSION_2:
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001343 ret = davinci_soc_platform_register(&pdev->dev);
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001344 break;
Jyri Sarha7f28f352014-06-13 12:49:59 +03001345#endif
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03001346#if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
1347 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1348 IS_MODULE(CONFIG_SND_EDMA_SOC))
1349 case MCASP_VERSION_3:
1350 ret = edma_pcm_platform_register(&pdev->dev);
1351 break;
1352#endif
Jyri Sarha7f28f352014-06-13 12:49:59 +03001353#if IS_BUILTIN(CONFIG_SND_OMAP_SOC) || \
1354 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1355 IS_MODULE(CONFIG_SND_OMAP_SOC))
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001356 case MCASP_VERSION_4:
1357 ret = omap_pcm_platform_register(&pdev->dev);
1358 break;
Jyri Sarha7f28f352014-06-13 12:49:59 +03001359#endif
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001360 default:
1361 dev_err(&pdev->dev, "Invalid McASP version: %d\n",
1362 mcasp->version);
1363 ret = -EINVAL;
1364 break;
1365 }
1366
1367 if (ret) {
1368 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001369 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301370 }
1371
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001372 return 0;
1373
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001374err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301375 pm_runtime_put_sync(&pdev->dev);
1376 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001377 return ret;
1378}
1379
1380static int davinci_mcasp_remove(struct platform_device *pdev)
1381{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301382 pm_runtime_put_sync(&pdev->dev);
1383 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001384
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001385 return 0;
1386}
1387
1388static struct platform_driver davinci_mcasp_driver = {
1389 .probe = davinci_mcasp_probe,
1390 .remove = davinci_mcasp_remove,
1391 .driver = {
1392 .name = "davinci-mcasp",
1393 .owner = THIS_MODULE,
Sachin Kamatea421eb2013-05-22 16:53:37 +05301394 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001395 },
1396};
1397
Axel Linf9b8a512011-11-25 10:09:27 +08001398module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001399
1400MODULE_AUTHOR("Steve Chen");
1401MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1402MODULE_LICENSE("GPL");