1f2b6a1b2SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2f2b6a1b2SKuninori Morimoto // 3f2b6a1b2SKuninori Morimoto // soc-topology.c -- ALSA SoC Topology 4f2b6a1b2SKuninori Morimoto // 5f2b6a1b2SKuninori Morimoto // Copyright (C) 2012 Texas Instruments Inc. 6f2b6a1b2SKuninori Morimoto // Copyright (C) 2015 Intel Corporation. 7f2b6a1b2SKuninori Morimoto // 8f2b6a1b2SKuninori Morimoto // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9f2b6a1b2SKuninori Morimoto // K, Mythri P <mythri.p.k@intel.com> 10f2b6a1b2SKuninori Morimoto // Prusty, Subhransu S <subhransu.s.prusty@intel.com> 11f2b6a1b2SKuninori Morimoto // B, Jayachandran <jayachandran.b@intel.com> 12f2b6a1b2SKuninori Morimoto // Abdullah, Omair M <omair.m.abdullah@intel.com> 13f2b6a1b2SKuninori Morimoto // Jin, Yao <yao.jin@intel.com> 14f2b6a1b2SKuninori Morimoto // Lin, Mengdong <mengdong.lin@intel.com> 15f2b6a1b2SKuninori Morimoto // 16f2b6a1b2SKuninori Morimoto // Add support to read audio firmware topology alongside firmware text. The 17f2b6a1b2SKuninori Morimoto // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links, 18f2b6a1b2SKuninori Morimoto // equalizers, firmware, coefficients etc. 19f2b6a1b2SKuninori Morimoto // 20f2b6a1b2SKuninori Morimoto // This file only manages the core ALSA and ASoC components, all other bespoke 21f2b6a1b2SKuninori Morimoto // firmware topology data is passed to component drivers for bespoke handling. 228a978234SLiam Girdwood 238a978234SLiam Girdwood #include <linux/kernel.h> 248a978234SLiam Girdwood #include <linux/export.h> 258a978234SLiam Girdwood #include <linux/list.h> 268a978234SLiam Girdwood #include <linux/firmware.h> 278a978234SLiam Girdwood #include <linux/slab.h> 288a978234SLiam Girdwood #include <sound/soc.h> 298a978234SLiam Girdwood #include <sound/soc-dapm.h> 308a978234SLiam Girdwood #include <sound/soc-topology.h> 3128a87eebSMengdong Lin #include <sound/tlv.h> 328a978234SLiam Girdwood 332114171dSPierre-Louis Bossart #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */ 342114171dSPierre-Louis Bossart 358a978234SLiam Girdwood /* 368a978234SLiam Girdwood * We make several passes over the data (since it wont necessarily be ordered) 378a978234SLiam Girdwood * and process objects in the following order. This guarantees the component 388a978234SLiam Girdwood * drivers will be ready with any vendor data before the mixers and DAPM objects 398a978234SLiam Girdwood * are loaded (that may make use of the vendor data). 408a978234SLiam Girdwood */ 418a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST 0 428a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR 1 435e2cd47aSAmadeusz Sławiński #define SOC_TPLG_PASS_CONTROL 2 448a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET 3 451a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI 4 461a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH 5 476257d224SAmadeusz Sławiński #define SOC_TPLG_PASS_BE_DAI 6 486257d224SAmadeusz Sławiński #define SOC_TPLG_PASS_LINK 7 498a978234SLiam Girdwood 508a978234SLiam Girdwood #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST 51593d9e52SMengdong Lin #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK 528a978234SLiam Girdwood 53583958faSMengdong Lin /* topology context */ 548a978234SLiam Girdwood struct soc_tplg { 558a978234SLiam Girdwood const struct firmware *fw; 568a978234SLiam Girdwood 578a978234SLiam Girdwood /* runtime FW parsing */ 5800ac8389SQinghua Jin const u8 *pos; /* read position */ 598a978234SLiam Girdwood const u8 *hdr_pos; /* header position */ 608a978234SLiam Girdwood unsigned int pass; /* pass number */ 618a978234SLiam Girdwood 628a978234SLiam Girdwood /* component caller */ 638a978234SLiam Girdwood struct device *dev; 648a978234SLiam Girdwood struct snd_soc_component *comp; 658a978234SLiam Girdwood u32 index; /* current block index */ 668a978234SLiam Girdwood 6788a17d8fSMengdong Lin /* vendor specific kcontrol operations */ 688a978234SLiam Girdwood const struct snd_soc_tplg_kcontrol_ops *io_ops; 698a978234SLiam Girdwood int io_ops_count; 708a978234SLiam Girdwood 711a3232d2SMengdong Lin /* vendor specific bytes ext handlers, for TLV bytes controls */ 721a3232d2SMengdong Lin const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; 731a3232d2SMengdong Lin int bytes_ext_ops_count; 741a3232d2SMengdong Lin 758a978234SLiam Girdwood /* optional fw loading callbacks to component drivers */ 768a978234SLiam Girdwood struct snd_soc_tplg_ops *ops; 778a978234SLiam Girdwood }; 788a978234SLiam Girdwood 798a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */ 808a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, 818a978234SLiam Girdwood unsigned int count, size_t bytes, const char *elem_type) 828a978234SLiam Girdwood { 838a978234SLiam Girdwood const u8 *end = tplg->pos + elem_size * count; 848a978234SLiam Girdwood 858a978234SLiam Girdwood if (end > tplg->fw->data + tplg->fw->size) { 868a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: %s overflow end of data\n", 878a978234SLiam Girdwood elem_type); 888a978234SLiam Girdwood return -EINVAL; 898a978234SLiam Girdwood } 908a978234SLiam Girdwood 918a978234SLiam Girdwood /* check there is enough room in chunk for control. 928a978234SLiam Girdwood extra bytes at the end of control are for vendor data here */ 938a978234SLiam Girdwood if (elem_size * count > bytes) { 948a978234SLiam Girdwood dev_err(tplg->dev, 958a978234SLiam Girdwood "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", 968a978234SLiam Girdwood elem_type, count, elem_size, bytes); 978a978234SLiam Girdwood return -EINVAL; 988a978234SLiam Girdwood } 998a978234SLiam Girdwood 1008a978234SLiam Girdwood return 0; 1018a978234SLiam Girdwood } 1028a978234SLiam Girdwood 1034fad3cc6SAmadeusz Sławiński static inline bool soc_tplg_is_eof(struct soc_tplg *tplg) 1048a978234SLiam Girdwood { 1058a978234SLiam Girdwood const u8 *end = tplg->hdr_pos; 1068a978234SLiam Girdwood 1078a978234SLiam Girdwood if (end >= tplg->fw->data + tplg->fw->size) 1084fad3cc6SAmadeusz Sławiński return true; 1094fad3cc6SAmadeusz Sławiński return false; 1108a978234SLiam Girdwood } 1118a978234SLiam Girdwood 1128a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) 1138a978234SLiam Girdwood { 1148a978234SLiam Girdwood return (unsigned long)(tplg->hdr_pos - tplg->fw->data); 1158a978234SLiam Girdwood } 1168a978234SLiam Girdwood 1178a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) 1188a978234SLiam Girdwood { 1198a978234SLiam Girdwood return (unsigned long)(tplg->pos - tplg->fw->data); 1208a978234SLiam Girdwood } 1218a978234SLiam Girdwood 1228a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */ 1238a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { 1248a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, 1258a978234SLiam Girdwood snd_soc_put_volsw, snd_soc_info_volsw}, 1268a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, 1278a978234SLiam Girdwood snd_soc_put_volsw_sx, NULL}, 1288a978234SLiam Girdwood {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, 1298a978234SLiam Girdwood snd_soc_put_enum_double, snd_soc_info_enum_double}, 1308a978234SLiam Girdwood {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, 1318a978234SLiam Girdwood snd_soc_put_enum_double, NULL}, 1328a978234SLiam Girdwood {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, 1338a978234SLiam Girdwood snd_soc_bytes_put, snd_soc_bytes_info}, 1348a978234SLiam Girdwood {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, 1358a978234SLiam Girdwood snd_soc_put_volsw_range, snd_soc_info_volsw_range}, 1368a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, 1378a978234SLiam Girdwood snd_soc_put_xr_sx, snd_soc_info_xr_sx}, 1388a978234SLiam Girdwood {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, 1398a978234SLiam Girdwood snd_soc_put_strobe, NULL}, 1408a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, 1412c57d478SJeeja KP snd_soc_dapm_put_volsw, snd_soc_info_volsw}, 1428a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, 1438a978234SLiam Girdwood snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, 1448a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, 1458a978234SLiam Girdwood snd_soc_dapm_put_enum_double, NULL}, 1468a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, 1478a978234SLiam Girdwood snd_soc_dapm_put_enum_double, NULL}, 1488a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, 1498a978234SLiam Girdwood snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, 1508a978234SLiam Girdwood }; 1518a978234SLiam Girdwood 1528a978234SLiam Girdwood struct soc_tplg_map { 1538a978234SLiam Girdwood int uid; 1548a978234SLiam Girdwood int kid; 1558a978234SLiam Girdwood }; 1568a978234SLiam Girdwood 1578a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */ 1588a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = { 1598a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, 1608a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, 1618a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, 1628a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, 1638a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, 1648a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, 1658a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, 1668a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, 1678a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, 1688a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, 1698a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, 1708a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, 1718a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, 1728a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, 1738a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, 1748a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, 1758a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer}, 1768a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler}, 1778a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect}, 1788a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen}, 1798a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src}, 1808a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc}, 1818a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder}, 1828a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder}, 1838a978234SLiam Girdwood }; 1848a978234SLiam Girdwood 1858f9974d9SAmadeusz Sławiński static int tplg_chan_get_reg(struct soc_tplg *tplg, 1868a978234SLiam Girdwood struct snd_soc_tplg_channel *chan, int map) 1878a978234SLiam Girdwood { 1888a978234SLiam Girdwood int i; 1898a978234SLiam Girdwood 1908a978234SLiam Girdwood for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { 1915aebe7c7SPierre-Louis Bossart if (le32_to_cpu(chan[i].id) == map) 1925aebe7c7SPierre-Louis Bossart return le32_to_cpu(chan[i].reg); 1938a978234SLiam Girdwood } 1948a978234SLiam Girdwood 1958a978234SLiam Girdwood return -EINVAL; 1968a978234SLiam Girdwood } 1978a978234SLiam Girdwood 1988f9974d9SAmadeusz Sławiński static int tplg_chan_get_shift(struct soc_tplg *tplg, 1998a978234SLiam Girdwood struct snd_soc_tplg_channel *chan, int map) 2008a978234SLiam Girdwood { 2018a978234SLiam Girdwood int i; 2028a978234SLiam Girdwood 2038a978234SLiam Girdwood for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { 2045aebe7c7SPierre-Louis Bossart if (le32_to_cpu(chan[i].id) == map) 2055aebe7c7SPierre-Louis Bossart return le32_to_cpu(chan[i].shift); 2068a978234SLiam Girdwood } 2078a978234SLiam Girdwood 2088a978234SLiam Girdwood return -EINVAL; 2098a978234SLiam Girdwood } 2108a978234SLiam Girdwood 2118a978234SLiam Girdwood static int get_widget_id(int tplg_type) 2128a978234SLiam Girdwood { 2138a978234SLiam Girdwood int i; 2148a978234SLiam Girdwood 2158a978234SLiam Girdwood for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { 2168a978234SLiam Girdwood if (tplg_type == dapm_map[i].uid) 2178a978234SLiam Girdwood return dapm_map[i].kid; 2188a978234SLiam Girdwood } 2198a978234SLiam Girdwood 2208a978234SLiam Girdwood return -EINVAL; 2218a978234SLiam Girdwood } 2228a978234SLiam Girdwood 2238a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg, 2248a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *hdr, int index) 2258a978234SLiam Girdwood { 2268a978234SLiam Girdwood dev_err(tplg->dev, 2278a978234SLiam Girdwood "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", 2288a978234SLiam Girdwood hdr->ops.get, hdr->ops.put, hdr->ops.info, index, 2298a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 2308a978234SLiam Girdwood } 2318a978234SLiam Girdwood 2328a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg, 2338a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *hdr, const char *name) 2348a978234SLiam Girdwood { 2358a978234SLiam Girdwood dev_err(tplg->dev, 23634b31045SAmadeusz Sławiński "ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", 2378a978234SLiam Girdwood name, hdr->ops.get, hdr->ops.put, hdr->ops.info, 2388a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 2398a978234SLiam Girdwood } 2408a978234SLiam Girdwood 2418a978234SLiam Girdwood /* pass vendor data to component driver for processing */ 242c2cbd0a7SKeyon Jie static int soc_tplg_vendor_load(struct soc_tplg *tplg, 2438a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 2448a978234SLiam Girdwood { 2458a978234SLiam Girdwood int ret = 0; 2468a978234SLiam Girdwood 247c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->vendor_load) 248c60b613aSLiam Girdwood ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); 2498a978234SLiam Girdwood else { 2508a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", 2518a978234SLiam Girdwood hdr->vendor_type); 2528a978234SLiam Girdwood return -EINVAL; 2538a978234SLiam Girdwood } 2548a978234SLiam Girdwood 2558a978234SLiam Girdwood if (ret < 0) 2568a978234SLiam Girdwood dev_err(tplg->dev, 2578a978234SLiam Girdwood "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", 2588a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), 2598a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), 2608a978234SLiam Girdwood hdr->type, hdr->vendor_type); 2618a978234SLiam Girdwood return ret; 2628a978234SLiam Girdwood } 2638a978234SLiam Girdwood 2648a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for 2658a978234SLiam Girdwood * external widgets where we can assign private data/ops */ 2668a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg, 2678a978234SLiam Girdwood struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 2688a978234SLiam Girdwood { 269c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->widget_load) 270c60b613aSLiam Girdwood return tplg->ops->widget_load(tplg->comp, tplg->index, w, 271c60b613aSLiam Girdwood tplg_w); 2728a978234SLiam Girdwood 2738a978234SLiam Girdwood return 0; 2748a978234SLiam Girdwood } 2758a978234SLiam Girdwood 276ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for 277ebd259d3SLiam Girdwood * external widgets where we can assign private data/ops */ 278ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg, 279ebd259d3SLiam Girdwood struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 280ebd259d3SLiam Girdwood { 281c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->widget_ready) 282c60b613aSLiam Girdwood return tplg->ops->widget_ready(tplg->comp, tplg->index, w, 283c60b613aSLiam Girdwood tplg_w); 284ebd259d3SLiam Girdwood 285ebd259d3SLiam Girdwood return 0; 286ebd259d3SLiam Girdwood } 287ebd259d3SLiam Girdwood 288183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */ 28964527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg, 290c60b613aSLiam Girdwood struct snd_soc_dai_driver *dai_drv, 291c60b613aSLiam Girdwood struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 2928a978234SLiam Girdwood { 293c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->dai_load) 294c60b613aSLiam Girdwood return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, 295c60b613aSLiam Girdwood pcm, dai); 2968a978234SLiam Girdwood 2978a978234SLiam Girdwood return 0; 2988a978234SLiam Girdwood } 2998a978234SLiam Girdwood 300183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */ 301acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg, 302c60b613aSLiam Girdwood struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) 303acfc7d46SMengdong Lin { 304c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->link_load) 305c60b613aSLiam Girdwood return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); 306acfc7d46SMengdong Lin 307acfc7d46SMengdong Lin return 0; 308acfc7d46SMengdong Lin } 309acfc7d46SMengdong Lin 3108a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */ 311415717e1SRanjani Sridharan static int soc_tplg_complete(struct soc_tplg *tplg) 3128a978234SLiam Girdwood { 313c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->complete) 314415717e1SRanjani Sridharan return tplg->ops->complete(tplg->comp); 315415717e1SRanjani Sridharan 316415717e1SRanjani Sridharan return 0; 3178a978234SLiam Girdwood } 3188a978234SLiam Girdwood 3198a978234SLiam Girdwood /* add a dynamic kcontrol */ 3208a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, 3218a978234SLiam Girdwood const struct snd_kcontrol_new *control_new, const char *prefix, 3228a978234SLiam Girdwood void *data, struct snd_kcontrol **kcontrol) 3238a978234SLiam Girdwood { 3248a978234SLiam Girdwood int err; 3258a978234SLiam Girdwood 3268a978234SLiam Girdwood *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); 3278a978234SLiam Girdwood if (*kcontrol == NULL) { 3288a978234SLiam Girdwood dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", 3298a978234SLiam Girdwood control_new->name); 3308a978234SLiam Girdwood return -ENOMEM; 3318a978234SLiam Girdwood } 3328a978234SLiam Girdwood 3338a978234SLiam Girdwood err = snd_ctl_add(card, *kcontrol); 3348a978234SLiam Girdwood if (err < 0) { 3358a978234SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 3368a978234SLiam Girdwood control_new->name, err); 3378a978234SLiam Girdwood return err; 3388a978234SLiam Girdwood } 3398a978234SLiam Girdwood 3408a978234SLiam Girdwood return 0; 3418a978234SLiam Girdwood } 3428a978234SLiam Girdwood 3438a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */ 3448a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, 3458a978234SLiam Girdwood struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) 3468a978234SLiam Girdwood { 3478a978234SLiam Girdwood struct snd_soc_component *comp = tplg->comp; 3488a978234SLiam Girdwood 3498a978234SLiam Girdwood return soc_tplg_add_dcontrol(comp->card->snd_card, 3502a710bb3SAmadeusz Sławiński tplg->dev, k, comp->name_prefix, comp, kcontrol); 3518a978234SLiam Girdwood } 3528a978234SLiam Girdwood 353fdfa3661SAmadeusz Sławiński /* remove kcontrol */ 354fdfa3661SAmadeusz Sławiński static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj, 355fdfa3661SAmadeusz Sławiński int pass) 3568a978234SLiam Girdwood { 3578a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 3588a978234SLiam Girdwood 3595e2cd47aSAmadeusz Sławiński if (pass != SOC_TPLG_PASS_CONTROL) 3608a978234SLiam Girdwood return; 3618a978234SLiam Girdwood 36231e92739SAmadeusz Sławiński if (dobj->unload) 36331e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 3648a978234SLiam Girdwood 36533ae6ae2SAmadeusz Sławiński snd_ctl_remove(card, dobj->control.kcontrol); 36633ae6ae2SAmadeusz Sławiński list_del(&dobj->list); 3678a978234SLiam Girdwood } 3688a978234SLiam Girdwood 3697df04ea7SRanjani Sridharan /* remove a route */ 3702abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_route(struct snd_soc_component *comp, 3717df04ea7SRanjani Sridharan struct snd_soc_dobj *dobj, int pass) 3727df04ea7SRanjani Sridharan { 3737df04ea7SRanjani Sridharan if (pass != SOC_TPLG_PASS_GRAPH) 3747df04ea7SRanjani Sridharan return; 3757df04ea7SRanjani Sridharan 37631e92739SAmadeusz Sławiński if (dobj->unload) 37731e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 3787df04ea7SRanjani Sridharan 3797df04ea7SRanjani Sridharan list_del(&dobj->list); 3807df04ea7SRanjani Sridharan } 3817df04ea7SRanjani Sridharan 3828a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */ 3832abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_widget(struct snd_soc_component *comp, 3848a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 3858a978234SLiam Girdwood { 3868a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 3878a978234SLiam Girdwood struct snd_soc_dapm_widget *w = 3888a978234SLiam Girdwood container_of(dobj, struct snd_soc_dapm_widget, dobj); 3898a978234SLiam Girdwood int i; 3908a978234SLiam Girdwood 3918a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_WIDGET) 3928a978234SLiam Girdwood return; 3938a978234SLiam Girdwood 39431e92739SAmadeusz Sławiński if (dobj->unload) 39531e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 3968a978234SLiam Girdwood 39705bdcf12SLiam Girdwood if (!w->kcontrols) 39805bdcf12SLiam Girdwood goto free_news; 39905bdcf12SLiam Girdwood 4008d456654SAmadeusz Sławiński for (i = 0; w->kcontrols && i < w->num_kcontrols; i++) 4018d456654SAmadeusz Sławiński snd_ctl_remove(card, w->kcontrols[i]); 40205bdcf12SLiam Girdwood 40305bdcf12SLiam Girdwood free_news: 40405bdcf12SLiam Girdwood 405a46e8393SAmadeusz Sławiński list_del(&dobj->list); 406a46e8393SAmadeusz Sławiński 4078a978234SLiam Girdwood /* widget w is freed by soc-dapm.c */ 4088a978234SLiam Girdwood } 4098a978234SLiam Girdwood 41064527e8aSMengdong Lin /* remove DAI configurations */ 4112abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_dai(struct snd_soc_component *comp, 4128a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 4138a978234SLiam Girdwood { 41464527e8aSMengdong Lin struct snd_soc_dai_driver *dai_drv = 41564527e8aSMengdong Lin container_of(dobj, struct snd_soc_dai_driver, dobj); 416fc4cb1e1SAmadeusz Sławiński struct snd_soc_dai *dai, *_dai; 41764527e8aSMengdong Lin 4188a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_PCM_DAI) 4198a978234SLiam Girdwood return; 4208a978234SLiam Girdwood 42131e92739SAmadeusz Sławiński if (dobj->unload) 42231e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 4238a978234SLiam Girdwood 424fc4cb1e1SAmadeusz Sławiński for_each_component_dais_safe(comp, dai, _dai) 42552abe6ccSGuennadi Liakhovetski if (dai->driver == dai_drv) 426fc4cb1e1SAmadeusz Sławiński snd_soc_unregister_dai(dai); 42752abe6ccSGuennadi Liakhovetski 4288a978234SLiam Girdwood list_del(&dobj->list); 4298a978234SLiam Girdwood } 4308a978234SLiam Girdwood 431acfc7d46SMengdong Lin /* remove link configurations */ 4322abfd4bdSAmadeusz Sławiński static void soc_tplg_remove_link(struct snd_soc_component *comp, 433acfc7d46SMengdong Lin struct snd_soc_dobj *dobj, int pass) 434acfc7d46SMengdong Lin { 435acfc7d46SMengdong Lin struct snd_soc_dai_link *link = 436acfc7d46SMengdong Lin container_of(dobj, struct snd_soc_dai_link, dobj); 437acfc7d46SMengdong Lin 438acfc7d46SMengdong Lin if (pass != SOC_TPLG_PASS_PCM_DAI) 439acfc7d46SMengdong Lin return; 440acfc7d46SMengdong Lin 44131e92739SAmadeusz Sławiński if (dobj->unload) 44231e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 443acfc7d46SMengdong Lin 444dd836ddfSDragos Tarcatu list_del(&dobj->list); 44550cd9b53SKuninori Morimoto snd_soc_remove_pcm_runtime(comp->card, 44650cd9b53SKuninori Morimoto snd_soc_get_pcm_runtime(comp->card, link)); 447acfc7d46SMengdong Lin } 448acfc7d46SMengdong Lin 449adfebb51SBard liao /* unload dai link */ 450adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp, 451adfebb51SBard liao struct snd_soc_dobj *dobj, int pass) 452adfebb51SBard liao { 453adfebb51SBard liao if (pass != SOC_TPLG_PASS_LINK) 454adfebb51SBard liao return; 455adfebb51SBard liao 45631e92739SAmadeusz Sławiński if (dobj->unload) 45731e92739SAmadeusz Sławiński dobj->unload(comp, dobj); 458adfebb51SBard liao 459adfebb51SBard liao /* 4602abfd4bdSAmadeusz Sławiński * We don't free the link here as what soc_tplg_remove_link() do since BE 461adfebb51SBard liao * links are not allocated by topology. 462adfebb51SBard liao * We however need to reset the dobj type to its initial values 463adfebb51SBard liao */ 464adfebb51SBard liao dobj->type = SND_SOC_DOBJ_NONE; 465adfebb51SBard liao list_del(&dobj->list); 466adfebb51SBard liao } 467adfebb51SBard liao 4688a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */ 4698a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, 4708a978234SLiam Girdwood struct snd_kcontrol_new *k, 4712b5cdb91SMengdong Lin const struct soc_tplg *tplg) 4728a978234SLiam Girdwood { 4732b5cdb91SMengdong Lin const struct snd_soc_tplg_kcontrol_ops *ops; 4741a3232d2SMengdong Lin const struct snd_soc_tplg_bytes_ext_ops *ext_ops; 4752b5cdb91SMengdong Lin int num_ops, i; 4768a978234SLiam Girdwood 4775aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES 4781a3232d2SMengdong Lin && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER 479feb00b73SAmadeusz Sławiński && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ 480feb00b73SAmadeusz Sławiński || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) 4811a3232d2SMengdong Lin && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 4821a3232d2SMengdong Lin struct soc_bytes_ext *sbe; 4831a3232d2SMengdong Lin struct snd_soc_tplg_bytes_control *be; 4841a3232d2SMengdong Lin 4851a3232d2SMengdong Lin sbe = (struct soc_bytes_ext *)k->private_value; 4861a3232d2SMengdong Lin be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); 4871a3232d2SMengdong Lin 4881a3232d2SMengdong Lin /* TLV bytes controls need standard kcontrol info handler, 4891a3232d2SMengdong Lin * TLV callback and extended put/get handlers. 4901a3232d2SMengdong Lin */ 491f4be978bSOmair M Abdullah k->info = snd_soc_bytes_info_ext; 4921a3232d2SMengdong Lin k->tlv.c = snd_soc_bytes_tlv_callback; 4931a3232d2SMengdong Lin 4946788fc1aSPierre-Louis Bossart /* 4956788fc1aSPierre-Louis Bossart * When a topology-based implementation abuses the 4966788fc1aSPierre-Louis Bossart * control interface and uses bytes_ext controls of 4976788fc1aSPierre-Louis Bossart * more than 512 bytes, we need to disable the size 4986788fc1aSPierre-Louis Bossart * checks, otherwise accesses to such controls will 4996788fc1aSPierre-Louis Bossart * return an -EINVAL error and prevent the card from 5006788fc1aSPierre-Louis Bossart * being configured. 5016788fc1aSPierre-Louis Bossart */ 5022c7463d0STakashi Iwai if (sbe->max > 512) 5036788fc1aSPierre-Louis Bossart k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK; 5046788fc1aSPierre-Louis Bossart 5051a3232d2SMengdong Lin ext_ops = tplg->bytes_ext_ops; 5061a3232d2SMengdong Lin num_ops = tplg->bytes_ext_ops_count; 5071a3232d2SMengdong Lin for (i = 0; i < num_ops; i++) { 50872bbeda0SPierre-Louis Bossart if (!sbe->put && 50972bbeda0SPierre-Louis Bossart ext_ops[i].id == le32_to_cpu(be->ext_ops.put)) 5101a3232d2SMengdong Lin sbe->put = ext_ops[i].put; 51172bbeda0SPierre-Louis Bossart if (!sbe->get && 51272bbeda0SPierre-Louis Bossart ext_ops[i].id == le32_to_cpu(be->ext_ops.get)) 5131a3232d2SMengdong Lin sbe->get = ext_ops[i].get; 5141a3232d2SMengdong Lin } 5151a3232d2SMengdong Lin 516819b9f60SDharageswari R if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get) 5171a3232d2SMengdong Lin return -EINVAL; 518819b9f60SDharageswari R if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put) 519819b9f60SDharageswari R return -EINVAL; 520819b9f60SDharageswari R return 0; 5211a3232d2SMengdong Lin } 5221a3232d2SMengdong Lin 52388a17d8fSMengdong Lin /* try and map vendor specific kcontrol handlers first */ 5242b5cdb91SMengdong Lin ops = tplg->io_ops; 5252b5cdb91SMengdong Lin num_ops = tplg->io_ops_count; 5268a978234SLiam Girdwood for (i = 0; i < num_ops; i++) { 5278a978234SLiam Girdwood 52872bbeda0SPierre-Louis Bossart if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) 5298a978234SLiam Girdwood k->put = ops[i].put; 53072bbeda0SPierre-Louis Bossart if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) 5318a978234SLiam Girdwood k->get = ops[i].get; 53272bbeda0SPierre-Louis Bossart if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) 5332b5cdb91SMengdong Lin k->info = ops[i].info; 5348a978234SLiam Girdwood } 5358a978234SLiam Girdwood 53688a17d8fSMengdong Lin /* vendor specific handlers found ? */ 53788a17d8fSMengdong Lin if (k->put && k->get && k->info) 53888a17d8fSMengdong Lin return 0; 53988a17d8fSMengdong Lin 54088a17d8fSMengdong Lin /* none found so try standard kcontrol handlers */ 5412b5cdb91SMengdong Lin ops = io_ops; 5422b5cdb91SMengdong Lin num_ops = ARRAY_SIZE(io_ops); 54388a17d8fSMengdong Lin for (i = 0; i < num_ops; i++) { 54488a17d8fSMengdong Lin 54572bbeda0SPierre-Louis Bossart if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) 54688a17d8fSMengdong Lin k->put = ops[i].put; 54772bbeda0SPierre-Louis Bossart if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) 54888a17d8fSMengdong Lin k->get = ops[i].get; 54972bbeda0SPierre-Louis Bossart if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) 5508a978234SLiam Girdwood k->info = ops[i].info; 5518a978234SLiam Girdwood } 5528a978234SLiam Girdwood 5538a978234SLiam Girdwood /* standard handlers found ? */ 5548a978234SLiam Girdwood if (k->put && k->get && k->info) 5558a978234SLiam Girdwood return 0; 5568a978234SLiam Girdwood 5578a978234SLiam Girdwood /* nothing to bind */ 5588a978234SLiam Girdwood return -EINVAL; 5598a978234SLiam Girdwood } 5608a978234SLiam Girdwood 5618a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */ 5628a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, 5638a978234SLiam Girdwood const struct snd_soc_tplg_widget_events *events, 5648a978234SLiam Girdwood int num_events, u16 event_type) 5658a978234SLiam Girdwood { 5668a978234SLiam Girdwood int i; 5678a978234SLiam Girdwood 5688a978234SLiam Girdwood w->event = NULL; 5698a978234SLiam Girdwood 5708a978234SLiam Girdwood for (i = 0; i < num_events; i++) { 5718a978234SLiam Girdwood if (event_type == events[i].type) { 5728a978234SLiam Girdwood 5738a978234SLiam Girdwood /* found - so assign event */ 5748a978234SLiam Girdwood w->event = events[i].event_handler; 5758a978234SLiam Girdwood return 0; 5768a978234SLiam Girdwood } 5778a978234SLiam Girdwood } 5788a978234SLiam Girdwood 5798a978234SLiam Girdwood /* not found */ 5808a978234SLiam Girdwood return -EINVAL; 5818a978234SLiam Girdwood } 5828a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); 5838a978234SLiam Girdwood 5848a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */ 585430791ddSAmadeusz Sławiński static int soc_tplg_control_load(struct soc_tplg *tplg, 5868a978234SLiam Girdwood struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) 5878a978234SLiam Girdwood { 588*ec5dffcdSAmadeusz Sławiński int ret = 0; 5898a978234SLiam Girdwood 590*ec5dffcdSAmadeusz Sławiński if (tplg->ops && tplg->ops->control_load) 591*ec5dffcdSAmadeusz Sławiński ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr); 592*ec5dffcdSAmadeusz Sławiński 593*ec5dffcdSAmadeusz Sławiński if (ret) 594*ec5dffcdSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name); 595*ec5dffcdSAmadeusz Sławiński 596*ec5dffcdSAmadeusz Sławiński return ret; 5978a978234SLiam Girdwood } 5988a978234SLiam Girdwood 59928a87eebSMengdong Lin 60028a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, 60128a87eebSMengdong Lin struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) 6028a978234SLiam Girdwood { 60328a87eebSMengdong Lin unsigned int item_len = 2 * sizeof(unsigned int); 60428a87eebSMengdong Lin unsigned int *p; 6058a978234SLiam Girdwood 606ff922622SAmadeusz Sławiński p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL); 60728a87eebSMengdong Lin if (!p) 6088a978234SLiam Girdwood return -ENOMEM; 6098a978234SLiam Girdwood 61028a87eebSMengdong Lin p[0] = SNDRV_CTL_TLVT_DB_SCALE; 61128a87eebSMengdong Lin p[1] = item_len; 6125aebe7c7SPierre-Louis Bossart p[2] = le32_to_cpu(scale->min); 6135aebe7c7SPierre-Louis Bossart p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK) 6145aebe7c7SPierre-Louis Bossart | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0); 6158a978234SLiam Girdwood 61628a87eebSMengdong Lin kc->tlv.p = (void *)p; 61728a87eebSMengdong Lin return 0; 61828a87eebSMengdong Lin } 61928a87eebSMengdong Lin 62028a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg, 62128a87eebSMengdong Lin struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) 62228a87eebSMengdong Lin { 62328a87eebSMengdong Lin struct snd_soc_tplg_ctl_tlv *tplg_tlv; 6245aebe7c7SPierre-Louis Bossart u32 access = le32_to_cpu(tc->access); 62528a87eebSMengdong Lin 6265aebe7c7SPierre-Louis Bossart if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) 62728a87eebSMengdong Lin return 0; 62828a87eebSMengdong Lin 6295aebe7c7SPierre-Louis Bossart if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { 63028a87eebSMengdong Lin tplg_tlv = &tc->tlv; 6315aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(tplg_tlv->type)) { 63228a87eebSMengdong Lin case SNDRV_CTL_TLVT_DB_SCALE: 63328a87eebSMengdong Lin return soc_tplg_create_tlv_db_scale(tplg, kc, 63428a87eebSMengdong Lin &tplg_tlv->scale); 63528a87eebSMengdong Lin 63628a87eebSMengdong Lin /* TODO: add support for other TLV types */ 63728a87eebSMengdong Lin default: 63828a87eebSMengdong Lin dev_dbg(tplg->dev, "Unsupported TLV type %d\n", 63928a87eebSMengdong Lin tplg_tlv->type); 64028a87eebSMengdong Lin return -EINVAL; 64128a87eebSMengdong Lin } 64228a87eebSMengdong Lin } 6438a978234SLiam Girdwood 6448a978234SLiam Girdwood return 0; 6458a978234SLiam Girdwood } 6468a978234SLiam Girdwood 6470db627c4SAmadeusz Sławiński static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size) 6488a978234SLiam Girdwood { 6498a978234SLiam Girdwood struct snd_soc_tplg_bytes_control *be; 6508a978234SLiam Girdwood struct soc_bytes_ext *sbe; 6518a978234SLiam Girdwood struct snd_kcontrol_new kc; 6520db627c4SAmadeusz Sławiński int ret = 0; 6538a978234SLiam Girdwood 6548a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 6553ce57f22SAmadeusz Sławiński sizeof(struct snd_soc_tplg_bytes_control), 6560db627c4SAmadeusz Sławiński 1, size, "mixer bytes")) 6578a978234SLiam Girdwood return -EINVAL; 6588a978234SLiam Girdwood 6598a978234SLiam Girdwood be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 6608a978234SLiam Girdwood 6618a978234SLiam Girdwood /* validate kcontrol */ 6628a978234SLiam Girdwood if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 6638a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 6648a978234SLiam Girdwood return -EINVAL; 6658a978234SLiam Girdwood 666ff922622SAmadeusz Sławiński sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL); 6678a978234SLiam Girdwood if (sbe == NULL) 6688a978234SLiam Girdwood return -ENOMEM; 6698a978234SLiam Girdwood 6708a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + 6715aebe7c7SPierre-Louis Bossart le32_to_cpu(be->priv.size)); 6728a978234SLiam Girdwood 6738a978234SLiam Girdwood dev_dbg(tplg->dev, 6748a978234SLiam Girdwood "ASoC: adding bytes kcontrol %s with access 0x%x\n", 6758a978234SLiam Girdwood be->hdr.name, be->hdr.access); 6768a978234SLiam Girdwood 6778a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 6788a978234SLiam Girdwood kc.name = be->hdr.name; 6798a978234SLiam Girdwood kc.private_value = (long)sbe; 6808a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 6815aebe7c7SPierre-Louis Bossart kc.access = le32_to_cpu(be->hdr.access); 6828a978234SLiam Girdwood 6835aebe7c7SPierre-Louis Bossart sbe->max = le32_to_cpu(be->max); 6848a978234SLiam Girdwood sbe->dobj.type = SND_SOC_DOBJ_BYTES; 68531e92739SAmadeusz Sławiński if (tplg->ops) 68631e92739SAmadeusz Sławiński sbe->dobj.unload = tplg->ops->control_unload; 6878a978234SLiam Girdwood INIT_LIST_HEAD(&sbe->dobj.list); 6888a978234SLiam Girdwood 6898a978234SLiam Girdwood /* map io handlers */ 6900db627c4SAmadeusz Sławiński ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); 6910db627c4SAmadeusz Sławiński if (ret) { 6928a978234SLiam Girdwood soc_control_err(tplg, &be->hdr, be->hdr.name); 6930db627c4SAmadeusz Sławiński goto err; 6948a978234SLiam Girdwood } 6958a978234SLiam Girdwood 6968a978234SLiam Girdwood /* pass control to driver for optional further init */ 6979e2ee000SAmadeusz Sławiński ret = soc_tplg_control_load(tplg, &kc, &be->hdr); 698*ec5dffcdSAmadeusz Sławiński if (ret < 0) 6990db627c4SAmadeusz Sławiński goto err; 7008a978234SLiam Girdwood 7018a978234SLiam Girdwood /* register control here */ 7020db627c4SAmadeusz Sławiński ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol); 7030db627c4SAmadeusz Sławiński if (ret < 0) { 7040db627c4SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to add %s\n", be->hdr.name); 7050db627c4SAmadeusz Sławiński goto err; 7068a978234SLiam Girdwood } 7078a978234SLiam Girdwood 7088a978234SLiam Girdwood list_add(&sbe->dobj.list, &tplg->comp->dobj_list); 7098a978234SLiam Girdwood 7100db627c4SAmadeusz Sławiński err: 7110db627c4SAmadeusz Sławiński return ret; 7128a978234SLiam Girdwood } 7138a978234SLiam Girdwood 7140db627c4SAmadeusz Sławiński static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size) 7158a978234SLiam Girdwood { 7168a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 7178a978234SLiam Girdwood struct soc_mixer_control *sm; 7188a978234SLiam Girdwood struct snd_kcontrol_new kc; 7190db627c4SAmadeusz Sławiński int ret = 0; 7208a978234SLiam Girdwood 7218a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 7228a978234SLiam Girdwood sizeof(struct snd_soc_tplg_mixer_control), 7230db627c4SAmadeusz Sławiński 1, size, "mixers")) 7248a978234SLiam Girdwood return -EINVAL; 7258a978234SLiam Girdwood 7268a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 7278a978234SLiam Girdwood 7288a978234SLiam Girdwood /* validate kcontrol */ 7298a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 7308a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 7318a978234SLiam Girdwood return -EINVAL; 7328a978234SLiam Girdwood 733ff922622SAmadeusz Sławiński sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL); 7348a978234SLiam Girdwood if (sm == NULL) 7358a978234SLiam Girdwood return -ENOMEM; 7368a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + 7375aebe7c7SPierre-Louis Bossart le32_to_cpu(mc->priv.size)); 7388a978234SLiam Girdwood 7398a978234SLiam Girdwood dev_dbg(tplg->dev, 7408a978234SLiam Girdwood "ASoC: adding mixer kcontrol %s with access 0x%x\n", 7418a978234SLiam Girdwood mc->hdr.name, mc->hdr.access); 7428a978234SLiam Girdwood 7438a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 7448a978234SLiam Girdwood kc.name = mc->hdr.name; 7458a978234SLiam Girdwood kc.private_value = (long)sm; 7468a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 7475aebe7c7SPierre-Louis Bossart kc.access = le32_to_cpu(mc->hdr.access); 7488a978234SLiam Girdwood 7498a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 7508f9974d9SAmadeusz Sławiński sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL); 7518f9974d9SAmadeusz Sławiński sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR); 7528f9974d9SAmadeusz Sławiński sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL); 7538f9974d9SAmadeusz Sławiński sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR); 7548a978234SLiam Girdwood 7555aebe7c7SPierre-Louis Bossart sm->max = le32_to_cpu(mc->max); 7565aebe7c7SPierre-Louis Bossart sm->min = le32_to_cpu(mc->min); 7575aebe7c7SPierre-Louis Bossart sm->invert = le32_to_cpu(mc->invert); 7585aebe7c7SPierre-Louis Bossart sm->platform_max = le32_to_cpu(mc->platform_max); 7598a978234SLiam Girdwood sm->dobj.index = tplg->index; 7608a978234SLiam Girdwood sm->dobj.type = SND_SOC_DOBJ_MIXER; 76131e92739SAmadeusz Sławiński if (tplg->ops) 76231e92739SAmadeusz Sławiński sm->dobj.unload = tplg->ops->control_unload; 7638a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 7648a978234SLiam Girdwood 7658a978234SLiam Girdwood /* map io handlers */ 7660db627c4SAmadeusz Sławiński ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); 7670db627c4SAmadeusz Sławiński if (ret) { 7688a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 7690db627c4SAmadeusz Sławiński goto err; 7708a978234SLiam Girdwood } 7718a978234SLiam Girdwood 7723789debfSBard liao /* create any TLV data */ 7730db627c4SAmadeusz Sławiński ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr); 7740db627c4SAmadeusz Sławiński if (ret < 0) { 7750db627c4SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name); 7760db627c4SAmadeusz Sławiński goto err; 777482db55aSAmadeusz Sławiński } 7783789debfSBard liao 7798a978234SLiam Girdwood /* pass control to driver for optional further init */ 7809e2ee000SAmadeusz Sławiński ret = soc_tplg_control_load(tplg, &kc, &mc->hdr); 781*ec5dffcdSAmadeusz Sławiński if (ret < 0) 7820db627c4SAmadeusz Sławiński goto err; 7838a978234SLiam Girdwood 7848a978234SLiam Girdwood /* register control here */ 7850db627c4SAmadeusz Sławiński ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol); 7860db627c4SAmadeusz Sławiński if (ret < 0) { 7870db627c4SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to add %s\n", mc->hdr.name); 7880db627c4SAmadeusz Sławiński goto err; 7898a978234SLiam Girdwood } 7908a978234SLiam Girdwood 7918a978234SLiam Girdwood list_add(&sm->dobj.list, &tplg->comp->dobj_list); 7928a978234SLiam Girdwood 7930db627c4SAmadeusz Sławiński err: 7940db627c4SAmadeusz Sławiński return ret; 7958a978234SLiam Girdwood } 7968a978234SLiam Girdwood 797ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se, 7988a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 7998a978234SLiam Girdwood { 8008a978234SLiam Girdwood int i, ret; 8018a978234SLiam Girdwood 802f5824e5cSAmadeusz Sławiński if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts)) 803f5824e5cSAmadeusz Sławiński return -EINVAL; 804f5824e5cSAmadeusz Sławiński 8058a978234SLiam Girdwood se->dobj.control.dtexts = 806ff922622SAmadeusz Sławiński devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); 8078a978234SLiam Girdwood if (se->dobj.control.dtexts == NULL) 8088a978234SLiam Girdwood return -ENOMEM; 8098a978234SLiam Girdwood 81072bbeda0SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(ec->items); i++) { 8118a978234SLiam Girdwood 8128a978234SLiam Girdwood if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 8138a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 8148a978234SLiam Girdwood ret = -EINVAL; 8158a978234SLiam Girdwood goto err; 8168a978234SLiam Girdwood } 8178a978234SLiam Girdwood 818ff922622SAmadeusz Sławiński se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL); 8198a978234SLiam Girdwood if (!se->dobj.control.dtexts[i]) { 8208a978234SLiam Girdwood ret = -ENOMEM; 8218a978234SLiam Girdwood goto err; 8228a978234SLiam Girdwood } 8238a978234SLiam Girdwood } 8248a978234SLiam Girdwood 8253cde818cSAmadeusz Sławiński se->items = le32_to_cpu(ec->items); 826b6e38b29SMousumi Jana se->texts = (const char * const *)se->dobj.control.dtexts; 8278a978234SLiam Girdwood return 0; 8288a978234SLiam Girdwood 8298a978234SLiam Girdwood err: 8303cde818cSAmadeusz Sławiński return ret; 8313cde818cSAmadeusz Sławiński } 8323cde818cSAmadeusz Sławiński 833ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se, 8348a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 8358a978234SLiam Girdwood { 8365aebe7c7SPierre-Louis Bossart int i; 8375aebe7c7SPierre-Louis Bossart 838631c78edSAmadeusz Sławiński /* 839631c78edSAmadeusz Sławiński * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS 840631c78edSAmadeusz Sławiński * values instead of using ARRAY_SIZE(ec->values) due to the fact that 841631c78edSAmadeusz Sławiński * it is oversized for its purpose. Additionally it is done so because 842631c78edSAmadeusz Sławiński * it is defined in UAPI header where it can't be easily changed. 843631c78edSAmadeusz Sławiński */ 844631c78edSAmadeusz Sławiński if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS) 8458a978234SLiam Girdwood return -EINVAL; 8468a978234SLiam Girdwood 847631c78edSAmadeusz Sławiński se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), 848543466efSDan Carpenter sizeof(*se->dobj.control.dvalues), 849376c0afeSAndrzej Hajda GFP_KERNEL); 8508a978234SLiam Girdwood if (!se->dobj.control.dvalues) 8518a978234SLiam Girdwood return -ENOMEM; 8528a978234SLiam Girdwood 8535aebe7c7SPierre-Louis Bossart /* convert from little-endian */ 8545aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(ec->items); i++) { 8555aebe7c7SPierre-Louis Bossart se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); 8565aebe7c7SPierre-Louis Bossart } 8575aebe7c7SPierre-Louis Bossart 8588a978234SLiam Girdwood return 0; 8598a978234SLiam Girdwood } 8608a978234SLiam Girdwood 8610db627c4SAmadeusz Sławiński static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size) 8628a978234SLiam Girdwood { 8638a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 8648a978234SLiam Girdwood struct soc_enum *se; 8658a978234SLiam Girdwood struct snd_kcontrol_new kc; 8660db627c4SAmadeusz Sławiński int ret = 0; 8678a978234SLiam Girdwood 8688a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 8698a978234SLiam Girdwood sizeof(struct snd_soc_tplg_enum_control), 8700db627c4SAmadeusz Sławiński 1, size, "enums")) 8718a978234SLiam Girdwood return -EINVAL; 8728a978234SLiam Girdwood 8738a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 8748a978234SLiam Girdwood 8758a978234SLiam Girdwood /* validate kcontrol */ 8768a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 8778a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 8788a978234SLiam Girdwood return -EINVAL; 8798a978234SLiam Girdwood 880ff922622SAmadeusz Sławiński se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL); 8818a978234SLiam Girdwood if (se == NULL) 8828a978234SLiam Girdwood return -ENOMEM; 8838a978234SLiam Girdwood 88402b64245SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 8855aebe7c7SPierre-Louis Bossart le32_to_cpu(ec->priv.size)); 88602b64245SLiam Girdwood 8878a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", 8888a978234SLiam Girdwood ec->hdr.name, ec->items); 8898a978234SLiam Girdwood 8908a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 8918a978234SLiam Girdwood kc.name = ec->hdr.name; 8928a978234SLiam Girdwood kc.private_value = (long)se; 8938a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 8945aebe7c7SPierre-Louis Bossart kc.access = le32_to_cpu(ec->hdr.access); 8958a978234SLiam Girdwood 8968f9974d9SAmadeusz Sławiński se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 8978f9974d9SAmadeusz Sławiński se->shift_l = tplg_chan_get_shift(tplg, ec->channel, 8988a978234SLiam Girdwood SNDRV_CHMAP_FL); 8998f9974d9SAmadeusz Sławiński se->shift_r = tplg_chan_get_shift(tplg, ec->channel, 9008a978234SLiam Girdwood SNDRV_CHMAP_FL); 9018a978234SLiam Girdwood 9025aebe7c7SPierre-Louis Bossart se->mask = le32_to_cpu(ec->mask); 9038a978234SLiam Girdwood se->dobj.index = tplg->index; 9048a978234SLiam Girdwood se->dobj.type = SND_SOC_DOBJ_ENUM; 90531e92739SAmadeusz Sławiński if (tplg->ops) 90631e92739SAmadeusz Sławiński se->dobj.unload = tplg->ops->control_unload; 9078a978234SLiam Girdwood INIT_LIST_HEAD(&se->dobj.list); 9088a978234SLiam Girdwood 9095aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(ec->hdr.ops.info)) { 9108a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 9118a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 9120db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create_values(tplg, se, ec); 9130db627c4SAmadeusz Sławiński if (ret < 0) { 9148a978234SLiam Girdwood dev_err(tplg->dev, 9158a978234SLiam Girdwood "ASoC: could not create values for %s\n", 9168a978234SLiam Girdwood ec->hdr.name); 9170db627c4SAmadeusz Sławiński goto err; 9188a978234SLiam Girdwood } 919df561f66SGustavo A. R. Silva fallthrough; 9208a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 9218a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 9228a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 9230db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create_texts(tplg, se, ec); 9240db627c4SAmadeusz Sławiński if (ret < 0) { 9258a978234SLiam Girdwood dev_err(tplg->dev, 9268a978234SLiam Girdwood "ASoC: could not create texts for %s\n", 9278a978234SLiam Girdwood ec->hdr.name); 9280db627c4SAmadeusz Sławiński goto err; 9298a978234SLiam Girdwood } 9308a978234SLiam Girdwood break; 9318a978234SLiam Girdwood default: 9320db627c4SAmadeusz Sławiński ret = -EINVAL; 9338a978234SLiam Girdwood dev_err(tplg->dev, 9348a978234SLiam Girdwood "ASoC: invalid enum control type %d for %s\n", 9358a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 9360db627c4SAmadeusz Sławiński goto err; 9378a978234SLiam Girdwood } 9388a978234SLiam Girdwood 9398a978234SLiam Girdwood /* map io handlers */ 9400db627c4SAmadeusz Sławiński ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); 9410db627c4SAmadeusz Sławiński if (ret) { 9428a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 9430db627c4SAmadeusz Sławiński goto err; 9448a978234SLiam Girdwood } 9458a978234SLiam Girdwood 9468a978234SLiam Girdwood /* pass control to driver for optional further init */ 9479e2ee000SAmadeusz Sławiński ret = soc_tplg_control_load(tplg, &kc, &ec->hdr); 948*ec5dffcdSAmadeusz Sławiński if (ret < 0) 9490db627c4SAmadeusz Sławiński goto err; 9508a978234SLiam Girdwood 9518a978234SLiam Girdwood /* register control here */ 9520db627c4SAmadeusz Sławiński ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol); 9530db627c4SAmadeusz Sławiński if (ret < 0) { 9540db627c4SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", ec->hdr.name); 9550db627c4SAmadeusz Sławiński goto err; 9568a978234SLiam Girdwood } 9578a978234SLiam Girdwood 9588a978234SLiam Girdwood list_add(&se->dobj.list, &tplg->comp->dobj_list); 959952bd937SPierre-Louis Bossart 9600db627c4SAmadeusz Sławiński err: 9610db627c4SAmadeusz Sławiński return ret; 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 { 9672ae548f3SAmadeusz Sławiński int ret; 9688a978234SLiam Girdwood int i; 9698a978234SLiam Girdwood 9708a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, 9718a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 9728a978234SLiam Girdwood 9735aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(hdr->count); i++) { 974ea8f6b29SKuninori Morimoto struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 9758a978234SLiam Girdwood 9765aebe7c7SPierre-Louis Bossart if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { 97706eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid control size\n"); 97806eb49f7SMengdong Lin return -EINVAL; 97906eb49f7SMengdong Lin } 98006eb49f7SMengdong Lin 9815aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(control_hdr->ops.info)) { 9828a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 9838a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 9848a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 9858a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 9868a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 9878a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 9888a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_PIN: 9890db627c4SAmadeusz Sławiński ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size)); 9908a978234SLiam Girdwood break; 9918a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 9928a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 9938a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 9948a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 9958a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 9960db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size)); 9978a978234SLiam Girdwood break; 9988a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 9990db627c4SAmadeusz Sławiński ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size)); 10008a978234SLiam Girdwood break; 10018a978234SLiam Girdwood default: 10028a978234SLiam Girdwood soc_bind_err(tplg, control_hdr, i); 10038a978234SLiam Girdwood return -EINVAL; 10048a978234SLiam Girdwood } 10052ae548f3SAmadeusz Sławiński if (ret < 0) { 10062ae548f3SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid control\n"); 10072ae548f3SAmadeusz Sławiński return ret; 10082ae548f3SAmadeusz Sławiński } 10092ae548f3SAmadeusz Sławiński 10108a978234SLiam Girdwood } 10118a978234SLiam Girdwood 10128a978234SLiam Girdwood return 0; 10138a978234SLiam Girdwood } 10148a978234SLiam Girdwood 1015503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */ 1016503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg, 1017503e79b7SLiam Girdwood struct snd_soc_dapm_route *route) 1018503e79b7SLiam Girdwood { 1019c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->dapm_route_load) 1020503e79b7SLiam Girdwood return tplg->ops->dapm_route_load(tplg->comp, tplg->index, 1021503e79b7SLiam Girdwood route); 1022503e79b7SLiam Girdwood 1023503e79b7SLiam Girdwood return 0; 1024503e79b7SLiam Girdwood } 1025503e79b7SLiam Girdwood 10268a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, 10278a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 10288a978234SLiam Girdwood { 10298a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 10308a978234SLiam Girdwood struct snd_soc_tplg_dapm_graph_elem *elem; 1031cc44c749SAmadeusz Sławiński struct snd_soc_dapm_route *route; 1032ff922622SAmadeusz Sławiński int count, i; 10337df04ea7SRanjani Sridharan int ret = 0; 10348a978234SLiam Girdwood 10355aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 10365aebe7c7SPierre-Louis Bossart 10378a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 10388a978234SLiam Girdwood sizeof(struct snd_soc_tplg_dapm_graph_elem), 10393ce57f22SAmadeusz Sławiński count, le32_to_cpu(hdr->payload_size), "graph")) 10408a978234SLiam Girdwood return -EINVAL; 10418a978234SLiam Girdwood 1042b75a6511SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, 1043b75a6511SLiam Girdwood hdr->index); 10448a978234SLiam Girdwood 10457df04ea7SRanjani Sridharan for (i = 0; i < count; i++) { 1046cc44c749SAmadeusz Sławiński route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL); 1047cc44c749SAmadeusz Sławiński if (!route) 10487df04ea7SRanjani Sridharan return -ENOMEM; 10498a978234SLiam Girdwood elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; 10508a978234SLiam Girdwood tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); 10518a978234SLiam Girdwood 10528a978234SLiam Girdwood /* validate routes */ 10538a978234SLiam Girdwood if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 10547df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 10557df04ea7SRanjani Sridharan ret = -EINVAL; 10567df04ea7SRanjani Sridharan break; 10577df04ea7SRanjani Sridharan } 10588a978234SLiam Girdwood if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 10597df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 10607df04ea7SRanjani Sridharan ret = -EINVAL; 10617df04ea7SRanjani Sridharan break; 10627df04ea7SRanjani Sridharan } 10638a978234SLiam Girdwood if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 10647df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 10657df04ea7SRanjani Sridharan ret = -EINVAL; 10667df04ea7SRanjani Sridharan break; 10678a978234SLiam Girdwood } 10688a978234SLiam Girdwood 1069cc44c749SAmadeusz Sławiński route->source = elem->source; 1070cc44c749SAmadeusz Sławiński route->sink = elem->sink; 10717df04ea7SRanjani Sridharan 10727df04ea7SRanjani Sridharan /* set to NULL atm for tplg users */ 1073cc44c749SAmadeusz Sławiński route->connected = NULL; 10747df04ea7SRanjani Sridharan if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) 1075cc44c749SAmadeusz Sławiński route->control = NULL; 10767df04ea7SRanjani Sridharan else 1077cc44c749SAmadeusz Sławiński route->control = elem->control; 10787df04ea7SRanjani Sridharan 10797df04ea7SRanjani Sridharan /* add route dobj to dobj_list */ 1080cc44c749SAmadeusz Sławiński route->dobj.type = SND_SOC_DOBJ_GRAPH; 108131e92739SAmadeusz Sławiński if (tplg->ops) 1082dd184c40SPeter Ujfalusi route->dobj.unload = tplg->ops->dapm_route_unload; 1083cc44c749SAmadeusz Sławiński route->dobj.index = tplg->index; 1084cc44c749SAmadeusz Sławiński list_add(&route->dobj.list, &tplg->comp->dobj_list); 10857df04ea7SRanjani Sridharan 1086cc44c749SAmadeusz Sławiński ret = soc_tplg_add_route(tplg, route); 10876f0307dfSPierre-Louis Bossart if (ret < 0) { 10888bf9475fSPierre-Louis Bossart dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret); 10896856e887SAmadeusz Sławiński break; 10906f0307dfSPierre-Louis Bossart } 10917df04ea7SRanjani Sridharan 10927df04ea7SRanjani Sridharan /* add route, but keep going if some fail */ 1093cc44c749SAmadeusz Sławiński snd_soc_dapm_add_routes(dapm, route, 1); 10947df04ea7SRanjani Sridharan } 10957df04ea7SRanjani Sridharan 10967df04ea7SRanjani Sridharan return ret; 10978a978234SLiam Girdwood } 10988a978234SLiam Girdwood 1099d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 11008a978234SLiam Girdwood { 11018a978234SLiam Girdwood struct soc_mixer_control *sm; 11028a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 1103d29d41e2SJaska Uimonen int err; 11048a978234SLiam Girdwood 11058a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 11068a978234SLiam Girdwood 11078a978234SLiam Girdwood /* validate kcontrol */ 11088a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 11098a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1110d29d41e2SJaska Uimonen return -EINVAL; 11119f90af3aSAmadeusz Sławiński 1112ff922622SAmadeusz Sławiński sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL); 1113d29d41e2SJaska Uimonen if (!sm) 1114d29d41e2SJaska Uimonen return -ENOMEM; 11158a978234SLiam Girdwood 1116d29d41e2SJaska Uimonen tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + 1117d29d41e2SJaska Uimonen le32_to_cpu(mc->priv.size); 111802b64245SLiam Girdwood 1119d29d41e2SJaska Uimonen dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n", 1120d29d41e2SJaska Uimonen mc->hdr.name); 11218a978234SLiam Girdwood 1122d29d41e2SJaska Uimonen kc->private_value = (long)sm; 1123d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL); 1124d29d41e2SJaska Uimonen if (!kc->name) 1125d29d41e2SJaska Uimonen return -ENOMEM; 1126d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1127d29d41e2SJaska Uimonen kc->access = le32_to_cpu(mc->hdr.access); 11288a978234SLiam Girdwood 11298a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 11308f9974d9SAmadeusz Sławiński sm->reg = tplg_chan_get_reg(tplg, mc->channel, 11318a978234SLiam Girdwood SNDRV_CHMAP_FL); 11328f9974d9SAmadeusz Sławiński sm->rreg = tplg_chan_get_reg(tplg, mc->channel, 11338a978234SLiam Girdwood SNDRV_CHMAP_FR); 11348f9974d9SAmadeusz Sławiński sm->shift = tplg_chan_get_shift(tplg, mc->channel, 11358a978234SLiam Girdwood SNDRV_CHMAP_FL); 11368f9974d9SAmadeusz Sławiński sm->rshift = tplg_chan_get_shift(tplg, mc->channel, 11378a978234SLiam Girdwood SNDRV_CHMAP_FR); 11388a978234SLiam Girdwood 113972bbeda0SPierre-Louis Bossart sm->max = le32_to_cpu(mc->max); 114072bbeda0SPierre-Louis Bossart sm->min = le32_to_cpu(mc->min); 114172bbeda0SPierre-Louis Bossart sm->invert = le32_to_cpu(mc->invert); 114272bbeda0SPierre-Louis Bossart sm->platform_max = le32_to_cpu(mc->platform_max); 11438a978234SLiam Girdwood sm->dobj.index = tplg->index; 11448a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 11458a978234SLiam Girdwood 11468a978234SLiam Girdwood /* map io handlers */ 1147d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg); 11488a978234SLiam Girdwood if (err) { 11498a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 1150d29d41e2SJaska Uimonen return err; 11518a978234SLiam Girdwood } 11528a978234SLiam Girdwood 11533789debfSBard liao /* create any TLV data */ 1154d29d41e2SJaska Uimonen err = soc_tplg_create_tlv(tplg, kc, &mc->hdr); 1155482db55aSAmadeusz Sławiński if (err < 0) { 1156482db55aSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", 1157482db55aSAmadeusz Sławiński mc->hdr.name); 1158d29d41e2SJaska Uimonen return err; 1159482db55aSAmadeusz Sławiński } 11603789debfSBard liao 11618a978234SLiam Girdwood /* pass control to driver for optional further init */ 11629e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &mc->hdr); 1163*ec5dffcdSAmadeusz Sławiński if (err < 0) 1164d29d41e2SJaska Uimonen return err; 11658a978234SLiam Girdwood 1166d29d41e2SJaska Uimonen return 0; 1167d29d41e2SJaska Uimonen } 1168d29d41e2SJaska Uimonen 1169d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 11708a978234SLiam Girdwood { 11718a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 11728a978234SLiam Girdwood struct soc_enum *se; 1173d29d41e2SJaska Uimonen int err; 11748a978234SLiam Girdwood 11758a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 11768a978234SLiam Girdwood /* validate kcontrol */ 11778a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 11788a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1179d29d41e2SJaska Uimonen return -EINVAL; 11808a978234SLiam Girdwood 1181ff922622SAmadeusz Sławiński se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL); 1182d29d41e2SJaska Uimonen if (!se) 1183d29d41e2SJaska Uimonen return -ENOMEM; 11848a978234SLiam Girdwood 118502b64245SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 118672bbeda0SPierre-Louis Bossart le32_to_cpu(ec->priv.size)); 118702b64245SLiam Girdwood 11888a978234SLiam Girdwood dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", 11898a978234SLiam Girdwood ec->hdr.name); 11908a978234SLiam Girdwood 1191d29d41e2SJaska Uimonen kc->private_value = (long)se; 1192d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL); 1193d29d41e2SJaska Uimonen if (!kc->name) 1194d29d41e2SJaska Uimonen return -ENOMEM; 1195d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1196d29d41e2SJaska Uimonen kc->access = le32_to_cpu(ec->hdr.access); 11978a978234SLiam Girdwood 11988a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 11998f9974d9SAmadeusz Sławiński se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 12008f9974d9SAmadeusz Sławiński se->shift_l = tplg_chan_get_shift(tplg, ec->channel, 12011a7dd6e2SMengdong Lin SNDRV_CHMAP_FL); 12028f9974d9SAmadeusz Sławiński se->shift_r = tplg_chan_get_shift(tplg, ec->channel, 12031a7dd6e2SMengdong Lin SNDRV_CHMAP_FR); 12048a978234SLiam Girdwood 120572bbeda0SPierre-Louis Bossart se->items = le32_to_cpu(ec->items); 120672bbeda0SPierre-Louis Bossart se->mask = le32_to_cpu(ec->mask); 12078a978234SLiam Girdwood se->dobj.index = tplg->index; 12088a978234SLiam Girdwood 12095aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(ec->hdr.ops.info)) { 12108a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 12118a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1212ff922622SAmadeusz Sławiński err = soc_tplg_denum_create_values(tplg, se, ec); 12138a978234SLiam Girdwood if (err < 0) { 12148a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create values for %s\n", 12158a978234SLiam Girdwood ec->hdr.name); 1216d29d41e2SJaska Uimonen return err; 12178a978234SLiam Girdwood } 1218df561f66SGustavo A. R. Silva fallthrough; 12198a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 12208a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 12218a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 1222ff922622SAmadeusz Sławiński err = soc_tplg_denum_create_texts(tplg, se, ec); 12238a978234SLiam Girdwood if (err < 0) { 12248a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create texts for %s\n", 12258a978234SLiam Girdwood ec->hdr.name); 1226d29d41e2SJaska Uimonen return err; 12278a978234SLiam Girdwood } 12288a978234SLiam Girdwood break; 12298a978234SLiam Girdwood default: 12308a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", 12318a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 1232d29d41e2SJaska Uimonen return -EINVAL; 12338a978234SLiam Girdwood } 12348a978234SLiam Girdwood 12358a978234SLiam Girdwood /* map io handlers */ 1236d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg); 12378a978234SLiam Girdwood if (err) { 12388a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 1239d29d41e2SJaska Uimonen return err; 12408a978234SLiam Girdwood } 12418a978234SLiam Girdwood 12428a978234SLiam Girdwood /* pass control to driver for optional further init */ 12439e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &ec->hdr); 1244*ec5dffcdSAmadeusz Sławiński if (err < 0) 1245d29d41e2SJaska Uimonen return err; 12461a7dd6e2SMengdong Lin 1247d29d41e2SJaska Uimonen return 0; 12488a978234SLiam Girdwood } 12498a978234SLiam Girdwood 1250d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 12518a978234SLiam Girdwood { 12528a978234SLiam Girdwood struct snd_soc_tplg_bytes_control *be; 12538a978234SLiam Girdwood struct soc_bytes_ext *sbe; 1254d29d41e2SJaska Uimonen int err; 12558a978234SLiam Girdwood 12568a978234SLiam Girdwood be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 12578a978234SLiam Girdwood 12588a978234SLiam Girdwood /* validate kcontrol */ 12598a978234SLiam Girdwood if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12608a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1261d29d41e2SJaska Uimonen return -EINVAL; 12628a978234SLiam Girdwood 1263ff922622SAmadeusz Sławiński sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL); 1264d29d41e2SJaska Uimonen if (!sbe) 1265d29d41e2SJaska Uimonen return -ENOMEM; 12668a978234SLiam Girdwood 12678a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + 12685aebe7c7SPierre-Louis Bossart le32_to_cpu(be->priv.size)); 12698a978234SLiam Girdwood 12708a978234SLiam Girdwood dev_dbg(tplg->dev, 12718a978234SLiam Girdwood "ASoC: adding bytes kcontrol %s with access 0x%x\n", 12728a978234SLiam Girdwood be->hdr.name, be->hdr.access); 12738a978234SLiam Girdwood 1274d29d41e2SJaska Uimonen kc->private_value = (long)sbe; 1275d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL); 1276d29d41e2SJaska Uimonen if (!kc->name) 1277d29d41e2SJaska Uimonen return -ENOMEM; 1278d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1279d29d41e2SJaska Uimonen kc->access = le32_to_cpu(be->hdr.access); 12808a978234SLiam Girdwood 128172bbeda0SPierre-Louis Bossart sbe->max = le32_to_cpu(be->max); 12828a978234SLiam Girdwood INIT_LIST_HEAD(&sbe->dobj.list); 12838a978234SLiam Girdwood 12848a978234SLiam Girdwood /* map standard io handlers and check for external handlers */ 1285d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg); 12868a978234SLiam Girdwood if (err) { 12878a978234SLiam Girdwood soc_control_err(tplg, &be->hdr, be->hdr.name); 1288d29d41e2SJaska Uimonen return err; 12898a978234SLiam Girdwood } 12908a978234SLiam Girdwood 12918a978234SLiam Girdwood /* pass control to driver for optional further init */ 12929e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &be->hdr); 1293*ec5dffcdSAmadeusz Sławiński if (err < 0) 1294d29d41e2SJaska Uimonen return err; 12958a978234SLiam Girdwood 1296d29d41e2SJaska Uimonen return 0; 12978a978234SLiam Girdwood } 12988a978234SLiam Girdwood 12998a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, 13008a978234SLiam Girdwood struct snd_soc_tplg_dapm_widget *w) 13018a978234SLiam Girdwood { 13028a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 13038a978234SLiam Girdwood struct snd_soc_dapm_widget template, *widget; 13048a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *control_hdr; 13058a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 1306b9c035aaSJaska Uimonen unsigned int *kcontrol_type = NULL; 1307d29d41e2SJaska Uimonen struct snd_kcontrol_new *kc; 1308d29d41e2SJaska Uimonen int mixer_count = 0; 1309d29d41e2SJaska Uimonen int bytes_count = 0; 1310d29d41e2SJaska Uimonen int enum_count = 0; 13118a978234SLiam Girdwood int ret = 0; 1312d29d41e2SJaska Uimonen int i; 13138a978234SLiam Girdwood 13148a978234SLiam Girdwood if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 13158a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 13168a978234SLiam Girdwood return -EINVAL; 13178a978234SLiam Girdwood if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 13188a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 13198a978234SLiam Girdwood return -EINVAL; 13208a978234SLiam Girdwood 13218a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", 13228a978234SLiam Girdwood w->name, w->id); 13238a978234SLiam Girdwood 13248a978234SLiam Girdwood memset(&template, 0, sizeof(template)); 13258a978234SLiam Girdwood 13268a978234SLiam Girdwood /* map user to kernel widget ID */ 13275aebe7c7SPierre-Louis Bossart template.id = get_widget_id(le32_to_cpu(w->id)); 1328752c938aSDan Carpenter if ((int)template.id < 0) 13298a978234SLiam Girdwood return template.id; 13308a978234SLiam Girdwood 1331c3421a6aSLiam Girdwood /* strings are allocated here, but used and freed by the widget */ 13328a978234SLiam Girdwood template.name = kstrdup(w->name, GFP_KERNEL); 13338a978234SLiam Girdwood if (!template.name) 13348a978234SLiam Girdwood return -ENOMEM; 13358a978234SLiam Girdwood template.sname = kstrdup(w->sname, GFP_KERNEL); 13368a978234SLiam Girdwood if (!template.sname) { 13378a978234SLiam Girdwood ret = -ENOMEM; 13388a978234SLiam Girdwood goto err; 13398a978234SLiam Girdwood } 13405aebe7c7SPierre-Louis Bossart template.reg = le32_to_cpu(w->reg); 13415aebe7c7SPierre-Louis Bossart template.shift = le32_to_cpu(w->shift); 13425aebe7c7SPierre-Louis Bossart template.mask = le32_to_cpu(w->mask); 13435aebe7c7SPierre-Louis Bossart template.subseq = le32_to_cpu(w->subseq); 13448a978234SLiam Girdwood template.on_val = w->invert ? 0 : 1; 13458a978234SLiam Girdwood template.off_val = w->invert ? 1 : 0; 13465aebe7c7SPierre-Louis Bossart template.ignore_suspend = le32_to_cpu(w->ignore_suspend); 13475aebe7c7SPierre-Louis Bossart template.event_flags = le16_to_cpu(w->event_flags); 13488a978234SLiam Girdwood template.dobj.index = tplg->index; 13498a978234SLiam Girdwood 13508a978234SLiam Girdwood tplg->pos += 13515aebe7c7SPierre-Louis Bossart (sizeof(struct snd_soc_tplg_dapm_widget) + 13525aebe7c7SPierre-Louis Bossart le32_to_cpu(w->priv.size)); 13535aebe7c7SPierre-Louis Bossart 13548a978234SLiam Girdwood if (w->num_kcontrols == 0) { 13558a978234SLiam Girdwood template.num_kcontrols = 0; 13568a978234SLiam Girdwood goto widget; 13578a978234SLiam Girdwood } 13588a978234SLiam Girdwood 1359d29d41e2SJaska Uimonen template.num_kcontrols = le32_to_cpu(w->num_kcontrols); 1360d29d41e2SJaska Uimonen kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL); 1361c173ee5bSAmadeusz Sławiński if (!kc) { 1362c173ee5bSAmadeusz Sławiński ret = -ENOMEM; 13639c363532SPeter Ujfalusi goto hdr_err; 1364c173ee5bSAmadeusz Sławiński } 1365d29d41e2SJaska Uimonen 1366d29d41e2SJaska Uimonen kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int), 1367d29d41e2SJaska Uimonen GFP_KERNEL); 1368c173ee5bSAmadeusz Sławiński if (!kcontrol_type) { 1369c173ee5bSAmadeusz Sławiński ret = -ENOMEM; 13709c363532SPeter Ujfalusi goto hdr_err; 1371c173ee5bSAmadeusz Sławiński } 1372d29d41e2SJaska Uimonen 13731baad7daSPierre-Louis Bossart for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) { 1374d29d41e2SJaska Uimonen control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 13755aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(control_hdr->ops.info)) { 13768a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 13778a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 13788a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 13798a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 13808a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 13818a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 1382d29d41e2SJaska Uimonen /* volume mixer */ 1383d29d41e2SJaska Uimonen kc[i].index = mixer_count; 1384d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER; 1385d29d41e2SJaska Uimonen mixer_count++; 1386d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]); 1387d29d41e2SJaska Uimonen if (ret < 0) 13888a978234SLiam Girdwood goto hdr_err; 13898a978234SLiam Girdwood break; 13908a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 13918a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 13928a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 13938a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 13948a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1395d29d41e2SJaska Uimonen /* enumerated mixer */ 1396d29d41e2SJaska Uimonen kc[i].index = enum_count; 1397d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM; 1398d29d41e2SJaska Uimonen enum_count++; 1399d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]); 1400d29d41e2SJaska Uimonen if (ret < 0) 14018a978234SLiam Girdwood goto hdr_err; 14028a978234SLiam Girdwood break; 14038a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 1404d29d41e2SJaska Uimonen /* bytes control */ 1405d29d41e2SJaska Uimonen kc[i].index = bytes_count; 1406d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES; 1407d29d41e2SJaska Uimonen bytes_count++; 1408d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]); 1409d29d41e2SJaska Uimonen if (ret < 0) 14108a978234SLiam Girdwood goto hdr_err; 14118a978234SLiam Girdwood break; 14128a978234SLiam Girdwood default: 14138a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", 14148a978234SLiam Girdwood control_hdr->ops.get, control_hdr->ops.put, 14155aebe7c7SPierre-Louis Bossart le32_to_cpu(control_hdr->ops.info)); 14168a978234SLiam Girdwood ret = -EINVAL; 14178a978234SLiam Girdwood goto hdr_err; 14188a978234SLiam Girdwood } 1419d29d41e2SJaska Uimonen } 1420d29d41e2SJaska Uimonen 1421d29d41e2SJaska Uimonen template.kcontrol_news = kc; 14228facf84bSPeter Ujfalusi dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n", 14238facf84bSPeter Ujfalusi w->name, mixer_count, enum_count, bytes_count); 14248a978234SLiam Girdwood 14258a978234SLiam Girdwood widget: 14268a978234SLiam Girdwood ret = soc_tplg_widget_load(tplg, &template, w); 14278a978234SLiam Girdwood if (ret < 0) 14288a978234SLiam Girdwood goto hdr_err; 14298a978234SLiam Girdwood 14308a978234SLiam Girdwood /* card dapm mutex is held by the core if we are loading topology 14318a978234SLiam Girdwood * data during sound card init. */ 14322b34c135SKuninori Morimoto if (snd_soc_card_is_instantiated(card)) 14338a978234SLiam Girdwood widget = snd_soc_dapm_new_control(dapm, &template); 14348a978234SLiam Girdwood else 14358a978234SLiam Girdwood widget = snd_soc_dapm_new_control_unlocked(dapm, &template); 143637e1df8cSLinus Walleij if (IS_ERR(widget)) { 143737e1df8cSLinus Walleij ret = PTR_ERR(widget); 14388a978234SLiam Girdwood goto hdr_err; 14398a978234SLiam Girdwood } 14408a978234SLiam Girdwood 14418a978234SLiam Girdwood widget->dobj.type = SND_SOC_DOBJ_WIDGET; 1442eea3dd4fSMengdong Lin widget->dobj.widget.kcontrol_type = kcontrol_type; 144331e92739SAmadeusz Sławiński if (tplg->ops) 144431e92739SAmadeusz Sławiński widget->dobj.unload = tplg->ops->widget_unload; 14458a978234SLiam Girdwood widget->dobj.index = tplg->index; 14468a978234SLiam Girdwood list_add(&widget->dobj.list, &tplg->comp->dobj_list); 1447ebd259d3SLiam Girdwood 1448ebd259d3SLiam Girdwood ret = soc_tplg_widget_ready(tplg, widget, w); 1449ebd259d3SLiam Girdwood if (ret < 0) 1450ebd259d3SLiam Girdwood goto ready_err; 1451ebd259d3SLiam Girdwood 14527620fe91SBard liao kfree(template.sname); 14537620fe91SBard liao kfree(template.name); 14547620fe91SBard liao 14558a978234SLiam Girdwood return 0; 14568a978234SLiam Girdwood 1457ebd259d3SLiam Girdwood ready_err: 14582abfd4bdSAmadeusz Sławiński soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET); 1459ebd259d3SLiam Girdwood snd_soc_dapm_free_widget(widget); 14608a978234SLiam Girdwood hdr_err: 14618a978234SLiam Girdwood kfree(template.sname); 14628a978234SLiam Girdwood err: 14638a978234SLiam Girdwood kfree(template.name); 14648a978234SLiam Girdwood return ret; 14658a978234SLiam Girdwood } 14668a978234SLiam Girdwood 14678a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, 14688a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 14698a978234SLiam Girdwood { 1470e9aa139fSKuninori Morimoto int count, i; 14715aebe7c7SPierre-Louis Bossart 14725aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 14738a978234SLiam Girdwood 14748a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); 14758a978234SLiam Girdwood 14768a978234SLiam Girdwood for (i = 0; i < count; i++) { 1477e9aa139fSKuninori Morimoto struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; 1478e9aa139fSKuninori Morimoto int ret; 1479e9aa139fSKuninori Morimoto 14802e288333SAmadeusz Sławiński /* 14812e288333SAmadeusz Sławiński * check if widget itself fits within topology file 14822e288333SAmadeusz Sławiński * use sizeof instead of widget->size, as we can't be sure 14832e288333SAmadeusz Sławiński * it is set properly yet (file may end before it is present) 14842e288333SAmadeusz Sławiński */ 14852e288333SAmadeusz Sławiński if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) { 14862e288333SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid widget data size\n"); 14872e288333SAmadeusz Sławiński return -EINVAL; 14882e288333SAmadeusz Sławiński } 14892e288333SAmadeusz Sławiński 14902e288333SAmadeusz Sławiński /* check if widget has proper size */ 14915aebe7c7SPierre-Louis Bossart if (le32_to_cpu(widget->size) != sizeof(*widget)) { 149206eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid widget size\n"); 149306eb49f7SMengdong Lin return -EINVAL; 149406eb49f7SMengdong Lin } 149506eb49f7SMengdong Lin 14962e288333SAmadeusz Sławiński /* check if widget private data fits within topology file */ 14972e288333SAmadeusz Sławiński if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) { 14982e288333SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid widget private data size\n"); 14992e288333SAmadeusz Sławiński return -EINVAL; 15002e288333SAmadeusz Sławiński } 15012e288333SAmadeusz Sławiński 15028a978234SLiam Girdwood ret = soc_tplg_dapm_widget_create(tplg, widget); 15037de76b62SMengdong Lin if (ret < 0) { 15048a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to load widget %s\n", 15058a978234SLiam Girdwood widget->name); 15067de76b62SMengdong Lin return ret; 15077de76b62SMengdong Lin } 15088a978234SLiam Girdwood } 15098a978234SLiam Girdwood 15108a978234SLiam Girdwood return 0; 15118a978234SLiam Girdwood } 15128a978234SLiam Girdwood 15138a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg) 15148a978234SLiam Girdwood { 15158a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 15168a978234SLiam Girdwood int ret; 15178a978234SLiam Girdwood 15188a978234SLiam Girdwood /* Card might not have been registered at this point. 15198a978234SLiam Girdwood * If so, just return success. 15208a978234SLiam Girdwood */ 15212b34c135SKuninori Morimoto if (!snd_soc_card_is_instantiated(card)) { 15228a978234SLiam Girdwood dev_warn(tplg->dev, "ASoC: Parent card not yet available," 1523cc9d4714SLiam Girdwood " widget card binding deferred\n"); 15248a978234SLiam Girdwood return 0; 15258a978234SLiam Girdwood } 15268a978234SLiam Girdwood 15278a978234SLiam Girdwood ret = snd_soc_dapm_new_widgets(card); 15288a978234SLiam Girdwood if (ret < 0) 15298a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", 15308a978234SLiam Girdwood ret); 15318a978234SLiam Girdwood 1532b784617aSAmadeusz Sławiński return ret; 15338a978234SLiam Girdwood } 15348a978234SLiam Girdwood 1535ff922622SAmadeusz Sławiński static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream, 1536b6b6e4d6SMengdong Lin struct snd_soc_tplg_stream_caps *caps) 1537b6b6e4d6SMengdong Lin { 1538ff922622SAmadeusz Sławiński stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL); 1539abc3caacSAmadeusz Sławiński if (!stream->stream_name) 1540abc3caacSAmadeusz Sławiński return -ENOMEM; 1541abc3caacSAmadeusz Sławiński 15425aebe7c7SPierre-Louis Bossart stream->channels_min = le32_to_cpu(caps->channels_min); 15435aebe7c7SPierre-Louis Bossart stream->channels_max = le32_to_cpu(caps->channels_max); 15445aebe7c7SPierre-Louis Bossart stream->rates = le32_to_cpu(caps->rates); 15455aebe7c7SPierre-Louis Bossart stream->rate_min = le32_to_cpu(caps->rate_min); 15465aebe7c7SPierre-Louis Bossart stream->rate_max = le32_to_cpu(caps->rate_max); 15475aebe7c7SPierre-Louis Bossart stream->formats = le64_to_cpu(caps->formats); 15485aebe7c7SPierre-Louis Bossart stream->sig_bits = le32_to_cpu(caps->sig_bits); 1549abc3caacSAmadeusz Sławiński 1550abc3caacSAmadeusz Sławiński return 0; 1551b6b6e4d6SMengdong Lin } 1552b6b6e4d6SMengdong Lin 15530038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, 15540038be9aSMengdong Lin unsigned int flag_mask, unsigned int flags) 15550038be9aSMengdong Lin { 15560038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) 1557f14654ddSKuninori Morimoto dai_drv->symmetric_rate = 155847108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 15590038be9aSMengdong Lin 15600038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) 15610038be9aSMengdong Lin dai_drv->symmetric_channels = 156247108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ? 15630038be9aSMengdong Lin 1 : 0; 15640038be9aSMengdong Lin 15650038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) 1566f14654ddSKuninori Morimoto dai_drv->symmetric_sample_bits = 156747108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 15680038be9aSMengdong Lin 1 : 0; 15690038be9aSMengdong Lin } 15700038be9aSMengdong Lin 157164527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg, 157264527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 157364527e8aSMengdong Lin { 157464527e8aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 157564527e8aSMengdong Lin struct snd_soc_pcm_stream *stream; 157664527e8aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 1577e443c205SKuninori Morimoto struct snd_soc_dai *dai; 1578e443c205SKuninori Morimoto struct snd_soc_dapm_context *dapm = 1579e443c205SKuninori Morimoto snd_soc_component_get_dapm(tplg->comp); 158064527e8aSMengdong Lin int ret; 158164527e8aSMengdong Lin 1582ff922622SAmadeusz Sławiński dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL); 158364527e8aSMengdong Lin if (dai_drv == NULL) 158464527e8aSMengdong Lin return -ENOMEM; 158564527e8aSMengdong Lin 1586abc3caacSAmadeusz Sławiński if (strlen(pcm->dai_name)) { 1587ff922622SAmadeusz Sławiński dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1588abc3caacSAmadeusz Sławiński if (!dai_drv->name) { 1589abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1590abc3caacSAmadeusz Sławiński goto err; 1591abc3caacSAmadeusz Sławiński } 1592abc3caacSAmadeusz Sławiński } 15935aebe7c7SPierre-Louis Bossart dai_drv->id = le32_to_cpu(pcm->dai_id); 159464527e8aSMengdong Lin 159564527e8aSMengdong Lin if (pcm->playback) { 159664527e8aSMengdong Lin stream = &dai_drv->playback; 159764527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 1598ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 1599abc3caacSAmadeusz Sławiński if (ret < 0) 1600abc3caacSAmadeusz Sławiński goto err; 160164527e8aSMengdong Lin } 160264527e8aSMengdong Lin 160364527e8aSMengdong Lin if (pcm->capture) { 160464527e8aSMengdong Lin stream = &dai_drv->capture; 160564527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 1606ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 1607abc3caacSAmadeusz Sławiński if (ret < 0) 1608abc3caacSAmadeusz Sławiński goto err; 160964527e8aSMengdong Lin } 161064527e8aSMengdong Lin 16115db6aab6SLiam Girdwood if (pcm->compress) 16125db6aab6SLiam Girdwood dai_drv->compress_new = snd_soc_new_compress; 16135db6aab6SLiam Girdwood 161464527e8aSMengdong Lin /* pass control to component driver for optional further init */ 1615c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); 161664527e8aSMengdong Lin if (ret < 0) { 1617e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 1618abc3caacSAmadeusz Sławiński goto err; 161964527e8aSMengdong Lin } 162064527e8aSMengdong Lin 162164527e8aSMengdong Lin dai_drv->dobj.index = tplg->index; 162264527e8aSMengdong Lin dai_drv->dobj.type = SND_SOC_DOBJ_PCM; 162331e92739SAmadeusz Sławiński if (tplg->ops) 162431e92739SAmadeusz Sławiński dai_drv->dobj.unload = tplg->ops->dai_unload; 162564527e8aSMengdong Lin list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); 162664527e8aSMengdong Lin 162764527e8aSMengdong Lin /* register the DAI to the component */ 1628fc4cb1e1SAmadeusz Sławiński dai = snd_soc_register_dai(tplg->comp, dai_drv, false); 1629e443c205SKuninori Morimoto if (!dai) 1630e443c205SKuninori Morimoto return -ENOMEM; 1631e443c205SKuninori Morimoto 1632e443c205SKuninori Morimoto /* Create the DAI widgets here */ 1633e443c205SKuninori Morimoto ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1634e443c205SKuninori Morimoto if (ret != 0) { 1635e443c205SKuninori Morimoto dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret); 1636fc4cb1e1SAmadeusz Sławiński snd_soc_unregister_dai(dai); 1637e443c205SKuninori Morimoto return ret; 1638e443c205SKuninori Morimoto } 1639e443c205SKuninori Morimoto 1640abc3caacSAmadeusz Sławiński return 0; 1641abc3caacSAmadeusz Sławiński 1642abc3caacSAmadeusz Sławiński err: 1643e443c205SKuninori Morimoto return ret; 164464527e8aSMengdong Lin } 164564527e8aSMengdong Lin 1646717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link, 1647717a8e72SMengdong Lin unsigned int flag_mask, unsigned int flags) 1648717a8e72SMengdong Lin { 1649717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) 1650f14654ddSKuninori Morimoto link->symmetric_rate = 165147108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 1652717a8e72SMengdong Lin 1653717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) 1654717a8e72SMengdong Lin link->symmetric_channels = 165547108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ? 1656717a8e72SMengdong Lin 1 : 0; 1657717a8e72SMengdong Lin 1658717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) 1659f14654ddSKuninori Morimoto link->symmetric_sample_bits = 166047108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 1661717a8e72SMengdong Lin 1 : 0; 16626ff67ccaSMengdong Lin 16636ff67ccaSMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) 16646ff67ccaSMengdong Lin link->ignore_suspend = 166547108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ? 16666ff67ccaSMengdong Lin 1 : 0; 1667717a8e72SMengdong Lin } 1668717a8e72SMengdong Lin 166967d1c21eSGuneshwor Singh /* create the FE DAI link */ 1670ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg, 1671acfc7d46SMengdong Lin struct snd_soc_tplg_pcm *pcm) 1672acfc7d46SMengdong Lin { 1673acfc7d46SMengdong Lin struct snd_soc_dai_link *link; 167423b946ceSKuninori Morimoto struct snd_soc_dai_link_component *dlc; 1675acfc7d46SMengdong Lin int ret; 1676acfc7d46SMengdong Lin 16776a7c51b4SKuninori Morimoto /* link + cpu + codec + platform */ 16786a7c51b4SKuninori Morimoto link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL); 1679acfc7d46SMengdong Lin if (link == NULL) 1680acfc7d46SMengdong Lin return -ENOMEM; 1681acfc7d46SMengdong Lin 168223b946ceSKuninori Morimoto dlc = (struct snd_soc_dai_link_component *)(link + 1); 168323b946ceSKuninori Morimoto 168423b946ceSKuninori Morimoto link->cpus = &dlc[0]; 168523b946ceSKuninori Morimoto link->num_cpus = 1; 168623b946ceSKuninori Morimoto 16878ce1cbd6SJaroslav Kysela link->dobj.index = tplg->index; 16888ce1cbd6SJaroslav Kysela link->dobj.type = SND_SOC_DOBJ_DAI_LINK; 168931e92739SAmadeusz Sławiński if (tplg->ops) 169031e92739SAmadeusz Sławiński link->dobj.unload = tplg->ops->link_unload; 16918ce1cbd6SJaroslav Kysela 16928f27c4abSMengdong Lin if (strlen(pcm->pcm_name)) { 1693ff922622SAmadeusz Sławiński link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1694ff922622SAmadeusz Sławiński link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1695abc3caacSAmadeusz Sławiński if (!link->name || !link->stream_name) { 1696abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1697abc3caacSAmadeusz Sławiński goto err; 1698abc3caacSAmadeusz Sławiński } 16998f27c4abSMengdong Lin } 17005aebe7c7SPierre-Louis Bossart link->id = le32_to_cpu(pcm->pcm_id); 1701acfc7d46SMengdong Lin 1702abc3caacSAmadeusz Sławiński if (strlen(pcm->dai_name)) { 1703ff922622SAmadeusz Sławiński link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1704abc3caacSAmadeusz Sławiński if (!link->cpus->dai_name) { 1705abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1706abc3caacSAmadeusz Sławiński goto err; 1707abc3caacSAmadeusz Sławiński } 1708abc3caacSAmadeusz Sławiński } 17098f27c4abSMengdong Lin 17105a7bec81SKuninori Morimoto /* 17115a7bec81SKuninori Morimoto * Many topology are assuming link has Codec / Platform, and 17125a7bec81SKuninori Morimoto * these might be overwritten at soc_tplg_dai_link_load(). 17135a7bec81SKuninori Morimoto * Don't use &asoc_dummy_dlc here. 17145a7bec81SKuninori Morimoto */ 17155a7bec81SKuninori Morimoto link->codecs = &dlc[1]; /* Don't use &asoc_dummy_dlc here */ 171623b946ceSKuninori Morimoto link->codecs->name = "snd-soc-dummy"; 171723b946ceSKuninori Morimoto link->codecs->dai_name = "snd-soc-dummy-dai"; 17185a7bec81SKuninori Morimoto link->num_codecs = 1; 171967d1c21eSGuneshwor Singh 17205a7bec81SKuninori Morimoto link->platforms = &dlc[2]; /* Don't use &asoc_dummy_dlc here */ 17216a7c51b4SKuninori Morimoto link->platforms->name = "snd-soc-dummy"; 17226a7c51b4SKuninori Morimoto link->num_platforms = 1; 17236a7c51b4SKuninori Morimoto 172467d1c21eSGuneshwor Singh /* enable DPCM */ 172567d1c21eSGuneshwor Singh link->dynamic = 1; 1726c403dcd8SKuninori Morimoto link->ignore_pmdown_time = 1; 17275aebe7c7SPierre-Louis Bossart link->dpcm_playback = le32_to_cpu(pcm->playback); 17285aebe7c7SPierre-Louis Bossart link->dpcm_capture = le32_to_cpu(pcm->capture); 1729717a8e72SMengdong Lin if (pcm->flag_mask) 17305aebe7c7SPierre-Louis Bossart set_link_flags(link, 17315aebe7c7SPierre-Louis Bossart le32_to_cpu(pcm->flag_mask), 17325aebe7c7SPierre-Louis Bossart le32_to_cpu(pcm->flags)); 173367d1c21eSGuneshwor Singh 1734acfc7d46SMengdong Lin /* pass control to component driver for optional further init */ 1735c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, NULL); 1736acfc7d46SMengdong Lin if (ret < 0) { 1737e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: FE link loading failed\n"); 173876d27036SDragos Tarcatu goto err; 173976d27036SDragos Tarcatu } 174076d27036SDragos Tarcatu 1741ffaf886eSKuninori Morimoto ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1); 174276d27036SDragos Tarcatu if (ret < 0) { 1743e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: adding FE link failed\n"); 174476d27036SDragos Tarcatu goto err; 1745acfc7d46SMengdong Lin } 1746acfc7d46SMengdong Lin 1747acfc7d46SMengdong Lin list_add(&link->dobj.list, &tplg->comp->dobj_list); 1748acfc7d46SMengdong Lin 1749acfc7d46SMengdong Lin return 0; 175076d27036SDragos Tarcatu err: 175176d27036SDragos Tarcatu return ret; 1752acfc7d46SMengdong Lin } 1753acfc7d46SMengdong Lin 1754acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */ 175564527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg, 175664527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 175764527e8aSMengdong Lin { 1758acfc7d46SMengdong Lin int ret; 1759acfc7d46SMengdong Lin 1760acfc7d46SMengdong Lin ret = soc_tplg_dai_create(tplg, pcm); 1761acfc7d46SMengdong Lin if (ret < 0) 1762acfc7d46SMengdong Lin return ret; 1763acfc7d46SMengdong Lin 1764ab4bc5eeSMengdong Lin return soc_tplg_fe_link_create(tplg, pcm); 176564527e8aSMengdong Lin } 176664527e8aSMengdong Lin 176755726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */ 176855726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, 176955726dc9SMengdong Lin struct snd_soc_tplg_stream_caps_v4 *src) 177055726dc9SMengdong Lin { 17715aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); 177255726dc9SMengdong Lin memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 177355726dc9SMengdong Lin dest->formats = src->formats; 177455726dc9SMengdong Lin dest->rates = src->rates; 177555726dc9SMengdong Lin dest->rate_min = src->rate_min; 177655726dc9SMengdong Lin dest->rate_max = src->rate_max; 177755726dc9SMengdong Lin dest->channels_min = src->channels_min; 177855726dc9SMengdong Lin dest->channels_max = src->channels_max; 177955726dc9SMengdong Lin dest->periods_min = src->periods_min; 178055726dc9SMengdong Lin dest->periods_max = src->periods_max; 178155726dc9SMengdong Lin dest->period_size_min = src->period_size_min; 178255726dc9SMengdong Lin dest->period_size_max = src->period_size_max; 178355726dc9SMengdong Lin dest->buffer_size_min = src->buffer_size_min; 178455726dc9SMengdong Lin dest->buffer_size_max = src->buffer_size_max; 178555726dc9SMengdong Lin } 178655726dc9SMengdong Lin 178755726dc9SMengdong Lin /** 178855726dc9SMengdong Lin * pcm_new_ver - Create the new version of PCM from the old version. 178955726dc9SMengdong Lin * @tplg: topology context 179055726dc9SMengdong Lin * @src: older version of pcm as a source 179155726dc9SMengdong Lin * @pcm: latest version of pcm created from the source 179255726dc9SMengdong Lin * 1793ce1f2571SColin Ian King * Support from version 4. User should free the returned pcm manually. 179455726dc9SMengdong Lin */ 179555726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg, 179655726dc9SMengdong Lin struct snd_soc_tplg_pcm *src, 179755726dc9SMengdong Lin struct snd_soc_tplg_pcm **pcm) 179855726dc9SMengdong Lin { 179955726dc9SMengdong Lin struct snd_soc_tplg_pcm *dest; 180055726dc9SMengdong Lin struct snd_soc_tplg_pcm_v4 *src_v4; 180155726dc9SMengdong Lin int i; 180255726dc9SMengdong Lin 180355726dc9SMengdong Lin *pcm = NULL; 180455726dc9SMengdong Lin 18055aebe7c7SPierre-Louis Bossart if (le32_to_cpu(src->size) != sizeof(*src_v4)) { 180655726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid PCM size\n"); 180755726dc9SMengdong Lin return -EINVAL; 180855726dc9SMengdong Lin } 180955726dc9SMengdong Lin 181055726dc9SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of PCM\n"); 181155726dc9SMengdong Lin src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; 181255726dc9SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 181355726dc9SMengdong Lin if (!dest) 181455726dc9SMengdong Lin return -ENOMEM; 181555726dc9SMengdong Lin 18165aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ 181755726dc9SMengdong Lin memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 181855726dc9SMengdong Lin memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 181955726dc9SMengdong Lin dest->pcm_id = src_v4->pcm_id; 182055726dc9SMengdong Lin dest->dai_id = src_v4->dai_id; 182155726dc9SMengdong Lin dest->playback = src_v4->playback; 182255726dc9SMengdong Lin dest->capture = src_v4->capture; 182355726dc9SMengdong Lin dest->compress = src_v4->compress; 182455726dc9SMengdong Lin dest->num_streams = src_v4->num_streams; 18255aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(dest->num_streams); i++) 182655726dc9SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 182755726dc9SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 182855726dc9SMengdong Lin 182955726dc9SMengdong Lin for (i = 0; i < 2; i++) 183055726dc9SMengdong Lin stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); 183155726dc9SMengdong Lin 183255726dc9SMengdong Lin *pcm = dest; 183355726dc9SMengdong Lin return 0; 183455726dc9SMengdong Lin } 183555726dc9SMengdong Lin 183664527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, 18378a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 18388a978234SLiam Girdwood { 183955726dc9SMengdong Lin struct snd_soc_tplg_pcm *pcm, *_pcm; 18405aebe7c7SPierre-Louis Bossart int count; 18415aebe7c7SPierre-Louis Bossart int size; 1842fd340455SVinod Koul int i; 184355726dc9SMengdong Lin bool abi_match; 1844a3039aefSDragos Tarcatu int ret; 18458a978234SLiam Girdwood 18465aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 18475aebe7c7SPierre-Louis Bossart 184855726dc9SMengdong Lin /* check the element size and count */ 184955726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 18505aebe7c7SPierre-Louis Bossart size = le32_to_cpu(pcm->size); 18515aebe7c7SPierre-Louis Bossart if (size > sizeof(struct snd_soc_tplg_pcm) 18525aebe7c7SPierre-Louis Bossart || size < sizeof(struct snd_soc_tplg_pcm_v4)) { 185355726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", 18545aebe7c7SPierre-Louis Bossart size); 185555726dc9SMengdong Lin return -EINVAL; 185655726dc9SMengdong Lin } 185755726dc9SMengdong Lin 18588a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 18595aebe7c7SPierre-Louis Bossart size, count, 18605aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->payload_size), 18613ce57f22SAmadeusz Sławiński "PCM DAI")) 18628a978234SLiam Girdwood return -EINVAL; 18638a978234SLiam Girdwood 186464527e8aSMengdong Lin for (i = 0; i < count; i++) { 186555726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 18665aebe7c7SPierre-Louis Bossart size = le32_to_cpu(pcm->size); 186755726dc9SMengdong Lin 186855726dc9SMengdong Lin /* check ABI version by size, create a new version of pcm 186955726dc9SMengdong Lin * if abi not match. 187055726dc9SMengdong Lin */ 18715aebe7c7SPierre-Louis Bossart if (size == sizeof(*pcm)) { 187255726dc9SMengdong Lin abi_match = true; 187355726dc9SMengdong Lin _pcm = pcm; 187455726dc9SMengdong Lin } else { 187555726dc9SMengdong Lin abi_match = false; 1876b3677fc3SAmadeusz Sławiński ret = pcm_new_ver(tplg, pcm, &_pcm); 1877b3677fc3SAmadeusz Sławiński if (ret < 0) 1878b3677fc3SAmadeusz Sławiński return ret; 187906eb49f7SMengdong Lin } 188006eb49f7SMengdong Lin 188155726dc9SMengdong Lin /* create the FE DAIs and DAI links */ 1882a3039aefSDragos Tarcatu ret = soc_tplg_pcm_create(tplg, _pcm); 1883a3039aefSDragos Tarcatu if (ret < 0) { 1884a3039aefSDragos Tarcatu if (!abi_match) 1885a3039aefSDragos Tarcatu kfree(_pcm); 1886a3039aefSDragos Tarcatu return ret; 1887a3039aefSDragos Tarcatu } 188855726dc9SMengdong Lin 1889717a8e72SMengdong Lin /* offset by version-specific struct size and 1890717a8e72SMengdong Lin * real priv data size 1891717a8e72SMengdong Lin */ 18925aebe7c7SPierre-Louis Bossart tplg->pos += size + le32_to_cpu(_pcm->priv.size); 1893717a8e72SMengdong Lin 189455726dc9SMengdong Lin if (!abi_match) 189555726dc9SMengdong Lin kfree(_pcm); /* free the duplicated one */ 189664527e8aSMengdong Lin } 189764527e8aSMengdong Lin 18988a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); 18998a978234SLiam Girdwood 19008a978234SLiam Girdwood return 0; 19018a978234SLiam Girdwood } 19028a978234SLiam Girdwood 19030038be9aSMengdong Lin /** 1904593d9e52SMengdong Lin * set_link_hw_format - Set the HW audio format of the physical DAI link. 19058abab35fSCharles Keepax * @link: &snd_soc_dai_link which should be updated 1906593d9e52SMengdong Lin * @cfg: physical link configs. 1907593d9e52SMengdong Lin * 1908593d9e52SMengdong Lin * Topology context contains a list of supported HW formats (configs) and 1909593d9e52SMengdong Lin * a default format ID for the physical link. This function will use this 1910593d9e52SMengdong Lin * default ID to choose the HW format to set the link's DAI format for init. 1911593d9e52SMengdong Lin */ 1912593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link, 1913593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 1914593d9e52SMengdong Lin { 1915593d9e52SMengdong Lin struct snd_soc_tplg_hw_config *hw_config; 1916f026c123SPierre-Louis Bossart unsigned char bclk_provider, fsync_provider; 1917593d9e52SMengdong Lin unsigned char invert_bclk, invert_fsync; 1918593d9e52SMengdong Lin int i; 1919593d9e52SMengdong Lin 19205aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { 1921593d9e52SMengdong Lin hw_config = &cfg->hw_config[i]; 1922593d9e52SMengdong Lin if (hw_config->id != cfg->default_hw_config_id) 1923593d9e52SMengdong Lin continue; 1924593d9e52SMengdong Lin 19255aebe7c7SPierre-Louis Bossart link->dai_fmt = le32_to_cpu(hw_config->fmt) & 19265aebe7c7SPierre-Louis Bossart SND_SOC_DAIFMT_FORMAT_MASK; 1927593d9e52SMengdong Lin 1928933e1c4aSKirill Marinushkin /* clock gating */ 1929fbeabd09SKirill Marinushkin switch (hw_config->clock_gated) { 1930fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_GATED: 1931933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_GATED; 1932fbeabd09SKirill Marinushkin break; 1933fbeabd09SKirill Marinushkin 1934fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_CONT: 1935933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_CONT; 1936fbeabd09SKirill Marinushkin break; 1937fbeabd09SKirill Marinushkin 1938fbeabd09SKirill Marinushkin default: 1939fbeabd09SKirill Marinushkin /* ignore the value */ 1940fbeabd09SKirill Marinushkin break; 1941fbeabd09SKirill Marinushkin } 1942933e1c4aSKirill Marinushkin 1943593d9e52SMengdong Lin /* clock signal polarity */ 1944593d9e52SMengdong Lin invert_bclk = hw_config->invert_bclk; 1945593d9e52SMengdong Lin invert_fsync = hw_config->invert_fsync; 1946593d9e52SMengdong Lin if (!invert_bclk && !invert_fsync) 1947593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; 1948593d9e52SMengdong Lin else if (!invert_bclk && invert_fsync) 1949593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; 1950593d9e52SMengdong Lin else if (invert_bclk && !invert_fsync) 1951593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; 1952593d9e52SMengdong Lin else 1953593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; 1954593d9e52SMengdong Lin 1955593d9e52SMengdong Lin /* clock masters */ 1956f026c123SPierre-Louis Bossart bclk_provider = (hw_config->bclk_provider == 1957f026c123SPierre-Louis Bossart SND_SOC_TPLG_BCLK_CP); 1958f026c123SPierre-Louis Bossart fsync_provider = (hw_config->fsync_provider == 1959f026c123SPierre-Louis Bossart SND_SOC_TPLG_FSYNC_CP); 1960f026c123SPierre-Louis Bossart if (bclk_provider && fsync_provider) 1961f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 1962f026c123SPierre-Louis Bossart else if (!bclk_provider && fsync_provider) 1963f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP; 1964f026c123SPierre-Louis Bossart else if (bclk_provider && !fsync_provider) 1965f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC; 1966593d9e52SMengdong Lin else 1967f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 1968593d9e52SMengdong Lin } 1969593d9e52SMengdong Lin } 1970593d9e52SMengdong Lin 1971593d9e52SMengdong Lin /** 1972593d9e52SMengdong Lin * link_new_ver - Create a new physical link config from the old 1973593d9e52SMengdong Lin * version of source. 19748abab35fSCharles Keepax * @tplg: topology context 1975593d9e52SMengdong Lin * @src: old version of phyical link config as a source 1976593d9e52SMengdong Lin * @link: latest version of physical link config created from the source 1977593d9e52SMengdong Lin * 1978ce1f2571SColin Ian King * Support from version 4. User need free the returned link config manually. 1979593d9e52SMengdong Lin */ 1980593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg, 1981593d9e52SMengdong Lin struct snd_soc_tplg_link_config *src, 1982593d9e52SMengdong Lin struct snd_soc_tplg_link_config **link) 1983593d9e52SMengdong Lin { 1984593d9e52SMengdong Lin struct snd_soc_tplg_link_config *dest; 1985593d9e52SMengdong Lin struct snd_soc_tplg_link_config_v4 *src_v4; 1986593d9e52SMengdong Lin int i; 1987593d9e52SMengdong Lin 1988593d9e52SMengdong Lin *link = NULL; 1989593d9e52SMengdong Lin 19905aebe7c7SPierre-Louis Bossart if (le32_to_cpu(src->size) != 19915aebe7c7SPierre-Louis Bossart sizeof(struct snd_soc_tplg_link_config_v4)) { 1992593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); 1993593d9e52SMengdong Lin return -EINVAL; 1994593d9e52SMengdong Lin } 1995593d9e52SMengdong Lin 1996593d9e52SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); 1997593d9e52SMengdong Lin 1998593d9e52SMengdong Lin src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; 1999593d9e52SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 2000593d9e52SMengdong Lin if (!dest) 2001593d9e52SMengdong Lin return -ENOMEM; 2002593d9e52SMengdong Lin 20035aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); 2004593d9e52SMengdong Lin dest->id = src_v4->id; 2005593d9e52SMengdong Lin dest->num_streams = src_v4->num_streams; 20065aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(dest->num_streams); i++) 2007593d9e52SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 2008593d9e52SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 2009593d9e52SMengdong Lin 2010593d9e52SMengdong Lin *link = dest; 2011593d9e52SMengdong Lin return 0; 2012593d9e52SMengdong Lin } 2013593d9e52SMengdong Lin 2014d6f31e0eSKuninori Morimoto /** 2015d6f31e0eSKuninori Morimoto * snd_soc_find_dai_link - Find a DAI link 2016d6f31e0eSKuninori Morimoto * 2017d6f31e0eSKuninori Morimoto * @card: soc card 2018d6f31e0eSKuninori Morimoto * @id: DAI link ID to match 2019d6f31e0eSKuninori Morimoto * @name: DAI link name to match, optional 2020d6f31e0eSKuninori Morimoto * @stream_name: DAI link stream name to match, optional 2021d6f31e0eSKuninori Morimoto * 2022d6f31e0eSKuninori Morimoto * This function will search all existing DAI links of the soc card to 2023d6f31e0eSKuninori Morimoto * find the link of the same ID. Since DAI links may not have their 2024d6f31e0eSKuninori Morimoto * unique ID, so name and stream name should also match if being 2025d6f31e0eSKuninori Morimoto * specified. 2026d6f31e0eSKuninori Morimoto * 2027d6f31e0eSKuninori Morimoto * Return: pointer of DAI link, or NULL if not found. 2028d6f31e0eSKuninori Morimoto */ 2029d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, 2030d6f31e0eSKuninori Morimoto int id, const char *name, 2031d6f31e0eSKuninori Morimoto const char *stream_name) 2032d6f31e0eSKuninori Morimoto { 2033d6f31e0eSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 2034d6f31e0eSKuninori Morimoto 2035d6f31e0eSKuninori Morimoto for_each_card_rtds(card, rtd) { 2036b81e8efaSKuninori Morimoto struct snd_soc_dai_link *link = rtd->dai_link; 2037d6f31e0eSKuninori Morimoto 2038d6f31e0eSKuninori Morimoto if (link->id != id) 2039d6f31e0eSKuninori Morimoto continue; 2040d6f31e0eSKuninori Morimoto 2041d6f31e0eSKuninori Morimoto if (name && (!link->name || strcmp(name, link->name))) 2042d6f31e0eSKuninori Morimoto continue; 2043d6f31e0eSKuninori Morimoto 2044d6f31e0eSKuninori Morimoto if (stream_name && (!link->stream_name 2045d6f31e0eSKuninori Morimoto || strcmp(stream_name, link->stream_name))) 2046d6f31e0eSKuninori Morimoto continue; 2047d6f31e0eSKuninori Morimoto 2048d6f31e0eSKuninori Morimoto return link; 2049d6f31e0eSKuninori Morimoto } 2050d6f31e0eSKuninori Morimoto 2051d6f31e0eSKuninori Morimoto return NULL; 2052d6f31e0eSKuninori Morimoto } 2053d6f31e0eSKuninori Morimoto 2054593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */ 2055593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg, 2056593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 2057593d9e52SMengdong Lin { 2058593d9e52SMengdong Lin struct snd_soc_dai_link *link; 2059593d9e52SMengdong Lin const char *name, *stream_name; 2060dbab1cb8SMengdong Lin size_t len; 2061593d9e52SMengdong Lin int ret; 2062593d9e52SMengdong Lin 2063dbab1cb8SMengdong Lin len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2064dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2065dbab1cb8SMengdong Lin return -EINVAL; 2066dbab1cb8SMengdong Lin else if (len) 2067dbab1cb8SMengdong Lin name = cfg->name; 2068dbab1cb8SMengdong Lin else 2069dbab1cb8SMengdong Lin name = NULL; 2070dbab1cb8SMengdong Lin 2071dbab1cb8SMengdong Lin len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2072dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2073dbab1cb8SMengdong Lin return -EINVAL; 2074dbab1cb8SMengdong Lin else if (len) 2075dbab1cb8SMengdong Lin stream_name = cfg->stream_name; 2076dbab1cb8SMengdong Lin else 2077dbab1cb8SMengdong Lin stream_name = NULL; 2078593d9e52SMengdong Lin 20795aebe7c7SPierre-Louis Bossart link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), 2080593d9e52SMengdong Lin name, stream_name); 2081593d9e52SMengdong Lin if (!link) { 2082593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", 2083593d9e52SMengdong Lin name, cfg->id); 2084593d9e52SMengdong Lin return -EINVAL; 2085593d9e52SMengdong Lin } 2086593d9e52SMengdong Lin 2087593d9e52SMengdong Lin /* hw format */ 2088593d9e52SMengdong Lin if (cfg->num_hw_configs) 2089593d9e52SMengdong Lin set_link_hw_format(link, cfg); 2090593d9e52SMengdong Lin 2091593d9e52SMengdong Lin /* flags */ 2092593d9e52SMengdong Lin if (cfg->flag_mask) 20935aebe7c7SPierre-Louis Bossart set_link_flags(link, 20945aebe7c7SPierre-Louis Bossart le32_to_cpu(cfg->flag_mask), 20955aebe7c7SPierre-Louis Bossart le32_to_cpu(cfg->flags)); 2096593d9e52SMengdong Lin 2097593d9e52SMengdong Lin /* pass control to component driver for optional further init */ 2098c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, cfg); 2099593d9e52SMengdong Lin if (ret < 0) { 2100593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link loading failed\n"); 2101593d9e52SMengdong Lin return ret; 2102593d9e52SMengdong Lin } 2103593d9e52SMengdong Lin 2104adfebb51SBard liao /* for unloading it in snd_soc_tplg_component_remove */ 2105adfebb51SBard liao link->dobj.index = tplg->index; 2106adfebb51SBard liao link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; 210731e92739SAmadeusz Sławiński if (tplg->ops) 210831e92739SAmadeusz Sławiński link->dobj.unload = tplg->ops->link_unload; 2109adfebb51SBard liao list_add(&link->dobj.list, &tplg->comp->dobj_list); 2110adfebb51SBard liao 2111593d9e52SMengdong Lin return 0; 2112593d9e52SMengdong Lin } 2113593d9e52SMengdong Lin 2114593d9e52SMengdong Lin 2115593d9e52SMengdong Lin /* Load physical link config elements from the topology context */ 2116593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg, 2117593d9e52SMengdong Lin struct snd_soc_tplg_hdr *hdr) 2118593d9e52SMengdong Lin { 2119593d9e52SMengdong Lin struct snd_soc_tplg_link_config *link, *_link; 21205aebe7c7SPierre-Louis Bossart int count; 21215aebe7c7SPierre-Louis Bossart int size; 2122593d9e52SMengdong Lin int i, ret; 2123593d9e52SMengdong Lin bool abi_match; 2124593d9e52SMengdong Lin 21255aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 21265aebe7c7SPierre-Louis Bossart 2127593d9e52SMengdong Lin /* check the element size and count */ 2128593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 21295aebe7c7SPierre-Louis Bossart size = le32_to_cpu(link->size); 21305aebe7c7SPierre-Louis Bossart if (size > sizeof(struct snd_soc_tplg_link_config) 21315aebe7c7SPierre-Louis Bossart || size < sizeof(struct snd_soc_tplg_link_config_v4)) { 2132593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", 21335aebe7c7SPierre-Louis Bossart size); 2134593d9e52SMengdong Lin return -EINVAL; 2135593d9e52SMengdong Lin } 2136593d9e52SMengdong Lin 21373ce57f22SAmadeusz Sławiński if (soc_tplg_check_elem_count(tplg, size, count, 21385aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->payload_size), 21393ce57f22SAmadeusz Sławiński "physical link config")) 2140593d9e52SMengdong Lin return -EINVAL; 2141593d9e52SMengdong Lin 2142593d9e52SMengdong Lin /* config physical DAI links */ 2143593d9e52SMengdong Lin for (i = 0; i < count; i++) { 2144593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 21455aebe7c7SPierre-Louis Bossart size = le32_to_cpu(link->size); 21465aebe7c7SPierre-Louis Bossart if (size == sizeof(*link)) { 2147593d9e52SMengdong Lin abi_match = true; 2148593d9e52SMengdong Lin _link = link; 2149593d9e52SMengdong Lin } else { 2150593d9e52SMengdong Lin abi_match = false; 2151593d9e52SMengdong Lin ret = link_new_ver(tplg, link, &_link); 2152593d9e52SMengdong Lin if (ret < 0) 2153593d9e52SMengdong Lin return ret; 2154593d9e52SMengdong Lin } 2155593d9e52SMengdong Lin 2156593d9e52SMengdong Lin ret = soc_tplg_link_config(tplg, _link); 21572b2d5c4dSDragos Tarcatu if (ret < 0) { 21582b2d5c4dSDragos Tarcatu if (!abi_match) 21592b2d5c4dSDragos Tarcatu kfree(_link); 2160593d9e52SMengdong Lin return ret; 21612b2d5c4dSDragos Tarcatu } 2162593d9e52SMengdong Lin 2163593d9e52SMengdong Lin /* offset by version-specific struct size and 2164593d9e52SMengdong Lin * real priv data size 2165593d9e52SMengdong Lin */ 21665aebe7c7SPierre-Louis Bossart tplg->pos += size + le32_to_cpu(_link->priv.size); 2167593d9e52SMengdong Lin 2168593d9e52SMengdong Lin if (!abi_match) 2169593d9e52SMengdong Lin kfree(_link); /* free the duplicated one */ 2170593d9e52SMengdong Lin } 2171593d9e52SMengdong Lin 2172593d9e52SMengdong Lin return 0; 2173593d9e52SMengdong Lin } 2174593d9e52SMengdong Lin 2175593d9e52SMengdong Lin /** 21769aa3f034SMengdong Lin * soc_tplg_dai_config - Find and configure an existing physical DAI. 21770038be9aSMengdong Lin * @tplg: topology context 21789aa3f034SMengdong Lin * @d: physical DAI configs. 21790038be9aSMengdong Lin * 21809aa3f034SMengdong Lin * The physical dai should already be registered by the platform driver. 21819aa3f034SMengdong Lin * The platform driver should specify the DAI name and ID for matching. 21820038be9aSMengdong Lin */ 21839aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg, 21849aa3f034SMengdong Lin struct snd_soc_tplg_dai *d) 21850038be9aSMengdong Lin { 21865aebe7c7SPierre-Louis Bossart struct snd_soc_dai_link_component dai_component; 21870038be9aSMengdong Lin struct snd_soc_dai *dai; 21880038be9aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 21890038be9aSMengdong Lin struct snd_soc_pcm_stream *stream; 21900038be9aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 21910038be9aSMengdong Lin int ret; 21920038be9aSMengdong Lin 21935aebe7c7SPierre-Louis Bossart memset(&dai_component, 0, sizeof(dai_component)); 21945aebe7c7SPierre-Louis Bossart 21959aa3f034SMengdong Lin dai_component.dai_name = d->dai_name; 21960038be9aSMengdong Lin dai = snd_soc_find_dai(&dai_component); 21970038be9aSMengdong Lin if (!dai) { 21989aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", 21999aa3f034SMengdong Lin d->dai_name); 22000038be9aSMengdong Lin return -EINVAL; 22010038be9aSMengdong Lin } 22020038be9aSMengdong Lin 22035aebe7c7SPierre-Louis Bossart if (le32_to_cpu(d->dai_id) != dai->id) { 22049aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", 22059aa3f034SMengdong Lin d->dai_name); 22060038be9aSMengdong Lin return -EINVAL; 22070038be9aSMengdong Lin } 22080038be9aSMengdong Lin 22090038be9aSMengdong Lin dai_drv = dai->driver; 22100038be9aSMengdong Lin if (!dai_drv) 22110038be9aSMengdong Lin return -EINVAL; 22120038be9aSMengdong Lin 22139aa3f034SMengdong Lin if (d->playback) { 22140038be9aSMengdong Lin stream = &dai_drv->playback; 22159aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 2216ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 2217abc3caacSAmadeusz Sławiński if (ret < 0) 2218abc3caacSAmadeusz Sławiński goto err; 22190038be9aSMengdong Lin } 22200038be9aSMengdong Lin 22219aa3f034SMengdong Lin if (d->capture) { 22220038be9aSMengdong Lin stream = &dai_drv->capture; 22239aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 2224ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 2225abc3caacSAmadeusz Sławiński if (ret < 0) 2226abc3caacSAmadeusz Sławiński goto err; 22270038be9aSMengdong Lin } 22280038be9aSMengdong Lin 22299aa3f034SMengdong Lin if (d->flag_mask) 22305aebe7c7SPierre-Louis Bossart set_dai_flags(dai_drv, 22315aebe7c7SPierre-Louis Bossart le32_to_cpu(d->flag_mask), 22325aebe7c7SPierre-Louis Bossart le32_to_cpu(d->flags)); 22330038be9aSMengdong Lin 22340038be9aSMengdong Lin /* pass control to component driver for optional further init */ 2235c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); 22360038be9aSMengdong Lin if (ret < 0) { 2237e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 2238abc3caacSAmadeusz Sławiński goto err; 22390038be9aSMengdong Lin } 22400038be9aSMengdong Lin 22410038be9aSMengdong Lin return 0; 2242abc3caacSAmadeusz Sławiński 2243abc3caacSAmadeusz Sławiński err: 2244abc3caacSAmadeusz Sławiński return ret; 22450038be9aSMengdong Lin } 22460038be9aSMengdong Lin 22479aa3f034SMengdong Lin /* load physical DAI elements */ 22489aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, 22490038be9aSMengdong Lin struct snd_soc_tplg_hdr *hdr) 22500038be9aSMengdong Lin { 22515aebe7c7SPierre-Louis Bossart int count; 225265a4cfddSKuninori Morimoto int i; 22530038be9aSMengdong Lin 22545aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 22555aebe7c7SPierre-Louis Bossart 22560038be9aSMengdong Lin /* config the existing BE DAIs */ 22570038be9aSMengdong Lin for (i = 0; i < count; i++) { 225865a4cfddSKuninori Morimoto struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos; 225965a4cfddSKuninori Morimoto int ret; 226065a4cfddSKuninori Morimoto 22615aebe7c7SPierre-Louis Bossart if (le32_to_cpu(dai->size) != sizeof(*dai)) { 22629aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); 22630038be9aSMengdong Lin return -EINVAL; 22640038be9aSMengdong Lin } 22650038be9aSMengdong Lin 2266dd8e871dSAmadeusz Sławiński ret = soc_tplg_dai_config(tplg, dai); 2267dd8e871dSAmadeusz Sławiński if (ret < 0) { 2268dd8e871dSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to configure DAI\n"); 2269dd8e871dSAmadeusz Sławiński return ret; 2270dd8e871dSAmadeusz Sławiński } 2271dd8e871dSAmadeusz Sławiński 22725aebe7c7SPierre-Louis Bossart tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); 22730038be9aSMengdong Lin } 22740038be9aSMengdong Lin 22750038be9aSMengdong Lin dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); 22760038be9aSMengdong Lin return 0; 22770038be9aSMengdong Lin } 22780038be9aSMengdong Lin 2279583958faSMengdong Lin /** 2280583958faSMengdong Lin * manifest_new_ver - Create a new version of manifest from the old version 2281583958faSMengdong Lin * of source. 22828abab35fSCharles Keepax * @tplg: topology context 2283583958faSMengdong Lin * @src: old version of manifest as a source 2284583958faSMengdong Lin * @manifest: latest version of manifest created from the source 2285583958faSMengdong Lin * 2286ce1f2571SColin Ian King * Support from version 4. Users need free the returned manifest manually. 2287583958faSMengdong Lin */ 2288583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg, 2289583958faSMengdong Lin struct snd_soc_tplg_manifest *src, 2290583958faSMengdong Lin struct snd_soc_tplg_manifest **manifest) 2291583958faSMengdong Lin { 2292583958faSMengdong Lin struct snd_soc_tplg_manifest *dest; 2293583958faSMengdong Lin struct snd_soc_tplg_manifest_v4 *src_v4; 22945aebe7c7SPierre-Louis Bossart int size; 2295583958faSMengdong Lin 2296583958faSMengdong Lin *manifest = NULL; 2297583958faSMengdong Lin 22985aebe7c7SPierre-Louis Bossart size = le32_to_cpu(src->size); 22995aebe7c7SPierre-Louis Bossart if (size != sizeof(*src_v4)) { 2300ac9391daSGuenter Roeck dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", 23015aebe7c7SPierre-Louis Bossart size); 23025aebe7c7SPierre-Louis Bossart if (size) 2303583958faSMengdong Lin return -EINVAL; 23045aebe7c7SPierre-Louis Bossart src->size = cpu_to_le32(sizeof(*src_v4)); 2305583958faSMengdong Lin } 2306583958faSMengdong Lin 2307583958faSMengdong Lin dev_warn(tplg->dev, "ASoC: old version of manifest\n"); 2308583958faSMengdong Lin 2309583958faSMengdong Lin src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; 23105aebe7c7SPierre-Louis Bossart dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size), 23115aebe7c7SPierre-Louis Bossart GFP_KERNEL); 2312583958faSMengdong Lin if (!dest) 2313583958faSMengdong Lin return -ENOMEM; 2314583958faSMengdong Lin 23155aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ 2316583958faSMengdong Lin dest->control_elems = src_v4->control_elems; 2317583958faSMengdong Lin dest->widget_elems = src_v4->widget_elems; 2318583958faSMengdong Lin dest->graph_elems = src_v4->graph_elems; 2319583958faSMengdong Lin dest->pcm_elems = src_v4->pcm_elems; 2320583958faSMengdong Lin dest->dai_link_elems = src_v4->dai_link_elems; 2321583958faSMengdong Lin dest->priv.size = src_v4->priv.size; 2322583958faSMengdong Lin if (dest->priv.size) 2323583958faSMengdong Lin memcpy(dest->priv.data, src_v4->priv.data, 23245aebe7c7SPierre-Louis Bossart le32_to_cpu(src_v4->priv.size)); 2325583958faSMengdong Lin 2326583958faSMengdong Lin *manifest = dest; 2327583958faSMengdong Lin return 0; 2328583958faSMengdong Lin } 23290038be9aSMengdong Lin 23308a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg, 23318a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 23328a978234SLiam Girdwood { 2333583958faSMengdong Lin struct snd_soc_tplg_manifest *manifest, *_manifest; 2334583958faSMengdong Lin bool abi_match; 2335242c46c0SDragos Tarcatu int ret = 0; 23368a978234SLiam Girdwood 23378a978234SLiam Girdwood manifest = (struct snd_soc_tplg_manifest *)tplg->pos; 2338583958faSMengdong Lin 2339583958faSMengdong Lin /* check ABI version by size, create a new manifest if abi not match */ 23405aebe7c7SPierre-Louis Bossart if (le32_to_cpu(manifest->size) == sizeof(*manifest)) { 2341583958faSMengdong Lin abi_match = true; 2342583958faSMengdong Lin _manifest = manifest; 2343583958faSMengdong Lin } else { 2344583958faSMengdong Lin abi_match = false; 234586e2d14bSCezary Rojewski 2346242c46c0SDragos Tarcatu ret = manifest_new_ver(tplg, manifest, &_manifest); 2347242c46c0SDragos Tarcatu if (ret < 0) 2348242c46c0SDragos Tarcatu return ret; 234906eb49f7SMengdong Lin } 235006eb49f7SMengdong Lin 2351583958faSMengdong Lin /* pass control to component driver for optional further init */ 2352c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->manifest) 2353242c46c0SDragos Tarcatu ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest); 23548a978234SLiam Girdwood 2355583958faSMengdong Lin if (!abi_match) /* free the duplicated one */ 2356583958faSMengdong Lin kfree(_manifest); 2357583958faSMengdong Lin 2358242c46c0SDragos Tarcatu return ret; 23598a978234SLiam Girdwood } 23608a978234SLiam Girdwood 23618a978234SLiam Girdwood /* validate header magic, size and type */ 236223e591dcSAmadeusz Sławiński static int soc_tplg_valid_header(struct soc_tplg *tplg, 23638a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 23648a978234SLiam Girdwood { 23655aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { 236606eb49f7SMengdong Lin dev_err(tplg->dev, 236706eb49f7SMengdong Lin "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", 23685aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 236906eb49f7SMengdong Lin tplg->fw->size); 237006eb49f7SMengdong Lin return -EINVAL; 237106eb49f7SMengdong Lin } 237206eb49f7SMengdong Lin 2373c5d184c9SAmadeusz Sławiński if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) { 237486e2d14bSCezary Rojewski dev_err(tplg->dev, 237586e2d14bSCezary Rojewski "ASoC: invalid header of type %d at offset %ld payload_size %d\n", 237686e2d14bSCezary Rojewski le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 237786e2d14bSCezary Rojewski hdr->payload_size); 237886e2d14bSCezary Rojewski return -EINVAL; 237986e2d14bSCezary Rojewski } 238086e2d14bSCezary Rojewski 23818a978234SLiam Girdwood /* big endian firmware objects not supported atm */ 238226d87881SAmadeusz Sławiński if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) { 23838a978234SLiam Girdwood dev_err(tplg->dev, 23848a978234SLiam Girdwood "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", 23858a978234SLiam Girdwood tplg->pass, hdr->magic, 23868a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 23878a978234SLiam Girdwood return -EINVAL; 23888a978234SLiam Girdwood } 23898a978234SLiam Girdwood 23905aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { 23918a978234SLiam Girdwood dev_err(tplg->dev, 23928a978234SLiam Girdwood "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", 23938a978234SLiam Girdwood tplg->pass, hdr->magic, 23948a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 23958a978234SLiam Girdwood return -EINVAL; 23968a978234SLiam Girdwood } 23978a978234SLiam Girdwood 2398288b8da7SMengdong Lin /* Support ABI from version 4 */ 23995aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || 24005aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { 24018a978234SLiam Girdwood dev_err(tplg->dev, 24028a978234SLiam Girdwood "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", 24038a978234SLiam Girdwood tplg->pass, hdr->abi, 24048a978234SLiam Girdwood SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), 24058a978234SLiam Girdwood tplg->fw->size); 24068a978234SLiam Girdwood return -EINVAL; 24078a978234SLiam Girdwood } 24088a978234SLiam Girdwood 24098a978234SLiam Girdwood if (hdr->payload_size == 0) { 24108a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", 24118a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg)); 24128a978234SLiam Girdwood return -EINVAL; 24138a978234SLiam Girdwood } 24148a978234SLiam Girdwood 2415d9b07b79SAmadeusz Sławiński return 0; 24168a978234SLiam Girdwood } 24178a978234SLiam Girdwood 24188a978234SLiam Girdwood /* check header type and call appropriate handler */ 24198a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg, 24208a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 24218a978234SLiam Girdwood { 242282ed7418SKeyon Jie int (*elem_load)(struct soc_tplg *tplg, 242382ed7418SKeyon Jie struct snd_soc_tplg_hdr *hdr); 242482ed7418SKeyon Jie unsigned int hdr_pass; 242582ed7418SKeyon Jie 24268a978234SLiam Girdwood tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); 24278a978234SLiam Girdwood 24285aebe7c7SPierre-Louis Bossart tplg->index = le32_to_cpu(hdr->index); 24298a978234SLiam Girdwood 24305aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(hdr->type)) { 24318a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MIXER: 24328a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_ENUM: 24338a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_BYTES: 24345e2cd47aSAmadeusz Sławiński hdr_pass = SOC_TPLG_PASS_CONTROL; 243582ed7418SKeyon Jie elem_load = soc_tplg_kcontrol_elems_load; 243682ed7418SKeyon Jie break; 24378a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_GRAPH: 243882ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_GRAPH; 243982ed7418SKeyon Jie elem_load = soc_tplg_dapm_graph_elems_load; 244082ed7418SKeyon Jie break; 24418a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_WIDGET: 244282ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_WIDGET; 244382ed7418SKeyon Jie elem_load = soc_tplg_dapm_widget_elems_load; 244482ed7418SKeyon Jie break; 24458a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_PCM: 244682ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_PCM_DAI; 244782ed7418SKeyon Jie elem_load = soc_tplg_pcm_elems_load; 244882ed7418SKeyon Jie break; 24493fbf7935SMengdong Lin case SND_SOC_TPLG_TYPE_DAI: 245082ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_BE_DAI; 245182ed7418SKeyon Jie elem_load = soc_tplg_dai_elems_load; 245282ed7418SKeyon Jie break; 2453593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_DAI_LINK: 2454593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_BACKEND_LINK: 2455593d9e52SMengdong Lin /* physical link configurations */ 245682ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_LINK; 245782ed7418SKeyon Jie elem_load = soc_tplg_link_elems_load; 245882ed7418SKeyon Jie break; 24598a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MANIFEST: 246082ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_MANIFEST; 246182ed7418SKeyon Jie elem_load = soc_tplg_manifest_load; 246282ed7418SKeyon Jie break; 24638a978234SLiam Girdwood default: 24648a978234SLiam Girdwood /* bespoke vendor data object */ 246582ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_VENDOR; 246682ed7418SKeyon Jie elem_load = soc_tplg_vendor_load; 246782ed7418SKeyon Jie break; 246882ed7418SKeyon Jie } 246982ed7418SKeyon Jie 247082ed7418SKeyon Jie if (tplg->pass == hdr_pass) { 247182ed7418SKeyon Jie dev_dbg(tplg->dev, 247282ed7418SKeyon Jie "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", 247382ed7418SKeyon Jie hdr->payload_size, hdr->type, hdr->version, 247482ed7418SKeyon Jie hdr->vendor_type, tplg->pass); 247582ed7418SKeyon Jie return elem_load(tplg, hdr); 24768a978234SLiam Girdwood } 24778a978234SLiam Girdwood 24788a978234SLiam Girdwood return 0; 24798a978234SLiam Girdwood } 24808a978234SLiam Girdwood 24818a978234SLiam Girdwood /* process the topology file headers */ 24828a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg) 24838a978234SLiam Girdwood { 24848a978234SLiam Girdwood int ret; 24858a978234SLiam Girdwood 24868a978234SLiam Girdwood /* process the header types from start to end */ 2487395f8fd6SAmadeusz Sławiński for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) { 2488f79e4b2aSKuninori Morimoto struct snd_soc_tplg_hdr *hdr; 24898a978234SLiam Girdwood 24908a978234SLiam Girdwood tplg->hdr_pos = tplg->fw->data; 24918a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 24928a978234SLiam Girdwood 24938a978234SLiam Girdwood while (!soc_tplg_is_eof(tplg)) { 24948a978234SLiam Girdwood 24958a978234SLiam Girdwood /* make sure header is valid before loading */ 249623e591dcSAmadeusz Sławiński ret = soc_tplg_valid_header(tplg, hdr); 24978bf9475fSPierre-Louis Bossart if (ret < 0) { 24988bf9475fSPierre-Louis Bossart dev_err(tplg->dev, 24998bf9475fSPierre-Louis Bossart "ASoC: topology: invalid header: %d\n", ret); 25008a978234SLiam Girdwood return ret; 25018bf9475fSPierre-Louis Bossart } 25028a978234SLiam Girdwood 25038a978234SLiam Girdwood /* load the header object */ 25048a978234SLiam Girdwood ret = soc_tplg_load_header(tplg, hdr); 25058bf9475fSPierre-Louis Bossart if (ret < 0) { 25068bf9475fSPierre-Louis Bossart dev_err(tplg->dev, 25078bf9475fSPierre-Louis Bossart "ASoC: topology: could not load header: %d\n", ret); 25088a978234SLiam Girdwood return ret; 25098bf9475fSPierre-Louis Bossart } 25108a978234SLiam Girdwood 25118a978234SLiam Girdwood /* goto next header */ 25125aebe7c7SPierre-Louis Bossart tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + 25138a978234SLiam Girdwood sizeof(struct snd_soc_tplg_hdr); 25148a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 25158a978234SLiam Girdwood } 25168a978234SLiam Girdwood 25178a978234SLiam Girdwood } 25188a978234SLiam Girdwood 25198a978234SLiam Girdwood /* signal DAPM we are complete */ 25208a978234SLiam Girdwood ret = soc_tplg_dapm_complete(tplg); 25218a978234SLiam Girdwood if (ret < 0) 25228a978234SLiam Girdwood dev_err(tplg->dev, 25238a978234SLiam Girdwood "ASoC: failed to initialise DAPM from Firmware\n"); 25248a978234SLiam Girdwood 25258a978234SLiam Girdwood return ret; 25268a978234SLiam Girdwood } 25278a978234SLiam Girdwood 25288a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg) 25298a978234SLiam Girdwood { 25308a978234SLiam Girdwood int ret; 25318a978234SLiam Girdwood 25328a978234SLiam Girdwood ret = soc_tplg_process_headers(tplg); 25338a978234SLiam Girdwood if (ret == 0) 2534415717e1SRanjani Sridharan return soc_tplg_complete(tplg); 25358a978234SLiam Girdwood 25368a978234SLiam Girdwood return ret; 25378a978234SLiam Girdwood } 25388a978234SLiam Girdwood 25398a978234SLiam Girdwood /* load audio component topology from "firmware" file */ 25408a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp, 2541a5b8f71cSAmadeusz Sławiński struct snd_soc_tplg_ops *ops, const struct firmware *fw) 25428a978234SLiam Girdwood { 25438a978234SLiam Girdwood struct soc_tplg tplg; 2544304017d3SBard liao int ret; 25458a978234SLiam Girdwood 2546d40ab86fSAmadeusz Sławiński /* 2547d40ab86fSAmadeusz Sławiński * check if we have sane parameters: 2548d40ab86fSAmadeusz Sławiński * comp - needs to exist to keep and reference data while parsing 2549d40ab86fSAmadeusz Sławiński * comp->card - used for setting card related parameters 2550f714fbc1SAmadeusz Sławiński * comp->card->dev - used for resource management and prints 2551d40ab86fSAmadeusz Sławiński * fw - we need it, as it is the very thing we parse 2552d40ab86fSAmadeusz Sławiński */ 2553f714fbc1SAmadeusz Sławiński if (!comp || !comp->card || !comp->card->dev || !fw) 2554c42464a4SAmadeusz Sławiński return -EINVAL; 2555c42464a4SAmadeusz Sławiński 25568a978234SLiam Girdwood /* setup parsing context */ 25578a978234SLiam Girdwood memset(&tplg, 0, sizeof(tplg)); 25588a978234SLiam Girdwood tplg.fw = fw; 2559f714fbc1SAmadeusz Sławiński tplg.dev = comp->card->dev; 25608a978234SLiam Girdwood tplg.comp = comp; 25619c88a983SAmadeusz Sławiński if (ops) { 25628a978234SLiam Girdwood tplg.ops = ops; 25638a978234SLiam Girdwood tplg.io_ops = ops->io_ops; 25648a978234SLiam Girdwood tplg.io_ops_count = ops->io_ops_count; 25651a3232d2SMengdong Lin tplg.bytes_ext_ops = ops->bytes_ext_ops; 25661a3232d2SMengdong Lin tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; 25679c88a983SAmadeusz Sławiński } 25688a978234SLiam Girdwood 2569304017d3SBard liao ret = soc_tplg_load(&tplg); 2570304017d3SBard liao /* free the created components if fail to load topology */ 2571304017d3SBard liao if (ret) 2572a5b8f71cSAmadeusz Sławiński snd_soc_tplg_component_remove(comp); 2573304017d3SBard liao 2574304017d3SBard liao return ret; 25758a978234SLiam Girdwood } 25768a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); 25778a978234SLiam Girdwood 25788a978234SLiam Girdwood /* remove dynamic controls from the component driver */ 2579a5b8f71cSAmadeusz Sławiński int snd_soc_tplg_component_remove(struct snd_soc_component *comp) 25808a978234SLiam Girdwood { 25817e567b5aSTakashi Iwai struct snd_card *card = comp->card->snd_card; 25828a978234SLiam Girdwood struct snd_soc_dobj *dobj, *next_dobj; 2583395f8fd6SAmadeusz Sławiński int pass; 25848a978234SLiam Girdwood 25858a978234SLiam Girdwood /* process the header types from end to start */ 2586395f8fd6SAmadeusz Sławiński for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) { 25878a978234SLiam Girdwood 25888a978234SLiam Girdwood /* remove mixer controls */ 25897e567b5aSTakashi Iwai down_write(&card->controls_rwsem); 25908a978234SLiam Girdwood list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, 25918a978234SLiam Girdwood list) { 25928a978234SLiam Girdwood 25938a978234SLiam Girdwood switch (dobj->type) { 25948a978234SLiam Girdwood case SND_SOC_DOBJ_BYTES: 2595fdfa3661SAmadeusz Sławiński case SND_SOC_DOBJ_ENUM: 2596fdfa3661SAmadeusz Sławiński case SND_SOC_DOBJ_MIXER: 2597fdfa3661SAmadeusz Sławiński soc_tplg_remove_kcontrol(comp, dobj, pass); 25988a978234SLiam Girdwood break; 25997df04ea7SRanjani Sridharan case SND_SOC_DOBJ_GRAPH: 26002abfd4bdSAmadeusz Sławiński soc_tplg_remove_route(comp, dobj, pass); 26017df04ea7SRanjani Sridharan break; 26028a978234SLiam Girdwood case SND_SOC_DOBJ_WIDGET: 26032abfd4bdSAmadeusz Sławiński soc_tplg_remove_widget(comp, dobj, pass); 26048a978234SLiam Girdwood break; 26058a978234SLiam Girdwood case SND_SOC_DOBJ_PCM: 26062abfd4bdSAmadeusz Sławiński soc_tplg_remove_dai(comp, dobj, pass); 26078a978234SLiam Girdwood break; 2608acfc7d46SMengdong Lin case SND_SOC_DOBJ_DAI_LINK: 26092abfd4bdSAmadeusz Sławiński soc_tplg_remove_link(comp, dobj, pass); 2610acfc7d46SMengdong Lin break; 2611adfebb51SBard liao case SND_SOC_DOBJ_BACKEND_LINK: 2612adfebb51SBard liao /* 2613adfebb51SBard liao * call link_unload ops if extra 2614adfebb51SBard liao * deinitialization is needed. 2615adfebb51SBard liao */ 2616adfebb51SBard liao remove_backend_link(comp, dobj, pass); 2617adfebb51SBard liao break; 26188a978234SLiam Girdwood default: 26198a978234SLiam Girdwood dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", 26208a978234SLiam Girdwood dobj->type); 26218a978234SLiam Girdwood break; 26228a978234SLiam Girdwood } 26238a978234SLiam Girdwood } 26247e567b5aSTakashi Iwai up_write(&card->controls_rwsem); 26258a978234SLiam Girdwood } 26268a978234SLiam Girdwood 26278a978234SLiam Girdwood /* let caller know if FW can be freed when no objects are left */ 26288a978234SLiam Girdwood return !list_empty(&comp->dobj_list); 26298a978234SLiam Girdwood } 26308a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); 2631