Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7177395f authored by Sedji Gaouaou's avatar Sedji Gaouaou Committed by Takashi Iwai
Browse files

ALSA: AC97: add AC97 support for AT91.



This patch add AC97 support for ATMEL AT91, using the AVR32 code.
While AVR is using a DMA, the AT91 chips are using a Peripheral Data
Controller.

Signed-off-by: default avatarSedji Gaouaou <sedji.gaouaou@atmel.com>
Acked-by: default avatarLiam Girdwood <lrg@slimlogic.co.uk>
Acked-by: default avatarMark Brown <broonie@opensource.wolfsonmicro.com>
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent ae761481
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ config SND_ATMEL_AC97C
	tristate "Atmel AC97 Controller (AC97C) driver"
	select SND_PCM
	select SND_AC97_CODEC
	depends on DW_DMAC && AVR32
	depends on (DW_DMAC && AVR32) || ARCH_AT91
	help
	  ALSA sound driver for the Atmel AC97 controller.

+254 −85
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include <linux/device.h>
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
#include <linux/atmel_pdc.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
@@ -31,6 +32,10 @@

#include <linux/dw_dmac.h>

#include <mach/cpu.h>
#include <mach/hardware.h>
#include <mach/gpio.h>

#include "ac97c.h"

enum {
@@ -63,6 +68,7 @@ struct atmel_ac97c {
	u64				cur_format;
	unsigned int			cur_rate;
	unsigned long			flags;
	int				playback_period, capture_period;
	/* Serialize access to opened variable */
	spinlock_t			lock;
	void __iomem			*regs;
@@ -242,10 +248,12 @@ static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
	if (retval < 0)
		return retval;
	/* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
	if (cpu_is_at32ap7000()) {
		/* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
		if (retval == 1)
			if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
				dw_dma_cyclic_free(chip->dma.tx_chan);

	}
	/* Set restrictions to params. */
	mutex_lock(&opened_mutex);
	chip->cur_rate = params_rate(hw_params);
@@ -266,9 +274,14 @@ static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
	if (retval < 0)
		return retval;
	/* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
	if (cpu_is_at32ap7000()) {
		if (retval < 0)
			return retval;
		/* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
		if (retval == 1)
			if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
				dw_dma_cyclic_free(chip->dma.rx_chan);
	}

	/* Set restrictions to params. */
	mutex_lock(&opened_mutex);
@@ -282,16 +295,20 @@ static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	if (cpu_is_at32ap7000()) {
		if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
			dw_dma_cyclic_free(chip->dma.tx_chan);
	}
	return snd_pcm_lib_free_pages(substream);
}

static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	if (cpu_is_at32ap7000()) {
		if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
			dw_dma_cyclic_free(chip->dma.rx_chan);
	}
	return snd_pcm_lib_free_pages(substream);
}

@@ -299,9 +316,11 @@ static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	int block_size = frames_to_bytes(runtime, runtime->period_size);
	unsigned long word = ac97c_readl(chip, OCA);
	int retval;

	chip->playback_period = 0;
	word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));

	/* assign channels to AC97C channel A */
@@ -324,6 +343,7 @@ static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)

	switch (runtime->format) {
	case SNDRV_PCM_FORMAT_S16_LE:
		if (cpu_is_at32ap7000())
			word |= AC97C_CMR_CEM_LITTLE;
		break;
	case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
@@ -363,9 +383,18 @@ static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
		dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
				runtime->rate);

	if (cpu_is_at32ap7000()) {
		if (!test_bit(DMA_TX_READY, &chip->flags))
			retval = atmel_ac97c_prepare_dma(chip, substream,
					DMA_TO_DEVICE);
	} else {
		/* Initialize and start the PDC */
		writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
		writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
		writel(runtime->dma_addr + block_size,
				chip->regs + ATMEL_PDC_TNPR);
		writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
	}

	return retval;
}
@@ -374,9 +403,11 @@ static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	int block_size = frames_to_bytes(runtime, runtime->period_size);
	unsigned long word = ac97c_readl(chip, ICA);
	int retval;

	chip->capture_period = 0;
	word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));

	/* assign channels to AC97C channel A */
