Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port |
| 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 6 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
| 7 | * Peter Ujfalusi <peter.ujfalusi@nokia.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/initval.h> |
| 32 | #include <sound/soc.h> |
| 33 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 34 | #include <plat/dma.h> |
| 35 | #include <plat/mcbsp.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 36 | #include "omap-mcbsp.h" |
| 37 | #include "omap-pcm.h" |
| 38 | |
Jarkko Nikula | 0b60485 | 2008-11-12 17:05:51 +0200 | [diff] [blame] | 39 | #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 40 | |
Ilkka Koskinen | 83905c1 | 2010-02-22 12:21:12 +0000 | [diff] [blame] | 41 | #define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \ |
| 42 | xhandler_get, xhandler_put) \ |
| 43 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ |
| 44 | .info = omap_mcbsp_st_info_volsw, \ |
| 45 | .get = xhandler_get, .put = xhandler_put, \ |
| 46 | .private_value = (unsigned long) &(struct soc_mixer_control) \ |
| 47 | {.min = xmin, .max = xmax} } |
| 48 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 49 | struct omap_mcbsp_data { |
| 50 | unsigned int bus_id; |
| 51 | struct omap_mcbsp_reg_cfg regs; |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 52 | unsigned int fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 53 | /* |
| 54 | * Flags indicating is the bus already activated and configured by |
| 55 | * another substream |
| 56 | */ |
| 57 | int active; |
| 58 | int configured; |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 59 | unsigned int in_freq; |
| 60 | int clk_div; |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 61 | int wlen; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 64 | static struct omap_mcbsp_data mcbsp_data[NUM_LINKS]; |
| 65 | |
| 66 | /* |
| 67 | * Stream DMA parameters. DMA request line and port address are set runtime |
| 68 | * since they are different between OMAP1 and later OMAPs |
| 69 | */ |
Jarkko Nikula | 2e89713 | 2008-10-09 15:57:21 +0300 | [diff] [blame] | 70 | static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 71 | |
| 72 | #if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX) |
| 73 | static const int omap1_dma_reqs[][2] = { |
| 74 | { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX }, |
| 75 | { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX }, |
| 76 | { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX }, |
| 77 | }; |
| 78 | static const unsigned long omap1_mcbsp_port[][2] = { |
| 79 | { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1, |
| 80 | OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 81 | { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1, |
| 82 | OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 83 | { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1, |
| 84 | OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 85 | }; |
| 86 | #else |
| 87 | static const int omap1_dma_reqs[][2] = {}; |
| 88 | static const unsigned long omap1_mcbsp_port[][2] = {}; |
| 89 | #endif |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 90 | |
Tony Lindgren | a8eb7ca | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 91 | #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 92 | static const int omap24xx_dma_reqs[][2] = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 93 | { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX }, |
| 94 | { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX }, |
Tony Lindgren | a8eb7ca | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 95 | #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 96 | { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX }, |
| 97 | { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX }, |
| 98 | { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX }, |
| 99 | #endif |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 100 | }; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 101 | #else |
| 102 | static const int omap24xx_dma_reqs[][2] = {}; |
| 103 | #endif |
| 104 | |
| 105 | #if defined(CONFIG_ARCH_OMAP2420) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 106 | static const unsigned long omap2420_mcbsp_port[][2] = { |
| 107 | { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1, |
| 108 | OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 109 | { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1, |
| 110 | OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 }, |
| 111 | }; |
| 112 | #else |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 113 | static const unsigned long omap2420_mcbsp_port[][2] = {}; |
| 114 | #endif |
| 115 | |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 116 | #if defined(CONFIG_ARCH_OMAP2430) |
| 117 | static const unsigned long omap2430_mcbsp_port[][2] = { |
| 118 | { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR, |
| 119 | OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR }, |
| 120 | { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR, |
| 121 | OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR }, |
| 122 | { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR, |
| 123 | OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR }, |
| 124 | { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR, |
| 125 | OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR }, |
| 126 | { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR, |
| 127 | OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR }, |
| 128 | }; |
| 129 | #else |
| 130 | static const unsigned long omap2430_mcbsp_port[][2] = {}; |
| 131 | #endif |
| 132 | |
Tony Lindgren | a8eb7ca | 2010-02-12 12:26:48 -0800 | [diff] [blame] | 133 | #if defined(CONFIG_ARCH_OMAP3) |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 134 | static const unsigned long omap34xx_mcbsp_port[][2] = { |
| 135 | { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR, |
| 136 | OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR }, |
| 137 | { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR, |
| 138 | OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR }, |
| 139 | { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR, |
| 140 | OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR }, |
| 141 | { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR, |
| 142 | OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR }, |
| 143 | { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR, |
| 144 | OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR }, |
| 145 | }; |
| 146 | #else |
| 147 | static const unsigned long omap34xx_mcbsp_port[][2] = {}; |
| 148 | #endif |
| 149 | |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 150 | static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream) |
| 151 | { |
| 152 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 153 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 154 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 155 | struct omap_pcm_dma_data *dma_data; |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 156 | int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id); |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 157 | int words; |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 158 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 159 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 160 | |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 161 | /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */ |
| 162 | if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 163 | /* |
| 164 | * Configure McBSP threshold based on either: |
| 165 | * packet_size, when the sDMA is in packet mode, or |
| 166 | * based on the period size. |
| 167 | */ |
| 168 | if (dma_data->packet_size) |
| 169 | words = dma_data->packet_size; |
| 170 | else |
| 171 | words = snd_pcm_lib_period_bytes(substream) / |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 172 | (mcbsp_data->wlen / 8); |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 173 | else |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 174 | words = 1; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 175 | |
| 176 | /* Configure McBSP internal buffer usage */ |
| 177 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 178 | omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, words); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 179 | else |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 180 | omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, words); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 181 | } |
| 182 | |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 183 | static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params, |
| 184 | struct snd_pcm_hw_rule *rule) |
| 185 | { |
| 186 | struct snd_interval *buffer_size = hw_param_interval(params, |
| 187 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE); |
| 188 | struct snd_interval *channels = hw_param_interval(params, |
| 189 | SNDRV_PCM_HW_PARAM_CHANNELS); |
| 190 | struct omap_mcbsp_data *mcbsp_data = rule->private; |
| 191 | struct snd_interval frames; |
| 192 | int size; |
| 193 | |
| 194 | snd_interval_any(&frames); |
| 195 | size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id); |
| 196 | |
| 197 | frames.min = size / channels->min; |
| 198 | frames.integer = 1; |
| 199 | return snd_interval_refine(buffer_size, &frames); |
| 200 | } |
| 201 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 202 | static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 203 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 204 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 205 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 206 | int bus_id = mcbsp_data->bus_id; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 207 | int err = 0; |
| 208 | |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 209 | if (!cpu_dai->active) |
| 210 | err = omap_mcbsp_request(bus_id); |
| 211 | |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 212 | /* |
| 213 | * OMAP3 McBSP FIFO is word structured. |
| 214 | * McBSP2 has 1024 + 256 = 1280 word long buffer, |
| 215 | * McBSP1,3,4,5 has 128 word long buffer |
| 216 | * This means that the size of the FIFO depends on the sample format. |
| 217 | * For example on McBSP3: |
| 218 | * 16bit samples: size is 128 * 2 = 256 bytes |
| 219 | * 32bit samples: size is 128 * 4 = 512 bytes |
| 220 | * It is simpler to place constraint for buffer and period based on |
| 221 | * channels. |
| 222 | * McBSP3 as example again (16 or 32 bit samples): |
| 223 | * 1 channel (mono): size is 128 frames (128 words) |
| 224 | * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words) |
| 225 | * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words) |
| 226 | */ |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 227 | if (cpu_is_omap343x()) { |
Jarkko Nikula | 6984992 | 2009-03-27 15:32:01 +0200 | [diff] [blame] | 228 | /* |
Peter Ujfalusi | 998a8a6 | 2010-07-29 09:51:28 +0300 | [diff] [blame] | 229 | * Rule for the buffer size. We should not allow |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 230 | * smaller buffer than the FIFO size to avoid underruns |
| 231 | */ |
| 232 | snd_pcm_hw_rule_add(substream->runtime, 0, |
| 233 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 234 | omap_mcbsp_hwrule_min_buffersize, |
| 235 | mcbsp_data, |
| 236 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); |
| 237 | |
Peter Ujfalusi | 998a8a6 | 2010-07-29 09:51:28 +0300 | [diff] [blame] | 238 | /* Make sure, that the period size is always even */ |
| 239 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 240 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 241 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 242 | |
| 243 | return err; |
| 244 | } |
| 245 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 246 | static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 247 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 248 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 249 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 250 | |
| 251 | if (!cpu_dai->active) { |
| 252 | omap_mcbsp_free(mcbsp_data->bus_id); |
| 253 | mcbsp_data->configured = 0; |
| 254 | } |
| 255 | } |
| 256 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 257 | static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 258 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 259 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 260 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 261 | int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 262 | |
| 263 | switch (cmd) { |
| 264 | case SNDRV_PCM_TRIGGER_START: |
| 265 | case SNDRV_PCM_TRIGGER_RESUME: |
| 266 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 267 | mcbsp_data->active++; |
| 268 | omap_mcbsp_start(mcbsp_data->bus_id, play, !play); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case SNDRV_PCM_TRIGGER_STOP: |
| 272 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 273 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Jarkko Nikula | c12abc0 | 2009-08-07 09:59:47 +0300 | [diff] [blame] | 274 | omap_mcbsp_stop(mcbsp_data->bus_id, play, !play); |
| 275 | mcbsp_data->active--; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | err = -EINVAL; |
| 279 | } |
| 280 | |
| 281 | return err; |
| 282 | } |
| 283 | |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 284 | static snd_pcm_sframes_t omap_mcbsp_dai_delay( |
| 285 | struct snd_pcm_substream *substream, |
| 286 | struct snd_soc_dai *dai) |
| 287 | { |
| 288 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 289 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 290 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 291 | u16 fifo_use; |
| 292 | snd_pcm_sframes_t delay; |
| 293 | |
| 294 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 295 | fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id); |
| 296 | else |
| 297 | fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id); |
| 298 | |
| 299 | /* |
| 300 | * Divide the used locations with the channel count to get the |
| 301 | * FIFO usage in samples (don't care about partial samples in the |
| 302 | * buffer). |
| 303 | */ |
| 304 | delay = fifo_use / substream->runtime->channels; |
| 305 | |
| 306 | return delay; |
| 307 | } |
| 308 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 309 | static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 310 | struct snd_pcm_hw_params *params, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 311 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 312 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 313 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 314 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
Peter Ujfalusi | 81ec027 | 2010-07-29 09:51:26 +0300 | [diff] [blame] | 315 | struct omap_pcm_dma_data *dma_data; |
| 316 | int dma, bus_id = mcbsp_data->bus_id; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 317 | int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 318 | int pkt_size = 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 319 | unsigned long port; |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 320 | unsigned int format, div, framesize, master; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 321 | |
Peter Ujfalusi | 81ec027 | 2010-07-29 09:51:26 +0300 | [diff] [blame] | 322 | dma_data = &omap_mcbsp_dai_dma_params[cpu_dai->id][substream->stream]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 323 | if (cpu_class_is_omap1()) { |
| 324 | dma = omap1_dma_reqs[bus_id][substream->stream]; |
| 325 | port = omap1_mcbsp_port[bus_id][substream->stream]; |
| 326 | } else if (cpu_is_omap2420()) { |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 327 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 328 | port = omap2420_mcbsp_port[bus_id][substream->stream]; |
Jarkko Nikula | 406e2c4 | 2008-10-09 15:57:20 +0300 | [diff] [blame] | 329 | } else if (cpu_is_omap2430()) { |
| 330 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
| 331 | port = omap2430_mcbsp_port[bus_id][substream->stream]; |
| 332 | } else if (cpu_is_omap343x()) { |
| 333 | dma = omap24xx_dma_reqs[bus_id][substream->stream]; |
| 334 | port = omap34xx_mcbsp_port[bus_id][substream->stream]; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 335 | } else { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 336 | return -ENODEV; |
| 337 | } |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 338 | switch (params_format(params)) { |
| 339 | case SNDRV_PCM_FORMAT_S16_LE: |
Peter Ujfalusi | 81ec027 | 2010-07-29 09:51:26 +0300 | [diff] [blame] | 340 | dma_data->data_type = OMAP_DMA_DATA_TYPE_S16; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 341 | wlen = 16; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 342 | break; |
| 343 | case SNDRV_PCM_FORMAT_S32_LE: |
Peter Ujfalusi | 81ec027 | 2010-07-29 09:51:26 +0300 | [diff] [blame] | 344 | dma_data->data_type = OMAP_DMA_DATA_TYPE_S32; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 345 | wlen = 32; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 346 | break; |
| 347 | default: |
| 348 | return -EINVAL; |
| 349 | } |
Peter Ujfalusi | 15d0143 | 2010-07-29 09:51:25 +0300 | [diff] [blame] | 350 | if (cpu_is_omap343x()) { |
| 351 | dma_data->set_threshold = omap_mcbsp_set_threshold; |
| 352 | /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */ |
| 353 | if (omap_mcbsp_get_dma_op_mode(bus_id) == |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 354 | MCBSP_DMA_MODE_THRESHOLD) { |
| 355 | int period_words, max_thrsh; |
| 356 | |
| 357 | period_words = params_period_bytes(params) / (wlen / 8); |
| 358 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 359 | max_thrsh = omap_mcbsp_get_max_tx_threshold( |
| 360 | mcbsp_data->bus_id); |
| 361 | else |
| 362 | max_thrsh = omap_mcbsp_get_max_rx_threshold( |
| 363 | mcbsp_data->bus_id); |
| 364 | /* |
| 365 | * If the period contains less or equal number of words, |
| 366 | * we are using the original threshold mode setup: |
| 367 | * McBSP threshold = sDMA frame size = period_size |
| 368 | * Otherwise we switch to sDMA packet mode: |
| 369 | * McBSP threshold = sDMA packet size |
| 370 | * sDMA frame size = period size |
| 371 | */ |
| 372 | if (period_words > max_thrsh) { |
| 373 | int divider = 0; |
| 374 | |
| 375 | /* |
| 376 | * Look for the biggest threshold value, which |
| 377 | * divides the period size evenly. |
| 378 | */ |
| 379 | divider = period_words / max_thrsh; |
| 380 | if (period_words % max_thrsh) |
| 381 | divider++; |
| 382 | while (period_words % divider && |
| 383 | divider < period_words) |
| 384 | divider++; |
| 385 | if (divider == period_words) |
| 386 | return -EINVAL; |
| 387 | |
| 388 | pkt_size = period_words / divider; |
| 389 | sync_mode = OMAP_DMA_SYNC_PACKET; |
| 390 | } else { |
| 391 | sync_mode = OMAP_DMA_SYNC_FRAME; |
| 392 | } |
| 393 | } |
Peter Ujfalusi | 15d0143 | 2010-07-29 09:51:25 +0300 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | dma_data->name = substream->stream ? "Audio Capture" : "Audio Playback"; |
| 397 | dma_data->dma_req = dma; |
| 398 | dma_data->port_addr = port; |
| 399 | dma_data->sync_mode = sync_mode; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 400 | dma_data->packet_size = pkt_size; |
Daniel Mack | fd23b7d | 2010-03-19 14:52:55 +0000 | [diff] [blame] | 401 | |
Peter Ujfalusi | 81ec027 | 2010-07-29 09:51:26 +0300 | [diff] [blame] | 402 | snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 403 | |
| 404 | if (mcbsp_data->configured) { |
| 405 | /* McBSP already configured by another stream */ |
| 406 | return 0; |
| 407 | } |
| 408 | |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 409 | format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
| 410 | wpf = channels = params_channels(params); |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 411 | if (channels == 2 && (format == SND_SOC_DAIFMT_I2S || |
| 412 | format == SND_SOC_DAIFMT_LEFT_J)) { |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 413 | /* Use dual-phase frames */ |
| 414 | regs->rcr2 |= RPHASE; |
| 415 | regs->xcr2 |= XPHASE; |
| 416 | /* Set 1 word per (McBSP) frame for phase1 and phase2 */ |
| 417 | wpf--; |
| 418 | regs->rcr2 |= RFRLEN2(wpf - 1); |
| 419 | regs->xcr2 |= XFRLEN2(wpf - 1); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 422 | regs->rcr1 |= RFRLEN1(wpf - 1); |
| 423 | regs->xcr1 |= XFRLEN1(wpf - 1); |
| 424 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 425 | switch (params_format(params)) { |
| 426 | case SNDRV_PCM_FORMAT_S16_LE: |
| 427 | /* Set word lengths */ |
| 428 | regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16); |
| 429 | regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16); |
| 430 | regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16); |
| 431 | regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 432 | break; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 433 | case SNDRV_PCM_FORMAT_S32_LE: |
| 434 | /* Set word lengths */ |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 435 | regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32); |
| 436 | regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32); |
| 437 | regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32); |
| 438 | regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32); |
| 439 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 440 | default: |
| 441 | /* Unsupported PCM format */ |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 445 | /* In McBSP master modes, FRAME (i.e. sample rate) is generated |
| 446 | * by _counting_ BCLKs. Calculate frame size in BCLKs */ |
| 447 | master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK; |
| 448 | if (master == SND_SOC_DAIFMT_CBS_CFS) { |
| 449 | div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1; |
| 450 | framesize = (mcbsp_data->in_freq / div) / params_rate(params); |
| 451 | |
| 452 | if (framesize < wlen * channels) { |
| 453 | printk(KERN_ERR "%s: not enough bandwidth for desired rate and " |
| 454 | "channels\n", __func__); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | } else |
| 458 | framesize = wlen * channels; |
| 459 | |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 460 | /* Set FS period and length in terms of bit clock periods */ |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 461 | switch (format) { |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 462 | case SND_SOC_DAIFMT_I2S: |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 463 | case SND_SOC_DAIFMT_LEFT_J: |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 464 | regs->srgr2 |= FPER(framesize - 1); |
| 465 | regs->srgr1 |= FWID((framesize >> 1) - 1); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 466 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 467 | case SND_SOC_DAIFMT_DSP_A: |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 468 | case SND_SOC_DAIFMT_DSP_B: |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 469 | regs->srgr2 |= FPER(framesize - 1); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 470 | regs->srgr1 |= FWID(0); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 474 | omap_mcbsp_config(bus_id, &mcbsp_data->regs); |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 475 | mcbsp_data->wlen = wlen; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 476 | mcbsp_data->configured = 1; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * This must be called before _set_clkdiv and _set_sysclk since McBSP register |
| 483 | * cache is initialized here |
| 484 | */ |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 485 | static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 486 | unsigned int fmt) |
| 487 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 488 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 489 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 490 | unsigned int temp_fmt = fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 491 | |
| 492 | if (mcbsp_data->configured) |
| 493 | return 0; |
| 494 | |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 495 | mcbsp_data->fmt = fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 496 | memset(regs, 0, sizeof(*regs)); |
| 497 | /* Generic McBSP register settings */ |
| 498 | regs->spcr2 |= XINTM(3) | FREE; |
| 499 | regs->spcr1 |= RINTM(3); |
Eero Nurkkala | c721bbd | 2009-08-20 16:18:23 +0300 | [diff] [blame] | 500 | /* RFIG and XFIG are not defined in 34xx */ |
| 501 | if (!cpu_is_omap34xx()) { |
| 502 | regs->rcr2 |= RFIG; |
| 503 | regs->xcr2 |= XFIG; |
| 504 | } |
Misael Lopez Cruz | ef390c0 | 2009-01-29 13:29:46 +0200 | [diff] [blame] | 505 | if (cpu_is_omap2430() || cpu_is_omap34xx()) { |
Jarkko Nikula | 32080af | 2009-08-23 12:24:26 +0300 | [diff] [blame] | 506 | regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE; |
| 507 | regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE; |
Misael Lopez Cruz | ef390c0 | 2009-01-29 13:29:46 +0200 | [diff] [blame] | 508 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 509 | |
| 510 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 511 | case SND_SOC_DAIFMT_I2S: |
| 512 | /* 1-bit data delay */ |
| 513 | regs->rcr2 |= RDATDLY(1); |
| 514 | regs->xcr2 |= XDATDLY(1); |
| 515 | break; |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 516 | case SND_SOC_DAIFMT_LEFT_J: |
| 517 | /* 0-bit data delay */ |
| 518 | regs->rcr2 |= RDATDLY(0); |
| 519 | regs->xcr2 |= XDATDLY(0); |
| 520 | regs->spcr1 |= RJUST(2); |
| 521 | /* Invert FS polarity configuration */ |
| 522 | temp_fmt ^= SND_SOC_DAIFMT_NB_IF; |
| 523 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 524 | case SND_SOC_DAIFMT_DSP_A: |
| 525 | /* 1-bit data delay */ |
| 526 | regs->rcr2 |= RDATDLY(1); |
| 527 | regs->xcr2 |= XDATDLY(1); |
| 528 | /* Invert FS polarity configuration */ |
| 529 | temp_fmt ^= SND_SOC_DAIFMT_NB_IF; |
| 530 | break; |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 531 | case SND_SOC_DAIFMT_DSP_B: |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 532 | /* 0-bit data delay */ |
| 533 | regs->rcr2 |= RDATDLY(0); |
| 534 | regs->xcr2 |= XDATDLY(0); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 535 | /* Invert FS polarity configuration */ |
| 536 | temp_fmt ^= SND_SOC_DAIFMT_NB_IF; |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 537 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 538 | default: |
| 539 | /* Unsupported data format */ |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
| 543 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 544 | case SND_SOC_DAIFMT_CBS_CFS: |
| 545 | /* McBSP master. Set FS and bit clocks as outputs */ |
| 546 | regs->pcr0 |= FSXM | FSRM | |
| 547 | CLKXM | CLKRM; |
| 548 | /* Sample rate generator drives the FS */ |
| 549 | regs->srgr2 |= FSGM; |
| 550 | break; |
| 551 | case SND_SOC_DAIFMT_CBM_CFM: |
| 552 | /* McBSP slave */ |
| 553 | break; |
| 554 | default: |
| 555 | /* Unsupported master/slave configuration */ |
| 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | /* Set bit clock (CLKX/CLKR) and FS polarities */ |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 560 | switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 561 | case SND_SOC_DAIFMT_NB_NF: |
| 562 | /* |
| 563 | * Normal BCLK + FS. |
| 564 | * FS active low. TX data driven on falling edge of bit clock |
| 565 | * and RX data sampled on rising edge of bit clock. |
| 566 | */ |
| 567 | regs->pcr0 |= FSXP | FSRP | |
| 568 | CLKXP | CLKRP; |
| 569 | break; |
| 570 | case SND_SOC_DAIFMT_NB_IF: |
| 571 | regs->pcr0 |= CLKXP | CLKRP; |
| 572 | break; |
| 573 | case SND_SOC_DAIFMT_IB_NF: |
| 574 | regs->pcr0 |= FSXP | FSRP; |
| 575 | break; |
| 576 | case SND_SOC_DAIFMT_IB_IF: |
| 577 | break; |
| 578 | default: |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 585 | static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 586 | int div_id, int div) |
| 587 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 588 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 589 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
| 590 | |
| 591 | if (div_id != OMAP_MCBSP_CLKGDV) |
| 592 | return -ENODEV; |
| 593 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 594 | mcbsp_data->clk_div = div; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 595 | regs->srgr1 |= CLKGDV(div - 1); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 600 | static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 601 | int clk_id, unsigned int freq, |
| 602 | int dir) |
| 603 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 604 | struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 605 | struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs; |
| 606 | int err = 0; |
| 607 | |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 608 | /* The McBSP signal muxing functions are only available on McBSP1 */ |
| 609 | if (clk_id == OMAP_MCBSP_CLKR_SRC_CLKR || |
| 610 | clk_id == OMAP_MCBSP_CLKR_SRC_CLKX || |
| 611 | clk_id == OMAP_MCBSP_FSR_SRC_FSR || |
| 612 | clk_id == OMAP_MCBSP_FSR_SRC_FSX) |
| 613 | if (cpu_class_is_omap1() || mcbsp_data->bus_id != 0) |
| 614 | return -EINVAL; |
| 615 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 616 | mcbsp_data->in_freq = freq; |
| 617 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 618 | switch (clk_id) { |
| 619 | case OMAP_MCBSP_SYSCLK_CLK: |
| 620 | regs->srgr2 |= CLKSM; |
| 621 | break; |
| 622 | case OMAP_MCBSP_SYSCLK_CLKS_FCLK: |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 623 | if (cpu_class_is_omap1()) { |
| 624 | err = -EINVAL; |
| 625 | break; |
| 626 | } |
| 627 | err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id, |
| 628 | MCBSP_CLKS_PRCM_SRC); |
| 629 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 630 | case OMAP_MCBSP_SYSCLK_CLKS_EXT: |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 631 | if (cpu_class_is_omap1()) { |
| 632 | err = 0; |
| 633 | break; |
| 634 | } |
| 635 | err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id, |
| 636 | MCBSP_CLKS_PAD_SRC); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 637 | break; |
| 638 | |
| 639 | case OMAP_MCBSP_SYSCLK_CLKX_EXT: |
| 640 | regs->srgr2 |= CLKSM; |
| 641 | case OMAP_MCBSP_SYSCLK_CLKR_EXT: |
| 642 | regs->pcr0 |= SCLKME; |
| 643 | break; |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 644 | |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 645 | |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 646 | case OMAP_MCBSP_CLKR_SRC_CLKR: |
Janusz Krzysztofik | 2335385 | 2010-11-02 15:50:32 +0100 | [diff] [blame] | 647 | if (cpu_class_is_omap1()) |
| 648 | break; |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 649 | omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKR); |
| 650 | break; |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 651 | case OMAP_MCBSP_CLKR_SRC_CLKX: |
Janusz Krzysztofik | 2335385 | 2010-11-02 15:50:32 +0100 | [diff] [blame] | 652 | if (cpu_class_is_omap1()) |
| 653 | break; |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 654 | omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKX); |
| 655 | break; |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 656 | case OMAP_MCBSP_FSR_SRC_FSR: |
Janusz Krzysztofik | 2335385 | 2010-11-02 15:50:32 +0100 | [diff] [blame] | 657 | if (cpu_class_is_omap1()) |
| 658 | break; |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 659 | omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSR); |
| 660 | break; |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 661 | case OMAP_MCBSP_FSR_SRC_FSX: |
Janusz Krzysztofik | 2335385 | 2010-11-02 15:50:32 +0100 | [diff] [blame] | 662 | if (cpu_class_is_omap1()) |
| 663 | break; |
Paul Walmsley | cf4c87a | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 664 | omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSX); |
Jarkko Nikula | d2c0bda | 2009-08-28 15:35:35 +0300 | [diff] [blame] | 665 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 666 | default: |
| 667 | err = -ENODEV; |
| 668 | } |
| 669 | |
| 670 | return err; |
| 671 | } |
| 672 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 673 | static struct snd_soc_dai_ops mcbsp_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 674 | .startup = omap_mcbsp_dai_startup, |
| 675 | .shutdown = omap_mcbsp_dai_shutdown, |
| 676 | .trigger = omap_mcbsp_dai_trigger, |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 677 | .delay = omap_mcbsp_dai_delay, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 678 | .hw_params = omap_mcbsp_dai_hw_params, |
| 679 | .set_fmt = omap_mcbsp_dai_set_dai_fmt, |
| 680 | .set_clkdiv = omap_mcbsp_dai_set_clkdiv, |
| 681 | .set_sysclk = omap_mcbsp_dai_set_dai_sysclk, |
| 682 | }; |
| 683 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 684 | static int mcbsp_dai_probe(struct snd_soc_dai *dai) |
| 685 | { |
| 686 | mcbsp_data[dai->id].bus_id = dai->id; |
| 687 | snd_soc_dai_set_drvdata(dai, &mcbsp_data[dai->id].bus_id); |
| 688 | return 0; |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 689 | } |
| 690 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 691 | static struct snd_soc_dai_driver omap_mcbsp_dai = |
| 692 | { |
| 693 | .probe = mcbsp_dai_probe, |
| 694 | .playback = { |
| 695 | .channels_min = 1, |
| 696 | .channels_max = 16, |
| 697 | .rates = OMAP_MCBSP_RATES, |
| 698 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE, |
| 699 | }, |
| 700 | .capture = { |
| 701 | .channels_min = 1, |
| 702 | .channels_max = 16, |
| 703 | .rates = OMAP_MCBSP_RATES, |
| 704 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE, |
| 705 | }, |
| 706 | .ops = &mcbsp_dai_ops, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 707 | }; |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 708 | |
G, Manjunath Kondaiah | 3484457 | 2010-09-08 08:53:43 +0530 | [diff] [blame] | 709 | static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol, |
Ilkka Koskinen | 83905c1 | 2010-02-22 12:21:12 +0000 | [diff] [blame] | 710 | struct snd_ctl_elem_info *uinfo) |
| 711 | { |
| 712 | struct soc_mixer_control *mc = |
| 713 | (struct soc_mixer_control *)kcontrol->private_value; |
| 714 | int max = mc->max; |
| 715 | int min = mc->min; |
| 716 | |
| 717 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 718 | uinfo->count = 1; |
| 719 | uinfo->value.integer.min = min; |
| 720 | uinfo->value.integer.max = max; |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \ |
| 725 | static int \ |
| 726 | omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \ |
| 727 | struct snd_ctl_elem_value *uc) \ |
| 728 | { \ |
| 729 | struct soc_mixer_control *mc = \ |
| 730 | (struct soc_mixer_control *)kc->private_value; \ |
| 731 | int max = mc->max; \ |
| 732 | int min = mc->min; \ |
| 733 | int val = uc->value.integer.value[0]; \ |
| 734 | \ |
| 735 | if (val < min || val > max) \ |
| 736 | return -EINVAL; \ |
| 737 | \ |
| 738 | /* OMAP McBSP implementation uses index values 0..4 */ \ |
| 739 | return omap_st_set_chgain((id)-1, channel, val); \ |
| 740 | } |
| 741 | |
| 742 | #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \ |
| 743 | static int \ |
| 744 | omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \ |
| 745 | struct snd_ctl_elem_value *uc) \ |
| 746 | { \ |
| 747 | s16 chgain; \ |
| 748 | \ |
| 749 | if (omap_st_get_chgain((id)-1, channel, &chgain)) \ |
| 750 | return -EAGAIN; \ |
| 751 | \ |
| 752 | uc->value.integer.value[0] = chgain; \ |
| 753 | return 0; \ |
| 754 | } |
| 755 | |
| 756 | OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0) |
| 757 | OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1) |
| 758 | OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0) |
| 759 | OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1) |
| 760 | OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0) |
| 761 | OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1) |
| 762 | OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0) |
| 763 | OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1) |
| 764 | |
| 765 | static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol, |
| 766 | struct snd_ctl_elem_value *ucontrol) |
| 767 | { |
| 768 | struct soc_mixer_control *mc = |
| 769 | (struct soc_mixer_control *)kcontrol->private_value; |
| 770 | u8 value = ucontrol->value.integer.value[0]; |
| 771 | |
| 772 | if (value == omap_st_is_enabled(mc->reg)) |
| 773 | return 0; |
| 774 | |
| 775 | if (value) |
| 776 | omap_st_enable(mc->reg); |
| 777 | else |
| 778 | omap_st_disable(mc->reg); |
| 779 | |
| 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol, |
| 784 | struct snd_ctl_elem_value *ucontrol) |
| 785 | { |
| 786 | struct soc_mixer_control *mc = |
| 787 | (struct soc_mixer_control *)kcontrol->private_value; |
| 788 | |
| 789 | ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = { |
| 794 | SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0, |
| 795 | omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode), |
| 796 | OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume", |
| 797 | -32768, 32767, |
| 798 | omap_mcbsp2_get_st_ch0_volume, |
| 799 | omap_mcbsp2_set_st_ch0_volume), |
| 800 | OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume", |
| 801 | -32768, 32767, |
| 802 | omap_mcbsp2_get_st_ch1_volume, |
| 803 | omap_mcbsp2_set_st_ch1_volume), |
| 804 | }; |
| 805 | |
| 806 | static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = { |
| 807 | SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0, |
| 808 | omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode), |
| 809 | OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume", |
| 810 | -32768, 32767, |
| 811 | omap_mcbsp3_get_st_ch0_volume, |
| 812 | omap_mcbsp3_set_st_ch0_volume), |
| 813 | OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume", |
| 814 | -32768, 32767, |
| 815 | omap_mcbsp3_get_st_ch1_volume, |
| 816 | omap_mcbsp3_set_st_ch1_volume), |
| 817 | }; |
| 818 | |
| 819 | int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id) |
| 820 | { |
| 821 | if (!cpu_is_omap34xx()) |
| 822 | return -ENODEV; |
| 823 | |
| 824 | switch (mcbsp_id) { |
| 825 | case 1: /* McBSP 2 */ |
| 826 | return snd_soc_add_controls(codec, omap_mcbsp2_st_controls, |
| 827 | ARRAY_SIZE(omap_mcbsp2_st_controls)); |
| 828 | case 2: /* McBSP 3 */ |
| 829 | return snd_soc_add_controls(codec, omap_mcbsp3_st_controls, |
| 830 | ARRAY_SIZE(omap_mcbsp3_st_controls)); |
| 831 | default: |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls); |
| 838 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 839 | static __devinit int asoc_mcbsp_probe(struct platform_device *pdev) |
| 840 | { |
| 841 | return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai); |
| 842 | } |
| 843 | |
| 844 | static int __devexit asoc_mcbsp_remove(struct platform_device *pdev) |
| 845 | { |
| 846 | snd_soc_unregister_dai(&pdev->dev); |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static struct platform_driver asoc_mcbsp_driver = { |
| 851 | .driver = { |
| 852 | .name = "omap-mcbsp-dai", |
| 853 | .owner = THIS_MODULE, |
| 854 | }, |
| 855 | |
| 856 | .probe = asoc_mcbsp_probe, |
| 857 | .remove = __devexit_p(asoc_mcbsp_remove), |
| 858 | }; |
| 859 | |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 860 | static int __init snd_omap_mcbsp_init(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 861 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 862 | return platform_driver_register(&asoc_mcbsp_driver); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 863 | } |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 864 | module_init(snd_omap_mcbsp_init); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 865 | |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 866 | static void __exit snd_omap_mcbsp_exit(void) |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 867 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 868 | platform_driver_unregister(&asoc_mcbsp_driver); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 869 | } |
Takashi Iwai | f73f2a6 | 2008-12-10 07:59:33 +0100 | [diff] [blame] | 870 | module_exit(snd_omap_mcbsp_exit); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 871 | |
Jarkko Nikula | b08f7a6 | 2009-04-17 14:42:26 +0300 | [diff] [blame] | 872 | MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 873 | MODULE_DESCRIPTION("OMAP I2S SoC Interface"); |
| 874 | MODULE_LICENSE("GPL"); |