1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2020 Intel Corporation. All rights reserved.
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
8 #include <linux/pm_runtime.h>
10 #include <sound/pcm_params.h>
11 #include <uapi/sound/tlv.h>
15 struct catpt_stream_template {
16 enum catpt_path_id path_id;
17 enum catpt_stream_type type;
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,
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,
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,
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,
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,
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,
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 = snd_soc_substream_to_rtd(substream);
78 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0);
79 enum catpt_stream_type type;
81 type = cpu_dai->driver->id;
83 /* account for capture in bidirectional dais */
85 case CATPT_STRM_TYPE_SYSTEM:
86 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
87 type = CATPT_STRM_TYPE_CAPTURE;
89 case CATPT_STRM_TYPE_BLUETOOTH_RENDER:
90 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
91 type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE;
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) {
113 spin_unlock(&cdev->list_lock);
117 static u32 catpt_stream_read_position(struct catpt_dev *cdev,
118 struct catpt_stream_runtime *stream)
122 memcpy_fromio(&pos, cdev->lpe_ba + stream->info.read_pos_regaddr,
127 static u32 catpt_stream_volume(struct catpt_dev *cdev,
128 struct catpt_stream_runtime *stream, u32 channel)
132 if (channel >= CATPT_CHANNELS_MAX)
135 offset = stream->info.volume_regaddr[channel];
136 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
140 static u32 catpt_mixer_volume(struct catpt_dev *cdev,
141 struct catpt_mixer_stream_info *info, u32 channel)
145 if (channel >= CATPT_CHANNELS_MAX)
148 offset = info->volume_regaddr[channel];
149 memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(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);
160 pages = snd_sgbuf_aligned_pages(rtm->dma_bytes);
162 for (i = 0; i < pages; i++) {
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);
172 *page_table |= (pfn << 4);
178 static u32 catpt_get_channel_map(enum catpt_channel_config 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);
240 static enum catpt_channel_config catpt_get_channel_config(u32 num_channels)
242 switch (num_channels) {
244 return CATPT_CHANNEL_CONFIG_5_POINT_1;
246 return CATPT_CHANNEL_CONFIG_5_POINT_0;
248 return CATPT_CHANNEL_CONFIG_QUATRO;
250 return CATPT_CHANNEL_CONFIG_2_POINT_1;
252 return CATPT_CHANNEL_CONFIG_MONO;
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_stream_template *template;
263 struct catpt_stream_runtime *stream;
264 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
265 struct resource *res;
268 template = catpt_get_stream_template(substream);
270 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
274 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, cdev->dev, PAGE_SIZE,
279 res = catpt_request_region(&cdev->dram, template->persistent_size);
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);
300 snd_dma_free_pages(&stream->pgtbl);
306 static void catpt_dai_shutdown(struct snd_pcm_substream *substream,
307 struct snd_soc_dai *dai)
309 struct catpt_stream_runtime *stream;
310 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
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);
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 snd_soc_component *component = dai->component;
333 struct snd_kcontrol *pos;
334 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
337 u32 id = stream->info.stream_hw_id;
339 /* only selected streams have individual controls */
341 case CATPT_PIN_ID_OFFLOAD1:
342 name = "Media0 Playback Volume";
344 case CATPT_PIN_ID_OFFLOAD2:
345 name = "Media1 Playback Volume";
347 case CATPT_PIN_ID_CAPTURE1:
348 name = "Mic Capture Volume";
350 case CATPT_PIN_ID_REFERENCE:
351 name = "Loopback Mute";
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)))
362 if (list_entry_is_head(pos, &component->card->snd_card->controls, list))
365 if (stream->template->type != CATPT_STRM_TYPE_LOOPBACK)
366 return catpt_set_dspvol(cdev, id, (long *)pos->private_value);
367 ret = catpt_ipc_mute_loopback(cdev, id, *(bool *)pos->private_value);
369 return CATPT_IPC_ERROR(ret);
373 static int catpt_dai_hw_params(struct snd_pcm_substream *substream,
374 struct snd_pcm_hw_params *params,
375 struct snd_soc_dai *dai)
377 struct snd_pcm_runtime *rtm = substream->runtime;
378 struct snd_dma_buffer *dmab;
379 struct catpt_stream_runtime *stream;
380 struct catpt_audio_format afmt;
381 struct catpt_ring_info rinfo;
382 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
385 stream = snd_soc_dai_get_dma_data(dai, substream);
386 if (stream->allocated)
389 memset(&afmt, 0, sizeof(afmt));
390 afmt.sample_rate = params_rate(params);
391 afmt.bit_depth = params_physical_width(params);
392 afmt.valid_bit_depth = params_width(params);
393 afmt.num_channels = params_channels(params);
394 afmt.channel_config = catpt_get_channel_config(afmt.num_channels);
395 afmt.channel_map = catpt_get_channel_map(afmt.channel_config);
396 afmt.interleaving = CATPT_INTERLEAVING_PER_CHANNEL;
398 dmab = snd_pcm_get_dma_buf(substream);
399 catpt_arrange_page_table(substream, &stream->pgtbl);
401 memset(&rinfo, 0, sizeof(rinfo));
402 rinfo.page_table_addr = stream->pgtbl.addr;
403 rinfo.num_pages = DIV_ROUND_UP(rtm->dma_bytes, PAGE_SIZE);
404 rinfo.size = rtm->dma_bytes;
406 rinfo.ring_first_page_pfn = PFN_DOWN(snd_sgbuf_get_addr(dmab, 0));
408 ret = catpt_ipc_alloc_stream(cdev, stream->template->path_id,
409 stream->template->type,
411 stream->template->num_entries,
412 stream->template->entries,
417 return CATPT_IPC_ERROR(ret);
419 ret = catpt_dai_apply_usettings(dai, stream);
423 stream->allocated = true;
427 static int catpt_dai_hw_free(struct snd_pcm_substream *substream,
428 struct snd_soc_dai *dai)
430 struct catpt_stream_runtime *stream;
431 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
433 stream = snd_soc_dai_get_dma_data(dai, substream);
434 if (!stream->allocated)
437 catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
438 catpt_ipc_free_stream(cdev, stream->info.stream_hw_id);
440 stream->allocated = false;
444 static int catpt_dai_prepare(struct snd_pcm_substream *substream,
445 struct snd_soc_dai *dai)
447 struct catpt_stream_runtime *stream;
448 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
451 stream = snd_soc_dai_get_dma_data(dai, substream);
452 if (stream->prepared)
455 ret = catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
457 return CATPT_IPC_ERROR(ret);
459 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
461 return CATPT_IPC_ERROR(ret);
463 stream->prepared = true;
467 static int catpt_dai_trigger(struct snd_pcm_substream *substream, int cmd,
468 struct snd_soc_dai *dai)
470 struct snd_pcm_runtime *runtime = substream->runtime;
471 struct catpt_stream_runtime *stream;
472 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
473 snd_pcm_uframes_t pos;
476 stream = snd_soc_dai_get_dma_data(dai, substream);
479 case SNDRV_PCM_TRIGGER_START:
480 /* only offload is set_write_pos driven */
481 if (stream->template->type != CATPT_STRM_TYPE_RENDER)
484 pos = frames_to_bytes(runtime, runtime->start_threshold);
486 * Dsp operates on buffer halves, thus max 2x set_write_pos
487 * (entire buffer filled) prior to stream start.
489 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
492 return CATPT_IPC_ERROR(ret);
494 case SNDRV_PCM_TRIGGER_RESUME:
495 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
497 catpt_dsp_update_lpclock(cdev);
498 ret = catpt_ipc_resume_stream(cdev, stream->info.stream_hw_id);
500 return CATPT_IPC_ERROR(ret);
503 case SNDRV_PCM_TRIGGER_STOP:
504 stream->prepared = false;
506 case SNDRV_PCM_TRIGGER_SUSPEND:
507 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
508 ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
509 catpt_dsp_update_lpclock(cdev);
511 return CATPT_IPC_ERROR(ret);
521 void catpt_stream_update_position(struct catpt_dev *cdev,
522 struct catpt_stream_runtime *stream,
523 struct catpt_notify_position *pos)
525 struct snd_pcm_substream *substream = stream->substream;
526 struct snd_pcm_runtime *r = substream->runtime;
527 snd_pcm_uframes_t dsppos, newpos;
530 dsppos = bytes_to_frames(r, pos->stream_position);
532 if (!stream->prepared)
534 /* only offload is set_write_pos driven */
535 if (stream->template->type != CATPT_STRM_TYPE_RENDER)
538 if (dsppos >= r->buffer_size / 2)
539 newpos = r->buffer_size / 2;
543 * Dsp operates on buffer halves, thus on every notify position
544 * (buffer half consumed) update wp to allow stream progression.
546 ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
547 frames_to_bytes(r, newpos),
550 dev_err(cdev->dev, "update position for stream %d failed: %d\n",
551 stream->info.stream_hw_id, ret);
555 snd_pcm_period_elapsed(substream);
558 /* 200 ms for 2 32-bit channels at 48kHz (native format) */
559 #define CATPT_BUFFER_MAX_SIZE 76800
560 #define CATPT_PCM_PERIODS_MAX 4
561 #define CATPT_PCM_PERIODS_MIN 2
563 static const struct snd_pcm_hardware catpt_pcm_hardware = {
564 .info = SNDRV_PCM_INFO_MMAP |
565 SNDRV_PCM_INFO_MMAP_VALID |
566 SNDRV_PCM_INFO_INTERLEAVED |
567 SNDRV_PCM_INFO_PAUSE |
568 SNDRV_PCM_INFO_RESUME |
569 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
570 .formats = SNDRV_PCM_FMTBIT_S16_LE |
571 SNDRV_PCM_FMTBIT_S24_LE |
572 SNDRV_PCM_FMTBIT_S32_LE,
573 .period_bytes_min = PAGE_SIZE,
574 .period_bytes_max = CATPT_BUFFER_MAX_SIZE / CATPT_PCM_PERIODS_MIN,
575 .periods_min = CATPT_PCM_PERIODS_MIN,
576 .periods_max = CATPT_PCM_PERIODS_MAX,
577 .buffer_bytes_max = CATPT_BUFFER_MAX_SIZE,
580 static int catpt_component_pcm_construct(struct snd_soc_component *component,
581 struct snd_soc_pcm_runtime *rtm)
583 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
585 snd_pcm_set_managed_buffer_all(rtm->pcm, SNDRV_DMA_TYPE_DEV_SG,
587 catpt_pcm_hardware.buffer_bytes_max,
588 catpt_pcm_hardware.buffer_bytes_max);
593 static int catpt_component_open(struct snd_soc_component *component,
594 struct snd_pcm_substream *substream)
596 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
598 if (!rtm->dai_link->no_pcm)
599 snd_soc_set_runtime_hwparams(substream, &catpt_pcm_hardware);
603 static snd_pcm_uframes_t
604 catpt_component_pointer(struct snd_soc_component *component,
605 struct snd_pcm_substream *substream)
607 struct snd_soc_pcm_runtime *rtm = snd_soc_substream_to_rtd(substream);
608 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtm, 0);
609 struct catpt_stream_runtime *stream;
610 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
613 if (rtm->dai_link->no_pcm)
616 stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
617 pos = catpt_stream_read_position(cdev, stream);
619 return bytes_to_frames(substream->runtime, pos);
622 static const struct snd_soc_dai_ops catpt_fe_dai_ops = {
623 .startup = catpt_dai_startup,
624 .shutdown = catpt_dai_shutdown,
625 .hw_params = catpt_dai_hw_params,
626 .hw_free = catpt_dai_hw_free,
627 .prepare = catpt_dai_prepare,
628 .trigger = catpt_dai_trigger,
631 static int catpt_dai_pcm_new(struct snd_soc_pcm_runtime *rtm,
632 struct snd_soc_dai *dai)
634 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtm, 0);
635 struct catpt_ssp_device_format devfmt;
636 struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
639 devfmt.iface = dai->driver->id;
640 devfmt.channels = codec_dai->driver->capture.channels_max;
642 switch (devfmt.iface) {
643 case CATPT_SSP_IFACE_0:
644 devfmt.mclk = CATPT_MCLK_FREQ_24_MHZ;
646 switch (devfmt.channels) {
648 devfmt.mode = CATPT_SSP_MODE_TDM_PROVIDER;
649 devfmt.clock_divider = 4;
653 devfmt.mode = CATPT_SSP_MODE_I2S_PROVIDER;
654 devfmt.clock_divider = 9;
659 case CATPT_SSP_IFACE_1:
660 devfmt.mclk = CATPT_MCLK_OFF;
661 devfmt.mode = CATPT_SSP_MODE_I2S_CONSUMER;
662 devfmt.clock_divider = 0;
666 /* see if this is a new configuration */
667 if (!memcmp(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt)))
670 ret = pm_runtime_resume_and_get(cdev->dev);
671 if (ret < 0 && ret != -EACCES)
674 ret = catpt_ipc_set_device_format(cdev, &devfmt);
676 pm_runtime_mark_last_busy(cdev->dev);
677 pm_runtime_put_autosuspend(cdev->dev);
680 return CATPT_IPC_ERROR(ret);
682 /* store device format set for given SSP */
683 memcpy(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt));
687 static const struct snd_soc_dai_ops catpt_dai_ops = {
688 .pcm_new = catpt_dai_pcm_new,
691 static struct snd_soc_dai_driver dai_drivers[] = {
694 .name = "System Pin",
695 .id = CATPT_STRM_TYPE_SYSTEM,
696 .ops = &catpt_fe_dai_ops,
698 .stream_name = "System Playback",
701 .rates = SNDRV_PCM_RATE_48000,
702 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
705 .stream_name = "Analog Capture",
708 .rates = SNDRV_PCM_RATE_48000,
709 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
713 .name = "Offload0 Pin",
714 .id = CATPT_STRM_TYPE_RENDER,
715 .ops = &catpt_fe_dai_ops,
717 .stream_name = "Offload0 Playback",
720 .rates = SNDRV_PCM_RATE_8000_192000,
721 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
725 .name = "Offload1 Pin",
726 .id = CATPT_STRM_TYPE_RENDER,
727 .ops = &catpt_fe_dai_ops,
729 .stream_name = "Offload1 Playback",
732 .rates = SNDRV_PCM_RATE_8000_192000,
733 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
737 .name = "Loopback Pin",
738 .id = CATPT_STRM_TYPE_LOOPBACK,
739 .ops = &catpt_fe_dai_ops,
741 .stream_name = "Loopback Capture",
744 .rates = SNDRV_PCM_RATE_48000,
745 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
749 .name = "Bluetooth Pin",
750 .id = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
751 .ops = &catpt_fe_dai_ops,
753 .stream_name = "Bluetooth Playback",
756 .rates = SNDRV_PCM_RATE_8000,
757 .formats = SNDRV_PCM_FMTBIT_S16_LE,
760 .stream_name = "Bluetooth Capture",
763 .rates = SNDRV_PCM_RATE_8000,
764 .formats = SNDRV_PCM_FMTBIT_S16_LE,
770 .id = CATPT_SSP_IFACE_0,
779 .ops = &catpt_dai_ops,
783 .id = CATPT_SSP_IFACE_1,
792 .ops = &catpt_dai_ops,
796 #define DSP_VOLUME_MAX S32_MAX /* 0db */
797 #define DSP_VOLUME_STEP_MAX 30
799 static u32 ctlvol_to_dspvol(u32 value)
801 if (value > DSP_VOLUME_STEP_MAX)
803 return DSP_VOLUME_MAX >> (DSP_VOLUME_STEP_MAX - value);
806 static u32 dspvol_to_ctlvol(u32 volume)
808 if (volume > DSP_VOLUME_MAX)
809 return DSP_VOLUME_STEP_MAX;
810 return volume ? __fls(volume) : 0;
813 static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol)
818 for (i = 1; i < CATPT_CHANNELS_MAX; i++)
819 if (ctlvol[i] != ctlvol[0])
822 if (i == CATPT_CHANNELS_MAX) {
823 dspvol = ctlvol_to_dspvol(ctlvol[0]);
825 ret = catpt_ipc_set_volume(cdev, stream_id,
826 CATPT_ALL_CHANNELS_MASK, dspvol,
827 0, CATPT_AUDIO_CURVE_NONE);
829 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
830 dspvol = ctlvol_to_dspvol(ctlvol[i]);
832 ret = catpt_ipc_set_volume(cdev, stream_id,
834 0, CATPT_AUDIO_CURVE_NONE);
841 return CATPT_IPC_ERROR(ret);
845 static int catpt_volume_info(struct snd_kcontrol *kcontrol,
846 struct snd_ctl_elem_info *uinfo)
848 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
849 uinfo->count = CATPT_CHANNELS_MAX;
850 uinfo->value.integer.min = 0;
851 uinfo->value.integer.max = DSP_VOLUME_STEP_MAX;
855 static int catpt_mixer_volume_get(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
858 struct snd_soc_component *component =
859 snd_soc_kcontrol_component(kcontrol);
860 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
865 ret = pm_runtime_resume_and_get(cdev->dev);
866 if (ret < 0 && ret != -EACCES)
869 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
870 dspvol = catpt_mixer_volume(cdev, &cdev->mixer, i);
871 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
874 pm_runtime_mark_last_busy(cdev->dev);
875 pm_runtime_put_autosuspend(cdev->dev);
880 static int catpt_mixer_volume_put(struct snd_kcontrol *kcontrol,
881 struct snd_ctl_elem_value *ucontrol)
883 struct snd_soc_component *component =
884 snd_soc_kcontrol_component(kcontrol);
885 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
888 ret = pm_runtime_resume_and_get(cdev->dev);
889 if (ret < 0 && ret != -EACCES)
892 ret = catpt_set_dspvol(cdev, cdev->mixer.mixer_hw_id,
893 ucontrol->value.integer.value);
895 pm_runtime_mark_last_busy(cdev->dev);
896 pm_runtime_put_autosuspend(cdev->dev);
901 static int catpt_stream_volume_get(struct snd_kcontrol *kcontrol,
902 struct snd_ctl_elem_value *ucontrol,
903 enum catpt_pin_id pin_id)
905 struct snd_soc_component *component =
906 snd_soc_kcontrol_component(kcontrol);
907 struct catpt_stream_runtime *stream;
908 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
909 long *ctlvol = (long *)kcontrol->private_value;
914 stream = catpt_stream_find(cdev, pin_id);
916 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
917 ucontrol->value.integer.value[i] = ctlvol[i];
921 ret = pm_runtime_resume_and_get(cdev->dev);
922 if (ret < 0 && ret != -EACCES)
925 for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
926 dspvol = catpt_stream_volume(cdev, stream, i);
927 ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
930 pm_runtime_mark_last_busy(cdev->dev);
931 pm_runtime_put_autosuspend(cdev->dev);
936 static int catpt_stream_volume_put(struct snd_kcontrol *kcontrol,
937 struct snd_ctl_elem_value *ucontrol,
938 enum catpt_pin_id pin_id)
940 struct snd_soc_component *component =
941 snd_soc_kcontrol_component(kcontrol);
942 struct catpt_stream_runtime *stream;
943 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
944 long *ctlvol = (long *)kcontrol->private_value;
947 stream = catpt_stream_find(cdev, pin_id);
949 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
950 ctlvol[i] = ucontrol->value.integer.value[i];
954 ret = pm_runtime_resume_and_get(cdev->dev);
955 if (ret < 0 && ret != -EACCES)
958 ret = catpt_set_dspvol(cdev, stream->info.stream_hw_id,
959 ucontrol->value.integer.value);
961 pm_runtime_mark_last_busy(cdev->dev);
962 pm_runtime_put_autosuspend(cdev->dev);
967 for (i = 0; i < CATPT_CHANNELS_MAX; i++)
968 ctlvol[i] = ucontrol->value.integer.value[i];
972 static int catpt_offload1_volume_get(struct snd_kcontrol *kctl,
973 struct snd_ctl_elem_value *uctl)
975 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
978 static int catpt_offload1_volume_put(struct snd_kcontrol *kctl,
979 struct snd_ctl_elem_value *uctl)
981 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
984 static int catpt_offload2_volume_get(struct snd_kcontrol *kctl,
985 struct snd_ctl_elem_value *uctl)
987 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
990 static int catpt_offload2_volume_put(struct snd_kcontrol *kctl,
991 struct snd_ctl_elem_value *uctl)
993 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
996 static int catpt_capture_volume_get(struct snd_kcontrol *kctl,
997 struct snd_ctl_elem_value *uctl)
999 return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
1002 static int catpt_capture_volume_put(struct snd_kcontrol *kctl,
1003 struct snd_ctl_elem_value *uctl)
1005 return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
1008 static int catpt_loopback_switch_get(struct snd_kcontrol *kcontrol,
1009 struct snd_ctl_elem_value *ucontrol)
1011 ucontrol->value.integer.value[0] = *(bool *)kcontrol->private_value;
1015 static int catpt_loopback_switch_put(struct snd_kcontrol *kcontrol,
1016 struct snd_ctl_elem_value *ucontrol)
1018 struct snd_soc_component *component =
1019 snd_soc_kcontrol_component(kcontrol);
1020 struct catpt_stream_runtime *stream;
1021 struct catpt_dev *cdev = dev_get_drvdata(component->dev);
1025 mute = (bool)ucontrol->value.integer.value[0];
1026 stream = catpt_stream_find(cdev, CATPT_PIN_ID_REFERENCE);
1028 *(bool *)kcontrol->private_value = mute;
1032 ret = pm_runtime_resume_and_get(cdev->dev);
1033 if (ret < 0 && ret != -EACCES)
1036 ret = catpt_ipc_mute_loopback(cdev, stream->info.stream_hw_id, mute);
1038 pm_runtime_mark_last_busy(cdev->dev);
1039 pm_runtime_put_autosuspend(cdev->dev);
1042 return CATPT_IPC_ERROR(ret);
1044 *(bool *)kcontrol->private_value = mute;
1048 static int catpt_waves_switch_get(struct snd_kcontrol *kcontrol,
1049 struct snd_ctl_elem_value *ucontrol)
1054 static int catpt_waves_switch_put(struct snd_kcontrol *kcontrol,
1055 struct snd_ctl_elem_value *ucontrol)
1060 static int catpt_waves_param_get(struct snd_kcontrol *kcontrol,
1061 unsigned int __user *bytes,
1067 static int catpt_waves_param_put(struct snd_kcontrol *kcontrol,
1068 const unsigned int __user *bytes,
1074 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(catpt_volume_tlv, -9000, 300, 1);
1076 #define CATPT_VOLUME_CTL(kname, sname) \
1077 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1079 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1080 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1081 .info = catpt_volume_info, \
1082 .get = catpt_##sname##_volume_get, \
1083 .put = catpt_##sname##_volume_put, \
1084 .tlv.p = catpt_volume_tlv, \
1085 .private_value = (unsigned long) \
1086 &(long[CATPT_CHANNELS_MAX]) {0} }
1088 static const struct snd_kcontrol_new component_kcontrols[] = {
1089 /* Master volume (mixer stream) */
1090 CATPT_VOLUME_CTL("Master Playback Volume", mixer),
1091 /* Individual volume controls for offload and capture */
1092 CATPT_VOLUME_CTL("Media0 Playback Volume", offload1),
1093 CATPT_VOLUME_CTL("Media1 Playback Volume", offload2),
1094 CATPT_VOLUME_CTL("Mic Capture Volume", capture),
1095 SOC_SINGLE_BOOL_EXT("Loopback Mute", (unsigned long)&(bool[1]) {0},
1096 catpt_loopback_switch_get, catpt_loopback_switch_put),
1097 /* Enable or disable WAVES module */
1098 SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
1099 catpt_waves_switch_get, catpt_waves_switch_put),
1100 /* WAVES module parameter control */
1101 SND_SOC_BYTES_TLV("Waves Set Param", 128,
1102 catpt_waves_param_get, catpt_waves_param_put),
1105 static const struct snd_soc_dapm_widget component_widgets[] = {
1106 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1107 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1108 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1109 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1111 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1114 static const struct snd_soc_dapm_route component_routes[] = {
1115 {"Playback VMixer", NULL, "System Playback"},
1116 {"Playback VMixer", NULL, "Offload0 Playback"},
1117 {"Playback VMixer", NULL, "Offload1 Playback"},
1119 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
1121 {"Analog Capture", NULL, "SSP0 CODEC IN"},
1122 {"Loopback Capture", NULL, "SSP0 CODEC IN"},
1124 {"SSP1 BT OUT", NULL, "Bluetooth Playback"},
1125 {"Bluetooth Capture", NULL, "SSP1 BT IN"},
1128 static const struct snd_soc_component_driver catpt_comp_driver = {
1129 .name = "catpt-platform",
1131 .pcm_construct = catpt_component_pcm_construct,
1132 .open = catpt_component_open,
1133 .pointer = catpt_component_pointer,
1135 .controls = component_kcontrols,
1136 .num_controls = ARRAY_SIZE(component_kcontrols),
1137 .dapm_widgets = component_widgets,
1138 .num_dapm_widgets = ARRAY_SIZE(component_widgets),
1139 .dapm_routes = component_routes,
1140 .num_dapm_routes = ARRAY_SIZE(component_routes),
1143 int catpt_arm_stream_templates(struct catpt_dev *cdev)
1145 struct resource *res;
1146 u32 scratch_size = 0;
1149 for (i = 0; i < ARRAY_SIZE(catpt_topology); i++) {
1150 struct catpt_stream_template *template;
1151 struct catpt_module_entry *entry;
1152 struct catpt_module_type *type;
1154 template = catpt_topology[i];
1155 template->persistent_size = 0;
1157 for (j = 0; j < template->num_entries; j++) {
1158 entry = &template->entries[j];
1159 type = &cdev->modules[entry->module_id];
1164 entry->entry_point = type->entry_point;
1165 template->persistent_size += type->persistent_size;
1166 if (type->scratch_size > scratch_size)
1167 scratch_size = type->scratch_size;
1172 /* allocate single scratch area for all modules */
1173 res = catpt_request_region(&cdev->dram, scratch_size);
1176 cdev->scratch = res;
1182 int catpt_register_plat_component(struct catpt_dev *cdev)
1184 struct snd_soc_component *component;
1187 component = devm_kzalloc(cdev->dev, sizeof(*component), GFP_KERNEL);
1191 ret = snd_soc_component_initialize(component, &catpt_comp_driver,
1196 component->name = catpt_comp_driver.name;
1197 return snd_soc_add_component(component, dai_drivers,
1198 ARRAY_SIZE(dai_drivers));