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>
34 * We make several passes over the data (since it wont necessarily be ordered)
35 * and process objects in the following order. This guarantees the component
36 * drivers will be ready with any vendor data before the mixers and DAPM objects
37 * are loaded (that may make use of the vendor data).
39 #define SOC_TPLG_PASS_MANIFEST 0
40 #define SOC_TPLG_PASS_VENDOR 1
41 #define SOC_TPLG_PASS_MIXER 2
42 #define SOC_TPLG_PASS_WIDGET 3
43 #define SOC_TPLG_PASS_PCM_DAI 4
44 #define SOC_TPLG_PASS_GRAPH 5
45 #define SOC_TPLG_PASS_PINS 6
46 #define SOC_TPLG_PASS_BE_DAI 7
47 #define SOC_TPLG_PASS_LINK 8
49 #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
50 #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
52 /* topology context */
54 const struct firmware *fw;
56 /* runtime FW parsing */
57 const u8 *pos; /* read postion */
58 const u8 *hdr_pos; /* header position */
59 unsigned int pass; /* pass number */
61 /* component caller */
63 struct snd_soc_component *comp;
64 u32 index; /* current block index */
65 u32 req_index; /* required index, only loaded/free matching blocks */
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 static int soc_tplg_process_headers(struct soc_tplg *tplg);
80 static void soc_tplg_complete(struct soc_tplg *tplg);
81 struct snd_soc_dapm_widget *
82 snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
83 const struct snd_soc_dapm_widget *widget);
84 struct snd_soc_dapm_widget *
85 snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
86 const struct snd_soc_dapm_widget *widget);
88 /* check we dont overflow the data for this control chunk */
89 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
90 unsigned int count, size_t bytes, const char *elem_type)
92 const u8 *end = tplg->pos + elem_size * count;
94 if (end > tplg->fw->data + tplg->fw->size) {
95 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
100 /* check there is enough room in chunk for control.
101 extra bytes at the end of control are for vendor data here */
102 if (elem_size * count > bytes) {
104 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
105 elem_type, count, elem_size, bytes);
112 static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
114 const u8 *end = tplg->hdr_pos;
116 if (end >= tplg->fw->data + tplg->fw->size)
121 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
123 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
126 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
128 return (unsigned long)(tplg->pos - tplg->fw->data);
131 /* mapping of Kcontrol types and associated operations. */
132 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
133 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
134 snd_soc_put_volsw, snd_soc_info_volsw},
135 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
136 snd_soc_put_volsw_sx, NULL},
137 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
138 snd_soc_put_enum_double, snd_soc_info_enum_double},
139 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, NULL},
141 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
142 snd_soc_bytes_put, snd_soc_bytes_info},
143 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
144 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
145 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
146 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
147 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
148 snd_soc_put_strobe, NULL},
149 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
150 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
151 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
152 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, NULL},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
158 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
161 struct soc_tplg_map {
166 /* mapping of widget types from UAPI IDs to kernel IDs */
167 static const struct soc_tplg_map dapm_map[] = {
168 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
169 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
170 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
171 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
172 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
173 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
174 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
175 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
176 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
177 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
178 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
179 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
180 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
181 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
182 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
183 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
184 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
185 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
186 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
187 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
188 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
189 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
190 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
191 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
194 static int tplc_chan_get_reg(struct soc_tplg *tplg,
195 struct snd_soc_tplg_channel *chan, int map)
199 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
200 if (le32_to_cpu(chan[i].id) == map)
201 return le32_to_cpu(chan[i].reg);
207 static int tplc_chan_get_shift(struct soc_tplg *tplg,
208 struct snd_soc_tplg_channel *chan, int map)
212 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
213 if (le32_to_cpu(chan[i].id) == map)
214 return le32_to_cpu(chan[i].shift);
220 static int get_widget_id(int tplg_type)
224 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
225 if (tplg_type == dapm_map[i].uid)
226 return dapm_map[i].kid;
232 static inline void soc_bind_err(struct soc_tplg *tplg,
233 struct snd_soc_tplg_ctl_hdr *hdr, int index)
236 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
237 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
238 soc_tplg_get_offset(tplg));
241 static inline void soc_control_err(struct soc_tplg *tplg,
242 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
245 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
246 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
247 soc_tplg_get_offset(tplg));
250 /* pass vendor data to component driver for processing */
251 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
252 struct snd_soc_tplg_hdr *hdr)
256 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
257 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
259 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
266 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
267 soc_tplg_get_hdr_offset(tplg),
268 soc_tplg_get_hdr_offset(tplg),
269 hdr->type, hdr->vendor_type);
273 /* pass vendor data to component driver for processing */
274 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
275 struct snd_soc_tplg_hdr *hdr)
277 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
280 return soc_tplg_vendor_load_(tplg, hdr);
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_load(struct soc_tplg *tplg,
286 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
288 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
289 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
295 /* optionally pass new dynamic widget to component driver. This is mainly for
296 * external widgets where we can assign private data/ops */
297 static int soc_tplg_widget_ready(struct soc_tplg *tplg,
298 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
300 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
301 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
307 /* pass DAI configurations to component driver for extra initialization */
308 static int soc_tplg_dai_load(struct soc_tplg *tplg,
309 struct snd_soc_dai_driver *dai_drv,
310 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
312 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
313 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
319 /* pass link configurations to component driver for extra initialization */
320 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
321 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
323 if (tplg->comp && tplg->ops && tplg->ops->link_load)
324 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
329 /* tell the component driver that all firmware has been loaded in this request */
330 static void soc_tplg_complete(struct soc_tplg *tplg)
332 if (tplg->comp && tplg->ops && tplg->ops->complete)
333 tplg->ops->complete(tplg->comp);
336 /* add a dynamic kcontrol */
337 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
338 const struct snd_kcontrol_new *control_new, const char *prefix,
339 void *data, struct snd_kcontrol **kcontrol)
343 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
344 if (*kcontrol == NULL) {
345 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
350 err = snd_ctl_add(card, *kcontrol);
352 dev_err(dev, "ASoC: Failed to add %s: %d\n",
353 control_new->name, err);
360 /* add a dynamic kcontrol for component driver */
361 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
362 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
364 struct snd_soc_component *comp = tplg->comp;
366 return soc_tplg_add_dcontrol(comp->card->snd_card,
367 comp->dev, k, NULL, comp, kcontrol);
370 /* remove a mixer kcontrol */
371 static void remove_mixer(struct snd_soc_component *comp,
372 struct snd_soc_dobj *dobj, int pass)
374 struct snd_card *card = comp->card->snd_card;
375 struct soc_mixer_control *sm =
376 container_of(dobj, struct soc_mixer_control, dobj);
377 const unsigned int *p = NULL;
379 if (pass != SOC_TPLG_PASS_MIXER)
382 if (dobj->ops && dobj->ops->control_unload)
383 dobj->ops->control_unload(comp, dobj);
385 if (dobj->control.kcontrol->tlv.p)
386 p = dobj->control.kcontrol->tlv.p;
387 snd_ctl_remove(card, dobj->control.kcontrol);
388 list_del(&dobj->list);
393 /* remove an enum kcontrol */
394 static void remove_enum(struct snd_soc_component *comp,
395 struct snd_soc_dobj *dobj, int pass)
397 struct snd_card *card = comp->card->snd_card;
398 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
401 if (pass != SOC_TPLG_PASS_MIXER)
404 if (dobj->ops && dobj->ops->control_unload)
405 dobj->ops->control_unload(comp, dobj);
407 snd_ctl_remove(card, dobj->control.kcontrol);
408 list_del(&dobj->list);
410 kfree(dobj->control.dvalues);
411 for (i = 0; i < se->items; i++)
412 kfree(dobj->control.dtexts[i]);
413 kfree(dobj->control.dtexts);
417 /* remove a byte kcontrol */
418 static void remove_bytes(struct snd_soc_component *comp,
419 struct snd_soc_dobj *dobj, int pass)
421 struct snd_card *card = comp->card->snd_card;
422 struct soc_bytes_ext *sb =
423 container_of(dobj, struct soc_bytes_ext, dobj);
425 if (pass != SOC_TPLG_PASS_MIXER)
428 if (dobj->ops && dobj->ops->control_unload)
429 dobj->ops->control_unload(comp, dobj);
431 snd_ctl_remove(card, dobj->control.kcontrol);
432 list_del(&dobj->list);
437 static void remove_route(struct snd_soc_component *comp,
438 struct snd_soc_dobj *dobj, int pass)
440 struct snd_soc_dapm_route *route =
441 container_of(dobj, struct snd_soc_dapm_route, dobj);
443 if (pass != SOC_TPLG_PASS_GRAPH)
446 if (dobj->ops && dobj->ops->dapm_route_unload)
447 dobj->ops->dapm_route_unload(comp, dobj);
449 list_del(&dobj->list);
453 /* remove a widget and it's kcontrols - routes must be removed first */
454 static void remove_widget(struct snd_soc_component *comp,
455 struct snd_soc_dobj *dobj, int pass)
457 struct snd_card *card = comp->card->snd_card;
458 struct snd_soc_dapm_widget *w =
459 container_of(dobj, struct snd_soc_dapm_widget, dobj);
462 if (pass != SOC_TPLG_PASS_WIDGET)
465 if (dobj->ops && dobj->ops->widget_unload)
466 dobj->ops->widget_unload(comp, dobj);
472 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
473 * The enum may either have an array of values or strings.
475 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
476 /* enumerated widget mixer */
477 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
478 struct snd_kcontrol *kcontrol = w->kcontrols[i];
479 struct soc_enum *se =
480 (struct soc_enum *)kcontrol->private_value;
483 snd_ctl_remove(card, kcontrol);
485 kfree(dobj->control.dvalues);
486 for (j = 0; j < se->items; j++)
487 kfree(dobj->control.dtexts[j]);
488 kfree(dobj->control.dtexts);
491 kfree(w->kcontrol_news[i].name);
494 /* volume mixer or bytes controls */
495 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
496 struct snd_kcontrol *kcontrol = w->kcontrols[i];
498 if (dobj->widget.kcontrol_type
499 == SND_SOC_TPLG_TYPE_MIXER)
500 kfree(kcontrol->tlv.p);
502 /* Private value is used as struct soc_mixer_control
503 * for volume mixers or soc_bytes_ext for bytes
506 kfree((void *)kcontrol->private_value);
507 snd_ctl_remove(card, kcontrol);
508 kfree(w->kcontrol_news[i].name);
513 kfree(w->kcontrol_news);
515 list_del(&dobj->list);
517 /* widget w is freed by soc-dapm.c */
520 /* remove DAI configurations */
521 static void remove_dai(struct snd_soc_component *comp,
522 struct snd_soc_dobj *dobj, int pass)
524 struct snd_soc_dai_driver *dai_drv =
525 container_of(dobj, struct snd_soc_dai_driver, dobj);
526 struct snd_soc_dai *dai;
528 if (pass != SOC_TPLG_PASS_PCM_DAI)
531 if (dobj->ops && dobj->ops->dai_unload)
532 dobj->ops->dai_unload(comp, dobj);
534 list_for_each_entry(dai, &comp->dai_list, list)
535 if (dai->driver == dai_drv)
538 kfree(dai_drv->playback.stream_name);
539 kfree(dai_drv->capture.stream_name);
540 kfree(dai_drv->name);
541 list_del(&dobj->list);
545 /* remove link configurations */
546 static void remove_link(struct snd_soc_component *comp,
547 struct snd_soc_dobj *dobj, int pass)
549 struct snd_soc_dai_link *link =
550 container_of(dobj, struct snd_soc_dai_link, dobj);
552 if (pass != SOC_TPLG_PASS_PCM_DAI)
555 if (dobj->ops && dobj->ops->link_unload)
556 dobj->ops->link_unload(comp, dobj);
559 kfree(link->stream_name);
560 kfree(link->cpu_dai_name);
562 list_del(&dobj->list);
563 snd_soc_remove_dai_link(comp->card, link);
567 /* unload dai link */
568 static void remove_backend_link(struct snd_soc_component *comp,
569 struct snd_soc_dobj *dobj, int pass)
571 if (pass != SOC_TPLG_PASS_LINK)
574 if (dobj->ops && dobj->ops->link_unload)
575 dobj->ops->link_unload(comp, dobj);
578 * We don't free the link here as what remove_link() do since BE
579 * links are not allocated by topology.
580 * We however need to reset the dobj type to its initial values
582 dobj->type = SND_SOC_DOBJ_NONE;
583 list_del(&dobj->list);
586 /* bind a kcontrol to it's IO handlers */
587 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
588 struct snd_kcontrol_new *k,
589 const struct soc_tplg *tplg)
591 const struct snd_soc_tplg_kcontrol_ops *ops;
592 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
595 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
596 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
597 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
598 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
599 struct soc_bytes_ext *sbe;
600 struct snd_soc_tplg_bytes_control *be;
602 sbe = (struct soc_bytes_ext *)k->private_value;
603 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
605 /* TLV bytes controls need standard kcontrol info handler,
606 * TLV callback and extended put/get handlers.
608 k->info = snd_soc_bytes_info_ext;
609 k->tlv.c = snd_soc_bytes_tlv_callback;
611 ext_ops = tplg->bytes_ext_ops;
612 num_ops = tplg->bytes_ext_ops_count;
613 for (i = 0; i < num_ops; i++) {
614 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
615 sbe->put = ext_ops[i].put;
616 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
617 sbe->get = ext_ops[i].get;
620 if (sbe->put && sbe->get)
626 /* try and map vendor specific kcontrol handlers first */
628 num_ops = tplg->io_ops_count;
629 for (i = 0; i < num_ops; i++) {
631 if (k->put == NULL && ops[i].id == hdr->ops.put)
633 if (k->get == NULL && ops[i].id == hdr->ops.get)
635 if (k->info == NULL && ops[i].id == hdr->ops.info)
636 k->info = ops[i].info;
639 /* vendor specific handlers found ? */
640 if (k->put && k->get && k->info)
643 /* none found so try standard kcontrol handlers */
645 num_ops = ARRAY_SIZE(io_ops);
646 for (i = 0; i < num_ops; i++) {
648 if (k->put == NULL && ops[i].id == hdr->ops.put)
650 if (k->get == NULL && ops[i].id == hdr->ops.get)
652 if (k->info == NULL && ops[i].id == hdr->ops.info)
653 k->info = ops[i].info;
656 /* standard handlers found ? */
657 if (k->put && k->get && k->info)
660 /* nothing to bind */
664 /* bind a widgets to it's evnt handlers */
665 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
666 const struct snd_soc_tplg_widget_events *events,
667 int num_events, u16 event_type)
673 for (i = 0; i < num_events; i++) {
674 if (event_type == events[i].type) {
676 /* found - so assign event */
677 w->event = events[i].event_handler;
685 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
687 /* optionally pass new dynamic kcontrol to component driver. */
688 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
689 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
691 if (tplg->comp && tplg->ops && tplg->ops->control_load)
692 return tplg->ops->control_load(tplg->comp, tplg->index, k,
699 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
700 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
702 unsigned int item_len = 2 * sizeof(unsigned int);
705 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
709 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
711 p[2] = le32_to_cpu(scale->min);
712 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
713 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
715 kc->tlv.p = (void *)p;
719 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
720 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
722 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
723 u32 access = le32_to_cpu(tc->access);
725 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
728 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
730 switch (le32_to_cpu(tplg_tlv->type)) {
731 case SNDRV_CTL_TLVT_DB_SCALE:
732 return soc_tplg_create_tlv_db_scale(tplg, kc,
735 /* TODO: add support for other TLV types */
737 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
746 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
747 struct snd_kcontrol_new *kc)
752 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
755 struct snd_soc_tplg_bytes_control *be;
756 struct soc_bytes_ext *sbe;
757 struct snd_kcontrol_new kc;
760 if (soc_tplg_check_elem_count(tplg,
761 sizeof(struct snd_soc_tplg_bytes_control), count,
762 size, "mixer bytes")) {
763 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
768 for (i = 0; i < count; i++) {
769 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
771 /* validate kcontrol */
772 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
773 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
776 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
780 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
781 le32_to_cpu(be->priv.size));
784 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
785 be->hdr.name, be->hdr.access);
787 memset(&kc, 0, sizeof(kc));
788 kc.name = be->hdr.name;
789 kc.private_value = (long)sbe;
790 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
791 kc.access = le32_to_cpu(be->hdr.access);
793 sbe->max = le32_to_cpu(be->max);
794 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
795 sbe->dobj.ops = tplg->ops;
796 INIT_LIST_HEAD(&sbe->dobj.list);
798 /* map io handlers */
799 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
801 soc_control_err(tplg, &be->hdr, be->hdr.name);
806 /* pass control to driver for optional further init */
807 err = soc_tplg_init_kcontrol(tplg, &kc,
808 (struct snd_soc_tplg_ctl_hdr *)be);
810 dev_err(tplg->dev, "ASoC: failed to init %s\n",
816 /* register control here */
817 err = soc_tplg_add_kcontrol(tplg, &kc,
818 &sbe->dobj.control.kcontrol);
820 dev_err(tplg->dev, "ASoC: failed to add %s\n",
826 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
832 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
835 struct snd_soc_tplg_mixer_control *mc;
836 struct soc_mixer_control *sm;
837 struct snd_kcontrol_new kc;
840 if (soc_tplg_check_elem_count(tplg,
841 sizeof(struct snd_soc_tplg_mixer_control),
842 count, size, "mixers")) {
844 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
849 for (i = 0; i < count; i++) {
850 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
852 /* validate kcontrol */
853 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
854 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
857 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
860 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
861 le32_to_cpu(mc->priv.size));
864 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
865 mc->hdr.name, mc->hdr.access);
867 memset(&kc, 0, sizeof(kc));
868 kc.name = mc->hdr.name;
869 kc.private_value = (long)sm;
870 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
871 kc.access = le32_to_cpu(mc->hdr.access);
873 /* we only support FL/FR channel mapping atm */
874 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
876 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
878 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
880 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
883 sm->max = le32_to_cpu(mc->max);
884 sm->min = le32_to_cpu(mc->min);
885 sm->invert = le32_to_cpu(mc->invert);
886 sm->platform_max = le32_to_cpu(mc->platform_max);
887 sm->dobj.index = tplg->index;
888 sm->dobj.ops = tplg->ops;
889 sm->dobj.type = SND_SOC_DOBJ_MIXER;
890 INIT_LIST_HEAD(&sm->dobj.list);
892 /* map io handlers */
893 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
895 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
900 /* create any TLV data */
901 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
903 /* pass control to driver for optional further init */
904 err = soc_tplg_init_kcontrol(tplg, &kc,
905 (struct snd_soc_tplg_ctl_hdr *) mc);
907 dev_err(tplg->dev, "ASoC: failed to init %s\n",
909 soc_tplg_free_tlv(tplg, &kc);
914 /* register control here */
915 err = soc_tplg_add_kcontrol(tplg, &kc,
916 &sm->dobj.control.kcontrol);
918 dev_err(tplg->dev, "ASoC: failed to add %s\n",
920 soc_tplg_free_tlv(tplg, &kc);
925 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
931 static int soc_tplg_denum_create_texts(struct soc_enum *se,
932 struct snd_soc_tplg_enum_control *ec)
936 se->dobj.control.dtexts =
937 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
938 if (se->dobj.control.dtexts == NULL)
941 for (i = 0; i < ec->items; i++) {
943 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
944 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
949 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
950 if (!se->dobj.control.dtexts[i]) {
956 se->texts = (const char * const *)se->dobj.control.dtexts;
960 for (--i; i >= 0; i--)
961 kfree(se->dobj.control.dtexts[i]);
962 kfree(se->dobj.control.dtexts);
966 static int soc_tplg_denum_create_values(struct soc_enum *se,
967 struct snd_soc_tplg_enum_control *ec)
971 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
974 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
977 if (!se->dobj.control.dvalues)
980 /* convert from little-endian */
981 for (i = 0; i < le32_to_cpu(ec->items); i++) {
982 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
988 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
991 struct snd_soc_tplg_enum_control *ec;
993 struct snd_kcontrol_new kc;
996 if (soc_tplg_check_elem_count(tplg,
997 sizeof(struct snd_soc_tplg_enum_control),
998 count, size, "enums")) {
1000 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1005 for (i = 0; i < count; i++) {
1006 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1008 /* validate kcontrol */
1009 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1010 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1013 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1017 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1018 le32_to_cpu(ec->priv.size));
1020 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1021 ec->hdr.name, ec->items);
1023 memset(&kc, 0, sizeof(kc));
1024 kc.name = ec->hdr.name;
1025 kc.private_value = (long)se;
1026 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1027 kc.access = le32_to_cpu(ec->hdr.access);
1029 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1030 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1032 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1035 se->items = le32_to_cpu(ec->items);
1036 se->mask = le32_to_cpu(ec->mask);
1037 se->dobj.index = tplg->index;
1038 se->dobj.type = SND_SOC_DOBJ_ENUM;
1039 se->dobj.ops = tplg->ops;
1040 INIT_LIST_HEAD(&se->dobj.list);
1042 switch (le32_to_cpu(ec->hdr.ops.info)) {
1043 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1044 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1045 err = soc_tplg_denum_create_values(se, ec);
1048 "ASoC: could not create values for %s\n",
1054 case SND_SOC_TPLG_CTL_ENUM:
1055 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1056 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1057 err = soc_tplg_denum_create_texts(se, ec);
1060 "ASoC: could not create texts for %s\n",
1068 "ASoC: invalid enum control type %d for %s\n",
1069 ec->hdr.ops.info, ec->hdr.name);
1074 /* map io handlers */
1075 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1077 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1082 /* pass control to driver for optional further init */
1083 err = soc_tplg_init_kcontrol(tplg, &kc,
1084 (struct snd_soc_tplg_ctl_hdr *) ec);
1086 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1092 /* register control here */
1093 ret = soc_tplg_add_kcontrol(tplg,
1094 &kc, &se->dobj.control.kcontrol);
1096 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1102 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1108 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1109 struct snd_soc_tplg_hdr *hdr)
1111 struct snd_soc_tplg_ctl_hdr *control_hdr;
1114 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1115 tplg->pos += le32_to_cpu(hdr->size) +
1116 le32_to_cpu(hdr->payload_size);
1120 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1121 soc_tplg_get_offset(tplg));
1123 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
1125 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1127 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
1128 dev_err(tplg->dev, "ASoC: invalid control size\n");
1132 switch (le32_to_cpu(control_hdr->ops.info)) {
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:
1140 soc_tplg_dmixer_create(tplg, 1,
1141 le32_to_cpu(hdr->payload_size));
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:
1148 soc_tplg_denum_create(tplg, 1,
1149 le32_to_cpu(hdr->payload_size));
1151 case SND_SOC_TPLG_CTL_BYTES:
1152 soc_tplg_dbytes_create(tplg, 1,
1153 le32_to_cpu(hdr->payload_size));
1156 soc_bind_err(tplg, control_hdr, i);
1164 /* optionally pass new dynamic kcontrol to component driver. */
1165 static int soc_tplg_add_route(struct soc_tplg *tplg,
1166 struct snd_soc_dapm_route *route)
1168 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1169 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1175 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1176 struct snd_soc_tplg_hdr *hdr)
1178 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1179 struct snd_soc_tplg_dapm_graph_elem *elem;
1180 struct snd_soc_dapm_route **routes;
1184 count = le32_to_cpu(hdr->count);
1186 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1188 le32_to_cpu(hdr->size) +
1189 le32_to_cpu(hdr->payload_size);
1194 if (soc_tplg_check_elem_count(tplg,
1195 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1196 count, le32_to_cpu(hdr->payload_size), "graph")) {
1198 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1203 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1206 /* allocate memory for pointer to array of dapm routes */
1207 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1213 * allocate memory for each dapm route in the array.
1214 * This needs to be done individually so that
1215 * each route can be freed when it is removed in remove_route().
1217 for (i = 0; i < count; i++) {
1218 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1220 /* free previously allocated memory */
1221 for (j = 0; j < i; j++)
1229 for (i = 0; i < count; i++) {
1230 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1231 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1233 /* validate routes */
1234 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1235 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1239 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1240 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1244 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1245 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1250 routes[i]->source = elem->source;
1251 routes[i]->sink = elem->sink;
1253 /* set to NULL atm for tplg users */
1254 routes[i]->connected = NULL;
1255 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1256 routes[i]->control = NULL;
1258 routes[i]->control = elem->control;
1260 /* add route dobj to dobj_list */
1261 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1262 routes[i]->dobj.ops = tplg->ops;
1263 routes[i]->dobj.index = tplg->index;
1264 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1266 soc_tplg_add_route(tplg, routes[i]);
1268 /* add route, but keep going if some fail */
1269 snd_soc_dapm_add_routes(dapm, routes[i], 1);
1272 /* free memory allocated for all dapm routes in case of error */
1274 for (i = 0; i < count ; i++)
1278 * free pointer to array of dapm routes as this is no longer needed.
1279 * The memory allocated for each dapm route will be freed
1280 * when it is removed in remove_route().
1287 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1288 struct soc_tplg *tplg, int num_kcontrols)
1290 struct snd_kcontrol_new *kc;
1291 struct soc_mixer_control *sm;
1292 struct snd_soc_tplg_mixer_control *mc;
1295 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1299 for (i = 0; i < num_kcontrols; i++) {
1300 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1301 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1305 /* validate kcontrol */
1306 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1307 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1310 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1311 le32_to_cpu(mc->priv.size));
1313 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1316 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1317 if (kc[i].name == NULL)
1319 kc[i].private_value = (long)sm;
1320 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1321 kc[i].access = mc->hdr.access;
1323 /* we only support FL/FR channel mapping atm */
1324 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1326 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1328 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1330 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1335 sm->invert = mc->invert;
1336 sm->platform_max = mc->platform_max;
1337 sm->dobj.index = tplg->index;
1338 INIT_LIST_HEAD(&sm->dobj.list);
1340 /* map io handlers */
1341 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1343 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1348 /* create any TLV data */
1349 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1351 /* pass control to driver for optional further init */
1352 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1353 (struct snd_soc_tplg_ctl_hdr *)mc);
1355 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1357 soc_tplg_free_tlv(tplg, &kc[i]);
1367 for (--i; i >= 0; i--) {
1368 kfree((void *)kc[i].private_value);
1375 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1376 struct soc_tplg *tplg, int num_kcontrols)
1378 struct snd_kcontrol_new *kc;
1379 struct snd_soc_tplg_enum_control *ec;
1380 struct soc_enum *se;
1383 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1387 for (i = 0; i < num_kcontrols; i++) {
1388 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1389 /* validate kcontrol */
1390 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1391 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1394 se = kzalloc(sizeof(*se), GFP_KERNEL);
1398 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1401 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1404 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1405 if (kc[i].name == NULL) {
1409 kc[i].private_value = (long)se;
1410 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1411 kc[i].access = ec->hdr.access;
1413 /* we only support FL/FR channel mapping atm */
1414 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1415 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1417 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1420 se->items = ec->items;
1421 se->mask = ec->mask;
1422 se->dobj.index = tplg->index;
1424 switch (le32_to_cpu(ec->hdr.ops.info)) {
1425 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1426 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1427 err = soc_tplg_denum_create_values(se, ec);
1429 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1434 case SND_SOC_TPLG_CTL_ENUM:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1436 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1437 err = soc_tplg_denum_create_texts(se, ec);
1439 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1445 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1446 ec->hdr.ops.info, ec->hdr.name);
1450 /* map io handlers */
1451 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1453 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1457 /* pass control to driver for optional further init */
1458 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1459 (struct snd_soc_tplg_ctl_hdr *)ec);
1461 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1470 for (; i >= 0; i--) {
1471 /* free values and texts */
1472 se = (struct soc_enum *)kc[i].private_value;
1476 kfree(se->dobj.control.dvalues);
1477 for (j = 0; j < ec->items; j++)
1478 kfree(se->dobj.control.dtexts[j]);
1479 kfree(se->dobj.control.dtexts);
1490 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1491 struct soc_tplg *tplg, int count)
1493 struct snd_soc_tplg_bytes_control *be;
1494 struct soc_bytes_ext *sbe;
1495 struct snd_kcontrol_new *kc;
1498 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1502 for (i = 0; i < count; i++) {
1503 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1505 /* validate kcontrol */
1506 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1507 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1510 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1514 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1515 le32_to_cpu(be->priv.size));
1518 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1519 be->hdr.name, be->hdr.access);
1521 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1522 if (kc[i].name == NULL) {
1526 kc[i].private_value = (long)sbe;
1527 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1528 kc[i].access = be->hdr.access;
1531 INIT_LIST_HEAD(&sbe->dobj.list);
1533 /* map standard io handlers and check for external handlers */
1534 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1536 soc_control_err(tplg, &be->hdr, be->hdr.name);
1541 /* pass control to driver for optional further init */
1542 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1543 (struct snd_soc_tplg_ctl_hdr *)be);
1545 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1555 for (--i; i >= 0; i--) {
1556 kfree((void *)kc[i].private_value);
1564 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1565 struct snd_soc_tplg_dapm_widget *w)
1567 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1568 struct snd_soc_dapm_widget template, *widget;
1569 struct snd_soc_tplg_ctl_hdr *control_hdr;
1570 struct snd_soc_card *card = tplg->comp->card;
1571 unsigned int kcontrol_type;
1574 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1575 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1577 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1578 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1581 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1584 memset(&template, 0, sizeof(template));
1586 /* map user to kernel widget ID */
1587 template.id = get_widget_id(le32_to_cpu(w->id));
1588 if (template.id < 0)
1591 /* strings are allocated here, but used and freed by the widget */
1592 template.name = kstrdup(w->name, GFP_KERNEL);
1595 template.sname = kstrdup(w->sname, GFP_KERNEL);
1596 if (!template.sname) {
1600 template.reg = le32_to_cpu(w->reg);
1601 template.shift = le32_to_cpu(w->shift);
1602 template.mask = le32_to_cpu(w->mask);
1603 template.subseq = le32_to_cpu(w->subseq);
1604 template.on_val = w->invert ? 0 : 1;
1605 template.off_val = w->invert ? 1 : 0;
1606 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1607 template.event_flags = le16_to_cpu(w->event_flags);
1608 template.dobj.index = tplg->index;
1611 (sizeof(struct snd_soc_tplg_dapm_widget) +
1612 le32_to_cpu(w->priv.size));
1614 if (w->num_kcontrols == 0) {
1616 template.num_kcontrols = 0;
1620 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1621 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1622 w->name, w->num_kcontrols, control_hdr->type);
1624 switch (le32_to_cpu(control_hdr->ops.info)) {
1625 case SND_SOC_TPLG_CTL_VOLSW:
1626 case SND_SOC_TPLG_CTL_STROBE:
1627 case SND_SOC_TPLG_CTL_VOLSW_SX:
1628 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1629 case SND_SOC_TPLG_CTL_RANGE:
1630 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1631 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
1632 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1633 template.kcontrol_news =
1634 soc_tplg_dapm_widget_dmixer_create(tplg,
1635 template.num_kcontrols);
1636 if (!template.kcontrol_news) {
1641 case SND_SOC_TPLG_CTL_ENUM:
1642 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1643 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1644 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1645 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1646 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
1647 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1648 template.kcontrol_news =
1649 soc_tplg_dapm_widget_denum_create(tplg,
1650 template.num_kcontrols);
1651 if (!template.kcontrol_news) {
1656 case SND_SOC_TPLG_CTL_BYTES:
1657 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1658 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1659 template.kcontrol_news =
1660 soc_tplg_dapm_widget_dbytes_create(tplg,
1661 template.num_kcontrols);
1662 if (!template.kcontrol_news) {
1668 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1669 control_hdr->ops.get, control_hdr->ops.put,
1670 le32_to_cpu(control_hdr->ops.info));
1676 ret = soc_tplg_widget_load(tplg, &template, w);
1680 /* card dapm mutex is held by the core if we are loading topology
1681 * data during sound card init. */
1682 if (card->instantiated)
1683 widget = snd_soc_dapm_new_control(dapm, &template);
1685 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1686 if (IS_ERR(widget)) {
1687 ret = PTR_ERR(widget);
1691 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1692 widget->dobj.widget.kcontrol_type = kcontrol_type;
1693 widget->dobj.ops = tplg->ops;
1694 widget->dobj.index = tplg->index;
1695 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1697 ret = soc_tplg_widget_ready(tplg, widget, w);
1701 kfree(template.sname);
1702 kfree(template.name);
1707 snd_soc_tplg_widget_remove(widget);
1708 snd_soc_dapm_free_widget(widget);
1710 kfree(template.sname);
1712 kfree(template.name);
1716 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1717 struct snd_soc_tplg_hdr *hdr)
1719 struct snd_soc_tplg_dapm_widget *widget;
1722 count = le32_to_cpu(hdr->count);
1724 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1727 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1729 for (i = 0; i < count; i++) {
1730 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1731 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1732 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1736 ret = soc_tplg_dapm_widget_create(tplg, widget);
1738 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1747 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1749 struct snd_soc_card *card = tplg->comp->card;
1752 /* Card might not have been registered at this point.
1753 * If so, just return success.
1755 if (!card || !card->instantiated) {
1756 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1757 " widget card binding deferred\n");
1761 ret = snd_soc_dapm_new_widgets(card);
1763 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1769 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1770 struct snd_soc_tplg_stream_caps *caps)
1772 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1773 stream->channels_min = le32_to_cpu(caps->channels_min);
1774 stream->channels_max = le32_to_cpu(caps->channels_max);
1775 stream->rates = le32_to_cpu(caps->rates);
1776 stream->rate_min = le32_to_cpu(caps->rate_min);
1777 stream->rate_max = le32_to_cpu(caps->rate_max);
1778 stream->formats = le64_to_cpu(caps->formats);
1779 stream->sig_bits = le32_to_cpu(caps->sig_bits);
1782 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1783 unsigned int flag_mask, unsigned int flags)
1785 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1786 dai_drv->symmetric_rates =
1787 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1789 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1790 dai_drv->symmetric_channels =
1791 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1794 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1795 dai_drv->symmetric_samplebits =
1796 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1800 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1801 struct snd_soc_tplg_pcm *pcm)
1803 struct snd_soc_dai_driver *dai_drv;
1804 struct snd_soc_pcm_stream *stream;
1805 struct snd_soc_tplg_stream_caps *caps;
1808 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1809 if (dai_drv == NULL)
1812 if (strlen(pcm->dai_name))
1813 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1814 dai_drv->id = le32_to_cpu(pcm->dai_id);
1816 if (pcm->playback) {
1817 stream = &dai_drv->playback;
1818 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1819 set_stream_info(stream, caps);
1823 stream = &dai_drv->capture;
1824 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1825 set_stream_info(stream, caps);
1829 dai_drv->compress_new = snd_soc_new_compress;
1831 /* pass control to component driver for optional further init */
1832 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1834 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1835 kfree(dai_drv->playback.stream_name);
1836 kfree(dai_drv->capture.stream_name);
1837 kfree(dai_drv->name);
1842 dai_drv->dobj.index = tplg->index;
1843 dai_drv->dobj.ops = tplg->ops;
1844 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1845 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1847 /* register the DAI to the component */
1848 return snd_soc_register_dai(tplg->comp, dai_drv);
1851 static void set_link_flags(struct snd_soc_dai_link *link,
1852 unsigned int flag_mask, unsigned int flags)
1854 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1855 link->symmetric_rates =
1856 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1858 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1859 link->symmetric_channels =
1860 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1863 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1864 link->symmetric_samplebits =
1865 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1868 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1869 link->ignore_suspend =
1870 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1874 /* create the FE DAI link */
1875 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1876 struct snd_soc_tplg_pcm *pcm)
1878 struct snd_soc_dai_link *link;
1881 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1885 if (strlen(pcm->pcm_name)) {
1886 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1887 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1889 link->id = le32_to_cpu(pcm->pcm_id);
1891 if (strlen(pcm->dai_name))
1892 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1894 link->codec_name = "snd-soc-dummy";
1895 link->codec_dai_name = "snd-soc-dummy-dai";
1899 link->dpcm_playback = le32_to_cpu(pcm->playback);
1900 link->dpcm_capture = le32_to_cpu(pcm->capture);
1902 set_link_flags(link,
1903 le32_to_cpu(pcm->flag_mask),
1904 le32_to_cpu(pcm->flags));
1906 /* pass control to component driver for optional further init */
1907 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1909 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1911 kfree(link->stream_name);
1912 kfree(link->cpu_dai_name);
1917 link->dobj.index = tplg->index;
1918 link->dobj.ops = tplg->ops;
1919 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1920 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1922 snd_soc_add_dai_link(tplg->comp->card, link);
1926 /* create a FE DAI and DAI link from the PCM object */
1927 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1928 struct snd_soc_tplg_pcm *pcm)
1932 ret = soc_tplg_dai_create(tplg, pcm);
1936 return soc_tplg_fe_link_create(tplg, pcm);
1939 /* copy stream caps from the old version 4 of source */
1940 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1941 struct snd_soc_tplg_stream_caps_v4 *src)
1943 dest->size = cpu_to_le32(sizeof(*dest));
1944 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1945 dest->formats = src->formats;
1946 dest->rates = src->rates;
1947 dest->rate_min = src->rate_min;
1948 dest->rate_max = src->rate_max;
1949 dest->channels_min = src->channels_min;
1950 dest->channels_max = src->channels_max;
1951 dest->periods_min = src->periods_min;
1952 dest->periods_max = src->periods_max;
1953 dest->period_size_min = src->period_size_min;
1954 dest->period_size_max = src->period_size_max;
1955 dest->buffer_size_min = src->buffer_size_min;
1956 dest->buffer_size_max = src->buffer_size_max;
1960 * pcm_new_ver - Create the new version of PCM from the old version.
1961 * @tplg: topology context
1962 * @src: older version of pcm as a source
1963 * @pcm: latest version of pcm created from the source
1965 * Support from vesion 4. User should free the returned pcm manually.
1967 static int pcm_new_ver(struct soc_tplg *tplg,
1968 struct snd_soc_tplg_pcm *src,
1969 struct snd_soc_tplg_pcm **pcm)
1971 struct snd_soc_tplg_pcm *dest;
1972 struct snd_soc_tplg_pcm_v4 *src_v4;
1977 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
1978 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1982 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1983 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1984 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1988 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
1989 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1990 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1991 dest->pcm_id = src_v4->pcm_id;
1992 dest->dai_id = src_v4->dai_id;
1993 dest->playback = src_v4->playback;
1994 dest->capture = src_v4->capture;
1995 dest->compress = src_v4->compress;
1996 dest->num_streams = src_v4->num_streams;
1997 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
1998 memcpy(&dest->stream[i], &src_v4->stream[i],
1999 sizeof(struct snd_soc_tplg_stream));
2001 for (i = 0; i < 2; i++)
2002 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2008 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
2009 struct snd_soc_tplg_hdr *hdr)
2011 struct snd_soc_tplg_pcm *pcm, *_pcm;
2017 count = le32_to_cpu(hdr->count);
2019 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2022 /* check the element size and count */
2023 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2024 size = le32_to_cpu(pcm->size);
2025 if (size > sizeof(struct snd_soc_tplg_pcm)
2026 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
2027 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
2032 if (soc_tplg_check_elem_count(tplg,
2034 le32_to_cpu(hdr->payload_size),
2036 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2041 for (i = 0; i < count; i++) {
2042 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2043 size = le32_to_cpu(pcm->size);
2045 /* check ABI version by size, create a new version of pcm
2048 if (size == sizeof(*pcm)) {
2053 pcm_new_ver(tplg, pcm, &_pcm);
2056 /* create the FE DAIs and DAI links */
2057 soc_tplg_pcm_create(tplg, _pcm);
2059 /* offset by version-specific struct size and
2060 * real priv data size
2062 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2065 kfree(_pcm); /* free the duplicated one */
2068 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
2074 * set_link_hw_format - Set the HW audio format of the physical DAI link.
2075 * @link: &snd_soc_dai_link which should be updated
2076 * @cfg: physical link configs.
2078 * Topology context contains a list of supported HW formats (configs) and
2079 * a default format ID for the physical link. This function will use this
2080 * default ID to choose the HW format to set the link's DAI format for init.
2082 static void set_link_hw_format(struct snd_soc_dai_link *link,
2083 struct snd_soc_tplg_link_config *cfg)
2085 struct snd_soc_tplg_hw_config *hw_config;
2086 unsigned char bclk_master, fsync_master;
2087 unsigned char invert_bclk, invert_fsync;
2090 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2091 hw_config = &cfg->hw_config[i];
2092 if (hw_config->id != cfg->default_hw_config_id)
2095 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2096 SND_SOC_DAIFMT_FORMAT_MASK;
2099 switch (hw_config->clock_gated) {
2100 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2101 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2104 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2105 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2109 /* ignore the value */
2113 /* clock signal polarity */
2114 invert_bclk = hw_config->invert_bclk;
2115 invert_fsync = hw_config->invert_fsync;
2116 if (!invert_bclk && !invert_fsync)
2117 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2118 else if (!invert_bclk && invert_fsync)
2119 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2120 else if (invert_bclk && !invert_fsync)
2121 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2123 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2126 bclk_master = (hw_config->bclk_master ==
2127 SND_SOC_TPLG_BCLK_CM);
2128 fsync_master = (hw_config->fsync_master ==
2129 SND_SOC_TPLG_FSYNC_CM);
2130 if (bclk_master && fsync_master)
2131 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2132 else if (!bclk_master && fsync_master)
2133 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2134 else if (bclk_master && !fsync_master)
2135 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2137 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2142 * link_new_ver - Create a new physical link config from the old
2143 * version of source.
2144 * @tplg: topology context
2145 * @src: old version of phyical link config as a source
2146 * @link: latest version of physical link config created from the source
2148 * Support from vesion 4. User need free the returned link config manually.
2150 static int link_new_ver(struct soc_tplg *tplg,
2151 struct snd_soc_tplg_link_config *src,
2152 struct snd_soc_tplg_link_config **link)
2154 struct snd_soc_tplg_link_config *dest;
2155 struct snd_soc_tplg_link_config_v4 *src_v4;
2160 if (le32_to_cpu(src->size) !=
2161 sizeof(struct snd_soc_tplg_link_config_v4)) {
2162 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2166 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2168 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2169 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2173 dest->size = cpu_to_le32(sizeof(*dest));
2174 dest->id = src_v4->id;
2175 dest->num_streams = src_v4->num_streams;
2176 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2177 memcpy(&dest->stream[i], &src_v4->stream[i],
2178 sizeof(struct snd_soc_tplg_stream));
2184 /* Find and configure an existing physical DAI link */
2185 static int soc_tplg_link_config(struct soc_tplg *tplg,
2186 struct snd_soc_tplg_link_config *cfg)
2188 struct snd_soc_dai_link *link;
2189 const char *name, *stream_name;
2193 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2194 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2201 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2202 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2205 stream_name = cfg->stream_name;
2209 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2212 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2218 if (cfg->num_hw_configs)
2219 set_link_hw_format(link, cfg);
2223 set_link_flags(link,
2224 le32_to_cpu(cfg->flag_mask),
2225 le32_to_cpu(cfg->flags));
2227 /* pass control to component driver for optional further init */
2228 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2230 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2234 /* for unloading it in snd_soc_tplg_component_remove */
2235 link->dobj.index = tplg->index;
2236 link->dobj.ops = tplg->ops;
2237 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2238 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2244 /* Load physical link config elements from the topology context */
2245 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2246 struct snd_soc_tplg_hdr *hdr)
2248 struct snd_soc_tplg_link_config *link, *_link;
2254 count = le32_to_cpu(hdr->count);
2256 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2257 tplg->pos += le32_to_cpu(hdr->size) +
2258 le32_to_cpu(hdr->payload_size);
2262 /* check the element size and count */
2263 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2264 size = le32_to_cpu(link->size);
2265 if (size > sizeof(struct snd_soc_tplg_link_config)
2266 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2267 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2272 if (soc_tplg_check_elem_count(tplg,
2274 le32_to_cpu(hdr->payload_size),
2275 "physical link config")) {
2276 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2281 /* config physical DAI links */
2282 for (i = 0; i < count; i++) {
2283 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2284 size = le32_to_cpu(link->size);
2285 if (size == sizeof(*link)) {
2290 ret = link_new_ver(tplg, link, &_link);
2295 ret = soc_tplg_link_config(tplg, _link);
2299 /* offset by version-specific struct size and
2300 * real priv data size
2302 tplg->pos += size + le32_to_cpu(_link->priv.size);
2305 kfree(_link); /* free the duplicated one */
2312 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2313 * @tplg: topology context
2314 * @d: physical DAI configs.
2316 * The physical dai should already be registered by the platform driver.
2317 * The platform driver should specify the DAI name and ID for matching.
2319 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2320 struct snd_soc_tplg_dai *d)
2322 struct snd_soc_dai_link_component dai_component;
2323 struct snd_soc_dai *dai;
2324 struct snd_soc_dai_driver *dai_drv;
2325 struct snd_soc_pcm_stream *stream;
2326 struct snd_soc_tplg_stream_caps *caps;
2329 memset(&dai_component, 0, sizeof(dai_component));
2331 dai_component.dai_name = d->dai_name;
2332 dai = snd_soc_find_dai(&dai_component);
2334 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2339 if (le32_to_cpu(d->dai_id) != dai->id) {
2340 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2345 dai_drv = dai->driver;
2350 stream = &dai_drv->playback;
2351 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2352 set_stream_info(stream, caps);
2356 stream = &dai_drv->capture;
2357 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2358 set_stream_info(stream, caps);
2362 set_dai_flags(dai_drv,
2363 le32_to_cpu(d->flag_mask),
2364 le32_to_cpu(d->flags));
2366 /* pass control to component driver for optional further init */
2367 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2369 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2376 /* load physical DAI elements */
2377 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2378 struct snd_soc_tplg_hdr *hdr)
2380 struct snd_soc_tplg_dai *dai;
2384 count = le32_to_cpu(hdr->count);
2386 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2389 /* config the existing BE DAIs */
2390 for (i = 0; i < count; i++) {
2391 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2392 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2393 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2397 soc_tplg_dai_config(tplg, dai);
2398 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2401 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2406 * manifest_new_ver - Create a new version of manifest from the old version
2408 * @tplg: topology context
2409 * @src: old version of manifest as a source
2410 * @manifest: latest version of manifest created from the source
2412 * Support from vesion 4. Users need free the returned manifest manually.
2414 static int manifest_new_ver(struct soc_tplg *tplg,
2415 struct snd_soc_tplg_manifest *src,
2416 struct snd_soc_tplg_manifest **manifest)
2418 struct snd_soc_tplg_manifest *dest;
2419 struct snd_soc_tplg_manifest_v4 *src_v4;
2424 size = le32_to_cpu(src->size);
2425 if (size != sizeof(*src_v4)) {
2426 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2430 src->size = cpu_to_le32(sizeof(*src_v4));
2433 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2435 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2436 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2441 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2442 dest->control_elems = src_v4->control_elems;
2443 dest->widget_elems = src_v4->widget_elems;
2444 dest->graph_elems = src_v4->graph_elems;
2445 dest->pcm_elems = src_v4->pcm_elems;
2446 dest->dai_link_elems = src_v4->dai_link_elems;
2447 dest->priv.size = src_v4->priv.size;
2448 if (dest->priv.size)
2449 memcpy(dest->priv.data, src_v4->priv.data,
2450 le32_to_cpu(src_v4->priv.size));
2456 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2457 struct snd_soc_tplg_hdr *hdr)
2459 struct snd_soc_tplg_manifest *manifest, *_manifest;
2463 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2466 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2468 /* check ABI version by size, create a new manifest if abi not match */
2469 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2471 _manifest = manifest;
2474 err = manifest_new_ver(tplg, manifest, &_manifest);
2479 /* pass control to component driver for optional further init */
2480 if (tplg->comp && tplg->ops && tplg->ops->manifest)
2481 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2483 if (!abi_match) /* free the duplicated one */
2489 /* validate header magic, size and type */
2490 static int soc_valid_header(struct soc_tplg *tplg,
2491 struct snd_soc_tplg_hdr *hdr)
2493 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2496 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2498 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2499 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2504 /* big endian firmware objects not supported atm */
2505 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2507 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2508 tplg->pass, hdr->magic,
2509 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2513 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2515 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2516 tplg->pass, hdr->magic,
2517 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2521 /* Support ABI from version 4 */
2522 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2523 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2525 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2526 tplg->pass, hdr->abi,
2527 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2532 if (hdr->payload_size == 0) {
2533 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2534 soc_tplg_get_hdr_offset(tplg));
2538 if (tplg->pass == le32_to_cpu(hdr->type))
2540 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2541 hdr->payload_size, hdr->type, hdr->version,
2542 hdr->vendor_type, tplg->pass);
2547 /* check header type and call appropriate handler */
2548 static int soc_tplg_load_header(struct soc_tplg *tplg,
2549 struct snd_soc_tplg_hdr *hdr)
2551 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2553 /* check for matching ID */
2554 if (le32_to_cpu(hdr->index) != tplg->req_index &&
2555 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2558 tplg->index = le32_to_cpu(hdr->index);
2560 switch (le32_to_cpu(hdr->type)) {
2561 case SND_SOC_TPLG_TYPE_MIXER:
2562 case SND_SOC_TPLG_TYPE_ENUM:
2563 case SND_SOC_TPLG_TYPE_BYTES:
2564 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2565 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2566 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2567 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2568 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2569 case SND_SOC_TPLG_TYPE_PCM:
2570 return soc_tplg_pcm_elems_load(tplg, hdr);
2571 case SND_SOC_TPLG_TYPE_DAI:
2572 return soc_tplg_dai_elems_load(tplg, hdr);
2573 case SND_SOC_TPLG_TYPE_DAI_LINK:
2574 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2575 /* physical link configurations */
2576 return soc_tplg_link_elems_load(tplg, hdr);
2577 case SND_SOC_TPLG_TYPE_MANIFEST:
2578 return soc_tplg_manifest_load(tplg, hdr);
2580 /* bespoke vendor data object */
2581 return soc_tplg_vendor_load(tplg, hdr);
2587 /* process the topology file headers */
2588 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2590 struct snd_soc_tplg_hdr *hdr;
2593 tplg->pass = SOC_TPLG_PASS_START;
2595 /* process the header types from start to end */
2596 while (tplg->pass <= SOC_TPLG_PASS_END) {
2598 tplg->hdr_pos = tplg->fw->data;
2599 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2601 while (!soc_tplg_is_eof(tplg)) {
2603 /* make sure header is valid before loading */
2604 ret = soc_valid_header(tplg, hdr);
2610 /* load the header object */
2611 ret = soc_tplg_load_header(tplg, hdr);
2615 /* goto next header */
2616 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2617 sizeof(struct snd_soc_tplg_hdr);
2618 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2621 /* next data type pass */
2625 /* signal DAPM we are complete */
2626 ret = soc_tplg_dapm_complete(tplg);
2629 "ASoC: failed to initialise DAPM from Firmware\n");
2634 static int soc_tplg_load(struct soc_tplg *tplg)
2638 ret = soc_tplg_process_headers(tplg);
2640 soc_tplg_complete(tplg);
2645 /* load audio component topology from "firmware" file */
2646 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2647 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2649 struct soc_tplg tplg;
2652 /* setup parsing context */
2653 memset(&tplg, 0, sizeof(tplg));
2655 tplg.dev = comp->dev;
2658 tplg.req_index = id;
2659 tplg.io_ops = ops->io_ops;
2660 tplg.io_ops_count = ops->io_ops_count;
2661 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2662 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2664 ret = soc_tplg_load(&tplg);
2665 /* free the created components if fail to load topology */
2667 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2671 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2673 /* remove this dynamic widget */
2674 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2676 /* make sure we are a widget */
2677 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2680 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2682 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2684 /* remove all dynamic widgets from this DAPM context */
2685 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2688 struct snd_soc_dapm_widget *w, *next_w;
2690 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2692 /* make sure we are a widget with correct context */
2693 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2697 if (w->dobj.index != index &&
2698 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2700 /* check and free and dynamic widget kcontrols */
2701 snd_soc_tplg_widget_remove(w);
2702 snd_soc_dapm_free_widget(w);
2704 snd_soc_dapm_reset_cache(dapm);
2706 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2708 /* remove dynamic controls from the component driver */
2709 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2711 struct snd_soc_dobj *dobj, *next_dobj;
2712 int pass = SOC_TPLG_PASS_END;
2714 /* process the header types from end to start */
2715 while (pass >= SOC_TPLG_PASS_START) {
2717 /* remove mixer controls */
2718 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2722 if (dobj->index != index &&
2723 index != SND_SOC_TPLG_INDEX_ALL)
2726 switch (dobj->type) {
2727 case SND_SOC_DOBJ_MIXER:
2728 remove_mixer(comp, dobj, pass);
2730 case SND_SOC_DOBJ_ENUM:
2731 remove_enum(comp, dobj, pass);
2733 case SND_SOC_DOBJ_BYTES:
2734 remove_bytes(comp, dobj, pass);
2736 case SND_SOC_DOBJ_GRAPH:
2737 remove_route(comp, dobj, pass);
2739 case SND_SOC_DOBJ_WIDGET:
2740 remove_widget(comp, dobj, pass);
2742 case SND_SOC_DOBJ_PCM:
2743 remove_dai(comp, dobj, pass);
2745 case SND_SOC_DOBJ_DAI_LINK:
2746 remove_link(comp, dobj, pass);
2748 case SND_SOC_DOBJ_BACKEND_LINK:
2750 * call link_unload ops if extra
2751 * deinitialization is needed.
2753 remove_backend_link(comp, dobj, pass);
2756 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2764 /* let caller know if FW can be freed when no objects are left */
2765 return !list_empty(&comp->dobj_list);
2767 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);