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
event_handler(uint32_t opcode,uint32_t token,void * payload,void * priv)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
event_handler_compr(uint32_t opcode,uint32_t token,void * payload,void * priv)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
q6apm_dai_prepare(struct snd_soc_component * component,struct snd_pcm_substream * substream)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 if (prtd->state) {
232 /* clear the previous setup if any */
233 q6apm_graph_stop(prtd->graph);
234 q6apm_free_fragments(prtd->graph, substream->stream);
235 }
236
237 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
238 /* rate and channels are sent to audio driver */
239 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
240 if (ret < 0) {
241 dev_err(dev, "%s: q6apm_open_write failed\n", __func__);
242 return ret;
243 }
244
245 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
246 if (ret < 0)
247 dev_err(dev, "%s: CMD Format block failed\n", __func__);
248
249 ret = q6apm_alloc_fragments(prtd->graph, substream->stream, prtd->phys,
250 (prtd->pcm_size / prtd->periods), prtd->periods);
251
252 if (ret < 0) {
253 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
254 return -ENOMEM;
255 }
256
257 ret = q6apm_graph_prepare(prtd->graph);
258 if (ret) {
259 dev_err(dev, "Failed to prepare Graph %d\n", ret);
260 return ret;
261 }
262
263 ret = q6apm_graph_start(prtd->graph);
264 if (ret) {
265 dev_err(dev, "Failed to Start Graph %d\n", ret);
266 return ret;
267 }
268
269 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
270 int i;
271 /* Queue the buffers for Capture ONLY after graph is started */
272 for (i = 0; i < runtime->periods; i++)
273 q6apm_read(prtd->graph);
274
275 }
276
277 /* Now that graph as been prepared and started update the internal state accordingly */
278 prtd->state = Q6APM_STREAM_RUNNING;
279
280 return 0;
281 }
282
q6apm_dai_ack(struct snd_soc_component * component,struct snd_pcm_substream * substream)283 static int q6apm_dai_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream)
284 {
285 struct snd_pcm_runtime *runtime = substream->runtime;
286 struct q6apm_dai_rtd *prtd = runtime->private_data;
287 int i, ret = 0, avail_periods;
288
289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
290 avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
291 for (i = 0; i < avail_periods; i++) {
292 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
293 if (ret < 0) {
294 dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
295 return ret;
296 }
297 prtd->queue_ptr += runtime->period_size;
298 }
299 }
300
301 return ret;
302 }
303
q6apm_dai_trigger(struct snd_soc_component * component,struct snd_pcm_substream * substream,int cmd)304 static int q6apm_dai_trigger(struct snd_soc_component *component,
305 struct snd_pcm_substream *substream, int cmd)
306 {
307 struct snd_pcm_runtime *runtime = substream->runtime;
308 struct q6apm_dai_rtd *prtd = runtime->private_data;
309 int ret = 0;
310
311 switch (cmd) {
312 case SNDRV_PCM_TRIGGER_START:
313 case SNDRV_PCM_TRIGGER_RESUME:
314 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
315 break;
316 case SNDRV_PCM_TRIGGER_STOP:
317 /* TODO support be handled via SoftPause Module */
318 prtd->state = Q6APM_STREAM_STOPPED;
319 prtd->queue_ptr = 0;
320 break;
321 case SNDRV_PCM_TRIGGER_SUSPEND:
322 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
323 break;
324 default:
325 ret = -EINVAL;
326 break;
327 }
328
329 return ret;
330 }
331
q6apm_dai_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)332 static int q6apm_dai_open(struct snd_soc_component *component,
333 struct snd_pcm_substream *substream)
334 {
335 struct snd_pcm_runtime *runtime = substream->runtime;
336 struct snd_soc_pcm_runtime *soc_prtd = snd_soc_substream_to_rtd(substream);
337 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0);
338 struct device *dev = component->dev;
339 struct q6apm_dai_data *pdata;
340 struct q6apm_dai_rtd *prtd;
341 int graph_id, ret;
342
343 graph_id = cpu_dai->driver->id;
344
345 pdata = snd_soc_component_get_drvdata(component);
346 if (!pdata) {
347 dev_err(dev, "Drv data not found ..\n");
348 return -EINVAL;
349 }
350
351 prtd = kzalloc_obj(*prtd);
352 if (prtd == NULL)
353 return -ENOMEM;
354
355 spin_lock_init(&prtd->lock);
356 prtd->substream = substream;
357 prtd->graph = q6apm_graph_open(dev, event_handler, prtd, graph_id, substream->stream);
358 if (IS_ERR(prtd->graph)) {
359 dev_err(dev, "%s: Could not allocate memory\n", __func__);
360 ret = PTR_ERR(prtd->graph);
361 goto err;
362 }
363
364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
365 runtime->hw = q6apm_dai_hardware_playback;
366 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
367 runtime->hw = q6apm_dai_hardware_capture;
368
369 /* Ensure that buffer size is a multiple of period size */
370 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
371 if (ret < 0) {
372 dev_err(dev, "snd_pcm_hw_constraint_integer failed\n");
373 goto err;
374 }
375
376 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
377 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
378 BUFFER_BYTES_MIN, BUFFER_BYTES_MAX);
379 if (ret < 0) {
380 dev_err(dev, "constraint for buffer bytes min max ret = %d\n", ret);
381 goto err;
382 }
383 }
384
385 /* setup 10ms latency to accommodate DSP restrictions */
386 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 480);
387 if (ret < 0) {
388 dev_err(dev, "constraint for period bytes step ret = %d\n", ret);
389 goto err;
390 }
391
392 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 480);
393 if (ret < 0) {
394 dev_err(dev, "constraint for buffer bytes step ret = %d\n", ret);
395 goto err;
396 }
397
398 runtime->private_data = prtd;
399 runtime->dma_bytes = BUFFER_BYTES_MAX;
400 if (pdata->sid < 0)
401 prtd->phys = substream->dma_buffer.addr;
402 else
403 prtd->phys = substream->dma_buffer.addr | (pdata->sid << 32);
404
405 return 0;
406 err:
407 kfree(prtd);
408
409 return ret;
410 }
411
q6apm_dai_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)412 static int q6apm_dai_close(struct snd_soc_component *component,
413 struct snd_pcm_substream *substream)
414 {
415 struct snd_pcm_runtime *runtime = substream->runtime;
416 struct q6apm_dai_rtd *prtd = runtime->private_data;
417
418 if (prtd->state) {
419 /* only stop graph that is started */
420 q6apm_graph_stop(prtd->graph);
421 q6apm_free_fragments(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
q6apm_dai_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)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
q6apm_dai_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)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
q6apm_dai_memory_map(struct snd_soc_component * component,struct snd_pcm_substream * substream,int graph_id)470 static int q6apm_dai_memory_map(struct snd_soc_component *component,
471 struct snd_pcm_substream *substream, int graph_id)
472 {
473 struct q6apm_dai_data *pdata;
474 struct device *dev = component->dev;
475 phys_addr_t phys;
476 int ret;
477
478 pdata = snd_soc_component_get_drvdata(component);
479 if (!pdata) {
480 dev_err(component->dev, "Drv data not found ..\n");
481 return -EINVAL;
482 }
483
484 if (pdata->sid < 0)
485 phys = substream->dma_buffer.addr;
486 else
487 phys = substream->dma_buffer.addr | (pdata->sid << 32);
488
489 ret = q6apm_map_memory_fixed_region(dev, graph_id, phys, BUFFER_BYTES_MAX);
490 if (ret < 0)
491 dev_err(dev, "Audio Start: Buffer Allocation failed rc = %d\n", ret);
492
493 return ret;
494 }
495
q6apm_dai_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)496 static int q6apm_dai_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
497 {
498 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
499 struct snd_pcm *pcm = rtd->pcm;
500 int size = BUFFER_BYTES_MAX;
501 int graph_id, ret;
502 struct snd_pcm_substream *substream;
503
504 graph_id = cpu_dai->driver->id;
505
506 ret = snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, component->dev, size);
507 if (ret)
508 return ret;
509
510 /* Note: DSP backend dais are uni-directional ONLY(either playback or capture) */
511 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
512 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
513 ret = q6apm_dai_memory_map(component, substream, graph_id);
514 if (ret)
515 return ret;
516 }
517
518 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
519 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
520 ret = q6apm_dai_memory_map(component, substream, graph_id);
521 if (ret)
522 return ret;
523 }
524
525 return 0;
526 }
527
q6apm_dai_memory_unmap(struct snd_soc_component * component,struct snd_pcm_substream * substream)528 static void q6apm_dai_memory_unmap(struct snd_soc_component *component,
529 struct snd_pcm_substream *substream)
530 {
531 struct snd_soc_pcm_runtime *soc_prtd;
532 struct snd_soc_dai *cpu_dai;
533 int graph_id;
534
535 soc_prtd = snd_soc_substream_to_rtd(substream);
536 if (!soc_prtd)
537 return;
538
539 cpu_dai = snd_soc_rtd_to_cpu(soc_prtd, 0);
540 if (!cpu_dai)
541 return;
542
543 graph_id = cpu_dai->driver->id;
544 q6apm_unmap_memory_fixed_region(component->dev, graph_id);
545 }
546
q6apm_dai_pcm_free(struct snd_soc_component * component,struct snd_pcm * pcm)547 static void q6apm_dai_pcm_free(struct snd_soc_component *component, struct snd_pcm *pcm)
548 {
549 struct snd_pcm_substream *substream;
550
551 substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
552 if (substream)
553 q6apm_dai_memory_unmap(component, substream);
554
555 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
556 if (substream)
557 q6apm_dai_memory_unmap(component, substream);
558 }
559
q6apm_dai_compr_open(struct snd_soc_component * component,struct snd_compr_stream * stream)560 static int q6apm_dai_compr_open(struct snd_soc_component *component,
561 struct snd_compr_stream *stream)
562 {
563 struct snd_soc_pcm_runtime *rtd = stream->private_data;
564 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
565 struct snd_compr_runtime *runtime = stream->runtime;
566 struct q6apm_dai_rtd *prtd;
567 struct q6apm_dai_data *pdata;
568 struct device *dev = component->dev;
569 int ret, size;
570 int graph_id;
571
572 graph_id = cpu_dai->driver->id;
573 pdata = snd_soc_component_get_drvdata(component);
574 if (!pdata)
575 return -EINVAL;
576
577 prtd = kzalloc_obj(*prtd);
578 if (prtd == NULL)
579 return -ENOMEM;
580
581 prtd->cstream = stream;
582 prtd->graph = q6apm_graph_open(dev, event_handler_compr, prtd, graph_id,
583 SNDRV_PCM_STREAM_PLAYBACK);
584 if (IS_ERR(prtd->graph)) {
585 ret = PTR_ERR(prtd->graph);
586 kfree(prtd);
587 return ret;
588 }
589
590 runtime->private_data = prtd;
591 runtime->dma_bytes = BUFFER_BYTES_MAX;
592 size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE * COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
593 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dev, size, &prtd->dma_buffer);
594 if (ret)
595 return ret;
596
597 if (pdata->sid < 0)
598 prtd->phys = prtd->dma_buffer.addr;
599 else
600 prtd->phys = prtd->dma_buffer.addr | (pdata->sid << 32);
601
602 snd_compr_set_runtime_buffer(stream, &prtd->dma_buffer);
603 spin_lock_init(&prtd->lock);
604
605 q6apm_enable_compress_module(dev, prtd->graph, true);
606 return 0;
607 }
608
q6apm_dai_compr_free(struct snd_soc_component * component,struct snd_compr_stream * stream)609 static int q6apm_dai_compr_free(struct snd_soc_component *component,
610 struct snd_compr_stream *stream)
611 {
612 struct snd_compr_runtime *runtime = stream->runtime;
613 struct q6apm_dai_rtd *prtd = runtime->private_data;
614
615 q6apm_graph_stop(prtd->graph);
616 q6apm_free_fragments(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK);
617 q6apm_unmap_memory_fixed_region(component->dev, prtd->graph->id);
618 q6apm_graph_close(prtd->graph);
619 snd_dma_free_pages(&prtd->dma_buffer);
620 prtd->graph = NULL;
621 kfree(prtd);
622 runtime->private_data = NULL;
623
624 return 0;
625 }
626
q6apm_dai_compr_get_caps(struct snd_soc_component * component,struct snd_compr_stream * stream,struct snd_compr_caps * caps)627 static int q6apm_dai_compr_get_caps(struct snd_soc_component *component,
628 struct snd_compr_stream *stream,
629 struct snd_compr_caps *caps)
630 {
631 caps->direction = SND_COMPRESS_PLAYBACK;
632 caps->min_fragment_size = COMPR_PLAYBACK_MIN_FRAGMENT_SIZE;
633 caps->max_fragment_size = COMPR_PLAYBACK_MAX_FRAGMENT_SIZE;
634 caps->min_fragments = COMPR_PLAYBACK_MIN_NUM_FRAGMENTS;
635 caps->max_fragments = COMPR_PLAYBACK_MAX_NUM_FRAGMENTS;
636 caps->num_codecs = 4;
637 caps->codecs[0] = SND_AUDIOCODEC_MP3;
638 caps->codecs[1] = SND_AUDIOCODEC_AAC;
639 caps->codecs[2] = SND_AUDIOCODEC_FLAC;
640 caps->codecs[3] = SND_AUDIOCODEC_OPUS_RAW;
641
642 return 0;
643 }
644
q6apm_dai_compr_get_codec_caps(struct snd_soc_component * component,struct snd_compr_stream * stream,struct snd_compr_codec_caps * codec)645 static int q6apm_dai_compr_get_codec_caps(struct snd_soc_component *component,
646 struct snd_compr_stream *stream,
647 struct snd_compr_codec_caps *codec)
648 {
649 switch (codec->codec) {
650 case SND_AUDIOCODEC_MP3:
651 *codec = q6apm_compr_caps;
652 break;
653 default:
654 break;
655 }
656
657 return 0;
658 }
659
q6apm_dai_compr_pointer(struct snd_soc_component * component,struct snd_compr_stream * stream,struct snd_compr_tstamp64 * tstamp)660 static int q6apm_dai_compr_pointer(struct snd_soc_component *component,
661 struct snd_compr_stream *stream,
662 struct snd_compr_tstamp64 *tstamp)
663 {
664 struct snd_compr_runtime *runtime = stream->runtime;
665 struct q6apm_dai_rtd *prtd = runtime->private_data;
666 uint64_t temp_copied_total;
667
668 guard(spinlock_irqsave)(&prtd->lock);
669 tstamp->copied_total = prtd->copied_total;
670 temp_copied_total = tstamp->copied_total;
671 tstamp->byte_offset = do_div(temp_copied_total, prtd->pcm_size);
672
673 return 0;
674 }
675
q6apm_dai_compr_trigger(struct snd_soc_component * component,struct snd_compr_stream * stream,int cmd)676 static int q6apm_dai_compr_trigger(struct snd_soc_component *component,
677 struct snd_compr_stream *stream, int cmd)
678 {
679 struct snd_compr_runtime *runtime = stream->runtime;
680 struct q6apm_dai_rtd *prtd = runtime->private_data;
681 int ret = 0;
682
683 switch (cmd) {
684 case SNDRV_PCM_TRIGGER_START:
685 case SNDRV_PCM_TRIGGER_RESUME:
686 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
687 ret = q6apm_write_async(prtd->graph, prtd->pcm_count, 0, 0, NO_TIMESTAMP);
688 break;
689 case SNDRV_PCM_TRIGGER_STOP:
690 break;
691 case SNDRV_PCM_TRIGGER_SUSPEND:
692 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
693 break;
694 case SND_COMPR_TRIGGER_NEXT_TRACK:
695 prtd->next_track = true;
696 break;
697 case SND_COMPR_TRIGGER_DRAIN:
698 case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
699 prtd->notify_on_drain = true;
700 break;
701 default:
702 ret = -EINVAL;
703 break;
704 }
705
706 return ret;
707 }
708
q6apm_dai_compr_ack(struct snd_soc_component * component,struct snd_compr_stream * stream,size_t count)709 static int q6apm_dai_compr_ack(struct snd_soc_component *component, struct snd_compr_stream *stream,
710 size_t count)
711 {
712 struct snd_compr_runtime *runtime = stream->runtime;
713 struct q6apm_dai_rtd *prtd = runtime->private_data;
714
715 guard(spinlock_irqsave)(&prtd->lock);
716 prtd->bytes_received += count;
717
718 return count;
719 }
720
q6apm_dai_compr_set_params(struct snd_soc_component * component,struct snd_compr_stream * stream,struct snd_compr_params * params)721 static int q6apm_dai_compr_set_params(struct snd_soc_component *component,
722 struct snd_compr_stream *stream,
723 struct snd_compr_params *params)
724 {
725 struct snd_compr_runtime *runtime = stream->runtime;
726 struct q6apm_dai_rtd *prtd = runtime->private_data;
727 struct q6apm_dai_data *pdata;
728 struct audioreach_module_config cfg;
729 struct snd_codec *codec = ¶ms->codec;
730 int dir = stream->direction;
731 int ret;
732
733 pdata = snd_soc_component_get_drvdata(component);
734 if (!pdata)
735 return -EINVAL;
736
737 prtd->periods = runtime->fragments;
738 prtd->pcm_count = runtime->fragment_size;
739 prtd->pcm_size = runtime->fragments * runtime->fragment_size;
740 prtd->bits_per_sample = 16;
741
742 if (prtd->next_track != true) {
743 memcpy(&prtd->codec, codec, sizeof(*codec));
744
745 ret = q6apm_set_real_module_id(component->dev, prtd->graph, codec->id);
746 if (ret)
747 return ret;
748
749 cfg.direction = dir;
750 cfg.sample_rate = codec->sample_rate;
751 cfg.num_channels = 2;
752 cfg.bit_width = prtd->bits_per_sample;
753 cfg.fmt = codec->id;
754 audioreach_set_default_channel_mapping(cfg.channel_map,
755 cfg.num_channels);
756 memcpy(&cfg.codec, codec, sizeof(*codec));
757
758 ret = q6apm_graph_media_format_shmem(prtd->graph, &cfg);
759 if (ret < 0)
760 return ret;
761
762 ret = q6apm_graph_media_format_pcm(prtd->graph, &cfg);
763 if (ret)
764 return ret;
765
766 ret = q6apm_alloc_fragments(prtd->graph, SNDRV_PCM_STREAM_PLAYBACK,
767 prtd->phys, (prtd->pcm_size / prtd->periods),
768 prtd->periods);
769 if (ret < 0)
770 return -ENOMEM;
771
772 ret = q6apm_graph_prepare(prtd->graph);
773 if (ret)
774 return ret;
775
776 ret = q6apm_graph_start(prtd->graph);
777 if (ret)
778 return ret;
779
780 } else {
781 cfg.direction = dir;
782 cfg.sample_rate = codec->sample_rate;
783 cfg.num_channels = 2;
784 cfg.bit_width = prtd->bits_per_sample;
785 cfg.fmt = codec->id;
786 memcpy(&cfg.codec, codec, sizeof(*codec));
787
788 ret = audioreach_compr_set_param(prtd->graph, &cfg);
789 if (ret < 0)
790 return ret;
791 }
792 prtd->state = Q6APM_STREAM_RUNNING;
793
794 return 0;
795 }
796
q6apm_dai_compr_set_metadata(struct snd_soc_component * component,struct snd_compr_stream * stream,struct snd_compr_metadata * metadata)797 static int q6apm_dai_compr_set_metadata(struct snd_soc_component *component,
798 struct snd_compr_stream *stream,
799 struct snd_compr_metadata *metadata)
800 {
801 struct snd_compr_runtime *runtime = stream->runtime;
802 struct q6apm_dai_rtd *prtd = runtime->private_data;
803 int ret = 0;
804
805 switch (metadata->key) {
806 case SNDRV_COMPRESS_ENCODER_PADDING:
807 q6apm_remove_trailing_silence(component->dev, prtd->graph,
808 metadata->value[0]);
809 break;
810 case SNDRV_COMPRESS_ENCODER_DELAY:
811 q6apm_remove_initial_silence(component->dev, prtd->graph,
812 metadata->value[0]);
813 break;
814 default:
815 ret = -EINVAL;
816 break;
817 }
818
819 return ret;
820 }
821
q6apm_dai_compr_mmap(struct snd_soc_component * component,struct snd_compr_stream * stream,struct vm_area_struct * vma)822 static int q6apm_dai_compr_mmap(struct snd_soc_component *component,
823 struct snd_compr_stream *stream,
824 struct vm_area_struct *vma)
825 {
826 struct snd_compr_runtime *runtime = stream->runtime;
827 struct q6apm_dai_rtd *prtd = runtime->private_data;
828 struct device *dev = component->dev;
829
830 return dma_mmap_coherent(dev, vma, prtd->dma_buffer.area, prtd->dma_buffer.addr,
831 prtd->dma_buffer.bytes);
832 }
833
q6apm_compr_copy(struct snd_soc_component * component,struct snd_compr_stream * stream,char __user * buf,size_t count)834 static int q6apm_compr_copy(struct snd_soc_component *component,
835 struct snd_compr_stream *stream, char __user *buf,
836 size_t count)
837 {
838 struct snd_compr_runtime *runtime = stream->runtime;
839 struct q6apm_dai_rtd *prtd = runtime->private_data;
840 void *dstn;
841 size_t copy;
842 u32 wflags = 0;
843 u32 app_pointer;
844 uint64_t bytes_received;
845 uint64_t temp_bytes_received;
846 uint32_t bytes_to_write;
847 uint64_t avail, bytes_in_flight = 0;
848
849 bytes_received = prtd->bytes_received;
850 temp_bytes_received = bytes_received;
851
852 /**
853 * Make sure that next track data pointer is aligned at 32 bit boundary
854 * This is a Mandatory requirement from DSP data buffers alignment
855 */
856 if (prtd->next_track) {
857 bytes_received = ALIGN(prtd->bytes_received, prtd->pcm_count);
858 temp_bytes_received = bytes_received;
859 }
860
861 app_pointer = do_div(temp_bytes_received, prtd->pcm_size);
862 dstn = prtd->dma_buffer.area + app_pointer;
863
864 if (count < prtd->pcm_size - app_pointer) {
865 if (copy_from_user(dstn, buf, count))
866 return -EFAULT;
867 } else {
868 copy = prtd->pcm_size - app_pointer;
869 if (copy_from_user(dstn, buf, copy))
870 return -EFAULT;
871 if (copy_from_user(prtd->dma_buffer.area, buf + copy, count - copy))
872 return -EFAULT;
873 }
874
875 guard(spinlock_irqsave)(&prtd->lock);
876 bytes_in_flight = prtd->bytes_received - prtd->copied_total;
877
878 if (prtd->next_track) {
879 prtd->next_track = false;
880 prtd->copied_total = ALIGN(prtd->copied_total, prtd->pcm_count);
881 prtd->bytes_sent = ALIGN(prtd->bytes_sent, prtd->pcm_count);
882 }
883
884 prtd->bytes_received = bytes_received + count;
885
886 /* Kick off the data to dsp if its starving!! */
887 if (prtd->state == Q6APM_STREAM_RUNNING && (bytes_in_flight == 0)) {
888 bytes_to_write = prtd->pcm_count;
889 avail = prtd->bytes_received - prtd->bytes_sent;
890
891 if (avail < prtd->pcm_count)
892 bytes_to_write = avail;
893
894 q6apm_write_async(prtd->graph, bytes_to_write, 0, 0, wflags);
895 prtd->bytes_sent += bytes_to_write;
896 }
897
898 return count;
899 }
900
901 static const struct snd_compress_ops q6apm_dai_compress_ops = {
902 .open = q6apm_dai_compr_open,
903 .free = q6apm_dai_compr_free,
904 .get_caps = q6apm_dai_compr_get_caps,
905 .get_codec_caps = q6apm_dai_compr_get_codec_caps,
906 .pointer = q6apm_dai_compr_pointer,
907 .trigger = q6apm_dai_compr_trigger,
908 .ack = q6apm_dai_compr_ack,
909 .set_params = q6apm_dai_compr_set_params,
910 .set_metadata = q6apm_dai_compr_set_metadata,
911 .mmap = q6apm_dai_compr_mmap,
912 .copy = q6apm_compr_copy,
913 };
914
915 static const struct snd_soc_component_driver q6apm_fe_dai_component = {
916 .name = DRV_NAME,
917 .open = q6apm_dai_open,
918 .close = q6apm_dai_close,
919 .prepare = q6apm_dai_prepare,
920 .pcm_new = q6apm_dai_pcm_new,
921 .pcm_free = q6apm_dai_pcm_free,
922 .hw_params = q6apm_dai_hw_params,
923 .pointer = q6apm_dai_pointer,
924 .trigger = q6apm_dai_trigger,
925 .ack = q6apm_dai_ack,
926 .compress_ops = &q6apm_dai_compress_ops,
927 .use_dai_pcm_id = true,
928 .remove_order = SND_SOC_COMP_ORDER_EARLY,
929 };
930
q6apm_dai_probe(struct platform_device * pdev)931 static int q6apm_dai_probe(struct platform_device *pdev)
932 {
933 struct device *dev = &pdev->dev;
934 struct device_node *node = dev->of_node;
935 struct q6apm_dai_data *pdata;
936 struct of_phandle_args args;
937 int rc;
938
939 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
940 if (!pdata)
941 return -ENOMEM;
942
943 rc = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
944 if (rc < 0)
945 pdata->sid = -1;
946 else
947 pdata->sid = args.args[0] & SID_MASK_DEFAULT;
948
949 dev_set_drvdata(dev, pdata);
950
951 return devm_snd_soc_register_component(dev, &q6apm_fe_dai_component, NULL, 0);
952 }
953
954 #ifdef CONFIG_OF
955 static const struct of_device_id q6apm_dai_device_id[] = {
956 { .compatible = "qcom,q6apm-dais" },
957 {},
958 };
959 MODULE_DEVICE_TABLE(of, q6apm_dai_device_id);
960 #endif
961
962 static struct platform_driver q6apm_dai_platform_driver = {
963 .driver = {
964 .name = "q6apm-dai",
965 .of_match_table = of_match_ptr(q6apm_dai_device_id),
966 },
967 .probe = q6apm_dai_probe,
968 };
969 module_platform_driver(q6apm_dai_platform_driver);
970
971 MODULE_DESCRIPTION("Q6APM dai driver");
972 MODULE_LICENSE("GPL");
973