Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / intel / catpt / pcm.c
blobe5d54bb1c42acce16de57f4313d0ee338088f929
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2020 Intel Corporation. All rights reserved.
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
8 #include <linux/pm_runtime.h>
9 #include <sound/soc.h>
10 #include <sound/pcm_params.h>
11 #include <uapi/sound/tlv.h>
12 #include "core.h"
13 #include "messages.h"
15 struct catpt_stream_template {
16 enum catpt_path_id path_id;
17 enum catpt_stream_type type;
18 u32 persistent_size;
19 u8 num_entries;
20 struct catpt_module_entry entries[];
23 static struct catpt_stream_template system_pb = {
24 .path_id = CATPT_PATH_SSP0_OUT,
25 .type = CATPT_STRM_TYPE_SYSTEM,
26 .num_entries = 1,
27 .entries = {{ CATPT_MODID_PCM_SYSTEM, 0 }},
30 static struct catpt_stream_template system_cp = {
31 .path_id = CATPT_PATH_SSP0_IN,
32 .type = CATPT_STRM_TYPE_CAPTURE,
33 .num_entries = 1,
34 .entries = {{ CATPT_MODID_PCM_CAPTURE, 0 }},
37 static struct catpt_stream_template offload_pb = {
38 .path_id = CATPT_PATH_SSP0_OUT,
39 .type = CATPT_STRM_TYPE_RENDER,
40 .num_entries = 1,
41 .entries = {{ CATPT_MODID_PCM, 0 }},
44 static struct catpt_stream_template loopback_cp = {
45 .path_id = CATPT_PATH_SSP0_OUT,
46 .type = CATPT_STRM_TYPE_LOOPBACK,
47 .num_entries = 1,
48 .entries = {{ CATPT_MODID_PCM_REFERENCE, 0 }},
51 static struct catpt_stream_template bluetooth_pb = {
52 .path_id = CATPT_PATH_SSP1_OUT,
53 .type = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
54 .num_entries = 1,
55 .entries = {{ CATPT_MODID_BLUETOOTH_RENDER, 0 }},
58 static struct catpt_stream_template bluetooth_cp = {
59 .path_id = CATPT_PATH_SSP1_IN,
60 .type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE,
61 .num_entries = 1,
62 .entries = {{ CATPT_MODID_BLUETOOTH_CAPTURE, 0 }},
65 static struct catpt_stream_template *catpt_topology[] = {
66 [CATPT_STRM_TYPE_RENDER] = &offload_pb,
67 [CATPT_STRM_TYPE_SYSTEM] = &system_pb,
68 [CATPT_STRM_TYPE_CAPTURE] = &system_cp,
69 [CATPT_STRM_TYPE_LOOPBACK] = &loopback_cp,
70 [CATPT_STRM_TYPE_BLUETOOTH_RENDER] = &bluetooth_pb,
71 [CATPT_STRM_TYPE_BLUETOOTH_CAPTURE] = &bluetooth_cp,
74 static struct catpt_stream_template *
75 catpt_get_stream_template(struct snd_pcm_substream *substream)
77 struct snd_soc_pcm_runtime *rtm = substream->private_data;
78 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtm, 0);
79 enum catpt_stream_type type;
81 type = cpu_dai->driver->id;
83 /* account for capture in bidirectional dais */
84 switch (type) {
85 case CATPT_STRM_TYPE_SYSTEM:
86 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
87 type = CATPT_STRM_TYPE_CAPTURE;
88 break;
89 case CATPT_STRM_TYPE_BLUETOOTH_RENDER:
90 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
91 type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE;
92 break;
93 default:
94 break;
97 return catpt_topology[type];
100 struct catpt_stream_runtime *
101 catpt_stream_find(struct catpt_dev *cdev, u8 stream_hw_id)
103 struct catpt_stream_runtime *pos, *result = NULL;
105 spin_lock(&cdev->list_lock);
106 list_for_each_entry(pos, &cdev->stream_list, node) {
107 if (pos->info.stream_hw_id == stream_hw_id) {
108 result = pos;
109 break;
113 spin_unlock(&cdev->list_lock);
114 return result;
117 static u32 catpt_stream_read_position(struct catpt_dev *cdev,
118 struct catpt_stream_runtime *stream)
120 u32 pos;
122 memcpy_fromio(&pos, cdev->lpe_ba + stream->info.read_pos_regaddr,
123 sizeof(pos));
124 return pos;
127 static u32 catpt_stream_volume(struct catpt_dev *cdev,
128 struct catpt_stream_runtime *stream, u32 channel)
130 u32 volume, offset;
132 if (channel >= CATPT_CHANNELS_MAX)
133 channel = 0;
135 offset = stream->info.volume_regaddr[channel];
136 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
137 return volume;
140 static u32 catpt_mixer_volume(struct catpt_dev *cdev,
141 struct catpt_mixer_stream_info *info, u32 channel)
143 u32 volume, offset;
145 if (channel >= CATPT_CHANNELS_MAX)
146 channel = 0;
148 offset = info->volume_regaddr[channel];
149 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
150 return volume;
153 static void catpt_arrange_page_table(struct snd_pcm_substream *substream,
154 struct snd_dma_buffer *pgtbl)
156 struct snd_pcm_runtime *rtm = substream->runtime;
157 struct snd_dma_buffer *databuf = snd_pcm_get_dma_buf(substream);
158 int i, pages;
160 pages = snd_sgbuf_aligned_pages(rtm->dma_bytes);
162 for (i = 0; i < pages; i++) {
163 u32 pfn, offset;
164 u32 *page_table;
166 pfn = PFN_DOWN(snd_sgbuf_get_addr(databuf, i * PAGE_SIZE));
167 /* incrementing by 2 on even and 3 on odd */
168 offset = ((i << 2) + i) >> 1;
169 page_table = (u32 *)(pgtbl->area + offset);
171 if (i & 1)
172 *page_table |= (pfn << 4);
173 else
174 *page_table |= pfn;
178 static u32 catpt_get_channel_map(enum catpt_channel_config config)
180 switch (config) {
181 case CATPT_CHANNEL_CONFIG_MONO:
182 return GENMASK(31, 4) | CATPT_CHANNEL_CENTER;
184 case CATPT_CHANNEL_CONFIG_STEREO:
185 return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
186 | (CATPT_CHANNEL_RIGHT << 4);
188 case CATPT_CHANNEL_CONFIG_2_POINT_1:
189 return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
190 | (CATPT_CHANNEL_RIGHT << 4)
191 | (CATPT_CHANNEL_LFE << 8);
193 case CATPT_CHANNEL_CONFIG_3_POINT_0:
194 return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
195 | (CATPT_CHANNEL_CENTER << 4)
196 | (CATPT_CHANNEL_RIGHT << 8);
198 case CATPT_CHANNEL_CONFIG_3_POINT_1:
199 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
200 | (CATPT_CHANNEL_CENTER << 4)
201 | (CATPT_CHANNEL_RIGHT << 8)
202 | (CATPT_CHANNEL_LFE << 12);
204 case CATPT_CHANNEL_CONFIG_QUATRO:
205 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
206 | (CATPT_CHANNEL_RIGHT << 4)
207 | (CATPT_CHANNEL_LEFT_SURROUND << 8)
208 | (CATPT_CHANNEL_RIGHT_SURROUND << 12);
210 case CATPT_CHANNEL_CONFIG_4_POINT_0:
211 return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
212 | (CATPT_CHANNEL_CENTER << 4)
213 | (CATPT_CHANNEL_RIGHT << 8)
214 | (CATPT_CHANNEL_CENTER_SURROUND << 12);
216 case CATPT_CHANNEL_CONFIG_5_POINT_0:
217 return GENMASK(31, 20) | CATPT_CHANNEL_LEFT
218 | (CATPT_CHANNEL_CENTER << 4)
219 | (CATPT_CHANNEL_RIGHT << 8)
220 | (CATPT_CHANNEL_LEFT_SURROUND << 12)
221 | (CATPT_CHANNEL_RIGHT_SURROUND << 16);
223 case CATPT_CHANNEL_CONFIG_5_POINT_1:
224 return GENMASK(31, 24) | CATPT_CHANNEL_CENTER
225 | (CATPT_CHANNEL_LEFT << 4)
226 | (CATPT_CHANNEL_RIGHT << 8)
227 | (CATPT_CHANNEL_LEFT_SURROUND << 12)
228 | (CATPT_CHANNEL_RIGHT_SURROUND << 16)
229 | (CATPT_CHANNEL_LFE << 20);
231 case CATPT_CHANNEL_CONFIG_DUAL_MONO:
232 return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
233 | (CATPT_CHANNEL_LEFT << 4);
235 default:
236 return U32_MAX;
240 static enum catpt_channel_config catpt_get_channel_config(u32 num_channels)
242 switch (num_channels) {
243 case 6:
244 return CATPT_CHANNEL_CONFIG_5_POINT_1;
245 case 5:
246 return CATPT_CHANNEL_CONFIG_5_POINT_0;
247 case 4:
248 return CATPT_CHANNEL_CONFIG_QUATRO;
249 case 3:
250 return CATPT_CHANNEL_CONFIG_2_POINT_1;
251 case 1:
252 return CATPT_CHANNEL_CONFIG_MONO;
253 case 2:
254 default:
255 return CATPT_CHANNEL_CONFIG_STEREO;
259 static int catpt_dai_startup(struct snd_pcm_substream *substream,
260 struct snd_soc_dai *dai)
262 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
263 struct catpt_stream_template *template;
264 struct catpt_stream_runtime *stream;
265 struct resource *res;
266 int ret;
268 template = catpt_get_stream_template(substream);
270 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
271 if (!stream)
272 return -ENOMEM;
274 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, cdev->dev, PAGE_SIZE,
275 &stream->pgtbl);
276 if (ret)
277 goto err_pgtbl;
279 res = catpt_request_region(&cdev->dram, template->persistent_size);
280 if (!res) {
281 ret = -EBUSY;
282 goto err_request;
285 catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
287 stream->template = template;
288 stream->persistent = res;
289 stream->substream = substream;
290 INIT_LIST_HEAD(&stream->node);
291 snd_soc_dai_set_dma_data(dai, substream, stream);
293 spin_lock(&cdev->list_lock);
294 list_add_tail(&stream->node, &cdev->stream_list);
295 spin_unlock(&cdev->list_lock);
297 return 0;
299 err_request:
300 snd_dma_free_pages(&stream->pgtbl);
301 err_pgtbl:
302 kfree(stream);
303 return ret;
306 static void catpt_dai_shutdown(struct snd_pcm_substream *substream,
307 struct snd_soc_dai *dai)
309 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
310 struct catpt_stream_runtime *stream;
312 stream = snd_soc_dai_get_dma_data(dai, substream);
314 spin_lock(&cdev->list_lock);
315 list_del(&stream->node);
316 spin_unlock(&cdev->list_lock);
318 release_resource(stream->persistent);
319 kfree(stream->persistent);
320 catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
322 snd_dma_free_pages(&stream->pgtbl);
323 kfree(stream);
324 snd_soc_dai_set_dma_data(dai, substream, NULL);
327 static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol);
329 static int catpt_dai_apply_usettings(struct snd_soc_dai *dai,
330 struct catpt_stream_runtime *stream)
332 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
333 struct snd_soc_component *component = dai->component;
334 struct snd_kcontrol *pos, *kctl = NULL;
335 const char *name;
336 int ret;
337 u32 id = stream->info.stream_hw_id;
339 /* only selected streams have individual controls */
340 switch (id) {
341 case CATPT_PIN_ID_OFFLOAD1:
342 name = "Media0 Playback Volume";
343 break;
344 case CATPT_PIN_ID_OFFLOAD2:
345 name = "Media1 Playback Volume";
346 break;
347 case CATPT_PIN_ID_CAPTURE1:
348 name = "Mic Capture Volume";
349 break;
350 case CATPT_PIN_ID_REFERENCE:
351 name = "Loopback Mute";
352 break;
353 default:
354 return 0;
357 list_for_each_entry(pos, &component->card->snd_card->controls, list) {
358 if (pos->private_data == component &&
359 !strncmp(name, pos->id.name, sizeof(pos->id.name))) {
360 kctl = pos;
361 break;
364 if (!kctl)
365 return -ENOENT;
367 if (stream->template->type != CATPT_STRM_TYPE_LOOPBACK)
368 return catpt_set_dspvol(cdev, id, (long *)kctl->private_value);
369 ret = catpt_ipc_mute_loopback(cdev, id, *(bool *)kctl->private_value);
370 if (ret)
371 return CATPT_IPC_ERROR(ret);
372 return 0;
375 static int catpt_dai_hw_params(struct snd_pcm_substream *substream,
376 struct snd_pcm_hw_params *params,
377 struct snd_soc_dai *dai)
379 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
380 struct catpt_stream_runtime *stream;
381 struct catpt_audio_format afmt;
382 struct catpt_ring_info rinfo;
383 struct snd_pcm_runtime *rtm = substream->runtime;
384 struct snd_dma_buffer *dmab;
385 int ret;
387 stream = snd_soc_dai_get_dma_data(dai, substream);
388 if (stream->allocated)
389 return 0;
391 memset(&afmt, 0, sizeof(afmt));
392 afmt.sample_rate = params_rate(params);
393 afmt.bit_depth = params_physical_width(params);
394 afmt.valid_bit_depth = params_width(params);
395 afmt.num_channels = params_channels(params);
396 afmt.channel_config = catpt_get_channel_config(afmt.num_channels);
397 afmt.channel_map = catpt_get_channel_map(afmt.channel_config);
398 afmt.interleaving = CATPT_INTERLEAVING_PER_CHANNEL;
400 dmab = snd_pcm_get_dma_buf(substream);
401 catpt_arrange_page_table(substream, &stream->pgtbl);
403 memset(&rinfo, 0, sizeof(rinfo));
404 rinfo.page_table_addr = stream->pgtbl.addr;
405 rinfo.num_pages = DIV_ROUND_UP(rtm->dma_bytes, PAGE_SIZE);
406 rinfo.size = rtm->dma_bytes;
407 rinfo.offset = 0;
408 rinfo.ring_first_page_pfn = PFN_DOWN(snd_sgbuf_get_addr(dmab, 0));
410 ret = catpt_ipc_alloc_stream(cdev, stream->template->path_id,
411 stream->template->type,
412 &afmt, &rinfo,
413 stream->template->num_entries,
414 stream->template->entries,
415 stream->persistent,
416 cdev->scratch,
417 &stream->info);
418 if (ret)
419 return CATPT_IPC_ERROR(ret);
421 ret = catpt_dai_apply_usettings(dai, stream);
422 if (ret)
423 return ret;
425 stream->allocated = true;
426 return 0;
429 static int catpt_dai_hw_free(struct snd_pcm_substream *substream,
430 struct snd_soc_dai *dai)
432 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
433 struct catpt_stream_runtime *stream;
435 stream = snd_soc_dai_get_dma_data(dai, substream);
436 if (!stream->allocated)
437 return 0;
439 catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
440 catpt_ipc_free_stream(cdev, stream->info.stream_hw_id);
442 stream->allocated = false;
443 return 0;
446 static int catpt_dai_prepare(struct snd_pcm_substream *substream,
447 struct snd_soc_dai *dai)
449 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
450 struct catpt_stream_runtime *stream;
451 int ret;
453 stream = snd_soc_dai_get_dma_data(dai, substream);
454 if (stream->prepared)
455 return 0;
457 ret = catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
458 if (ret)
459 return CATPT_IPC_ERROR(ret);
461 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
462 if (ret)
463 return CATPT_IPC_ERROR(ret);
465 stream->prepared = true;
466 return 0;
469 static int catpt_dai_trigger(struct snd_pcm_substream *substream, int cmd,
470 struct snd_soc_dai *dai)
472 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
473 struct catpt_stream_runtime *stream;
474 struct snd_pcm_runtime *runtime = substream->runtime;
475 snd_pcm_uframes_t pos;
476 int ret;
478 stream = snd_soc_dai_get_dma_data(dai, substream);
480 switch (cmd) {
481 case SNDRV_PCM_TRIGGER_START:
482 /* only offload is set_write_pos driven */
483 if (stream->template->type != CATPT_STRM_TYPE_RENDER)
484 goto resume_stream;
486 pos = frames_to_bytes(runtime, runtime->start_threshold);
488 * Dsp operates on buffer halves, thus max 2x set_write_pos
489 * (entire buffer filled) prior to stream start.
491 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
492 pos, false, false);
493 if (ret)
494 return CATPT_IPC_ERROR(ret);
495 fallthrough;
496 case SNDRV_PCM_TRIGGER_RESUME:
497 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
498 resume_stream:
499 catpt_dsp_update_lpclock(cdev);
500 ret = catpt_ipc_resume_stream(cdev, stream->info.stream_hw_id);
501 if (ret)
502 return CATPT_IPC_ERROR(ret);
503 break;
505 case SNDRV_PCM_TRIGGER_STOP:
506 stream->prepared = false;
507 fallthrough;
508 case SNDRV_PCM_TRIGGER_SUSPEND:
509 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
510 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
511 catpt_dsp_update_lpclock(cdev);
512 if (ret)
513 return CATPT_IPC_ERROR(ret);
514 break;
516 default:
517 break;
520 return 0;
523 void catpt_stream_update_position(struct catpt_dev *cdev,
524 struct catpt_stream_runtime *stream,
525 struct catpt_notify_position *pos)
527 struct snd_pcm_substream *substream = stream->substream;
528 struct snd_pcm_runtime *r = substream->runtime;
529 snd_pcm_uframes_t dsppos, newpos;
530 int ret;
532 dsppos = bytes_to_frames(r, pos->stream_position);
534 if (!stream->prepared)
535 goto exit;
536 /* only offload is set_write_pos driven */
537 if (stream->template->type != CATPT_STRM_TYPE_RENDER)
538 goto exit;
540 if (dsppos >= r->buffer_size / 2)
541 newpos = r->buffer_size / 2;
542 else
543 newpos = 0;
545 * Dsp operates on buffer halves, thus on every notify position
546 * (buffer half consumed) update wp to allow stream progression.
548 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
549 frames_to_bytes(r, newpos),
550 false, false);
551 if (ret) {
552 dev_err(cdev->dev, "update position for stream %d failed: %d\n",
553 stream->info.stream_hw_id, ret);
554 return;
556 exit:
557 snd_pcm_period_elapsed(substream);
560 /* 200 ms for 2 32-bit channels at 48kHz (native format) */
561 #define CATPT_BUFFER_MAX_SIZE 76800
562 #define CATPT_PCM_PERIODS_MAX 4
563 #define CATPT_PCM_PERIODS_MIN 2
565 static const struct snd_pcm_hardware catpt_pcm_hardware = {
566 .info = SNDRV_PCM_INFO_MMAP |
567 SNDRV_PCM_INFO_MMAP_VALID |
568 SNDRV_PCM_INFO_INTERLEAVED |
569 SNDRV_PCM_INFO_PAUSE |
570 SNDRV_PCM_INFO_RESUME |
571 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
572 .formats = SNDRV_PCM_FMTBIT_S16_LE |
573 SNDRV_PCM_FMTBIT_S24_LE |
574 SNDRV_PCM_FMTBIT_S32_LE,
575 .period_bytes_min = PAGE_SIZE,
576 .period_bytes_max = CATPT_BUFFER_MAX_SIZE / CATPT_PCM_PERIODS_MIN,
577 .periods_min = CATPT_PCM_PERIODS_MIN,
578 .periods_max = CATPT_PCM_PERIODS_MAX,
579 .buffer_bytes_max = CATPT_BUFFER_MAX_SIZE,
582 static int catpt_component_pcm_construct(struct snd_soc_component *component,
583 struct snd_soc_pcm_runtime *rtm)
585 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
587 snd_pcm_set_managed_buffer_all(rtm->pcm, SNDRV_DMA_TYPE_DEV_SG,
588 cdev->dev,
589 catpt_pcm_hardware.buffer_bytes_max,
590 catpt_pcm_hardware.buffer_bytes_max);
592 return 0;
595 static int catpt_component_open(struct snd_soc_component *component,
596 struct snd_pcm_substream *substream)
598 struct snd_soc_pcm_runtime *rtm = substream->private_data;
600 if (rtm->dai_link->no_pcm)
601 return 0;
602 snd_soc_set_runtime_hwparams(substream, &catpt_pcm_hardware);
603 return 0;
606 static snd_pcm_uframes_t
607 catpt_component_pointer(struct snd_soc_component *component,
608 struct snd_pcm_substream *substream)
610 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
611 struct catpt_stream_runtime *stream;
612 struct snd_soc_pcm_runtime *rtm = substream->private_data;
613 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtm, 0);
614 u32 pos;
616 if (rtm->dai_link->no_pcm)
617 return 0;
619 stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
620 pos = catpt_stream_read_position(cdev, stream);
622 return bytes_to_frames(substream->runtime, pos);
625 static const struct snd_soc_dai_ops catpt_fe_dai_ops = {
626 .startup = catpt_dai_startup,
627 .shutdown = catpt_dai_shutdown,
628 .hw_params = catpt_dai_hw_params,
629 .hw_free = catpt_dai_hw_free,
630 .prepare = catpt_dai_prepare,
631 .trigger = catpt_dai_trigger,
634 static int catpt_dai_pcm_new(struct snd_soc_pcm_runtime *rtm,
635 struct snd_soc_dai *dai)
637 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtm, 0);
638 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
639 struct catpt_ssp_device_format devfmt;
640 int ret;
642 devfmt.iface = dai->driver->id;
643 devfmt.channels = codec_dai->driver->capture.channels_max;
645 switch (devfmt.iface) {
646 case CATPT_SSP_IFACE_0:
647 devfmt.mclk = CATPT_MCLK_FREQ_24_MHZ;
649 switch (devfmt.channels) {
650 case 4:
651 devfmt.mode = CATPT_SSP_MODE_TDM_PROVIDER;
652 devfmt.clock_divider = 4;
653 break;
654 case 2:
655 default:
656 devfmt.mode = CATPT_SSP_MODE_I2S_PROVIDER;
657 devfmt.clock_divider = 9;
658 break;
660 break;
662 case CATPT_SSP_IFACE_1:
663 devfmt.mclk = CATPT_MCLK_OFF;
664 devfmt.mode = CATPT_SSP_MODE_I2S_CONSUMER;
665 devfmt.clock_divider = 0;
666 break;
669 /* see if this is a new configuration */
670 if (!memcmp(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt)))
671 return 0;
673 pm_runtime_get_sync(cdev->dev);
675 ret = catpt_ipc_set_device_format(cdev, &devfmt);
677 pm_runtime_mark_last_busy(cdev->dev);
678 pm_runtime_put_autosuspend(cdev->dev);
680 if (ret)
681 return CATPT_IPC_ERROR(ret);
683 /* store device format set for given SSP */
684 memcpy(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt));
685 return 0;
688 static struct snd_soc_dai_driver dai_drivers[] = {
689 /* FE DAIs */
691 .name = "System Pin",
692 .id = CATPT_STRM_TYPE_SYSTEM,
693 .ops = &catpt_fe_dai_ops,
694 .playback = {
695 .stream_name = "System Playback",
696 .channels_min = 2,
697 .channels_max = 2,
698 .rates = SNDRV_PCM_RATE_48000,
699 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
701 .capture = {
702 .stream_name = "Analog Capture",
703 .channels_min = 2,
704 .channels_max = 4,
705 .rates = SNDRV_PCM_RATE_48000,
706 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
710 .name = "Offload0 Pin",
711 .id = CATPT_STRM_TYPE_RENDER,
712 .ops = &catpt_fe_dai_ops,
713 .playback = {
714 .stream_name = "Offload0 Playback",
715 .channels_min = 2,
716 .channels_max = 2,
717 .rates = SNDRV_PCM_RATE_8000_192000,
718 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
722 .name = "Offload1 Pin",
723 .id = CATPT_STRM_TYPE_RENDER,
724 .ops = &catpt_fe_dai_ops,
725 .playback = {
726 .stream_name = "Offload1 Playback",
727 .channels_min = 2,
728 .channels_max = 2,
729 .rates = SNDRV_PCM_RATE_8000_192000,
730 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
734 .name = "Loopback Pin",
735 .id = CATPT_STRM_TYPE_LOOPBACK,
736 .ops = &catpt_fe_dai_ops,
737 .capture = {
738 .stream_name = "Loopback Capture",
739 .channels_min = 2,
740 .channels_max = 2,
741 .rates = SNDRV_PCM_RATE_48000,
742 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
746 .name = "Bluetooth Pin",
747 .id = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
748 .ops = &catpt_fe_dai_ops,
749 .playback = {
750 .stream_name = "Bluetooth Playback",
751 .channels_min = 1,
752 .channels_max = 1,
753 .rates = SNDRV_PCM_RATE_8000,
754 .formats = SNDRV_PCM_FMTBIT_S16_LE,
756 .capture = {
757 .stream_name = "Bluetooth Capture",
758 .channels_min = 1,
759 .channels_max = 1,
760 .rates = SNDRV_PCM_RATE_8000,
761 .formats = SNDRV_PCM_FMTBIT_S16_LE,
764 /* BE DAIs */
766 .name = "ssp0-port",
767 .id = CATPT_SSP_IFACE_0,
768 .pcm_new = catpt_dai_pcm_new,
769 .playback = {
770 .channels_min = 1,
771 .channels_max = 8,
773 .capture = {
774 .channels_min = 1,
775 .channels_max = 8,
779 .name = "ssp1-port",
780 .id = CATPT_SSP_IFACE_1,
781 .pcm_new = catpt_dai_pcm_new,
782 .playback = {
783 .channels_min = 1,
784 .channels_max = 8,
786 .capture = {
787 .channels_min = 1,
788 .channels_max = 8,
793 #define DSP_VOLUME_MAX S32_MAX /* 0db */
794 #define DSP_VOLUME_STEP_MAX 30
796 static u32 ctlvol_to_dspvol(u32 value)
798 if (value > DSP_VOLUME_STEP_MAX)
799 value = 0;
800 return DSP_VOLUME_MAX >> (DSP_VOLUME_STEP_MAX - value);
803 static u32 dspvol_to_ctlvol(u32 volume)
805 if (volume > DSP_VOLUME_MAX)
806 return DSP_VOLUME_STEP_MAX;
807 return volume ? __fls(volume) : 0;
810 static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol)
812 u32 dspvol;
813 int ret, i;
815 for (i = 1; i < CATPT_CHANNELS_MAX; i++)
816 if (ctlvol[i] != ctlvol[0])
817 break;
819 if (i == CATPT_CHANNELS_MAX) {
820 dspvol = ctlvol_to_dspvol(ctlvol[0]);
822 ret = catpt_ipc_set_volume(cdev, stream_id,
823 CATPT_ALL_CHANNELS_MASK, dspvol,
824 0, CATPT_AUDIO_CURVE_NONE);
825 } else {
826 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
827 dspvol = ctlvol_to_dspvol(ctlvol[i]);
829 ret = catpt_ipc_set_volume(cdev, stream_id,
830 i, dspvol,
831 0, CATPT_AUDIO_CURVE_NONE);
832 if (ret)
833 break;
837 if (ret)
838 return CATPT_IPC_ERROR(ret);
839 return 0;
842 static int catpt_volume_info(struct snd_kcontrol *kcontrol,
843 struct snd_ctl_elem_info *uinfo)
845 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
846 uinfo->count = CATPT_CHANNELS_MAX;
847 uinfo->value.integer.min = 0;
848 uinfo->value.integer.max = DSP_VOLUME_STEP_MAX;
849 return 0;
852 static int catpt_mixer_volume_get(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
855 struct snd_soc_component *component =
856 snd_soc_kcontrol_component(kcontrol);
857 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
858 u32 dspvol;
859 int i;
861 pm_runtime_get_sync(cdev->dev);
863 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
864 dspvol = catpt_mixer_volume(cdev, &cdev->mixer, i);
865 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
868 pm_runtime_mark_last_busy(cdev->dev);
869 pm_runtime_put_autosuspend(cdev->dev);
871 return 0;
874 static int catpt_mixer_volume_put(struct snd_kcontrol *kcontrol,
875 struct snd_ctl_elem_value *ucontrol)
877 struct snd_soc_component *component =
878 snd_soc_kcontrol_component(kcontrol);
879 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
880 int ret;
882 pm_runtime_get_sync(cdev->dev);
884 ret = catpt_set_dspvol(cdev, cdev->mixer.mixer_hw_id,
885 ucontrol->value.integer.value);
887 pm_runtime_mark_last_busy(cdev->dev);
888 pm_runtime_put_autosuspend(cdev->dev);
890 return ret;
893 static int catpt_stream_volume_get(struct snd_kcontrol *kcontrol,
894 struct snd_ctl_elem_value *ucontrol,
895 enum catpt_pin_id pin_id)
897 struct snd_soc_component *component =
898 snd_soc_kcontrol_component(kcontrol);
899 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
900 struct catpt_stream_runtime *stream;
901 long *ctlvol = (long *)kcontrol->private_value;
902 u32 dspvol;
903 int i;
905 stream = catpt_stream_find(cdev, pin_id);
906 if (!stream) {
907 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
908 ucontrol->value.integer.value[i] = ctlvol[i];
909 return 0;
912 pm_runtime_get_sync(cdev->dev);
914 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
915 dspvol = catpt_stream_volume(cdev, stream, i);
916 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
919 pm_runtime_mark_last_busy(cdev->dev);
920 pm_runtime_put_autosuspend(cdev->dev);
922 return 0;
925 static int catpt_stream_volume_put(struct snd_kcontrol *kcontrol,
926 struct snd_ctl_elem_value *ucontrol,
927 enum catpt_pin_id pin_id)
929 struct snd_soc_component *component =
930 snd_soc_kcontrol_component(kcontrol);
931 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
932 struct catpt_stream_runtime *stream;
933 long *ctlvol = (long *)kcontrol->private_value;
934 int ret, i;
936 stream = catpt_stream_find(cdev, pin_id);
937 if (!stream) {
938 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
939 ctlvol[i] = ucontrol->value.integer.value[i];
940 return 0;
943 pm_runtime_get_sync(cdev->dev);
945 ret = catpt_set_dspvol(cdev, stream->info.stream_hw_id,
946 ucontrol->value.integer.value);
948 pm_runtime_mark_last_busy(cdev->dev);
949 pm_runtime_put_autosuspend(cdev->dev);
951 if (ret)
952 return ret;
954 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
955 ctlvol[i] = ucontrol->value.integer.value[i];
956 return 0;
959 static int catpt_offload1_volume_get(struct snd_kcontrol *kctl,
960 struct snd_ctl_elem_value *uctl)
962 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
965 static int catpt_offload1_volume_put(struct snd_kcontrol *kctl,
966 struct snd_ctl_elem_value *uctl)
968 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
971 static int catpt_offload2_volume_get(struct snd_kcontrol *kctl,
972 struct snd_ctl_elem_value *uctl)
974 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
977 static int catpt_offload2_volume_put(struct snd_kcontrol *kctl,
978 struct snd_ctl_elem_value *uctl)
980 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
983 static int catpt_capture_volume_get(struct snd_kcontrol *kctl,
984 struct snd_ctl_elem_value *uctl)
986 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
989 static int catpt_capture_volume_put(struct snd_kcontrol *kctl,
990 struct snd_ctl_elem_value *uctl)
992 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
995 static int catpt_loopback_switch_get(struct snd_kcontrol *kcontrol,
996 struct snd_ctl_elem_value *ucontrol)
998 ucontrol->value.integer.value[0] = *(bool *)kcontrol->private_value;
999 return 0;
1002 static int catpt_loopback_switch_put(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_value *ucontrol)
1005 struct snd_soc_component *component =
1006 snd_soc_kcontrol_component(kcontrol);
1007 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
1008 struct catpt_stream_runtime *stream;
1009 bool mute;
1010 int ret;
1012 mute = (bool)ucontrol->value.integer.value[0];
1013 stream = catpt_stream_find(cdev, CATPT_PIN_ID_REFERENCE);
1014 if (!stream) {
1015 *(bool *)kcontrol->private_value = mute;
1016 return 0;
1019 pm_runtime_get_sync(cdev->dev);
1021 ret = catpt_ipc_mute_loopback(cdev, stream->info.stream_hw_id, mute);
1023 pm_runtime_mark_last_busy(cdev->dev);
1024 pm_runtime_put_autosuspend(cdev->dev);
1026 if (ret)
1027 return CATPT_IPC_ERROR(ret);
1029 *(bool *)kcontrol->private_value = mute;
1030 return 0;
1033 static int catpt_waves_switch_get(struct snd_kcontrol *kcontrol,
1034 struct snd_ctl_elem_value *ucontrol)
1036 return 0;
1039 static int catpt_waves_switch_put(struct snd_kcontrol *kcontrol,
1040 struct snd_ctl_elem_value *ucontrol)
1042 return 0;
1045 static int catpt_waves_param_get(struct snd_kcontrol *kcontrol,
1046 unsigned int __user *bytes,
1047 unsigned int size)
1049 return 0;
1052 static int catpt_waves_param_put(struct snd_kcontrol *kcontrol,
1053 const unsigned int __user *bytes,
1054 unsigned int size)
1056 return 0;
1059 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(catpt_volume_tlv, -9000, 300, 1);
1061 #define CATPT_VOLUME_CTL(kname, sname) \
1062 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1063 .name = (kname), \
1064 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1065 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1066 .info = catpt_volume_info, \
1067 .get = catpt_##sname##_volume_get, \
1068 .put = catpt_##sname##_volume_put, \
1069 .tlv.p = catpt_volume_tlv, \
1070 .private_value = (unsigned long) \
1071 &(long[CATPT_CHANNELS_MAX]) {0} }
1073 static const struct snd_kcontrol_new component_kcontrols[] = {
1074 /* Master volume (mixer stream) */
1075 CATPT_VOLUME_CTL("Master Playback Volume", mixer),
1076 /* Individual volume controls for offload and capture */
1077 CATPT_VOLUME_CTL("Media0 Playback Volume", offload1),
1078 CATPT_VOLUME_CTL("Media1 Playback Volume", offload2),
1079 CATPT_VOLUME_CTL("Mic Capture Volume", capture),
1080 SOC_SINGLE_BOOL_EXT("Loopback Mute", (unsigned long)&(bool[1]) {0},
1081 catpt_loopback_switch_get, catpt_loopback_switch_put),
1082 /* Enable or disable WAVES module */
1083 SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
1084 catpt_waves_switch_get, catpt_waves_switch_put),
1085 /* WAVES module parameter control */
1086 SND_SOC_BYTES_TLV("Waves Set Param", 128,
1087 catpt_waves_param_get, catpt_waves_param_put),
1090 static const struct snd_soc_dapm_widget component_widgets[] = {
1091 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1092 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1093 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1094 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1096 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1099 static const struct snd_soc_dapm_route component_routes[] = {
1100 {"Playback VMixer", NULL, "System Playback"},
1101 {"Playback VMixer", NULL, "Offload0 Playback"},
1102 {"Playback VMixer", NULL, "Offload1 Playback"},
1104 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
1106 {"Analog Capture", NULL, "SSP0 CODEC IN"},
1107 {"Loopback Capture", NULL, "SSP0 CODEC IN"},
1109 {"SSP1 BT OUT", NULL, "Bluetooth Playback"},
1110 {"Bluetooth Capture", NULL, "SSP1 BT IN"},
1113 static const struct snd_soc_component_driver catpt_comp_driver = {
1114 .name = "catpt-platform",
1116 .pcm_construct = catpt_component_pcm_construct,
1117 .open = catpt_component_open,
1118 .pointer = catpt_component_pointer,
1120 .controls = component_kcontrols,
1121 .num_controls = ARRAY_SIZE(component_kcontrols),
1122 .dapm_widgets = component_widgets,
1123 .num_dapm_widgets = ARRAY_SIZE(component_widgets),
1124 .dapm_routes = component_routes,
1125 .num_dapm_routes = ARRAY_SIZE(component_routes),
1128 int catpt_arm_stream_templates(struct catpt_dev *cdev)
1130 struct resource *res;
1131 u32 scratch_size = 0;
1132 int i, j;
1134 for (i = 0; i < ARRAY_SIZE(catpt_topology); i++) {
1135 struct catpt_stream_template *template;
1136 struct catpt_module_entry *entry;
1137 struct catpt_module_type *type;
1139 template = catpt_topology[i];
1140 template->persistent_size = 0;
1142 for (j = 0; j < template->num_entries; j++) {
1143 entry = &template->entries[j];
1144 type = &cdev->modules[entry->module_id];
1146 if (!type->loaded)
1147 return -ENOENT;
1149 entry->entry_point = type->entry_point;
1150 template->persistent_size += type->persistent_size;
1151 if (type->scratch_size > scratch_size)
1152 scratch_size = type->scratch_size;
1156 if (scratch_size) {
1157 /* allocate single scratch area for all modules */
1158 res = catpt_request_region(&cdev->dram, scratch_size);
1159 if (!res)
1160 return -EBUSY;
1161 cdev->scratch = res;
1164 return 0;
1167 int catpt_register_plat_component(struct catpt_dev *cdev)
1169 struct snd_soc_component *component;
1170 int ret;
1172 component = devm_kzalloc(cdev->dev, sizeof(*component), GFP_KERNEL);
1173 if (!component)
1174 return -ENOMEM;
1176 ret = snd_soc_component_initialize(component, &catpt_comp_driver,
1177 cdev->dev);
1178 if (ret)
1179 return ret;
1181 component->name = catpt_comp_driver.name;
1182 return snd_soc_add_component(component, dai_drivers,
1183 ARRAY_SIZE(dai_drivers));