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 { 588ec5dffcdSAmadeusz Sławiński int ret = 0; 5898a978234SLiam Girdwood 590ec5dffcdSAmadeusz Sławiński if (tplg->ops && tplg->ops->control_load) 591ec5dffcdSAmadeusz Sławiński ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr); 592ec5dffcdSAmadeusz Sławiński 593ec5dffcdSAmadeusz Sławiński if (ret) 594ec5dffcdSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name); 595ec5dffcdSAmadeusz Sławiński 596ec5dffcdSAmadeusz 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); 698ec5dffcdSAmadeusz 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); 7032316c11fSAmadeusz Sławiński if (ret < 0) 7040db627c4SAmadeusz Sławiński goto err; 7058a978234SLiam Girdwood 7068a978234SLiam Girdwood list_add(&sbe->dobj.list, &tplg->comp->dobj_list); 7078a978234SLiam Girdwood 7080db627c4SAmadeusz Sławiński err: 7090db627c4SAmadeusz Sławiński return ret; 7108a978234SLiam Girdwood } 7118a978234SLiam Girdwood 7120db627c4SAmadeusz Sławiński static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size) 7138a978234SLiam Girdwood { 7148a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 7158a978234SLiam Girdwood struct soc_mixer_control *sm; 7168a978234SLiam Girdwood struct snd_kcontrol_new kc; 7170db627c4SAmadeusz Sławiński int ret = 0; 7188a978234SLiam Girdwood 7198a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 7208a978234SLiam Girdwood sizeof(struct snd_soc_tplg_mixer_control), 7210db627c4SAmadeusz Sławiński 1, size, "mixers")) 7228a978234SLiam Girdwood return -EINVAL; 7238a978234SLiam Girdwood 7248a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 7258a978234SLiam Girdwood 7268a978234SLiam Girdwood /* validate kcontrol */ 7278a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 7288a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 7298a978234SLiam Girdwood return -EINVAL; 7308a978234SLiam Girdwood 731ff922622SAmadeusz Sławiński sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL); 7328a978234SLiam Girdwood if (sm == NULL) 7338a978234SLiam Girdwood return -ENOMEM; 7348a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + 7355aebe7c7SPierre-Louis Bossart le32_to_cpu(mc->priv.size)); 7368a978234SLiam Girdwood 7378a978234SLiam Girdwood dev_dbg(tplg->dev, 7388a978234SLiam Girdwood "ASoC: adding mixer kcontrol %s with access 0x%x\n", 7398a978234SLiam Girdwood mc->hdr.name, mc->hdr.access); 7408a978234SLiam Girdwood 7418a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 7428a978234SLiam Girdwood kc.name = mc->hdr.name; 7438a978234SLiam Girdwood kc.private_value = (long)sm; 7448a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 7455aebe7c7SPierre-Louis Bossart kc.access = le32_to_cpu(mc->hdr.access); 7468a978234SLiam Girdwood 7478a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 7488f9974d9SAmadeusz Sławiński sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL); 7498f9974d9SAmadeusz Sławiński sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR); 7508f9974d9SAmadeusz Sławiński sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL); 7518f9974d9SAmadeusz Sławiński sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR); 7528a978234SLiam Girdwood 7535aebe7c7SPierre-Louis Bossart sm->max = le32_to_cpu(mc->max); 7545aebe7c7SPierre-Louis Bossart sm->min = le32_to_cpu(mc->min); 7555aebe7c7SPierre-Louis Bossart sm->invert = le32_to_cpu(mc->invert); 7565aebe7c7SPierre-Louis Bossart sm->platform_max = le32_to_cpu(mc->platform_max); 7578a978234SLiam Girdwood sm->dobj.index = tplg->index; 7588a978234SLiam Girdwood sm->dobj.type = SND_SOC_DOBJ_MIXER; 75931e92739SAmadeusz Sławiński if (tplg->ops) 76031e92739SAmadeusz Sławiński sm->dobj.unload = tplg->ops->control_unload; 7618a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 7628a978234SLiam Girdwood 7638a978234SLiam Girdwood /* map io handlers */ 7640db627c4SAmadeusz Sławiński ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); 7650db627c4SAmadeusz Sławiński if (ret) { 7668a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 7670db627c4SAmadeusz Sławiński goto err; 7688a978234SLiam Girdwood } 7698a978234SLiam Girdwood 7703789debfSBard liao /* create any TLV data */ 7710db627c4SAmadeusz Sławiński ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr); 7720db627c4SAmadeusz Sławiński if (ret < 0) { 7730db627c4SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name); 7740db627c4SAmadeusz Sławiński goto err; 775482db55aSAmadeusz Sławiński } 7763789debfSBard liao 7778a978234SLiam Girdwood /* pass control to driver for optional further init */ 7789e2ee000SAmadeusz Sławiński ret = soc_tplg_control_load(tplg, &kc, &mc->hdr); 779ec5dffcdSAmadeusz Sławiński if (ret < 0) 7800db627c4SAmadeusz Sławiński goto err; 7818a978234SLiam Girdwood 7828a978234SLiam Girdwood /* register control here */ 7830db627c4SAmadeusz Sławiński ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol); 7842316c11fSAmadeusz Sławiński if (ret < 0) 7850db627c4SAmadeusz Sławiński goto err; 7868a978234SLiam Girdwood 7878a978234SLiam Girdwood list_add(&sm->dobj.list, &tplg->comp->dobj_list); 7888a978234SLiam Girdwood 7890db627c4SAmadeusz Sławiński err: 7900db627c4SAmadeusz Sławiński return ret; 7918a978234SLiam Girdwood } 7928a978234SLiam Girdwood 793ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se, 7948a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 7958a978234SLiam Girdwood { 7968a978234SLiam Girdwood int i, ret; 7978a978234SLiam Girdwood 798f5824e5cSAmadeusz Sławiński if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts)) 799f5824e5cSAmadeusz Sławiński return -EINVAL; 800f5824e5cSAmadeusz Sławiński 8018a978234SLiam Girdwood se->dobj.control.dtexts = 802ff922622SAmadeusz Sławiński devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); 8038a978234SLiam Girdwood if (se->dobj.control.dtexts == NULL) 8048a978234SLiam Girdwood return -ENOMEM; 8058a978234SLiam Girdwood 80672bbeda0SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(ec->items); i++) { 8078a978234SLiam Girdwood 8088a978234SLiam Girdwood if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 8098a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 8108a978234SLiam Girdwood ret = -EINVAL; 8118a978234SLiam Girdwood goto err; 8128a978234SLiam Girdwood } 8138a978234SLiam Girdwood 814ff922622SAmadeusz Sławiński se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL); 8158a978234SLiam Girdwood if (!se->dobj.control.dtexts[i]) { 8168a978234SLiam Girdwood ret = -ENOMEM; 8178a978234SLiam Girdwood goto err; 8188a978234SLiam Girdwood } 8198a978234SLiam Girdwood } 8208a978234SLiam Girdwood 8213cde818cSAmadeusz Sławiński se->items = le32_to_cpu(ec->items); 822b6e38b29SMousumi Jana se->texts = (const char * const *)se->dobj.control.dtexts; 8238a978234SLiam Girdwood return 0; 8248a978234SLiam Girdwood 8258a978234SLiam Girdwood err: 8263cde818cSAmadeusz Sławiński return ret; 8273cde818cSAmadeusz Sławiński } 8283cde818cSAmadeusz Sławiński 829ff922622SAmadeusz Sławiński static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se, 8308a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 8318a978234SLiam Girdwood { 8325aebe7c7SPierre-Louis Bossart int i; 8335aebe7c7SPierre-Louis Bossart 834631c78edSAmadeusz Sławiński /* 835631c78edSAmadeusz Sławiński * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS 836631c78edSAmadeusz Sławiński * values instead of using ARRAY_SIZE(ec->values) due to the fact that 837631c78edSAmadeusz Sławiński * it is oversized for its purpose. Additionally it is done so because 838631c78edSAmadeusz Sławiński * it is defined in UAPI header where it can't be easily changed. 839631c78edSAmadeusz Sławiński */ 840631c78edSAmadeusz Sławiński if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS) 8418a978234SLiam Girdwood return -EINVAL; 8428a978234SLiam Girdwood 843631c78edSAmadeusz Sławiński se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), 844543466efSDan Carpenter sizeof(*se->dobj.control.dvalues), 845376c0afeSAndrzej Hajda GFP_KERNEL); 8468a978234SLiam Girdwood if (!se->dobj.control.dvalues) 8478a978234SLiam Girdwood return -ENOMEM; 8488a978234SLiam Girdwood 8495aebe7c7SPierre-Louis Bossart /* convert from little-endian */ 8505aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(ec->items); i++) { 8515aebe7c7SPierre-Louis Bossart se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); 8525aebe7c7SPierre-Louis Bossart } 8535aebe7c7SPierre-Louis Bossart 8548a978234SLiam Girdwood return 0; 8558a978234SLiam Girdwood } 8568a978234SLiam Girdwood 8570db627c4SAmadeusz Sławiński static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size) 8588a978234SLiam Girdwood { 8598a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 8608a978234SLiam Girdwood struct soc_enum *se; 8618a978234SLiam Girdwood struct snd_kcontrol_new kc; 8620db627c4SAmadeusz Sławiński int ret = 0; 8638a978234SLiam Girdwood 8648a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 8658a978234SLiam Girdwood sizeof(struct snd_soc_tplg_enum_control), 8660db627c4SAmadeusz Sławiński 1, size, "enums")) 8678a978234SLiam Girdwood return -EINVAL; 8688a978234SLiam Girdwood 8698a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 8708a978234SLiam Girdwood 8718a978234SLiam Girdwood /* validate kcontrol */ 8728a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 8738a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 8748a978234SLiam Girdwood return -EINVAL; 8758a978234SLiam Girdwood 876ff922622SAmadeusz Sławiński se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL); 8778a978234SLiam Girdwood if (se == NULL) 8788a978234SLiam Girdwood return -ENOMEM; 8798a978234SLiam Girdwood 88002b64245SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 8815aebe7c7SPierre-Louis Bossart le32_to_cpu(ec->priv.size)); 88202b64245SLiam Girdwood 8838a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", 8848a978234SLiam Girdwood ec->hdr.name, ec->items); 8858a978234SLiam Girdwood 8868a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 8878a978234SLiam Girdwood kc.name = ec->hdr.name; 8888a978234SLiam Girdwood kc.private_value = (long)se; 8898a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 8905aebe7c7SPierre-Louis Bossart kc.access = le32_to_cpu(ec->hdr.access); 8918a978234SLiam Girdwood 8928f9974d9SAmadeusz Sławiński se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 8938f9974d9SAmadeusz Sławiński se->shift_l = tplg_chan_get_shift(tplg, ec->channel, 8948a978234SLiam Girdwood SNDRV_CHMAP_FL); 8958f9974d9SAmadeusz Sławiński se->shift_r = tplg_chan_get_shift(tplg, ec->channel, 8968a978234SLiam Girdwood SNDRV_CHMAP_FL); 8978a978234SLiam Girdwood 8985aebe7c7SPierre-Louis Bossart se->mask = le32_to_cpu(ec->mask); 8998a978234SLiam Girdwood se->dobj.index = tplg->index; 9008a978234SLiam Girdwood se->dobj.type = SND_SOC_DOBJ_ENUM; 90131e92739SAmadeusz Sławiński if (tplg->ops) 90231e92739SAmadeusz Sławiński se->dobj.unload = tplg->ops->control_unload; 9038a978234SLiam Girdwood INIT_LIST_HEAD(&se->dobj.list); 9048a978234SLiam Girdwood 9055aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(ec->hdr.ops.info)) { 9068a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 9078a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 9080db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create_values(tplg, se, ec); 9090db627c4SAmadeusz Sławiński if (ret < 0) { 9108a978234SLiam Girdwood dev_err(tplg->dev, 9118a978234SLiam Girdwood "ASoC: could not create values for %s\n", 9128a978234SLiam Girdwood ec->hdr.name); 9130db627c4SAmadeusz Sławiński goto err; 9148a978234SLiam Girdwood } 915df561f66SGustavo A. R. Silva fallthrough; 9168a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 9178a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 9188a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 9190db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create_texts(tplg, se, ec); 9200db627c4SAmadeusz Sławiński if (ret < 0) { 9218a978234SLiam Girdwood dev_err(tplg->dev, 9228a978234SLiam Girdwood "ASoC: could not create texts for %s\n", 9238a978234SLiam Girdwood ec->hdr.name); 9240db627c4SAmadeusz Sławiński goto err; 9258a978234SLiam Girdwood } 9268a978234SLiam Girdwood break; 9278a978234SLiam Girdwood default: 9280db627c4SAmadeusz Sławiński ret = -EINVAL; 9298a978234SLiam Girdwood dev_err(tplg->dev, 9308a978234SLiam Girdwood "ASoC: invalid enum control type %d for %s\n", 9318a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 9320db627c4SAmadeusz Sławiński goto err; 9338a978234SLiam Girdwood } 9348a978234SLiam Girdwood 9358a978234SLiam Girdwood /* map io handlers */ 9360db627c4SAmadeusz Sławiński ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); 9370db627c4SAmadeusz Sławiński if (ret) { 9388a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 9390db627c4SAmadeusz Sławiński goto err; 9408a978234SLiam Girdwood } 9418a978234SLiam Girdwood 9428a978234SLiam Girdwood /* pass control to driver for optional further init */ 9439e2ee000SAmadeusz Sławiński ret = soc_tplg_control_load(tplg, &kc, &ec->hdr); 944ec5dffcdSAmadeusz Sławiński if (ret < 0) 9450db627c4SAmadeusz Sławiński goto err; 9468a978234SLiam Girdwood 9478a978234SLiam Girdwood /* register control here */ 9480db627c4SAmadeusz Sławiński ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol); 9492316c11fSAmadeusz Sławiński if (ret < 0) 9500db627c4SAmadeusz Sławiński goto err; 9518a978234SLiam Girdwood 9528a978234SLiam Girdwood list_add(&se->dobj.list, &tplg->comp->dobj_list); 953952bd937SPierre-Louis Bossart 9540db627c4SAmadeusz Sławiński err: 9550db627c4SAmadeusz Sławiński return ret; 9568a978234SLiam Girdwood } 9578a978234SLiam Girdwood 9588a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, 9598a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 9608a978234SLiam Girdwood { 9612ae548f3SAmadeusz Sławiński int ret; 9628a978234SLiam Girdwood int i; 9638a978234SLiam Girdwood 9648a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, 9658a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 9668a978234SLiam Girdwood 9675aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(hdr->count); i++) { 968ea8f6b29SKuninori Morimoto struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 9698a978234SLiam Girdwood 9705aebe7c7SPierre-Louis Bossart if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { 97106eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid control size\n"); 97206eb49f7SMengdong Lin return -EINVAL; 97306eb49f7SMengdong Lin } 97406eb49f7SMengdong Lin 9755aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(control_hdr->ops.info)) { 9768a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 9778a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 9788a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 9798a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 9808a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 9818a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 9828a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_PIN: 9830db627c4SAmadeusz Sławiński ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size)); 9848a978234SLiam Girdwood break; 9858a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 9868a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 9878a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 9888a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 9898a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 9900db627c4SAmadeusz Sławiński ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size)); 9918a978234SLiam Girdwood break; 9928a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 9930db627c4SAmadeusz Sławiński ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size)); 9948a978234SLiam Girdwood break; 9958a978234SLiam Girdwood default: 9968a978234SLiam Girdwood soc_bind_err(tplg, control_hdr, i); 9978a978234SLiam Girdwood return -EINVAL; 9988a978234SLiam Girdwood } 9992ae548f3SAmadeusz Sławiński if (ret < 0) { 10002ae548f3SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid control\n"); 10012ae548f3SAmadeusz Sławiński return ret; 10022ae548f3SAmadeusz Sławiński } 10032ae548f3SAmadeusz Sławiński 10048a978234SLiam Girdwood } 10058a978234SLiam Girdwood 10068a978234SLiam Girdwood return 0; 10078a978234SLiam Girdwood } 10088a978234SLiam Girdwood 1009503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */ 1010503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg, 1011503e79b7SLiam Girdwood struct snd_soc_dapm_route *route) 1012503e79b7SLiam Girdwood { 1013c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->dapm_route_load) 1014503e79b7SLiam Girdwood return tplg->ops->dapm_route_load(tplg->comp, tplg->index, 1015503e79b7SLiam Girdwood route); 1016503e79b7SLiam Girdwood 1017503e79b7SLiam Girdwood return 0; 1018503e79b7SLiam Girdwood } 1019503e79b7SLiam Girdwood 10208a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, 10218a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 10228a978234SLiam Girdwood { 10238a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 1024bd865c76SAmadeusz Sławiński const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN; 10258a978234SLiam Girdwood struct snd_soc_tplg_dapm_graph_elem *elem; 1026cc44c749SAmadeusz Sławiński struct snd_soc_dapm_route *route; 1027ff922622SAmadeusz Sławiński int count, i; 10287df04ea7SRanjani Sridharan int ret = 0; 10298a978234SLiam Girdwood 10305aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 10315aebe7c7SPierre-Louis Bossart 10328a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 10338a978234SLiam Girdwood sizeof(struct snd_soc_tplg_dapm_graph_elem), 10343ce57f22SAmadeusz Sławiński count, le32_to_cpu(hdr->payload_size), "graph")) 10358a978234SLiam Girdwood return -EINVAL; 10368a978234SLiam Girdwood 1037b75a6511SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, 1038b75a6511SLiam Girdwood hdr->index); 10398a978234SLiam Girdwood 10407df04ea7SRanjani Sridharan for (i = 0; i < count; i++) { 1041cc44c749SAmadeusz Sławiński route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL); 1042cc44c749SAmadeusz Sławiński if (!route) 10437df04ea7SRanjani Sridharan return -ENOMEM; 10448a978234SLiam Girdwood elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; 10458a978234SLiam Girdwood tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); 10468a978234SLiam Girdwood 10478a978234SLiam Girdwood /* validate routes */ 1048bd865c76SAmadeusz Sławiński if ((strnlen(elem->source, maxlen) == maxlen) || 1049bd865c76SAmadeusz Sławiński (strnlen(elem->sink, maxlen) == maxlen) || 1050bd865c76SAmadeusz Sławiński (strnlen(elem->control, maxlen) == maxlen)) { 10517df04ea7SRanjani Sridharan ret = -EINVAL; 10527df04ea7SRanjani Sridharan break; 10538a978234SLiam Girdwood } 10548a978234SLiam Girdwood 1055*7c3e55d8SAmadeusz Sławiński route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL); 1056*7c3e55d8SAmadeusz Sławiński route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL); 1057ab5a6208SAmadeusz Sławiński if (!route->source || !route->sink) { 1058ab5a6208SAmadeusz Sławiński ret = -ENOMEM; 1059ab5a6208SAmadeusz Sławiński break; 1060ab5a6208SAmadeusz Sławiński } 10617df04ea7SRanjani Sridharan 1062bd865c76SAmadeusz Sławiński if (strnlen(elem->control, maxlen) != 0) { 1063*7c3e55d8SAmadeusz Sławiński route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL); 1064ab5a6208SAmadeusz Sławiński if (!route->control) { 1065ab5a6208SAmadeusz Sławiński ret = -ENOMEM; 1066ab5a6208SAmadeusz Sławiński break; 1067ab5a6208SAmadeusz Sławiński } 1068ab5a6208SAmadeusz Sławiński } 10697df04ea7SRanjani Sridharan 10707df04ea7SRanjani Sridharan /* add route dobj to dobj_list */ 1071cc44c749SAmadeusz Sławiński route->dobj.type = SND_SOC_DOBJ_GRAPH; 107231e92739SAmadeusz Sławiński if (tplg->ops) 1073dd184c40SPeter Ujfalusi route->dobj.unload = tplg->ops->dapm_route_unload; 1074cc44c749SAmadeusz Sławiński route->dobj.index = tplg->index; 1075cc44c749SAmadeusz Sławiński list_add(&route->dobj.list, &tplg->comp->dobj_list); 10767df04ea7SRanjani Sridharan 1077cc44c749SAmadeusz Sławiński ret = soc_tplg_add_route(tplg, route); 10786f0307dfSPierre-Louis Bossart if (ret < 0) { 10798bf9475fSPierre-Louis Bossart dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret); 10806856e887SAmadeusz Sławiński break; 10816f0307dfSPierre-Louis Bossart } 10827df04ea7SRanjani Sridharan 10837df04ea7SRanjani Sridharan /* add route, but keep going if some fail */ 1084cc44c749SAmadeusz Sławiński snd_soc_dapm_add_routes(dapm, route, 1); 10857df04ea7SRanjani Sridharan } 10867df04ea7SRanjani Sridharan 10877df04ea7SRanjani Sridharan return ret; 10888a978234SLiam Girdwood } 10898a978234SLiam Girdwood 1090d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 10918a978234SLiam Girdwood { 10928a978234SLiam Girdwood struct soc_mixer_control *sm; 10938a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 1094d29d41e2SJaska Uimonen int err; 10958a978234SLiam Girdwood 10968a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 10978a978234SLiam Girdwood 10988a978234SLiam Girdwood /* validate kcontrol */ 10998a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 11008a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1101d29d41e2SJaska Uimonen return -EINVAL; 11029f90af3aSAmadeusz Sławiński 1103ff922622SAmadeusz Sławiński sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL); 1104d29d41e2SJaska Uimonen if (!sm) 1105d29d41e2SJaska Uimonen return -ENOMEM; 11068a978234SLiam Girdwood 1107d29d41e2SJaska Uimonen tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + 1108d29d41e2SJaska Uimonen le32_to_cpu(mc->priv.size); 110902b64245SLiam Girdwood 1110d29d41e2SJaska Uimonen dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n", 1111d29d41e2SJaska Uimonen mc->hdr.name); 11128a978234SLiam Girdwood 1113d29d41e2SJaska Uimonen kc->private_value = (long)sm; 1114d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL); 1115d29d41e2SJaska Uimonen if (!kc->name) 1116d29d41e2SJaska Uimonen return -ENOMEM; 1117d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1118d29d41e2SJaska Uimonen kc->access = le32_to_cpu(mc->hdr.access); 11198a978234SLiam Girdwood 11208a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 11218f9974d9SAmadeusz Sławiński sm->reg = tplg_chan_get_reg(tplg, mc->channel, 11228a978234SLiam Girdwood SNDRV_CHMAP_FL); 11238f9974d9SAmadeusz Sławiński sm->rreg = tplg_chan_get_reg(tplg, mc->channel, 11248a978234SLiam Girdwood SNDRV_CHMAP_FR); 11258f9974d9SAmadeusz Sławiński sm->shift = tplg_chan_get_shift(tplg, mc->channel, 11268a978234SLiam Girdwood SNDRV_CHMAP_FL); 11278f9974d9SAmadeusz Sławiński sm->rshift = tplg_chan_get_shift(tplg, mc->channel, 11288a978234SLiam Girdwood SNDRV_CHMAP_FR); 11298a978234SLiam Girdwood 113072bbeda0SPierre-Louis Bossart sm->max = le32_to_cpu(mc->max); 113172bbeda0SPierre-Louis Bossart sm->min = le32_to_cpu(mc->min); 113272bbeda0SPierre-Louis Bossart sm->invert = le32_to_cpu(mc->invert); 113372bbeda0SPierre-Louis Bossart sm->platform_max = le32_to_cpu(mc->platform_max); 11348a978234SLiam Girdwood sm->dobj.index = tplg->index; 11358a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 11368a978234SLiam Girdwood 11378a978234SLiam Girdwood /* map io handlers */ 1138d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg); 11398a978234SLiam Girdwood if (err) { 11408a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 1141d29d41e2SJaska Uimonen return err; 11428a978234SLiam Girdwood } 11438a978234SLiam Girdwood 11443789debfSBard liao /* create any TLV data */ 1145d29d41e2SJaska Uimonen err = soc_tplg_create_tlv(tplg, kc, &mc->hdr); 1146482db55aSAmadeusz Sławiński if (err < 0) { 1147482db55aSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", 1148482db55aSAmadeusz Sławiński mc->hdr.name); 1149d29d41e2SJaska Uimonen return err; 1150482db55aSAmadeusz Sławiński } 11513789debfSBard liao 11528a978234SLiam Girdwood /* pass control to driver for optional further init */ 11539e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &mc->hdr); 1154ec5dffcdSAmadeusz Sławiński if (err < 0) 1155d29d41e2SJaska Uimonen return err; 11568a978234SLiam Girdwood 1157d29d41e2SJaska Uimonen return 0; 1158d29d41e2SJaska Uimonen } 1159d29d41e2SJaska Uimonen 1160d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 11618a978234SLiam Girdwood { 11628a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 11638a978234SLiam Girdwood struct soc_enum *se; 1164d29d41e2SJaska Uimonen int err; 11658a978234SLiam Girdwood 11668a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 11678a978234SLiam Girdwood /* validate kcontrol */ 11688a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 11698a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1170d29d41e2SJaska Uimonen return -EINVAL; 11718a978234SLiam Girdwood 1172ff922622SAmadeusz Sławiński se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL); 1173d29d41e2SJaska Uimonen if (!se) 1174d29d41e2SJaska Uimonen return -ENOMEM; 11758a978234SLiam Girdwood 117602b64245SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 117772bbeda0SPierre-Louis Bossart le32_to_cpu(ec->priv.size)); 117802b64245SLiam Girdwood 11798a978234SLiam Girdwood dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", 11808a978234SLiam Girdwood ec->hdr.name); 11818a978234SLiam Girdwood 1182d29d41e2SJaska Uimonen kc->private_value = (long)se; 1183d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL); 1184d29d41e2SJaska Uimonen if (!kc->name) 1185d29d41e2SJaska Uimonen return -ENOMEM; 1186d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1187d29d41e2SJaska Uimonen kc->access = le32_to_cpu(ec->hdr.access); 11888a978234SLiam Girdwood 11898a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 11908f9974d9SAmadeusz Sławiński se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 11918f9974d9SAmadeusz Sławiński se->shift_l = tplg_chan_get_shift(tplg, ec->channel, 11921a7dd6e2SMengdong Lin SNDRV_CHMAP_FL); 11938f9974d9SAmadeusz Sławiński se->shift_r = tplg_chan_get_shift(tplg, ec->channel, 11941a7dd6e2SMengdong Lin SNDRV_CHMAP_FR); 11958a978234SLiam Girdwood 119672bbeda0SPierre-Louis Bossart se->items = le32_to_cpu(ec->items); 119772bbeda0SPierre-Louis Bossart se->mask = le32_to_cpu(ec->mask); 11988a978234SLiam Girdwood se->dobj.index = tplg->index; 11998a978234SLiam Girdwood 12005aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(ec->hdr.ops.info)) { 12018a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 12028a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1203ff922622SAmadeusz Sławiński err = soc_tplg_denum_create_values(tplg, se, ec); 12048a978234SLiam Girdwood if (err < 0) { 12058a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create values for %s\n", 12068a978234SLiam Girdwood ec->hdr.name); 1207d29d41e2SJaska Uimonen return err; 12088a978234SLiam Girdwood } 1209df561f66SGustavo A. R. Silva fallthrough; 12108a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 12118a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 12128a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 1213ff922622SAmadeusz Sławiński err = soc_tplg_denum_create_texts(tplg, se, ec); 12148a978234SLiam Girdwood if (err < 0) { 12158a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create texts for %s\n", 12168a978234SLiam Girdwood ec->hdr.name); 1217d29d41e2SJaska Uimonen return err; 12188a978234SLiam Girdwood } 12198a978234SLiam Girdwood break; 12208a978234SLiam Girdwood default: 12218a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", 12228a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 1223d29d41e2SJaska Uimonen return -EINVAL; 12248a978234SLiam Girdwood } 12258a978234SLiam Girdwood 12268a978234SLiam Girdwood /* map io handlers */ 1227d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg); 12288a978234SLiam Girdwood if (err) { 12298a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 1230d29d41e2SJaska Uimonen return err; 12318a978234SLiam Girdwood } 12328a978234SLiam Girdwood 12338a978234SLiam Girdwood /* pass control to driver for optional further init */ 12349e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &ec->hdr); 1235ec5dffcdSAmadeusz Sławiński if (err < 0) 1236d29d41e2SJaska Uimonen return err; 12371a7dd6e2SMengdong Lin 1238d29d41e2SJaska Uimonen return 0; 12398a978234SLiam Girdwood } 12408a978234SLiam Girdwood 1241d29d41e2SJaska Uimonen static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 12428a978234SLiam Girdwood { 12438a978234SLiam Girdwood struct snd_soc_tplg_bytes_control *be; 12448a978234SLiam Girdwood struct soc_bytes_ext *sbe; 1245d29d41e2SJaska Uimonen int err; 12468a978234SLiam Girdwood 12478a978234SLiam Girdwood be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 12488a978234SLiam Girdwood 12498a978234SLiam Girdwood /* validate kcontrol */ 12508a978234SLiam Girdwood if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12518a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1252d29d41e2SJaska Uimonen return -EINVAL; 12538a978234SLiam Girdwood 1254ff922622SAmadeusz Sławiński sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL); 1255d29d41e2SJaska Uimonen if (!sbe) 1256d29d41e2SJaska Uimonen return -ENOMEM; 12578a978234SLiam Girdwood 12588a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + 12595aebe7c7SPierre-Louis Bossart le32_to_cpu(be->priv.size)); 12608a978234SLiam Girdwood 12618a978234SLiam Girdwood dev_dbg(tplg->dev, 12628a978234SLiam Girdwood "ASoC: adding bytes kcontrol %s with access 0x%x\n", 12638a978234SLiam Girdwood be->hdr.name, be->hdr.access); 12648a978234SLiam Girdwood 1265d29d41e2SJaska Uimonen kc->private_value = (long)sbe; 1266d29d41e2SJaska Uimonen kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL); 1267d29d41e2SJaska Uimonen if (!kc->name) 1268d29d41e2SJaska Uimonen return -ENOMEM; 1269d29d41e2SJaska Uimonen kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1270d29d41e2SJaska Uimonen kc->access = le32_to_cpu(be->hdr.access); 12718a978234SLiam Girdwood 127272bbeda0SPierre-Louis Bossart sbe->max = le32_to_cpu(be->max); 12738a978234SLiam Girdwood INIT_LIST_HEAD(&sbe->dobj.list); 12748a978234SLiam Girdwood 12758a978234SLiam Girdwood /* map standard io handlers and check for external handlers */ 1276d29d41e2SJaska Uimonen err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg); 12778a978234SLiam Girdwood if (err) { 12788a978234SLiam Girdwood soc_control_err(tplg, &be->hdr, be->hdr.name); 1279d29d41e2SJaska Uimonen return err; 12808a978234SLiam Girdwood } 12818a978234SLiam Girdwood 12828a978234SLiam Girdwood /* pass control to driver for optional further init */ 12839e2ee000SAmadeusz Sławiński err = soc_tplg_control_load(tplg, kc, &be->hdr); 1284ec5dffcdSAmadeusz Sławiński if (err < 0) 1285d29d41e2SJaska Uimonen return err; 12868a978234SLiam Girdwood 1287d29d41e2SJaska Uimonen return 0; 12888a978234SLiam Girdwood } 12898a978234SLiam Girdwood 12908a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, 12918a978234SLiam Girdwood struct snd_soc_tplg_dapm_widget *w) 12928a978234SLiam Girdwood { 12938a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 12948a978234SLiam Girdwood struct snd_soc_dapm_widget template, *widget; 12958a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *control_hdr; 12968a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 1297b9c035aaSJaska Uimonen unsigned int *kcontrol_type = NULL; 1298d29d41e2SJaska Uimonen struct snd_kcontrol_new *kc; 1299d29d41e2SJaska Uimonen int mixer_count = 0; 1300d29d41e2SJaska Uimonen int bytes_count = 0; 1301d29d41e2SJaska Uimonen int enum_count = 0; 13028a978234SLiam Girdwood int ret = 0; 1303d29d41e2SJaska Uimonen int i; 13048a978234SLiam Girdwood 13058a978234SLiam Girdwood if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 13068a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 13078a978234SLiam Girdwood return -EINVAL; 13088a978234SLiam Girdwood if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 13098a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 13108a978234SLiam Girdwood return -EINVAL; 13118a978234SLiam Girdwood 13128a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", 13138a978234SLiam Girdwood w->name, w->id); 13148a978234SLiam Girdwood 13158a978234SLiam Girdwood memset(&template, 0, sizeof(template)); 13168a978234SLiam Girdwood 13178a978234SLiam Girdwood /* map user to kernel widget ID */ 13185aebe7c7SPierre-Louis Bossart template.id = get_widget_id(le32_to_cpu(w->id)); 1319752c938aSDan Carpenter if ((int)template.id < 0) 13208a978234SLiam Girdwood return template.id; 13218a978234SLiam Girdwood 1322c3421a6aSLiam Girdwood /* strings are allocated here, but used and freed by the widget */ 13238a978234SLiam Girdwood template.name = kstrdup(w->name, GFP_KERNEL); 13248a978234SLiam Girdwood if (!template.name) 13258a978234SLiam Girdwood return -ENOMEM; 13268a978234SLiam Girdwood template.sname = kstrdup(w->sname, GFP_KERNEL); 13278a978234SLiam Girdwood if (!template.sname) { 13288a978234SLiam Girdwood ret = -ENOMEM; 13298a978234SLiam Girdwood goto err; 13308a978234SLiam Girdwood } 13315aebe7c7SPierre-Louis Bossart template.reg = le32_to_cpu(w->reg); 13325aebe7c7SPierre-Louis Bossart template.shift = le32_to_cpu(w->shift); 13335aebe7c7SPierre-Louis Bossart template.mask = le32_to_cpu(w->mask); 13345aebe7c7SPierre-Louis Bossart template.subseq = le32_to_cpu(w->subseq); 13358a978234SLiam Girdwood template.on_val = w->invert ? 0 : 1; 13368a978234SLiam Girdwood template.off_val = w->invert ? 1 : 0; 13375aebe7c7SPierre-Louis Bossart template.ignore_suspend = le32_to_cpu(w->ignore_suspend); 13385aebe7c7SPierre-Louis Bossart template.event_flags = le16_to_cpu(w->event_flags); 13398a978234SLiam Girdwood template.dobj.index = tplg->index; 13408a978234SLiam Girdwood 13418a978234SLiam Girdwood tplg->pos += 13425aebe7c7SPierre-Louis Bossart (sizeof(struct snd_soc_tplg_dapm_widget) + 13435aebe7c7SPierre-Louis Bossart le32_to_cpu(w->priv.size)); 13445aebe7c7SPierre-Louis Bossart 13458a978234SLiam Girdwood if (w->num_kcontrols == 0) { 13468a978234SLiam Girdwood template.num_kcontrols = 0; 13478a978234SLiam Girdwood goto widget; 13488a978234SLiam Girdwood } 13498a978234SLiam Girdwood 1350d29d41e2SJaska Uimonen template.num_kcontrols = le32_to_cpu(w->num_kcontrols); 1351d29d41e2SJaska Uimonen kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL); 1352c173ee5bSAmadeusz Sławiński if (!kc) { 1353c173ee5bSAmadeusz Sławiński ret = -ENOMEM; 13549c363532SPeter Ujfalusi goto hdr_err; 1355c173ee5bSAmadeusz Sławiński } 1356d29d41e2SJaska Uimonen 1357d29d41e2SJaska Uimonen kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int), 1358d29d41e2SJaska Uimonen GFP_KERNEL); 1359c173ee5bSAmadeusz Sławiński if (!kcontrol_type) { 1360c173ee5bSAmadeusz Sławiński ret = -ENOMEM; 13619c363532SPeter Ujfalusi goto hdr_err; 1362c173ee5bSAmadeusz Sławiński } 1363d29d41e2SJaska Uimonen 13641baad7daSPierre-Louis Bossart for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) { 1365d29d41e2SJaska Uimonen control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 13665aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(control_hdr->ops.info)) { 13678a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 13688a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 13698a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 13708a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 13718a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 13728a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 1373d29d41e2SJaska Uimonen /* volume mixer */ 1374d29d41e2SJaska Uimonen kc[i].index = mixer_count; 1375d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER; 1376d29d41e2SJaska Uimonen mixer_count++; 1377d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]); 1378d29d41e2SJaska Uimonen if (ret < 0) 13798a978234SLiam Girdwood goto hdr_err; 13808a978234SLiam Girdwood break; 13818a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 13828a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 13838a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 13848a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 13858a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1386d29d41e2SJaska Uimonen /* enumerated mixer */ 1387d29d41e2SJaska Uimonen kc[i].index = enum_count; 1388d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM; 1389d29d41e2SJaska Uimonen enum_count++; 1390d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]); 1391d29d41e2SJaska Uimonen if (ret < 0) 13928a978234SLiam Girdwood goto hdr_err; 13938a978234SLiam Girdwood break; 13948a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 1395d29d41e2SJaska Uimonen /* bytes control */ 1396d29d41e2SJaska Uimonen kc[i].index = bytes_count; 1397d29d41e2SJaska Uimonen kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES; 1398d29d41e2SJaska Uimonen bytes_count++; 1399d29d41e2SJaska Uimonen ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]); 1400d29d41e2SJaska Uimonen if (ret < 0) 14018a978234SLiam Girdwood goto hdr_err; 14028a978234SLiam Girdwood break; 14038a978234SLiam Girdwood default: 14048a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", 14058a978234SLiam Girdwood control_hdr->ops.get, control_hdr->ops.put, 14065aebe7c7SPierre-Louis Bossart le32_to_cpu(control_hdr->ops.info)); 14078a978234SLiam Girdwood ret = -EINVAL; 14088a978234SLiam Girdwood goto hdr_err; 14098a978234SLiam Girdwood } 1410d29d41e2SJaska Uimonen } 1411d29d41e2SJaska Uimonen 1412d29d41e2SJaska Uimonen template.kcontrol_news = kc; 14138facf84bSPeter Ujfalusi dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n", 14148facf84bSPeter Ujfalusi w->name, mixer_count, enum_count, bytes_count); 14158a978234SLiam Girdwood 14168a978234SLiam Girdwood widget: 14178a978234SLiam Girdwood ret = soc_tplg_widget_load(tplg, &template, w); 14188a978234SLiam Girdwood if (ret < 0) 14198a978234SLiam Girdwood goto hdr_err; 14208a978234SLiam Girdwood 14218a978234SLiam Girdwood /* card dapm mutex is held by the core if we are loading topology 14228a978234SLiam Girdwood * data during sound card init. */ 14232b34c135SKuninori Morimoto if (snd_soc_card_is_instantiated(card)) 14248a978234SLiam Girdwood widget = snd_soc_dapm_new_control(dapm, &template); 14258a978234SLiam Girdwood else 14268a978234SLiam Girdwood widget = snd_soc_dapm_new_control_unlocked(dapm, &template); 142737e1df8cSLinus Walleij if (IS_ERR(widget)) { 142837e1df8cSLinus Walleij ret = PTR_ERR(widget); 14298a978234SLiam Girdwood goto hdr_err; 14308a978234SLiam Girdwood } 14318a978234SLiam Girdwood 14328a978234SLiam Girdwood widget->dobj.type = SND_SOC_DOBJ_WIDGET; 1433eea3dd4fSMengdong Lin widget->dobj.widget.kcontrol_type = kcontrol_type; 143431e92739SAmadeusz Sławiński if (tplg->ops) 143531e92739SAmadeusz Sławiński widget->dobj.unload = tplg->ops->widget_unload; 14368a978234SLiam Girdwood widget->dobj.index = tplg->index; 14378a978234SLiam Girdwood list_add(&widget->dobj.list, &tplg->comp->dobj_list); 1438ebd259d3SLiam Girdwood 1439ebd259d3SLiam Girdwood ret = soc_tplg_widget_ready(tplg, widget, w); 1440ebd259d3SLiam Girdwood if (ret < 0) 1441ebd259d3SLiam Girdwood goto ready_err; 1442ebd259d3SLiam Girdwood 14437620fe91SBard liao kfree(template.sname); 14447620fe91SBard liao kfree(template.name); 14457620fe91SBard liao 14468a978234SLiam Girdwood return 0; 14478a978234SLiam Girdwood 1448ebd259d3SLiam Girdwood ready_err: 14492abfd4bdSAmadeusz Sławiński soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET); 1450ebd259d3SLiam Girdwood snd_soc_dapm_free_widget(widget); 14518a978234SLiam Girdwood hdr_err: 14528a978234SLiam Girdwood kfree(template.sname); 14538a978234SLiam Girdwood err: 14548a978234SLiam Girdwood kfree(template.name); 14558a978234SLiam Girdwood return ret; 14568a978234SLiam Girdwood } 14578a978234SLiam Girdwood 14588a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, 14598a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 14608a978234SLiam Girdwood { 1461e9aa139fSKuninori Morimoto int count, i; 14625aebe7c7SPierre-Louis Bossart 14635aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 14648a978234SLiam Girdwood 14658a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); 14668a978234SLiam Girdwood 14678a978234SLiam Girdwood for (i = 0; i < count; i++) { 1468e9aa139fSKuninori Morimoto struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; 1469e9aa139fSKuninori Morimoto int ret; 1470e9aa139fSKuninori Morimoto 14712e288333SAmadeusz Sławiński /* 14722e288333SAmadeusz Sławiński * check if widget itself fits within topology file 14732e288333SAmadeusz Sławiński * use sizeof instead of widget->size, as we can't be sure 14742e288333SAmadeusz Sławiński * it is set properly yet (file may end before it is present) 14752e288333SAmadeusz Sławiński */ 14762e288333SAmadeusz Sławiński if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) { 14772e288333SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid widget data size\n"); 14782e288333SAmadeusz Sławiński return -EINVAL; 14792e288333SAmadeusz Sławiński } 14802e288333SAmadeusz Sławiński 14812e288333SAmadeusz Sławiński /* check if widget has proper size */ 14825aebe7c7SPierre-Louis Bossart if (le32_to_cpu(widget->size) != sizeof(*widget)) { 148306eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid widget size\n"); 148406eb49f7SMengdong Lin return -EINVAL; 148506eb49f7SMengdong Lin } 148606eb49f7SMengdong Lin 14872e288333SAmadeusz Sławiński /* check if widget private data fits within topology file */ 14882e288333SAmadeusz Sławiński if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) { 14892e288333SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: invalid widget private data size\n"); 14902e288333SAmadeusz Sławiński return -EINVAL; 14912e288333SAmadeusz Sławiński } 14922e288333SAmadeusz Sławiński 14938a978234SLiam Girdwood ret = soc_tplg_dapm_widget_create(tplg, widget); 14947de76b62SMengdong Lin if (ret < 0) { 14958a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to load widget %s\n", 14968a978234SLiam Girdwood widget->name); 14977de76b62SMengdong Lin return ret; 14987de76b62SMengdong Lin } 14998a978234SLiam Girdwood } 15008a978234SLiam Girdwood 15018a978234SLiam Girdwood return 0; 15028a978234SLiam Girdwood } 15038a978234SLiam Girdwood 15048a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg) 15058a978234SLiam Girdwood { 15068a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 15078a978234SLiam Girdwood int ret; 15088a978234SLiam Girdwood 15098a978234SLiam Girdwood /* Card might not have been registered at this point. 15108a978234SLiam Girdwood * If so, just return success. 15118a978234SLiam Girdwood */ 15122b34c135SKuninori Morimoto if (!snd_soc_card_is_instantiated(card)) { 151353085402SAmadeusz Sławiński dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n"); 15148a978234SLiam Girdwood return 0; 15158a978234SLiam Girdwood } 15168a978234SLiam Girdwood 15178a978234SLiam Girdwood ret = snd_soc_dapm_new_widgets(card); 15188a978234SLiam Girdwood if (ret < 0) 151953085402SAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret); 15208a978234SLiam Girdwood 1521b784617aSAmadeusz Sławiński return ret; 15228a978234SLiam Girdwood } 15238a978234SLiam Girdwood 1524ff922622SAmadeusz Sławiński static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream, 1525b6b6e4d6SMengdong Lin struct snd_soc_tplg_stream_caps *caps) 1526b6b6e4d6SMengdong Lin { 1527ff922622SAmadeusz Sławiński stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL); 1528abc3caacSAmadeusz Sławiński if (!stream->stream_name) 1529abc3caacSAmadeusz Sławiński return -ENOMEM; 1530abc3caacSAmadeusz Sławiński 15315aebe7c7SPierre-Louis Bossart stream->channels_min = le32_to_cpu(caps->channels_min); 15325aebe7c7SPierre-Louis Bossart stream->channels_max = le32_to_cpu(caps->channels_max); 15335aebe7c7SPierre-Louis Bossart stream->rates = le32_to_cpu(caps->rates); 15345aebe7c7SPierre-Louis Bossart stream->rate_min = le32_to_cpu(caps->rate_min); 15355aebe7c7SPierre-Louis Bossart stream->rate_max = le32_to_cpu(caps->rate_max); 15365aebe7c7SPierre-Louis Bossart stream->formats = le64_to_cpu(caps->formats); 15375aebe7c7SPierre-Louis Bossart stream->sig_bits = le32_to_cpu(caps->sig_bits); 1538abc3caacSAmadeusz Sławiński 1539abc3caacSAmadeusz Sławiński return 0; 1540b6b6e4d6SMengdong Lin } 1541b6b6e4d6SMengdong Lin 15420038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, 15430038be9aSMengdong Lin unsigned int flag_mask, unsigned int flags) 15440038be9aSMengdong Lin { 15450038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) 1546f14654ddSKuninori Morimoto dai_drv->symmetric_rate = 154747108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 15480038be9aSMengdong Lin 15490038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) 15500038be9aSMengdong Lin dai_drv->symmetric_channels = 155147108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ? 15520038be9aSMengdong Lin 1 : 0; 15530038be9aSMengdong Lin 15540038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) 1555f14654ddSKuninori Morimoto dai_drv->symmetric_sample_bits = 155647108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 15570038be9aSMengdong Lin 1 : 0; 15580038be9aSMengdong Lin } 15590038be9aSMengdong Lin 156080585b0cSKuninori Morimoto static const struct snd_soc_dai_ops tplg_dai_ops = { 156180585b0cSKuninori Morimoto .compress_new = snd_soc_new_compress, 156280585b0cSKuninori Morimoto }; 156380585b0cSKuninori Morimoto 156464527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg, 156564527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 156664527e8aSMengdong Lin { 156764527e8aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 156864527e8aSMengdong Lin struct snd_soc_pcm_stream *stream; 156964527e8aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 1570e443c205SKuninori Morimoto struct snd_soc_dai *dai; 1571e443c205SKuninori Morimoto struct snd_soc_dapm_context *dapm = 1572e443c205SKuninori Morimoto snd_soc_component_get_dapm(tplg->comp); 157364527e8aSMengdong Lin int ret; 157464527e8aSMengdong Lin 1575ff922622SAmadeusz Sławiński dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL); 157664527e8aSMengdong Lin if (dai_drv == NULL) 157764527e8aSMengdong Lin return -ENOMEM; 157864527e8aSMengdong Lin 1579abc3caacSAmadeusz Sławiński if (strlen(pcm->dai_name)) { 1580ff922622SAmadeusz Sławiński dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1581abc3caacSAmadeusz Sławiński if (!dai_drv->name) { 1582abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1583abc3caacSAmadeusz Sławiński goto err; 1584abc3caacSAmadeusz Sławiński } 1585abc3caacSAmadeusz Sławiński } 15865aebe7c7SPierre-Louis Bossart dai_drv->id = le32_to_cpu(pcm->dai_id); 158764527e8aSMengdong Lin 158864527e8aSMengdong Lin if (pcm->playback) { 158964527e8aSMengdong Lin stream = &dai_drv->playback; 159064527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 1591ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 1592abc3caacSAmadeusz Sławiński if (ret < 0) 1593abc3caacSAmadeusz Sławiński goto err; 159464527e8aSMengdong Lin } 159564527e8aSMengdong Lin 159664527e8aSMengdong Lin if (pcm->capture) { 159764527e8aSMengdong Lin stream = &dai_drv->capture; 159864527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 1599ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 1600abc3caacSAmadeusz Sławiński if (ret < 0) 1601abc3caacSAmadeusz Sławiński goto err; 160264527e8aSMengdong Lin } 160364527e8aSMengdong Lin 16045db6aab6SLiam Girdwood if (pcm->compress) 160580585b0cSKuninori Morimoto dai_drv->ops = &tplg_dai_ops; 16065db6aab6SLiam Girdwood 160764527e8aSMengdong Lin /* pass control to component driver for optional further init */ 1608c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); 160964527e8aSMengdong Lin if (ret < 0) { 1610e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 1611abc3caacSAmadeusz Sławiński goto err; 161264527e8aSMengdong Lin } 161364527e8aSMengdong Lin 161464527e8aSMengdong Lin dai_drv->dobj.index = tplg->index; 161564527e8aSMengdong Lin dai_drv->dobj.type = SND_SOC_DOBJ_PCM; 161631e92739SAmadeusz Sławiński if (tplg->ops) 161731e92739SAmadeusz Sławiński dai_drv->dobj.unload = tplg->ops->dai_unload; 161864527e8aSMengdong Lin list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); 161964527e8aSMengdong Lin 162064527e8aSMengdong Lin /* register the DAI to the component */ 1621fc4cb1e1SAmadeusz Sławiński dai = snd_soc_register_dai(tplg->comp, dai_drv, false); 1622e443c205SKuninori Morimoto if (!dai) 1623e443c205SKuninori Morimoto return -ENOMEM; 1624e443c205SKuninori Morimoto 1625e443c205SKuninori Morimoto /* Create the DAI widgets here */ 1626e443c205SKuninori Morimoto ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1627e443c205SKuninori Morimoto if (ret != 0) { 1628e443c205SKuninori Morimoto dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret); 1629fc4cb1e1SAmadeusz Sławiński snd_soc_unregister_dai(dai); 1630e443c205SKuninori Morimoto return ret; 1631e443c205SKuninori Morimoto } 1632e443c205SKuninori Morimoto 1633abc3caacSAmadeusz Sławiński return 0; 1634abc3caacSAmadeusz Sławiński 1635abc3caacSAmadeusz Sławiński err: 1636e443c205SKuninori Morimoto return ret; 163764527e8aSMengdong Lin } 163864527e8aSMengdong Lin 1639717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link, 1640717a8e72SMengdong Lin unsigned int flag_mask, unsigned int flags) 1641717a8e72SMengdong Lin { 1642717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) 1643f14654ddSKuninori Morimoto link->symmetric_rate = 164447108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 1645717a8e72SMengdong Lin 1646717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) 1647717a8e72SMengdong Lin link->symmetric_channels = 164847108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ? 1649717a8e72SMengdong Lin 1 : 0; 1650717a8e72SMengdong Lin 1651717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) 1652f14654ddSKuninori Morimoto link->symmetric_sample_bits = 165347108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 1654717a8e72SMengdong Lin 1 : 0; 16556ff67ccaSMengdong Lin 16566ff67ccaSMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) 16576ff67ccaSMengdong Lin link->ignore_suspend = 165847108a61SPierre-Louis Bossart (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ? 16596ff67ccaSMengdong Lin 1 : 0; 1660717a8e72SMengdong Lin } 1661717a8e72SMengdong Lin 166267d1c21eSGuneshwor Singh /* create the FE DAI link */ 1663ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg, 1664acfc7d46SMengdong Lin struct snd_soc_tplg_pcm *pcm) 1665acfc7d46SMengdong Lin { 1666acfc7d46SMengdong Lin struct snd_soc_dai_link *link; 166723b946ceSKuninori Morimoto struct snd_soc_dai_link_component *dlc; 1668acfc7d46SMengdong Lin int ret; 1669acfc7d46SMengdong Lin 16706a7c51b4SKuninori Morimoto /* link + cpu + codec + platform */ 16716a7c51b4SKuninori Morimoto link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL); 1672acfc7d46SMengdong Lin if (link == NULL) 1673acfc7d46SMengdong Lin return -ENOMEM; 1674acfc7d46SMengdong Lin 167523b946ceSKuninori Morimoto dlc = (struct snd_soc_dai_link_component *)(link + 1); 167623b946ceSKuninori Morimoto 167723b946ceSKuninori Morimoto link->cpus = &dlc[0]; 167823b946ceSKuninori Morimoto link->num_cpus = 1; 167923b946ceSKuninori Morimoto 16808ce1cbd6SJaroslav Kysela link->dobj.index = tplg->index; 16818ce1cbd6SJaroslav Kysela link->dobj.type = SND_SOC_DOBJ_DAI_LINK; 168231e92739SAmadeusz Sławiński if (tplg->ops) 168331e92739SAmadeusz Sławiński link->dobj.unload = tplg->ops->link_unload; 16848ce1cbd6SJaroslav Kysela 16858f27c4abSMengdong Lin if (strlen(pcm->pcm_name)) { 1686ff922622SAmadeusz Sławiński link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1687ff922622SAmadeusz Sławiński link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1688abc3caacSAmadeusz Sławiński if (!link->name || !link->stream_name) { 1689abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1690abc3caacSAmadeusz Sławiński goto err; 1691abc3caacSAmadeusz Sławiński } 16928f27c4abSMengdong Lin } 16935aebe7c7SPierre-Louis Bossart link->id = le32_to_cpu(pcm->pcm_id); 1694acfc7d46SMengdong Lin 1695abc3caacSAmadeusz Sławiński if (strlen(pcm->dai_name)) { 1696ff922622SAmadeusz Sławiński link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1697abc3caacSAmadeusz Sławiński if (!link->cpus->dai_name) { 1698abc3caacSAmadeusz Sławiński ret = -ENOMEM; 1699abc3caacSAmadeusz Sławiński goto err; 1700abc3caacSAmadeusz Sławiński } 1701abc3caacSAmadeusz Sławiński } 17028f27c4abSMengdong Lin 17035a7bec81SKuninori Morimoto /* 17045a7bec81SKuninori Morimoto * Many topology are assuming link has Codec / Platform, and 17055a7bec81SKuninori Morimoto * these might be overwritten at soc_tplg_dai_link_load(). 17065a7bec81SKuninori Morimoto * Don't use &asoc_dummy_dlc here. 17075a7bec81SKuninori Morimoto */ 17085a7bec81SKuninori Morimoto link->codecs = &dlc[1]; /* Don't use &asoc_dummy_dlc here */ 170923b946ceSKuninori Morimoto link->codecs->name = "snd-soc-dummy"; 171023b946ceSKuninori Morimoto link->codecs->dai_name = "snd-soc-dummy-dai"; 17115a7bec81SKuninori Morimoto link->num_codecs = 1; 171267d1c21eSGuneshwor Singh 17135a7bec81SKuninori Morimoto link->platforms = &dlc[2]; /* Don't use &asoc_dummy_dlc here */ 17146a7c51b4SKuninori Morimoto link->platforms->name = "snd-soc-dummy"; 17156a7c51b4SKuninori Morimoto link->num_platforms = 1; 17166a7c51b4SKuninori Morimoto 171767d1c21eSGuneshwor Singh /* enable DPCM */ 171867d1c21eSGuneshwor Singh link->dynamic = 1; 1719c403dcd8SKuninori Morimoto link->ignore_pmdown_time = 1; 17205aebe7c7SPierre-Louis Bossart link->dpcm_playback = le32_to_cpu(pcm->playback); 17215aebe7c7SPierre-Louis Bossart link->dpcm_capture = le32_to_cpu(pcm->capture); 1722717a8e72SMengdong Lin if (pcm->flag_mask) 17235aebe7c7SPierre-Louis Bossart set_link_flags(link, 17245aebe7c7SPierre-Louis Bossart le32_to_cpu(pcm->flag_mask), 17255aebe7c7SPierre-Louis Bossart le32_to_cpu(pcm->flags)); 172667d1c21eSGuneshwor Singh 1727acfc7d46SMengdong Lin /* pass control to component driver for optional further init */ 1728c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, NULL); 1729acfc7d46SMengdong Lin if (ret < 0) { 1730e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: FE link loading failed\n"); 173176d27036SDragos Tarcatu goto err; 173276d27036SDragos Tarcatu } 173376d27036SDragos Tarcatu 1734ffaf886eSKuninori Morimoto ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1); 173576d27036SDragos Tarcatu if (ret < 0) { 1736b6c3bddaSJohan Hovold if (ret != -EPROBE_DEFER) 1737e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: adding FE link failed\n"); 173876d27036SDragos Tarcatu goto err; 1739acfc7d46SMengdong Lin } 1740acfc7d46SMengdong Lin 1741acfc7d46SMengdong Lin list_add(&link->dobj.list, &tplg->comp->dobj_list); 1742acfc7d46SMengdong Lin 1743acfc7d46SMengdong Lin return 0; 174476d27036SDragos Tarcatu err: 174576d27036SDragos Tarcatu return ret; 1746acfc7d46SMengdong Lin } 1747acfc7d46SMengdong Lin 1748acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */ 174964527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg, 175064527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 175164527e8aSMengdong Lin { 1752acfc7d46SMengdong Lin int ret; 1753acfc7d46SMengdong Lin 1754acfc7d46SMengdong Lin ret = soc_tplg_dai_create(tplg, pcm); 1755acfc7d46SMengdong Lin if (ret < 0) 1756acfc7d46SMengdong Lin return ret; 1757acfc7d46SMengdong Lin 1758ab4bc5eeSMengdong Lin return soc_tplg_fe_link_create(tplg, pcm); 175964527e8aSMengdong Lin } 176064527e8aSMengdong Lin 176155726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */ 176255726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, 176355726dc9SMengdong Lin struct snd_soc_tplg_stream_caps_v4 *src) 176455726dc9SMengdong Lin { 17655aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); 176655726dc9SMengdong Lin memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 176755726dc9SMengdong Lin dest->formats = src->formats; 176855726dc9SMengdong Lin dest->rates = src->rates; 176955726dc9SMengdong Lin dest->rate_min = src->rate_min; 177055726dc9SMengdong Lin dest->rate_max = src->rate_max; 177155726dc9SMengdong Lin dest->channels_min = src->channels_min; 177255726dc9SMengdong Lin dest->channels_max = src->channels_max; 177355726dc9SMengdong Lin dest->periods_min = src->periods_min; 177455726dc9SMengdong Lin dest->periods_max = src->periods_max; 177555726dc9SMengdong Lin dest->period_size_min = src->period_size_min; 177655726dc9SMengdong Lin dest->period_size_max = src->period_size_max; 177755726dc9SMengdong Lin dest->buffer_size_min = src->buffer_size_min; 177855726dc9SMengdong Lin dest->buffer_size_max = src->buffer_size_max; 177955726dc9SMengdong Lin } 178055726dc9SMengdong Lin 178155726dc9SMengdong Lin /** 178255726dc9SMengdong Lin * pcm_new_ver - Create the new version of PCM from the old version. 178355726dc9SMengdong Lin * @tplg: topology context 178455726dc9SMengdong Lin * @src: older version of pcm as a source 178555726dc9SMengdong Lin * @pcm: latest version of pcm created from the source 178655726dc9SMengdong Lin * 1787ce1f2571SColin Ian King * Support from version 4. User should free the returned pcm manually. 178855726dc9SMengdong Lin */ 178955726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg, 179055726dc9SMengdong Lin struct snd_soc_tplg_pcm *src, 179155726dc9SMengdong Lin struct snd_soc_tplg_pcm **pcm) 179255726dc9SMengdong Lin { 179355726dc9SMengdong Lin struct snd_soc_tplg_pcm *dest; 179455726dc9SMengdong Lin struct snd_soc_tplg_pcm_v4 *src_v4; 179555726dc9SMengdong Lin int i; 179655726dc9SMengdong Lin 179755726dc9SMengdong Lin *pcm = NULL; 179855726dc9SMengdong Lin 17995aebe7c7SPierre-Louis Bossart if (le32_to_cpu(src->size) != sizeof(*src_v4)) { 180055726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid PCM size\n"); 180155726dc9SMengdong Lin return -EINVAL; 180255726dc9SMengdong Lin } 180355726dc9SMengdong Lin 180455726dc9SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of PCM\n"); 180555726dc9SMengdong Lin src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; 180655726dc9SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 180755726dc9SMengdong Lin if (!dest) 180855726dc9SMengdong Lin return -ENOMEM; 180955726dc9SMengdong Lin 18105aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ 181155726dc9SMengdong Lin memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 181255726dc9SMengdong Lin memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 181355726dc9SMengdong Lin dest->pcm_id = src_v4->pcm_id; 181455726dc9SMengdong Lin dest->dai_id = src_v4->dai_id; 181555726dc9SMengdong Lin dest->playback = src_v4->playback; 181655726dc9SMengdong Lin dest->capture = src_v4->capture; 181755726dc9SMengdong Lin dest->compress = src_v4->compress; 181855726dc9SMengdong Lin dest->num_streams = src_v4->num_streams; 18195aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(dest->num_streams); i++) 182055726dc9SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 182155726dc9SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 182255726dc9SMengdong Lin 182355726dc9SMengdong Lin for (i = 0; i < 2; i++) 182455726dc9SMengdong Lin stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); 182555726dc9SMengdong Lin 182655726dc9SMengdong Lin *pcm = dest; 182755726dc9SMengdong Lin return 0; 182855726dc9SMengdong Lin } 182955726dc9SMengdong Lin 183064527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, 18318a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 18328a978234SLiam Girdwood { 183355726dc9SMengdong Lin struct snd_soc_tplg_pcm *pcm, *_pcm; 18345aebe7c7SPierre-Louis Bossart int count; 18355aebe7c7SPierre-Louis Bossart int size; 1836fd340455SVinod Koul int i; 183755726dc9SMengdong Lin bool abi_match; 1838a3039aefSDragos Tarcatu int ret; 18398a978234SLiam Girdwood 18405aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 18415aebe7c7SPierre-Louis Bossart 184255726dc9SMengdong Lin /* check the element size and count */ 184355726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 18445aebe7c7SPierre-Louis Bossart size = le32_to_cpu(pcm->size); 18455aebe7c7SPierre-Louis Bossart if (size > sizeof(struct snd_soc_tplg_pcm) 18465aebe7c7SPierre-Louis Bossart || size < sizeof(struct snd_soc_tplg_pcm_v4)) { 184755726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", 18485aebe7c7SPierre-Louis Bossart size); 184955726dc9SMengdong Lin return -EINVAL; 185055726dc9SMengdong Lin } 185155726dc9SMengdong Lin 18528a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 18535aebe7c7SPierre-Louis Bossart size, count, 18545aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->payload_size), 18553ce57f22SAmadeusz Sławiński "PCM DAI")) 18568a978234SLiam Girdwood return -EINVAL; 18578a978234SLiam Girdwood 185864527e8aSMengdong Lin for (i = 0; i < count; i++) { 185955726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 18605aebe7c7SPierre-Louis Bossart size = le32_to_cpu(pcm->size); 186155726dc9SMengdong Lin 186255726dc9SMengdong Lin /* check ABI version by size, create a new version of pcm 186355726dc9SMengdong Lin * if abi not match. 186455726dc9SMengdong Lin */ 18655aebe7c7SPierre-Louis Bossart if (size == sizeof(*pcm)) { 186655726dc9SMengdong Lin abi_match = true; 186755726dc9SMengdong Lin _pcm = pcm; 186855726dc9SMengdong Lin } else { 186955726dc9SMengdong Lin abi_match = false; 1870b3677fc3SAmadeusz Sławiński ret = pcm_new_ver(tplg, pcm, &_pcm); 1871b3677fc3SAmadeusz Sławiński if (ret < 0) 1872b3677fc3SAmadeusz Sławiński return ret; 187306eb49f7SMengdong Lin } 187406eb49f7SMengdong Lin 187555726dc9SMengdong Lin /* create the FE DAIs and DAI links */ 1876a3039aefSDragos Tarcatu ret = soc_tplg_pcm_create(tplg, _pcm); 1877a3039aefSDragos Tarcatu if (ret < 0) { 1878a3039aefSDragos Tarcatu if (!abi_match) 1879a3039aefSDragos Tarcatu kfree(_pcm); 1880a3039aefSDragos Tarcatu return ret; 1881a3039aefSDragos Tarcatu } 188255726dc9SMengdong Lin 1883717a8e72SMengdong Lin /* offset by version-specific struct size and 1884717a8e72SMengdong Lin * real priv data size 1885717a8e72SMengdong Lin */ 18865aebe7c7SPierre-Louis Bossart tplg->pos += size + le32_to_cpu(_pcm->priv.size); 1887717a8e72SMengdong Lin 188855726dc9SMengdong Lin if (!abi_match) 188955726dc9SMengdong Lin kfree(_pcm); /* free the duplicated one */ 189064527e8aSMengdong Lin } 189164527e8aSMengdong Lin 18928a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); 18938a978234SLiam Girdwood 18948a978234SLiam Girdwood return 0; 18958a978234SLiam Girdwood } 18968a978234SLiam Girdwood 18970038be9aSMengdong Lin /** 1898593d9e52SMengdong Lin * set_link_hw_format - Set the HW audio format of the physical DAI link. 18998abab35fSCharles Keepax * @link: &snd_soc_dai_link which should be updated 1900593d9e52SMengdong Lin * @cfg: physical link configs. 1901593d9e52SMengdong Lin * 1902593d9e52SMengdong Lin * Topology context contains a list of supported HW formats (configs) and 1903593d9e52SMengdong Lin * a default format ID for the physical link. This function will use this 1904593d9e52SMengdong Lin * default ID to choose the HW format to set the link's DAI format for init. 1905593d9e52SMengdong Lin */ 1906593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link, 1907593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 1908593d9e52SMengdong Lin { 1909593d9e52SMengdong Lin struct snd_soc_tplg_hw_config *hw_config; 1910f026c123SPierre-Louis Bossart unsigned char bclk_provider, fsync_provider; 1911593d9e52SMengdong Lin unsigned char invert_bclk, invert_fsync; 1912593d9e52SMengdong Lin int i; 1913593d9e52SMengdong Lin 19145aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { 1915593d9e52SMengdong Lin hw_config = &cfg->hw_config[i]; 1916593d9e52SMengdong Lin if (hw_config->id != cfg->default_hw_config_id) 1917593d9e52SMengdong Lin continue; 1918593d9e52SMengdong Lin 19195aebe7c7SPierre-Louis Bossart link->dai_fmt = le32_to_cpu(hw_config->fmt) & 19205aebe7c7SPierre-Louis Bossart SND_SOC_DAIFMT_FORMAT_MASK; 1921593d9e52SMengdong Lin 1922933e1c4aSKirill Marinushkin /* clock gating */ 1923fbeabd09SKirill Marinushkin switch (hw_config->clock_gated) { 1924fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_GATED: 1925933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_GATED; 1926fbeabd09SKirill Marinushkin break; 1927fbeabd09SKirill Marinushkin 1928fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_CONT: 1929933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_CONT; 1930fbeabd09SKirill Marinushkin break; 1931fbeabd09SKirill Marinushkin 1932fbeabd09SKirill Marinushkin default: 1933fbeabd09SKirill Marinushkin /* ignore the value */ 1934fbeabd09SKirill Marinushkin break; 1935fbeabd09SKirill Marinushkin } 1936933e1c4aSKirill Marinushkin 1937593d9e52SMengdong Lin /* clock signal polarity */ 1938593d9e52SMengdong Lin invert_bclk = hw_config->invert_bclk; 1939593d9e52SMengdong Lin invert_fsync = hw_config->invert_fsync; 1940593d9e52SMengdong Lin if (!invert_bclk && !invert_fsync) 1941593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; 1942593d9e52SMengdong Lin else if (!invert_bclk && invert_fsync) 1943593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; 1944593d9e52SMengdong Lin else if (invert_bclk && !invert_fsync) 1945593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; 1946593d9e52SMengdong Lin else 1947593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; 1948593d9e52SMengdong Lin 1949593d9e52SMengdong Lin /* clock masters */ 1950f026c123SPierre-Louis Bossart bclk_provider = (hw_config->bclk_provider == 1951f026c123SPierre-Louis Bossart SND_SOC_TPLG_BCLK_CP); 1952f026c123SPierre-Louis Bossart fsync_provider = (hw_config->fsync_provider == 1953f026c123SPierre-Louis Bossart SND_SOC_TPLG_FSYNC_CP); 1954f026c123SPierre-Louis Bossart if (bclk_provider && fsync_provider) 1955f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 1956f026c123SPierre-Louis Bossart else if (!bclk_provider && fsync_provider) 1957f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP; 1958f026c123SPierre-Louis Bossart else if (bclk_provider && !fsync_provider) 1959f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC; 1960593d9e52SMengdong Lin else 1961f026c123SPierre-Louis Bossart link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 1962593d9e52SMengdong Lin } 1963593d9e52SMengdong Lin } 1964593d9e52SMengdong Lin 1965593d9e52SMengdong Lin /** 1966593d9e52SMengdong Lin * link_new_ver - Create a new physical link config from the old 1967593d9e52SMengdong Lin * version of source. 19688abab35fSCharles Keepax * @tplg: topology context 1969593d9e52SMengdong Lin * @src: old version of phyical link config as a source 1970593d9e52SMengdong Lin * @link: latest version of physical link config created from the source 1971593d9e52SMengdong Lin * 1972ce1f2571SColin Ian King * Support from version 4. User need free the returned link config manually. 1973593d9e52SMengdong Lin */ 1974593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg, 1975593d9e52SMengdong Lin struct snd_soc_tplg_link_config *src, 1976593d9e52SMengdong Lin struct snd_soc_tplg_link_config **link) 1977593d9e52SMengdong Lin { 1978593d9e52SMengdong Lin struct snd_soc_tplg_link_config *dest; 1979593d9e52SMengdong Lin struct snd_soc_tplg_link_config_v4 *src_v4; 1980593d9e52SMengdong Lin int i; 1981593d9e52SMengdong Lin 1982593d9e52SMengdong Lin *link = NULL; 1983593d9e52SMengdong Lin 19845aebe7c7SPierre-Louis Bossart if (le32_to_cpu(src->size) != 19855aebe7c7SPierre-Louis Bossart sizeof(struct snd_soc_tplg_link_config_v4)) { 1986593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); 1987593d9e52SMengdong Lin return -EINVAL; 1988593d9e52SMengdong Lin } 1989593d9e52SMengdong Lin 1990593d9e52SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); 1991593d9e52SMengdong Lin 1992593d9e52SMengdong Lin src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; 1993593d9e52SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 1994593d9e52SMengdong Lin if (!dest) 1995593d9e52SMengdong Lin return -ENOMEM; 1996593d9e52SMengdong Lin 19975aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); 1998593d9e52SMengdong Lin dest->id = src_v4->id; 1999593d9e52SMengdong Lin dest->num_streams = src_v4->num_streams; 20005aebe7c7SPierre-Louis Bossart for (i = 0; i < le32_to_cpu(dest->num_streams); i++) 2001593d9e52SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 2002593d9e52SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 2003593d9e52SMengdong Lin 2004593d9e52SMengdong Lin *link = dest; 2005593d9e52SMengdong Lin return 0; 2006593d9e52SMengdong Lin } 2007593d9e52SMengdong Lin 2008d6f31e0eSKuninori Morimoto /** 2009d6f31e0eSKuninori Morimoto * snd_soc_find_dai_link - Find a DAI link 2010d6f31e0eSKuninori Morimoto * 2011d6f31e0eSKuninori Morimoto * @card: soc card 2012d6f31e0eSKuninori Morimoto * @id: DAI link ID to match 2013d6f31e0eSKuninori Morimoto * @name: DAI link name to match, optional 2014d6f31e0eSKuninori Morimoto * @stream_name: DAI link stream name to match, optional 2015d6f31e0eSKuninori Morimoto * 2016d6f31e0eSKuninori Morimoto * This function will search all existing DAI links of the soc card to 2017d6f31e0eSKuninori Morimoto * find the link of the same ID. Since DAI links may not have their 2018d6f31e0eSKuninori Morimoto * unique ID, so name and stream name should also match if being 2019d6f31e0eSKuninori Morimoto * specified. 2020d6f31e0eSKuninori Morimoto * 2021d6f31e0eSKuninori Morimoto * Return: pointer of DAI link, or NULL if not found. 2022d6f31e0eSKuninori Morimoto */ 2023d6f31e0eSKuninori Morimoto static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, 2024d6f31e0eSKuninori Morimoto int id, const char *name, 2025d6f31e0eSKuninori Morimoto const char *stream_name) 2026d6f31e0eSKuninori Morimoto { 2027d6f31e0eSKuninori Morimoto struct snd_soc_pcm_runtime *rtd; 2028d6f31e0eSKuninori Morimoto 2029d6f31e0eSKuninori Morimoto for_each_card_rtds(card, rtd) { 2030b81e8efaSKuninori Morimoto struct snd_soc_dai_link *link = rtd->dai_link; 2031d6f31e0eSKuninori Morimoto 2032d6f31e0eSKuninori Morimoto if (link->id != id) 2033d6f31e0eSKuninori Morimoto continue; 2034d6f31e0eSKuninori Morimoto 2035e018e0b3SRanjani Sridharan if (name && (!link->name || !strstr(link->name, name))) 2036d6f31e0eSKuninori Morimoto continue; 2037d6f31e0eSKuninori Morimoto 2038e018e0b3SRanjani Sridharan if (stream_name && (!link->stream_name || 2039e018e0b3SRanjani Sridharan !strstr(link->stream_name, stream_name))) 2040d6f31e0eSKuninori Morimoto continue; 2041d6f31e0eSKuninori Morimoto 2042d6f31e0eSKuninori Morimoto return link; 2043d6f31e0eSKuninori Morimoto } 2044d6f31e0eSKuninori Morimoto 2045d6f31e0eSKuninori Morimoto return NULL; 2046d6f31e0eSKuninori Morimoto } 2047d6f31e0eSKuninori Morimoto 2048593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */ 2049593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg, 2050593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 2051593d9e52SMengdong Lin { 2052593d9e52SMengdong Lin struct snd_soc_dai_link *link; 2053593d9e52SMengdong Lin const char *name, *stream_name; 2054dbab1cb8SMengdong Lin size_t len; 2055593d9e52SMengdong Lin int ret; 2056593d9e52SMengdong Lin 2057dbab1cb8SMengdong Lin len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2058dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2059dbab1cb8SMengdong Lin return -EINVAL; 2060dbab1cb8SMengdong Lin else if (len) 2061dbab1cb8SMengdong Lin name = cfg->name; 2062dbab1cb8SMengdong Lin else 2063dbab1cb8SMengdong Lin name = NULL; 2064dbab1cb8SMengdong Lin 2065dbab1cb8SMengdong Lin len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2066dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2067dbab1cb8SMengdong Lin return -EINVAL; 2068dbab1cb8SMengdong Lin else if (len) 2069dbab1cb8SMengdong Lin stream_name = cfg->stream_name; 2070dbab1cb8SMengdong Lin else 2071dbab1cb8SMengdong Lin stream_name = NULL; 2072593d9e52SMengdong Lin 20735aebe7c7SPierre-Louis Bossart link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), 2074593d9e52SMengdong Lin name, stream_name); 2075593d9e52SMengdong Lin if (!link) { 2076593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", 2077593d9e52SMengdong Lin name, cfg->id); 2078593d9e52SMengdong Lin return -EINVAL; 2079593d9e52SMengdong Lin } 2080593d9e52SMengdong Lin 2081593d9e52SMengdong Lin /* hw format */ 2082593d9e52SMengdong Lin if (cfg->num_hw_configs) 2083593d9e52SMengdong Lin set_link_hw_format(link, cfg); 2084593d9e52SMengdong Lin 2085593d9e52SMengdong Lin /* flags */ 2086593d9e52SMengdong Lin if (cfg->flag_mask) 20875aebe7c7SPierre-Louis Bossart set_link_flags(link, 20885aebe7c7SPierre-Louis Bossart le32_to_cpu(cfg->flag_mask), 20895aebe7c7SPierre-Louis Bossart le32_to_cpu(cfg->flags)); 2090593d9e52SMengdong Lin 2091593d9e52SMengdong Lin /* pass control to component driver for optional further init */ 2092c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, cfg); 2093593d9e52SMengdong Lin if (ret < 0) { 2094593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link loading failed\n"); 2095593d9e52SMengdong Lin return ret; 2096593d9e52SMengdong Lin } 2097593d9e52SMengdong Lin 2098adfebb51SBard liao /* for unloading it in snd_soc_tplg_component_remove */ 2099adfebb51SBard liao link->dobj.index = tplg->index; 2100adfebb51SBard liao link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; 210131e92739SAmadeusz Sławiński if (tplg->ops) 210231e92739SAmadeusz Sławiński link->dobj.unload = tplg->ops->link_unload; 2103adfebb51SBard liao list_add(&link->dobj.list, &tplg->comp->dobj_list); 2104adfebb51SBard liao 2105593d9e52SMengdong Lin return 0; 2106593d9e52SMengdong Lin } 2107593d9e52SMengdong Lin 2108593d9e52SMengdong Lin 2109593d9e52SMengdong Lin /* Load physical link config elements from the topology context */ 2110593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg, 2111593d9e52SMengdong Lin struct snd_soc_tplg_hdr *hdr) 2112593d9e52SMengdong Lin { 2113593d9e52SMengdong Lin struct snd_soc_tplg_link_config *link, *_link; 21145aebe7c7SPierre-Louis Bossart int count; 21155aebe7c7SPierre-Louis Bossart int size; 2116593d9e52SMengdong Lin int i, ret; 2117593d9e52SMengdong Lin bool abi_match; 2118593d9e52SMengdong Lin 21195aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 21205aebe7c7SPierre-Louis Bossart 2121593d9e52SMengdong Lin /* check the element size and count */ 2122593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 21235aebe7c7SPierre-Louis Bossart size = le32_to_cpu(link->size); 21245aebe7c7SPierre-Louis Bossart if (size > sizeof(struct snd_soc_tplg_link_config) 21255aebe7c7SPierre-Louis Bossart || size < sizeof(struct snd_soc_tplg_link_config_v4)) { 2126593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", 21275aebe7c7SPierre-Louis Bossart size); 2128593d9e52SMengdong Lin return -EINVAL; 2129593d9e52SMengdong Lin } 2130593d9e52SMengdong Lin 21313ce57f22SAmadeusz Sławiński if (soc_tplg_check_elem_count(tplg, size, count, 21325aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->payload_size), 21333ce57f22SAmadeusz Sławiński "physical link config")) 2134593d9e52SMengdong Lin return -EINVAL; 2135593d9e52SMengdong Lin 2136593d9e52SMengdong Lin /* config physical DAI links */ 2137593d9e52SMengdong Lin for (i = 0; i < count; i++) { 2138593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 21395aebe7c7SPierre-Louis Bossart size = le32_to_cpu(link->size); 21405aebe7c7SPierre-Louis Bossart if (size == sizeof(*link)) { 2141593d9e52SMengdong Lin abi_match = true; 2142593d9e52SMengdong Lin _link = link; 2143593d9e52SMengdong Lin } else { 2144593d9e52SMengdong Lin abi_match = false; 2145593d9e52SMengdong Lin ret = link_new_ver(tplg, link, &_link); 2146593d9e52SMengdong Lin if (ret < 0) 2147593d9e52SMengdong Lin return ret; 2148593d9e52SMengdong Lin } 2149593d9e52SMengdong Lin 2150593d9e52SMengdong Lin ret = soc_tplg_link_config(tplg, _link); 21512b2d5c4dSDragos Tarcatu if (ret < 0) { 21522b2d5c4dSDragos Tarcatu if (!abi_match) 21532b2d5c4dSDragos Tarcatu kfree(_link); 2154593d9e52SMengdong Lin return ret; 21552b2d5c4dSDragos Tarcatu } 2156593d9e52SMengdong Lin 2157593d9e52SMengdong Lin /* offset by version-specific struct size and 2158593d9e52SMengdong Lin * real priv data size 2159593d9e52SMengdong Lin */ 21605aebe7c7SPierre-Louis Bossart tplg->pos += size + le32_to_cpu(_link->priv.size); 2161593d9e52SMengdong Lin 2162593d9e52SMengdong Lin if (!abi_match) 2163593d9e52SMengdong Lin kfree(_link); /* free the duplicated one */ 2164593d9e52SMengdong Lin } 2165593d9e52SMengdong Lin 2166593d9e52SMengdong Lin return 0; 2167593d9e52SMengdong Lin } 2168593d9e52SMengdong Lin 2169593d9e52SMengdong Lin /** 21709aa3f034SMengdong Lin * soc_tplg_dai_config - Find and configure an existing physical DAI. 21710038be9aSMengdong Lin * @tplg: topology context 21729aa3f034SMengdong Lin * @d: physical DAI configs. 21730038be9aSMengdong Lin * 21749aa3f034SMengdong Lin * The physical dai should already be registered by the platform driver. 21759aa3f034SMengdong Lin * The platform driver should specify the DAI name and ID for matching. 21760038be9aSMengdong Lin */ 21779aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg, 21789aa3f034SMengdong Lin struct snd_soc_tplg_dai *d) 21790038be9aSMengdong Lin { 21805aebe7c7SPierre-Louis Bossart struct snd_soc_dai_link_component dai_component; 21810038be9aSMengdong Lin struct snd_soc_dai *dai; 21820038be9aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 21830038be9aSMengdong Lin struct snd_soc_pcm_stream *stream; 21840038be9aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 21850038be9aSMengdong Lin int ret; 21860038be9aSMengdong Lin 21875aebe7c7SPierre-Louis Bossart memset(&dai_component, 0, sizeof(dai_component)); 21885aebe7c7SPierre-Louis Bossart 21899aa3f034SMengdong Lin dai_component.dai_name = d->dai_name; 21900038be9aSMengdong Lin dai = snd_soc_find_dai(&dai_component); 21910038be9aSMengdong Lin if (!dai) { 21929aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", 21939aa3f034SMengdong Lin d->dai_name); 21940038be9aSMengdong Lin return -EINVAL; 21950038be9aSMengdong Lin } 21960038be9aSMengdong Lin 21975aebe7c7SPierre-Louis Bossart if (le32_to_cpu(d->dai_id) != dai->id) { 21989aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", 21999aa3f034SMengdong Lin d->dai_name); 22000038be9aSMengdong Lin return -EINVAL; 22010038be9aSMengdong Lin } 22020038be9aSMengdong Lin 22030038be9aSMengdong Lin dai_drv = dai->driver; 22040038be9aSMengdong Lin if (!dai_drv) 22050038be9aSMengdong Lin return -EINVAL; 22060038be9aSMengdong Lin 22079aa3f034SMengdong Lin if (d->playback) { 22080038be9aSMengdong Lin stream = &dai_drv->playback; 22099aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 2210ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 2211abc3caacSAmadeusz Sławiński if (ret < 0) 2212abc3caacSAmadeusz Sławiński goto err; 22130038be9aSMengdong Lin } 22140038be9aSMengdong Lin 22159aa3f034SMengdong Lin if (d->capture) { 22160038be9aSMengdong Lin stream = &dai_drv->capture; 22179aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 2218ff922622SAmadeusz Sławiński ret = set_stream_info(tplg, stream, caps); 2219abc3caacSAmadeusz Sławiński if (ret < 0) 2220abc3caacSAmadeusz Sławiński goto err; 22210038be9aSMengdong Lin } 22220038be9aSMengdong Lin 22239aa3f034SMengdong Lin if (d->flag_mask) 22245aebe7c7SPierre-Louis Bossart set_dai_flags(dai_drv, 22255aebe7c7SPierre-Louis Bossart le32_to_cpu(d->flag_mask), 22265aebe7c7SPierre-Louis Bossart le32_to_cpu(d->flags)); 22270038be9aSMengdong Lin 22280038be9aSMengdong Lin /* pass control to component driver for optional further init */ 2229c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); 22300038be9aSMengdong Lin if (ret < 0) { 2231e59db12bSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 2232abc3caacSAmadeusz Sławiński goto err; 22330038be9aSMengdong Lin } 22340038be9aSMengdong Lin 22350038be9aSMengdong Lin return 0; 2236abc3caacSAmadeusz Sławiński 2237abc3caacSAmadeusz Sławiński err: 2238abc3caacSAmadeusz Sławiński return ret; 22390038be9aSMengdong Lin } 22400038be9aSMengdong Lin 22419aa3f034SMengdong Lin /* load physical DAI elements */ 22429aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, 22430038be9aSMengdong Lin struct snd_soc_tplg_hdr *hdr) 22440038be9aSMengdong Lin { 22455aebe7c7SPierre-Louis Bossart int count; 224665a4cfddSKuninori Morimoto int i; 22470038be9aSMengdong Lin 22485aebe7c7SPierre-Louis Bossart count = le32_to_cpu(hdr->count); 22495aebe7c7SPierre-Louis Bossart 22500038be9aSMengdong Lin /* config the existing BE DAIs */ 22510038be9aSMengdong Lin for (i = 0; i < count; i++) { 225265a4cfddSKuninori Morimoto struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos; 225365a4cfddSKuninori Morimoto int ret; 225465a4cfddSKuninori Morimoto 22555aebe7c7SPierre-Louis Bossart if (le32_to_cpu(dai->size) != sizeof(*dai)) { 22569aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); 22570038be9aSMengdong Lin return -EINVAL; 22580038be9aSMengdong Lin } 22590038be9aSMengdong Lin 2260dd8e871dSAmadeusz Sławiński ret = soc_tplg_dai_config(tplg, dai); 2261dd8e871dSAmadeusz Sławiński if (ret < 0) { 2262dd8e871dSAmadeusz Sławiński dev_err(tplg->dev, "ASoC: failed to configure DAI\n"); 2263dd8e871dSAmadeusz Sławiński return ret; 2264dd8e871dSAmadeusz Sławiński } 2265dd8e871dSAmadeusz Sławiński 22665aebe7c7SPierre-Louis Bossart tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); 22670038be9aSMengdong Lin } 22680038be9aSMengdong Lin 22690038be9aSMengdong Lin dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); 22700038be9aSMengdong Lin return 0; 22710038be9aSMengdong Lin } 22720038be9aSMengdong Lin 2273583958faSMengdong Lin /** 2274583958faSMengdong Lin * manifest_new_ver - Create a new version of manifest from the old version 2275583958faSMengdong Lin * of source. 22768abab35fSCharles Keepax * @tplg: topology context 2277583958faSMengdong Lin * @src: old version of manifest as a source 2278583958faSMengdong Lin * @manifest: latest version of manifest created from the source 2279583958faSMengdong Lin * 2280ce1f2571SColin Ian King * Support from version 4. Users need free the returned manifest manually. 2281583958faSMengdong Lin */ 2282583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg, 2283583958faSMengdong Lin struct snd_soc_tplg_manifest *src, 2284583958faSMengdong Lin struct snd_soc_tplg_manifest **manifest) 2285583958faSMengdong Lin { 2286583958faSMengdong Lin struct snd_soc_tplg_manifest *dest; 2287583958faSMengdong Lin struct snd_soc_tplg_manifest_v4 *src_v4; 22885aebe7c7SPierre-Louis Bossart int size; 2289583958faSMengdong Lin 2290583958faSMengdong Lin *manifest = NULL; 2291583958faSMengdong Lin 22925aebe7c7SPierre-Louis Bossart size = le32_to_cpu(src->size); 22935aebe7c7SPierre-Louis Bossart if (size != sizeof(*src_v4)) { 2294ac9391daSGuenter Roeck dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", 22955aebe7c7SPierre-Louis Bossart size); 22965aebe7c7SPierre-Louis Bossart if (size) 2297583958faSMengdong Lin return -EINVAL; 22985aebe7c7SPierre-Louis Bossart src->size = cpu_to_le32(sizeof(*src_v4)); 2299583958faSMengdong Lin } 2300583958faSMengdong Lin 2301583958faSMengdong Lin dev_warn(tplg->dev, "ASoC: old version of manifest\n"); 2302583958faSMengdong Lin 2303583958faSMengdong Lin src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; 23045aebe7c7SPierre-Louis Bossart dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size), 23055aebe7c7SPierre-Louis Bossart GFP_KERNEL); 2306583958faSMengdong Lin if (!dest) 2307583958faSMengdong Lin return -ENOMEM; 2308583958faSMengdong Lin 23095aebe7c7SPierre-Louis Bossart dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ 2310583958faSMengdong Lin dest->control_elems = src_v4->control_elems; 2311583958faSMengdong Lin dest->widget_elems = src_v4->widget_elems; 2312583958faSMengdong Lin dest->graph_elems = src_v4->graph_elems; 2313583958faSMengdong Lin dest->pcm_elems = src_v4->pcm_elems; 2314583958faSMengdong Lin dest->dai_link_elems = src_v4->dai_link_elems; 2315583958faSMengdong Lin dest->priv.size = src_v4->priv.size; 2316583958faSMengdong Lin if (dest->priv.size) 2317583958faSMengdong Lin memcpy(dest->priv.data, src_v4->priv.data, 23185aebe7c7SPierre-Louis Bossart le32_to_cpu(src_v4->priv.size)); 2319583958faSMengdong Lin 2320583958faSMengdong Lin *manifest = dest; 2321583958faSMengdong Lin return 0; 2322583958faSMengdong Lin } 23230038be9aSMengdong Lin 23248a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg, 23258a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 23268a978234SLiam Girdwood { 2327583958faSMengdong Lin struct snd_soc_tplg_manifest *manifest, *_manifest; 2328583958faSMengdong Lin bool abi_match; 2329242c46c0SDragos Tarcatu int ret = 0; 23308a978234SLiam Girdwood 23318a978234SLiam Girdwood manifest = (struct snd_soc_tplg_manifest *)tplg->pos; 2332583958faSMengdong Lin 2333583958faSMengdong Lin /* check ABI version by size, create a new manifest if abi not match */ 23345aebe7c7SPierre-Louis Bossart if (le32_to_cpu(manifest->size) == sizeof(*manifest)) { 2335583958faSMengdong Lin abi_match = true; 2336583958faSMengdong Lin _manifest = manifest; 2337583958faSMengdong Lin } else { 2338583958faSMengdong Lin abi_match = false; 233986e2d14bSCezary Rojewski 2340242c46c0SDragos Tarcatu ret = manifest_new_ver(tplg, manifest, &_manifest); 2341242c46c0SDragos Tarcatu if (ret < 0) 2342242c46c0SDragos Tarcatu return ret; 234306eb49f7SMengdong Lin } 234406eb49f7SMengdong Lin 2345583958faSMengdong Lin /* pass control to component driver for optional further init */ 2346c42464a4SAmadeusz Sławiński if (tplg->ops && tplg->ops->manifest) 2347242c46c0SDragos Tarcatu ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest); 23488a978234SLiam Girdwood 2349583958faSMengdong Lin if (!abi_match) /* free the duplicated one */ 2350583958faSMengdong Lin kfree(_manifest); 2351583958faSMengdong Lin 2352242c46c0SDragos Tarcatu return ret; 23538a978234SLiam Girdwood } 23548a978234SLiam Girdwood 23558a978234SLiam Girdwood /* validate header magic, size and type */ 235623e591dcSAmadeusz Sławiński static int soc_tplg_valid_header(struct soc_tplg *tplg, 23578a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 23588a978234SLiam Girdwood { 23595aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { 236006eb49f7SMengdong Lin dev_err(tplg->dev, 236106eb49f7SMengdong Lin "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", 23625aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 236306eb49f7SMengdong Lin tplg->fw->size); 236406eb49f7SMengdong Lin return -EINVAL; 236506eb49f7SMengdong Lin } 236606eb49f7SMengdong Lin 2367c5d184c9SAmadeusz Sławiński if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) { 236886e2d14bSCezary Rojewski dev_err(tplg->dev, 236986e2d14bSCezary Rojewski "ASoC: invalid header of type %d at offset %ld payload_size %d\n", 237086e2d14bSCezary Rojewski le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 237186e2d14bSCezary Rojewski hdr->payload_size); 237286e2d14bSCezary Rojewski return -EINVAL; 237386e2d14bSCezary Rojewski } 237486e2d14bSCezary Rojewski 23758a978234SLiam Girdwood /* big endian firmware objects not supported atm */ 237626d87881SAmadeusz Sławiński if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) { 23778a978234SLiam Girdwood dev_err(tplg->dev, 23788a978234SLiam Girdwood "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", 23798a978234SLiam Girdwood tplg->pass, hdr->magic, 23808a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 23818a978234SLiam Girdwood return -EINVAL; 23828a978234SLiam Girdwood } 23838a978234SLiam Girdwood 23845aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { 23858a978234SLiam Girdwood dev_err(tplg->dev, 23868a978234SLiam Girdwood "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", 23878a978234SLiam Girdwood tplg->pass, hdr->magic, 23888a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 23898a978234SLiam Girdwood return -EINVAL; 23908a978234SLiam Girdwood } 23918a978234SLiam Girdwood 2392288b8da7SMengdong Lin /* Support ABI from version 4 */ 23935aebe7c7SPierre-Louis Bossart if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || 23945aebe7c7SPierre-Louis Bossart le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { 23958a978234SLiam Girdwood dev_err(tplg->dev, 23968a978234SLiam Girdwood "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", 23978a978234SLiam Girdwood tplg->pass, hdr->abi, 23988a978234SLiam Girdwood SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), 23998a978234SLiam Girdwood tplg->fw->size); 24008a978234SLiam Girdwood return -EINVAL; 24018a978234SLiam Girdwood } 24028a978234SLiam Girdwood 24038a978234SLiam Girdwood if (hdr->payload_size == 0) { 24048a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", 24058a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg)); 24068a978234SLiam Girdwood return -EINVAL; 24078a978234SLiam Girdwood } 24088a978234SLiam Girdwood 2409d9b07b79SAmadeusz Sławiński return 0; 24108a978234SLiam Girdwood } 24118a978234SLiam Girdwood 24128a978234SLiam Girdwood /* check header type and call appropriate handler */ 24138a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg, 24148a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 24158a978234SLiam Girdwood { 241682ed7418SKeyon Jie int (*elem_load)(struct soc_tplg *tplg, 241782ed7418SKeyon Jie struct snd_soc_tplg_hdr *hdr); 241882ed7418SKeyon Jie unsigned int hdr_pass; 241982ed7418SKeyon Jie 24208a978234SLiam Girdwood tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); 24218a978234SLiam Girdwood 24225aebe7c7SPierre-Louis Bossart tplg->index = le32_to_cpu(hdr->index); 24238a978234SLiam Girdwood 24245aebe7c7SPierre-Louis Bossart switch (le32_to_cpu(hdr->type)) { 24258a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MIXER: 24268a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_ENUM: 24278a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_BYTES: 24285e2cd47aSAmadeusz Sławiński hdr_pass = SOC_TPLG_PASS_CONTROL; 242982ed7418SKeyon Jie elem_load = soc_tplg_kcontrol_elems_load; 243082ed7418SKeyon Jie break; 24318a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_GRAPH: 243282ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_GRAPH; 243382ed7418SKeyon Jie elem_load = soc_tplg_dapm_graph_elems_load; 243482ed7418SKeyon Jie break; 24358a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_WIDGET: 243682ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_WIDGET; 243782ed7418SKeyon Jie elem_load = soc_tplg_dapm_widget_elems_load; 243882ed7418SKeyon Jie break; 24398a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_PCM: 244082ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_PCM_DAI; 244182ed7418SKeyon Jie elem_load = soc_tplg_pcm_elems_load; 244282ed7418SKeyon Jie break; 24433fbf7935SMengdong Lin case SND_SOC_TPLG_TYPE_DAI: 244482ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_BE_DAI; 244582ed7418SKeyon Jie elem_load = soc_tplg_dai_elems_load; 244682ed7418SKeyon Jie break; 2447593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_DAI_LINK: 2448593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_BACKEND_LINK: 2449593d9e52SMengdong Lin /* physical link configurations */ 245082ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_LINK; 245182ed7418SKeyon Jie elem_load = soc_tplg_link_elems_load; 245282ed7418SKeyon Jie break; 24538a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MANIFEST: 245482ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_MANIFEST; 245582ed7418SKeyon Jie elem_load = soc_tplg_manifest_load; 245682ed7418SKeyon Jie break; 24578a978234SLiam Girdwood default: 24588a978234SLiam Girdwood /* bespoke vendor data object */ 245982ed7418SKeyon Jie hdr_pass = SOC_TPLG_PASS_VENDOR; 246082ed7418SKeyon Jie elem_load = soc_tplg_vendor_load; 246182ed7418SKeyon Jie break; 246282ed7418SKeyon Jie } 246382ed7418SKeyon Jie 246482ed7418SKeyon Jie if (tplg->pass == hdr_pass) { 246582ed7418SKeyon Jie dev_dbg(tplg->dev, 246682ed7418SKeyon Jie "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", 246782ed7418SKeyon Jie hdr->payload_size, hdr->type, hdr->version, 246882ed7418SKeyon Jie hdr->vendor_type, tplg->pass); 246982ed7418SKeyon Jie return elem_load(tplg, hdr); 24708a978234SLiam Girdwood } 24718a978234SLiam Girdwood 24728a978234SLiam Girdwood return 0; 24738a978234SLiam Girdwood } 24748a978234SLiam Girdwood 24758a978234SLiam Girdwood /* process the topology file headers */ 24768a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg) 24778a978234SLiam Girdwood { 24788a978234SLiam Girdwood int ret; 24798a978234SLiam Girdwood 24808a978234SLiam Girdwood /* process the header types from start to end */ 2481395f8fd6SAmadeusz Sławiński for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) { 2482f79e4b2aSKuninori Morimoto struct snd_soc_tplg_hdr *hdr; 24838a978234SLiam Girdwood 24848a978234SLiam Girdwood tplg->hdr_pos = tplg->fw->data; 24858a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 24868a978234SLiam Girdwood 24878a978234SLiam Girdwood while (!soc_tplg_is_eof(tplg)) { 24888a978234SLiam Girdwood 24898a978234SLiam Girdwood /* make sure header is valid before loading */ 249023e591dcSAmadeusz Sławiński ret = soc_tplg_valid_header(tplg, hdr); 2491f9d1fe7eSAmadeusz Sławiński if (ret < 0) 24928a978234SLiam Girdwood return ret; 24938a978234SLiam Girdwood 24948a978234SLiam Girdwood /* load the header object */ 24958a978234SLiam Girdwood ret = soc_tplg_load_header(tplg, hdr); 24968bf9475fSPierre-Louis Bossart if (ret < 0) { 2497b6c3bddaSJohan Hovold if (ret != -EPROBE_DEFER) { 24988bf9475fSPierre-Louis Bossart dev_err(tplg->dev, 2499b6c3bddaSJohan Hovold "ASoC: topology: could not load header: %d\n", 2500b6c3bddaSJohan Hovold ret); 2501b6c3bddaSJohan Hovold } 25028a978234SLiam Girdwood return ret; 25038bf9475fSPierre-Louis Bossart } 25048a978234SLiam Girdwood 25058a978234SLiam Girdwood /* goto next header */ 25065aebe7c7SPierre-Louis Bossart tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + 25078a978234SLiam Girdwood sizeof(struct snd_soc_tplg_hdr); 25088a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 25098a978234SLiam Girdwood } 25108a978234SLiam Girdwood 25118a978234SLiam Girdwood } 25128a978234SLiam Girdwood 25138a978234SLiam Girdwood /* signal DAPM we are complete */ 25148a978234SLiam Girdwood ret = soc_tplg_dapm_complete(tplg); 25158a978234SLiam Girdwood 25168a978234SLiam Girdwood return ret; 25178a978234SLiam Girdwood } 25188a978234SLiam Girdwood 25198a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg) 25208a978234SLiam Girdwood { 25218a978234SLiam Girdwood int ret; 25228a978234SLiam Girdwood 25238a978234SLiam Girdwood ret = soc_tplg_process_headers(tplg); 25248a978234SLiam Girdwood if (ret == 0) 2525415717e1SRanjani Sridharan return soc_tplg_complete(tplg); 25268a978234SLiam Girdwood 25278a978234SLiam Girdwood return ret; 25288a978234SLiam Girdwood } 25298a978234SLiam Girdwood 25308a978234SLiam Girdwood /* load audio component topology from "firmware" file */ 25318a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp, 2532a5b8f71cSAmadeusz Sławiński struct snd_soc_tplg_ops *ops, const struct firmware *fw) 25338a978234SLiam Girdwood { 25348a978234SLiam Girdwood struct soc_tplg tplg; 2535304017d3SBard liao int ret; 25368a978234SLiam Girdwood 2537d40ab86fSAmadeusz Sławiński /* 2538d40ab86fSAmadeusz Sławiński * check if we have sane parameters: 2539d40ab86fSAmadeusz Sławiński * comp - needs to exist to keep and reference data while parsing 2540d40ab86fSAmadeusz Sławiński * comp->card - used for setting card related parameters 2541f714fbc1SAmadeusz Sławiński * comp->card->dev - used for resource management and prints 2542d40ab86fSAmadeusz Sławiński * fw - we need it, as it is the very thing we parse 2543d40ab86fSAmadeusz Sławiński */ 2544f714fbc1SAmadeusz Sławiński if (!comp || !comp->card || !comp->card->dev || !fw) 2545c42464a4SAmadeusz Sławiński return -EINVAL; 2546c42464a4SAmadeusz Sławiński 25478a978234SLiam Girdwood /* setup parsing context */ 25488a978234SLiam Girdwood memset(&tplg, 0, sizeof(tplg)); 25498a978234SLiam Girdwood tplg.fw = fw; 2550f714fbc1SAmadeusz Sławiński tplg.dev = comp->card->dev; 25518a978234SLiam Girdwood tplg.comp = comp; 25529c88a983SAmadeusz Sławiński if (ops) { 25538a978234SLiam Girdwood tplg.ops = ops; 25548a978234SLiam Girdwood tplg.io_ops = ops->io_ops; 25558a978234SLiam Girdwood tplg.io_ops_count = ops->io_ops_count; 25561a3232d2SMengdong Lin tplg.bytes_ext_ops = ops->bytes_ext_ops; 25571a3232d2SMengdong Lin tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; 25589c88a983SAmadeusz Sławiński } 25598a978234SLiam Girdwood 2560304017d3SBard liao ret = soc_tplg_load(&tplg); 2561304017d3SBard liao /* free the created components if fail to load topology */ 2562304017d3SBard liao if (ret) 2563a5b8f71cSAmadeusz Sławiński snd_soc_tplg_component_remove(comp); 2564304017d3SBard liao 2565304017d3SBard liao return ret; 25668a978234SLiam Girdwood } 25678a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); 25688a978234SLiam Girdwood 25698a978234SLiam Girdwood /* remove dynamic controls from the component driver */ 2570a5b8f71cSAmadeusz Sławiński int snd_soc_tplg_component_remove(struct snd_soc_component *comp) 25718a978234SLiam Girdwood { 25728a978234SLiam Girdwood struct snd_soc_dobj *dobj, *next_dobj; 2573395f8fd6SAmadeusz Sławiński int pass; 25748a978234SLiam Girdwood 25758a978234SLiam Girdwood /* process the header types from end to start */ 2576395f8fd6SAmadeusz Sławiński for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) { 25778a978234SLiam Girdwood 25788a978234SLiam Girdwood /* remove mixer controls */ 25798a978234SLiam Girdwood list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, 25808a978234SLiam Girdwood list) { 25818a978234SLiam Girdwood 25828a978234SLiam Girdwood switch (dobj->type) { 25838a978234SLiam Girdwood case SND_SOC_DOBJ_BYTES: 2584fdfa3661SAmadeusz Sławiński case SND_SOC_DOBJ_ENUM: 2585fdfa3661SAmadeusz Sławiński case SND_SOC_DOBJ_MIXER: 2586fdfa3661SAmadeusz Sławiński soc_tplg_remove_kcontrol(comp, dobj, pass); 25878a978234SLiam Girdwood break; 25887df04ea7SRanjani Sridharan case SND_SOC_DOBJ_GRAPH: 25892abfd4bdSAmadeusz Sławiński soc_tplg_remove_route(comp, dobj, pass); 25907df04ea7SRanjani Sridharan break; 25918a978234SLiam Girdwood case SND_SOC_DOBJ_WIDGET: 25922abfd4bdSAmadeusz Sławiński soc_tplg_remove_widget(comp, dobj, pass); 25938a978234SLiam Girdwood break; 25948a978234SLiam Girdwood case SND_SOC_DOBJ_PCM: 25952abfd4bdSAmadeusz Sławiński soc_tplg_remove_dai(comp, dobj, pass); 25968a978234SLiam Girdwood break; 2597acfc7d46SMengdong Lin case SND_SOC_DOBJ_DAI_LINK: 25982abfd4bdSAmadeusz Sławiński soc_tplg_remove_link(comp, dobj, pass); 2599acfc7d46SMengdong Lin break; 2600adfebb51SBard liao case SND_SOC_DOBJ_BACKEND_LINK: 2601adfebb51SBard liao /* 2602adfebb51SBard liao * call link_unload ops if extra 2603adfebb51SBard liao * deinitialization is needed. 2604adfebb51SBard liao */ 2605adfebb51SBard liao remove_backend_link(comp, dobj, pass); 2606adfebb51SBard liao break; 26078a978234SLiam Girdwood default: 26088a978234SLiam Girdwood dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", 26098a978234SLiam Girdwood dobj->type); 26108a978234SLiam Girdwood break; 26118a978234SLiam Girdwood } 26128a978234SLiam Girdwood } 26138a978234SLiam Girdwood } 26148a978234SLiam Girdwood 26158a978234SLiam Girdwood /* let caller know if FW can be freed when no objects are left */ 26168a978234SLiam Girdwood return !list_empty(&comp->dobj_list); 26178a978234SLiam Girdwood } 26188a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); 2619