xref: /openbmc/linux/sound/soc/soc-topology.c (revision 0038be9a)
18a978234SLiam Girdwood /*
28a978234SLiam Girdwood  * soc-topology.c  --  ALSA SoC Topology
38a978234SLiam Girdwood  *
48a978234SLiam Girdwood  * Copyright (C) 2012 Texas Instruments Inc.
58a978234SLiam Girdwood  * Copyright (C) 2015 Intel Corporation.
68a978234SLiam Girdwood  *
78a978234SLiam Girdwood  * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
88a978234SLiam Girdwood  *		K, Mythri P <mythri.p.k@intel.com>
98a978234SLiam Girdwood  *		Prusty, Subhransu S <subhransu.s.prusty@intel.com>
108a978234SLiam Girdwood  *		B, Jayachandran <jayachandran.b@intel.com>
118a978234SLiam Girdwood  *		Abdullah, Omair M <omair.m.abdullah@intel.com>
128a978234SLiam Girdwood  *		Jin, Yao <yao.jin@intel.com>
138a978234SLiam Girdwood  *		Lin, Mengdong <mengdong.lin@intel.com>
148a978234SLiam Girdwood  *
158a978234SLiam Girdwood  *  This program is free software; you can redistribute  it and/or modify it
168a978234SLiam Girdwood  *  under  the terms of  the GNU General  Public License as published by the
178a978234SLiam Girdwood  *  Free Software Foundation;  either version 2 of the  License, or (at your
188a978234SLiam Girdwood  *  option) any later version.
198a978234SLiam Girdwood  *
208a978234SLiam Girdwood  *  Add support to read audio firmware topology alongside firmware text. The
218a978234SLiam Girdwood  *  topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
228a978234SLiam Girdwood  *  equalizers, firmware, coefficients etc.
238a978234SLiam Girdwood  *
248a978234SLiam Girdwood  *  This file only manages the core ALSA and ASoC components, all other bespoke
258a978234SLiam Girdwood  *  firmware topology data is passed to component drivers for bespoke handling.
268a978234SLiam Girdwood  */
278a978234SLiam Girdwood 
288a978234SLiam Girdwood #include <linux/kernel.h>
298a978234SLiam Girdwood #include <linux/export.h>
308a978234SLiam Girdwood #include <linux/list.h>
318a978234SLiam Girdwood #include <linux/firmware.h>
328a978234SLiam Girdwood #include <linux/slab.h>
338a978234SLiam Girdwood #include <sound/soc.h>
348a978234SLiam Girdwood #include <sound/soc-dapm.h>
358a978234SLiam Girdwood #include <sound/soc-topology.h>
3628a87eebSMengdong Lin #include <sound/tlv.h>
378a978234SLiam Girdwood 
388a978234SLiam Girdwood /*
398a978234SLiam Girdwood  * We make several passes over the data (since it wont necessarily be ordered)
408a978234SLiam Girdwood  * and process objects in the following order. This guarantees the component
418a978234SLiam Girdwood  * drivers will be ready with any vendor data before the mixers and DAPM objects
428a978234SLiam Girdwood  * are loaded (that may make use of the vendor data).
438a978234SLiam Girdwood  */
448a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST		0
458a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR		1
468a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER		2
478a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET		3
481a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI		4
491a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH		5
501a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PINS		6
51*0038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI		7
528a978234SLiam Girdwood 
538a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
54*0038be9aSMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_BE_DAI
558a978234SLiam Girdwood 
568a978234SLiam Girdwood struct soc_tplg {
578a978234SLiam Girdwood 	const struct firmware *fw;
588a978234SLiam Girdwood 
598a978234SLiam Girdwood 	/* runtime FW parsing */
608a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
618a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
628a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
638a978234SLiam Girdwood 
648a978234SLiam Girdwood 	/* component caller */
658a978234SLiam Girdwood 	struct device *dev;
668a978234SLiam Girdwood 	struct snd_soc_component *comp;
678a978234SLiam Girdwood 	u32 index;	/* current block index */
688a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
698a978234SLiam Girdwood 
7088a17d8fSMengdong Lin 	/* vendor specific kcontrol operations */
718a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
728a978234SLiam Girdwood 	int io_ops_count;
738a978234SLiam Girdwood 
741a3232d2SMengdong Lin 	/* vendor specific bytes ext handlers, for TLV bytes controls */
751a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
761a3232d2SMengdong Lin 	int bytes_ext_ops_count;
771a3232d2SMengdong Lin 
788a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
798a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
808a978234SLiam Girdwood };
818a978234SLiam Girdwood 
828a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg);
838a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
848a978234SLiam Girdwood struct snd_soc_dapm_widget *
858a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
868a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
878a978234SLiam Girdwood struct snd_soc_dapm_widget *
888a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
898a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
908a978234SLiam Girdwood 
918a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
928a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
938a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
948a978234SLiam Girdwood {
958a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
968a978234SLiam Girdwood 
978a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
988a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
998a978234SLiam Girdwood 			elem_type);
1008a978234SLiam Girdwood 		return -EINVAL;
1018a978234SLiam Girdwood 	}
1028a978234SLiam Girdwood 
1038a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
1048a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1058a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1068a978234SLiam Girdwood 		dev_err(tplg->dev,
1078a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1088a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1098a978234SLiam Girdwood 		return -EINVAL;
1108a978234SLiam Girdwood 	}
1118a978234SLiam Girdwood 
1128a978234SLiam Girdwood 	return 0;
1138a978234SLiam Girdwood }
1148a978234SLiam Girdwood 
1158a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1168a978234SLiam Girdwood {
1178a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1188a978234SLiam Girdwood 
1198a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1208a978234SLiam Girdwood 		return 1;
1218a978234SLiam Girdwood 	return 0;
1228a978234SLiam Girdwood }
1238a978234SLiam Girdwood 
1248a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1258a978234SLiam Girdwood {
1268a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1278a978234SLiam Girdwood }
1288a978234SLiam Girdwood 
1298a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1308a978234SLiam Girdwood {
1318a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1328a978234SLiam Girdwood }
1338a978234SLiam Girdwood 
1348a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1358a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1368a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1378a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1388a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1398a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1408a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1418a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1428a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1438a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1448a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1458a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1468a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1478a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1488a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1498a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1508a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1518a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1528a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1532c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1548a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1558a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1568a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1578a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1588a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1598a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1608a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1618a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1628a978234SLiam Girdwood };
1638a978234SLiam Girdwood 
1648a978234SLiam Girdwood struct soc_tplg_map {
1658a978234SLiam Girdwood 	int uid;
1668a978234SLiam Girdwood 	int kid;
1678a978234SLiam Girdwood };
1688a978234SLiam Girdwood 
1698a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1708a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1818a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1828a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1838a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1848a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1858a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1868a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1878a978234SLiam Girdwood };
1888a978234SLiam Girdwood 
1898a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1908a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1918a978234SLiam Girdwood {
1928a978234SLiam Girdwood 	int i;
1938a978234SLiam Girdwood 
1948a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
1958a978234SLiam Girdwood 		if (chan[i].id == map)
1968a978234SLiam Girdwood 			return chan[i].reg;
1978a978234SLiam Girdwood 	}
1988a978234SLiam Girdwood 
1998a978234SLiam Girdwood 	return -EINVAL;
2008a978234SLiam Girdwood }
2018a978234SLiam Girdwood 
2028a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2038a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2048a978234SLiam Girdwood {
2058a978234SLiam Girdwood 	int i;
2068a978234SLiam Girdwood 
2078a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2088a978234SLiam Girdwood 		if (chan[i].id == map)
2098a978234SLiam Girdwood 			return chan[i].shift;
2108a978234SLiam Girdwood 	}
2118a978234SLiam Girdwood 
2128a978234SLiam Girdwood 	return -EINVAL;
2138a978234SLiam Girdwood }
2148a978234SLiam Girdwood 
2158a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2168a978234SLiam Girdwood {
2178a978234SLiam Girdwood 	int i;
2188a978234SLiam Girdwood 
2198a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2208a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2218a978234SLiam Girdwood 			return dapm_map[i].kid;
2228a978234SLiam Girdwood 	}
2238a978234SLiam Girdwood 
2248a978234SLiam Girdwood 	return -EINVAL;
2258a978234SLiam Girdwood }
2268a978234SLiam Girdwood 
2278a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2288a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2298a978234SLiam Girdwood {
2308a978234SLiam Girdwood 	dev_err(tplg->dev,
2318a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2328a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2338a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2348a978234SLiam Girdwood }
2358a978234SLiam Girdwood 
2368a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2378a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2388a978234SLiam Girdwood {
2398a978234SLiam Girdwood 	dev_err(tplg->dev,
2408a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2418a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2428a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2438a978234SLiam Girdwood }
2448a978234SLiam Girdwood 
2458a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2468a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2478a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2488a978234SLiam Girdwood {
2498a978234SLiam Girdwood 	int ret = 0;
2508a978234SLiam Girdwood 
2518a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
2528a978234SLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, hdr);
2538a978234SLiam Girdwood 	else {
2548a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2558a978234SLiam Girdwood 			hdr->vendor_type);
2568a978234SLiam Girdwood 		return -EINVAL;
2578a978234SLiam Girdwood 	}
2588a978234SLiam Girdwood 
2598a978234SLiam Girdwood 	if (ret < 0)
2608a978234SLiam Girdwood 		dev_err(tplg->dev,
2618a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2628a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2638a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2648a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2658a978234SLiam Girdwood 	return ret;
2668a978234SLiam Girdwood }
2678a978234SLiam Girdwood 
2688a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2698a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2708a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2718a978234SLiam Girdwood {
2728a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2738a978234SLiam Girdwood 		return 0;
2748a978234SLiam Girdwood 
2758a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2768a978234SLiam Girdwood }
2778a978234SLiam Girdwood 
2788a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2798a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2808a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2818a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2828a978234SLiam Girdwood {
2838a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
2848a978234SLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, w, tplg_w);
2858a978234SLiam Girdwood 
2868a978234SLiam Girdwood 	return 0;
2878a978234SLiam Girdwood }
2888a978234SLiam Girdwood 
28964527e8aSMengdong Lin /* pass DAI configurations to component driver for extra intialization */
29064527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
29164527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
2928a978234SLiam Girdwood {
29364527e8aSMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
29464527e8aSMengdong Lin 		return tplg->ops->dai_load(tplg->comp, dai_drv);
2958a978234SLiam Girdwood 
2968a978234SLiam Girdwood 	return 0;
2978a978234SLiam Girdwood }
2988a978234SLiam Girdwood 
299acfc7d46SMengdong Lin /* pass link configurations to component driver for extra intialization */
300acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
301acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link)
302acfc7d46SMengdong Lin {
303acfc7d46SMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
304acfc7d46SMengdong Lin 		return tplg->ops->link_load(tplg->comp, link);
305acfc7d46SMengdong Lin 
306acfc7d46SMengdong Lin 	return 0;
307acfc7d46SMengdong Lin }
308acfc7d46SMengdong Lin 
3098a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3108a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3118a978234SLiam Girdwood {
3128a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3138a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3148a978234SLiam Girdwood }
3158a978234SLiam Girdwood 
3168a978234SLiam Girdwood /* add a dynamic kcontrol */
3178a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3188a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3198a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3208a978234SLiam Girdwood {
3218a978234SLiam Girdwood 	int err;
3228a978234SLiam Girdwood 
3238a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3248a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3258a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3268a978234SLiam Girdwood 		control_new->name);
3278a978234SLiam Girdwood 		return -ENOMEM;
3288a978234SLiam Girdwood 	}
3298a978234SLiam Girdwood 
3308a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3318a978234SLiam Girdwood 	if (err < 0) {
3328a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3338a978234SLiam Girdwood 			control_new->name, err);
3348a978234SLiam Girdwood 		return err;
3358a978234SLiam Girdwood 	}
3368a978234SLiam Girdwood 
3378a978234SLiam Girdwood 	return 0;
3388a978234SLiam Girdwood }
3398a978234SLiam Girdwood 
3408a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3418a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3428a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3438a978234SLiam Girdwood {
3448a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3458a978234SLiam Girdwood 
3468a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3478a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3488a978234SLiam Girdwood }
3498a978234SLiam Girdwood 
3508a978234SLiam Girdwood /* remove a mixer kcontrol */
3518a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3528a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3538a978234SLiam Girdwood {
3548a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3558a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3568a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3578a978234SLiam Girdwood 	const unsigned int *p = NULL;
3588a978234SLiam Girdwood 
3598a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3608a978234SLiam Girdwood 		return;
3618a978234SLiam Girdwood 
3628a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3638a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3648a978234SLiam Girdwood 
3658a978234SLiam Girdwood 	if (sm->dobj.control.kcontrol->tlv.p)
3668a978234SLiam Girdwood 		p = sm->dobj.control.kcontrol->tlv.p;
3678a978234SLiam Girdwood 	snd_ctl_remove(card, sm->dobj.control.kcontrol);
3688a978234SLiam Girdwood 	list_del(&sm->dobj.list);
3698a978234SLiam Girdwood 	kfree(sm);
3708a978234SLiam Girdwood 	kfree(p);
3718a978234SLiam Girdwood }
3728a978234SLiam Girdwood 
3738a978234SLiam Girdwood /* remove an enum kcontrol */
3748a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3758a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3768a978234SLiam Girdwood {
3778a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3788a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
3798a978234SLiam Girdwood 	int i;
3808a978234SLiam Girdwood 
3818a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3828a978234SLiam Girdwood 		return;
3838a978234SLiam Girdwood 
3848a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3858a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3868a978234SLiam Girdwood 
3878a978234SLiam Girdwood 	snd_ctl_remove(card, se->dobj.control.kcontrol);
3888a978234SLiam Girdwood 	list_del(&se->dobj.list);
3898a978234SLiam Girdwood 
3908a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
3918a978234SLiam Girdwood 	for (i = 0; i < se->items; i++)
3928a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
3938a978234SLiam Girdwood 	kfree(se);
3948a978234SLiam Girdwood }
3958a978234SLiam Girdwood 
3968a978234SLiam Girdwood /* remove a byte kcontrol */
3978a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
3988a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3998a978234SLiam Girdwood {
4008a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4018a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4028a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4038a978234SLiam Girdwood 
4048a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4058a978234SLiam Girdwood 		return;
4068a978234SLiam Girdwood 
4078a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4088a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4098a978234SLiam Girdwood 
4108a978234SLiam Girdwood 	snd_ctl_remove(card, sb->dobj.control.kcontrol);
4118a978234SLiam Girdwood 	list_del(&sb->dobj.list);
4128a978234SLiam Girdwood 	kfree(sb);
4138a978234SLiam Girdwood }
4148a978234SLiam Girdwood 
4158a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4168a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4178a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4188a978234SLiam Girdwood {
4198a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4208a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4218a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4228a978234SLiam Girdwood 	int i;
4238a978234SLiam Girdwood 
4248a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4258a978234SLiam Girdwood 		return;
4268a978234SLiam Girdwood 
4278a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4288a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4298a978234SLiam Girdwood 
4308a978234SLiam Girdwood 	/*
4318a978234SLiam Girdwood 	 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
4328a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4338a978234SLiam Girdwood 	 */
4348a978234SLiam Girdwood 	if (dobj->widget.kcontrol_enum) {
4358a978234SLiam Girdwood 		/* enumerated widget mixer */
4368a978234SLiam Girdwood 		struct soc_enum *se =
4378a978234SLiam Girdwood 			(struct soc_enum *)w->kcontrols[0]->private_value;
4388a978234SLiam Girdwood 
4398a978234SLiam Girdwood 		snd_ctl_remove(card, w->kcontrols[0]);
4408a978234SLiam Girdwood 
4418a978234SLiam Girdwood 		kfree(se->dobj.control.dvalues);
4428a978234SLiam Girdwood 		for (i = 0; i < se->items; i++)
4438a978234SLiam Girdwood 			kfree(se->dobj.control.dtexts[i]);
4448a978234SLiam Girdwood 
4458a978234SLiam Girdwood 		kfree(se);
4468a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4478a978234SLiam Girdwood 	} else {
4488a978234SLiam Girdwood 		/* non enumerated widget mixer */
4498a978234SLiam Girdwood 		for (i = 0; i < w->num_kcontrols; i++) {
4508a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4518a978234SLiam Girdwood 			struct soc_mixer_control *sm =
4528a978234SLiam Girdwood 			(struct soc_mixer_control *) kcontrol->private_value;
4538a978234SLiam Girdwood 
4548a978234SLiam Girdwood 			kfree(w->kcontrols[i]->tlv.p);
4558a978234SLiam Girdwood 
4568a978234SLiam Girdwood 			snd_ctl_remove(card, w->kcontrols[i]);
4578a978234SLiam Girdwood 			kfree(sm);
4588a978234SLiam Girdwood 		}
4598a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4608a978234SLiam Girdwood 	}
4618a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
4628a978234SLiam Girdwood }
4638a978234SLiam Girdwood 
46464527e8aSMengdong Lin /* remove DAI configurations */
46564527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
4668a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4678a978234SLiam Girdwood {
46864527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
46964527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
47064527e8aSMengdong Lin 
4718a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
4728a978234SLiam Girdwood 		return;
4738a978234SLiam Girdwood 
47464527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
47564527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
4768a978234SLiam Girdwood 
4778a978234SLiam Girdwood 	list_del(&dobj->list);
47864527e8aSMengdong Lin 	kfree(dai_drv);
4798a978234SLiam Girdwood }
4808a978234SLiam Girdwood 
481acfc7d46SMengdong Lin /* remove link configurations */
482acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
483acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
484acfc7d46SMengdong Lin {
485acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
486acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
487acfc7d46SMengdong Lin 
488acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
489acfc7d46SMengdong Lin 		return;
490acfc7d46SMengdong Lin 
491acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
492acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
493acfc7d46SMengdong Lin 
494acfc7d46SMengdong Lin 	list_del(&dobj->list);
495acfc7d46SMengdong Lin 	snd_soc_remove_dai_link(comp->card, link);
496acfc7d46SMengdong Lin 	kfree(link);
497acfc7d46SMengdong Lin }
498acfc7d46SMengdong Lin 
4998a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5008a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5018a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5022b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5038a978234SLiam Girdwood {
5042b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5051a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5062b5cdb91SMengdong Lin 	int num_ops, i;
5078a978234SLiam Girdwood 
5081a3232d2SMengdong Lin 	if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
5091a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5101a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5111a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5121a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5131a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
5141a3232d2SMengdong Lin 
5151a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
5161a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
5171a3232d2SMengdong Lin 
5181a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
5191a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
5201a3232d2SMengdong Lin 		 */
521f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
5221a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
5231a3232d2SMengdong Lin 
5241a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
5251a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
5261a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
5271a3232d2SMengdong Lin 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
5281a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
5291a3232d2SMengdong Lin 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
5301a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
5311a3232d2SMengdong Lin 		}
5321a3232d2SMengdong Lin 
5331a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
5341a3232d2SMengdong Lin 			return 0;
5351a3232d2SMengdong Lin 		else
5361a3232d2SMengdong Lin 			return -EINVAL;
5371a3232d2SMengdong Lin 	}
5381a3232d2SMengdong Lin 
53988a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
5402b5cdb91SMengdong Lin 	ops = tplg->io_ops;
5412b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
5428a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
5438a978234SLiam Girdwood 
5442b5cdb91SMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
5458a978234SLiam Girdwood 			k->put = ops[i].put;
5462b5cdb91SMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
5478a978234SLiam Girdwood 			k->get = ops[i].get;
5482b5cdb91SMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
5492b5cdb91SMengdong Lin 			k->info = ops[i].info;
5508a978234SLiam Girdwood 	}
5518a978234SLiam Girdwood 
55288a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
55388a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
55488a17d8fSMengdong Lin 		return 0;
55588a17d8fSMengdong Lin 
55688a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
5572b5cdb91SMengdong Lin 	ops = io_ops;
5582b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
55988a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
56088a17d8fSMengdong Lin 
56188a17d8fSMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
56288a17d8fSMengdong Lin 			k->put = ops[i].put;
56388a17d8fSMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
56488a17d8fSMengdong Lin 			k->get = ops[i].get;
56588a17d8fSMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
5668a978234SLiam Girdwood 			k->info = ops[i].info;
5678a978234SLiam Girdwood 	}
5688a978234SLiam Girdwood 
5698a978234SLiam Girdwood 	/* standard handlers found ? */
5708a978234SLiam Girdwood 	if (k->put && k->get && k->info)
5718a978234SLiam Girdwood 		return 0;
5728a978234SLiam Girdwood 
5738a978234SLiam Girdwood 	/* nothing to bind */
5748a978234SLiam Girdwood 	return -EINVAL;
5758a978234SLiam Girdwood }
5768a978234SLiam Girdwood 
5778a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
5788a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
5798a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
5808a978234SLiam Girdwood 		int num_events, u16 event_type)
5818a978234SLiam Girdwood {
5828a978234SLiam Girdwood 	int i;
5838a978234SLiam Girdwood 
5848a978234SLiam Girdwood 	w->event = NULL;
5858a978234SLiam Girdwood 
5868a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
5878a978234SLiam Girdwood 		if (event_type == events[i].type) {
5888a978234SLiam Girdwood 
5898a978234SLiam Girdwood 			/* found - so assign event */
5908a978234SLiam Girdwood 			w->event = events[i].event_handler;
5918a978234SLiam Girdwood 			return 0;
5928a978234SLiam Girdwood 		}
5938a978234SLiam Girdwood 	}
5948a978234SLiam Girdwood 
5958a978234SLiam Girdwood 	/* not found */
5968a978234SLiam Girdwood 	return -EINVAL;
5978a978234SLiam Girdwood }
5988a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
5998a978234SLiam Girdwood 
6008a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6018a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6028a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6038a978234SLiam Girdwood {
6048a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
6058a978234SLiam Girdwood 		return tplg->ops->control_load(tplg->comp, k, hdr);
6068a978234SLiam Girdwood 
6078a978234SLiam Girdwood 	return 0;
6088a978234SLiam Girdwood }
6098a978234SLiam Girdwood 
61028a87eebSMengdong Lin 
61128a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
61228a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6138a978234SLiam Girdwood {
61428a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
61528a87eebSMengdong Lin 	unsigned int *p;
6168a978234SLiam Girdwood 
61728a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
61828a87eebSMengdong Lin 	if (!p)
6198a978234SLiam Girdwood 		return -ENOMEM;
6208a978234SLiam Girdwood 
62128a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
62228a87eebSMengdong Lin 	p[1] = item_len;
62328a87eebSMengdong Lin 	p[2] = scale->min;
62428a87eebSMengdong Lin 	p[3] = (scale->step & TLV_DB_SCALE_MASK)
62528a87eebSMengdong Lin 			| (scale->mute ? TLV_DB_SCALE_MUTE : 0);
6268a978234SLiam Girdwood 
62728a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
62828a87eebSMengdong Lin 	return 0;
62928a87eebSMengdong Lin }
63028a87eebSMengdong Lin 
63128a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
63228a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
63328a87eebSMengdong Lin {
63428a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
63528a87eebSMengdong Lin 
63628a87eebSMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
63728a87eebSMengdong Lin 		return 0;
63828a87eebSMengdong Lin 
6391a3232d2SMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
64028a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
64128a87eebSMengdong Lin 		switch (tplg_tlv->type) {
64228a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
64328a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
64428a87eebSMengdong Lin 					&tplg_tlv->scale);
64528a87eebSMengdong Lin 
64628a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
64728a87eebSMengdong Lin 		default:
64828a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
64928a87eebSMengdong Lin 					tplg_tlv->type);
65028a87eebSMengdong Lin 			return -EINVAL;
65128a87eebSMengdong Lin 		}
65228a87eebSMengdong Lin 	}
6538a978234SLiam Girdwood 
6548a978234SLiam Girdwood 	return 0;
6558a978234SLiam Girdwood }
6568a978234SLiam Girdwood 
6578a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
6588a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
6598a978234SLiam Girdwood {
6608a978234SLiam Girdwood 	kfree(kc->tlv.p);
6618a978234SLiam Girdwood }
6628a978234SLiam Girdwood 
6638a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
6648a978234SLiam Girdwood 	size_t size)
6658a978234SLiam Girdwood {
6668a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
6678a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
6688a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
6698a978234SLiam Girdwood 	int i, err;
6708a978234SLiam Girdwood 
6718a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
6728a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
6738a978234SLiam Girdwood 			size, "mixer bytes")) {
6748a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
6758a978234SLiam Girdwood 			count);
6768a978234SLiam Girdwood 		return -EINVAL;
6778a978234SLiam Girdwood 	}
6788a978234SLiam Girdwood 
6798a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
6808a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
6818a978234SLiam Girdwood 
6828a978234SLiam Girdwood 		/* validate kcontrol */
6838a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
6848a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
6858a978234SLiam Girdwood 			return -EINVAL;
6868a978234SLiam Girdwood 
6878a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
6888a978234SLiam Girdwood 		if (sbe == NULL)
6898a978234SLiam Girdwood 			return -ENOMEM;
6908a978234SLiam Girdwood 
6918a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
6928a978234SLiam Girdwood 			be->priv.size);
6938a978234SLiam Girdwood 
6948a978234SLiam Girdwood 		dev_dbg(tplg->dev,
6958a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
6968a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
6978a978234SLiam Girdwood 
6988a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
6998a978234SLiam Girdwood 		kc.name = be->hdr.name;
7008a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7018a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7028a978234SLiam Girdwood 		kc.access = be->hdr.access;
7038a978234SLiam Girdwood 
7048a978234SLiam Girdwood 		sbe->max = be->max;
7058a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7068a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7078a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7088a978234SLiam Girdwood 
7098a978234SLiam Girdwood 		/* map io handlers */
7102b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7118a978234SLiam Girdwood 		if (err) {
7128a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
7138a978234SLiam Girdwood 			kfree(sbe);
7148a978234SLiam Girdwood 			continue;
7158a978234SLiam Girdwood 		}
7168a978234SLiam Girdwood 
7178a978234SLiam Girdwood 		/* pass control to driver for optional further init */
7188a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
7198a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
7208a978234SLiam Girdwood 		if (err < 0) {
7218a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
7228a978234SLiam Girdwood 				be->hdr.name);
7238a978234SLiam Girdwood 			kfree(sbe);
7248a978234SLiam Girdwood 			continue;
7258a978234SLiam Girdwood 		}
7268a978234SLiam Girdwood 
7278a978234SLiam Girdwood 		/* register control here */
7288a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
7298a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
7308a978234SLiam Girdwood 		if (err < 0) {
7318a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
7328a978234SLiam Girdwood 				be->hdr.name);
7338a978234SLiam Girdwood 			kfree(sbe);
7348a978234SLiam Girdwood 			continue;
7358a978234SLiam Girdwood 		}
7368a978234SLiam Girdwood 
7378a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
7388a978234SLiam Girdwood 	}
7398a978234SLiam Girdwood 	return 0;
7408a978234SLiam Girdwood 
7418a978234SLiam Girdwood }
7428a978234SLiam Girdwood 
7438a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
7448a978234SLiam Girdwood 	size_t size)
7458a978234SLiam Girdwood {
7468a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
7478a978234SLiam Girdwood 	struct soc_mixer_control *sm;
7488a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7498a978234SLiam Girdwood 	int i, err;
7508a978234SLiam Girdwood 
7518a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7528a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
7538a978234SLiam Girdwood 		count, size, "mixers")) {
7548a978234SLiam Girdwood 
7558a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
7568a978234SLiam Girdwood 			count);
7578a978234SLiam Girdwood 		return -EINVAL;
7588a978234SLiam Girdwood 	}
7598a978234SLiam Girdwood 
7608a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7618a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
7628a978234SLiam Girdwood 
7638a978234SLiam Girdwood 		/* validate kcontrol */
7648a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7658a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7668a978234SLiam Girdwood 			return -EINVAL;
7678a978234SLiam Girdwood 
7688a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
7698a978234SLiam Girdwood 		if (sm == NULL)
7708a978234SLiam Girdwood 			return -ENOMEM;
7718a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
7728a978234SLiam Girdwood 			mc->priv.size);
7738a978234SLiam Girdwood 
7748a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7758a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
7768a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
7778a978234SLiam Girdwood 
7788a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7798a978234SLiam Girdwood 		kc.name = mc->hdr.name;
7808a978234SLiam Girdwood 		kc.private_value = (long)sm;
7818a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7828a978234SLiam Girdwood 		kc.access = mc->hdr.access;
7838a978234SLiam Girdwood 
7848a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
7858a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
7868a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
7878a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
7888a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
7898a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
7908a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
7918a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
7928a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
7938a978234SLiam Girdwood 
7948a978234SLiam Girdwood 		sm->max = mc->max;
7958a978234SLiam Girdwood 		sm->min = mc->min;
7968a978234SLiam Girdwood 		sm->invert = mc->invert;
7978a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
7988a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
7998a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8008a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8018a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8028a978234SLiam Girdwood 
8038a978234SLiam Girdwood 		/* map io handlers */
8042b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8058a978234SLiam Girdwood 		if (err) {
8068a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8078a978234SLiam Girdwood 			kfree(sm);
8088a978234SLiam Girdwood 			continue;
8098a978234SLiam Girdwood 		}
8108a978234SLiam Girdwood 
8118a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8128a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8138a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
8148a978234SLiam Girdwood 		if (err < 0) {
8158a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8168a978234SLiam Girdwood 				mc->hdr.name);
8178a978234SLiam Girdwood 			kfree(sm);
8188a978234SLiam Girdwood 			continue;
8198a978234SLiam Girdwood 		}
8208a978234SLiam Girdwood 
8218a978234SLiam Girdwood 		/* create any TLV data */
82228a87eebSMengdong Lin 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
8238a978234SLiam Girdwood 
8248a978234SLiam Girdwood 		/* register control here */
8258a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8268a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
8278a978234SLiam Girdwood 		if (err < 0) {
8288a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8298a978234SLiam Girdwood 				mc->hdr.name);
8308a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
8318a978234SLiam Girdwood 			kfree(sm);
8328a978234SLiam Girdwood 			continue;
8338a978234SLiam Girdwood 		}
8348a978234SLiam Girdwood 
8358a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
8368a978234SLiam Girdwood 	}
8378a978234SLiam Girdwood 
8388a978234SLiam Girdwood 	return 0;
8398a978234SLiam Girdwood }
8408a978234SLiam Girdwood 
8418a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
8428a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
8438a978234SLiam Girdwood {
8448a978234SLiam Girdwood 	int i, ret;
8458a978234SLiam Girdwood 
8468a978234SLiam Girdwood 	se->dobj.control.dtexts =
8478a978234SLiam Girdwood 		kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
8488a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
8498a978234SLiam Girdwood 		return -ENOMEM;
8508a978234SLiam Girdwood 
8518a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
8528a978234SLiam Girdwood 
8538a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8548a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
8558a978234SLiam Girdwood 			ret = -EINVAL;
8568a978234SLiam Girdwood 			goto err;
8578a978234SLiam Girdwood 		}
8588a978234SLiam Girdwood 
8598a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
8608a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
8618a978234SLiam Girdwood 			ret = -ENOMEM;
8628a978234SLiam Girdwood 			goto err;
8638a978234SLiam Girdwood 		}
8648a978234SLiam Girdwood 	}
8658a978234SLiam Girdwood 
8668a978234SLiam Girdwood 	return 0;
8678a978234SLiam Girdwood 
8688a978234SLiam Girdwood err:
8698a978234SLiam Girdwood 	for (--i; i >= 0; i--)
8708a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
8718a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
8728a978234SLiam Girdwood 	return ret;
8738a978234SLiam Girdwood }
8748a978234SLiam Girdwood 
8758a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
8768a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
8778a978234SLiam Girdwood {
8788a978234SLiam Girdwood 	if (ec->items > sizeof(*ec->values))
8798a978234SLiam Girdwood 		return -EINVAL;
8808a978234SLiam Girdwood 
881376c0afeSAndrzej Hajda 	se->dobj.control.dvalues = kmemdup(ec->values,
882376c0afeSAndrzej Hajda 					   ec->items * sizeof(u32),
883376c0afeSAndrzej Hajda 					   GFP_KERNEL);
8848a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
8858a978234SLiam Girdwood 		return -ENOMEM;
8868a978234SLiam Girdwood 
8878a978234SLiam Girdwood 	return 0;
8888a978234SLiam Girdwood }
8898a978234SLiam Girdwood 
8908a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
8918a978234SLiam Girdwood 	size_t size)
8928a978234SLiam Girdwood {
8938a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
8948a978234SLiam Girdwood 	struct soc_enum *se;
8958a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8968a978234SLiam Girdwood 	int i, ret, err;
8978a978234SLiam Girdwood 
8988a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8998a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
9008a978234SLiam Girdwood 		count, size, "enums")) {
9018a978234SLiam Girdwood 
9028a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
9038a978234SLiam Girdwood 			count);
9048a978234SLiam Girdwood 		return -EINVAL;
9058a978234SLiam Girdwood 	}
9068a978234SLiam Girdwood 
9078a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
9088a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
9098a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
9108a978234SLiam Girdwood 			ec->priv.size);
9118a978234SLiam Girdwood 
9128a978234SLiam Girdwood 		/* validate kcontrol */
9138a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9148a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
9158a978234SLiam Girdwood 			return -EINVAL;
9168a978234SLiam Girdwood 
9178a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
9188a978234SLiam Girdwood 		if (se == NULL)
9198a978234SLiam Girdwood 			return -ENOMEM;
9208a978234SLiam Girdwood 
9218a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
9228a978234SLiam Girdwood 			ec->hdr.name, ec->items);
9238a978234SLiam Girdwood 
9248a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
9258a978234SLiam Girdwood 		kc.name = ec->hdr.name;
9268a978234SLiam Girdwood 		kc.private_value = (long)se;
9278a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
9288a978234SLiam Girdwood 		kc.access = ec->hdr.access;
9298a978234SLiam Girdwood 
9308a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
9318a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
9328a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
9338a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
9348a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
9358a978234SLiam Girdwood 
9368a978234SLiam Girdwood 		se->items = ec->items;
9378a978234SLiam Girdwood 		se->mask = ec->mask;
9388a978234SLiam Girdwood 		se->dobj.index = tplg->index;
9398a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
9408a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
9418a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
9428a978234SLiam Girdwood 
9438a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
9448a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9458a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9468a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
9478a978234SLiam Girdwood 			if (err < 0) {
9488a978234SLiam Girdwood 				dev_err(tplg->dev,
9498a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
9508a978234SLiam Girdwood 					ec->hdr.name);
9518a978234SLiam Girdwood 				kfree(se);
9528a978234SLiam Girdwood 				continue;
9538a978234SLiam Girdwood 			}
9548a978234SLiam Girdwood 			/* fall through and create texts */
9558a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9568a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9578a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9588a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
9598a978234SLiam Girdwood 			if (err < 0) {
9608a978234SLiam Girdwood 				dev_err(tplg->dev,
9618a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
9628a978234SLiam Girdwood 					ec->hdr.name);
9638a978234SLiam Girdwood 				kfree(se);
9648a978234SLiam Girdwood 				continue;
9658a978234SLiam Girdwood 			}
9668a978234SLiam Girdwood 			break;
9678a978234SLiam Girdwood 		default:
9688a978234SLiam Girdwood 			dev_err(tplg->dev,
9698a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
9708a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
9718a978234SLiam Girdwood 			kfree(se);
9728a978234SLiam Girdwood 			continue;
9738a978234SLiam Girdwood 		}
9748a978234SLiam Girdwood 
9758a978234SLiam Girdwood 		/* map io handlers */
9762b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
9778a978234SLiam Girdwood 		if (err) {
9788a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
9798a978234SLiam Girdwood 			kfree(se);
9808a978234SLiam Girdwood 			continue;
9818a978234SLiam Girdwood 		}
9828a978234SLiam Girdwood 
9838a978234SLiam Girdwood 		/* pass control to driver for optional further init */
9848a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
9858a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
9868a978234SLiam Girdwood 		if (err < 0) {
9878a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
9888a978234SLiam Girdwood 				ec->hdr.name);
9898a978234SLiam Girdwood 			kfree(se);
9908a978234SLiam Girdwood 			continue;
9918a978234SLiam Girdwood 		}
9928a978234SLiam Girdwood 
9938a978234SLiam Girdwood 		/* register control here */
9948a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
9958a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
9968a978234SLiam Girdwood 		if (ret < 0) {
9978a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
9988a978234SLiam Girdwood 				ec->hdr.name);
9998a978234SLiam Girdwood 			kfree(se);
10008a978234SLiam Girdwood 			continue;
10018a978234SLiam Girdwood 		}
10028a978234SLiam Girdwood 
10038a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
10048a978234SLiam Girdwood 	}
10058a978234SLiam Girdwood 
10068a978234SLiam Girdwood 	return 0;
10078a978234SLiam Girdwood }
10088a978234SLiam Girdwood 
10098a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
10108a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10118a978234SLiam Girdwood {
10128a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
10138a978234SLiam Girdwood 	int i;
10148a978234SLiam Girdwood 
10158a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
10168a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10178a978234SLiam Girdwood 		return 0;
10188a978234SLiam Girdwood 	}
10198a978234SLiam Girdwood 
10208a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
10218a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
10228a978234SLiam Girdwood 
10238a978234SLiam Girdwood 	for (i = 0; i < hdr->count; i++) {
10248a978234SLiam Girdwood 
10258a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
10268a978234SLiam Girdwood 
102706eb49f7SMengdong Lin 		if (control_hdr->size != sizeof(*control_hdr)) {
102806eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
102906eb49f7SMengdong Lin 			return -EINVAL;
103006eb49f7SMengdong Lin 		}
103106eb49f7SMengdong Lin 
10328a978234SLiam Girdwood 		switch (control_hdr->ops.info) {
10338a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
10348a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
10358a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
10368a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
10378a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
10388a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
10398a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
10408a978234SLiam Girdwood 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
10418a978234SLiam Girdwood 			break;
10428a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10438a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10448a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10458a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10468a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10478a978234SLiam Girdwood 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
10488a978234SLiam Girdwood 			break;
10498a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
10508a978234SLiam Girdwood 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
10518a978234SLiam Girdwood 			break;
10528a978234SLiam Girdwood 		default:
10538a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
10548a978234SLiam Girdwood 			return -EINVAL;
10558a978234SLiam Girdwood 		}
10568a978234SLiam Girdwood 	}
10578a978234SLiam Girdwood 
10588a978234SLiam Girdwood 	return 0;
10598a978234SLiam Girdwood }
10608a978234SLiam Girdwood 
10618a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
10628a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10638a978234SLiam Girdwood {
10648a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
10658a978234SLiam Girdwood 	struct snd_soc_dapm_route route;
10668a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
10678a978234SLiam Girdwood 	int count = hdr->count, i;
10688a978234SLiam Girdwood 
10698a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
10708a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10718a978234SLiam Girdwood 		return 0;
10728a978234SLiam Girdwood 	}
10738a978234SLiam Girdwood 
10748a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10758a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
10768a978234SLiam Girdwood 		count, hdr->payload_size, "graph")) {
10778a978234SLiam Girdwood 
10788a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
10798a978234SLiam Girdwood 			count);
10808a978234SLiam Girdwood 		return -EINVAL;
10818a978234SLiam Girdwood 	}
10828a978234SLiam Girdwood 
10838a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
10848a978234SLiam Girdwood 
10858a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
10868a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
10878a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
10888a978234SLiam Girdwood 
10898a978234SLiam Girdwood 		/* validate routes */
10908a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10918a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10928a978234SLiam Girdwood 			return -EINVAL;
10938a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10948a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10958a978234SLiam Girdwood 			return -EINVAL;
10968a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10978a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10988a978234SLiam Girdwood 			return -EINVAL;
10998a978234SLiam Girdwood 
11008a978234SLiam Girdwood 		route.source = elem->source;
11018a978234SLiam Girdwood 		route.sink = elem->sink;
11028a978234SLiam Girdwood 		route.connected = NULL; /* set to NULL atm for tplg users */
11038a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
11048a978234SLiam Girdwood 			route.control = NULL;
11058a978234SLiam Girdwood 		else
11068a978234SLiam Girdwood 			route.control = elem->control;
11078a978234SLiam Girdwood 
11088a978234SLiam Girdwood 		/* add route, but keep going if some fail */
11098a978234SLiam Girdwood 		snd_soc_dapm_add_routes(dapm, &route, 1);
11108a978234SLiam Girdwood 	}
11118a978234SLiam Girdwood 
11128a978234SLiam Girdwood 	return 0;
11138a978234SLiam Girdwood }
11148a978234SLiam Girdwood 
11158a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
11168a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
11178a978234SLiam Girdwood {
11188a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
11198a978234SLiam Girdwood 	struct soc_mixer_control *sm;
11208a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
11218a978234SLiam Girdwood 	int i, err;
11228a978234SLiam Girdwood 
11234ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
11248a978234SLiam Girdwood 	if (kc == NULL)
11258a978234SLiam Girdwood 		return NULL;
11268a978234SLiam Girdwood 
11278a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
11288a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
11298a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
11308a978234SLiam Girdwood 		if (sm == NULL)
11318a978234SLiam Girdwood 			goto err;
11328a978234SLiam Girdwood 
11338a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
11348a978234SLiam Girdwood 			mc->priv.size);
11358a978234SLiam Girdwood 
11368a978234SLiam Girdwood 		/* validate kcontrol */
11378a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11388a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11398a978234SLiam Girdwood 			goto err_str;
11408a978234SLiam Girdwood 
11418a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
11428a978234SLiam Girdwood 			mc->hdr.name, i);
11438a978234SLiam Girdwood 
11448a978234SLiam Girdwood 		kc[i].name = mc->hdr.name;
11458a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
11468a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
11478a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
11488a978234SLiam Girdwood 
11498a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
11508a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
11518a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11528a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
11538a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11548a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
11558a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11568a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
11578a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11588a978234SLiam Girdwood 
11598a978234SLiam Girdwood 		sm->max = mc->max;
11608a978234SLiam Girdwood 		sm->min = mc->min;
11618a978234SLiam Girdwood 		sm->invert = mc->invert;
11628a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
11638a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
11648a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
11658a978234SLiam Girdwood 
11668a978234SLiam Girdwood 		/* map io handlers */
11672b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
11688a978234SLiam Girdwood 		if (err) {
11698a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
11708a978234SLiam Girdwood 			kfree(sm);
11718a978234SLiam Girdwood 			continue;
11728a978234SLiam Girdwood 		}
11738a978234SLiam Girdwood 
11748a978234SLiam Girdwood 		/* pass control to driver for optional further init */
11758a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
11768a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
11778a978234SLiam Girdwood 		if (err < 0) {
11788a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
11798a978234SLiam Girdwood 				mc->hdr.name);
11808a978234SLiam Girdwood 			kfree(sm);
11818a978234SLiam Girdwood 			continue;
11828a978234SLiam Girdwood 		}
11838a978234SLiam Girdwood 	}
11848a978234SLiam Girdwood 	return kc;
11858a978234SLiam Girdwood 
11868a978234SLiam Girdwood err_str:
11878a978234SLiam Girdwood 	kfree(sm);
11888a978234SLiam Girdwood err:
11898a978234SLiam Girdwood 	for (--i; i >= 0; i--)
11908a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
11918a978234SLiam Girdwood 	kfree(kc);
11928a978234SLiam Girdwood 	return NULL;
11938a978234SLiam Girdwood }
11948a978234SLiam Girdwood 
11958a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
11968a978234SLiam Girdwood 	struct soc_tplg *tplg)
11978a978234SLiam Girdwood {
11988a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
11998a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
12008a978234SLiam Girdwood 	struct soc_enum *se;
12018a978234SLiam Girdwood 	int i, err;
12028a978234SLiam Girdwood 
12038a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
12048a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
12058a978234SLiam Girdwood 		ec->priv.size);
12068a978234SLiam Girdwood 
12078a978234SLiam Girdwood 	/* validate kcontrol */
12088a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12098a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
12108a978234SLiam Girdwood 		return NULL;
12118a978234SLiam Girdwood 
12128a978234SLiam Girdwood 	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
12138a978234SLiam Girdwood 	if (kc == NULL)
12148a978234SLiam Girdwood 		return NULL;
12158a978234SLiam Girdwood 
12168a978234SLiam Girdwood 	se = kzalloc(sizeof(*se), GFP_KERNEL);
12178a978234SLiam Girdwood 	if (se == NULL)
12188a978234SLiam Girdwood 		goto err;
12198a978234SLiam Girdwood 
12208a978234SLiam Girdwood 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
12218a978234SLiam Girdwood 		ec->hdr.name);
12228a978234SLiam Girdwood 
12238a978234SLiam Girdwood 	kc->name = ec->hdr.name;
12248a978234SLiam Girdwood 	kc->private_value = (long)se;
12258a978234SLiam Girdwood 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
12268a978234SLiam Girdwood 	kc->access = ec->hdr.access;
12278a978234SLiam Girdwood 
12288a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
12298a978234SLiam Girdwood 	se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
12308a978234SLiam Girdwood 	se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
12318a978234SLiam Girdwood 	se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
12328a978234SLiam Girdwood 
12338a978234SLiam Girdwood 	se->items = ec->items;
12348a978234SLiam Girdwood 	se->mask = ec->mask;
12358a978234SLiam Girdwood 	se->dobj.index = tplg->index;
12368a978234SLiam Girdwood 
12378a978234SLiam Girdwood 	switch (ec->hdr.ops.info) {
12388a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
12398a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
12408a978234SLiam Girdwood 		err = soc_tplg_denum_create_values(se, ec);
12418a978234SLiam Girdwood 		if (err < 0) {
12428a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
12438a978234SLiam Girdwood 				ec->hdr.name);
12448a978234SLiam Girdwood 			goto err_se;
12458a978234SLiam Girdwood 		}
12468a978234SLiam Girdwood 		/* fall through to create texts */
12478a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
12488a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
12498a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
12508a978234SLiam Girdwood 		err = soc_tplg_denum_create_texts(se, ec);
12518a978234SLiam Girdwood 		if (err < 0) {
12528a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
12538a978234SLiam Girdwood 				ec->hdr.name);
12548a978234SLiam Girdwood 			goto err_se;
12558a978234SLiam Girdwood 		}
12568a978234SLiam Girdwood 		break;
12578a978234SLiam Girdwood 	default:
12588a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
12598a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
12608a978234SLiam Girdwood 		goto err_se;
12618a978234SLiam Girdwood 	}
12628a978234SLiam Girdwood 
12638a978234SLiam Girdwood 	/* map io handlers */
12642b5cdb91SMengdong Lin 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
12658a978234SLiam Girdwood 	if (err) {
12668a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
12678a978234SLiam Girdwood 		goto err_se;
12688a978234SLiam Girdwood 	}
12698a978234SLiam Girdwood 
12708a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12718a978234SLiam Girdwood 	err = soc_tplg_init_kcontrol(tplg, kc,
12728a978234SLiam Girdwood 		(struct snd_soc_tplg_ctl_hdr *)ec);
12738a978234SLiam Girdwood 	if (err < 0) {
12748a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
12758a978234SLiam Girdwood 			ec->hdr.name);
12768a978234SLiam Girdwood 		goto err_se;
12778a978234SLiam Girdwood 	}
12788a978234SLiam Girdwood 
12798a978234SLiam Girdwood 	return kc;
12808a978234SLiam Girdwood 
12818a978234SLiam Girdwood err_se:
12828a978234SLiam Girdwood 	/* free values and texts */
12838a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
12848a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++)
12858a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
12868a978234SLiam Girdwood 
12878a978234SLiam Girdwood 	kfree(se);
12888a978234SLiam Girdwood err:
12898a978234SLiam Girdwood 	kfree(kc);
12908a978234SLiam Girdwood 
12918a978234SLiam Girdwood 	return NULL;
12928a978234SLiam Girdwood }
12938a978234SLiam Girdwood 
12948a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
12958a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
12968a978234SLiam Girdwood {
12978a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
12988a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
12998a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13008a978234SLiam Girdwood 	int i, err;
13018a978234SLiam Girdwood 
13024ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
13038a978234SLiam Girdwood 	if (!kc)
13048a978234SLiam Girdwood 		return NULL;
13058a978234SLiam Girdwood 
13068a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
13078a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
13088a978234SLiam Girdwood 
13098a978234SLiam Girdwood 		/* validate kcontrol */
13108a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13118a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13128a978234SLiam Girdwood 			goto err;
13138a978234SLiam Girdwood 
13148a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
13158a978234SLiam Girdwood 		if (sbe == NULL)
13168a978234SLiam Girdwood 			goto err;
13178a978234SLiam Girdwood 
13188a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
13198a978234SLiam Girdwood 			be->priv.size);
13208a978234SLiam Girdwood 
13218a978234SLiam Girdwood 		dev_dbg(tplg->dev,
13228a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
13238a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
13248a978234SLiam Girdwood 
13258a978234SLiam Girdwood 		kc[i].name = be->hdr.name;
13268a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
13278a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13288a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
13298a978234SLiam Girdwood 
13308a978234SLiam Girdwood 		sbe->max = be->max;
13318a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
13328a978234SLiam Girdwood 
13338a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
13342b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
13358a978234SLiam Girdwood 		if (err) {
13368a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
13378a978234SLiam Girdwood 			kfree(sbe);
13388a978234SLiam Girdwood 			continue;
13398a978234SLiam Girdwood 		}
13408a978234SLiam Girdwood 
13418a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13428a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13438a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
13448a978234SLiam Girdwood 		if (err < 0) {
13458a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13468a978234SLiam Girdwood 				be->hdr.name);
13478a978234SLiam Girdwood 			kfree(sbe);
13488a978234SLiam Girdwood 			continue;
13498a978234SLiam Girdwood 		}
13508a978234SLiam Girdwood 	}
13518a978234SLiam Girdwood 
13528a978234SLiam Girdwood 	return kc;
13538a978234SLiam Girdwood 
13548a978234SLiam Girdwood err:
13558a978234SLiam Girdwood 	for (--i; i >= 0; i--)
13568a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
13578a978234SLiam Girdwood 
13588a978234SLiam Girdwood 	kfree(kc);
13598a978234SLiam Girdwood 	return NULL;
13608a978234SLiam Girdwood }
13618a978234SLiam Girdwood 
13628a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
13638a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
13648a978234SLiam Girdwood {
13658a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
13668a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
13678a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
13688a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
13698a978234SLiam Girdwood 	int ret = 0;
13708a978234SLiam Girdwood 
13718a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13728a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13738a978234SLiam Girdwood 		return -EINVAL;
13748a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13758a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13768a978234SLiam Girdwood 		return -EINVAL;
13778a978234SLiam Girdwood 
13788a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
13798a978234SLiam Girdwood 		w->name, w->id);
13808a978234SLiam Girdwood 
13818a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
13828a978234SLiam Girdwood 
13838a978234SLiam Girdwood 	/* map user to kernel widget ID */
13848a978234SLiam Girdwood 	template.id = get_widget_id(w->id);
13858a978234SLiam Girdwood 	if (template.id < 0)
13868a978234SLiam Girdwood 		return template.id;
13878a978234SLiam Girdwood 
13888a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
13898a978234SLiam Girdwood 	if (!template.name)
13908a978234SLiam Girdwood 		return -ENOMEM;
13918a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
13928a978234SLiam Girdwood 	if (!template.sname) {
13938a978234SLiam Girdwood 		ret = -ENOMEM;
13948a978234SLiam Girdwood 		goto err;
13958a978234SLiam Girdwood 	}
13968a978234SLiam Girdwood 	template.reg = w->reg;
13978a978234SLiam Girdwood 	template.shift = w->shift;
13988a978234SLiam Girdwood 	template.mask = w->mask;
13996dc6db79SSubhransu S. Prusty 	template.subseq = w->subseq;
14008a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
14018a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
14028a978234SLiam Girdwood 	template.ignore_suspend = w->ignore_suspend;
14038a978234SLiam Girdwood 	template.event_flags = w->event_flags;
14048a978234SLiam Girdwood 	template.dobj.index = tplg->index;
14058a978234SLiam Girdwood 
14068a978234SLiam Girdwood 	tplg->pos +=
14078a978234SLiam Girdwood 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
14088a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
14098a978234SLiam Girdwood 		template.num_kcontrols = 0;
14108a978234SLiam Girdwood 		goto widget;
14118a978234SLiam Girdwood 	}
14128a978234SLiam Girdwood 
14138a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
14148a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
14158a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
14168a978234SLiam Girdwood 
14178a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
14188a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
14198a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
14208a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
14218a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
14228a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
14238a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
14248a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
14258a978234SLiam Girdwood 		template.kcontrol_news =
14268a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
14278a978234SLiam Girdwood 			template.num_kcontrols);
14288a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14298a978234SLiam Girdwood 			ret = -ENOMEM;
14308a978234SLiam Girdwood 			goto hdr_err;
14318a978234SLiam Girdwood 		}
14328a978234SLiam Girdwood 		break;
14338a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
14348a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
14358a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14368a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14378a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14388a978234SLiam Girdwood 		template.dobj.widget.kcontrol_enum = 1;
14398a978234SLiam Girdwood 		template.num_kcontrols = 1;
14408a978234SLiam Girdwood 		template.kcontrol_news =
14418a978234SLiam Girdwood 			soc_tplg_dapm_widget_denum_create(tplg);
14428a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14438a978234SLiam Girdwood 			ret = -ENOMEM;
14448a978234SLiam Girdwood 			goto hdr_err;
14458a978234SLiam Girdwood 		}
14468a978234SLiam Girdwood 		break;
14478a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
14488a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
14498a978234SLiam Girdwood 		template.kcontrol_news =
14508a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
14518a978234SLiam Girdwood 				template.num_kcontrols);
14528a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14538a978234SLiam Girdwood 			ret = -ENOMEM;
14548a978234SLiam Girdwood 			goto hdr_err;
14558a978234SLiam Girdwood 		}
14568a978234SLiam Girdwood 		break;
14578a978234SLiam Girdwood 	default:
14588a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
14598a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
14608a978234SLiam Girdwood 			control_hdr->ops.info);
14618a978234SLiam Girdwood 		ret = -EINVAL;
14628a978234SLiam Girdwood 		goto hdr_err;
14638a978234SLiam Girdwood 	}
14648a978234SLiam Girdwood 
14658a978234SLiam Girdwood widget:
14668a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
14678a978234SLiam Girdwood 	if (ret < 0)
14688a978234SLiam Girdwood 		goto hdr_err;
14698a978234SLiam Girdwood 
14708a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
14718a978234SLiam Girdwood 	 * data during sound card init. */
14728a978234SLiam Girdwood 	if (card->instantiated)
14738a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
14748a978234SLiam Girdwood 	else
14758a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
14768a978234SLiam Girdwood 	if (widget == NULL) {
14778a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
14788a978234SLiam Girdwood 			w->name);
14798a978234SLiam Girdwood 		goto hdr_err;
14808a978234SLiam Girdwood 	}
14818a978234SLiam Girdwood 
14828a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
14838a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
14848a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
14858ea41674SJeeja KP 	kfree(template.sname);
14868ea41674SJeeja KP 	kfree(template.name);
14878a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
14888a978234SLiam Girdwood 	return 0;
14898a978234SLiam Girdwood 
14908a978234SLiam Girdwood hdr_err:
14918a978234SLiam Girdwood 	kfree(template.sname);
14928a978234SLiam Girdwood err:
14938a978234SLiam Girdwood 	kfree(template.name);
14948a978234SLiam Girdwood 	return ret;
14958a978234SLiam Girdwood }
14968a978234SLiam Girdwood 
14978a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
14988a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
14998a978234SLiam Girdwood {
15008a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
15018a978234SLiam Girdwood 	int ret, count = hdr->count, i;
15028a978234SLiam Girdwood 
15038a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
15048a978234SLiam Girdwood 		return 0;
15058a978234SLiam Girdwood 
15068a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
15078a978234SLiam Girdwood 
15088a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
15098a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
151006eb49f7SMengdong Lin 		if (widget->size != sizeof(*widget)) {
151106eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
151206eb49f7SMengdong Lin 			return -EINVAL;
151306eb49f7SMengdong Lin 		}
151406eb49f7SMengdong Lin 
15158a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
15167de76b62SMengdong Lin 		if (ret < 0) {
15178a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
15188a978234SLiam Girdwood 				widget->name);
15197de76b62SMengdong Lin 			return ret;
15207de76b62SMengdong Lin 		}
15218a978234SLiam Girdwood 	}
15228a978234SLiam Girdwood 
15238a978234SLiam Girdwood 	return 0;
15248a978234SLiam Girdwood }
15258a978234SLiam Girdwood 
15268a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
15278a978234SLiam Girdwood {
15288a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
15298a978234SLiam Girdwood 	int ret;
15308a978234SLiam Girdwood 
15318a978234SLiam Girdwood 	/* Card might not have been registered at this point.
15328a978234SLiam Girdwood 	 * If so, just return success.
15338a978234SLiam Girdwood 	*/
15348a978234SLiam Girdwood 	if (!card || !card->instantiated) {
15358a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
15368a978234SLiam Girdwood 				"Do not add new widgets now\n");
15378a978234SLiam Girdwood 		return 0;
15388a978234SLiam Girdwood 	}
15398a978234SLiam Girdwood 
15408a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
15418a978234SLiam Girdwood 	if (ret < 0)
15428a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
15438a978234SLiam Girdwood 			ret);
15448a978234SLiam Girdwood 
15458a978234SLiam Girdwood 	return 0;
15468a978234SLiam Girdwood }
15478a978234SLiam Girdwood 
1548b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream,
1549b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1550b6b6e4d6SMengdong Lin {
1551b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1552b6b6e4d6SMengdong Lin 	stream->channels_min = caps->channels_min;
1553b6b6e4d6SMengdong Lin 	stream->channels_max = caps->channels_max;
1554b6b6e4d6SMengdong Lin 	stream->rates = caps->rates;
1555b6b6e4d6SMengdong Lin 	stream->rate_min = caps->rate_min;
1556b6b6e4d6SMengdong Lin 	stream->rate_max = caps->rate_max;
1557b6b6e4d6SMengdong Lin 	stream->formats = caps->formats;
1558b6b6e4d6SMengdong Lin }
1559b6b6e4d6SMengdong Lin 
1560*0038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1561*0038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
1562*0038be9aSMengdong Lin {
1563*0038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1564*0038be9aSMengdong Lin 		dai_drv->symmetric_rates =
1565*0038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1566*0038be9aSMengdong Lin 
1567*0038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1568*0038be9aSMengdong Lin 		dai_drv->symmetric_channels =
1569*0038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1570*0038be9aSMengdong Lin 			1 : 0;
1571*0038be9aSMengdong Lin 
1572*0038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1573*0038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
1574*0038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1575*0038be9aSMengdong Lin 			1 : 0;
1576*0038be9aSMengdong Lin }
1577*0038be9aSMengdong Lin 
157864527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
157964527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
158064527e8aSMengdong Lin {
158164527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
158264527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
158364527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
158464527e8aSMengdong Lin 	int ret;
158564527e8aSMengdong Lin 
158664527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
158764527e8aSMengdong Lin 	if (dai_drv == NULL)
158864527e8aSMengdong Lin 		return -ENOMEM;
158964527e8aSMengdong Lin 
159064527e8aSMengdong Lin 	dai_drv->name = pcm->dai_name;
159164527e8aSMengdong Lin 	dai_drv->id = pcm->dai_id;
159264527e8aSMengdong Lin 
159364527e8aSMengdong Lin 	if (pcm->playback) {
159464527e8aSMengdong Lin 		stream = &dai_drv->playback;
159564527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1596b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
159764527e8aSMengdong Lin 	}
159864527e8aSMengdong Lin 
159964527e8aSMengdong Lin 	if (pcm->capture) {
160064527e8aSMengdong Lin 		stream = &dai_drv->capture;
160164527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1602b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
160364527e8aSMengdong Lin 	}
160464527e8aSMengdong Lin 
160564527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
160664527e8aSMengdong Lin 	ret = soc_tplg_dai_load(tplg, dai_drv);
160764527e8aSMengdong Lin 	if (ret < 0) {
160864527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
160964527e8aSMengdong Lin 		kfree(dai_drv);
161064527e8aSMengdong Lin 		return ret;
161164527e8aSMengdong Lin 	}
161264527e8aSMengdong Lin 
161364527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
161464527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
161564527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
161664527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
161764527e8aSMengdong Lin 
161864527e8aSMengdong Lin 	/* register the DAI to the component */
161964527e8aSMengdong Lin 	return snd_soc_register_dai(tplg->comp, dai_drv);
162064527e8aSMengdong Lin }
162164527e8aSMengdong Lin 
162267d1c21eSGuneshwor Singh /* create the FE DAI link */
1623acfc7d46SMengdong Lin static int soc_tplg_link_create(struct soc_tplg *tplg,
1624acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1625acfc7d46SMengdong Lin {
1626acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
1627acfc7d46SMengdong Lin 	int ret;
1628acfc7d46SMengdong Lin 
1629acfc7d46SMengdong Lin 	link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1630acfc7d46SMengdong Lin 	if (link == NULL)
1631acfc7d46SMengdong Lin 		return -ENOMEM;
1632acfc7d46SMengdong Lin 
1633acfc7d46SMengdong Lin 	link->name = pcm->pcm_name;
1634acfc7d46SMengdong Lin 	link->stream_name = pcm->pcm_name;
1635b84fff5aSMengdong Lin 	link->id = pcm->pcm_id;
1636acfc7d46SMengdong Lin 
163767d1c21eSGuneshwor Singh 	link->cpu_dai_name = pcm->dai_name;
163867d1c21eSGuneshwor Singh 	link->codec_name = "snd-soc-dummy";
163967d1c21eSGuneshwor Singh 	link->codec_dai_name = "snd-soc-dummy-dai";
164067d1c21eSGuneshwor Singh 
164167d1c21eSGuneshwor Singh 	/* enable DPCM */
164267d1c21eSGuneshwor Singh 	link->dynamic = 1;
164367d1c21eSGuneshwor Singh 	link->dpcm_playback = pcm->playback;
164467d1c21eSGuneshwor Singh 	link->dpcm_capture = pcm->capture;
164567d1c21eSGuneshwor Singh 
1646acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1647acfc7d46SMengdong Lin 	ret = soc_tplg_dai_link_load(tplg, link);
1648acfc7d46SMengdong Lin 	if (ret < 0) {
1649acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1650acfc7d46SMengdong Lin 		kfree(link);
1651acfc7d46SMengdong Lin 		return ret;
1652acfc7d46SMengdong Lin 	}
1653acfc7d46SMengdong Lin 
1654acfc7d46SMengdong Lin 	link->dobj.index = tplg->index;
1655acfc7d46SMengdong Lin 	link->dobj.ops = tplg->ops;
1656acfc7d46SMengdong Lin 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1657acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1658acfc7d46SMengdong Lin 
1659acfc7d46SMengdong Lin 	snd_soc_add_dai_link(tplg->comp->card, link);
1660acfc7d46SMengdong Lin 	return 0;
1661acfc7d46SMengdong Lin }
1662acfc7d46SMengdong Lin 
1663acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
166464527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
166564527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
166664527e8aSMengdong Lin {
1667acfc7d46SMengdong Lin 	int ret;
1668acfc7d46SMengdong Lin 
1669acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1670acfc7d46SMengdong Lin 	if (ret < 0)
1671acfc7d46SMengdong Lin 		return ret;
1672acfc7d46SMengdong Lin 
1673acfc7d46SMengdong Lin 	return  soc_tplg_link_create(tplg, pcm);
167464527e8aSMengdong Lin }
167564527e8aSMengdong Lin 
167664527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
16778a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
16788a978234SLiam Girdwood {
167964527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm;
16808a978234SLiam Girdwood 	int count = hdr->count;
168164527e8aSMengdong Lin 	int i;
16828a978234SLiam Girdwood 
16838a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
16848a978234SLiam Girdwood 		return 0;
16858a978234SLiam Girdwood 
16868a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
16875b2688a5SVedang Patel 		sizeof(struct snd_soc_tplg_pcm), count,
16888a978234SLiam Girdwood 		hdr->payload_size, "PCM DAI")) {
16898a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
16908a978234SLiam Girdwood 			count);
16918a978234SLiam Girdwood 		return -EINVAL;
16928a978234SLiam Girdwood 	}
16938a978234SLiam Girdwood 
169464527e8aSMengdong Lin 	/* create the FE DAIs and DAI links */
169506eb49f7SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
169664527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
169706eb49f7SMengdong Lin 		if (pcm->size != sizeof(*pcm)) {
169806eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid pcm size\n");
169906eb49f7SMengdong Lin 			return -EINVAL;
170006eb49f7SMengdong Lin 		}
170106eb49f7SMengdong Lin 
170264527e8aSMengdong Lin 		soc_tplg_pcm_create(tplg, pcm);
170364527e8aSMengdong Lin 		pcm++;
170464527e8aSMengdong Lin 	}
170564527e8aSMengdong Lin 
17068a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
17075b2688a5SVedang Patel 	tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
17088a978234SLiam Girdwood 
17098a978234SLiam Girdwood 	return 0;
17108a978234SLiam Girdwood }
17118a978234SLiam Girdwood 
1712*0038be9aSMengdong Lin /* *
1713*0038be9aSMengdong Lin  * soc_tplg_be_dai_config - Find and configure an existing BE DAI.
1714*0038be9aSMengdong Lin  * @tplg: topology context
1715*0038be9aSMengdong Lin  * @be: topology BE DAI configs.
1716*0038be9aSMengdong Lin  *
1717*0038be9aSMengdong Lin  * The BE dai should already be registered by the platform driver. The
1718*0038be9aSMengdong Lin  * platform driver should specify the BE DAI name and ID for matching.
1719*0038be9aSMengdong Lin  */
1720*0038be9aSMengdong Lin static int soc_tplg_be_dai_config(struct soc_tplg *tplg,
1721*0038be9aSMengdong Lin 				  struct snd_soc_tplg_be_dai *be)
1722*0038be9aSMengdong Lin {
1723*0038be9aSMengdong Lin 	struct snd_soc_dai_link_component dai_component = {0};
1724*0038be9aSMengdong Lin 	struct snd_soc_dai *dai;
1725*0038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
1726*0038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
1727*0038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
1728*0038be9aSMengdong Lin 	int ret;
1729*0038be9aSMengdong Lin 
1730*0038be9aSMengdong Lin 	dai_component.dai_name = be->dai_name;
1731*0038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
1732*0038be9aSMengdong Lin 	if (!dai) {
1733*0038be9aSMengdong Lin 		dev_err(tplg->dev, "ASoC: BE DAI %s not registered\n",
1734*0038be9aSMengdong Lin 			be->dai_name);
1735*0038be9aSMengdong Lin 		return -EINVAL;
1736*0038be9aSMengdong Lin 	}
1737*0038be9aSMengdong Lin 
1738*0038be9aSMengdong Lin 	if (be->dai_id != dai->id) {
1739*0038be9aSMengdong Lin 		dev_err(tplg->dev, "ASoC: BE DAI %s id mismatch\n",
1740*0038be9aSMengdong Lin 			be->dai_name);
1741*0038be9aSMengdong Lin 		return -EINVAL;
1742*0038be9aSMengdong Lin 	}
1743*0038be9aSMengdong Lin 
1744*0038be9aSMengdong Lin 	dai_drv = dai->driver;
1745*0038be9aSMengdong Lin 	if (!dai_drv)
1746*0038be9aSMengdong Lin 		return -EINVAL;
1747*0038be9aSMengdong Lin 
1748*0038be9aSMengdong Lin 	if (be->playback) {
1749*0038be9aSMengdong Lin 		stream = &dai_drv->playback;
1750*0038be9aSMengdong Lin 		caps = &be->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1751*0038be9aSMengdong Lin 		set_stream_info(stream, caps);
1752*0038be9aSMengdong Lin 	}
1753*0038be9aSMengdong Lin 
1754*0038be9aSMengdong Lin 	if (be->capture) {
1755*0038be9aSMengdong Lin 		stream = &dai_drv->capture;
1756*0038be9aSMengdong Lin 		caps = &be->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1757*0038be9aSMengdong Lin 		set_stream_info(stream, caps);
1758*0038be9aSMengdong Lin 	}
1759*0038be9aSMengdong Lin 
1760*0038be9aSMengdong Lin 	if (be->flag_mask)
1761*0038be9aSMengdong Lin 		set_dai_flags(dai_drv, be->flag_mask, be->flags);
1762*0038be9aSMengdong Lin 
1763*0038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
1764*0038be9aSMengdong Lin 	ret = soc_tplg_dai_load(tplg, dai_drv);
1765*0038be9aSMengdong Lin 	if (ret < 0) {
1766*0038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1767*0038be9aSMengdong Lin 		return ret;
1768*0038be9aSMengdong Lin 	}
1769*0038be9aSMengdong Lin 
1770*0038be9aSMengdong Lin 	return 0;
1771*0038be9aSMengdong Lin }
1772*0038be9aSMengdong Lin 
1773*0038be9aSMengdong Lin static int soc_tplg_be_dai_elems_load(struct soc_tplg *tplg,
1774*0038be9aSMengdong Lin 				      struct snd_soc_tplg_hdr *hdr)
1775*0038be9aSMengdong Lin {
1776*0038be9aSMengdong Lin 	struct snd_soc_tplg_be_dai *be;
1777*0038be9aSMengdong Lin 	int count = hdr->count;
1778*0038be9aSMengdong Lin 	int i;
1779*0038be9aSMengdong Lin 
1780*0038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
1781*0038be9aSMengdong Lin 		return 0;
1782*0038be9aSMengdong Lin 
1783*0038be9aSMengdong Lin 	/* config the existing BE DAIs */
1784*0038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
1785*0038be9aSMengdong Lin 		be = (struct snd_soc_tplg_be_dai *)tplg->pos;
1786*0038be9aSMengdong Lin 		if (be->size != sizeof(*be)) {
1787*0038be9aSMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid BE DAI size\n");
1788*0038be9aSMengdong Lin 			return -EINVAL;
1789*0038be9aSMengdong Lin 		}
1790*0038be9aSMengdong Lin 
1791*0038be9aSMengdong Lin 		soc_tplg_be_dai_config(tplg, be);
1792*0038be9aSMengdong Lin 		tplg->pos += (sizeof(*be) + be->priv.size);
1793*0038be9aSMengdong Lin 	}
1794*0038be9aSMengdong Lin 
1795*0038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
1796*0038be9aSMengdong Lin 	return 0;
1797*0038be9aSMengdong Lin }
1798*0038be9aSMengdong Lin 
1799*0038be9aSMengdong Lin 
18008a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
18018a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
18028a978234SLiam Girdwood {
18038a978234SLiam Girdwood 	struct snd_soc_tplg_manifest *manifest;
18048a978234SLiam Girdwood 
18058a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
18068a978234SLiam Girdwood 		return 0;
18078a978234SLiam Girdwood 
18088a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
180906eb49f7SMengdong Lin 	if (manifest->size != sizeof(*manifest)) {
181006eb49f7SMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid manifest size\n");
181106eb49f7SMengdong Lin 		return -EINVAL;
181206eb49f7SMengdong Lin 	}
181306eb49f7SMengdong Lin 
18148a978234SLiam Girdwood 	tplg->pos += sizeof(struct snd_soc_tplg_manifest);
18158a978234SLiam Girdwood 
18168a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
18178a978234SLiam Girdwood 		return tplg->ops->manifest(tplg->comp, manifest);
18188a978234SLiam Girdwood 
18198a978234SLiam Girdwood 	dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
18208a978234SLiam Girdwood 	return 0;
18218a978234SLiam Girdwood }
18228a978234SLiam Girdwood 
18238a978234SLiam Girdwood /* validate header magic, size and type */
18248a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
18258a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
18268a978234SLiam Girdwood {
18278a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
18288a978234SLiam Girdwood 		return 0;
18298a978234SLiam Girdwood 
183006eb49f7SMengdong Lin 	if (hdr->size != sizeof(*hdr)) {
183106eb49f7SMengdong Lin 		dev_err(tplg->dev,
183206eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
183306eb49f7SMengdong Lin 			hdr->type, soc_tplg_get_hdr_offset(tplg),
183406eb49f7SMengdong Lin 			tplg->fw->size);
183506eb49f7SMengdong Lin 		return -EINVAL;
183606eb49f7SMengdong Lin 	}
183706eb49f7SMengdong Lin 
18388a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
18398a978234SLiam Girdwood 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
18408a978234SLiam Girdwood 		dev_err(tplg->dev,
18418a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
18428a978234SLiam Girdwood 			tplg->pass, hdr->magic,
18438a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
18448a978234SLiam Girdwood 		return -EINVAL;
18458a978234SLiam Girdwood 	}
18468a978234SLiam Girdwood 
18478a978234SLiam Girdwood 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
18488a978234SLiam Girdwood 		dev_err(tplg->dev,
18498a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
18508a978234SLiam Girdwood 			tplg->pass, hdr->magic,
18518a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
18528a978234SLiam Girdwood 		return -EINVAL;
18538a978234SLiam Girdwood 	}
18548a978234SLiam Girdwood 
18558a978234SLiam Girdwood 	if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
18568a978234SLiam Girdwood 		dev_err(tplg->dev,
18578a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
18588a978234SLiam Girdwood 			tplg->pass, hdr->abi,
18598a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
18608a978234SLiam Girdwood 			tplg->fw->size);
18618a978234SLiam Girdwood 		return -EINVAL;
18628a978234SLiam Girdwood 	}
18638a978234SLiam Girdwood 
18648a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
18658a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
18668a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
18678a978234SLiam Girdwood 		return -EINVAL;
18688a978234SLiam Girdwood 	}
18698a978234SLiam Girdwood 
18708a978234SLiam Girdwood 	if (tplg->pass == hdr->type)
18718a978234SLiam Girdwood 		dev_dbg(tplg->dev,
18728a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
18738a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
18748a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
18758a978234SLiam Girdwood 
18768a978234SLiam Girdwood 	return 1;
18778a978234SLiam Girdwood }
18788a978234SLiam Girdwood 
18798a978234SLiam Girdwood /* check header type and call appropriate handler */
18808a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
18818a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
18828a978234SLiam Girdwood {
18838a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
18848a978234SLiam Girdwood 
18858a978234SLiam Girdwood 	/* check for matching ID */
18868a978234SLiam Girdwood 	if (hdr->index != tplg->req_index &&
18878a978234SLiam Girdwood 		hdr->index != SND_SOC_TPLG_INDEX_ALL)
18888a978234SLiam Girdwood 		return 0;
18898a978234SLiam Girdwood 
18908a978234SLiam Girdwood 	tplg->index = hdr->index;
18918a978234SLiam Girdwood 
18928a978234SLiam Girdwood 	switch (hdr->type) {
18938a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
18948a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
18958a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
18968a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
18978a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
18988a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
18998a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
19008a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
19018a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
190264527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
1903*0038be9aSMengdong Lin 	case SND_SOC_TPLG_TYPE_BE_DAI:
1904*0038be9aSMengdong Lin 		return soc_tplg_be_dai_elems_load(tplg, hdr);
19058a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
19068a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
19078a978234SLiam Girdwood 	default:
19088a978234SLiam Girdwood 		/* bespoke vendor data object */
19098a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
19108a978234SLiam Girdwood 	}
19118a978234SLiam Girdwood 
19128a978234SLiam Girdwood 	return 0;
19138a978234SLiam Girdwood }
19148a978234SLiam Girdwood 
19158a978234SLiam Girdwood /* process the topology file headers */
19168a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
19178a978234SLiam Girdwood {
19188a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
19198a978234SLiam Girdwood 	int ret;
19208a978234SLiam Girdwood 
19218a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
19228a978234SLiam Girdwood 
19238a978234SLiam Girdwood 	/* process the header types from start to end */
19248a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
19258a978234SLiam Girdwood 
19268a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
19278a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
19288a978234SLiam Girdwood 
19298a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
19308a978234SLiam Girdwood 
19318a978234SLiam Girdwood 			/* make sure header is valid before loading */
19328a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
19338a978234SLiam Girdwood 			if (ret < 0)
19348a978234SLiam Girdwood 				return ret;
19358a978234SLiam Girdwood 			else if (ret == 0)
19368a978234SLiam Girdwood 				break;
19378a978234SLiam Girdwood 
19388a978234SLiam Girdwood 			/* load the header object */
19398a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
19408a978234SLiam Girdwood 			if (ret < 0)
19418a978234SLiam Girdwood 				return ret;
19428a978234SLiam Girdwood 
19438a978234SLiam Girdwood 			/* goto next header */
19448a978234SLiam Girdwood 			tplg->hdr_pos += hdr->payload_size +
19458a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
19468a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
19478a978234SLiam Girdwood 		}
19488a978234SLiam Girdwood 
19498a978234SLiam Girdwood 		/* next data type pass */
19508a978234SLiam Girdwood 		tplg->pass++;
19518a978234SLiam Girdwood 	}
19528a978234SLiam Girdwood 
19538a978234SLiam Girdwood 	/* signal DAPM we are complete */
19548a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
19558a978234SLiam Girdwood 	if (ret < 0)
19568a978234SLiam Girdwood 		dev_err(tplg->dev,
19578a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
19588a978234SLiam Girdwood 
19598a978234SLiam Girdwood 	return ret;
19608a978234SLiam Girdwood }
19618a978234SLiam Girdwood 
19628a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
19638a978234SLiam Girdwood {
19648a978234SLiam Girdwood 	int ret;
19658a978234SLiam Girdwood 
19668a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
19678a978234SLiam Girdwood 	if (ret == 0)
19688a978234SLiam Girdwood 		soc_tplg_complete(tplg);
19698a978234SLiam Girdwood 
19708a978234SLiam Girdwood 	return ret;
19718a978234SLiam Girdwood }
19728a978234SLiam Girdwood 
19738a978234SLiam Girdwood /* load audio component topology from "firmware" file */
19748a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
19758a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
19768a978234SLiam Girdwood {
19778a978234SLiam Girdwood 	struct soc_tplg tplg;
19788a978234SLiam Girdwood 
19798a978234SLiam Girdwood 	/* setup parsing context */
19808a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
19818a978234SLiam Girdwood 	tplg.fw = fw;
19828a978234SLiam Girdwood 	tplg.dev = comp->dev;
19838a978234SLiam Girdwood 	tplg.comp = comp;
19848a978234SLiam Girdwood 	tplg.ops = ops;
19858a978234SLiam Girdwood 	tplg.req_index = id;
19868a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
19878a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
19881a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
19891a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
19908a978234SLiam Girdwood 
19918a978234SLiam Girdwood 	return soc_tplg_load(&tplg);
19928a978234SLiam Girdwood }
19938a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
19948a978234SLiam Girdwood 
19958a978234SLiam Girdwood /* remove this dynamic widget */
19968a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
19978a978234SLiam Girdwood {
19988a978234SLiam Girdwood 	/* make sure we are a widget */
19998a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
20008a978234SLiam Girdwood 		return;
20018a978234SLiam Girdwood 
20028a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
20038a978234SLiam Girdwood }
20048a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
20058a978234SLiam Girdwood 
20068a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
20078a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
20088a978234SLiam Girdwood 	u32 index)
20098a978234SLiam Girdwood {
20108a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
20118a978234SLiam Girdwood 
20128a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
20138a978234SLiam Girdwood 
20148a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
20158a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
20168a978234SLiam Girdwood 			continue;
20178a978234SLiam Girdwood 
20188a978234SLiam Girdwood 		/* match ID */
20198a978234SLiam Girdwood 		if (w->dobj.index != index &&
20208a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
20218a978234SLiam Girdwood 			continue;
20228a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
20238a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2024b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
20258a978234SLiam Girdwood 	}
2026fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
20278a978234SLiam Girdwood }
20288a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
20298a978234SLiam Girdwood 
20308a978234SLiam Girdwood /* remove dynamic controls from the component driver */
20318a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
20328a978234SLiam Girdwood {
20338a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
20348a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
20358a978234SLiam Girdwood 
20368a978234SLiam Girdwood 	/* process the header types from end to start */
20378a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
20388a978234SLiam Girdwood 
20398a978234SLiam Girdwood 		/* remove mixer controls */
20408a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
20418a978234SLiam Girdwood 			list) {
20428a978234SLiam Girdwood 
20438a978234SLiam Girdwood 			/* match index */
20448a978234SLiam Girdwood 			if (dobj->index != index &&
20458a978234SLiam Girdwood 				dobj->index != SND_SOC_TPLG_INDEX_ALL)
20468a978234SLiam Girdwood 				continue;
20478a978234SLiam Girdwood 
20488a978234SLiam Girdwood 			switch (dobj->type) {
20498a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
20508a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
20518a978234SLiam Girdwood 				break;
20528a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
20538a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
20548a978234SLiam Girdwood 				break;
20558a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
20568a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
20578a978234SLiam Girdwood 				break;
20588a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
20598a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
20608a978234SLiam Girdwood 				break;
20618a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
206264527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
20638a978234SLiam Girdwood 				break;
2064acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2065acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2066acfc7d46SMengdong Lin 				break;
20678a978234SLiam Girdwood 			default:
20688a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
20698a978234SLiam Girdwood 					dobj->type);
20708a978234SLiam Girdwood 				break;
20718a978234SLiam Girdwood 			}
20728a978234SLiam Girdwood 		}
20738a978234SLiam Girdwood 		pass--;
20748a978234SLiam Girdwood 	}
20758a978234SLiam Girdwood 
20768a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
20778a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
20788a978234SLiam Girdwood }
20798a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2080