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