2 * Driver for Atmel AC97C
4 * Copyright (C) 2005-2009 Atmel Corporation
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.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/bitmap.h>
13 #include <linux/device.h>
14 #include <linux/atmel_pdc.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/mutex.h>
20 #include <linux/gpio.h>
21 #include <linux/types.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of_device.h>
27 #include <sound/core.h>
28 #include <sound/initval.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/ac97_codec.h>
32 #include <sound/atmel-ac97c.h>
33 #include <sound/memalloc.h>
37 /* Serialize access to opened variable */
38 static DEFINE_MUTEX(opened_mutex);
42 struct platform_device *pdev;
44 struct snd_pcm_substream *playback_substream;
45 struct snd_pcm_substream *capture_substream;
46 struct snd_card *card;
48 struct snd_ac97 *ac97;
49 struct snd_ac97_bus *ac97_bus;
52 unsigned int cur_rate;
53 int playback_period, capture_period;
54 /* Serialize access to opened variable */
62 #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
64 #define ac97c_writel(chip, reg, val) \
65 __raw_writel((val), (chip)->regs + AC97C_##reg)
66 #define ac97c_readl(chip, reg) \
67 __raw_readl((chip)->regs + AC97C_##reg)
69 static struct snd_pcm_hardware atmel_ac97c_hw = {
70 .info = (SNDRV_PCM_INFO_MMAP
71 | SNDRV_PCM_INFO_MMAP_VALID
72 | SNDRV_PCM_INFO_INTERLEAVED
73 | SNDRV_PCM_INFO_BLOCK_TRANSFER
74 | SNDRV_PCM_INFO_JOINT_DUPLEX
75 | SNDRV_PCM_INFO_RESUME
76 | SNDRV_PCM_INFO_PAUSE),
77 .formats = (SNDRV_PCM_FMTBIT_S16_BE
78 | SNDRV_PCM_FMTBIT_S16_LE),
79 .rates = (SNDRV_PCM_RATE_CONTINUOUS),
84 .buffer_bytes_max = 2 * 2 * 64 * 2048,
85 .period_bytes_min = 4096,
86 .period_bytes_max = 4096,
91 static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
93 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
94 struct snd_pcm_runtime *runtime = substream->runtime;
96 mutex_lock(&opened_mutex);
98 runtime->hw = atmel_ac97c_hw;
100 runtime->hw.rate_min = chip->cur_rate;
101 runtime->hw.rate_max = chip->cur_rate;
103 if (chip->cur_format)
104 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
105 mutex_unlock(&opened_mutex);
106 chip->playback_substream = substream;
110 static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
112 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
113 struct snd_pcm_runtime *runtime = substream->runtime;
115 mutex_lock(&opened_mutex);
117 runtime->hw = atmel_ac97c_hw;
118 if (chip->cur_rate) {
119 runtime->hw.rate_min = chip->cur_rate;
120 runtime->hw.rate_max = chip->cur_rate;
122 if (chip->cur_format)
123 runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
124 mutex_unlock(&opened_mutex);
125 chip->capture_substream = substream;
129 static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
131 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
133 mutex_lock(&opened_mutex);
137 chip->cur_format = 0;
139 mutex_unlock(&opened_mutex);
141 chip->playback_substream = NULL;
146 static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
148 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
150 mutex_lock(&opened_mutex);
154 chip->cur_format = 0;
156 mutex_unlock(&opened_mutex);
158 chip->capture_substream = NULL;
163 static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
164 struct snd_pcm_hw_params *hw_params)
166 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
169 retval = snd_pcm_lib_malloc_pages(substream,
170 params_buffer_bytes(hw_params));
174 /* Set restrictions to params. */
175 mutex_lock(&opened_mutex);
176 chip->cur_rate = params_rate(hw_params);
177 chip->cur_format = params_format(hw_params);
178 mutex_unlock(&opened_mutex);
183 static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
184 struct snd_pcm_hw_params *hw_params)
186 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
189 retval = snd_pcm_lib_malloc_pages(substream,
190 params_buffer_bytes(hw_params));
194 /* Set restrictions to params. */
195 mutex_lock(&opened_mutex);
196 chip->cur_rate = params_rate(hw_params);
197 chip->cur_format = params_format(hw_params);
198 mutex_unlock(&opened_mutex);
203 static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
205 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
206 struct snd_pcm_runtime *runtime = substream->runtime;
207 int block_size = frames_to_bytes(runtime, runtime->period_size);
208 unsigned long word = ac97c_readl(chip, OCA);
211 chip->playback_period = 0;
212 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
214 /* assign channels to AC97C channel A */
215 switch (runtime->channels) {
217 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
220 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
221 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
224 /* TODO: support more than two channels */
227 ac97c_writel(chip, OCA, word);
229 /* configure sample format and size */
230 word = ac97c_readl(chip, CAMR);
231 if (chip->opened <= 1)
232 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
234 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
236 switch (runtime->format) {
237 case SNDRV_PCM_FORMAT_S16_LE:
239 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
240 word &= ~(AC97C_CMR_CEM_LITTLE);
243 word = ac97c_readl(chip, OCA);
244 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
245 ac97c_writel(chip, OCA, word);
249 /* Enable underrun interrupt on channel A */
250 word |= AC97C_CSR_UNRUN;
252 ac97c_writel(chip, CAMR, word);
254 /* Enable channel A event interrupt */
255 word = ac97c_readl(chip, IMR);
256 word |= AC97C_SR_CAEVT;
257 ac97c_writel(chip, IER, word);
259 /* set variable rate if needed */
260 if (runtime->rate != 48000) {
261 word = ac97c_readl(chip, MR);
262 word |= AC97C_MR_VRA;
263 ac97c_writel(chip, MR, word);
265 word = ac97c_readl(chip, MR);
266 word &= ~(AC97C_MR_VRA);
267 ac97c_writel(chip, MR, word);
270 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
273 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
276 /* Initialize and start the PDC */
277 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
278 writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
279 writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_TNPR);
280 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
285 static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
287 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
288 struct snd_pcm_runtime *runtime = substream->runtime;
289 int block_size = frames_to_bytes(runtime, runtime->period_size);
290 unsigned long word = ac97c_readl(chip, ICA);
293 chip->capture_period = 0;
294 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
296 /* assign channels to AC97C channel A */
297 switch (runtime->channels) {
299 word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
302 word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
303 | AC97C_CH_ASSIGN(PCM_RIGHT, A);
306 /* TODO: support more than two channels */
309 ac97c_writel(chip, ICA, word);
311 /* configure sample format and size */
312 word = ac97c_readl(chip, CAMR);
313 if (chip->opened <= 1)
314 word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
316 word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
318 switch (runtime->format) {
319 case SNDRV_PCM_FORMAT_S16_LE:
321 case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
322 word &= ~(AC97C_CMR_CEM_LITTLE);
325 word = ac97c_readl(chip, ICA);
326 word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
327 ac97c_writel(chip, ICA, word);
331 /* Enable overrun interrupt on channel A */
332 word |= AC97C_CSR_OVRUN;
334 ac97c_writel(chip, CAMR, word);
336 /* Enable channel A event interrupt */
337 word = ac97c_readl(chip, IMR);
338 word |= AC97C_SR_CAEVT;
339 ac97c_writel(chip, IER, word);
341 /* set variable rate if needed */
342 if (runtime->rate != 48000) {
343 word = ac97c_readl(chip, MR);
344 word |= AC97C_MR_VRA;
345 ac97c_writel(chip, MR, word);
347 word = ac97c_readl(chip, MR);
348 word &= ~(AC97C_MR_VRA);
349 ac97c_writel(chip, MR, word);
352 retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
355 dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
358 /* Initialize and start the PDC */
359 writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
360 writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
361 writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_RNPR);
362 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
368 atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
370 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
371 unsigned long camr, ptcr = 0;
373 camr = ac97c_readl(chip, CAMR);
376 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
377 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
378 case SNDRV_PCM_TRIGGER_START:
379 ptcr = ATMEL_PDC_TXTEN;
380 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
382 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
383 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
384 case SNDRV_PCM_TRIGGER_STOP:
385 ptcr |= ATMEL_PDC_TXTDIS;
386 if (chip->opened <= 1)
387 camr &= ~AC97C_CMR_CENA;
393 ac97c_writel(chip, CAMR, camr);
394 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
399 atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
401 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
402 unsigned long camr, ptcr = 0;
404 camr = ac97c_readl(chip, CAMR);
405 ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
408 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
409 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
410 case SNDRV_PCM_TRIGGER_START:
411 ptcr = ATMEL_PDC_RXTEN;
412 camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
414 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
415 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
416 case SNDRV_PCM_TRIGGER_STOP:
417 ptcr |= ATMEL_PDC_RXTDIS;
418 if (chip->opened <= 1)
419 camr &= ~AC97C_CMR_CENA;
425 ac97c_writel(chip, CAMR, camr);
426 writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
430 static snd_pcm_uframes_t
431 atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
433 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
434 struct snd_pcm_runtime *runtime = substream->runtime;
435 snd_pcm_uframes_t frames;
438 bytes = readl(chip->regs + ATMEL_PDC_TPR);
439 bytes -= runtime->dma_addr;
441 frames = bytes_to_frames(runtime, bytes);
442 if (frames >= runtime->buffer_size)
443 frames -= runtime->buffer_size;
447 static snd_pcm_uframes_t
448 atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
450 struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
451 struct snd_pcm_runtime *runtime = substream->runtime;
452 snd_pcm_uframes_t frames;
455 bytes = readl(chip->regs + ATMEL_PDC_RPR);
456 bytes -= runtime->dma_addr;
458 frames = bytes_to_frames(runtime, bytes);
459 if (frames >= runtime->buffer_size)
460 frames -= runtime->buffer_size;
464 static struct snd_pcm_ops atmel_ac97_playback_ops = {
465 .open = atmel_ac97c_playback_open,
466 .close = atmel_ac97c_playback_close,
467 .ioctl = snd_pcm_lib_ioctl,
468 .hw_params = atmel_ac97c_playback_hw_params,
469 .hw_free = snd_pcm_lib_free_pages,
470 .prepare = atmel_ac97c_playback_prepare,
471 .trigger = atmel_ac97c_playback_trigger,
472 .pointer = atmel_ac97c_playback_pointer,
475 static struct snd_pcm_ops atmel_ac97_capture_ops = {
476 .open = atmel_ac97c_capture_open,
477 .close = atmel_ac97c_capture_close,
478 .ioctl = snd_pcm_lib_ioctl,
479 .hw_params = atmel_ac97c_capture_hw_params,
480 .hw_free = snd_pcm_lib_free_pages,
481 .prepare = atmel_ac97c_capture_prepare,
482 .trigger = atmel_ac97c_capture_trigger,
483 .pointer = atmel_ac97c_capture_pointer,
486 static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
488 struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
489 irqreturn_t retval = IRQ_NONE;
490 u32 sr = ac97c_readl(chip, SR);
491 u32 casr = ac97c_readl(chip, CASR);
492 u32 cosr = ac97c_readl(chip, COSR);
493 u32 camr = ac97c_readl(chip, CAMR);
495 if (sr & AC97C_SR_CAEVT) {
496 struct snd_pcm_runtime *runtime;
497 int offset, next_period, block_size;
498 dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
499 casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
500 casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
501 casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
502 casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
503 casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
504 !casr ? " NONE" : "");
505 if ((casr & camr) & AC97C_CSR_ENDTX) {
506 runtime = chip->playback_substream->runtime;
507 block_size = frames_to_bytes(runtime, runtime->period_size);
508 chip->playback_period++;
510 if (chip->playback_period == runtime->periods)
511 chip->playback_period = 0;
512 next_period = chip->playback_period + 1;
513 if (next_period == runtime->periods)
516 offset = block_size * next_period;
518 writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_TNPR);
519 writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
521 snd_pcm_period_elapsed(chip->playback_substream);
523 if ((casr & camr) & AC97C_CSR_ENDRX) {
524 runtime = chip->capture_substream->runtime;
525 block_size = frames_to_bytes(runtime, runtime->period_size);
526 chip->capture_period++;
528 if (chip->capture_period == runtime->periods)
529 chip->capture_period = 0;
530 next_period = chip->capture_period + 1;
531 if (next_period == runtime->periods)
534 offset = block_size * next_period;
536 writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_RNPR);
537 writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
538 snd_pcm_period_elapsed(chip->capture_substream);
540 retval = IRQ_HANDLED;
543 if (sr & AC97C_SR_COEVT) {
544 dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
545 cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
546 cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
547 cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
548 cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
549 !cosr ? " NONE" : "");
550 retval = IRQ_HANDLED;
553 if (retval == IRQ_NONE) {
554 dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
555 "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
561 static struct ac97_pcm at91_ac97_pcm_defs[] = {
566 .slots = ((1 << AC97_SLOT_PCM_LEFT)
567 | (1 << AC97_SLOT_PCM_RIGHT)),
575 .slots = ((1 << AC97_SLOT_PCM_LEFT)
576 | (1 << AC97_SLOT_PCM_RIGHT)),
584 .slots = (1<<AC97_SLOT_MIC),
589 static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
592 struct snd_pcm_hardware hw = atmel_ac97c_hw;
595 retval = snd_ac97_pcm_assign(chip->ac97_bus,
596 ARRAY_SIZE(at91_ac97_pcm_defs),
601 retval = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
605 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &atmel_ac97_capture_ops);
606 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_ac97_playback_ops);
608 retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
609 &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
610 hw.buffer_bytes_max);
614 pcm->private_data = chip;
616 strcpy(pcm->name, chip->card->shortname);
622 static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
624 struct snd_ac97_template template;
625 memset(&template, 0, sizeof(template));
626 template.private_data = chip;
627 return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
630 static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
633 struct atmel_ac97c *chip = get_chip(ac97);
637 word = (reg & 0x7f) << 16 | val;
640 if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
641 ac97c_writel(chip, COTHR, word);
647 dev_dbg(&chip->pdev->dev, "codec write timeout\n");
650 static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
653 struct atmel_ac97c *chip = get_chip(ac97);
658 word = (0x80 | (reg & 0x7f)) << 16;
660 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
661 ac97c_readl(chip, CORHR);
667 if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
668 ac97c_writel(chip, COTHR, word);
680 if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
681 unsigned short val = ac97c_readl(chip, CORHR);
692 dev_dbg(&chip->pdev->dev, "codec read timeout\n");
696 static void atmel_ac97c_reset(struct atmel_ac97c *chip)
698 ac97c_writel(chip, MR, 0);
699 ac97c_writel(chip, MR, AC97C_MR_ENA);
700 ac97c_writel(chip, CAMR, 0);
701 ac97c_writel(chip, COMR, 0);
703 if (gpio_is_valid(chip->reset_pin)) {
704 gpio_set_value(chip->reset_pin, 0);
705 /* AC97 v2.2 specifications says minimum 1 us. */
707 gpio_set_value(chip->reset_pin, 1);
709 ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
711 ac97c_writel(chip, MR, AC97C_MR_ENA);
716 static const struct of_device_id atmel_ac97c_dt_ids[] = {
717 { .compatible = "atmel,at91sam9263-ac97c", },
720 MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
722 static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
724 struct ac97c_platform_data *pdata;
725 struct device_node *node = dev->of_node;
728 dev_err(dev, "Device does not have associated DT data\n");
729 return ERR_PTR(-EINVAL);
732 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
734 return ERR_PTR(-ENOMEM);
736 pdata->reset_pin = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
741 static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
743 dev_err(dev, "no platform data defined\n");
744 return ERR_PTR(-ENXIO);
748 static int atmel_ac97c_probe(struct platform_device *pdev)
750 struct snd_card *card;
751 struct atmel_ac97c *chip;
752 struct resource *regs;
753 struct ac97c_platform_data *pdata;
755 static struct snd_ac97_bus_ops ops = {
756 .write = atmel_ac97c_write,
757 .read = atmel_ac97c_read,
762 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
764 dev_dbg(&pdev->dev, "no memory resource\n");
768 pdata = dev_get_platdata(&pdev->dev);
770 pdata = atmel_ac97c_probe_dt(&pdev->dev);
772 return PTR_ERR(pdata);
775 irq = platform_get_irq(pdev, 0);
777 dev_dbg(&pdev->dev, "could not get irq: %d\n", irq);
781 pclk = clk_get(&pdev->dev, "ac97_clk");
783 dev_dbg(&pdev->dev, "no peripheral clock\n");
784 return PTR_ERR(pclk);
786 clk_prepare_enable(pclk);
788 retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
789 SNDRV_DEFAULT_STR1, THIS_MODULE,
790 sizeof(struct atmel_ac97c), &card);
792 dev_dbg(&pdev->dev, "could not create sound card device\n");
793 goto err_snd_card_new;
796 chip = get_chip(card);
798 retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
800 dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
801 goto err_request_irq;
805 spin_lock_init(&chip->lock);
807 strcpy(card->driver, "Atmel AC97C");
808 strcpy(card->shortname, "Atmel AC97C");
809 sprintf(card->longname, "Atmel AC97 controller");
814 chip->regs = ioremap(regs->start, resource_size(regs));
817 dev_dbg(&pdev->dev, "could not remap register memory\n");
822 if (gpio_is_valid(pdata->reset_pin)) {
823 if (gpio_request(pdata->reset_pin, "reset_pin")) {
824 dev_dbg(&pdev->dev, "reset pin not available\n");
825 chip->reset_pin = -ENODEV;
827 gpio_direction_output(pdata->reset_pin, 1);
828 chip->reset_pin = pdata->reset_pin;
831 chip->reset_pin = -EINVAL;
834 atmel_ac97c_reset(chip);
836 /* Enable overrun interrupt from codec channel */
837 ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
838 ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
840 retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
842 dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
846 retval = atmel_ac97c_mixer_new(chip);
848 dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
852 retval = atmel_ac97c_pcm_new(chip);
854 dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
858 retval = snd_card_register(card);
860 dev_dbg(&pdev->dev, "could not register sound card\n");
864 platform_set_drvdata(pdev, card);
866 dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
872 if (gpio_is_valid(chip->reset_pin))
873 gpio_free(chip->reset_pin);
881 clk_disable_unprepare(pclk);
886 #ifdef CONFIG_PM_SLEEP
887 static int atmel_ac97c_suspend(struct device *pdev)
889 struct snd_card *card = dev_get_drvdata(pdev);
890 struct atmel_ac97c *chip = card->private_data;
892 clk_disable_unprepare(chip->pclk);
896 static int atmel_ac97c_resume(struct device *pdev)
898 struct snd_card *card = dev_get_drvdata(pdev);
899 struct atmel_ac97c *chip = card->private_data;
901 clk_prepare_enable(chip->pclk);
905 static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
906 #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
908 #define ATMEL_AC97C_PM_OPS NULL
911 static int atmel_ac97c_remove(struct platform_device *pdev)
913 struct snd_card *card = platform_get_drvdata(pdev);
914 struct atmel_ac97c *chip = get_chip(card);
916 if (gpio_is_valid(chip->reset_pin))
917 gpio_free(chip->reset_pin);
919 ac97c_writel(chip, CAMR, 0);
920 ac97c_writel(chip, COMR, 0);
921 ac97c_writel(chip, MR, 0);
923 clk_disable_unprepare(chip->pclk);
926 free_irq(chip->irq, chip);
933 static struct platform_driver atmel_ac97c_driver = {
934 .probe = atmel_ac97c_probe,
935 .remove = atmel_ac97c_remove,
937 .name = "atmel_ac97c",
938 .pm = ATMEL_AC97C_PM_OPS,
939 .of_match_table = of_match_ptr(atmel_ac97c_dt_ids),
942 module_platform_driver(atmel_ac97c_driver);
944 MODULE_LICENSE("GPL");
945 MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
946 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");