xref: /openbmc/linux/sound/soc/soc-topology.c (revision 3789debf)
1f2b6a1b2SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2f2b6a1b2SKuninori Morimoto //
3f2b6a1b2SKuninori Morimoto // soc-topology.c  --  ALSA SoC Topology
4f2b6a1b2SKuninori Morimoto //
5f2b6a1b2SKuninori Morimoto // Copyright (C) 2012 Texas Instruments Inc.
6f2b6a1b2SKuninori Morimoto // Copyright (C) 2015 Intel Corporation.
7f2b6a1b2SKuninori Morimoto //
8f2b6a1b2SKuninori Morimoto // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9f2b6a1b2SKuninori Morimoto //		K, Mythri P <mythri.p.k@intel.com>
10f2b6a1b2SKuninori Morimoto //		Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11f2b6a1b2SKuninori Morimoto //		B, Jayachandran <jayachandran.b@intel.com>
12f2b6a1b2SKuninori Morimoto //		Abdullah, Omair M <omair.m.abdullah@intel.com>
13f2b6a1b2SKuninori Morimoto //		Jin, Yao <yao.jin@intel.com>
14f2b6a1b2SKuninori Morimoto //		Lin, Mengdong <mengdong.lin@intel.com>
15f2b6a1b2SKuninori Morimoto //
16f2b6a1b2SKuninori Morimoto //  Add support to read audio firmware topology alongside firmware text. The
17f2b6a1b2SKuninori Morimoto //  topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18f2b6a1b2SKuninori Morimoto //  equalizers, firmware, coefficients etc.
19f2b6a1b2SKuninori Morimoto //
20f2b6a1b2SKuninori Morimoto //  This file only manages the core ALSA and ASoC components, all other bespoke
21f2b6a1b2SKuninori Morimoto //  firmware topology data is passed to component drivers for bespoke handling.
228a978234SLiam Girdwood 
238a978234SLiam Girdwood #include <linux/kernel.h>
248a978234SLiam Girdwood #include <linux/export.h>
258a978234SLiam Girdwood #include <linux/list.h>
268a978234SLiam Girdwood #include <linux/firmware.h>
278a978234SLiam Girdwood #include <linux/slab.h>
288a978234SLiam Girdwood #include <sound/soc.h>
298a978234SLiam Girdwood #include <sound/soc-dapm.h>
308a978234SLiam Girdwood #include <sound/soc-topology.h>
3128a87eebSMengdong Lin #include <sound/tlv.h>
328a978234SLiam Girdwood 
338a978234SLiam Girdwood /*
348a978234SLiam Girdwood  * We make several passes over the data (since it wont necessarily be ordered)
358a978234SLiam Girdwood  * and process objects in the following order. This guarantees the component
368a978234SLiam Girdwood  * drivers will be ready with any vendor data before the mixers and DAPM objects
378a978234SLiam Girdwood  * are loaded (that may make use of the vendor data).
388a978234SLiam Girdwood  */
398a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST		0
408a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR		1
418a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER		2
428a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET		3
431a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI		4
441a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH		5
451a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PINS		6
460038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI		7
47593d9e52SMengdong Lin #define SOC_TPLG_PASS_LINK		8
488a978234SLiam Girdwood 
498a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
50593d9e52SMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_LINK
518a978234SLiam Girdwood 
52583958faSMengdong Lin /* topology context */
538a978234SLiam Girdwood struct soc_tplg {
548a978234SLiam Girdwood 	const struct firmware *fw;
558a978234SLiam Girdwood 
568a978234SLiam Girdwood 	/* runtime FW parsing */
578a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
588a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
598a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
608a978234SLiam Girdwood 
618a978234SLiam Girdwood 	/* component caller */
628a978234SLiam Girdwood 	struct device *dev;
638a978234SLiam Girdwood 	struct snd_soc_component *comp;
648a978234SLiam Girdwood 	u32 index;	/* current block index */
658a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
668a978234SLiam Girdwood 
6788a17d8fSMengdong Lin 	/* vendor specific kcontrol operations */
688a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
698a978234SLiam Girdwood 	int io_ops_count;
708a978234SLiam Girdwood 
711a3232d2SMengdong Lin 	/* vendor specific bytes ext handlers, for TLV bytes controls */
721a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
731a3232d2SMengdong Lin 	int bytes_ext_ops_count;
741a3232d2SMengdong Lin 
758a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
768a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
778a978234SLiam Girdwood };
788a978234SLiam Girdwood 
798a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg);
808a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
818a978234SLiam Girdwood struct snd_soc_dapm_widget *
828a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
838a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
848a978234SLiam Girdwood struct snd_soc_dapm_widget *
858a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
868a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
878a978234SLiam Girdwood 
888a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
898a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
908a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
918a978234SLiam Girdwood {
928a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
938a978234SLiam Girdwood 
948a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
958a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
968a978234SLiam Girdwood 			elem_type);
978a978234SLiam Girdwood 		return -EINVAL;
988a978234SLiam Girdwood 	}
998a978234SLiam Girdwood 
1008a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
1018a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1028a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1038a978234SLiam Girdwood 		dev_err(tplg->dev,
1048a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1058a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1068a978234SLiam Girdwood 		return -EINVAL;
1078a978234SLiam Girdwood 	}
1088a978234SLiam Girdwood 
1098a978234SLiam Girdwood 	return 0;
1108a978234SLiam Girdwood }
1118a978234SLiam Girdwood 
1128a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1138a978234SLiam Girdwood {
1148a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1158a978234SLiam Girdwood 
1168a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1178a978234SLiam Girdwood 		return 1;
1188a978234SLiam Girdwood 	return 0;
1198a978234SLiam Girdwood }
1208a978234SLiam Girdwood 
1218a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1228a978234SLiam Girdwood {
1238a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1248a978234SLiam Girdwood }
1258a978234SLiam Girdwood 
1268a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1278a978234SLiam Girdwood {
1288a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1298a978234SLiam Girdwood }
1308a978234SLiam Girdwood 
1318a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1328a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1338a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1348a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1358a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1368a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1378a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1388a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1398a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1408a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1418a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1428a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1438a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1448a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1458a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1468a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1478a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1488a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1498a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1502c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1518a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1528a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1538a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1548a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1558a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1568a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1578a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1588a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1598a978234SLiam Girdwood };
1608a978234SLiam Girdwood 
1618a978234SLiam Girdwood struct soc_tplg_map {
1628a978234SLiam Girdwood 	int uid;
1638a978234SLiam Girdwood 	int kid;
1648a978234SLiam Girdwood };
1658a978234SLiam Girdwood 
1668a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1678a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1688a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1708a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1818a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1828a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1838a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1848a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
1858a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
1868a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
1878a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
1888a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
1898a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
1908a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
1918a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
1928a978234SLiam Girdwood };
1938a978234SLiam Girdwood 
1948a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1958a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1968a978234SLiam Girdwood {
1978a978234SLiam Girdwood 	int i;
1988a978234SLiam Girdwood 
1998a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2008a978234SLiam Girdwood 		if (chan[i].id == map)
2018a978234SLiam Girdwood 			return chan[i].reg;
2028a978234SLiam Girdwood 	}
2038a978234SLiam Girdwood 
2048a978234SLiam Girdwood 	return -EINVAL;
2058a978234SLiam Girdwood }
2068a978234SLiam Girdwood 
2078a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2088a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2098a978234SLiam Girdwood {
2108a978234SLiam Girdwood 	int i;
2118a978234SLiam Girdwood 
2128a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2138a978234SLiam Girdwood 		if (chan[i].id == map)
2148a978234SLiam Girdwood 			return chan[i].shift;
2158a978234SLiam Girdwood 	}
2168a978234SLiam Girdwood 
2178a978234SLiam Girdwood 	return -EINVAL;
2188a978234SLiam Girdwood }
2198a978234SLiam Girdwood 
2208a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2218a978234SLiam Girdwood {
2228a978234SLiam Girdwood 	int i;
2238a978234SLiam Girdwood 
2248a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2258a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2268a978234SLiam Girdwood 			return dapm_map[i].kid;
2278a978234SLiam Girdwood 	}
2288a978234SLiam Girdwood 
2298a978234SLiam Girdwood 	return -EINVAL;
2308a978234SLiam Girdwood }
2318a978234SLiam Girdwood 
2328a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2338a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2348a978234SLiam Girdwood {
2358a978234SLiam Girdwood 	dev_err(tplg->dev,
2368a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2378a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2388a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2398a978234SLiam Girdwood }
2408a978234SLiam Girdwood 
2418a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2428a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2438a978234SLiam Girdwood {
2448a978234SLiam Girdwood 	dev_err(tplg->dev,
2458a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2468a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2478a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2488a978234SLiam Girdwood }
2498a978234SLiam Girdwood 
2508a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2518a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2528a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2538a978234SLiam Girdwood {
2548a978234SLiam Girdwood 	int ret = 0;
2558a978234SLiam Girdwood 
2568a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
257c60b613aSLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
2588a978234SLiam Girdwood 	else {
2598a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2608a978234SLiam Girdwood 			hdr->vendor_type);
2618a978234SLiam Girdwood 		return -EINVAL;
2628a978234SLiam Girdwood 	}
2638a978234SLiam Girdwood 
2648a978234SLiam Girdwood 	if (ret < 0)
2658a978234SLiam Girdwood 		dev_err(tplg->dev,
2668a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2678a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2688a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2698a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2708a978234SLiam Girdwood 	return ret;
2718a978234SLiam Girdwood }
2728a978234SLiam Girdwood 
2738a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2748a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2758a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2768a978234SLiam Girdwood {
2778a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2788a978234SLiam Girdwood 		return 0;
2798a978234SLiam Girdwood 
2808a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2818a978234SLiam Girdwood }
2828a978234SLiam Girdwood 
2838a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2848a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2858a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2868a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2878a978234SLiam Girdwood {
2888a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
289c60b613aSLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
290c60b613aSLiam Girdwood 			tplg_w);
2918a978234SLiam Girdwood 
2928a978234SLiam Girdwood 	return 0;
2938a978234SLiam Girdwood }
2948a978234SLiam Girdwood 
295ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
296ebd259d3SLiam Girdwood  * external widgets where we can assign private data/ops */
297ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg,
298ebd259d3SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
299ebd259d3SLiam Girdwood {
300ebd259d3SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
301c60b613aSLiam Girdwood 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
302c60b613aSLiam Girdwood 			tplg_w);
303ebd259d3SLiam Girdwood 
304ebd259d3SLiam Girdwood 	return 0;
305ebd259d3SLiam Girdwood }
306ebd259d3SLiam Girdwood 
307183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */
30864527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
309c60b613aSLiam Girdwood 	struct snd_soc_dai_driver *dai_drv,
310c60b613aSLiam Girdwood 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
3118a978234SLiam Girdwood {
31264527e8aSMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
313c60b613aSLiam Girdwood 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
314c60b613aSLiam Girdwood 			pcm, dai);
3158a978234SLiam Girdwood 
3168a978234SLiam Girdwood 	return 0;
3178a978234SLiam Girdwood }
3188a978234SLiam Girdwood 
319183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */
320acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
321c60b613aSLiam Girdwood 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
322acfc7d46SMengdong Lin {
323acfc7d46SMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
324c60b613aSLiam Girdwood 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
325acfc7d46SMengdong Lin 
326acfc7d46SMengdong Lin 	return 0;
327acfc7d46SMengdong Lin }
328acfc7d46SMengdong Lin 
3298a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3308a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3318a978234SLiam Girdwood {
3328a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3338a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3348a978234SLiam Girdwood }
3358a978234SLiam Girdwood 
3368a978234SLiam Girdwood /* add a dynamic kcontrol */
3378a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3388a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3398a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3408a978234SLiam Girdwood {
3418a978234SLiam Girdwood 	int err;
3428a978234SLiam Girdwood 
3438a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3448a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3458a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3468a978234SLiam Girdwood 		control_new->name);
3478a978234SLiam Girdwood 		return -ENOMEM;
3488a978234SLiam Girdwood 	}
3498a978234SLiam Girdwood 
3508a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3518a978234SLiam Girdwood 	if (err < 0) {
3528a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3538a978234SLiam Girdwood 			control_new->name, err);
3548a978234SLiam Girdwood 		return err;
3558a978234SLiam Girdwood 	}
3568a978234SLiam Girdwood 
3578a978234SLiam Girdwood 	return 0;
3588a978234SLiam Girdwood }
3598a978234SLiam Girdwood 
3608a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3618a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3628a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3638a978234SLiam Girdwood {
3648a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3658a978234SLiam Girdwood 
3668a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3678a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3688a978234SLiam Girdwood }
3698a978234SLiam Girdwood 
3708a978234SLiam Girdwood /* remove a mixer kcontrol */
3718a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3728a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3738a978234SLiam Girdwood {
3748a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3758a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3768a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3778a978234SLiam Girdwood 	const unsigned int *p = NULL;
3788a978234SLiam Girdwood 
3798a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3808a978234SLiam Girdwood 		return;
3818a978234SLiam Girdwood 
3828a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3838a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3848a978234SLiam Girdwood 
38533ae6ae2SAmadeusz Sławiński 	if (dobj->control.kcontrol->tlv.p)
38633ae6ae2SAmadeusz Sławiński 		p = dobj->control.kcontrol->tlv.p;
38733ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
38833ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
3898a978234SLiam Girdwood 	kfree(sm);
3908a978234SLiam Girdwood 	kfree(p);
3918a978234SLiam Girdwood }
3928a978234SLiam Girdwood 
3938a978234SLiam Girdwood /* remove an enum kcontrol */
3948a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3958a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3968a978234SLiam Girdwood {
3978a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3988a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
3998a978234SLiam Girdwood 	int i;
4008a978234SLiam Girdwood 
4018a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4028a978234SLiam Girdwood 		return;
4038a978234SLiam Girdwood 
4048a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4058a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4068a978234SLiam Girdwood 
40733ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
40833ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4098a978234SLiam Girdwood 
41033ae6ae2SAmadeusz Sławiński 	kfree(dobj->control.dvalues);
4118a978234SLiam Girdwood 	for (i = 0; i < se->items; i++)
41233ae6ae2SAmadeusz Sławiński 		kfree(dobj->control.dtexts[i]);
41334db6a3eSAmadeusz Sławiński 	kfree(dobj->control.dtexts);
4148a978234SLiam Girdwood 	kfree(se);
4158a978234SLiam Girdwood }
4168a978234SLiam Girdwood 
4178a978234SLiam Girdwood /* remove a byte kcontrol */
4188a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4198a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4208a978234SLiam Girdwood {
4218a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4228a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4238a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4248a978234SLiam Girdwood 
4258a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4268a978234SLiam Girdwood 		return;
4278a978234SLiam Girdwood 
4288a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4298a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4308a978234SLiam Girdwood 
43133ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
43233ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4338a978234SLiam Girdwood 	kfree(sb);
4348a978234SLiam Girdwood }
4358a978234SLiam Girdwood 
4367df04ea7SRanjani Sridharan /* remove a route */
4377df04ea7SRanjani Sridharan static void remove_route(struct snd_soc_component *comp,
4387df04ea7SRanjani Sridharan 			 struct snd_soc_dobj *dobj, int pass)
4397df04ea7SRanjani Sridharan {
4407df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route *route =
4417df04ea7SRanjani Sridharan 		container_of(dobj, struct snd_soc_dapm_route, dobj);
4427df04ea7SRanjani Sridharan 
4437df04ea7SRanjani Sridharan 	if (pass != SOC_TPLG_PASS_GRAPH)
4447df04ea7SRanjani Sridharan 		return;
4457df04ea7SRanjani Sridharan 
4467df04ea7SRanjani Sridharan 	if (dobj->ops && dobj->ops->dapm_route_unload)
4477df04ea7SRanjani Sridharan 		dobj->ops->dapm_route_unload(comp, dobj);
4487df04ea7SRanjani Sridharan 
4497df04ea7SRanjani Sridharan 	list_del(&dobj->list);
4507df04ea7SRanjani Sridharan 	kfree(route);
4517df04ea7SRanjani Sridharan }
4527df04ea7SRanjani Sridharan 
4538a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4548a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4558a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4568a978234SLiam Girdwood {
4578a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4588a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4598a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4608a978234SLiam Girdwood 	int i;
4618a978234SLiam Girdwood 
4628a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4638a978234SLiam Girdwood 		return;
4648a978234SLiam Girdwood 
4658a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4668a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4678a978234SLiam Girdwood 
46805bdcf12SLiam Girdwood 	if (!w->kcontrols)
46905bdcf12SLiam Girdwood 		goto free_news;
47005bdcf12SLiam Girdwood 
4718a978234SLiam Girdwood 	/*
4721a7dd6e2SMengdong Lin 	 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
4738a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4748a978234SLiam Girdwood 	 */
475eea3dd4fSMengdong Lin 	if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
4768a978234SLiam Girdwood 		/* enumerated widget mixer */
477f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4781a7dd6e2SMengdong Lin 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4798a978234SLiam Girdwood 			struct soc_enum *se =
4801a7dd6e2SMengdong Lin 				(struct soc_enum *)kcontrol->private_value;
481d7766aa5SColin Ian King 			int j;
4828a978234SLiam Girdwood 
4831a7dd6e2SMengdong Lin 			snd_ctl_remove(card, kcontrol);
4848a978234SLiam Girdwood 
48533ae6ae2SAmadeusz Sławiński 			kfree(dobj->control.dvalues);
486d7766aa5SColin Ian King 			for (j = 0; j < se->items; j++)
48733ae6ae2SAmadeusz Sławiński 				kfree(dobj->control.dtexts[j]);
48834db6a3eSAmadeusz Sławiński 			kfree(dobj->control.dtexts);
4898a978234SLiam Girdwood 
4908a978234SLiam Girdwood 			kfree(se);
491267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
4921a7dd6e2SMengdong Lin 		}
4938a978234SLiam Girdwood 	} else {
494eea3dd4fSMengdong Lin 		/* volume mixer or bytes controls */
495f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4968a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4978a978234SLiam Girdwood 
498eea3dd4fSMengdong Lin 			if (dobj->widget.kcontrol_type
499eea3dd4fSMengdong Lin 			    == SND_SOC_TPLG_TYPE_MIXER)
500eea3dd4fSMengdong Lin 				kfree(kcontrol->tlv.p);
5018a978234SLiam Girdwood 
502eea3dd4fSMengdong Lin 			/* Private value is used as struct soc_mixer_control
503eea3dd4fSMengdong Lin 			 * for volume mixers or soc_bytes_ext for bytes
504eea3dd4fSMengdong Lin 			 * controls.
505eea3dd4fSMengdong Lin 			 */
506eea3dd4fSMengdong Lin 			kfree((void *)kcontrol->private_value);
507c2b36129SColin Ian King 			snd_ctl_remove(card, kcontrol);
508267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
5098a978234SLiam Girdwood 		}
5108a978234SLiam Girdwood 	}
51105bdcf12SLiam Girdwood 
51205bdcf12SLiam Girdwood free_news:
5138a978234SLiam Girdwood 	kfree(w->kcontrol_news);
51405bdcf12SLiam Girdwood 
515a46e8393SAmadeusz Sławiński 	list_del(&dobj->list);
516a46e8393SAmadeusz Sławiński 
5178a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
5188a978234SLiam Girdwood }
5198a978234SLiam Girdwood 
52064527e8aSMengdong Lin /* remove DAI configurations */
52164527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
5228a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
5238a978234SLiam Girdwood {
52464527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
52564527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
52652abe6ccSGuennadi Liakhovetski 	struct snd_soc_dai *dai;
52764527e8aSMengdong Lin 
5288a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
5298a978234SLiam Girdwood 		return;
5308a978234SLiam Girdwood 
53164527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
53264527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
5338a978234SLiam Girdwood 
53452abe6ccSGuennadi Liakhovetski 	list_for_each_entry(dai, &comp->dai_list, list)
53552abe6ccSGuennadi Liakhovetski 		if (dai->driver == dai_drv)
53652abe6ccSGuennadi Liakhovetski 			dai->driver = NULL;
53752abe6ccSGuennadi Liakhovetski 
5388f27c4abSMengdong Lin 	kfree(dai_drv->name);
5398a978234SLiam Girdwood 	list_del(&dobj->list);
54064527e8aSMengdong Lin 	kfree(dai_drv);
5418a978234SLiam Girdwood }
5428a978234SLiam Girdwood 
543acfc7d46SMengdong Lin /* remove link configurations */
544acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
545acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
546acfc7d46SMengdong Lin {
547acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
548acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
549acfc7d46SMengdong Lin 
550acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
551acfc7d46SMengdong Lin 		return;
552acfc7d46SMengdong Lin 
553acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
554acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
555acfc7d46SMengdong Lin 
5568f27c4abSMengdong Lin 	kfree(link->name);
5578f27c4abSMengdong Lin 	kfree(link->stream_name);
5588f27c4abSMengdong Lin 	kfree(link->cpu_dai_name);
5598f27c4abSMengdong Lin 
560acfc7d46SMengdong Lin 	list_del(&dobj->list);
561acfc7d46SMengdong Lin 	snd_soc_remove_dai_link(comp->card, link);
562acfc7d46SMengdong Lin 	kfree(link);
563acfc7d46SMengdong Lin }
564acfc7d46SMengdong Lin 
565adfebb51SBard liao /* unload dai link */
566adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp,
567adfebb51SBard liao 	struct snd_soc_dobj *dobj, int pass)
568adfebb51SBard liao {
569adfebb51SBard liao 	if (pass != SOC_TPLG_PASS_LINK)
570adfebb51SBard liao 		return;
571adfebb51SBard liao 
572adfebb51SBard liao 	if (dobj->ops && dobj->ops->link_unload)
573adfebb51SBard liao 		dobj->ops->link_unload(comp, dobj);
574adfebb51SBard liao 
575adfebb51SBard liao 	/*
576adfebb51SBard liao 	 * We don't free the link here as what remove_link() do since BE
577adfebb51SBard liao 	 * links are not allocated by topology.
578adfebb51SBard liao 	 * We however need to reset the dobj type to its initial values
579adfebb51SBard liao 	 */
580adfebb51SBard liao 	dobj->type = SND_SOC_DOBJ_NONE;
581adfebb51SBard liao 	list_del(&dobj->list);
582adfebb51SBard liao }
583adfebb51SBard liao 
5848a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5858a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5868a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5872b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5888a978234SLiam Girdwood {
5892b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5901a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5912b5cdb91SMengdong Lin 	int num_ops, i;
5928a978234SLiam Girdwood 
5931a3232d2SMengdong Lin 	if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
5941a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5951a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5961a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5971a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5981a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
5991a3232d2SMengdong Lin 
6001a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
6011a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
6021a3232d2SMengdong Lin 
6031a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
6041a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
6051a3232d2SMengdong Lin 		 */
606f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
6071a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
6081a3232d2SMengdong Lin 
6091a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
6101a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
6111a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
6121a3232d2SMengdong Lin 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
6131a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
6141a3232d2SMengdong Lin 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
6151a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
6161a3232d2SMengdong Lin 		}
6171a3232d2SMengdong Lin 
6181a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
6191a3232d2SMengdong Lin 			return 0;
6201a3232d2SMengdong Lin 		else
6211a3232d2SMengdong Lin 			return -EINVAL;
6221a3232d2SMengdong Lin 	}
6231a3232d2SMengdong Lin 
62488a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
6252b5cdb91SMengdong Lin 	ops = tplg->io_ops;
6262b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
6278a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
6288a978234SLiam Girdwood 
6292b5cdb91SMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
6308a978234SLiam Girdwood 			k->put = ops[i].put;
6312b5cdb91SMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
6328a978234SLiam Girdwood 			k->get = ops[i].get;
6332b5cdb91SMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6342b5cdb91SMengdong Lin 			k->info = ops[i].info;
6358a978234SLiam Girdwood 	}
6368a978234SLiam Girdwood 
63788a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
63888a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
63988a17d8fSMengdong Lin 		return 0;
64088a17d8fSMengdong Lin 
64188a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
6422b5cdb91SMengdong Lin 	ops = io_ops;
6432b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
64488a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
64588a17d8fSMengdong Lin 
64688a17d8fSMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
64788a17d8fSMengdong Lin 			k->put = ops[i].put;
64888a17d8fSMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
64988a17d8fSMengdong Lin 			k->get = ops[i].get;
65088a17d8fSMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6518a978234SLiam Girdwood 			k->info = ops[i].info;
6528a978234SLiam Girdwood 	}
6538a978234SLiam Girdwood 
6548a978234SLiam Girdwood 	/* standard handlers found ? */
6558a978234SLiam Girdwood 	if (k->put && k->get && k->info)
6568a978234SLiam Girdwood 		return 0;
6578a978234SLiam Girdwood 
6588a978234SLiam Girdwood 	/* nothing to bind */
6598a978234SLiam Girdwood 	return -EINVAL;
6608a978234SLiam Girdwood }
6618a978234SLiam Girdwood 
6628a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
6638a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
6648a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
6658a978234SLiam Girdwood 		int num_events, u16 event_type)
6668a978234SLiam Girdwood {
6678a978234SLiam Girdwood 	int i;
6688a978234SLiam Girdwood 
6698a978234SLiam Girdwood 	w->event = NULL;
6708a978234SLiam Girdwood 
6718a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
6728a978234SLiam Girdwood 		if (event_type == events[i].type) {
6738a978234SLiam Girdwood 
6748a978234SLiam Girdwood 			/* found - so assign event */
6758a978234SLiam Girdwood 			w->event = events[i].event_handler;
6768a978234SLiam Girdwood 			return 0;
6778a978234SLiam Girdwood 		}
6788a978234SLiam Girdwood 	}
6798a978234SLiam Girdwood 
6808a978234SLiam Girdwood 	/* not found */
6818a978234SLiam Girdwood 	return -EINVAL;
6828a978234SLiam Girdwood }
6838a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
6848a978234SLiam Girdwood 
6858a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6868a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6878a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6888a978234SLiam Girdwood {
6898a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
690c60b613aSLiam Girdwood 		return tplg->ops->control_load(tplg->comp, tplg->index, k,
691c60b613aSLiam Girdwood 			hdr);
6928a978234SLiam Girdwood 
6938a978234SLiam Girdwood 	return 0;
6948a978234SLiam Girdwood }
6958a978234SLiam Girdwood 
69628a87eebSMengdong Lin 
69728a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
69828a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6998a978234SLiam Girdwood {
70028a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
70128a87eebSMengdong Lin 	unsigned int *p;
7028a978234SLiam Girdwood 
70328a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
70428a87eebSMengdong Lin 	if (!p)
7058a978234SLiam Girdwood 		return -ENOMEM;
7068a978234SLiam Girdwood 
70728a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
70828a87eebSMengdong Lin 	p[1] = item_len;
70928a87eebSMengdong Lin 	p[2] = scale->min;
71028a87eebSMengdong Lin 	p[3] = (scale->step & TLV_DB_SCALE_MASK)
71128a87eebSMengdong Lin 			| (scale->mute ? TLV_DB_SCALE_MUTE : 0);
7128a978234SLiam Girdwood 
71328a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
71428a87eebSMengdong Lin 	return 0;
71528a87eebSMengdong Lin }
71628a87eebSMengdong Lin 
71728a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
71828a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
71928a87eebSMengdong Lin {
72028a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
72128a87eebSMengdong Lin 
72228a87eebSMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
72328a87eebSMengdong Lin 		return 0;
72428a87eebSMengdong Lin 
7251a3232d2SMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
72628a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
72728a87eebSMengdong Lin 		switch (tplg_tlv->type) {
72828a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
72928a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
73028a87eebSMengdong Lin 					&tplg_tlv->scale);
73128a87eebSMengdong Lin 
73228a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
73328a87eebSMengdong Lin 		default:
73428a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
73528a87eebSMengdong Lin 					tplg_tlv->type);
73628a87eebSMengdong Lin 			return -EINVAL;
73728a87eebSMengdong Lin 		}
73828a87eebSMengdong Lin 	}
7398a978234SLiam Girdwood 
7408a978234SLiam Girdwood 	return 0;
7418a978234SLiam Girdwood }
7428a978234SLiam Girdwood 
7438a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
7448a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
7458a978234SLiam Girdwood {
7468a978234SLiam Girdwood 	kfree(kc->tlv.p);
7478a978234SLiam Girdwood }
7488a978234SLiam Girdwood 
7498a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
7508a978234SLiam Girdwood 	size_t size)
7518a978234SLiam Girdwood {
7528a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
7538a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
7548a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7558a978234SLiam Girdwood 	int i, err;
7568a978234SLiam Girdwood 
7578a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7588a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
7598a978234SLiam Girdwood 			size, "mixer bytes")) {
7608a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
7618a978234SLiam Girdwood 			count);
7628a978234SLiam Girdwood 		return -EINVAL;
7638a978234SLiam Girdwood 	}
7648a978234SLiam Girdwood 
7658a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7668a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
7678a978234SLiam Girdwood 
7688a978234SLiam Girdwood 		/* validate kcontrol */
7698a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7708a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7718a978234SLiam Girdwood 			return -EINVAL;
7728a978234SLiam Girdwood 
7738a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
7748a978234SLiam Girdwood 		if (sbe == NULL)
7758a978234SLiam Girdwood 			return -ENOMEM;
7768a978234SLiam Girdwood 
7778a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
7788a978234SLiam Girdwood 			be->priv.size);
7798a978234SLiam Girdwood 
7808a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7818a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
7828a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
7838a978234SLiam Girdwood 
7848a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7858a978234SLiam Girdwood 		kc.name = be->hdr.name;
7868a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7878a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7888a978234SLiam Girdwood 		kc.access = be->hdr.access;
7898a978234SLiam Girdwood 
7908a978234SLiam Girdwood 		sbe->max = be->max;
7918a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7928a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7938a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7948a978234SLiam Girdwood 
7958a978234SLiam Girdwood 		/* map io handlers */
7962b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7978a978234SLiam Girdwood 		if (err) {
7988a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
7998a978234SLiam Girdwood 			kfree(sbe);
8008a978234SLiam Girdwood 			continue;
8018a978234SLiam Girdwood 		}
8028a978234SLiam Girdwood 
8038a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8048a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8058a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
8068a978234SLiam Girdwood 		if (err < 0) {
8078a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8088a978234SLiam Girdwood 				be->hdr.name);
8098a978234SLiam Girdwood 			kfree(sbe);
8108a978234SLiam Girdwood 			continue;
8118a978234SLiam Girdwood 		}
8128a978234SLiam Girdwood 
8138a978234SLiam Girdwood 		/* register control here */
8148a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8158a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
8168a978234SLiam Girdwood 		if (err < 0) {
8178a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8188a978234SLiam Girdwood 				be->hdr.name);
8198a978234SLiam Girdwood 			kfree(sbe);
8208a978234SLiam Girdwood 			continue;
8218a978234SLiam Girdwood 		}
8228a978234SLiam Girdwood 
8238a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
8248a978234SLiam Girdwood 	}
8258a978234SLiam Girdwood 	return 0;
8268a978234SLiam Girdwood 
8278a978234SLiam Girdwood }
8288a978234SLiam Girdwood 
8298a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
8308a978234SLiam Girdwood 	size_t size)
8318a978234SLiam Girdwood {
8328a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
8338a978234SLiam Girdwood 	struct soc_mixer_control *sm;
8348a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8358a978234SLiam Girdwood 	int i, err;
8368a978234SLiam Girdwood 
8378a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8388a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
8398a978234SLiam Girdwood 		count, size, "mixers")) {
8408a978234SLiam Girdwood 
8418a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
8428a978234SLiam Girdwood 			count);
8438a978234SLiam Girdwood 		return -EINVAL;
8448a978234SLiam Girdwood 	}
8458a978234SLiam Girdwood 
8468a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
8478a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
8488a978234SLiam Girdwood 
8498a978234SLiam Girdwood 		/* validate kcontrol */
8508a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8518a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8528a978234SLiam Girdwood 			return -EINVAL;
8538a978234SLiam Girdwood 
8548a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
8558a978234SLiam Girdwood 		if (sm == NULL)
8568a978234SLiam Girdwood 			return -ENOMEM;
8578a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
8588a978234SLiam Girdwood 			mc->priv.size);
8598a978234SLiam Girdwood 
8608a978234SLiam Girdwood 		dev_dbg(tplg->dev,
8618a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
8628a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
8638a978234SLiam Girdwood 
8648a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
8658a978234SLiam Girdwood 		kc.name = mc->hdr.name;
8668a978234SLiam Girdwood 		kc.private_value = (long)sm;
8678a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8688a978234SLiam Girdwood 		kc.access = mc->hdr.access;
8698a978234SLiam Girdwood 
8708a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
8718a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
8728a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8738a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
8748a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8758a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
8768a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8778a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
8788a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8798a978234SLiam Girdwood 
8808a978234SLiam Girdwood 		sm->max = mc->max;
8818a978234SLiam Girdwood 		sm->min = mc->min;
8828a978234SLiam Girdwood 		sm->invert = mc->invert;
8838a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
8848a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
8858a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8868a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8878a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8888a978234SLiam Girdwood 
8898a978234SLiam Girdwood 		/* map io handlers */
8902b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8918a978234SLiam Girdwood 		if (err) {
8928a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8938a978234SLiam Girdwood 			kfree(sm);
8948a978234SLiam Girdwood 			continue;
8958a978234SLiam Girdwood 		}
8968a978234SLiam Girdwood 
897*3789debfSBard liao 		/* create any TLV data */
898*3789debfSBard liao 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
899*3789debfSBard liao 
9008a978234SLiam Girdwood 		/* pass control to driver for optional further init */
9018a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
9028a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
9038a978234SLiam Girdwood 		if (err < 0) {
9048a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
9058a978234SLiam Girdwood 				mc->hdr.name);
906*3789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc);
9078a978234SLiam Girdwood 			kfree(sm);
9088a978234SLiam Girdwood 			continue;
9098a978234SLiam Girdwood 		}
9108a978234SLiam Girdwood 
9118a978234SLiam Girdwood 		/* register control here */
9128a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
9138a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
9148a978234SLiam Girdwood 		if (err < 0) {
9158a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
9168a978234SLiam Girdwood 				mc->hdr.name);
9178a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
9188a978234SLiam Girdwood 			kfree(sm);
9198a978234SLiam Girdwood 			continue;
9208a978234SLiam Girdwood 		}
9218a978234SLiam Girdwood 
9228a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
9238a978234SLiam Girdwood 	}
9248a978234SLiam Girdwood 
9258a978234SLiam Girdwood 	return 0;
9268a978234SLiam Girdwood }
9278a978234SLiam Girdwood 
9288a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
9298a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9308a978234SLiam Girdwood {
9318a978234SLiam Girdwood 	int i, ret;
9328a978234SLiam Girdwood 
9338a978234SLiam Girdwood 	se->dobj.control.dtexts =
9346396bb22SKees Cook 		kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
9358a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
9368a978234SLiam Girdwood 		return -ENOMEM;
9378a978234SLiam Girdwood 
9388a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
9398a978234SLiam Girdwood 
9408a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9418a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
9428a978234SLiam Girdwood 			ret = -EINVAL;
9438a978234SLiam Girdwood 			goto err;
9448a978234SLiam Girdwood 		}
9458a978234SLiam Girdwood 
9468a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
9478a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
9488a978234SLiam Girdwood 			ret = -ENOMEM;
9498a978234SLiam Girdwood 			goto err;
9508a978234SLiam Girdwood 		}
9518a978234SLiam Girdwood 	}
9528a978234SLiam Girdwood 
953b6e38b29SMousumi Jana 	se->texts = (const char * const *)se->dobj.control.dtexts;
9548a978234SLiam Girdwood 	return 0;
9558a978234SLiam Girdwood 
9568a978234SLiam Girdwood err:
9578a978234SLiam Girdwood 	for (--i; i >= 0; i--)
9588a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
9598a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
9608a978234SLiam Girdwood 	return ret;
9618a978234SLiam Girdwood }
9628a978234SLiam Girdwood 
9638a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
9648a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9658a978234SLiam Girdwood {
9668a978234SLiam Girdwood 	if (ec->items > sizeof(*ec->values))
9678a978234SLiam Girdwood 		return -EINVAL;
9688a978234SLiam Girdwood 
969376c0afeSAndrzej Hajda 	se->dobj.control.dvalues = kmemdup(ec->values,
970376c0afeSAndrzej Hajda 					   ec->items * sizeof(u32),
971376c0afeSAndrzej Hajda 					   GFP_KERNEL);
9728a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
9738a978234SLiam Girdwood 		return -ENOMEM;
9748a978234SLiam Girdwood 
9758a978234SLiam Girdwood 	return 0;
9768a978234SLiam Girdwood }
9778a978234SLiam Girdwood 
9788a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
9798a978234SLiam Girdwood 	size_t size)
9808a978234SLiam Girdwood {
9818a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
9828a978234SLiam Girdwood 	struct soc_enum *se;
9838a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
9848a978234SLiam Girdwood 	int i, ret, err;
9858a978234SLiam Girdwood 
9868a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
9878a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
9888a978234SLiam Girdwood 		count, size, "enums")) {
9898a978234SLiam Girdwood 
9908a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
9918a978234SLiam Girdwood 			count);
9928a978234SLiam Girdwood 		return -EINVAL;
9938a978234SLiam Girdwood 	}
9948a978234SLiam Girdwood 
9958a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
9968a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
9978a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
9988a978234SLiam Girdwood 			ec->priv.size);
9998a978234SLiam Girdwood 
10008a978234SLiam Girdwood 		/* validate kcontrol */
10018a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10028a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10038a978234SLiam Girdwood 			return -EINVAL;
10048a978234SLiam Girdwood 
10058a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
10068a978234SLiam Girdwood 		if (se == NULL)
10078a978234SLiam Girdwood 			return -ENOMEM;
10088a978234SLiam Girdwood 
10098a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
10108a978234SLiam Girdwood 			ec->hdr.name, ec->items);
10118a978234SLiam Girdwood 
10128a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
10138a978234SLiam Girdwood 		kc.name = ec->hdr.name;
10148a978234SLiam Girdwood 		kc.private_value = (long)se;
10158a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
10168a978234SLiam Girdwood 		kc.access = ec->hdr.access;
10178a978234SLiam Girdwood 
10188a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
10198a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
10208a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10218a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
10228a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10238a978234SLiam Girdwood 
10248a978234SLiam Girdwood 		se->items = ec->items;
10258a978234SLiam Girdwood 		se->mask = ec->mask;
10268a978234SLiam Girdwood 		se->dobj.index = tplg->index;
10278a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
10288a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
10298a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
10308a978234SLiam Girdwood 
10318a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
10328a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10338a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10348a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
10358a978234SLiam Girdwood 			if (err < 0) {
10368a978234SLiam Girdwood 				dev_err(tplg->dev,
10378a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
10388a978234SLiam Girdwood 					ec->hdr.name);
10398a978234SLiam Girdwood 				kfree(se);
10408a978234SLiam Girdwood 				continue;
10418a978234SLiam Girdwood 			}
10429c6c4d96STakashi Iwai 			/* fall through */
10438a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10448a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10458a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10468a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
10478a978234SLiam Girdwood 			if (err < 0) {
10488a978234SLiam Girdwood 				dev_err(tplg->dev,
10498a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
10508a978234SLiam Girdwood 					ec->hdr.name);
10518a978234SLiam Girdwood 				kfree(se);
10528a978234SLiam Girdwood 				continue;
10538a978234SLiam Girdwood 			}
10548a978234SLiam Girdwood 			break;
10558a978234SLiam Girdwood 		default:
10568a978234SLiam Girdwood 			dev_err(tplg->dev,
10578a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
10588a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
10598a978234SLiam Girdwood 			kfree(se);
10608a978234SLiam Girdwood 			continue;
10618a978234SLiam Girdwood 		}
10628a978234SLiam Girdwood 
10638a978234SLiam Girdwood 		/* map io handlers */
10642b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
10658a978234SLiam Girdwood 		if (err) {
10668a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
10678a978234SLiam Girdwood 			kfree(se);
10688a978234SLiam Girdwood 			continue;
10698a978234SLiam Girdwood 		}
10708a978234SLiam Girdwood 
10718a978234SLiam Girdwood 		/* pass control to driver for optional further init */
10728a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
10738a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
10748a978234SLiam Girdwood 		if (err < 0) {
10758a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
10768a978234SLiam Girdwood 				ec->hdr.name);
10778a978234SLiam Girdwood 			kfree(se);
10788a978234SLiam Girdwood 			continue;
10798a978234SLiam Girdwood 		}
10808a978234SLiam Girdwood 
10818a978234SLiam Girdwood 		/* register control here */
10828a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
10838a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
10848a978234SLiam Girdwood 		if (ret < 0) {
10858a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
10868a978234SLiam Girdwood 				ec->hdr.name);
10878a978234SLiam Girdwood 			kfree(se);
10888a978234SLiam Girdwood 			continue;
10898a978234SLiam Girdwood 		}
10908a978234SLiam Girdwood 
10918a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
10928a978234SLiam Girdwood 	}
10938a978234SLiam Girdwood 
10948a978234SLiam Girdwood 	return 0;
10958a978234SLiam Girdwood }
10968a978234SLiam Girdwood 
10978a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
10988a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10998a978234SLiam Girdwood {
11008a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
11018a978234SLiam Girdwood 	int i;
11028a978234SLiam Girdwood 
11038a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
11048a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
11058a978234SLiam Girdwood 		return 0;
11068a978234SLiam Girdwood 	}
11078a978234SLiam Girdwood 
11088a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
11098a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
11108a978234SLiam Girdwood 
11118a978234SLiam Girdwood 	for (i = 0; i < hdr->count; i++) {
11128a978234SLiam Girdwood 
11138a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
11148a978234SLiam Girdwood 
111506eb49f7SMengdong Lin 		if (control_hdr->size != sizeof(*control_hdr)) {
111606eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
111706eb49f7SMengdong Lin 			return -EINVAL;
111806eb49f7SMengdong Lin 		}
111906eb49f7SMengdong Lin 
11208a978234SLiam Girdwood 		switch (control_hdr->ops.info) {
11218a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
11228a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
11238a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
11248a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
11258a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
11268a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
11278a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
11288a978234SLiam Girdwood 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
11298a978234SLiam Girdwood 			break;
11308a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
11318a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
11328a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
11338a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
11348a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
11358a978234SLiam Girdwood 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
11368a978234SLiam Girdwood 			break;
11378a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
11388a978234SLiam Girdwood 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
11398a978234SLiam Girdwood 			break;
11408a978234SLiam Girdwood 		default:
11418a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
11428a978234SLiam Girdwood 			return -EINVAL;
11438a978234SLiam Girdwood 		}
11448a978234SLiam Girdwood 	}
11458a978234SLiam Girdwood 
11468a978234SLiam Girdwood 	return 0;
11478a978234SLiam Girdwood }
11488a978234SLiam Girdwood 
1149503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1150503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1151503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1152503e79b7SLiam Girdwood {
1153503e79b7SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1154503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1155503e79b7SLiam Girdwood 			route);
1156503e79b7SLiam Girdwood 
1157503e79b7SLiam Girdwood 	return 0;
1158503e79b7SLiam Girdwood }
1159503e79b7SLiam Girdwood 
11608a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
11618a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11628a978234SLiam Girdwood {
11638a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
11648a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
11657df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route **routes;
11667df04ea7SRanjani Sridharan 	int count = hdr->count, i, j;
11677df04ea7SRanjani Sridharan 	int ret = 0;
11688a978234SLiam Girdwood 
11698a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
11708a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
11718a978234SLiam Girdwood 		return 0;
11728a978234SLiam Girdwood 	}
11738a978234SLiam Girdwood 
11748a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
11758a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
11768a978234SLiam Girdwood 		count, hdr->payload_size, "graph")) {
11778a978234SLiam Girdwood 
11788a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
11798a978234SLiam Girdwood 			count);
11808a978234SLiam Girdwood 		return -EINVAL;
11818a978234SLiam Girdwood 	}
11828a978234SLiam Girdwood 
1183b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1184b75a6511SLiam Girdwood 		hdr->index);
11858a978234SLiam Girdwood 
11867df04ea7SRanjani Sridharan 	/* allocate memory for pointer to array of dapm routes */
11877df04ea7SRanjani Sridharan 	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
11887df04ea7SRanjani Sridharan 			 GFP_KERNEL);
11897df04ea7SRanjani Sridharan 	if (!routes)
11907df04ea7SRanjani Sridharan 		return -ENOMEM;
11917df04ea7SRanjani Sridharan 
11927df04ea7SRanjani Sridharan 	/*
11937df04ea7SRanjani Sridharan 	 * allocate memory for each dapm route in the array.
11947df04ea7SRanjani Sridharan 	 * This needs to be done individually so that
11957df04ea7SRanjani Sridharan 	 * each route can be freed when it is removed in remove_route().
11967df04ea7SRanjani Sridharan 	 */
11977df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
11987df04ea7SRanjani Sridharan 		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
11997df04ea7SRanjani Sridharan 		if (!routes[i]) {
12007df04ea7SRanjani Sridharan 			/* free previously allocated memory */
12017df04ea7SRanjani Sridharan 			for (j = 0; j < i; j++)
12027df04ea7SRanjani Sridharan 				kfree(routes[j]);
12037df04ea7SRanjani Sridharan 
12047df04ea7SRanjani Sridharan 			kfree(routes);
12057df04ea7SRanjani Sridharan 			return -ENOMEM;
12067df04ea7SRanjani Sridharan 		}
12077df04ea7SRanjani Sridharan 	}
12087df04ea7SRanjani Sridharan 
12098a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12108a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
12118a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
12128a978234SLiam Girdwood 
12138a978234SLiam Girdwood 		/* validate routes */
12148a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12157df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12167df04ea7SRanjani Sridharan 			ret = -EINVAL;
12177df04ea7SRanjani Sridharan 			break;
12187df04ea7SRanjani Sridharan 		}
12198a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12207df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12217df04ea7SRanjani Sridharan 			ret = -EINVAL;
12227df04ea7SRanjani Sridharan 			break;
12237df04ea7SRanjani Sridharan 		}
12248a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12257df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12267df04ea7SRanjani Sridharan 			ret = -EINVAL;
12277df04ea7SRanjani Sridharan 			break;
12288a978234SLiam Girdwood 		}
12298a978234SLiam Girdwood 
12307df04ea7SRanjani Sridharan 		routes[i]->source = elem->source;
12317df04ea7SRanjani Sridharan 		routes[i]->sink = elem->sink;
12327df04ea7SRanjani Sridharan 
12337df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
12347df04ea7SRanjani Sridharan 		routes[i]->connected = NULL;
12357df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
12367df04ea7SRanjani Sridharan 			routes[i]->control = NULL;
12377df04ea7SRanjani Sridharan 		else
12387df04ea7SRanjani Sridharan 			routes[i]->control = elem->control;
12397df04ea7SRanjani Sridharan 
12407df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
12417df04ea7SRanjani Sridharan 		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
12427df04ea7SRanjani Sridharan 		routes[i]->dobj.ops = tplg->ops;
12437df04ea7SRanjani Sridharan 		routes[i]->dobj.index = tplg->index;
12447df04ea7SRanjani Sridharan 		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
12457df04ea7SRanjani Sridharan 
12467df04ea7SRanjani Sridharan 		soc_tplg_add_route(tplg, routes[i]);
12477df04ea7SRanjani Sridharan 
12487df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
12497df04ea7SRanjani Sridharan 		snd_soc_dapm_add_routes(dapm, routes[i], 1);
12507df04ea7SRanjani Sridharan 	}
12517df04ea7SRanjani Sridharan 
12527df04ea7SRanjani Sridharan 	/* free memory allocated for all dapm routes in case of error */
12537df04ea7SRanjani Sridharan 	if (ret < 0)
12547df04ea7SRanjani Sridharan 		for (i = 0; i < count ; i++)
12557df04ea7SRanjani Sridharan 			kfree(routes[i]);
12567df04ea7SRanjani Sridharan 
12577df04ea7SRanjani Sridharan 	/*
12587df04ea7SRanjani Sridharan 	 * free pointer to array of dapm routes as this is no longer needed.
12597df04ea7SRanjani Sridharan 	 * The memory allocated for each dapm route will be freed
12607df04ea7SRanjani Sridharan 	 * when it is removed in remove_route().
12617df04ea7SRanjani Sridharan 	 */
12627df04ea7SRanjani Sridharan 	kfree(routes);
12637df04ea7SRanjani Sridharan 
12647df04ea7SRanjani Sridharan 	return ret;
12658a978234SLiam Girdwood }
12668a978234SLiam Girdwood 
12678a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
12688a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
12698a978234SLiam Girdwood {
12708a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
12718a978234SLiam Girdwood 	struct soc_mixer_control *sm;
12728a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
12738a978234SLiam Girdwood 	int i, err;
12748a978234SLiam Girdwood 
12754ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
12768a978234SLiam Girdwood 	if (kc == NULL)
12778a978234SLiam Girdwood 		return NULL;
12788a978234SLiam Girdwood 
12798a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
12808a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
12818a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
12828a978234SLiam Girdwood 		if (sm == NULL)
12838a978234SLiam Girdwood 			goto err;
12848a978234SLiam Girdwood 
12858a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
12868a978234SLiam Girdwood 			mc->priv.size);
12878a978234SLiam Girdwood 
12888a978234SLiam Girdwood 		/* validate kcontrol */
12898a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12908a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
12918a978234SLiam Girdwood 			goto err_str;
12928a978234SLiam Girdwood 
12938a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
12948a978234SLiam Girdwood 			mc->hdr.name, i);
12958a978234SLiam Girdwood 
1296267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1297267e2c6fSLiam Girdwood 		if (kc[i].name == NULL)
1298267e2c6fSLiam Girdwood 			goto err_str;
12998a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
13008a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13018a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
13028a978234SLiam Girdwood 
13038a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13048a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
13058a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13068a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
13078a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13088a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
13098a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13108a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
13118a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13128a978234SLiam Girdwood 
13138a978234SLiam Girdwood 		sm->max = mc->max;
13148a978234SLiam Girdwood 		sm->min = mc->min;
13158a978234SLiam Girdwood 		sm->invert = mc->invert;
13168a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
13178a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
13188a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
13198a978234SLiam Girdwood 
13208a978234SLiam Girdwood 		/* map io handlers */
13212b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
13228a978234SLiam Girdwood 		if (err) {
13238a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
13248a978234SLiam Girdwood 			kfree(sm);
13258a978234SLiam Girdwood 			continue;
13268a978234SLiam Girdwood 		}
13278a978234SLiam Girdwood 
1328*3789debfSBard liao 		/* create any TLV data */
1329*3789debfSBard liao 		soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1330*3789debfSBard liao 
13318a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13328a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13338a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
13348a978234SLiam Girdwood 		if (err < 0) {
13358a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13368a978234SLiam Girdwood 				mc->hdr.name);
1337*3789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc[i]);
13388a978234SLiam Girdwood 			kfree(sm);
13398a978234SLiam Girdwood 			continue;
13408a978234SLiam Girdwood 		}
13418a978234SLiam Girdwood 	}
13428a978234SLiam Girdwood 	return kc;
13438a978234SLiam Girdwood 
13448a978234SLiam Girdwood err_str:
13458a978234SLiam Girdwood 	kfree(sm);
13468a978234SLiam Girdwood err:
1347267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
13488a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1349267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1350267e2c6fSLiam Girdwood 	}
13518a978234SLiam Girdwood 	kfree(kc);
13528a978234SLiam Girdwood 	return NULL;
13538a978234SLiam Girdwood }
13548a978234SLiam Girdwood 
13558a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
13561a7dd6e2SMengdong Lin 	struct soc_tplg *tplg, int num_kcontrols)
13578a978234SLiam Girdwood {
13588a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13598a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
13608a978234SLiam Girdwood 	struct soc_enum *se;
13611a7dd6e2SMengdong Lin 	int i, j, err;
13628a978234SLiam Girdwood 
13631a7dd6e2SMengdong Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13641a7dd6e2SMengdong Lin 	if (kc == NULL)
13651a7dd6e2SMengdong Lin 		return NULL;
13661a7dd6e2SMengdong Lin 
13671a7dd6e2SMengdong Lin 	for (i = 0; i < num_kcontrols; i++) {
13688a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
13698a978234SLiam Girdwood 		/* validate kcontrol */
13708a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13718a978234SLiam Girdwood 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1372f3ee9096SChristophe JAILLET 			goto err;
13738a978234SLiam Girdwood 
13748a978234SLiam Girdwood 		se = kzalloc(sizeof(*se), GFP_KERNEL);
13758a978234SLiam Girdwood 		if (se == NULL)
13768a978234SLiam Girdwood 			goto err;
13778a978234SLiam Girdwood 
13788a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
13798a978234SLiam Girdwood 			ec->hdr.name);
13808a978234SLiam Girdwood 
1381267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
138265030ff3SDan Carpenter 		if (kc[i].name == NULL) {
138365030ff3SDan Carpenter 			kfree(se);
1384267e2c6fSLiam Girdwood 			goto err_se;
138565030ff3SDan Carpenter 		}
13861a7dd6e2SMengdong Lin 		kc[i].private_value = (long)se;
13871a7dd6e2SMengdong Lin 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13881a7dd6e2SMengdong Lin 		kc[i].access = ec->hdr.access;
13898a978234SLiam Girdwood 
13908a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13918a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
13921a7dd6e2SMengdong Lin 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
13931a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FL);
13941a7dd6e2SMengdong Lin 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
13951a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FR);
13968a978234SLiam Girdwood 
13978a978234SLiam Girdwood 		se->items = ec->items;
13988a978234SLiam Girdwood 		se->mask = ec->mask;
13998a978234SLiam Girdwood 		se->dobj.index = tplg->index;
14008a978234SLiam Girdwood 
14018a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
14028a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
14038a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14048a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
14058a978234SLiam Girdwood 			if (err < 0) {
14068a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
14078a978234SLiam Girdwood 					ec->hdr.name);
14088a978234SLiam Girdwood 				goto err_se;
14098a978234SLiam Girdwood 			}
14109c6c4d96STakashi Iwai 			/* fall through */
14118a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
14128a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14138a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14148a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
14158a978234SLiam Girdwood 			if (err < 0) {
14168a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
14178a978234SLiam Girdwood 					ec->hdr.name);
14188a978234SLiam Girdwood 				goto err_se;
14198a978234SLiam Girdwood 			}
14208a978234SLiam Girdwood 			break;
14218a978234SLiam Girdwood 		default:
14228a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
14238a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
14248a978234SLiam Girdwood 			goto err_se;
14258a978234SLiam Girdwood 		}
14268a978234SLiam Girdwood 
14278a978234SLiam Girdwood 		/* map io handlers */
14281a7dd6e2SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
14298a978234SLiam Girdwood 		if (err) {
14308a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
14318a978234SLiam Girdwood 			goto err_se;
14328a978234SLiam Girdwood 		}
14338a978234SLiam Girdwood 
14348a978234SLiam Girdwood 		/* pass control to driver for optional further init */
14351a7dd6e2SMengdong Lin 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
14368a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)ec);
14378a978234SLiam Girdwood 		if (err < 0) {
14388a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
14398a978234SLiam Girdwood 				ec->hdr.name);
14408a978234SLiam Girdwood 			goto err_se;
14418a978234SLiam Girdwood 		}
14428a978234SLiam Girdwood 
14431a7dd6e2SMengdong Lin 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
14441a7dd6e2SMengdong Lin 				ec->priv.size);
14451a7dd6e2SMengdong Lin 	}
14461a7dd6e2SMengdong Lin 
14478a978234SLiam Girdwood 	return kc;
14488a978234SLiam Girdwood 
14498a978234SLiam Girdwood err_se:
14501a7dd6e2SMengdong Lin 	for (; i >= 0; i--) {
14518a978234SLiam Girdwood 		/* free values and texts */
14521a7dd6e2SMengdong Lin 		se = (struct soc_enum *)kc[i].private_value;
14536d5574edSChristophe JAILLET 		if (!se)
14546d5574edSChristophe JAILLET 			continue;
14556d5574edSChristophe JAILLET 
14568a978234SLiam Girdwood 		kfree(se->dobj.control.dvalues);
14571a7dd6e2SMengdong Lin 		for (j = 0; j < ec->items; j++)
14581a7dd6e2SMengdong Lin 			kfree(se->dobj.control.dtexts[j]);
145934db6a3eSAmadeusz Sławiński 		kfree(se->dobj.control.dtexts);
14608a978234SLiam Girdwood 
14618a978234SLiam Girdwood 		kfree(se);
1462267e2c6fSLiam Girdwood 		kfree(kc[i].name);
14631a7dd6e2SMengdong Lin 	}
14648a978234SLiam Girdwood err:
14658a978234SLiam Girdwood 	kfree(kc);
14668a978234SLiam Girdwood 
14678a978234SLiam Girdwood 	return NULL;
14688a978234SLiam Girdwood }
14698a978234SLiam Girdwood 
14708a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
14718a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
14728a978234SLiam Girdwood {
14738a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
14748a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
14758a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
14768a978234SLiam Girdwood 	int i, err;
14778a978234SLiam Girdwood 
14784ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
14798a978234SLiam Girdwood 	if (!kc)
14808a978234SLiam Girdwood 		return NULL;
14818a978234SLiam Girdwood 
14828a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
14838a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
14848a978234SLiam Girdwood 
14858a978234SLiam Girdwood 		/* validate kcontrol */
14868a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
14878a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
14888a978234SLiam Girdwood 			goto err;
14898a978234SLiam Girdwood 
14908a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
14918a978234SLiam Girdwood 		if (sbe == NULL)
14928a978234SLiam Girdwood 			goto err;
14938a978234SLiam Girdwood 
14948a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
14958a978234SLiam Girdwood 			be->priv.size);
14968a978234SLiam Girdwood 
14978a978234SLiam Girdwood 		dev_dbg(tplg->dev,
14988a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
14998a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
15008a978234SLiam Girdwood 
1501267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
150265030ff3SDan Carpenter 		if (kc[i].name == NULL) {
150365030ff3SDan Carpenter 			kfree(sbe);
1504267e2c6fSLiam Girdwood 			goto err;
150565030ff3SDan Carpenter 		}
15068a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
15078a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
15088a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
15098a978234SLiam Girdwood 
15108a978234SLiam Girdwood 		sbe->max = be->max;
15118a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
15128a978234SLiam Girdwood 
15138a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
15142b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
15158a978234SLiam Girdwood 		if (err) {
15168a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
15178a978234SLiam Girdwood 			kfree(sbe);
15188a978234SLiam Girdwood 			continue;
15198a978234SLiam Girdwood 		}
15208a978234SLiam Girdwood 
15218a978234SLiam Girdwood 		/* pass control to driver for optional further init */
15228a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
15238a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
15248a978234SLiam Girdwood 		if (err < 0) {
15258a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
15268a978234SLiam Girdwood 				be->hdr.name);
15278a978234SLiam Girdwood 			kfree(sbe);
15288a978234SLiam Girdwood 			continue;
15298a978234SLiam Girdwood 		}
15308a978234SLiam Girdwood 	}
15318a978234SLiam Girdwood 
15328a978234SLiam Girdwood 	return kc;
15338a978234SLiam Girdwood 
15348a978234SLiam Girdwood err:
1535267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
15368a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1537267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1538267e2c6fSLiam Girdwood 	}
15398a978234SLiam Girdwood 
15408a978234SLiam Girdwood 	kfree(kc);
15418a978234SLiam Girdwood 	return NULL;
15428a978234SLiam Girdwood }
15438a978234SLiam Girdwood 
15448a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
15458a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
15468a978234SLiam Girdwood {
15478a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
15488a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
15498a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
15508a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1551eea3dd4fSMengdong Lin 	unsigned int kcontrol_type;
15528a978234SLiam Girdwood 	int ret = 0;
15538a978234SLiam Girdwood 
15548a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15558a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15568a978234SLiam Girdwood 		return -EINVAL;
15578a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15588a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15598a978234SLiam Girdwood 		return -EINVAL;
15608a978234SLiam Girdwood 
15618a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
15628a978234SLiam Girdwood 		w->name, w->id);
15638a978234SLiam Girdwood 
15648a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
15658a978234SLiam Girdwood 
15668a978234SLiam Girdwood 	/* map user to kernel widget ID */
15678a978234SLiam Girdwood 	template.id = get_widget_id(w->id);
15688a978234SLiam Girdwood 	if (template.id < 0)
15698a978234SLiam Girdwood 		return template.id;
15708a978234SLiam Girdwood 
1571c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
15728a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
15738a978234SLiam Girdwood 	if (!template.name)
15748a978234SLiam Girdwood 		return -ENOMEM;
15758a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
15768a978234SLiam Girdwood 	if (!template.sname) {
15778a978234SLiam Girdwood 		ret = -ENOMEM;
15788a978234SLiam Girdwood 		goto err;
15798a978234SLiam Girdwood 	}
15808a978234SLiam Girdwood 	template.reg = w->reg;
15818a978234SLiam Girdwood 	template.shift = w->shift;
15828a978234SLiam Girdwood 	template.mask = w->mask;
15836dc6db79SSubhransu S. Prusty 	template.subseq = w->subseq;
15848a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
15858a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
15868a978234SLiam Girdwood 	template.ignore_suspend = w->ignore_suspend;
15878a978234SLiam Girdwood 	template.event_flags = w->event_flags;
15888a978234SLiam Girdwood 	template.dobj.index = tplg->index;
15898a978234SLiam Girdwood 
15908a978234SLiam Girdwood 	tplg->pos +=
15918a978234SLiam Girdwood 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
15928a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
1593dd5abb74SArnd Bergmann 		kcontrol_type = 0;
15948a978234SLiam Girdwood 		template.num_kcontrols = 0;
15958a978234SLiam Girdwood 		goto widget;
15968a978234SLiam Girdwood 	}
15978a978234SLiam Girdwood 
15988a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
15998a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
16008a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
16018a978234SLiam Girdwood 
16028a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
16038a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
16048a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
16058a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
16068a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
16078a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
16088a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1609eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
16108a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
16118a978234SLiam Girdwood 		template.kcontrol_news =
16128a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
16138a978234SLiam Girdwood 			template.num_kcontrols);
16148a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16158a978234SLiam Girdwood 			ret = -ENOMEM;
16168a978234SLiam Girdwood 			goto hdr_err;
16178a978234SLiam Girdwood 		}
16188a978234SLiam Girdwood 		break;
16198a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
16208a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
16218a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
16228a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
16238a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1624eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
16251a7dd6e2SMengdong Lin 		template.num_kcontrols = w->num_kcontrols;
16268a978234SLiam Girdwood 		template.kcontrol_news =
16271a7dd6e2SMengdong Lin 			soc_tplg_dapm_widget_denum_create(tplg,
16281a7dd6e2SMengdong Lin 			template.num_kcontrols);
16298a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16308a978234SLiam Girdwood 			ret = -ENOMEM;
16318a978234SLiam Girdwood 			goto hdr_err;
16328a978234SLiam Girdwood 		}
16338a978234SLiam Girdwood 		break;
16348a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
1635eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
16368a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
16378a978234SLiam Girdwood 		template.kcontrol_news =
16388a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
16398a978234SLiam Girdwood 				template.num_kcontrols);
16408a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16418a978234SLiam Girdwood 			ret = -ENOMEM;
16428a978234SLiam Girdwood 			goto hdr_err;
16438a978234SLiam Girdwood 		}
16448a978234SLiam Girdwood 		break;
16458a978234SLiam Girdwood 	default:
16468a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
16478a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
16488a978234SLiam Girdwood 			control_hdr->ops.info);
16498a978234SLiam Girdwood 		ret = -EINVAL;
16508a978234SLiam Girdwood 		goto hdr_err;
16518a978234SLiam Girdwood 	}
16528a978234SLiam Girdwood 
16538a978234SLiam Girdwood widget:
16548a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
16558a978234SLiam Girdwood 	if (ret < 0)
16568a978234SLiam Girdwood 		goto hdr_err;
16578a978234SLiam Girdwood 
16588a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
16598a978234SLiam Girdwood 	 * data during sound card init. */
16608a978234SLiam Girdwood 	if (card->instantiated)
16618a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
16628a978234SLiam Girdwood 	else
16638a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
166437e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
166537e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
16668a978234SLiam Girdwood 		goto hdr_err;
16678a978234SLiam Girdwood 	}
16688a978234SLiam Girdwood 
16698a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1670eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
16718a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
16728a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
16738a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1674ebd259d3SLiam Girdwood 
1675ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1676ebd259d3SLiam Girdwood 	if (ret < 0)
1677ebd259d3SLiam Girdwood 		goto ready_err;
1678ebd259d3SLiam Girdwood 
16797620fe91SBard liao 	kfree(template.sname);
16807620fe91SBard liao 	kfree(template.name);
16817620fe91SBard liao 
16828a978234SLiam Girdwood 	return 0;
16838a978234SLiam Girdwood 
1684ebd259d3SLiam Girdwood ready_err:
1685ebd259d3SLiam Girdwood 	snd_soc_tplg_widget_remove(widget);
1686ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
16878a978234SLiam Girdwood hdr_err:
16888a978234SLiam Girdwood 	kfree(template.sname);
16898a978234SLiam Girdwood err:
16908a978234SLiam Girdwood 	kfree(template.name);
16918a978234SLiam Girdwood 	return ret;
16928a978234SLiam Girdwood }
16938a978234SLiam Girdwood 
16948a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
16958a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
16968a978234SLiam Girdwood {
16978a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
16988a978234SLiam Girdwood 	int ret, count = hdr->count, i;
16998a978234SLiam Girdwood 
17008a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
17018a978234SLiam Girdwood 		return 0;
17028a978234SLiam Girdwood 
17038a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
17048a978234SLiam Girdwood 
17058a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
17068a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
170706eb49f7SMengdong Lin 		if (widget->size != sizeof(*widget)) {
170806eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
170906eb49f7SMengdong Lin 			return -EINVAL;
171006eb49f7SMengdong Lin 		}
171106eb49f7SMengdong Lin 
17128a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
17137de76b62SMengdong Lin 		if (ret < 0) {
17148a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
17158a978234SLiam Girdwood 				widget->name);
17167de76b62SMengdong Lin 			return ret;
17177de76b62SMengdong Lin 		}
17188a978234SLiam Girdwood 	}
17198a978234SLiam Girdwood 
17208a978234SLiam Girdwood 	return 0;
17218a978234SLiam Girdwood }
17228a978234SLiam Girdwood 
17238a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
17248a978234SLiam Girdwood {
17258a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
17268a978234SLiam Girdwood 	int ret;
17278a978234SLiam Girdwood 
17288a978234SLiam Girdwood 	/* Card might not have been registered at this point.
17298a978234SLiam Girdwood 	 * If so, just return success.
17308a978234SLiam Girdwood 	*/
17318a978234SLiam Girdwood 	if (!card || !card->instantiated) {
17328a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1733cc9d4714SLiam Girdwood 			" widget card binding deferred\n");
17348a978234SLiam Girdwood 		return 0;
17358a978234SLiam Girdwood 	}
17368a978234SLiam Girdwood 
17378a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
17388a978234SLiam Girdwood 	if (ret < 0)
17398a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
17408a978234SLiam Girdwood 			ret);
17418a978234SLiam Girdwood 
17428a978234SLiam Girdwood 	return 0;
17438a978234SLiam Girdwood }
17448a978234SLiam Girdwood 
1745b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream,
1746b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1747b6b6e4d6SMengdong Lin {
1748b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1749b6b6e4d6SMengdong Lin 	stream->channels_min = caps->channels_min;
1750b6b6e4d6SMengdong Lin 	stream->channels_max = caps->channels_max;
1751b6b6e4d6SMengdong Lin 	stream->rates = caps->rates;
1752b6b6e4d6SMengdong Lin 	stream->rate_min = caps->rate_min;
1753b6b6e4d6SMengdong Lin 	stream->rate_max = caps->rate_max;
1754b6b6e4d6SMengdong Lin 	stream->formats = caps->formats;
1755f918e169SMengdong Lin 	stream->sig_bits = caps->sig_bits;
1756b6b6e4d6SMengdong Lin }
1757b6b6e4d6SMengdong Lin 
17580038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
17590038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
17600038be9aSMengdong Lin {
17610038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
17620038be9aSMengdong Lin 		dai_drv->symmetric_rates =
17630038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
17640038be9aSMengdong Lin 
17650038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
17660038be9aSMengdong Lin 		dai_drv->symmetric_channels =
17670038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
17680038be9aSMengdong Lin 			1 : 0;
17690038be9aSMengdong Lin 
17700038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
17710038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
17720038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
17730038be9aSMengdong Lin 			1 : 0;
17740038be9aSMengdong Lin }
17750038be9aSMengdong Lin 
177664527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
177764527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
177864527e8aSMengdong Lin {
177964527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
178064527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
178164527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
178264527e8aSMengdong Lin 	int ret;
178364527e8aSMengdong Lin 
178464527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
178564527e8aSMengdong Lin 	if (dai_drv == NULL)
178664527e8aSMengdong Lin 		return -ENOMEM;
178764527e8aSMengdong Lin 
17888f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
17898f27c4abSMengdong Lin 		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
179064527e8aSMengdong Lin 	dai_drv->id = pcm->dai_id;
179164527e8aSMengdong Lin 
179264527e8aSMengdong Lin 	if (pcm->playback) {
179364527e8aSMengdong Lin 		stream = &dai_drv->playback;
179464527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1795b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
179664527e8aSMengdong Lin 	}
179764527e8aSMengdong Lin 
179864527e8aSMengdong Lin 	if (pcm->capture) {
179964527e8aSMengdong Lin 		stream = &dai_drv->capture;
180064527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1801b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
180264527e8aSMengdong Lin 	}
180364527e8aSMengdong Lin 
18045db6aab6SLiam Girdwood 	if (pcm->compress)
18055db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
18065db6aab6SLiam Girdwood 
180764527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1808c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
180964527e8aSMengdong Lin 	if (ret < 0) {
181064527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
181164527e8aSMengdong Lin 		kfree(dai_drv);
181264527e8aSMengdong Lin 		return ret;
181364527e8aSMengdong Lin 	}
181464527e8aSMengdong Lin 
181564527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
181664527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
181764527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
181864527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
181964527e8aSMengdong Lin 
182064527e8aSMengdong Lin 	/* register the DAI to the component */
182164527e8aSMengdong Lin 	return snd_soc_register_dai(tplg->comp, dai_drv);
182264527e8aSMengdong Lin }
182364527e8aSMengdong Lin 
1824717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1825717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1826717a8e72SMengdong Lin {
1827717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1828717a8e72SMengdong Lin 		link->symmetric_rates =
1829717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1830717a8e72SMengdong Lin 
1831717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1832717a8e72SMengdong Lin 		link->symmetric_channels =
1833717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1834717a8e72SMengdong Lin 			1 : 0;
1835717a8e72SMengdong Lin 
1836717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1837717a8e72SMengdong Lin 		link->symmetric_samplebits =
1838717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1839717a8e72SMengdong Lin 			1 : 0;
18406ff67ccaSMengdong Lin 
18416ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
18426ff67ccaSMengdong Lin 		link->ignore_suspend =
18436ff67ccaSMengdong Lin 		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
18446ff67ccaSMengdong Lin 		1 : 0;
1845717a8e72SMengdong Lin }
1846717a8e72SMengdong Lin 
184767d1c21eSGuneshwor Singh /* create the FE DAI link */
1848ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1849acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1850acfc7d46SMengdong Lin {
1851acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
1852acfc7d46SMengdong Lin 	int ret;
1853acfc7d46SMengdong Lin 
1854acfc7d46SMengdong Lin 	link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1855acfc7d46SMengdong Lin 	if (link == NULL)
1856acfc7d46SMengdong Lin 		return -ENOMEM;
1857acfc7d46SMengdong Lin 
18588f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
18598f27c4abSMengdong Lin 		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
18608f27c4abSMengdong Lin 		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
18618f27c4abSMengdong Lin 	}
1862b84fff5aSMengdong Lin 	link->id = pcm->pcm_id;
1863acfc7d46SMengdong Lin 
18648f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
18658f27c4abSMengdong Lin 		link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
18668f27c4abSMengdong Lin 
186767d1c21eSGuneshwor Singh 	link->codec_name = "snd-soc-dummy";
186867d1c21eSGuneshwor Singh 	link->codec_dai_name = "snd-soc-dummy-dai";
186967d1c21eSGuneshwor Singh 
187067d1c21eSGuneshwor Singh 	/* enable DPCM */
187167d1c21eSGuneshwor Singh 	link->dynamic = 1;
187267d1c21eSGuneshwor Singh 	link->dpcm_playback = pcm->playback;
187367d1c21eSGuneshwor Singh 	link->dpcm_capture = pcm->capture;
1874717a8e72SMengdong Lin 	if (pcm->flag_mask)
1875717a8e72SMengdong Lin 		set_link_flags(link, pcm->flag_mask, pcm->flags);
187667d1c21eSGuneshwor Singh 
1877acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1878c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1879acfc7d46SMengdong Lin 	if (ret < 0) {
1880acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1881acfc7d46SMengdong Lin 		kfree(link);
1882acfc7d46SMengdong Lin 		return ret;
1883acfc7d46SMengdong Lin 	}
1884acfc7d46SMengdong Lin 
1885acfc7d46SMengdong Lin 	link->dobj.index = tplg->index;
1886acfc7d46SMengdong Lin 	link->dobj.ops = tplg->ops;
1887acfc7d46SMengdong Lin 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1888acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1889acfc7d46SMengdong Lin 
1890acfc7d46SMengdong Lin 	snd_soc_add_dai_link(tplg->comp->card, link);
1891acfc7d46SMengdong Lin 	return 0;
1892acfc7d46SMengdong Lin }
1893acfc7d46SMengdong Lin 
1894acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
189564527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
189664527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
189764527e8aSMengdong Lin {
1898acfc7d46SMengdong Lin 	int ret;
1899acfc7d46SMengdong Lin 
1900acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1901acfc7d46SMengdong Lin 	if (ret < 0)
1902acfc7d46SMengdong Lin 		return ret;
1903acfc7d46SMengdong Lin 
1904ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
190564527e8aSMengdong Lin }
190664527e8aSMengdong Lin 
190755726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
190855726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
190955726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
191055726dc9SMengdong Lin {
191155726dc9SMengdong Lin 	dest->size = sizeof(*dest);
191255726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
191355726dc9SMengdong Lin 	dest->formats = src->formats;
191455726dc9SMengdong Lin 	dest->rates = src->rates;
191555726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
191655726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
191755726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
191855726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
191955726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
192055726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
192155726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
192255726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
192355726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
192455726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
192555726dc9SMengdong Lin }
192655726dc9SMengdong Lin 
192755726dc9SMengdong Lin /**
192855726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
192955726dc9SMengdong Lin  * @tplg: topology context
193055726dc9SMengdong Lin  * @src: older version of pcm as a source
193155726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
193255726dc9SMengdong Lin  *
193355726dc9SMengdong Lin  * Support from vesion 4. User should free the returned pcm manually.
193455726dc9SMengdong Lin  */
193555726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
193655726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
193755726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
193855726dc9SMengdong Lin {
193955726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
194055726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
194155726dc9SMengdong Lin 	int i;
194255726dc9SMengdong Lin 
194355726dc9SMengdong Lin 	*pcm = NULL;
194455726dc9SMengdong Lin 
194555726dc9SMengdong Lin 	if (src->size != sizeof(*src_v4)) {
194655726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
194755726dc9SMengdong Lin 		return -EINVAL;
194855726dc9SMengdong Lin 	}
194955726dc9SMengdong Lin 
195055726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
195155726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
195255726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
195355726dc9SMengdong Lin 	if (!dest)
195455726dc9SMengdong Lin 		return -ENOMEM;
195555726dc9SMengdong Lin 
195655726dc9SMengdong Lin 	dest->size = sizeof(*dest);	/* size of latest abi version */
195755726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
195855726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
195955726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
196055726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
196155726dc9SMengdong Lin 	dest->playback = src_v4->playback;
196255726dc9SMengdong Lin 	dest->capture = src_v4->capture;
196355726dc9SMengdong Lin 	dest->compress = src_v4->compress;
196455726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
196555726dc9SMengdong Lin 	for (i = 0; i < dest->num_streams; i++)
196655726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
196755726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
196855726dc9SMengdong Lin 
196955726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
197055726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
197155726dc9SMengdong Lin 
197255726dc9SMengdong Lin 	*pcm = dest;
197355726dc9SMengdong Lin 	return 0;
197455726dc9SMengdong Lin }
197555726dc9SMengdong Lin 
197664527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
19778a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
19788a978234SLiam Girdwood {
197955726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
19808a978234SLiam Girdwood 	int count = hdr->count;
1981fd340455SVinod Koul 	int i;
198255726dc9SMengdong Lin 	bool abi_match;
19838a978234SLiam Girdwood 
19848a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
19858a978234SLiam Girdwood 		return 0;
19868a978234SLiam Girdwood 
198755726dc9SMengdong Lin 	/* check the element size and count */
198855726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
198955726dc9SMengdong Lin 	if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
199055726dc9SMengdong Lin 		|| pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
199155726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
199255726dc9SMengdong Lin 			pcm->size);
199355726dc9SMengdong Lin 		return -EINVAL;
199455726dc9SMengdong Lin 	}
199555726dc9SMengdong Lin 
19968a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
199755726dc9SMengdong Lin 		pcm->size, count,
19988a978234SLiam Girdwood 		hdr->payload_size, "PCM DAI")) {
19998a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
20008a978234SLiam Girdwood 			count);
20018a978234SLiam Girdwood 		return -EINVAL;
20028a978234SLiam Girdwood 	}
20038a978234SLiam Girdwood 
200464527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
200555726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
200655726dc9SMengdong Lin 
200755726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
200855726dc9SMengdong Lin 		 * if abi not match.
200955726dc9SMengdong Lin 		 */
201055726dc9SMengdong Lin 		if (pcm->size == sizeof(*pcm)) {
201155726dc9SMengdong Lin 			abi_match = true;
201255726dc9SMengdong Lin 			_pcm = pcm;
201355726dc9SMengdong Lin 		} else {
201455726dc9SMengdong Lin 			abi_match = false;
2015fd340455SVinod Koul 			pcm_new_ver(tplg, pcm, &_pcm);
201606eb49f7SMengdong Lin 		}
201706eb49f7SMengdong Lin 
201855726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
201955726dc9SMengdong Lin 		soc_tplg_pcm_create(tplg, _pcm);
202055726dc9SMengdong Lin 
2021717a8e72SMengdong Lin 		/* offset by version-specific struct size and
2022717a8e72SMengdong Lin 		 * real priv data size
2023717a8e72SMengdong Lin 		 */
2024717a8e72SMengdong Lin 		tplg->pos += pcm->size + _pcm->priv.size;
2025717a8e72SMengdong Lin 
202655726dc9SMengdong Lin 		if (!abi_match)
202755726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
202864527e8aSMengdong Lin 	}
202964527e8aSMengdong Lin 
20308a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
20318a978234SLiam Girdwood 
20328a978234SLiam Girdwood 	return 0;
20338a978234SLiam Girdwood }
20348a978234SLiam Girdwood 
20350038be9aSMengdong Lin /**
2036593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
20378abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
2038593d9e52SMengdong Lin  * @cfg: physical link configs.
2039593d9e52SMengdong Lin  *
2040593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
2041593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
2042593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
2043593d9e52SMengdong Lin  */
2044593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
2045593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
2046593d9e52SMengdong Lin {
2047593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
2048593d9e52SMengdong Lin 	unsigned char bclk_master, fsync_master;
2049593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
2050593d9e52SMengdong Lin 	int i;
2051593d9e52SMengdong Lin 
2052593d9e52SMengdong Lin 	for (i = 0; i < cfg->num_hw_configs; i++) {
2053593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
2054593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
2055593d9e52SMengdong Lin 			continue;
2056593d9e52SMengdong Lin 
2057593d9e52SMengdong Lin 		link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2058593d9e52SMengdong Lin 
2059933e1c4aSKirill Marinushkin 		/* clock gating */
2060fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
2061fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2062933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2063fbeabd09SKirill Marinushkin 			break;
2064fbeabd09SKirill Marinushkin 
2065fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2066933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2067fbeabd09SKirill Marinushkin 			break;
2068fbeabd09SKirill Marinushkin 
2069fbeabd09SKirill Marinushkin 		default:
2070fbeabd09SKirill Marinushkin 			/* ignore the value */
2071fbeabd09SKirill Marinushkin 			break;
2072fbeabd09SKirill Marinushkin 		}
2073933e1c4aSKirill Marinushkin 
2074593d9e52SMengdong Lin 		/* clock signal polarity */
2075593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
2076593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
2077593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
2078593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2079593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
2080593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2081593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
2082593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2083593d9e52SMengdong Lin 		else
2084593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2085593d9e52SMengdong Lin 
2086593d9e52SMengdong Lin 		/* clock masters */
2087a941e2faSKirill Marinushkin 		bclk_master = (hw_config->bclk_master ==
2088a941e2faSKirill Marinushkin 			       SND_SOC_TPLG_BCLK_CM);
2089a941e2faSKirill Marinushkin 		fsync_master = (hw_config->fsync_master ==
2090a941e2faSKirill Marinushkin 				SND_SOC_TPLG_FSYNC_CM);
2091a941e2faSKirill Marinushkin 		if (bclk_master && fsync_master)
2092593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2093593d9e52SMengdong Lin 		else if (!bclk_master && fsync_master)
2094a941e2faSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2095a941e2faSKirill Marinushkin 		else if (bclk_master && !fsync_master)
2096593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2097593d9e52SMengdong Lin 		else
2098593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2099593d9e52SMengdong Lin 	}
2100593d9e52SMengdong Lin }
2101593d9e52SMengdong Lin 
2102593d9e52SMengdong Lin /**
2103593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
2104593d9e52SMengdong Lin  * version of source.
21058abab35fSCharles Keepax  * @tplg: topology context
2106593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
2107593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
2108593d9e52SMengdong Lin  *
2109593d9e52SMengdong Lin  * Support from vesion 4. User need free the returned link config manually.
2110593d9e52SMengdong Lin  */
2111593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
2112593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
2113593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
2114593d9e52SMengdong Lin {
2115593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
2116593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
2117593d9e52SMengdong Lin 	int i;
2118593d9e52SMengdong Lin 
2119593d9e52SMengdong Lin 	*link = NULL;
2120593d9e52SMengdong Lin 
2121593d9e52SMengdong Lin 	if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2122593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2123593d9e52SMengdong Lin 		return -EINVAL;
2124593d9e52SMengdong Lin 	}
2125593d9e52SMengdong Lin 
2126593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2127593d9e52SMengdong Lin 
2128593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2129593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2130593d9e52SMengdong Lin 	if (!dest)
2131593d9e52SMengdong Lin 		return -ENOMEM;
2132593d9e52SMengdong Lin 
2133593d9e52SMengdong Lin 	dest->size = sizeof(*dest);
2134593d9e52SMengdong Lin 	dest->id = src_v4->id;
2135593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
2136593d9e52SMengdong Lin 	for (i = 0; i < dest->num_streams; i++)
2137593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2138593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2139593d9e52SMengdong Lin 
2140593d9e52SMengdong Lin 	*link = dest;
2141593d9e52SMengdong Lin 	return 0;
2142593d9e52SMengdong Lin }
2143593d9e52SMengdong Lin 
2144593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2145593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2146593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2147593d9e52SMengdong Lin {
2148593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2149593d9e52SMengdong Lin 	const char *name, *stream_name;
2150dbab1cb8SMengdong Lin 	size_t len;
2151593d9e52SMengdong Lin 	int ret;
2152593d9e52SMengdong Lin 
2153dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2154dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2155dbab1cb8SMengdong Lin 		return -EINVAL;
2156dbab1cb8SMengdong Lin 	else if (len)
2157dbab1cb8SMengdong Lin 		name = cfg->name;
2158dbab1cb8SMengdong Lin 	else
2159dbab1cb8SMengdong Lin 		name = NULL;
2160dbab1cb8SMengdong Lin 
2161dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2162dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2163dbab1cb8SMengdong Lin 		return -EINVAL;
2164dbab1cb8SMengdong Lin 	else if (len)
2165dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2166dbab1cb8SMengdong Lin 	else
2167dbab1cb8SMengdong Lin 		stream_name = NULL;
2168593d9e52SMengdong Lin 
2169593d9e52SMengdong Lin 	link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2170593d9e52SMengdong Lin 				     name, stream_name);
2171593d9e52SMengdong Lin 	if (!link) {
2172593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2173593d9e52SMengdong Lin 			name, cfg->id);
2174593d9e52SMengdong Lin 		return -EINVAL;
2175593d9e52SMengdong Lin 	}
2176593d9e52SMengdong Lin 
2177593d9e52SMengdong Lin 	/* hw format */
2178593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2179593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2180593d9e52SMengdong Lin 
2181593d9e52SMengdong Lin 	/* flags */
2182593d9e52SMengdong Lin 	if (cfg->flag_mask)
2183593d9e52SMengdong Lin 		set_link_flags(link, cfg->flag_mask, cfg->flags);
2184593d9e52SMengdong Lin 
2185593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2186c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2187593d9e52SMengdong Lin 	if (ret < 0) {
2188593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2189593d9e52SMengdong Lin 		return ret;
2190593d9e52SMengdong Lin 	}
2191593d9e52SMengdong Lin 
2192adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2193adfebb51SBard liao 	link->dobj.index = tplg->index;
2194adfebb51SBard liao 	link->dobj.ops = tplg->ops;
2195adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2196adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2197adfebb51SBard liao 
2198593d9e52SMengdong Lin 	return 0;
2199593d9e52SMengdong Lin }
2200593d9e52SMengdong Lin 
2201593d9e52SMengdong Lin 
2202593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2203593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2204593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2205593d9e52SMengdong Lin {
2206593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
2207593d9e52SMengdong Lin 	int count = hdr->count;
2208593d9e52SMengdong Lin 	int i, ret;
2209593d9e52SMengdong Lin 	bool abi_match;
2210593d9e52SMengdong Lin 
2211593d9e52SMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_LINK) {
2212593d9e52SMengdong Lin 		tplg->pos += hdr->size + hdr->payload_size;
2213593d9e52SMengdong Lin 		return 0;
2214593d9e52SMengdong Lin 	};
2215593d9e52SMengdong Lin 
2216593d9e52SMengdong Lin 	/* check the element size and count */
2217593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
2218593d9e52SMengdong Lin 	if (link->size > sizeof(struct snd_soc_tplg_link_config)
2219593d9e52SMengdong Lin 		|| link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2220593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2221593d9e52SMengdong Lin 			link->size);
2222593d9e52SMengdong Lin 		return -EINVAL;
2223593d9e52SMengdong Lin 	}
2224593d9e52SMengdong Lin 
2225593d9e52SMengdong Lin 	if (soc_tplg_check_elem_count(tplg,
2226593d9e52SMengdong Lin 		link->size, count,
2227593d9e52SMengdong Lin 		hdr->payload_size, "physical link config")) {
2228593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2229593d9e52SMengdong Lin 			count);
2230593d9e52SMengdong Lin 		return -EINVAL;
2231593d9e52SMengdong Lin 	}
2232593d9e52SMengdong Lin 
2233593d9e52SMengdong Lin 	/* config physical DAI links */
2234593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2235593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
2236593d9e52SMengdong Lin 		if (link->size == sizeof(*link)) {
2237593d9e52SMengdong Lin 			abi_match = true;
2238593d9e52SMengdong Lin 			_link = link;
2239593d9e52SMengdong Lin 		} else {
2240593d9e52SMengdong Lin 			abi_match = false;
2241593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2242593d9e52SMengdong Lin 			if (ret < 0)
2243593d9e52SMengdong Lin 				return ret;
2244593d9e52SMengdong Lin 		}
2245593d9e52SMengdong Lin 
2246593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
2247593d9e52SMengdong Lin 		if (ret < 0)
2248593d9e52SMengdong Lin 			return ret;
2249593d9e52SMengdong Lin 
2250593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2251593d9e52SMengdong Lin 		 * real priv data size
2252593d9e52SMengdong Lin 		 */
2253593d9e52SMengdong Lin 		tplg->pos += link->size + _link->priv.size;
2254593d9e52SMengdong Lin 
2255593d9e52SMengdong Lin 		if (!abi_match)
2256593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2257593d9e52SMengdong Lin 	}
2258593d9e52SMengdong Lin 
2259593d9e52SMengdong Lin 	return 0;
2260593d9e52SMengdong Lin }
2261593d9e52SMengdong Lin 
2262593d9e52SMengdong Lin /**
22639aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
22640038be9aSMengdong Lin  * @tplg: topology context
22659aa3f034SMengdong Lin  * @d: physical DAI configs.
22660038be9aSMengdong Lin  *
22679aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
22689aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
22690038be9aSMengdong Lin  */
22709aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
22719aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
22720038be9aSMengdong Lin {
22730038be9aSMengdong Lin 	struct snd_soc_dai_link_component dai_component = {0};
22740038be9aSMengdong Lin 	struct snd_soc_dai *dai;
22750038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
22760038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
22770038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
22780038be9aSMengdong Lin 	int ret;
22790038be9aSMengdong Lin 
22809aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
22810038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
22820038be9aSMengdong Lin 	if (!dai) {
22839aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
22849aa3f034SMengdong Lin 			d->dai_name);
22850038be9aSMengdong Lin 		return -EINVAL;
22860038be9aSMengdong Lin 	}
22870038be9aSMengdong Lin 
22889aa3f034SMengdong Lin 	if (d->dai_id != dai->id) {
22899aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
22909aa3f034SMengdong Lin 			d->dai_name);
22910038be9aSMengdong Lin 		return -EINVAL;
22920038be9aSMengdong Lin 	}
22930038be9aSMengdong Lin 
22940038be9aSMengdong Lin 	dai_drv = dai->driver;
22950038be9aSMengdong Lin 	if (!dai_drv)
22960038be9aSMengdong Lin 		return -EINVAL;
22970038be9aSMengdong Lin 
22989aa3f034SMengdong Lin 	if (d->playback) {
22990038be9aSMengdong Lin 		stream = &dai_drv->playback;
23009aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
23010038be9aSMengdong Lin 		set_stream_info(stream, caps);
23020038be9aSMengdong Lin 	}
23030038be9aSMengdong Lin 
23049aa3f034SMengdong Lin 	if (d->capture) {
23050038be9aSMengdong Lin 		stream = &dai_drv->capture;
23069aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
23070038be9aSMengdong Lin 		set_stream_info(stream, caps);
23080038be9aSMengdong Lin 	}
23090038be9aSMengdong Lin 
23109aa3f034SMengdong Lin 	if (d->flag_mask)
23119aa3f034SMengdong Lin 		set_dai_flags(dai_drv, d->flag_mask, d->flags);
23120038be9aSMengdong Lin 
23130038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2314c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
23150038be9aSMengdong Lin 	if (ret < 0) {
23160038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
23170038be9aSMengdong Lin 		return ret;
23180038be9aSMengdong Lin 	}
23190038be9aSMengdong Lin 
23200038be9aSMengdong Lin 	return 0;
23210038be9aSMengdong Lin }
23220038be9aSMengdong Lin 
23239aa3f034SMengdong Lin /* load physical DAI elements */
23249aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
23250038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
23260038be9aSMengdong Lin {
23279aa3f034SMengdong Lin 	struct snd_soc_tplg_dai *dai;
23280038be9aSMengdong Lin 	int count = hdr->count;
23290038be9aSMengdong Lin 	int i;
23300038be9aSMengdong Lin 
23310038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
23320038be9aSMengdong Lin 		return 0;
23330038be9aSMengdong Lin 
23340038be9aSMengdong Lin 	/* config the existing BE DAIs */
23350038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
23369aa3f034SMengdong Lin 		dai = (struct snd_soc_tplg_dai *)tplg->pos;
23379aa3f034SMengdong Lin 		if (dai->size != sizeof(*dai)) {
23389aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
23390038be9aSMengdong Lin 			return -EINVAL;
23400038be9aSMengdong Lin 		}
23410038be9aSMengdong Lin 
23429aa3f034SMengdong Lin 		soc_tplg_dai_config(tplg, dai);
23439aa3f034SMengdong Lin 		tplg->pos += (sizeof(*dai) + dai->priv.size);
23440038be9aSMengdong Lin 	}
23450038be9aSMengdong Lin 
23460038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
23470038be9aSMengdong Lin 	return 0;
23480038be9aSMengdong Lin }
23490038be9aSMengdong Lin 
2350583958faSMengdong Lin /**
2351583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2352583958faSMengdong Lin  * of source.
23538abab35fSCharles Keepax  * @tplg: topology context
2354583958faSMengdong Lin  * @src: old version of manifest as a source
2355583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2356583958faSMengdong Lin  *
2357583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
2358583958faSMengdong Lin  */
2359583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2360583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2361583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2362583958faSMengdong Lin {
2363583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2364583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
2365583958faSMengdong Lin 
2366583958faSMengdong Lin 	*manifest = NULL;
2367583958faSMengdong Lin 
2368583958faSMengdong Lin 	if (src->size != sizeof(*src_v4)) {
2369ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2370ac9391daSGuenter Roeck 			 src->size);
2371ac9391daSGuenter Roeck 		if (src->size)
2372583958faSMengdong Lin 			return -EINVAL;
2373ac9391daSGuenter Roeck 		src->size = sizeof(*src_v4);
2374583958faSMengdong Lin 	}
2375583958faSMengdong Lin 
2376583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2377583958faSMengdong Lin 
2378583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2379583958faSMengdong Lin 	dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2380583958faSMengdong Lin 	if (!dest)
2381583958faSMengdong Lin 		return -ENOMEM;
2382583958faSMengdong Lin 
2383583958faSMengdong Lin 	dest->size = sizeof(*dest);	/* size of latest abi version */
2384583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2385583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2386583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2387583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2388583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2389583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2390583958faSMengdong Lin 	if (dest->priv.size)
2391583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
2392583958faSMengdong Lin 		       src_v4->priv.size);
2393583958faSMengdong Lin 
2394583958faSMengdong Lin 	*manifest = dest;
2395583958faSMengdong Lin 	return 0;
2396583958faSMengdong Lin }
23970038be9aSMengdong Lin 
23988a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
23998a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
24008a978234SLiam Girdwood {
2401583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2402583958faSMengdong Lin 	bool abi_match;
2403583958faSMengdong Lin 	int err;
24048a978234SLiam Girdwood 
24058a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
24068a978234SLiam Girdwood 		return 0;
24078a978234SLiam Girdwood 
24088a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2409583958faSMengdong Lin 
2410583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
2411583958faSMengdong Lin 	if (manifest->size == sizeof(*manifest)) {
2412583958faSMengdong Lin 		abi_match = true;
2413583958faSMengdong Lin 		_manifest = manifest;
2414583958faSMengdong Lin 	} else {
2415583958faSMengdong Lin 		abi_match = false;
2416583958faSMengdong Lin 		err = manifest_new_ver(tplg, manifest, &_manifest);
2417583958faSMengdong Lin 		if (err < 0)
2418583958faSMengdong Lin 			return err;
241906eb49f7SMengdong Lin 	}
242006eb49f7SMengdong Lin 
2421583958faSMengdong Lin 	/* pass control to component driver for optional further init */
24228a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
2423c60b613aSLiam Girdwood 		return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
24248a978234SLiam Girdwood 
2425583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2426583958faSMengdong Lin 		kfree(_manifest);
2427583958faSMengdong Lin 
24288a978234SLiam Girdwood 	return 0;
24298a978234SLiam Girdwood }
24308a978234SLiam Girdwood 
24318a978234SLiam Girdwood /* validate header magic, size and type */
24328a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
24338a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24348a978234SLiam Girdwood {
24358a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
24368a978234SLiam Girdwood 		return 0;
24378a978234SLiam Girdwood 
243806eb49f7SMengdong Lin 	if (hdr->size != sizeof(*hdr)) {
243906eb49f7SMengdong Lin 		dev_err(tplg->dev,
244006eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
244106eb49f7SMengdong Lin 			hdr->type, soc_tplg_get_hdr_offset(tplg),
244206eb49f7SMengdong Lin 			tplg->fw->size);
244306eb49f7SMengdong Lin 		return -EINVAL;
244406eb49f7SMengdong Lin 	}
244506eb49f7SMengdong Lin 
24468a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
24478a978234SLiam Girdwood 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
24488a978234SLiam Girdwood 		dev_err(tplg->dev,
24498a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
24508a978234SLiam Girdwood 			tplg->pass, hdr->magic,
24518a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
24528a978234SLiam Girdwood 		return -EINVAL;
24538a978234SLiam Girdwood 	}
24548a978234SLiam Girdwood 
24558a978234SLiam Girdwood 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
24568a978234SLiam Girdwood 		dev_err(tplg->dev,
24578a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
24588a978234SLiam Girdwood 			tplg->pass, hdr->magic,
24598a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
24608a978234SLiam Girdwood 		return -EINVAL;
24618a978234SLiam Girdwood 	}
24628a978234SLiam Girdwood 
2463288b8da7SMengdong Lin 	/* Support ABI from version 4 */
2464288b8da7SMengdong Lin 	if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2465288b8da7SMengdong Lin 		|| hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
24668a978234SLiam Girdwood 		dev_err(tplg->dev,
24678a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
24688a978234SLiam Girdwood 			tplg->pass, hdr->abi,
24698a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
24708a978234SLiam Girdwood 			tplg->fw->size);
24718a978234SLiam Girdwood 		return -EINVAL;
24728a978234SLiam Girdwood 	}
24738a978234SLiam Girdwood 
24748a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
24758a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
24768a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
24778a978234SLiam Girdwood 		return -EINVAL;
24788a978234SLiam Girdwood 	}
24798a978234SLiam Girdwood 
24808a978234SLiam Girdwood 	if (tplg->pass == hdr->type)
24818a978234SLiam Girdwood 		dev_dbg(tplg->dev,
24828a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
24838a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
24848a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
24858a978234SLiam Girdwood 
24868a978234SLiam Girdwood 	return 1;
24878a978234SLiam Girdwood }
24888a978234SLiam Girdwood 
24898a978234SLiam Girdwood /* check header type and call appropriate handler */
24908a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
24918a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24928a978234SLiam Girdwood {
24938a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
24948a978234SLiam Girdwood 
24958a978234SLiam Girdwood 	/* check for matching ID */
24968a978234SLiam Girdwood 	if (hdr->index != tplg->req_index &&
2497bb97142bSLiam Girdwood 		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
24988a978234SLiam Girdwood 		return 0;
24998a978234SLiam Girdwood 
25008a978234SLiam Girdwood 	tplg->index = hdr->index;
25018a978234SLiam Girdwood 
25028a978234SLiam Girdwood 	switch (hdr->type) {
25038a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
25048a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
25058a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
25068a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
25078a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
25088a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
25098a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
25108a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
25118a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
251264527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
25133fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
25149aa3f034SMengdong Lin 		return soc_tplg_dai_elems_load(tplg, hdr);
2515593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2516593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2517593d9e52SMengdong Lin 		/* physical link configurations */
2518593d9e52SMengdong Lin 		return soc_tplg_link_elems_load(tplg, hdr);
25198a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
25208a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
25218a978234SLiam Girdwood 	default:
25228a978234SLiam Girdwood 		/* bespoke vendor data object */
25238a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
25248a978234SLiam Girdwood 	}
25258a978234SLiam Girdwood 
25268a978234SLiam Girdwood 	return 0;
25278a978234SLiam Girdwood }
25288a978234SLiam Girdwood 
25298a978234SLiam Girdwood /* process the topology file headers */
25308a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
25318a978234SLiam Girdwood {
25328a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
25338a978234SLiam Girdwood 	int ret;
25348a978234SLiam Girdwood 
25358a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
25368a978234SLiam Girdwood 
25378a978234SLiam Girdwood 	/* process the header types from start to end */
25388a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
25398a978234SLiam Girdwood 
25408a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
25418a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25428a978234SLiam Girdwood 
25438a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
25448a978234SLiam Girdwood 
25458a978234SLiam Girdwood 			/* make sure header is valid before loading */
25468a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
25478a978234SLiam Girdwood 			if (ret < 0)
25488a978234SLiam Girdwood 				return ret;
25498a978234SLiam Girdwood 			else if (ret == 0)
25508a978234SLiam Girdwood 				break;
25518a978234SLiam Girdwood 
25528a978234SLiam Girdwood 			/* load the header object */
25538a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
25548a978234SLiam Girdwood 			if (ret < 0)
25558a978234SLiam Girdwood 				return ret;
25568a978234SLiam Girdwood 
25578a978234SLiam Girdwood 			/* goto next header */
25588a978234SLiam Girdwood 			tplg->hdr_pos += hdr->payload_size +
25598a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
25608a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25618a978234SLiam Girdwood 		}
25628a978234SLiam Girdwood 
25638a978234SLiam Girdwood 		/* next data type pass */
25648a978234SLiam Girdwood 		tplg->pass++;
25658a978234SLiam Girdwood 	}
25668a978234SLiam Girdwood 
25678a978234SLiam Girdwood 	/* signal DAPM we are complete */
25688a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
25698a978234SLiam Girdwood 	if (ret < 0)
25708a978234SLiam Girdwood 		dev_err(tplg->dev,
25718a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
25728a978234SLiam Girdwood 
25738a978234SLiam Girdwood 	return ret;
25748a978234SLiam Girdwood }
25758a978234SLiam Girdwood 
25768a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
25778a978234SLiam Girdwood {
25788a978234SLiam Girdwood 	int ret;
25798a978234SLiam Girdwood 
25808a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
25818a978234SLiam Girdwood 	if (ret == 0)
25828a978234SLiam Girdwood 		soc_tplg_complete(tplg);
25838a978234SLiam Girdwood 
25848a978234SLiam Girdwood 	return ret;
25858a978234SLiam Girdwood }
25868a978234SLiam Girdwood 
25878a978234SLiam Girdwood /* load audio component topology from "firmware" file */
25888a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
25898a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
25908a978234SLiam Girdwood {
25918a978234SLiam Girdwood 	struct soc_tplg tplg;
2592304017d3SBard liao 	int ret;
25938a978234SLiam Girdwood 
25948a978234SLiam Girdwood 	/* setup parsing context */
25958a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
25968a978234SLiam Girdwood 	tplg.fw = fw;
25978a978234SLiam Girdwood 	tplg.dev = comp->dev;
25988a978234SLiam Girdwood 	tplg.comp = comp;
25998a978234SLiam Girdwood 	tplg.ops = ops;
26008a978234SLiam Girdwood 	tplg.req_index = id;
26018a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
26028a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
26031a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
26041a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
26058a978234SLiam Girdwood 
2606304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2607304017d3SBard liao 	/* free the created components if fail to load topology */
2608304017d3SBard liao 	if (ret)
2609304017d3SBard liao 		snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2610304017d3SBard liao 
2611304017d3SBard liao 	return ret;
26128a978234SLiam Girdwood }
26138a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
26148a978234SLiam Girdwood 
26158a978234SLiam Girdwood /* remove this dynamic widget */
26168a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
26178a978234SLiam Girdwood {
26188a978234SLiam Girdwood 	/* make sure we are a widget */
26198a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
26208a978234SLiam Girdwood 		return;
26218a978234SLiam Girdwood 
26228a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
26238a978234SLiam Girdwood }
26248a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
26258a978234SLiam Girdwood 
26268a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
26278a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
26288a978234SLiam Girdwood 	u32 index)
26298a978234SLiam Girdwood {
26308a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
26318a978234SLiam Girdwood 
26328a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
26338a978234SLiam Girdwood 
26348a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
26358a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
26368a978234SLiam Girdwood 			continue;
26378a978234SLiam Girdwood 
26388a978234SLiam Girdwood 		/* match ID */
26398a978234SLiam Girdwood 		if (w->dobj.index != index &&
26408a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
26418a978234SLiam Girdwood 			continue;
26428a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
26438a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2644b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
26458a978234SLiam Girdwood 	}
2646fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
26478a978234SLiam Girdwood }
26488a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
26498a978234SLiam Girdwood 
26508a978234SLiam Girdwood /* remove dynamic controls from the component driver */
26518a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
26528a978234SLiam Girdwood {
26538a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
26548a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
26558a978234SLiam Girdwood 
26568a978234SLiam Girdwood 	/* process the header types from end to start */
26578a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
26588a978234SLiam Girdwood 
26598a978234SLiam Girdwood 		/* remove mixer controls */
26608a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
26618a978234SLiam Girdwood 			list) {
26628a978234SLiam Girdwood 
26638a978234SLiam Girdwood 			/* match index */
26648a978234SLiam Girdwood 			if (dobj->index != index &&
2665feb12f0cSYan Wang 				index != SND_SOC_TPLG_INDEX_ALL)
26668a978234SLiam Girdwood 				continue;
26678a978234SLiam Girdwood 
26688a978234SLiam Girdwood 			switch (dobj->type) {
26698a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
26708a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
26718a978234SLiam Girdwood 				break;
26728a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
26738a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
26748a978234SLiam Girdwood 				break;
26758a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
26768a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
26778a978234SLiam Girdwood 				break;
26787df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
26797df04ea7SRanjani Sridharan 				remove_route(comp, dobj, pass);
26807df04ea7SRanjani Sridharan 				break;
26818a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
26828a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
26838a978234SLiam Girdwood 				break;
26848a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
268564527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
26868a978234SLiam Girdwood 				break;
2687acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2688acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2689acfc7d46SMengdong Lin 				break;
2690adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2691adfebb51SBard liao 				/*
2692adfebb51SBard liao 				 * call link_unload ops if extra
2693adfebb51SBard liao 				 * deinitialization is needed.
2694adfebb51SBard liao 				 */
2695adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2696adfebb51SBard liao 				break;
26978a978234SLiam Girdwood 			default:
26988a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
26998a978234SLiam Girdwood 					dobj->type);
27008a978234SLiam Girdwood 				break;
27018a978234SLiam Girdwood 			}
27028a978234SLiam Girdwood 		}
27038a978234SLiam Girdwood 		pass--;
27048a978234SLiam Girdwood 	}
27058a978234SLiam Girdwood 
27068a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
27078a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
27088a978234SLiam Girdwood }
27098a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2710