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