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