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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/clk.h> |
| 19 | |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/pcm.h> |
| 22 | #include <sound/pcm_params.h> |
| 23 | #include <sound/initval.h> |
| 24 | #include <sound/soc.h> |
| 25 | |
Mark Brown | ff7d04b | 2009-07-08 16:54:51 +0100 | [diff] [blame] | 26 | #include <mach/asp.h> |
| 27 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 28 | #include "davinci-pcm.h" |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 29 | #include "davinci-i2s.h" |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 30 | |
David Brownell | a62114c | 2009-05-14 12:47:42 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * NOTE: terminology here is confusing. |
| 34 | * |
| 35 | * - This driver supports the "Audio Serial Port" (ASP), |
| 36 | * found on dm6446, dm355, and other DaVinci chips. |
| 37 | * |
| 38 | * - But it labels it a "Multi-channel Buffered Serial Port" |
| 39 | * (McBSP) as on older chips like the dm642 ... which was |
| 40 | * backward-compatible, possibly explaining that confusion. |
| 41 | * |
| 42 | * - OMAP chips have a controller called McBSP, which is |
| 43 | * incompatible with the DaVinci flavor of McBSP. |
| 44 | * |
| 45 | * - Newer DaVinci chips have a controller called McASP, |
| 46 | * incompatible with ASP and with either McBSP. |
| 47 | * |
| 48 | * In short: this uses ASP to implement I2S, not McBSP. |
| 49 | * And it won't be the only DaVinci implemention of I2S. |
| 50 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 51 | #define DAVINCI_MCBSP_DRR_REG 0x00 |
| 52 | #define DAVINCI_MCBSP_DXR_REG 0x04 |
| 53 | #define DAVINCI_MCBSP_SPCR_REG 0x08 |
| 54 | #define DAVINCI_MCBSP_RCR_REG 0x0c |
| 55 | #define DAVINCI_MCBSP_XCR_REG 0x10 |
| 56 | #define DAVINCI_MCBSP_SRGR_REG 0x14 |
| 57 | #define DAVINCI_MCBSP_PCR_REG 0x24 |
| 58 | |
| 59 | #define DAVINCI_MCBSP_SPCR_RRST (1 << 0) |
| 60 | #define DAVINCI_MCBSP_SPCR_RINTM(v) ((v) << 4) |
| 61 | #define DAVINCI_MCBSP_SPCR_XRST (1 << 16) |
| 62 | #define DAVINCI_MCBSP_SPCR_XINTM(v) ((v) << 20) |
| 63 | #define DAVINCI_MCBSP_SPCR_GRST (1 << 22) |
| 64 | #define DAVINCI_MCBSP_SPCR_FRST (1 << 23) |
| 65 | #define DAVINCI_MCBSP_SPCR_FREE (1 << 25) |
| 66 | |
| 67 | #define DAVINCI_MCBSP_RCR_RWDLEN1(v) ((v) << 5) |
| 68 | #define DAVINCI_MCBSP_RCR_RFRLEN1(v) ((v) << 8) |
| 69 | #define DAVINCI_MCBSP_RCR_RDATDLY(v) ((v) << 16) |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 70 | #define DAVINCI_MCBSP_RCR_RFIG (1 << 18) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 71 | #define DAVINCI_MCBSP_RCR_RWDLEN2(v) ((v) << 21) |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 72 | #define DAVINCI_MCBSP_RCR_RFRLEN2(v) ((v) << 24) |
| 73 | #define DAVINCI_MCBSP_RCR_RPHASE BIT(31) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 74 | |
| 75 | #define DAVINCI_MCBSP_XCR_XWDLEN1(v) ((v) << 5) |
| 76 | #define DAVINCI_MCBSP_XCR_XFRLEN1(v) ((v) << 8) |
| 77 | #define DAVINCI_MCBSP_XCR_XDATDLY(v) ((v) << 16) |
| 78 | #define DAVINCI_MCBSP_XCR_XFIG (1 << 18) |
| 79 | #define DAVINCI_MCBSP_XCR_XWDLEN2(v) ((v) << 21) |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 80 | #define DAVINCI_MCBSP_XCR_XFRLEN2(v) ((v) << 24) |
| 81 | #define DAVINCI_MCBSP_XCR_XPHASE BIT(31) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 82 | |
| 83 | #define DAVINCI_MCBSP_SRGR_FWID(v) ((v) << 8) |
| 84 | #define DAVINCI_MCBSP_SRGR_FPER(v) ((v) << 16) |
| 85 | #define DAVINCI_MCBSP_SRGR_FSGM (1 << 28) |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 86 | #define DAVINCI_MCBSP_SRGR_CLKSM BIT(29) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 87 | |
| 88 | #define DAVINCI_MCBSP_PCR_CLKRP (1 << 0) |
| 89 | #define DAVINCI_MCBSP_PCR_CLKXP (1 << 1) |
| 90 | #define DAVINCI_MCBSP_PCR_FSRP (1 << 2) |
| 91 | #define DAVINCI_MCBSP_PCR_FSXP (1 << 3) |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 92 | #define DAVINCI_MCBSP_PCR_SCLKME (1 << 7) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 93 | #define DAVINCI_MCBSP_PCR_CLKRM (1 << 8) |
| 94 | #define DAVINCI_MCBSP_PCR_CLKXM (1 << 9) |
| 95 | #define DAVINCI_MCBSP_PCR_FSRM (1 << 10) |
| 96 | #define DAVINCI_MCBSP_PCR_FSXM (1 << 11) |
| 97 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 98 | enum { |
| 99 | DAVINCI_MCBSP_WORD_8 = 0, |
| 100 | DAVINCI_MCBSP_WORD_12, |
| 101 | DAVINCI_MCBSP_WORD_16, |
| 102 | DAVINCI_MCBSP_WORD_20, |
| 103 | DAVINCI_MCBSP_WORD_24, |
| 104 | DAVINCI_MCBSP_WORD_32, |
| 105 | }; |
| 106 | |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 107 | static const unsigned char data_type[SNDRV_PCM_FORMAT_S32_LE + 1] = { |
| 108 | [SNDRV_PCM_FORMAT_S8] = 1, |
| 109 | [SNDRV_PCM_FORMAT_S16_LE] = 2, |
| 110 | [SNDRV_PCM_FORMAT_S32_LE] = 4, |
| 111 | }; |
| 112 | |
| 113 | static const unsigned char asp_word_length[SNDRV_PCM_FORMAT_S32_LE + 1] = { |
| 114 | [SNDRV_PCM_FORMAT_S8] = DAVINCI_MCBSP_WORD_8, |
| 115 | [SNDRV_PCM_FORMAT_S16_LE] = DAVINCI_MCBSP_WORD_16, |
| 116 | [SNDRV_PCM_FORMAT_S32_LE] = DAVINCI_MCBSP_WORD_32, |
| 117 | }; |
| 118 | |
| 119 | static const unsigned char double_fmt[SNDRV_PCM_FORMAT_S32_LE + 1] = { |
| 120 | [SNDRV_PCM_FORMAT_S8] = SNDRV_PCM_FORMAT_S16_LE, |
| 121 | [SNDRV_PCM_FORMAT_S16_LE] = SNDRV_PCM_FORMAT_S32_LE, |
| 122 | }; |
| 123 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 124 | struct davinci_mcbsp_dev { |
Raffaele Recalcati | ec63755 | 2010-07-06 10:39:03 +0200 | [diff] [blame^] | 125 | struct device *dev; |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 126 | struct davinci_pcm_dma_params dma_params[2]; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 127 | void __iomem *base; |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 128 | #define MOD_DSP_A 0 |
| 129 | #define MOD_DSP_B 1 |
| 130 | int mode; |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 131 | u32 pcr; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 132 | struct clk *clk; |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 133 | /* |
| 134 | * Combining both channels into 1 element will at least double the |
| 135 | * amount of time between servicing the dma channel, increase |
| 136 | * effiency, and reduce the chance of overrun/underrun. But, |
| 137 | * it will result in the left & right channels being swapped. |
| 138 | * |
| 139 | * If relabeling the left and right channels is not possible, |
| 140 | * you may want to let the codec know to swap them back. |
| 141 | * |
| 142 | * It may allow x10 the amount of time to service dma requests, |
| 143 | * if the codec is master and is using an unnecessarily fast bit clock |
| 144 | * (ie. tlvaic23b), independent of the sample rate. So, having an |
| 145 | * entire frame at once means it can be serviced at the sample rate |
| 146 | * instead of the bit clock rate. |
| 147 | * |
| 148 | * In the now unlikely case that an underrun still |
| 149 | * occurs, both the left and right samples will be repeated |
| 150 | * so that no pops are heard, and the left and right channels |
| 151 | * won't end up being swapped because of the underrun. |
| 152 | */ |
| 153 | unsigned enable_channel_combine:1; |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 154 | |
| 155 | unsigned int fmt; |
| 156 | int clk_div; |
Raffaele Recalcati | ec63755 | 2010-07-06 10:39:03 +0200 | [diff] [blame^] | 157 | int clk_input_pin; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static inline void davinci_mcbsp_write_reg(struct davinci_mcbsp_dev *dev, |
| 161 | int reg, u32 val) |
| 162 | { |
| 163 | __raw_writel(val, dev->base + reg); |
| 164 | } |
| 165 | |
| 166 | static inline u32 davinci_mcbsp_read_reg(struct davinci_mcbsp_dev *dev, int reg) |
| 167 | { |
| 168 | return __raw_readl(dev->base + reg); |
| 169 | } |
| 170 | |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 171 | static void toggle_clock(struct davinci_mcbsp_dev *dev, int playback) |
| 172 | { |
| 173 | u32 m = playback ? DAVINCI_MCBSP_PCR_CLKXP : DAVINCI_MCBSP_PCR_CLKRP; |
| 174 | /* The clock needs to toggle to complete reset. |
| 175 | * So, fake it by toggling the clk polarity. |
| 176 | */ |
| 177 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr ^ m); |
| 178 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, dev->pcr); |
| 179 | } |
| 180 | |
Troy Kisky | f9af37c | 2009-07-04 19:29:53 -0700 | [diff] [blame] | 181 | static void davinci_mcbsp_start(struct davinci_mcbsp_dev *dev, |
| 182 | struct snd_pcm_substream *substream) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 183 | { |
| 184 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 185 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 87689d5 | 2008-12-02 16:01:14 +0000 | [diff] [blame] | 186 | struct snd_soc_platform *platform = socdev->card->platform; |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 187 | int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 188 | u32 spcr; |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 189 | u32 mask = playback ? DAVINCI_MCBSP_SPCR_XRST : DAVINCI_MCBSP_SPCR_RRST; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 190 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 191 | if (spcr & mask) { |
| 192 | /* start off disabled */ |
| 193 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, |
| 194 | spcr & ~mask); |
| 195 | toggle_clock(dev, playback); |
| 196 | } |
Troy Kisky | 1bef449 | 2009-07-04 19:29:55 -0700 | [diff] [blame] | 197 | if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM | |
| 198 | DAVINCI_MCBSP_PCR_CLKXM | DAVINCI_MCBSP_PCR_CLKRM)) { |
| 199 | /* Start the sample generator */ |
| 200 | spcr |= DAVINCI_MCBSP_SPCR_GRST; |
| 201 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
| 202 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 203 | |
Troy Kisky | 1bef449 | 2009-07-04 19:29:55 -0700 | [diff] [blame] | 204 | if (playback) { |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 205 | /* Stop the DMA to avoid data loss */ |
| 206 | /* while the transmitter is out of reset to handle XSYNCERR */ |
| 207 | if (platform->pcm_ops->trigger) { |
Troy Kisky | eba575c | 2009-07-04 19:29:54 -0700 | [diff] [blame] | 208 | int ret = platform->pcm_ops->trigger(substream, |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 209 | SNDRV_PCM_TRIGGER_STOP); |
| 210 | if (ret < 0) |
| 211 | printk(KERN_DEBUG "Playback DMA stop failed\n"); |
| 212 | } |
| 213 | |
| 214 | /* Enable the transmitter */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 215 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 216 | spcr |= DAVINCI_MCBSP_SPCR_XRST; |
| 217 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 218 | |
| 219 | /* wait for any unexpected frame sync error to occur */ |
| 220 | udelay(100); |
| 221 | |
| 222 | /* Disable the transmitter to clear any outstanding XSYNCERR */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 223 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 224 | spcr &= ~DAVINCI_MCBSP_SPCR_XRST; |
| 225 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 226 | toggle_clock(dev, playback); |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 227 | |
| 228 | /* Restart the DMA */ |
| 229 | if (platform->pcm_ops->trigger) { |
Troy Kisky | eba575c | 2009-07-04 19:29:54 -0700 | [diff] [blame] | 230 | int ret = platform->pcm_ops->trigger(substream, |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 231 | SNDRV_PCM_TRIGGER_START); |
| 232 | if (ret < 0) |
| 233 | printk(KERN_DEBUG "Playback DMA start failed\n"); |
| 234 | } |
Naresh Medisetty | fb0ef64 | 2008-11-12 10:26:31 +0530 | [diff] [blame] | 235 | } |
| 236 | |
Troy Kisky | 1bef449 | 2009-07-04 19:29:55 -0700 | [diff] [blame] | 237 | /* Enable transmitter or receiver */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 238 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
Troy Kisky | 1bef449 | 2009-07-04 19:29:55 -0700 | [diff] [blame] | 239 | spcr |= mask; |
| 240 | |
| 241 | if (dev->pcr & (DAVINCI_MCBSP_PCR_FSXM | DAVINCI_MCBSP_PCR_FSRM)) { |
| 242 | /* Start frame sync */ |
| 243 | spcr |= DAVINCI_MCBSP_SPCR_FRST; |
| 244 | } |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 245 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Troy Kisky | f9af37c | 2009-07-04 19:29:53 -0700 | [diff] [blame] | 248 | static void davinci_mcbsp_stop(struct davinci_mcbsp_dev *dev, int playback) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 249 | { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 250 | u32 spcr; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 251 | |
| 252 | /* Reset transmitter/receiver and sample rate/frame sync generators */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 253 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
| 254 | spcr &= ~(DAVINCI_MCBSP_SPCR_GRST | DAVINCI_MCBSP_SPCR_FRST); |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 255 | spcr &= playback ? ~DAVINCI_MCBSP_SPCR_XRST : ~DAVINCI_MCBSP_SPCR_RRST; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 256 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 257 | toggle_clock(dev, playback); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 260 | #define DEFAULT_BITPERSAMPLE 16 |
| 261 | |
Liam Girdwood | 9cb132d | 2008-07-07 16:07:42 +0100 | [diff] [blame] | 262 | 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] | 263 | unsigned int fmt) |
| 264 | { |
| 265 | struct davinci_mcbsp_dev *dev = cpu_dai->private_data; |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 266 | unsigned int pcr; |
| 267 | unsigned int srgr; |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 268 | /* Attention srgr is updated by hw_params! */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 269 | srgr = DAVINCI_MCBSP_SRGR_FSGM | |
| 270 | DAVINCI_MCBSP_SRGR_FPER(DEFAULT_BITPERSAMPLE * 2 - 1) | |
| 271 | DAVINCI_MCBSP_SRGR_FWID(DEFAULT_BITPERSAMPLE - 1); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 272 | |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 273 | dev->fmt = fmt; |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 274 | /* set master/slave audio interface */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 275 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 276 | case SND_SOC_DAIFMT_CBS_CFS: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 277 | /* cpu is master */ |
| 278 | pcr = DAVINCI_MCBSP_PCR_FSXM | |
| 279 | DAVINCI_MCBSP_PCR_FSRM | |
| 280 | DAVINCI_MCBSP_PCR_CLKXM | |
| 281 | DAVINCI_MCBSP_PCR_CLKRM; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 282 | break; |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 283 | case SND_SOC_DAIFMT_CBM_CFS: |
Raffaele Recalcati | ec63755 | 2010-07-06 10:39:03 +0200 | [diff] [blame^] | 284 | pcr = DAVINCI_MCBSP_PCR_FSRM | DAVINCI_MCBSP_PCR_FSXM; |
| 285 | /* |
| 286 | * Selection of the clock input pin that is the |
| 287 | * input for the Sample Rate Generator. |
| 288 | * McBSP FSR and FSX are driven by the Sample Rate |
| 289 | * Generator. |
| 290 | */ |
| 291 | switch (dev->clk_input_pin) { |
| 292 | case MCBSP_CLKS: |
| 293 | pcr |= DAVINCI_MCBSP_PCR_CLKXM | |
| 294 | DAVINCI_MCBSP_PCR_CLKRM; |
| 295 | break; |
| 296 | case MCBSP_CLKR: |
| 297 | pcr |= DAVINCI_MCBSP_PCR_SCLKME; |
| 298 | break; |
| 299 | default: |
| 300 | dev_err(dev->dev, "bad clk_input_pin\n"); |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
Hugo Villeneuve | b402dff | 2008-11-08 13:26:09 -0500 | [diff] [blame] | 304 | break; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 305 | case SND_SOC_DAIFMT_CBM_CFM: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 306 | /* codec is master */ |
| 307 | pcr = 0; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 308 | break; |
| 309 | default: |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 310 | printk(KERN_ERR "%s:bad master\n", __func__); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 314 | /* interface format */ |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 315 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 316 | case SND_SOC_DAIFMT_I2S: |
Troy Kisky | 07d8d9d | 2008-12-19 13:05:24 -0700 | [diff] [blame] | 317 | /* Davinci doesn't support TRUE I2S, but some codecs will have |
| 318 | * the left and right channels contiguous. This allows |
| 319 | * dsp_a mode to be used with an inverted normal frame clk. |
| 320 | * If your codec is master and does not have contiguous |
| 321 | * channels, then you will have sound on only one channel. |
| 322 | * Try using a different mode, or codec as slave. |
| 323 | * |
| 324 | * The TLV320AIC33 is an example of a codec where this works. |
| 325 | * It has a variable bit clock frequency allowing it to have |
| 326 | * valid data on every bit clock. |
| 327 | * |
| 328 | * The TLV320AIC23 is an example of a codec where this does not |
| 329 | * work. It has a fixed bit clock frequency with progressively |
| 330 | * more empty bit clock slots between channels as the sample |
| 331 | * rate is lowered. |
| 332 | */ |
| 333 | fmt ^= SND_SOC_DAIFMT_NB_IF; |
| 334 | case SND_SOC_DAIFMT_DSP_A: |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 335 | dev->mode = MOD_DSP_A; |
| 336 | break; |
| 337 | case SND_SOC_DAIFMT_DSP_B: |
| 338 | dev->mode = MOD_DSP_B; |
Troy Kisky | 69ab820 | 2008-12-18 12:36:44 -0700 | [diff] [blame] | 339 | break; |
| 340 | default: |
| 341 | printk(KERN_ERR "%s:bad format\n", __func__); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 345 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 346 | case SND_SOC_DAIFMT_NB_NF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 347 | /* CLKRP Receive clock polarity, |
| 348 | * 1 - sampled on rising edge of CLKR |
| 349 | * valid on rising edge |
| 350 | * CLKXP Transmit clock polarity, |
| 351 | * 1 - clocked on falling edge of CLKX |
| 352 | * valid on rising edge |
| 353 | * FSRP Receive frame sync pol, 0 - active high |
| 354 | * FSXP Transmit frame sync pol, 0 - active high |
| 355 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 356 | pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 357 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 358 | case SND_SOC_DAIFMT_IB_IF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 359 | /* CLKRP Receive clock polarity, |
| 360 | * 0 - sampled on falling edge of CLKR |
| 361 | * valid on falling edge |
| 362 | * CLKXP Transmit clock polarity, |
| 363 | * 0 - clocked on rising edge of CLKX |
| 364 | * valid on falling edge |
| 365 | * FSRP Receive frame sync pol, 1 - active low |
| 366 | * FSXP Transmit frame sync pol, 1 - active low |
| 367 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 368 | pcr |= (DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 369 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 370 | case SND_SOC_DAIFMT_NB_IF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 371 | /* CLKRP Receive clock polarity, |
| 372 | * 1 - sampled on rising edge of CLKR |
| 373 | * valid on rising edge |
| 374 | * CLKXP Transmit clock polarity, |
| 375 | * 1 - clocked on falling edge of CLKX |
| 376 | * valid on rising edge |
| 377 | * FSRP Receive frame sync pol, 1 - active low |
| 378 | * FSXP Transmit frame sync pol, 1 - active low |
| 379 | */ |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 380 | pcr |= (DAVINCI_MCBSP_PCR_CLKXP | DAVINCI_MCBSP_PCR_CLKRP | |
| 381 | DAVINCI_MCBSP_PCR_FSXP | DAVINCI_MCBSP_PCR_FSRP); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 382 | break; |
Troy Kisky | 9e03162 | 2008-12-19 13:05:23 -0700 | [diff] [blame] | 383 | case SND_SOC_DAIFMT_IB_NF: |
Troy Kisky | 664b4af | 2008-12-18 12:36:41 -0700 | [diff] [blame] | 384 | /* CLKRP Receive clock polarity, |
| 385 | * 0 - sampled on falling edge of CLKR |
| 386 | * valid on falling edge |
| 387 | * CLKXP Transmit clock polarity, |
| 388 | * 0 - clocked on rising edge of CLKX |
| 389 | * valid on falling edge |
| 390 | * FSRP Receive frame sync pol, 0 - active high |
| 391 | * FSXP Transmit frame sync pol, 0 - active high |
| 392 | */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 393 | break; |
| 394 | default: |
| 395 | return -EINVAL; |
| 396 | } |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 397 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr); |
Troy Kisky | c392bec | 2009-07-04 19:29:52 -0700 | [diff] [blame] | 398 | dev->pcr = pcr; |
Troy Kisky | 21903c1 | 2008-12-18 12:36:43 -0700 | [diff] [blame] | 399 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_PCR_REG, pcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 403 | static int davinci_i2s_dai_set_clkdiv(struct snd_soc_dai *cpu_dai, |
| 404 | int div_id, int div) |
| 405 | { |
| 406 | struct davinci_mcbsp_dev *dev = cpu_dai->private_data; |
| 407 | |
| 408 | if (div_id != DAVINCI_MCBSP_CLKGDV) |
| 409 | return -ENODEV; |
| 410 | |
| 411 | dev->clk_div = div; |
| 412 | return 0; |
| 413 | } |
| 414 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 415 | static int davinci_i2s_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 416 | struct snd_pcm_hw_params *params, |
| 417 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 418 | { |
Troy Kisky | 9bb7415 | 2009-08-06 16:55:31 -0700 | [diff] [blame] | 419 | struct davinci_mcbsp_dev *dev = dai->private_data; |
Troy Kisky | 81ac55a | 2009-09-11 14:29:02 -0700 | [diff] [blame] | 420 | struct davinci_pcm_dma_params *dma_params = |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 421 | &dev->dma_params[substream->stream]; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 422 | struct snd_interval *i = NULL; |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 423 | int mcbsp_word_length, master; |
| 424 | unsigned int rcr, xcr, srgr, clk_div, freq, framesize; |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 425 | u32 spcr; |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 426 | snd_pcm_format_t fmt; |
| 427 | unsigned element_cnt = 1; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 428 | |
| 429 | /* general line settings */ |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 430 | spcr = davinci_mcbsp_read_reg(dev, DAVINCI_MCBSP_SPCR_REG); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 431 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 432 | spcr |= DAVINCI_MCBSP_SPCR_RINTM(3) | DAVINCI_MCBSP_SPCR_FREE; |
| 433 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 434 | } else { |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 435 | spcr |= DAVINCI_MCBSP_SPCR_XINTM(3) | DAVINCI_MCBSP_SPCR_FREE; |
| 436 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SPCR_REG, spcr); |
Naresh Medisetty | cb6e206 | 2008-11-18 11:01:03 +0530 | [diff] [blame] | 437 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 438 | |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 439 | master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK; |
| 440 | fmt = params_format(params); |
| 441 | mcbsp_word_length = asp_word_length[fmt]; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 442 | |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 443 | switch (master) { |
| 444 | case SND_SOC_DAIFMT_CBS_CFS: |
| 445 | freq = clk_get_rate(dev->clk); |
| 446 | srgr = DAVINCI_MCBSP_SRGR_FSGM | |
| 447 | DAVINCI_MCBSP_SRGR_CLKSM; |
| 448 | srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * |
| 449 | 8 - 1); |
| 450 | /* symmetric waveforms */ |
| 451 | clk_div = freq / (mcbsp_word_length * 16) / |
| 452 | params->rate_num * params->rate_den; |
| 453 | srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * |
| 454 | 16 - 1); |
| 455 | clk_div &= 0xFF; |
| 456 | srgr |= clk_div; |
| 457 | break; |
| 458 | case SND_SOC_DAIFMT_CBM_CFS: |
| 459 | srgr = DAVINCI_MCBSP_SRGR_FSGM; |
| 460 | clk_div = dev->clk_div - 1; |
| 461 | srgr |= DAVINCI_MCBSP_SRGR_FWID(mcbsp_word_length * 8 - 1); |
| 462 | srgr |= DAVINCI_MCBSP_SRGR_FPER(mcbsp_word_length * 16 - 1); |
| 463 | clk_div &= 0xFF; |
| 464 | srgr |= clk_div; |
| 465 | break; |
| 466 | case SND_SOC_DAIFMT_CBM_CFM: |
| 467 | /* Clock and frame sync given from external sources */ |
| 468 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
| 469 | srgr = DAVINCI_MCBSP_SRGR_FSGM; |
| 470 | srgr |= DAVINCI_MCBSP_SRGR_FWID(snd_interval_value(i) - 1); |
| 471 | pr_debug("%s - %d FWID set: re-read srgr = %X\n", |
| 472 | __func__, __LINE__, snd_interval_value(i) - 1); |
| 473 | |
| 474 | i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS); |
| 475 | srgr |= DAVINCI_MCBSP_SRGR_FPER(snd_interval_value(i) - 1); |
| 476 | break; |
| 477 | default: |
| 478 | return -EINVAL; |
| 479 | } |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 480 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_SRGR_REG, srgr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 481 | |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 482 | rcr = DAVINCI_MCBSP_RCR_RFIG; |
| 483 | xcr = DAVINCI_MCBSP_XCR_XFIG; |
| 484 | if (dev->mode == MOD_DSP_B) { |
| 485 | rcr |= DAVINCI_MCBSP_RCR_RDATDLY(0); |
| 486 | xcr |= DAVINCI_MCBSP_XCR_XDATDLY(0); |
| 487 | } else { |
| 488 | rcr |= DAVINCI_MCBSP_RCR_RDATDLY(1); |
| 489 | xcr |= DAVINCI_MCBSP_XCR_XDATDLY(1); |
| 490 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 491 | /* Determine xfer data type */ |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 492 | fmt = params_format(params); |
| 493 | if ((fmt > SNDRV_PCM_FORMAT_S32_LE) || !data_type[fmt]) { |
Jean Delvare | 9b6e12e | 2008-08-26 15:47:55 +0200 | [diff] [blame] | 494 | printk(KERN_WARNING "davinci-i2s: unsupported PCM format\n"); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 495 | return -EINVAL; |
| 496 | } |
| 497 | |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 498 | if (params_channels(params) == 2) { |
| 499 | element_cnt = 2; |
| 500 | if (double_fmt[fmt] && dev->enable_channel_combine) { |
| 501 | element_cnt = 1; |
| 502 | fmt = double_fmt[fmt]; |
| 503 | } |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 504 | switch (master) { |
| 505 | case SND_SOC_DAIFMT_CBS_CFS: |
| 506 | case SND_SOC_DAIFMT_CBS_CFM: |
| 507 | rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(0); |
| 508 | xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(0); |
| 509 | rcr |= DAVINCI_MCBSP_RCR_RPHASE; |
| 510 | xcr |= DAVINCI_MCBSP_XCR_XPHASE; |
| 511 | break; |
| 512 | case SND_SOC_DAIFMT_CBM_CFM: |
| 513 | case SND_SOC_DAIFMT_CBM_CFS: |
| 514 | rcr |= DAVINCI_MCBSP_RCR_RFRLEN2(element_cnt - 1); |
| 515 | xcr |= DAVINCI_MCBSP_XCR_XFRLEN2(element_cnt - 1); |
| 516 | break; |
| 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 520 | } |
| 521 | dma_params->acnt = dma_params->data_type = data_type[fmt]; |
Chaithrika U S | 4fa9c1a | 2009-09-30 17:32:27 -0400 | [diff] [blame] | 522 | dma_params->fifo_level = 0; |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 523 | mcbsp_word_length = asp_word_length[fmt]; |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 524 | |
| 525 | switch (master) { |
| 526 | case SND_SOC_DAIFMT_CBS_CFS: |
| 527 | case SND_SOC_DAIFMT_CBS_CFM: |
| 528 | rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(0); |
| 529 | xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(0); |
| 530 | break; |
| 531 | case SND_SOC_DAIFMT_CBM_CFM: |
| 532 | case SND_SOC_DAIFMT_CBM_CFS: |
| 533 | rcr |= DAVINCI_MCBSP_RCR_RFRLEN1(element_cnt - 1); |
| 534 | xcr |= DAVINCI_MCBSP_XCR_XFRLEN1(element_cnt - 1); |
| 535 | break; |
| 536 | default: |
| 537 | return -EINVAL; |
| 538 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 539 | |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 540 | rcr |= DAVINCI_MCBSP_RCR_RWDLEN1(mcbsp_word_length) | |
| 541 | DAVINCI_MCBSP_RCR_RWDLEN2(mcbsp_word_length); |
| 542 | xcr |= DAVINCI_MCBSP_XCR_XWDLEN1(mcbsp_word_length) | |
| 543 | DAVINCI_MCBSP_XCR_XWDLEN2(mcbsp_word_length); |
| 544 | |
| 545 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Troy Kisky | 35cf635 | 2009-07-04 19:29:51 -0700 | [diff] [blame] | 546 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_XCR_REG, xcr); |
Troy Kisky | f5cfa95 | 2009-07-04 19:29:57 -0700 | [diff] [blame] | 547 | else |
| 548 | davinci_mcbsp_write_reg(dev, DAVINCI_MCBSP_RCR_REG, rcr); |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 549 | |
| 550 | pr_debug("%s - %d srgr=%X\n", __func__, __LINE__, srgr); |
| 551 | pr_debug("%s - %d xcr=%X\n", __func__, __LINE__, xcr); |
| 552 | pr_debug("%s - %d rcr=%X\n", __func__, __LINE__, rcr); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Troy Kisky | af0adf3 | 2009-07-04 19:29:59 -0700 | [diff] [blame] | 556 | static int davinci_i2s_prepare(struct snd_pcm_substream *substream, |
| 557 | struct snd_soc_dai *dai) |
| 558 | { |
Troy Kisky | 9bb7415 | 2009-08-06 16:55:31 -0700 | [diff] [blame] | 559 | struct davinci_mcbsp_dev *dev = dai->private_data; |
Troy Kisky | af0adf3 | 2009-07-04 19:29:59 -0700 | [diff] [blame] | 560 | int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 561 | davinci_mcbsp_stop(dev, playback); |
| 562 | if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) { |
| 563 | /* codec is master */ |
| 564 | davinci_mcbsp_start(dev, substream); |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 569 | static int davinci_i2s_trigger(struct snd_pcm_substream *substream, int cmd, |
| 570 | struct snd_soc_dai *dai) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 571 | { |
Troy Kisky | 9bb7415 | 2009-08-06 16:55:31 -0700 | [diff] [blame] | 572 | struct davinci_mcbsp_dev *dev = dai->private_data; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 573 | int ret = 0; |
Troy Kisky | f9af37c | 2009-07-04 19:29:53 -0700 | [diff] [blame] | 574 | int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
Troy Kisky | af0adf3 | 2009-07-04 19:29:59 -0700 | [diff] [blame] | 575 | if ((dev->pcr & DAVINCI_MCBSP_PCR_FSXM) == 0) |
| 576 | return 0; /* return if codec is master */ |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 577 | |
| 578 | switch (cmd) { |
| 579 | case SNDRV_PCM_TRIGGER_START: |
| 580 | case SNDRV_PCM_TRIGGER_RESUME: |
| 581 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Troy Kisky | f9af37c | 2009-07-04 19:29:53 -0700 | [diff] [blame] | 582 | davinci_mcbsp_start(dev, substream); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 583 | break; |
| 584 | case SNDRV_PCM_TRIGGER_STOP: |
| 585 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 586 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Troy Kisky | f9af37c | 2009-07-04 19:29:53 -0700 | [diff] [blame] | 587 | davinci_mcbsp_stop(dev, playback); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 588 | break; |
| 589 | default: |
| 590 | ret = -EINVAL; |
| 591 | } |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 592 | return ret; |
| 593 | } |
| 594 | |
Troy Kisky | af0adf3 | 2009-07-04 19:29:59 -0700 | [diff] [blame] | 595 | static void davinci_i2s_shutdown(struct snd_pcm_substream *substream, |
| 596 | struct snd_soc_dai *dai) |
| 597 | { |
Troy Kisky | 9bb7415 | 2009-08-06 16:55:31 -0700 | [diff] [blame] | 598 | struct davinci_mcbsp_dev *dev = dai->private_data; |
Troy Kisky | af0adf3 | 2009-07-04 19:29:59 -0700 | [diff] [blame] | 599 | int playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 600 | davinci_mcbsp_stop(dev, playback); |
| 601 | } |
| 602 | |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 603 | #define DAVINCI_I2S_RATES SNDRV_PCM_RATE_8000_96000 |
| 604 | |
| 605 | static struct snd_soc_dai_ops davinci_i2s_dai_ops = { |
Mark Brown | 3f405b4 | 2009-07-07 19:18:46 +0100 | [diff] [blame] | 606 | .shutdown = davinci_i2s_shutdown, |
| 607 | .prepare = davinci_i2s_prepare, |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 608 | .trigger = davinci_i2s_trigger, |
| 609 | .hw_params = davinci_i2s_hw_params, |
| 610 | .set_fmt = davinci_i2s_set_dai_fmt, |
Raffaele Recalcati | a4c8ea2 | 2010-07-06 10:39:02 +0200 | [diff] [blame] | 611 | .set_clkdiv = davinci_i2s_dai_set_clkdiv, |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 612 | |
| 613 | }; |
| 614 | |
| 615 | struct snd_soc_dai davinci_i2s_dai = { |
| 616 | .name = "davinci-i2s", |
| 617 | .id = 0, |
| 618 | .playback = { |
| 619 | .channels_min = 2, |
| 620 | .channels_max = 2, |
| 621 | .rates = DAVINCI_I2S_RATES, |
| 622 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 623 | .capture = { |
| 624 | .channels_min = 2, |
| 625 | .channels_max = 2, |
| 626 | .rates = DAVINCI_I2S_RATES, |
| 627 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 628 | .ops = &davinci_i2s_dai_ops, |
| 629 | |
| 630 | }; |
| 631 | EXPORT_SYMBOL_GPL(davinci_i2s_dai); |
| 632 | |
| 633 | static int davinci_i2s_probe(struct platform_device *pdev) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 634 | { |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 635 | struct snd_platform_data *pdata = pdev->dev.platform_data; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 636 | struct davinci_mcbsp_dev *dev; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 637 | struct resource *mem, *ioarea, *res; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 638 | int ret; |
| 639 | |
| 640 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 641 | if (!mem) { |
| 642 | dev_err(&pdev->dev, "no mem resource?\n"); |
| 643 | return -ENODEV; |
| 644 | } |
| 645 | |
| 646 | ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1, |
| 647 | pdev->name); |
| 648 | if (!ioarea) { |
| 649 | dev_err(&pdev->dev, "McBSP region already claimed\n"); |
| 650 | return -EBUSY; |
| 651 | } |
| 652 | |
| 653 | dev = kzalloc(sizeof(struct davinci_mcbsp_dev), GFP_KERNEL); |
| 654 | if (!dev) { |
| 655 | ret = -ENOMEM; |
| 656 | goto err_release_region; |
| 657 | } |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 658 | if (pdata) { |
Troy Kisky | 0d6c977 | 2009-11-18 17:49:51 -0700 | [diff] [blame] | 659 | dev->enable_channel_combine = pdata->enable_channel_combine; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 660 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].sram_size = |
| 661 | pdata->sram_size_playback; |
| 662 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].sram_size = |
| 663 | pdata->sram_size_capture; |
Raffaele Recalcati | ec63755 | 2010-07-06 10:39:03 +0200 | [diff] [blame^] | 664 | dev->clk_input_pin = pdata->clk_input_pin; |
Troy Kisky | 1e224f3 | 2009-11-18 17:49:53 -0700 | [diff] [blame] | 665 | } |
Kevin Hilman | 3e46a44 | 2009-07-15 10:42:09 -0700 | [diff] [blame] | 666 | dev->clk = clk_get(&pdev->dev, NULL); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 667 | if (IS_ERR(dev->clk)) { |
| 668 | ret = -ENODEV; |
| 669 | goto err_free_mem; |
| 670 | } |
| 671 | clk_enable(dev->clk); |
| 672 | |
| 673 | dev->base = (void __iomem *)IO_ADDRESS(mem->start); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 674 | |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 675 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].dma_addr = |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 676 | (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DXR_REG); |
| 677 | |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 678 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].dma_addr = |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 679 | (dma_addr_t)(io_v2p(dev->base) + DAVINCI_MCBSP_DRR_REG); |
| 680 | |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 681 | /* first TX, then RX */ |
| 682 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 683 | if (!res) { |
| 684 | dev_err(&pdev->dev, "no DMA resource\n"); |
Chaithrika U S | efd13be | 2009-06-08 06:49:41 -0400 | [diff] [blame] | 685 | ret = -ENXIO; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 686 | goto err_free_mem; |
| 687 | } |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 688 | dev->dma_params[SNDRV_PCM_STREAM_PLAYBACK].channel = res->start; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 689 | |
| 690 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 691 | if (!res) { |
| 692 | dev_err(&pdev->dev, "no DMA resource\n"); |
Chaithrika U S | efd13be | 2009-06-08 06:49:41 -0400 | [diff] [blame] | 693 | ret = -ENXIO; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 694 | goto err_free_mem; |
| 695 | } |
Troy Kisky | 92e2a6f | 2009-09-11 14:29:03 -0700 | [diff] [blame] | 696 | dev->dma_params[SNDRV_PCM_STREAM_CAPTURE].channel = res->start; |
Raffaele Recalcati | ec63755 | 2010-07-06 10:39:03 +0200 | [diff] [blame^] | 697 | dev->dev = &pdev->dev; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 698 | |
| 699 | davinci_i2s_dai.private_data = dev; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 700 | davinci_i2s_dai.capture.dma_data = dev->dma_params; |
| 701 | davinci_i2s_dai.playback.dma_data = dev->dma_params; |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 702 | ret = snd_soc_register_dai(&davinci_i2s_dai); |
| 703 | if (ret != 0) |
| 704 | goto err_free_mem; |
| 705 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 706 | return 0; |
| 707 | |
| 708 | err_free_mem: |
| 709 | kfree(dev); |
| 710 | err_release_region: |
| 711 | release_mem_region(mem->start, (mem->end - mem->start) + 1); |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 716 | static int davinci_i2s_remove(struct platform_device *pdev) |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 717 | { |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 718 | struct davinci_mcbsp_dev *dev = davinci_i2s_dai.private_data; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 719 | struct resource *mem; |
| 720 | |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 721 | snd_soc_unregister_dai(&davinci_i2s_dai); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 722 | clk_disable(dev->clk); |
| 723 | clk_put(dev->clk); |
| 724 | dev->clk = NULL; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 725 | kfree(dev); |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 726 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 727 | release_mem_region(mem->start, (mem->end - mem->start) + 1); |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 728 | |
| 729 | return 0; |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 732 | static struct platform_driver davinci_mcbsp_driver = { |
| 733 | .probe = davinci_i2s_probe, |
| 734 | .remove = davinci_i2s_remove, |
| 735 | .driver = { |
| 736 | .name = "davinci-asp", |
| 737 | .owner = THIS_MODULE, |
| 738 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 739 | }; |
| 740 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 741 | static int __init davinci_i2s_init(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 742 | { |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 743 | return platform_driver_register(&davinci_mcbsp_driver); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 744 | } |
| 745 | module_init(davinci_i2s_init); |
| 746 | |
| 747 | static void __exit davinci_i2s_exit(void) |
| 748 | { |
Chaithrika U S | 5204d49 | 2009-06-05 06:28:23 -0400 | [diff] [blame] | 749 | platform_driver_unregister(&davinci_mcbsp_driver); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 750 | } |
| 751 | module_exit(davinci_i2s_exit); |
| 752 | |
Vladimir Barinov | 310355c | 2008-02-18 11:40:22 +0100 | [diff] [blame] | 753 | MODULE_AUTHOR("Vladimir Barinov"); |
| 754 | MODULE_DESCRIPTION("TI DAVINCI I2S (McBSP) SoC Interface"); |
| 755 | MODULE_LICENSE("GPL"); |