Thomas Gleixner | 8e8e69d | 2019-05-29 07:17:59 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 2 | /* |
| 3 | * sst_mfld_platform.c - Intel MID Platform driver |
| 4 | * |
| 5 | * Copyright (C) 2010-2014 Intel Corp |
| 6 | * Author: Vinod Koul <vinod.koul@intel.com> |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | */ |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <sound/core.h> |
| 17 | #include <sound/pcm.h> |
| 18 | #include <sound/pcm_params.h> |
| 19 | #include <sound/soc.h> |
| 20 | #include <sound/compress_driver.h> |
| 21 | #include "sst-mfld-platform.h" |
| 22 | |
| 23 | /* compress stream operations */ |
| 24 | static void sst_compr_fragment_elapsed(void *arg) |
| 25 | { |
| 26 | struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg; |
| 27 | |
| 28 | pr_debug("fragment elapsed by driver\n"); |
| 29 | if (cstream) |
| 30 | snd_compr_fragment_elapsed(cstream); |
| 31 | } |
| 32 | |
Vinod Koul | bd17aa4 | 2014-05-15 21:38:16 +0530 | [diff] [blame] | 33 | static void sst_drain_notify(void *arg) |
| 34 | { |
| 35 | struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg; |
| 36 | |
| 37 | pr_debug("drain notify by driver\n"); |
| 38 | if (cstream) |
| 39 | snd_compr_drain_notify(cstream); |
| 40 | } |
| 41 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 42 | static int sst_platform_compr_open(struct snd_soc_component *component, |
| 43 | struct snd_compr_stream *cstream) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 44 | { |
| 45 | |
| 46 | int ret_val = 0; |
| 47 | struct snd_compr_runtime *runtime = cstream->runtime; |
| 48 | struct sst_runtime_stream *stream; |
| 49 | |
| 50 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 51 | if (!stream) |
| 52 | return -ENOMEM; |
| 53 | |
| 54 | spin_lock_init(&stream->status_lock); |
| 55 | |
| 56 | /* get the sst ops */ |
| 57 | if (!sst || !try_module_get(sst->dev->driver->owner)) { |
| 58 | pr_err("no device available to run\n"); |
| 59 | ret_val = -ENODEV; |
| 60 | goto out_ops; |
| 61 | } |
| 62 | stream->compr_ops = sst->compr_ops; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 63 | stream->id = 0; |
Subhransu S. Prusty | fdcc4a0 | 2014-10-30 16:20:58 +0530 | [diff] [blame] | 64 | |
| 65 | /* Turn on LPE */ |
| 66 | sst->compr_ops->power(sst->dev, true); |
| 67 | |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 68 | sst_set_stream_status(stream, SST_PLATFORM_INIT); |
| 69 | runtime->private_data = stream; |
| 70 | return 0; |
| 71 | out_ops: |
| 72 | kfree(stream); |
| 73 | return ret_val; |
| 74 | } |
| 75 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 76 | static int sst_platform_compr_free(struct snd_soc_component *component, |
| 77 | struct snd_compr_stream *cstream) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 78 | { |
| 79 | struct sst_runtime_stream *stream; |
| 80 | int ret_val = 0, str_id; |
| 81 | |
| 82 | stream = cstream->runtime->private_data; |
Subhransu S. Prusty | fdcc4a0 | 2014-10-30 16:20:58 +0530 | [diff] [blame] | 83 | /* Turn off LPE */ |
| 84 | sst->compr_ops->power(sst->dev, false); |
| 85 | |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 86 | /*need to check*/ |
| 87 | str_id = stream->id; |
| 88 | if (str_id) |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 89 | ret_val = stream->compr_ops->close(sst->dev, str_id); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 90 | module_put(sst->dev->driver->owner); |
| 91 | kfree(stream); |
| 92 | pr_debug("%s: %d\n", __func__, ret_val); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 96 | static int sst_platform_compr_set_params(struct snd_soc_component *component, |
| 97 | struct snd_compr_stream *cstream, |
| 98 | struct snd_compr_params *params) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 99 | { |
| 100 | struct sst_runtime_stream *stream; |
| 101 | int retval; |
| 102 | struct snd_sst_params str_params; |
| 103 | struct sst_compress_cb cb; |
Kuninori Morimoto | 6840962 | 2018-01-29 02:41:28 +0000 | [diff] [blame] | 104 | struct sst_data *ctx = snd_soc_component_get_drvdata(component); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 105 | |
| 106 | stream = cstream->runtime->private_data; |
| 107 | /* construct fw structure for this*/ |
| 108 | memset(&str_params, 0, sizeof(str_params)); |
| 109 | |
Vinod Koul | 0ec66fe | 2014-06-13 18:03:57 +0530 | [diff] [blame] | 110 | /* fill the device type and stream id to pass to SST driver */ |
| 111 | retval = sst_fill_stream_params(cstream, ctx, &str_params, true); |
| 112 | pr_debug("compr_set_params: fill stream params ret_val = 0x%x\n", retval); |
| 113 | if (retval < 0) |
| 114 | return retval; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 115 | |
| 116 | switch (params->codec.id) { |
| 117 | case SND_AUDIOCODEC_MP3: { |
| 118 | str_params.codec = SST_CODEC_TYPE_MP3; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 119 | str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in; |
| 120 | str_params.sparams.uc.mp3_params.pcm_wd_sz = 16; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | case SND_AUDIOCODEC_AAC: { |
| 125 | str_params.codec = SST_CODEC_TYPE_AAC; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 126 | str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in; |
| 127 | str_params.sparams.uc.aac_params.pcm_wd_sz = 16; |
| 128 | if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS) |
| 129 | str_params.sparams.uc.aac_params.bs_format = |
| 130 | AAC_BIT_STREAM_ADTS; |
| 131 | else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW) |
| 132 | str_params.sparams.uc.aac_params.bs_format = |
| 133 | AAC_BIT_STREAM_RAW; |
| 134 | else { |
| 135 | pr_err("Undefined format%d\n", params->codec.format); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | str_params.sparams.uc.aac_params.externalsr = |
| 139 | params->codec.sample_rate; |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | default: |
| 144 | pr_err("codec not supported, id =%d\n", params->codec.id); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
| 148 | str_params.aparams.ring_buf_info[0].addr = |
| 149 | virt_to_phys(cstream->runtime->buffer); |
| 150 | str_params.aparams.ring_buf_info[0].size = |
| 151 | cstream->runtime->buffer_size; |
| 152 | str_params.aparams.sg_count = 1; |
| 153 | str_params.aparams.frag_size = cstream->runtime->fragment_size; |
| 154 | |
| 155 | cb.param = cstream; |
| 156 | cb.compr_cb = sst_compr_fragment_elapsed; |
Vinod Koul | bd17aa4 | 2014-05-15 21:38:16 +0530 | [diff] [blame] | 157 | cb.drain_cb_param = cstream; |
| 158 | cb.drain_notify = sst_drain_notify; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 159 | |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 160 | retval = stream->compr_ops->open(sst->dev, &str_params, &cb); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 161 | if (retval < 0) { |
| 162 | pr_err("stream allocation failed %d\n", retval); |
| 163 | return retval; |
| 164 | } |
| 165 | |
| 166 | stream->id = retval; |
| 167 | return 0; |
| 168 | } |
| 169 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 170 | static int sst_platform_compr_trigger(struct snd_soc_component *component, |
| 171 | struct snd_compr_stream *cstream, int cmd) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 172 | { |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 173 | struct sst_runtime_stream *stream = cstream->runtime->private_data; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 174 | |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 175 | switch (cmd) { |
| 176 | case SNDRV_PCM_TRIGGER_START: |
| 177 | if (stream->compr_ops->stream_start) |
| 178 | return stream->compr_ops->stream_start(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 179 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 180 | case SNDRV_PCM_TRIGGER_STOP: |
| 181 | if (stream->compr_ops->stream_drop) |
| 182 | return stream->compr_ops->stream_drop(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 183 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 184 | case SND_COMPR_TRIGGER_DRAIN: |
| 185 | if (stream->compr_ops->stream_drain) |
| 186 | return stream->compr_ops->stream_drain(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 187 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 188 | case SND_COMPR_TRIGGER_PARTIAL_DRAIN: |
| 189 | if (stream->compr_ops->stream_partial_drain) |
| 190 | return stream->compr_ops->stream_partial_drain(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 191 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 192 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 193 | if (stream->compr_ops->stream_pause) |
| 194 | return stream->compr_ops->stream_pause(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 195 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 196 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 197 | if (stream->compr_ops->stream_pause_release) |
| 198 | return stream->compr_ops->stream_pause_release(sst->dev, stream->id); |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 199 | break; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 200 | } |
Alan Cox | 18f5839 | 2016-06-23 22:07:03 +0530 | [diff] [blame] | 201 | return -EINVAL; |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 202 | } |
| 203 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 204 | static int sst_platform_compr_pointer(struct snd_soc_component *component, |
| 205 | struct snd_compr_stream *cstream, |
| 206 | struct snd_compr_tstamp *tstamp) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 207 | { |
| 208 | struct sst_runtime_stream *stream; |
| 209 | |
| 210 | stream = cstream->runtime->private_data; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 211 | stream->compr_ops->tstamp(sst->dev, stream->id, tstamp); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 212 | tstamp->byte_offset = tstamp->copied_total % |
| 213 | (u32)cstream->runtime->buffer_size; |
| 214 | pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset); |
| 215 | return 0; |
| 216 | } |
| 217 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 218 | static int sst_platform_compr_ack(struct snd_soc_component *component, |
| 219 | struct snd_compr_stream *cstream, |
| 220 | size_t bytes) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 221 | { |
| 222 | struct sst_runtime_stream *stream; |
| 223 | |
| 224 | stream = cstream->runtime->private_data; |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 225 | stream->compr_ops->ack(sst->dev, stream->id, (unsigned long)bytes); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 226 | stream->bytes_written += bytes; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 231 | static int sst_platform_compr_get_caps(struct snd_soc_component *component, |
| 232 | struct snd_compr_stream *cstream, |
| 233 | struct snd_compr_caps *caps) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 234 | { |
| 235 | struct sst_runtime_stream *stream = |
| 236 | cstream->runtime->private_data; |
| 237 | |
| 238 | return stream->compr_ops->get_caps(caps); |
| 239 | } |
| 240 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 241 | static int sst_platform_compr_get_codec_caps(struct snd_soc_component *component, |
| 242 | struct snd_compr_stream *cstream, |
| 243 | struct snd_compr_codec_caps *codec) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 244 | { |
| 245 | struct sst_runtime_stream *stream = |
| 246 | cstream->runtime->private_data; |
| 247 | |
| 248 | return stream->compr_ops->get_codec_caps(codec); |
| 249 | } |
| 250 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 251 | static int sst_platform_compr_set_metadata(struct snd_soc_component *component, |
| 252 | struct snd_compr_stream *cstream, |
| 253 | struct snd_compr_metadata *metadata) |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 254 | { |
| 255 | struct sst_runtime_stream *stream = |
| 256 | cstream->runtime->private_data; |
| 257 | |
Subhransu S. Prusty | 06cb1eb | 2014-08-21 18:20:49 +0530 | [diff] [blame] | 258 | return stream->compr_ops->set_metadata(sst->dev, stream->id, metadata); |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Kuninori Morimoto | c60e445 | 2020-04-20 16:09:09 +0900 | [diff] [blame] | 261 | const struct snd_compress_ops sst_platform_compress_ops = { |
Vinod Koul | 4b68b4e | 2014-05-05 14:27:50 +0530 | [diff] [blame] | 262 | |
| 263 | .open = sst_platform_compr_open, |
| 264 | .free = sst_platform_compr_free, |
| 265 | .set_params = sst_platform_compr_set_params, |
| 266 | .set_metadata = sst_platform_compr_set_metadata, |
| 267 | .trigger = sst_platform_compr_trigger, |
| 268 | .pointer = sst_platform_compr_pointer, |
| 269 | .ack = sst_platform_compr_ack, |
| 270 | .get_caps = sst_platform_compr_get_caps, |
| 271 | .get_codec_caps = sst_platform_compr_get_codec_caps, |
| 272 | }; |