Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor |
| 3 | * |
Vladimir Barinov | d6b5203 | 2008-09-29 23:14:11 +0400 | [diff] [blame] | 4 | * Author: Vladimir Barinov, <vbarinov@embeddedalley.com> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 5 | * Copyright: (C) 2007 MontaVista Software, Inc., <source@mvista.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/clk.h> |
| 18 | |
| 19 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> |
| 21 | #include <sound/pcm_params.h> |
| 22 | #include <sound/initval.h> |
| 23 | #include <sound/soc.h> |
| 24 | |
| 25 | #include "davinci-pcm.h" |
| 26 | |
David Brownell | a62114c | 2009-05-14 12:47:42 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * NOTE: terminology here is confusing. |
| 30 | * |
| 31 | * - This driver supports the "Audio Serial Port" (ASP), |
| 32 | * found on dm6446, dm355, and other DaVinci chips. |
| 33 | * |
| 34 | * - But it labels it a "Multi-channel Buffered Serial Port" |
| 35 | * (McBSP) as on older chips like the dm642 ... which was |
| 36 | * backward-compatible, possibly explaining that confusion. |
| 37 | * |
| 38 | * - OMAP chips have a controller called McBSP, which is |
| 39 | * incompatible with the DaVinci flavor of McBSP. |
| 40 | * |
| 41 | * - Newer DaVinci chips have a controller called McASP, |
| 42 | * incompatible with ASP and with either McBSP. |
| 43 | * |
| 44 | * In short: this uses ASP to implement I2S, not McBSP. |
| 45 | * And it won't be the only DaVinci implemention of I2S. |
| 46 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 47 | #define DAVINCI_MCBSP_DRR_REG 0x00 |
| 48 | #define DAVINCI_MCBSP_DXR_REG 0x04 |
| 49 | #define DAVINCI_MCBSP_SPCR_REG 0x08 |
| 50 | #define DAVINCI_MCBSP_RCR_REG 0x0c |
| 51 | #define DAVINCI_MCBSP_XCR_REG 0x10 |
| 52 | #define DAVINCI_MCBSP_SRGR_REG 0x14 |
| 53 | #define DAVINCI_MCBSP_PCR_REG 0x24 |
| 54 | |
| 55 | #define DAVINCI_MCBSP_SPCR_RRST (1 << 0) |
| 56 | #define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4) |
| 57 | #define DAVINCI_MCBSP_SPCR_XRST (1 << 16) |
| 58 | #define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20) |
| 59 | #define DAVINCI_MCBSP_SPCR_GRST (1 << 22) |
| 60 | #define DAVINCI_MCBSP_SPCR_FRST (1 << 23) |
| 61 | #define DAVINCI_MCBSP_SPCR_FREE (1 << 25) |
| 62 | |
| 63 | #define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5) |
| 64 | #define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8) |
| 65 | #define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16) |
| 66 | #define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21) |
| 67 | |
| 68 | #define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5) |
| 69 | #define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8) |
| 70 | #define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16) |
| 71 | #define DAVINCI_MCBSP_XCR_XFIG (1 << 18) |
| 72 | #define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21) |
| 73 | |
| 74 | #define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8) |
| 75 | #define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16) |
| 76 | #define DAVINCI_MCBSP_SRGR_FSGM (1 << 28) |
| 77 | |
| 78 | #define DAVINCI_MCBSP_PCR_CLKRP (1 << 0) |
| 79 | #define DAVINCI_MCBSP_PCR_CLKXP (1 << 1) |
| 80 | #define DAVINCI_MCBSP_PCR_FSRP (1 << 2) |
| 81 | #define DAVINCI_MCBSP_PCR_FSXP (1 << 3) |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 82 | #define DAVINCI_MCBSP_PCR_SCLKME (1 << 7) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 83 | #define DAVINCI_MCBSP_PCR_CLKRM (1 << 8) |
| 84 | #define DAVINCI_MCBSP_PCR_CLKXM (1 << 9) |
| 85 | #define DAVINCI_MCBSP_PCR_FSRM (1 << 10) |
| 86 | #define DAVINCI_MCBSP_PCR_FSXM (1 << 11) |
| 87 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 88 | enum { |
| 89 | DAVINCI_MCBSP_WORD_8 = 0, |
| 90 | DAVINCI_MCBSP_WORD_12, |
| 91 | DAVINCI_MCBSP_WORD_16, |
| 92 | DAVINCI_MCBSP_WORD_20, |
| 93 | DAVINCI_MCBSP_WORD_24, |
| 94 | DAVINCI_MCBSP_WORD_32, |
| 95 | }; |
| 96 | |
| 97 | static struct davinci_pcm_dma_params davinci_i2s_pcm_out = { |
| 98 | .name = "I2S PCM Stereo out", |
| 99 | }; |
| 100 | |
| 101 | static struct davinci_pcm_dma_params davinci_i2s_pcm_in = { |
| 102 | .name = "I2S PCM Stereo in", |
| 103 | }; |
| 104 | |
| 105 | struct davinci_mcbsp_dev { |
| 106 | void __iomem *base; |
| 107 | struct clk *clk; |
| 108 | struct davinci_pcm_dma_params *dma_params[2]; |
| 109 | }; |
| 110 | |
| 111 | static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev, |
| 112 | int reg, u32 val) |
| 113 | { |
| 114 | __raw_writel(val, dev->base + reg); |
| 115 | } |
| 116 | |
| 117 | static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg) |
| 118 | { |
| 119 | return __raw_readl(dev->base + reg); |
| 120 | } |
| 121 | |
| 122 | static void davinci_mcbsp_start(struct snd_pcm_substream *substream) |
| 123 | { |
| 124 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 125 | struct davinci_mcbsp_dev *dev = rtd->dai->cpu_dai->private_data; |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 126 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 127 | struct snd_soc_platform *platform = socdev->card->platform; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 128 | u32 spcr; |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 129 | int ret; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 130 | |
| 131 | /* Start the sample generator and enable transmitter/receiver */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 132 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 133 | spcr |= DAVINCI_MCBSP_SPCR_GRST; |
| 134 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 135 | |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 136 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 137 | /* Stop the DMA to avoid data loss */ |
| 138 | /* while the transmitter is out of reset to handle XSYNCERR */ |
| 139 | if (platform->pcm_ops->trigger) { |
| 140 | ret = platform->pcm_ops->trigger(substream, |
| 141 | SNDRV_PCM_TRIGGER_STOP); |
| 142 | if (ret < 0) |
| 143 | printk(KERN_DEBUG "Playback DMA stop failed\n"); |
| 144 | } |
| 145 | |
| 146 | /* Enable the transmitter */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 147 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 148 | spcr |= DAVINCI_MCBSP_SPCR_XRST; |
| 149 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 150 | |
| 151 | /* wait for any unexpected frame sync error to occur */ |
| 152 | udelay(100); |
| 153 | |
| 154 | /* Disable the transmitter to clear any outstanding XSYNCERR */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 155 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 156 | spcr &= ~DAVINCI_MCBSP_SPCR_XRST; |
| 157 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 158 | |
| 159 | /* Restart the DMA */ |
| 160 | if (platform->pcm_ops->trigger) { |
| 161 | ret = platform->pcm_ops->trigger(substream, |
| 162 | SNDRV_PCM_TRIGGER_START); |
| 163 | if (ret < 0) |
| 164 | printk(KERN_DEBUG "Playback DMA start failed\n"); |
| 165 | } |
| 166 | /* Enable the transmitter */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 167 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 168 | spcr |= DAVINCI_MCBSP_SPCR_XRST; |
| 169 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 170 | |
| 171 | } else { |
| 172 | |
| 173 | /* Enable the reciever */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 174 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 175 | spcr |= DAVINCI_MCBSP_SPCR_RRST; |
| 176 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 180 | /* Start frame sync */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 181 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 182 | spcr |= DAVINCI_MCBSP_SPCR_FRST; |
| 183 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void davinci_mcbsp_stop(struct snd_pcm_substream *substream) |
| 187 | { |
| 188 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 189 | struct davinci_mcbsp_dev *dev = rtd->dai->cpu_dai->private_data; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 190 | u32 spcr; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 191 | |
| 192 | /* Reset transmitter/receiver and sample rate/frame sync generators */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 193 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 194 | spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 195 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 196 | spcr &= ~DAVINCI_MCBSP_SPCR_XRST; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 197 | else |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 198 | spcr &= ~DAVINCI_MCBSP_SPCR_RRST; |
| 199 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 202 | static int davinci_i2s_startup(struct snd_pcm_substream *substream, |
| 203 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 204 | { |
| 205 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 206 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 207 | struct davinci_mcbsp_dev *dev = rtd->dai->cpu_dai->private_data; |
| 208 | |
| 209 | cpu_dai->dma_data = dev->dma_params[substream->stream]; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 214 | #define DEFAULT_BITPERSAMPLE 16 |
| 215 | |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 216 | static int davinci_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 217 | unsigned int fmt) |
| 218 | { |
| 219 | struct davinci_mcbsp_dev *dev = cpu_dai->private_data; |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 220 | unsigned int pcr; |
| 221 | unsigned int srgr; |
| 222 | unsigned int rcr; |
| 223 | unsigned int xcr; |
| 224 | srgr = DAVINCI_MCBSP_SRGR_FSGM | |
| 225 | DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) | |
| 226 | DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 227 | |
| 228 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 229 | case SND_SOC_DAIFMT_CBS_CFS: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 230 | /* cpu is master */ |
| 231 | pcr = DAVINCI_MCBSP_PCR_FSXM | |
| 232 | DAVINCI_MCBSP_PCR_FSRM | |
| 233 | DAVINCI_MCBSP_PCR_CLKXM | |
| 234 | DAVINCI_MCBSP_PCR_CLKRM; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 235 | break; |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 236 | case SND_SOC_DAIFMT_CBM_CFS: |
| 237 | /* McBSP CLKR pin is the input for the Sample Rate Generator. |
| 238 | * McBSP FSR and FSX are driven by the Sample Rate Generator. */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 239 | pcr = DAVINCI_MCBSP_PCR_SCLKME | |
| 240 | DAVINCI_MCBSP_PCR_FSXM | |
| 241 | DAVINCI_MCBSP_PCR_FSRM; |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 242 | break; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 243 | case SND_SOC_DAIFMT_CBM_CFM: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 244 | /* codec is master */ |
| 245 | pcr = 0; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 246 | break; |
| 247 | default: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 248 | printk(KERN_ERR "%s:bad master\n", __func__); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | } |
| 251 | |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 252 | rcr = DAVINCI_MCBSP_RCR_RFRLEN1(1); |
| 253 | xcr = DAVINCI_MCBSP_XCR_XFIG | DAVINCI_MCBSP_XCR_XFRLEN1(1); |
| 254 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
Troy Kisky | 07d8d9d | 2008-12-19 13:05:24 -0700 | [diff] [blame] | 255 | case SND_SOC_DAIFMT_DSP_B: |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 256 | break; |
| 257 | case SND_SOC_DAIFMT_I2S: |
Troy Kisky | 07d8d9d | 2008-12-19 13:05:24 -0700 | [diff] [blame] | 258 | /* Davinci doesn't support TRUE I2S, but some codecs will have |
| 259 | * the left and right channels contiguous. This allows |
| 260 | * dsp_a mode to be used with an inverted normal frame clk. |
| 261 | * If your codec is master and does not have contiguous |
| 262 | * channels, then you will have sound on only one channel. |
| 263 | * Try using a different mode, or codec as slave. |
| 264 | * |
| 265 | * The TLV320AIC33 is an example of a codec where this works. |
| 266 | * It has a variable bit clock frequency allowing it to have |
| 267 | * valid data on every bit clock. |
| 268 | * |
| 269 | * The TLV320AIC23 is an example of a codec where this does not |
| 270 | * work. It has a fixed bit clock frequency with progressively |
| 271 | * more empty bit clock slots between channels as the sample |
| 272 | * rate is lowered. |
| 273 | */ |
| 274 | fmt ^= SND_SOC_DAIFMT_NB_IF; |
| 275 | case SND_SOC_DAIFMT_DSP_A: |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 276 | rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1); |
| 277 | xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1); |
| 278 | break; |
| 279 | default: |
| 280 | printk(KERN_ERR "%s:bad format\n", __func__); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 284 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 285 | case SND_SOC_DAIFMT_NB_NF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 286 | /* CLKRP Receive clock polarity, |
| 287 | * 1 - sampled on rising edge of CLKR |
| 288 | * valid on rising edge |
| 289 | * CLKXP Transmit clock polarity, |
| 290 | * 1 - clocked on falling edge of CLKX |
| 291 | * valid on rising edge |
| 292 | * FSRP Receive frame sync pol, 0 - active high |
| 293 | * FSXP Transmit frame sync pol, 0 - active high |
| 294 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 295 | pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 296 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 297 | case SND_SOC_DAIFMT_IB_IF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 298 | /* CLKRP Receive clock polarity, |
| 299 | * 0 - sampled on falling edge of CLKR |
| 300 | * valid on falling edge |
| 301 | * CLKXP Transmit clock polarity, |
| 302 | * 0 - clocked on rising edge of CLKX |
| 303 | * valid on falling edge |
| 304 | * FSRP Receive frame sync pol, 1 - active low |
| 305 | * FSXP Transmit frame sync pol, 1 - active low |
| 306 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 307 | pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 308 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 309 | case SND_SOC_DAIFMT_NB_IF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 310 | /* CLKRP Receive clock polarity, |
| 311 | * 1 - sampled on rising edge of CLKR |
| 312 | * valid on rising edge |
| 313 | * CLKXP Transmit clock polarity, |
| 314 | * 1 - clocked on falling edge of CLKX |
| 315 | * valid on rising edge |
| 316 | * FSRP Receive frame sync pol, 1 - active low |
| 317 | * FSXP Transmit frame sync pol, 1 - active low |
| 318 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 319 | pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP | |
| 320 | DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 321 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 322 | case SND_SOC_DAIFMT_IB_NF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 323 | /* CLKRP Receive clock polarity, |
| 324 | * 0 - sampled on falling edge of CLKR |
| 325 | * valid on falling edge |
| 326 | * CLKXP Transmit clock polarity, |
| 327 | * 0 - clocked on rising edge of CLKX |
| 328 | * valid on falling edge |
| 329 | * FSRP Receive frame sync pol, 0 - active high |
| 330 | * FSXP Transmit frame sync pol, 0 - active high |
| 331 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 332 | break; |
| 333 | default: |
| 334 | return -EINVAL; |
| 335 | } |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 336 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr); |
| 337 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr); |
| 338 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr); |
| 339 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int davinci_i2s_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 344 | struct snd_pcm_hw_params *params, |
| 345 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 346 | { |
| 347 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 348 | struct davinci_pcm_dma_params *dma_params = rtd->dai->cpu_dai->dma_data; |
| 349 | struct davinci_mcbsp_dev *dev = rtd->dai->cpu_dai->private_data; |
| 350 | struct snd_interval *i = NULL; |
| 351 | int mcbsp_word_length; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 352 | unsigned int rcr, xcr, srgr; |
| 353 | u32 spcr; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 354 | |
| 355 | /* general line settings */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 356 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 357 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 358 | spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE; |
| 359 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 360 | } else { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 361 | spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE; |
| 362 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 363 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 364 | |
| 365 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 366 | srgr = DAVINCI_MCBSP_SRGR_FSGM; |
| 367 | srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 368 | |
| 369 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS); |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 370 | srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1); |
| 371 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 372 | |
| 373 | /* Determine xfer data type */ |
| 374 | switch (params_format(params)) { |
| 375 | case SNDRV_PCM_FORMAT_S8: |
| 376 | dma_params->data_type = 1; |
| 377 | mcbsp_word_length = DAVINCI_MCBSP_WORD_8; |
| 378 | break; |
| 379 | case SNDRV_PCM_FORMAT_S16_LE: |
| 380 | dma_params->data_type = 2; |
| 381 | mcbsp_word_length = DAVINCI_MCBSP_WORD_16; |
| 382 | break; |
| 383 | case SNDRV_PCM_FORMAT_S32_LE: |
| 384 | dma_params->data_type = 4; |
| 385 | mcbsp_word_length = DAVINCI_MCBSP_WORD_32; |
| 386 | break; |
| 387 | default: |
Jean Delvare | 9b6e12e | 2008-08-26 15:47:55 +0200 | [diff] [blame] | 388 | printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n"); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 389 | return -EINVAL; |
| 390 | } |
| 391 | |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 392 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 393 | rcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_RCR_REG); |
| 394 | rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) | |
| 395 | DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length); |
| 396 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 397 | |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 398 | } else { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame^] | 399 | xcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_XCR_REG); |
| 400 | xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) | |
| 401 | DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length); |
| 402 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 403 | |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 404 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 408 | static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd, |
| 409 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 410 | { |
| 411 | int ret = 0; |
| 412 | |
| 413 | switch (cmd) { |
| 414 | case SNDRV_PCM_TRIGGER_START: |
| 415 | case SNDRV_PCM_TRIGGER_RESUME: |
| 416 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 417 | davinci_mcbsp_start(substream); |
| 418 | break; |
| 419 | case SNDRV_PCM_TRIGGER_STOP: |
| 420 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 421 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 422 | davinci_mcbsp_stop(substream); |
| 423 | break; |
| 424 | default: |
| 425 | ret = -EINVAL; |
| 426 | } |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
Mark Brown | bdb9287 | 2008-06-11 13:47:10 +0100 | [diff] [blame] | 431 | static int davinci_i2s_probe(struct platform_device *pdev, |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 432 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 433 | { |
| 434 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 435 | struct snd_soc_card *card = socdev->card; |
David Brownell | a62114c | 2009-05-14 12:47:42 -0700 | [diff] [blame] | 436 | struct snd_soc_dai *cpu_dai = card->dai_link->cpu_dai; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 437 | struct davinci_mcbsp_dev *dev; |
| 438 | struct resource *mem, *ioarea; |
| 439 | struct evm_snd_platform_data *pdata; |
| 440 | int ret; |
| 441 | |
| 442 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 443 | if (!mem) { |
| 444 | dev_err(&pdev->dev, "no mem resource?\n"); |
| 445 | return -ENODEV; |
| 446 | } |
| 447 | |
| 448 | ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1, |
| 449 | pdev->name); |
| 450 | if (!ioarea) { |
| 451 | dev_err(&pdev->dev, "McBSP region already claimed\n"); |
| 452 | return -EBUSY; |
| 453 | } |
| 454 | |
| 455 | dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL); |
| 456 | if (!dev) { |
| 457 | ret = -ENOMEM; |
| 458 | goto err_release_region; |
| 459 | } |
| 460 | |
| 461 | cpu_dai->private_data = dev; |
| 462 | |
David Brownell | a62114c | 2009-05-14 12:47:42 -0700 | [diff] [blame] | 463 | dev->clk = clk_get(&pdev->dev, NULL); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 464 | if (IS_ERR(dev->clk)) { |
| 465 | ret = -ENODEV; |
| 466 | goto err_free_mem; |
| 467 | } |
| 468 | clk_enable(dev->clk); |
| 469 | |
| 470 | dev->base = (void __iomem *)IO_ADDRESS(mem->start); |
| 471 | pdata = pdev->dev.platform_data; |
| 472 | |
| 473 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK] = &davinci_i2s_pcm_out; |
| 474 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->channel = pdata->tx_dma_ch; |
| 475 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK]->dma_addr = |
| 476 | (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG); |
| 477 | |
| 478 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE] = &davinci_i2s_pcm_in; |
| 479 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->channel = pdata->rx_dma_ch; |
| 480 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE]->dma_addr = |
| 481 | (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG); |
| 482 | |
| 483 | return 0; |
| 484 | |
| 485 | err_free_mem: |
| 486 | kfree(dev); |
| 487 | err_release_region: |
| 488 | release_mem_region(mem->start, (mem->end - mem->start) + 1); |
| 489 | |
| 490 | return ret; |
| 491 | } |
| 492 | |
Mark Brown | bdb9287 | 2008-06-11 13:47:10 +0100 | [diff] [blame] | 493 | static void davinci_i2s_remove(struct platform_device *pdev, |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 494 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 495 | { |
| 496 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 497 | struct snd_soc_card *card = socdev->card; |
David Brownell | a62114c | 2009-05-14 12:47:42 -0700 | [diff] [blame] | 498 | struct snd_soc_dai *cpu_dai = card->dai_link->cpu_dai; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 499 | struct davinci_mcbsp_dev *dev = cpu_dai->private_data; |
| 500 | struct resource *mem; |
| 501 | |
| 502 | clk_disable(dev->clk); |
| 503 | clk_put(dev->clk); |
| 504 | dev->clk = NULL; |
| 505 | |
| 506 | kfree(dev); |
| 507 | |
| 508 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 509 | release_mem_region(mem->start, (mem->end - mem->start) + 1); |
| 510 | } |
| 511 | |
| 512 | #define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000 |
| 513 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 514 | static struct snd_soc_dai_ops davinci_i2s_dai_ops = { |
| 515 | .startup = davinci_i2s_startup, |
| 516 | .trigger = davinci_i2s_trigger, |
| 517 | .hw_params = davinci_i2s_hw_params, |
| 518 | .set_fmt = davinci_i2s_set_dai_fmt, |
| 519 | }; |
| 520 | |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 521 | struct snd_soc_dai davinci_i2s_dai = { |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 522 | .name = "davinci-i2s", |
| 523 | .id = 0, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 524 | .probe = davinci_i2s_probe, |
| 525 | .remove = davinci_i2s_remove, |
| 526 | .playback = { |
| 527 | .channels_min = 2, |
| 528 | .channels_max = 2, |
| 529 | .rates = DAVINCI_I2S_RATES, |
| 530 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 531 | .capture = { |
| 532 | .channels_min = 2, |
| 533 | .channels_max = 2, |
| 534 | .rates = DAVINCI_I2S_RATES, |
| 535 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 536 | .ops = &davinci_i2s_dai_ops, |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 537 | }; |
| 538 | EXPORT_SYMBOL_GPL(davinci_i2s_dai); |
| 539 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 540 | static int __init davinci_i2s_init(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 541 | { |
| 542 | return snd_soc_register_dai(&davinci_i2s_dai); |
| 543 | } |
| 544 | module_init(davinci_i2s_init); |
| 545 | |
| 546 | static void __exit davinci_i2s_exit(void) |
| 547 | { |
| 548 | snd_soc_unregister_dai(&davinci_i2s_dai); |
| 549 | } |
| 550 | module_exit(davinci_i2s_exit); |
| 551 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 552 | MODULE_AUTHOR("Vladimir Barinov"); |
| 553 | MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface"); |
| 554 | MODULE_LICENSE("GPL"); |