apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kirkwood-i2s.c |
| 3 | * |
| 4 | * (c) 2010 Arnaud Patard <apatard@mandriva.com> |
Arnaud Patard (Rtp) | 6973789 | 2010-09-09 14:10:33 +0200 | [diff] [blame] | 5 | * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org> |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/mbus.h> |
| 19 | #include <linux/delay.h> |
Andrew Lunn | e919c71 | 2012-03-09 09:56:41 +0100 | [diff] [blame] | 20 | #include <linux/clk.h> |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 21 | #include <sound/pcm.h> |
| 22 | #include <sound/pcm_params.h> |
| 23 | #include <sound/soc.h> |
Arnd Bergmann | c02cecb | 2012-08-24 15:21:54 +0200 | [diff] [blame] | 24 | #include <linux/platform_data/asoc-kirkwood.h> |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 25 | #include "kirkwood.h" |
| 26 | |
| 27 | #define DRV_NAME "kirkwood-i2s" |
| 28 | |
| 29 | #define KIRKWOOD_I2S_RATES \ |
| 30 | (SNDRV_PCM_RATE_44100 | \ |
| 31 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000) |
| 32 | #define KIRKWOOD_I2S_FORMATS \ |
| 33 | (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 34 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 35 | SNDRV_PCM_FMTBIT_S32_LE) |
| 36 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 37 | static int kirkwood_i2s_set_fmt(struct snd_soc_dai *cpu_dai, |
| 38 | unsigned int fmt) |
| 39 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 40 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(cpu_dai); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 41 | unsigned long mask; |
| 42 | unsigned long value; |
| 43 | |
| 44 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 45 | case SND_SOC_DAIFMT_RIGHT_J: |
| 46 | mask = KIRKWOOD_I2S_CTL_RJ; |
| 47 | break; |
| 48 | case SND_SOC_DAIFMT_LEFT_J: |
| 49 | mask = KIRKWOOD_I2S_CTL_LJ; |
| 50 | break; |
| 51 | case SND_SOC_DAIFMT_I2S: |
| 52 | mask = KIRKWOOD_I2S_CTL_I2S; |
| 53 | break; |
| 54 | default: |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Set same format for playback and record |
| 60 | * This avoids some troubles. |
| 61 | */ |
| 62 | value = readl(priv->io+KIRKWOOD_I2S_PLAYCTL); |
| 63 | value &= ~KIRKWOOD_I2S_CTL_JUST_MASK; |
| 64 | value |= mask; |
| 65 | writel(value, priv->io+KIRKWOOD_I2S_PLAYCTL); |
| 66 | |
| 67 | value = readl(priv->io+KIRKWOOD_I2S_RECCTL); |
| 68 | value &= ~KIRKWOOD_I2S_CTL_JUST_MASK; |
| 69 | value |= mask; |
| 70 | writel(value, priv->io+KIRKWOOD_I2S_RECCTL); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline void kirkwood_set_dco(void __iomem *io, unsigned long rate) |
| 76 | { |
| 77 | unsigned long value; |
| 78 | |
| 79 | value = KIRKWOOD_DCO_CTL_OFFSET_0; |
| 80 | switch (rate) { |
| 81 | default: |
| 82 | case 44100: |
| 83 | value |= KIRKWOOD_DCO_CTL_FREQ_11; |
| 84 | break; |
| 85 | case 48000: |
| 86 | value |= KIRKWOOD_DCO_CTL_FREQ_12; |
| 87 | break; |
| 88 | case 96000: |
| 89 | value |= KIRKWOOD_DCO_CTL_FREQ_24; |
| 90 | break; |
| 91 | } |
| 92 | writel(value, io + KIRKWOOD_DCO_CTL); |
| 93 | |
| 94 | /* wait for dco locked */ |
| 95 | do { |
| 96 | cpu_relax(); |
| 97 | value = readl(io + KIRKWOOD_DCO_SPCR_STATUS); |
Russell King | 2424d45 | 2012-11-20 12:18:32 +0000 | [diff] [blame] | 98 | value &= KIRKWOOD_DCO_SPCR_STATUS_DCO_LOCK; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 99 | } while (value == 0); |
| 100 | } |
| 101 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 102 | static int kirkwood_i2s_startup(struct snd_pcm_substream *substream, |
| 103 | struct snd_soc_dai *dai) |
| 104 | { |
| 105 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai); |
| 106 | |
| 107 | snd_soc_dai_set_dma_data(dai, substream, priv); |
| 108 | return 0; |
| 109 | } |
| 110 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 111 | static int kirkwood_i2s_hw_params(struct snd_pcm_substream *substream, |
| 112 | struct snd_pcm_hw_params *params, |
| 113 | struct snd_soc_dai *dai) |
| 114 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 115 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai); |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 116 | uint32_t ctl_play, ctl_rec; |
| 117 | unsigned int i2s_reg; |
| 118 | unsigned long i2s_value; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 119 | |
| 120 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 121 | i2s_reg = KIRKWOOD_I2S_PLAYCTL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 122 | } else { |
| 123 | i2s_reg = KIRKWOOD_I2S_RECCTL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* set dco conf */ |
| 127 | kirkwood_set_dco(priv->io, params_rate(params)); |
| 128 | |
| 129 | i2s_value = readl(priv->io+i2s_reg); |
| 130 | i2s_value &= ~KIRKWOOD_I2S_CTL_SIZE_MASK; |
| 131 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Size settings in play/rec i2s control regs and play/rec control |
| 134 | * regs must be the same. |
| 135 | */ |
| 136 | switch (params_format(params)) { |
| 137 | case SNDRV_PCM_FORMAT_S16_LE: |
| 138 | i2s_value |= KIRKWOOD_I2S_CTL_SIZE_16; |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 139 | ctl_play = KIRKWOOD_PLAYCTL_SIZE_16_C | |
| 140 | KIRKWOOD_PLAYCTL_I2S_EN; |
| 141 | ctl_rec = KIRKWOOD_RECCTL_SIZE_16_C | |
| 142 | KIRKWOOD_RECCTL_I2S_EN; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 143 | break; |
| 144 | /* |
| 145 | * doesn't work... S20_3LE != kirkwood 20bit format ? |
| 146 | * |
| 147 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 148 | i2s_value |= KIRKWOOD_I2S_CTL_SIZE_20; |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 149 | ctl_play = KIRKWOOD_PLAYCTL_SIZE_20 | |
| 150 | KIRKWOOD_PLAYCTL_I2S_EN; |
| 151 | ctl_rec = KIRKWOOD_RECCTL_SIZE_20 | |
| 152 | KIRKWOOD_RECCTL_I2S_EN; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 153 | break; |
| 154 | */ |
| 155 | case SNDRV_PCM_FORMAT_S24_LE: |
| 156 | i2s_value |= KIRKWOOD_I2S_CTL_SIZE_24; |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 157 | ctl_play = KIRKWOOD_PLAYCTL_SIZE_24 | |
| 158 | KIRKWOOD_PLAYCTL_I2S_EN; |
| 159 | ctl_rec = KIRKWOOD_RECCTL_SIZE_24 | |
| 160 | KIRKWOOD_RECCTL_I2S_EN; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 161 | break; |
| 162 | case SNDRV_PCM_FORMAT_S32_LE: |
| 163 | i2s_value |= KIRKWOOD_I2S_CTL_SIZE_32; |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 164 | ctl_play = KIRKWOOD_PLAYCTL_SIZE_32 | |
| 165 | KIRKWOOD_PLAYCTL_I2S_EN; |
| 166 | ctl_rec = KIRKWOOD_RECCTL_SIZE_32 | |
| 167 | KIRKWOOD_RECCTL_I2S_EN; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 168 | break; |
| 169 | default: |
| 170 | return -EINVAL; |
| 171 | } |
arnaud.patard@rtp-net.org | dfe4c93 | 2010-07-11 23:28:31 +0200 | [diff] [blame] | 172 | |
| 173 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
arnaud.patard@rtp-net.org | dfe4c93 | 2010-07-11 23:28:31 +0200 | [diff] [blame] | 174 | if (params_channels(params) == 1) |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 175 | ctl_play |= KIRKWOOD_PLAYCTL_MONO_BOTH; |
arnaud.patard@rtp-net.org | dfe4c93 | 2010-07-11 23:28:31 +0200 | [diff] [blame] | 176 | else |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 177 | ctl_play |= KIRKWOOD_PLAYCTL_MONO_OFF; |
| 178 | |
| 179 | priv->ctl_play &= ~(KIRKWOOD_PLAYCTL_MONO_MASK | |
| 180 | KIRKWOOD_PLAYCTL_I2S_EN | |
| 181 | KIRKWOOD_PLAYCTL_SPDIF_EN | |
| 182 | KIRKWOOD_PLAYCTL_SIZE_MASK); |
| 183 | priv->ctl_play |= ctl_play; |
| 184 | } else { |
| 185 | priv->ctl_rec &= ~KIRKWOOD_RECCTL_SIZE_MASK; |
| 186 | priv->ctl_rec |= ctl_rec; |
arnaud.patard@rtp-net.org | dfe4c93 | 2010-07-11 23:28:31 +0200 | [diff] [blame] | 187 | } |
| 188 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 189 | writel(i2s_value, priv->io+i2s_reg); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int kirkwood_i2s_play_trigger(struct snd_pcm_substream *substream, |
| 195 | int cmd, struct snd_soc_dai *dai) |
| 196 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 197 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai); |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 198 | uint32_t ctl, value; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 199 | |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 200 | ctl = readl(priv->io + KIRKWOOD_PLAYCTL); |
| 201 | if (ctl & KIRKWOOD_PLAYCTL_PAUSE) { |
| 202 | unsigned timeout = 5000; |
| 203 | /* |
| 204 | * The Armada510 spec says that if we enter pause mode, the |
| 205 | * busy bit must be read back as clear _twice_. Make sure |
| 206 | * we respect that otherwise we get DMA underruns. |
| 207 | */ |
| 208 | do { |
| 209 | value = ctl; |
| 210 | ctl = readl(priv->io + KIRKWOOD_PLAYCTL); |
| 211 | if (!((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY)) |
| 212 | break; |
| 213 | udelay(1); |
| 214 | } while (timeout--); |
| 215 | |
| 216 | if ((ctl | value) & KIRKWOOD_PLAYCTL_PLAY_BUSY) |
| 217 | dev_notice(dai->dev, "timed out waiting for busy to deassert: %08x\n", |
| 218 | ctl); |
| 219 | } |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 220 | |
| 221 | switch (cmd) { |
| 222 | case SNDRV_PCM_TRIGGER_START: |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 223 | /* configure */ |
| 224 | ctl = priv->ctl_play; |
| 225 | value = ctl & ~(KIRKWOOD_PLAYCTL_I2S_EN | |
| 226 | KIRKWOOD_PLAYCTL_SPDIF_EN); |
| 227 | writel(value, priv->io + KIRKWOOD_PLAYCTL); |
| 228 | |
| 229 | /* enable interrupts */ |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 230 | value = readl(priv->io + KIRKWOOD_INT_MASK); |
| 231 | value |= KIRKWOOD_INT_CAUSE_PLAY_BYTES; |
| 232 | writel(value, priv->io + KIRKWOOD_INT_MASK); |
| 233 | |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 234 | /* enable playback */ |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 235 | writel(ctl, priv->io + KIRKWOOD_PLAYCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 236 | break; |
| 237 | |
| 238 | case SNDRV_PCM_TRIGGER_STOP: |
| 239 | /* stop audio, disable interrupts */ |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 240 | ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE; |
| 241 | writel(ctl, priv->io + KIRKWOOD_PLAYCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 242 | |
| 243 | value = readl(priv->io + KIRKWOOD_INT_MASK); |
| 244 | value &= ~KIRKWOOD_INT_CAUSE_PLAY_BYTES; |
| 245 | writel(value, priv->io + KIRKWOOD_INT_MASK); |
| 246 | |
| 247 | /* disable all playbacks */ |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 248 | ctl &= ~(KIRKWOOD_PLAYCTL_I2S_EN | KIRKWOOD_PLAYCTL_SPDIF_EN); |
| 249 | writel(ctl, priv->io + KIRKWOOD_PLAYCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 250 | break; |
| 251 | |
| 252 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 253 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 254 | ctl |= KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE; |
| 255 | writel(ctl, priv->io + KIRKWOOD_PLAYCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case SNDRV_PCM_TRIGGER_RESUME: |
| 259 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Russell King | 982b604 | 2012-11-20 12:18:52 +0000 | [diff] [blame] | 260 | ctl &= ~(KIRKWOOD_PLAYCTL_PAUSE | KIRKWOOD_PLAYCTL_I2S_MUTE); |
| 261 | writel(ctl, priv->io + KIRKWOOD_PLAYCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | default: |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int kirkwood_i2s_rec_trigger(struct snd_pcm_substream *substream, |
| 272 | int cmd, struct snd_soc_dai *dai) |
| 273 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 274 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai); |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 275 | uint32_t ctl, value; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 276 | |
| 277 | value = readl(priv->io + KIRKWOOD_RECCTL); |
| 278 | |
| 279 | switch (cmd) { |
| 280 | case SNDRV_PCM_TRIGGER_START: |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 281 | /* configure */ |
| 282 | ctl = priv->ctl_rec; |
| 283 | value = ctl & ~KIRKWOOD_RECCTL_I2S_EN; |
| 284 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 285 | |
| 286 | /* enable interrupts */ |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 287 | value = readl(priv->io + KIRKWOOD_INT_MASK); |
| 288 | value |= KIRKWOOD_INT_CAUSE_REC_BYTES; |
| 289 | writel(value, priv->io + KIRKWOOD_INT_MASK); |
| 290 | |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 291 | /* enable record */ |
| 292 | writel(ctl, priv->io + KIRKWOOD_RECCTL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case SNDRV_PCM_TRIGGER_STOP: |
| 296 | /* stop audio, disable interrupts */ |
| 297 | value = readl(priv->io + KIRKWOOD_RECCTL); |
arnaud.patard@rtp-net.org | b424ec9 | 2010-07-11 23:28:32 +0200 | [diff] [blame] | 298 | value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 299 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 300 | |
| 301 | value = readl(priv->io + KIRKWOOD_INT_MASK); |
| 302 | value &= ~KIRKWOOD_INT_CAUSE_REC_BYTES; |
| 303 | writel(value, priv->io + KIRKWOOD_INT_MASK); |
| 304 | |
| 305 | /* disable all records */ |
| 306 | value = readl(priv->io + KIRKWOOD_RECCTL); |
| 307 | value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN); |
| 308 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 309 | break; |
| 310 | |
| 311 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 312 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 313 | value = readl(priv->io + KIRKWOOD_RECCTL); |
arnaud.patard@rtp-net.org | b424ec9 | 2010-07-11 23:28:32 +0200 | [diff] [blame] | 314 | value |= KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 315 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 316 | break; |
| 317 | |
| 318 | case SNDRV_PCM_TRIGGER_RESUME: |
| 319 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 320 | value = readl(priv->io + KIRKWOOD_RECCTL); |
arnaud.patard@rtp-net.org | b424ec9 | 2010-07-11 23:28:32 +0200 | [diff] [blame] | 321 | value &= ~(KIRKWOOD_RECCTL_PAUSE | KIRKWOOD_RECCTL_MUTE); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 322 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 323 | break; |
| 324 | |
| 325 | default: |
| 326 | return -EINVAL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int kirkwood_i2s_trigger(struct snd_pcm_substream *substream, int cmd, |
| 333 | struct snd_soc_dai *dai) |
| 334 | { |
| 335 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 336 | return kirkwood_i2s_play_trigger(substream, cmd, dai); |
| 337 | else |
| 338 | return kirkwood_i2s_rec_trigger(substream, cmd, dai); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 343 | static int kirkwood_i2s_probe(struct snd_soc_dai *dai) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 344 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 345 | struct kirkwood_dma_data *priv = snd_soc_dai_get_drvdata(dai); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 346 | unsigned long value; |
| 347 | unsigned int reg_data; |
| 348 | |
| 349 | /* put system in a "safe" state : */ |
| 350 | /* disable audio interrupts */ |
| 351 | writel(0xffffffff, priv->io + KIRKWOOD_INT_CAUSE); |
| 352 | writel(0, priv->io + KIRKWOOD_INT_MASK); |
| 353 | |
| 354 | reg_data = readl(priv->io + 0x1200); |
| 355 | reg_data &= (~(0x333FF8)); |
| 356 | reg_data |= 0x111D18; |
| 357 | writel(reg_data, priv->io + 0x1200); |
| 358 | |
| 359 | msleep(500); |
| 360 | |
| 361 | reg_data = readl(priv->io + 0x1200); |
| 362 | reg_data &= (~(0x333FF8)); |
| 363 | reg_data |= 0x111D18; |
| 364 | writel(reg_data, priv->io + 0x1200); |
| 365 | |
| 366 | /* disable playback/record */ |
| 367 | value = readl(priv->io + KIRKWOOD_PLAYCTL); |
| 368 | value &= ~(KIRKWOOD_PLAYCTL_I2S_EN|KIRKWOOD_PLAYCTL_SPDIF_EN); |
| 369 | writel(value, priv->io + KIRKWOOD_PLAYCTL); |
| 370 | |
| 371 | value = readl(priv->io + KIRKWOOD_RECCTL); |
| 372 | value &= ~(KIRKWOOD_RECCTL_I2S_EN | KIRKWOOD_RECCTL_SPDIF_EN); |
| 373 | writel(value, priv->io + KIRKWOOD_RECCTL); |
| 374 | |
| 375 | return 0; |
| 376 | |
| 377 | } |
| 378 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 379 | static int kirkwood_i2s_remove(struct snd_soc_dai *dai) |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 380 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 381 | return 0; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 384 | static const struct snd_soc_dai_ops kirkwood_i2s_dai_ops = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 385 | .startup = kirkwood_i2s_startup, |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 386 | .trigger = kirkwood_i2s_trigger, |
| 387 | .hw_params = kirkwood_i2s_hw_params, |
| 388 | .set_fmt = kirkwood_i2s_set_fmt, |
| 389 | }; |
| 390 | |
| 391 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 392 | static struct snd_soc_dai_driver kirkwood_i2s_dai = { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 393 | .probe = kirkwood_i2s_probe, |
| 394 | .remove = kirkwood_i2s_remove, |
| 395 | .playback = { |
| 396 | .channels_min = 1, |
| 397 | .channels_max = 2, |
| 398 | .rates = KIRKWOOD_I2S_RATES, |
| 399 | .formats = KIRKWOOD_I2S_FORMATS,}, |
| 400 | .capture = { |
| 401 | .channels_min = 1, |
| 402 | .channels_max = 2, |
| 403 | .rates = KIRKWOOD_I2S_RATES, |
| 404 | .formats = KIRKWOOD_I2S_FORMATS,}, |
| 405 | .ops = &kirkwood_i2s_dai_ops, |
| 406 | }; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 407 | |
| 408 | static __devinit int kirkwood_i2s_dev_probe(struct platform_device *pdev) |
| 409 | { |
| 410 | struct resource *mem; |
| 411 | struct kirkwood_asoc_platform_data *data = |
| 412 | pdev->dev.platform_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 413 | struct kirkwood_dma_data *priv; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 414 | int err; |
| 415 | |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 416 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 417 | if (!priv) { |
| 418 | dev_err(&pdev->dev, "allocation failed\n"); |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 419 | return -ENOMEM; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 420 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 421 | dev_set_drvdata(&pdev->dev, priv); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 422 | |
| 423 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 424 | if (!mem) { |
| 425 | dev_err(&pdev->dev, "platform_get_resource failed\n"); |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 426 | return -ENXIO; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 429 | priv->io = devm_request_and_ioremap(&pdev->dev, mem); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 430 | if (!priv->io) { |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 431 | dev_err(&pdev->dev, "devm_request_and_ioremap failed\n"); |
| 432 | return -ENOMEM; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | priv->irq = platform_get_irq(pdev, 0); |
| 436 | if (priv->irq <= 0) { |
| 437 | dev_err(&pdev->dev, "platform_get_irq failed\n"); |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 438 | return -ENXIO; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Axel Lin | d4d9820 | 2011-12-28 16:05:22 +0800 | [diff] [blame] | 441 | if (!data) { |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 442 | dev_err(&pdev->dev, "no platform data ?!\n"); |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 443 | return -EINVAL; |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 444 | } |
| 445 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 446 | priv->burst = data->burst; |
| 447 | |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 448 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
Andrew Lunn | e919c71 | 2012-03-09 09:56:41 +0100 | [diff] [blame] | 449 | if (IS_ERR(priv->clk)) { |
| 450 | dev_err(&pdev->dev, "no clock\n"); |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 451 | return PTR_ERR(priv->clk); |
Andrew Lunn | e919c71 | 2012-03-09 09:56:41 +0100 | [diff] [blame] | 452 | } |
Russell King | dbc517b | 2012-11-20 12:19:33 +0000 | [diff] [blame] | 453 | |
| 454 | err = clk_prepare_enable(priv->clk); |
| 455 | if (err < 0) |
| 456 | return err; |
Andrew Lunn | e919c71 | 2012-03-09 09:56:41 +0100 | [diff] [blame] | 457 | |
Russell King | d8d11ba | 2012-11-20 12:19:53 +0000 | [diff] [blame^] | 458 | priv->extclk = clk_get(&pdev->dev, "extclk"); |
| 459 | if (!IS_ERR(priv->extclk)) { |
| 460 | if (priv->extclk == priv->clk) { |
| 461 | clk_put(priv->extclk); |
| 462 | priv->extclk = ERR_PTR(-EINVAL); |
| 463 | } else { |
| 464 | dev_info(&pdev->dev, "found external clock\n"); |
| 465 | clk_prepare_enable(priv->extclk); |
| 466 | soc_dai = &kirkwood_i2s_dai_extclk; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Some sensible defaults - this reflects the powerup values */ |
| 471 | priv->ctl_play = KIRKWOOD_PLAYCTL_SIZE_24; |
| 472 | priv->ctl_rec = KIRKWOOD_RECCTL_SIZE_24; |
| 473 | |
| 474 | /* Select the burst size */ |
| 475 | if (data->burst == 32) { |
| 476 | priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_32; |
| 477 | priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_32; |
| 478 | } else { |
| 479 | priv->ctl_play |= KIRKWOOD_PLAYCTL_BURST_128; |
| 480 | priv->ctl_rec |= KIRKWOOD_RECCTL_BURST_128; |
| 481 | } |
| 482 | |
Simon Baatz | baffab2 | 2012-07-19 00:04:09 +0200 | [diff] [blame] | 483 | err = snd_soc_register_dai(&pdev->dev, &kirkwood_i2s_dai); |
| 484 | if (!err) |
| 485 | return 0; |
| 486 | dev_err(&pdev->dev, "snd_soc_register_dai failed\n"); |
| 487 | |
| 488 | clk_disable_unprepare(priv->clk); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 489 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 490 | return err; |
| 491 | } |
| 492 | |
| 493 | static __devexit int kirkwood_i2s_dev_remove(struct platform_device *pdev) |
| 494 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 495 | struct kirkwood_dma_data *priv = dev_get_drvdata(&pdev->dev); |
| 496 | |
| 497 | snd_soc_unregister_dai(&pdev->dev); |
Andrew Lunn | e919c71 | 2012-03-09 09:56:41 +0100 | [diff] [blame] | 498 | |
| 499 | clk_disable_unprepare(priv->clk); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 500 | |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static struct platform_driver kirkwood_i2s_driver = { |
| 505 | .probe = kirkwood_i2s_dev_probe, |
Axel Lin | 48860e4 | 2011-10-02 09:18:17 +0800 | [diff] [blame] | 506 | .remove = __devexit_p(kirkwood_i2s_dev_remove), |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 507 | .driver = { |
| 508 | .name = DRV_NAME, |
| 509 | .owner = THIS_MODULE, |
| 510 | }, |
| 511 | }; |
| 512 | |
Axel Lin | 41b1022 | 2011-11-24 11:43:09 +0800 | [diff] [blame] | 513 | module_platform_driver(kirkwood_i2s_driver); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 514 | |
| 515 | /* Module information */ |
Arnaud Patard (Rtp) | 6973789 | 2010-09-09 14:10:33 +0200 | [diff] [blame] | 516 | MODULE_AUTHOR("Arnaud Patard, <arnaud.patard@rtp-net.org>"); |
apatard@mandriva.com | f9b9598 | 2010-05-31 13:49:14 +0200 | [diff] [blame] | 517 | MODULE_DESCRIPTION("Kirkwood I2S SoC Interface"); |
| 518 | MODULE_LICENSE("GPL"); |
| 519 | MODULE_ALIAS("platform:kirkwood-i2s"); |