blob: 18191084b8b8474cb4339f17968123bfec88654f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Ola Lilja01a0c112012-05-24 15:26:32 +02002/*
3 * Copyright (C) ST-Ericsson SA 2012
4 *
5 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
6 * Roger Nilsson <roger.xr.nilsson@stericsson.com>
7 * for ST-Ericsson.
8 *
9 * License terms:
Ola Lilja01a0c112012-05-24 15:26:32 +020010 */
11
12#include <asm/page.h>
13
14#include <linux/module.h>
15#include <linux/dma-mapping.h>
16#include <linux/dmaengine.h>
17#include <linux/slab.h>
Linus Walleij865fab62012-10-18 14:20:16 +020018#include <linux/platform_data/dma-ste-dma40.h>
Ola Lilja01a0c112012-05-24 15:26:32 +020019
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22#include <sound/soc.h>
23#include <sound/dmaengine_pcm.h>
24
25#include "ux500_msp_i2s.h"
26#include "ux500_pcm.h"
27
Lars-Peter Clausen69b6f192013-04-03 11:02:55 +020028#define UX500_PLATFORM_PERIODS_BYTES_MIN 128
29#define UX500_PLATFORM_PERIODS_BYTES_MAX (64 * PAGE_SIZE)
30#define UX500_PLATFORM_PERIODS_MIN 2
31#define UX500_PLATFORM_PERIODS_MAX 48
32#define UX500_PLATFORM_BUFFER_BYTES_MAX (2048 * PAGE_SIZE)
33
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +020034static const struct snd_pcm_hardware ux500_pcm_hw = {
Ola Lilja01a0c112012-05-24 15:26:32 +020035 .info = SNDRV_PCM_INFO_INTERLEAVED |
36 SNDRV_PCM_INFO_MMAP |
37 SNDRV_PCM_INFO_RESUME |
38 SNDRV_PCM_INFO_PAUSE,
Ola Lilja01a0c112012-05-24 15:26:32 +020039 .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
40 .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
41 .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
42 .periods_min = UX500_PLATFORM_PERIODS_MIN,
43 .periods_max = UX500_PLATFORM_PERIODS_MAX,
44};
45
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +020046static struct dma_chan *ux500_pcm_request_chan(struct snd_soc_pcm_runtime *rtd,
47 struct snd_pcm_substream *substream)
Ola Lilja01a0c112012-05-24 15:26:32 +020048{
Kuninori Morimoto99396e32020-03-23 14:21:35 +090049 struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
Ola Lilja01a0c112012-05-24 15:26:32 +020050 u16 per_data_width, mem_data_width;
51 struct stedma40_chan_cfg *dma_cfg;
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +020052 struct ux500_msp_dma_params *dma_params;
Ola Lilja01a0c112012-05-24 15:26:32 +020053
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +020054 dma_params = snd_soc_dai_get_dma_data(dai, substream);
55 dma_cfg = dma_params->dma_cfg;
Ola Lilja01a0c112012-05-24 15:26:32 +020056
Lee Jones43f2e1a2013-05-15 11:51:57 +020057 mem_data_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Ola Lilja01a0c112012-05-24 15:26:32 +020058
Ola Lilja01a0c112012-05-24 15:26:32 +020059 switch (dma_params->data_size) {
60 case 32:
Lee Jones43f2e1a2013-05-15 11:51:57 +020061 per_data_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Ola Lilja01a0c112012-05-24 15:26:32 +020062 break;
63 case 16:
Lee Jones43f2e1a2013-05-15 11:51:57 +020064 per_data_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Ola Lilja01a0c112012-05-24 15:26:32 +020065 break;
66 case 8:
Lee Jones43f2e1a2013-05-15 11:51:57 +020067 per_data_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
Ola Lilja01a0c112012-05-24 15:26:32 +020068 break;
69 default:
Lee Jones43f2e1a2013-05-15 11:51:57 +020070 per_data_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Ola Lilja01a0c112012-05-24 15:26:32 +020071 }
72
Ola Lilja01a0c112012-05-24 15:26:32 +020073 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
74 dma_cfg->src_info.data_width = mem_data_width;
75 dma_cfg->dst_info.data_width = per_data_width;
76 } else {
77 dma_cfg->src_info.data_width = per_data_width;
78 dma_cfg->dst_info.data_width = mem_data_width;
79 }
80
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +020081 return snd_dmaengine_pcm_request_channel(stedma40_filter, dma_cfg);
Ola Lilja01a0c112012-05-24 15:26:32 +020082}
83
Fabio Baltierieef64732013-06-12 09:57:59 +020084static int ux500_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *params,
86 struct dma_slave_config *slave_config)
87{
Kuninori Morimoto3e44c4792020-07-20 10:17:44 +090088 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
Kuninori Morimoto99396e32020-03-23 14:21:35 +090089 struct msp_i2s_platform_data *pdata = asoc_rtd_to_cpu(rtd, 0)->dev->platform_data;
Lee Jonesf6c37752013-12-19 15:55:03 +000090 struct snd_dmaengine_dai_dma_data *snd_dma_params;
91 struct ux500_msp_dma_params *ste_dma_params;
92 dma_addr_t dma_addr;
Fabio Baltierieef64732013-06-12 09:57:59 +020093 int ret;
94
Lee Jonesf6c37752013-12-19 15:55:03 +000095 if (pdata) {
96 ste_dma_params =
Kuninori Morimoto99396e32020-03-23 14:21:35 +090097 snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
Lee Jonesf6c37752013-12-19 15:55:03 +000098 dma_addr = ste_dma_params->tx_rx_addr;
99 } else {
100 snd_dma_params =
Kuninori Morimoto99396e32020-03-23 14:21:35 +0900101 snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
Lee Jonesf6c37752013-12-19 15:55:03 +0000102 dma_addr = snd_dma_params->addr;
103 }
Fabio Baltierieef64732013-06-12 09:57:59 +0200104
105 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
106 if (ret)
107 return ret;
108
109 slave_config->dst_maxburst = 4;
Fabio Baltierieef64732013-06-12 09:57:59 +0200110 slave_config->src_maxburst = 4;
Lee Jones609a3052013-12-19 15:55:02 +0000111
112 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
113 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
Fabio Baltierieef64732013-06-12 09:57:59 +0200114
115 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Lee Jonesf6c37752013-12-19 15:55:03 +0000116 slave_config->dst_addr = dma_addr;
Fabio Baltierieef64732013-06-12 09:57:59 +0200117 else
Lee Jonesf6c37752013-12-19 15:55:03 +0000118 slave_config->src_addr = dma_addr;
Fabio Baltierieef64732013-06-12 09:57:59 +0200119
120 return 0;
121}
122
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +0200123static const struct snd_dmaengine_pcm_config ux500_dmaengine_pcm_config = {
124 .pcm_hardware = &ux500_pcm_hw,
125 .compat_request_channel = ux500_pcm_request_chan,
126 .prealloc_buffer_size = 128 * 1024,
Fabio Baltierieef64732013-06-12 09:57:59 +0200127 .prepare_slave_config = ux500_pcm_prepare_slave_config,
Ola Lilja01a0c112012-05-24 15:26:32 +0200128};
129
Lee Jones86a3fdf2013-12-19 15:55:07 +0000130static const struct snd_dmaengine_pcm_config ux500_dmaengine_of_pcm_config = {
131 .compat_request_channel = ux500_pcm_request_chan,
132 .prepare_slave_config = ux500_pcm_prepare_slave_config,
133};
134
Bill Pembertonda794872012-12-07 09:26:35 -0500135int ux500_pcm_register_platform(struct platform_device *pdev)
Ola Lilja01a0c112012-05-24 15:26:32 +0200136{
Lee Jones86a3fdf2013-12-19 15:55:07 +0000137 const struct snd_dmaengine_pcm_config *pcm_config;
138 struct device_node *np = pdev->dev.of_node;
Ola Lilja01a0c112012-05-24 15:26:32 +0200139 int ret;
140
Lee Jones86a3fdf2013-12-19 15:55:07 +0000141 if (np)
142 pcm_config = &ux500_dmaengine_of_pcm_config;
143 else
144 pcm_config = &ux500_dmaengine_pcm_config;
145
146 ret = snd_dmaengine_pcm_register(&pdev->dev, pcm_config,
Lee Jones86a3fdf2013-12-19 15:55:07 +0000147 SND_DMAENGINE_PCM_FLAG_COMPAT);
Ola Lilja01a0c112012-05-24 15:26:32 +0200148 if (ret < 0) {
149 dev_err(&pdev->dev,
150 "%s: ERROR: Failed to register platform '%s' (%d)!\n",
151 __func__, pdev->name, ret);
152 return ret;
153 }
154
155 return 0;
156}
Lee Jones1428c202012-11-23 13:05:41 +0000157EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
Ola Lilja01a0c112012-05-24 15:26:32 +0200158
Bill Pembertonda794872012-12-07 09:26:35 -0500159int ux500_pcm_unregister_platform(struct platform_device *pdev)
Ola Lilja01a0c112012-05-24 15:26:32 +0200160{
Lars-Peter Clausen22f38f72013-04-15 19:20:04 +0200161 snd_dmaengine_pcm_unregister(&pdev->dev);
Ola Lilja01a0c112012-05-24 15:26:32 +0200162 return 0;
163}
Lee Jones1428c202012-11-23 13:05:41 +0000164EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);
Arnd Bergmann1783c9d2018-01-10 17:34:45 +0100165
166MODULE_AUTHOR("Ola Lilja");
167MODULE_AUTHOR("Roger Nilsson");
168MODULE_DESCRIPTION("ASoC UX500 driver");
169MODULE_LICENSE("GPL v2");