@@ -399,6 +430,7 @@ static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)

	switch (runtime->format) {
	case SNDRV_PCM_FORMAT_S16_LE:
		if (cpu_is_at32ap7000())
			word |= AC97C_CMR_CEM_LITTLE;
		break;
	case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
@@ -438,9 +470,18 @@ static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
		dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
				runtime->rate);

	if (cpu_is_at32ap7000()) {
		if (!test_bit(DMA_RX_READY, &chip->flags))
			retval = atmel_ac97c_prepare_dma(chip, substream,
					DMA_FROM_DEVICE);
	} else {
		/* Initialize and start the PDC */
		writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
		writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
		writel(runtime->dma_addr + block_size,
				chip->regs + ATMEL_PDC_RNPR);
		writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
	}

	return retval;
}
@@ -449,7 +490,7 @@ static int
atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	unsigned long camr;
	unsigned long camr, ptcr = 0;
	int retval = 0;

	camr = ac97c_readl(chip, CAMR);
@@ -458,15 +499,22 @@ atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
	case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
	case SNDRV_PCM_TRIGGER_START:
		if (cpu_is_at32ap7000()) {
			retval = dw_dma_cyclic_start(chip->dma.tx_chan);
			if (retval)
				goto out;
		} else {
			ptcr = ATMEL_PDC_TXTEN;
		}
		camr |= AC97C_CMR_CENA;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
	case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
	case SNDRV_PCM_TRIGGER_STOP:
		if (cpu_is_at32ap7000())
			dw_dma_cyclic_stop(chip->dma.tx_chan);
		else
			ptcr |= ATMEL_PDC_TXTDIS;
		if (chip->opened <= 1)
			camr &= ~AC97C_CMR_CENA;
		break;
@@ -476,6 +524,8 @@ atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
	}

	ac97c_writel(chip, CAMR, camr);
	if (!cpu_is_at32ap7000())
		writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
out:
	return retval;
}
@@ -484,24 +534,32 @@ static int
atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
	unsigned long camr;
	unsigned long camr, ptcr = 0;
	int retval = 0;

	camr = ac97c_readl(chip, CAMR);
	ptcr = readl(chip->regs + ATMEL_PDC_PTSR);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
	case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
	case SNDRV_PCM_TRIGGER_START:
		if (cpu_is_at32ap7000()) {
			retval = dw_dma_cyclic_start(chip->dma.rx_chan);
			if (retval)
				goto out;
		} else {
			ptcr = ATMEL_PDC_RXTEN;
		}
		camr |= AC97C_CMR_CENA;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
	case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
	case SNDRV_PCM_TRIGGER_STOP:
		if (cpu_is_at32ap7000())
			dw_dma_cyclic_stop(chip->dma.rx_chan);
		else
			ptcr |= (ATMEL_PDC_RXTDIS);
		if (chip->opened <= 1)
			camr &= ~AC97C_CMR_CENA;
		break;
@@ -511,6 +569,8 @@ atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
	}

	ac97c_writel(chip, CAMR, camr);
	if (!cpu_is_at32ap7000())
		writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
out:
	return retval;
}
@@ -523,7 +583,10 @@ atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
	snd_pcm_uframes_t	frames;
	unsigned long		bytes;

	if (cpu_is_at32ap7000())
		bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
	else
		bytes = readl(chip->regs + ATMEL_PDC_TPR);
	bytes -= runtime->dma_addr;

	frames = bytes_to_frames(runtime, bytes);
