Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * imx-pcm-dma-mx2.c -- ALSA Soc Audio Layer |
| 3 | * |
| 4 | * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de> |
| 5 | * |
| 6 | * This code is based on code copyrighted by Freescale, |
| 7 | * Liam Girdwood, Javier Martin and probably others. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | */ |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 23 | #include <linux/dmaengine.h> |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 24 | #include <linux/types.h> |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 25 | |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/initval.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/pcm_params.h> |
| 30 | #include <sound/soc.h> |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 31 | #include <sound/dmaengine_pcm.h> |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 32 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 33 | #include <mach/dma.h> |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 34 | |
Shawn Guo | 4762fba | 2012-03-05 22:30:55 +0800 | [diff] [blame] | 35 | #include "imx-pcm.h" |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 36 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 37 | static bool filter(struct dma_chan *chan, void *param) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 38 | { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 39 | if (!imx_dma_is_general_purpose(chan)) |
| 40 | return false; |
| 41 | |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 42 | chan->private = param; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 43 | |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 44 | return true; |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 48 | struct snd_pcm_hw_params *params) |
| 49 | { |
| 50 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 51 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame] | 52 | struct imx_pcm_dma_params *dma_params; |
| 53 | struct dma_slave_config slave_config; |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame] | 54 | int ret; |
| 55 | |
| 56 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 57 | |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 58 | ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config); |
| 59 | if (ret) |
| 60 | return ret; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 61 | |
Linus Torvalds | ef08e78 | 2012-03-29 15:34:57 -0700 | [diff] [blame] | 62 | slave_config.device_fc = false; |
| 63 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 64 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 65 | slave_config.dst_addr = dma_params->dma_addr; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 66 | slave_config.dst_maxburst = dma_params->burstsize; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 67 | } else { |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 68 | slave_config.src_addr = dma_params->dma_addr; |
Sascha Hauer | 6584cb8 | 2011-07-06 11:18:33 +0200 | [diff] [blame] | 69 | slave_config.src_maxburst = dma_params->burstsize; |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Lars-Peter Clausen | 4564d10 | 2012-02-22 10:49:06 +0100 | [diff] [blame] | 72 | ret = dmaengine_slave_config(chan, &slave_config); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 73 | if (ret) |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 74 | return ret; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 75 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 76 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 77 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 81 | static struct snd_pcm_hardware snd_imx_hardware = { |
| 82 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 83 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 84 | SNDRV_PCM_INFO_MMAP | |
| 85 | SNDRV_PCM_INFO_MMAP_VALID | |
| 86 | SNDRV_PCM_INFO_PAUSE | |
| 87 | SNDRV_PCM_INFO_RESUME, |
| 88 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 89 | .rate_min = 8000, |
| 90 | .channels_min = 2, |
| 91 | .channels_max = 2, |
| 92 | .buffer_bytes_max = IMX_SSI_DMABUF_SIZE, |
| 93 | .period_bytes_min = 128, |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 94 | .period_bytes_max = 65535, /* Limited by SDMA engine */ |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 95 | .periods_min = 2, |
| 96 | .periods_max = 255, |
| 97 | .fifo_size = 0, |
| 98 | }; |
| 99 | |
| 100 | static int snd_imx_open(struct snd_pcm_substream *substream) |
| 101 | { |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 102 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 103 | struct imx_pcm_dma_params *dma_params; |
| 104 | struct imx_dma_data *dma_data; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 105 | int ret; |
| 106 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 107 | snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 108 | |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 109 | dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
| 110 | |
| 111 | dma_data = kzalloc(sizeof(*dma_data), GFP_KERNEL); |
| 112 | dma_data->peripheral_type = IMX_DMATYPE_SSI; |
| 113 | dma_data->priority = DMA_PRIO_HIGH; |
| 114 | dma_data->dma_request = dma_params->dma; |
| 115 | |
| 116 | ret = snd_dmaengine_pcm_open(substream, filter, dma_data); |
| 117 | if (ret) { |
| 118 | kfree(dma_data); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | snd_dmaengine_pcm_set_data(substream, dma_data); |
| 123 | |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int snd_imx_close(struct snd_pcm_substream *substream) |
| 128 | { |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 129 | struct imx_dma_data *dma_data = snd_dmaengine_pcm_get_data(substream); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 130 | |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 131 | snd_dmaengine_pcm_close(substream); |
| 132 | kfree(dma_data); |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 133 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static struct snd_pcm_ops imx_pcm_ops = { |
| 138 | .open = snd_imx_open, |
Sascha Hauer | bf974a0 | 2010-11-04 17:05:43 +0100 | [diff] [blame] | 139 | .close = snd_imx_close, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 140 | .ioctl = snd_pcm_lib_ioctl, |
| 141 | .hw_params = snd_imx_pcm_hw_params, |
Lars-Peter Clausen | c307e8e | 2012-02-22 10:49:09 +0100 | [diff] [blame] | 142 | .trigger = snd_dmaengine_pcm_trigger, |
| 143 | .pointer = snd_dmaengine_pcm_pointer, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 144 | .mmap = snd_imx_pcm_mmap, |
| 145 | }; |
| 146 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 147 | static struct snd_soc_platform_driver imx_soc_platform_mx2 = { |
| 148 | .ops = &imx_pcm_ops, |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 149 | .pcm_new = imx_pcm_new, |
| 150 | .pcm_free = imx_pcm_free, |
| 151 | }; |
| 152 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 153 | static int __devinit imx_soc_platform_probe(struct platform_device *pdev) |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 154 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 155 | return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 158 | static int __devexit imx_soc_platform_remove(struct platform_device *pdev) |
| 159 | { |
| 160 | snd_soc_unregister_platform(&pdev->dev); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static struct platform_driver imx_pcm_driver = { |
| 165 | .driver = { |
| 166 | .name = "imx-pcm-audio", |
| 167 | .owner = THIS_MODULE, |
| 168 | }, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 169 | .probe = imx_soc_platform_probe, |
| 170 | .remove = __devexit_p(imx_soc_platform_remove), |
| 171 | }; |
| 172 | |
Axel Lin | 7a24b2b | 2011-11-24 15:03:50 +0800 | [diff] [blame] | 173 | module_platform_driver(imx_pcm_driver); |
Arnaud Patard (Rtp) | 96dcabb | 2011-06-22 22:21:49 +0200 | [diff] [blame] | 174 | MODULE_LICENSE("GPL"); |
| 175 | MODULE_ALIAS("platform:imx-pcm-audio"); |