blob: bd85c98bf5a5513813898ae3ff95518fd0c0abc0 [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
Peter Ujfalusi70091a32013-11-14 11:35:29 +020038struct davinci_mcasp {
Peter Ujfalusi21400a72013-11-14 11:35:26 +020039 struct davinci_pcm_dma_params dma_params[2];
40 void __iomem *base;
41 struct device *dev;
42
43 /* McASP specific data */
44 int tdm_slots;
45 u8 op_mode;
46 u8 num_serializer;
47 u8 *serial_dir;
48 u8 version;
49 u16 bclk_lrclk_ratio;
50
51 /* McASP FIFO related */
52 u8 txnumevt;
53 u8 rxnumevt;
54
55#ifdef CONFIG_PM_SLEEP
56 struct {
57 u32 txfmtctl;
58 u32 rxfmtctl;
59 u32 txfmt;
60 u32 rxfmt;
61 u32 aclkxctl;
62 u32 aclkrctl;
63 u32 pdir;
64 } context;
65#endif
66};
67
Chaithrika U Sb67f4482009-06-05 06:28:40 -040068static inline void mcasp_set_bits(void __iomem *reg, u32 val)
69{
70 __raw_writel(__raw_readl(reg) | val, reg);
71}
72
73static inline void mcasp_clr_bits(void __iomem *reg, u32 val)
74{
75 __raw_writel((__raw_readl(reg) & ~(val)), reg);
76}
77
78static inline void mcasp_mod_bits(void __iomem *reg, u32 val, u32 mask)
79{
80 __raw_writel((__raw_readl(reg) & ~mask) | val, reg);
81}
82
83static inline void mcasp_set_reg(void __iomem *reg, u32 val)
84{
85 __raw_writel(val, reg);
86}
87
88static inline u32 mcasp_get_reg(void __iomem *reg)
89{
90 return (unsigned int)__raw_readl(reg);
91}
92
Peter Ujfalusieba0ecf2013-11-14 11:35:28 +020093static void mcasp_set_ctl_reg(void __iomem *regs, u32 val)
Chaithrika U Sb67f4482009-06-05 06:28:40 -040094{
95 int i = 0;
96
97 mcasp_set_bits(regs, val);
98
99 /* programming GBLCTL needs to read back from GBLCTL and verfiy */
100 /* loop count is to avoid the lock-up */
101 for (i = 0; i < 1000; i++) {
102 if ((mcasp_get_reg(regs) & val) == val)
103 break;
104 }
105
106 if (i == 1000 && ((mcasp_get_reg(regs) & val) != val))
107 printk(KERN_ERR "GBLCTL write error\n");
108}
109
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200110static void mcasp_start_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400111{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200112 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXHCLKRST);
113 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXCLKRST);
114 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXSERCLR);
115 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXBUF_REG, 0);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400116
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200117 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
118 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
119 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXBUF_REG, 0);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400120
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200121 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXSMRST);
122 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, RXFSRST);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400123}
124
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200125static void mcasp_start_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400126{
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400127 u8 offset = 0, i;
128 u32 cnt;
129
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200130 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, TXHCLKRST);
131 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, TXCLKRST);
132 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, TXSERCLR);
133 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXBUF_REG, 0);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400134
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200135 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, TXSMRST);
136 mcasp_set_ctl_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, TXFSRST);
137 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXBUF_REG, 0);
138 for (i = 0; i < mcasp->num_serializer; i++) {
139 if (mcasp->serial_dir[i] == TX_MODE) {
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400140 offset = i;
141 break;
142 }
143 }
144
145 /* wait for TX ready */
146 cnt = 0;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200147 while (!(mcasp_get_reg(mcasp->base + DAVINCI_MCASP_XRSRCTL_REG(offset)) &
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400148 TXSTATE) && (cnt < 100000))
149 cnt++;
150
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200151 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXBUF_REG, 0);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400152}
153
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200154static void davinci_mcasp_start(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400155{
Chaithrika U S539d3d82009-09-23 10:12:08 -0400156 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200157 if (mcasp->txnumevt) { /* enable FIFO */
158 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530159 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200160 mcasp_clr_bits(mcasp->base + MCASP_VER3_WFIFOCTL,
161 FIFO_ENABLE);
162 mcasp_set_bits(mcasp->base + MCASP_VER3_WFIFOCTL,
163 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530164 break;
165 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200166 mcasp_clr_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530167 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200168 mcasp_set_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530169 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
170 }
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530171 }
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200172 mcasp_start_tx(mcasp);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400173 } else {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200174 if (mcasp->rxnumevt) { /* enable FIFO */
175 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530176 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200177 mcasp_clr_bits(mcasp->base + MCASP_VER3_RFIFOCTL,
178 FIFO_ENABLE);
179 mcasp_set_bits(mcasp->base + MCASP_VER3_RFIFOCTL,
180 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530181 break;
182 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200183 mcasp_clr_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530184 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200185 mcasp_set_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530186 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
187 }
Vaibhav Bedia0d624272012-08-08 20:40:31 +0530188 }
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200189 mcasp_start_rx(mcasp);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400190 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400191}
192
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200193static void mcasp_stop_rx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400194{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200195 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_GBLCTLR_REG, 0);
196 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400197}
198
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200199static void mcasp_stop_tx(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400200{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200201 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_GBLCTLX_REG, 0);
202 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400203}
204
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200205static void davinci_mcasp_stop(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400206{
Chaithrika U S539d3d82009-09-23 10:12:08 -0400207 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200208 if (mcasp->txnumevt) { /* disable FIFO */
209 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530210 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200211 mcasp_clr_bits(mcasp->base + MCASP_VER3_WFIFOCTL,
212 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530213 break;
214 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200215 mcasp_clr_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530216 DAVINCI_MCASP_WFIFOCTL, FIFO_ENABLE);
217 }
218 }
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200219 mcasp_stop_tx(mcasp);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400220 } else {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200221 if (mcasp->rxnumevt) { /* disable FIFO */
222 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530223 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200224 mcasp_clr_bits(mcasp->base + MCASP_VER3_RFIFOCTL,
225 FIFO_ENABLE);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530226 break;
227
228 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200229 mcasp_clr_bits(mcasp->base +
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530230 DAVINCI_MCASP_RFIFOCTL, FIFO_ENABLE);
231 }
232 }
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200233 mcasp_stop_rx(mcasp);
Chaithrika U S539d3d82009-09-23 10:12:08 -0400234 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400235}
236
237static int davinci_mcasp_set_dai_fmt(struct snd_soc_dai *cpu_dai,
238 unsigned int fmt)
239{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200240 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
241 void __iomem *base = mcasp->base;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400242
Daniel Mack5296cf22012-10-04 15:08:42 +0200243 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
244 case SND_SOC_DAIFMT_DSP_B:
245 case SND_SOC_DAIFMT_AC97:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200246 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
247 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Daniel Mack5296cf22012-10-04 15:08:42 +0200248 break;
249 default:
250 /* configure a full-word SYNC pulse (LRCLK) */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200251 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_TXFMCTL_REG, FSXDUR);
252 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_RXFMCTL_REG, FSRDUR);
Daniel Mack5296cf22012-10-04 15:08:42 +0200253
254 /* make 1st data bit occur one ACLK cycle after the frame sync */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200255 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_TXFMT_REG, FSXDLY(1));
256 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_RXFMT_REG, FSRDLY(1));
Daniel Mack5296cf22012-10-04 15:08:42 +0200257 break;
258 }
259
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400260 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
261 case SND_SOC_DAIFMT_CBS_CFS:
262 /* codec is clock and frame slave */
263 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
264 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
265
266 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
267 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
268
Marek Belisko81ee6832013-04-26 14:38:11 +0200269 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
270 ACLKX | ACLKR);
271 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
272 AFSX | AFSR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400273 break;
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400274 case SND_SOC_DAIFMT_CBM_CFS:
275 /* codec is clock master and frame slave */
Ben Gardinera90f5492011-04-21 14:19:03 -0400276 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400277 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
278
Ben Gardinera90f5492011-04-21 14:19:03 -0400279 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400280 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
281
Ben Gardinerdb92f432011-04-21 14:19:04 -0400282 mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
283 ACLKX | ACLKR);
Ben Gardiner9595c8f2011-04-21 14:19:02 -0400284 mcasp_set_bits(base + DAVINCI_MCASP_PDIR_REG,
Ben Gardinerdb92f432011-04-21 14:19:04 -0400285 AFSX | AFSR);
Chaithrika U S517ee6c2009-08-11 16:59:12 -0400286 break;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400287 case SND_SOC_DAIFMT_CBM_CFM:
288 /* codec is clock and frame master */
289 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXE);
290 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, AFSXE);
291
292 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRE);
293 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, AFSRE);
294
Ben Gardiner9595c8f2011-04-21 14:19:02 -0400295 mcasp_clr_bits(base + DAVINCI_MCASP_PDIR_REG,
296 ACLKX | AHCLKX | AFSX | ACLKR | AHCLKR | AFSR);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400297 break;
298
299 default:
300 return -EINVAL;
301 }
302
303 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
304 case SND_SOC_DAIFMT_IB_NF:
305 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
306 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
307
308 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
309 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
310 break;
311
312 case SND_SOC_DAIFMT_NB_IF:
313 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
314 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
315
316 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
317 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
318 break;
319
320 case SND_SOC_DAIFMT_IB_IF:
321 mcasp_clr_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
322 mcasp_set_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
323
324 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
325 mcasp_set_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
326 break;
327
328 case SND_SOC_DAIFMT_NB_NF:
329 mcasp_set_bits(base + DAVINCI_MCASP_ACLKXCTL_REG, ACLKXPOL);
330 mcasp_clr_bits(base + DAVINCI_MCASP_TXFMCTL_REG, FSXPOL);
331
Marek Beliskodf4a4ee2013-05-03 07:37:36 +0200332 mcasp_set_bits(base + DAVINCI_MCASP_ACLKRCTL_REG, ACLKRPOL);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400333 mcasp_clr_bits(base + DAVINCI_MCASP_RXFMCTL_REG, FSRPOL);
334 break;
335
336 default:
337 return -EINVAL;
338 }
339
340 return 0;
341}
342
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200343static int davinci_mcasp_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
344{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200345 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200346
347 switch (div_id) {
348 case 0: /* MCLK divider */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200349 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_AHCLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200350 AHCLKXDIV(div - 1), AHCLKXDIV_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200351 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_AHCLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200352 AHCLKRDIV(div - 1), AHCLKRDIV_MASK);
353 break;
354
355 case 1: /* BCLK divider */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200356 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_ACLKXCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200357 ACLKXDIV(div - 1), ACLKXDIV_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200358 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_ACLKRCTL_REG,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200359 ACLKRDIV(div - 1), ACLKRDIV_MASK);
360 break;
361
Daniel Mack1b3bc062012-12-05 18:20:38 +0100362 case 2: /* BCLK/LRCLK ratio */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200363 mcasp->bclk_lrclk_ratio = div;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100364 break;
365
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200366 default:
367 return -EINVAL;
368 }
369
370 return 0;
371}
372
Daniel Mack5b66aa22012-10-04 15:08:41 +0200373static int davinci_mcasp_set_sysclk(struct snd_soc_dai *dai, int clk_id,
374 unsigned int freq, int dir)
375{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200376 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200377
378 if (dir == SND_SOC_CLOCK_OUT) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200379 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
380 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
381 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200382 } else {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200383 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXE);
384 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_AHCLKRCTL_REG, AHCLKRE);
385 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_PDIR_REG, AHCLKX);
Daniel Mack5b66aa22012-10-04 15:08:41 +0200386 }
387
388 return 0;
389}
390
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200391static int davinci_config_channel_size(struct davinci_mcasp *mcasp,
Daniel Mackba764b32012-12-05 18:20:37 +0100392 int word_length)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400393{
Daniel Mackba764b32012-12-05 18:20:37 +0100394 u32 fmt;
Daniel Mack79671892013-05-16 15:25:01 +0200395 u32 tx_rotate = (word_length / 4) & 0x7;
396 u32 rx_rotate = (32 - word_length) / 4;
Daniel Mackba764b32012-12-05 18:20:37 +0100397 u32 mask = (1ULL << word_length) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400398
Daniel Mack1b3bc062012-12-05 18:20:38 +0100399 /*
400 * if s BCLK-to-LRCLK ratio has been configured via the set_clkdiv()
401 * callback, take it into account here. That allows us to for example
402 * send 32 bits per channel to the codec, while only 16 of them carry
403 * audio payload.
Michal Bachratyd486fea2013-04-19 15:28:44 +0200404 * The clock ratio is given for a full period of data (for I2S format
405 * both left and right channels), so it has to be divided by number of
406 * tdm-slots (for I2S - divided by 2).
Daniel Mack1b3bc062012-12-05 18:20:38 +0100407 */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200408 if (mcasp->bclk_lrclk_ratio)
409 word_length = mcasp->bclk_lrclk_ratio / mcasp->tdm_slots;
Daniel Mack1b3bc062012-12-05 18:20:38 +0100410
Daniel Mackba764b32012-12-05 18:20:37 +0100411 /* mapping of the XSSZ bit-field as described in the datasheet */
412 fmt = (word_length >> 1) - 1;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400413
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200414 if (mcasp->op_mode != DAVINCI_MCASP_DIT_MODE) {
415 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_RXFMT_REG,
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200416 RXSSZ(fmt), RXSSZ(0x0F));
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200417 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_TXFMT_REG,
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200418 TXSSZ(fmt), TXSSZ(0x0F));
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200419 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_TXFMT_REG,
Daniel Mack79671892013-05-16 15:25:01 +0200420 TXROT(tx_rotate), TXROT(7));
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200421 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_RXFMT_REG,
Daniel Mack79671892013-05-16 15:25:01 +0200422 RXROT(rx_rotate), RXROT(7));
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200423 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXMASK_REG,
Yegor Yefremovf5023af2013-04-04 16:13:20 +0200424 mask);
425 }
426
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200427 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXMASK_REG, mask);
Chaithrika U S0c31cf32009-09-15 18:13:29 -0400428
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400429 return 0;
430}
431
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200432static int davinci_hw_common_param(struct davinci_mcasp *mcasp, int stream,
Michal Bachraty2952b272013-02-28 16:07:08 +0100433 int channels)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400434{
435 int i;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400436 u8 tx_ser = 0;
437 u8 rx_ser = 0;
Michal Bachraty2952b272013-02-28 16:07:08 +0100438 u8 ser;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200439 u8 slots = mcasp->tdm_slots;
Michal Bachraty2952b272013-02-28 16:07:08 +0100440 u8 max_active_serializers = (channels + slots - 1) / slots;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400441 /* Default configuration */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200442 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_PWREMUMGT_REG, MCASP_SOFT);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400443
444 /* All PINS as McASP */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200445 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_PFUNC_REG, 0x00000000);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400446
447 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200448 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXSTAT_REG, 0xFFFFFFFF);
449 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_XEVTCTL_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400450 TXDATADMADIS);
451 } else {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200452 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXSTAT_REG, 0xFFFFFFFF);
453 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_REVTCTL_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400454 RXDATADMADIS);
455 }
456
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200457 for (i = 0; i < mcasp->num_serializer; i++) {
458 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_XRSRCTL_REG(i),
459 mcasp->serial_dir[i]);
460 if (mcasp->serial_dir[i] == TX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100461 tx_ser < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200462 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_PDIR_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400463 AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400464 tx_ser++;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200465 } else if (mcasp->serial_dir[i] == RX_MODE &&
Michal Bachraty2952b272013-02-28 16:07:08 +0100466 rx_ser < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200467 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_PDIR_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400468 AXR(i));
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400469 rx_ser++;
Michal Bachraty2952b272013-02-28 16:07:08 +0100470 } else {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200471 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_XRSRCTL_REG(i),
Michal Bachraty2952b272013-02-28 16:07:08 +0100472 SRMOD_INACTIVE, SRMOD_MASK);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400473 }
474 }
475
Daniel Mackecf327c2013-03-08 14:19:38 +0100476 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
477 ser = tx_ser;
478 else
479 ser = rx_ser;
480
481 if (ser < max_active_serializers) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200482 dev_warn(mcasp->dev, "stream has more channels (%d) than are "
Daniel Mackecf327c2013-03-08 14:19:38 +0100483 "enabled in mcasp (%d)\n", channels, ser * slots);
484 return -EINVAL;
485 }
486
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200487 if (mcasp->txnumevt && stream == SNDRV_PCM_STREAM_PLAYBACK) {
488 if (mcasp->txnumevt * tx_ser > 64)
489 mcasp->txnumevt = 1;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400490
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200491 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530492 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200493 mcasp_mod_bits(mcasp->base + MCASP_VER3_WFIFOCTL, tx_ser,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400494 NUMDMA_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200495 mcasp_mod_bits(mcasp->base + MCASP_VER3_WFIFOCTL,
496 ((mcasp->txnumevt * tx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530497 break;
498 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200499 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_WFIFOCTL,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530500 tx_ser, NUMDMA_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200501 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_WFIFOCTL,
502 ((mcasp->txnumevt * tx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530503 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400504 }
505
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200506 if (mcasp->rxnumevt && stream == SNDRV_PCM_STREAM_CAPTURE) {
507 if (mcasp->rxnumevt * rx_ser > 64)
508 mcasp->rxnumevt = 1;
509 switch (mcasp->version) {
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530510 case MCASP_VERSION_3:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200511 mcasp_mod_bits(mcasp->base + MCASP_VER3_RFIFOCTL, rx_ser,
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400512 NUMDMA_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200513 mcasp_mod_bits(mcasp->base + MCASP_VER3_RFIFOCTL,
514 ((mcasp->rxnumevt * rx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530515 break;
516 default:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200517 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_RFIFOCTL,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530518 rx_ser, NUMDMA_MASK);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200519 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_RFIFOCTL,
520 ((mcasp->rxnumevt * rx_ser) << 8), NUMEVT_MASK);
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530521 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400522 }
Michal Bachraty2952b272013-02-28 16:07:08 +0100523
524 return 0;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400525}
526
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200527static void davinci_hw_param(struct davinci_mcasp *mcasp, int stream)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400528{
529 int i, active_slots;
530 u32 mask = 0;
531
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200532 active_slots = (mcasp->tdm_slots > 31) ? 32 : mcasp->tdm_slots;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400533 for (i = 0; i < active_slots; i++)
534 mask |= (1 << i);
535
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200536 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_ACLKXCTL_REG, TX_ASYNC);
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400537
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400538 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
539 /* bit stream is MSB first with no delay */
540 /* DSP_B mode */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200541 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXTDM_REG, mask);
542 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_TXFMT_REG, TXORD);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400543
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200544 if ((mcasp->tdm_slots >= 2) && (mcasp->tdm_slots <= 32))
545 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_TXFMCTL_REG,
546 FSXMOD(mcasp->tdm_slots), FSXMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400547 else
548 printk(KERN_ERR "playback tdm slot %d not supported\n",
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200549 mcasp->tdm_slots);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400550 } else {
551 /* bit stream is MSB first with no delay */
552 /* DSP_B mode */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200553 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_RXFMT_REG, RXORD);
554 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_RXTDM_REG, mask);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400555
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200556 if ((mcasp->tdm_slots >= 2) && (mcasp->tdm_slots <= 32))
557 mcasp_mod_bits(mcasp->base + DAVINCI_MCASP_RXFMCTL_REG,
558 FSRMOD(mcasp->tdm_slots), FSRMOD(0x1FF));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400559 else
560 printk(KERN_ERR "capture tdm slot %d not supported\n",
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200561 mcasp->tdm_slots);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400562 }
563}
564
565/* S/PDIF */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200566static void davinci_hw_dit_param(struct davinci_mcasp *mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400567{
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400568 /* Set the TX format : 24 bit right rotation, 32 bit slot, Pad 0
569 and LSB first */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200570 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_TXFMT_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400571 TXROT(6) | TXSSZ(15));
572
573 /* Set TX frame synch : DIT Mode, 1 bit width, internal, rising edge */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200574 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXFMCTL_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400575 AFSXE | FSXMOD(0x180));
576
577 /* Set the TX tdm : for all the slots */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200578 mcasp_set_reg(mcasp->base + DAVINCI_MCASP_TXTDM_REG, 0xFFFFFFFF);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400579
580 /* Set the TX clock controls : div = 1 and internal */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200581 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_ACLKXCTL_REG,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400582 ACLKXE | TX_ASYNC);
583
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200584 mcasp_clr_bits(mcasp->base + DAVINCI_MCASP_XEVTCTL_REG, TXDATADMADIS);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400585
586 /* Only 44100 and 48000 are valid, both have the same setting */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200587 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_AHCLKXCTL_REG, AHCLKXDIV(3));
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400588
589 /* Enable the DIT */
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200590 mcasp_set_bits(mcasp->base + DAVINCI_MCASP_TXDITCTL_REG, DITEN);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400591}
592
593static int davinci_mcasp_hw_params(struct snd_pcm_substream *substream,
594 struct snd_pcm_hw_params *params,
595 struct snd_soc_dai *cpu_dai)
596{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200597 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400598 struct davinci_pcm_dma_params *dma_params =
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200599 &mcasp->dma_params[substream->stream];
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400600 int word_length;
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400601 u8 fifo_level;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200602 u8 slots = mcasp->tdm_slots;
Michal Bachraty7c21a782013-04-19 15:28:03 +0200603 u8 active_serializers;
Michal Bachraty2952b272013-02-28 16:07:08 +0100604 int channels;
605 struct snd_interval *pcm_channels = hw_param_interval(params,
606 SNDRV_PCM_HW_PARAM_CHANNELS);
607 channels = pcm_channels->min;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400608
Michal Bachraty7c21a782013-04-19 15:28:03 +0200609 active_serializers = (channels + slots - 1) / slots;
610
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200611 if (davinci_hw_common_param(mcasp, substream->stream, channels) == -EINVAL)
Michal Bachraty2952b272013-02-28 16:07:08 +0100612 return -EINVAL;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400613 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200614 fifo_level = mcasp->txnumevt * active_serializers;
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400615 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200616 fifo_level = mcasp->rxnumevt * active_serializers;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400617
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200618 if (mcasp->op_mode == DAVINCI_MCASP_DIT_MODE)
619 davinci_hw_dit_param(mcasp);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400620 else
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200621 davinci_hw_param(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400622
623 switch (params_format(params)) {
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400624 case SNDRV_PCM_FORMAT_U8:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400625 case SNDRV_PCM_FORMAT_S8:
626 dma_params->data_type = 1;
Daniel Mackba764b32012-12-05 18:20:37 +0100627 word_length = 8;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400628 break;
629
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400630 case SNDRV_PCM_FORMAT_U16_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400631 case SNDRV_PCM_FORMAT_S16_LE:
632 dma_params->data_type = 2;
Daniel Mackba764b32012-12-05 18:20:37 +0100633 word_length = 16;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400634 break;
635
Daniel Mack21eb24d2012-10-09 09:35:16 +0200636 case SNDRV_PCM_FORMAT_U24_3LE:
637 case SNDRV_PCM_FORMAT_S24_3LE:
Daniel Mack21eb24d2012-10-09 09:35:16 +0200638 dma_params->data_type = 3;
Daniel Mackba764b32012-12-05 18:20:37 +0100639 word_length = 24;
Daniel Mack21eb24d2012-10-09 09:35:16 +0200640 break;
641
Daniel Mack6b7fa012012-10-09 11:56:40 +0200642 case SNDRV_PCM_FORMAT_U24_LE:
643 case SNDRV_PCM_FORMAT_S24_LE:
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400644 case SNDRV_PCM_FORMAT_U32_LE:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400645 case SNDRV_PCM_FORMAT_S32_LE:
646 dma_params->data_type = 4;
Daniel Mackba764b32012-12-05 18:20:37 +0100647 word_length = 32;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400648 break;
649
650 default:
651 printk(KERN_WARNING "davinci-mcasp: unsupported PCM format");
652 return -EINVAL;
653 }
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400654
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200655 if (mcasp->version == MCASP_VERSION_2 && !fifo_level)
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400656 dma_params->acnt = 4;
657 else
Chaithrika U S6a99fb52009-08-11 16:58:52 -0400658 dma_params->acnt = dma_params->data_type;
659
Chaithrika U S4fa9c1a2009-09-30 17:32:27 -0400660 dma_params->fifo_level = fifo_level;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200661 davinci_config_channel_size(mcasp, word_length);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400662
663 return 0;
664}
665
666static int davinci_mcasp_trigger(struct snd_pcm_substream *substream,
667 int cmd, struct snd_soc_dai *cpu_dai)
668{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200669 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(cpu_dai);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400670 int ret = 0;
671
672 switch (cmd) {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400673 case SNDRV_PCM_TRIGGER_RESUME:
Chaithrika U Se473b842010-01-20 17:06:33 +0530674 case SNDRV_PCM_TRIGGER_START:
675 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200676 ret = pm_runtime_get_sync(mcasp->dev);
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530677 if (IS_ERR_VALUE(ret))
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200678 dev_err(mcasp->dev, "pm_runtime_get_sync() failed\n");
679 davinci_mcasp_start(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400680 break;
681
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400682 case SNDRV_PCM_TRIGGER_SUSPEND:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200683 davinci_mcasp_stop(mcasp, substream->stream);
684 ret = pm_runtime_put_sync(mcasp->dev);
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530685 if (IS_ERR_VALUE(ret))
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200686 dev_err(mcasp->dev, "pm_runtime_put_sync() failed\n");
Chaithrika U Sa47979b2009-12-03 18:56:56 +0530687 break;
688
689 case SNDRV_PCM_TRIGGER_STOP:
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400690 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200691 davinci_mcasp_stop(mcasp, substream->stream);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400692 break;
693
694 default:
695 ret = -EINVAL;
696 }
697
698 return ret;
699}
700
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000701static int davinci_mcasp_startup(struct snd_pcm_substream *substream,
702 struct snd_soc_dai *dai)
703{
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200704 struct davinci_mcasp *mcasp = snd_soc_dai_get_drvdata(dai);
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000705
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200706 snd_soc_dai_set_dma_data(dai, substream, mcasp->dma_params);
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000707 return 0;
708}
709
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100710static const struct snd_soc_dai_ops davinci_mcasp_dai_ops = {
Chris Paulson-Ellisbedad0c2010-11-16 12:27:09 +0000711 .startup = davinci_mcasp_startup,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400712 .trigger = davinci_mcasp_trigger,
713 .hw_params = davinci_mcasp_hw_params,
714 .set_fmt = davinci_mcasp_set_dai_fmt,
Daniel Mack4ed8c9b2012-10-04 15:08:39 +0200715 .set_clkdiv = davinci_mcasp_set_clkdiv,
Daniel Mack5b66aa22012-10-04 15:08:41 +0200716 .set_sysclk = davinci_mcasp_set_sysclk,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400717};
718
Peter Ujfalusied29cd52013-11-14 11:35:22 +0200719#define DAVINCI_MCASP_RATES SNDRV_PCM_RATE_8000_192000
720
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400721#define DAVINCI_MCASP_PCM_FMTS (SNDRV_PCM_FMTBIT_S8 | \
722 SNDRV_PCM_FMTBIT_U8 | \
723 SNDRV_PCM_FMTBIT_S16_LE | \
724 SNDRV_PCM_FMTBIT_U16_LE | \
Daniel Mack21eb24d2012-10-09 09:35:16 +0200725 SNDRV_PCM_FMTBIT_S24_LE | \
726 SNDRV_PCM_FMTBIT_U24_LE | \
727 SNDRV_PCM_FMTBIT_S24_3LE | \
728 SNDRV_PCM_FMTBIT_U24_3LE | \
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400729 SNDRV_PCM_FMTBIT_S32_LE | \
730 SNDRV_PCM_FMTBIT_U32_LE)
731
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732static struct snd_soc_dai_driver davinci_mcasp_dai[] = {
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400733 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000734 .name = "davinci-mcasp.0",
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400735 .playback = {
736 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100737 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400738 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400739 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400740 },
741 .capture = {
742 .channels_min = 2,
Michal Bachraty2952b272013-02-28 16:07:08 +0100743 .channels_max = 32 * 16,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400744 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400745 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400746 },
747 .ops = &davinci_mcasp_dai_ops,
748
749 },
750 {
Peter Ujfalusi58e48d92013-11-14 11:35:24 +0200751 .name = "davinci-mcasp.1",
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400752 .playback = {
753 .channels_min = 1,
754 .channels_max = 384,
755 .rates = DAVINCI_MCASP_RATES,
Ben Gardiner0a9d1382011-08-26 12:02:44 -0400756 .formats = DAVINCI_MCASP_PCM_FMTS,
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400757 },
758 .ops = &davinci_mcasp_dai_ops,
759 },
760
761};
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400762
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -0700763static const struct snd_soc_component_driver davinci_mcasp_component = {
764 .name = "davinci-mcasp",
765};
766
Jyri Sarha256ba182013-10-18 18:37:42 +0300767/* Some HW specific values and defaults. The rest is filled in from DT. */
768static struct snd_platform_data dm646x_mcasp_pdata = {
769 .tx_dma_offset = 0x400,
770 .rx_dma_offset = 0x400,
771 .asp_chan_q = EVENTQ_0,
772 .version = MCASP_VERSION_1,
773};
774
775static struct snd_platform_data da830_mcasp_pdata = {
776 .tx_dma_offset = 0x2000,
777 .rx_dma_offset = 0x2000,
778 .asp_chan_q = EVENTQ_0,
779 .version = MCASP_VERSION_2,
780};
781
782static struct snd_platform_data omap2_mcasp_pdata = {
783 .tx_dma_offset = 0,
784 .rx_dma_offset = 0,
785 .asp_chan_q = EVENTQ_0,
786 .version = MCASP_VERSION_3,
787};
788
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530789static const struct of_device_id mcasp_dt_ids[] = {
790 {
791 .compatible = "ti,dm646x-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300792 .data = &dm646x_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530793 },
794 {
795 .compatible = "ti,da830-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300796 .data = &da830_mcasp_pdata,
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530797 },
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530798 {
Jyri Sarha3af9e032013-10-18 18:37:44 +0300799 .compatible = "ti,am33xx-mcasp-audio",
Jyri Sarha256ba182013-10-18 18:37:42 +0300800 .data = &omap2_mcasp_pdata,
Hebbar, Gururajae5ec69d2012-09-03 13:40:40 +0530801 },
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530802 { /* sentinel */ }
803};
804MODULE_DEVICE_TABLE(of, mcasp_dt_ids);
805
806static struct snd_platform_data *davinci_mcasp_set_pdata_from_of(
807 struct platform_device *pdev)
808{
809 struct device_node *np = pdev->dev.of_node;
810 struct snd_platform_data *pdata = NULL;
811 const struct of_device_id *match =
Sachin Kamatea421eb2013-05-22 16:53:37 +0530812 of_match_device(mcasp_dt_ids, &pdev->dev);
Jyri Sarha4023fe62013-10-18 18:37:43 +0300813 struct of_phandle_args dma_spec;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530814
815 const u32 *of_serial_dir32;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530816 u32 val;
817 int i, ret = 0;
818
819 if (pdev->dev.platform_data) {
820 pdata = pdev->dev.platform_data;
821 return pdata;
822 } else if (match) {
Jyri Sarha256ba182013-10-18 18:37:42 +0300823 pdata = (struct snd_platform_data *) match->data;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530824 } else {
825 /* control shouldn't reach here. something is wrong */
826 ret = -EINVAL;
827 goto nodata;
828 }
829
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530830 ret = of_property_read_u32(np, "op-mode", &val);
831 if (ret >= 0)
832 pdata->op_mode = val;
833
834 ret = of_property_read_u32(np, "tdm-slots", &val);
Michal Bachraty2952b272013-02-28 16:07:08 +0100835 if (ret >= 0) {
836 if (val < 2 || val > 32) {
837 dev_err(&pdev->dev,
838 "tdm-slots must be in rage [2-32]\n");
839 ret = -EINVAL;
840 goto nodata;
841 }
842
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530843 pdata->tdm_slots = val;
Michal Bachraty2952b272013-02-28 16:07:08 +0100844 }
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530845
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530846 of_serial_dir32 = of_get_property(np, "serial-dir", &val);
847 val /= sizeof(u32);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530848 if (of_serial_dir32) {
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300849 u8 *of_serial_dir = devm_kzalloc(&pdev->dev,
850 (sizeof(*of_serial_dir) * val),
851 GFP_KERNEL);
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530852 if (!of_serial_dir) {
853 ret = -ENOMEM;
854 goto nodata;
855 }
856
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300857 for (i = 0; i < val; i++)
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530858 of_serial_dir[i] = be32_to_cpup(&of_serial_dir32[i]);
859
Peter Ujfalusi1427e662013-10-18 18:37:46 +0300860 pdata->num_serializer = val;
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530861 pdata->serial_dir = of_serial_dir;
862 }
863
Jyri Sarha4023fe62013-10-18 18:37:43 +0300864 ret = of_property_match_string(np, "dma-names", "tx");
865 if (ret < 0)
866 goto nodata;
867
868 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
869 &dma_spec);
870 if (ret < 0)
871 goto nodata;
872
873 pdata->tx_dma_channel = dma_spec.args[0];
874
875 ret = of_property_match_string(np, "dma-names", "rx");
876 if (ret < 0)
877 goto nodata;
878
879 ret = of_parse_phandle_with_args(np, "dmas", "#dma-cells", ret,
880 &dma_spec);
881 if (ret < 0)
882 goto nodata;
883
884 pdata->rx_dma_channel = dma_spec.args[0];
885
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530886 ret = of_property_read_u32(np, "tx-num-evt", &val);
887 if (ret >= 0)
888 pdata->txnumevt = val;
889
890 ret = of_property_read_u32(np, "rx-num-evt", &val);
891 if (ret >= 0)
892 pdata->rxnumevt = val;
893
894 ret = of_property_read_u32(np, "sram-size-playback", &val);
895 if (ret >= 0)
896 pdata->sram_size_playback = val;
897
898 ret = of_property_read_u32(np, "sram-size-capture", &val);
899 if (ret >= 0)
900 pdata->sram_size_capture = val;
901
902 return pdata;
903
904nodata:
905 if (ret < 0) {
906 dev_err(&pdev->dev, "Error populating platform data, err %d\n",
907 ret);
908 pdata = NULL;
909 }
910 return pdata;
911}
912
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400913static int davinci_mcasp_probe(struct platform_device *pdev)
914{
915 struct davinci_pcm_dma_params *dma_data;
Jyri Sarha256ba182013-10-18 18:37:42 +0300916 struct resource *mem, *ioarea, *res, *dat;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400917 struct snd_platform_data *pdata;
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200918 struct davinci_mcasp *mcasp;
Julia Lawall96d31e22011-12-29 17:51:21 +0100919 int ret;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400920
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530921 if (!pdev->dev.platform_data && !pdev->dev.of_node) {
922 dev_err(&pdev->dev, "No platform data supplied\n");
923 return -EINVAL;
924 }
925
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200926 mcasp = devm_kzalloc(&pdev->dev, sizeof(struct davinci_mcasp),
Julia Lawall96d31e22011-12-29 17:51:21 +0100927 GFP_KERNEL);
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200928 if (!mcasp)
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400929 return -ENOMEM;
930
Hebbar, Gururaja3e3b8c32012-08-27 18:56:42 +0530931 pdata = davinci_mcasp_set_pdata_from_of(pdev);
932 if (!pdata) {
933 dev_err(&pdev->dev, "no platform data\n");
934 return -EINVAL;
935 }
936
Jyri Sarha256ba182013-10-18 18:37:42 +0300937 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400938 if (!mem) {
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200939 dev_warn(mcasp->dev,
Jyri Sarha256ba182013-10-18 18:37:42 +0300940 "\"mpu\" mem resource not found, using index 0\n");
941 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
942 if (!mem) {
943 dev_err(&pdev->dev, "no mem resource?\n");
944 return -ENODEV;
945 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400946 }
947
Julia Lawall96d31e22011-12-29 17:51:21 +0100948 ioarea = devm_request_mem_region(&pdev->dev, mem->start,
Vaibhav Bediad852f4462011-02-09 18:39:52 +0530949 resource_size(mem), pdev->name);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400950 if (!ioarea) {
951 dev_err(&pdev->dev, "Audio region already claimed\n");
Julia Lawall96d31e22011-12-29 17:51:21 +0100952 return -EBUSY;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400953 }
954
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530955 pm_runtime_enable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400956
Hebbar, Gururaja10884342012-08-08 20:40:32 +0530957 ret = pm_runtime_get_sync(&pdev->dev);
958 if (IS_ERR_VALUE(ret)) {
959 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
960 return ret;
961 }
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400962
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200963 mcasp->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
964 if (!mcasp->base) {
Vaibhav Bedia4f82f022011-02-09 18:39:54 +0530965 dev_err(&pdev->dev, "ioremap failed\n");
966 ret = -ENOMEM;
967 goto err_release_clk;
968 }
969
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200970 mcasp->op_mode = pdata->op_mode;
971 mcasp->tdm_slots = pdata->tdm_slots;
972 mcasp->num_serializer = pdata->num_serializer;
973 mcasp->serial_dir = pdata->serial_dir;
974 mcasp->version = pdata->version;
975 mcasp->txnumevt = pdata->txnumevt;
976 mcasp->rxnumevt = pdata->rxnumevt;
977 mcasp->dev = &pdev->dev;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400978
Jyri Sarha256ba182013-10-18 18:37:42 +0300979 dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
980 if (!dat)
981 dat = mem;
982
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200983 dma_data = &mcasp->dma_params[SNDRV_PCM_STREAM_PLAYBACK];
Sekhar Nori48519f02010-07-19 12:31:16 +0530984 dma_data->asp_chan_q = pdata->asp_chan_q;
985 dma_data->ram_chan_q = pdata->ram_chan_q;
Matt Porterb8ec56d2012-10-17 16:08:03 +0200986 dma_data->sram_pool = pdata->sram_pool;
Ben Gardinera0c83262011-05-18 09:27:45 -0400987 dma_data->sram_size = pdata->sram_size_playback;
Jyri Sarha256ba182013-10-18 18:37:42 +0300988 dma_data->dma_addr = dat->start + pdata->tx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400989
Chaithrika U Sb67f4482009-06-05 06:28:40 -0400990 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
Jyri Sarha4023fe62013-10-18 18:37:43 +0300991 if (res)
992 dma_data->channel = res->start;
993 else
994 dma_data->channel = pdata->tx_dma_channel;
Troy Kisky92e2a6f2009-09-11 14:29:03 -0700995
Peter Ujfalusi70091a32013-11-14 11:35:29 +0200996 dma_data = &mcasp->dma_params[SNDRV_PCM_STREAM_CAPTURE];
Sekhar Nori48519f02010-07-19 12:31:16 +0530997 dma_data->asp_chan_q = pdata->asp_chan_q;
998 dma_data->ram_chan_q = pdata->ram_chan_q;
Matt Porterb8ec56d2012-10-17 16:08:03 +0200999 dma_data->sram_pool = pdata->sram_pool;
Ben Gardinera0c83262011-05-18 09:27:45 -04001000 dma_data->sram_size = pdata->sram_size_capture;
Jyri Sarha256ba182013-10-18 18:37:42 +03001001 dma_data->dma_addr = dat->start + pdata->rx_dma_offset;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001002
1003 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
Jyri Sarha4023fe62013-10-18 18:37:43 +03001004 if (res)
1005 dma_data->channel = res->start;
1006 else
1007 dma_data->channel = pdata->rx_dma_channel;
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001008
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001009 dev_set_drvdata(&pdev->dev, mcasp);
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001010 ret = snd_soc_register_component(&pdev->dev, &davinci_mcasp_component,
1011 &davinci_mcasp_dai[pdata->op_mode], 1);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001012
1013 if (ret != 0)
Julia Lawall96d31e22011-12-29 17:51:21 +01001014 goto err_release_clk;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301015
1016 ret = davinci_soc_platform_register(&pdev->dev);
1017 if (ret) {
1018 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001019 goto err_unregister_component;
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301020 }
1021
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001022 return 0;
1023
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001024err_unregister_component:
1025 snd_soc_unregister_component(&pdev->dev);
Vaibhav Bediaeef6d7b2011-02-09 18:39:53 +05301026err_release_clk:
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301027 pm_runtime_put_sync(&pdev->dev);
1028 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001029 return ret;
1030}
1031
1032static int davinci_mcasp_remove(struct platform_device *pdev)
1033{
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001034
Kuninori Morimotoeeef0ed2013-03-21 03:31:19 -07001035 snd_soc_unregister_component(&pdev->dev);
Hebbar, Gururajaf08095a2012-08-27 18:56:39 +05301036 davinci_soc_platform_unregister(&pdev->dev);
Hebbar, Gururaja10884342012-08-08 20:40:32 +05301037
1038 pm_runtime_put_sync(&pdev->dev);
1039 pm_runtime_disable(&pdev->dev);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001040
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001041 return 0;
1042}
1043
Daniel Macka85e4192013-10-01 14:50:02 +02001044#ifdef CONFIG_PM_SLEEP
1045static int davinci_mcasp_suspend(struct device *dev)
1046{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001047 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
1048 void __iomem *base = mcasp->base;
Daniel Macka85e4192013-10-01 14:50:02 +02001049
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001050 mcasp->context.txfmtctl = mcasp_get_reg(base + DAVINCI_MCASP_TXFMCTL_REG);
1051 mcasp->context.rxfmtctl = mcasp_get_reg(base + DAVINCI_MCASP_RXFMCTL_REG);
1052 mcasp->context.txfmt = mcasp_get_reg(base + DAVINCI_MCASP_TXFMT_REG);
1053 mcasp->context.rxfmt = mcasp_get_reg(base + DAVINCI_MCASP_RXFMT_REG);
1054 mcasp->context.aclkxctl = mcasp_get_reg(base + DAVINCI_MCASP_ACLKXCTL_REG);
1055 mcasp->context.aclkrctl = mcasp_get_reg(base + DAVINCI_MCASP_ACLKRCTL_REG);
1056 mcasp->context.pdir = mcasp_get_reg(base + DAVINCI_MCASP_PDIR_REG);
Daniel Macka85e4192013-10-01 14:50:02 +02001057
1058 return 0;
1059}
1060
1061static int davinci_mcasp_resume(struct device *dev)
1062{
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001063 struct davinci_mcasp *mcasp = dev_get_drvdata(dev);
1064 void __iomem *base = mcasp->base;
Daniel Macka85e4192013-10-01 14:50:02 +02001065
Peter Ujfalusi70091a32013-11-14 11:35:29 +02001066 mcasp_set_reg(base + DAVINCI_MCASP_TXFMCTL_REG, mcasp->context.txfmtctl);
1067 mcasp_set_reg(base + DAVINCI_MCASP_RXFMCTL_REG, mcasp->context.rxfmtctl);
1068 mcasp_set_reg(base + DAVINCI_MCASP_TXFMT_REG, mcasp->context.txfmt);
1069 mcasp_set_reg(base + DAVINCI_MCASP_RXFMT_REG, mcasp->context.rxfmt);
1070 mcasp_set_reg(base + DAVINCI_MCASP_ACLKXCTL_REG, mcasp->context.aclkxctl);
1071 mcasp_set_reg(base + DAVINCI_MCASP_ACLKRCTL_REG, mcasp->context.aclkrctl);
1072 mcasp_set_reg(base + DAVINCI_MCASP_PDIR_REG, mcasp->context.pdir);
Daniel Macka85e4192013-10-01 14:50:02 +02001073
1074 return 0;
1075}
1076#endif
1077
1078SIMPLE_DEV_PM_OPS(davinci_mcasp_pm_ops,
1079 davinci_mcasp_suspend,
1080 davinci_mcasp_resume);
1081
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001082static struct platform_driver davinci_mcasp_driver = {
1083 .probe = davinci_mcasp_probe,
1084 .remove = davinci_mcasp_remove,
1085 .driver = {
1086 .name = "davinci-mcasp",
1087 .owner = THIS_MODULE,
Daniel Macka85e4192013-10-01 14:50:02 +02001088 .pm = &davinci_mcasp_pm_ops,
Sachin Kamatea421eb2013-05-22 16:53:37 +05301089 .of_match_table = mcasp_dt_ids,
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001090 },
1091};
1092
Axel Linf9b8a512011-11-25 10:09:27 +08001093module_platform_driver(davinci_mcasp_driver);
Chaithrika U Sb67f4482009-06-05 06:28:40 -04001094
1095MODULE_AUTHOR("Steve Chen");
1096MODULE_DESCRIPTION("TI DAVINCI McASP SoC Interface");
1097MODULE_LICENSE("GPL");