Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 1 | /* |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 2 | * Driver for Atmel AC97C |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005-2009 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/bitmap.h> |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 14 | #include <linux/atmel_pdc.h> |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/gpio.h> |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 21 | #include <linux/types.h> |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 22 | #include <linux/io.h> |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_gpio.h> |
| 25 | #include <linux/of_device.h> |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 26 | |
| 27 | #include <sound/core.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
| 31 | #include <sound/ac97_codec.h> |
| 32 | #include <sound/atmel-ac97c.h> |
| 33 | #include <sound/memalloc.h> |
| 34 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 35 | #include "ac97c.h" |
| 36 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 37 | /* Serialize access to opened variable */ |
| 38 | static DEFINE_MUTEX(opened_mutex); |
| 39 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 40 | struct atmel_ac97c { |
| 41 | struct clk *pclk; |
| 42 | struct platform_device *pdev; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 43 | |
| 44 | struct snd_pcm_substream *playback_substream; |
| 45 | struct snd_pcm_substream *capture_substream; |
| 46 | struct snd_card *card; |
| 47 | struct snd_pcm *pcm; |
| 48 | struct snd_ac97 *ac97; |
| 49 | struct snd_ac97_bus *ac97_bus; |
| 50 | |
| 51 | u64 cur_format; |
| 52 | unsigned int cur_rate; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 53 | int playback_period, capture_period; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 54 | /* Serialize access to opened variable */ |
| 55 | spinlock_t lock; |
| 56 | void __iomem *regs; |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 57 | int irq; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 58 | int opened; |
| 59 | int reset_pin; |
| 60 | }; |
| 61 | |
| 62 | #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data) |
| 63 | |
| 64 | #define ac97c_writel(chip, reg, val) \ |
| 65 | __raw_writel((val), (chip)->regs + AC97C_##reg) |
| 66 | #define ac97c_readl(chip, reg) \ |
| 67 | __raw_readl((chip)->regs + AC97C_##reg) |
| 68 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 69 | static struct snd_pcm_hardware atmel_ac97c_hw = { |
| 70 | .info = (SNDRV_PCM_INFO_MMAP |
| 71 | | SNDRV_PCM_INFO_MMAP_VALID |
| 72 | | SNDRV_PCM_INFO_INTERLEAVED |
| 73 | | SNDRV_PCM_INFO_BLOCK_TRANSFER |
| 74 | | SNDRV_PCM_INFO_JOINT_DUPLEX |
| 75 | | SNDRV_PCM_INFO_RESUME |
| 76 | | SNDRV_PCM_INFO_PAUSE), |
| 77 | .formats = (SNDRV_PCM_FMTBIT_S16_BE |
| 78 | | SNDRV_PCM_FMTBIT_S16_LE), |
| 79 | .rates = (SNDRV_PCM_RATE_CONTINUOUS), |
| 80 | .rate_min = 4000, |
| 81 | .rate_max = 48000, |
| 82 | .channels_min = 1, |
| 83 | .channels_max = 2, |
Hans-Christian Egtvedt | c42eec0 | 2009-04-02 08:21:13 +0200 | [diff] [blame] | 84 | .buffer_bytes_max = 2 * 2 * 64 * 2048, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 85 | .period_bytes_min = 4096, |
| 86 | .period_bytes_max = 4096, |
Hans-Christian Egtvedt | c42eec0 | 2009-04-02 08:21:13 +0200 | [diff] [blame] | 87 | .periods_min = 6, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 88 | .periods_max = 64, |
| 89 | }; |
| 90 | |
| 91 | static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream) |
| 92 | { |
| 93 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 94 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 95 | |
| 96 | mutex_lock(&opened_mutex); |
| 97 | chip->opened++; |
| 98 | runtime->hw = atmel_ac97c_hw; |
| 99 | if (chip->cur_rate) { |
| 100 | runtime->hw.rate_min = chip->cur_rate; |
| 101 | runtime->hw.rate_max = chip->cur_rate; |
| 102 | } |
| 103 | if (chip->cur_format) |
Eldad Zack | 74c34ca | 2013-04-23 01:00:41 +0200 | [diff] [blame] | 104 | runtime->hw.formats = pcm_format_to_bits(chip->cur_format); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 105 | mutex_unlock(&opened_mutex); |
| 106 | chip->playback_substream = substream; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream) |
| 111 | { |
| 112 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 113 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 114 | |
| 115 | mutex_lock(&opened_mutex); |
| 116 | chip->opened++; |
| 117 | runtime->hw = atmel_ac97c_hw; |
| 118 | if (chip->cur_rate) { |
| 119 | runtime->hw.rate_min = chip->cur_rate; |
| 120 | runtime->hw.rate_max = chip->cur_rate; |
| 121 | } |
| 122 | if (chip->cur_format) |
Eldad Zack | 74c34ca | 2013-04-23 01:00:41 +0200 | [diff] [blame] | 123 | runtime->hw.formats = pcm_format_to_bits(chip->cur_format); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 124 | mutex_unlock(&opened_mutex); |
| 125 | chip->capture_substream = substream; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream) |
| 130 | { |
| 131 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 132 | |
| 133 | mutex_lock(&opened_mutex); |
| 134 | chip->opened--; |
| 135 | if (!chip->opened) { |
| 136 | chip->cur_rate = 0; |
| 137 | chip->cur_format = 0; |
| 138 | } |
| 139 | mutex_unlock(&opened_mutex); |
| 140 | |
| 141 | chip->playback_substream = NULL; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream) |
| 147 | { |
| 148 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 149 | |
| 150 | mutex_lock(&opened_mutex); |
| 151 | chip->opened--; |
| 152 | if (!chip->opened) { |
| 153 | chip->cur_rate = 0; |
| 154 | chip->cur_format = 0; |
| 155 | } |
| 156 | mutex_unlock(&opened_mutex); |
| 157 | |
| 158 | chip->capture_substream = NULL; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream, |
| 164 | struct snd_pcm_hw_params *hw_params) |
| 165 | { |
| 166 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 167 | int retval; |
| 168 | |
| 169 | retval = snd_pcm_lib_malloc_pages(substream, |
| 170 | params_buffer_bytes(hw_params)); |
| 171 | if (retval < 0) |
| 172 | return retval; |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 173 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 174 | /* Set restrictions to params. */ |
| 175 | mutex_lock(&opened_mutex); |
| 176 | chip->cur_rate = params_rate(hw_params); |
| 177 | chip->cur_format = params_format(hw_params); |
| 178 | mutex_unlock(&opened_mutex); |
| 179 | |
| 180 | return retval; |
| 181 | } |
| 182 | |
| 183 | static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream, |
| 184 | struct snd_pcm_hw_params *hw_params) |
| 185 | { |
| 186 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 187 | int retval; |
| 188 | |
| 189 | retval = snd_pcm_lib_malloc_pages(substream, |
| 190 | params_buffer_bytes(hw_params)); |
| 191 | if (retval < 0) |
| 192 | return retval; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 193 | |
| 194 | /* Set restrictions to params. */ |
| 195 | mutex_lock(&opened_mutex); |
| 196 | chip->cur_rate = params_rate(hw_params); |
| 197 | chip->cur_format = params_format(hw_params); |
| 198 | mutex_unlock(&opened_mutex); |
| 199 | |
| 200 | return retval; |
| 201 | } |
| 202 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 203 | static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream) |
| 204 | { |
| 205 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 206 | struct snd_pcm_runtime *runtime = substream->runtime; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 207 | int block_size = frames_to_bytes(runtime, runtime->period_size); |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 208 | unsigned long word = ac97c_readl(chip, OCA); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 209 | int retval; |
| 210 | |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 211 | chip->playback_period = 0; |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 212 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); |
| 213 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 214 | /* assign channels to AC97C channel A */ |
| 215 | switch (runtime->channels) { |
| 216 | case 1: |
| 217 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A); |
| 218 | break; |
| 219 | case 2: |
| 220 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A) |
| 221 | | AC97C_CH_ASSIGN(PCM_RIGHT, A); |
| 222 | break; |
| 223 | default: |
| 224 | /* TODO: support more than two channels */ |
| 225 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 226 | } |
| 227 | ac97c_writel(chip, OCA, word); |
| 228 | |
| 229 | /* configure sample format and size */ |
Sedji Gaouaou | ec2755a | 2010-02-25 18:59:40 +0100 | [diff] [blame] | 230 | word = ac97c_readl(chip, CAMR); |
| 231 | if (chip->opened <= 1) |
| 232 | word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; |
| 233 | else |
| 234 | word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 235 | |
| 236 | switch (runtime->format) { |
| 237 | case SNDRV_PCM_FORMAT_S16_LE: |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 238 | break; |
| 239 | case SNDRV_PCM_FORMAT_S16_BE: /* fall through */ |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 240 | word &= ~(AC97C_CMR_CEM_LITTLE); |
| 241 | break; |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 242 | default: |
| 243 | word = ac97c_readl(chip, OCA); |
| 244 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); |
| 245 | ac97c_writel(chip, OCA, word); |
| 246 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 249 | /* Enable underrun interrupt on channel A */ |
| 250 | word |= AC97C_CSR_UNRUN; |
| 251 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 252 | ac97c_writel(chip, CAMR, word); |
| 253 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 254 | /* Enable channel A event interrupt */ |
| 255 | word = ac97c_readl(chip, IMR); |
| 256 | word |= AC97C_SR_CAEVT; |
| 257 | ac97c_writel(chip, IER, word); |
| 258 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 259 | /* set variable rate if needed */ |
| 260 | if (runtime->rate != 48000) { |
| 261 | word = ac97c_readl(chip, MR); |
| 262 | word |= AC97C_MR_VRA; |
| 263 | ac97c_writel(chip, MR, word); |
| 264 | } else { |
| 265 | word = ac97c_readl(chip, MR); |
| 266 | word &= ~(AC97C_MR_VRA); |
| 267 | ac97c_writel(chip, MR, word); |
| 268 | } |
| 269 | |
| 270 | retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, |
| 271 | runtime->rate); |
| 272 | if (retval) |
| 273 | dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n", |
| 274 | runtime->rate); |
| 275 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 276 | /* Initialize and start the PDC */ |
| 277 | writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR); |
| 278 | writel(block_size / 2, chip->regs + ATMEL_PDC_TCR); |
| 279 | writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_TNPR); |
| 280 | writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 281 | |
| 282 | return retval; |
| 283 | } |
| 284 | |
| 285 | static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream) |
| 286 | { |
| 287 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 288 | struct snd_pcm_runtime *runtime = substream->runtime; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 289 | int block_size = frames_to_bytes(runtime, runtime->period_size); |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 290 | unsigned long word = ac97c_readl(chip, ICA); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 291 | int retval; |
| 292 | |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 293 | chip->capture_period = 0; |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 294 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); |
| 295 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 296 | /* assign channels to AC97C channel A */ |
| 297 | switch (runtime->channels) { |
| 298 | case 1: |
| 299 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A); |
| 300 | break; |
| 301 | case 2: |
| 302 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A) |
| 303 | | AC97C_CH_ASSIGN(PCM_RIGHT, A); |
| 304 | break; |
| 305 | default: |
| 306 | /* TODO: support more than two channels */ |
| 307 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 308 | } |
| 309 | ac97c_writel(chip, ICA, word); |
| 310 | |
| 311 | /* configure sample format and size */ |
Sedji Gaouaou | ec2755a | 2010-02-25 18:59:40 +0100 | [diff] [blame] | 312 | word = ac97c_readl(chip, CAMR); |
| 313 | if (chip->opened <= 1) |
| 314 | word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; |
| 315 | else |
| 316 | word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 317 | |
| 318 | switch (runtime->format) { |
| 319 | case SNDRV_PCM_FORMAT_S16_LE: |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 320 | break; |
| 321 | case SNDRV_PCM_FORMAT_S16_BE: /* fall through */ |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 322 | word &= ~(AC97C_CMR_CEM_LITTLE); |
| 323 | break; |
Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 324 | default: |
| 325 | word = ac97c_readl(chip, ICA); |
| 326 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); |
| 327 | ac97c_writel(chip, ICA, word); |
| 328 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 331 | /* Enable overrun interrupt on channel A */ |
| 332 | word |= AC97C_CSR_OVRUN; |
| 333 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 334 | ac97c_writel(chip, CAMR, word); |
| 335 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 336 | /* Enable channel A event interrupt */ |
| 337 | word = ac97c_readl(chip, IMR); |
| 338 | word |= AC97C_SR_CAEVT; |
| 339 | ac97c_writel(chip, IER, word); |
| 340 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 341 | /* set variable rate if needed */ |
| 342 | if (runtime->rate != 48000) { |
| 343 | word = ac97c_readl(chip, MR); |
| 344 | word |= AC97C_MR_VRA; |
| 345 | ac97c_writel(chip, MR, word); |
| 346 | } else { |
| 347 | word = ac97c_readl(chip, MR); |
| 348 | word &= ~(AC97C_MR_VRA); |
| 349 | ac97c_writel(chip, MR, word); |
| 350 | } |
| 351 | |
| 352 | retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, |
| 353 | runtime->rate); |
| 354 | if (retval) |
| 355 | dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n", |
| 356 | runtime->rate); |
| 357 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 358 | /* Initialize and start the PDC */ |
| 359 | writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR); |
| 360 | writel(block_size / 2, chip->regs + ATMEL_PDC_RCR); |
| 361 | writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_RNPR); |
| 362 | writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 363 | |
| 364 | return retval; |
| 365 | } |
| 366 | |
| 367 | static int |
| 368 | atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 369 | { |
| 370 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 371 | unsigned long camr, ptcr = 0; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 372 | |
| 373 | camr = ac97c_readl(chip, CAMR); |
| 374 | |
| 375 | switch (cmd) { |
| 376 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ |
| 377 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ |
| 378 | case SNDRV_PCM_TRIGGER_START: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 379 | ptcr = ATMEL_PDC_TXTEN; |
Sedji Gaouaou | ec2755a | 2010-02-25 18:59:40 +0100 | [diff] [blame] | 380 | camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 381 | break; |
| 382 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ |
| 383 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ |
| 384 | case SNDRV_PCM_TRIGGER_STOP: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 385 | ptcr |= ATMEL_PDC_TXTDIS; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 386 | if (chip->opened <= 1) |
| 387 | camr &= ~AC97C_CMR_CENA; |
| 388 | break; |
| 389 | default: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 390 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | ac97c_writel(chip, CAMR, camr); |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 394 | writel(ptcr, chip->regs + ATMEL_PDC_PTCR); |
| 395 | return 0; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static int |
| 399 | atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd) |
| 400 | { |
| 401 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 402 | unsigned long camr, ptcr = 0; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 403 | |
| 404 | camr = ac97c_readl(chip, CAMR); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 405 | ptcr = readl(chip->regs + ATMEL_PDC_PTSR); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 406 | |
| 407 | switch (cmd) { |
| 408 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ |
| 409 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ |
| 410 | case SNDRV_PCM_TRIGGER_START: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 411 | ptcr = ATMEL_PDC_RXTEN; |
Sedji Gaouaou | ec2755a | 2010-02-25 18:59:40 +0100 | [diff] [blame] | 412 | camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 413 | break; |
| 414 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ |
| 415 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ |
| 416 | case SNDRV_PCM_TRIGGER_STOP: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 417 | ptcr |= ATMEL_PDC_RXTDIS; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 418 | if (chip->opened <= 1) |
| 419 | camr &= ~AC97C_CMR_CENA; |
| 420 | break; |
| 421 | default: |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 422 | return -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | ac97c_writel(chip, CAMR, camr); |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 426 | writel(ptcr, chip->regs + ATMEL_PDC_PTCR); |
| 427 | return 0; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static snd_pcm_uframes_t |
| 431 | atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream) |
| 432 | { |
| 433 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 434 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 435 | snd_pcm_uframes_t frames; |
| 436 | unsigned long bytes; |
| 437 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 438 | bytes = readl(chip->regs + ATMEL_PDC_TPR); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 439 | bytes -= runtime->dma_addr; |
| 440 | |
| 441 | frames = bytes_to_frames(runtime, bytes); |
| 442 | if (frames >= runtime->buffer_size) |
| 443 | frames -= runtime->buffer_size; |
| 444 | return frames; |
| 445 | } |
| 446 | |
| 447 | static snd_pcm_uframes_t |
| 448 | atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream) |
| 449 | { |
| 450 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); |
| 451 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 452 | snd_pcm_uframes_t frames; |
| 453 | unsigned long bytes; |
| 454 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 455 | bytes = readl(chip->regs + ATMEL_PDC_RPR); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 456 | bytes -= runtime->dma_addr; |
| 457 | |
| 458 | frames = bytes_to_frames(runtime, bytes); |
| 459 | if (frames >= runtime->buffer_size) |
| 460 | frames -= runtime->buffer_size; |
| 461 | return frames; |
| 462 | } |
| 463 | |
| 464 | static struct snd_pcm_ops atmel_ac97_playback_ops = { |
| 465 | .open = atmel_ac97c_playback_open, |
| 466 | .close = atmel_ac97c_playback_close, |
| 467 | .ioctl = snd_pcm_lib_ioctl, |
| 468 | .hw_params = atmel_ac97c_playback_hw_params, |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 469 | .hw_free = snd_pcm_lib_free_pages, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 470 | .prepare = atmel_ac97c_playback_prepare, |
| 471 | .trigger = atmel_ac97c_playback_trigger, |
| 472 | .pointer = atmel_ac97c_playback_pointer, |
| 473 | }; |
| 474 | |
| 475 | static struct snd_pcm_ops atmel_ac97_capture_ops = { |
| 476 | .open = atmel_ac97c_capture_open, |
| 477 | .close = atmel_ac97c_capture_close, |
| 478 | .ioctl = snd_pcm_lib_ioctl, |
| 479 | .hw_params = atmel_ac97c_capture_hw_params, |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 480 | .hw_free = snd_pcm_lib_free_pages, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 481 | .prepare = atmel_ac97c_capture_prepare, |
| 482 | .trigger = atmel_ac97c_capture_trigger, |
| 483 | .pointer = atmel_ac97c_capture_pointer, |
| 484 | }; |
| 485 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 486 | static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev) |
| 487 | { |
| 488 | struct atmel_ac97c *chip = (struct atmel_ac97c *)dev; |
| 489 | irqreturn_t retval = IRQ_NONE; |
| 490 | u32 sr = ac97c_readl(chip, SR); |
| 491 | u32 casr = ac97c_readl(chip, CASR); |
| 492 | u32 cosr = ac97c_readl(chip, COSR); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 493 | u32 camr = ac97c_readl(chip, CAMR); |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 494 | |
| 495 | if (sr & AC97C_SR_CAEVT) { |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 496 | struct snd_pcm_runtime *runtime; |
| 497 | int offset, next_period, block_size; |
Yegor Yefremov | f534116 | 2010-06-08 08:57:13 +0200 | [diff] [blame] | 498 | dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n", |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 499 | casr & AC97C_CSR_OVRUN ? " OVRUN" : "", |
| 500 | casr & AC97C_CSR_RXRDY ? " RXRDY" : "", |
| 501 | casr & AC97C_CSR_UNRUN ? " UNRUN" : "", |
| 502 | casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "", |
| 503 | casr & AC97C_CSR_TXRDY ? " TXRDY" : "", |
| 504 | !casr ? " NONE" : ""); |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 505 | if ((casr & camr) & AC97C_CSR_ENDTX) { |
| 506 | runtime = chip->playback_substream->runtime; |
| 507 | block_size = frames_to_bytes(runtime, runtime->period_size); |
| 508 | chip->playback_period++; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 509 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 510 | if (chip->playback_period == runtime->periods) |
| 511 | chip->playback_period = 0; |
| 512 | next_period = chip->playback_period + 1; |
| 513 | if (next_period == runtime->periods) |
| 514 | next_period = 0; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 515 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 516 | offset = block_size * next_period; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 517 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 518 | writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_TNPR); |
| 519 | writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 520 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 521 | snd_pcm_period_elapsed(chip->playback_substream); |
| 522 | } |
| 523 | if ((casr & camr) & AC97C_CSR_ENDRX) { |
| 524 | runtime = chip->capture_substream->runtime; |
| 525 | block_size = frames_to_bytes(runtime, runtime->period_size); |
| 526 | chip->capture_period++; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 527 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 528 | if (chip->capture_period == runtime->periods) |
| 529 | chip->capture_period = 0; |
| 530 | next_period = chip->capture_period + 1; |
| 531 | if (next_period == runtime->periods) |
| 532 | next_period = 0; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 533 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 534 | offset = block_size * next_period; |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 535 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 536 | writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_RNPR); |
| 537 | writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR); |
| 538 | snd_pcm_period_elapsed(chip->capture_substream); |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 539 | } |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 540 | retval = IRQ_HANDLED; |
| 541 | } |
| 542 | |
| 543 | if (sr & AC97C_SR_COEVT) { |
| 544 | dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n", |
| 545 | cosr & AC97C_CSR_OVRUN ? " OVRUN" : "", |
| 546 | cosr & AC97C_CSR_RXRDY ? " RXRDY" : "", |
| 547 | cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "", |
| 548 | cosr & AC97C_CSR_TXRDY ? " TXRDY" : "", |
| 549 | !cosr ? " NONE" : ""); |
| 550 | retval = IRQ_HANDLED; |
| 551 | } |
| 552 | |
| 553 | if (retval == IRQ_NONE) { |
| 554 | dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x " |
| 555 | "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr); |
| 556 | } |
| 557 | |
| 558 | return retval; |
| 559 | } |
| 560 | |
Bill Pemberton | 61dc674 | 2012-12-06 12:35:13 -0500 | [diff] [blame] | 561 | static struct ac97_pcm at91_ac97_pcm_defs[] = { |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 562 | /* Playback */ |
| 563 | { |
| 564 | .exclusive = 1, |
| 565 | .r = { { |
| 566 | .slots = ((1 << AC97_SLOT_PCM_LEFT) |
| 567 | | (1 << AC97_SLOT_PCM_RIGHT)), |
| 568 | } }, |
| 569 | }, |
| 570 | /* PCM in */ |
| 571 | { |
| 572 | .stream = 1, |
| 573 | .exclusive = 1, |
| 574 | .r = { { |
| 575 | .slots = ((1 << AC97_SLOT_PCM_LEFT) |
| 576 | | (1 << AC97_SLOT_PCM_RIGHT)), |
| 577 | } } |
| 578 | }, |
| 579 | /* Mic in */ |
| 580 | { |
| 581 | .stream = 1, |
| 582 | .exclusive = 1, |
| 583 | .r = { { |
| 584 | .slots = (1<<AC97_SLOT_MIC), |
| 585 | } } |
| 586 | }, |
| 587 | }; |
| 588 | |
Bill Pemberton | 61dc674 | 2012-12-06 12:35:13 -0500 | [diff] [blame] | 589 | static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip) |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 590 | { |
| 591 | struct snd_pcm *pcm; |
| 592 | struct snd_pcm_hardware hw = atmel_ac97c_hw; |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 593 | int retval; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 594 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 595 | retval = snd_ac97_pcm_assign(chip->ac97_bus, |
| 596 | ARRAY_SIZE(at91_ac97_pcm_defs), |
| 597 | at91_ac97_pcm_defs); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 598 | if (retval) |
| 599 | return retval; |
| 600 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 601 | retval = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm); |
| 602 | if (retval) |
| 603 | return retval; |
| 604 | |
| 605 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &atmel_ac97_capture_ops); |
| 606 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_ac97_playback_ops); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 607 | |
| 608 | retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 609 | &chip->pdev->dev, hw.periods_min * hw.period_bytes_min, |
| 610 | hw.buffer_bytes_max); |
| 611 | if (retval) |
| 612 | return retval; |
| 613 | |
| 614 | pcm->private_data = chip; |
| 615 | pcm->info_flags = 0; |
| 616 | strcpy(pcm->name, chip->card->shortname); |
| 617 | chip->pcm = pcm; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip) |
| 623 | { |
| 624 | struct snd_ac97_template template; |
| 625 | memset(&template, 0, sizeof(template)); |
| 626 | template.private_data = chip; |
| 627 | return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97); |
| 628 | } |
| 629 | |
| 630 | static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg, |
| 631 | unsigned short val) |
| 632 | { |
| 633 | struct atmel_ac97c *chip = get_chip(ac97); |
| 634 | unsigned long word; |
| 635 | int timeout = 40; |
| 636 | |
| 637 | word = (reg & 0x7f) << 16 | val; |
| 638 | |
| 639 | do { |
| 640 | if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) { |
| 641 | ac97c_writel(chip, COTHR, word); |
| 642 | return; |
| 643 | } |
| 644 | udelay(1); |
| 645 | } while (--timeout); |
| 646 | |
| 647 | dev_dbg(&chip->pdev->dev, "codec write timeout\n"); |
| 648 | } |
| 649 | |
| 650 | static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97, |
| 651 | unsigned short reg) |
| 652 | { |
| 653 | struct atmel_ac97c *chip = get_chip(ac97); |
| 654 | unsigned long word; |
| 655 | int timeout = 40; |
| 656 | int write = 10; |
| 657 | |
| 658 | word = (0x80 | (reg & 0x7f)) << 16; |
| 659 | |
| 660 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) |
| 661 | ac97c_readl(chip, CORHR); |
| 662 | |
| 663 | retry_write: |
| 664 | timeout = 40; |
| 665 | |
| 666 | do { |
| 667 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) { |
| 668 | ac97c_writel(chip, COTHR, word); |
| 669 | goto read_reg; |
| 670 | } |
| 671 | udelay(10); |
| 672 | } while (--timeout); |
| 673 | |
| 674 | if (!--write) |
| 675 | goto timed_out; |
| 676 | goto retry_write; |
| 677 | |
| 678 | read_reg: |
| 679 | do { |
| 680 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) { |
| 681 | unsigned short val = ac97c_readl(chip, CORHR); |
| 682 | return val; |
| 683 | } |
| 684 | udelay(10); |
| 685 | } while (--timeout); |
| 686 | |
| 687 | if (!--write) |
| 688 | goto timed_out; |
| 689 | goto retry_write; |
| 690 | |
| 691 | timed_out: |
| 692 | dev_dbg(&chip->pdev->dev, "codec read timeout\n"); |
| 693 | return 0xffff; |
| 694 | } |
| 695 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 696 | static void atmel_ac97c_reset(struct atmel_ac97c *chip) |
| 697 | { |
Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 698 | ac97c_writel(chip, MR, 0); |
| 699 | ac97c_writel(chip, MR, AC97C_MR_ENA); |
| 700 | ac97c_writel(chip, CAMR, 0); |
| 701 | ac97c_writel(chip, COMR, 0); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 702 | |
| 703 | if (gpio_is_valid(chip->reset_pin)) { |
| 704 | gpio_set_value(chip->reset_pin, 0); |
| 705 | /* AC97 v2.2 specifications says minimum 1 us. */ |
Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 706 | udelay(2); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 707 | gpio_set_value(chip->reset_pin, 1); |
Bo Shen | 8015e3d | 2011-12-19 17:57:52 +0800 | [diff] [blame] | 708 | } else { |
| 709 | ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA); |
| 710 | udelay(2); |
| 711 | ac97c_writel(chip, MR, AC97C_MR_ENA); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 712 | } |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 715 | #ifdef CONFIG_OF |
| 716 | static const struct of_device_id atmel_ac97c_dt_ids[] = { |
| 717 | { .compatible = "atmel,at91sam9263-ac97c", }, |
| 718 | { } |
| 719 | }; |
| 720 | MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids); |
| 721 | |
| 722 | static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev) |
| 723 | { |
| 724 | struct ac97c_platform_data *pdata; |
| 725 | struct device_node *node = dev->of_node; |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 726 | |
| 727 | if (!node) { |
| 728 | dev_err(dev, "Device does not have associated DT data\n"); |
| 729 | return ERR_PTR(-EINVAL); |
| 730 | } |
| 731 | |
| 732 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 733 | if (!pdata) |
| 734 | return ERR_PTR(-ENOMEM); |
| 735 | |
| 736 | pdata->reset_pin = of_get_named_gpio(dev->of_node, "ac97-gpios", 2); |
| 737 | |
| 738 | return pdata; |
| 739 | } |
| 740 | #else |
| 741 | static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev) |
| 742 | { |
| 743 | dev_err(dev, "no platform data defined\n"); |
| 744 | return ERR_PTR(-ENXIO); |
| 745 | } |
| 746 | #endif |
| 747 | |
Bill Pemberton | 61dc674 | 2012-12-06 12:35:13 -0500 | [diff] [blame] | 748 | static int atmel_ac97c_probe(struct platform_device *pdev) |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 749 | { |
| 750 | struct snd_card *card; |
| 751 | struct atmel_ac97c *chip; |
| 752 | struct resource *regs; |
| 753 | struct ac97c_platform_data *pdata; |
| 754 | struct clk *pclk; |
| 755 | static struct snd_ac97_bus_ops ops = { |
| 756 | .write = atmel_ac97c_write, |
| 757 | .read = atmel_ac97c_read, |
| 758 | }; |
| 759 | int retval; |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 760 | int irq; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 761 | |
| 762 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 763 | if (!regs) { |
| 764 | dev_dbg(&pdev->dev, "no memory resource\n"); |
| 765 | return -ENXIO; |
| 766 | } |
| 767 | |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 768 | pdata = dev_get_platdata(&pdev->dev); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 769 | if (!pdata) { |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 770 | pdata = atmel_ac97c_probe_dt(&pdev->dev); |
| 771 | if (IS_ERR(pdata)) |
| 772 | return PTR_ERR(pdata); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 775 | irq = platform_get_irq(pdev, 0); |
| 776 | if (irq < 0) { |
Gustavo A. R. Silva | 7720113 | 2017-06-30 17:34:23 -0500 | [diff] [blame] | 777 | dev_dbg(&pdev->dev, "could not get irq: %d\n", irq); |
| 778 | return irq; |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 781 | pclk = clk_get(&pdev->dev, "ac97_clk"); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 782 | if (IS_ERR(pclk)) { |
| 783 | dev_dbg(&pdev->dev, "no peripheral clock\n"); |
| 784 | return PTR_ERR(pclk); |
| 785 | } |
Alexander Stein | 1132015 | 2014-12-05 20:10:06 +0100 | [diff] [blame] | 786 | clk_prepare_enable(pclk); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 787 | |
Takashi Iwai | a4f2473 | 2014-01-29 14:26:22 +0100 | [diff] [blame] | 788 | retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, |
| 789 | SNDRV_DEFAULT_STR1, THIS_MODULE, |
| 790 | sizeof(struct atmel_ac97c), &card); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 791 | if (retval) { |
| 792 | dev_dbg(&pdev->dev, "could not create sound card device\n"); |
| 793 | goto err_snd_card_new; |
| 794 | } |
| 795 | |
| 796 | chip = get_chip(card); |
| 797 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 798 | retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip); |
| 799 | if (retval) { |
| 800 | dev_dbg(&pdev->dev, "unable to request irq %d\n", irq); |
| 801 | goto err_request_irq; |
| 802 | } |
| 803 | chip->irq = irq; |
| 804 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 805 | spin_lock_init(&chip->lock); |
| 806 | |
| 807 | strcpy(card->driver, "Atmel AC97C"); |
| 808 | strcpy(card->shortname, "Atmel AC97C"); |
| 809 | sprintf(card->longname, "Atmel AC97 controller"); |
| 810 | |
| 811 | chip->card = card; |
| 812 | chip->pclk = pclk; |
| 813 | chip->pdev = pdev; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 814 | chip->regs = ioremap(regs->start, resource_size(regs)); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 815 | |
| 816 | if (!chip->regs) { |
| 817 | dev_dbg(&pdev->dev, "could not remap register memory\n"); |
Julia Lawall | 0c23e46 | 2012-08-19 09:02:57 +0200 | [diff] [blame] | 818 | retval = -ENOMEM; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 819 | goto err_ioremap; |
| 820 | } |
| 821 | |
| 822 | if (gpio_is_valid(pdata->reset_pin)) { |
| 823 | if (gpio_request(pdata->reset_pin, "reset_pin")) { |
| 824 | dev_dbg(&pdev->dev, "reset pin not available\n"); |
| 825 | chip->reset_pin = -ENODEV; |
| 826 | } else { |
| 827 | gpio_direction_output(pdata->reset_pin, 1); |
| 828 | chip->reset_pin = pdata->reset_pin; |
| 829 | } |
Bo Shen | b2522f9 | 2012-05-11 17:39:28 +0800 | [diff] [blame] | 830 | } else { |
| 831 | chip->reset_pin = -EINVAL; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 834 | atmel_ac97c_reset(chip); |
| 835 | |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 836 | /* Enable overrun interrupt from codec channel */ |
| 837 | ac97c_writel(chip, COMR, AC97C_CSR_OVRUN); |
| 838 | ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT); |
| 839 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 840 | retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus); |
| 841 | if (retval) { |
| 842 | dev_dbg(&pdev->dev, "could not register on ac97 bus\n"); |
| 843 | goto err_ac97_bus; |
| 844 | } |
| 845 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 846 | retval = atmel_ac97c_mixer_new(chip); |
| 847 | if (retval) { |
| 848 | dev_dbg(&pdev->dev, "could not register ac97 mixer\n"); |
| 849 | goto err_ac97_bus; |
| 850 | } |
| 851 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 852 | retval = atmel_ac97c_pcm_new(chip); |
| 853 | if (retval) { |
| 854 | dev_dbg(&pdev->dev, "could not register ac97 pcm device\n"); |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 855 | goto err_ac97_bus; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | retval = snd_card_register(card); |
| 859 | if (retval) { |
| 860 | dev_dbg(&pdev->dev, "could not register sound card\n"); |
Andy Shevchenko | 020c526 | 2017-05-09 19:18:36 +0300 | [diff] [blame] | 861 | goto err_ac97_bus; |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | platform_set_drvdata(pdev, card); |
| 865 | |
Sedji Gaouaou | 7177395 | 2010-03-01 12:19:18 +0100 | [diff] [blame] | 866 | dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n", |
| 867 | chip->regs, irq); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 868 | |
| 869 | return 0; |
| 870 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 871 | err_ac97_bus: |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 872 | if (gpio_is_valid(chip->reset_pin)) |
| 873 | gpio_free(chip->reset_pin); |
| 874 | |
| 875 | iounmap(chip->regs); |
| 876 | err_ioremap: |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 877 | free_irq(irq, chip); |
| 878 | err_request_irq: |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 879 | snd_card_free(card); |
| 880 | err_snd_card_new: |
Alexander Stein | 1132015 | 2014-12-05 20:10:06 +0100 | [diff] [blame] | 881 | clk_disable_unprepare(pclk); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 882 | clk_put(pclk); |
| 883 | return retval; |
| 884 | } |
| 885 | |
Takashi Iwai | d34e4e0 | 2012-08-09 15:47:15 +0200 | [diff] [blame] | 886 | #ifdef CONFIG_PM_SLEEP |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 887 | static int atmel_ac97c_suspend(struct device *pdev) |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 888 | { |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 889 | struct snd_card *card = dev_get_drvdata(pdev); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 890 | struct atmel_ac97c *chip = card->private_data; |
| 891 | |
Alexander Stein | 1132015 | 2014-12-05 20:10:06 +0100 | [diff] [blame] | 892 | clk_disable_unprepare(chip->pclk); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 896 | static int atmel_ac97c_resume(struct device *pdev) |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 897 | { |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 898 | struct snd_card *card = dev_get_drvdata(pdev); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 899 | struct atmel_ac97c *chip = card->private_data; |
| 900 | |
Alexander Stein | 1132015 | 2014-12-05 20:10:06 +0100 | [diff] [blame] | 901 | clk_prepare_enable(chip->pclk); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 902 | return 0; |
| 903 | } |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 904 | |
| 905 | static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume); |
| 906 | #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 907 | #else |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 908 | #define ATMEL_AC97C_PM_OPS NULL |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 909 | #endif |
| 910 | |
Bill Pemberton | 61dc674 | 2012-12-06 12:35:13 -0500 | [diff] [blame] | 911 | static int atmel_ac97c_remove(struct platform_device *pdev) |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 912 | { |
| 913 | struct snd_card *card = platform_get_drvdata(pdev); |
| 914 | struct atmel_ac97c *chip = get_chip(card); |
| 915 | |
| 916 | if (gpio_is_valid(chip->reset_pin)) |
| 917 | gpio_free(chip->reset_pin); |
| 918 | |
Hans-Christian Egtvedt | bd74a18 | 2009-04-02 08:21:16 +0200 | [diff] [blame] | 919 | ac97c_writel(chip, CAMR, 0); |
| 920 | ac97c_writel(chip, COMR, 0); |
| 921 | ac97c_writel(chip, MR, 0); |
| 922 | |
Alexander Stein | 1132015 | 2014-12-05 20:10:06 +0100 | [diff] [blame] | 923 | clk_disable_unprepare(chip->pclk); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 924 | clk_put(chip->pclk); |
| 925 | iounmap(chip->regs); |
Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 926 | free_irq(chip->irq, chip); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 927 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 928 | snd_card_free(card); |
| 929 | |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static struct platform_driver atmel_ac97c_driver = { |
Alexander Stein | 4b973ee | 2014-04-15 19:38:56 +0200 | [diff] [blame] | 934 | .probe = atmel_ac97c_probe, |
Bill Pemberton | 61dc674 | 2012-12-06 12:35:13 -0500 | [diff] [blame] | 935 | .remove = atmel_ac97c_remove, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 936 | .driver = { |
| 937 | .name = "atmel_ac97c", |
Takashi Iwai | 284e7ca | 2012-07-02 11:22:40 +0200 | [diff] [blame] | 938 | .pm = ATMEL_AC97C_PM_OPS, |
Alexander Stein | b2d8957 | 2014-12-29 13:08:39 +0100 | [diff] [blame] | 939 | .of_match_table = of_match_ptr(atmel_ac97c_dt_ids), |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 940 | }, |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 941 | }; |
Alexander Stein | 4b973ee | 2014-04-15 19:38:56 +0200 | [diff] [blame] | 942 | module_platform_driver(atmel_ac97c_driver); |
Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 943 | |
| 944 | MODULE_LICENSE("GPL"); |
| 945 | MODULE_DESCRIPTION("Driver for Atmel AC97 controller"); |
Hans-Christian Egtvedt | 0cfae7c | 2011-06-28 16:59:14 +0200 | [diff] [blame] | 946 | MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>"); |