xref: /openbmc/linux/sound/soc/soc-topology.c (revision 67786b29)
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
435e2cd47aSAmadeusz Sławiński #define SOC_TPLG_PASS_CONTROL		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
476257d224SAmadeusz Sławiński #define SOC_TPLG_PASS_BE_DAI		6
486257d224SAmadeusz Sławiński #define SOC_TPLG_PASS_LINK		7
498a978234SLiam Girdwood 
508a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
51593d9e52SMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_LINK
528a978234SLiam Girdwood 
53583958faSMengdong Lin /* topology context */
548a978234SLiam Girdwood struct soc_tplg {
558a978234SLiam Girdwood 	const struct firmware *fw;
568a978234SLiam Girdwood 
578a978234SLiam Girdwood 	/* runtime FW parsing */
5800ac8389SQinghua Jin 	const u8 *pos;		/* read position */
598a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
608a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
618a978234SLiam Girdwood 
628a978234SLiam Girdwood 	/* component caller */
638a978234SLiam Girdwood 	struct device *dev;
648a978234SLiam Girdwood 	struct snd_soc_component *comp;
658a978234SLiam Girdwood 	u32 index;	/* current block index */
668a978234SLiam Girdwood 
6788a17d8fSMengdong Lin 	/* vendor specific kcontrol operations */
688a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
698a978234SLiam Girdwood 	int io_ops_count;
708a978234SLiam Girdwood 
711a3232d2SMengdong Lin 	/* vendor specific bytes ext handlers, for TLV bytes controls */
721a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
731a3232d2SMengdong Lin 	int bytes_ext_ops_count;
741a3232d2SMengdong Lin 
758a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
768a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
778a978234SLiam Girdwood };
788a978234SLiam Girdwood 
798a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
soc_tplg_check_elem_count(struct soc_tplg * tplg,size_t elem_size,unsigned int count,size_t bytes,const char * elem_type)808a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
818a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
828a978234SLiam Girdwood {
838a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
848a978234SLiam Girdwood 
858a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
868a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
878a978234SLiam Girdwood 			elem_type);
888a978234SLiam Girdwood 		return -EINVAL;
898a978234SLiam Girdwood 	}
908a978234SLiam Girdwood 
918a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
928a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
938a978234SLiam Girdwood 	if (elem_size * count > bytes) {
948a978234SLiam Girdwood 		dev_err(tplg->dev,
958a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
968a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
978a978234SLiam Girdwood 		return -EINVAL;
988a978234SLiam Girdwood 	}
998a978234SLiam Girdwood 
1008a978234SLiam Girdwood 	return 0;
1018a978234SLiam Girdwood }
1028a978234SLiam Girdwood 
soc_tplg_is_eof(struct soc_tplg * tplg)1034fad3cc6SAmadeusz Sławiński static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
1048a978234SLiam Girdwood {
1058a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1068a978234SLiam Girdwood 
1078a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1084fad3cc6SAmadeusz Sławiński 		return true;
1094fad3cc6SAmadeusz Sławiński 	return false;
1108a978234SLiam Girdwood }
1118a978234SLiam Girdwood 
soc_tplg_get_hdr_offset(struct soc_tplg * tplg)1128a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1138a978234SLiam Girdwood {
1148a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1158a978234SLiam Girdwood }
1168a978234SLiam Girdwood 
soc_tplg_get_offset(struct soc_tplg * tplg)1178a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1188a978234SLiam Girdwood {
1198a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1208a978234SLiam Girdwood }
1218a978234SLiam Girdwood 
1228a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1238a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1248a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1258a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1268a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1278a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1288a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1298a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1308a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1318a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1328a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1338a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1348a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1358a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1368a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1378a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1388a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1398a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1408a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1412c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1428a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1438a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1448a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1458a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1468a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1478a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1488a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1498a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1508a978234SLiam Girdwood };
1518a978234SLiam Girdwood 
1528a978234SLiam Girdwood struct soc_tplg_map {
1538a978234SLiam Girdwood 	int uid;
1548a978234SLiam Girdwood 	int kid;
1558a978234SLiam Girdwood };
1568a978234SLiam Girdwood 
1578a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1588a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1598a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1608a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1618a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1628a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1638a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1648a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1658a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1668a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1678a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1688a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1708a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1758a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
1768a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
1778a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
1788a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
1798a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
1808a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
1818a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
1828a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
1838a978234SLiam Girdwood };
1848a978234SLiam Girdwood 
tplg_chan_get_reg(struct soc_tplg * tplg,struct snd_soc_tplg_channel * chan,int map)1858f9974d9SAmadeusz Sławiński static int tplg_chan_get_reg(struct soc_tplg *tplg,
1868a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1878a978234SLiam Girdwood {
1888a978234SLiam Girdwood 	int i;
1898a978234SLiam Girdwood 
1908a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
1915aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
1925aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].reg);
1938a978234SLiam Girdwood 	}
1948a978234SLiam Girdwood 
1958a978234SLiam Girdwood 	return -EINVAL;
1968a978234SLiam Girdwood }
1978a978234SLiam Girdwood 
tplg_chan_get_shift(struct soc_tplg * tplg,struct snd_soc_tplg_channel * chan,int map)1988f9974d9SAmadeusz Sławiński static int tplg_chan_get_shift(struct soc_tplg *tplg,
1998a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2008a978234SLiam Girdwood {
2018a978234SLiam Girdwood 	int i;
2028a978234SLiam Girdwood 
2038a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2045aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(chan[i].id) == map)
2055aebe7c7SPierre-Louis Bossart 			return le32_to_cpu(chan[i].shift);
2068a978234SLiam Girdwood 	}
2078a978234SLiam Girdwood 
2088a978234SLiam Girdwood 	return -EINVAL;
2098a978234SLiam Girdwood }
2108a978234SLiam Girdwood 
get_widget_id(int tplg_type)2118a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2128a978234SLiam Girdwood {
2138a978234SLiam Girdwood 	int i;
2148a978234SLiam Girdwood 
2158a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2168a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2178a978234SLiam Girdwood 			return dapm_map[i].kid;
2188a978234SLiam Girdwood 	}
2198a978234SLiam Girdwood 
2208a978234SLiam Girdwood 	return -EINVAL;
2218a978234SLiam Girdwood }
2228a978234SLiam Girdwood 
soc_bind_err(struct soc_tplg * tplg,struct snd_soc_tplg_ctl_hdr * hdr,int index)2238a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2248a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2258a978234SLiam Girdwood {
2268a978234SLiam Girdwood 	dev_err(tplg->dev,
2278a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2288a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2298a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2308a978234SLiam Girdwood }
2318a978234SLiam Girdwood 
soc_control_err(struct soc_tplg * tplg,struct snd_soc_tplg_ctl_hdr * hdr,const char * name)2328a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2338a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2348a978234SLiam Girdwood {
2358a978234SLiam Girdwood 	dev_err(tplg->dev,
23634b31045SAmadeusz Sławiński 		"ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2378a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2388a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2398a978234SLiam Girdwood }
2408a978234SLiam Girdwood 
2418a978234SLiam Girdwood /* pass vendor data to component driver for processing */
soc_tplg_vendor_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)242c2cbd0a7SKeyon Jie static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2438a978234SLiam Girdwood 				struct snd_soc_tplg_hdr *hdr)
2448a978234SLiam Girdwood {
2458a978234SLiam Girdwood 	int ret = 0;
2468a978234SLiam Girdwood 
247c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->vendor_load)
248c60b613aSLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
2498a978234SLiam Girdwood 	else {
2508a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2518a978234SLiam Girdwood 			hdr->vendor_type);
2528a978234SLiam Girdwood 		return -EINVAL;
2538a978234SLiam Girdwood 	}
2548a978234SLiam Girdwood 
2558a978234SLiam Girdwood 	if (ret < 0)
2568a978234SLiam Girdwood 		dev_err(tplg->dev,
2578a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2588a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2598a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2608a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2618a978234SLiam Girdwood 	return ret;
2628a978234SLiam Girdwood }
2638a978234SLiam Girdwood 
2648a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2658a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
soc_tplg_widget_load(struct soc_tplg * tplg,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * tplg_w)2668a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2678a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2688a978234SLiam Girdwood {
269c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->widget_load)
270c60b613aSLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
271c60b613aSLiam Girdwood 			tplg_w);
2728a978234SLiam Girdwood 
2738a978234SLiam Girdwood 	return 0;
2748a978234SLiam Girdwood }
2758a978234SLiam Girdwood 
276ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
277ebd259d3SLiam Girdwood  * external widgets where we can assign private data/ops */
soc_tplg_widget_ready(struct soc_tplg * tplg,struct snd_soc_dapm_widget * w,struct snd_soc_tplg_dapm_widget * tplg_w)278ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg,
279ebd259d3SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
280ebd259d3SLiam Girdwood {
281c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->widget_ready)
282c60b613aSLiam Girdwood 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
283c60b613aSLiam Girdwood 			tplg_w);
284ebd259d3SLiam Girdwood 
285ebd259d3SLiam Girdwood 	return 0;
286ebd259d3SLiam Girdwood }
287ebd259d3SLiam Girdwood 
288183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */
soc_tplg_dai_load(struct soc_tplg * tplg,struct snd_soc_dai_driver * dai_drv,struct snd_soc_tplg_pcm * pcm,struct snd_soc_dai * dai)28964527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
290c60b613aSLiam Girdwood 	struct snd_soc_dai_driver *dai_drv,
291c60b613aSLiam Girdwood 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2928a978234SLiam Girdwood {
293c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dai_load)
294c60b613aSLiam Girdwood 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
295c60b613aSLiam Girdwood 			pcm, dai);
2968a978234SLiam Girdwood 
2978a978234SLiam Girdwood 	return 0;
2988a978234SLiam Girdwood }
2998a978234SLiam Girdwood 
300183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */
soc_tplg_dai_link_load(struct soc_tplg * tplg,struct snd_soc_dai_link * link,struct snd_soc_tplg_link_config * cfg)301acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
302c60b613aSLiam Girdwood 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
303acfc7d46SMengdong Lin {
304c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->link_load)
305c60b613aSLiam Girdwood 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
306acfc7d46SMengdong Lin 
307acfc7d46SMengdong Lin 	return 0;
308acfc7d46SMengdong Lin }
309acfc7d46SMengdong Lin 
3108a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
soc_tplg_complete(struct soc_tplg * tplg)311415717e1SRanjani Sridharan static int soc_tplg_complete(struct soc_tplg *tplg)
3128a978234SLiam Girdwood {
313c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->complete)
314415717e1SRanjani Sridharan 		return tplg->ops->complete(tplg->comp);
315415717e1SRanjani Sridharan 
316415717e1SRanjani Sridharan 	return 0;
3178a978234SLiam Girdwood }
3188a978234SLiam Girdwood 
3198a978234SLiam Girdwood /* add a dynamic kcontrol */
soc_tplg_add_dcontrol(struct snd_card * card,struct device * dev,const struct snd_kcontrol_new * control_new,const char * prefix,void * data,struct snd_kcontrol ** kcontrol)3208a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3218a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3228a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3238a978234SLiam Girdwood {
3248a978234SLiam Girdwood 	int err;
3258a978234SLiam Girdwood 
3268a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3278a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3288a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3298a978234SLiam Girdwood 		control_new->name);
3308a978234SLiam Girdwood 		return -ENOMEM;
3318a978234SLiam Girdwood 	}
3328a978234SLiam Girdwood 
3338a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3348a978234SLiam Girdwood 	if (err < 0) {
3358a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3368a978234SLiam Girdwood 			control_new->name, err);
3378a978234SLiam Girdwood 		return err;
3388a978234SLiam Girdwood 	}
3398a978234SLiam Girdwood 
3408a978234SLiam Girdwood 	return 0;
3418a978234SLiam Girdwood }
3428a978234SLiam Girdwood 
3438a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
soc_tplg_add_kcontrol(struct soc_tplg * tplg,struct snd_kcontrol_new * k,struct snd_kcontrol ** kcontrol)3448a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3458a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3468a978234SLiam Girdwood {
3478a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3488a978234SLiam Girdwood 
3498a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3502a710bb3SAmadeusz Sławiński 				tplg->dev, k, comp->name_prefix, comp, kcontrol);
3518a978234SLiam Girdwood }
3528a978234SLiam Girdwood 
353fdfa3661SAmadeusz Sławiński /* remove kcontrol */
soc_tplg_remove_kcontrol(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)354fdfa3661SAmadeusz Sławiński static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
355fdfa3661SAmadeusz Sławiński 				     int pass)
3568a978234SLiam Girdwood {
3578a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3588a978234SLiam Girdwood 
3595e2cd47aSAmadeusz Sławiński 	if (pass != SOC_TPLG_PASS_CONTROL)
3608a978234SLiam Girdwood 		return;
3618a978234SLiam Girdwood 
36231e92739SAmadeusz Sławiński 	if (dobj->unload)
36331e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
3648a978234SLiam Girdwood 
36533ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
36633ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
3678a978234SLiam Girdwood }
3688a978234SLiam Girdwood 
3697df04ea7SRanjani Sridharan /* remove a route */
soc_tplg_remove_route(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)3702abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_route(struct snd_soc_component *comp,
3717df04ea7SRanjani Sridharan 			 struct snd_soc_dobj *dobj, int pass)
3727df04ea7SRanjani Sridharan {
3737df04ea7SRanjani Sridharan 	if (pass != SOC_TPLG_PASS_GRAPH)
3747df04ea7SRanjani Sridharan 		return;
3757df04ea7SRanjani Sridharan 
37631e92739SAmadeusz Sławiński 	if (dobj->unload)
37731e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
3787df04ea7SRanjani Sridharan 
3797df04ea7SRanjani Sridharan 	list_del(&dobj->list);
3807df04ea7SRanjani Sridharan }
3817df04ea7SRanjani Sridharan 
3828a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
soc_tplg_remove_widget(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)3832abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_widget(struct snd_soc_component *comp,
3848a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3858a978234SLiam Girdwood {
3868a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3878a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
3888a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
3898a978234SLiam Girdwood 	int i;
3908a978234SLiam Girdwood 
3918a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
3928a978234SLiam Girdwood 		return;
3938a978234SLiam Girdwood 
39431e92739SAmadeusz Sławiński 	if (dobj->unload)
39531e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
3968a978234SLiam Girdwood 
39705bdcf12SLiam Girdwood 	if (!w->kcontrols)
39805bdcf12SLiam Girdwood 		goto free_news;
39905bdcf12SLiam Girdwood 
4008d456654SAmadeusz Sławiński 	for (i = 0; w->kcontrols && i < w->num_kcontrols; i++)
4018d456654SAmadeusz Sławiński 		snd_ctl_remove(card, w->kcontrols[i]);
40205bdcf12SLiam Girdwood 
40305bdcf12SLiam Girdwood free_news:
40405bdcf12SLiam Girdwood 
405a46e8393SAmadeusz Sławiński 	list_del(&dobj->list);
406a46e8393SAmadeusz Sławiński 
4078a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
4088a978234SLiam Girdwood }
4098a978234SLiam Girdwood 
41064527e8aSMengdong Lin /* remove DAI configurations */
soc_tplg_remove_dai(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)4112abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_dai(struct snd_soc_component *comp,
4128a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4138a978234SLiam Girdwood {
41464527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
41564527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
416fc4cb1e1SAmadeusz Sławiński 	struct snd_soc_dai *dai, *_dai;
41764527e8aSMengdong Lin 
4188a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
4198a978234SLiam Girdwood 		return;
4208a978234SLiam Girdwood 
42131e92739SAmadeusz Sławiński 	if (dobj->unload)
42231e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
4238a978234SLiam Girdwood 
424fc4cb1e1SAmadeusz Sławiński 	for_each_component_dais_safe(comp, dai, _dai)
42552abe6ccSGuennadi Liakhovetski 		if (dai->driver == dai_drv)
426fc4cb1e1SAmadeusz Sławiński 			snd_soc_unregister_dai(dai);
42752abe6ccSGuennadi Liakhovetski 
4288a978234SLiam Girdwood 	list_del(&dobj->list);
4298a978234SLiam Girdwood }
4308a978234SLiam Girdwood 
431acfc7d46SMengdong Lin /* remove link configurations */
soc_tplg_remove_link(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)4322abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_link(struct snd_soc_component *comp,
433acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
434acfc7d46SMengdong Lin {
435acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
436acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
437acfc7d46SMengdong Lin 
438acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
439acfc7d46SMengdong Lin 		return;
440acfc7d46SMengdong Lin 
44131e92739SAmadeusz Sławiński 	if (dobj->unload)
44231e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
443acfc7d46SMengdong Lin 
444dd836ddfSDragos Tarcatu 	list_del(&dobj->list);
44550cd9b53SKuninori Morimoto 	snd_soc_remove_pcm_runtime(comp->card,
44650cd9b53SKuninori Morimoto 			snd_soc_get_pcm_runtime(comp->card, link));
447acfc7d46SMengdong Lin }
448acfc7d46SMengdong Lin 
449adfebb51SBard liao /* unload dai link */
remove_backend_link(struct snd_soc_component * comp,struct snd_soc_dobj * dobj,int pass)450adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp,
451adfebb51SBard liao 	struct snd_soc_dobj *dobj, int pass)
452adfebb51SBard liao {
453adfebb51SBard liao 	if (pass != SOC_TPLG_PASS_LINK)
454adfebb51SBard liao 		return;
455adfebb51SBard liao 
45631e92739SAmadeusz Sławiński 	if (dobj->unload)
45731e92739SAmadeusz Sławiński 		dobj->unload(comp, dobj);
458adfebb51SBard liao 
459adfebb51SBard liao 	/*
4602abfd4bdSAmadeusz Sławiński 	 * We don't free the link here as what soc_tplg_remove_link() do since BE
461adfebb51SBard liao 	 * links are not allocated by topology.
462adfebb51SBard liao 	 * We however need to reset the dobj type to its initial values
463adfebb51SBard liao 	 */
464adfebb51SBard liao 	dobj->type = SND_SOC_DOBJ_NONE;
465adfebb51SBard liao 	list_del(&dobj->list);
466adfebb51SBard liao }
467adfebb51SBard liao 
4688a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr * hdr,struct snd_kcontrol_new * k,const struct soc_tplg * tplg)4698a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
4708a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
4712b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
4728a978234SLiam Girdwood {
4732b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
4741a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
4752b5cdb91SMengdong Lin 	int num_ops, i;
4768a978234SLiam Girdwood 
4775aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
4781a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
479feb00b73SAmadeusz Sławiński 		&& (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
480feb00b73SAmadeusz Sławiński 		    || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
4811a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
4821a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
4831a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
4841a3232d2SMengdong Lin 
4851a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
4861a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
4871a3232d2SMengdong Lin 
4881a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
4891a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
4901a3232d2SMengdong Lin 		 */
491f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
4921a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
4931a3232d2SMengdong Lin 
4946788fc1aSPierre-Louis Bossart 		/*
4956788fc1aSPierre-Louis Bossart 		 * When a topology-based implementation abuses the
4966788fc1aSPierre-Louis Bossart 		 * control interface and uses bytes_ext controls of
4976788fc1aSPierre-Louis Bossart 		 * more than 512 bytes, we need to disable the size
4986788fc1aSPierre-Louis Bossart 		 * checks, otherwise accesses to such controls will
4996788fc1aSPierre-Louis Bossart 		 * return an -EINVAL error and prevent the card from
5006788fc1aSPierre-Louis Bossart 		 * being configured.
5016788fc1aSPierre-Louis Bossart 		 */
5022c7463d0STakashi Iwai 		if (sbe->max > 512)
5036788fc1aSPierre-Louis Bossart 			k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
5046788fc1aSPierre-Louis Bossart 
5051a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
5061a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
5071a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
50872bbeda0SPierre-Louis Bossart 			if (!sbe->put &&
50972bbeda0SPierre-Louis Bossart 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
5101a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
51172bbeda0SPierre-Louis Bossart 			if (!sbe->get &&
51272bbeda0SPierre-Louis Bossart 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
5131a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
5141a3232d2SMengdong Lin 		}
5151a3232d2SMengdong Lin 
516819b9f60SDharageswari R 		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
5171a3232d2SMengdong Lin 			return -EINVAL;
518819b9f60SDharageswari R 		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
519819b9f60SDharageswari R 			return -EINVAL;
520819b9f60SDharageswari R 		return 0;
5211a3232d2SMengdong Lin 	}
5221a3232d2SMengdong Lin 
52388a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
5242b5cdb91SMengdong Lin 	ops = tplg->io_ops;
5252b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
5268a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
5278a978234SLiam Girdwood 
52872bbeda0SPierre-Louis Bossart 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
5298a978234SLiam Girdwood 			k->put = ops[i].put;
53072bbeda0SPierre-Louis Bossart 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
5318a978234SLiam Girdwood 			k->get = ops[i].get;
53272bbeda0SPierre-Louis Bossart 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
5332b5cdb91SMengdong Lin 			k->info = ops[i].info;
5348a978234SLiam Girdwood 	}
5358a978234SLiam Girdwood 
53688a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
53788a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
53888a17d8fSMengdong Lin 		return 0;
53988a17d8fSMengdong Lin 
54088a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
5412b5cdb91SMengdong Lin 	ops = io_ops;
5422b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
54388a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
54488a17d8fSMengdong Lin 
54572bbeda0SPierre-Louis Bossart 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
54688a17d8fSMengdong Lin 			k->put = ops[i].put;
54772bbeda0SPierre-Louis Bossart 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
54888a17d8fSMengdong Lin 			k->get = ops[i].get;
54972bbeda0SPierre-Louis Bossart 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
5508a978234SLiam Girdwood 			k->info = ops[i].info;
5518a978234SLiam Girdwood 	}
5528a978234SLiam Girdwood 
5538a978234SLiam Girdwood 	/* standard handlers found ? */
5548a978234SLiam Girdwood 	if (k->put && k->get && k->info)
5558a978234SLiam Girdwood 		return 0;
5568a978234SLiam Girdwood 
5578a978234SLiam Girdwood 	/* nothing to bind */
5588a978234SLiam Girdwood 	return -EINVAL;
5598a978234SLiam Girdwood }
5608a978234SLiam Girdwood 
5618a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget * w,const struct snd_soc_tplg_widget_events * events,int num_events,u16 event_type)5628a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
5638a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
5648a978234SLiam Girdwood 		int num_events, u16 event_type)
5658a978234SLiam Girdwood {
5668a978234SLiam Girdwood 	int i;
5678a978234SLiam Girdwood 
5688a978234SLiam Girdwood 	w->event = NULL;
5698a978234SLiam Girdwood 
5708a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
5718a978234SLiam Girdwood 		if (event_type == events[i].type) {
5728a978234SLiam Girdwood 
5738a978234SLiam Girdwood 			/* found - so assign event */
5748a978234SLiam Girdwood 			w->event = events[i].event_handler;
5758a978234SLiam Girdwood 			return 0;
5768a978234SLiam Girdwood 		}
5778a978234SLiam Girdwood 	}
5788a978234SLiam Girdwood 
5798a978234SLiam Girdwood 	/* not found */
5808a978234SLiam Girdwood 	return -EINVAL;
5818a978234SLiam Girdwood }
5828a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
5838a978234SLiam Girdwood 
5848a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
soc_tplg_control_load(struct soc_tplg * tplg,struct snd_kcontrol_new * k,struct snd_soc_tplg_ctl_hdr * hdr)585430791ddSAmadeusz Sławiński static int soc_tplg_control_load(struct soc_tplg *tplg,
5868a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
5878a978234SLiam Girdwood {
588ec5dffcdSAmadeusz Sławiński 	int ret = 0;
5898a978234SLiam Girdwood 
590ec5dffcdSAmadeusz Sławiński 	if (tplg->ops && tplg->ops->control_load)
591ec5dffcdSAmadeusz Sławiński 		ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
592ec5dffcdSAmadeusz Sławiński 
593ec5dffcdSAmadeusz Sławiński 	if (ret)
594ec5dffcdSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
595ec5dffcdSAmadeusz Sławiński 
596ec5dffcdSAmadeusz Sławiński 	return ret;
5978a978234SLiam Girdwood }
5988a978234SLiam Girdwood 
59928a87eebSMengdong Lin 
soc_tplg_create_tlv_db_scale(struct soc_tplg * tplg,struct snd_kcontrol_new * kc,struct snd_soc_tplg_tlv_dbscale * scale)60028a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
60128a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6028a978234SLiam Girdwood {
60328a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
60428a87eebSMengdong Lin 	unsigned int *p;
6058a978234SLiam Girdwood 
606ff922622SAmadeusz Sławiński 	p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
60728a87eebSMengdong Lin 	if (!p)
6088a978234SLiam Girdwood 		return -ENOMEM;
6098a978234SLiam Girdwood 
61028a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
61128a87eebSMengdong Lin 	p[1] = item_len;
6125aebe7c7SPierre-Louis Bossart 	p[2] = le32_to_cpu(scale->min);
6135aebe7c7SPierre-Louis Bossart 	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
6145aebe7c7SPierre-Louis Bossart 		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
6158a978234SLiam Girdwood 
61628a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
61728a87eebSMengdong Lin 	return 0;
61828a87eebSMengdong Lin }
61928a87eebSMengdong Lin 
soc_tplg_create_tlv(struct soc_tplg * tplg,struct snd_kcontrol_new * kc,struct snd_soc_tplg_ctl_hdr * tc)62028a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
62128a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
62228a87eebSMengdong Lin {
62328a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
6245aebe7c7SPierre-Louis Bossart 	u32 access = le32_to_cpu(tc->access);
62528a87eebSMengdong Lin 
6265aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
62728a87eebSMengdong Lin 		return 0;
62828a87eebSMengdong Lin 
6295aebe7c7SPierre-Louis Bossart 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
63028a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
6315aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(tplg_tlv->type)) {
63228a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
63328a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
63428a87eebSMengdong Lin 					&tplg_tlv->scale);
63528a87eebSMengdong Lin 
63628a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
63728a87eebSMengdong Lin 		default:
63828a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
63928a87eebSMengdong Lin 					tplg_tlv->type);
64028a87eebSMengdong Lin 			return -EINVAL;
64128a87eebSMengdong Lin 		}
64228a87eebSMengdong Lin 	}
6438a978234SLiam Girdwood 
6448a978234SLiam Girdwood 	return 0;
6458a978234SLiam Girdwood }
6468a978234SLiam Girdwood 
soc_tplg_dbytes_create(struct soc_tplg * tplg,size_t size)6470db627c4SAmadeusz Sławiński static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
6488a978234SLiam Girdwood {
6498a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
6508a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
6518a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
6520db627c4SAmadeusz Sławiński 	int ret = 0;
6538a978234SLiam Girdwood 
6548a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
6553ce57f22SAmadeusz Sławiński 				      sizeof(struct snd_soc_tplg_bytes_control),
6560db627c4SAmadeusz Sławiński 				      1, size, "mixer bytes"))
6578a978234SLiam Girdwood 		return -EINVAL;
6588a978234SLiam Girdwood 
6598a978234SLiam Girdwood 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
6608a978234SLiam Girdwood 
6618a978234SLiam Girdwood 	/* validate kcontrol */
6628a978234SLiam Girdwood 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
6638a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
6648a978234SLiam Girdwood 		return -EINVAL;
6658a978234SLiam Girdwood 
666ff922622SAmadeusz Sławiński 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
6678a978234SLiam Girdwood 	if (sbe == NULL)
6688a978234SLiam Girdwood 		return -ENOMEM;
6698a978234SLiam Girdwood 
6708a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
6715aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(be->priv.size));
6728a978234SLiam Girdwood 
6738a978234SLiam Girdwood 	dev_dbg(tplg->dev,
6748a978234SLiam Girdwood 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
6758a978234SLiam Girdwood 		be->hdr.name, be->hdr.access);
6768a978234SLiam Girdwood 
6778a978234SLiam Girdwood 	memset(&kc, 0, sizeof(kc));
6788a978234SLiam Girdwood 	kc.name = be->hdr.name;
6798a978234SLiam Girdwood 	kc.private_value = (long)sbe;
6808a978234SLiam Girdwood 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
6815aebe7c7SPierre-Louis Bossart 	kc.access = le32_to_cpu(be->hdr.access);
6828a978234SLiam Girdwood 
6835aebe7c7SPierre-Louis Bossart 	sbe->max = le32_to_cpu(be->max);
6848a978234SLiam Girdwood 	sbe->dobj.type = SND_SOC_DOBJ_BYTES;
68531e92739SAmadeusz Sławiński 	if (tplg->ops)
68631e92739SAmadeusz Sławiński 		sbe->dobj.unload = tplg->ops->control_unload;
6878a978234SLiam Girdwood 	INIT_LIST_HEAD(&sbe->dobj.list);
6888a978234SLiam Girdwood 
6898a978234SLiam Girdwood 	/* map io handlers */
6900db627c4SAmadeusz Sławiński 	ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
6910db627c4SAmadeusz Sławiński 	if (ret) {
6928a978234SLiam Girdwood 		soc_control_err(tplg, &be->hdr, be->hdr.name);
6930db627c4SAmadeusz Sławiński 		goto err;
6948a978234SLiam Girdwood 	}
6958a978234SLiam Girdwood 
6968a978234SLiam Girdwood 	/* pass control to driver for optional further init */
6979e2ee000SAmadeusz Sławiński 	ret = soc_tplg_control_load(tplg, &kc, &be->hdr);
698ec5dffcdSAmadeusz Sławiński 	if (ret < 0)
6990db627c4SAmadeusz Sławiński 		goto err;
7008a978234SLiam Girdwood 
7018a978234SLiam Girdwood 	/* register control here */
7020db627c4SAmadeusz Sławiński 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
7032316c11fSAmadeusz Sławiński 	if (ret < 0)
7040db627c4SAmadeusz Sławiński 		goto err;
7058a978234SLiam Girdwood 
7068a978234SLiam Girdwood 	list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
7078a978234SLiam Girdwood 
7080db627c4SAmadeusz Sławiński err:
7090db627c4SAmadeusz Sławiński 	return ret;
7108a978234SLiam Girdwood }
7118a978234SLiam Girdwood 
soc_tplg_dmixer_create(struct soc_tplg * tplg,size_t size)7120db627c4SAmadeusz Sławiński static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
7138a978234SLiam Girdwood {
7148a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
7158a978234SLiam Girdwood 	struct soc_mixer_control *sm;
7168a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7170db627c4SAmadeusz Sławiński 	int ret = 0;
7188a978234SLiam Girdwood 
7198a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7208a978234SLiam Girdwood 				      sizeof(struct snd_soc_tplg_mixer_control),
7210db627c4SAmadeusz Sławiński 				      1, size, "mixers"))
7228a978234SLiam Girdwood 		return -EINVAL;
7238a978234SLiam Girdwood 
7248a978234SLiam Girdwood 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
7258a978234SLiam Girdwood 
7268a978234SLiam Girdwood 	/* validate kcontrol */
7278a978234SLiam Girdwood 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7288a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7298a978234SLiam Girdwood 		return -EINVAL;
7308a978234SLiam Girdwood 
731ff922622SAmadeusz Sławiński 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
7328a978234SLiam Girdwood 	if (sm == NULL)
7338a978234SLiam Girdwood 		return -ENOMEM;
7348a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
7355aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(mc->priv.size));
7368a978234SLiam Girdwood 
7378a978234SLiam Girdwood 	dev_dbg(tplg->dev,
7388a978234SLiam Girdwood 		"ASoC: adding mixer kcontrol %s with access 0x%x\n",
7398a978234SLiam Girdwood 		mc->hdr.name, mc->hdr.access);
7408a978234SLiam Girdwood 
7418a978234SLiam Girdwood 	memset(&kc, 0, sizeof(kc));
7428a978234SLiam Girdwood 	kc.name = mc->hdr.name;
7438a978234SLiam Girdwood 	kc.private_value = (long)sm;
7448a978234SLiam Girdwood 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7455aebe7c7SPierre-Louis Bossart 	kc.access = le32_to_cpu(mc->hdr.access);
7468a978234SLiam Girdwood 
7478a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
7488f9974d9SAmadeusz Sławiński 	sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
7498f9974d9SAmadeusz Sławiński 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
7508f9974d9SAmadeusz Sławiński 	sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
7518f9974d9SAmadeusz Sławiński 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
7528a978234SLiam Girdwood 
7535aebe7c7SPierre-Louis Bossart 	sm->max = le32_to_cpu(mc->max);
7545aebe7c7SPierre-Louis Bossart 	sm->min = le32_to_cpu(mc->min);
7555aebe7c7SPierre-Louis Bossart 	sm->invert = le32_to_cpu(mc->invert);
7565aebe7c7SPierre-Louis Bossart 	sm->platform_max = le32_to_cpu(mc->platform_max);
7578a978234SLiam Girdwood 	sm->dobj.index = tplg->index;
7588a978234SLiam Girdwood 	sm->dobj.type = SND_SOC_DOBJ_MIXER;
75931e92739SAmadeusz Sławiński 	if (tplg->ops)
76031e92739SAmadeusz Sławiński 		sm->dobj.unload = tplg->ops->control_unload;
7618a978234SLiam Girdwood 	INIT_LIST_HEAD(&sm->dobj.list);
7628a978234SLiam Girdwood 
7638a978234SLiam Girdwood 	/* map io handlers */
7640db627c4SAmadeusz Sławiński 	ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
7650db627c4SAmadeusz Sławiński 	if (ret) {
7668a978234SLiam Girdwood 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
7670db627c4SAmadeusz Sławiński 		goto err;
7688a978234SLiam Girdwood 	}
7698a978234SLiam Girdwood 
7703789debfSBard liao 	/* create any TLV data */
7710db627c4SAmadeusz Sławiński 	ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
7720db627c4SAmadeusz Sławiński 	if (ret < 0) {
7730db627c4SAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
7740db627c4SAmadeusz Sławiński 		goto err;
775482db55aSAmadeusz Sławiński 	}
7763789debfSBard liao 
7778a978234SLiam Girdwood 	/* pass control to driver for optional further init */
7789e2ee000SAmadeusz Sławiński 	ret = soc_tplg_control_load(tplg, &kc, &mc->hdr);
779ec5dffcdSAmadeusz Sławiński 	if (ret < 0)
7800db627c4SAmadeusz Sławiński 		goto err;
7818a978234SLiam Girdwood 
7828a978234SLiam Girdwood 	/* register control here */
7830db627c4SAmadeusz Sławiński 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
7842316c11fSAmadeusz Sławiński 	if (ret < 0)
7850db627c4SAmadeusz Sławiński 		goto err;
7868a978234SLiam Girdwood 
7878a978234SLiam Girdwood 	list_add(&sm->dobj.list, &tplg->comp->dobj_list);
7888a978234SLiam Girdwood 
7890db627c4SAmadeusz Sławiński err:
7900db627c4SAmadeusz Sławiński 	return ret;
7918a978234SLiam Girdwood }
7928a978234SLiam Girdwood 
soc_tplg_denum_create_texts(struct soc_tplg * tplg,struct soc_enum * se,struct snd_soc_tplg_enum_control * ec)793ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
7948a978234SLiam Girdwood 				       struct snd_soc_tplg_enum_control *ec)
7958a978234SLiam Girdwood {
7968a978234SLiam Girdwood 	int i, ret;
7978a978234SLiam Girdwood 
798f5824e5cSAmadeusz Sławiński 	if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
799f5824e5cSAmadeusz Sławiński 		return -EINVAL;
800f5824e5cSAmadeusz Sławiński 
8018a978234SLiam Girdwood 	se->dobj.control.dtexts =
802ff922622SAmadeusz Sławiński 		devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
8038a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
8048a978234SLiam Girdwood 		return -ENOMEM;
8058a978234SLiam Girdwood 
80672bbeda0SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
8078a978234SLiam Girdwood 
8088a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8098a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
8108a978234SLiam Girdwood 			ret = -EINVAL;
8118a978234SLiam Girdwood 			goto err;
8128a978234SLiam Girdwood 		}
8138a978234SLiam Girdwood 
814ff922622SAmadeusz Sławiński 		se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
8158a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
8168a978234SLiam Girdwood 			ret = -ENOMEM;
8178a978234SLiam Girdwood 			goto err;
8188a978234SLiam Girdwood 		}
8198a978234SLiam Girdwood 	}
8208a978234SLiam Girdwood 
8213cde818cSAmadeusz Sławiński 	se->items = le32_to_cpu(ec->items);
822b6e38b29SMousumi Jana 	se->texts = (const char * const *)se->dobj.control.dtexts;
8238a978234SLiam Girdwood 	return 0;
8248a978234SLiam Girdwood 
8258a978234SLiam Girdwood err:
8263cde818cSAmadeusz Sławiński 	return ret;
8273cde818cSAmadeusz Sławiński }
8283cde818cSAmadeusz Sławiński 
soc_tplg_denum_create_values(struct soc_tplg * tplg,struct soc_enum * se,struct snd_soc_tplg_enum_control * ec)829ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
8308a978234SLiam Girdwood 					struct snd_soc_tplg_enum_control *ec)
8318a978234SLiam Girdwood {
8325aebe7c7SPierre-Louis Bossart 	int i;
8335aebe7c7SPierre-Louis Bossart 
834631c78edSAmadeusz Sławiński 	/*
835631c78edSAmadeusz Sławiński 	 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
836631c78edSAmadeusz Sławiński 	 * values instead of using ARRAY_SIZE(ec->values) due to the fact that
837631c78edSAmadeusz Sławiński 	 * it is oversized for its purpose. Additionally it is done so because
838631c78edSAmadeusz Sławiński 	 * it is defined in UAPI header where it can't be easily changed.
839631c78edSAmadeusz Sławiński 	 */
840631c78edSAmadeusz Sławiński 	if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
8418a978234SLiam Girdwood 		return -EINVAL;
8428a978234SLiam Girdwood 
843631c78edSAmadeusz Sławiński 	se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
844543466efSDan Carpenter 					   sizeof(*se->dobj.control.dvalues),
845376c0afeSAndrzej Hajda 					   GFP_KERNEL);
8468a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
8478a978234SLiam Girdwood 		return -ENOMEM;
8488a978234SLiam Girdwood 
8495aebe7c7SPierre-Louis Bossart 	/* convert from little-endian */
8505aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
8515aebe7c7SPierre-Louis Bossart 		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
8525aebe7c7SPierre-Louis Bossart 	}
8535aebe7c7SPierre-Louis Bossart 
854*67786b29SAmadeusz Sławiński 	se->items = le32_to_cpu(ec->items);
855*67786b29SAmadeusz Sławiński 	se->values = (const unsigned int *)se->dobj.control.dvalues;
8568a978234SLiam Girdwood 	return 0;
8578a978234SLiam Girdwood }
8588a978234SLiam Girdwood 
soc_tplg_denum_create(struct soc_tplg * tplg,size_t size)8590db627c4SAmadeusz Sławiński static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
8608a978234SLiam Girdwood {
8618a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
8628a978234SLiam Girdwood 	struct soc_enum *se;
8638a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8640db627c4SAmadeusz Sławiński 	int ret = 0;
8658a978234SLiam Girdwood 
8668a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8678a978234SLiam Girdwood 				      sizeof(struct snd_soc_tplg_enum_control),
8680db627c4SAmadeusz Sławiński 				      1, size, "enums"))
8698a978234SLiam Girdwood 		return -EINVAL;
8708a978234SLiam Girdwood 
8718a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
8728a978234SLiam Girdwood 
8738a978234SLiam Girdwood 	/* validate kcontrol */
8748a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8758a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8768a978234SLiam Girdwood 		return -EINVAL;
8778a978234SLiam Girdwood 
878ff922622SAmadeusz Sławiński 	se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
8798a978234SLiam Girdwood 	if (se == NULL)
8808a978234SLiam Girdwood 		return -ENOMEM;
8818a978234SLiam Girdwood 
88202b64245SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
8835aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(ec->priv.size));
88402b64245SLiam Girdwood 
8858a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
8868a978234SLiam Girdwood 		ec->hdr.name, ec->items);
8878a978234SLiam Girdwood 
8888a978234SLiam Girdwood 	memset(&kc, 0, sizeof(kc));
8898a978234SLiam Girdwood 	kc.name = ec->hdr.name;
8908a978234SLiam Girdwood 	kc.private_value = (long)se;
8918a978234SLiam Girdwood 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8925aebe7c7SPierre-Louis Bossart 	kc.access = le32_to_cpu(ec->hdr.access);
8938a978234SLiam Girdwood 
8948f9974d9SAmadeusz Sławiński 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
8958f9974d9SAmadeusz Sławiński 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
8968a978234SLiam Girdwood 		SNDRV_CHMAP_FL);
8978f9974d9SAmadeusz Sławiński 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
8988a978234SLiam Girdwood 		SNDRV_CHMAP_FL);
8998a978234SLiam Girdwood 
9005aebe7c7SPierre-Louis Bossart 	se->mask = le32_to_cpu(ec->mask);
9018a978234SLiam Girdwood 	se->dobj.index = tplg->index;
9028a978234SLiam Girdwood 	se->dobj.type = SND_SOC_DOBJ_ENUM;
90331e92739SAmadeusz Sławiński 	if (tplg->ops)
90431e92739SAmadeusz Sławiński 		se->dobj.unload = tplg->ops->control_unload;
9058a978234SLiam Girdwood 	INIT_LIST_HEAD(&se->dobj.list);
9068a978234SLiam Girdwood 
9075aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(ec->hdr.ops.info)) {
9088a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9098a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
9100db627c4SAmadeusz Sławiński 		ret = soc_tplg_denum_create_values(tplg, se, ec);
9110db627c4SAmadeusz Sławiński 		if (ret < 0) {
9128a978234SLiam Girdwood 			dev_err(tplg->dev,
9138a978234SLiam Girdwood 				"ASoC: could not create values for %s\n",
9148a978234SLiam Girdwood 				ec->hdr.name);
9150db627c4SAmadeusz Sławiński 			goto err;
9168a978234SLiam Girdwood 		}
917df561f66SGustavo A. R. Silva 		fallthrough;
9188a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
9198a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9208a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9210db627c4SAmadeusz Sławiński 		ret = soc_tplg_denum_create_texts(tplg, se, ec);
9220db627c4SAmadeusz Sławiński 		if (ret < 0) {
9238a978234SLiam Girdwood 			dev_err(tplg->dev,
9248a978234SLiam Girdwood 				"ASoC: could not create texts for %s\n",
9258a978234SLiam Girdwood 				ec->hdr.name);
9260db627c4SAmadeusz Sławiński 			goto err;
9278a978234SLiam Girdwood 		}
9288a978234SLiam Girdwood 		break;
9298a978234SLiam Girdwood 	default:
9300db627c4SAmadeusz Sławiński 		ret = -EINVAL;
9318a978234SLiam Girdwood 		dev_err(tplg->dev,
9328a978234SLiam Girdwood 			"ASoC: invalid enum control type %d for %s\n",
9338a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
9340db627c4SAmadeusz Sławiński 		goto err;
9358a978234SLiam Girdwood 	}
9368a978234SLiam Girdwood 
9378a978234SLiam Girdwood 	/* map io handlers */
9380db627c4SAmadeusz Sławiński 	ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
9390db627c4SAmadeusz Sławiński 	if (ret) {
9408a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
9410db627c4SAmadeusz Sławiński 		goto err;
9428a978234SLiam Girdwood 	}
9438a978234SLiam Girdwood 
9448a978234SLiam Girdwood 	/* pass control to driver for optional further init */
9459e2ee000SAmadeusz Sławiński 	ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
946ec5dffcdSAmadeusz Sławiński 	if (ret < 0)
9470db627c4SAmadeusz Sławiński 		goto err;
9488a978234SLiam Girdwood 
9498a978234SLiam Girdwood 	/* register control here */
9500db627c4SAmadeusz Sławiński 	ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
9512316c11fSAmadeusz Sławiński 	if (ret < 0)
9520db627c4SAmadeusz Sławiński 		goto err;
9538a978234SLiam Girdwood 
9548a978234SLiam Girdwood 	list_add(&se->dobj.list, &tplg->comp->dobj_list);
955952bd937SPierre-Louis Bossart 
9560db627c4SAmadeusz Sławiński err:
9570db627c4SAmadeusz Sławiński 	return ret;
9588a978234SLiam Girdwood }
9598a978234SLiam Girdwood 
soc_tplg_kcontrol_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)9608a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
9618a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
9628a978234SLiam Girdwood {
9632ae548f3SAmadeusz Sławiński 	int ret;
9648a978234SLiam Girdwood 	int i;
9658a978234SLiam Girdwood 
9668a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
9678a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
9688a978234SLiam Girdwood 
9695aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
970ea8f6b29SKuninori Morimoto 		struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
9718a978234SLiam Girdwood 
9725aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
97306eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
97406eb49f7SMengdong Lin 			return -EINVAL;
97506eb49f7SMengdong Lin 		}
97606eb49f7SMengdong Lin 
9775aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
9788a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
9798a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
9808a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
9818a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
9828a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
9838a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
9848a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
9850db627c4SAmadeusz Sławiński 			ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
9868a978234SLiam Girdwood 			break;
9878a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9888a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9898a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9908a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9918a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9920db627c4SAmadeusz Sławiński 			ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
9938a978234SLiam Girdwood 			break;
9948a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
9950db627c4SAmadeusz Sławiński 			ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
9968a978234SLiam Girdwood 			break;
9978a978234SLiam Girdwood 		default:
9988a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
9998a978234SLiam Girdwood 			return -EINVAL;
10008a978234SLiam Girdwood 		}
10012ae548f3SAmadeusz Sławiński 		if (ret < 0) {
10022ae548f3SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid control\n");
10032ae548f3SAmadeusz Sławiński 			return ret;
10042ae548f3SAmadeusz Sławiński 		}
10052ae548f3SAmadeusz Sławiński 
10068a978234SLiam Girdwood 	}
10078a978234SLiam Girdwood 
10088a978234SLiam Girdwood 	return 0;
10098a978234SLiam Girdwood }
10108a978234SLiam Girdwood 
1011503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
soc_tplg_add_route(struct soc_tplg * tplg,struct snd_soc_dapm_route * route)1012503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1013503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1014503e79b7SLiam Girdwood {
1015c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dapm_route_load)
1016503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1017503e79b7SLiam Girdwood 			route);
1018503e79b7SLiam Girdwood 
1019503e79b7SLiam Girdwood 	return 0;
1020503e79b7SLiam Girdwood }
1021503e79b7SLiam Girdwood 
soc_tplg_dapm_graph_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)10228a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
10238a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10248a978234SLiam Girdwood {
10258a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1026bd865c76SAmadeusz Sławiński 	const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
10278a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
1028cc44c749SAmadeusz Sławiński 	struct snd_soc_dapm_route *route;
1029ff922622SAmadeusz Sławiński 	int count, i;
10307df04ea7SRanjani Sridharan 	int ret = 0;
10318a978234SLiam Girdwood 
10325aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
10335aebe7c7SPierre-Louis Bossart 
10348a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10358a978234SLiam Girdwood 				      sizeof(struct snd_soc_tplg_dapm_graph_elem),
10363ce57f22SAmadeusz Sławiński 				      count, le32_to_cpu(hdr->payload_size), "graph"))
10378a978234SLiam Girdwood 		return -EINVAL;
10388a978234SLiam Girdwood 
1039b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1040b75a6511SLiam Girdwood 		hdr->index);
10418a978234SLiam Girdwood 
10427df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
1043cc44c749SAmadeusz Sławiński 		route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1044cc44c749SAmadeusz Sławiński 		if (!route)
10457df04ea7SRanjani Sridharan 			return -ENOMEM;
10468a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
10478a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
10488a978234SLiam Girdwood 
10498a978234SLiam Girdwood 		/* validate routes */
1050bd865c76SAmadeusz Sławiński 		if ((strnlen(elem->source, maxlen) == maxlen) ||
1051bd865c76SAmadeusz Sławiński 		    (strnlen(elem->sink, maxlen) == maxlen) ||
1052bd865c76SAmadeusz Sławiński 		    (strnlen(elem->control, maxlen) == maxlen)) {
10537df04ea7SRanjani Sridharan 			ret = -EINVAL;
10547df04ea7SRanjani Sridharan 			break;
10558a978234SLiam Girdwood 		}
10568a978234SLiam Girdwood 
10577c3e55d8SAmadeusz Sławiński 		route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL);
10587c3e55d8SAmadeusz Sławiński 		route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL);
1059ab5a6208SAmadeusz Sławiński 		if (!route->source || !route->sink) {
1060ab5a6208SAmadeusz Sławiński 			ret = -ENOMEM;
1061ab5a6208SAmadeusz Sławiński 			break;
1062ab5a6208SAmadeusz Sławiński 		}
10637df04ea7SRanjani Sridharan 
1064bd865c76SAmadeusz Sławiński 		if (strnlen(elem->control, maxlen) != 0) {
10657c3e55d8SAmadeusz Sławiński 			route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL);
1066ab5a6208SAmadeusz Sławiński 			if (!route->control) {
1067ab5a6208SAmadeusz Sławiński 				ret = -ENOMEM;
1068ab5a6208SAmadeusz Sławiński 				break;
1069ab5a6208SAmadeusz Sławiński 			}
1070ab5a6208SAmadeusz Sławiński 		}
10717df04ea7SRanjani Sridharan 
10727df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
1073cc44c749SAmadeusz Sławiński 		route->dobj.type = SND_SOC_DOBJ_GRAPH;
107431e92739SAmadeusz Sławiński 		if (tplg->ops)
1075dd184c40SPeter Ujfalusi 			route->dobj.unload = tplg->ops->dapm_route_unload;
1076cc44c749SAmadeusz Sławiński 		route->dobj.index = tplg->index;
1077cc44c749SAmadeusz Sławiński 		list_add(&route->dobj.list, &tplg->comp->dobj_list);
10787df04ea7SRanjani Sridharan 
1079cc44c749SAmadeusz Sławiński 		ret = soc_tplg_add_route(tplg, route);
10806f0307dfSPierre-Louis Bossart 		if (ret < 0) {
10818bf9475fSPierre-Louis Bossart 			dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
10826856e887SAmadeusz Sławiński 			break;
10836f0307dfSPierre-Louis Bossart 		}
10847df04ea7SRanjani Sridharan 
10857df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
1086cc44c749SAmadeusz Sławiński 		snd_soc_dapm_add_routes(dapm, route, 1);
10877df04ea7SRanjani Sridharan 	}
10887df04ea7SRanjani Sridharan 
10897df04ea7SRanjani Sridharan 	return ret;
10908a978234SLiam Girdwood }
10918a978234SLiam Girdwood 
soc_tplg_dapm_widget_dmixer_create(struct soc_tplg * tplg,struct snd_kcontrol_new * kc)1092d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
10938a978234SLiam Girdwood {
10948a978234SLiam Girdwood 	struct soc_mixer_control *sm;
10958a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
1096d29d41e2SJaska Uimonen 	int err;
10978a978234SLiam Girdwood 
10988a978234SLiam Girdwood 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
10998a978234SLiam Girdwood 
11008a978234SLiam Girdwood 	/* validate kcontrol */
11018a978234SLiam Girdwood 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11028a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1103d29d41e2SJaska Uimonen 		return -EINVAL;
11049f90af3aSAmadeusz Sławiński 
1105ff922622SAmadeusz Sławiński 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
1106d29d41e2SJaska Uimonen 	if (!sm)
1107d29d41e2SJaska Uimonen 		return -ENOMEM;
11088a978234SLiam Girdwood 
1109d29d41e2SJaska Uimonen 	tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) +
1110d29d41e2SJaska Uimonen 		le32_to_cpu(mc->priv.size);
111102b64245SLiam Girdwood 
1112d29d41e2SJaska Uimonen 	dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n",
1113d29d41e2SJaska Uimonen 		mc->hdr.name);
11148a978234SLiam Girdwood 
1115d29d41e2SJaska Uimonen 	kc->private_value = (long)sm;
1116d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
1117d29d41e2SJaska Uimonen 	if (!kc->name)
1118d29d41e2SJaska Uimonen 		return -ENOMEM;
1119d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1120d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(mc->hdr.access);
11218a978234SLiam Girdwood 
11228a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
11238f9974d9SAmadeusz Sławiński 	sm->reg = tplg_chan_get_reg(tplg, mc->channel,
11248a978234SLiam Girdwood 				    SNDRV_CHMAP_FL);
11258f9974d9SAmadeusz Sławiński 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel,
11268a978234SLiam Girdwood 				     SNDRV_CHMAP_FR);
11278f9974d9SAmadeusz Sławiński 	sm->shift = tplg_chan_get_shift(tplg, mc->channel,
11288a978234SLiam Girdwood 					SNDRV_CHMAP_FL);
11298f9974d9SAmadeusz Sławiński 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel,
11308a978234SLiam Girdwood 					 SNDRV_CHMAP_FR);
11318a978234SLiam Girdwood 
113272bbeda0SPierre-Louis Bossart 	sm->max = le32_to_cpu(mc->max);
113372bbeda0SPierre-Louis Bossart 	sm->min = le32_to_cpu(mc->min);
113472bbeda0SPierre-Louis Bossart 	sm->invert = le32_to_cpu(mc->invert);
113572bbeda0SPierre-Louis Bossart 	sm->platform_max = le32_to_cpu(mc->platform_max);
11368a978234SLiam Girdwood 	sm->dobj.index = tplg->index;
11378a978234SLiam Girdwood 	INIT_LIST_HEAD(&sm->dobj.list);
11388a978234SLiam Girdwood 
11398a978234SLiam Girdwood 	/* map io handlers */
1140d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
11418a978234SLiam Girdwood 	if (err) {
11428a978234SLiam Girdwood 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1143d29d41e2SJaska Uimonen 		return err;
11448a978234SLiam Girdwood 	}
11458a978234SLiam Girdwood 
11463789debfSBard liao 	/* create any TLV data */
1147d29d41e2SJaska Uimonen 	err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
1148482db55aSAmadeusz Sławiński 	if (err < 0) {
1149482db55aSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1150482db55aSAmadeusz Sławiński 			mc->hdr.name);
1151d29d41e2SJaska Uimonen 		return err;
1152482db55aSAmadeusz Sławiński 	}
11533789debfSBard liao 
11548a978234SLiam Girdwood 	/* pass control to driver for optional further init */
11559e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &mc->hdr);
1156ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1157d29d41e2SJaska Uimonen 		return err;
11588a978234SLiam Girdwood 
1159d29d41e2SJaska Uimonen 	return 0;
1160d29d41e2SJaska Uimonen }
1161d29d41e2SJaska Uimonen 
soc_tplg_dapm_widget_denum_create(struct soc_tplg * tplg,struct snd_kcontrol_new * kc)1162d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
11638a978234SLiam Girdwood {
11648a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
11658a978234SLiam Girdwood 	struct soc_enum *se;
1166d29d41e2SJaska Uimonen 	int err;
11678a978234SLiam Girdwood 
11688a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
11698a978234SLiam Girdwood 	/* validate kcontrol */
11708a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11718a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1172d29d41e2SJaska Uimonen 		return -EINVAL;
11738a978234SLiam Girdwood 
1174ff922622SAmadeusz Sławiński 	se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
1175d29d41e2SJaska Uimonen 	if (!se)
1176d29d41e2SJaska Uimonen 		return -ENOMEM;
11778a978234SLiam Girdwood 
117802b64245SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
117972bbeda0SPierre-Louis Bossart 		      le32_to_cpu(ec->priv.size));
118002b64245SLiam Girdwood 
11818a978234SLiam Girdwood 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
11828a978234SLiam Girdwood 		ec->hdr.name);
11838a978234SLiam Girdwood 
1184d29d41e2SJaska Uimonen 	kc->private_value = (long)se;
1185d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
1186d29d41e2SJaska Uimonen 	if (!kc->name)
1187d29d41e2SJaska Uimonen 		return -ENOMEM;
1188d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1189d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(ec->hdr.access);
11908a978234SLiam Girdwood 
11918a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
11928f9974d9SAmadeusz Sławiński 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
11938f9974d9SAmadeusz Sławiński 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
11941a7dd6e2SMengdong Lin 					  SNDRV_CHMAP_FL);
11958f9974d9SAmadeusz Sławiński 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
11961a7dd6e2SMengdong Lin 					  SNDRV_CHMAP_FR);
11978a978234SLiam Girdwood 
119872bbeda0SPierre-Louis Bossart 	se->items = le32_to_cpu(ec->items);
119972bbeda0SPierre-Louis Bossart 	se->mask = le32_to_cpu(ec->mask);
12008a978234SLiam Girdwood 	se->dobj.index = tplg->index;
12018a978234SLiam Girdwood 
12025aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(ec->hdr.ops.info)) {
12038a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
12048a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1205ff922622SAmadeusz Sławiński 		err = soc_tplg_denum_create_values(tplg, se, ec);
12068a978234SLiam Girdwood 		if (err < 0) {
12078a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
12088a978234SLiam Girdwood 				ec->hdr.name);
1209d29d41e2SJaska Uimonen 			return err;
12108a978234SLiam Girdwood 		}
1211df561f66SGustavo A. R. Silva 		fallthrough;
12128a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
12138a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
12148a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1215ff922622SAmadeusz Sławiński 		err = soc_tplg_denum_create_texts(tplg, se, ec);
12168a978234SLiam Girdwood 		if (err < 0) {
12178a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
12188a978234SLiam Girdwood 				ec->hdr.name);
1219d29d41e2SJaska Uimonen 			return err;
12208a978234SLiam Girdwood 		}
12218a978234SLiam Girdwood 		break;
12228a978234SLiam Girdwood 	default:
12238a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
12248a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
1225d29d41e2SJaska Uimonen 		return -EINVAL;
12268a978234SLiam Girdwood 	}
12278a978234SLiam Girdwood 
12288a978234SLiam Girdwood 	/* map io handlers */
1229d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
12308a978234SLiam Girdwood 	if (err) {
12318a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1232d29d41e2SJaska Uimonen 		return err;
12338a978234SLiam Girdwood 	}
12348a978234SLiam Girdwood 
12358a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12369e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &ec->hdr);
1237ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1238d29d41e2SJaska Uimonen 		return err;
12391a7dd6e2SMengdong Lin 
1240d29d41e2SJaska Uimonen 	return 0;
12418a978234SLiam Girdwood }
12428a978234SLiam Girdwood 
soc_tplg_dapm_widget_dbytes_create(struct soc_tplg * tplg,struct snd_kcontrol_new * kc)1243d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
12448a978234SLiam Girdwood {
12458a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
12468a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
1247d29d41e2SJaska Uimonen 	int err;
12488a978234SLiam Girdwood 
12498a978234SLiam Girdwood 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
12508a978234SLiam Girdwood 
12518a978234SLiam Girdwood 	/* validate kcontrol */
12528a978234SLiam Girdwood 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12538a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1254d29d41e2SJaska Uimonen 		return -EINVAL;
12558a978234SLiam Girdwood 
1256ff922622SAmadeusz Sławiński 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
1257d29d41e2SJaska Uimonen 	if (!sbe)
1258d29d41e2SJaska Uimonen 		return -ENOMEM;
12598a978234SLiam Girdwood 
12608a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
12615aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(be->priv.size));
12628a978234SLiam Girdwood 
12638a978234SLiam Girdwood 	dev_dbg(tplg->dev,
12648a978234SLiam Girdwood 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
12658a978234SLiam Girdwood 		be->hdr.name, be->hdr.access);
12668a978234SLiam Girdwood 
1267d29d41e2SJaska Uimonen 	kc->private_value = (long)sbe;
1268d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
1269d29d41e2SJaska Uimonen 	if (!kc->name)
1270d29d41e2SJaska Uimonen 		return -ENOMEM;
1271d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1272d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(be->hdr.access);
12738a978234SLiam Girdwood 
127472bbeda0SPierre-Louis Bossart 	sbe->max = le32_to_cpu(be->max);
12758a978234SLiam Girdwood 	INIT_LIST_HEAD(&sbe->dobj.list);
12768a978234SLiam Girdwood 
12778a978234SLiam Girdwood 	/* map standard io handlers and check for external handlers */
1278d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
12798a978234SLiam Girdwood 	if (err) {
12808a978234SLiam Girdwood 		soc_control_err(tplg, &be->hdr, be->hdr.name);
1281d29d41e2SJaska Uimonen 		return err;
12828a978234SLiam Girdwood 	}
12838a978234SLiam Girdwood 
12848a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12859e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &be->hdr);
1286ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1287d29d41e2SJaska Uimonen 		return err;
12888a978234SLiam Girdwood 
1289d29d41e2SJaska Uimonen 	return 0;
12908a978234SLiam Girdwood }
12918a978234SLiam Girdwood 
soc_tplg_dapm_widget_create(struct soc_tplg * tplg,struct snd_soc_tplg_dapm_widget * w)12928a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
12938a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
12948a978234SLiam Girdwood {
12958a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
12968a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
12978a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
12988a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1299b9c035aaSJaska Uimonen 	unsigned int *kcontrol_type = NULL;
1300d29d41e2SJaska Uimonen 	struct snd_kcontrol_new *kc;
1301d29d41e2SJaska Uimonen 	int mixer_count = 0;
1302d29d41e2SJaska Uimonen 	int bytes_count = 0;
1303d29d41e2SJaska Uimonen 	int enum_count = 0;
13048a978234SLiam Girdwood 	int ret = 0;
1305d29d41e2SJaska Uimonen 	int i;
13068a978234SLiam Girdwood 
13078a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13088a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13098a978234SLiam Girdwood 		return -EINVAL;
13108a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13118a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13128a978234SLiam Girdwood 		return -EINVAL;
13138a978234SLiam Girdwood 
13148a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
13158a978234SLiam Girdwood 		w->name, w->id);
13168a978234SLiam Girdwood 
13178a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
13188a978234SLiam Girdwood 
13198a978234SLiam Girdwood 	/* map user to kernel widget ID */
13205aebe7c7SPierre-Louis Bossart 	template.id = get_widget_id(le32_to_cpu(w->id));
1321752c938aSDan Carpenter 	if ((int)template.id < 0)
13228a978234SLiam Girdwood 		return template.id;
13238a978234SLiam Girdwood 
1324c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
13258a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
13268a978234SLiam Girdwood 	if (!template.name)
13278a978234SLiam Girdwood 		return -ENOMEM;
13288a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
13298a978234SLiam Girdwood 	if (!template.sname) {
13308a978234SLiam Girdwood 		ret = -ENOMEM;
13318a978234SLiam Girdwood 		goto err;
13328a978234SLiam Girdwood 	}
13335aebe7c7SPierre-Louis Bossart 	template.reg = le32_to_cpu(w->reg);
13345aebe7c7SPierre-Louis Bossart 	template.shift = le32_to_cpu(w->shift);
13355aebe7c7SPierre-Louis Bossart 	template.mask = le32_to_cpu(w->mask);
13365aebe7c7SPierre-Louis Bossart 	template.subseq = le32_to_cpu(w->subseq);
13378a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
13388a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
13395aebe7c7SPierre-Louis Bossart 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
13405aebe7c7SPierre-Louis Bossart 	template.event_flags = le16_to_cpu(w->event_flags);
13418a978234SLiam Girdwood 	template.dobj.index = tplg->index;
13428a978234SLiam Girdwood 
13438a978234SLiam Girdwood 	tplg->pos +=
13445aebe7c7SPierre-Louis Bossart 		(sizeof(struct snd_soc_tplg_dapm_widget) +
13455aebe7c7SPierre-Louis Bossart 		 le32_to_cpu(w->priv.size));
13465aebe7c7SPierre-Louis Bossart 
13478a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
13488a978234SLiam Girdwood 		template.num_kcontrols = 0;
13498a978234SLiam Girdwood 		goto widget;
13508a978234SLiam Girdwood 	}
13518a978234SLiam Girdwood 
1352d29d41e2SJaska Uimonen 	template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1353d29d41e2SJaska Uimonen 	kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1354c173ee5bSAmadeusz Sławiński 	if (!kc) {
1355c173ee5bSAmadeusz Sławiński 		ret = -ENOMEM;
13569c363532SPeter Ujfalusi 		goto hdr_err;
1357c173ee5bSAmadeusz Sławiński 	}
1358d29d41e2SJaska Uimonen 
1359d29d41e2SJaska Uimonen 	kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1360d29d41e2SJaska Uimonen 				     GFP_KERNEL);
1361c173ee5bSAmadeusz Sławiński 	if (!kcontrol_type) {
1362c173ee5bSAmadeusz Sławiński 		ret = -ENOMEM;
13639c363532SPeter Ujfalusi 		goto hdr_err;
1364c173ee5bSAmadeusz Sławiński 	}
1365d29d41e2SJaska Uimonen 
13661baad7daSPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1367d29d41e2SJaska Uimonen 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
13685aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
13698a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
13708a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
13718a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
13728a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
13738a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
13748a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1375d29d41e2SJaska Uimonen 			/* volume mixer */
1376d29d41e2SJaska Uimonen 			kc[i].index = mixer_count;
1377d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1378d29d41e2SJaska Uimonen 			mixer_count++;
1379d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]);
1380d29d41e2SJaska Uimonen 			if (ret < 0)
13818a978234SLiam Girdwood 				goto hdr_err;
13828a978234SLiam Girdwood 			break;
13838a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
13848a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
13858a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
13868a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
13878a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1388d29d41e2SJaska Uimonen 			/* enumerated mixer */
1389d29d41e2SJaska Uimonen 			kc[i].index = enum_count;
1390d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1391d29d41e2SJaska Uimonen 			enum_count++;
1392d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]);
1393d29d41e2SJaska Uimonen 			if (ret < 0)
13948a978234SLiam Girdwood 				goto hdr_err;
13958a978234SLiam Girdwood 			break;
13968a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
1397d29d41e2SJaska Uimonen 			/* bytes control */
1398d29d41e2SJaska Uimonen 			kc[i].index = bytes_count;
1399d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1400d29d41e2SJaska Uimonen 			bytes_count++;
1401d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]);
1402d29d41e2SJaska Uimonen 			if (ret < 0)
14038a978234SLiam Girdwood 				goto hdr_err;
14048a978234SLiam Girdwood 			break;
14058a978234SLiam Girdwood 		default:
14068a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
14078a978234SLiam Girdwood 				control_hdr->ops.get, control_hdr->ops.put,
14085aebe7c7SPierre-Louis Bossart 				le32_to_cpu(control_hdr->ops.info));
14098a978234SLiam Girdwood 			ret = -EINVAL;
14108a978234SLiam Girdwood 			goto hdr_err;
14118a978234SLiam Girdwood 		}
1412d29d41e2SJaska Uimonen 	}
1413d29d41e2SJaska Uimonen 
1414d29d41e2SJaska Uimonen 	template.kcontrol_news = kc;
14158facf84bSPeter Ujfalusi 	dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
14168facf84bSPeter Ujfalusi 		w->name, mixer_count, enum_count, bytes_count);
14178a978234SLiam Girdwood 
14188a978234SLiam Girdwood widget:
14198a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
14208a978234SLiam Girdwood 	if (ret < 0)
14218a978234SLiam Girdwood 		goto hdr_err;
14228a978234SLiam Girdwood 
14238a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
14248a978234SLiam Girdwood 	 * data during sound card init. */
14252b34c135SKuninori Morimoto 	if (snd_soc_card_is_instantiated(card))
14268a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
14278a978234SLiam Girdwood 	else
14288a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
142937e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
143037e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
14318a978234SLiam Girdwood 		goto hdr_err;
14328a978234SLiam Girdwood 	}
14338a978234SLiam Girdwood 
14348a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1435eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
143631e92739SAmadeusz Sławiński 	if (tplg->ops)
143731e92739SAmadeusz Sławiński 		widget->dobj.unload = tplg->ops->widget_unload;
14388a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
14398a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1440ebd259d3SLiam Girdwood 
1441ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1442ebd259d3SLiam Girdwood 	if (ret < 0)
1443ebd259d3SLiam Girdwood 		goto ready_err;
1444ebd259d3SLiam Girdwood 
14457620fe91SBard liao 	kfree(template.sname);
14467620fe91SBard liao 	kfree(template.name);
14477620fe91SBard liao 
14488a978234SLiam Girdwood 	return 0;
14498a978234SLiam Girdwood 
1450ebd259d3SLiam Girdwood ready_err:
14512abfd4bdSAmadeusz Sławiński 	soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1452ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
14538a978234SLiam Girdwood hdr_err:
14548a978234SLiam Girdwood 	kfree(template.sname);
14558a978234SLiam Girdwood err:
14568a978234SLiam Girdwood 	kfree(template.name);
14578a978234SLiam Girdwood 	return ret;
14588a978234SLiam Girdwood }
14598a978234SLiam Girdwood 
soc_tplg_dapm_widget_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)14608a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
14618a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
14628a978234SLiam Girdwood {
1463e9aa139fSKuninori Morimoto 	int count, i;
14645aebe7c7SPierre-Louis Bossart 
14655aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
14668a978234SLiam Girdwood 
14678a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
14688a978234SLiam Girdwood 
14698a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
1470e9aa139fSKuninori Morimoto 		struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1471e9aa139fSKuninori Morimoto 		int ret;
1472e9aa139fSKuninori Morimoto 
14732e288333SAmadeusz Sławiński 		/*
14742e288333SAmadeusz Sławiński 		 * check if widget itself fits within topology file
14752e288333SAmadeusz Sławiński 		 * use sizeof instead of widget->size, as we can't be sure
14762e288333SAmadeusz Sławiński 		 * it is set properly yet (file may end before it is present)
14772e288333SAmadeusz Sławiński 		 */
14782e288333SAmadeusz Sławiński 		if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
14792e288333SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid widget data size\n");
14802e288333SAmadeusz Sławiński 			return -EINVAL;
14812e288333SAmadeusz Sławiński 		}
14822e288333SAmadeusz Sławiński 
14832e288333SAmadeusz Sławiński 		/* check if widget has proper size */
14845aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
148506eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
148606eb49f7SMengdong Lin 			return -EINVAL;
148706eb49f7SMengdong Lin 		}
148806eb49f7SMengdong Lin 
14892e288333SAmadeusz Sławiński 		/* check if widget private data fits within topology file */
14902e288333SAmadeusz Sławiński 		if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
14912e288333SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
14922e288333SAmadeusz Sławiński 			return -EINVAL;
14932e288333SAmadeusz Sławiński 		}
14942e288333SAmadeusz Sławiński 
14958a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
14967de76b62SMengdong Lin 		if (ret < 0) {
14978a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
14988a978234SLiam Girdwood 				widget->name);
14997de76b62SMengdong Lin 			return ret;
15007de76b62SMengdong Lin 		}
15018a978234SLiam Girdwood 	}
15028a978234SLiam Girdwood 
15038a978234SLiam Girdwood 	return 0;
15048a978234SLiam Girdwood }
15058a978234SLiam Girdwood 
soc_tplg_dapm_complete(struct soc_tplg * tplg)15068a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
15078a978234SLiam Girdwood {
15088a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
15098a978234SLiam Girdwood 	int ret;
15108a978234SLiam Girdwood 
15118a978234SLiam Girdwood 	/* Card might not have been registered at this point.
15128a978234SLiam Girdwood 	 * If so, just return success.
15138a978234SLiam Girdwood 	*/
15142b34c135SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card)) {
151553085402SAmadeusz Sławiński 		dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
15168a978234SLiam Girdwood 		return 0;
15178a978234SLiam Girdwood 	}
15188a978234SLiam Girdwood 
15198a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
15208a978234SLiam Girdwood 	if (ret < 0)
152153085402SAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
15228a978234SLiam Girdwood 
1523b784617aSAmadeusz Sławiński 	return ret;
15248a978234SLiam Girdwood }
15258a978234SLiam Girdwood 
set_stream_info(struct soc_tplg * tplg,struct snd_soc_pcm_stream * stream,struct snd_soc_tplg_stream_caps * caps)1526ff922622SAmadeusz Sławiński static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1527b6b6e4d6SMengdong Lin 			   struct snd_soc_tplg_stream_caps *caps)
1528b6b6e4d6SMengdong Lin {
1529ff922622SAmadeusz Sławiński 	stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1530abc3caacSAmadeusz Sławiński 	if (!stream->stream_name)
1531abc3caacSAmadeusz Sławiński 		return -ENOMEM;
1532abc3caacSAmadeusz Sławiński 
15335aebe7c7SPierre-Louis Bossart 	stream->channels_min = le32_to_cpu(caps->channels_min);
15345aebe7c7SPierre-Louis Bossart 	stream->channels_max = le32_to_cpu(caps->channels_max);
15355aebe7c7SPierre-Louis Bossart 	stream->rates = le32_to_cpu(caps->rates);
15365aebe7c7SPierre-Louis Bossart 	stream->rate_min = le32_to_cpu(caps->rate_min);
15375aebe7c7SPierre-Louis Bossart 	stream->rate_max = le32_to_cpu(caps->rate_max);
15385aebe7c7SPierre-Louis Bossart 	stream->formats = le64_to_cpu(caps->formats);
15395aebe7c7SPierre-Louis Bossart 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1540abc3caacSAmadeusz Sławiński 
1541abc3caacSAmadeusz Sławiński 	return 0;
1542b6b6e4d6SMengdong Lin }
1543b6b6e4d6SMengdong Lin 
set_dai_flags(struct snd_soc_dai_driver * dai_drv,unsigned int flag_mask,unsigned int flags)15440038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
15450038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
15460038be9aSMengdong Lin {
15470038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1548f14654ddSKuninori Morimoto 		dai_drv->symmetric_rate =
154947108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
15500038be9aSMengdong Lin 
15510038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
15520038be9aSMengdong Lin 		dai_drv->symmetric_channels =
155347108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
15540038be9aSMengdong Lin 			1 : 0;
15550038be9aSMengdong Lin 
15560038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1557f14654ddSKuninori Morimoto 		dai_drv->symmetric_sample_bits =
155847108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
15590038be9aSMengdong Lin 			1 : 0;
15600038be9aSMengdong Lin }
15610038be9aSMengdong Lin 
156280585b0cSKuninori Morimoto static const struct snd_soc_dai_ops tplg_dai_ops = {
156380585b0cSKuninori Morimoto 	.compress_new	= snd_soc_new_compress,
156480585b0cSKuninori Morimoto };
156580585b0cSKuninori Morimoto 
soc_tplg_dai_create(struct soc_tplg * tplg,struct snd_soc_tplg_pcm * pcm)156664527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
156764527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
156864527e8aSMengdong Lin {
156964527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
157064527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
157164527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
1572e443c205SKuninori Morimoto 	struct snd_soc_dai *dai;
1573e443c205SKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1574e443c205SKuninori Morimoto 		snd_soc_component_get_dapm(tplg->comp);
157564527e8aSMengdong Lin 	int ret;
157664527e8aSMengdong Lin 
1577ff922622SAmadeusz Sławiński 	dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
157864527e8aSMengdong Lin 	if (dai_drv == NULL)
157964527e8aSMengdong Lin 		return -ENOMEM;
158064527e8aSMengdong Lin 
1581abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
1582ff922622SAmadeusz Sławiński 		dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1583abc3caacSAmadeusz Sławiński 		if (!dai_drv->name) {
1584abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1585abc3caacSAmadeusz Sławiński 			goto err;
1586abc3caacSAmadeusz Sławiński 		}
1587abc3caacSAmadeusz Sławiński 	}
15885aebe7c7SPierre-Louis Bossart 	dai_drv->id = le32_to_cpu(pcm->dai_id);
158964527e8aSMengdong Lin 
159064527e8aSMengdong Lin 	if (pcm->playback) {
159164527e8aSMengdong Lin 		stream = &dai_drv->playback;
159264527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1593ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
1594abc3caacSAmadeusz Sławiński 		if (ret < 0)
1595abc3caacSAmadeusz Sławiński 			goto err;
159664527e8aSMengdong Lin 	}
159764527e8aSMengdong Lin 
159864527e8aSMengdong Lin 	if (pcm->capture) {
159964527e8aSMengdong Lin 		stream = &dai_drv->capture;
160064527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1601ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
1602abc3caacSAmadeusz Sławiński 		if (ret < 0)
1603abc3caacSAmadeusz Sławiński 			goto err;
160464527e8aSMengdong Lin 	}
160564527e8aSMengdong Lin 
16065db6aab6SLiam Girdwood 	if (pcm->compress)
160780585b0cSKuninori Morimoto 		dai_drv->ops = &tplg_dai_ops;
16085db6aab6SLiam Girdwood 
160964527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1610c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
161164527e8aSMengdong Lin 	if (ret < 0) {
1612e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1613abc3caacSAmadeusz Sławiński 		goto err;
161464527e8aSMengdong Lin 	}
161564527e8aSMengdong Lin 
161664527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
161764527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
161831e92739SAmadeusz Sławiński 	if (tplg->ops)
161931e92739SAmadeusz Sławiński 		dai_drv->dobj.unload = tplg->ops->dai_unload;
162064527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
162164527e8aSMengdong Lin 
162264527e8aSMengdong Lin 	/* register the DAI to the component */
1623fc4cb1e1SAmadeusz Sławiński 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1624e443c205SKuninori Morimoto 	if (!dai)
1625e443c205SKuninori Morimoto 		return -ENOMEM;
1626e443c205SKuninori Morimoto 
1627e443c205SKuninori Morimoto 	/* Create the DAI widgets here */
1628e443c205SKuninori Morimoto 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1629e443c205SKuninori Morimoto 	if (ret != 0) {
1630e443c205SKuninori Morimoto 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1631fc4cb1e1SAmadeusz Sławiński 		snd_soc_unregister_dai(dai);
1632e443c205SKuninori Morimoto 		return ret;
1633e443c205SKuninori Morimoto 	}
1634e443c205SKuninori Morimoto 
1635abc3caacSAmadeusz Sławiński 	return 0;
1636abc3caacSAmadeusz Sławiński 
1637abc3caacSAmadeusz Sławiński err:
1638e443c205SKuninori Morimoto 	return ret;
163964527e8aSMengdong Lin }
164064527e8aSMengdong Lin 
set_link_flags(struct snd_soc_dai_link * link,unsigned int flag_mask,unsigned int flags)1641717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1642717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1643717a8e72SMengdong Lin {
1644717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1645f14654ddSKuninori Morimoto 		link->symmetric_rate =
164647108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1647717a8e72SMengdong Lin 
1648717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1649717a8e72SMengdong Lin 		link->symmetric_channels =
165047108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1651717a8e72SMengdong Lin 			1 : 0;
1652717a8e72SMengdong Lin 
1653717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1654f14654ddSKuninori Morimoto 		link->symmetric_sample_bits =
165547108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1656717a8e72SMengdong Lin 			1 : 0;
16576ff67ccaSMengdong Lin 
16586ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
16596ff67ccaSMengdong Lin 		link->ignore_suspend =
166047108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
16616ff67ccaSMengdong Lin 			1 : 0;
1662717a8e72SMengdong Lin }
1663717a8e72SMengdong Lin 
166467d1c21eSGuneshwor Singh /* create the FE DAI link */
soc_tplg_fe_link_create(struct soc_tplg * tplg,struct snd_soc_tplg_pcm * pcm)1665ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1666acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1667acfc7d46SMengdong Lin {
1668acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
166923b946ceSKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
1670acfc7d46SMengdong Lin 	int ret;
1671acfc7d46SMengdong Lin 
16726a7c51b4SKuninori Morimoto 	/* link + cpu + codec + platform */
16736a7c51b4SKuninori Morimoto 	link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1674acfc7d46SMengdong Lin 	if (link == NULL)
1675acfc7d46SMengdong Lin 		return -ENOMEM;
1676acfc7d46SMengdong Lin 
167723b946ceSKuninori Morimoto 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
167823b946ceSKuninori Morimoto 
167923b946ceSKuninori Morimoto 	link->cpus	= &dlc[0];
168023b946ceSKuninori Morimoto 	link->num_cpus	 = 1;
168123b946ceSKuninori Morimoto 
16828ce1cbd6SJaroslav Kysela 	link->dobj.index = tplg->index;
16838ce1cbd6SJaroslav Kysela 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
168431e92739SAmadeusz Sławiński 	if (tplg->ops)
168531e92739SAmadeusz Sławiński 		link->dobj.unload = tplg->ops->link_unload;
16868ce1cbd6SJaroslav Kysela 
16878f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
1688ff922622SAmadeusz Sławiński 		link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1689ff922622SAmadeusz Sławiński 		link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1690abc3caacSAmadeusz Sławiński 		if (!link->name || !link->stream_name) {
1691abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1692abc3caacSAmadeusz Sławiński 			goto err;
1693abc3caacSAmadeusz Sławiński 		}
16948f27c4abSMengdong Lin 	}
16955aebe7c7SPierre-Louis Bossart 	link->id = le32_to_cpu(pcm->pcm_id);
1696acfc7d46SMengdong Lin 
1697abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
1698ff922622SAmadeusz Sławiński 		link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1699abc3caacSAmadeusz Sławiński 		if (!link->cpus->dai_name) {
1700abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1701abc3caacSAmadeusz Sławiński 			goto err;
1702abc3caacSAmadeusz Sławiński 		}
1703abc3caacSAmadeusz Sławiński 	}
17048f27c4abSMengdong Lin 
17055a7bec81SKuninori Morimoto 	/*
17065a7bec81SKuninori Morimoto 	 * Many topology are assuming link has Codec / Platform, and
17075a7bec81SKuninori Morimoto 	 * these might be overwritten at soc_tplg_dai_link_load().
17085a7bec81SKuninori Morimoto 	 * Don't use &asoc_dummy_dlc here.
17095a7bec81SKuninori Morimoto 	 */
17105a7bec81SKuninori Morimoto 	link->codecs		= &dlc[1];	/* Don't use &asoc_dummy_dlc here */
171123b946ceSKuninori Morimoto 	link->codecs->name	= "snd-soc-dummy";
171223b946ceSKuninori Morimoto 	link->codecs->dai_name	= "snd-soc-dummy-dai";
17135a7bec81SKuninori Morimoto 	link->num_codecs	= 1;
171467d1c21eSGuneshwor Singh 
17155a7bec81SKuninori Morimoto 	link->platforms		= &dlc[2];	/* Don't use &asoc_dummy_dlc here */
17166a7c51b4SKuninori Morimoto 	link->platforms->name	= "snd-soc-dummy";
17176a7c51b4SKuninori Morimoto 	link->num_platforms	= 1;
17186a7c51b4SKuninori Morimoto 
171967d1c21eSGuneshwor Singh 	/* enable DPCM */
172067d1c21eSGuneshwor Singh 	link->dynamic = 1;
1721c403dcd8SKuninori Morimoto 	link->ignore_pmdown_time = 1;
17225aebe7c7SPierre-Louis Bossart 	link->dpcm_playback = le32_to_cpu(pcm->playback);
17235aebe7c7SPierre-Louis Bossart 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1724717a8e72SMengdong Lin 	if (pcm->flag_mask)
17255aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
17265aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flag_mask),
17275aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flags));
172867d1c21eSGuneshwor Singh 
1729acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1730c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1731acfc7d46SMengdong Lin 	if (ret < 0) {
1732e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: FE link loading failed\n");
173376d27036SDragos Tarcatu 		goto err;
173476d27036SDragos Tarcatu 	}
173576d27036SDragos Tarcatu 
1736ffaf886eSKuninori Morimoto 	ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
173776d27036SDragos Tarcatu 	if (ret < 0) {
1738b6c3bddaSJohan Hovold 		if (ret != -EPROBE_DEFER)
1739e59db12bSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: adding FE link failed\n");
174076d27036SDragos Tarcatu 		goto err;
1741acfc7d46SMengdong Lin 	}
1742acfc7d46SMengdong Lin 
1743acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1744acfc7d46SMengdong Lin 
1745acfc7d46SMengdong Lin 	return 0;
174676d27036SDragos Tarcatu err:
174776d27036SDragos Tarcatu 	return ret;
1748acfc7d46SMengdong Lin }
1749acfc7d46SMengdong Lin 
1750acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
soc_tplg_pcm_create(struct soc_tplg * tplg,struct snd_soc_tplg_pcm * pcm)175164527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
175264527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
175364527e8aSMengdong Lin {
1754acfc7d46SMengdong Lin 	int ret;
1755acfc7d46SMengdong Lin 
1756acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1757acfc7d46SMengdong Lin 	if (ret < 0)
1758acfc7d46SMengdong Lin 		return ret;
1759acfc7d46SMengdong Lin 
1760ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
176164527e8aSMengdong Lin }
176264527e8aSMengdong Lin 
176355726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
stream_caps_new_ver(struct snd_soc_tplg_stream_caps * dest,struct snd_soc_tplg_stream_caps_v4 * src)176455726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
176555726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
176655726dc9SMengdong Lin {
17675aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
176855726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
176955726dc9SMengdong Lin 	dest->formats = src->formats;
177055726dc9SMengdong Lin 	dest->rates = src->rates;
177155726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
177255726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
177355726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
177455726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
177555726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
177655726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
177755726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
177855726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
177955726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
178055726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
178155726dc9SMengdong Lin }
178255726dc9SMengdong Lin 
178355726dc9SMengdong Lin /**
178455726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
178555726dc9SMengdong Lin  * @tplg: topology context
178655726dc9SMengdong Lin  * @src: older version of pcm as a source
178755726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
178855726dc9SMengdong Lin  *
1789ce1f2571SColin Ian King  * Support from version 4. User should free the returned pcm manually.
179055726dc9SMengdong Lin  */
pcm_new_ver(struct soc_tplg * tplg,struct snd_soc_tplg_pcm * src,struct snd_soc_tplg_pcm ** pcm)179155726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
179255726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
179355726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
179455726dc9SMengdong Lin {
179555726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
179655726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
179755726dc9SMengdong Lin 	int i;
179855726dc9SMengdong Lin 
179955726dc9SMengdong Lin 	*pcm = NULL;
180055726dc9SMengdong Lin 
18015aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
180255726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
180355726dc9SMengdong Lin 		return -EINVAL;
180455726dc9SMengdong Lin 	}
180555726dc9SMengdong Lin 
180655726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
180755726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
180855726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
180955726dc9SMengdong Lin 	if (!dest)
181055726dc9SMengdong Lin 		return -ENOMEM;
181155726dc9SMengdong Lin 
18125aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
181355726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
181455726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
181555726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
181655726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
181755726dc9SMengdong Lin 	dest->playback = src_v4->playback;
181855726dc9SMengdong Lin 	dest->capture = src_v4->capture;
181955726dc9SMengdong Lin 	dest->compress = src_v4->compress;
182055726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
18215aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
182255726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
182355726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
182455726dc9SMengdong Lin 
182555726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
182655726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
182755726dc9SMengdong Lin 
182855726dc9SMengdong Lin 	*pcm = dest;
182955726dc9SMengdong Lin 	return 0;
183055726dc9SMengdong Lin }
183155726dc9SMengdong Lin 
soc_tplg_pcm_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)183264527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
18338a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
18348a978234SLiam Girdwood {
183555726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
18365aebe7c7SPierre-Louis Bossart 	int count;
18375aebe7c7SPierre-Louis Bossart 	int size;
1838fd340455SVinod Koul 	int i;
183955726dc9SMengdong Lin 	bool abi_match;
1840a3039aefSDragos Tarcatu 	int ret;
18418a978234SLiam Girdwood 
18425aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
18435aebe7c7SPierre-Louis Bossart 
184455726dc9SMengdong Lin 	/* check the element size and count */
184555726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
18465aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(pcm->size);
18475aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_pcm)
18485aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
184955726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
18505aebe7c7SPierre-Louis Bossart 			size);
185155726dc9SMengdong Lin 		return -EINVAL;
185255726dc9SMengdong Lin 	}
185355726dc9SMengdong Lin 
18548a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
18555aebe7c7SPierre-Louis Bossart 				      size, count,
18565aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
18573ce57f22SAmadeusz Sławiński 				      "PCM DAI"))
18588a978234SLiam Girdwood 		return -EINVAL;
18598a978234SLiam Girdwood 
186064527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
186155726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
18625aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(pcm->size);
186355726dc9SMengdong Lin 
186455726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
186555726dc9SMengdong Lin 		 * if abi not match.
186655726dc9SMengdong Lin 		 */
18675aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*pcm)) {
186855726dc9SMengdong Lin 			abi_match = true;
186955726dc9SMengdong Lin 			_pcm = pcm;
187055726dc9SMengdong Lin 		} else {
187155726dc9SMengdong Lin 			abi_match = false;
1872b3677fc3SAmadeusz Sławiński 			ret = pcm_new_ver(tplg, pcm, &_pcm);
1873b3677fc3SAmadeusz Sławiński 			if (ret < 0)
1874b3677fc3SAmadeusz Sławiński 				return ret;
187506eb49f7SMengdong Lin 		}
187606eb49f7SMengdong Lin 
187755726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
1878a3039aefSDragos Tarcatu 		ret = soc_tplg_pcm_create(tplg, _pcm);
1879a3039aefSDragos Tarcatu 		if (ret < 0) {
1880a3039aefSDragos Tarcatu 			if (!abi_match)
1881a3039aefSDragos Tarcatu 				kfree(_pcm);
1882a3039aefSDragos Tarcatu 			return ret;
1883a3039aefSDragos Tarcatu 		}
188455726dc9SMengdong Lin 
1885717a8e72SMengdong Lin 		/* offset by version-specific struct size and
1886717a8e72SMengdong Lin 		 * real priv data size
1887717a8e72SMengdong Lin 		 */
18885aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
1889717a8e72SMengdong Lin 
189055726dc9SMengdong Lin 		if (!abi_match)
189155726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
189264527e8aSMengdong Lin 	}
189364527e8aSMengdong Lin 
18948a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
18958a978234SLiam Girdwood 
18968a978234SLiam Girdwood 	return 0;
18978a978234SLiam Girdwood }
18988a978234SLiam Girdwood 
18990038be9aSMengdong Lin /**
1900593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
19018abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
1902593d9e52SMengdong Lin  * @cfg: physical link configs.
1903593d9e52SMengdong Lin  *
1904593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
1905593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
1906593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
1907593d9e52SMengdong Lin  */
set_link_hw_format(struct snd_soc_dai_link * link,struct snd_soc_tplg_link_config * cfg)1908593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
1909593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
1910593d9e52SMengdong Lin {
1911593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
1912f026c123SPierre-Louis Bossart 	unsigned char bclk_provider, fsync_provider;
1913593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
1914593d9e52SMengdong Lin 	int i;
1915593d9e52SMengdong Lin 
19165aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1917593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
1918593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
1919593d9e52SMengdong Lin 			continue;
1920593d9e52SMengdong Lin 
19215aebe7c7SPierre-Louis Bossart 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
19225aebe7c7SPierre-Louis Bossart 			SND_SOC_DAIFMT_FORMAT_MASK;
1923593d9e52SMengdong Lin 
1924933e1c4aSKirill Marinushkin 		/* clock gating */
1925fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
1926fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1927933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1928fbeabd09SKirill Marinushkin 			break;
1929fbeabd09SKirill Marinushkin 
1930fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1931933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1932fbeabd09SKirill Marinushkin 			break;
1933fbeabd09SKirill Marinushkin 
1934fbeabd09SKirill Marinushkin 		default:
1935fbeabd09SKirill Marinushkin 			/* ignore the value */
1936fbeabd09SKirill Marinushkin 			break;
1937fbeabd09SKirill Marinushkin 		}
1938933e1c4aSKirill Marinushkin 
1939593d9e52SMengdong Lin 		/* clock signal polarity */
1940593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
1941593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
1942593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
1943593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1944593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
1945593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1946593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
1947593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1948593d9e52SMengdong Lin 		else
1949593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1950593d9e52SMengdong Lin 
1951593d9e52SMengdong Lin 		/* clock masters */
1952f026c123SPierre-Louis Bossart 		bclk_provider = (hw_config->bclk_provider ==
1953f026c123SPierre-Louis Bossart 			       SND_SOC_TPLG_BCLK_CP);
1954f026c123SPierre-Louis Bossart 		fsync_provider = (hw_config->fsync_provider ==
1955f026c123SPierre-Louis Bossart 				SND_SOC_TPLG_FSYNC_CP);
1956f026c123SPierre-Louis Bossart 		if (bclk_provider && fsync_provider)
1957f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1958f026c123SPierre-Louis Bossart 		else if (!bclk_provider && fsync_provider)
1959f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1960f026c123SPierre-Louis Bossart 		else if (bclk_provider && !fsync_provider)
1961f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1962593d9e52SMengdong Lin 		else
1963f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1964593d9e52SMengdong Lin 	}
1965593d9e52SMengdong Lin }
1966593d9e52SMengdong Lin 
1967593d9e52SMengdong Lin /**
1968593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
1969593d9e52SMengdong Lin  * version of source.
19708abab35fSCharles Keepax  * @tplg: topology context
1971593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
1972593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
1973593d9e52SMengdong Lin  *
1974ce1f2571SColin Ian King  * Support from version 4. User need free the returned link config manually.
1975593d9e52SMengdong Lin  */
link_new_ver(struct soc_tplg * tplg,struct snd_soc_tplg_link_config * src,struct snd_soc_tplg_link_config ** link)1976593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
1977593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
1978593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
1979593d9e52SMengdong Lin {
1980593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
1981593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
1982593d9e52SMengdong Lin 	int i;
1983593d9e52SMengdong Lin 
1984593d9e52SMengdong Lin 	*link = NULL;
1985593d9e52SMengdong Lin 
19865aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) !=
19875aebe7c7SPierre-Louis Bossart 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
1988593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
1989593d9e52SMengdong Lin 		return -EINVAL;
1990593d9e52SMengdong Lin 	}
1991593d9e52SMengdong Lin 
1992593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
1993593d9e52SMengdong Lin 
1994593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
1995593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1996593d9e52SMengdong Lin 	if (!dest)
1997593d9e52SMengdong Lin 		return -ENOMEM;
1998593d9e52SMengdong Lin 
19995aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
2000593d9e52SMengdong Lin 	dest->id = src_v4->id;
2001593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
20025aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2003593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2004593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2005593d9e52SMengdong Lin 
2006593d9e52SMengdong Lin 	*link = dest;
2007593d9e52SMengdong Lin 	return 0;
2008593d9e52SMengdong Lin }
2009593d9e52SMengdong Lin 
2010d6f31e0eSKuninori Morimoto /**
2011d6f31e0eSKuninori Morimoto  * snd_soc_find_dai_link - Find a DAI link
2012d6f31e0eSKuninori Morimoto  *
2013d6f31e0eSKuninori Morimoto  * @card: soc card
2014d6f31e0eSKuninori Morimoto  * @id: DAI link ID to match
2015d6f31e0eSKuninori Morimoto  * @name: DAI link name to match, optional
2016d6f31e0eSKuninori Morimoto  * @stream_name: DAI link stream name to match, optional
2017d6f31e0eSKuninori Morimoto  *
2018d6f31e0eSKuninori Morimoto  * This function will search all existing DAI links of the soc card to
2019d6f31e0eSKuninori Morimoto  * find the link of the same ID. Since DAI links may not have their
2020d6f31e0eSKuninori Morimoto  * unique ID, so name and stream name should also match if being
2021d6f31e0eSKuninori Morimoto  * specified.
2022d6f31e0eSKuninori Morimoto  *
2023d6f31e0eSKuninori Morimoto  * Return: pointer of DAI link, or NULL if not found.
2024d6f31e0eSKuninori Morimoto  */
snd_soc_find_dai_link(struct snd_soc_card * card,int id,const char * name,const char * stream_name)2025d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2026d6f31e0eSKuninori Morimoto 						      int id, const char *name,
2027d6f31e0eSKuninori Morimoto 						      const char *stream_name)
2028d6f31e0eSKuninori Morimoto {
2029d6f31e0eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
2030d6f31e0eSKuninori Morimoto 
2031d6f31e0eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2032b81e8efaSKuninori Morimoto 		struct snd_soc_dai_link *link = rtd->dai_link;
2033d6f31e0eSKuninori Morimoto 
2034d6f31e0eSKuninori Morimoto 		if (link->id != id)
2035d6f31e0eSKuninori Morimoto 			continue;
2036d6f31e0eSKuninori Morimoto 
2037e018e0b3SRanjani Sridharan 		if (name && (!link->name || !strstr(link->name, name)))
2038d6f31e0eSKuninori Morimoto 			continue;
2039d6f31e0eSKuninori Morimoto 
2040e018e0b3SRanjani Sridharan 		if (stream_name && (!link->stream_name ||
2041e018e0b3SRanjani Sridharan 				    !strstr(link->stream_name, stream_name)))
2042d6f31e0eSKuninori Morimoto 			continue;
2043d6f31e0eSKuninori Morimoto 
2044d6f31e0eSKuninori Morimoto 		return link;
2045d6f31e0eSKuninori Morimoto 	}
2046d6f31e0eSKuninori Morimoto 
2047d6f31e0eSKuninori Morimoto 	return NULL;
2048d6f31e0eSKuninori Morimoto }
2049d6f31e0eSKuninori Morimoto 
2050593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
soc_tplg_link_config(struct soc_tplg * tplg,struct snd_soc_tplg_link_config * cfg)2051593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2052593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2053593d9e52SMengdong Lin {
2054593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2055593d9e52SMengdong Lin 	const char *name, *stream_name;
2056dbab1cb8SMengdong Lin 	size_t len;
2057593d9e52SMengdong Lin 	int ret;
2058593d9e52SMengdong Lin 
2059dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2060dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2061dbab1cb8SMengdong Lin 		return -EINVAL;
2062dbab1cb8SMengdong Lin 	else if (len)
2063dbab1cb8SMengdong Lin 		name = cfg->name;
2064dbab1cb8SMengdong Lin 	else
2065dbab1cb8SMengdong Lin 		name = NULL;
2066dbab1cb8SMengdong Lin 
2067dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2068dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2069dbab1cb8SMengdong Lin 		return -EINVAL;
2070dbab1cb8SMengdong Lin 	else if (len)
2071dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2072dbab1cb8SMengdong Lin 	else
2073dbab1cb8SMengdong Lin 		stream_name = NULL;
2074593d9e52SMengdong Lin 
20755aebe7c7SPierre-Louis Bossart 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2076593d9e52SMengdong Lin 				     name, stream_name);
2077593d9e52SMengdong Lin 	if (!link) {
2078593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2079593d9e52SMengdong Lin 			name, cfg->id);
2080593d9e52SMengdong Lin 		return -EINVAL;
2081593d9e52SMengdong Lin 	}
2082593d9e52SMengdong Lin 
2083593d9e52SMengdong Lin 	/* hw format */
2084593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2085593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2086593d9e52SMengdong Lin 
2087593d9e52SMengdong Lin 	/* flags */
2088593d9e52SMengdong Lin 	if (cfg->flag_mask)
20895aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
20905aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flag_mask),
20915aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flags));
2092593d9e52SMengdong Lin 
2093593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2094c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2095593d9e52SMengdong Lin 	if (ret < 0) {
2096593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2097593d9e52SMengdong Lin 		return ret;
2098593d9e52SMengdong Lin 	}
2099593d9e52SMengdong Lin 
2100adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2101adfebb51SBard liao 	link->dobj.index = tplg->index;
2102adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
210331e92739SAmadeusz Sławiński 	if (tplg->ops)
210431e92739SAmadeusz Sławiński 		link->dobj.unload = tplg->ops->link_unload;
2105adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2106adfebb51SBard liao 
2107593d9e52SMengdong Lin 	return 0;
2108593d9e52SMengdong Lin }
2109593d9e52SMengdong Lin 
2110593d9e52SMengdong Lin 
2111593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
soc_tplg_link_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)2112593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2113593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2114593d9e52SMengdong Lin {
2115593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
21165aebe7c7SPierre-Louis Bossart 	int count;
21175aebe7c7SPierre-Louis Bossart 	int size;
2118593d9e52SMengdong Lin 	int i, ret;
2119593d9e52SMengdong Lin 	bool abi_match;
2120593d9e52SMengdong Lin 
21215aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
21225aebe7c7SPierre-Louis Bossart 
2123593d9e52SMengdong Lin 	/* check the element size and count */
2124593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
21255aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(link->size);
21265aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_link_config)
21275aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2128593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
21295aebe7c7SPierre-Louis Bossart 			size);
2130593d9e52SMengdong Lin 		return -EINVAL;
2131593d9e52SMengdong Lin 	}
2132593d9e52SMengdong Lin 
21333ce57f22SAmadeusz Sławiński 	if (soc_tplg_check_elem_count(tplg, size, count,
21345aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
21353ce57f22SAmadeusz Sławiński 				      "physical link config"))
2136593d9e52SMengdong Lin 		return -EINVAL;
2137593d9e52SMengdong Lin 
2138593d9e52SMengdong Lin 	/* config physical DAI links */
2139593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2140593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
21415aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(link->size);
21425aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*link)) {
2143593d9e52SMengdong Lin 			abi_match = true;
2144593d9e52SMengdong Lin 			_link = link;
2145593d9e52SMengdong Lin 		} else {
2146593d9e52SMengdong Lin 			abi_match = false;
2147593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2148593d9e52SMengdong Lin 			if (ret < 0)
2149593d9e52SMengdong Lin 				return ret;
2150593d9e52SMengdong Lin 		}
2151593d9e52SMengdong Lin 
2152593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
21532b2d5c4dSDragos Tarcatu 		if (ret < 0) {
21542b2d5c4dSDragos Tarcatu 			if (!abi_match)
21552b2d5c4dSDragos Tarcatu 				kfree(_link);
2156593d9e52SMengdong Lin 			return ret;
21572b2d5c4dSDragos Tarcatu 		}
2158593d9e52SMengdong Lin 
2159593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2160593d9e52SMengdong Lin 		 * real priv data size
2161593d9e52SMengdong Lin 		 */
21625aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2163593d9e52SMengdong Lin 
2164593d9e52SMengdong Lin 		if (!abi_match)
2165593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2166593d9e52SMengdong Lin 	}
2167593d9e52SMengdong Lin 
2168593d9e52SMengdong Lin 	return 0;
2169593d9e52SMengdong Lin }
2170593d9e52SMengdong Lin 
2171593d9e52SMengdong Lin /**
21729aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
21730038be9aSMengdong Lin  * @tplg: topology context
21749aa3f034SMengdong Lin  * @d: physical DAI configs.
21750038be9aSMengdong Lin  *
21769aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
21779aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
21780038be9aSMengdong Lin  */
soc_tplg_dai_config(struct soc_tplg * tplg,struct snd_soc_tplg_dai * d)21799aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
21809aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
21810038be9aSMengdong Lin {
21825aebe7c7SPierre-Louis Bossart 	struct snd_soc_dai_link_component dai_component;
21830038be9aSMengdong Lin 	struct snd_soc_dai *dai;
21840038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
21850038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
21860038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
21870038be9aSMengdong Lin 	int ret;
21880038be9aSMengdong Lin 
21895aebe7c7SPierre-Louis Bossart 	memset(&dai_component, 0, sizeof(dai_component));
21905aebe7c7SPierre-Louis Bossart 
21919aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
21920038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
21930038be9aSMengdong Lin 	if (!dai) {
21949aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
21959aa3f034SMengdong Lin 			d->dai_name);
21960038be9aSMengdong Lin 		return -EINVAL;
21970038be9aSMengdong Lin 	}
21980038be9aSMengdong Lin 
21995aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(d->dai_id) != dai->id) {
22009aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
22019aa3f034SMengdong Lin 			d->dai_name);
22020038be9aSMengdong Lin 		return -EINVAL;
22030038be9aSMengdong Lin 	}
22040038be9aSMengdong Lin 
22050038be9aSMengdong Lin 	dai_drv = dai->driver;
22060038be9aSMengdong Lin 	if (!dai_drv)
22070038be9aSMengdong Lin 		return -EINVAL;
22080038be9aSMengdong Lin 
22099aa3f034SMengdong Lin 	if (d->playback) {
22100038be9aSMengdong Lin 		stream = &dai_drv->playback;
22119aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2212ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
2213abc3caacSAmadeusz Sławiński 		if (ret < 0)
2214abc3caacSAmadeusz Sławiński 			goto err;
22150038be9aSMengdong Lin 	}
22160038be9aSMengdong Lin 
22179aa3f034SMengdong Lin 	if (d->capture) {
22180038be9aSMengdong Lin 		stream = &dai_drv->capture;
22199aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2220ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
2221abc3caacSAmadeusz Sławiński 		if (ret < 0)
2222abc3caacSAmadeusz Sławiński 			goto err;
22230038be9aSMengdong Lin 	}
22240038be9aSMengdong Lin 
22259aa3f034SMengdong Lin 	if (d->flag_mask)
22265aebe7c7SPierre-Louis Bossart 		set_dai_flags(dai_drv,
22275aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flag_mask),
22285aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flags));
22290038be9aSMengdong Lin 
22300038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2231c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
22320038be9aSMengdong Lin 	if (ret < 0) {
2233e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2234abc3caacSAmadeusz Sławiński 		goto err;
22350038be9aSMengdong Lin 	}
22360038be9aSMengdong Lin 
22370038be9aSMengdong Lin 	return 0;
2238abc3caacSAmadeusz Sławiński 
2239abc3caacSAmadeusz Sławiński err:
2240abc3caacSAmadeusz Sławiński 	return ret;
22410038be9aSMengdong Lin }
22420038be9aSMengdong Lin 
22439aa3f034SMengdong Lin /* load physical DAI elements */
soc_tplg_dai_elems_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)22449aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
22450038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
22460038be9aSMengdong Lin {
22475aebe7c7SPierre-Louis Bossart 	int count;
224865a4cfddSKuninori Morimoto 	int i;
22490038be9aSMengdong Lin 
22505aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
22515aebe7c7SPierre-Louis Bossart 
22520038be9aSMengdong Lin 	/* config the existing BE DAIs */
22530038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
225465a4cfddSKuninori Morimoto 		struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
225565a4cfddSKuninori Morimoto 		int ret;
225665a4cfddSKuninori Morimoto 
22575aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
22589aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
22590038be9aSMengdong Lin 			return -EINVAL;
22600038be9aSMengdong Lin 		}
22610038be9aSMengdong Lin 
2262dd8e871dSAmadeusz Sławiński 		ret = soc_tplg_dai_config(tplg, dai);
2263dd8e871dSAmadeusz Sławiński 		if (ret < 0) {
2264dd8e871dSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2265dd8e871dSAmadeusz Sławiński 			return ret;
2266dd8e871dSAmadeusz Sławiński 		}
2267dd8e871dSAmadeusz Sławiński 
22685aebe7c7SPierre-Louis Bossart 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
22690038be9aSMengdong Lin 	}
22700038be9aSMengdong Lin 
22710038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
22720038be9aSMengdong Lin 	return 0;
22730038be9aSMengdong Lin }
22740038be9aSMengdong Lin 
2275583958faSMengdong Lin /**
2276583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2277583958faSMengdong Lin  * of source.
22788abab35fSCharles Keepax  * @tplg: topology context
2279583958faSMengdong Lin  * @src: old version of manifest as a source
2280583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2281583958faSMengdong Lin  *
2282ce1f2571SColin Ian King  * Support from version 4. Users need free the returned manifest manually.
2283583958faSMengdong Lin  */
manifest_new_ver(struct soc_tplg * tplg,struct snd_soc_tplg_manifest * src,struct snd_soc_tplg_manifest ** manifest)2284583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2285583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2286583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2287583958faSMengdong Lin {
2288583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2289583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
22905aebe7c7SPierre-Louis Bossart 	int size;
2291583958faSMengdong Lin 
2292583958faSMengdong Lin 	*manifest = NULL;
2293583958faSMengdong Lin 
22945aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(src->size);
22955aebe7c7SPierre-Louis Bossart 	if (size != sizeof(*src_v4)) {
2296ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
22975aebe7c7SPierre-Louis Bossart 			 size);
22985aebe7c7SPierre-Louis Bossart 		if (size)
2299583958faSMengdong Lin 			return -EINVAL;
23005aebe7c7SPierre-Louis Bossart 		src->size = cpu_to_le32(sizeof(*src_v4));
2301583958faSMengdong Lin 	}
2302583958faSMengdong Lin 
2303583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2304583958faSMengdong Lin 
2305583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
23065aebe7c7SPierre-Louis Bossart 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
23075aebe7c7SPierre-Louis Bossart 		       GFP_KERNEL);
2308583958faSMengdong Lin 	if (!dest)
2309583958faSMengdong Lin 		return -ENOMEM;
2310583958faSMengdong Lin 
23115aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2312583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2313583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2314583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2315583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2316583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2317583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2318583958faSMengdong Lin 	if (dest->priv.size)
2319583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
23205aebe7c7SPierre-Louis Bossart 		       le32_to_cpu(src_v4->priv.size));
2321583958faSMengdong Lin 
2322583958faSMengdong Lin 	*manifest = dest;
2323583958faSMengdong Lin 	return 0;
2324583958faSMengdong Lin }
23250038be9aSMengdong Lin 
soc_tplg_manifest_load(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)23268a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
23278a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
23288a978234SLiam Girdwood {
2329583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2330583958faSMengdong Lin 	bool abi_match;
2331242c46c0SDragos Tarcatu 	int ret = 0;
23328a978234SLiam Girdwood 
23338a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2334583958faSMengdong Lin 
2335583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
23365aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2337583958faSMengdong Lin 		abi_match = true;
2338583958faSMengdong Lin 		_manifest = manifest;
2339583958faSMengdong Lin 	} else {
2340583958faSMengdong Lin 		abi_match = false;
234186e2d14bSCezary Rojewski 
2342242c46c0SDragos Tarcatu 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2343242c46c0SDragos Tarcatu 		if (ret < 0)
2344242c46c0SDragos Tarcatu 			return ret;
234506eb49f7SMengdong Lin 	}
234606eb49f7SMengdong Lin 
2347583958faSMengdong Lin 	/* pass control to component driver for optional further init */
2348c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->manifest)
2349242c46c0SDragos Tarcatu 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
23508a978234SLiam Girdwood 
2351583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2352583958faSMengdong Lin 		kfree(_manifest);
2353583958faSMengdong Lin 
2354242c46c0SDragos Tarcatu 	return ret;
23558a978234SLiam Girdwood }
23568a978234SLiam Girdwood 
23578a978234SLiam Girdwood /* validate header magic, size and type */
soc_tplg_valid_header(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)235823e591dcSAmadeusz Sławiński static int soc_tplg_valid_header(struct soc_tplg *tplg,
23598a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
23608a978234SLiam Girdwood {
23615aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
236206eb49f7SMengdong Lin 		dev_err(tplg->dev,
236306eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
23645aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
236506eb49f7SMengdong Lin 			tplg->fw->size);
236606eb49f7SMengdong Lin 		return -EINVAL;
236706eb49f7SMengdong Lin 	}
236806eb49f7SMengdong Lin 
2369c5d184c9SAmadeusz Sławiński 	if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
237086e2d14bSCezary Rojewski 		dev_err(tplg->dev,
237186e2d14bSCezary Rojewski 			"ASoC: invalid header of type %d at offset %ld payload_size %d\n",
237286e2d14bSCezary Rojewski 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
237386e2d14bSCezary Rojewski 			hdr->payload_size);
237486e2d14bSCezary Rojewski 		return -EINVAL;
237586e2d14bSCezary Rojewski 	}
237686e2d14bSCezary Rojewski 
23778a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
237826d87881SAmadeusz Sławiński 	if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
23798a978234SLiam Girdwood 		dev_err(tplg->dev,
23808a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
23818a978234SLiam Girdwood 			tplg->pass, hdr->magic,
23828a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
23838a978234SLiam Girdwood 		return -EINVAL;
23848a978234SLiam Girdwood 	}
23858a978234SLiam Girdwood 
23865aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
23878a978234SLiam Girdwood 		dev_err(tplg->dev,
23888a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
23898a978234SLiam Girdwood 			tplg->pass, hdr->magic,
23908a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
23918a978234SLiam Girdwood 		return -EINVAL;
23928a978234SLiam Girdwood 	}
23938a978234SLiam Girdwood 
2394288b8da7SMengdong Lin 	/* Support ABI from version 4 */
23955aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
23965aebe7c7SPierre-Louis Bossart 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
23978a978234SLiam Girdwood 		dev_err(tplg->dev,
23988a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
23998a978234SLiam Girdwood 			tplg->pass, hdr->abi,
24008a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
24018a978234SLiam Girdwood 			tplg->fw->size);
24028a978234SLiam Girdwood 		return -EINVAL;
24038a978234SLiam Girdwood 	}
24048a978234SLiam Girdwood 
24058a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
24068a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
24078a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
24088a978234SLiam Girdwood 		return -EINVAL;
24098a978234SLiam Girdwood 	}
24108a978234SLiam Girdwood 
2411d9b07b79SAmadeusz Sławiński 	return 0;
24128a978234SLiam Girdwood }
24138a978234SLiam Girdwood 
24148a978234SLiam Girdwood /* check header type and call appropriate handler */
soc_tplg_load_header(struct soc_tplg * tplg,struct snd_soc_tplg_hdr * hdr)24158a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
24168a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24178a978234SLiam Girdwood {
241882ed7418SKeyon Jie 	int (*elem_load)(struct soc_tplg *tplg,
241982ed7418SKeyon Jie 			 struct snd_soc_tplg_hdr *hdr);
242082ed7418SKeyon Jie 	unsigned int hdr_pass;
242182ed7418SKeyon Jie 
24228a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
24238a978234SLiam Girdwood 
24245aebe7c7SPierre-Louis Bossart 	tplg->index = le32_to_cpu(hdr->index);
24258a978234SLiam Girdwood 
24265aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(hdr->type)) {
24278a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
24288a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
24298a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
24305e2cd47aSAmadeusz Sławiński 		hdr_pass = SOC_TPLG_PASS_CONTROL;
243182ed7418SKeyon Jie 		elem_load = soc_tplg_kcontrol_elems_load;
243282ed7418SKeyon Jie 		break;
24338a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
243482ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_GRAPH;
243582ed7418SKeyon Jie 		elem_load = soc_tplg_dapm_graph_elems_load;
243682ed7418SKeyon Jie 		break;
24378a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
243882ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_WIDGET;
243982ed7418SKeyon Jie 		elem_load = soc_tplg_dapm_widget_elems_load;
244082ed7418SKeyon Jie 		break;
24418a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
244282ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_PCM_DAI;
244382ed7418SKeyon Jie 		elem_load = soc_tplg_pcm_elems_load;
244482ed7418SKeyon Jie 		break;
24453fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
244682ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_BE_DAI;
244782ed7418SKeyon Jie 		elem_load = soc_tplg_dai_elems_load;
244882ed7418SKeyon Jie 		break;
2449593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2450593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2451593d9e52SMengdong Lin 		/* physical link configurations */
245282ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_LINK;
245382ed7418SKeyon Jie 		elem_load = soc_tplg_link_elems_load;
245482ed7418SKeyon Jie 		break;
24558a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
245682ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_MANIFEST;
245782ed7418SKeyon Jie 		elem_load = soc_tplg_manifest_load;
245882ed7418SKeyon Jie 		break;
24598a978234SLiam Girdwood 	default:
24608a978234SLiam Girdwood 		/* bespoke vendor data object */
246182ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_VENDOR;
246282ed7418SKeyon Jie 		elem_load = soc_tplg_vendor_load;
246382ed7418SKeyon Jie 		break;
246482ed7418SKeyon Jie 	}
246582ed7418SKeyon Jie 
246682ed7418SKeyon Jie 	if (tplg->pass == hdr_pass) {
246782ed7418SKeyon Jie 		dev_dbg(tplg->dev,
246882ed7418SKeyon Jie 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
246982ed7418SKeyon Jie 			hdr->payload_size, hdr->type, hdr->version,
247082ed7418SKeyon Jie 			hdr->vendor_type, tplg->pass);
247182ed7418SKeyon Jie 		return elem_load(tplg, hdr);
24728a978234SLiam Girdwood 	}
24738a978234SLiam Girdwood 
24748a978234SLiam Girdwood 	return 0;
24758a978234SLiam Girdwood }
24768a978234SLiam Girdwood 
24778a978234SLiam Girdwood /* process the topology file headers */
soc_tplg_process_headers(struct soc_tplg * tplg)24788a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
24798a978234SLiam Girdwood {
24808a978234SLiam Girdwood 	int ret;
24818a978234SLiam Girdwood 
24828a978234SLiam Girdwood 	/* process the header types from start to end */
2483395f8fd6SAmadeusz Sławiński 	for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2484f79e4b2aSKuninori Morimoto 		struct snd_soc_tplg_hdr *hdr;
24858a978234SLiam Girdwood 
24868a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
24878a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
24888a978234SLiam Girdwood 
24898a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
24908a978234SLiam Girdwood 
24918a978234SLiam Girdwood 			/* make sure header is valid before loading */
249223e591dcSAmadeusz Sławiński 			ret = soc_tplg_valid_header(tplg, hdr);
2493f9d1fe7eSAmadeusz Sławiński 			if (ret < 0)
24948a978234SLiam Girdwood 				return ret;
24958a978234SLiam Girdwood 
24968a978234SLiam Girdwood 			/* load the header object */
24978a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
24988bf9475fSPierre-Louis Bossart 			if (ret < 0) {
2499b6c3bddaSJohan Hovold 				if (ret != -EPROBE_DEFER) {
25008bf9475fSPierre-Louis Bossart 					dev_err(tplg->dev,
2501b6c3bddaSJohan Hovold 						"ASoC: topology: could not load header: %d\n",
2502b6c3bddaSJohan Hovold 						ret);
2503b6c3bddaSJohan Hovold 				}
25048a978234SLiam Girdwood 				return ret;
25058bf9475fSPierre-Louis Bossart 			}
25068a978234SLiam Girdwood 
25078a978234SLiam Girdwood 			/* goto next header */
25085aebe7c7SPierre-Louis Bossart 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
25098a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
25108a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25118a978234SLiam Girdwood 		}
25128a978234SLiam Girdwood 
25138a978234SLiam Girdwood 	}
25148a978234SLiam Girdwood 
25158a978234SLiam Girdwood 	/* signal DAPM we are complete */
25168a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
25178a978234SLiam Girdwood 
25188a978234SLiam Girdwood 	return ret;
25198a978234SLiam Girdwood }
25208a978234SLiam Girdwood 
soc_tplg_load(struct soc_tplg * tplg)25218a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
25228a978234SLiam Girdwood {
25238a978234SLiam Girdwood 	int ret;
25248a978234SLiam Girdwood 
25258a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
25268a978234SLiam Girdwood 	if (ret == 0)
2527415717e1SRanjani Sridharan 		return soc_tplg_complete(tplg);
25288a978234SLiam Girdwood 
25298a978234SLiam Girdwood 	return ret;
25308a978234SLiam Girdwood }
25318a978234SLiam Girdwood 
25328a978234SLiam Girdwood /* load audio component topology from "firmware" file */
snd_soc_tplg_component_load(struct snd_soc_component * comp,struct snd_soc_tplg_ops * ops,const struct firmware * fw)25338a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2534a5b8f71cSAmadeusz Sławiński 	struct snd_soc_tplg_ops *ops, const struct firmware *fw)
25358a978234SLiam Girdwood {
25368a978234SLiam Girdwood 	struct soc_tplg tplg;
2537304017d3SBard liao 	int ret;
25388a978234SLiam Girdwood 
2539d40ab86fSAmadeusz Sławiński 	/*
2540d40ab86fSAmadeusz Sławiński 	 * check if we have sane parameters:
2541d40ab86fSAmadeusz Sławiński 	 * comp - needs to exist to keep and reference data while parsing
2542d40ab86fSAmadeusz Sławiński 	 * comp->card - used for setting card related parameters
2543f714fbc1SAmadeusz Sławiński 	 * comp->card->dev - used for resource management and prints
2544d40ab86fSAmadeusz Sławiński 	 * fw - we need it, as it is the very thing we parse
2545d40ab86fSAmadeusz Sławiński 	 */
2546f714fbc1SAmadeusz Sławiński 	if (!comp || !comp->card || !comp->card->dev || !fw)
2547c42464a4SAmadeusz Sławiński 		return -EINVAL;
2548c42464a4SAmadeusz Sławiński 
25498a978234SLiam Girdwood 	/* setup parsing context */
25508a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
25518a978234SLiam Girdwood 	tplg.fw = fw;
2552f714fbc1SAmadeusz Sławiński 	tplg.dev = comp->card->dev;
25538a978234SLiam Girdwood 	tplg.comp = comp;
25549c88a983SAmadeusz Sławiński 	if (ops) {
25558a978234SLiam Girdwood 		tplg.ops = ops;
25568a978234SLiam Girdwood 		tplg.io_ops = ops->io_ops;
25578a978234SLiam Girdwood 		tplg.io_ops_count = ops->io_ops_count;
25581a3232d2SMengdong Lin 		tplg.bytes_ext_ops = ops->bytes_ext_ops;
25591a3232d2SMengdong Lin 		tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
25609c88a983SAmadeusz Sławiński 	}
25618a978234SLiam Girdwood 
2562304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2563304017d3SBard liao 	/* free the created components if fail to load topology */
2564304017d3SBard liao 	if (ret)
2565a5b8f71cSAmadeusz Sławiński 		snd_soc_tplg_component_remove(comp);
2566304017d3SBard liao 
2567304017d3SBard liao 	return ret;
25688a978234SLiam Girdwood }
25698a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
25708a978234SLiam Girdwood 
25718a978234SLiam Girdwood /* remove dynamic controls from the component driver */
snd_soc_tplg_component_remove(struct snd_soc_component * comp)2572a5b8f71cSAmadeusz Sławiński int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
25738a978234SLiam Girdwood {
25748a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
2575395f8fd6SAmadeusz Sławiński 	int pass;
25768a978234SLiam Girdwood 
25778a978234SLiam Girdwood 	/* process the header types from end to start */
2578395f8fd6SAmadeusz Sławiński 	for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
25798a978234SLiam Girdwood 
25808a978234SLiam Girdwood 		/* remove mixer controls */
25818a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
25828a978234SLiam Girdwood 			list) {
25838a978234SLiam Girdwood 
25848a978234SLiam Girdwood 			switch (dobj->type) {
25858a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
2586fdfa3661SAmadeusz Sławiński 			case SND_SOC_DOBJ_ENUM:
2587fdfa3661SAmadeusz Sławiński 			case SND_SOC_DOBJ_MIXER:
2588fdfa3661SAmadeusz Sławiński 				soc_tplg_remove_kcontrol(comp, dobj, pass);
25898a978234SLiam Girdwood 				break;
25907df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
25912abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_route(comp, dobj, pass);
25927df04ea7SRanjani Sridharan 				break;
25938a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
25942abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_widget(comp, dobj, pass);
25958a978234SLiam Girdwood 				break;
25968a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
25972abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_dai(comp, dobj, pass);
25988a978234SLiam Girdwood 				break;
2599acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
26002abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_link(comp, dobj, pass);
2601acfc7d46SMengdong Lin 				break;
2602adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2603adfebb51SBard liao 				/*
2604adfebb51SBard liao 				 * call link_unload ops if extra
2605adfebb51SBard liao 				 * deinitialization is needed.
2606adfebb51SBard liao 				 */
2607adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2608adfebb51SBard liao 				break;
26098a978234SLiam Girdwood 			default:
26108a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
26118a978234SLiam Girdwood 					dobj->type);
26128a978234SLiam Girdwood 				break;
26138a978234SLiam Girdwood 			}
26148a978234SLiam Girdwood 		}
26158a978234SLiam Girdwood 	}
26168a978234SLiam Girdwood 
26178a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
26188a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
26198a978234SLiam Girdwood }
26208a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2621