xref: /openbmc/linux/sound/soc/soc-topology.c (revision 53085402)
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 */
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 
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 
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 
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 
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 
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 
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 
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 
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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. */
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 
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 
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 
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 
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 
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 
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 
8548a978234SLiam Girdwood 	return 0;
8558a978234SLiam Girdwood }
8568a978234SLiam Girdwood 
8570db627c4SAmadeusz Sławiński static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
8588a978234SLiam Girdwood {
8598a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
8608a978234SLiam Girdwood 	struct soc_enum *se;
8618a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8620db627c4SAmadeusz Sławiński 	int ret = 0;
8638a978234SLiam Girdwood 
8648a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8658a978234SLiam Girdwood 				      sizeof(struct snd_soc_tplg_enum_control),
8660db627c4SAmadeusz Sławiński 				      1, size, "enums"))
8678a978234SLiam Girdwood 		return -EINVAL;
8688a978234SLiam Girdwood 
8698a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
8708a978234SLiam Girdwood 
8718a978234SLiam Girdwood 	/* validate kcontrol */
8728a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8738a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8748a978234SLiam Girdwood 		return -EINVAL;
8758a978234SLiam Girdwood 
876ff922622SAmadeusz Sławiński 	se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
8778a978234SLiam Girdwood 	if (se == NULL)
8788a978234SLiam Girdwood 		return -ENOMEM;
8798a978234SLiam Girdwood 
88002b64245SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
8815aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(ec->priv.size));
88202b64245SLiam Girdwood 
8838a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
8848a978234SLiam Girdwood 		ec->hdr.name, ec->items);
8858a978234SLiam Girdwood 
8868a978234SLiam Girdwood 	memset(&kc, 0, sizeof(kc));
8878a978234SLiam Girdwood 	kc.name = ec->hdr.name;
8888a978234SLiam Girdwood 	kc.private_value = (long)se;
8898a978234SLiam Girdwood 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8905aebe7c7SPierre-Louis Bossart 	kc.access = le32_to_cpu(ec->hdr.access);
8918a978234SLiam Girdwood 
8928f9974d9SAmadeusz Sławiński 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
8938f9974d9SAmadeusz Sławiński 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
8948a978234SLiam Girdwood 		SNDRV_CHMAP_FL);
8958f9974d9SAmadeusz Sławiński 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
8968a978234SLiam Girdwood 		SNDRV_CHMAP_FL);
8978a978234SLiam Girdwood 
8985aebe7c7SPierre-Louis Bossart 	se->mask = le32_to_cpu(ec->mask);
8998a978234SLiam Girdwood 	se->dobj.index = tplg->index;
9008a978234SLiam Girdwood 	se->dobj.type = SND_SOC_DOBJ_ENUM;
90131e92739SAmadeusz Sławiński 	if (tplg->ops)
90231e92739SAmadeusz Sławiński 		se->dobj.unload = tplg->ops->control_unload;
9038a978234SLiam Girdwood 	INIT_LIST_HEAD(&se->dobj.list);
9048a978234SLiam Girdwood 
9055aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(ec->hdr.ops.info)) {
9068a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9078a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
9080db627c4SAmadeusz Sławiński 		ret = soc_tplg_denum_create_values(tplg, se, ec);
9090db627c4SAmadeusz Sławiński 		if (ret < 0) {
9108a978234SLiam Girdwood 			dev_err(tplg->dev,
9118a978234SLiam Girdwood 				"ASoC: could not create values for %s\n",
9128a978234SLiam Girdwood 				ec->hdr.name);
9130db627c4SAmadeusz Sławiński 			goto err;
9148a978234SLiam Girdwood 		}
915df561f66SGustavo A. R. Silva 		fallthrough;
9168a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
9178a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9188a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9190db627c4SAmadeusz Sławiński 		ret = soc_tplg_denum_create_texts(tplg, se, ec);
9200db627c4SAmadeusz Sławiński 		if (ret < 0) {
9218a978234SLiam Girdwood 			dev_err(tplg->dev,
9228a978234SLiam Girdwood 				"ASoC: could not create texts for %s\n",
9238a978234SLiam Girdwood 				ec->hdr.name);
9240db627c4SAmadeusz Sławiński 			goto err;
9258a978234SLiam Girdwood 		}
9268a978234SLiam Girdwood 		break;
9278a978234SLiam Girdwood 	default:
9280db627c4SAmadeusz Sławiński 		ret = -EINVAL;
9298a978234SLiam Girdwood 		dev_err(tplg->dev,
9308a978234SLiam Girdwood 			"ASoC: invalid enum control type %d for %s\n",
9318a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
9320db627c4SAmadeusz Sławiński 		goto err;
9338a978234SLiam Girdwood 	}
9348a978234SLiam Girdwood 
9358a978234SLiam Girdwood 	/* map io handlers */
9360db627c4SAmadeusz Sławiński 	ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
9370db627c4SAmadeusz Sławiński 	if (ret) {
9388a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
9390db627c4SAmadeusz Sławiński 		goto err;
9408a978234SLiam Girdwood 	}
9418a978234SLiam Girdwood 
9428a978234SLiam Girdwood 	/* pass control to driver for optional further init */
9439e2ee000SAmadeusz Sławiński 	ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
944ec5dffcdSAmadeusz Sławiński 	if (ret < 0)
9450db627c4SAmadeusz Sławiński 		goto err;
9468a978234SLiam Girdwood 
9478a978234SLiam Girdwood 	/* register control here */
9480db627c4SAmadeusz Sławiński 	ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
9492316c11fSAmadeusz Sławiński 	if (ret < 0)
9500db627c4SAmadeusz Sławiński 		goto err;
9518a978234SLiam Girdwood 
9528a978234SLiam Girdwood 	list_add(&se->dobj.list, &tplg->comp->dobj_list);
953952bd937SPierre-Louis Bossart 
9540db627c4SAmadeusz Sławiński err:
9550db627c4SAmadeusz Sławiński 	return ret;
9568a978234SLiam Girdwood }
9578a978234SLiam Girdwood 
9588a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
9598a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
9608a978234SLiam Girdwood {
9612ae548f3SAmadeusz Sławiński 	int ret;
9628a978234SLiam Girdwood 	int i;
9638a978234SLiam Girdwood 
9648a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
9658a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
9668a978234SLiam Girdwood 
9675aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
968ea8f6b29SKuninori Morimoto 		struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
9698a978234SLiam Girdwood 
9705aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
97106eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
97206eb49f7SMengdong Lin 			return -EINVAL;
97306eb49f7SMengdong Lin 		}
97406eb49f7SMengdong Lin 
9755aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
9768a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
9778a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
9788a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
9798a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
9808a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
9818a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
9828a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
9830db627c4SAmadeusz Sławiński 			ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
9848a978234SLiam Girdwood 			break;
9858a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9868a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9878a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9888a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9898a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9900db627c4SAmadeusz Sławiński 			ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
9918a978234SLiam Girdwood 			break;
9928a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
9930db627c4SAmadeusz Sławiński 			ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
9948a978234SLiam Girdwood 			break;
9958a978234SLiam Girdwood 		default:
9968a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
9978a978234SLiam Girdwood 			return -EINVAL;
9988a978234SLiam Girdwood 		}
9992ae548f3SAmadeusz Sławiński 		if (ret < 0) {
10002ae548f3SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid control\n");
10012ae548f3SAmadeusz Sławiński 			return ret;
10022ae548f3SAmadeusz Sławiński 		}
10032ae548f3SAmadeusz Sławiński 
10048a978234SLiam Girdwood 	}
10058a978234SLiam Girdwood 
10068a978234SLiam Girdwood 	return 0;
10078a978234SLiam Girdwood }
10088a978234SLiam Girdwood 
1009503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1010503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1011503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1012503e79b7SLiam Girdwood {
1013c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->dapm_route_load)
1014503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1015503e79b7SLiam Girdwood 			route);
1016503e79b7SLiam Girdwood 
1017503e79b7SLiam Girdwood 	return 0;
1018503e79b7SLiam Girdwood }
1019503e79b7SLiam Girdwood 
10208a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
10218a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10228a978234SLiam Girdwood {
10238a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
10248a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
1025cc44c749SAmadeusz Sławiński 	struct snd_soc_dapm_route *route;
1026ff922622SAmadeusz Sławiński 	int count, i;
10277df04ea7SRanjani Sridharan 	int ret = 0;
10288a978234SLiam Girdwood 
10295aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
10305aebe7c7SPierre-Louis Bossart 
10318a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10328a978234SLiam Girdwood 				      sizeof(struct snd_soc_tplg_dapm_graph_elem),
10333ce57f22SAmadeusz Sławiński 				      count, le32_to_cpu(hdr->payload_size), "graph"))
10348a978234SLiam Girdwood 		return -EINVAL;
10358a978234SLiam Girdwood 
1036b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1037b75a6511SLiam Girdwood 		hdr->index);
10388a978234SLiam Girdwood 
10397df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
1040cc44c749SAmadeusz Sławiński 		route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1041cc44c749SAmadeusz Sławiński 		if (!route)
10427df04ea7SRanjani Sridharan 			return -ENOMEM;
10438a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
10448a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
10458a978234SLiam Girdwood 
10468a978234SLiam Girdwood 		/* validate routes */
10478a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10487df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
10497df04ea7SRanjani Sridharan 			ret = -EINVAL;
10507df04ea7SRanjani Sridharan 			break;
10517df04ea7SRanjani Sridharan 		}
10528a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10537df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
10547df04ea7SRanjani Sridharan 			ret = -EINVAL;
10557df04ea7SRanjani Sridharan 			break;
10567df04ea7SRanjani Sridharan 		}
10578a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10587df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
10597df04ea7SRanjani Sridharan 			ret = -EINVAL;
10607df04ea7SRanjani Sridharan 			break;
10618a978234SLiam Girdwood 		}
10628a978234SLiam Girdwood 
1063cc44c749SAmadeusz Sławiński 		route->source = elem->source;
1064cc44c749SAmadeusz Sławiński 		route->sink = elem->sink;
10657df04ea7SRanjani Sridharan 
10667df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
1067cc44c749SAmadeusz Sławiński 		route->connected = NULL;
10687df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1069cc44c749SAmadeusz Sławiński 			route->control = NULL;
10707df04ea7SRanjani Sridharan 		else
1071cc44c749SAmadeusz Sławiński 			route->control = elem->control;
10727df04ea7SRanjani Sridharan 
10737df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
1074cc44c749SAmadeusz Sławiński 		route->dobj.type = SND_SOC_DOBJ_GRAPH;
107531e92739SAmadeusz Sławiński 		if (tplg->ops)
1076dd184c40SPeter Ujfalusi 			route->dobj.unload = tplg->ops->dapm_route_unload;
1077cc44c749SAmadeusz Sławiński 		route->dobj.index = tplg->index;
1078cc44c749SAmadeusz Sławiński 		list_add(&route->dobj.list, &tplg->comp->dobj_list);
10797df04ea7SRanjani Sridharan 
1080cc44c749SAmadeusz Sławiński 		ret = soc_tplg_add_route(tplg, route);
10816f0307dfSPierre-Louis Bossart 		if (ret < 0) {
10828bf9475fSPierre-Louis Bossart 			dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
10836856e887SAmadeusz Sławiński 			break;
10846f0307dfSPierre-Louis Bossart 		}
10857df04ea7SRanjani Sridharan 
10867df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
1087cc44c749SAmadeusz Sławiński 		snd_soc_dapm_add_routes(dapm, route, 1);
10887df04ea7SRanjani Sridharan 	}
10897df04ea7SRanjani Sridharan 
10907df04ea7SRanjani Sridharan 	return ret;
10918a978234SLiam Girdwood }
10928a978234SLiam Girdwood 
1093d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
10948a978234SLiam Girdwood {
10958a978234SLiam Girdwood 	struct soc_mixer_control *sm;
10968a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
1097d29d41e2SJaska Uimonen 	int err;
10988a978234SLiam Girdwood 
10998a978234SLiam Girdwood 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
11008a978234SLiam Girdwood 
11018a978234SLiam Girdwood 	/* validate kcontrol */
11028a978234SLiam Girdwood 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11038a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1104d29d41e2SJaska Uimonen 		return -EINVAL;
11059f90af3aSAmadeusz Sławiński 
1106ff922622SAmadeusz Sławiński 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
1107d29d41e2SJaska Uimonen 	if (!sm)
1108d29d41e2SJaska Uimonen 		return -ENOMEM;
11098a978234SLiam Girdwood 
1110d29d41e2SJaska Uimonen 	tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) +
1111d29d41e2SJaska Uimonen 		le32_to_cpu(mc->priv.size);
111202b64245SLiam Girdwood 
1113d29d41e2SJaska Uimonen 	dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n",
1114d29d41e2SJaska Uimonen 		mc->hdr.name);
11158a978234SLiam Girdwood 
1116d29d41e2SJaska Uimonen 	kc->private_value = (long)sm;
1117d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
1118d29d41e2SJaska Uimonen 	if (!kc->name)
1119d29d41e2SJaska Uimonen 		return -ENOMEM;
1120d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1121d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(mc->hdr.access);
11228a978234SLiam Girdwood 
11238a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
11248f9974d9SAmadeusz Sławiński 	sm->reg = tplg_chan_get_reg(tplg, mc->channel,
11258a978234SLiam Girdwood 				    SNDRV_CHMAP_FL);
11268f9974d9SAmadeusz Sławiński 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel,
11278a978234SLiam Girdwood 				     SNDRV_CHMAP_FR);
11288f9974d9SAmadeusz Sławiński 	sm->shift = tplg_chan_get_shift(tplg, mc->channel,
11298a978234SLiam Girdwood 					SNDRV_CHMAP_FL);
11308f9974d9SAmadeusz Sławiński 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel,
11318a978234SLiam Girdwood 					 SNDRV_CHMAP_FR);
11328a978234SLiam Girdwood 
113372bbeda0SPierre-Louis Bossart 	sm->max = le32_to_cpu(mc->max);
113472bbeda0SPierre-Louis Bossart 	sm->min = le32_to_cpu(mc->min);
113572bbeda0SPierre-Louis Bossart 	sm->invert = le32_to_cpu(mc->invert);
113672bbeda0SPierre-Louis Bossart 	sm->platform_max = le32_to_cpu(mc->platform_max);
11378a978234SLiam Girdwood 	sm->dobj.index = tplg->index;
11388a978234SLiam Girdwood 	INIT_LIST_HEAD(&sm->dobj.list);
11398a978234SLiam Girdwood 
11408a978234SLiam Girdwood 	/* map io handlers */
1141d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
11428a978234SLiam Girdwood 	if (err) {
11438a978234SLiam Girdwood 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1144d29d41e2SJaska Uimonen 		return err;
11458a978234SLiam Girdwood 	}
11468a978234SLiam Girdwood 
11473789debfSBard liao 	/* create any TLV data */
1148d29d41e2SJaska Uimonen 	err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
1149482db55aSAmadeusz Sławiński 	if (err < 0) {
1150482db55aSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1151482db55aSAmadeusz Sławiński 			mc->hdr.name);
1152d29d41e2SJaska Uimonen 		return err;
1153482db55aSAmadeusz Sławiński 	}
11543789debfSBard liao 
11558a978234SLiam Girdwood 	/* pass control to driver for optional further init */
11569e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &mc->hdr);
1157ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1158d29d41e2SJaska Uimonen 		return err;
11598a978234SLiam Girdwood 
1160d29d41e2SJaska Uimonen 	return 0;
1161d29d41e2SJaska Uimonen }
1162d29d41e2SJaska Uimonen 
1163d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
11648a978234SLiam Girdwood {
11658a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
11668a978234SLiam Girdwood 	struct soc_enum *se;
1167d29d41e2SJaska Uimonen 	int err;
11688a978234SLiam Girdwood 
11698a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
11708a978234SLiam Girdwood 	/* validate kcontrol */
11718a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11728a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1173d29d41e2SJaska Uimonen 		return -EINVAL;
11748a978234SLiam Girdwood 
1175ff922622SAmadeusz Sławiński 	se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
1176d29d41e2SJaska Uimonen 	if (!se)
1177d29d41e2SJaska Uimonen 		return -ENOMEM;
11788a978234SLiam Girdwood 
117902b64245SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
118072bbeda0SPierre-Louis Bossart 		      le32_to_cpu(ec->priv.size));
118102b64245SLiam Girdwood 
11828a978234SLiam Girdwood 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
11838a978234SLiam Girdwood 		ec->hdr.name);
11848a978234SLiam Girdwood 
1185d29d41e2SJaska Uimonen 	kc->private_value = (long)se;
1186d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
1187d29d41e2SJaska Uimonen 	if (!kc->name)
1188d29d41e2SJaska Uimonen 		return -ENOMEM;
1189d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1190d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(ec->hdr.access);
11918a978234SLiam Girdwood 
11928a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
11938f9974d9SAmadeusz Sławiński 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
11948f9974d9SAmadeusz Sławiński 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
11951a7dd6e2SMengdong Lin 					  SNDRV_CHMAP_FL);
11968f9974d9SAmadeusz Sławiński 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
11971a7dd6e2SMengdong Lin 					  SNDRV_CHMAP_FR);
11988a978234SLiam Girdwood 
119972bbeda0SPierre-Louis Bossart 	se->items = le32_to_cpu(ec->items);
120072bbeda0SPierre-Louis Bossart 	se->mask = le32_to_cpu(ec->mask);
12018a978234SLiam Girdwood 	se->dobj.index = tplg->index;
12028a978234SLiam Girdwood 
12035aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(ec->hdr.ops.info)) {
12048a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
12058a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1206ff922622SAmadeusz Sławiński 		err = soc_tplg_denum_create_values(tplg, se, ec);
12078a978234SLiam Girdwood 		if (err < 0) {
12088a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
12098a978234SLiam Girdwood 				ec->hdr.name);
1210d29d41e2SJaska Uimonen 			return err;
12118a978234SLiam Girdwood 		}
1212df561f66SGustavo A. R. Silva 		fallthrough;
12138a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
12148a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
12158a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1216ff922622SAmadeusz Sławiński 		err = soc_tplg_denum_create_texts(tplg, se, ec);
12178a978234SLiam Girdwood 		if (err < 0) {
12188a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
12198a978234SLiam Girdwood 				ec->hdr.name);
1220d29d41e2SJaska Uimonen 			return err;
12218a978234SLiam Girdwood 		}
12228a978234SLiam Girdwood 		break;
12238a978234SLiam Girdwood 	default:
12248a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
12258a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
1226d29d41e2SJaska Uimonen 		return -EINVAL;
12278a978234SLiam Girdwood 	}
12288a978234SLiam Girdwood 
12298a978234SLiam Girdwood 	/* map io handlers */
1230d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
12318a978234SLiam Girdwood 	if (err) {
12328a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1233d29d41e2SJaska Uimonen 		return err;
12348a978234SLiam Girdwood 	}
12358a978234SLiam Girdwood 
12368a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12379e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &ec->hdr);
1238ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1239d29d41e2SJaska Uimonen 		return err;
12401a7dd6e2SMengdong Lin 
1241d29d41e2SJaska Uimonen 	return 0;
12428a978234SLiam Girdwood }
12438a978234SLiam Girdwood 
1244d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
12458a978234SLiam Girdwood {
12468a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
12478a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
1248d29d41e2SJaska Uimonen 	int err;
12498a978234SLiam Girdwood 
12508a978234SLiam Girdwood 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
12518a978234SLiam Girdwood 
12528a978234SLiam Girdwood 	/* validate kcontrol */
12538a978234SLiam Girdwood 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12548a978234SLiam Girdwood 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1255d29d41e2SJaska Uimonen 		return -EINVAL;
12568a978234SLiam Girdwood 
1257ff922622SAmadeusz Sławiński 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
1258d29d41e2SJaska Uimonen 	if (!sbe)
1259d29d41e2SJaska Uimonen 		return -ENOMEM;
12608a978234SLiam Girdwood 
12618a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
12625aebe7c7SPierre-Louis Bossart 		      le32_to_cpu(be->priv.size));
12638a978234SLiam Girdwood 
12648a978234SLiam Girdwood 	dev_dbg(tplg->dev,
12658a978234SLiam Girdwood 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
12668a978234SLiam Girdwood 		be->hdr.name, be->hdr.access);
12678a978234SLiam Girdwood 
1268d29d41e2SJaska Uimonen 	kc->private_value = (long)sbe;
1269d29d41e2SJaska Uimonen 	kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
1270d29d41e2SJaska Uimonen 	if (!kc->name)
1271d29d41e2SJaska Uimonen 		return -ENOMEM;
1272d29d41e2SJaska Uimonen 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1273d29d41e2SJaska Uimonen 	kc->access = le32_to_cpu(be->hdr.access);
12748a978234SLiam Girdwood 
127572bbeda0SPierre-Louis Bossart 	sbe->max = le32_to_cpu(be->max);
12768a978234SLiam Girdwood 	INIT_LIST_HEAD(&sbe->dobj.list);
12778a978234SLiam Girdwood 
12788a978234SLiam Girdwood 	/* map standard io handlers and check for external handlers */
1279d29d41e2SJaska Uimonen 	err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
12808a978234SLiam Girdwood 	if (err) {
12818a978234SLiam Girdwood 		soc_control_err(tplg, &be->hdr, be->hdr.name);
1282d29d41e2SJaska Uimonen 		return err;
12838a978234SLiam Girdwood 	}
12848a978234SLiam Girdwood 
12858a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12869e2ee000SAmadeusz Sławiński 	err = soc_tplg_control_load(tplg, kc, &be->hdr);
1287ec5dffcdSAmadeusz Sławiński 	if (err < 0)
1288d29d41e2SJaska Uimonen 		return err;
12898a978234SLiam Girdwood 
1290d29d41e2SJaska Uimonen 	return 0;
12918a978234SLiam Girdwood }
12928a978234SLiam Girdwood 
12938a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
12948a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
12958a978234SLiam Girdwood {
12968a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
12978a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
12988a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
12998a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1300b9c035aaSJaska Uimonen 	unsigned int *kcontrol_type = NULL;
1301d29d41e2SJaska Uimonen 	struct snd_kcontrol_new *kc;
1302d29d41e2SJaska Uimonen 	int mixer_count = 0;
1303d29d41e2SJaska Uimonen 	int bytes_count = 0;
1304d29d41e2SJaska Uimonen 	int enum_count = 0;
13058a978234SLiam Girdwood 	int ret = 0;
1306d29d41e2SJaska Uimonen 	int i;
13078a978234SLiam Girdwood 
13088a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13098a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13108a978234SLiam Girdwood 		return -EINVAL;
13118a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13128a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13138a978234SLiam Girdwood 		return -EINVAL;
13148a978234SLiam Girdwood 
13158a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
13168a978234SLiam Girdwood 		w->name, w->id);
13178a978234SLiam Girdwood 
13188a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
13198a978234SLiam Girdwood 
13208a978234SLiam Girdwood 	/* map user to kernel widget ID */
13215aebe7c7SPierre-Louis Bossart 	template.id = get_widget_id(le32_to_cpu(w->id));
1322752c938aSDan Carpenter 	if ((int)template.id < 0)
13238a978234SLiam Girdwood 		return template.id;
13248a978234SLiam Girdwood 
1325c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
13268a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
13278a978234SLiam Girdwood 	if (!template.name)
13288a978234SLiam Girdwood 		return -ENOMEM;
13298a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
13308a978234SLiam Girdwood 	if (!template.sname) {
13318a978234SLiam Girdwood 		ret = -ENOMEM;
13328a978234SLiam Girdwood 		goto err;
13338a978234SLiam Girdwood 	}
13345aebe7c7SPierre-Louis Bossart 	template.reg = le32_to_cpu(w->reg);
13355aebe7c7SPierre-Louis Bossart 	template.shift = le32_to_cpu(w->shift);
13365aebe7c7SPierre-Louis Bossart 	template.mask = le32_to_cpu(w->mask);
13375aebe7c7SPierre-Louis Bossart 	template.subseq = le32_to_cpu(w->subseq);
13388a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
13398a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
13405aebe7c7SPierre-Louis Bossart 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
13415aebe7c7SPierre-Louis Bossart 	template.event_flags = le16_to_cpu(w->event_flags);
13428a978234SLiam Girdwood 	template.dobj.index = tplg->index;
13438a978234SLiam Girdwood 
13448a978234SLiam Girdwood 	tplg->pos +=
13455aebe7c7SPierre-Louis Bossart 		(sizeof(struct snd_soc_tplg_dapm_widget) +
13465aebe7c7SPierre-Louis Bossart 		 le32_to_cpu(w->priv.size));
13475aebe7c7SPierre-Louis Bossart 
13488a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
13498a978234SLiam Girdwood 		template.num_kcontrols = 0;
13508a978234SLiam Girdwood 		goto widget;
13518a978234SLiam Girdwood 	}
13528a978234SLiam Girdwood 
1353d29d41e2SJaska Uimonen 	template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1354d29d41e2SJaska Uimonen 	kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1355c173ee5bSAmadeusz Sławiński 	if (!kc) {
1356c173ee5bSAmadeusz Sławiński 		ret = -ENOMEM;
13579c363532SPeter Ujfalusi 		goto hdr_err;
1358c173ee5bSAmadeusz Sławiński 	}
1359d29d41e2SJaska Uimonen 
1360d29d41e2SJaska Uimonen 	kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1361d29d41e2SJaska Uimonen 				     GFP_KERNEL);
1362c173ee5bSAmadeusz Sławiński 	if (!kcontrol_type) {
1363c173ee5bSAmadeusz Sławiński 		ret = -ENOMEM;
13649c363532SPeter Ujfalusi 		goto hdr_err;
1365c173ee5bSAmadeusz Sławiński 	}
1366d29d41e2SJaska Uimonen 
13671baad7daSPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1368d29d41e2SJaska Uimonen 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
13695aebe7c7SPierre-Louis Bossart 		switch (le32_to_cpu(control_hdr->ops.info)) {
13708a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
13718a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
13728a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
13738a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
13748a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
13758a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1376d29d41e2SJaska Uimonen 			/* volume mixer */
1377d29d41e2SJaska Uimonen 			kc[i].index = mixer_count;
1378d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1379d29d41e2SJaska Uimonen 			mixer_count++;
1380d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]);
1381d29d41e2SJaska Uimonen 			if (ret < 0)
13828a978234SLiam Girdwood 				goto hdr_err;
13838a978234SLiam Girdwood 			break;
13848a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
13858a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
13868a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
13878a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
13888a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1389d29d41e2SJaska Uimonen 			/* enumerated mixer */
1390d29d41e2SJaska Uimonen 			kc[i].index = enum_count;
1391d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1392d29d41e2SJaska Uimonen 			enum_count++;
1393d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]);
1394d29d41e2SJaska Uimonen 			if (ret < 0)
13958a978234SLiam Girdwood 				goto hdr_err;
13968a978234SLiam Girdwood 			break;
13978a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
1398d29d41e2SJaska Uimonen 			/* bytes control */
1399d29d41e2SJaska Uimonen 			kc[i].index = bytes_count;
1400d29d41e2SJaska Uimonen 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1401d29d41e2SJaska Uimonen 			bytes_count++;
1402d29d41e2SJaska Uimonen 			ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]);
1403d29d41e2SJaska Uimonen 			if (ret < 0)
14048a978234SLiam Girdwood 				goto hdr_err;
14058a978234SLiam Girdwood 			break;
14068a978234SLiam Girdwood 		default:
14078a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
14088a978234SLiam Girdwood 				control_hdr->ops.get, control_hdr->ops.put,
14095aebe7c7SPierre-Louis Bossart 				le32_to_cpu(control_hdr->ops.info));
14108a978234SLiam Girdwood 			ret = -EINVAL;
14118a978234SLiam Girdwood 			goto hdr_err;
14128a978234SLiam Girdwood 		}
1413d29d41e2SJaska Uimonen 	}
1414d29d41e2SJaska Uimonen 
1415d29d41e2SJaska Uimonen 	template.kcontrol_news = kc;
14168facf84bSPeter Ujfalusi 	dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
14178facf84bSPeter Ujfalusi 		w->name, mixer_count, enum_count, bytes_count);
14188a978234SLiam Girdwood 
14198a978234SLiam Girdwood widget:
14208a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
14218a978234SLiam Girdwood 	if (ret < 0)
14228a978234SLiam Girdwood 		goto hdr_err;
14238a978234SLiam Girdwood 
14248a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
14258a978234SLiam Girdwood 	 * data during sound card init. */
14262b34c135SKuninori Morimoto 	if (snd_soc_card_is_instantiated(card))
14278a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
14288a978234SLiam Girdwood 	else
14298a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
143037e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
143137e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
14328a978234SLiam Girdwood 		goto hdr_err;
14338a978234SLiam Girdwood 	}
14348a978234SLiam Girdwood 
14358a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1436eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
143731e92739SAmadeusz Sławiński 	if (tplg->ops)
143831e92739SAmadeusz Sławiński 		widget->dobj.unload = tplg->ops->widget_unload;
14398a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
14408a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1441ebd259d3SLiam Girdwood 
1442ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1443ebd259d3SLiam Girdwood 	if (ret < 0)
1444ebd259d3SLiam Girdwood 		goto ready_err;
1445ebd259d3SLiam Girdwood 
14467620fe91SBard liao 	kfree(template.sname);
14477620fe91SBard liao 	kfree(template.name);
14487620fe91SBard liao 
14498a978234SLiam Girdwood 	return 0;
14508a978234SLiam Girdwood 
1451ebd259d3SLiam Girdwood ready_err:
14522abfd4bdSAmadeusz Sławiński 	soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1453ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
14548a978234SLiam Girdwood hdr_err:
14558a978234SLiam Girdwood 	kfree(template.sname);
14568a978234SLiam Girdwood err:
14578a978234SLiam Girdwood 	kfree(template.name);
14588a978234SLiam Girdwood 	return ret;
14598a978234SLiam Girdwood }
14608a978234SLiam Girdwood 
14618a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
14628a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
14638a978234SLiam Girdwood {
1464e9aa139fSKuninori Morimoto 	int count, i;
14655aebe7c7SPierre-Louis Bossart 
14665aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
14678a978234SLiam Girdwood 
14688a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
14698a978234SLiam Girdwood 
14708a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
1471e9aa139fSKuninori Morimoto 		struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1472e9aa139fSKuninori Morimoto 		int ret;
1473e9aa139fSKuninori Morimoto 
14742e288333SAmadeusz Sławiński 		/*
14752e288333SAmadeusz Sławiński 		 * check if widget itself fits within topology file
14762e288333SAmadeusz Sławiński 		 * use sizeof instead of widget->size, as we can't be sure
14772e288333SAmadeusz Sławiński 		 * it is set properly yet (file may end before it is present)
14782e288333SAmadeusz Sławiński 		 */
14792e288333SAmadeusz Sławiński 		if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
14802e288333SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid widget data size\n");
14812e288333SAmadeusz Sławiński 			return -EINVAL;
14822e288333SAmadeusz Sławiński 		}
14832e288333SAmadeusz Sławiński 
14842e288333SAmadeusz Sławiński 		/* check if widget has proper size */
14855aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
148606eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
148706eb49f7SMengdong Lin 			return -EINVAL;
148806eb49f7SMengdong Lin 		}
148906eb49f7SMengdong Lin 
14902e288333SAmadeusz Sławiński 		/* check if widget private data fits within topology file */
14912e288333SAmadeusz Sławiński 		if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
14922e288333SAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
14932e288333SAmadeusz Sławiński 			return -EINVAL;
14942e288333SAmadeusz Sławiński 		}
14952e288333SAmadeusz Sławiński 
14968a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
14977de76b62SMengdong Lin 		if (ret < 0) {
14988a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
14998a978234SLiam Girdwood 				widget->name);
15007de76b62SMengdong Lin 			return ret;
15017de76b62SMengdong Lin 		}
15028a978234SLiam Girdwood 	}
15038a978234SLiam Girdwood 
15048a978234SLiam Girdwood 	return 0;
15058a978234SLiam Girdwood }
15068a978234SLiam Girdwood 
15078a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
15088a978234SLiam Girdwood {
15098a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
15108a978234SLiam Girdwood 	int ret;
15118a978234SLiam Girdwood 
15128a978234SLiam Girdwood 	/* Card might not have been registered at this point.
15138a978234SLiam Girdwood 	 * If so, just return success.
15148a978234SLiam Girdwood 	*/
15152b34c135SKuninori Morimoto 	if (!snd_soc_card_is_instantiated(card)) {
1516*53085402SAmadeusz Sławiński 		dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
15178a978234SLiam Girdwood 		return 0;
15188a978234SLiam Girdwood 	}
15198a978234SLiam Girdwood 
15208a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
15218a978234SLiam Girdwood 	if (ret < 0)
1522*53085402SAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
15238a978234SLiam Girdwood 
1524b784617aSAmadeusz Sławiński 	return ret;
15258a978234SLiam Girdwood }
15268a978234SLiam Girdwood 
1527ff922622SAmadeusz Sławiński static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1528b6b6e4d6SMengdong Lin 			   struct snd_soc_tplg_stream_caps *caps)
1529b6b6e4d6SMengdong Lin {
1530ff922622SAmadeusz Sławiński 	stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1531abc3caacSAmadeusz Sławiński 	if (!stream->stream_name)
1532abc3caacSAmadeusz Sławiński 		return -ENOMEM;
1533abc3caacSAmadeusz Sławiński 
15345aebe7c7SPierre-Louis Bossart 	stream->channels_min = le32_to_cpu(caps->channels_min);
15355aebe7c7SPierre-Louis Bossart 	stream->channels_max = le32_to_cpu(caps->channels_max);
15365aebe7c7SPierre-Louis Bossart 	stream->rates = le32_to_cpu(caps->rates);
15375aebe7c7SPierre-Louis Bossart 	stream->rate_min = le32_to_cpu(caps->rate_min);
15385aebe7c7SPierre-Louis Bossart 	stream->rate_max = le32_to_cpu(caps->rate_max);
15395aebe7c7SPierre-Louis Bossart 	stream->formats = le64_to_cpu(caps->formats);
15405aebe7c7SPierre-Louis Bossart 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1541abc3caacSAmadeusz Sławiński 
1542abc3caacSAmadeusz Sławiński 	return 0;
1543b6b6e4d6SMengdong Lin }
1544b6b6e4d6SMengdong Lin 
15450038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
15460038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
15470038be9aSMengdong Lin {
15480038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1549f14654ddSKuninori Morimoto 		dai_drv->symmetric_rate =
155047108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
15510038be9aSMengdong Lin 
15520038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
15530038be9aSMengdong Lin 		dai_drv->symmetric_channels =
155447108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
15550038be9aSMengdong Lin 			1 : 0;
15560038be9aSMengdong Lin 
15570038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1558f14654ddSKuninori Morimoto 		dai_drv->symmetric_sample_bits =
155947108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
15600038be9aSMengdong Lin 			1 : 0;
15610038be9aSMengdong Lin }
15620038be9aSMengdong Lin 
156364527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
156464527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
156564527e8aSMengdong Lin {
156664527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
156764527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
156864527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
1569e443c205SKuninori Morimoto 	struct snd_soc_dai *dai;
1570e443c205SKuninori Morimoto 	struct snd_soc_dapm_context *dapm =
1571e443c205SKuninori Morimoto 		snd_soc_component_get_dapm(tplg->comp);
157264527e8aSMengdong Lin 	int ret;
157364527e8aSMengdong Lin 
1574ff922622SAmadeusz Sławiński 	dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
157564527e8aSMengdong Lin 	if (dai_drv == NULL)
157664527e8aSMengdong Lin 		return -ENOMEM;
157764527e8aSMengdong Lin 
1578abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
1579ff922622SAmadeusz Sławiński 		dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1580abc3caacSAmadeusz Sławiński 		if (!dai_drv->name) {
1581abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1582abc3caacSAmadeusz Sławiński 			goto err;
1583abc3caacSAmadeusz Sławiński 		}
1584abc3caacSAmadeusz Sławiński 	}
15855aebe7c7SPierre-Louis Bossart 	dai_drv->id = le32_to_cpu(pcm->dai_id);
158664527e8aSMengdong Lin 
158764527e8aSMengdong Lin 	if (pcm->playback) {
158864527e8aSMengdong Lin 		stream = &dai_drv->playback;
158964527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1590ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
1591abc3caacSAmadeusz Sławiński 		if (ret < 0)
1592abc3caacSAmadeusz Sławiński 			goto err;
159364527e8aSMengdong Lin 	}
159464527e8aSMengdong Lin 
159564527e8aSMengdong Lin 	if (pcm->capture) {
159664527e8aSMengdong Lin 		stream = &dai_drv->capture;
159764527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1598ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
1599abc3caacSAmadeusz Sławiński 		if (ret < 0)
1600abc3caacSAmadeusz Sławiński 			goto err;
160164527e8aSMengdong Lin 	}
160264527e8aSMengdong Lin 
16035db6aab6SLiam Girdwood 	if (pcm->compress)
16045db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
16055db6aab6SLiam Girdwood 
160664527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1607c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
160864527e8aSMengdong Lin 	if (ret < 0) {
1609e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1610abc3caacSAmadeusz Sławiński 		goto err;
161164527e8aSMengdong Lin 	}
161264527e8aSMengdong Lin 
161364527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
161464527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
161531e92739SAmadeusz Sławiński 	if (tplg->ops)
161631e92739SAmadeusz Sławiński 		dai_drv->dobj.unload = tplg->ops->dai_unload;
161764527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
161864527e8aSMengdong Lin 
161964527e8aSMengdong Lin 	/* register the DAI to the component */
1620fc4cb1e1SAmadeusz Sławiński 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1621e443c205SKuninori Morimoto 	if (!dai)
1622e443c205SKuninori Morimoto 		return -ENOMEM;
1623e443c205SKuninori Morimoto 
1624e443c205SKuninori Morimoto 	/* Create the DAI widgets here */
1625e443c205SKuninori Morimoto 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1626e443c205SKuninori Morimoto 	if (ret != 0) {
1627e443c205SKuninori Morimoto 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1628fc4cb1e1SAmadeusz Sławiński 		snd_soc_unregister_dai(dai);
1629e443c205SKuninori Morimoto 		return ret;
1630e443c205SKuninori Morimoto 	}
1631e443c205SKuninori Morimoto 
1632abc3caacSAmadeusz Sławiński 	return 0;
1633abc3caacSAmadeusz Sławiński 
1634abc3caacSAmadeusz Sławiński err:
1635e443c205SKuninori Morimoto 	return ret;
163664527e8aSMengdong Lin }
163764527e8aSMengdong Lin 
1638717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1639717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1640717a8e72SMengdong Lin {
1641717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1642f14654ddSKuninori Morimoto 		link->symmetric_rate =
164347108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1644717a8e72SMengdong Lin 
1645717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1646717a8e72SMengdong Lin 		link->symmetric_channels =
164747108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1648717a8e72SMengdong Lin 			1 : 0;
1649717a8e72SMengdong Lin 
1650717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1651f14654ddSKuninori Morimoto 		link->symmetric_sample_bits =
165247108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1653717a8e72SMengdong Lin 			1 : 0;
16546ff67ccaSMengdong Lin 
16556ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
16566ff67ccaSMengdong Lin 		link->ignore_suspend =
165747108a61SPierre-Louis Bossart 			(flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
16586ff67ccaSMengdong Lin 			1 : 0;
1659717a8e72SMengdong Lin }
1660717a8e72SMengdong Lin 
166167d1c21eSGuneshwor Singh /* create the FE DAI link */
1662ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1663acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1664acfc7d46SMengdong Lin {
1665acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
166623b946ceSKuninori Morimoto 	struct snd_soc_dai_link_component *dlc;
1667acfc7d46SMengdong Lin 	int ret;
1668acfc7d46SMengdong Lin 
16696a7c51b4SKuninori Morimoto 	/* link + cpu + codec + platform */
16706a7c51b4SKuninori Morimoto 	link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1671acfc7d46SMengdong Lin 	if (link == NULL)
1672acfc7d46SMengdong Lin 		return -ENOMEM;
1673acfc7d46SMengdong Lin 
167423b946ceSKuninori Morimoto 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
167523b946ceSKuninori Morimoto 
167623b946ceSKuninori Morimoto 	link->cpus	= &dlc[0];
167723b946ceSKuninori Morimoto 	link->num_cpus	 = 1;
167823b946ceSKuninori Morimoto 
16798ce1cbd6SJaroslav Kysela 	link->dobj.index = tplg->index;
16808ce1cbd6SJaroslav Kysela 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
168131e92739SAmadeusz Sławiński 	if (tplg->ops)
168231e92739SAmadeusz Sławiński 		link->dobj.unload = tplg->ops->link_unload;
16838ce1cbd6SJaroslav Kysela 
16848f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
1685ff922622SAmadeusz Sławiński 		link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1686ff922622SAmadeusz Sławiński 		link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1687abc3caacSAmadeusz Sławiński 		if (!link->name || !link->stream_name) {
1688abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1689abc3caacSAmadeusz Sławiński 			goto err;
1690abc3caacSAmadeusz Sławiński 		}
16918f27c4abSMengdong Lin 	}
16925aebe7c7SPierre-Louis Bossart 	link->id = le32_to_cpu(pcm->pcm_id);
1693acfc7d46SMengdong Lin 
1694abc3caacSAmadeusz Sławiński 	if (strlen(pcm->dai_name)) {
1695ff922622SAmadeusz Sławiński 		link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1696abc3caacSAmadeusz Sławiński 		if (!link->cpus->dai_name) {
1697abc3caacSAmadeusz Sławiński 			ret = -ENOMEM;
1698abc3caacSAmadeusz Sławiński 			goto err;
1699abc3caacSAmadeusz Sławiński 		}
1700abc3caacSAmadeusz Sławiński 	}
17018f27c4abSMengdong Lin 
17025a7bec81SKuninori Morimoto 	/*
17035a7bec81SKuninori Morimoto 	 * Many topology are assuming link has Codec / Platform, and
17045a7bec81SKuninori Morimoto 	 * these might be overwritten at soc_tplg_dai_link_load().
17055a7bec81SKuninori Morimoto 	 * Don't use &asoc_dummy_dlc here.
17065a7bec81SKuninori Morimoto 	 */
17075a7bec81SKuninori Morimoto 	link->codecs		= &dlc[1];	/* Don't use &asoc_dummy_dlc here */
170823b946ceSKuninori Morimoto 	link->codecs->name	= "snd-soc-dummy";
170923b946ceSKuninori Morimoto 	link->codecs->dai_name	= "snd-soc-dummy-dai";
17105a7bec81SKuninori Morimoto 	link->num_codecs	= 1;
171167d1c21eSGuneshwor Singh 
17125a7bec81SKuninori Morimoto 	link->platforms		= &dlc[2];	/* Don't use &asoc_dummy_dlc here */
17136a7c51b4SKuninori Morimoto 	link->platforms->name	= "snd-soc-dummy";
17146a7c51b4SKuninori Morimoto 	link->num_platforms	= 1;
17156a7c51b4SKuninori Morimoto 
171667d1c21eSGuneshwor Singh 	/* enable DPCM */
171767d1c21eSGuneshwor Singh 	link->dynamic = 1;
1718c403dcd8SKuninori Morimoto 	link->ignore_pmdown_time = 1;
17195aebe7c7SPierre-Louis Bossart 	link->dpcm_playback = le32_to_cpu(pcm->playback);
17205aebe7c7SPierre-Louis Bossart 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1721717a8e72SMengdong Lin 	if (pcm->flag_mask)
17225aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
17235aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flag_mask),
17245aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(pcm->flags));
172567d1c21eSGuneshwor Singh 
1726acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1727c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1728acfc7d46SMengdong Lin 	if (ret < 0) {
1729e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: FE link loading failed\n");
173076d27036SDragos Tarcatu 		goto err;
173176d27036SDragos Tarcatu 	}
173276d27036SDragos Tarcatu 
1733ffaf886eSKuninori Morimoto 	ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
173476d27036SDragos Tarcatu 	if (ret < 0) {
1735e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: adding FE link failed\n");
173676d27036SDragos Tarcatu 		goto err;
1737acfc7d46SMengdong Lin 	}
1738acfc7d46SMengdong Lin 
1739acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1740acfc7d46SMengdong Lin 
1741acfc7d46SMengdong Lin 	return 0;
174276d27036SDragos Tarcatu err:
174376d27036SDragos Tarcatu 	return ret;
1744acfc7d46SMengdong Lin }
1745acfc7d46SMengdong Lin 
1746acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
174764527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
174864527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
174964527e8aSMengdong Lin {
1750acfc7d46SMengdong Lin 	int ret;
1751acfc7d46SMengdong Lin 
1752acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1753acfc7d46SMengdong Lin 	if (ret < 0)
1754acfc7d46SMengdong Lin 		return ret;
1755acfc7d46SMengdong Lin 
1756ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
175764527e8aSMengdong Lin }
175864527e8aSMengdong Lin 
175955726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
176055726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
176155726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
176255726dc9SMengdong Lin {
17635aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
176455726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
176555726dc9SMengdong Lin 	dest->formats = src->formats;
176655726dc9SMengdong Lin 	dest->rates = src->rates;
176755726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
176855726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
176955726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
177055726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
177155726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
177255726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
177355726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
177455726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
177555726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
177655726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
177755726dc9SMengdong Lin }
177855726dc9SMengdong Lin 
177955726dc9SMengdong Lin /**
178055726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
178155726dc9SMengdong Lin  * @tplg: topology context
178255726dc9SMengdong Lin  * @src: older version of pcm as a source
178355726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
178455726dc9SMengdong Lin  *
1785ce1f2571SColin Ian King  * Support from version 4. User should free the returned pcm manually.
178655726dc9SMengdong Lin  */
178755726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
178855726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
178955726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
179055726dc9SMengdong Lin {
179155726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
179255726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
179355726dc9SMengdong Lin 	int i;
179455726dc9SMengdong Lin 
179555726dc9SMengdong Lin 	*pcm = NULL;
179655726dc9SMengdong Lin 
17975aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
179855726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
179955726dc9SMengdong Lin 		return -EINVAL;
180055726dc9SMengdong Lin 	}
180155726dc9SMengdong Lin 
180255726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
180355726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
180455726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
180555726dc9SMengdong Lin 	if (!dest)
180655726dc9SMengdong Lin 		return -ENOMEM;
180755726dc9SMengdong Lin 
18085aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
180955726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
181055726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
181155726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
181255726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
181355726dc9SMengdong Lin 	dest->playback = src_v4->playback;
181455726dc9SMengdong Lin 	dest->capture = src_v4->capture;
181555726dc9SMengdong Lin 	dest->compress = src_v4->compress;
181655726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
18175aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
181855726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
181955726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
182055726dc9SMengdong Lin 
182155726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
182255726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
182355726dc9SMengdong Lin 
182455726dc9SMengdong Lin 	*pcm = dest;
182555726dc9SMengdong Lin 	return 0;
182655726dc9SMengdong Lin }
182755726dc9SMengdong Lin 
182864527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
18298a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
18308a978234SLiam Girdwood {
183155726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
18325aebe7c7SPierre-Louis Bossart 	int count;
18335aebe7c7SPierre-Louis Bossart 	int size;
1834fd340455SVinod Koul 	int i;
183555726dc9SMengdong Lin 	bool abi_match;
1836a3039aefSDragos Tarcatu 	int ret;
18378a978234SLiam Girdwood 
18385aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
18395aebe7c7SPierre-Louis Bossart 
184055726dc9SMengdong Lin 	/* check the element size and count */
184155726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
18425aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(pcm->size);
18435aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_pcm)
18445aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
184555726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
18465aebe7c7SPierre-Louis Bossart 			size);
184755726dc9SMengdong Lin 		return -EINVAL;
184855726dc9SMengdong Lin 	}
184955726dc9SMengdong Lin 
18508a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
18515aebe7c7SPierre-Louis Bossart 				      size, count,
18525aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
18533ce57f22SAmadeusz Sławiński 				      "PCM DAI"))
18548a978234SLiam Girdwood 		return -EINVAL;
18558a978234SLiam Girdwood 
185664527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
185755726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
18585aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(pcm->size);
185955726dc9SMengdong Lin 
186055726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
186155726dc9SMengdong Lin 		 * if abi not match.
186255726dc9SMengdong Lin 		 */
18635aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*pcm)) {
186455726dc9SMengdong Lin 			abi_match = true;
186555726dc9SMengdong Lin 			_pcm = pcm;
186655726dc9SMengdong Lin 		} else {
186755726dc9SMengdong Lin 			abi_match = false;
1868b3677fc3SAmadeusz Sławiński 			ret = pcm_new_ver(tplg, pcm, &_pcm);
1869b3677fc3SAmadeusz Sławiński 			if (ret < 0)
1870b3677fc3SAmadeusz Sławiński 				return ret;
187106eb49f7SMengdong Lin 		}
187206eb49f7SMengdong Lin 
187355726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
1874a3039aefSDragos Tarcatu 		ret = soc_tplg_pcm_create(tplg, _pcm);
1875a3039aefSDragos Tarcatu 		if (ret < 0) {
1876a3039aefSDragos Tarcatu 			if (!abi_match)
1877a3039aefSDragos Tarcatu 				kfree(_pcm);
1878a3039aefSDragos Tarcatu 			return ret;
1879a3039aefSDragos Tarcatu 		}
188055726dc9SMengdong Lin 
1881717a8e72SMengdong Lin 		/* offset by version-specific struct size and
1882717a8e72SMengdong Lin 		 * real priv data size
1883717a8e72SMengdong Lin 		 */
18845aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
1885717a8e72SMengdong Lin 
188655726dc9SMengdong Lin 		if (!abi_match)
188755726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
188864527e8aSMengdong Lin 	}
188964527e8aSMengdong Lin 
18908a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
18918a978234SLiam Girdwood 
18928a978234SLiam Girdwood 	return 0;
18938a978234SLiam Girdwood }
18948a978234SLiam Girdwood 
18950038be9aSMengdong Lin /**
1896593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
18978abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
1898593d9e52SMengdong Lin  * @cfg: physical link configs.
1899593d9e52SMengdong Lin  *
1900593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
1901593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
1902593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
1903593d9e52SMengdong Lin  */
1904593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
1905593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
1906593d9e52SMengdong Lin {
1907593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
1908f026c123SPierre-Louis Bossart 	unsigned char bclk_provider, fsync_provider;
1909593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
1910593d9e52SMengdong Lin 	int i;
1911593d9e52SMengdong Lin 
19125aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1913593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
1914593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
1915593d9e52SMengdong Lin 			continue;
1916593d9e52SMengdong Lin 
19175aebe7c7SPierre-Louis Bossart 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
19185aebe7c7SPierre-Louis Bossart 			SND_SOC_DAIFMT_FORMAT_MASK;
1919593d9e52SMengdong Lin 
1920933e1c4aSKirill Marinushkin 		/* clock gating */
1921fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
1922fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1923933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1924fbeabd09SKirill Marinushkin 			break;
1925fbeabd09SKirill Marinushkin 
1926fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1927933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1928fbeabd09SKirill Marinushkin 			break;
1929fbeabd09SKirill Marinushkin 
1930fbeabd09SKirill Marinushkin 		default:
1931fbeabd09SKirill Marinushkin 			/* ignore the value */
1932fbeabd09SKirill Marinushkin 			break;
1933fbeabd09SKirill Marinushkin 		}
1934933e1c4aSKirill Marinushkin 
1935593d9e52SMengdong Lin 		/* clock signal polarity */
1936593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
1937593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
1938593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
1939593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1940593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
1941593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1942593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
1943593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1944593d9e52SMengdong Lin 		else
1945593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1946593d9e52SMengdong Lin 
1947593d9e52SMengdong Lin 		/* clock masters */
1948f026c123SPierre-Louis Bossart 		bclk_provider = (hw_config->bclk_provider ==
1949f026c123SPierre-Louis Bossart 			       SND_SOC_TPLG_BCLK_CP);
1950f026c123SPierre-Louis Bossart 		fsync_provider = (hw_config->fsync_provider ==
1951f026c123SPierre-Louis Bossart 				SND_SOC_TPLG_FSYNC_CP);
1952f026c123SPierre-Louis Bossart 		if (bclk_provider && fsync_provider)
1953f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1954f026c123SPierre-Louis Bossart 		else if (!bclk_provider && fsync_provider)
1955f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1956f026c123SPierre-Louis Bossart 		else if (bclk_provider && !fsync_provider)
1957f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1958593d9e52SMengdong Lin 		else
1959f026c123SPierre-Louis Bossart 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1960593d9e52SMengdong Lin 	}
1961593d9e52SMengdong Lin }
1962593d9e52SMengdong Lin 
1963593d9e52SMengdong Lin /**
1964593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
1965593d9e52SMengdong Lin  * version of source.
19668abab35fSCharles Keepax  * @tplg: topology context
1967593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
1968593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
1969593d9e52SMengdong Lin  *
1970ce1f2571SColin Ian King  * Support from version 4. User need free the returned link config manually.
1971593d9e52SMengdong Lin  */
1972593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
1973593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
1974593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
1975593d9e52SMengdong Lin {
1976593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
1977593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
1978593d9e52SMengdong Lin 	int i;
1979593d9e52SMengdong Lin 
1980593d9e52SMengdong Lin 	*link = NULL;
1981593d9e52SMengdong Lin 
19825aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(src->size) !=
19835aebe7c7SPierre-Louis Bossart 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
1984593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
1985593d9e52SMengdong Lin 		return -EINVAL;
1986593d9e52SMengdong Lin 	}
1987593d9e52SMengdong Lin 
1988593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
1989593d9e52SMengdong Lin 
1990593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
1991593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1992593d9e52SMengdong Lin 	if (!dest)
1993593d9e52SMengdong Lin 		return -ENOMEM;
1994593d9e52SMengdong Lin 
19955aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest));
1996593d9e52SMengdong Lin 	dest->id = src_v4->id;
1997593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
19985aebe7c7SPierre-Louis Bossart 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
1999593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2000593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2001593d9e52SMengdong Lin 
2002593d9e52SMengdong Lin 	*link = dest;
2003593d9e52SMengdong Lin 	return 0;
2004593d9e52SMengdong Lin }
2005593d9e52SMengdong Lin 
2006d6f31e0eSKuninori Morimoto /**
2007d6f31e0eSKuninori Morimoto  * snd_soc_find_dai_link - Find a DAI link
2008d6f31e0eSKuninori Morimoto  *
2009d6f31e0eSKuninori Morimoto  * @card: soc card
2010d6f31e0eSKuninori Morimoto  * @id: DAI link ID to match
2011d6f31e0eSKuninori Morimoto  * @name: DAI link name to match, optional
2012d6f31e0eSKuninori Morimoto  * @stream_name: DAI link stream name to match, optional
2013d6f31e0eSKuninori Morimoto  *
2014d6f31e0eSKuninori Morimoto  * This function will search all existing DAI links of the soc card to
2015d6f31e0eSKuninori Morimoto  * find the link of the same ID. Since DAI links may not have their
2016d6f31e0eSKuninori Morimoto  * unique ID, so name and stream name should also match if being
2017d6f31e0eSKuninori Morimoto  * specified.
2018d6f31e0eSKuninori Morimoto  *
2019d6f31e0eSKuninori Morimoto  * Return: pointer of DAI link, or NULL if not found.
2020d6f31e0eSKuninori Morimoto  */
2021d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2022d6f31e0eSKuninori Morimoto 						      int id, const char *name,
2023d6f31e0eSKuninori Morimoto 						      const char *stream_name)
2024d6f31e0eSKuninori Morimoto {
2025d6f31e0eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd;
2026d6f31e0eSKuninori Morimoto 
2027d6f31e0eSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
2028b81e8efaSKuninori Morimoto 		struct snd_soc_dai_link *link = rtd->dai_link;
2029d6f31e0eSKuninori Morimoto 
2030d6f31e0eSKuninori Morimoto 		if (link->id != id)
2031d6f31e0eSKuninori Morimoto 			continue;
2032d6f31e0eSKuninori Morimoto 
2033d6f31e0eSKuninori Morimoto 		if (name && (!link->name || strcmp(name, link->name)))
2034d6f31e0eSKuninori Morimoto 			continue;
2035d6f31e0eSKuninori Morimoto 
2036d6f31e0eSKuninori Morimoto 		if (stream_name && (!link->stream_name
2037d6f31e0eSKuninori Morimoto 				    || strcmp(stream_name, link->stream_name)))
2038d6f31e0eSKuninori Morimoto 			continue;
2039d6f31e0eSKuninori Morimoto 
2040d6f31e0eSKuninori Morimoto 		return link;
2041d6f31e0eSKuninori Morimoto 	}
2042d6f31e0eSKuninori Morimoto 
2043d6f31e0eSKuninori Morimoto 	return NULL;
2044d6f31e0eSKuninori Morimoto }
2045d6f31e0eSKuninori Morimoto 
2046593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2047593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2048593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2049593d9e52SMengdong Lin {
2050593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2051593d9e52SMengdong Lin 	const char *name, *stream_name;
2052dbab1cb8SMengdong Lin 	size_t len;
2053593d9e52SMengdong Lin 	int ret;
2054593d9e52SMengdong Lin 
2055dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2056dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2057dbab1cb8SMengdong Lin 		return -EINVAL;
2058dbab1cb8SMengdong Lin 	else if (len)
2059dbab1cb8SMengdong Lin 		name = cfg->name;
2060dbab1cb8SMengdong Lin 	else
2061dbab1cb8SMengdong Lin 		name = NULL;
2062dbab1cb8SMengdong Lin 
2063dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2064dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2065dbab1cb8SMengdong Lin 		return -EINVAL;
2066dbab1cb8SMengdong Lin 	else if (len)
2067dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2068dbab1cb8SMengdong Lin 	else
2069dbab1cb8SMengdong Lin 		stream_name = NULL;
2070593d9e52SMengdong Lin 
20715aebe7c7SPierre-Louis Bossart 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2072593d9e52SMengdong Lin 				     name, stream_name);
2073593d9e52SMengdong Lin 	if (!link) {
2074593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2075593d9e52SMengdong Lin 			name, cfg->id);
2076593d9e52SMengdong Lin 		return -EINVAL;
2077593d9e52SMengdong Lin 	}
2078593d9e52SMengdong Lin 
2079593d9e52SMengdong Lin 	/* hw format */
2080593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2081593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2082593d9e52SMengdong Lin 
2083593d9e52SMengdong Lin 	/* flags */
2084593d9e52SMengdong Lin 	if (cfg->flag_mask)
20855aebe7c7SPierre-Louis Bossart 		set_link_flags(link,
20865aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flag_mask),
20875aebe7c7SPierre-Louis Bossart 			       le32_to_cpu(cfg->flags));
2088593d9e52SMengdong Lin 
2089593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2090c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2091593d9e52SMengdong Lin 	if (ret < 0) {
2092593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2093593d9e52SMengdong Lin 		return ret;
2094593d9e52SMengdong Lin 	}
2095593d9e52SMengdong Lin 
2096adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2097adfebb51SBard liao 	link->dobj.index = tplg->index;
2098adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
209931e92739SAmadeusz Sławiński 	if (tplg->ops)
210031e92739SAmadeusz Sławiński 		link->dobj.unload = tplg->ops->link_unload;
2101adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2102adfebb51SBard liao 
2103593d9e52SMengdong Lin 	return 0;
2104593d9e52SMengdong Lin }
2105593d9e52SMengdong Lin 
2106593d9e52SMengdong Lin 
2107593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2108593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2109593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2110593d9e52SMengdong Lin {
2111593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
21125aebe7c7SPierre-Louis Bossart 	int count;
21135aebe7c7SPierre-Louis Bossart 	int size;
2114593d9e52SMengdong Lin 	int i, ret;
2115593d9e52SMengdong Lin 	bool abi_match;
2116593d9e52SMengdong Lin 
21175aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
21185aebe7c7SPierre-Louis Bossart 
2119593d9e52SMengdong Lin 	/* check the element size and count */
2120593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
21215aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(link->size);
21225aebe7c7SPierre-Louis Bossart 	if (size > sizeof(struct snd_soc_tplg_link_config)
21235aebe7c7SPierre-Louis Bossart 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2124593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
21255aebe7c7SPierre-Louis Bossart 			size);
2126593d9e52SMengdong Lin 		return -EINVAL;
2127593d9e52SMengdong Lin 	}
2128593d9e52SMengdong Lin 
21293ce57f22SAmadeusz Sławiński 	if (soc_tplg_check_elem_count(tplg, size, count,
21305aebe7c7SPierre-Louis Bossart 				      le32_to_cpu(hdr->payload_size),
21313ce57f22SAmadeusz Sławiński 				      "physical link config"))
2132593d9e52SMengdong Lin 		return -EINVAL;
2133593d9e52SMengdong Lin 
2134593d9e52SMengdong Lin 	/* config physical DAI links */
2135593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2136593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
21375aebe7c7SPierre-Louis Bossart 		size = le32_to_cpu(link->size);
21385aebe7c7SPierre-Louis Bossart 		if (size == sizeof(*link)) {
2139593d9e52SMengdong Lin 			abi_match = true;
2140593d9e52SMengdong Lin 			_link = link;
2141593d9e52SMengdong Lin 		} else {
2142593d9e52SMengdong Lin 			abi_match = false;
2143593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2144593d9e52SMengdong Lin 			if (ret < 0)
2145593d9e52SMengdong Lin 				return ret;
2146593d9e52SMengdong Lin 		}
2147593d9e52SMengdong Lin 
2148593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
21492b2d5c4dSDragos Tarcatu 		if (ret < 0) {
21502b2d5c4dSDragos Tarcatu 			if (!abi_match)
21512b2d5c4dSDragos Tarcatu 				kfree(_link);
2152593d9e52SMengdong Lin 			return ret;
21532b2d5c4dSDragos Tarcatu 		}
2154593d9e52SMengdong Lin 
2155593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2156593d9e52SMengdong Lin 		 * real priv data size
2157593d9e52SMengdong Lin 		 */
21585aebe7c7SPierre-Louis Bossart 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2159593d9e52SMengdong Lin 
2160593d9e52SMengdong Lin 		if (!abi_match)
2161593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2162593d9e52SMengdong Lin 	}
2163593d9e52SMengdong Lin 
2164593d9e52SMengdong Lin 	return 0;
2165593d9e52SMengdong Lin }
2166593d9e52SMengdong Lin 
2167593d9e52SMengdong Lin /**
21689aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
21690038be9aSMengdong Lin  * @tplg: topology context
21709aa3f034SMengdong Lin  * @d: physical DAI configs.
21710038be9aSMengdong Lin  *
21729aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
21739aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
21740038be9aSMengdong Lin  */
21759aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
21769aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
21770038be9aSMengdong Lin {
21785aebe7c7SPierre-Louis Bossart 	struct snd_soc_dai_link_component dai_component;
21790038be9aSMengdong Lin 	struct snd_soc_dai *dai;
21800038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
21810038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
21820038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
21830038be9aSMengdong Lin 	int ret;
21840038be9aSMengdong Lin 
21855aebe7c7SPierre-Louis Bossart 	memset(&dai_component, 0, sizeof(dai_component));
21865aebe7c7SPierre-Louis Bossart 
21879aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
21880038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
21890038be9aSMengdong Lin 	if (!dai) {
21909aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
21919aa3f034SMengdong Lin 			d->dai_name);
21920038be9aSMengdong Lin 		return -EINVAL;
21930038be9aSMengdong Lin 	}
21940038be9aSMengdong Lin 
21955aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(d->dai_id) != dai->id) {
21969aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
21979aa3f034SMengdong Lin 			d->dai_name);
21980038be9aSMengdong Lin 		return -EINVAL;
21990038be9aSMengdong Lin 	}
22000038be9aSMengdong Lin 
22010038be9aSMengdong Lin 	dai_drv = dai->driver;
22020038be9aSMengdong Lin 	if (!dai_drv)
22030038be9aSMengdong Lin 		return -EINVAL;
22040038be9aSMengdong Lin 
22059aa3f034SMengdong Lin 	if (d->playback) {
22060038be9aSMengdong Lin 		stream = &dai_drv->playback;
22079aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2208ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
2209abc3caacSAmadeusz Sławiński 		if (ret < 0)
2210abc3caacSAmadeusz Sławiński 			goto err;
22110038be9aSMengdong Lin 	}
22120038be9aSMengdong Lin 
22139aa3f034SMengdong Lin 	if (d->capture) {
22140038be9aSMengdong Lin 		stream = &dai_drv->capture;
22159aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2216ff922622SAmadeusz Sławiński 		ret = set_stream_info(tplg, stream, caps);
2217abc3caacSAmadeusz Sławiński 		if (ret < 0)
2218abc3caacSAmadeusz Sławiński 			goto err;
22190038be9aSMengdong Lin 	}
22200038be9aSMengdong Lin 
22219aa3f034SMengdong Lin 	if (d->flag_mask)
22225aebe7c7SPierre-Louis Bossart 		set_dai_flags(dai_drv,
22235aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flag_mask),
22245aebe7c7SPierre-Louis Bossart 			      le32_to_cpu(d->flags));
22250038be9aSMengdong Lin 
22260038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2227c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
22280038be9aSMengdong Lin 	if (ret < 0) {
2229e59db12bSAmadeusz Sławiński 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2230abc3caacSAmadeusz Sławiński 		goto err;
22310038be9aSMengdong Lin 	}
22320038be9aSMengdong Lin 
22330038be9aSMengdong Lin 	return 0;
2234abc3caacSAmadeusz Sławiński 
2235abc3caacSAmadeusz Sławiński err:
2236abc3caacSAmadeusz Sławiński 	return ret;
22370038be9aSMengdong Lin }
22380038be9aSMengdong Lin 
22399aa3f034SMengdong Lin /* load physical DAI elements */
22409aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
22410038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
22420038be9aSMengdong Lin {
22435aebe7c7SPierre-Louis Bossart 	int count;
224465a4cfddSKuninori Morimoto 	int i;
22450038be9aSMengdong Lin 
22465aebe7c7SPierre-Louis Bossart 	count = le32_to_cpu(hdr->count);
22475aebe7c7SPierre-Louis Bossart 
22480038be9aSMengdong Lin 	/* config the existing BE DAIs */
22490038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
225065a4cfddSKuninori Morimoto 		struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
225165a4cfddSKuninori Morimoto 		int ret;
225265a4cfddSKuninori Morimoto 
22535aebe7c7SPierre-Louis Bossart 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
22549aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
22550038be9aSMengdong Lin 			return -EINVAL;
22560038be9aSMengdong Lin 		}
22570038be9aSMengdong Lin 
2258dd8e871dSAmadeusz Sławiński 		ret = soc_tplg_dai_config(tplg, dai);
2259dd8e871dSAmadeusz Sławiński 		if (ret < 0) {
2260dd8e871dSAmadeusz Sławiński 			dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2261dd8e871dSAmadeusz Sławiński 			return ret;
2262dd8e871dSAmadeusz Sławiński 		}
2263dd8e871dSAmadeusz Sławiński 
22645aebe7c7SPierre-Louis Bossart 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
22650038be9aSMengdong Lin 	}
22660038be9aSMengdong Lin 
22670038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
22680038be9aSMengdong Lin 	return 0;
22690038be9aSMengdong Lin }
22700038be9aSMengdong Lin 
2271583958faSMengdong Lin /**
2272583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2273583958faSMengdong Lin  * of source.
22748abab35fSCharles Keepax  * @tplg: topology context
2275583958faSMengdong Lin  * @src: old version of manifest as a source
2276583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2277583958faSMengdong Lin  *
2278ce1f2571SColin Ian King  * Support from version 4. Users need free the returned manifest manually.
2279583958faSMengdong Lin  */
2280583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2281583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2282583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2283583958faSMengdong Lin {
2284583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2285583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
22865aebe7c7SPierre-Louis Bossart 	int size;
2287583958faSMengdong Lin 
2288583958faSMengdong Lin 	*manifest = NULL;
2289583958faSMengdong Lin 
22905aebe7c7SPierre-Louis Bossart 	size = le32_to_cpu(src->size);
22915aebe7c7SPierre-Louis Bossart 	if (size != sizeof(*src_v4)) {
2292ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
22935aebe7c7SPierre-Louis Bossart 			 size);
22945aebe7c7SPierre-Louis Bossart 		if (size)
2295583958faSMengdong Lin 			return -EINVAL;
22965aebe7c7SPierre-Louis Bossart 		src->size = cpu_to_le32(sizeof(*src_v4));
2297583958faSMengdong Lin 	}
2298583958faSMengdong Lin 
2299583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2300583958faSMengdong Lin 
2301583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
23025aebe7c7SPierre-Louis Bossart 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
23035aebe7c7SPierre-Louis Bossart 		       GFP_KERNEL);
2304583958faSMengdong Lin 	if (!dest)
2305583958faSMengdong Lin 		return -ENOMEM;
2306583958faSMengdong Lin 
23075aebe7c7SPierre-Louis Bossart 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2308583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2309583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2310583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2311583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2312583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2313583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2314583958faSMengdong Lin 	if (dest->priv.size)
2315583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
23165aebe7c7SPierre-Louis Bossart 		       le32_to_cpu(src_v4->priv.size));
2317583958faSMengdong Lin 
2318583958faSMengdong Lin 	*manifest = dest;
2319583958faSMengdong Lin 	return 0;
2320583958faSMengdong Lin }
23210038be9aSMengdong Lin 
23228a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
23238a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
23248a978234SLiam Girdwood {
2325583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2326583958faSMengdong Lin 	bool abi_match;
2327242c46c0SDragos Tarcatu 	int ret = 0;
23288a978234SLiam Girdwood 
23298a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2330583958faSMengdong Lin 
2331583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
23325aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2333583958faSMengdong Lin 		abi_match = true;
2334583958faSMengdong Lin 		_manifest = manifest;
2335583958faSMengdong Lin 	} else {
2336583958faSMengdong Lin 		abi_match = false;
233786e2d14bSCezary Rojewski 
2338242c46c0SDragos Tarcatu 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2339242c46c0SDragos Tarcatu 		if (ret < 0)
2340242c46c0SDragos Tarcatu 			return ret;
234106eb49f7SMengdong Lin 	}
234206eb49f7SMengdong Lin 
2343583958faSMengdong Lin 	/* pass control to component driver for optional further init */
2344c42464a4SAmadeusz Sławiński 	if (tplg->ops && tplg->ops->manifest)
2345242c46c0SDragos Tarcatu 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
23468a978234SLiam Girdwood 
2347583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2348583958faSMengdong Lin 		kfree(_manifest);
2349583958faSMengdong Lin 
2350242c46c0SDragos Tarcatu 	return ret;
23518a978234SLiam Girdwood }
23528a978234SLiam Girdwood 
23538a978234SLiam Girdwood /* validate header magic, size and type */
235423e591dcSAmadeusz Sławiński static int soc_tplg_valid_header(struct soc_tplg *tplg,
23558a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
23568a978234SLiam Girdwood {
23575aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
235806eb49f7SMengdong Lin 		dev_err(tplg->dev,
235906eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
23605aebe7c7SPierre-Louis Bossart 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
236106eb49f7SMengdong Lin 			tplg->fw->size);
236206eb49f7SMengdong Lin 		return -EINVAL;
236306eb49f7SMengdong Lin 	}
236406eb49f7SMengdong Lin 
2365c5d184c9SAmadeusz Sławiński 	if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
236686e2d14bSCezary Rojewski 		dev_err(tplg->dev,
236786e2d14bSCezary Rojewski 			"ASoC: invalid header of type %d at offset %ld payload_size %d\n",
236886e2d14bSCezary Rojewski 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
236986e2d14bSCezary Rojewski 			hdr->payload_size);
237086e2d14bSCezary Rojewski 		return -EINVAL;
237186e2d14bSCezary Rojewski 	}
237286e2d14bSCezary Rojewski 
23738a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
237426d87881SAmadeusz Sławiński 	if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
23758a978234SLiam Girdwood 		dev_err(tplg->dev,
23768a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
23778a978234SLiam Girdwood 			tplg->pass, hdr->magic,
23788a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
23798a978234SLiam Girdwood 		return -EINVAL;
23808a978234SLiam Girdwood 	}
23818a978234SLiam Girdwood 
23825aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
23838a978234SLiam Girdwood 		dev_err(tplg->dev,
23848a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
23858a978234SLiam Girdwood 			tplg->pass, hdr->magic,
23868a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
23878a978234SLiam Girdwood 		return -EINVAL;
23888a978234SLiam Girdwood 	}
23898a978234SLiam Girdwood 
2390288b8da7SMengdong Lin 	/* Support ABI from version 4 */
23915aebe7c7SPierre-Louis Bossart 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
23925aebe7c7SPierre-Louis Bossart 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
23938a978234SLiam Girdwood 		dev_err(tplg->dev,
23948a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
23958a978234SLiam Girdwood 			tplg->pass, hdr->abi,
23968a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
23978a978234SLiam Girdwood 			tplg->fw->size);
23988a978234SLiam Girdwood 		return -EINVAL;
23998a978234SLiam Girdwood 	}
24008a978234SLiam Girdwood 
24018a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
24028a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
24038a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
24048a978234SLiam Girdwood 		return -EINVAL;
24058a978234SLiam Girdwood 	}
24068a978234SLiam Girdwood 
2407d9b07b79SAmadeusz Sławiński 	return 0;
24088a978234SLiam Girdwood }
24098a978234SLiam Girdwood 
24108a978234SLiam Girdwood /* check header type and call appropriate handler */
24118a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
24128a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24138a978234SLiam Girdwood {
241482ed7418SKeyon Jie 	int (*elem_load)(struct soc_tplg *tplg,
241582ed7418SKeyon Jie 			 struct snd_soc_tplg_hdr *hdr);
241682ed7418SKeyon Jie 	unsigned int hdr_pass;
241782ed7418SKeyon Jie 
24188a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
24198a978234SLiam Girdwood 
24205aebe7c7SPierre-Louis Bossart 	tplg->index = le32_to_cpu(hdr->index);
24218a978234SLiam Girdwood 
24225aebe7c7SPierre-Louis Bossart 	switch (le32_to_cpu(hdr->type)) {
24238a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
24248a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
24258a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
24265e2cd47aSAmadeusz Sławiński 		hdr_pass = SOC_TPLG_PASS_CONTROL;
242782ed7418SKeyon Jie 		elem_load = soc_tplg_kcontrol_elems_load;
242882ed7418SKeyon Jie 		break;
24298a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
243082ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_GRAPH;
243182ed7418SKeyon Jie 		elem_load = soc_tplg_dapm_graph_elems_load;
243282ed7418SKeyon Jie 		break;
24338a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
243482ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_WIDGET;
243582ed7418SKeyon Jie 		elem_load = soc_tplg_dapm_widget_elems_load;
243682ed7418SKeyon Jie 		break;
24378a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
243882ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_PCM_DAI;
243982ed7418SKeyon Jie 		elem_load = soc_tplg_pcm_elems_load;
244082ed7418SKeyon Jie 		break;
24413fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
244282ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_BE_DAI;
244382ed7418SKeyon Jie 		elem_load = soc_tplg_dai_elems_load;
244482ed7418SKeyon Jie 		break;
2445593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2446593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2447593d9e52SMengdong Lin 		/* physical link configurations */
244882ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_LINK;
244982ed7418SKeyon Jie 		elem_load = soc_tplg_link_elems_load;
245082ed7418SKeyon Jie 		break;
24518a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
245282ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_MANIFEST;
245382ed7418SKeyon Jie 		elem_load = soc_tplg_manifest_load;
245482ed7418SKeyon Jie 		break;
24558a978234SLiam Girdwood 	default:
24568a978234SLiam Girdwood 		/* bespoke vendor data object */
245782ed7418SKeyon Jie 		hdr_pass = SOC_TPLG_PASS_VENDOR;
245882ed7418SKeyon Jie 		elem_load = soc_tplg_vendor_load;
245982ed7418SKeyon Jie 		break;
246082ed7418SKeyon Jie 	}
246182ed7418SKeyon Jie 
246282ed7418SKeyon Jie 	if (tplg->pass == hdr_pass) {
246382ed7418SKeyon Jie 		dev_dbg(tplg->dev,
246482ed7418SKeyon Jie 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
246582ed7418SKeyon Jie 			hdr->payload_size, hdr->type, hdr->version,
246682ed7418SKeyon Jie 			hdr->vendor_type, tplg->pass);
246782ed7418SKeyon Jie 		return elem_load(tplg, hdr);
24688a978234SLiam Girdwood 	}
24698a978234SLiam Girdwood 
24708a978234SLiam Girdwood 	return 0;
24718a978234SLiam Girdwood }
24728a978234SLiam Girdwood 
24738a978234SLiam Girdwood /* process the topology file headers */
24748a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
24758a978234SLiam Girdwood {
24768a978234SLiam Girdwood 	int ret;
24778a978234SLiam Girdwood 
24788a978234SLiam Girdwood 	/* process the header types from start to end */
2479395f8fd6SAmadeusz Sławiński 	for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2480f79e4b2aSKuninori Morimoto 		struct snd_soc_tplg_hdr *hdr;
24818a978234SLiam Girdwood 
24828a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
24838a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
24848a978234SLiam Girdwood 
24858a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
24868a978234SLiam Girdwood 
24878a978234SLiam Girdwood 			/* make sure header is valid before loading */
248823e591dcSAmadeusz Sławiński 			ret = soc_tplg_valid_header(tplg, hdr);
24898bf9475fSPierre-Louis Bossart 			if (ret < 0) {
24908bf9475fSPierre-Louis Bossart 				dev_err(tplg->dev,
24918bf9475fSPierre-Louis Bossart 					"ASoC: topology: invalid header: %d\n", ret);
24928a978234SLiam Girdwood 				return ret;
24938bf9475fSPierre-Louis Bossart 			}
24948a978234SLiam Girdwood 
24958a978234SLiam Girdwood 			/* load the header object */
24968a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
24978bf9475fSPierre-Louis Bossart 			if (ret < 0) {
24988bf9475fSPierre-Louis Bossart 				dev_err(tplg->dev,
24998bf9475fSPierre-Louis Bossart 					"ASoC: topology: could not load header: %d\n", ret);
25008a978234SLiam Girdwood 				return ret;
25018bf9475fSPierre-Louis Bossart 			}
25028a978234SLiam Girdwood 
25038a978234SLiam Girdwood 			/* goto next header */
25045aebe7c7SPierre-Louis Bossart 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
25058a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
25068a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25078a978234SLiam Girdwood 		}
25088a978234SLiam Girdwood 
25098a978234SLiam Girdwood 	}
25108a978234SLiam Girdwood 
25118a978234SLiam Girdwood 	/* signal DAPM we are complete */
25128a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
25138a978234SLiam Girdwood 	if (ret < 0)
25148a978234SLiam Girdwood 		dev_err(tplg->dev,
25158a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
25168a978234SLiam Girdwood 
25178a978234SLiam Girdwood 	return ret;
25188a978234SLiam Girdwood }
25198a978234SLiam Girdwood 
25208a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
25218a978234SLiam Girdwood {
25228a978234SLiam Girdwood 	int ret;
25238a978234SLiam Girdwood 
25248a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
25258a978234SLiam Girdwood 	if (ret == 0)
2526415717e1SRanjani Sridharan 		return soc_tplg_complete(tplg);
25278a978234SLiam Girdwood 
25288a978234SLiam Girdwood 	return ret;
25298a978234SLiam Girdwood }
25308a978234SLiam Girdwood 
25318a978234SLiam Girdwood /* load audio component topology from "firmware" file */
25328a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2533a5b8f71cSAmadeusz Sławiński 	struct snd_soc_tplg_ops *ops, const struct firmware *fw)
25348a978234SLiam Girdwood {
25358a978234SLiam Girdwood 	struct soc_tplg tplg;
2536304017d3SBard liao 	int ret;
25378a978234SLiam Girdwood 
2538d40ab86fSAmadeusz Sławiński 	/*
2539d40ab86fSAmadeusz Sławiński 	 * check if we have sane parameters:
2540d40ab86fSAmadeusz Sławiński 	 * comp - needs to exist to keep and reference data while parsing
2541d40ab86fSAmadeusz Sławiński 	 * comp->card - used for setting card related parameters
2542f714fbc1SAmadeusz Sławiński 	 * comp->card->dev - used for resource management and prints
2543d40ab86fSAmadeusz Sławiński 	 * fw - we need it, as it is the very thing we parse
2544d40ab86fSAmadeusz Sławiński 	 */
2545f714fbc1SAmadeusz Sławiński 	if (!comp || !comp->card || !comp->card->dev || !fw)
2546c42464a4SAmadeusz Sławiński 		return -EINVAL;
2547c42464a4SAmadeusz Sławiński 
25488a978234SLiam Girdwood 	/* setup parsing context */
25498a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
25508a978234SLiam Girdwood 	tplg.fw = fw;
2551f714fbc1SAmadeusz Sławiński 	tplg.dev = comp->card->dev;
25528a978234SLiam Girdwood 	tplg.comp = comp;
25539c88a983SAmadeusz Sławiński 	if (ops) {
25548a978234SLiam Girdwood 		tplg.ops = ops;
25558a978234SLiam Girdwood 		tplg.io_ops = ops->io_ops;
25568a978234SLiam Girdwood 		tplg.io_ops_count = ops->io_ops_count;
25571a3232d2SMengdong Lin 		tplg.bytes_ext_ops = ops->bytes_ext_ops;
25581a3232d2SMengdong Lin 		tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
25599c88a983SAmadeusz Sławiński 	}
25608a978234SLiam Girdwood 
2561304017d3SBard liao 	ret = soc_tplg_load(&tplg);
2562304017d3SBard liao 	/* free the created components if fail to load topology */
2563304017d3SBard liao 	if (ret)
2564a5b8f71cSAmadeusz Sławiński 		snd_soc_tplg_component_remove(comp);
2565304017d3SBard liao 
2566304017d3SBard liao 	return ret;
25678a978234SLiam Girdwood }
25688a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
25698a978234SLiam Girdwood 
25708a978234SLiam Girdwood /* remove dynamic controls from the component driver */
2571a5b8f71cSAmadeusz Sławiński int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
25728a978234SLiam Girdwood {
25737e567b5aSTakashi Iwai 	struct snd_card *card = comp->card->snd_card;
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 */
25817e567b5aSTakashi Iwai 		down_write(&card->controls_rwsem);
25828a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
25838a978234SLiam Girdwood 			list) {
25848a978234SLiam Girdwood 
25858a978234SLiam Girdwood 			switch (dobj->type) {
25868a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
2587fdfa3661SAmadeusz Sławiński 			case SND_SOC_DOBJ_ENUM:
2588fdfa3661SAmadeusz Sławiński 			case SND_SOC_DOBJ_MIXER:
2589fdfa3661SAmadeusz Sławiński 				soc_tplg_remove_kcontrol(comp, dobj, pass);
25908a978234SLiam Girdwood 				break;
25917df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
25922abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_route(comp, dobj, pass);
25937df04ea7SRanjani Sridharan 				break;
25948a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
25952abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_widget(comp, dobj, pass);
25968a978234SLiam Girdwood 				break;
25978a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
25982abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_dai(comp, dobj, pass);
25998a978234SLiam Girdwood 				break;
2600acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
26012abfd4bdSAmadeusz Sławiński 				soc_tplg_remove_link(comp, dobj, pass);
2602acfc7d46SMengdong Lin 				break;
2603adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2604adfebb51SBard liao 				/*
2605adfebb51SBard liao 				 * call link_unload ops if extra
2606adfebb51SBard liao 				 * deinitialization is needed.
2607adfebb51SBard liao 				 */
2608adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2609adfebb51SBard liao 				break;
26108a978234SLiam Girdwood 			default:
26118a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
26128a978234SLiam Girdwood 					dobj->type);
26138a978234SLiam Girdwood 				break;
26148a978234SLiam Girdwood 			}
26158a978234SLiam Girdwood 		}
26167e567b5aSTakashi Iwai 		up_write(&card->controls_rwsem);
26178a978234SLiam Girdwood 	}
26188a978234SLiam Girdwood 
26198a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
26208a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
26218a978234SLiam Girdwood }
26228a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2623