1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-topology.c -- ALSA SoC Topology
5 // Copyright (C) 2012 Texas Instruments Inc.
6 // Copyright (C) 2015 Intel Corporation.
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>
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.
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.
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>
31 #include <sound/tlv.h>
33 #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
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).
41 #define SOC_TPLG_PASS_MANIFEST 0
42 #define SOC_TPLG_PASS_VENDOR 1
43 #define SOC_TPLG_PASS_CONTROL 2
44 #define SOC_TPLG_PASS_WIDGET 3
45 #define SOC_TPLG_PASS_PCM_DAI 4
46 #define SOC_TPLG_PASS_GRAPH 5
47 #define SOC_TPLG_PASS_BE_DAI 6
48 #define SOC_TPLG_PASS_LINK 7
50 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
51 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
53 /* topology context */
55 const struct firmware *fw;
57 /* runtime FW parsing */
58 const u8 *pos; /* read position */
59 const u8 *hdr_pos; /* header position */
60 unsigned int pass; /* pass number */
62 /* component caller */
64 struct snd_soc_component *comp;
65 u32 index; /* current block index */
67 /* vendor specific kcontrol operations */
68 const struct snd_soc_tplg_kcontrol_ops *io_ops;
71 /* vendor specific bytes ext handlers, for TLV bytes controls */
72 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
73 int bytes_ext_ops_count;
75 /* optional fw loading callbacks to component drivers */
76 struct snd_soc_tplg_ops *ops;
79 /* check we dont overflow the data for this control chunk */
80 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
81 unsigned int count, size_t bytes, const char *elem_type)
83 const u8 *end = tplg->pos + elem_size * count;
85 if (end > tplg->fw->data + tplg->fw->size) {
86 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
91 /* check there is enough room in chunk for control.
92 extra bytes at the end of control are for vendor data here */
93 if (elem_size * count > bytes) {
95 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
96 elem_type, count, elem_size, bytes);
103 static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
105 const u8 *end = tplg->hdr_pos;
107 if (end >= tplg->fw->data + tplg->fw->size)
112 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
114 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
117 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
119 return (unsigned long)(tplg->pos - tplg->fw->data);
122 /* mapping of Kcontrol types and associated operations. */
123 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
124 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
125 snd_soc_put_volsw, snd_soc_info_volsw},
126 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
127 snd_soc_put_volsw_sx, NULL},
128 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
129 snd_soc_put_enum_double, snd_soc_info_enum_double},
130 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
131 snd_soc_put_enum_double, NULL},
132 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
133 snd_soc_bytes_put, snd_soc_bytes_info},
134 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
135 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
136 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
137 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
138 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
139 snd_soc_put_strobe, NULL},
140 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
141 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
142 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
143 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
144 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
145 snd_soc_dapm_put_enum_double, NULL},
146 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
147 snd_soc_dapm_put_enum_double, NULL},
148 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
149 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
152 struct soc_tplg_map {
157 /* mapping of widget types from UAPI IDs to kernel IDs */
158 static const struct soc_tplg_map dapm_map[] = {
159 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
160 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
161 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
162 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
163 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
164 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
165 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
166 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
167 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
168 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
169 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
170 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
171 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
172 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
173 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
174 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
175 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
176 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
177 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
178 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
179 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
180 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
181 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
182 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
185 static int tplg_chan_get_reg(struct soc_tplg *tplg,
186 struct snd_soc_tplg_channel *chan, int map)
190 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
191 if (le32_to_cpu(chan[i].id) == map)
192 return le32_to_cpu(chan[i].reg);
198 static int tplg_chan_get_shift(struct soc_tplg *tplg,
199 struct snd_soc_tplg_channel *chan, int map)
203 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
204 if (le32_to_cpu(chan[i].id) == map)
205 return le32_to_cpu(chan[i].shift);
211 static int get_widget_id(int tplg_type)
215 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
216 if (tplg_type == dapm_map[i].uid)
217 return dapm_map[i].kid;
223 static inline void soc_bind_err(struct soc_tplg *tplg,
224 struct snd_soc_tplg_ctl_hdr *hdr, int index)
227 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
228 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
229 soc_tplg_get_offset(tplg));
232 static inline void soc_control_err(struct soc_tplg *tplg,
233 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
236 "ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
237 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
238 soc_tplg_get_offset(tplg));
241 /* pass vendor data to component driver for processing */
242 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
243 struct snd_soc_tplg_hdr *hdr)
247 if (tplg->ops && tplg->ops->vendor_load)
248 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
250 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
257 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
258 soc_tplg_get_hdr_offset(tplg),
259 soc_tplg_get_hdr_offset(tplg),
260 hdr->type, hdr->vendor_type);
264 /* optionally pass new dynamic widget to component driver. This is mainly for
265 * external widgets where we can assign private data/ops */
266 static int soc_tplg_widget_load(struct soc_tplg *tplg,
267 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
269 if (tplg->ops && tplg->ops->widget_load)
270 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
276 /* optionally pass new dynamic widget to component driver. This is mainly for
277 * external widgets where we can assign private data/ops */
278 static int soc_tplg_widget_ready(struct soc_tplg *tplg,
279 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
281 if (tplg->ops && tplg->ops->widget_ready)
282 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
288 /* pass DAI configurations to component driver for extra initialization */
289 static int soc_tplg_dai_load(struct soc_tplg *tplg,
290 struct snd_soc_dai_driver *dai_drv,
291 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
293 if (tplg->ops && tplg->ops->dai_load)
294 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
300 /* pass link configurations to component driver for extra initialization */
301 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
302 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
304 if (tplg->ops && tplg->ops->link_load)
305 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
310 /* tell the component driver that all firmware has been loaded in this request */
311 static int soc_tplg_complete(struct soc_tplg *tplg)
313 if (tplg->ops && tplg->ops->complete)
314 return tplg->ops->complete(tplg->comp);
319 /* add a dynamic kcontrol */
320 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
321 const struct snd_kcontrol_new *control_new, const char *prefix,
322 void *data, struct snd_kcontrol **kcontrol)
326 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
327 if (*kcontrol == NULL) {
328 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
333 err = snd_ctl_add(card, *kcontrol);
335 dev_err(dev, "ASoC: Failed to add %s: %d\n",
336 control_new->name, err);
343 /* add a dynamic kcontrol for component driver */
344 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
345 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
347 struct snd_soc_component *comp = tplg->comp;
349 return soc_tplg_add_dcontrol(comp->card->snd_card,
350 tplg->dev, k, comp->name_prefix, comp, kcontrol);
353 /* remove kcontrol */
354 static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
357 struct snd_card *card = comp->card->snd_card;
359 if (pass != SOC_TPLG_PASS_CONTROL)
363 dobj->unload(comp, dobj);
365 snd_ctl_remove(card, dobj->control.kcontrol);
366 list_del(&dobj->list);
370 static void soc_tplg_remove_route(struct snd_soc_component *comp,
371 struct snd_soc_dobj *dobj, int pass)
373 if (pass != SOC_TPLG_PASS_GRAPH)
377 dobj->unload(comp, dobj);
379 list_del(&dobj->list);
382 /* remove a widget and it's kcontrols - routes must be removed first */
383 static void soc_tplg_remove_widget(struct snd_soc_component *comp,
384 struct snd_soc_dobj *dobj, int pass)
386 struct snd_card *card = comp->card->snd_card;
387 struct snd_soc_dapm_widget *w =
388 container_of(dobj, struct snd_soc_dapm_widget, dobj);
391 if (pass != SOC_TPLG_PASS_WIDGET)
395 dobj->unload(comp, dobj);
400 for (i = 0; w->kcontrols && i < w->num_kcontrols; i++)
401 snd_ctl_remove(card, w->kcontrols[i]);
405 list_del(&dobj->list);
407 /* widget w is freed by soc-dapm.c */
410 /* remove DAI configurations */
411 static void soc_tplg_remove_dai(struct snd_soc_component *comp,
412 struct snd_soc_dobj *dobj, int pass)
414 struct snd_soc_dai_driver *dai_drv =
415 container_of(dobj, struct snd_soc_dai_driver, dobj);
416 struct snd_soc_dai *dai, *_dai;
418 if (pass != SOC_TPLG_PASS_PCM_DAI)
422 dobj->unload(comp, dobj);
424 for_each_component_dais_safe(comp, dai, _dai)
425 if (dai->driver == dai_drv)
426 snd_soc_unregister_dai(dai);
428 list_del(&dobj->list);
431 /* remove link configurations */
432 static void soc_tplg_remove_link(struct snd_soc_component *comp,
433 struct snd_soc_dobj *dobj, int pass)
435 struct snd_soc_dai_link *link =
436 container_of(dobj, struct snd_soc_dai_link, dobj);
438 if (pass != SOC_TPLG_PASS_PCM_DAI)
442 dobj->unload(comp, dobj);
444 list_del(&dobj->list);
445 snd_soc_remove_pcm_runtime(comp->card,
446 snd_soc_get_pcm_runtime(comp->card, link));
449 /* unload dai link */
450 static void remove_backend_link(struct snd_soc_component *comp,
451 struct snd_soc_dobj *dobj, int pass)
453 if (pass != SOC_TPLG_PASS_LINK)
457 dobj->unload(comp, dobj);
460 * We don't free the link here as what soc_tplg_remove_link() do since BE
461 * links are not allocated by topology.
462 * We however need to reset the dobj type to its initial values
464 dobj->type = SND_SOC_DOBJ_NONE;
465 list_del(&dobj->list);
468 /* bind a kcontrol to it's IO handlers */
469 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
470 struct snd_kcontrol_new *k,
471 const struct soc_tplg *tplg)
473 const struct snd_soc_tplg_kcontrol_ops *ops;
474 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
477 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
478 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
479 && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
480 || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
481 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
482 struct soc_bytes_ext *sbe;
483 struct snd_soc_tplg_bytes_control *be;
485 sbe = (struct soc_bytes_ext *)k->private_value;
486 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
488 /* TLV bytes controls need standard kcontrol info handler,
489 * TLV callback and extended put/get handlers.
491 k->info = snd_soc_bytes_info_ext;
492 k->tlv.c = snd_soc_bytes_tlv_callback;
495 * When a topology-based implementation abuses the
496 * control interface and uses bytes_ext controls of
497 * more than 512 bytes, we need to disable the size
498 * checks, otherwise accesses to such controls will
499 * return an -EINVAL error and prevent the card from
503 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
505 ext_ops = tplg->bytes_ext_ops;
506 num_ops = tplg->bytes_ext_ops_count;
507 for (i = 0; i < num_ops; i++) {
509 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
510 sbe->put = ext_ops[i].put;
512 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
513 sbe->get = ext_ops[i].get;
516 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
518 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
523 /* try and map vendor specific kcontrol handlers first */
525 num_ops = tplg->io_ops_count;
526 for (i = 0; i < num_ops; i++) {
528 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
530 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
532 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
533 k->info = ops[i].info;
536 /* vendor specific handlers found ? */
537 if (k->put && k->get && k->info)
540 /* none found so try standard kcontrol handlers */
542 num_ops = ARRAY_SIZE(io_ops);
543 for (i = 0; i < num_ops; i++) {
545 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
547 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
549 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
550 k->info = ops[i].info;
553 /* standard handlers found ? */
554 if (k->put && k->get && k->info)
557 /* nothing to bind */
561 /* bind a widgets to it's evnt handlers */
562 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
563 const struct snd_soc_tplg_widget_events *events,
564 int num_events, u16 event_type)
570 for (i = 0; i < num_events; i++) {
571 if (event_type == events[i].type) {
573 /* found - so assign event */
574 w->event = events[i].event_handler;
582 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
584 /* optionally pass new dynamic kcontrol to component driver. */
585 static int soc_tplg_control_load(struct soc_tplg *tplg,
586 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
590 if (tplg->ops && tplg->ops->control_load)
591 ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
594 dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
600 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
601 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
603 unsigned int item_len = 2 * sizeof(unsigned int);
606 p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
610 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
612 p[2] = le32_to_cpu(scale->min);
613 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
614 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
616 kc->tlv.p = (void *)p;
620 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
621 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
623 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
624 u32 access = le32_to_cpu(tc->access);
626 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
629 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
631 switch (le32_to_cpu(tplg_tlv->type)) {
632 case SNDRV_CTL_TLVT_DB_SCALE:
633 return soc_tplg_create_tlv_db_scale(tplg, kc,
636 /* TODO: add support for other TLV types */
638 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
647 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
649 struct snd_soc_tplg_bytes_control *be;
650 struct soc_bytes_ext *sbe;
651 struct snd_kcontrol_new kc;
654 if (soc_tplg_check_elem_count(tplg,
655 sizeof(struct snd_soc_tplg_bytes_control),
656 1, size, "mixer bytes"))
659 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
661 /* validate kcontrol */
662 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
663 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
666 sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
670 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
671 le32_to_cpu(be->priv.size));
674 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
675 be->hdr.name, be->hdr.access);
677 memset(&kc, 0, sizeof(kc));
678 kc.name = be->hdr.name;
679 kc.private_value = (long)sbe;
680 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
681 kc.access = le32_to_cpu(be->hdr.access);
683 sbe->max = le32_to_cpu(be->max);
684 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
686 sbe->dobj.unload = tplg->ops->control_unload;
687 INIT_LIST_HEAD(&sbe->dobj.list);
689 /* map io handlers */
690 ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
692 soc_control_err(tplg, &be->hdr, be->hdr.name);
696 /* pass control to driver for optional further init */
697 ret = soc_tplg_control_load(tplg, &kc, &be->hdr);
701 /* register control here */
702 ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
706 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
712 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
714 struct snd_soc_tplg_mixer_control *mc;
715 struct soc_mixer_control *sm;
716 struct snd_kcontrol_new kc;
719 if (soc_tplg_check_elem_count(tplg,
720 sizeof(struct snd_soc_tplg_mixer_control),
724 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
726 /* validate kcontrol */
727 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
728 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
731 sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
734 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
735 le32_to_cpu(mc->priv.size));
738 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
739 mc->hdr.name, mc->hdr.access);
741 memset(&kc, 0, sizeof(kc));
742 kc.name = mc->hdr.name;
743 kc.private_value = (long)sm;
744 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
745 kc.access = le32_to_cpu(mc->hdr.access);
747 /* we only support FL/FR channel mapping atm */
748 sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
749 sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
750 sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
751 sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
753 sm->max = le32_to_cpu(mc->max);
754 sm->min = le32_to_cpu(mc->min);
755 sm->invert = le32_to_cpu(mc->invert);
756 sm->platform_max = le32_to_cpu(mc->platform_max);
757 sm->dobj.index = tplg->index;
758 sm->dobj.type = SND_SOC_DOBJ_MIXER;
760 sm->dobj.unload = tplg->ops->control_unload;
761 INIT_LIST_HEAD(&sm->dobj.list);
763 /* map io handlers */
764 ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
766 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
770 /* create any TLV data */
771 ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
773 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
777 /* pass control to driver for optional further init */
778 ret = soc_tplg_control_load(tplg, &kc, &mc->hdr);
782 /* register control here */
783 ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
787 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
793 static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
794 struct snd_soc_tplg_enum_control *ec)
798 if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
801 se->dobj.control.dtexts =
802 devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
803 if (se->dobj.control.dtexts == NULL)
806 for (i = 0; i < le32_to_cpu(ec->items); i++) {
808 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
809 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
814 se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
815 if (!se->dobj.control.dtexts[i]) {
821 se->items = le32_to_cpu(ec->items);
822 se->texts = (const char * const *)se->dobj.control.dtexts;
829 static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
830 struct snd_soc_tplg_enum_control *ec)
835 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
836 * values instead of using ARRAY_SIZE(ec->values) due to the fact that
837 * it is oversized for its purpose. Additionally it is done so because
838 * it is defined in UAPI header where it can't be easily changed.
840 if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
843 se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
844 sizeof(*se->dobj.control.dvalues),
846 if (!se->dobj.control.dvalues)
849 /* convert from little-endian */
850 for (i = 0; i < le32_to_cpu(ec->items); i++) {
851 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
857 static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
859 struct snd_soc_tplg_enum_control *ec;
861 struct snd_kcontrol_new kc;
864 if (soc_tplg_check_elem_count(tplg,
865 sizeof(struct snd_soc_tplg_enum_control),
869 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
871 /* validate kcontrol */
872 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
873 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
876 se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
880 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
881 le32_to_cpu(ec->priv.size));
883 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
884 ec->hdr.name, ec->items);
886 memset(&kc, 0, sizeof(kc));
887 kc.name = ec->hdr.name;
888 kc.private_value = (long)se;
889 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
890 kc.access = le32_to_cpu(ec->hdr.access);
892 se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
893 se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
895 se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
898 se->mask = le32_to_cpu(ec->mask);
899 se->dobj.index = tplg->index;
900 se->dobj.type = SND_SOC_DOBJ_ENUM;
902 se->dobj.unload = tplg->ops->control_unload;
903 INIT_LIST_HEAD(&se->dobj.list);
905 switch (le32_to_cpu(ec->hdr.ops.info)) {
906 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
907 case SND_SOC_TPLG_CTL_ENUM_VALUE:
908 ret = soc_tplg_denum_create_values(tplg, se, ec);
911 "ASoC: could not create values for %s\n",
916 case SND_SOC_TPLG_CTL_ENUM:
917 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
918 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
919 ret = soc_tplg_denum_create_texts(tplg, se, ec);
922 "ASoC: could not create texts for %s\n",
930 "ASoC: invalid enum control type %d for %s\n",
931 ec->hdr.ops.info, ec->hdr.name);
935 /* map io handlers */
936 ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
938 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
942 /* pass control to driver for optional further init */
943 ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
947 /* register control here */
948 ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
952 list_add(&se->dobj.list, &tplg->comp->dobj_list);
958 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
959 struct snd_soc_tplg_hdr *hdr)
964 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
965 soc_tplg_get_offset(tplg));
967 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
968 struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
970 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
971 dev_err(tplg->dev, "ASoC: invalid control size\n");
975 switch (le32_to_cpu(control_hdr->ops.info)) {
976 case SND_SOC_TPLG_CTL_VOLSW:
977 case SND_SOC_TPLG_CTL_STROBE:
978 case SND_SOC_TPLG_CTL_VOLSW_SX:
979 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
980 case SND_SOC_TPLG_CTL_RANGE:
981 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
982 case SND_SOC_TPLG_DAPM_CTL_PIN:
983 ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
985 case SND_SOC_TPLG_CTL_ENUM:
986 case SND_SOC_TPLG_CTL_ENUM_VALUE:
987 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
988 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
989 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
990 ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
992 case SND_SOC_TPLG_CTL_BYTES:
993 ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
996 soc_bind_err(tplg, control_hdr, i);
1000 dev_err(tplg->dev, "ASoC: invalid control\n");
1009 /* optionally pass new dynamic kcontrol to component driver. */
1010 static int soc_tplg_add_route(struct soc_tplg *tplg,
1011 struct snd_soc_dapm_route *route)
1013 if (tplg->ops && tplg->ops->dapm_route_load)
1014 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1020 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1021 struct snd_soc_tplg_hdr *hdr)
1023 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1024 struct snd_soc_tplg_dapm_graph_elem *elem;
1025 struct snd_soc_dapm_route *route;
1029 count = le32_to_cpu(hdr->count);
1031 if (soc_tplg_check_elem_count(tplg,
1032 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1033 count, le32_to_cpu(hdr->payload_size), "graph"))
1036 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1039 for (i = 0; i < count; i++) {
1040 route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1043 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1044 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1046 /* validate routes */
1047 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1048 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1052 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1053 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1057 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1058 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1063 route->source = elem->source;
1064 route->sink = elem->sink;
1066 /* set to NULL atm for tplg users */
1067 route->connected = NULL;
1068 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1069 route->control = NULL;
1071 route->control = elem->control;
1073 /* add route dobj to dobj_list */
1074 route->dobj.type = SND_SOC_DOBJ_GRAPH;
1076 route->dobj.unload = tplg->ops->dapm_route_unload;
1077 route->dobj.index = tplg->index;
1078 list_add(&route->dobj.list, &tplg->comp->dobj_list);
1080 ret = soc_tplg_add_route(tplg, route);
1082 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
1086 ret = snd_soc_dapm_add_routes(dapm, route, 1);
1088 if (!dapm->card->disable_route_checks) {
1089 dev_err(tplg->dev, "ASoC: dapm_add_routes failed: %d\n", ret);
1093 "ASoC: disable_route_checks set, ignoring dapm_add_routes errors\n");
1100 static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1102 struct soc_mixer_control *sm;
1103 struct snd_soc_tplg_mixer_control *mc;
1106 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1108 /* validate kcontrol */
1109 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1110 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1113 sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
1117 tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) +
1118 le32_to_cpu(mc->priv.size);
1120 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n",
1123 kc->private_value = (long)sm;
1124 kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
1127 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1128 kc->access = le32_to_cpu(mc->hdr.access);
1130 /* we only support FL/FR channel mapping atm */
1131 sm->reg = tplg_chan_get_reg(tplg, mc->channel,
1133 sm->rreg = tplg_chan_get_reg(tplg, mc->channel,
1135 sm->shift = tplg_chan_get_shift(tplg, mc->channel,
1137 sm->rshift = tplg_chan_get_shift(tplg, mc->channel,
1140 sm->max = le32_to_cpu(mc->max);
1141 sm->min = le32_to_cpu(mc->min);
1142 sm->invert = le32_to_cpu(mc->invert);
1143 sm->platform_max = le32_to_cpu(mc->platform_max);
1144 sm->dobj.index = tplg->index;
1145 INIT_LIST_HEAD(&sm->dobj.list);
1147 /* map io handlers */
1148 err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
1150 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1154 /* create any TLV data */
1155 err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
1157 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1162 /* pass control to driver for optional further init */
1163 err = soc_tplg_control_load(tplg, kc, &mc->hdr);
1170 static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1172 struct snd_soc_tplg_enum_control *ec;
1173 struct soc_enum *se;
1176 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1177 /* validate kcontrol */
1178 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1179 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1182 se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
1186 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1187 le32_to_cpu(ec->priv.size));
1189 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1192 kc->private_value = (long)se;
1193 kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
1196 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1197 kc->access = le32_to_cpu(ec->hdr.access);
1199 /* we only support FL/FR channel mapping atm */
1200 se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1201 se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
1203 se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
1206 se->items = le32_to_cpu(ec->items);
1207 se->mask = le32_to_cpu(ec->mask);
1208 se->dobj.index = tplg->index;
1210 switch (le32_to_cpu(ec->hdr.ops.info)) {
1211 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1212 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1213 err = soc_tplg_denum_create_values(tplg, se, ec);
1215 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1220 case SND_SOC_TPLG_CTL_ENUM:
1221 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1222 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1223 err = soc_tplg_denum_create_texts(tplg, se, ec);
1225 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1231 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1232 ec->hdr.ops.info, ec->hdr.name);
1236 /* map io handlers */
1237 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1239 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1243 /* pass control to driver for optional further init */
1244 err = soc_tplg_control_load(tplg, kc, &ec->hdr);
1251 static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1253 struct snd_soc_tplg_bytes_control *be;
1254 struct soc_bytes_ext *sbe;
1257 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1259 /* validate kcontrol */
1260 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1261 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1264 sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
1268 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1269 le32_to_cpu(be->priv.size));
1272 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1273 be->hdr.name, be->hdr.access);
1275 kc->private_value = (long)sbe;
1276 kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
1279 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1280 kc->access = le32_to_cpu(be->hdr.access);
1282 sbe->max = le32_to_cpu(be->max);
1283 INIT_LIST_HEAD(&sbe->dobj.list);
1285 /* map standard io handlers and check for external handlers */
1286 err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
1288 soc_control_err(tplg, &be->hdr, be->hdr.name);
1292 /* pass control to driver for optional further init */
1293 err = soc_tplg_control_load(tplg, kc, &be->hdr);
1300 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1301 struct snd_soc_tplg_dapm_widget *w)
1303 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1304 struct snd_soc_dapm_widget template, *widget;
1305 struct snd_soc_tplg_ctl_hdr *control_hdr;
1306 struct snd_soc_card *card = tplg->comp->card;
1307 unsigned int *kcontrol_type = NULL;
1308 struct snd_kcontrol_new *kc;
1309 int mixer_count = 0;
1310 int bytes_count = 0;
1315 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1316 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1318 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1319 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1322 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1325 memset(&template, 0, sizeof(template));
1327 /* map user to kernel widget ID */
1328 template.id = get_widget_id(le32_to_cpu(w->id));
1329 if ((int)template.id < 0)
1332 /* strings are allocated here, but used and freed by the widget */
1333 template.name = kstrdup(w->name, GFP_KERNEL);
1336 template.sname = kstrdup(w->sname, GFP_KERNEL);
1337 if (!template.sname) {
1341 template.reg = le32_to_cpu(w->reg);
1342 template.shift = le32_to_cpu(w->shift);
1343 template.mask = le32_to_cpu(w->mask);
1344 template.subseq = le32_to_cpu(w->subseq);
1345 template.on_val = w->invert ? 0 : 1;
1346 template.off_val = w->invert ? 1 : 0;
1347 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1348 template.event_flags = le16_to_cpu(w->event_flags);
1349 template.dobj.index = tplg->index;
1352 (sizeof(struct snd_soc_tplg_dapm_widget) +
1353 le32_to_cpu(w->priv.size));
1355 if (w->num_kcontrols == 0) {
1356 template.num_kcontrols = 0;
1360 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1361 kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1367 kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1369 if (!kcontrol_type) {
1374 for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1375 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1376 switch (le32_to_cpu(control_hdr->ops.info)) {
1377 case SND_SOC_TPLG_CTL_VOLSW:
1378 case SND_SOC_TPLG_CTL_STROBE:
1379 case SND_SOC_TPLG_CTL_VOLSW_SX:
1380 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1381 case SND_SOC_TPLG_CTL_RANGE:
1382 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1384 kc[i].index = mixer_count;
1385 kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1387 ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]);
1391 case SND_SOC_TPLG_CTL_ENUM:
1392 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1393 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1394 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1395 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1396 /* enumerated mixer */
1397 kc[i].index = enum_count;
1398 kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1400 ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]);
1404 case SND_SOC_TPLG_CTL_BYTES:
1406 kc[i].index = bytes_count;
1407 kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1409 ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]);
1414 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1415 control_hdr->ops.get, control_hdr->ops.put,
1416 le32_to_cpu(control_hdr->ops.info));
1422 template.kcontrol_news = kc;
1423 dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
1424 w->name, mixer_count, enum_count, bytes_count);
1427 ret = soc_tplg_widget_load(tplg, &template, w);
1431 /* card dapm mutex is held by the core if we are loading topology
1432 * data during sound card init. */
1433 if (snd_soc_card_is_instantiated(card))
1434 widget = snd_soc_dapm_new_control(dapm, &template);
1436 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1437 if (IS_ERR(widget)) {
1438 ret = PTR_ERR(widget);
1442 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1443 widget->dobj.widget.kcontrol_type = kcontrol_type;
1445 widget->dobj.unload = tplg->ops->widget_unload;
1446 widget->dobj.index = tplg->index;
1447 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1449 ret = soc_tplg_widget_ready(tplg, widget, w);
1453 kfree(template.sname);
1454 kfree(template.name);
1459 soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1460 snd_soc_dapm_free_widget(widget);
1462 kfree(template.sname);
1464 kfree(template.name);
1468 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1469 struct snd_soc_tplg_hdr *hdr)
1473 count = le32_to_cpu(hdr->count);
1475 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1477 for (i = 0; i < count; i++) {
1478 struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1482 * check if widget itself fits within topology file
1483 * use sizeof instead of widget->size, as we can't be sure
1484 * it is set properly yet (file may end before it is present)
1486 if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
1487 dev_err(tplg->dev, "ASoC: invalid widget data size\n");
1491 /* check if widget has proper size */
1492 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1493 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1497 /* check if widget private data fits within topology file */
1498 if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
1499 dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
1503 ret = soc_tplg_dapm_widget_create(tplg, widget);
1505 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1514 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1516 struct snd_soc_card *card = tplg->comp->card;
1519 /* Card might not have been registered at this point.
1520 * If so, just return success.
1522 if (!snd_soc_card_is_instantiated(card)) {
1523 dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
1527 ret = snd_soc_dapm_new_widgets(card);
1529 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
1534 static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1535 struct snd_soc_tplg_stream_caps *caps)
1537 stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1538 if (!stream->stream_name)
1541 stream->channels_min = le32_to_cpu(caps->channels_min);
1542 stream->channels_max = le32_to_cpu(caps->channels_max);
1543 stream->rates = le32_to_cpu(caps->rates);
1544 stream->rate_min = le32_to_cpu(caps->rate_min);
1545 stream->rate_max = le32_to_cpu(caps->rate_max);
1546 stream->formats = le64_to_cpu(caps->formats);
1547 stream->sig_bits = le32_to_cpu(caps->sig_bits);
1552 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1553 unsigned int flag_mask, unsigned int flags)
1555 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1556 dai_drv->symmetric_rate =
1557 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1559 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1560 dai_drv->symmetric_channels =
1561 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
1564 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1565 dai_drv->symmetric_sample_bits =
1566 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1570 static const struct snd_soc_dai_ops tplg_dai_ops = {
1571 .compress_new = snd_soc_new_compress,
1574 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1575 struct snd_soc_tplg_pcm *pcm)
1577 struct snd_soc_dai_driver *dai_drv;
1578 struct snd_soc_pcm_stream *stream;
1579 struct snd_soc_tplg_stream_caps *caps;
1580 struct snd_soc_dai *dai;
1581 struct snd_soc_dapm_context *dapm =
1582 snd_soc_component_get_dapm(tplg->comp);
1585 dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1586 if (dai_drv == NULL)
1589 if (strlen(pcm->dai_name)) {
1590 dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1591 if (!dai_drv->name) {
1596 dai_drv->id = le32_to_cpu(pcm->dai_id);
1598 if (pcm->playback) {
1599 stream = &dai_drv->playback;
1600 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1601 ret = set_stream_info(tplg, stream, caps);
1607 stream = &dai_drv->capture;
1608 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1609 ret = set_stream_info(tplg, stream, caps);
1615 dai_drv->ops = &tplg_dai_ops;
1617 /* pass control to component driver for optional further init */
1618 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1620 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1624 dai_drv->dobj.index = tplg->index;
1625 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1627 dai_drv->dobj.unload = tplg->ops->dai_unload;
1628 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1630 /* register the DAI to the component */
1631 dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1635 /* Create the DAI widgets here */
1636 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1638 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1639 snd_soc_unregister_dai(dai);
1649 static void set_link_flags(struct snd_soc_dai_link *link,
1650 unsigned int flag_mask, unsigned int flags)
1652 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1653 link->symmetric_rate =
1654 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1656 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1657 link->symmetric_channels =
1658 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1661 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1662 link->symmetric_sample_bits =
1663 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1666 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1667 link->ignore_suspend =
1668 (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
1672 /* create the FE DAI link */
1673 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1674 struct snd_soc_tplg_pcm *pcm)
1676 struct snd_soc_dai_link *link;
1677 struct snd_soc_dai_link_component *dlc;
1680 /* link + cpu + codec + platform */
1681 link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1685 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1687 link->cpus = &dlc[0];
1690 link->dobj.index = tplg->index;
1691 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1693 link->dobj.unload = tplg->ops->link_unload;
1695 if (strlen(pcm->pcm_name)) {
1696 link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1697 link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1698 if (!link->name || !link->stream_name) {
1703 link->id = le32_to_cpu(pcm->pcm_id);
1705 if (strlen(pcm->dai_name)) {
1706 link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1707 if (!link->cpus->dai_name) {
1714 * Many topology are assuming link has Codec / Platform, and
1715 * these might be overwritten at soc_tplg_dai_link_load().
1716 * Don't use &snd_soc_dummy_dlc here.
1718 link->codecs = &dlc[1]; /* Don't use &snd_soc_dummy_dlc here */
1719 link->codecs->name = "snd-soc-dummy";
1720 link->codecs->dai_name = "snd-soc-dummy-dai";
1721 link->num_codecs = 1;
1723 link->platforms = &dlc[2]; /* Don't use &snd_soc_dummy_dlc here */
1724 link->platforms->name = "snd-soc-dummy";
1725 link->num_platforms = 1;
1729 link->ignore_pmdown_time = 1;
1730 link->dpcm_playback = le32_to_cpu(pcm->playback);
1731 link->dpcm_capture = le32_to_cpu(pcm->capture);
1733 set_link_flags(link,
1734 le32_to_cpu(pcm->flag_mask),
1735 le32_to_cpu(pcm->flags));
1737 /* pass control to component driver for optional further init */
1738 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1740 dev_err(tplg->dev, "ASoC: FE link loading failed\n");
1744 ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
1746 if (ret != -EPROBE_DEFER)
1747 dev_err(tplg->dev, "ASoC: adding FE link failed\n");
1751 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1758 /* create a FE DAI and DAI link from the PCM object */
1759 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1760 struct snd_soc_tplg_pcm *pcm)
1764 ret = soc_tplg_dai_create(tplg, pcm);
1768 return soc_tplg_fe_link_create(tplg, pcm);
1771 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1772 struct snd_soc_tplg_hdr *hdr)
1774 struct snd_soc_tplg_pcm *pcm;
1780 count = le32_to_cpu(hdr->count);
1782 /* check the element size and count */
1783 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1784 size = le32_to_cpu(pcm->size);
1785 if (size > sizeof(struct snd_soc_tplg_pcm)) {
1786 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1791 if (soc_tplg_check_elem_count(tplg,
1793 le32_to_cpu(hdr->payload_size),
1797 for (i = 0; i < count; i++) {
1798 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1799 size = le32_to_cpu(pcm->size);
1801 /* check ABI version by size, create a new version of pcm
1804 if (size != sizeof(*pcm))
1807 /* create the FE DAIs and DAI links */
1808 ret = soc_tplg_pcm_create(tplg, pcm);
1812 /* offset by version-specific struct size and
1813 * real priv data size
1815 tplg->pos += size + le32_to_cpu(pcm->priv.size);
1818 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1824 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1825 * @link: &snd_soc_dai_link which should be updated
1826 * @cfg: physical link configs.
1828 * Topology context contains a list of supported HW formats (configs) and
1829 * a default format ID for the physical link. This function will use this
1830 * default ID to choose the HW format to set the link's DAI format for init.
1832 static void set_link_hw_format(struct snd_soc_dai_link *link,
1833 struct snd_soc_tplg_link_config *cfg)
1835 struct snd_soc_tplg_hw_config *hw_config;
1836 unsigned char bclk_provider, fsync_provider;
1837 unsigned char invert_bclk, invert_fsync;
1840 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1841 hw_config = &cfg->hw_config[i];
1842 if (hw_config->id != cfg->default_hw_config_id)
1845 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
1846 SND_SOC_DAIFMT_FORMAT_MASK;
1849 switch (hw_config->clock_gated) {
1850 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1851 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1854 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1855 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1859 /* ignore the value */
1863 /* clock signal polarity */
1864 invert_bclk = hw_config->invert_bclk;
1865 invert_fsync = hw_config->invert_fsync;
1866 if (!invert_bclk && !invert_fsync)
1867 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1868 else if (!invert_bclk && invert_fsync)
1869 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1870 else if (invert_bclk && !invert_fsync)
1871 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1873 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1876 bclk_provider = (hw_config->bclk_provider ==
1877 SND_SOC_TPLG_BCLK_CP);
1878 fsync_provider = (hw_config->fsync_provider ==
1879 SND_SOC_TPLG_FSYNC_CP);
1880 if (bclk_provider && fsync_provider)
1881 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1882 else if (!bclk_provider && fsync_provider)
1883 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1884 else if (bclk_provider && !fsync_provider)
1885 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1887 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1892 * snd_soc_find_dai_link - Find a DAI link
1895 * @id: DAI link ID to match
1896 * @name: DAI link name to match, optional
1897 * @stream_name: DAI link stream name to match, optional
1899 * This function will search all existing DAI links of the soc card to
1900 * find the link of the same ID. Since DAI links may not have their
1901 * unique ID, so name and stream name should also match if being
1904 * Return: pointer of DAI link, or NULL if not found.
1906 static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1907 int id, const char *name,
1908 const char *stream_name)
1910 struct snd_soc_pcm_runtime *rtd;
1912 for_each_card_rtds(card, rtd) {
1913 struct snd_soc_dai_link *link = rtd->dai_link;
1918 if (name && (!link->name || !strstr(link->name, name)))
1921 if (stream_name && (!link->stream_name ||
1922 !strstr(link->stream_name, stream_name)))
1931 /* Find and configure an existing physical DAI link */
1932 static int soc_tplg_link_config(struct soc_tplg *tplg,
1933 struct snd_soc_tplg_link_config *cfg)
1935 struct snd_soc_dai_link *link;
1936 const char *name, *stream_name;
1940 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1941 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1948 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1949 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1952 stream_name = cfg->stream_name;
1956 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
1959 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
1965 if (cfg->num_hw_configs)
1966 set_link_hw_format(link, cfg);
1970 set_link_flags(link,
1971 le32_to_cpu(cfg->flag_mask),
1972 le32_to_cpu(cfg->flags));
1974 /* pass control to component driver for optional further init */
1975 ret = soc_tplg_dai_link_load(tplg, link, cfg);
1977 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
1981 /* for unloading it in snd_soc_tplg_component_remove */
1982 link->dobj.index = tplg->index;
1983 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
1985 link->dobj.unload = tplg->ops->link_unload;
1986 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1992 /* Load physical link config elements from the topology context */
1993 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
1994 struct snd_soc_tplg_hdr *hdr)
1996 struct snd_soc_tplg_link_config *link;
2001 count = le32_to_cpu(hdr->count);
2003 /* check the element size and count */
2004 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2005 size = le32_to_cpu(link->size);
2006 if (size > sizeof(struct snd_soc_tplg_link_config)) {
2007 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2012 if (soc_tplg_check_elem_count(tplg, size, count,
2013 le32_to_cpu(hdr->payload_size),
2014 "physical link config"))
2017 /* config physical DAI links */
2018 for (i = 0; i < count; i++) {
2019 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2020 size = le32_to_cpu(link->size);
2021 if (size != sizeof(*link))
2024 ret = soc_tplg_link_config(tplg, link);
2028 /* offset by version-specific struct size and
2029 * real priv data size
2031 tplg->pos += size + le32_to_cpu(link->priv.size);
2038 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2039 * @tplg: topology context
2040 * @d: physical DAI configs.
2042 * The physical dai should already be registered by the platform driver.
2043 * The platform driver should specify the DAI name and ID for matching.
2045 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2046 struct snd_soc_tplg_dai *d)
2048 struct snd_soc_dai_link_component dai_component;
2049 struct snd_soc_dai *dai;
2050 struct snd_soc_dai_driver *dai_drv;
2051 struct snd_soc_pcm_stream *stream;
2052 struct snd_soc_tplg_stream_caps *caps;
2055 memset(&dai_component, 0, sizeof(dai_component));
2057 dai_component.dai_name = d->dai_name;
2058 dai = snd_soc_find_dai(&dai_component);
2060 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2065 if (le32_to_cpu(d->dai_id) != dai->id) {
2066 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2071 dai_drv = dai->driver;
2076 stream = &dai_drv->playback;
2077 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2078 ret = set_stream_info(tplg, stream, caps);
2084 stream = &dai_drv->capture;
2085 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2086 ret = set_stream_info(tplg, stream, caps);
2092 set_dai_flags(dai_drv,
2093 le32_to_cpu(d->flag_mask),
2094 le32_to_cpu(d->flags));
2096 /* pass control to component driver for optional further init */
2097 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2099 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2109 /* load physical DAI elements */
2110 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2111 struct snd_soc_tplg_hdr *hdr)
2116 count = le32_to_cpu(hdr->count);
2118 /* config the existing BE DAIs */
2119 for (i = 0; i < count; i++) {
2120 struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
2123 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2124 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2128 ret = soc_tplg_dai_config(tplg, dai);
2130 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2134 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2137 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2141 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2142 struct snd_soc_tplg_hdr *hdr)
2144 struct snd_soc_tplg_manifest *manifest;
2147 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2149 /* check ABI version by size, create a new manifest if abi not match */
2150 if (le32_to_cpu(manifest->size) != sizeof(*manifest))
2153 /* pass control to component driver for optional further init */
2154 if (tplg->ops && tplg->ops->manifest)
2155 ret = tplg->ops->manifest(tplg->comp, tplg->index, manifest);
2160 /* validate header magic, size and type */
2161 static int soc_tplg_valid_header(struct soc_tplg *tplg,
2162 struct snd_soc_tplg_hdr *hdr)
2164 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2166 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2167 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2172 if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
2174 "ASoC: invalid header of type %d at offset %ld payload_size %d\n",
2175 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2180 /* big endian firmware objects not supported atm */
2181 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
2183 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2184 tplg->pass, hdr->magic,
2185 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2189 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2191 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2192 tplg->pass, hdr->magic,
2193 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2197 /* Support ABI from version 4 */
2198 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2199 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2201 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2202 tplg->pass, hdr->abi,
2203 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2208 if (hdr->payload_size == 0) {
2209 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2210 soc_tplg_get_hdr_offset(tplg));
2217 /* check header type and call appropriate handler */
2218 static int soc_tplg_load_header(struct soc_tplg *tplg,
2219 struct snd_soc_tplg_hdr *hdr)
2221 int (*elem_load)(struct soc_tplg *tplg,
2222 struct snd_soc_tplg_hdr *hdr);
2223 unsigned int hdr_pass;
2225 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2227 tplg->index = le32_to_cpu(hdr->index);
2229 switch (le32_to_cpu(hdr->type)) {
2230 case SND_SOC_TPLG_TYPE_MIXER:
2231 case SND_SOC_TPLG_TYPE_ENUM:
2232 case SND_SOC_TPLG_TYPE_BYTES:
2233 hdr_pass = SOC_TPLG_PASS_CONTROL;
2234 elem_load = soc_tplg_kcontrol_elems_load;
2236 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2237 hdr_pass = SOC_TPLG_PASS_GRAPH;
2238 elem_load = soc_tplg_dapm_graph_elems_load;
2240 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2241 hdr_pass = SOC_TPLG_PASS_WIDGET;
2242 elem_load = soc_tplg_dapm_widget_elems_load;
2244 case SND_SOC_TPLG_TYPE_PCM:
2245 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2246 elem_load = soc_tplg_pcm_elems_load;
2248 case SND_SOC_TPLG_TYPE_DAI:
2249 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2250 elem_load = soc_tplg_dai_elems_load;
2252 case SND_SOC_TPLG_TYPE_DAI_LINK:
2253 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2254 /* physical link configurations */
2255 hdr_pass = SOC_TPLG_PASS_LINK;
2256 elem_load = soc_tplg_link_elems_load;
2258 case SND_SOC_TPLG_TYPE_MANIFEST:
2259 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2260 elem_load = soc_tplg_manifest_load;
2263 /* bespoke vendor data object */
2264 hdr_pass = SOC_TPLG_PASS_VENDOR;
2265 elem_load = soc_tplg_vendor_load;
2269 if (tplg->pass == hdr_pass) {
2271 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2272 hdr->payload_size, hdr->type, hdr->version,
2273 hdr->vendor_type, tplg->pass);
2274 return elem_load(tplg, hdr);
2280 /* process the topology file headers */
2281 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2285 /* process the header types from start to end */
2286 for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2287 struct snd_soc_tplg_hdr *hdr;
2289 tplg->hdr_pos = tplg->fw->data;
2290 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2292 while (!soc_tplg_is_eof(tplg)) {
2294 /* make sure header is valid before loading */
2295 ret = soc_tplg_valid_header(tplg, hdr);
2299 /* load the header object */
2300 ret = soc_tplg_load_header(tplg, hdr);
2302 if (ret != -EPROBE_DEFER) {
2304 "ASoC: topology: could not load header: %d\n",
2310 /* goto next header */
2311 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2312 sizeof(struct snd_soc_tplg_hdr);
2313 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2318 /* signal DAPM we are complete */
2319 ret = soc_tplg_dapm_complete(tplg);
2324 static int soc_tplg_load(struct soc_tplg *tplg)
2328 ret = soc_tplg_process_headers(tplg);
2330 return soc_tplg_complete(tplg);
2335 /* load audio component topology from "firmware" file */
2336 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2337 struct snd_soc_tplg_ops *ops, const struct firmware *fw)
2339 struct soc_tplg tplg;
2343 * check if we have sane parameters:
2344 * comp - needs to exist to keep and reference data while parsing
2345 * comp->card - used for setting card related parameters
2346 * comp->card->dev - used for resource management and prints
2347 * fw - we need it, as it is the very thing we parse
2349 if (!comp || !comp->card || !comp->card->dev || !fw)
2352 /* setup parsing context */
2353 memset(&tplg, 0, sizeof(tplg));
2355 tplg.dev = comp->card->dev;
2359 tplg.io_ops = ops->io_ops;
2360 tplg.io_ops_count = ops->io_ops_count;
2361 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2362 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2365 ret = soc_tplg_load(&tplg);
2366 /* free the created components if fail to load topology */
2368 snd_soc_tplg_component_remove(comp);
2372 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2374 /* remove dynamic controls from the component driver */
2375 int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
2377 struct snd_soc_dobj *dobj, *next_dobj;
2380 /* process the header types from end to start */
2381 for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
2383 /* remove mixer controls */
2384 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2387 switch (dobj->type) {
2388 case SND_SOC_DOBJ_BYTES:
2389 case SND_SOC_DOBJ_ENUM:
2390 case SND_SOC_DOBJ_MIXER:
2391 soc_tplg_remove_kcontrol(comp, dobj, pass);
2393 case SND_SOC_DOBJ_GRAPH:
2394 soc_tplg_remove_route(comp, dobj, pass);
2396 case SND_SOC_DOBJ_WIDGET:
2397 soc_tplg_remove_widget(comp, dobj, pass);
2399 case SND_SOC_DOBJ_PCM:
2400 soc_tplg_remove_dai(comp, dobj, pass);
2402 case SND_SOC_DOBJ_DAI_LINK:
2403 soc_tplg_remove_link(comp, dobj, pass);
2405 case SND_SOC_DOBJ_BACKEND_LINK:
2407 * call link_unload ops if extra
2408 * deinitialization is needed.
2410 remove_backend_link(comp, dobj, pass);
2413 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2420 /* let caller know if FW can be freed when no objects are left */
2421 return !list_empty(&comp->dobj_list);
2423 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);