Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 2 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 3 | #include <linux/slab.h> |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 4 | #include <linux/module.h> |
| 5 | #include <linux/dma-mapping.h> |
Daniel Mack | d65a145 | 2013-08-12 10:42:39 +0200 | [diff] [blame] | 6 | #include <linux/dmaengine.h> |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 7 | #include <linux/dma/pxa-dma.h> |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 8 | |
| 9 | #include <sound/core.h> |
| 10 | #include <sound/pcm.h> |
| 11 | #include <sound/pcm_params.h> |
| 12 | #include <sound/pxa2xx-lib.h> |
Daniel Mack | d65a145 | 2013-08-12 10:42:39 +0200 | [diff] [blame] | 13 | #include <sound/dmaengine_pcm.h> |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 14 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 15 | static const struct snd_pcm_hardware pxa2xx_pcm_hardware = { |
| 16 | .info = SNDRV_PCM_INFO_MMAP | |
| 17 | SNDRV_PCM_INFO_MMAP_VALID | |
| 18 | SNDRV_PCM_INFO_INTERLEAVED | |
| 19 | SNDRV_PCM_INFO_PAUSE | |
| 20 | SNDRV_PCM_INFO_RESUME, |
| 21 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
Daniel Mack | 456ec80 | 2018-06-27 21:33:56 +0200 | [diff] [blame] | 22 | SNDRV_PCM_FMTBIT_S24_LE | |
| 23 | SNDRV_PCM_FMTBIT_S32_LE, |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 24 | .period_bytes_min = 32, |
| 25 | .period_bytes_max = 8192 - 32, |
| 26 | .periods_min = 1, |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 27 | .periods_max = 256, |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 28 | .buffer_bytes_max = 128 * 1024, |
| 29 | .fifo_size = 32, |
| 30 | }; |
| 31 | |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 32 | int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 33 | struct snd_pcm_hw_params *params) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 34 | { |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 35 | struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream); |
| 36 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 37 | struct snd_dmaengine_dai_dma_data *dma_params; |
| 38 | struct dma_slave_config config; |
| 39 | int ret; |
Daniel Mack | d65a145 | 2013-08-12 10:42:39 +0200 | [diff] [blame] | 40 | |
Kuninori Morimoto | 575be88 | 2020-03-23 14:21:49 +0900 | [diff] [blame] | 41 | dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 42 | if (!dma_params) |
| 43 | return 0; |
Daniel Mack | d65a145 | 2013-08-12 10:42:39 +0200 | [diff] [blame] | 44 | |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 45 | ret = snd_hwparams_to_dma_slave_config(substream, params, &config); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
Kuninori Morimoto | 575be88 | 2020-03-23 14:21:49 +0900 | [diff] [blame] | 50 | snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream), |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 51 | &config); |
| 52 | |
| 53 | ret = dmaengine_slave_config(chan, &config); |
| 54 | if (ret) |
| 55 | return ret; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 56 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 57 | return 0; |
| 58 | } |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 59 | EXPORT_SYMBOL(pxa2xx_pcm_hw_params); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 60 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 61 | int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 62 | { |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 63 | return snd_dmaengine_pcm_trigger(substream, cmd); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL(pxa2xx_pcm_trigger); |
| 66 | |
| 67 | snd_pcm_uframes_t |
| 68 | pxa2xx_pcm_pointer(struct snd_pcm_substream *substream) |
| 69 | { |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 70 | return snd_dmaengine_pcm_pointer(substream); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 71 | } |
| 72 | EXPORT_SYMBOL(pxa2xx_pcm_pointer); |
| 73 | |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 74 | int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 75 | { |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 76 | return 0; |
| 77 | } |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 78 | EXPORT_SYMBOL(pxa2xx_pcm_prepare); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 79 | |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 80 | int pxa2xx_pcm_open(struct snd_pcm_substream *substream) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 81 | { |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 82 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 83 | struct snd_pcm_runtime *runtime = substream->runtime; |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 84 | struct snd_dmaengine_dai_dma_data *dma_params; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 85 | int ret; |
| 86 | |
| 87 | runtime->hw = pxa2xx_pcm_hardware; |
| 88 | |
Kuninori Morimoto | 575be88 | 2020-03-23 14:21:49 +0900 | [diff] [blame] | 89 | dma_params = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 90 | if (!dma_params) |
| 91 | return 0; |
| 92 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 93 | /* |
| 94 | * For mysterious reasons (and despite what the manual says) |
| 95 | * playback samples are lost if the DMA count is not a multiple |
| 96 | * of the DMA burst size. Let's add a rule to enforce that. |
| 97 | */ |
| 98 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 99 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32); |
| 100 | if (ret) |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 101 | return ret; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 102 | |
| 103 | ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 104 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32); |
| 105 | if (ret) |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 106 | return ret; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 107 | |
| 108 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 109 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 110 | if (ret < 0) |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 111 | return ret; |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 112 | |
Robert Jarzmik | 8f54061 | 2018-06-28 22:08:37 +0200 | [diff] [blame] | 113 | return snd_dmaengine_pcm_open( |
Kuninori Morimoto | 575be88 | 2020-03-23 14:21:49 +0900 | [diff] [blame] | 114 | substream, dma_request_slave_channel(asoc_rtd_to_cpu(rtd, 0)->dev, |
Robert Jarzmik | 8f54061 | 2018-06-28 22:08:37 +0200 | [diff] [blame] | 115 | dma_params->chan_name)); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 116 | } |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 117 | EXPORT_SYMBOL(pxa2xx_pcm_open); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 118 | |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 119 | int pxa2xx_pcm_close(struct snd_pcm_substream *substream) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 120 | { |
Daniel Mack | 58ceb57 | 2015-09-30 22:51:52 +0200 | [diff] [blame] | 121 | return snd_dmaengine_pcm_close_release_chan(substream); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 122 | } |
Daniel Mack | a716067 | 2018-06-27 21:33:54 +0200 | [diff] [blame] | 123 | EXPORT_SYMBOL(pxa2xx_pcm_close); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 124 | |
Takashi Iwai | 7f2da3d | 2021-08-02 09:28:05 +0200 | [diff] [blame] | 125 | int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm) |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 126 | { |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 127 | size_t size = pxa2xx_pcm_hardware.buffer_bytes_max; |
Takashi Iwai | 7f2da3d | 2021-08-02 09:28:05 +0200 | [diff] [blame] | 128 | |
| 129 | return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC, |
| 130 | pcm->card->dev, size); |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 131 | } |
| 132 | EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer); |
| 133 | |
Kuninori Morimoto | f8772e1 | 2019-10-02 14:33:50 +0900 | [diff] [blame] | 134 | int pxa2xx_soc_pcm_new(struct snd_soc_component *component, |
| 135 | struct snd_soc_pcm_runtime *rtd) |
Daniel Mack | 7afd1b0 | 2018-06-27 21:33:55 +0200 | [diff] [blame] | 136 | { |
| 137 | struct snd_card *card = rtd->card->snd_card; |
| 138 | struct snd_pcm *pcm = rtd->pcm; |
| 139 | int ret; |
| 140 | |
| 141 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
Takashi Iwai | 7f2da3d | 2021-08-02 09:28:05 +0200 | [diff] [blame] | 145 | return pxa2xx_pcm_preallocate_dma_buffer(pcm); |
Daniel Mack | 7afd1b0 | 2018-06-27 21:33:55 +0200 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL(pxa2xx_soc_pcm_new); |
| 148 | |
Kuninori Morimoto | f8772e1 | 2019-10-02 14:33:50 +0900 | [diff] [blame] | 149 | int pxa2xx_soc_pcm_open(struct snd_soc_component *component, |
| 150 | struct snd_pcm_substream *substream) |
| 151 | { |
| 152 | return pxa2xx_pcm_open(substream); |
| 153 | } |
| 154 | EXPORT_SYMBOL(pxa2xx_soc_pcm_open); |
| 155 | |
| 156 | int pxa2xx_soc_pcm_close(struct snd_soc_component *component, |
| 157 | struct snd_pcm_substream *substream) |
| 158 | { |
| 159 | return pxa2xx_pcm_close(substream); |
| 160 | } |
| 161 | EXPORT_SYMBOL(pxa2xx_soc_pcm_close); |
| 162 | |
| 163 | int pxa2xx_soc_pcm_hw_params(struct snd_soc_component *component, |
| 164 | struct snd_pcm_substream *substream, |
| 165 | struct snd_pcm_hw_params *params) |
| 166 | { |
| 167 | return pxa2xx_pcm_hw_params(substream, params); |
| 168 | } |
| 169 | EXPORT_SYMBOL(pxa2xx_soc_pcm_hw_params); |
| 170 | |
Kuninori Morimoto | f8772e1 | 2019-10-02 14:33:50 +0900 | [diff] [blame] | 171 | int pxa2xx_soc_pcm_prepare(struct snd_soc_component *component, |
| 172 | struct snd_pcm_substream *substream) |
| 173 | { |
| 174 | return pxa2xx_pcm_prepare(substream); |
| 175 | } |
| 176 | EXPORT_SYMBOL(pxa2xx_soc_pcm_prepare); |
| 177 | |
| 178 | int pxa2xx_soc_pcm_trigger(struct snd_soc_component *component, |
| 179 | struct snd_pcm_substream *substream, int cmd) |
| 180 | { |
| 181 | return pxa2xx_pcm_trigger(substream, cmd); |
| 182 | } |
| 183 | EXPORT_SYMBOL(pxa2xx_soc_pcm_trigger); |
| 184 | |
| 185 | snd_pcm_uframes_t |
| 186 | pxa2xx_soc_pcm_pointer(struct snd_soc_component *component, |
| 187 | struct snd_pcm_substream *substream) |
| 188 | { |
| 189 | return pxa2xx_pcm_pointer(substream); |
| 190 | } |
| 191 | EXPORT_SYMBOL(pxa2xx_soc_pcm_pointer); |
| 192 | |
Dmitry Baryshkov | a6d7731 | 2008-09-10 05:01:20 +0400 | [diff] [blame] | 193 | MODULE_AUTHOR("Nicolas Pitre"); |
| 194 | MODULE_DESCRIPTION("Intel PXA2xx sound library"); |
| 195 | MODULE_LICENSE("GPL"); |