| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// Copyright (c) 2021, Linaro Limited |
| 3 |
|
| 4 |
#include <linux/init.h> |
| 5 |
#include <linux/err.h> |
| 6 |
#include <linux/module.h> |
| 7 |
#include <linux/of.h> |
| 8 |
#include <linux/platform_device.h> |
| 9 |
#include <linux/slab.h> |
| 10 |
#include <sound/soc.h> |
| 11 |
#include <sound/soc-dapm.h> |
| 12 |
#include <linux/spinlock.h> |
| 13 |
#include <sound/pcm.h> |
| 14 |
#include <asm/div64.h> |
| 15 |
#include <asm/dma.h> |
| 16 |
#include <linux/dma-mapping.h> |
| 17 |
#include <sound/pcm_params.h> |
| 18 |
#include "q6apm.h" |
| 19 |
|
| 20 |
#define DRV_NAME "q6apm-dai" |
| 21 |
|
| 22 |
#define PLAYBACK_MIN_NUM_PERIODS 2 |
| 23 |
#define PLAYBACK_MAX_NUM_PERIODS 8 |
| 24 |
#define PLAYBACK_MAX_PERIOD_SIZE 65536 |
| 25 |
#define PLAYBACK_MIN_PERIOD_SIZE 128 |
| 26 |
#define CAPTURE_MIN_NUM_PERIODS 2 |
| 27 |
#define CAPTURE_MAX_NUM_PERIODS 8 |
| 28 |
#define CAPTURE_MAX_PERIOD_SIZE 65536 |
| 29 |
#define CAPTURE_MIN_PERIOD_SIZE 6144 |
| 30 |
#define BUFFER_BYTES_MAX (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE) |
| 31 |
#define BUFFER_BYTES_MIN (PLAYBACK_MIN_NUM_PERIODS * PLAYBACK_MIN_PERIOD_SIZE) |
| 32 |
#define COMPR_PLAYBACK_MAX_FRAGMENT_SIZE (128 * 1024) |
| 33 |
#define COMPR_PLAYBACK_MAX_NUM_FRAGMENTS (16 * 4) |
| 34 |
#define COMPR_PLAYBACK_MIN_FRAGMENT_SIZE (8 * 1024) |
| 35 |
#define COMPR_PLAYBACK_MIN_NUM_FRAGMENTS (4) |
| 36 |
#define SID_MASK_DEFAULT 0xF |
| 37 |
|
| 38 |
static const struct snd_compr_codec_caps q6apm_compr_caps = { |
| 39 |
.num_descriptors = 1, |
| 40 |
.descriptor[0].max_ch = 2, |
| 41 |
.descriptor[0].sample_rates = { 8000, 11025, 12000, 16000, 22050, |
| 42 |
24000, 32000, 44100, 48000, 88200, |
| 43 |
96000, 176400, 192000 }, |
| 44 |
.descriptor[0].num_sample_rates = 13, |
| 45 |
.descriptor[0].bit_rate[0] = 320, |
| 46 |
.descriptor[0].bit_rate[1] = 128, |
| 47 |
.descriptor[0].num_bitrates = 2, |
| 48 |
.descriptor[0].profiles = 0, |
| 49 |
.descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO, |
| 50 |
.descriptor[0].formats = 0, |
| 51 |
}; |
| 52 |
|
| 53 |
enum stream_state { |
| 54 |
Q6APM_STREAM_IDLE = 0, |
| 55 |
Q6APM_STREAM_STOPPED, |
| 56 |
Q6APM_STREAM_RUNNING, |
| 57 |
}; |
| 58 |
|
| 59 |
struct q6apm_dai_rtd { |
| 60 |
struct snd_pcm_substream *substream; |
| 61 |
struct snd_compr_stream *cstream; |
| 62 |
struct snd_codec codec; |
| 63 |
struct snd_compr_params codec_param; |
| 64 |
struct snd_dma_buffer dma_buffer; |
| 65 |
phys_addr_t phys; |
| 66 |
unsigned int pcm_size; |
| 67 |
unsigned int pcm_count; |
| 68 |
unsigned int periods; |
| 69 |
uint64_t bytes_sent; |
| 70 |
uint64_t bytes_received; |
| 71 |
uint64_t copied_total; |
| 72 |
uint16_t bits_per_sample; |
| 73 |
snd_pcm_uframes_t queue_ptr; |
| 74 |
bool next_track; |
| 75 |
enum stream_state state; |
| 76 |
struct q6apm_graph *graph; |
| 77 |
spinlock_t lock; |
| 78 |
bool notify_on_drain; |
| 79 |
}; |
| 80 |
|
| 81 |
struct q6apm_dai_data { |
| 82 |
long long sid; |
| 83 |
}; |
| 84 |
|
| 85 |
static const struct snd_pcm_hardware q6apm_dai_hardware_capture = { |
| 86 |
.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 87 |
SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED | |
| 88 |
SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME | |
| 89 |
SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | |
| 90 |
SNDRV_PCM_INFO_BATCH), |
| 91 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE), |
| 92 |
.rates = SNDRV_PCM_RATE_8000_48000, |
| 93 |
.rate_min = 8000, |
| 94 |
.rate_max = 48000, |
| 95 |
.channels_min = 2, |
| 96 |
.channels_max = 4, |
| 97 |
.buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE, |
| 98 |
.period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, |
| 99 |
.period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, |
| 100 |
.periods_min = CAPTURE_MIN_NUM_PERIODS, |
| 101 |
.periods_max = CAPTURE_MAX_NUM_PERIODS, |
| 102 |
.fifo_size = 0, |
| 103 |
}; |
| 104 |
|
| 105 |
static const struct snd_pcm_hardware q6apm_dai_hardware_playback = { |
| 106 |
.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 107 |
SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_INTERLEAVED | |
| 108 |
SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME | |
| 109 |
SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR | |
| 110 |
SNDRV_PCM_INFO_BATCH), |
| 111 |
.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE), |
| 112 |
.rates = SNDRV_PCM_RATE_8000_192000, |
| 113 |
.rate_min = 8000, |
| 114 |
.rate_max = 192000, |
| 115 |
.channels_min = 2, |
| 116 |
.channels_max = 8, |
| 117 |
.buffer_bytes_max = (PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE), |
| 118 |
.period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, |
| 119 |
.period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, |
| 120 |
.periods_min = PLAYBACK_MIN_NUM_PERIODS, |
| 121 |
.periods_max = PLAYBACK_MAX_NUM_PERIODS, |
| 122 |
.fifo_size = 0, |
| 123 |
}; |
| 124 |
|
| 125 |
static void event_handler(uint32_t opcode, uint32_t token, void *payload, void *priv) |
| 126 |
{ |
| 127 |
struct q6apm_dai_rtd *prtd = priv; |
| 128 |
struct snd_pcm_substream *substream = prtd->substream; |
| 129 |
|
| 130 |
switch (opcode) { |
| 131 |
case APM_CLIENT_EVENT_CMD_EOS_DONE: |
| 132 |
prtd->state = Q6APM_STREAM_STOPPED; |
| 133 |
break; |
| 134 |
case APM_CLIENT_EVENT_DATA_WRITE_DONE: |
| 135 |
snd_pcm_period_elapsed(substream); |
| 136 |
|
| 137 |
break; |
| 138 |
case APM_CLIENT_EVENT_DATA_READ_DONE: |
| 139 |
snd_pcm_period_elapsed(substream); |
| 140 |
if (prtd->state == Q6APM_STREAM_RUNNING) |
| 141 |
q6apm_read(prtd->graph); |
| 142 |
|
| 143 |
break; |
| 144 |
default: |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static void event_handler_compr(uint32_t opcode, uint32_t token, |
| 150 |
void *payload, void *priv) |
| 151 |
{ |
| 152 |
struct q6apm_dai_rtd *prtd = priv; |
| 153 |
struct snd_compr_stream *substream = prtd->cstream; |
| 154 |
uint32_t wflags = 0; |
| 155 |
uint64_t avail; |
| 156 |
uint32_t bytes_written, bytes_to_write; |
| 157 |
bool is_last_buffer = false; |
| 158 |
|
| 159 |
guard(spinlock_irqsave)(&prtd->lock); |
| 160 |
switch (opcode) { |
| 161 |
case APM_CLIENT_EVENT_CMD_EOS_DONE: |
| 162 |
if (prtd->notify_on_drain) { |
| 163 |
snd_compr_drain_notify(prtd->cstream); |
| 164 |
prtd->notify_on_drain = false; |
| 165 |
} else { |
| 166 |
prtd->state = Q6APM_STREAM_STOPPED; |
| 167 |
} |
| 168 |
break; |
| 169 |
case APM_CLIENT_EVENT_DATA_WRITE_DONE: |
| 170 |
bytes_written = token >> APM_WRITE_TOKEN_LEN_SHIFT; |
| 171 |
prtd->copied_total += bytes_written; |
| 172 |
snd_compr_fragment_elapsed(substream); |
| 173 |
|
| 174 |
if (prtd->state != Q6APM_STREAM_RUNNING) |
| 175 |
break; |
| 176 |
|
| 177 |
avail = prtd->bytes_received - prtd->bytes_sent; |
| 178 |
|
| 179 |
if (avail > prtd->pcm_count) { |
| 180 |
bytes_to_write = prtd->pcm_count; |
| 181 |
} else { |
| 182 |
if (substream->partial_drain || prtd->notify_on_drain) |
| 183 |
is_last_buffer = true; |
| 184 |
bytes_to_write = avail; |
| 185 |
} |
| 186 |
|
| 187 |
if (bytes_to_write) { |
| 188 |
if (substream->partial_drain && is_last_buffer) |
| 189 |
wflags |= APM_LAST_BUFFER_FLAG; |
| 190 |
|
| 191 |
q6apm_write_async(prtd->graph, |
| 192 |
bytes_to_write, 0, 0, wflags); |
| 193 |
|
| 194 |
prtd->bytes_sent += bytes_to_write; |
| 195 |
|
| 196 |
if (prtd->notify_on_drain && is_last_buffer) |
| 197 |
audioreach_shared_memory_send_eos(prtd->graph); |
| 198 |
} |
| 199 |
|
| 200 |
break; |
| 201 |
default: |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
static int q6apm_dai_prepare(struct snd_soc_component *component, |
| 207 |
struct snd_pcm_substream *substream) |
| 208 |
{ |
| 209 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 210 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 211 |
struct audioreach_module_config cfg; |
| 212 |
struct device *dev = component->dev; |
| 213 |
struct q6apm_dai_data *pdata; |
| 214 |
int ret; |
| 215 |
|
| 216 |
pdata = snd_soc_component_get_drvdata(component); |
| 217 |
if (!pdata) |
| 218 |
return -EINVAL; |
| 219 |
|
| 220 |
if (!prtd || !prtd->graph) { |
| 221 |
dev_err(dev, "%s: private data null or audio client freed\n", __func__); |
| 222 |
return -EINVAL; |
| 223 |
} |
| 224 |
|
| 225 |
cfg.direction = substream->stream; |
| 226 |
cfg.sample_rate = runtime->rate; |
| 227 |
cfg.num_channels = runtime->channels; |
| 228 |
cfg.bit_width = prtd->bits_per_sample; |
| 229 |
cfg.fmt = SND_AUDIOCODEC_PCM; |
| 230 |
audioreach_set_default_channel_mapping(cfg.channel_map, runtime->channels); |
| 231 |
|
| 232 |
if (prtd->state) { |
| 233 |
/* clear the previous setup if any */ |
| 234 |
q6apm_graph_stop(prtd->graph); |
| 235 |
q6apm_unmap_memory_regions(prtd->graph, substream->stream); |
| 236 |
} |
| 237 |
|
| 238 |
prtd->pcm_count = snd_pcm_lib_period_bytes(substream); |
| 239 |
/* rate and channels are sent to audio driver */ |
| 240 |
ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg); |
| 241 |
if (ret < 0) { |
| 242 |
dev_err(dev, "%s: q6apm_open_write failed\n", __func__); |
| 243 |
return ret; |
| 244 |
} |
| 245 |
|
| 246 |
ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg); |
| 247 |
if (ret < 0) |
| 248 |
dev_err(dev, "%s: CMD Format block failed\n", __func__); |
| 249 |
|
| 250 |
ret = q6apm_map_memory_regions(prtd->graph, substream->stream, prtd->phys, |
| 251 |
(prtd->pcm_size / prtd->periods), prtd->periods); |
| 252 |
|
| 253 |
if (ret < 0) { |
| 254 |
dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret); |
| 255 |
return -ENOMEM; |
| 256 |
} |
| 257 |
|
| 258 |
ret = q6apm_graph_prepare(prtd->graph); |
| 259 |
if (ret) { |
| 260 |
dev_err(dev, "Failed to prepare Graph %d\n", ret); |
| 261 |
return ret; |
| 262 |
} |
| 263 |
|
| 264 |
ret = q6apm_graph_start(prtd->graph); |
| 265 |
if (ret) { |
| 266 |
dev_err(dev, "Failed to Start Graph %d\n", ret); |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 271 |
int i; |
| 272 |
/* Queue the buffers for Capture ONLY after graph is started */ |
| 273 |
for (i = 0; i < runtime->periods; i++) |
| 274 |
q6apm_read(prtd->graph); |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
/* Now that graph as been prepared and started update the internal state accordingly */ |
| 279 |
prtd->state = Q6APM_STREAM_RUNNING; |
| 280 |
|
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream) |
| 285 |
{ |
| 286 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 287 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 288 |
int i, ret = 0, avail_periods; |
| 289 |
|
| 290 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 291 |
avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size; |
| 292 |
for (i = 0; i < avail_periods; i++) { |
| 293 |
ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP); |
| 294 |
if (ret < 0) { |
| 295 |
dev_err(component->dev, "Error queuing playback buffer %d\n", ret); |
| 296 |
return ret; |
| 297 |
} |
| 298 |
prtd->queue_ptr += runtime->period_size; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return ret; |
| 303 |
} |
| 304 |
|
| 305 |
static int q6apm_dai_trigger(struct snd_soc_component *component, |
| 306 |
struct snd_pcm_substream *substream, int cmd) |
| 307 |
{ |
| 308 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 309 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 310 |
int ret = 0; |
| 311 |
|
| 312 |
switch (cmd) { |
| 313 |
case SNDRV_PCM_TRIGGER_START: |
| 314 |
case SNDRV_PCM_TRIGGER_RESUME: |
| 315 |
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 316 |
break; |
| 317 |
case SNDRV_PCM_TRIGGER_STOP: |
| 318 |
/* TODO support be handled via SoftPause Module */ |
| 319 |
prtd->state = Q6APM_STREAM_STOPPED; |
| 320 |
prtd->queue_ptr = 0; |
| 321 |
break; |
| 322 |
case SNDRV_PCM_TRIGGER_SUSPEND: |
| 323 |
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 324 |
break; |
| 325 |
default: |
| 326 |
ret = -EINVAL; |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
return ret; |
| 331 |
} |
| 332 |
|
| 333 |
static int q6apm_dai_open(struct snd_soc_component *component, |
| 334 |
struct snd_pcm_substream *substream) |
| 335 |
{ |
| 336 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 337 |
struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream); |
| 338 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0); |
| 339 |
struct device *dev = component->dev; |
| 340 |
struct q6apm_dai_data *pdata; |
| 341 |
struct q6apm_dai_rtd *prtd; |
| 342 |
int graph_id, ret; |
| 343 |
|
| 344 |
graph_id = cpu_dai->driver->id; |
| 345 |
|
| 346 |
pdata = snd_soc_component_get_drvdata(component); |
| 347 |
if (!pdata) { |
| 348 |
dev_err(dev, "Drv data not found ..\n"); |
| 349 |
return -EINVAL; |
| 350 |
} |
| 351 |
|
| 352 |
prtd = kzalloc_obj(*prtd); |
| 353 |
if (prtd == NULL) |
| 354 |
return -ENOMEM; |
| 355 |
|
| 356 |
spin_lock_init(&prtd->lock); |
| 357 |
prtd->substream = substream; |
| 358 |
prtd->graph = q6apm_graph_open(dev, event_handler, prtd, graph_id); |
| 359 |
if (IS_ERR(prtd->graph)) { |
| 360 |
dev_err(dev, "%s: Could not allocate memory\n", __func__); |
| 361 |
ret = PTR_ERR(prtd->graph); |
| 362 |
goto err; |
| 363 |
} |
| 364 |
|
| 365 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 366 |
runtime->hw = q6apm_dai_hardware_playback; |
| 367 |
else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 368 |
runtime->hw = q6apm_dai_hardware_capture; |
| 369 |
|
| 370 |
/* Ensure that buffer size is a multiple of period size */ |
| 371 |
ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 372 |
if (ret < 0) { |
| 373 |
dev_err(dev, "snd_pcm_hw_constraint_integer failed\n"); |
| 374 |
goto err; |
| 375 |
} |
| 376 |
|
| 377 |
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 378 |
ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 379 |
BUFFER_BYTES_MIN, BUFFER_BYTES_MAX); |
| 380 |
if (ret < 0) { |
| 381 |
dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret); |
| 382 |
goto err; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/* setup 10ms latency to accommodate DSP restrictions */ |
| 387 |
ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 480); |
| 388 |
if (ret < 0) { |
| 389 |
dev_err(dev, "constraint for period bytes step ret = %d\n", ret); |
| 390 |
goto err; |
| 391 |
} |
| 392 |
|
| 393 |
ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 480); |
| 394 |
if (ret < 0) { |
| 395 |
dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret); |
| 396 |
goto err; |
| 397 |
} |
| 398 |
|
| 399 |
runtime->private_data = prtd; |
| 400 |
runtime->dma_bytes = BUFFER_BYTES_MAX; |
| 401 |
if (pdata->sid < 0) |
| 402 |
prtd->phys = substream->dma_buffer.addr; |
| 403 |
else |
| 404 |
prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32); |
| 405 |
|
| 406 |
return 0; |
| 407 |
err: |
| 408 |
kfree(prtd); |
| 409 |
|
| 410 |
return ret; |
| 411 |
} |
| 412 |
|
| 413 |
static int q6apm_dai_close(struct snd_soc_component *component, |
| 414 |
struct snd_pcm_substream *substream) |
| 415 |
{ |
| 416 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 417 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 418 |
|
| 419 |
if (prtd->state) { /* only stop graph that is started */ |
| 420 |
q6apm_graph_stop(prtd->graph); |
| 421 |
q6apm_unmap_memory_regions(prtd->graph, substream->stream); |
| 422 |
} |
| 423 |
|
| 424 |
q6apm_graph_close(prtd->graph); |
| 425 |
prtd->graph = NULL; |
| 426 |
kfree(prtd); |
| 427 |
runtime->private_data = NULL; |
| 428 |
|
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
static snd_pcm_uframes_t q6apm_dai_pointer(struct snd_soc_component *component, |
| 433 |
struct snd_pcm_substream *substream) |
| 434 |
{ |
| 435 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 436 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 437 |
snd_pcm_uframes_t ptr; |
| 438 |
|
| 439 |
ptr = q6apm_get_hw_pointer(prtd->graph, substream->stream) * runtime->period_size; |
| 440 |
if (ptr) |
| 441 |
return ptr - 1; |
| 442 |
|
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
static int q6apm_dai_hw_params(struct snd_soc_component *component, |
| 447 |
struct snd_pcm_substream *substream, |
| 448 |
struct snd_pcm_hw_params *params) |
| 449 |
{ |
| 450 |
struct snd_pcm_runtime *runtime = substream->runtime; |
| 451 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 452 |
|
| 453 |
prtd->pcm_size = params_buffer_bytes(params); |
| 454 |
prtd->periods = params_periods(params); |
| 455 |
|
| 456 |
switch (params_format(params)) { |
| 457 |
case SNDRV_PCM_FORMAT_S16_LE: |
| 458 |
prtd->bits_per_sample = 16; |
| 459 |
break; |
| 460 |
case SNDRV_PCM_FORMAT_S24_LE: |
| 461 |
prtd->bits_per_sample = 24; |
| 462 |
break; |
| 463 |
default: |
| 464 |
return -EINVAL; |
| 465 |
} |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd) |
| 471 |
{ |
| 472 |
int size = BUFFER_BYTES_MAX; |
| 473 |
|
| 474 |
return snd_pcm_set_fixed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, component->dev, size); |
| 475 |
} |
| 476 |
|
| 477 |
static int q6apm_dai_compr_open(struct snd_soc_component *component, |
| 478 |
struct snd_compr_stream *stream) |
| 479 |
{ |
| 480 |
struct snd_soc_pcm_runtime *rtd = stream->private_data; |
| 481 |
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0); |
| 482 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 483 |
struct q6apm_dai_rtd *prtd; |
| 484 |
struct q6apm_dai_data *pdata; |
| 485 |
struct device *dev = component->dev; |
| 486 |
int ret, size; |
| 487 |
int graph_id; |
| 488 |
|
| 489 |
graph_id = cpu_dai->driver->id; |
| 490 |
pdata = snd_soc_component_get_drvdata(component); |
| 491 |
if (!pdata) |
| 492 |
return -EINVAL; |
| 493 |
|
| 494 |
prtd = kzalloc_obj(*prtd); |
| 495 |
if (prtd == NULL) |
| 496 |
return -ENOMEM; |
| 497 |
|
| 498 |
prtd->cstream = stream; |
| 499 |
prtd->graph = q6apm_graph_open(dev, event_handler_compr, prtd, graph_id); |
| 500 |
if (IS_ERR(prtd->graph)) { |
| 501 |
ret = PTR_ERR(prtd->graph); |
| 502 |
kfree(prtd); |
| 503 |
return ret; |
| 504 |
} |
| 505 |
|
| 506 |
runtime->private_data = prtd; |
| 507 |
runtime->dma_bytes = BUFFER_BYTES_MAX; |
| 508 |
size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; |
| 509 |
ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &prtd->dma_buffer); |
| 510 |
if (ret) |
| 511 |
return ret; |
| 512 |
|
| 513 |
if (pdata->sid < 0) |
| 514 |
prtd->phys = prtd->dma_buffer.addr; |
| 515 |
else |
| 516 |
prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32); |
| 517 |
|
| 518 |
snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer); |
| 519 |
spin_lock_init(&prtd->lock); |
| 520 |
|
| 521 |
q6apm_enable_compress_module(dev, prtd->graph, true); |
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
static int q6apm_dai_compr_free(struct snd_soc_component *component, |
| 526 |
struct snd_compr_stream *stream) |
| 527 |
{ |
| 528 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 529 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 530 |
|
| 531 |
q6apm_graph_stop(prtd->graph); |
| 532 |
q6apm_unmap_memory_regions(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK); |
| 533 |
q6apm_graph_close(prtd->graph); |
| 534 |
snd_dma_free_pages(&prtd->dma_buffer); |
| 535 |
prtd->graph = NULL; |
| 536 |
kfree(prtd); |
| 537 |
runtime->private_data = NULL; |
| 538 |
|
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
static int q6apm_dai_compr_get_caps(struct snd_soc_component *component, |
| 543 |
struct snd_compr_stream *stream, |
| 544 |
struct snd_compr_caps *caps) |
| 545 |
{ |
| 546 |
caps->direction = SND_COMPRESS_PLAYBACK; |
| 547 |
caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE; |
| 548 |
caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE; |
| 549 |
caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS; |
| 550 |
caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS; |
| 551 |
caps->num_codecs = 4; |
| 552 |
caps->codecs[0] = SND_AUDIOCODEC_MP3; |
| 553 |
caps->codecs[1] = SND_AUDIOCODEC_AAC; |
| 554 |
caps->codecs[2] = SND_AUDIOCODEC_FLAC; |
| 555 |
caps->codecs[3] = SND_AUDIOCODEC_OPUS_RAW; |
| 556 |
|
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
static int q6apm_dai_compr_get_codec_caps(struct snd_soc_component *component, |
| 561 |
struct snd_compr_stream *stream, |
| 562 |
struct snd_compr_codec_caps *codec) |
| 563 |
{ |
| 564 |
switch (codec->codec) { |
| 565 |
case SND_AUDIOCODEC_MP3: |
| 566 |
*codec = q6apm_compr_caps; |
| 567 |
break; |
| 568 |
default: |
| 569 |
break; |
| 570 |
} |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
static int q6apm_dai_compr_pointer(struct snd_soc_component *component, |
| 576 |
struct snd_compr_stream *stream, |
| 577 |
struct snd_compr_tstamp64 *tstamp) |
| 578 |
{ |
| 579 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 580 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 581 |
uint64_t temp_copied_total; |
| 582 |
|
| 583 |
guard(spinlock_irqsave)(&prtd->lock); |
| 584 |
tstamp->copied_total = prtd->copied_total; |
| 585 |
temp_copied_total = tstamp->copied_total; |
| 586 |
tstamp->byte_offset = do_div(temp_copied_total, prtd->pcm_size); |
| 587 |
|
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
static int q6apm_dai_compr_trigger(struct snd_soc_component *component, |
| 592 |
struct snd_compr_stream *stream, int cmd) |
| 593 |
{ |
| 594 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 595 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 596 |
int ret = 0; |
| 597 |
|
| 598 |
switch (cmd) { |
| 599 |
case SNDRV_PCM_TRIGGER_START: |
| 600 |
case SNDRV_PCM_TRIGGER_RESUME: |
| 601 |
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 602 |
ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP); |
| 603 |
break; |
| 604 |
case SNDRV_PCM_TRIGGER_STOP: |
| 605 |
break; |
| 606 |
case SNDRV_PCM_TRIGGER_SUSPEND: |
| 607 |
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 608 |
break; |
| 609 |
case SND_COMPR_TRIGGER_NEXT_TRACK: |
| 610 |
prtd->next_track = true; |
| 611 |
break; |
| 612 |
case SND_COMPR_TRIGGER_DRAIN: |
| 613 |
case SND_COMPR_TRIGGER_PARTIAL_DRAIN: |
| 614 |
prtd->notify_on_drain = true; |
| 615 |
break; |
| 616 |
default: |
| 617 |
ret = -EINVAL; |
| 618 |
break; |
| 619 |
} |
| 620 |
|
| 621 |
return ret; |
| 622 |
} |
| 623 |
|
| 624 |
static int q6apm_dai_compr_ack(struct snd_soc_component *component, struct snd_compr_stream *stream, |
| 625 |
size_t count) |
| 626 |
{ |
| 627 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 628 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 629 |
|
| 630 |
guard(spinlock_irqsave)(&prtd->lock); |
| 631 |
prtd->bytes_received += count; |
| 632 |
|
| 633 |
return count; |
| 634 |
} |
| 635 |
|
| 636 |
static int q6apm_dai_compr_set_params(struct snd_soc_component *component, |
| 637 |
struct snd_compr_stream *stream, |
| 638 |
struct snd_compr_params *params) |
| 639 |
{ |
| 640 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 641 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 642 |
struct q6apm_dai_data *pdata; |
| 643 |
struct audioreach_module_config cfg; |
| 644 |
struct snd_codec *codec = ¶ms->codec; |
| 645 |
int dir = stream->direction; |
| 646 |
int ret; |
| 647 |
|
| 648 |
pdata = snd_soc_component_get_drvdata(component); |
| 649 |
if (!pdata) |
| 650 |
return -EINVAL; |
| 651 |
|
| 652 |
prtd->periods = runtime->fragments; |
| 653 |
prtd->pcm_count = runtime->fragment_size; |
| 654 |
prtd->pcm_size = runtime->fragments * runtime->fragment_size; |
| 655 |
prtd->bits_per_sample = 16; |
| 656 |
|
| 657 |
if (prtd->next_track != true) { |
| 658 |
memcpy(&prtd->codec, codec, sizeof(*codec)); |
| 659 |
|
| 660 |
ret = q6apm_set_real_module_id(component->dev, prtd->graph, codec->id); |
| 661 |
if (ret) |
| 662 |
return ret; |
| 663 |
|
| 664 |
cfg.direction = dir; |
| 665 |
cfg.sample_rate = codec->sample_rate; |
| 666 |
cfg.num_channels = 2; |
| 667 |
cfg.bit_width = prtd->bits_per_sample; |
| 668 |
cfg.fmt = codec->id; |
| 669 |
audioreach_set_default_channel_mapping(cfg.channel_map, |
| 670 |
cfg.num_channels); |
| 671 |
memcpy(&cfg.codec, codec, sizeof(*codec)); |
| 672 |
|
| 673 |
ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg); |
| 674 |
if (ret < 0) |
| 675 |
return ret; |
| 676 |
|
| 677 |
ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg); |
| 678 |
if (ret) |
| 679 |
return ret; |
| 680 |
|
| 681 |
ret = q6apm_map_memory_regions(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK, |
| 682 |
prtd->phys, (prtd->pcm_size / prtd->periods), |
| 683 |
prtd->periods); |
| 684 |
if (ret < 0) |
| 685 |
return -ENOMEM; |
| 686 |
|
| 687 |
ret = q6apm_graph_prepare(prtd->graph); |
| 688 |
if (ret) |
| 689 |
return ret; |
| 690 |
|
| 691 |
ret = q6apm_graph_start(prtd->graph); |
| 692 |
if (ret) |
| 693 |
return ret; |
| 694 |
|
| 695 |
} else { |
| 696 |
cfg.direction = dir; |
| 697 |
cfg.sample_rate = codec->sample_rate; |
| 698 |
cfg.num_channels = 2; |
| 699 |
cfg.bit_width = prtd->bits_per_sample; |
| 700 |
cfg.fmt = codec->id; |
| 701 |
memcpy(&cfg.codec, codec, sizeof(*codec)); |
| 702 |
|
| 703 |
ret = audioreach_compr_set_param(prtd->graph, &cfg); |
| 704 |
if (ret < 0) |
| 705 |
return ret; |
| 706 |
} |
| 707 |
prtd->state = Q6APM_STREAM_RUNNING; |
| 708 |
|
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
static int q6apm_dai_compr_set_metadata(struct snd_soc_component *component, |
| 713 |
struct snd_compr_stream *stream, |
| 714 |
struct snd_compr_metadata *metadata) |
| 715 |
{ |
| 716 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 717 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 718 |
int ret = 0; |
| 719 |
|
| 720 |
switch (metadata->key) { |
| 721 |
case SNDRV_COMPRESS_ENCODER_PADDING: |
| 722 |
q6apm_remove_trailing_silence(component->dev, prtd->graph, |
| 723 |
metadata->value[0]); |
| 724 |
break; |
| 725 |
case SNDRV_COMPRESS_ENCODER_DELAY: |
| 726 |
q6apm_remove_initial_silence(component->dev, prtd->graph, |
| 727 |
metadata->value[0]); |
| 728 |
break; |
| 729 |
default: |
| 730 |
ret = -EINVAL; |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
return ret; |
| 735 |
} |
| 736 |
|
| 737 |
static int q6apm_dai_compr_mmap(struct snd_soc_component *component, |
| 738 |
struct snd_compr_stream *stream, |
| 739 |
struct vm_area_struct *vma) |
| 740 |
{ |
| 741 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 742 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 743 |
struct device *dev = component->dev; |
| 744 |
|
| 745 |
return dma_mmap_coherent(dev, vma, prtd->dma_buffer.area, prtd->dma_buffer.addr, |
| 746 |
prtd->dma_buffer.bytes); |
| 747 |
} |
| 748 |
|
| 749 |
static int q6apm_compr_copy(struct snd_soc_component *component, |
| 750 |
struct snd_compr_stream *stream, char __user *buf, |
| 751 |
size_t count) |
| 752 |
{ |
| 753 |
struct snd_compr_runtime *runtime = stream->runtime; |
| 754 |
struct q6apm_dai_rtd *prtd = runtime->private_data; |
| 755 |
void *dstn; |
| 756 |
size_t copy; |
| 757 |
u32 wflags = 0; |
| 758 |
u32 app_pointer; |
| 759 |
uint64_t bytes_received; |
| 760 |
uint64_t temp_bytes_received; |
| 761 |
uint32_t bytes_to_write; |
| 762 |
uint64_t avail, bytes_in_flight = 0; |
| 763 |
|
| 764 |
bytes_received = prtd->bytes_received; |
| 765 |
temp_bytes_received = bytes_received; |
| 766 |
|
| 767 |
/** |
| 768 |
* Make sure that next track data pointer is aligned at 32 bit boundary |
| 769 |
* This is a Mandatory requirement from DSP data buffers alignment |
| 770 |
*/ |
| 771 |
if (prtd->next_track) { |
| 772 |
bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count); |
| 773 |
temp_bytes_received = bytes_received; |
| 774 |
} |
| 775 |
|
| 776 |
app_pointer = do_div(temp_bytes_received, prtd->pcm_size); |
| 777 |
dstn = prtd->dma_buffer.area + app_pointer; |
| 778 |
|
| 779 |
if (count < prtd->pcm_size - app_pointer) { |
| 780 |
if (copy_from_user(dstn, buf, count)) |
| 781 |
return -EFAULT; |
| 782 |
} else { |
| 783 |
copy = prtd->pcm_size - app_pointer; |
| 784 |
if (copy_from_user(dstn, buf, copy)) |
| 785 |
return -EFAULT; |
| 786 |
if (copy_from_user(prtd->dma_buffer.area, buf + copy, count - copy)) |
| 787 |
return -EFAULT; |
| 788 |
} |
| 789 |
|
| 790 |
guard(spinlock_irqsave)(&prtd->lock); |
| 791 |
bytes_in_flight = prtd->bytes_received - prtd->copied_total; |
| 792 |
|
| 793 |
if (prtd->next_track) { |
| 794 |
prtd->next_track = false; |
| 795 |
prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count); |
| 796 |
prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count); |
| 797 |
} |
| 798 |
|
| 799 |
prtd->bytes_received = bytes_received + count; |
| 800 |
|
| 801 |
/* Kick off the data to dsp if its starving!! */ |
| 802 |
if (prtd->state == Q6APM_STREAM_RUNNING && (bytes_in_flight == 0)) { |
| 803 |
bytes_to_write = prtd->pcm_count; |
| 804 |
avail = prtd->bytes_received - prtd->bytes_sent; |
| 805 |
|
| 806 |
if (avail < prtd->pcm_count) |
| 807 |
bytes_to_write = avail; |
| 808 |
|
| 809 |
q6apm_write_async(prtd->graph, bytes_to_write, 0, 0, wflags); |
| 810 |
prtd->bytes_sent += bytes_to_write; |
| 811 |
} |
| 812 |
|
| 813 |
return count; |
| 814 |
} |
| 815 |
|
| 816 |
static const struct snd_compress_ops q6apm_dai_compress_ops = { |
| 817 |
.open = q6apm_dai_compr_open, |
| 818 |
.free = q6apm_dai_compr_free, |
| 819 |
.get_caps = q6apm_dai_compr_get_caps, |
| 820 |
.get_codec_caps = q6apm_dai_compr_get_codec_caps, |
| 821 |
.pointer = q6apm_dai_compr_pointer, |
| 822 |
.trigger = q6apm_dai_compr_trigger, |
| 823 |
.ack = q6apm_dai_compr_ack, |
| 824 |
.set_params = q6apm_dai_compr_set_params, |
| 825 |
.set_metadata = q6apm_dai_compr_set_metadata, |
| 826 |
.mmap = q6apm_dai_compr_mmap, |
| 827 |
.copy = q6apm_compr_copy, |
| 828 |
}; |
| 829 |
|
| 830 |
static const struct snd_soc_component_driver q6apm_fe_dai_component = { |
| 831 |
.name = DRV_NAME, |
| 832 |
.open = q6apm_dai_open, |
| 833 |
.close = q6apm_dai_close, |
| 834 |
.prepare = q6apm_dai_prepare, |
| 835 |
.pcm_construct = q6apm_dai_pcm_new, |
| 836 |
.hw_params = q6apm_dai_hw_params, |
| 837 |
.pointer = q6apm_dai_pointer, |
| 838 |
.trigger = q6apm_dai_trigger, |
| 839 |
.ack = q6apm_dai_ack, |
| 840 |
.compress_ops = &q6apm_dai_compress_ops, |
| 841 |
.use_dai_pcm_id = true, |
| 842 |
.remove_order = SND_SOC_COMP_ORDER_EARLY, |
| 843 |
}; |
| 844 |
|
| 845 |
static int q6apm_dai_probe(struct platform_device *pdev) |
| 846 |
{ |
| 847 |
struct device *dev = &pdev->dev; |
| 848 |
struct device_node *node = dev->of_node; |
| 849 |
struct q6apm_dai_data *pdata; |
| 850 |
struct of_phandle_args args; |
| 851 |
int rc; |
| 852 |
|
| 853 |
pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 854 |
if (!pdata) |
| 855 |
return -ENOMEM; |
| 856 |
|
| 857 |
rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args); |
| 858 |
if (rc < 0) |
| 859 |
pdata->sid = -1; |
| 860 |
else |
| 861 |
pdata->sid = args.args[0] & SID_MASK_DEFAULT; |
| 862 |
|
| 863 |
dev_set_drvdata(dev, pdata); |
| 864 |
|
| 865 |
return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0); |
| 866 |
} |
| 867 |
|
| 868 |
#ifdef CONFIG_OF |
| 869 |
static const struct of_device_id q6apm_dai_device_id[] = { |
| 870 |
{ .compatible = "qcom,q6apm-dais" }, |
| 871 |
{}, |
| 872 |
}; |
| 873 |
MODULE_DEVICE_TABLE(of, q6apm_dai_device_id); |
| 874 |
#endif |
| 875 |
|
| 876 |
static struct platform_driver q6apm_dai_platform_driver = { |
| 877 |
.driver = { |
| 878 |
.name = "q6apm-dai", |
| 879 |
.of_match_table = of_match_ptr(q6apm_dai_device_id), |
| 880 |
}, |
| 881 |
.probe = q6apm_dai_probe, |
| 882 |
}; |
| 883 |
module_platform_driver(q6apm_dai_platform_driver); |
| 884 |
|
| 885 |
MODULE_DESCRIPTION("Q6APM dai driver"); |
| 886 |
MODULE_LICENSE("GPL"); |