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