| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. |
| 3 |
// Copyright (c) 2018, Linaro Limited |
| 4 |
|
| 5 |
#include <dt-bindings/sound/qcom,q6asm.h> |
| 6 |
#include <linux/init.h> |
| 7 |
#include <linux/err.h> |
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/of.h> |
| 10 |
#include <linux/platform_device.h> |
| 11 |
#include <linux/slab.h> |
| 12 |
#include <sound/soc.h> |
| 13 |
#include <sound/soc-dapm.h> |
| 14 |
#include <sound/pcm.h> |
| 15 |
#include <linux/spinlock.h> |
| 16 |
#include <sound/compress_driver.h> |
| 17 |
#include <asm/div64.h> |
| 18 |
#include <asm/dma.h> |
| 19 |
#include <linux/dma-mapping.h> |
| 20 |
#include <sound/pcm_params.h> |
| 21 |
#include "q6asm.h" |
| 22 |
#include "q6routing.h" |
| 23 |
#include "q6dsp-errno.h" |
| 24 |
|
| 25 |
#define DRV_NAME "q6asm-fe-dai" |
| 26 |
|
| 27 |
#define PLAYBACK_MIN_NUM_PERIODS 2 |
| 28 |
#define PLAYBACK_MAX_NUM_PERIODS 8 |
| 29 |
#define PLAYBACK_MAX_PERIOD_SIZE 65536 |
| 30 |
#define PLAYBACK_MIN_PERIOD_SIZE 128 |
| 31 |
#define CAPTURE_MIN_NUM_PERIODS 2 |
| 32 |
#define CAPTURE_MAX_NUM_PERIODS 8 |
| 33 |
#define CAPTURE_MAX_PERIOD_SIZE 4096 |
| 34 |
#define CAPTURE_MIN_PERIOD_SIZE 320 |
| 35 |
#define SID_MASK_DEFAULT 0xF |
| 36 |
|
| 37 |
/* Default values used if user space does not set */ |
| 38 |
#define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024) |
| 39 |
#define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024) |
| 40 |
#define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4) |
| 41 |
#define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4) |
| 42 |
|
| 43 |
#define ALAC_CH_LAYOUT_MONO ((101 << 16) | 1) |
| 44 |
#define ALAC_CH_LAYOUT_STEREO ((101 << 16) | 2) |
| 45 |
|
| 46 |
enum stream_state { |
| 47 |
Q6ASM_STREAM_IDLE = 0, |
| 48 |
Q6ASM_STREAM_STOPPED, |
| 49 |
Q6ASM_STREAM_RUNNING, |
| 50 |
}; |
| 51 |
|
| 52 |
struct q6asm_dai_rtd { |
| 53 |
struct snd_pcm_substream *substream; |
| 54 |
struct snd_compr_stream *cstream; |
| 55 |
struct snd_codec codec; |
| 56 |
struct snd_dma_buffer dma_buffer; |
| 57 |
spinlock_t lock; |
| 58 |
phys_addr_t phys; |
| 59 |
unsigned int pcm_size; |
| 60 |
unsigned int pcm_count; |
| 61 |
unsigned int periods; |
| 62 |
uint64_t bytes_sent; |
| 63 |
uint64_t bytes_received; |
| 64 |
uint64_t copied_total; |
| 65 |
uint16_t bits_per_sample; |
| 66 |
snd_pcm_uframes_t queue_ptr; |
| 67 |
uint16_t source; /* Encoding source bit mask */ |
| 68 |
struct audio_client *audio_client; |
| 69 |
uint32_t next_track_stream_id; |
| 70 |
bool next_track; |
| 71 |
uint32_t stream_id; |
| 72 |
uint16_t session_id; |
| 73 |
enum stream_state state; |
| 74 |
uint32_t initial_samples_drop; |
| 75 |
uint32_t trailing_samples_drop; |
| 76 |
bool notify_on_drain; |
| 77 |
}; |
| 78 |
|
| 79 |
struct q6asm_dai_data { |
| 80 |
struct snd_soc_dai_driver *dais; |
| 81 |
int num_dais; |
| 82 |
long long int sid; |
| 83 |
}; |
| 84 |
|
| 85 |
static const struct snd_pcm_hardware q6asm_dai_hardware_capture = { |
| 86 |
.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH | |
| 87 |
SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 88 |
SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | |
| 89 |
SNDRV_PCM_INFO_MMAP_VALID | |
| 90 |
SNDRV_PCM_INFO_INTERLEAVED | |
| 91 |
SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), |
| 92 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | |
| 93 |
SNDRV_PCM_FMTBIT_S24_LE), |
| 94 |
.rates = SNDRV_PCM_RATE_8000_48000, |
| 95 |
.rate_min = 8000, |
| 96 |
.rate_max = 48000, |
| 97 |
.channels_min = 1, |
| 98 |
.channels_max = 4, |
| 99 |
.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * |
| 100 |
CAPTURE_MAX_PERIOD_SIZE, |
| 101 |
.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, |
| 102 |
.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, |
| 103 |
.periods_min = CAPTURE_MIN_NUM_PERIODS, |
| 104 |
.periods_max = CAPTURE_MAX_NUM_PERIODS, |
| 105 |
.fifo_size = 0, |
| 106 |
}; |
| 107 |
|
| 108 |
static const struct snd_pcm_hardware q6asm_dai_hardware_playback = { |
| 109 |
.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BATCH | |
| 110 |
SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 111 |
SNDRV_PCM_INFO_MMAP_VALID | |
| 112 |
SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | |
| 113 |
SNDRV_PCM_INFO_INTERLEAVED | |
| 114 |
SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), |
| 115 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | |
| 116 |
SNDRV_PCM_FMTBIT_S24_LE), |
| 117 |
.rates = SNDRV_PCM_RATE_8000_192000, |
| 118 |
.rate_min = 8000, |
| 119 |
.rate_max = 192000, |
| 120 |
.channels_min = 1, |
| 121 |
.channels_max = 8, |
| 122 |
.buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * |
| 123 |
PLAYBACK_MAX_PERIOD_SIZE), |
| 124 |
.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, |
| 125 |
.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, |
| 126 |
.periods_min = PLAYBACK_MIN_NUM_PERIODS, |
| 127 |
.periods_max = PLAYBACK_MAX_NUM_PERIODS, |
| 128 |
.fifo_size = 0, |
| 129 |
}; |
| 130 |
|
| 131 |
#define Q6ASM_FEDAI_DRIVER(num) { \ |
| 132 |
.playback = { \ |
| 133 |
.stream_name = "MultiMedia"#num" Playback", \ |
| 134 |
.rates = (SNDRV_PCM_RATE_8000_48000 | \ |
| 135 |
SNDRV_PCM_RATE_12000 | \ |
| 136 |
SNDRV_PCM_RATE_24000 | \ |
| 137 |
SNDRV_PCM_RATE_88200 | \ |
| 138 |
SNDRV_PCM_RATE_96000 | \ |
| 139 |
SNDRV_PCM_RATE_176400 | \ |
| 140 |
SNDRV_PCM_RATE_192000), \ |
| 141 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 142 |
SNDRV_PCM_FMTBIT_S24_LE), \ |
| 143 |
.channels_min = 1, \ |
| 144 |
.channels_max = 8, \ |
| 145 |
.rate_min = 8000, \ |
| 146 |
.rate_max = 192000, \ |
| 147 |
}, \ |
| 148 |
.capture = { \ |
| 149 |
.stream_name = "MultiMedia"#num" Capture", \ |
| 150 |
.rates = (SNDRV_PCM_RATE_8000_48000 | \ |
| 151 |
SNDRV_PCM_RATE_12000 | \ |
| 152 |
SNDRV_PCM_RATE_24000), \ |
| 153 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 154 |
SNDRV_PCM_FMTBIT_S24_LE), \ |
| 155 |
.channels_min = 1, \ |
| 156 |
.channels_max = 4, \ |
| 157 |
.rate_min = 8000, \ |
| 158 |
.rate_max = 48000, \ |
| 159 |
}, \ |
| 160 |
.name = "MultiMedia"#num, \ |
| 161 |
.id = MSM_FRONTEND_DAI_MULTIMEDIA##num, \ |
| 162 |
} |
| 163 |
|
| 164 |
static const struct snd_compr_codec_caps q6asm_compr_caps = { |
| 165 |
.num_descriptors = 1, |
| 166 |
.descriptor[0].max_ch = 2, |
| 167 |
.descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050, |
| 168 |
24000, 32000, 44100, 48000, 88200, |
| 169 |
96000, 176400, 192000 }, |
| 170 |
.descriptor[0].num_sample_rates = 13, |
| 171 |
.descriptor[0].bit_rate[0] = 320, |
| 172 |
.descriptor[0].bit_rate[1] = 128, |
| 173 |
.descriptor[0].num_bitrates = 2, |
| 174 |
.descriptor[0].profiles = 0, |
| 175 |
.descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO, |
| 176 |
.descriptor[0].formats = 0, |
| 177 |
}; |
| 178 |
|
| 179 |
static void event_handler(uint32_t opcode, uint32_t token, |
| 180 |
void *payload, void *priv) |
| 181 |
{ |
| 182 |
struct q6asm_dai_rtd *prtd = priv; |
| 183 |
struct snd_pcm_substream *substream = prtd->substream; |
| 184 |
|
| 185 |
switch (opcode) { |
| 186 |
case ASM_CLIENT_EVENT_CMD_RUN_DONE: |
| 187 |
break; |
| 188 |
case ASM_CLIENT_EVENT_CMD_EOS_DONE: |
| 189 |
break; |
| 190 |
case ASM_CLIENT_EVENT_DATA_WRITE_DONE: { |
| 191 |
snd_pcm_period_elapsed(substream); |
| 192 |
break; |
| 193 |
} |
| 194 |
case ASM_CLIENT_EVENT_DATA_READ_DONE: |
| 195 |
snd_pcm_period_elapsed(substream); |
| 196 |
if (prtd->state == Q6ASM_STREAM_RUNNING) |
| 197 |
q6asm_read(prtd->audio_client, prtd->stream_id); |
| 198 |
|
| 199 |
break; |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static int q6asm_dai_prepare(struct snd_soc_component *component, |
| 206 |
struct snd_pcm_substream *substream) |
| 207 |
{ |
| 208 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 209 |
struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream); |
| 210 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 211 |
struct q6asm_dai_data *pdata; |
| 212 |
struct device *dev = component->dev; |
| 213 |
int ret, i; |
| 214 |
|
| 215 |
pdata = snd_soc_component_get_drvdata(component); |
| 216 |
if (!pdata) |
| 217 |
return -EINVAL; |
| 218 |
|
| 219 |
if (!prtd || !prtd->audio_client) { |
| 220 |
dev_err(dev, "%s: private data null or audio client freed\n", |
| 221 |
__func__); |
| 222 |
return -EINVAL; |
| 223 |
} |
| 224 |
|
| 225 |
prtd->pcm_count = snd_pcm_lib_period_bytes(substream); |
| 226 |
/* rate and channels are sent to audio driver */ |
| 227 |
if (prtd->state == Q6ASM_STREAM_RUNNING) { |
| 228 |
/* clear the previous setup if any */ |
| 229 |
ret = q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE); |
| 230 |
if (ret < 0) { |
| 231 |
dev_err(dev, "Failed to close q6asm stream %d\n", prtd->stream_id); |
| 232 |
return ret; |
| 233 |
} |
| 234 |
|
| 235 |
ret = q6asm_unmap_memory_regions(substream->stream, prtd->audio_client); |
| 236 |
if (ret < 0) { |
| 237 |
dev_err(dev, "Failed to unmap memory regions for q6asm stream %d\n", |
| 238 |
prtd->stream_id); |
| 239 |
return ret; |
| 240 |
} |
| 241 |
|
| 242 |
q6routing_stream_close(soc_prtd->dai_link->id, |
| 243 |
substream->stream); |
| 244 |
prtd->state = Q6ASM_STREAM_STOPPED; |
| 245 |
} |
| 246 |
|
| 247 |
ret = q6asm_map_memory_regions(substream->stream, prtd->audio_client, |
| 248 |
prtd->phys, |
| 249 |
(prtd->pcm_size / prtd->periods), |
| 250 |
prtd->periods); |
| 251 |
|
| 252 |
if (ret < 0) { |
| 253 |
dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", |
| 254 |
ret); |
| 255 |
return -ENOMEM; |
| 256 |
} |
| 257 |
|
| 258 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 259 |
ret = q6asm_open_write(prtd->audio_client, prtd->stream_id, |
| 260 |
FORMAT_LINEAR_PCM, |
| 261 |
0, prtd->bits_per_sample, false); |
| 262 |
} else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 263 |
ret = q6asm_open_read(prtd->audio_client, prtd->stream_id, |
| 264 |
FORMAT_LINEAR_PCM, |
| 265 |
prtd->bits_per_sample); |
| 266 |
} |
| 267 |
|
| 268 |
if (ret < 0) { |
| 269 |
dev_err(dev, "%s: q6asm_open_write failed\n", __func__); |
| 270 |
goto open_err; |
| 271 |
} |
| 272 |
|
| 273 |
prtd->session_id = q6asm_get_session_id(prtd->audio_client); |
| 274 |
ret = q6routing_stream_open(soc_prtd->dai_link->id, LEGACY_PCM_MODE, |
| 275 |
prtd->session_id, substream->stream); |
| 276 |
if (ret) { |
| 277 |
dev_err(dev, "%s: stream reg failed ret:%d\n", __func__, ret); |
| 278 |
goto routing_err; |
| 279 |
} |
| 280 |
|
| 281 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 282 |
ret = q6asm_media_format_block_multi_ch_pcm( |
| 283 |
prtd->audio_client, prtd->stream_id, |
| 284 |
runtime->rate, runtime->channels, NULL, |
| 285 |
prtd->bits_per_sample); |
| 286 |
} else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 287 |
ret = q6asm_enc_cfg_blk_pcm_format_support(prtd->audio_client, |
| 288 |
prtd->stream_id, |
| 289 |
runtime->rate, |
| 290 |
runtime->channels, |
| 291 |
prtd->bits_per_sample); |
| 292 |
|
| 293 |
/* Queue the buffers */ |
| 294 |
for (i = 0; i < runtime->periods; i++) |
| 295 |
q6asm_read(prtd->audio_client, prtd->stream_id); |
| 296 |
|
| 297 |
} |
| 298 |
if (ret < 0) |
| 299 |
dev_info(dev, "%s: CMD Format block failed\n", __func__); |
| 300 |
else |
| 301 |
prtd->state = Q6ASM_STREAM_RUNNING; |
| 302 |
|
| 303 |
return ret; |
| 304 |
|
| 305 |
routing_err: |
| 306 |
q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE); |
| 307 |
open_err: |
| 308 |
q6asm_unmap_memory_regions(substream->stream, prtd->audio_client); |
| 309 |
|
| 310 |
return ret; |
| 311 |
} |
| 312 |
|
| 313 |
static int q6asm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream) |
| 314 |
{ |
| 315 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 316 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 317 |
int i, ret = 0, avail_periods; |
| 318 |
|
| 319 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && prtd->state == Q6ASM_STREAM_RUNNING) { |
| 320 |
avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size; |
| 321 |
for (i = 0; i < avail_periods; i++) { |
| 322 |
ret = q6asm_write_async(prtd->audio_client, prtd->stream_id, |
| 323 |
prtd->pcm_count, 0, 0, 0); |
| 324 |
|
| 325 |
if (ret < 0) { |
| 326 |
dev_err(component->dev, "Error queuing playback buffer %d\n", ret); |
| 327 |
return ret; |
| 328 |
} |
| 329 |
prtd->queue_ptr += runtime->period_size; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
return ret; |
| 334 |
} |
| 335 |
|
| 336 |
static int q6asm_dai_trigger(struct snd_soc_component *component, |
| 337 |
struct snd_pcm_substream *substream, int cmd) |
| 338 |
{ |
| 339 |
int ret = 0; |
| 340 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 341 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 342 |
|
| 343 |
switch (cmd) { |
| 344 |
case SNDRV_PCM_TRIGGER_START: |
| 345 |
case SNDRV_PCM_TRIGGER_RESUME: |
| 346 |
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 347 |
ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id, |
| 348 |
0, 0, 0); |
| 349 |
break; |
| 350 |
case SNDRV_PCM_TRIGGER_STOP: |
| 351 |
ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id, |
| 352 |
CMD_EOS); |
| 353 |
break; |
| 354 |
case SNDRV_PCM_TRIGGER_SUSPEND: |
| 355 |
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 356 |
ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id, |
| 357 |
CMD_PAUSE); |
| 358 |
break; |
| 359 |
default: |
| 360 |
ret = -EINVAL; |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
return ret; |
| 365 |
} |
| 366 |
|
| 367 |
static int q6asm_dai_open(struct snd_soc_component *component, |
| 368 |
struct snd_pcm_substream *substream) |
| 369 |
{ |
| 370 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 371 |
struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream); |
| 372 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0); |
| 373 |
struct q6asm_dai_rtd *prtd; |
| 374 |
struct q6asm_dai_data *pdata; |
| 375 |
struct device *dev = component->dev; |
| 376 |
int ret = 0; |
| 377 |
int stream_id; |
| 378 |
|
| 379 |
stream_id = cpu_dai->driver->id; |
| 380 |
|
| 381 |
pdata = snd_soc_component_get_drvdata(component); |
| 382 |
if (!pdata) { |
| 383 |
dev_err(dev, "Drv data not found ..\n"); |
| 384 |
return -EINVAL; |
| 385 |
} |
| 386 |
|
| 387 |
prtd = kzalloc_obj(struct q6asm_dai_rtd); |
| 388 |
if (prtd == NULL) |
| 389 |
return -ENOMEM; |
| 390 |
|
| 391 |
prtd->substream = substream; |
| 392 |
prtd->audio_client = q6asm_audio_client_alloc(dev, |
| 393 |
(q6asm_cb)event_handler, prtd, stream_id, |
| 394 |
LEGACY_PCM_MODE); |
| 395 |
if (IS_ERR(prtd->audio_client)) { |
| 396 |
dev_info(dev, "%s: Could not allocate memory\n", __func__); |
| 397 |
ret = PTR_ERR(prtd->audio_client); |
| 398 |
kfree(prtd); |
| 399 |
return ret; |
| 400 |
} |
| 401 |
|
| 402 |
/* DSP expects stream id from 1 */ |
| 403 |
prtd->stream_id = 1; |
| 404 |
|
| 405 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 406 |
runtime->hw = q6asm_dai_hardware_playback; |
| 407 |
else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 408 |
runtime->hw = q6asm_dai_hardware_capture; |
| 409 |
|
| 410 |
/* Ensure that buffer size is a multiple of period size */ |
| 411 |
ret = snd_pcm_hw_constraint_integer(runtime, |
| 412 |
SNDRV_PCM_HW_PARAM_PERIODS); |
| 413 |
if (ret < 0) |
| 414 |
dev_info(dev, "snd_pcm_hw_constraint_integer failed\n"); |
| 415 |
|
| 416 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 417 |
ret = snd_pcm_hw_constraint_minmax(runtime, |
| 418 |
SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 419 |
PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE, |
| 420 |
PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE); |
| 421 |
if (ret < 0) { |
| 422 |
dev_err(dev, "constraint for buffer bytes min max ret = %d\n", |
| 423 |
ret); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 428 |
SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 480); |
| 429 |
if (ret < 0) { |
| 430 |
dev_err(dev, "constraint for period bytes step ret = %d\n", |
| 431 |
ret); |
| 432 |
} |
| 433 |
ret = snd_pcm_hw_constraint_step(runtime, 0, |
| 434 |
SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 480); |
| 435 |
if (ret < 0) { |
| 436 |
dev_err(dev, "constraint for buffer bytes step ret = %d\n", |
| 437 |
ret); |
| 438 |
} |
| 439 |
|
| 440 |
runtime->private_data = prtd; |
| 441 |
|
| 442 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 443 |
snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_playback); |
| 444 |
runtime->dma_bytes = q6asm_dai_hardware_playback.buffer_bytes_max; |
| 445 |
} else { |
| 446 |
snd_soc_set_runtime_hwparams(substream, &q6asm_dai_hardware_capture); |
| 447 |
runtime->dma_bytes = q6asm_dai_hardware_capture.buffer_bytes_max; |
| 448 |
} |
| 449 |
|
| 450 |
if (pdata->sid < 0) |
| 451 |
prtd->phys = substream->dma_buffer.addr; |
| 452 |
else |
| 453 |
prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32); |
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
static int q6asm_dai_close(struct snd_soc_component *component, |
| 459 |
struct snd_pcm_substream *substream) |
| 460 |
{ |
| 461 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 462 |
struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream); |
| 463 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 464 |
|
| 465 |
if (prtd->audio_client) { |
| 466 |
if (prtd->state == Q6ASM_STREAM_RUNNING) { |
| 467 |
q6asm_cmd(prtd->audio_client, prtd->stream_id, |
| 468 |
CMD_CLOSE); |
| 469 |
q6asm_unmap_memory_regions(substream->stream, |
| 470 |
prtd->audio_client); |
| 471 |
} |
| 472 |
q6asm_audio_client_free(prtd->audio_client); |
| 473 |
prtd->audio_client = NULL; |
| 474 |
} |
| 475 |
q6routing_stream_close(soc_prtd->dai_link->id, |
| 476 |
substream->stream); |
| 477 |
kfree(prtd); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
static snd_pcm_uframes_t q6asm_dai_pointer(struct snd_soc_component *component, |
| 482 |
struct snd_pcm_substream *substream) |
| 483 |
{ |
| 484 |
|
| 485 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 486 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 487 |
snd_pcm_uframes_t ptr; |
| 488 |
|
| 489 |
ptr = q6asm_get_hw_pointer(prtd->audio_client, substream->stream) * runtime->period_size; |
| 490 |
if (ptr) |
| 491 |
return ptr - 1; |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static int q6asm_dai_hw_params(struct snd_soc_component *component, |
| 497 |
struct snd_pcm_substream *substream, |
| 498 |
struct snd_pcm_hw_params *params) |
| 499 |
{ |
| 500 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 501 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 502 |
|
| 503 |
prtd->pcm_size = params_buffer_bytes(params); |
| 504 |
prtd->periods = params_periods(params); |
| 505 |
|
| 506 |
switch (params_format(params)) { |
| 507 |
case SNDRV_PCM_FORMAT_S16_LE: |
| 508 |
prtd->bits_per_sample = 16; |
| 509 |
break; |
| 510 |
case SNDRV_PCM_FORMAT_S24_LE: |
| 511 |
prtd->bits_per_sample = 24; |
| 512 |
break; |
| 513 |
} |
| 514 |
|
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
static void compress_event_handler(uint32_t opcode, uint32_t token, |
| 519 |
void *payload, void *priv) |
| 520 |
{ |
| 521 |
struct q6asm_dai_rtd *prtd = priv; |
| 522 |
struct snd_compr_stream *substream = prtd->cstream; |
| 523 |
u32 wflags = 0; |
| 524 |
uint64_t avail; |
| 525 |
uint32_t bytes_written, bytes_to_write; |
| 526 |
bool is_last_buffer = false; |
| 527 |
|
| 528 |
guard(spinlock_irqsave)(&prtd->lock); |
| 529 |
|
| 530 |
switch (opcode) { |
| 531 |
case ASM_CLIENT_EVENT_CMD_RUN_DONE: |
| 532 |
if (!prtd->bytes_sent) { |
| 533 |
q6asm_stream_remove_initial_silence(prtd->audio_client, |
| 534 |
prtd->stream_id, |
| 535 |
prtd->initial_samples_drop); |
| 536 |
|
| 537 |
q6asm_write_async(prtd->audio_client, prtd->stream_id, |
| 538 |
prtd->pcm_count, 0, 0, 0); |
| 539 |
prtd->bytes_sent += prtd->pcm_count; |
| 540 |
} |
| 541 |
|
| 542 |
break; |
| 543 |
|
| 544 |
case ASM_CLIENT_EVENT_CMD_EOS_DONE: |
| 545 |
if (prtd->notify_on_drain) { |
| 546 |
if (substream->partial_drain) { |
| 547 |
/* |
| 548 |
* Close old stream and make it stale, switch |
| 549 |
* the active stream now! |
| 550 |
*/ |
| 551 |
q6asm_cmd_nowait(prtd->audio_client, |
| 552 |
prtd->stream_id, |
| 553 |
CMD_CLOSE); |
| 554 |
/* |
| 555 |
* vaild stream ids start from 1, So we are |
| 556 |
* toggling this between 1 and 2. |
| 557 |
*/ |
| 558 |
prtd->stream_id = (prtd->stream_id == 1 ? 2 : 1); |
| 559 |
} |
| 560 |
|
| 561 |
snd_compr_drain_notify(prtd->cstream); |
| 562 |
prtd->notify_on_drain = false; |
| 563 |
|
| 564 |
} |
| 565 |
break; |
| 566 |
|
| 567 |
case ASM_CLIENT_EVENT_DATA_WRITE_DONE: |
| 568 |
|
| 569 |
bytes_written = token >> ASM_WRITE_TOKEN_LEN_SHIFT; |
| 570 |
prtd->copied_total += bytes_written; |
| 571 |
snd_compr_fragment_elapsed(substream); |
| 572 |
|
| 573 |
if (prtd->state != Q6ASM_STREAM_RUNNING) |
| 574 |
break; |
| 575 |
|
| 576 |
avail = prtd->bytes_received - prtd->bytes_sent; |
| 577 |
if (avail > prtd->pcm_count) { |
| 578 |
bytes_to_write = prtd->pcm_count; |
| 579 |
} else { |
| 580 |
if (substream->partial_drain || prtd->notify_on_drain) |
| 581 |
is_last_buffer = true; |
| 582 |
bytes_to_write = avail; |
| 583 |
} |
| 584 |
|
| 585 |
if (bytes_to_write) { |
| 586 |
if (substream->partial_drain && is_last_buffer) { |
| 587 |
wflags |= ASM_LAST_BUFFER_FLAG; |
| 588 |
q6asm_stream_remove_trailing_silence(prtd->audio_client, |
| 589 |
prtd->stream_id, |
| 590 |
prtd->trailing_samples_drop); |
| 591 |
} |
| 592 |
|
| 593 |
q6asm_write_async(prtd->audio_client, prtd->stream_id, |
| 594 |
bytes_to_write, 0, 0, wflags); |
| 595 |
|
| 596 |
prtd->bytes_sent += bytes_to_write; |
| 597 |
} |
| 598 |
|
| 599 |
if (prtd->notify_on_drain && is_last_buffer) |
| 600 |
q6asm_cmd_nowait(prtd->audio_client, |
| 601 |
prtd->stream_id, CMD_EOS); |
| 602 |
|
| 603 |
break; |
| 604 |
|
| 605 |
default: |
| 606 |
break; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
static int q6asm_dai_compr_open(struct snd_soc_component *component, |
| 611 |
struct snd_compr_stream *stream) |
| 612 |
{ |
| 613 |
struct snd_soc_pcm_runtime *rtd = stream->private_data; |
| 614 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 615 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 616 |
struct q6asm_dai_data *pdata; |
| 617 |
struct device *dev = component->dev; |
| 618 |
struct q6asm_dai_rtd *prtd; |
| 619 |
int stream_id, size, ret; |
| 620 |
|
| 621 |
stream_id = cpu_dai->driver->id; |
| 622 |
pdata = snd_soc_component_get_drvdata(component); |
| 623 |
if (!pdata) { |
| 624 |
dev_err(dev, "Drv data not found ..\n"); |
| 625 |
return -EINVAL; |
| 626 |
} |
| 627 |
|
| 628 |
prtd = kzalloc_obj(*prtd); |
| 629 |
if (!prtd) |
| 630 |
return -ENOMEM; |
| 631 |
|
| 632 |
/* DSP expects stream id from 1 */ |
| 633 |
prtd->stream_id = 1; |
| 634 |
|
| 635 |
prtd->cstream = stream; |
| 636 |
prtd->audio_client = q6asm_audio_client_alloc(dev, |
| 637 |
(q6asm_cb)compress_event_handler, |
| 638 |
prtd, stream_id, LEGACY_PCM_MODE); |
| 639 |
if (IS_ERR(prtd->audio_client)) { |
| 640 |
dev_err(dev, "Could not allocate memory\n"); |
| 641 |
ret = PTR_ERR(prtd->audio_client); |
| 642 |
goto free_prtd; |
| 643 |
} |
| 644 |
|
| 645 |
size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * |
| 646 |
COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; |
| 647 |
ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, |
| 648 |
&prtd->dma_buffer); |
| 649 |
if (ret) { |
| 650 |
dev_err(dev, "Cannot allocate buffer(s)\n"); |
| 651 |
goto free_client; |
| 652 |
} |
| 653 |
|
| 654 |
if (pdata->sid < 0) |
| 655 |
prtd->phys = prtd->dma_buffer.addr; |
| 656 |
else |
| 657 |
prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32); |
| 658 |
|
| 659 |
snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer); |
| 660 |
spin_lock_init(&prtd->lock); |
| 661 |
runtime->private_data = prtd; |
| 662 |
|
| 663 |
return 0; |
| 664 |
|
| 665 |
free_client: |
| 666 |
q6asm_audio_client_free(prtd->audio_client); |
| 667 |
free_prtd: |
| 668 |
kfree(prtd); |
| 669 |
|
| 670 |
return ret; |
| 671 |
} |
| 672 |
|
| 673 |
static int q6asm_dai_compr_free(struct snd_soc_component *component, |
| 674 |
struct snd_compr_stream *stream) |
| 675 |
{ |
| 676 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 677 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 678 |
struct snd_soc_pcm_runtime *rtd = stream->private_data; |
| 679 |
|
| 680 |
if (prtd->audio_client) { |
| 681 |
if (prtd->state == Q6ASM_STREAM_RUNNING) { |
| 682 |
q6asm_cmd(prtd->audio_client, prtd->stream_id, |
| 683 |
CMD_CLOSE); |
| 684 |
if (prtd->next_track_stream_id) { |
| 685 |
q6asm_cmd(prtd->audio_client, |
| 686 |
prtd->next_track_stream_id, |
| 687 |
CMD_CLOSE); |
| 688 |
} |
| 689 |
|
| 690 |
q6asm_unmap_memory_regions(stream->direction, |
| 691 |
prtd->audio_client); |
| 692 |
} |
| 693 |
snd_dma_free_pages(&prtd->dma_buffer); |
| 694 |
q6asm_audio_client_free(prtd->audio_client); |
| 695 |
prtd->audio_client = NULL; |
| 696 |
} |
| 697 |
q6routing_stream_close(rtd->dai_link->id, stream->direction); |
| 698 |
kfree(prtd); |
| 699 |
|
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
static int __q6asm_dai_compr_set_codec_params(struct snd_soc_component *component, |
| 704 |
struct snd_compr_stream *stream, |
| 705 |
struct snd_codec *codec, |
| 706 |
int stream_id) |
| 707 |
{ |
| 708 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 709 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 710 |
struct q6asm_flac_cfg flac_cfg; |
| 711 |
struct q6asm_wma_cfg wma_cfg; |
| 712 |
struct q6asm_alac_cfg alac_cfg; |
| 713 |
struct q6asm_ape_cfg ape_cfg; |
| 714 |
unsigned int wma_v9 = 0; |
| 715 |
struct device *dev = component->dev; |
| 716 |
int ret; |
| 717 |
union snd_codec_options *codec_options; |
| 718 |
struct snd_dec_flac *flac; |
| 719 |
struct snd_dec_wma *wma; |
| 720 |
struct snd_dec_alac *alac; |
| 721 |
struct snd_dec_ape *ape; |
| 722 |
|
| 723 |
codec_options = &(prtd->codec.options); |
| 724 |
|
| 725 |
memcpy(&prtd->codec, codec, sizeof(*codec)); |
| 726 |
|
| 727 |
switch (codec->id) { |
| 728 |
case SND_AUDIOCODEC_FLAC: |
| 729 |
|
| 730 |
memset(&flac_cfg, 0x0, sizeof(struct q6asm_flac_cfg)); |
| 731 |
flac = &codec_options->flac_d; |
| 732 |
|
| 733 |
flac_cfg.ch_cfg = codec->ch_in; |
| 734 |
flac_cfg.sample_rate = codec->sample_rate; |
| 735 |
flac_cfg.stream_info_present = 1; |
| 736 |
flac_cfg.sample_size = flac->sample_size; |
| 737 |
flac_cfg.min_blk_size = flac->min_blk_size; |
| 738 |
flac_cfg.max_blk_size = flac->max_blk_size; |
| 739 |
flac_cfg.max_frame_size = flac->max_frame_size; |
| 740 |
flac_cfg.min_frame_size = flac->min_frame_size; |
| 741 |
|
| 742 |
ret = q6asm_stream_media_format_block_flac(prtd->audio_client, |
| 743 |
stream_id, |
| 744 |
&flac_cfg); |
| 745 |
if (ret < 0) { |
| 746 |
dev_err(dev, "FLAC CMD Format block failed:%d\n", ret); |
| 747 |
return -EIO; |
| 748 |
} |
| 749 |
break; |
| 750 |
|
| 751 |
case SND_AUDIOCODEC_WMA: |
| 752 |
wma = &codec_options->wma_d; |
| 753 |
|
| 754 |
memset(&wma_cfg, 0x0, sizeof(struct q6asm_wma_cfg)); |
| 755 |
|
| 756 |
wma_cfg.sample_rate = codec->sample_rate; |
| 757 |
wma_cfg.num_channels = codec->ch_in; |
| 758 |
wma_cfg.bytes_per_sec = codec->bit_rate / 8; |
| 759 |
wma_cfg.block_align = codec->align; |
| 760 |
wma_cfg.bits_per_sample = prtd->bits_per_sample; |
| 761 |
wma_cfg.enc_options = wma->encoder_option; |
| 762 |
wma_cfg.adv_enc_options = wma->adv_encoder_option; |
| 763 |
wma_cfg.adv_enc_options2 = wma->adv_encoder_option2; |
| 764 |
|
| 765 |
if (wma_cfg.num_channels == 1) |
| 766 |
wma_cfg.channel_mask = 4; /* Mono Center */ |
| 767 |
else if (wma_cfg.num_channels == 2) |
| 768 |
wma_cfg.channel_mask = 3; /* Stereo FL/FR */ |
| 769 |
else |
| 770 |
return -EINVAL; |
| 771 |
|
| 772 |
/* check the codec profile */ |
| 773 |
switch (codec->profile) { |
| 774 |
case SND_AUDIOPROFILE_WMA9: |
| 775 |
wma_cfg.fmtag = 0x161; |
| 776 |
wma_v9 = 1; |
| 777 |
break; |
| 778 |
|
| 779 |
case SND_AUDIOPROFILE_WMA10: |
| 780 |
wma_cfg.fmtag = 0x166; |
| 781 |
break; |
| 782 |
|
| 783 |
case SND_AUDIOPROFILE_WMA9_PRO: |
| 784 |
wma_cfg.fmtag = 0x162; |
| 785 |
break; |
| 786 |
|
| 787 |
case SND_AUDIOPROFILE_WMA9_LOSSLESS: |
| 788 |
wma_cfg.fmtag = 0x163; |
| 789 |
break; |
| 790 |
|
| 791 |
case SND_AUDIOPROFILE_WMA10_LOSSLESS: |
| 792 |
wma_cfg.fmtag = 0x167; |
| 793 |
break; |
| 794 |
|
| 795 |
default: |
| 796 |
dev_err(dev, "Unknown WMA profile:%x\n", |
| 797 |
codec->profile); |
| 798 |
return -EIO; |
| 799 |
} |
| 800 |
|
| 801 |
if (wma_v9) |
| 802 |
ret = q6asm_stream_media_format_block_wma_v9( |
| 803 |
prtd->audio_client, stream_id, |
| 804 |
&wma_cfg); |
| 805 |
else |
| 806 |
ret = q6asm_stream_media_format_block_wma_v10( |
| 807 |
prtd->audio_client, stream_id, |
| 808 |
&wma_cfg); |
| 809 |
if (ret < 0) { |
| 810 |
dev_err(dev, "WMA9 CMD failed:%d\n", ret); |
| 811 |
return -EIO; |
| 812 |
} |
| 813 |
break; |
| 814 |
|
| 815 |
case SND_AUDIOCODEC_ALAC: |
| 816 |
memset(&alac_cfg, 0x0, sizeof(alac_cfg)); |
| 817 |
alac = &codec_options->alac_d; |
| 818 |
|
| 819 |
alac_cfg.sample_rate = codec->sample_rate; |
| 820 |
alac_cfg.avg_bit_rate = codec->bit_rate; |
| 821 |
alac_cfg.bit_depth = prtd->bits_per_sample; |
| 822 |
alac_cfg.num_channels = codec->ch_in; |
| 823 |
|
| 824 |
alac_cfg.frame_length = alac->frame_length; |
| 825 |
alac_cfg.pb = alac->pb; |
| 826 |
alac_cfg.mb = alac->mb; |
| 827 |
alac_cfg.kb = alac->kb; |
| 828 |
alac_cfg.max_run = alac->max_run; |
| 829 |
alac_cfg.compatible_version = alac->compatible_version; |
| 830 |
alac_cfg.max_frame_bytes = alac->max_frame_bytes; |
| 831 |
|
| 832 |
switch (codec->ch_in) { |
| 833 |
case 1: |
| 834 |
alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_MONO; |
| 835 |
break; |
| 836 |
case 2: |
| 837 |
alac_cfg.channel_layout_tag = ALAC_CH_LAYOUT_STEREO; |
| 838 |
break; |
| 839 |
} |
| 840 |
ret = q6asm_stream_media_format_block_alac(prtd->audio_client, |
| 841 |
stream_id, |
| 842 |
&alac_cfg); |
| 843 |
if (ret < 0) { |
| 844 |
dev_err(dev, "ALAC CMD Format block failed:%d\n", ret); |
| 845 |
return -EIO; |
| 846 |
} |
| 847 |
break; |
| 848 |
|
| 849 |
case SND_AUDIOCODEC_APE: |
| 850 |
memset(&ape_cfg, 0x0, sizeof(ape_cfg)); |
| 851 |
ape = &codec_options->ape_d; |
| 852 |
|
| 853 |
ape_cfg.sample_rate = codec->sample_rate; |
| 854 |
ape_cfg.num_channels = codec->ch_in; |
| 855 |
ape_cfg.bits_per_sample = prtd->bits_per_sample; |
| 856 |
|
| 857 |
ape_cfg.compatible_version = ape->compatible_version; |
| 858 |
ape_cfg.compression_level = ape->compression_level; |
| 859 |
ape_cfg.format_flags = ape->format_flags; |
| 860 |
ape_cfg.blocks_per_frame = ape->blocks_per_frame; |
| 861 |
ape_cfg.final_frame_blocks = ape->final_frame_blocks; |
| 862 |
ape_cfg.total_frames = ape->total_frames; |
| 863 |
ape_cfg.seek_table_present = ape->seek_table_present; |
| 864 |
|
| 865 |
ret = q6asm_stream_media_format_block_ape(prtd->audio_client, |
| 866 |
stream_id, |
| 867 |
&ape_cfg); |
| 868 |
if (ret < 0) { |
| 869 |
dev_err(dev, "APE CMD Format block failed:%d\n", ret); |
| 870 |
return -EIO; |
| 871 |
} |
| 872 |
break; |
| 873 |
|
| 874 |
default: |
| 875 |
break; |
| 876 |
} |
| 877 |
|
| 878 |
return 0; |
| 879 |
} |
| 880 |
|
| 881 |
static int q6asm_dai_compr_set_params(struct snd_soc_component *component, |
| 882 |
struct snd_compr_stream *stream, |
| 883 |
struct snd_compr_params *params) |
| 884 |
{ |
| 885 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 886 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 887 |
struct snd_soc_pcm_runtime *rtd = stream->private_data; |
| 888 |
int dir = stream->direction; |
| 889 |
struct q6asm_dai_data *pdata; |
| 890 |
struct device *dev = component->dev; |
| 891 |
int ret; |
| 892 |
|
| 893 |
pdata = snd_soc_component_get_drvdata(component); |
| 894 |
if (!pdata) |
| 895 |
return -EINVAL; |
| 896 |
|
| 897 |
if (!prtd || !prtd->audio_client) { |
| 898 |
dev_err(dev, "private data null or audio client freed\n"); |
| 899 |
return -EINVAL; |
| 900 |
} |
| 901 |
|
| 902 |
prtd->periods = runtime->fragments; |
| 903 |
prtd->pcm_count = runtime->fragment_size; |
| 904 |
prtd->pcm_size = runtime->fragments * runtime->fragment_size; |
| 905 |
prtd->bits_per_sample = 16; |
| 906 |
|
| 907 |
if (dir == SND_COMPRESS_PLAYBACK) { |
| 908 |
ret = q6asm_open_write(prtd->audio_client, prtd->stream_id, params->codec.id, |
| 909 |
params->codec.profile, prtd->bits_per_sample, |
| 910 |
true); |
| 911 |
|
| 912 |
if (ret < 0) { |
| 913 |
dev_err(dev, "q6asm_open_write failed\n"); |
| 914 |
goto open_err; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
prtd->session_id = q6asm_get_session_id(prtd->audio_client); |
| 919 |
ret = q6routing_stream_open(rtd->dai_link->id, LEGACY_PCM_MODE, |
| 920 |
prtd->session_id, dir); |
| 921 |
if (ret) { |
| 922 |
dev_err(dev, "Stream reg failed ret:%d\n", ret); |
| 923 |
goto routing_err; |
| 924 |
} |
| 925 |
|
| 926 |
ret = __q6asm_dai_compr_set_codec_params(component, stream, |
| 927 |
¶ms->codec, |
| 928 |
prtd->stream_id); |
| 929 |
if (ret) { |
| 930 |
dev_err(dev, "codec param setup failed ret:%d\n", ret); |
| 931 |
goto q6_err; |
| 932 |
} |
| 933 |
|
| 934 |
ret = q6asm_map_memory_regions(dir, prtd->audio_client, prtd->phys, |
| 935 |
(prtd->pcm_size / prtd->periods), |
| 936 |
prtd->periods); |
| 937 |
|
| 938 |
if (ret < 0) { |
| 939 |
dev_err(dev, "Buffer Mapping failed ret:%d\n", ret); |
| 940 |
ret = -ENOMEM; |
| 941 |
goto q6_err; |
| 942 |
} |
| 943 |
|
| 944 |
prtd->state = Q6ASM_STREAM_RUNNING; |
| 945 |
|
| 946 |
return 0; |
| 947 |
|
| 948 |
q6_err: |
| 949 |
q6routing_stream_close(rtd->dai_link->id, dir); |
| 950 |
routing_err: |
| 951 |
q6asm_cmd(prtd->audio_client, prtd->stream_id, CMD_CLOSE); |
| 952 |
|
| 953 |
open_err: |
| 954 |
return ret; |
| 955 |
} |
| 956 |
|
| 957 |
static int q6asm_dai_compr_set_metadata(struct snd_soc_component *component, |
| 958 |
struct snd_compr_stream *stream, |
| 959 |
struct snd_compr_metadata *metadata) |
| 960 |
{ |
| 961 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 962 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 963 |
int ret = 0; |
| 964 |
|
| 965 |
switch (metadata->key) { |
| 966 |
case SNDRV_COMPRESS_ENCODER_PADDING: |
| 967 |
prtd->trailing_samples_drop = metadata->value[0]; |
| 968 |
break; |
| 969 |
case SNDRV_COMPRESS_ENCODER_DELAY: |
| 970 |
prtd->initial_samples_drop = metadata->value[0]; |
| 971 |
if (prtd->next_track_stream_id) { |
| 972 |
ret = q6asm_open_write(prtd->audio_client, |
| 973 |
prtd->next_track_stream_id, |
| 974 |
prtd->codec.id, |
| 975 |
prtd->codec.profile, |
| 976 |
prtd->bits_per_sample, |
| 977 |
true); |
| 978 |
if (ret < 0) { |
| 979 |
dev_err(component->dev, "q6asm_open_write failed\n"); |
| 980 |
return ret; |
| 981 |
} |
| 982 |
ret = __q6asm_dai_compr_set_codec_params(component, stream, |
| 983 |
&prtd->codec, |
| 984 |
prtd->next_track_stream_id); |
| 985 |
if (ret < 0) { |
| 986 |
dev_err(component->dev, "q6asm_open_write failed\n"); |
| 987 |
return ret; |
| 988 |
} |
| 989 |
|
| 990 |
ret = q6asm_stream_remove_initial_silence(prtd->audio_client, |
| 991 |
prtd->next_track_stream_id, |
| 992 |
prtd->initial_samples_drop); |
| 993 |
prtd->next_track_stream_id = 0; |
| 994 |
|
| 995 |
} |
| 996 |
|
| 997 |
break; |
| 998 |
default: |
| 999 |
ret = -EINVAL; |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
|
| 1003 |
return ret; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static int q6asm_dai_compr_trigger(struct snd_soc_component *component, |
| 1007 |
struct snd_compr_stream *stream, int cmd) |
| 1008 |
{ |
| 1009 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 1010 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 1011 |
int ret = 0; |
| 1012 |
|
| 1013 |
switch (cmd) { |
| 1014 |
case SNDRV_PCM_TRIGGER_START: |
| 1015 |
case SNDRV_PCM_TRIGGER_RESUME: |
| 1016 |
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1017 |
ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id, |
| 1018 |
0, 0, 0); |
| 1019 |
break; |
| 1020 |
case SNDRV_PCM_TRIGGER_STOP: |
| 1021 |
ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id, |
| 1022 |
CMD_EOS); |
| 1023 |
break; |
| 1024 |
case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1025 |
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1026 |
ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id, |
| 1027 |
CMD_PAUSE); |
| 1028 |
break; |
| 1029 |
case SND_COMPR_TRIGGER_NEXT_TRACK: |
| 1030 |
prtd->next_track = true; |
| 1031 |
prtd->next_track_stream_id = (prtd->stream_id == 1 ? 2 : 1); |
| 1032 |
break; |
| 1033 |
case SND_COMPR_TRIGGER_DRAIN: |
| 1034 |
case SND_COMPR_TRIGGER_PARTIAL_DRAIN: |
| 1035 |
prtd->notify_on_drain = true; |
| 1036 |
break; |
| 1037 |
default: |
| 1038 |
ret = -EINVAL; |
| 1039 |
break; |
| 1040 |
} |
| 1041 |
|
| 1042 |
return ret; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int q6asm_dai_compr_pointer(struct snd_soc_component *component, |
| 1046 |
struct snd_compr_stream *stream, |
| 1047 |
struct snd_compr_tstamp64 *tstamp) |
| 1048 |
{ |
| 1049 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 1050 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 1051 |
uint64_t temp_copied_total; |
| 1052 |
|
| 1053 |
guard(spinlock_irqsave)(&prtd->lock); |
| 1054 |
|
| 1055 |
tstamp->copied_total = prtd->copied_total; |
| 1056 |
temp_copied_total = tstamp->copied_total; |
| 1057 |
tstamp->byte_offset = do_div(temp_copied_total, prtd->pcm_size); |
| 1058 |
|
| 1059 |
return 0; |
| 1060 |
} |
| 1061 |
|
| 1062 |
static int q6asm_compr_copy(struct snd_soc_component *component, |
| 1063 |
struct snd_compr_stream *stream, char __user *buf, |
| 1064 |
size_t count) |
| 1065 |
{ |
| 1066 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 1067 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 1068 |
u32 wflags = 0; |
| 1069 |
uint64_t avail, bytes_in_flight = 0; |
| 1070 |
void *dstn; |
| 1071 |
size_t copy; |
| 1072 |
u32 app_pointer; |
| 1073 |
uint64_t bytes_received; |
| 1074 |
uint64_t temp_bytes_received; |
| 1075 |
|
| 1076 |
bytes_received = prtd->bytes_received; |
| 1077 |
temp_bytes_received = bytes_received; |
| 1078 |
|
| 1079 |
/** |
| 1080 |
* Make sure that next track data pointer is aligned at 32 bit boundary |
| 1081 |
* This is a Mandatory requirement from DSP data buffers alignment |
| 1082 |
*/ |
| 1083 |
if (prtd->next_track) { |
| 1084 |
bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count); |
| 1085 |
temp_bytes_received = bytes_received; |
| 1086 |
} |
| 1087 |
|
| 1088 |
app_pointer = do_div(temp_bytes_received, prtd->pcm_size); |
| 1089 |
dstn = prtd->dma_buffer.area + app_pointer; |
| 1090 |
|
| 1091 |
if (count < prtd->pcm_size - app_pointer) { |
| 1092 |
if (copy_from_user(dstn, buf, count)) |
| 1093 |
return -EFAULT; |
| 1094 |
} else { |
| 1095 |
copy = prtd->pcm_size - app_pointer; |
| 1096 |
if (copy_from_user(dstn, buf, copy)) |
| 1097 |
return -EFAULT; |
| 1098 |
if (copy_from_user(prtd->dma_buffer.area, buf + copy, |
| 1099 |
count - copy)) |
| 1100 |
return -EFAULT; |
| 1101 |
} |
| 1102 |
|
| 1103 |
guard(spinlock_irqsave)(&prtd->lock); |
| 1104 |
|
| 1105 |
bytes_in_flight = prtd->bytes_received - prtd->copied_total; |
| 1106 |
|
| 1107 |
if (prtd->next_track) { |
| 1108 |
prtd->next_track = false; |
| 1109 |
prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count); |
| 1110 |
prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count); |
| 1111 |
} |
| 1112 |
|
| 1113 |
prtd->bytes_received = bytes_received + count; |
| 1114 |
|
| 1115 |
/* Kick off the data to dsp if its starving!! */ |
| 1116 |
if (prtd->state == Q6ASM_STREAM_RUNNING && (bytes_in_flight == 0)) { |
| 1117 |
uint32_t bytes_to_write = prtd->pcm_count; |
| 1118 |
|
| 1119 |
avail = prtd->bytes_received - prtd->bytes_sent; |
| 1120 |
|
| 1121 |
if (avail < prtd->pcm_count) |
| 1122 |
bytes_to_write = avail; |
| 1123 |
|
| 1124 |
q6asm_write_async(prtd->audio_client, prtd->stream_id, |
| 1125 |
bytes_to_write, 0, 0, wflags); |
| 1126 |
prtd->bytes_sent += bytes_to_write; |
| 1127 |
} |
| 1128 |
|
| 1129 |
return count; |
| 1130 |
} |
| 1131 |
|
| 1132 |
static int q6asm_dai_compr_mmap(struct snd_soc_component *component, |
| 1133 |
struct snd_compr_stream *stream, |
| 1134 |
struct vm_area_struct *vma) |
| 1135 |
{ |
| 1136 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 1137 |
struct q6asm_dai_rtd *prtd = runtime->private_data; |
| 1138 |
struct device *dev = component->dev; |
| 1139 |
|
| 1140 |
return dma_mmap_coherent(dev, vma, |
| 1141 |
prtd->dma_buffer.area, prtd->dma_buffer.addr, |
| 1142 |
prtd->dma_buffer.bytes); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static int q6asm_dai_compr_get_caps(struct snd_soc_component *component, |
| 1146 |
struct snd_compr_stream *stream, |
| 1147 |
struct snd_compr_caps *caps) |
| 1148 |
{ |
| 1149 |
caps->direction = SND_COMPRESS_PLAYBACK; |
| 1150 |
caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE; |
| 1151 |
caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE; |
| 1152 |
caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS; |
| 1153 |
caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; |
| 1154 |
caps->num_codecs = 5; |
| 1155 |
caps->codecs[0] = SND_AUDIOCODEC_MP3; |
| 1156 |
caps->codecs[1] = SND_AUDIOCODEC_FLAC; |
| 1157 |
caps->codecs[2] = SND_AUDIOCODEC_WMA; |
| 1158 |
caps->codecs[3] = SND_AUDIOCODEC_ALAC; |
| 1159 |
caps->codecs[4] = SND_AUDIOCODEC_APE; |
| 1160 |
|
| 1161 |
return 0; |
| 1162 |
} |
| 1163 |
|
| 1164 |
static int q6asm_dai_compr_get_codec_caps(struct snd_soc_component *component, |
| 1165 |
struct snd_compr_stream *stream, |
| 1166 |
struct snd_compr_codec_caps *codec) |
| 1167 |
{ |
| 1168 |
switch (codec->codec) { |
| 1169 |
case SND_AUDIOCODEC_MP3: |
| 1170 |
*codec = q6asm_compr_caps; |
| 1171 |
break; |
| 1172 |
default: |
| 1173 |
break; |
| 1174 |
} |
| 1175 |
|
| 1176 |
return 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static const struct snd_compress_ops q6asm_dai_compress_ops = { |
| 1180 |
.open = q6asm_dai_compr_open, |
| 1181 |
.free = q6asm_dai_compr_free, |
| 1182 |
.set_params = q6asm_dai_compr_set_params, |
| 1183 |
.set_metadata = q6asm_dai_compr_set_metadata, |
| 1184 |
.pointer = q6asm_dai_compr_pointer, |
| 1185 |
.trigger = q6asm_dai_compr_trigger, |
| 1186 |
.get_caps = q6asm_dai_compr_get_caps, |
| 1187 |
.get_codec_caps = q6asm_dai_compr_get_codec_caps, |
| 1188 |
.mmap = q6asm_dai_compr_mmap, |
| 1189 |
.copy = q6asm_compr_copy, |
| 1190 |
}; |
| 1191 |
|
| 1192 |
static int q6asm_dai_pcm_new(struct snd_soc_component *component, |
| 1193 |
struct snd_soc_pcm_runtime *rtd) |
| 1194 |
{ |
| 1195 |
struct snd_pcm *pcm = rtd->pcm; |
| 1196 |
size_t size = q6asm_dai_hardware_playback.buffer_bytes_max; |
| 1197 |
|
| 1198 |
return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, |
| 1199 |
component->dev, size); |
| 1200 |
} |
| 1201 |
|
| 1202 |
static const struct snd_soc_dapm_widget q6asm_dapm_widgets[] = { |
| 1203 |
SND_SOC_DAPM_AIF_IN("MM_DL1", "MultiMedia1 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1204 |
SND_SOC_DAPM_AIF_IN("MM_DL2", "MultiMedia2 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1205 |
SND_SOC_DAPM_AIF_IN("MM_DL3", "MultiMedia3 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1206 |
SND_SOC_DAPM_AIF_IN("MM_DL4", "MultiMedia4 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1207 |
SND_SOC_DAPM_AIF_IN("MM_DL5", "MultiMedia5 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1208 |
SND_SOC_DAPM_AIF_IN("MM_DL6", "MultiMedia6 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1209 |
SND_SOC_DAPM_AIF_IN("MM_DL7", "MultiMedia7 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1210 |
SND_SOC_DAPM_AIF_IN("MM_DL8", "MultiMedia8 Playback", 0, SND_SOC_NOPM, 0, 0), |
| 1211 |
SND_SOC_DAPM_AIF_OUT("MM_UL1", "MultiMedia1 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1212 |
SND_SOC_DAPM_AIF_OUT("MM_UL2", "MultiMedia2 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1213 |
SND_SOC_DAPM_AIF_OUT("MM_UL3", "MultiMedia3 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1214 |
SND_SOC_DAPM_AIF_OUT("MM_UL4", "MultiMedia4 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1215 |
SND_SOC_DAPM_AIF_OUT("MM_UL5", "MultiMedia5 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1216 |
SND_SOC_DAPM_AIF_OUT("MM_UL6", "MultiMedia6 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1217 |
SND_SOC_DAPM_AIF_OUT("MM_UL7", "MultiMedia7 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1218 |
SND_SOC_DAPM_AIF_OUT("MM_UL8", "MultiMedia8 Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1219 |
}; |
| 1220 |
|
| 1221 |
static const struct snd_soc_component_driver q6asm_fe_dai_component = { |
| 1222 |
.name = DRV_NAME, |
| 1223 |
.open = q6asm_dai_open, |
| 1224 |
.hw_params = q6asm_dai_hw_params, |
| 1225 |
.close = q6asm_dai_close, |
| 1226 |
.prepare = q6asm_dai_prepare, |
| 1227 |
.trigger = q6asm_dai_trigger, |
| 1228 |
.ack = q6asm_dai_ack, |
| 1229 |
.pointer = q6asm_dai_pointer, |
| 1230 |
.pcm_construct = q6asm_dai_pcm_new, |
| 1231 |
.compress_ops = &q6asm_dai_compress_ops, |
| 1232 |
.dapm_widgets = q6asm_dapm_widgets, |
| 1233 |
.num_dapm_widgets = ARRAY_SIZE(q6asm_dapm_widgets), |
| 1234 |
.legacy_dai_naming = 1, |
| 1235 |
}; |
| 1236 |
|
| 1237 |
static struct snd_soc_dai_driver q6asm_fe_dais_template[] = { |
| 1238 |
Q6ASM_FEDAI_DRIVER(1), |
| 1239 |
Q6ASM_FEDAI_DRIVER(2), |
| 1240 |
Q6ASM_FEDAI_DRIVER(3), |
| 1241 |
Q6ASM_FEDAI_DRIVER(4), |
| 1242 |
Q6ASM_FEDAI_DRIVER(5), |
| 1243 |
Q6ASM_FEDAI_DRIVER(6), |
| 1244 |
Q6ASM_FEDAI_DRIVER(7), |
| 1245 |
Q6ASM_FEDAI_DRIVER(8), |
| 1246 |
}; |
| 1247 |
|
| 1248 |
static const struct snd_soc_dai_ops q6asm_dai_ops = { |
| 1249 |
.compress_new = snd_soc_new_compress, |
| 1250 |
}; |
| 1251 |
|
| 1252 |
static int of_q6asm_parse_dai_data(struct device *dev, |
| 1253 |
struct q6asm_dai_data *pdata) |
| 1254 |
{ |
| 1255 |
struct snd_soc_dai_driver *dai_drv; |
| 1256 |
struct snd_soc_pcm_stream empty_stream; |
| 1257 |
struct device_node *node; |
| 1258 |
int ret, id, dir, idx = 0; |
| 1259 |
|
| 1260 |
|
| 1261 |
pdata->num_dais = of_get_child_count(dev->of_node); |
| 1262 |
if (!pdata->num_dais) { |
| 1263 |
dev_err(dev, "No dais found in DT\n"); |
| 1264 |
return -EINVAL; |
| 1265 |
} |
| 1266 |
|
| 1267 |
pdata->dais = devm_kcalloc(dev, pdata->num_dais, sizeof(*dai_drv), |
| 1268 |
GFP_KERNEL); |
| 1269 |
if (!pdata->dais) |
| 1270 |
return -ENOMEM; |
| 1271 |
|
| 1272 |
memset(&empty_stream, 0, sizeof(empty_stream)); |
| 1273 |
|
| 1274 |
for_each_child_of_node(dev->of_node, node) { |
| 1275 |
ret = of_property_read_u32(node, "reg", &id); |
| 1276 |
if (ret || id >= MAX_SESSIONS || id < 0) { |
| 1277 |
dev_err(dev, "valid dai id not found:%d\n", ret); |
| 1278 |
continue; |
| 1279 |
} |
| 1280 |
|
| 1281 |
dai_drv = &pdata->dais[idx++]; |
| 1282 |
*dai_drv = q6asm_fe_dais_template[id]; |
| 1283 |
|
| 1284 |
ret = of_property_read_u32(node, "direction", &dir); |
| 1285 |
if (ret) |
| 1286 |
continue; |
| 1287 |
|
| 1288 |
if (dir == Q6ASM_DAI_RX) |
| 1289 |
dai_drv->capture = empty_stream; |
| 1290 |
else if (dir == Q6ASM_DAI_TX) |
| 1291 |
dai_drv->playback = empty_stream; |
| 1292 |
|
| 1293 |
if (of_property_read_bool(node, "is-compress-dai")) |
| 1294 |
dai_drv->ops = &q6asm_dai_ops; |
| 1295 |
} |
| 1296 |
|
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
static int q6asm_dai_probe(struct platform_device *pdev) |
| 1301 |
{ |
| 1302 |
struct device *dev = &pdev->dev; |
| 1303 |
struct device_node *node = dev->of_node; |
| 1304 |
struct of_phandle_args args; |
| 1305 |
struct q6asm_dai_data *pdata; |
| 1306 |
int rc; |
| 1307 |
|
| 1308 |
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 1309 |
if (!pdata) |
| 1310 |
return -ENOMEM; |
| 1311 |
|
| 1312 |
rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args); |
| 1313 |
if (rc < 0) |
| 1314 |
pdata->sid = -1; |
| 1315 |
else |
| 1316 |
pdata->sid = args.args[0] & SID_MASK_DEFAULT; |
| 1317 |
|
| 1318 |
dev_set_drvdata(dev, pdata); |
| 1319 |
|
| 1320 |
rc = of_q6asm_parse_dai_data(dev, pdata); |
| 1321 |
if (rc) |
| 1322 |
return rc; |
| 1323 |
|
| 1324 |
return devm_snd_soc_register_component(dev, &q6asm_fe_dai_component, |
| 1325 |
pdata->dais, pdata->num_dais); |
| 1326 |
} |
| 1327 |
|
| 1328 |
#ifdef CONFIG_OF |
| 1329 |
static const struct of_device_id q6asm_dai_device_id[] = { |
| 1330 |
{ .compatible = "qcom,q6asm-dais" }, |
| 1331 |
{}, |
| 1332 |
}; |
| 1333 |
MODULE_DEVICE_TABLE(of, q6asm_dai_device_id); |
| 1334 |
#endif |
| 1335 |
|
| 1336 |
static struct platform_driver q6asm_dai_platform_driver = { |
| 1337 |
.driver = { |
| 1338 |
.name = "q6asm-dai", |
| 1339 |
.of_match_table = of_match_ptr(q6asm_dai_device_id), |
| 1340 |
}, |
| 1341 |
.probe = q6asm_dai_probe, |
| 1342 |
}; |
| 1343 |
module_platform_driver(q6asm_dai_platform_driver); |
| 1344 |
|
| 1345 |
MODULE_DESCRIPTION("Q6ASM dai driver"); |
| 1346 |
MODULE_LICENSE("GPL v2"); |