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