xref: /openbmc/linux/sound/soc/soc-topology.c (revision 583958fa)
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
510038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI		7
528a978234SLiam Girdwood 
538a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
540038be9aSMengdong Lin #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_BE_DAI
558a978234SLiam Girdwood 
56*583958faSMengdong Lin 
57*583958faSMengdong Lin /*
58*583958faSMengdong Lin  * Old version of ABI structs, supported for backward compatibility.
59*583958faSMengdong Lin  */
60*583958faSMengdong Lin 
61*583958faSMengdong Lin /* Manifest v4 */
62*583958faSMengdong Lin struct snd_soc_tplg_manifest_v4 {
63*583958faSMengdong Lin 	__le32 size;		/* in bytes of this structure */
64*583958faSMengdong Lin 	__le32 control_elems;	/* number of control elements */
65*583958faSMengdong Lin 	__le32 widget_elems;	/* number of widget elements */
66*583958faSMengdong Lin 	__le32 graph_elems;	/* number of graph elements */
67*583958faSMengdong Lin 	__le32 pcm_elems;	/* number of PCM elements */
68*583958faSMengdong Lin 	__le32 dai_link_elems;	/* number of DAI link elements */
69*583958faSMengdong Lin 	struct snd_soc_tplg_private priv;
70*583958faSMengdong Lin } __packed;
71*583958faSMengdong Lin 
72*583958faSMengdong Lin /* topology context */
738a978234SLiam Girdwood struct soc_tplg {
748a978234SLiam Girdwood 	const struct firmware *fw;
758a978234SLiam Girdwood 
768a978234SLiam Girdwood 	/* runtime FW parsing */
778a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
788a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
798a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
808a978234SLiam Girdwood 
818a978234SLiam Girdwood 	/* component caller */
828a978234SLiam Girdwood 	struct device *dev;
838a978234SLiam Girdwood 	struct snd_soc_component *comp;
848a978234SLiam Girdwood 	u32 index;	/* current block index */
858a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
868a978234SLiam Girdwood 
8788a17d8fSMengdong Lin 	/* vendor specific kcontrol operations */
888a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
898a978234SLiam Girdwood 	int io_ops_count;
908a978234SLiam Girdwood 
911a3232d2SMengdong Lin 	/* vendor specific bytes ext handlers, for TLV bytes controls */
921a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
931a3232d2SMengdong Lin 	int bytes_ext_ops_count;
941a3232d2SMengdong Lin 
958a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
968a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
978a978234SLiam Girdwood };
988a978234SLiam Girdwood 
998a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg);
1008a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
1018a978234SLiam Girdwood struct snd_soc_dapm_widget *
1028a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
1038a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
1048a978234SLiam Girdwood struct snd_soc_dapm_widget *
1058a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
1068a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
1078a978234SLiam Girdwood 
1088a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
1098a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
1108a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
1118a978234SLiam Girdwood {
1128a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
1138a978234SLiam Girdwood 
1148a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
1158a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
1168a978234SLiam Girdwood 			elem_type);
1178a978234SLiam Girdwood 		return -EINVAL;
1188a978234SLiam Girdwood 	}
1198a978234SLiam Girdwood 
1208a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
1218a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
1228a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1238a978234SLiam Girdwood 		dev_err(tplg->dev,
1248a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1258a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1268a978234SLiam Girdwood 		return -EINVAL;
1278a978234SLiam Girdwood 	}
1288a978234SLiam Girdwood 
1298a978234SLiam Girdwood 	return 0;
1308a978234SLiam Girdwood }
1318a978234SLiam Girdwood 
1328a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1338a978234SLiam Girdwood {
1348a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1358a978234SLiam Girdwood 
1368a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1378a978234SLiam Girdwood 		return 1;
1388a978234SLiam Girdwood 	return 0;
1398a978234SLiam Girdwood }
1408a978234SLiam Girdwood 
1418a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1428a978234SLiam Girdwood {
1438a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1448a978234SLiam Girdwood }
1458a978234SLiam Girdwood 
1468a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1478a978234SLiam Girdwood {
1488a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1498a978234SLiam Girdwood }
1508a978234SLiam Girdwood 
1518a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1528a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1538a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1548a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1558a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1568a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1578a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1588a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1598a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1608a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1618a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1628a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1638a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1648a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1658a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1668a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1678a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1688a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1702c57d478SJeeja KP 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1728a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1748a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1768a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1788a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1798a978234SLiam Girdwood };
1808a978234SLiam Girdwood 
1818a978234SLiam Girdwood struct soc_tplg_map {
1828a978234SLiam Girdwood 	int uid;
1838a978234SLiam Girdwood 	int kid;
1848a978234SLiam Girdwood };
1858a978234SLiam Girdwood 
1868a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1878a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1888a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1898a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1908a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1918a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1928a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1938a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1948a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1958a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1968a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1978a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1988a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1998a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
2008a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
2018a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
2028a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
2038a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
2048a978234SLiam Girdwood };
2058a978234SLiam Girdwood 
2068a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
2078a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2088a978234SLiam Girdwood {
2098a978234SLiam Girdwood 	int i;
2108a978234SLiam Girdwood 
2118a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2128a978234SLiam Girdwood 		if (chan[i].id == map)
2138a978234SLiam Girdwood 			return chan[i].reg;
2148a978234SLiam Girdwood 	}
2158a978234SLiam Girdwood 
2168a978234SLiam Girdwood 	return -EINVAL;
2178a978234SLiam Girdwood }
2188a978234SLiam Girdwood 
2198a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
2208a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
2218a978234SLiam Girdwood {
2228a978234SLiam Girdwood 	int i;
2238a978234SLiam Girdwood 
2248a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2258a978234SLiam Girdwood 		if (chan[i].id == map)
2268a978234SLiam Girdwood 			return chan[i].shift;
2278a978234SLiam Girdwood 	}
2288a978234SLiam Girdwood 
2298a978234SLiam Girdwood 	return -EINVAL;
2308a978234SLiam Girdwood }
2318a978234SLiam Girdwood 
2328a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2338a978234SLiam Girdwood {
2348a978234SLiam Girdwood 	int i;
2358a978234SLiam Girdwood 
2368a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2378a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2388a978234SLiam Girdwood 			return dapm_map[i].kid;
2398a978234SLiam Girdwood 	}
2408a978234SLiam Girdwood 
2418a978234SLiam Girdwood 	return -EINVAL;
2428a978234SLiam Girdwood }
2438a978234SLiam Girdwood 
2448a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2458a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2468a978234SLiam Girdwood {
2478a978234SLiam Girdwood 	dev_err(tplg->dev,
2488a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2498a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2508a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2518a978234SLiam Girdwood }
2528a978234SLiam Girdwood 
2538a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2548a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2558a978234SLiam Girdwood {
2568a978234SLiam Girdwood 	dev_err(tplg->dev,
2578a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2588a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2598a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2608a978234SLiam Girdwood }
2618a978234SLiam Girdwood 
2628a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2638a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2648a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2658a978234SLiam Girdwood {
2668a978234SLiam Girdwood 	int ret = 0;
2678a978234SLiam Girdwood 
2688a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
2698a978234SLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, hdr);
2708a978234SLiam Girdwood 	else {
2718a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2728a978234SLiam Girdwood 			hdr->vendor_type);
2738a978234SLiam Girdwood 		return -EINVAL;
2748a978234SLiam Girdwood 	}
2758a978234SLiam Girdwood 
2768a978234SLiam Girdwood 	if (ret < 0)
2778a978234SLiam Girdwood 		dev_err(tplg->dev,
2788a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
2798a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2808a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
2818a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
2828a978234SLiam Girdwood 	return ret;
2838a978234SLiam Girdwood }
2848a978234SLiam Girdwood 
2858a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2868a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
2878a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2888a978234SLiam Girdwood {
2898a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
2908a978234SLiam Girdwood 		return 0;
2918a978234SLiam Girdwood 
2928a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
2938a978234SLiam Girdwood }
2948a978234SLiam Girdwood 
2958a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
2968a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
2978a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
2988a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
2998a978234SLiam Girdwood {
3008a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
3018a978234SLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, w, tplg_w);
3028a978234SLiam Girdwood 
3038a978234SLiam Girdwood 	return 0;
3048a978234SLiam Girdwood }
3058a978234SLiam Girdwood 
30664527e8aSMengdong Lin /* pass DAI configurations to component driver for extra intialization */
30764527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg,
30864527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv)
3098a978234SLiam Girdwood {
31064527e8aSMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
31164527e8aSMengdong Lin 		return tplg->ops->dai_load(tplg->comp, dai_drv);
3128a978234SLiam Girdwood 
3138a978234SLiam Girdwood 	return 0;
3148a978234SLiam Girdwood }
3158a978234SLiam Girdwood 
316acfc7d46SMengdong Lin /* pass link configurations to component driver for extra intialization */
317acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
318acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link)
319acfc7d46SMengdong Lin {
320acfc7d46SMengdong Lin 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
321acfc7d46SMengdong Lin 		return tplg->ops->link_load(tplg->comp, link);
322acfc7d46SMengdong Lin 
323acfc7d46SMengdong Lin 	return 0;
324acfc7d46SMengdong Lin }
325acfc7d46SMengdong Lin 
3268a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3278a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3288a978234SLiam Girdwood {
3298a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3308a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3318a978234SLiam Girdwood }
3328a978234SLiam Girdwood 
3338a978234SLiam Girdwood /* add a dynamic kcontrol */
3348a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3358a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3368a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3378a978234SLiam Girdwood {
3388a978234SLiam Girdwood 	int err;
3398a978234SLiam Girdwood 
3408a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3418a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3428a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3438a978234SLiam Girdwood 		control_new->name);
3448a978234SLiam Girdwood 		return -ENOMEM;
3458a978234SLiam Girdwood 	}
3468a978234SLiam Girdwood 
3478a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3488a978234SLiam Girdwood 	if (err < 0) {
3498a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3508a978234SLiam Girdwood 			control_new->name, err);
3518a978234SLiam Girdwood 		return err;
3528a978234SLiam Girdwood 	}
3538a978234SLiam Girdwood 
3548a978234SLiam Girdwood 	return 0;
3558a978234SLiam Girdwood }
3568a978234SLiam Girdwood 
3578a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3588a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3598a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3608a978234SLiam Girdwood {
3618a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3628a978234SLiam Girdwood 
3638a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3648a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3658a978234SLiam Girdwood }
3668a978234SLiam Girdwood 
3678a978234SLiam Girdwood /* remove a mixer kcontrol */
3688a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3698a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3708a978234SLiam Girdwood {
3718a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3728a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3738a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3748a978234SLiam Girdwood 	const unsigned int *p = NULL;
3758a978234SLiam Girdwood 
3768a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3778a978234SLiam Girdwood 		return;
3788a978234SLiam Girdwood 
3798a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3808a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3818a978234SLiam Girdwood 
3828a978234SLiam Girdwood 	if (sm->dobj.control.kcontrol->tlv.p)
3838a978234SLiam Girdwood 		p = sm->dobj.control.kcontrol->tlv.p;
3848a978234SLiam Girdwood 	snd_ctl_remove(card, sm->dobj.control.kcontrol);
3858a978234SLiam Girdwood 	list_del(&sm->dobj.list);
3868a978234SLiam Girdwood 	kfree(sm);
3878a978234SLiam Girdwood 	kfree(p);
3888a978234SLiam Girdwood }
3898a978234SLiam Girdwood 
3908a978234SLiam Girdwood /* remove an enum kcontrol */
3918a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
3928a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3938a978234SLiam Girdwood {
3948a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3958a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
3968a978234SLiam Girdwood 	int i;
3978a978234SLiam Girdwood 
3988a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3998a978234SLiam Girdwood 		return;
4008a978234SLiam Girdwood 
4018a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4028a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4038a978234SLiam Girdwood 
4048a978234SLiam Girdwood 	snd_ctl_remove(card, se->dobj.control.kcontrol);
4058a978234SLiam Girdwood 	list_del(&se->dobj.list);
4068a978234SLiam Girdwood 
4078a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
4088a978234SLiam Girdwood 	for (i = 0; i < se->items; i++)
4098a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
4108a978234SLiam Girdwood 	kfree(se);
4118a978234SLiam Girdwood }
4128a978234SLiam Girdwood 
4138a978234SLiam Girdwood /* remove a byte kcontrol */
4148a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4158a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4168a978234SLiam Girdwood {
4178a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4188a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4198a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4208a978234SLiam Girdwood 
4218a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4228a978234SLiam Girdwood 		return;
4238a978234SLiam Girdwood 
4248a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4258a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4268a978234SLiam Girdwood 
4278a978234SLiam Girdwood 	snd_ctl_remove(card, sb->dobj.control.kcontrol);
4288a978234SLiam Girdwood 	list_del(&sb->dobj.list);
4298a978234SLiam Girdwood 	kfree(sb);
4308a978234SLiam Girdwood }
4318a978234SLiam Girdwood 
4328a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4338a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4348a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4358a978234SLiam Girdwood {
4368a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4378a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4388a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4398a978234SLiam Girdwood 	int i;
4408a978234SLiam Girdwood 
4418a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4428a978234SLiam Girdwood 		return;
4438a978234SLiam Girdwood 
4448a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4458a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4468a978234SLiam Girdwood 
4478a978234SLiam Girdwood 	/*
4488a978234SLiam Girdwood 	 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
4498a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4508a978234SLiam Girdwood 	 */
4518a978234SLiam Girdwood 	if (dobj->widget.kcontrol_enum) {
4528a978234SLiam Girdwood 		/* enumerated widget mixer */
4538a978234SLiam Girdwood 		struct soc_enum *se =
4548a978234SLiam Girdwood 			(struct soc_enum *)w->kcontrols[0]->private_value;
4558a978234SLiam Girdwood 
4568a978234SLiam Girdwood 		snd_ctl_remove(card, w->kcontrols[0]);
4578a978234SLiam Girdwood 
4588a978234SLiam Girdwood 		kfree(se->dobj.control.dvalues);
4598a978234SLiam Girdwood 		for (i = 0; i < se->items; i++)
4608a978234SLiam Girdwood 			kfree(se->dobj.control.dtexts[i]);
4618a978234SLiam Girdwood 
4628a978234SLiam Girdwood 		kfree(se);
4638a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4648a978234SLiam Girdwood 	} else {
4658a978234SLiam Girdwood 		/* non enumerated widget mixer */
4668a978234SLiam Girdwood 		for (i = 0; i < w->num_kcontrols; i++) {
4678a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4688a978234SLiam Girdwood 			struct soc_mixer_control *sm =
4698a978234SLiam Girdwood 			(struct soc_mixer_control *) kcontrol->private_value;
4708a978234SLiam Girdwood 
4718a978234SLiam Girdwood 			kfree(w->kcontrols[i]->tlv.p);
4728a978234SLiam Girdwood 
4738a978234SLiam Girdwood 			snd_ctl_remove(card, w->kcontrols[i]);
4748a978234SLiam Girdwood 			kfree(sm);
4758a978234SLiam Girdwood 		}
4768a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4778a978234SLiam Girdwood 	}
4788a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
4798a978234SLiam Girdwood }
4808a978234SLiam Girdwood 
48164527e8aSMengdong Lin /* remove DAI configurations */
48264527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp,
4838a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4848a978234SLiam Girdwood {
48564527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv =
48664527e8aSMengdong Lin 		container_of(dobj, struct snd_soc_dai_driver, dobj);
48764527e8aSMengdong Lin 
4888a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
4898a978234SLiam Girdwood 		return;
4908a978234SLiam Girdwood 
49164527e8aSMengdong Lin 	if (dobj->ops && dobj->ops->dai_unload)
49264527e8aSMengdong Lin 		dobj->ops->dai_unload(comp, dobj);
4938a978234SLiam Girdwood 
4948a978234SLiam Girdwood 	list_del(&dobj->list);
49564527e8aSMengdong Lin 	kfree(dai_drv);
4968a978234SLiam Girdwood }
4978a978234SLiam Girdwood 
498acfc7d46SMengdong Lin /* remove link configurations */
499acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp,
500acfc7d46SMengdong Lin 	struct snd_soc_dobj *dobj, int pass)
501acfc7d46SMengdong Lin {
502acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link =
503acfc7d46SMengdong Lin 		container_of(dobj, struct snd_soc_dai_link, dobj);
504acfc7d46SMengdong Lin 
505acfc7d46SMengdong Lin 	if (pass != SOC_TPLG_PASS_PCM_DAI)
506acfc7d46SMengdong Lin 		return;
507acfc7d46SMengdong Lin 
508acfc7d46SMengdong Lin 	if (dobj->ops && dobj->ops->link_unload)
509acfc7d46SMengdong Lin 		dobj->ops->link_unload(comp, dobj);
510acfc7d46SMengdong Lin 
511acfc7d46SMengdong Lin 	list_del(&dobj->list);
512acfc7d46SMengdong Lin 	snd_soc_remove_dai_link(comp->card, link);
513acfc7d46SMengdong Lin 	kfree(link);
514acfc7d46SMengdong Lin }
515acfc7d46SMengdong Lin 
5168a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5178a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5188a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5192b5cdb91SMengdong Lin 	const struct soc_tplg *tplg)
5208a978234SLiam Girdwood {
5212b5cdb91SMengdong Lin 	const struct snd_soc_tplg_kcontrol_ops *ops;
5221a3232d2SMengdong Lin 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
5232b5cdb91SMengdong Lin 	int num_ops, i;
5248a978234SLiam Girdwood 
5251a3232d2SMengdong Lin 	if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
5261a3232d2SMengdong Lin 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
5271a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
5281a3232d2SMengdong Lin 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
5291a3232d2SMengdong Lin 		struct soc_bytes_ext *sbe;
5301a3232d2SMengdong Lin 		struct snd_soc_tplg_bytes_control *be;
5311a3232d2SMengdong Lin 
5321a3232d2SMengdong Lin 		sbe = (struct soc_bytes_ext *)k->private_value;
5331a3232d2SMengdong Lin 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
5341a3232d2SMengdong Lin 
5351a3232d2SMengdong Lin 		/* TLV bytes controls need standard kcontrol info handler,
5361a3232d2SMengdong Lin 		 * TLV callback and extended put/get handlers.
5371a3232d2SMengdong Lin 		 */
538f4be978bSOmair M Abdullah 		k->info = snd_soc_bytes_info_ext;
5391a3232d2SMengdong Lin 		k->tlv.c = snd_soc_bytes_tlv_callback;
5401a3232d2SMengdong Lin 
5411a3232d2SMengdong Lin 		ext_ops = tplg->bytes_ext_ops;
5421a3232d2SMengdong Lin 		num_ops = tplg->bytes_ext_ops_count;
5431a3232d2SMengdong Lin 		for (i = 0; i < num_ops; i++) {
5441a3232d2SMengdong Lin 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
5451a3232d2SMengdong Lin 				sbe->put = ext_ops[i].put;
5461a3232d2SMengdong Lin 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
5471a3232d2SMengdong Lin 				sbe->get = ext_ops[i].get;
5481a3232d2SMengdong Lin 		}
5491a3232d2SMengdong Lin 
5501a3232d2SMengdong Lin 		if (sbe->put && sbe->get)
5511a3232d2SMengdong Lin 			return 0;
5521a3232d2SMengdong Lin 		else
5531a3232d2SMengdong Lin 			return -EINVAL;
5541a3232d2SMengdong Lin 	}
5551a3232d2SMengdong Lin 
55688a17d8fSMengdong Lin 	/* try and map vendor specific kcontrol handlers first */
5572b5cdb91SMengdong Lin 	ops = tplg->io_ops;
5582b5cdb91SMengdong Lin 	num_ops = tplg->io_ops_count;
5598a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
5608a978234SLiam Girdwood 
5612b5cdb91SMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
5628a978234SLiam Girdwood 			k->put = ops[i].put;
5632b5cdb91SMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
5648a978234SLiam Girdwood 			k->get = ops[i].get;
5652b5cdb91SMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
5662b5cdb91SMengdong Lin 			k->info = ops[i].info;
5678a978234SLiam Girdwood 	}
5688a978234SLiam Girdwood 
56988a17d8fSMengdong Lin 	/* vendor specific handlers found ? */
57088a17d8fSMengdong Lin 	if (k->put && k->get && k->info)
57188a17d8fSMengdong Lin 		return 0;
57288a17d8fSMengdong Lin 
57388a17d8fSMengdong Lin 	/* none found so try standard kcontrol handlers */
5742b5cdb91SMengdong Lin 	ops = io_ops;
5752b5cdb91SMengdong Lin 	num_ops = ARRAY_SIZE(io_ops);
57688a17d8fSMengdong Lin 	for (i = 0; i < num_ops; i++) {
57788a17d8fSMengdong Lin 
57888a17d8fSMengdong Lin 		if (k->put == NULL && ops[i].id == hdr->ops.put)
57988a17d8fSMengdong Lin 			k->put = ops[i].put;
58088a17d8fSMengdong Lin 		if (k->get == NULL && ops[i].id == hdr->ops.get)
58188a17d8fSMengdong Lin 			k->get = ops[i].get;
58288a17d8fSMengdong Lin 		if (k->info == NULL && ops[i].id == hdr->ops.info)
5838a978234SLiam Girdwood 			k->info = ops[i].info;
5848a978234SLiam Girdwood 	}
5858a978234SLiam Girdwood 
5868a978234SLiam Girdwood 	/* standard handlers found ? */
5878a978234SLiam Girdwood 	if (k->put && k->get && k->info)
5888a978234SLiam Girdwood 		return 0;
5898a978234SLiam Girdwood 
5908a978234SLiam Girdwood 	/* nothing to bind */
5918a978234SLiam Girdwood 	return -EINVAL;
5928a978234SLiam Girdwood }
5938a978234SLiam Girdwood 
5948a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
5958a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
5968a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
5978a978234SLiam Girdwood 		int num_events, u16 event_type)
5988a978234SLiam Girdwood {
5998a978234SLiam Girdwood 	int i;
6008a978234SLiam Girdwood 
6018a978234SLiam Girdwood 	w->event = NULL;
6028a978234SLiam Girdwood 
6038a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
6048a978234SLiam Girdwood 		if (event_type == events[i].type) {
6058a978234SLiam Girdwood 
6068a978234SLiam Girdwood 			/* found - so assign event */
6078a978234SLiam Girdwood 			w->event = events[i].event_handler;
6088a978234SLiam Girdwood 			return 0;
6098a978234SLiam Girdwood 		}
6108a978234SLiam Girdwood 	}
6118a978234SLiam Girdwood 
6128a978234SLiam Girdwood 	/* not found */
6138a978234SLiam Girdwood 	return -EINVAL;
6148a978234SLiam Girdwood }
6158a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
6168a978234SLiam Girdwood 
6178a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
6188a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
6198a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
6208a978234SLiam Girdwood {
6218a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
6228a978234SLiam Girdwood 		return tplg->ops->control_load(tplg->comp, k, hdr);
6238a978234SLiam Girdwood 
6248a978234SLiam Girdwood 	return 0;
6258a978234SLiam Girdwood }
6268a978234SLiam Girdwood 
62728a87eebSMengdong Lin 
62828a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
62928a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
6308a978234SLiam Girdwood {
63128a87eebSMengdong Lin 	unsigned int item_len = 2 * sizeof(unsigned int);
63228a87eebSMengdong Lin 	unsigned int *p;
6338a978234SLiam Girdwood 
63428a87eebSMengdong Lin 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
63528a87eebSMengdong Lin 	if (!p)
6368a978234SLiam Girdwood 		return -ENOMEM;
6378a978234SLiam Girdwood 
63828a87eebSMengdong Lin 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
63928a87eebSMengdong Lin 	p[1] = item_len;
64028a87eebSMengdong Lin 	p[2] = scale->min;
64128a87eebSMengdong Lin 	p[3] = (scale->step & TLV_DB_SCALE_MASK)
64228a87eebSMengdong Lin 			| (scale->mute ? TLV_DB_SCALE_MUTE : 0);
6438a978234SLiam Girdwood 
64428a87eebSMengdong Lin 	kc->tlv.p = (void *)p;
64528a87eebSMengdong Lin 	return 0;
64628a87eebSMengdong Lin }
64728a87eebSMengdong Lin 
64828a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg,
64928a87eebSMengdong Lin 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
65028a87eebSMengdong Lin {
65128a87eebSMengdong Lin 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
65228a87eebSMengdong Lin 
65328a87eebSMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
65428a87eebSMengdong Lin 		return 0;
65528a87eebSMengdong Lin 
6561a3232d2SMengdong Lin 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
65728a87eebSMengdong Lin 		tplg_tlv = &tc->tlv;
65828a87eebSMengdong Lin 		switch (tplg_tlv->type) {
65928a87eebSMengdong Lin 		case SNDRV_CTL_TLVT_DB_SCALE:
66028a87eebSMengdong Lin 			return soc_tplg_create_tlv_db_scale(tplg, kc,
66128a87eebSMengdong Lin 					&tplg_tlv->scale);
66228a87eebSMengdong Lin 
66328a87eebSMengdong Lin 		/* TODO: add support for other TLV types */
66428a87eebSMengdong Lin 		default:
66528a87eebSMengdong Lin 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
66628a87eebSMengdong Lin 					tplg_tlv->type);
66728a87eebSMengdong Lin 			return -EINVAL;
66828a87eebSMengdong Lin 		}
66928a87eebSMengdong Lin 	}
6708a978234SLiam Girdwood 
6718a978234SLiam Girdwood 	return 0;
6728a978234SLiam Girdwood }
6738a978234SLiam Girdwood 
6748a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
6758a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
6768a978234SLiam Girdwood {
6778a978234SLiam Girdwood 	kfree(kc->tlv.p);
6788a978234SLiam Girdwood }
6798a978234SLiam Girdwood 
6808a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
6818a978234SLiam Girdwood 	size_t size)
6828a978234SLiam Girdwood {
6838a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
6848a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
6858a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
6868a978234SLiam Girdwood 	int i, err;
6878a978234SLiam Girdwood 
6888a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
6898a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
6908a978234SLiam Girdwood 			size, "mixer bytes")) {
6918a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
6928a978234SLiam Girdwood 			count);
6938a978234SLiam Girdwood 		return -EINVAL;
6948a978234SLiam Girdwood 	}
6958a978234SLiam Girdwood 
6968a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
6978a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
6988a978234SLiam Girdwood 
6998a978234SLiam Girdwood 		/* validate kcontrol */
7008a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7018a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7028a978234SLiam Girdwood 			return -EINVAL;
7038a978234SLiam Girdwood 
7048a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
7058a978234SLiam Girdwood 		if (sbe == NULL)
7068a978234SLiam Girdwood 			return -ENOMEM;
7078a978234SLiam Girdwood 
7088a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
7098a978234SLiam Girdwood 			be->priv.size);
7108a978234SLiam Girdwood 
7118a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7128a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
7138a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
7148a978234SLiam Girdwood 
7158a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7168a978234SLiam Girdwood 		kc.name = be->hdr.name;
7178a978234SLiam Girdwood 		kc.private_value = (long)sbe;
7188a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7198a978234SLiam Girdwood 		kc.access = be->hdr.access;
7208a978234SLiam Girdwood 
7218a978234SLiam Girdwood 		sbe->max = be->max;
7228a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
7238a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
7248a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
7258a978234SLiam Girdwood 
7268a978234SLiam Girdwood 		/* map io handlers */
7272b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
7288a978234SLiam Girdwood 		if (err) {
7298a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
7308a978234SLiam Girdwood 			kfree(sbe);
7318a978234SLiam Girdwood 			continue;
7328a978234SLiam Girdwood 		}
7338a978234SLiam Girdwood 
7348a978234SLiam Girdwood 		/* pass control to driver for optional further init */
7358a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
7368a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
7378a978234SLiam Girdwood 		if (err < 0) {
7388a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
7398a978234SLiam Girdwood 				be->hdr.name);
7408a978234SLiam Girdwood 			kfree(sbe);
7418a978234SLiam Girdwood 			continue;
7428a978234SLiam Girdwood 		}
7438a978234SLiam Girdwood 
7448a978234SLiam Girdwood 		/* register control here */
7458a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
7468a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
7478a978234SLiam Girdwood 		if (err < 0) {
7488a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
7498a978234SLiam Girdwood 				be->hdr.name);
7508a978234SLiam Girdwood 			kfree(sbe);
7518a978234SLiam Girdwood 			continue;
7528a978234SLiam Girdwood 		}
7538a978234SLiam Girdwood 
7548a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
7558a978234SLiam Girdwood 	}
7568a978234SLiam Girdwood 	return 0;
7578a978234SLiam Girdwood 
7588a978234SLiam Girdwood }
7598a978234SLiam Girdwood 
7608a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
7618a978234SLiam Girdwood 	size_t size)
7628a978234SLiam Girdwood {
7638a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
7648a978234SLiam Girdwood 	struct soc_mixer_control *sm;
7658a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7668a978234SLiam Girdwood 	int i, err;
7678a978234SLiam Girdwood 
7688a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7698a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
7708a978234SLiam Girdwood 		count, size, "mixers")) {
7718a978234SLiam Girdwood 
7728a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
7738a978234SLiam Girdwood 			count);
7748a978234SLiam Girdwood 		return -EINVAL;
7758a978234SLiam Girdwood 	}
7768a978234SLiam Girdwood 
7778a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7788a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
7798a978234SLiam Girdwood 
7808a978234SLiam Girdwood 		/* validate kcontrol */
7818a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7828a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7838a978234SLiam Girdwood 			return -EINVAL;
7848a978234SLiam Girdwood 
7858a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
7868a978234SLiam Girdwood 		if (sm == NULL)
7878a978234SLiam Girdwood 			return -ENOMEM;
7888a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
7898a978234SLiam Girdwood 			mc->priv.size);
7908a978234SLiam Girdwood 
7918a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7928a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
7938a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
7948a978234SLiam Girdwood 
7958a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7968a978234SLiam Girdwood 		kc.name = mc->hdr.name;
7978a978234SLiam Girdwood 		kc.private_value = (long)sm;
7988a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7998a978234SLiam Girdwood 		kc.access = mc->hdr.access;
8008a978234SLiam Girdwood 
8018a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
8028a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
8038a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8048a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
8058a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8068a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
8078a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8088a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
8098a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
8108a978234SLiam Girdwood 
8118a978234SLiam Girdwood 		sm->max = mc->max;
8128a978234SLiam Girdwood 		sm->min = mc->min;
8138a978234SLiam Girdwood 		sm->invert = mc->invert;
8148a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
8158a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
8168a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
8178a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
8188a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
8198a978234SLiam Girdwood 
8208a978234SLiam Girdwood 		/* map io handlers */
8212b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
8228a978234SLiam Girdwood 		if (err) {
8238a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
8248a978234SLiam Girdwood 			kfree(sm);
8258a978234SLiam Girdwood 			continue;
8268a978234SLiam Girdwood 		}
8278a978234SLiam Girdwood 
8288a978234SLiam Girdwood 		/* pass control to driver for optional further init */
8298a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
8308a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
8318a978234SLiam Girdwood 		if (err < 0) {
8328a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
8338a978234SLiam Girdwood 				mc->hdr.name);
8348a978234SLiam Girdwood 			kfree(sm);
8358a978234SLiam Girdwood 			continue;
8368a978234SLiam Girdwood 		}
8378a978234SLiam Girdwood 
8388a978234SLiam Girdwood 		/* create any TLV data */
83928a87eebSMengdong Lin 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
8408a978234SLiam Girdwood 
8418a978234SLiam Girdwood 		/* register control here */
8428a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
8438a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
8448a978234SLiam Girdwood 		if (err < 0) {
8458a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
8468a978234SLiam Girdwood 				mc->hdr.name);
8478a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
8488a978234SLiam Girdwood 			kfree(sm);
8498a978234SLiam Girdwood 			continue;
8508a978234SLiam Girdwood 		}
8518a978234SLiam Girdwood 
8528a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
8538a978234SLiam Girdwood 	}
8548a978234SLiam Girdwood 
8558a978234SLiam Girdwood 	return 0;
8568a978234SLiam Girdwood }
8578a978234SLiam Girdwood 
8588a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
8598a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
8608a978234SLiam Girdwood {
8618a978234SLiam Girdwood 	int i, ret;
8628a978234SLiam Girdwood 
8638a978234SLiam Girdwood 	se->dobj.control.dtexts =
8648a978234SLiam Girdwood 		kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
8658a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
8668a978234SLiam Girdwood 		return -ENOMEM;
8678a978234SLiam Girdwood 
8688a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
8698a978234SLiam Girdwood 
8708a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8718a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
8728a978234SLiam Girdwood 			ret = -EINVAL;
8738a978234SLiam Girdwood 			goto err;
8748a978234SLiam Girdwood 		}
8758a978234SLiam Girdwood 
8768a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
8778a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
8788a978234SLiam Girdwood 			ret = -ENOMEM;
8798a978234SLiam Girdwood 			goto err;
8808a978234SLiam Girdwood 		}
8818a978234SLiam Girdwood 	}
8828a978234SLiam Girdwood 
8838a978234SLiam Girdwood 	return 0;
8848a978234SLiam Girdwood 
8858a978234SLiam Girdwood err:
8868a978234SLiam Girdwood 	for (--i; i >= 0; i--)
8878a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
8888a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
8898a978234SLiam Girdwood 	return ret;
8908a978234SLiam Girdwood }
8918a978234SLiam Girdwood 
8928a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
8938a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
8948a978234SLiam Girdwood {
8958a978234SLiam Girdwood 	if (ec->items > sizeof(*ec->values))
8968a978234SLiam Girdwood 		return -EINVAL;
8978a978234SLiam Girdwood 
898376c0afeSAndrzej Hajda 	se->dobj.control.dvalues = kmemdup(ec->values,
899376c0afeSAndrzej Hajda 					   ec->items * sizeof(u32),
900376c0afeSAndrzej Hajda 					   GFP_KERNEL);
9018a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
9028a978234SLiam Girdwood 		return -ENOMEM;
9038a978234SLiam Girdwood 
9048a978234SLiam Girdwood 	return 0;
9058a978234SLiam Girdwood }
9068a978234SLiam Girdwood 
9078a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
9088a978234SLiam Girdwood 	size_t size)
9098a978234SLiam Girdwood {
9108a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
9118a978234SLiam Girdwood 	struct soc_enum *se;
9128a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
9138a978234SLiam Girdwood 	int i, ret, err;
9148a978234SLiam Girdwood 
9158a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
9168a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
9178a978234SLiam Girdwood 		count, size, "enums")) {
9188a978234SLiam Girdwood 
9198a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
9208a978234SLiam Girdwood 			count);
9218a978234SLiam Girdwood 		return -EINVAL;
9228a978234SLiam Girdwood 	}
9238a978234SLiam Girdwood 
9248a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
9258a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
9268a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
9278a978234SLiam Girdwood 			ec->priv.size);
9288a978234SLiam Girdwood 
9298a978234SLiam Girdwood 		/* validate kcontrol */
9308a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
9318a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
9328a978234SLiam Girdwood 			return -EINVAL;
9338a978234SLiam Girdwood 
9348a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
9358a978234SLiam Girdwood 		if (se == NULL)
9368a978234SLiam Girdwood 			return -ENOMEM;
9378a978234SLiam Girdwood 
9388a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
9398a978234SLiam Girdwood 			ec->hdr.name, ec->items);
9408a978234SLiam Girdwood 
9418a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
9428a978234SLiam Girdwood 		kc.name = ec->hdr.name;
9438a978234SLiam Girdwood 		kc.private_value = (long)se;
9448a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
9458a978234SLiam Girdwood 		kc.access = ec->hdr.access;
9468a978234SLiam Girdwood 
9478a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
9488a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
9498a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
9508a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
9518a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
9528a978234SLiam Girdwood 
9538a978234SLiam Girdwood 		se->items = ec->items;
9548a978234SLiam Girdwood 		se->mask = ec->mask;
9558a978234SLiam Girdwood 		se->dobj.index = tplg->index;
9568a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
9578a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
9588a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
9598a978234SLiam Girdwood 
9608a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
9618a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9628a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9638a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
9648a978234SLiam Girdwood 			if (err < 0) {
9658a978234SLiam Girdwood 				dev_err(tplg->dev,
9668a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
9678a978234SLiam Girdwood 					ec->hdr.name);
9688a978234SLiam Girdwood 				kfree(se);
9698a978234SLiam Girdwood 				continue;
9708a978234SLiam Girdwood 			}
9718a978234SLiam Girdwood 			/* fall through and create texts */
9728a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9738a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9748a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9758a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
9768a978234SLiam Girdwood 			if (err < 0) {
9778a978234SLiam Girdwood 				dev_err(tplg->dev,
9788a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
9798a978234SLiam Girdwood 					ec->hdr.name);
9808a978234SLiam Girdwood 				kfree(se);
9818a978234SLiam Girdwood 				continue;
9828a978234SLiam Girdwood 			}
9838a978234SLiam Girdwood 			break;
9848a978234SLiam Girdwood 		default:
9858a978234SLiam Girdwood 			dev_err(tplg->dev,
9868a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
9878a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
9888a978234SLiam Girdwood 			kfree(se);
9898a978234SLiam Girdwood 			continue;
9908a978234SLiam Girdwood 		}
9918a978234SLiam Girdwood 
9928a978234SLiam Girdwood 		/* map io handlers */
9932b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
9948a978234SLiam Girdwood 		if (err) {
9958a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
9968a978234SLiam Girdwood 			kfree(se);
9978a978234SLiam Girdwood 			continue;
9988a978234SLiam Girdwood 		}
9998a978234SLiam Girdwood 
10008a978234SLiam Girdwood 		/* pass control to driver for optional further init */
10018a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
10028a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
10038a978234SLiam Girdwood 		if (err < 0) {
10048a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
10058a978234SLiam Girdwood 				ec->hdr.name);
10068a978234SLiam Girdwood 			kfree(se);
10078a978234SLiam Girdwood 			continue;
10088a978234SLiam Girdwood 		}
10098a978234SLiam Girdwood 
10108a978234SLiam Girdwood 		/* register control here */
10118a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
10128a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
10138a978234SLiam Girdwood 		if (ret < 0) {
10148a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
10158a978234SLiam Girdwood 				ec->hdr.name);
10168a978234SLiam Girdwood 			kfree(se);
10178a978234SLiam Girdwood 			continue;
10188a978234SLiam Girdwood 		}
10198a978234SLiam Girdwood 
10208a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
10218a978234SLiam Girdwood 	}
10228a978234SLiam Girdwood 
10238a978234SLiam Girdwood 	return 0;
10248a978234SLiam Girdwood }
10258a978234SLiam Girdwood 
10268a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
10278a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10288a978234SLiam Girdwood {
10298a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
10308a978234SLiam Girdwood 	int i;
10318a978234SLiam Girdwood 
10328a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
10338a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10348a978234SLiam Girdwood 		return 0;
10358a978234SLiam Girdwood 	}
10368a978234SLiam Girdwood 
10378a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
10388a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
10398a978234SLiam Girdwood 
10408a978234SLiam Girdwood 	for (i = 0; i < hdr->count; i++) {
10418a978234SLiam Girdwood 
10428a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
10438a978234SLiam Girdwood 
104406eb49f7SMengdong Lin 		if (control_hdr->size != sizeof(*control_hdr)) {
104506eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid control size\n");
104606eb49f7SMengdong Lin 			return -EINVAL;
104706eb49f7SMengdong Lin 		}
104806eb49f7SMengdong Lin 
10498a978234SLiam Girdwood 		switch (control_hdr->ops.info) {
10508a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
10518a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
10528a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
10538a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
10548a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
10558a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
10568a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
10578a978234SLiam Girdwood 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
10588a978234SLiam Girdwood 			break;
10598a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
10608a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
10618a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
10628a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
10638a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
10648a978234SLiam Girdwood 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
10658a978234SLiam Girdwood 			break;
10668a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
10678a978234SLiam Girdwood 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
10688a978234SLiam Girdwood 			break;
10698a978234SLiam Girdwood 		default:
10708a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
10718a978234SLiam Girdwood 			return -EINVAL;
10728a978234SLiam Girdwood 		}
10738a978234SLiam Girdwood 	}
10748a978234SLiam Girdwood 
10758a978234SLiam Girdwood 	return 0;
10768a978234SLiam Girdwood }
10778a978234SLiam Girdwood 
10788a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
10798a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10808a978234SLiam Girdwood {
10818a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
10828a978234SLiam Girdwood 	struct snd_soc_dapm_route route;
10838a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
10848a978234SLiam Girdwood 	int count = hdr->count, i;
10858a978234SLiam Girdwood 
10868a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
10878a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10888a978234SLiam Girdwood 		return 0;
10898a978234SLiam Girdwood 	}
10908a978234SLiam Girdwood 
10918a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10928a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
10938a978234SLiam Girdwood 		count, hdr->payload_size, "graph")) {
10948a978234SLiam Girdwood 
10958a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
10968a978234SLiam Girdwood 			count);
10978a978234SLiam Girdwood 		return -EINVAL;
10988a978234SLiam Girdwood 	}
10998a978234SLiam Girdwood 
11008a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
11018a978234SLiam Girdwood 
11028a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
11038a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
11048a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
11058a978234SLiam Girdwood 
11068a978234SLiam Girdwood 		/* validate routes */
11078a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11088a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11098a978234SLiam Girdwood 			return -EINVAL;
11108a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11118a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11128a978234SLiam Girdwood 			return -EINVAL;
11138a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11148a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11158a978234SLiam Girdwood 			return -EINVAL;
11168a978234SLiam Girdwood 
11178a978234SLiam Girdwood 		route.source = elem->source;
11188a978234SLiam Girdwood 		route.sink = elem->sink;
11198a978234SLiam Girdwood 		route.connected = NULL; /* set to NULL atm for tplg users */
11208a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
11218a978234SLiam Girdwood 			route.control = NULL;
11228a978234SLiam Girdwood 		else
11238a978234SLiam Girdwood 			route.control = elem->control;
11248a978234SLiam Girdwood 
11258a978234SLiam Girdwood 		/* add route, but keep going if some fail */
11268a978234SLiam Girdwood 		snd_soc_dapm_add_routes(dapm, &route, 1);
11278a978234SLiam Girdwood 	}
11288a978234SLiam Girdwood 
11298a978234SLiam Girdwood 	return 0;
11308a978234SLiam Girdwood }
11318a978234SLiam Girdwood 
11328a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
11338a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
11348a978234SLiam Girdwood {
11358a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
11368a978234SLiam Girdwood 	struct soc_mixer_control *sm;
11378a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
11388a978234SLiam Girdwood 	int i, err;
11398a978234SLiam Girdwood 
11404ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
11418a978234SLiam Girdwood 	if (kc == NULL)
11428a978234SLiam Girdwood 		return NULL;
11438a978234SLiam Girdwood 
11448a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
11458a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
11468a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
11478a978234SLiam Girdwood 		if (sm == NULL)
11488a978234SLiam Girdwood 			goto err;
11498a978234SLiam Girdwood 
11508a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
11518a978234SLiam Girdwood 			mc->priv.size);
11528a978234SLiam Girdwood 
11538a978234SLiam Girdwood 		/* validate kcontrol */
11548a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11558a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11568a978234SLiam Girdwood 			goto err_str;
11578a978234SLiam Girdwood 
11588a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
11598a978234SLiam Girdwood 			mc->hdr.name, i);
11608a978234SLiam Girdwood 
11618a978234SLiam Girdwood 		kc[i].name = mc->hdr.name;
11628a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
11638a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
11648a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
11658a978234SLiam Girdwood 
11668a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
11678a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
11688a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11698a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
11708a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11718a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
11728a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11738a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
11748a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11758a978234SLiam Girdwood 
11768a978234SLiam Girdwood 		sm->max = mc->max;
11778a978234SLiam Girdwood 		sm->min = mc->min;
11788a978234SLiam Girdwood 		sm->invert = mc->invert;
11798a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
11808a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
11818a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
11828a978234SLiam Girdwood 
11838a978234SLiam Girdwood 		/* map io handlers */
11842b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
11858a978234SLiam Girdwood 		if (err) {
11868a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
11878a978234SLiam Girdwood 			kfree(sm);
11888a978234SLiam Girdwood 			continue;
11898a978234SLiam Girdwood 		}
11908a978234SLiam Girdwood 
11918a978234SLiam Girdwood 		/* pass control to driver for optional further init */
11928a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
11938a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
11948a978234SLiam Girdwood 		if (err < 0) {
11958a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
11968a978234SLiam Girdwood 				mc->hdr.name);
11978a978234SLiam Girdwood 			kfree(sm);
11988a978234SLiam Girdwood 			continue;
11998a978234SLiam Girdwood 		}
12008a978234SLiam Girdwood 	}
12018a978234SLiam Girdwood 	return kc;
12028a978234SLiam Girdwood 
12038a978234SLiam Girdwood err_str:
12048a978234SLiam Girdwood 	kfree(sm);
12058a978234SLiam Girdwood err:
12068a978234SLiam Girdwood 	for (--i; i >= 0; i--)
12078a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
12088a978234SLiam Girdwood 	kfree(kc);
12098a978234SLiam Girdwood 	return NULL;
12108a978234SLiam Girdwood }
12118a978234SLiam Girdwood 
12128a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
12138a978234SLiam Girdwood 	struct soc_tplg *tplg)
12148a978234SLiam Girdwood {
12158a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
12168a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
12178a978234SLiam Girdwood 	struct soc_enum *se;
12188a978234SLiam Girdwood 	int i, err;
12198a978234SLiam Girdwood 
12208a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
12218a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
12228a978234SLiam Girdwood 		ec->priv.size);
12238a978234SLiam Girdwood 
12248a978234SLiam Girdwood 	/* validate kcontrol */
12258a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12268a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
12278a978234SLiam Girdwood 		return NULL;
12288a978234SLiam Girdwood 
12298a978234SLiam Girdwood 	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
12308a978234SLiam Girdwood 	if (kc == NULL)
12318a978234SLiam Girdwood 		return NULL;
12328a978234SLiam Girdwood 
12338a978234SLiam Girdwood 	se = kzalloc(sizeof(*se), GFP_KERNEL);
12348a978234SLiam Girdwood 	if (se == NULL)
12358a978234SLiam Girdwood 		goto err;
12368a978234SLiam Girdwood 
12378a978234SLiam Girdwood 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
12388a978234SLiam Girdwood 		ec->hdr.name);
12398a978234SLiam Girdwood 
12408a978234SLiam Girdwood 	kc->name = ec->hdr.name;
12418a978234SLiam Girdwood 	kc->private_value = (long)se;
12428a978234SLiam Girdwood 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
12438a978234SLiam Girdwood 	kc->access = ec->hdr.access;
12448a978234SLiam Girdwood 
12458a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
12468a978234SLiam Girdwood 	se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
12478a978234SLiam Girdwood 	se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
12488a978234SLiam Girdwood 	se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
12498a978234SLiam Girdwood 
12508a978234SLiam Girdwood 	se->items = ec->items;
12518a978234SLiam Girdwood 	se->mask = ec->mask;
12528a978234SLiam Girdwood 	se->dobj.index = tplg->index;
12538a978234SLiam Girdwood 
12548a978234SLiam Girdwood 	switch (ec->hdr.ops.info) {
12558a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
12568a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
12578a978234SLiam Girdwood 		err = soc_tplg_denum_create_values(se, ec);
12588a978234SLiam Girdwood 		if (err < 0) {
12598a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
12608a978234SLiam Girdwood 				ec->hdr.name);
12618a978234SLiam Girdwood 			goto err_se;
12628a978234SLiam Girdwood 		}
12638a978234SLiam Girdwood 		/* fall through to create texts */
12648a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
12658a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
12668a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
12678a978234SLiam Girdwood 		err = soc_tplg_denum_create_texts(se, ec);
12688a978234SLiam Girdwood 		if (err < 0) {
12698a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
12708a978234SLiam Girdwood 				ec->hdr.name);
12718a978234SLiam Girdwood 			goto err_se;
12728a978234SLiam Girdwood 		}
12738a978234SLiam Girdwood 		break;
12748a978234SLiam Girdwood 	default:
12758a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
12768a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
12778a978234SLiam Girdwood 		goto err_se;
12788a978234SLiam Girdwood 	}
12798a978234SLiam Girdwood 
12808a978234SLiam Girdwood 	/* map io handlers */
12812b5cdb91SMengdong Lin 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
12828a978234SLiam Girdwood 	if (err) {
12838a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
12848a978234SLiam Girdwood 		goto err_se;
12858a978234SLiam Girdwood 	}
12868a978234SLiam Girdwood 
12878a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12888a978234SLiam Girdwood 	err = soc_tplg_init_kcontrol(tplg, kc,
12898a978234SLiam Girdwood 		(struct snd_soc_tplg_ctl_hdr *)ec);
12908a978234SLiam Girdwood 	if (err < 0) {
12918a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
12928a978234SLiam Girdwood 			ec->hdr.name);
12938a978234SLiam Girdwood 		goto err_se;
12948a978234SLiam Girdwood 	}
12958a978234SLiam Girdwood 
12968a978234SLiam Girdwood 	return kc;
12978a978234SLiam Girdwood 
12988a978234SLiam Girdwood err_se:
12998a978234SLiam Girdwood 	/* free values and texts */
13008a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
13018a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++)
13028a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
13038a978234SLiam Girdwood 
13048a978234SLiam Girdwood 	kfree(se);
13058a978234SLiam Girdwood err:
13068a978234SLiam Girdwood 	kfree(kc);
13078a978234SLiam Girdwood 
13088a978234SLiam Girdwood 	return NULL;
13098a978234SLiam Girdwood }
13108a978234SLiam Girdwood 
13118a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
13128a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
13138a978234SLiam Girdwood {
13148a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
13158a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
13168a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
13178a978234SLiam Girdwood 	int i, err;
13188a978234SLiam Girdwood 
13194ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
13208a978234SLiam Girdwood 	if (!kc)
13218a978234SLiam Girdwood 		return NULL;
13228a978234SLiam Girdwood 
13238a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
13248a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
13258a978234SLiam Girdwood 
13268a978234SLiam Girdwood 		/* validate kcontrol */
13278a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13288a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13298a978234SLiam Girdwood 			goto err;
13308a978234SLiam Girdwood 
13318a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
13328a978234SLiam Girdwood 		if (sbe == NULL)
13338a978234SLiam Girdwood 			goto err;
13348a978234SLiam Girdwood 
13358a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
13368a978234SLiam Girdwood 			be->priv.size);
13378a978234SLiam Girdwood 
13388a978234SLiam Girdwood 		dev_dbg(tplg->dev,
13398a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
13408a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
13418a978234SLiam Girdwood 
13428a978234SLiam Girdwood 		kc[i].name = be->hdr.name;
13438a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
13448a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
13458a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
13468a978234SLiam Girdwood 
13478a978234SLiam Girdwood 		sbe->max = be->max;
13488a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
13498a978234SLiam Girdwood 
13508a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
13512b5cdb91SMengdong Lin 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
13528a978234SLiam Girdwood 		if (err) {
13538a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
13548a978234SLiam Girdwood 			kfree(sbe);
13558a978234SLiam Girdwood 			continue;
13568a978234SLiam Girdwood 		}
13578a978234SLiam Girdwood 
13588a978234SLiam Girdwood 		/* pass control to driver for optional further init */
13598a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
13608a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
13618a978234SLiam Girdwood 		if (err < 0) {
13628a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13638a978234SLiam Girdwood 				be->hdr.name);
13648a978234SLiam Girdwood 			kfree(sbe);
13658a978234SLiam Girdwood 			continue;
13668a978234SLiam Girdwood 		}
13678a978234SLiam Girdwood 	}
13688a978234SLiam Girdwood 
13698a978234SLiam Girdwood 	return kc;
13708a978234SLiam Girdwood 
13718a978234SLiam Girdwood err:
13728a978234SLiam Girdwood 	for (--i; i >= 0; i--)
13738a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
13748a978234SLiam Girdwood 
13758a978234SLiam Girdwood 	kfree(kc);
13768a978234SLiam Girdwood 	return NULL;
13778a978234SLiam Girdwood }
13788a978234SLiam Girdwood 
13798a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
13808a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
13818a978234SLiam Girdwood {
13828a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
13838a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
13848a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
13858a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
13868a978234SLiam Girdwood 	int ret = 0;
13878a978234SLiam Girdwood 
13888a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13898a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13908a978234SLiam Girdwood 		return -EINVAL;
13918a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13928a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13938a978234SLiam Girdwood 		return -EINVAL;
13948a978234SLiam Girdwood 
13958a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
13968a978234SLiam Girdwood 		w->name, w->id);
13978a978234SLiam Girdwood 
13988a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
13998a978234SLiam Girdwood 
14008a978234SLiam Girdwood 	/* map user to kernel widget ID */
14018a978234SLiam Girdwood 	template.id = get_widget_id(w->id);
14028a978234SLiam Girdwood 	if (template.id < 0)
14038a978234SLiam Girdwood 		return template.id;
14048a978234SLiam Girdwood 
14058a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
14068a978234SLiam Girdwood 	if (!template.name)
14078a978234SLiam Girdwood 		return -ENOMEM;
14088a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
14098a978234SLiam Girdwood 	if (!template.sname) {
14108a978234SLiam Girdwood 		ret = -ENOMEM;
14118a978234SLiam Girdwood 		goto err;
14128a978234SLiam Girdwood 	}
14138a978234SLiam Girdwood 	template.reg = w->reg;
14148a978234SLiam Girdwood 	template.shift = w->shift;
14158a978234SLiam Girdwood 	template.mask = w->mask;
14166dc6db79SSubhransu S. Prusty 	template.subseq = w->subseq;
14178a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
14188a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
14198a978234SLiam Girdwood 	template.ignore_suspend = w->ignore_suspend;
14208a978234SLiam Girdwood 	template.event_flags = w->event_flags;
14218a978234SLiam Girdwood 	template.dobj.index = tplg->index;
14228a978234SLiam Girdwood 
14238a978234SLiam Girdwood 	tplg->pos +=
14248a978234SLiam Girdwood 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
14258a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
14268a978234SLiam Girdwood 		template.num_kcontrols = 0;
14278a978234SLiam Girdwood 		goto widget;
14288a978234SLiam Girdwood 	}
14298a978234SLiam Girdwood 
14308a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
14318a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
14328a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
14338a978234SLiam Girdwood 
14348a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
14358a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
14368a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
14378a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
14388a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
14398a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
14408a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
14418a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
14428a978234SLiam Girdwood 		template.kcontrol_news =
14438a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
14448a978234SLiam Girdwood 			template.num_kcontrols);
14458a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14468a978234SLiam Girdwood 			ret = -ENOMEM;
14478a978234SLiam Girdwood 			goto hdr_err;
14488a978234SLiam Girdwood 		}
14498a978234SLiam Girdwood 		break;
14508a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
14518a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
14528a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
14538a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
14548a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
14558a978234SLiam Girdwood 		template.dobj.widget.kcontrol_enum = 1;
14568a978234SLiam Girdwood 		template.num_kcontrols = 1;
14578a978234SLiam Girdwood 		template.kcontrol_news =
14588a978234SLiam Girdwood 			soc_tplg_dapm_widget_denum_create(tplg);
14598a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14608a978234SLiam Girdwood 			ret = -ENOMEM;
14618a978234SLiam Girdwood 			goto hdr_err;
14628a978234SLiam Girdwood 		}
14638a978234SLiam Girdwood 		break;
14648a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
14658a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
14668a978234SLiam Girdwood 		template.kcontrol_news =
14678a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
14688a978234SLiam Girdwood 				template.num_kcontrols);
14698a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14708a978234SLiam Girdwood 			ret = -ENOMEM;
14718a978234SLiam Girdwood 			goto hdr_err;
14728a978234SLiam Girdwood 		}
14738a978234SLiam Girdwood 		break;
14748a978234SLiam Girdwood 	default:
14758a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
14768a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
14778a978234SLiam Girdwood 			control_hdr->ops.info);
14788a978234SLiam Girdwood 		ret = -EINVAL;
14798a978234SLiam Girdwood 		goto hdr_err;
14808a978234SLiam Girdwood 	}
14818a978234SLiam Girdwood 
14828a978234SLiam Girdwood widget:
14838a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
14848a978234SLiam Girdwood 	if (ret < 0)
14858a978234SLiam Girdwood 		goto hdr_err;
14868a978234SLiam Girdwood 
14878a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
14888a978234SLiam Girdwood 	 * data during sound card init. */
14898a978234SLiam Girdwood 	if (card->instantiated)
14908a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
14918a978234SLiam Girdwood 	else
14928a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
14938a978234SLiam Girdwood 	if (widget == NULL) {
14948a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
14958a978234SLiam Girdwood 			w->name);
14968ae3ea48SWei Yongjun 		ret = -ENOMEM;
14978a978234SLiam Girdwood 		goto hdr_err;
14988a978234SLiam Girdwood 	}
14998a978234SLiam Girdwood 
15008a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
15018a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
15028a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
15038ea41674SJeeja KP 	kfree(template.sname);
15048ea41674SJeeja KP 	kfree(template.name);
15058a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
15068a978234SLiam Girdwood 	return 0;
15078a978234SLiam Girdwood 
15088a978234SLiam Girdwood hdr_err:
15098a978234SLiam Girdwood 	kfree(template.sname);
15108a978234SLiam Girdwood err:
15118a978234SLiam Girdwood 	kfree(template.name);
15128a978234SLiam Girdwood 	return ret;
15138a978234SLiam Girdwood }
15148a978234SLiam Girdwood 
15158a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
15168a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
15178a978234SLiam Girdwood {
15188a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
15198a978234SLiam Girdwood 	int ret, count = hdr->count, i;
15208a978234SLiam Girdwood 
15218a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
15228a978234SLiam Girdwood 		return 0;
15238a978234SLiam Girdwood 
15248a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
15258a978234SLiam Girdwood 
15268a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
15278a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
152806eb49f7SMengdong Lin 		if (widget->size != sizeof(*widget)) {
152906eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
153006eb49f7SMengdong Lin 			return -EINVAL;
153106eb49f7SMengdong Lin 		}
153206eb49f7SMengdong Lin 
15338a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
15347de76b62SMengdong Lin 		if (ret < 0) {
15358a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
15368a978234SLiam Girdwood 				widget->name);
15377de76b62SMengdong Lin 			return ret;
15387de76b62SMengdong Lin 		}
15398a978234SLiam Girdwood 	}
15408a978234SLiam Girdwood 
15418a978234SLiam Girdwood 	return 0;
15428a978234SLiam Girdwood }
15438a978234SLiam Girdwood 
15448a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
15458a978234SLiam Girdwood {
15468a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
15478a978234SLiam Girdwood 	int ret;
15488a978234SLiam Girdwood 
15498a978234SLiam Girdwood 	/* Card might not have been registered at this point.
15508a978234SLiam Girdwood 	 * If so, just return success.
15518a978234SLiam Girdwood 	*/
15528a978234SLiam Girdwood 	if (!card || !card->instantiated) {
15538a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
15548a978234SLiam Girdwood 				"Do not add new widgets now\n");
15558a978234SLiam Girdwood 		return 0;
15568a978234SLiam Girdwood 	}
15578a978234SLiam Girdwood 
15588a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
15598a978234SLiam Girdwood 	if (ret < 0)
15608a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
15618a978234SLiam Girdwood 			ret);
15628a978234SLiam Girdwood 
15638a978234SLiam Girdwood 	return 0;
15648a978234SLiam Girdwood }
15658a978234SLiam Girdwood 
1566b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream,
1567b6b6e4d6SMengdong Lin 	struct snd_soc_tplg_stream_caps *caps)
1568b6b6e4d6SMengdong Lin {
1569b6b6e4d6SMengdong Lin 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1570b6b6e4d6SMengdong Lin 	stream->channels_min = caps->channels_min;
1571b6b6e4d6SMengdong Lin 	stream->channels_max = caps->channels_max;
1572b6b6e4d6SMengdong Lin 	stream->rates = caps->rates;
1573b6b6e4d6SMengdong Lin 	stream->rate_min = caps->rate_min;
1574b6b6e4d6SMengdong Lin 	stream->rate_max = caps->rate_max;
1575b6b6e4d6SMengdong Lin 	stream->formats = caps->formats;
1576f918e169SMengdong Lin 	stream->sig_bits = caps->sig_bits;
1577b6b6e4d6SMengdong Lin }
1578b6b6e4d6SMengdong Lin 
15790038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
15800038be9aSMengdong Lin 			  unsigned int flag_mask, unsigned int flags)
15810038be9aSMengdong Lin {
15820038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
15830038be9aSMengdong Lin 		dai_drv->symmetric_rates =
15840038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
15850038be9aSMengdong Lin 
15860038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
15870038be9aSMengdong Lin 		dai_drv->symmetric_channels =
15880038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
15890038be9aSMengdong Lin 			1 : 0;
15900038be9aSMengdong Lin 
15910038be9aSMengdong Lin 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
15920038be9aSMengdong Lin 		dai_drv->symmetric_samplebits =
15930038be9aSMengdong Lin 			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
15940038be9aSMengdong Lin 			1 : 0;
15950038be9aSMengdong Lin }
15960038be9aSMengdong Lin 
159764527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg,
159864527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
159964527e8aSMengdong Lin {
160064527e8aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
160164527e8aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
160264527e8aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
160364527e8aSMengdong Lin 	int ret;
160464527e8aSMengdong Lin 
160564527e8aSMengdong Lin 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
160664527e8aSMengdong Lin 	if (dai_drv == NULL)
160764527e8aSMengdong Lin 		return -ENOMEM;
160864527e8aSMengdong Lin 
160964527e8aSMengdong Lin 	dai_drv->name = pcm->dai_name;
161064527e8aSMengdong Lin 	dai_drv->id = pcm->dai_id;
161164527e8aSMengdong Lin 
161264527e8aSMengdong Lin 	if (pcm->playback) {
161364527e8aSMengdong Lin 		stream = &dai_drv->playback;
161464527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1615b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
161664527e8aSMengdong Lin 	}
161764527e8aSMengdong Lin 
161864527e8aSMengdong Lin 	if (pcm->capture) {
161964527e8aSMengdong Lin 		stream = &dai_drv->capture;
162064527e8aSMengdong Lin 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1621b6b6e4d6SMengdong Lin 		set_stream_info(stream, caps);
162264527e8aSMengdong Lin 	}
162364527e8aSMengdong Lin 
162464527e8aSMengdong Lin 	/* pass control to component driver for optional further init */
162564527e8aSMengdong Lin 	ret = soc_tplg_dai_load(tplg, dai_drv);
162664527e8aSMengdong Lin 	if (ret < 0) {
162764527e8aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
162864527e8aSMengdong Lin 		kfree(dai_drv);
162964527e8aSMengdong Lin 		return ret;
163064527e8aSMengdong Lin 	}
163164527e8aSMengdong Lin 
163264527e8aSMengdong Lin 	dai_drv->dobj.index = tplg->index;
163364527e8aSMengdong Lin 	dai_drv->dobj.ops = tplg->ops;
163464527e8aSMengdong Lin 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
163564527e8aSMengdong Lin 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
163664527e8aSMengdong Lin 
163764527e8aSMengdong Lin 	/* register the DAI to the component */
163864527e8aSMengdong Lin 	return snd_soc_register_dai(tplg->comp, dai_drv);
163964527e8aSMengdong Lin }
164064527e8aSMengdong Lin 
164167d1c21eSGuneshwor Singh /* create the FE DAI link */
1642acfc7d46SMengdong Lin static int soc_tplg_link_create(struct soc_tplg *tplg,
1643acfc7d46SMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
1644acfc7d46SMengdong Lin {
1645acfc7d46SMengdong Lin 	struct snd_soc_dai_link *link;
1646acfc7d46SMengdong Lin 	int ret;
1647acfc7d46SMengdong Lin 
1648acfc7d46SMengdong Lin 	link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1649acfc7d46SMengdong Lin 	if (link == NULL)
1650acfc7d46SMengdong Lin 		return -ENOMEM;
1651acfc7d46SMengdong Lin 
1652acfc7d46SMengdong Lin 	link->name = pcm->pcm_name;
1653acfc7d46SMengdong Lin 	link->stream_name = pcm->pcm_name;
1654b84fff5aSMengdong Lin 	link->id = pcm->pcm_id;
1655acfc7d46SMengdong Lin 
165667d1c21eSGuneshwor Singh 	link->cpu_dai_name = pcm->dai_name;
165767d1c21eSGuneshwor Singh 	link->codec_name = "snd-soc-dummy";
165867d1c21eSGuneshwor Singh 	link->codec_dai_name = "snd-soc-dummy-dai";
165967d1c21eSGuneshwor Singh 
166067d1c21eSGuneshwor Singh 	/* enable DPCM */
166167d1c21eSGuneshwor Singh 	link->dynamic = 1;
166267d1c21eSGuneshwor Singh 	link->dpcm_playback = pcm->playback;
166367d1c21eSGuneshwor Singh 	link->dpcm_capture = pcm->capture;
166467d1c21eSGuneshwor Singh 
1665acfc7d46SMengdong Lin 	/* pass control to component driver for optional further init */
1666acfc7d46SMengdong Lin 	ret = soc_tplg_dai_link_load(tplg, link);
1667acfc7d46SMengdong Lin 	if (ret < 0) {
1668acfc7d46SMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1669acfc7d46SMengdong Lin 		kfree(link);
1670acfc7d46SMengdong Lin 		return ret;
1671acfc7d46SMengdong Lin 	}
1672acfc7d46SMengdong Lin 
1673acfc7d46SMengdong Lin 	link->dobj.index = tplg->index;
1674acfc7d46SMengdong Lin 	link->dobj.ops = tplg->ops;
1675acfc7d46SMengdong Lin 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1676acfc7d46SMengdong Lin 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1677acfc7d46SMengdong Lin 
1678acfc7d46SMengdong Lin 	snd_soc_add_dai_link(tplg->comp->card, link);
1679acfc7d46SMengdong Lin 	return 0;
1680acfc7d46SMengdong Lin }
1681acfc7d46SMengdong Lin 
1682acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */
168364527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg,
168464527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm)
168564527e8aSMengdong Lin {
1686acfc7d46SMengdong Lin 	int ret;
1687acfc7d46SMengdong Lin 
1688acfc7d46SMengdong Lin 	ret = soc_tplg_dai_create(tplg, pcm);
1689acfc7d46SMengdong Lin 	if (ret < 0)
1690acfc7d46SMengdong Lin 		return ret;
1691acfc7d46SMengdong Lin 
1692acfc7d46SMengdong Lin 	return  soc_tplg_link_create(tplg, pcm);
169364527e8aSMengdong Lin }
169464527e8aSMengdong Lin 
169564527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
16968a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
16978a978234SLiam Girdwood {
169864527e8aSMengdong Lin 	struct snd_soc_tplg_pcm *pcm;
16998a978234SLiam Girdwood 	int count = hdr->count;
170064527e8aSMengdong Lin 	int i;
17018a978234SLiam Girdwood 
17028a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
17038a978234SLiam Girdwood 		return 0;
17048a978234SLiam Girdwood 
17058a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
17065b2688a5SVedang Patel 		sizeof(struct snd_soc_tplg_pcm), count,
17078a978234SLiam Girdwood 		hdr->payload_size, "PCM DAI")) {
17088a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
17098a978234SLiam Girdwood 			count);
17108a978234SLiam Girdwood 		return -EINVAL;
17118a978234SLiam Girdwood 	}
17128a978234SLiam Girdwood 
171364527e8aSMengdong Lin 	/* create the FE DAIs and DAI links */
171406eb49f7SMengdong Lin 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
171564527e8aSMengdong Lin 	for (i = 0; i < count; i++) {
171606eb49f7SMengdong Lin 		if (pcm->size != sizeof(*pcm)) {
171706eb49f7SMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid pcm size\n");
171806eb49f7SMengdong Lin 			return -EINVAL;
171906eb49f7SMengdong Lin 		}
172006eb49f7SMengdong Lin 
172164527e8aSMengdong Lin 		soc_tplg_pcm_create(tplg, pcm);
172264527e8aSMengdong Lin 		pcm++;
172364527e8aSMengdong Lin 	}
172464527e8aSMengdong Lin 
17258a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
17265b2688a5SVedang Patel 	tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
17278a978234SLiam Girdwood 
17288a978234SLiam Girdwood 	return 0;
17298a978234SLiam Girdwood }
17308a978234SLiam Girdwood 
17310038be9aSMengdong Lin /* *
17320038be9aSMengdong Lin  * soc_tplg_be_dai_config - Find and configure an existing BE DAI.
17330038be9aSMengdong Lin  * @tplg: topology context
17340038be9aSMengdong Lin  * @be: topology BE DAI configs.
17350038be9aSMengdong Lin  *
17360038be9aSMengdong Lin  * The BE dai should already be registered by the platform driver. The
17370038be9aSMengdong Lin  * platform driver should specify the BE DAI name and ID for matching.
17380038be9aSMengdong Lin  */
17390038be9aSMengdong Lin static int soc_tplg_be_dai_config(struct soc_tplg *tplg,
17400038be9aSMengdong Lin 				  struct snd_soc_tplg_be_dai *be)
17410038be9aSMengdong Lin {
17420038be9aSMengdong Lin 	struct snd_soc_dai_link_component dai_component = {0};
17430038be9aSMengdong Lin 	struct snd_soc_dai *dai;
17440038be9aSMengdong Lin 	struct snd_soc_dai_driver *dai_drv;
17450038be9aSMengdong Lin 	struct snd_soc_pcm_stream *stream;
17460038be9aSMengdong Lin 	struct snd_soc_tplg_stream_caps *caps;
17470038be9aSMengdong Lin 	int ret;
17480038be9aSMengdong Lin 
17490038be9aSMengdong Lin 	dai_component.dai_name = be->dai_name;
17500038be9aSMengdong Lin 	dai = snd_soc_find_dai(&dai_component);
17510038be9aSMengdong Lin 	if (!dai) {
17520038be9aSMengdong Lin 		dev_err(tplg->dev, "ASoC: BE DAI %s not registered\n",
17530038be9aSMengdong Lin 			be->dai_name);
17540038be9aSMengdong Lin 		return -EINVAL;
17550038be9aSMengdong Lin 	}
17560038be9aSMengdong Lin 
17570038be9aSMengdong Lin 	if (be->dai_id != dai->id) {
17580038be9aSMengdong Lin 		dev_err(tplg->dev, "ASoC: BE DAI %s id mismatch\n",
17590038be9aSMengdong Lin 			be->dai_name);
17600038be9aSMengdong Lin 		return -EINVAL;
17610038be9aSMengdong Lin 	}
17620038be9aSMengdong Lin 
17630038be9aSMengdong Lin 	dai_drv = dai->driver;
17640038be9aSMengdong Lin 	if (!dai_drv)
17650038be9aSMengdong Lin 		return -EINVAL;
17660038be9aSMengdong Lin 
17670038be9aSMengdong Lin 	if (be->playback) {
17680038be9aSMengdong Lin 		stream = &dai_drv->playback;
17690038be9aSMengdong Lin 		caps = &be->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
17700038be9aSMengdong Lin 		set_stream_info(stream, caps);
17710038be9aSMengdong Lin 	}
17720038be9aSMengdong Lin 
17730038be9aSMengdong Lin 	if (be->capture) {
17740038be9aSMengdong Lin 		stream = &dai_drv->capture;
17750038be9aSMengdong Lin 		caps = &be->caps[SND_SOC_TPLG_STREAM_CAPTURE];
17760038be9aSMengdong Lin 		set_stream_info(stream, caps);
17770038be9aSMengdong Lin 	}
17780038be9aSMengdong Lin 
17790038be9aSMengdong Lin 	if (be->flag_mask)
17800038be9aSMengdong Lin 		set_dai_flags(dai_drv, be->flag_mask, be->flags);
17810038be9aSMengdong Lin 
17820038be9aSMengdong Lin 	/* pass control to component driver for optional further init */
17830038be9aSMengdong Lin 	ret = soc_tplg_dai_load(tplg, dai_drv);
17840038be9aSMengdong Lin 	if (ret < 0) {
17850038be9aSMengdong Lin 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
17860038be9aSMengdong Lin 		return ret;
17870038be9aSMengdong Lin 	}
17880038be9aSMengdong Lin 
17890038be9aSMengdong Lin 	return 0;
17900038be9aSMengdong Lin }
17910038be9aSMengdong Lin 
17920038be9aSMengdong Lin static int soc_tplg_be_dai_elems_load(struct soc_tplg *tplg,
17930038be9aSMengdong Lin 				      struct snd_soc_tplg_hdr *hdr)
17940038be9aSMengdong Lin {
17950038be9aSMengdong Lin 	struct snd_soc_tplg_be_dai *be;
17960038be9aSMengdong Lin 	int count = hdr->count;
17970038be9aSMengdong Lin 	int i;
17980038be9aSMengdong Lin 
17990038be9aSMengdong Lin 	if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
18000038be9aSMengdong Lin 		return 0;
18010038be9aSMengdong Lin 
18020038be9aSMengdong Lin 	/* config the existing BE DAIs */
18030038be9aSMengdong Lin 	for (i = 0; i < count; i++) {
18040038be9aSMengdong Lin 		be = (struct snd_soc_tplg_be_dai *)tplg->pos;
18050038be9aSMengdong Lin 		if (be->size != sizeof(*be)) {
18060038be9aSMengdong Lin 			dev_err(tplg->dev, "ASoC: invalid BE DAI size\n");
18070038be9aSMengdong Lin 			return -EINVAL;
18080038be9aSMengdong Lin 		}
18090038be9aSMengdong Lin 
18100038be9aSMengdong Lin 		soc_tplg_be_dai_config(tplg, be);
18110038be9aSMengdong Lin 		tplg->pos += (sizeof(*be) + be->priv.size);
18120038be9aSMengdong Lin 	}
18130038be9aSMengdong Lin 
18140038be9aSMengdong Lin 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
18150038be9aSMengdong Lin 	return 0;
18160038be9aSMengdong Lin }
18170038be9aSMengdong Lin 
1818*583958faSMengdong Lin /**
1819*583958faSMengdong Lin  * manifest_new_ver - Create a new version of manifest from the old version
1820*583958faSMengdong Lin  * of source.
1821*583958faSMengdong Lin  * @toplogy: topology context
1822*583958faSMengdong Lin  * @src: old version of manifest as a source
1823*583958faSMengdong Lin  * @manifest: latest version of manifest created from the source
1824*583958faSMengdong Lin  *
1825*583958faSMengdong Lin  * Support from vesion 4. Users need free the returned manifest manually.
1826*583958faSMengdong Lin  */
1827*583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg,
1828*583958faSMengdong Lin 			    struct snd_soc_tplg_manifest *src,
1829*583958faSMengdong Lin 			    struct snd_soc_tplg_manifest **manifest)
1830*583958faSMengdong Lin {
1831*583958faSMengdong Lin 	struct snd_soc_tplg_manifest *dest;
1832*583958faSMengdong Lin 	struct snd_soc_tplg_manifest_v4 *src_v4;
1833*583958faSMengdong Lin 
1834*583958faSMengdong Lin 	*manifest = NULL;
1835*583958faSMengdong Lin 
1836*583958faSMengdong Lin 	if (src->size != sizeof(*src_v4)) {
1837*583958faSMengdong Lin 		dev_err(tplg->dev, "ASoC: invalid manifest size\n");
1838*583958faSMengdong Lin 		return -EINVAL;
1839*583958faSMengdong Lin 	}
1840*583958faSMengdong Lin 
1841*583958faSMengdong Lin 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
1842*583958faSMengdong Lin 
1843*583958faSMengdong Lin 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
1844*583958faSMengdong Lin 	dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
1845*583958faSMengdong Lin 	if (!dest)
1846*583958faSMengdong Lin 		return -ENOMEM;
1847*583958faSMengdong Lin 
1848*583958faSMengdong Lin 	dest->size = sizeof(*dest);	/* size of latest abi version */
1849*583958faSMengdong Lin 	dest->control_elems = src_v4->control_elems;
1850*583958faSMengdong Lin 	dest->widget_elems = src_v4->widget_elems;
1851*583958faSMengdong Lin 	dest->graph_elems = src_v4->graph_elems;
1852*583958faSMengdong Lin 	dest->pcm_elems = src_v4->pcm_elems;
1853*583958faSMengdong Lin 	dest->dai_link_elems = src_v4->dai_link_elems;
1854*583958faSMengdong Lin 	dest->priv.size = src_v4->priv.size;
1855*583958faSMengdong Lin 	if (dest->priv.size)
1856*583958faSMengdong Lin 		memcpy(dest->priv.data, src_v4->priv.data,
1857*583958faSMengdong Lin 		       src_v4->priv.size);
1858*583958faSMengdong Lin 
1859*583958faSMengdong Lin 	*manifest = dest;
1860*583958faSMengdong Lin 	return 0;
1861*583958faSMengdong Lin }
18620038be9aSMengdong Lin 
18638a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
18648a978234SLiam Girdwood 				  struct snd_soc_tplg_hdr *hdr)
18658a978234SLiam Girdwood {
1866*583958faSMengdong Lin 	struct snd_soc_tplg_manifest *manifest, *_manifest;
1867*583958faSMengdong Lin 	bool abi_match;
1868*583958faSMengdong Lin 	int err;
18698a978234SLiam Girdwood 
18708a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
18718a978234SLiam Girdwood 		return 0;
18728a978234SLiam Girdwood 
18738a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1874*583958faSMengdong Lin 
1875*583958faSMengdong Lin 	/* check ABI version by size, create a new manifest if abi not match */
1876*583958faSMengdong Lin 	if (manifest->size == sizeof(*manifest)) {
1877*583958faSMengdong Lin 		abi_match = true;
1878*583958faSMengdong Lin 		_manifest = manifest;
1879*583958faSMengdong Lin 	} else {
1880*583958faSMengdong Lin 		abi_match = false;
1881*583958faSMengdong Lin 		err = manifest_new_ver(tplg, manifest, &_manifest);
1882*583958faSMengdong Lin 		if (err < 0)
1883*583958faSMengdong Lin 			return err;
188406eb49f7SMengdong Lin 	}
188506eb49f7SMengdong Lin 
1886*583958faSMengdong Lin 	/* pass control to component driver for optional further init */
18878a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
1888*583958faSMengdong Lin 		return tplg->ops->manifest(tplg->comp, _manifest);
18898a978234SLiam Girdwood 
1890*583958faSMengdong Lin 	if (!abi_match)	/* free the duplicated one */
1891*583958faSMengdong Lin 		kfree(_manifest);
1892*583958faSMengdong Lin 
18938a978234SLiam Girdwood 	return 0;
18948a978234SLiam Girdwood }
18958a978234SLiam Girdwood 
18968a978234SLiam Girdwood /* validate header magic, size and type */
18978a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
18988a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
18998a978234SLiam Girdwood {
19008a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
19018a978234SLiam Girdwood 		return 0;
19028a978234SLiam Girdwood 
190306eb49f7SMengdong Lin 	if (hdr->size != sizeof(*hdr)) {
190406eb49f7SMengdong Lin 		dev_err(tplg->dev,
190506eb49f7SMengdong Lin 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
190606eb49f7SMengdong Lin 			hdr->type, soc_tplg_get_hdr_offset(tplg),
190706eb49f7SMengdong Lin 			tplg->fw->size);
190806eb49f7SMengdong Lin 		return -EINVAL;
190906eb49f7SMengdong Lin 	}
191006eb49f7SMengdong Lin 
19118a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
19128a978234SLiam Girdwood 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
19138a978234SLiam Girdwood 		dev_err(tplg->dev,
19148a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
19158a978234SLiam Girdwood 			tplg->pass, hdr->magic,
19168a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
19178a978234SLiam Girdwood 		return -EINVAL;
19188a978234SLiam Girdwood 	}
19198a978234SLiam Girdwood 
19208a978234SLiam Girdwood 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
19218a978234SLiam Girdwood 		dev_err(tplg->dev,
19228a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
19238a978234SLiam Girdwood 			tplg->pass, hdr->magic,
19248a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
19258a978234SLiam Girdwood 		return -EINVAL;
19268a978234SLiam Girdwood 	}
19278a978234SLiam Girdwood 
19288a978234SLiam Girdwood 	if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
19298a978234SLiam Girdwood 		dev_err(tplg->dev,
19308a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
19318a978234SLiam Girdwood 			tplg->pass, hdr->abi,
19328a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
19338a978234SLiam Girdwood 			tplg->fw->size);
19348a978234SLiam Girdwood 		return -EINVAL;
19358a978234SLiam Girdwood 	}
19368a978234SLiam Girdwood 
19378a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
19388a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
19398a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
19408a978234SLiam Girdwood 		return -EINVAL;
19418a978234SLiam Girdwood 	}
19428a978234SLiam Girdwood 
19438a978234SLiam Girdwood 	if (tplg->pass == hdr->type)
19448a978234SLiam Girdwood 		dev_dbg(tplg->dev,
19458a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
19468a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
19478a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
19488a978234SLiam Girdwood 
19498a978234SLiam Girdwood 	return 1;
19508a978234SLiam Girdwood }
19518a978234SLiam Girdwood 
19528a978234SLiam Girdwood /* check header type and call appropriate handler */
19538a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
19548a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
19558a978234SLiam Girdwood {
19568a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
19578a978234SLiam Girdwood 
19588a978234SLiam Girdwood 	/* check for matching ID */
19598a978234SLiam Girdwood 	if (hdr->index != tplg->req_index &&
19608a978234SLiam Girdwood 		hdr->index != SND_SOC_TPLG_INDEX_ALL)
19618a978234SLiam Girdwood 		return 0;
19628a978234SLiam Girdwood 
19638a978234SLiam Girdwood 	tplg->index = hdr->index;
19648a978234SLiam Girdwood 
19658a978234SLiam Girdwood 	switch (hdr->type) {
19668a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
19678a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
19688a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
19698a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
19708a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
19718a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
19728a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
19738a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
19748a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
197564527e8aSMengdong Lin 		return soc_tplg_pcm_elems_load(tplg, hdr);
19760038be9aSMengdong Lin 	case SND_SOC_TPLG_TYPE_BE_DAI:
19770038be9aSMengdong Lin 		return soc_tplg_be_dai_elems_load(tplg, hdr);
19788a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
19798a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
19808a978234SLiam Girdwood 	default:
19818a978234SLiam Girdwood 		/* bespoke vendor data object */
19828a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
19838a978234SLiam Girdwood 	}
19848a978234SLiam Girdwood 
19858a978234SLiam Girdwood 	return 0;
19868a978234SLiam Girdwood }
19878a978234SLiam Girdwood 
19888a978234SLiam Girdwood /* process the topology file headers */
19898a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
19908a978234SLiam Girdwood {
19918a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
19928a978234SLiam Girdwood 	int ret;
19938a978234SLiam Girdwood 
19948a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
19958a978234SLiam Girdwood 
19968a978234SLiam Girdwood 	/* process the header types from start to end */
19978a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
19988a978234SLiam Girdwood 
19998a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
20008a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
20018a978234SLiam Girdwood 
20028a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
20038a978234SLiam Girdwood 
20048a978234SLiam Girdwood 			/* make sure header is valid before loading */
20058a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
20068a978234SLiam Girdwood 			if (ret < 0)
20078a978234SLiam Girdwood 				return ret;
20088a978234SLiam Girdwood 			else if (ret == 0)
20098a978234SLiam Girdwood 				break;
20108a978234SLiam Girdwood 
20118a978234SLiam Girdwood 			/* load the header object */
20128a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
20138a978234SLiam Girdwood 			if (ret < 0)
20148a978234SLiam Girdwood 				return ret;
20158a978234SLiam Girdwood 
20168a978234SLiam Girdwood 			/* goto next header */
20178a978234SLiam Girdwood 			tplg->hdr_pos += hdr->payload_size +
20188a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
20198a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
20208a978234SLiam Girdwood 		}
20218a978234SLiam Girdwood 
20228a978234SLiam Girdwood 		/* next data type pass */
20238a978234SLiam Girdwood 		tplg->pass++;
20248a978234SLiam Girdwood 	}
20258a978234SLiam Girdwood 
20268a978234SLiam Girdwood 	/* signal DAPM we are complete */
20278a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
20288a978234SLiam Girdwood 	if (ret < 0)
20298a978234SLiam Girdwood 		dev_err(tplg->dev,
20308a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
20318a978234SLiam Girdwood 
20328a978234SLiam Girdwood 	return ret;
20338a978234SLiam Girdwood }
20348a978234SLiam Girdwood 
20358a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
20368a978234SLiam Girdwood {
20378a978234SLiam Girdwood 	int ret;
20388a978234SLiam Girdwood 
20398a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
20408a978234SLiam Girdwood 	if (ret == 0)
20418a978234SLiam Girdwood 		soc_tplg_complete(tplg);
20428a978234SLiam Girdwood 
20438a978234SLiam Girdwood 	return ret;
20448a978234SLiam Girdwood }
20458a978234SLiam Girdwood 
20468a978234SLiam Girdwood /* load audio component topology from "firmware" file */
20478a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
20488a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
20498a978234SLiam Girdwood {
20508a978234SLiam Girdwood 	struct soc_tplg tplg;
20518a978234SLiam Girdwood 
20528a978234SLiam Girdwood 	/* setup parsing context */
20538a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
20548a978234SLiam Girdwood 	tplg.fw = fw;
20558a978234SLiam Girdwood 	tplg.dev = comp->dev;
20568a978234SLiam Girdwood 	tplg.comp = comp;
20578a978234SLiam Girdwood 	tplg.ops = ops;
20588a978234SLiam Girdwood 	tplg.req_index = id;
20598a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
20608a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
20611a3232d2SMengdong Lin 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
20621a3232d2SMengdong Lin 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
20638a978234SLiam Girdwood 
20648a978234SLiam Girdwood 	return soc_tplg_load(&tplg);
20658a978234SLiam Girdwood }
20668a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
20678a978234SLiam Girdwood 
20688a978234SLiam Girdwood /* remove this dynamic widget */
20698a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
20708a978234SLiam Girdwood {
20718a978234SLiam Girdwood 	/* make sure we are a widget */
20728a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
20738a978234SLiam Girdwood 		return;
20748a978234SLiam Girdwood 
20758a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
20768a978234SLiam Girdwood }
20778a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
20788a978234SLiam Girdwood 
20798a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
20808a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
20818a978234SLiam Girdwood 	u32 index)
20828a978234SLiam Girdwood {
20838a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
20848a978234SLiam Girdwood 
20858a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
20868a978234SLiam Girdwood 
20878a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
20888a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
20898a978234SLiam Girdwood 			continue;
20908a978234SLiam Girdwood 
20918a978234SLiam Girdwood 		/* match ID */
20928a978234SLiam Girdwood 		if (w->dobj.index != index &&
20938a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
20948a978234SLiam Girdwood 			continue;
20958a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
20968a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
2097b97e2698SLars-Peter Clausen 		snd_soc_dapm_free_widget(w);
20988a978234SLiam Girdwood 	}
2099fd589a1bSJyri Sarha 	snd_soc_dapm_reset_cache(dapm);
21008a978234SLiam Girdwood }
21018a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
21028a978234SLiam Girdwood 
21038a978234SLiam Girdwood /* remove dynamic controls from the component driver */
21048a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
21058a978234SLiam Girdwood {
21068a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
21078a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
21088a978234SLiam Girdwood 
21098a978234SLiam Girdwood 	/* process the header types from end to start */
21108a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
21118a978234SLiam Girdwood 
21128a978234SLiam Girdwood 		/* remove mixer controls */
21138a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
21148a978234SLiam Girdwood 			list) {
21158a978234SLiam Girdwood 
21168a978234SLiam Girdwood 			/* match index */
21178a978234SLiam Girdwood 			if (dobj->index != index &&
21188a978234SLiam Girdwood 				dobj->index != SND_SOC_TPLG_INDEX_ALL)
21198a978234SLiam Girdwood 				continue;
21208a978234SLiam Girdwood 
21218a978234SLiam Girdwood 			switch (dobj->type) {
21228a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
21238a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
21248a978234SLiam Girdwood 				break;
21258a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
21268a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
21278a978234SLiam Girdwood 				break;
21288a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
21298a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
21308a978234SLiam Girdwood 				break;
21318a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
21328a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
21338a978234SLiam Girdwood 				break;
21348a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
213564527e8aSMengdong Lin 				remove_dai(comp, dobj, pass);
21368a978234SLiam Girdwood 				break;
2137acfc7d46SMengdong Lin 			case SND_SOC_DOBJ_DAI_LINK:
2138acfc7d46SMengdong Lin 				remove_link(comp, dobj, pass);
2139acfc7d46SMengdong Lin 				break;
21408a978234SLiam Girdwood 			default:
21418a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
21428a978234SLiam Girdwood 					dobj->type);
21438a978234SLiam Girdwood 				break;
21448a978234SLiam Girdwood 			}
21458a978234SLiam Girdwood 		}
21468a978234SLiam Girdwood 		pass--;
21478a978234SLiam Girdwood 	}
21488a978234SLiam Girdwood 
21498a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
21508a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
21518a978234SLiam Girdwood }
21528a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2153