Kuninori Morimoto | f2b6a1b | 2018-07-02 06:24:45 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // soc-topology.c -- ALSA SoC Topology |
| 4 | // |
| 5 | // Copyright (C) 2012 Texas Instruments Inc. |
| 6 | // Copyright (C) 2015 Intel Corporation. |
| 7 | // |
| 8 | // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 9 | // K, Mythri P <mythri.p.k@intel.com> |
| 10 | // Prusty, Subhransu S <subhransu.s.prusty@intel.com> |
| 11 | // B, Jayachandran <jayachandran.b@intel.com> |
| 12 | // Abdullah, Omair M <omair.m.abdullah@intel.com> |
| 13 | // Jin, Yao <yao.jin@intel.com> |
| 14 | // Lin, Mengdong <mengdong.lin@intel.com> |
| 15 | // |
| 16 | // Add support to read audio firmware topology alongside firmware text. The |
| 17 | // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links, |
| 18 | // equalizers, firmware, coefficients etc. |
| 19 | // |
| 20 | // This file only manages the core ALSA and ASoC components, all other bespoke |
| 21 | // firmware topology data is passed to component drivers for bespoke handling. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/firmware.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | #include <sound/soc-topology.h> |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 31 | #include <sound/tlv.h> |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 32 | |
Pierre-Louis Bossart | 2114171 | 2019-04-04 14:13:58 -0500 | [diff] [blame] | 33 | #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */ |
| 34 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 35 | /* |
| 36 | * We make several passes over the data (since it wont necessarily be ordered) |
| 37 | * and process objects in the following order. This guarantees the component |
| 38 | * drivers will be ready with any vendor data before the mixers and DAPM objects |
| 39 | * are loaded (that may make use of the vendor data). |
| 40 | */ |
| 41 | #define SOC_TPLG_PASS_MANIFEST 0 |
| 42 | #define SOC_TPLG_PASS_VENDOR 1 |
| 43 | #define SOC_TPLG_PASS_MIXER 2 |
| 44 | #define SOC_TPLG_PASS_WIDGET 3 |
Mengdong Lin | 1a8e7fa | 2015-08-10 22:48:30 +0800 | [diff] [blame] | 45 | #define SOC_TPLG_PASS_PCM_DAI 4 |
| 46 | #define SOC_TPLG_PASS_GRAPH 5 |
| 47 | #define SOC_TPLG_PASS_PINS 6 |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 48 | #define SOC_TPLG_PASS_BE_DAI 7 |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 49 | #define SOC_TPLG_PASS_LINK 8 |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 50 | |
| 51 | #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 52 | #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 53 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 54 | /* topology context */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 55 | struct soc_tplg { |
| 56 | const struct firmware *fw; |
| 57 | |
| 58 | /* runtime FW parsing */ |
| 59 | const u8 *pos; /* read postion */ |
| 60 | const u8 *hdr_pos; /* header position */ |
| 61 | unsigned int pass; /* pass number */ |
| 62 | |
| 63 | /* component caller */ |
| 64 | struct device *dev; |
| 65 | struct snd_soc_component *comp; |
| 66 | u32 index; /* current block index */ |
| 67 | u32 req_index; /* required index, only loaded/free matching blocks */ |
| 68 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 69 | /* vendor specific kcontrol operations */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 70 | const struct snd_soc_tplg_kcontrol_ops *io_ops; |
| 71 | int io_ops_count; |
| 72 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 73 | /* vendor specific bytes ext handlers, for TLV bytes controls */ |
| 74 | const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; |
| 75 | int bytes_ext_ops_count; |
| 76 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 77 | /* optional fw loading callbacks to component drivers */ |
| 78 | struct snd_soc_tplg_ops *ops; |
| 79 | }; |
| 80 | |
| 81 | static int soc_tplg_process_headers(struct soc_tplg *tplg); |
| 82 | static void soc_tplg_complete(struct soc_tplg *tplg); |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 83 | static void soc_tplg_denum_remove_texts(struct soc_enum *se); |
| 84 | static void soc_tplg_denum_remove_values(struct soc_enum *se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 85 | |
| 86 | /* check we dont overflow the data for this control chunk */ |
| 87 | static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, |
| 88 | unsigned int count, size_t bytes, const char *elem_type) |
| 89 | { |
| 90 | const u8 *end = tplg->pos + elem_size * count; |
| 91 | |
| 92 | if (end > tplg->fw->data + tplg->fw->size) { |
| 93 | dev_err(tplg->dev, "ASoC: %s overflow end of data\n", |
| 94 | elem_type); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | /* check there is enough room in chunk for control. |
| 99 | extra bytes at the end of control are for vendor data here */ |
| 100 | if (elem_size * count > bytes) { |
| 101 | dev_err(tplg->dev, |
| 102 | "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", |
| 103 | elem_type, count, elem_size, bytes); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static inline int soc_tplg_is_eof(struct soc_tplg *tplg) |
| 111 | { |
| 112 | const u8 *end = tplg->hdr_pos; |
| 113 | |
| 114 | if (end >= tplg->fw->data + tplg->fw->size) |
| 115 | return 1; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) |
| 120 | { |
| 121 | return (unsigned long)(tplg->hdr_pos - tplg->fw->data); |
| 122 | } |
| 123 | |
| 124 | static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) |
| 125 | { |
| 126 | return (unsigned long)(tplg->pos - tplg->fw->data); |
| 127 | } |
| 128 | |
| 129 | /* mapping of Kcontrol types and associated operations. */ |
| 130 | static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { |
| 131 | {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, |
| 132 | snd_soc_put_volsw, snd_soc_info_volsw}, |
| 133 | {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, |
| 134 | snd_soc_put_volsw_sx, NULL}, |
| 135 | {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, |
| 136 | snd_soc_put_enum_double, snd_soc_info_enum_double}, |
| 137 | {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, |
| 138 | snd_soc_put_enum_double, NULL}, |
| 139 | {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, |
| 140 | snd_soc_bytes_put, snd_soc_bytes_info}, |
| 141 | {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, |
| 142 | snd_soc_put_volsw_range, snd_soc_info_volsw_range}, |
| 143 | {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, |
| 144 | snd_soc_put_xr_sx, snd_soc_info_xr_sx}, |
| 145 | {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, |
| 146 | snd_soc_put_strobe, NULL}, |
| 147 | {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, |
Jeeja KP | 2c57d478 | 2015-07-14 13:10:47 +0530 | [diff] [blame] | 148 | snd_soc_dapm_put_volsw, snd_soc_info_volsw}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 149 | {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, |
| 150 | snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, |
| 151 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, |
| 152 | snd_soc_dapm_put_enum_double, NULL}, |
| 153 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, |
| 154 | snd_soc_dapm_put_enum_double, NULL}, |
| 155 | {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, |
| 156 | snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, |
| 157 | }; |
| 158 | |
| 159 | struct soc_tplg_map { |
| 160 | int uid; |
| 161 | int kid; |
| 162 | }; |
| 163 | |
| 164 | /* mapping of widget types from UAPI IDs to kernel IDs */ |
| 165 | static const struct soc_tplg_map dapm_map[] = { |
| 166 | {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, |
| 167 | {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, |
| 168 | {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, |
| 169 | {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, |
| 170 | {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, |
| 171 | {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, |
| 172 | {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, |
| 173 | {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, |
| 174 | {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, |
| 175 | {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, |
| 176 | {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, |
| 177 | {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, |
| 178 | {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, |
| 179 | {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, |
| 180 | {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, |
| 181 | {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, |
Liam Girdwood | 8a70b45 | 2017-06-29 14:22:24 +0100 | [diff] [blame] | 182 | {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer}, |
| 183 | {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler}, |
| 184 | {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect}, |
| 185 | {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen}, |
| 186 | {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src}, |
| 187 | {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc}, |
| 188 | {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder}, |
| 189 | {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static int tplc_chan_get_reg(struct soc_tplg *tplg, |
| 193 | struct snd_soc_tplg_channel *chan, int map) |
| 194 | { |
| 195 | int i; |
| 196 | |
| 197 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 198 | if (le32_to_cpu(chan[i].id) == map) |
| 199 | return le32_to_cpu(chan[i].reg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | static int tplc_chan_get_shift(struct soc_tplg *tplg, |
| 206 | struct snd_soc_tplg_channel *chan, int map) |
| 207 | { |
| 208 | int i; |
| 209 | |
| 210 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 211 | if (le32_to_cpu(chan[i].id) == map) |
| 212 | return le32_to_cpu(chan[i].shift); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | static int get_widget_id(int tplg_type) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { |
| 223 | if (tplg_type == dapm_map[i].uid) |
| 224 | return dapm_map[i].kid; |
| 225 | } |
| 226 | |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 230 | static inline void soc_bind_err(struct soc_tplg *tplg, |
| 231 | struct snd_soc_tplg_ctl_hdr *hdr, int index) |
| 232 | { |
| 233 | dev_err(tplg->dev, |
| 234 | "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", |
| 235 | hdr->ops.get, hdr->ops.put, hdr->ops.info, index, |
| 236 | soc_tplg_get_offset(tplg)); |
| 237 | } |
| 238 | |
| 239 | static inline void soc_control_err(struct soc_tplg *tplg, |
| 240 | struct snd_soc_tplg_ctl_hdr *hdr, const char *name) |
| 241 | { |
| 242 | dev_err(tplg->dev, |
| 243 | "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", |
| 244 | name, hdr->ops.get, hdr->ops.put, hdr->ops.info, |
| 245 | soc_tplg_get_offset(tplg)); |
| 246 | } |
| 247 | |
| 248 | /* pass vendor data to component driver for processing */ |
Keyon Jie | c2cbd0a | 2020-05-27 10:28:01 +0800 | [diff] [blame^] | 249 | static int soc_tplg_vendor_load(struct soc_tplg *tplg, |
| 250 | struct snd_soc_tplg_hdr *hdr) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 251 | { |
| 252 | int ret = 0; |
| 253 | |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 254 | if (tplg->ops && tplg->ops->vendor_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 255 | ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 256 | else { |
| 257 | dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", |
| 258 | hdr->vendor_type); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | if (ret < 0) |
| 263 | dev_err(tplg->dev, |
| 264 | "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", |
| 265 | soc_tplg_get_hdr_offset(tplg), |
| 266 | soc_tplg_get_hdr_offset(tplg), |
| 267 | hdr->type, hdr->vendor_type); |
| 268 | return ret; |
| 269 | } |
| 270 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 271 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 272 | * external widgets where we can assign private data/ops */ |
| 273 | static int soc_tplg_widget_load(struct soc_tplg *tplg, |
| 274 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 275 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 276 | if (tplg->ops && tplg->ops->widget_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 277 | return tplg->ops->widget_load(tplg->comp, tplg->index, w, |
| 278 | tplg_w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 283 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 284 | * external widgets where we can assign private data/ops */ |
| 285 | static int soc_tplg_widget_ready(struct soc_tplg *tplg, |
| 286 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 287 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 288 | if (tplg->ops && tplg->ops->widget_ready) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 289 | return tplg->ops->widget_ready(tplg->comp, tplg->index, w, |
| 290 | tplg_w); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 295 | /* pass DAI configurations to component driver for extra initialization */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 296 | static int soc_tplg_dai_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 297 | struct snd_soc_dai_driver *dai_drv, |
| 298 | struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 299 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 300 | if (tplg->ops && tplg->ops->dai_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 301 | return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, |
| 302 | pcm, dai); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 307 | /* pass link configurations to component driver for extra initialization */ |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 308 | static int soc_tplg_dai_link_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 309 | struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 310 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 311 | if (tplg->ops && tplg->ops->link_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 312 | return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 317 | /* tell the component driver that all firmware has been loaded in this request */ |
| 318 | static void soc_tplg_complete(struct soc_tplg *tplg) |
| 319 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 320 | if (tplg->ops && tplg->ops->complete) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 321 | tplg->ops->complete(tplg->comp); |
| 322 | } |
| 323 | |
| 324 | /* add a dynamic kcontrol */ |
| 325 | static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, |
| 326 | const struct snd_kcontrol_new *control_new, const char *prefix, |
| 327 | void *data, struct snd_kcontrol **kcontrol) |
| 328 | { |
| 329 | int err; |
| 330 | |
| 331 | *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); |
| 332 | if (*kcontrol == NULL) { |
| 333 | dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", |
| 334 | control_new->name); |
| 335 | return -ENOMEM; |
| 336 | } |
| 337 | |
| 338 | err = snd_ctl_add(card, *kcontrol); |
| 339 | if (err < 0) { |
| 340 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
| 341 | control_new->name, err); |
| 342 | return err; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* add a dynamic kcontrol for component driver */ |
| 349 | static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, |
| 350 | struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) |
| 351 | { |
| 352 | struct snd_soc_component *comp = tplg->comp; |
| 353 | |
| 354 | return soc_tplg_add_dcontrol(comp->card->snd_card, |
이경택 | abca9e4a | 2020-04-01 18:05:24 +0900 | [diff] [blame] | 355 | comp->dev, k, comp->name_prefix, comp, kcontrol); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* remove a mixer kcontrol */ |
| 359 | static void remove_mixer(struct snd_soc_component *comp, |
| 360 | struct snd_soc_dobj *dobj, int pass) |
| 361 | { |
| 362 | struct snd_card *card = comp->card->snd_card; |
| 363 | struct soc_mixer_control *sm = |
| 364 | container_of(dobj, struct soc_mixer_control, dobj); |
| 365 | const unsigned int *p = NULL; |
| 366 | |
| 367 | if (pass != SOC_TPLG_PASS_MIXER) |
| 368 | return; |
| 369 | |
| 370 | if (dobj->ops && dobj->ops->control_unload) |
| 371 | dobj->ops->control_unload(comp, dobj); |
| 372 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 373 | if (dobj->control.kcontrol->tlv.p) |
| 374 | p = dobj->control.kcontrol->tlv.p; |
| 375 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 376 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 377 | kfree(sm); |
| 378 | kfree(p); |
| 379 | } |
| 380 | |
| 381 | /* remove an enum kcontrol */ |
| 382 | static void remove_enum(struct snd_soc_component *comp, |
| 383 | struct snd_soc_dobj *dobj, int pass) |
| 384 | { |
| 385 | struct snd_card *card = comp->card->snd_card; |
| 386 | struct soc_enum *se = container_of(dobj, struct soc_enum, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 387 | |
| 388 | if (pass != SOC_TPLG_PASS_MIXER) |
| 389 | return; |
| 390 | |
| 391 | if (dobj->ops && dobj->ops->control_unload) |
| 392 | dobj->ops->control_unload(comp, dobj); |
| 393 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 394 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 395 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 396 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 397 | soc_tplg_denum_remove_values(se); |
| 398 | soc_tplg_denum_remove_texts(se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 399 | kfree(se); |
| 400 | } |
| 401 | |
| 402 | /* remove a byte kcontrol */ |
| 403 | static void remove_bytes(struct snd_soc_component *comp, |
| 404 | struct snd_soc_dobj *dobj, int pass) |
| 405 | { |
| 406 | struct snd_card *card = comp->card->snd_card; |
| 407 | struct soc_bytes_ext *sb = |
| 408 | container_of(dobj, struct soc_bytes_ext, dobj); |
| 409 | |
| 410 | if (pass != SOC_TPLG_PASS_MIXER) |
| 411 | return; |
| 412 | |
| 413 | if (dobj->ops && dobj->ops->control_unload) |
| 414 | dobj->ops->control_unload(comp, dobj); |
| 415 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 416 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 417 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 418 | kfree(sb); |
| 419 | } |
| 420 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 421 | /* remove a route */ |
| 422 | static void remove_route(struct snd_soc_component *comp, |
| 423 | struct snd_soc_dobj *dobj, int pass) |
| 424 | { |
| 425 | struct snd_soc_dapm_route *route = |
| 426 | container_of(dobj, struct snd_soc_dapm_route, dobj); |
| 427 | |
| 428 | if (pass != SOC_TPLG_PASS_GRAPH) |
| 429 | return; |
| 430 | |
| 431 | if (dobj->ops && dobj->ops->dapm_route_unload) |
| 432 | dobj->ops->dapm_route_unload(comp, dobj); |
| 433 | |
| 434 | list_del(&dobj->list); |
| 435 | kfree(route); |
| 436 | } |
| 437 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 438 | /* remove a widget and it's kcontrols - routes must be removed first */ |
| 439 | static void remove_widget(struct snd_soc_component *comp, |
| 440 | struct snd_soc_dobj *dobj, int pass) |
| 441 | { |
| 442 | struct snd_card *card = comp->card->snd_card; |
| 443 | struct snd_soc_dapm_widget *w = |
| 444 | container_of(dobj, struct snd_soc_dapm_widget, dobj); |
| 445 | int i; |
| 446 | |
| 447 | if (pass != SOC_TPLG_PASS_WIDGET) |
| 448 | return; |
| 449 | |
| 450 | if (dobj->ops && dobj->ops->widget_unload) |
| 451 | dobj->ops->widget_unload(comp, dobj); |
| 452 | |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 453 | if (!w->kcontrols) |
| 454 | goto free_news; |
| 455 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 456 | /* |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 457 | * Dynamic Widgets either have 1..N enum kcontrols or mixers. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 458 | * The enum may either have an array of values or strings. |
| 459 | */ |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 460 | if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 461 | /* enumerated widget mixer */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 462 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 463 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
| 464 | struct soc_enum *se = |
| 465 | (struct soc_enum *)kcontrol->private_value; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 466 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 467 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 468 | |
Ranjani Sridharan | 54f8844 | 2019-04-04 19:48:33 -0700 | [diff] [blame] | 469 | /* free enum kcontrol's dvalues and dtexts */ |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 470 | soc_tplg_denum_remove_values(se); |
| 471 | soc_tplg_denum_remove_texts(se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 472 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 473 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 474 | kfree(w->kcontrol_news[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 475 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 476 | } else { |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 477 | /* volume mixer or bytes controls */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 478 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 479 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 480 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 481 | if (dobj->widget.kcontrol_type |
| 482 | == SND_SOC_TPLG_TYPE_MIXER) |
| 483 | kfree(kcontrol->tlv.p); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 484 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 485 | /* Private value is used as struct soc_mixer_control |
| 486 | * for volume mixers or soc_bytes_ext for bytes |
| 487 | * controls. |
| 488 | */ |
| 489 | kfree((void *)kcontrol->private_value); |
Colin Ian King | c2b3612 | 2016-12-09 14:17:47 +0000 | [diff] [blame] | 490 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 491 | kfree(w->kcontrol_news[i].name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 492 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 493 | } |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 494 | |
| 495 | free_news: |
| 496 | kfree(w->kcontrol_news); |
| 497 | |
Amadeusz Sławiński | a46e839 | 2019-01-25 14:06:43 -0600 | [diff] [blame] | 498 | list_del(&dobj->list); |
| 499 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 500 | /* widget w is freed by soc-dapm.c */ |
| 501 | } |
| 502 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 503 | /* remove DAI configurations */ |
| 504 | static void remove_dai(struct snd_soc_component *comp, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 505 | struct snd_soc_dobj *dobj, int pass) |
| 506 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 507 | struct snd_soc_dai_driver *dai_drv = |
| 508 | container_of(dobj, struct snd_soc_dai_driver, dobj); |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 509 | struct snd_soc_dai *dai; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 510 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 511 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 512 | return; |
| 513 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 514 | if (dobj->ops && dobj->ops->dai_unload) |
| 515 | dobj->ops->dai_unload(comp, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 516 | |
Kuninori Morimoto | 43ca5da | 2019-08-20 14:05:32 +0900 | [diff] [blame] | 517 | for_each_component_dais(comp, dai) |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 518 | if (dai->driver == dai_drv) |
| 519 | dai->driver = NULL; |
| 520 | |
Bard liao | 7b6f68a | 2019-03-05 23:57:52 +0800 | [diff] [blame] | 521 | kfree(dai_drv->playback.stream_name); |
| 522 | kfree(dai_drv->capture.stream_name); |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 523 | kfree(dai_drv->name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 524 | list_del(&dobj->list); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 525 | kfree(dai_drv); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 528 | /* remove link configurations */ |
| 529 | static void remove_link(struct snd_soc_component *comp, |
| 530 | struct snd_soc_dobj *dobj, int pass) |
| 531 | { |
| 532 | struct snd_soc_dai_link *link = |
| 533 | container_of(dobj, struct snd_soc_dai_link, dobj); |
| 534 | |
| 535 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 536 | return; |
| 537 | |
| 538 | if (dobj->ops && dobj->ops->link_unload) |
| 539 | dobj->ops->link_unload(comp, dobj); |
| 540 | |
Dragos Tarcatu | dd836dd | 2019-12-04 15:04:47 -0600 | [diff] [blame] | 541 | list_del(&dobj->list); |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 542 | snd_soc_remove_pcm_runtime(comp->card, |
| 543 | snd_soc_get_pcm_runtime(comp->card, link)); |
Dragos Tarcatu | dd836dd | 2019-12-04 15:04:47 -0600 | [diff] [blame] | 544 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 545 | kfree(link->name); |
| 546 | kfree(link->stream_name); |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 547 | kfree(link->cpus->dai_name); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 548 | kfree(link); |
| 549 | } |
| 550 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 551 | /* unload dai link */ |
| 552 | static void remove_backend_link(struct snd_soc_component *comp, |
| 553 | struct snd_soc_dobj *dobj, int pass) |
| 554 | { |
| 555 | if (pass != SOC_TPLG_PASS_LINK) |
| 556 | return; |
| 557 | |
| 558 | if (dobj->ops && dobj->ops->link_unload) |
| 559 | dobj->ops->link_unload(comp, dobj); |
| 560 | |
| 561 | /* |
| 562 | * We don't free the link here as what remove_link() do since BE |
| 563 | * links are not allocated by topology. |
| 564 | * We however need to reset the dobj type to its initial values |
| 565 | */ |
| 566 | dobj->type = SND_SOC_DOBJ_NONE; |
| 567 | list_del(&dobj->list); |
| 568 | } |
| 569 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 570 | /* bind a kcontrol to it's IO handlers */ |
| 571 | static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, |
| 572 | struct snd_kcontrol_new *k, |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 573 | const struct soc_tplg *tplg) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 574 | { |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 575 | const struct snd_soc_tplg_kcontrol_ops *ops; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 576 | const struct snd_soc_tplg_bytes_ext_ops *ext_ops; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 577 | int num_ops, i; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 578 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 579 | if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 580 | && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER |
| 581 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
| 582 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 583 | struct soc_bytes_ext *sbe; |
| 584 | struct snd_soc_tplg_bytes_control *be; |
| 585 | |
| 586 | sbe = (struct soc_bytes_ext *)k->private_value; |
| 587 | be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); |
| 588 | |
| 589 | /* TLV bytes controls need standard kcontrol info handler, |
| 590 | * TLV callback and extended put/get handlers. |
| 591 | */ |
Omair M Abdullah | f4be978 | 2015-11-09 23:20:01 +0530 | [diff] [blame] | 592 | k->info = snd_soc_bytes_info_ext; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 593 | k->tlv.c = snd_soc_bytes_tlv_callback; |
| 594 | |
| 595 | ext_ops = tplg->bytes_ext_ops; |
| 596 | num_ops = tplg->bytes_ext_ops_count; |
| 597 | for (i = 0; i < num_ops; i++) { |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 598 | if (!sbe->put && |
| 599 | ext_ops[i].id == le32_to_cpu(be->ext_ops.put)) |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 600 | sbe->put = ext_ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 601 | if (!sbe->get && |
| 602 | ext_ops[i].id == le32_to_cpu(be->ext_ops.get)) |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 603 | sbe->get = ext_ops[i].get; |
| 604 | } |
| 605 | |
| 606 | if (sbe->put && sbe->get) |
| 607 | return 0; |
| 608 | else |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 612 | /* try and map vendor specific kcontrol handlers first */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 613 | ops = tplg->io_ops; |
| 614 | num_ops = tplg->io_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 615 | for (i = 0; i < num_ops; i++) { |
| 616 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 617 | if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 618 | k->put = ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 619 | if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 620 | k->get = ops[i].get; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 621 | if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 622 | k->info = ops[i].info; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 625 | /* vendor specific handlers found ? */ |
| 626 | if (k->put && k->get && k->info) |
| 627 | return 0; |
| 628 | |
| 629 | /* none found so try standard kcontrol handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 630 | ops = io_ops; |
| 631 | num_ops = ARRAY_SIZE(io_ops); |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 632 | for (i = 0; i < num_ops; i++) { |
| 633 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 634 | if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 635 | k->put = ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 636 | if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 637 | k->get = ops[i].get; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 638 | if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 639 | k->info = ops[i].info; |
| 640 | } |
| 641 | |
| 642 | /* standard handlers found ? */ |
| 643 | if (k->put && k->get && k->info) |
| 644 | return 0; |
| 645 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 646 | /* nothing to bind */ |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
| 650 | /* bind a widgets to it's evnt handlers */ |
| 651 | int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, |
| 652 | const struct snd_soc_tplg_widget_events *events, |
| 653 | int num_events, u16 event_type) |
| 654 | { |
| 655 | int i; |
| 656 | |
| 657 | w->event = NULL; |
| 658 | |
| 659 | for (i = 0; i < num_events; i++) { |
| 660 | if (event_type == events[i].type) { |
| 661 | |
| 662 | /* found - so assign event */ |
| 663 | w->event = events[i].event_handler; |
| 664 | return 0; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /* not found */ |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); |
| 672 | |
| 673 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 674 | static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, |
| 675 | struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) |
| 676 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 677 | if (tplg->ops && tplg->ops->control_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 678 | return tplg->ops->control_load(tplg->comp, tplg->index, k, |
| 679 | hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 684 | |
| 685 | static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, |
| 686 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 687 | { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 688 | unsigned int item_len = 2 * sizeof(unsigned int); |
| 689 | unsigned int *p; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 690 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 691 | p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); |
| 692 | if (!p) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 693 | return -ENOMEM; |
| 694 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 695 | p[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 696 | p[1] = item_len; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 697 | p[2] = le32_to_cpu(scale->min); |
| 698 | p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK) |
| 699 | | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 700 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 701 | kc->tlv.p = (void *)p; |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int soc_tplg_create_tlv(struct soc_tplg *tplg, |
| 706 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) |
| 707 | { |
| 708 | struct snd_soc_tplg_ctl_tlv *tplg_tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 709 | u32 access = le32_to_cpu(tc->access); |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 710 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 711 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 712 | return 0; |
| 713 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 714 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 715 | tplg_tlv = &tc->tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 716 | switch (le32_to_cpu(tplg_tlv->type)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 717 | case SNDRV_CTL_TLVT_DB_SCALE: |
| 718 | return soc_tplg_create_tlv_db_scale(tplg, kc, |
| 719 | &tplg_tlv->scale); |
| 720 | |
| 721 | /* TODO: add support for other TLV types */ |
| 722 | default: |
| 723 | dev_dbg(tplg->dev, "Unsupported TLV type %d\n", |
| 724 | tplg_tlv->type); |
| 725 | return -EINVAL; |
| 726 | } |
| 727 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, |
| 733 | struct snd_kcontrol_new *kc) |
| 734 | { |
| 735 | kfree(kc->tlv.p); |
| 736 | } |
| 737 | |
| 738 | static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, |
| 739 | size_t size) |
| 740 | { |
| 741 | struct snd_soc_tplg_bytes_control *be; |
| 742 | struct soc_bytes_ext *sbe; |
| 743 | struct snd_kcontrol_new kc; |
| 744 | int i, err; |
| 745 | |
| 746 | if (soc_tplg_check_elem_count(tplg, |
| 747 | sizeof(struct snd_soc_tplg_bytes_control), count, |
| 748 | size, "mixer bytes")) { |
| 749 | dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", |
| 750 | count); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
| 754 | for (i = 0; i < count; i++) { |
| 755 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 756 | |
| 757 | /* validate kcontrol */ |
| 758 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 759 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 760 | return -EINVAL; |
| 761 | |
| 762 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 763 | if (sbe == NULL) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 767 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 768 | |
| 769 | dev_dbg(tplg->dev, |
| 770 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 771 | be->hdr.name, be->hdr.access); |
| 772 | |
| 773 | memset(&kc, 0, sizeof(kc)); |
| 774 | kc.name = be->hdr.name; |
| 775 | kc.private_value = (long)sbe; |
| 776 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 777 | kc.access = le32_to_cpu(be->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 778 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 779 | sbe->max = le32_to_cpu(be->max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 780 | sbe->dobj.type = SND_SOC_DOBJ_BYTES; |
| 781 | sbe->dobj.ops = tplg->ops; |
| 782 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 783 | |
| 784 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 785 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 786 | if (err) { |
| 787 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 788 | kfree(sbe); |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | /* pass control to driver for optional further init */ |
| 793 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 794 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 795 | if (err < 0) { |
| 796 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 797 | be->hdr.name); |
| 798 | kfree(sbe); |
| 799 | continue; |
| 800 | } |
| 801 | |
| 802 | /* register control here */ |
| 803 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 804 | &sbe->dobj.control.kcontrol); |
| 805 | if (err < 0) { |
| 806 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 807 | be->hdr.name); |
| 808 | kfree(sbe); |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | list_add(&sbe->dobj.list, &tplg->comp->dobj_list); |
| 813 | } |
| 814 | return 0; |
| 815 | |
| 816 | } |
| 817 | |
| 818 | static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, |
| 819 | size_t size) |
| 820 | { |
| 821 | struct snd_soc_tplg_mixer_control *mc; |
| 822 | struct soc_mixer_control *sm; |
| 823 | struct snd_kcontrol_new kc; |
| 824 | int i, err; |
| 825 | |
| 826 | if (soc_tplg_check_elem_count(tplg, |
| 827 | sizeof(struct snd_soc_tplg_mixer_control), |
| 828 | count, size, "mixers")) { |
| 829 | |
| 830 | dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", |
| 831 | count); |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | |
| 835 | for (i = 0; i < count; i++) { |
| 836 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 837 | |
| 838 | /* validate kcontrol */ |
| 839 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 840 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 841 | return -EINVAL; |
| 842 | |
| 843 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 844 | if (sm == NULL) |
| 845 | return -ENOMEM; |
| 846 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 847 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 848 | |
| 849 | dev_dbg(tplg->dev, |
| 850 | "ASoC: adding mixer kcontrol %s with access 0x%x\n", |
| 851 | mc->hdr.name, mc->hdr.access); |
| 852 | |
| 853 | memset(&kc, 0, sizeof(kc)); |
| 854 | kc.name = mc->hdr.name; |
| 855 | kc.private_value = (long)sm; |
| 856 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 857 | kc.access = le32_to_cpu(mc->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 858 | |
| 859 | /* we only support FL/FR channel mapping atm */ |
| 860 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 861 | SNDRV_CHMAP_FL); |
| 862 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 863 | SNDRV_CHMAP_FR); |
| 864 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 865 | SNDRV_CHMAP_FL); |
| 866 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 867 | SNDRV_CHMAP_FR); |
| 868 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 869 | sm->max = le32_to_cpu(mc->max); |
| 870 | sm->min = le32_to_cpu(mc->min); |
| 871 | sm->invert = le32_to_cpu(mc->invert); |
| 872 | sm->platform_max = le32_to_cpu(mc->platform_max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 873 | sm->dobj.index = tplg->index; |
| 874 | sm->dobj.ops = tplg->ops; |
| 875 | sm->dobj.type = SND_SOC_DOBJ_MIXER; |
| 876 | INIT_LIST_HEAD(&sm->dobj.list); |
| 877 | |
| 878 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 879 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 880 | if (err) { |
| 881 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 882 | kfree(sm); |
| 883 | continue; |
| 884 | } |
| 885 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 886 | /* create any TLV data */ |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 887 | err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr); |
| 888 | if (err < 0) { |
| 889 | dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", |
| 890 | mc->hdr.name); |
| 891 | kfree(sm); |
| 892 | continue; |
| 893 | } |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 894 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 895 | /* pass control to driver for optional further init */ |
| 896 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 897 | (struct snd_soc_tplg_ctl_hdr *) mc); |
| 898 | if (err < 0) { |
| 899 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 900 | mc->hdr.name); |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 901 | soc_tplg_free_tlv(tplg, &kc); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 902 | kfree(sm); |
| 903 | continue; |
| 904 | } |
| 905 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 906 | /* register control here */ |
| 907 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 908 | &sm->dobj.control.kcontrol); |
| 909 | if (err < 0) { |
| 910 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 911 | mc->hdr.name); |
| 912 | soc_tplg_free_tlv(tplg, &kc); |
| 913 | kfree(sm); |
| 914 | continue; |
| 915 | } |
| 916 | |
| 917 | list_add(&sm->dobj.list, &tplg->comp->dobj_list); |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static int soc_tplg_denum_create_texts(struct soc_enum *se, |
| 924 | struct snd_soc_tplg_enum_control *ec) |
| 925 | { |
| 926 | int i, ret; |
| 927 | |
| 928 | se->dobj.control.dtexts = |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 929 | kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 930 | if (se->dobj.control.dtexts == NULL) |
| 931 | return -ENOMEM; |
| 932 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 933 | for (i = 0; i < le32_to_cpu(ec->items); i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 934 | |
| 935 | if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 936 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 937 | ret = -EINVAL; |
| 938 | goto err; |
| 939 | } |
| 940 | |
| 941 | se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); |
| 942 | if (!se->dobj.control.dtexts[i]) { |
| 943 | ret = -ENOMEM; |
| 944 | goto err; |
| 945 | } |
| 946 | } |
| 947 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 948 | se->items = le32_to_cpu(ec->items); |
Mousumi Jana | b6e38b2 | 2017-04-11 13:06:22 +0530 | [diff] [blame] | 949 | se->texts = (const char * const *)se->dobj.control.dtexts; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 950 | return 0; |
| 951 | |
| 952 | err: |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 953 | se->items = i; |
| 954 | soc_tplg_denum_remove_texts(se); |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | static inline void soc_tplg_denum_remove_texts(struct soc_enum *se) |
| 959 | { |
| 960 | int i = se->items; |
| 961 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 962 | for (--i; i >= 0; i--) |
| 963 | kfree(se->dobj.control.dtexts[i]); |
| 964 | kfree(se->dobj.control.dtexts); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static int soc_tplg_denum_create_values(struct soc_enum *se, |
| 968 | struct snd_soc_tplg_enum_control *ec) |
| 969 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 970 | int i; |
| 971 | |
| 972 | if (le32_to_cpu(ec->items) > sizeof(*ec->values)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 973 | return -EINVAL; |
| 974 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 975 | se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) * |
| 976 | sizeof(u32), |
Andrzej Hajda | 376c0af | 2015-08-07 09:59:37 +0200 | [diff] [blame] | 977 | GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 978 | if (!se->dobj.control.dvalues) |
| 979 | return -ENOMEM; |
| 980 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 981 | /* convert from little-endian */ |
| 982 | for (i = 0; i < le32_to_cpu(ec->items); i++) { |
| 983 | se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); |
| 984 | } |
| 985 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 986 | return 0; |
| 987 | } |
| 988 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 989 | static inline void soc_tplg_denum_remove_values(struct soc_enum *se) |
| 990 | { |
| 991 | kfree(se->dobj.control.dvalues); |
| 992 | } |
| 993 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 994 | static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, |
| 995 | size_t size) |
| 996 | { |
| 997 | struct snd_soc_tplg_enum_control *ec; |
| 998 | struct soc_enum *se; |
| 999 | struct snd_kcontrol_new kc; |
| 1000 | int i, ret, err; |
| 1001 | |
| 1002 | if (soc_tplg_check_elem_count(tplg, |
| 1003 | sizeof(struct snd_soc_tplg_enum_control), |
| 1004 | count, size, "enums")) { |
| 1005 | |
| 1006 | dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", |
| 1007 | count); |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | |
| 1011 | for (i = 0; i < count; i++) { |
| 1012 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1013 | |
| 1014 | /* validate kcontrol */ |
| 1015 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1016 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1017 | return -EINVAL; |
| 1018 | |
| 1019 | se = kzalloc((sizeof(*se)), GFP_KERNEL); |
| 1020 | if (se == NULL) |
| 1021 | return -ENOMEM; |
| 1022 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1023 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1024 | le32_to_cpu(ec->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1025 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1026 | dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", |
| 1027 | ec->hdr.name, ec->items); |
| 1028 | |
| 1029 | memset(&kc, 0, sizeof(kc)); |
| 1030 | kc.name = ec->hdr.name; |
| 1031 | kc.private_value = (long)se; |
| 1032 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1033 | kc.access = le32_to_cpu(ec->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1034 | |
| 1035 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1036 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1037 | SNDRV_CHMAP_FL); |
| 1038 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1039 | SNDRV_CHMAP_FL); |
| 1040 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1041 | se->mask = le32_to_cpu(ec->mask); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1042 | se->dobj.index = tplg->index; |
| 1043 | se->dobj.type = SND_SOC_DOBJ_ENUM; |
| 1044 | se->dobj.ops = tplg->ops; |
| 1045 | INIT_LIST_HEAD(&se->dobj.list); |
| 1046 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1047 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1048 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1049 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1050 | err = soc_tplg_denum_create_values(se, ec); |
| 1051 | if (err < 0) { |
| 1052 | dev_err(tplg->dev, |
| 1053 | "ASoC: could not create values for %s\n", |
| 1054 | ec->hdr.name); |
| 1055 | kfree(se); |
| 1056 | continue; |
| 1057 | } |
Takashi Iwai | 9c6c4d9 | 2018-10-04 20:30:06 +0200 | [diff] [blame] | 1058 | /* fall through */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1059 | case SND_SOC_TPLG_CTL_ENUM: |
| 1060 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1061 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1062 | err = soc_tplg_denum_create_texts(se, ec); |
| 1063 | if (err < 0) { |
| 1064 | dev_err(tplg->dev, |
| 1065 | "ASoC: could not create texts for %s\n", |
| 1066 | ec->hdr.name); |
| 1067 | kfree(se); |
| 1068 | continue; |
| 1069 | } |
| 1070 | break; |
| 1071 | default: |
| 1072 | dev_err(tplg->dev, |
| 1073 | "ASoC: invalid enum control type %d for %s\n", |
| 1074 | ec->hdr.ops.info, ec->hdr.name); |
| 1075 | kfree(se); |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1080 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1081 | if (err) { |
| 1082 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1083 | kfree(se); |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | /* pass control to driver for optional further init */ |
| 1088 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 1089 | (struct snd_soc_tplg_ctl_hdr *) ec); |
| 1090 | if (err < 0) { |
| 1091 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1092 | ec->hdr.name); |
| 1093 | kfree(se); |
| 1094 | continue; |
| 1095 | } |
| 1096 | |
| 1097 | /* register control here */ |
| 1098 | ret = soc_tplg_add_kcontrol(tplg, |
| 1099 | &kc, &se->dobj.control.kcontrol); |
| 1100 | if (ret < 0) { |
| 1101 | dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", |
| 1102 | ec->hdr.name); |
| 1103 | kfree(se); |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | list_add(&se->dobj.list, &tplg->comp->dobj_list); |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, |
| 1114 | struct snd_soc_tplg_hdr *hdr) |
| 1115 | { |
| 1116 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1117 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1118 | int i; |
| 1119 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1120 | dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, |
| 1121 | soc_tplg_get_offset(tplg)); |
| 1122 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1123 | for (i = 0; i < le32_to_cpu(hdr->count); i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1124 | |
| 1125 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1126 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1127 | if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1128 | dev_err(tplg->dev, "ASoC: invalid control size\n"); |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1132 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1133 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1134 | case SND_SOC_TPLG_CTL_STROBE: |
| 1135 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1136 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1137 | case SND_SOC_TPLG_CTL_RANGE: |
| 1138 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
| 1139 | case SND_SOC_TPLG_DAPM_CTL_PIN: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1140 | ret = soc_tplg_dmixer_create(tplg, 1, |
| 1141 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | case SND_SOC_TPLG_CTL_ENUM: |
| 1144 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1145 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1146 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1147 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1148 | ret = soc_tplg_denum_create(tplg, 1, |
| 1149 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1150 | break; |
| 1151 | case SND_SOC_TPLG_CTL_BYTES: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1152 | ret = soc_tplg_dbytes_create(tplg, 1, |
| 1153 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
| 1156 | soc_bind_err(tplg, control_hdr, i); |
| 1157 | return -EINVAL; |
| 1158 | } |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1159 | if (ret < 0) { |
| 1160 | dev_err(tplg->dev, "ASoC: invalid control\n"); |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1169 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 1170 | static int soc_tplg_add_route(struct soc_tplg *tplg, |
| 1171 | struct snd_soc_dapm_route *route) |
| 1172 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 1173 | if (tplg->ops && tplg->ops->dapm_route_load) |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1174 | return tplg->ops->dapm_route_load(tplg->comp, tplg->index, |
| 1175 | route); |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1180 | static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, |
| 1181 | struct snd_soc_tplg_hdr *hdr) |
| 1182 | { |
| 1183 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1184 | struct snd_soc_tplg_dapm_graph_elem *elem; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1185 | struct snd_soc_dapm_route **routes; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1186 | int count, i, j; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1187 | int ret = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1188 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1189 | count = le32_to_cpu(hdr->count); |
| 1190 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1191 | if (soc_tplg_check_elem_count(tplg, |
| 1192 | sizeof(struct snd_soc_tplg_dapm_graph_elem), |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1193 | count, le32_to_cpu(hdr->payload_size), "graph")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1194 | |
| 1195 | dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", |
| 1196 | count); |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | |
Liam Girdwood | b75a651 | 2017-06-29 14:22:26 +0100 | [diff] [blame] | 1200 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, |
| 1201 | hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1202 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1203 | /* allocate memory for pointer to array of dapm routes */ |
| 1204 | routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *), |
| 1205 | GFP_KERNEL); |
| 1206 | if (!routes) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | /* |
| 1210 | * allocate memory for each dapm route in the array. |
| 1211 | * This needs to be done individually so that |
| 1212 | * each route can be freed when it is removed in remove_route(). |
| 1213 | */ |
| 1214 | for (i = 0; i < count; i++) { |
| 1215 | routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL); |
| 1216 | if (!routes[i]) { |
| 1217 | /* free previously allocated memory */ |
| 1218 | for (j = 0; j < i; j++) |
| 1219 | kfree(routes[j]); |
| 1220 | |
| 1221 | kfree(routes); |
| 1222 | return -ENOMEM; |
| 1223 | } |
| 1224 | } |
| 1225 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1226 | for (i = 0; i < count; i++) { |
| 1227 | elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; |
| 1228 | tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); |
| 1229 | |
| 1230 | /* validate routes */ |
| 1231 | if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1232 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1233 | ret = -EINVAL; |
| 1234 | break; |
| 1235 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1236 | if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1237 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1238 | ret = -EINVAL; |
| 1239 | break; |
| 1240 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1241 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1242 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1243 | ret = -EINVAL; |
| 1244 | break; |
| 1245 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1246 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1247 | routes[i]->source = elem->source; |
| 1248 | routes[i]->sink = elem->sink; |
| 1249 | |
| 1250 | /* set to NULL atm for tplg users */ |
| 1251 | routes[i]->connected = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1252 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1253 | routes[i]->control = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1254 | else |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1255 | routes[i]->control = elem->control; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1256 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1257 | /* add route dobj to dobj_list */ |
| 1258 | routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH; |
| 1259 | routes[i]->dobj.ops = tplg->ops; |
| 1260 | routes[i]->dobj.index = tplg->index; |
| 1261 | list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list); |
| 1262 | |
Amadeusz Sławiński | 6856e88 | 2020-03-27 16:47:27 -0400 | [diff] [blame] | 1263 | ret = soc_tplg_add_route(tplg, routes[i]); |
| 1264 | if (ret < 0) |
| 1265 | break; |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1266 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1267 | /* add route, but keep going if some fail */ |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1268 | snd_soc_dapm_add_routes(dapm, routes[i], 1); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1271 | /* free memory allocated for all dapm routes in case of error */ |
| 1272 | if (ret < 0) |
| 1273 | for (i = 0; i < count ; i++) |
| 1274 | kfree(routes[i]); |
| 1275 | |
| 1276 | /* |
| 1277 | * free pointer to array of dapm routes as this is no longer needed. |
| 1278 | * The memory allocated for each dapm route will be freed |
| 1279 | * when it is removed in remove_route(). |
| 1280 | */ |
| 1281 | kfree(routes); |
| 1282 | |
| 1283 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( |
| 1287 | struct soc_tplg *tplg, int num_kcontrols) |
| 1288 | { |
| 1289 | struct snd_kcontrol_new *kc; |
| 1290 | struct soc_mixer_control *sm; |
| 1291 | struct snd_soc_tplg_mixer_control *mc; |
| 1292 | int i, err; |
| 1293 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1294 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1295 | if (kc == NULL) |
| 1296 | return NULL; |
| 1297 | |
| 1298 | for (i = 0; i < num_kcontrols; i++) { |
| 1299 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1300 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1301 | /* validate kcontrol */ |
| 1302 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1303 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1304 | goto err_sm; |
| 1305 | |
| 1306 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 1307 | if (sm == NULL) |
| 1308 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1309 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1310 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1311 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1312 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1313 | dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", |
| 1314 | mc->hdr.name, i); |
| 1315 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1316 | kc[i].private_value = (long)sm; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1317 | kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL); |
| 1318 | if (kc[i].name == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1319 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1320 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1321 | kc[i].access = le32_to_cpu(mc->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1322 | |
| 1323 | /* we only support FL/FR channel mapping atm */ |
| 1324 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 1325 | SNDRV_CHMAP_FL); |
| 1326 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 1327 | SNDRV_CHMAP_FR); |
| 1328 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 1329 | SNDRV_CHMAP_FL); |
| 1330 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 1331 | SNDRV_CHMAP_FR); |
| 1332 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1333 | sm->max = le32_to_cpu(mc->max); |
| 1334 | sm->min = le32_to_cpu(mc->min); |
| 1335 | sm->invert = le32_to_cpu(mc->invert); |
| 1336 | sm->platform_max = le32_to_cpu(mc->platform_max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1337 | sm->dobj.index = tplg->index; |
| 1338 | INIT_LIST_HEAD(&sm->dobj.list); |
| 1339 | |
| 1340 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1341 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1342 | if (err) { |
| 1343 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1344 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1347 | /* create any TLV data */ |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 1348 | err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr); |
| 1349 | if (err < 0) { |
| 1350 | dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", |
| 1351 | mc->hdr.name); |
| 1352 | kfree(sm); |
| 1353 | continue; |
| 1354 | } |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1355 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1356 | /* pass control to driver for optional further init */ |
| 1357 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1358 | (struct snd_soc_tplg_ctl_hdr *)mc); |
| 1359 | if (err < 0) { |
| 1360 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1361 | mc->hdr.name); |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1362 | soc_tplg_free_tlv(tplg, &kc[i]); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1363 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | return kc; |
| 1367 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1368 | err_sm: |
| 1369 | for (; i >= 0; i--) { |
| 1370 | sm = (struct soc_mixer_control *)kc[i].private_value; |
| 1371 | kfree(sm); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1372 | kfree(kc[i].name); |
| 1373 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1374 | kfree(kc); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1375 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1376 | return NULL; |
| 1377 | } |
| 1378 | |
| 1379 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1380 | struct soc_tplg *tplg, int num_kcontrols) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1381 | { |
| 1382 | struct snd_kcontrol_new *kc; |
| 1383 | struct snd_soc_tplg_enum_control *ec; |
| 1384 | struct soc_enum *se; |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 1385 | int i, err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1386 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1387 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1388 | if (kc == NULL) |
| 1389 | return NULL; |
| 1390 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1391 | for (i = 0; i < num_kcontrols; i++) { |
| 1392 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
| 1393 | /* validate kcontrol */ |
| 1394 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1395 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1396 | goto err_se; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1397 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1398 | se = kzalloc(sizeof(*se), GFP_KERNEL); |
| 1399 | if (se == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1400 | goto err_se; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1401 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1402 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1403 | le32_to_cpu(ec->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1404 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1405 | dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1406 | ec->hdr.name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1407 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1408 | kc[i].private_value = (long)se; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1409 | kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1410 | if (kc[i].name == NULL) |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1411 | goto err_se; |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1412 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1413 | kc[i].access = le32_to_cpu(ec->hdr.access); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1414 | |
| 1415 | /* we only support FL/FR channel mapping atm */ |
| 1416 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1417 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1418 | SNDRV_CHMAP_FL); |
| 1419 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1420 | SNDRV_CHMAP_FR); |
| 1421 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1422 | se->items = le32_to_cpu(ec->items); |
| 1423 | se->mask = le32_to_cpu(ec->mask); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1424 | se->dobj.index = tplg->index; |
| 1425 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1426 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1427 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1428 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1429 | err = soc_tplg_denum_create_values(se, ec); |
| 1430 | if (err < 0) { |
| 1431 | dev_err(tplg->dev, "ASoC: could not create values for %s\n", |
| 1432 | ec->hdr.name); |
| 1433 | goto err_se; |
| 1434 | } |
Takashi Iwai | 9c6c4d9 | 2018-10-04 20:30:06 +0200 | [diff] [blame] | 1435 | /* fall through */ |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1436 | case SND_SOC_TPLG_CTL_ENUM: |
| 1437 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1438 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1439 | err = soc_tplg_denum_create_texts(se, ec); |
| 1440 | if (err < 0) { |
| 1441 | dev_err(tplg->dev, "ASoC: could not create texts for %s\n", |
| 1442 | ec->hdr.name); |
| 1443 | goto err_se; |
| 1444 | } |
| 1445 | break; |
| 1446 | default: |
| 1447 | dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", |
| 1448 | ec->hdr.ops.info, ec->hdr.name); |
| 1449 | goto err_se; |
| 1450 | } |
| 1451 | |
| 1452 | /* map io handlers */ |
| 1453 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg); |
| 1454 | if (err) { |
| 1455 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1456 | goto err_se; |
| 1457 | } |
| 1458 | |
| 1459 | /* pass control to driver for optional further init */ |
| 1460 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1461 | (struct snd_soc_tplg_ctl_hdr *)ec); |
| 1462 | if (err < 0) { |
| 1463 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1464 | ec->hdr.name); |
| 1465 | goto err_se; |
| 1466 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | return kc; |
| 1470 | |
| 1471 | err_se: |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1472 | for (; i >= 0; i--) { |
| 1473 | /* free values and texts */ |
| 1474 | se = (struct soc_enum *)kc[i].private_value; |
Christophe JAILLET | 6d5574e | 2017-09-14 22:44:12 +0200 | [diff] [blame] | 1475 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1476 | if (se) { |
| 1477 | soc_tplg_denum_remove_values(se); |
| 1478 | soc_tplg_denum_remove_texts(se); |
| 1479 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1480 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1481 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1482 | kfree(kc[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1483 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1484 | kfree(kc); |
| 1485 | |
| 1486 | return NULL; |
| 1487 | } |
| 1488 | |
| 1489 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1490 | struct soc_tplg *tplg, int num_kcontrols) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1491 | { |
| 1492 | struct snd_soc_tplg_bytes_control *be; |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1493 | struct soc_bytes_ext *sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1494 | struct snd_kcontrol_new *kc; |
| 1495 | int i, err; |
| 1496 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1497 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1498 | if (!kc) |
| 1499 | return NULL; |
| 1500 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1501 | for (i = 0; i < num_kcontrols; i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1502 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 1503 | |
| 1504 | /* validate kcontrol */ |
| 1505 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1506 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1507 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1508 | |
| 1509 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 1510 | if (sbe == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1511 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1512 | |
| 1513 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1514 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1515 | |
| 1516 | dev_dbg(tplg->dev, |
| 1517 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 1518 | be->hdr.name, be->hdr.access); |
| 1519 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1520 | kc[i].private_value = (long)sbe; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1521 | kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1522 | if (kc[i].name == NULL) |
| 1523 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1524 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1525 | kc[i].access = le32_to_cpu(be->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1526 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1527 | sbe->max = le32_to_cpu(be->max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1528 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 1529 | |
| 1530 | /* map standard io handlers and check for external handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1531 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1532 | if (err) { |
| 1533 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1534 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* pass control to driver for optional further init */ |
| 1538 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1539 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 1540 | if (err < 0) { |
| 1541 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1542 | be->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1543 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | return kc; |
| 1548 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1549 | err_sbe: |
| 1550 | for (; i >= 0; i--) { |
| 1551 | sbe = (struct soc_bytes_ext *)kc[i].private_value; |
| 1552 | kfree(sbe); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1553 | kfree(kc[i].name); |
| 1554 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1555 | kfree(kc); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1556 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1557 | return NULL; |
| 1558 | } |
| 1559 | |
| 1560 | static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, |
| 1561 | struct snd_soc_tplg_dapm_widget *w) |
| 1562 | { |
| 1563 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
| 1564 | struct snd_soc_dapm_widget template, *widget; |
| 1565 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1566 | struct snd_soc_card *card = tplg->comp->card; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1567 | unsigned int kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1568 | int ret = 0; |
| 1569 | |
| 1570 | if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1571 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1572 | return -EINVAL; |
| 1573 | if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1574 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1575 | return -EINVAL; |
| 1576 | |
| 1577 | dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", |
| 1578 | w->name, w->id); |
| 1579 | |
| 1580 | memset(&template, 0, sizeof(template)); |
| 1581 | |
| 1582 | /* map user to kernel widget ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1583 | template.id = get_widget_id(le32_to_cpu(w->id)); |
Dan Carpenter | 752c938 | 2019-09-25 14:06:24 +0300 | [diff] [blame] | 1584 | if ((int)template.id < 0) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1585 | return template.id; |
| 1586 | |
Liam Girdwood | c3421a6 | 2017-06-06 15:45:09 +0100 | [diff] [blame] | 1587 | /* strings are allocated here, but used and freed by the widget */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1588 | template.name = kstrdup(w->name, GFP_KERNEL); |
| 1589 | if (!template.name) |
| 1590 | return -ENOMEM; |
| 1591 | template.sname = kstrdup(w->sname, GFP_KERNEL); |
| 1592 | if (!template.sname) { |
| 1593 | ret = -ENOMEM; |
| 1594 | goto err; |
| 1595 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1596 | template.reg = le32_to_cpu(w->reg); |
| 1597 | template.shift = le32_to_cpu(w->shift); |
| 1598 | template.mask = le32_to_cpu(w->mask); |
| 1599 | template.subseq = le32_to_cpu(w->subseq); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1600 | template.on_val = w->invert ? 0 : 1; |
| 1601 | template.off_val = w->invert ? 1 : 0; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1602 | template.ignore_suspend = le32_to_cpu(w->ignore_suspend); |
| 1603 | template.event_flags = le16_to_cpu(w->event_flags); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1604 | template.dobj.index = tplg->index; |
| 1605 | |
| 1606 | tplg->pos += |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1607 | (sizeof(struct snd_soc_tplg_dapm_widget) + |
| 1608 | le32_to_cpu(w->priv.size)); |
| 1609 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1610 | if (w->num_kcontrols == 0) { |
Arnd Bergmann | dd5abb7 | 2016-12-09 12:51:46 +0100 | [diff] [blame] | 1611 | kcontrol_type = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1612 | template.num_kcontrols = 0; |
| 1613 | goto widget; |
| 1614 | } |
| 1615 | |
| 1616 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1617 | dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", |
| 1618 | w->name, w->num_kcontrols, control_hdr->type); |
| 1619 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1620 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1621 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1622 | case SND_SOC_TPLG_CTL_STROBE: |
| 1623 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1624 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1625 | case SND_SOC_TPLG_CTL_RANGE: |
| 1626 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1627 | kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1628 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1629 | template.kcontrol_news = |
| 1630 | soc_tplg_dapm_widget_dmixer_create(tplg, |
| 1631 | template.num_kcontrols); |
| 1632 | if (!template.kcontrol_news) { |
| 1633 | ret = -ENOMEM; |
| 1634 | goto hdr_err; |
| 1635 | } |
| 1636 | break; |
| 1637 | case SND_SOC_TPLG_CTL_ENUM: |
| 1638 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1639 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1640 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1641 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1642 | kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1643 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1644 | template.kcontrol_news = |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1645 | soc_tplg_dapm_widget_denum_create(tplg, |
| 1646 | template.num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1647 | if (!template.kcontrol_news) { |
| 1648 | ret = -ENOMEM; |
| 1649 | goto hdr_err; |
| 1650 | } |
| 1651 | break; |
| 1652 | case SND_SOC_TPLG_CTL_BYTES: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1653 | kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1654 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1655 | template.kcontrol_news = |
| 1656 | soc_tplg_dapm_widget_dbytes_create(tplg, |
| 1657 | template.num_kcontrols); |
| 1658 | if (!template.kcontrol_news) { |
| 1659 | ret = -ENOMEM; |
| 1660 | goto hdr_err; |
| 1661 | } |
| 1662 | break; |
| 1663 | default: |
| 1664 | dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", |
| 1665 | control_hdr->ops.get, control_hdr->ops.put, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1666 | le32_to_cpu(control_hdr->ops.info)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1667 | ret = -EINVAL; |
| 1668 | goto hdr_err; |
| 1669 | } |
| 1670 | |
| 1671 | widget: |
| 1672 | ret = soc_tplg_widget_load(tplg, &template, w); |
| 1673 | if (ret < 0) |
| 1674 | goto hdr_err; |
| 1675 | |
| 1676 | /* card dapm mutex is held by the core if we are loading topology |
| 1677 | * data during sound card init. */ |
| 1678 | if (card->instantiated) |
| 1679 | widget = snd_soc_dapm_new_control(dapm, &template); |
| 1680 | else |
| 1681 | widget = snd_soc_dapm_new_control_unlocked(dapm, &template); |
Linus Walleij | 37e1df8 | 2017-01-13 10:23:52 +0100 | [diff] [blame] | 1682 | if (IS_ERR(widget)) { |
| 1683 | ret = PTR_ERR(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1684 | goto hdr_err; |
| 1685 | } |
| 1686 | |
| 1687 | widget->dobj.type = SND_SOC_DOBJ_WIDGET; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1688 | widget->dobj.widget.kcontrol_type = kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1689 | widget->dobj.ops = tplg->ops; |
| 1690 | widget->dobj.index = tplg->index; |
| 1691 | list_add(&widget->dobj.list, &tplg->comp->dobj_list); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1692 | |
| 1693 | ret = soc_tplg_widget_ready(tplg, widget, w); |
| 1694 | if (ret < 0) |
| 1695 | goto ready_err; |
| 1696 | |
Bard liao | 7620fe9 | 2019-01-25 14:06:45 -0600 | [diff] [blame] | 1697 | kfree(template.sname); |
| 1698 | kfree(template.name); |
| 1699 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1700 | return 0; |
| 1701 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1702 | ready_err: |
| 1703 | snd_soc_tplg_widget_remove(widget); |
| 1704 | snd_soc_dapm_free_widget(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1705 | hdr_err: |
| 1706 | kfree(template.sname); |
| 1707 | err: |
| 1708 | kfree(template.name); |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, |
| 1713 | struct snd_soc_tplg_hdr *hdr) |
| 1714 | { |
| 1715 | struct snd_soc_tplg_dapm_widget *widget; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1716 | int ret, count, i; |
| 1717 | |
| 1718 | count = le32_to_cpu(hdr->count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1719 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1720 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); |
| 1721 | |
| 1722 | for (i = 0; i < count; i++) { |
| 1723 | widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1724 | if (le32_to_cpu(widget->size) != sizeof(*widget)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1725 | dev_err(tplg->dev, "ASoC: invalid widget size\n"); |
| 1726 | return -EINVAL; |
| 1727 | } |
| 1728 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1729 | ret = soc_tplg_dapm_widget_create(tplg, widget); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1730 | if (ret < 0) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1731 | dev_err(tplg->dev, "ASoC: failed to load widget %s\n", |
| 1732 | widget->name); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1733 | return ret; |
| 1734 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | static int soc_tplg_dapm_complete(struct soc_tplg *tplg) |
| 1741 | { |
| 1742 | struct snd_soc_card *card = tplg->comp->card; |
| 1743 | int ret; |
| 1744 | |
| 1745 | /* Card might not have been registered at this point. |
| 1746 | * If so, just return success. |
| 1747 | */ |
| 1748 | if (!card || !card->instantiated) { |
| 1749 | dev_warn(tplg->dev, "ASoC: Parent card not yet available," |
Liam Girdwood | cc9d471 | 2017-06-06 15:45:08 +0100 | [diff] [blame] | 1750 | " widget card binding deferred\n"); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | ret = snd_soc_dapm_new_widgets(card); |
| 1755 | if (ret < 0) |
| 1756 | dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", |
| 1757 | ret); |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1762 | static int set_stream_info(struct snd_soc_pcm_stream *stream, |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1763 | struct snd_soc_tplg_stream_caps *caps) |
| 1764 | { |
| 1765 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1766 | if (!stream->stream_name) |
| 1767 | return -ENOMEM; |
| 1768 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1769 | stream->channels_min = le32_to_cpu(caps->channels_min); |
| 1770 | stream->channels_max = le32_to_cpu(caps->channels_max); |
| 1771 | stream->rates = le32_to_cpu(caps->rates); |
| 1772 | stream->rate_min = le32_to_cpu(caps->rate_min); |
| 1773 | stream->rate_max = le32_to_cpu(caps->rate_max); |
| 1774 | stream->formats = le64_to_cpu(caps->formats); |
| 1775 | stream->sig_bits = le32_to_cpu(caps->sig_bits); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1776 | |
| 1777 | return 0; |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1778 | } |
| 1779 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 1780 | static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, |
| 1781 | unsigned int flag_mask, unsigned int flags) |
| 1782 | { |
| 1783 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) |
| 1784 | dai_drv->symmetric_rates = |
| 1785 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1786 | |
| 1787 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) |
| 1788 | dai_drv->symmetric_channels = |
| 1789 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1790 | 1 : 0; |
| 1791 | |
| 1792 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1793 | dai_drv->symmetric_samplebits = |
| 1794 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1795 | 1 : 0; |
| 1796 | } |
| 1797 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1798 | static int soc_tplg_dai_create(struct soc_tplg *tplg, |
| 1799 | struct snd_soc_tplg_pcm *pcm) |
| 1800 | { |
| 1801 | struct snd_soc_dai_driver *dai_drv; |
| 1802 | struct snd_soc_pcm_stream *stream; |
| 1803 | struct snd_soc_tplg_stream_caps *caps; |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1804 | struct snd_soc_dai *dai; |
| 1805 | struct snd_soc_dapm_context *dapm = |
| 1806 | snd_soc_component_get_dapm(tplg->comp); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1807 | int ret; |
| 1808 | |
| 1809 | dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); |
| 1810 | if (dai_drv == NULL) |
| 1811 | return -ENOMEM; |
| 1812 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1813 | if (strlen(pcm->dai_name)) { |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1814 | dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1815 | if (!dai_drv->name) { |
| 1816 | ret = -ENOMEM; |
| 1817 | goto err; |
| 1818 | } |
| 1819 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1820 | dai_drv->id = le32_to_cpu(pcm->dai_id); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1821 | |
| 1822 | if (pcm->playback) { |
| 1823 | stream = &dai_drv->playback; |
| 1824 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1825 | ret = set_stream_info(stream, caps); |
| 1826 | if (ret < 0) |
| 1827 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | if (pcm->capture) { |
| 1831 | stream = &dai_drv->capture; |
| 1832 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1833 | ret = set_stream_info(stream, caps); |
| 1834 | if (ret < 0) |
| 1835 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1836 | } |
| 1837 | |
Liam Girdwood | 5db6aab | 2018-03-27 14:30:45 +0100 | [diff] [blame] | 1838 | if (pcm->compress) |
| 1839 | dai_drv->compress_new = snd_soc_new_compress; |
| 1840 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1841 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1842 | ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1843 | if (ret < 0) { |
| 1844 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1845 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | dai_drv->dobj.index = tplg->index; |
| 1849 | dai_drv->dobj.ops = tplg->ops; |
| 1850 | dai_drv->dobj.type = SND_SOC_DOBJ_PCM; |
| 1851 | list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); |
| 1852 | |
| 1853 | /* register the DAI to the component */ |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1854 | dai = snd_soc_register_dai(tplg->comp, dai_drv, false); |
| 1855 | if (!dai) |
| 1856 | return -ENOMEM; |
| 1857 | |
| 1858 | /* Create the DAI widgets here */ |
| 1859 | ret = snd_soc_dapm_new_dai_widgets(dapm, dai); |
| 1860 | if (ret != 0) { |
| 1861 | dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret); |
| 1862 | snd_soc_unregister_dai(dai); |
| 1863 | return ret; |
| 1864 | } |
| 1865 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1866 | return 0; |
| 1867 | |
| 1868 | err: |
| 1869 | kfree(dai_drv->playback.stream_name); |
| 1870 | kfree(dai_drv->capture.stream_name); |
| 1871 | kfree(dai_drv->name); |
| 1872 | kfree(dai_drv); |
| 1873 | |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1874 | return ret; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1877 | static void set_link_flags(struct snd_soc_dai_link *link, |
| 1878 | unsigned int flag_mask, unsigned int flags) |
| 1879 | { |
| 1880 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) |
| 1881 | link->symmetric_rates = |
| 1882 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1883 | |
| 1884 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) |
| 1885 | link->symmetric_channels = |
| 1886 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1887 | 1 : 0; |
| 1888 | |
| 1889 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1890 | link->symmetric_samplebits = |
| 1891 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1892 | 1 : 0; |
Mengdong Lin | 6ff67cc | 2016-11-03 01:05:32 +0800 | [diff] [blame] | 1893 | |
| 1894 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) |
| 1895 | link->ignore_suspend = |
| 1896 | flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ? |
| 1897 | 1 : 0; |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1898 | } |
| 1899 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1900 | /* create the FE DAI link */ |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 1901 | static int soc_tplg_fe_link_create(struct soc_tplg *tplg, |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1902 | struct snd_soc_tplg_pcm *pcm) |
| 1903 | { |
| 1904 | struct snd_soc_dai_link *link; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1905 | struct snd_soc_dai_link_component *dlc; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1906 | int ret; |
| 1907 | |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1908 | /* link + cpu + codec + platform */ |
| 1909 | link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1910 | if (link == NULL) |
| 1911 | return -ENOMEM; |
| 1912 | |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1913 | dlc = (struct snd_soc_dai_link_component *)(link + 1); |
| 1914 | |
| 1915 | link->cpus = &dlc[0]; |
| 1916 | link->codecs = &dlc[1]; |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1917 | link->platforms = &dlc[2]; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1918 | |
| 1919 | link->num_cpus = 1; |
| 1920 | link->num_codecs = 1; |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1921 | link->num_platforms = 1; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1922 | |
Jaroslav Kysela | 8ce1cbd6 | 2020-01-22 20:07:52 +0100 | [diff] [blame] | 1923 | link->dobj.index = tplg->index; |
| 1924 | link->dobj.ops = tplg->ops; |
| 1925 | link->dobj.type = SND_SOC_DOBJ_DAI_LINK; |
| 1926 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1927 | if (strlen(pcm->pcm_name)) { |
| 1928 | link->name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
| 1929 | link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1930 | if (!link->name || !link->stream_name) { |
| 1931 | ret = -ENOMEM; |
| 1932 | goto err; |
| 1933 | } |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1934 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1935 | link->id = le32_to_cpu(pcm->pcm_id); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1936 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1937 | if (strlen(pcm->dai_name)) { |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1938 | link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1939 | if (!link->cpus->dai_name) { |
| 1940 | ret = -ENOMEM; |
| 1941 | goto err; |
| 1942 | } |
| 1943 | } |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1944 | |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1945 | link->codecs->name = "snd-soc-dummy"; |
| 1946 | link->codecs->dai_name = "snd-soc-dummy-dai"; |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1947 | |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1948 | link->platforms->name = "snd-soc-dummy"; |
| 1949 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1950 | /* enable DPCM */ |
| 1951 | link->dynamic = 1; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1952 | link->dpcm_playback = le32_to_cpu(pcm->playback); |
| 1953 | link->dpcm_capture = le32_to_cpu(pcm->capture); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1954 | if (pcm->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1955 | set_link_flags(link, |
| 1956 | le32_to_cpu(pcm->flag_mask), |
| 1957 | le32_to_cpu(pcm->flags)); |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1958 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1959 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1960 | ret = soc_tplg_dai_link_load(tplg, link, NULL); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1961 | if (ret < 0) { |
| 1962 | dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n"); |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1963 | goto err; |
| 1964 | } |
| 1965 | |
Mark Brown | 2acf6ce | 2019-12-10 13:27:14 +0000 | [diff] [blame] | 1966 | ret = snd_soc_add_pcm_runtime(tplg->comp->card, link); |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1967 | if (ret < 0) { |
| 1968 | dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n"); |
| 1969 | goto err; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1970 | } |
| 1971 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1972 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 1973 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1974 | return 0; |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1975 | err: |
| 1976 | kfree(link->name); |
| 1977 | kfree(link->stream_name); |
| 1978 | kfree(link->cpus->dai_name); |
| 1979 | kfree(link); |
| 1980 | return ret; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* create a FE DAI and DAI link from the PCM object */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1984 | static int soc_tplg_pcm_create(struct soc_tplg *tplg, |
| 1985 | struct snd_soc_tplg_pcm *pcm) |
| 1986 | { |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1987 | int ret; |
| 1988 | |
| 1989 | ret = soc_tplg_dai_create(tplg, pcm); |
| 1990 | if (ret < 0) |
| 1991 | return ret; |
| 1992 | |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 1993 | return soc_tplg_fe_link_create(tplg, pcm); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1996 | /* copy stream caps from the old version 4 of source */ |
| 1997 | static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, |
| 1998 | struct snd_soc_tplg_stream_caps_v4 *src) |
| 1999 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2000 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2001 | memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2002 | dest->formats = src->formats; |
| 2003 | dest->rates = src->rates; |
| 2004 | dest->rate_min = src->rate_min; |
| 2005 | dest->rate_max = src->rate_max; |
| 2006 | dest->channels_min = src->channels_min; |
| 2007 | dest->channels_max = src->channels_max; |
| 2008 | dest->periods_min = src->periods_min; |
| 2009 | dest->periods_max = src->periods_max; |
| 2010 | dest->period_size_min = src->period_size_min; |
| 2011 | dest->period_size_max = src->period_size_max; |
| 2012 | dest->buffer_size_min = src->buffer_size_min; |
| 2013 | dest->buffer_size_max = src->buffer_size_max; |
| 2014 | } |
| 2015 | |
| 2016 | /** |
| 2017 | * pcm_new_ver - Create the new version of PCM from the old version. |
| 2018 | * @tplg: topology context |
| 2019 | * @src: older version of pcm as a source |
| 2020 | * @pcm: latest version of pcm created from the source |
| 2021 | * |
| 2022 | * Support from vesion 4. User should free the returned pcm manually. |
| 2023 | */ |
| 2024 | static int pcm_new_ver(struct soc_tplg *tplg, |
| 2025 | struct snd_soc_tplg_pcm *src, |
| 2026 | struct snd_soc_tplg_pcm **pcm) |
| 2027 | { |
| 2028 | struct snd_soc_tplg_pcm *dest; |
| 2029 | struct snd_soc_tplg_pcm_v4 *src_v4; |
| 2030 | int i; |
| 2031 | |
| 2032 | *pcm = NULL; |
| 2033 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2034 | if (le32_to_cpu(src->size) != sizeof(*src_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2035 | dev_err(tplg->dev, "ASoC: invalid PCM size\n"); |
| 2036 | return -EINVAL; |
| 2037 | } |
| 2038 | |
| 2039 | dev_warn(tplg->dev, "ASoC: old version of PCM\n"); |
| 2040 | src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; |
| 2041 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 2042 | if (!dest) |
| 2043 | return -ENOMEM; |
| 2044 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2045 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2046 | memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2047 | memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2048 | dest->pcm_id = src_v4->pcm_id; |
| 2049 | dest->dai_id = src_v4->dai_id; |
| 2050 | dest->playback = src_v4->playback; |
| 2051 | dest->capture = src_v4->capture; |
| 2052 | dest->compress = src_v4->compress; |
| 2053 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2054 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2055 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 2056 | sizeof(struct snd_soc_tplg_stream)); |
| 2057 | |
| 2058 | for (i = 0; i < 2; i++) |
| 2059 | stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); |
| 2060 | |
| 2061 | *pcm = dest; |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2065 | static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2066 | struct snd_soc_tplg_hdr *hdr) |
| 2067 | { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2068 | struct snd_soc_tplg_pcm *pcm, *_pcm; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2069 | int count; |
| 2070 | int size; |
Vinod Koul | fd34045 | 2016-12-08 23:01:27 +0530 | [diff] [blame] | 2071 | int i; |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2072 | bool abi_match; |
Dragos Tarcatu | a3039ae | 2019-12-09 18:39:39 -0600 | [diff] [blame] | 2073 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2074 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2075 | count = le32_to_cpu(hdr->count); |
| 2076 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2077 | /* check the element size and count */ |
| 2078 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2079 | size = le32_to_cpu(pcm->size); |
| 2080 | if (size > sizeof(struct snd_soc_tplg_pcm) |
| 2081 | || size < sizeof(struct snd_soc_tplg_pcm_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2082 | dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2083 | size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2084 | return -EINVAL; |
| 2085 | } |
| 2086 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2087 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2088 | size, count, |
| 2089 | le32_to_cpu(hdr->payload_size), |
| 2090 | "PCM DAI")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2091 | dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", |
| 2092 | count); |
| 2093 | return -EINVAL; |
| 2094 | } |
| 2095 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2096 | for (i = 0; i < count; i++) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2097 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2098 | size = le32_to_cpu(pcm->size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2099 | |
| 2100 | /* check ABI version by size, create a new version of pcm |
| 2101 | * if abi not match. |
| 2102 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2103 | if (size == sizeof(*pcm)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2104 | abi_match = true; |
| 2105 | _pcm = pcm; |
| 2106 | } else { |
| 2107 | abi_match = false; |
Amadeusz Sławiński | b3677fc3 | 2020-03-27 16:47:28 -0400 | [diff] [blame] | 2108 | ret = pcm_new_ver(tplg, pcm, &_pcm); |
| 2109 | if (ret < 0) |
| 2110 | return ret; |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2111 | } |
| 2112 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2113 | /* create the FE DAIs and DAI links */ |
Dragos Tarcatu | a3039ae | 2019-12-09 18:39:39 -0600 | [diff] [blame] | 2114 | ret = soc_tplg_pcm_create(tplg, _pcm); |
| 2115 | if (ret < 0) { |
| 2116 | if (!abi_match) |
| 2117 | kfree(_pcm); |
| 2118 | return ret; |
| 2119 | } |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2120 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2121 | /* offset by version-specific struct size and |
| 2122 | * real priv data size |
| 2123 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2124 | tplg->pos += size + le32_to_cpu(_pcm->priv.size); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2125 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2126 | if (!abi_match) |
| 2127 | kfree(_pcm); /* free the duplicated one */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2130 | dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2131 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2132 | return 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2135 | /** |
| 2136 | * set_link_hw_format - Set the HW audio format of the physical DAI link. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2137 | * @link: &snd_soc_dai_link which should be updated |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2138 | * @cfg: physical link configs. |
| 2139 | * |
| 2140 | * Topology context contains a list of supported HW formats (configs) and |
| 2141 | * a default format ID for the physical link. This function will use this |
| 2142 | * default ID to choose the HW format to set the link's DAI format for init. |
| 2143 | */ |
| 2144 | static void set_link_hw_format(struct snd_soc_dai_link *link, |
| 2145 | struct snd_soc_tplg_link_config *cfg) |
| 2146 | { |
| 2147 | struct snd_soc_tplg_hw_config *hw_config; |
| 2148 | unsigned char bclk_master, fsync_master; |
| 2149 | unsigned char invert_bclk, invert_fsync; |
| 2150 | int i; |
| 2151 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2152 | for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2153 | hw_config = &cfg->hw_config[i]; |
| 2154 | if (hw_config->id != cfg->default_hw_config_id) |
| 2155 | continue; |
| 2156 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2157 | link->dai_fmt = le32_to_cpu(hw_config->fmt) & |
| 2158 | SND_SOC_DAIFMT_FORMAT_MASK; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2159 | |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2160 | /* clock gating */ |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2161 | switch (hw_config->clock_gated) { |
| 2162 | case SND_SOC_TPLG_DAI_CLK_GATE_GATED: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2163 | link->dai_fmt |= SND_SOC_DAIFMT_GATED; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2164 | break; |
| 2165 | |
| 2166 | case SND_SOC_TPLG_DAI_CLK_GATE_CONT: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2167 | link->dai_fmt |= SND_SOC_DAIFMT_CONT; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2168 | break; |
| 2169 | |
| 2170 | default: |
| 2171 | /* ignore the value */ |
| 2172 | break; |
| 2173 | } |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2174 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2175 | /* clock signal polarity */ |
| 2176 | invert_bclk = hw_config->invert_bclk; |
| 2177 | invert_fsync = hw_config->invert_fsync; |
| 2178 | if (!invert_bclk && !invert_fsync) |
| 2179 | link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; |
| 2180 | else if (!invert_bclk && invert_fsync) |
| 2181 | link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; |
| 2182 | else if (invert_bclk && !invert_fsync) |
| 2183 | link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; |
| 2184 | else |
| 2185 | link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; |
| 2186 | |
| 2187 | /* clock masters */ |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2188 | bclk_master = (hw_config->bclk_master == |
| 2189 | SND_SOC_TPLG_BCLK_CM); |
| 2190 | fsync_master = (hw_config->fsync_master == |
| 2191 | SND_SOC_TPLG_FSYNC_CM); |
| 2192 | if (bclk_master && fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2193 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2194 | else if (!bclk_master && fsync_master) |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2195 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 2196 | else if (bclk_master && !fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2197 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 2198 | else |
| 2199 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | /** |
| 2204 | * link_new_ver - Create a new physical link config from the old |
| 2205 | * version of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2206 | * @tplg: topology context |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2207 | * @src: old version of phyical link config as a source |
| 2208 | * @link: latest version of physical link config created from the source |
| 2209 | * |
| 2210 | * Support from vesion 4. User need free the returned link config manually. |
| 2211 | */ |
| 2212 | static int link_new_ver(struct soc_tplg *tplg, |
| 2213 | struct snd_soc_tplg_link_config *src, |
| 2214 | struct snd_soc_tplg_link_config **link) |
| 2215 | { |
| 2216 | struct snd_soc_tplg_link_config *dest; |
| 2217 | struct snd_soc_tplg_link_config_v4 *src_v4; |
| 2218 | int i; |
| 2219 | |
| 2220 | *link = NULL; |
| 2221 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2222 | if (le32_to_cpu(src->size) != |
| 2223 | sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2224 | dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); |
| 2225 | return -EINVAL; |
| 2226 | } |
| 2227 | |
| 2228 | dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); |
| 2229 | |
| 2230 | src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; |
| 2231 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 2232 | if (!dest) |
| 2233 | return -ENOMEM; |
| 2234 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2235 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2236 | dest->id = src_v4->id; |
| 2237 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2238 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2239 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 2240 | sizeof(struct snd_soc_tplg_stream)); |
| 2241 | |
| 2242 | *link = dest; |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
Kuninori Morimoto | d6f31e0 | 2019-12-10 09:34:14 +0900 | [diff] [blame] | 2246 | /** |
| 2247 | * snd_soc_find_dai_link - Find a DAI link |
| 2248 | * |
| 2249 | * @card: soc card |
| 2250 | * @id: DAI link ID to match |
| 2251 | * @name: DAI link name to match, optional |
| 2252 | * @stream_name: DAI link stream name to match, optional |
| 2253 | * |
| 2254 | * This function will search all existing DAI links of the soc card to |
| 2255 | * find the link of the same ID. Since DAI links may not have their |
| 2256 | * unique ID, so name and stream name should also match if being |
| 2257 | * specified. |
| 2258 | * |
| 2259 | * Return: pointer of DAI link, or NULL if not found. |
| 2260 | */ |
| 2261 | static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, |
| 2262 | int id, const char *name, |
| 2263 | const char *stream_name) |
| 2264 | { |
| 2265 | struct snd_soc_pcm_runtime *rtd; |
| 2266 | struct snd_soc_dai_link *link; |
| 2267 | |
| 2268 | for_each_card_rtds(card, rtd) { |
| 2269 | link = rtd->dai_link; |
| 2270 | |
| 2271 | if (link->id != id) |
| 2272 | continue; |
| 2273 | |
| 2274 | if (name && (!link->name || strcmp(name, link->name))) |
| 2275 | continue; |
| 2276 | |
| 2277 | if (stream_name && (!link->stream_name |
| 2278 | || strcmp(stream_name, link->stream_name))) |
| 2279 | continue; |
| 2280 | |
| 2281 | return link; |
| 2282 | } |
| 2283 | |
| 2284 | return NULL; |
| 2285 | } |
| 2286 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2287 | /* Find and configure an existing physical DAI link */ |
| 2288 | static int soc_tplg_link_config(struct soc_tplg *tplg, |
| 2289 | struct snd_soc_tplg_link_config *cfg) |
| 2290 | { |
| 2291 | struct snd_soc_dai_link *link; |
| 2292 | const char *name, *stream_name; |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2293 | size_t len; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2294 | int ret; |
| 2295 | |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2296 | len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2297 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2298 | return -EINVAL; |
| 2299 | else if (len) |
| 2300 | name = cfg->name; |
| 2301 | else |
| 2302 | name = NULL; |
| 2303 | |
| 2304 | len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2305 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2306 | return -EINVAL; |
| 2307 | else if (len) |
| 2308 | stream_name = cfg->stream_name; |
| 2309 | else |
| 2310 | stream_name = NULL; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2311 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2312 | link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2313 | name, stream_name); |
| 2314 | if (!link) { |
| 2315 | dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", |
| 2316 | name, cfg->id); |
| 2317 | return -EINVAL; |
| 2318 | } |
| 2319 | |
| 2320 | /* hw format */ |
| 2321 | if (cfg->num_hw_configs) |
| 2322 | set_link_hw_format(link, cfg); |
| 2323 | |
| 2324 | /* flags */ |
| 2325 | if (cfg->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2326 | set_link_flags(link, |
| 2327 | le32_to_cpu(cfg->flag_mask), |
| 2328 | le32_to_cpu(cfg->flags)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2329 | |
| 2330 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2331 | ret = soc_tplg_dai_link_load(tplg, link, cfg); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2332 | if (ret < 0) { |
| 2333 | dev_err(tplg->dev, "ASoC: physical link loading failed\n"); |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2337 | /* for unloading it in snd_soc_tplg_component_remove */ |
| 2338 | link->dobj.index = tplg->index; |
| 2339 | link->dobj.ops = tplg->ops; |
| 2340 | link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; |
| 2341 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 2342 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2343 | return 0; |
| 2344 | } |
| 2345 | |
| 2346 | |
| 2347 | /* Load physical link config elements from the topology context */ |
| 2348 | static int soc_tplg_link_elems_load(struct soc_tplg *tplg, |
| 2349 | struct snd_soc_tplg_hdr *hdr) |
| 2350 | { |
| 2351 | struct snd_soc_tplg_link_config *link, *_link; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2352 | int count; |
| 2353 | int size; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2354 | int i, ret; |
| 2355 | bool abi_match; |
| 2356 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2357 | count = le32_to_cpu(hdr->count); |
| 2358 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2359 | /* check the element size and count */ |
| 2360 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2361 | size = le32_to_cpu(link->size); |
| 2362 | if (size > sizeof(struct snd_soc_tplg_link_config) |
| 2363 | || size < sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2364 | dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2365 | size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2366 | return -EINVAL; |
| 2367 | } |
| 2368 | |
| 2369 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2370 | size, count, |
| 2371 | le32_to_cpu(hdr->payload_size), |
| 2372 | "physical link config")) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2373 | dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n", |
| 2374 | count); |
| 2375 | return -EINVAL; |
| 2376 | } |
| 2377 | |
| 2378 | /* config physical DAI links */ |
| 2379 | for (i = 0; i < count; i++) { |
| 2380 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2381 | size = le32_to_cpu(link->size); |
| 2382 | if (size == sizeof(*link)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2383 | abi_match = true; |
| 2384 | _link = link; |
| 2385 | } else { |
| 2386 | abi_match = false; |
| 2387 | ret = link_new_ver(tplg, link, &_link); |
| 2388 | if (ret < 0) |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | ret = soc_tplg_link_config(tplg, _link); |
Dragos Tarcatu | 2b2d5c4 | 2020-02-07 20:53:24 +0200 | [diff] [blame] | 2393 | if (ret < 0) { |
| 2394 | if (!abi_match) |
| 2395 | kfree(_link); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2396 | return ret; |
Dragos Tarcatu | 2b2d5c4 | 2020-02-07 20:53:24 +0200 | [diff] [blame] | 2397 | } |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2398 | |
| 2399 | /* offset by version-specific struct size and |
| 2400 | * real priv data size |
| 2401 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2402 | tplg->pos += size + le32_to_cpu(_link->priv.size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2403 | |
| 2404 | if (!abi_match) |
| 2405 | kfree(_link); /* free the duplicated one */ |
| 2406 | } |
| 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2411 | /** |
| 2412 | * soc_tplg_dai_config - Find and configure an existing physical DAI. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2413 | * @tplg: topology context |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2414 | * @d: physical DAI configs. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2415 | * |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2416 | * The physical dai should already be registered by the platform driver. |
| 2417 | * The platform driver should specify the DAI name and ID for matching. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2418 | */ |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2419 | static int soc_tplg_dai_config(struct soc_tplg *tplg, |
| 2420 | struct snd_soc_tplg_dai *d) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2421 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2422 | struct snd_soc_dai_link_component dai_component; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2423 | struct snd_soc_dai *dai; |
| 2424 | struct snd_soc_dai_driver *dai_drv; |
| 2425 | struct snd_soc_pcm_stream *stream; |
| 2426 | struct snd_soc_tplg_stream_caps *caps; |
| 2427 | int ret; |
| 2428 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2429 | memset(&dai_component, 0, sizeof(dai_component)); |
| 2430 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2431 | dai_component.dai_name = d->dai_name; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2432 | dai = snd_soc_find_dai(&dai_component); |
| 2433 | if (!dai) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2434 | dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", |
| 2435 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2436 | return -EINVAL; |
| 2437 | } |
| 2438 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2439 | if (le32_to_cpu(d->dai_id) != dai->id) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2440 | dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", |
| 2441 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2442 | return -EINVAL; |
| 2443 | } |
| 2444 | |
| 2445 | dai_drv = dai->driver; |
| 2446 | if (!dai_drv) |
| 2447 | return -EINVAL; |
| 2448 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2449 | if (d->playback) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2450 | stream = &dai_drv->playback; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2451 | caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2452 | ret = set_stream_info(stream, caps); |
| 2453 | if (ret < 0) |
| 2454 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2455 | } |
| 2456 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2457 | if (d->capture) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2458 | stream = &dai_drv->capture; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2459 | caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2460 | ret = set_stream_info(stream, caps); |
| 2461 | if (ret < 0) |
| 2462 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2463 | } |
| 2464 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2465 | if (d->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2466 | set_dai_flags(dai_drv, |
| 2467 | le32_to_cpu(d->flag_mask), |
| 2468 | le32_to_cpu(d->flags)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2469 | |
| 2470 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2471 | ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2472 | if (ret < 0) { |
| 2473 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2474 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | return 0; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2478 | |
| 2479 | err: |
| 2480 | kfree(dai_drv->playback.stream_name); |
| 2481 | kfree(dai_drv->capture.stream_name); |
| 2482 | return ret; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2483 | } |
| 2484 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2485 | /* load physical DAI elements */ |
| 2486 | static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, |
| 2487 | struct snd_soc_tplg_hdr *hdr) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2488 | { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2489 | struct snd_soc_tplg_dai *dai; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2490 | int count; |
Amadeusz Sławiński | dd8e871 | 2020-03-27 16:47:29 -0400 | [diff] [blame] | 2491 | int i, ret; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2492 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2493 | count = le32_to_cpu(hdr->count); |
| 2494 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2495 | /* config the existing BE DAIs */ |
| 2496 | for (i = 0; i < count; i++) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2497 | dai = (struct snd_soc_tplg_dai *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2498 | if (le32_to_cpu(dai->size) != sizeof(*dai)) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2499 | dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2500 | return -EINVAL; |
| 2501 | } |
| 2502 | |
Amadeusz Sławiński | dd8e871 | 2020-03-27 16:47:29 -0400 | [diff] [blame] | 2503 | ret = soc_tplg_dai_config(tplg, dai); |
| 2504 | if (ret < 0) { |
| 2505 | dev_err(tplg->dev, "ASoC: failed to configure DAI\n"); |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2509 | tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2516 | /** |
| 2517 | * manifest_new_ver - Create a new version of manifest from the old version |
| 2518 | * of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2519 | * @tplg: topology context |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2520 | * @src: old version of manifest as a source |
| 2521 | * @manifest: latest version of manifest created from the source |
| 2522 | * |
| 2523 | * Support from vesion 4. Users need free the returned manifest manually. |
| 2524 | */ |
| 2525 | static int manifest_new_ver(struct soc_tplg *tplg, |
| 2526 | struct snd_soc_tplg_manifest *src, |
| 2527 | struct snd_soc_tplg_manifest **manifest) |
| 2528 | { |
| 2529 | struct snd_soc_tplg_manifest *dest; |
| 2530 | struct snd_soc_tplg_manifest_v4 *src_v4; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2531 | int size; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2532 | |
| 2533 | *manifest = NULL; |
| 2534 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2535 | size = le32_to_cpu(src->size); |
| 2536 | if (size != sizeof(*src_v4)) { |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2537 | dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2538 | size); |
| 2539 | if (size) |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2540 | return -EINVAL; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2541 | src->size = cpu_to_le32(sizeof(*src_v4)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | dev_warn(tplg->dev, "ASoC: old version of manifest\n"); |
| 2545 | |
| 2546 | src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2547 | dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size), |
| 2548 | GFP_KERNEL); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2549 | if (!dest) |
| 2550 | return -ENOMEM; |
| 2551 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2552 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2553 | dest->control_elems = src_v4->control_elems; |
| 2554 | dest->widget_elems = src_v4->widget_elems; |
| 2555 | dest->graph_elems = src_v4->graph_elems; |
| 2556 | dest->pcm_elems = src_v4->pcm_elems; |
| 2557 | dest->dai_link_elems = src_v4->dai_link_elems; |
| 2558 | dest->priv.size = src_v4->priv.size; |
| 2559 | if (dest->priv.size) |
| 2560 | memcpy(dest->priv.data, src_v4->priv.data, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2561 | le32_to_cpu(src_v4->priv.size)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2562 | |
| 2563 | *manifest = dest; |
| 2564 | return 0; |
| 2565 | } |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2566 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2567 | static int soc_tplg_manifest_load(struct soc_tplg *tplg, |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2568 | struct snd_soc_tplg_hdr *hdr) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2569 | { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2570 | struct snd_soc_tplg_manifest *manifest, *_manifest; |
| 2571 | bool abi_match; |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2572 | int ret = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2573 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2574 | manifest = (struct snd_soc_tplg_manifest *)tplg->pos; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2575 | |
| 2576 | /* check ABI version by size, create a new manifest if abi not match */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2577 | if (le32_to_cpu(manifest->size) == sizeof(*manifest)) { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2578 | abi_match = true; |
| 2579 | _manifest = manifest; |
| 2580 | } else { |
| 2581 | abi_match = false; |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2582 | ret = manifest_new_ver(tplg, manifest, &_manifest); |
| 2583 | if (ret < 0) |
| 2584 | return ret; |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2585 | } |
| 2586 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2587 | /* pass control to component driver for optional further init */ |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 2588 | if (tplg->ops && tplg->ops->manifest) |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2589 | ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2590 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2591 | if (!abi_match) /* free the duplicated one */ |
| 2592 | kfree(_manifest); |
| 2593 | |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2594 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /* validate header magic, size and type */ |
| 2598 | static int soc_valid_header(struct soc_tplg *tplg, |
| 2599 | struct snd_soc_tplg_hdr *hdr) |
| 2600 | { |
| 2601 | if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) |
| 2602 | return 0; |
| 2603 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2604 | if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2605 | dev_err(tplg->dev, |
| 2606 | "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2607 | le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2608 | tplg->fw->size); |
| 2609 | return -EINVAL; |
| 2610 | } |
| 2611 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2612 | /* big endian firmware objects not supported atm */ |
Amadeusz Sławiński | 26d8788 | 2020-04-15 12:24:35 -0400 | [diff] [blame] | 2613 | if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2614 | dev_err(tplg->dev, |
| 2615 | "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", |
| 2616 | tplg->pass, hdr->magic, |
| 2617 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2618 | return -EINVAL; |
| 2619 | } |
| 2620 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2621 | if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2622 | dev_err(tplg->dev, |
| 2623 | "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", |
| 2624 | tplg->pass, hdr->magic, |
| 2625 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2626 | return -EINVAL; |
| 2627 | } |
| 2628 | |
Mengdong Lin | 288b8da | 2016-11-03 01:03:17 +0800 | [diff] [blame] | 2629 | /* Support ABI from version 4 */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2630 | if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || |
| 2631 | le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2632 | dev_err(tplg->dev, |
| 2633 | "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", |
| 2634 | tplg->pass, hdr->abi, |
| 2635 | SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), |
| 2636 | tplg->fw->size); |
| 2637 | return -EINVAL; |
| 2638 | } |
| 2639 | |
| 2640 | if (hdr->payload_size == 0) { |
| 2641 | dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", |
| 2642 | soc_tplg_get_hdr_offset(tplg)); |
| 2643 | return -EINVAL; |
| 2644 | } |
| 2645 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2646 | return 1; |
| 2647 | } |
| 2648 | |
| 2649 | /* check header type and call appropriate handler */ |
| 2650 | static int soc_tplg_load_header(struct soc_tplg *tplg, |
| 2651 | struct snd_soc_tplg_hdr *hdr) |
| 2652 | { |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2653 | int (*elem_load)(struct soc_tplg *tplg, |
| 2654 | struct snd_soc_tplg_hdr *hdr); |
| 2655 | unsigned int hdr_pass; |
| 2656 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2657 | tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); |
| 2658 | |
| 2659 | /* check for matching ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2660 | if (le32_to_cpu(hdr->index) != tplg->req_index && |
Liam Girdwood | bb97142 | 2017-06-29 14:22:25 +0100 | [diff] [blame] | 2661 | tplg->req_index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2662 | return 0; |
| 2663 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2664 | tplg->index = le32_to_cpu(hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2665 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2666 | switch (le32_to_cpu(hdr->type)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2667 | case SND_SOC_TPLG_TYPE_MIXER: |
| 2668 | case SND_SOC_TPLG_TYPE_ENUM: |
| 2669 | case SND_SOC_TPLG_TYPE_BYTES: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2670 | hdr_pass = SOC_TPLG_PASS_MIXER; |
| 2671 | elem_load = soc_tplg_kcontrol_elems_load; |
| 2672 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2673 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2674 | hdr_pass = SOC_TPLG_PASS_GRAPH; |
| 2675 | elem_load = soc_tplg_dapm_graph_elems_load; |
| 2676 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2677 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2678 | hdr_pass = SOC_TPLG_PASS_WIDGET; |
| 2679 | elem_load = soc_tplg_dapm_widget_elems_load; |
| 2680 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2681 | case SND_SOC_TPLG_TYPE_PCM: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2682 | hdr_pass = SOC_TPLG_PASS_PCM_DAI; |
| 2683 | elem_load = soc_tplg_pcm_elems_load; |
| 2684 | break; |
Mengdong Lin | 3fbf793 | 2016-11-03 01:05:01 +0800 | [diff] [blame] | 2685 | case SND_SOC_TPLG_TYPE_DAI: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2686 | hdr_pass = SOC_TPLG_PASS_BE_DAI; |
| 2687 | elem_load = soc_tplg_dai_elems_load; |
| 2688 | break; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2689 | case SND_SOC_TPLG_TYPE_DAI_LINK: |
| 2690 | case SND_SOC_TPLG_TYPE_BACKEND_LINK: |
| 2691 | /* physical link configurations */ |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2692 | hdr_pass = SOC_TPLG_PASS_LINK; |
| 2693 | elem_load = soc_tplg_link_elems_load; |
| 2694 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2695 | case SND_SOC_TPLG_TYPE_MANIFEST: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2696 | hdr_pass = SOC_TPLG_PASS_MANIFEST; |
| 2697 | elem_load = soc_tplg_manifest_load; |
| 2698 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2699 | default: |
| 2700 | /* bespoke vendor data object */ |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2701 | hdr_pass = SOC_TPLG_PASS_VENDOR; |
| 2702 | elem_load = soc_tplg_vendor_load; |
| 2703 | break; |
| 2704 | } |
| 2705 | |
| 2706 | if (tplg->pass == hdr_pass) { |
| 2707 | dev_dbg(tplg->dev, |
| 2708 | "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", |
| 2709 | hdr->payload_size, hdr->type, hdr->version, |
| 2710 | hdr->vendor_type, tplg->pass); |
| 2711 | return elem_load(tplg, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | return 0; |
| 2715 | } |
| 2716 | |
| 2717 | /* process the topology file headers */ |
| 2718 | static int soc_tplg_process_headers(struct soc_tplg *tplg) |
| 2719 | { |
| 2720 | struct snd_soc_tplg_hdr *hdr; |
| 2721 | int ret; |
| 2722 | |
| 2723 | tplg->pass = SOC_TPLG_PASS_START; |
| 2724 | |
| 2725 | /* process the header types from start to end */ |
| 2726 | while (tplg->pass <= SOC_TPLG_PASS_END) { |
| 2727 | |
| 2728 | tplg->hdr_pos = tplg->fw->data; |
| 2729 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2730 | |
| 2731 | while (!soc_tplg_is_eof(tplg)) { |
| 2732 | |
| 2733 | /* make sure header is valid before loading */ |
| 2734 | ret = soc_valid_header(tplg, hdr); |
| 2735 | if (ret < 0) |
| 2736 | return ret; |
| 2737 | else if (ret == 0) |
| 2738 | break; |
| 2739 | |
| 2740 | /* load the header object */ |
| 2741 | ret = soc_tplg_load_header(tplg, hdr); |
| 2742 | if (ret < 0) |
| 2743 | return ret; |
| 2744 | |
| 2745 | /* goto next header */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2746 | tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2747 | sizeof(struct snd_soc_tplg_hdr); |
| 2748 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2749 | } |
| 2750 | |
| 2751 | /* next data type pass */ |
| 2752 | tplg->pass++; |
| 2753 | } |
| 2754 | |
| 2755 | /* signal DAPM we are complete */ |
| 2756 | ret = soc_tplg_dapm_complete(tplg); |
| 2757 | if (ret < 0) |
| 2758 | dev_err(tplg->dev, |
| 2759 | "ASoC: failed to initialise DAPM from Firmware\n"); |
| 2760 | |
| 2761 | return ret; |
| 2762 | } |
| 2763 | |
| 2764 | static int soc_tplg_load(struct soc_tplg *tplg) |
| 2765 | { |
| 2766 | int ret; |
| 2767 | |
| 2768 | ret = soc_tplg_process_headers(tplg); |
| 2769 | if (ret == 0) |
| 2770 | soc_tplg_complete(tplg); |
| 2771 | |
| 2772 | return ret; |
| 2773 | } |
| 2774 | |
| 2775 | /* load audio component topology from "firmware" file */ |
| 2776 | int snd_soc_tplg_component_load(struct snd_soc_component *comp, |
| 2777 | struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) |
| 2778 | { |
| 2779 | struct soc_tplg tplg; |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2780 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2781 | |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 2782 | /* component needs to exist to keep and reference data while parsing */ |
| 2783 | if (!comp) |
| 2784 | return -EINVAL; |
| 2785 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2786 | /* setup parsing context */ |
| 2787 | memset(&tplg, 0, sizeof(tplg)); |
| 2788 | tplg.fw = fw; |
| 2789 | tplg.dev = comp->dev; |
| 2790 | tplg.comp = comp; |
| 2791 | tplg.ops = ops; |
| 2792 | tplg.req_index = id; |
| 2793 | tplg.io_ops = ops->io_ops; |
| 2794 | tplg.io_ops_count = ops->io_ops_count; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 2795 | tplg.bytes_ext_ops = ops->bytes_ext_ops; |
| 2796 | tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2797 | |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2798 | ret = soc_tplg_load(&tplg); |
| 2799 | /* free the created components if fail to load topology */ |
| 2800 | if (ret) |
| 2801 | snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL); |
| 2802 | |
| 2803 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2804 | } |
| 2805 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); |
| 2806 | |
| 2807 | /* remove this dynamic widget */ |
| 2808 | void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) |
| 2809 | { |
| 2810 | /* make sure we are a widget */ |
| 2811 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET) |
| 2812 | return; |
| 2813 | |
| 2814 | remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); |
| 2815 | } |
| 2816 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); |
| 2817 | |
| 2818 | /* remove all dynamic widgets from this DAPM context */ |
| 2819 | void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, |
| 2820 | u32 index) |
| 2821 | { |
| 2822 | struct snd_soc_dapm_widget *w, *next_w; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2823 | |
Kuninori Morimoto | 1459669 | 2020-03-09 13:08:21 +0900 | [diff] [blame] | 2824 | for_each_card_widgets_safe(dapm->card, w, next_w) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2825 | |
| 2826 | /* make sure we are a widget with correct context */ |
| 2827 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) |
| 2828 | continue; |
| 2829 | |
| 2830 | /* match ID */ |
| 2831 | if (w->dobj.index != index && |
| 2832 | w->dobj.index != SND_SOC_TPLG_INDEX_ALL) |
| 2833 | continue; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2834 | /* check and free and dynamic widget kcontrols */ |
| 2835 | snd_soc_tplg_widget_remove(w); |
Lars-Peter Clausen | b97e269 | 2015-07-21 18:11:07 +0200 | [diff] [blame] | 2836 | snd_soc_dapm_free_widget(w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2837 | } |
Jyri Sarha | fd589a1 | 2015-11-10 18:12:42 +0200 | [diff] [blame] | 2838 | snd_soc_dapm_reset_cache(dapm); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2839 | } |
| 2840 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); |
| 2841 | |
| 2842 | /* remove dynamic controls from the component driver */ |
| 2843 | int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) |
| 2844 | { |
| 2845 | struct snd_soc_dobj *dobj, *next_dobj; |
| 2846 | int pass = SOC_TPLG_PASS_END; |
| 2847 | |
| 2848 | /* process the header types from end to start */ |
| 2849 | while (pass >= SOC_TPLG_PASS_START) { |
| 2850 | |
| 2851 | /* remove mixer controls */ |
| 2852 | list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, |
| 2853 | list) { |
| 2854 | |
| 2855 | /* match index */ |
| 2856 | if (dobj->index != index && |
Yan Wang | feb12f0 | 2018-03-26 16:48:00 +0100 | [diff] [blame] | 2857 | index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2858 | continue; |
| 2859 | |
| 2860 | switch (dobj->type) { |
| 2861 | case SND_SOC_DOBJ_MIXER: |
| 2862 | remove_mixer(comp, dobj, pass); |
| 2863 | break; |
| 2864 | case SND_SOC_DOBJ_ENUM: |
| 2865 | remove_enum(comp, dobj, pass); |
| 2866 | break; |
| 2867 | case SND_SOC_DOBJ_BYTES: |
| 2868 | remove_bytes(comp, dobj, pass); |
| 2869 | break; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 2870 | case SND_SOC_DOBJ_GRAPH: |
| 2871 | remove_route(comp, dobj, pass); |
| 2872 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2873 | case SND_SOC_DOBJ_WIDGET: |
| 2874 | remove_widget(comp, dobj, pass); |
| 2875 | break; |
| 2876 | case SND_SOC_DOBJ_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2877 | remove_dai(comp, dobj, pass); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2878 | break; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 2879 | case SND_SOC_DOBJ_DAI_LINK: |
| 2880 | remove_link(comp, dobj, pass); |
| 2881 | break; |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2882 | case SND_SOC_DOBJ_BACKEND_LINK: |
| 2883 | /* |
| 2884 | * call link_unload ops if extra |
| 2885 | * deinitialization is needed. |
| 2886 | */ |
| 2887 | remove_backend_link(comp, dobj, pass); |
| 2888 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2889 | default: |
| 2890 | dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", |
| 2891 | dobj->type); |
| 2892 | break; |
| 2893 | } |
| 2894 | } |
| 2895 | pass--; |
| 2896 | } |
| 2897 | |
| 2898 | /* let caller know if FW can be freed when no objects are left */ |
| 2899 | return !list_empty(&comp->dobj_list); |
| 2900 | } |
| 2901 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); |