1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2311ce4feSLiam Girdwood //
3311ce4feSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license. When using or
4311ce4feSLiam Girdwood // redistributing this file, you may do so under either license.
5311ce4feSLiam Girdwood //
6311ce4feSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7311ce4feSLiam Girdwood //
8311ce4feSLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9311ce4feSLiam Girdwood //
10311ce4feSLiam Girdwood
11d1c6c4a9SGuennadi Liakhovetski #include <linux/bits.h>
12d1c6c4a9SGuennadi Liakhovetski #include <linux/device.h>
13d1c6c4a9SGuennadi Liakhovetski #include <linux/errno.h>
14311ce4feSLiam Girdwood #include <linux/firmware.h>
159ef91cadSGuennadi Liakhovetski #include <linux/workqueue.h>
16311ce4feSLiam Girdwood #include <sound/tlv.h>
17311ce4feSLiam Girdwood #include <uapi/sound/sof/tokens.h>
18311ce4feSLiam Girdwood #include "sof-priv.h"
19ee1e79b7SRanjani Sridharan #include "sof-audio.h"
20311ce4feSLiam Girdwood #include "ops.h"
21311ce4feSLiam Girdwood
22311ce4feSLiam Girdwood #define COMP_ID_UNASSIGNED 0xffffffff
23311ce4feSLiam Girdwood /*
24311ce4feSLiam Girdwood * Constants used in the computation of linear volume gain
25311ce4feSLiam Girdwood * from dB gain 20th root of 10 in Q1.16 fixed-point notation
26311ce4feSLiam Girdwood */
27311ce4feSLiam Girdwood #define VOL_TWENTIETH_ROOT_OF_TEN 73533
28311ce4feSLiam Girdwood /* 40th root of 10 in Q1.16 fixed-point notation*/
29311ce4feSLiam Girdwood #define VOL_FORTIETH_ROOT_OF_TEN 69419
30b5cee8feSRanjani Sridharan
31311ce4feSLiam Girdwood /* 0.5 dB step value in topology TLV */
32311ce4feSLiam Girdwood #define VOL_HALF_DB_STEP 50
33311ce4feSLiam Girdwood
34311ce4feSLiam Girdwood /* TLV data items */
35311ce4feSLiam Girdwood #define TLV_MIN 0
36311ce4feSLiam Girdwood #define TLV_STEP 1
37311ce4feSLiam Girdwood #define TLV_MUTE 2
38311ce4feSLiam Girdwood
39d87524bfSRanjani Sridharan /**
40d87524bfSRanjani Sridharan * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
41d87524bfSRanjani Sridharan * token ID.
42d87524bfSRanjani Sridharan * @scomp: pointer to SOC component
43d87524bfSRanjani Sridharan * @object: target IPC struct to save the parsed values
44d87524bfSRanjani Sridharan * @token_id: token ID for the token array to be searched
45d87524bfSRanjani Sridharan * @tuples: pointer to the tuples array
46d87524bfSRanjani Sridharan * @num_tuples: number of tuples in the tuples array
47d87524bfSRanjani Sridharan * @object_size: size of the object
48d87524bfSRanjani Sridharan * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
49d87524bfSRanjani Sridharan * looks for @token_instance_num of each token in the token array associated
50d87524bfSRanjani Sridharan * with the @token_id
51d87524bfSRanjani Sridharan */
sof_update_ipc_object(struct snd_soc_component * scomp,void * object,enum sof_tokens token_id,struct snd_sof_tuple * tuples,int num_tuples,size_t object_size,int token_instance_num)52d87524bfSRanjani Sridharan int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
53d87524bfSRanjani Sridharan struct snd_sof_tuple *tuples, int num_tuples,
54d87524bfSRanjani Sridharan size_t object_size, int token_instance_num)
55d87524bfSRanjani Sridharan {
56d87524bfSRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
57dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
58dbdbf88bSPeter Ujfalusi const struct sof_token_info *token_list;
59d87524bfSRanjani Sridharan const struct sof_topology_token *tokens;
60d87524bfSRanjani Sridharan int i, j;
61d87524bfSRanjani Sridharan
62dbdbf88bSPeter Ujfalusi token_list = tplg_ops ? tplg_ops->token_list : NULL;
63dbdbf88bSPeter Ujfalusi /* nothing to do if token_list is NULL */
64dbdbf88bSPeter Ujfalusi if (!token_list)
65dbdbf88bSPeter Ujfalusi return 0;
66dbdbf88bSPeter Ujfalusi
67d87524bfSRanjani Sridharan if (token_list[token_id].count < 0) {
68d87524bfSRanjani Sridharan dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
69d87524bfSRanjani Sridharan return -EINVAL;
70d87524bfSRanjani Sridharan }
71d87524bfSRanjani Sridharan
72d87524bfSRanjani Sridharan /* No tokens to match */
73d87524bfSRanjani Sridharan if (!token_list[token_id].count)
74d87524bfSRanjani Sridharan return 0;
75d87524bfSRanjani Sridharan
76d87524bfSRanjani Sridharan tokens = token_list[token_id].tokens;
77d87524bfSRanjani Sridharan if (!tokens) {
78d87524bfSRanjani Sridharan dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
79d87524bfSRanjani Sridharan return -EINVAL;
80d87524bfSRanjani Sridharan }
81d87524bfSRanjani Sridharan
82d87524bfSRanjani Sridharan for (i = 0; i < token_list[token_id].count; i++) {
83d87524bfSRanjani Sridharan int offset = 0;
84d87524bfSRanjani Sridharan int num_tokens_matched = 0;
85d87524bfSRanjani Sridharan
86d87524bfSRanjani Sridharan for (j = 0; j < num_tuples; j++) {
87d87524bfSRanjani Sridharan if (tokens[i].token == tuples[j].token) {
88d87524bfSRanjani Sridharan switch (tokens[i].type) {
89d87524bfSRanjani Sridharan case SND_SOC_TPLG_TUPLE_TYPE_WORD:
90d87524bfSRanjani Sridharan {
91d87524bfSRanjani Sridharan u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
92d87524bfSRanjani Sridharan offset);
93d87524bfSRanjani Sridharan
94d87524bfSRanjani Sridharan *val = tuples[j].value.v;
95d87524bfSRanjani Sridharan break;
96d87524bfSRanjani Sridharan }
97d87524bfSRanjani Sridharan case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
98d87524bfSRanjani Sridharan case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
99d87524bfSRanjani Sridharan {
100d87524bfSRanjani Sridharan u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
101d87524bfSRanjani Sridharan offset);
102d87524bfSRanjani Sridharan
103d87524bfSRanjani Sridharan *val = (u16)tuples[j].value.v;
104d87524bfSRanjani Sridharan break;
105d87524bfSRanjani Sridharan }
106d87524bfSRanjani Sridharan case SND_SOC_TPLG_TUPLE_TYPE_STRING:
107d87524bfSRanjani Sridharan {
108d87524bfSRanjani Sridharan if (!tokens[i].get_token) {
109d87524bfSRanjani Sridharan dev_err(scomp->dev,
110d87524bfSRanjani Sridharan "get_token not defined for token %d in %s\n",
111d87524bfSRanjani Sridharan tokens[i].token, token_list[token_id].name);
112d87524bfSRanjani Sridharan return -EINVAL;
113d87524bfSRanjani Sridharan }
114d87524bfSRanjani Sridharan
115d87524bfSRanjani Sridharan tokens[i].get_token((void *)tuples[j].value.s, object,
116d87524bfSRanjani Sridharan tokens[i].offset + offset);
117d87524bfSRanjani Sridharan break;
118d87524bfSRanjani Sridharan }
119d87524bfSRanjani Sridharan default:
120d87524bfSRanjani Sridharan break;
121d87524bfSRanjani Sridharan }
122d87524bfSRanjani Sridharan
123d87524bfSRanjani Sridharan num_tokens_matched++;
124d87524bfSRanjani Sridharan
125d87524bfSRanjani Sridharan /* found all required sets of current token. Move to the next one */
126d87524bfSRanjani Sridharan if (!(num_tokens_matched % token_instance_num))
127d87524bfSRanjani Sridharan break;
128d87524bfSRanjani Sridharan
129d87524bfSRanjani Sridharan /* move to the next object */
130d87524bfSRanjani Sridharan offset += object_size;
131d87524bfSRanjani Sridharan }
132d87524bfSRanjani Sridharan }
133d87524bfSRanjani Sridharan }
134d87524bfSRanjani Sridharan
135d87524bfSRanjani Sridharan return 0;
136d87524bfSRanjani Sridharan }
137d87524bfSRanjani Sridharan
get_tlv_data(const int * p,int tlv[SOF_TLV_ITEMS])13848d2a1ceSRanjani Sridharan static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
139311ce4feSLiam Girdwood {
140311ce4feSLiam Girdwood /* we only support dB scale TLV type at the moment */
141311ce4feSLiam Girdwood if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
142311ce4feSLiam Girdwood return -EINVAL;
143311ce4feSLiam Girdwood
144311ce4feSLiam Girdwood /* min value in topology tlv data is multiplied by 100 */
145311ce4feSLiam Girdwood tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
146311ce4feSLiam Girdwood
147311ce4feSLiam Girdwood /* volume steps */
148311ce4feSLiam Girdwood tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
149311ce4feSLiam Girdwood TLV_DB_SCALE_MASK);
150311ce4feSLiam Girdwood
151311ce4feSLiam Girdwood /* mute ON/OFF */
152311ce4feSLiam Girdwood if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
153311ce4feSLiam Girdwood TLV_DB_SCALE_MUTE) == 0)
154311ce4feSLiam Girdwood tlv[TLV_MUTE] = 0;
155311ce4feSLiam Girdwood else
156311ce4feSLiam Girdwood tlv[TLV_MUTE] = 1;
157311ce4feSLiam Girdwood
158311ce4feSLiam Girdwood return 0;
159311ce4feSLiam Girdwood }
160311ce4feSLiam Girdwood
161311ce4feSLiam Girdwood /*
162311ce4feSLiam Girdwood * Function to truncate an unsigned 64-bit number
163311ce4feSLiam Girdwood * by x bits and return 32-bit unsigned number. This
164311ce4feSLiam Girdwood * function also takes care of rounding while truncating
165311ce4feSLiam Girdwood */
vol_shift_64(u64 i,u32 x)166311ce4feSLiam Girdwood static inline u32 vol_shift_64(u64 i, u32 x)
167311ce4feSLiam Girdwood {
168311ce4feSLiam Girdwood /* do not truncate more than 32 bits */
169311ce4feSLiam Girdwood if (x > 32)
170311ce4feSLiam Girdwood x = 32;
171311ce4feSLiam Girdwood
172311ce4feSLiam Girdwood if (x == 0)
173311ce4feSLiam Girdwood return (u32)i;
174311ce4feSLiam Girdwood
175311ce4feSLiam Girdwood return (u32)(((i >> (x - 1)) + 1) >> 1);
176311ce4feSLiam Girdwood }
177311ce4feSLiam Girdwood
178311ce4feSLiam Girdwood /*
179311ce4feSLiam Girdwood * Function to compute a ^ exp where,
180311ce4feSLiam Girdwood * a is a fractional number represented by a fixed-point
181311ce4feSLiam Girdwood * integer with a fractional world length of "fwl"
182311ce4feSLiam Girdwood * exp is an integer
183311ce4feSLiam Girdwood * fwl is the fractional word length
184311ce4feSLiam Girdwood * Return value is a fractional number represented by a
185311ce4feSLiam Girdwood * fixed-point integer with a fractional word length of "fwl"
186311ce4feSLiam Girdwood */
vol_pow32(u32 a,int exp,u32 fwl)187311ce4feSLiam Girdwood static u32 vol_pow32(u32 a, int exp, u32 fwl)
188311ce4feSLiam Girdwood {
189311ce4feSLiam Girdwood int i, iter;
190311ce4feSLiam Girdwood u32 power = 1 << fwl;
191311ce4feSLiam Girdwood u64 numerator;
192311ce4feSLiam Girdwood
193311ce4feSLiam Girdwood /* if exponent is 0, return 1 */
194311ce4feSLiam Girdwood if (exp == 0)
195311ce4feSLiam Girdwood return power;
196311ce4feSLiam Girdwood
197311ce4feSLiam Girdwood /* determine the number of iterations based on the exponent */
198311ce4feSLiam Girdwood if (exp < 0)
199311ce4feSLiam Girdwood iter = exp * -1;
200311ce4feSLiam Girdwood else
201311ce4feSLiam Girdwood iter = exp;
202311ce4feSLiam Girdwood
203311ce4feSLiam Girdwood /* mutiply a "iter" times to compute power */
204311ce4feSLiam Girdwood for (i = 0; i < iter; i++) {
205311ce4feSLiam Girdwood /*
206311ce4feSLiam Girdwood * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
207311ce4feSLiam Girdwood * Truncate product back to fwl fractional bits with rounding
208311ce4feSLiam Girdwood */
209311ce4feSLiam Girdwood power = vol_shift_64((u64)power * a, fwl);
210311ce4feSLiam Girdwood }
211311ce4feSLiam Girdwood
212311ce4feSLiam Girdwood if (exp > 0) {
213311ce4feSLiam Girdwood /* if exp is positive, return the result */
214311ce4feSLiam Girdwood return power;
215311ce4feSLiam Girdwood }
216311ce4feSLiam Girdwood
217311ce4feSLiam Girdwood /* if exp is negative, return the multiplicative inverse */
218311ce4feSLiam Girdwood numerator = (u64)1 << (fwl << 1);
219311ce4feSLiam Girdwood do_div(numerator, power);
220311ce4feSLiam Girdwood
221311ce4feSLiam Girdwood return (u32)numerator;
222311ce4feSLiam Girdwood }
223311ce4feSLiam Girdwood
224311ce4feSLiam Girdwood /*
225311ce4feSLiam Girdwood * Function to calculate volume gain from TLV data.
226311ce4feSLiam Girdwood * This function can only handle gain steps that are multiples of 0.5 dB
227311ce4feSLiam Girdwood */
vol_compute_gain(u32 value,int * tlv)22848d2a1ceSRanjani Sridharan u32 vol_compute_gain(u32 value, int *tlv)
229311ce4feSLiam Girdwood {
230311ce4feSLiam Girdwood int dB_gain;
231311ce4feSLiam Girdwood u32 linear_gain;
232311ce4feSLiam Girdwood int f_step;
233311ce4feSLiam Girdwood
234311ce4feSLiam Girdwood /* mute volume */
235311ce4feSLiam Girdwood if (value == 0 && tlv[TLV_MUTE])
236311ce4feSLiam Girdwood return 0;
237311ce4feSLiam Girdwood
238311ce4feSLiam Girdwood /*
239311ce4feSLiam Girdwood * compute dB gain from tlv. tlv_step
240311ce4feSLiam Girdwood * in topology is multiplied by 100
241311ce4feSLiam Girdwood */
242311ce4feSLiam Girdwood dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
243311ce4feSLiam Girdwood
244311ce4feSLiam Girdwood /*
245311ce4feSLiam Girdwood * compute linear gain represented by fixed-point
246311ce4feSLiam Girdwood * int with VOLUME_FWL fractional bits
247311ce4feSLiam Girdwood */
248311ce4feSLiam Girdwood linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
249311ce4feSLiam Girdwood
250311ce4feSLiam Girdwood /* extract the fractional part of volume step */
251311ce4feSLiam Girdwood f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
252311ce4feSLiam Girdwood
253311ce4feSLiam Girdwood /* if volume step is an odd multiple of 0.5 dB */
254311ce4feSLiam Girdwood if (f_step == VOL_HALF_DB_STEP && (value & 1))
255311ce4feSLiam Girdwood linear_gain = vol_shift_64((u64)linear_gain *
256311ce4feSLiam Girdwood VOL_FORTIETH_ROOT_OF_TEN,
257311ce4feSLiam Girdwood VOLUME_FWL);
258311ce4feSLiam Girdwood
259311ce4feSLiam Girdwood return linear_gain;
260311ce4feSLiam Girdwood }
261311ce4feSLiam Girdwood
262311ce4feSLiam Girdwood /*
263311ce4feSLiam Girdwood * Set up volume table for kcontrols from tlv data
264311ce4feSLiam Girdwood * "size" specifies the number of entries in the table
265311ce4feSLiam Girdwood */
set_up_volume_table(struct snd_sof_control * scontrol,int tlv[SOF_TLV_ITEMS],int size)266311ce4feSLiam Girdwood static int set_up_volume_table(struct snd_sof_control *scontrol,
26748d2a1ceSRanjani Sridharan int tlv[SOF_TLV_ITEMS], int size)
268311ce4feSLiam Girdwood {
26948d2a1ceSRanjani Sridharan struct snd_soc_component *scomp = scontrol->scomp;
27048d2a1ceSRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
271dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
272311ce4feSLiam Girdwood
273dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table)
27448d2a1ceSRanjani Sridharan return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
275311ce4feSLiam Girdwood
27648d2a1ceSRanjani Sridharan dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
27748d2a1ceSRanjani Sridharan return -EINVAL;
278311ce4feSLiam Girdwood }
279311ce4feSLiam Girdwood
280311ce4feSLiam Girdwood struct sof_dai_types {
281311ce4feSLiam Girdwood const char *name;
282311ce4feSLiam Girdwood enum sof_ipc_dai_type type;
283311ce4feSLiam Girdwood };
284311ce4feSLiam Girdwood
285311ce4feSLiam Girdwood static const struct sof_dai_types sof_dais[] = {
286311ce4feSLiam Girdwood {"SSP", SOF_DAI_INTEL_SSP},
287311ce4feSLiam Girdwood {"HDA", SOF_DAI_INTEL_HDA},
288311ce4feSLiam Girdwood {"DMIC", SOF_DAI_INTEL_DMIC},
2894d6bbf1aSPierre-Louis Bossart {"ALH", SOF_DAI_INTEL_ALH},
290f59b16efSDaniel Baluta {"SAI", SOF_DAI_IMX_SAI},
291f59b16efSDaniel Baluta {"ESAI", SOF_DAI_IMX_ESAI},
292efb931cdSAjit Kumar Pandey {"ACP", SOF_DAI_AMD_BT},
293efb931cdSAjit Kumar Pandey {"ACPSP", SOF_DAI_AMD_SP},
294efb931cdSAjit Kumar Pandey {"ACPDMIC", SOF_DAI_AMD_DMIC},
295ed2562c6SV sujith kumar Reddy {"ACPHS", SOF_DAI_AMD_HS},
296b72bfcffSYC Hung {"AFE", SOF_DAI_MEDIATEK_AFE},
29775af4199SV sujith kumar Reddy {"ACPSP_VIRTUAL", SOF_DAI_AMD_SP_VIRTUAL},
29875af4199SV sujith kumar Reddy {"ACPHS_VIRTUAL", SOF_DAI_AMD_HS_VIRTUAL},
29975af4199SV sujith kumar Reddy
300311ce4feSLiam Girdwood };
301311ce4feSLiam Girdwood
find_dai(const char * name)302311ce4feSLiam Girdwood static enum sof_ipc_dai_type find_dai(const char *name)
303311ce4feSLiam Girdwood {
304311ce4feSLiam Girdwood int i;
305311ce4feSLiam Girdwood
306311ce4feSLiam Girdwood for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
307311ce4feSLiam Girdwood if (strcmp(name, sof_dais[i].name) == 0)
308311ce4feSLiam Girdwood return sof_dais[i].type;
309311ce4feSLiam Girdwood }
310311ce4feSLiam Girdwood
311311ce4feSLiam Girdwood return SOF_DAI_INTEL_NONE;
312311ce4feSLiam Girdwood }
313311ce4feSLiam Girdwood
314311ce4feSLiam Girdwood /*
315311ce4feSLiam Girdwood * Supported Frame format types and lookup, add new ones to end of list.
316311ce4feSLiam Girdwood */
317311ce4feSLiam Girdwood
318311ce4feSLiam Girdwood struct sof_frame_types {
319311ce4feSLiam Girdwood const char *name;
320311ce4feSLiam Girdwood enum sof_ipc_frame frame;
321311ce4feSLiam Girdwood };
322311ce4feSLiam Girdwood
323311ce4feSLiam Girdwood static const struct sof_frame_types sof_frames[] = {
324311ce4feSLiam Girdwood {"s16le", SOF_IPC_FRAME_S16_LE},
325311ce4feSLiam Girdwood {"s24le", SOF_IPC_FRAME_S24_4LE},
326311ce4feSLiam Girdwood {"s32le", SOF_IPC_FRAME_S32_LE},
327311ce4feSLiam Girdwood {"float", SOF_IPC_FRAME_FLOAT},
328311ce4feSLiam Girdwood };
329311ce4feSLiam Girdwood
find_format(const char * name)330311ce4feSLiam Girdwood static enum sof_ipc_frame find_format(const char *name)
331311ce4feSLiam Girdwood {
332311ce4feSLiam Girdwood int i;
333311ce4feSLiam Girdwood
334311ce4feSLiam Girdwood for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
335311ce4feSLiam Girdwood if (strcmp(name, sof_frames[i].name) == 0)
336311ce4feSLiam Girdwood return sof_frames[i].frame;
337311ce4feSLiam Girdwood }
338311ce4feSLiam Girdwood
339311ce4feSLiam Girdwood /* use s32le if nothing is specified */
340311ce4feSLiam Girdwood return SOF_IPC_FRAME_S32_LE;
341311ce4feSLiam Girdwood }
342311ce4feSLiam Girdwood
get_token_u32(void * elem,void * object,u32 offset)343ea7e5ee6SRanjani Sridharan int get_token_u32(void *elem, void *object, u32 offset)
344311ce4feSLiam Girdwood {
345311ce4feSLiam Girdwood struct snd_soc_tplg_vendor_value_elem *velem = elem;
346311ce4feSLiam Girdwood u32 *val = (u32 *)((u8 *)object + offset);
347311ce4feSLiam Girdwood
348311ce4feSLiam Girdwood *val = le32_to_cpu(velem->value);
349311ce4feSLiam Girdwood return 0;
350311ce4feSLiam Girdwood }
351311ce4feSLiam Girdwood
get_token_u16(void * elem,void * object,u32 offset)352ea7e5ee6SRanjani Sridharan int get_token_u16(void *elem, void *object, u32 offset)
353311ce4feSLiam Girdwood {
354311ce4feSLiam Girdwood struct snd_soc_tplg_vendor_value_elem *velem = elem;
355311ce4feSLiam Girdwood u16 *val = (u16 *)((u8 *)object + offset);
356311ce4feSLiam Girdwood
357311ce4feSLiam Girdwood *val = (u16)le32_to_cpu(velem->value);
358311ce4feSLiam Girdwood return 0;
359311ce4feSLiam Girdwood }
360311ce4feSLiam Girdwood
get_token_uuid(void * elem,void * object,u32 offset)361ea7e5ee6SRanjani Sridharan int get_token_uuid(void *elem, void *object, u32 offset)
36292f500cfSKeyon Jie {
36392f500cfSKeyon Jie struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
36492f500cfSKeyon Jie u8 *dst = (u8 *)object + offset;
36592f500cfSKeyon Jie
36692f500cfSKeyon Jie memcpy(dst, velem->uuid, UUID_SIZE);
36792f500cfSKeyon Jie
36892f500cfSKeyon Jie return 0;
36992f500cfSKeyon Jie }
37092f500cfSKeyon Jie
3713b3acedbSChao Song /*
3723b3acedbSChao Song * The string gets from topology will be stored in heap, the owner only
3733b3acedbSChao Song * holds a char* member point to the heap.
3743b3acedbSChao Song */
get_token_string(void * elem,void * object,u32 offset)3753b3acedbSChao Song int get_token_string(void *elem, void *object, u32 offset)
3763b3acedbSChao Song {
3773b3acedbSChao Song /* "dst" here points to the char* member of the owner */
3783b3acedbSChao Song char **dst = (char **)((u8 *)object + offset);
3793b3acedbSChao Song
3803b3acedbSChao Song *dst = kstrdup(elem, GFP_KERNEL);
3813b3acedbSChao Song if (!*dst)
3823b3acedbSChao Song return -ENOMEM;
3833b3acedbSChao Song return 0;
3843b3acedbSChao Song };
3853b3acedbSChao Song
get_token_comp_format(void * elem,void * object,u32 offset)386ea7e5ee6SRanjani Sridharan int get_token_comp_format(void *elem, void *object, u32 offset)
387311ce4feSLiam Girdwood {
388311ce4feSLiam Girdwood u32 *val = (u32 *)((u8 *)object + offset);
389311ce4feSLiam Girdwood
3905ef969e2SRanjani Sridharan *val = find_format((const char *)elem);
391311ce4feSLiam Girdwood return 0;
392311ce4feSLiam Girdwood }
393311ce4feSLiam Girdwood
get_token_dai_type(void * elem,void * object,u32 offset)394ea7e5ee6SRanjani Sridharan int get_token_dai_type(void *elem, void *object, u32 offset)
395311ce4feSLiam Girdwood {
396311ce4feSLiam Girdwood u32 *val = (u32 *)((u8 *)object + offset);
397311ce4feSLiam Girdwood
3985ef969e2SRanjani Sridharan *val = find_dai((const char *)elem);
399311ce4feSLiam Girdwood return 0;
400311ce4feSLiam Girdwood }
401311ce4feSLiam Girdwood
402311ce4feSLiam Girdwood /* PCM */
4034a949409SKeyon Jie static const struct sof_topology_token stream_tokens[] = {
404909dadf2SRanjani Sridharan {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
4056a6b5727SRanjani Sridharan offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
406909dadf2SRanjani Sridharan {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
4076a6b5727SRanjani Sridharan offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
4084a949409SKeyon Jie };
4094a949409SKeyon Jie
4105d43001aSJaska Uimonen /* Leds */
4115d43001aSJaska Uimonen static const struct sof_topology_token led_tokens[] = {
4125d43001aSJaska Uimonen {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
4136a6b5727SRanjani Sridharan offsetof(struct snd_sof_led_control, use_led)},
414909dadf2SRanjani Sridharan {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
415909dadf2SRanjani Sridharan offsetof(struct snd_sof_led_control, direction)},
416b72bfcffSYC Hung };
417b72bfcffSYC Hung
4186327c729SChao Song static const struct sof_topology_token comp_pin_tokens[] = {
419bb79f2a6SRanjani Sridharan {SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
420bb79f2a6SRanjani Sridharan offsetof(struct snd_sof_widget, num_input_pins)},
421bb79f2a6SRanjani Sridharan {SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
422bb79f2a6SRanjani Sridharan offsetof(struct snd_sof_widget, num_output_pins)},
4236327c729SChao Song };
4246327c729SChao Song
425bb79f2a6SRanjani Sridharan static const struct sof_topology_token comp_input_pin_binding_tokens[] = {
426bb79f2a6SRanjani Sridharan {SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
4273b3acedbSChao Song get_token_string, 0},
4283b3acedbSChao Song };
4293b3acedbSChao Song
430bb79f2a6SRanjani Sridharan static const struct sof_topology_token comp_output_pin_binding_tokens[] = {
431bb79f2a6SRanjani Sridharan {SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
4323b3acedbSChao Song get_token_string, 0},
4333b3acedbSChao Song };
4343b3acedbSChao Song
43538a9a067SRanjani Sridharan /**
43638a9a067SRanjani Sridharan * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
43738a9a067SRanjani Sridharan * @scomp: pointer to soc component
43838a9a067SRanjani Sridharan * @object: target ipc struct for parsed values
43938a9a067SRanjani Sridharan * @offset: offset within the object pointer
44038a9a067SRanjani Sridharan * @tokens: array of struct sof_topology_token containing the tokens to be matched
44138a9a067SRanjani Sridharan * @num_tokens: number of tokens in tokens array
44238a9a067SRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
44338a9a067SRanjani Sridharan *
44438a9a067SRanjani Sridharan * This function parses multiple sets of string type tokens in vendor arrays
44538a9a067SRanjani Sridharan */
sof_parse_uuid_tokens(struct snd_soc_component * scomp,void * object,size_t offset,const struct sof_topology_token * tokens,int num_tokens,struct snd_soc_tplg_vendor_array * array)446f228a5b1SJaska Uimonen static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
44738a9a067SRanjani Sridharan void *object, size_t offset,
44838a9a067SRanjani Sridharan const struct sof_topology_token *tokens, int num_tokens,
44938a9a067SRanjani Sridharan struct snd_soc_tplg_vendor_array *array)
450311ce4feSLiam Girdwood {
451311ce4feSLiam Girdwood struct snd_soc_tplg_vendor_uuid_elem *elem;
452f228a5b1SJaska Uimonen int found = 0;
453311ce4feSLiam Girdwood int i, j;
454311ce4feSLiam Girdwood
455311ce4feSLiam Girdwood /* parse element by element */
456311ce4feSLiam Girdwood for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
457311ce4feSLiam Girdwood elem = &array->uuid[i];
458311ce4feSLiam Girdwood
459311ce4feSLiam Girdwood /* search for token */
46038a9a067SRanjani Sridharan for (j = 0; j < num_tokens; j++) {
461311ce4feSLiam Girdwood /* match token type */
462311ce4feSLiam Girdwood if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
463311ce4feSLiam Girdwood continue;
464311ce4feSLiam Girdwood
465311ce4feSLiam Girdwood /* match token id */
466311ce4feSLiam Girdwood if (tokens[j].token != le32_to_cpu(elem->token))
467311ce4feSLiam Girdwood continue;
468311ce4feSLiam Girdwood
469311ce4feSLiam Girdwood /* matched - now load token */
470a1687c68SJaska Uimonen tokens[j].get_token(elem, object,
4716a6b5727SRanjani Sridharan offset + tokens[j].offset);
472f228a5b1SJaska Uimonen
473f228a5b1SJaska Uimonen found++;
474311ce4feSLiam Girdwood }
475311ce4feSLiam Girdwood }
476311ce4feSLiam Girdwood
477f228a5b1SJaska Uimonen return found;
478f228a5b1SJaska Uimonen }
479f228a5b1SJaska Uimonen
48038a9a067SRanjani Sridharan /**
4817006d20eSRanjani Sridharan * sof_copy_tuples - Parse tokens and copy them to the @tuples array
4827006d20eSRanjani Sridharan * @sdev: pointer to struct snd_sof_dev
4837006d20eSRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
4847006d20eSRanjani Sridharan * @array_size: size of @array
4857006d20eSRanjani Sridharan * @token_id: Token ID associated with a token array
4867006d20eSRanjani Sridharan * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
4877006d20eSRanjani Sridharan * looks for @token_instance_num of each token in the token array associated
4887006d20eSRanjani Sridharan * with the @token_id
4897006d20eSRanjani Sridharan * @tuples: tuples array to copy the matched tuples to
4907006d20eSRanjani Sridharan * @tuples_size: size of @tuples
4917006d20eSRanjani Sridharan * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
4927006d20eSRanjani Sridharan *
4937006d20eSRanjani Sridharan */
sof_copy_tuples(struct snd_sof_dev * sdev,struct snd_soc_tplg_vendor_array * array,int array_size,u32 token_id,int token_instance_num,struct snd_sof_tuple * tuples,int tuples_size,int * num_copied_tuples)4947006d20eSRanjani Sridharan static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
4957006d20eSRanjani Sridharan int array_size, u32 token_id, int token_instance_num,
4967006d20eSRanjani Sridharan struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
4977006d20eSRanjani Sridharan {
498dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
499dbdbf88bSPeter Ujfalusi const struct sof_token_info *token_list;
5007006d20eSRanjani Sridharan const struct sof_topology_token *tokens;
5017006d20eSRanjani Sridharan int found = 0;
5027006d20eSRanjani Sridharan int num_tokens, asize;
5037006d20eSRanjani Sridharan int i, j;
5047006d20eSRanjani Sridharan
505dbdbf88bSPeter Ujfalusi token_list = tplg_ops ? tplg_ops->token_list : NULL;
5067006d20eSRanjani Sridharan /* nothing to do if token_list is NULL */
5077006d20eSRanjani Sridharan if (!token_list)
5087006d20eSRanjani Sridharan return 0;
5097006d20eSRanjani Sridharan
5107006d20eSRanjani Sridharan if (!tuples || !num_copied_tuples) {
5117006d20eSRanjani Sridharan dev_err(sdev->dev, "Invalid tuples array\n");
5127006d20eSRanjani Sridharan return -EINVAL;
5137006d20eSRanjani Sridharan }
5147006d20eSRanjani Sridharan
5157006d20eSRanjani Sridharan tokens = token_list[token_id].tokens;
5167006d20eSRanjani Sridharan num_tokens = token_list[token_id].count;
5177006d20eSRanjani Sridharan
5187006d20eSRanjani Sridharan if (!tokens) {
5197006d20eSRanjani Sridharan dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
5207006d20eSRanjani Sridharan return -EINVAL;
5217006d20eSRanjani Sridharan }
5227006d20eSRanjani Sridharan
5237006d20eSRanjani Sridharan /* check if there's space in the tuples array for new tokens */
5247006d20eSRanjani Sridharan if (*num_copied_tuples >= tuples_size) {
5257006d20eSRanjani Sridharan dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
5267006d20eSRanjani Sridharan token_list[token_id].name);
5277006d20eSRanjani Sridharan return -EINVAL;
5287006d20eSRanjani Sridharan }
5297006d20eSRanjani Sridharan
5307006d20eSRanjani Sridharan while (array_size > 0 && found < num_tokens * token_instance_num) {
5317006d20eSRanjani Sridharan asize = le32_to_cpu(array->size);
5327006d20eSRanjani Sridharan
5337006d20eSRanjani Sridharan /* validate asize */
5347006d20eSRanjani Sridharan if (asize < 0) {
5357006d20eSRanjani Sridharan dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
5367006d20eSRanjani Sridharan return -EINVAL;
5377006d20eSRanjani Sridharan }
5387006d20eSRanjani Sridharan
5397006d20eSRanjani Sridharan /* make sure there is enough data before parsing */
5407006d20eSRanjani Sridharan array_size -= asize;
5417006d20eSRanjani Sridharan if (array_size < 0) {
5427006d20eSRanjani Sridharan dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
5437006d20eSRanjani Sridharan return -EINVAL;
5447006d20eSRanjani Sridharan }
5457006d20eSRanjani Sridharan
5467006d20eSRanjani Sridharan /* parse element by element */
5477006d20eSRanjani Sridharan for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
5487006d20eSRanjani Sridharan /* search for token */
5497006d20eSRanjani Sridharan for (j = 0; j < num_tokens; j++) {
5507006d20eSRanjani Sridharan /* match token type */
5517006d20eSRanjani Sridharan if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
5527006d20eSRanjani Sridharan tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
5537006d20eSRanjani Sridharan tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
5547006d20eSRanjani Sridharan tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
5557006d20eSRanjani Sridharan tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
5567006d20eSRanjani Sridharan continue;
5577006d20eSRanjani Sridharan
5587006d20eSRanjani Sridharan if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
5597006d20eSRanjani Sridharan struct snd_soc_tplg_vendor_string_elem *elem;
5607006d20eSRanjani Sridharan
5617006d20eSRanjani Sridharan elem = &array->string[i];
5627006d20eSRanjani Sridharan
5637006d20eSRanjani Sridharan /* match token id */
5647006d20eSRanjani Sridharan if (tokens[j].token != le32_to_cpu(elem->token))
5657006d20eSRanjani Sridharan continue;
5667006d20eSRanjani Sridharan
5677006d20eSRanjani Sridharan tuples[*num_copied_tuples].token = tokens[j].token;
5687006d20eSRanjani Sridharan tuples[*num_copied_tuples].value.s = elem->string;
5697006d20eSRanjani Sridharan } else {
5707006d20eSRanjani Sridharan struct snd_soc_tplg_vendor_value_elem *elem;
5717006d20eSRanjani Sridharan
5727006d20eSRanjani Sridharan elem = &array->value[i];
5737006d20eSRanjani Sridharan
5747006d20eSRanjani Sridharan /* match token id */
5757006d20eSRanjani Sridharan if (tokens[j].token != le32_to_cpu(elem->token))
5767006d20eSRanjani Sridharan continue;
5777006d20eSRanjani Sridharan
5787006d20eSRanjani Sridharan tuples[*num_copied_tuples].token = tokens[j].token;
5797006d20eSRanjani Sridharan tuples[*num_copied_tuples].value.v =
5807006d20eSRanjani Sridharan le32_to_cpu(elem->value);
5817006d20eSRanjani Sridharan }
5827006d20eSRanjani Sridharan found++;
5837006d20eSRanjani Sridharan (*num_copied_tuples)++;
5847006d20eSRanjani Sridharan
5857006d20eSRanjani Sridharan /* stop if there's no space for any more new tuples */
5867006d20eSRanjani Sridharan if (*num_copied_tuples == tuples_size)
5877006d20eSRanjani Sridharan return 0;
5887006d20eSRanjani Sridharan }
58941c5305cSRanjani Sridharan
59041c5305cSRanjani Sridharan /* stop when we've found the required token instances */
59141c5305cSRanjani Sridharan if (found == num_tokens * token_instance_num)
59241c5305cSRanjani Sridharan return 0;
5937006d20eSRanjani Sridharan }
5947006d20eSRanjani Sridharan
5957006d20eSRanjani Sridharan /* next array */
5967006d20eSRanjani Sridharan array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
5977006d20eSRanjani Sridharan }
5987006d20eSRanjani Sridharan
5997006d20eSRanjani Sridharan return 0;
6007006d20eSRanjani Sridharan }
6017006d20eSRanjani Sridharan
6027006d20eSRanjani Sridharan /**
60338a9a067SRanjani Sridharan * sof_parse_string_tokens - Parse multiple sets of tokens
60438a9a067SRanjani Sridharan * @scomp: pointer to soc component
60538a9a067SRanjani Sridharan * @object: target ipc struct for parsed values
60638a9a067SRanjani Sridharan * @offset: offset within the object pointer
60738a9a067SRanjani Sridharan * @tokens: array of struct sof_topology_token containing the tokens to be matched
60838a9a067SRanjani Sridharan * @num_tokens: number of tokens in tokens array
60938a9a067SRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
61038a9a067SRanjani Sridharan *
61138a9a067SRanjani Sridharan * This function parses multiple sets of string type tokens in vendor arrays
61238a9a067SRanjani Sridharan */
sof_parse_string_tokens(struct snd_soc_component * scomp,void * object,int offset,const struct sof_topology_token * tokens,int num_tokens,struct snd_soc_tplg_vendor_array * array)613f228a5b1SJaska Uimonen static int sof_parse_string_tokens(struct snd_soc_component *scomp,
61438a9a067SRanjani Sridharan void *object, int offset,
61538a9a067SRanjani Sridharan const struct sof_topology_token *tokens, int num_tokens,
61638a9a067SRanjani Sridharan struct snd_soc_tplg_vendor_array *array)
617311ce4feSLiam Girdwood {
618311ce4feSLiam Girdwood struct snd_soc_tplg_vendor_string_elem *elem;
619f228a5b1SJaska Uimonen int found = 0;
6203b3acedbSChao Song int i, j, ret;
621311ce4feSLiam Girdwood
622311ce4feSLiam Girdwood /* parse element by element */
623311ce4feSLiam Girdwood for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
624311ce4feSLiam Girdwood elem = &array->string[i];
625311ce4feSLiam Girdwood
626311ce4feSLiam Girdwood /* search for token */
62738a9a067SRanjani Sridharan for (j = 0; j < num_tokens; j++) {
628311ce4feSLiam Girdwood /* match token type */
629311ce4feSLiam Girdwood if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
630311ce4feSLiam Girdwood continue;
631311ce4feSLiam Girdwood
632311ce4feSLiam Girdwood /* match token id */
633311ce4feSLiam Girdwood if (tokens[j].token != le32_to_cpu(elem->token))
634311ce4feSLiam Girdwood continue;
635311ce4feSLiam Girdwood
636311ce4feSLiam Girdwood /* matched - now load token */
6373b3acedbSChao Song ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
6383b3acedbSChao Song if (ret < 0)
6393b3acedbSChao Song return ret;
640f228a5b1SJaska Uimonen
641f228a5b1SJaska Uimonen found++;
642311ce4feSLiam Girdwood }
643311ce4feSLiam Girdwood }
644311ce4feSLiam Girdwood
645f228a5b1SJaska Uimonen return found;
646f228a5b1SJaska Uimonen }
647f228a5b1SJaska Uimonen
64838a9a067SRanjani Sridharan /**
64938a9a067SRanjani Sridharan * sof_parse_word_tokens - Parse multiple sets of tokens
65038a9a067SRanjani Sridharan * @scomp: pointer to soc component
65138a9a067SRanjani Sridharan * @object: target ipc struct for parsed values
65238a9a067SRanjani Sridharan * @offset: offset within the object pointer
65338a9a067SRanjani Sridharan * @tokens: array of struct sof_topology_token containing the tokens to be matched
65438a9a067SRanjani Sridharan * @num_tokens: number of tokens in tokens array
65538a9a067SRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
65638a9a067SRanjani Sridharan *
65738a9a067SRanjani Sridharan * This function parses multiple sets of word type tokens in vendor arrays
65838a9a067SRanjani Sridharan */
sof_parse_word_tokens(struct snd_soc_component * scomp,void * object,int offset,const struct sof_topology_token * tokens,int num_tokens,struct snd_soc_tplg_vendor_array * array)659f228a5b1SJaska Uimonen static int sof_parse_word_tokens(struct snd_soc_component *scomp,
66038a9a067SRanjani Sridharan void *object, int offset,
66138a9a067SRanjani Sridharan const struct sof_topology_token *tokens, int num_tokens,
66238a9a067SRanjani Sridharan struct snd_soc_tplg_vendor_array *array)
663311ce4feSLiam Girdwood {
664311ce4feSLiam Girdwood struct snd_soc_tplg_vendor_value_elem *elem;
665f228a5b1SJaska Uimonen int found = 0;
666311ce4feSLiam Girdwood int i, j;
667311ce4feSLiam Girdwood
668311ce4feSLiam Girdwood /* parse element by element */
669311ce4feSLiam Girdwood for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
670311ce4feSLiam Girdwood elem = &array->value[i];
671311ce4feSLiam Girdwood
672311ce4feSLiam Girdwood /* search for token */
67338a9a067SRanjani Sridharan for (j = 0; j < num_tokens; j++) {
674311ce4feSLiam Girdwood /* match token type */
675311ce4feSLiam Girdwood if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
6762e305a07SKeyon Jie tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
6772e305a07SKeyon Jie tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
6782e305a07SKeyon Jie tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
679311ce4feSLiam Girdwood continue;
680311ce4feSLiam Girdwood
681311ce4feSLiam Girdwood /* match token id */
682311ce4feSLiam Girdwood if (tokens[j].token != le32_to_cpu(elem->token))
683311ce4feSLiam Girdwood continue;
684311ce4feSLiam Girdwood
685311ce4feSLiam Girdwood /* load token */
68638a9a067SRanjani Sridharan tokens[j].get_token(elem, object, offset + tokens[j].offset);
687f228a5b1SJaska Uimonen
688f228a5b1SJaska Uimonen found++;
689311ce4feSLiam Girdwood }
690311ce4feSLiam Girdwood }
691f228a5b1SJaska Uimonen
692f228a5b1SJaska Uimonen return found;
693311ce4feSLiam Girdwood }
694311ce4feSLiam Girdwood
695a1687c68SJaska Uimonen /**
696a1687c68SJaska Uimonen * sof_parse_token_sets - Parse multiple sets of tokens
697a1687c68SJaska Uimonen * @scomp: pointer to soc component
698a1687c68SJaska Uimonen * @object: target ipc struct for parsed values
699a1687c68SJaska Uimonen * @tokens: token definition array describing what tokens to parse
700a1687c68SJaska Uimonen * @count: number of tokens in definition array
701e0974a38SRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
702e0974a38SRanjani Sridharan * @array_size: total size of @array
703e0974a38SRanjani Sridharan * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
704e0974a38SRanjani Sridharan * looks for @token_instance_num of each token in the @tokens
705a1687c68SJaska Uimonen * @object_size: offset to next target ipc struct with multiple sets
706a1687c68SJaska Uimonen *
707a1687c68SJaska Uimonen * This function parses multiple sets of tokens in vendor arrays into
708a1687c68SJaska Uimonen * consecutive ipc structs.
709a1687c68SJaska Uimonen */
sof_parse_token_sets(struct snd_soc_component * scomp,void * object,const struct sof_topology_token * tokens,int count,struct snd_soc_tplg_vendor_array * array,int array_size,int token_instance_num,size_t object_size)710a1687c68SJaska Uimonen static int sof_parse_token_sets(struct snd_soc_component *scomp,
711e0974a38SRanjani Sridharan void *object, const struct sof_topology_token *tokens,
712e0974a38SRanjani Sridharan int count, struct snd_soc_tplg_vendor_array *array,
713e0974a38SRanjani Sridharan int array_size, int token_instance_num, size_t object_size)
714311ce4feSLiam Girdwood {
715a1687c68SJaska Uimonen size_t offset = 0;
716f228a5b1SJaska Uimonen int found = 0;
717a1687c68SJaska Uimonen int total = 0;
718311ce4feSLiam Girdwood int asize;
7193b3acedbSChao Song int ret;
720311ce4feSLiam Girdwood
721e0974a38SRanjani Sridharan while (array_size > 0 && total < count * token_instance_num) {
722311ce4feSLiam Girdwood asize = le32_to_cpu(array->size);
723311ce4feSLiam Girdwood
724311ce4feSLiam Girdwood /* validate asize */
725311ce4feSLiam Girdwood if (asize < 0) { /* FIXME: A zero-size array makes no sense */
726ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: invalid array size 0x%x\n",
727311ce4feSLiam Girdwood asize);
728311ce4feSLiam Girdwood return -EINVAL;
729311ce4feSLiam Girdwood }
730311ce4feSLiam Girdwood
731311ce4feSLiam Girdwood /* make sure there is enough data before parsing */
732e0974a38SRanjani Sridharan array_size -= asize;
733e0974a38SRanjani Sridharan if (array_size < 0) {
734ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: invalid array size 0x%x\n",
735311ce4feSLiam Girdwood asize);
736311ce4feSLiam Girdwood return -EINVAL;
737311ce4feSLiam Girdwood }
738311ce4feSLiam Girdwood
739311ce4feSLiam Girdwood /* call correct parser depending on type */
740311ce4feSLiam Girdwood switch (le32_to_cpu(array->type)) {
741311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_UUID:
74238a9a067SRanjani Sridharan found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
74338a9a067SRanjani Sridharan array);
744311ce4feSLiam Girdwood break;
745311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_STRING:
7463b3acedbSChao Song
7473b3acedbSChao Song ret = sof_parse_string_tokens(scomp, object, offset, tokens, count,
74838a9a067SRanjani Sridharan array);
7493b3acedbSChao Song if (ret < 0) {
7503b3acedbSChao Song dev_err(scomp->dev, "error: no memory to copy string token\n");
7513b3acedbSChao Song return ret;
7523b3acedbSChao Song }
7533b3acedbSChao Song
7543b3acedbSChao Song found += ret;
755311ce4feSLiam Girdwood break;
756311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
757311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
758311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_WORD:
759311ce4feSLiam Girdwood case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
76038a9a067SRanjani Sridharan found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
76138a9a067SRanjani Sridharan array);
762311ce4feSLiam Girdwood break;
763311ce4feSLiam Girdwood default:
764ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: unknown token type %d\n",
765311ce4feSLiam Girdwood array->type);
766311ce4feSLiam Girdwood return -EINVAL;
767311ce4feSLiam Girdwood }
768311ce4feSLiam Girdwood
769311ce4feSLiam Girdwood /* next array */
770311ce4feSLiam Girdwood array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
771311ce4feSLiam Girdwood + asize);
772a1687c68SJaska Uimonen
773a1687c68SJaska Uimonen /* move to next target struct */
774a1687c68SJaska Uimonen if (found >= count) {
775a1687c68SJaska Uimonen offset += object_size;
776a1687c68SJaska Uimonen total += found;
777a1687c68SJaska Uimonen found = 0;
778311ce4feSLiam Girdwood }
779a1687c68SJaska Uimonen }
780a1687c68SJaska Uimonen
781311ce4feSLiam Girdwood return 0;
782311ce4feSLiam Girdwood }
783311ce4feSLiam Girdwood
7845f8333f6SRanjani Sridharan /**
7855f8333f6SRanjani Sridharan * sof_parse_tokens - Parse one set of tokens
7865f8333f6SRanjani Sridharan * @scomp: pointer to soc component
7875f8333f6SRanjani Sridharan * @object: target ipc struct for parsed values
7885f8333f6SRanjani Sridharan * @tokens: token definition array describing what tokens to parse
7895f8333f6SRanjani Sridharan * @num_tokens: number of tokens in definition array
7905f8333f6SRanjani Sridharan * @array: source pointer to consecutive vendor arrays in topology
7915f8333f6SRanjani Sridharan * @array_size: total size of @array
7925f8333f6SRanjani Sridharan *
7935f8333f6SRanjani Sridharan * This function parses a single set of tokens in vendor arrays into
7945f8333f6SRanjani Sridharan * consecutive ipc structs.
7955f8333f6SRanjani Sridharan */
sof_parse_tokens(struct snd_soc_component * scomp,void * object,const struct sof_topology_token * tokens,int num_tokens,struct snd_soc_tplg_vendor_array * array,int array_size)7965f8333f6SRanjani Sridharan static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
7975f8333f6SRanjani Sridharan const struct sof_topology_token *tokens, int num_tokens,
798a1687c68SJaska Uimonen struct snd_soc_tplg_vendor_array *array,
7995f8333f6SRanjani Sridharan int array_size)
8005f8333f6SRanjani Sridharan
801a1687c68SJaska Uimonen {
802a1687c68SJaska Uimonen /*
803a1687c68SJaska Uimonen * sof_parse_tokens is used when topology contains only a single set of
804a1687c68SJaska Uimonen * identical tuples arrays. So additional parameters to
805a1687c68SJaska Uimonen * sof_parse_token_sets are sets = 1 (only 1 set) and
806a1687c68SJaska Uimonen * object_size = 0 (irrelevant).
807a1687c68SJaska Uimonen */
8085f8333f6SRanjani Sridharan return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
8095f8333f6SRanjani Sridharan array_size, 1, 0);
810a1687c68SJaska Uimonen }
811a1687c68SJaska Uimonen
812acf1b71cSJaska Uimonen /*
813acf1b71cSJaska Uimonen * Standard Kcontrols.
814acf1b71cSJaska Uimonen */
815acf1b71cSJaska Uimonen
sof_control_load_volume(struct snd_soc_component * scomp,struct snd_sof_control * scontrol,struct snd_kcontrol_new * kc,struct snd_soc_tplg_ctl_hdr * hdr)816acf1b71cSJaska Uimonen static int sof_control_load_volume(struct snd_soc_component *scomp,
817acf1b71cSJaska Uimonen struct snd_sof_control *scontrol,
818acf1b71cSJaska Uimonen struct snd_kcontrol_new *kc,
819acf1b71cSJaska Uimonen struct snd_soc_tplg_ctl_hdr *hdr)
820acf1b71cSJaska Uimonen {
821acf1b71cSJaska Uimonen struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
822acf1b71cSJaska Uimonen struct snd_soc_tplg_mixer_control *mc =
823acf1b71cSJaska Uimonen container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
82448d2a1ceSRanjani Sridharan int tlv[SOF_TLV_ITEMS];
8259b014266SJaroslav Kysela unsigned int mask;
826b9f8e138SGuennadi Liakhovetski int ret;
827acf1b71cSJaska Uimonen
828acf1b71cSJaska Uimonen /* validate topology data */
829b5cee8feSRanjani Sridharan if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
830b5cee8feSRanjani Sridharan return -EINVAL;
831acf1b71cSJaska Uimonen
832fca18e62SJaska Uimonen /*
833fca18e62SJaska Uimonen * If control has more than 2 channels we need to override the info. This is because even if
834fca18e62SJaska Uimonen * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
835fca18e62SJaska Uimonen * pre-defined dapm control types (and related functions) creating the actual control
836fca18e62SJaska Uimonen * restrict the channels only to mono or stereo.
837fca18e62SJaska Uimonen */
838fca18e62SJaska Uimonen if (le32_to_cpu(mc->num_channels) > 2)
839fca18e62SJaska Uimonen kc->info = snd_sof_volume_info;
840fca18e62SJaska Uimonen
841acf1b71cSJaska Uimonen scontrol->comp_id = sdev->next_comp_id;
842acf1b71cSJaska Uimonen scontrol->min_volume_step = le32_to_cpu(mc->min);
843acf1b71cSJaska Uimonen scontrol->max_volume_step = le32_to_cpu(mc->max);
844acf1b71cSJaska Uimonen scontrol->num_channels = le32_to_cpu(mc->num_channels);
845acf1b71cSJaska Uimonen
846b5cee8feSRanjani Sridharan scontrol->max = le32_to_cpu(mc->max);
847b5cee8feSRanjani Sridharan if (le32_to_cpu(mc->max) == 1)
8481b4efdafSDragos Tarcatu goto skip;
849acf1b71cSJaska Uimonen
850acf1b71cSJaska Uimonen /* extract tlv data */
851941d3f0dSRanjani Sridharan if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
852ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: invalid TLV data\n");
853b5cee8feSRanjani Sridharan return -EINVAL;
854acf1b71cSJaska Uimonen }
855acf1b71cSJaska Uimonen
856acf1b71cSJaska Uimonen /* set up volume table */
857acf1b71cSJaska Uimonen ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
858acf1b71cSJaska Uimonen if (ret < 0) {
859ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: setting up volume table\n");
860b5cee8feSRanjani Sridharan return ret;
861acf1b71cSJaska Uimonen }
862acf1b71cSJaska Uimonen
8631b4efdafSDragos Tarcatu skip:
8645d43001aSJaska Uimonen /* set up possible led control from mixer private data */
8655d43001aSJaska Uimonen ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
8665d43001aSJaska Uimonen ARRAY_SIZE(led_tokens), mc->priv.array,
8675d43001aSJaska Uimonen le32_to_cpu(mc->priv.size));
8688a3ab38cSPierre-Louis Bossart if (ret != 0) {
869ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: parse led tokens failed %d\n",
8708a3ab38cSPierre-Louis Bossart le32_to_cpu(mc->priv.size));
871b5cee8feSRanjani Sridharan goto err;
8728a3ab38cSPierre-Louis Bossart }
8735d43001aSJaska Uimonen
8749b014266SJaroslav Kysela if (scontrol->led_ctl.use_led) {
8759b014266SJaroslav Kysela mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
8769b014266SJaroslav Kysela SNDRV_CTL_ELEM_ACCESS_SPK_LED;
8779b014266SJaroslav Kysela scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
8789b014266SJaroslav Kysela scontrol->access |= mask;
8799b014266SJaroslav Kysela kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
8809b014266SJaroslav Kysela kc->access |= mask;
8819b014266SJaroslav Kysela sdev->led_present = true;
8829b014266SJaroslav Kysela }
8839b014266SJaroslav Kysela
884ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
885acf1b71cSJaska Uimonen scontrol->comp_id, scontrol->num_channels);
886acf1b71cSJaska Uimonen
887b9f8e138SGuennadi Liakhovetski return 0;
8881b4efdafSDragos Tarcatu
889b5cee8feSRanjani Sridharan err:
8901b4efdafSDragos Tarcatu if (le32_to_cpu(mc->max) > 1)
8911b4efdafSDragos Tarcatu kfree(scontrol->volume_table);
892b5cee8feSRanjani Sridharan
8931b4efdafSDragos Tarcatu return ret;
894acf1b71cSJaska Uimonen }
895acf1b71cSJaska Uimonen
sof_control_load_enum(struct snd_soc_component * scomp,struct snd_sof_control * scontrol,struct snd_kcontrol_new * kc,struct snd_soc_tplg_ctl_hdr * hdr)896acf1b71cSJaska Uimonen static int sof_control_load_enum(struct snd_soc_component *scomp,
897acf1b71cSJaska Uimonen struct snd_sof_control *scontrol,
898acf1b71cSJaska Uimonen struct snd_kcontrol_new *kc,
899acf1b71cSJaska Uimonen struct snd_soc_tplg_ctl_hdr *hdr)
900acf1b71cSJaska Uimonen {
901acf1b71cSJaska Uimonen struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
902acf1b71cSJaska Uimonen struct snd_soc_tplg_enum_control *ec =
903acf1b71cSJaska Uimonen container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
904acf1b71cSJaska Uimonen
905acf1b71cSJaska Uimonen /* validate topology data */
906acf1b71cSJaska Uimonen if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
907acf1b71cSJaska Uimonen return -EINVAL;
908acf1b71cSJaska Uimonen
909acf1b71cSJaska Uimonen scontrol->comp_id = sdev->next_comp_id;
910acf1b71cSJaska Uimonen scontrol->num_channels = le32_to_cpu(ec->num_channels);
911acf1b71cSJaska Uimonen
912ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
913acf1b71cSJaska Uimonen scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
914acf1b71cSJaska Uimonen
915acf1b71cSJaska Uimonen return 0;
916acf1b71cSJaska Uimonen }
917acf1b71cSJaska Uimonen
sof_control_load_bytes(struct snd_soc_component * scomp,struct snd_sof_control * scontrol,struct snd_kcontrol_new * kc,struct snd_soc_tplg_ctl_hdr * hdr)918acf1b71cSJaska Uimonen static int sof_control_load_bytes(struct snd_soc_component *scomp,
919acf1b71cSJaska Uimonen struct snd_sof_control *scontrol,
920acf1b71cSJaska Uimonen struct snd_kcontrol_new *kc,
921acf1b71cSJaska Uimonen struct snd_soc_tplg_ctl_hdr *hdr)
922acf1b71cSJaska Uimonen {
923acf1b71cSJaska Uimonen struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
924acf1b71cSJaska Uimonen struct snd_soc_tplg_bytes_control *control =
925acf1b71cSJaska Uimonen container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
926acf1b71cSJaska Uimonen struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
9270e4ea878SGuennadi Liakhovetski size_t priv_size = le32_to_cpu(control->priv.size);
928acf1b71cSJaska Uimonen
929b5cee8feSRanjani Sridharan scontrol->max_size = sbe->max;
930acf1b71cSJaska Uimonen scontrol->comp_id = sdev->next_comp_id;
931acf1b71cSJaska Uimonen
932b5cee8feSRanjani Sridharan dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
933acf1b71cSJaska Uimonen
934b5cee8feSRanjani Sridharan /* copy the private data */
935b5cee8feSRanjani Sridharan if (priv_size > 0) {
936b26f965fSYihao Han scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
937b5cee8feSRanjani Sridharan if (!scontrol->priv)
938b5cee8feSRanjani Sridharan return -ENOMEM;
939acf1b71cSJaska Uimonen
940b5cee8feSRanjani Sridharan scontrol->priv_size = priv_size;
941acf1b71cSJaska Uimonen }
9421b4efdafSDragos Tarcatu
943b9f8e138SGuennadi Liakhovetski return 0;
944acf1b71cSJaska Uimonen }
945acf1b71cSJaska Uimonen
946311ce4feSLiam Girdwood /* external kcontrol init - used for any driver specific init */
sof_control_load(struct snd_soc_component * scomp,int index,struct snd_kcontrol_new * kc,struct snd_soc_tplg_ctl_hdr * hdr)947311ce4feSLiam Girdwood static int sof_control_load(struct snd_soc_component *scomp, int index,
948311ce4feSLiam Girdwood struct snd_kcontrol_new *kc,
949311ce4feSLiam Girdwood struct snd_soc_tplg_ctl_hdr *hdr)
950311ce4feSLiam Girdwood {
951311ce4feSLiam Girdwood struct soc_mixer_control *sm;
952311ce4feSLiam Girdwood struct soc_bytes_ext *sbe;
953311ce4feSLiam Girdwood struct soc_enum *se;
954311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
955311ce4feSLiam Girdwood struct snd_soc_dobj *dobj;
956311ce4feSLiam Girdwood struct snd_sof_control *scontrol;
957b9f8e138SGuennadi Liakhovetski int ret;
958311ce4feSLiam Girdwood
959ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
960311ce4feSLiam Girdwood hdr->type, hdr->name);
961311ce4feSLiam Girdwood
962311ce4feSLiam Girdwood scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
963311ce4feSLiam Girdwood if (!scontrol)
964311ce4feSLiam Girdwood return -ENOMEM;
965311ce4feSLiam Girdwood
966b5cee8feSRanjani Sridharan scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
9679b91d0ecSYu Liao if (!scontrol->name) {
9689b91d0ecSYu Liao kfree(scontrol);
969b5cee8feSRanjani Sridharan return -ENOMEM;
9709b91d0ecSYu Liao }
971b5cee8feSRanjani Sridharan
972ee1e79b7SRanjani Sridharan scontrol->scomp = scomp;
973199a3754SRanjani Sridharan scontrol->access = kc->access;
974b5cee8feSRanjani Sridharan scontrol->info_type = le32_to_cpu(hdr->ops.info);
975b5cee8feSRanjani Sridharan scontrol->index = kc->index;
976311ce4feSLiam Girdwood
977311ce4feSLiam Girdwood switch (le32_to_cpu(hdr->ops.info)) {
978311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW:
979311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX:
980311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
981311ce4feSLiam Girdwood sm = (struct soc_mixer_control *)kc->private_value;
982311ce4feSLiam Girdwood dobj = &sm->dobj;
983311ce4feSLiam Girdwood ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
984311ce4feSLiam Girdwood break;
985311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_BYTES:
986311ce4feSLiam Girdwood sbe = (struct soc_bytes_ext *)kc->private_value;
987311ce4feSLiam Girdwood dobj = &sbe->dobj;
988311ce4feSLiam Girdwood ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
989311ce4feSLiam Girdwood break;
990311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_ENUM:
991311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE:
992311ce4feSLiam Girdwood se = (struct soc_enum *)kc->private_value;
993311ce4feSLiam Girdwood dobj = &se->dobj;
994311ce4feSLiam Girdwood ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
995311ce4feSLiam Girdwood break;
996311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_RANGE:
997311ce4feSLiam Girdwood case SND_SOC_TPLG_CTL_STROBE:
998311ce4feSLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW:
999311ce4feSLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1000311ce4feSLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1001311ce4feSLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1002311ce4feSLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_PIN:
1003311ce4feSLiam Girdwood default:
1004ee1e79b7SRanjani Sridharan dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1005311ce4feSLiam Girdwood hdr->ops.get, hdr->ops.put, hdr->ops.info);
10065708cc2fSPeter Ujfalusi kfree(scontrol->name);
1007311ce4feSLiam Girdwood kfree(scontrol);
1008311ce4feSLiam Girdwood return 0;
1009311ce4feSLiam Girdwood }
1010311ce4feSLiam Girdwood
10111b4efdafSDragos Tarcatu if (ret < 0) {
10125708cc2fSPeter Ujfalusi kfree(scontrol->name);
10131b4efdafSDragos Tarcatu kfree(scontrol);
10141b4efdafSDragos Tarcatu return ret;
10151b4efdafSDragos Tarcatu }
10161b4efdafSDragos Tarcatu
101749c22696SKai-Heng Feng scontrol->led_ctl.led_value = -1;
101849c22696SKai-Heng Feng
1019311ce4feSLiam Girdwood dobj->private = scontrol;
1020311ce4feSLiam Girdwood list_add(&scontrol->list, &sdev->kcontrol_list);
1021b9f8e138SGuennadi Liakhovetski return 0;
1022311ce4feSLiam Girdwood }
1023311ce4feSLiam Girdwood
sof_control_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)1024311ce4feSLiam Girdwood static int sof_control_unload(struct snd_soc_component *scomp,
1025311ce4feSLiam Girdwood struct snd_soc_dobj *dobj)
1026311ce4feSLiam Girdwood {
1027311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1028dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1029311ce4feSLiam Girdwood struct snd_sof_control *scontrol = dobj->private;
1030b5cee8feSRanjani Sridharan int ret = 0;
1031311ce4feSLiam Girdwood
1032b5cee8feSRanjani Sridharan dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
1033311ce4feSLiam Girdwood
1034dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->control_free) {
1035dbdbf88bSPeter Ujfalusi ret = tplg_ops->control_free(sdev, scontrol);
1036b5cee8feSRanjani Sridharan if (ret < 0)
1037b5cee8feSRanjani Sridharan dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
1038b5cee8feSRanjani Sridharan }
1039311ce4feSLiam Girdwood
1040b5cee8feSRanjani Sridharan /* free all data before returning in case of error too */
1041b5cee8feSRanjani Sridharan kfree(scontrol->ipc_control_data);
1042b5cee8feSRanjani Sridharan kfree(scontrol->priv);
1043b5cee8feSRanjani Sridharan kfree(scontrol->name);
1044311ce4feSLiam Girdwood list_del(&scontrol->list);
1045311ce4feSLiam Girdwood kfree(scontrol);
1046b5cee8feSRanjani Sridharan
1047b5cee8feSRanjani Sridharan return ret;
1048311ce4feSLiam Girdwood }
1049311ce4feSLiam Girdwood
1050311ce4feSLiam Girdwood /*
1051311ce4feSLiam Girdwood * DAI Topology
1052311ce4feSLiam Girdwood */
1053311ce4feSLiam Girdwood
sof_connect_dai_widget(struct snd_soc_component * scomp,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * tw,struct snd_sof_dai * dai)1054311ce4feSLiam Girdwood static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1055311ce4feSLiam Girdwood struct snd_soc_dapm_widget *w,
1056311ce4feSLiam Girdwood struct snd_soc_tplg_dapm_widget *tw,
1057311ce4feSLiam Girdwood struct snd_sof_dai *dai)
1058311ce4feSLiam Girdwood {
1059311ce4feSLiam Girdwood struct snd_soc_card *card = scomp->card;
1060311ce4feSLiam Girdwood struct snd_soc_pcm_runtime *rtd;
1061c59aca98SBard Liao struct snd_soc_dai *cpu_dai;
1062323f09a6SKuninori Morimoto int stream;
1063c59aca98SBard Liao int i;
1064311ce4feSLiam Girdwood
106514bdc7b2SPeter Ujfalusi if (!w->sname) {
106614bdc7b2SPeter Ujfalusi dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
106714bdc7b2SPeter Ujfalusi return -EINVAL;
106814bdc7b2SPeter Ujfalusi }
106914bdc7b2SPeter Ujfalusi
1070323f09a6SKuninori Morimoto if (w->id == snd_soc_dapm_dai_out)
1071323f09a6SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE;
1072afd7c141SPeter Ujfalusi else if (w->id == snd_soc_dapm_dai_in)
1073323f09a6SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK;
1074323f09a6SKuninori Morimoto else
1075323f09a6SKuninori Morimoto goto end;
1076323f09a6SKuninori Morimoto
1077311ce4feSLiam Girdwood list_for_each_entry(rtd, &card->rtd_list, list) {
1078311ce4feSLiam Girdwood /* does stream match DAI link ? */
107914bdc7b2SPeter Ujfalusi if (!rtd->dai_link->stream_name ||
1080fe887887SRanjani Sridharan !strstr(rtd->dai_link->stream_name, w->sname))
1081311ce4feSLiam Girdwood continue;
1082311ce4feSLiam Girdwood
108310100165SBard Liao for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
108410100165SBard Liao /*
108510100165SBard Liao * Please create DAI widget in the right order
108610100165SBard Liao * to ensure BE will connect to the right DAI
108710100165SBard Liao * widget.
108810100165SBard Liao */
1089323f09a6SKuninori Morimoto if (!snd_soc_dai_get_widget(cpu_dai, stream)) {
1090323f09a6SKuninori Morimoto snd_soc_dai_set_widget(cpu_dai, stream, w);
109110100165SBard Liao break;
109210100165SBard Liao }
109310100165SBard Liao }
10943989ade2SKuninori Morimoto if (i == rtd->dai_link->num_cpus) {
1095323f09a6SKuninori Morimoto dev_err(scomp->dev, "error: can't find BE for DAI %s\n", w->name);
109610100165SBard Liao
109710100165SBard Liao return -EINVAL;
109810100165SBard Liao }
1099323f09a6SKuninori Morimoto
1100311ce4feSLiam Girdwood dai->name = rtd->dai_link->name;
1101ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1102311ce4feSLiam Girdwood w->name, rtd->dai_link->name);
110310100165SBard Liao }
1104323f09a6SKuninori Morimoto end:
1105311ce4feSLiam Girdwood /* check we have a connection */
1106311ce4feSLiam Girdwood if (!dai->name) {
1107ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1108311ce4feSLiam Girdwood w->name, w->sname);
1109311ce4feSLiam Girdwood return -EINVAL;
1110311ce4feSLiam Girdwood }
1111311ce4feSLiam Girdwood
1112311ce4feSLiam Girdwood return 0;
1113311ce4feSLiam Girdwood }
1114311ce4feSLiam Girdwood
sof_disconnect_dai_widget(struct snd_soc_component * scomp,struct snd_soc_dapm_widget * w)111520744617SPierre-Louis Bossart static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
111620744617SPierre-Louis Bossart struct snd_soc_dapm_widget *w)
111720744617SPierre-Louis Bossart {
111820744617SPierre-Louis Bossart struct snd_soc_card *card = scomp->card;
111920744617SPierre-Louis Bossart struct snd_soc_pcm_runtime *rtd;
112055cb3dc2SPierre-Louis Bossart const char *sname = w->sname;
112120744617SPierre-Louis Bossart struct snd_soc_dai *cpu_dai;
1122323f09a6SKuninori Morimoto int i, stream;
112320744617SPierre-Louis Bossart
112455cb3dc2SPierre-Louis Bossart if (!sname)
112520744617SPierre-Louis Bossart return;
112620744617SPierre-Louis Bossart
1127323f09a6SKuninori Morimoto if (w->id == snd_soc_dapm_dai_out)
1128323f09a6SKuninori Morimoto stream = SNDRV_PCM_STREAM_CAPTURE;
1129323f09a6SKuninori Morimoto else if (w->id == snd_soc_dapm_dai_in)
1130323f09a6SKuninori Morimoto stream = SNDRV_PCM_STREAM_PLAYBACK;
1131323f09a6SKuninori Morimoto else
1132323f09a6SKuninori Morimoto return;
1133323f09a6SKuninori Morimoto
113420744617SPierre-Louis Bossart list_for_each_entry(rtd, &card->rtd_list, list) {
113520744617SPierre-Louis Bossart /* does stream match DAI link ? */
113620744617SPierre-Louis Bossart if (!rtd->dai_link->stream_name ||
113761dfc431SBard Liao !strstr(rtd->dai_link->stream_name, sname))
113820744617SPierre-Louis Bossart continue;
113920744617SPierre-Louis Bossart
1140323f09a6SKuninori Morimoto for_each_rtd_cpu_dais(rtd, i, cpu_dai)
1141323f09a6SKuninori Morimoto if (snd_soc_dai_get_widget(cpu_dai, stream) == w) {
1142323f09a6SKuninori Morimoto snd_soc_dai_set_widget(cpu_dai, stream, NULL);
114320744617SPierre-Louis Bossart break;
114420744617SPierre-Louis Bossart }
114520744617SPierre-Louis Bossart }
114620744617SPierre-Louis Bossart }
114720744617SPierre-Louis Bossart
1148311ce4feSLiam Girdwood /* bind PCM ID to host component ID */
spcm_bind(struct snd_soc_component * scomp,struct snd_sof_pcm * spcm,int dir)1149ee1e79b7SRanjani Sridharan static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1150311ce4feSLiam Girdwood int dir)
1151311ce4feSLiam Girdwood {
115228d40e7aSPeter Ujfalusi struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1153311ce4feSLiam Girdwood struct snd_sof_widget *host_widget;
1154311ce4feSLiam Girdwood
115528d40e7aSPeter Ujfalusi if (sdev->dspless_mode_selected)
115628d40e7aSPeter Ujfalusi return 0;
115728d40e7aSPeter Ujfalusi
1158ee1e79b7SRanjani Sridharan host_widget = snd_sof_find_swidget_sname(scomp,
1159311ce4feSLiam Girdwood spcm->pcm.caps[dir].name,
1160311ce4feSLiam Girdwood dir);
1161311ce4feSLiam Girdwood if (!host_widget) {
1162ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1163311ce4feSLiam Girdwood return -EINVAL;
1164311ce4feSLiam Girdwood }
1165311ce4feSLiam Girdwood
1166311ce4feSLiam Girdwood spcm->stream[dir].comp_id = host_widget->comp_id;
1167311ce4feSLiam Girdwood
1168311ce4feSLiam Girdwood return 0;
1169311ce4feSLiam Girdwood }
1170311ce4feSLiam Girdwood
sof_get_token_value(u32 token_id,struct snd_sof_tuple * tuples,int num_tuples)11712cabd02bSRanjani Sridharan static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
11722cabd02bSRanjani Sridharan {
11732cabd02bSRanjani Sridharan int i;
11742cabd02bSRanjani Sridharan
11752cabd02bSRanjani Sridharan if (!tuples)
11762cabd02bSRanjani Sridharan return -EINVAL;
11772cabd02bSRanjani Sridharan
11782cabd02bSRanjani Sridharan for (i = 0; i < num_tuples; i++) {
11792cabd02bSRanjani Sridharan if (tuples[i].token == token_id)
11802cabd02bSRanjani Sridharan return tuples[i].value.v;
11812cabd02bSRanjani Sridharan }
11822cabd02bSRanjani Sridharan
11832cabd02bSRanjani Sridharan return -EINVAL;
11842cabd02bSRanjani Sridharan }
11852cabd02bSRanjani Sridharan
sof_widget_parse_tokens(struct snd_soc_component * scomp,struct snd_sof_widget * swidget,struct snd_soc_tplg_dapm_widget * tw,enum sof_tokens * object_token_list,int count)11867006d20eSRanjani Sridharan static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
11877006d20eSRanjani Sridharan struct snd_soc_tplg_dapm_widget *tw,
11887006d20eSRanjani Sridharan enum sof_tokens *object_token_list, int count)
1189311ce4feSLiam Girdwood {
11907006d20eSRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1191dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1192311ce4feSLiam Girdwood struct snd_soc_tplg_private *private = &tw->priv;
1193dbdbf88bSPeter Ujfalusi const struct sof_token_info *token_list;
11947006d20eSRanjani Sridharan int num_tuples = 0;
11957006d20eSRanjani Sridharan int ret, i;
1196311ce4feSLiam Girdwood
1197dbdbf88bSPeter Ujfalusi token_list = tplg_ops ? tplg_ops->token_list : NULL;
1198dbdbf88bSPeter Ujfalusi /* nothing to do if token_list is NULL */
1199dbdbf88bSPeter Ujfalusi if (!token_list)
1200dbdbf88bSPeter Ujfalusi return 0;
1201dbdbf88bSPeter Ujfalusi
12027006d20eSRanjani Sridharan if (count > 0 && !object_token_list) {
12037006d20eSRanjani Sridharan dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
12047006d20eSRanjani Sridharan return -EINVAL;
12057006d20eSRanjani Sridharan }
12067006d20eSRanjani Sridharan
12077006d20eSRanjani Sridharan /* calculate max size of tuples array */
12087006d20eSRanjani Sridharan for (i = 0; i < count; i++)
12097006d20eSRanjani Sridharan num_tuples += token_list[object_token_list[i]].count;
12107006d20eSRanjani Sridharan
12117006d20eSRanjani Sridharan /* allocate memory for tuples array */
12120a480df0SChristophe JAILLET swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
12137006d20eSRanjani Sridharan if (!swidget->tuples)
1214311ce4feSLiam Girdwood return -ENOMEM;
1215311ce4feSLiam Girdwood
12167006d20eSRanjani Sridharan /* parse token list for widget */
12177006d20eSRanjani Sridharan for (i = 0; i < count; i++) {
12182cabd02bSRanjani Sridharan int num_sets = 1;
12192cabd02bSRanjani Sridharan
12207006d20eSRanjani Sridharan if (object_token_list[i] >= SOF_TOKEN_COUNT) {
12217006d20eSRanjani Sridharan dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
12227006d20eSRanjani Sridharan object_token_list[i], swidget->widget->name);
12237006d20eSRanjani Sridharan ret = -EINVAL;
1224311ce4feSLiam Girdwood goto err;
1225311ce4feSLiam Girdwood }
1226311ce4feSLiam Girdwood
12272cabd02bSRanjani Sridharan switch (object_token_list[i]) {
12282cabd02bSRanjani Sridharan case SOF_COMP_EXT_TOKENS:
12297006d20eSRanjani Sridharan /* parse and save UUID in swidget */
12307006d20eSRanjani Sridharan ret = sof_parse_tokens(scomp, swidget,
12317006d20eSRanjani Sridharan token_list[object_token_list[i]].tokens,
12327006d20eSRanjani Sridharan token_list[object_token_list[i]].count,
12337006d20eSRanjani Sridharan private->array, le32_to_cpu(private->size));
12347006d20eSRanjani Sridharan if (ret < 0) {
12357006d20eSRanjani Sridharan dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
12367006d20eSRanjani Sridharan token_list[object_token_list[i]].name,
12377006d20eSRanjani Sridharan swidget->widget->name);
1238311ce4feSLiam Girdwood goto err;
1239311ce4feSLiam Girdwood }
1240311ce4feSLiam Girdwood
12417006d20eSRanjani Sridharan continue;
12422cabd02bSRanjani Sridharan case SOF_IN_AUDIO_FORMAT_TOKENS:
12434fdef47aSRanjani Sridharan num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS,
12442cabd02bSRanjani Sridharan swidget->tuples, swidget->num_tuples);
12452cabd02bSRanjani Sridharan if (num_sets < 0) {
12464fdef47aSRanjani Sridharan dev_err(sdev->dev, "Invalid input audio format count for %s\n",
12472cabd02bSRanjani Sridharan swidget->widget->name);
12482cabd02bSRanjani Sridharan ret = num_sets;
12492cabd02bSRanjani Sridharan goto err;
12502cabd02bSRanjani Sridharan }
12514fdef47aSRanjani Sridharan break;
12524fdef47aSRanjani Sridharan case SOF_OUT_AUDIO_FORMAT_TOKENS:
12534fdef47aSRanjani Sridharan num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS,
12544fdef47aSRanjani Sridharan swidget->tuples, swidget->num_tuples);
12554fdef47aSRanjani Sridharan if (num_sets < 0) {
12564fdef47aSRanjani Sridharan dev_err(sdev->dev, "Invalid output audio format count for %s\n",
12574fdef47aSRanjani Sridharan swidget->widget->name);
12584fdef47aSRanjani Sridharan ret = num_sets;
12594fdef47aSRanjani Sridharan goto err;
12604fdef47aSRanjani Sridharan }
12614fdef47aSRanjani Sridharan break;
12624fdef47aSRanjani Sridharan default:
12634fdef47aSRanjani Sridharan break;
12644fdef47aSRanjani Sridharan }
12652cabd02bSRanjani Sridharan
12662cabd02bSRanjani Sridharan if (num_sets > 1) {
12672cabd02bSRanjani Sridharan struct snd_sof_tuple *new_tuples;
12682cabd02bSRanjani Sridharan
12691c0d023cSRanjani Sridharan num_tuples += token_list[object_token_list[i]].count * (num_sets - 1);
12702cabd02bSRanjani Sridharan new_tuples = krealloc(swidget->tuples,
12712cabd02bSRanjani Sridharan sizeof(*new_tuples) * num_tuples, GFP_KERNEL);
12722cabd02bSRanjani Sridharan if (!new_tuples) {
12732cabd02bSRanjani Sridharan ret = -ENOMEM;
12742cabd02bSRanjani Sridharan goto err;
12752cabd02bSRanjani Sridharan }
12762cabd02bSRanjani Sridharan
12772cabd02bSRanjani Sridharan swidget->tuples = new_tuples;
12782cabd02bSRanjani Sridharan }
1279311ce4feSLiam Girdwood
12807006d20eSRanjani Sridharan /* copy one set of tuples per token ID into swidget->tuples */
12817006d20eSRanjani Sridharan ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
12822cabd02bSRanjani Sridharan object_token_list[i], num_sets, swidget->tuples,
12837006d20eSRanjani Sridharan num_tuples, &swidget->num_tuples);
12847006d20eSRanjani Sridharan if (ret < 0) {
12857006d20eSRanjani Sridharan dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
12867006d20eSRanjani Sridharan token_list[object_token_list[i]].name, swidget->widget->name, ret);
12877006d20eSRanjani Sridharan goto err;
12887006d20eSRanjani Sridharan }
12897006d20eSRanjani Sridharan }
1290311ce4feSLiam Girdwood
12911b7d57d7SRanjani Sridharan return 0;
1292311ce4feSLiam Girdwood err:
12937006d20eSRanjani Sridharan kfree(swidget->tuples);
1294311ce4feSLiam Girdwood return ret;
1295311ce4feSLiam Girdwood }
1296311ce4feSLiam Girdwood
sof_free_pin_binding(struct snd_sof_widget * swidget,bool pin_type)12973b3acedbSChao Song static void sof_free_pin_binding(struct snd_sof_widget *swidget,
12983b3acedbSChao Song bool pin_type)
12993b3acedbSChao Song {
13003b3acedbSChao Song char **pin_binding;
13013b3acedbSChao Song u32 num_pins;
13023b3acedbSChao Song int i;
13033b3acedbSChao Song
1304bb79f2a6SRanjani Sridharan if (pin_type == SOF_PIN_TYPE_INPUT) {
1305bb79f2a6SRanjani Sridharan pin_binding = swidget->input_pin_binding;
1306bb79f2a6SRanjani Sridharan num_pins = swidget->num_input_pins;
13073b3acedbSChao Song } else {
1308bb79f2a6SRanjani Sridharan pin_binding = swidget->output_pin_binding;
1309bb79f2a6SRanjani Sridharan num_pins = swidget->num_output_pins;
13103b3acedbSChao Song }
13113b3acedbSChao Song
13123b3acedbSChao Song if (pin_binding) {
13133b3acedbSChao Song for (i = 0; i < num_pins; i++)
13143b3acedbSChao Song kfree(pin_binding[i]);
13153b3acedbSChao Song }
13163b3acedbSChao Song
13173b3acedbSChao Song kfree(pin_binding);
13183b3acedbSChao Song }
13193b3acedbSChao Song
sof_parse_pin_binding(struct snd_sof_widget * swidget,struct snd_soc_tplg_private * priv,bool pin_type)13203b3acedbSChao Song static int sof_parse_pin_binding(struct snd_sof_widget *swidget,
13213b3acedbSChao Song struct snd_soc_tplg_private *priv, bool pin_type)
13223b3acedbSChao Song {
13233b3acedbSChao Song const struct sof_topology_token *pin_binding_token;
13243b3acedbSChao Song char *pin_binding[SOF_WIDGET_MAX_NUM_PINS];
13253b3acedbSChao Song int token_count;
13263b3acedbSChao Song u32 num_pins;
13273b3acedbSChao Song char **pb;
13283b3acedbSChao Song int ret;
13293b3acedbSChao Song int i;
13303b3acedbSChao Song
1331bb79f2a6SRanjani Sridharan if (pin_type == SOF_PIN_TYPE_INPUT) {
1332bb79f2a6SRanjani Sridharan num_pins = swidget->num_input_pins;
1333bb79f2a6SRanjani Sridharan pin_binding_token = comp_input_pin_binding_tokens;
1334bb79f2a6SRanjani Sridharan token_count = ARRAY_SIZE(comp_input_pin_binding_tokens);
13353b3acedbSChao Song } else {
1336bb79f2a6SRanjani Sridharan num_pins = swidget->num_output_pins;
1337bb79f2a6SRanjani Sridharan pin_binding_token = comp_output_pin_binding_tokens;
1338bb79f2a6SRanjani Sridharan token_count = ARRAY_SIZE(comp_output_pin_binding_tokens);
13393b3acedbSChao Song }
13403b3acedbSChao Song
13413b3acedbSChao Song memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *));
13423b3acedbSChao Song ret = sof_parse_token_sets(swidget->scomp, pin_binding, pin_binding_token,
13433b3acedbSChao Song token_count, priv->array, le32_to_cpu(priv->size),
13443b3acedbSChao Song num_pins, sizeof(char *));
13453b3acedbSChao Song if (ret < 0)
13463b3acedbSChao Song goto err;
13473b3acedbSChao Song
13483b3acedbSChao Song /* copy pin binding array to swidget only if it is defined in topology */
13493b3acedbSChao Song if (pin_binding[0]) {
13503b3acedbSChao Song pb = kmemdup(pin_binding, num_pins * sizeof(char *), GFP_KERNEL);
13513b3acedbSChao Song if (!pb) {
13523b3acedbSChao Song ret = -ENOMEM;
13533b3acedbSChao Song goto err;
13543b3acedbSChao Song }
1355bb79f2a6SRanjani Sridharan if (pin_type == SOF_PIN_TYPE_INPUT)
1356bb79f2a6SRanjani Sridharan swidget->input_pin_binding = pb;
13573b3acedbSChao Song else
1358bb79f2a6SRanjani Sridharan swidget->output_pin_binding = pb;
13593b3acedbSChao Song }
13603b3acedbSChao Song
13613b3acedbSChao Song return 0;
13623b3acedbSChao Song
13633b3acedbSChao Song err:
13643b3acedbSChao Song for (i = 0; i < num_pins; i++)
13653b3acedbSChao Song kfree(pin_binding[i]);
13663b3acedbSChao Song
13673b3acedbSChao Song return ret;
13683b3acedbSChao Song }
13693b3acedbSChao Song
get_w_no_wname_in_long_name(void * elem,void * object,u32 offset)137056ce7b79SJyri Sarha static int get_w_no_wname_in_long_name(void *elem, void *object, u32 offset)
137156ce7b79SJyri Sarha {
137256ce7b79SJyri Sarha struct snd_soc_tplg_vendor_value_elem *velem = elem;
137356ce7b79SJyri Sarha struct snd_soc_dapm_widget *w = object;
137456ce7b79SJyri Sarha
137556ce7b79SJyri Sarha w->no_wname_in_kcontrol_name = !!le32_to_cpu(velem->value);
137656ce7b79SJyri Sarha return 0;
137756ce7b79SJyri Sarha }
137856ce7b79SJyri Sarha
137956ce7b79SJyri Sarha static const struct sof_topology_token dapm_widget_tokens[] = {
138056ce7b79SJyri Sarha {SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
138156ce7b79SJyri Sarha get_w_no_wname_in_long_name, 0}
138256ce7b79SJyri Sarha };
138356ce7b79SJyri Sarha
1384311ce4feSLiam Girdwood /* external widget init - used for any driver specific init */
sof_widget_ready(struct snd_soc_component * scomp,int index,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * tw)1385311ce4feSLiam Girdwood static int sof_widget_ready(struct snd_soc_component *scomp, int index,
1386311ce4feSLiam Girdwood struct snd_soc_dapm_widget *w,
1387311ce4feSLiam Girdwood struct snd_soc_tplg_dapm_widget *tw)
1388311ce4feSLiam Girdwood {
1389311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1390dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1391dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_widget_ops *widget_ops;
13926327c729SChao Song struct snd_soc_tplg_private *priv = &tw->priv;
1393dbdbf88bSPeter Ujfalusi enum sof_tokens *token_list = NULL;
1394311ce4feSLiam Girdwood struct snd_sof_widget *swidget;
1395311ce4feSLiam Girdwood struct snd_sof_dai *dai;
1396dbdbf88bSPeter Ujfalusi int token_list_size = 0;
1397311ce4feSLiam Girdwood int ret = 0;
1398311ce4feSLiam Girdwood
1399311ce4feSLiam Girdwood swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
1400311ce4feSLiam Girdwood if (!swidget)
1401311ce4feSLiam Girdwood return -ENOMEM;
1402311ce4feSLiam Girdwood
1403ee1e79b7SRanjani Sridharan swidget->scomp = scomp;
1404311ce4feSLiam Girdwood swidget->widget = w;
1405311ce4feSLiam Girdwood swidget->comp_id = sdev->next_comp_id++;
1406311ce4feSLiam Girdwood swidget->id = w->id;
1407311ce4feSLiam Girdwood swidget->pipeline_id = index;
1408311ce4feSLiam Girdwood swidget->private = NULL;
1409f94f3915SPeter Ujfalusi mutex_init(&swidget->setup_mutex);
1410f94f3915SPeter Ujfalusi
1411bb79f2a6SRanjani Sridharan ida_init(&swidget->output_queue_ida);
1412bb79f2a6SRanjani Sridharan ida_init(&swidget->input_queue_ida);
1413311ce4feSLiam Girdwood
141456ce7b79SJyri Sarha ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, ARRAY_SIZE(dapm_widget_tokens),
141556ce7b79SJyri Sarha priv->array, le32_to_cpu(priv->size));
141656ce7b79SJyri Sarha if (ret < 0) {
141756ce7b79SJyri Sarha dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n",
141856ce7b79SJyri Sarha w->name);
141956ce7b79SJyri Sarha goto widget_free;
142056ce7b79SJyri Sarha }
142156ce7b79SJyri Sarha
14226327c729SChao Song ret = sof_parse_tokens(scomp, swidget, comp_pin_tokens,
14236327c729SChao Song ARRAY_SIZE(comp_pin_tokens), priv->array,
14246327c729SChao Song le32_to_cpu(priv->size));
14256327c729SChao Song if (ret < 0) {
14266327c729SChao Song dev_err(scomp->dev, "failed to parse component pin tokens for %s\n",
14276327c729SChao Song w->name);
14286ba8ddf8SRanjani Sridharan goto widget_free;
14296327c729SChao Song }
14306327c729SChao Song
1431bb79f2a6SRanjani Sridharan if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS ||
1432bb79f2a6SRanjani Sridharan swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) {
1433bb79f2a6SRanjani Sridharan dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n",
1434bb79f2a6SRanjani Sridharan swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins);
14356ba8ddf8SRanjani Sridharan ret = -EINVAL;
14366ba8ddf8SRanjani Sridharan goto widget_free;
14376327c729SChao Song }
14386327c729SChao Song
1439bb79f2a6SRanjani Sridharan if (swidget->num_input_pins > 1) {
1440bb79f2a6SRanjani Sridharan ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT);
14413b3acedbSChao Song /* on parsing error, pin binding is not allocated, nothing to free. */
14423b3acedbSChao Song if (ret < 0) {
1443bb79f2a6SRanjani Sridharan dev_err(scomp->dev, "failed to parse input pin binding for %s\n",
14443b3acedbSChao Song w->name);
14456ba8ddf8SRanjani Sridharan goto widget_free;
14463b3acedbSChao Song }
14473b3acedbSChao Song }
14483b3acedbSChao Song
1449bb79f2a6SRanjani Sridharan if (swidget->num_output_pins > 1) {
1450bb79f2a6SRanjani Sridharan ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT);
14513b3acedbSChao Song /* on parsing error, pin binding is not allocated, nothing to free. */
14523b3acedbSChao Song if (ret < 0) {
1453bb79f2a6SRanjani Sridharan dev_err(scomp->dev, "failed to parse output pin binding for %s\n",
14543b3acedbSChao Song w->name);
14556ba8ddf8SRanjani Sridharan goto widget_free;
14563b3acedbSChao Song }
14573b3acedbSChao Song }
14583b3acedbSChao Song
14596327c729SChao Song dev_dbg(scomp->dev,
14606327c729SChao Song "tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n",
14616327c729SChao Song swidget->comp_id, w->name, swidget->id, index,
1462bb79f2a6SRanjani Sridharan swidget->num_input_pins, swidget->num_output_pins,
14636327c729SChao Song strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none");
1464311ce4feSLiam Girdwood
1465dbdbf88bSPeter Ujfalusi widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1466dbdbf88bSPeter Ujfalusi if (widget_ops) {
14677006d20eSRanjani Sridharan token_list = widget_ops[w->id].token_list;
14687006d20eSRanjani Sridharan token_list_size = widget_ops[w->id].token_list_size;
1469dbdbf88bSPeter Ujfalusi }
14707006d20eSRanjani Sridharan
1471311ce4feSLiam Girdwood /* handle any special case widgets */
1472311ce4feSLiam Girdwood switch (w->id) {
1473311ce4feSLiam Girdwood case snd_soc_dapm_dai_in:
1474311ce4feSLiam Girdwood case snd_soc_dapm_dai_out:
1475311ce4feSLiam Girdwood dai = kzalloc(sizeof(*dai), GFP_KERNEL);
1476311ce4feSLiam Girdwood if (!dai) {
14776ba8ddf8SRanjani Sridharan ret = -ENOMEM;
14786ba8ddf8SRanjani Sridharan goto widget_free;
1479311ce4feSLiam Girdwood }
1480311ce4feSLiam Girdwood
1481909dadf2SRanjani Sridharan ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
14824a230769SRanjani Sridharan if (!ret)
14834a230769SRanjani Sridharan ret = sof_connect_dai_widget(scomp, w, tw, dai);
14844a230769SRanjani Sridharan if (ret < 0) {
14854a230769SRanjani Sridharan kfree(dai);
14864a230769SRanjani Sridharan break;
14874a230769SRanjani Sridharan }
1488311ce4feSLiam Girdwood list_add(&dai->list, &sdev->dai_list);
1489311ce4feSLiam Girdwood swidget->private = dai;
1490311ce4feSLiam Girdwood break;
1491f2cf24a1SRanjani Sridharan case snd_soc_dapm_effect:
1492f2cf24a1SRanjani Sridharan /* check we have some tokens - we need at least process type */
1493f2cf24a1SRanjani Sridharan if (le32_to_cpu(tw->priv.size) == 0) {
1494f2cf24a1SRanjani Sridharan dev_err(scomp->dev, "error: process tokens not found\n");
1495f2cf24a1SRanjani Sridharan ret = -EINVAL;
1496f2cf24a1SRanjani Sridharan break;
1497f2cf24a1SRanjani Sridharan }
1498f2cf24a1SRanjani Sridharan ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1499f2cf24a1SRanjani Sridharan break;
1500311ce4feSLiam Girdwood case snd_soc_dapm_pga:
15018a2e4a73SRanjani Sridharan if (!le32_to_cpu(tw->num_kcontrols)) {
15028a2e4a73SRanjani Sridharan dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
15038a2e4a73SRanjani Sridharan tw->num_kcontrols);
15048a2e4a73SRanjani Sridharan ret = -EINVAL;
1505311ce4feSLiam Girdwood break;
15068a2e4a73SRanjani Sridharan }
15078a2e4a73SRanjani Sridharan
15088a2e4a73SRanjani Sridharan fallthrough;
150930f41680SRanjani Sridharan case snd_soc_dapm_mixer:
1510311ce4feSLiam Girdwood case snd_soc_dapm_buffer:
1511311ce4feSLiam Girdwood case snd_soc_dapm_scheduler:
1512311ce4feSLiam Girdwood case snd_soc_dapm_aif_out:
1513311ce4feSLiam Girdwood case snd_soc_dapm_aif_in:
15148d8b1293SRanjani Sridharan case snd_soc_dapm_src:
1515cb7ed49aSRanjani Sridharan case snd_soc_dapm_asrc:
1516111d66f6SRanjani Sridharan case snd_soc_dapm_siggen:
1517683b54efSRanjani Sridharan case snd_soc_dapm_mux:
1518683b54efSRanjani Sridharan case snd_soc_dapm_demux:
15197006d20eSRanjani Sridharan ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1520311ce4feSLiam Girdwood break;
1521311ce4feSLiam Girdwood case snd_soc_dapm_switch:
1522311ce4feSLiam Girdwood case snd_soc_dapm_dai_link:
1523311ce4feSLiam Girdwood case snd_soc_dapm_kcontrol:
1524311ce4feSLiam Girdwood default:
1525f46ff506SRanjani Sridharan dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
1526311ce4feSLiam Girdwood break;
1527311ce4feSLiam Girdwood }
1528311ce4feSLiam Girdwood
1529909dadf2SRanjani Sridharan /* check token parsing reply */
15301b7d57d7SRanjani Sridharan if (ret < 0) {
1531ee1e79b7SRanjani Sridharan dev_err(scomp->dev,
15321b7d57d7SRanjani Sridharan "error: failed to add widget id %d type %d name : %s stream %s\n",
1533311ce4feSLiam Girdwood tw->shift, swidget->id, tw->name,
1534311ce4feSLiam Girdwood strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
15351b7d57d7SRanjani Sridharan ? tw->sname : "none");
15366ba8ddf8SRanjani Sridharan goto widget_free;
1537311ce4feSLiam Girdwood }
1538311ce4feSLiam Girdwood
15393d59eaefSPeter Ujfalusi if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
15403d59eaefSPeter Ujfalusi swidget->core = SOF_DSP_PRIMARY_CORE;
15413d59eaefSPeter Ujfalusi } else {
15423d59eaefSPeter Ujfalusi int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
15433d59eaefSPeter Ujfalusi swidget->num_tuples);
15443d59eaefSPeter Ujfalusi
15453d59eaefSPeter Ujfalusi if (core >= 0)
15463d59eaefSPeter Ujfalusi swidget->core = core;
15473d59eaefSPeter Ujfalusi }
15483d59eaefSPeter Ujfalusi
1549311ce4feSLiam Girdwood /* bind widget to external event */
1550311ce4feSLiam Girdwood if (tw->event_type) {
1551dbdbf88bSPeter Ujfalusi if (widget_ops && widget_ops[w->id].bind_event) {
15528ef1439cSRanjani Sridharan ret = widget_ops[w->id].bind_event(scomp, swidget,
1553311ce4feSLiam Girdwood le16_to_cpu(tw->event_type));
1554311ce4feSLiam Girdwood if (ret) {
15558ef1439cSRanjani Sridharan dev_err(scomp->dev, "widget event binding failed for %s\n",
15568ef1439cSRanjani Sridharan swidget->widget->name);
15576ba8ddf8SRanjani Sridharan goto free;
1558311ce4feSLiam Girdwood }
1559311ce4feSLiam Girdwood }
15608ef1439cSRanjani Sridharan }
1561311ce4feSLiam Girdwood
15629c04363dSRanjani Sridharan /* create and add pipeline for scheduler type widgets */
15639c04363dSRanjani Sridharan if (w->id == snd_soc_dapm_scheduler) {
15649c04363dSRanjani Sridharan struct snd_sof_pipeline *spipe;
15659c04363dSRanjani Sridharan
15669c04363dSRanjani Sridharan spipe = kzalloc(sizeof(*spipe), GFP_KERNEL);
15679c04363dSRanjani Sridharan if (!spipe) {
15686ba8ddf8SRanjani Sridharan ret = -ENOMEM;
15696ba8ddf8SRanjani Sridharan goto free;
15709c04363dSRanjani Sridharan }
15719c04363dSRanjani Sridharan
15729c04363dSRanjani Sridharan spipe->pipe_widget = swidget;
15739c04363dSRanjani Sridharan swidget->spipe = spipe;
15749c04363dSRanjani Sridharan list_add(&spipe->list, &sdev->pipeline_list);
15759c04363dSRanjani Sridharan }
15769c04363dSRanjani Sridharan
1577311ce4feSLiam Girdwood w->dobj.private = swidget;
1578311ce4feSLiam Girdwood list_add(&swidget->list, &sdev->widget_list);
1579311ce4feSLiam Girdwood return ret;
15806ba8ddf8SRanjani Sridharan free:
15816ba8ddf8SRanjani Sridharan kfree(swidget->private);
15826ba8ddf8SRanjani Sridharan kfree(swidget->tuples);
15836ba8ddf8SRanjani Sridharan widget_free:
15846ba8ddf8SRanjani Sridharan kfree(swidget);
15856ba8ddf8SRanjani Sridharan return ret;
1586311ce4feSLiam Girdwood }
1587311ce4feSLiam Girdwood
sof_route_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)1588311ce4feSLiam Girdwood static int sof_route_unload(struct snd_soc_component *scomp,
1589311ce4feSLiam Girdwood struct snd_soc_dobj *dobj)
1590311ce4feSLiam Girdwood {
1591311ce4feSLiam Girdwood struct snd_sof_route *sroute;
1592311ce4feSLiam Girdwood
1593311ce4feSLiam Girdwood sroute = dobj->private;
1594311ce4feSLiam Girdwood if (!sroute)
1595311ce4feSLiam Girdwood return 0;
1596311ce4feSLiam Girdwood
1597311ce4feSLiam Girdwood /* free sroute and its private data */
1598311ce4feSLiam Girdwood kfree(sroute->private);
1599311ce4feSLiam Girdwood list_del(&sroute->list);
1600311ce4feSLiam Girdwood kfree(sroute);
1601311ce4feSLiam Girdwood
1602311ce4feSLiam Girdwood return 0;
1603311ce4feSLiam Girdwood }
1604311ce4feSLiam Girdwood
sof_widget_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)1605311ce4feSLiam Girdwood static int sof_widget_unload(struct snd_soc_component *scomp,
1606311ce4feSLiam Girdwood struct snd_soc_dobj *dobj)
1607311ce4feSLiam Girdwood {
16087006d20eSRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1609dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1610dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_widget_ops *widget_ops;
1611311ce4feSLiam Girdwood const struct snd_kcontrol_new *kc;
1612311ce4feSLiam Girdwood struct snd_soc_dapm_widget *widget;
1613311ce4feSLiam Girdwood struct snd_sof_control *scontrol;
1614311ce4feSLiam Girdwood struct snd_sof_widget *swidget;
1615311ce4feSLiam Girdwood struct soc_mixer_control *sm;
1616311ce4feSLiam Girdwood struct soc_bytes_ext *sbe;
1617311ce4feSLiam Girdwood struct snd_sof_dai *dai;
1618311ce4feSLiam Girdwood struct soc_enum *se;
1619311ce4feSLiam Girdwood int i;
1620311ce4feSLiam Girdwood
1621311ce4feSLiam Girdwood swidget = dobj->private;
1622311ce4feSLiam Girdwood if (!swidget)
1623311ce4feSLiam Girdwood return 0;
1624311ce4feSLiam Girdwood
1625311ce4feSLiam Girdwood widget = swidget->widget;
1626311ce4feSLiam Girdwood
1627311ce4feSLiam Girdwood switch (swidget->id) {
1628311ce4feSLiam Girdwood case snd_soc_dapm_dai_in:
1629311ce4feSLiam Girdwood case snd_soc_dapm_dai_out:
1630311ce4feSLiam Girdwood dai = swidget->private;
1631311ce4feSLiam Girdwood
1632909dadf2SRanjani Sridharan if (dai)
1633311ce4feSLiam Girdwood list_del(&dai->list);
163420744617SPierre-Louis Bossart
163520744617SPierre-Louis Bossart sof_disconnect_dai_widget(scomp, widget);
163620744617SPierre-Louis Bossart
1637311ce4feSLiam Girdwood break;
16389c04363dSRanjani Sridharan case snd_soc_dapm_scheduler:
16399c04363dSRanjani Sridharan {
16409c04363dSRanjani Sridharan struct snd_sof_pipeline *spipe = swidget->spipe;
16419c04363dSRanjani Sridharan
16429c04363dSRanjani Sridharan list_del(&spipe->list);
16439c04363dSRanjani Sridharan kfree(spipe);
16449c04363dSRanjani Sridharan swidget->spipe = NULL;
16459c04363dSRanjani Sridharan break;
16469c04363dSRanjani Sridharan }
1647311ce4feSLiam Girdwood default:
1648311ce4feSLiam Girdwood break;
1649311ce4feSLiam Girdwood }
1650311ce4feSLiam Girdwood for (i = 0; i < widget->num_kcontrols; i++) {
1651311ce4feSLiam Girdwood kc = &widget->kcontrol_news[i];
1652d29d41e2SJaska Uimonen switch (widget->dobj.widget.kcontrol_type[i]) {
1653311ce4feSLiam Girdwood case SND_SOC_TPLG_TYPE_MIXER:
1654311ce4feSLiam Girdwood sm = (struct soc_mixer_control *)kc->private_value;
1655311ce4feSLiam Girdwood scontrol = sm->dobj.private;
1656311ce4feSLiam Girdwood if (sm->max > 1)
1657311ce4feSLiam Girdwood kfree(scontrol->volume_table);
1658311ce4feSLiam Girdwood break;
1659311ce4feSLiam Girdwood case SND_SOC_TPLG_TYPE_ENUM:
1660311ce4feSLiam Girdwood se = (struct soc_enum *)kc->private_value;
1661311ce4feSLiam Girdwood scontrol = se->dobj.private;
1662311ce4feSLiam Girdwood break;
1663311ce4feSLiam Girdwood case SND_SOC_TPLG_TYPE_BYTES:
1664311ce4feSLiam Girdwood sbe = (struct soc_bytes_ext *)kc->private_value;
1665311ce4feSLiam Girdwood scontrol = sbe->dobj.private;
1666311ce4feSLiam Girdwood break;
1667311ce4feSLiam Girdwood default:
1668ee1e79b7SRanjani Sridharan dev_warn(scomp->dev, "unsupported kcontrol_type\n");
1669311ce4feSLiam Girdwood goto out;
1670311ce4feSLiam Girdwood }
1671b5cee8feSRanjani Sridharan kfree(scontrol->ipc_control_data);
1672311ce4feSLiam Girdwood list_del(&scontrol->list);
16735708cc2fSPeter Ujfalusi kfree(scontrol->name);
1674311ce4feSLiam Girdwood kfree(scontrol);
1675311ce4feSLiam Girdwood }
1676311ce4feSLiam Girdwood
1677311ce4feSLiam Girdwood out:
16787006d20eSRanjani Sridharan /* free IPC related data */
1679dbdbf88bSPeter Ujfalusi widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1680dbdbf88bSPeter Ujfalusi if (widget_ops && widget_ops[swidget->id].ipc_free)
16817006d20eSRanjani Sridharan widget_ops[swidget->id].ipc_free(swidget);
16827006d20eSRanjani Sridharan
1683bb79f2a6SRanjani Sridharan ida_destroy(&swidget->output_queue_ida);
1684bb79f2a6SRanjani Sridharan ida_destroy(&swidget->input_queue_ida);
1685c84443dbSChao Song
1686bb79f2a6SRanjani Sridharan sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT);
1687bb79f2a6SRanjani Sridharan sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT);
16883b3acedbSChao Song
16897006d20eSRanjani Sridharan kfree(swidget->tuples);
16907006d20eSRanjani Sridharan
1691311ce4feSLiam Girdwood /* remove and free swidget object */
1692311ce4feSLiam Girdwood list_del(&swidget->list);
1693311ce4feSLiam Girdwood kfree(swidget);
1694311ce4feSLiam Girdwood
169598418a08SRanjani Sridharan return 0;
1696311ce4feSLiam Girdwood }
1697311ce4feSLiam Girdwood
1698311ce4feSLiam Girdwood /*
1699311ce4feSLiam Girdwood * DAI HW configuration.
1700311ce4feSLiam Girdwood */
1701311ce4feSLiam Girdwood
1702311ce4feSLiam Girdwood /* FE DAI - used for any driver specific init */
sof_dai_load(struct snd_soc_component * scomp,int index,struct snd_soc_dai_driver * dai_drv,struct snd_soc_tplg_pcm * pcm,struct snd_soc_dai * dai)1703311ce4feSLiam Girdwood static int sof_dai_load(struct snd_soc_component *scomp, int index,
1704311ce4feSLiam Girdwood struct snd_soc_dai_driver *dai_drv,
1705311ce4feSLiam Girdwood struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
1706311ce4feSLiam Girdwood {
1707311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
17087201a3d4SRanjani Sridharan const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1709311ce4feSLiam Girdwood struct snd_soc_tplg_stream_caps *caps;
1710c5232c01SKeyon Jie struct snd_soc_tplg_private *private = &pcm->priv;
1711311ce4feSLiam Girdwood struct snd_sof_pcm *spcm;
1712c688cf1dSGuennadi Liakhovetski int stream;
1713b9f8e138SGuennadi Liakhovetski int ret;
1714311ce4feSLiam Girdwood
1715311ce4feSLiam Girdwood /* nothing to do for BEs atm */
1716311ce4feSLiam Girdwood if (!pcm)
1717311ce4feSLiam Girdwood return 0;
1718311ce4feSLiam Girdwood
1719311ce4feSLiam Girdwood spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
1720311ce4feSLiam Girdwood if (!spcm)
1721311ce4feSLiam Girdwood return -ENOMEM;
1722311ce4feSLiam Girdwood
1723ee1e79b7SRanjani Sridharan spcm->scomp = scomp;
1724c688cf1dSGuennadi Liakhovetski
17259ef91cadSGuennadi Liakhovetski for_each_pcm_streams(stream) {
1726c688cf1dSGuennadi Liakhovetski spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
1727858f7a5cSDaniel Baluta if (pcm->compress)
1728858f7a5cSDaniel Baluta snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1729858f7a5cSDaniel Baluta else
1730858f7a5cSDaniel Baluta snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
17319ef91cadSGuennadi Liakhovetski }
1732311ce4feSLiam Girdwood
1733311ce4feSLiam Girdwood spcm->pcm = *pcm;
1734ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
1735494e8f65SPierre-Louis Bossart
17367201a3d4SRanjani Sridharan /* perform pcm set op */
17377201a3d4SRanjani Sridharan if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
17387201a3d4SRanjani Sridharan ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
1739d7368ad5SKamil Duljas if (ret < 0) {
1740d7368ad5SKamil Duljas kfree(spcm);
17417201a3d4SRanjani Sridharan return ret;
17427201a3d4SRanjani Sridharan }
1743d7368ad5SKamil Duljas }
17447201a3d4SRanjani Sridharan
1745311ce4feSLiam Girdwood dai_drv->dobj.private = spcm;
1746311ce4feSLiam Girdwood list_add(&spcm->list, &sdev->pcm_list);
1747311ce4feSLiam Girdwood
1748c5232c01SKeyon Jie ret = sof_parse_tokens(scomp, spcm, stream_tokens,
1749c5232c01SKeyon Jie ARRAY_SIZE(stream_tokens), private->array,
1750c5232c01SKeyon Jie le32_to_cpu(private->size));
1751c5232c01SKeyon Jie if (ret) {
1752ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
1753c5232c01SKeyon Jie le32_to_cpu(private->size));
1754c5232c01SKeyon Jie return ret;
1755c5232c01SKeyon Jie }
1756c5232c01SKeyon Jie
1757311ce4feSLiam Girdwood /* do we need to allocate playback PCM DMA pages */
1758311ce4feSLiam Girdwood if (!spcm->pcm.playback)
1759311ce4feSLiam Girdwood goto capture;
1760311ce4feSLiam Girdwood
1761c688cf1dSGuennadi Liakhovetski stream = SNDRV_PCM_STREAM_PLAYBACK;
1762c688cf1dSGuennadi Liakhovetski
1763311ce4feSLiam Girdwood caps = &spcm->pcm.caps[stream];
1764311ce4feSLiam Girdwood
1765311ce4feSLiam Girdwood /* allocate playback page table buffer */
1766311ce4feSLiam Girdwood ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1767311ce4feSLiam Girdwood PAGE_SIZE, &spcm->stream[stream].page_table);
1768311ce4feSLiam Girdwood if (ret < 0) {
1769ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1770311ce4feSLiam Girdwood caps->name, ret);
1771311ce4feSLiam Girdwood
1772311ce4feSLiam Girdwood return ret;
1773311ce4feSLiam Girdwood }
1774311ce4feSLiam Girdwood
1775311ce4feSLiam Girdwood /* bind pcm to host comp */
1776ee1e79b7SRanjani Sridharan ret = spcm_bind(scomp, spcm, stream);
1777311ce4feSLiam Girdwood if (ret) {
1778ee1e79b7SRanjani Sridharan dev_err(scomp->dev,
1779311ce4feSLiam Girdwood "error: can't bind pcm to host\n");
1780311ce4feSLiam Girdwood goto free_playback_tables;
1781311ce4feSLiam Girdwood }
1782311ce4feSLiam Girdwood
1783311ce4feSLiam Girdwood capture:
1784311ce4feSLiam Girdwood stream = SNDRV_PCM_STREAM_CAPTURE;
1785311ce4feSLiam Girdwood
1786311ce4feSLiam Girdwood /* do we need to allocate capture PCM DMA pages */
1787311ce4feSLiam Girdwood if (!spcm->pcm.capture)
1788311ce4feSLiam Girdwood return ret;
1789311ce4feSLiam Girdwood
1790311ce4feSLiam Girdwood caps = &spcm->pcm.caps[stream];
1791311ce4feSLiam Girdwood
1792311ce4feSLiam Girdwood /* allocate capture page table buffer */
1793311ce4feSLiam Girdwood ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1794311ce4feSLiam Girdwood PAGE_SIZE, &spcm->stream[stream].page_table);
1795311ce4feSLiam Girdwood if (ret < 0) {
1796ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1797311ce4feSLiam Girdwood caps->name, ret);
1798311ce4feSLiam Girdwood goto free_playback_tables;
1799311ce4feSLiam Girdwood }
1800311ce4feSLiam Girdwood
1801311ce4feSLiam Girdwood /* bind pcm to host comp */
1802ee1e79b7SRanjani Sridharan ret = spcm_bind(scomp, spcm, stream);
1803311ce4feSLiam Girdwood if (ret) {
1804ee1e79b7SRanjani Sridharan dev_err(scomp->dev,
1805311ce4feSLiam Girdwood "error: can't bind pcm to host\n");
1806311ce4feSLiam Girdwood snd_dma_free_pages(&spcm->stream[stream].page_table);
1807311ce4feSLiam Girdwood goto free_playback_tables;
1808311ce4feSLiam Girdwood }
1809311ce4feSLiam Girdwood
1810311ce4feSLiam Girdwood return ret;
1811311ce4feSLiam Girdwood
1812311ce4feSLiam Girdwood free_playback_tables:
1813311ce4feSLiam Girdwood if (spcm->pcm.playback)
1814311ce4feSLiam Girdwood snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1815311ce4feSLiam Girdwood
1816311ce4feSLiam Girdwood return ret;
1817311ce4feSLiam Girdwood }
1818311ce4feSLiam Girdwood
sof_dai_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)1819311ce4feSLiam Girdwood static int sof_dai_unload(struct snd_soc_component *scomp,
1820311ce4feSLiam Girdwood struct snd_soc_dobj *dobj)
1821311ce4feSLiam Girdwood {
18227201a3d4SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
18237201a3d4SRanjani Sridharan const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1824311ce4feSLiam Girdwood struct snd_sof_pcm *spcm = dobj->private;
1825311ce4feSLiam Girdwood
1826311ce4feSLiam Girdwood /* free PCM DMA pages */
1827311ce4feSLiam Girdwood if (spcm->pcm.playback)
1828311ce4feSLiam Girdwood snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1829311ce4feSLiam Girdwood
1830311ce4feSLiam Girdwood if (spcm->pcm.capture)
1831311ce4feSLiam Girdwood snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
1832311ce4feSLiam Girdwood
18337201a3d4SRanjani Sridharan /* perform pcm free op */
18347201a3d4SRanjani Sridharan if (ipc_pcm_ops && ipc_pcm_ops->pcm_free)
18357201a3d4SRanjani Sridharan ipc_pcm_ops->pcm_free(sdev, spcm);
18367201a3d4SRanjani Sridharan
1837311ce4feSLiam Girdwood /* remove from list and free spcm */
1838311ce4feSLiam Girdwood list_del(&spcm->list);
1839311ce4feSLiam Girdwood kfree(spcm);
1840311ce4feSLiam Girdwood
1841311ce4feSLiam Girdwood return 0;
1842311ce4feSLiam Girdwood }
1843311ce4feSLiam Girdwood
1844909dadf2SRanjani Sridharan static const struct sof_topology_token common_dai_link_tokens[] = {
1845909dadf2SRanjani Sridharan {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
1846909dadf2SRanjani Sridharan offsetof(struct snd_sof_dai_link, type)},
1847909dadf2SRanjani Sridharan };
18484d6bbf1aSPierre-Louis Bossart
1849311ce4feSLiam Girdwood /* DAI link - used for any driver specific init */
sof_link_load(struct snd_soc_component * scomp,int index,struct snd_soc_dai_link * link,struct snd_soc_tplg_link_config * cfg)1850909dadf2SRanjani Sridharan static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
1851311ce4feSLiam Girdwood struct snd_soc_tplg_link_config *cfg)
1852311ce4feSLiam Girdwood {
1853909dadf2SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1854dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1855311ce4feSLiam Girdwood struct snd_soc_tplg_private *private = &cfg->priv;
1856dbdbf88bSPeter Ujfalusi const struct sof_token_info *token_list;
1857909dadf2SRanjani Sridharan struct snd_sof_dai_link *slink;
1858909dadf2SRanjani Sridharan u32 token_id = 0;
1859909dadf2SRanjani Sridharan int num_tuples = 0;
1860909dadf2SRanjani Sridharan int ret, num_sets;
1861311ce4feSLiam Girdwood
18627ba06110SKuninori Morimoto if (!link->platforms) {
1863ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: no platforms\n");
18647ba06110SKuninori Morimoto return -EINVAL;
18657ba06110SKuninori Morimoto }
1866ee1e79b7SRanjani Sridharan link->platforms->name = dev_name(scomp->dev);
1867311ce4feSLiam Girdwood
1868e380c907SRanjani Sridharan if (tplg_ops && tplg_ops->link_setup) {
1869e380c907SRanjani Sridharan ret = tplg_ops->link_setup(sdev, link);
1870e380c907SRanjani Sridharan if (ret < 0)
1871e380c907SRanjani Sridharan return ret;
1872e380c907SRanjani Sridharan }
1873e380c907SRanjani Sridharan
1874e380c907SRanjani Sridharan /* Set nonatomic property for FE dai links as their trigger action involves IPC's */
1875311ce4feSLiam Girdwood if (!link->no_pcm) {
1876311ce4feSLiam Girdwood link->nonatomic = true;
1877311ce4feSLiam Girdwood return 0;
1878311ce4feSLiam Girdwood }
1879311ce4feSLiam Girdwood
1880311ce4feSLiam Girdwood /* check we have some tokens - we need at least DAI type */
1881311ce4feSLiam Girdwood if (le32_to_cpu(private->size) == 0) {
1882ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
1883311ce4feSLiam Girdwood return -EINVAL;
1884311ce4feSLiam Girdwood }
1885311ce4feSLiam Girdwood
1886909dadf2SRanjani Sridharan slink = kzalloc(sizeof(*slink), GFP_KERNEL);
1887909dadf2SRanjani Sridharan if (!slink)
1888c1c03888SJaska Uimonen return -ENOMEM;
1889311ce4feSLiam Girdwood
1890909dadf2SRanjani Sridharan slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
1891909dadf2SRanjani Sridharan slink->hw_configs = kmemdup(cfg->hw_config,
1892909dadf2SRanjani Sridharan sizeof(*slink->hw_configs) * slink->num_hw_configs,
1893909dadf2SRanjani Sridharan GFP_KERNEL);
1894909dadf2SRanjani Sridharan if (!slink->hw_configs) {
1895909dadf2SRanjani Sridharan kfree(slink);
1896909dadf2SRanjani Sridharan return -ENOMEM;
1897c1c03888SJaska Uimonen }
1898311ce4feSLiam Girdwood
1899909dadf2SRanjani Sridharan slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
1900909dadf2SRanjani Sridharan slink->link = link;
1901909dadf2SRanjani Sridharan
1902909dadf2SRanjani Sridharan dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
1903909dadf2SRanjani Sridharan slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
1904909dadf2SRanjani Sridharan
1905909dadf2SRanjani Sridharan ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
1906909dadf2SRanjani Sridharan ARRAY_SIZE(common_dai_link_tokens),
1907909dadf2SRanjani Sridharan private->array, le32_to_cpu(private->size));
1908909dadf2SRanjani Sridharan if (ret < 0) {
1909909dadf2SRanjani Sridharan dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
1910909dadf2SRanjani Sridharan kfree(slink->hw_configs);
1911909dadf2SRanjani Sridharan kfree(slink);
1912909dadf2SRanjani Sridharan return ret;
1913909dadf2SRanjani Sridharan }
1914909dadf2SRanjani Sridharan
1915dbdbf88bSPeter Ujfalusi token_list = tplg_ops ? tplg_ops->token_list : NULL;
1916909dadf2SRanjani Sridharan if (!token_list)
1917909dadf2SRanjani Sridharan goto out;
1918909dadf2SRanjani Sridharan
1919909dadf2SRanjani Sridharan /* calculate size of tuples array */
1920909dadf2SRanjani Sridharan num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
1921909dadf2SRanjani Sridharan num_sets = slink->num_hw_configs;
1922909dadf2SRanjani Sridharan switch (slink->type) {
1923311ce4feSLiam Girdwood case SOF_DAI_INTEL_SSP:
1924909dadf2SRanjani Sridharan token_id = SOF_SSP_TOKENS;
1925909dadf2SRanjani Sridharan num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
1926311ce4feSLiam Girdwood break;
1927311ce4feSLiam Girdwood case SOF_DAI_INTEL_DMIC:
1928909dadf2SRanjani Sridharan token_id = SOF_DMIC_TOKENS;
1929909dadf2SRanjani Sridharan num_tuples += token_list[SOF_DMIC_TOKENS].count;
1930909dadf2SRanjani Sridharan
1931909dadf2SRanjani Sridharan /* Allocate memory for max PDM controllers */
1932909dadf2SRanjani Sridharan num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
1933311ce4feSLiam Girdwood break;
1934311ce4feSLiam Girdwood case SOF_DAI_INTEL_HDA:
1935909dadf2SRanjani Sridharan token_id = SOF_HDA_TOKENS;
1936909dadf2SRanjani Sridharan num_tuples += token_list[SOF_HDA_TOKENS].count;
1937311ce4feSLiam Girdwood break;
19384d6bbf1aSPierre-Louis Bossart case SOF_DAI_INTEL_ALH:
1939909dadf2SRanjani Sridharan token_id = SOF_ALH_TOKENS;
1940909dadf2SRanjani Sridharan num_tuples += token_list[SOF_ALH_TOKENS].count;
19414d6bbf1aSPierre-Louis Bossart break;
1942f59b16efSDaniel Baluta case SOF_DAI_IMX_SAI:
1943909dadf2SRanjani Sridharan token_id = SOF_SAI_TOKENS;
1944909dadf2SRanjani Sridharan num_tuples += token_list[SOF_SAI_TOKENS].count;
1945f59b16efSDaniel Baluta break;
1946f59b16efSDaniel Baluta case SOF_DAI_IMX_ESAI:
1947909dadf2SRanjani Sridharan token_id = SOF_ESAI_TOKENS;
1948909dadf2SRanjani Sridharan num_tuples += token_list[SOF_ESAI_TOKENS].count;
1949efb931cdSAjit Kumar Pandey break;
1950b72bfcffSYC Hung case SOF_DAI_MEDIATEK_AFE:
1951909dadf2SRanjani Sridharan token_id = SOF_AFE_TOKENS;
1952909dadf2SRanjani Sridharan num_tuples += token_list[SOF_AFE_TOKENS].count;
1953b72bfcffSYC Hung break;
1954689614ceSAjit Kumar Pandey case SOF_DAI_AMD_DMIC:
1955689614ceSAjit Kumar Pandey token_id = SOF_ACPDMIC_TOKENS;
1956689614ceSAjit Kumar Pandey num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
1957689614ceSAjit Kumar Pandey break;
195875af4199SV sujith kumar Reddy case SOF_DAI_AMD_SP:
195975af4199SV sujith kumar Reddy case SOF_DAI_AMD_HS:
196075af4199SV sujith kumar Reddy case SOF_DAI_AMD_SP_VIRTUAL:
196175af4199SV sujith kumar Reddy case SOF_DAI_AMD_HS_VIRTUAL:
196275af4199SV sujith kumar Reddy token_id = SOF_ACPI2S_TOKENS;
196375af4199SV sujith kumar Reddy num_tuples += token_list[SOF_ACPI2S_TOKENS].count;
196475af4199SV sujith kumar Reddy break;
1965311ce4feSLiam Girdwood default:
1966311ce4feSLiam Girdwood break;
1967311ce4feSLiam Girdwood }
1968311ce4feSLiam Girdwood
1969909dadf2SRanjani Sridharan /* allocate memory for tuples array */
19700a480df0SChristophe JAILLET slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
1971909dadf2SRanjani Sridharan if (!slink->tuples) {
1972909dadf2SRanjani Sridharan kfree(slink->hw_configs);
1973909dadf2SRanjani Sridharan kfree(slink);
1974909dadf2SRanjani Sridharan return -ENOMEM;
1975909dadf2SRanjani Sridharan }
1976909dadf2SRanjani Sridharan
1977a5ba725eSRanjani Sridharan if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
1978909dadf2SRanjani Sridharan /* parse one set of DAI link tokens */
1979909dadf2SRanjani Sridharan ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1980909dadf2SRanjani Sridharan SOF_DAI_LINK_TOKENS, 1, slink->tuples,
1981909dadf2SRanjani Sridharan num_tuples, &slink->num_tuples);
1982909dadf2SRanjani Sridharan if (ret < 0) {
1983909dadf2SRanjani Sridharan dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1984909dadf2SRanjani Sridharan token_list[SOF_DAI_LINK_TOKENS].name, link->name);
1985909dadf2SRanjani Sridharan goto err;
1986909dadf2SRanjani Sridharan }
1987a5ba725eSRanjani Sridharan }
1988909dadf2SRanjani Sridharan
1989909dadf2SRanjani Sridharan /* nothing more to do if there are no DAI type-specific tokens defined */
1990909dadf2SRanjani Sridharan if (!token_id || !token_list[token_id].tokens)
1991909dadf2SRanjani Sridharan goto out;
1992909dadf2SRanjani Sridharan
1993909dadf2SRanjani Sridharan /* parse "num_sets" sets of DAI-specific tokens */
1994909dadf2SRanjani Sridharan ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1995909dadf2SRanjani Sridharan token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
1996909dadf2SRanjani Sridharan if (ret < 0) {
1997909dadf2SRanjani Sridharan dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1998909dadf2SRanjani Sridharan token_list[token_id].name, link->name);
1999909dadf2SRanjani Sridharan goto err;
2000909dadf2SRanjani Sridharan }
2001909dadf2SRanjani Sridharan
2002909dadf2SRanjani Sridharan /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
2003909dadf2SRanjani Sridharan if (token_id == SOF_DMIC_TOKENS) {
2004909dadf2SRanjani Sridharan num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
2005909dadf2SRanjani Sridharan slink->tuples, slink->num_tuples);
2006909dadf2SRanjani Sridharan
2007909dadf2SRanjani Sridharan if (num_sets < 0) {
2008909dadf2SRanjani Sridharan dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
2009909dadf2SRanjani Sridharan ret = num_sets;
2010909dadf2SRanjani Sridharan goto err;
2011909dadf2SRanjani Sridharan }
2012909dadf2SRanjani Sridharan
2013909dadf2SRanjani Sridharan ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
2014909dadf2SRanjani Sridharan SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
2015909dadf2SRanjani Sridharan num_tuples, &slink->num_tuples);
2016909dadf2SRanjani Sridharan if (ret < 0) {
2017909dadf2SRanjani Sridharan dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
2018909dadf2SRanjani Sridharan token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
2019909dadf2SRanjani Sridharan goto err;
2020909dadf2SRanjani Sridharan }
2021909dadf2SRanjani Sridharan }
2022909dadf2SRanjani Sridharan out:
2023909dadf2SRanjani Sridharan link->dobj.private = slink;
2024909dadf2SRanjani Sridharan list_add(&slink->list, &sdev->dai_link_list);
2025909dadf2SRanjani Sridharan
2026909dadf2SRanjani Sridharan return 0;
2027909dadf2SRanjani Sridharan
2028909dadf2SRanjani Sridharan err:
2029909dadf2SRanjani Sridharan kfree(slink->tuples);
2030909dadf2SRanjani Sridharan kfree(slink->hw_configs);
2031909dadf2SRanjani Sridharan kfree(slink);
2032c1c03888SJaska Uimonen
2033c1c03888SJaska Uimonen return ret;
2034311ce4feSLiam Girdwood }
2035311ce4feSLiam Girdwood
sof_link_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)2036909dadf2SRanjani Sridharan static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
2037909dadf2SRanjani Sridharan {
2038909dadf2SRanjani Sridharan struct snd_sof_dai_link *slink = dobj->private;
2039909dadf2SRanjani Sridharan
2040909dadf2SRanjani Sridharan if (!slink)
2041909dadf2SRanjani Sridharan return 0;
2042909dadf2SRanjani Sridharan
2043*f39bde3fSChen-Yu Tsai slink->link->platforms->name = NULL;
2044*f39bde3fSChen-Yu Tsai
2045909dadf2SRanjani Sridharan kfree(slink->tuples);
2046909dadf2SRanjani Sridharan list_del(&slink->list);
2047909dadf2SRanjani Sridharan kfree(slink->hw_configs);
2048909dadf2SRanjani Sridharan kfree(slink);
2049909dadf2SRanjani Sridharan dobj->private = NULL;
2050909dadf2SRanjani Sridharan
2051909dadf2SRanjani Sridharan return 0;
2052909dadf2SRanjani Sridharan }
2053909dadf2SRanjani Sridharan
2054311ce4feSLiam Girdwood /* DAI link - used for any driver specific init */
sof_route_load(struct snd_soc_component * scomp,int index,struct snd_soc_dapm_route * route)2055311ce4feSLiam Girdwood static int sof_route_load(struct snd_soc_component *scomp, int index,
2056311ce4feSLiam Girdwood struct snd_soc_dapm_route *route)
2057311ce4feSLiam Girdwood {
2058311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2059311ce4feSLiam Girdwood struct snd_sof_widget *source_swidget, *sink_swidget;
2060311ce4feSLiam Girdwood struct snd_soc_dobj *dobj = &route->dobj;
2061311ce4feSLiam Girdwood struct snd_sof_route *sroute;
2062311ce4feSLiam Girdwood int ret = 0;
2063311ce4feSLiam Girdwood
2064311ce4feSLiam Girdwood /* allocate memory for sroute and connect */
2065311ce4feSLiam Girdwood sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
2066311ce4feSLiam Girdwood if (!sroute)
2067311ce4feSLiam Girdwood return -ENOMEM;
2068311ce4feSLiam Girdwood
2069ee1e79b7SRanjani Sridharan sroute->scomp = scomp;
2070ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "sink %s control %s source %s\n",
2071311ce4feSLiam Girdwood route->sink, route->control ? route->control : "none",
2072311ce4feSLiam Girdwood route->source);
2073311ce4feSLiam Girdwood
2074311ce4feSLiam Girdwood /* source component */
2075ee1e79b7SRanjani Sridharan source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
2076311ce4feSLiam Girdwood if (!source_swidget) {
2077ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: source %s not found\n",
2078311ce4feSLiam Girdwood route->source);
2079311ce4feSLiam Girdwood ret = -EINVAL;
2080311ce4feSLiam Girdwood goto err;
2081311ce4feSLiam Girdwood }
2082311ce4feSLiam Girdwood
2083311ce4feSLiam Girdwood /*
2084311ce4feSLiam Girdwood * Virtual widgets of type output/out_drv may be added in topology
2085311ce4feSLiam Girdwood * for compatibility. These are not handled by the FW.
2086311ce4feSLiam Girdwood * So, don't send routes whose source/sink widget is of such types
2087311ce4feSLiam Girdwood * to the DSP.
2088311ce4feSLiam Girdwood */
2089311ce4feSLiam Girdwood if (source_swidget->id == snd_soc_dapm_out_drv ||
2090311ce4feSLiam Girdwood source_swidget->id == snd_soc_dapm_output)
2091311ce4feSLiam Girdwood goto err;
2092311ce4feSLiam Girdwood
2093311ce4feSLiam Girdwood /* sink component */
2094ee1e79b7SRanjani Sridharan sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
2095311ce4feSLiam Girdwood if (!sink_swidget) {
2096ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: sink %s not found\n",
2097311ce4feSLiam Girdwood route->sink);
2098311ce4feSLiam Girdwood ret = -EINVAL;
2099311ce4feSLiam Girdwood goto err;
2100311ce4feSLiam Girdwood }
2101311ce4feSLiam Girdwood
2102311ce4feSLiam Girdwood /*
2103311ce4feSLiam Girdwood * Don't send routes whose sink widget is of type
2104311ce4feSLiam Girdwood * output or out_drv to the DSP
2105311ce4feSLiam Girdwood */
2106311ce4feSLiam Girdwood if (sink_swidget->id == snd_soc_dapm_out_drv ||
2107311ce4feSLiam Girdwood sink_swidget->id == snd_soc_dapm_output)
2108311ce4feSLiam Girdwood goto err;
2109311ce4feSLiam Girdwood
2110311ce4feSLiam Girdwood sroute->route = route;
2111311ce4feSLiam Girdwood dobj->private = sroute;
21120a2dea1fSRanjani Sridharan sroute->src_widget = source_swidget;
21130a2dea1fSRanjani Sridharan sroute->sink_widget = sink_swidget;
2114311ce4feSLiam Girdwood
2115311ce4feSLiam Girdwood /* add route to route list */
2116311ce4feSLiam Girdwood list_add(&sroute->list, &sdev->route_list);
2117311ce4feSLiam Girdwood
2118b9f8e138SGuennadi Liakhovetski return 0;
2119311ce4feSLiam Girdwood err:
2120311ce4feSLiam Girdwood kfree(sroute);
2121311ce4feSLiam Girdwood return ret;
2122311ce4feSLiam Girdwood }
2123311ce4feSLiam Girdwood
21242c28ecadSRanjani Sridharan /**
21259c04363dSRanjani Sridharan * sof_set_widget_pipeline - Set pipeline for a component
21262c28ecadSRanjani Sridharan * @sdev: pointer to struct snd_sof_dev
21279c04363dSRanjani Sridharan * @spipe: pointer to struct snd_sof_pipeline
21282c28ecadSRanjani Sridharan * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
21292c28ecadSRanjani Sridharan *
21302c28ecadSRanjani Sridharan * Return: 0 if successful, -EINVAL on error.
21312c28ecadSRanjani Sridharan * The function checks if @swidget is associated with any volatile controls. If so, setting
21322c28ecadSRanjani Sridharan * the dynamic_pipeline_widget is disallowed.
21332c28ecadSRanjani Sridharan */
sof_set_widget_pipeline(struct snd_sof_dev * sdev,struct snd_sof_pipeline * spipe,struct snd_sof_widget * swidget)21349c04363dSRanjani Sridharan static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe,
21352c28ecadSRanjani Sridharan struct snd_sof_widget *swidget)
21362c28ecadSRanjani Sridharan {
21379c04363dSRanjani Sridharan struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
21382c28ecadSRanjani Sridharan struct snd_sof_control *scontrol;
21392c28ecadSRanjani Sridharan
21402c28ecadSRanjani Sridharan if (pipe_widget->dynamic_pipeline_widget) {
21412c28ecadSRanjani Sridharan /* dynamic widgets cannot have volatile kcontrols */
21422c28ecadSRanjani Sridharan list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
21432c28ecadSRanjani Sridharan if (scontrol->comp_id == swidget->comp_id &&
21442c28ecadSRanjani Sridharan (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
21452c28ecadSRanjani Sridharan dev_err(sdev->dev,
21462c28ecadSRanjani Sridharan "error: volatile control found for dynamic widget %s\n",
21472c28ecadSRanjani Sridharan swidget->widget->name);
21482c28ecadSRanjani Sridharan return -EINVAL;
21492c28ecadSRanjani Sridharan }
21502c28ecadSRanjani Sridharan }
21512c28ecadSRanjani Sridharan
21529c04363dSRanjani Sridharan /* set the pipeline and apply the dynamic_pipeline_widget_flag */
21539c04363dSRanjani Sridharan swidget->spipe = spipe;
21542c28ecadSRanjani Sridharan swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
21552c28ecadSRanjani Sridharan
21562c28ecadSRanjani Sridharan return 0;
21572c28ecadSRanjani Sridharan }
21582c28ecadSRanjani Sridharan
2159311ce4feSLiam Girdwood /* completion - called at completion of firmware loading */
sof_complete(struct snd_soc_component * scomp)2160415717e1SRanjani Sridharan static int sof_complete(struct snd_soc_component *scomp)
2161311ce4feSLiam Girdwood {
2162311ce4feSLiam Girdwood struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2163dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2164dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_widget_ops *widget_ops;
2165b5cee8feSRanjani Sridharan struct snd_sof_control *scontrol;
21669c04363dSRanjani Sridharan struct snd_sof_pipeline *spipe;
2167415717e1SRanjani Sridharan int ret;
2168311ce4feSLiam Girdwood
2169dbdbf88bSPeter Ujfalusi widget_ops = tplg_ops ? tplg_ops->widget : NULL;
2170dbdbf88bSPeter Ujfalusi
2171b5cee8feSRanjani Sridharan /* first update all control IPC structures based on the IPC version */
2172dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->control_setup)
2173b5cee8feSRanjani Sridharan list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2174dbdbf88bSPeter Ujfalusi ret = tplg_ops->control_setup(sdev, scontrol);
2175b5cee8feSRanjani Sridharan if (ret < 0) {
2176b5cee8feSRanjani Sridharan dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
2177b5cee8feSRanjani Sridharan scontrol->name);
2178b5cee8feSRanjani Sridharan return ret;
2179b5cee8feSRanjani Sridharan }
2180b5cee8feSRanjani Sridharan }
2181b5cee8feSRanjani Sridharan
21823d3e223fSRanjani Sridharan /* set up the IPC structures for the pipeline widgets */
21833d3e223fSRanjani Sridharan list_for_each_entry(spipe, &sdev->pipeline_list, list) {
21843d3e223fSRanjani Sridharan struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
21853d3e223fSRanjani Sridharan struct snd_sof_widget *swidget;
21863d3e223fSRanjani Sridharan
2187defc0c63SRanjani Sridharan pipe_widget->instance_id = -EINVAL;
2188defc0c63SRanjani Sridharan
21893d3e223fSRanjani Sridharan /* Update the scheduler widget's IPC structure */
21903d3e223fSRanjani Sridharan if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) {
21913d3e223fSRanjani Sridharan ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget);
21923d3e223fSRanjani Sridharan if (ret < 0) {
21933d3e223fSRanjani Sridharan dev_err(sdev->dev, "failed updating IPC struct for %s\n",
21943d3e223fSRanjani Sridharan pipe_widget->widget->name);
21953d3e223fSRanjani Sridharan return ret;
21963d3e223fSRanjani Sridharan }
21973d3e223fSRanjani Sridharan }
21983d3e223fSRanjani Sridharan
21993d3e223fSRanjani Sridharan /* set the pipeline and update the IPC structure for the non scheduler widgets */
22003d3e223fSRanjani Sridharan list_for_each_entry(swidget, &sdev->widget_list, list)
22013d3e223fSRanjani Sridharan if (swidget->widget->id != snd_soc_dapm_scheduler &&
22023d3e223fSRanjani Sridharan swidget->pipeline_id == pipe_widget->pipeline_id) {
22033d3e223fSRanjani Sridharan ret = sof_set_widget_pipeline(sdev, spipe, swidget);
22043d3e223fSRanjani Sridharan if (ret < 0)
22053d3e223fSRanjani Sridharan return ret;
22063d3e223fSRanjani Sridharan
2207dbdbf88bSPeter Ujfalusi if (widget_ops && widget_ops[swidget->id].ipc_setup) {
22087006d20eSRanjani Sridharan ret = widget_ops[swidget->id].ipc_setup(swidget);
22097006d20eSRanjani Sridharan if (ret < 0) {
22103d3e223fSRanjani Sridharan dev_err(sdev->dev,
22113d3e223fSRanjani Sridharan "failed updating IPC struct for %s\n",
22127006d20eSRanjani Sridharan swidget->widget->name);
22137006d20eSRanjani Sridharan return ret;
22147006d20eSRanjani Sridharan }
22157006d20eSRanjani Sridharan }
22167006d20eSRanjani Sridharan }
2217311ce4feSLiam Girdwood }
22181b7d57d7SRanjani Sridharan
2219c0e7969cSRanjani Sridharan /* verify topology components loading including dynamic pipelines */
222012b401f4SPeter Ujfalusi if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
2221dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->set_up_all_pipelines &&
2222dbdbf88bSPeter Ujfalusi tplg_ops->tear_down_all_pipelines) {
2223dbdbf88bSPeter Ujfalusi ret = tplg_ops->set_up_all_pipelines(sdev, true);
2224c0e7969cSRanjani Sridharan if (ret < 0) {
222531cd6e46SRanjani Sridharan dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
222631cd6e46SRanjani Sridharan ret);
2227c0e7969cSRanjani Sridharan return ret;
2228c0e7969cSRanjani Sridharan }
2229c0e7969cSRanjani Sridharan
2230dbdbf88bSPeter Ujfalusi ret = tplg_ops->tear_down_all_pipelines(sdev, true);
2231c0e7969cSRanjani Sridharan if (ret < 0) {
223231cd6e46SRanjani Sridharan dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
223331cd6e46SRanjani Sridharan ret);
2234c0e7969cSRanjani Sridharan return ret;
2235c0e7969cSRanjani Sridharan }
2236c0e7969cSRanjani Sridharan }
223731cd6e46SRanjani Sridharan }
2238c0e7969cSRanjani Sridharan
22391b7d57d7SRanjani Sridharan /* set up static pipelines */
2240dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->set_up_all_pipelines)
2241dbdbf88bSPeter Ujfalusi return tplg_ops->set_up_all_pipelines(sdev, false);
224231cd6e46SRanjani Sridharan
224331cd6e46SRanjani Sridharan return 0;
2244311ce4feSLiam Girdwood }
2245311ce4feSLiam Girdwood
2246311ce4feSLiam Girdwood /* manifest - optional to inform component of manifest */
sof_manifest(struct snd_soc_component * scomp,int index,struct snd_soc_tplg_manifest * man)2247311ce4feSLiam Girdwood static int sof_manifest(struct snd_soc_component *scomp, int index,
2248311ce4feSLiam Girdwood struct snd_soc_tplg_manifest *man)
2249311ce4feSLiam Girdwood {
2250323aa1f0SRanjani Sridharan struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2251dbdbf88bSPeter Ujfalusi const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2252311ce4feSLiam Girdwood
2253dbdbf88bSPeter Ujfalusi if (tplg_ops && tplg_ops->parse_manifest)
2254dbdbf88bSPeter Ujfalusi return tplg_ops->parse_manifest(scomp, index, man);
22558e3a6e45SPierre-Louis Bossart
22568e3a6e45SPierre-Louis Bossart return 0;
22578e3a6e45SPierre-Louis Bossart }
22588e3a6e45SPierre-Louis Bossart
2259311ce4feSLiam Girdwood /* vendor specific kcontrol handlers available for binding */
2260311ce4feSLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
2261311ce4feSLiam Girdwood {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
2262311ce4feSLiam Girdwood {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
2263311ce4feSLiam Girdwood {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
2264311ce4feSLiam Girdwood {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
2265311ce4feSLiam Girdwood };
2266311ce4feSLiam Girdwood
2267311ce4feSLiam Girdwood /* vendor specific bytes ext handlers available for binding */
2268311ce4feSLiam Girdwood static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
2269311ce4feSLiam Girdwood {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
2270783560d0SDharageswari R {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
2271311ce4feSLiam Girdwood };
2272311ce4feSLiam Girdwood
2273311ce4feSLiam Girdwood static struct snd_soc_tplg_ops sof_tplg_ops = {
2274311ce4feSLiam Girdwood /* external kcontrol init - used for any driver specific init */
2275311ce4feSLiam Girdwood .control_load = sof_control_load,
2276311ce4feSLiam Girdwood .control_unload = sof_control_unload,
2277311ce4feSLiam Girdwood
2278311ce4feSLiam Girdwood /* external kcontrol init - used for any driver specific init */
2279311ce4feSLiam Girdwood .dapm_route_load = sof_route_load,
2280311ce4feSLiam Girdwood .dapm_route_unload = sof_route_unload,
2281311ce4feSLiam Girdwood
2282311ce4feSLiam Girdwood /* external widget init - used for any driver specific init */
2283311ce4feSLiam Girdwood /* .widget_load is not currently used */
2284311ce4feSLiam Girdwood .widget_ready = sof_widget_ready,
2285311ce4feSLiam Girdwood .widget_unload = sof_widget_unload,
2286311ce4feSLiam Girdwood
2287311ce4feSLiam Girdwood /* FE DAI - used for any driver specific init */
2288311ce4feSLiam Girdwood .dai_load = sof_dai_load,
2289311ce4feSLiam Girdwood .dai_unload = sof_dai_unload,
2290311ce4feSLiam Girdwood
2291311ce4feSLiam Girdwood /* DAI link - used for any driver specific init */
2292311ce4feSLiam Girdwood .link_load = sof_link_load,
2293909dadf2SRanjani Sridharan .link_unload = sof_link_unload,
2294311ce4feSLiam Girdwood
2295311ce4feSLiam Girdwood /* completion - called at completion of firmware loading */
2296311ce4feSLiam Girdwood .complete = sof_complete,
2297311ce4feSLiam Girdwood
2298311ce4feSLiam Girdwood /* manifest - optional to inform component of manifest */
2299311ce4feSLiam Girdwood .manifest = sof_manifest,
2300311ce4feSLiam Girdwood
2301311ce4feSLiam Girdwood /* vendor specific kcontrol handlers available for binding */
2302311ce4feSLiam Girdwood .io_ops = sof_io_ops,
2303311ce4feSLiam Girdwood .io_ops_count = ARRAY_SIZE(sof_io_ops),
2304311ce4feSLiam Girdwood
2305311ce4feSLiam Girdwood /* vendor specific bytes ext handlers available for binding */
2306311ce4feSLiam Girdwood .bytes_ext_ops = sof_bytes_ext_ops,
2307311ce4feSLiam Girdwood .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
2308311ce4feSLiam Girdwood };
2309311ce4feSLiam Girdwood
snd_sof_dspless_kcontrol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)231028d40e7aSPeter Ujfalusi static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol,
231128d40e7aSPeter Ujfalusi struct snd_ctl_elem_value *ucontrol)
231228d40e7aSPeter Ujfalusi {
231328d40e7aSPeter Ujfalusi return 0;
231428d40e7aSPeter Ujfalusi }
231528d40e7aSPeter Ujfalusi
231628d40e7aSPeter Ujfalusi static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = {
231728d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
231828d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
231928d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
232028d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
232128d40e7aSPeter Ujfalusi };
232228d40e7aSPeter Ujfalusi
snd_sof_dspless_bytes_ext_get(struct snd_kcontrol * kcontrol,unsigned int __user * binary_data,unsigned int size)232328d40e7aSPeter Ujfalusi static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol,
232428d40e7aSPeter Ujfalusi unsigned int __user *binary_data,
232528d40e7aSPeter Ujfalusi unsigned int size)
232628d40e7aSPeter Ujfalusi {
232728d40e7aSPeter Ujfalusi return 0;
232828d40e7aSPeter Ujfalusi }
232928d40e7aSPeter Ujfalusi
snd_sof_dspless_bytes_ext_put(struct snd_kcontrol * kcontrol,const unsigned int __user * binary_data,unsigned int size)233028d40e7aSPeter Ujfalusi static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol,
233128d40e7aSPeter Ujfalusi const unsigned int __user *binary_data,
233228d40e7aSPeter Ujfalusi unsigned int size)
233328d40e7aSPeter Ujfalusi {
233428d40e7aSPeter Ujfalusi return 0;
233528d40e7aSPeter Ujfalusi }
233628d40e7aSPeter Ujfalusi
233728d40e7aSPeter Ujfalusi static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = {
233828d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put},
233928d40e7aSPeter Ujfalusi {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get},
234028d40e7aSPeter Ujfalusi };
234128d40e7aSPeter Ujfalusi
234228d40e7aSPeter Ujfalusi /* external widget init - used for any driver specific init */
sof_dspless_widget_ready(struct snd_soc_component * scomp,int index,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * tw)234328d40e7aSPeter Ujfalusi static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index,
234428d40e7aSPeter Ujfalusi struct snd_soc_dapm_widget *w,
234528d40e7aSPeter Ujfalusi struct snd_soc_tplg_dapm_widget *tw)
234628d40e7aSPeter Ujfalusi {
234728d40e7aSPeter Ujfalusi if (WIDGET_IS_DAI(w->id)) {
234828d40e7aSPeter Ujfalusi struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
234928d40e7aSPeter Ujfalusi struct snd_sof_widget *swidget;
235028d40e7aSPeter Ujfalusi struct snd_sof_dai dai;
235128d40e7aSPeter Ujfalusi int ret;
235228d40e7aSPeter Ujfalusi
235328d40e7aSPeter Ujfalusi swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
235428d40e7aSPeter Ujfalusi if (!swidget)
235528d40e7aSPeter Ujfalusi return -ENOMEM;
235628d40e7aSPeter Ujfalusi
235728d40e7aSPeter Ujfalusi memset(&dai, 0, sizeof(dai));
235828d40e7aSPeter Ujfalusi
235928d40e7aSPeter Ujfalusi ret = sof_connect_dai_widget(scomp, w, tw, &dai);
236028d40e7aSPeter Ujfalusi if (ret) {
236128d40e7aSPeter Ujfalusi kfree(swidget);
236228d40e7aSPeter Ujfalusi return ret;
236328d40e7aSPeter Ujfalusi }
236428d40e7aSPeter Ujfalusi
236528d40e7aSPeter Ujfalusi swidget->scomp = scomp;
236628d40e7aSPeter Ujfalusi swidget->widget = w;
236728d40e7aSPeter Ujfalusi mutex_init(&swidget->setup_mutex);
236828d40e7aSPeter Ujfalusi w->dobj.private = swidget;
236928d40e7aSPeter Ujfalusi list_add(&swidget->list, &sdev->widget_list);
237028d40e7aSPeter Ujfalusi }
237128d40e7aSPeter Ujfalusi
237228d40e7aSPeter Ujfalusi return 0;
237328d40e7aSPeter Ujfalusi }
237428d40e7aSPeter Ujfalusi
sof_dspless_widget_unload(struct snd_soc_component * scomp,struct snd_soc_dobj * dobj)237528d40e7aSPeter Ujfalusi static int sof_dspless_widget_unload(struct snd_soc_component *scomp,
237628d40e7aSPeter Ujfalusi struct snd_soc_dobj *dobj)
237728d40e7aSPeter Ujfalusi {
237828d40e7aSPeter Ujfalusi struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
237928d40e7aSPeter Ujfalusi
238028d40e7aSPeter Ujfalusi if (WIDGET_IS_DAI(w->id)) {
238128d40e7aSPeter Ujfalusi struct snd_sof_widget *swidget = dobj->private;
238228d40e7aSPeter Ujfalusi
238328d40e7aSPeter Ujfalusi sof_disconnect_dai_widget(scomp, w);
238428d40e7aSPeter Ujfalusi
238528d40e7aSPeter Ujfalusi if (!swidget)
238628d40e7aSPeter Ujfalusi return 0;
238728d40e7aSPeter Ujfalusi
238828d40e7aSPeter Ujfalusi /* remove and free swidget object */
238928d40e7aSPeter Ujfalusi list_del(&swidget->list);
239028d40e7aSPeter Ujfalusi kfree(swidget);
239128d40e7aSPeter Ujfalusi }
239228d40e7aSPeter Ujfalusi
239328d40e7aSPeter Ujfalusi return 0;
239428d40e7aSPeter Ujfalusi }
239528d40e7aSPeter Ujfalusi
sof_dspless_link_load(struct snd_soc_component * scomp,int index,struct snd_soc_dai_link * link,struct snd_soc_tplg_link_config * cfg)239628d40e7aSPeter Ujfalusi static int sof_dspless_link_load(struct snd_soc_component *scomp, int index,
239728d40e7aSPeter Ujfalusi struct snd_soc_dai_link *link,
239828d40e7aSPeter Ujfalusi struct snd_soc_tplg_link_config *cfg)
239928d40e7aSPeter Ujfalusi {
240028d40e7aSPeter Ujfalusi link->platforms->name = dev_name(scomp->dev);
240128d40e7aSPeter Ujfalusi
240228d40e7aSPeter Ujfalusi /* Set nonatomic property for FE dai links for FE-BE compatibility */
240328d40e7aSPeter Ujfalusi if (!link->no_pcm)
240428d40e7aSPeter Ujfalusi link->nonatomic = true;
240528d40e7aSPeter Ujfalusi
240628d40e7aSPeter Ujfalusi return 0;
240728d40e7aSPeter Ujfalusi }
240828d40e7aSPeter Ujfalusi
240928d40e7aSPeter Ujfalusi static struct snd_soc_tplg_ops sof_dspless_tplg_ops = {
241028d40e7aSPeter Ujfalusi /* external widget init - used for any driver specific init */
241128d40e7aSPeter Ujfalusi .widget_ready = sof_dspless_widget_ready,
241228d40e7aSPeter Ujfalusi .widget_unload = sof_dspless_widget_unload,
241328d40e7aSPeter Ujfalusi
241428d40e7aSPeter Ujfalusi /* FE DAI - used for any driver specific init */
241528d40e7aSPeter Ujfalusi .dai_load = sof_dai_load,
241628d40e7aSPeter Ujfalusi .dai_unload = sof_dai_unload,
241728d40e7aSPeter Ujfalusi
241828d40e7aSPeter Ujfalusi /* DAI link - used for any driver specific init */
241928d40e7aSPeter Ujfalusi .link_load = sof_dspless_link_load,
242028d40e7aSPeter Ujfalusi
242128d40e7aSPeter Ujfalusi /* vendor specific kcontrol handlers available for binding */
242228d40e7aSPeter Ujfalusi .io_ops = sof_dspless_io_ops,
242328d40e7aSPeter Ujfalusi .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops),
242428d40e7aSPeter Ujfalusi
242528d40e7aSPeter Ujfalusi /* vendor specific bytes ext handlers available for binding */
242628d40e7aSPeter Ujfalusi .bytes_ext_ops = sof_dspless_bytes_ext_ops,
242728d40e7aSPeter Ujfalusi .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops),
242828d40e7aSPeter Ujfalusi };
242928d40e7aSPeter Ujfalusi
snd_sof_load_topology(struct snd_soc_component * scomp,const char * file)2430ee1e79b7SRanjani Sridharan int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
2431311ce4feSLiam Girdwood {
24329b014266SJaroslav Kysela struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2433311ce4feSLiam Girdwood const struct firmware *fw;
2434311ce4feSLiam Girdwood int ret;
2435311ce4feSLiam Girdwood
2436ee1e79b7SRanjani Sridharan dev_dbg(scomp->dev, "loading topology:%s\n", file);
2437311ce4feSLiam Girdwood
2438ee1e79b7SRanjani Sridharan ret = request_firmware(&fw, file, scomp->dev);
2439311ce4feSLiam Girdwood if (ret < 0) {
2440ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2441311ce4feSLiam Girdwood file, ret);
244289e641aeSKai Vehmanen dev_err(scomp->dev,
244389e641aeSKai Vehmanen "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
2444311ce4feSLiam Girdwood return ret;
2445311ce4feSLiam Girdwood }
2446311ce4feSLiam Girdwood
244728d40e7aSPeter Ujfalusi if (sdev->dspless_mode_selected)
244828d40e7aSPeter Ujfalusi ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
244928d40e7aSPeter Ujfalusi else
2450a5b8f71cSAmadeusz Sławiński ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
245128d40e7aSPeter Ujfalusi
2452311ce4feSLiam Girdwood if (ret < 0) {
2453ee1e79b7SRanjani Sridharan dev_err(scomp->dev, "error: tplg component load failed %d\n",
2454311ce4feSLiam Girdwood ret);
2455311ce4feSLiam Girdwood ret = -EINVAL;
2456311ce4feSLiam Girdwood }
2457311ce4feSLiam Girdwood
2458311ce4feSLiam Girdwood release_firmware(fw);
24599b014266SJaroslav Kysela
24609b014266SJaroslav Kysela if (ret >= 0 && sdev->led_present)
24619b014266SJaroslav Kysela ret = snd_ctl_led_request();
24629b014266SJaroslav Kysela
2463311ce4feSLiam Girdwood return ret;
2464311ce4feSLiam Girdwood }
2465311ce4feSLiam Girdwood EXPORT_SYMBOL(snd_sof_load_topology);
2466