blob: 0c882995a3575a05841de42fae80015b9b7611a5 [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>
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020029#include <linux/platform_data/davinci_asp.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040030
Daniel Mack64792852014-03-27 11:27:40 +010031#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040032#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/initval.h>
36#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020037#include <sound/dmaengine_pcm.h>
Jyri Sarha87c19362014-05-26 11:51:14 +030038#include <sound/omap-pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040039
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 Ujfalusi453c4992013-11-14 11:35:34 +020068 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020069 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020070 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020071 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020072 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020073
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;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020083 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020084 int dma_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020085
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020086 int sysclk_freq;
87 bool bclk_master;
88
Peter Ujfalusi21400a72013-11-14 11:35:26 +020089 /* McASP FIFO related */
90 u8 txnumevt;
91 u8 rxnumevt;
92
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +020093 bool dat_port;
94
Peter Ujfalusi11277832014-11-10 12:32:16 +020095 /* Used for comstraint setting on the second stream */
96 u32 channels;
97
Peter Ujfalusi21400a72013-11-14 11:35:26 +020098#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi790bb942014-02-03 14:51:52 +020099 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200100#endif
101};
102
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200103static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
104 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400105{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200106 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400107 __raw_writel(__raw_readl(reg) | val, reg);
108}
109
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200110static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
111 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400112{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200113 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400114 __raw_writel((__raw_readl(reg) & ~(val)), reg);
115}
116
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200117static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
118 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400119{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200120 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400121 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
122}
123
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200124static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
125 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400126{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200127 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400128}
129
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200130static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400131{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200132 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400133}
134
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200135static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400136{
137 int i = 0;
138
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200139 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400140
141 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
142 /* loop count is to avoid the lock-up */
143 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200144 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400145 break;
146 }
147
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200148 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400149 printk(KERN_ERR "GBLCTL write error\n");
150}
151
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200152static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
153{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200154 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
155 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200156
157 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
158}
159
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200160static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400161{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200162 if (mcasp->rxnumevt) { /* enable FIFO */
163 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
164
165 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
166 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
167 }
168
Peter Ujfalusi44982732014-10-29 13:55:45 +0200169 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200170 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
171 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200172 /*
173 * When ASYNC == 0 the transmit and receive sections operate
174 * synchronously from the transmit clock and frame sync. We need to make
175 * sure that the TX signlas are enabled when starting reception.
176 */
177 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200178 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
179 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200180 }
181
Peter Ujfalusi44982732014-10-29 13:55:45 +0200182 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200183 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200184 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200185 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200186 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200187 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200188 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200189 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200190
191 /* enable receive IRQs */
192 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
193 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400194}
195
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200196static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400197{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400198 u32 cnt;
199
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200200 if (mcasp->txnumevt) { /* enable FIFO */
201 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
202
203 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
204 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
205 }
206
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200207 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200208 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
209 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200210 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200211 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400212
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200213 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400214 cnt = 0;
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200215 while (!(mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) &
216 ~XRDATA) && (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400217 cnt++;
218
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200219 /* Release TX state machine */
220 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
221 /* Release Frame Sync generator */
222 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200223
224 /* enable transmit IRQs */
225 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
226 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400227}
228
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200229static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400230{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200231 mcasp->streams++;
232
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200233 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200234 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200235 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200236 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400237}
238
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200239static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400240{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200241 /* disable IRQ sources */
242 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
243 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
244
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200245 /*
246 * In synchronous mode stop the TX clocks if no other stream is
247 * running
248 */
249 if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200250 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200251
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200252 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
253 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200254
255 if (mcasp->rxnumevt) { /* disable FIFO */
256 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
257
258 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
259 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400260}
261
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200262static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400263{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200264 u32 val = 0;
265
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200266 /* disable IRQ sources */
267 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
268 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
269
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200270 /*
271 * In synchronous mode keep TX clocks running if the capture stream is
272 * still running.
273 */
274 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
275 val = TXHCLKRST | TXCLKRST | TXFSRST;
276
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200277 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
278 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200279
280 if (mcasp->txnumevt) { /* disable FIFO */
281 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
282
283 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
284 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400285}
286
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200287static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400288{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200289 mcasp->streams--;
290
Peter Ujfalusi03808662014-10-29 13:55:46 +0200291 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200292 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200293 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200294 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400295}
296
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200297static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
298{
299 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
300 struct snd_pcm_substream *substream;
301 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
302 u32 handled_mask = 0;
303 u32 stat;
304
305 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
306 if (stat & XUNDRN & irq_mask) {
307 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
308 handled_mask |= XUNDRN;
309
310 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
311 if (substream) {
312 snd_pcm_stream_lock_irq(substream);
313 if (snd_pcm_running(substream))
314 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
315 snd_pcm_stream_unlock_irq(substream);
316 }
317 }
318
319 if (!handled_mask)
320 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
321 stat);
322
323 if (stat & XRERR)
324 handled_mask |= XRERR;
325
326 /* Ack the handled event only */
327 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
328
329 return IRQ_RETVAL(handled_mask);
330}
331
332static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
333{
334 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
335 struct snd_pcm_substream *substream;
336 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
337 u32 handled_mask = 0;
338 u32 stat;
339
340 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
341 if (stat & ROVRN & irq_mask) {
342 dev_warn(mcasp->dev, "Receive buffer overflow\n");
343 handled_mask |= ROVRN;
344
345 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
346 if (substream) {
347 snd_pcm_stream_lock_irq(substream);
348 if (snd_pcm_running(substream))
349 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
350 snd_pcm_stream_unlock_irq(substream);
351 }
352 }
353
354 if (!handled_mask)
355 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
356 stat);
357
358 if (stat & XRERR)
359 handled_mask |= XRERR;
360
361 /* Ack the handled event only */
362 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
363
364 return IRQ_RETVAL(handled_mask);
365}
366
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200367static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
368{
369 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
370 irqreturn_t ret = IRQ_NONE;
371
372 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
373 ret = davinci_mcasp_tx_irq_handler(irq, data);
374
375 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
376 ret |= davinci_mcasp_rx_irq_handler(irq, data);
377
378 return ret;
379}
380
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400381static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
382 unsigned int fmt)
383{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200384 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200385 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300386 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300387 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300388 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400389
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200390 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200391 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300392 case SND_SOC_DAIFMT_DSP_A:
393 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
394 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300395 /* 1st data bit occur one ACLK cycle after the frame sync */
396 data_delay = 1;
397 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200398 case SND_SOC_DAIFMT_DSP_B:
399 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200400 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
401 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300402 /* No delay after FS */
403 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200404 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300405 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200406 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200407 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
408 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300409 /* 1st data bit occur one ACLK cycle after the frame sync */
410 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300411 /* FS need to be inverted */
412 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200413 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300414 case SND_SOC_DAIFMT_LEFT_J:
415 /* configure a full-word SYNC pulse (LRCLK) */
416 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
417 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
418 /* No delay after FS */
419 data_delay = 0;
420 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300421 default:
422 ret = -EINVAL;
423 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200424 }
425
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300426 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
427 FSXDLY(3));
428 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
429 FSRDLY(3));
430
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400431 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
432 case SND_SOC_DAIFMT_CBS_CFS:
433 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200434 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
435 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400436
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200437 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
438 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400439
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200440 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
441 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200442 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400443 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200444 case SND_SOC_DAIFMT_CBS_CFM:
445 /* codec is clock slave and frame master */
446 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
447 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
448
449 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
450 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
451
452 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
453 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
454 mcasp->bclk_master = 1;
455 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400456 case SND_SOC_DAIFMT_CBM_CFS:
457 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200458 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
459 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400460
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200461 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
462 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400463
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200464 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
465 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200466 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400467 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400468 case SND_SOC_DAIFMT_CBM_CFM:
469 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200470 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
471 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400472
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200473 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
474 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400475
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200476 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
477 ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200478 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400479 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400480 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200481 ret = -EINVAL;
482 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400483 }
484
485 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
486 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200487 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300488 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300489 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400490 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400491 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200492 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300493 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300494 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400495 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400496 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200497 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300498 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300499 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400500 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400501 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200502 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200503 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300504 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400505 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400506 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200507 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300508 goto out;
509 }
510
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300511 if (inv_fs)
512 fs_pol_rising = !fs_pol_rising;
513
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300514 if (fs_pol_rising) {
515 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
516 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
517 } else {
518 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
519 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400520 }
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200521out:
522 pm_runtime_put_sync(mcasp->dev);
523 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400524}
525
Jyri Sarha88135432014-08-06 16:47:16 +0300526static int __davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
527 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200528{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200529 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200530
531 switch (div_id) {
532 case 0: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200533 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200534 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200535 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200536 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
537 break;
538
539 case 1: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200540 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200541 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200542 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200543 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300544 if (explicit)
545 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200546 break;
547
Daniel Mack1b3bc062012-12-05 18:20:38 +0100548 case 2: /* BCLK/LRCLK ratio */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200549 mcasp->bclk_lrclk_ratio = div;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100550 break;
551
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200552 default:
553 return -EINVAL;
554 }
555
556 return 0;
557}
558
Jyri Sarha88135432014-08-06 16:47:16 +0300559static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
560 int div)
561{
562 return __davinci_mcasp_set_clkdiv(dai, div_id, div, 1);
563}
564
Daniel Mack5b66aa22012-10-04 15:08:41 +0200565static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
566 unsigned int freq, int dir)
567{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200568 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200569
570 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200571 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
572 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
573 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200574 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200575 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
576 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
577 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200578 }
579
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200580 mcasp->sysclk_freq = freq;
581
Daniel Mack5b66aa22012-10-04 15:08:41 +0200582 return 0;
583}
584
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200585static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Daniel Mackba764b32012-12-05 18:20:37 +0100586 int word_length)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400587{
Daniel Mackba764b32012-12-05 18:20:37 +0100588 u32 fmt;
Daniel Mack79671892013-05-16 15:25:01 +0200589 u32 tx_rotate = (word_length / 4) & 0x7;
Daniel Mackba764b32012-12-05 18:20:37 +0100590 u32 mask = (1ULL << word_length) - 1;
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300591 /*
592 * For captured data we should not rotate, inversion and masking is
593 * enoguh to get the data to the right position:
594 * Format data from bus after reverse (XRBUF)
595 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
596 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
597 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
598 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
599 */
600 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400601
Daniel Mack1b3bc062012-12-05 18:20:38 +0100602 /*
603 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
604 * callback, take it into account here. That allows us to for example
605 * send 32 bits per channel to the codec, while only 16 of them carry
606 * audio payload.
Michal Bachratyd486fea2013-04-19 15:28:44 +0200607 * The clock ratio is given for a full period of data (for I2S format
608 * both left and right channels), so it has to be divided by number of
609 * tdm-slots (for I2S - divided by 2).
Daniel Mack1b3bc062012-12-05 18:20:38 +0100610 */
Peter Ujfalusid742b922014-11-10 12:32:19 +0200611 if (mcasp->bclk_lrclk_ratio) {
612 u32 slot_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
613
614 /*
615 * When we have more bclk then it is needed for the data, we
616 * need to use the rotation to move the received samples to have
617 * correct alignment.
618 */
619 rx_rotate = (slot_length - word_length) / 4;
620 word_length = slot_length;
621 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100622
Daniel Mackba764b32012-12-05 18:20:37 +0100623 /* mapping of the XSSZ bit-field as described in the datasheet */
624 fmt = (word_length >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400625
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200626 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200627 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
628 RXSSZ(0x0F));
629 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
630 TXSSZ(0x0F));
631 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
632 TXROT(7));
633 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
634 RXROT(7));
635 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200636 }
637
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200638 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400639
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400640 return 0;
641}
642
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200643static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300644 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400645{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300646 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400647 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400648 u8 tx_ser = 0;
649 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200650 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100651 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300652 int active_serializers, numevt, n;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200653 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400654 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300655 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200656 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400657
658 /* All PINS as McASP */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200659 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400660
661 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200662 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
663 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400664 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200665 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
666 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400667 }
668
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200669 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200670 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
671 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200672 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100673 tx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200674 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400675 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200676 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100677 rx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200678 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400679 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100680 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200681 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
682 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400683 }
684 }
685
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300686 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
687 active_serializers = tx_ser;
688 numevt = mcasp->txnumevt;
689 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
690 } else {
691 active_serializers = rx_ser;
692 numevt = mcasp->rxnumevt;
693 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
694 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100695
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300696 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200697 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300698 "enabled in mcasp (%d)\n", channels,
699 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100700 return -EINVAL;
701 }
702
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300703 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300704 if (!numevt) {
705 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300706 if (active_serializers > 1) {
707 /*
708 * If more than one serializers are in use we have one
709 * DMA request to provide data for all serializers.
710 * For example if three serializers are enabled the DMA
711 * need to transfer three words per DMA request.
712 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300713 dma_data->maxburst = active_serializers;
714 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300715 dma_data->maxburst = 0;
716 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300717 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300718 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400719
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300720 if (period_words % active_serializers) {
721 dev_err(mcasp->dev, "Invalid combination of period words and "
722 "active serializers: %d, %d\n", period_words,
723 active_serializers);
724 return -EINVAL;
725 }
726
727 /*
728 * Calculate the optimal AFIFO depth for platform side:
729 * The number of words for numevt need to be in steps of active
730 * serializers.
731 */
732 n = numevt % active_serializers;
733 if (n)
734 numevt += (active_serializers - n);
735 while (period_words % numevt && numevt > 0)
736 numevt -= active_serializers;
737 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300738 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400739
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300740 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
741 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100742
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300743 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300744 if (numevt == 1)
745 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300746 dma_data->maxburst = numevt;
747
Michal Bachraty2952b272013-02-28 16:07:08 +0100748 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400749}
750
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200751static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
752 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400753{
754 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200755 int total_slots;
756 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400757 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200758 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400759
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200760 total_slots = mcasp->tdm_slots;
761
762 /*
763 * If more than one serializer is needed, then use them with
764 * their specified tdm_slots count. Otherwise, one serializer
765 * can cope with the transaction using as many slots as channels
766 * in the stream, requires channels symmetry
767 */
768 active_serializers = (channels + total_slots - 1) / total_slots;
769 if (active_serializers == 1)
770 active_slots = channels;
771 else
772 active_slots = total_slots;
773
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400774 for (i = 0; i < active_slots; i++)
775 mask |= (1 << i);
776
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200777 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400778
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200779 if (!mcasp->dat_port)
780 busel = TXSEL;
781
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200782 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
783 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
784 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200785 FSXMOD(total_slots), FSXMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400786
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200787 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
788 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
789 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200790 FSRMOD(total_slots), FSRMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400791
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200792 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400793}
794
795/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100796static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
797 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400798{
Daniel Mack64792852014-03-27 11:27:40 +0100799 u32 cs_value = 0;
800 u8 *cs_bytes = (u8*) &cs_value;
801
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400802 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
803 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200804 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400805
806 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200807 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400808
809 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200810 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400811
812 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200813 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400814
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200815 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400816
817 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200818 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400819
820 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200821 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200822
Daniel Mack64792852014-03-27 11:27:40 +0100823 /* Set S/PDIF channel status bits */
824 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
825 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
826
827 switch (rate) {
828 case 22050:
829 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
830 break;
831 case 24000:
832 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
833 break;
834 case 32000:
835 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
836 break;
837 case 44100:
838 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
839 break;
840 case 48000:
841 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
842 break;
843 case 88200:
844 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
845 break;
846 case 96000:
847 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
848 break;
849 case 176400:
850 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
851 break;
852 case 192000:
853 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
854 break;
855 default:
856 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
857 return -EINVAL;
858 }
859
860 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
861 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
862
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200863 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400864}
865
866static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
867 struct snd_pcm_hw_params *params,
868 struct snd_soc_dai *cpu_dai)
869{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200870 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400871 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +0200872 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300873 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200874 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200875
Daniel Mack82675252014-07-16 14:04:41 +0200876 /*
877 * If mcasp is BCLK master, and a BCLK divider was not provided by
878 * the machine driver, we need to calculate the ratio.
879 */
880 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200881 unsigned int bclk_freq = snd_soc_params_to_bclk(params);
Jyri Sarha09298782014-06-13 12:50:00 +0300882 unsigned int div = mcasp->sysclk_freq / bclk_freq;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200883 if (mcasp->sysclk_freq % bclk_freq != 0) {
Jyri Sarha09298782014-06-13 12:50:00 +0300884 if (((mcasp->sysclk_freq / div) - bclk_freq) >
885 (bclk_freq - (mcasp->sysclk_freq / (div+1))))
886 div++;
887 dev_warn(mcasp->dev,
888 "Inaccurate BCLK: %u Hz / %u != %u Hz\n",
889 mcasp->sysclk_freq, div, bclk_freq);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200890 }
Jyri Sarha88135432014-08-06 16:47:16 +0300891 __davinci_mcasp_set_clkdiv(cpu_dai, 1, div, 0);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200892 }
893
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300894 ret = mcasp_common_hw_param(mcasp, substream->stream,
895 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +0200896 if (ret)
897 return ret;
898
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200899 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +0100900 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400901 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200902 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
903 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200904
905 if (ret)
906 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400907
908 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400909 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400910 case SNDRV_PCM_FORMAT_S8:
Daniel Mackba764b32012-12-05 18:20:37 +0100911 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400912 break;
913
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400914 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400915 case SNDRV_PCM_FORMAT_S16_LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100916 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400917 break;
918
Daniel Mack21eb24d2012-10-09 09:35:16 +0200919 case SNDRV_PCM_FORMAT_U24_3LE:
920 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100921 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +0200922 break;
923
Daniel Mack6b7fa012012-10-09 11:56:40 +0200924 case SNDRV_PCM_FORMAT_U24_LE:
925 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +0300926 word_length = 24;
927 break;
928
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400929 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400930 case SNDRV_PCM_FORMAT_S32_LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100931 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400932 break;
933
934 default:
935 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
936 return -EINVAL;
937 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400938
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200939 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400940
Peter Ujfalusi11277832014-11-10 12:32:16 +0200941 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
942 mcasp->channels = channels;
943
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400944 return 0;
945}
946
947static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
948 int cmd, struct snd_soc_dai *cpu_dai)
949{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200950 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400951 int ret = 0;
952
953 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400954 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +0530955 case SNDRV_PCM_TRIGGER_START:
956 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200957 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400958 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400959 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530960 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400961 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200962 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400963 break;
964
965 default:
966 ret = -EINVAL;
967 }
968
969 return ret;
970}
971
Peter Ujfalusi11277832014-11-10 12:32:16 +0200972static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
973 struct snd_soc_dai *cpu_dai)
974{
975 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
976 u32 max_channels = 0;
977 int i, dir;
978
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200979 mcasp->substreams[substream->stream] = substream;
980
Peter Ujfalusi11277832014-11-10 12:32:16 +0200981 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
982 return 0;
983
984 /*
985 * Limit the maximum allowed channels for the first stream:
986 * number of serializers for the direction * tdm slots per serializer
987 */
988 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
989 dir = TX_MODE;
990 else
991 dir = RX_MODE;
992
993 for (i = 0; i < mcasp->num_serializer; i++) {
994 if (mcasp->serial_dir[i] == dir)
995 max_channels++;
996 }
997 max_channels *= mcasp->tdm_slots;
998 /*
999 * If the already active stream has less channels than the calculated
1000 * limnit based on the seirializers * tdm_slots, we need to use that as
1001 * a constraint for the second stream.
1002 * Otherwise (first stream or less allowed channels) we use the
1003 * calculated constraint.
1004 */
1005 if (mcasp->channels && mcasp->channels < max_channels)
1006 max_channels = mcasp->channels;
1007
1008 snd_pcm_hw_constraint_minmax(substream->runtime,
1009 SNDRV_PCM_HW_PARAM_CHANNELS,
1010 2, max_channels);
1011 return 0;
1012}
1013
1014static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1015 struct snd_soc_dai *cpu_dai)
1016{
1017 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1018
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001019 mcasp->substreams[substream->stream] = NULL;
1020
Peter Ujfalusi11277832014-11-10 12:32:16 +02001021 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1022 return;
1023
1024 if (!cpu_dai->active)
1025 mcasp->channels = 0;
1026}
1027
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001028static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001029 .startup = davinci_mcasp_startup,
1030 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001031 .trigger = davinci_mcasp_trigger,
1032 .hw_params = davinci_mcasp_hw_params,
1033 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001034 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001035 .set_sysclk = davinci_mcasp_set_sysclk,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001036};
1037
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001038static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1039{
1040 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1041
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001042 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1043 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001044
1045 return 0;
1046}
1047
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001048#ifdef CONFIG_PM_SLEEP
1049static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
1050{
1051 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001052 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001053 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001054 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001055
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001056 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1057 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001058
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001059 if (mcasp->txnumevt) {
1060 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1061 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
1062 }
1063 if (mcasp->rxnumevt) {
1064 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1065 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
1066 }
1067
1068 for (i = 0; i < mcasp->num_serializer; i++)
1069 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
1070 DAVINCI_MCASP_XRSRCTL_REG(i));
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001071
1072 return 0;
1073}
1074
1075static int davinci_mcasp_resume(struct snd_soc_dai *dai)
1076{
1077 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001078 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001079 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001080 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001081
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001082 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1083 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001084
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001085 if (mcasp->txnumevt) {
1086 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1087 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
1088 }
1089 if (mcasp->rxnumevt) {
1090 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1091 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
1092 }
1093
1094 for (i = 0; i < mcasp->num_serializer; i++)
1095 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
1096 context->xrsr_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001097
1098 return 0;
1099}
1100#else
1101#define davinci_mcasp_suspend NULL
1102#define davinci_mcasp_resume NULL
1103#endif
1104
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001105#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1106
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001107#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1108 SNDRV_PCM_FMTBIT_U8 | \
1109 SNDRV_PCM_FMTBIT_S16_LE | \
1110 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001111 SNDRV_PCM_FMTBIT_S24_LE | \
1112 SNDRV_PCM_FMTBIT_U24_LE | \
1113 SNDRV_PCM_FMTBIT_S24_3LE | \
1114 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001115 SNDRV_PCM_FMTBIT_S32_LE | \
1116 SNDRV_PCM_FMTBIT_U32_LE)
1117
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001118static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001119 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001120 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001121 .probe = davinci_mcasp_dai_probe,
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001122 .suspend = davinci_mcasp_suspend,
1123 .resume = davinci_mcasp_resume,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001124 .playback = {
1125 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +01001126 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001127 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001128 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001129 },
1130 .capture = {
1131 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +01001132 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001133 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001134 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001135 },
1136 .ops = &davinci_mcasp_dai_ops,
1137
Peter Ujfalusid75249f2014-11-10 12:32:18 +02001138 .symmetric_samplebits = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001139 },
1140 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001141 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001142 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001143 .playback = {
1144 .channels_min = 1,
1145 .channels_max = 384,
1146 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001147 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001148 },
1149 .ops = &davinci_mcasp_dai_ops,
1150 },
1151
1152};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001153
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001154static const struct snd_soc_component_driver davinci_mcasp_component = {
1155 .name = "davinci-mcasp",
1156};
1157
Jyri Sarha256ba182013-10-18 18:37:42 +03001158/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001159static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001160 .tx_dma_offset = 0x400,
1161 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001162 .version = MCASP_VERSION_1,
1163};
1164
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001165static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001166 .tx_dma_offset = 0x2000,
1167 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001168 .version = MCASP_VERSION_2,
1169};
1170
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001171static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001172 .tx_dma_offset = 0,
1173 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001174 .version = MCASP_VERSION_3,
1175};
1176
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001177static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001178 .tx_dma_offset = 0x200,
1179 .rx_dma_offset = 0x284,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001180 .version = MCASP_VERSION_4,
1181};
1182
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301183static const struct of_device_id mcasp_dt_ids[] = {
1184 {
1185 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001186 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301187 },
1188 {
1189 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001190 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301191 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301192 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001193 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001194 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301195 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001196 {
1197 .compatible = "ti,dra7-mcasp-audio",
1198 .data = &dra7_mcasp_pdata,
1199 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301200 { /* sentinel */ }
1201};
1202MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1203
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001204static int mcasp_reparent_fck(struct platform_device *pdev)
1205{
1206 struct device_node *node = pdev->dev.of_node;
1207 struct clk *gfclk, *parent_clk;
1208 const char *parent_name;
1209 int ret;
1210
1211 if (!node)
1212 return 0;
1213
1214 parent_name = of_get_property(node, "fck_parent", NULL);
1215 if (!parent_name)
1216 return 0;
1217
1218 gfclk = clk_get(&pdev->dev, "fck");
1219 if (IS_ERR(gfclk)) {
1220 dev_err(&pdev->dev, "failed to get fck\n");
1221 return PTR_ERR(gfclk);
1222 }
1223
1224 parent_clk = clk_get(NULL, parent_name);
1225 if (IS_ERR(parent_clk)) {
1226 dev_err(&pdev->dev, "failed to get parent clock\n");
1227 ret = PTR_ERR(parent_clk);
1228 goto err1;
1229 }
1230
1231 ret = clk_set_parent(gfclk, parent_clk);
1232 if (ret) {
1233 dev_err(&pdev->dev, "failed to reparent fck\n");
1234 goto err2;
1235 }
1236
1237err2:
1238 clk_put(parent_clk);
1239err1:
1240 clk_put(gfclk);
1241 return ret;
1242}
1243
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001244static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301245 struct platform_device *pdev)
1246{
1247 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001248 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301249 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301250 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001251 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301252
1253 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301254 u32 val;
1255 int i, ret = 0;
1256
1257 if (pdev->dev.platform_data) {
1258 pdata = pdev->dev.platform_data;
1259 return pdata;
1260 } else if (match) {
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001261 pdata = (struct davinci_mcasp_pdata*) match->data;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301262 } else {
1263 /* control shouldn't reach here. something is wrong */
1264 ret = -EINVAL;
1265 goto nodata;
1266 }
1267
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301268 ret = of_property_read_u32(np, "op-mode", &val);
1269 if (ret >= 0)
1270 pdata->op_mode = val;
1271
1272 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001273 if (ret >= 0) {
1274 if (val < 2 || val > 32) {
1275 dev_err(&pdev->dev,
1276 "tdm-slots must be in rage [2-32]\n");
1277 ret = -EINVAL;
1278 goto nodata;
1279 }
1280
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301281 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001282 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301283
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301284 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1285 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301286 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001287 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1288 (sizeof(*of_serial_dir) * val),
1289 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301290 if (!of_serial_dir) {
1291 ret = -ENOMEM;
1292 goto nodata;
1293 }
1294
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001295 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301296 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1297
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001298 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301299 pdata->serial_dir = of_serial_dir;
1300 }
1301
Jyri Sarha4023fe62013-10-18 18:37:43 +03001302 ret = of_property_match_string(np, "dma-names", "tx");
1303 if (ret < 0)
1304 goto nodata;
1305
1306 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1307 &dma_spec);
1308 if (ret < 0)
1309 goto nodata;
1310
1311 pdata->tx_dma_channel = dma_spec.args[0];
1312
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001313 /* RX is not valid in DIT mode */
1314 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1315 ret = of_property_match_string(np, "dma-names", "rx");
1316 if (ret < 0)
1317 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001318
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001319 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1320 &dma_spec);
1321 if (ret < 0)
1322 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001323
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001324 pdata->rx_dma_channel = dma_spec.args[0];
1325 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001326
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301327 ret = of_property_read_u32(np, "tx-num-evt", &val);
1328 if (ret >= 0)
1329 pdata->txnumevt = val;
1330
1331 ret = of_property_read_u32(np, "rx-num-evt", &val);
1332 if (ret >= 0)
1333 pdata->rxnumevt = val;
1334
1335 ret = of_property_read_u32(np, "sram-size-playback", &val);
1336 if (ret >= 0)
1337 pdata->sram_size_playback = val;
1338
1339 ret = of_property_read_u32(np, "sram-size-capture", &val);
1340 if (ret >= 0)
1341 pdata->sram_size_capture = val;
1342
1343 return pdata;
1344
1345nodata:
1346 if (ret < 0) {
1347 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1348 ret);
1349 pdata = NULL;
1350 }
1351 return pdata;
1352}
1353
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001354static int davinci_mcasp_probe(struct platform_device *pdev)
1355{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001356 struct snd_dmaengine_dai_dma_data *dma_data;
Jyri Sarha256ba182013-10-18 18:37:42 +03001357 struct resource *mem, *ioarea, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001358 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001359 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001360 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001361 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001362 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01001363 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001364
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301365 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
1366 dev_err(&pdev->dev, "No platform data supplied\n");
1367 return -EINVAL;
1368 }
1369
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001370 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01001371 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001372 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001373 return -ENOMEM;
1374
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301375 pdata = davinci_mcasp_set_pdata_from_of(pdev);
1376 if (!pdata) {
1377 dev_err(&pdev->dev, "no platform data\n");
1378 return -EINVAL;
1379 }
1380
Jyri Sarha256ba182013-10-18 18:37:42 +03001381 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001382 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001383 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03001384 "\"mpu\" mem resource not found, using index 0\n");
1385 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1386 if (!mem) {
1387 dev_err(&pdev->dev, "no mem resource?\n");
1388 return -ENODEV;
1389 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001390 }
1391
Julia Lawall96d31e22011-12-29 17:51:21 +01001392 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
Vaibhav Bediad852f4462011-02-09 18:39:52 +05301393 resource_size(mem), pdev->name);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001394 if (!ioarea) {
1395 dev_err(&pdev->dev, "Audio region already claimed\n");
Julia Lawall96d31e22011-12-29 17:51:21 +01001396 return -EBUSY;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001397 }
1398
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301399 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001400
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301401 ret = pm_runtime_get_sync(&pdev->dev);
1402 if (IS_ERR_VALUE(ret)) {
1403 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
Anil Kumar7771ef32014-11-09 18:15:14 +05301404 pm_runtime_disable(&pdev->dev);
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301405 return ret;
1406 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001407
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001408 mcasp->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
1409 if (!mcasp->base) {
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301410 dev_err(&pdev->dev, "ioremap failed\n");
1411 ret = -ENOMEM;
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001412 goto err;
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301413 }
1414
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001415 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02001416 /* sanity check for tdm slots parameter */
1417 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1418 if (pdata->tdm_slots < 2) {
1419 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1420 pdata->tdm_slots);
1421 mcasp->tdm_slots = 2;
1422 } else if (pdata->tdm_slots > 32) {
1423 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1424 pdata->tdm_slots);
1425 mcasp->tdm_slots = 32;
1426 } else {
1427 mcasp->tdm_slots = pdata->tdm_slots;
1428 }
1429 }
1430
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001431 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001432#ifdef CONFIG_PM_SLEEP
1433 mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
1434 sizeof(u32) * mcasp->num_serializer,
1435 GFP_KERNEL);
1436#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001437 mcasp->serial_dir = pdata->serial_dir;
1438 mcasp->version = pdata->version;
1439 mcasp->txnumevt = pdata->txnumevt;
1440 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02001441
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001442 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001443
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001444 irq = platform_get_irq_byname(pdev, "common");
1445 if (irq >= 0) {
1446 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common\n",
1447 dev_name(&pdev->dev));
1448 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1449 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02001450 IRQF_ONESHOT | IRQF_SHARED,
1451 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001452 if (ret) {
1453 dev_err(&pdev->dev, "common IRQ request failed\n");
1454 goto err;
1455 }
1456
1457 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1458 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1459 }
1460
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001461 irq = platform_get_irq_byname(pdev, "rx");
1462 if (irq >= 0) {
1463 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx\n",
1464 dev_name(&pdev->dev));
1465 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1466 davinci_mcasp_rx_irq_handler,
1467 IRQF_ONESHOT, irq_name, mcasp);
1468 if (ret) {
1469 dev_err(&pdev->dev, "RX IRQ request failed\n");
1470 goto err;
1471 }
1472
1473 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1474 }
1475
1476 irq = platform_get_irq_byname(pdev, "tx");
1477 if (irq >= 0) {
1478 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx\n",
1479 dev_name(&pdev->dev));
1480 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1481 davinci_mcasp_tx_irq_handler,
1482 IRQF_ONESHOT, irq_name, mcasp);
1483 if (ret) {
1484 dev_err(&pdev->dev, "TX IRQ request failed\n");
1485 goto err;
1486 }
1487
1488 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1489 }
1490
Jyri Sarha256ba182013-10-18 18:37:42 +03001491 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001492 if (dat)
1493 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03001494
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001495 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001496 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001497 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001498 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001499 dma_data->addr = mem->start + pdata->tx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001500
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001501 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001502 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001503 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001504 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001505 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001506 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07001507
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001508 /* dmaengine filter data for DT and non-DT boot */
1509 if (pdev->dev.of_node)
1510 dma_data->filter_data = "tx";
1511 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001512 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001513
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001514 /* RX is not valid in DIT mode */
1515 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001516 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001517 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001518 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001519 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001520 dma_data->addr = mem->start + pdata->rx_dma_offset;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001521
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001522 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001523 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1524 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001525 *dma = res->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001526 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001527 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001528
1529 /* dmaengine filter data for DT and non-DT boot */
1530 if (pdev->dev.of_node)
1531 dma_data->filter_data = "rx";
1532 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001533 dma_data->filter_data = dma;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001534 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001535
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001536 if (mcasp->version < MCASP_VERSION_3) {
1537 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001538 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001539 mcasp->dat_port = true;
1540 } else {
1541 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
1542 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001543
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001544 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001545
1546 mcasp_reparent_fck(pdev);
1547
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001548 ret = devm_snd_soc_register_component(&pdev->dev,
1549 &davinci_mcasp_component,
1550 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001551
1552 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001553 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301554
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001555 switch (mcasp->version) {
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03001556#if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
1557 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1558 IS_MODULE(CONFIG_SND_EDMA_SOC))
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001559 case MCASP_VERSION_1:
1560 case MCASP_VERSION_2:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03001561 case MCASP_VERSION_3:
1562 ret = edma_pcm_platform_register(&pdev->dev);
1563 break;
1564#endif
Jyri Sarha7f28f352014-06-13 12:49:59 +03001565#if IS_BUILTIN(CONFIG_SND_OMAP_SOC) || \
1566 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1567 IS_MODULE(CONFIG_SND_OMAP_SOC))
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001568 case MCASP_VERSION_4:
1569 ret = omap_pcm_platform_register(&pdev->dev);
1570 break;
Jyri Sarha7f28f352014-06-13 12:49:59 +03001571#endif
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001572 default:
1573 dev_err(&pdev->dev, "Invalid McASP version: %d\n",
1574 mcasp->version);
1575 ret = -EINVAL;
1576 break;
1577 }
1578
1579 if (ret) {
1580 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001581 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301582 }
1583
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001584 return 0;
1585
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001586err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301587 pm_runtime_put_sync(&pdev->dev);
1588 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001589 return ret;
1590}
1591
1592static int davinci_mcasp_remove(struct platform_device *pdev)
1593{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301594 pm_runtime_put_sync(&pdev->dev);
1595 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001596
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001597 return 0;
1598}
1599
1600static struct platform_driver davinci_mcasp_driver = {
1601 .probe = davinci_mcasp_probe,
1602 .remove = davinci_mcasp_remove,
1603 .driver = {
1604 .name = "davinci-mcasp",
Sachin Kamatea421eb2013-05-22 16:53:37 +05301605 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001606 },
1607};
1608
Axel Linf9b8a512011-11-25 10:09:27 +08001609module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001610
1611MODULE_AUTHOR("Steve Chen");
1612MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1613MODULE_LICENSE("GPL");