blob: 79fc4aa2fa291068d87e0b07a4dd07712dbbc928 [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>
Jyri Sarhaa75a0532015-03-20 13:31:08 +020030#include <linux/math64.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040031
Daniel Mack64792852014-03-27 11:27:40 +010032#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040033#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/initval.h>
37#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020038#include <sound/dmaengine_pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040039
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +030040#include "edma-pcm.h"
Peter Ujfalusi077a4032018-05-09 14:03:55 +030041#include "../omap/sdma-pcm.h"
Chaithrika U Sb67f4482009-06-05 06:28:40 -040042#include "davinci-mcasp.h"
43
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +030044#define MCASP_MAX_AFIFO_DEPTH 64
45
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030046static u32 context_regs[] = {
47 DAVINCI_MCASP_TXFMCTL_REG,
48 DAVINCI_MCASP_RXFMCTL_REG,
49 DAVINCI_MCASP_TXFMT_REG,
50 DAVINCI_MCASP_RXFMT_REG,
51 DAVINCI_MCASP_ACLKXCTL_REG,
52 DAVINCI_MCASP_ACLKRCTL_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030053 DAVINCI_MCASP_AHCLKXCTL_REG,
54 DAVINCI_MCASP_AHCLKRCTL_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030055 DAVINCI_MCASP_PDIR_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030056 DAVINCI_MCASP_RXMASK_REG,
57 DAVINCI_MCASP_TXMASK_REG,
58 DAVINCI_MCASP_RXTDM_REG,
59 DAVINCI_MCASP_TXTDM_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030060};
61
Peter Ujfalusi790bb942014-02-03 14:51:52 +020062struct davinci_mcasp_context {
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030063 u32 config_regs[ARRAY_SIZE(context_regs)];
Peter Ujfalusif114ce62014-10-01 16:02:12 +030064 u32 afifo_regs[2]; /* for read/write fifo control registers */
65 u32 *xrsr_regs; /* for serializer configuration */
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +020066 bool pm_state;
Peter Ujfalusi790bb942014-02-03 14:51:52 +020067};
68
Jyri Sarhaa75a0532015-03-20 13:31:08 +020069struct davinci_mcasp_ruledata {
70 struct davinci_mcasp *mcasp;
71 int serializers;
72};
73
Peter Ujfalusi70091a32013-11-14 11:35:29 +020074struct davinci_mcasp {
Peter Ujfalusi453c4992013-11-14 11:35:34 +020075 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020076 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020077 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020078 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020079 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +020080 unsigned int dai_fmt;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020081
82 /* McASP specific data */
83 int tdm_slots;
Jyri Sarhadd55ff82015-09-09 21:27:44 +030084 u32 tdm_mask[2];
85 int slot_width;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020086 u8 op_mode;
87 u8 num_serializer;
88 u8 *serial_dir;
89 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020090 u8 bclk_div;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020091 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020092 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020093 int dma_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020094
Jyri Sarhaab8b14b2014-01-27 17:37:52 +020095 int sysclk_freq;
96 bool bclk_master;
97
Peter Ujfalusi21400a72013-11-14 11:35:26 +020098 /* McASP FIFO related */
99 u8 txnumevt;
100 u8 rxnumevt;
101
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200102 bool dat_port;
103
Peter Ujfalusi11277832014-11-10 12:32:16 +0200104 /* Used for comstraint setting on the second stream */
105 u32 channels;
106
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200107#ifdef CONFIG_PM_SLEEP
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200108 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200109#endif
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200110
111 struct davinci_mcasp_ruledata ruledata[2];
Jyri Sarha5935a052015-04-23 16:16:05 +0300112 struct snd_pcm_hw_constraint_list chconstr[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200113};
114
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200115static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
116 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400117{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200118 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400119 __raw_writel(__raw_readl(reg) | val, reg);
120}
121
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200122static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
123 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400124{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200125 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400126 __raw_writel((__raw_readl(reg) & ~(val)), reg);
127}
128
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200129static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
130 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400131{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200132 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400133 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
134}
135
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200136static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
137 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400138{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200139 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400140}
141
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200142static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400143{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200144 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400145}
146
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200147static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400148{
149 int i = 0;
150
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200151 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400152
153 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
154 /* loop count is to avoid the lock-up */
155 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200156 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400157 break;
158 }
159
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200160 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400161 printk(KERN_ERR "GBLCTL write error\n");
162}
163
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200164static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
165{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200166 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
167 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200168
169 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
170}
171
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200172static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400173{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200174 if (mcasp->rxnumevt) { /* enable FIFO */
175 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
176
177 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
178 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
179 }
180
Peter Ujfalusi44982732014-10-29 13:55:45 +0200181 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200182 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
183 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200184 /*
185 * When ASYNC == 0 the transmit and receive sections operate
186 * synchronously from the transmit clock and frame sync. We need to make
187 * sure that the TX signlas are enabled when starting reception.
188 */
189 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200190 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
191 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200192 }
193
Peter Ujfalusi44982732014-10-29 13:55:45 +0200194 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200195 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200196 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200197 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200198 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200199 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200200 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200201 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200202
203 /* enable receive IRQs */
204 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
205 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400206}
207
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200208static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400209{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400210 u32 cnt;
211
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200212 if (mcasp->txnumevt) { /* enable FIFO */
213 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
214
215 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
216 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
217 }
218
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200219 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200220 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
221 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200222 /* Activate serializer(s) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200223 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400224
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200225 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400226 cnt = 0;
Peter Ujfalusie2a0c9f2015-12-11 13:06:24 +0200227 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
228 (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400229 cnt++;
230
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200231 /* Release TX state machine */
232 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
233 /* Release Frame Sync generator */
234 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200235
236 /* enable transmit IRQs */
237 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
238 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400239}
240
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200241static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400242{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200243 mcasp->streams++;
244
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200245 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200246 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200247 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200248 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400249}
250
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200251static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400252{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200253 /* disable IRQ sources */
254 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
255 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
256
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200257 /*
258 * In synchronous mode stop the TX clocks if no other stream is
259 * running
260 */
261 if (mcasp_is_synchronous(mcasp) && !mcasp->streams)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200262 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200263
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200264 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
265 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200266
267 if (mcasp->rxnumevt) { /* disable FIFO */
268 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
269
270 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
271 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400272}
273
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200274static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400275{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200276 u32 val = 0;
277
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200278 /* disable IRQ sources */
279 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
280 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
281
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200282 /*
283 * In synchronous mode keep TX clocks running if the capture stream is
284 * still running.
285 */
286 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
287 val = TXHCLKRST | TXCLKRST | TXFSRST;
288
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200289 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
290 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200291
292 if (mcasp->txnumevt) { /* disable FIFO */
293 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
294
295 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
296 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400297}
298
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200299static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400300{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200301 mcasp->streams--;
302
Peter Ujfalusi03808662014-10-29 13:55:46 +0200303 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200304 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200305 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200306 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400307}
308
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200309static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
310{
311 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
312 struct snd_pcm_substream *substream;
313 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
314 u32 handled_mask = 0;
315 u32 stat;
316
317 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
318 if (stat & XUNDRN & irq_mask) {
319 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
320 handled_mask |= XUNDRN;
321
322 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
323 if (substream) {
324 snd_pcm_stream_lock_irq(substream);
325 if (snd_pcm_running(substream))
326 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
327 snd_pcm_stream_unlock_irq(substream);
328 }
329 }
330
331 if (!handled_mask)
332 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
333 stat);
334
335 if (stat & XRERR)
336 handled_mask |= XRERR;
337
338 /* Ack the handled event only */
339 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
340
341 return IRQ_RETVAL(handled_mask);
342}
343
344static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
345{
346 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
347 struct snd_pcm_substream *substream;
348 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
349 u32 handled_mask = 0;
350 u32 stat;
351
352 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
353 if (stat & ROVRN & irq_mask) {
354 dev_warn(mcasp->dev, "Receive buffer overflow\n");
355 handled_mask |= ROVRN;
356
357 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
358 if (substream) {
359 snd_pcm_stream_lock_irq(substream);
360 if (snd_pcm_running(substream))
361 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
362 snd_pcm_stream_unlock_irq(substream);
363 }
364 }
365
366 if (!handled_mask)
367 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
368 stat);
369
370 if (stat & XRERR)
371 handled_mask |= XRERR;
372
373 /* Ack the handled event only */
374 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
375
376 return IRQ_RETVAL(handled_mask);
377}
378
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200379static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
380{
381 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
382 irqreturn_t ret = IRQ_NONE;
383
384 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
385 ret = davinci_mcasp_tx_irq_handler(irq, data);
386
387 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
388 ret |= davinci_mcasp_rx_irq_handler(irq, data);
389
390 return ret;
391}
392
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400393static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
394 unsigned int fmt)
395{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200396 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200397 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300398 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300399 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300400 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400401
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200402 if (!fmt)
403 return 0;
404
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200405 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200406 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300407 case SND_SOC_DAIFMT_DSP_A:
408 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
409 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300410 /* 1st data bit occur one ACLK cycle after the frame sync */
411 data_delay = 1;
412 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200413 case SND_SOC_DAIFMT_DSP_B:
414 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200415 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
416 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300417 /* No delay after FS */
418 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200419 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300420 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200421 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200422 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
423 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300424 /* 1st data bit occur one ACLK cycle after the frame sync */
425 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300426 /* FS need to be inverted */
427 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200428 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300429 case SND_SOC_DAIFMT_LEFT_J:
430 /* configure a full-word SYNC pulse (LRCLK) */
431 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
432 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
433 /* No delay after FS */
434 data_delay = 0;
435 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300436 default:
437 ret = -EINVAL;
438 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200439 }
440
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300441 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
442 FSXDLY(3));
443 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
444 FSRDLY(3));
445
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400446 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
447 case SND_SOC_DAIFMT_CBS_CFS:
448 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200449 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
450 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400451
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200452 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
453 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400454
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200455 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
456 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200457 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400458 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200459 case SND_SOC_DAIFMT_CBS_CFM:
460 /* codec is clock slave and frame master */
461 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
462 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
463
464 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
465 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
466
467 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
468 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
469 mcasp->bclk_master = 1;
470 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400471 case SND_SOC_DAIFMT_CBM_CFS:
472 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200473 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
474 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400475
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200476 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
477 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400478
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200479 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, ACLKX | ACLKR);
480 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AFSX | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200481 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400482 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400483 case SND_SOC_DAIFMT_CBM_CFM:
484 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200485 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
486 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400487
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200488 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
489 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400490
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200491 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG,
Jim Lodes823ecdd2016-04-25 11:08:10 -0500492 ACLKX | AFSX | ACLKR | AHCLKR | AFSR);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200493 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400494 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400495 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200496 ret = -EINVAL;
497 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400498 }
499
500 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
501 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200502 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300503 mcasp_clr_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 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200507 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300508 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300509 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400510 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400511 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200512 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300513 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300514 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400515 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400516 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200517 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200518 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300519 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400520 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400521 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200522 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300523 goto out;
524 }
525
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300526 if (inv_fs)
527 fs_pol_rising = !fs_pol_rising;
528
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300529 if (fs_pol_rising) {
530 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
531 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
532 } else {
533 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
534 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400535 }
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200536
537 mcasp->dai_fmt = fmt;
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200538out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200539 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200540 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400541}
542
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300543static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
Jyri Sarha88135432014-08-06 16:47:16 +0300544 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200545{
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200546 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200547 switch (div_id) {
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300548 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200549 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200550 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200551 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200552 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
553 break;
554
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300555 case MCASP_CLKDIV_BCLK: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200556 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200557 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200558 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200559 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300560 if (explicit)
561 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200562 break;
563
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300564 case MCASP_CLKDIV_BCLK_FS_RATIO:
565 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300566 * BCLK/LRCLK ratio descries how many bit-clock cycles
567 * fit into one frame. The clock ratio is given for a
568 * full period of data (for I2S format both left and
569 * right channels), so it has to be divided by number
570 * of tdm-slots (for I2S - divided by 2).
571 * Instead of storing this ratio, we calculate a new
572 * tdm_slot width by dividing the the ratio by the
573 * number of configured tdm slots.
574 */
575 mcasp->slot_width = div / mcasp->tdm_slots;
576 if (div % mcasp->tdm_slots)
577 dev_warn(mcasp->dev,
578 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
579 __func__, div, mcasp->tdm_slots);
Daniel Mack1b3bc062012-12-05 18:20:38 +0100580 break;
581
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200582 default:
583 return -EINVAL;
584 }
585
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200586 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200587 return 0;
588}
589
Jyri Sarha88135432014-08-06 16:47:16 +0300590static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
591 int div)
592{
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300593 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
594
595 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
Jyri Sarha88135432014-08-06 16:47:16 +0300596}
597
Daniel Mack5b66aa22012-10-04 15:08:41 +0200598static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
599 unsigned int freq, int dir)
600{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200601 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200602
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200603 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200604 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200605 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
606 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
607 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200608 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200609 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
610 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
611 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200612 }
613
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200614 mcasp->sysclk_freq = freq;
615
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200616 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200617 return 0;
618}
619
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300620/* All serializers must have equal number of channels */
621static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
622 int serializers)
623{
624 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
625 unsigned int *list = (unsigned int *) cl->list;
626 int slots = mcasp->tdm_slots;
627 int i, count = 0;
628
629 if (mcasp->tdm_mask[stream])
630 slots = hweight32(mcasp->tdm_mask[stream]);
631
Peter Ujfalusie4798d22017-05-11 09:58:22 +0300632 for (i = 1; i <= slots; i++)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300633 list[count++] = i;
634
635 for (i = 2; i <= serializers; i++)
636 list[count++] = i*slots;
637
638 cl->count = count;
639
640 return 0;
641}
642
643static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
644{
645 int rx_serializers = 0, tx_serializers = 0, ret, i;
646
647 for (i = 0; i < mcasp->num_serializer; i++)
648 if (mcasp->serial_dir[i] == TX_MODE)
649 tx_serializers++;
650 else if (mcasp->serial_dir[i] == RX_MODE)
651 rx_serializers++;
652
653 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
654 tx_serializers);
655 if (ret)
656 return ret;
657
658 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
659 rx_serializers);
660
661 return ret;
662}
663
664
665static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
666 unsigned int tx_mask,
667 unsigned int rx_mask,
668 int slots, int slot_width)
669{
670 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
671
672 dev_dbg(mcasp->dev,
673 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
674 __func__, tx_mask, rx_mask, slots, slot_width);
675
676 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
677 dev_err(mcasp->dev,
678 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
679 tx_mask, rx_mask, slots);
680 return -EINVAL;
681 }
682
683 if (slot_width &&
684 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
685 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
686 __func__, slot_width);
687 return -EINVAL;
688 }
689
690 mcasp->tdm_slots = slots;
Andreas Dannenberg1bdd5932015-11-09 12:19:19 -0600691 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
692 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300693 mcasp->slot_width = slot_width;
694
695 return davinci_mcasp_set_ch_constraints(mcasp);
696}
697
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200698static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Jyri Sarha14a998b2015-09-17 10:39:05 +0300699 int sample_width)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400700{
Daniel Mackba764b32012-12-05 18:20:37 +0100701 u32 fmt;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300702 u32 tx_rotate = (sample_width / 4) & 0x7;
703 u32 mask = (1ULL << sample_width) - 1;
704 u32 slot_width = sample_width;
705
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300706 /*
707 * For captured data we should not rotate, inversion and masking is
708 * enoguh to get the data to the right position:
709 * Format data from bus after reverse (XRBUF)
710 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
711 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
712 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
713 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
714 */
715 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400716
Daniel Mack1b3bc062012-12-05 18:20:38 +0100717 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300718 * Setting the tdm slot width either with set_clkdiv() or
719 * set_tdm_slot() allows us to for example send 32 bits per
720 * channel to the codec, while only 16 of them carry audio
721 * payload.
Daniel Mack1b3bc062012-12-05 18:20:38 +0100722 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300723 if (mcasp->slot_width) {
Peter Ujfalusid742b922014-11-10 12:32:19 +0200724 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300725 * When we have more bclk then it is needed for the
726 * data, we need to use the rotation to move the
727 * received samples to have correct alignment.
Peter Ujfalusid742b922014-11-10 12:32:19 +0200728 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300729 slot_width = mcasp->slot_width;
730 rx_rotate = (slot_width - sample_width) / 4;
Peter Ujfalusid742b922014-11-10 12:32:19 +0200731 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100732
Daniel Mackba764b32012-12-05 18:20:37 +0100733 /* mapping of the XSSZ bit-field as described in the datasheet */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300734 fmt = (slot_width >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400735
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200736 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200737 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
738 RXSSZ(0x0F));
739 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
740 TXSSZ(0x0F));
741 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
742 TXROT(7));
743 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
744 RXROT(7));
745 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200746 }
747
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200748 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400749
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400750 return 0;
751}
752
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200753static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300754 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400755{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300756 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400757 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400758 u8 tx_ser = 0;
759 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200760 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100761 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusi72383192015-09-14 16:06:48 +0300762 int active_serializers, numevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200763 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400764 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300765 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200766 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400767
768 /* All PINS as McASP */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200769 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400770
771 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200772 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
773 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400774 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200775 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
776 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400777 }
778
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200779 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200780 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
781 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200782 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100783 tx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200784 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Misael Lopez Cruz19db62e2015-06-08 16:03:47 +0300785 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
786 DISMOD_LOW, DISMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400787 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200788 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100789 rx_ser < max_active_serializers) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200790 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400791 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100792 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200793 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
794 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400795 }
796 }
797
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300798 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
799 active_serializers = tx_ser;
800 numevt = mcasp->txnumevt;
801 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
802 } else {
803 active_serializers = rx_ser;
804 numevt = mcasp->rxnumevt;
805 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
806 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100807
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300808 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200809 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300810 "enabled in mcasp (%d)\n", channels,
811 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100812 return -EINVAL;
813 }
814
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300815 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300816 if (!numevt) {
817 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300818 if (active_serializers > 1) {
819 /*
820 * If more than one serializers are in use we have one
821 * DMA request to provide data for all serializers.
822 * For example if three serializers are enabled the DMA
823 * need to transfer three words per DMA request.
824 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300825 dma_data->maxburst = active_serializers;
826 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300827 dma_data->maxburst = 0;
828 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300829 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300830 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400831
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300832 if (period_words % active_serializers) {
833 dev_err(mcasp->dev, "Invalid combination of period words and "
834 "active serializers: %d, %d\n", period_words,
835 active_serializers);
836 return -EINVAL;
837 }
838
839 /*
840 * Calculate the optimal AFIFO depth for platform side:
841 * The number of words for numevt need to be in steps of active
842 * serializers.
843 */
Peter Ujfalusi72383192015-09-14 16:06:48 +0300844 numevt = (numevt / active_serializers) * active_serializers;
845
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300846 while (period_words % numevt && numevt > 0)
847 numevt -= active_serializers;
848 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300849 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400850
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300851 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
852 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100853
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300854 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300855 if (numevt == 1)
856 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300857 dma_data->maxburst = numevt;
858
Michal Bachraty2952b272013-02-28 16:07:08 +0100859 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400860}
861
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200862static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
863 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400864{
865 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200866 int total_slots;
867 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400868 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200869 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400870
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200871 total_slots = mcasp->tdm_slots;
872
873 /*
874 * If more than one serializer is needed, then use them with
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300875 * all the specified tdm_slots. Otherwise, one serializer can
876 * cope with the transaction using just as many slots as there
877 * are channels in the stream.
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200878 */
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300879 if (mcasp->tdm_mask[stream]) {
880 active_slots = hweight32(mcasp->tdm_mask[stream]);
881 active_serializers = (channels + active_slots - 1) /
882 active_slots;
883 if (active_serializers == 1) {
884 active_slots = channels;
885 for (i = 0; i < total_slots; i++) {
886 if ((1 << i) & mcasp->tdm_mask[stream]) {
887 mask |= (1 << i);
888 if (--active_slots <= 0)
889 break;
890 }
891 }
892 }
893 } else {
894 active_serializers = (channels + total_slots - 1) / total_slots;
895 if (active_serializers == 1)
896 active_slots = channels;
897 else
898 active_slots = total_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200899
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300900 for (i = 0; i < active_slots; i++)
901 mask |= (1 << i);
902 }
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200903 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400904
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200905 if (!mcasp->dat_port)
906 busel = TXSEL;
907
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300908 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
909 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
910 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
911 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
912 FSXMOD(total_slots), FSXMOD(0x1FF));
913 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
914 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
915 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
916 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
917 FSRMOD(total_slots), FSRMOD(0x1FF));
Peter Ujfalusi0ad7d3a2015-11-23 12:51:53 +0200918 /*
919 * If McASP is set to be TX/RX synchronous and the playback is
920 * not running already we need to configure the TX slots in
921 * order to have correct FSX on the bus
922 */
923 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
924 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
925 FSXMOD(total_slots), FSXMOD(0x1FF));
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300926 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400927
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200928 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400929}
930
931/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100932static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
933 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400934{
Daniel Mack64792852014-03-27 11:27:40 +0100935 u32 cs_value = 0;
936 u8 *cs_bytes = (u8*) &cs_value;
937
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400938 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
939 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200940 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400941
942 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200943 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400944
945 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200946 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400947
948 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200949 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400950
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200951 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400952
953 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200954 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400955
956 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200957 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200958
Daniel Mack64792852014-03-27 11:27:40 +0100959 /* Set S/PDIF channel status bits */
960 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
961 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
962
963 switch (rate) {
964 case 22050:
965 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
966 break;
967 case 24000:
968 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
969 break;
970 case 32000:
971 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
972 break;
973 case 44100:
974 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
975 break;
976 case 48000:
977 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
978 break;
979 case 88200:
980 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
981 break;
982 case 96000:
983 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
984 break;
985 case 176400:
986 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
987 break;
988 case 192000:
989 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
990 break;
991 default:
992 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
993 return -EINVAL;
994 }
995
996 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
997 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
998
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200999 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001000}
1001
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001002static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001003 unsigned int bclk_freq, bool set)
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001004{
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001005 int error_ppm;
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001006 unsigned int sysclk_freq = mcasp->sysclk_freq;
1007 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1008 int div = sysclk_freq / bclk_freq;
1009 int rem = sysclk_freq % bclk_freq;
1010 int aux_div = 1;
1011
1012 if (div > (ACLKXDIV_MASK + 1)) {
1013 if (reg & AHCLKXE) {
1014 aux_div = div / (ACLKXDIV_MASK + 1);
1015 if (div % (ACLKXDIV_MASK + 1))
1016 aux_div++;
1017
1018 sysclk_freq /= aux_div;
1019 div = sysclk_freq / bclk_freq;
1020 rem = sysclk_freq % bclk_freq;
1021 } else if (set) {
1022 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1023 sysclk_freq);
1024 }
1025 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001026
1027 if (rem != 0) {
1028 if (div == 0 ||
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001029 ((sysclk_freq / div) - bclk_freq) >
1030 (bclk_freq - (sysclk_freq / (div+1)))) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001031 div++;
1032 rem = rem - bclk_freq;
1033 }
1034 }
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001035 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1036 (int)bclk_freq)) / div - 1000000;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001037
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001038 if (set) {
1039 if (error_ppm)
1040 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1041 error_ppm);
1042
1043 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001044 if (reg & AHCLKXE)
1045 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1046 aux_div, 0);
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001047 }
1048
1049 return error_ppm;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001050}
1051
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001052static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1053 struct snd_pcm_hw_params *params,
1054 struct snd_soc_dai *cpu_dai)
1055{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001056 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001057 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +02001058 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001059 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001060 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001061
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +02001062 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1063 if (ret)
1064 return ret;
1065
Daniel Mack82675252014-07-16 14:04:41 +02001066 /*
1067 * If mcasp is BCLK master, and a BCLK divider was not provided by
1068 * the machine driver, we need to calculate the ratio.
1069 */
1070 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001071 int slots = mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001072 int rate = params_rate(params);
1073 int sbits = params_width(params);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001074
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001075 if (mcasp->slot_width)
1076 sbits = mcasp->slot_width;
1077
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001078 davinci_mcasp_calc_clk_div(mcasp, rate * sbits * slots, true);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001079 }
1080
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001081 ret = mcasp_common_hw_param(mcasp, substream->stream,
1082 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +02001083 if (ret)
1084 return ret;
1085
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001086 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +01001087 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001088 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +02001089 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1090 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001091
1092 if (ret)
1093 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001094
1095 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001096 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001097 case SNDRV_PCM_FORMAT_S8:
Daniel Mackba764b32012-12-05 18:20:37 +01001098 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001099 break;
1100
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001101 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001102 case SNDRV_PCM_FORMAT_S16_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001103 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001104 break;
1105
Daniel Mack21eb24d2012-10-09 09:35:16 +02001106 case SNDRV_PCM_FORMAT_U24_3LE:
1107 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001108 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +02001109 break;
1110
Daniel Mack6b7fa012012-10-09 11:56:40 +02001111 case SNDRV_PCM_FORMAT_U24_LE:
1112 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +03001113 word_length = 24;
1114 break;
1115
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001116 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001117 case SNDRV_PCM_FORMAT_S32_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001118 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001119 break;
1120
1121 default:
1122 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1123 return -EINVAL;
1124 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -04001125
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001126 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001127
Peter Ujfalusi11277832014-11-10 12:32:16 +02001128 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
1129 mcasp->channels = channels;
1130
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001131 return 0;
1132}
1133
1134static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1135 int cmd, struct snd_soc_dai *cpu_dai)
1136{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001137 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001138 int ret = 0;
1139
1140 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001141 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +05301142 case SNDRV_PCM_TRIGGER_START:
1143 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001144 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001145 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001146 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +05301147 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001148 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001149 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001150 break;
1151
1152 default:
1153 ret = -EINVAL;
1154 }
1155
1156 return ret;
1157}
1158
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001159static const unsigned int davinci_mcasp_dai_rates[] = {
1160 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1161 88200, 96000, 176400, 192000,
1162};
1163
1164#define DAVINCI_MAX_RATE_ERROR_PPM 1000
1165
1166static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1167 struct snd_pcm_hw_rule *rule)
1168{
1169 struct davinci_mcasp_ruledata *rd = rule->private;
1170 struct snd_interval *ri =
1171 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1172 int sbits = params_width(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001173 int slots = rd->mcasp->tdm_slots;
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001174 struct snd_interval range;
1175 int i;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001176
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001177 if (rd->mcasp->slot_width)
1178 sbits = rd->mcasp->slot_width;
1179
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001180 snd_interval_any(&range);
1181 range.empty = 1;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001182
1183 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001184 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001185 uint bclk_freq = sbits*slots*
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001186 davinci_mcasp_dai_rates[i];
1187 int ppm;
1188
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001189 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, bclk_freq,
1190 false);
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001191 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1192 if (range.empty) {
1193 range.min = davinci_mcasp_dai_rates[i];
1194 range.empty = 0;
1195 }
1196 range.max = davinci_mcasp_dai_rates[i];
1197 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001198 }
1199 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001200
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001201 dev_dbg(rd->mcasp->dev,
1202 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1203 ri->min, ri->max, range.min, range.max, sbits, slots);
1204
1205 return snd_interval_refine(hw_param_interval(params, rule->var),
1206 &range);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001207}
1208
1209static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1210 struct snd_pcm_hw_rule *rule)
1211{
1212 struct davinci_mcasp_ruledata *rd = rule->private;
1213 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1214 struct snd_mask nfmt;
1215 int rate = params_rate(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001216 int slots = rd->mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001217 int i, count = 0;
1218
1219 snd_mask_none(&nfmt);
1220
Peter Ujfalusi9be072a2016-09-01 10:05:12 +03001221 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001222 if (snd_mask_test(fmt, i)) {
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001223 uint sbits = snd_pcm_format_width(i);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001224 int ppm;
1225
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001226 if (rd->mcasp->slot_width)
1227 sbits = rd->mcasp->slot_width;
1228
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001229 ppm = davinci_mcasp_calc_clk_div(rd->mcasp,
1230 sbits * slots * rate,
1231 false);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001232 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1233 snd_mask_set(&nfmt, i);
1234 count++;
1235 }
1236 }
1237 }
1238 dev_dbg(rd->mcasp->dev,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001239 "%d possible sample format for %d Hz and %d tdm slots\n",
1240 count, rate, slots);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001241
1242 return snd_mask_refine(fmt, &nfmt);
1243}
1244
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001245static int davinci_mcasp_hw_rule_min_periodsize(
1246 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1247{
1248 struct snd_interval *period_size = hw_param_interval(params,
1249 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1250 struct snd_interval frames;
1251
1252 snd_interval_any(&frames);
1253 frames.min = 64;
1254 frames.integer = 1;
1255
1256 return snd_interval_refine(period_size, &frames);
1257}
1258
Peter Ujfalusi11277832014-11-10 12:32:16 +02001259static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1260 struct snd_soc_dai *cpu_dai)
1261{
1262 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001263 struct davinci_mcasp_ruledata *ruledata =
1264 &mcasp->ruledata[substream->stream];
Peter Ujfalusi11277832014-11-10 12:32:16 +02001265 u32 max_channels = 0;
1266 int i, dir;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001267 int tdm_slots = mcasp->tdm_slots;
1268
Peter Ujfalusi19357362016-05-09 13:39:14 +03001269 /* Do not allow more then one stream per direction */
1270 if (mcasp->substreams[substream->stream])
1271 return -EBUSY;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001272
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001273 mcasp->substreams[substream->stream] = substream;
1274
Peter Ujfalusi19357362016-05-09 13:39:14 +03001275 if (mcasp->tdm_mask[substream->stream])
1276 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1277
Peter Ujfalusi11277832014-11-10 12:32:16 +02001278 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1279 return 0;
1280
1281 /*
1282 * Limit the maximum allowed channels for the first stream:
1283 * number of serializers for the direction * tdm slots per serializer
1284 */
1285 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1286 dir = TX_MODE;
1287 else
1288 dir = RX_MODE;
1289
1290 for (i = 0; i < mcasp->num_serializer; i++) {
1291 if (mcasp->serial_dir[i] == dir)
1292 max_channels++;
1293 }
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001294 ruledata->serializers = max_channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001295 max_channels *= tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001296 /*
1297 * If the already active stream has less channels than the calculated
1298 * limnit based on the seirializers * tdm_slots, we need to use that as
1299 * a constraint for the second stream.
1300 * Otherwise (first stream or less allowed channels) we use the
1301 * calculated constraint.
1302 */
1303 if (mcasp->channels && mcasp->channels < max_channels)
1304 max_channels = mcasp->channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001305 /*
1306 * But we can always allow channels upto the amount of
1307 * the available tdm_slots.
1308 */
1309 if (max_channels < tdm_slots)
1310 max_channels = tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001311
1312 snd_pcm_hw_constraint_minmax(substream->runtime,
1313 SNDRV_PCM_HW_PARAM_CHANNELS,
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001314 0, max_channels);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001315
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001316 snd_pcm_hw_constraint_list(substream->runtime,
1317 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1318 &mcasp->chconstr[substream->stream]);
1319
1320 if (mcasp->slot_width)
1321 snd_pcm_hw_constraint_minmax(substream->runtime,
1322 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1323 8, mcasp->slot_width);
Jyri Sarha5935a052015-04-23 16:16:05 +03001324
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001325 /*
1326 * If we rely on implicit BCLK divider setting we should
1327 * set constraints based on what we can provide.
1328 */
1329 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1330 int ret;
1331
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001332 ruledata->mcasp = mcasp;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001333
1334 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1335 SNDRV_PCM_HW_PARAM_RATE,
1336 davinci_mcasp_hw_rule_rate,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001337 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001338 SNDRV_PCM_HW_PARAM_FORMAT, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001339 if (ret)
1340 return ret;
1341 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1342 SNDRV_PCM_HW_PARAM_FORMAT,
1343 davinci_mcasp_hw_rule_format,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001344 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001345 SNDRV_PCM_HW_PARAM_RATE, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001346 if (ret)
1347 return ret;
1348 }
1349
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001350 snd_pcm_hw_rule_add(substream->runtime, 0,
1351 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1352 davinci_mcasp_hw_rule_min_periodsize, NULL,
1353 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1354
Peter Ujfalusi11277832014-11-10 12:32:16 +02001355 return 0;
1356}
1357
1358static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1359 struct snd_soc_dai *cpu_dai)
1360{
1361 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1362
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001363 mcasp->substreams[substream->stream] = NULL;
1364
Peter Ujfalusi11277832014-11-10 12:32:16 +02001365 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1366 return;
1367
1368 if (!cpu_dai->active)
1369 mcasp->channels = 0;
1370}
1371
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001372static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001373 .startup = davinci_mcasp_startup,
1374 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001375 .trigger = davinci_mcasp_trigger,
1376 .hw_params = davinci_mcasp_hw_params,
1377 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001378 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001379 .set_sysclk = davinci_mcasp_set_sysclk,
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001380 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001381};
1382
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001383static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1384{
1385 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1386
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001387 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1388 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001389
1390 return 0;
1391}
1392
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001393#ifdef CONFIG_PM_SLEEP
1394static int davinci_mcasp_suspend(struct snd_soc_dai *dai)
1395{
1396 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001397 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001398 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001399 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001400
Peter Ujfalusi27796e72015-04-30 11:57:41 +03001401 context->pm_state = pm_runtime_active(mcasp->dev);
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001402 if (!context->pm_state)
1403 pm_runtime_get_sync(mcasp->dev);
1404
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001405 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1406 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001407
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001408 if (mcasp->txnumevt) {
1409 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1410 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
1411 }
1412 if (mcasp->rxnumevt) {
1413 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1414 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
1415 }
1416
1417 for (i = 0; i < mcasp->num_serializer; i++)
1418 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
1419 DAVINCI_MCASP_XRSRCTL_REG(i));
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001420
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001421 pm_runtime_put_sync(mcasp->dev);
1422
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001423 return 0;
1424}
1425
1426static int davinci_mcasp_resume(struct snd_soc_dai *dai)
1427{
1428 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Peter Ujfalusi790bb942014-02-03 14:51:52 +02001429 struct davinci_mcasp_context *context = &mcasp->context;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001430 u32 reg;
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001431 int i;
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001432
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001433 pm_runtime_get_sync(mcasp->dev);
1434
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +03001435 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
1436 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001437
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001438 if (mcasp->txnumevt) {
1439 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
1440 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
1441 }
1442 if (mcasp->rxnumevt) {
1443 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
1444 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
1445 }
1446
1447 for (i = 0; i < mcasp->num_serializer; i++)
1448 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
1449 context->xrsr_regs[i]);
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001450
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +02001451 if (!context->pm_state)
1452 pm_runtime_put_sync(mcasp->dev);
1453
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001454 return 0;
1455}
1456#else
1457#define davinci_mcasp_suspend NULL
1458#define davinci_mcasp_resume NULL
1459#endif
1460
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001461#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1462
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001463#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1464 SNDRV_PCM_FMTBIT_U8 | \
1465 SNDRV_PCM_FMTBIT_S16_LE | \
1466 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001467 SNDRV_PCM_FMTBIT_S24_LE | \
1468 SNDRV_PCM_FMTBIT_U24_LE | \
1469 SNDRV_PCM_FMTBIT_S24_3LE | \
1470 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001471 SNDRV_PCM_FMTBIT_S32_LE | \
1472 SNDRV_PCM_FMTBIT_U32_LE)
1473
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001474static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001475 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001476 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001477 .probe = davinci_mcasp_dai_probe,
Peter Ujfalusi135014a2014-01-30 15:21:32 +02001478 .suspend = davinci_mcasp_suspend,
1479 .resume = davinci_mcasp_resume,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001480 .playback = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001481 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001482 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001483 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001484 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001485 },
1486 .capture = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001487 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001488 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001489 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001490 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001491 },
1492 .ops = &davinci_mcasp_dai_ops,
1493
Peter Ujfalusid75249f2014-11-10 12:32:18 +02001494 .symmetric_samplebits = 1,
Jyri Sarha295c3402015-09-09 21:27:42 +03001495 .symmetric_rates = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001496 },
1497 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001498 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001499 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001500 .playback = {
1501 .channels_min = 1,
1502 .channels_max = 384,
1503 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001504 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001505 },
1506 .ops = &davinci_mcasp_dai_ops,
1507 },
1508
1509};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001510
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001511static const struct snd_soc_component_driver davinci_mcasp_component = {
1512 .name = "davinci-mcasp",
1513};
1514
Jyri Sarha256ba182013-10-18 18:37:42 +03001515/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001516static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001517 .tx_dma_offset = 0x400,
1518 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001519 .version = MCASP_VERSION_1,
1520};
1521
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001522static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001523 .tx_dma_offset = 0x2000,
1524 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001525 .version = MCASP_VERSION_2,
1526};
1527
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001528static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001529 .tx_dma_offset = 0,
1530 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001531 .version = MCASP_VERSION_3,
1532};
1533
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001534static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001535 /* The CFG port offset will be calculated if it is needed */
1536 .tx_dma_offset = 0,
1537 .rx_dma_offset = 0,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001538 .version = MCASP_VERSION_4,
1539};
1540
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301541static const struct of_device_id mcasp_dt_ids[] = {
1542 {
1543 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001544 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301545 },
1546 {
1547 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001548 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301549 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301550 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001551 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001552 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301553 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001554 {
1555 .compatible = "ti,dra7-mcasp-audio",
1556 .data = &dra7_mcasp_pdata,
1557 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301558 { /* sentinel */ }
1559};
1560MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1561
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001562static int mcasp_reparent_fck(struct platform_device *pdev)
1563{
1564 struct device_node *node = pdev->dev.of_node;
1565 struct clk *gfclk, *parent_clk;
1566 const char *parent_name;
1567 int ret;
1568
1569 if (!node)
1570 return 0;
1571
1572 parent_name = of_get_property(node, "fck_parent", NULL);
1573 if (!parent_name)
1574 return 0;
1575
Peter Ujfalusic6702542016-01-27 15:02:49 +02001576 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1577
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001578 gfclk = clk_get(&pdev->dev, "fck");
1579 if (IS_ERR(gfclk)) {
1580 dev_err(&pdev->dev, "failed to get fck\n");
1581 return PTR_ERR(gfclk);
1582 }
1583
1584 parent_clk = clk_get(NULL, parent_name);
1585 if (IS_ERR(parent_clk)) {
1586 dev_err(&pdev->dev, "failed to get parent clock\n");
1587 ret = PTR_ERR(parent_clk);
1588 goto err1;
1589 }
1590
1591 ret = clk_set_parent(gfclk, parent_clk);
1592 if (ret) {
1593 dev_err(&pdev->dev, "failed to reparent fck\n");
1594 goto err2;
1595 }
1596
1597err2:
1598 clk_put(parent_clk);
1599err1:
1600 clk_put(gfclk);
1601 return ret;
1602}
1603
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001604static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301605 struct platform_device *pdev)
1606{
1607 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001608 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301609 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301610 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001611 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301612
1613 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301614 u32 val;
1615 int i, ret = 0;
1616
1617 if (pdev->dev.platform_data) {
1618 pdata = pdev->dev.platform_data;
1619 return pdata;
1620 } else if (match) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001621 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1622 GFP_KERNEL);
1623 if (!pdata) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001624 ret = -ENOMEM;
1625 return pdata;
1626 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301627 } else {
1628 /* control shouldn't reach here. something is wrong */
1629 ret = -EINVAL;
1630 goto nodata;
1631 }
1632
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301633 ret = of_property_read_u32(np, "op-mode", &val);
1634 if (ret >= 0)
1635 pdata->op_mode = val;
1636
1637 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001638 if (ret >= 0) {
1639 if (val < 2 || val > 32) {
1640 dev_err(&pdev->dev,
1641 "tdm-slots must be in rage [2-32]\n");
1642 ret = -EINVAL;
1643 goto nodata;
1644 }
1645
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301646 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001647 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301648
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301649 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1650 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301651 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001652 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1653 (sizeof(*of_serial_dir) * val),
1654 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301655 if (!of_serial_dir) {
1656 ret = -ENOMEM;
1657 goto nodata;
1658 }
1659
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001660 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301661 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1662
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001663 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301664 pdata->serial_dir = of_serial_dir;
1665 }
1666
Jyri Sarha4023fe62013-10-18 18:37:43 +03001667 ret = of_property_match_string(np, "dma-names", "tx");
1668 if (ret < 0)
1669 goto nodata;
1670
1671 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1672 &dma_spec);
1673 if (ret < 0)
1674 goto nodata;
1675
1676 pdata->tx_dma_channel = dma_spec.args[0];
1677
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001678 /* RX is not valid in DIT mode */
1679 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1680 ret = of_property_match_string(np, "dma-names", "rx");
1681 if (ret < 0)
1682 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001683
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001684 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1685 &dma_spec);
1686 if (ret < 0)
1687 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001688
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001689 pdata->rx_dma_channel = dma_spec.args[0];
1690 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001691
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301692 ret = of_property_read_u32(np, "tx-num-evt", &val);
1693 if (ret >= 0)
1694 pdata->txnumevt = val;
1695
1696 ret = of_property_read_u32(np, "rx-num-evt", &val);
1697 if (ret >= 0)
1698 pdata->rxnumevt = val;
1699
1700 ret = of_property_read_u32(np, "sram-size-playback", &val);
1701 if (ret >= 0)
1702 pdata->sram_size_playback = val;
1703
1704 ret = of_property_read_u32(np, "sram-size-capture", &val);
1705 if (ret >= 0)
1706 pdata->sram_size_capture = val;
1707
1708 return pdata;
1709
1710nodata:
1711 if (ret < 0) {
1712 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1713 ret);
1714 pdata = NULL;
1715 }
1716 return pdata;
1717}
1718
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001719enum {
1720 PCM_EDMA,
1721 PCM_SDMA,
1722};
1723static const char *sdma_prefix = "ti,omap";
1724
1725static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1726{
1727 struct dma_chan *chan;
1728 const char *tmp;
1729 int ret = PCM_EDMA;
1730
1731 if (!mcasp->dev->of_node)
1732 return PCM_EDMA;
1733
1734 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
1735 chan = dma_request_slave_channel_reason(mcasp->dev, tmp);
1736 if (IS_ERR(chan)) {
1737 if (PTR_ERR(chan) != -EPROBE_DEFER)
1738 dev_err(mcasp->dev,
1739 "Can't verify DMA configuration (%ld)\n",
1740 PTR_ERR(chan));
1741 return PTR_ERR(chan);
1742 }
Takashi Iwaibefff4f2017-09-07 10:59:17 +02001743 if (WARN_ON(!chan->device || !chan->device->dev))
1744 return -EINVAL;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001745
1746 if (chan->device->dev->of_node)
1747 ret = of_property_read_string(chan->device->dev->of_node,
1748 "compatible", &tmp);
1749 else
1750 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1751
1752 dma_release_channel(chan);
1753 if (ret)
1754 return ret;
1755
1756 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1757 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1758 return PCM_SDMA;
1759
1760 return PCM_EDMA;
1761}
1762
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001763static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1764{
1765 int i;
1766 u32 offset = 0;
1767
1768 if (pdata->version != MCASP_VERSION_4)
1769 return pdata->tx_dma_offset;
1770
1771 for (i = 0; i < pdata->num_serializer; i++) {
1772 if (pdata->serial_dir[i] == TX_MODE) {
1773 if (!offset) {
1774 offset = DAVINCI_MCASP_TXBUF_REG(i);
1775 } else {
1776 pr_err("%s: Only one serializer allowed!\n",
1777 __func__);
1778 break;
1779 }
1780 }
1781 }
1782
1783 return offset;
1784}
1785
1786static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1787{
1788 int i;
1789 u32 offset = 0;
1790
1791 if (pdata->version != MCASP_VERSION_4)
1792 return pdata->rx_dma_offset;
1793
1794 for (i = 0; i < pdata->num_serializer; i++) {
1795 if (pdata->serial_dir[i] == RX_MODE) {
1796 if (!offset) {
1797 offset = DAVINCI_MCASP_RXBUF_REG(i);
1798 } else {
1799 pr_err("%s: Only one serializer allowed!\n",
1800 __func__);
1801 break;
1802 }
1803 }
1804 }
1805
1806 return offset;
1807}
1808
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001809static int davinci_mcasp_probe(struct platform_device *pdev)
1810{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001811 struct snd_dmaengine_dai_dma_data *dma_data;
Axel Lin508a43f2015-08-24 16:47:36 +08001812 struct resource *mem, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001813 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001814 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001815 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001816 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001817 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01001818 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001819
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301820 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
1821 dev_err(&pdev->dev, "No platform data supplied\n");
1822 return -EINVAL;
1823 }
1824
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001825 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01001826 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001827 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001828 return -ENOMEM;
1829
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301830 pdata = davinci_mcasp_set_pdata_from_of(pdev);
1831 if (!pdata) {
1832 dev_err(&pdev->dev, "no platform data\n");
1833 return -EINVAL;
1834 }
1835
Jyri Sarha256ba182013-10-18 18:37:42 +03001836 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001837 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001838 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03001839 "\"mpu\" mem resource not found, using index 0\n");
1840 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1841 if (!mem) {
1842 dev_err(&pdev->dev, "no mem resource?\n");
1843 return -ENODEV;
1844 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001845 }
1846
Axel Lin508a43f2015-08-24 16:47:36 +08001847 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
1848 if (IS_ERR(mcasp->base))
1849 return PTR_ERR(mcasp->base);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001850
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301851 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001852
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001853 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02001854 /* sanity check for tdm slots parameter */
1855 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
1856 if (pdata->tdm_slots < 2) {
1857 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1858 pdata->tdm_slots);
1859 mcasp->tdm_slots = 2;
1860 } else if (pdata->tdm_slots > 32) {
1861 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
1862 pdata->tdm_slots);
1863 mcasp->tdm_slots = 32;
1864 } else {
1865 mcasp->tdm_slots = pdata->tdm_slots;
1866 }
1867 }
1868
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001869 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001870#ifdef CONFIG_PM_SLEEP
1871 mcasp->context.xrsr_regs = devm_kzalloc(&pdev->dev,
1872 sizeof(u32) * mcasp->num_serializer,
1873 GFP_KERNEL);
Christophe Jaillet4243e042017-08-27 08:46:50 +02001874 if (!mcasp->context.xrsr_regs) {
1875 ret = -ENOMEM;
1876 goto err;
1877 }
Peter Ujfalusif114ce62014-10-01 16:02:12 +03001878#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001879 mcasp->serial_dir = pdata->serial_dir;
1880 mcasp->version = pdata->version;
1881 mcasp->txnumevt = pdata->txnumevt;
1882 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02001883
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001884 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001885
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001886 irq = platform_get_irq_byname(pdev, "common");
1887 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001888 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001889 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301890 if (!irq_name) {
1891 ret = -ENOMEM;
1892 goto err;
1893 }
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001894 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1895 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02001896 IRQF_ONESHOT | IRQF_SHARED,
1897 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02001898 if (ret) {
1899 dev_err(&pdev->dev, "common IRQ request failed\n");
1900 goto err;
1901 }
1902
1903 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1904 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1905 }
1906
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001907 irq = platform_get_irq_byname(pdev, "rx");
1908 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001909 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001910 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301911 if (!irq_name) {
1912 ret = -ENOMEM;
1913 goto err;
1914 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001915 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1916 davinci_mcasp_rx_irq_handler,
1917 IRQF_ONESHOT, irq_name, mcasp);
1918 if (ret) {
1919 dev_err(&pdev->dev, "RX IRQ request failed\n");
1920 goto err;
1921 }
1922
1923 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
1924 }
1925
1926 irq = platform_get_irq_byname(pdev, "tx");
1927 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03001928 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001929 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05301930 if (!irq_name) {
1931 ret = -ENOMEM;
1932 goto err;
1933 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001934 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1935 davinci_mcasp_tx_irq_handler,
1936 IRQF_ONESHOT, irq_name, mcasp);
1937 if (ret) {
1938 dev_err(&pdev->dev, "TX IRQ request failed\n");
1939 goto err;
1940 }
1941
1942 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
1943 }
1944
Jyri Sarha256ba182013-10-18 18:37:42 +03001945 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001946 if (dat)
1947 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03001948
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001949 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001950 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001951 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001952 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001953 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001954
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001955 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001956 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001957 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001958 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001959 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001960 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07001961
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001962 /* dmaengine filter data for DT and non-DT boot */
1963 if (pdev->dev.of_node)
1964 dma_data->filter_data = "tx";
1965 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001966 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02001967
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001968 /* RX is not valid in DIT mode */
1969 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001970 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001971 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001972 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001973 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001974 dma_data->addr =
1975 mem->start + davinci_mcasp_rxdma_offset(pdata);
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001976
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001977 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001978 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1979 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001980 *dma = res->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001981 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001982 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001983
1984 /* dmaengine filter data for DT and non-DT boot */
1985 if (pdev->dev.of_node)
1986 dma_data->filter_data = "rx";
1987 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001988 dma_data->filter_data = dma;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001989 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001990
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001991 if (mcasp->version < MCASP_VERSION_3) {
1992 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02001993 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02001994 mcasp->dat_port = true;
1995 } else {
1996 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
1997 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001998
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001999 /* Allocate memory for long enough list for all possible
2000 * scenarios. Maximum number tdm slots is 32 and there cannot
2001 * be more serializers than given in the configuration. The
2002 * serializer directions could be taken into account, but it
2003 * would make code much more complex and save only couple of
2004 * bytes.
2005 */
2006 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
2007 devm_kzalloc(mcasp->dev, sizeof(unsigned int) *
Peter Ujfalusie4798d22017-05-11 09:58:22 +03002008 (32 + mcasp->num_serializer - 1),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002009 GFP_KERNEL);
2010
2011 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
2012 devm_kzalloc(mcasp->dev, sizeof(unsigned int) *
Peter Ujfalusie4798d22017-05-11 09:58:22 +03002013 (32 + mcasp->num_serializer - 1),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002014 GFP_KERNEL);
2015
2016 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
Christophe Jaillet1b8b68b2017-09-16 07:40:29 +02002017 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2018 ret = -ENOMEM;
2019 goto err;
2020 }
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002021
2022 ret = davinci_mcasp_set_ch_constraints(mcasp);
Jyri Sarha5935a052015-04-23 16:16:05 +03002023 if (ret)
2024 goto err;
2025
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002026 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02002027
2028 mcasp_reparent_fck(pdev);
2029
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002030 ret = devm_snd_soc_register_component(&pdev->dev,
2031 &davinci_mcasp_component,
2032 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002033
2034 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002035 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302036
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002037 ret = davinci_mcasp_get_dma_type(mcasp);
2038 switch (ret) {
2039 case PCM_EDMA:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002040#if IS_BUILTIN(CONFIG_SND_EDMA_SOC) || \
2041 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
2042 IS_MODULE(CONFIG_SND_EDMA_SOC))
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002043 ret = edma_pcm_platform_register(&pdev->dev);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002044#else
2045 dev_err(&pdev->dev, "Missing SND_EDMA_SOC\n");
2046 ret = -EINVAL;
2047 goto err;
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002048#endif
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002049 break;
2050 case PCM_SDMA:
Peter Ujfalusi077a4032018-05-09 14:03:55 +03002051#if IS_BUILTIN(CONFIG_SND_SDMA_SOC) || \
Jyri Sarha7f28f352014-06-13 12:49:59 +03002052 (IS_MODULE(CONFIG_SND_DAVINCI_SOC_MCASP) && \
Peter Ujfalusi077a4032018-05-09 14:03:55 +03002053 IS_MODULE(CONFIG_SND_SDMA_SOC))
2054 ret = sdma_pcm_platform_register(&pdev->dev, NULL, NULL);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002055#else
2056 dev_err(&pdev->dev, "Missing SND_SDMA_SOC\n");
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002057 ret = -EINVAL;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002058 goto err;
2059#endif
2060 break;
2061 default:
2062 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2063 case -EPROBE_DEFER:
2064 goto err;
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002065 break;
2066 }
2067
2068 if (ret) {
2069 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002070 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302071 }
2072
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002073 return 0;
2074
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002075err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302076 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002077 return ret;
2078}
2079
2080static int davinci_mcasp_remove(struct platform_device *pdev)
2081{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302082 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002083
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002084 return 0;
2085}
2086
2087static struct platform_driver davinci_mcasp_driver = {
2088 .probe = davinci_mcasp_probe,
2089 .remove = davinci_mcasp_remove,
2090 .driver = {
2091 .name = "davinci-mcasp",
Sachin Kamatea421eb2013-05-22 16:53:37 +05302092 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002093 },
2094};
2095
Axel Linf9b8a512011-11-25 10:09:27 +08002096module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002097
2098MODULE_AUTHOR("Steve Chen");
2099MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2100MODULE_LICENSE("GPL");