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 338a978234SLiam Girdwood /* 348a978234SLiam Girdwood * We make several passes over the data (since it wont necessarily be ordered) 358a978234SLiam Girdwood * and process objects in the following order. This guarantees the component 368a978234SLiam Girdwood * drivers will be ready with any vendor data before the mixers and DAPM objects 378a978234SLiam Girdwood * are loaded (that may make use of the vendor data). 388a978234SLiam Girdwood */ 398a978234SLiam Girdwood #define SOC_TPLG_PASS_MANIFEST 0 408a978234SLiam Girdwood #define SOC_TPLG_PASS_VENDOR 1 418a978234SLiam Girdwood #define SOC_TPLG_PASS_MIXER 2 428a978234SLiam Girdwood #define SOC_TPLG_PASS_WIDGET 3 431a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PCM_DAI 4 441a8e7fabSMengdong Lin #define SOC_TPLG_PASS_GRAPH 5 451a8e7fabSMengdong Lin #define SOC_TPLG_PASS_PINS 6 460038be9aSMengdong Lin #define SOC_TPLG_PASS_BE_DAI 7 47593d9e52SMengdong Lin #define SOC_TPLG_PASS_LINK 8 488a978234SLiam Girdwood 498a978234SLiam Girdwood #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST 50593d9e52SMengdong Lin #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK 518a978234SLiam Girdwood 52583958faSMengdong Lin /* topology context */ 538a978234SLiam Girdwood struct soc_tplg { 548a978234SLiam Girdwood const struct firmware *fw; 558a978234SLiam Girdwood 568a978234SLiam Girdwood /* runtime FW parsing */ 578a978234SLiam Girdwood const u8 *pos; /* read postion */ 588a978234SLiam Girdwood const u8 *hdr_pos; /* header position */ 598a978234SLiam Girdwood unsigned int pass; /* pass number */ 608a978234SLiam Girdwood 618a978234SLiam Girdwood /* component caller */ 628a978234SLiam Girdwood struct device *dev; 638a978234SLiam Girdwood struct snd_soc_component *comp; 648a978234SLiam Girdwood u32 index; /* current block index */ 658a978234SLiam Girdwood u32 req_index; /* required index, only loaded/free matching blocks */ 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 static int soc_tplg_process_headers(struct soc_tplg *tplg); 808a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg); 818a978234SLiam Girdwood struct snd_soc_dapm_widget * 828a978234SLiam Girdwood snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, 838a978234SLiam Girdwood const struct snd_soc_dapm_widget *widget); 848a978234SLiam Girdwood struct snd_soc_dapm_widget * 858a978234SLiam Girdwood snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, 868a978234SLiam Girdwood const struct snd_soc_dapm_widget *widget); 878a978234SLiam Girdwood 888a978234SLiam Girdwood /* check we dont overflow the data for this control chunk */ 898a978234SLiam Girdwood static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, 908a978234SLiam Girdwood unsigned int count, size_t bytes, const char *elem_type) 918a978234SLiam Girdwood { 928a978234SLiam Girdwood const u8 *end = tplg->pos + elem_size * count; 938a978234SLiam Girdwood 948a978234SLiam Girdwood if (end > tplg->fw->data + tplg->fw->size) { 958a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: %s overflow end of data\n", 968a978234SLiam Girdwood elem_type); 978a978234SLiam Girdwood return -EINVAL; 988a978234SLiam Girdwood } 998a978234SLiam Girdwood 1008a978234SLiam Girdwood /* check there is enough room in chunk for control. 1018a978234SLiam Girdwood extra bytes at the end of control are for vendor data here */ 1028a978234SLiam Girdwood if (elem_size * count > bytes) { 1038a978234SLiam Girdwood dev_err(tplg->dev, 1048a978234SLiam Girdwood "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", 1058a978234SLiam Girdwood elem_type, count, elem_size, bytes); 1068a978234SLiam Girdwood return -EINVAL; 1078a978234SLiam Girdwood } 1088a978234SLiam Girdwood 1098a978234SLiam Girdwood return 0; 1108a978234SLiam Girdwood } 1118a978234SLiam Girdwood 1128a978234SLiam Girdwood static inline int soc_tplg_is_eof(struct soc_tplg *tplg) 1138a978234SLiam Girdwood { 1148a978234SLiam Girdwood const u8 *end = tplg->hdr_pos; 1158a978234SLiam Girdwood 1168a978234SLiam Girdwood if (end >= tplg->fw->data + tplg->fw->size) 1178a978234SLiam Girdwood return 1; 1188a978234SLiam Girdwood return 0; 1198a978234SLiam Girdwood } 1208a978234SLiam Girdwood 1218a978234SLiam Girdwood static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) 1228a978234SLiam Girdwood { 1238a978234SLiam Girdwood return (unsigned long)(tplg->hdr_pos - tplg->fw->data); 1248a978234SLiam Girdwood } 1258a978234SLiam Girdwood 1268a978234SLiam Girdwood static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) 1278a978234SLiam Girdwood { 1288a978234SLiam Girdwood return (unsigned long)(tplg->pos - tplg->fw->data); 1298a978234SLiam Girdwood } 1308a978234SLiam Girdwood 1318a978234SLiam Girdwood /* mapping of Kcontrol types and associated operations. */ 1328a978234SLiam Girdwood static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { 1338a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, 1348a978234SLiam Girdwood snd_soc_put_volsw, snd_soc_info_volsw}, 1358a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, 1368a978234SLiam Girdwood snd_soc_put_volsw_sx, NULL}, 1378a978234SLiam Girdwood {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, 1388a978234SLiam Girdwood snd_soc_put_enum_double, snd_soc_info_enum_double}, 1398a978234SLiam Girdwood {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, 1408a978234SLiam Girdwood snd_soc_put_enum_double, NULL}, 1418a978234SLiam Girdwood {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, 1428a978234SLiam Girdwood snd_soc_bytes_put, snd_soc_bytes_info}, 1438a978234SLiam Girdwood {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, 1448a978234SLiam Girdwood snd_soc_put_volsw_range, snd_soc_info_volsw_range}, 1458a978234SLiam Girdwood {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, 1468a978234SLiam Girdwood snd_soc_put_xr_sx, snd_soc_info_xr_sx}, 1478a978234SLiam Girdwood {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, 1488a978234SLiam Girdwood snd_soc_put_strobe, NULL}, 1498a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, 1502c57d478SJeeja KP snd_soc_dapm_put_volsw, snd_soc_info_volsw}, 1518a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, 1528a978234SLiam Girdwood snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, 1538a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, 1548a978234SLiam Girdwood snd_soc_dapm_put_enum_double, NULL}, 1558a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, 1568a978234SLiam Girdwood snd_soc_dapm_put_enum_double, NULL}, 1578a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, 1588a978234SLiam Girdwood snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, 1598a978234SLiam Girdwood }; 1608a978234SLiam Girdwood 1618a978234SLiam Girdwood struct soc_tplg_map { 1628a978234SLiam Girdwood int uid; 1638a978234SLiam Girdwood int kid; 1648a978234SLiam Girdwood }; 1658a978234SLiam Girdwood 1668a978234SLiam Girdwood /* mapping of widget types from UAPI IDs to kernel IDs */ 1678a978234SLiam Girdwood static const struct soc_tplg_map dapm_map[] = { 1688a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, 1698a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, 1708a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, 1718a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, 1728a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, 1738a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, 1748a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, 1758a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, 1768a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, 1778a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, 1788a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, 1798a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, 1808a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, 1818a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, 1828a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, 1838a978234SLiam Girdwood {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, 1848a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer}, 1858a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler}, 1868a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect}, 1878a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen}, 1888a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src}, 1898a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc}, 1908a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder}, 1918a70b454SLiam Girdwood {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder}, 1928a978234SLiam Girdwood }; 1938a978234SLiam Girdwood 1948a978234SLiam Girdwood static int tplc_chan_get_reg(struct soc_tplg *tplg, 1958a978234SLiam Girdwood struct snd_soc_tplg_channel *chan, int map) 1968a978234SLiam Girdwood { 1978a978234SLiam Girdwood int i; 1988a978234SLiam Girdwood 1998a978234SLiam Girdwood for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { 2008a978234SLiam Girdwood if (chan[i].id == map) 2018a978234SLiam Girdwood return chan[i].reg; 2028a978234SLiam Girdwood } 2038a978234SLiam Girdwood 2048a978234SLiam Girdwood return -EINVAL; 2058a978234SLiam Girdwood } 2068a978234SLiam Girdwood 2078a978234SLiam Girdwood static int tplc_chan_get_shift(struct soc_tplg *tplg, 2088a978234SLiam Girdwood struct snd_soc_tplg_channel *chan, int map) 2098a978234SLiam Girdwood { 2108a978234SLiam Girdwood int i; 2118a978234SLiam Girdwood 2128a978234SLiam Girdwood for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { 2138a978234SLiam Girdwood if (chan[i].id == map) 2148a978234SLiam Girdwood return chan[i].shift; 2158a978234SLiam Girdwood } 2168a978234SLiam Girdwood 2178a978234SLiam Girdwood return -EINVAL; 2188a978234SLiam Girdwood } 2198a978234SLiam Girdwood 2208a978234SLiam Girdwood static int get_widget_id(int tplg_type) 2218a978234SLiam Girdwood { 2228a978234SLiam Girdwood int i; 2238a978234SLiam Girdwood 2248a978234SLiam Girdwood for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { 2258a978234SLiam Girdwood if (tplg_type == dapm_map[i].uid) 2268a978234SLiam Girdwood return dapm_map[i].kid; 2278a978234SLiam Girdwood } 2288a978234SLiam Girdwood 2298a978234SLiam Girdwood return -EINVAL; 2308a978234SLiam Girdwood } 2318a978234SLiam Girdwood 2328a978234SLiam Girdwood static inline void soc_bind_err(struct soc_tplg *tplg, 2338a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *hdr, int index) 2348a978234SLiam Girdwood { 2358a978234SLiam Girdwood dev_err(tplg->dev, 2368a978234SLiam Girdwood "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", 2378a978234SLiam Girdwood hdr->ops.get, hdr->ops.put, hdr->ops.info, index, 2388a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 2398a978234SLiam Girdwood } 2408a978234SLiam Girdwood 2418a978234SLiam Girdwood static inline void soc_control_err(struct soc_tplg *tplg, 2428a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *hdr, const char *name) 2438a978234SLiam Girdwood { 2448a978234SLiam Girdwood dev_err(tplg->dev, 2458a978234SLiam Girdwood "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", 2468a978234SLiam Girdwood name, hdr->ops.get, hdr->ops.put, hdr->ops.info, 2478a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 2488a978234SLiam Girdwood } 2498a978234SLiam Girdwood 2508a978234SLiam Girdwood /* pass vendor data to component driver for processing */ 2518a978234SLiam Girdwood static int soc_tplg_vendor_load_(struct soc_tplg *tplg, 2528a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 2538a978234SLiam Girdwood { 2548a978234SLiam Girdwood int ret = 0; 2558a978234SLiam Girdwood 2568a978234SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->vendor_load) 257c60b613aSLiam Girdwood ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); 2588a978234SLiam Girdwood else { 2598a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", 2608a978234SLiam Girdwood hdr->vendor_type); 2618a978234SLiam Girdwood return -EINVAL; 2628a978234SLiam Girdwood } 2638a978234SLiam Girdwood 2648a978234SLiam Girdwood if (ret < 0) 2658a978234SLiam Girdwood dev_err(tplg->dev, 2668a978234SLiam Girdwood "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", 2678a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), 2688a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), 2698a978234SLiam Girdwood hdr->type, hdr->vendor_type); 2708a978234SLiam Girdwood return ret; 2718a978234SLiam Girdwood } 2728a978234SLiam Girdwood 2738a978234SLiam Girdwood /* pass vendor data to component driver for processing */ 2748a978234SLiam Girdwood static int soc_tplg_vendor_load(struct soc_tplg *tplg, 2758a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 2768a978234SLiam Girdwood { 2778a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_VENDOR) 2788a978234SLiam Girdwood return 0; 2798a978234SLiam Girdwood 2808a978234SLiam Girdwood return soc_tplg_vendor_load_(tplg, hdr); 2818a978234SLiam Girdwood } 2828a978234SLiam Girdwood 2838a978234SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for 2848a978234SLiam Girdwood * external widgets where we can assign private data/ops */ 2858a978234SLiam Girdwood static int soc_tplg_widget_load(struct soc_tplg *tplg, 2868a978234SLiam Girdwood struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 2878a978234SLiam Girdwood { 2888a978234SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->widget_load) 289c60b613aSLiam Girdwood return tplg->ops->widget_load(tplg->comp, tplg->index, w, 290c60b613aSLiam Girdwood tplg_w); 2918a978234SLiam Girdwood 2928a978234SLiam Girdwood return 0; 2938a978234SLiam Girdwood } 2948a978234SLiam Girdwood 295ebd259d3SLiam Girdwood /* optionally pass new dynamic widget to component driver. This is mainly for 296ebd259d3SLiam Girdwood * external widgets where we can assign private data/ops */ 297ebd259d3SLiam Girdwood static int soc_tplg_widget_ready(struct soc_tplg *tplg, 298ebd259d3SLiam Girdwood struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 299ebd259d3SLiam Girdwood { 300ebd259d3SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->widget_ready) 301c60b613aSLiam Girdwood return tplg->ops->widget_ready(tplg->comp, tplg->index, w, 302c60b613aSLiam Girdwood tplg_w); 303ebd259d3SLiam Girdwood 304ebd259d3SLiam Girdwood return 0; 305ebd259d3SLiam Girdwood } 306ebd259d3SLiam Girdwood 307183b8021SMasahiro Yamada /* pass DAI configurations to component driver for extra initialization */ 30864527e8aSMengdong Lin static int soc_tplg_dai_load(struct soc_tplg *tplg, 309c60b613aSLiam Girdwood struct snd_soc_dai_driver *dai_drv, 310c60b613aSLiam Girdwood struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 3118a978234SLiam Girdwood { 31264527e8aSMengdong Lin if (tplg->comp && tplg->ops && tplg->ops->dai_load) 313c60b613aSLiam Girdwood return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, 314c60b613aSLiam Girdwood pcm, dai); 3158a978234SLiam Girdwood 3168a978234SLiam Girdwood return 0; 3178a978234SLiam Girdwood } 3188a978234SLiam Girdwood 319183b8021SMasahiro Yamada /* pass link configurations to component driver for extra initialization */ 320acfc7d46SMengdong Lin static int soc_tplg_dai_link_load(struct soc_tplg *tplg, 321c60b613aSLiam Girdwood struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) 322acfc7d46SMengdong Lin { 323acfc7d46SMengdong Lin if (tplg->comp && tplg->ops && tplg->ops->link_load) 324c60b613aSLiam Girdwood return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); 325acfc7d46SMengdong Lin 326acfc7d46SMengdong Lin return 0; 327acfc7d46SMengdong Lin } 328acfc7d46SMengdong Lin 3298a978234SLiam Girdwood /* tell the component driver that all firmware has been loaded in this request */ 3308a978234SLiam Girdwood static void soc_tplg_complete(struct soc_tplg *tplg) 3318a978234SLiam Girdwood { 3328a978234SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->complete) 3338a978234SLiam Girdwood tplg->ops->complete(tplg->comp); 3348a978234SLiam Girdwood } 3358a978234SLiam Girdwood 3368a978234SLiam Girdwood /* add a dynamic kcontrol */ 3378a978234SLiam Girdwood static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, 3388a978234SLiam Girdwood const struct snd_kcontrol_new *control_new, const char *prefix, 3398a978234SLiam Girdwood void *data, struct snd_kcontrol **kcontrol) 3408a978234SLiam Girdwood { 3418a978234SLiam Girdwood int err; 3428a978234SLiam Girdwood 3438a978234SLiam Girdwood *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); 3448a978234SLiam Girdwood if (*kcontrol == NULL) { 3458a978234SLiam Girdwood dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", 3468a978234SLiam Girdwood control_new->name); 3478a978234SLiam Girdwood return -ENOMEM; 3488a978234SLiam Girdwood } 3498a978234SLiam Girdwood 3508a978234SLiam Girdwood err = snd_ctl_add(card, *kcontrol); 3518a978234SLiam Girdwood if (err < 0) { 3528a978234SLiam Girdwood dev_err(dev, "ASoC: Failed to add %s: %d\n", 3538a978234SLiam Girdwood control_new->name, err); 3548a978234SLiam Girdwood return err; 3558a978234SLiam Girdwood } 3568a978234SLiam Girdwood 3578a978234SLiam Girdwood return 0; 3588a978234SLiam Girdwood } 3598a978234SLiam Girdwood 3608a978234SLiam Girdwood /* add a dynamic kcontrol for component driver */ 3618a978234SLiam Girdwood static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, 3628a978234SLiam Girdwood struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) 3638a978234SLiam Girdwood { 3648a978234SLiam Girdwood struct snd_soc_component *comp = tplg->comp; 3658a978234SLiam Girdwood 3668a978234SLiam Girdwood return soc_tplg_add_dcontrol(comp->card->snd_card, 3678a978234SLiam Girdwood comp->dev, k, NULL, comp, kcontrol); 3688a978234SLiam Girdwood } 3698a978234SLiam Girdwood 3708a978234SLiam Girdwood /* remove a mixer kcontrol */ 3718a978234SLiam Girdwood static void remove_mixer(struct snd_soc_component *comp, 3728a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 3738a978234SLiam Girdwood { 3748a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 3758a978234SLiam Girdwood struct soc_mixer_control *sm = 3768a978234SLiam Girdwood container_of(dobj, struct soc_mixer_control, dobj); 3778a978234SLiam Girdwood const unsigned int *p = NULL; 3788a978234SLiam Girdwood 3798a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_MIXER) 3808a978234SLiam Girdwood return; 3818a978234SLiam Girdwood 3828a978234SLiam Girdwood if (dobj->ops && dobj->ops->control_unload) 3838a978234SLiam Girdwood dobj->ops->control_unload(comp, dobj); 3848a978234SLiam Girdwood 38533ae6ae2SAmadeusz Sławiński if (dobj->control.kcontrol->tlv.p) 38633ae6ae2SAmadeusz Sławiński p = dobj->control.kcontrol->tlv.p; 38733ae6ae2SAmadeusz Sławiński snd_ctl_remove(card, dobj->control.kcontrol); 38833ae6ae2SAmadeusz Sławiński list_del(&dobj->list); 3898a978234SLiam Girdwood kfree(sm); 3908a978234SLiam Girdwood kfree(p); 3918a978234SLiam Girdwood } 3928a978234SLiam Girdwood 3938a978234SLiam Girdwood /* remove an enum kcontrol */ 3948a978234SLiam Girdwood static void remove_enum(struct snd_soc_component *comp, 3958a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 3968a978234SLiam Girdwood { 3978a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 3988a978234SLiam Girdwood struct soc_enum *se = container_of(dobj, struct soc_enum, dobj); 3998a978234SLiam Girdwood int i; 4008a978234SLiam Girdwood 4018a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_MIXER) 4028a978234SLiam Girdwood return; 4038a978234SLiam Girdwood 4048a978234SLiam Girdwood if (dobj->ops && dobj->ops->control_unload) 4058a978234SLiam Girdwood dobj->ops->control_unload(comp, dobj); 4068a978234SLiam Girdwood 40733ae6ae2SAmadeusz Sławiński snd_ctl_remove(card, dobj->control.kcontrol); 40833ae6ae2SAmadeusz Sławiński list_del(&dobj->list); 4098a978234SLiam Girdwood 41033ae6ae2SAmadeusz Sławiński kfree(dobj->control.dvalues); 4118a978234SLiam Girdwood for (i = 0; i < se->items; i++) 41233ae6ae2SAmadeusz Sławiński kfree(dobj->control.dtexts[i]); 41334db6a3eSAmadeusz Sławiński kfree(dobj->control.dtexts); 4148a978234SLiam Girdwood kfree(se); 4158a978234SLiam Girdwood } 4168a978234SLiam Girdwood 4178a978234SLiam Girdwood /* remove a byte kcontrol */ 4188a978234SLiam Girdwood static void remove_bytes(struct snd_soc_component *comp, 4198a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 4208a978234SLiam Girdwood { 4218a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 4228a978234SLiam Girdwood struct soc_bytes_ext *sb = 4238a978234SLiam Girdwood container_of(dobj, struct soc_bytes_ext, dobj); 4248a978234SLiam Girdwood 4258a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_MIXER) 4268a978234SLiam Girdwood return; 4278a978234SLiam Girdwood 4288a978234SLiam Girdwood if (dobj->ops && dobj->ops->control_unload) 4298a978234SLiam Girdwood dobj->ops->control_unload(comp, dobj); 4308a978234SLiam Girdwood 43133ae6ae2SAmadeusz Sławiński snd_ctl_remove(card, dobj->control.kcontrol); 43233ae6ae2SAmadeusz Sławiński list_del(&dobj->list); 4338a978234SLiam Girdwood kfree(sb); 4348a978234SLiam Girdwood } 4358a978234SLiam Girdwood 4367df04ea7SRanjani Sridharan /* remove a route */ 4377df04ea7SRanjani Sridharan static void remove_route(struct snd_soc_component *comp, 4387df04ea7SRanjani Sridharan struct snd_soc_dobj *dobj, int pass) 4397df04ea7SRanjani Sridharan { 4407df04ea7SRanjani Sridharan struct snd_soc_dapm_route *route = 4417df04ea7SRanjani Sridharan container_of(dobj, struct snd_soc_dapm_route, dobj); 4427df04ea7SRanjani Sridharan 4437df04ea7SRanjani Sridharan if (pass != SOC_TPLG_PASS_GRAPH) 4447df04ea7SRanjani Sridharan return; 4457df04ea7SRanjani Sridharan 4467df04ea7SRanjani Sridharan if (dobj->ops && dobj->ops->dapm_route_unload) 4477df04ea7SRanjani Sridharan dobj->ops->dapm_route_unload(comp, dobj); 4487df04ea7SRanjani Sridharan 4497df04ea7SRanjani Sridharan list_del(&dobj->list); 4507df04ea7SRanjani Sridharan kfree(route); 4517df04ea7SRanjani Sridharan } 4527df04ea7SRanjani Sridharan 4538a978234SLiam Girdwood /* remove a widget and it's kcontrols - routes must be removed first */ 4548a978234SLiam Girdwood static void remove_widget(struct snd_soc_component *comp, 4558a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 4568a978234SLiam Girdwood { 4578a978234SLiam Girdwood struct snd_card *card = comp->card->snd_card; 4588a978234SLiam Girdwood struct snd_soc_dapm_widget *w = 4598a978234SLiam Girdwood container_of(dobj, struct snd_soc_dapm_widget, dobj); 4608a978234SLiam Girdwood int i; 4618a978234SLiam Girdwood 4628a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_WIDGET) 4638a978234SLiam Girdwood return; 4648a978234SLiam Girdwood 4658a978234SLiam Girdwood if (dobj->ops && dobj->ops->widget_unload) 4668a978234SLiam Girdwood dobj->ops->widget_unload(comp, dobj); 4678a978234SLiam Girdwood 46805bdcf12SLiam Girdwood if (!w->kcontrols) 46905bdcf12SLiam Girdwood goto free_news; 47005bdcf12SLiam Girdwood 4718a978234SLiam Girdwood /* 4721a7dd6e2SMengdong Lin * Dynamic Widgets either have 1..N enum kcontrols or mixers. 4738a978234SLiam Girdwood * The enum may either have an array of values or strings. 4748a978234SLiam Girdwood */ 475eea3dd4fSMengdong Lin if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) { 4768a978234SLiam Girdwood /* enumerated widget mixer */ 477f53c4c20SLiam Girdwood for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { 4781a7dd6e2SMengdong Lin struct snd_kcontrol *kcontrol = w->kcontrols[i]; 4798a978234SLiam Girdwood struct soc_enum *se = 4801a7dd6e2SMengdong Lin (struct soc_enum *)kcontrol->private_value; 481d7766aa5SColin Ian King int j; 4828a978234SLiam Girdwood 4831a7dd6e2SMengdong Lin snd_ctl_remove(card, kcontrol); 4848a978234SLiam Girdwood 48533ae6ae2SAmadeusz Sławiński kfree(dobj->control.dvalues); 486d7766aa5SColin Ian King for (j = 0; j < se->items; j++) 48733ae6ae2SAmadeusz Sławiński kfree(dobj->control.dtexts[j]); 48834db6a3eSAmadeusz Sławiński kfree(dobj->control.dtexts); 4898a978234SLiam Girdwood 4908a978234SLiam Girdwood kfree(se); 491267e2c6fSLiam Girdwood kfree(w->kcontrol_news[i].name); 4921a7dd6e2SMengdong Lin } 4938a978234SLiam Girdwood } else { 494eea3dd4fSMengdong Lin /* volume mixer or bytes controls */ 495f53c4c20SLiam Girdwood for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { 4968a978234SLiam Girdwood struct snd_kcontrol *kcontrol = w->kcontrols[i]; 4978a978234SLiam Girdwood 498eea3dd4fSMengdong Lin if (dobj->widget.kcontrol_type 499eea3dd4fSMengdong Lin == SND_SOC_TPLG_TYPE_MIXER) 500eea3dd4fSMengdong Lin kfree(kcontrol->tlv.p); 5018a978234SLiam Girdwood 502eea3dd4fSMengdong Lin /* Private value is used as struct soc_mixer_control 503eea3dd4fSMengdong Lin * for volume mixers or soc_bytes_ext for bytes 504eea3dd4fSMengdong Lin * controls. 505eea3dd4fSMengdong Lin */ 506eea3dd4fSMengdong Lin kfree((void *)kcontrol->private_value); 507c2b36129SColin Ian King snd_ctl_remove(card, kcontrol); 508267e2c6fSLiam Girdwood kfree(w->kcontrol_news[i].name); 5098a978234SLiam Girdwood } 5108a978234SLiam Girdwood } 51105bdcf12SLiam Girdwood 51205bdcf12SLiam Girdwood free_news: 5138a978234SLiam Girdwood kfree(w->kcontrol_news); 51405bdcf12SLiam Girdwood 515a46e8393SAmadeusz Sławiński list_del(&dobj->list); 516a46e8393SAmadeusz Sławiński 5178a978234SLiam Girdwood /* widget w is freed by soc-dapm.c */ 5188a978234SLiam Girdwood } 5198a978234SLiam Girdwood 52064527e8aSMengdong Lin /* remove DAI configurations */ 52164527e8aSMengdong Lin static void remove_dai(struct snd_soc_component *comp, 5228a978234SLiam Girdwood struct snd_soc_dobj *dobj, int pass) 5238a978234SLiam Girdwood { 52464527e8aSMengdong Lin struct snd_soc_dai_driver *dai_drv = 52564527e8aSMengdong Lin container_of(dobj, struct snd_soc_dai_driver, dobj); 52652abe6ccSGuennadi Liakhovetski struct snd_soc_dai *dai; 52764527e8aSMengdong Lin 5288a978234SLiam Girdwood if (pass != SOC_TPLG_PASS_PCM_DAI) 5298a978234SLiam Girdwood return; 5308a978234SLiam Girdwood 53164527e8aSMengdong Lin if (dobj->ops && dobj->ops->dai_unload) 53264527e8aSMengdong Lin dobj->ops->dai_unload(comp, dobj); 5338a978234SLiam Girdwood 53452abe6ccSGuennadi Liakhovetski list_for_each_entry(dai, &comp->dai_list, list) 53552abe6ccSGuennadi Liakhovetski if (dai->driver == dai_drv) 53652abe6ccSGuennadi Liakhovetski dai->driver = NULL; 53752abe6ccSGuennadi Liakhovetski 538*7b6f68a4SBard liao kfree(dai_drv->playback.stream_name); 539*7b6f68a4SBard liao kfree(dai_drv->capture.stream_name); 5408f27c4abSMengdong Lin kfree(dai_drv->name); 5418a978234SLiam Girdwood list_del(&dobj->list); 54264527e8aSMengdong Lin kfree(dai_drv); 5438a978234SLiam Girdwood } 5448a978234SLiam Girdwood 545acfc7d46SMengdong Lin /* remove link configurations */ 546acfc7d46SMengdong Lin static void remove_link(struct snd_soc_component *comp, 547acfc7d46SMengdong Lin struct snd_soc_dobj *dobj, int pass) 548acfc7d46SMengdong Lin { 549acfc7d46SMengdong Lin struct snd_soc_dai_link *link = 550acfc7d46SMengdong Lin container_of(dobj, struct snd_soc_dai_link, dobj); 551acfc7d46SMengdong Lin 552acfc7d46SMengdong Lin if (pass != SOC_TPLG_PASS_PCM_DAI) 553acfc7d46SMengdong Lin return; 554acfc7d46SMengdong Lin 555acfc7d46SMengdong Lin if (dobj->ops && dobj->ops->link_unload) 556acfc7d46SMengdong Lin dobj->ops->link_unload(comp, dobj); 557acfc7d46SMengdong Lin 5588f27c4abSMengdong Lin kfree(link->name); 5598f27c4abSMengdong Lin kfree(link->stream_name); 5608f27c4abSMengdong Lin kfree(link->cpu_dai_name); 5618f27c4abSMengdong Lin 562acfc7d46SMengdong Lin list_del(&dobj->list); 563acfc7d46SMengdong Lin snd_soc_remove_dai_link(comp->card, link); 564acfc7d46SMengdong Lin kfree(link); 565acfc7d46SMengdong Lin } 566acfc7d46SMengdong Lin 567adfebb51SBard liao /* unload dai link */ 568adfebb51SBard liao static void remove_backend_link(struct snd_soc_component *comp, 569adfebb51SBard liao struct snd_soc_dobj *dobj, int pass) 570adfebb51SBard liao { 571adfebb51SBard liao if (pass != SOC_TPLG_PASS_LINK) 572adfebb51SBard liao return; 573adfebb51SBard liao 574adfebb51SBard liao if (dobj->ops && dobj->ops->link_unload) 575adfebb51SBard liao dobj->ops->link_unload(comp, dobj); 576adfebb51SBard liao 577adfebb51SBard liao /* 578adfebb51SBard liao * We don't free the link here as what remove_link() do since BE 579adfebb51SBard liao * links are not allocated by topology. 580adfebb51SBard liao * We however need to reset the dobj type to its initial values 581adfebb51SBard liao */ 582adfebb51SBard liao dobj->type = SND_SOC_DOBJ_NONE; 583adfebb51SBard liao list_del(&dobj->list); 584adfebb51SBard liao } 585adfebb51SBard liao 5868a978234SLiam Girdwood /* bind a kcontrol to it's IO handlers */ 5878a978234SLiam Girdwood static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, 5888a978234SLiam Girdwood struct snd_kcontrol_new *k, 5892b5cdb91SMengdong Lin const struct soc_tplg *tplg) 5908a978234SLiam Girdwood { 5912b5cdb91SMengdong Lin const struct snd_soc_tplg_kcontrol_ops *ops; 5921a3232d2SMengdong Lin const struct snd_soc_tplg_bytes_ext_ops *ext_ops; 5932b5cdb91SMengdong Lin int num_ops, i; 5948a978234SLiam Girdwood 5951a3232d2SMengdong Lin if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES 5961a3232d2SMengdong Lin && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER 5971a3232d2SMengdong Lin && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE 5981a3232d2SMengdong Lin && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 5991a3232d2SMengdong Lin struct soc_bytes_ext *sbe; 6001a3232d2SMengdong Lin struct snd_soc_tplg_bytes_control *be; 6011a3232d2SMengdong Lin 6021a3232d2SMengdong Lin sbe = (struct soc_bytes_ext *)k->private_value; 6031a3232d2SMengdong Lin be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); 6041a3232d2SMengdong Lin 6051a3232d2SMengdong Lin /* TLV bytes controls need standard kcontrol info handler, 6061a3232d2SMengdong Lin * TLV callback and extended put/get handlers. 6071a3232d2SMengdong Lin */ 608f4be978bSOmair M Abdullah k->info = snd_soc_bytes_info_ext; 6091a3232d2SMengdong Lin k->tlv.c = snd_soc_bytes_tlv_callback; 6101a3232d2SMengdong Lin 6111a3232d2SMengdong Lin ext_ops = tplg->bytes_ext_ops; 6121a3232d2SMengdong Lin num_ops = tplg->bytes_ext_ops_count; 6131a3232d2SMengdong Lin for (i = 0; i < num_ops; i++) { 6141a3232d2SMengdong Lin if (!sbe->put && ext_ops[i].id == be->ext_ops.put) 6151a3232d2SMengdong Lin sbe->put = ext_ops[i].put; 6161a3232d2SMengdong Lin if (!sbe->get && ext_ops[i].id == be->ext_ops.get) 6171a3232d2SMengdong Lin sbe->get = ext_ops[i].get; 6181a3232d2SMengdong Lin } 6191a3232d2SMengdong Lin 6201a3232d2SMengdong Lin if (sbe->put && sbe->get) 6211a3232d2SMengdong Lin return 0; 6221a3232d2SMengdong Lin else 6231a3232d2SMengdong Lin return -EINVAL; 6241a3232d2SMengdong Lin } 6251a3232d2SMengdong Lin 62688a17d8fSMengdong Lin /* try and map vendor specific kcontrol handlers first */ 6272b5cdb91SMengdong Lin ops = tplg->io_ops; 6282b5cdb91SMengdong Lin num_ops = tplg->io_ops_count; 6298a978234SLiam Girdwood for (i = 0; i < num_ops; i++) { 6308a978234SLiam Girdwood 6312b5cdb91SMengdong Lin if (k->put == NULL && ops[i].id == hdr->ops.put) 6328a978234SLiam Girdwood k->put = ops[i].put; 6332b5cdb91SMengdong Lin if (k->get == NULL && ops[i].id == hdr->ops.get) 6348a978234SLiam Girdwood k->get = ops[i].get; 6352b5cdb91SMengdong Lin if (k->info == NULL && ops[i].id == hdr->ops.info) 6362b5cdb91SMengdong Lin k->info = ops[i].info; 6378a978234SLiam Girdwood } 6388a978234SLiam Girdwood 63988a17d8fSMengdong Lin /* vendor specific handlers found ? */ 64088a17d8fSMengdong Lin if (k->put && k->get && k->info) 64188a17d8fSMengdong Lin return 0; 64288a17d8fSMengdong Lin 64388a17d8fSMengdong Lin /* none found so try standard kcontrol handlers */ 6442b5cdb91SMengdong Lin ops = io_ops; 6452b5cdb91SMengdong Lin num_ops = ARRAY_SIZE(io_ops); 64688a17d8fSMengdong Lin for (i = 0; i < num_ops; i++) { 64788a17d8fSMengdong Lin 64888a17d8fSMengdong Lin if (k->put == NULL && ops[i].id == hdr->ops.put) 64988a17d8fSMengdong Lin k->put = ops[i].put; 65088a17d8fSMengdong Lin if (k->get == NULL && ops[i].id == hdr->ops.get) 65188a17d8fSMengdong Lin k->get = ops[i].get; 65288a17d8fSMengdong Lin if (k->info == NULL && ops[i].id == hdr->ops.info) 6538a978234SLiam Girdwood k->info = ops[i].info; 6548a978234SLiam Girdwood } 6558a978234SLiam Girdwood 6568a978234SLiam Girdwood /* standard handlers found ? */ 6578a978234SLiam Girdwood if (k->put && k->get && k->info) 6588a978234SLiam Girdwood return 0; 6598a978234SLiam Girdwood 6608a978234SLiam Girdwood /* nothing to bind */ 6618a978234SLiam Girdwood return -EINVAL; 6628a978234SLiam Girdwood } 6638a978234SLiam Girdwood 6648a978234SLiam Girdwood /* bind a widgets to it's evnt handlers */ 6658a978234SLiam Girdwood int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, 6668a978234SLiam Girdwood const struct snd_soc_tplg_widget_events *events, 6678a978234SLiam Girdwood int num_events, u16 event_type) 6688a978234SLiam Girdwood { 6698a978234SLiam Girdwood int i; 6708a978234SLiam Girdwood 6718a978234SLiam Girdwood w->event = NULL; 6728a978234SLiam Girdwood 6738a978234SLiam Girdwood for (i = 0; i < num_events; i++) { 6748a978234SLiam Girdwood if (event_type == events[i].type) { 6758a978234SLiam Girdwood 6768a978234SLiam Girdwood /* found - so assign event */ 6778a978234SLiam Girdwood w->event = events[i].event_handler; 6788a978234SLiam Girdwood return 0; 6798a978234SLiam Girdwood } 6808a978234SLiam Girdwood } 6818a978234SLiam Girdwood 6828a978234SLiam Girdwood /* not found */ 6838a978234SLiam Girdwood return -EINVAL; 6848a978234SLiam Girdwood } 6858a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); 6868a978234SLiam Girdwood 6878a978234SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */ 6888a978234SLiam Girdwood static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, 6898a978234SLiam Girdwood struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) 6908a978234SLiam Girdwood { 6918a978234SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->control_load) 692c60b613aSLiam Girdwood return tplg->ops->control_load(tplg->comp, tplg->index, k, 693c60b613aSLiam Girdwood hdr); 6948a978234SLiam Girdwood 6958a978234SLiam Girdwood return 0; 6968a978234SLiam Girdwood } 6978a978234SLiam Girdwood 69828a87eebSMengdong Lin 69928a87eebSMengdong Lin static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, 70028a87eebSMengdong Lin struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) 7018a978234SLiam Girdwood { 70228a87eebSMengdong Lin unsigned int item_len = 2 * sizeof(unsigned int); 70328a87eebSMengdong Lin unsigned int *p; 7048a978234SLiam Girdwood 70528a87eebSMengdong Lin p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); 70628a87eebSMengdong Lin if (!p) 7078a978234SLiam Girdwood return -ENOMEM; 7088a978234SLiam Girdwood 70928a87eebSMengdong Lin p[0] = SNDRV_CTL_TLVT_DB_SCALE; 71028a87eebSMengdong Lin p[1] = item_len; 71128a87eebSMengdong Lin p[2] = scale->min; 71228a87eebSMengdong Lin p[3] = (scale->step & TLV_DB_SCALE_MASK) 71328a87eebSMengdong Lin | (scale->mute ? TLV_DB_SCALE_MUTE : 0); 7148a978234SLiam Girdwood 71528a87eebSMengdong Lin kc->tlv.p = (void *)p; 71628a87eebSMengdong Lin return 0; 71728a87eebSMengdong Lin } 71828a87eebSMengdong Lin 71928a87eebSMengdong Lin static int soc_tplg_create_tlv(struct soc_tplg *tplg, 72028a87eebSMengdong Lin struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) 72128a87eebSMengdong Lin { 72228a87eebSMengdong Lin struct snd_soc_tplg_ctl_tlv *tplg_tlv; 72328a87eebSMengdong Lin 72428a87eebSMengdong Lin if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) 72528a87eebSMengdong Lin return 0; 72628a87eebSMengdong Lin 7271a3232d2SMengdong Lin if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { 72828a87eebSMengdong Lin tplg_tlv = &tc->tlv; 72928a87eebSMengdong Lin switch (tplg_tlv->type) { 73028a87eebSMengdong Lin case SNDRV_CTL_TLVT_DB_SCALE: 73128a87eebSMengdong Lin return soc_tplg_create_tlv_db_scale(tplg, kc, 73228a87eebSMengdong Lin &tplg_tlv->scale); 73328a87eebSMengdong Lin 73428a87eebSMengdong Lin /* TODO: add support for other TLV types */ 73528a87eebSMengdong Lin default: 73628a87eebSMengdong Lin dev_dbg(tplg->dev, "Unsupported TLV type %d\n", 73728a87eebSMengdong Lin tplg_tlv->type); 73828a87eebSMengdong Lin return -EINVAL; 73928a87eebSMengdong Lin } 74028a87eebSMengdong Lin } 7418a978234SLiam Girdwood 7428a978234SLiam Girdwood return 0; 7438a978234SLiam Girdwood } 7448a978234SLiam Girdwood 7458a978234SLiam Girdwood static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, 7468a978234SLiam Girdwood struct snd_kcontrol_new *kc) 7478a978234SLiam Girdwood { 7488a978234SLiam Girdwood kfree(kc->tlv.p); 7498a978234SLiam Girdwood } 7508a978234SLiam Girdwood 7518a978234SLiam Girdwood static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, 7528a978234SLiam Girdwood size_t size) 7538a978234SLiam Girdwood { 7548a978234SLiam Girdwood struct snd_soc_tplg_bytes_control *be; 7558a978234SLiam Girdwood struct soc_bytes_ext *sbe; 7568a978234SLiam Girdwood struct snd_kcontrol_new kc; 7578a978234SLiam Girdwood int i, err; 7588a978234SLiam Girdwood 7598a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 7608a978234SLiam Girdwood sizeof(struct snd_soc_tplg_bytes_control), count, 7618a978234SLiam Girdwood size, "mixer bytes")) { 7628a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", 7638a978234SLiam Girdwood count); 7648a978234SLiam Girdwood return -EINVAL; 7658a978234SLiam Girdwood } 7668a978234SLiam Girdwood 7678a978234SLiam Girdwood for (i = 0; i < count; i++) { 7688a978234SLiam Girdwood be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 7698a978234SLiam Girdwood 7708a978234SLiam Girdwood /* validate kcontrol */ 7718a978234SLiam Girdwood if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 7728a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 7738a978234SLiam Girdwood return -EINVAL; 7748a978234SLiam Girdwood 7758a978234SLiam Girdwood sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); 7768a978234SLiam Girdwood if (sbe == NULL) 7778a978234SLiam Girdwood return -ENOMEM; 7788a978234SLiam Girdwood 7798a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + 7808a978234SLiam Girdwood be->priv.size); 7818a978234SLiam Girdwood 7828a978234SLiam Girdwood dev_dbg(tplg->dev, 7838a978234SLiam Girdwood "ASoC: adding bytes kcontrol %s with access 0x%x\n", 7848a978234SLiam Girdwood be->hdr.name, be->hdr.access); 7858a978234SLiam Girdwood 7868a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 7878a978234SLiam Girdwood kc.name = be->hdr.name; 7888a978234SLiam Girdwood kc.private_value = (long)sbe; 7898a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 7908a978234SLiam Girdwood kc.access = be->hdr.access; 7918a978234SLiam Girdwood 7928a978234SLiam Girdwood sbe->max = be->max; 7938a978234SLiam Girdwood sbe->dobj.type = SND_SOC_DOBJ_BYTES; 7948a978234SLiam Girdwood sbe->dobj.ops = tplg->ops; 7958a978234SLiam Girdwood INIT_LIST_HEAD(&sbe->dobj.list); 7968a978234SLiam Girdwood 7978a978234SLiam Girdwood /* map io handlers */ 7982b5cdb91SMengdong Lin err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); 7998a978234SLiam Girdwood if (err) { 8008a978234SLiam Girdwood soc_control_err(tplg, &be->hdr, be->hdr.name); 8018a978234SLiam Girdwood kfree(sbe); 8028a978234SLiam Girdwood continue; 8038a978234SLiam Girdwood } 8048a978234SLiam Girdwood 8058a978234SLiam Girdwood /* pass control to driver for optional further init */ 8068a978234SLiam Girdwood err = soc_tplg_init_kcontrol(tplg, &kc, 8078a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *)be); 8088a978234SLiam Girdwood if (err < 0) { 8098a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 8108a978234SLiam Girdwood be->hdr.name); 8118a978234SLiam Girdwood kfree(sbe); 8128a978234SLiam Girdwood continue; 8138a978234SLiam Girdwood } 8148a978234SLiam Girdwood 8158a978234SLiam Girdwood /* register control here */ 8168a978234SLiam Girdwood err = soc_tplg_add_kcontrol(tplg, &kc, 8178a978234SLiam Girdwood &sbe->dobj.control.kcontrol); 8188a978234SLiam Girdwood if (err < 0) { 8198a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to add %s\n", 8208a978234SLiam Girdwood be->hdr.name); 8218a978234SLiam Girdwood kfree(sbe); 8228a978234SLiam Girdwood continue; 8238a978234SLiam Girdwood } 8248a978234SLiam Girdwood 8258a978234SLiam Girdwood list_add(&sbe->dobj.list, &tplg->comp->dobj_list); 8268a978234SLiam Girdwood } 8278a978234SLiam Girdwood return 0; 8288a978234SLiam Girdwood 8298a978234SLiam Girdwood } 8308a978234SLiam Girdwood 8318a978234SLiam Girdwood static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, 8328a978234SLiam Girdwood size_t size) 8338a978234SLiam Girdwood { 8348a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 8358a978234SLiam Girdwood struct soc_mixer_control *sm; 8368a978234SLiam Girdwood struct snd_kcontrol_new kc; 8378a978234SLiam Girdwood int i, err; 8388a978234SLiam Girdwood 8398a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 8408a978234SLiam Girdwood sizeof(struct snd_soc_tplg_mixer_control), 8418a978234SLiam Girdwood count, size, "mixers")) { 8428a978234SLiam Girdwood 8438a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", 8448a978234SLiam Girdwood count); 8458a978234SLiam Girdwood return -EINVAL; 8468a978234SLiam Girdwood } 8478a978234SLiam Girdwood 8488a978234SLiam Girdwood for (i = 0; i < count; i++) { 8498a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 8508a978234SLiam Girdwood 8518a978234SLiam Girdwood /* validate kcontrol */ 8528a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 8538a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 8548a978234SLiam Girdwood return -EINVAL; 8558a978234SLiam Girdwood 8568a978234SLiam Girdwood sm = kzalloc(sizeof(*sm), GFP_KERNEL); 8578a978234SLiam Girdwood if (sm == NULL) 8588a978234SLiam Girdwood return -ENOMEM; 8598a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + 8608a978234SLiam Girdwood mc->priv.size); 8618a978234SLiam Girdwood 8628a978234SLiam Girdwood dev_dbg(tplg->dev, 8638a978234SLiam Girdwood "ASoC: adding mixer kcontrol %s with access 0x%x\n", 8648a978234SLiam Girdwood mc->hdr.name, mc->hdr.access); 8658a978234SLiam Girdwood 8668a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 8678a978234SLiam Girdwood kc.name = mc->hdr.name; 8688a978234SLiam Girdwood kc.private_value = (long)sm; 8698a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 8708a978234SLiam Girdwood kc.access = mc->hdr.access; 8718a978234SLiam Girdwood 8728a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 8738a978234SLiam Girdwood sm->reg = tplc_chan_get_reg(tplg, mc->channel, 8748a978234SLiam Girdwood SNDRV_CHMAP_FL); 8758a978234SLiam Girdwood sm->rreg = tplc_chan_get_reg(tplg, mc->channel, 8768a978234SLiam Girdwood SNDRV_CHMAP_FR); 8778a978234SLiam Girdwood sm->shift = tplc_chan_get_shift(tplg, mc->channel, 8788a978234SLiam Girdwood SNDRV_CHMAP_FL); 8798a978234SLiam Girdwood sm->rshift = tplc_chan_get_shift(tplg, mc->channel, 8808a978234SLiam Girdwood SNDRV_CHMAP_FR); 8818a978234SLiam Girdwood 8828a978234SLiam Girdwood sm->max = mc->max; 8838a978234SLiam Girdwood sm->min = mc->min; 8848a978234SLiam Girdwood sm->invert = mc->invert; 8858a978234SLiam Girdwood sm->platform_max = mc->platform_max; 8868a978234SLiam Girdwood sm->dobj.index = tplg->index; 8878a978234SLiam Girdwood sm->dobj.ops = tplg->ops; 8888a978234SLiam Girdwood sm->dobj.type = SND_SOC_DOBJ_MIXER; 8898a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 8908a978234SLiam Girdwood 8918a978234SLiam Girdwood /* map io handlers */ 8922b5cdb91SMengdong Lin err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); 8938a978234SLiam Girdwood if (err) { 8948a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 8958a978234SLiam Girdwood kfree(sm); 8968a978234SLiam Girdwood continue; 8978a978234SLiam Girdwood } 8988a978234SLiam Girdwood 8993789debfSBard liao /* create any TLV data */ 9003789debfSBard liao soc_tplg_create_tlv(tplg, &kc, &mc->hdr); 9013789debfSBard liao 9028a978234SLiam Girdwood /* pass control to driver for optional further init */ 9038a978234SLiam Girdwood err = soc_tplg_init_kcontrol(tplg, &kc, 9048a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *) mc); 9058a978234SLiam Girdwood if (err < 0) { 9068a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 9078a978234SLiam Girdwood mc->hdr.name); 9083789debfSBard liao soc_tplg_free_tlv(tplg, &kc); 9098a978234SLiam Girdwood kfree(sm); 9108a978234SLiam Girdwood continue; 9118a978234SLiam Girdwood } 9128a978234SLiam Girdwood 9138a978234SLiam Girdwood /* register control here */ 9148a978234SLiam Girdwood err = soc_tplg_add_kcontrol(tplg, &kc, 9158a978234SLiam Girdwood &sm->dobj.control.kcontrol); 9168a978234SLiam Girdwood if (err < 0) { 9178a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to add %s\n", 9188a978234SLiam Girdwood mc->hdr.name); 9198a978234SLiam Girdwood soc_tplg_free_tlv(tplg, &kc); 9208a978234SLiam Girdwood kfree(sm); 9218a978234SLiam Girdwood continue; 9228a978234SLiam Girdwood } 9238a978234SLiam Girdwood 9248a978234SLiam Girdwood list_add(&sm->dobj.list, &tplg->comp->dobj_list); 9258a978234SLiam Girdwood } 9268a978234SLiam Girdwood 9278a978234SLiam Girdwood return 0; 9288a978234SLiam Girdwood } 9298a978234SLiam Girdwood 9308a978234SLiam Girdwood static int soc_tplg_denum_create_texts(struct soc_enum *se, 9318a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 9328a978234SLiam Girdwood { 9338a978234SLiam Girdwood int i, ret; 9348a978234SLiam Girdwood 9358a978234SLiam Girdwood se->dobj.control.dtexts = 9366396bb22SKees Cook kcalloc(ec->items, sizeof(char *), GFP_KERNEL); 9378a978234SLiam Girdwood if (se->dobj.control.dtexts == NULL) 9388a978234SLiam Girdwood return -ENOMEM; 9398a978234SLiam Girdwood 9408a978234SLiam Girdwood for (i = 0; i < ec->items; i++) { 9418a978234SLiam Girdwood 9428a978234SLiam Girdwood if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 9438a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 9448a978234SLiam Girdwood ret = -EINVAL; 9458a978234SLiam Girdwood goto err; 9468a978234SLiam Girdwood } 9478a978234SLiam Girdwood 9488a978234SLiam Girdwood se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); 9498a978234SLiam Girdwood if (!se->dobj.control.dtexts[i]) { 9508a978234SLiam Girdwood ret = -ENOMEM; 9518a978234SLiam Girdwood goto err; 9528a978234SLiam Girdwood } 9538a978234SLiam Girdwood } 9548a978234SLiam Girdwood 955b6e38b29SMousumi Jana se->texts = (const char * const *)se->dobj.control.dtexts; 9568a978234SLiam Girdwood return 0; 9578a978234SLiam Girdwood 9588a978234SLiam Girdwood err: 9598a978234SLiam Girdwood for (--i; i >= 0; i--) 9608a978234SLiam Girdwood kfree(se->dobj.control.dtexts[i]); 9618a978234SLiam Girdwood kfree(se->dobj.control.dtexts); 9628a978234SLiam Girdwood return ret; 9638a978234SLiam Girdwood } 9648a978234SLiam Girdwood 9658a978234SLiam Girdwood static int soc_tplg_denum_create_values(struct soc_enum *se, 9668a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec) 9678a978234SLiam Girdwood { 9688a978234SLiam Girdwood if (ec->items > sizeof(*ec->values)) 9698a978234SLiam Girdwood return -EINVAL; 9708a978234SLiam Girdwood 971376c0afeSAndrzej Hajda se->dobj.control.dvalues = kmemdup(ec->values, 972376c0afeSAndrzej Hajda ec->items * sizeof(u32), 973376c0afeSAndrzej Hajda GFP_KERNEL); 9748a978234SLiam Girdwood if (!se->dobj.control.dvalues) 9758a978234SLiam Girdwood return -ENOMEM; 9768a978234SLiam Girdwood 9778a978234SLiam Girdwood return 0; 9788a978234SLiam Girdwood } 9798a978234SLiam Girdwood 9808a978234SLiam Girdwood static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, 9818a978234SLiam Girdwood size_t size) 9828a978234SLiam Girdwood { 9838a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 9848a978234SLiam Girdwood struct soc_enum *se; 9858a978234SLiam Girdwood struct snd_kcontrol_new kc; 9868a978234SLiam Girdwood int i, ret, err; 9878a978234SLiam Girdwood 9888a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 9898a978234SLiam Girdwood sizeof(struct snd_soc_tplg_enum_control), 9908a978234SLiam Girdwood count, size, "enums")) { 9918a978234SLiam Girdwood 9928a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", 9938a978234SLiam Girdwood count); 9948a978234SLiam Girdwood return -EINVAL; 9958a978234SLiam Girdwood } 9968a978234SLiam Girdwood 9978a978234SLiam Girdwood for (i = 0; i < count; i++) { 9988a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 9998a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 10008a978234SLiam Girdwood ec->priv.size); 10018a978234SLiam Girdwood 10028a978234SLiam Girdwood /* validate kcontrol */ 10038a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 10048a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 10058a978234SLiam Girdwood return -EINVAL; 10068a978234SLiam Girdwood 10078a978234SLiam Girdwood se = kzalloc((sizeof(*se)), GFP_KERNEL); 10088a978234SLiam Girdwood if (se == NULL) 10098a978234SLiam Girdwood return -ENOMEM; 10108a978234SLiam Girdwood 10118a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", 10128a978234SLiam Girdwood ec->hdr.name, ec->items); 10138a978234SLiam Girdwood 10148a978234SLiam Girdwood memset(&kc, 0, sizeof(kc)); 10158a978234SLiam Girdwood kc.name = ec->hdr.name; 10168a978234SLiam Girdwood kc.private_value = (long)se; 10178a978234SLiam Girdwood kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 10188a978234SLiam Girdwood kc.access = ec->hdr.access; 10198a978234SLiam Girdwood 10208a978234SLiam Girdwood se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 10218a978234SLiam Girdwood se->shift_l = tplc_chan_get_shift(tplg, ec->channel, 10228a978234SLiam Girdwood SNDRV_CHMAP_FL); 10238a978234SLiam Girdwood se->shift_r = tplc_chan_get_shift(tplg, ec->channel, 10248a978234SLiam Girdwood SNDRV_CHMAP_FL); 10258a978234SLiam Girdwood 10268a978234SLiam Girdwood se->items = ec->items; 10278a978234SLiam Girdwood se->mask = ec->mask; 10288a978234SLiam Girdwood se->dobj.index = tplg->index; 10298a978234SLiam Girdwood se->dobj.type = SND_SOC_DOBJ_ENUM; 10308a978234SLiam Girdwood se->dobj.ops = tplg->ops; 10318a978234SLiam Girdwood INIT_LIST_HEAD(&se->dobj.list); 10328a978234SLiam Girdwood 10338a978234SLiam Girdwood switch (ec->hdr.ops.info) { 10348a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 10358a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 10368a978234SLiam Girdwood err = soc_tplg_denum_create_values(se, ec); 10378a978234SLiam Girdwood if (err < 0) { 10388a978234SLiam Girdwood dev_err(tplg->dev, 10398a978234SLiam Girdwood "ASoC: could not create values for %s\n", 10408a978234SLiam Girdwood ec->hdr.name); 10418a978234SLiam Girdwood kfree(se); 10428a978234SLiam Girdwood continue; 10438a978234SLiam Girdwood } 10449c6c4d96STakashi Iwai /* fall through */ 10458a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 10468a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 10478a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 10488a978234SLiam Girdwood err = soc_tplg_denum_create_texts(se, ec); 10498a978234SLiam Girdwood if (err < 0) { 10508a978234SLiam Girdwood dev_err(tplg->dev, 10518a978234SLiam Girdwood "ASoC: could not create texts for %s\n", 10528a978234SLiam Girdwood ec->hdr.name); 10538a978234SLiam Girdwood kfree(se); 10548a978234SLiam Girdwood continue; 10558a978234SLiam Girdwood } 10568a978234SLiam Girdwood break; 10578a978234SLiam Girdwood default: 10588a978234SLiam Girdwood dev_err(tplg->dev, 10598a978234SLiam Girdwood "ASoC: invalid enum control type %d for %s\n", 10608a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 10618a978234SLiam Girdwood kfree(se); 10628a978234SLiam Girdwood continue; 10638a978234SLiam Girdwood } 10648a978234SLiam Girdwood 10658a978234SLiam Girdwood /* map io handlers */ 10662b5cdb91SMengdong Lin err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); 10678a978234SLiam Girdwood if (err) { 10688a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 10698a978234SLiam Girdwood kfree(se); 10708a978234SLiam Girdwood continue; 10718a978234SLiam Girdwood } 10728a978234SLiam Girdwood 10738a978234SLiam Girdwood /* pass control to driver for optional further init */ 10748a978234SLiam Girdwood err = soc_tplg_init_kcontrol(tplg, &kc, 10758a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *) ec); 10768a978234SLiam Girdwood if (err < 0) { 10778a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 10788a978234SLiam Girdwood ec->hdr.name); 10798a978234SLiam Girdwood kfree(se); 10808a978234SLiam Girdwood continue; 10818a978234SLiam Girdwood } 10828a978234SLiam Girdwood 10838a978234SLiam Girdwood /* register control here */ 10848a978234SLiam Girdwood ret = soc_tplg_add_kcontrol(tplg, 10858a978234SLiam Girdwood &kc, &se->dobj.control.kcontrol); 10868a978234SLiam Girdwood if (ret < 0) { 10878a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", 10888a978234SLiam Girdwood ec->hdr.name); 10898a978234SLiam Girdwood kfree(se); 10908a978234SLiam Girdwood continue; 10918a978234SLiam Girdwood } 10928a978234SLiam Girdwood 10938a978234SLiam Girdwood list_add(&se->dobj.list, &tplg->comp->dobj_list); 10948a978234SLiam Girdwood } 10958a978234SLiam Girdwood 10968a978234SLiam Girdwood return 0; 10978a978234SLiam Girdwood } 10988a978234SLiam Girdwood 10998a978234SLiam Girdwood static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, 11008a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 11018a978234SLiam Girdwood { 11028a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *control_hdr; 11038a978234SLiam Girdwood int i; 11048a978234SLiam Girdwood 11058a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_MIXER) { 11068a978234SLiam Girdwood tplg->pos += hdr->size + hdr->payload_size; 11078a978234SLiam Girdwood return 0; 11088a978234SLiam Girdwood } 11098a978234SLiam Girdwood 11108a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, 11118a978234SLiam Girdwood soc_tplg_get_offset(tplg)); 11128a978234SLiam Girdwood 11138a978234SLiam Girdwood for (i = 0; i < hdr->count; i++) { 11148a978234SLiam Girdwood 11158a978234SLiam Girdwood control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 11168a978234SLiam Girdwood 111706eb49f7SMengdong Lin if (control_hdr->size != sizeof(*control_hdr)) { 111806eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid control size\n"); 111906eb49f7SMengdong Lin return -EINVAL; 112006eb49f7SMengdong Lin } 112106eb49f7SMengdong Lin 11228a978234SLiam Girdwood switch (control_hdr->ops.info) { 11238a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 11248a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 11258a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 11268a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 11278a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 11288a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 11298a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_PIN: 11308a978234SLiam Girdwood soc_tplg_dmixer_create(tplg, 1, hdr->payload_size); 11318a978234SLiam Girdwood break; 11328a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 11338a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 11348a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 11358a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 11368a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 11378a978234SLiam Girdwood soc_tplg_denum_create(tplg, 1, hdr->payload_size); 11388a978234SLiam Girdwood break; 11398a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 11408a978234SLiam Girdwood soc_tplg_dbytes_create(tplg, 1, hdr->payload_size); 11418a978234SLiam Girdwood break; 11428a978234SLiam Girdwood default: 11438a978234SLiam Girdwood soc_bind_err(tplg, control_hdr, i); 11448a978234SLiam Girdwood return -EINVAL; 11458a978234SLiam Girdwood } 11468a978234SLiam Girdwood } 11478a978234SLiam Girdwood 11488a978234SLiam Girdwood return 0; 11498a978234SLiam Girdwood } 11508a978234SLiam Girdwood 1151503e79b7SLiam Girdwood /* optionally pass new dynamic kcontrol to component driver. */ 1152503e79b7SLiam Girdwood static int soc_tplg_add_route(struct soc_tplg *tplg, 1153503e79b7SLiam Girdwood struct snd_soc_dapm_route *route) 1154503e79b7SLiam Girdwood { 1155503e79b7SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load) 1156503e79b7SLiam Girdwood return tplg->ops->dapm_route_load(tplg->comp, tplg->index, 1157503e79b7SLiam Girdwood route); 1158503e79b7SLiam Girdwood 1159503e79b7SLiam Girdwood return 0; 1160503e79b7SLiam Girdwood } 1161503e79b7SLiam Girdwood 11628a978234SLiam Girdwood static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, 11638a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 11648a978234SLiam Girdwood { 11658a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 11668a978234SLiam Girdwood struct snd_soc_tplg_dapm_graph_elem *elem; 11677df04ea7SRanjani Sridharan struct snd_soc_dapm_route **routes; 11687df04ea7SRanjani Sridharan int count = hdr->count, i, j; 11697df04ea7SRanjani Sridharan int ret = 0; 11708a978234SLiam Girdwood 11718a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_GRAPH) { 11728a978234SLiam Girdwood tplg->pos += hdr->size + hdr->payload_size; 11738a978234SLiam Girdwood return 0; 11748a978234SLiam Girdwood } 11758a978234SLiam Girdwood 11768a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 11778a978234SLiam Girdwood sizeof(struct snd_soc_tplg_dapm_graph_elem), 11788a978234SLiam Girdwood count, hdr->payload_size, "graph")) { 11798a978234SLiam Girdwood 11808a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", 11818a978234SLiam Girdwood count); 11828a978234SLiam Girdwood return -EINVAL; 11838a978234SLiam Girdwood } 11848a978234SLiam Girdwood 1185b75a6511SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, 1186b75a6511SLiam Girdwood hdr->index); 11878a978234SLiam Girdwood 11887df04ea7SRanjani Sridharan /* allocate memory for pointer to array of dapm routes */ 11897df04ea7SRanjani Sridharan routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *), 11907df04ea7SRanjani Sridharan GFP_KERNEL); 11917df04ea7SRanjani Sridharan if (!routes) 11927df04ea7SRanjani Sridharan return -ENOMEM; 11937df04ea7SRanjani Sridharan 11947df04ea7SRanjani Sridharan /* 11957df04ea7SRanjani Sridharan * allocate memory for each dapm route in the array. 11967df04ea7SRanjani Sridharan * This needs to be done individually so that 11977df04ea7SRanjani Sridharan * each route can be freed when it is removed in remove_route(). 11987df04ea7SRanjani Sridharan */ 11997df04ea7SRanjani Sridharan for (i = 0; i < count; i++) { 12007df04ea7SRanjani Sridharan routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL); 12017df04ea7SRanjani Sridharan if (!routes[i]) { 12027df04ea7SRanjani Sridharan /* free previously allocated memory */ 12037df04ea7SRanjani Sridharan for (j = 0; j < i; j++) 12047df04ea7SRanjani Sridharan kfree(routes[j]); 12057df04ea7SRanjani Sridharan 12067df04ea7SRanjani Sridharan kfree(routes); 12077df04ea7SRanjani Sridharan return -ENOMEM; 12087df04ea7SRanjani Sridharan } 12097df04ea7SRanjani Sridharan } 12107df04ea7SRanjani Sridharan 12118a978234SLiam Girdwood for (i = 0; i < count; i++) { 12128a978234SLiam Girdwood elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; 12138a978234SLiam Girdwood tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); 12148a978234SLiam Girdwood 12158a978234SLiam Girdwood /* validate routes */ 12168a978234SLiam Girdwood if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12177df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 12187df04ea7SRanjani Sridharan ret = -EINVAL; 12197df04ea7SRanjani Sridharan break; 12207df04ea7SRanjani Sridharan } 12218a978234SLiam Girdwood if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12227df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 12237df04ea7SRanjani Sridharan ret = -EINVAL; 12247df04ea7SRanjani Sridharan break; 12257df04ea7SRanjani Sridharan } 12268a978234SLiam Girdwood if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12277df04ea7SRanjani Sridharan SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 12287df04ea7SRanjani Sridharan ret = -EINVAL; 12297df04ea7SRanjani Sridharan break; 12308a978234SLiam Girdwood } 12318a978234SLiam Girdwood 12327df04ea7SRanjani Sridharan routes[i]->source = elem->source; 12337df04ea7SRanjani Sridharan routes[i]->sink = elem->sink; 12347df04ea7SRanjani Sridharan 12357df04ea7SRanjani Sridharan /* set to NULL atm for tplg users */ 12367df04ea7SRanjani Sridharan routes[i]->connected = NULL; 12377df04ea7SRanjani Sridharan if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) 12387df04ea7SRanjani Sridharan routes[i]->control = NULL; 12397df04ea7SRanjani Sridharan else 12407df04ea7SRanjani Sridharan routes[i]->control = elem->control; 12417df04ea7SRanjani Sridharan 12427df04ea7SRanjani Sridharan /* add route dobj to dobj_list */ 12437df04ea7SRanjani Sridharan routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH; 12447df04ea7SRanjani Sridharan routes[i]->dobj.ops = tplg->ops; 12457df04ea7SRanjani Sridharan routes[i]->dobj.index = tplg->index; 12467df04ea7SRanjani Sridharan list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list); 12477df04ea7SRanjani Sridharan 12487df04ea7SRanjani Sridharan soc_tplg_add_route(tplg, routes[i]); 12497df04ea7SRanjani Sridharan 12507df04ea7SRanjani Sridharan /* add route, but keep going if some fail */ 12517df04ea7SRanjani Sridharan snd_soc_dapm_add_routes(dapm, routes[i], 1); 12527df04ea7SRanjani Sridharan } 12537df04ea7SRanjani Sridharan 12547df04ea7SRanjani Sridharan /* free memory allocated for all dapm routes in case of error */ 12557df04ea7SRanjani Sridharan if (ret < 0) 12567df04ea7SRanjani Sridharan for (i = 0; i < count ; i++) 12577df04ea7SRanjani Sridharan kfree(routes[i]); 12587df04ea7SRanjani Sridharan 12597df04ea7SRanjani Sridharan /* 12607df04ea7SRanjani Sridharan * free pointer to array of dapm routes as this is no longer needed. 12617df04ea7SRanjani Sridharan * The memory allocated for each dapm route will be freed 12627df04ea7SRanjani Sridharan * when it is removed in remove_route(). 12637df04ea7SRanjani Sridharan */ 12647df04ea7SRanjani Sridharan kfree(routes); 12657df04ea7SRanjani Sridharan 12667df04ea7SRanjani Sridharan return ret; 12678a978234SLiam Girdwood } 12688a978234SLiam Girdwood 12698a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( 12708a978234SLiam Girdwood struct soc_tplg *tplg, int num_kcontrols) 12718a978234SLiam Girdwood { 12728a978234SLiam Girdwood struct snd_kcontrol_new *kc; 12738a978234SLiam Girdwood struct soc_mixer_control *sm; 12748a978234SLiam Girdwood struct snd_soc_tplg_mixer_control *mc; 12758a978234SLiam Girdwood int i, err; 12768a978234SLiam Girdwood 12774ca7deb1SAxel Lin kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); 12788a978234SLiam Girdwood if (kc == NULL) 12798a978234SLiam Girdwood return NULL; 12808a978234SLiam Girdwood 12818a978234SLiam Girdwood for (i = 0; i < num_kcontrols; i++) { 12828a978234SLiam Girdwood mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 12838a978234SLiam Girdwood sm = kzalloc(sizeof(*sm), GFP_KERNEL); 12848a978234SLiam Girdwood if (sm == NULL) 12858a978234SLiam Girdwood goto err; 12868a978234SLiam Girdwood 12878a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + 12888a978234SLiam Girdwood mc->priv.size); 12898a978234SLiam Girdwood 12908a978234SLiam Girdwood /* validate kcontrol */ 12918a978234SLiam Girdwood if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 12928a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 12938a978234SLiam Girdwood goto err_str; 12948a978234SLiam Girdwood 12958a978234SLiam Girdwood dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", 12968a978234SLiam Girdwood mc->hdr.name, i); 12978a978234SLiam Girdwood 1298267e2c6fSLiam Girdwood kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL); 1299267e2c6fSLiam Girdwood if (kc[i].name == NULL) 1300267e2c6fSLiam Girdwood goto err_str; 13018a978234SLiam Girdwood kc[i].private_value = (long)sm; 13028a978234SLiam Girdwood kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 13038a978234SLiam Girdwood kc[i].access = mc->hdr.access; 13048a978234SLiam Girdwood 13058a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 13068a978234SLiam Girdwood sm->reg = tplc_chan_get_reg(tplg, mc->channel, 13078a978234SLiam Girdwood SNDRV_CHMAP_FL); 13088a978234SLiam Girdwood sm->rreg = tplc_chan_get_reg(tplg, mc->channel, 13098a978234SLiam Girdwood SNDRV_CHMAP_FR); 13108a978234SLiam Girdwood sm->shift = tplc_chan_get_shift(tplg, mc->channel, 13118a978234SLiam Girdwood SNDRV_CHMAP_FL); 13128a978234SLiam Girdwood sm->rshift = tplc_chan_get_shift(tplg, mc->channel, 13138a978234SLiam Girdwood SNDRV_CHMAP_FR); 13148a978234SLiam Girdwood 13158a978234SLiam Girdwood sm->max = mc->max; 13168a978234SLiam Girdwood sm->min = mc->min; 13178a978234SLiam Girdwood sm->invert = mc->invert; 13188a978234SLiam Girdwood sm->platform_max = mc->platform_max; 13198a978234SLiam Girdwood sm->dobj.index = tplg->index; 13208a978234SLiam Girdwood INIT_LIST_HEAD(&sm->dobj.list); 13218a978234SLiam Girdwood 13228a978234SLiam Girdwood /* map io handlers */ 13232b5cdb91SMengdong Lin err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); 13248a978234SLiam Girdwood if (err) { 13258a978234SLiam Girdwood soc_control_err(tplg, &mc->hdr, mc->hdr.name); 13268a978234SLiam Girdwood kfree(sm); 13278a978234SLiam Girdwood continue; 13288a978234SLiam Girdwood } 13298a978234SLiam Girdwood 13303789debfSBard liao /* create any TLV data */ 13313789debfSBard liao soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr); 13323789debfSBard liao 13338a978234SLiam Girdwood /* pass control to driver for optional further init */ 13348a978234SLiam Girdwood err = soc_tplg_init_kcontrol(tplg, &kc[i], 13358a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *)mc); 13368a978234SLiam Girdwood if (err < 0) { 13378a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 13388a978234SLiam Girdwood mc->hdr.name); 13393789debfSBard liao soc_tplg_free_tlv(tplg, &kc[i]); 13408a978234SLiam Girdwood kfree(sm); 13418a978234SLiam Girdwood continue; 13428a978234SLiam Girdwood } 13438a978234SLiam Girdwood } 13448a978234SLiam Girdwood return kc; 13458a978234SLiam Girdwood 13468a978234SLiam Girdwood err_str: 13478a978234SLiam Girdwood kfree(sm); 13488a978234SLiam Girdwood err: 1349267e2c6fSLiam Girdwood for (--i; i >= 0; i--) { 13508a978234SLiam Girdwood kfree((void *)kc[i].private_value); 1351267e2c6fSLiam Girdwood kfree(kc[i].name); 1352267e2c6fSLiam Girdwood } 13538a978234SLiam Girdwood kfree(kc); 13548a978234SLiam Girdwood return NULL; 13558a978234SLiam Girdwood } 13568a978234SLiam Girdwood 13578a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( 13581a7dd6e2SMengdong Lin struct soc_tplg *tplg, int num_kcontrols) 13598a978234SLiam Girdwood { 13608a978234SLiam Girdwood struct snd_kcontrol_new *kc; 13618a978234SLiam Girdwood struct snd_soc_tplg_enum_control *ec; 13628a978234SLiam Girdwood struct soc_enum *se; 13631a7dd6e2SMengdong Lin int i, j, err; 13648a978234SLiam Girdwood 13651a7dd6e2SMengdong Lin kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); 13661a7dd6e2SMengdong Lin if (kc == NULL) 13671a7dd6e2SMengdong Lin return NULL; 13681a7dd6e2SMengdong Lin 13691a7dd6e2SMengdong Lin for (i = 0; i < num_kcontrols; i++) { 13708a978234SLiam Girdwood ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 13718a978234SLiam Girdwood /* validate kcontrol */ 13728a978234SLiam Girdwood if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 13738a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1374f3ee9096SChristophe JAILLET goto err; 13758a978234SLiam Girdwood 13768a978234SLiam Girdwood se = kzalloc(sizeof(*se), GFP_KERNEL); 13778a978234SLiam Girdwood if (se == NULL) 13788a978234SLiam Girdwood goto err; 13798a978234SLiam Girdwood 13808a978234SLiam Girdwood dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", 13818a978234SLiam Girdwood ec->hdr.name); 13828a978234SLiam Girdwood 1383267e2c6fSLiam Girdwood kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL); 138465030ff3SDan Carpenter if (kc[i].name == NULL) { 138565030ff3SDan Carpenter kfree(se); 1386267e2c6fSLiam Girdwood goto err_se; 138765030ff3SDan Carpenter } 13881a7dd6e2SMengdong Lin kc[i].private_value = (long)se; 13891a7dd6e2SMengdong Lin kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 13901a7dd6e2SMengdong Lin kc[i].access = ec->hdr.access; 13918a978234SLiam Girdwood 13928a978234SLiam Girdwood /* we only support FL/FR channel mapping atm */ 13938a978234SLiam Girdwood se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 13941a7dd6e2SMengdong Lin se->shift_l = tplc_chan_get_shift(tplg, ec->channel, 13951a7dd6e2SMengdong Lin SNDRV_CHMAP_FL); 13961a7dd6e2SMengdong Lin se->shift_r = tplc_chan_get_shift(tplg, ec->channel, 13971a7dd6e2SMengdong Lin SNDRV_CHMAP_FR); 13988a978234SLiam Girdwood 13998a978234SLiam Girdwood se->items = ec->items; 14008a978234SLiam Girdwood se->mask = ec->mask; 14018a978234SLiam Girdwood se->dobj.index = tplg->index; 14028a978234SLiam Girdwood 14038a978234SLiam Girdwood switch (ec->hdr.ops.info) { 14048a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 14058a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 14068a978234SLiam Girdwood err = soc_tplg_denum_create_values(se, ec); 14078a978234SLiam Girdwood if (err < 0) { 14088a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create values for %s\n", 14098a978234SLiam Girdwood ec->hdr.name); 14108a978234SLiam Girdwood goto err_se; 14118a978234SLiam Girdwood } 14129c6c4d96STakashi Iwai /* fall through */ 14138a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 14148a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 14158a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 14168a978234SLiam Girdwood err = soc_tplg_denum_create_texts(se, ec); 14178a978234SLiam Girdwood if (err < 0) { 14188a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: could not create texts for %s\n", 14198a978234SLiam Girdwood ec->hdr.name); 14208a978234SLiam Girdwood goto err_se; 14218a978234SLiam Girdwood } 14228a978234SLiam Girdwood break; 14238a978234SLiam Girdwood default: 14248a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", 14258a978234SLiam Girdwood ec->hdr.ops.info, ec->hdr.name); 14268a978234SLiam Girdwood goto err_se; 14278a978234SLiam Girdwood } 14288a978234SLiam Girdwood 14298a978234SLiam Girdwood /* map io handlers */ 14301a7dd6e2SMengdong Lin err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg); 14318a978234SLiam Girdwood if (err) { 14328a978234SLiam Girdwood soc_control_err(tplg, &ec->hdr, ec->hdr.name); 14338a978234SLiam Girdwood goto err_se; 14348a978234SLiam Girdwood } 14358a978234SLiam Girdwood 14368a978234SLiam Girdwood /* pass control to driver for optional further init */ 14371a7dd6e2SMengdong Lin err = soc_tplg_init_kcontrol(tplg, &kc[i], 14388a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *)ec); 14398a978234SLiam Girdwood if (err < 0) { 14408a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 14418a978234SLiam Girdwood ec->hdr.name); 14428a978234SLiam Girdwood goto err_se; 14438a978234SLiam Girdwood } 14448a978234SLiam Girdwood 14451a7dd6e2SMengdong Lin tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + 14461a7dd6e2SMengdong Lin ec->priv.size); 14471a7dd6e2SMengdong Lin } 14481a7dd6e2SMengdong Lin 14498a978234SLiam Girdwood return kc; 14508a978234SLiam Girdwood 14518a978234SLiam Girdwood err_se: 14521a7dd6e2SMengdong Lin for (; i >= 0; i--) { 14538a978234SLiam Girdwood /* free values and texts */ 14541a7dd6e2SMengdong Lin se = (struct soc_enum *)kc[i].private_value; 14556d5574edSChristophe JAILLET if (!se) 14566d5574edSChristophe JAILLET continue; 14576d5574edSChristophe JAILLET 14588a978234SLiam Girdwood kfree(se->dobj.control.dvalues); 14591a7dd6e2SMengdong Lin for (j = 0; j < ec->items; j++) 14601a7dd6e2SMengdong Lin kfree(se->dobj.control.dtexts[j]); 146134db6a3eSAmadeusz Sławiński kfree(se->dobj.control.dtexts); 14628a978234SLiam Girdwood 14638a978234SLiam Girdwood kfree(se); 1464267e2c6fSLiam Girdwood kfree(kc[i].name); 14651a7dd6e2SMengdong Lin } 14668a978234SLiam Girdwood err: 14678a978234SLiam Girdwood kfree(kc); 14688a978234SLiam Girdwood 14698a978234SLiam Girdwood return NULL; 14708a978234SLiam Girdwood } 14718a978234SLiam Girdwood 14728a978234SLiam Girdwood static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( 14738a978234SLiam Girdwood struct soc_tplg *tplg, int count) 14748a978234SLiam Girdwood { 14758a978234SLiam Girdwood struct snd_soc_tplg_bytes_control *be; 14768a978234SLiam Girdwood struct soc_bytes_ext *sbe; 14778a978234SLiam Girdwood struct snd_kcontrol_new *kc; 14788a978234SLiam Girdwood int i, err; 14798a978234SLiam Girdwood 14804ca7deb1SAxel Lin kc = kcalloc(count, sizeof(*kc), GFP_KERNEL); 14818a978234SLiam Girdwood if (!kc) 14828a978234SLiam Girdwood return NULL; 14838a978234SLiam Girdwood 14848a978234SLiam Girdwood for (i = 0; i < count; i++) { 14858a978234SLiam Girdwood be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 14868a978234SLiam Girdwood 14878a978234SLiam Girdwood /* validate kcontrol */ 14888a978234SLiam Girdwood if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 14898a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 14908a978234SLiam Girdwood goto err; 14918a978234SLiam Girdwood 14928a978234SLiam Girdwood sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); 14938a978234SLiam Girdwood if (sbe == NULL) 14948a978234SLiam Girdwood goto err; 14958a978234SLiam Girdwood 14968a978234SLiam Girdwood tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + 14978a978234SLiam Girdwood be->priv.size); 14988a978234SLiam Girdwood 14998a978234SLiam Girdwood dev_dbg(tplg->dev, 15008a978234SLiam Girdwood "ASoC: adding bytes kcontrol %s with access 0x%x\n", 15018a978234SLiam Girdwood be->hdr.name, be->hdr.access); 15028a978234SLiam Girdwood 1503267e2c6fSLiam Girdwood kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL); 150465030ff3SDan Carpenter if (kc[i].name == NULL) { 150565030ff3SDan Carpenter kfree(sbe); 1506267e2c6fSLiam Girdwood goto err; 150765030ff3SDan Carpenter } 15088a978234SLiam Girdwood kc[i].private_value = (long)sbe; 15098a978234SLiam Girdwood kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 15108a978234SLiam Girdwood kc[i].access = be->hdr.access; 15118a978234SLiam Girdwood 15128a978234SLiam Girdwood sbe->max = be->max; 15138a978234SLiam Girdwood INIT_LIST_HEAD(&sbe->dobj.list); 15148a978234SLiam Girdwood 15158a978234SLiam Girdwood /* map standard io handlers and check for external handlers */ 15162b5cdb91SMengdong Lin err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); 15178a978234SLiam Girdwood if (err) { 15188a978234SLiam Girdwood soc_control_err(tplg, &be->hdr, be->hdr.name); 15198a978234SLiam Girdwood kfree(sbe); 15208a978234SLiam Girdwood continue; 15218a978234SLiam Girdwood } 15228a978234SLiam Girdwood 15238a978234SLiam Girdwood /* pass control to driver for optional further init */ 15248a978234SLiam Girdwood err = soc_tplg_init_kcontrol(tplg, &kc[i], 15258a978234SLiam Girdwood (struct snd_soc_tplg_ctl_hdr *)be); 15268a978234SLiam Girdwood if (err < 0) { 15278a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to init %s\n", 15288a978234SLiam Girdwood be->hdr.name); 15298a978234SLiam Girdwood kfree(sbe); 15308a978234SLiam Girdwood continue; 15318a978234SLiam Girdwood } 15328a978234SLiam Girdwood } 15338a978234SLiam Girdwood 15348a978234SLiam Girdwood return kc; 15358a978234SLiam Girdwood 15368a978234SLiam Girdwood err: 1537267e2c6fSLiam Girdwood for (--i; i >= 0; i--) { 15388a978234SLiam Girdwood kfree((void *)kc[i].private_value); 1539267e2c6fSLiam Girdwood kfree(kc[i].name); 1540267e2c6fSLiam Girdwood } 15418a978234SLiam Girdwood 15428a978234SLiam Girdwood kfree(kc); 15438a978234SLiam Girdwood return NULL; 15448a978234SLiam Girdwood } 15458a978234SLiam Girdwood 15468a978234SLiam Girdwood static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, 15478a978234SLiam Girdwood struct snd_soc_tplg_dapm_widget *w) 15488a978234SLiam Girdwood { 15498a978234SLiam Girdwood struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; 15508a978234SLiam Girdwood struct snd_soc_dapm_widget template, *widget; 15518a978234SLiam Girdwood struct snd_soc_tplg_ctl_hdr *control_hdr; 15528a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 1553eea3dd4fSMengdong Lin unsigned int kcontrol_type; 15548a978234SLiam Girdwood int ret = 0; 15558a978234SLiam Girdwood 15568a978234SLiam Girdwood if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 15578a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 15588a978234SLiam Girdwood return -EINVAL; 15598a978234SLiam Girdwood if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 15608a978234SLiam Girdwood SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 15618a978234SLiam Girdwood return -EINVAL; 15628a978234SLiam Girdwood 15638a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", 15648a978234SLiam Girdwood w->name, w->id); 15658a978234SLiam Girdwood 15668a978234SLiam Girdwood memset(&template, 0, sizeof(template)); 15678a978234SLiam Girdwood 15688a978234SLiam Girdwood /* map user to kernel widget ID */ 15698a978234SLiam Girdwood template.id = get_widget_id(w->id); 15708a978234SLiam Girdwood if (template.id < 0) 15718a978234SLiam Girdwood return template.id; 15728a978234SLiam Girdwood 1573c3421a6aSLiam Girdwood /* strings are allocated here, but used and freed by the widget */ 15748a978234SLiam Girdwood template.name = kstrdup(w->name, GFP_KERNEL); 15758a978234SLiam Girdwood if (!template.name) 15768a978234SLiam Girdwood return -ENOMEM; 15778a978234SLiam Girdwood template.sname = kstrdup(w->sname, GFP_KERNEL); 15788a978234SLiam Girdwood if (!template.sname) { 15798a978234SLiam Girdwood ret = -ENOMEM; 15808a978234SLiam Girdwood goto err; 15818a978234SLiam Girdwood } 15828a978234SLiam Girdwood template.reg = w->reg; 15838a978234SLiam Girdwood template.shift = w->shift; 15848a978234SLiam Girdwood template.mask = w->mask; 15856dc6db79SSubhransu S. Prusty template.subseq = w->subseq; 15868a978234SLiam Girdwood template.on_val = w->invert ? 0 : 1; 15878a978234SLiam Girdwood template.off_val = w->invert ? 1 : 0; 15888a978234SLiam Girdwood template.ignore_suspend = w->ignore_suspend; 15898a978234SLiam Girdwood template.event_flags = w->event_flags; 15908a978234SLiam Girdwood template.dobj.index = tplg->index; 15918a978234SLiam Girdwood 15928a978234SLiam Girdwood tplg->pos += 15938a978234SLiam Girdwood (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size); 15948a978234SLiam Girdwood if (w->num_kcontrols == 0) { 1595dd5abb74SArnd Bergmann kcontrol_type = 0; 15968a978234SLiam Girdwood template.num_kcontrols = 0; 15978a978234SLiam Girdwood goto widget; 15988a978234SLiam Girdwood } 15998a978234SLiam Girdwood 16008a978234SLiam Girdwood control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 16018a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", 16028a978234SLiam Girdwood w->name, w->num_kcontrols, control_hdr->type); 16038a978234SLiam Girdwood 16048a978234SLiam Girdwood switch (control_hdr->ops.info) { 16058a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW: 16068a978234SLiam Girdwood case SND_SOC_TPLG_CTL_STROBE: 16078a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_SX: 16088a978234SLiam Girdwood case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 16098a978234SLiam Girdwood case SND_SOC_TPLG_CTL_RANGE: 16108a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_VOLSW: 1611eea3dd4fSMengdong Lin kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */ 16128a978234SLiam Girdwood template.num_kcontrols = w->num_kcontrols; 16138a978234SLiam Girdwood template.kcontrol_news = 16148a978234SLiam Girdwood soc_tplg_dapm_widget_dmixer_create(tplg, 16158a978234SLiam Girdwood template.num_kcontrols); 16168a978234SLiam Girdwood if (!template.kcontrol_news) { 16178a978234SLiam Girdwood ret = -ENOMEM; 16188a978234SLiam Girdwood goto hdr_err; 16198a978234SLiam Girdwood } 16208a978234SLiam Girdwood break; 16218a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM: 16228a978234SLiam Girdwood case SND_SOC_TPLG_CTL_ENUM_VALUE: 16238a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 16248a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 16258a978234SLiam Girdwood case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1626eea3dd4fSMengdong Lin kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */ 16271a7dd6e2SMengdong Lin template.num_kcontrols = w->num_kcontrols; 16288a978234SLiam Girdwood template.kcontrol_news = 16291a7dd6e2SMengdong Lin soc_tplg_dapm_widget_denum_create(tplg, 16301a7dd6e2SMengdong Lin template.num_kcontrols); 16318a978234SLiam Girdwood if (!template.kcontrol_news) { 16328a978234SLiam Girdwood ret = -ENOMEM; 16338a978234SLiam Girdwood goto hdr_err; 16348a978234SLiam Girdwood } 16358a978234SLiam Girdwood break; 16368a978234SLiam Girdwood case SND_SOC_TPLG_CTL_BYTES: 1637eea3dd4fSMengdong Lin kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */ 16388a978234SLiam Girdwood template.num_kcontrols = w->num_kcontrols; 16398a978234SLiam Girdwood template.kcontrol_news = 16408a978234SLiam Girdwood soc_tplg_dapm_widget_dbytes_create(tplg, 16418a978234SLiam Girdwood template.num_kcontrols); 16428a978234SLiam Girdwood if (!template.kcontrol_news) { 16438a978234SLiam Girdwood ret = -ENOMEM; 16448a978234SLiam Girdwood goto hdr_err; 16458a978234SLiam Girdwood } 16468a978234SLiam Girdwood break; 16478a978234SLiam Girdwood default: 16488a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", 16498a978234SLiam Girdwood control_hdr->ops.get, control_hdr->ops.put, 16508a978234SLiam Girdwood control_hdr->ops.info); 16518a978234SLiam Girdwood ret = -EINVAL; 16528a978234SLiam Girdwood goto hdr_err; 16538a978234SLiam Girdwood } 16548a978234SLiam Girdwood 16558a978234SLiam Girdwood widget: 16568a978234SLiam Girdwood ret = soc_tplg_widget_load(tplg, &template, w); 16578a978234SLiam Girdwood if (ret < 0) 16588a978234SLiam Girdwood goto hdr_err; 16598a978234SLiam Girdwood 16608a978234SLiam Girdwood /* card dapm mutex is held by the core if we are loading topology 16618a978234SLiam Girdwood * data during sound card init. */ 16628a978234SLiam Girdwood if (card->instantiated) 16638a978234SLiam Girdwood widget = snd_soc_dapm_new_control(dapm, &template); 16648a978234SLiam Girdwood else 16658a978234SLiam Girdwood widget = snd_soc_dapm_new_control_unlocked(dapm, &template); 166637e1df8cSLinus Walleij if (IS_ERR(widget)) { 166737e1df8cSLinus Walleij ret = PTR_ERR(widget); 16688a978234SLiam Girdwood goto hdr_err; 16698a978234SLiam Girdwood } 16708a978234SLiam Girdwood 16718a978234SLiam Girdwood widget->dobj.type = SND_SOC_DOBJ_WIDGET; 1672eea3dd4fSMengdong Lin widget->dobj.widget.kcontrol_type = kcontrol_type; 16738a978234SLiam Girdwood widget->dobj.ops = tplg->ops; 16748a978234SLiam Girdwood widget->dobj.index = tplg->index; 16758a978234SLiam Girdwood list_add(&widget->dobj.list, &tplg->comp->dobj_list); 1676ebd259d3SLiam Girdwood 1677ebd259d3SLiam Girdwood ret = soc_tplg_widget_ready(tplg, widget, w); 1678ebd259d3SLiam Girdwood if (ret < 0) 1679ebd259d3SLiam Girdwood goto ready_err; 1680ebd259d3SLiam Girdwood 16817620fe91SBard liao kfree(template.sname); 16827620fe91SBard liao kfree(template.name); 16837620fe91SBard liao 16848a978234SLiam Girdwood return 0; 16858a978234SLiam Girdwood 1686ebd259d3SLiam Girdwood ready_err: 1687ebd259d3SLiam Girdwood snd_soc_tplg_widget_remove(widget); 1688ebd259d3SLiam Girdwood snd_soc_dapm_free_widget(widget); 16898a978234SLiam Girdwood hdr_err: 16908a978234SLiam Girdwood kfree(template.sname); 16918a978234SLiam Girdwood err: 16928a978234SLiam Girdwood kfree(template.name); 16938a978234SLiam Girdwood return ret; 16948a978234SLiam Girdwood } 16958a978234SLiam Girdwood 16968a978234SLiam Girdwood static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, 16978a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 16988a978234SLiam Girdwood { 16998a978234SLiam Girdwood struct snd_soc_tplg_dapm_widget *widget; 17008a978234SLiam Girdwood int ret, count = hdr->count, i; 17018a978234SLiam Girdwood 17028a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_WIDGET) 17038a978234SLiam Girdwood return 0; 17048a978234SLiam Girdwood 17058a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); 17068a978234SLiam Girdwood 17078a978234SLiam Girdwood for (i = 0; i < count; i++) { 17088a978234SLiam Girdwood widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; 170906eb49f7SMengdong Lin if (widget->size != sizeof(*widget)) { 171006eb49f7SMengdong Lin dev_err(tplg->dev, "ASoC: invalid widget size\n"); 171106eb49f7SMengdong Lin return -EINVAL; 171206eb49f7SMengdong Lin } 171306eb49f7SMengdong Lin 17148a978234SLiam Girdwood ret = soc_tplg_dapm_widget_create(tplg, widget); 17157de76b62SMengdong Lin if (ret < 0) { 17168a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to load widget %s\n", 17178a978234SLiam Girdwood widget->name); 17187de76b62SMengdong Lin return ret; 17197de76b62SMengdong Lin } 17208a978234SLiam Girdwood } 17218a978234SLiam Girdwood 17228a978234SLiam Girdwood return 0; 17238a978234SLiam Girdwood } 17248a978234SLiam Girdwood 17258a978234SLiam Girdwood static int soc_tplg_dapm_complete(struct soc_tplg *tplg) 17268a978234SLiam Girdwood { 17278a978234SLiam Girdwood struct snd_soc_card *card = tplg->comp->card; 17288a978234SLiam Girdwood int ret; 17298a978234SLiam Girdwood 17308a978234SLiam Girdwood /* Card might not have been registered at this point. 17318a978234SLiam Girdwood * If so, just return success. 17328a978234SLiam Girdwood */ 17338a978234SLiam Girdwood if (!card || !card->instantiated) { 17348a978234SLiam Girdwood dev_warn(tplg->dev, "ASoC: Parent card not yet available," 1735cc9d4714SLiam Girdwood " widget card binding deferred\n"); 17368a978234SLiam Girdwood return 0; 17378a978234SLiam Girdwood } 17388a978234SLiam Girdwood 17398a978234SLiam Girdwood ret = snd_soc_dapm_new_widgets(card); 17408a978234SLiam Girdwood if (ret < 0) 17418a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", 17428a978234SLiam Girdwood ret); 17438a978234SLiam Girdwood 17448a978234SLiam Girdwood return 0; 17458a978234SLiam Girdwood } 17468a978234SLiam Girdwood 1747b6b6e4d6SMengdong Lin static void set_stream_info(struct snd_soc_pcm_stream *stream, 1748b6b6e4d6SMengdong Lin struct snd_soc_tplg_stream_caps *caps) 1749b6b6e4d6SMengdong Lin { 1750b6b6e4d6SMengdong Lin stream->stream_name = kstrdup(caps->name, GFP_KERNEL); 1751b6b6e4d6SMengdong Lin stream->channels_min = caps->channels_min; 1752b6b6e4d6SMengdong Lin stream->channels_max = caps->channels_max; 1753b6b6e4d6SMengdong Lin stream->rates = caps->rates; 1754b6b6e4d6SMengdong Lin stream->rate_min = caps->rate_min; 1755b6b6e4d6SMengdong Lin stream->rate_max = caps->rate_max; 1756b6b6e4d6SMengdong Lin stream->formats = caps->formats; 1757f918e169SMengdong Lin stream->sig_bits = caps->sig_bits; 1758b6b6e4d6SMengdong Lin } 1759b6b6e4d6SMengdong Lin 17600038be9aSMengdong Lin static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, 17610038be9aSMengdong Lin unsigned int flag_mask, unsigned int flags) 17620038be9aSMengdong Lin { 17630038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) 17640038be9aSMengdong Lin dai_drv->symmetric_rates = 17650038be9aSMengdong Lin flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0; 17660038be9aSMengdong Lin 17670038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) 17680038be9aSMengdong Lin dai_drv->symmetric_channels = 17690038be9aSMengdong Lin flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ? 17700038be9aSMengdong Lin 1 : 0; 17710038be9aSMengdong Lin 17720038be9aSMengdong Lin if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) 17730038be9aSMengdong Lin dai_drv->symmetric_samplebits = 17740038be9aSMengdong Lin flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ? 17750038be9aSMengdong Lin 1 : 0; 17760038be9aSMengdong Lin } 17770038be9aSMengdong Lin 177864527e8aSMengdong Lin static int soc_tplg_dai_create(struct soc_tplg *tplg, 177964527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 178064527e8aSMengdong Lin { 178164527e8aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 178264527e8aSMengdong Lin struct snd_soc_pcm_stream *stream; 178364527e8aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 178464527e8aSMengdong Lin int ret; 178564527e8aSMengdong Lin 178664527e8aSMengdong Lin dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); 178764527e8aSMengdong Lin if (dai_drv == NULL) 178864527e8aSMengdong Lin return -ENOMEM; 178964527e8aSMengdong Lin 17908f27c4abSMengdong Lin if (strlen(pcm->dai_name)) 17918f27c4abSMengdong Lin dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL); 179264527e8aSMengdong Lin dai_drv->id = pcm->dai_id; 179364527e8aSMengdong Lin 179464527e8aSMengdong Lin if (pcm->playback) { 179564527e8aSMengdong Lin stream = &dai_drv->playback; 179664527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 1797b6b6e4d6SMengdong Lin set_stream_info(stream, caps); 179864527e8aSMengdong Lin } 179964527e8aSMengdong Lin 180064527e8aSMengdong Lin if (pcm->capture) { 180164527e8aSMengdong Lin stream = &dai_drv->capture; 180264527e8aSMengdong Lin caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 1803b6b6e4d6SMengdong Lin set_stream_info(stream, caps); 180464527e8aSMengdong Lin } 180564527e8aSMengdong Lin 18065db6aab6SLiam Girdwood if (pcm->compress) 18075db6aab6SLiam Girdwood dai_drv->compress_new = snd_soc_new_compress; 18085db6aab6SLiam Girdwood 180964527e8aSMengdong Lin /* pass control to component driver for optional further init */ 1810c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); 181164527e8aSMengdong Lin if (ret < 0) { 181264527e8aSMengdong Lin dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); 1813*7b6f68a4SBard liao kfree(dai_drv->playback.stream_name); 1814*7b6f68a4SBard liao kfree(dai_drv->capture.stream_name); 1815*7b6f68a4SBard liao kfree(dai_drv->name); 181664527e8aSMengdong Lin kfree(dai_drv); 181764527e8aSMengdong Lin return ret; 181864527e8aSMengdong Lin } 181964527e8aSMengdong Lin 182064527e8aSMengdong Lin dai_drv->dobj.index = tplg->index; 182164527e8aSMengdong Lin dai_drv->dobj.ops = tplg->ops; 182264527e8aSMengdong Lin dai_drv->dobj.type = SND_SOC_DOBJ_PCM; 182364527e8aSMengdong Lin list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); 182464527e8aSMengdong Lin 182564527e8aSMengdong Lin /* register the DAI to the component */ 182664527e8aSMengdong Lin return snd_soc_register_dai(tplg->comp, dai_drv); 182764527e8aSMengdong Lin } 182864527e8aSMengdong Lin 1829717a8e72SMengdong Lin static void set_link_flags(struct snd_soc_dai_link *link, 1830717a8e72SMengdong Lin unsigned int flag_mask, unsigned int flags) 1831717a8e72SMengdong Lin { 1832717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) 1833717a8e72SMengdong Lin link->symmetric_rates = 1834717a8e72SMengdong Lin flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0; 1835717a8e72SMengdong Lin 1836717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) 1837717a8e72SMengdong Lin link->symmetric_channels = 1838717a8e72SMengdong Lin flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ? 1839717a8e72SMengdong Lin 1 : 0; 1840717a8e72SMengdong Lin 1841717a8e72SMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) 1842717a8e72SMengdong Lin link->symmetric_samplebits = 1843717a8e72SMengdong Lin flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ? 1844717a8e72SMengdong Lin 1 : 0; 18456ff67ccaSMengdong Lin 18466ff67ccaSMengdong Lin if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) 18476ff67ccaSMengdong Lin link->ignore_suspend = 18486ff67ccaSMengdong Lin flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ? 18496ff67ccaSMengdong Lin 1 : 0; 1850717a8e72SMengdong Lin } 1851717a8e72SMengdong Lin 185267d1c21eSGuneshwor Singh /* create the FE DAI link */ 1853ab4bc5eeSMengdong Lin static int soc_tplg_fe_link_create(struct soc_tplg *tplg, 1854acfc7d46SMengdong Lin struct snd_soc_tplg_pcm *pcm) 1855acfc7d46SMengdong Lin { 1856acfc7d46SMengdong Lin struct snd_soc_dai_link *link; 1857acfc7d46SMengdong Lin int ret; 1858acfc7d46SMengdong Lin 1859acfc7d46SMengdong Lin link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL); 1860acfc7d46SMengdong Lin if (link == NULL) 1861acfc7d46SMengdong Lin return -ENOMEM; 1862acfc7d46SMengdong Lin 18638f27c4abSMengdong Lin if (strlen(pcm->pcm_name)) { 18648f27c4abSMengdong Lin link->name = kstrdup(pcm->pcm_name, GFP_KERNEL); 18658f27c4abSMengdong Lin link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL); 18668f27c4abSMengdong Lin } 1867b84fff5aSMengdong Lin link->id = pcm->pcm_id; 1868acfc7d46SMengdong Lin 18698f27c4abSMengdong Lin if (strlen(pcm->dai_name)) 18708f27c4abSMengdong Lin link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL); 18718f27c4abSMengdong Lin 187267d1c21eSGuneshwor Singh link->codec_name = "snd-soc-dummy"; 187367d1c21eSGuneshwor Singh link->codec_dai_name = "snd-soc-dummy-dai"; 187467d1c21eSGuneshwor Singh 187567d1c21eSGuneshwor Singh /* enable DPCM */ 187667d1c21eSGuneshwor Singh link->dynamic = 1; 187767d1c21eSGuneshwor Singh link->dpcm_playback = pcm->playback; 187867d1c21eSGuneshwor Singh link->dpcm_capture = pcm->capture; 1879717a8e72SMengdong Lin if (pcm->flag_mask) 1880717a8e72SMengdong Lin set_link_flags(link, pcm->flag_mask, pcm->flags); 188167d1c21eSGuneshwor Singh 1882acfc7d46SMengdong Lin /* pass control to component driver for optional further init */ 1883c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, NULL); 1884acfc7d46SMengdong Lin if (ret < 0) { 1885acfc7d46SMengdong Lin dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n"); 1886acfc7d46SMengdong Lin kfree(link); 1887acfc7d46SMengdong Lin return ret; 1888acfc7d46SMengdong Lin } 1889acfc7d46SMengdong Lin 1890acfc7d46SMengdong Lin link->dobj.index = tplg->index; 1891acfc7d46SMengdong Lin link->dobj.ops = tplg->ops; 1892acfc7d46SMengdong Lin link->dobj.type = SND_SOC_DOBJ_DAI_LINK; 1893acfc7d46SMengdong Lin list_add(&link->dobj.list, &tplg->comp->dobj_list); 1894acfc7d46SMengdong Lin 1895acfc7d46SMengdong Lin snd_soc_add_dai_link(tplg->comp->card, link); 1896acfc7d46SMengdong Lin return 0; 1897acfc7d46SMengdong Lin } 1898acfc7d46SMengdong Lin 1899acfc7d46SMengdong Lin /* create a FE DAI and DAI link from the PCM object */ 190064527e8aSMengdong Lin static int soc_tplg_pcm_create(struct soc_tplg *tplg, 190164527e8aSMengdong Lin struct snd_soc_tplg_pcm *pcm) 190264527e8aSMengdong Lin { 1903acfc7d46SMengdong Lin int ret; 1904acfc7d46SMengdong Lin 1905acfc7d46SMengdong Lin ret = soc_tplg_dai_create(tplg, pcm); 1906acfc7d46SMengdong Lin if (ret < 0) 1907acfc7d46SMengdong Lin return ret; 1908acfc7d46SMengdong Lin 1909ab4bc5eeSMengdong Lin return soc_tplg_fe_link_create(tplg, pcm); 191064527e8aSMengdong Lin } 191164527e8aSMengdong Lin 191255726dc9SMengdong Lin /* copy stream caps from the old version 4 of source */ 191355726dc9SMengdong Lin static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, 191455726dc9SMengdong Lin struct snd_soc_tplg_stream_caps_v4 *src) 191555726dc9SMengdong Lin { 191655726dc9SMengdong Lin dest->size = sizeof(*dest); 191755726dc9SMengdong Lin memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 191855726dc9SMengdong Lin dest->formats = src->formats; 191955726dc9SMengdong Lin dest->rates = src->rates; 192055726dc9SMengdong Lin dest->rate_min = src->rate_min; 192155726dc9SMengdong Lin dest->rate_max = src->rate_max; 192255726dc9SMengdong Lin dest->channels_min = src->channels_min; 192355726dc9SMengdong Lin dest->channels_max = src->channels_max; 192455726dc9SMengdong Lin dest->periods_min = src->periods_min; 192555726dc9SMengdong Lin dest->periods_max = src->periods_max; 192655726dc9SMengdong Lin dest->period_size_min = src->period_size_min; 192755726dc9SMengdong Lin dest->period_size_max = src->period_size_max; 192855726dc9SMengdong Lin dest->buffer_size_min = src->buffer_size_min; 192955726dc9SMengdong Lin dest->buffer_size_max = src->buffer_size_max; 193055726dc9SMengdong Lin } 193155726dc9SMengdong Lin 193255726dc9SMengdong Lin /** 193355726dc9SMengdong Lin * pcm_new_ver - Create the new version of PCM from the old version. 193455726dc9SMengdong Lin * @tplg: topology context 193555726dc9SMengdong Lin * @src: older version of pcm as a source 193655726dc9SMengdong Lin * @pcm: latest version of pcm created from the source 193755726dc9SMengdong Lin * 193855726dc9SMengdong Lin * Support from vesion 4. User should free the returned pcm manually. 193955726dc9SMengdong Lin */ 194055726dc9SMengdong Lin static int pcm_new_ver(struct soc_tplg *tplg, 194155726dc9SMengdong Lin struct snd_soc_tplg_pcm *src, 194255726dc9SMengdong Lin struct snd_soc_tplg_pcm **pcm) 194355726dc9SMengdong Lin { 194455726dc9SMengdong Lin struct snd_soc_tplg_pcm *dest; 194555726dc9SMengdong Lin struct snd_soc_tplg_pcm_v4 *src_v4; 194655726dc9SMengdong Lin int i; 194755726dc9SMengdong Lin 194855726dc9SMengdong Lin *pcm = NULL; 194955726dc9SMengdong Lin 195055726dc9SMengdong Lin if (src->size != sizeof(*src_v4)) { 195155726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid PCM size\n"); 195255726dc9SMengdong Lin return -EINVAL; 195355726dc9SMengdong Lin } 195455726dc9SMengdong Lin 195555726dc9SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of PCM\n"); 195655726dc9SMengdong Lin src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; 195755726dc9SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 195855726dc9SMengdong Lin if (!dest) 195955726dc9SMengdong Lin return -ENOMEM; 196055726dc9SMengdong Lin 196155726dc9SMengdong Lin dest->size = sizeof(*dest); /* size of latest abi version */ 196255726dc9SMengdong Lin memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 196355726dc9SMengdong Lin memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 196455726dc9SMengdong Lin dest->pcm_id = src_v4->pcm_id; 196555726dc9SMengdong Lin dest->dai_id = src_v4->dai_id; 196655726dc9SMengdong Lin dest->playback = src_v4->playback; 196755726dc9SMengdong Lin dest->capture = src_v4->capture; 196855726dc9SMengdong Lin dest->compress = src_v4->compress; 196955726dc9SMengdong Lin dest->num_streams = src_v4->num_streams; 197055726dc9SMengdong Lin for (i = 0; i < dest->num_streams; i++) 197155726dc9SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 197255726dc9SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 197355726dc9SMengdong Lin 197455726dc9SMengdong Lin for (i = 0; i < 2; i++) 197555726dc9SMengdong Lin stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); 197655726dc9SMengdong Lin 197755726dc9SMengdong Lin *pcm = dest; 197855726dc9SMengdong Lin return 0; 197955726dc9SMengdong Lin } 198055726dc9SMengdong Lin 198164527e8aSMengdong Lin static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, 19828a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 19838a978234SLiam Girdwood { 198455726dc9SMengdong Lin struct snd_soc_tplg_pcm *pcm, *_pcm; 19858a978234SLiam Girdwood int count = hdr->count; 1986fd340455SVinod Koul int i; 198755726dc9SMengdong Lin bool abi_match; 19888a978234SLiam Girdwood 19898a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_PCM_DAI) 19908a978234SLiam Girdwood return 0; 19918a978234SLiam Girdwood 199255726dc9SMengdong Lin /* check the element size and count */ 199355726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 199455726dc9SMengdong Lin if (pcm->size > sizeof(struct snd_soc_tplg_pcm) 199555726dc9SMengdong Lin || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) { 199655726dc9SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", 199755726dc9SMengdong Lin pcm->size); 199855726dc9SMengdong Lin return -EINVAL; 199955726dc9SMengdong Lin } 200055726dc9SMengdong Lin 20018a978234SLiam Girdwood if (soc_tplg_check_elem_count(tplg, 200255726dc9SMengdong Lin pcm->size, count, 20038a978234SLiam Girdwood hdr->payload_size, "PCM DAI")) { 20048a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", 20058a978234SLiam Girdwood count); 20068a978234SLiam Girdwood return -EINVAL; 20078a978234SLiam Girdwood } 20088a978234SLiam Girdwood 200964527e8aSMengdong Lin for (i = 0; i < count; i++) { 201055726dc9SMengdong Lin pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 201155726dc9SMengdong Lin 201255726dc9SMengdong Lin /* check ABI version by size, create a new version of pcm 201355726dc9SMengdong Lin * if abi not match. 201455726dc9SMengdong Lin */ 201555726dc9SMengdong Lin if (pcm->size == sizeof(*pcm)) { 201655726dc9SMengdong Lin abi_match = true; 201755726dc9SMengdong Lin _pcm = pcm; 201855726dc9SMengdong Lin } else { 201955726dc9SMengdong Lin abi_match = false; 2020fd340455SVinod Koul pcm_new_ver(tplg, pcm, &_pcm); 202106eb49f7SMengdong Lin } 202206eb49f7SMengdong Lin 202355726dc9SMengdong Lin /* create the FE DAIs and DAI links */ 202455726dc9SMengdong Lin soc_tplg_pcm_create(tplg, _pcm); 202555726dc9SMengdong Lin 2026717a8e72SMengdong Lin /* offset by version-specific struct size and 2027717a8e72SMengdong Lin * real priv data size 2028717a8e72SMengdong Lin */ 2029717a8e72SMengdong Lin tplg->pos += pcm->size + _pcm->priv.size; 2030717a8e72SMengdong Lin 203155726dc9SMengdong Lin if (!abi_match) 203255726dc9SMengdong Lin kfree(_pcm); /* free the duplicated one */ 203364527e8aSMengdong Lin } 203464527e8aSMengdong Lin 20358a978234SLiam Girdwood dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); 20368a978234SLiam Girdwood 20378a978234SLiam Girdwood return 0; 20388a978234SLiam Girdwood } 20398a978234SLiam Girdwood 20400038be9aSMengdong Lin /** 2041593d9e52SMengdong Lin * set_link_hw_format - Set the HW audio format of the physical DAI link. 20428abab35fSCharles Keepax * @link: &snd_soc_dai_link which should be updated 2043593d9e52SMengdong Lin * @cfg: physical link configs. 2044593d9e52SMengdong Lin * 2045593d9e52SMengdong Lin * Topology context contains a list of supported HW formats (configs) and 2046593d9e52SMengdong Lin * a default format ID for the physical link. This function will use this 2047593d9e52SMengdong Lin * default ID to choose the HW format to set the link's DAI format for init. 2048593d9e52SMengdong Lin */ 2049593d9e52SMengdong Lin static void set_link_hw_format(struct snd_soc_dai_link *link, 2050593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 2051593d9e52SMengdong Lin { 2052593d9e52SMengdong Lin struct snd_soc_tplg_hw_config *hw_config; 2053593d9e52SMengdong Lin unsigned char bclk_master, fsync_master; 2054593d9e52SMengdong Lin unsigned char invert_bclk, invert_fsync; 2055593d9e52SMengdong Lin int i; 2056593d9e52SMengdong Lin 2057593d9e52SMengdong Lin for (i = 0; i < cfg->num_hw_configs; i++) { 2058593d9e52SMengdong Lin hw_config = &cfg->hw_config[i]; 2059593d9e52SMengdong Lin if (hw_config->id != cfg->default_hw_config_id) 2060593d9e52SMengdong Lin continue; 2061593d9e52SMengdong Lin 2062593d9e52SMengdong Lin link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK; 2063593d9e52SMengdong Lin 2064933e1c4aSKirill Marinushkin /* clock gating */ 2065fbeabd09SKirill Marinushkin switch (hw_config->clock_gated) { 2066fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_GATED: 2067933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_GATED; 2068fbeabd09SKirill Marinushkin break; 2069fbeabd09SKirill Marinushkin 2070fbeabd09SKirill Marinushkin case SND_SOC_TPLG_DAI_CLK_GATE_CONT: 2071933e1c4aSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_CONT; 2072fbeabd09SKirill Marinushkin break; 2073fbeabd09SKirill Marinushkin 2074fbeabd09SKirill Marinushkin default: 2075fbeabd09SKirill Marinushkin /* ignore the value */ 2076fbeabd09SKirill Marinushkin break; 2077fbeabd09SKirill Marinushkin } 2078933e1c4aSKirill Marinushkin 2079593d9e52SMengdong Lin /* clock signal polarity */ 2080593d9e52SMengdong Lin invert_bclk = hw_config->invert_bclk; 2081593d9e52SMengdong Lin invert_fsync = hw_config->invert_fsync; 2082593d9e52SMengdong Lin if (!invert_bclk && !invert_fsync) 2083593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; 2084593d9e52SMengdong Lin else if (!invert_bclk && invert_fsync) 2085593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; 2086593d9e52SMengdong Lin else if (invert_bclk && !invert_fsync) 2087593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; 2088593d9e52SMengdong Lin else 2089593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; 2090593d9e52SMengdong Lin 2091593d9e52SMengdong Lin /* clock masters */ 2092a941e2faSKirill Marinushkin bclk_master = (hw_config->bclk_master == 2093a941e2faSKirill Marinushkin SND_SOC_TPLG_BCLK_CM); 2094a941e2faSKirill Marinushkin fsync_master = (hw_config->fsync_master == 2095a941e2faSKirill Marinushkin SND_SOC_TPLG_FSYNC_CM); 2096a941e2faSKirill Marinushkin if (bclk_master && fsync_master) 2097593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; 2098593d9e52SMengdong Lin else if (!bclk_master && fsync_master) 2099a941e2faSKirill Marinushkin link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; 2100a941e2faSKirill Marinushkin else if (bclk_master && !fsync_master) 2101593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; 2102593d9e52SMengdong Lin else 2103593d9e52SMengdong Lin link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; 2104593d9e52SMengdong Lin } 2105593d9e52SMengdong Lin } 2106593d9e52SMengdong Lin 2107593d9e52SMengdong Lin /** 2108593d9e52SMengdong Lin * link_new_ver - Create a new physical link config from the old 2109593d9e52SMengdong Lin * version of source. 21108abab35fSCharles Keepax * @tplg: topology context 2111593d9e52SMengdong Lin * @src: old version of phyical link config as a source 2112593d9e52SMengdong Lin * @link: latest version of physical link config created from the source 2113593d9e52SMengdong Lin * 2114593d9e52SMengdong Lin * Support from vesion 4. User need free the returned link config manually. 2115593d9e52SMengdong Lin */ 2116593d9e52SMengdong Lin static int link_new_ver(struct soc_tplg *tplg, 2117593d9e52SMengdong Lin struct snd_soc_tplg_link_config *src, 2118593d9e52SMengdong Lin struct snd_soc_tplg_link_config **link) 2119593d9e52SMengdong Lin { 2120593d9e52SMengdong Lin struct snd_soc_tplg_link_config *dest; 2121593d9e52SMengdong Lin struct snd_soc_tplg_link_config_v4 *src_v4; 2122593d9e52SMengdong Lin int i; 2123593d9e52SMengdong Lin 2124593d9e52SMengdong Lin *link = NULL; 2125593d9e52SMengdong Lin 2126593d9e52SMengdong Lin if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) { 2127593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); 2128593d9e52SMengdong Lin return -EINVAL; 2129593d9e52SMengdong Lin } 2130593d9e52SMengdong Lin 2131593d9e52SMengdong Lin dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); 2132593d9e52SMengdong Lin 2133593d9e52SMengdong Lin src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; 2134593d9e52SMengdong Lin dest = kzalloc(sizeof(*dest), GFP_KERNEL); 2135593d9e52SMengdong Lin if (!dest) 2136593d9e52SMengdong Lin return -ENOMEM; 2137593d9e52SMengdong Lin 2138593d9e52SMengdong Lin dest->size = sizeof(*dest); 2139593d9e52SMengdong Lin dest->id = src_v4->id; 2140593d9e52SMengdong Lin dest->num_streams = src_v4->num_streams; 2141593d9e52SMengdong Lin for (i = 0; i < dest->num_streams; i++) 2142593d9e52SMengdong Lin memcpy(&dest->stream[i], &src_v4->stream[i], 2143593d9e52SMengdong Lin sizeof(struct snd_soc_tplg_stream)); 2144593d9e52SMengdong Lin 2145593d9e52SMengdong Lin *link = dest; 2146593d9e52SMengdong Lin return 0; 2147593d9e52SMengdong Lin } 2148593d9e52SMengdong Lin 2149593d9e52SMengdong Lin /* Find and configure an existing physical DAI link */ 2150593d9e52SMengdong Lin static int soc_tplg_link_config(struct soc_tplg *tplg, 2151593d9e52SMengdong Lin struct snd_soc_tplg_link_config *cfg) 2152593d9e52SMengdong Lin { 2153593d9e52SMengdong Lin struct snd_soc_dai_link *link; 2154593d9e52SMengdong Lin const char *name, *stream_name; 2155dbab1cb8SMengdong Lin size_t len; 2156593d9e52SMengdong Lin int ret; 2157593d9e52SMengdong Lin 2158dbab1cb8SMengdong Lin len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2159dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2160dbab1cb8SMengdong Lin return -EINVAL; 2161dbab1cb8SMengdong Lin else if (len) 2162dbab1cb8SMengdong Lin name = cfg->name; 2163dbab1cb8SMengdong Lin else 2164dbab1cb8SMengdong Lin name = NULL; 2165dbab1cb8SMengdong Lin 2166dbab1cb8SMengdong Lin len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 2167dbab1cb8SMengdong Lin if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 2168dbab1cb8SMengdong Lin return -EINVAL; 2169dbab1cb8SMengdong Lin else if (len) 2170dbab1cb8SMengdong Lin stream_name = cfg->stream_name; 2171dbab1cb8SMengdong Lin else 2172dbab1cb8SMengdong Lin stream_name = NULL; 2173593d9e52SMengdong Lin 2174593d9e52SMengdong Lin link = snd_soc_find_dai_link(tplg->comp->card, cfg->id, 2175593d9e52SMengdong Lin name, stream_name); 2176593d9e52SMengdong Lin if (!link) { 2177593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", 2178593d9e52SMengdong Lin name, cfg->id); 2179593d9e52SMengdong Lin return -EINVAL; 2180593d9e52SMengdong Lin } 2181593d9e52SMengdong Lin 2182593d9e52SMengdong Lin /* hw format */ 2183593d9e52SMengdong Lin if (cfg->num_hw_configs) 2184593d9e52SMengdong Lin set_link_hw_format(link, cfg); 2185593d9e52SMengdong Lin 2186593d9e52SMengdong Lin /* flags */ 2187593d9e52SMengdong Lin if (cfg->flag_mask) 2188593d9e52SMengdong Lin set_link_flags(link, cfg->flag_mask, cfg->flags); 2189593d9e52SMengdong Lin 2190593d9e52SMengdong Lin /* pass control to component driver for optional further init */ 2191c60b613aSLiam Girdwood ret = soc_tplg_dai_link_load(tplg, link, cfg); 2192593d9e52SMengdong Lin if (ret < 0) { 2193593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: physical link loading failed\n"); 2194593d9e52SMengdong Lin return ret; 2195593d9e52SMengdong Lin } 2196593d9e52SMengdong Lin 2197adfebb51SBard liao /* for unloading it in snd_soc_tplg_component_remove */ 2198adfebb51SBard liao link->dobj.index = tplg->index; 2199adfebb51SBard liao link->dobj.ops = tplg->ops; 2200adfebb51SBard liao link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; 2201adfebb51SBard liao list_add(&link->dobj.list, &tplg->comp->dobj_list); 2202adfebb51SBard liao 2203593d9e52SMengdong Lin return 0; 2204593d9e52SMengdong Lin } 2205593d9e52SMengdong Lin 2206593d9e52SMengdong Lin 2207593d9e52SMengdong Lin /* Load physical link config elements from the topology context */ 2208593d9e52SMengdong Lin static int soc_tplg_link_elems_load(struct soc_tplg *tplg, 2209593d9e52SMengdong Lin struct snd_soc_tplg_hdr *hdr) 2210593d9e52SMengdong Lin { 2211593d9e52SMengdong Lin struct snd_soc_tplg_link_config *link, *_link; 2212593d9e52SMengdong Lin int count = hdr->count; 2213593d9e52SMengdong Lin int i, ret; 2214593d9e52SMengdong Lin bool abi_match; 2215593d9e52SMengdong Lin 2216593d9e52SMengdong Lin if (tplg->pass != SOC_TPLG_PASS_LINK) { 2217593d9e52SMengdong Lin tplg->pos += hdr->size + hdr->payload_size; 2218593d9e52SMengdong Lin return 0; 2219593d9e52SMengdong Lin }; 2220593d9e52SMengdong Lin 2221593d9e52SMengdong Lin /* check the element size and count */ 2222593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 2223593d9e52SMengdong Lin if (link->size > sizeof(struct snd_soc_tplg_link_config) 2224593d9e52SMengdong Lin || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) { 2225593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", 2226593d9e52SMengdong Lin link->size); 2227593d9e52SMengdong Lin return -EINVAL; 2228593d9e52SMengdong Lin } 2229593d9e52SMengdong Lin 2230593d9e52SMengdong Lin if (soc_tplg_check_elem_count(tplg, 2231593d9e52SMengdong Lin link->size, count, 2232593d9e52SMengdong Lin hdr->payload_size, "physical link config")) { 2233593d9e52SMengdong Lin dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n", 2234593d9e52SMengdong Lin count); 2235593d9e52SMengdong Lin return -EINVAL; 2236593d9e52SMengdong Lin } 2237593d9e52SMengdong Lin 2238593d9e52SMengdong Lin /* config physical DAI links */ 2239593d9e52SMengdong Lin for (i = 0; i < count; i++) { 2240593d9e52SMengdong Lin link = (struct snd_soc_tplg_link_config *)tplg->pos; 2241593d9e52SMengdong Lin if (link->size == sizeof(*link)) { 2242593d9e52SMengdong Lin abi_match = true; 2243593d9e52SMengdong Lin _link = link; 2244593d9e52SMengdong Lin } else { 2245593d9e52SMengdong Lin abi_match = false; 2246593d9e52SMengdong Lin ret = link_new_ver(tplg, link, &_link); 2247593d9e52SMengdong Lin if (ret < 0) 2248593d9e52SMengdong Lin return ret; 2249593d9e52SMengdong Lin } 2250593d9e52SMengdong Lin 2251593d9e52SMengdong Lin ret = soc_tplg_link_config(tplg, _link); 2252593d9e52SMengdong Lin if (ret < 0) 2253593d9e52SMengdong Lin return ret; 2254593d9e52SMengdong Lin 2255593d9e52SMengdong Lin /* offset by version-specific struct size and 2256593d9e52SMengdong Lin * real priv data size 2257593d9e52SMengdong Lin */ 2258593d9e52SMengdong Lin tplg->pos += link->size + _link->priv.size; 2259593d9e52SMengdong Lin 2260593d9e52SMengdong Lin if (!abi_match) 2261593d9e52SMengdong Lin kfree(_link); /* free the duplicated one */ 2262593d9e52SMengdong Lin } 2263593d9e52SMengdong Lin 2264593d9e52SMengdong Lin return 0; 2265593d9e52SMengdong Lin } 2266593d9e52SMengdong Lin 2267593d9e52SMengdong Lin /** 22689aa3f034SMengdong Lin * soc_tplg_dai_config - Find and configure an existing physical DAI. 22690038be9aSMengdong Lin * @tplg: topology context 22709aa3f034SMengdong Lin * @d: physical DAI configs. 22710038be9aSMengdong Lin * 22729aa3f034SMengdong Lin * The physical dai should already be registered by the platform driver. 22739aa3f034SMengdong Lin * The platform driver should specify the DAI name and ID for matching. 22740038be9aSMengdong Lin */ 22759aa3f034SMengdong Lin static int soc_tplg_dai_config(struct soc_tplg *tplg, 22769aa3f034SMengdong Lin struct snd_soc_tplg_dai *d) 22770038be9aSMengdong Lin { 22780038be9aSMengdong Lin struct snd_soc_dai_link_component dai_component = {0}; 22790038be9aSMengdong Lin struct snd_soc_dai *dai; 22800038be9aSMengdong Lin struct snd_soc_dai_driver *dai_drv; 22810038be9aSMengdong Lin struct snd_soc_pcm_stream *stream; 22820038be9aSMengdong Lin struct snd_soc_tplg_stream_caps *caps; 22830038be9aSMengdong Lin int ret; 22840038be9aSMengdong Lin 22859aa3f034SMengdong Lin dai_component.dai_name = d->dai_name; 22860038be9aSMengdong Lin dai = snd_soc_find_dai(&dai_component); 22870038be9aSMengdong Lin if (!dai) { 22889aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", 22899aa3f034SMengdong Lin d->dai_name); 22900038be9aSMengdong Lin return -EINVAL; 22910038be9aSMengdong Lin } 22920038be9aSMengdong Lin 22939aa3f034SMengdong Lin if (d->dai_id != dai->id) { 22949aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", 22959aa3f034SMengdong Lin d->dai_name); 22960038be9aSMengdong Lin return -EINVAL; 22970038be9aSMengdong Lin } 22980038be9aSMengdong Lin 22990038be9aSMengdong Lin dai_drv = dai->driver; 23000038be9aSMengdong Lin if (!dai_drv) 23010038be9aSMengdong Lin return -EINVAL; 23020038be9aSMengdong Lin 23039aa3f034SMengdong Lin if (d->playback) { 23040038be9aSMengdong Lin stream = &dai_drv->playback; 23059aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 23060038be9aSMengdong Lin set_stream_info(stream, caps); 23070038be9aSMengdong Lin } 23080038be9aSMengdong Lin 23099aa3f034SMengdong Lin if (d->capture) { 23100038be9aSMengdong Lin stream = &dai_drv->capture; 23119aa3f034SMengdong Lin caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 23120038be9aSMengdong Lin set_stream_info(stream, caps); 23130038be9aSMengdong Lin } 23140038be9aSMengdong Lin 23159aa3f034SMengdong Lin if (d->flag_mask) 23169aa3f034SMengdong Lin set_dai_flags(dai_drv, d->flag_mask, d->flags); 23170038be9aSMengdong Lin 23180038be9aSMengdong Lin /* pass control to component driver for optional further init */ 2319c60b613aSLiam Girdwood ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); 23200038be9aSMengdong Lin if (ret < 0) { 23210038be9aSMengdong Lin dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); 23220038be9aSMengdong Lin return ret; 23230038be9aSMengdong Lin } 23240038be9aSMengdong Lin 23250038be9aSMengdong Lin return 0; 23260038be9aSMengdong Lin } 23270038be9aSMengdong Lin 23289aa3f034SMengdong Lin /* load physical DAI elements */ 23299aa3f034SMengdong Lin static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, 23300038be9aSMengdong Lin struct snd_soc_tplg_hdr *hdr) 23310038be9aSMengdong Lin { 23329aa3f034SMengdong Lin struct snd_soc_tplg_dai *dai; 23330038be9aSMengdong Lin int count = hdr->count; 23340038be9aSMengdong Lin int i; 23350038be9aSMengdong Lin 23360038be9aSMengdong Lin if (tplg->pass != SOC_TPLG_PASS_BE_DAI) 23370038be9aSMengdong Lin return 0; 23380038be9aSMengdong Lin 23390038be9aSMengdong Lin /* config the existing BE DAIs */ 23400038be9aSMengdong Lin for (i = 0; i < count; i++) { 23419aa3f034SMengdong Lin dai = (struct snd_soc_tplg_dai *)tplg->pos; 23429aa3f034SMengdong Lin if (dai->size != sizeof(*dai)) { 23439aa3f034SMengdong Lin dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); 23440038be9aSMengdong Lin return -EINVAL; 23450038be9aSMengdong Lin } 23460038be9aSMengdong Lin 23479aa3f034SMengdong Lin soc_tplg_dai_config(tplg, dai); 23489aa3f034SMengdong Lin tplg->pos += (sizeof(*dai) + dai->priv.size); 23490038be9aSMengdong Lin } 23500038be9aSMengdong Lin 23510038be9aSMengdong Lin dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); 23520038be9aSMengdong Lin return 0; 23530038be9aSMengdong Lin } 23540038be9aSMengdong Lin 2355583958faSMengdong Lin /** 2356583958faSMengdong Lin * manifest_new_ver - Create a new version of manifest from the old version 2357583958faSMengdong Lin * of source. 23588abab35fSCharles Keepax * @tplg: topology context 2359583958faSMengdong Lin * @src: old version of manifest as a source 2360583958faSMengdong Lin * @manifest: latest version of manifest created from the source 2361583958faSMengdong Lin * 2362583958faSMengdong Lin * Support from vesion 4. Users need free the returned manifest manually. 2363583958faSMengdong Lin */ 2364583958faSMengdong Lin static int manifest_new_ver(struct soc_tplg *tplg, 2365583958faSMengdong Lin struct snd_soc_tplg_manifest *src, 2366583958faSMengdong Lin struct snd_soc_tplg_manifest **manifest) 2367583958faSMengdong Lin { 2368583958faSMengdong Lin struct snd_soc_tplg_manifest *dest; 2369583958faSMengdong Lin struct snd_soc_tplg_manifest_v4 *src_v4; 2370583958faSMengdong Lin 2371583958faSMengdong Lin *manifest = NULL; 2372583958faSMengdong Lin 2373583958faSMengdong Lin if (src->size != sizeof(*src_v4)) { 2374ac9391daSGuenter Roeck dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", 2375ac9391daSGuenter Roeck src->size); 2376ac9391daSGuenter Roeck if (src->size) 2377583958faSMengdong Lin return -EINVAL; 2378ac9391daSGuenter Roeck src->size = sizeof(*src_v4); 2379583958faSMengdong Lin } 2380583958faSMengdong Lin 2381583958faSMengdong Lin dev_warn(tplg->dev, "ASoC: old version of manifest\n"); 2382583958faSMengdong Lin 2383583958faSMengdong Lin src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; 2384583958faSMengdong Lin dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL); 2385583958faSMengdong Lin if (!dest) 2386583958faSMengdong Lin return -ENOMEM; 2387583958faSMengdong Lin 2388583958faSMengdong Lin dest->size = sizeof(*dest); /* size of latest abi version */ 2389583958faSMengdong Lin dest->control_elems = src_v4->control_elems; 2390583958faSMengdong Lin dest->widget_elems = src_v4->widget_elems; 2391583958faSMengdong Lin dest->graph_elems = src_v4->graph_elems; 2392583958faSMengdong Lin dest->pcm_elems = src_v4->pcm_elems; 2393583958faSMengdong Lin dest->dai_link_elems = src_v4->dai_link_elems; 2394583958faSMengdong Lin dest->priv.size = src_v4->priv.size; 2395583958faSMengdong Lin if (dest->priv.size) 2396583958faSMengdong Lin memcpy(dest->priv.data, src_v4->priv.data, 2397583958faSMengdong Lin src_v4->priv.size); 2398583958faSMengdong Lin 2399583958faSMengdong Lin *manifest = dest; 2400583958faSMengdong Lin return 0; 2401583958faSMengdong Lin } 24020038be9aSMengdong Lin 24038a978234SLiam Girdwood static int soc_tplg_manifest_load(struct soc_tplg *tplg, 24048a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 24058a978234SLiam Girdwood { 2406583958faSMengdong Lin struct snd_soc_tplg_manifest *manifest, *_manifest; 2407583958faSMengdong Lin bool abi_match; 2408583958faSMengdong Lin int err; 24098a978234SLiam Girdwood 24108a978234SLiam Girdwood if (tplg->pass != SOC_TPLG_PASS_MANIFEST) 24118a978234SLiam Girdwood return 0; 24128a978234SLiam Girdwood 24138a978234SLiam Girdwood manifest = (struct snd_soc_tplg_manifest *)tplg->pos; 2414583958faSMengdong Lin 2415583958faSMengdong Lin /* check ABI version by size, create a new manifest if abi not match */ 2416583958faSMengdong Lin if (manifest->size == sizeof(*manifest)) { 2417583958faSMengdong Lin abi_match = true; 2418583958faSMengdong Lin _manifest = manifest; 2419583958faSMengdong Lin } else { 2420583958faSMengdong Lin abi_match = false; 2421583958faSMengdong Lin err = manifest_new_ver(tplg, manifest, &_manifest); 2422583958faSMengdong Lin if (err < 0) 2423583958faSMengdong Lin return err; 242406eb49f7SMengdong Lin } 242506eb49f7SMengdong Lin 2426583958faSMengdong Lin /* pass control to component driver for optional further init */ 24278a978234SLiam Girdwood if (tplg->comp && tplg->ops && tplg->ops->manifest) 2428c60b613aSLiam Girdwood return tplg->ops->manifest(tplg->comp, tplg->index, _manifest); 24298a978234SLiam Girdwood 2430583958faSMengdong Lin if (!abi_match) /* free the duplicated one */ 2431583958faSMengdong Lin kfree(_manifest); 2432583958faSMengdong Lin 24338a978234SLiam Girdwood return 0; 24348a978234SLiam Girdwood } 24358a978234SLiam Girdwood 24368a978234SLiam Girdwood /* validate header magic, size and type */ 24378a978234SLiam Girdwood static int soc_valid_header(struct soc_tplg *tplg, 24388a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 24398a978234SLiam Girdwood { 24408a978234SLiam Girdwood if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) 24418a978234SLiam Girdwood return 0; 24428a978234SLiam Girdwood 244306eb49f7SMengdong Lin if (hdr->size != sizeof(*hdr)) { 244406eb49f7SMengdong Lin dev_err(tplg->dev, 244506eb49f7SMengdong Lin "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", 244606eb49f7SMengdong Lin hdr->type, soc_tplg_get_hdr_offset(tplg), 244706eb49f7SMengdong Lin tplg->fw->size); 244806eb49f7SMengdong Lin return -EINVAL; 244906eb49f7SMengdong Lin } 245006eb49f7SMengdong Lin 24518a978234SLiam Girdwood /* big endian firmware objects not supported atm */ 24528a978234SLiam Girdwood if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) { 24538a978234SLiam Girdwood dev_err(tplg->dev, 24548a978234SLiam Girdwood "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", 24558a978234SLiam Girdwood tplg->pass, hdr->magic, 24568a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 24578a978234SLiam Girdwood return -EINVAL; 24588a978234SLiam Girdwood } 24598a978234SLiam Girdwood 24608a978234SLiam Girdwood if (hdr->magic != SND_SOC_TPLG_MAGIC) { 24618a978234SLiam Girdwood dev_err(tplg->dev, 24628a978234SLiam Girdwood "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", 24638a978234SLiam Girdwood tplg->pass, hdr->magic, 24648a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 24658a978234SLiam Girdwood return -EINVAL; 24668a978234SLiam Girdwood } 24678a978234SLiam Girdwood 2468288b8da7SMengdong Lin /* Support ABI from version 4 */ 2469288b8da7SMengdong Lin if (hdr->abi > SND_SOC_TPLG_ABI_VERSION 2470288b8da7SMengdong Lin || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) { 24718a978234SLiam Girdwood dev_err(tplg->dev, 24728a978234SLiam Girdwood "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", 24738a978234SLiam Girdwood tplg->pass, hdr->abi, 24748a978234SLiam Girdwood SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), 24758a978234SLiam Girdwood tplg->fw->size); 24768a978234SLiam Girdwood return -EINVAL; 24778a978234SLiam Girdwood } 24788a978234SLiam Girdwood 24798a978234SLiam Girdwood if (hdr->payload_size == 0) { 24808a978234SLiam Girdwood dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", 24818a978234SLiam Girdwood soc_tplg_get_hdr_offset(tplg)); 24828a978234SLiam Girdwood return -EINVAL; 24838a978234SLiam Girdwood } 24848a978234SLiam Girdwood 24858a978234SLiam Girdwood if (tplg->pass == hdr->type) 24868a978234SLiam Girdwood dev_dbg(tplg->dev, 24878a978234SLiam Girdwood "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", 24888a978234SLiam Girdwood hdr->payload_size, hdr->type, hdr->version, 24898a978234SLiam Girdwood hdr->vendor_type, tplg->pass); 24908a978234SLiam Girdwood 24918a978234SLiam Girdwood return 1; 24928a978234SLiam Girdwood } 24938a978234SLiam Girdwood 24948a978234SLiam Girdwood /* check header type and call appropriate handler */ 24958a978234SLiam Girdwood static int soc_tplg_load_header(struct soc_tplg *tplg, 24968a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr) 24978a978234SLiam Girdwood { 24988a978234SLiam Girdwood tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); 24998a978234SLiam Girdwood 25008a978234SLiam Girdwood /* check for matching ID */ 25018a978234SLiam Girdwood if (hdr->index != tplg->req_index && 2502bb97142bSLiam Girdwood tplg->req_index != SND_SOC_TPLG_INDEX_ALL) 25038a978234SLiam Girdwood return 0; 25048a978234SLiam Girdwood 25058a978234SLiam Girdwood tplg->index = hdr->index; 25068a978234SLiam Girdwood 25078a978234SLiam Girdwood switch (hdr->type) { 25088a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MIXER: 25098a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_ENUM: 25108a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_BYTES: 25118a978234SLiam Girdwood return soc_tplg_kcontrol_elems_load(tplg, hdr); 25128a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_GRAPH: 25138a978234SLiam Girdwood return soc_tplg_dapm_graph_elems_load(tplg, hdr); 25148a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_DAPM_WIDGET: 25158a978234SLiam Girdwood return soc_tplg_dapm_widget_elems_load(tplg, hdr); 25168a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_PCM: 251764527e8aSMengdong Lin return soc_tplg_pcm_elems_load(tplg, hdr); 25183fbf7935SMengdong Lin case SND_SOC_TPLG_TYPE_DAI: 25199aa3f034SMengdong Lin return soc_tplg_dai_elems_load(tplg, hdr); 2520593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_DAI_LINK: 2521593d9e52SMengdong Lin case SND_SOC_TPLG_TYPE_BACKEND_LINK: 2522593d9e52SMengdong Lin /* physical link configurations */ 2523593d9e52SMengdong Lin return soc_tplg_link_elems_load(tplg, hdr); 25248a978234SLiam Girdwood case SND_SOC_TPLG_TYPE_MANIFEST: 25258a978234SLiam Girdwood return soc_tplg_manifest_load(tplg, hdr); 25268a978234SLiam Girdwood default: 25278a978234SLiam Girdwood /* bespoke vendor data object */ 25288a978234SLiam Girdwood return soc_tplg_vendor_load(tplg, hdr); 25298a978234SLiam Girdwood } 25308a978234SLiam Girdwood 25318a978234SLiam Girdwood return 0; 25328a978234SLiam Girdwood } 25338a978234SLiam Girdwood 25348a978234SLiam Girdwood /* process the topology file headers */ 25358a978234SLiam Girdwood static int soc_tplg_process_headers(struct soc_tplg *tplg) 25368a978234SLiam Girdwood { 25378a978234SLiam Girdwood struct snd_soc_tplg_hdr *hdr; 25388a978234SLiam Girdwood int ret; 25398a978234SLiam Girdwood 25408a978234SLiam Girdwood tplg->pass = SOC_TPLG_PASS_START; 25418a978234SLiam Girdwood 25428a978234SLiam Girdwood /* process the header types from start to end */ 25438a978234SLiam Girdwood while (tplg->pass <= SOC_TPLG_PASS_END) { 25448a978234SLiam Girdwood 25458a978234SLiam Girdwood tplg->hdr_pos = tplg->fw->data; 25468a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 25478a978234SLiam Girdwood 25488a978234SLiam Girdwood while (!soc_tplg_is_eof(tplg)) { 25498a978234SLiam Girdwood 25508a978234SLiam Girdwood /* make sure header is valid before loading */ 25518a978234SLiam Girdwood ret = soc_valid_header(tplg, hdr); 25528a978234SLiam Girdwood if (ret < 0) 25538a978234SLiam Girdwood return ret; 25548a978234SLiam Girdwood else if (ret == 0) 25558a978234SLiam Girdwood break; 25568a978234SLiam Girdwood 25578a978234SLiam Girdwood /* load the header object */ 25588a978234SLiam Girdwood ret = soc_tplg_load_header(tplg, hdr); 25598a978234SLiam Girdwood if (ret < 0) 25608a978234SLiam Girdwood return ret; 25618a978234SLiam Girdwood 25628a978234SLiam Girdwood /* goto next header */ 25638a978234SLiam Girdwood tplg->hdr_pos += hdr->payload_size + 25648a978234SLiam Girdwood sizeof(struct snd_soc_tplg_hdr); 25658a978234SLiam Girdwood hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 25668a978234SLiam Girdwood } 25678a978234SLiam Girdwood 25688a978234SLiam Girdwood /* next data type pass */ 25698a978234SLiam Girdwood tplg->pass++; 25708a978234SLiam Girdwood } 25718a978234SLiam Girdwood 25728a978234SLiam Girdwood /* signal DAPM we are complete */ 25738a978234SLiam Girdwood ret = soc_tplg_dapm_complete(tplg); 25748a978234SLiam Girdwood if (ret < 0) 25758a978234SLiam Girdwood dev_err(tplg->dev, 25768a978234SLiam Girdwood "ASoC: failed to initialise DAPM from Firmware\n"); 25778a978234SLiam Girdwood 25788a978234SLiam Girdwood return ret; 25798a978234SLiam Girdwood } 25808a978234SLiam Girdwood 25818a978234SLiam Girdwood static int soc_tplg_load(struct soc_tplg *tplg) 25828a978234SLiam Girdwood { 25838a978234SLiam Girdwood int ret; 25848a978234SLiam Girdwood 25858a978234SLiam Girdwood ret = soc_tplg_process_headers(tplg); 25868a978234SLiam Girdwood if (ret == 0) 25878a978234SLiam Girdwood soc_tplg_complete(tplg); 25888a978234SLiam Girdwood 25898a978234SLiam Girdwood return ret; 25908a978234SLiam Girdwood } 25918a978234SLiam Girdwood 25928a978234SLiam Girdwood /* load audio component topology from "firmware" file */ 25938a978234SLiam Girdwood int snd_soc_tplg_component_load(struct snd_soc_component *comp, 25948a978234SLiam Girdwood struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) 25958a978234SLiam Girdwood { 25968a978234SLiam Girdwood struct soc_tplg tplg; 2597304017d3SBard liao int ret; 25988a978234SLiam Girdwood 25998a978234SLiam Girdwood /* setup parsing context */ 26008a978234SLiam Girdwood memset(&tplg, 0, sizeof(tplg)); 26018a978234SLiam Girdwood tplg.fw = fw; 26028a978234SLiam Girdwood tplg.dev = comp->dev; 26038a978234SLiam Girdwood tplg.comp = comp; 26048a978234SLiam Girdwood tplg.ops = ops; 26058a978234SLiam Girdwood tplg.req_index = id; 26068a978234SLiam Girdwood tplg.io_ops = ops->io_ops; 26078a978234SLiam Girdwood tplg.io_ops_count = ops->io_ops_count; 26081a3232d2SMengdong Lin tplg.bytes_ext_ops = ops->bytes_ext_ops; 26091a3232d2SMengdong Lin tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; 26108a978234SLiam Girdwood 2611304017d3SBard liao ret = soc_tplg_load(&tplg); 2612304017d3SBard liao /* free the created components if fail to load topology */ 2613304017d3SBard liao if (ret) 2614304017d3SBard liao snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL); 2615304017d3SBard liao 2616304017d3SBard liao return ret; 26178a978234SLiam Girdwood } 26188a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); 26198a978234SLiam Girdwood 26208a978234SLiam Girdwood /* remove this dynamic widget */ 26218a978234SLiam Girdwood void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) 26228a978234SLiam Girdwood { 26238a978234SLiam Girdwood /* make sure we are a widget */ 26248a978234SLiam Girdwood if (w->dobj.type != SND_SOC_DOBJ_WIDGET) 26258a978234SLiam Girdwood return; 26268a978234SLiam Girdwood 26278a978234SLiam Girdwood remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); 26288a978234SLiam Girdwood } 26298a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); 26308a978234SLiam Girdwood 26318a978234SLiam Girdwood /* remove all dynamic widgets from this DAPM context */ 26328a978234SLiam Girdwood void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, 26338a978234SLiam Girdwood u32 index) 26348a978234SLiam Girdwood { 26358a978234SLiam Girdwood struct snd_soc_dapm_widget *w, *next_w; 26368a978234SLiam Girdwood 26378a978234SLiam Girdwood list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { 26388a978234SLiam Girdwood 26398a978234SLiam Girdwood /* make sure we are a widget with correct context */ 26408a978234SLiam Girdwood if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) 26418a978234SLiam Girdwood continue; 26428a978234SLiam Girdwood 26438a978234SLiam Girdwood /* match ID */ 26448a978234SLiam Girdwood if (w->dobj.index != index && 26458a978234SLiam Girdwood w->dobj.index != SND_SOC_TPLG_INDEX_ALL) 26468a978234SLiam Girdwood continue; 26478a978234SLiam Girdwood /* check and free and dynamic widget kcontrols */ 26488a978234SLiam Girdwood snd_soc_tplg_widget_remove(w); 2649b97e2698SLars-Peter Clausen snd_soc_dapm_free_widget(w); 26508a978234SLiam Girdwood } 2651fd589a1bSJyri Sarha snd_soc_dapm_reset_cache(dapm); 26528a978234SLiam Girdwood } 26538a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); 26548a978234SLiam Girdwood 26558a978234SLiam Girdwood /* remove dynamic controls from the component driver */ 26568a978234SLiam Girdwood int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) 26578a978234SLiam Girdwood { 26588a978234SLiam Girdwood struct snd_soc_dobj *dobj, *next_dobj; 26598a978234SLiam Girdwood int pass = SOC_TPLG_PASS_END; 26608a978234SLiam Girdwood 26618a978234SLiam Girdwood /* process the header types from end to start */ 26628a978234SLiam Girdwood while (pass >= SOC_TPLG_PASS_START) { 26638a978234SLiam Girdwood 26648a978234SLiam Girdwood /* remove mixer controls */ 26658a978234SLiam Girdwood list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, 26668a978234SLiam Girdwood list) { 26678a978234SLiam Girdwood 26688a978234SLiam Girdwood /* match index */ 26698a978234SLiam Girdwood if (dobj->index != index && 2670feb12f0cSYan Wang index != SND_SOC_TPLG_INDEX_ALL) 26718a978234SLiam Girdwood continue; 26728a978234SLiam Girdwood 26738a978234SLiam Girdwood switch (dobj->type) { 26748a978234SLiam Girdwood case SND_SOC_DOBJ_MIXER: 26758a978234SLiam Girdwood remove_mixer(comp, dobj, pass); 26768a978234SLiam Girdwood break; 26778a978234SLiam Girdwood case SND_SOC_DOBJ_ENUM: 26788a978234SLiam Girdwood remove_enum(comp, dobj, pass); 26798a978234SLiam Girdwood break; 26808a978234SLiam Girdwood case SND_SOC_DOBJ_BYTES: 26818a978234SLiam Girdwood remove_bytes(comp, dobj, pass); 26828a978234SLiam Girdwood break; 26837df04ea7SRanjani Sridharan case SND_SOC_DOBJ_GRAPH: 26847df04ea7SRanjani Sridharan remove_route(comp, dobj, pass); 26857df04ea7SRanjani Sridharan break; 26868a978234SLiam Girdwood case SND_SOC_DOBJ_WIDGET: 26878a978234SLiam Girdwood remove_widget(comp, dobj, pass); 26888a978234SLiam Girdwood break; 26898a978234SLiam Girdwood case SND_SOC_DOBJ_PCM: 269064527e8aSMengdong Lin remove_dai(comp, dobj, pass); 26918a978234SLiam Girdwood break; 2692acfc7d46SMengdong Lin case SND_SOC_DOBJ_DAI_LINK: 2693acfc7d46SMengdong Lin remove_link(comp, dobj, pass); 2694acfc7d46SMengdong Lin break; 2695adfebb51SBard liao case SND_SOC_DOBJ_BACKEND_LINK: 2696adfebb51SBard liao /* 2697adfebb51SBard liao * call link_unload ops if extra 2698adfebb51SBard liao * deinitialization is needed. 2699adfebb51SBard liao */ 2700adfebb51SBard liao remove_backend_link(comp, dobj, pass); 2701adfebb51SBard liao break; 27028a978234SLiam Girdwood default: 27038a978234SLiam Girdwood dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", 27048a978234SLiam Girdwood dobj->type); 27058a978234SLiam Girdwood break; 27068a978234SLiam Girdwood } 27078a978234SLiam Girdwood } 27088a978234SLiam Girdwood pass--; 27098a978234SLiam Girdwood } 27108a978234SLiam Girdwood 27118a978234SLiam Girdwood /* let caller know if FW can be freed when no objects are left */ 27128a978234SLiam Girdwood return !list_empty(&comp->dobj_list); 27138a978234SLiam Girdwood } 27148a978234SLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); 2715