blob: 70107956dd0c86f903249fcab638ce90d15fb051 [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>
Hebbar, Gururaja10884342012-08-08 20:40:32 +053024#include <linux/pm_runtime.h>
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +053025#include <linux/of.h>
26#include <linux/of_platform.h>
27#include <linux/of_device.h>
Chaithrika U Sb67f4482009-06-05 06:28:40 -040028
29#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/initval.h>
33#include <sound/soc.h>
34
35#include "davinci-pcm.h"
36#include "davinci-mcasp.h"
37
Chaithrika U Sb67f4482009-06-05 06:28:40 -040038#define DAVINCI_MCASP_NUM_SERIALIZER 16
39
Peter Ujfalusi21400a72013-11-14 11:35:26 +020040struct davinci_audio_dev {
41 struct davinci_pcm_dma_params dma_params[2];
42 void __iomem *base;
43 struct device *dev;
44
45 /* McASP specific data */
46 int tdm_slots;
47 u8 op_mode;
48 u8 num_serializer;
49 u8 *serial_dir;
50 u8 version;
51 u16 bclk_lrclk_ratio;
52
53 /* McASP FIFO related */
54 u8 txnumevt;
55 u8 rxnumevt;
56
57#ifdef CONFIG_PM_SLEEP
58 struct {
59 u32 txfmtctl;
60 u32 rxfmtctl;
61 u32 txfmt;
62 u32 rxfmt;
63 u32 aclkxctl;
64 u32 aclkrctl;
65 u32 pdir;
66 } context;
67#endif
68};
69
Chaithrika U Sb67f4482009-06-05 06:28:40 -040070static inline void mcasp_set_bits(void __iomem *reg, u32 val)
71{
72 __raw_writel(__raw_readl(reg) | val, reg);
73}
74
75static inline void mcasp_clr_bits(void __iomem *reg, u32 val)
76{
77 __raw_writel((__raw_readl(reg) & ~(val)), reg);
78}
79
80static inline void mcasp_mod_bits(void __iomem *reg, u32 val, u32 mask)
81{
82 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
83}
84
85static inline void mcasp_set_reg(void __iomem *reg, u32 val)
86{
87 __raw_writel(val, reg);
88}
89
90static inline u32 mcasp_get_reg(void __iomem *reg)
91{
92 return (unsigned int)__raw_readl(reg);
93}
94
95static inline void mcasp_set_ctl_reg(void __iomem *regs, u32 val)
96{
97 int i = 0;
98
99 mcasp_set_bits(regs, val);
100
101 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
102 /* loop count is to avoid the lock-up */
103 for (i = 0; i < 1000; i++) {
104 if ((mcasp_get_reg(regs) & val) == val)
105 break;
106 }
107
108 if (i == 1000 && ((mcasp_get_reg(regs) & val) != val))
109 printk(KERN_ERR "GBLCTL write error\n");
110}
111
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400112static void mcasp_start_rx(struct davinci_audio_dev *dev)
113{
114 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
115 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
116 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
117 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXBUF_REG, 0);
118
119 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
120 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
121 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXBUF_REG, 0);
122
123 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
124 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
125}
126
127static void mcasp_start_tx(struct davinci_audio_dev *dev)
128{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400129 u8 offset = 0, i;
130 u32 cnt;
131
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400132 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
133 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
134 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
135 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
136
137 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
138 mcasp_set_ctl_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
139 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400140 for (i = 0; i < dev->num_serializer; i++) {
141 if (dev->serial_dir[i] == TX_MODE) {
142 offset = i;
143 break;
144 }
145 }
146
147 /* wait for TX ready */
148 cnt = 0;
149 while (!(mcasp_get_reg(dev->base + DAVINCI_MCASP_XRSRCTL_REG(offset)) &
150 TXSTATE) && (cnt < 100000))
151 cnt++;
152
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400153 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXBUF_REG, 0);
154}
155
156static void davinci_mcasp_start(struct davinci_audio_dev *dev, int stream)
157{
Chaithrika U S539d3d82009-09-23 10:12:08 -0400158 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530159 if (dev->txnumevt) { /* enable FIFO */
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530160 switch (dev->version) {
161 case MCASP_VERSION_3:
162 mcasp_clr_bits(dev->base + MCASP_VER3_WFIFOCTL,
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530163 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530164 mcasp_set_bits(dev->base + MCASP_VER3_WFIFOCTL,
Chaithrika U S539d3d82009-09-23 10:12:08 -0400165 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530166 break;
167 default:
168 mcasp_clr_bits(dev->base +
169 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
170 mcasp_set_bits(dev->base +
171 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
172 }
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530173 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400174 mcasp_start_tx(dev);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400175 } else {
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530176 if (dev->rxnumevt) { /* enable FIFO */
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530177 switch (dev->version) {
178 case MCASP_VERSION_3:
179 mcasp_clr_bits(dev->base + MCASP_VER3_RFIFOCTL,
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530180 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530181 mcasp_set_bits(dev->base + MCASP_VER3_RFIFOCTL,
Chaithrika U S539d3d82009-09-23 10:12:08 -0400182 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530183 break;
184 default:
185 mcasp_clr_bits(dev->base +
186 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
187 mcasp_set_bits(dev->base +
188 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
189 }
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530190 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400191 mcasp_start_rx(dev);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400192 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400193}
194
195static void mcasp_stop_rx(struct davinci_audio_dev *dev)
196{
197 mcasp_set_reg(dev->base + DAVINCI_MCASP_GBLCTLR_REG, 0);
198 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
199}
200
201static void mcasp_stop_tx(struct davinci_audio_dev *dev)
202{
203 mcasp_set_reg(dev->base + DAVINCI_MCASP_GBLCTLX_REG, 0);
204 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
205}
206
207static void davinci_mcasp_stop(struct davinci_audio_dev *dev, int stream)
208{
Chaithrika U S539d3d82009-09-23 10:12:08 -0400209 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530210 if (dev->txnumevt) { /* disable FIFO */
211 switch (dev->version) {
212 case MCASP_VERSION_3:
213 mcasp_clr_bits(dev->base + MCASP_VER3_WFIFOCTL,
Chaithrika U S539d3d82009-09-23 10:12:08 -0400214 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530215 break;
216 default:
217 mcasp_clr_bits(dev->base +
218 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
219 }
220 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400221 mcasp_stop_tx(dev);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400222 } else {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530223 if (dev->rxnumevt) { /* disable FIFO */
224 switch (dev->version) {
225 case MCASP_VERSION_3:
226 mcasp_clr_bits(dev->base + MCASP_VER3_RFIFOCTL,
Chaithrika U S539d3d82009-09-23 10:12:08 -0400227 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530228 break;
229
230 default:
231 mcasp_clr_bits(dev->base +
232 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
233 }
234 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400235 mcasp_stop_rx(dev);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400236 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400237}
238
239static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
240 unsigned int fmt)
241{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400243 void __iomem *base = dev->base;
244
Daniel Mack5296cf22012-10-04 15:08:42 +0200245 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
246 case SND_SOC_DAIFMT_DSP_B:
247 case SND_SOC_DAIFMT_AC97:
248 mcasp_clr_bits(dev->base + DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
249 mcasp_clr_bits(dev->base + DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
250 break;
251 default:
252 /* configure a full-word SYNC pulse (LRCLK) */
253 mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
254 mcasp_set_bits(dev->base + DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
255
256 /* make 1st data bit occur one ACLK cycle after the frame sync */
257 mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMT_REG, FSXDLY(1));
258 mcasp_set_bits(dev->base + DAVINCI_MCASP_RXFMT_REG, FSRDLY(1));
259 break;
260 }
261
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400262 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
263 case SND_SOC_DAIFMT_CBS_CFS:
264 /* codec is clock and frame slave */
265 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
266 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
267
268 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
269 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
270
Marek Belisko81ee6832013-04-26 14:38:11 +0200271 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
272 ACLKX | ACLKR);
273 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
274 AFSX | AFSR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400275 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400276 case SND_SOC_DAIFMT_CBM_CFS:
277 /* codec is clock master and frame slave */
Ben Gardinera90f5492011-04-21 14:19:03 -0400278 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400279 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
280
Ben Gardinera90f5492011-04-21 14:19:03 -0400281 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400282 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
283
Ben Gardinerdb92f432011-04-21 14:19:04 -0400284 mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
285 ACLKX | ACLKR);
Ben Gardiner9595c8f2011-04-21 14:19:02 -0400286 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
Ben Gardinerdb92f432011-04-21 14:19:04 -0400287 AFSX | AFSR);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400288 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400289 case SND_SOC_DAIFMT_CBM_CFM:
290 /* codec is clock and frame master */
291 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
292 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
293
294 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
295 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
296
Ben Gardiner9595c8f2011-04-21 14:19:02 -0400297 mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
298 ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400299 break;
300
301 default:
302 return -EINVAL;
303 }
304
305 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
306 case SND_SOC_DAIFMT_IB_NF:
307 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
308 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
309
310 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
311 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
312 break;
313
314 case SND_SOC_DAIFMT_NB_IF:
315 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
316 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
317
318 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
319 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
320 break;
321
322 case SND_SOC_DAIFMT_IB_IF:
323 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
324 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
325
326 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
327 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
328 break;
329
330 case SND_SOC_DAIFMT_NB_NF:
331 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
332 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
333
Marek Beliskodf4a4ee2013-05-03 07:37:36 +0200334 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400335 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
336 break;
337
338 default:
339 return -EINVAL;
340 }
341
342 return 0;
343}
344
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200345static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
346{
347 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(dai);
348
349 switch (div_id) {
350 case 0: /* MCLK divider */
351 mcasp_mod_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG,
352 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
353 mcasp_mod_bits(dev->base + DAVINCI_MCASP_AHCLKRCTL_REG,
354 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
355 break;
356
357 case 1: /* BCLK divider */
358 mcasp_mod_bits(dev->base + DAVINCI_MCASP_ACLKXCTL_REG,
359 ACLKXDIV(div - 1), ACLKXDIV_MASK);
360 mcasp_mod_bits(dev->base + DAVINCI_MCASP_ACLKRCTL_REG,
361 ACLKRDIV(div - 1), ACLKRDIV_MASK);
362 break;
363
Daniel Mack1b3bc062012-12-05 18:20:38 +0100364 case 2: /* BCLK/LRCLK ratio */
365 dev->bclk_lrclk_ratio = div;
366 break;
367
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200368 default:
369 return -EINVAL;
370 }
371
372 return 0;
373}
374
Daniel Mack5b66aa22012-10-04 15:08:41 +0200375static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
376 unsigned int freq, int dir)
377{
378 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(dai);
379
380 if (dir == SND_SOC_CLOCK_OUT) {
381 mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
382 mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
383 mcasp_set_bits(dev->base + DAVINCI_MCASP_PDIR_REG, AHCLKX);
384 } else {
385 mcasp_clr_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
386 mcasp_clr_bits(dev->base + DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
387 mcasp_clr_bits(dev->base + DAVINCI_MCASP_PDIR_REG, AHCLKX);
388 }
389
390 return 0;
391}
392
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400393static int davinci_config_channel_size(struct davinci_audio_dev *dev,
Daniel Mackba764b32012-12-05 18:20:37 +0100394 int word_length)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400395{
Daniel Mackba764b32012-12-05 18:20:37 +0100396 u32 fmt;
Daniel Mack79671892013-05-16 15:25:01 +0200397 u32 tx_rotate = (word_length / 4) & 0x7;
398 u32 rx_rotate = (32 - word_length) / 4;
Daniel Mackba764b32012-12-05 18:20:37 +0100399 u32 mask = (1ULL << word_length) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400400
Daniel Mack1b3bc062012-12-05 18:20:38 +0100401 /*
402 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
403 * callback, take it into account here. That allows us to for example
404 * send 32 bits per channel to the codec, while only 16 of them carry
405 * audio payload.
Michal Bachratyd486fea2013-04-19 15:28:44 +0200406 * The clock ratio is given for a full period of data (for I2S format
407 * both left and right channels), so it has to be divided by number of
408 * tdm-slots (for I2S - divided by 2).
Daniel Mack1b3bc062012-12-05 18:20:38 +0100409 */
410 if (dev->bclk_lrclk_ratio)
Michal Bachratyd486fea2013-04-19 15:28:44 +0200411 word_length = dev->bclk_lrclk_ratio / dev->tdm_slots;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100412
Daniel Mackba764b32012-12-05 18:20:37 +0100413 /* mapping of the XSSZ bit-field as described in the datasheet */
414 fmt = (word_length >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400415
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200416 if (dev->op_mode != DAVINCI_MCASP_DIT_MODE) {
417 mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMT_REG,
418 RXSSZ(fmt), RXSSZ(0x0F));
419 mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMT_REG,
420 TXSSZ(fmt), TXSSZ(0x0F));
421 mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMT_REG,
Daniel Mack79671892013-05-16 15:25:01 +0200422 TXROT(tx_rotate), TXROT(7));
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200423 mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMT_REG,
Daniel Mack79671892013-05-16 15:25:01 +0200424 RXROT(rx_rotate), RXROT(7));
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200425 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXMASK_REG,
426 mask);
427 }
428
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400429 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400430
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400431 return 0;
432}
433
Michal Bachraty2952b272013-02-28 16:07:08 +0100434static int davinci_hw_common_param(struct davinci_audio_dev *dev, int stream,
435 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400436{
437 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400438 u8 tx_ser = 0;
439 u8 rx_ser = 0;
Michal Bachraty2952b272013-02-28 16:07:08 +0100440 u8 ser;
441 u8 slots = dev->tdm_slots;
442 u8 max_active_serializers = (channels + slots - 1) / slots;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400443 /* Default configuration */
444 mcasp_set_bits(dev->base + DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
445
446 /* All PINS as McASP */
447 mcasp_set_reg(dev->base + DAVINCI_MCASP_PFUNC_REG, 0x00000000);
448
449 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
450 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
451 mcasp_clr_bits(dev->base + DAVINCI_MCASP_XEVTCTL_REG,
452 TXDATADMADIS);
453 } else {
454 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
455 mcasp_clr_bits(dev->base + DAVINCI_MCASP_REVTCTL_REG,
456 RXDATADMADIS);
457 }
458
459 for (i = 0; i < dev->num_serializer; i++) {
460 mcasp_set_bits(dev->base + DAVINCI_MCASP_XRSRCTL_REG(i),
461 dev->serial_dir[i]);
Michal Bachraty2952b272013-02-28 16:07:08 +0100462 if (dev->serial_dir[i] == TX_MODE &&
463 tx_ser < max_active_serializers) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400464 mcasp_set_bits(dev->base + DAVINCI_MCASP_PDIR_REG,
465 AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400466 tx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100467 } else if (dev->serial_dir[i] == RX_MODE &&
468 rx_ser < max_active_serializers) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400469 mcasp_clr_bits(dev->base + DAVINCI_MCASP_PDIR_REG,
470 AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400471 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100472 } else {
473 mcasp_mod_bits(dev->base + DAVINCI_MCASP_XRSRCTL_REG(i),
474 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400475 }
476 }
477
Daniel Mackecf327c2013-03-08 14:19:38 +0100478 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
479 ser = tx_ser;
480 else
481 ser = rx_ser;
482
483 if (ser < max_active_serializers) {
484 dev_warn(dev->dev, "stream has more channels (%d) than are "
485 "enabled in mcasp (%d)\n", channels, ser * slots);
486 return -EINVAL;
487 }
488
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400489 if (dev->txnumevt && stream == SNDRV_PCM_STREAM_PLAYBACK) {
490 if (dev->txnumevt * tx_ser > 64)
491 dev->txnumevt = 1;
492
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530493 switch (dev->version) {
494 case MCASP_VERSION_3:
495 mcasp_mod_bits(dev->base + MCASP_VER3_WFIFOCTL, tx_ser,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400496 NUMDMA_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530497 mcasp_mod_bits(dev->base + MCASP_VER3_WFIFOCTL,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400498 ((dev->txnumevt * tx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530499 break;
500 default:
501 mcasp_mod_bits(dev->base + DAVINCI_MCASP_WFIFOCTL,
502 tx_ser, NUMDMA_MASK);
503 mcasp_mod_bits(dev->base + DAVINCI_MCASP_WFIFOCTL,
504 ((dev->txnumevt * tx_ser) << 8), NUMEVT_MASK);
505 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400506 }
507
508 if (dev->rxnumevt && stream == SNDRV_PCM_STREAM_CAPTURE) {
509 if (dev->rxnumevt * rx_ser > 64)
510 dev->rxnumevt = 1;
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530511 switch (dev->version) {
512 case MCASP_VERSION_3:
513 mcasp_mod_bits(dev->base + MCASP_VER3_RFIFOCTL, rx_ser,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400514 NUMDMA_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530515 mcasp_mod_bits(dev->base + MCASP_VER3_RFIFOCTL,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400516 ((dev->rxnumevt * rx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530517 break;
518 default:
519 mcasp_mod_bits(dev->base + DAVINCI_MCASP_RFIFOCTL,
520 rx_ser, NUMDMA_MASK);
521 mcasp_mod_bits(dev->base + DAVINCI_MCASP_RFIFOCTL,
522 ((dev->rxnumevt * rx_ser) << 8), NUMEVT_MASK);
523 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400524 }
Michal Bachraty2952b272013-02-28 16:07:08 +0100525
526 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400527}
528
529static void davinci_hw_param(struct davinci_audio_dev *dev, int stream)
530{
531 int i, active_slots;
532 u32 mask = 0;
533
534 active_slots = (dev->tdm_slots > 31) ? 32 : dev->tdm_slots;
535 for (i = 0; i < active_slots; i++)
536 mask |= (1 << i);
537
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400538 mcasp_clr_bits(dev->base + DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
539
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400540 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
541 /* bit stream is MSB first with no delay */
542 /* DSP_B mode */
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400543 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXTDM_REG, mask);
544 mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMT_REG, TXORD);
545
Ben Gardiner049cfaa2011-04-21 14:19:01 -0400546 if ((dev->tdm_slots >= 2) && (dev->tdm_slots <= 32))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400547 mcasp_mod_bits(dev->base + DAVINCI_MCASP_TXFMCTL_REG,
548 FSXMOD(dev->tdm_slots), FSXMOD(0x1FF));
549 else
550 printk(KERN_ERR "playback tdm slot %d not supported\n",
551 dev->tdm_slots);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400552 } else {
553 /* bit stream is MSB first with no delay */
554 /* DSP_B mode */
555 mcasp_set_bits(dev->base + DAVINCI_MCASP_RXFMT_REG, RXORD);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400556 mcasp_set_reg(dev->base + DAVINCI_MCASP_RXTDM_REG, mask);
557
Ben Gardiner049cfaa2011-04-21 14:19:01 -0400558 if ((dev->tdm_slots >= 2) && (dev->tdm_slots <= 32))
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400559 mcasp_mod_bits(dev->base + DAVINCI_MCASP_RXFMCTL_REG,
560 FSRMOD(dev->tdm_slots), FSRMOD(0x1FF));
561 else
562 printk(KERN_ERR "capture tdm slot %d not supported\n",
563 dev->tdm_slots);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400564 }
565}
566
567/* S/PDIF */
568static void davinci_hw_dit_param(struct davinci_audio_dev *dev)
569{
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400570 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
571 and LSB first */
572 mcasp_set_bits(dev->base + DAVINCI_MCASP_TXFMT_REG,
573 TXROT(6) | TXSSZ(15));
574
575 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
576 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXFMCTL_REG,
577 AFSXE | FSXMOD(0x180));
578
579 /* Set the TX tdm : for all the slots */
580 mcasp_set_reg(dev->base + DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
581
582 /* Set the TX clock controls : div = 1 and internal */
583 mcasp_set_bits(dev->base + DAVINCI_MCASP_ACLKXCTL_REG,
584 ACLKXE | TX_ASYNC);
585
586 mcasp_clr_bits(dev->base + DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
587
588 /* Only 44100 and 48000 are valid, both have the same setting */
589 mcasp_set_bits(dev->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
590
591 /* Enable the DIT */
592 mcasp_set_bits(dev->base + DAVINCI_MCASP_TXDITCTL_REG, DITEN);
593}
594
595static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
596 struct snd_pcm_hw_params *params,
597 struct snd_soc_dai *cpu_dai)
598{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400600 struct davinci_pcm_dma_params *dma_params =
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700601 &dev->dma_params[substream->stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400602 int word_length;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400603 u8 fifo_level;
Michal Bachraty2952b272013-02-28 16:07:08 +0100604 u8 slots = dev->tdm_slots;
Michal Bachraty7c21a782013-04-19 15:28:03 +0200605 u8 active_serializers;
Michal Bachraty2952b272013-02-28 16:07:08 +0100606 int channels;
607 struct snd_interval *pcm_channels = hw_param_interval(params,
608 SNDRV_PCM_HW_PARAM_CHANNELS);
609 channels = pcm_channels->min;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400610
Michal Bachraty7c21a782013-04-19 15:28:03 +0200611 active_serializers = (channels + slots - 1) / slots;
612
Michal Bachraty2952b272013-02-28 16:07:08 +0100613 if (davinci_hw_common_param(dev, substream->stream, channels) == -EINVAL)
614 return -EINVAL;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400615 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Michal Bachraty7c21a782013-04-19 15:28:03 +0200616 fifo_level = dev->txnumevt * active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400617 else
Michal Bachraty7c21a782013-04-19 15:28:03 +0200618 fifo_level = dev->rxnumevt * active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400619
620 if (dev->op_mode == DAVINCI_MCASP_DIT_MODE)
621 davinci_hw_dit_param(dev);
622 else
623 davinci_hw_param(dev, substream->stream);
624
625 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400626 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400627 case SNDRV_PCM_FORMAT_S8:
628 dma_params->data_type = 1;
Daniel Mackba764b32012-12-05 18:20:37 +0100629 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400630 break;
631
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400632 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400633 case SNDRV_PCM_FORMAT_S16_LE:
634 dma_params->data_type = 2;
Daniel Mackba764b32012-12-05 18:20:37 +0100635 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400636 break;
637
Daniel Mack21eb24d2012-10-09 09:35:16 +0200638 case SNDRV_PCM_FORMAT_U24_3LE:
639 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mack21eb24d2012-10-09 09:35:16 +0200640 dma_params->data_type = 3;
Daniel Mackba764b32012-12-05 18:20:37 +0100641 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +0200642 break;
643
Daniel Mack6b7fa012012-10-09 11:56:40 +0200644 case SNDRV_PCM_FORMAT_U24_LE:
645 case SNDRV_PCM_FORMAT_S24_LE:
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400646 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400647 case SNDRV_PCM_FORMAT_S32_LE:
648 dma_params->data_type = 4;
Daniel Mackba764b32012-12-05 18:20:37 +0100649 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400650 break;
651
652 default:
653 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
654 return -EINVAL;
655 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400656
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400657 if (dev->version == MCASP_VERSION_2 && !fifo_level)
658 dma_params->acnt = 4;
659 else
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400660 dma_params->acnt = dma_params->data_type;
661
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400662 dma_params->fifo_level = fifo_level;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400663 davinci_config_channel_size(dev, word_length);
664
665 return 0;
666}
667
668static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
669 int cmd, struct snd_soc_dai *cpu_dai)
670{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400672 int ret = 0;
673
674 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400675 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +0530676 case SNDRV_PCM_TRIGGER_START:
677 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530678 ret = pm_runtime_get_sync(dev->dev);
679 if (IS_ERR_VALUE(ret))
680 dev_err(dev->dev, "pm_runtime_get_sync() failed\n");
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400681 davinci_mcasp_start(dev, substream->stream);
682 break;
683
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400684 case SNDRV_PCM_TRIGGER_SUSPEND:
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530685 davinci_mcasp_stop(dev, substream->stream);
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530686 ret = pm_runtime_put_sync(dev->dev);
687 if (IS_ERR_VALUE(ret))
688 dev_err(dev->dev, "pm_runtime_put_sync() failed\n");
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530689 break;
690
691 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400692 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
693 davinci_mcasp_stop(dev, substream->stream);
694 break;
695
696 default:
697 ret = -EINVAL;
698 }
699
700 return ret;
701}
702
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000703static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
704 struct snd_soc_dai *dai)
705{
706 struct davinci_audio_dev *dev = snd_soc_dai_get_drvdata(dai);
707
708 snd_soc_dai_set_dma_data(dai, substream, dev->dma_params);
709 return 0;
710}
711
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100712static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000713 .startup = davinci_mcasp_startup,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400714 .trigger = davinci_mcasp_trigger,
715 .hw_params = davinci_mcasp_hw_params,
716 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200717 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +0200718 .set_sysclk = davinci_mcasp_set_sysclk,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400719};
720
Peter Ujfalusied29cd52013-11-14 11:35:22 +0200721#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
722
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400723#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
724 SNDRV_PCM_FMTBIT_U8 | \
725 SNDRV_PCM_FMTBIT_S16_LE | \
726 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +0200727 SNDRV_PCM_FMTBIT_S24_LE | \
728 SNDRV_PCM_FMTBIT_U24_LE | \
729 SNDRV_PCM_FMTBIT_S24_3LE | \
730 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400731 SNDRV_PCM_FMTBIT_S32_LE | \
732 SNDRV_PCM_FMTBIT_U32_LE)
733
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400735 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 .name = "davinci-mcasp.0",
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400737 .playback = {
738 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100739 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400740 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400741 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400742 },
743 .capture = {
744 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100745 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400746 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400747 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400748 },
749 .ops = &davinci_mcasp_dai_ops,
750
751 },
752 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +0200753 .name = "davinci-mcasp.1",
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400754 .playback = {
755 .channels_min = 1,
756 .channels_max = 384,
757 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400758 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400759 },
760 .ops = &davinci_mcasp_dai_ops,
761 },
762
763};
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400764
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -0700765static const struct snd_soc_component_driver davinci_mcasp_component = {
766 .name = "davinci-mcasp",
767};
768
Jyri Sarha256ba182013-10-18 18:37:42 +0300769/* Some HW specific values and defaults. The rest is filled in from DT. */
770static struct snd_platform_data dm646x_mcasp_pdata = {
771 .tx_dma_offset = 0x400,
772 .rx_dma_offset = 0x400,
773 .asp_chan_q = EVENTQ_0,
774 .version = MCASP_VERSION_1,
775};
776
777static struct snd_platform_data da830_mcasp_pdata = {
778 .tx_dma_offset = 0x2000,
779 .rx_dma_offset = 0x2000,
780 .asp_chan_q = EVENTQ_0,
781 .version = MCASP_VERSION_2,
782};
783
784static struct snd_platform_data omap2_mcasp_pdata = {
785 .tx_dma_offset = 0,
786 .rx_dma_offset = 0,
787 .asp_chan_q = EVENTQ_0,
788 .version = MCASP_VERSION_3,
789};
790
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530791static const struct of_device_id mcasp_dt_ids[] = {
792 {
793 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300794 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530795 },
796 {
797 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300798 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530799 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530800 {
Jyri Sarha3af9e032013-10-18 18:37:44 +0300801 .compatible = "ti,am33xx-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300802 .data = &omap2_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530803 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530804 { /* sentinel */ }
805};
806MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
807
808static struct snd_platform_data *davinci_mcasp_set_pdata_from_of(
809 struct platform_device *pdev)
810{
811 struct device_node *np = pdev->dev.of_node;
812 struct snd_platform_data *pdata = NULL;
813 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +0530814 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +0300815 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530816
817 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530818 u32 val;
819 int i, ret = 0;
820
821 if (pdev->dev.platform_data) {
822 pdata = pdev->dev.platform_data;
823 return pdata;
824 } else if (match) {
Jyri Sarha256ba182013-10-18 18:37:42 +0300825 pdata = (struct snd_platform_data *) match->data;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530826 } else {
827 /* control shouldn't reach here. something is wrong */
828 ret = -EINVAL;
829 goto nodata;
830 }
831
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530832 ret = of_property_read_u32(np, "op-mode", &val);
833 if (ret >= 0)
834 pdata->op_mode = val;
835
836 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +0100837 if (ret >= 0) {
838 if (val < 2 || val > 32) {
839 dev_err(&pdev->dev,
840 "tdm-slots must be in rage [2-32]\n");
841 ret = -EINVAL;
842 goto nodata;
843 }
844
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530845 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +0100846 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530847
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530848 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
849 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530850 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300851 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
852 (sizeof(*of_serial_dir) * val),
853 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530854 if (!of_serial_dir) {
855 ret = -ENOMEM;
856 goto nodata;
857 }
858
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300859 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530860 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
861
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300862 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530863 pdata->serial_dir = of_serial_dir;
864 }
865
Jyri Sarha4023fe62013-10-18 18:37:43 +0300866 ret = of_property_match_string(np, "dma-names", "tx");
867 if (ret < 0)
868 goto nodata;
869
870 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
871 &dma_spec);
872 if (ret < 0)
873 goto nodata;
874
875 pdata->tx_dma_channel = dma_spec.args[0];
876
877 ret = of_property_match_string(np, "dma-names", "rx");
878 if (ret < 0)
879 goto nodata;
880
881 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
882 &dma_spec);
883 if (ret < 0)
884 goto nodata;
885
886 pdata->rx_dma_channel = dma_spec.args[0];
887
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530888 ret = of_property_read_u32(np, "tx-num-evt", &val);
889 if (ret >= 0)
890 pdata->txnumevt = val;
891
892 ret = of_property_read_u32(np, "rx-num-evt", &val);
893 if (ret >= 0)
894 pdata->rxnumevt = val;
895
896 ret = of_property_read_u32(np, "sram-size-playback", &val);
897 if (ret >= 0)
898 pdata->sram_size_playback = val;
899
900 ret = of_property_read_u32(np, "sram-size-capture", &val);
901 if (ret >= 0)
902 pdata->sram_size_capture = val;
903
904 return pdata;
905
906nodata:
907 if (ret < 0) {
908 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
909 ret);
910 pdata = NULL;
911 }
912 return pdata;
913}
914
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400915static int davinci_mcasp_probe(struct platform_device *pdev)
916{
917 struct davinci_pcm_dma_params *dma_data;
Jyri Sarha256ba182013-10-18 18:37:42 +0300918 struct resource *mem, *ioarea, *res, *dat;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400919 struct snd_platform_data *pdata;
920 struct davinci_audio_dev *dev;
Julia Lawall96d31e22011-12-29 17:51:21 +0100921 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400922
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530923 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
924 dev_err(&pdev->dev, "No platform data supplied\n");
925 return -EINVAL;
926 }
927
Julia Lawall96d31e22011-12-29 17:51:21 +0100928 dev = devm_kzalloc(&pdev->dev, sizeof(struct davinci_audio_dev),
929 GFP_KERNEL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400930 if (!dev)
931 return -ENOMEM;
932
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530933 pdata = davinci_mcasp_set_pdata_from_of(pdev);
934 if (!pdata) {
935 dev_err(&pdev->dev, "no platform data\n");
936 return -EINVAL;
937 }
938
Jyri Sarha256ba182013-10-18 18:37:42 +0300939 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400940 if (!mem) {
Jyri Sarha256ba182013-10-18 18:37:42 +0300941 dev_warn(dev->dev,
942 "\"mpu\" mem resource not found, using index 0\n");
943 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
944 if (!mem) {
945 dev_err(&pdev->dev, "no mem resource?\n");
946 return -ENODEV;
947 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400948 }
949
Julia Lawall96d31e22011-12-29 17:51:21 +0100950 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
Vaibhav Bediad852f4462011-02-09 18:39:52 +0530951 resource_size(mem), pdev->name);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400952 if (!ioarea) {
953 dev_err(&pdev->dev, "Audio region already claimed\n");
Julia Lawall96d31e22011-12-29 17:51:21 +0100954 return -EBUSY;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400955 }
956
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530957 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400958
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530959 ret = pm_runtime_get_sync(&pdev->dev);
960 if (IS_ERR_VALUE(ret)) {
961 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
962 return ret;
963 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400964
Julia Lawall96d31e22011-12-29 17:51:21 +0100965 dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
Vaibhav Bedia4f82f022011-02-09 18:39:54 +0530966 if (!dev->base) {
967 dev_err(&pdev->dev, "ioremap failed\n");
968 ret = -ENOMEM;
969 goto err_release_clk;
970 }
971
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400972 dev->op_mode = pdata->op_mode;
973 dev->tdm_slots = pdata->tdm_slots;
974 dev->num_serializer = pdata->num_serializer;
975 dev->serial_dir = pdata->serial_dir;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400976 dev->version = pdata->version;
977 dev->txnumevt = pdata->txnumevt;
978 dev->rxnumevt = pdata->rxnumevt;
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530979 dev->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400980
Jyri Sarha256ba182013-10-18 18:37:42 +0300981 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
982 if (!dat)
983 dat = mem;
984
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700985 dma_data = &dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK];
Sekhar Nori48519f02010-07-19 12:31:16 +0530986 dma_data->asp_chan_q = pdata->asp_chan_q;
987 dma_data->ram_chan_q = pdata->ram_chan_q;
Matt Porterb8ec56d2012-10-17 16:08:03 +0200988 dma_data->sram_pool = pdata->sram_pool;
Ben Gardinera0c83262011-05-18 09:27:45 -0400989 dma_data->sram_size = pdata->sram_size_playback;
Jyri Sarha256ba182013-10-18 18:37:42 +0300990 dma_data->dma_addr = dat->start + pdata->tx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400991
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400992 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +0300993 if (res)
994 dma_data->channel = res->start;
995 else
996 dma_data->channel = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700997
998 dma_data = &dev->dma_params[SNDRV_PCM_STREAM_CAPTURE];
Sekhar Nori48519f02010-07-19 12:31:16 +0530999 dma_data->asp_chan_q = pdata->asp_chan_q;
1000 dma_data->ram_chan_q = pdata->ram_chan_q;
Matt Porterb8ec56d2012-10-17 16:08:03 +02001001 dma_data->sram_pool = pdata->sram_pool;
Ben Gardinera0c83262011-05-18 09:27:45 -04001002 dma_data->sram_size = pdata->sram_size_capture;
Jyri Sarha256ba182013-10-18 18:37:42 +03001003 dma_data->dma_addr = dat->start + pdata->rx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001004
1005 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001006 if (res)
1007 dma_data->channel = res->start;
1008 else
1009 dma_data->channel = pdata->rx_dma_channel;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001010
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 dev_set_drvdata(&pdev->dev, dev);
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001012 ret = snd_soc_register_component(&pdev->dev, &davinci_mcasp_component,
1013 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001014
1015 if (ret != 0)
Julia Lawall96d31e22011-12-29 17:51:21 +01001016 goto err_release_clk;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301017
1018 ret = davinci_soc_platform_register(&pdev->dev);
1019 if (ret) {
1020 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001021 goto err_unregister_component;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301022 }
1023
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001024 return 0;
1025
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001026err_unregister_component:
1027 snd_soc_unregister_component(&pdev->dev);
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +05301028err_release_clk:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301029 pm_runtime_put_sync(&pdev->dev);
1030 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001031 return ret;
1032}
1033
1034static int davinci_mcasp_remove(struct platform_device *pdev)
1035{
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001036
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001037 snd_soc_unregister_component(&pdev->dev);
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301038 davinci_soc_platform_unregister(&pdev->dev);
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301039
1040 pm_runtime_put_sync(&pdev->dev);
1041 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001042
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001043 return 0;
1044}
1045
Daniel Macka85e4192013-10-01 14:50:02 +02001046#ifdef CONFIG_PM_SLEEP
1047static int davinci_mcasp_suspend(struct device *dev)
1048{
1049 struct davinci_audio_dev *a = dev_get_drvdata(dev);
1050 void __iomem *base = a->base;
1051
1052 a->context.txfmtctl = mcasp_get_reg(base + DAVINCI_MCASP_TXFMCTL_REG);
1053 a->context.rxfmtctl = mcasp_get_reg(base + DAVINCI_MCASP_RXFMCTL_REG);
1054 a->context.txfmt = mcasp_get_reg(base + DAVINCI_MCASP_TXFMT_REG);
1055 a->context.rxfmt = mcasp_get_reg(base + DAVINCI_MCASP_RXFMT_REG);
1056 a->context.aclkxctl = mcasp_get_reg(base + DAVINCI_MCASP_ACLKXCTL_REG);
1057 a->context.aclkrctl = mcasp_get_reg(base + DAVINCI_MCASP_ACLKRCTL_REG);
1058 a->context.pdir = mcasp_get_reg(base + DAVINCI_MCASP_PDIR_REG);
1059
1060 return 0;
1061}
1062
1063static int davinci_mcasp_resume(struct device *dev)
1064{
1065 struct davinci_audio_dev *a = dev_get_drvdata(dev);
1066 void __iomem *base = a->base;
1067
1068 mcasp_set_reg(base + DAVINCI_MCASP_TXFMCTL_REG, a->context.txfmtctl);
1069 mcasp_set_reg(base + DAVINCI_MCASP_RXFMCTL_REG, a->context.rxfmtctl);
1070 mcasp_set_reg(base + DAVINCI_MCASP_TXFMT_REG, a->context.txfmt);
1071 mcasp_set_reg(base + DAVINCI_MCASP_RXFMT_REG, a->context.rxfmt);
1072 mcasp_set_reg(base + DAVINCI_MCASP_ACLKXCTL_REG, a->context.aclkxctl);
1073 mcasp_set_reg(base + DAVINCI_MCASP_ACLKRCTL_REG, a->context.aclkrctl);
1074 mcasp_set_reg(base + DAVINCI_MCASP_PDIR_REG, a->context.pdir);
1075
1076 return 0;
1077}
1078#endif
1079
1080SIMPLE_DEV_PM_OPS(davinci_mcasp_pm_ops,
1081 davinci_mcasp_suspend,
1082 davinci_mcasp_resume);
1083
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001084static struct platform_driver davinci_mcasp_driver = {
1085 .probe = davinci_mcasp_probe,
1086 .remove = davinci_mcasp_remove,
1087 .driver = {
1088 .name = "davinci-mcasp",
1089 .owner = THIS_MODULE,
Daniel Macka85e4192013-10-01 14:50:02 +02001090 .pm = &davinci_mcasp_pm_ops,
Sachin Kamatea421eb2013-05-22 16:53:37 +05301091 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001092 },
1093};
1094
Axel Linf9b8a512011-11-25 10:09:27 +08001095module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001096
1097MODULE_AUTHOR("Steve Chen");
1098MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1099MODULE_LICENSE("GPL");