Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 2 | /* |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 3 | * sst_mfld_platform.c - Intel MID Platform driver |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 4 | * |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 5 | * Copyright (C) 2010-2014 Intel Corp |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 6 | * Author: Vinod Koul <vinod.koul@intel.com> |
| 7 | * Author: Harsha Priya <priya.harsha@intel.com> |
| 8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | * |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 11 | */ |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/io.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 17 | #include <sound/core.h> |
| 18 | #include <sound/pcm.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include <sound/soc.h> |
Vinod Koul | c514a91 | 2012-08-16 17:10:42 +0530 | [diff] [blame] | 21 | #include <sound/compress_driver.h> |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 22 | #include <asm/platform_sst_audio.h> |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 23 | #include "sst-mfld-platform.h" |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 24 | #include "sst-atom-controls.h" |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 25 | |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 26 | struct sst_device *sst; |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 27 | static DEFINE_MUTEX(sst_lock); |
| 28 | |
| 29 | int sst_register_dsp(struct sst_device *dev) |
| 30 | { |
Takashi Iwai | 656a22a | 2013-11-05 18:40:01 +0100 | [diff] [blame] | 31 | if (WARN_ON(!dev)) |
| 32 | return -EINVAL; |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 33 | if (!try_module_get(dev->dev->driver->owner)) |
| 34 | return -ENODEV; |
| 35 | mutex_lock(&sst_lock); |
| 36 | if (sst) { |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 37 | dev_err(dev->dev, "we already have a device %s\n", sst->name); |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 38 | module_put(dev->dev->driver->owner); |
| 39 | mutex_unlock(&sst_lock); |
| 40 | return -EEXIST; |
| 41 | } |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 42 | dev_dbg(dev->dev, "registering device %s\n", dev->name); |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 43 | sst = dev; |
| 44 | mutex_unlock(&sst_lock); |
| 45 | return 0; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(sst_register_dsp); |
| 48 | |
| 49 | int sst_unregister_dsp(struct sst_device *dev) |
| 50 | { |
Takashi Iwai | 656a22a | 2013-11-05 18:40:01 +0100 | [diff] [blame] | 51 | if (WARN_ON(!dev)) |
| 52 | return -EINVAL; |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 53 | if (dev != sst) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | mutex_lock(&sst_lock); |
| 57 | |
| 58 | if (!sst) { |
| 59 | mutex_unlock(&sst_lock); |
| 60 | return -EIO; |
| 61 | } |
| 62 | |
| 63 | module_put(sst->dev->driver->owner); |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 64 | dev_dbg(dev->dev, "unreg %s\n", sst->name); |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 65 | sst = NULL; |
| 66 | mutex_unlock(&sst_lock); |
| 67 | return 0; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(sst_unregister_dsp); |
| 70 | |
Bhumika Goyal | a7468e4 | 2017-08-17 15:46:08 +0530 | [diff] [blame] | 71 | static const struct snd_pcm_hardware sst_platform_pcm_hw = { |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 72 | .info = (SNDRV_PCM_INFO_INTERLEAVED | |
| 73 | SNDRV_PCM_INFO_DOUBLE | |
| 74 | SNDRV_PCM_INFO_PAUSE | |
| 75 | SNDRV_PCM_INFO_RESUME | |
| 76 | SNDRV_PCM_INFO_MMAP| |
| 77 | SNDRV_PCM_INFO_MMAP_VALID | |
| 78 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 79 | SNDRV_PCM_INFO_SYNC_START), |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 80 | .buffer_bytes_max = SST_MAX_BUFFER, |
| 81 | .period_bytes_min = SST_MIN_PERIOD_BYTES, |
| 82 | .period_bytes_max = SST_MAX_PERIOD_BYTES, |
| 83 | .periods_min = SST_MIN_PERIODS, |
| 84 | .periods_max = SST_MAX_PERIODS, |
| 85 | .fifo_size = SST_FIFO_SIZE, |
| 86 | }; |
| 87 | |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 88 | static struct sst_dev_stream_map dpcm_strm_map[] = { |
| 89 | {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, /* Reserved, not in use */ |
| 90 | {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA1_IN, SST_TASK_ID_MEDIA, 0}, |
| 91 | {MERR_DPCM_COMPR, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA0_IN, SST_TASK_ID_MEDIA, 0}, |
| 92 | {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_CAPTURE, PIPE_PCM1_OUT, SST_TASK_ID_MEDIA, 0}, |
Pierre-Louis Bossart | 8788f83 | 2015-12-17 20:35:44 -0600 | [diff] [blame] | 93 | {MERR_DPCM_DEEP_BUFFER, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA3_IN, SST_TASK_ID_MEDIA, 0}, |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 94 | }; |
| 95 | |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 96 | static int sst_media_digital_mute(struct snd_soc_dai *dai, int mute, int stream) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 97 | { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 98 | |
| 99 | return sst_send_pipe_gains(dai, stream, mute); |
| 100 | } |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 101 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 102 | /* helper functions */ |
Vinod Koul | 4496ffa | 2014-05-05 14:27:49 +0530 | [diff] [blame] | 103 | void sst_set_stream_status(struct sst_runtime_stream *stream, |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 104 | int state) |
| 105 | { |
Lu Guanqun | d89b0a1 | 2011-04-08 15:38:48 +0800 | [diff] [blame] | 106 | unsigned long flags; |
| 107 | spin_lock_irqsave(&stream->status_lock, flags); |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 108 | stream->stream_status = state; |
Lu Guanqun | d89b0a1 | 2011-04-08 15:38:48 +0800 | [diff] [blame] | 109 | spin_unlock_irqrestore(&stream->status_lock, flags); |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline int sst_get_stream_status(struct sst_runtime_stream *stream) |
| 113 | { |
| 114 | int state; |
Lu Guanqun | d89b0a1 | 2011-04-08 15:38:48 +0800 | [diff] [blame] | 115 | unsigned long flags; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 116 | |
Lu Guanqun | d89b0a1 | 2011-04-08 15:38:48 +0800 | [diff] [blame] | 117 | spin_lock_irqsave(&stream->status_lock, flags); |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 118 | state = stream->stream_status; |
Lu Guanqun | d89b0a1 | 2011-04-08 15:38:48 +0800 | [diff] [blame] | 119 | spin_unlock_irqrestore(&stream->status_lock, flags); |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 120 | return state; |
| 121 | } |
| 122 | |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 123 | static void sst_fill_alloc_params(struct snd_pcm_substream *substream, |
| 124 | struct snd_sst_alloc_params_ext *alloc_param) |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 125 | { |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 126 | unsigned int channels; |
| 127 | snd_pcm_uframes_t period_size; |
| 128 | ssize_t periodbytes; |
| 129 | ssize_t buffer_bytes = snd_pcm_lib_buffer_bytes(substream); |
Takashi Iwai | e28ac04 | 2021-08-22 09:21:27 +0200 | [diff] [blame] | 130 | u32 buffer_addr = substream->runtime->dma_addr; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 131 | |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 132 | channels = substream->runtime->channels; |
| 133 | period_size = substream->runtime->period_size; |
| 134 | periodbytes = samples_to_bytes(substream->runtime, period_size); |
| 135 | alloc_param->ring_buf_info[0].addr = buffer_addr; |
| 136 | alloc_param->ring_buf_info[0].size = buffer_bytes; |
| 137 | alloc_param->sg_count = 1; |
| 138 | alloc_param->reserved = 0; |
| 139 | alloc_param->frag_size = periodbytes * channels; |
| 140 | |
| 141 | } |
| 142 | static void sst_fill_pcm_params(struct snd_pcm_substream *substream, |
| 143 | struct snd_sst_stream_params *param) |
| 144 | { |
| 145 | param->uc.pcm_params.num_chan = (u8) substream->runtime->channels; |
| 146 | param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits; |
| 147 | param->uc.pcm_params.sfreq = substream->runtime->rate; |
| 148 | |
| 149 | /* PCM stream via ALSA interface */ |
| 150 | param->uc.pcm_params.use_offload_path = 0; |
| 151 | param->uc.pcm_params.reserved2 = 0; |
| 152 | memset(param->uc.pcm_params.channel_map, 0, sizeof(u8)); |
| 153 | |
| 154 | } |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 155 | |
| 156 | static int sst_get_stream_mapping(int dev, int sdev, int dir, |
| 157 | struct sst_dev_stream_map *map, int size) |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 158 | { |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 159 | int i; |
| 160 | |
| 161 | if (map == NULL) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | |
| 165 | /* index 0 is not used in stream map */ |
| 166 | for (i = 1; i < size; i++) { |
| 167 | if ((map[i].dev_num == dev) && (map[i].direction == dir)) |
| 168 | return i; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int sst_fill_stream_params(void *substream, |
| 174 | const struct sst_data *ctx, struct snd_sst_params *str_params, bool is_compress) |
| 175 | { |
| 176 | int map_size; |
| 177 | int index; |
| 178 | struct sst_dev_stream_map *map; |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 179 | struct snd_pcm_substream *pstream = NULL; |
| 180 | struct snd_compr_stream *cstream = NULL; |
| 181 | |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 182 | map = ctx->pdata->pdev_strm_map; |
| 183 | map_size = ctx->pdata->strm_map_size; |
| 184 | |
Pierre-Louis Bossart | 10583cd | 2018-12-16 16:49:08 -0600 | [diff] [blame] | 185 | if (is_compress) |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 186 | cstream = (struct snd_compr_stream *)substream; |
| 187 | else |
| 188 | pstream = (struct snd_pcm_substream *)substream; |
| 189 | |
| 190 | str_params->stream_type = SST_STREAM_TYPE_MUSIC; |
| 191 | |
| 192 | /* For pcm streams */ |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 193 | if (pstream) { |
| 194 | index = sst_get_stream_mapping(pstream->pcm->device, |
| 195 | pstream->number, pstream->stream, |
| 196 | map, map_size); |
| 197 | if (index <= 0) |
| 198 | return -EINVAL; |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 199 | |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 200 | str_params->stream_id = index; |
| 201 | str_params->device_type = map[index].device_id; |
| 202 | str_params->task = map[index].task_id; |
| 203 | |
| 204 | str_params->ops = (u8)pstream->stream; |
| 205 | } |
| 206 | |
| 207 | if (cstream) { |
| 208 | index = sst_get_stream_mapping(cstream->device->device, |
| 209 | 0, cstream->direction, |
| 210 | map, map_size); |
| 211 | if (index <= 0) |
| 212 | return -EINVAL; |
| 213 | str_params->stream_id = index; |
| 214 | str_params->device_type = map[index].device_id; |
| 215 | str_params->task = map[index].task_id; |
| 216 | |
| 217 | str_params->ops = (u8)cstream->direction; |
| 218 | } |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 219 | return 0; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 220 | } |
| 221 | |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 222 | static int sst_platform_alloc_stream(struct snd_pcm_substream *substream, |
Subhransu S. Prusty | 6df5d76 | 2014-09-09 15:11:32 +0530 | [diff] [blame] | 223 | struct snd_soc_dai *dai) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 224 | { |
| 225 | struct sst_runtime_stream *stream = |
| 226 | substream->runtime->private_data; |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 227 | struct snd_sst_stream_params param = {{{0,},},}; |
| 228 | struct snd_sst_params str_params = {0}; |
| 229 | struct snd_sst_alloc_params_ext alloc_params = {0}; |
| 230 | int ret_val = 0; |
Subhransu S. Prusty | 6df5d76 | 2014-09-09 15:11:32 +0530 | [diff] [blame] | 231 | struct sst_data *ctx = snd_soc_dai_get_drvdata(dai); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 232 | |
| 233 | /* set codec params and inform SST driver the same */ |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 234 | sst_fill_pcm_params(substream, ¶m); |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 235 | sst_fill_alloc_params(substream, &alloc_params); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 236 | str_params.sparams = param; |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 237 | str_params.aparams = alloc_params; |
| 238 | str_params.codec = SST_CODEC_TYPE_PCM; |
| 239 | |
| 240 | /* fill the device type and stream id to pass to SST driver */ |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 241 | ret_val = sst_fill_stream_params(substream, ctx, &str_params, false); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 242 | if (ret_val < 0) |
| 243 | return ret_val; |
| 244 | |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 245 | stream->stream_info.str_id = str_params.stream_id; |
| 246 | |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 247 | ret_val = stream->ops->open(sst->dev, &str_params); |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 248 | if (ret_val <= 0) |
| 249 | return ret_val; |
| 250 | |
| 251 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 252 | return ret_val; |
| 253 | } |
| 254 | |
Vinod Koul | 9daa5bd | 2014-06-13 18:03:52 +0530 | [diff] [blame] | 255 | static void sst_period_elapsed(void *arg) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 256 | { |
Vinod Koul | 9daa5bd | 2014-06-13 18:03:52 +0530 | [diff] [blame] | 257 | struct snd_pcm_substream *substream = arg; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 258 | struct sst_runtime_stream *stream; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 259 | int status; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 260 | |
| 261 | if (!substream || !substream->runtime) |
| 262 | return; |
| 263 | stream = substream->runtime->private_data; |
| 264 | if (!stream) |
| 265 | return; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 266 | status = sst_get_stream_status(stream); |
| 267 | if (status != SST_PLATFORM_RUNNING) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 268 | return; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 269 | snd_pcm_period_elapsed(substream); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int sst_platform_init_stream(struct snd_pcm_substream *substream) |
| 273 | { |
| 274 | struct sst_runtime_stream *stream = |
| 275 | substream->runtime->private_data; |
Kuninori Morimoto | 2ab9a40 | 2020-07-27 10:08:42 +0900 | [diff] [blame] | 276 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 277 | int ret_val; |
| 278 | |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 279 | dev_dbg(rtd->dev, "setting buffer ptr param\n"); |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 280 | sst_set_stream_status(stream, SST_PLATFORM_INIT); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 281 | stream->stream_info.period_elapsed = sst_period_elapsed; |
Vinod Koul | 9daa5bd | 2014-06-13 18:03:52 +0530 | [diff] [blame] | 282 | stream->stream_info.arg = substream; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 283 | stream->stream_info.buffer_ptr = 0; |
| 284 | stream->stream_info.sfreq = substream->runtime->rate; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 285 | ret_val = stream->ops->stream_init(sst->dev, &stream->stream_info); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 286 | if (ret_val) |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 287 | dev_err(rtd->dev, "control_set ret error %d\n", ret_val); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 288 | return ret_val; |
| 289 | |
| 290 | } |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 291 | |
Vinod Koul | 0121327 | 2014-09-19 16:46:03 +0530 | [diff] [blame] | 292 | static int power_up_sst(struct sst_runtime_stream *stream) |
| 293 | { |
| 294 | return stream->ops->power(sst->dev, true); |
| 295 | } |
| 296 | |
| 297 | static void power_down_sst(struct sst_runtime_stream *stream) |
| 298 | { |
| 299 | stream->ops->power(sst->dev, false); |
| 300 | } |
| 301 | |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 302 | static int sst_media_open(struct snd_pcm_substream *substream, |
| 303 | struct snd_soc_dai *dai) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 304 | { |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 305 | int ret_val = 0; |
Lu Guanqun | 22be504 | 2011-09-06 15:21:38 +0800 | [diff] [blame] | 306 | struct snd_pcm_runtime *runtime = substream->runtime; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 307 | struct sst_runtime_stream *stream; |
Lu Guanqun | 22be504 | 2011-09-06 15:21:38 +0800 | [diff] [blame] | 308 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 309 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 310 | if (!stream) |
| 311 | return -ENOMEM; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 312 | spin_lock_init(&stream->status_lock); |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 313 | |
| 314 | /* get the sst ops */ |
| 315 | mutex_lock(&sst_lock); |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 316 | if (!sst || |
| 317 | !try_module_get(sst->dev->driver->owner)) { |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 318 | dev_err(dai->dev, "no device available to run\n"); |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 319 | ret_val = -ENODEV; |
| 320 | goto out_ops; |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 321 | } |
| 322 | stream->ops = sst->ops; |
| 323 | mutex_unlock(&sst_lock); |
| 324 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 325 | stream->stream_info.str_id = 0; |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 326 | |
Vinod Koul | 9daa5bd | 2014-06-13 18:03:52 +0530 | [diff] [blame] | 327 | stream->stream_info.arg = substream; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 328 | /* allocate memory for SST API set */ |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 329 | runtime->private_data = stream; |
Lu Guanqun | 283e42e | 2011-09-06 15:21:43 +0800 | [diff] [blame] | 330 | |
Vinod Koul | 0121327 | 2014-09-19 16:46:03 +0530 | [diff] [blame] | 331 | ret_val = power_up_sst(stream); |
| 332 | if (ret_val < 0) |
Dinghao Liu | 062fa09 | 2020-08-13 16:41:10 +0800 | [diff] [blame] | 333 | goto out_power_up; |
Vinod Koul | 0121327 | 2014-09-19 16:46:03 +0530 | [diff] [blame] | 334 | |
Brent Lu | 5e7820e | 2020-07-31 20:26:04 +0800 | [diff] [blame] | 335 | /* |
| 336 | * Make sure the period to be multiple of 1ms to align the |
| 337 | * design of firmware. Apply same rule to buffer size to make |
| 338 | * sure alsa could always find a value for period size |
| 339 | * regardless the buffer size given by user space. |
| 340 | */ |
| 341 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 342 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 48); |
| 343 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 344 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 48); |
| 345 | |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 346 | /* Make sure, that the period size is always even */ |
| 347 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 348 | SNDRV_PCM_HW_PARAM_PERIODS, 2); |
| 349 | |
| 350 | return snd_pcm_hw_constraint_integer(runtime, |
| 351 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 352 | out_ops: |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 353 | mutex_unlock(&sst_lock); |
Dinghao Liu | 062fa09 | 2020-08-13 16:41:10 +0800 | [diff] [blame] | 354 | out_power_up: |
| 355 | kfree(stream); |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 356 | return ret_val; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 357 | } |
| 358 | |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 359 | static void sst_media_close(struct snd_pcm_substream *substream, |
| 360 | struct snd_soc_dai *dai) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 361 | { |
| 362 | struct sst_runtime_stream *stream; |
Vinod Koul | 7d7c80f | 2016-12-08 23:01:35 +0530 | [diff] [blame] | 363 | int str_id; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 364 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 365 | stream = substream->runtime->private_data; |
Vinod Koul | 0121327 | 2014-09-19 16:46:03 +0530 | [diff] [blame] | 366 | power_down_sst(stream); |
| 367 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 368 | str_id = stream->stream_info.str_id; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 369 | if (str_id) |
Vinod Koul | 7d7c80f | 2016-12-08 23:01:35 +0530 | [diff] [blame] | 370 | stream->ops->close(sst->dev, str_id); |
Vinod Koul | 03c3304 | 2011-12-05 19:13:41 +0530 | [diff] [blame] | 371 | module_put(sst->dev->driver->owner); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 372 | kfree(stream); |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 373 | } |
| 374 | |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 375 | static int sst_media_prepare(struct snd_pcm_substream *substream, |
| 376 | struct snd_soc_dai *dai) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 377 | { |
| 378 | struct sst_runtime_stream *stream; |
Pierre-Louis Bossart | b0754c5 | 2020-08-13 15:01:29 -0500 | [diff] [blame] | 379 | int ret_val, str_id; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 380 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 381 | stream = substream->runtime->private_data; |
| 382 | str_id = stream->stream_info.str_id; |
| 383 | if (stream->stream_info.str_id) { |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 384 | ret_val = stream->ops->stream_drop(sst->dev, str_id); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 385 | return ret_val; |
| 386 | } |
| 387 | |
Subhransu S. Prusty | 6df5d76 | 2014-09-09 15:11:32 +0530 | [diff] [blame] | 388 | ret_val = sst_platform_alloc_stream(substream, dai); |
Vinod Koul | a870cdce | 2014-06-13 18:03:55 +0530 | [diff] [blame] | 389 | if (ret_val <= 0) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 390 | return ret_val; |
| 391 | snprintf(substream->pcm->id, sizeof(substream->pcm->id), |
| 392 | "%d", stream->stream_info.str_id); |
| 393 | |
| 394 | ret_val = sst_platform_init_stream(substream); |
| 395 | if (ret_val) |
| 396 | return ret_val; |
| 397 | substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER; |
Pierre-Louis Bossart | 5ab56a2 | 2020-08-13 15:01:33 -0500 | [diff] [blame] | 398 | return 0; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 399 | } |
| 400 | |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 401 | static int sst_enable_ssp(struct snd_pcm_substream *substream, |
| 402 | struct snd_soc_dai *dai) |
| 403 | { |
| 404 | int ret = 0; |
| 405 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 406 | if (!snd_soc_dai_active(dai)) { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 407 | ret = sst_handle_vb_timer(dai, true); |
Vinod Koul | 711bc94 | 2015-05-06 22:06:42 +0530 | [diff] [blame] | 408 | sst_fill_ssp_defaults(dai); |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 409 | } |
| 410 | return ret; |
| 411 | } |
| 412 | |
Vinod Koul | 711bc94 | 2015-05-06 22:06:42 +0530 | [diff] [blame] | 413 | static int sst_be_hw_params(struct snd_pcm_substream *substream, |
| 414 | struct snd_pcm_hw_params *params, |
| 415 | struct snd_soc_dai *dai) |
| 416 | { |
| 417 | int ret = 0; |
| 418 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 419 | if (snd_soc_dai_active(dai) == 1) |
Vinod Koul | 711bc94 | 2015-05-06 22:06:42 +0530 | [diff] [blame] | 420 | ret = send_ssp_cmd(dai, dai->name, 1); |
| 421 | return ret; |
| 422 | } |
| 423 | |
Vinod Koul | 0b44e34 | 2015-05-06 22:06:43 +0530 | [diff] [blame] | 424 | static int sst_set_format(struct snd_soc_dai *dai, unsigned int fmt) |
| 425 | { |
| 426 | int ret = 0; |
| 427 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 428 | if (!snd_soc_dai_active(dai)) |
Vinod Koul | 0b44e34 | 2015-05-06 22:06:43 +0530 | [diff] [blame] | 429 | return 0; |
| 430 | |
| 431 | ret = sst_fill_ssp_config(dai, fmt); |
| 432 | if (ret < 0) |
| 433 | dev_err(dai->dev, "sst_set_format failed..\n"); |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
Vinod Koul | 83f125e | 2015-05-06 22:06:44 +0530 | [diff] [blame] | 438 | static int sst_platform_set_ssp_slot(struct snd_soc_dai *dai, |
| 439 | unsigned int tx_mask, unsigned int rx_mask, |
| 440 | int slots, int slot_width) { |
| 441 | int ret = 0; |
| 442 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 443 | if (!snd_soc_dai_active(dai)) |
Vinod Koul | 83f125e | 2015-05-06 22:06:44 +0530 | [diff] [blame] | 444 | return ret; |
| 445 | |
| 446 | ret = sst_fill_ssp_slot(dai, tx_mask, rx_mask, slots, slot_width); |
| 447 | if (ret < 0) |
| 448 | dev_err(dai->dev, "sst_fill_ssp_slot failed..%d\n", ret); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 453 | static void sst_disable_ssp(struct snd_pcm_substream *substream, |
| 454 | struct snd_soc_dai *dai) |
| 455 | { |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 456 | if (!snd_soc_dai_active(dai)) { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 457 | send_ssp_cmd(dai, dai->name, 0); |
| 458 | sst_handle_vb_timer(dai, false); |
| 459 | } |
| 460 | } |
| 461 | |
Gustavo A. R. Silva | c115a31 | 2017-07-13 01:26:06 -0500 | [diff] [blame] | 462 | static const struct snd_soc_dai_ops sst_media_dai_ops = { |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 463 | .startup = sst_media_open, |
| 464 | .shutdown = sst_media_close, |
| 465 | .prepare = sst_media_prepare, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 466 | .mute_stream = sst_media_digital_mute, |
| 467 | }; |
| 468 | |
Gustavo A. R. Silva | c115a31 | 2017-07-13 01:26:06 -0500 | [diff] [blame] | 469 | static const struct snd_soc_dai_ops sst_compr_dai_ops = { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 470 | .mute_stream = sst_media_digital_mute, |
| 471 | }; |
| 472 | |
Gustavo A. R. Silva | c115a31 | 2017-07-13 01:26:06 -0500 | [diff] [blame] | 473 | static const struct snd_soc_dai_ops sst_be_dai_ops = { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 474 | .startup = sst_enable_ssp, |
Vinod Koul | 711bc94 | 2015-05-06 22:06:42 +0530 | [diff] [blame] | 475 | .hw_params = sst_be_hw_params, |
Vinod Koul | 0b44e34 | 2015-05-06 22:06:43 +0530 | [diff] [blame] | 476 | .set_fmt = sst_set_format, |
Vinod Koul | 83f125e | 2015-05-06 22:06:44 +0530 | [diff] [blame] | 477 | .set_tdm_slot = sst_platform_set_ssp_slot, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 478 | .shutdown = sst_disable_ssp, |
| 479 | }; |
| 480 | |
| 481 | static struct snd_soc_dai_driver sst_platform_dai[] = { |
| 482 | { |
| 483 | .name = "media-cpu-dai", |
| 484 | .ops = &sst_media_dai_ops, |
| 485 | .playback = { |
| 486 | .stream_name = "Headset Playback", |
| 487 | .channels_min = SST_STEREO, |
| 488 | .channels_max = SST_STEREO, |
Hans de Goede | 632aeeb | 2021-03-24 14:27:11 +0100 | [diff] [blame] | 489 | .rates = SNDRV_PCM_RATE_48000, |
Hans de Goede | aa65bac | 2021-03-24 14:27:10 +0100 | [diff] [blame] | 490 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 491 | }, |
| 492 | .capture = { |
| 493 | .stream_name = "Headset Capture", |
| 494 | .channels_min = 1, |
| 495 | .channels_max = 2, |
Hans de Goede | 632aeeb | 2021-03-24 14:27:11 +0100 | [diff] [blame] | 496 | .rates = SNDRV_PCM_RATE_48000, |
Hans de Goede | aa65bac | 2021-03-24 14:27:10 +0100 | [diff] [blame] | 497 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 498 | }, |
| 499 | }, |
| 500 | { |
Pierre-Louis Bossart | 8788f83 | 2015-12-17 20:35:44 -0600 | [diff] [blame] | 501 | .name = "deepbuffer-cpu-dai", |
| 502 | .ops = &sst_media_dai_ops, |
| 503 | .playback = { |
| 504 | .stream_name = "Deepbuffer Playback", |
| 505 | .channels_min = SST_STEREO, |
| 506 | .channels_max = SST_STEREO, |
Hans de Goede | 632aeeb | 2021-03-24 14:27:11 +0100 | [diff] [blame] | 507 | .rates = SNDRV_PCM_RATE_48000, |
Hans de Goede | aa65bac | 2021-03-24 14:27:10 +0100 | [diff] [blame] | 508 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
Pierre-Louis Bossart | 8788f83 | 2015-12-17 20:35:44 -0600 | [diff] [blame] | 509 | }, |
| 510 | }, |
| 511 | { |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 512 | .name = "compress-cpu-dai", |
Jie Yang | 6f0c422 | 2015-10-13 23:41:00 +0800 | [diff] [blame] | 513 | .compress_new = snd_soc_new_compress, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 514 | .ops = &sst_compr_dai_ops, |
| 515 | .playback = { |
| 516 | .stream_name = "Compress Playback", |
Pierre-Louis Bossart | 292d420 | 2016-02-02 12:49:49 -0600 | [diff] [blame] | 517 | .channels_min = 1, |
Vinod Koul | c82351d | 2014-10-15 20:12:59 +0530 | [diff] [blame] | 518 | }, |
| 519 | }, |
| 520 | /* BE CPU Dais */ |
| 521 | { |
| 522 | .name = "ssp0-port", |
| 523 | .ops = &sst_be_dai_ops, |
| 524 | .playback = { |
| 525 | .stream_name = "ssp0 Tx", |
| 526 | .channels_min = SST_STEREO, |
| 527 | .channels_max = SST_STEREO, |
| 528 | .rates = SNDRV_PCM_RATE_48000, |
| 529 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 530 | }, |
| 531 | .capture = { |
| 532 | .stream_name = "ssp0 Rx", |
| 533 | .channels_min = SST_STEREO, |
| 534 | .channels_max = SST_STEREO, |
| 535 | .rates = SNDRV_PCM_RATE_48000, |
| 536 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 537 | }, |
| 538 | }, |
| 539 | { |
| 540 | .name = "ssp1-port", |
| 541 | .ops = &sst_be_dai_ops, |
| 542 | .playback = { |
| 543 | .stream_name = "ssp1 Tx", |
| 544 | .channels_min = SST_STEREO, |
| 545 | .channels_max = SST_STEREO, |
| 546 | .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000, |
| 547 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 548 | }, |
| 549 | .capture = { |
| 550 | .stream_name = "ssp1 Rx", |
| 551 | .channels_min = SST_STEREO, |
| 552 | .channels_max = SST_STEREO, |
| 553 | .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000, |
| 554 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 555 | }, |
| 556 | }, |
| 557 | { |
| 558 | .name = "ssp2-port", |
| 559 | .ops = &sst_be_dai_ops, |
| 560 | .playback = { |
| 561 | .stream_name = "ssp2 Tx", |
| 562 | .channels_min = SST_STEREO, |
| 563 | .channels_max = SST_STEREO, |
| 564 | .rates = SNDRV_PCM_RATE_48000, |
| 565 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 566 | }, |
| 567 | .capture = { |
| 568 | .stream_name = "ssp2 Rx", |
| 569 | .channels_min = SST_STEREO, |
| 570 | .channels_max = SST_STEREO, |
| 571 | .rates = SNDRV_PCM_RATE_48000, |
| 572 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 573 | }, |
| 574 | }, |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 575 | }; |
| 576 | |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 577 | static int sst_soc_open(struct snd_soc_component *component, |
| 578 | struct snd_pcm_substream *substream) |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 579 | { |
| 580 | struct snd_pcm_runtime *runtime; |
| 581 | |
| 582 | if (substream->pcm->internal) |
| 583 | return 0; |
| 584 | |
| 585 | runtime = substream->runtime; |
| 586 | runtime->hw = sst_platform_pcm_hw; |
| 587 | return 0; |
| 588 | } |
| 589 | |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 590 | static int sst_soc_trigger(struct snd_soc_component *component, |
| 591 | struct snd_pcm_substream *substream, int cmd) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 592 | { |
| 593 | int ret_val = 0, str_id; |
| 594 | struct sst_runtime_stream *stream; |
Subhransu S. Prusty | 5981c2d | 2014-08-04 15:04:20 +0530 | [diff] [blame] | 595 | int status; |
Kuninori Morimoto | 2ab9a40 | 2020-07-27 10:08:42 +0900 | [diff] [blame] | 596 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 597 | |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 598 | dev_dbg(rtd->dev, "%s called\n", __func__); |
Vinod Koul | d2b16b8 | 2014-09-09 15:11:24 +0530 | [diff] [blame] | 599 | if (substream->pcm->internal) |
| 600 | return 0; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 601 | stream = substream->runtime->private_data; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 602 | str_id = stream->stream_info.str_id; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 603 | switch (cmd) { |
| 604 | case SNDRV_PCM_TRIGGER_START: |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 605 | dev_dbg(rtd->dev, "sst: Trigger Start\n"); |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 606 | status = SST_PLATFORM_RUNNING; |
Vinod Koul | 9daa5bd | 2014-06-13 18:03:52 +0530 | [diff] [blame] | 607 | stream->stream_info.arg = substream; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 608 | ret_val = stream->ops->stream_start(sst->dev, str_id); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 609 | break; |
| 610 | case SNDRV_PCM_TRIGGER_STOP: |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 611 | dev_dbg(rtd->dev, "sst: in stop\n"); |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 612 | status = SST_PLATFORM_DROPPED; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 613 | ret_val = stream->ops->stream_drop(sst->dev, str_id); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 614 | break; |
| 615 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Vinod Koul | fc9406a | 2015-02-12 09:59:57 +0530 | [diff] [blame] | 616 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 617 | dev_dbg(rtd->dev, "sst: in pause\n"); |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 618 | status = SST_PLATFORM_PAUSED; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 619 | ret_val = stream->ops->stream_pause(sst->dev, str_id); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 620 | break; |
| 621 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Vinod Koul | fc9406a | 2015-02-12 09:59:57 +0530 | [diff] [blame] | 622 | case SNDRV_PCM_TRIGGER_RESUME: |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 623 | dev_dbg(rtd->dev, "sst: in pause release\n"); |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 624 | status = SST_PLATFORM_RUNNING; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 625 | ret_val = stream->ops->stream_pause_release(sst->dev, str_id); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 626 | break; |
| 627 | default: |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 628 | return -EINVAL; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 629 | } |
Subhransu S. Prusty | 5981c2d | 2014-08-04 15:04:20 +0530 | [diff] [blame] | 630 | |
Harsha Priya | a211701 | 2011-01-11 14:50:59 +0530 | [diff] [blame] | 631 | if (!ret_val) |
| 632 | sst_set_stream_status(stream, status); |
| 633 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 634 | return ret_val; |
| 635 | } |
| 636 | |
| 637 | |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 638 | static snd_pcm_uframes_t sst_soc_pointer(struct snd_soc_component *component, |
| 639 | struct snd_pcm_substream *substream) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 640 | { |
| 641 | struct sst_runtime_stream *stream; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 642 | int ret_val, status; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 643 | struct pcm_stream_info *str_info; |
Kuninori Morimoto | 2ab9a40 | 2020-07-27 10:08:42 +0900 | [diff] [blame] | 644 | struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 645 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 646 | stream = substream->runtime->private_data; |
Harsha Priya | 5c68536 | 2011-01-11 14:50:35 +0530 | [diff] [blame] | 647 | status = sst_get_stream_status(stream); |
| 648 | if (status == SST_PLATFORM_INIT) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 649 | return 0; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 650 | str_info = &stream->stream_info; |
Subhransu S. Prusty | b12b087c | 2014-08-04 15:04:21 +0530 | [diff] [blame] | 651 | ret_val = stream->ops->stream_read_tstamp(sst->dev, str_info); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 652 | if (ret_val) { |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 653 | dev_err(rtd->dev, "sst: error code = %d\n", ret_val); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 654 | return ret_val; |
| 655 | } |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 656 | return str_info->buffer_ptr; |
xingchao | 9ab8843 | 2011-04-27 16:58:54 -0400 | [diff] [blame] | 657 | } |
| 658 | |
Kuninori Morimoto | 796b64a | 2021-11-16 16:45:29 +0900 | [diff] [blame] | 659 | static snd_pcm_sframes_t sst_soc_delay(struct snd_soc_component *component, |
| 660 | struct snd_pcm_substream *substream) |
| 661 | { |
| 662 | struct sst_runtime_stream *stream = substream->runtime->private_data; |
| 663 | struct pcm_stream_info *str_info = &stream->stream_info; |
| 664 | |
| 665 | if (sst_get_stream_status(stream) == SST_PLATFORM_INIT) |
| 666 | return 0; |
| 667 | |
| 668 | return str_info->pcm_delay; |
| 669 | } |
| 670 | |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 671 | static int sst_soc_pcm_new(struct snd_soc_component *component, |
| 672 | struct snd_soc_pcm_runtime *rtd) |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 673 | { |
Kuninori Morimoto | 0d1571c | 2020-03-23 14:19:05 +0900 | [diff] [blame] | 674 | struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0); |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 675 | struct snd_pcm *pcm = rtd->pcm; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 676 | |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 677 | if (dai->driver->playback.channels_min || |
| 678 | dai->driver->capture.channels_min) { |
Takashi Iwai | 02298145 | 2019-12-10 15:26:11 +0100 | [diff] [blame] | 679 | snd_pcm_set_managed_buffer_all(pcm, |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 680 | SNDRV_DMA_TYPE_CONTINUOUS, |
Vinod Koul | 6cc0f4e | 2014-06-13 18:03:51 +0530 | [diff] [blame] | 681 | snd_dma_continuous_data(GFP_DMA), |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 682 | SST_MIN_BUFFER, SST_MAX_BUFFER); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 683 | } |
Takashi Iwai | 62961dd | 2019-02-04 16:38:42 +0100 | [diff] [blame] | 684 | return 0; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 685 | } |
Vinod Koul | c514a91 | 2012-08-16 17:10:42 +0530 | [diff] [blame] | 686 | |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 687 | static int sst_soc_probe(struct snd_soc_component *component) |
Vinod Koul | d8499c9 | 2014-08-04 15:15:55 +0530 | [diff] [blame] | 688 | { |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 689 | struct sst_data *drv = dev_get_drvdata(component->dev); |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 690 | |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 691 | drv->soc_card = component->card; |
| 692 | return sst_dsp_init_v2_dpcm(component); |
Vinod Koul | d8499c9 | 2014-08-04 15:15:55 +0530 | [diff] [blame] | 693 | } |
| 694 | |
Guenter Roeck | 8f71370 | 2019-03-22 15:39:48 -0700 | [diff] [blame] | 695 | static void sst_soc_remove(struct snd_soc_component *component) |
| 696 | { |
| 697 | struct sst_data *drv = dev_get_drvdata(component->dev); |
| 698 | |
| 699 | drv->soc_card = NULL; |
| 700 | } |
| 701 | |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 702 | static const struct snd_soc_component_driver sst_soc_platform_drv = { |
| 703 | .name = DRV_NAME, |
Vinod Koul | d8499c9 | 2014-08-04 15:15:55 +0530 | [diff] [blame] | 704 | .probe = sst_soc_probe, |
Guenter Roeck | 8f71370 | 2019-03-22 15:39:48 -0700 | [diff] [blame] | 705 | .remove = sst_soc_remove, |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 706 | .open = sst_soc_open, |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 707 | .trigger = sst_soc_trigger, |
| 708 | .pointer = sst_soc_pointer, |
Kuninori Morimoto | 796b64a | 2021-11-16 16:45:29 +0900 | [diff] [blame] | 709 | .delay = sst_soc_delay, |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 710 | .compress_ops = &sst_platform_compress_ops, |
Kuninori Morimoto | 9b9974d | 2019-10-02 14:31:41 +0900 | [diff] [blame] | 711 | .pcm_construct = sst_soc_pcm_new, |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 712 | }; |
| 713 | |
| 714 | static int sst_platform_probe(struct platform_device *pdev) |
| 715 | { |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 716 | struct sst_data *drv; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 717 | int ret; |
Subhransu S. Prusty | 2741d43 | 2014-07-30 18:39:05 +0530 | [diff] [blame] | 718 | struct sst_platform_data *pdata; |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 719 | |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 720 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
Subhransu S. Prusty | 19a23a5 | 2014-07-30 18:36:00 +0530 | [diff] [blame] | 721 | if (drv == NULL) { |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 722 | return -ENOMEM; |
| 723 | } |
| 724 | |
Subhransu S. Prusty | 2741d43 | 2014-07-30 18:39:05 +0530 | [diff] [blame] | 725 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 726 | if (pdata == NULL) { |
Subhransu S. Prusty | 2741d43 | 2014-07-30 18:39:05 +0530 | [diff] [blame] | 727 | return -ENOMEM; |
| 728 | } |
| 729 | |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 730 | pdata->pdev_strm_map = dpcm_strm_map; |
| 731 | pdata->strm_map_size = ARRAY_SIZE(dpcm_strm_map); |
| 732 | drv->pdata = pdata; |
Subhransu S. Prusty | 3172fcd | 2014-10-30 16:21:44 +0530 | [diff] [blame] | 733 | drv->pdev = pdev; |
Vinod Koul | 61b165c | 2014-06-13 18:03:56 +0530 | [diff] [blame] | 734 | mutex_init(&drv->lock); |
| 735 | dev_set_drvdata(&pdev->dev, drv); |
| 736 | |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 737 | ret = devm_snd_soc_register_component(&pdev->dev, &sst_soc_platform_drv, |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 738 | sst_platform_dai, ARRAY_SIZE(sst_platform_dai)); |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 739 | if (ret) |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 740 | dev_err(&pdev->dev, "registering cpu dais failed\n"); |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 741 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | static int sst_platform_remove(struct platform_device *pdev) |
| 746 | { |
Subhransu S. Prusty | 0202475 | 2014-09-02 18:05:56 +0530 | [diff] [blame] | 747 | dev_dbg(&pdev->dev, "sst_platform_remove success\n"); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 751 | #ifdef CONFIG_PM_SLEEP |
| 752 | |
| 753 | static int sst_soc_prepare(struct device *dev) |
| 754 | { |
| 755 | struct sst_data *drv = dev_get_drvdata(dev); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 756 | struct snd_soc_pcm_runtime *rtd; |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 757 | |
Takashi Iwai | 2fc995a | 2016-11-25 16:54:06 +0100 | [diff] [blame] | 758 | if (!drv->soc_card) |
| 759 | return 0; |
| 760 | |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 761 | /* suspend all pcms first */ |
| 762 | snd_soc_suspend(drv->soc_card->dev); |
| 763 | snd_soc_poweroff(drv->soc_card->dev); |
| 764 | |
| 765 | /* set the SSPs to idle */ |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 766 | for_each_card_rtds(drv->soc_card, rtd) { |
Kuninori Morimoto | 0d1571c | 2020-03-23 14:19:05 +0900 | [diff] [blame] | 767 | struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0); |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 768 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 769 | if (snd_soc_dai_active(dai)) { |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 770 | send_ssp_cmd(dai, dai->name, 0); |
| 771 | sst_handle_vb_timer(dai, false); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static void sst_soc_complete(struct device *dev) |
| 779 | { |
| 780 | struct sst_data *drv = dev_get_drvdata(dev); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 781 | struct snd_soc_pcm_runtime *rtd; |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 782 | |
Takashi Iwai | 2fc995a | 2016-11-25 16:54:06 +0100 | [diff] [blame] | 783 | if (!drv->soc_card) |
| 784 | return; |
| 785 | |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 786 | /* restart SSPs */ |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 787 | for_each_card_rtds(drv->soc_card, rtd) { |
Kuninori Morimoto | 0d1571c | 2020-03-23 14:19:05 +0900 | [diff] [blame] | 788 | struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0); |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 789 | |
Kuninori Morimoto | 846d0a17 | 2020-05-15 09:47:22 +0900 | [diff] [blame] | 790 | if (snd_soc_dai_active(dai)) { |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 791 | sst_handle_vb_timer(dai, true); |
| 792 | send_ssp_cmd(dai, dai->name, 1); |
| 793 | } |
| 794 | } |
| 795 | snd_soc_resume(drv->soc_card->dev); |
| 796 | } |
| 797 | |
| 798 | #else |
| 799 | |
| 800 | #define sst_soc_prepare NULL |
| 801 | #define sst_soc_complete NULL |
| 802 | |
| 803 | #endif |
| 804 | |
| 805 | |
| 806 | static const struct dev_pm_ops sst_platform_pm = { |
| 807 | .prepare = sst_soc_prepare, |
| 808 | .complete = sst_soc_complete, |
| 809 | }; |
| 810 | |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 811 | static struct platform_driver sst_platform_driver = { |
| 812 | .driver = { |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 813 | .name = "sst-mfld-platform", |
Vinod Koul | 54e6bec | 2015-02-12 09:59:58 +0530 | [diff] [blame] | 814 | .pm = &sst_platform_pm, |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 815 | }, |
| 816 | .probe = sst_platform_probe, |
| 817 | .remove = sst_platform_remove, |
| 818 | }; |
| 819 | |
Axel Lin | 29515d6 | 2011-11-24 11:51:56 +0800 | [diff] [blame] | 820 | module_platform_driver(sst_platform_driver); |
Vinod Koul | d927fda | 2011-01-04 20:16:32 +0530 | [diff] [blame] | 821 | |
| 822 | MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver"); |
| 823 | MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>"); |
| 824 | MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>"); |
| 825 | MODULE_LICENSE("GPL v2"); |
Andy Shevchenko | 231a091 | 2017-01-16 15:12:29 +0200 | [diff] [blame] | 826 | MODULE_ALIAS("platform:sst-atom-hifi2-platform"); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 827 | MODULE_ALIAS("platform:sst-mfld-platform"); |