blob: 8bec0dc4f75489018bc51877c311c5f5b31862c6 [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>
Peter Ujfalusica3d9432018-11-16 15:41:39 +020031#include <linux/bitmap.h>
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +020032#include <linux/gpio/driver.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040033
Daniel Mack64792852014-03-27 11:27:40 +010034#include <sound/asoundef.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/initval.h>
39#include <sound/soc.h>
Peter Ujfalusi453c4992013-11-14 11:35:34 +020040#include <sound/dmaengine_pcm.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040041
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +030042#include "edma-pcm.h"
Peter Ujfalusif2055e12018-12-17 14:21:34 +020043#include "sdma-pcm.h"
Chaithrika U Sb67f4482009-06-05 06:28:40 -040044#include "davinci-mcasp.h"
45
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +030046#define MCASP_MAX_AFIFO_DEPTH 64
47
Arnd Bergmann8ca51042019-03-07 11:11:30 +010048#ifdef CONFIG_PM
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030049static u32 context_regs[] = {
50 DAVINCI_MCASP_TXFMCTL_REG,
51 DAVINCI_MCASP_RXFMCTL_REG,
52 DAVINCI_MCASP_TXFMT_REG,
53 DAVINCI_MCASP_RXFMT_REG,
54 DAVINCI_MCASP_ACLKXCTL_REG,
55 DAVINCI_MCASP_ACLKRCTL_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030056 DAVINCI_MCASP_AHCLKXCTL_REG,
57 DAVINCI_MCASP_AHCLKRCTL_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030058 DAVINCI_MCASP_PDIR_REG,
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +020059 DAVINCI_MCASP_PFUNC_REG,
Peter Ujfalusif114ce62014-10-01 16:02:12 +030060 DAVINCI_MCASP_RXMASK_REG,
61 DAVINCI_MCASP_TXMASK_REG,
62 DAVINCI_MCASP_RXTDM_REG,
63 DAVINCI_MCASP_TXTDM_REG,
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030064};
65
Peter Ujfalusi790bb942014-02-03 14:51:52 +020066struct davinci_mcasp_context {
Peter Ujfalusi1cc0c052014-10-01 16:02:11 +030067 u32 config_regs[ARRAY_SIZE(context_regs)];
Peter Ujfalusif114ce62014-10-01 16:02:12 +030068 u32 afifo_regs[2]; /* for read/write fifo control registers */
69 u32 *xrsr_regs; /* for serializer configuration */
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +020070 bool pm_state;
Peter Ujfalusi790bb942014-02-03 14:51:52 +020071};
Arnd Bergmann8ca51042019-03-07 11:11:30 +010072#endif
Peter Ujfalusi790bb942014-02-03 14:51:52 +020073
Jyri Sarhaa75a0532015-03-20 13:31:08 +020074struct davinci_mcasp_ruledata {
75 struct davinci_mcasp *mcasp;
76 int serializers;
77};
78
Peter Ujfalusi70091a32013-11-14 11:35:29 +020079struct davinci_mcasp {
Peter Ujfalusi453c4992013-11-14 11:35:34 +020080 struct snd_dmaengine_dai_dma_data dma_data[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +020081 void __iomem *base;
Peter Ujfalusi487dce82013-11-14 11:35:31 +020082 u32 fifo_base;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020083 struct device *dev;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020084 struct snd_pcm_substream *substreams[2];
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +020085 unsigned int dai_fmt;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020086
87 /* McASP specific data */
88 int tdm_slots;
Jyri Sarhadd55ff82015-09-09 21:27:44 +030089 u32 tdm_mask[2];
90 int slot_width;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020091 u8 op_mode;
Peter Ujfalusibc184542018-11-16 15:41:41 +020092 u8 dismod;
Peter Ujfalusi21400a72013-11-14 11:35:26 +020093 u8 num_serializer;
94 u8 *serial_dir;
95 u8 version;
Daniel Mack82675252014-07-16 14:04:41 +020096 u8 bclk_div;
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +020097 int streams;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +020098 u32 irq_request[2];
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +020099 int dma_request[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200100
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200101 int sysclk_freq;
102 bool bclk_master;
Peter Ujfalusi764958f2019-06-11 15:29:41 +0300103 u32 auxclk_fs_ratio;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200104
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200105 unsigned long pdir; /* Pin direction bitfield */
106
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200107 /* McASP FIFO related */
108 u8 txnumevt;
109 u8 rxnumevt;
110
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200111 bool dat_port;
112
Peter Ujfalusi11277832014-11-10 12:32:16 +0200113 /* Used for comstraint setting on the second stream */
114 u32 channels;
115
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +0200116#ifdef CONFIG_GPIOLIB
117 struct gpio_chip gpio_chip;
118#endif
119
Peter Ujfalusi61754712019-01-03 16:05:50 +0200120#ifdef CONFIG_PM
Peter Ujfalusi790bb942014-02-03 14:51:52 +0200121 struct davinci_mcasp_context context;
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200122#endif
Jyri Sarhaa75a0532015-03-20 13:31:08 +0200123
124 struct davinci_mcasp_ruledata ruledata[2];
Jyri Sarha5935a052015-04-23 16:16:05 +0300125 struct snd_pcm_hw_constraint_list chconstr[2];
Peter Ujfalusi21400a72013-11-14 11:35:26 +0200126};
127
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200128static inline void mcasp_set_bits(struct davinci_mcasp *mcasp, u32 offset,
129 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400130{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200131 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400132 __raw_writel(__raw_readl(reg) | val, reg);
133}
134
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200135static inline void mcasp_clr_bits(struct davinci_mcasp *mcasp, u32 offset,
136 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400137{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200138 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400139 __raw_writel((__raw_readl(reg) & ~(val)), reg);
140}
141
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200142static inline void mcasp_mod_bits(struct davinci_mcasp *mcasp, u32 offset,
143 u32 val, u32 mask)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400144{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200145 void __iomem *reg = mcasp->base + offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400146 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
147}
148
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200149static inline void mcasp_set_reg(struct davinci_mcasp *mcasp, u32 offset,
150 u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400151{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200152 __raw_writel(val, mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400153}
154
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200155static inline u32 mcasp_get_reg(struct davinci_mcasp *mcasp, u32 offset)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400156{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200157 return (u32)__raw_readl(mcasp->base + offset);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400158}
159
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200160static void mcasp_set_ctl_reg(struct davinci_mcasp *mcasp, u32 ctl_reg, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400161{
162 int i = 0;
163
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200164 mcasp_set_bits(mcasp, ctl_reg, val);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400165
166 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
167 /* loop count is to avoid the lock-up */
168 for (i = 0; i < 1000; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200169 if ((mcasp_get_reg(mcasp, ctl_reg) & val) == val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400170 break;
171 }
172
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200173 if (i == 1000 && ((mcasp_get_reg(mcasp, ctl_reg) & val) != val))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400174 printk(KERN_ERR "GBLCTL write error\n");
175}
176
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200177static bool mcasp_is_synchronous(struct davinci_mcasp *mcasp)
178{
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200179 u32 rxfmctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXFMCTL_REG);
180 u32 aclkxctl = mcasp_get_reg(mcasp, DAVINCI_MCASP_ACLKXCTL_REG);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200181
182 return !(aclkxctl & TX_ASYNC) && rxfmctl & AFSRE;
183}
184
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200185static inline void mcasp_set_clk_pdir(struct davinci_mcasp *mcasp, bool enable)
186{
187 u32 bit = PIN_BIT_AMUTE;
188
189 for_each_set_bit_from(bit, &mcasp->pdir, PIN_BIT_AFSR + 1) {
190 if (enable)
191 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
192 else
193 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
194 }
195}
196
197static inline void mcasp_set_axr_pdir(struct davinci_mcasp *mcasp, bool enable)
198{
199 u32 bit;
200
201 for_each_set_bit(bit, &mcasp->pdir, PIN_BIT_AFSR) {
202 if (enable)
203 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
204 else
205 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(bit));
206 }
207}
208
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200209static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400210{
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200211 if (mcasp->rxnumevt) { /* enable FIFO */
212 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
213
214 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
215 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
216 }
217
Peter Ujfalusi44982732014-10-29 13:55:45 +0200218 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200219 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
220 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200221 /*
222 * When ASYNC == 0 the transmit and receive sections operate
223 * synchronously from the transmit clock and frame sync. We need to make
224 * sure that the TX signlas are enabled when starting reception.
225 */
226 if (mcasp_is_synchronous(mcasp)) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200227 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
228 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200229 }
230
Peter Ujfalusi44982732014-10-29 13:55:45 +0200231 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200232 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200233 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200234 /* Release RX state machine */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200235 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
Peter Ujfalusi44982732014-10-29 13:55:45 +0200236 /* Release Frame Sync generator */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200237 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200238 if (mcasp_is_synchronous(mcasp))
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200239 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200240
241 /* enable receive IRQs */
242 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
243 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400244}
245
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200246static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400247{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400248 u32 cnt;
249
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200250 if (mcasp->txnumevt) { /* enable FIFO */
251 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
252
253 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
254 mcasp_set_bits(mcasp, reg, FIFO_ENABLE);
255 }
256
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200257 /* Start clocks */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200258 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
259 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200260 mcasp_set_clk_pdir(mcasp, true);
261
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200262 /* Activate serializer(s) */
Peter Ujfalusi1003c272018-11-16 15:41:38 +0200263 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200264 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400265
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200266 /* wait for XDATA to be cleared */
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400267 cnt = 0;
Peter Ujfalusie2a0c9f2015-12-11 13:06:24 +0200268 while ((mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG) & XRDATA) &&
269 (cnt < 100000))
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400270 cnt++;
271
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200272 mcasp_set_axr_pdir(mcasp, true);
273
Peter Ujfalusi36bcecd2014-10-29 13:55:44 +0200274 /* Release TX state machine */
275 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
276 /* Release Frame Sync generator */
277 mcasp_set_ctl_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200278
279 /* enable transmit IRQs */
280 mcasp_set_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
281 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400282}
283
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200284static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400285{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200286 mcasp->streams++;
287
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200288 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200289 mcasp_start_tx(mcasp);
Peter Ujfalusibb372af2014-10-29 13:55:47 +0200290 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200291 mcasp_start_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400292}
293
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200294static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400295{
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200296 /* disable IRQ sources */
297 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLR_REG,
298 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE]);
299
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200300 /*
301 * In synchronous mode stop the TX clocks if no other stream is
302 * running
303 */
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200304 if (mcasp_is_synchronous(mcasp) && !mcasp->streams) {
305 mcasp_set_clk_pdir(mcasp, false);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200306 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, 0);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200307 }
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200308
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200309 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLR_REG, 0);
310 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200311
312 if (mcasp->rxnumevt) { /* disable FIFO */
313 u32 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
314
315 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
316 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400317}
318
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200319static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400320{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200321 u32 val = 0;
322
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200323 /* disable IRQ sources */
324 mcasp_clr_bits(mcasp, DAVINCI_MCASP_EVTCTLX_REG,
325 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK]);
326
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200327 /*
328 * In synchronous mode keep TX clocks running if the capture stream is
329 * still running.
330 */
331 if (mcasp_is_synchronous(mcasp) && mcasp->streams)
332 val = TXHCLKRST | TXCLKRST | TXFSRST;
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200333 else
334 mcasp_set_clk_pdir(mcasp, false);
335
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200336
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200337 mcasp_set_reg(mcasp, DAVINCI_MCASP_GBLCTLX_REG, val);
338 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200339
340 if (mcasp->txnumevt) { /* disable FIFO */
341 u32 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
342
343 mcasp_clr_bits(mcasp, reg, FIFO_ENABLE);
344 }
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200345
346 mcasp_set_axr_pdir(mcasp, false);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400347}
348
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200349static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400350{
Peter Ujfalusi4dcb5a02013-11-14 11:35:33 +0200351 mcasp->streams--;
352
Peter Ujfalusi03808662014-10-29 13:55:46 +0200353 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200354 mcasp_stop_tx(mcasp);
Peter Ujfalusi03808662014-10-29 13:55:46 +0200355 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200356 mcasp_stop_rx(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400357}
358
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200359static irqreturn_t davinci_mcasp_tx_irq_handler(int irq, void *data)
360{
361 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
362 struct snd_pcm_substream *substream;
363 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK];
364 u32 handled_mask = 0;
365 u32 stat;
366
367 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG);
368 if (stat & XUNDRN & irq_mask) {
369 dev_warn(mcasp->dev, "Transmit buffer underflow\n");
370 handled_mask |= XUNDRN;
371
372 substream = mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200373 if (substream)
374 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200375 }
376
377 if (!handled_mask)
378 dev_warn(mcasp->dev, "unhandled tx event. txstat: 0x%08x\n",
379 stat);
380
381 if (stat & XRERR)
382 handled_mask |= XRERR;
383
384 /* Ack the handled event only */
385 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, handled_mask);
386
387 return IRQ_RETVAL(handled_mask);
388}
389
390static irqreturn_t davinci_mcasp_rx_irq_handler(int irq, void *data)
391{
392 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
393 struct snd_pcm_substream *substream;
394 u32 irq_mask = mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE];
395 u32 handled_mask = 0;
396 u32 stat;
397
398 stat = mcasp_get_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG);
399 if (stat & ROVRN & irq_mask) {
400 dev_warn(mcasp->dev, "Receive buffer overflow\n");
401 handled_mask |= ROVRN;
402
403 substream = mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwaidae35d12018-07-04 16:01:43 +0200404 if (substream)
405 snd_pcm_stop_xrun(substream);
Misael Lopez Cruza7a33242014-11-12 16:38:05 +0200406 }
407
408 if (!handled_mask)
409 dev_warn(mcasp->dev, "unhandled rx event. rxstat: 0x%08x\n",
410 stat);
411
412 if (stat & XRERR)
413 handled_mask |= XRERR;
414
415 /* Ack the handled event only */
416 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, handled_mask);
417
418 return IRQ_RETVAL(handled_mask);
419}
420
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +0200421static irqreturn_t davinci_mcasp_common_irq_handler(int irq, void *data)
422{
423 struct davinci_mcasp *mcasp = (struct davinci_mcasp *)data;
424 irqreturn_t ret = IRQ_NONE;
425
426 if (mcasp->substreams[SNDRV_PCM_STREAM_PLAYBACK])
427 ret = davinci_mcasp_tx_irq_handler(irq, data);
428
429 if (mcasp->substreams[SNDRV_PCM_STREAM_CAPTURE])
430 ret |= davinci_mcasp_rx_irq_handler(irq, data);
431
432 return ret;
433}
434
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400435static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
436 unsigned int fmt)
437{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200438 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200439 int ret = 0;
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300440 u32 data_delay;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300441 bool fs_pol_rising;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300442 bool inv_fs = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400443
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200444 if (!fmt)
445 return 0;
446
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200447 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5296cf22012-10-04 15:08:42 +0200448 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300449 case SND_SOC_DAIFMT_DSP_A:
450 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
451 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi188edc52014-04-04 14:31:43 +0300452 /* 1st data bit occur one ACLK cycle after the frame sync */
453 data_delay = 1;
454 break;
Daniel Mack5296cf22012-10-04 15:08:42 +0200455 case SND_SOC_DAIFMT_DSP_B:
456 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200457 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
458 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300459 /* No delay after FS */
460 data_delay = 0;
Daniel Mack5296cf22012-10-04 15:08:42 +0200461 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300462 case SND_SOC_DAIFMT_I2S:
Daniel Mack5296cf22012-10-04 15:08:42 +0200463 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200464 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
465 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300466 /* 1st data bit occur one ACLK cycle after the frame sync */
467 data_delay = 1;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300468 /* FS need to be inverted */
469 inv_fs = true;
Daniel Mack5296cf22012-10-04 15:08:42 +0200470 break;
Peter Ujfalusi423761e2014-04-04 14:31:46 +0300471 case SND_SOC_DAIFMT_LEFT_J:
472 /* configure a full-word SYNC pulse (LRCLK) */
473 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
474 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
475 /* No delay after FS */
476 data_delay = 0;
477 break;
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300478 default:
479 ret = -EINVAL;
480 goto out;
Daniel Mack5296cf22012-10-04 15:08:42 +0200481 }
482
Peter Ujfalusi6dfa9a42014-04-04 14:31:42 +0300483 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, FSXDLY(data_delay),
484 FSXDLY(3));
485 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, FSRDLY(data_delay),
486 FSRDLY(3));
487
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400488 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
489 case SND_SOC_DAIFMT_CBS_CFS:
490 /* codec is clock and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200491 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
492 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400493
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200494 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
495 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400496
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200497 /* BCLK */
498 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
499 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
500 /* Frame Sync */
501 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
502 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
503
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200504 mcasp->bclk_master = 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400505 break;
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200506 case SND_SOC_DAIFMT_CBS_CFM:
507 /* codec is clock slave and frame master */
508 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
509 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
510
511 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
512 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
513
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200514 /* BCLK */
515 set_bit(PIN_BIT_ACLKX, &mcasp->pdir);
516 set_bit(PIN_BIT_ACLKR, &mcasp->pdir);
517 /* Frame Sync */
518 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
519 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
520
Peter Ujfalusi226e2f12015-02-12 16:41:26 +0200521 mcasp->bclk_master = 1;
522 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400523 case SND_SOC_DAIFMT_CBM_CFS:
524 /* codec is clock master and frame slave */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200525 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
526 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400527
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200528 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
529 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400530
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200531 /* BCLK */
532 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
533 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
534 /* Frame Sync */
535 set_bit(PIN_BIT_AFSX, &mcasp->pdir);
536 set_bit(PIN_BIT_AFSR, &mcasp->pdir);
537
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200538 mcasp->bclk_master = 0;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400539 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400540 case SND_SOC_DAIFMT_CBM_CFM:
541 /* codec is clock and frame master */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200542 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
543 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400544
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200545 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
546 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400547
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200548 /* BCLK */
549 clear_bit(PIN_BIT_ACLKX, &mcasp->pdir);
550 clear_bit(PIN_BIT_ACLKR, &mcasp->pdir);
551 /* Frame Sync */
552 clear_bit(PIN_BIT_AFSX, &mcasp->pdir);
553 clear_bit(PIN_BIT_AFSR, &mcasp->pdir);
554
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200555 mcasp->bclk_master = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400556 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400557 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200558 ret = -EINVAL;
559 goto out;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400560 }
561
562 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
563 case SND_SOC_DAIFMT_IB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200564 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300565 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300566 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400567 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400568 case SND_SOC_DAIFMT_NB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200569 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300570 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300571 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400572 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400573 case SND_SOC_DAIFMT_IB_IF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200574 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusi74ddd8c2014-04-04 14:31:41 +0300575 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300576 fs_pol_rising = false;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400577 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400578 case SND_SOC_DAIFMT_NB_NF:
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200579 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200580 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300581 fs_pol_rising = true;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400582 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400583 default:
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200584 ret = -EINVAL;
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300585 goto out;
586 }
587
Peter Ujfalusiffd950f2014-04-04 14:31:45 +0300588 if (inv_fs)
589 fs_pol_rising = !fs_pol_rising;
590
Peter Ujfalusi83f12502014-04-04 14:31:44 +0300591 if (fs_pol_rising) {
592 mcasp_clr_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
593 mcasp_clr_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
594 } else {
595 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
596 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400597 }
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +0200598
599 mcasp->dai_fmt = fmt;
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200600out:
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200601 pm_runtime_put(mcasp->dev);
Peter Ujfalusi1d17a042014-01-30 15:21:30 +0200602 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400603}
604
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300605static int __davinci_mcasp_set_clkdiv(struct davinci_mcasp *mcasp, int div_id,
Jyri Sarha88135432014-08-06 16:47:16 +0300606 int div, bool explicit)
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200607{
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200608 pm_runtime_get_sync(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200609 switch (div_id) {
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300610 case MCASP_CLKDIV_AUXCLK: /* MCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200611 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200612 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200613 mcasp_mod_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200614 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
615 break;
616
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300617 case MCASP_CLKDIV_BCLK: /* BCLK divider */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200618 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200619 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200620 mcasp_mod_bits(mcasp, DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200621 ACLKRDIV(div - 1), ACLKRDIV_MASK);
Jyri Sarha88135432014-08-06 16:47:16 +0300622 if (explicit)
623 mcasp->bclk_div = div;
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200624 break;
625
Peter Ujfalusi20d4b102016-05-09 13:42:29 +0300626 case MCASP_CLKDIV_BCLK_FS_RATIO:
627 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300628 * BCLK/LRCLK ratio descries how many bit-clock cycles
629 * fit into one frame. The clock ratio is given for a
630 * full period of data (for I2S format both left and
631 * right channels), so it has to be divided by number
632 * of tdm-slots (for I2S - divided by 2).
633 * Instead of storing this ratio, we calculate a new
634 * tdm_slot width by dividing the the ratio by the
635 * number of configured tdm slots.
636 */
637 mcasp->slot_width = div / mcasp->tdm_slots;
638 if (div % mcasp->tdm_slots)
639 dev_warn(mcasp->dev,
640 "%s(): BCLK/LRCLK %d is not divisible by %d tdm slots",
641 __func__, div, mcasp->tdm_slots);
Daniel Mack1b3bc062012-12-05 18:20:38 +0100642 break;
643
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200644 default:
645 return -EINVAL;
646 }
647
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200648 pm_runtime_put(mcasp->dev);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200649 return 0;
650}
651
Jyri Sarha88135432014-08-06 16:47:16 +0300652static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id,
653 int div)
654{
Peter Ujfalusi226e73e2016-05-09 13:42:30 +0300655 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
656
657 return __davinci_mcasp_set_clkdiv(mcasp, div_id, div, 1);
Jyri Sarha88135432014-08-06 16:47:16 +0300658}
659
Daniel Mack5b66aa22012-10-04 15:08:41 +0200660static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
661 unsigned int freq, int dir)
662{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200663 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200664
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200665 pm_runtime_get_sync(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200666 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200667 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
668 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200669 set_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200670 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200671 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
672 mcasp_clr_bits(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200673 clear_bit(PIN_BIT_AHCLKX, &mcasp->pdir);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200674 }
675
Jyri Sarhaab8b14b2014-01-27 17:37:52 +0200676 mcasp->sysclk_freq = freq;
677
Peter Ujfalusi6afda7f2015-03-05 16:55:21 +0200678 pm_runtime_put(mcasp->dev);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200679 return 0;
680}
681
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300682/* All serializers must have equal number of channels */
683static int davinci_mcasp_ch_constraint(struct davinci_mcasp *mcasp, int stream,
684 int serializers)
685{
686 struct snd_pcm_hw_constraint_list *cl = &mcasp->chconstr[stream];
687 unsigned int *list = (unsigned int *) cl->list;
688 int slots = mcasp->tdm_slots;
689 int i, count = 0;
690
691 if (mcasp->tdm_mask[stream])
692 slots = hweight32(mcasp->tdm_mask[stream]);
693
Peter Ujfalusie4798d22017-05-11 09:58:22 +0300694 for (i = 1; i <= slots; i++)
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300695 list[count++] = i;
696
697 for (i = 2; i <= serializers; i++)
698 list[count++] = i*slots;
699
700 cl->count = count;
701
702 return 0;
703}
704
705static int davinci_mcasp_set_ch_constraints(struct davinci_mcasp *mcasp)
706{
707 int rx_serializers = 0, tx_serializers = 0, ret, i;
708
709 for (i = 0; i < mcasp->num_serializer; i++)
710 if (mcasp->serial_dir[i] == TX_MODE)
711 tx_serializers++;
712 else if (mcasp->serial_dir[i] == RX_MODE)
713 rx_serializers++;
714
715 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_PLAYBACK,
716 tx_serializers);
717 if (ret)
718 return ret;
719
720 ret = davinci_mcasp_ch_constraint(mcasp, SNDRV_PCM_STREAM_CAPTURE,
721 rx_serializers);
722
723 return ret;
724}
725
726
727static int davinci_mcasp_set_tdm_slot(struct snd_soc_dai *dai,
728 unsigned int tx_mask,
729 unsigned int rx_mask,
730 int slots, int slot_width)
731{
732 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
733
734 dev_dbg(mcasp->dev,
735 "%s() tx_mask 0x%08x rx_mask 0x%08x slots %d width %d\n",
736 __func__, tx_mask, rx_mask, slots, slot_width);
737
738 if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
739 dev_err(mcasp->dev,
740 "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
741 tx_mask, rx_mask, slots);
742 return -EINVAL;
743 }
744
745 if (slot_width &&
746 (slot_width < 8 || slot_width > 32 || slot_width % 4 != 0)) {
747 dev_err(mcasp->dev, "%s: Unsupported slot_width %d\n",
748 __func__, slot_width);
749 return -EINVAL;
750 }
751
752 mcasp->tdm_slots = slots;
Andreas Dannenberg1bdd5932015-11-09 12:19:19 -0600753 mcasp->tdm_mask[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
754 mcasp->tdm_mask[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300755 mcasp->slot_width = slot_width;
756
757 return davinci_mcasp_set_ch_constraints(mcasp);
758}
759
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200760static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Jyri Sarha14a998b2015-09-17 10:39:05 +0300761 int sample_width)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400762{
Daniel Mackba764b32012-12-05 18:20:37 +0100763 u32 fmt;
Jyri Sarha14a998b2015-09-17 10:39:05 +0300764 u32 tx_rotate = (sample_width / 4) & 0x7;
765 u32 mask = (1ULL << sample_width) - 1;
766 u32 slot_width = sample_width;
767
Peter Ujfalusife0a29e2014-09-04 10:52:53 +0300768 /*
769 * For captured data we should not rotate, inversion and masking is
770 * enoguh to get the data to the right position:
771 * Format data from bus after reverse (XRBUF)
772 * S16_LE: |LSB|MSB|xxx|xxx| |xxx|xxx|MSB|LSB|
773 * S24_3LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
774 * S24_LE: |LSB|DAT|MSB|xxx| |xxx|MSB|DAT|LSB|
775 * S32_LE: |LSB|DAT|DAT|MSB| |MSB|DAT|DAT|LSB|
776 */
777 u32 rx_rotate = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400778
Daniel Mack1b3bc062012-12-05 18:20:38 +0100779 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300780 * Setting the tdm slot width either with set_clkdiv() or
781 * set_tdm_slot() allows us to for example send 32 bits per
782 * channel to the codec, while only 16 of them carry audio
783 * payload.
Daniel Mack1b3bc062012-12-05 18:20:38 +0100784 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300785 if (mcasp->slot_width) {
Peter Ujfalusid742b922014-11-10 12:32:19 +0200786 /*
Jyri Sarha14a998b2015-09-17 10:39:05 +0300787 * When we have more bclk then it is needed for the
788 * data, we need to use the rotation to move the
789 * received samples to have correct alignment.
Peter Ujfalusid742b922014-11-10 12:32:19 +0200790 */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300791 slot_width = mcasp->slot_width;
792 rx_rotate = (slot_width - sample_width) / 4;
Peter Ujfalusid742b922014-11-10 12:32:19 +0200793 }
Daniel Mack1b3bc062012-12-05 18:20:38 +0100794
Daniel Mackba764b32012-12-05 18:20:37 +0100795 /* mapping of the XSSZ bit-field as described in the datasheet */
Jyri Sarha14a998b2015-09-17 10:39:05 +0300796 fmt = (slot_width >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400797
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200798 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200799 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXSSZ(fmt),
800 RXSSZ(0x0F));
801 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXSSZ(fmt),
802 TXSSZ(0x0F));
803 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(tx_rotate),
804 TXROT(7));
805 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, RXROT(rx_rotate),
806 RXROT(7));
807 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXMASK_REG, mask);
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200808 }
809
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200810 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400811
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400812 return 0;
813}
814
Peter Ujfalusi662ffae2014-01-30 15:15:22 +0200815static int mcasp_common_hw_param(struct davinci_mcasp *mcasp, int stream,
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300816 int period_words, int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400817{
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300818 struct snd_dmaengine_dai_dma_data *dma_data = &mcasp->dma_data[stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400819 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400820 u8 tx_ser = 0;
821 u8 rx_ser = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200822 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100823 u8 max_active_serializers = (channels + slots - 1) / slots;
Peter Ujfalusi72383192015-09-14 16:06:48 +0300824 int active_serializers, numevt;
Peter Ujfalusi487dce82013-11-14 11:35:31 +0200825 u32 reg;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400826 /* Default configuration */
Peter Ujfalusi40448e52014-04-04 15:56:30 +0300827 if (mcasp->version < MCASP_VERSION_3)
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200828 mcasp_set_bits(mcasp, DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400829
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400830 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200831 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
832 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400833 } else {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200834 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
835 mcasp_clr_bits(mcasp, DAVINCI_MCASP_REVTCTL_REG, RXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400836 }
837
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200838 for (i = 0; i < mcasp->num_serializer; i++) {
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200839 mcasp_set_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
840 mcasp->serial_dir[i]);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200841 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100842 tx_ser < max_active_serializers) {
Misael Lopez Cruz19db62e2015-06-08 16:03:47 +0300843 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
Peter Ujfalusibc184542018-11-16 15:41:41 +0200844 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200845 set_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400846 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200847 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100848 rx_ser < max_active_serializers) {
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200849 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400850 rx_ser++;
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300851 } else {
852 /* Inactive or unused pin, set it to inactive */
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200853 mcasp_mod_bits(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
854 SRMOD_INACTIVE, SRMOD_MASK);
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300855 /* If unused, set DISMOD for the pin */
856 if (mcasp->serial_dir[i] != INACTIVE_MODE)
857 mcasp_mod_bits(mcasp,
858 DAVINCI_MCASP_XRSRCTL_REG(i),
859 mcasp->dismod, DISMOD_MASK);
Peter Ujfalusica3d9432018-11-16 15:41:39 +0200860 clear_bit(PIN_BIT_AXR(i), &mcasp->pdir);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400861 }
862 }
863
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300864 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
865 active_serializers = tx_ser;
866 numevt = mcasp->txnumevt;
867 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
868 } else {
869 active_serializers = rx_ser;
870 numevt = mcasp->rxnumevt;
871 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
872 }
Daniel Mackecf327c2013-03-08 14:19:38 +0100873
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300874 if (active_serializers < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200875 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300876 "enabled in mcasp (%d)\n", channels,
877 active_serializers * slots);
Daniel Mackecf327c2013-03-08 14:19:38 +0100878 return -EINVAL;
879 }
880
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300881 /* AFIFO is not in use */
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300882 if (!numevt) {
883 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300884 if (active_serializers > 1) {
885 /*
886 * If more than one serializers are in use we have one
887 * DMA request to provide data for all serializers.
888 * For example if three serializers are enabled the DMA
889 * need to transfer three words per DMA request.
890 */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300891 dma_data->maxburst = active_serializers;
892 } else {
Peter Ujfalusi33445642014-04-01 15:55:12 +0300893 dma_data->maxburst = 0;
894 }
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300895 return 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300896 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400897
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300898 if (period_words % active_serializers) {
899 dev_err(mcasp->dev, "Invalid combination of period words and "
900 "active serializers: %d, %d\n", period_words,
901 active_serializers);
902 return -EINVAL;
903 }
904
905 /*
906 * Calculate the optimal AFIFO depth for platform side:
907 * The number of words for numevt need to be in steps of active
908 * serializers.
909 */
Peter Ujfalusi72383192015-09-14 16:06:48 +0300910 numevt = (numevt / active_serializers) * active_serializers;
911
Peter Ujfalusidd093a02014-04-01 15:55:11 +0300912 while (period_words % numevt && numevt > 0)
913 numevt -= active_serializers;
914 if (numevt <= 0)
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300915 numevt = active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400916
Peter Ujfalusi0bf0e8a2014-04-01 15:55:09 +0300917 mcasp_mod_bits(mcasp, reg, active_serializers, NUMDMA_MASK);
918 mcasp_mod_bits(mcasp, reg, NUMEVT(numevt), NUMEVT_MASK);
Michal Bachraty2952b272013-02-28 16:07:08 +0100919
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300920 /* Configure the burst size for platform drivers */
Peter Ujfalusi33445642014-04-01 15:55:12 +0300921 if (numevt == 1)
922 numevt = 0;
Peter Ujfalusi5f04c602014-04-01 15:55:10 +0300923 dma_data->maxburst = numevt;
924
Michal Bachraty2952b272013-02-28 16:07:08 +0100925 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400926}
927
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200928static int mcasp_i2s_hw_param(struct davinci_mcasp *mcasp, int stream,
929 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400930{
931 int i, active_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200932 int total_slots;
933 int active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400934 u32 mask = 0;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200935 u32 busel = 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400936
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200937 total_slots = mcasp->tdm_slots;
938
939 /*
940 * If more than one serializer is needed, then use them with
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300941 * all the specified tdm_slots. Otherwise, one serializer can
942 * cope with the transaction using just as many slots as there
943 * are channels in the stream.
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200944 */
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300945 if (mcasp->tdm_mask[stream]) {
946 active_slots = hweight32(mcasp->tdm_mask[stream]);
947 active_serializers = (channels + active_slots - 1) /
948 active_slots;
949 if (active_serializers == 1) {
950 active_slots = channels;
951 for (i = 0; i < total_slots; i++) {
952 if ((1 << i) & mcasp->tdm_mask[stream]) {
953 mask |= (1 << i);
954 if (--active_slots <= 0)
955 break;
956 }
957 }
958 }
959 } else {
960 active_serializers = (channels + total_slots - 1) / total_slots;
961 if (active_serializers == 1)
962 active_slots = channels;
963 else
964 active_slots = total_slots;
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +0200965
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300966 for (i = 0; i < active_slots; i++)
967 mask |= (1 << i);
968 }
Peter Ujfalusi5dd17a32019-06-20 12:20:01 +0300969
Peter Ujfalusif68205a2013-11-14 11:35:36 +0200970 mcasp_clr_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400971
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +0200972 if (!mcasp->dat_port)
973 busel = TXSEL;
974
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300975 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
976 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, mask);
977 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, busel | TXORD);
978 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
979 FSXMOD(total_slots), FSXMOD(0x1FF));
980 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
981 mcasp_set_reg(mcasp, DAVINCI_MCASP_RXTDM_REG, mask);
982 mcasp_set_bits(mcasp, DAVINCI_MCASP_RXFMT_REG, busel | RXORD);
983 mcasp_mod_bits(mcasp, DAVINCI_MCASP_RXFMCTL_REG,
984 FSRMOD(total_slots), FSRMOD(0x1FF));
Peter Ujfalusi0ad7d3a2015-11-23 12:51:53 +0200985 /*
986 * If McASP is set to be TX/RX synchronous and the playback is
987 * not running already we need to configure the TX slots in
988 * order to have correct FSX on the bus
989 */
990 if (mcasp_is_synchronous(mcasp) && !mcasp->channels)
991 mcasp_mod_bits(mcasp, DAVINCI_MCASP_TXFMCTL_REG,
992 FSXMOD(total_slots), FSXMOD(0x1FF));
Jyri Sarhadd55ff82015-09-09 21:27:44 +0300993 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400994
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +0200995 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400996}
997
998/* S/PDIF */
Daniel Mack64792852014-03-27 11:27:40 +0100999static int mcasp_dit_hw_param(struct davinci_mcasp *mcasp,
1000 unsigned int rate)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001001{
Daniel Mack64792852014-03-27 11:27:40 +01001002 u32 cs_value = 0;
1003 u8 *cs_bytes = (u8*) &cs_value;
1004
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001005 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
1006 and LSB first */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001007 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXFMT_REG, TXROT(6) | TXSSZ(15));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001008
1009 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001010 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXFMCTL_REG, AFSXE | FSXMOD(0x180));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001011
1012 /* Set the TX tdm : for all the slots */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001013 mcasp_set_reg(mcasp, DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001014
1015 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001016 mcasp_set_bits(mcasp, DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE | TX_ASYNC);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001017
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001018 mcasp_clr_bits(mcasp, DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001019
1020 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001021 mcasp_set_bits(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001022
1023 /* Enable the DIT */
Peter Ujfalusif68205a2013-11-14 11:35:36 +02001024 mcasp_set_bits(mcasp, DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001025
Daniel Mack64792852014-03-27 11:27:40 +01001026 /* Set S/PDIF channel status bits */
1027 cs_bytes[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
1028 cs_bytes[1] = IEC958_AES1_CON_PCM_CODER;
1029
1030 switch (rate) {
1031 case 22050:
1032 cs_bytes[3] |= IEC958_AES3_CON_FS_22050;
1033 break;
1034 case 24000:
1035 cs_bytes[3] |= IEC958_AES3_CON_FS_24000;
1036 break;
1037 case 32000:
1038 cs_bytes[3] |= IEC958_AES3_CON_FS_32000;
1039 break;
1040 case 44100:
1041 cs_bytes[3] |= IEC958_AES3_CON_FS_44100;
1042 break;
1043 case 48000:
1044 cs_bytes[3] |= IEC958_AES3_CON_FS_48000;
1045 break;
1046 case 88200:
1047 cs_bytes[3] |= IEC958_AES3_CON_FS_88200;
1048 break;
1049 case 96000:
1050 cs_bytes[3] |= IEC958_AES3_CON_FS_96000;
1051 break;
1052 case 176400:
1053 cs_bytes[3] |= IEC958_AES3_CON_FS_176400;
1054 break;
1055 case 192000:
1056 cs_bytes[3] |= IEC958_AES3_CON_FS_192000;
1057 break;
1058 default:
1059 printk(KERN_WARNING "unsupported sampling rate: %d\n", rate);
1060 return -EINVAL;
1061 }
1062
1063 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRA_REG, cs_value);
1064 mcasp_set_reg(mcasp, DAVINCI_MCASP_DITCSRB_REG, cs_value);
1065
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001066 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001067}
1068
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001069static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001070 unsigned int sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001071 unsigned int bclk_freq, bool set)
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001072{
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001073 u32 reg = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG);
1074 int div = sysclk_freq / bclk_freq;
1075 int rem = sysclk_freq % bclk_freq;
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001076 int error_ppm;
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001077 int aux_div = 1;
1078
1079 if (div > (ACLKXDIV_MASK + 1)) {
1080 if (reg & AHCLKXE) {
1081 aux_div = div / (ACLKXDIV_MASK + 1);
1082 if (div % (ACLKXDIV_MASK + 1))
1083 aux_div++;
1084
1085 sysclk_freq /= aux_div;
1086 div = sysclk_freq / bclk_freq;
1087 rem = sysclk_freq % bclk_freq;
1088 } else if (set) {
1089 dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
1090 sysclk_freq);
1091 }
1092 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001093
1094 if (rem != 0) {
1095 if (div == 0 ||
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001096 ((sysclk_freq / div) - bclk_freq) >
1097 (bclk_freq - (sysclk_freq / (div+1)))) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001098 div++;
1099 rem = rem - bclk_freq;
1100 }
1101 }
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001102 error_ppm = (div*1000000 + (int)div64_long(1000000LL*rem,
1103 (int)bclk_freq)) / div - 1000000;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001104
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001105 if (set) {
1106 if (error_ppm)
1107 dev_info(mcasp->dev, "Sample-rate is off by %d PPM\n",
1108 error_ppm);
1109
1110 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_BCLK, div, 0);
Peter Ujfalusiddecd142016-05-09 13:42:32 +03001111 if (reg & AHCLKXE)
1112 __davinci_mcasp_set_clkdiv(mcasp, MCASP_CLKDIV_AUXCLK,
1113 aux_div, 0);
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001114 }
1115
1116 return error_ppm;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001117}
1118
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001119static inline u32 davinci_mcasp_tx_delay(struct davinci_mcasp *mcasp)
1120{
1121 if (!mcasp->txnumevt)
1122 return 0;
1123
1124 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_WFIFOSTS_OFFSET);
1125}
1126
1127static inline u32 davinci_mcasp_rx_delay(struct davinci_mcasp *mcasp)
1128{
1129 if (!mcasp->rxnumevt)
1130 return 0;
1131
1132 return mcasp_get_reg(mcasp, mcasp->fifo_base + MCASP_RFIFOSTS_OFFSET);
1133}
1134
1135static snd_pcm_sframes_t davinci_mcasp_delay(
1136 struct snd_pcm_substream *substream,
1137 struct snd_soc_dai *cpu_dai)
1138{
1139 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1140 u32 fifo_use;
1141
1142 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1143 fifo_use = davinci_mcasp_tx_delay(mcasp);
1144 else
1145 fifo_use = davinci_mcasp_rx_delay(mcasp);
1146
1147 /*
1148 * Divide the used locations with the channel count to get the
1149 * FIFO usage in samples (don't care about partial samples in the
1150 * buffer).
1151 */
1152 return fifo_use / substream->runtime->channels;
1153}
1154
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001155static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
1156 struct snd_pcm_hw_params *params,
1157 struct snd_soc_dai *cpu_dai)
1158{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001159 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001160 int word_length;
Peter Ujfalusia7e46bd2014-02-03 14:51:50 +02001161 int channels = params_channels(params);
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001162 int period_size = params_period_size(params);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001163 int ret;
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001164
Peter Ujfalusi4a11ff22016-03-11 13:18:51 +02001165 ret = davinci_mcasp_set_dai_fmt(cpu_dai, mcasp->dai_fmt);
1166 if (ret)
1167 return ret;
1168
Daniel Mack82675252014-07-16 14:04:41 +02001169 /*
1170 * If mcasp is BCLK master, and a BCLK divider was not provided by
1171 * the machine driver, we need to calculate the ratio.
1172 */
1173 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
Jyri Sarha1f114f72015-04-23 16:16:04 +03001174 int slots = mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001175 int rate = params_rate(params);
1176 int sbits = params_width(params);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001177
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001178 if (mcasp->slot_width)
1179 sbits = mcasp->slot_width;
1180
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001181 davinci_mcasp_calc_clk_div(mcasp, mcasp->sysclk_freq,
1182 rate * sbits * slots, true);
Jyri Sarhaab8b14b2014-01-27 17:37:52 +02001183 }
1184
Peter Ujfalusidd093a02014-04-01 15:55:11 +03001185 ret = mcasp_common_hw_param(mcasp, substream->stream,
1186 period_size * channels, channels);
Peter Ujfalusi0f7d9a62014-01-30 15:15:24 +02001187 if (ret)
1188 return ret;
1189
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001190 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
Daniel Mack64792852014-03-27 11:27:40 +01001191 ret = mcasp_dit_hw_param(mcasp, params_rate(params));
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001192 else
Misael Lopez Cruz18a4f552014-11-10 12:32:17 +02001193 ret = mcasp_i2s_hw_param(mcasp, substream->stream,
1194 channels);
Peter Ujfalusi2c56c4c2014-01-30 15:15:23 +02001195
1196 if (ret)
1197 return ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001198
1199 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001200 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001201 case SNDRV_PCM_FORMAT_S8:
Daniel Mackba764b32012-12-05 18:20:37 +01001202 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001203 break;
1204
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001205 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001206 case SNDRV_PCM_FORMAT_S16_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001207 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001208 break;
1209
Daniel Mack21eb24d2012-10-09 09:35:16 +02001210 case SNDRV_PCM_FORMAT_U24_3LE:
1211 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001212 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +02001213 break;
1214
Daniel Mack6b7fa012012-10-09 11:56:40 +02001215 case SNDRV_PCM_FORMAT_U24_LE:
1216 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusi182bef82014-06-26 08:09:24 +03001217 word_length = 24;
1218 break;
1219
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001220 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001221 case SNDRV_PCM_FORMAT_S32_LE:
Daniel Mackba764b32012-12-05 18:20:37 +01001222 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001223 break;
1224
1225 default:
1226 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
1227 return -EINVAL;
1228 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -04001229
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001230 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001231
Peter Ujfalusi11277832014-11-10 12:32:16 +02001232 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE)
1233 mcasp->channels = channels;
1234
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001235 return 0;
1236}
1237
1238static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
1239 int cmd, struct snd_soc_dai *cpu_dai)
1240{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001241 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001242 int ret = 0;
1243
1244 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001245 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +05301246 case SNDRV_PCM_TRIGGER_START:
1247 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001248 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001249 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001250 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +05301251 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001252 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001253 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001254 break;
1255
1256 default:
1257 ret = -EINVAL;
1258 }
1259
1260 return ret;
1261}
1262
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001263static const unsigned int davinci_mcasp_dai_rates[] = {
1264 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
1265 88200, 96000, 176400, 192000,
1266};
1267
1268#define DAVINCI_MAX_RATE_ERROR_PPM 1000
1269
1270static int davinci_mcasp_hw_rule_rate(struct snd_pcm_hw_params *params,
1271 struct snd_pcm_hw_rule *rule)
1272{
1273 struct davinci_mcasp_ruledata *rd = rule->private;
1274 struct snd_interval *ri =
1275 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1276 int sbits = params_width(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001277 int slots = rd->mcasp->tdm_slots;
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001278 struct snd_interval range;
1279 int i;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001280
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001281 if (rd->mcasp->slot_width)
1282 sbits = rd->mcasp->slot_width;
1283
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001284 snd_interval_any(&range);
1285 range.empty = 1;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001286
1287 for (i = 0; i < ARRAY_SIZE(davinci_mcasp_dai_rates); i++) {
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001288 if (snd_interval_test(ri, davinci_mcasp_dai_rates[i])) {
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001289 uint bclk_freq = sbits * slots *
1290 davinci_mcasp_dai_rates[i];
1291 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001292 int ppm;
1293
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001294 if (rd->mcasp->auxclk_fs_ratio)
1295 sysclk_freq = davinci_mcasp_dai_rates[i] *
1296 rd->mcasp->auxclk_fs_ratio;
1297 else
1298 sysclk_freq = rd->mcasp->sysclk_freq;
1299
1300 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
1301 bclk_freq, false);
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001302 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1303 if (range.empty) {
1304 range.min = davinci_mcasp_dai_rates[i];
1305 range.empty = 0;
1306 }
1307 range.max = davinci_mcasp_dai_rates[i];
1308 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001309 }
1310 }
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001311
Jyri Sarha518f6ba2015-04-23 16:16:06 +03001312 dev_dbg(rd->mcasp->dev,
1313 "Frequencies %d-%d -> %d-%d for %d sbits and %d tdm slots\n",
1314 ri->min, ri->max, range.min, range.max, sbits, slots);
1315
1316 return snd_interval_refine(hw_param_interval(params, rule->var),
1317 &range);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001318}
1319
1320static int davinci_mcasp_hw_rule_format(struct snd_pcm_hw_params *params,
1321 struct snd_pcm_hw_rule *rule)
1322{
1323 struct davinci_mcasp_ruledata *rd = rule->private;
1324 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1325 struct snd_mask nfmt;
1326 int rate = params_rate(params);
Jyri Sarha1f114f72015-04-23 16:16:04 +03001327 int slots = rd->mcasp->tdm_slots;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001328 int i, count = 0;
1329
1330 snd_mask_none(&nfmt);
1331
Peter Ujfalusi9be072a2016-09-01 10:05:12 +03001332 for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001333 if (snd_mask_test(fmt, i)) {
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001334 uint sbits = snd_pcm_format_width(i);
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001335 unsigned int sysclk_freq;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001336 int ppm;
1337
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001338 if (rd->mcasp->auxclk_fs_ratio)
1339 sysclk_freq = rate *
1340 rd->mcasp->auxclk_fs_ratio;
1341 else
1342 sysclk_freq = rd->mcasp->sysclk_freq;
1343
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001344 if (rd->mcasp->slot_width)
1345 sbits = rd->mcasp->slot_width;
1346
Peter Ujfalusi764958f2019-06-11 15:29:41 +03001347 ppm = davinci_mcasp_calc_clk_div(rd->mcasp, sysclk_freq,
Peter Ujfalusi3e9bee12016-05-09 13:42:31 +03001348 sbits * slots * rate,
1349 false);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001350 if (abs(ppm) < DAVINCI_MAX_RATE_ERROR_PPM) {
1351 snd_mask_set(&nfmt, i);
1352 count++;
1353 }
1354 }
1355 }
1356 dev_dbg(rd->mcasp->dev,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001357 "%d possible sample format for %d Hz and %d tdm slots\n",
1358 count, rate, slots);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001359
1360 return snd_mask_refine(fmt, &nfmt);
1361}
1362
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001363static int davinci_mcasp_hw_rule_min_periodsize(
1364 struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1365{
1366 struct snd_interval *period_size = hw_param_interval(params,
1367 SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1368 struct snd_interval frames;
1369
1370 snd_interval_any(&frames);
1371 frames.min = 64;
1372 frames.integer = 1;
1373
1374 return snd_interval_refine(period_size, &frames);
1375}
1376
Peter Ujfalusi11277832014-11-10 12:32:16 +02001377static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
1378 struct snd_soc_dai *cpu_dai)
1379{
1380 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001381 struct davinci_mcasp_ruledata *ruledata =
1382 &mcasp->ruledata[substream->stream];
Peter Ujfalusi11277832014-11-10 12:32:16 +02001383 u32 max_channels = 0;
1384 int i, dir;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001385 int tdm_slots = mcasp->tdm_slots;
1386
Peter Ujfalusi19357362016-05-09 13:39:14 +03001387 /* Do not allow more then one stream per direction */
1388 if (mcasp->substreams[substream->stream])
1389 return -EBUSY;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001390
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001391 mcasp->substreams[substream->stream] = substream;
1392
Peter Ujfalusi19357362016-05-09 13:39:14 +03001393 if (mcasp->tdm_mask[substream->stream])
1394 tdm_slots = hweight32(mcasp->tdm_mask[substream->stream]);
1395
Peter Ujfalusi11277832014-11-10 12:32:16 +02001396 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1397 return 0;
1398
1399 /*
1400 * Limit the maximum allowed channels for the first stream:
1401 * number of serializers for the direction * tdm slots per serializer
1402 */
1403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1404 dir = TX_MODE;
1405 else
1406 dir = RX_MODE;
1407
1408 for (i = 0; i < mcasp->num_serializer; i++) {
1409 if (mcasp->serial_dir[i] == dir)
1410 max_channels++;
1411 }
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001412 ruledata->serializers = max_channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001413 max_channels *= tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001414 /*
1415 * If the already active stream has less channels than the calculated
1416 * limnit based on the seirializers * tdm_slots, we need to use that as
1417 * a constraint for the second stream.
1418 * Otherwise (first stream or less allowed channels) we use the
1419 * calculated constraint.
1420 */
1421 if (mcasp->channels && mcasp->channels < max_channels)
1422 max_channels = mcasp->channels;
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001423 /*
1424 * But we can always allow channels upto the amount of
1425 * the available tdm_slots.
1426 */
1427 if (max_channels < tdm_slots)
1428 max_channels = tdm_slots;
Peter Ujfalusi11277832014-11-10 12:32:16 +02001429
1430 snd_pcm_hw_constraint_minmax(substream->runtime,
1431 SNDRV_PCM_HW_PARAM_CHANNELS,
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001432 0, max_channels);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001433
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001434 snd_pcm_hw_constraint_list(substream->runtime,
1435 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1436 &mcasp->chconstr[substream->stream]);
1437
1438 if (mcasp->slot_width)
1439 snd_pcm_hw_constraint_minmax(substream->runtime,
1440 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1441 8, mcasp->slot_width);
Jyri Sarha5935a052015-04-23 16:16:05 +03001442
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001443 /*
1444 * If we rely on implicit BCLK divider setting we should
1445 * set constraints based on what we can provide.
1446 */
1447 if (mcasp->bclk_master && mcasp->bclk_div == 0 && mcasp->sysclk_freq) {
1448 int ret;
1449
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001450 ruledata->mcasp = mcasp;
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001451
1452 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1453 SNDRV_PCM_HW_PARAM_RATE,
1454 davinci_mcasp_hw_rule_rate,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001455 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001456 SNDRV_PCM_HW_PARAM_FORMAT, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001457 if (ret)
1458 return ret;
1459 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
1460 SNDRV_PCM_HW_PARAM_FORMAT,
1461 davinci_mcasp_hw_rule_format,
Peter Ujfalusi4cd9db02015-04-07 14:03:53 +03001462 ruledata,
Jyri Sarha1f114f72015-04-23 16:16:04 +03001463 SNDRV_PCM_HW_PARAM_RATE, -1);
Jyri Sarhaa75a0532015-03-20 13:31:08 +02001464 if (ret)
1465 return ret;
1466 }
1467
Peter Ujfalusid43c17d2018-01-05 12:18:07 +02001468 snd_pcm_hw_rule_add(substream->runtime, 0,
1469 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1470 davinci_mcasp_hw_rule_min_periodsize, NULL,
1471 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1472
Peter Ujfalusi11277832014-11-10 12:32:16 +02001473 return 0;
1474}
1475
1476static void davinci_mcasp_shutdown(struct snd_pcm_substream *substream,
1477 struct snd_soc_dai *cpu_dai)
1478{
1479 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
1480
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02001481 mcasp->substreams[substream->stream] = NULL;
1482
Peter Ujfalusi11277832014-11-10 12:32:16 +02001483 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
1484 return;
1485
1486 if (!cpu_dai->active)
1487 mcasp->channels = 0;
1488}
1489
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001490static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Peter Ujfalusi11277832014-11-10 12:32:16 +02001491 .startup = davinci_mcasp_startup,
1492 .shutdown = davinci_mcasp_shutdown,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001493 .trigger = davinci_mcasp_trigger,
Peter Ujfalusi5fcb4572018-08-31 11:24:56 +03001494 .delay = davinci_mcasp_delay,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001495 .hw_params = davinci_mcasp_hw_params,
1496 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +02001497 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +02001498 .set_sysclk = davinci_mcasp_set_sysclk,
Jyri Sarhadd55ff82015-09-09 21:27:44 +03001499 .set_tdm_slot = davinci_mcasp_set_tdm_slot,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001500};
1501
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001502static int davinci_mcasp_dai_probe(struct snd_soc_dai *dai)
1503{
1504 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
1505
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02001506 dai->playback_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
1507 dai->capture_dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001508
1509 return 0;
1510}
1511
Peter Ujfalusied29cd52013-11-14 11:35:22 +02001512#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
1513
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001514#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1515 SNDRV_PCM_FMTBIT_U8 | \
1516 SNDRV_PCM_FMTBIT_S16_LE | \
1517 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +02001518 SNDRV_PCM_FMTBIT_S24_LE | \
1519 SNDRV_PCM_FMTBIT_U24_LE | \
1520 SNDRV_PCM_FMTBIT_S24_3LE | \
1521 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001522 SNDRV_PCM_FMTBIT_S32_LE | \
1523 SNDRV_PCM_FMTBIT_U32_LE)
1524
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001525static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001526 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001527 .name = "davinci-mcasp.0",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001528 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001529 .playback = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001530 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001531 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001532 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001533 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001534 },
1535 .capture = {
Peter Ujfalusie4798d22017-05-11 09:58:22 +03001536 .channels_min = 1,
Michal Bachraty2952b272013-02-28 16:07:08 +01001537 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001538 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001539 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001540 },
1541 .ops = &davinci_mcasp_dai_ops,
1542
Peter Ujfalusid75249f2014-11-10 12:32:18 +02001543 .symmetric_samplebits = 1,
Jyri Sarha295c3402015-09-09 21:27:42 +03001544 .symmetric_rates = 1,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001545 },
1546 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +02001547 .name = "davinci-mcasp.1",
Peter Ujfalusid5902f692014-04-01 15:55:07 +03001548 .probe = davinci_mcasp_dai_probe,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001549 .playback = {
1550 .channels_min = 1,
1551 .channels_max = 384,
1552 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -04001553 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001554 },
1555 .ops = &davinci_mcasp_dai_ops,
1556 },
1557
1558};
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001559
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001560static const struct snd_soc_component_driver davinci_mcasp_component = {
1561 .name = "davinci-mcasp",
1562};
1563
Jyri Sarha256ba182013-10-18 18:37:42 +03001564/* Some HW specific values and defaults. The rest is filled in from DT. */
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001565static struct davinci_mcasp_pdata dm646x_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001566 .tx_dma_offset = 0x400,
1567 .rx_dma_offset = 0x400,
Jyri Sarha256ba182013-10-18 18:37:42 +03001568 .version = MCASP_VERSION_1,
1569};
1570
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001571static struct davinci_mcasp_pdata da830_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001572 .tx_dma_offset = 0x2000,
1573 .rx_dma_offset = 0x2000,
Jyri Sarha256ba182013-10-18 18:37:42 +03001574 .version = MCASP_VERSION_2,
1575};
1576
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001577static struct davinci_mcasp_pdata am33xx_mcasp_pdata = {
Jyri Sarha256ba182013-10-18 18:37:42 +03001578 .tx_dma_offset = 0,
1579 .rx_dma_offset = 0,
Jyri Sarha256ba182013-10-18 18:37:42 +03001580 .version = MCASP_VERSION_3,
1581};
1582
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001583static struct davinci_mcasp_pdata dra7_mcasp_pdata = {
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001584 /* The CFG port offset will be calculated if it is needed */
1585 .tx_dma_offset = 0,
1586 .rx_dma_offset = 0,
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001587 .version = MCASP_VERSION_4,
1588};
1589
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301590static const struct of_device_id mcasp_dt_ids[] = {
1591 {
1592 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001593 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301594 },
1595 {
1596 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +03001597 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301598 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301599 {
Jyri Sarha3af9e032013-10-18 18:37:44 +03001600 .compatible = "ti,am33xx-mcasp-audio",
Peter Ujfalusib14899d2013-11-14 11:35:37 +02001601 .data = &am33xx_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +05301602 },
Peter Ujfalusi453c4992013-11-14 11:35:34 +02001603 {
1604 .compatible = "ti,dra7-mcasp-audio",
1605 .data = &dra7_mcasp_pdata,
1606 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301607 { /* sentinel */ }
1608};
1609MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
1610
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001611static int mcasp_reparent_fck(struct platform_device *pdev)
1612{
1613 struct device_node *node = pdev->dev.of_node;
1614 struct clk *gfclk, *parent_clk;
1615 const char *parent_name;
1616 int ret;
1617
1618 if (!node)
1619 return 0;
1620
1621 parent_name = of_get_property(node, "fck_parent", NULL);
1622 if (!parent_name)
1623 return 0;
1624
Peter Ujfalusic6702542016-01-27 15:02:49 +02001625 dev_warn(&pdev->dev, "Update the bindings to use assigned-clocks!\n");
1626
Peter Ujfalusiae726e92013-11-14 11:35:35 +02001627 gfclk = clk_get(&pdev->dev, "fck");
1628 if (IS_ERR(gfclk)) {
1629 dev_err(&pdev->dev, "failed to get fck\n");
1630 return PTR_ERR(gfclk);
1631 }
1632
1633 parent_clk = clk_get(NULL, parent_name);
1634 if (IS_ERR(parent_clk)) {
1635 dev_err(&pdev->dev, "failed to get parent clock\n");
1636 ret = PTR_ERR(parent_clk);
1637 goto err1;
1638 }
1639
1640 ret = clk_set_parent(gfclk, parent_clk);
1641 if (ret) {
1642 dev_err(&pdev->dev, "failed to reparent fck\n");
1643 goto err2;
1644 }
1645
1646err2:
1647 clk_put(parent_clk);
1648err1:
1649 clk_put(gfclk);
1650 return ret;
1651}
1652
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001653static struct davinci_mcasp_pdata *davinci_mcasp_set_pdata_from_of(
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301654 struct platform_device *pdev)
1655{
1656 struct device_node *np = pdev->dev.of_node;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02001657 struct davinci_mcasp_pdata *pdata = NULL;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301658 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +05301659 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001660 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301661
1662 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301663 u32 val;
1664 int i, ret = 0;
1665
1666 if (pdev->dev.platform_data) {
1667 pdata = pdev->dev.platform_data;
Peter Ujfalusibc184542018-11-16 15:41:41 +02001668 pdata->dismod = DISMOD_LOW;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301669 return pdata;
1670 } else if (match) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001671 pdata = devm_kmemdup(&pdev->dev, match->data, sizeof(*pdata),
1672 GFP_KERNEL);
1673 if (!pdata) {
Peter Ujfalusi272ee032016-06-02 12:55:24 +03001674 ret = -ENOMEM;
1675 return pdata;
1676 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301677 } else {
1678 /* control shouldn't reach here. something is wrong */
1679 ret = -EINVAL;
1680 goto nodata;
1681 }
1682
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301683 ret = of_property_read_u32(np, "op-mode", &val);
1684 if (ret >= 0)
1685 pdata->op_mode = val;
1686
1687 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +01001688 if (ret >= 0) {
1689 if (val < 2 || val > 32) {
1690 dev_err(&pdev->dev,
1691 "tdm-slots must be in rage [2-32]\n");
1692 ret = -EINVAL;
1693 goto nodata;
1694 }
1695
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301696 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +01001697 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301698
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301699 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
1700 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301701 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001702 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
1703 (sizeof(*of_serial_dir) * val),
1704 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301705 if (!of_serial_dir) {
1706 ret = -ENOMEM;
1707 goto nodata;
1708 }
1709
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001710 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301711 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
1712
Peter Ujfalusi1427e662013-10-18 18:37:46 +03001713 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301714 pdata->serial_dir = of_serial_dir;
1715 }
1716
Jyri Sarha4023fe62013-10-18 18:37:43 +03001717 ret = of_property_match_string(np, "dma-names", "tx");
1718 if (ret < 0)
1719 goto nodata;
1720
1721 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1722 &dma_spec);
1723 if (ret < 0)
1724 goto nodata;
1725
1726 pdata->tx_dma_channel = dma_spec.args[0];
1727
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001728 /* RX is not valid in DIT mode */
1729 if (pdata->op_mode != DAVINCI_MCASP_DIT_MODE) {
1730 ret = of_property_match_string(np, "dma-names", "rx");
1731 if (ret < 0)
1732 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001733
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001734 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
1735 &dma_spec);
1736 if (ret < 0)
1737 goto nodata;
Jyri Sarha4023fe62013-10-18 18:37:43 +03001738
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02001739 pdata->rx_dma_channel = dma_spec.args[0];
1740 }
Jyri Sarha4023fe62013-10-18 18:37:43 +03001741
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301742 ret = of_property_read_u32(np, "tx-num-evt", &val);
1743 if (ret >= 0)
1744 pdata->txnumevt = val;
1745
1746 ret = of_property_read_u32(np, "rx-num-evt", &val);
1747 if (ret >= 0)
1748 pdata->rxnumevt = val;
1749
1750 ret = of_property_read_u32(np, "sram-size-playback", &val);
1751 if (ret >= 0)
1752 pdata->sram_size_playback = val;
1753
1754 ret = of_property_read_u32(np, "sram-size-capture", &val);
1755 if (ret >= 0)
1756 pdata->sram_size_capture = val;
1757
Peter Ujfalusibc184542018-11-16 15:41:41 +02001758 ret = of_property_read_u32(np, "dismod", &val);
1759 if (ret >= 0) {
1760 if (val == 0 || val == 2 || val == 3) {
1761 pdata->dismod = DISMOD_VAL(val);
1762 } else {
1763 dev_warn(&pdev->dev, "Invalid dismod value: %u\n", val);
1764 pdata->dismod = DISMOD_LOW;
1765 }
1766 } else {
1767 pdata->dismod = DISMOD_LOW;
1768 }
1769
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05301770 return pdata;
1771
1772nodata:
1773 if (ret < 0) {
1774 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
1775 ret);
1776 pdata = NULL;
1777 }
1778 return pdata;
1779}
1780
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001781enum {
1782 PCM_EDMA,
1783 PCM_SDMA,
1784};
1785static const char *sdma_prefix = "ti,omap";
1786
1787static int davinci_mcasp_get_dma_type(struct davinci_mcasp *mcasp)
1788{
1789 struct dma_chan *chan;
1790 const char *tmp;
1791 int ret = PCM_EDMA;
1792
1793 if (!mcasp->dev->of_node)
1794 return PCM_EDMA;
1795
1796 tmp = mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK].filter_data;
1797 chan = dma_request_slave_channel_reason(mcasp->dev, tmp);
1798 if (IS_ERR(chan)) {
1799 if (PTR_ERR(chan) != -EPROBE_DEFER)
1800 dev_err(mcasp->dev,
1801 "Can't verify DMA configuration (%ld)\n",
1802 PTR_ERR(chan));
1803 return PTR_ERR(chan);
1804 }
Takashi Iwaibefff4f2017-09-07 10:59:17 +02001805 if (WARN_ON(!chan->device || !chan->device->dev))
1806 return -EINVAL;
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03001807
1808 if (chan->device->dev->of_node)
1809 ret = of_property_read_string(chan->device->dev->of_node,
1810 "compatible", &tmp);
1811 else
1812 dev_dbg(mcasp->dev, "DMA controller has no of-node\n");
1813
1814 dma_release_channel(chan);
1815 if (ret)
1816 return ret;
1817
1818 dev_dbg(mcasp->dev, "DMA controller compatible = \"%s\"\n", tmp);
1819 if (!strncmp(tmp, sdma_prefix, strlen(sdma_prefix)))
1820 return PCM_SDMA;
1821
1822 return PCM_EDMA;
1823}
1824
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03001825static u32 davinci_mcasp_txdma_offset(struct davinci_mcasp_pdata *pdata)
1826{
1827 int i;
1828 u32 offset = 0;
1829
1830 if (pdata->version != MCASP_VERSION_4)
1831 return pdata->tx_dma_offset;
1832
1833 for (i = 0; i < pdata->num_serializer; i++) {
1834 if (pdata->serial_dir[i] == TX_MODE) {
1835 if (!offset) {
1836 offset = DAVINCI_MCASP_TXBUF_REG(i);
1837 } else {
1838 pr_err("%s: Only one serializer allowed!\n",
1839 __func__);
1840 break;
1841 }
1842 }
1843 }
1844
1845 return offset;
1846}
1847
1848static u32 davinci_mcasp_rxdma_offset(struct davinci_mcasp_pdata *pdata)
1849{
1850 int i;
1851 u32 offset = 0;
1852
1853 if (pdata->version != MCASP_VERSION_4)
1854 return pdata->rx_dma_offset;
1855
1856 for (i = 0; i < pdata->num_serializer; i++) {
1857 if (pdata->serial_dir[i] == RX_MODE) {
1858 if (!offset) {
1859 offset = DAVINCI_MCASP_RXBUF_REG(i);
1860 } else {
1861 pr_err("%s: Only one serializer allowed!\n",
1862 __func__);
1863 break;
1864 }
1865 }
1866 }
1867
1868 return offset;
1869}
1870
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02001871#ifdef CONFIG_GPIOLIB
1872static int davinci_mcasp_gpio_request(struct gpio_chip *chip, unsigned offset)
1873{
1874 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1875
1876 if (mcasp->num_serializer && offset < mcasp->num_serializer &&
1877 mcasp->serial_dir[offset] != INACTIVE_MODE) {
1878 dev_err(mcasp->dev, "AXR%u pin is used for audio\n", offset);
1879 return -EBUSY;
1880 }
1881
1882 /* Do not change the PIN yet */
1883
1884 return pm_runtime_get_sync(mcasp->dev);
1885}
1886
1887static void davinci_mcasp_gpio_free(struct gpio_chip *chip, unsigned offset)
1888{
1889 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1890
1891 /* Set the direction to input */
1892 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
1893
1894 /* Set the pin as McASP pin */
1895 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
1896
1897 pm_runtime_put_sync(mcasp->dev);
1898}
1899
1900static int davinci_mcasp_gpio_direction_out(struct gpio_chip *chip,
1901 unsigned offset, int value)
1902{
1903 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1904 u32 val;
1905
1906 if (value)
1907 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
1908 else
1909 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
1910
1911 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
1912 if (!(val & BIT(offset))) {
1913 /* Set the pin as GPIO pin */
1914 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
1915
1916 /* Set the direction to output */
1917 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
1918 }
1919
1920 return 0;
1921}
1922
1923static void davinci_mcasp_gpio_set(struct gpio_chip *chip, unsigned offset,
1924 int value)
1925{
1926 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1927
1928 if (value)
1929 mcasp_set_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
1930 else
1931 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDOUT_REG, BIT(offset));
1932}
1933
1934static int davinci_mcasp_gpio_direction_in(struct gpio_chip *chip,
1935 unsigned offset)
1936{
1937 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1938 u32 val;
1939
1940 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PFUNC_REG);
1941 if (!(val & BIT(offset))) {
1942 /* Set the direction to input */
1943 mcasp_clr_bits(mcasp, DAVINCI_MCASP_PDIR_REG, BIT(offset));
1944
1945 /* Set the pin as GPIO pin */
1946 mcasp_set_bits(mcasp, DAVINCI_MCASP_PFUNC_REG, BIT(offset));
1947 }
1948
1949 return 0;
1950}
1951
1952static int davinci_mcasp_gpio_get(struct gpio_chip *chip, unsigned offset)
1953{
1954 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1955 u32 val;
1956
1957 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDSET_REG);
1958 if (val & BIT(offset))
1959 return 1;
1960
1961 return 0;
1962}
1963
1964static int davinci_mcasp_gpio_get_direction(struct gpio_chip *chip,
1965 unsigned offset)
1966{
1967 struct davinci_mcasp *mcasp = gpiochip_get_data(chip);
1968 u32 val;
1969
1970 val = mcasp_get_reg(mcasp, DAVINCI_MCASP_PDIR_REG);
1971 if (val & BIT(offset))
1972 return 0;
1973
1974 return 1;
1975}
1976
1977static const struct gpio_chip davinci_mcasp_template_chip = {
1978 .owner = THIS_MODULE,
1979 .request = davinci_mcasp_gpio_request,
1980 .free = davinci_mcasp_gpio_free,
1981 .direction_output = davinci_mcasp_gpio_direction_out,
1982 .set = davinci_mcasp_gpio_set,
1983 .direction_input = davinci_mcasp_gpio_direction_in,
1984 .get = davinci_mcasp_gpio_get,
1985 .get_direction = davinci_mcasp_gpio_get_direction,
1986 .base = -1,
1987 .ngpio = 32,
1988};
1989
1990static int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
1991{
1992 if (!of_property_read_bool(mcasp->dev->of_node, "gpio-controller"))
1993 return 0;
1994
1995 mcasp->gpio_chip = davinci_mcasp_template_chip;
1996 mcasp->gpio_chip.label = dev_name(mcasp->dev);
1997 mcasp->gpio_chip.parent = mcasp->dev;
1998#ifdef CONFIG_OF_GPIO
1999 mcasp->gpio_chip.of_node = mcasp->dev->of_node;
2000#endif
2001
2002 return devm_gpiochip_add_data(mcasp->dev, &mcasp->gpio_chip, mcasp);
2003}
2004
2005#else /* CONFIG_GPIOLIB */
2006static inline int davinci_mcasp_init_gpiochip(struct davinci_mcasp *mcasp)
2007{
2008 return 0;
2009}
2010#endif /* CONFIG_GPIOLIB */
2011
Peter Ujfalusi764958f2019-06-11 15:29:41 +03002012static int davinci_mcasp_get_dt_params(struct davinci_mcasp *mcasp)
2013{
2014 struct device_node *np = mcasp->dev->of_node;
2015 int ret;
2016 u32 val;
2017
2018 if (!np)
2019 return 0;
2020
2021 ret = of_property_read_u32(np, "auxclk-fs-ratio", &val);
2022 if (ret >= 0)
2023 mcasp->auxclk_fs_ratio = val;
2024
2025 return 0;
2026}
2027
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002028static int davinci_mcasp_probe(struct platform_device *pdev)
2029{
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002030 struct snd_dmaengine_dai_dma_data *dma_data;
Axel Lin508a43f2015-08-24 16:47:36 +08002031 struct resource *mem, *res, *dat;
Peter Ujfalusid1debaf2014-02-03 14:51:51 +02002032 struct davinci_mcasp_pdata *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002033 struct davinci_mcasp *mcasp;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002034 char *irq_name;
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002035 int *dma;
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002036 int irq;
Julia Lawall96d31e22011-12-29 17:51:21 +01002037 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002038
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05302039 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
2040 dev_err(&pdev->dev, "No platform data supplied\n");
2041 return -EINVAL;
2042 }
2043
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002044 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +01002045 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002046 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002047 return -ENOMEM;
2048
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +05302049 pdata = davinci_mcasp_set_pdata_from_of(pdev);
2050 if (!pdata) {
2051 dev_err(&pdev->dev, "no platform data\n");
2052 return -EINVAL;
2053 }
2054
Jyri Sarha256ba182013-10-18 18:37:42 +03002055 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002056 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002057 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +03002058 "\"mpu\" mem resource not found, using index 0\n");
2059 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2060 if (!mem) {
2061 dev_err(&pdev->dev, "no mem resource?\n");
2062 return -ENODEV;
2063 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002064 }
2065
Axel Lin508a43f2015-08-24 16:47:36 +08002066 mcasp->base = devm_ioremap_resource(&pdev->dev, mem);
2067 if (IS_ERR(mcasp->base))
2068 return PTR_ERR(mcasp->base);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002069
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302070 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002071
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002072 mcasp->op_mode = pdata->op_mode;
Peter Ujfalusi1a5923d2014-11-10 12:32:15 +02002073 /* sanity check for tdm slots parameter */
2074 if (mcasp->op_mode == DAVINCI_MCASP_IIS_MODE) {
2075 if (pdata->tdm_slots < 2) {
2076 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2077 pdata->tdm_slots);
2078 mcasp->tdm_slots = 2;
2079 } else if (pdata->tdm_slots > 32) {
2080 dev_err(&pdev->dev, "invalid tdm slots: %d\n",
2081 pdata->tdm_slots);
2082 mcasp->tdm_slots = 32;
2083 } else {
2084 mcasp->tdm_slots = pdata->tdm_slots;
2085 }
2086 }
2087
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002088 mcasp->num_serializer = pdata->num_serializer;
Peter Ujfalusi61754712019-01-03 16:05:50 +02002089#ifdef CONFIG_PM
Kees Cooka86854d2018-06-12 14:07:58 -07002090 mcasp->context.xrsr_regs = devm_kcalloc(&pdev->dev,
2091 mcasp->num_serializer, sizeof(u32),
Peter Ujfalusif114ce62014-10-01 16:02:12 +03002092 GFP_KERNEL);
Christophe Jaillet4243e042017-08-27 08:46:50 +02002093 if (!mcasp->context.xrsr_regs) {
2094 ret = -ENOMEM;
2095 goto err;
2096 }
Peter Ujfalusif114ce62014-10-01 16:02:12 +03002097#endif
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002098 mcasp->serial_dir = pdata->serial_dir;
2099 mcasp->version = pdata->version;
2100 mcasp->txnumevt = pdata->txnumevt;
2101 mcasp->rxnumevt = pdata->rxnumevt;
Peter Ujfalusibc184542018-11-16 15:41:41 +02002102 mcasp->dismod = pdata->dismod;
Peter Ujfalusi487dce82013-11-14 11:35:31 +02002103
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002104 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002105
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002106 irq = platform_get_irq_byname(pdev, "common");
2107 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002108 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_common",
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002109 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302110 if (!irq_name) {
2111 ret = -ENOMEM;
2112 goto err;
2113 }
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002114 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2115 davinci_mcasp_common_irq_handler,
Peter Ujfalusi8f511ff2015-02-02 14:38:32 +02002116 IRQF_ONESHOT | IRQF_SHARED,
2117 irq_name, mcasp);
Peter Ujfalusi5a1b8a82014-12-30 16:10:32 +02002118 if (ret) {
2119 dev_err(&pdev->dev, "common IRQ request failed\n");
2120 goto err;
2121 }
2122
2123 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2124 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2125 }
2126
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002127 irq = platform_get_irq_byname(pdev, "rx");
2128 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002129 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_rx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002130 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302131 if (!irq_name) {
2132 ret = -ENOMEM;
2133 goto err;
2134 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002135 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2136 davinci_mcasp_rx_irq_handler,
2137 IRQF_ONESHOT, irq_name, mcasp);
2138 if (ret) {
2139 dev_err(&pdev->dev, "RX IRQ request failed\n");
2140 goto err;
2141 }
2142
2143 mcasp->irq_request[SNDRV_PCM_STREAM_CAPTURE] = ROVRN;
2144 }
2145
2146 irq = platform_get_irq_byname(pdev, "tx");
2147 if (irq >= 0) {
Peter Ujfalusiab1fffe2015-09-18 15:02:50 +03002148 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_tx",
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002149 dev_name(&pdev->dev));
Arvind Yadav0c8b7942017-09-20 15:36:09 +05302150 if (!irq_name) {
2151 ret = -ENOMEM;
2152 goto err;
2153 }
Misael Lopez Cruza7a33242014-11-12 16:38:05 +02002154 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
2155 davinci_mcasp_tx_irq_handler,
2156 IRQF_ONESHOT, irq_name, mcasp);
2157 if (ret) {
2158 dev_err(&pdev->dev, "TX IRQ request failed\n");
2159 goto err;
2160 }
2161
2162 mcasp->irq_request[SNDRV_PCM_STREAM_PLAYBACK] = XUNDRN;
2163 }
2164
Jyri Sarha256ba182013-10-18 18:37:42 +03002165 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002166 if (dat)
2167 mcasp->dat_port = true;
Jyri Sarha256ba182013-10-18 18:37:42 +03002168
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002169 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002170 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002171 dma_data->addr = dat->start;
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002172 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03002173 dma_data->addr = mem->start + davinci_mcasp_txdma_offset(pdata);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002174
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002175 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_PLAYBACK];
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002176 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +03002177 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002178 *dma = res->start;
Jyri Sarha4023fe62013-10-18 18:37:43 +03002179 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002180 *dma = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -07002181
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002182 /* dmaengine filter data for DT and non-DT boot */
2183 if (pdev->dev.of_node)
2184 dma_data->filter_data = "tx";
2185 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002186 dma_data->filter_data = dma;
Peter Ujfalusi8de131f2014-03-14 16:42:46 +02002187
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002188 /* RX is not valid in DIT mode */
2189 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002190 dma_data = &mcasp->dma_data[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002191 if (dat)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002192 dma_data->addr = dat->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002193 else
Peter Ujfalusi9ac00132016-06-02 12:55:05 +03002194 dma_data->addr =
2195 mem->start + davinci_mcasp_rxdma_offset(pdata);
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002196
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002197 dma = &mcasp->dma_request[SNDRV_PCM_STREAM_CAPTURE];
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002198 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
2199 if (res)
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002200 *dma = res->start;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002201 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002202 *dma = pdata->rx_dma_channel;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002203
2204 /* dmaengine filter data for DT and non-DT boot */
2205 if (pdev->dev.of_node)
2206 dma_data->filter_data = "rx";
2207 else
Peter Ujfalusi9759e7e2015-03-03 16:45:20 +02002208 dma_data->filter_data = dma;
Peter Ujfalusicaa1d792015-02-02 14:38:33 +02002209 }
Peter Ujfalusi453c4992013-11-14 11:35:34 +02002210
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002211 if (mcasp->version < MCASP_VERSION_3) {
2212 mcasp->fifo_base = DAVINCI_MCASP_V2_AFIFO_BASE;
Peter Ujfalusi64ebdec2014-03-07 15:03:55 +02002213 /* dma_params->dma_addr is pointing to the data port address */
Peter Ujfalusicbc7956c2013-11-14 11:35:32 +02002214 mcasp->dat_port = true;
2215 } else {
2216 mcasp->fifo_base = DAVINCI_MCASP_V3_AFIFO_BASE;
2217 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002218
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002219 /* Allocate memory for long enough list for all possible
2220 * scenarios. Maximum number tdm slots is 32 and there cannot
2221 * be more serializers than given in the configuration. The
2222 * serializer directions could be taken into account, but it
2223 * would make code much more complex and save only couple of
2224 * bytes.
2225 */
2226 mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002227 devm_kcalloc(mcasp->dev,
2228 32 + mcasp->num_serializer - 1,
2229 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002230 GFP_KERNEL);
2231
2232 mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list =
Kees Cooka86854d2018-06-12 14:07:58 -07002233 devm_kcalloc(mcasp->dev,
2234 32 + mcasp->num_serializer - 1,
2235 sizeof(unsigned int),
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002236 GFP_KERNEL);
2237
2238 if (!mcasp->chconstr[SNDRV_PCM_STREAM_PLAYBACK].list ||
Christophe Jaillet1b8b68b2017-09-16 07:40:29 +02002239 !mcasp->chconstr[SNDRV_PCM_STREAM_CAPTURE].list) {
2240 ret = -ENOMEM;
2241 goto err;
2242 }
Jyri Sarhadd55ff82015-09-09 21:27:44 +03002243
2244 ret = davinci_mcasp_set_ch_constraints(mcasp);
Jyri Sarha5935a052015-04-23 16:16:05 +03002245 if (ret)
2246 goto err;
2247
Peter Ujfalusi70091a32013-11-14 11:35:29 +02002248 dev_set_drvdata(&pdev->dev, mcasp);
Peter Ujfalusiae726e92013-11-14 11:35:35 +02002249
2250 mcasp_reparent_fck(pdev);
2251
Peter Ujfalusi540f1ba72019-01-03 16:05:52 +02002252 /* All PINS as McASP */
2253 pm_runtime_get_sync(mcasp->dev);
2254 mcasp_set_reg(mcasp, DAVINCI_MCASP_PFUNC_REG, 0x00000000);
2255 pm_runtime_put(mcasp->dev);
2256
2257 ret = davinci_mcasp_init_gpiochip(mcasp);
2258 if (ret)
2259 goto err;
2260
Peter Ujfalusi764958f2019-06-11 15:29:41 +03002261 ret = davinci_mcasp_get_dt_params(mcasp);
2262 if (ret)
2263 return -EINVAL;
2264
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002265 ret = devm_snd_soc_register_component(&pdev->dev,
2266 &davinci_mcasp_component,
2267 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002268
2269 if (ret != 0)
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002270 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302271
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002272 ret = davinci_mcasp_get_dma_type(mcasp);
2273 switch (ret) {
2274 case PCM_EDMA:
Peter Ujfalusif3f9cfa2014-07-16 15:12:04 +03002275 ret = edma_pcm_platform_register(&pdev->dev);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002276 break;
2277 case PCM_SDMA:
Peter Ujfalusi077a4032018-05-09 14:03:55 +03002278 ret = sdma_pcm_platform_register(&pdev->dev, NULL, NULL);
Jyri Sarha9fbd58c2015-06-02 23:09:34 +03002279 break;
2280 default:
2281 dev_err(&pdev->dev, "No DMA controller found (%d)\n", ret);
2282 case -EPROBE_DEFER:
2283 goto err;
Peter Ujfalusid5c6c592014-04-16 15:46:20 +03002284 break;
2285 }
2286
2287 if (ret) {
2288 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002289 goto err;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05302290 }
2291
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002292 return 0;
2293
Peter Ujfalusib6bb3702014-04-22 14:03:13 +03002294err:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302295 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002296 return ret;
2297}
2298
2299static int davinci_mcasp_remove(struct platform_device *pdev)
2300{
Hebbar, Gururaja10884342012-08-08 20:40:32 +05302301 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002302
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002303 return 0;
2304}
2305
Peter Ujfalusi61754712019-01-03 16:05:50 +02002306#ifdef CONFIG_PM
2307static int davinci_mcasp_runtime_suspend(struct device *dev)
2308{
2309 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2310 struct davinci_mcasp_context *context = &mcasp->context;
2311 u32 reg;
2312 int i;
2313
2314 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2315 context->config_regs[i] = mcasp_get_reg(mcasp, context_regs[i]);
2316
2317 if (mcasp->txnumevt) {
2318 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2319 context->afifo_regs[0] = mcasp_get_reg(mcasp, reg);
2320 }
2321 if (mcasp->rxnumevt) {
2322 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2323 context->afifo_regs[1] = mcasp_get_reg(mcasp, reg);
2324 }
2325
2326 for (i = 0; i < mcasp->num_serializer; i++)
2327 context->xrsr_regs[i] = mcasp_get_reg(mcasp,
2328 DAVINCI_MCASP_XRSRCTL_REG(i));
2329
2330 return 0;
2331}
2332
2333static int davinci_mcasp_runtime_resume(struct device *dev)
2334{
2335 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
2336 struct davinci_mcasp_context *context = &mcasp->context;
2337 u32 reg;
2338 int i;
2339
2340 for (i = 0; i < ARRAY_SIZE(context_regs); i++)
2341 mcasp_set_reg(mcasp, context_regs[i], context->config_regs[i]);
2342
2343 if (mcasp->txnumevt) {
2344 reg = mcasp->fifo_base + MCASP_WFIFOCTL_OFFSET;
2345 mcasp_set_reg(mcasp, reg, context->afifo_regs[0]);
2346 }
2347 if (mcasp->rxnumevt) {
2348 reg = mcasp->fifo_base + MCASP_RFIFOCTL_OFFSET;
2349 mcasp_set_reg(mcasp, reg, context->afifo_regs[1]);
2350 }
2351
2352 for (i = 0; i < mcasp->num_serializer; i++)
2353 mcasp_set_reg(mcasp, DAVINCI_MCASP_XRSRCTL_REG(i),
2354 context->xrsr_regs[i]);
2355
2356 return 0;
2357}
2358
2359#endif
2360
2361static const struct dev_pm_ops davinci_mcasp_pm_ops = {
2362 SET_RUNTIME_PM_OPS(davinci_mcasp_runtime_suspend,
2363 davinci_mcasp_runtime_resume,
2364 NULL)
2365};
2366
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002367static struct platform_driver davinci_mcasp_driver = {
2368 .probe = davinci_mcasp_probe,
2369 .remove = davinci_mcasp_remove,
2370 .driver = {
2371 .name = "davinci-mcasp",
Peter Ujfalusi61754712019-01-03 16:05:50 +02002372 .pm = &davinci_mcasp_pm_ops,
Sachin Kamatea421eb2013-05-22 16:53:37 +05302373 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002374 },
2375};
2376
Axel Linf9b8a512011-11-25 10:09:27 +08002377module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04002378
2379MODULE_AUTHOR("Steve Chen");
2380MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
2381MODULE_LICENSE("GPL");