xref: /openbmc/linux/sound/soc/soc-topology.c (revision 482db55a)
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 */
897*482db55aSAmadeusz Sławiński 		err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
898*482db55aSAmadeusz Sławiński 		if (err < 0) {
899*482db55aSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
900*482db55aSAmadeusz Sławiński 				mc->hdr.name);
901*482db55aSAmadeusz Sławiński 			kfree(sm);
902*482db55aSAmadeusz Sławiński 			continue;
903*482db55aSAmadeusz 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;
11278a978234SLiam Girdwood 	int i;
11288a978234SLiam Girdwood 
11298a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
11305aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
11315aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
11328a978234SLiam Girdwood 		return 0;
11338a978234SLiam Girdwood 	}
11348a978234SLiam Girdwood 
11358a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
11368a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
11378a978234SLiam Girdwood 
11385aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
11398a978234SLiam Girdwood 
11408a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
11418a978234SLiam Girdwood 
11425aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
114306eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
114406eb49f7SMengdong Lin 			return -EINVAL;
114506eb49f7SMengdong Lin 		}
114606eb49f7SMengdong Lin 
11475aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
11488a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
11498a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
11508a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
11518a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
11528a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
11538a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
11548a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
11555aebe7c7SPierre-Louis Bossart 			soc_tplg_dmixer_create(tplg, 1,
11565aebe7c7SPierre-Louis Bossart 					       le32_to_cpu(hdr->payload_size));
11578a978234SLiam Girdwood 			break;
11588a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
11598a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
11608a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
11618a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
11628a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
11635aebe7c7SPierre-Louis Bossart 			soc_tplg_denum_create(tplg, 1,
11645aebe7c7SPierre-Louis Bossart 					      le32_to_cpu(hdr->payload_size));
11658a978234SLiam Girdwood 			break;
11668a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
11675aebe7c7SPierre-Louis Bossart 			soc_tplg_dbytes_create(tplg, 1,
11685aebe7c7SPierre-Louis Bossart 					       le32_to_cpu(hdr->payload_size));
11698a978234SLiam Girdwood 			break;
11708a978234SLiam Girdwood 		default:
11718a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
11728a978234SLiam Girdwood 			return -EINVAL;
11738a978234SLiam Girdwood 		}
11748a978234SLiam Girdwood 	}
11758a978234SLiam Girdwood 
11768a978234SLiam Girdwood 	return 0;
11778a978234SLiam Girdwood }
11788a978234SLiam Girdwood 
1179503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1180503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1181503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1182503e79b7SLiam Girdwood {
1183c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dapm_route_load)
1184503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1185503e79b7SLiam Girdwood 			route);
1186503e79b7SLiam Girdwood 
1187503e79b7SLiam Girdwood 	return 0;
1188503e79b7SLiam Girdwood }
1189503e79b7SLiam Girdwood 
11908a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
11918a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11928a978234SLiam Girdwood {
11938a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
11948a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
11957df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route **routes;
11965aebe7c7SPierre-Louis Bossart 	int count, i, j;
11977df04ea7SRanjani Sridharan 	int ret = 0;
11988a978234SLiam Girdwood 
11995aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
12005aebe7c7SPierre-Louis Bossart 
12018a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
12025aebe7c7SPierre-Louis Bossart 		tplg->pos +=
12035aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->size) +
12045aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
12055aebe7c7SPierre-Louis Bossart 
12068a978234SLiam Girdwood 		return 0;
12078a978234SLiam Girdwood 	}
12088a978234SLiam Girdwood 
12098a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
12108a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
12115aebe7c7SPierre-Louis Bossart 		count, le32_to_cpu(hdr->payload_size), "graph")) {
12128a978234SLiam Girdwood 
12138a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
12148a978234SLiam Girdwood 			count);
12158a978234SLiam Girdwood 		return -EINVAL;
12168a978234SLiam Girdwood 	}
12178a978234SLiam Girdwood 
1218b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1219b75a6511SLiam Girdwood 		hdr->index);
12208a978234SLiam Girdwood 
12217df04ea7SRanjani Sridharan 	/* allocate memory for pointer to array of dapm routes */
12227df04ea7SRanjani Sridharan 	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
12237df04ea7SRanjani Sridharan 			 GFP_KERNEL);
12247df04ea7SRanjani Sridharan 	if (!routes)
12257df04ea7SRanjani Sridharan 		return -ENOMEM;
12267df04ea7SRanjani Sridharan 
12277df04ea7SRanjani Sridharan 	/*
12287df04ea7SRanjani Sridharan 	 * allocate memory for each dapm route in the array.
12297df04ea7SRanjani Sridharan 	 * This needs to be done individually so that
12307df04ea7SRanjani Sridharan 	 * each route can be freed when it is removed in remove_route().
12317df04ea7SRanjani Sridharan 	 */
12327df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
12337df04ea7SRanjani Sridharan 		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
12347df04ea7SRanjani Sridharan 		if (!routes[i]) {
12357df04ea7SRanjani Sridharan 			/* free previously allocated memory */
12367df04ea7SRanjani Sridharan 			for (j = 0; j < i; j++)
12377df04ea7SRanjani Sridharan 				kfree(routes[j]);
12387df04ea7SRanjani Sridharan 
12397df04ea7SRanjani Sridharan 			kfree(routes);
12407df04ea7SRanjani Sridharan 			return -ENOMEM;
12417df04ea7SRanjani Sridharan 		}
12427df04ea7SRanjani Sridharan 	}
12437df04ea7SRanjani Sridharan 
12448a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12458a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
12468a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
12478a978234SLiam Girdwood 
12488a978234SLiam Girdwood 		/* validate routes */
12498a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12507df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12517df04ea7SRanjani Sridharan 			ret = -EINVAL;
12527df04ea7SRanjani Sridharan 			break;
12537df04ea7SRanjani Sridharan 		}
12548a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12557df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12567df04ea7SRanjani Sridharan 			ret = -EINVAL;
12577df04ea7SRanjani Sridharan 			break;
12587df04ea7SRanjani Sridharan 		}
12598a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12607df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12617df04ea7SRanjani Sridharan 			ret = -EINVAL;
12627df04ea7SRanjani Sridharan 			break;
12638a978234SLiam Girdwood 		}
12648a978234SLiam Girdwood 
12657df04ea7SRanjani Sridharan 		routes[i]->source = elem->source;
12667df04ea7SRanjani Sridharan 		routes[i]->sink = elem->sink;
12677df04ea7SRanjani Sridharan 
12687df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
12697df04ea7SRanjani Sridharan 		routes[i]->connected = NULL;
12707df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
12717df04ea7SRanjani Sridharan 			routes[i]->control = NULL;
12727df04ea7SRanjani Sridharan 		else
12737df04ea7SRanjani Sridharan 			routes[i]->control = elem->control;
12747df04ea7SRanjani Sridharan 
12757df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
12767df04ea7SRanjani Sridharan 		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
12777df04ea7SRanjani Sridharan 		routes[i]->dobj.ops = tplg->ops;
12787df04ea7SRanjani Sridharan 		routes[i]->dobj.index = tplg->index;
12797df04ea7SRanjani Sridharan 		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
12807df04ea7SRanjani Sridharan 
12817df04ea7SRanjani Sridharan 		soc_tplg_add_route(tplg, routes[i]);
12827df04ea7SRanjani Sridharan 
12837df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
12847df04ea7SRanjani Sridharan 		snd_soc_dapm_add_routes(dapm, routes[i], 1);
12857df04ea7SRanjani Sridharan 	}
12867df04ea7SRanjani Sridharan 
12877df04ea7SRanjani Sridharan 	/* free memory allocated for all dapm routes in case of error */
12887df04ea7SRanjani Sridharan 	if (ret < 0)
12897df04ea7SRanjani Sridharan 		for (i = 0; i < count ; i++)
12907df04ea7SRanjani Sridharan 			kfree(routes[i]);
12917df04ea7SRanjani Sridharan 
12927df04ea7SRanjani Sridharan 	/*
12937df04ea7SRanjani Sridharan 	 * free pointer to array of dapm routes as this is no longer needed.
12947df04ea7SRanjani Sridharan 	 * The memory allocated for each dapm route will be freed
12957df04ea7SRanjani Sridharan 	 * when it is removed in remove_route().
12967df04ea7SRanjani Sridharan 	 */
12977df04ea7SRanjani Sridharan 	kfree(routes);
12987df04ea7SRanjani Sridharan 
12997df04ea7SRanjani Sridharan 	return ret;
13008a978234SLiam Girdwood }
13018a978234SLiam Girdwood 
13028a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
13038a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
13048a978234SLiam Girdwood {
13058a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13068a978234SLiam Girdwood 	struct soc_mixer_control *sm;
13078a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
13088a978234SLiam Girdwood 	int i, err;
13098a978234SLiam Girdwood 
13104ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13118a978234SLiam Girdwood 	if (kc == NULL)
13128a978234SLiam Girdwood 		return NULL;
13138a978234SLiam Girdwood 
13148a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
13158a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
13168a978234SLiam Girdwood 
13178a978234SLiam Girdwood 		/* validate kcontrol */
13188a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13198a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13209f90af3aSAmadeusz Sławiński 			goto err_sm;
13219f90af3aSAmadeusz Sławiński 
13229f90af3aSAmadeusz Sławiński 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
13239f90af3aSAmadeusz Sławiński 		if (sm == NULL)
13249f90af3aSAmadeusz Sławiński 			goto err_sm;
13258a978234SLiam Girdwood 
132602b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
13275aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(mc->priv.size));
132802b64245SLiam Girdwood 
13298a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
13308a978234SLiam Girdwood 			mc->hdr.name, i);
13318a978234SLiam Girdwood 
13321ad741d0SColin Ian King 		kc[i].private_value = (long)sm;
1333267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1334267e2c6fSLiam Girdwood 		if (kc[i].name == NULL)
13359f90af3aSAmadeusz Sławiński 			goto err_sm;
13368a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
133772bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(mc->hdr.access);
13388a978234SLiam Girdwood 
13398a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13408a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
13418a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13428a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
13438a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13448a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
13458a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13468a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
13478a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13488a978234SLiam Girdwood 
134972bbeda0SPierre-Louis Bossart 		sm->max = le32_to_cpu(mc->max);
135072bbeda0SPierre-Louis Bossart 		sm->min = le32_to_cpu(mc->min);
135172bbeda0SPierre-Louis Bossart 		sm->invert = le32_to_cpu(mc->invert);
135272bbeda0SPierre-Louis Bossart 		sm->platform_max = le32_to_cpu(mc->platform_max);
13538a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
13548a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
13558a978234SLiam Girdwood 
13568a978234SLiam Girdwood 		/* map io handlers */
13572b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
13588a978234SLiam Girdwood 		if (err) {
13598a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
13609f90af3aSAmadeusz Sławiński 			goto err_sm;
13618a978234SLiam Girdwood 		}
13628a978234SLiam Girdwood 
13633789debfSBard liao 		/* create any TLV data */
1364*482db55aSAmadeusz Sławiński 		err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1365*482db55aSAmadeusz Sławiński 		if (err < 0) {
1366*482db55aSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1367*482db55aSAmadeusz Sławiński 				mc->hdr.name);
1368*482db55aSAmadeusz Sławiński 			kfree(sm);
1369*482db55aSAmadeusz Sławiński 			continue;
1370*482db55aSAmadeusz Sławiński 		}
13713789debfSBard liao 
13728a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13738a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13748a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
13758a978234SLiam Girdwood 		if (err < 0) {
13768a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13778a978234SLiam Girdwood 				mc->hdr.name);
13783789debfSBard liao 			soc_tplg_free_tlv(tplg, &kc[i]);
13799f90af3aSAmadeusz Sławiński 			goto err_sm;
13808a978234SLiam Girdwood 		}
13818a978234SLiam Girdwood 	}
13828a978234SLiam Girdwood 	return kc;
13838a978234SLiam Girdwood 
13849f90af3aSAmadeusz Sławiński err_sm:
13859f90af3aSAmadeusz Sławiński 	for (; i >= 0; i--) {
13869f90af3aSAmadeusz Sławiński 		sm = (struct soc_mixer_control *)kc[i].private_value;
13878a978234SLiam Girdwood 		kfree(sm);
1388267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1389267e2c6fSLiam Girdwood 	}
13908a978234SLiam Girdwood 	kfree(kc);
13919f90af3aSAmadeusz Sławiński 
13928a978234SLiam Girdwood 	return NULL;
13938a978234SLiam Girdwood }
13948a978234SLiam Girdwood 
13958a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
13961a7dd6e2SMengdong Lin 	struct soc_tplg *tplg, int num_kcontrols)
13978a978234SLiam Girdwood {
13988a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13998a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
14008a978234SLiam Girdwood 	struct soc_enum *se;
14013cde818cSAmadeusz Sławiński 	int i, err;
14028a978234SLiam Girdwood 
14031a7dd6e2SMengdong Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
14041a7dd6e2SMengdong Lin 	if (kc == NULL)
14051a7dd6e2SMengdong Lin 		return NULL;
14061a7dd6e2SMengdong Lin 
14071a7dd6e2SMengdong Lin 	for (i = 0; i < num_kcontrols; i++) {
14088a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
14098a978234SLiam Girdwood 		/* validate kcontrol */
14108a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
14118a978234SLiam Girdwood 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
14129f90af3aSAmadeusz Sławiński 			goto err_se;
14138a978234SLiam Girdwood 
14148a978234SLiam Girdwood 		se = kzalloc(sizeof(*se), GFP_KERNEL);
14158a978234SLiam Girdwood 		if (se == NULL)
14169f90af3aSAmadeusz Sławiński 			goto err_se;
14178a978234SLiam Girdwood 
141802b64245SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
141972bbeda0SPierre-Louis Bossart 			      le32_to_cpu(ec->priv.size));
142002b64245SLiam Girdwood 
14218a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
14228a978234SLiam Girdwood 			ec->hdr.name);
14238a978234SLiam Girdwood 
14241ad741d0SColin Ian King 		kc[i].private_value = (long)se;
1425267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
14269f90af3aSAmadeusz Sławiński 		if (kc[i].name == NULL)
1427267e2c6fSLiam Girdwood 			goto err_se;
14281a7dd6e2SMengdong Lin 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
142972bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(ec->hdr.access);
14308a978234SLiam Girdwood 
14318a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
14328a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
14331a7dd6e2SMengdong Lin 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
14341a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FL);
14351a7dd6e2SMengdong Lin 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
14361a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FR);
14378a978234SLiam Girdwood 
143872bbeda0SPierre-Louis Bossart 		se->items = le32_to_cpu(ec->items);
143972bbeda0SPierre-Louis Bossart 		se->mask = le32_to_cpu(ec->mask);
14408a978234SLiam Girdwood 		se->dobj.index = tplg->index;
14418a978234SLiam Girdwood 
14425aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(ec->hdr.ops.info)) {
14438a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
14448a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14458a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
14468a978234SLiam Girdwood 			if (err < 0) {
14478a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
14488a978234SLiam Girdwood 					ec->hdr.name);
14498a978234SLiam Girdwood 				goto err_se;
14508a978234SLiam Girdwood 			}
14519c6c4d96STakashi Iwai 			/* fall through */
14528a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
14538a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14548a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14558a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
14568a978234SLiam Girdwood 			if (err < 0) {
14578a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
14588a978234SLiam Girdwood 					ec->hdr.name);
14598a978234SLiam Girdwood 				goto err_se;
14608a978234SLiam Girdwood 			}
14618a978234SLiam Girdwood 			break;
14628a978234SLiam Girdwood 		default:
14638a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
14648a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
14658a978234SLiam Girdwood 			goto err_se;
14668a978234SLiam Girdwood 		}
14678a978234SLiam Girdwood 
14688a978234SLiam Girdwood 		/* map io handlers */
14691a7dd6e2SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
14708a978234SLiam Girdwood 		if (err) {
14718a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
14728a978234SLiam Girdwood 			goto err_se;
14738a978234SLiam Girdwood 		}
14748a978234SLiam Girdwood 
14758a978234SLiam Girdwood 		/* pass control to driver for optional further init */
14761a7dd6e2SMengdong Lin 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
14778a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)ec);
14788a978234SLiam Girdwood 		if (err < 0) {
14798a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
14808a978234SLiam Girdwood 				ec->hdr.name);
14818a978234SLiam Girdwood 			goto err_se;
14828a978234SLiam Girdwood 		}
14831a7dd6e2SMengdong Lin 	}
14841a7dd6e2SMengdong Lin 
14858a978234SLiam Girdwood 	return kc;
14868a978234SLiam Girdwood 
14878a978234SLiam Girdwood err_se:
14881a7dd6e2SMengdong Lin 	for (; i >= 0; i--) {
14898a978234SLiam Girdwood 		/* free values and texts */
14901a7dd6e2SMengdong Lin 		se = (struct soc_enum *)kc[i].private_value;
14916d5574edSChristophe JAILLET 
14929f90af3aSAmadeusz Sławiński 		if (se) {
14933cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_values(se);
14943cde818cSAmadeusz Sławiński 			soc_tplg_denum_remove_texts(se);
14959f90af3aSAmadeusz Sławiński 		}
14968a978234SLiam Girdwood 
14978a978234SLiam Girdwood 		kfree(se);
1498267e2c6fSLiam Girdwood 		kfree(kc[i].name);
14991a7dd6e2SMengdong Lin 	}
15008a978234SLiam Girdwood 	kfree(kc);
15018a978234SLiam Girdwood 
15028a978234SLiam Girdwood 	return NULL;
15038a978234SLiam Girdwood }
15048a978234SLiam Girdwood 
15058a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
15069f90af3aSAmadeusz Sławiński 	struct soc_tplg *tplg, int num_kcontrols)
15078a978234SLiam Girdwood {
15088a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
15098a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
15108a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
15118a978234SLiam Girdwood 	int i, err;
15128a978234SLiam Girdwood 
15139f90af3aSAmadeusz Sławiński 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
15148a978234SLiam Girdwood 	if (!kc)
15158a978234SLiam Girdwood 		return NULL;
15168a978234SLiam Girdwood 
15179f90af3aSAmadeusz Sławiński 	for (i = 0; i < num_kcontrols; i++) {
15188a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
15198a978234SLiam Girdwood 
15208a978234SLiam Girdwood 		/* validate kcontrol */
15218a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15228a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15239f90af3aSAmadeusz Sławiński 			goto err_sbe;
15248a978234SLiam Girdwood 
15258a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
15268a978234SLiam Girdwood 		if (sbe == NULL)
15279f90af3aSAmadeusz Sławiński 			goto err_sbe;
15288a978234SLiam Girdwood 
15298a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
15305aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(be->priv.size));
15318a978234SLiam Girdwood 
15328a978234SLiam Girdwood 		dev_dbg(tplg->dev,
15338a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
15348a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
15358a978234SLiam Girdwood 
15361ad741d0SColin Ian King 		kc[i].private_value = (long)sbe;
1537267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
15389f90af3aSAmadeusz Sławiński 		if (kc[i].name == NULL)
15399f90af3aSAmadeusz Sławiński 			goto err_sbe;
15408a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
154172bbeda0SPierre-Louis Bossart 		kc[i].access = le32_to_cpu(be->hdr.access);
15428a978234SLiam Girdwood 
154372bbeda0SPierre-Louis Bossart 		sbe->max = le32_to_cpu(be->max);
15448a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
15458a978234SLiam Girdwood 
15468a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
15472b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
15488a978234SLiam Girdwood 		if (err) {
15498a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
15509f90af3aSAmadeusz Sławiński 			goto err_sbe;
15518a978234SLiam Girdwood 		}
15528a978234SLiam Girdwood 
15538a978234SLiam Girdwood 		/* pass control to driver for optional further init */
15548a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
15558a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
15568a978234SLiam Girdwood 		if (err < 0) {
15578a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
15588a978234SLiam Girdwood 				be->hdr.name);
15599f90af3aSAmadeusz Sławiński 			goto err_sbe;
15608a978234SLiam Girdwood 		}
15618a978234SLiam Girdwood 	}
15628a978234SLiam Girdwood 
15638a978234SLiam Girdwood 	return kc;
15648a978234SLiam Girdwood 
15659f90af3aSAmadeusz Sławiński err_sbe:
15669f90af3aSAmadeusz Sławiński 	for (; i >= 0; i--) {
15679f90af3aSAmadeusz Sławiński 		sbe = (struct soc_bytes_ext *)kc[i].private_value;
15689f90af3aSAmadeusz Sławiński 		kfree(sbe);
1569267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1570267e2c6fSLiam Girdwood 	}
15718a978234SLiam Girdwood 	kfree(kc);
15729f90af3aSAmadeusz Sławiński 
15738a978234SLiam Girdwood 	return NULL;
15748a978234SLiam Girdwood }
15758a978234SLiam Girdwood 
15768a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
15778a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
15788a978234SLiam Girdwood {
15798a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
15808a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
15818a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
15828a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1583eea3dd4fSMengdong Lin 	unsigned int kcontrol_type;
15848a978234SLiam Girdwood 	int ret = 0;
15858a978234SLiam Girdwood 
15868a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15878a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15888a978234SLiam Girdwood 		return -EINVAL;
15898a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15908a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15918a978234SLiam Girdwood 		return -EINVAL;
15928a978234SLiam Girdwood 
15938a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
15948a978234SLiam Girdwood 		w->name, w->id);
15958a978234SLiam Girdwood 
15968a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
15978a978234SLiam Girdwood 
15988a978234SLiam Girdwood 	/* map user to kernel widget ID */
15995aebe7c7SPierre-Louis Bossart 	template.id = get_widget_id(le32_to_cpu(w->id));
1600752c938aSDan Carpenter 	if ((int)template.id < 0)
16018a978234SLiam Girdwood 		return template.id;
16028a978234SLiam Girdwood 
1603c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
16048a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
16058a978234SLiam Girdwood 	if (!template.name)
16068a978234SLiam Girdwood 		return -ENOMEM;
16078a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
16088a978234SLiam Girdwood 	if (!template.sname) {
16098a978234SLiam Girdwood 		ret = -ENOMEM;
16108a978234SLiam Girdwood 		goto err;
16118a978234SLiam Girdwood 	}
16125aebe7c7SPierre-Louis Bossart 	template.reg = le32_to_cpu(w->reg);
16135aebe7c7SPierre-Louis Bossart 	template.shift = le32_to_cpu(w->shift);
16145aebe7c7SPierre-Louis Bossart 	template.mask = le32_to_cpu(w->mask);
16155aebe7c7SPierre-Louis Bossart 	template.subseq = le32_to_cpu(w->subseq);
16168a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
16178a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
16185aebe7c7SPierre-Louis Bossart 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
16195aebe7c7SPierre-Louis Bossart 	template.event_flags = le16_to_cpu(w->event_flags);
16208a978234SLiam Girdwood 	template.dobj.index = tplg->index;
16218a978234SLiam Girdwood 
16228a978234SLiam Girdwood 	tplg->pos +=
16235aebe7c7SPierre-Louis Bossart 		(sizeof(struct snd_soc_tplg_dapm_widget) +
16245aebe7c7SPierre-Louis Bossart 		 le32_to_cpu(w->priv.size));
16255aebe7c7SPierre-Louis Bossart 
16268a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
1627dd5abb74SArnd Bergmann 		kcontrol_type = 0;
16288a978234SLiam Girdwood 		template.num_kcontrols = 0;
16298a978234SLiam Girdwood 		goto widget;
16308a978234SLiam Girdwood 	}
16318a978234SLiam Girdwood 
16328a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
16338a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
16348a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
16358a978234SLiam Girdwood 
16365aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(control_hdr->ops.info)) {
16378a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
16388a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
16398a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
16408a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
16418a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
16428a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1643eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
16445aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16458a978234SLiam Girdwood 		template.kcontrol_news =
16468a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
16478a978234SLiam Girdwood 			template.num_kcontrols);
16488a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16498a978234SLiam Girdwood 			ret = -ENOMEM;
16508a978234SLiam Girdwood 			goto hdr_err;
16518a978234SLiam Girdwood 		}
16528a978234SLiam Girdwood 		break;
16538a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
16548a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
16558a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
16568a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
16578a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1658eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
16595aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16608a978234SLiam Girdwood 		template.kcontrol_news =
16611a7dd6e2SMengdong Lin 			soc_tplg_dapm_widget_denum_create(tplg,
16621a7dd6e2SMengdong Lin 			template.num_kcontrols);
16638a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16648a978234SLiam Girdwood 			ret = -ENOMEM;
16658a978234SLiam Girdwood 			goto hdr_err;
16668a978234SLiam Girdwood 		}
16678a978234SLiam Girdwood 		break;
16688a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
1669eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
16705aebe7c7SPierre-Louis Bossart 		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
16718a978234SLiam Girdwood 		template.kcontrol_news =
16728a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
16738a978234SLiam Girdwood 				template.num_kcontrols);
16748a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16758a978234SLiam Girdwood 			ret = -ENOMEM;
16768a978234SLiam Girdwood 			goto hdr_err;
16778a978234SLiam Girdwood 		}
16788a978234SLiam Girdwood 		break;
16798a978234SLiam Girdwood 	default:
16808a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
16818a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
16825aebe7c7SPierre-Louis Bossart 			le32_to_cpu(control_hdr->ops.info));
16838a978234SLiam Girdwood 		ret = -EINVAL;
16848a978234SLiam Girdwood 		goto hdr_err;
16858a978234SLiam Girdwood 	}
16868a978234SLiam Girdwood 
16878a978234SLiam Girdwood widget:
16888a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
16898a978234SLiam Girdwood 	if (ret < 0)
16908a978234SLiam Girdwood 		goto hdr_err;
16918a978234SLiam Girdwood 
16928a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
16938a978234SLiam Girdwood 	 * data during sound card init. */
16948a978234SLiam Girdwood 	if (card->instantiated)
16958a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
16968a978234SLiam Girdwood 	else
16978a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
169837e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
169937e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
17008a978234SLiam Girdwood 		goto hdr_err;
17018a978234SLiam Girdwood 	}
17028a978234SLiam Girdwood 
17038a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1704eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
17058a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
17068a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
17078a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1708ebd259d3SLiam Girdwood 
1709ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1710ebd259d3SLiam Girdwood 	if (ret < 0)
1711ebd259d3SLiam Girdwood 		goto ready_err;
1712ebd259d3SLiam Girdwood 
17137620fe91SBard liao 	kfree(template.sname);
17147620fe91SBard liao 	kfree(template.name);
17157620fe91SBard liao 
17168a978234SLiam Girdwood 	return 0;
17178a978234SLiam Girdwood 
1718ebd259d3SLiam Girdwood ready_err:
1719ebd259d3SLiam Girdwood 	snd_soc_tplg_widget_remove(widget);
1720ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
17218a978234SLiam Girdwood hdr_err:
17228a978234SLiam Girdwood 	kfree(template.sname);
17238a978234SLiam Girdwood err:
17248a978234SLiam Girdwood 	kfree(template.name);
17258a978234SLiam Girdwood 	return ret;
17268a978234SLiam Girdwood }
17278a978234SLiam Girdwood 
17288a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
17298a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
17308a978234SLiam Girdwood {
17318a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
17325aebe7c7SPierre-Louis Bossart 	int ret, count, i;
17335aebe7c7SPierre-Louis Bossart 
17345aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
17358a978234SLiam Girdwood 
17368a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
17378a978234SLiam Girdwood 		return 0;
17388a978234SLiam Girdwood 
17398a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
17408a978234SLiam Girdwood 
17418a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
17428a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
17435aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
174406eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
174506eb49f7SMengdong Lin 			return -EINVAL;
174606eb49f7SMengdong Lin 		}
174706eb49f7SMengdong Lin 
17488a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
17497de76b62SMengdong Lin 		if (ret < 0) {
17508a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
17518a978234SLiam Girdwood 				widget->name);
17527de76b62SMengdong Lin 			return ret;
17537de76b62SMengdong Lin 		}
17548a978234SLiam Girdwood 	}
17558a978234SLiam Girdwood 
17568a978234SLiam Girdwood 	return 0;
17578a978234SLiam Girdwood }
17588a978234SLiam Girdwood 
17598a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
17608a978234SLiam Girdwood {
17618a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
17628a978234SLiam Girdwood 	int ret;
17638a978234SLiam Girdwood 
17648a978234SLiam Girdwood 	/* Card might not have been registered at this point.
17658a978234SLiam Girdwood 	 * If so, just return success.
17668a978234SLiam Girdwood 	*/
17678a978234SLiam Girdwood 	if (!card || !card->instantiated) {
17688a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1769cc9d4714SLiam Girdwood 			" widget card binding deferred\n");
17708a978234SLiam Girdwood 		return 0;
17718a978234SLiam Girdwood 	}
17728a978234SLiam Girdwood 
17738a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
17748a978234SLiam Girdwood 	if (ret < 0)
17758a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
17768a978234SLiam Girdwood 			ret);
17778a978234SLiam Girdwood 
17788a978234SLiam Girdwood 	return 0;
17798a978234SLiam Girdwood }
17808a978234SLiam Girdwood 
1781abc3caacSAmadeusz Sławiński static int set_stream_info(struct snd_soc_pcm_stream *stream,
1782b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1783b6b6e4d6SMengdong Lin {
1784b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1785abc3caacSAmadeusz Sławiński 	if (!stream->stream_name)
1786abc3caacSAmadeusz Sławiński 		return -ENOMEM;
1787abc3caacSAmadeusz Sławiński 
17885aebe7c7SPierre-Louis Bossart 	stream->channels_min = le32_to_cpu(caps->channels_min);
17895aebe7c7SPierre-Louis Bossart 	stream->channels_max = le32_to_cpu(caps->channels_max);
17905aebe7c7SPierre-Louis Bossart 	stream->rates = le32_to_cpu(caps->rates);
17915aebe7c7SPierre-Louis Bossart 	stream->rate_min = le32_to_cpu(caps->rate_min);
17925aebe7c7SPierre-Louis Bossart 	stream->rate_max = le32_to_cpu(caps->rate_max);
17935aebe7c7SPierre-Louis Bossart 	stream->formats = le64_to_cpu(caps->formats);
17945aebe7c7SPierre-Louis Bossart 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1795abc3caacSAmadeusz Sławiński 
1796abc3caacSAmadeusz Sławiński 	return 0;
1797b6b6e4d6SMengdong Lin }
1798b6b6e4d6SMengdong Lin 
17990038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
18000038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
18010038be9aSMengdong Lin {
18020038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
18030038be9aSMengdong Lin 		dai_drv->symmetric_rates =
18040038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
18050038be9aSMengdong Lin 
18060038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
18070038be9aSMengdong Lin 		dai_drv->symmetric_channels =
18080038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
18090038be9aSMengdong Lin 			1 : 0;
18100038be9aSMengdong Lin 
18110038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
18120038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
18130038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
18140038be9aSMengdong Lin 			1 : 0;
18150038be9aSMengdong Lin }
18160038be9aSMengdong Lin 
181764527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
181864527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
181964527e8aSMengdong Lin {
182064527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
182164527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
182264527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
1823e443c205SKuninori Morimoto 	struct snd_soc_dai *dai;
1824e443c205SKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1825e443c205SKuninori Morimoto 		snd_soc_component_get_dapm(tplg->comp);
182664527e8aSMengdong Lin 	int ret;
182764527e8aSMengdong Lin 
182864527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
182964527e8aSMengdong Lin 	if (dai_drv == NULL)
183064527e8aSMengdong Lin 		return -ENOMEM;
183164527e8aSMengdong Lin 
1832abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
18338f27c4abSMengdong Lin 		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1834abc3caacSAmadeusz Sławiński 		if (!dai_drv->name) {
1835abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1836abc3caacSAmadeusz Sławiński 			goto err;
1837abc3caacSAmadeusz Sławiński 		}
1838abc3caacSAmadeusz Sławiński 	}
18395aebe7c7SPierre-Louis Bossart 	dai_drv->id = le32_to_cpu(pcm->dai_id);
184064527e8aSMengdong Lin 
184164527e8aSMengdong Lin 	if (pcm->playback) {
184264527e8aSMengdong Lin 		stream = &dai_drv->playback;
184364527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1844abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
1845abc3caacSAmadeusz Sławiński 		if (ret < 0)
1846abc3caacSAmadeusz Sławiński 			goto err;
184764527e8aSMengdong Lin 	}
184864527e8aSMengdong Lin 
184964527e8aSMengdong Lin 	if (pcm->capture) {
185064527e8aSMengdong Lin 		stream = &dai_drv->capture;
185164527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1852abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
1853abc3caacSAmadeusz Sławiński 		if (ret < 0)
1854abc3caacSAmadeusz Sławiński 			goto err;
185564527e8aSMengdong Lin 	}
185664527e8aSMengdong Lin 
18575db6aab6SLiam Girdwood 	if (pcm->compress)
18585db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
18595db6aab6SLiam Girdwood 
186064527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1861c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
186264527e8aSMengdong Lin 	if (ret < 0) {
186364527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1864abc3caacSAmadeusz Sławiński 		goto err;
186564527e8aSMengdong Lin 	}
186664527e8aSMengdong Lin 
186764527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
186864527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
186964527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
187064527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
187164527e8aSMengdong Lin 
187264527e8aSMengdong Lin 	/* register the DAI to the component */
1873e443c205SKuninori Morimoto 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1874e443c205SKuninori Morimoto 	if (!dai)
1875e443c205SKuninori Morimoto 		return -ENOMEM;
1876e443c205SKuninori Morimoto 
1877e443c205SKuninori Morimoto 	/* Create the DAI widgets here */
1878e443c205SKuninori Morimoto 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1879e443c205SKuninori Morimoto 	if (ret != 0) {
1880e443c205SKuninori Morimoto 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1881e443c205SKuninori Morimoto 		snd_soc_unregister_dai(dai);
1882e443c205SKuninori Morimoto 		return ret;
1883e443c205SKuninori Morimoto 	}
1884e443c205SKuninori Morimoto 
1885abc3caacSAmadeusz Sławiński 	return 0;
1886abc3caacSAmadeusz Sławiński 
1887abc3caacSAmadeusz Sławiński err:
1888abc3caacSAmadeusz Sławiński 	kfree(dai_drv->playback.stream_name);
1889abc3caacSAmadeusz Sławiński 	kfree(dai_drv->capture.stream_name);
1890abc3caacSAmadeusz Sławiński 	kfree(dai_drv->name);
1891abc3caacSAmadeusz Sławiński 	kfree(dai_drv);
1892abc3caacSAmadeusz Sławiński 
1893e443c205SKuninori Morimoto 	return ret;
189464527e8aSMengdong Lin }
189564527e8aSMengdong Lin 
1896717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1897717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1898717a8e72SMengdong Lin {
1899717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1900717a8e72SMengdong Lin 		link->symmetric_rates =
1901717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1902717a8e72SMengdong Lin 
1903717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1904717a8e72SMengdong Lin 		link->symmetric_channels =
1905717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1906717a8e72SMengdong Lin 			1 : 0;
1907717a8e72SMengdong Lin 
1908717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1909717a8e72SMengdong Lin 		link->symmetric_samplebits =
1910717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1911717a8e72SMengdong Lin 			1 : 0;
19126ff67ccaSMengdong Lin 
19136ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
19146ff67ccaSMengdong Lin 		link->ignore_suspend =
19156ff67ccaSMengdong Lin 		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
19166ff67ccaSMengdong Lin 		1 : 0;
1917717a8e72SMengdong Lin }
1918717a8e72SMengdong Lin 
191967d1c21eSGuneshwor Singh /* create the FE DAI link */
1920ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1921acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1922acfc7d46SMengdong Lin {
1923acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
192423b946ceSKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
1925acfc7d46SMengdong Lin 	int ret;
1926acfc7d46SMengdong Lin 
19273e6de894SPierre-Louis Bossart 	/* link + cpu + codec + platform */
19283e6de894SPierre-Louis Bossart 	link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1929acfc7d46SMengdong Lin 	if (link == NULL)
1930acfc7d46SMengdong Lin 		return -ENOMEM;
1931acfc7d46SMengdong Lin 
193223b946ceSKuninori Morimoto 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
193323b946ceSKuninori Morimoto 
193423b946ceSKuninori Morimoto 	link->cpus	= &dlc[0];
193523b946ceSKuninori Morimoto 	link->codecs	= &dlc[1];
19363e6de894SPierre-Louis Bossart 	link->platforms	= &dlc[2];
193723b946ceSKuninori Morimoto 
193823b946ceSKuninori Morimoto 	link->num_cpus	 = 1;
193923b946ceSKuninori Morimoto 	link->num_codecs = 1;
19403e6de894SPierre-Louis Bossart 	link->num_platforms = 1;
194123b946ceSKuninori Morimoto 
19428ce1cbd6SJaroslav Kysela 	link->dobj.index = tplg->index;
19438ce1cbd6SJaroslav Kysela 	link->dobj.ops = tplg->ops;
19448ce1cbd6SJaroslav Kysela 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
19458ce1cbd6SJaroslav Kysela 
19468f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
19478f27c4abSMengdong Lin 		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
19488f27c4abSMengdong Lin 		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1949abc3caacSAmadeusz Sławiński 		if (!link->name || !link->stream_name) {
1950abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1951abc3caacSAmadeusz Sławiński 			goto err;
1952abc3caacSAmadeusz Sławiński 		}
19538f27c4abSMengdong Lin 	}
19545aebe7c7SPierre-Louis Bossart 	link->id = le32_to_cpu(pcm->pcm_id);
1955acfc7d46SMengdong Lin 
1956abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
195723b946ceSKuninori Morimoto 		link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1958abc3caacSAmadeusz Sławiński 		if (!link->cpus->dai_name) {
1959abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1960abc3caacSAmadeusz Sławiński 			goto err;
1961abc3caacSAmadeusz Sławiński 		}
1962abc3caacSAmadeusz Sławiński 	}
19638f27c4abSMengdong Lin 
196423b946ceSKuninori Morimoto 	link->codecs->name = "snd-soc-dummy";
196523b946ceSKuninori Morimoto 	link->codecs->dai_name = "snd-soc-dummy-dai";
196667d1c21eSGuneshwor Singh 
19673e6de894SPierre-Louis Bossart 	link->platforms->name = "snd-soc-dummy";
19683e6de894SPierre-Louis Bossart 
196967d1c21eSGuneshwor Singh 	/* enable DPCM */
197067d1c21eSGuneshwor Singh 	link->dynamic = 1;
19715aebe7c7SPierre-Louis Bossart 	link->dpcm_playback = le32_to_cpu(pcm->playback);
19725aebe7c7SPierre-Louis Bossart 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1973717a8e72SMengdong Lin 	if (pcm->flag_mask)
19745aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
19755aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flag_mask),
19765aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flags));
197767d1c21eSGuneshwor Singh 
1978acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1979c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1980acfc7d46SMengdong Lin 	if (ret < 0) {
1981acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
198276d27036SDragos Tarcatu 		goto err;
198376d27036SDragos Tarcatu 	}
198476d27036SDragos Tarcatu 
19852acf6ce2SMark Brown 	ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
198676d27036SDragos Tarcatu 	if (ret < 0) {
198776d27036SDragos Tarcatu 		dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
198876d27036SDragos Tarcatu 		goto err;
1989acfc7d46SMengdong Lin 	}
1990acfc7d46SMengdong Lin 
1991acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1992acfc7d46SMengdong Lin 
1993acfc7d46SMengdong Lin 	return 0;
199476d27036SDragos Tarcatu err:
199576d27036SDragos Tarcatu 	kfree(link->name);
199676d27036SDragos Tarcatu 	kfree(link->stream_name);
199776d27036SDragos Tarcatu 	kfree(link->cpus->dai_name);
199876d27036SDragos Tarcatu 	kfree(link);
199976d27036SDragos Tarcatu 	return ret;
2000acfc7d46SMengdong Lin }
2001acfc7d46SMengdong Lin 
2002acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
200364527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
200464527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
200564527e8aSMengdong Lin {
2006acfc7d46SMengdong Lin 	int ret;
2007acfc7d46SMengdong Lin 
2008acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
2009acfc7d46SMengdong Lin 	if (ret < 0)
2010acfc7d46SMengdong Lin 		return ret;
2011acfc7d46SMengdong Lin 
2012ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
201364527e8aSMengdong Lin }
201464527e8aSMengdong Lin 
201555726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
201655726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
201755726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
201855726dc9SMengdong Lin {
20195aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
202055726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
202155726dc9SMengdong Lin 	dest->formats = src->formats;
202255726dc9SMengdong Lin 	dest->rates = src->rates;
202355726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
202455726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
202555726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
202655726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
202755726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
202855726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
202955726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
203055726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
203155726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
203255726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
203355726dc9SMengdong Lin }
203455726dc9SMengdong Lin 
203555726dc9SMengdong Lin /**
203655726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
203755726dc9SMengdong Lin  * @tplg: topology context
203855726dc9SMengdong Lin  * @src: older version of pcm as a source
203955726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
204055726dc9SMengdong Lin  *
204155726dc9SMengdong Lin  * Support from vesion 4. User should free the returned pcm manually.
204255726dc9SMengdong Lin  */
204355726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
204455726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
204555726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
204655726dc9SMengdong Lin {
204755726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
204855726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
204955726dc9SMengdong Lin 	int i;
205055726dc9SMengdong Lin 
205155726dc9SMengdong Lin 	*pcm = NULL;
205255726dc9SMengdong Lin 
20535aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
205455726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
205555726dc9SMengdong Lin 		return -EINVAL;
205655726dc9SMengdong Lin 	}
205755726dc9SMengdong Lin 
205855726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
205955726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
206055726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
206155726dc9SMengdong Lin 	if (!dest)
206255726dc9SMengdong Lin 		return -ENOMEM;
206355726dc9SMengdong Lin 
20645aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
206555726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
206655726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
206755726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
206855726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
206955726dc9SMengdong Lin 	dest->playback = src_v4->playback;
207055726dc9SMengdong Lin 	dest->capture = src_v4->capture;
207155726dc9SMengdong Lin 	dest->compress = src_v4->compress;
207255726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
20735aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
207455726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
207555726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
207655726dc9SMengdong Lin 
207755726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
207855726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
207955726dc9SMengdong Lin 
208055726dc9SMengdong Lin 	*pcm = dest;
208155726dc9SMengdong Lin 	return 0;
208255726dc9SMengdong Lin }
208355726dc9SMengdong Lin 
208464527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
20858a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
20868a978234SLiam Girdwood {
208755726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
20885aebe7c7SPierre-Louis Bossart 	int count;
20895aebe7c7SPierre-Louis Bossart 	int size;
2090fd340455SVinod Koul 	int i;
209155726dc9SMengdong Lin 	bool abi_match;
2092a3039aefSDragos Tarcatu 	int ret;
20938a978234SLiam Girdwood 
20945aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
20955aebe7c7SPierre-Louis Bossart 
20968a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
20978a978234SLiam Girdwood 		return 0;
20988a978234SLiam Girdwood 
209955726dc9SMengdong Lin 	/* check the element size and count */
210055726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
21015aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(pcm->size);
21025aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_pcm)
21035aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
210455726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
21055aebe7c7SPierre-Louis Bossart 			size);
210655726dc9SMengdong Lin 		return -EINVAL;
210755726dc9SMengdong Lin 	}
210855726dc9SMengdong Lin 
21098a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
21105aebe7c7SPierre-Louis Bossart 				      size, count,
21115aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
21125aebe7c7SPierre-Louis Bossart 				      "PCM DAI")) {
21138a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
21148a978234SLiam Girdwood 			count);
21158a978234SLiam Girdwood 		return -EINVAL;
21168a978234SLiam Girdwood 	}
21178a978234SLiam Girdwood 
211864527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
211955726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
21205aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(pcm->size);
212155726dc9SMengdong Lin 
212255726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
212355726dc9SMengdong Lin 		 * if abi not match.
212455726dc9SMengdong Lin 		 */
21255aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*pcm)) {
212655726dc9SMengdong Lin 			abi_match = true;
212755726dc9SMengdong Lin 			_pcm = pcm;
212855726dc9SMengdong Lin 		} else {
212955726dc9SMengdong Lin 			abi_match = false;
2130fd340455SVinod Koul 			pcm_new_ver(tplg, pcm, &_pcm);
213106eb49f7SMengdong Lin 		}
213206eb49f7SMengdong Lin 
213355726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
2134a3039aefSDragos Tarcatu 		ret = soc_tplg_pcm_create(tplg, _pcm);
2135a3039aefSDragos Tarcatu 		if (ret < 0) {
2136a3039aefSDragos Tarcatu 			if (!abi_match)
2137a3039aefSDragos Tarcatu 				kfree(_pcm);
2138a3039aefSDragos Tarcatu 			return ret;
2139a3039aefSDragos Tarcatu 		}
214055726dc9SMengdong Lin 
2141717a8e72SMengdong Lin 		/* offset by version-specific struct size and
2142717a8e72SMengdong Lin 		 * real priv data size
2143717a8e72SMengdong Lin 		 */
21445aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2145717a8e72SMengdong Lin 
214655726dc9SMengdong Lin 		if (!abi_match)
214755726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
214864527e8aSMengdong Lin 	}
214964527e8aSMengdong Lin 
21508a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
21518a978234SLiam Girdwood 
21528a978234SLiam Girdwood 	return 0;
21538a978234SLiam Girdwood }
21548a978234SLiam Girdwood 
21550038be9aSMengdong Lin /**
2156593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
21578abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
2158593d9e52SMengdong Lin  * @cfg: physical link configs.
2159593d9e52SMengdong Lin  *
2160593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
2161593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
2162593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
2163593d9e52SMengdong Lin  */
2164593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
2165593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
2166593d9e52SMengdong Lin {
2167593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
2168593d9e52SMengdong Lin 	unsigned char bclk_master, fsync_master;
2169593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
2170593d9e52SMengdong Lin 	int i;
2171593d9e52SMengdong Lin 
21725aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2173593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
2174593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
2175593d9e52SMengdong Lin 			continue;
2176593d9e52SMengdong Lin 
21775aebe7c7SPierre-Louis Bossart 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
21785aebe7c7SPierre-Louis Bossart 			SND_SOC_DAIFMT_FORMAT_MASK;
2179593d9e52SMengdong Lin 
2180933e1c4aSKirill Marinushkin 		/* clock gating */
2181fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
2182fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2183933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2184fbeabd09SKirill Marinushkin 			break;
2185fbeabd09SKirill Marinushkin 
2186fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2187933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2188fbeabd09SKirill Marinushkin 			break;
2189fbeabd09SKirill Marinushkin 
2190fbeabd09SKirill Marinushkin 		default:
2191fbeabd09SKirill Marinushkin 			/* ignore the value */
2192fbeabd09SKirill Marinushkin 			break;
2193fbeabd09SKirill Marinushkin 		}
2194933e1c4aSKirill Marinushkin 
2195593d9e52SMengdong Lin 		/* clock signal polarity */
2196593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
2197593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
2198593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
2199593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2200593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
2201593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2202593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
2203593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2204593d9e52SMengdong Lin 		else
2205593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2206593d9e52SMengdong Lin 
2207593d9e52SMengdong Lin 		/* clock masters */
2208a941e2faSKirill Marinushkin 		bclk_master = (hw_config->bclk_master ==
2209a941e2faSKirill Marinushkin 			       SND_SOC_TPLG_BCLK_CM);
2210a941e2faSKirill Marinushkin 		fsync_master = (hw_config->fsync_master ==
2211a941e2faSKirill Marinushkin 				SND_SOC_TPLG_FSYNC_CM);
2212a941e2faSKirill Marinushkin 		if (bclk_master && fsync_master)
2213593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2214593d9e52SMengdong Lin 		else if (!bclk_master && fsync_master)
2215a941e2faSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2216a941e2faSKirill Marinushkin 		else if (bclk_master && !fsync_master)
2217593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2218593d9e52SMengdong Lin 		else
2219593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2220593d9e52SMengdong Lin 	}
2221593d9e52SMengdong Lin }
2222593d9e52SMengdong Lin 
2223593d9e52SMengdong Lin /**
2224593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
2225593d9e52SMengdong Lin  * version of source.
22268abab35fSCharles Keepax  * @tplg: topology context
2227593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
2228593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
2229593d9e52SMengdong Lin  *
2230593d9e52SMengdong Lin  * Support from vesion 4. User need free the returned link config manually.
2231593d9e52SMengdong Lin  */
2232593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
2233593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
2234593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
2235593d9e52SMengdong Lin {
2236593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
2237593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
2238593d9e52SMengdong Lin 	int i;
2239593d9e52SMengdong Lin 
2240593d9e52SMengdong Lin 	*link = NULL;
2241593d9e52SMengdong Lin 
22425aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) !=
22435aebe7c7SPierre-Louis Bossart 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
2244593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2245593d9e52SMengdong Lin 		return -EINVAL;
2246593d9e52SMengdong Lin 	}
2247593d9e52SMengdong Lin 
2248593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2249593d9e52SMengdong Lin 
2250593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2251593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2252593d9e52SMengdong Lin 	if (!dest)
2253593d9e52SMengdong Lin 		return -ENOMEM;
2254593d9e52SMengdong Lin 
22555aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
2256593d9e52SMengdong Lin 	dest->id = src_v4->id;
2257593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
22585aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2259593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2260593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2261593d9e52SMengdong Lin 
2262593d9e52SMengdong Lin 	*link = dest;
2263593d9e52SMengdong Lin 	return 0;
2264593d9e52SMengdong Lin }
2265593d9e52SMengdong Lin 
2266d6f31e0eSKuninori Morimoto /**
2267d6f31e0eSKuninori Morimoto  * snd_soc_find_dai_link - Find a DAI link
2268d6f31e0eSKuninori Morimoto  *
2269d6f31e0eSKuninori Morimoto  * @card: soc card
2270d6f31e0eSKuninori Morimoto  * @id: DAI link ID to match
2271d6f31e0eSKuninori Morimoto  * @name: DAI link name to match, optional
2272d6f31e0eSKuninori Morimoto  * @stream_name: DAI link stream name to match, optional
2273d6f31e0eSKuninori Morimoto  *
2274d6f31e0eSKuninori Morimoto  * This function will search all existing DAI links of the soc card to
2275d6f31e0eSKuninori Morimoto  * find the link of the same ID. Since DAI links may not have their
2276d6f31e0eSKuninori Morimoto  * unique ID, so name and stream name should also match if being
2277d6f31e0eSKuninori Morimoto  * specified.
2278d6f31e0eSKuninori Morimoto  *
2279d6f31e0eSKuninori Morimoto  * Return: pointer of DAI link, or NULL if not found.
2280d6f31e0eSKuninori Morimoto  */
2281d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2282d6f31e0eSKuninori Morimoto 						      int id, const char *name,
2283d6f31e0eSKuninori Morimoto 						      const char *stream_name)
2284d6f31e0eSKuninori Morimoto {
2285d6f31e0eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
2286d6f31e0eSKuninori Morimoto 	struct snd_soc_dai_link *link;
2287d6f31e0eSKuninori Morimoto 
2288d6f31e0eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2289d6f31e0eSKuninori Morimoto 		link = rtd->dai_link;
2290d6f31e0eSKuninori Morimoto 
2291d6f31e0eSKuninori Morimoto 		if (link->id != id)
2292d6f31e0eSKuninori Morimoto 			continue;
2293d6f31e0eSKuninori Morimoto 
2294d6f31e0eSKuninori Morimoto 		if (name && (!link->name || strcmp(name, link->name)))
2295d6f31e0eSKuninori Morimoto 			continue;
2296d6f31e0eSKuninori Morimoto 
2297d6f31e0eSKuninori Morimoto 		if (stream_name && (!link->stream_name
2298d6f31e0eSKuninori Morimoto 				    || strcmp(stream_name, link->stream_name)))
2299d6f31e0eSKuninori Morimoto 			continue;
2300d6f31e0eSKuninori Morimoto 
2301d6f31e0eSKuninori Morimoto 		return link;
2302d6f31e0eSKuninori Morimoto 	}
2303d6f31e0eSKuninori Morimoto 
2304d6f31e0eSKuninori Morimoto 	return NULL;
2305d6f31e0eSKuninori Morimoto }
2306d6f31e0eSKuninori Morimoto 
2307593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2308593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2309593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2310593d9e52SMengdong Lin {
2311593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2312593d9e52SMengdong Lin 	const char *name, *stream_name;
2313dbab1cb8SMengdong Lin 	size_t len;
2314593d9e52SMengdong Lin 	int ret;
2315593d9e52SMengdong Lin 
2316dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2317dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2318dbab1cb8SMengdong Lin 		return -EINVAL;
2319dbab1cb8SMengdong Lin 	else if (len)
2320dbab1cb8SMengdong Lin 		name = cfg->name;
2321dbab1cb8SMengdong Lin 	else
2322dbab1cb8SMengdong Lin 		name = NULL;
2323dbab1cb8SMengdong Lin 
2324dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2325dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2326dbab1cb8SMengdong Lin 		return -EINVAL;
2327dbab1cb8SMengdong Lin 	else if (len)
2328dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2329dbab1cb8SMengdong Lin 	else
2330dbab1cb8SMengdong Lin 		stream_name = NULL;
2331593d9e52SMengdong Lin 
23325aebe7c7SPierre-Louis Bossart 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2333593d9e52SMengdong Lin 				     name, stream_name);
2334593d9e52SMengdong Lin 	if (!link) {
2335593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2336593d9e52SMengdong Lin 			name, cfg->id);
2337593d9e52SMengdong Lin 		return -EINVAL;
2338593d9e52SMengdong Lin 	}
2339593d9e52SMengdong Lin 
2340593d9e52SMengdong Lin 	/* hw format */
2341593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2342593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2343593d9e52SMengdong Lin 
2344593d9e52SMengdong Lin 	/* flags */
2345593d9e52SMengdong Lin 	if (cfg->flag_mask)
23465aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
23475aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flag_mask),
23485aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flags));
2349593d9e52SMengdong Lin 
2350593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2351c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2352593d9e52SMengdong Lin 	if (ret < 0) {
2353593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2354593d9e52SMengdong Lin 		return ret;
2355593d9e52SMengdong Lin 	}
2356593d9e52SMengdong Lin 
2357adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2358adfebb51SBard liao 	link->dobj.index = tplg->index;
2359adfebb51SBard liao 	link->dobj.ops = tplg->ops;
2360adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2361adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2362adfebb51SBard liao 
2363593d9e52SMengdong Lin 	return 0;
2364593d9e52SMengdong Lin }
2365593d9e52SMengdong Lin 
2366593d9e52SMengdong Lin 
2367593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2368593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2369593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2370593d9e52SMengdong Lin {
2371593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
23725aebe7c7SPierre-Louis Bossart 	int count;
23735aebe7c7SPierre-Louis Bossart 	int size;
2374593d9e52SMengdong Lin 	int i, ret;
2375593d9e52SMengdong Lin 	bool abi_match;
2376593d9e52SMengdong Lin 
23775aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
23785aebe7c7SPierre-Louis Bossart 
2379593d9e52SMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_LINK) {
23805aebe7c7SPierre-Louis Bossart 		tplg->pos += le32_to_cpu(hdr->size) +
23815aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->payload_size);
2382593d9e52SMengdong Lin 		return 0;
2383593d9e52SMengdong Lin 	};
2384593d9e52SMengdong Lin 
2385593d9e52SMengdong Lin 	/* check the element size and count */
2386593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
23875aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(link->size);
23885aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_link_config)
23895aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2390593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
23915aebe7c7SPierre-Louis Bossart 			size);
2392593d9e52SMengdong Lin 		return -EINVAL;
2393593d9e52SMengdong Lin 	}
2394593d9e52SMengdong Lin 
2395593d9e52SMengdong Lin 	if (soc_tplg_check_elem_count(tplg,
23965aebe7c7SPierre-Louis Bossart 				      size, count,
23975aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
23985aebe7c7SPierre-Louis Bossart 				      "physical link config")) {
2399593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2400593d9e52SMengdong Lin 			count);
2401593d9e52SMengdong Lin 		return -EINVAL;
2402593d9e52SMengdong Lin 	}
2403593d9e52SMengdong Lin 
2404593d9e52SMengdong Lin 	/* config physical DAI links */
2405593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2406593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
24075aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(link->size);
24085aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*link)) {
2409593d9e52SMengdong Lin 			abi_match = true;
2410593d9e52SMengdong Lin 			_link = link;
2411593d9e52SMengdong Lin 		} else {
2412593d9e52SMengdong Lin 			abi_match = false;
2413593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2414593d9e52SMengdong Lin 			if (ret < 0)
2415593d9e52SMengdong Lin 				return ret;
2416593d9e52SMengdong Lin 		}
2417593d9e52SMengdong Lin 
2418593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
24192b2d5c4dSDragos Tarcatu 		if (ret < 0) {
24202b2d5c4dSDragos Tarcatu 			if (!abi_match)
24212b2d5c4dSDragos Tarcatu 				kfree(_link);
2422593d9e52SMengdong Lin 			return ret;
24232b2d5c4dSDragos Tarcatu 		}
2424593d9e52SMengdong Lin 
2425593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2426593d9e52SMengdong Lin 		 * real priv data size
2427593d9e52SMengdong Lin 		 */
24285aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2429593d9e52SMengdong Lin 
2430593d9e52SMengdong Lin 		if (!abi_match)
2431593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2432593d9e52SMengdong Lin 	}
2433593d9e52SMengdong Lin 
2434593d9e52SMengdong Lin 	return 0;
2435593d9e52SMengdong Lin }
2436593d9e52SMengdong Lin 
2437593d9e52SMengdong Lin /**
24389aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
24390038be9aSMengdong Lin  * @tplg: topology context
24409aa3f034SMengdong Lin  * @d: physical DAI configs.
24410038be9aSMengdong Lin  *
24429aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
24439aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
24440038be9aSMengdong Lin  */
24459aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
24469aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
24470038be9aSMengdong Lin {
24485aebe7c7SPierre-Louis Bossart 	struct snd_soc_dai_link_component dai_component;
24490038be9aSMengdong Lin 	struct snd_soc_dai *dai;
24500038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
24510038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
24520038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
24530038be9aSMengdong Lin 	int ret;
24540038be9aSMengdong Lin 
24555aebe7c7SPierre-Louis Bossart 	memset(&dai_component, 0, sizeof(dai_component));
24565aebe7c7SPierre-Louis Bossart 
24579aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
24580038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
24590038be9aSMengdong Lin 	if (!dai) {
24609aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
24619aa3f034SMengdong Lin 			d->dai_name);
24620038be9aSMengdong Lin 		return -EINVAL;
24630038be9aSMengdong Lin 	}
24640038be9aSMengdong Lin 
24655aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(d->dai_id) != dai->id) {
24669aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
24679aa3f034SMengdong Lin 			d->dai_name);
24680038be9aSMengdong Lin 		return -EINVAL;
24690038be9aSMengdong Lin 	}
24700038be9aSMengdong Lin 
24710038be9aSMengdong Lin 	dai_drv = dai->driver;
24720038be9aSMengdong Lin 	if (!dai_drv)
24730038be9aSMengdong Lin 		return -EINVAL;
24740038be9aSMengdong Lin 
24759aa3f034SMengdong Lin 	if (d->playback) {
24760038be9aSMengdong Lin 		stream = &dai_drv->playback;
24779aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2478abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
2479abc3caacSAmadeusz Sławiński 		if (ret < 0)
2480abc3caacSAmadeusz Sławiński 			goto err;
24810038be9aSMengdong Lin 	}
24820038be9aSMengdong Lin 
24839aa3f034SMengdong Lin 	if (d->capture) {
24840038be9aSMengdong Lin 		stream = &dai_drv->capture;
24859aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2486abc3caacSAmadeusz Sławiński 		ret = set_stream_info(stream, caps);
2487abc3caacSAmadeusz Sławiński 		if (ret < 0)
2488abc3caacSAmadeusz Sławiński 			goto err;
24890038be9aSMengdong Lin 	}
24900038be9aSMengdong Lin 
24919aa3f034SMengdong Lin 	if (d->flag_mask)
24925aebe7c7SPierre-Louis Bossart 		set_dai_flags(dai_drv,
24935aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flag_mask),
24945aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flags));
24950038be9aSMengdong Lin 
24960038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2497c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
24980038be9aSMengdong Lin 	if (ret < 0) {
24990038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2500abc3caacSAmadeusz Sławiński 		goto err;
25010038be9aSMengdong Lin 	}
25020038be9aSMengdong Lin 
25030038be9aSMengdong Lin 	return 0;
2504abc3caacSAmadeusz Sławiński 
2505abc3caacSAmadeusz Sławiński err:
2506abc3caacSAmadeusz Sławiński 	kfree(dai_drv->playback.stream_name);
2507abc3caacSAmadeusz Sławiński 	kfree(dai_drv->capture.stream_name);
2508abc3caacSAmadeusz Sławiński 	return ret;
25090038be9aSMengdong Lin }
25100038be9aSMengdong Lin 
25119aa3f034SMengdong Lin /* load physical DAI elements */
25129aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
25130038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
25140038be9aSMengdong Lin {
25159aa3f034SMengdong Lin 	struct snd_soc_tplg_dai *dai;
25165aebe7c7SPierre-Louis Bossart 	int count;
25170038be9aSMengdong Lin 	int i;
25180038be9aSMengdong Lin 
25195aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
25205aebe7c7SPierre-Louis Bossart 
25210038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
25220038be9aSMengdong Lin 		return 0;
25230038be9aSMengdong Lin 
25240038be9aSMengdong Lin 	/* config the existing BE DAIs */
25250038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
25269aa3f034SMengdong Lin 		dai = (struct snd_soc_tplg_dai *)tplg->pos;
25275aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
25289aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
25290038be9aSMengdong Lin 			return -EINVAL;
25300038be9aSMengdong Lin 		}
25310038be9aSMengdong Lin 
25329aa3f034SMengdong Lin 		soc_tplg_dai_config(tplg, dai);
25335aebe7c7SPierre-Louis Bossart 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
25340038be9aSMengdong Lin 	}
25350038be9aSMengdong Lin 
25360038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
25370038be9aSMengdong Lin 	return 0;
25380038be9aSMengdong Lin }
25390038be9aSMengdong Lin 
2540583958faSMengdong Lin /**
2541583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2542583958faSMengdong Lin  * of source.
25438abab35fSCharles Keepax  * @tplg: topology context
2544583958faSMengdong Lin  * @src: old version of manifest as a source
2545583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2546583958faSMengdong Lin  *
2547583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
2548583958faSMengdong Lin  */
2549583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2550583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2551583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2552583958faSMengdong Lin {
2553583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2554583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
25555aebe7c7SPierre-Louis Bossart 	int size;
2556583958faSMengdong Lin 
2557583958faSMengdong Lin 	*manifest = NULL;
2558583958faSMengdong Lin 
25595aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(src->size);
25605aebe7c7SPierre-Louis Bossart 	if (size != sizeof(*src_v4)) {
2561ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
25625aebe7c7SPierre-Louis Bossart 			 size);
25635aebe7c7SPierre-Louis Bossart 		if (size)
2564583958faSMengdong Lin 			return -EINVAL;
25655aebe7c7SPierre-Louis Bossart 		src->size = cpu_to_le32(sizeof(*src_v4));
2566583958faSMengdong Lin 	}
2567583958faSMengdong Lin 
2568583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2569583958faSMengdong Lin 
2570583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
25715aebe7c7SPierre-Louis Bossart 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
25725aebe7c7SPierre-Louis Bossart 		       GFP_KERNEL);
2573583958faSMengdong Lin 	if (!dest)
2574583958faSMengdong Lin 		return -ENOMEM;
2575583958faSMengdong Lin 
25765aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2577583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2578583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2579583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2580583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2581583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2582583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2583583958faSMengdong Lin 	if (dest->priv.size)
2584583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
25855aebe7c7SPierre-Louis Bossart 		       le32_to_cpu(src_v4->priv.size));
2586583958faSMengdong Lin 
2587583958faSMengdong Lin 	*manifest = dest;
2588583958faSMengdong Lin 	return 0;
2589583958faSMengdong Lin }
25900038be9aSMengdong Lin 
25918a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
25928a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
25938a978234SLiam Girdwood {
2594583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2595583958faSMengdong Lin 	bool abi_match;
2596242c46c0SDragos Tarcatu 	int ret = 0;
25978a978234SLiam Girdwood 
25988a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
25998a978234SLiam Girdwood 		return 0;
26008a978234SLiam Girdwood 
26018a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2602583958faSMengdong Lin 
2603583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
26045aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2605583958faSMengdong Lin 		abi_match = true;
2606583958faSMengdong Lin 		_manifest = manifest;
2607583958faSMengdong Lin 	} else {
2608583958faSMengdong Lin 		abi_match = false;
2609242c46c0SDragos Tarcatu 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2610242c46c0SDragos Tarcatu 		if (ret < 0)
2611242c46c0SDragos Tarcatu 			return ret;
261206eb49f7SMengdong Lin 	}
261306eb49f7SMengdong Lin 
2614583958faSMengdong Lin 	/* pass control to component driver for optional further init */
2615c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->manifest)
2616242c46c0SDragos Tarcatu 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
26178a978234SLiam Girdwood 
2618583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2619583958faSMengdong Lin 		kfree(_manifest);
2620583958faSMengdong Lin 
2621242c46c0SDragos Tarcatu 	return ret;
26228a978234SLiam Girdwood }
26238a978234SLiam Girdwood 
26248a978234SLiam Girdwood /* validate header magic, size and type */
26258a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
26268a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
26278a978234SLiam Girdwood {
26288a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
26298a978234SLiam Girdwood 		return 0;
26308a978234SLiam Girdwood 
26315aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
263206eb49f7SMengdong Lin 		dev_err(tplg->dev,
263306eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
26345aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
263506eb49f7SMengdong Lin 			tplg->fw->size);
263606eb49f7SMengdong Lin 		return -EINVAL;
263706eb49f7SMengdong Lin 	}
263806eb49f7SMengdong Lin 
26398a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
26402114171dSPierre-Louis Bossart 	if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
26418a978234SLiam Girdwood 		dev_err(tplg->dev,
26428a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
26438a978234SLiam Girdwood 			tplg->pass, hdr->magic,
26448a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
26458a978234SLiam Girdwood 		return -EINVAL;
26468a978234SLiam Girdwood 	}
26478a978234SLiam Girdwood 
26485aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
26498a978234SLiam Girdwood 		dev_err(tplg->dev,
26508a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
26518a978234SLiam Girdwood 			tplg->pass, hdr->magic,
26528a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
26538a978234SLiam Girdwood 		return -EINVAL;
26548a978234SLiam Girdwood 	}
26558a978234SLiam Girdwood 
2656288b8da7SMengdong Lin 	/* Support ABI from version 4 */
26575aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
26585aebe7c7SPierre-Louis Bossart 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
26598a978234SLiam Girdwood 		dev_err(tplg->dev,
26608a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
26618a978234SLiam Girdwood 			tplg->pass, hdr->abi,
26628a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
26638a978234SLiam Girdwood 			tplg->fw->size);
26648a978234SLiam Girdwood 		return -EINVAL;
26658a978234SLiam Girdwood 	}
26668a978234SLiam Girdwood 
26678a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
26688a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
26698a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
26708a978234SLiam Girdwood 		return -EINVAL;
26718a978234SLiam Girdwood 	}
26728a978234SLiam Girdwood 
26735aebe7c7SPierre-Louis Bossart 	if (tplg->pass == le32_to_cpu(hdr->type))
26748a978234SLiam Girdwood 		dev_dbg(tplg->dev,
26758a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
26768a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
26778a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
26788a978234SLiam Girdwood 
26798a978234SLiam Girdwood 	return 1;
26808a978234SLiam Girdwood }
26818a978234SLiam Girdwood 
26828a978234SLiam Girdwood /* check header type and call appropriate handler */
26838a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
26848a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
26858a978234SLiam Girdwood {
26868a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
26878a978234SLiam Girdwood 
26888a978234SLiam Girdwood 	/* check for matching ID */
26895aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->index) != tplg->req_index &&
2690bb97142bSLiam Girdwood 		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
26918a978234SLiam Girdwood 		return 0;
26928a978234SLiam Girdwood 
26935aebe7c7SPierre-Louis Bossart 	tplg->index = le32_to_cpu(hdr->index);
26948a978234SLiam Girdwood 
26955aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(hdr->type)) {
26968a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
26978a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
26988a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
26998a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
27008a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
27018a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
27028a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
27038a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
27048a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
270564527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
27063fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
27079aa3f034SMengdong Lin 		return soc_tplg_dai_elems_load(tplg, hdr);
2708593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2709593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2710593d9e52SMengdong Lin 		/* physical link configurations */
2711593d9e52SMengdong Lin 		return soc_tplg_link_elems_load(tplg, hdr);
27128a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
27138a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
27148a978234SLiam Girdwood 	default:
27158a978234SLiam Girdwood 		/* bespoke vendor data object */
27168a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
27178a978234SLiam Girdwood 	}
27188a978234SLiam Girdwood 
27198a978234SLiam Girdwood 	return 0;
27208a978234SLiam Girdwood }
27218a978234SLiam Girdwood 
27228a978234SLiam Girdwood /* process the topology file headers */
27238a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
27248a978234SLiam Girdwood {
27258a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
27268a978234SLiam Girdwood 	int ret;
27278a978234SLiam Girdwood 
27288a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
27298a978234SLiam Girdwood 
27308a978234SLiam Girdwood 	/* process the header types from start to end */
27318a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
27328a978234SLiam Girdwood 
27338a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
27348a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
27358a978234SLiam Girdwood 
27368a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
27378a978234SLiam Girdwood 
27388a978234SLiam Girdwood 			/* make sure header is valid before loading */
27398a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
27408a978234SLiam Girdwood 			if (ret < 0)
27418a978234SLiam Girdwood 				return ret;
27428a978234SLiam Girdwood 			else if (ret == 0)
27438a978234SLiam Girdwood 				break;
27448a978234SLiam Girdwood 
27458a978234SLiam Girdwood 			/* load the header object */
27468a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
27478a978234SLiam Girdwood 			if (ret < 0)
27488a978234SLiam Girdwood 				return ret;
27498a978234SLiam Girdwood 
27508a978234SLiam Girdwood 			/* goto next header */
27515aebe7c7SPierre-Louis Bossart 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
27528a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
27538a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
27548a978234SLiam Girdwood 		}
27558a978234SLiam Girdwood 
27568a978234SLiam Girdwood 		/* next data type pass */
27578a978234SLiam Girdwood 		tplg->pass++;
27588a978234SLiam Girdwood 	}
27598a978234SLiam Girdwood 
27608a978234SLiam Girdwood 	/* signal DAPM we are complete */
27618a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
27628a978234SLiam Girdwood 	if (ret < 0)
27638a978234SLiam Girdwood 		dev_err(tplg->dev,
27648a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
27658a978234SLiam Girdwood 
27668a978234SLiam Girdwood 	return ret;
27678a978234SLiam Girdwood }
27688a978234SLiam Girdwood 
27698a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
27708a978234SLiam Girdwood {
27718a978234SLiam Girdwood 	int ret;
27728a978234SLiam Girdwood 
27738a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
27748a978234SLiam Girdwood 	if (ret == 0)
27758a978234SLiam Girdwood 		soc_tplg_complete(tplg);
27768a978234SLiam Girdwood 
27778a978234SLiam Girdwood 	return ret;
27788a978234SLiam Girdwood }
27798a978234SLiam Girdwood 
27808a978234SLiam Girdwood /* load audio component topology from "firmware" file */
27818a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
27828a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
27838a978234SLiam Girdwood {
27848a978234SLiam Girdwood 	struct soc_tplg tplg;
2785304017d3SBard liao 	int ret;
27868a978234SLiam Girdwood 
2787c42464a4SAmadeusz Sławiński 	/* component needs to exist to keep and reference data while parsing */
2788c42464a4SAmadeusz Sławiński 	if (!comp)
2789c42464a4SAmadeusz Sławiński 		return -EINVAL;
2790c42464a4SAmadeusz Sławiński 
27918a978234SLiam Girdwood 	/* setup parsing context */
27928a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
27938a978234SLiam Girdwood 	tplg.fw = fw;
27948a978234SLiam Girdwood 	tplg.dev = comp->dev;
27958a978234SLiam Girdwood 	tplg.comp = comp;
27968a978234SLiam Girdwood 	tplg.ops = ops;
27978a978234SLiam Girdwood 	tplg.req_index = id;
27988a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
27998a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
28001a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
28011a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
28028a978234SLiam Girdwood 
2803304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2804304017d3SBard liao 	/* free the created components if fail to load topology */
2805304017d3SBard liao 	if (ret)
2806304017d3SBard liao 		snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2807304017d3SBard liao 
2808304017d3SBard liao 	return ret;
28098a978234SLiam Girdwood }
28108a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
28118a978234SLiam Girdwood 
28128a978234SLiam Girdwood /* remove this dynamic widget */
28138a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
28148a978234SLiam Girdwood {
28158a978234SLiam Girdwood 	/* make sure we are a widget */
28168a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
28178a978234SLiam Girdwood 		return;
28188a978234SLiam Girdwood 
28198a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
28208a978234SLiam Girdwood }
28218a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
28228a978234SLiam Girdwood 
28238a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
28248a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
28258a978234SLiam Girdwood 	u32 index)
28268a978234SLiam Girdwood {
28278a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
28288a978234SLiam Girdwood 
282914596692SKuninori Morimoto 	for_each_card_widgets_safe(dapm->card, w, next_w) {
28308a978234SLiam Girdwood 
28318a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
28328a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
28338a978234SLiam Girdwood 			continue;
28348a978234SLiam Girdwood 
28358a978234SLiam Girdwood 		/* match ID */
28368a978234SLiam Girdwood 		if (w->dobj.index != index &&
28378a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
28388a978234SLiam Girdwood 			continue;
28398a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
28408a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2841b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
28428a978234SLiam Girdwood 	}
2843fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
28448a978234SLiam Girdwood }
28458a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
28468a978234SLiam Girdwood 
28478a978234SLiam Girdwood /* remove dynamic controls from the component driver */
28488a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
28498a978234SLiam Girdwood {
28508a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
28518a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
28528a978234SLiam Girdwood 
28538a978234SLiam Girdwood 	/* process the header types from end to start */
28548a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
28558a978234SLiam Girdwood 
28568a978234SLiam Girdwood 		/* remove mixer controls */
28578a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
28588a978234SLiam Girdwood 			list) {
28598a978234SLiam Girdwood 
28608a978234SLiam Girdwood 			/* match index */
28618a978234SLiam Girdwood 			if (dobj->index != index &&
2862feb12f0cSYan Wang 				index != SND_SOC_TPLG_INDEX_ALL)
28638a978234SLiam Girdwood 				continue;
28648a978234SLiam Girdwood 
28658a978234SLiam Girdwood 			switch (dobj->type) {
28668a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
28678a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
28688a978234SLiam Girdwood 				break;
28698a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
28708a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
28718a978234SLiam Girdwood 				break;
28728a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
28738a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
28748a978234SLiam Girdwood 				break;
28757df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
28767df04ea7SRanjani Sridharan 				remove_route(comp, dobj, pass);
28777df04ea7SRanjani Sridharan 				break;
28788a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
28798a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
28808a978234SLiam Girdwood 				break;
28818a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
288264527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
28838a978234SLiam Girdwood 				break;
2884acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2885acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2886acfc7d46SMengdong Lin 				break;
2887adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2888adfebb51SBard liao 				/*
2889adfebb51SBard liao 				 * call link_unload ops if extra
2890adfebb51SBard liao 				 * deinitialization is needed.
2891adfebb51SBard liao 				 */
2892adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2893adfebb51SBard liao 				break;
28948a978234SLiam Girdwood 			default:
28958a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
28968a978234SLiam Girdwood 					dobj->type);
28978a978234SLiam Girdwood 				break;
28988a978234SLiam Girdwood 			}
28998a978234SLiam Girdwood 		}
29008a978234SLiam Girdwood 		pass--;
29018a978234SLiam Girdwood 	}
29028a978234SLiam Girdwood 
29038a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
29048a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
29058a978234SLiam Girdwood }
29068a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2907