xref: /openbmc/linux/sound/soc/soc-topology.c (revision 3cde818c)
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 
332114171dSPierre-Louis Bossart #define SOC_TPLG_MAGIC_BIG_ENDIAN            0x436F5341 /* ASoC in reverse */
342114171dSPierre-Louis Bossart 
358a978234SLiam Girdwood /*
368a978234SLiam Girdwood  * We make several passes over the data (since it wont necessarily be ordered)
378a978234SLiam Girdwood  * and process objects in the following order. This guarantees the component
388a978234SLiam Girdwood  * drivers will be ready with any vendor data before the mixers and DAPM objects
398a978234SLiam Girdwood  * are loaded (that may make use of the vendor data).
408a978234SLiam Girdwood  */
418a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST		0
428a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR		1
438a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER		2
448a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET		3
451a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI		4
461a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH		5
471a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PINS		6
480038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI		7
49593d9e52SMengdong Lin #define SOC_TPLG_PASS_LINK		8
508a978234SLiam Girdwood 
518a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
52593d9e52SMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_LINK
538a978234SLiam Girdwood 
54583958faSMengdong Lin /* topology context */
558a978234SLiam Girdwood struct soc_tplg {
568a978234SLiam Girdwood 	const struct firmware *fw;
578a978234SLiam Girdwood 
588a978234SLiam Girdwood 	/* runtime FW parsing */
598a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
608a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
618a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
628a978234SLiam Girdwood 
638a978234SLiam Girdwood 	/* component caller */
648a978234SLiam Girdwood 	struct device *dev;
658a978234SLiam Girdwood 	struct snd_soc_component *comp;
668a978234SLiam Girdwood 	u32 index;	/* current block index */
678a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
688a978234SLiam Girdwood 
6988a17d8fSMengdong Lin 	/* vendor specific kcontrol operations */
708a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
718a978234SLiam Girdwood 	int io_ops_count;
728a978234SLiam Girdwood 
731a3232d2SMengdong Lin 	/* vendor specific bytes ext handlers, for TLV bytes controls */
741a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
751a3232d2SMengdong Lin 	int bytes_ext_ops_count;
761a3232d2SMengdong Lin 
778a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
788a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
798a978234SLiam Girdwood };
808a978234SLiam Girdwood 
818a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg);
828a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
838a978234SLiam Girdwood struct snd_soc_dapm_widget *
848a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
858a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
868a978234SLiam Girdwood struct snd_soc_dapm_widget *
878a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
888a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
89*3cde818cSAmadeusz Sławiński static void soc_tplg_denum_remove_texts(struct soc_enum *se);
90*3cde818cSAmadeusz Sławiński static void soc_tplg_denum_remove_values(struct soc_enum *se);
918a978234SLiam Girdwood 
928a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
938a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
948a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
958a978234SLiam Girdwood {
968a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
978a978234SLiam Girdwood 
988a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
998a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
1008a978234SLiam Girdwood 			elem_type);
1018a978234SLiam Girdwood 		return -EINVAL;
1028a978234SLiam Girdwood 	}
1038a978234SLiam Girdwood 
1048a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
1058a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1068a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1078a978234SLiam Girdwood 		dev_err(tplg->dev,
1088a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1098a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1108a978234SLiam Girdwood 		return -EINVAL;
1118a978234SLiam Girdwood 	}
1128a978234SLiam Girdwood 
1138a978234SLiam Girdwood 	return 0;
1148a978234SLiam Girdwood }
1158a978234SLiam Girdwood 
1168a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1178a978234SLiam Girdwood {
1188a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1198a978234SLiam Girdwood 
1208a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1218a978234SLiam Girdwood 		return 1;
1228a978234SLiam Girdwood 	return 0;
1238a978234SLiam Girdwood }
1248a978234SLiam Girdwood 
1258a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1268a978234SLiam Girdwood {
1278a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1288a978234SLiam Girdwood }
1298a978234SLiam Girdwood 
1308a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1318a978234SLiam Girdwood {
1328a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1338a978234SLiam Girdwood }
1348a978234SLiam Girdwood 
1358a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1368a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1378a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1388a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1398a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1408a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1418a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1428a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1438a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1448a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1458a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1468a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1478a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1488a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1498a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1508a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1518a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1528a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1538a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1542c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1558a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1568a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1578a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1588a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1598a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1608a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1618a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1628a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1638a978234SLiam Girdwood };
1648a978234SLiam Girdwood 
1658a978234SLiam Girdwood struct soc_tplg_map {
1668a978234SLiam Girdwood 	int uid;
1678a978234SLiam Girdwood 	int kid;
1688a978234SLiam Girdwood };
1698a978234SLiam Girdwood 
1708a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1718a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1818a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1828a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1838a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1848a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1858a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1868a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1878a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1888a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
1898a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
1908a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
1918a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
1928a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
1938a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
1948a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
1958a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
1968a978234SLiam Girdwood };
1978a978234SLiam Girdwood 
1988a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1998a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2008a978234SLiam Girdwood {
2018a978234SLiam Girdwood 	int i;
2028a978234SLiam Girdwood 
2038a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2045aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
2055aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].reg);
2068a978234SLiam Girdwood 	}
2078a978234SLiam Girdwood 
2088a978234SLiam Girdwood 	return -EINVAL;
2098a978234SLiam Girdwood }
2108a978234SLiam Girdwood 
2118a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2128a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2138a978234SLiam Girdwood {
2148a978234SLiam Girdwood 	int i;
2158a978234SLiam Girdwood 
2168a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2175aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
2185aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].shift);
2198a978234SLiam Girdwood 	}
2208a978234SLiam Girdwood 
2218a978234SLiam Girdwood 	return -EINVAL;
2228a978234SLiam Girdwood }
2238a978234SLiam Girdwood 
2248a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2258a978234SLiam Girdwood {
2268a978234SLiam Girdwood 	int i;
2278a978234SLiam Girdwood 
2288a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2298a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2308a978234SLiam Girdwood 			return dapm_map[i].kid;
2318a978234SLiam Girdwood 	}
2328a978234SLiam Girdwood 
2338a978234SLiam Girdwood 	return -EINVAL;
2348a978234SLiam Girdwood }
2358a978234SLiam Girdwood 
2368a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2378a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2388a978234SLiam Girdwood {
2398a978234SLiam Girdwood 	dev_err(tplg->dev,
2408a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2418a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2428a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2438a978234SLiam Girdwood }
2448a978234SLiam Girdwood 
2458a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2468a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2478a978234SLiam Girdwood {
2488a978234SLiam Girdwood 	dev_err(tplg->dev,
2498a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2508a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2518a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2528a978234SLiam Girdwood }
2538a978234SLiam Girdwood 
2548a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2558a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2568a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2578a978234SLiam Girdwood {
2588a978234SLiam Girdwood 	int ret = 0;
2598a978234SLiam Girdwood 
2608a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
261c60b613aSLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
2628a978234SLiam Girdwood 	else {
2638a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2648a978234SLiam Girdwood 			hdr->vendor_type);
2658a978234SLiam Girdwood 		return -EINVAL;
2668a978234SLiam Girdwood 	}
2678a978234SLiam Girdwood 
2688a978234SLiam Girdwood 	if (ret < 0)
2698a978234SLiam Girdwood 		dev_err(tplg->dev,
2708a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2718a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2728a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2738a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2748a978234SLiam Girdwood 	return ret;
2758a978234SLiam Girdwood }
2768a978234SLiam Girdwood 
2778a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2788a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2798a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2808a978234SLiam Girdwood {
2818a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2828a978234SLiam Girdwood 		return 0;
2838a978234SLiam Girdwood 
2848a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2858a978234SLiam Girdwood }
2868a978234SLiam Girdwood 
2878a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2888a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2898a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2908a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2918a978234SLiam Girdwood {
2928a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
293c60b613aSLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
294c60b613aSLiam Girdwood 			tplg_w);
2958a978234SLiam Girdwood 
2968a978234SLiam Girdwood 	return 0;
2978a978234SLiam Girdwood }
2988a978234SLiam Girdwood 
299ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
300ebd259d3SLiam Girdwood  * external widgets where we can assign private data/ops */
301ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg,
302ebd259d3SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
303ebd259d3SLiam Girdwood {
304ebd259d3SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
305c60b613aSLiam Girdwood 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
306c60b613aSLiam Girdwood 			tplg_w);
307ebd259d3SLiam Girdwood 
308ebd259d3SLiam Girdwood 	return 0;
309ebd259d3SLiam Girdwood }
310ebd259d3SLiam Girdwood 
311183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */
31264527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
313c60b613aSLiam Girdwood 	struct snd_soc_dai_driver *dai_drv,
314c60b613aSLiam Girdwood 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
3158a978234SLiam Girdwood {
31664527e8aSMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
317c60b613aSLiam Girdwood 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
318c60b613aSLiam Girdwood 			pcm, dai);
3198a978234SLiam Girdwood 
3208a978234SLiam Girdwood 	return 0;
3218a978234SLiam Girdwood }
3228a978234SLiam Girdwood 
323183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */
324acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
325c60b613aSLiam Girdwood 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
326acfc7d46SMengdong Lin {
327acfc7d46SMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
328c60b613aSLiam Girdwood 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
329acfc7d46SMengdong Lin 
330acfc7d46SMengdong Lin 	return 0;
331acfc7d46SMengdong Lin }
332acfc7d46SMengdong Lin 
3338a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3348a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3358a978234SLiam Girdwood {
3368a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3378a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3388a978234SLiam Girdwood }
3398a978234SLiam Girdwood 
3408a978234SLiam Girdwood /* add a dynamic kcontrol */
3418a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3428a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3438a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3448a978234SLiam Girdwood {
3458a978234SLiam Girdwood 	int err;
3468a978234SLiam Girdwood 
3478a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3488a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3498a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3508a978234SLiam Girdwood 		control_new->name);
3518a978234SLiam Girdwood 		return -ENOMEM;
3528a978234SLiam Girdwood 	}
3538a978234SLiam Girdwood 
3548a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3558a978234SLiam Girdwood 	if (err < 0) {
3568a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3578a978234SLiam Girdwood 			control_new->name, err);
3588a978234SLiam Girdwood 		return err;
3598a978234SLiam Girdwood 	}
3608a978234SLiam Girdwood 
3618a978234SLiam Girdwood 	return 0;
3628a978234SLiam Girdwood }
3638a978234SLiam Girdwood 
3648a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3658a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3668a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3678a978234SLiam Girdwood {
3688a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3698a978234SLiam Girdwood 
3708a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3718a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3728a978234SLiam Girdwood }
3738a978234SLiam Girdwood 
3748a978234SLiam Girdwood /* remove a mixer kcontrol */
3758a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3768a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3778a978234SLiam Girdwood {
3788a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3798a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3808a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3818a978234SLiam Girdwood 	const unsigned int *p = NULL;
3828a978234SLiam Girdwood 
3838a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3848a978234SLiam Girdwood 		return;
3858a978234SLiam Girdwood 
3868a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3878a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3888a978234SLiam Girdwood 
38933ae6ae2SAmadeusz Sławiński 	if (dobj->control.kcontrol->tlv.p)
39033ae6ae2SAmadeusz Sławiński 		p = dobj->control.kcontrol->tlv.p;
39133ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
39233ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
3938a978234SLiam Girdwood 	kfree(sm);
3948a978234SLiam Girdwood 	kfree(p);
3958a978234SLiam Girdwood }
3968a978234SLiam Girdwood 
3978a978234SLiam Girdwood /* remove an enum kcontrol */
3988a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3998a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4008a978234SLiam Girdwood {
4018a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4028a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
4038a978234SLiam Girdwood 
4048a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4058a978234SLiam Girdwood 		return;
4068a978234SLiam Girdwood 
4078a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4088a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4098a978234SLiam Girdwood 
41033ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
41133ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4128a978234SLiam Girdwood 
413*3cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_values(se);
414*3cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_texts(se);
4158a978234SLiam Girdwood 	kfree(se);
4168a978234SLiam Girdwood }
4178a978234SLiam Girdwood 
4188a978234SLiam Girdwood /* remove a byte kcontrol */
4198a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4208a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4218a978234SLiam Girdwood {
4228a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4238a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4248a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4258a978234SLiam Girdwood 
4268a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4278a978234SLiam Girdwood 		return;
4288a978234SLiam Girdwood 
4298a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4308a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4318a978234SLiam Girdwood 
43233ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
43333ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4348a978234SLiam Girdwood 	kfree(sb);
4358a978234SLiam Girdwood }
4368a978234SLiam Girdwood 
4377df04ea7SRanjani Sridharan /* remove a route */
4387df04ea7SRanjani Sridharan static void remove_route(struct snd_soc_component *comp,
4397df04ea7SRanjani Sridharan 			 struct snd_soc_dobj *dobj, int pass)
4407df04ea7SRanjani Sridharan {
4417df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route *route =
4427df04ea7SRanjani Sridharan 		container_of(dobj, struct snd_soc_dapm_route, dobj);
4437df04ea7SRanjani Sridharan 
4447df04ea7SRanjani Sridharan 	if (pass != SOC_TPLG_PASS_GRAPH)
4457df04ea7SRanjani Sridharan 		return;
4467df04ea7SRanjani Sridharan 
4477df04ea7SRanjani Sridharan 	if (dobj->ops && dobj->ops->dapm_route_unload)
4487df04ea7SRanjani Sridharan 		dobj->ops->dapm_route_unload(comp, dobj);
4497df04ea7SRanjani Sridharan 
4507df04ea7SRanjani Sridharan 	list_del(&dobj->list);
4517df04ea7SRanjani Sridharan 	kfree(route);
4527df04ea7SRanjani Sridharan }
4537df04ea7SRanjani Sridharan 
4548a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4558a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4568a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4578a978234SLiam Girdwood {
4588a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4598a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4608a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4618a978234SLiam Girdwood 	int i;
4628a978234SLiam Girdwood 
4638a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4648a978234SLiam Girdwood 		return;
4658a978234SLiam Girdwood 
4668a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4678a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4688a978234SLiam Girdwood 
46905bdcf12SLiam Girdwood 	if (!w->kcontrols)
47005bdcf12SLiam Girdwood 		goto free_news;
47105bdcf12SLiam Girdwood 
4728a978234SLiam Girdwood 	/*
4731a7dd6e2SMengdong Lin 	 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
4748a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4758a978234SLiam Girdwood 	 */
476eea3dd4fSMengdong Lin 	if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
4778a978234SLiam Girdwood 		/* enumerated widget mixer */
478f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4791a7dd6e2SMengdong Lin 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4808a978234SLiam Girdwood 			struct soc_enum *se =
4811a7dd6e2SMengdong Lin 				(struct soc_enum *)kcontrol->private_value;
4828a978234SLiam Girdwood 
4831a7dd6e2SMengdong Lin 			snd_ctl_remove(card, kcontrol);
4848a978234SLiam Girdwood 
48554f8844eSRanjani Sridharan 			/* free enum kcontrol's dvalues and dtexts */
486*3cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_values(se);
487*3cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_texts(se);
4888a978234SLiam Girdwood 
4898a978234SLiam Girdwood 			kfree(se);
490267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
4911a7dd6e2SMengdong Lin 		}
4928a978234SLiam Girdwood 	} else {
493eea3dd4fSMengdong Lin 		/* volume mixer or bytes controls */
494f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4958a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4968a978234SLiam Girdwood 
497eea3dd4fSMengdong Lin 			if (dobj->widget.kcontrol_type
498eea3dd4fSMengdong Lin 			    == SND_SOC_TPLG_TYPE_MIXER)
499eea3dd4fSMengdong Lin 				kfree(kcontrol->tlv.p);
5008a978234SLiam Girdwood 
501eea3dd4fSMengdong Lin 			/* Private value is used as struct soc_mixer_control
502eea3dd4fSMengdong Lin 			 * for volume mixers or soc_bytes_ext for bytes
503eea3dd4fSMengdong Lin 			 * controls.
504eea3dd4fSMengdong Lin 			 */
505eea3dd4fSMengdong Lin 			kfree((void *)kcontrol->private_value);
506c2b36129SColin Ian King 			snd_ctl_remove(card, kcontrol);
507267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
5088a978234SLiam Girdwood 		}
5098a978234SLiam Girdwood 	}
51005bdcf12SLiam Girdwood 
51105bdcf12SLiam Girdwood free_news:
5128a978234SLiam Girdwood 	kfree(w->kcontrol_news);
51305bdcf12SLiam Girdwood 
514a46e8393SAmadeusz Sławiński 	list_del(&dobj->list);
515a46e8393SAmadeusz Sławiński 
5168a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
5178a978234SLiam Girdwood }
5188a978234SLiam Girdwood 
51964527e8aSMengdong Lin /* remove DAI configurations */
52064527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
5218a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
5228a978234SLiam Girdwood {
52364527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
52464527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
52552abe6ccSGuennadi Liakhovetski 	struct snd_soc_dai *dai;
52664527e8aSMengdong Lin 
5278a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
5288a978234SLiam Girdwood 		return;
5298a978234SLiam Girdwood 
53064527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
53164527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
5328a978234SLiam Girdwood 
53352abe6ccSGuennadi Liakhovetski 	list_for_each_entry(dai, &comp->dai_list, list)
53452abe6ccSGuennadi Liakhovetski 		if (dai->driver == dai_drv)
53552abe6ccSGuennadi Liakhovetski 			dai->driver = NULL;
53652abe6ccSGuennadi Liakhovetski 
5377b6f68a4SBard liao 	kfree(dai_drv->playback.stream_name);
5387b6f68a4SBard liao 	kfree(dai_drv->capture.stream_name);
5398f27c4abSMengdong Lin 	kfree(dai_drv->name);
5408a978234SLiam Girdwood 	list_del(&dobj->list);
54164527e8aSMengdong Lin 	kfree(dai_drv);
5428a978234SLiam Girdwood }
5438a978234SLiam Girdwood 
544acfc7d46SMengdong Lin /* remove link configurations */
545acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
546acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
547acfc7d46SMengdong Lin {
548acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
549acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
550acfc7d46SMengdong Lin 
551acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
552acfc7d46SMengdong Lin 		return;
553acfc7d46SMengdong Lin 
554acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
555acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
556acfc7d46SMengdong Lin 
5578f27c4abSMengdong Lin 	kfree(link->name);
5588f27c4abSMengdong Lin 	kfree(link->stream_name);
55923b946ceSKuninori Morimoto 	kfree(link->cpus->dai_name);
5608f27c4abSMengdong Lin 
561acfc7d46SMengdong Lin 	list_del(&dobj->list);
562acfc7d46SMengdong Lin 	snd_soc_remove_dai_link(comp->card, link);
563acfc7d46SMengdong Lin 	kfree(link);
564acfc7d46SMengdong Lin }
565acfc7d46SMengdong Lin 
566adfebb51SBard liao /* unload dai link */
567adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp,
568adfebb51SBard liao 	struct snd_soc_dobj *dobj, int pass)
569adfebb51SBard liao {
570adfebb51SBard liao 	if (pass != SOC_TPLG_PASS_LINK)
571adfebb51SBard liao 		return;
572adfebb51SBard liao 
573adfebb51SBard liao 	if (dobj->ops && dobj->ops->link_unload)
574adfebb51SBard liao 		dobj->ops->link_unload(comp, dobj);
575adfebb51SBard liao 
576adfebb51SBard liao 	/*
577adfebb51SBard liao 	 * We don't free the link here as what remove_link() do since BE
578adfebb51SBard liao 	 * links are not allocated by topology.
579adfebb51SBard liao 	 * We however need to reset the dobj type to its initial values
580adfebb51SBard liao 	 */
581adfebb51SBard liao 	dobj->type = SND_SOC_DOBJ_NONE;
582adfebb51SBard liao 	list_del(&dobj->list);
583adfebb51SBard liao }
584adfebb51SBard liao 
5858a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5868a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5878a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5882b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5898a978234SLiam Girdwood {
5902b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5911a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5922b5cdb91SMengdong Lin 	int num_ops, i;
5938a978234SLiam Girdwood 
5945aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
5951a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5961a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5971a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5981a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5991a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
6001a3232d2SMengdong Lin 
6011a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
6021a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
6031a3232d2SMengdong Lin 
6041a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
6051a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
6061a3232d2SMengdong Lin 		 */
607f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
6081a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
6091a3232d2SMengdong Lin 
6101a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
6111a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
6121a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
6131a3232d2SMengdong Lin 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
6141a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
6151a3232d2SMengdong Lin 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
6161a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
6171a3232d2SMengdong Lin 		}
6181a3232d2SMengdong Lin 
6191a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
6201a3232d2SMengdong Lin 			return 0;
6211a3232d2SMengdong Lin 		else
6221a3232d2SMengdong Lin 			return -EINVAL;
6231a3232d2SMengdong Lin 	}
6241a3232d2SMengdong Lin 
62588a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
6262b5cdb91SMengdong Lin 	ops = tplg->io_ops;
6272b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
6288a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
6298a978234SLiam Girdwood 
6302b5cdb91SMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
6318a978234SLiam Girdwood 			k->put = ops[i].put;
6322b5cdb91SMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
6338a978234SLiam Girdwood 			k->get = ops[i].get;
6342b5cdb91SMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6352b5cdb91SMengdong Lin 			k->info = ops[i].info;
6368a978234SLiam Girdwood 	}
6378a978234SLiam Girdwood 
63888a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
63988a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
64088a17d8fSMengdong Lin 		return 0;
64188a17d8fSMengdong Lin 
64288a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
6432b5cdb91SMengdong Lin 	ops = io_ops;
6442b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
64588a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
64688a17d8fSMengdong Lin 
64788a17d8fSMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
64888a17d8fSMengdong Lin 			k->put = ops[i].put;
64988a17d8fSMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
65088a17d8fSMengdong Lin 			k->get = ops[i].get;
65188a17d8fSMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6528a978234SLiam Girdwood 			k->info = ops[i].info;
6538a978234SLiam Girdwood 	}
6548a978234SLiam Girdwood 
6558a978234SLiam Girdwood 	/* standard handlers found ? */
6568a978234SLiam Girdwood 	if (k->put && k->get && k->info)
6578a978234SLiam Girdwood 		return 0;
6588a978234SLiam Girdwood 
6598a978234SLiam Girdwood 	/* nothing to bind */
6608a978234SLiam Girdwood 	return -EINVAL;
6618a978234SLiam Girdwood }
6628a978234SLiam Girdwood 
6638a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
6648a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
6658a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
6668a978234SLiam Girdwood 		int num_events, u16 event_type)
6678a978234SLiam Girdwood {
6688a978234SLiam Girdwood 	int i;
6698a978234SLiam Girdwood 
6708a978234SLiam Girdwood 	w->event = NULL;
6718a978234SLiam Girdwood 
6728a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
6738a978234SLiam Girdwood 		if (event_type == events[i].type) {
6748a978234SLiam Girdwood 
6758a978234SLiam Girdwood 			/* found - so assign event */
6768a978234SLiam Girdwood 			w->event = events[i].event_handler;
6778a978234SLiam Girdwood 			return 0;
6788a978234SLiam Girdwood 		}
6798a978234SLiam Girdwood 	}
6808a978234SLiam Girdwood 
6818a978234SLiam Girdwood 	/* not found */
6828a978234SLiam Girdwood 	return -EINVAL;
6838a978234SLiam Girdwood }
6848a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
6858a978234SLiam Girdwood 
6868a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6878a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6888a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6898a978234SLiam Girdwood {
6908a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
691c60b613aSLiam Girdwood 		return tplg->ops->control_load(tplg->comp, tplg->index, k,
692c60b613aSLiam Girdwood 			hdr);
6938a978234SLiam Girdwood 
6948a978234SLiam Girdwood 	return 0;
6958a978234SLiam Girdwood }
6968a978234SLiam Girdwood 
69728a87eebSMengdong Lin 
69828a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
69928a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
7008a978234SLiam Girdwood {
70128a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
70228a87eebSMengdong Lin 	unsigned int *p;
7038a978234SLiam Girdwood 
70428a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
70528a87eebSMengdong Lin 	if (!p)
7068a978234SLiam Girdwood 		return -ENOMEM;
7078a978234SLiam Girdwood 
70828a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
70928a87eebSMengdong Lin 	p[1] = item_len;
7105aebe7c7SPierre-Louis Bossart 	p[2] = le32_to_cpu(scale->min);
7115aebe7c7SPierre-Louis Bossart 	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
7125aebe7c7SPierre-Louis Bossart 		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
7138a978234SLiam Girdwood 
71428a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
71528a87eebSMengdong Lin 	return 0;
71628a87eebSMengdong Lin }
71728a87eebSMengdong Lin 
71828a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
71928a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
72028a87eebSMengdong Lin {
72128a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
7225aebe7c7SPierre-Louis Bossart 	u32 access = le32_to_cpu(tc->access);
72328a87eebSMengdong Lin 
7245aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
72528a87eebSMengdong Lin 		return 0;
72628a87eebSMengdong Lin 
7275aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
72828a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
7295aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(tplg_tlv->type)) {
73028a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
73128a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
73228a87eebSMengdong Lin 					&tplg_tlv->scale);
73328a87eebSMengdong Lin 
73428a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
73528a87eebSMengdong Lin 		default:
73628a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
73728a87eebSMengdong Lin 					tplg_tlv->type);
73828a87eebSMengdong Lin 			return -EINVAL;
73928a87eebSMengdong Lin 		}
74028a87eebSMengdong Lin 	}
7418a978234SLiam Girdwood 
7428a978234SLiam Girdwood 	return 0;
7438a978234SLiam Girdwood }
7448a978234SLiam Girdwood 
7458a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
7468a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
7478a978234SLiam Girdwood {
7488a978234SLiam Girdwood 	kfree(kc->tlv.p);
7498a978234SLiam Girdwood }
7508a978234SLiam Girdwood 
7518a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
7528a978234SLiam Girdwood 	size_t size)
7538a978234SLiam Girdwood {
7548a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
7558a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
7568a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7578a978234SLiam Girdwood 	int i, err;
7588a978234SLiam Girdwood 
7598a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7608a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
7618a978234SLiam Girdwood 			size, "mixer bytes")) {
7628a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
7638a978234SLiam Girdwood 			count);
7648a978234SLiam Girdwood 		return -EINVAL;
7658a978234SLiam Girdwood 	}
7668a978234SLiam Girdwood 
7678a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7688a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
7698a978234SLiam Girdwood 
7708a978234SLiam Girdwood 		/* validate kcontrol */
7718a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7728a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7738a978234SLiam Girdwood 			return -EINVAL;
7748a978234SLiam Girdwood 
7758a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
7768a978234SLiam Girdwood 		if (sbe == NULL)
7778a978234SLiam Girdwood 			return -ENOMEM;
7788a978234SLiam Girdwood 
7798a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
7805aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(be->priv.size));
7818a978234SLiam Girdwood 
7828a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7838a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
7848a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
7858a978234SLiam Girdwood 
7868a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7878a978234SLiam Girdwood 		kc.name = be->hdr.name;
7888a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7898a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7905aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(be->hdr.access);
7918a978234SLiam Girdwood 
7925aebe7c7SPierre-Louis Bossart 		sbe->max = le32_to_cpu(be->max);
7938a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7948a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7958a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7968a978234SLiam Girdwood 
7978a978234SLiam Girdwood 		/* map io handlers */
7982b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7998a978234SLiam Girdwood 		if (err) {
8008a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
8018a978234SLiam Girdwood 			kfree(sbe);
8028a978234SLiam Girdwood 			continue;
8038a978234SLiam Girdwood 		}
8048a978234SLiam Girdwood 
8058a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8068a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8078a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
8088a978234SLiam Girdwood 		if (err < 0) {
8098a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8108a978234SLiam Girdwood 				be->hdr.name);
8118a978234SLiam Girdwood 			kfree(sbe);
8128a978234SLiam Girdwood 			continue;
8138a978234SLiam Girdwood 		}
8148a978234SLiam Girdwood 
8158a978234SLiam Girdwood 		/* register control here */
8168a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8178a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
8188a978234SLiam Girdwood 		if (err < 0) {
8198a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8208a978234SLiam Girdwood 				be->hdr.name);
8218a978234SLiam Girdwood 			kfree(sbe);
8228a978234SLiam Girdwood 			continue;
8238a978234SLiam Girdwood 		}
8248a978234SLiam Girdwood 
8258a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
8268a978234SLiam Girdwood 	}
8278a978234SLiam Girdwood 	return 0;
8288a978234SLiam Girdwood 
8298a978234SLiam Girdwood }
8308a978234SLiam Girdwood 
8318a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
8328a978234SLiam Girdwood 	size_t size)
8338a978234SLiam Girdwood {
8348a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
8358a978234SLiam Girdwood 	struct soc_mixer_control *sm;
8368a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8378a978234SLiam Girdwood 	int i, err;
8388a978234SLiam Girdwood 
8398a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8408a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
8418a978234SLiam Girdwood 		count, size, "mixers")) {
8428a978234SLiam Girdwood 
8438a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
8448a978234SLiam Girdwood 			count);
8458a978234SLiam Girdwood 		return -EINVAL;
8468a978234SLiam Girdwood 	}
8478a978234SLiam Girdwood 
8488a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
8498a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
8508a978234SLiam Girdwood 
8518a978234SLiam Girdwood 		/* validate kcontrol */
8528a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8538a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8548a978234SLiam Girdwood 			return -EINVAL;
8558a978234SLiam Girdwood 
8568a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
8578a978234SLiam Girdwood 		if (sm == NULL)
8588a978234SLiam Girdwood 			return -ENOMEM;
8598a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
8605aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(mc->priv.size));
8618a978234SLiam Girdwood 
8628a978234SLiam Girdwood 		dev_dbg(tplg->dev,
8638a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
8648a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
8658a978234SLiam Girdwood 
8668a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
8678a978234SLiam Girdwood 		kc.name = mc->hdr.name;
8688a978234SLiam Girdwood 		kc.private_value = (long)sm;
8698a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8705aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(mc->hdr.access);
8718a978234SLiam Girdwood 
8728a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
8738a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
8748a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8758a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
8768a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8778a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
8788a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8798a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
8808a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8818a978234SLiam Girdwood 
8825aebe7c7SPierre-Louis Bossart 		sm->max = le32_to_cpu(mc->max);
8835aebe7c7SPierre-Louis Bossart 		sm->min = le32_to_cpu(mc->min);
8845aebe7c7SPierre-Louis Bossart 		sm->invert = le32_to_cpu(mc->invert);
8855aebe7c7SPierre-Louis Bossart 		sm->platform_max = le32_to_cpu(mc->platform_max);
8868a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
8878a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8888a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8898a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8908a978234SLiam Girdwood 
8918a978234SLiam Girdwood 		/* map io handlers */
8922b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8938a978234SLiam Girdwood 		if (err) {
8948a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8958a978234SLiam Girdwood 			kfree(sm);
8968a978234SLiam Girdwood 			continue;
8978a978234SLiam Girdwood 		}
8988a978234SLiam Girdwood 
8993789debfSBard liao 		/* create any TLV data */
9003789debfSBard liao 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
9013789debfSBard liao 
9028a978234SLiam Girdwood 		/* pass control to driver for optional further init */
9038a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
9048a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
9058a978234SLiam Girdwood 		if (err < 0) {
9068a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
9078a978234SLiam Girdwood 				mc->hdr.name);
9083789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc);
9098a978234SLiam Girdwood 			kfree(sm);
9108a978234SLiam Girdwood 			continue;
9118a978234SLiam Girdwood 		}
9128a978234SLiam Girdwood 
9138a978234SLiam Girdwood 		/* register control here */
9148a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
9158a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
9168a978234SLiam Girdwood 		if (err < 0) {
9178a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
9188a978234SLiam Girdwood 				mc->hdr.name);
9198a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
9208a978234SLiam Girdwood 			kfree(sm);
9218a978234SLiam Girdwood 			continue;
9228a978234SLiam Girdwood 		}
9238a978234SLiam Girdwood 
9248a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
9258a978234SLiam Girdwood 	}
9268a978234SLiam Girdwood 
9278a978234SLiam Girdwood 	return 0;
9288a978234SLiam Girdwood }
9298a978234SLiam Girdwood 
9308a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
9318a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9328a978234SLiam Girdwood {
9338a978234SLiam Girdwood 	int i, ret;
9348a978234SLiam Girdwood 
9358a978234SLiam Girdwood 	se->dobj.control.dtexts =
9365aebe7c7SPierre-Louis Bossart 		kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
9378a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
9388a978234SLiam Girdwood 		return -ENOMEM;
9398a978234SLiam Girdwood 
9408a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
9418a978234SLiam Girdwood 
9428a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9438a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
9448a978234SLiam Girdwood 			ret = -EINVAL;
9458a978234SLiam Girdwood 			goto err;
9468a978234SLiam Girdwood 		}
9478a978234SLiam Girdwood 
9488a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
9498a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
9508a978234SLiam Girdwood 			ret = -ENOMEM;
9518a978234SLiam Girdwood 			goto err;
9528a978234SLiam Girdwood 		}
9538a978234SLiam Girdwood 	}
9548a978234SLiam Girdwood 
955*3cde818cSAmadeusz Sławiński 	se->items = le32_to_cpu(ec->items);
956b6e38b29SMousumi Jana 	se->texts = (const char * const *)se->dobj.control.dtexts;
9578a978234SLiam Girdwood 	return 0;
9588a978234SLiam Girdwood 
9598a978234SLiam Girdwood err:
960*3cde818cSAmadeusz Sławiński 	se->items = i;
961*3cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_texts(se);
962*3cde818cSAmadeusz Sławiński 	return ret;
963*3cde818cSAmadeusz Sławiński }
964*3cde818cSAmadeusz Sławiński 
965*3cde818cSAmadeusz Sławiński static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
966*3cde818cSAmadeusz Sławiński {
967*3cde818cSAmadeusz Sławiński 	int i = se->items;
968*3cde818cSAmadeusz Sławiński 
9698a978234SLiam Girdwood 	for (--i; i >= 0; i--)
9708a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
9718a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
9728a978234SLiam Girdwood }
9738a978234SLiam Girdwood 
9748a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
9758a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9768a978234SLiam Girdwood {
9775aebe7c7SPierre-Louis Bossart 	int i;
9785aebe7c7SPierre-Louis Bossart 
9795aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(ec->items) > sizeof(*ec->values))
9808a978234SLiam Girdwood 		return -EINVAL;
9818a978234SLiam Girdwood 
9825aebe7c7SPierre-Louis Bossart 	se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
9835aebe7c7SPierre-Louis Bossart 					   sizeof(u32),
984376c0afeSAndrzej Hajda 					   GFP_KERNEL);
9858a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
9868a978234SLiam Girdwood 		return -ENOMEM;
9878a978234SLiam Girdwood 
9885aebe7c7SPierre-Louis Bossart 	/* convert from little-endian */
9895aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
9905aebe7c7SPierre-Louis Bossart 		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
9915aebe7c7SPierre-Louis Bossart 	}
9925aebe7c7SPierre-Louis Bossart 
9938a978234SLiam Girdwood 	return 0;
9948a978234SLiam Girdwood }
9958a978234SLiam Girdwood 
996*3cde818cSAmadeusz Sławiński static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
997*3cde818cSAmadeusz Sławiński {
998*3cde818cSAmadeusz Sławiński 	kfree(se->dobj.control.dvalues);
999*3cde818cSAmadeusz Sławiński }
1000*3cde818cSAmadeusz Sławiński 
10018a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
10028a978234SLiam Girdwood 	size_t size)
10038a978234SLiam Girdwood {
10048a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
10058a978234SLiam Girdwood 	struct soc_enum *se;
10068a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
10078a978234SLiam Girdwood 	int i, ret, err;
10088a978234SLiam Girdwood 
10098a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10108a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
10118a978234SLiam Girdwood 		count, size, "enums")) {
10128a978234SLiam Girdwood 
10138a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
10148a978234SLiam Girdwood 			count);
10158a978234SLiam Girdwood 		return -EINVAL;
10168a978234SLiam Girdwood 	}
10178a978234SLiam Girdwood 
10188a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
10198a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
10208a978234SLiam Girdwood 
10218a978234SLiam Girdwood 		/* validate kcontrol */
10228a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10238a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10248a978234SLiam Girdwood 			return -EINVAL;
10258a978234SLiam Girdwood 
10268a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
10278a978234SLiam Girdwood 		if (se == NULL)
10288a978234SLiam Girdwood 			return -ENOMEM;
10298a978234SLiam Girdwood 
103002b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
10315aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(ec->priv.size));
103202b64245SLiam Girdwood 
10338a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
10348a978234SLiam Girdwood 			ec->hdr.name, ec->items);
10358a978234SLiam Girdwood 
10368a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
10378a978234SLiam Girdwood 		kc.name = ec->hdr.name;
10388a978234SLiam Girdwood 		kc.private_value = (long)se;
10398a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
10405aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(ec->hdr.access);
10418a978234SLiam Girdwood 
10428a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
10438a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
10448a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10458a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
10468a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10478a978234SLiam Girdwood 
10485aebe7c7SPierre-Louis Bossart 		se->mask = le32_to_cpu(ec->mask);
10498a978234SLiam Girdwood 		se->dobj.index = tplg->index;
10508a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
10518a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
10528a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
10538a978234SLiam Girdwood 
10545aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(ec->hdr.ops.info)) {
10558a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10568a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10578a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
10588a978234SLiam Girdwood 			if (err < 0) {
10598a978234SLiam Girdwood 				dev_err(tplg->dev,
10608a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
10618a978234SLiam Girdwood 					ec->hdr.name);
10628a978234SLiam Girdwood 				kfree(se);
10638a978234SLiam Girdwood 				continue;
10648a978234SLiam Girdwood 			}
10659c6c4d96STakashi Iwai 			/* fall through */
10668a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10678a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10688a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10698a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
10708a978234SLiam Girdwood 			if (err < 0) {
10718a978234SLiam Girdwood 				dev_err(tplg->dev,
10728a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
10738a978234SLiam Girdwood 					ec->hdr.name);
10748a978234SLiam Girdwood 				kfree(se);
10758a978234SLiam Girdwood 				continue;
10768a978234SLiam Girdwood 			}
10778a978234SLiam Girdwood 			break;
10788a978234SLiam Girdwood 		default:
10798a978234SLiam Girdwood 			dev_err(tplg->dev,
10808a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
10818a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
10828a978234SLiam Girdwood 			kfree(se);
10838a978234SLiam Girdwood 			continue;
10848a978234SLiam Girdwood 		}
10858a978234SLiam Girdwood 
10868a978234SLiam Girdwood 		/* map io handlers */
10872b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
10888a978234SLiam Girdwood 		if (err) {
10898a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
10908a978234SLiam Girdwood 			kfree(se);
10918a978234SLiam Girdwood 			continue;
10928a978234SLiam Girdwood 		}
10938a978234SLiam Girdwood 
10948a978234SLiam Girdwood 		/* pass control to driver for optional further init */
10958a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
10968a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
10978a978234SLiam Girdwood 		if (err < 0) {
10988a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
10998a978234SLiam Girdwood 				ec->hdr.name);
11008a978234SLiam Girdwood 			kfree(se);
11018a978234SLiam Girdwood 			continue;
11028a978234SLiam Girdwood 		}
11038a978234SLiam Girdwood 
11048a978234SLiam Girdwood 		/* register control here */
11058a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
11068a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
11078a978234SLiam Girdwood 		if (ret < 0) {
11088a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
11098a978234SLiam Girdwood 				ec->hdr.name);
11108a978234SLiam Girdwood 			kfree(se);
11118a978234SLiam Girdwood 			continue;
11128a978234SLiam Girdwood 		}
11138a978234SLiam Girdwood 
11148a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
11158a978234SLiam Girdwood 	}
11168a978234SLiam Girdwood 
11178a978234SLiam Girdwood 	return 0;
11188a978234SLiam Girdwood }
11198a978234SLiam Girdwood 
11208a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
11218a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11228a978234SLiam Girdwood {
11238a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
11248a978234SLiam Girdwood 	int i;
11258a978234SLiam Girdwood 
11268a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
11275aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
11285aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
11298a978234SLiam Girdwood 		return 0;
11308a978234SLiam Girdwood 	}
11318a978234SLiam Girdwood 
11328a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
11338a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
11348a978234SLiam Girdwood 
11355aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
11368a978234SLiam Girdwood 
11378a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
11388a978234SLiam Girdwood 
11395aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
114006eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
114106eb49f7SMengdong Lin 			return -EINVAL;
114206eb49f7SMengdong Lin 		}
114306eb49f7SMengdong Lin 
11445aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
11458a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
11468a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
11478a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
11488a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
11498a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
11508a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
11518a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
11525aebe7c7SPierre-Louis Bossart 			soc_tplg_dmixer_create(tplg, 1,
11535aebe7c7SPierre-Louis Bossart 					       le32_to_cpu(hdr->payload_size));
11548a978234SLiam Girdwood 			break;
11558a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
11568a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
11578a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
11588a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
11598a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
11605aebe7c7SPierre-Louis Bossart 			soc_tplg_denum_create(tplg, 1,
11615aebe7c7SPierre-Louis Bossart 					      le32_to_cpu(hdr->payload_size));
11628a978234SLiam Girdwood 			break;
11638a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
11645aebe7c7SPierre-Louis Bossart 			soc_tplg_dbytes_create(tplg, 1,
11655aebe7c7SPierre-Louis Bossart 					       le32_to_cpu(hdr->payload_size));
11668a978234SLiam Girdwood 			break;
11678a978234SLiam Girdwood 		default:
11688a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
11698a978234SLiam Girdwood 			return -EINVAL;
11708a978234SLiam Girdwood 		}
11718a978234SLiam Girdwood 	}
11728a978234SLiam Girdwood 
11738a978234SLiam Girdwood 	return 0;
11748a978234SLiam Girdwood }
11758a978234SLiam Girdwood 
1176503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1177503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1178503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1179503e79b7SLiam Girdwood {
1180503e79b7SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1181503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1182503e79b7SLiam Girdwood 			route);
1183503e79b7SLiam Girdwood 
1184503e79b7SLiam Girdwood 	return 0;
1185503e79b7SLiam Girdwood }
1186503e79b7SLiam Girdwood 
11878a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
11888a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11898a978234SLiam Girdwood {
11908a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
11918a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
11927df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route **routes;
11935aebe7c7SPierre-Louis Bossart 	int count, i, j;
11947df04ea7SRanjani Sridharan 	int ret = 0;
11958a978234SLiam Girdwood 
11965aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
11975aebe7c7SPierre-Louis Bossart 
11988a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
11995aebe7c7SPierre-Louis Bossart 		tplg->pos +=
12005aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->size) +
12015aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
12025aebe7c7SPierre-Louis Bossart 
12038a978234SLiam Girdwood 		return 0;
12048a978234SLiam Girdwood 	}
12058a978234SLiam Girdwood 
12068a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
12078a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
12085aebe7c7SPierre-Louis Bossart 		count, le32_to_cpu(hdr->payload_size), "graph")) {
12098a978234SLiam Girdwood 
12108a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
12118a978234SLiam Girdwood 			count);
12128a978234SLiam Girdwood 		return -EINVAL;
12138a978234SLiam Girdwood 	}
12148a978234SLiam Girdwood 
1215b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1216b75a6511SLiam Girdwood 		hdr->index);
12178a978234SLiam Girdwood 
12187df04ea7SRanjani Sridharan 	/* allocate memory for pointer to array of dapm routes */
12197df04ea7SRanjani Sridharan 	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
12207df04ea7SRanjani Sridharan 			 GFP_KERNEL);
12217df04ea7SRanjani Sridharan 	if (!routes)
12227df04ea7SRanjani Sridharan 		return -ENOMEM;
12237df04ea7SRanjani Sridharan 
12247df04ea7SRanjani Sridharan 	/*
12257df04ea7SRanjani Sridharan 	 * allocate memory for each dapm route in the array.
12267df04ea7SRanjani Sridharan 	 * This needs to be done individually so that
12277df04ea7SRanjani Sridharan 	 * each route can be freed when it is removed in remove_route().
12287df04ea7SRanjani Sridharan 	 */
12297df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
12307df04ea7SRanjani Sridharan 		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
12317df04ea7SRanjani Sridharan 		if (!routes[i]) {
12327df04ea7SRanjani Sridharan 			/* free previously allocated memory */
12337df04ea7SRanjani Sridharan 			for (j = 0; j < i; j++)
12347df04ea7SRanjani Sridharan 				kfree(routes[j]);
12357df04ea7SRanjani Sridharan 
12367df04ea7SRanjani Sridharan 			kfree(routes);
12377df04ea7SRanjani Sridharan 			return -ENOMEM;
12387df04ea7SRanjani Sridharan 		}
12397df04ea7SRanjani Sridharan 	}
12407df04ea7SRanjani Sridharan 
12418a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12428a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
12438a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
12448a978234SLiam Girdwood 
12458a978234SLiam Girdwood 		/* validate routes */
12468a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12477df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12487df04ea7SRanjani Sridharan 			ret = -EINVAL;
12497df04ea7SRanjani Sridharan 			break;
12507df04ea7SRanjani Sridharan 		}
12518a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12527df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12537df04ea7SRanjani Sridharan 			ret = -EINVAL;
12547df04ea7SRanjani Sridharan 			break;
12557df04ea7SRanjani Sridharan 		}
12568a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12577df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12587df04ea7SRanjani Sridharan 			ret = -EINVAL;
12597df04ea7SRanjani Sridharan 			break;
12608a978234SLiam Girdwood 		}
12618a978234SLiam Girdwood 
12627df04ea7SRanjani Sridharan 		routes[i]->source = elem->source;
12637df04ea7SRanjani Sridharan 		routes[i]->sink = elem->sink;
12647df04ea7SRanjani Sridharan 
12657df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
12667df04ea7SRanjani Sridharan 		routes[i]->connected = NULL;
12677df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
12687df04ea7SRanjani Sridharan 			routes[i]->control = NULL;
12697df04ea7SRanjani Sridharan 		else
12707df04ea7SRanjani Sridharan 			routes[i]->control = elem->control;
12717df04ea7SRanjani Sridharan 
12727df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
12737df04ea7SRanjani Sridharan 		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
12747df04ea7SRanjani Sridharan 		routes[i]->dobj.ops = tplg->ops;
12757df04ea7SRanjani Sridharan 		routes[i]->dobj.index = tplg->index;
12767df04ea7SRanjani Sridharan 		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
12777df04ea7SRanjani Sridharan 
12787df04ea7SRanjani Sridharan 		soc_tplg_add_route(tplg, routes[i]);
12797df04ea7SRanjani Sridharan 
12807df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
12817df04ea7SRanjani Sridharan 		snd_soc_dapm_add_routes(dapm, routes[i], 1);
12827df04ea7SRanjani Sridharan 	}
12837df04ea7SRanjani Sridharan 
12847df04ea7SRanjani Sridharan 	/* free memory allocated for all dapm routes in case of error */
12857df04ea7SRanjani Sridharan 	if (ret < 0)
12867df04ea7SRanjani Sridharan 		for (i = 0; i < count ; i++)
12877df04ea7SRanjani Sridharan 			kfree(routes[i]);
12887df04ea7SRanjani Sridharan 
12897df04ea7SRanjani Sridharan 	/*
12907df04ea7SRanjani Sridharan 	 * free pointer to array of dapm routes as this is no longer needed.
12917df04ea7SRanjani Sridharan 	 * The memory allocated for each dapm route will be freed
12927df04ea7SRanjani Sridharan 	 * when it is removed in remove_route().
12937df04ea7SRanjani Sridharan 	 */
12947df04ea7SRanjani Sridharan 	kfree(routes);
12957df04ea7SRanjani Sridharan 
12967df04ea7SRanjani Sridharan 	return ret;
12978a978234SLiam Girdwood }
12988a978234SLiam Girdwood 
12998a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
13008a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
13018a978234SLiam Girdwood {
13028a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13038a978234SLiam Girdwood 	struct soc_mixer_control *sm;
13048a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
13058a978234SLiam Girdwood 	int i, err;
13068a978234SLiam Girdwood 
13074ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13088a978234SLiam Girdwood 	if (kc == NULL)
13098a978234SLiam Girdwood 		return NULL;
13108a978234SLiam Girdwood 
13118a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
13128a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
13138a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
13148a978234SLiam Girdwood 		if (sm == NULL)
13158a978234SLiam Girdwood 			goto err;
13168a978234SLiam Girdwood 
13178a978234SLiam Girdwood 		/* validate kcontrol */
13188a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13198a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13208a978234SLiam Girdwood 			goto err_str;
13218a978234SLiam Girdwood 
132202b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
13235aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(mc->priv.size));
132402b64245SLiam Girdwood 
13258a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
13268a978234SLiam Girdwood 			mc->hdr.name, i);
13278a978234SLiam Girdwood 
1328267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1329267e2c6fSLiam Girdwood 		if (kc[i].name == NULL)
1330267e2c6fSLiam Girdwood 			goto err_str;
13318a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
13328a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13338a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
13348a978234SLiam Girdwood 
13358a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13368a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
13378a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13388a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
13398a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13408a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
13418a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13428a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
13438a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13448a978234SLiam Girdwood 
13458a978234SLiam Girdwood 		sm->max = mc->max;
13468a978234SLiam Girdwood 		sm->min = mc->min;
13478a978234SLiam Girdwood 		sm->invert = mc->invert;
13488a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
13498a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
13508a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
13518a978234SLiam Girdwood 
13528a978234SLiam Girdwood 		/* map io handlers */
13532b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
13548a978234SLiam Girdwood 		if (err) {
13558a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
13568a978234SLiam Girdwood 			kfree(sm);
13578a978234SLiam Girdwood 			continue;
13588a978234SLiam Girdwood 		}
13598a978234SLiam Girdwood 
13603789debfSBard liao 		/* create any TLV data */
13613789debfSBard liao 		soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
13623789debfSBard liao 
13638a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13648a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13658a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
13668a978234SLiam Girdwood 		if (err < 0) {
13678a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13688a978234SLiam Girdwood 				mc->hdr.name);
13693789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc[i]);
13708a978234SLiam Girdwood 			kfree(sm);
13718a978234SLiam Girdwood 			continue;
13728a978234SLiam Girdwood 		}
13738a978234SLiam Girdwood 	}
13748a978234SLiam Girdwood 	return kc;
13758a978234SLiam Girdwood 
13768a978234SLiam Girdwood err_str:
13778a978234SLiam Girdwood 	kfree(sm);
13788a978234SLiam Girdwood err:
1379267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
13808a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1381267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1382267e2c6fSLiam Girdwood 	}
13838a978234SLiam Girdwood 	kfree(kc);
13848a978234SLiam Girdwood 	return NULL;
13858a978234SLiam Girdwood }
13868a978234SLiam Girdwood 
13878a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
13881a7dd6e2SMengdong Lin 	struct soc_tplg *tplg, int num_kcontrols)
13898a978234SLiam Girdwood {
13908a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13918a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
13928a978234SLiam Girdwood 	struct soc_enum *se;
1393*3cde818cSAmadeusz Sławiński 	int i, err;
13948a978234SLiam Girdwood 
13951a7dd6e2SMengdong Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13961a7dd6e2SMengdong Lin 	if (kc == NULL)
13971a7dd6e2SMengdong Lin 		return NULL;
13981a7dd6e2SMengdong Lin 
13991a7dd6e2SMengdong Lin 	for (i = 0; i < num_kcontrols; i++) {
14008a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
14018a978234SLiam Girdwood 		/* validate kcontrol */
14028a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
14038a978234SLiam Girdwood 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1404f3ee9096SChristophe JAILLET 			goto err;
14058a978234SLiam Girdwood 
14068a978234SLiam Girdwood 		se = kzalloc(sizeof(*se), GFP_KERNEL);
14078a978234SLiam Girdwood 		if (se == NULL)
14088a978234SLiam Girdwood 			goto err;
14098a978234SLiam Girdwood 
141002b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
141102b64245SLiam Girdwood 				ec->priv.size);
141202b64245SLiam Girdwood 
14138a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
14148a978234SLiam Girdwood 			ec->hdr.name);
14158a978234SLiam Girdwood 
1416267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
141765030ff3SDan Carpenter 		if (kc[i].name == NULL) {
141865030ff3SDan Carpenter 			kfree(se);
1419267e2c6fSLiam Girdwood 			goto err_se;
142065030ff3SDan Carpenter 		}
14211a7dd6e2SMengdong Lin 		kc[i].private_value = (long)se;
14221a7dd6e2SMengdong Lin 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
14231a7dd6e2SMengdong Lin 		kc[i].access = ec->hdr.access;
14248a978234SLiam Girdwood 
14258a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
14268a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
14271a7dd6e2SMengdong Lin 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
14281a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FL);
14291a7dd6e2SMengdong Lin 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
14301a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FR);
14318a978234SLiam Girdwood 
14328a978234SLiam Girdwood 		se->items = ec->items;
14338a978234SLiam Girdwood 		se->mask = ec->mask;
14348a978234SLiam Girdwood 		se->dobj.index = tplg->index;
14358a978234SLiam Girdwood 
14365aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(ec->hdr.ops.info)) {
14378a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
14388a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14398a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
14408a978234SLiam Girdwood 			if (err < 0) {
14418a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
14428a978234SLiam Girdwood 					ec->hdr.name);
14438a978234SLiam Girdwood 				goto err_se;
14448a978234SLiam Girdwood 			}
14459c6c4d96STakashi Iwai 			/* fall through */
14468a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
14478a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14488a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14498a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
14508a978234SLiam Girdwood 			if (err < 0) {
14518a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
14528a978234SLiam Girdwood 					ec->hdr.name);
14538a978234SLiam Girdwood 				goto err_se;
14548a978234SLiam Girdwood 			}
14558a978234SLiam Girdwood 			break;
14568a978234SLiam Girdwood 		default:
14578a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
14588a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
14598a978234SLiam Girdwood 			goto err_se;
14608a978234SLiam Girdwood 		}
14618a978234SLiam Girdwood 
14628a978234SLiam Girdwood 		/* map io handlers */
14631a7dd6e2SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
14648a978234SLiam Girdwood 		if (err) {
14658a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
14668a978234SLiam Girdwood 			goto err_se;
14678a978234SLiam Girdwood 		}
14688a978234SLiam Girdwood 
14698a978234SLiam Girdwood 		/* pass control to driver for optional further init */
14701a7dd6e2SMengdong Lin 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
14718a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)ec);
14728a978234SLiam Girdwood 		if (err < 0) {
14738a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
14748a978234SLiam Girdwood 				ec->hdr.name);
14758a978234SLiam Girdwood 			goto err_se;
14768a978234SLiam Girdwood 		}
14771a7dd6e2SMengdong Lin 	}
14781a7dd6e2SMengdong Lin 
14798a978234SLiam Girdwood 	return kc;
14808a978234SLiam Girdwood 
14818a978234SLiam Girdwood err_se:
14821a7dd6e2SMengdong Lin 	for (; i >= 0; i--) {
14838a978234SLiam Girdwood 		/* free values and texts */
14841a7dd6e2SMengdong Lin 		se = (struct soc_enum *)kc[i].private_value;
14856d5574edSChristophe JAILLET 		if (!se)
14866d5574edSChristophe JAILLET 			continue;
14876d5574edSChristophe JAILLET 
1488*3cde818cSAmadeusz Sławiński 		soc_tplg_denum_remove_values(se);
1489*3cde818cSAmadeusz Sławiński 		soc_tplg_denum_remove_texts(se);
14908a978234SLiam Girdwood 
14918a978234SLiam Girdwood 		kfree(se);
1492267e2c6fSLiam Girdwood 		kfree(kc[i].name);
14931a7dd6e2SMengdong Lin 	}
14948a978234SLiam Girdwood err:
14958a978234SLiam Girdwood 	kfree(kc);
14968a978234SLiam Girdwood 
14978a978234SLiam Girdwood 	return NULL;
14988a978234SLiam Girdwood }
14998a978234SLiam Girdwood 
15008a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
15018a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
15028a978234SLiam Girdwood {
15038a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
15048a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
15058a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
15068a978234SLiam Girdwood 	int i, err;
15078a978234SLiam Girdwood 
15084ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
15098a978234SLiam Girdwood 	if (!kc)
15108a978234SLiam Girdwood 		return NULL;
15118a978234SLiam Girdwood 
15128a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
15138a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
15148a978234SLiam Girdwood 
15158a978234SLiam Girdwood 		/* validate kcontrol */
15168a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15178a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15188a978234SLiam Girdwood 			goto err;
15198a978234SLiam Girdwood 
15208a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
15218a978234SLiam Girdwood 		if (sbe == NULL)
15228a978234SLiam Girdwood 			goto err;
15238a978234SLiam Girdwood 
15248a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
15255aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(be->priv.size));
15268a978234SLiam Girdwood 
15278a978234SLiam Girdwood 		dev_dbg(tplg->dev,
15288a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
15298a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
15308a978234SLiam Girdwood 
1531267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
153265030ff3SDan Carpenter 		if (kc[i].name == NULL) {
153365030ff3SDan Carpenter 			kfree(sbe);
1534267e2c6fSLiam Girdwood 			goto err;
153565030ff3SDan Carpenter 		}
15368a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
15378a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
15388a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
15398a978234SLiam Girdwood 
15408a978234SLiam Girdwood 		sbe->max = be->max;
15418a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
15428a978234SLiam Girdwood 
15438a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
15442b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
15458a978234SLiam Girdwood 		if (err) {
15468a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
15478a978234SLiam Girdwood 			kfree(sbe);
15488a978234SLiam Girdwood 			continue;
15498a978234SLiam Girdwood 		}
15508a978234SLiam Girdwood 
15518a978234SLiam Girdwood 		/* pass control to driver for optional further init */
15528a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
15538a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
15548a978234SLiam Girdwood 		if (err < 0) {
15558a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
15568a978234SLiam Girdwood 				be->hdr.name);
15578a978234SLiam Girdwood 			kfree(sbe);
15588a978234SLiam Girdwood 			continue;
15598a978234SLiam Girdwood 		}
15608a978234SLiam Girdwood 	}
15618a978234SLiam Girdwood 
15628a978234SLiam Girdwood 	return kc;
15638a978234SLiam Girdwood 
15648a978234SLiam Girdwood err:
1565267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
15668a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1567267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1568267e2c6fSLiam Girdwood 	}
15698a978234SLiam Girdwood 
15708a978234SLiam Girdwood 	kfree(kc);
15718a978234SLiam Girdwood 	return NULL;
15728a978234SLiam Girdwood }
15738a978234SLiam Girdwood 
15748a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
15758a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
15768a978234SLiam Girdwood {
15778a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
15788a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
15798a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
15808a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1581eea3dd4fSMengdong Lin 	unsigned int kcontrol_type;
15828a978234SLiam Girdwood 	int ret = 0;
15838a978234SLiam Girdwood 
15848a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15858a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15868a978234SLiam Girdwood 		return -EINVAL;
15878a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15888a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15898a978234SLiam Girdwood 		return -EINVAL;
15908a978234SLiam Girdwood 
15918a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
15928a978234SLiam Girdwood 		w->name, w->id);
15938a978234SLiam Girdwood 
15948a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
15958a978234SLiam Girdwood 
15968a978234SLiam Girdwood 	/* map user to kernel widget ID */
15975aebe7c7SPierre-Louis Bossart 	template.id = get_widget_id(le32_to_cpu(w->id));
15988a978234SLiam Girdwood 	if (template.id < 0)
15998a978234SLiam Girdwood 		return template.id;
16008a978234SLiam Girdwood 
1601c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
16028a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
16038a978234SLiam Girdwood 	if (!template.name)
16048a978234SLiam Girdwood 		return -ENOMEM;
16058a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
16068a978234SLiam Girdwood 	if (!template.sname) {
16078a978234SLiam Girdwood 		ret = -ENOMEM;
16088a978234SLiam Girdwood 		goto err;
16098a978234SLiam Girdwood 	}
16105aebe7c7SPierre-Louis Bossart 	template.reg = le32_to_cpu(w->reg);
16115aebe7c7SPierre-Louis Bossart 	template.shift = le32_to_cpu(w->shift);
16125aebe7c7SPierre-Louis Bossart 	template.mask = le32_to_cpu(w->mask);
16135aebe7c7SPierre-Louis Bossart 	template.subseq = le32_to_cpu(w->subseq);
16148a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
16158a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
16165aebe7c7SPierre-Louis Bossart 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
16175aebe7c7SPierre-Louis Bossart 	template.event_flags = le16_to_cpu(w->event_flags);
16188a978234SLiam Girdwood 	template.dobj.index = tplg->index;
16198a978234SLiam Girdwood 
16208a978234SLiam Girdwood 	tplg->pos +=
16215aebe7c7SPierre-Louis Bossart 		(sizeof(struct snd_soc_tplg_dapm_widget) +
16225aebe7c7SPierre-Louis Bossart 		 le32_to_cpu(w->priv.size));
16235aebe7c7SPierre-Louis Bossart 
16248a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
1625dd5abb74SArnd Bergmann 		kcontrol_type = 0;
16268a978234SLiam Girdwood 		template.num_kcontrols = 0;
16278a978234SLiam Girdwood 		goto widget;
16288a978234SLiam Girdwood 	}
16298a978234SLiam Girdwood 
16308a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
16318a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
16328a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
16338a978234SLiam Girdwood 
16345aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(control_hdr->ops.info)) {
16358a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
16368a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
16378a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
16388a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
16398a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
16408a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1641eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
16425aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16438a978234SLiam Girdwood 		template.kcontrol_news =
16448a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
16458a978234SLiam Girdwood 			template.num_kcontrols);
16468a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16478a978234SLiam Girdwood 			ret = -ENOMEM;
16488a978234SLiam Girdwood 			goto hdr_err;
16498a978234SLiam Girdwood 		}
16508a978234SLiam Girdwood 		break;
16518a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
16528a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
16538a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
16548a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
16558a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1656eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
16575aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16588a978234SLiam Girdwood 		template.kcontrol_news =
16591a7dd6e2SMengdong Lin 			soc_tplg_dapm_widget_denum_create(tplg,
16601a7dd6e2SMengdong Lin 			template.num_kcontrols);
16618a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16628a978234SLiam Girdwood 			ret = -ENOMEM;
16638a978234SLiam Girdwood 			goto hdr_err;
16648a978234SLiam Girdwood 		}
16658a978234SLiam Girdwood 		break;
16668a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
1667eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
16685aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16698a978234SLiam Girdwood 		template.kcontrol_news =
16708a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
16718a978234SLiam Girdwood 				template.num_kcontrols);
16728a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16738a978234SLiam Girdwood 			ret = -ENOMEM;
16748a978234SLiam Girdwood 			goto hdr_err;
16758a978234SLiam Girdwood 		}
16768a978234SLiam Girdwood 		break;
16778a978234SLiam Girdwood 	default:
16788a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
16798a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
16805aebe7c7SPierre-Louis Bossart 			le32_to_cpu(control_hdr->ops.info));
16818a978234SLiam Girdwood 		ret = -EINVAL;
16828a978234SLiam Girdwood 		goto hdr_err;
16838a978234SLiam Girdwood 	}
16848a978234SLiam Girdwood 
16858a978234SLiam Girdwood widget:
16868a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
16878a978234SLiam Girdwood 	if (ret < 0)
16888a978234SLiam Girdwood 		goto hdr_err;
16898a978234SLiam Girdwood 
16908a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
16918a978234SLiam Girdwood 	 * data during sound card init. */
16928a978234SLiam Girdwood 	if (card->instantiated)
16938a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
16948a978234SLiam Girdwood 	else
16958a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
169637e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
169737e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
16988a978234SLiam Girdwood 		goto hdr_err;
16998a978234SLiam Girdwood 	}
17008a978234SLiam Girdwood 
17018a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1702eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
17038a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
17048a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
17058a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1706ebd259d3SLiam Girdwood 
1707ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1708ebd259d3SLiam Girdwood 	if (ret < 0)
1709ebd259d3SLiam Girdwood 		goto ready_err;
1710ebd259d3SLiam Girdwood 
17117620fe91SBard liao 	kfree(template.sname);
17127620fe91SBard liao 	kfree(template.name);
17137620fe91SBard liao 
17148a978234SLiam Girdwood 	return 0;
17158a978234SLiam Girdwood 
1716ebd259d3SLiam Girdwood ready_err:
1717ebd259d3SLiam Girdwood 	snd_soc_tplg_widget_remove(widget);
1718ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
17198a978234SLiam Girdwood hdr_err:
17208a978234SLiam Girdwood 	kfree(template.sname);
17218a978234SLiam Girdwood err:
17228a978234SLiam Girdwood 	kfree(template.name);
17238a978234SLiam Girdwood 	return ret;
17248a978234SLiam Girdwood }
17258a978234SLiam Girdwood 
17268a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
17278a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
17288a978234SLiam Girdwood {
17298a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
17305aebe7c7SPierre-Louis Bossart 	int ret, count, i;
17315aebe7c7SPierre-Louis Bossart 
17325aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
17338a978234SLiam Girdwood 
17348a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
17358a978234SLiam Girdwood 		return 0;
17368a978234SLiam Girdwood 
17378a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
17388a978234SLiam Girdwood 
17398a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
17408a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
17415aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
174206eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
174306eb49f7SMengdong Lin 			return -EINVAL;
174406eb49f7SMengdong Lin 		}
174506eb49f7SMengdong Lin 
17468a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
17477de76b62SMengdong Lin 		if (ret < 0) {
17488a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
17498a978234SLiam Girdwood 				widget->name);
17507de76b62SMengdong Lin 			return ret;
17517de76b62SMengdong Lin 		}
17528a978234SLiam Girdwood 	}
17538a978234SLiam Girdwood 
17548a978234SLiam Girdwood 	return 0;
17558a978234SLiam Girdwood }
17568a978234SLiam Girdwood 
17578a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
17588a978234SLiam Girdwood {
17598a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
17608a978234SLiam Girdwood 	int ret;
17618a978234SLiam Girdwood 
17628a978234SLiam Girdwood 	/* Card might not have been registered at this point.
17638a978234SLiam Girdwood 	 * If so, just return success.
17648a978234SLiam Girdwood 	*/
17658a978234SLiam Girdwood 	if (!card || !card->instantiated) {
17668a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1767cc9d4714SLiam Girdwood 			" widget card binding deferred\n");
17688a978234SLiam Girdwood 		return 0;
17698a978234SLiam Girdwood 	}
17708a978234SLiam Girdwood 
17718a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
17728a978234SLiam Girdwood 	if (ret < 0)
17738a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
17748a978234SLiam Girdwood 			ret);
17758a978234SLiam Girdwood 
17768a978234SLiam Girdwood 	return 0;
17778a978234SLiam Girdwood }
17788a978234SLiam Girdwood 
1779b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream,
1780b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1781b6b6e4d6SMengdong Lin {
1782b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
17835aebe7c7SPierre-Louis Bossart 	stream->channels_min = le32_to_cpu(caps->channels_min);
17845aebe7c7SPierre-Louis Bossart 	stream->channels_max = le32_to_cpu(caps->channels_max);
17855aebe7c7SPierre-Louis Bossart 	stream->rates = le32_to_cpu(caps->rates);
17865aebe7c7SPierre-Louis Bossart 	stream->rate_min = le32_to_cpu(caps->rate_min);
17875aebe7c7SPierre-Louis Bossart 	stream->rate_max = le32_to_cpu(caps->rate_max);
17885aebe7c7SPierre-Louis Bossart 	stream->formats = le64_to_cpu(caps->formats);
17895aebe7c7SPierre-Louis Bossart 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1790b6b6e4d6SMengdong Lin }
1791b6b6e4d6SMengdong Lin 
17920038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
17930038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
17940038be9aSMengdong Lin {
17950038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
17960038be9aSMengdong Lin 		dai_drv->symmetric_rates =
17970038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
17980038be9aSMengdong Lin 
17990038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
18000038be9aSMengdong Lin 		dai_drv->symmetric_channels =
18010038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
18020038be9aSMengdong Lin 			1 : 0;
18030038be9aSMengdong Lin 
18040038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
18050038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
18060038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
18070038be9aSMengdong Lin 			1 : 0;
18080038be9aSMengdong Lin }
18090038be9aSMengdong Lin 
181064527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
181164527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
181264527e8aSMengdong Lin {
181364527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
181464527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
181564527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
181664527e8aSMengdong Lin 	int ret;
181764527e8aSMengdong Lin 
181864527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
181964527e8aSMengdong Lin 	if (dai_drv == NULL)
182064527e8aSMengdong Lin 		return -ENOMEM;
182164527e8aSMengdong Lin 
18228f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
18238f27c4abSMengdong Lin 		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
18245aebe7c7SPierre-Louis Bossart 	dai_drv->id = le32_to_cpu(pcm->dai_id);
182564527e8aSMengdong Lin 
182664527e8aSMengdong Lin 	if (pcm->playback) {
182764527e8aSMengdong Lin 		stream = &dai_drv->playback;
182864527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1829b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
183064527e8aSMengdong Lin 	}
183164527e8aSMengdong Lin 
183264527e8aSMengdong Lin 	if (pcm->capture) {
183364527e8aSMengdong Lin 		stream = &dai_drv->capture;
183464527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1835b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
183664527e8aSMengdong Lin 	}
183764527e8aSMengdong Lin 
18385db6aab6SLiam Girdwood 	if (pcm->compress)
18395db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
18405db6aab6SLiam Girdwood 
184164527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1842c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
184364527e8aSMengdong Lin 	if (ret < 0) {
184464527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
18457b6f68a4SBard liao 		kfree(dai_drv->playback.stream_name);
18467b6f68a4SBard liao 		kfree(dai_drv->capture.stream_name);
18477b6f68a4SBard liao 		kfree(dai_drv->name);
184864527e8aSMengdong Lin 		kfree(dai_drv);
184964527e8aSMengdong Lin 		return ret;
185064527e8aSMengdong Lin 	}
185164527e8aSMengdong Lin 
185264527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
185364527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
185464527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
185564527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
185664527e8aSMengdong Lin 
185764527e8aSMengdong Lin 	/* register the DAI to the component */
185864527e8aSMengdong Lin 	return snd_soc_register_dai(tplg->comp, dai_drv);
185964527e8aSMengdong Lin }
186064527e8aSMengdong Lin 
1861717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1862717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1863717a8e72SMengdong Lin {
1864717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1865717a8e72SMengdong Lin 		link->symmetric_rates =
1866717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1867717a8e72SMengdong Lin 
1868717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1869717a8e72SMengdong Lin 		link->symmetric_channels =
1870717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1871717a8e72SMengdong Lin 			1 : 0;
1872717a8e72SMengdong Lin 
1873717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1874717a8e72SMengdong Lin 		link->symmetric_samplebits =
1875717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1876717a8e72SMengdong Lin 			1 : 0;
18776ff67ccaSMengdong Lin 
18786ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
18796ff67ccaSMengdong Lin 		link->ignore_suspend =
18806ff67ccaSMengdong Lin 		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
18816ff67ccaSMengdong Lin 		1 : 0;
1882717a8e72SMengdong Lin }
1883717a8e72SMengdong Lin 
188467d1c21eSGuneshwor Singh /* create the FE DAI link */
1885ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1886acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1887acfc7d46SMengdong Lin {
1888acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
188923b946ceSKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
1890acfc7d46SMengdong Lin 	int ret;
1891acfc7d46SMengdong Lin 
18923e6de894SPierre-Louis Bossart 	/* link + cpu + codec + platform */
18933e6de894SPierre-Louis Bossart 	link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1894acfc7d46SMengdong Lin 	if (link == NULL)
1895acfc7d46SMengdong Lin 		return -ENOMEM;
1896acfc7d46SMengdong Lin 
189723b946ceSKuninori Morimoto 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
189823b946ceSKuninori Morimoto 
189923b946ceSKuninori Morimoto 	link->cpus	= &dlc[0];
190023b946ceSKuninori Morimoto 	link->codecs	= &dlc[1];
19013e6de894SPierre-Louis Bossart 	link->platforms	= &dlc[2];
190223b946ceSKuninori Morimoto 
190323b946ceSKuninori Morimoto 	link->num_cpus	 = 1;
190423b946ceSKuninori Morimoto 	link->num_codecs = 1;
19053e6de894SPierre-Louis Bossart 	link->num_platforms = 1;
190623b946ceSKuninori Morimoto 
19078f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
19088f27c4abSMengdong Lin 		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
19098f27c4abSMengdong Lin 		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
19108f27c4abSMengdong Lin 	}
19115aebe7c7SPierre-Louis Bossart 	link->id = le32_to_cpu(pcm->pcm_id);
1912acfc7d46SMengdong Lin 
19138f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
191423b946ceSKuninori Morimoto 		link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
19158f27c4abSMengdong Lin 
191623b946ceSKuninori Morimoto 	link->codecs->name = "snd-soc-dummy";
191723b946ceSKuninori Morimoto 	link->codecs->dai_name = "snd-soc-dummy-dai";
191867d1c21eSGuneshwor Singh 
19193e6de894SPierre-Louis Bossart 	link->platforms->name = "snd-soc-dummy";
19203e6de894SPierre-Louis Bossart 
192167d1c21eSGuneshwor Singh 	/* enable DPCM */
192267d1c21eSGuneshwor Singh 	link->dynamic = 1;
19235aebe7c7SPierre-Louis Bossart 	link->dpcm_playback = le32_to_cpu(pcm->playback);
19245aebe7c7SPierre-Louis Bossart 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1925717a8e72SMengdong Lin 	if (pcm->flag_mask)
19265aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
19275aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flag_mask),
19285aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flags));
192967d1c21eSGuneshwor Singh 
1930acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1931c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1932acfc7d46SMengdong Lin 	if (ret < 0) {
1933acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1934b3718b8fSBard liao 		kfree(link->name);
1935b3718b8fSBard liao 		kfree(link->stream_name);
193623b946ceSKuninori Morimoto 		kfree(link->cpus->dai_name);
1937acfc7d46SMengdong Lin 		kfree(link);
1938acfc7d46SMengdong Lin 		return ret;
1939acfc7d46SMengdong Lin 	}
1940acfc7d46SMengdong Lin 
1941acfc7d46SMengdong Lin 	link->dobj.index = tplg->index;
1942acfc7d46SMengdong Lin 	link->dobj.ops = tplg->ops;
1943acfc7d46SMengdong Lin 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1944acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1945acfc7d46SMengdong Lin 
1946acfc7d46SMengdong Lin 	snd_soc_add_dai_link(tplg->comp->card, link);
1947acfc7d46SMengdong Lin 	return 0;
1948acfc7d46SMengdong Lin }
1949acfc7d46SMengdong Lin 
1950acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
195164527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
195264527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
195364527e8aSMengdong Lin {
1954acfc7d46SMengdong Lin 	int ret;
1955acfc7d46SMengdong Lin 
1956acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1957acfc7d46SMengdong Lin 	if (ret < 0)
1958acfc7d46SMengdong Lin 		return ret;
1959acfc7d46SMengdong Lin 
1960ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
196164527e8aSMengdong Lin }
196264527e8aSMengdong Lin 
196355726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
196455726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
196555726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
196655726dc9SMengdong Lin {
19675aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
196855726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
196955726dc9SMengdong Lin 	dest->formats = src->formats;
197055726dc9SMengdong Lin 	dest->rates = src->rates;
197155726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
197255726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
197355726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
197455726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
197555726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
197655726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
197755726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
197855726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
197955726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
198055726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
198155726dc9SMengdong Lin }
198255726dc9SMengdong Lin 
198355726dc9SMengdong Lin /**
198455726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
198555726dc9SMengdong Lin  * @tplg: topology context
198655726dc9SMengdong Lin  * @src: older version of pcm as a source
198755726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
198855726dc9SMengdong Lin  *
198955726dc9SMengdong Lin  * Support from vesion 4. User should free the returned pcm manually.
199055726dc9SMengdong Lin  */
199155726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
199255726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
199355726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
199455726dc9SMengdong Lin {
199555726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
199655726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
199755726dc9SMengdong Lin 	int i;
199855726dc9SMengdong Lin 
199955726dc9SMengdong Lin 	*pcm = NULL;
200055726dc9SMengdong Lin 
20015aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
200255726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
200355726dc9SMengdong Lin 		return -EINVAL;
200455726dc9SMengdong Lin 	}
200555726dc9SMengdong Lin 
200655726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
200755726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
200855726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
200955726dc9SMengdong Lin 	if (!dest)
201055726dc9SMengdong Lin 		return -ENOMEM;
201155726dc9SMengdong Lin 
20125aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
201355726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
201455726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
201555726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
201655726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
201755726dc9SMengdong Lin 	dest->playback = src_v4->playback;
201855726dc9SMengdong Lin 	dest->capture = src_v4->capture;
201955726dc9SMengdong Lin 	dest->compress = src_v4->compress;
202055726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
20215aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
202255726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
202355726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
202455726dc9SMengdong Lin 
202555726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
202655726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
202755726dc9SMengdong Lin 
202855726dc9SMengdong Lin 	*pcm = dest;
202955726dc9SMengdong Lin 	return 0;
203055726dc9SMengdong Lin }
203155726dc9SMengdong Lin 
203264527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
20338a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
20348a978234SLiam Girdwood {
203555726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
20365aebe7c7SPierre-Louis Bossart 	int count;
20375aebe7c7SPierre-Louis Bossart 	int size;
2038fd340455SVinod Koul 	int i;
203955726dc9SMengdong Lin 	bool abi_match;
20408a978234SLiam Girdwood 
20415aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
20425aebe7c7SPierre-Louis Bossart 
20438a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
20448a978234SLiam Girdwood 		return 0;
20458a978234SLiam Girdwood 
204655726dc9SMengdong Lin 	/* check the element size and count */
204755726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
20485aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(pcm->size);
20495aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_pcm)
20505aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
205155726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
20525aebe7c7SPierre-Louis Bossart 			size);
205355726dc9SMengdong Lin 		return -EINVAL;
205455726dc9SMengdong Lin 	}
205555726dc9SMengdong Lin 
20568a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
20575aebe7c7SPierre-Louis Bossart 				      size, count,
20585aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
20595aebe7c7SPierre-Louis Bossart 				      "PCM DAI")) {
20608a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
20618a978234SLiam Girdwood 			count);
20628a978234SLiam Girdwood 		return -EINVAL;
20638a978234SLiam Girdwood 	}
20648a978234SLiam Girdwood 
206564527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
206655726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
20675aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(pcm->size);
206855726dc9SMengdong Lin 
206955726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
207055726dc9SMengdong Lin 		 * if abi not match.
207155726dc9SMengdong Lin 		 */
20725aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*pcm)) {
207355726dc9SMengdong Lin 			abi_match = true;
207455726dc9SMengdong Lin 			_pcm = pcm;
207555726dc9SMengdong Lin 		} else {
207655726dc9SMengdong Lin 			abi_match = false;
2077fd340455SVinod Koul 			pcm_new_ver(tplg, pcm, &_pcm);
207806eb49f7SMengdong Lin 		}
207906eb49f7SMengdong Lin 
208055726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
208155726dc9SMengdong Lin 		soc_tplg_pcm_create(tplg, _pcm);
208255726dc9SMengdong Lin 
2083717a8e72SMengdong Lin 		/* offset by version-specific struct size and
2084717a8e72SMengdong Lin 		 * real priv data size
2085717a8e72SMengdong Lin 		 */
20865aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2087717a8e72SMengdong Lin 
208855726dc9SMengdong Lin 		if (!abi_match)
208955726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
209064527e8aSMengdong Lin 	}
209164527e8aSMengdong Lin 
20928a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
20938a978234SLiam Girdwood 
20948a978234SLiam Girdwood 	return 0;
20958a978234SLiam Girdwood }
20968a978234SLiam Girdwood 
20970038be9aSMengdong Lin /**
2098593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
20998abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
2100593d9e52SMengdong Lin  * @cfg: physical link configs.
2101593d9e52SMengdong Lin  *
2102593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
2103593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
2104593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
2105593d9e52SMengdong Lin  */
2106593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
2107593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
2108593d9e52SMengdong Lin {
2109593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
2110593d9e52SMengdong Lin 	unsigned char bclk_master, fsync_master;
2111593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
2112593d9e52SMengdong Lin 	int i;
2113593d9e52SMengdong Lin 
21145aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2115593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
2116593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
2117593d9e52SMengdong Lin 			continue;
2118593d9e52SMengdong Lin 
21195aebe7c7SPierre-Louis Bossart 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
21205aebe7c7SPierre-Louis Bossart 			SND_SOC_DAIFMT_FORMAT_MASK;
2121593d9e52SMengdong Lin 
2122933e1c4aSKirill Marinushkin 		/* clock gating */
2123fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
2124fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2125933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2126fbeabd09SKirill Marinushkin 			break;
2127fbeabd09SKirill Marinushkin 
2128fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2129933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2130fbeabd09SKirill Marinushkin 			break;
2131fbeabd09SKirill Marinushkin 
2132fbeabd09SKirill Marinushkin 		default:
2133fbeabd09SKirill Marinushkin 			/* ignore the value */
2134fbeabd09SKirill Marinushkin 			break;
2135fbeabd09SKirill Marinushkin 		}
2136933e1c4aSKirill Marinushkin 
2137593d9e52SMengdong Lin 		/* clock signal polarity */
2138593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
2139593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
2140593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
2141593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2142593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
2143593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2144593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
2145593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2146593d9e52SMengdong Lin 		else
2147593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2148593d9e52SMengdong Lin 
2149593d9e52SMengdong Lin 		/* clock masters */
2150a941e2faSKirill Marinushkin 		bclk_master = (hw_config->bclk_master ==
2151a941e2faSKirill Marinushkin 			       SND_SOC_TPLG_BCLK_CM);
2152a941e2faSKirill Marinushkin 		fsync_master = (hw_config->fsync_master ==
2153a941e2faSKirill Marinushkin 				SND_SOC_TPLG_FSYNC_CM);
2154a941e2faSKirill Marinushkin 		if (bclk_master && fsync_master)
2155593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2156593d9e52SMengdong Lin 		else if (!bclk_master && fsync_master)
2157a941e2faSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2158a941e2faSKirill Marinushkin 		else if (bclk_master && !fsync_master)
2159593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2160593d9e52SMengdong Lin 		else
2161593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2162593d9e52SMengdong Lin 	}
2163593d9e52SMengdong Lin }
2164593d9e52SMengdong Lin 
2165593d9e52SMengdong Lin /**
2166593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
2167593d9e52SMengdong Lin  * version of source.
21688abab35fSCharles Keepax  * @tplg: topology context
2169593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
2170593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
2171593d9e52SMengdong Lin  *
2172593d9e52SMengdong Lin  * Support from vesion 4. User need free the returned link config manually.
2173593d9e52SMengdong Lin  */
2174593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
2175593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
2176593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
2177593d9e52SMengdong Lin {
2178593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
2179593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
2180593d9e52SMengdong Lin 	int i;
2181593d9e52SMengdong Lin 
2182593d9e52SMengdong Lin 	*link = NULL;
2183593d9e52SMengdong Lin 
21845aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) !=
21855aebe7c7SPierre-Louis Bossart 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
2186593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2187593d9e52SMengdong Lin 		return -EINVAL;
2188593d9e52SMengdong Lin 	}
2189593d9e52SMengdong Lin 
2190593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2191593d9e52SMengdong Lin 
2192593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2193593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2194593d9e52SMengdong Lin 	if (!dest)
2195593d9e52SMengdong Lin 		return -ENOMEM;
2196593d9e52SMengdong Lin 
21975aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
2198593d9e52SMengdong Lin 	dest->id = src_v4->id;
2199593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
22005aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2201593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2202593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2203593d9e52SMengdong Lin 
2204593d9e52SMengdong Lin 	*link = dest;
2205593d9e52SMengdong Lin 	return 0;
2206593d9e52SMengdong Lin }
2207593d9e52SMengdong Lin 
2208593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2209593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2210593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2211593d9e52SMengdong Lin {
2212593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2213593d9e52SMengdong Lin 	const char *name, *stream_name;
2214dbab1cb8SMengdong Lin 	size_t len;
2215593d9e52SMengdong Lin 	int ret;
2216593d9e52SMengdong Lin 
2217dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2218dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2219dbab1cb8SMengdong Lin 		return -EINVAL;
2220dbab1cb8SMengdong Lin 	else if (len)
2221dbab1cb8SMengdong Lin 		name = cfg->name;
2222dbab1cb8SMengdong Lin 	else
2223dbab1cb8SMengdong Lin 		name = NULL;
2224dbab1cb8SMengdong Lin 
2225dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2226dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2227dbab1cb8SMengdong Lin 		return -EINVAL;
2228dbab1cb8SMengdong Lin 	else if (len)
2229dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2230dbab1cb8SMengdong Lin 	else
2231dbab1cb8SMengdong Lin 		stream_name = NULL;
2232593d9e52SMengdong Lin 
22335aebe7c7SPierre-Louis Bossart 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2234593d9e52SMengdong Lin 				     name, stream_name);
2235593d9e52SMengdong Lin 	if (!link) {
2236593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2237593d9e52SMengdong Lin 			name, cfg->id);
2238593d9e52SMengdong Lin 		return -EINVAL;
2239593d9e52SMengdong Lin 	}
2240593d9e52SMengdong Lin 
2241593d9e52SMengdong Lin 	/* hw format */
2242593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2243593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2244593d9e52SMengdong Lin 
2245593d9e52SMengdong Lin 	/* flags */
2246593d9e52SMengdong Lin 	if (cfg->flag_mask)
22475aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
22485aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flag_mask),
22495aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flags));
2250593d9e52SMengdong Lin 
2251593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2252c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2253593d9e52SMengdong Lin 	if (ret < 0) {
2254593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2255593d9e52SMengdong Lin 		return ret;
2256593d9e52SMengdong Lin 	}
2257593d9e52SMengdong Lin 
2258adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2259adfebb51SBard liao 	link->dobj.index = tplg->index;
2260adfebb51SBard liao 	link->dobj.ops = tplg->ops;
2261adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2262adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2263adfebb51SBard liao 
2264593d9e52SMengdong Lin 	return 0;
2265593d9e52SMengdong Lin }
2266593d9e52SMengdong Lin 
2267593d9e52SMengdong Lin 
2268593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2269593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2270593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2271593d9e52SMengdong Lin {
2272593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
22735aebe7c7SPierre-Louis Bossart 	int count;
22745aebe7c7SPierre-Louis Bossart 	int size;
2275593d9e52SMengdong Lin 	int i, ret;
2276593d9e52SMengdong Lin 	bool abi_match;
2277593d9e52SMengdong Lin 
22785aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
22795aebe7c7SPierre-Louis Bossart 
2280593d9e52SMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_LINK) {
22815aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
22825aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
2283593d9e52SMengdong Lin 		return 0;
2284593d9e52SMengdong Lin 	};
2285593d9e52SMengdong Lin 
2286593d9e52SMengdong Lin 	/* check the element size and count */
2287593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
22885aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(link->size);
22895aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_link_config)
22905aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2291593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
22925aebe7c7SPierre-Louis Bossart 			size);
2293593d9e52SMengdong Lin 		return -EINVAL;
2294593d9e52SMengdong Lin 	}
2295593d9e52SMengdong Lin 
2296593d9e52SMengdong Lin 	if (soc_tplg_check_elem_count(tplg,
22975aebe7c7SPierre-Louis Bossart 				      size, count,
22985aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
22995aebe7c7SPierre-Louis Bossart 				      "physical link config")) {
2300593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2301593d9e52SMengdong Lin 			count);
2302593d9e52SMengdong Lin 		return -EINVAL;
2303593d9e52SMengdong Lin 	}
2304593d9e52SMengdong Lin 
2305593d9e52SMengdong Lin 	/* config physical DAI links */
2306593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2307593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
23085aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(link->size);
23095aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*link)) {
2310593d9e52SMengdong Lin 			abi_match = true;
2311593d9e52SMengdong Lin 			_link = link;
2312593d9e52SMengdong Lin 		} else {
2313593d9e52SMengdong Lin 			abi_match = false;
2314593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2315593d9e52SMengdong Lin 			if (ret < 0)
2316593d9e52SMengdong Lin 				return ret;
2317593d9e52SMengdong Lin 		}
2318593d9e52SMengdong Lin 
2319593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
2320593d9e52SMengdong Lin 		if (ret < 0)
2321593d9e52SMengdong Lin 			return ret;
2322593d9e52SMengdong Lin 
2323593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2324593d9e52SMengdong Lin 		 * real priv data size
2325593d9e52SMengdong Lin 		 */
23265aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2327593d9e52SMengdong Lin 
2328593d9e52SMengdong Lin 		if (!abi_match)
2329593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2330593d9e52SMengdong Lin 	}
2331593d9e52SMengdong Lin 
2332593d9e52SMengdong Lin 	return 0;
2333593d9e52SMengdong Lin }
2334593d9e52SMengdong Lin 
2335593d9e52SMengdong Lin /**
23369aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
23370038be9aSMengdong Lin  * @tplg: topology context
23389aa3f034SMengdong Lin  * @d: physical DAI configs.
23390038be9aSMengdong Lin  *
23409aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
23419aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
23420038be9aSMengdong Lin  */
23439aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
23449aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
23450038be9aSMengdong Lin {
23465aebe7c7SPierre-Louis Bossart 	struct snd_soc_dai_link_component dai_component;
23470038be9aSMengdong Lin 	struct snd_soc_dai *dai;
23480038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
23490038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
23500038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
23510038be9aSMengdong Lin 	int ret;
23520038be9aSMengdong Lin 
23535aebe7c7SPierre-Louis Bossart 	memset(&dai_component, 0, sizeof(dai_component));
23545aebe7c7SPierre-Louis Bossart 
23559aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
23560038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
23570038be9aSMengdong Lin 	if (!dai) {
23589aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
23599aa3f034SMengdong Lin 			d->dai_name);
23600038be9aSMengdong Lin 		return -EINVAL;
23610038be9aSMengdong Lin 	}
23620038be9aSMengdong Lin 
23635aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(d->dai_id) != dai->id) {
23649aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
23659aa3f034SMengdong Lin 			d->dai_name);
23660038be9aSMengdong Lin 		return -EINVAL;
23670038be9aSMengdong Lin 	}
23680038be9aSMengdong Lin 
23690038be9aSMengdong Lin 	dai_drv = dai->driver;
23700038be9aSMengdong Lin 	if (!dai_drv)
23710038be9aSMengdong Lin 		return -EINVAL;
23720038be9aSMengdong Lin 
23739aa3f034SMengdong Lin 	if (d->playback) {
23740038be9aSMengdong Lin 		stream = &dai_drv->playback;
23759aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
23760038be9aSMengdong Lin 		set_stream_info(stream, caps);
23770038be9aSMengdong Lin 	}
23780038be9aSMengdong Lin 
23799aa3f034SMengdong Lin 	if (d->capture) {
23800038be9aSMengdong Lin 		stream = &dai_drv->capture;
23819aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
23820038be9aSMengdong Lin 		set_stream_info(stream, caps);
23830038be9aSMengdong Lin 	}
23840038be9aSMengdong Lin 
23859aa3f034SMengdong Lin 	if (d->flag_mask)
23865aebe7c7SPierre-Louis Bossart 		set_dai_flags(dai_drv,
23875aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flag_mask),
23885aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flags));
23890038be9aSMengdong Lin 
23900038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2391c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
23920038be9aSMengdong Lin 	if (ret < 0) {
23930038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
23940038be9aSMengdong Lin 		return ret;
23950038be9aSMengdong Lin 	}
23960038be9aSMengdong Lin 
23970038be9aSMengdong Lin 	return 0;
23980038be9aSMengdong Lin }
23990038be9aSMengdong Lin 
24009aa3f034SMengdong Lin /* load physical DAI elements */
24019aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
24020038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
24030038be9aSMengdong Lin {
24049aa3f034SMengdong Lin 	struct snd_soc_tplg_dai *dai;
24055aebe7c7SPierre-Louis Bossart 	int count;
24060038be9aSMengdong Lin 	int i;
24070038be9aSMengdong Lin 
24085aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
24095aebe7c7SPierre-Louis Bossart 
24100038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
24110038be9aSMengdong Lin 		return 0;
24120038be9aSMengdong Lin 
24130038be9aSMengdong Lin 	/* config the existing BE DAIs */
24140038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
24159aa3f034SMengdong Lin 		dai = (struct snd_soc_tplg_dai *)tplg->pos;
24165aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
24179aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
24180038be9aSMengdong Lin 			return -EINVAL;
24190038be9aSMengdong Lin 		}
24200038be9aSMengdong Lin 
24219aa3f034SMengdong Lin 		soc_tplg_dai_config(tplg, dai);
24225aebe7c7SPierre-Louis Bossart 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
24230038be9aSMengdong Lin 	}
24240038be9aSMengdong Lin 
24250038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
24260038be9aSMengdong Lin 	return 0;
24270038be9aSMengdong Lin }
24280038be9aSMengdong Lin 
2429583958faSMengdong Lin /**
2430583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2431583958faSMengdong Lin  * of source.
24328abab35fSCharles Keepax  * @tplg: topology context
2433583958faSMengdong Lin  * @src: old version of manifest as a source
2434583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2435583958faSMengdong Lin  *
2436583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
2437583958faSMengdong Lin  */
2438583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2439583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2440583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2441583958faSMengdong Lin {
2442583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2443583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
24445aebe7c7SPierre-Louis Bossart 	int size;
2445583958faSMengdong Lin 
2446583958faSMengdong Lin 	*manifest = NULL;
2447583958faSMengdong Lin 
24485aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(src->size);
24495aebe7c7SPierre-Louis Bossart 	if (size != sizeof(*src_v4)) {
2450ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
24515aebe7c7SPierre-Louis Bossart 			 size);
24525aebe7c7SPierre-Louis Bossart 		if (size)
2453583958faSMengdong Lin 			return -EINVAL;
24545aebe7c7SPierre-Louis Bossart 		src->size = cpu_to_le32(sizeof(*src_v4));
2455583958faSMengdong Lin 	}
2456583958faSMengdong Lin 
2457583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2458583958faSMengdong Lin 
2459583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
24605aebe7c7SPierre-Louis Bossart 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
24615aebe7c7SPierre-Louis Bossart 		       GFP_KERNEL);
2462583958faSMengdong Lin 	if (!dest)
2463583958faSMengdong Lin 		return -ENOMEM;
2464583958faSMengdong Lin 
24655aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2466583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2467583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2468583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2469583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2470583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2471583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2472583958faSMengdong Lin 	if (dest->priv.size)
2473583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
24745aebe7c7SPierre-Louis Bossart 		       le32_to_cpu(src_v4->priv.size));
2475583958faSMengdong Lin 
2476583958faSMengdong Lin 	*manifest = dest;
2477583958faSMengdong Lin 	return 0;
2478583958faSMengdong Lin }
24790038be9aSMengdong Lin 
24808a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
24818a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
24828a978234SLiam Girdwood {
2483583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2484583958faSMengdong Lin 	bool abi_match;
2485583958faSMengdong Lin 	int err;
24868a978234SLiam Girdwood 
24878a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
24888a978234SLiam Girdwood 		return 0;
24898a978234SLiam Girdwood 
24908a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2491583958faSMengdong Lin 
2492583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
24935aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2494583958faSMengdong Lin 		abi_match = true;
2495583958faSMengdong Lin 		_manifest = manifest;
2496583958faSMengdong Lin 	} else {
2497583958faSMengdong Lin 		abi_match = false;
2498583958faSMengdong Lin 		err = manifest_new_ver(tplg, manifest, &_manifest);
2499583958faSMengdong Lin 		if (err < 0)
2500583958faSMengdong Lin 			return err;
250106eb49f7SMengdong Lin 	}
250206eb49f7SMengdong Lin 
2503583958faSMengdong Lin 	/* pass control to component driver for optional further init */
25048a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
2505c60b613aSLiam Girdwood 		return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
25068a978234SLiam Girdwood 
2507583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2508583958faSMengdong Lin 		kfree(_manifest);
2509583958faSMengdong Lin 
25108a978234SLiam Girdwood 	return 0;
25118a978234SLiam Girdwood }
25128a978234SLiam Girdwood 
25138a978234SLiam Girdwood /* validate header magic, size and type */
25148a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
25158a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
25168a978234SLiam Girdwood {
25178a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
25188a978234SLiam Girdwood 		return 0;
25198a978234SLiam Girdwood 
25205aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
252106eb49f7SMengdong Lin 		dev_err(tplg->dev,
252206eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
25235aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
252406eb49f7SMengdong Lin 			tplg->fw->size);
252506eb49f7SMengdong Lin 		return -EINVAL;
252606eb49f7SMengdong Lin 	}
252706eb49f7SMengdong Lin 
25288a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
25292114171dSPierre-Louis Bossart 	if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
25308a978234SLiam Girdwood 		dev_err(tplg->dev,
25318a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
25328a978234SLiam Girdwood 			tplg->pass, hdr->magic,
25338a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
25348a978234SLiam Girdwood 		return -EINVAL;
25358a978234SLiam Girdwood 	}
25368a978234SLiam Girdwood 
25375aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
25388a978234SLiam Girdwood 		dev_err(tplg->dev,
25398a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
25408a978234SLiam Girdwood 			tplg->pass, hdr->magic,
25418a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
25428a978234SLiam Girdwood 		return -EINVAL;
25438a978234SLiam Girdwood 	}
25448a978234SLiam Girdwood 
2545288b8da7SMengdong Lin 	/* Support ABI from version 4 */
25465aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
25475aebe7c7SPierre-Louis Bossart 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
25488a978234SLiam Girdwood 		dev_err(tplg->dev,
25498a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
25508a978234SLiam Girdwood 			tplg->pass, hdr->abi,
25518a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
25528a978234SLiam Girdwood 			tplg->fw->size);
25538a978234SLiam Girdwood 		return -EINVAL;
25548a978234SLiam Girdwood 	}
25558a978234SLiam Girdwood 
25568a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
25578a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
25588a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
25598a978234SLiam Girdwood 		return -EINVAL;
25608a978234SLiam Girdwood 	}
25618a978234SLiam Girdwood 
25625aebe7c7SPierre-Louis Bossart 	if (tplg->pass == le32_to_cpu(hdr->type))
25638a978234SLiam Girdwood 		dev_dbg(tplg->dev,
25648a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
25658a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
25668a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
25678a978234SLiam Girdwood 
25688a978234SLiam Girdwood 	return 1;
25698a978234SLiam Girdwood }
25708a978234SLiam Girdwood 
25718a978234SLiam Girdwood /* check header type and call appropriate handler */
25728a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
25738a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
25748a978234SLiam Girdwood {
25758a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
25768a978234SLiam Girdwood 
25778a978234SLiam Girdwood 	/* check for matching ID */
25785aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->index) != tplg->req_index &&
2579bb97142bSLiam Girdwood 		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
25808a978234SLiam Girdwood 		return 0;
25818a978234SLiam Girdwood 
25825aebe7c7SPierre-Louis Bossart 	tplg->index = le32_to_cpu(hdr->index);
25838a978234SLiam Girdwood 
25845aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(hdr->type)) {
25858a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
25868a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
25878a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
25888a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
25898a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
25908a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
25918a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
25928a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
25938a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
259464527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
25953fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
25969aa3f034SMengdong Lin 		return soc_tplg_dai_elems_load(tplg, hdr);
2597593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2598593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2599593d9e52SMengdong Lin 		/* physical link configurations */
2600593d9e52SMengdong Lin 		return soc_tplg_link_elems_load(tplg, hdr);
26018a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
26028a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
26038a978234SLiam Girdwood 	default:
26048a978234SLiam Girdwood 		/* bespoke vendor data object */
26058a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
26068a978234SLiam Girdwood 	}
26078a978234SLiam Girdwood 
26088a978234SLiam Girdwood 	return 0;
26098a978234SLiam Girdwood }
26108a978234SLiam Girdwood 
26118a978234SLiam Girdwood /* process the topology file headers */
26128a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
26138a978234SLiam Girdwood {
26148a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
26158a978234SLiam Girdwood 	int ret;
26168a978234SLiam Girdwood 
26178a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
26188a978234SLiam Girdwood 
26198a978234SLiam Girdwood 	/* process the header types from start to end */
26208a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
26218a978234SLiam Girdwood 
26228a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
26238a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
26248a978234SLiam Girdwood 
26258a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
26268a978234SLiam Girdwood 
26278a978234SLiam Girdwood 			/* make sure header is valid before loading */
26288a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
26298a978234SLiam Girdwood 			if (ret < 0)
26308a978234SLiam Girdwood 				return ret;
26318a978234SLiam Girdwood 			else if (ret == 0)
26328a978234SLiam Girdwood 				break;
26338a978234SLiam Girdwood 
26348a978234SLiam Girdwood 			/* load the header object */
26358a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
26368a978234SLiam Girdwood 			if (ret < 0)
26378a978234SLiam Girdwood 				return ret;
26388a978234SLiam Girdwood 
26398a978234SLiam Girdwood 			/* goto next header */
26405aebe7c7SPierre-Louis Bossart 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
26418a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
26428a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
26438a978234SLiam Girdwood 		}
26448a978234SLiam Girdwood 
26458a978234SLiam Girdwood 		/* next data type pass */
26468a978234SLiam Girdwood 		tplg->pass++;
26478a978234SLiam Girdwood 	}
26488a978234SLiam Girdwood 
26498a978234SLiam Girdwood 	/* signal DAPM we are complete */
26508a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
26518a978234SLiam Girdwood 	if (ret < 0)
26528a978234SLiam Girdwood 		dev_err(tplg->dev,
26538a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
26548a978234SLiam Girdwood 
26558a978234SLiam Girdwood 	return ret;
26568a978234SLiam Girdwood }
26578a978234SLiam Girdwood 
26588a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
26598a978234SLiam Girdwood {
26608a978234SLiam Girdwood 	int ret;
26618a978234SLiam Girdwood 
26628a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
26638a978234SLiam Girdwood 	if (ret == 0)
26648a978234SLiam Girdwood 		soc_tplg_complete(tplg);
26658a978234SLiam Girdwood 
26668a978234SLiam Girdwood 	return ret;
26678a978234SLiam Girdwood }
26688a978234SLiam Girdwood 
26698a978234SLiam Girdwood /* load audio component topology from "firmware" file */
26708a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
26718a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
26728a978234SLiam Girdwood {
26738a978234SLiam Girdwood 	struct soc_tplg tplg;
2674304017d3SBard liao 	int ret;
26758a978234SLiam Girdwood 
26768a978234SLiam Girdwood 	/* setup parsing context */
26778a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
26788a978234SLiam Girdwood 	tplg.fw = fw;
26798a978234SLiam Girdwood 	tplg.dev = comp->dev;
26808a978234SLiam Girdwood 	tplg.comp = comp;
26818a978234SLiam Girdwood 	tplg.ops = ops;
26828a978234SLiam Girdwood 	tplg.req_index = id;
26838a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
26848a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
26851a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
26861a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
26878a978234SLiam Girdwood 
2688304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2689304017d3SBard liao 	/* free the created components if fail to load topology */
2690304017d3SBard liao 	if (ret)
2691304017d3SBard liao 		snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2692304017d3SBard liao 
2693304017d3SBard liao 	return ret;
26948a978234SLiam Girdwood }
26958a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
26968a978234SLiam Girdwood 
26978a978234SLiam Girdwood /* remove this dynamic widget */
26988a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
26998a978234SLiam Girdwood {
27008a978234SLiam Girdwood 	/* make sure we are a widget */
27018a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
27028a978234SLiam Girdwood 		return;
27038a978234SLiam Girdwood 
27048a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
27058a978234SLiam Girdwood }
27068a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
27078a978234SLiam Girdwood 
27088a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
27098a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
27108a978234SLiam Girdwood 	u32 index)
27118a978234SLiam Girdwood {
27128a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
27138a978234SLiam Girdwood 
27148a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
27158a978234SLiam Girdwood 
27168a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
27178a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
27188a978234SLiam Girdwood 			continue;
27198a978234SLiam Girdwood 
27208a978234SLiam Girdwood 		/* match ID */
27218a978234SLiam Girdwood 		if (w->dobj.index != index &&
27228a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
27238a978234SLiam Girdwood 			continue;
27248a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
27258a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2726b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
27278a978234SLiam Girdwood 	}
2728fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
27298a978234SLiam Girdwood }
27308a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
27318a978234SLiam Girdwood 
27328a978234SLiam Girdwood /* remove dynamic controls from the component driver */
27338a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
27348a978234SLiam Girdwood {
27358a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
27368a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
27378a978234SLiam Girdwood 
27388a978234SLiam Girdwood 	/* process the header types from end to start */
27398a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
27408a978234SLiam Girdwood 
27418a978234SLiam Girdwood 		/* remove mixer controls */
27428a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
27438a978234SLiam Girdwood 			list) {
27448a978234SLiam Girdwood 
27458a978234SLiam Girdwood 			/* match index */
27468a978234SLiam Girdwood 			if (dobj->index != index &&
2747feb12f0cSYan Wang 				index != SND_SOC_TPLG_INDEX_ALL)
27488a978234SLiam Girdwood 				continue;
27498a978234SLiam Girdwood 
27508a978234SLiam Girdwood 			switch (dobj->type) {
27518a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
27528a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
27538a978234SLiam Girdwood 				break;
27548a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
27558a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
27568a978234SLiam Girdwood 				break;
27578a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
27588a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
27598a978234SLiam Girdwood 				break;
27607df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
27617df04ea7SRanjani Sridharan 				remove_route(comp, dobj, pass);
27627df04ea7SRanjani Sridharan 				break;
27638a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
27648a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
27658a978234SLiam Girdwood 				break;
27668a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
276764527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
27688a978234SLiam Girdwood 				break;
2769acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2770acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2771acfc7d46SMengdong Lin 				break;
2772adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2773adfebb51SBard liao 				/*
2774adfebb51SBard liao 				 * call link_unload ops if extra
2775adfebb51SBard liao 				 * deinitialization is needed.
2776adfebb51SBard liao 				 */
2777adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2778adfebb51SBard liao 				break;
27798a978234SLiam Girdwood 			default:
27808a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
27818a978234SLiam Girdwood 					dobj->type);
27828a978234SLiam Girdwood 				break;
27838a978234SLiam Girdwood 			}
27848a978234SLiam Girdwood 		}
27858a978234SLiam Girdwood 		pass--;
27868a978234SLiam Girdwood 	}
27878a978234SLiam Girdwood 
27888a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
27898a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
27908a978234SLiam Girdwood }
27918a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2792