xref: /openbmc/linux/sound/soc/soc-topology.c (revision 2ae548f3)
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);
833cde818cSAmadeusz Sławiński static void soc_tplg_denum_remove_texts(struct soc_enum *se);
843cde818cSAmadeusz Sławiński static void soc_tplg_denum_remove_values(struct soc_enum *se);
858a978234SLiam Girdwood 
868a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
878a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
888a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
898a978234SLiam Girdwood {
908a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
918a978234SLiam Girdwood 
928a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
938a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
948a978234SLiam Girdwood 			elem_type);
958a978234SLiam Girdwood 		return -EINVAL;
968a978234SLiam Girdwood 	}
978a978234SLiam Girdwood 
988a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
998a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1008a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1018a978234SLiam Girdwood 		dev_err(tplg->dev,
1028a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1038a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1048a978234SLiam Girdwood 		return -EINVAL;
1058a978234SLiam Girdwood 	}
1068a978234SLiam Girdwood 
1078a978234SLiam Girdwood 	return 0;
1088a978234SLiam Girdwood }
1098a978234SLiam Girdwood 
1108a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1118a978234SLiam Girdwood {
1128a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1138a978234SLiam Girdwood 
1148a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1158a978234SLiam Girdwood 		return 1;
1168a978234SLiam Girdwood 	return 0;
1178a978234SLiam Girdwood }
1188a978234SLiam Girdwood 
1198a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1208a978234SLiam Girdwood {
1218a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1228a978234SLiam Girdwood }
1238a978234SLiam Girdwood 
1248a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1258a978234SLiam Girdwood {
1268a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1278a978234SLiam Girdwood }
1288a978234SLiam Girdwood 
1298a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1308a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1318a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1328a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1338a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1348a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1358a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1368a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1378a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1388a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1398a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1408a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1418a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1428a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1438a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1448a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1458a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1468a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1478a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1482c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1498a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1508a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1518a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1528a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1538a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1548a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1558a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1568a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1578a978234SLiam Girdwood };
1588a978234SLiam Girdwood 
1598a978234SLiam Girdwood struct soc_tplg_map {
1608a978234SLiam Girdwood 	int uid;
1618a978234SLiam Girdwood 	int kid;
1628a978234SLiam Girdwood };
1638a978234SLiam Girdwood 
1648a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1658a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1668a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1678a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1688a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1708a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1818a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1828a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
1838a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
1848a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
1858a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
1868a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
1878a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
1888a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
1898a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
1908a978234SLiam Girdwood };
1918a978234SLiam Girdwood 
1928a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1938a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1948a978234SLiam Girdwood {
1958a978234SLiam Girdwood 	int i;
1968a978234SLiam Girdwood 
1978a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
1985aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
1995aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].reg);
2008a978234SLiam Girdwood 	}
2018a978234SLiam Girdwood 
2028a978234SLiam Girdwood 	return -EINVAL;
2038a978234SLiam Girdwood }
2048a978234SLiam Girdwood 
2058a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2068a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2078a978234SLiam Girdwood {
2088a978234SLiam Girdwood 	int i;
2098a978234SLiam Girdwood 
2108a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2115aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
2125aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].shift);
2138a978234SLiam Girdwood 	}
2148a978234SLiam Girdwood 
2158a978234SLiam Girdwood 	return -EINVAL;
2168a978234SLiam Girdwood }
2178a978234SLiam Girdwood 
2188a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2198a978234SLiam Girdwood {
2208a978234SLiam Girdwood 	int i;
2218a978234SLiam Girdwood 
2228a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2238a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2248a978234SLiam Girdwood 			return dapm_map[i].kid;
2258a978234SLiam Girdwood 	}
2268a978234SLiam Girdwood 
2278a978234SLiam Girdwood 	return -EINVAL;
2288a978234SLiam Girdwood }
2298a978234SLiam Girdwood 
2308a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2318a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2328a978234SLiam Girdwood {
2338a978234SLiam Girdwood 	dev_err(tplg->dev,
2348a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2358a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2368a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2378a978234SLiam Girdwood }
2388a978234SLiam Girdwood 
2398a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2408a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2418a978234SLiam Girdwood {
2428a978234SLiam Girdwood 	dev_err(tplg->dev,
2438a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2448a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2458a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2468a978234SLiam Girdwood }
2478a978234SLiam Girdwood 
2488a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2498a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2508a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2518a978234SLiam Girdwood {
2528a978234SLiam Girdwood 	int ret = 0;
2538a978234SLiam Girdwood 
254c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->vendor_load)
255c60b613aSLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
2568a978234SLiam Girdwood 	else {
2578a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2588a978234SLiam Girdwood 			hdr->vendor_type);
2598a978234SLiam Girdwood 		return -EINVAL;
2608a978234SLiam Girdwood 	}
2618a978234SLiam Girdwood 
2628a978234SLiam Girdwood 	if (ret < 0)
2638a978234SLiam Girdwood 		dev_err(tplg->dev,
2648a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2658a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2668a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2678a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2688a978234SLiam Girdwood 	return ret;
2698a978234SLiam Girdwood }
2708a978234SLiam Girdwood 
2718a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2728a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2738a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2748a978234SLiam Girdwood {
2758a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2768a978234SLiam Girdwood 		return 0;
2778a978234SLiam Girdwood 
2788a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2798a978234SLiam Girdwood }
2808a978234SLiam Girdwood 
2818a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2828a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2838a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2848a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2858a978234SLiam Girdwood {
286c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->widget_load)
287c60b613aSLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
288c60b613aSLiam Girdwood 			tplg_w);
2898a978234SLiam Girdwood 
2908a978234SLiam Girdwood 	return 0;
2918a978234SLiam Girdwood }
2928a978234SLiam Girdwood 
293ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
294ebd259d3SLiam Girdwood  * external widgets where we can assign private data/ops */
295ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg,
296ebd259d3SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
297ebd259d3SLiam Girdwood {
298c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->widget_ready)
299c60b613aSLiam Girdwood 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
300c60b613aSLiam Girdwood 			tplg_w);
301ebd259d3SLiam Girdwood 
302ebd259d3SLiam Girdwood 	return 0;
303ebd259d3SLiam Girdwood }
304ebd259d3SLiam Girdwood 
305183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */
30664527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
307c60b613aSLiam Girdwood 	struct snd_soc_dai_driver *dai_drv,
308c60b613aSLiam Girdwood 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
3098a978234SLiam Girdwood {
310c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dai_load)
311c60b613aSLiam Girdwood 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
312c60b613aSLiam Girdwood 			pcm, dai);
3138a978234SLiam Girdwood 
3148a978234SLiam Girdwood 	return 0;
3158a978234SLiam Girdwood }
3168a978234SLiam Girdwood 
317183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */
318acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
319c60b613aSLiam Girdwood 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
320acfc7d46SMengdong Lin {
321c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->link_load)
322c60b613aSLiam Girdwood 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
323acfc7d46SMengdong Lin 
324acfc7d46SMengdong Lin 	return 0;
325acfc7d46SMengdong Lin }
326acfc7d46SMengdong Lin 
3278a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3288a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3298a978234SLiam Girdwood {
330c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->complete)
3318a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3328a978234SLiam Girdwood }
3338a978234SLiam Girdwood 
3348a978234SLiam Girdwood /* add a dynamic kcontrol */
3358a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3368a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3378a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3388a978234SLiam Girdwood {
3398a978234SLiam Girdwood 	int err;
3408a978234SLiam Girdwood 
3418a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3428a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3438a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3448a978234SLiam Girdwood 		control_new->name);
3458a978234SLiam Girdwood 		return -ENOMEM;
3468a978234SLiam Girdwood 	}
3478a978234SLiam Girdwood 
3488a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3498a978234SLiam Girdwood 	if (err < 0) {
3508a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3518a978234SLiam Girdwood 			control_new->name, err);
3528a978234SLiam Girdwood 		return err;
3538a978234SLiam Girdwood 	}
3548a978234SLiam Girdwood 
3558a978234SLiam Girdwood 	return 0;
3568a978234SLiam Girdwood }
3578a978234SLiam Girdwood 
3588a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3598a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3608a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3618a978234SLiam Girdwood {
3628a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3638a978234SLiam Girdwood 
3648a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
365abca9e4aS이경택 				comp->dev, k, comp->name_prefix, comp, kcontrol);
3668a978234SLiam Girdwood }
3678a978234SLiam Girdwood 
3688a978234SLiam Girdwood /* remove a mixer kcontrol */
3698a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3708a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3718a978234SLiam Girdwood {
3728a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3738a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3748a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3758a978234SLiam Girdwood 	const unsigned int *p = NULL;
3768a978234SLiam Girdwood 
3778a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3788a978234SLiam Girdwood 		return;
3798a978234SLiam Girdwood 
3808a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3818a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3828a978234SLiam Girdwood 
38333ae6ae2SAmadeusz Sławiński 	if (dobj->control.kcontrol->tlv.p)
38433ae6ae2SAmadeusz Sławiński 		p = dobj->control.kcontrol->tlv.p;
38533ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
38633ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
3878a978234SLiam Girdwood 	kfree(sm);
3888a978234SLiam Girdwood 	kfree(p);
3898a978234SLiam Girdwood }
3908a978234SLiam Girdwood 
3918a978234SLiam Girdwood /* remove an enum kcontrol */
3928a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3938a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3948a978234SLiam Girdwood {
3958a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3968a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
3978a978234SLiam Girdwood 
3988a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3998a978234SLiam Girdwood 		return;
4008a978234SLiam Girdwood 
4018a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4028a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4038a978234SLiam Girdwood 
40433ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
40533ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4068a978234SLiam Girdwood 
4073cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_values(se);
4083cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_texts(se);
4098a978234SLiam Girdwood 	kfree(se);
4108a978234SLiam Girdwood }
4118a978234SLiam Girdwood 
4128a978234SLiam Girdwood /* remove a byte kcontrol */
4138a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4148a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4158a978234SLiam Girdwood {
4168a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4178a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4188a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4198a978234SLiam Girdwood 
4208a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4218a978234SLiam Girdwood 		return;
4228a978234SLiam Girdwood 
4238a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4248a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4258a978234SLiam Girdwood 
42633ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
42733ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4288a978234SLiam Girdwood 	kfree(sb);
4298a978234SLiam Girdwood }
4308a978234SLiam Girdwood 
4317df04ea7SRanjani Sridharan /* remove a route */
4327df04ea7SRanjani Sridharan static void remove_route(struct snd_soc_component *comp,
4337df04ea7SRanjani Sridharan 			 struct snd_soc_dobj *dobj, int pass)
4347df04ea7SRanjani Sridharan {
4357df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route *route =
4367df04ea7SRanjani Sridharan 		container_of(dobj, struct snd_soc_dapm_route, dobj);
4377df04ea7SRanjani Sridharan 
4387df04ea7SRanjani Sridharan 	if (pass != SOC_TPLG_PASS_GRAPH)
4397df04ea7SRanjani Sridharan 		return;
4407df04ea7SRanjani Sridharan 
4417df04ea7SRanjani Sridharan 	if (dobj->ops && dobj->ops->dapm_route_unload)
4427df04ea7SRanjani Sridharan 		dobj->ops->dapm_route_unload(comp, dobj);
4437df04ea7SRanjani Sridharan 
4447df04ea7SRanjani Sridharan 	list_del(&dobj->list);
4457df04ea7SRanjani Sridharan 	kfree(route);
4467df04ea7SRanjani Sridharan }
4477df04ea7SRanjani Sridharan 
4488a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4498a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4508a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4518a978234SLiam Girdwood {
4528a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4538a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4548a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4558a978234SLiam Girdwood 	int i;
4568a978234SLiam Girdwood 
4578a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4588a978234SLiam Girdwood 		return;
4598a978234SLiam Girdwood 
4608a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4618a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4628a978234SLiam Girdwood 
46305bdcf12SLiam Girdwood 	if (!w->kcontrols)
46405bdcf12SLiam Girdwood 		goto free_news;
46505bdcf12SLiam Girdwood 
4668a978234SLiam Girdwood 	/*
4671a7dd6e2SMengdong Lin 	 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
4688a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4698a978234SLiam Girdwood 	 */
470eea3dd4fSMengdong Lin 	if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
4718a978234SLiam Girdwood 		/* enumerated widget mixer */
472f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4731a7dd6e2SMengdong Lin 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4748a978234SLiam Girdwood 			struct soc_enum *se =
4751a7dd6e2SMengdong Lin 				(struct soc_enum *)kcontrol->private_value;
4768a978234SLiam Girdwood 
4771a7dd6e2SMengdong Lin 			snd_ctl_remove(card, kcontrol);
4788a978234SLiam Girdwood 
47954f8844eSRanjani Sridharan 			/* free enum kcontrol's dvalues and dtexts */
4803cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_values(se);
4813cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_texts(se);
4828a978234SLiam Girdwood 
4838a978234SLiam Girdwood 			kfree(se);
484267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
4851a7dd6e2SMengdong Lin 		}
4868a978234SLiam Girdwood 	} else {
487eea3dd4fSMengdong Lin 		/* volume mixer or bytes controls */
488f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4898a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4908a978234SLiam Girdwood 
491eea3dd4fSMengdong Lin 			if (dobj->widget.kcontrol_type
492eea3dd4fSMengdong Lin 			    == SND_SOC_TPLG_TYPE_MIXER)
493eea3dd4fSMengdong Lin 				kfree(kcontrol->tlv.p);
4948a978234SLiam Girdwood 
495eea3dd4fSMengdong Lin 			/* Private value is used as struct soc_mixer_control
496eea3dd4fSMengdong Lin 			 * for volume mixers or soc_bytes_ext for bytes
497eea3dd4fSMengdong Lin 			 * controls.
498eea3dd4fSMengdong Lin 			 */
499eea3dd4fSMengdong Lin 			kfree((void *)kcontrol->private_value);
500c2b36129SColin Ian King 			snd_ctl_remove(card, kcontrol);
501267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
5028a978234SLiam Girdwood 		}
5038a978234SLiam Girdwood 	}
50405bdcf12SLiam Girdwood 
50505bdcf12SLiam Girdwood free_news:
5068a978234SLiam Girdwood 	kfree(w->kcontrol_news);
50705bdcf12SLiam Girdwood 
508a46e8393SAmadeusz Sławiński 	list_del(&dobj->list);
509a46e8393SAmadeusz Sławiński 
5108a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
5118a978234SLiam Girdwood }
5128a978234SLiam Girdwood 
51364527e8aSMengdong Lin /* remove DAI configurations */
51464527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
5158a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
5168a978234SLiam Girdwood {
51764527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
51864527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
51952abe6ccSGuennadi Liakhovetski 	struct snd_soc_dai *dai;
52064527e8aSMengdong Lin 
5218a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
5228a978234SLiam Girdwood 		return;
5238a978234SLiam Girdwood 
52464527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
52564527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
5268a978234SLiam Girdwood 
52743ca5dabSKuninori Morimoto 	for_each_component_dais(comp, dai)
52852abe6ccSGuennadi Liakhovetski 		if (dai->driver == dai_drv)
52952abe6ccSGuennadi Liakhovetski 			dai->driver = NULL;
53052abe6ccSGuennadi Liakhovetski 
5317b6f68a4SBard liao 	kfree(dai_drv->playback.stream_name);
5327b6f68a4SBard liao 	kfree(dai_drv->capture.stream_name);
5338f27c4abSMengdong Lin 	kfree(dai_drv->name);
5348a978234SLiam Girdwood 	list_del(&dobj->list);
53564527e8aSMengdong Lin 	kfree(dai_drv);
5368a978234SLiam Girdwood }
5378a978234SLiam Girdwood 
538acfc7d46SMengdong Lin /* remove link configurations */
539acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
540acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
541acfc7d46SMengdong Lin {
542acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
543acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
544acfc7d46SMengdong Lin 
545acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
546acfc7d46SMengdong Lin 		return;
547acfc7d46SMengdong Lin 
548acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
549acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
550acfc7d46SMengdong Lin 
551dd836ddfSDragos Tarcatu 	list_del(&dobj->list);
55250cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(comp->card,
55350cd9b53SKuninori Morimoto 			snd_soc_get_pcm_runtime(comp->card, link));
554dd836ddfSDragos Tarcatu 
5558f27c4abSMengdong Lin 	kfree(link->name);
5568f27c4abSMengdong Lin 	kfree(link->stream_name);
55723b946ceSKuninori Morimoto 	kfree(link->cpus->dai_name);
558acfc7d46SMengdong Lin 	kfree(link);
559acfc7d46SMengdong Lin }
560acfc7d46SMengdong Lin 
561adfebb51SBard liao /* unload dai link */
562adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp,
563adfebb51SBard liao 	struct snd_soc_dobj *dobj, int pass)
564adfebb51SBard liao {
565adfebb51SBard liao 	if (pass != SOC_TPLG_PASS_LINK)
566adfebb51SBard liao 		return;
567adfebb51SBard liao 
568adfebb51SBard liao 	if (dobj->ops && dobj->ops->link_unload)
569adfebb51SBard liao 		dobj->ops->link_unload(comp, dobj);
570adfebb51SBard liao 
571adfebb51SBard liao 	/*
572adfebb51SBard liao 	 * We don't free the link here as what remove_link() do since BE
573adfebb51SBard liao 	 * links are not allocated by topology.
574adfebb51SBard liao 	 * We however need to reset the dobj type to its initial values
575adfebb51SBard liao 	 */
576adfebb51SBard liao 	dobj->type = SND_SOC_DOBJ_NONE;
577adfebb51SBard liao 	list_del(&dobj->list);
578adfebb51SBard liao }
579adfebb51SBard liao 
5808a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5818a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5828a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5832b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5848a978234SLiam Girdwood {
5852b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5861a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5872b5cdb91SMengdong Lin 	int num_ops, i;
5888a978234SLiam Girdwood 
5895aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
5901a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5911a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5921a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5931a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5941a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
5951a3232d2SMengdong Lin 
5961a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
5971a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
5981a3232d2SMengdong Lin 
5991a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
6001a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
6011a3232d2SMengdong Lin 		 */
602f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
6031a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
6041a3232d2SMengdong Lin 
6051a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
6061a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
6071a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
60872bbeda0SPierre-Louis Bossart 			if (!sbe->put &&
60972bbeda0SPierre-Louis Bossart 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
6101a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
61172bbeda0SPierre-Louis Bossart 			if (!sbe->get &&
61272bbeda0SPierre-Louis Bossart 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
6131a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
6141a3232d2SMengdong Lin 		}
6151a3232d2SMengdong Lin 
6161a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
6171a3232d2SMengdong Lin 			return 0;
6181a3232d2SMengdong Lin 		else
6191a3232d2SMengdong Lin 			return -EINVAL;
6201a3232d2SMengdong Lin 	}
6211a3232d2SMengdong Lin 
62288a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
6232b5cdb91SMengdong Lin 	ops = tplg->io_ops;
6242b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
6258a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
6268a978234SLiam Girdwood 
62772bbeda0SPierre-Louis Bossart 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
6288a978234SLiam Girdwood 			k->put = ops[i].put;
62972bbeda0SPierre-Louis Bossart 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
6308a978234SLiam Girdwood 			k->get = ops[i].get;
63172bbeda0SPierre-Louis Bossart 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
6322b5cdb91SMengdong Lin 			k->info = ops[i].info;
6338a978234SLiam Girdwood 	}
6348a978234SLiam Girdwood 
63588a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
63688a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
63788a17d8fSMengdong Lin 		return 0;
63888a17d8fSMengdong Lin 
63988a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
6402b5cdb91SMengdong Lin 	ops = io_ops;
6412b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
64288a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
64388a17d8fSMengdong Lin 
64472bbeda0SPierre-Louis Bossart 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
64588a17d8fSMengdong Lin 			k->put = ops[i].put;
64672bbeda0SPierre-Louis Bossart 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
64788a17d8fSMengdong Lin 			k->get = ops[i].get;
64872bbeda0SPierre-Louis Bossart 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
6498a978234SLiam Girdwood 			k->info = ops[i].info;
6508a978234SLiam Girdwood 	}
6518a978234SLiam Girdwood 
6528a978234SLiam Girdwood 	/* standard handlers found ? */
6538a978234SLiam Girdwood 	if (k->put && k->get && k->info)
6548a978234SLiam Girdwood 		return 0;
6558a978234SLiam Girdwood 
6568a978234SLiam Girdwood 	/* nothing to bind */
6578a978234SLiam Girdwood 	return -EINVAL;
6588a978234SLiam Girdwood }
6598a978234SLiam Girdwood 
6608a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
6618a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
6628a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
6638a978234SLiam Girdwood 		int num_events, u16 event_type)
6648a978234SLiam Girdwood {
6658a978234SLiam Girdwood 	int i;
6668a978234SLiam Girdwood 
6678a978234SLiam Girdwood 	w->event = NULL;
6688a978234SLiam Girdwood 
6698a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
6708a978234SLiam Girdwood 		if (event_type == events[i].type) {
6718a978234SLiam Girdwood 
6728a978234SLiam Girdwood 			/* found - so assign event */
6738a978234SLiam Girdwood 			w->event = events[i].event_handler;
6748a978234SLiam Girdwood 			return 0;
6758a978234SLiam Girdwood 		}
6768a978234SLiam Girdwood 	}
6778a978234SLiam Girdwood 
6788a978234SLiam Girdwood 	/* not found */
6798a978234SLiam Girdwood 	return -EINVAL;
6808a978234SLiam Girdwood }
6818a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
6828a978234SLiam Girdwood 
6838a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6848a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6858a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6868a978234SLiam Girdwood {
687c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->control_load)
688c60b613aSLiam Girdwood 		return tplg->ops->control_load(tplg->comp, tplg->index, k,
689c60b613aSLiam Girdwood 			hdr);
6908a978234SLiam Girdwood 
6918a978234SLiam Girdwood 	return 0;
6928a978234SLiam Girdwood }
6938a978234SLiam Girdwood 
69428a87eebSMengdong Lin 
69528a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
69628a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6978a978234SLiam Girdwood {
69828a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
69928a87eebSMengdong Lin 	unsigned int *p;
7008a978234SLiam Girdwood 
70128a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
70228a87eebSMengdong Lin 	if (!p)
7038a978234SLiam Girdwood 		return -ENOMEM;
7048a978234SLiam Girdwood 
70528a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
70628a87eebSMengdong Lin 	p[1] = item_len;
7075aebe7c7SPierre-Louis Bossart 	p[2] = le32_to_cpu(scale->min);
7085aebe7c7SPierre-Louis Bossart 	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
7095aebe7c7SPierre-Louis Bossart 		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
7108a978234SLiam Girdwood 
71128a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
71228a87eebSMengdong Lin 	return 0;
71328a87eebSMengdong Lin }
71428a87eebSMengdong Lin 
71528a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
71628a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
71728a87eebSMengdong Lin {
71828a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
7195aebe7c7SPierre-Louis Bossart 	u32 access = le32_to_cpu(tc->access);
72028a87eebSMengdong Lin 
7215aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
72228a87eebSMengdong Lin 		return 0;
72328a87eebSMengdong Lin 
7245aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
72528a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
7265aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(tplg_tlv->type)) {
72728a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
72828a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
72928a87eebSMengdong Lin 					&tplg_tlv->scale);
73028a87eebSMengdong Lin 
73128a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
73228a87eebSMengdong Lin 		default:
73328a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
73428a87eebSMengdong Lin 					tplg_tlv->type);
73528a87eebSMengdong Lin 			return -EINVAL;
73628a87eebSMengdong Lin 		}
73728a87eebSMengdong Lin 	}
7388a978234SLiam Girdwood 
7398a978234SLiam Girdwood 	return 0;
7408a978234SLiam Girdwood }
7418a978234SLiam Girdwood 
7428a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
7438a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
7448a978234SLiam Girdwood {
7458a978234SLiam Girdwood 	kfree(kc->tlv.p);
7468a978234SLiam Girdwood }
7478a978234SLiam Girdwood 
7488a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
7498a978234SLiam Girdwood 	size_t size)
7508a978234SLiam Girdwood {
7518a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
7528a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
7538a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7548a978234SLiam Girdwood 	int i, err;
7558a978234SLiam Girdwood 
7568a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7578a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
7588a978234SLiam Girdwood 			size, "mixer bytes")) {
7598a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
7608a978234SLiam Girdwood 			count);
7618a978234SLiam Girdwood 		return -EINVAL;
7628a978234SLiam Girdwood 	}
7638a978234SLiam Girdwood 
7648a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7658a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
7668a978234SLiam Girdwood 
7678a978234SLiam Girdwood 		/* validate kcontrol */
7688a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7698a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7708a978234SLiam Girdwood 			return -EINVAL;
7718a978234SLiam Girdwood 
7728a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
7738a978234SLiam Girdwood 		if (sbe == NULL)
7748a978234SLiam Girdwood 			return -ENOMEM;
7758a978234SLiam Girdwood 
7768a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
7775aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(be->priv.size));
7788a978234SLiam Girdwood 
7798a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7808a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
7818a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
7828a978234SLiam Girdwood 
7838a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7848a978234SLiam Girdwood 		kc.name = be->hdr.name;
7858a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7868a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7875aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(be->hdr.access);
7888a978234SLiam Girdwood 
7895aebe7c7SPierre-Louis Bossart 		sbe->max = le32_to_cpu(be->max);
7908a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7918a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7928a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7938a978234SLiam Girdwood 
7948a978234SLiam Girdwood 		/* map io handlers */
7952b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7968a978234SLiam Girdwood 		if (err) {
7978a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
7988a978234SLiam Girdwood 			kfree(sbe);
7998a978234SLiam Girdwood 			continue;
8008a978234SLiam Girdwood 		}
8018a978234SLiam Girdwood 
8028a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8038a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8048a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
8058a978234SLiam Girdwood 		if (err < 0) {
8068a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8078a978234SLiam Girdwood 				be->hdr.name);
8088a978234SLiam Girdwood 			kfree(sbe);
8098a978234SLiam Girdwood 			continue;
8108a978234SLiam Girdwood 		}
8118a978234SLiam Girdwood 
8128a978234SLiam Girdwood 		/* register control here */
8138a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8148a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
8158a978234SLiam Girdwood 		if (err < 0) {
8168a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8178a978234SLiam Girdwood 				be->hdr.name);
8188a978234SLiam Girdwood 			kfree(sbe);
8198a978234SLiam Girdwood 			continue;
8208a978234SLiam Girdwood 		}
8218a978234SLiam Girdwood 
8228a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
8238a978234SLiam Girdwood 	}
8248a978234SLiam Girdwood 	return 0;
8258a978234SLiam Girdwood 
8268a978234SLiam Girdwood }
8278a978234SLiam Girdwood 
8288a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
8298a978234SLiam Girdwood 	size_t size)
8308a978234SLiam Girdwood {
8318a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
8328a978234SLiam Girdwood 	struct soc_mixer_control *sm;
8338a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8348a978234SLiam Girdwood 	int i, err;
8358a978234SLiam Girdwood 
8368a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8378a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
8388a978234SLiam Girdwood 		count, size, "mixers")) {
8398a978234SLiam Girdwood 
8408a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
8418a978234SLiam Girdwood 			count);
8428a978234SLiam Girdwood 		return -EINVAL;
8438a978234SLiam Girdwood 	}
8448a978234SLiam Girdwood 
8458a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
8468a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
8478a978234SLiam Girdwood 
8488a978234SLiam Girdwood 		/* validate kcontrol */
8498a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8508a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8518a978234SLiam Girdwood 			return -EINVAL;
8528a978234SLiam Girdwood 
8538a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
8548a978234SLiam Girdwood 		if (sm == NULL)
8558a978234SLiam Girdwood 			return -ENOMEM;
8568a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
8575aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(mc->priv.size));
8588a978234SLiam Girdwood 
8598a978234SLiam Girdwood 		dev_dbg(tplg->dev,
8608a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
8618a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
8628a978234SLiam Girdwood 
8638a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
8648a978234SLiam Girdwood 		kc.name = mc->hdr.name;
8658a978234SLiam Girdwood 		kc.private_value = (long)sm;
8668a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8675aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(mc->hdr.access);
8688a978234SLiam Girdwood 
8698a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
8708a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
8718a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8728a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
8738a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8748a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
8758a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8768a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
8778a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8788a978234SLiam Girdwood 
8795aebe7c7SPierre-Louis Bossart 		sm->max = le32_to_cpu(mc->max);
8805aebe7c7SPierre-Louis Bossart 		sm->min = le32_to_cpu(mc->min);
8815aebe7c7SPierre-Louis Bossart 		sm->invert = le32_to_cpu(mc->invert);
8825aebe7c7SPierre-Louis Bossart 		sm->platform_max = le32_to_cpu(mc->platform_max);
8838a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
8848a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8858a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8868a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8878a978234SLiam Girdwood 
8888a978234SLiam Girdwood 		/* map io handlers */
8892b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8908a978234SLiam Girdwood 		if (err) {
8918a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8928a978234SLiam Girdwood 			kfree(sm);
8938a978234SLiam Girdwood 			continue;
8948a978234SLiam Girdwood 		}
8958a978234SLiam Girdwood 
8963789debfSBard liao 		/* create any TLV data */
897482db55aSAmadeusz Sławiński 		err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
898482db55aSAmadeusz Sławiński 		if (err < 0) {
899482db55aSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
900482db55aSAmadeusz Sławiński 				mc->hdr.name);
901482db55aSAmadeusz Sławiński 			kfree(sm);
902482db55aSAmadeusz Sławiński 			continue;
903482db55aSAmadeusz Sławiński 		}
9043789debfSBard liao 
9058a978234SLiam Girdwood 		/* pass control to driver for optional further init */
9068a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
9078a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
9088a978234SLiam Girdwood 		if (err < 0) {
9098a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
9108a978234SLiam Girdwood 				mc->hdr.name);
9113789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc);
9128a978234SLiam Girdwood 			kfree(sm);
9138a978234SLiam Girdwood 			continue;
9148a978234SLiam Girdwood 		}
9158a978234SLiam Girdwood 
9168a978234SLiam Girdwood 		/* register control here */
9178a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
9188a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
9198a978234SLiam Girdwood 		if (err < 0) {
9208a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
9218a978234SLiam Girdwood 				mc->hdr.name);
9228a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
9238a978234SLiam Girdwood 			kfree(sm);
9248a978234SLiam Girdwood 			continue;
9258a978234SLiam Girdwood 		}
9268a978234SLiam Girdwood 
9278a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
9288a978234SLiam Girdwood 	}
9298a978234SLiam Girdwood 
9308a978234SLiam Girdwood 	return 0;
9318a978234SLiam Girdwood }
9328a978234SLiam Girdwood 
9338a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
9348a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9358a978234SLiam Girdwood {
9368a978234SLiam Girdwood 	int i, ret;
9378a978234SLiam Girdwood 
9388a978234SLiam Girdwood 	se->dobj.control.dtexts =
9395aebe7c7SPierre-Louis Bossart 		kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
9408a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
9418a978234SLiam Girdwood 		return -ENOMEM;
9428a978234SLiam Girdwood 
94372bbeda0SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
9448a978234SLiam Girdwood 
9458a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9468a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
9478a978234SLiam Girdwood 			ret = -EINVAL;
9488a978234SLiam Girdwood 			goto err;
9498a978234SLiam Girdwood 		}
9508a978234SLiam Girdwood 
9518a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
9528a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
9538a978234SLiam Girdwood 			ret = -ENOMEM;
9548a978234SLiam Girdwood 			goto err;
9558a978234SLiam Girdwood 		}
9568a978234SLiam Girdwood 	}
9578a978234SLiam Girdwood 
9583cde818cSAmadeusz Sławiński 	se->items = le32_to_cpu(ec->items);
959b6e38b29SMousumi Jana 	se->texts = (const char * const *)se->dobj.control.dtexts;
9608a978234SLiam Girdwood 	return 0;
9618a978234SLiam Girdwood 
9628a978234SLiam Girdwood err:
9633cde818cSAmadeusz Sławiński 	se->items = i;
9643cde818cSAmadeusz Sławiński 	soc_tplg_denum_remove_texts(se);
9653cde818cSAmadeusz Sławiński 	return ret;
9663cde818cSAmadeusz Sławiński }
9673cde818cSAmadeusz Sławiński 
9683cde818cSAmadeusz Sławiński static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
9693cde818cSAmadeusz Sławiński {
9703cde818cSAmadeusz Sławiński 	int i = se->items;
9713cde818cSAmadeusz Sławiński 
9728a978234SLiam Girdwood 	for (--i; i >= 0; i--)
9738a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
9748a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
9758a978234SLiam Girdwood }
9768a978234SLiam Girdwood 
9778a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
9788a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9798a978234SLiam Girdwood {
9805aebe7c7SPierre-Louis Bossart 	int i;
9815aebe7c7SPierre-Louis Bossart 
9825aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(ec->items) > sizeof(*ec->values))
9838a978234SLiam Girdwood 		return -EINVAL;
9848a978234SLiam Girdwood 
9855aebe7c7SPierre-Louis Bossart 	se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
9865aebe7c7SPierre-Louis Bossart 					   sizeof(u32),
987376c0afeSAndrzej Hajda 					   GFP_KERNEL);
9888a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
9898a978234SLiam Girdwood 		return -ENOMEM;
9908a978234SLiam Girdwood 
9915aebe7c7SPierre-Louis Bossart 	/* convert from little-endian */
9925aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
9935aebe7c7SPierre-Louis Bossart 		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
9945aebe7c7SPierre-Louis Bossart 	}
9955aebe7c7SPierre-Louis Bossart 
9968a978234SLiam Girdwood 	return 0;
9978a978234SLiam Girdwood }
9988a978234SLiam Girdwood 
9993cde818cSAmadeusz Sławiński static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
10003cde818cSAmadeusz Sławiński {
10013cde818cSAmadeusz Sławiński 	kfree(se->dobj.control.dvalues);
10023cde818cSAmadeusz Sławiński }
10033cde818cSAmadeusz Sławiński 
10048a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
10058a978234SLiam Girdwood 	size_t size)
10068a978234SLiam Girdwood {
10078a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
10088a978234SLiam Girdwood 	struct soc_enum *se;
10098a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
10108a978234SLiam Girdwood 	int i, ret, err;
10118a978234SLiam Girdwood 
10128a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10138a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
10148a978234SLiam Girdwood 		count, size, "enums")) {
10158a978234SLiam Girdwood 
10168a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
10178a978234SLiam Girdwood 			count);
10188a978234SLiam Girdwood 		return -EINVAL;
10198a978234SLiam Girdwood 	}
10208a978234SLiam Girdwood 
10218a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
10228a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
10238a978234SLiam Girdwood 
10248a978234SLiam Girdwood 		/* validate kcontrol */
10258a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10268a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10278a978234SLiam Girdwood 			return -EINVAL;
10288a978234SLiam Girdwood 
10298a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
10308a978234SLiam Girdwood 		if (se == NULL)
10318a978234SLiam Girdwood 			return -ENOMEM;
10328a978234SLiam Girdwood 
103302b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
10345aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(ec->priv.size));
103502b64245SLiam Girdwood 
10368a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
10378a978234SLiam Girdwood 			ec->hdr.name, ec->items);
10388a978234SLiam Girdwood 
10398a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
10408a978234SLiam Girdwood 		kc.name = ec->hdr.name;
10418a978234SLiam Girdwood 		kc.private_value = (long)se;
10428a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
10435aebe7c7SPierre-Louis Bossart 		kc.access = le32_to_cpu(ec->hdr.access);
10448a978234SLiam Girdwood 
10458a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
10468a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
10478a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10488a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
10498a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10508a978234SLiam Girdwood 
10515aebe7c7SPierre-Louis Bossart 		se->mask = le32_to_cpu(ec->mask);
10528a978234SLiam Girdwood 		se->dobj.index = tplg->index;
10538a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
10548a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
10558a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
10568a978234SLiam Girdwood 
10575aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(ec->hdr.ops.info)) {
10588a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10598a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10608a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
10618a978234SLiam Girdwood 			if (err < 0) {
10628a978234SLiam Girdwood 				dev_err(tplg->dev,
10638a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
10648a978234SLiam Girdwood 					ec->hdr.name);
10658a978234SLiam Girdwood 				kfree(se);
10668a978234SLiam Girdwood 				continue;
10678a978234SLiam Girdwood 			}
10689c6c4d96STakashi Iwai 			/* fall through */
10698a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10708a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10718a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10728a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
10738a978234SLiam Girdwood 			if (err < 0) {
10748a978234SLiam Girdwood 				dev_err(tplg->dev,
10758a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
10768a978234SLiam Girdwood 					ec->hdr.name);
10778a978234SLiam Girdwood 				kfree(se);
10788a978234SLiam Girdwood 				continue;
10798a978234SLiam Girdwood 			}
10808a978234SLiam Girdwood 			break;
10818a978234SLiam Girdwood 		default:
10828a978234SLiam Girdwood 			dev_err(tplg->dev,
10838a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
10848a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
10858a978234SLiam Girdwood 			kfree(se);
10868a978234SLiam Girdwood 			continue;
10878a978234SLiam Girdwood 		}
10888a978234SLiam Girdwood 
10898a978234SLiam Girdwood 		/* map io handlers */
10902b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
10918a978234SLiam Girdwood 		if (err) {
10928a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
10938a978234SLiam Girdwood 			kfree(se);
10948a978234SLiam Girdwood 			continue;
10958a978234SLiam Girdwood 		}
10968a978234SLiam Girdwood 
10978a978234SLiam Girdwood 		/* pass control to driver for optional further init */
10988a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
10998a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
11008a978234SLiam Girdwood 		if (err < 0) {
11018a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
11028a978234SLiam Girdwood 				ec->hdr.name);
11038a978234SLiam Girdwood 			kfree(se);
11048a978234SLiam Girdwood 			continue;
11058a978234SLiam Girdwood 		}
11068a978234SLiam Girdwood 
11078a978234SLiam Girdwood 		/* register control here */
11088a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
11098a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
11108a978234SLiam Girdwood 		if (ret < 0) {
11118a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
11128a978234SLiam Girdwood 				ec->hdr.name);
11138a978234SLiam Girdwood 			kfree(se);
11148a978234SLiam Girdwood 			continue;
11158a978234SLiam Girdwood 		}
11168a978234SLiam Girdwood 
11178a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
11188a978234SLiam Girdwood 	}
11198a978234SLiam Girdwood 
11208a978234SLiam Girdwood 	return 0;
11218a978234SLiam Girdwood }
11228a978234SLiam Girdwood 
11238a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
11248a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11258a978234SLiam Girdwood {
11268a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
1127*2ae548f3SAmadeusz Sławiński 	int ret;
11288a978234SLiam Girdwood 	int i;
11298a978234SLiam Girdwood 
11308a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
11315aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
11325aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
11338a978234SLiam Girdwood 		return 0;
11348a978234SLiam Girdwood 	}
11358a978234SLiam Girdwood 
11368a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
11378a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
11388a978234SLiam Girdwood 
11395aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
11408a978234SLiam Girdwood 
11418a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
11428a978234SLiam Girdwood 
11435aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
114406eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
114506eb49f7SMengdong Lin 			return -EINVAL;
114606eb49f7SMengdong Lin 		}
114706eb49f7SMengdong Lin 
11485aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
11498a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
11508a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
11518a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
11528a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
11538a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
11548a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
11558a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
1156*2ae548f3SAmadeusz Sławiński 			ret = soc_tplg_dmixer_create(tplg, 1,
11575aebe7c7SPierre-Louis Bossart 					le32_to_cpu(hdr->payload_size));
11588a978234SLiam Girdwood 			break;
11598a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
11608a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
11618a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
11628a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
11638a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1164*2ae548f3SAmadeusz Sławiński 			ret = soc_tplg_denum_create(tplg, 1,
11655aebe7c7SPierre-Louis Bossart 					le32_to_cpu(hdr->payload_size));
11668a978234SLiam Girdwood 			break;
11678a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
1168*2ae548f3SAmadeusz Sławiński 			ret = soc_tplg_dbytes_create(tplg, 1,
11695aebe7c7SPierre-Louis Bossart 					le32_to_cpu(hdr->payload_size));
11708a978234SLiam Girdwood 			break;
11718a978234SLiam Girdwood 		default:
11728a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
11738a978234SLiam Girdwood 			return -EINVAL;
11748a978234SLiam Girdwood 		}
1175*2ae548f3SAmadeusz Sławiński 		if (ret < 0) {
1176*2ae548f3SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid control\n");
1177*2ae548f3SAmadeusz Sławiński 			return ret;
1178*2ae548f3SAmadeusz Sławiński 		}
1179*2ae548f3SAmadeusz Sławiński 
11808a978234SLiam Girdwood 	}
11818a978234SLiam Girdwood 
11828a978234SLiam Girdwood 	return 0;
11838a978234SLiam Girdwood }
11848a978234SLiam Girdwood 
1185503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1186503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1187503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1188503e79b7SLiam Girdwood {
1189c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dapm_route_load)
1190503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1191503e79b7SLiam Girdwood 			route);
1192503e79b7SLiam Girdwood 
1193503e79b7SLiam Girdwood 	return 0;
1194503e79b7SLiam Girdwood }
1195503e79b7SLiam Girdwood 
11968a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
11978a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11988a978234SLiam Girdwood {
11998a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
12008a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
12017df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route **routes;
12025aebe7c7SPierre-Louis Bossart 	int count, i, j;
12037df04ea7SRanjani Sridharan 	int ret = 0;
12048a978234SLiam Girdwood 
12055aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
12065aebe7c7SPierre-Louis Bossart 
12078a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
12085aebe7c7SPierre-Louis Bossart 		tplg->pos +=
12095aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->size) +
12105aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
12115aebe7c7SPierre-Louis Bossart 
12128a978234SLiam Girdwood 		return 0;
12138a978234SLiam Girdwood 	}
12148a978234SLiam Girdwood 
12158a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
12168a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
12175aebe7c7SPierre-Louis Bossart 		count, le32_to_cpu(hdr->payload_size), "graph")) {
12188a978234SLiam Girdwood 
12198a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
12208a978234SLiam Girdwood 			count);
12218a978234SLiam Girdwood 		return -EINVAL;
12228a978234SLiam Girdwood 	}
12238a978234SLiam Girdwood 
1224b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1225b75a6511SLiam Girdwood 		hdr->index);
12268a978234SLiam Girdwood 
12277df04ea7SRanjani Sridharan 	/* allocate memory for pointer to array of dapm routes */
12287df04ea7SRanjani Sridharan 	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
12297df04ea7SRanjani Sridharan 			 GFP_KERNEL);
12307df04ea7SRanjani Sridharan 	if (!routes)
12317df04ea7SRanjani Sridharan 		return -ENOMEM;
12327df04ea7SRanjani Sridharan 
12337df04ea7SRanjani Sridharan 	/*
12347df04ea7SRanjani Sridharan 	 * allocate memory for each dapm route in the array.
12357df04ea7SRanjani Sridharan 	 * This needs to be done individually so that
12367df04ea7SRanjani Sridharan 	 * each route can be freed when it is removed in remove_route().
12377df04ea7SRanjani Sridharan 	 */
12387df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
12397df04ea7SRanjani Sridharan 		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
12407df04ea7SRanjani Sridharan 		if (!routes[i]) {
12417df04ea7SRanjani Sridharan 			/* free previously allocated memory */
12427df04ea7SRanjani Sridharan 			for (j = 0; j < i; j++)
12437df04ea7SRanjani Sridharan 				kfree(routes[j]);
12447df04ea7SRanjani Sridharan 
12457df04ea7SRanjani Sridharan 			kfree(routes);
12467df04ea7SRanjani Sridharan 			return -ENOMEM;
12477df04ea7SRanjani Sridharan 		}
12487df04ea7SRanjani Sridharan 	}
12497df04ea7SRanjani Sridharan 
12508a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12518a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
12528a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
12538a978234SLiam Girdwood 
12548a978234SLiam Girdwood 		/* validate routes */
12558a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12567df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12577df04ea7SRanjani Sridharan 			ret = -EINVAL;
12587df04ea7SRanjani Sridharan 			break;
12597df04ea7SRanjani Sridharan 		}
12608a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12617df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12627df04ea7SRanjani Sridharan 			ret = -EINVAL;
12637df04ea7SRanjani Sridharan 			break;
12647df04ea7SRanjani Sridharan 		}
12658a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12667df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12677df04ea7SRanjani Sridharan 			ret = -EINVAL;
12687df04ea7SRanjani Sridharan 			break;
12698a978234SLiam Girdwood 		}
12708a978234SLiam Girdwood 
12717df04ea7SRanjani Sridharan 		routes[i]->source = elem->source;
12727df04ea7SRanjani Sridharan 		routes[i]->sink = elem->sink;
12737df04ea7SRanjani Sridharan 
12747df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
12757df04ea7SRanjani Sridharan 		routes[i]->connected = NULL;
12767df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
12777df04ea7SRanjani Sridharan 			routes[i]->control = NULL;
12787df04ea7SRanjani Sridharan 		else
12797df04ea7SRanjani Sridharan 			routes[i]->control = elem->control;
12807df04ea7SRanjani Sridharan 
12817df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
12827df04ea7SRanjani Sridharan 		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
12837df04ea7SRanjani Sridharan 		routes[i]->dobj.ops = tplg->ops;
12847df04ea7SRanjani Sridharan 		routes[i]->dobj.index = tplg->index;
12857df04ea7SRanjani Sridharan 		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
12867df04ea7SRanjani Sridharan 
12877df04ea7SRanjani Sridharan 		soc_tplg_add_route(tplg, routes[i]);
12887df04ea7SRanjani Sridharan 
12897df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
12907df04ea7SRanjani Sridharan 		snd_soc_dapm_add_routes(dapm, routes[i], 1);
12917df04ea7SRanjani Sridharan 	}
12927df04ea7SRanjani Sridharan 
12937df04ea7SRanjani Sridharan 	/* free memory allocated for all dapm routes in case of error */
12947df04ea7SRanjani Sridharan 	if (ret < 0)
12957df04ea7SRanjani Sridharan 		for (i = 0; i < count ; i++)
12967df04ea7SRanjani Sridharan 			kfree(routes[i]);
12977df04ea7SRanjani Sridharan 
12987df04ea7SRanjani Sridharan 	/*
12997df04ea7SRanjani Sridharan 	 * free pointer to array of dapm routes as this is no longer needed.
13007df04ea7SRanjani Sridharan 	 * The memory allocated for each dapm route will be freed
13017df04ea7SRanjani Sridharan 	 * when it is removed in remove_route().
13027df04ea7SRanjani Sridharan 	 */
13037df04ea7SRanjani Sridharan 	kfree(routes);
13047df04ea7SRanjani Sridharan 
13057df04ea7SRanjani Sridharan 	return ret;
13068a978234SLiam Girdwood }
13078a978234SLiam Girdwood 
13088a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
13098a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
13108a978234SLiam Girdwood {
13118a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13128a978234SLiam Girdwood 	struct soc_mixer_control *sm;
13138a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
13148a978234SLiam Girdwood 	int i, err;
13158a978234SLiam Girdwood 
13164ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13178a978234SLiam Girdwood 	if (kc == NULL)
13188a978234SLiam Girdwood 		return NULL;
13198a978234SLiam Girdwood 
13208a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
13218a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
13228a978234SLiam Girdwood 
13238a978234SLiam Girdwood 		/* validate kcontrol */
13248a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13258a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13269f90af3aSAmadeusz Sławiński 			goto err_sm;
13279f90af3aSAmadeusz Sławiński 
13289f90af3aSAmadeusz Sławiński 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
13299f90af3aSAmadeusz Sławiński 		if (sm == NULL)
13309f90af3aSAmadeusz Sławiński 			goto err_sm;
13318a978234SLiam Girdwood 
133202b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
13335aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(mc->priv.size));
133402b64245SLiam Girdwood 
13358a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
13368a978234SLiam Girdwood 			mc->hdr.name, i);
13378a978234SLiam Girdwood 
13381ad741d0SColin Ian King 		kc[i].private_value = (long)sm;
1339267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1340267e2c6fSLiam Girdwood 		if (kc[i].name == NULL)
13419f90af3aSAmadeusz Sławiński 			goto err_sm;
13428a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
134372bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(mc->hdr.access);
13448a978234SLiam Girdwood 
13458a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13468a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
13478a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13488a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
13498a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13508a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
13518a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13528a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
13538a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13548a978234SLiam Girdwood 
135572bbeda0SPierre-Louis Bossart 		sm->max = le32_to_cpu(mc->max);
135672bbeda0SPierre-Louis Bossart 		sm->min = le32_to_cpu(mc->min);
135772bbeda0SPierre-Louis Bossart 		sm->invert = le32_to_cpu(mc->invert);
135872bbeda0SPierre-Louis Bossart 		sm->platform_max = le32_to_cpu(mc->platform_max);
13598a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
13608a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
13618a978234SLiam Girdwood 
13628a978234SLiam Girdwood 		/* map io handlers */
13632b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
13648a978234SLiam Girdwood 		if (err) {
13658a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
13669f90af3aSAmadeusz Sławiński 			goto err_sm;
13678a978234SLiam Girdwood 		}
13688a978234SLiam Girdwood 
13693789debfSBard liao 		/* create any TLV data */
1370482db55aSAmadeusz Sławiński 		err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1371482db55aSAmadeusz Sławiński 		if (err < 0) {
1372482db55aSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1373482db55aSAmadeusz Sławiński 				mc->hdr.name);
1374482db55aSAmadeusz Sławiński 			kfree(sm);
1375482db55aSAmadeusz Sławiński 			continue;
1376482db55aSAmadeusz Sławiński 		}
13773789debfSBard liao 
13788a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13798a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13808a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
13818a978234SLiam Girdwood 		if (err < 0) {
13828a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13838a978234SLiam Girdwood 				mc->hdr.name);
13843789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc[i]);
13859f90af3aSAmadeusz Sławiński 			goto err_sm;
13868a978234SLiam Girdwood 		}
13878a978234SLiam Girdwood 	}
13888a978234SLiam Girdwood 	return kc;
13898a978234SLiam Girdwood 
13909f90af3aSAmadeusz Sławiński err_sm:
13919f90af3aSAmadeusz Sławiński 	for (; i >= 0; i--) {
13929f90af3aSAmadeusz Sławiński 		sm = (struct soc_mixer_control *)kc[i].private_value;
13938a978234SLiam Girdwood 		kfree(sm);
1394267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1395267e2c6fSLiam Girdwood 	}
13968a978234SLiam Girdwood 	kfree(kc);
13979f90af3aSAmadeusz Sławiński 
13988a978234SLiam Girdwood 	return NULL;
13998a978234SLiam Girdwood }
14008a978234SLiam Girdwood 
14018a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
14021a7dd6e2SMengdong Lin 	struct soc_tplg *tplg, int num_kcontrols)
14038a978234SLiam Girdwood {
14048a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
14058a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
14068a978234SLiam Girdwood 	struct soc_enum *se;
14073cde818cSAmadeusz Sławiński 	int i, err;
14088a978234SLiam Girdwood 
14091a7dd6e2SMengdong Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
14101a7dd6e2SMengdong Lin 	if (kc == NULL)
14111a7dd6e2SMengdong Lin 		return NULL;
14121a7dd6e2SMengdong Lin 
14131a7dd6e2SMengdong Lin 	for (i = 0; i < num_kcontrols; i++) {
14148a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
14158a978234SLiam Girdwood 		/* validate kcontrol */
14168a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
14178a978234SLiam Girdwood 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
14189f90af3aSAmadeusz Sławiński 			goto err_se;
14198a978234SLiam Girdwood 
14208a978234SLiam Girdwood 		se = kzalloc(sizeof(*se), GFP_KERNEL);
14218a978234SLiam Girdwood 		if (se == NULL)
14229f90af3aSAmadeusz Sławiński 			goto err_se;
14238a978234SLiam Girdwood 
142402b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
142572bbeda0SPierre-Louis Bossart 			      le32_to_cpu(ec->priv.size));
142602b64245SLiam Girdwood 
14278a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
14288a978234SLiam Girdwood 			ec->hdr.name);
14298a978234SLiam Girdwood 
14301ad741d0SColin Ian King 		kc[i].private_value = (long)se;
1431267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
14329f90af3aSAmadeusz Sławiński 		if (kc[i].name == NULL)
1433267e2c6fSLiam Girdwood 			goto err_se;
14341a7dd6e2SMengdong Lin 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
143572bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(ec->hdr.access);
14368a978234SLiam Girdwood 
14378a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
14388a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
14391a7dd6e2SMengdong Lin 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
14401a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FL);
14411a7dd6e2SMengdong Lin 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
14421a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FR);
14438a978234SLiam Girdwood 
144472bbeda0SPierre-Louis Bossart 		se->items = le32_to_cpu(ec->items);
144572bbeda0SPierre-Louis Bossart 		se->mask = le32_to_cpu(ec->mask);
14468a978234SLiam Girdwood 		se->dobj.index = tplg->index;
14478a978234SLiam Girdwood 
14485aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(ec->hdr.ops.info)) {
14498a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
14508a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14518a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
14528a978234SLiam Girdwood 			if (err < 0) {
14538a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
14548a978234SLiam Girdwood 					ec->hdr.name);
14558a978234SLiam Girdwood 				goto err_se;
14568a978234SLiam Girdwood 			}
14579c6c4d96STakashi Iwai 			/* fall through */
14588a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
14598a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14608a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14618a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
14628a978234SLiam Girdwood 			if (err < 0) {
14638a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
14648a978234SLiam Girdwood 					ec->hdr.name);
14658a978234SLiam Girdwood 				goto err_se;
14668a978234SLiam Girdwood 			}
14678a978234SLiam Girdwood 			break;
14688a978234SLiam Girdwood 		default:
14698a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
14708a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
14718a978234SLiam Girdwood 			goto err_se;
14728a978234SLiam Girdwood 		}
14738a978234SLiam Girdwood 
14748a978234SLiam Girdwood 		/* map io handlers */
14751a7dd6e2SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
14768a978234SLiam Girdwood 		if (err) {
14778a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
14788a978234SLiam Girdwood 			goto err_se;
14798a978234SLiam Girdwood 		}
14808a978234SLiam Girdwood 
14818a978234SLiam Girdwood 		/* pass control to driver for optional further init */
14821a7dd6e2SMengdong Lin 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
14838a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)ec);
14848a978234SLiam Girdwood 		if (err < 0) {
14858a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
14868a978234SLiam Girdwood 				ec->hdr.name);
14878a978234SLiam Girdwood 			goto err_se;
14888a978234SLiam Girdwood 		}
14891a7dd6e2SMengdong Lin 	}
14901a7dd6e2SMengdong Lin 
14918a978234SLiam Girdwood 	return kc;
14928a978234SLiam Girdwood 
14938a978234SLiam Girdwood err_se:
14941a7dd6e2SMengdong Lin 	for (; i >= 0; i--) {
14958a978234SLiam Girdwood 		/* free values and texts */
14961a7dd6e2SMengdong Lin 		se = (struct soc_enum *)kc[i].private_value;
14976d5574edSChristophe JAILLET 
14989f90af3aSAmadeusz Sławiński 		if (se) {
14993cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_values(se);
15003cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_texts(se);
15019f90af3aSAmadeusz Sławiński 		}
15028a978234SLiam Girdwood 
15038a978234SLiam Girdwood 		kfree(se);
1504267e2c6fSLiam Girdwood 		kfree(kc[i].name);
15051a7dd6e2SMengdong Lin 	}
15068a978234SLiam Girdwood 	kfree(kc);
15078a978234SLiam Girdwood 
15088a978234SLiam Girdwood 	return NULL;
15098a978234SLiam Girdwood }
15108a978234SLiam Girdwood 
15118a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
15129f90af3aSAmadeusz Sławiński 	struct soc_tplg *tplg, int num_kcontrols)
15138a978234SLiam Girdwood {
15148a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
15158a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
15168a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
15178a978234SLiam Girdwood 	int i, err;
15188a978234SLiam Girdwood 
15199f90af3aSAmadeusz Sławiński 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
15208a978234SLiam Girdwood 	if (!kc)
15218a978234SLiam Girdwood 		return NULL;
15228a978234SLiam Girdwood 
15239f90af3aSAmadeusz Sławiński 	for (i = 0; i < num_kcontrols; i++) {
15248a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
15258a978234SLiam Girdwood 
15268a978234SLiam Girdwood 		/* validate kcontrol */
15278a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15288a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15299f90af3aSAmadeusz Sławiński 			goto err_sbe;
15308a978234SLiam Girdwood 
15318a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
15328a978234SLiam Girdwood 		if (sbe == NULL)
15339f90af3aSAmadeusz Sławiński 			goto err_sbe;
15348a978234SLiam Girdwood 
15358a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
15365aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(be->priv.size));
15378a978234SLiam Girdwood 
15388a978234SLiam Girdwood 		dev_dbg(tplg->dev,
15398a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
15408a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
15418a978234SLiam Girdwood 
15421ad741d0SColin Ian King 		kc[i].private_value = (long)sbe;
1543267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
15449f90af3aSAmadeusz Sławiński 		if (kc[i].name == NULL)
15459f90af3aSAmadeusz Sławiński 			goto err_sbe;
15468a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
154772bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(be->hdr.access);
15488a978234SLiam Girdwood 
154972bbeda0SPierre-Louis Bossart 		sbe->max = le32_to_cpu(be->max);
15508a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
15518a978234SLiam Girdwood 
15528a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
15532b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
15548a978234SLiam Girdwood 		if (err) {
15558a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
15569f90af3aSAmadeusz Sławiński 			goto err_sbe;
15578a978234SLiam Girdwood 		}
15588a978234SLiam Girdwood 
15598a978234SLiam Girdwood 		/* pass control to driver for optional further init */
15608a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
15618a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
15628a978234SLiam Girdwood 		if (err < 0) {
15638a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
15648a978234SLiam Girdwood 				be->hdr.name);
15659f90af3aSAmadeusz Sławiński 			goto err_sbe;
15668a978234SLiam Girdwood 		}
15678a978234SLiam Girdwood 	}
15688a978234SLiam Girdwood 
15698a978234SLiam Girdwood 	return kc;
15708a978234SLiam Girdwood 
15719f90af3aSAmadeusz Sławiński err_sbe:
15729f90af3aSAmadeusz Sławiński 	for (; i >= 0; i--) {
15739f90af3aSAmadeusz Sławiński 		sbe = (struct soc_bytes_ext *)kc[i].private_value;
15749f90af3aSAmadeusz Sławiński 		kfree(sbe);
1575267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1576267e2c6fSLiam Girdwood 	}
15778a978234SLiam Girdwood 	kfree(kc);
15789f90af3aSAmadeusz Sławiński 
15798a978234SLiam Girdwood 	return NULL;
15808a978234SLiam Girdwood }
15818a978234SLiam Girdwood 
15828a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
15838a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
15848a978234SLiam Girdwood {
15858a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
15868a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
15878a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
15888a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1589eea3dd4fSMengdong Lin 	unsigned int kcontrol_type;
15908a978234SLiam Girdwood 	int ret = 0;
15918a978234SLiam Girdwood 
15928a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15938a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15948a978234SLiam Girdwood 		return -EINVAL;
15958a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15968a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15978a978234SLiam Girdwood 		return -EINVAL;
15988a978234SLiam Girdwood 
15998a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
16008a978234SLiam Girdwood 		w->name, w->id);
16018a978234SLiam Girdwood 
16028a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
16038a978234SLiam Girdwood 
16048a978234SLiam Girdwood 	/* map user to kernel widget ID */
16055aebe7c7SPierre-Louis Bossart 	template.id = get_widget_id(le32_to_cpu(w->id));
1606752c938aSDan Carpenter 	if ((int)template.id < 0)
16078a978234SLiam Girdwood 		return template.id;
16088a978234SLiam Girdwood 
1609c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
16108a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
16118a978234SLiam Girdwood 	if (!template.name)
16128a978234SLiam Girdwood 		return -ENOMEM;
16138a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
16148a978234SLiam Girdwood 	if (!template.sname) {
16158a978234SLiam Girdwood 		ret = -ENOMEM;
16168a978234SLiam Girdwood 		goto err;
16178a978234SLiam Girdwood 	}
16185aebe7c7SPierre-Louis Bossart 	template.reg = le32_to_cpu(w->reg);
16195aebe7c7SPierre-Louis Bossart 	template.shift = le32_to_cpu(w->shift);
16205aebe7c7SPierre-Louis Bossart 	template.mask = le32_to_cpu(w->mask);
16215aebe7c7SPierre-Louis Bossart 	template.subseq = le32_to_cpu(w->subseq);
16228a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
16238a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
16245aebe7c7SPierre-Louis Bossart 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
16255aebe7c7SPierre-Louis Bossart 	template.event_flags = le16_to_cpu(w->event_flags);
16268a978234SLiam Girdwood 	template.dobj.index = tplg->index;
16278a978234SLiam Girdwood 
16288a978234SLiam Girdwood 	tplg->pos +=
16295aebe7c7SPierre-Louis Bossart 		(sizeof(struct snd_soc_tplg_dapm_widget) +
16305aebe7c7SPierre-Louis Bossart 		 le32_to_cpu(w->priv.size));
16315aebe7c7SPierre-Louis Bossart 
16328a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
1633dd5abb74SArnd Bergmann 		kcontrol_type = 0;
16348a978234SLiam Girdwood 		template.num_kcontrols = 0;
16358a978234SLiam Girdwood 		goto widget;
16368a978234SLiam Girdwood 	}
16378a978234SLiam Girdwood 
16388a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
16398a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
16408a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
16418a978234SLiam Girdwood 
16425aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(control_hdr->ops.info)) {
16438a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
16448a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
16458a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
16468a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
16478a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
16488a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1649eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
16505aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16518a978234SLiam Girdwood 		template.kcontrol_news =
16528a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
16538a978234SLiam Girdwood 			template.num_kcontrols);
16548a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16558a978234SLiam Girdwood 			ret = -ENOMEM;
16568a978234SLiam Girdwood 			goto hdr_err;
16578a978234SLiam Girdwood 		}
16588a978234SLiam Girdwood 		break;
16598a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
16608a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
16618a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
16628a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
16638a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1664eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
16655aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16668a978234SLiam Girdwood 		template.kcontrol_news =
16671a7dd6e2SMengdong Lin 			soc_tplg_dapm_widget_denum_create(tplg,
16681a7dd6e2SMengdong Lin 			template.num_kcontrols);
16698a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16708a978234SLiam Girdwood 			ret = -ENOMEM;
16718a978234SLiam Girdwood 			goto hdr_err;
16728a978234SLiam Girdwood 		}
16738a978234SLiam Girdwood 		break;
16748a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
1675eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
16765aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16778a978234SLiam Girdwood 		template.kcontrol_news =
16788a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
16798a978234SLiam Girdwood 				template.num_kcontrols);
16808a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16818a978234SLiam Girdwood 			ret = -ENOMEM;
16828a978234SLiam Girdwood 			goto hdr_err;
16838a978234SLiam Girdwood 		}
16848a978234SLiam Girdwood 		break;
16858a978234SLiam Girdwood 	default:
16868a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
16878a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
16885aebe7c7SPierre-Louis Bossart 			le32_to_cpu(control_hdr->ops.info));
16898a978234SLiam Girdwood 		ret = -EINVAL;
16908a978234SLiam Girdwood 		goto hdr_err;
16918a978234SLiam Girdwood 	}
16928a978234SLiam Girdwood 
16938a978234SLiam Girdwood widget:
16948a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
16958a978234SLiam Girdwood 	if (ret < 0)
16968a978234SLiam Girdwood 		goto hdr_err;
16978a978234SLiam Girdwood 
16988a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
16998a978234SLiam Girdwood 	 * data during sound card init. */
17008a978234SLiam Girdwood 	if (card->instantiated)
17018a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
17028a978234SLiam Girdwood 	else
17038a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
170437e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
170537e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
17068a978234SLiam Girdwood 		goto hdr_err;
17078a978234SLiam Girdwood 	}
17088a978234SLiam Girdwood 
17098a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1710eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
17118a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
17128a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
17138a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1714ebd259d3SLiam Girdwood 
1715ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1716ebd259d3SLiam Girdwood 	if (ret < 0)
1717ebd259d3SLiam Girdwood 		goto ready_err;
1718ebd259d3SLiam Girdwood 
17197620fe91SBard liao 	kfree(template.sname);
17207620fe91SBard liao 	kfree(template.name);
17217620fe91SBard liao 
17228a978234SLiam Girdwood 	return 0;
17238a978234SLiam Girdwood 
1724ebd259d3SLiam Girdwood ready_err:
1725ebd259d3SLiam Girdwood 	snd_soc_tplg_widget_remove(widget);
1726ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
17278a978234SLiam Girdwood hdr_err:
17288a978234SLiam Girdwood 	kfree(template.sname);
17298a978234SLiam Girdwood err:
17308a978234SLiam Girdwood 	kfree(template.name);
17318a978234SLiam Girdwood 	return ret;
17328a978234SLiam Girdwood }
17338a978234SLiam Girdwood 
17348a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
17358a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
17368a978234SLiam Girdwood {
17378a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
17385aebe7c7SPierre-Louis Bossart 	int ret, count, i;
17395aebe7c7SPierre-Louis Bossart 
17405aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
17418a978234SLiam Girdwood 
17428a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
17438a978234SLiam Girdwood 		return 0;
17448a978234SLiam Girdwood 
17458a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
17468a978234SLiam Girdwood 
17478a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
17488a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
17495aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
175006eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
175106eb49f7SMengdong Lin 			return -EINVAL;
175206eb49f7SMengdong Lin 		}
175306eb49f7SMengdong Lin 
17548a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
17557de76b62SMengdong Lin 		if (ret < 0) {
17568a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
17578a978234SLiam Girdwood 				widget->name);
17587de76b62SMengdong Lin 			return ret;
17597de76b62SMengdong Lin 		}
17608a978234SLiam Girdwood 	}
17618a978234SLiam Girdwood 
17628a978234SLiam Girdwood 	return 0;
17638a978234SLiam Girdwood }
17648a978234SLiam Girdwood 
17658a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
17668a978234SLiam Girdwood {
17678a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
17688a978234SLiam Girdwood 	int ret;
17698a978234SLiam Girdwood 
17708a978234SLiam Girdwood 	/* Card might not have been registered at this point.
17718a978234SLiam Girdwood 	 * If so, just return success.
17728a978234SLiam Girdwood 	*/
17738a978234SLiam Girdwood 	if (!card || !card->instantiated) {
17748a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1775cc9d4714SLiam Girdwood 			" widget card binding deferred\n");
17768a978234SLiam Girdwood 		return 0;
17778a978234SLiam Girdwood 	}
17788a978234SLiam Girdwood 
17798a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
17808a978234SLiam Girdwood 	if (ret < 0)
17818a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
17828a978234SLiam Girdwood 			ret);
17838a978234SLiam Girdwood 
17848a978234SLiam Girdwood 	return 0;
17858a978234SLiam Girdwood }
17868a978234SLiam Girdwood 
1787abc3caacSAmadeusz Sławiński static int set_stream_info(struct snd_soc_pcm_stream *stream,
1788b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1789b6b6e4d6SMengdong Lin {
1790b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1791abc3caacSAmadeusz Sławiński 	if (!stream->stream_name)
1792abc3caacSAmadeusz Sławiński 		return -ENOMEM;
1793abc3caacSAmadeusz Sławiński 
17945aebe7c7SPierre-Louis Bossart 	stream->channels_min = le32_to_cpu(caps->channels_min);
17955aebe7c7SPierre-Louis Bossart 	stream->channels_max = le32_to_cpu(caps->channels_max);
17965aebe7c7SPierre-Louis Bossart 	stream->rates = le32_to_cpu(caps->rates);
17975aebe7c7SPierre-Louis Bossart 	stream->rate_min = le32_to_cpu(caps->rate_min);
17985aebe7c7SPierre-Louis Bossart 	stream->rate_max = le32_to_cpu(caps->rate_max);
17995aebe7c7SPierre-Louis Bossart 	stream->formats = le64_to_cpu(caps->formats);
18005aebe7c7SPierre-Louis Bossart 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1801abc3caacSAmadeusz Sławiński 
1802abc3caacSAmadeusz Sławiński 	return 0;
1803b6b6e4d6SMengdong Lin }
1804b6b6e4d6SMengdong Lin 
18050038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
18060038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
18070038be9aSMengdong Lin {
18080038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
18090038be9aSMengdong Lin 		dai_drv->symmetric_rates =
18100038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
18110038be9aSMengdong Lin 
18120038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
18130038be9aSMengdong Lin 		dai_drv->symmetric_channels =
18140038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
18150038be9aSMengdong Lin 			1 : 0;
18160038be9aSMengdong Lin 
18170038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
18180038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
18190038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
18200038be9aSMengdong Lin 			1 : 0;
18210038be9aSMengdong Lin }
18220038be9aSMengdong Lin 
182364527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
182464527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
182564527e8aSMengdong Lin {
182664527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
182764527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
182864527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
1829e443c205SKuninori Morimoto 	struct snd_soc_dai *dai;
1830e443c205SKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1831e443c205SKuninori Morimoto 		snd_soc_component_get_dapm(tplg->comp);
183264527e8aSMengdong Lin 	int ret;
183364527e8aSMengdong Lin 
183464527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
183564527e8aSMengdong Lin 	if (dai_drv == NULL)
183664527e8aSMengdong Lin 		return -ENOMEM;
183764527e8aSMengdong Lin 
1838abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
18398f27c4abSMengdong Lin 		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1840abc3caacSAmadeusz Sławiński 		if (!dai_drv->name) {
1841abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1842abc3caacSAmadeusz Sławiński 			goto err;
1843abc3caacSAmadeusz Sławiński 		}
1844abc3caacSAmadeusz Sławiński 	}
18455aebe7c7SPierre-Louis Bossart 	dai_drv->id = le32_to_cpu(pcm->dai_id);
184664527e8aSMengdong Lin 
184764527e8aSMengdong Lin 	if (pcm->playback) {
184864527e8aSMengdong Lin 		stream = &dai_drv->playback;
184964527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1850abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
1851abc3caacSAmadeusz Sławiński 		if (ret < 0)
1852abc3caacSAmadeusz Sławiński 			goto err;
185364527e8aSMengdong Lin 	}
185464527e8aSMengdong Lin 
185564527e8aSMengdong Lin 	if (pcm->capture) {
185664527e8aSMengdong Lin 		stream = &dai_drv->capture;
185764527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1858abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
1859abc3caacSAmadeusz Sławiński 		if (ret < 0)
1860abc3caacSAmadeusz Sławiński 			goto err;
186164527e8aSMengdong Lin 	}
186264527e8aSMengdong Lin 
18635db6aab6SLiam Girdwood 	if (pcm->compress)
18645db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
18655db6aab6SLiam Girdwood 
186664527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1867c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
186864527e8aSMengdong Lin 	if (ret < 0) {
186964527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1870abc3caacSAmadeusz Sławiński 		goto err;
187164527e8aSMengdong Lin 	}
187264527e8aSMengdong Lin 
187364527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
187464527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
187564527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
187664527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
187764527e8aSMengdong Lin 
187864527e8aSMengdong Lin 	/* register the DAI to the component */
1879e443c205SKuninori Morimoto 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1880e443c205SKuninori Morimoto 	if (!dai)
1881e443c205SKuninori Morimoto 		return -ENOMEM;
1882e443c205SKuninori Morimoto 
1883e443c205SKuninori Morimoto 	/* Create the DAI widgets here */
1884e443c205SKuninori Morimoto 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1885e443c205SKuninori Morimoto 	if (ret != 0) {
1886e443c205SKuninori Morimoto 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1887e443c205SKuninori Morimoto 		snd_soc_unregister_dai(dai);
1888e443c205SKuninori Morimoto 		return ret;
1889e443c205SKuninori Morimoto 	}
1890e443c205SKuninori Morimoto 
1891abc3caacSAmadeusz Sławiński 	return 0;
1892abc3caacSAmadeusz Sławiński 
1893abc3caacSAmadeusz Sławiński err:
1894abc3caacSAmadeusz Sławiński 	kfree(dai_drv->playback.stream_name);
1895abc3caacSAmadeusz Sławiński 	kfree(dai_drv->capture.stream_name);
1896abc3caacSAmadeusz Sławiński 	kfree(dai_drv->name);
1897abc3caacSAmadeusz Sławiński 	kfree(dai_drv);
1898abc3caacSAmadeusz Sławiński 
1899e443c205SKuninori Morimoto 	return ret;
190064527e8aSMengdong Lin }
190164527e8aSMengdong Lin 
1902717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1903717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1904717a8e72SMengdong Lin {
1905717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1906717a8e72SMengdong Lin 		link->symmetric_rates =
1907717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1908717a8e72SMengdong Lin 
1909717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1910717a8e72SMengdong Lin 		link->symmetric_channels =
1911717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1912717a8e72SMengdong Lin 			1 : 0;
1913717a8e72SMengdong Lin 
1914717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1915717a8e72SMengdong Lin 		link->symmetric_samplebits =
1916717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1917717a8e72SMengdong Lin 			1 : 0;
19186ff67ccaSMengdong Lin 
19196ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
19206ff67ccaSMengdong Lin 		link->ignore_suspend =
19216ff67ccaSMengdong Lin 		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
19226ff67ccaSMengdong Lin 		1 : 0;
1923717a8e72SMengdong Lin }
1924717a8e72SMengdong Lin 
192567d1c21eSGuneshwor Singh /* create the FE DAI link */
1926ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1927acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1928acfc7d46SMengdong Lin {
1929acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
193023b946ceSKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
1931acfc7d46SMengdong Lin 	int ret;
1932acfc7d46SMengdong Lin 
19333e6de894SPierre-Louis Bossart 	/* link + cpu + codec + platform */
19343e6de894SPierre-Louis Bossart 	link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1935acfc7d46SMengdong Lin 	if (link == NULL)
1936acfc7d46SMengdong Lin 		return -ENOMEM;
1937acfc7d46SMengdong Lin 
193823b946ceSKuninori Morimoto 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
193923b946ceSKuninori Morimoto 
194023b946ceSKuninori Morimoto 	link->cpus	= &dlc[0];
194123b946ceSKuninori Morimoto 	link->codecs	= &dlc[1];
19423e6de894SPierre-Louis Bossart 	link->platforms	= &dlc[2];
194323b946ceSKuninori Morimoto 
194423b946ceSKuninori Morimoto 	link->num_cpus	 = 1;
194523b946ceSKuninori Morimoto 	link->num_codecs = 1;
19463e6de894SPierre-Louis Bossart 	link->num_platforms = 1;
194723b946ceSKuninori Morimoto 
19488ce1cbd6SJaroslav Kysela 	link->dobj.index = tplg->index;
19498ce1cbd6SJaroslav Kysela 	link->dobj.ops = tplg->ops;
19508ce1cbd6SJaroslav Kysela 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
19518ce1cbd6SJaroslav Kysela 
19528f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
19538f27c4abSMengdong Lin 		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
19548f27c4abSMengdong Lin 		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1955abc3caacSAmadeusz Sławiński 		if (!link->name || !link->stream_name) {
1956abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1957abc3caacSAmadeusz Sławiński 			goto err;
1958abc3caacSAmadeusz Sławiński 		}
19598f27c4abSMengdong Lin 	}
19605aebe7c7SPierre-Louis Bossart 	link->id = le32_to_cpu(pcm->pcm_id);
1961acfc7d46SMengdong Lin 
1962abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
196323b946ceSKuninori Morimoto 		link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1964abc3caacSAmadeusz Sławiński 		if (!link->cpus->dai_name) {
1965abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1966abc3caacSAmadeusz Sławiński 			goto err;
1967abc3caacSAmadeusz Sławiński 		}
1968abc3caacSAmadeusz Sławiński 	}
19698f27c4abSMengdong Lin 
197023b946ceSKuninori Morimoto 	link->codecs->name = "snd-soc-dummy";
197123b946ceSKuninori Morimoto 	link->codecs->dai_name = "snd-soc-dummy-dai";
197267d1c21eSGuneshwor Singh 
19733e6de894SPierre-Louis Bossart 	link->platforms->name = "snd-soc-dummy";
19743e6de894SPierre-Louis Bossart 
197567d1c21eSGuneshwor Singh 	/* enable DPCM */
197667d1c21eSGuneshwor Singh 	link->dynamic = 1;
19775aebe7c7SPierre-Louis Bossart 	link->dpcm_playback = le32_to_cpu(pcm->playback);
19785aebe7c7SPierre-Louis Bossart 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1979717a8e72SMengdong Lin 	if (pcm->flag_mask)
19805aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
19815aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flag_mask),
19825aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flags));
198367d1c21eSGuneshwor Singh 
1984acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1985c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1986acfc7d46SMengdong Lin 	if (ret < 0) {
1987acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
198876d27036SDragos Tarcatu 		goto err;
198976d27036SDragos Tarcatu 	}
199076d27036SDragos Tarcatu 
19912acf6ce2SMark Brown 	ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
199276d27036SDragos Tarcatu 	if (ret < 0) {
199376d27036SDragos Tarcatu 		dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
199476d27036SDragos Tarcatu 		goto err;
1995acfc7d46SMengdong Lin 	}
1996acfc7d46SMengdong Lin 
1997acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1998acfc7d46SMengdong Lin 
1999acfc7d46SMengdong Lin 	return 0;
200076d27036SDragos Tarcatu err:
200176d27036SDragos Tarcatu 	kfree(link->name);
200276d27036SDragos Tarcatu 	kfree(link->stream_name);
200376d27036SDragos Tarcatu 	kfree(link->cpus->dai_name);
200476d27036SDragos Tarcatu 	kfree(link);
200576d27036SDragos Tarcatu 	return ret;
2006acfc7d46SMengdong Lin }
2007acfc7d46SMengdong Lin 
2008acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
200964527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
201064527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
201164527e8aSMengdong Lin {
2012acfc7d46SMengdong Lin 	int ret;
2013acfc7d46SMengdong Lin 
2014acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
2015acfc7d46SMengdong Lin 	if (ret < 0)
2016acfc7d46SMengdong Lin 		return ret;
2017acfc7d46SMengdong Lin 
2018ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
201964527e8aSMengdong Lin }
202064527e8aSMengdong Lin 
202155726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
202255726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
202355726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
202455726dc9SMengdong Lin {
20255aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
202655726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
202755726dc9SMengdong Lin 	dest->formats = src->formats;
202855726dc9SMengdong Lin 	dest->rates = src->rates;
202955726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
203055726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
203155726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
203255726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
203355726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
203455726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
203555726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
203655726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
203755726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
203855726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
203955726dc9SMengdong Lin }
204055726dc9SMengdong Lin 
204155726dc9SMengdong Lin /**
204255726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
204355726dc9SMengdong Lin  * @tplg: topology context
204455726dc9SMengdong Lin  * @src: older version of pcm as a source
204555726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
204655726dc9SMengdong Lin  *
204755726dc9SMengdong Lin  * Support from vesion 4. User should free the returned pcm manually.
204855726dc9SMengdong Lin  */
204955726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
205055726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
205155726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
205255726dc9SMengdong Lin {
205355726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
205455726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
205555726dc9SMengdong Lin 	int i;
205655726dc9SMengdong Lin 
205755726dc9SMengdong Lin 	*pcm = NULL;
205855726dc9SMengdong Lin 
20595aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
206055726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
206155726dc9SMengdong Lin 		return -EINVAL;
206255726dc9SMengdong Lin 	}
206355726dc9SMengdong Lin 
206455726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
206555726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
206655726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
206755726dc9SMengdong Lin 	if (!dest)
206855726dc9SMengdong Lin 		return -ENOMEM;
206955726dc9SMengdong Lin 
20705aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
207155726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
207255726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
207355726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
207455726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
207555726dc9SMengdong Lin 	dest->playback = src_v4->playback;
207655726dc9SMengdong Lin 	dest->capture = src_v4->capture;
207755726dc9SMengdong Lin 	dest->compress = src_v4->compress;
207855726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
20795aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
208055726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
208155726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
208255726dc9SMengdong Lin 
208355726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
208455726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
208555726dc9SMengdong Lin 
208655726dc9SMengdong Lin 	*pcm = dest;
208755726dc9SMengdong Lin 	return 0;
208855726dc9SMengdong Lin }
208955726dc9SMengdong Lin 
209064527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
20918a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
20928a978234SLiam Girdwood {
209355726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
20945aebe7c7SPierre-Louis Bossart 	int count;
20955aebe7c7SPierre-Louis Bossart 	int size;
2096fd340455SVinod Koul 	int i;
209755726dc9SMengdong Lin 	bool abi_match;
2098a3039aefSDragos Tarcatu 	int ret;
20998a978234SLiam Girdwood 
21005aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
21015aebe7c7SPierre-Louis Bossart 
21028a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
21038a978234SLiam Girdwood 		return 0;
21048a978234SLiam Girdwood 
210555726dc9SMengdong Lin 	/* check the element size and count */
210655726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
21075aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(pcm->size);
21085aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_pcm)
21095aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
211055726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
21115aebe7c7SPierre-Louis Bossart 			size);
211255726dc9SMengdong Lin 		return -EINVAL;
211355726dc9SMengdong Lin 	}
211455726dc9SMengdong Lin 
21158a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
21165aebe7c7SPierre-Louis Bossart 				      size, count,
21175aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
21185aebe7c7SPierre-Louis Bossart 				      "PCM DAI")) {
21198a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
21208a978234SLiam Girdwood 			count);
21218a978234SLiam Girdwood 		return -EINVAL;
21228a978234SLiam Girdwood 	}
21238a978234SLiam Girdwood 
212464527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
212555726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
21265aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(pcm->size);
212755726dc9SMengdong Lin 
212855726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
212955726dc9SMengdong Lin 		 * if abi not match.
213055726dc9SMengdong Lin 		 */
21315aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*pcm)) {
213255726dc9SMengdong Lin 			abi_match = true;
213355726dc9SMengdong Lin 			_pcm = pcm;
213455726dc9SMengdong Lin 		} else {
213555726dc9SMengdong Lin 			abi_match = false;
2136fd340455SVinod Koul 			pcm_new_ver(tplg, pcm, &_pcm);
213706eb49f7SMengdong Lin 		}
213806eb49f7SMengdong Lin 
213955726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
2140a3039aefSDragos Tarcatu 		ret = soc_tplg_pcm_create(tplg, _pcm);
2141a3039aefSDragos Tarcatu 		if (ret < 0) {
2142a3039aefSDragos Tarcatu 			if (!abi_match)
2143a3039aefSDragos Tarcatu 				kfree(_pcm);
2144a3039aefSDragos Tarcatu 			return ret;
2145a3039aefSDragos Tarcatu 		}
214655726dc9SMengdong Lin 
2147717a8e72SMengdong Lin 		/* offset by version-specific struct size and
2148717a8e72SMengdong Lin 		 * real priv data size
2149717a8e72SMengdong Lin 		 */
21505aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2151717a8e72SMengdong Lin 
215255726dc9SMengdong Lin 		if (!abi_match)
215355726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
215464527e8aSMengdong Lin 	}
215564527e8aSMengdong Lin 
21568a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
21578a978234SLiam Girdwood 
21588a978234SLiam Girdwood 	return 0;
21598a978234SLiam Girdwood }
21608a978234SLiam Girdwood 
21610038be9aSMengdong Lin /**
2162593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
21638abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
2164593d9e52SMengdong Lin  * @cfg: physical link configs.
2165593d9e52SMengdong Lin  *
2166593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
2167593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
2168593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
2169593d9e52SMengdong Lin  */
2170593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
2171593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
2172593d9e52SMengdong Lin {
2173593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
2174593d9e52SMengdong Lin 	unsigned char bclk_master, fsync_master;
2175593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
2176593d9e52SMengdong Lin 	int i;
2177593d9e52SMengdong Lin 
21785aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2179593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
2180593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
2181593d9e52SMengdong Lin 			continue;
2182593d9e52SMengdong Lin 
21835aebe7c7SPierre-Louis Bossart 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
21845aebe7c7SPierre-Louis Bossart 			SND_SOC_DAIFMT_FORMAT_MASK;
2185593d9e52SMengdong Lin 
2186933e1c4aSKirill Marinushkin 		/* clock gating */
2187fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
2188fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2189933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2190fbeabd09SKirill Marinushkin 			break;
2191fbeabd09SKirill Marinushkin 
2192fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2193933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2194fbeabd09SKirill Marinushkin 			break;
2195fbeabd09SKirill Marinushkin 
2196fbeabd09SKirill Marinushkin 		default:
2197fbeabd09SKirill Marinushkin 			/* ignore the value */
2198fbeabd09SKirill Marinushkin 			break;
2199fbeabd09SKirill Marinushkin 		}
2200933e1c4aSKirill Marinushkin 
2201593d9e52SMengdong Lin 		/* clock signal polarity */
2202593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
2203593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
2204593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
2205593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2206593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
2207593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2208593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
2209593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2210593d9e52SMengdong Lin 		else
2211593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2212593d9e52SMengdong Lin 
2213593d9e52SMengdong Lin 		/* clock masters */
2214a941e2faSKirill Marinushkin 		bclk_master = (hw_config->bclk_master ==
2215a941e2faSKirill Marinushkin 			       SND_SOC_TPLG_BCLK_CM);
2216a941e2faSKirill Marinushkin 		fsync_master = (hw_config->fsync_master ==
2217a941e2faSKirill Marinushkin 				SND_SOC_TPLG_FSYNC_CM);
2218a941e2faSKirill Marinushkin 		if (bclk_master && fsync_master)
2219593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2220593d9e52SMengdong Lin 		else if (!bclk_master && fsync_master)
2221a941e2faSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2222a941e2faSKirill Marinushkin 		else if (bclk_master && !fsync_master)
2223593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2224593d9e52SMengdong Lin 		else
2225593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2226593d9e52SMengdong Lin 	}
2227593d9e52SMengdong Lin }
2228593d9e52SMengdong Lin 
2229593d9e52SMengdong Lin /**
2230593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
2231593d9e52SMengdong Lin  * version of source.
22328abab35fSCharles Keepax  * @tplg: topology context
2233593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
2234593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
2235593d9e52SMengdong Lin  *
2236593d9e52SMengdong Lin  * Support from vesion 4. User need free the returned link config manually.
2237593d9e52SMengdong Lin  */
2238593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
2239593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
2240593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
2241593d9e52SMengdong Lin {
2242593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
2243593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
2244593d9e52SMengdong Lin 	int i;
2245593d9e52SMengdong Lin 
2246593d9e52SMengdong Lin 	*link = NULL;
2247593d9e52SMengdong Lin 
22485aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) !=
22495aebe7c7SPierre-Louis Bossart 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
2250593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2251593d9e52SMengdong Lin 		return -EINVAL;
2252593d9e52SMengdong Lin 	}
2253593d9e52SMengdong Lin 
2254593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2255593d9e52SMengdong Lin 
2256593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2257593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2258593d9e52SMengdong Lin 	if (!dest)
2259593d9e52SMengdong Lin 		return -ENOMEM;
2260593d9e52SMengdong Lin 
22615aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
2262593d9e52SMengdong Lin 	dest->id = src_v4->id;
2263593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
22645aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2265593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2266593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2267593d9e52SMengdong Lin 
2268593d9e52SMengdong Lin 	*link = dest;
2269593d9e52SMengdong Lin 	return 0;
2270593d9e52SMengdong Lin }
2271593d9e52SMengdong Lin 
2272d6f31e0eSKuninori Morimoto /**
2273d6f31e0eSKuninori Morimoto  * snd_soc_find_dai_link - Find a DAI link
2274d6f31e0eSKuninori Morimoto  *
2275d6f31e0eSKuninori Morimoto  * @card: soc card
2276d6f31e0eSKuninori Morimoto  * @id: DAI link ID to match
2277d6f31e0eSKuninori Morimoto  * @name: DAI link name to match, optional
2278d6f31e0eSKuninori Morimoto  * @stream_name: DAI link stream name to match, optional
2279d6f31e0eSKuninori Morimoto  *
2280d6f31e0eSKuninori Morimoto  * This function will search all existing DAI links of the soc card to
2281d6f31e0eSKuninori Morimoto  * find the link of the same ID. Since DAI links may not have their
2282d6f31e0eSKuninori Morimoto  * unique ID, so name and stream name should also match if being
2283d6f31e0eSKuninori Morimoto  * specified.
2284d6f31e0eSKuninori Morimoto  *
2285d6f31e0eSKuninori Morimoto  * Return: pointer of DAI link, or NULL if not found.
2286d6f31e0eSKuninori Morimoto  */
2287d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2288d6f31e0eSKuninori Morimoto 						      int id, const char *name,
2289d6f31e0eSKuninori Morimoto 						      const char *stream_name)
2290d6f31e0eSKuninori Morimoto {
2291d6f31e0eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
2292d6f31e0eSKuninori Morimoto 	struct snd_soc_dai_link *link;
2293d6f31e0eSKuninori Morimoto 
2294d6f31e0eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2295d6f31e0eSKuninori Morimoto 		link = rtd->dai_link;
2296d6f31e0eSKuninori Morimoto 
2297d6f31e0eSKuninori Morimoto 		if (link->id != id)
2298d6f31e0eSKuninori Morimoto 			continue;
2299d6f31e0eSKuninori Morimoto 
2300d6f31e0eSKuninori Morimoto 		if (name && (!link->name || strcmp(name, link->name)))
2301d6f31e0eSKuninori Morimoto 			continue;
2302d6f31e0eSKuninori Morimoto 
2303d6f31e0eSKuninori Morimoto 		if (stream_name && (!link->stream_name
2304d6f31e0eSKuninori Morimoto 				    || strcmp(stream_name, link->stream_name)))
2305d6f31e0eSKuninori Morimoto 			continue;
2306d6f31e0eSKuninori Morimoto 
2307d6f31e0eSKuninori Morimoto 		return link;
2308d6f31e0eSKuninori Morimoto 	}
2309d6f31e0eSKuninori Morimoto 
2310d6f31e0eSKuninori Morimoto 	return NULL;
2311d6f31e0eSKuninori Morimoto }
2312d6f31e0eSKuninori Morimoto 
2313593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2314593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2315593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2316593d9e52SMengdong Lin {
2317593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2318593d9e52SMengdong Lin 	const char *name, *stream_name;
2319dbab1cb8SMengdong Lin 	size_t len;
2320593d9e52SMengdong Lin 	int ret;
2321593d9e52SMengdong Lin 
2322dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2323dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2324dbab1cb8SMengdong Lin 		return -EINVAL;
2325dbab1cb8SMengdong Lin 	else if (len)
2326dbab1cb8SMengdong Lin 		name = cfg->name;
2327dbab1cb8SMengdong Lin 	else
2328dbab1cb8SMengdong Lin 		name = NULL;
2329dbab1cb8SMengdong Lin 
2330dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2331dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2332dbab1cb8SMengdong Lin 		return -EINVAL;
2333dbab1cb8SMengdong Lin 	else if (len)
2334dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2335dbab1cb8SMengdong Lin 	else
2336dbab1cb8SMengdong Lin 		stream_name = NULL;
2337593d9e52SMengdong Lin 
23385aebe7c7SPierre-Louis Bossart 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2339593d9e52SMengdong Lin 				     name, stream_name);
2340593d9e52SMengdong Lin 	if (!link) {
2341593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2342593d9e52SMengdong Lin 			name, cfg->id);
2343593d9e52SMengdong Lin 		return -EINVAL;
2344593d9e52SMengdong Lin 	}
2345593d9e52SMengdong Lin 
2346593d9e52SMengdong Lin 	/* hw format */
2347593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2348593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2349593d9e52SMengdong Lin 
2350593d9e52SMengdong Lin 	/* flags */
2351593d9e52SMengdong Lin 	if (cfg->flag_mask)
23525aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
23535aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flag_mask),
23545aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flags));
2355593d9e52SMengdong Lin 
2356593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2357c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2358593d9e52SMengdong Lin 	if (ret < 0) {
2359593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2360593d9e52SMengdong Lin 		return ret;
2361593d9e52SMengdong Lin 	}
2362593d9e52SMengdong Lin 
2363adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2364adfebb51SBard liao 	link->dobj.index = tplg->index;
2365adfebb51SBard liao 	link->dobj.ops = tplg->ops;
2366adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2367adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2368adfebb51SBard liao 
2369593d9e52SMengdong Lin 	return 0;
2370593d9e52SMengdong Lin }
2371593d9e52SMengdong Lin 
2372593d9e52SMengdong Lin 
2373593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2374593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2375593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2376593d9e52SMengdong Lin {
2377593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
23785aebe7c7SPierre-Louis Bossart 	int count;
23795aebe7c7SPierre-Louis Bossart 	int size;
2380593d9e52SMengdong Lin 	int i, ret;
2381593d9e52SMengdong Lin 	bool abi_match;
2382593d9e52SMengdong Lin 
23835aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
23845aebe7c7SPierre-Louis Bossart 
2385593d9e52SMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_LINK) {
23865aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
23875aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
2388593d9e52SMengdong Lin 		return 0;
2389593d9e52SMengdong Lin 	};
2390593d9e52SMengdong Lin 
2391593d9e52SMengdong Lin 	/* check the element size and count */
2392593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
23935aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(link->size);
23945aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_link_config)
23955aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2396593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
23975aebe7c7SPierre-Louis Bossart 			size);
2398593d9e52SMengdong Lin 		return -EINVAL;
2399593d9e52SMengdong Lin 	}
2400593d9e52SMengdong Lin 
2401593d9e52SMengdong Lin 	if (soc_tplg_check_elem_count(tplg,
24025aebe7c7SPierre-Louis Bossart 				      size, count,
24035aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
24045aebe7c7SPierre-Louis Bossart 				      "physical link config")) {
2405593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2406593d9e52SMengdong Lin 			count);
2407593d9e52SMengdong Lin 		return -EINVAL;
2408593d9e52SMengdong Lin 	}
2409593d9e52SMengdong Lin 
2410593d9e52SMengdong Lin 	/* config physical DAI links */
2411593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2412593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
24135aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(link->size);
24145aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*link)) {
2415593d9e52SMengdong Lin 			abi_match = true;
2416593d9e52SMengdong Lin 			_link = link;
2417593d9e52SMengdong Lin 		} else {
2418593d9e52SMengdong Lin 			abi_match = false;
2419593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2420593d9e52SMengdong Lin 			if (ret < 0)
2421593d9e52SMengdong Lin 				return ret;
2422593d9e52SMengdong Lin 		}
2423593d9e52SMengdong Lin 
2424593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
24252b2d5c4dSDragos Tarcatu 		if (ret < 0) {
24262b2d5c4dSDragos Tarcatu 			if (!abi_match)
24272b2d5c4dSDragos Tarcatu 				kfree(_link);
2428593d9e52SMengdong Lin 			return ret;
24292b2d5c4dSDragos Tarcatu 		}
2430593d9e52SMengdong Lin 
2431593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2432593d9e52SMengdong Lin 		 * real priv data size
2433593d9e52SMengdong Lin 		 */
24345aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2435593d9e52SMengdong Lin 
2436593d9e52SMengdong Lin 		if (!abi_match)
2437593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2438593d9e52SMengdong Lin 	}
2439593d9e52SMengdong Lin 
2440593d9e52SMengdong Lin 	return 0;
2441593d9e52SMengdong Lin }
2442593d9e52SMengdong Lin 
2443593d9e52SMengdong Lin /**
24449aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
24450038be9aSMengdong Lin  * @tplg: topology context
24469aa3f034SMengdong Lin  * @d: physical DAI configs.
24470038be9aSMengdong Lin  *
24489aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
24499aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
24500038be9aSMengdong Lin  */
24519aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
24529aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
24530038be9aSMengdong Lin {
24545aebe7c7SPierre-Louis Bossart 	struct snd_soc_dai_link_component dai_component;
24550038be9aSMengdong Lin 	struct snd_soc_dai *dai;
24560038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
24570038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
24580038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
24590038be9aSMengdong Lin 	int ret;
24600038be9aSMengdong Lin 
24615aebe7c7SPierre-Louis Bossart 	memset(&dai_component, 0, sizeof(dai_component));
24625aebe7c7SPierre-Louis Bossart 
24639aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
24640038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
24650038be9aSMengdong Lin 	if (!dai) {
24669aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
24679aa3f034SMengdong Lin 			d->dai_name);
24680038be9aSMengdong Lin 		return -EINVAL;
24690038be9aSMengdong Lin 	}
24700038be9aSMengdong Lin 
24715aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(d->dai_id) != dai->id) {
24729aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
24739aa3f034SMengdong Lin 			d->dai_name);
24740038be9aSMengdong Lin 		return -EINVAL;
24750038be9aSMengdong Lin 	}
24760038be9aSMengdong Lin 
24770038be9aSMengdong Lin 	dai_drv = dai->driver;
24780038be9aSMengdong Lin 	if (!dai_drv)
24790038be9aSMengdong Lin 		return -EINVAL;
24800038be9aSMengdong Lin 
24819aa3f034SMengdong Lin 	if (d->playback) {
24820038be9aSMengdong Lin 		stream = &dai_drv->playback;
24839aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2484abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
2485abc3caacSAmadeusz Sławiński 		if (ret < 0)
2486abc3caacSAmadeusz Sławiński 			goto err;
24870038be9aSMengdong Lin 	}
24880038be9aSMengdong Lin 
24899aa3f034SMengdong Lin 	if (d->capture) {
24900038be9aSMengdong Lin 		stream = &dai_drv->capture;
24919aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2492abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
2493abc3caacSAmadeusz Sławiński 		if (ret < 0)
2494abc3caacSAmadeusz Sławiński 			goto err;
24950038be9aSMengdong Lin 	}
24960038be9aSMengdong Lin 
24979aa3f034SMengdong Lin 	if (d->flag_mask)
24985aebe7c7SPierre-Louis Bossart 		set_dai_flags(dai_drv,
24995aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flag_mask),
25005aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flags));
25010038be9aSMengdong Lin 
25020038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2503c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
25040038be9aSMengdong Lin 	if (ret < 0) {
25050038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2506abc3caacSAmadeusz Sławiński 		goto err;
25070038be9aSMengdong Lin 	}
25080038be9aSMengdong Lin 
25090038be9aSMengdong Lin 	return 0;
2510abc3caacSAmadeusz Sławiński 
2511abc3caacSAmadeusz Sławiński err:
2512abc3caacSAmadeusz Sławiński 	kfree(dai_drv->playback.stream_name);
2513abc3caacSAmadeusz Sławiński 	kfree(dai_drv->capture.stream_name);
2514abc3caacSAmadeusz Sławiński 	return ret;
25150038be9aSMengdong Lin }
25160038be9aSMengdong Lin 
25179aa3f034SMengdong Lin /* load physical DAI elements */
25189aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
25190038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
25200038be9aSMengdong Lin {
25219aa3f034SMengdong Lin 	struct snd_soc_tplg_dai *dai;
25225aebe7c7SPierre-Louis Bossart 	int count;
25230038be9aSMengdong Lin 	int i;
25240038be9aSMengdong Lin 
25255aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
25265aebe7c7SPierre-Louis Bossart 
25270038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
25280038be9aSMengdong Lin 		return 0;
25290038be9aSMengdong Lin 
25300038be9aSMengdong Lin 	/* config the existing BE DAIs */
25310038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
25329aa3f034SMengdong Lin 		dai = (struct snd_soc_tplg_dai *)tplg->pos;
25335aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
25349aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
25350038be9aSMengdong Lin 			return -EINVAL;
25360038be9aSMengdong Lin 		}
25370038be9aSMengdong Lin 
25389aa3f034SMengdong Lin 		soc_tplg_dai_config(tplg, dai);
25395aebe7c7SPierre-Louis Bossart 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
25400038be9aSMengdong Lin 	}
25410038be9aSMengdong Lin 
25420038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
25430038be9aSMengdong Lin 	return 0;
25440038be9aSMengdong Lin }
25450038be9aSMengdong Lin 
2546583958faSMengdong Lin /**
2547583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2548583958faSMengdong Lin  * of source.
25498abab35fSCharles Keepax  * @tplg: topology context
2550583958faSMengdong Lin  * @src: old version of manifest as a source
2551583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2552583958faSMengdong Lin  *
2553583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
2554583958faSMengdong Lin  */
2555583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2556583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2557583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2558583958faSMengdong Lin {
2559583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2560583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
25615aebe7c7SPierre-Louis Bossart 	int size;
2562583958faSMengdong Lin 
2563583958faSMengdong Lin 	*manifest = NULL;
2564583958faSMengdong Lin 
25655aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(src->size);
25665aebe7c7SPierre-Louis Bossart 	if (size != sizeof(*src_v4)) {
2567ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
25685aebe7c7SPierre-Louis Bossart 			 size);
25695aebe7c7SPierre-Louis Bossart 		if (size)
2570583958faSMengdong Lin 			return -EINVAL;
25715aebe7c7SPierre-Louis Bossart 		src->size = cpu_to_le32(sizeof(*src_v4));
2572583958faSMengdong Lin 	}
2573583958faSMengdong Lin 
2574583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2575583958faSMengdong Lin 
2576583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
25775aebe7c7SPierre-Louis Bossart 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
25785aebe7c7SPierre-Louis Bossart 		       GFP_KERNEL);
2579583958faSMengdong Lin 	if (!dest)
2580583958faSMengdong Lin 		return -ENOMEM;
2581583958faSMengdong Lin 
25825aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2583583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2584583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2585583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2586583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2587583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2588583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2589583958faSMengdong Lin 	if (dest->priv.size)
2590583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
25915aebe7c7SPierre-Louis Bossart 		       le32_to_cpu(src_v4->priv.size));
2592583958faSMengdong Lin 
2593583958faSMengdong Lin 	*manifest = dest;
2594583958faSMengdong Lin 	return 0;
2595583958faSMengdong Lin }
25960038be9aSMengdong Lin 
25978a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
25988a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
25998a978234SLiam Girdwood {
2600583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2601583958faSMengdong Lin 	bool abi_match;
2602242c46c0SDragos Tarcatu 	int ret = 0;
26038a978234SLiam Girdwood 
26048a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
26058a978234SLiam Girdwood 		return 0;
26068a978234SLiam Girdwood 
26078a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2608583958faSMengdong Lin 
2609583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
26105aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2611583958faSMengdong Lin 		abi_match = true;
2612583958faSMengdong Lin 		_manifest = manifest;
2613583958faSMengdong Lin 	} else {
2614583958faSMengdong Lin 		abi_match = false;
2615242c46c0SDragos Tarcatu 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2616242c46c0SDragos Tarcatu 		if (ret < 0)
2617242c46c0SDragos Tarcatu 			return ret;
261806eb49f7SMengdong Lin 	}
261906eb49f7SMengdong Lin 
2620583958faSMengdong Lin 	/* pass control to component driver for optional further init */
2621c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->manifest)
2622242c46c0SDragos Tarcatu 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
26238a978234SLiam Girdwood 
2624583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2625583958faSMengdong Lin 		kfree(_manifest);
2626583958faSMengdong Lin 
2627242c46c0SDragos Tarcatu 	return ret;
26288a978234SLiam Girdwood }
26298a978234SLiam Girdwood 
26308a978234SLiam Girdwood /* validate header magic, size and type */
26318a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
26328a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
26338a978234SLiam Girdwood {
26348a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
26358a978234SLiam Girdwood 		return 0;
26368a978234SLiam Girdwood 
26375aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
263806eb49f7SMengdong Lin 		dev_err(tplg->dev,
263906eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
26405aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
264106eb49f7SMengdong Lin 			tplg->fw->size);
264206eb49f7SMengdong Lin 		return -EINVAL;
264306eb49f7SMengdong Lin 	}
264406eb49f7SMengdong Lin 
26458a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
26462114171dSPierre-Louis Bossart 	if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
26478a978234SLiam Girdwood 		dev_err(tplg->dev,
26488a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
26498a978234SLiam Girdwood 			tplg->pass, hdr->magic,
26508a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
26518a978234SLiam Girdwood 		return -EINVAL;
26528a978234SLiam Girdwood 	}
26538a978234SLiam Girdwood 
26545aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
26558a978234SLiam Girdwood 		dev_err(tplg->dev,
26568a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
26578a978234SLiam Girdwood 			tplg->pass, hdr->magic,
26588a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
26598a978234SLiam Girdwood 		return -EINVAL;
26608a978234SLiam Girdwood 	}
26618a978234SLiam Girdwood 
2662288b8da7SMengdong Lin 	/* Support ABI from version 4 */
26635aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
26645aebe7c7SPierre-Louis Bossart 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
26658a978234SLiam Girdwood 		dev_err(tplg->dev,
26668a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
26678a978234SLiam Girdwood 			tplg->pass, hdr->abi,
26688a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
26698a978234SLiam Girdwood 			tplg->fw->size);
26708a978234SLiam Girdwood 		return -EINVAL;
26718a978234SLiam Girdwood 	}
26728a978234SLiam Girdwood 
26738a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
26748a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
26758a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
26768a978234SLiam Girdwood 		return -EINVAL;
26778a978234SLiam Girdwood 	}
26788a978234SLiam Girdwood 
26795aebe7c7SPierre-Louis Bossart 	if (tplg->pass == le32_to_cpu(hdr->type))
26808a978234SLiam Girdwood 		dev_dbg(tplg->dev,
26818a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
26828a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
26838a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
26848a978234SLiam Girdwood 
26858a978234SLiam Girdwood 	return 1;
26868a978234SLiam Girdwood }
26878a978234SLiam Girdwood 
26888a978234SLiam Girdwood /* check header type and call appropriate handler */
26898a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
26908a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
26918a978234SLiam Girdwood {
26928a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
26938a978234SLiam Girdwood 
26948a978234SLiam Girdwood 	/* check for matching ID */
26955aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->index) != tplg->req_index &&
2696bb97142bSLiam Girdwood 		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
26978a978234SLiam Girdwood 		return 0;
26988a978234SLiam Girdwood 
26995aebe7c7SPierre-Louis Bossart 	tplg->index = le32_to_cpu(hdr->index);
27008a978234SLiam Girdwood 
27015aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(hdr->type)) {
27028a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
27038a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
27048a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
27058a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
27068a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
27078a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
27088a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
27098a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
27108a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
271164527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
27123fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
27139aa3f034SMengdong Lin 		return soc_tplg_dai_elems_load(tplg, hdr);
2714593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2715593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2716593d9e52SMengdong Lin 		/* physical link configurations */
2717593d9e52SMengdong Lin 		return soc_tplg_link_elems_load(tplg, hdr);
27188a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
27198a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
27208a978234SLiam Girdwood 	default:
27218a978234SLiam Girdwood 		/* bespoke vendor data object */
27228a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
27238a978234SLiam Girdwood 	}
27248a978234SLiam Girdwood 
27258a978234SLiam Girdwood 	return 0;
27268a978234SLiam Girdwood }
27278a978234SLiam Girdwood 
27288a978234SLiam Girdwood /* process the topology file headers */
27298a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
27308a978234SLiam Girdwood {
27318a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
27328a978234SLiam Girdwood 	int ret;
27338a978234SLiam Girdwood 
27348a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
27358a978234SLiam Girdwood 
27368a978234SLiam Girdwood 	/* process the header types from start to end */
27378a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
27388a978234SLiam Girdwood 
27398a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
27408a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
27418a978234SLiam Girdwood 
27428a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
27438a978234SLiam Girdwood 
27448a978234SLiam Girdwood 			/* make sure header is valid before loading */
27458a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
27468a978234SLiam Girdwood 			if (ret < 0)
27478a978234SLiam Girdwood 				return ret;
27488a978234SLiam Girdwood 			else if (ret == 0)
27498a978234SLiam Girdwood 				break;
27508a978234SLiam Girdwood 
27518a978234SLiam Girdwood 			/* load the header object */
27528a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
27538a978234SLiam Girdwood 			if (ret < 0)
27548a978234SLiam Girdwood 				return ret;
27558a978234SLiam Girdwood 
27568a978234SLiam Girdwood 			/* goto next header */
27575aebe7c7SPierre-Louis Bossart 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
27588a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
27598a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
27608a978234SLiam Girdwood 		}
27618a978234SLiam Girdwood 
27628a978234SLiam Girdwood 		/* next data type pass */
27638a978234SLiam Girdwood 		tplg->pass++;
27648a978234SLiam Girdwood 	}
27658a978234SLiam Girdwood 
27668a978234SLiam Girdwood 	/* signal DAPM we are complete */
27678a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
27688a978234SLiam Girdwood 	if (ret < 0)
27698a978234SLiam Girdwood 		dev_err(tplg->dev,
27708a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
27718a978234SLiam Girdwood 
27728a978234SLiam Girdwood 	return ret;
27738a978234SLiam Girdwood }
27748a978234SLiam Girdwood 
27758a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
27768a978234SLiam Girdwood {
27778a978234SLiam Girdwood 	int ret;
27788a978234SLiam Girdwood 
27798a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
27808a978234SLiam Girdwood 	if (ret == 0)
27818a978234SLiam Girdwood 		soc_tplg_complete(tplg);
27828a978234SLiam Girdwood 
27838a978234SLiam Girdwood 	return ret;
27848a978234SLiam Girdwood }
27858a978234SLiam Girdwood 
27868a978234SLiam Girdwood /* load audio component topology from "firmware" file */
27878a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
27888a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
27898a978234SLiam Girdwood {
27908a978234SLiam Girdwood 	struct soc_tplg tplg;
2791304017d3SBard liao 	int ret;
27928a978234SLiam Girdwood 
2793c42464a4SAmadeusz Sławiński 	/* component needs to exist to keep and reference data while parsing */
2794c42464a4SAmadeusz Sławiński 	if (!comp)
2795c42464a4SAmadeusz Sławiński 		return -EINVAL;
2796c42464a4SAmadeusz Sławiński 
27978a978234SLiam Girdwood 	/* setup parsing context */
27988a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
27998a978234SLiam Girdwood 	tplg.fw = fw;
28008a978234SLiam Girdwood 	tplg.dev = comp->dev;
28018a978234SLiam Girdwood 	tplg.comp = comp;
28028a978234SLiam Girdwood 	tplg.ops = ops;
28038a978234SLiam Girdwood 	tplg.req_index = id;
28048a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
28058a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
28061a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
28071a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
28088a978234SLiam Girdwood 
2809304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2810304017d3SBard liao 	/* free the created components if fail to load topology */
2811304017d3SBard liao 	if (ret)
2812304017d3SBard liao 		snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2813304017d3SBard liao 
2814304017d3SBard liao 	return ret;
28158a978234SLiam Girdwood }
28168a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
28178a978234SLiam Girdwood 
28188a978234SLiam Girdwood /* remove this dynamic widget */
28198a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
28208a978234SLiam Girdwood {
28218a978234SLiam Girdwood 	/* make sure we are a widget */
28228a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
28238a978234SLiam Girdwood 		return;
28248a978234SLiam Girdwood 
28258a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
28268a978234SLiam Girdwood }
28278a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
28288a978234SLiam Girdwood 
28298a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
28308a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
28318a978234SLiam Girdwood 	u32 index)
28328a978234SLiam Girdwood {
28338a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
28348a978234SLiam Girdwood 
283514596692SKuninori Morimoto 	for_each_card_widgets_safe(dapm->card, w, next_w) {
28368a978234SLiam Girdwood 
28378a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
28388a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
28398a978234SLiam Girdwood 			continue;
28408a978234SLiam Girdwood 
28418a978234SLiam Girdwood 		/* match ID */
28428a978234SLiam Girdwood 		if (w->dobj.index != index &&
28438a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
28448a978234SLiam Girdwood 			continue;
28458a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
28468a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2847b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
28488a978234SLiam Girdwood 	}
2849fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
28508a978234SLiam Girdwood }
28518a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
28528a978234SLiam Girdwood 
28538a978234SLiam Girdwood /* remove dynamic controls from the component driver */
28548a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
28558a978234SLiam Girdwood {
28568a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
28578a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
28588a978234SLiam Girdwood 
28598a978234SLiam Girdwood 	/* process the header types from end to start */
28608a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
28618a978234SLiam Girdwood 
28628a978234SLiam Girdwood 		/* remove mixer controls */
28638a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
28648a978234SLiam Girdwood 			list) {
28658a978234SLiam Girdwood 
28668a978234SLiam Girdwood 			/* match index */
28678a978234SLiam Girdwood 			if (dobj->index != index &&
2868feb12f0cSYan Wang 				index != SND_SOC_TPLG_INDEX_ALL)
28698a978234SLiam Girdwood 				continue;
28708a978234SLiam Girdwood 
28718a978234SLiam Girdwood 			switch (dobj->type) {
28728a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
28738a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
28748a978234SLiam Girdwood 				break;
28758a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
28768a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
28778a978234SLiam Girdwood 				break;
28788a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
28798a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
28808a978234SLiam Girdwood 				break;
28817df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
28827df04ea7SRanjani Sridharan 				remove_route(comp, dobj, pass);
28837df04ea7SRanjani Sridharan 				break;
28848a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
28858a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
28868a978234SLiam Girdwood 				break;
28878a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
288864527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
28898a978234SLiam Girdwood 				break;
2890acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2891acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2892acfc7d46SMengdong Lin 				break;
2893adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2894adfebb51SBard liao 				/*
2895adfebb51SBard liao 				 * call link_unload ops if extra
2896adfebb51SBard liao 				 * deinitialization is needed.
2897adfebb51SBard liao 				 */
2898adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2899adfebb51SBard liao 				break;
29008a978234SLiam Girdwood 			default:
29018a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
29028a978234SLiam Girdwood 					dobj->type);
29038a978234SLiam Girdwood 				break;
29048a978234SLiam Girdwood 			}
29058a978234SLiam Girdwood 		}
29068a978234SLiam Girdwood 		pass--;
29078a978234SLiam Girdwood 	}
29088a978234SLiam Girdwood 
29098a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
29108a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
29118a978234SLiam Girdwood }
29128a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2913