| 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> | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 14 | #include <linux/dmaengine.h> | 
|  | 15 | #include <linux/dma-mapping.h> | 
|  | 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> | 
|  | 21 | #include <linux/gpio.h> | 
|  | 22 | #include <linux/io.h> | 
|  | 23 |  | 
|  | 24 | #include <sound/core.h> | 
|  | 25 | #include <sound/initval.h> | 
|  | 26 | #include <sound/pcm.h> | 
|  | 27 | #include <sound/pcm_params.h> | 
|  | 28 | #include <sound/ac97_codec.h> | 
|  | 29 | #include <sound/atmel-ac97c.h> | 
|  | 30 | #include <sound/memalloc.h> | 
|  | 31 |  | 
|  | 32 | #include <linux/dw_dmac.h> | 
|  | 33 |  | 
|  | 34 | #include "ac97c.h" | 
|  | 35 |  | 
|  | 36 | enum { | 
|  | 37 | DMA_TX_READY = 0, | 
|  | 38 | DMA_RX_READY, | 
|  | 39 | DMA_TX_CHAN_PRESENT, | 
|  | 40 | DMA_RX_CHAN_PRESENT, | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | /* Serialize access to opened variable */ | 
|  | 44 | static DEFINE_MUTEX(opened_mutex); | 
|  | 45 |  | 
|  | 46 | struct atmel_ac97c_dma { | 
|  | 47 | struct dma_chan			*rx_chan; | 
|  | 48 | struct dma_chan			*tx_chan; | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 | struct atmel_ac97c { | 
|  | 52 | struct clk			*pclk; | 
|  | 53 | struct platform_device		*pdev; | 
|  | 54 | struct atmel_ac97c_dma		dma; | 
|  | 55 |  | 
|  | 56 | struct snd_pcm_substream	*playback_substream; | 
|  | 57 | struct snd_pcm_substream	*capture_substream; | 
|  | 58 | struct snd_card			*card; | 
|  | 59 | struct snd_pcm			*pcm; | 
|  | 60 | struct snd_ac97			*ac97; | 
|  | 61 | struct snd_ac97_bus		*ac97_bus; | 
|  | 62 |  | 
|  | 63 | u64				cur_format; | 
|  | 64 | unsigned int			cur_rate; | 
|  | 65 | unsigned long			flags; | 
|  | 66 | /* Serialize access to opened variable */ | 
|  | 67 | spinlock_t			lock; | 
|  | 68 | void __iomem			*regs; | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 69 | int				irq; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 70 | int				opened; | 
|  | 71 | int				reset_pin; | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data) | 
|  | 75 |  | 
|  | 76 | #define ac97c_writel(chip, reg, val)			\ | 
|  | 77 | __raw_writel((val), (chip)->regs + AC97C_##reg) | 
|  | 78 | #define ac97c_readl(chip, reg)				\ | 
|  | 79 | __raw_readl((chip)->regs + AC97C_##reg) | 
|  | 80 |  | 
|  | 81 | /* This function is called by the DMA driver. */ | 
|  | 82 | static void atmel_ac97c_dma_playback_period_done(void *arg) | 
|  | 83 | { | 
|  | 84 | struct atmel_ac97c *chip = arg; | 
|  | 85 | snd_pcm_period_elapsed(chip->playback_substream); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static void atmel_ac97c_dma_capture_period_done(void *arg) | 
|  | 89 | { | 
|  | 90 | struct atmel_ac97c *chip = arg; | 
|  | 91 | snd_pcm_period_elapsed(chip->capture_substream); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip, | 
|  | 95 | struct snd_pcm_substream *substream, | 
|  | 96 | enum dma_data_direction direction) | 
|  | 97 | { | 
|  | 98 | struct dma_chan			*chan; | 
|  | 99 | struct dw_cyclic_desc		*cdesc; | 
|  | 100 | struct snd_pcm_runtime		*runtime = substream->runtime; | 
|  | 101 | unsigned long			buffer_len, period_len; | 
|  | 102 |  | 
|  | 103 | /* | 
|  | 104 | * We don't do DMA on "complex" transfers, i.e. with | 
|  | 105 | * non-halfword-aligned buffers or lengths. | 
|  | 106 | */ | 
|  | 107 | if (runtime->dma_addr & 1 || runtime->buffer_size & 1) { | 
|  | 108 | dev_dbg(&chip->pdev->dev, "too complex transfer\n"); | 
|  | 109 | return -EINVAL; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | if (direction == DMA_TO_DEVICE) | 
|  | 113 | chan = chip->dma.tx_chan; | 
|  | 114 | else | 
|  | 115 | chan = chip->dma.rx_chan; | 
|  | 116 |  | 
|  | 117 | buffer_len = frames_to_bytes(runtime, runtime->buffer_size); | 
|  | 118 | period_len = frames_to_bytes(runtime, runtime->period_size); | 
|  | 119 |  | 
|  | 120 | cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len, | 
|  | 121 | period_len, direction); | 
|  | 122 | if (IS_ERR(cdesc)) { | 
|  | 123 | dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n"); | 
|  | 124 | return PTR_ERR(cdesc); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | if (direction == DMA_TO_DEVICE) { | 
|  | 128 | cdesc->period_callback = atmel_ac97c_dma_playback_period_done; | 
|  | 129 | set_bit(DMA_TX_READY, &chip->flags); | 
|  | 130 | } else { | 
|  | 131 | cdesc->period_callback = atmel_ac97c_dma_capture_period_done; | 
|  | 132 | set_bit(DMA_RX_READY, &chip->flags); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | cdesc->period_callback_param = chip; | 
|  | 136 |  | 
|  | 137 | return 0; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | static struct snd_pcm_hardware atmel_ac97c_hw = { | 
|  | 141 | .info			= (SNDRV_PCM_INFO_MMAP | 
|  | 142 | | SNDRV_PCM_INFO_MMAP_VALID | 
|  | 143 | | SNDRV_PCM_INFO_INTERLEAVED | 
|  | 144 | | SNDRV_PCM_INFO_BLOCK_TRANSFER | 
|  | 145 | | SNDRV_PCM_INFO_JOINT_DUPLEX | 
|  | 146 | | SNDRV_PCM_INFO_RESUME | 
|  | 147 | | SNDRV_PCM_INFO_PAUSE), | 
|  | 148 | .formats		= (SNDRV_PCM_FMTBIT_S16_BE | 
|  | 149 | | SNDRV_PCM_FMTBIT_S16_LE), | 
|  | 150 | .rates			= (SNDRV_PCM_RATE_CONTINUOUS), | 
|  | 151 | .rate_min		= 4000, | 
|  | 152 | .rate_max		= 48000, | 
|  | 153 | .channels_min		= 1, | 
|  | 154 | .channels_max		= 2, | 
| Hans-Christian Egtvedt | c42eec0 | 2009-04-02 08:21:13 +0200 | [diff] [blame] | 155 | .buffer_bytes_max	= 2 * 2 * 64 * 2048, | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 156 | .period_bytes_min	= 4096, | 
|  | 157 | .period_bytes_max	= 4096, | 
| Hans-Christian Egtvedt | c42eec0 | 2009-04-02 08:21:13 +0200 | [diff] [blame] | 158 | .periods_min		= 6, | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 159 | .periods_max		= 64, | 
|  | 160 | }; | 
|  | 161 |  | 
|  | 162 | static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream) | 
|  | 163 | { | 
|  | 164 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 165 | struct snd_pcm_runtime *runtime = substream->runtime; | 
|  | 166 |  | 
|  | 167 | mutex_lock(&opened_mutex); | 
|  | 168 | chip->opened++; | 
|  | 169 | runtime->hw = atmel_ac97c_hw; | 
|  | 170 | if (chip->cur_rate) { | 
|  | 171 | runtime->hw.rate_min = chip->cur_rate; | 
|  | 172 | runtime->hw.rate_max = chip->cur_rate; | 
|  | 173 | } | 
|  | 174 | if (chip->cur_format) | 
|  | 175 | runtime->hw.formats = (1ULL << chip->cur_format); | 
|  | 176 | mutex_unlock(&opened_mutex); | 
|  | 177 | chip->playback_substream = substream; | 
|  | 178 | return 0; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream) | 
|  | 182 | { | 
|  | 183 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 184 | struct snd_pcm_runtime *runtime = substream->runtime; | 
|  | 185 |  | 
|  | 186 | mutex_lock(&opened_mutex); | 
|  | 187 | chip->opened++; | 
|  | 188 | runtime->hw = atmel_ac97c_hw; | 
|  | 189 | if (chip->cur_rate) { | 
|  | 190 | runtime->hw.rate_min = chip->cur_rate; | 
|  | 191 | runtime->hw.rate_max = chip->cur_rate; | 
|  | 192 | } | 
|  | 193 | if (chip->cur_format) | 
|  | 194 | runtime->hw.formats = (1ULL << chip->cur_format); | 
|  | 195 | mutex_unlock(&opened_mutex); | 
|  | 196 | chip->capture_substream = substream; | 
|  | 197 | return 0; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream) | 
|  | 201 | { | 
|  | 202 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 203 |  | 
|  | 204 | mutex_lock(&opened_mutex); | 
|  | 205 | chip->opened--; | 
|  | 206 | if (!chip->opened) { | 
|  | 207 | chip->cur_rate = 0; | 
|  | 208 | chip->cur_format = 0; | 
|  | 209 | } | 
|  | 210 | mutex_unlock(&opened_mutex); | 
|  | 211 |  | 
|  | 212 | chip->playback_substream = NULL; | 
|  | 213 |  | 
|  | 214 | return 0; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream) | 
|  | 218 | { | 
|  | 219 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 220 |  | 
|  | 221 | mutex_lock(&opened_mutex); | 
|  | 222 | chip->opened--; | 
|  | 223 | if (!chip->opened) { | 
|  | 224 | chip->cur_rate = 0; | 
|  | 225 | chip->cur_format = 0; | 
|  | 226 | } | 
|  | 227 | mutex_unlock(&opened_mutex); | 
|  | 228 |  | 
|  | 229 | chip->capture_substream = NULL; | 
|  | 230 |  | 
|  | 231 | return 0; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream, | 
|  | 235 | struct snd_pcm_hw_params *hw_params) | 
|  | 236 | { | 
|  | 237 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 238 | int retval; | 
|  | 239 |  | 
|  | 240 | retval = snd_pcm_lib_malloc_pages(substream, | 
|  | 241 | params_buffer_bytes(hw_params)); | 
|  | 242 | if (retval < 0) | 
|  | 243 | return retval; | 
|  | 244 | /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */ | 
|  | 245 | if (retval == 1) | 
|  | 246 | if (test_and_clear_bit(DMA_TX_READY, &chip->flags)) | 
|  | 247 | dw_dma_cyclic_free(chip->dma.tx_chan); | 
|  | 248 |  | 
|  | 249 | /* Set restrictions to params. */ | 
|  | 250 | mutex_lock(&opened_mutex); | 
|  | 251 | chip->cur_rate = params_rate(hw_params); | 
|  | 252 | chip->cur_format = params_format(hw_params); | 
|  | 253 | mutex_unlock(&opened_mutex); | 
|  | 254 |  | 
|  | 255 | return retval; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream, | 
|  | 259 | struct snd_pcm_hw_params *hw_params) | 
|  | 260 | { | 
|  | 261 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 262 | int retval; | 
|  | 263 |  | 
|  | 264 | retval = snd_pcm_lib_malloc_pages(substream, | 
|  | 265 | params_buffer_bytes(hw_params)); | 
|  | 266 | if (retval < 0) | 
|  | 267 | return retval; | 
|  | 268 | /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */ | 
|  | 269 | if (retval == 1) | 
|  | 270 | if (test_and_clear_bit(DMA_RX_READY, &chip->flags)) | 
|  | 271 | dw_dma_cyclic_free(chip->dma.rx_chan); | 
|  | 272 |  | 
|  | 273 | /* Set restrictions to params. */ | 
|  | 274 | mutex_lock(&opened_mutex); | 
|  | 275 | chip->cur_rate = params_rate(hw_params); | 
|  | 276 | chip->cur_format = params_format(hw_params); | 
|  | 277 | mutex_unlock(&opened_mutex); | 
|  | 278 |  | 
|  | 279 | return retval; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream) | 
|  | 283 | { | 
|  | 284 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 285 | if (test_and_clear_bit(DMA_TX_READY, &chip->flags)) | 
|  | 286 | dw_dma_cyclic_free(chip->dma.tx_chan); | 
|  | 287 | return snd_pcm_lib_free_pages(substream); | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream) | 
|  | 291 | { | 
|  | 292 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 293 | if (test_and_clear_bit(DMA_RX_READY, &chip->flags)) | 
|  | 294 | dw_dma_cyclic_free(chip->dma.rx_chan); | 
|  | 295 | return snd_pcm_lib_free_pages(substream); | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream) | 
|  | 299 | { | 
|  | 300 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 301 | struct snd_pcm_runtime *runtime = substream->runtime; | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 302 | unsigned long word = ac97c_readl(chip, OCA); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 303 | int retval; | 
|  | 304 |  | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 305 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); | 
|  | 306 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 307 | /* assign channels to AC97C channel A */ | 
|  | 308 | switch (runtime->channels) { | 
|  | 309 | case 1: | 
|  | 310 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A); | 
|  | 311 | break; | 
|  | 312 | case 2: | 
|  | 313 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A) | 
|  | 314 | | AC97C_CH_ASSIGN(PCM_RIGHT, A); | 
|  | 315 | break; | 
|  | 316 | default: | 
|  | 317 | /* TODO: support more than two channels */ | 
|  | 318 | return -EINVAL; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 319 | } | 
|  | 320 | ac97c_writel(chip, OCA, word); | 
|  | 321 |  | 
|  | 322 | /* configure sample format and size */ | 
|  | 323 | word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; | 
|  | 324 |  | 
|  | 325 | switch (runtime->format) { | 
|  | 326 | case SNDRV_PCM_FORMAT_S16_LE: | 
|  | 327 | word |= AC97C_CMR_CEM_LITTLE; | 
|  | 328 | break; | 
|  | 329 | case SNDRV_PCM_FORMAT_S16_BE: /* fall through */ | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 330 | word &= ~(AC97C_CMR_CEM_LITTLE); | 
|  | 331 | break; | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 332 | default: | 
|  | 333 | word = ac97c_readl(chip, OCA); | 
|  | 334 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); | 
|  | 335 | ac97c_writel(chip, OCA, word); | 
|  | 336 | return -EINVAL; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 339 | /* Enable underrun interrupt on channel A */ | 
|  | 340 | word |= AC97C_CSR_UNRUN; | 
|  | 341 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 342 | ac97c_writel(chip, CAMR, word); | 
|  | 343 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 344 | /* Enable channel A event interrupt */ | 
|  | 345 | word = ac97c_readl(chip, IMR); | 
|  | 346 | word |= AC97C_SR_CAEVT; | 
|  | 347 | ac97c_writel(chip, IER, word); | 
|  | 348 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 349 | /* set variable rate if needed */ | 
|  | 350 | if (runtime->rate != 48000) { | 
|  | 351 | word = ac97c_readl(chip, MR); | 
|  | 352 | word |= AC97C_MR_VRA; | 
|  | 353 | ac97c_writel(chip, MR, word); | 
|  | 354 | } else { | 
|  | 355 | word = ac97c_readl(chip, MR); | 
|  | 356 | word &= ~(AC97C_MR_VRA); | 
|  | 357 | ac97c_writel(chip, MR, word); | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE, | 
|  | 361 | runtime->rate); | 
|  | 362 | if (retval) | 
|  | 363 | dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n", | 
|  | 364 | runtime->rate); | 
|  | 365 |  | 
|  | 366 | if (!test_bit(DMA_TX_READY, &chip->flags)) | 
|  | 367 | retval = atmel_ac97c_prepare_dma(chip, substream, | 
|  | 368 | DMA_TO_DEVICE); | 
|  | 369 |  | 
|  | 370 | return retval; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream) | 
|  | 374 | { | 
|  | 375 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 376 | struct snd_pcm_runtime *runtime = substream->runtime; | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 377 | unsigned long word = ac97c_readl(chip, ICA); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 378 | int retval; | 
|  | 379 |  | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 380 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); | 
|  | 381 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 382 | /* assign channels to AC97C channel A */ | 
|  | 383 | switch (runtime->channels) { | 
|  | 384 | case 1: | 
|  | 385 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A); | 
|  | 386 | break; | 
|  | 387 | case 2: | 
|  | 388 | word |= AC97C_CH_ASSIGN(PCM_LEFT, A) | 
|  | 389 | | AC97C_CH_ASSIGN(PCM_RIGHT, A); | 
|  | 390 | break; | 
|  | 391 | default: | 
|  | 392 | /* TODO: support more than two channels */ | 
|  | 393 | return -EINVAL; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 394 | } | 
|  | 395 | ac97c_writel(chip, ICA, word); | 
|  | 396 |  | 
|  | 397 | /* configure sample format and size */ | 
|  | 398 | word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16; | 
|  | 399 |  | 
|  | 400 | switch (runtime->format) { | 
|  | 401 | case SNDRV_PCM_FORMAT_S16_LE: | 
|  | 402 | word |= AC97C_CMR_CEM_LITTLE; | 
|  | 403 | break; | 
|  | 404 | case SNDRV_PCM_FORMAT_S16_BE: /* fall through */ | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 405 | word &= ~(AC97C_CMR_CEM_LITTLE); | 
|  | 406 | break; | 
| Hans-Christian Egtvedt | 128ed6a | 2009-04-02 08:21:12 +0200 | [diff] [blame] | 407 | default: | 
|  | 408 | word = ac97c_readl(chip, ICA); | 
|  | 409 | word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT)); | 
|  | 410 | ac97c_writel(chip, ICA, word); | 
|  | 411 | return -EINVAL; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 414 | /* Enable overrun interrupt on channel A */ | 
|  | 415 | word |= AC97C_CSR_OVRUN; | 
|  | 416 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 417 | ac97c_writel(chip, CAMR, word); | 
|  | 418 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 419 | /* Enable channel A event interrupt */ | 
|  | 420 | word = ac97c_readl(chip, IMR); | 
|  | 421 | word |= AC97C_SR_CAEVT; | 
|  | 422 | ac97c_writel(chip, IER, word); | 
|  | 423 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 424 | /* set variable rate if needed */ | 
|  | 425 | if (runtime->rate != 48000) { | 
|  | 426 | word = ac97c_readl(chip, MR); | 
|  | 427 | word |= AC97C_MR_VRA; | 
|  | 428 | ac97c_writel(chip, MR, word); | 
|  | 429 | } else { | 
|  | 430 | word = ac97c_readl(chip, MR); | 
|  | 431 | word &= ~(AC97C_MR_VRA); | 
|  | 432 | ac97c_writel(chip, MR, word); | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE, | 
|  | 436 | runtime->rate); | 
|  | 437 | if (retval) | 
|  | 438 | dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n", | 
|  | 439 | runtime->rate); | 
|  | 440 |  | 
|  | 441 | if (!test_bit(DMA_RX_READY, &chip->flags)) | 
|  | 442 | retval = atmel_ac97c_prepare_dma(chip, substream, | 
|  | 443 | DMA_FROM_DEVICE); | 
|  | 444 |  | 
|  | 445 | return retval; | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | static int | 
|  | 449 | atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd) | 
|  | 450 | { | 
|  | 451 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 452 | unsigned long camr; | 
|  | 453 | int retval = 0; | 
|  | 454 |  | 
|  | 455 | camr = ac97c_readl(chip, CAMR); | 
|  | 456 |  | 
|  | 457 | switch (cmd) { | 
|  | 458 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ | 
|  | 459 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ | 
|  | 460 | case SNDRV_PCM_TRIGGER_START: | 
|  | 461 | retval = dw_dma_cyclic_start(chip->dma.tx_chan); | 
|  | 462 | if (retval) | 
|  | 463 | goto out; | 
|  | 464 | camr |= AC97C_CMR_CENA; | 
|  | 465 | break; | 
|  | 466 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ | 
|  | 467 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ | 
|  | 468 | case SNDRV_PCM_TRIGGER_STOP: | 
|  | 469 | dw_dma_cyclic_stop(chip->dma.tx_chan); | 
|  | 470 | if (chip->opened <= 1) | 
|  | 471 | camr &= ~AC97C_CMR_CENA; | 
|  | 472 | break; | 
|  | 473 | default: | 
|  | 474 | retval = -EINVAL; | 
|  | 475 | goto out; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | ac97c_writel(chip, CAMR, camr); | 
|  | 479 | out: | 
|  | 480 | return retval; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | static int | 
|  | 484 | atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd) | 
|  | 485 | { | 
|  | 486 | struct atmel_ac97c *chip = snd_pcm_substream_chip(substream); | 
|  | 487 | unsigned long camr; | 
|  | 488 | int retval = 0; | 
|  | 489 |  | 
|  | 490 | camr = ac97c_readl(chip, CAMR); | 
|  | 491 |  | 
|  | 492 | switch (cmd) { | 
|  | 493 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ | 
|  | 494 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ | 
|  | 495 | case SNDRV_PCM_TRIGGER_START: | 
|  | 496 | retval = dw_dma_cyclic_start(chip->dma.rx_chan); | 
|  | 497 | if (retval) | 
|  | 498 | goto out; | 
|  | 499 | camr |= AC97C_CMR_CENA; | 
|  | 500 | break; | 
|  | 501 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ | 
|  | 502 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ | 
|  | 503 | case SNDRV_PCM_TRIGGER_STOP: | 
|  | 504 | dw_dma_cyclic_stop(chip->dma.rx_chan); | 
|  | 505 | if (chip->opened <= 1) | 
|  | 506 | camr &= ~AC97C_CMR_CENA; | 
|  | 507 | break; | 
|  | 508 | default: | 
|  | 509 | retval = -EINVAL; | 
|  | 510 | break; | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | ac97c_writel(chip, CAMR, camr); | 
|  | 514 | out: | 
|  | 515 | return retval; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | static snd_pcm_uframes_t | 
|  | 519 | atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream) | 
|  | 520 | { | 
|  | 521 | struct atmel_ac97c	*chip = snd_pcm_substream_chip(substream); | 
|  | 522 | struct snd_pcm_runtime	*runtime = substream->runtime; | 
|  | 523 | snd_pcm_uframes_t	frames; | 
|  | 524 | unsigned long		bytes; | 
|  | 525 |  | 
|  | 526 | bytes = dw_dma_get_src_addr(chip->dma.tx_chan); | 
|  | 527 | bytes -= runtime->dma_addr; | 
|  | 528 |  | 
|  | 529 | frames = bytes_to_frames(runtime, bytes); | 
|  | 530 | if (frames >= runtime->buffer_size) | 
|  | 531 | frames -= runtime->buffer_size; | 
|  | 532 | return frames; | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | static snd_pcm_uframes_t | 
|  | 536 | atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream) | 
|  | 537 | { | 
|  | 538 | struct atmel_ac97c	*chip = snd_pcm_substream_chip(substream); | 
|  | 539 | struct snd_pcm_runtime	*runtime = substream->runtime; | 
|  | 540 | snd_pcm_uframes_t	frames; | 
|  | 541 | unsigned long		bytes; | 
|  | 542 |  | 
|  | 543 | bytes = dw_dma_get_dst_addr(chip->dma.rx_chan); | 
|  | 544 | bytes -= runtime->dma_addr; | 
|  | 545 |  | 
|  | 546 | frames = bytes_to_frames(runtime, bytes); | 
|  | 547 | if (frames >= runtime->buffer_size) | 
|  | 548 | frames -= runtime->buffer_size; | 
|  | 549 | return frames; | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | static struct snd_pcm_ops atmel_ac97_playback_ops = { | 
|  | 553 | .open		= atmel_ac97c_playback_open, | 
|  | 554 | .close		= atmel_ac97c_playback_close, | 
|  | 555 | .ioctl		= snd_pcm_lib_ioctl, | 
|  | 556 | .hw_params	= atmel_ac97c_playback_hw_params, | 
|  | 557 | .hw_free	= atmel_ac97c_playback_hw_free, | 
|  | 558 | .prepare	= atmel_ac97c_playback_prepare, | 
|  | 559 | .trigger	= atmel_ac97c_playback_trigger, | 
|  | 560 | .pointer	= atmel_ac97c_playback_pointer, | 
|  | 561 | }; | 
|  | 562 |  | 
|  | 563 | static struct snd_pcm_ops atmel_ac97_capture_ops = { | 
|  | 564 | .open		= atmel_ac97c_capture_open, | 
|  | 565 | .close		= atmel_ac97c_capture_close, | 
|  | 566 | .ioctl		= snd_pcm_lib_ioctl, | 
|  | 567 | .hw_params	= atmel_ac97c_capture_hw_params, | 
|  | 568 | .hw_free	= atmel_ac97c_capture_hw_free, | 
|  | 569 | .prepare	= atmel_ac97c_capture_prepare, | 
|  | 570 | .trigger	= atmel_ac97c_capture_trigger, | 
|  | 571 | .pointer	= atmel_ac97c_capture_pointer, | 
|  | 572 | }; | 
|  | 573 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 574 | static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev) | 
|  | 575 | { | 
|  | 576 | struct atmel_ac97c	*chip  = (struct atmel_ac97c *)dev; | 
|  | 577 | irqreturn_t		retval = IRQ_NONE; | 
|  | 578 | u32			sr     = ac97c_readl(chip, SR); | 
|  | 579 | u32			casr   = ac97c_readl(chip, CASR); | 
|  | 580 | u32			cosr   = ac97c_readl(chip, COSR); | 
|  | 581 |  | 
|  | 582 | if (sr & AC97C_SR_CAEVT) { | 
|  | 583 | dev_info(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n", | 
|  | 584 | casr & AC97C_CSR_OVRUN   ? " OVRUN"   : "", | 
|  | 585 | casr & AC97C_CSR_RXRDY   ? " RXRDY"   : "", | 
|  | 586 | casr & AC97C_CSR_UNRUN   ? " UNRUN"   : "", | 
|  | 587 | casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "", | 
|  | 588 | casr & AC97C_CSR_TXRDY   ? " TXRDY"   : "", | 
|  | 589 | !casr                    ? " NONE"    : ""); | 
|  | 590 | retval = IRQ_HANDLED; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | if (sr & AC97C_SR_COEVT) { | 
|  | 594 | dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n", | 
|  | 595 | cosr & AC97C_CSR_OVRUN   ? " OVRUN"   : "", | 
|  | 596 | cosr & AC97C_CSR_RXRDY   ? " RXRDY"   : "", | 
|  | 597 | cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "", | 
|  | 598 | cosr & AC97C_CSR_TXRDY   ? " TXRDY"   : "", | 
|  | 599 | !cosr                    ? " NONE"    : ""); | 
|  | 600 | retval = IRQ_HANDLED; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | if (retval == IRQ_NONE) { | 
|  | 604 | dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x " | 
|  | 605 | "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr); | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | return retval; | 
|  | 609 | } | 
|  | 610 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 611 | static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip) | 
|  | 612 | { | 
|  | 613 | struct snd_pcm		*pcm; | 
|  | 614 | struct snd_pcm_hardware	hw = atmel_ac97c_hw; | 
|  | 615 | int			capture, playback, retval; | 
|  | 616 |  | 
|  | 617 | capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags); | 
|  | 618 | playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags); | 
|  | 619 |  | 
|  | 620 | retval = snd_pcm_new(chip->card, chip->card->shortname, | 
|  | 621 | chip->pdev->id, playback, capture, &pcm); | 
|  | 622 | if (retval) | 
|  | 623 | return retval; | 
|  | 624 |  | 
|  | 625 | if (capture) | 
|  | 626 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, | 
|  | 627 | &atmel_ac97_capture_ops); | 
|  | 628 | if (playback) | 
|  | 629 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, | 
|  | 630 | &atmel_ac97_playback_ops); | 
|  | 631 |  | 
|  | 632 | retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | 
|  | 633 | &chip->pdev->dev, hw.periods_min * hw.period_bytes_min, | 
|  | 634 | hw.buffer_bytes_max); | 
|  | 635 | if (retval) | 
|  | 636 | return retval; | 
|  | 637 |  | 
|  | 638 | pcm->private_data = chip; | 
|  | 639 | pcm->info_flags = 0; | 
|  | 640 | strcpy(pcm->name, chip->card->shortname); | 
|  | 641 | chip->pcm = pcm; | 
|  | 642 |  | 
|  | 643 | return 0; | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip) | 
|  | 647 | { | 
|  | 648 | struct snd_ac97_template template; | 
|  | 649 | memset(&template, 0, sizeof(template)); | 
|  | 650 | template.private_data = chip; | 
|  | 651 | return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97); | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg, | 
|  | 655 | unsigned short val) | 
|  | 656 | { | 
|  | 657 | struct atmel_ac97c *chip = get_chip(ac97); | 
|  | 658 | unsigned long word; | 
|  | 659 | int timeout = 40; | 
|  | 660 |  | 
|  | 661 | word = (reg & 0x7f) << 16 | val; | 
|  | 662 |  | 
|  | 663 | do { | 
|  | 664 | if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) { | 
|  | 665 | ac97c_writel(chip, COTHR, word); | 
|  | 666 | return; | 
|  | 667 | } | 
|  | 668 | udelay(1); | 
|  | 669 | } while (--timeout); | 
|  | 670 |  | 
|  | 671 | dev_dbg(&chip->pdev->dev, "codec write timeout\n"); | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97, | 
|  | 675 | unsigned short reg) | 
|  | 676 | { | 
|  | 677 | struct atmel_ac97c *chip = get_chip(ac97); | 
|  | 678 | unsigned long word; | 
|  | 679 | int timeout = 40; | 
|  | 680 | int write = 10; | 
|  | 681 |  | 
|  | 682 | word = (0x80 | (reg & 0x7f)) << 16; | 
|  | 683 |  | 
|  | 684 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) | 
|  | 685 | ac97c_readl(chip, CORHR); | 
|  | 686 |  | 
|  | 687 | retry_write: | 
|  | 688 | timeout = 40; | 
|  | 689 |  | 
|  | 690 | do { | 
|  | 691 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) { | 
|  | 692 | ac97c_writel(chip, COTHR, word); | 
|  | 693 | goto read_reg; | 
|  | 694 | } | 
|  | 695 | udelay(10); | 
|  | 696 | } while (--timeout); | 
|  | 697 |  | 
|  | 698 | if (!--write) | 
|  | 699 | goto timed_out; | 
|  | 700 | goto retry_write; | 
|  | 701 |  | 
|  | 702 | read_reg: | 
|  | 703 | do { | 
|  | 704 | if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) { | 
|  | 705 | unsigned short val = ac97c_readl(chip, CORHR); | 
|  | 706 | return val; | 
|  | 707 | } | 
|  | 708 | udelay(10); | 
|  | 709 | } while (--timeout); | 
|  | 710 |  | 
|  | 711 | if (!--write) | 
|  | 712 | goto timed_out; | 
|  | 713 | goto retry_write; | 
|  | 714 |  | 
|  | 715 | timed_out: | 
|  | 716 | dev_dbg(&chip->pdev->dev, "codec read timeout\n"); | 
|  | 717 | return 0xffff; | 
|  | 718 | } | 
|  | 719 |  | 
|  | 720 | static bool filter(struct dma_chan *chan, void *slave) | 
|  | 721 | { | 
|  | 722 | struct dw_dma_slave *dws = slave; | 
|  | 723 |  | 
|  | 724 | if (dws->dma_dev == chan->device->dev) { | 
|  | 725 | chan->private = dws; | 
|  | 726 | return true; | 
|  | 727 | } else | 
|  | 728 | return false; | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | static void atmel_ac97c_reset(struct atmel_ac97c *chip) | 
|  | 732 | { | 
| Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 733 | ac97c_writel(chip, MR,   0); | 
|  | 734 | ac97c_writel(chip, MR,   AC97C_MR_ENA); | 
|  | 735 | ac97c_writel(chip, CAMR, 0); | 
|  | 736 | ac97c_writel(chip, COMR, 0); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 737 |  | 
|  | 738 | if (gpio_is_valid(chip->reset_pin)) { | 
|  | 739 | gpio_set_value(chip->reset_pin, 0); | 
|  | 740 | /* AC97 v2.2 specifications says minimum 1 us. */ | 
| Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 741 | udelay(2); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 742 | gpio_set_value(chip->reset_pin, 1); | 
|  | 743 | } | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 744 | } | 
|  | 745 |  | 
|  | 746 | static int __devinit atmel_ac97c_probe(struct platform_device *pdev) | 
|  | 747 | { | 
|  | 748 | struct snd_card			*card; | 
|  | 749 | struct atmel_ac97c		*chip; | 
|  | 750 | struct resource			*regs; | 
|  | 751 | struct ac97c_platform_data	*pdata; | 
|  | 752 | struct clk			*pclk; | 
|  | 753 | static struct snd_ac97_bus_ops	ops = { | 
|  | 754 | .write	= atmel_ac97c_write, | 
|  | 755 | .read	= atmel_ac97c_read, | 
|  | 756 | }; | 
|  | 757 | int				retval; | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 758 | int				irq; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 759 |  | 
|  | 760 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 761 | if (!regs) { | 
|  | 762 | dev_dbg(&pdev->dev, "no memory resource\n"); | 
|  | 763 | return -ENXIO; | 
|  | 764 | } | 
|  | 765 |  | 
|  | 766 | pdata = pdev->dev.platform_data; | 
|  | 767 | if (!pdata) { | 
|  | 768 | dev_dbg(&pdev->dev, "no platform data\n"); | 
|  | 769 | return -ENXIO; | 
|  | 770 | } | 
|  | 771 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 772 | irq = platform_get_irq(pdev, 0); | 
|  | 773 | if (irq < 0) { | 
|  | 774 | dev_dbg(&pdev->dev, "could not get irq\n"); | 
|  | 775 | return -ENXIO; | 
|  | 776 | } | 
|  | 777 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 778 | pclk = clk_get(&pdev->dev, "pclk"); | 
|  | 779 | if (IS_ERR(pclk)) { | 
|  | 780 | dev_dbg(&pdev->dev, "no peripheral clock\n"); | 
|  | 781 | return PTR_ERR(pclk); | 
|  | 782 | } | 
|  | 783 | clk_enable(pclk); | 
|  | 784 |  | 
|  | 785 | retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, | 
|  | 786 | THIS_MODULE, sizeof(struct atmel_ac97c), &card); | 
|  | 787 | if (retval) { | 
|  | 788 | dev_dbg(&pdev->dev, "could not create sound card device\n"); | 
|  | 789 | goto err_snd_card_new; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | chip = get_chip(card); | 
|  | 793 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 794 | retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip); | 
|  | 795 | if (retval) { | 
|  | 796 | dev_dbg(&pdev->dev, "unable to request irq %d\n", irq); | 
|  | 797 | goto err_request_irq; | 
|  | 798 | } | 
|  | 799 | chip->irq = irq; | 
|  | 800 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 801 | spin_lock_init(&chip->lock); | 
|  | 802 |  | 
|  | 803 | strcpy(card->driver, "Atmel AC97C"); | 
|  | 804 | strcpy(card->shortname, "Atmel AC97C"); | 
|  | 805 | sprintf(card->longname, "Atmel AC97 controller"); | 
|  | 806 |  | 
|  | 807 | chip->card = card; | 
|  | 808 | chip->pclk = pclk; | 
|  | 809 | chip->pdev = pdev; | 
|  | 810 | chip->regs = ioremap(regs->start, regs->end - regs->start + 1); | 
|  | 811 |  | 
|  | 812 | if (!chip->regs) { | 
|  | 813 | dev_dbg(&pdev->dev, "could not remap register memory\n"); | 
|  | 814 | goto err_ioremap; | 
|  | 815 | } | 
|  | 816 |  | 
|  | 817 | if (gpio_is_valid(pdata->reset_pin)) { | 
|  | 818 | if (gpio_request(pdata->reset_pin, "reset_pin")) { | 
|  | 819 | dev_dbg(&pdev->dev, "reset pin not available\n"); | 
|  | 820 | chip->reset_pin = -ENODEV; | 
|  | 821 | } else { | 
|  | 822 | gpio_direction_output(pdata->reset_pin, 1); | 
|  | 823 | chip->reset_pin = pdata->reset_pin; | 
|  | 824 | } | 
|  | 825 | } | 
|  | 826 |  | 
|  | 827 | snd_card_set_dev(card, &pdev->dev); | 
|  | 828 |  | 
| Hans-Christian Egtvedt | 81baf3a | 2009-04-02 08:21:15 +0200 | [diff] [blame] | 829 | atmel_ac97c_reset(chip); | 
|  | 830 |  | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 831 | /* Enable overrun interrupt from codec channel */ | 
|  | 832 | ac97c_writel(chip, COMR, AC97C_CSR_OVRUN); | 
|  | 833 | ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT); | 
|  | 834 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 835 | retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus); | 
|  | 836 | if (retval) { | 
|  | 837 | dev_dbg(&pdev->dev, "could not register on ac97 bus\n"); | 
|  | 838 | goto err_ac97_bus; | 
|  | 839 | } | 
|  | 840 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 841 | retval = atmel_ac97c_mixer_new(chip); | 
|  | 842 | if (retval) { | 
|  | 843 | dev_dbg(&pdev->dev, "could not register ac97 mixer\n"); | 
|  | 844 | goto err_ac97_bus; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | if (pdata->rx_dws.dma_dev) { | 
|  | 848 | struct dw_dma_slave *dws = &pdata->rx_dws; | 
|  | 849 | dma_cap_mask_t mask; | 
|  | 850 |  | 
|  | 851 | dws->rx_reg = regs->start + AC97C_CARHR + 2; | 
|  | 852 |  | 
|  | 853 | dma_cap_zero(mask); | 
|  | 854 | dma_cap_set(DMA_SLAVE, mask); | 
|  | 855 |  | 
|  | 856 | chip->dma.rx_chan = dma_request_channel(mask, filter, dws); | 
|  | 857 |  | 
|  | 858 | dev_info(&chip->pdev->dev, "using %s for DMA RX\n", | 
| Hans-Christian Egtvedt | 2357285 | 2009-04-02 08:21:17 +0200 | [diff] [blame] | 859 | dev_name(&chip->dma.rx_chan->dev->device)); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 860 | set_bit(DMA_RX_CHAN_PRESENT, &chip->flags); | 
|  | 861 | } | 
|  | 862 |  | 
|  | 863 | if (pdata->tx_dws.dma_dev) { | 
|  | 864 | struct dw_dma_slave *dws = &pdata->tx_dws; | 
|  | 865 | dma_cap_mask_t mask; | 
|  | 866 |  | 
|  | 867 | dws->tx_reg = regs->start + AC97C_CATHR + 2; | 
|  | 868 |  | 
|  | 869 | dma_cap_zero(mask); | 
|  | 870 | dma_cap_set(DMA_SLAVE, mask); | 
|  | 871 |  | 
|  | 872 | chip->dma.tx_chan = dma_request_channel(mask, filter, dws); | 
|  | 873 |  | 
|  | 874 | dev_info(&chip->pdev->dev, "using %s for DMA TX\n", | 
| Hans-Christian Egtvedt | 2357285 | 2009-04-02 08:21:17 +0200 | [diff] [blame] | 875 | dev_name(&chip->dma.tx_chan->dev->device)); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 876 | set_bit(DMA_TX_CHAN_PRESENT, &chip->flags); | 
|  | 877 | } | 
|  | 878 |  | 
|  | 879 | if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) && | 
|  | 880 | !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) { | 
|  | 881 | dev_dbg(&pdev->dev, "DMA not available\n"); | 
|  | 882 | retval = -ENODEV; | 
|  | 883 | goto err_dma; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | retval = atmel_ac97c_pcm_new(chip); | 
|  | 887 | if (retval) { | 
|  | 888 | dev_dbg(&pdev->dev, "could not register ac97 pcm device\n"); | 
|  | 889 | goto err_dma; | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | retval = snd_card_register(card); | 
|  | 893 | if (retval) { | 
|  | 894 | dev_dbg(&pdev->dev, "could not register sound card\n"); | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 895 | goto err_dma; | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 896 | } | 
|  | 897 |  | 
|  | 898 | platform_set_drvdata(pdev, card); | 
|  | 899 |  | 
|  | 900 | dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n", | 
|  | 901 | chip->regs); | 
|  | 902 |  | 
|  | 903 | return 0; | 
|  | 904 |  | 
|  | 905 | err_dma: | 
|  | 906 | if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags)) | 
|  | 907 | dma_release_channel(chip->dma.rx_chan); | 
|  | 908 | if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) | 
|  | 909 | dma_release_channel(chip->dma.tx_chan); | 
|  | 910 | clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags); | 
|  | 911 | clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags); | 
|  | 912 | chip->dma.rx_chan = NULL; | 
|  | 913 | chip->dma.tx_chan = NULL; | 
|  | 914 | err_ac97_bus: | 
|  | 915 | snd_card_set_dev(card, NULL); | 
|  | 916 |  | 
|  | 917 | if (gpio_is_valid(chip->reset_pin)) | 
|  | 918 | gpio_free(chip->reset_pin); | 
|  | 919 |  | 
|  | 920 | iounmap(chip->regs); | 
|  | 921 | err_ioremap: | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 922 | free_irq(irq, chip); | 
|  | 923 | err_request_irq: | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 924 | snd_card_free(card); | 
|  | 925 | err_snd_card_new: | 
|  | 926 | clk_disable(pclk); | 
|  | 927 | clk_put(pclk); | 
|  | 928 | return retval; | 
|  | 929 | } | 
|  | 930 |  | 
|  | 931 | #ifdef CONFIG_PM | 
|  | 932 | static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg) | 
|  | 933 | { | 
|  | 934 | struct snd_card *card = platform_get_drvdata(pdev); | 
|  | 935 | struct atmel_ac97c *chip = card->private_data; | 
|  | 936 |  | 
|  | 937 | if (test_bit(DMA_RX_READY, &chip->flags)) | 
|  | 938 | dw_dma_cyclic_stop(chip->dma.rx_chan); | 
|  | 939 | if (test_bit(DMA_TX_READY, &chip->flags)) | 
|  | 940 | dw_dma_cyclic_stop(chip->dma.tx_chan); | 
|  | 941 | clk_disable(chip->pclk); | 
|  | 942 |  | 
|  | 943 | return 0; | 
|  | 944 | } | 
|  | 945 |  | 
|  | 946 | static int atmel_ac97c_resume(struct platform_device *pdev) | 
|  | 947 | { | 
|  | 948 | struct snd_card *card = platform_get_drvdata(pdev); | 
|  | 949 | struct atmel_ac97c *chip = card->private_data; | 
|  | 950 |  | 
|  | 951 | clk_enable(chip->pclk); | 
|  | 952 | if (test_bit(DMA_RX_READY, &chip->flags)) | 
|  | 953 | dw_dma_cyclic_start(chip->dma.rx_chan); | 
|  | 954 | if (test_bit(DMA_TX_READY, &chip->flags)) | 
|  | 955 | dw_dma_cyclic_start(chip->dma.tx_chan); | 
|  | 956 |  | 
|  | 957 | return 0; | 
|  | 958 | } | 
|  | 959 | #else | 
|  | 960 | #define atmel_ac97c_suspend NULL | 
|  | 961 | #define atmel_ac97c_resume NULL | 
|  | 962 | #endif | 
|  | 963 |  | 
|  | 964 | static int __devexit atmel_ac97c_remove(struct platform_device *pdev) | 
|  | 965 | { | 
|  | 966 | struct snd_card *card = platform_get_drvdata(pdev); | 
|  | 967 | struct atmel_ac97c *chip = get_chip(card); | 
|  | 968 |  | 
|  | 969 | if (gpio_is_valid(chip->reset_pin)) | 
|  | 970 | gpio_free(chip->reset_pin); | 
|  | 971 |  | 
| Hans-Christian Egtvedt | bd74a18 | 2009-04-02 08:21:16 +0200 | [diff] [blame] | 972 | ac97c_writel(chip, CAMR, 0); | 
|  | 973 | ac97c_writel(chip, COMR, 0); | 
|  | 974 | ac97c_writel(chip, MR,   0); | 
|  | 975 |  | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 976 | clk_disable(chip->pclk); | 
|  | 977 | clk_put(chip->pclk); | 
|  | 978 | iounmap(chip->regs); | 
| Hans-Christian Egtvedt | df16358 | 2009-04-02 08:21:14 +0200 | [diff] [blame] | 979 | free_irq(chip->irq, chip); | 
| Hans-Christian Egtvedt | 4ede028 | 2009-02-05 13:11:00 +0100 | [diff] [blame] | 980 |  | 
|  | 981 | if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags)) | 
|  | 982 | dma_release_channel(chip->dma.rx_chan); | 
|  | 983 | if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) | 
|  | 984 | dma_release_channel(chip->dma.tx_chan); | 
|  | 985 | clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags); | 
|  | 986 | clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags); | 
|  | 987 | chip->dma.rx_chan = NULL; | 
|  | 988 | chip->dma.tx_chan = NULL; | 
|  | 989 |  | 
|  | 990 | snd_card_set_dev(card, NULL); | 
|  | 991 | snd_card_free(card); | 
|  | 992 |  | 
|  | 993 | platform_set_drvdata(pdev, NULL); | 
|  | 994 |  | 
|  | 995 | return 0; | 
|  | 996 | } | 
|  | 997 |  | 
|  | 998 | static struct platform_driver atmel_ac97c_driver = { | 
|  | 999 | .remove		= __devexit_p(atmel_ac97c_remove), | 
|  | 1000 | .driver		= { | 
|  | 1001 | .name	= "atmel_ac97c", | 
|  | 1002 | }, | 
|  | 1003 | .suspend	= atmel_ac97c_suspend, | 
|  | 1004 | .resume		= atmel_ac97c_resume, | 
|  | 1005 | }; | 
|  | 1006 |  | 
|  | 1007 | static int __init atmel_ac97c_init(void) | 
|  | 1008 | { | 
|  | 1009 | return platform_driver_probe(&atmel_ac97c_driver, | 
|  | 1010 | atmel_ac97c_probe); | 
|  | 1011 | } | 
|  | 1012 | module_init(atmel_ac97c_init); | 
|  | 1013 |  | 
|  | 1014 | static void __exit atmel_ac97c_exit(void) | 
|  | 1015 | { | 
|  | 1016 | platform_driver_unregister(&atmel_ac97c_driver); | 
|  | 1017 | } | 
|  | 1018 | module_exit(atmel_ac97c_exit); | 
|  | 1019 |  | 
|  | 1020 | MODULE_LICENSE("GPL"); | 
|  | 1021 | MODULE_DESCRIPTION("Driver for Atmel AC97 controller"); | 
|  | 1022 | MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>"); |