xref: /openbmc/linux/sound/soc/soc-topology.c (revision adfebb51)
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 
338a978234SLiam Girdwood /*
348a978234SLiam Girdwood  * We make several passes over the data (since it wont necessarily be ordered)
358a978234SLiam Girdwood  * and process objects in the following order. This guarantees the component
368a978234SLiam Girdwood  * drivers will be ready with any vendor data before the mixers and DAPM objects
378a978234SLiam Girdwood  * are loaded (that may make use of the vendor data).
388a978234SLiam Girdwood  */
398a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST		0
408a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR		1
418a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER		2
428a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET		3
431a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI		4
441a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH		5
451a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PINS		6
460038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI		7
47593d9e52SMengdong Lin #define SOC_TPLG_PASS_LINK		8
488a978234SLiam Girdwood 
498a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
50593d9e52SMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_LINK
518a978234SLiam Girdwood 
52583958faSMengdong Lin /* topology context */
538a978234SLiam Girdwood struct soc_tplg {
548a978234SLiam Girdwood 	const struct firmware *fw;
558a978234SLiam Girdwood 
568a978234SLiam Girdwood 	/* runtime FW parsing */
578a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
588a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
598a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
608a978234SLiam Girdwood 
618a978234SLiam Girdwood 	/* component caller */
628a978234SLiam Girdwood 	struct device *dev;
638a978234SLiam Girdwood 	struct snd_soc_component *comp;
648a978234SLiam Girdwood 	u32 index;	/* current block index */
658a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
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 static int soc_tplg_process_headers(struct soc_tplg *tplg);
808a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
818a978234SLiam Girdwood struct snd_soc_dapm_widget *
828a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
838a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
848a978234SLiam Girdwood struct snd_soc_dapm_widget *
858a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
868a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
878a978234SLiam Girdwood 
888a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
898a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
908a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
918a978234SLiam Girdwood {
928a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
938a978234SLiam Girdwood 
948a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
958a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
968a978234SLiam Girdwood 			elem_type);
978a978234SLiam Girdwood 		return -EINVAL;
988a978234SLiam Girdwood 	}
998a978234SLiam Girdwood 
1008a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
1018a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1028a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1038a978234SLiam Girdwood 		dev_err(tplg->dev,
1048a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1058a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1068a978234SLiam Girdwood 		return -EINVAL;
1078a978234SLiam Girdwood 	}
1088a978234SLiam Girdwood 
1098a978234SLiam Girdwood 	return 0;
1108a978234SLiam Girdwood }
1118a978234SLiam Girdwood 
1128a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1138a978234SLiam Girdwood {
1148a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1158a978234SLiam Girdwood 
1168a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1178a978234SLiam Girdwood 		return 1;
1188a978234SLiam Girdwood 	return 0;
1198a978234SLiam Girdwood }
1208a978234SLiam Girdwood 
1218a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1228a978234SLiam Girdwood {
1238a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1248a978234SLiam Girdwood }
1258a978234SLiam Girdwood 
1268a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1278a978234SLiam Girdwood {
1288a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1298a978234SLiam Girdwood }
1308a978234SLiam Girdwood 
1318a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1328a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1338a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1348a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1358a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1368a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1378a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1388a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1398a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1408a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1418a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1428a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1438a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1448a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1458a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1468a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1478a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1488a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1498a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1502c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1518a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1528a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1538a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1548a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1558a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1568a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1578a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1588a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1598a978234SLiam Girdwood };
1608a978234SLiam Girdwood 
1618a978234SLiam Girdwood struct soc_tplg_map {
1628a978234SLiam Girdwood 	int uid;
1638a978234SLiam Girdwood 	int kid;
1648a978234SLiam Girdwood };
1658a978234SLiam Girdwood 
1668a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1678a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1688a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1708a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1818a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1828a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1838a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1848a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
1858a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
1868a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
1878a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
1888a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
1898a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
1908a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
1918a70b454SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
1928a978234SLiam Girdwood };
1938a978234SLiam Girdwood 
1948a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1958a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1968a978234SLiam Girdwood {
1978a978234SLiam Girdwood 	int i;
1988a978234SLiam Girdwood 
1998a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2008a978234SLiam Girdwood 		if (chan[i].id == map)
2018a978234SLiam Girdwood 			return chan[i].reg;
2028a978234SLiam Girdwood 	}
2038a978234SLiam Girdwood 
2048a978234SLiam Girdwood 	return -EINVAL;
2058a978234SLiam Girdwood }
2068a978234SLiam Girdwood 
2078a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2088a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2098a978234SLiam Girdwood {
2108a978234SLiam Girdwood 	int i;
2118a978234SLiam Girdwood 
2128a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2138a978234SLiam Girdwood 		if (chan[i].id == map)
2148a978234SLiam Girdwood 			return chan[i].shift;
2158a978234SLiam Girdwood 	}
2168a978234SLiam Girdwood 
2178a978234SLiam Girdwood 	return -EINVAL;
2188a978234SLiam Girdwood }
2198a978234SLiam Girdwood 
2208a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2218a978234SLiam Girdwood {
2228a978234SLiam Girdwood 	int i;
2238a978234SLiam Girdwood 
2248a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2258a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2268a978234SLiam Girdwood 			return dapm_map[i].kid;
2278a978234SLiam Girdwood 	}
2288a978234SLiam Girdwood 
2298a978234SLiam Girdwood 	return -EINVAL;
2308a978234SLiam Girdwood }
2318a978234SLiam Girdwood 
2328a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2338a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2348a978234SLiam Girdwood {
2358a978234SLiam Girdwood 	dev_err(tplg->dev,
2368a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2378a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2388a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2398a978234SLiam Girdwood }
2408a978234SLiam Girdwood 
2418a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2428a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2438a978234SLiam Girdwood {
2448a978234SLiam Girdwood 	dev_err(tplg->dev,
2458a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2468a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2478a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2488a978234SLiam Girdwood }
2498a978234SLiam Girdwood 
2508a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2518a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2528a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2538a978234SLiam Girdwood {
2548a978234SLiam Girdwood 	int ret = 0;
2558a978234SLiam Girdwood 
2568a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
257c60b613aSLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
2588a978234SLiam Girdwood 	else {
2598a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2608a978234SLiam Girdwood 			hdr->vendor_type);
2618a978234SLiam Girdwood 		return -EINVAL;
2628a978234SLiam Girdwood 	}
2638a978234SLiam Girdwood 
2648a978234SLiam Girdwood 	if (ret < 0)
2658a978234SLiam Girdwood 		dev_err(tplg->dev,
2668a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2678a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2688a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2698a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2708a978234SLiam Girdwood 	return ret;
2718a978234SLiam Girdwood }
2728a978234SLiam Girdwood 
2738a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2748a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2758a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2768a978234SLiam Girdwood {
2778a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2788a978234SLiam Girdwood 		return 0;
2798a978234SLiam Girdwood 
2808a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2818a978234SLiam Girdwood }
2828a978234SLiam Girdwood 
2838a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2848a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2858a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2868a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2878a978234SLiam Girdwood {
2888a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
289c60b613aSLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
290c60b613aSLiam Girdwood 			tplg_w);
2918a978234SLiam Girdwood 
2928a978234SLiam Girdwood 	return 0;
2938a978234SLiam Girdwood }
2948a978234SLiam Girdwood 
295ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
296ebd259d3SLiam Girdwood  * external widgets where we can assign private data/ops */
297ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg,
298ebd259d3SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
299ebd259d3SLiam Girdwood {
300ebd259d3SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
301c60b613aSLiam Girdwood 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
302c60b613aSLiam Girdwood 			tplg_w);
303ebd259d3SLiam Girdwood 
304ebd259d3SLiam Girdwood 	return 0;
305ebd259d3SLiam Girdwood }
306ebd259d3SLiam Girdwood 
307183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */
30864527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
309c60b613aSLiam Girdwood 	struct snd_soc_dai_driver *dai_drv,
310c60b613aSLiam Girdwood 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
3118a978234SLiam Girdwood {
31264527e8aSMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
313c60b613aSLiam Girdwood 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
314c60b613aSLiam Girdwood 			pcm, dai);
3158a978234SLiam Girdwood 
3168a978234SLiam Girdwood 	return 0;
3178a978234SLiam Girdwood }
3188a978234SLiam Girdwood 
319183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */
320acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
321c60b613aSLiam Girdwood 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
322acfc7d46SMengdong Lin {
323acfc7d46SMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
324c60b613aSLiam Girdwood 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
325acfc7d46SMengdong Lin 
326acfc7d46SMengdong Lin 	return 0;
327acfc7d46SMengdong Lin }
328acfc7d46SMengdong Lin 
3298a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3308a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3318a978234SLiam Girdwood {
3328a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3338a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3348a978234SLiam Girdwood }
3358a978234SLiam Girdwood 
3368a978234SLiam Girdwood /* add a dynamic kcontrol */
3378a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3388a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3398a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3408a978234SLiam Girdwood {
3418a978234SLiam Girdwood 	int err;
3428a978234SLiam Girdwood 
3438a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3448a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3458a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3468a978234SLiam Girdwood 		control_new->name);
3478a978234SLiam Girdwood 		return -ENOMEM;
3488a978234SLiam Girdwood 	}
3498a978234SLiam Girdwood 
3508a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3518a978234SLiam Girdwood 	if (err < 0) {
3528a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3538a978234SLiam Girdwood 			control_new->name, err);
3548a978234SLiam Girdwood 		return err;
3558a978234SLiam Girdwood 	}
3568a978234SLiam Girdwood 
3578a978234SLiam Girdwood 	return 0;
3588a978234SLiam Girdwood }
3598a978234SLiam Girdwood 
3608a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3618a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3628a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3638a978234SLiam Girdwood {
3648a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3658a978234SLiam Girdwood 
3668a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3678a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3688a978234SLiam Girdwood }
3698a978234SLiam Girdwood 
3708a978234SLiam Girdwood /* remove a mixer kcontrol */
3718a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3728a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3738a978234SLiam Girdwood {
3748a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3758a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3768a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3778a978234SLiam Girdwood 	const unsigned int *p = NULL;
3788a978234SLiam Girdwood 
3798a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3808a978234SLiam Girdwood 		return;
3818a978234SLiam Girdwood 
3828a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3838a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3848a978234SLiam Girdwood 
38533ae6ae2SAmadeusz Sławiński 	if (dobj->control.kcontrol->tlv.p)
38633ae6ae2SAmadeusz Sławiński 		p = dobj->control.kcontrol->tlv.p;
38733ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
38833ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
3898a978234SLiam Girdwood 	kfree(sm);
3908a978234SLiam Girdwood 	kfree(p);
3918a978234SLiam Girdwood }
3928a978234SLiam Girdwood 
3938a978234SLiam Girdwood /* remove an enum kcontrol */
3948a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3958a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3968a978234SLiam Girdwood {
3978a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3988a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
3998a978234SLiam Girdwood 	int i;
4008a978234SLiam Girdwood 
4018a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4028a978234SLiam Girdwood 		return;
4038a978234SLiam Girdwood 
4048a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4058a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4068a978234SLiam Girdwood 
40733ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
40833ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4098a978234SLiam Girdwood 
41033ae6ae2SAmadeusz Sławiński 	kfree(dobj->control.dvalues);
4118a978234SLiam Girdwood 	for (i = 0; i < se->items; i++)
41233ae6ae2SAmadeusz Sławiński 		kfree(dobj->control.dtexts[i]);
41334db6a3eSAmadeusz Sławiński 	kfree(dobj->control.dtexts);
4148a978234SLiam Girdwood 	kfree(se);
4158a978234SLiam Girdwood }
4168a978234SLiam Girdwood 
4178a978234SLiam Girdwood /* remove a byte kcontrol */
4188a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4198a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4208a978234SLiam Girdwood {
4218a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4228a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4238a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4248a978234SLiam Girdwood 
4258a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4268a978234SLiam Girdwood 		return;
4278a978234SLiam Girdwood 
4288a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4298a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4308a978234SLiam Girdwood 
43133ae6ae2SAmadeusz Sławiński 	snd_ctl_remove(card, dobj->control.kcontrol);
43233ae6ae2SAmadeusz Sławiński 	list_del(&dobj->list);
4338a978234SLiam Girdwood 	kfree(sb);
4348a978234SLiam Girdwood }
4358a978234SLiam Girdwood 
4367df04ea7SRanjani Sridharan /* remove a route */
4377df04ea7SRanjani Sridharan static void remove_route(struct snd_soc_component *comp,
4387df04ea7SRanjani Sridharan 			 struct snd_soc_dobj *dobj, int pass)
4397df04ea7SRanjani Sridharan {
4407df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route *route =
4417df04ea7SRanjani Sridharan 		container_of(dobj, struct snd_soc_dapm_route, dobj);
4427df04ea7SRanjani Sridharan 
4437df04ea7SRanjani Sridharan 	if (pass != SOC_TPLG_PASS_GRAPH)
4447df04ea7SRanjani Sridharan 		return;
4457df04ea7SRanjani Sridharan 
4467df04ea7SRanjani Sridharan 	if (dobj->ops && dobj->ops->dapm_route_unload)
4477df04ea7SRanjani Sridharan 		dobj->ops->dapm_route_unload(comp, dobj);
4487df04ea7SRanjani Sridharan 
4497df04ea7SRanjani Sridharan 	list_del(&dobj->list);
4507df04ea7SRanjani Sridharan 	kfree(route);
4517df04ea7SRanjani Sridharan }
4527df04ea7SRanjani Sridharan 
4538a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4548a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4558a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4568a978234SLiam Girdwood {
4578a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4588a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4598a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4608a978234SLiam Girdwood 	int i;
4618a978234SLiam Girdwood 
4628a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4638a978234SLiam Girdwood 		return;
4648a978234SLiam Girdwood 
4658a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4668a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4678a978234SLiam Girdwood 
46805bdcf12SLiam Girdwood 	if (!w->kcontrols)
46905bdcf12SLiam Girdwood 		goto free_news;
47005bdcf12SLiam Girdwood 
4718a978234SLiam Girdwood 	/*
4721a7dd6e2SMengdong Lin 	 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
4738a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4748a978234SLiam Girdwood 	 */
475eea3dd4fSMengdong Lin 	if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
4768a978234SLiam Girdwood 		/* enumerated widget mixer */
477f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4781a7dd6e2SMengdong Lin 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4798a978234SLiam Girdwood 			struct soc_enum *se =
4801a7dd6e2SMengdong Lin 				(struct soc_enum *)kcontrol->private_value;
481d7766aa5SColin Ian King 			int j;
4828a978234SLiam Girdwood 
4831a7dd6e2SMengdong Lin 			snd_ctl_remove(card, kcontrol);
4848a978234SLiam Girdwood 
48533ae6ae2SAmadeusz Sławiński 			kfree(dobj->control.dvalues);
486d7766aa5SColin Ian King 			for (j = 0; j < se->items; j++)
48733ae6ae2SAmadeusz Sławiński 				kfree(dobj->control.dtexts[j]);
48834db6a3eSAmadeusz Sławiński 			kfree(dobj->control.dtexts);
4898a978234SLiam Girdwood 
4908a978234SLiam Girdwood 			kfree(se);
491267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
4921a7dd6e2SMengdong Lin 		}
4938a978234SLiam Girdwood 	} else {
494eea3dd4fSMengdong Lin 		/* volume mixer or bytes controls */
495f53c4c20SLiam Girdwood 		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
4968a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4978a978234SLiam Girdwood 
498eea3dd4fSMengdong Lin 			if (dobj->widget.kcontrol_type
499eea3dd4fSMengdong Lin 			    == SND_SOC_TPLG_TYPE_MIXER)
500eea3dd4fSMengdong Lin 				kfree(kcontrol->tlv.p);
5018a978234SLiam Girdwood 
502eea3dd4fSMengdong Lin 			/* Private value is used as struct soc_mixer_control
503eea3dd4fSMengdong Lin 			 * for volume mixers or soc_bytes_ext for bytes
504eea3dd4fSMengdong Lin 			 * controls.
505eea3dd4fSMengdong Lin 			 */
506eea3dd4fSMengdong Lin 			kfree((void *)kcontrol->private_value);
507c2b36129SColin Ian King 			snd_ctl_remove(card, kcontrol);
508267e2c6fSLiam Girdwood 			kfree(w->kcontrol_news[i].name);
5098a978234SLiam Girdwood 		}
5108a978234SLiam Girdwood 	}
51105bdcf12SLiam Girdwood 
51205bdcf12SLiam Girdwood free_news:
5138a978234SLiam Girdwood 	kfree(w->kcontrol_news);
51405bdcf12SLiam Girdwood 
515a46e8393SAmadeusz Sławiński 	list_del(&dobj->list);
516a46e8393SAmadeusz Sławiński 
5178a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
5188a978234SLiam Girdwood }
5198a978234SLiam Girdwood 
52064527e8aSMengdong Lin /* remove DAI configurations */
52164527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
5228a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
5238a978234SLiam Girdwood {
52464527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
52564527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
52664527e8aSMengdong Lin 
5278a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
5288a978234SLiam Girdwood 		return;
5298a978234SLiam Girdwood 
53064527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
53164527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
5328a978234SLiam Girdwood 
5338f27c4abSMengdong Lin 	kfree(dai_drv->name);
5348a978234SLiam Girdwood 	list_del(&dobj->list);
53564527e8aSMengdong Lin 	kfree(dai_drv);
5368a978234SLiam Girdwood }
5378a978234SLiam Girdwood 
538acfc7d46SMengdong Lin /* remove link configurations */
539acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
540acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
541acfc7d46SMengdong Lin {
542acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
543acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
544acfc7d46SMengdong Lin 
545acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
546acfc7d46SMengdong Lin 		return;
547acfc7d46SMengdong Lin 
548acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
549acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
550acfc7d46SMengdong Lin 
5518f27c4abSMengdong Lin 	kfree(link->name);
5528f27c4abSMengdong Lin 	kfree(link->stream_name);
5538f27c4abSMengdong Lin 	kfree(link->cpu_dai_name);
5548f27c4abSMengdong Lin 
555acfc7d46SMengdong Lin 	list_del(&dobj->list);
556acfc7d46SMengdong Lin 	snd_soc_remove_dai_link(comp->card, link);
557acfc7d46SMengdong Lin 	kfree(link);
558acfc7d46SMengdong Lin }
559acfc7d46SMengdong Lin 
560*adfebb51SBard liao /* unload dai link */
561*adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp,
562*adfebb51SBard liao 	struct snd_soc_dobj *dobj, int pass)
563*adfebb51SBard liao {
564*adfebb51SBard liao 	if (pass != SOC_TPLG_PASS_LINK)
565*adfebb51SBard liao 		return;
566*adfebb51SBard liao 
567*adfebb51SBard liao 	if (dobj->ops && dobj->ops->link_unload)
568*adfebb51SBard liao 		dobj->ops->link_unload(comp, dobj);
569*adfebb51SBard liao 
570*adfebb51SBard liao 	/*
571*adfebb51SBard liao 	 * We don't free the link here as what remove_link() do since BE
572*adfebb51SBard liao 	 * links are not allocated by topology.
573*adfebb51SBard liao 	 * We however need to reset the dobj type to its initial values
574*adfebb51SBard liao 	 */
575*adfebb51SBard liao 	dobj->type = SND_SOC_DOBJ_NONE;
576*adfebb51SBard liao 	list_del(&dobj->list);
577*adfebb51SBard liao }
578*adfebb51SBard liao 
5798a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5808a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5818a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5822b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5838a978234SLiam Girdwood {
5842b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5851a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5862b5cdb91SMengdong Lin 	int num_ops, i;
5878a978234SLiam Girdwood 
5881a3232d2SMengdong Lin 	if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
5891a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5901a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5911a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5921a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5931a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
5941a3232d2SMengdong Lin 
5951a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
5961a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
5971a3232d2SMengdong Lin 
5981a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
5991a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
6001a3232d2SMengdong Lin 		 */
601f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
6021a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
6031a3232d2SMengdong Lin 
6041a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
6051a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
6061a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
6071a3232d2SMengdong Lin 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
6081a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
6091a3232d2SMengdong Lin 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
6101a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
6111a3232d2SMengdong Lin 		}
6121a3232d2SMengdong Lin 
6131a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
6141a3232d2SMengdong Lin 			return 0;
6151a3232d2SMengdong Lin 		else
6161a3232d2SMengdong Lin 			return -EINVAL;
6171a3232d2SMengdong Lin 	}
6181a3232d2SMengdong Lin 
61988a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
6202b5cdb91SMengdong Lin 	ops = tplg->io_ops;
6212b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
6228a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
6238a978234SLiam Girdwood 
6242b5cdb91SMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
6258a978234SLiam Girdwood 			k->put = ops[i].put;
6262b5cdb91SMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
6278a978234SLiam Girdwood 			k->get = ops[i].get;
6282b5cdb91SMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6292b5cdb91SMengdong Lin 			k->info = ops[i].info;
6308a978234SLiam Girdwood 	}
6318a978234SLiam Girdwood 
63288a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
63388a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
63488a17d8fSMengdong Lin 		return 0;
63588a17d8fSMengdong Lin 
63688a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
6372b5cdb91SMengdong Lin 	ops = io_ops;
6382b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
63988a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
64088a17d8fSMengdong Lin 
64188a17d8fSMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
64288a17d8fSMengdong Lin 			k->put = ops[i].put;
64388a17d8fSMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
64488a17d8fSMengdong Lin 			k->get = ops[i].get;
64588a17d8fSMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
6468a978234SLiam Girdwood 			k->info = ops[i].info;
6478a978234SLiam Girdwood 	}
6488a978234SLiam Girdwood 
6498a978234SLiam Girdwood 	/* standard handlers found ? */
6508a978234SLiam Girdwood 	if (k->put && k->get && k->info)
6518a978234SLiam Girdwood 		return 0;
6528a978234SLiam Girdwood 
6538a978234SLiam Girdwood 	/* nothing to bind */
6548a978234SLiam Girdwood 	return -EINVAL;
6558a978234SLiam Girdwood }
6568a978234SLiam Girdwood 
6578a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
6588a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
6598a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
6608a978234SLiam Girdwood 		int num_events, u16 event_type)
6618a978234SLiam Girdwood {
6628a978234SLiam Girdwood 	int i;
6638a978234SLiam Girdwood 
6648a978234SLiam Girdwood 	w->event = NULL;
6658a978234SLiam Girdwood 
6668a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
6678a978234SLiam Girdwood 		if (event_type == events[i].type) {
6688a978234SLiam Girdwood 
6698a978234SLiam Girdwood 			/* found - so assign event */
6708a978234SLiam Girdwood 			w->event = events[i].event_handler;
6718a978234SLiam Girdwood 			return 0;
6728a978234SLiam Girdwood 		}
6738a978234SLiam Girdwood 	}
6748a978234SLiam Girdwood 
6758a978234SLiam Girdwood 	/* not found */
6768a978234SLiam Girdwood 	return -EINVAL;
6778a978234SLiam Girdwood }
6788a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
6798a978234SLiam Girdwood 
6808a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6818a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6828a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6838a978234SLiam Girdwood {
6848a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
685c60b613aSLiam Girdwood 		return tplg->ops->control_load(tplg->comp, tplg->index, k,
686c60b613aSLiam Girdwood 			hdr);
6878a978234SLiam Girdwood 
6888a978234SLiam Girdwood 	return 0;
6898a978234SLiam Girdwood }
6908a978234SLiam Girdwood 
69128a87eebSMengdong Lin 
69228a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
69328a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6948a978234SLiam Girdwood {
69528a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
69628a87eebSMengdong Lin 	unsigned int *p;
6978a978234SLiam Girdwood 
69828a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
69928a87eebSMengdong Lin 	if (!p)
7008a978234SLiam Girdwood 		return -ENOMEM;
7018a978234SLiam Girdwood 
70228a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
70328a87eebSMengdong Lin 	p[1] = item_len;
70428a87eebSMengdong Lin 	p[2] = scale->min;
70528a87eebSMengdong Lin 	p[3] = (scale->step & TLV_DB_SCALE_MASK)
70628a87eebSMengdong Lin 			| (scale->mute ? TLV_DB_SCALE_MUTE : 0);
7078a978234SLiam Girdwood 
70828a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
70928a87eebSMengdong Lin 	return 0;
71028a87eebSMengdong Lin }
71128a87eebSMengdong Lin 
71228a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
71328a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
71428a87eebSMengdong Lin {
71528a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
71628a87eebSMengdong Lin 
71728a87eebSMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
71828a87eebSMengdong Lin 		return 0;
71928a87eebSMengdong Lin 
7201a3232d2SMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
72128a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
72228a87eebSMengdong Lin 		switch (tplg_tlv->type) {
72328a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
72428a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
72528a87eebSMengdong Lin 					&tplg_tlv->scale);
72628a87eebSMengdong Lin 
72728a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
72828a87eebSMengdong Lin 		default:
72928a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
73028a87eebSMengdong Lin 					tplg_tlv->type);
73128a87eebSMengdong Lin 			return -EINVAL;
73228a87eebSMengdong Lin 		}
73328a87eebSMengdong Lin 	}
7348a978234SLiam Girdwood 
7358a978234SLiam Girdwood 	return 0;
7368a978234SLiam Girdwood }
7378a978234SLiam Girdwood 
7388a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
7398a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
7408a978234SLiam Girdwood {
7418a978234SLiam Girdwood 	kfree(kc->tlv.p);
7428a978234SLiam Girdwood }
7438a978234SLiam Girdwood 
7448a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
7458a978234SLiam Girdwood 	size_t size)
7468a978234SLiam Girdwood {
7478a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
7488a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
7498a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7508a978234SLiam Girdwood 	int i, err;
7518a978234SLiam Girdwood 
7528a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7538a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
7548a978234SLiam Girdwood 			size, "mixer bytes")) {
7558a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
7568a978234SLiam Girdwood 			count);
7578a978234SLiam Girdwood 		return -EINVAL;
7588a978234SLiam Girdwood 	}
7598a978234SLiam Girdwood 
7608a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7618a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
7628a978234SLiam Girdwood 
7638a978234SLiam Girdwood 		/* validate kcontrol */
7648a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7658a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7668a978234SLiam Girdwood 			return -EINVAL;
7678a978234SLiam Girdwood 
7688a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
7698a978234SLiam Girdwood 		if (sbe == NULL)
7708a978234SLiam Girdwood 			return -ENOMEM;
7718a978234SLiam Girdwood 
7728a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
7738a978234SLiam Girdwood 			be->priv.size);
7748a978234SLiam Girdwood 
7758a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7768a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
7778a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
7788a978234SLiam Girdwood 
7798a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7808a978234SLiam Girdwood 		kc.name = be->hdr.name;
7818a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7828a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7838a978234SLiam Girdwood 		kc.access = be->hdr.access;
7848a978234SLiam Girdwood 
7858a978234SLiam Girdwood 		sbe->max = be->max;
7868a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7878a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7888a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7898a978234SLiam Girdwood 
7908a978234SLiam Girdwood 		/* map io handlers */
7912b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7928a978234SLiam Girdwood 		if (err) {
7938a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
7948a978234SLiam Girdwood 			kfree(sbe);
7958a978234SLiam Girdwood 			continue;
7968a978234SLiam Girdwood 		}
7978a978234SLiam Girdwood 
7988a978234SLiam Girdwood 		/* pass control to driver for optional further init */
7998a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8008a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
8018a978234SLiam Girdwood 		if (err < 0) {
8028a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8038a978234SLiam Girdwood 				be->hdr.name);
8048a978234SLiam Girdwood 			kfree(sbe);
8058a978234SLiam Girdwood 			continue;
8068a978234SLiam Girdwood 		}
8078a978234SLiam Girdwood 
8088a978234SLiam Girdwood 		/* register control here */
8098a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8108a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
8118a978234SLiam Girdwood 		if (err < 0) {
8128a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8138a978234SLiam Girdwood 				be->hdr.name);
8148a978234SLiam Girdwood 			kfree(sbe);
8158a978234SLiam Girdwood 			continue;
8168a978234SLiam Girdwood 		}
8178a978234SLiam Girdwood 
8188a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
8198a978234SLiam Girdwood 	}
8208a978234SLiam Girdwood 	return 0;
8218a978234SLiam Girdwood 
8228a978234SLiam Girdwood }
8238a978234SLiam Girdwood 
8248a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
8258a978234SLiam Girdwood 	size_t size)
8268a978234SLiam Girdwood {
8278a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
8288a978234SLiam Girdwood 	struct soc_mixer_control *sm;
8298a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8308a978234SLiam Girdwood 	int i, err;
8318a978234SLiam Girdwood 
8328a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8338a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
8348a978234SLiam Girdwood 		count, size, "mixers")) {
8358a978234SLiam Girdwood 
8368a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
8378a978234SLiam Girdwood 			count);
8388a978234SLiam Girdwood 		return -EINVAL;
8398a978234SLiam Girdwood 	}
8408a978234SLiam Girdwood 
8418a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
8428a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
8438a978234SLiam Girdwood 
8448a978234SLiam Girdwood 		/* validate kcontrol */
8458a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8468a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8478a978234SLiam Girdwood 			return -EINVAL;
8488a978234SLiam Girdwood 
8498a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
8508a978234SLiam Girdwood 		if (sm == NULL)
8518a978234SLiam Girdwood 			return -ENOMEM;
8528a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
8538a978234SLiam Girdwood 			mc->priv.size);
8548a978234SLiam Girdwood 
8558a978234SLiam Girdwood 		dev_dbg(tplg->dev,
8568a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
8578a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
8588a978234SLiam Girdwood 
8598a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
8608a978234SLiam Girdwood 		kc.name = mc->hdr.name;
8618a978234SLiam Girdwood 		kc.private_value = (long)sm;
8628a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8638a978234SLiam Girdwood 		kc.access = mc->hdr.access;
8648a978234SLiam Girdwood 
8658a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
8668a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
8678a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8688a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
8698a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8708a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
8718a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8728a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
8738a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8748a978234SLiam Girdwood 
8758a978234SLiam Girdwood 		sm->max = mc->max;
8768a978234SLiam Girdwood 		sm->min = mc->min;
8778a978234SLiam Girdwood 		sm->invert = mc->invert;
8788a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
8798a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
8808a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8818a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8828a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8838a978234SLiam Girdwood 
8848a978234SLiam Girdwood 		/* map io handlers */
8852b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8868a978234SLiam Girdwood 		if (err) {
8878a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8888a978234SLiam Girdwood 			kfree(sm);
8898a978234SLiam Girdwood 			continue;
8908a978234SLiam Girdwood 		}
8918a978234SLiam Girdwood 
8928a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8938a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8948a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
8958a978234SLiam Girdwood 		if (err < 0) {
8968a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8978a978234SLiam Girdwood 				mc->hdr.name);
8988a978234SLiam Girdwood 			kfree(sm);
8998a978234SLiam Girdwood 			continue;
9008a978234SLiam Girdwood 		}
9018a978234SLiam Girdwood 
9028a978234SLiam Girdwood 		/* create any TLV data */
90328a87eebSMengdong Lin 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
9048a978234SLiam Girdwood 
9058a978234SLiam Girdwood 		/* register control here */
9068a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
9078a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
9088a978234SLiam Girdwood 		if (err < 0) {
9098a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
9108a978234SLiam Girdwood 				mc->hdr.name);
9118a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
9128a978234SLiam Girdwood 			kfree(sm);
9138a978234SLiam Girdwood 			continue;
9148a978234SLiam Girdwood 		}
9158a978234SLiam Girdwood 
9168a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
9178a978234SLiam Girdwood 	}
9188a978234SLiam Girdwood 
9198a978234SLiam Girdwood 	return 0;
9208a978234SLiam Girdwood }
9218a978234SLiam Girdwood 
9228a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
9238a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9248a978234SLiam Girdwood {
9258a978234SLiam Girdwood 	int i, ret;
9268a978234SLiam Girdwood 
9278a978234SLiam Girdwood 	se->dobj.control.dtexts =
9286396bb22SKees Cook 		kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
9298a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
9308a978234SLiam Girdwood 		return -ENOMEM;
9318a978234SLiam Girdwood 
9328a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
9338a978234SLiam Girdwood 
9348a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9358a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
9368a978234SLiam Girdwood 			ret = -EINVAL;
9378a978234SLiam Girdwood 			goto err;
9388a978234SLiam Girdwood 		}
9398a978234SLiam Girdwood 
9408a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
9418a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
9428a978234SLiam Girdwood 			ret = -ENOMEM;
9438a978234SLiam Girdwood 			goto err;
9448a978234SLiam Girdwood 		}
9458a978234SLiam Girdwood 	}
9468a978234SLiam Girdwood 
947b6e38b29SMousumi Jana 	se->texts = (const char * const *)se->dobj.control.dtexts;
9488a978234SLiam Girdwood 	return 0;
9498a978234SLiam Girdwood 
9508a978234SLiam Girdwood err:
9518a978234SLiam Girdwood 	for (--i; i >= 0; i--)
9528a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
9538a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
9548a978234SLiam Girdwood 	return ret;
9558a978234SLiam Girdwood }
9568a978234SLiam Girdwood 
9578a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
9588a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
9598a978234SLiam Girdwood {
9608a978234SLiam Girdwood 	if (ec->items > sizeof(*ec->values))
9618a978234SLiam Girdwood 		return -EINVAL;
9628a978234SLiam Girdwood 
963376c0afeSAndrzej Hajda 	se->dobj.control.dvalues = kmemdup(ec->values,
964376c0afeSAndrzej Hajda 					   ec->items * sizeof(u32),
965376c0afeSAndrzej Hajda 					   GFP_KERNEL);
9668a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
9678a978234SLiam Girdwood 		return -ENOMEM;
9688a978234SLiam Girdwood 
9698a978234SLiam Girdwood 	return 0;
9708a978234SLiam Girdwood }
9718a978234SLiam Girdwood 
9728a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
9738a978234SLiam Girdwood 	size_t size)
9748a978234SLiam Girdwood {
9758a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
9768a978234SLiam Girdwood 	struct soc_enum *se;
9778a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
9788a978234SLiam Girdwood 	int i, ret, err;
9798a978234SLiam Girdwood 
9808a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
9818a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
9828a978234SLiam Girdwood 		count, size, "enums")) {
9838a978234SLiam Girdwood 
9848a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
9858a978234SLiam Girdwood 			count);
9868a978234SLiam Girdwood 		return -EINVAL;
9878a978234SLiam Girdwood 	}
9888a978234SLiam Girdwood 
9898a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
9908a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
9918a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
9928a978234SLiam Girdwood 			ec->priv.size);
9938a978234SLiam Girdwood 
9948a978234SLiam Girdwood 		/* validate kcontrol */
9958a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9968a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
9978a978234SLiam Girdwood 			return -EINVAL;
9988a978234SLiam Girdwood 
9998a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
10008a978234SLiam Girdwood 		if (se == NULL)
10018a978234SLiam Girdwood 			return -ENOMEM;
10028a978234SLiam Girdwood 
10038a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
10048a978234SLiam Girdwood 			ec->hdr.name, ec->items);
10058a978234SLiam Girdwood 
10068a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
10078a978234SLiam Girdwood 		kc.name = ec->hdr.name;
10088a978234SLiam Girdwood 		kc.private_value = (long)se;
10098a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
10108a978234SLiam Girdwood 		kc.access = ec->hdr.access;
10118a978234SLiam Girdwood 
10128a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
10138a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
10148a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10158a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
10168a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
10178a978234SLiam Girdwood 
10188a978234SLiam Girdwood 		se->items = ec->items;
10198a978234SLiam Girdwood 		se->mask = ec->mask;
10208a978234SLiam Girdwood 		se->dobj.index = tplg->index;
10218a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
10228a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
10238a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
10248a978234SLiam Girdwood 
10258a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
10268a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10278a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10288a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
10298a978234SLiam Girdwood 			if (err < 0) {
10308a978234SLiam Girdwood 				dev_err(tplg->dev,
10318a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
10328a978234SLiam Girdwood 					ec->hdr.name);
10338a978234SLiam Girdwood 				kfree(se);
10348a978234SLiam Girdwood 				continue;
10358a978234SLiam Girdwood 			}
10369c6c4d96STakashi Iwai 			/* fall through */
10378a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10388a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10398a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10408a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
10418a978234SLiam Girdwood 			if (err < 0) {
10428a978234SLiam Girdwood 				dev_err(tplg->dev,
10438a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
10448a978234SLiam Girdwood 					ec->hdr.name);
10458a978234SLiam Girdwood 				kfree(se);
10468a978234SLiam Girdwood 				continue;
10478a978234SLiam Girdwood 			}
10488a978234SLiam Girdwood 			break;
10498a978234SLiam Girdwood 		default:
10508a978234SLiam Girdwood 			dev_err(tplg->dev,
10518a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
10528a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
10538a978234SLiam Girdwood 			kfree(se);
10548a978234SLiam Girdwood 			continue;
10558a978234SLiam Girdwood 		}
10568a978234SLiam Girdwood 
10578a978234SLiam Girdwood 		/* map io handlers */
10582b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
10598a978234SLiam Girdwood 		if (err) {
10608a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
10618a978234SLiam Girdwood 			kfree(se);
10628a978234SLiam Girdwood 			continue;
10638a978234SLiam Girdwood 		}
10648a978234SLiam Girdwood 
10658a978234SLiam Girdwood 		/* pass control to driver for optional further init */
10668a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
10678a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
10688a978234SLiam Girdwood 		if (err < 0) {
10698a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
10708a978234SLiam Girdwood 				ec->hdr.name);
10718a978234SLiam Girdwood 			kfree(se);
10728a978234SLiam Girdwood 			continue;
10738a978234SLiam Girdwood 		}
10748a978234SLiam Girdwood 
10758a978234SLiam Girdwood 		/* register control here */
10768a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
10778a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
10788a978234SLiam Girdwood 		if (ret < 0) {
10798a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
10808a978234SLiam Girdwood 				ec->hdr.name);
10818a978234SLiam Girdwood 			kfree(se);
10828a978234SLiam Girdwood 			continue;
10838a978234SLiam Girdwood 		}
10848a978234SLiam Girdwood 
10858a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
10868a978234SLiam Girdwood 	}
10878a978234SLiam Girdwood 
10888a978234SLiam Girdwood 	return 0;
10898a978234SLiam Girdwood }
10908a978234SLiam Girdwood 
10918a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
10928a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10938a978234SLiam Girdwood {
10948a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
10958a978234SLiam Girdwood 	int i;
10968a978234SLiam Girdwood 
10978a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
10988a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10998a978234SLiam Girdwood 		return 0;
11008a978234SLiam Girdwood 	}
11018a978234SLiam Girdwood 
11028a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
11038a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
11048a978234SLiam Girdwood 
11058a978234SLiam Girdwood 	for (i = 0; i < hdr->count; i++) {
11068a978234SLiam Girdwood 
11078a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
11088a978234SLiam Girdwood 
110906eb49f7SMengdong Lin 		if (control_hdr->size != sizeof(*control_hdr)) {
111006eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
111106eb49f7SMengdong Lin 			return -EINVAL;
111206eb49f7SMengdong Lin 		}
111306eb49f7SMengdong Lin 
11148a978234SLiam Girdwood 		switch (control_hdr->ops.info) {
11158a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
11168a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
11178a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
11188a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
11198a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
11208a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
11218a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
11228a978234SLiam Girdwood 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
11238a978234SLiam Girdwood 			break;
11248a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
11258a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
11268a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
11278a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
11288a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
11298a978234SLiam Girdwood 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
11308a978234SLiam Girdwood 			break;
11318a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
11328a978234SLiam Girdwood 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
11338a978234SLiam Girdwood 			break;
11348a978234SLiam Girdwood 		default:
11358a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
11368a978234SLiam Girdwood 			return -EINVAL;
11378a978234SLiam Girdwood 		}
11388a978234SLiam Girdwood 	}
11398a978234SLiam Girdwood 
11408a978234SLiam Girdwood 	return 0;
11418a978234SLiam Girdwood }
11428a978234SLiam Girdwood 
1143503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
1144503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg,
1145503e79b7SLiam Girdwood 	struct snd_soc_dapm_route *route)
1146503e79b7SLiam Girdwood {
1147503e79b7SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1148503e79b7SLiam Girdwood 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1149503e79b7SLiam Girdwood 			route);
1150503e79b7SLiam Girdwood 
1151503e79b7SLiam Girdwood 	return 0;
1152503e79b7SLiam Girdwood }
1153503e79b7SLiam Girdwood 
11548a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
11558a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
11568a978234SLiam Girdwood {
11578a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
11588a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
11597df04ea7SRanjani Sridharan 	struct snd_soc_dapm_route **routes;
11607df04ea7SRanjani Sridharan 	int count = hdr->count, i, j;
11617df04ea7SRanjani Sridharan 	int ret = 0;
11628a978234SLiam Girdwood 
11638a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
11648a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
11658a978234SLiam Girdwood 		return 0;
11668a978234SLiam Girdwood 	}
11678a978234SLiam Girdwood 
11688a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
11698a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
11708a978234SLiam Girdwood 		count, hdr->payload_size, "graph")) {
11718a978234SLiam Girdwood 
11728a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
11738a978234SLiam Girdwood 			count);
11748a978234SLiam Girdwood 		return -EINVAL;
11758a978234SLiam Girdwood 	}
11768a978234SLiam Girdwood 
1177b75a6511SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1178b75a6511SLiam Girdwood 		hdr->index);
11798a978234SLiam Girdwood 
11807df04ea7SRanjani Sridharan 	/* allocate memory for pointer to array of dapm routes */
11817df04ea7SRanjani Sridharan 	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
11827df04ea7SRanjani Sridharan 			 GFP_KERNEL);
11837df04ea7SRanjani Sridharan 	if (!routes)
11847df04ea7SRanjani Sridharan 		return -ENOMEM;
11857df04ea7SRanjani Sridharan 
11867df04ea7SRanjani Sridharan 	/*
11877df04ea7SRanjani Sridharan 	 * allocate memory for each dapm route in the array.
11887df04ea7SRanjani Sridharan 	 * This needs to be done individually so that
11897df04ea7SRanjani Sridharan 	 * each route can be freed when it is removed in remove_route().
11907df04ea7SRanjani Sridharan 	 */
11917df04ea7SRanjani Sridharan 	for (i = 0; i < count; i++) {
11927df04ea7SRanjani Sridharan 		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
11937df04ea7SRanjani Sridharan 		if (!routes[i]) {
11947df04ea7SRanjani Sridharan 			/* free previously allocated memory */
11957df04ea7SRanjani Sridharan 			for (j = 0; j < i; j++)
11967df04ea7SRanjani Sridharan 				kfree(routes[j]);
11977df04ea7SRanjani Sridharan 
11987df04ea7SRanjani Sridharan 			kfree(routes);
11997df04ea7SRanjani Sridharan 			return -ENOMEM;
12007df04ea7SRanjani Sridharan 		}
12017df04ea7SRanjani Sridharan 	}
12027df04ea7SRanjani Sridharan 
12038a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12048a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
12058a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
12068a978234SLiam Girdwood 
12078a978234SLiam Girdwood 		/* validate routes */
12088a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12097df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12107df04ea7SRanjani Sridharan 			ret = -EINVAL;
12117df04ea7SRanjani Sridharan 			break;
12127df04ea7SRanjani Sridharan 		}
12138a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12147df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12157df04ea7SRanjani Sridharan 			ret = -EINVAL;
12167df04ea7SRanjani Sridharan 			break;
12177df04ea7SRanjani Sridharan 		}
12188a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12197df04ea7SRanjani Sridharan 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
12207df04ea7SRanjani Sridharan 			ret = -EINVAL;
12217df04ea7SRanjani Sridharan 			break;
12228a978234SLiam Girdwood 		}
12238a978234SLiam Girdwood 
12247df04ea7SRanjani Sridharan 		routes[i]->source = elem->source;
12257df04ea7SRanjani Sridharan 		routes[i]->sink = elem->sink;
12267df04ea7SRanjani Sridharan 
12277df04ea7SRanjani Sridharan 		/* set to NULL atm for tplg users */
12287df04ea7SRanjani Sridharan 		routes[i]->connected = NULL;
12297df04ea7SRanjani Sridharan 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
12307df04ea7SRanjani Sridharan 			routes[i]->control = NULL;
12317df04ea7SRanjani Sridharan 		else
12327df04ea7SRanjani Sridharan 			routes[i]->control = elem->control;
12337df04ea7SRanjani Sridharan 
12347df04ea7SRanjani Sridharan 		/* add route dobj to dobj_list */
12357df04ea7SRanjani Sridharan 		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
12367df04ea7SRanjani Sridharan 		routes[i]->dobj.ops = tplg->ops;
12377df04ea7SRanjani Sridharan 		routes[i]->dobj.index = tplg->index;
12387df04ea7SRanjani Sridharan 		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
12397df04ea7SRanjani Sridharan 
12407df04ea7SRanjani Sridharan 		soc_tplg_add_route(tplg, routes[i]);
12417df04ea7SRanjani Sridharan 
12427df04ea7SRanjani Sridharan 		/* add route, but keep going if some fail */
12437df04ea7SRanjani Sridharan 		snd_soc_dapm_add_routes(dapm, routes[i], 1);
12447df04ea7SRanjani Sridharan 	}
12457df04ea7SRanjani Sridharan 
12467df04ea7SRanjani Sridharan 	/* free memory allocated for all dapm routes in case of error */
12477df04ea7SRanjani Sridharan 	if (ret < 0)
12487df04ea7SRanjani Sridharan 		for (i = 0; i < count ; i++)
12497df04ea7SRanjani Sridharan 			kfree(routes[i]);
12507df04ea7SRanjani Sridharan 
12517df04ea7SRanjani Sridharan 	/*
12527df04ea7SRanjani Sridharan 	 * free pointer to array of dapm routes as this is no longer needed.
12537df04ea7SRanjani Sridharan 	 * The memory allocated for each dapm route will be freed
12547df04ea7SRanjani Sridharan 	 * when it is removed in remove_route().
12557df04ea7SRanjani Sridharan 	 */
12567df04ea7SRanjani Sridharan 	kfree(routes);
12577df04ea7SRanjani Sridharan 
12587df04ea7SRanjani Sridharan 	return ret;
12598a978234SLiam Girdwood }
12608a978234SLiam Girdwood 
12618a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
12628a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
12638a978234SLiam Girdwood {
12648a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
12658a978234SLiam Girdwood 	struct soc_mixer_control *sm;
12668a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
12678a978234SLiam Girdwood 	int i, err;
12688a978234SLiam Girdwood 
12694ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
12708a978234SLiam Girdwood 	if (kc == NULL)
12718a978234SLiam Girdwood 		return NULL;
12728a978234SLiam Girdwood 
12738a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
12748a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
12758a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
12768a978234SLiam Girdwood 		if (sm == NULL)
12778a978234SLiam Girdwood 			goto err;
12788a978234SLiam Girdwood 
12798a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
12808a978234SLiam Girdwood 			mc->priv.size);
12818a978234SLiam Girdwood 
12828a978234SLiam Girdwood 		/* validate kcontrol */
12838a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12848a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
12858a978234SLiam Girdwood 			goto err_str;
12868a978234SLiam Girdwood 
12878a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
12888a978234SLiam Girdwood 			mc->hdr.name, i);
12898a978234SLiam Girdwood 
1290267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1291267e2c6fSLiam Girdwood 		if (kc[i].name == NULL)
1292267e2c6fSLiam Girdwood 			goto err_str;
12938a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
12948a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
12958a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
12968a978234SLiam Girdwood 
12978a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
12988a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
12998a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13008a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
13018a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13028a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
13038a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
13048a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
13058a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
13068a978234SLiam Girdwood 
13078a978234SLiam Girdwood 		sm->max = mc->max;
13088a978234SLiam Girdwood 		sm->min = mc->min;
13098a978234SLiam Girdwood 		sm->invert = mc->invert;
13108a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
13118a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
13128a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
13138a978234SLiam Girdwood 
13148a978234SLiam Girdwood 		/* map io handlers */
13152b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
13168a978234SLiam Girdwood 		if (err) {
13178a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
13188a978234SLiam Girdwood 			kfree(sm);
13198a978234SLiam Girdwood 			continue;
13208a978234SLiam Girdwood 		}
13218a978234SLiam Girdwood 
13228a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13238a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13248a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
13258a978234SLiam Girdwood 		if (err < 0) {
13268a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13278a978234SLiam Girdwood 				mc->hdr.name);
13288a978234SLiam Girdwood 			kfree(sm);
13298a978234SLiam Girdwood 			continue;
13308a978234SLiam Girdwood 		}
1331bde8b388SRanjani Sridharan 
1332bde8b388SRanjani Sridharan 		/* create any TLV data */
1333bde8b388SRanjani Sridharan 		soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
13348a978234SLiam Girdwood 	}
13358a978234SLiam Girdwood 	return kc;
13368a978234SLiam Girdwood 
13378a978234SLiam Girdwood err_str:
13388a978234SLiam Girdwood 	kfree(sm);
13398a978234SLiam Girdwood err:
1340267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
13418a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1342267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1343267e2c6fSLiam Girdwood 	}
13448a978234SLiam Girdwood 	kfree(kc);
13458a978234SLiam Girdwood 	return NULL;
13468a978234SLiam Girdwood }
13478a978234SLiam Girdwood 
13488a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
13491a7dd6e2SMengdong Lin 	struct soc_tplg *tplg, int num_kcontrols)
13508a978234SLiam Girdwood {
13518a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13528a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
13538a978234SLiam Girdwood 	struct soc_enum *se;
13541a7dd6e2SMengdong Lin 	int i, j, err;
13558a978234SLiam Girdwood 
13561a7dd6e2SMengdong Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
13571a7dd6e2SMengdong Lin 	if (kc == NULL)
13581a7dd6e2SMengdong Lin 		return NULL;
13591a7dd6e2SMengdong Lin 
13601a7dd6e2SMengdong Lin 	for (i = 0; i < num_kcontrols; i++) {
13618a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
13628a978234SLiam Girdwood 		/* validate kcontrol */
13638a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13648a978234SLiam Girdwood 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1365f3ee9096SChristophe JAILLET 			goto err;
13668a978234SLiam Girdwood 
13678a978234SLiam Girdwood 		se = kzalloc(sizeof(*se), GFP_KERNEL);
13688a978234SLiam Girdwood 		if (se == NULL)
13698a978234SLiam Girdwood 			goto err;
13708a978234SLiam Girdwood 
13718a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
13728a978234SLiam Girdwood 			ec->hdr.name);
13738a978234SLiam Girdwood 
1374267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
137565030ff3SDan Carpenter 		if (kc[i].name == NULL) {
137665030ff3SDan Carpenter 			kfree(se);
1377267e2c6fSLiam Girdwood 			goto err_se;
137865030ff3SDan Carpenter 		}
13791a7dd6e2SMengdong Lin 		kc[i].private_value = (long)se;
13801a7dd6e2SMengdong Lin 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13811a7dd6e2SMengdong Lin 		kc[i].access = ec->hdr.access;
13828a978234SLiam Girdwood 
13838a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
13848a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
13851a7dd6e2SMengdong Lin 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
13861a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FL);
13871a7dd6e2SMengdong Lin 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
13881a7dd6e2SMengdong Lin 						  SNDRV_CHMAP_FR);
13898a978234SLiam Girdwood 
13908a978234SLiam Girdwood 		se->items = ec->items;
13918a978234SLiam Girdwood 		se->mask = ec->mask;
13928a978234SLiam Girdwood 		se->dobj.index = tplg->index;
13938a978234SLiam Girdwood 
13948a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
13958a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
13968a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
13978a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
13988a978234SLiam Girdwood 			if (err < 0) {
13998a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
14008a978234SLiam Girdwood 					ec->hdr.name);
14018a978234SLiam Girdwood 				goto err_se;
14028a978234SLiam Girdwood 			}
14039c6c4d96STakashi Iwai 			/* fall through */
14048a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
14058a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14068a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14078a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
14088a978234SLiam Girdwood 			if (err < 0) {
14098a978234SLiam Girdwood 				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
14108a978234SLiam Girdwood 					ec->hdr.name);
14118a978234SLiam Girdwood 				goto err_se;
14128a978234SLiam Girdwood 			}
14138a978234SLiam Girdwood 			break;
14148a978234SLiam Girdwood 		default:
14158a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
14168a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
14178a978234SLiam Girdwood 			goto err_se;
14188a978234SLiam Girdwood 		}
14198a978234SLiam Girdwood 
14208a978234SLiam Girdwood 		/* map io handlers */
14211a7dd6e2SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
14228a978234SLiam Girdwood 		if (err) {
14238a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
14248a978234SLiam Girdwood 			goto err_se;
14258a978234SLiam Girdwood 		}
14268a978234SLiam Girdwood 
14278a978234SLiam Girdwood 		/* pass control to driver for optional further init */
14281a7dd6e2SMengdong Lin 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
14298a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)ec);
14308a978234SLiam Girdwood 		if (err < 0) {
14318a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
14328a978234SLiam Girdwood 				ec->hdr.name);
14338a978234SLiam Girdwood 			goto err_se;
14348a978234SLiam Girdwood 		}
14358a978234SLiam Girdwood 
14361a7dd6e2SMengdong Lin 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
14371a7dd6e2SMengdong Lin 				ec->priv.size);
14381a7dd6e2SMengdong Lin 	}
14391a7dd6e2SMengdong Lin 
14408a978234SLiam Girdwood 	return kc;
14418a978234SLiam Girdwood 
14428a978234SLiam Girdwood err_se:
14431a7dd6e2SMengdong Lin 	for (; i >= 0; i--) {
14448a978234SLiam Girdwood 		/* free values and texts */
14451a7dd6e2SMengdong Lin 		se = (struct soc_enum *)kc[i].private_value;
14466d5574edSChristophe JAILLET 		if (!se)
14476d5574edSChristophe JAILLET 			continue;
14486d5574edSChristophe JAILLET 
14498a978234SLiam Girdwood 		kfree(se->dobj.control.dvalues);
14501a7dd6e2SMengdong Lin 		for (j = 0; j < ec->items; j++)
14511a7dd6e2SMengdong Lin 			kfree(se->dobj.control.dtexts[j]);
145234db6a3eSAmadeusz Sławiński 		kfree(se->dobj.control.dtexts);
14538a978234SLiam Girdwood 
14548a978234SLiam Girdwood 		kfree(se);
1455267e2c6fSLiam Girdwood 		kfree(kc[i].name);
14561a7dd6e2SMengdong Lin 	}
14578a978234SLiam Girdwood err:
14588a978234SLiam Girdwood 	kfree(kc);
14598a978234SLiam Girdwood 
14608a978234SLiam Girdwood 	return NULL;
14618a978234SLiam Girdwood }
14628a978234SLiam Girdwood 
14638a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
14648a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
14658a978234SLiam Girdwood {
14668a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
14678a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
14688a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
14698a978234SLiam Girdwood 	int i, err;
14708a978234SLiam Girdwood 
14714ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
14728a978234SLiam Girdwood 	if (!kc)
14738a978234SLiam Girdwood 		return NULL;
14748a978234SLiam Girdwood 
14758a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
14768a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
14778a978234SLiam Girdwood 
14788a978234SLiam Girdwood 		/* validate kcontrol */
14798a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
14808a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
14818a978234SLiam Girdwood 			goto err;
14828a978234SLiam Girdwood 
14838a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
14848a978234SLiam Girdwood 		if (sbe == NULL)
14858a978234SLiam Girdwood 			goto err;
14868a978234SLiam Girdwood 
14878a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
14888a978234SLiam Girdwood 			be->priv.size);
14898a978234SLiam Girdwood 
14908a978234SLiam Girdwood 		dev_dbg(tplg->dev,
14918a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
14928a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
14938a978234SLiam Girdwood 
1494267e2c6fSLiam Girdwood 		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
149565030ff3SDan Carpenter 		if (kc[i].name == NULL) {
149665030ff3SDan Carpenter 			kfree(sbe);
1497267e2c6fSLiam Girdwood 			goto err;
149865030ff3SDan Carpenter 		}
14998a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
15008a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
15018a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
15028a978234SLiam Girdwood 
15038a978234SLiam Girdwood 		sbe->max = be->max;
15048a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
15058a978234SLiam Girdwood 
15068a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
15072b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
15088a978234SLiam Girdwood 		if (err) {
15098a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
15108a978234SLiam Girdwood 			kfree(sbe);
15118a978234SLiam Girdwood 			continue;
15128a978234SLiam Girdwood 		}
15138a978234SLiam Girdwood 
15148a978234SLiam Girdwood 		/* pass control to driver for optional further init */
15158a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
15168a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
15178a978234SLiam Girdwood 		if (err < 0) {
15188a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
15198a978234SLiam Girdwood 				be->hdr.name);
15208a978234SLiam Girdwood 			kfree(sbe);
15218a978234SLiam Girdwood 			continue;
15228a978234SLiam Girdwood 		}
15238a978234SLiam Girdwood 	}
15248a978234SLiam Girdwood 
15258a978234SLiam Girdwood 	return kc;
15268a978234SLiam Girdwood 
15278a978234SLiam Girdwood err:
1528267e2c6fSLiam Girdwood 	for (--i; i >= 0; i--) {
15298a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
1530267e2c6fSLiam Girdwood 		kfree(kc[i].name);
1531267e2c6fSLiam Girdwood 	}
15328a978234SLiam Girdwood 
15338a978234SLiam Girdwood 	kfree(kc);
15348a978234SLiam Girdwood 	return NULL;
15358a978234SLiam Girdwood }
15368a978234SLiam Girdwood 
15378a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
15388a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
15398a978234SLiam Girdwood {
15408a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
15418a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
15428a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
15438a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
1544eea3dd4fSMengdong Lin 	unsigned int kcontrol_type;
15458a978234SLiam Girdwood 	int ret = 0;
15468a978234SLiam Girdwood 
15478a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15488a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15498a978234SLiam Girdwood 		return -EINVAL;
15508a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
15518a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
15528a978234SLiam Girdwood 		return -EINVAL;
15538a978234SLiam Girdwood 
15548a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
15558a978234SLiam Girdwood 		w->name, w->id);
15568a978234SLiam Girdwood 
15578a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
15588a978234SLiam Girdwood 
15598a978234SLiam Girdwood 	/* map user to kernel widget ID */
15608a978234SLiam Girdwood 	template.id = get_widget_id(w->id);
15618a978234SLiam Girdwood 	if (template.id < 0)
15628a978234SLiam Girdwood 		return template.id;
15638a978234SLiam Girdwood 
1564c3421a6aSLiam Girdwood 	/* strings are allocated here, but used and freed by the widget */
15658a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
15668a978234SLiam Girdwood 	if (!template.name)
15678a978234SLiam Girdwood 		return -ENOMEM;
15688a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
15698a978234SLiam Girdwood 	if (!template.sname) {
15708a978234SLiam Girdwood 		ret = -ENOMEM;
15718a978234SLiam Girdwood 		goto err;
15728a978234SLiam Girdwood 	}
15738a978234SLiam Girdwood 	template.reg = w->reg;
15748a978234SLiam Girdwood 	template.shift = w->shift;
15758a978234SLiam Girdwood 	template.mask = w->mask;
15766dc6db79SSubhransu S. Prusty 	template.subseq = w->subseq;
15778a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
15788a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
15798a978234SLiam Girdwood 	template.ignore_suspend = w->ignore_suspend;
15808a978234SLiam Girdwood 	template.event_flags = w->event_flags;
15818a978234SLiam Girdwood 	template.dobj.index = tplg->index;
15828a978234SLiam Girdwood 
15838a978234SLiam Girdwood 	tplg->pos +=
15848a978234SLiam Girdwood 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
15858a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
1586dd5abb74SArnd Bergmann 		kcontrol_type = 0;
15878a978234SLiam Girdwood 		template.num_kcontrols = 0;
15888a978234SLiam Girdwood 		goto widget;
15898a978234SLiam Girdwood 	}
15908a978234SLiam Girdwood 
15918a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
15928a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
15938a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
15948a978234SLiam Girdwood 
15958a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
15968a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
15978a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
15988a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
15998a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
16008a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
16018a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1602eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
16038a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
16048a978234SLiam Girdwood 		template.kcontrol_news =
16058a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
16068a978234SLiam Girdwood 			template.num_kcontrols);
16078a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16088a978234SLiam Girdwood 			ret = -ENOMEM;
16098a978234SLiam Girdwood 			goto hdr_err;
16108a978234SLiam Girdwood 		}
16118a978234SLiam Girdwood 		break;
16128a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
16138a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
16148a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
16158a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
16168a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1617eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
16181a7dd6e2SMengdong Lin 		template.num_kcontrols = w->num_kcontrols;
16198a978234SLiam Girdwood 		template.kcontrol_news =
16201a7dd6e2SMengdong Lin 			soc_tplg_dapm_widget_denum_create(tplg,
16211a7dd6e2SMengdong Lin 			template.num_kcontrols);
16228a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16238a978234SLiam Girdwood 			ret = -ENOMEM;
16248a978234SLiam Girdwood 			goto hdr_err;
16258a978234SLiam Girdwood 		}
16268a978234SLiam Girdwood 		break;
16278a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
1628eea3dd4fSMengdong Lin 		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
16298a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
16308a978234SLiam Girdwood 		template.kcontrol_news =
16318a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
16328a978234SLiam Girdwood 				template.num_kcontrols);
16338a978234SLiam Girdwood 		if (!template.kcontrol_news) {
16348a978234SLiam Girdwood 			ret = -ENOMEM;
16358a978234SLiam Girdwood 			goto hdr_err;
16368a978234SLiam Girdwood 		}
16378a978234SLiam Girdwood 		break;
16388a978234SLiam Girdwood 	default:
16398a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
16408a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
16418a978234SLiam Girdwood 			control_hdr->ops.info);
16428a978234SLiam Girdwood 		ret = -EINVAL;
16438a978234SLiam Girdwood 		goto hdr_err;
16448a978234SLiam Girdwood 	}
16458a978234SLiam Girdwood 
16468a978234SLiam Girdwood widget:
16478a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
16488a978234SLiam Girdwood 	if (ret < 0)
16498a978234SLiam Girdwood 		goto hdr_err;
16508a978234SLiam Girdwood 
16518a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
16528a978234SLiam Girdwood 	 * data during sound card init. */
16538a978234SLiam Girdwood 	if (card->instantiated)
16548a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
16558a978234SLiam Girdwood 	else
16568a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
165737e1df8cSLinus Walleij 	if (IS_ERR(widget)) {
165837e1df8cSLinus Walleij 		ret = PTR_ERR(widget);
16598a978234SLiam Girdwood 		goto hdr_err;
16608a978234SLiam Girdwood 	}
16618a978234SLiam Girdwood 
16628a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1663eea3dd4fSMengdong Lin 	widget->dobj.widget.kcontrol_type = kcontrol_type;
16648a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
16658a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
16668a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1667ebd259d3SLiam Girdwood 
1668ebd259d3SLiam Girdwood 	ret = soc_tplg_widget_ready(tplg, widget, w);
1669ebd259d3SLiam Girdwood 	if (ret < 0)
1670ebd259d3SLiam Girdwood 		goto ready_err;
1671ebd259d3SLiam Girdwood 
16727620fe91SBard liao 	kfree(template.sname);
16737620fe91SBard liao 	kfree(template.name);
16747620fe91SBard liao 
16758a978234SLiam Girdwood 	return 0;
16768a978234SLiam Girdwood 
1677ebd259d3SLiam Girdwood ready_err:
1678ebd259d3SLiam Girdwood 	snd_soc_tplg_widget_remove(widget);
1679ebd259d3SLiam Girdwood 	snd_soc_dapm_free_widget(widget);
16808a978234SLiam Girdwood hdr_err:
16818a978234SLiam Girdwood 	kfree(template.sname);
16828a978234SLiam Girdwood err:
16838a978234SLiam Girdwood 	kfree(template.name);
16848a978234SLiam Girdwood 	return ret;
16858a978234SLiam Girdwood }
16868a978234SLiam Girdwood 
16878a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
16888a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
16898a978234SLiam Girdwood {
16908a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
16918a978234SLiam Girdwood 	int ret, count = hdr->count, i;
16928a978234SLiam Girdwood 
16938a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
16948a978234SLiam Girdwood 		return 0;
16958a978234SLiam Girdwood 
16968a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
16978a978234SLiam Girdwood 
16988a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
16998a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
170006eb49f7SMengdong Lin 		if (widget->size != sizeof(*widget)) {
170106eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
170206eb49f7SMengdong Lin 			return -EINVAL;
170306eb49f7SMengdong Lin 		}
170406eb49f7SMengdong Lin 
17058a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
17067de76b62SMengdong Lin 		if (ret < 0) {
17078a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
17088a978234SLiam Girdwood 				widget->name);
17097de76b62SMengdong Lin 			return ret;
17107de76b62SMengdong Lin 		}
17118a978234SLiam Girdwood 	}
17128a978234SLiam Girdwood 
17138a978234SLiam Girdwood 	return 0;
17148a978234SLiam Girdwood }
17158a978234SLiam Girdwood 
17168a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
17178a978234SLiam Girdwood {
17188a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
17198a978234SLiam Girdwood 	int ret;
17208a978234SLiam Girdwood 
17218a978234SLiam Girdwood 	/* Card might not have been registered at this point.
17228a978234SLiam Girdwood 	 * If so, just return success.
17238a978234SLiam Girdwood 	*/
17248a978234SLiam Girdwood 	if (!card || !card->instantiated) {
17258a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1726cc9d4714SLiam Girdwood 			" widget card binding deferred\n");
17278a978234SLiam Girdwood 		return 0;
17288a978234SLiam Girdwood 	}
17298a978234SLiam Girdwood 
17308a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
17318a978234SLiam Girdwood 	if (ret < 0)
17328a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
17338a978234SLiam Girdwood 			ret);
17348a978234SLiam Girdwood 
17358a978234SLiam Girdwood 	return 0;
17368a978234SLiam Girdwood }
17378a978234SLiam Girdwood 
1738b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream,
1739b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1740b6b6e4d6SMengdong Lin {
1741b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1742b6b6e4d6SMengdong Lin 	stream->channels_min = caps->channels_min;
1743b6b6e4d6SMengdong Lin 	stream->channels_max = caps->channels_max;
1744b6b6e4d6SMengdong Lin 	stream->rates = caps->rates;
1745b6b6e4d6SMengdong Lin 	stream->rate_min = caps->rate_min;
1746b6b6e4d6SMengdong Lin 	stream->rate_max = caps->rate_max;
1747b6b6e4d6SMengdong Lin 	stream->formats = caps->formats;
1748f918e169SMengdong Lin 	stream->sig_bits = caps->sig_bits;
1749b6b6e4d6SMengdong Lin }
1750b6b6e4d6SMengdong Lin 
17510038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
17520038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
17530038be9aSMengdong Lin {
17540038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
17550038be9aSMengdong Lin 		dai_drv->symmetric_rates =
17560038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
17570038be9aSMengdong Lin 
17580038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
17590038be9aSMengdong Lin 		dai_drv->symmetric_channels =
17600038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
17610038be9aSMengdong Lin 			1 : 0;
17620038be9aSMengdong Lin 
17630038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
17640038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
17650038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
17660038be9aSMengdong Lin 			1 : 0;
17670038be9aSMengdong Lin }
17680038be9aSMengdong Lin 
176964527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
177064527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
177164527e8aSMengdong Lin {
177264527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
177364527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
177464527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
177564527e8aSMengdong Lin 	int ret;
177664527e8aSMengdong Lin 
177764527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
177864527e8aSMengdong Lin 	if (dai_drv == NULL)
177964527e8aSMengdong Lin 		return -ENOMEM;
178064527e8aSMengdong Lin 
17818f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
17828f27c4abSMengdong Lin 		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
178364527e8aSMengdong Lin 	dai_drv->id = pcm->dai_id;
178464527e8aSMengdong Lin 
178564527e8aSMengdong Lin 	if (pcm->playback) {
178664527e8aSMengdong Lin 		stream = &dai_drv->playback;
178764527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1788b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
178964527e8aSMengdong Lin 	}
179064527e8aSMengdong Lin 
179164527e8aSMengdong Lin 	if (pcm->capture) {
179264527e8aSMengdong Lin 		stream = &dai_drv->capture;
179364527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1794b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
179564527e8aSMengdong Lin 	}
179664527e8aSMengdong Lin 
17975db6aab6SLiam Girdwood 	if (pcm->compress)
17985db6aab6SLiam Girdwood 		dai_drv->compress_new = snd_soc_new_compress;
17995db6aab6SLiam Girdwood 
180064527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
1801c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
180264527e8aSMengdong Lin 	if (ret < 0) {
180364527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
180464527e8aSMengdong Lin 		kfree(dai_drv);
180564527e8aSMengdong Lin 		return ret;
180664527e8aSMengdong Lin 	}
180764527e8aSMengdong Lin 
180864527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
180964527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
181064527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
181164527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
181264527e8aSMengdong Lin 
181364527e8aSMengdong Lin 	/* register the DAI to the component */
181464527e8aSMengdong Lin 	return snd_soc_register_dai(tplg->comp, dai_drv);
181564527e8aSMengdong Lin }
181664527e8aSMengdong Lin 
1817717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link,
1818717a8e72SMengdong Lin 		unsigned int flag_mask, unsigned int flags)
1819717a8e72SMengdong Lin {
1820717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1821717a8e72SMengdong Lin 		link->symmetric_rates =
1822717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1823717a8e72SMengdong Lin 
1824717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1825717a8e72SMengdong Lin 		link->symmetric_channels =
1826717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1827717a8e72SMengdong Lin 			1 : 0;
1828717a8e72SMengdong Lin 
1829717a8e72SMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1830717a8e72SMengdong Lin 		link->symmetric_samplebits =
1831717a8e72SMengdong Lin 			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1832717a8e72SMengdong Lin 			1 : 0;
18336ff67ccaSMengdong Lin 
18346ff67ccaSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
18356ff67ccaSMengdong Lin 		link->ignore_suspend =
18366ff67ccaSMengdong Lin 		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
18376ff67ccaSMengdong Lin 		1 : 0;
1838717a8e72SMengdong Lin }
1839717a8e72SMengdong Lin 
184067d1c21eSGuneshwor Singh /* create the FE DAI link */
1841ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1842acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1843acfc7d46SMengdong Lin {
1844acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
1845acfc7d46SMengdong Lin 	int ret;
1846acfc7d46SMengdong Lin 
1847acfc7d46SMengdong Lin 	link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1848acfc7d46SMengdong Lin 	if (link == NULL)
1849acfc7d46SMengdong Lin 		return -ENOMEM;
1850acfc7d46SMengdong Lin 
18518f27c4abSMengdong Lin 	if (strlen(pcm->pcm_name)) {
18528f27c4abSMengdong Lin 		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
18538f27c4abSMengdong Lin 		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
18548f27c4abSMengdong Lin 	}
1855b84fff5aSMengdong Lin 	link->id = pcm->pcm_id;
1856acfc7d46SMengdong Lin 
18578f27c4abSMengdong Lin 	if (strlen(pcm->dai_name))
18588f27c4abSMengdong Lin 		link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
18598f27c4abSMengdong Lin 
186067d1c21eSGuneshwor Singh 	link->codec_name = "snd-soc-dummy";
186167d1c21eSGuneshwor Singh 	link->codec_dai_name = "snd-soc-dummy-dai";
186267d1c21eSGuneshwor Singh 
186367d1c21eSGuneshwor Singh 	/* enable DPCM */
186467d1c21eSGuneshwor Singh 	link->dynamic = 1;
186567d1c21eSGuneshwor Singh 	link->dpcm_playback = pcm->playback;
186667d1c21eSGuneshwor Singh 	link->dpcm_capture = pcm->capture;
1867717a8e72SMengdong Lin 	if (pcm->flag_mask)
1868717a8e72SMengdong Lin 		set_link_flags(link, pcm->flag_mask, pcm->flags);
186967d1c21eSGuneshwor Singh 
1870acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1871c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1872acfc7d46SMengdong Lin 	if (ret < 0) {
1873acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1874acfc7d46SMengdong Lin 		kfree(link);
1875acfc7d46SMengdong Lin 		return ret;
1876acfc7d46SMengdong Lin 	}
1877acfc7d46SMengdong Lin 
1878acfc7d46SMengdong Lin 	link->dobj.index = tplg->index;
1879acfc7d46SMengdong Lin 	link->dobj.ops = tplg->ops;
1880acfc7d46SMengdong Lin 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1881acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1882acfc7d46SMengdong Lin 
1883acfc7d46SMengdong Lin 	snd_soc_add_dai_link(tplg->comp->card, link);
1884acfc7d46SMengdong Lin 	return 0;
1885acfc7d46SMengdong Lin }
1886acfc7d46SMengdong Lin 
1887acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
188864527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
188964527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
189064527e8aSMengdong Lin {
1891acfc7d46SMengdong Lin 	int ret;
1892acfc7d46SMengdong Lin 
1893acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1894acfc7d46SMengdong Lin 	if (ret < 0)
1895acfc7d46SMengdong Lin 		return ret;
1896acfc7d46SMengdong Lin 
1897ab4bc5eeSMengdong Lin 	return  soc_tplg_fe_link_create(tplg, pcm);
189864527e8aSMengdong Lin }
189964527e8aSMengdong Lin 
190055726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */
190155726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
190255726dc9SMengdong Lin 				struct snd_soc_tplg_stream_caps_v4 *src)
190355726dc9SMengdong Lin {
190455726dc9SMengdong Lin 	dest->size = sizeof(*dest);
190555726dc9SMengdong Lin 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
190655726dc9SMengdong Lin 	dest->formats = src->formats;
190755726dc9SMengdong Lin 	dest->rates = src->rates;
190855726dc9SMengdong Lin 	dest->rate_min = src->rate_min;
190955726dc9SMengdong Lin 	dest->rate_max = src->rate_max;
191055726dc9SMengdong Lin 	dest->channels_min = src->channels_min;
191155726dc9SMengdong Lin 	dest->channels_max = src->channels_max;
191255726dc9SMengdong Lin 	dest->periods_min = src->periods_min;
191355726dc9SMengdong Lin 	dest->periods_max = src->periods_max;
191455726dc9SMengdong Lin 	dest->period_size_min = src->period_size_min;
191555726dc9SMengdong Lin 	dest->period_size_max = src->period_size_max;
191655726dc9SMengdong Lin 	dest->buffer_size_min = src->buffer_size_min;
191755726dc9SMengdong Lin 	dest->buffer_size_max = src->buffer_size_max;
191855726dc9SMengdong Lin }
191955726dc9SMengdong Lin 
192055726dc9SMengdong Lin /**
192155726dc9SMengdong Lin  * pcm_new_ver - Create the new version of PCM from the old version.
192255726dc9SMengdong Lin  * @tplg: topology context
192355726dc9SMengdong Lin  * @src: older version of pcm as a source
192455726dc9SMengdong Lin  * @pcm: latest version of pcm created from the source
192555726dc9SMengdong Lin  *
192655726dc9SMengdong Lin  * Support from vesion 4. User should free the returned pcm manually.
192755726dc9SMengdong Lin  */
192855726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg,
192955726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm *src,
193055726dc9SMengdong Lin 		       struct snd_soc_tplg_pcm **pcm)
193155726dc9SMengdong Lin {
193255726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *dest;
193355726dc9SMengdong Lin 	struct snd_soc_tplg_pcm_v4 *src_v4;
193455726dc9SMengdong Lin 	int i;
193555726dc9SMengdong Lin 
193655726dc9SMengdong Lin 	*pcm = NULL;
193755726dc9SMengdong Lin 
193855726dc9SMengdong Lin 	if (src->size != sizeof(*src_v4)) {
193955726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
194055726dc9SMengdong Lin 		return -EINVAL;
194155726dc9SMengdong Lin 	}
194255726dc9SMengdong Lin 
194355726dc9SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
194455726dc9SMengdong Lin 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
194555726dc9SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
194655726dc9SMengdong Lin 	if (!dest)
194755726dc9SMengdong Lin 		return -ENOMEM;
194855726dc9SMengdong Lin 
194955726dc9SMengdong Lin 	dest->size = sizeof(*dest);	/* size of latest abi version */
195055726dc9SMengdong Lin 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
195155726dc9SMengdong Lin 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
195255726dc9SMengdong Lin 	dest->pcm_id = src_v4->pcm_id;
195355726dc9SMengdong Lin 	dest->dai_id = src_v4->dai_id;
195455726dc9SMengdong Lin 	dest->playback = src_v4->playback;
195555726dc9SMengdong Lin 	dest->capture = src_v4->capture;
195655726dc9SMengdong Lin 	dest->compress = src_v4->compress;
195755726dc9SMengdong Lin 	dest->num_streams = src_v4->num_streams;
195855726dc9SMengdong Lin 	for (i = 0; i < dest->num_streams; i++)
195955726dc9SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
196055726dc9SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
196155726dc9SMengdong Lin 
196255726dc9SMengdong Lin 	for (i = 0; i < 2; i++)
196355726dc9SMengdong Lin 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
196455726dc9SMengdong Lin 
196555726dc9SMengdong Lin 	*pcm = dest;
196655726dc9SMengdong Lin 	return 0;
196755726dc9SMengdong Lin }
196855726dc9SMengdong Lin 
196964527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
19708a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
19718a978234SLiam Girdwood {
197255726dc9SMengdong Lin 	struct snd_soc_tplg_pcm *pcm, *_pcm;
19738a978234SLiam Girdwood 	int count = hdr->count;
1974fd340455SVinod Koul 	int i;
197555726dc9SMengdong Lin 	bool abi_match;
19768a978234SLiam Girdwood 
19778a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
19788a978234SLiam Girdwood 		return 0;
19798a978234SLiam Girdwood 
198055726dc9SMengdong Lin 	/* check the element size and count */
198155726dc9SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
198255726dc9SMengdong Lin 	if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
198355726dc9SMengdong Lin 		|| pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
198455726dc9SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
198555726dc9SMengdong Lin 			pcm->size);
198655726dc9SMengdong Lin 		return -EINVAL;
198755726dc9SMengdong Lin 	}
198855726dc9SMengdong Lin 
19898a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
199055726dc9SMengdong Lin 		pcm->size, count,
19918a978234SLiam Girdwood 		hdr->payload_size, "PCM DAI")) {
19928a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
19938a978234SLiam Girdwood 			count);
19948a978234SLiam Girdwood 		return -EINVAL;
19958a978234SLiam Girdwood 	}
19968a978234SLiam Girdwood 
199764527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
199855726dc9SMengdong Lin 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
199955726dc9SMengdong Lin 
200055726dc9SMengdong Lin 		/* check ABI version by size, create a new version of pcm
200155726dc9SMengdong Lin 		 * if abi not match.
200255726dc9SMengdong Lin 		 */
200355726dc9SMengdong Lin 		if (pcm->size == sizeof(*pcm)) {
200455726dc9SMengdong Lin 			abi_match = true;
200555726dc9SMengdong Lin 			_pcm = pcm;
200655726dc9SMengdong Lin 		} else {
200755726dc9SMengdong Lin 			abi_match = false;
2008fd340455SVinod Koul 			pcm_new_ver(tplg, pcm, &_pcm);
200906eb49f7SMengdong Lin 		}
201006eb49f7SMengdong Lin 
201155726dc9SMengdong Lin 		/* create the FE DAIs and DAI links */
201255726dc9SMengdong Lin 		soc_tplg_pcm_create(tplg, _pcm);
201355726dc9SMengdong Lin 
2014717a8e72SMengdong Lin 		/* offset by version-specific struct size and
2015717a8e72SMengdong Lin 		 * real priv data size
2016717a8e72SMengdong Lin 		 */
2017717a8e72SMengdong Lin 		tplg->pos += pcm->size + _pcm->priv.size;
2018717a8e72SMengdong Lin 
201955726dc9SMengdong Lin 		if (!abi_match)
202055726dc9SMengdong Lin 			kfree(_pcm); /* free the duplicated one */
202164527e8aSMengdong Lin 	}
202264527e8aSMengdong Lin 
20238a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
20248a978234SLiam Girdwood 
20258a978234SLiam Girdwood 	return 0;
20268a978234SLiam Girdwood }
20278a978234SLiam Girdwood 
20280038be9aSMengdong Lin /**
2029593d9e52SMengdong Lin  * set_link_hw_format - Set the HW audio format of the physical DAI link.
20308abab35fSCharles Keepax  * @link: &snd_soc_dai_link which should be updated
2031593d9e52SMengdong Lin  * @cfg: physical link configs.
2032593d9e52SMengdong Lin  *
2033593d9e52SMengdong Lin  * Topology context contains a list of supported HW formats (configs) and
2034593d9e52SMengdong Lin  * a default format ID for the physical link. This function will use this
2035593d9e52SMengdong Lin  * default ID to choose the HW format to set the link's DAI format for init.
2036593d9e52SMengdong Lin  */
2037593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link,
2038593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *cfg)
2039593d9e52SMengdong Lin {
2040593d9e52SMengdong Lin 	struct snd_soc_tplg_hw_config *hw_config;
2041593d9e52SMengdong Lin 	unsigned char bclk_master, fsync_master;
2042593d9e52SMengdong Lin 	unsigned char invert_bclk, invert_fsync;
2043593d9e52SMengdong Lin 	int i;
2044593d9e52SMengdong Lin 
2045593d9e52SMengdong Lin 	for (i = 0; i < cfg->num_hw_configs; i++) {
2046593d9e52SMengdong Lin 		hw_config = &cfg->hw_config[i];
2047593d9e52SMengdong Lin 		if (hw_config->id != cfg->default_hw_config_id)
2048593d9e52SMengdong Lin 			continue;
2049593d9e52SMengdong Lin 
2050593d9e52SMengdong Lin 		link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2051593d9e52SMengdong Lin 
2052933e1c4aSKirill Marinushkin 		/* clock gating */
2053fbeabd09SKirill Marinushkin 		switch (hw_config->clock_gated) {
2054fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2055933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2056fbeabd09SKirill Marinushkin 			break;
2057fbeabd09SKirill Marinushkin 
2058fbeabd09SKirill Marinushkin 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2059933e1c4aSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2060fbeabd09SKirill Marinushkin 			break;
2061fbeabd09SKirill Marinushkin 
2062fbeabd09SKirill Marinushkin 		default:
2063fbeabd09SKirill Marinushkin 			/* ignore the value */
2064fbeabd09SKirill Marinushkin 			break;
2065fbeabd09SKirill Marinushkin 		}
2066933e1c4aSKirill Marinushkin 
2067593d9e52SMengdong Lin 		/* clock signal polarity */
2068593d9e52SMengdong Lin 		invert_bclk = hw_config->invert_bclk;
2069593d9e52SMengdong Lin 		invert_fsync = hw_config->invert_fsync;
2070593d9e52SMengdong Lin 		if (!invert_bclk && !invert_fsync)
2071593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2072593d9e52SMengdong Lin 		else if (!invert_bclk && invert_fsync)
2073593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2074593d9e52SMengdong Lin 		else if (invert_bclk && !invert_fsync)
2075593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2076593d9e52SMengdong Lin 		else
2077593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2078593d9e52SMengdong Lin 
2079593d9e52SMengdong Lin 		/* clock masters */
2080a941e2faSKirill Marinushkin 		bclk_master = (hw_config->bclk_master ==
2081a941e2faSKirill Marinushkin 			       SND_SOC_TPLG_BCLK_CM);
2082a941e2faSKirill Marinushkin 		fsync_master = (hw_config->fsync_master ==
2083a941e2faSKirill Marinushkin 				SND_SOC_TPLG_FSYNC_CM);
2084a941e2faSKirill Marinushkin 		if (bclk_master && fsync_master)
2085593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2086593d9e52SMengdong Lin 		else if (!bclk_master && fsync_master)
2087a941e2faSKirill Marinushkin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2088a941e2faSKirill Marinushkin 		else if (bclk_master && !fsync_master)
2089593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2090593d9e52SMengdong Lin 		else
2091593d9e52SMengdong Lin 			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2092593d9e52SMengdong Lin 	}
2093593d9e52SMengdong Lin }
2094593d9e52SMengdong Lin 
2095593d9e52SMengdong Lin /**
2096593d9e52SMengdong Lin  * link_new_ver - Create a new physical link config from the old
2097593d9e52SMengdong Lin  * version of source.
20988abab35fSCharles Keepax  * @tplg: topology context
2099593d9e52SMengdong Lin  * @src: old version of phyical link config as a source
2100593d9e52SMengdong Lin  * @link: latest version of physical link config created from the source
2101593d9e52SMengdong Lin  *
2102593d9e52SMengdong Lin  * Support from vesion 4. User need free the returned link config manually.
2103593d9e52SMengdong Lin  */
2104593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg,
2105593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config *src,
2106593d9e52SMengdong Lin 			struct snd_soc_tplg_link_config **link)
2107593d9e52SMengdong Lin {
2108593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *dest;
2109593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config_v4 *src_v4;
2110593d9e52SMengdong Lin 	int i;
2111593d9e52SMengdong Lin 
2112593d9e52SMengdong Lin 	*link = NULL;
2113593d9e52SMengdong Lin 
2114593d9e52SMengdong Lin 	if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2115593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2116593d9e52SMengdong Lin 		return -EINVAL;
2117593d9e52SMengdong Lin 	}
2118593d9e52SMengdong Lin 
2119593d9e52SMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2120593d9e52SMengdong Lin 
2121593d9e52SMengdong Lin 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2122593d9e52SMengdong Lin 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2123593d9e52SMengdong Lin 	if (!dest)
2124593d9e52SMengdong Lin 		return -ENOMEM;
2125593d9e52SMengdong Lin 
2126593d9e52SMengdong Lin 	dest->size = sizeof(*dest);
2127593d9e52SMengdong Lin 	dest->id = src_v4->id;
2128593d9e52SMengdong Lin 	dest->num_streams = src_v4->num_streams;
2129593d9e52SMengdong Lin 	for (i = 0; i < dest->num_streams; i++)
2130593d9e52SMengdong Lin 		memcpy(&dest->stream[i], &src_v4->stream[i],
2131593d9e52SMengdong Lin 		       sizeof(struct snd_soc_tplg_stream));
2132593d9e52SMengdong Lin 
2133593d9e52SMengdong Lin 	*link = dest;
2134593d9e52SMengdong Lin 	return 0;
2135593d9e52SMengdong Lin }
2136593d9e52SMengdong Lin 
2137593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */
2138593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg,
2139593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *cfg)
2140593d9e52SMengdong Lin {
2141593d9e52SMengdong Lin 	struct snd_soc_dai_link *link;
2142593d9e52SMengdong Lin 	const char *name, *stream_name;
2143dbab1cb8SMengdong Lin 	size_t len;
2144593d9e52SMengdong Lin 	int ret;
2145593d9e52SMengdong Lin 
2146dbab1cb8SMengdong Lin 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2147dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2148dbab1cb8SMengdong Lin 		return -EINVAL;
2149dbab1cb8SMengdong Lin 	else if (len)
2150dbab1cb8SMengdong Lin 		name = cfg->name;
2151dbab1cb8SMengdong Lin 	else
2152dbab1cb8SMengdong Lin 		name = NULL;
2153dbab1cb8SMengdong Lin 
2154dbab1cb8SMengdong Lin 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2155dbab1cb8SMengdong Lin 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2156dbab1cb8SMengdong Lin 		return -EINVAL;
2157dbab1cb8SMengdong Lin 	else if (len)
2158dbab1cb8SMengdong Lin 		stream_name = cfg->stream_name;
2159dbab1cb8SMengdong Lin 	else
2160dbab1cb8SMengdong Lin 		stream_name = NULL;
2161593d9e52SMengdong Lin 
2162593d9e52SMengdong Lin 	link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2163593d9e52SMengdong Lin 				     name, stream_name);
2164593d9e52SMengdong Lin 	if (!link) {
2165593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2166593d9e52SMengdong Lin 			name, cfg->id);
2167593d9e52SMengdong Lin 		return -EINVAL;
2168593d9e52SMengdong Lin 	}
2169593d9e52SMengdong Lin 
2170593d9e52SMengdong Lin 	/* hw format */
2171593d9e52SMengdong Lin 	if (cfg->num_hw_configs)
2172593d9e52SMengdong Lin 		set_link_hw_format(link, cfg);
2173593d9e52SMengdong Lin 
2174593d9e52SMengdong Lin 	/* flags */
2175593d9e52SMengdong Lin 	if (cfg->flag_mask)
2176593d9e52SMengdong Lin 		set_link_flags(link, cfg->flag_mask, cfg->flags);
2177593d9e52SMengdong Lin 
2178593d9e52SMengdong Lin 	/* pass control to component driver for optional further init */
2179c60b613aSLiam Girdwood 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2180593d9e52SMengdong Lin 	if (ret < 0) {
2181593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2182593d9e52SMengdong Lin 		return ret;
2183593d9e52SMengdong Lin 	}
2184593d9e52SMengdong Lin 
2185*adfebb51SBard liao 	/* for unloading it in snd_soc_tplg_component_remove */
2186*adfebb51SBard liao 	link->dobj.index = tplg->index;
2187*adfebb51SBard liao 	link->dobj.ops = tplg->ops;
2188*adfebb51SBard liao 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2189*adfebb51SBard liao 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2190*adfebb51SBard liao 
2191593d9e52SMengdong Lin 	return 0;
2192593d9e52SMengdong Lin }
2193593d9e52SMengdong Lin 
2194593d9e52SMengdong Lin 
2195593d9e52SMengdong Lin /* Load physical link config elements from the topology context */
2196593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2197593d9e52SMengdong Lin 	struct snd_soc_tplg_hdr *hdr)
2198593d9e52SMengdong Lin {
2199593d9e52SMengdong Lin 	struct snd_soc_tplg_link_config *link, *_link;
2200593d9e52SMengdong Lin 	int count = hdr->count;
2201593d9e52SMengdong Lin 	int i, ret;
2202593d9e52SMengdong Lin 	bool abi_match;
2203593d9e52SMengdong Lin 
2204593d9e52SMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_LINK) {
2205593d9e52SMengdong Lin 		tplg->pos += hdr->size + hdr->payload_size;
2206593d9e52SMengdong Lin 		return 0;
2207593d9e52SMengdong Lin 	};
2208593d9e52SMengdong Lin 
2209593d9e52SMengdong Lin 	/* check the element size and count */
2210593d9e52SMengdong Lin 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
2211593d9e52SMengdong Lin 	if (link->size > sizeof(struct snd_soc_tplg_link_config)
2212593d9e52SMengdong Lin 		|| link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2213593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2214593d9e52SMengdong Lin 			link->size);
2215593d9e52SMengdong Lin 		return -EINVAL;
2216593d9e52SMengdong Lin 	}
2217593d9e52SMengdong Lin 
2218593d9e52SMengdong Lin 	if (soc_tplg_check_elem_count(tplg,
2219593d9e52SMengdong Lin 		link->size, count,
2220593d9e52SMengdong Lin 		hdr->payload_size, "physical link config")) {
2221593d9e52SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2222593d9e52SMengdong Lin 			count);
2223593d9e52SMengdong Lin 		return -EINVAL;
2224593d9e52SMengdong Lin 	}
2225593d9e52SMengdong Lin 
2226593d9e52SMengdong Lin 	/* config physical DAI links */
2227593d9e52SMengdong Lin 	for (i = 0; i < count; i++) {
2228593d9e52SMengdong Lin 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
2229593d9e52SMengdong Lin 		if (link->size == sizeof(*link)) {
2230593d9e52SMengdong Lin 			abi_match = true;
2231593d9e52SMengdong Lin 			_link = link;
2232593d9e52SMengdong Lin 		} else {
2233593d9e52SMengdong Lin 			abi_match = false;
2234593d9e52SMengdong Lin 			ret = link_new_ver(tplg, link, &_link);
2235593d9e52SMengdong Lin 			if (ret < 0)
2236593d9e52SMengdong Lin 				return ret;
2237593d9e52SMengdong Lin 		}
2238593d9e52SMengdong Lin 
2239593d9e52SMengdong Lin 		ret = soc_tplg_link_config(tplg, _link);
2240593d9e52SMengdong Lin 		if (ret < 0)
2241593d9e52SMengdong Lin 			return ret;
2242593d9e52SMengdong Lin 
2243593d9e52SMengdong Lin 		/* offset by version-specific struct size and
2244593d9e52SMengdong Lin 		 * real priv data size
2245593d9e52SMengdong Lin 		 */
2246593d9e52SMengdong Lin 		tplg->pos += link->size + _link->priv.size;
2247593d9e52SMengdong Lin 
2248593d9e52SMengdong Lin 		if (!abi_match)
2249593d9e52SMengdong Lin 			kfree(_link); /* free the duplicated one */
2250593d9e52SMengdong Lin 	}
2251593d9e52SMengdong Lin 
2252593d9e52SMengdong Lin 	return 0;
2253593d9e52SMengdong Lin }
2254593d9e52SMengdong Lin 
2255593d9e52SMengdong Lin /**
22569aa3f034SMengdong Lin  * soc_tplg_dai_config - Find and configure an existing physical DAI.
22570038be9aSMengdong Lin  * @tplg: topology context
22589aa3f034SMengdong Lin  * @d: physical DAI configs.
22590038be9aSMengdong Lin  *
22609aa3f034SMengdong Lin  * The physical dai should already be registered by the platform driver.
22619aa3f034SMengdong Lin  * The platform driver should specify the DAI name and ID for matching.
22620038be9aSMengdong Lin  */
22639aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg,
22649aa3f034SMengdong Lin 			       struct snd_soc_tplg_dai *d)
22650038be9aSMengdong Lin {
22660038be9aSMengdong Lin 	struct snd_soc_dai_link_component dai_component = {0};
22670038be9aSMengdong Lin 	struct snd_soc_dai *dai;
22680038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
22690038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
22700038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
22710038be9aSMengdong Lin 	int ret;
22720038be9aSMengdong Lin 
22739aa3f034SMengdong Lin 	dai_component.dai_name = d->dai_name;
22740038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
22750038be9aSMengdong Lin 	if (!dai) {
22769aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
22779aa3f034SMengdong Lin 			d->dai_name);
22780038be9aSMengdong Lin 		return -EINVAL;
22790038be9aSMengdong Lin 	}
22800038be9aSMengdong Lin 
22819aa3f034SMengdong Lin 	if (d->dai_id != dai->id) {
22829aa3f034SMengdong Lin 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
22839aa3f034SMengdong Lin 			d->dai_name);
22840038be9aSMengdong Lin 		return -EINVAL;
22850038be9aSMengdong Lin 	}
22860038be9aSMengdong Lin 
22870038be9aSMengdong Lin 	dai_drv = dai->driver;
22880038be9aSMengdong Lin 	if (!dai_drv)
22890038be9aSMengdong Lin 		return -EINVAL;
22900038be9aSMengdong Lin 
22919aa3f034SMengdong Lin 	if (d->playback) {
22920038be9aSMengdong Lin 		stream = &dai_drv->playback;
22939aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
22940038be9aSMengdong Lin 		set_stream_info(stream, caps);
22950038be9aSMengdong Lin 	}
22960038be9aSMengdong Lin 
22979aa3f034SMengdong Lin 	if (d->capture) {
22980038be9aSMengdong Lin 		stream = &dai_drv->capture;
22999aa3f034SMengdong Lin 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
23000038be9aSMengdong Lin 		set_stream_info(stream, caps);
23010038be9aSMengdong Lin 	}
23020038be9aSMengdong Lin 
23039aa3f034SMengdong Lin 	if (d->flag_mask)
23049aa3f034SMengdong Lin 		set_dai_flags(dai_drv, d->flag_mask, d->flags);
23050038be9aSMengdong Lin 
23060038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
2307c60b613aSLiam Girdwood 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
23080038be9aSMengdong Lin 	if (ret < 0) {
23090038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
23100038be9aSMengdong Lin 		return ret;
23110038be9aSMengdong Lin 	}
23120038be9aSMengdong Lin 
23130038be9aSMengdong Lin 	return 0;
23140038be9aSMengdong Lin }
23150038be9aSMengdong Lin 
23169aa3f034SMengdong Lin /* load physical DAI elements */
23179aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
23180038be9aSMengdong Lin 				   struct snd_soc_tplg_hdr *hdr)
23190038be9aSMengdong Lin {
23209aa3f034SMengdong Lin 	struct snd_soc_tplg_dai *dai;
23210038be9aSMengdong Lin 	int count = hdr->count;
23220038be9aSMengdong Lin 	int i;
23230038be9aSMengdong Lin 
23240038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
23250038be9aSMengdong Lin 		return 0;
23260038be9aSMengdong Lin 
23270038be9aSMengdong Lin 	/* config the existing BE DAIs */
23280038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
23299aa3f034SMengdong Lin 		dai = (struct snd_soc_tplg_dai *)tplg->pos;
23309aa3f034SMengdong Lin 		if (dai->size != sizeof(*dai)) {
23319aa3f034SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
23320038be9aSMengdong Lin 			return -EINVAL;
23330038be9aSMengdong Lin 		}
23340038be9aSMengdong Lin 
23359aa3f034SMengdong Lin 		soc_tplg_dai_config(tplg, dai);
23369aa3f034SMengdong Lin 		tplg->pos += (sizeof(*dai) + dai->priv.size);
23370038be9aSMengdong Lin 	}
23380038be9aSMengdong Lin 
23390038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
23400038be9aSMengdong Lin 	return 0;
23410038be9aSMengdong Lin }
23420038be9aSMengdong Lin 
2343583958faSMengdong Lin /**
2344583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
2345583958faSMengdong Lin  * of source.
23468abab35fSCharles Keepax  * @tplg: topology context
2347583958faSMengdong Lin  * @src: old version of manifest as a source
2348583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
2349583958faSMengdong Lin  *
2350583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
2351583958faSMengdong Lin  */
2352583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
2353583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
2354583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
2355583958faSMengdong Lin {
2356583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
2357583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
2358583958faSMengdong Lin 
2359583958faSMengdong Lin 	*manifest = NULL;
2360583958faSMengdong Lin 
2361583958faSMengdong Lin 	if (src->size != sizeof(*src_v4)) {
2362ac9391daSGuenter Roeck 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2363ac9391daSGuenter Roeck 			 src->size);
2364ac9391daSGuenter Roeck 		if (src->size)
2365583958faSMengdong Lin 			return -EINVAL;
2366ac9391daSGuenter Roeck 		src->size = sizeof(*src_v4);
2367583958faSMengdong Lin 	}
2368583958faSMengdong Lin 
2369583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2370583958faSMengdong Lin 
2371583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2372583958faSMengdong Lin 	dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2373583958faSMengdong Lin 	if (!dest)
2374583958faSMengdong Lin 		return -ENOMEM;
2375583958faSMengdong Lin 
2376583958faSMengdong Lin 	dest->size = sizeof(*dest);	/* size of latest abi version */
2377583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
2378583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
2379583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
2380583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
2381583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
2382583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
2383583958faSMengdong Lin 	if (dest->priv.size)
2384583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
2385583958faSMengdong Lin 		       src_v4->priv.size);
2386583958faSMengdong Lin 
2387583958faSMengdong Lin 	*manifest = dest;
2388583958faSMengdong Lin 	return 0;
2389583958faSMengdong Lin }
23900038be9aSMengdong Lin 
23918a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
23928a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
23938a978234SLiam Girdwood {
2394583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2395583958faSMengdong Lin 	bool abi_match;
2396583958faSMengdong Lin 	int err;
23978a978234SLiam Girdwood 
23988a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
23998a978234SLiam Girdwood 		return 0;
24008a978234SLiam Girdwood 
24018a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2402583958faSMengdong Lin 
2403583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
2404583958faSMengdong Lin 	if (manifest->size == sizeof(*manifest)) {
2405583958faSMengdong Lin 		abi_match = true;
2406583958faSMengdong Lin 		_manifest = manifest;
2407583958faSMengdong Lin 	} else {
2408583958faSMengdong Lin 		abi_match = false;
2409583958faSMengdong Lin 		err = manifest_new_ver(tplg, manifest, &_manifest);
2410583958faSMengdong Lin 		if (err < 0)
2411583958faSMengdong Lin 			return err;
241206eb49f7SMengdong Lin 	}
241306eb49f7SMengdong Lin 
2414583958faSMengdong Lin 	/* pass control to component driver for optional further init */
24158a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
2416c60b613aSLiam Girdwood 		return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
24178a978234SLiam Girdwood 
2418583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
2419583958faSMengdong Lin 		kfree(_manifest);
2420583958faSMengdong Lin 
24218a978234SLiam Girdwood 	return 0;
24228a978234SLiam Girdwood }
24238a978234SLiam Girdwood 
24248a978234SLiam Girdwood /* validate header magic, size and type */
24258a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
24268a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24278a978234SLiam Girdwood {
24288a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
24298a978234SLiam Girdwood 		return 0;
24308a978234SLiam Girdwood 
243106eb49f7SMengdong Lin 	if (hdr->size != sizeof(*hdr)) {
243206eb49f7SMengdong Lin 		dev_err(tplg->dev,
243306eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
243406eb49f7SMengdong Lin 			hdr->type, soc_tplg_get_hdr_offset(tplg),
243506eb49f7SMengdong Lin 			tplg->fw->size);
243606eb49f7SMengdong Lin 		return -EINVAL;
243706eb49f7SMengdong Lin 	}
243806eb49f7SMengdong Lin 
24398a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
24408a978234SLiam Girdwood 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
24418a978234SLiam Girdwood 		dev_err(tplg->dev,
24428a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
24438a978234SLiam Girdwood 			tplg->pass, hdr->magic,
24448a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
24458a978234SLiam Girdwood 		return -EINVAL;
24468a978234SLiam Girdwood 	}
24478a978234SLiam Girdwood 
24488a978234SLiam Girdwood 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
24498a978234SLiam Girdwood 		dev_err(tplg->dev,
24508a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
24518a978234SLiam Girdwood 			tplg->pass, hdr->magic,
24528a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
24538a978234SLiam Girdwood 		return -EINVAL;
24548a978234SLiam Girdwood 	}
24558a978234SLiam Girdwood 
2456288b8da7SMengdong Lin 	/* Support ABI from version 4 */
2457288b8da7SMengdong Lin 	if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2458288b8da7SMengdong Lin 		|| hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
24598a978234SLiam Girdwood 		dev_err(tplg->dev,
24608a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
24618a978234SLiam Girdwood 			tplg->pass, hdr->abi,
24628a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
24638a978234SLiam Girdwood 			tplg->fw->size);
24648a978234SLiam Girdwood 		return -EINVAL;
24658a978234SLiam Girdwood 	}
24668a978234SLiam Girdwood 
24678a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
24688a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
24698a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
24708a978234SLiam Girdwood 		return -EINVAL;
24718a978234SLiam Girdwood 	}
24728a978234SLiam Girdwood 
24738a978234SLiam Girdwood 	if (tplg->pass == hdr->type)
24748a978234SLiam Girdwood 		dev_dbg(tplg->dev,
24758a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
24768a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
24778a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
24788a978234SLiam Girdwood 
24798a978234SLiam Girdwood 	return 1;
24808a978234SLiam Girdwood }
24818a978234SLiam Girdwood 
24828a978234SLiam Girdwood /* check header type and call appropriate handler */
24838a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
24848a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
24858a978234SLiam Girdwood {
24868a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
24878a978234SLiam Girdwood 
24888a978234SLiam Girdwood 	/* check for matching ID */
24898a978234SLiam Girdwood 	if (hdr->index != tplg->req_index &&
2490bb97142bSLiam Girdwood 		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
24918a978234SLiam Girdwood 		return 0;
24928a978234SLiam Girdwood 
24938a978234SLiam Girdwood 	tplg->index = hdr->index;
24948a978234SLiam Girdwood 
24958a978234SLiam Girdwood 	switch (hdr->type) {
24968a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
24978a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
24988a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
24998a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
25008a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
25018a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
25028a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
25038a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
25048a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
250564527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
25063fbf7935SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI:
25079aa3f034SMengdong Lin 		return soc_tplg_dai_elems_load(tplg, hdr);
2508593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2509593d9e52SMengdong Lin 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2510593d9e52SMengdong Lin 		/* physical link configurations */
2511593d9e52SMengdong Lin 		return soc_tplg_link_elems_load(tplg, hdr);
25128a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
25138a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
25148a978234SLiam Girdwood 	default:
25158a978234SLiam Girdwood 		/* bespoke vendor data object */
25168a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
25178a978234SLiam Girdwood 	}
25188a978234SLiam Girdwood 
25198a978234SLiam Girdwood 	return 0;
25208a978234SLiam Girdwood }
25218a978234SLiam Girdwood 
25228a978234SLiam Girdwood /* process the topology file headers */
25238a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
25248a978234SLiam Girdwood {
25258a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
25268a978234SLiam Girdwood 	int ret;
25278a978234SLiam Girdwood 
25288a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
25298a978234SLiam Girdwood 
25308a978234SLiam Girdwood 	/* process the header types from start to end */
25318a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
25328a978234SLiam Girdwood 
25338a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
25348a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25358a978234SLiam Girdwood 
25368a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
25378a978234SLiam Girdwood 
25388a978234SLiam Girdwood 			/* make sure header is valid before loading */
25398a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
25408a978234SLiam Girdwood 			if (ret < 0)
25418a978234SLiam Girdwood 				return ret;
25428a978234SLiam Girdwood 			else if (ret == 0)
25438a978234SLiam Girdwood 				break;
25448a978234SLiam Girdwood 
25458a978234SLiam Girdwood 			/* load the header object */
25468a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
25478a978234SLiam Girdwood 			if (ret < 0)
25488a978234SLiam Girdwood 				return ret;
25498a978234SLiam Girdwood 
25508a978234SLiam Girdwood 			/* goto next header */
25518a978234SLiam Girdwood 			tplg->hdr_pos += hdr->payload_size +
25528a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
25538a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
25548a978234SLiam Girdwood 		}
25558a978234SLiam Girdwood 
25568a978234SLiam Girdwood 		/* next data type pass */
25578a978234SLiam Girdwood 		tplg->pass++;
25588a978234SLiam Girdwood 	}
25598a978234SLiam Girdwood 
25608a978234SLiam Girdwood 	/* signal DAPM we are complete */
25618a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
25628a978234SLiam Girdwood 	if (ret < 0)
25638a978234SLiam Girdwood 		dev_err(tplg->dev,
25648a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
25658a978234SLiam Girdwood 
25668a978234SLiam Girdwood 	return ret;
25678a978234SLiam Girdwood }
25688a978234SLiam Girdwood 
25698a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
25708a978234SLiam Girdwood {
25718a978234SLiam Girdwood 	int ret;
25728a978234SLiam Girdwood 
25738a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
25748a978234SLiam Girdwood 	if (ret == 0)
25758a978234SLiam Girdwood 		soc_tplg_complete(tplg);
25768a978234SLiam Girdwood 
25778a978234SLiam Girdwood 	return ret;
25788a978234SLiam Girdwood }
25798a978234SLiam Girdwood 
25808a978234SLiam Girdwood /* load audio component topology from "firmware" file */
25818a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
25828a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
25838a978234SLiam Girdwood {
25848a978234SLiam Girdwood 	struct soc_tplg tplg;
25858a978234SLiam Girdwood 
25868a978234SLiam Girdwood 	/* setup parsing context */
25878a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
25888a978234SLiam Girdwood 	tplg.fw = fw;
25898a978234SLiam Girdwood 	tplg.dev = comp->dev;
25908a978234SLiam Girdwood 	tplg.comp = comp;
25918a978234SLiam Girdwood 	tplg.ops = ops;
25928a978234SLiam Girdwood 	tplg.req_index = id;
25938a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
25948a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
25951a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
25961a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
25978a978234SLiam Girdwood 
25988a978234SLiam Girdwood 	return soc_tplg_load(&tplg);
25998a978234SLiam Girdwood }
26008a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
26018a978234SLiam Girdwood 
26028a978234SLiam Girdwood /* remove this dynamic widget */
26038a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
26048a978234SLiam Girdwood {
26058a978234SLiam Girdwood 	/* make sure we are a widget */
26068a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
26078a978234SLiam Girdwood 		return;
26088a978234SLiam Girdwood 
26098a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
26108a978234SLiam Girdwood }
26118a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
26128a978234SLiam Girdwood 
26138a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
26148a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
26158a978234SLiam Girdwood 	u32 index)
26168a978234SLiam Girdwood {
26178a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
26188a978234SLiam Girdwood 
26198a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
26208a978234SLiam Girdwood 
26218a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
26228a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
26238a978234SLiam Girdwood 			continue;
26248a978234SLiam Girdwood 
26258a978234SLiam Girdwood 		/* match ID */
26268a978234SLiam Girdwood 		if (w->dobj.index != index &&
26278a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
26288a978234SLiam Girdwood 			continue;
26298a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
26308a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2631b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
26328a978234SLiam Girdwood 	}
2633fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
26348a978234SLiam Girdwood }
26358a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
26368a978234SLiam Girdwood 
26378a978234SLiam Girdwood /* remove dynamic controls from the component driver */
26388a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
26398a978234SLiam Girdwood {
26408a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
26418a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
26428a978234SLiam Girdwood 
26438a978234SLiam Girdwood 	/* process the header types from end to start */
26448a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
26458a978234SLiam Girdwood 
26468a978234SLiam Girdwood 		/* remove mixer controls */
26478a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
26488a978234SLiam Girdwood 			list) {
26498a978234SLiam Girdwood 
26508a978234SLiam Girdwood 			/* match index */
26518a978234SLiam Girdwood 			if (dobj->index != index &&
2652feb12f0cSYan Wang 				index != SND_SOC_TPLG_INDEX_ALL)
26538a978234SLiam Girdwood 				continue;
26548a978234SLiam Girdwood 
26558a978234SLiam Girdwood 			switch (dobj->type) {
26568a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
26578a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
26588a978234SLiam Girdwood 				break;
26598a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
26608a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
26618a978234SLiam Girdwood 				break;
26628a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
26638a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
26648a978234SLiam Girdwood 				break;
26657df04ea7SRanjani Sridharan 			case SND_SOC_DOBJ_GRAPH:
26667df04ea7SRanjani Sridharan 				remove_route(comp, dobj, pass);
26677df04ea7SRanjani Sridharan 				break;
26688a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
26698a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
26708a978234SLiam Girdwood 				break;
26718a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
267264527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
26738a978234SLiam Girdwood 				break;
2674acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2675acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2676acfc7d46SMengdong Lin 				break;
2677*adfebb51SBard liao 			case SND_SOC_DOBJ_BACKEND_LINK:
2678*adfebb51SBard liao 				/*
2679*adfebb51SBard liao 				 * call link_unload ops if extra
2680*adfebb51SBard liao 				 * deinitialization is needed.
2681*adfebb51SBard liao 				 */
2682*adfebb51SBard liao 				remove_backend_link(comp, dobj, pass);
2683*adfebb51SBard liao 				break;
26848a978234SLiam Girdwood 			default:
26858a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
26868a978234SLiam Girdwood 					dobj->type);
26878a978234SLiam Girdwood 				break;
26888a978234SLiam Girdwood 			}
26898a978234SLiam Girdwood 		}
26908a978234SLiam Girdwood 		pass--;
26918a978234SLiam Girdwood 	}
26928a978234SLiam Girdwood 
26938a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
26948a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
26958a978234SLiam Girdwood }
26968a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2697