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
= 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 */
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_dev
*cdev
= dev_get_drvdata(dai
->dev
);
263 struct catpt_stream_template
*template;
264 struct catpt_stream_runtime
*stream
;
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_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
);
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
;
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
))) {
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
);
371 return CATPT_IPC_ERROR(ret
);
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
;
387 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
388 if (stream
->allocated
)
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
;
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
,
413 stream
->template->num_entries
,
414 stream
->template->entries
,
419 return CATPT_IPC_ERROR(ret
);
421 ret
= catpt_dai_apply_usettings(dai
, stream
);
425 stream
->allocated
= true;
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
)
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;
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
;
453 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
454 if (stream
->prepared
)
457 ret
= catpt_ipc_reset_stream(cdev
, stream
->info
.stream_hw_id
);
459 return CATPT_IPC_ERROR(ret
);
461 ret
= catpt_ipc_pause_stream(cdev
, stream
->info
.stream_hw_id
);
463 return CATPT_IPC_ERROR(ret
);
465 stream
->prepared
= true;
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
;
478 stream
= snd_soc_dai_get_dma_data(dai
, substream
);
481 case SNDRV_PCM_TRIGGER_START
:
482 /* only offload is set_write_pos driven */
483 if (stream
->template->type
!= CATPT_STRM_TYPE_RENDER
)
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
,
494 return CATPT_IPC_ERROR(ret
);
496 case SNDRV_PCM_TRIGGER_RESUME
:
497 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
499 catpt_dsp_update_lpclock(cdev
);
500 ret
= catpt_ipc_resume_stream(cdev
, stream
->info
.stream_hw_id
);
502 return CATPT_IPC_ERROR(ret
);
505 case SNDRV_PCM_TRIGGER_STOP
:
506 stream
->prepared
= false;
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
);
513 return CATPT_IPC_ERROR(ret
);
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
;
532 dsppos
= bytes_to_frames(r
, pos
->stream_position
);
534 if (!stream
->prepared
)
536 /* only offload is set_write_pos driven */
537 if (stream
->template->type
!= CATPT_STRM_TYPE_RENDER
)
540 if (dsppos
>= r
->buffer_size
/ 2)
541 newpos
= r
->buffer_size
/ 2;
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
),
552 dev_err(cdev
->dev
, "update position for stream %d failed: %d\n",
553 stream
->info
.stream_hw_id
, ret
);
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
,
589 catpt_pcm_hardware
.buffer_bytes_max
,
590 catpt_pcm_hardware
.buffer_bytes_max
);
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
)
602 snd_soc_set_runtime_hwparams(substream
, &catpt_pcm_hardware
);
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);
616 if (rtm
->dai_link
->no_pcm
)
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
;
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
) {
651 devfmt
.mode
= CATPT_SSP_MODE_TDM_PROVIDER
;
652 devfmt
.clock_divider
= 4;
656 devfmt
.mode
= CATPT_SSP_MODE_I2S_PROVIDER
;
657 devfmt
.clock_divider
= 9;
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;
669 /* see if this is a new configuration */
670 if (!memcmp(&cdev
->devfmt
[devfmt
.iface
], &devfmt
, sizeof(devfmt
)))
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
);
681 return CATPT_IPC_ERROR(ret
);
683 /* store device format set for given SSP */
684 memcpy(&cdev
->devfmt
[devfmt
.iface
], &devfmt
, sizeof(devfmt
));
688 static struct snd_soc_dai_driver dai_drivers
[] = {
691 .name
= "System Pin",
692 .id
= CATPT_STRM_TYPE_SYSTEM
,
693 .ops
= &catpt_fe_dai_ops
,
695 .stream_name
= "System Playback",
698 .rates
= SNDRV_PCM_RATE_48000
,
699 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
702 .stream_name
= "Analog Capture",
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
,
714 .stream_name
= "Offload0 Playback",
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
,
726 .stream_name
= "Offload1 Playback",
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
,
738 .stream_name
= "Loopback Capture",
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
,
750 .stream_name
= "Bluetooth Playback",
753 .rates
= SNDRV_PCM_RATE_8000
,
754 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
757 .stream_name
= "Bluetooth Capture",
760 .rates
= SNDRV_PCM_RATE_8000
,
761 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
767 .id
= CATPT_SSP_IFACE_0
,
768 .pcm_new
= catpt_dai_pcm_new
,
780 .id
= CATPT_SSP_IFACE_1
,
781 .pcm_new
= catpt_dai_pcm_new
,
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
)
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
)
815 for (i
= 1; i
< CATPT_CHANNELS_MAX
; i
++)
816 if (ctlvol
[i
] != ctlvol
[0])
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
);
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
,
831 0, CATPT_AUDIO_CURVE_NONE
);
838 return CATPT_IPC_ERROR(ret
);
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
;
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
);
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
);
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
);
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
);
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
;
905 stream
= catpt_stream_find(cdev
, pin_id
);
907 for (i
= 0; i
< CATPT_CHANNELS_MAX
; i
++)
908 ucontrol
->value
.integer
.value
[i
] = ctlvol
[i
];
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
);
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
;
936 stream
= catpt_stream_find(cdev
, pin_id
);
938 for (i
= 0; i
< CATPT_CHANNELS_MAX
; i
++)
939 ctlvol
[i
] = ucontrol
->value
.integer
.value
[i
];
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
);
954 for (i
= 0; i
< CATPT_CHANNELS_MAX
; i
++)
955 ctlvol
[i
] = ucontrol
->value
.integer
.value
[i
];
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
;
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
;
1012 mute
= (bool)ucontrol
->value
.integer
.value
[0];
1013 stream
= catpt_stream_find(cdev
, CATPT_PIN_ID_REFERENCE
);
1015 *(bool *)kcontrol
->private_value
= mute
;
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
);
1027 return CATPT_IPC_ERROR(ret
);
1029 *(bool *)kcontrol
->private_value
= mute
;
1033 static int catpt_waves_switch_get(struct snd_kcontrol
*kcontrol
,
1034 struct snd_ctl_elem_value
*ucontrol
)
1039 static int catpt_waves_switch_put(struct snd_kcontrol
*kcontrol
,
1040 struct snd_ctl_elem_value
*ucontrol
)
1045 static int catpt_waves_param_get(struct snd_kcontrol
*kcontrol
,
1046 unsigned int __user
*bytes
,
1052 static int catpt_waves_param_put(struct snd_kcontrol
*kcontrol
,
1053 const unsigned int __user
*bytes
,
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, \
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;
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
];
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
;
1157 /* allocate single scratch area for all modules */
1158 res
= catpt_request_region(&cdev
->dram
, scratch_size
);
1161 cdev
->scratch
= res
;
1167 int catpt_register_plat_component(struct catpt_dev
*cdev
)
1169 struct snd_soc_component
*component
;
1172 component
= devm_kzalloc(cdev
->dev
, sizeof(*component
), GFP_KERNEL
);
1176 ret
= snd_soc_component_initialize(component
, &catpt_comp_driver
,
1181 component
->name
= catpt_comp_driver
.name
;
1182 return snd_soc_add_component(component
, dai_drivers
,
1183 ARRAY_SIZE(dai_drivers
));