Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Au1000/Au1500/Au1100 Audio DMA support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * copied almost verbatim from the old ALSA driver, written by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *			Charles Eidsness <charles@cooper-street.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sound/pcm_params.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/mach-au1x00/au1000.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/mach-au1x00/au1000_dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "psc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define DRV_NAME "au1x_dma"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct pcm_period {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u32 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u32 relative_end;	/* relative to start of buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct pcm_period *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct audio_stream {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct pcm_period *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	unsigned int period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct alchemy_pcm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct audio_stream stream[2];	/* playback & capture */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void au1000_release_dma_link(struct audio_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct pcm_period *pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct pcm_period *pointer_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	stream->period_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	stream->periods = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	pointer = stream->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		pointer_next = pointer->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		kfree(pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		pointer = pointer_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	} while (pointer != stream->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	stream->buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static int au1000_setup_dma_link(struct audio_stream *stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				 unsigned int period_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 				 unsigned int periods)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct snd_pcm_substream *substream = stream->substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct pcm_period *pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	unsigned long dma_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	dma_start = virt_to_phys(runtime->dma_area);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (stream->period_size == period_bytes &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	    stream->periods == periods)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return 0; /* not changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	au1000_release_dma_link(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	stream->period_size = period_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	stream->periods = periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	stream->buffer = kmalloc(sizeof(struct pcm_period), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!stream->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	pointer = stream->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	for (i = 0; i < periods; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		pointer->start = (u32)(dma_start + (i * period_bytes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (i < periods - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			pointer->next = kmalloc(sizeof(struct pcm_period),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 						GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (!pointer->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				au1000_release_dma_link(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			pointer = pointer->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	pointer->next = stream->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void au1000_dma_stop(struct audio_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (stream->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		disable_dma(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void au1000_dma_start(struct audio_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (!stream->buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	init_dma(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (get_dma_active_buffer(stream->dma) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		clear_dma_done0(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		set_dma_addr0(stream->dma, stream->buffer->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		set_dma_count0(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		set_dma_addr1(stream->dma, stream->buffer->next->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		set_dma_count1(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		clear_dma_done1(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		set_dma_addr1(stream->dma, stream->buffer->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		set_dma_count1(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		set_dma_addr0(stream->dma, stream->buffer->next->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		set_dma_count0(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	enable_dma_buffers(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	start_dma(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static irqreturn_t au1000_dma_interrupt(int irq, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct audio_stream *stream = (struct audio_stream *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct snd_pcm_substream *substream = stream->substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	switch (get_dma_buffer_done(stream->dma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	case DMA_D0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		stream->buffer = stream->buffer->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		clear_dma_done0(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		set_dma_addr0(stream->dma, stream->buffer->next->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		set_dma_count0(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		enable_dma_buffer0(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	case DMA_D1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		stream->buffer = stream->buffer->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		clear_dma_done1(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		set_dma_addr1(stream->dma, stream->buffer->next->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		set_dma_count1(stream->dma, stream->period_size >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		enable_dma_buffer1(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	case (DMA_D0 | DMA_D1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		pr_debug("DMA %d missed interrupt.\n", stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		au1000_dma_stop(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		au1000_dma_start(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	case (~DMA_D0 & ~DMA_D1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		pr_debug("DMA %d empty irq.\n", stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	snd_pcm_period_elapsed(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static const struct snd_pcm_hardware alchemy_pcm_hardware = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	.info		  = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			    SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.period_bytes_min = 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	.period_bytes_max = 16 * 1024 - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.periods_min	  = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.periods_max	  = 255,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.buffer_bytes_max = 128 * 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.fifo_size	  = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static inline struct alchemy_pcm_ctx *ss_to_ctx(struct snd_pcm_substream *ss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 						struct snd_soc_component *component)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return snd_soc_component_get_drvdata(component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					    struct snd_soc_component *component)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return &(ctx->stream[ss->stream]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int alchemy_pcm_open(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			    struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int *dmaids, s = substream->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	dmaids = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (!dmaids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return -ENODEV;	/* whoa, has ordering changed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/* DMA setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	ctx->stream[s].dma = request_au1000_dma(dmaids[s], name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					au1000_dma_interrupt, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					&ctx->stream[s]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	set_dma_mode(ctx->stream[s].dma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		     get_dma_mode(ctx->stream[s].dma) & ~DMA_NC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ctx->stream[s].substream = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	ctx->stream[s].buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int alchemy_pcm_close(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			     struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	int stype = substream->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	ctx->stream[stype].substream = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	free_au1000_dma(ctx->stream[stype].dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int alchemy_pcm_hw_params(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				 struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				 struct snd_pcm_hw_params *hw_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct audio_stream *stream = ss_to_as(substream, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return au1000_setup_dma_link(stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				     params_period_bytes(hw_params),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				     params_periods(hw_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int alchemy_pcm_hw_free(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			       struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct audio_stream *stream = ss_to_as(substream, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	au1000_release_dma_link(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int alchemy_pcm_trigger(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			       struct snd_pcm_substream *substream, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	struct audio_stream *stream = ss_to_as(substream, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		au1000_dma_start(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		au1000_dma_stop(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 					     struct snd_pcm_substream *ss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct audio_stream *stream = ss_to_as(ss, component);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	long location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	location = get_dma_residue(stream->dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	location = stream->buffer->relative_end - location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (location == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		location = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return bytes_to_frames(ss->runtime, location);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int alchemy_pcm_new(struct snd_soc_component *component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			   struct snd_soc_pcm_runtime *rtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct snd_pcm *pcm = rtd->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 				       NULL, 65536, (4096 * 1024) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static struct snd_soc_component_driver alchemy_pcm_soc_component = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.name		= DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.open		= alchemy_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	.close		= alchemy_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	.hw_params	= alchemy_pcm_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	.hw_free	= alchemy_pcm_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	.trigger	= alchemy_pcm_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	.pointer	= alchemy_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	.pcm_construct	= alchemy_pcm_new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int alchemy_pcm_drvprobe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct alchemy_pcm_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	platform_set_drvdata(pdev, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return devm_snd_soc_register_component(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 					&alchemy_pcm_soc_component, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static struct platform_driver alchemy_pcmdma_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		.name	= "alchemy-pcm-dma",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	.probe		= alchemy_pcm_drvprobe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) module_platform_driver(alchemy_pcmdma_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) MODULE_AUTHOR("Manuel Lauss");