blob: 33cea0728cd26e47864ac711349100dfc0ef3c5b [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 Ujfalusi6afda7f2015-03-05 16:55:21 +020065 bool pm_state;
Peter Ujfalusi790bb942014-02-03 14:51:52 +020066};
67
Peter Ujfalusi70091a32013-11-14 11:35:29 +020068struct davinci_mcasp {
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;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020073 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020074
75 /* McASP specific data */
76 int tdm_slots;
77 u8 op_mode;
78 u8 num_serializer;
79 u8 *serial_dir;
80 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020081 u8 bclk_div;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020082 u16 bclk_lrclk_ratio;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020083 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020084 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020085 int dma_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020086
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020087 int sysclk_freq;
88 bool bclk_master;
89
Peter Ujfalusi21400a72013-11-14 11:35:26 +020090 /* McASP FIFO related */
91 u8 txnumevt;
92 u8 rxnumevt;
93
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +020094 bool dat_port;
95
Peter Ujfalusi11277832014-11-10 12:32:16 +020096 /* Used for comstraint setting on the second stream */
97 u32 channels;
98
Peter Ujfalusi21400a72013-11-14 11:35:26 +020099#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200100 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200101#endif
102};
103
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200104static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
105 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400106{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200107 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400108 __raw_writel(__raw_readl(reg) | val, reg);
109}
110
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200111static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
112 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400113{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200114 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400115 __raw_writel((__raw_readl(reg) & ~(val)), reg);
116}
117
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200118static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
119 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400120{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200121 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400122 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
123}
124
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200125static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
126 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400127{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200128 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400129}
130
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200131static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400132{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200133 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400134}
135
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200136static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400137{
138 int i = 0;
139
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200140 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400141
142 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
143 /* loop count is to avoid the lock-up */
144 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200145 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400146 break;
147 }
148
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200149 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400150 printk(KERN_ERR "GBLCTL write error\n");
151}
152
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200153static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
154{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200155 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
156 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200157
158 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
159}
160
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200161static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400162{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200163 if (mcasp->rxnumevt) { /* enable FIFO */
164 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
165
166 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
167 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
168 }
169
Peter Ujfalusi44982732014-10-29 13:55:45 +0200170 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200171 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
172 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200173 /*
174 * When ASYNC == 0 the transmit and receive sections operate
175 * synchronously from the transmit clock and frame sync. We need to make
176 * sure that the TX signlas are enabled when starting reception.
177 */
178 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200179 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
180 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200181 }
182
Peter Ujfalusi44982732014-10-29 13:55:45 +0200183 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200184 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200185 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200186 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200187 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200188 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200189 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200190 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200191
192 /* enable receive IRQs */
193 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
194 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400195}
196
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200197static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400198{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400199 u32 cnt;
200
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200201 if (mcasp->txnumevt) { /* enable FIFO */
202 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
203
204 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
205 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
206 }
207
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200208 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200209 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
210 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200211 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200212 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400213
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200214 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400215 cnt = 0;
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200216 while (!(mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) &
217 ~XRDATA) && (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400218 cnt++;
219
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200220 /* Release TX state machine */
221 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
222 /* Release Frame Sync generator */
223 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200224
225 /* enable transmit IRQs */
226 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
227 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400228}
229
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200230static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400231{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200232 mcasp->streams++;
233
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200234 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200235 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200236 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200237 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400238}
239
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200240static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400241{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200242 /* disable IRQ sources */
243 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
244 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
245
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200246 /*
247 * In synchronous mode stop the TX clocks if no other stream is
248 * running
249 */
250 if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200251 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200252
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200253 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
254 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200255
256 if (mcasp->rxnumevt) { /* disable FIFO */
257 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
258
259 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
260 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400261}
262
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200263static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400264{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200265 u32 val = 0;
266
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200267 /* disable IRQ sources */
268 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
269 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
270
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200271 /*
272 * In synchronous mode keep TX clocks running if the capture stream is
273 * still running.
274 */
275 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
276 val = TXHCLKRST | TXCLKRST | TXFSRST;
277
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200278 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
279 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200280
281 if (mcasp->txnumevt) { /* disable FIFO */
282 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
283
284 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
285 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400286}
287
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200288static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400289{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200290 mcasp->streams--;
291
Peter Ujfalusi03808662014-10-29 13:55:46 +0200292 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200293 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200294 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200295 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400296}
297
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200298static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
299{
300 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
301 struct snd_pcm_substream *substream;
302 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
303 u32 handled_mask = 0;
304 u32 stat;
305
306 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
307 if (stat & XUNDRN & irq_mask) {
308 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
309 handled_mask |= XUNDRN;
310
311 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
312 if (substream) {
313 snd_pcm_stream_lock_irq(substream);
314 if (snd_pcm_running(substream))
315 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
316 snd_pcm_stream_unlock_irq(substream);
317 }
318 }
319
320 if (!handled_mask)
321 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
322 stat);
323
324 if (stat & XRERR)
325 handled_mask |= XRERR;
326
327 /* Ack the handled event only */
328 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
329
330 return IRQ_RETVAL(handled_mask);
331}
332
333static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
334{
335 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
336 struct snd_pcm_substream *substream;
337 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
338 u32 handled_mask = 0;
339 u32 stat;
340
341 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
342 if (stat & ROVRN & irq_mask) {
343 dev_warn(mcasp->dev, "Receive buffer overflow\n");
344 handled_mask |= ROVRN;
345
346 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
347 if (substream) {
348 snd_pcm_stream_lock_irq(substream);
349 if (snd_pcm_running(substream))
350 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
351 snd_pcm_stream_unlock_irq(substream);
352 }
353 }
354
355 if (!handled_mask)
356 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
357 stat);
358
359 if (stat & XRERR)
360 handled_mask |= XRERR;
361
362 /* Ack the handled event only */
363 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
364
365 return IRQ_RETVAL(handled_mask);
366}
367
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200368static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
369{
370 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
371 irqreturn_t ret = IRQ_NONE;
372
373 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
374 ret = davinci_mcasp_tx_irq_handler(irq, data);
375
376 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
377 ret |= davinci_mcasp_rx_irq_handler(irq, data);
378
379 return ret;
380}
381
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400382static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
383 unsigned int fmt)
384{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200385 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200386 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300387 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300388 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300389 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400390
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200391 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200392 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300393 case SND_SOC_DAIFMT_DSP_A:
394 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
395 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300396 /* 1st data bit occur one ACLK cycle after the frame sync */
397 data_delay = 1;
398 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200399 case SND_SOC_DAIFMT_DSP_B:
400 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200401 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
402 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300403 /* No delay after FS */
404 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200405 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300406 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200407 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200408 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
409 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300410 /* 1st data bit occur one ACLK cycle after the frame sync */
411 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300412 /* FS need to be inverted */
413 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200414 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300415 case SND_SOC_DAIFMT_LEFT_J:
416 /* configure a full-word SYNC pulse (LRCLK) */
417 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
418 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
419 /* No delay after FS */
420 data_delay = 0;
421 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300422 default:
423 ret = -EINVAL;
424 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200425 }
426
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300427 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
428 FSXDLY(3));
429 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
430 FSRDLY(3));
431
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400432 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
433 case SND_SOC_DAIFMT_CBS_CFS:
434 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200435 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
436 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400437
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200438 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
439 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400440
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200441 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
442 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200443 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400444 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200445 case SND_SOC_DAIFMT_CBS_CFM:
446 /* codec is clock slave and frame master */
447 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
448 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
449
450 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
451 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
452
453 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
454 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
455 mcasp->bclk_master = 1;
456 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400457 case SND_SOC_DAIFMT_CBM_CFS:
458 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200459 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
460 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400461
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200462 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
463 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400464
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200465 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
466 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200467 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400468 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400469 case SND_SOC_DAIFMT_CBM_CFM:
470 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200471 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
472 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400473
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200474 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
475 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400476
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200477 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
478 ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200479 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400480 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400481 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200482 ret = -EINVAL;
483 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400484 }
485
486 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
487 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200488 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300489 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300490 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400491 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400492 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200493 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300494 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300495 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400496 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400497 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200498 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300499 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300500 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400501 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400502 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200503 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200504 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300505 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400506 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400507 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200508 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300509 goto out;
510 }
511
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300512 if (inv_fs)
513 fs_pol_rising = !fs_pol_rising;
514
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300515 if (fs_pol_rising) {
516 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
517 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
518 } else {
519 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
520 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400521 }
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200522out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200523 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200524 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400525}
526
Jyri Sarha88135432014-08-06 16:47:16 +0300527static int __davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
528 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200529{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200530 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200531
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200532 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200533 switch (div_id) {
534 case 0: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200535 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200536 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200537 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200538 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
539 break;
540
541 case 1: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200542 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200543 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200544 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200545 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300546 if (explicit)
547 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200548 break;
549
Daniel Mack1b3bc062012-12-05 18:20:38 +0100550 case 2: /* BCLK/LRCLK ratio */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200551 mcasp->bclk_lrclk_ratio = div;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100552 break;
553
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200554 default:
555 return -EINVAL;
556 }
557
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200558 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200559 return 0;
560}
561
Jyri Sarha88135432014-08-06 16:47:16 +0300562static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
563 int div)
564{
565 return __davinci_mcasp_set_clkdiv(dai, div_id, div, 1);
566}
567
Daniel Mack5b66aa22012-10-04 15:08:41 +0200568static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
569 unsigned int freq, int dir)
570{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200571 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200572
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200573 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200574 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200575 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
576 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
577 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200578 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200579 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
580 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
581 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200582 }
583
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200584 mcasp->sysclk_freq = freq;
585
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200586 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200587 return 0;
588}
589
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200590static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Daniel Mackba764b32012-12-05 18:20:37 +0100591 int word_length)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400592{
Daniel Mackba764b32012-12-05 18:20:37 +0100593 u32 fmt;
Daniel Mack79671892013-05-16 15:25:01 +0200594 u32 tx_rotate = (word_length / 4) & 0x7;
Daniel Mackba764b32012-12-05 18:20:37 +0100595 u32 mask = (1ULL << word_length) - 1;
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300596 /*
597 * For captured data we should not rotate, inversion and masking is
598 * enoguh to get the data to the right position:
599 * Format data from bus after reverse (XRBUF)
600 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
601 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
602 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
603 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
604 */
605 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400606
Daniel Mack1b3bc062012-12-05 18:20:38 +0100607 /*
608 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
609 * callback, take it into account here. That allows us to for example
610 * send 32 bits per channel to the codec, while only 16 of them carry
611 * audio payload.
Michal Bachratyd486fea2013-04-19 15:28:44 +0200612 * The clock ratio is given for a full period of data (for I2S format
613 * both left and right channels), so it has to be divided by number of
614 * tdm-slots (for I2S - divided by 2).
Daniel Mack1b3bc062012-12-05 18:20:38 +0100615 */
Peter Ujfalusid742b922014-11-10 12:32:19 +0200616 if (mcasp->bclk_lrclk_ratio) {
617 u32 slot_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
618
619 /*
620 * When we have more bclk then it is needed for the data, we
621 * need to use the rotation to move the received samples to have
622 * correct alignment.
623 */
624 rx_rotate = (slot_length - word_length) / 4;
625 word_length = slot_length;
626 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100627
Daniel Mackba764b32012-12-05 18:20:37 +0100628 /* mapping of the XSSZ bit-field as described in the datasheet */
629 fmt = (word_length >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400630
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200631 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200632 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
633 RXSSZ(0x0F));
634 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
635 TXSSZ(0x0F));
636 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
637 TXROT(7));
638 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
639 RXROT(7));
640 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200641 }
642
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200643 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400644
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400645 return 0;
646}
647
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200648static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300649 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400650{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300651 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400652 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400653 u8 tx_ser = 0;
654 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200655 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100656 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300657 int active_serializers, numevt, n;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200658 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400659 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300660 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200661 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400662
663 /* All PINS as McASP */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200664 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400665
666 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200667 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
668 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400669 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200670 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
671 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400672 }
673
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200674 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200675 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
676 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200677 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100678 tx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200679 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400680 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200681 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100682 rx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200683 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400684 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100685 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200686 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
687 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400688 }
689 }
690
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300691 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
692 active_serializers = tx_ser;
693 numevt = mcasp->txnumevt;
694 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
695 } else {
696 active_serializers = rx_ser;
697 numevt = mcasp->rxnumevt;
698 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
699 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100700
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300701 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200702 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300703 "enabled in mcasp (%d)\n", channels,
704 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100705 return -EINVAL;
706 }
707
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300708 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300709 if (!numevt) {
710 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300711 if (active_serializers > 1) {
712 /*
713 * If more than one serializers are in use we have one
714 * DMA request to provide data for all serializers.
715 * For example if three serializers are enabled the DMA
716 * need to transfer three words per DMA request.
717 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300718 dma_data->maxburst = active_serializers;
719 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300720 dma_data->maxburst = 0;
721 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300722 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300723 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400724
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300725 if (period_words % active_serializers) {
726 dev_err(mcasp->dev, "Invalid combination of period words and "
727 "active serializers: %d, %d\n", period_words,
728 active_serializers);
729 return -EINVAL;
730 }
731
732 /*
733 * Calculate the optimal AFIFO depth for platform side:
734 * The number of words for numevt need to be in steps of active
735 * serializers.
736 */
737 n = numevt % active_serializers;
738 if (n)
739 numevt += (active_serializers - n);
740 while (period_words % numevt && numevt > 0)
741 numevt -= active_serializers;
742 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300743 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400744
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300745 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
746 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100747
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300748 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300749 if (numevt == 1)
750 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300751 dma_data->maxburst = numevt;
752
Michal Bachraty2952b272013-02-28 16:07:08 +0100753 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400754}
755
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200756static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
757 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400758{
759 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200760 int total_slots;
761 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400762 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200763 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400764
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200765 total_slots = mcasp->tdm_slots;
766
767 /*
768 * If more than one serializer is needed, then use them with
769 * their specified tdm_slots count. Otherwise, one serializer
770 * can cope with the transaction using as many slots as channels
771 * in the stream, requires channels symmetry
772 */
773 active_serializers = (channels + total_slots - 1) / total_slots;
774 if (active_serializers == 1)
775 active_slots = channels;
776 else
777 active_slots = total_slots;
778
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400779 for (i = 0; i < active_slots; i++)
780 mask |= (1 << i);
781
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200782 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400783
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200784 if (!mcasp->dat_port)
785 busel = TXSEL;
786
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200787 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
788 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
789 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200790 FSXMOD(total_slots), FSXMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400791
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200792 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
793 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
794 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200795 FSRMOD(total_slots), FSRMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400796
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200797 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400798}
799
800/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100801static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
802 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400803{
Daniel Mack64792852014-03-27 11:27:40 +0100804 u32 cs_value = 0;
805 u8 *cs_bytes = (u8*) &cs_value;
806
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400807 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
808 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200809 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400810
811 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200812 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400813
814 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200815 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400816
817 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200818 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400819
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200820 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400821
822 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200823 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400824
825 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200826 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200827
Daniel Mack64792852014-03-27 11:27:40 +0100828 /* Set S/PDIF channel status bits */
829 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
830 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
831
832 switch (rate) {
833 case 22050:
834 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
835 break;
836 case 24000:
837 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
838 break;
839 case 32000:
840 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
841 break;
842 case 44100:
843 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
844 break;
845 case 48000:
846 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
847 break;
848 case 88200:
849 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
850 break;
851 case 96000:
852 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
853 break;
854 case 176400:
855 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
856 break;
857 case 192000:
858 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
859 break;
860 default:
861 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
862 return -EINVAL;
863 }
864
865 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
866 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
867
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200868 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400869}
870
871static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
872 struct snd_pcm_hw_params *params,
873 struct snd_soc_dai *cpu_dai)
874{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200875 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400876 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +0200877 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300878 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200879 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200880
Daniel Mack82675252014-07-16 14:04:41 +0200881 /*
882 * If mcasp is BCLK master, and a BCLK divider was not provided by
883 * the machine driver, we need to calculate the ratio.
884 */
885 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200886 unsigned int bclk_freq = snd_soc_params_to_bclk(params);
Jyri Sarha09298782014-06-13 12:50:00 +0300887 unsigned int div = mcasp->sysclk_freq / bclk_freq;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200888 if (mcasp->sysclk_freq % bclk_freq != 0) {
Jyri Sarha09298782014-06-13 12:50:00 +0300889 if (((mcasp->sysclk_freq / div) - bclk_freq) >
890 (bclk_freq - (mcasp->sysclk_freq / (div+1))))
891 div++;
892 dev_warn(mcasp->dev,
893 "Inaccurate BCLK: %u Hz / %u != %u Hz\n",
894 mcasp->sysclk_freq, div, bclk_freq);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200895 }
Jyri Sarha88135432014-08-06 16:47:16 +0300896 __davinci_mcasp_set_clkdiv(cpu_dai, 1, div, 0);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200897 }
898
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300899 ret = mcasp_common_hw_param(mcasp, substream->stream,
900 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +0200901 if (ret)
902 return ret;
903
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200904 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +0100905 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400906 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200907 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
908 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200909
910 if (ret)
911 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400912
913 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400914 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400915 case SNDRV_PCM_FORMAT_S8:
Daniel Mackba764b32012-12-05 18:20:37 +0100916 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400917 break;
918
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400919 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400920 case SNDRV_PCM_FORMAT_S16_LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100921 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400922 break;
923
Daniel Mack21eb24d2012-10-09 09:35:16 +0200924 case SNDRV_PCM_FORMAT_U24_3LE:
925 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100926 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +0200927 break;
928
Daniel Mack6b7fa012012-10-09 11:56:40 +0200929 case SNDRV_PCM_FORMAT_U24_LE:
930 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +0300931 word_length = 24;
932 break;
933
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400934 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400935 case SNDRV_PCM_FORMAT_S32_LE:
Daniel Mackba764b32012-12-05 18:20:37 +0100936 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400937 break;
938
939 default:
940 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
941 return -EINVAL;
942 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400943
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200944 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400945
Peter Ujfalusi11277832014-11-10 12:32:16 +0200946 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
947 mcasp->channels = channels;
948
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400949 return 0;
950}
951
952static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
953 int cmd, struct snd_soc_dai *cpu_dai)
954{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200955 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400956 int ret = 0;
957
958 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400959 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +0530960 case SNDRV_PCM_TRIGGER_START:
961 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200962 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400963 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400964 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530965 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400966 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200967 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400968 break;
969
970 default:
971 ret = -EINVAL;
972 }
973
974 return ret;
975}
976
Peter Ujfalusi11277832014-11-10 12:32:16 +0200977static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
978 struct snd_soc_dai *cpu_dai)
979{
980 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
981 u32 max_channels = 0;
982 int i, dir;
983
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200984 mcasp->substreams[substream->stream] = substream;
985
Peter Ujfalusi11277832014-11-10 12:32:16 +0200986 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
987 return 0;
988
989 /*
990 * Limit the maximum allowed channels for the first stream:
991 * number of serializers for the direction * tdm slots per serializer
992 */
993 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
994 dir = TX_MODE;
995 else
996 dir = RX_MODE;
997
998 for (i = 0; i < mcasp->num_serializer; i++) {
999 if (mcasp->serial_dir[i] == dir)
1000 max_channels++;
1001 }
1002 max_channels *= mcasp->tdm_slots;
1003 /*
1004 * If the already active stream has less channels than the calculated
1005 * limnit based on the seirializers * tdm_slots, we need to use that as
1006 * a constraint for the second stream.
1007 * Otherwise (first stream or less allowed channels) we use the
1008 * calculated constraint.
1009 */
1010 if (mcasp->channels && mcasp->channels < max_channels)
1011 max_channels = mcasp->channels;
1012
1013 snd_pcm_hw_constraint_minmax(substream->runtime,
1014 SNDRV_PCM_HW_PARAM_CHANNELS,
1015 2, max_channels);
1016 return 0;
1017}
1018
1019static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1020 struct snd_soc_dai *cpu_dai)
1021{
1022 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1023
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001024 mcasp->substreams[substream->stream] = NULL;
1025
Peter Ujfalusi11277832014-11-10 12:32:16 +02001026 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1027 return;
1028
1029 if (!cpu_dai->active)
1030 mcasp->channels = 0;
1031}
1032
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001033static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001034 .startup = davinci_mcasp_startup,
1035 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001036 .trigger = davinci_mcasp_trigger,
1037 .hw_params = davinci_mcasp_hw_params,
1038 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001039 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001040 .set_sysclk = davinci_mcasp_set_sysclk,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001041};
1042
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001043static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1044{
1045 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1046
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001047 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1048 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001049
1050 return 0;
1051}
1052
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001053#ifdef CONFIG_PM_SLEEP
1054static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
1055{
1056 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001057 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001058 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001059 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001060
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001061 context->pm_state = pm_runtime_enabled(mcasp->dev)
1062 if (!context->pm_state)
1063 pm_runtime_get_sync(mcasp->dev);
1064
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001065 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1066 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001067
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001068 if (mcasp->txnumevt) {
1069 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1070 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
1071 }
1072 if (mcasp->rxnumevt) {
1073 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1074 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
1075 }
1076
1077 for (i = 0; i < mcasp->num_serializer; i++)
1078 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
1079 DAVINCI_MCASP_XRSRCTL_REG(i));
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001080
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001081 pm_runtime_put_sync(mcasp->dev);
1082
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001083 return 0;
1084}
1085
1086static int davinci_mcasp_resume(struct snd_soc_dai *dai)
1087{
1088 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001089 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001090 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001091 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001092
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001093 pm_runtime_get_sync(mcasp->dev);
1094
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001095 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1096 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001097
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001098 if (mcasp->txnumevt) {
1099 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1100 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
1101 }
1102 if (mcasp->rxnumevt) {
1103 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1104 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
1105 }
1106
1107 for (i = 0; i < mcasp->num_serializer; i++)
1108 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
1109 context->xrsr_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001110
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001111 if (!context->pm_state)
1112 pm_runtime_put_sync(mcasp->dev);
1113
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001114 return 0;
1115}
1116#else
1117#define davinci_mcasp_suspend NULL
1118#define davinci_mcasp_resume NULL
1119#endif
1120
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001121#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1122
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001123#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1124 SNDRV_PCM_FMTBIT_U8 | \
1125 SNDRV_PCM_FMTBIT_S16_LE | \
1126 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001127 SNDRV_PCM_FMTBIT_S24_LE | \
1128 SNDRV_PCM_FMTBIT_U24_LE | \
1129 SNDRV_PCM_FMTBIT_S24_3LE | \
1130 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001131 SNDRV_PCM_FMTBIT_S32_LE | \
1132 SNDRV_PCM_FMTBIT_U32_LE)
1133
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001134static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001135 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001136 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001137 .probe = davinci_mcasp_dai_probe,
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001138 .suspend = davinci_mcasp_suspend,
1139 .resume = davinci_mcasp_resume,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001140 .playback = {
1141 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +01001142 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001143 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001144 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001145 },
1146 .capture = {
1147 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +01001148 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001149 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001150 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001151 },
1152 .ops = &davinci_mcasp_dai_ops,
1153
Peter Ujfalusid75249f2014-11-10 12:32:18 +02001154 .symmetric_samplebits = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001155 },
1156 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001157 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f62014-04-01 15:55:07 +03001158 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001159 .playback = {
1160 .channels_min = 1,
1161 .channels_max = 384,
1162 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001163 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001164 },
1165 .ops = &davinci_mcasp_dai_ops,
1166 },
1167
1168};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001169
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001170static const struct snd_soc_component_driver davinci_mcasp_component = {
1171 .name = "davinci-mcasp",
1172};
1173
Jyri Sarha256ba182013-10-18 18:37:42 +03001174/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001175static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001176 .tx_dma_offset = 0x400,
1177 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001178 .version = MCASP_VERSION_1,
1179};
1180
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001181static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001182 .tx_dma_offset = 0x2000,
1183 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001184 .version = MCASP_VERSION_2,
1185};
1186
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001187static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001188 .tx_dma_offset = 0,
1189 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001190 .version = MCASP_VERSION_3,
1191};
1192
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001193static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001194 .tx_dma_offset = 0x200,
1195 .rx_dma_offset = 0x284,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001196 .version = MCASP_VERSION_4,
1197};
1198
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301199static const struct of_device_id mcasp_dt_ids[] = {
1200 {
1201 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001202 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301203 },
1204 {
1205 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001206 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301207 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301208 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001209 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001210 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301211 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001212 {
1213 .compatible = "ti,dra7-mcasp-audio",
1214 .data = &dra7_mcasp_pdata,
1215 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301216 { /* sentinel */ }
1217};
1218MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1219
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001220static int mcasp_reparent_fck(struct platform_device *pdev)
1221{
1222 struct device_node *node = pdev->dev.of_node;
1223 struct clk *gfclk, *parent_clk;
1224 const char *parent_name;
1225 int ret;
1226
1227 if (!node)
1228 return 0;
1229
1230 parent_name = of_get_property(node, "fck_parent", NULL);
1231 if (!parent_name)
1232 return 0;
1233
1234 gfclk = clk_get(&pdev->dev, "fck");
1235 if (IS_ERR(gfclk)) {
1236 dev_err(&pdev->dev, "failed to get fck\n");
1237 return PTR_ERR(gfclk);
1238 }
1239
1240 parent_clk = clk_get(NULL, parent_name);
1241 if (IS_ERR(parent_clk)) {
1242 dev_err(&pdev->dev, "failed to get parent clock\n");
1243 ret = PTR_ERR(parent_clk);
1244 goto err1;
1245 }
1246
1247 ret = clk_set_parent(gfclk, parent_clk);
1248 if (ret) {
1249 dev_err(&pdev->dev, "failed to reparent fck\n");
1250 goto err2;
1251 }
1252
1253err2:
1254 clk_put(parent_clk);
1255err1:
1256 clk_put(gfclk);
1257 return ret;
1258}
1259
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001260static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301261 struct platform_device *pdev)
1262{
1263 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001264 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301265 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301266 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001267 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301268
1269 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301270 u32 val;
1271 int i, ret = 0;
1272
1273 if (pdev->dev.platform_data) {
1274 pdata = pdev->dev.platform_data;
1275 return pdata;
1276 } else if (match) {
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001277 pdata = (struct davinci_mcasp_pdata*) match->data;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301278 } else {
1279 /* control shouldn't reach here. something is wrong */
1280 ret = -EINVAL;
1281 goto nodata;
1282 }
1283
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301284 ret = of_property_read_u32(np, "op-mode", &val);
1285 if (ret >= 0)
1286 pdata->op_mode = val;
1287
1288 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001289 if (ret >= 0) {
1290 if (val < 2 || val > 32) {
1291 dev_err(&pdev->dev,
1292 "tdm-slots must be in rage [2-32]\n");
1293 ret = -EINVAL;
1294 goto nodata;
1295 }
1296
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301297 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001298 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301299
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301300 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1301 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301302 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001303 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1304 (sizeof(*of_serial_dir) * val),
1305 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301306 if (!of_serial_dir) {
1307 ret = -ENOMEM;
1308 goto nodata;
1309 }
1310
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001311 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301312 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1313
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001314 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301315 pdata->serial_dir = of_serial_dir;
1316 }
1317
Jyri Sarha4023fe62013-10-18 18:37:43 +03001318 ret = of_property_match_string(np, "dma-names", "tx");
1319 if (ret < 0)
1320 goto nodata;
1321
1322 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1323 &dma_spec);
1324 if (ret < 0)
1325 goto nodata;
1326
1327 pdata->tx_dma_channel = dma_spec.args[0];
1328
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001329 /* RX is not valid in DIT mode */
1330 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1331 ret = of_property_match_string(np, "dma-names", "rx");
1332 if (ret < 0)
1333 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001334
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001335 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1336 &dma_spec);
1337 if (ret < 0)
1338 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001339
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001340 pdata->rx_dma_channel = dma_spec.args[0];
1341 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001342
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301343 ret = of_property_read_u32(np, "tx-num-evt", &val);
1344 if (ret >= 0)
1345 pdata->txnumevt = val;
1346
1347 ret = of_property_read_u32(np, "rx-num-evt", &val);
1348 if (ret >= 0)
1349 pdata->rxnumevt = val;
1350
1351 ret = of_property_read_u32(np, "sram-size-playback", &val);
1352 if (ret >= 0)
1353 pdata->sram_size_playback = val;
1354
1355 ret = of_property_read_u32(np, "sram-size-capture", &val);
1356 if (ret >= 0)
1357 pdata->sram_size_capture = val;
1358
1359 return pdata;
1360
1361nodata:
1362 if (ret < 0) {
1363 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1364 ret);
1365 pdata = NULL;
1366 }
1367 return pdata;
1368}
1369
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001370static int davinci_mcasp_probe(struct platform_device *pdev)
1371{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001372 struct snd_dmaengine_dai_dma_data *dma_data;
Jyri Sarha256ba182013-10-18 18:37:42 +03001373 struct resource *mem, *ioarea, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001374 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001375 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001376 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001377 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001378 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01001379 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001380
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301381 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
1382 dev_err(&pdev->dev, "No platform data supplied\n");
1383 return -EINVAL;
1384 }
1385
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001386 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01001387 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001388 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001389 return -ENOMEM;
1390
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301391 pdata = davinci_mcasp_set_pdata_from_of(pdev);
1392 if (!pdata) {
1393 dev_err(&pdev->dev, "no platform data\n");
1394 return -EINVAL;
1395 }
1396
Jyri Sarha256ba182013-10-18 18:37:42 +03001397 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001398 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001399 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03001400 "\"mpu\" mem resource not found, using index 0\n");
1401 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1402 if (!mem) {
1403 dev_err(&pdev->dev, "no mem resource?\n");
1404 return -ENODEV;
1405 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001406 }
1407
Julia Lawall96d31e22011-12-29 17:51:21 +01001408 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
Vaibhav Bediad852f4462011-02-09 18:39:52 +05301409 resource_size(mem), pdev->name);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001410 if (!ioarea) {
1411 dev_err(&pdev->dev, "Audio region already claimed\n");
Julia Lawall96d31e22011-12-29 17:51:21 +01001412 return -EBUSY;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001413 }
1414
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301415 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001416
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001417 mcasp->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
1418 if (!mcasp->base) {
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301419 dev_err(&pdev->dev, "ioremap failed\n");
1420 ret = -ENOMEM;
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001421 goto err;
Vaibhav Bedia4f82f022011-02-09 18:39:54 +05301422 }
1423
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001424 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02001425 /* sanity check for tdm slots parameter */
1426 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1427 if (pdata->tdm_slots < 2) {
1428 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1429 pdata->tdm_slots);
1430 mcasp->tdm_slots = 2;
1431 } else if (pdata->tdm_slots > 32) {
1432 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1433 pdata->tdm_slots);
1434 mcasp->tdm_slots = 32;
1435 } else {
1436 mcasp->tdm_slots = pdata->tdm_slots;
1437 }
1438 }
1439
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001440 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001441#ifdef CONFIG_PM_SLEEP
1442 mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
1443 sizeof(u32) * mcasp->num_serializer,
1444 GFP_KERNEL);
1445#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001446 mcasp->serial_dir = pdata->serial_dir;
1447 mcasp->version = pdata->version;
1448 mcasp->txnumevt = pdata->txnumevt;
1449 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02001450
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001451 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001452
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001453 irq = platform_get_irq_byname(pdev, "common");
1454 if (irq >= 0) {
1455 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common\n",
1456 dev_name(&pdev->dev));
1457 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1458 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02001459 IRQF_ONESHOT | IRQF_SHARED,
1460 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001461 if (ret) {
1462 dev_err(&pdev->dev, "common IRQ request failed\n");
1463 goto err;
1464 }
1465
1466 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1467 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1468 }
1469
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001470 irq = platform_get_irq_byname(pdev, "rx");
1471 if (irq >= 0) {
1472 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx\n",
1473 dev_name(&pdev->dev));
1474 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1475 davinci_mcasp_rx_irq_handler,
1476 IRQF_ONESHOT, irq_name, mcasp);
1477 if (ret) {
1478 dev_err(&pdev->dev, "RX IRQ request failed\n");
1479 goto err;
1480 }
1481
1482 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1483 }
1484
1485 irq = platform_get_irq_byname(pdev, "tx");
1486 if (irq >= 0) {
1487 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx\n",
1488 dev_name(&pdev->dev));
1489 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1490 davinci_mcasp_tx_irq_handler,
1491 IRQF_ONESHOT, irq_name, mcasp);
1492 if (ret) {
1493 dev_err(&pdev->dev, "TX IRQ request failed\n");
1494 goto err;
1495 }
1496
1497 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1498 }
1499
Jyri Sarha256ba182013-10-18 18:37:42 +03001500 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001501 if (dat)
1502 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03001503
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001504 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001505 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001506 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001507 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001508 dma_data->addr = mem->start + pdata->tx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001509
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001510 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001511 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001512 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001513 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001514 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001515 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07001516
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001517 /* dmaengine filter data for DT and non-DT boot */
1518 if (pdev->dev.of_node)
1519 dma_data->filter_data = "tx";
1520 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001521 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001522
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001523 /* RX is not valid in DIT mode */
1524 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001525 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001526 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001527 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001528 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001529 dma_data->addr = mem->start + pdata->rx_dma_offset;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001530
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001531 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001532 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1533 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001534 *dma = res->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001535 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001536 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001537
1538 /* dmaengine filter data for DT and non-DT boot */
1539 if (pdev->dev.of_node)
1540 dma_data->filter_data = "rx";
1541 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001542 dma_data->filter_data = dma;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001543 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001544
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001545 if (mcasp->version < MCASP_VERSION_3) {
1546 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001547 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001548 mcasp->dat_port = true;
1549 } else {
1550 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
1551 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001552
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001553 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001554
1555 mcasp_reparent_fck(pdev);
1556
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001557 ret = devm_snd_soc_register_component(&pdev->dev,
1558 &davinci_mcasp_component,
1559 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001560
1561 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001562 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301563
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001564 switch (mcasp->version) {
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03001565#if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
1566 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1567 IS_MODULE(CONFIG_SND_EDMA_SOC))
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001568 case MCASP_VERSION_1:
1569 case MCASP_VERSION_2:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03001570 case MCASP_VERSION_3:
1571 ret = edma_pcm_platform_register(&pdev->dev);
1572 break;
1573#endif
Jyri Sarha7f28f352014-06-13 12:49:59 +03001574#if IS_BUILTIN(CONFIG_SND_OMAP_SOC) || \
1575 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
1576 IS_MODULE(CONFIG_SND_OMAP_SOC))
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001577 case MCASP_VERSION_4:
1578 ret = omap_pcm_platform_register(&pdev->dev);
1579 break;
Jyri Sarha7f28f352014-06-13 12:49:59 +03001580#endif
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03001581 default:
1582 dev_err(&pdev->dev, "Invalid McASP version: %d\n",
1583 mcasp->version);
1584 ret = -EINVAL;
1585 break;
1586 }
1587
1588 if (ret) {
1589 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001590 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301591 }
1592
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001593 return 0;
1594
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03001595err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301596 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001597 return ret;
1598}
1599
1600static int davinci_mcasp_remove(struct platform_device *pdev)
1601{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301602 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001603
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001604 return 0;
1605}
1606
1607static struct platform_driver davinci_mcasp_driver = {
1608 .probe = davinci_mcasp_probe,
1609 .remove = davinci_mcasp_remove,
1610 .driver = {
1611 .name = "davinci-mcasp",
Sachin Kamatea421eb2013-05-22 16:53:37 +05301612 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001613 },
1614};
1615
Axel Linf9b8a512011-11-25 10:09:27 +08001616module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001617
1618MODULE_AUTHOR("Steve Chen");
1619MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1620MODULE_LICENSE("GPL");