@@ -540,7 +603,10 @@ atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
	snd_pcm_uframes_t	frames;
	unsigned long		bytes;

	if (cpu_is_at32ap7000())
		bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
	else
		bytes = readl(chip->regs + ATMEL_PDC_RPR);
	bytes -= runtime->dma_addr;

	frames = bytes_to_frames(runtime, bytes);
@@ -578,8 +644,11 @@ static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
	u32			sr     = ac97c_readl(chip, SR);
	u32			casr   = ac97c_readl(chip, CASR);
	u32			cosr   = ac97c_readl(chip, COSR);
	u32			camr   = ac97c_readl(chip, CAMR);

	if (sr & AC97C_SR_CAEVT) {
		struct snd_pcm_runtime *runtime;
		int offset, next_period, block_size;
		dev_info(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
				casr & AC97C_CSR_OVRUN   ? " OVRUN"   : "",
				casr & AC97C_CSR_RXRDY   ? " RXRDY"   : "",
@@ -587,6 +656,50 @@ static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
				casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
				casr & AC97C_CSR_TXRDY   ? " TXRDY"   : "",
				!casr                    ? " NONE"    : "");
		if (!cpu_is_at32ap7000()) {
			if ((casr & camr) & AC97C_CSR_ENDTX) {
				runtime = chip->playback_substream->runtime;
				block_size = frames_to_bytes(runtime,
						runtime->period_size);
				chip->playback_period++;

				if (chip->playback_period == runtime->periods)
					chip->playback_period = 0;
				next_period = chip->playback_period + 1;
				if (next_period == runtime->periods)
					next_period = 0;

				offset = block_size * next_period;

				writel(runtime->dma_addr + offset,
						chip->regs + ATMEL_PDC_TNPR);
				writel(block_size / 2,
						chip->regs + ATMEL_PDC_TNCR);

				snd_pcm_period_elapsed(
						chip->playback_substream);
			}
			if ((casr & camr) & AC97C_CSR_ENDRX) {
				runtime = chip->capture_substream->runtime;
				block_size = frames_to_bytes(runtime,
						runtime->period_size);
				chip->capture_period++;

				if (chip->capture_period == runtime->periods)
					chip->capture_period = 0;
				next_period = chip->capture_period + 1;
				if (next_period == runtime->periods)
					next_period = 0;

				offset = block_size * next_period;

				writel(runtime->dma_addr + offset,
						chip->regs + ATMEL_PDC_RNPR);
				writel(block_size / 2,
						chip->regs + ATMEL_PDC_RNCR);
				snd_pcm_period_elapsed(chip->capture_substream);
			}
		}
		retval = IRQ_HANDLED;
	}

@@ -608,15 +721,50 @@ static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
	return retval;
}

static struct ac97_pcm at91_ac97_pcm_defs[] __devinitdata = {
	/* Playback */
	{
		.exclusive = 1,
		.r = { {
			.slots = ((1 << AC97_SLOT_PCM_LEFT)
				  | (1 << AC97_SLOT_PCM_RIGHT)),
		} },
	},
	/* PCM in */
	{
		.stream = 1,
		.exclusive = 1,
		.r = { {
			.slots = ((1 << AC97_SLOT_PCM_LEFT)
					| (1 << AC97_SLOT_PCM_RIGHT)),
		} }
	},
	/* Mic in */
	{
		.stream = 1,
		.exclusive = 1,
		.r = { {
			.slots = (1<<AC97_SLOT_MIC),
		} }
	},
};

static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
{
	struct snd_pcm		*pcm;
	struct snd_pcm_hardware	hw = atmel_ac97c_hw;
	int			capture, playback, retval;
	int			capture, playback, retval, err;

	capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
	playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);

	if (!cpu_is_at32ap7000()) {
		err = snd_ac97_pcm_assign(chip->ac97_bus,
				ARRAY_SIZE(at91_ac97_pcm_defs),
				at91_ac97_pcm_defs);
		if (err)
			return err;
	}
	retval = snd_pcm_new(chip->card, chip->card->shortname,
			chip->pdev->id, playback, capture, &pcm);
	if (retval)
