xref: /openbmc/linux/sound/soc/soc-topology.c (revision 4ca7deb1)
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>
368a978234SLiam Girdwood 
378a978234SLiam Girdwood /*
388a978234SLiam Girdwood  * We make several passes over the data (since it wont necessarily be ordered)
398a978234SLiam Girdwood  * and process objects in the following order. This guarantees the component
408a978234SLiam Girdwood  * drivers will be ready with any vendor data before the mixers and DAPM objects
418a978234SLiam Girdwood  * are loaded (that may make use of the vendor data).
428a978234SLiam Girdwood  */
438a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST		0
448a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR		1
458a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER		2
468a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET		3
478a978234SLiam Girdwood #define SOC_TPLG_PASS_GRAPH		4
488a978234SLiam Girdwood #define SOC_TPLG_PASS_PINS		5
498a978234SLiam Girdwood #define SOC_TPLG_PASS_PCM_DAI		6
508a978234SLiam Girdwood 
518a978234SLiam Girdwood #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
528a978234SLiam Girdwood #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_PCM_DAI
538a978234SLiam Girdwood 
548a978234SLiam Girdwood struct soc_tplg {
558a978234SLiam Girdwood 	const struct firmware *fw;
568a978234SLiam Girdwood 
578a978234SLiam Girdwood 	/* runtime FW parsing */
588a978234SLiam Girdwood 	const u8 *pos;		/* read postion */
598a978234SLiam Girdwood 	const u8 *hdr_pos;	/* header position */
608a978234SLiam Girdwood 	unsigned int pass;	/* pass number */
618a978234SLiam Girdwood 
628a978234SLiam Girdwood 	/* component caller */
638a978234SLiam Girdwood 	struct device *dev;
648a978234SLiam Girdwood 	struct snd_soc_component *comp;
658a978234SLiam Girdwood 	u32 index;	/* current block index */
668a978234SLiam Girdwood 	u32 req_index;	/* required index, only loaded/free matching blocks */
678a978234SLiam Girdwood 
688a978234SLiam Girdwood 	/* kcontrol operations */
698a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
708a978234SLiam Girdwood 	int io_ops_count;
718a978234SLiam Girdwood 
728a978234SLiam Girdwood 	/* optional fw loading callbacks to component drivers */
738a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops;
748a978234SLiam Girdwood };
758a978234SLiam Girdwood 
768a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg);
778a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg);
788a978234SLiam Girdwood struct snd_soc_dapm_widget *
798a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
808a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
818a978234SLiam Girdwood struct snd_soc_dapm_widget *
828a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
838a978234SLiam Girdwood 			 const struct snd_soc_dapm_widget *widget);
848a978234SLiam Girdwood 
858a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */
868a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
878a978234SLiam Girdwood 	unsigned int count, size_t bytes, const char *elem_type)
888a978234SLiam Girdwood {
898a978234SLiam Girdwood 	const u8 *end = tplg->pos + elem_size * count;
908a978234SLiam Girdwood 
918a978234SLiam Girdwood 	if (end > tplg->fw->data + tplg->fw->size) {
928a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
938a978234SLiam Girdwood 			elem_type);
948a978234SLiam Girdwood 		return -EINVAL;
958a978234SLiam Girdwood 	}
968a978234SLiam Girdwood 
978a978234SLiam Girdwood 	/* check there is enough room in chunk for control.
988a978234SLiam Girdwood 	   extra bytes at the end of control are for vendor data here  */
998a978234SLiam Girdwood 	if (elem_size * count > bytes) {
1008a978234SLiam Girdwood 		dev_err(tplg->dev,
1018a978234SLiam Girdwood 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
1028a978234SLiam Girdwood 			elem_type, count, elem_size, bytes);
1038a978234SLiam Girdwood 		return -EINVAL;
1048a978234SLiam Girdwood 	}
1058a978234SLiam Girdwood 
1068a978234SLiam Girdwood 	return 0;
1078a978234SLiam Girdwood }
1088a978234SLiam Girdwood 
1098a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
1108a978234SLiam Girdwood {
1118a978234SLiam Girdwood 	const u8 *end = tplg->hdr_pos;
1128a978234SLiam Girdwood 
1138a978234SLiam Girdwood 	if (end >= tplg->fw->data + tplg->fw->size)
1148a978234SLiam Girdwood 		return 1;
1158a978234SLiam Girdwood 	return 0;
1168a978234SLiam Girdwood }
1178a978234SLiam Girdwood 
1188a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
1198a978234SLiam Girdwood {
1208a978234SLiam Girdwood 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
1218a978234SLiam Girdwood }
1228a978234SLiam Girdwood 
1238a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
1248a978234SLiam Girdwood {
1258a978234SLiam Girdwood 	return (unsigned long)(tplg->pos - tplg->fw->data);
1268a978234SLiam Girdwood }
1278a978234SLiam Girdwood 
1288a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */
1298a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
1308a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
1318a978234SLiam Girdwood 		snd_soc_put_volsw, snd_soc_info_volsw},
1328a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
1338a978234SLiam Girdwood 		snd_soc_put_volsw_sx, NULL},
1348a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
1358a978234SLiam Girdwood 		snd_soc_put_enum_double, snd_soc_info_enum_double},
1368a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
1378a978234SLiam Girdwood 		snd_soc_put_enum_double, NULL},
1388a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
1398a978234SLiam Girdwood 		snd_soc_bytes_put, snd_soc_bytes_info},
1408a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
1418a978234SLiam Girdwood 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
1428a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
1438a978234SLiam Girdwood 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
1448a978234SLiam Girdwood 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
1458a978234SLiam Girdwood 		snd_soc_put_strobe, NULL},
1468a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
1478a978234SLiam Girdwood 		snd_soc_dapm_put_volsw, NULL},
1488a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
1498a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
1508a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
1518a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1528a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
1538a978234SLiam Girdwood 		snd_soc_dapm_put_enum_double, NULL},
1548a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
1558a978234SLiam Girdwood 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
1568a978234SLiam Girdwood };
1578a978234SLiam Girdwood 
1588a978234SLiam Girdwood struct soc_tplg_map {
1598a978234SLiam Girdwood 	int uid;
1608a978234SLiam Girdwood 	int kid;
1618a978234SLiam Girdwood };
1628a978234SLiam Girdwood 
1638a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */
1648a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = {
1658a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
1668a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
1678a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
1688a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
1698a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
1708a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
1718a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
1728a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
1738a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
1748a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
1758a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
1768a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
1778a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
1788a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
1798a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
1808a978234SLiam Girdwood 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
1818a978234SLiam Girdwood };
1828a978234SLiam Girdwood 
1838a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg,
1848a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1858a978234SLiam Girdwood {
1868a978234SLiam Girdwood 	int i;
1878a978234SLiam Girdwood 
1888a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
1898a978234SLiam Girdwood 		if (chan[i].id == map)
1908a978234SLiam Girdwood 			return chan[i].reg;
1918a978234SLiam Girdwood 	}
1928a978234SLiam Girdwood 
1938a978234SLiam Girdwood 	return -EINVAL;
1948a978234SLiam Girdwood }
1958a978234SLiam Girdwood 
1968a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg,
1978a978234SLiam Girdwood 	struct snd_soc_tplg_channel *chan, int map)
1988a978234SLiam Girdwood {
1998a978234SLiam Girdwood 	int i;
2008a978234SLiam Girdwood 
2018a978234SLiam Girdwood 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
2028a978234SLiam Girdwood 		if (chan[i].id == map)
2038a978234SLiam Girdwood 			return chan[i].shift;
2048a978234SLiam Girdwood 	}
2058a978234SLiam Girdwood 
2068a978234SLiam Girdwood 	return -EINVAL;
2078a978234SLiam Girdwood }
2088a978234SLiam Girdwood 
2098a978234SLiam Girdwood static int get_widget_id(int tplg_type)
2108a978234SLiam Girdwood {
2118a978234SLiam Girdwood 	int i;
2128a978234SLiam Girdwood 
2138a978234SLiam Girdwood 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
2148a978234SLiam Girdwood 		if (tplg_type == dapm_map[i].uid)
2158a978234SLiam Girdwood 			return dapm_map[i].kid;
2168a978234SLiam Girdwood 	}
2178a978234SLiam Girdwood 
2188a978234SLiam Girdwood 	return -EINVAL;
2198a978234SLiam Girdwood }
2208a978234SLiam Girdwood 
2218a978234SLiam Girdwood static enum snd_soc_dobj_type get_dobj_mixer_type(
2228a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr)
2238a978234SLiam Girdwood {
2248a978234SLiam Girdwood 	if (control_hdr == NULL)
2258a978234SLiam Girdwood 		return SND_SOC_DOBJ_NONE;
2268a978234SLiam Girdwood 
2278a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
2288a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
2298a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
2308a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
2318a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
2328a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
2338a978234SLiam Girdwood 		return SND_SOC_DOBJ_MIXER;
2348a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
2358a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
2368a978234SLiam Girdwood 		return SND_SOC_DOBJ_ENUM;
2378a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
2388a978234SLiam Girdwood 		return SND_SOC_DOBJ_BYTES;
2398a978234SLiam Girdwood 	default:
2408a978234SLiam Girdwood 		return SND_SOC_DOBJ_NONE;
2418a978234SLiam Girdwood 	}
2428a978234SLiam Girdwood }
2438a978234SLiam Girdwood 
2448a978234SLiam Girdwood static enum snd_soc_dobj_type get_dobj_type(struct snd_soc_tplg_hdr *hdr,
2458a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr)
2468a978234SLiam Girdwood {
2478a978234SLiam Girdwood 	switch (hdr->type) {
2488a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
2498a978234SLiam Girdwood 		return get_dobj_mixer_type(control_hdr);
2508a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2518a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
2528a978234SLiam Girdwood 		return SND_SOC_DOBJ_NONE;
2538a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2548a978234SLiam Girdwood 		return SND_SOC_DOBJ_WIDGET;
2558a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2568a978234SLiam Girdwood 		return SND_SOC_DOBJ_DAI_LINK;
2578a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
2588a978234SLiam Girdwood 		return SND_SOC_DOBJ_PCM;
2598a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_CODEC_LINK:
2608a978234SLiam Girdwood 		return SND_SOC_DOBJ_CODEC_LINK;
2618a978234SLiam Girdwood 	default:
2628a978234SLiam Girdwood 		return SND_SOC_DOBJ_NONE;
2638a978234SLiam Girdwood 	}
2648a978234SLiam Girdwood }
2658a978234SLiam Girdwood 
2668a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg,
2678a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
2688a978234SLiam Girdwood {
2698a978234SLiam Girdwood 	dev_err(tplg->dev,
2708a978234SLiam Girdwood 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
2718a978234SLiam Girdwood 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
2728a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2738a978234SLiam Girdwood }
2748a978234SLiam Girdwood 
2758a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg,
2768a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
2778a978234SLiam Girdwood {
2788a978234SLiam Girdwood 	dev_err(tplg->dev,
2798a978234SLiam Girdwood 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
2808a978234SLiam Girdwood 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
2818a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
2828a978234SLiam Girdwood }
2838a978234SLiam Girdwood 
2848a978234SLiam Girdwood /* pass vendor data to component driver for processing */
2858a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
2868a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
2878a978234SLiam Girdwood {
2888a978234SLiam Girdwood 	int ret = 0;
2898a978234SLiam Girdwood 
2908a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
2918a978234SLiam Girdwood 		ret = tplg->ops->vendor_load(tplg->comp, hdr);
2928a978234SLiam Girdwood 	else {
2938a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
2948a978234SLiam Girdwood 			hdr->vendor_type);
2958a978234SLiam Girdwood 		return -EINVAL;
2968a978234SLiam Girdwood 	}
2978a978234SLiam Girdwood 
2988a978234SLiam Girdwood 	if (ret < 0)
2998a978234SLiam Girdwood 		dev_err(tplg->dev,
3008a978234SLiam Girdwood 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
3018a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
3028a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg),
3038a978234SLiam Girdwood 			hdr->type, hdr->vendor_type);
3048a978234SLiam Girdwood 	return ret;
3058a978234SLiam Girdwood }
3068a978234SLiam Girdwood 
3078a978234SLiam Girdwood /* pass vendor data to component driver for processing */
3088a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg,
3098a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
3108a978234SLiam Girdwood {
3118a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
3128a978234SLiam Girdwood 		return 0;
3138a978234SLiam Girdwood 
3148a978234SLiam Girdwood 	return soc_tplg_vendor_load_(tplg, hdr);
3158a978234SLiam Girdwood }
3168a978234SLiam Girdwood 
3178a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for
3188a978234SLiam Girdwood  * external widgets where we can assign private data/ops */
3198a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg,
3208a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
3218a978234SLiam Girdwood {
3228a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
3238a978234SLiam Girdwood 		return tplg->ops->widget_load(tplg->comp, w, tplg_w);
3248a978234SLiam Girdwood 
3258a978234SLiam Girdwood 	return 0;
3268a978234SLiam Girdwood }
3278a978234SLiam Girdwood 
3288a978234SLiam Girdwood /* pass dynamic FEs configurations to component driver */
3298a978234SLiam Girdwood static int soc_tplg_pcm_dai_load(struct soc_tplg *tplg,
3308a978234SLiam Girdwood 	struct snd_soc_tplg_pcm_dai *pcm_dai, int num_pcm_dai)
3318a978234SLiam Girdwood {
3328a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->pcm_dai_load)
3338a978234SLiam Girdwood 		return tplg->ops->pcm_dai_load(tplg->comp, pcm_dai, num_pcm_dai);
3348a978234SLiam Girdwood 
3358a978234SLiam Girdwood 	return 0;
3368a978234SLiam Girdwood }
3378a978234SLiam Girdwood 
3388a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */
3398a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg)
3408a978234SLiam Girdwood {
3418a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->complete)
3428a978234SLiam Girdwood 		tplg->ops->complete(tplg->comp);
3438a978234SLiam Girdwood }
3448a978234SLiam Girdwood 
3458a978234SLiam Girdwood /* add a dynamic kcontrol */
3468a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
3478a978234SLiam Girdwood 	const struct snd_kcontrol_new *control_new, const char *prefix,
3488a978234SLiam Girdwood 	void *data, struct snd_kcontrol **kcontrol)
3498a978234SLiam Girdwood {
3508a978234SLiam Girdwood 	int err;
3518a978234SLiam Girdwood 
3528a978234SLiam Girdwood 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
3538a978234SLiam Girdwood 	if (*kcontrol == NULL) {
3548a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
3558a978234SLiam Girdwood 		control_new->name);
3568a978234SLiam Girdwood 		return -ENOMEM;
3578a978234SLiam Girdwood 	}
3588a978234SLiam Girdwood 
3598a978234SLiam Girdwood 	err = snd_ctl_add(card, *kcontrol);
3608a978234SLiam Girdwood 	if (err < 0) {
3618a978234SLiam Girdwood 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
3628a978234SLiam Girdwood 			control_new->name, err);
3638a978234SLiam Girdwood 		return err;
3648a978234SLiam Girdwood 	}
3658a978234SLiam Girdwood 
3668a978234SLiam Girdwood 	return 0;
3678a978234SLiam Girdwood }
3688a978234SLiam Girdwood 
3698a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */
3708a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
3718a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
3728a978234SLiam Girdwood {
3738a978234SLiam Girdwood 	struct snd_soc_component *comp = tplg->comp;
3748a978234SLiam Girdwood 
3758a978234SLiam Girdwood 	return soc_tplg_add_dcontrol(comp->card->snd_card,
3768a978234SLiam Girdwood 				comp->dev, k, NULL, comp, kcontrol);
3778a978234SLiam Girdwood }
3788a978234SLiam Girdwood 
3798a978234SLiam Girdwood /* remove a mixer kcontrol */
3808a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp,
3818a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
3828a978234SLiam Girdwood {
3838a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
3848a978234SLiam Girdwood 	struct soc_mixer_control *sm =
3858a978234SLiam Girdwood 		container_of(dobj, struct soc_mixer_control, dobj);
3868a978234SLiam Girdwood 	const unsigned int *p = NULL;
3878a978234SLiam Girdwood 
3888a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
3898a978234SLiam Girdwood 		return;
3908a978234SLiam Girdwood 
3918a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
3928a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
3938a978234SLiam Girdwood 
3948a978234SLiam Girdwood 	if (sm->dobj.control.kcontrol->tlv.p)
3958a978234SLiam Girdwood 		p = sm->dobj.control.kcontrol->tlv.p;
3968a978234SLiam Girdwood 	snd_ctl_remove(card, sm->dobj.control.kcontrol);
3978a978234SLiam Girdwood 	list_del(&sm->dobj.list);
3988a978234SLiam Girdwood 	kfree(sm);
3998a978234SLiam Girdwood 	kfree(p);
4008a978234SLiam Girdwood }
4018a978234SLiam Girdwood 
4028a978234SLiam Girdwood /* remove an enum kcontrol */
4038a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp,
4048a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4058a978234SLiam Girdwood {
4068a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4078a978234SLiam Girdwood 	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
4088a978234SLiam Girdwood 	int i;
4098a978234SLiam Girdwood 
4108a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4118a978234SLiam Girdwood 		return;
4128a978234SLiam Girdwood 
4138a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4148a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4158a978234SLiam Girdwood 
4168a978234SLiam Girdwood 	snd_ctl_remove(card, se->dobj.control.kcontrol);
4178a978234SLiam Girdwood 	list_del(&se->dobj.list);
4188a978234SLiam Girdwood 
4198a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
4208a978234SLiam Girdwood 	for (i = 0; i < se->items; i++)
4218a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
4228a978234SLiam Girdwood 	kfree(se);
4238a978234SLiam Girdwood }
4248a978234SLiam Girdwood 
4258a978234SLiam Girdwood /* remove a byte kcontrol */
4268a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp,
4278a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4288a978234SLiam Girdwood {
4298a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4308a978234SLiam Girdwood 	struct soc_bytes_ext *sb =
4318a978234SLiam Girdwood 		container_of(dobj, struct soc_bytes_ext, dobj);
4328a978234SLiam Girdwood 
4338a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_MIXER)
4348a978234SLiam Girdwood 		return;
4358a978234SLiam Girdwood 
4368a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->control_unload)
4378a978234SLiam Girdwood 		dobj->ops->control_unload(comp, dobj);
4388a978234SLiam Girdwood 
4398a978234SLiam Girdwood 	snd_ctl_remove(card, sb->dobj.control.kcontrol);
4408a978234SLiam Girdwood 	list_del(&sb->dobj.list);
4418a978234SLiam Girdwood 	kfree(sb);
4428a978234SLiam Girdwood }
4438a978234SLiam Girdwood 
4448a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */
4458a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp,
4468a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4478a978234SLiam Girdwood {
4488a978234SLiam Girdwood 	struct snd_card *card = comp->card->snd_card;
4498a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w =
4508a978234SLiam Girdwood 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
4518a978234SLiam Girdwood 	int i;
4528a978234SLiam Girdwood 
4538a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_WIDGET)
4548a978234SLiam Girdwood 		return;
4558a978234SLiam Girdwood 
4568a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->widget_unload)
4578a978234SLiam Girdwood 		dobj->ops->widget_unload(comp, dobj);
4588a978234SLiam Girdwood 
4598a978234SLiam Girdwood 	/*
4608a978234SLiam Girdwood 	 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
4618a978234SLiam Girdwood 	 * The enum may either have an array of values or strings.
4628a978234SLiam Girdwood 	 */
4638a978234SLiam Girdwood 	if (dobj->widget.kcontrol_enum) {
4648a978234SLiam Girdwood 		/* enumerated widget mixer */
4658a978234SLiam Girdwood 		struct soc_enum *se =
4668a978234SLiam Girdwood 			(struct soc_enum *)w->kcontrols[0]->private_value;
4678a978234SLiam Girdwood 
4688a978234SLiam Girdwood 		snd_ctl_remove(card, w->kcontrols[0]);
4698a978234SLiam Girdwood 
4708a978234SLiam Girdwood 		kfree(se->dobj.control.dvalues);
4718a978234SLiam Girdwood 		for (i = 0; i < se->items; i++)
4728a978234SLiam Girdwood 			kfree(se->dobj.control.dtexts[i]);
4738a978234SLiam Girdwood 
4748a978234SLiam Girdwood 		kfree(se);
4758a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4768a978234SLiam Girdwood 	} else {
4778a978234SLiam Girdwood 		/* non enumerated widget mixer */
4788a978234SLiam Girdwood 		for (i = 0; i < w->num_kcontrols; i++) {
4798a978234SLiam Girdwood 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
4808a978234SLiam Girdwood 			struct soc_mixer_control *sm =
4818a978234SLiam Girdwood 			(struct soc_mixer_control *) kcontrol->private_value;
4828a978234SLiam Girdwood 
4838a978234SLiam Girdwood 			kfree(w->kcontrols[i]->tlv.p);
4848a978234SLiam Girdwood 
4858a978234SLiam Girdwood 			snd_ctl_remove(card, w->kcontrols[i]);
4868a978234SLiam Girdwood 			kfree(sm);
4878a978234SLiam Girdwood 		}
4888a978234SLiam Girdwood 		kfree(w->kcontrol_news);
4898a978234SLiam Girdwood 	}
4908a978234SLiam Girdwood 	/* widget w is freed by soc-dapm.c */
4918a978234SLiam Girdwood }
4928a978234SLiam Girdwood 
4938a978234SLiam Girdwood /* remove PCM DAI configurations */
4948a978234SLiam Girdwood static void remove_pcm_dai(struct snd_soc_component *comp,
4958a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, int pass)
4968a978234SLiam Girdwood {
4978a978234SLiam Girdwood 	if (pass != SOC_TPLG_PASS_PCM_DAI)
4988a978234SLiam Girdwood 		return;
4998a978234SLiam Girdwood 
5008a978234SLiam Girdwood 	if (dobj->ops && dobj->ops->pcm_dai_unload)
5018a978234SLiam Girdwood 		dobj->ops->pcm_dai_unload(comp, dobj);
5028a978234SLiam Girdwood 
5038a978234SLiam Girdwood 	list_del(&dobj->list);
5048a978234SLiam Girdwood 	kfree(dobj);
5058a978234SLiam Girdwood }
5068a978234SLiam Girdwood 
5078a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */
5088a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
5098a978234SLiam Girdwood 	struct snd_kcontrol_new *k,
5108a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *ops, int num_ops,
5118a978234SLiam Girdwood 	const struct snd_soc_tplg_kcontrol_ops *bops, int num_bops)
5128a978234SLiam Girdwood {
5138a978234SLiam Girdwood 	int i;
5148a978234SLiam Girdwood 
5158a978234SLiam Girdwood 	/* try and map standard kcontrols handler first */
5168a978234SLiam Girdwood 	for (i = 0; i < num_ops; i++) {
5178a978234SLiam Girdwood 
5188a978234SLiam Girdwood 		if (ops[i].id == hdr->ops.put)
5198a978234SLiam Girdwood 			k->put = ops[i].put;
5208a978234SLiam Girdwood 		if (ops[i].id == hdr->ops.get)
5218a978234SLiam Girdwood 			k->get = ops[i].get;
5228a978234SLiam Girdwood 		if (ops[i].id == hdr->ops.info)
5238a978234SLiam Girdwood 			k->info = ops[i].info;
5248a978234SLiam Girdwood 	}
5258a978234SLiam Girdwood 
5268a978234SLiam Girdwood 	/* standard handlers found ? */
5278a978234SLiam Girdwood 	if (k->put && k->get && k->info)
5288a978234SLiam Girdwood 		return 0;
5298a978234SLiam Girdwood 
5308a978234SLiam Girdwood 	/* none found so try bespoke handlers */
5318a978234SLiam Girdwood 	for (i = 0; i < num_bops; i++) {
5328a978234SLiam Girdwood 
5338a978234SLiam Girdwood 		if (k->put == NULL && bops[i].id == hdr->ops.put)
5348a978234SLiam Girdwood 			k->put = bops[i].put;
5358a978234SLiam Girdwood 		if (k->get == NULL && bops[i].id == hdr->ops.get)
5368a978234SLiam Girdwood 			k->get = bops[i].get;
5378a978234SLiam Girdwood 		if (k->info == NULL && ops[i].id == hdr->ops.info)
5388a978234SLiam Girdwood 			k->info = bops[i].info;
5398a978234SLiam Girdwood 	}
5408a978234SLiam Girdwood 
5418a978234SLiam Girdwood 	/* bespoke handlers found ? */
5428a978234SLiam Girdwood 	if (k->put && k->get && k->info)
5438a978234SLiam Girdwood 		return 0;
5448a978234SLiam Girdwood 
5458a978234SLiam Girdwood 	/* nothing to bind */
5468a978234SLiam Girdwood 	return -EINVAL;
5478a978234SLiam Girdwood }
5488a978234SLiam Girdwood 
5498a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */
5508a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
5518a978234SLiam Girdwood 		const struct snd_soc_tplg_widget_events *events,
5528a978234SLiam Girdwood 		int num_events, u16 event_type)
5538a978234SLiam Girdwood {
5548a978234SLiam Girdwood 	int i;
5558a978234SLiam Girdwood 
5568a978234SLiam Girdwood 	w->event = NULL;
5578a978234SLiam Girdwood 
5588a978234SLiam Girdwood 	for (i = 0; i < num_events; i++) {
5598a978234SLiam Girdwood 		if (event_type == events[i].type) {
5608a978234SLiam Girdwood 
5618a978234SLiam Girdwood 			/* found - so assign event */
5628a978234SLiam Girdwood 			w->event = events[i].event_handler;
5638a978234SLiam Girdwood 			return 0;
5648a978234SLiam Girdwood 		}
5658a978234SLiam Girdwood 	}
5668a978234SLiam Girdwood 
5678a978234SLiam Girdwood 	/* not found */
5688a978234SLiam Girdwood 	return -EINVAL;
5698a978234SLiam Girdwood }
5708a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
5718a978234SLiam Girdwood 
5728a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */
5738a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
5748a978234SLiam Girdwood 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
5758a978234SLiam Girdwood {
5768a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
5778a978234SLiam Girdwood 		return tplg->ops->control_load(tplg->comp, k, hdr);
5788a978234SLiam Girdwood 
5798a978234SLiam Girdwood 	return 0;
5808a978234SLiam Girdwood }
5818a978234SLiam Girdwood 
5828a978234SLiam Girdwood static int soc_tplg_create_tlv(struct soc_tplg *tplg,
5838a978234SLiam Girdwood 	struct snd_kcontrol_new *kc, u32 tlv_size)
5848a978234SLiam Girdwood {
5858a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
5868a978234SLiam Girdwood 	struct snd_ctl_tlv *tlv;
5878a978234SLiam Girdwood 
5888a978234SLiam Girdwood 	if (tlv_size == 0)
5898a978234SLiam Girdwood 		return 0;
5908a978234SLiam Girdwood 
5918a978234SLiam Girdwood 	tplg_tlv = (struct snd_soc_tplg_ctl_tlv *) tplg->pos;
5928a978234SLiam Girdwood 	tplg->pos += tlv_size;
5938a978234SLiam Girdwood 
5948a978234SLiam Girdwood 	tlv = kzalloc(sizeof(*tlv) + tlv_size, GFP_KERNEL);
5958a978234SLiam Girdwood 	if (tlv == NULL)
5968a978234SLiam Girdwood 		return -ENOMEM;
5978a978234SLiam Girdwood 
5988a978234SLiam Girdwood 	dev_dbg(tplg->dev, " created TLV type %d size %d bytes\n",
5998a978234SLiam Girdwood 		tplg_tlv->numid, tplg_tlv->size);
6008a978234SLiam Girdwood 
6018a978234SLiam Girdwood 	tlv->numid = tplg_tlv->numid;
6028a978234SLiam Girdwood 	tlv->length = tplg_tlv->size;
6038a978234SLiam Girdwood 	memcpy(tlv->tlv, tplg_tlv + 1, tplg_tlv->size);
6048a978234SLiam Girdwood 	kc->tlv.p = (void *)tlv;
6058a978234SLiam Girdwood 
6068a978234SLiam Girdwood 	return 0;
6078a978234SLiam Girdwood }
6088a978234SLiam Girdwood 
6098a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
6108a978234SLiam Girdwood 	struct snd_kcontrol_new *kc)
6118a978234SLiam Girdwood {
6128a978234SLiam Girdwood 	kfree(kc->tlv.p);
6138a978234SLiam Girdwood }
6148a978234SLiam Girdwood 
6158a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
6168a978234SLiam Girdwood 	size_t size)
6178a978234SLiam Girdwood {
6188a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
6198a978234SLiam Girdwood 	struct soc_bytes_ext *sbe;
6208a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
6218a978234SLiam Girdwood 	int i, err;
6228a978234SLiam Girdwood 
6238a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
6248a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_bytes_control), count,
6258a978234SLiam Girdwood 			size, "mixer bytes")) {
6268a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
6278a978234SLiam Girdwood 			count);
6288a978234SLiam Girdwood 		return -EINVAL;
6298a978234SLiam Girdwood 	}
6308a978234SLiam Girdwood 
6318a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
6328a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
6338a978234SLiam Girdwood 
6348a978234SLiam Girdwood 		/* validate kcontrol */
6358a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
6368a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
6378a978234SLiam Girdwood 			return -EINVAL;
6388a978234SLiam Girdwood 
6398a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
6408a978234SLiam Girdwood 		if (sbe == NULL)
6418a978234SLiam Girdwood 			return -ENOMEM;
6428a978234SLiam Girdwood 
6438a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
6448a978234SLiam Girdwood 			be->priv.size);
6458a978234SLiam Girdwood 
6468a978234SLiam Girdwood 		dev_dbg(tplg->dev,
6478a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
6488a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
6498a978234SLiam Girdwood 
6508a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
6518a978234SLiam Girdwood 		kc.name = be->hdr.name;
6528a978234SLiam Girdwood 		kc.private_value = (long)sbe;
6538a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
6548a978234SLiam Girdwood 		kc.access = be->hdr.access;
6558a978234SLiam Girdwood 
6568a978234SLiam Girdwood 		sbe->max = be->max;
6578a978234SLiam Girdwood 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
6588a978234SLiam Girdwood 		sbe->dobj.ops = tplg->ops;
6598a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
6608a978234SLiam Girdwood 
6618a978234SLiam Girdwood 		/* map io handlers */
6628a978234SLiam Girdwood 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, io_ops,
6638a978234SLiam Girdwood 			ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
6648a978234SLiam Girdwood 		if (err) {
6658a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
6668a978234SLiam Girdwood 			kfree(sbe);
6678a978234SLiam Girdwood 			continue;
6688a978234SLiam Girdwood 		}
6698a978234SLiam Girdwood 
6708a978234SLiam Girdwood 		/* pass control to driver for optional further init */
6718a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
6728a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
6738a978234SLiam Girdwood 		if (err < 0) {
6748a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
6758a978234SLiam Girdwood 				be->hdr.name);
6768a978234SLiam Girdwood 			kfree(sbe);
6778a978234SLiam Girdwood 			continue;
6788a978234SLiam Girdwood 		}
6798a978234SLiam Girdwood 
6808a978234SLiam Girdwood 		/* register control here */
6818a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
6828a978234SLiam Girdwood 			&sbe->dobj.control.kcontrol);
6838a978234SLiam Girdwood 		if (err < 0) {
6848a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
6858a978234SLiam Girdwood 				be->hdr.name);
6868a978234SLiam Girdwood 			kfree(sbe);
6878a978234SLiam Girdwood 			continue;
6888a978234SLiam Girdwood 		}
6898a978234SLiam Girdwood 
6908a978234SLiam Girdwood 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
6918a978234SLiam Girdwood 	}
6928a978234SLiam Girdwood 	return 0;
6938a978234SLiam Girdwood 
6948a978234SLiam Girdwood }
6958a978234SLiam Girdwood 
6968a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
6978a978234SLiam Girdwood 	size_t size)
6988a978234SLiam Girdwood {
6998a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
7008a978234SLiam Girdwood 	struct soc_mixer_control *sm;
7018a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
7028a978234SLiam Girdwood 	int i, err;
7038a978234SLiam Girdwood 
7048a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
7058a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_mixer_control),
7068a978234SLiam Girdwood 		count, size, "mixers")) {
7078a978234SLiam Girdwood 
7088a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
7098a978234SLiam Girdwood 			count);
7108a978234SLiam Girdwood 		return -EINVAL;
7118a978234SLiam Girdwood 	}
7128a978234SLiam Girdwood 
7138a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
7148a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
7158a978234SLiam Girdwood 
7168a978234SLiam Girdwood 		/* validate kcontrol */
7178a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
7188a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
7198a978234SLiam Girdwood 			return -EINVAL;
7208a978234SLiam Girdwood 
7218a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
7228a978234SLiam Girdwood 		if (sm == NULL)
7238a978234SLiam Girdwood 			return -ENOMEM;
7248a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
7258a978234SLiam Girdwood 			mc->priv.size);
7268a978234SLiam Girdwood 
7278a978234SLiam Girdwood 		dev_dbg(tplg->dev,
7288a978234SLiam Girdwood 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
7298a978234SLiam Girdwood 			mc->hdr.name, mc->hdr.access);
7308a978234SLiam Girdwood 
7318a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
7328a978234SLiam Girdwood 		kc.name = mc->hdr.name;
7338a978234SLiam Girdwood 		kc.private_value = (long)sm;
7348a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
7358a978234SLiam Girdwood 		kc.access = mc->hdr.access;
7368a978234SLiam Girdwood 
7378a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
7388a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
7398a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
7408a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
7418a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
7428a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
7438a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
7448a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
7458a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
7468a978234SLiam Girdwood 
7478a978234SLiam Girdwood 		sm->max = mc->max;
7488a978234SLiam Girdwood 		sm->min = mc->min;
7498a978234SLiam Girdwood 		sm->invert = mc->invert;
7508a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
7518a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
7528a978234SLiam Girdwood 		sm->dobj.ops = tplg->ops;
7538a978234SLiam Girdwood 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
7548a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
7558a978234SLiam Girdwood 
7568a978234SLiam Girdwood 		/* map io handlers */
7578a978234SLiam Girdwood 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, io_ops,
7588a978234SLiam Girdwood 			ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
7598a978234SLiam Girdwood 		if (err) {
7608a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
7618a978234SLiam Girdwood 			kfree(sm);
7628a978234SLiam Girdwood 			continue;
7638a978234SLiam Girdwood 		}
7648a978234SLiam Girdwood 
7658a978234SLiam Girdwood 		/* pass control to driver for optional further init */
7668a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
7678a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) mc);
7688a978234SLiam Girdwood 		if (err < 0) {
7698a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
7708a978234SLiam Girdwood 				mc->hdr.name);
7718a978234SLiam Girdwood 			kfree(sm);
7728a978234SLiam Girdwood 			continue;
7738a978234SLiam Girdwood 		}
7748a978234SLiam Girdwood 
7758a978234SLiam Girdwood 		/* create any TLV data */
7768a978234SLiam Girdwood 		soc_tplg_create_tlv(tplg, &kc, mc->hdr.tlv_size);
7778a978234SLiam Girdwood 
7788a978234SLiam Girdwood 		/* register control here */
7798a978234SLiam Girdwood 		err = soc_tplg_add_kcontrol(tplg, &kc,
7808a978234SLiam Girdwood 			&sm->dobj.control.kcontrol);
7818a978234SLiam Girdwood 		if (err < 0) {
7828a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
7838a978234SLiam Girdwood 				mc->hdr.name);
7848a978234SLiam Girdwood 			soc_tplg_free_tlv(tplg, &kc);
7858a978234SLiam Girdwood 			kfree(sm);
7868a978234SLiam Girdwood 			continue;
7878a978234SLiam Girdwood 		}
7888a978234SLiam Girdwood 
7898a978234SLiam Girdwood 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
7908a978234SLiam Girdwood 	}
7918a978234SLiam Girdwood 
7928a978234SLiam Girdwood 	return 0;
7938a978234SLiam Girdwood }
7948a978234SLiam Girdwood 
7958a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se,
7968a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
7978a978234SLiam Girdwood {
7988a978234SLiam Girdwood 	int i, ret;
7998a978234SLiam Girdwood 
8008a978234SLiam Girdwood 	se->dobj.control.dtexts =
8018a978234SLiam Girdwood 		kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
8028a978234SLiam Girdwood 	if (se->dobj.control.dtexts == NULL)
8038a978234SLiam Girdwood 		return -ENOMEM;
8048a978234SLiam Girdwood 
8058a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++) {
8068a978234SLiam Girdwood 
8078a978234SLiam Girdwood 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8088a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
8098a978234SLiam Girdwood 			ret = -EINVAL;
8108a978234SLiam Girdwood 			goto err;
8118a978234SLiam Girdwood 		}
8128a978234SLiam Girdwood 
8138a978234SLiam Girdwood 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
8148a978234SLiam Girdwood 		if (!se->dobj.control.dtexts[i]) {
8158a978234SLiam Girdwood 			ret = -ENOMEM;
8168a978234SLiam Girdwood 			goto err;
8178a978234SLiam Girdwood 		}
8188a978234SLiam Girdwood 	}
8198a978234SLiam Girdwood 
8208a978234SLiam Girdwood 	return 0;
8218a978234SLiam Girdwood 
8228a978234SLiam Girdwood err:
8238a978234SLiam Girdwood 	for (--i; i >= 0; i--)
8248a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
8258a978234SLiam Girdwood 	kfree(se->dobj.control.dtexts);
8268a978234SLiam Girdwood 	return ret;
8278a978234SLiam Girdwood }
8288a978234SLiam Girdwood 
8298a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se,
8308a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec)
8318a978234SLiam Girdwood {
8328a978234SLiam Girdwood 	if (ec->items > sizeof(*ec->values))
8338a978234SLiam Girdwood 		return -EINVAL;
8348a978234SLiam Girdwood 
8358a978234SLiam Girdwood 	se->dobj.control.dvalues =
8368a978234SLiam Girdwood 		kmalloc(ec->items * sizeof(u32), GFP_KERNEL);
8378a978234SLiam Girdwood 	if (!se->dobj.control.dvalues)
8388a978234SLiam Girdwood 		return -ENOMEM;
8398a978234SLiam Girdwood 
8408a978234SLiam Girdwood 	memcpy(se->dobj.control.dvalues, ec->values, ec->items * sizeof(u32));
8418a978234SLiam Girdwood 	return 0;
8428a978234SLiam Girdwood }
8438a978234SLiam Girdwood 
8448a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
8458a978234SLiam Girdwood 	size_t size)
8468a978234SLiam Girdwood {
8478a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
8488a978234SLiam Girdwood 	struct soc_enum *se;
8498a978234SLiam Girdwood 	struct snd_kcontrol_new kc;
8508a978234SLiam Girdwood 	int i, ret, err;
8518a978234SLiam Girdwood 
8528a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
8538a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_enum_control),
8548a978234SLiam Girdwood 		count, size, "enums")) {
8558a978234SLiam Girdwood 
8568a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
8578a978234SLiam Girdwood 			count);
8588a978234SLiam Girdwood 		return -EINVAL;
8598a978234SLiam Girdwood 	}
8608a978234SLiam Girdwood 
8618a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
8628a978234SLiam Girdwood 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
8638a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
8648a978234SLiam Girdwood 			ec->priv.size);
8658a978234SLiam Girdwood 
8668a978234SLiam Girdwood 		/* validate kcontrol */
8678a978234SLiam Girdwood 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
8688a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
8698a978234SLiam Girdwood 			return -EINVAL;
8708a978234SLiam Girdwood 
8718a978234SLiam Girdwood 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
8728a978234SLiam Girdwood 		if (se == NULL)
8738a978234SLiam Girdwood 			return -ENOMEM;
8748a978234SLiam Girdwood 
8758a978234SLiam Girdwood 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
8768a978234SLiam Girdwood 			ec->hdr.name, ec->items);
8778a978234SLiam Girdwood 
8788a978234SLiam Girdwood 		memset(&kc, 0, sizeof(kc));
8798a978234SLiam Girdwood 		kc.name = ec->hdr.name;
8808a978234SLiam Girdwood 		kc.private_value = (long)se;
8818a978234SLiam Girdwood 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
8828a978234SLiam Girdwood 		kc.access = ec->hdr.access;
8838a978234SLiam Girdwood 
8848a978234SLiam Girdwood 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
8858a978234SLiam Girdwood 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
8868a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8878a978234SLiam Girdwood 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
8888a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
8898a978234SLiam Girdwood 
8908a978234SLiam Girdwood 		se->items = ec->items;
8918a978234SLiam Girdwood 		se->mask = ec->mask;
8928a978234SLiam Girdwood 		se->dobj.index = tplg->index;
8938a978234SLiam Girdwood 		se->dobj.type = SND_SOC_DOBJ_ENUM;
8948a978234SLiam Girdwood 		se->dobj.ops = tplg->ops;
8958a978234SLiam Girdwood 		INIT_LIST_HEAD(&se->dobj.list);
8968a978234SLiam Girdwood 
8978a978234SLiam Girdwood 		switch (ec->hdr.ops.info) {
8988a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
8998a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9008a978234SLiam Girdwood 			err = soc_tplg_denum_create_values(se, ec);
9018a978234SLiam Girdwood 			if (err < 0) {
9028a978234SLiam Girdwood 				dev_err(tplg->dev,
9038a978234SLiam Girdwood 					"ASoC: could not create values for %s\n",
9048a978234SLiam Girdwood 					ec->hdr.name);
9058a978234SLiam Girdwood 				kfree(se);
9068a978234SLiam Girdwood 				continue;
9078a978234SLiam Girdwood 			}
9088a978234SLiam Girdwood 			/* fall through and create texts */
9098a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9108a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9118a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9128a978234SLiam Girdwood 			err = soc_tplg_denum_create_texts(se, ec);
9138a978234SLiam Girdwood 			if (err < 0) {
9148a978234SLiam Girdwood 				dev_err(tplg->dev,
9158a978234SLiam Girdwood 					"ASoC: could not create texts for %s\n",
9168a978234SLiam Girdwood 					ec->hdr.name);
9178a978234SLiam Girdwood 				kfree(se);
9188a978234SLiam Girdwood 				continue;
9198a978234SLiam Girdwood 			}
9208a978234SLiam Girdwood 			break;
9218a978234SLiam Girdwood 		default:
9228a978234SLiam Girdwood 			dev_err(tplg->dev,
9238a978234SLiam Girdwood 				"ASoC: invalid enum control type %d for %s\n",
9248a978234SLiam Girdwood 				ec->hdr.ops.info, ec->hdr.name);
9258a978234SLiam Girdwood 			kfree(se);
9268a978234SLiam Girdwood 			continue;
9278a978234SLiam Girdwood 		}
9288a978234SLiam Girdwood 
9298a978234SLiam Girdwood 		/* map io handlers */
9308a978234SLiam Girdwood 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, io_ops,
9318a978234SLiam Girdwood 			ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
9328a978234SLiam Girdwood 		if (err) {
9338a978234SLiam Girdwood 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
9348a978234SLiam Girdwood 			kfree(se);
9358a978234SLiam Girdwood 			continue;
9368a978234SLiam Girdwood 		}
9378a978234SLiam Girdwood 
9388a978234SLiam Girdwood 		/* pass control to driver for optional further init */
9398a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc,
9408a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *) ec);
9418a978234SLiam Girdwood 		if (err < 0) {
9428a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
9438a978234SLiam Girdwood 				ec->hdr.name);
9448a978234SLiam Girdwood 			kfree(se);
9458a978234SLiam Girdwood 			continue;
9468a978234SLiam Girdwood 		}
9478a978234SLiam Girdwood 
9488a978234SLiam Girdwood 		/* register control here */
9498a978234SLiam Girdwood 		ret = soc_tplg_add_kcontrol(tplg,
9508a978234SLiam Girdwood 			&kc, &se->dobj.control.kcontrol);
9518a978234SLiam Girdwood 		if (ret < 0) {
9528a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
9538a978234SLiam Girdwood 				ec->hdr.name);
9548a978234SLiam Girdwood 			kfree(se);
9558a978234SLiam Girdwood 			continue;
9568a978234SLiam Girdwood 		}
9578a978234SLiam Girdwood 
9588a978234SLiam Girdwood 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
9598a978234SLiam Girdwood 	}
9608a978234SLiam Girdwood 
9618a978234SLiam Girdwood 	return 0;
9628a978234SLiam Girdwood }
9638a978234SLiam Girdwood 
9648a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
9658a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
9668a978234SLiam Girdwood {
9678a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
9688a978234SLiam Girdwood 	int i;
9698a978234SLiam Girdwood 
9708a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
9718a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
9728a978234SLiam Girdwood 		return 0;
9738a978234SLiam Girdwood 	}
9748a978234SLiam Girdwood 
9758a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
9768a978234SLiam Girdwood 		soc_tplg_get_offset(tplg));
9778a978234SLiam Girdwood 
9788a978234SLiam Girdwood 	for (i = 0; i < hdr->count; i++) {
9798a978234SLiam Girdwood 
9808a978234SLiam Girdwood 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
9818a978234SLiam Girdwood 
9828a978234SLiam Girdwood 		switch (control_hdr->ops.info) {
9838a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW:
9848a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_STROBE:
9858a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_SX:
9868a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
9878a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_RANGE:
9888a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
9898a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_PIN:
9908a978234SLiam Girdwood 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
9918a978234SLiam Girdwood 			break;
9928a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM:
9938a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
9948a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
9958a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
9968a978234SLiam Girdwood 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
9978a978234SLiam Girdwood 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
9988a978234SLiam Girdwood 			break;
9998a978234SLiam Girdwood 		case SND_SOC_TPLG_CTL_BYTES:
10008a978234SLiam Girdwood 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
10018a978234SLiam Girdwood 			break;
10028a978234SLiam Girdwood 		default:
10038a978234SLiam Girdwood 			soc_bind_err(tplg, control_hdr, i);
10048a978234SLiam Girdwood 			return -EINVAL;
10058a978234SLiam Girdwood 		}
10068a978234SLiam Girdwood 	}
10078a978234SLiam Girdwood 
10088a978234SLiam Girdwood 	return 0;
10098a978234SLiam Girdwood }
10108a978234SLiam Girdwood 
10118a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
10128a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
10138a978234SLiam Girdwood {
10148a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
10158a978234SLiam Girdwood 	struct snd_soc_dapm_route route;
10168a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_graph_elem *elem;
10178a978234SLiam Girdwood 	int count = hdr->count, i;
10188a978234SLiam Girdwood 
10198a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
10208a978234SLiam Girdwood 		tplg->pos += hdr->size + hdr->payload_size;
10218a978234SLiam Girdwood 		return 0;
10228a978234SLiam Girdwood 	}
10238a978234SLiam Girdwood 
10248a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
10258a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
10268a978234SLiam Girdwood 		count, hdr->payload_size, "graph")) {
10278a978234SLiam Girdwood 
10288a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
10298a978234SLiam Girdwood 			count);
10308a978234SLiam Girdwood 		return -EINVAL;
10318a978234SLiam Girdwood 	}
10328a978234SLiam Girdwood 
10338a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
10348a978234SLiam Girdwood 
10358a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
10368a978234SLiam Girdwood 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
10378a978234SLiam Girdwood 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
10388a978234SLiam Girdwood 
10398a978234SLiam Girdwood 		/* validate routes */
10408a978234SLiam Girdwood 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10418a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10428a978234SLiam Girdwood 			return -EINVAL;
10438a978234SLiam Girdwood 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10448a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10458a978234SLiam Girdwood 			return -EINVAL;
10468a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10478a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10488a978234SLiam Girdwood 			return -EINVAL;
10498a978234SLiam Girdwood 
10508a978234SLiam Girdwood 		route.source = elem->source;
10518a978234SLiam Girdwood 		route.sink = elem->sink;
10528a978234SLiam Girdwood 		route.connected = NULL; /* set to NULL atm for tplg users */
10538a978234SLiam Girdwood 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
10548a978234SLiam Girdwood 			route.control = NULL;
10558a978234SLiam Girdwood 		else
10568a978234SLiam Girdwood 			route.control = elem->control;
10578a978234SLiam Girdwood 
10588a978234SLiam Girdwood 		/* add route, but keep going if some fail */
10598a978234SLiam Girdwood 		snd_soc_dapm_add_routes(dapm, &route, 1);
10608a978234SLiam Girdwood 	}
10618a978234SLiam Girdwood 
10628a978234SLiam Girdwood 	return 0;
10638a978234SLiam Girdwood }
10648a978234SLiam Girdwood 
10658a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
10668a978234SLiam Girdwood 	struct soc_tplg *tplg, int num_kcontrols)
10678a978234SLiam Girdwood {
10688a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
10698a978234SLiam Girdwood 	struct soc_mixer_control *sm;
10708a978234SLiam Girdwood 	struct snd_soc_tplg_mixer_control *mc;
10718a978234SLiam Girdwood 	int i, err;
10728a978234SLiam Girdwood 
1073*4ca7deb1SAxel Lin 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
10748a978234SLiam Girdwood 	if (kc == NULL)
10758a978234SLiam Girdwood 		return NULL;
10768a978234SLiam Girdwood 
10778a978234SLiam Girdwood 	for (i = 0; i < num_kcontrols; i++) {
10788a978234SLiam Girdwood 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
10798a978234SLiam Girdwood 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
10808a978234SLiam Girdwood 		if (sm == NULL)
10818a978234SLiam Girdwood 			goto err;
10828a978234SLiam Girdwood 
10838a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
10848a978234SLiam Girdwood 			mc->priv.size);
10858a978234SLiam Girdwood 
10868a978234SLiam Girdwood 		/* validate kcontrol */
10878a978234SLiam Girdwood 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
10888a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
10898a978234SLiam Girdwood 			goto err_str;
10908a978234SLiam Girdwood 
10918a978234SLiam Girdwood 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
10928a978234SLiam Girdwood 			mc->hdr.name, i);
10938a978234SLiam Girdwood 
10948a978234SLiam Girdwood 		kc[i].name = mc->hdr.name;
10958a978234SLiam Girdwood 		kc[i].private_value = (long)sm;
10968a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
10978a978234SLiam Girdwood 		kc[i].access = mc->hdr.access;
10988a978234SLiam Girdwood 
10998a978234SLiam Girdwood 		/* we only support FL/FR channel mapping atm */
11008a978234SLiam Girdwood 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
11018a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11028a978234SLiam Girdwood 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
11038a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11048a978234SLiam Girdwood 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
11058a978234SLiam Girdwood 			SNDRV_CHMAP_FL);
11068a978234SLiam Girdwood 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
11078a978234SLiam Girdwood 			SNDRV_CHMAP_FR);
11088a978234SLiam Girdwood 
11098a978234SLiam Girdwood 		sm->max = mc->max;
11108a978234SLiam Girdwood 		sm->min = mc->min;
11118a978234SLiam Girdwood 		sm->invert = mc->invert;
11128a978234SLiam Girdwood 		sm->platform_max = mc->platform_max;
11138a978234SLiam Girdwood 		sm->dobj.index = tplg->index;
11148a978234SLiam Girdwood 		INIT_LIST_HEAD(&sm->dobj.list);
11158a978234SLiam Girdwood 
11168a978234SLiam Girdwood 		/* map io handlers */
11178a978234SLiam Girdwood 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], io_ops,
11188a978234SLiam Girdwood 			ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
11198a978234SLiam Girdwood 		if (err) {
11208a978234SLiam Girdwood 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
11218a978234SLiam Girdwood 			kfree(sm);
11228a978234SLiam Girdwood 			continue;
11238a978234SLiam Girdwood 		}
11248a978234SLiam Girdwood 
11258a978234SLiam Girdwood 		/* pass control to driver for optional further init */
11268a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
11278a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)mc);
11288a978234SLiam Girdwood 		if (err < 0) {
11298a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
11308a978234SLiam Girdwood 				mc->hdr.name);
11318a978234SLiam Girdwood 			kfree(sm);
11328a978234SLiam Girdwood 			continue;
11338a978234SLiam Girdwood 		}
11348a978234SLiam Girdwood 	}
11358a978234SLiam Girdwood 	return kc;
11368a978234SLiam Girdwood 
11378a978234SLiam Girdwood err_str:
11388a978234SLiam Girdwood 	kfree(sm);
11398a978234SLiam Girdwood err:
11408a978234SLiam Girdwood 	for (--i; i >= 0; i--)
11418a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
11428a978234SLiam Girdwood 	kfree(kc);
11438a978234SLiam Girdwood 	return NULL;
11448a978234SLiam Girdwood }
11458a978234SLiam Girdwood 
11468a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
11478a978234SLiam Girdwood 	struct soc_tplg *tplg)
11488a978234SLiam Girdwood {
11498a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
11508a978234SLiam Girdwood 	struct snd_soc_tplg_enum_control *ec;
11518a978234SLiam Girdwood 	struct soc_enum *se;
11528a978234SLiam Girdwood 	int i, err;
11538a978234SLiam Girdwood 
11548a978234SLiam Girdwood 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
11558a978234SLiam Girdwood 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
11568a978234SLiam Girdwood 		ec->priv.size);
11578a978234SLiam Girdwood 
11588a978234SLiam Girdwood 	/* validate kcontrol */
11598a978234SLiam Girdwood 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
11608a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
11618a978234SLiam Girdwood 		return NULL;
11628a978234SLiam Girdwood 
11638a978234SLiam Girdwood 	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
11648a978234SLiam Girdwood 	if (kc == NULL)
11658a978234SLiam Girdwood 		return NULL;
11668a978234SLiam Girdwood 
11678a978234SLiam Girdwood 	se = kzalloc(sizeof(*se), GFP_KERNEL);
11688a978234SLiam Girdwood 	if (se == NULL)
11698a978234SLiam Girdwood 		goto err;
11708a978234SLiam Girdwood 
11718a978234SLiam Girdwood 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
11728a978234SLiam Girdwood 		ec->hdr.name);
11738a978234SLiam Girdwood 
11748a978234SLiam Girdwood 	kc->name = ec->hdr.name;
11758a978234SLiam Girdwood 	kc->private_value = (long)se;
11768a978234SLiam Girdwood 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
11778a978234SLiam Girdwood 	kc->access = ec->hdr.access;
11788a978234SLiam Girdwood 
11798a978234SLiam Girdwood 	/* we only support FL/FR channel mapping atm */
11808a978234SLiam Girdwood 	se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
11818a978234SLiam Girdwood 	se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
11828a978234SLiam Girdwood 	se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
11838a978234SLiam Girdwood 
11848a978234SLiam Girdwood 	se->items = ec->items;
11858a978234SLiam Girdwood 	se->mask = ec->mask;
11868a978234SLiam Girdwood 	se->dobj.index = tplg->index;
11878a978234SLiam Girdwood 
11888a978234SLiam Girdwood 	switch (ec->hdr.ops.info) {
11898a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
11908a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
11918a978234SLiam Girdwood 		err = soc_tplg_denum_create_values(se, ec);
11928a978234SLiam Girdwood 		if (err < 0) {
11938a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
11948a978234SLiam Girdwood 				ec->hdr.name);
11958a978234SLiam Girdwood 			goto err_se;
11968a978234SLiam Girdwood 		}
11978a978234SLiam Girdwood 		/* fall through to create texts */
11988a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
11998a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
12008a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
12018a978234SLiam Girdwood 		err = soc_tplg_denum_create_texts(se, ec);
12028a978234SLiam Girdwood 		if (err < 0) {
12038a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
12048a978234SLiam Girdwood 				ec->hdr.name);
12058a978234SLiam Girdwood 			goto err_se;
12068a978234SLiam Girdwood 		}
12078a978234SLiam Girdwood 		break;
12088a978234SLiam Girdwood 	default:
12098a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
12108a978234SLiam Girdwood 			ec->hdr.ops.info, ec->hdr.name);
12118a978234SLiam Girdwood 		goto err_se;
12128a978234SLiam Girdwood 	}
12138a978234SLiam Girdwood 
12148a978234SLiam Girdwood 	/* map io handlers */
12158a978234SLiam Girdwood 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, io_ops,
12168a978234SLiam Girdwood 		ARRAY_SIZE(io_ops), tplg->io_ops, tplg->io_ops_count);
12178a978234SLiam Girdwood 	if (err) {
12188a978234SLiam Girdwood 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
12198a978234SLiam Girdwood 		goto err_se;
12208a978234SLiam Girdwood 	}
12218a978234SLiam Girdwood 
12228a978234SLiam Girdwood 	/* pass control to driver for optional further init */
12238a978234SLiam Girdwood 	err = soc_tplg_init_kcontrol(tplg, kc,
12248a978234SLiam Girdwood 		(struct snd_soc_tplg_ctl_hdr *)ec);
12258a978234SLiam Girdwood 	if (err < 0) {
12268a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
12278a978234SLiam Girdwood 			ec->hdr.name);
12288a978234SLiam Girdwood 		goto err_se;
12298a978234SLiam Girdwood 	}
12308a978234SLiam Girdwood 
12318a978234SLiam Girdwood 	return kc;
12328a978234SLiam Girdwood 
12338a978234SLiam Girdwood err_se:
12348a978234SLiam Girdwood 	/* free values and texts */
12358a978234SLiam Girdwood 	kfree(se->dobj.control.dvalues);
12368a978234SLiam Girdwood 	for (i = 0; i < ec->items; i++)
12378a978234SLiam Girdwood 		kfree(se->dobj.control.dtexts[i]);
12388a978234SLiam Girdwood 
12398a978234SLiam Girdwood 	kfree(se);
12408a978234SLiam Girdwood err:
12418a978234SLiam Girdwood 	kfree(kc);
12428a978234SLiam Girdwood 
12438a978234SLiam Girdwood 	return NULL;
12448a978234SLiam Girdwood }
12458a978234SLiam Girdwood 
12468a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
12478a978234SLiam Girdwood 	struct soc_tplg *tplg, int count)
12488a978234SLiam Girdwood {
12498a978234SLiam Girdwood 	struct snd_soc_tplg_bytes_control *be;
12508a978234SLiam Girdwood 	struct soc_bytes_ext  *sbe;
12518a978234SLiam Girdwood 	struct snd_kcontrol_new *kc;
12528a978234SLiam Girdwood 	int i, err;
12538a978234SLiam Girdwood 
1254*4ca7deb1SAxel Lin 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
12558a978234SLiam Girdwood 	if (!kc)
12568a978234SLiam Girdwood 		return NULL;
12578a978234SLiam Girdwood 
12588a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
12598a978234SLiam Girdwood 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
12608a978234SLiam Girdwood 
12618a978234SLiam Girdwood 		/* validate kcontrol */
12628a978234SLiam Girdwood 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
12638a978234SLiam Girdwood 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
12648a978234SLiam Girdwood 			goto err;
12658a978234SLiam Girdwood 
12668a978234SLiam Girdwood 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
12678a978234SLiam Girdwood 		if (sbe == NULL)
12688a978234SLiam Girdwood 			goto err;
12698a978234SLiam Girdwood 
12708a978234SLiam Girdwood 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
12718a978234SLiam Girdwood 			be->priv.size);
12728a978234SLiam Girdwood 
12738a978234SLiam Girdwood 		dev_dbg(tplg->dev,
12748a978234SLiam Girdwood 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
12758a978234SLiam Girdwood 			be->hdr.name, be->hdr.access);
12768a978234SLiam Girdwood 
12778a978234SLiam Girdwood 		kc[i].name = be->hdr.name;
12788a978234SLiam Girdwood 		kc[i].private_value = (long)sbe;
12798a978234SLiam Girdwood 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
12808a978234SLiam Girdwood 		kc[i].access = be->hdr.access;
12818a978234SLiam Girdwood 
12828a978234SLiam Girdwood 		sbe->max = be->max;
12838a978234SLiam Girdwood 		INIT_LIST_HEAD(&sbe->dobj.list);
12848a978234SLiam Girdwood 
12858a978234SLiam Girdwood 		/* map standard io handlers and check for external handlers */
12868a978234SLiam Girdwood 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], io_ops,
12878a978234SLiam Girdwood 				ARRAY_SIZE(io_ops), tplg->io_ops,
12888a978234SLiam Girdwood 				tplg->io_ops_count);
12898a978234SLiam Girdwood 		if (err) {
12908a978234SLiam Girdwood 			soc_control_err(tplg, &be->hdr, be->hdr.name);
12918a978234SLiam Girdwood 			kfree(sbe);
12928a978234SLiam Girdwood 			continue;
12938a978234SLiam Girdwood 		}
12948a978234SLiam Girdwood 
12958a978234SLiam Girdwood 		/* pass control to driver for optional further init */
12968a978234SLiam Girdwood 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
12978a978234SLiam Girdwood 			(struct snd_soc_tplg_ctl_hdr *)be);
12988a978234SLiam Girdwood 		if (err < 0) {
12998a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
13008a978234SLiam Girdwood 				be->hdr.name);
13018a978234SLiam Girdwood 			kfree(sbe);
13028a978234SLiam Girdwood 			continue;
13038a978234SLiam Girdwood 		}
13048a978234SLiam Girdwood 	}
13058a978234SLiam Girdwood 
13068a978234SLiam Girdwood 	return kc;
13078a978234SLiam Girdwood 
13088a978234SLiam Girdwood err:
13098a978234SLiam Girdwood 	for (--i; i >= 0; i--)
13108a978234SLiam Girdwood 		kfree((void *)kc[i].private_value);
13118a978234SLiam Girdwood 
13128a978234SLiam Girdwood 	kfree(kc);
13138a978234SLiam Girdwood 	return NULL;
13148a978234SLiam Girdwood }
13158a978234SLiam Girdwood 
13168a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
13178a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *w)
13188a978234SLiam Girdwood {
13198a978234SLiam Girdwood 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
13208a978234SLiam Girdwood 	struct snd_soc_dapm_widget template, *widget;
13218a978234SLiam Girdwood 	struct snd_soc_tplg_ctl_hdr *control_hdr;
13228a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
13238a978234SLiam Girdwood 	int ret = 0;
13248a978234SLiam Girdwood 
13258a978234SLiam Girdwood 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13268a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13278a978234SLiam Girdwood 		return -EINVAL;
13288a978234SLiam Girdwood 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
13298a978234SLiam Girdwood 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
13308a978234SLiam Girdwood 		return -EINVAL;
13318a978234SLiam Girdwood 
13328a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
13338a978234SLiam Girdwood 		w->name, w->id);
13348a978234SLiam Girdwood 
13358a978234SLiam Girdwood 	memset(&template, 0, sizeof(template));
13368a978234SLiam Girdwood 
13378a978234SLiam Girdwood 	/* map user to kernel widget ID */
13388a978234SLiam Girdwood 	template.id = get_widget_id(w->id);
13398a978234SLiam Girdwood 	if (template.id < 0)
13408a978234SLiam Girdwood 		return template.id;
13418a978234SLiam Girdwood 
13428a978234SLiam Girdwood 	template.name = kstrdup(w->name, GFP_KERNEL);
13438a978234SLiam Girdwood 	if (!template.name)
13448a978234SLiam Girdwood 		return -ENOMEM;
13458a978234SLiam Girdwood 	template.sname = kstrdup(w->sname, GFP_KERNEL);
13468a978234SLiam Girdwood 	if (!template.sname) {
13478a978234SLiam Girdwood 		ret = -ENOMEM;
13488a978234SLiam Girdwood 		goto err;
13498a978234SLiam Girdwood 	}
13508a978234SLiam Girdwood 	template.reg = w->reg;
13518a978234SLiam Girdwood 	template.shift = w->shift;
13528a978234SLiam Girdwood 	template.mask = w->mask;
13538a978234SLiam Girdwood 	template.on_val = w->invert ? 0 : 1;
13548a978234SLiam Girdwood 	template.off_val = w->invert ? 1 : 0;
13558a978234SLiam Girdwood 	template.ignore_suspend = w->ignore_suspend;
13568a978234SLiam Girdwood 	template.event_flags = w->event_flags;
13578a978234SLiam Girdwood 	template.dobj.index = tplg->index;
13588a978234SLiam Girdwood 
13598a978234SLiam Girdwood 	tplg->pos +=
13608a978234SLiam Girdwood 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
13618a978234SLiam Girdwood 	if (w->num_kcontrols == 0) {
13628a978234SLiam Girdwood 		template.num_kcontrols = 0;
13638a978234SLiam Girdwood 		goto widget;
13648a978234SLiam Girdwood 	}
13658a978234SLiam Girdwood 
13668a978234SLiam Girdwood 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
13678a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
13688a978234SLiam Girdwood 		w->name, w->num_kcontrols, control_hdr->type);
13698a978234SLiam Girdwood 
13708a978234SLiam Girdwood 	switch (control_hdr->ops.info) {
13718a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW:
13728a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_STROBE:
13738a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_SX:
13748a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
13758a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_RANGE:
13768a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
13778a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
13788a978234SLiam Girdwood 		template.kcontrol_news =
13798a978234SLiam Girdwood 			soc_tplg_dapm_widget_dmixer_create(tplg,
13808a978234SLiam Girdwood 			template.num_kcontrols);
13818a978234SLiam Girdwood 		if (!template.kcontrol_news) {
13828a978234SLiam Girdwood 			ret = -ENOMEM;
13838a978234SLiam Girdwood 			goto hdr_err;
13848a978234SLiam Girdwood 		}
13858a978234SLiam Girdwood 		break;
13868a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM:
13878a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
13888a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
13898a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
13908a978234SLiam Girdwood 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
13918a978234SLiam Girdwood 		template.dobj.widget.kcontrol_enum = 1;
13928a978234SLiam Girdwood 		template.num_kcontrols = 1;
13938a978234SLiam Girdwood 		template.kcontrol_news =
13948a978234SLiam Girdwood 			soc_tplg_dapm_widget_denum_create(tplg);
13958a978234SLiam Girdwood 		if (!template.kcontrol_news) {
13968a978234SLiam Girdwood 			ret = -ENOMEM;
13978a978234SLiam Girdwood 			goto hdr_err;
13988a978234SLiam Girdwood 		}
13998a978234SLiam Girdwood 		break;
14008a978234SLiam Girdwood 	case SND_SOC_TPLG_CTL_BYTES:
14018a978234SLiam Girdwood 		template.num_kcontrols = w->num_kcontrols;
14028a978234SLiam Girdwood 		template.kcontrol_news =
14038a978234SLiam Girdwood 			soc_tplg_dapm_widget_dbytes_create(tplg,
14048a978234SLiam Girdwood 				template.num_kcontrols);
14058a978234SLiam Girdwood 		if (!template.kcontrol_news) {
14068a978234SLiam Girdwood 			ret = -ENOMEM;
14078a978234SLiam Girdwood 			goto hdr_err;
14088a978234SLiam Girdwood 		}
14098a978234SLiam Girdwood 		break;
14108a978234SLiam Girdwood 	default:
14118a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
14128a978234SLiam Girdwood 			control_hdr->ops.get, control_hdr->ops.put,
14138a978234SLiam Girdwood 			control_hdr->ops.info);
14148a978234SLiam Girdwood 		ret = -EINVAL;
14158a978234SLiam Girdwood 		goto hdr_err;
14168a978234SLiam Girdwood 	}
14178a978234SLiam Girdwood 
14188a978234SLiam Girdwood widget:
14198a978234SLiam Girdwood 	ret = soc_tplg_widget_load(tplg, &template, w);
14208a978234SLiam Girdwood 	if (ret < 0)
14218a978234SLiam Girdwood 		goto hdr_err;
14228a978234SLiam Girdwood 
14238a978234SLiam Girdwood 	/* card dapm mutex is held by the core if we are loading topology
14248a978234SLiam Girdwood 	 * data during sound card init. */
14258a978234SLiam Girdwood 	if (card->instantiated)
14268a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control(dapm, &template);
14278a978234SLiam Girdwood 	else
14288a978234SLiam Girdwood 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
14298a978234SLiam Girdwood 	if (widget == NULL) {
14308a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
14318a978234SLiam Girdwood 			w->name);
14328a978234SLiam Girdwood 		goto hdr_err;
14338a978234SLiam Girdwood 	}
14348a978234SLiam Girdwood 
14358a978234SLiam Girdwood 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
14368a978234SLiam Girdwood 	widget->dobj.ops = tplg->ops;
14378a978234SLiam Girdwood 	widget->dobj.index = tplg->index;
14388a978234SLiam Girdwood 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
14398a978234SLiam Girdwood 	return 0;
14408a978234SLiam Girdwood 
14418a978234SLiam Girdwood hdr_err:
14428a978234SLiam Girdwood 	kfree(template.sname);
14438a978234SLiam Girdwood err:
14448a978234SLiam Girdwood 	kfree(template.name);
14458a978234SLiam Girdwood 	return ret;
14468a978234SLiam Girdwood }
14478a978234SLiam Girdwood 
14488a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
14498a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
14508a978234SLiam Girdwood {
14518a978234SLiam Girdwood 	struct snd_soc_tplg_dapm_widget *widget;
14528a978234SLiam Girdwood 	int ret, count = hdr->count, i;
14538a978234SLiam Girdwood 
14548a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
14558a978234SLiam Girdwood 		return 0;
14568a978234SLiam Girdwood 
14578a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
14588a978234SLiam Girdwood 
14598a978234SLiam Girdwood 	for (i = 0; i < count; i++) {
14608a978234SLiam Girdwood 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
14618a978234SLiam Girdwood 		ret = soc_tplg_dapm_widget_create(tplg, widget);
14628a978234SLiam Girdwood 		if (ret < 0)
14638a978234SLiam Girdwood 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
14648a978234SLiam Girdwood 				widget->name);
14658a978234SLiam Girdwood 	}
14668a978234SLiam Girdwood 
14678a978234SLiam Girdwood 	return 0;
14688a978234SLiam Girdwood }
14698a978234SLiam Girdwood 
14708a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
14718a978234SLiam Girdwood {
14728a978234SLiam Girdwood 	struct snd_soc_card *card = tplg->comp->card;
14738a978234SLiam Girdwood 	int ret;
14748a978234SLiam Girdwood 
14758a978234SLiam Girdwood 	/* Card might not have been registered at this point.
14768a978234SLiam Girdwood 	 * If so, just return success.
14778a978234SLiam Girdwood 	*/
14788a978234SLiam Girdwood 	if (!card || !card->instantiated) {
14798a978234SLiam Girdwood 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
14808a978234SLiam Girdwood 				"Do not add new widgets now\n");
14818a978234SLiam Girdwood 		return 0;
14828a978234SLiam Girdwood 	}
14838a978234SLiam Girdwood 
14848a978234SLiam Girdwood 	ret = snd_soc_dapm_new_widgets(card);
14858a978234SLiam Girdwood 	if (ret < 0)
14868a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
14878a978234SLiam Girdwood 			ret);
14888a978234SLiam Girdwood 
14898a978234SLiam Girdwood 	return 0;
14908a978234SLiam Girdwood }
14918a978234SLiam Girdwood 
14928a978234SLiam Girdwood static int soc_tplg_pcm_dai_elems_load(struct soc_tplg *tplg,
14938a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
14948a978234SLiam Girdwood {
14958a978234SLiam Girdwood 	struct snd_soc_tplg_pcm_dai *pcm_dai;
14968a978234SLiam Girdwood 	struct snd_soc_dobj *dobj;
14978a978234SLiam Girdwood 	int count = hdr->count;
14988a978234SLiam Girdwood 	int ret;
14998a978234SLiam Girdwood 
15008a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
15018a978234SLiam Girdwood 		return 0;
15028a978234SLiam Girdwood 
15038a978234SLiam Girdwood 	pcm_dai = (struct snd_soc_tplg_pcm_dai *)tplg->pos;
15048a978234SLiam Girdwood 
15058a978234SLiam Girdwood 	if (soc_tplg_check_elem_count(tplg,
15068a978234SLiam Girdwood 		sizeof(struct snd_soc_tplg_pcm_dai), count,
15078a978234SLiam Girdwood 		hdr->payload_size, "PCM DAI")) {
15088a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
15098a978234SLiam Girdwood 			count);
15108a978234SLiam Girdwood 		return -EINVAL;
15118a978234SLiam Girdwood 	}
15128a978234SLiam Girdwood 
15138a978234SLiam Girdwood 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
15148a978234SLiam Girdwood 	tplg->pos += sizeof(struct snd_soc_tplg_pcm_dai) * count;
15158a978234SLiam Girdwood 
15168a978234SLiam Girdwood 	dobj = kzalloc(sizeof(struct snd_soc_dobj), GFP_KERNEL);
15178a978234SLiam Girdwood 	if (dobj == NULL)
15188a978234SLiam Girdwood 		return -ENOMEM;
15198a978234SLiam Girdwood 
15208a978234SLiam Girdwood 	/* Call the platform driver call back to register the dais */
15218a978234SLiam Girdwood 	ret = soc_tplg_pcm_dai_load(tplg, pcm_dai, count);
15228a978234SLiam Girdwood 	if (ret < 0) {
15238a978234SLiam Girdwood 		dev_err(tplg->comp->dev, "ASoC: PCM DAI loading failed\n");
15248a978234SLiam Girdwood 		goto err;
15258a978234SLiam Girdwood 	}
15268a978234SLiam Girdwood 
15278a978234SLiam Girdwood 	dobj->type = get_dobj_type(hdr, NULL);
15288a978234SLiam Girdwood 	dobj->pcm_dai.count = count;
15298a978234SLiam Girdwood 	dobj->pcm_dai.pd = pcm_dai;
15308a978234SLiam Girdwood 	dobj->ops = tplg->ops;
15318a978234SLiam Girdwood 	dobj->index = tplg->index;
15328a978234SLiam Girdwood 	list_add(&dobj->list, &tplg->comp->dobj_list);
15338a978234SLiam Girdwood 	return 0;
15348a978234SLiam Girdwood 
15358a978234SLiam Girdwood err:
15368a978234SLiam Girdwood 	kfree(dobj);
15378a978234SLiam Girdwood 	return ret;
15388a978234SLiam Girdwood }
15398a978234SLiam Girdwood 
15408a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg,
15418a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
15428a978234SLiam Girdwood {
15438a978234SLiam Girdwood 	struct snd_soc_tplg_manifest *manifest;
15448a978234SLiam Girdwood 
15458a978234SLiam Girdwood 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
15468a978234SLiam Girdwood 		return 0;
15478a978234SLiam Girdwood 
15488a978234SLiam Girdwood 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
15498a978234SLiam Girdwood 	tplg->pos += sizeof(struct snd_soc_tplg_manifest);
15508a978234SLiam Girdwood 
15518a978234SLiam Girdwood 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
15528a978234SLiam Girdwood 		return tplg->ops->manifest(tplg->comp, manifest);
15538a978234SLiam Girdwood 
15548a978234SLiam Girdwood 	dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
15558a978234SLiam Girdwood 	return 0;
15568a978234SLiam Girdwood }
15578a978234SLiam Girdwood 
15588a978234SLiam Girdwood /* validate header magic, size and type */
15598a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg,
15608a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
15618a978234SLiam Girdwood {
15628a978234SLiam Girdwood 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
15638a978234SLiam Girdwood 		return 0;
15648a978234SLiam Girdwood 
15658a978234SLiam Girdwood 	/* big endian firmware objects not supported atm */
15668a978234SLiam Girdwood 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
15678a978234SLiam Girdwood 		dev_err(tplg->dev,
15688a978234SLiam Girdwood 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
15698a978234SLiam Girdwood 			tplg->pass, hdr->magic,
15708a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
15718a978234SLiam Girdwood 		return -EINVAL;
15728a978234SLiam Girdwood 	}
15738a978234SLiam Girdwood 
15748a978234SLiam Girdwood 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
15758a978234SLiam Girdwood 		dev_err(tplg->dev,
15768a978234SLiam Girdwood 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
15778a978234SLiam Girdwood 			tplg->pass, hdr->magic,
15788a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
15798a978234SLiam Girdwood 		return -EINVAL;
15808a978234SLiam Girdwood 	}
15818a978234SLiam Girdwood 
15828a978234SLiam Girdwood 	if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
15838a978234SLiam Girdwood 		dev_err(tplg->dev,
15848a978234SLiam Girdwood 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
15858a978234SLiam Girdwood 			tplg->pass, hdr->abi,
15868a978234SLiam Girdwood 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
15878a978234SLiam Girdwood 			tplg->fw->size);
15888a978234SLiam Girdwood 		return -EINVAL;
15898a978234SLiam Girdwood 	}
15908a978234SLiam Girdwood 
15918a978234SLiam Girdwood 	if (hdr->payload_size == 0) {
15928a978234SLiam Girdwood 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
15938a978234SLiam Girdwood 			soc_tplg_get_hdr_offset(tplg));
15948a978234SLiam Girdwood 		return -EINVAL;
15958a978234SLiam Girdwood 	}
15968a978234SLiam Girdwood 
15978a978234SLiam Girdwood 	if (tplg->pass == hdr->type)
15988a978234SLiam Girdwood 		dev_dbg(tplg->dev,
15998a978234SLiam Girdwood 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
16008a978234SLiam Girdwood 			hdr->payload_size, hdr->type, hdr->version,
16018a978234SLiam Girdwood 			hdr->vendor_type, tplg->pass);
16028a978234SLiam Girdwood 
16038a978234SLiam Girdwood 	return 1;
16048a978234SLiam Girdwood }
16058a978234SLiam Girdwood 
16068a978234SLiam Girdwood /* check header type and call appropriate handler */
16078a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg,
16088a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr)
16098a978234SLiam Girdwood {
16108a978234SLiam Girdwood 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
16118a978234SLiam Girdwood 
16128a978234SLiam Girdwood 	/* check for matching ID */
16138a978234SLiam Girdwood 	if (hdr->index != tplg->req_index &&
16148a978234SLiam Girdwood 		hdr->index != SND_SOC_TPLG_INDEX_ALL)
16158a978234SLiam Girdwood 		return 0;
16168a978234SLiam Girdwood 
16178a978234SLiam Girdwood 	tplg->index = hdr->index;
16188a978234SLiam Girdwood 
16198a978234SLiam Girdwood 	switch (hdr->type) {
16208a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MIXER:
16218a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_ENUM:
16228a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_BYTES:
16238a978234SLiam Girdwood 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
16248a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
16258a978234SLiam Girdwood 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
16268a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
16278a978234SLiam Girdwood 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
16288a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_PCM:
16298a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_DAI_LINK:
16308a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_CODEC_LINK:
16318a978234SLiam Girdwood 		return soc_tplg_pcm_dai_elems_load(tplg, hdr);
16328a978234SLiam Girdwood 	case SND_SOC_TPLG_TYPE_MANIFEST:
16338a978234SLiam Girdwood 		return soc_tplg_manifest_load(tplg, hdr);
16348a978234SLiam Girdwood 	default:
16358a978234SLiam Girdwood 		/* bespoke vendor data object */
16368a978234SLiam Girdwood 		return soc_tplg_vendor_load(tplg, hdr);
16378a978234SLiam Girdwood 	}
16388a978234SLiam Girdwood 
16398a978234SLiam Girdwood 	return 0;
16408a978234SLiam Girdwood }
16418a978234SLiam Girdwood 
16428a978234SLiam Girdwood /* process the topology file headers */
16438a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg)
16448a978234SLiam Girdwood {
16458a978234SLiam Girdwood 	struct snd_soc_tplg_hdr *hdr;
16468a978234SLiam Girdwood 	int ret;
16478a978234SLiam Girdwood 
16488a978234SLiam Girdwood 	tplg->pass = SOC_TPLG_PASS_START;
16498a978234SLiam Girdwood 
16508a978234SLiam Girdwood 	/* process the header types from start to end */
16518a978234SLiam Girdwood 	while (tplg->pass <= SOC_TPLG_PASS_END) {
16528a978234SLiam Girdwood 
16538a978234SLiam Girdwood 		tplg->hdr_pos = tplg->fw->data;
16548a978234SLiam Girdwood 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
16558a978234SLiam Girdwood 
16568a978234SLiam Girdwood 		while (!soc_tplg_is_eof(tplg)) {
16578a978234SLiam Girdwood 
16588a978234SLiam Girdwood 			/* make sure header is valid before loading */
16598a978234SLiam Girdwood 			ret = soc_valid_header(tplg, hdr);
16608a978234SLiam Girdwood 			if (ret < 0)
16618a978234SLiam Girdwood 				return ret;
16628a978234SLiam Girdwood 			else if (ret == 0)
16638a978234SLiam Girdwood 				break;
16648a978234SLiam Girdwood 
16658a978234SLiam Girdwood 			/* load the header object */
16668a978234SLiam Girdwood 			ret = soc_tplg_load_header(tplg, hdr);
16678a978234SLiam Girdwood 			if (ret < 0)
16688a978234SLiam Girdwood 				return ret;
16698a978234SLiam Girdwood 
16708a978234SLiam Girdwood 			/* goto next header */
16718a978234SLiam Girdwood 			tplg->hdr_pos += hdr->payload_size +
16728a978234SLiam Girdwood 				sizeof(struct snd_soc_tplg_hdr);
16738a978234SLiam Girdwood 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
16748a978234SLiam Girdwood 		}
16758a978234SLiam Girdwood 
16768a978234SLiam Girdwood 		/* next data type pass */
16778a978234SLiam Girdwood 		tplg->pass++;
16788a978234SLiam Girdwood 	}
16798a978234SLiam Girdwood 
16808a978234SLiam Girdwood 	/* signal DAPM we are complete */
16818a978234SLiam Girdwood 	ret = soc_tplg_dapm_complete(tplg);
16828a978234SLiam Girdwood 	if (ret < 0)
16838a978234SLiam Girdwood 		dev_err(tplg->dev,
16848a978234SLiam Girdwood 			"ASoC: failed to initialise DAPM from Firmware\n");
16858a978234SLiam Girdwood 
16868a978234SLiam Girdwood 	return ret;
16878a978234SLiam Girdwood }
16888a978234SLiam Girdwood 
16898a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg)
16908a978234SLiam Girdwood {
16918a978234SLiam Girdwood 	int ret;
16928a978234SLiam Girdwood 
16938a978234SLiam Girdwood 	ret = soc_tplg_process_headers(tplg);
16948a978234SLiam Girdwood 	if (ret == 0)
16958a978234SLiam Girdwood 		soc_tplg_complete(tplg);
16968a978234SLiam Girdwood 
16978a978234SLiam Girdwood 	return ret;
16988a978234SLiam Girdwood }
16998a978234SLiam Girdwood 
17008a978234SLiam Girdwood /* load audio component topology from "firmware" file */
17018a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp,
17028a978234SLiam Girdwood 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
17038a978234SLiam Girdwood {
17048a978234SLiam Girdwood 	struct soc_tplg tplg;
17058a978234SLiam Girdwood 
17068a978234SLiam Girdwood 	/* setup parsing context */
17078a978234SLiam Girdwood 	memset(&tplg, 0, sizeof(tplg));
17088a978234SLiam Girdwood 	tplg.fw = fw;
17098a978234SLiam Girdwood 	tplg.dev = comp->dev;
17108a978234SLiam Girdwood 	tplg.comp = comp;
17118a978234SLiam Girdwood 	tplg.ops = ops;
17128a978234SLiam Girdwood 	tplg.req_index = id;
17138a978234SLiam Girdwood 	tplg.io_ops = ops->io_ops;
17148a978234SLiam Girdwood 	tplg.io_ops_count = ops->io_ops_count;
17158a978234SLiam Girdwood 
17168a978234SLiam Girdwood 	return soc_tplg_load(&tplg);
17178a978234SLiam Girdwood }
17188a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
17198a978234SLiam Girdwood 
17208a978234SLiam Girdwood /* remove this dynamic widget */
17218a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
17228a978234SLiam Girdwood {
17238a978234SLiam Girdwood 	/* make sure we are a widget */
17248a978234SLiam Girdwood 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
17258a978234SLiam Girdwood 		return;
17268a978234SLiam Girdwood 
17278a978234SLiam Girdwood 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
17288a978234SLiam Girdwood }
17298a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
17308a978234SLiam Girdwood 
17318a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */
17328a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
17338a978234SLiam Girdwood 	u32 index)
17348a978234SLiam Girdwood {
17358a978234SLiam Girdwood 	struct snd_soc_dapm_widget *w, *next_w;
17368a978234SLiam Girdwood 	struct snd_soc_dapm_path *p, *next_p;
17378a978234SLiam Girdwood 
17388a978234SLiam Girdwood 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
17398a978234SLiam Girdwood 
17408a978234SLiam Girdwood 		/* make sure we are a widget with correct context */
17418a978234SLiam Girdwood 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
17428a978234SLiam Girdwood 			continue;
17438a978234SLiam Girdwood 
17448a978234SLiam Girdwood 		/* match ID */
17458a978234SLiam Girdwood 		if (w->dobj.index != index &&
17468a978234SLiam Girdwood 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
17478a978234SLiam Girdwood 			continue;
17488a978234SLiam Girdwood 
17498a978234SLiam Girdwood 		list_del(&w->list);
17508a978234SLiam Girdwood 
17518a978234SLiam Girdwood 		/*
17528a978234SLiam Girdwood 		 * remove source and sink paths associated to this widget.
17538a978234SLiam Girdwood 		 * While removing the path, remove reference to it from both
17548a978234SLiam Girdwood 		 * source and sink widgets so that path is removed only once.
17558a978234SLiam Girdwood 		 */
17568a978234SLiam Girdwood 		list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
17578a978234SLiam Girdwood 			list_del(&p->list_sink);
17588a978234SLiam Girdwood 			list_del(&p->list_source);
17598a978234SLiam Girdwood 			list_del(&p->list);
17608a978234SLiam Girdwood 			kfree(p);
17618a978234SLiam Girdwood 		}
17628a978234SLiam Girdwood 		list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
17638a978234SLiam Girdwood 			list_del(&p->list_sink);
17648a978234SLiam Girdwood 			list_del(&p->list_source);
17658a978234SLiam Girdwood 			list_del(&p->list);
17668a978234SLiam Girdwood 			kfree(p);
17678a978234SLiam Girdwood 		}
17688a978234SLiam Girdwood 		/* check and free and dynamic widget kcontrols */
17698a978234SLiam Girdwood 		snd_soc_tplg_widget_remove(w);
17708a978234SLiam Girdwood 		kfree(w->kcontrols);
17718a978234SLiam Girdwood 		kfree(w->name);
17728a978234SLiam Girdwood 		kfree(w);
17738a978234SLiam Girdwood 	}
17748a978234SLiam Girdwood }
17758a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
17768a978234SLiam Girdwood 
17778a978234SLiam Girdwood /* remove dynamic controls from the component driver */
17788a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
17798a978234SLiam Girdwood {
17808a978234SLiam Girdwood 	struct snd_soc_dobj *dobj, *next_dobj;
17818a978234SLiam Girdwood 	int pass = SOC_TPLG_PASS_END;
17828a978234SLiam Girdwood 
17838a978234SLiam Girdwood 	/* process the header types from end to start */
17848a978234SLiam Girdwood 	while (pass >= SOC_TPLG_PASS_START) {
17858a978234SLiam Girdwood 
17868a978234SLiam Girdwood 		/* remove mixer controls */
17878a978234SLiam Girdwood 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
17888a978234SLiam Girdwood 			list) {
17898a978234SLiam Girdwood 
17908a978234SLiam Girdwood 			/* match index */
17918a978234SLiam Girdwood 			if (dobj->index != index &&
17928a978234SLiam Girdwood 				dobj->index != SND_SOC_TPLG_INDEX_ALL)
17938a978234SLiam Girdwood 				continue;
17948a978234SLiam Girdwood 
17958a978234SLiam Girdwood 			switch (dobj->type) {
17968a978234SLiam Girdwood 			case SND_SOC_DOBJ_MIXER:
17978a978234SLiam Girdwood 				remove_mixer(comp, dobj, pass);
17988a978234SLiam Girdwood 				break;
17998a978234SLiam Girdwood 			case SND_SOC_DOBJ_ENUM:
18008a978234SLiam Girdwood 				remove_enum(comp, dobj, pass);
18018a978234SLiam Girdwood 				break;
18028a978234SLiam Girdwood 			case SND_SOC_DOBJ_BYTES:
18038a978234SLiam Girdwood 				remove_bytes(comp, dobj, pass);
18048a978234SLiam Girdwood 				break;
18058a978234SLiam Girdwood 			case SND_SOC_DOBJ_WIDGET:
18068a978234SLiam Girdwood 				remove_widget(comp, dobj, pass);
18078a978234SLiam Girdwood 				break;
18088a978234SLiam Girdwood 			case SND_SOC_DOBJ_PCM:
18098a978234SLiam Girdwood 			case SND_SOC_DOBJ_DAI_LINK:
18108a978234SLiam Girdwood 			case SND_SOC_DOBJ_CODEC_LINK:
18118a978234SLiam Girdwood 				remove_pcm_dai(comp, dobj, pass);
18128a978234SLiam Girdwood 				break;
18138a978234SLiam Girdwood 			default:
18148a978234SLiam Girdwood 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
18158a978234SLiam Girdwood 					dobj->type);
18168a978234SLiam Girdwood 				break;
18178a978234SLiam Girdwood 			}
18188a978234SLiam Girdwood 		}
18198a978234SLiam Girdwood 		pass--;
18208a978234SLiam Girdwood 	}
18218a978234SLiam Girdwood 
18228a978234SLiam Girdwood 	/* let caller know if FW can be freed when no objects are left */
18238a978234SLiam Girdwood 	return !list_empty(&comp->dobj_list);
18248a978234SLiam Girdwood }
18258a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
1826