xref: /OK3568_Linux_fs/kernel/sound/soc/intel/catpt/pcm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Copyright(c) 2020 Intel Corporation. All rights reserved.
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6*4882a593Smuzhiyun //
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/pm_runtime.h>
9*4882a593Smuzhiyun #include <sound/soc.h>
10*4882a593Smuzhiyun #include <sound/pcm_params.h>
11*4882a593Smuzhiyun #include <uapi/sound/tlv.h>
12*4882a593Smuzhiyun #include "core.h"
13*4882a593Smuzhiyun #include "messages.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun struct catpt_stream_template {
16*4882a593Smuzhiyun 	enum catpt_path_id path_id;
17*4882a593Smuzhiyun 	enum catpt_stream_type type;
18*4882a593Smuzhiyun 	u32 persistent_size;
19*4882a593Smuzhiyun 	u8 num_entries;
20*4882a593Smuzhiyun 	struct catpt_module_entry entries[];
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static struct catpt_stream_template system_pb = {
24*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP0_OUT,
25*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_SYSTEM,
26*4882a593Smuzhiyun 	.num_entries = 1,
27*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_PCM_SYSTEM, 0 }},
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct catpt_stream_template system_cp = {
31*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP0_IN,
32*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_CAPTURE,
33*4882a593Smuzhiyun 	.num_entries = 1,
34*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_PCM_CAPTURE, 0 }},
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static struct catpt_stream_template offload_pb = {
38*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP0_OUT,
39*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_RENDER,
40*4882a593Smuzhiyun 	.num_entries = 1,
41*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_PCM, 0 }},
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun static struct catpt_stream_template loopback_cp = {
45*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP0_OUT,
46*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_LOOPBACK,
47*4882a593Smuzhiyun 	.num_entries = 1,
48*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_PCM_REFERENCE, 0 }},
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun static struct catpt_stream_template bluetooth_pb = {
52*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP1_OUT,
53*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
54*4882a593Smuzhiyun 	.num_entries = 1,
55*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_BLUETOOTH_RENDER, 0 }},
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static struct catpt_stream_template bluetooth_cp = {
59*4882a593Smuzhiyun 	.path_id = CATPT_PATH_SSP1_IN,
60*4882a593Smuzhiyun 	.type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE,
61*4882a593Smuzhiyun 	.num_entries = 1,
62*4882a593Smuzhiyun 	.entries = {{ CATPT_MODID_BLUETOOTH_CAPTURE, 0 }},
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static struct catpt_stream_template *catpt_topology[] = {
66*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_RENDER]		= &offload_pb,
67*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_SYSTEM]		= &system_pb,
68*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_CAPTURE]		= &system_cp,
69*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_LOOPBACK]		= &loopback_cp,
70*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_BLUETOOTH_RENDER]	= &bluetooth_pb,
71*4882a593Smuzhiyun 	[CATPT_STRM_TYPE_BLUETOOTH_CAPTURE]	= &bluetooth_cp,
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun static struct catpt_stream_template *
catpt_get_stream_template(struct snd_pcm_substream * substream)75*4882a593Smuzhiyun catpt_get_stream_template(struct snd_pcm_substream *substream)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtm = substream->private_data;
78*4882a593Smuzhiyun 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtm, 0);
79*4882a593Smuzhiyun 	enum catpt_stream_type type;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	type = cpu_dai->driver->id;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* account for capture in bidirectional dais */
84*4882a593Smuzhiyun 	switch (type) {
85*4882a593Smuzhiyun 	case CATPT_STRM_TYPE_SYSTEM:
86*4882a593Smuzhiyun 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
87*4882a593Smuzhiyun 			type = CATPT_STRM_TYPE_CAPTURE;
88*4882a593Smuzhiyun 		break;
89*4882a593Smuzhiyun 	case CATPT_STRM_TYPE_BLUETOOTH_RENDER:
90*4882a593Smuzhiyun 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
91*4882a593Smuzhiyun 			type = CATPT_STRM_TYPE_BLUETOOTH_CAPTURE;
92*4882a593Smuzhiyun 		break;
93*4882a593Smuzhiyun 	default:
94*4882a593Smuzhiyun 		break;
95*4882a593Smuzhiyun 	};
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	return catpt_topology[type];
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun struct catpt_stream_runtime *
catpt_stream_find(struct catpt_dev * cdev,u8 stream_hw_id)101*4882a593Smuzhiyun catpt_stream_find(struct catpt_dev *cdev, u8 stream_hw_id)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct catpt_stream_runtime *pos, *result = NULL;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	spin_lock(&cdev->list_lock);
106*4882a593Smuzhiyun 	list_for_each_entry(pos, &cdev->stream_list, node) {
107*4882a593Smuzhiyun 		if (pos->info.stream_hw_id == stream_hw_id) {
108*4882a593Smuzhiyun 			result = pos;
109*4882a593Smuzhiyun 			break;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	spin_unlock(&cdev->list_lock);
114*4882a593Smuzhiyun 	return result;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
catpt_stream_read_position(struct catpt_dev * cdev,struct catpt_stream_runtime * stream)117*4882a593Smuzhiyun static u32 catpt_stream_read_position(struct catpt_dev *cdev,
118*4882a593Smuzhiyun 				      struct catpt_stream_runtime *stream)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	u32 pos;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	memcpy_fromio(&pos, cdev->lpe_ba + stream->info.read_pos_regaddr,
123*4882a593Smuzhiyun 		      sizeof(pos));
124*4882a593Smuzhiyun 	return pos;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
catpt_stream_volume(struct catpt_dev * cdev,struct catpt_stream_runtime * stream,u32 channel)127*4882a593Smuzhiyun static u32 catpt_stream_volume(struct catpt_dev *cdev,
128*4882a593Smuzhiyun 			       struct catpt_stream_runtime *stream, u32 channel)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	u32 volume, offset;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (channel >= CATPT_CHANNELS_MAX)
133*4882a593Smuzhiyun 		channel = 0;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	offset = stream->info.volume_regaddr[channel];
136*4882a593Smuzhiyun 	memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
137*4882a593Smuzhiyun 	return volume;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
catpt_mixer_volume(struct catpt_dev * cdev,struct catpt_mixer_stream_info * info,u32 channel)140*4882a593Smuzhiyun static u32 catpt_mixer_volume(struct catpt_dev *cdev,
141*4882a593Smuzhiyun 			      struct catpt_mixer_stream_info *info, u32 channel)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	u32 volume, offset;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	if (channel >= CATPT_CHANNELS_MAX)
146*4882a593Smuzhiyun 		channel = 0;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	offset = info->volume_regaddr[channel];
149*4882a593Smuzhiyun 	memcpy_fromio(&volume, cdev->lpe_ba + offset, sizeof(volume));
150*4882a593Smuzhiyun 	return volume;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
catpt_arrange_page_table(struct snd_pcm_substream * substream,struct snd_dma_buffer * pgtbl)153*4882a593Smuzhiyun static void catpt_arrange_page_table(struct snd_pcm_substream *substream,
154*4882a593Smuzhiyun 				     struct snd_dma_buffer *pgtbl)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	struct snd_pcm_runtime *rtm = substream->runtime;
157*4882a593Smuzhiyun 	struct snd_dma_buffer *databuf = snd_pcm_get_dma_buf(substream);
158*4882a593Smuzhiyun 	int i, pages;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	pages = snd_sgbuf_aligned_pages(rtm->dma_bytes);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	for (i = 0; i < pages; i++) {
163*4882a593Smuzhiyun 		u32 pfn, offset;
164*4882a593Smuzhiyun 		u32 *page_table;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 		pfn = PFN_DOWN(snd_sgbuf_get_addr(databuf, i * PAGE_SIZE));
167*4882a593Smuzhiyun 		/* incrementing by 2 on even and 3 on odd */
168*4882a593Smuzhiyun 		offset = ((i << 2) + i) >> 1;
169*4882a593Smuzhiyun 		page_table = (u32 *)(pgtbl->area + offset);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		if (i & 1)
172*4882a593Smuzhiyun 			*page_table |= (pfn << 4);
173*4882a593Smuzhiyun 		else
174*4882a593Smuzhiyun 			*page_table |= pfn;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
catpt_get_channel_map(enum catpt_channel_config config)178*4882a593Smuzhiyun static u32 catpt_get_channel_map(enum catpt_channel_config config)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	switch (config) {
181*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_MONO:
182*4882a593Smuzhiyun 		return GENMASK(31, 4) | CATPT_CHANNEL_CENTER;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_STEREO:
185*4882a593Smuzhiyun 		return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
186*4882a593Smuzhiyun 				      | (CATPT_CHANNEL_RIGHT << 4);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_2_POINT_1:
189*4882a593Smuzhiyun 		return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
190*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 4)
191*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LFE << 8);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_3_POINT_0:
194*4882a593Smuzhiyun 		return GENMASK(31, 12) | CATPT_CHANNEL_LEFT
195*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_CENTER << 4)
196*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 8);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_3_POINT_1:
199*4882a593Smuzhiyun 		return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
200*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_CENTER << 4)
201*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 8)
202*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LFE << 12);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_QUATRO:
205*4882a593Smuzhiyun 		return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
206*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 4)
207*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LEFT_SURROUND << 8)
208*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT_SURROUND << 12);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_4_POINT_0:
211*4882a593Smuzhiyun 		return GENMASK(31, 16) | CATPT_CHANNEL_LEFT
212*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_CENTER << 4)
213*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 8)
214*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_CENTER_SURROUND << 12);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_5_POINT_0:
217*4882a593Smuzhiyun 		return GENMASK(31, 20) | CATPT_CHANNEL_LEFT
218*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_CENTER << 4)
219*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 8)
220*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LEFT_SURROUND << 12)
221*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT_SURROUND << 16);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_5_POINT_1:
224*4882a593Smuzhiyun 		return GENMASK(31, 24) | CATPT_CHANNEL_CENTER
225*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LEFT << 4)
226*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT << 8)
227*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LEFT_SURROUND << 12)
228*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_RIGHT_SURROUND << 16)
229*4882a593Smuzhiyun 				       | (CATPT_CHANNEL_LFE << 20);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	case CATPT_CHANNEL_CONFIG_DUAL_MONO:
232*4882a593Smuzhiyun 		return GENMASK(31, 8) | CATPT_CHANNEL_LEFT
233*4882a593Smuzhiyun 				      | (CATPT_CHANNEL_LEFT << 4);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	default:
236*4882a593Smuzhiyun 		return U32_MAX;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
catpt_get_channel_config(u32 num_channels)240*4882a593Smuzhiyun static enum catpt_channel_config catpt_get_channel_config(u32 num_channels)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	switch (num_channels) {
243*4882a593Smuzhiyun 	case 6:
244*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_5_POINT_1;
245*4882a593Smuzhiyun 	case 5:
246*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_5_POINT_0;
247*4882a593Smuzhiyun 	case 4:
248*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_QUATRO;
249*4882a593Smuzhiyun 	case 3:
250*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_2_POINT_1;
251*4882a593Smuzhiyun 	case 1:
252*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_MONO;
253*4882a593Smuzhiyun 	case 2:
254*4882a593Smuzhiyun 	default:
255*4882a593Smuzhiyun 		return CATPT_CHANNEL_CONFIG_STEREO;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
catpt_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)259*4882a593Smuzhiyun static int catpt_dai_startup(struct snd_pcm_substream *substream,
260*4882a593Smuzhiyun 			     struct snd_soc_dai *dai)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
263*4882a593Smuzhiyun 	struct catpt_stream_template *template;
264*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
265*4882a593Smuzhiyun 	struct resource *res;
266*4882a593Smuzhiyun 	int ret;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	template = catpt_get_stream_template(substream);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
271*4882a593Smuzhiyun 	if (!stream)
272*4882a593Smuzhiyun 		return -ENOMEM;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, cdev->dev, PAGE_SIZE,
275*4882a593Smuzhiyun 				  &stream->pgtbl);
276*4882a593Smuzhiyun 	if (ret)
277*4882a593Smuzhiyun 		goto err_pgtbl;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	res = catpt_request_region(&cdev->dram, template->persistent_size);
280*4882a593Smuzhiyun 	if (!res) {
281*4882a593Smuzhiyun 		ret = -EBUSY;
282*4882a593Smuzhiyun 		goto err_request;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	stream->template = template;
288*4882a593Smuzhiyun 	stream->persistent = res;
289*4882a593Smuzhiyun 	stream->substream = substream;
290*4882a593Smuzhiyun 	INIT_LIST_HEAD(&stream->node);
291*4882a593Smuzhiyun 	snd_soc_dai_set_dma_data(dai, substream, stream);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	spin_lock(&cdev->list_lock);
294*4882a593Smuzhiyun 	list_add_tail(&stream->node, &cdev->stream_list);
295*4882a593Smuzhiyun 	spin_unlock(&cdev->list_lock);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	return 0;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun err_request:
300*4882a593Smuzhiyun 	snd_dma_free_pages(&stream->pgtbl);
301*4882a593Smuzhiyun err_pgtbl:
302*4882a593Smuzhiyun 	kfree(stream);
303*4882a593Smuzhiyun 	return ret;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
catpt_dai_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)306*4882a593Smuzhiyun static void catpt_dai_shutdown(struct snd_pcm_substream *substream,
307*4882a593Smuzhiyun 			       struct snd_soc_dai *dai)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
310*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(dai, substream);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	spin_lock(&cdev->list_lock);
315*4882a593Smuzhiyun 	list_del(&stream->node);
316*4882a593Smuzhiyun 	spin_unlock(&cdev->list_lock);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	release_resource(stream->persistent);
319*4882a593Smuzhiyun 	kfree(stream->persistent);
320*4882a593Smuzhiyun 	catpt_dsp_update_srampge(cdev, &cdev->dram, cdev->spec->dram_mask);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	snd_dma_free_pages(&stream->pgtbl);
323*4882a593Smuzhiyun 	kfree(stream);
324*4882a593Smuzhiyun 	snd_soc_dai_set_dma_data(dai, substream, NULL);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
catpt_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)327*4882a593Smuzhiyun static int catpt_dai_hw_params(struct snd_pcm_substream *substream,
328*4882a593Smuzhiyun 			       struct snd_pcm_hw_params *params,
329*4882a593Smuzhiyun 			       struct snd_soc_dai *dai)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
332*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
333*4882a593Smuzhiyun 	struct catpt_audio_format afmt;
334*4882a593Smuzhiyun 	struct catpt_ring_info rinfo;
335*4882a593Smuzhiyun 	struct snd_pcm_runtime *rtm = substream->runtime;
336*4882a593Smuzhiyun 	struct snd_dma_buffer *dmab;
337*4882a593Smuzhiyun 	int ret;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(dai, substream);
340*4882a593Smuzhiyun 	if (stream->allocated)
341*4882a593Smuzhiyun 		return 0;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	memset(&afmt, 0, sizeof(afmt));
344*4882a593Smuzhiyun 	afmt.sample_rate = params_rate(params);
345*4882a593Smuzhiyun 	afmt.bit_depth = params_physical_width(params);
346*4882a593Smuzhiyun 	afmt.valid_bit_depth = params_width(params);
347*4882a593Smuzhiyun 	afmt.num_channels = params_channels(params);
348*4882a593Smuzhiyun 	afmt.channel_config = catpt_get_channel_config(afmt.num_channels);
349*4882a593Smuzhiyun 	afmt.channel_map = catpt_get_channel_map(afmt.channel_config);
350*4882a593Smuzhiyun 	afmt.interleaving = CATPT_INTERLEAVING_PER_CHANNEL;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	dmab = snd_pcm_get_dma_buf(substream);
353*4882a593Smuzhiyun 	catpt_arrange_page_table(substream, &stream->pgtbl);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	memset(&rinfo, 0, sizeof(rinfo));
356*4882a593Smuzhiyun 	rinfo.page_table_addr = stream->pgtbl.addr;
357*4882a593Smuzhiyun 	rinfo.num_pages = DIV_ROUND_UP(rtm->dma_bytes, PAGE_SIZE);
358*4882a593Smuzhiyun 	rinfo.size = rtm->dma_bytes;
359*4882a593Smuzhiyun 	rinfo.offset = 0;
360*4882a593Smuzhiyun 	rinfo.ring_first_page_pfn = PFN_DOWN(snd_sgbuf_get_addr(dmab, 0));
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	ret = catpt_ipc_alloc_stream(cdev, stream->template->path_id,
363*4882a593Smuzhiyun 				     stream->template->type,
364*4882a593Smuzhiyun 				     &afmt, &rinfo,
365*4882a593Smuzhiyun 				     stream->template->num_entries,
366*4882a593Smuzhiyun 				     stream->template->entries,
367*4882a593Smuzhiyun 				     stream->persistent,
368*4882a593Smuzhiyun 				     cdev->scratch,
369*4882a593Smuzhiyun 				     &stream->info);
370*4882a593Smuzhiyun 	if (ret)
371*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	stream->allocated = true;
374*4882a593Smuzhiyun 	return 0;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
catpt_dai_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)377*4882a593Smuzhiyun static int catpt_dai_hw_free(struct snd_pcm_substream *substream,
378*4882a593Smuzhiyun 			     struct snd_soc_dai *dai)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
381*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(dai, substream);
384*4882a593Smuzhiyun 	if (!stream->allocated)
385*4882a593Smuzhiyun 		return 0;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
388*4882a593Smuzhiyun 	catpt_ipc_free_stream(cdev, stream->info.stream_hw_id);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	stream->allocated = false;
391*4882a593Smuzhiyun 	return 0;
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol);
395*4882a593Smuzhiyun 
catpt_dai_apply_usettings(struct snd_soc_dai * dai,struct catpt_stream_runtime * stream)396*4882a593Smuzhiyun static int catpt_dai_apply_usettings(struct snd_soc_dai *dai,
397*4882a593Smuzhiyun 				     struct catpt_stream_runtime *stream)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
400*4882a593Smuzhiyun 	struct snd_soc_component *component = dai->component;
401*4882a593Smuzhiyun 	struct snd_kcontrol *pos, *kctl = NULL;
402*4882a593Smuzhiyun 	const char *name;
403*4882a593Smuzhiyun 	int ret;
404*4882a593Smuzhiyun 	u32 id = stream->info.stream_hw_id;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	/* only selected streams have individual controls */
407*4882a593Smuzhiyun 	switch (id) {
408*4882a593Smuzhiyun 	case CATPT_PIN_ID_OFFLOAD1:
409*4882a593Smuzhiyun 		name = "Media0 Playback Volume";
410*4882a593Smuzhiyun 		break;
411*4882a593Smuzhiyun 	case CATPT_PIN_ID_OFFLOAD2:
412*4882a593Smuzhiyun 		name = "Media1 Playback Volume";
413*4882a593Smuzhiyun 		break;
414*4882a593Smuzhiyun 	case CATPT_PIN_ID_CAPTURE1:
415*4882a593Smuzhiyun 		name = "Mic Capture Volume";
416*4882a593Smuzhiyun 		break;
417*4882a593Smuzhiyun 	case CATPT_PIN_ID_REFERENCE:
418*4882a593Smuzhiyun 		name = "Loopback Mute";
419*4882a593Smuzhiyun 		break;
420*4882a593Smuzhiyun 	default:
421*4882a593Smuzhiyun 		return 0;
422*4882a593Smuzhiyun 	};
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	list_for_each_entry(pos, &component->card->snd_card->controls, list) {
425*4882a593Smuzhiyun 		if (pos->private_data == component &&
426*4882a593Smuzhiyun 		    !strncmp(name, pos->id.name, sizeof(pos->id.name))) {
427*4882a593Smuzhiyun 			kctl = pos;
428*4882a593Smuzhiyun 			break;
429*4882a593Smuzhiyun 		}
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun 	if (!kctl)
432*4882a593Smuzhiyun 		return -ENOENT;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if (stream->template->type != CATPT_STRM_TYPE_LOOPBACK)
435*4882a593Smuzhiyun 		return catpt_set_dspvol(cdev, id, (long *)kctl->private_value);
436*4882a593Smuzhiyun 	ret = catpt_ipc_mute_loopback(cdev, id, *(bool *)kctl->private_value);
437*4882a593Smuzhiyun 	if (ret)
438*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
439*4882a593Smuzhiyun 	return 0;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
catpt_dai_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)442*4882a593Smuzhiyun static int catpt_dai_prepare(struct snd_pcm_substream *substream,
443*4882a593Smuzhiyun 			     struct snd_soc_dai *dai)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
446*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
447*4882a593Smuzhiyun 	int ret;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(dai, substream);
450*4882a593Smuzhiyun 	if (stream->prepared)
451*4882a593Smuzhiyun 		return 0;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	ret = catpt_ipc_reset_stream(cdev, stream->info.stream_hw_id);
454*4882a593Smuzhiyun 	if (ret)
455*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
458*4882a593Smuzhiyun 	if (ret)
459*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	ret = catpt_dai_apply_usettings(dai, stream);
462*4882a593Smuzhiyun 	if (ret)
463*4882a593Smuzhiyun 		return ret;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	stream->prepared = true;
466*4882a593Smuzhiyun 	return 0;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
catpt_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)469*4882a593Smuzhiyun static int catpt_dai_trigger(struct snd_pcm_substream *substream, int cmd,
470*4882a593Smuzhiyun 			     struct snd_soc_dai *dai)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
473*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
474*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
475*4882a593Smuzhiyun 	snd_pcm_uframes_t pos;
476*4882a593Smuzhiyun 	int ret;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(dai, substream);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	switch (cmd) {
481*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_START:
482*4882a593Smuzhiyun 		/* only offload is set_write_pos driven */
483*4882a593Smuzhiyun 		if (stream->template->type != CATPT_STRM_TYPE_RENDER)
484*4882a593Smuzhiyun 			goto resume_stream;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 		pos = frames_to_bytes(runtime, runtime->start_threshold);
487*4882a593Smuzhiyun 		/*
488*4882a593Smuzhiyun 		 * Dsp operates on buffer halves, thus max 2x set_write_pos
489*4882a593Smuzhiyun 		 * (entire buffer filled) prior to stream start.
490*4882a593Smuzhiyun 		 */
491*4882a593Smuzhiyun 		ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
492*4882a593Smuzhiyun 					      pos, false, false);
493*4882a593Smuzhiyun 		if (ret)
494*4882a593Smuzhiyun 			return CATPT_IPC_ERROR(ret);
495*4882a593Smuzhiyun 		fallthrough;
496*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_RESUME:
497*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
498*4882a593Smuzhiyun 	resume_stream:
499*4882a593Smuzhiyun 		catpt_dsp_update_lpclock(cdev);
500*4882a593Smuzhiyun 		ret = catpt_ipc_resume_stream(cdev, stream->info.stream_hw_id);
501*4882a593Smuzhiyun 		if (ret)
502*4882a593Smuzhiyun 			return CATPT_IPC_ERROR(ret);
503*4882a593Smuzhiyun 		break;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_STOP:
506*4882a593Smuzhiyun 		stream->prepared = false;
507*4882a593Smuzhiyun 		fallthrough;
508*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_SUSPEND:
509*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
510*4882a593Smuzhiyun 		ret = catpt_ipc_pause_stream(cdev, stream->info.stream_hw_id);
511*4882a593Smuzhiyun 		catpt_dsp_update_lpclock(cdev);
512*4882a593Smuzhiyun 		if (ret)
513*4882a593Smuzhiyun 			return CATPT_IPC_ERROR(ret);
514*4882a593Smuzhiyun 		break;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	default:
517*4882a593Smuzhiyun 		break;
518*4882a593Smuzhiyun 	}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	return 0;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
catpt_stream_update_position(struct catpt_dev * cdev,struct catpt_stream_runtime * stream,struct catpt_notify_position * pos)523*4882a593Smuzhiyun void catpt_stream_update_position(struct catpt_dev *cdev,
524*4882a593Smuzhiyun 				  struct catpt_stream_runtime *stream,
525*4882a593Smuzhiyun 				  struct catpt_notify_position *pos)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = stream->substream;
528*4882a593Smuzhiyun 	struct snd_pcm_runtime *r = substream->runtime;
529*4882a593Smuzhiyun 	snd_pcm_uframes_t dsppos, newpos;
530*4882a593Smuzhiyun 	int ret;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	dsppos = bytes_to_frames(r, pos->stream_position);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	if (!stream->prepared)
535*4882a593Smuzhiyun 		goto exit;
536*4882a593Smuzhiyun 	/* only offload is set_write_pos driven */
537*4882a593Smuzhiyun 	if (stream->template->type != CATPT_STRM_TYPE_RENDER)
538*4882a593Smuzhiyun 		goto exit;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	if (dsppos >= r->buffer_size / 2)
541*4882a593Smuzhiyun 		newpos = r->buffer_size / 2;
542*4882a593Smuzhiyun 	else
543*4882a593Smuzhiyun 		newpos = 0;
544*4882a593Smuzhiyun 	/*
545*4882a593Smuzhiyun 	 * Dsp operates on buffer halves, thus on every notify position
546*4882a593Smuzhiyun 	 * (buffer half consumed) update wp to allow stream progression.
547*4882a593Smuzhiyun 	 */
548*4882a593Smuzhiyun 	ret = catpt_ipc_set_write_pos(cdev, stream->info.stream_hw_id,
549*4882a593Smuzhiyun 				      frames_to_bytes(r, newpos),
550*4882a593Smuzhiyun 				      false, false);
551*4882a593Smuzhiyun 	if (ret) {
552*4882a593Smuzhiyun 		dev_err(cdev->dev, "update position for stream %d failed: %d\n",
553*4882a593Smuzhiyun 			stream->info.stream_hw_id, ret);
554*4882a593Smuzhiyun 		return;
555*4882a593Smuzhiyun 	}
556*4882a593Smuzhiyun exit:
557*4882a593Smuzhiyun 	snd_pcm_period_elapsed(substream);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun /* 200 ms for 2 32-bit channels at 48kHz (native format) */
561*4882a593Smuzhiyun #define CATPT_BUFFER_MAX_SIZE	76800
562*4882a593Smuzhiyun #define CATPT_PCM_PERIODS_MAX	4
563*4882a593Smuzhiyun #define CATPT_PCM_PERIODS_MIN	2
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun static const struct snd_pcm_hardware catpt_pcm_hardware = {
566*4882a593Smuzhiyun 	.info			= SNDRV_PCM_INFO_MMAP |
567*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_MMAP_VALID |
568*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_INTERLEAVED |
569*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_PAUSE |
570*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_RESUME |
571*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
572*4882a593Smuzhiyun 	.formats		= SNDRV_PCM_FMTBIT_S16_LE |
573*4882a593Smuzhiyun 				  SNDRV_PCM_FMTBIT_S24_LE |
574*4882a593Smuzhiyun 				  SNDRV_PCM_FMTBIT_S32_LE,
575*4882a593Smuzhiyun 	.period_bytes_min	= PAGE_SIZE,
576*4882a593Smuzhiyun 	.period_bytes_max	= CATPT_BUFFER_MAX_SIZE / CATPT_PCM_PERIODS_MIN,
577*4882a593Smuzhiyun 	.periods_min		= CATPT_PCM_PERIODS_MIN,
578*4882a593Smuzhiyun 	.periods_max		= CATPT_PCM_PERIODS_MAX,
579*4882a593Smuzhiyun 	.buffer_bytes_max	= CATPT_BUFFER_MAX_SIZE,
580*4882a593Smuzhiyun };
581*4882a593Smuzhiyun 
catpt_component_pcm_construct(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtm)582*4882a593Smuzhiyun static int catpt_component_pcm_construct(struct snd_soc_component *component,
583*4882a593Smuzhiyun 					 struct snd_soc_pcm_runtime *rtm)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	snd_pcm_set_managed_buffer_all(rtm->pcm, SNDRV_DMA_TYPE_DEV_SG,
588*4882a593Smuzhiyun 				       cdev->dev,
589*4882a593Smuzhiyun 				       catpt_pcm_hardware.buffer_bytes_max,
590*4882a593Smuzhiyun 				       catpt_pcm_hardware.buffer_bytes_max);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	return 0;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
catpt_component_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)595*4882a593Smuzhiyun static int catpt_component_open(struct snd_soc_component *component,
596*4882a593Smuzhiyun 				struct snd_pcm_substream *substream)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtm = substream->private_data;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (rtm->dai_link->no_pcm)
601*4882a593Smuzhiyun 		return 0;
602*4882a593Smuzhiyun 	snd_soc_set_runtime_hwparams(substream, &catpt_pcm_hardware);
603*4882a593Smuzhiyun 	return 0;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun static snd_pcm_uframes_t
catpt_component_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)607*4882a593Smuzhiyun catpt_component_pointer(struct snd_soc_component *component,
608*4882a593Smuzhiyun 			struct snd_pcm_substream *substream)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
611*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
612*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtm = substream->private_data;
613*4882a593Smuzhiyun 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtm, 0);
614*4882a593Smuzhiyun 	u32 pos;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	if (rtm->dai_link->no_pcm)
617*4882a593Smuzhiyun 		return 0;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
620*4882a593Smuzhiyun 	pos = catpt_stream_read_position(cdev, stream);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	return bytes_to_frames(substream->runtime, pos);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun static const struct snd_soc_dai_ops catpt_fe_dai_ops = {
626*4882a593Smuzhiyun 	.startup = catpt_dai_startup,
627*4882a593Smuzhiyun 	.shutdown = catpt_dai_shutdown,
628*4882a593Smuzhiyun 	.hw_params = catpt_dai_hw_params,
629*4882a593Smuzhiyun 	.hw_free = catpt_dai_hw_free,
630*4882a593Smuzhiyun 	.prepare = catpt_dai_prepare,
631*4882a593Smuzhiyun 	.trigger = catpt_dai_trigger,
632*4882a593Smuzhiyun };
633*4882a593Smuzhiyun 
catpt_dai_pcm_new(struct snd_soc_pcm_runtime * rtm,struct snd_soc_dai * dai)634*4882a593Smuzhiyun static int catpt_dai_pcm_new(struct snd_soc_pcm_runtime *rtm,
635*4882a593Smuzhiyun 			     struct snd_soc_dai *dai)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtm, 0);
638*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(dai->dev);
639*4882a593Smuzhiyun 	struct catpt_ssp_device_format devfmt;
640*4882a593Smuzhiyun 	int ret;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	devfmt.iface = dai->driver->id;
643*4882a593Smuzhiyun 	devfmt.channels = codec_dai->driver->capture.channels_max;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	switch (devfmt.iface) {
646*4882a593Smuzhiyun 	case CATPT_SSP_IFACE_0:
647*4882a593Smuzhiyun 		devfmt.mclk = CATPT_MCLK_FREQ_24_MHZ;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 		switch (devfmt.channels) {
650*4882a593Smuzhiyun 		case 4:
651*4882a593Smuzhiyun 			devfmt.mode = CATPT_SSP_MODE_TDM_PROVIDER;
652*4882a593Smuzhiyun 			devfmt.clock_divider = 4;
653*4882a593Smuzhiyun 			break;
654*4882a593Smuzhiyun 		case 2:
655*4882a593Smuzhiyun 		default:
656*4882a593Smuzhiyun 			devfmt.mode = CATPT_SSP_MODE_I2S_PROVIDER;
657*4882a593Smuzhiyun 			devfmt.clock_divider = 9;
658*4882a593Smuzhiyun 			break;
659*4882a593Smuzhiyun 		}
660*4882a593Smuzhiyun 		break;
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	case CATPT_SSP_IFACE_1:
663*4882a593Smuzhiyun 		devfmt.mclk = CATPT_MCLK_OFF;
664*4882a593Smuzhiyun 		devfmt.mode = CATPT_SSP_MODE_I2S_CONSUMER;
665*4882a593Smuzhiyun 		devfmt.clock_divider = 0;
666*4882a593Smuzhiyun 		break;
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* see if this is a new configuration */
670*4882a593Smuzhiyun 	if (!memcmp(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt)))
671*4882a593Smuzhiyun 		return 0;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	ret = catpt_ipc_set_device_format(cdev, &devfmt);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
678*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (ret)
681*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	/* store device format set for given SSP */
684*4882a593Smuzhiyun 	memcpy(&cdev->devfmt[devfmt.iface], &devfmt, sizeof(devfmt));
685*4882a593Smuzhiyun 	return 0;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun static struct snd_soc_dai_driver dai_drivers[] = {
689*4882a593Smuzhiyun /* FE DAIs */
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun 	.name  = "System Pin",
692*4882a593Smuzhiyun 	.id = CATPT_STRM_TYPE_SYSTEM,
693*4882a593Smuzhiyun 	.ops = &catpt_fe_dai_ops,
694*4882a593Smuzhiyun 	.playback = {
695*4882a593Smuzhiyun 		.stream_name = "System Playback",
696*4882a593Smuzhiyun 		.channels_min = 2,
697*4882a593Smuzhiyun 		.channels_max = 2,
698*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_48000,
699*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
700*4882a593Smuzhiyun 	},
701*4882a593Smuzhiyun 	.capture = {
702*4882a593Smuzhiyun 		.stream_name = "Analog Capture",
703*4882a593Smuzhiyun 		.channels_min = 2,
704*4882a593Smuzhiyun 		.channels_max = 4,
705*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_48000,
706*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
707*4882a593Smuzhiyun 	},
708*4882a593Smuzhiyun },
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	.name  = "Offload0 Pin",
711*4882a593Smuzhiyun 	.id = CATPT_STRM_TYPE_RENDER,
712*4882a593Smuzhiyun 	.ops = &catpt_fe_dai_ops,
713*4882a593Smuzhiyun 	.playback = {
714*4882a593Smuzhiyun 		.stream_name = "Offload0 Playback",
715*4882a593Smuzhiyun 		.channels_min = 2,
716*4882a593Smuzhiyun 		.channels_max = 2,
717*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_8000_192000,
718*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
719*4882a593Smuzhiyun 	},
720*4882a593Smuzhiyun },
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	.name  = "Offload1 Pin",
723*4882a593Smuzhiyun 	.id = CATPT_STRM_TYPE_RENDER,
724*4882a593Smuzhiyun 	.ops = &catpt_fe_dai_ops,
725*4882a593Smuzhiyun 	.playback = {
726*4882a593Smuzhiyun 		.stream_name = "Offload1 Playback",
727*4882a593Smuzhiyun 		.channels_min = 2,
728*4882a593Smuzhiyun 		.channels_max = 2,
729*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_8000_192000,
730*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
731*4882a593Smuzhiyun 	},
732*4882a593Smuzhiyun },
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	.name  = "Loopback Pin",
735*4882a593Smuzhiyun 	.id = CATPT_STRM_TYPE_LOOPBACK,
736*4882a593Smuzhiyun 	.ops = &catpt_fe_dai_ops,
737*4882a593Smuzhiyun 	.capture = {
738*4882a593Smuzhiyun 		.stream_name = "Loopback Capture",
739*4882a593Smuzhiyun 		.channels_min = 2,
740*4882a593Smuzhiyun 		.channels_max = 2,
741*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_48000,
742*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
743*4882a593Smuzhiyun 	},
744*4882a593Smuzhiyun },
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	.name  = "Bluetooth Pin",
747*4882a593Smuzhiyun 	.id = CATPT_STRM_TYPE_BLUETOOTH_RENDER,
748*4882a593Smuzhiyun 	.ops = &catpt_fe_dai_ops,
749*4882a593Smuzhiyun 	.playback = {
750*4882a593Smuzhiyun 		.stream_name = "Bluetooth Playback",
751*4882a593Smuzhiyun 		.channels_min = 1,
752*4882a593Smuzhiyun 		.channels_max = 1,
753*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_8000,
754*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
755*4882a593Smuzhiyun 	},
756*4882a593Smuzhiyun 	.capture = {
757*4882a593Smuzhiyun 		.stream_name = "Bluetooth Capture",
758*4882a593Smuzhiyun 		.channels_min = 1,
759*4882a593Smuzhiyun 		.channels_max = 1,
760*4882a593Smuzhiyun 		.rates = SNDRV_PCM_RATE_8000,
761*4882a593Smuzhiyun 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
762*4882a593Smuzhiyun 	},
763*4882a593Smuzhiyun },
764*4882a593Smuzhiyun /* BE DAIs */
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	.name = "ssp0-port",
767*4882a593Smuzhiyun 	.id = CATPT_SSP_IFACE_0,
768*4882a593Smuzhiyun 	.pcm_new = catpt_dai_pcm_new,
769*4882a593Smuzhiyun 	.playback = {
770*4882a593Smuzhiyun 		.channels_min = 1,
771*4882a593Smuzhiyun 		.channels_max = 8,
772*4882a593Smuzhiyun 	},
773*4882a593Smuzhiyun 	.capture = {
774*4882a593Smuzhiyun 		.channels_min = 1,
775*4882a593Smuzhiyun 		.channels_max = 8,
776*4882a593Smuzhiyun 	},
777*4882a593Smuzhiyun },
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	.name = "ssp1-port",
780*4882a593Smuzhiyun 	.id = CATPT_SSP_IFACE_1,
781*4882a593Smuzhiyun 	.pcm_new = catpt_dai_pcm_new,
782*4882a593Smuzhiyun 	.playback = {
783*4882a593Smuzhiyun 		.channels_min = 1,
784*4882a593Smuzhiyun 		.channels_max = 8,
785*4882a593Smuzhiyun 	},
786*4882a593Smuzhiyun 	.capture = {
787*4882a593Smuzhiyun 		.channels_min = 1,
788*4882a593Smuzhiyun 		.channels_max = 8,
789*4882a593Smuzhiyun 	},
790*4882a593Smuzhiyun },
791*4882a593Smuzhiyun };
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun #define DSP_VOLUME_MAX		S32_MAX /* 0db */
794*4882a593Smuzhiyun #define DSP_VOLUME_STEP_MAX	30
795*4882a593Smuzhiyun 
ctlvol_to_dspvol(u32 value)796*4882a593Smuzhiyun static u32 ctlvol_to_dspvol(u32 value)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	if (value > DSP_VOLUME_STEP_MAX)
799*4882a593Smuzhiyun 		value = 0;
800*4882a593Smuzhiyun 	return DSP_VOLUME_MAX >> (DSP_VOLUME_STEP_MAX - value);
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun 
dspvol_to_ctlvol(u32 volume)803*4882a593Smuzhiyun static u32 dspvol_to_ctlvol(u32 volume)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	if (volume > DSP_VOLUME_MAX)
806*4882a593Smuzhiyun 		return DSP_VOLUME_STEP_MAX;
807*4882a593Smuzhiyun 	return volume ? __fls(volume) : 0;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
catpt_set_dspvol(struct catpt_dev * cdev,u8 stream_id,long * ctlvol)810*4882a593Smuzhiyun static int catpt_set_dspvol(struct catpt_dev *cdev, u8 stream_id, long *ctlvol)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	u32 dspvol;
813*4882a593Smuzhiyun 	int ret, i;
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	for (i = 1; i < CATPT_CHANNELS_MAX; i++)
816*4882a593Smuzhiyun 		if (ctlvol[i] != ctlvol[0])
817*4882a593Smuzhiyun 			break;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	if (i == CATPT_CHANNELS_MAX) {
820*4882a593Smuzhiyun 		dspvol = ctlvol_to_dspvol(ctlvol[0]);
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		ret = catpt_ipc_set_volume(cdev, stream_id,
823*4882a593Smuzhiyun 					   CATPT_ALL_CHANNELS_MASK, dspvol,
824*4882a593Smuzhiyun 					   0, CATPT_AUDIO_CURVE_NONE);
825*4882a593Smuzhiyun 	} else {
826*4882a593Smuzhiyun 		for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
827*4882a593Smuzhiyun 			dspvol = ctlvol_to_dspvol(ctlvol[i]);
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 			ret = catpt_ipc_set_volume(cdev, stream_id,
830*4882a593Smuzhiyun 						   i, dspvol,
831*4882a593Smuzhiyun 						   0, CATPT_AUDIO_CURVE_NONE);
832*4882a593Smuzhiyun 			if (ret)
833*4882a593Smuzhiyun 				break;
834*4882a593Smuzhiyun 		}
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	if (ret)
838*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
839*4882a593Smuzhiyun 	return 0;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun 
catpt_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)842*4882a593Smuzhiyun static int catpt_volume_info(struct snd_kcontrol *kcontrol,
843*4882a593Smuzhiyun 			     struct snd_ctl_elem_info *uinfo)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
846*4882a593Smuzhiyun 	uinfo->count = CATPT_CHANNELS_MAX;
847*4882a593Smuzhiyun 	uinfo->value.integer.min = 0;
848*4882a593Smuzhiyun 	uinfo->value.integer.max = DSP_VOLUME_STEP_MAX;
849*4882a593Smuzhiyun 	return 0;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun 
catpt_mixer_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)852*4882a593Smuzhiyun static int catpt_mixer_volume_get(struct snd_kcontrol *kcontrol,
853*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	struct snd_soc_component *component =
856*4882a593Smuzhiyun 		snd_soc_kcontrol_component(kcontrol);
857*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
858*4882a593Smuzhiyun 	u32 dspvol;
859*4882a593Smuzhiyun 	int i;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
864*4882a593Smuzhiyun 		dspvol = catpt_mixer_volume(cdev, &cdev->mixer, i);
865*4882a593Smuzhiyun 		ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
866*4882a593Smuzhiyun 	}
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
869*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
catpt_mixer_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)874*4882a593Smuzhiyun static int catpt_mixer_volume_put(struct snd_kcontrol *kcontrol,
875*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct snd_soc_component *component =
878*4882a593Smuzhiyun 		snd_soc_kcontrol_component(kcontrol);
879*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
880*4882a593Smuzhiyun 	int ret;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	ret = catpt_set_dspvol(cdev, cdev->mixer.mixer_hw_id,
885*4882a593Smuzhiyun 			       ucontrol->value.integer.value);
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
888*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	return ret;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun 
catpt_stream_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol,enum catpt_pin_id pin_id)893*4882a593Smuzhiyun static int catpt_stream_volume_get(struct snd_kcontrol *kcontrol,
894*4882a593Smuzhiyun 				   struct snd_ctl_elem_value *ucontrol,
895*4882a593Smuzhiyun 				   enum catpt_pin_id pin_id)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	struct snd_soc_component *component =
898*4882a593Smuzhiyun 		snd_soc_kcontrol_component(kcontrol);
899*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
900*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
901*4882a593Smuzhiyun 	long *ctlvol = (long *)kcontrol->private_value;
902*4882a593Smuzhiyun 	u32 dspvol;
903*4882a593Smuzhiyun 	int i;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	stream = catpt_stream_find(cdev, pin_id);
906*4882a593Smuzhiyun 	if (!stream) {
907*4882a593Smuzhiyun 		for (i = 0; i < CATPT_CHANNELS_MAX; i++)
908*4882a593Smuzhiyun 			ucontrol->value.integer.value[i] = ctlvol[i];
909*4882a593Smuzhiyun 		return 0;
910*4882a593Smuzhiyun 	}
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	for (i = 0; i < CATPT_CHANNELS_MAX; i++) {
915*4882a593Smuzhiyun 		dspvol = catpt_stream_volume(cdev, stream, i);
916*4882a593Smuzhiyun 		ucontrol->value.integer.value[i] = dspvol_to_ctlvol(dspvol);
917*4882a593Smuzhiyun 	}
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
920*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	return 0;
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun 
catpt_stream_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol,enum catpt_pin_id pin_id)925*4882a593Smuzhiyun static int catpt_stream_volume_put(struct snd_kcontrol *kcontrol,
926*4882a593Smuzhiyun 				   struct snd_ctl_elem_value *ucontrol,
927*4882a593Smuzhiyun 				   enum catpt_pin_id pin_id)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	struct snd_soc_component *component =
930*4882a593Smuzhiyun 		snd_soc_kcontrol_component(kcontrol);
931*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
932*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
933*4882a593Smuzhiyun 	long *ctlvol = (long *)kcontrol->private_value;
934*4882a593Smuzhiyun 	int ret, i;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	stream = catpt_stream_find(cdev, pin_id);
937*4882a593Smuzhiyun 	if (!stream) {
938*4882a593Smuzhiyun 		for (i = 0; i < CATPT_CHANNELS_MAX; i++)
939*4882a593Smuzhiyun 			ctlvol[i] = ucontrol->value.integer.value[i];
940*4882a593Smuzhiyun 		return 0;
941*4882a593Smuzhiyun 	}
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	ret = catpt_set_dspvol(cdev, stream->info.stream_hw_id,
946*4882a593Smuzhiyun 			       ucontrol->value.integer.value);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
949*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	if (ret)
952*4882a593Smuzhiyun 		return ret;
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	for (i = 0; i < CATPT_CHANNELS_MAX; i++)
955*4882a593Smuzhiyun 		ctlvol[i] = ucontrol->value.integer.value[i];
956*4882a593Smuzhiyun 	return 0;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun 
catpt_offload1_volume_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)959*4882a593Smuzhiyun static int catpt_offload1_volume_get(struct snd_kcontrol *kctl,
960*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *uctl)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun 	return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
catpt_offload1_volume_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)965*4882a593Smuzhiyun static int catpt_offload1_volume_put(struct snd_kcontrol *kctl,
966*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *uctl)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD1);
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun 
catpt_offload2_volume_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)971*4882a593Smuzhiyun static int catpt_offload2_volume_get(struct snd_kcontrol *kctl,
972*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *uctl)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun 	return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun 
catpt_offload2_volume_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)977*4882a593Smuzhiyun static int catpt_offload2_volume_put(struct snd_kcontrol *kctl,
978*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *uctl)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun 	return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_OFFLOAD2);
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun 
catpt_capture_volume_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)983*4882a593Smuzhiyun static int catpt_capture_volume_get(struct snd_kcontrol *kctl,
984*4882a593Smuzhiyun 				    struct snd_ctl_elem_value *uctl)
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	return catpt_stream_volume_get(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
catpt_capture_volume_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * uctl)989*4882a593Smuzhiyun static int catpt_capture_volume_put(struct snd_kcontrol *kctl,
990*4882a593Smuzhiyun 				    struct snd_ctl_elem_value *uctl)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun 	return catpt_stream_volume_put(kctl, uctl, CATPT_PIN_ID_CAPTURE1);
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun 
catpt_loopback_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)995*4882a593Smuzhiyun static int catpt_loopback_switch_get(struct snd_kcontrol *kcontrol,
996*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *ucontrol)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun 	ucontrol->value.integer.value[0] = *(bool *)kcontrol->private_value;
999*4882a593Smuzhiyun 	return 0;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun 
catpt_loopback_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1002*4882a593Smuzhiyun static int catpt_loopback_switch_put(struct snd_kcontrol *kcontrol,
1003*4882a593Smuzhiyun 				     struct snd_ctl_elem_value *ucontrol)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun 	struct snd_soc_component *component =
1006*4882a593Smuzhiyun 		snd_soc_kcontrol_component(kcontrol);
1007*4882a593Smuzhiyun 	struct catpt_dev *cdev = dev_get_drvdata(component->dev);
1008*4882a593Smuzhiyun 	struct catpt_stream_runtime *stream;
1009*4882a593Smuzhiyun 	bool mute;
1010*4882a593Smuzhiyun 	int ret;
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	mute = (bool)ucontrol->value.integer.value[0];
1013*4882a593Smuzhiyun 	stream = catpt_stream_find(cdev, CATPT_PIN_ID_REFERENCE);
1014*4882a593Smuzhiyun 	if (!stream) {
1015*4882a593Smuzhiyun 		*(bool *)kcontrol->private_value = mute;
1016*4882a593Smuzhiyun 		return 0;
1017*4882a593Smuzhiyun 	}
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	pm_runtime_get_sync(cdev->dev);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	ret = catpt_ipc_mute_loopback(cdev, stream->info.stream_hw_id, mute);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(cdev->dev);
1024*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(cdev->dev);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	if (ret)
1027*4882a593Smuzhiyun 		return CATPT_IPC_ERROR(ret);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	*(bool *)kcontrol->private_value = mute;
1030*4882a593Smuzhiyun 	return 0;
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun 
catpt_waves_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1033*4882a593Smuzhiyun static int catpt_waves_switch_get(struct snd_kcontrol *kcontrol,
1034*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
1035*4882a593Smuzhiyun {
1036*4882a593Smuzhiyun 	return 0;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun 
catpt_waves_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1039*4882a593Smuzhiyun static int catpt_waves_switch_put(struct snd_kcontrol *kcontrol,
1040*4882a593Smuzhiyun 				  struct snd_ctl_elem_value *ucontrol)
1041*4882a593Smuzhiyun {
1042*4882a593Smuzhiyun 	return 0;
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun 
catpt_waves_param_get(struct snd_kcontrol * kcontrol,unsigned int __user * bytes,unsigned int size)1045*4882a593Smuzhiyun static int catpt_waves_param_get(struct snd_kcontrol *kcontrol,
1046*4882a593Smuzhiyun 				 unsigned int __user *bytes,
1047*4882a593Smuzhiyun 				 unsigned int size)
1048*4882a593Smuzhiyun {
1049*4882a593Smuzhiyun 	return 0;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun 
catpt_waves_param_put(struct snd_kcontrol * kcontrol,const unsigned int __user * bytes,unsigned int size)1052*4882a593Smuzhiyun static int catpt_waves_param_put(struct snd_kcontrol *kcontrol,
1053*4882a593Smuzhiyun 				 const unsigned int __user *bytes,
1054*4882a593Smuzhiyun 				 unsigned int size)
1055*4882a593Smuzhiyun {
1056*4882a593Smuzhiyun 	return 0;
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(catpt_volume_tlv, -9000, 300, 1);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun #define CATPT_VOLUME_CTL(kname, sname) \
1062*4882a593Smuzhiyun {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1063*4882a593Smuzhiyun 	.name = (kname), \
1064*4882a593Smuzhiyun 	.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1065*4882a593Smuzhiyun 		  SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1066*4882a593Smuzhiyun 	.info = catpt_volume_info, \
1067*4882a593Smuzhiyun 	.get = catpt_##sname##_volume_get, \
1068*4882a593Smuzhiyun 	.put = catpt_##sname##_volume_put, \
1069*4882a593Smuzhiyun 	.tlv.p = catpt_volume_tlv, \
1070*4882a593Smuzhiyun 	.private_value = (unsigned long) \
1071*4882a593Smuzhiyun 		&(long[CATPT_CHANNELS_MAX]) {0} }
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun static const struct snd_kcontrol_new component_kcontrols[] = {
1074*4882a593Smuzhiyun /* Master volume (mixer stream) */
1075*4882a593Smuzhiyun CATPT_VOLUME_CTL("Master Playback Volume", mixer),
1076*4882a593Smuzhiyun /* Individual volume controls for offload and capture */
1077*4882a593Smuzhiyun CATPT_VOLUME_CTL("Media0 Playback Volume", offload1),
1078*4882a593Smuzhiyun CATPT_VOLUME_CTL("Media1 Playback Volume", offload2),
1079*4882a593Smuzhiyun CATPT_VOLUME_CTL("Mic Capture Volume", capture),
1080*4882a593Smuzhiyun SOC_SINGLE_BOOL_EXT("Loopback Mute", (unsigned long)&(bool[1]) {0},
1081*4882a593Smuzhiyun 		    catpt_loopback_switch_get, catpt_loopback_switch_put),
1082*4882a593Smuzhiyun /* Enable or disable WAVES module */
1083*4882a593Smuzhiyun SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
1084*4882a593Smuzhiyun 		    catpt_waves_switch_get, catpt_waves_switch_put),
1085*4882a593Smuzhiyun /* WAVES module parameter control */
1086*4882a593Smuzhiyun SND_SOC_BYTES_TLV("Waves Set Param", 128,
1087*4882a593Smuzhiyun 		  catpt_waves_param_get, catpt_waves_param_put),
1088*4882a593Smuzhiyun };
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun static const struct snd_soc_dapm_widget component_widgets[] = {
1091*4882a593Smuzhiyun 	SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1092*4882a593Smuzhiyun 	SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1093*4882a593Smuzhiyun 	SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1094*4882a593Smuzhiyun 	SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1097*4882a593Smuzhiyun };
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun static const struct snd_soc_dapm_route component_routes[] = {
1100*4882a593Smuzhiyun 	{"Playback VMixer", NULL, "System Playback"},
1101*4882a593Smuzhiyun 	{"Playback VMixer", NULL, "Offload0 Playback"},
1102*4882a593Smuzhiyun 	{"Playback VMixer", NULL, "Offload1 Playback"},
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	{"SSP0 CODEC OUT", NULL, "Playback VMixer"},
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	{"Analog Capture", NULL, "SSP0 CODEC IN"},
1107*4882a593Smuzhiyun 	{"Loopback Capture", NULL, "SSP0 CODEC IN"},
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	{"SSP1 BT OUT", NULL, "Bluetooth Playback"},
1110*4882a593Smuzhiyun 	{"Bluetooth Capture", NULL, "SSP1 BT IN"},
1111*4882a593Smuzhiyun };
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun static const struct snd_soc_component_driver catpt_comp_driver = {
1114*4882a593Smuzhiyun 	.name = "catpt-platform",
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	.pcm_construct = catpt_component_pcm_construct,
1117*4882a593Smuzhiyun 	.open = catpt_component_open,
1118*4882a593Smuzhiyun 	.pointer = catpt_component_pointer,
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	.controls = component_kcontrols,
1121*4882a593Smuzhiyun 	.num_controls = ARRAY_SIZE(component_kcontrols),
1122*4882a593Smuzhiyun 	.dapm_widgets = component_widgets,
1123*4882a593Smuzhiyun 	.num_dapm_widgets = ARRAY_SIZE(component_widgets),
1124*4882a593Smuzhiyun 	.dapm_routes = component_routes,
1125*4882a593Smuzhiyun 	.num_dapm_routes = ARRAY_SIZE(component_routes),
1126*4882a593Smuzhiyun };
1127*4882a593Smuzhiyun 
catpt_arm_stream_templates(struct catpt_dev * cdev)1128*4882a593Smuzhiyun int catpt_arm_stream_templates(struct catpt_dev *cdev)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	struct resource *res;
1131*4882a593Smuzhiyun 	u32 scratch_size = 0;
1132*4882a593Smuzhiyun 	int i, j;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(catpt_topology); i++) {
1135*4882a593Smuzhiyun 		struct catpt_stream_template *template;
1136*4882a593Smuzhiyun 		struct catpt_module_entry *entry;
1137*4882a593Smuzhiyun 		struct catpt_module_type *type;
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 		template = catpt_topology[i];
1140*4882a593Smuzhiyun 		template->persistent_size = 0;
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 		for (j = 0; j < template->num_entries; j++) {
1143*4882a593Smuzhiyun 			entry = &template->entries[j];
1144*4882a593Smuzhiyun 			type = &cdev->modules[entry->module_id];
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 			if (!type->loaded)
1147*4882a593Smuzhiyun 				return -ENOENT;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 			entry->entry_point = type->entry_point;
1150*4882a593Smuzhiyun 			template->persistent_size += type->persistent_size;
1151*4882a593Smuzhiyun 			if (type->scratch_size > scratch_size)
1152*4882a593Smuzhiyun 				scratch_size = type->scratch_size;
1153*4882a593Smuzhiyun 		}
1154*4882a593Smuzhiyun 	}
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	if (scratch_size) {
1157*4882a593Smuzhiyun 		/* allocate single scratch area for all modules */
1158*4882a593Smuzhiyun 		res = catpt_request_region(&cdev->dram, scratch_size);
1159*4882a593Smuzhiyun 		if (!res)
1160*4882a593Smuzhiyun 			return -EBUSY;
1161*4882a593Smuzhiyun 		cdev->scratch = res;
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	return 0;
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
catpt_register_plat_component(struct catpt_dev * cdev)1167*4882a593Smuzhiyun int catpt_register_plat_component(struct catpt_dev *cdev)
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun 	struct snd_soc_component *component;
1170*4882a593Smuzhiyun 	int ret;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	component = devm_kzalloc(cdev->dev, sizeof(*component), GFP_KERNEL);
1173*4882a593Smuzhiyun 	if (!component)
1174*4882a593Smuzhiyun 		return -ENOMEM;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	ret = snd_soc_component_initialize(component, &catpt_comp_driver,
1177*4882a593Smuzhiyun 					   cdev->dev);
1178*4882a593Smuzhiyun 	if (ret)
1179*4882a593Smuzhiyun 		return ret;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	component->name = catpt_comp_driver.name;
1182*4882a593Smuzhiyun 	return snd_soc_add_component(component, dai_drivers,
1183*4882a593Smuzhiyun 				     ARRAY_SIZE(dai_drivers));
1184*4882a593Smuzhiyun }
1185