@@ -775,7 +923,12 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
		return -ENXIO;
	}

	if (cpu_is_at32ap7000()) {
		pclk = clk_get(&pdev->dev, "pclk");
	} else {
		pclk = clk_get(&pdev->dev, "ac97_clk");
	}

	if (IS_ERR(pclk)) {
		dev_dbg(&pdev->dev, "no peripheral clock\n");
		return PTR_ERR(pclk);
@@ -844,6 +997,7 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
		goto err_ac97_bus;
	}

	if (cpu_is_at32ap7000()) {
		if (pdata->rx_dws.dma_dev) {
			struct dw_dma_slave *dws = &pdata->rx_dws;
			dma_cap_mask_t mask;
@@ -853,7 +1007,8 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
			dma_cap_zero(mask);
			dma_cap_set(DMA_SLAVE, mask);

		chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
			chip->dma.rx_chan = dma_request_channel(mask, filter,
								dws);

			dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
				dev_name(&chip->dma.rx_chan->dev->device));
@@ -869,7 +1024,8 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
			dma_cap_zero(mask);
			dma_cap_set(DMA_SLAVE, mask);

		chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
			chip->dma.tx_chan = dma_request_channel(mask, filter,
								dws);

			dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
				dev_name(&chip->dma.tx_chan->dev->device));
@@ -882,6 +1038,12 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
			retval = -ENODEV;
			goto err_dma;
		}
	} else {
		/* Just pretend that we have DMA channel(for at91 i is actually
		 * the PDC) */
		set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
		set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
	}

	retval = atmel_ac97c_pcm_new(chip);
	if (retval) {
@@ -897,12 +1059,13 @@ static int __devinit atmel_ac97c_probe(struct platform_device *pdev)

	platform_set_drvdata(pdev, card);

	dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
			chip->regs);
	dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
			chip->regs, irq);

	return 0;

err_dma:
	if (cpu_is_at32ap7000()) {
		if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
			dma_release_channel(chip->dma.rx_chan);
		if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
@@ -911,6 +1074,7 @@ err_dma:
		clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
		chip->dma.rx_chan = NULL;
		chip->dma.tx_chan = NULL;
	}
err_ac97_bus:
	snd_card_set_dev(card, NULL);

@@ -934,10 +1098,12 @@ static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
	struct snd_card *card = platform_get_drvdata(pdev);
	struct atmel_ac97c *chip = card->private_data;

	if (cpu_is_at32ap7000()) {
		if (test_bit(DMA_RX_READY, &chip->flags))
			dw_dma_cyclic_stop(chip->dma.rx_chan);
		if (test_bit(DMA_TX_READY, &chip->flags))
			dw_dma_cyclic_stop(chip->dma.tx_chan);
	}
	clk_disable(chip->pclk);

	return 0;
@@ -949,11 +1115,12 @@ static int atmel_ac97c_resume(struct platform_device *pdev)
	struct atmel_ac97c *chip = card->private_data;

	clk_enable(chip->pclk);
	if (cpu_is_at32ap7000()) {
		if (test_bit(DMA_RX_READY, &chip->flags))
			dw_dma_cyclic_start(chip->dma.rx_chan);
		if (test_bit(DMA_TX_READY, &chip->flags))
			dw_dma_cyclic_start(chip->dma.tx_chan);

	}
	return 0;
}
#else
@@ -978,6 +1145,7 @@ static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
	iounmap(chip->regs);
	free_irq(chip->irq, chip);

	if (cpu_is_at32ap7000()) {
		if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
			dma_release_channel(chip->dma.rx_chan);
		if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
@@ -986,6 +1154,7 @@ static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
		clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
		chip->dma.rx_chan = NULL;
		chip->dma.tx_chan = NULL;
	}

	snd_card_set_dev(card, NULL);
	snd_card_free(card);