1 // SPDX-License-Identifier: GPL-2.0-only 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 * 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 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 42 static int sst_platform_compr_open(struct snd_soc_component *component, 43 struct snd_compr_stream *cstream) 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; 63 stream->id = 0; 64 65 /* Turn on LPE */ 66 sst->compr_ops->power(sst->dev, true); 67 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 76 static int sst_platform_compr_free(struct snd_soc_component *component, 77 struct snd_compr_stream *cstream) 78 { 79 struct sst_runtime_stream *stream; 80 int ret_val = 0, str_id; 81 82 stream = cstream->runtime->private_data; 83 /* Turn off LPE */ 84 sst->compr_ops->power(sst->dev, false); 85 86 /*need to check*/ 87 str_id = stream->id; 88 if (str_id) 89 ret_val = stream->compr_ops->close(sst->dev, str_id); 90 module_put(sst->dev->driver->owner); 91 kfree(stream); 92 pr_debug("%s: %d\n", __func__, ret_val); 93 return 0; 94 } 95 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) 99 { 100 struct sst_runtime_stream *stream; 101 int retval; 102 struct snd_sst_params str_params; 103 struct sst_compress_cb cb; 104 struct sst_data *ctx = snd_soc_component_get_drvdata(component); 105 106 stream = cstream->runtime->private_data; 107 /* construct fw structure for this*/ 108 memset(&str_params, 0, sizeof(str_params)); 109 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; 115 116 switch (params->codec.id) { 117 case SND_AUDIOCODEC_MP3: { 118 str_params.codec = SST_CODEC_TYPE_MP3; 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; 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; 157 cb.drain_cb_param = cstream; 158 cb.drain_notify = sst_drain_notify; 159 160 retval = stream->compr_ops->open(sst->dev, &str_params, &cb); 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 170 static int sst_platform_compr_trigger(struct snd_soc_component *component, 171 struct snd_compr_stream *cstream, int cmd) 172 { 173 struct sst_runtime_stream *stream = cstream->runtime->private_data; 174 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); 179 break; 180 case SNDRV_PCM_TRIGGER_STOP: 181 if (stream->compr_ops->stream_drop) 182 return stream->compr_ops->stream_drop(sst->dev, stream->id); 183 break; 184 case SND_COMPR_TRIGGER_DRAIN: 185 if (stream->compr_ops->stream_drain) 186 return stream->compr_ops->stream_drain(sst->dev, stream->id); 187 break; 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); 191 break; 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); 195 break; 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); 199 break; 200 } 201 return -EINVAL; 202 } 203 204 static int sst_platform_compr_pointer(struct snd_soc_component *component, 205 struct snd_compr_stream *cstream, 206 struct snd_compr_tstamp *tstamp) 207 { 208 struct sst_runtime_stream *stream; 209 210 stream = cstream->runtime->private_data; 211 stream->compr_ops->tstamp(sst->dev, stream->id, tstamp); 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 218 static int sst_platform_compr_ack(struct snd_soc_component *component, 219 struct snd_compr_stream *cstream, 220 size_t bytes) 221 { 222 struct sst_runtime_stream *stream; 223 224 stream = cstream->runtime->private_data; 225 stream->compr_ops->ack(sst->dev, stream->id, (unsigned long)bytes); 226 stream->bytes_written += bytes; 227 228 return 0; 229 } 230 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) 234 { 235 struct sst_runtime_stream *stream = 236 cstream->runtime->private_data; 237 238 return stream->compr_ops->get_caps(caps); 239 } 240 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) 244 { 245 struct sst_runtime_stream *stream = 246 cstream->runtime->private_data; 247 248 return stream->compr_ops->get_codec_caps(codec); 249 } 250 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) 254 { 255 struct sst_runtime_stream *stream = 256 cstream->runtime->private_data; 257 258 return stream->compr_ops->set_metadata(sst->dev, stream->id, metadata); 259 } 260 261 const struct snd_compress_ops sst_platform_compress_ops = { 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 }; 273