Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Au1000/Au1500/Au1100 Audio DMA support.
0004  *
0005  * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com>
0006  *
0007  * copied almost verbatim from the old ALSA driver, written by
0008  *          Charles Eidsness <charles@cooper-street.com>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/slab.h>
0015 #include <linux/dma-mapping.h>
0016 #include <sound/core.h>
0017 #include <sound/pcm.h>
0018 #include <sound/pcm_params.h>
0019 #include <sound/soc.h>
0020 #include <asm/mach-au1x00/au1000.h>
0021 #include <asm/mach-au1x00/au1000_dma.h>
0022 
0023 #include "psc.h"
0024 
0025 #define DRV_NAME "au1x_dma"
0026 
0027 struct pcm_period {
0028     u32 start;
0029     u32 relative_end;   /* relative to start of buffer */
0030     struct pcm_period *next;
0031 };
0032 
0033 struct audio_stream {
0034     struct snd_pcm_substream *substream;
0035     int dma;
0036     struct pcm_period *buffer;
0037     unsigned int period_size;
0038     unsigned int periods;
0039 };
0040 
0041 struct alchemy_pcm_ctx {
0042     struct audio_stream stream[2];  /* playback & capture */
0043 };
0044 
0045 static void au1000_release_dma_link(struct audio_stream *stream)
0046 {
0047     struct pcm_period *pointer;
0048     struct pcm_period *pointer_next;
0049 
0050     stream->period_size = 0;
0051     stream->periods = 0;
0052     pointer = stream->buffer;
0053     if (!pointer)
0054         return;
0055     do {
0056         pointer_next = pointer->next;
0057         kfree(pointer);
0058         pointer = pointer_next;
0059     } while (pointer != stream->buffer);
0060     stream->buffer = NULL;
0061 }
0062 
0063 static int au1000_setup_dma_link(struct audio_stream *stream,
0064                  unsigned int period_bytes,
0065                  unsigned int periods)
0066 {
0067     struct snd_pcm_substream *substream = stream->substream;
0068     struct snd_pcm_runtime *runtime = substream->runtime;
0069     struct pcm_period *pointer;
0070     unsigned long dma_start;
0071     int i;
0072 
0073     dma_start = virt_to_phys(runtime->dma_area);
0074 
0075     if (stream->period_size == period_bytes &&
0076         stream->periods == periods)
0077         return 0; /* not changed */
0078 
0079     au1000_release_dma_link(stream);
0080 
0081     stream->period_size = period_bytes;
0082     stream->periods = periods;
0083 
0084     stream->buffer = kmalloc(sizeof(struct pcm_period), GFP_KERNEL);
0085     if (!stream->buffer)
0086         return -ENOMEM;
0087     pointer = stream->buffer;
0088     for (i = 0; i < periods; i++) {
0089         pointer->start = (u32)(dma_start + (i * period_bytes));
0090         pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
0091         if (i < periods - 1) {
0092             pointer->next = kmalloc(sizeof(struct pcm_period),
0093                         GFP_KERNEL);
0094             if (!pointer->next) {
0095                 au1000_release_dma_link(stream);
0096                 return -ENOMEM;
0097             }
0098             pointer = pointer->next;
0099         }
0100     }
0101     pointer->next = stream->buffer;
0102     return 0;
0103 }
0104 
0105 static void au1000_dma_stop(struct audio_stream *stream)
0106 {
0107     if (stream->buffer)
0108         disable_dma(stream->dma);
0109 }
0110 
0111 static void au1000_dma_start(struct audio_stream *stream)
0112 {
0113     if (!stream->buffer)
0114         return;
0115 
0116     init_dma(stream->dma);
0117     if (get_dma_active_buffer(stream->dma) == 0) {
0118         clear_dma_done0(stream->dma);
0119         set_dma_addr0(stream->dma, stream->buffer->start);
0120         set_dma_count0(stream->dma, stream->period_size >> 1);
0121         set_dma_addr1(stream->dma, stream->buffer->next->start);
0122         set_dma_count1(stream->dma, stream->period_size >> 1);
0123     } else {
0124         clear_dma_done1(stream->dma);
0125         set_dma_addr1(stream->dma, stream->buffer->start);
0126         set_dma_count1(stream->dma, stream->period_size >> 1);
0127         set_dma_addr0(stream->dma, stream->buffer->next->start);
0128         set_dma_count0(stream->dma, stream->period_size >> 1);
0129     }
0130     enable_dma_buffers(stream->dma);
0131     start_dma(stream->dma);
0132 }
0133 
0134 static irqreturn_t au1000_dma_interrupt(int irq, void *ptr)
0135 {
0136     struct audio_stream *stream = (struct audio_stream *)ptr;
0137     struct snd_pcm_substream *substream = stream->substream;
0138 
0139     switch (get_dma_buffer_done(stream->dma)) {
0140     case DMA_D0:
0141         stream->buffer = stream->buffer->next;
0142         clear_dma_done0(stream->dma);
0143         set_dma_addr0(stream->dma, stream->buffer->next->start);
0144         set_dma_count0(stream->dma, stream->period_size >> 1);
0145         enable_dma_buffer0(stream->dma);
0146         break;
0147     case DMA_D1:
0148         stream->buffer = stream->buffer->next;
0149         clear_dma_done1(stream->dma);
0150         set_dma_addr1(stream->dma, stream->buffer->next->start);
0151         set_dma_count1(stream->dma, stream->period_size >> 1);
0152         enable_dma_buffer1(stream->dma);
0153         break;
0154     case (DMA_D0 | DMA_D1):
0155         pr_debug("DMA %d missed interrupt.\n", stream->dma);
0156         au1000_dma_stop(stream);
0157         au1000_dma_start(stream);
0158         break;
0159     case (~DMA_D0 & ~DMA_D1):
0160         pr_debug("DMA %d empty irq.\n", stream->dma);
0161     }
0162     snd_pcm_period_elapsed(substream);
0163     return IRQ_HANDLED;
0164 }
0165 
0166 static const struct snd_pcm_hardware alchemy_pcm_hardware = {
0167     .info         = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
0168                 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
0169     .period_bytes_min = 1024,
0170     .period_bytes_max = 16 * 1024 - 1,
0171     .periods_min      = 4,
0172     .periods_max      = 255,
0173     .buffer_bytes_max = 128 * 1024,
0174     .fifo_size    = 16,
0175 };
0176 
0177 static inline struct alchemy_pcm_ctx *ss_to_ctx(struct snd_pcm_substream *ss,
0178                         struct snd_soc_component *component)
0179 {
0180     return snd_soc_component_get_drvdata(component);
0181 }
0182 
0183 static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss,
0184                         struct snd_soc_component *component)
0185 {
0186     struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss, component);
0187     return &(ctx->stream[ss->stream]);
0188 }
0189 
0190 static int alchemy_pcm_open(struct snd_soc_component *component,
0191                 struct snd_pcm_substream *substream)
0192 {
0193     struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
0194     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0195     int *dmaids, s = substream->stream;
0196     char *name;
0197 
0198     dmaids = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
0199     if (!dmaids)
0200         return -ENODEV; /* whoa, has ordering changed? */
0201 
0202     /* DMA setup */
0203     name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx";
0204     ctx->stream[s].dma = request_au1000_dma(dmaids[s], name,
0205                     au1000_dma_interrupt, 0,
0206                     &ctx->stream[s]);
0207     set_dma_mode(ctx->stream[s].dma,
0208              get_dma_mode(ctx->stream[s].dma) & ~DMA_NC);
0209 
0210     ctx->stream[s].substream = substream;
0211     ctx->stream[s].buffer = NULL;
0212     snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware);
0213 
0214     return 0;
0215 }
0216 
0217 static int alchemy_pcm_close(struct snd_soc_component *component,
0218                  struct snd_pcm_substream *substream)
0219 {
0220     struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
0221     int stype = substream->stream;
0222 
0223     ctx->stream[stype].substream = NULL;
0224     free_au1000_dma(ctx->stream[stype].dma);
0225 
0226     return 0;
0227 }
0228 
0229 static int alchemy_pcm_hw_params(struct snd_soc_component *component,
0230                  struct snd_pcm_substream *substream,
0231                  struct snd_pcm_hw_params *hw_params)
0232 {
0233     struct audio_stream *stream = ss_to_as(substream, component);
0234 
0235     return au1000_setup_dma_link(stream,
0236                      params_period_bytes(hw_params),
0237                      params_periods(hw_params));
0238 }
0239 
0240 static int alchemy_pcm_hw_free(struct snd_soc_component *component,
0241                    struct snd_pcm_substream *substream)
0242 {
0243     struct audio_stream *stream = ss_to_as(substream, component);
0244     au1000_release_dma_link(stream);
0245     return 0;
0246 }
0247 
0248 static int alchemy_pcm_trigger(struct snd_soc_component *component,
0249                    struct snd_pcm_substream *substream, int cmd)
0250 {
0251     struct audio_stream *stream = ss_to_as(substream, component);
0252     int err = 0;
0253 
0254     switch (cmd) {
0255     case SNDRV_PCM_TRIGGER_START:
0256         au1000_dma_start(stream);
0257         break;
0258     case SNDRV_PCM_TRIGGER_STOP:
0259         au1000_dma_stop(stream);
0260         break;
0261     default:
0262         err = -EINVAL;
0263         break;
0264     }
0265     return err;
0266 }
0267 
0268 static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_soc_component *component,
0269                          struct snd_pcm_substream *ss)
0270 {
0271     struct audio_stream *stream = ss_to_as(ss, component);
0272     long location;
0273 
0274     location = get_dma_residue(stream->dma);
0275     location = stream->buffer->relative_end - location;
0276     if (location == -1)
0277         location = 0;
0278     return bytes_to_frames(ss->runtime, location);
0279 }
0280 
0281 static int alchemy_pcm_new(struct snd_soc_component *component,
0282                struct snd_soc_pcm_runtime *rtd)
0283 {
0284     struct snd_pcm *pcm = rtd->pcm;
0285 
0286     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
0287                        NULL, 65536, (4096 * 1024) - 1);
0288 
0289     return 0;
0290 }
0291 
0292 static struct snd_soc_component_driver alchemy_pcm_soc_component = {
0293     .name       = DRV_NAME,
0294     .open       = alchemy_pcm_open,
0295     .close      = alchemy_pcm_close,
0296     .hw_params  = alchemy_pcm_hw_params,
0297     .hw_free    = alchemy_pcm_hw_free,
0298     .trigger    = alchemy_pcm_trigger,
0299     .pointer    = alchemy_pcm_pointer,
0300     .pcm_construct  = alchemy_pcm_new,
0301 };
0302 
0303 static int alchemy_pcm_drvprobe(struct platform_device *pdev)
0304 {
0305     struct alchemy_pcm_ctx *ctx;
0306 
0307     ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
0308     if (!ctx)
0309         return -ENOMEM;
0310 
0311     platform_set_drvdata(pdev, ctx);
0312 
0313     return devm_snd_soc_register_component(&pdev->dev,
0314                     &alchemy_pcm_soc_component, NULL, 0);
0315 }
0316 
0317 static struct platform_driver alchemy_pcmdma_driver = {
0318     .driver = {
0319         .name   = "alchemy-pcm-dma",
0320     },
0321     .probe      = alchemy_pcm_drvprobe,
0322 };
0323 
0324 module_platform_driver(alchemy_pcmdma_driver);
0325 
0326 MODULE_LICENSE("GPL");
0327 MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver");
0328 MODULE_AUTHOR("Manuel Lauss");