Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 1 | /* |
Stephen Warren | ef280d3 | 2012-04-05 15:54:53 -0600 | [diff] [blame] | 2 | * tegra20_i2s.c - Tegra20 I2S driver |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 3 | * |
| 4 | * Author: Stephen Warren <swarren@nvidia.com> |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame] | 5 | * Copyright (C) 2010,2012 - NVIDIA, Inc. |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on code copyright/by: |
| 8 | * |
| 9 | * Copyright (c) 2009-2010, NVIDIA Corporation. |
| 10 | * Scott Peterson <speterson@nvidia.com> |
| 11 | * |
| 12 | * Copyright (C) 2010 Google, Inc. |
| 13 | * Iliyan Malchev <malchev@google.com> |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * version 2 as published by the Free Software Foundation. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, but |
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 27 | * 02110-1301 USA |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/debugfs.h> |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/platform_device.h> |
| 36 | #include <linux/seq_file.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/io.h> |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 39 | #include <linux/of.h> |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 40 | #include <mach/iomap.h> |
| 41 | #include <sound/core.h> |
| 42 | #include <sound/pcm.h> |
| 43 | #include <sound/pcm_params.h> |
| 44 | #include <sound/soc.h> |
| 45 | |
Stephen Warren | ef280d3 | 2012-04-05 15:54:53 -0600 | [diff] [blame] | 46 | #include "tegra20_i2s.h" |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 47 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 48 | #define DRV_NAME "tegra20-i2s" |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 49 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 50 | static inline void tegra20_i2s_write(struct tegra20_i2s *i2s, u32 reg, u32 val) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 51 | { |
| 52 | __raw_writel(val, i2s->regs + reg); |
| 53 | } |
| 54 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 55 | static inline u32 tegra20_i2s_read(struct tegra20_i2s *i2s, u32 reg) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 56 | { |
| 57 | return __raw_readl(i2s->regs + reg); |
| 58 | } |
| 59 | |
| 60 | #ifdef CONFIG_DEBUG_FS |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 61 | static int tegra20_i2s_show(struct seq_file *s, void *unused) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 62 | { |
| 63 | #define REG(r) { r, #r } |
| 64 | static const struct { |
| 65 | int offset; |
| 66 | const char *name; |
| 67 | } regs[] = { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 68 | REG(TEGRA20_I2S_CTRL), |
| 69 | REG(TEGRA20_I2S_STATUS), |
| 70 | REG(TEGRA20_I2S_TIMING), |
| 71 | REG(TEGRA20_I2S_FIFO_SCR), |
| 72 | REG(TEGRA20_I2S_PCM_CTRL), |
| 73 | REG(TEGRA20_I2S_NW_CTRL), |
| 74 | REG(TEGRA20_I2S_TDM_CTRL), |
| 75 | REG(TEGRA20_I2S_TDM_TX_RX_CTRL), |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 76 | }; |
| 77 | #undef REG |
| 78 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 79 | struct tegra20_i2s *i2s = s->private; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 80 | int i; |
| 81 | |
| 82 | for (i = 0; i < ARRAY_SIZE(regs); i++) { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 83 | u32 val = tegra20_i2s_read(i2s, regs[i].offset); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 84 | seq_printf(s, "%s = %08x\n", regs[i].name, val); |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 90 | static int tegra20_i2s_debug_open(struct inode *inode, struct file *file) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 91 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 92 | return single_open(file, tegra20_i2s_show, inode->i_private); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 95 | static const struct file_operations tegra20_i2s_debug_fops = { |
| 96 | .open = tegra20_i2s_debug_open, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 97 | .read = seq_read, |
| 98 | .llseek = seq_lseek, |
| 99 | .release = single_release, |
| 100 | }; |
| 101 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 102 | static void tegra20_i2s_debug_add(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 103 | { |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 104 | i2s->debug = debugfs_create_file(i2s->dai.name, S_IRUGO, |
| 105 | snd_soc_debugfs_root, i2s, |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 106 | &tegra20_i2s_debug_fops); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 109 | static void tegra20_i2s_debug_remove(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 110 | { |
| 111 | if (i2s->debug) |
| 112 | debugfs_remove(i2s->debug); |
| 113 | } |
| 114 | #else |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 115 | static inline void tegra20_i2s_debug_add(struct tegra20_i2s *i2s, int id) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 116 | { |
| 117 | } |
| 118 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 119 | static inline void tegra20_i2s_debug_remove(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | #endif |
| 123 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 124 | static int tegra20_i2s_set_fmt(struct snd_soc_dai *dai, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 125 | unsigned int fmt) |
| 126 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 127 | struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 128 | |
| 129 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 130 | case SND_SOC_DAIFMT_NB_NF: |
| 131 | break; |
| 132 | default: |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 136 | i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_MASTER_ENABLE; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 137 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 138 | case SND_SOC_DAIFMT_CBS_CFS: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 139 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_MASTER_ENABLE; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 140 | break; |
| 141 | case SND_SOC_DAIFMT_CBM_CFM: |
| 142 | break; |
| 143 | default: |
| 144 | return -EINVAL; |
| 145 | } |
| 146 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 147 | i2s->reg_ctrl &= ~(TEGRA20_I2S_CTRL_BIT_FORMAT_MASK | |
| 148 | TEGRA20_I2S_CTRL_LRCK_MASK); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 149 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 150 | case SND_SOC_DAIFMT_DSP_A: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 151 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP; |
| 152 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 153 | break; |
| 154 | case SND_SOC_DAIFMT_DSP_B: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 155 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP; |
| 156 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_R_LOW; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 157 | break; |
| 158 | case SND_SOC_DAIFMT_I2S: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 159 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_I2S; |
| 160 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 161 | break; |
| 162 | case SND_SOC_DAIFMT_RIGHT_J: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 163 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_RJM; |
| 164 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 165 | break; |
| 166 | case SND_SOC_DAIFMT_LEFT_J: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 167 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_FORMAT_LJM; |
| 168 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_LRCK_L_LOW; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 169 | break; |
| 170 | default: |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 177 | static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream, |
| 178 | struct snd_pcm_hw_params *params, |
| 179 | struct snd_soc_dai *dai) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 180 | { |
Stephen Warren | 7deb2b4 | 2012-03-30 17:07:21 -0600 | [diff] [blame] | 181 | struct device *dev = substream->pcm->card->dev; |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 182 | struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 183 | u32 reg; |
| 184 | int ret, sample_size, srate, i2sclock, bitcnt; |
| 185 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 186 | i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_BIT_SIZE_MASK; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 187 | switch (params_format(params)) { |
| 188 | case SNDRV_PCM_FORMAT_S16_LE: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 189 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_16; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 190 | sample_size = 16; |
| 191 | break; |
| 192 | case SNDRV_PCM_FORMAT_S24_LE: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 193 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_24; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 194 | sample_size = 24; |
| 195 | break; |
| 196 | case SNDRV_PCM_FORMAT_S32_LE: |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 197 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_BIT_SIZE_32; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 198 | sample_size = 32; |
| 199 | break; |
| 200 | default: |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | srate = params_rate(params); |
| 205 | |
| 206 | /* Final "* 2" required by Tegra hardware */ |
| 207 | i2sclock = srate * params_channels(params) * sample_size * 2; |
| 208 | |
| 209 | ret = clk_set_rate(i2s->clk_i2s, i2sclock); |
| 210 | if (ret) { |
| 211 | dev_err(dev, "Can't set I2S clock rate: %d\n", ret); |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | bitcnt = (i2sclock / (2 * srate)) - 1; |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 216 | if (bitcnt < 0 || bitcnt > TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 217 | return -EINVAL; |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 218 | reg = bitcnt << TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 219 | |
| 220 | if (i2sclock % (2 * srate)) |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 221 | reg |= TEGRA20_I2S_TIMING_NON_SYM_ENABLE; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 222 | |
Stephen Warren | 30d436a | 2012-03-30 17:07:16 -0600 | [diff] [blame] | 223 | clk_enable(i2s->clk_i2s); |
Stephen Warren | 713d136 | 2011-07-01 13:56:13 -0600 | [diff] [blame] | 224 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 225 | tegra20_i2s_write(i2s, TEGRA20_I2S_TIMING, reg); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 226 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 227 | tegra20_i2s_write(i2s, TEGRA20_I2S_FIFO_SCR, |
| 228 | TEGRA20_I2S_FIFO_SCR_FIFO2_ATN_LVL_FOUR_SLOTS | |
| 229 | TEGRA20_I2S_FIFO_SCR_FIFO1_ATN_LVL_FOUR_SLOTS); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 230 | |
Stephen Warren | 30d436a | 2012-03-30 17:07:16 -0600 | [diff] [blame] | 231 | clk_disable(i2s->clk_i2s); |
Stephen Warren | 713d136 | 2011-07-01 13:56:13 -0600 | [diff] [blame] | 232 | |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 236 | static void tegra20_i2s_start_playback(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 237 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 238 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_FIFO1_ENABLE; |
| 239 | tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 242 | static void tegra20_i2s_stop_playback(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 243 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 244 | i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_FIFO1_ENABLE; |
| 245 | tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 248 | static void tegra20_i2s_start_capture(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 249 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 250 | i2s->reg_ctrl |= TEGRA20_I2S_CTRL_FIFO2_ENABLE; |
| 251 | tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 254 | static void tegra20_i2s_stop_capture(struct tegra20_i2s *i2s) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 255 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 256 | i2s->reg_ctrl &= ~TEGRA20_I2S_CTRL_FIFO2_ENABLE; |
| 257 | tegra20_i2s_write(i2s, TEGRA20_I2S_CTRL, i2s->reg_ctrl); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 260 | static int tegra20_i2s_trigger(struct snd_pcm_substream *substream, int cmd, |
| 261 | struct snd_soc_dai *dai) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 262 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 263 | struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 264 | |
| 265 | switch (cmd) { |
| 266 | case SNDRV_PCM_TRIGGER_START: |
| 267 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 268 | case SNDRV_PCM_TRIGGER_RESUME: |
Stephen Warren | 30d436a | 2012-03-30 17:07:16 -0600 | [diff] [blame] | 269 | clk_enable(i2s->clk_i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 270 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 271 | tegra20_i2s_start_playback(i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 272 | else |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 273 | tegra20_i2s_start_capture(i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 274 | break; |
| 275 | case SNDRV_PCM_TRIGGER_STOP: |
| 276 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 277 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 278 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 279 | tegra20_i2s_stop_playback(i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 280 | else |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 281 | tegra20_i2s_stop_capture(i2s); |
Stephen Warren | 30d436a | 2012-03-30 17:07:16 -0600 | [diff] [blame] | 282 | clk_disable(i2s->clk_i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 283 | break; |
| 284 | default: |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 291 | static int tegra20_i2s_probe(struct snd_soc_dai *dai) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 292 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 293 | struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 294 | |
| 295 | dai->capture_dma_data = &i2s->capture_dma_data; |
| 296 | dai->playback_dma_data = &i2s->playback_dma_data; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 301 | static const struct snd_soc_dai_ops tegra20_i2s_dai_ops = { |
| 302 | .set_fmt = tegra20_i2s_set_fmt, |
| 303 | .hw_params = tegra20_i2s_hw_params, |
| 304 | .trigger = tegra20_i2s_trigger, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 305 | }; |
| 306 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 307 | static const struct snd_soc_dai_driver tegra20_i2s_dai_template = { |
| 308 | .probe = tegra20_i2s_probe, |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 309 | .playback = { |
| 310 | .channels_min = 2, |
| 311 | .channels_max = 2, |
| 312 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 313 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 314 | }, |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 315 | .capture = { |
| 316 | .channels_min = 2, |
| 317 | .channels_max = 2, |
| 318 | .rates = SNDRV_PCM_RATE_8000_96000, |
| 319 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 320 | }, |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 321 | .ops = &tegra20_i2s_dai_ops, |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 322 | .symmetric_rates = 1, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 325 | static __devinit int tegra20_i2s_platform_probe(struct platform_device *pdev) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 326 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 327 | struct tegra20_i2s *i2s; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 328 | struct resource *mem, *memregion, *dmareq; |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 329 | u32 of_dma[2]; |
| 330 | u32 dma_ch; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 331 | int ret; |
| 332 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 333 | i2s = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_i2s), GFP_KERNEL); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 334 | if (!i2s) { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 335 | dev_err(&pdev->dev, "Can't allocate tegra20_i2s\n"); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 336 | ret = -ENOMEM; |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 337 | goto err; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 338 | } |
| 339 | dev_set_drvdata(&pdev->dev, i2s); |
| 340 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 341 | i2s->dai = tegra20_i2s_dai_template; |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 342 | i2s->dai.name = dev_name(&pdev->dev); |
| 343 | |
Stephen Warren | b5f9cfe | 2011-07-01 13:56:14 -0600 | [diff] [blame] | 344 | i2s->clk_i2s = clk_get(&pdev->dev, NULL); |
Stephen Warren | 422650e | 2011-01-11 12:48:53 -0700 | [diff] [blame] | 345 | if (IS_ERR(i2s->clk_i2s)) { |
Stephen Warren | 713dce4 | 2011-01-28 14:26:41 -0700 | [diff] [blame] | 346 | dev_err(&pdev->dev, "Can't retrieve i2s clock\n"); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 347 | ret = PTR_ERR(i2s->clk_i2s); |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 348 | goto err; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 352 | if (!mem) { |
| 353 | dev_err(&pdev->dev, "No memory resource\n"); |
| 354 | ret = -ENODEV; |
| 355 | goto err_clk_put; |
| 356 | } |
| 357 | |
| 358 | dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 359 | if (!dmareq) { |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 360 | if (of_property_read_u32_array(pdev->dev.of_node, |
| 361 | "nvidia,dma-request-selector", |
| 362 | of_dma, 2) < 0) { |
| 363 | dev_err(&pdev->dev, "No DMA resource\n"); |
| 364 | ret = -ENODEV; |
| 365 | goto err_clk_put; |
| 366 | } |
| 367 | dma_ch = of_dma[1]; |
| 368 | } else { |
| 369 | dma_ch = dmareq->start; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 372 | memregion = devm_request_mem_region(&pdev->dev, mem->start, |
| 373 | resource_size(mem), DRV_NAME); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 374 | if (!memregion) { |
| 375 | dev_err(&pdev->dev, "Memory region already claimed\n"); |
| 376 | ret = -EBUSY; |
| 377 | goto err_clk_put; |
| 378 | } |
| 379 | |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 380 | i2s->regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem)); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 381 | if (!i2s->regs) { |
| 382 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 383 | ret = -ENOMEM; |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 384 | goto err_clk_put; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 387 | i2s->capture_dma_data.addr = mem->start + TEGRA20_I2S_FIFO2; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 388 | i2s->capture_dma_data.wrap = 4; |
| 389 | i2s->capture_dma_data.width = 32; |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 390 | i2s->capture_dma_data.req_sel = dma_ch; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 391 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 392 | i2s->playback_dma_data.addr = mem->start + TEGRA20_I2S_FIFO1; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 393 | i2s->playback_dma_data.wrap = 4; |
| 394 | i2s->playback_dma_data.width = 32; |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 395 | i2s->playback_dma_data.req_sel = dma_ch; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 396 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 397 | i2s->reg_ctrl = TEGRA20_I2S_CTRL_FIFO_FORMAT_PACKED; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 398 | |
Stephen Warren | d4a2eca | 2011-11-23 13:33:25 -0700 | [diff] [blame] | 399 | ret = snd_soc_register_dai(&pdev->dev, &i2s->dai); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 400 | if (ret) { |
| 401 | dev_err(&pdev->dev, "Could not register DAI: %d\n", ret); |
| 402 | ret = -ENOMEM; |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 403 | goto err_clk_put; |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame] | 406 | ret = tegra_pcm_platform_register(&pdev->dev); |
| 407 | if (ret) { |
| 408 | dev_err(&pdev->dev, "Could not register PCM: %d\n", ret); |
| 409 | goto err_unregister_dai; |
| 410 | } |
| 411 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 412 | tegra20_i2s_debug_add(i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 413 | |
| 414 | return 0; |
| 415 | |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame] | 416 | err_unregister_dai: |
| 417 | snd_soc_unregister_dai(&pdev->dev); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 418 | err_clk_put: |
| 419 | clk_put(i2s->clk_i2s); |
Stephen Warren | bea0ed0 | 2011-11-22 18:21:16 -0700 | [diff] [blame] | 420 | err: |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 421 | return ret; |
| 422 | } |
| 423 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 424 | static int __devexit tegra20_i2s_platform_remove(struct platform_device *pdev) |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 425 | { |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 426 | struct tegra20_i2s *i2s = dev_get_drvdata(&pdev->dev); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 427 | |
Stephen Warren | 518de86 | 2012-03-20 14:55:49 -0600 | [diff] [blame] | 428 | tegra_pcm_platform_unregister(&pdev->dev); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 429 | snd_soc_unregister_dai(&pdev->dev); |
| 430 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 431 | tegra20_i2s_debug_remove(i2s); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 432 | |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 433 | clk_put(i2s->clk_i2s); |
| 434 | |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 438 | static const struct of_device_id tegra20_i2s_of_match[] __devinitconst = { |
Stephen Warren | bf55499 | 2011-11-29 18:36:48 -0700 | [diff] [blame] | 439 | { .compatible = "nvidia,tegra20-i2s", }, |
| 440 | {}, |
| 441 | }; |
| 442 | |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 443 | static struct platform_driver tegra20_i2s_driver = { |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 444 | .driver = { |
| 445 | .name = DRV_NAME, |
| 446 | .owner = THIS_MODULE, |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 447 | .of_match_table = tegra20_i2s_of_match, |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 448 | }, |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 449 | .probe = tegra20_i2s_platform_probe, |
| 450 | .remove = __devexit_p(tegra20_i2s_platform_remove), |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 451 | }; |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 452 | module_platform_driver(tegra20_i2s_driver); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 453 | |
| 454 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 455 | MODULE_DESCRIPTION("Tegra20 I2S ASoC driver"); |
Stephen Warren | 71f78e2 | 2011-01-07 22:36:14 -0700 | [diff] [blame] | 456 | MODULE_LICENSE("GPL"); |
Stephen Warren | 8eb3420 | 2011-02-10 15:37:19 -0700 | [diff] [blame] | 457 | MODULE_ALIAS("platform:" DRV_NAME); |
Stephen Warren | 896637a | 2012-04-06 10:30:52 -0600 | [diff] [blame^] | 458 | MODULE_DEVICE_TABLE(of, tegra20_i2s_of_match); |