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 (chan[i].id == map)
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 (chan[i].id == map)
214 return 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 (sm->dobj.control.kcontrol->tlv.p)
386 p = sm->dobj.control.kcontrol->tlv.p;
387 snd_ctl_remove(card, sm->dobj.control.kcontrol);
388 list_del(&sm->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, se->dobj.control.kcontrol);
408 list_del(&se->dobj.list);
410 kfree(se->dobj.control.dvalues);
411 for (i = 0; i < se->items; i++)
412 kfree(se->dobj.control.dtexts[i]);
416 /* remove a byte kcontrol */
417 static void remove_bytes(struct snd_soc_component *comp,
418 struct snd_soc_dobj *dobj, int pass)
420 struct snd_card *card = comp->card->snd_card;
421 struct soc_bytes_ext *sb =
422 container_of(dobj, struct soc_bytes_ext, dobj);
424 if (pass != SOC_TPLG_PASS_MIXER)
427 if (dobj->ops && dobj->ops->control_unload)
428 dobj->ops->control_unload(comp, dobj);
430 snd_ctl_remove(card, sb->dobj.control.kcontrol);
431 list_del(&sb->dobj.list);
435 /* remove a widget and it's kcontrols - routes must be removed first */
436 static void remove_widget(struct snd_soc_component *comp,
437 struct snd_soc_dobj *dobj, int pass)
439 struct snd_card *card = comp->card->snd_card;
440 struct snd_soc_dapm_widget *w =
441 container_of(dobj, struct snd_soc_dapm_widget, dobj);
444 if (pass != SOC_TPLG_PASS_WIDGET)
447 if (dobj->ops && dobj->ops->widget_unload)
448 dobj->ops->widget_unload(comp, dobj);
454 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
455 * The enum may either have an array of values or strings.
457 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
458 /* enumerated widget mixer */
459 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
460 struct snd_kcontrol *kcontrol = w->kcontrols[i];
461 struct soc_enum *se =
462 (struct soc_enum *)kcontrol->private_value;
465 snd_ctl_remove(card, kcontrol);
467 kfree(se->dobj.control.dvalues);
468 for (j = 0; j < se->items; j++)
469 kfree(se->dobj.control.dtexts[j]);
472 kfree(w->kcontrol_news[i].name);
475 /* volume mixer or bytes controls */
476 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
477 struct snd_kcontrol *kcontrol = w->kcontrols[i];
479 if (dobj->widget.kcontrol_type
480 == SND_SOC_TPLG_TYPE_MIXER)
481 kfree(kcontrol->tlv.p);
483 /* Private value is used as struct soc_mixer_control
484 * for volume mixers or soc_bytes_ext for bytes
487 kfree((void *)kcontrol->private_value);
488 snd_ctl_remove(card, kcontrol);
489 kfree(w->kcontrol_news[i].name);
494 kfree(w->kcontrol_news);
496 /* widget w is freed by soc-dapm.c */
499 /* remove DAI configurations */
500 static void remove_dai(struct snd_soc_component *comp,
501 struct snd_soc_dobj *dobj, int pass)
503 struct snd_soc_dai_driver *dai_drv =
504 container_of(dobj, struct snd_soc_dai_driver, dobj);
506 if (pass != SOC_TPLG_PASS_PCM_DAI)
509 if (dobj->ops && dobj->ops->dai_unload)
510 dobj->ops->dai_unload(comp, dobj);
512 kfree(dai_drv->name);
513 list_del(&dobj->list);
517 /* remove link configurations */
518 static void remove_link(struct snd_soc_component *comp,
519 struct snd_soc_dobj *dobj, int pass)
521 struct snd_soc_dai_link *link =
522 container_of(dobj, struct snd_soc_dai_link, dobj);
524 if (pass != SOC_TPLG_PASS_PCM_DAI)
527 if (dobj->ops && dobj->ops->link_unload)
528 dobj->ops->link_unload(comp, dobj);
531 kfree(link->stream_name);
532 kfree(link->cpu_dai_name);
534 list_del(&dobj->list);
535 snd_soc_remove_dai_link(comp->card, link);
539 /* bind a kcontrol to it's IO handlers */
540 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
541 struct snd_kcontrol_new *k,
542 const struct soc_tplg *tplg)
544 const struct snd_soc_tplg_kcontrol_ops *ops;
545 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
548 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
549 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
550 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
551 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
552 struct soc_bytes_ext *sbe;
553 struct snd_soc_tplg_bytes_control *be;
555 sbe = (struct soc_bytes_ext *)k->private_value;
556 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
558 /* TLV bytes controls need standard kcontrol info handler,
559 * TLV callback and extended put/get handlers.
561 k->info = snd_soc_bytes_info_ext;
562 k->tlv.c = snd_soc_bytes_tlv_callback;
564 ext_ops = tplg->bytes_ext_ops;
565 num_ops = tplg->bytes_ext_ops_count;
566 for (i = 0; i < num_ops; i++) {
567 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
568 sbe->put = ext_ops[i].put;
569 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
570 sbe->get = ext_ops[i].get;
573 if (sbe->put && sbe->get)
579 /* try and map vendor specific kcontrol handlers first */
581 num_ops = tplg->io_ops_count;
582 for (i = 0; i < num_ops; i++) {
584 if (k->put == NULL && ops[i].id == hdr->ops.put)
586 if (k->get == NULL && ops[i].id == hdr->ops.get)
588 if (k->info == NULL && ops[i].id == hdr->ops.info)
589 k->info = ops[i].info;
592 /* vendor specific handlers found ? */
593 if (k->put && k->get && k->info)
596 /* none found so try standard kcontrol handlers */
598 num_ops = ARRAY_SIZE(io_ops);
599 for (i = 0; i < num_ops; i++) {
601 if (k->put == NULL && ops[i].id == hdr->ops.put)
603 if (k->get == NULL && ops[i].id == hdr->ops.get)
605 if (k->info == NULL && ops[i].id == hdr->ops.info)
606 k->info = ops[i].info;
609 /* standard handlers found ? */
610 if (k->put && k->get && k->info)
613 /* nothing to bind */
617 /* bind a widgets to it's evnt handlers */
618 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
619 const struct snd_soc_tplg_widget_events *events,
620 int num_events, u16 event_type)
626 for (i = 0; i < num_events; i++) {
627 if (event_type == events[i].type) {
629 /* found - so assign event */
630 w->event = events[i].event_handler;
638 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
640 /* optionally pass new dynamic kcontrol to component driver. */
641 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
642 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
644 if (tplg->comp && tplg->ops && tplg->ops->control_load)
645 return tplg->ops->control_load(tplg->comp, tplg->index, k,
652 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
653 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
655 unsigned int item_len = 2 * sizeof(unsigned int);
658 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
662 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
665 p[3] = (scale->step & TLV_DB_SCALE_MASK)
666 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
668 kc->tlv.p = (void *)p;
672 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
673 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
675 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
677 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
680 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
682 switch (tplg_tlv->type) {
683 case SNDRV_CTL_TLVT_DB_SCALE:
684 return soc_tplg_create_tlv_db_scale(tplg, kc,
687 /* TODO: add support for other TLV types */
689 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
698 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
699 struct snd_kcontrol_new *kc)
704 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
707 struct snd_soc_tplg_bytes_control *be;
708 struct soc_bytes_ext *sbe;
709 struct snd_kcontrol_new kc;
712 if (soc_tplg_check_elem_count(tplg,
713 sizeof(struct snd_soc_tplg_bytes_control), count,
714 size, "mixer bytes")) {
715 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
720 for (i = 0; i < count; i++) {
721 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
723 /* validate kcontrol */
724 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
725 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
728 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
732 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
736 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
737 be->hdr.name, be->hdr.access);
739 memset(&kc, 0, sizeof(kc));
740 kc.name = be->hdr.name;
741 kc.private_value = (long)sbe;
742 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
743 kc.access = be->hdr.access;
746 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
747 sbe->dobj.ops = tplg->ops;
748 INIT_LIST_HEAD(&sbe->dobj.list);
750 /* map io handlers */
751 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
753 soc_control_err(tplg, &be->hdr, be->hdr.name);
758 /* pass control to driver for optional further init */
759 err = soc_tplg_init_kcontrol(tplg, &kc,
760 (struct snd_soc_tplg_ctl_hdr *)be);
762 dev_err(tplg->dev, "ASoC: failed to init %s\n",
768 /* register control here */
769 err = soc_tplg_add_kcontrol(tplg, &kc,
770 &sbe->dobj.control.kcontrol);
772 dev_err(tplg->dev, "ASoC: failed to add %s\n",
778 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
784 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
787 struct snd_soc_tplg_mixer_control *mc;
788 struct soc_mixer_control *sm;
789 struct snd_kcontrol_new kc;
792 if (soc_tplg_check_elem_count(tplg,
793 sizeof(struct snd_soc_tplg_mixer_control),
794 count, size, "mixers")) {
796 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
801 for (i = 0; i < count; i++) {
802 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
804 /* validate kcontrol */
805 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
806 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
809 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
812 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
816 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
817 mc->hdr.name, mc->hdr.access);
819 memset(&kc, 0, sizeof(kc));
820 kc.name = mc->hdr.name;
821 kc.private_value = (long)sm;
822 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
823 kc.access = mc->hdr.access;
825 /* we only support FL/FR channel mapping atm */
826 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
828 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
830 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
832 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
837 sm->invert = mc->invert;
838 sm->platform_max = mc->platform_max;
839 sm->dobj.index = tplg->index;
840 sm->dobj.ops = tplg->ops;
841 sm->dobj.type = SND_SOC_DOBJ_MIXER;
842 INIT_LIST_HEAD(&sm->dobj.list);
844 /* map io handlers */
845 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
847 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
852 /* pass control to driver for optional further init */
853 err = soc_tplg_init_kcontrol(tplg, &kc,
854 (struct snd_soc_tplg_ctl_hdr *) mc);
856 dev_err(tplg->dev, "ASoC: failed to init %s\n",
862 /* create any TLV data */
863 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
865 /* register control here */
866 err = soc_tplg_add_kcontrol(tplg, &kc,
867 &sm->dobj.control.kcontrol);
869 dev_err(tplg->dev, "ASoC: failed to add %s\n",
871 soc_tplg_free_tlv(tplg, &kc);
876 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
882 static int soc_tplg_denum_create_texts(struct soc_enum *se,
883 struct snd_soc_tplg_enum_control *ec)
887 se->dobj.control.dtexts =
888 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
889 if (se->dobj.control.dtexts == NULL)
892 for (i = 0; i < ec->items; i++) {
894 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
895 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
900 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
901 if (!se->dobj.control.dtexts[i]) {
907 se->texts = (const char * const *)se->dobj.control.dtexts;
911 for (--i; i >= 0; i--)
912 kfree(se->dobj.control.dtexts[i]);
913 kfree(se->dobj.control.dtexts);
917 static int soc_tplg_denum_create_values(struct soc_enum *se,
918 struct snd_soc_tplg_enum_control *ec)
920 if (ec->items > sizeof(*ec->values))
923 se->dobj.control.dvalues = kmemdup(ec->values,
924 ec->items * sizeof(u32),
926 if (!se->dobj.control.dvalues)
932 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
935 struct snd_soc_tplg_enum_control *ec;
937 struct snd_kcontrol_new kc;
940 if (soc_tplg_check_elem_count(tplg,
941 sizeof(struct snd_soc_tplg_enum_control),
942 count, size, "enums")) {
944 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
949 for (i = 0; i < count; i++) {
950 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
951 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
954 /* validate kcontrol */
955 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
956 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
959 se = kzalloc((sizeof(*se)), GFP_KERNEL);
963 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
964 ec->hdr.name, ec->items);
966 memset(&kc, 0, sizeof(kc));
967 kc.name = ec->hdr.name;
968 kc.private_value = (long)se;
969 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
970 kc.access = ec->hdr.access;
972 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
973 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
975 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
978 se->items = ec->items;
980 se->dobj.index = tplg->index;
981 se->dobj.type = SND_SOC_DOBJ_ENUM;
982 se->dobj.ops = tplg->ops;
983 INIT_LIST_HEAD(&se->dobj.list);
985 switch (ec->hdr.ops.info) {
986 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
987 case SND_SOC_TPLG_CTL_ENUM_VALUE:
988 err = soc_tplg_denum_create_values(se, ec);
991 "ASoC: could not create values for %s\n",
997 case SND_SOC_TPLG_CTL_ENUM:
998 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
999 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1000 err = soc_tplg_denum_create_texts(se, ec);
1003 "ASoC: could not create texts for %s\n",
1011 "ASoC: invalid enum control type %d for %s\n",
1012 ec->hdr.ops.info, ec->hdr.name);
1017 /* map io handlers */
1018 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1020 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1025 /* pass control to driver for optional further init */
1026 err = soc_tplg_init_kcontrol(tplg, &kc,
1027 (struct snd_soc_tplg_ctl_hdr *) ec);
1029 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1035 /* register control here */
1036 ret = soc_tplg_add_kcontrol(tplg,
1037 &kc, &se->dobj.control.kcontrol);
1039 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1045 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1051 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1052 struct snd_soc_tplg_hdr *hdr)
1054 struct snd_soc_tplg_ctl_hdr *control_hdr;
1057 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1058 tplg->pos += hdr->size + hdr->payload_size;
1062 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1063 soc_tplg_get_offset(tplg));
1065 for (i = 0; i < hdr->count; i++) {
1067 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1069 if (control_hdr->size != sizeof(*control_hdr)) {
1070 dev_err(tplg->dev, "ASoC: invalid control size\n");
1074 switch (control_hdr->ops.info) {
1075 case SND_SOC_TPLG_CTL_VOLSW:
1076 case SND_SOC_TPLG_CTL_STROBE:
1077 case SND_SOC_TPLG_CTL_VOLSW_SX:
1078 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1079 case SND_SOC_TPLG_CTL_RANGE:
1080 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1081 case SND_SOC_TPLG_DAPM_CTL_PIN:
1082 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1084 case SND_SOC_TPLG_CTL_ENUM:
1085 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1086 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1087 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1088 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1089 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1091 case SND_SOC_TPLG_CTL_BYTES:
1092 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1095 soc_bind_err(tplg, control_hdr, i);
1103 /* optionally pass new dynamic kcontrol to component driver. */
1104 static int soc_tplg_add_route(struct soc_tplg *tplg,
1105 struct snd_soc_dapm_route *route)
1107 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1108 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1114 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1115 struct snd_soc_tplg_hdr *hdr)
1117 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1118 struct snd_soc_dapm_route route;
1119 struct snd_soc_tplg_dapm_graph_elem *elem;
1120 int count = hdr->count, i;
1122 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1123 tplg->pos += hdr->size + hdr->payload_size;
1127 if (soc_tplg_check_elem_count(tplg,
1128 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1129 count, hdr->payload_size, "graph")) {
1131 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1136 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1139 for (i = 0; i < count; i++) {
1140 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1141 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1143 /* validate routes */
1144 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1145 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1147 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1148 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1150 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1151 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1154 route.source = elem->source;
1155 route.sink = elem->sink;
1156 route.connected = NULL; /* set to NULL atm for tplg users */
1157 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1158 route.control = NULL;
1160 route.control = elem->control;
1162 soc_tplg_add_route(tplg, &route);
1164 /* add route, but keep going if some fail */
1165 snd_soc_dapm_add_routes(dapm, &route, 1);
1171 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1172 struct soc_tplg *tplg, int num_kcontrols)
1174 struct snd_kcontrol_new *kc;
1175 struct soc_mixer_control *sm;
1176 struct snd_soc_tplg_mixer_control *mc;
1179 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1183 for (i = 0; i < num_kcontrols; i++) {
1184 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1185 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1189 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1192 /* validate kcontrol */
1193 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1194 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1197 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1200 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1201 if (kc[i].name == NULL)
1203 kc[i].private_value = (long)sm;
1204 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1205 kc[i].access = mc->hdr.access;
1207 /* we only support FL/FR channel mapping atm */
1208 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1210 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1212 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1214 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1219 sm->invert = mc->invert;
1220 sm->platform_max = mc->platform_max;
1221 sm->dobj.index = tplg->index;
1222 INIT_LIST_HEAD(&sm->dobj.list);
1224 /* map io handlers */
1225 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1227 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1232 /* pass control to driver for optional further init */
1233 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1234 (struct snd_soc_tplg_ctl_hdr *)mc);
1236 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1242 /* create any TLV data */
1243 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1250 for (--i; i >= 0; i--) {
1251 kfree((void *)kc[i].private_value);
1258 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1259 struct soc_tplg *tplg, int num_kcontrols)
1261 struct snd_kcontrol_new *kc;
1262 struct snd_soc_tplg_enum_control *ec;
1263 struct soc_enum *se;
1266 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1270 for (i = 0; i < num_kcontrols; i++) {
1271 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1272 /* validate kcontrol */
1273 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1274 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1277 se = kzalloc(sizeof(*se), GFP_KERNEL);
1281 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1284 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1285 if (kc[i].name == NULL) {
1289 kc[i].private_value = (long)se;
1290 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1291 kc[i].access = ec->hdr.access;
1293 /* we only support FL/FR channel mapping atm */
1294 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1295 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1297 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1300 se->items = ec->items;
1301 se->mask = ec->mask;
1302 se->dobj.index = tplg->index;
1304 switch (ec->hdr.ops.info) {
1305 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1306 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1307 err = soc_tplg_denum_create_values(se, ec);
1309 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1314 case SND_SOC_TPLG_CTL_ENUM:
1315 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1316 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1317 err = soc_tplg_denum_create_texts(se, ec);
1319 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1325 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1326 ec->hdr.ops.info, ec->hdr.name);
1330 /* map io handlers */
1331 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1333 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1337 /* pass control to driver for optional further init */
1338 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1339 (struct snd_soc_tplg_ctl_hdr *)ec);
1341 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1346 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1353 for (; i >= 0; i--) {
1354 /* free values and texts */
1355 se = (struct soc_enum *)kc[i].private_value;
1359 kfree(se->dobj.control.dvalues);
1360 for (j = 0; j < ec->items; j++)
1361 kfree(se->dobj.control.dtexts[j]);
1372 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1373 struct soc_tplg *tplg, int count)
1375 struct snd_soc_tplg_bytes_control *be;
1376 struct soc_bytes_ext *sbe;
1377 struct snd_kcontrol_new *kc;
1380 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1384 for (i = 0; i < count; i++) {
1385 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1387 /* validate kcontrol */
1388 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1389 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1392 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1396 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1400 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1401 be->hdr.name, be->hdr.access);
1403 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1404 if (kc[i].name == NULL) {
1408 kc[i].private_value = (long)sbe;
1409 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1410 kc[i].access = be->hdr.access;
1413 INIT_LIST_HEAD(&sbe->dobj.list);
1415 /* map standard io handlers and check for external handlers */
1416 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1418 soc_control_err(tplg, &be->hdr, be->hdr.name);
1423 /* pass control to driver for optional further init */
1424 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1425 (struct snd_soc_tplg_ctl_hdr *)be);
1427 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1437 for (--i; i >= 0; i--) {
1438 kfree((void *)kc[i].private_value);
1446 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1447 struct snd_soc_tplg_dapm_widget *w)
1449 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1450 struct snd_soc_dapm_widget template, *widget;
1451 struct snd_soc_tplg_ctl_hdr *control_hdr;
1452 struct snd_soc_card *card = tplg->comp->card;
1453 unsigned int kcontrol_type;
1456 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1457 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1459 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1460 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1463 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1466 memset(&template, 0, sizeof(template));
1468 /* map user to kernel widget ID */
1469 template.id = get_widget_id(w->id);
1470 if (template.id < 0)
1473 /* strings are allocated here, but used and freed by the widget */
1474 template.name = kstrdup(w->name, GFP_KERNEL);
1477 template.sname = kstrdup(w->sname, GFP_KERNEL);
1478 if (!template.sname) {
1482 template.reg = w->reg;
1483 template.shift = w->shift;
1484 template.mask = w->mask;
1485 template.subseq = w->subseq;
1486 template.on_val = w->invert ? 0 : 1;
1487 template.off_val = w->invert ? 1 : 0;
1488 template.ignore_suspend = w->ignore_suspend;
1489 template.event_flags = w->event_flags;
1490 template.dobj.index = tplg->index;
1493 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1494 if (w->num_kcontrols == 0) {
1496 template.num_kcontrols = 0;
1500 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1501 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1502 w->name, w->num_kcontrols, control_hdr->type);
1504 switch (control_hdr->ops.info) {
1505 case SND_SOC_TPLG_CTL_VOLSW:
1506 case SND_SOC_TPLG_CTL_STROBE:
1507 case SND_SOC_TPLG_CTL_VOLSW_SX:
1508 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1509 case SND_SOC_TPLG_CTL_RANGE:
1510 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1511 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
1512 template.num_kcontrols = w->num_kcontrols;
1513 template.kcontrol_news =
1514 soc_tplg_dapm_widget_dmixer_create(tplg,
1515 template.num_kcontrols);
1516 if (!template.kcontrol_news) {
1521 case SND_SOC_TPLG_CTL_ENUM:
1522 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1523 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1524 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1525 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1526 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
1527 template.num_kcontrols = w->num_kcontrols;
1528 template.kcontrol_news =
1529 soc_tplg_dapm_widget_denum_create(tplg,
1530 template.num_kcontrols);
1531 if (!template.kcontrol_news) {
1536 case SND_SOC_TPLG_CTL_BYTES:
1537 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1538 template.num_kcontrols = w->num_kcontrols;
1539 template.kcontrol_news =
1540 soc_tplg_dapm_widget_dbytes_create(tplg,
1541 template.num_kcontrols);
1542 if (!template.kcontrol_news) {
1548 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1549 control_hdr->ops.get, control_hdr->ops.put,
1550 control_hdr->ops.info);
1556 ret = soc_tplg_widget_load(tplg, &template, w);
1560 /* card dapm mutex is held by the core if we are loading topology
1561 * data during sound card init. */
1562 if (card->instantiated)
1563 widget = snd_soc_dapm_new_control(dapm, &template);
1565 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1566 if (IS_ERR(widget)) {
1567 ret = PTR_ERR(widget);
1571 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1572 widget->dobj.widget.kcontrol_type = kcontrol_type;
1573 widget->dobj.ops = tplg->ops;
1574 widget->dobj.index = tplg->index;
1575 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1577 ret = soc_tplg_widget_ready(tplg, widget, w);
1584 snd_soc_tplg_widget_remove(widget);
1585 snd_soc_dapm_free_widget(widget);
1587 kfree(template.sname);
1589 kfree(template.name);
1593 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1594 struct snd_soc_tplg_hdr *hdr)
1596 struct snd_soc_tplg_dapm_widget *widget;
1597 int ret, count = hdr->count, i;
1599 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1602 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1604 for (i = 0; i < count; i++) {
1605 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1606 if (widget->size != sizeof(*widget)) {
1607 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1611 ret = soc_tplg_dapm_widget_create(tplg, widget);
1613 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1622 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1624 struct snd_soc_card *card = tplg->comp->card;
1627 /* Card might not have been registered at this point.
1628 * If so, just return success.
1630 if (!card || !card->instantiated) {
1631 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1632 " widget card binding deferred\n");
1636 ret = snd_soc_dapm_new_widgets(card);
1638 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1644 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1645 struct snd_soc_tplg_stream_caps *caps)
1647 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1648 stream->channels_min = caps->channels_min;
1649 stream->channels_max = caps->channels_max;
1650 stream->rates = caps->rates;
1651 stream->rate_min = caps->rate_min;
1652 stream->rate_max = caps->rate_max;
1653 stream->formats = caps->formats;
1654 stream->sig_bits = caps->sig_bits;
1657 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1658 unsigned int flag_mask, unsigned int flags)
1660 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1661 dai_drv->symmetric_rates =
1662 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1664 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1665 dai_drv->symmetric_channels =
1666 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1669 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1670 dai_drv->symmetric_samplebits =
1671 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1675 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1676 struct snd_soc_tplg_pcm *pcm)
1678 struct snd_soc_dai_driver *dai_drv;
1679 struct snd_soc_pcm_stream *stream;
1680 struct snd_soc_tplg_stream_caps *caps;
1683 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1684 if (dai_drv == NULL)
1687 if (strlen(pcm->dai_name))
1688 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1689 dai_drv->id = pcm->dai_id;
1691 if (pcm->playback) {
1692 stream = &dai_drv->playback;
1693 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1694 set_stream_info(stream, caps);
1698 stream = &dai_drv->capture;
1699 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1700 set_stream_info(stream, caps);
1704 dai_drv->compress_new = snd_soc_new_compress;
1706 /* pass control to component driver for optional further init */
1707 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1709 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1714 dai_drv->dobj.index = tplg->index;
1715 dai_drv->dobj.ops = tplg->ops;
1716 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1717 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1719 /* register the DAI to the component */
1720 return snd_soc_register_dai(tplg->comp, dai_drv);
1723 static void set_link_flags(struct snd_soc_dai_link *link,
1724 unsigned int flag_mask, unsigned int flags)
1726 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1727 link->symmetric_rates =
1728 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1730 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1731 link->symmetric_channels =
1732 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1735 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1736 link->symmetric_samplebits =
1737 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1740 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1741 link->ignore_suspend =
1742 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1746 /* create the FE DAI link */
1747 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1748 struct snd_soc_tplg_pcm *pcm)
1750 struct snd_soc_dai_link *link;
1753 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1757 if (strlen(pcm->pcm_name)) {
1758 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1759 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1761 link->id = pcm->pcm_id;
1763 if (strlen(pcm->dai_name))
1764 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1766 link->codec_name = "snd-soc-dummy";
1767 link->codec_dai_name = "snd-soc-dummy-dai";
1771 link->dpcm_playback = pcm->playback;
1772 link->dpcm_capture = pcm->capture;
1774 set_link_flags(link, pcm->flag_mask, pcm->flags);
1776 /* pass control to component driver for optional further init */
1777 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1779 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1784 link->dobj.index = tplg->index;
1785 link->dobj.ops = tplg->ops;
1786 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1787 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1789 snd_soc_add_dai_link(tplg->comp->card, link);
1793 /* create a FE DAI and DAI link from the PCM object */
1794 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1795 struct snd_soc_tplg_pcm *pcm)
1799 ret = soc_tplg_dai_create(tplg, pcm);
1803 return soc_tplg_fe_link_create(tplg, pcm);
1806 /* copy stream caps from the old version 4 of source */
1807 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1808 struct snd_soc_tplg_stream_caps_v4 *src)
1810 dest->size = sizeof(*dest);
1811 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1812 dest->formats = src->formats;
1813 dest->rates = src->rates;
1814 dest->rate_min = src->rate_min;
1815 dest->rate_max = src->rate_max;
1816 dest->channels_min = src->channels_min;
1817 dest->channels_max = src->channels_max;
1818 dest->periods_min = src->periods_min;
1819 dest->periods_max = src->periods_max;
1820 dest->period_size_min = src->period_size_min;
1821 dest->period_size_max = src->period_size_max;
1822 dest->buffer_size_min = src->buffer_size_min;
1823 dest->buffer_size_max = src->buffer_size_max;
1827 * pcm_new_ver - Create the new version of PCM from the old version.
1828 * @tplg: topology context
1829 * @src: older version of pcm as a source
1830 * @pcm: latest version of pcm created from the source
1832 * Support from vesion 4. User should free the returned pcm manually.
1834 static int pcm_new_ver(struct soc_tplg *tplg,
1835 struct snd_soc_tplg_pcm *src,
1836 struct snd_soc_tplg_pcm **pcm)
1838 struct snd_soc_tplg_pcm *dest;
1839 struct snd_soc_tplg_pcm_v4 *src_v4;
1844 if (src->size != sizeof(*src_v4)) {
1845 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1849 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1850 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1851 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1855 dest->size = sizeof(*dest); /* size of latest abi version */
1856 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1857 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1858 dest->pcm_id = src_v4->pcm_id;
1859 dest->dai_id = src_v4->dai_id;
1860 dest->playback = src_v4->playback;
1861 dest->capture = src_v4->capture;
1862 dest->compress = src_v4->compress;
1863 dest->num_streams = src_v4->num_streams;
1864 for (i = 0; i < dest->num_streams; i++)
1865 memcpy(&dest->stream[i], &src_v4->stream[i],
1866 sizeof(struct snd_soc_tplg_stream));
1868 for (i = 0; i < 2; i++)
1869 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1875 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1876 struct snd_soc_tplg_hdr *hdr)
1878 struct snd_soc_tplg_pcm *pcm, *_pcm;
1879 int count = hdr->count;
1883 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1886 /* check the element size and count */
1887 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1888 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1889 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1890 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1895 if (soc_tplg_check_elem_count(tplg,
1897 hdr->payload_size, "PCM DAI")) {
1898 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1903 for (i = 0; i < count; i++) {
1904 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1906 /* check ABI version by size, create a new version of pcm
1909 if (pcm->size == sizeof(*pcm)) {
1914 pcm_new_ver(tplg, pcm, &_pcm);
1917 /* create the FE DAIs and DAI links */
1918 soc_tplg_pcm_create(tplg, _pcm);
1920 /* offset by version-specific struct size and
1921 * real priv data size
1923 tplg->pos += pcm->size + _pcm->priv.size;
1926 kfree(_pcm); /* free the duplicated one */
1929 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1935 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1936 * @link: &snd_soc_dai_link which should be updated
1937 * @cfg: physical link configs.
1939 * Topology context contains a list of supported HW formats (configs) and
1940 * a default format ID for the physical link. This function will use this
1941 * default ID to choose the HW format to set the link's DAI format for init.
1943 static void set_link_hw_format(struct snd_soc_dai_link *link,
1944 struct snd_soc_tplg_link_config *cfg)
1946 struct snd_soc_tplg_hw_config *hw_config;
1947 unsigned char bclk_master, fsync_master;
1948 unsigned char invert_bclk, invert_fsync;
1951 for (i = 0; i < cfg->num_hw_configs; i++) {
1952 hw_config = &cfg->hw_config[i];
1953 if (hw_config->id != cfg->default_hw_config_id)
1956 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1959 switch (hw_config->clock_gated) {
1960 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1961 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1964 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1965 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1969 /* ignore the value */
1973 /* clock signal polarity */
1974 invert_bclk = hw_config->invert_bclk;
1975 invert_fsync = hw_config->invert_fsync;
1976 if (!invert_bclk && !invert_fsync)
1977 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1978 else if (!invert_bclk && invert_fsync)
1979 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1980 else if (invert_bclk && !invert_fsync)
1981 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1983 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1986 bclk_master = (hw_config->bclk_master ==
1987 SND_SOC_TPLG_BCLK_CM);
1988 fsync_master = (hw_config->fsync_master ==
1989 SND_SOC_TPLG_FSYNC_CM);
1990 if (bclk_master && fsync_master)
1991 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1992 else if (!bclk_master && fsync_master)
1993 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1994 else if (bclk_master && !fsync_master)
1995 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1997 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2002 * link_new_ver - Create a new physical link config from the old
2003 * version of source.
2004 * @tplg: topology context
2005 * @src: old version of phyical link config as a source
2006 * @link: latest version of physical link config created from the source
2008 * Support from vesion 4. User need free the returned link config manually.
2010 static int link_new_ver(struct soc_tplg *tplg,
2011 struct snd_soc_tplg_link_config *src,
2012 struct snd_soc_tplg_link_config **link)
2014 struct snd_soc_tplg_link_config *dest;
2015 struct snd_soc_tplg_link_config_v4 *src_v4;
2020 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2021 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2025 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2027 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2028 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2032 dest->size = sizeof(*dest);
2033 dest->id = src_v4->id;
2034 dest->num_streams = src_v4->num_streams;
2035 for (i = 0; i < dest->num_streams; i++)
2036 memcpy(&dest->stream[i], &src_v4->stream[i],
2037 sizeof(struct snd_soc_tplg_stream));
2043 /* Find and configure an existing physical DAI link */
2044 static int soc_tplg_link_config(struct soc_tplg *tplg,
2045 struct snd_soc_tplg_link_config *cfg)
2047 struct snd_soc_dai_link *link;
2048 const char *name, *stream_name;
2052 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2053 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2060 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2061 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2064 stream_name = cfg->stream_name;
2068 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2071 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2077 if (cfg->num_hw_configs)
2078 set_link_hw_format(link, cfg);
2082 set_link_flags(link, cfg->flag_mask, cfg->flags);
2084 /* pass control to component driver for optional further init */
2085 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2087 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2095 /* Load physical link config elements from the topology context */
2096 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2097 struct snd_soc_tplg_hdr *hdr)
2099 struct snd_soc_tplg_link_config *link, *_link;
2100 int count = hdr->count;
2104 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2105 tplg->pos += hdr->size + hdr->payload_size;
2109 /* check the element size and count */
2110 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2111 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2112 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2113 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2118 if (soc_tplg_check_elem_count(tplg,
2120 hdr->payload_size, "physical link config")) {
2121 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2126 /* config physical DAI links */
2127 for (i = 0; i < count; i++) {
2128 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2129 if (link->size == sizeof(*link)) {
2134 ret = link_new_ver(tplg, link, &_link);
2139 ret = soc_tplg_link_config(tplg, _link);
2143 /* offset by version-specific struct size and
2144 * real priv data size
2146 tplg->pos += link->size + _link->priv.size;
2149 kfree(_link); /* free the duplicated one */
2156 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2157 * @tplg: topology context
2158 * @d: physical DAI configs.
2160 * The physical dai should already be registered by the platform driver.
2161 * The platform driver should specify the DAI name and ID for matching.
2163 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2164 struct snd_soc_tplg_dai *d)
2166 struct snd_soc_dai_link_component dai_component = {0};
2167 struct snd_soc_dai *dai;
2168 struct snd_soc_dai_driver *dai_drv;
2169 struct snd_soc_pcm_stream *stream;
2170 struct snd_soc_tplg_stream_caps *caps;
2173 dai_component.dai_name = d->dai_name;
2174 dai = snd_soc_find_dai(&dai_component);
2176 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2181 if (d->dai_id != dai->id) {
2182 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2187 dai_drv = dai->driver;
2192 stream = &dai_drv->playback;
2193 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2194 set_stream_info(stream, caps);
2198 stream = &dai_drv->capture;
2199 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2200 set_stream_info(stream, caps);
2204 set_dai_flags(dai_drv, d->flag_mask, d->flags);
2206 /* pass control to component driver for optional further init */
2207 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2209 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2216 /* load physical DAI elements */
2217 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2218 struct snd_soc_tplg_hdr *hdr)
2220 struct snd_soc_tplg_dai *dai;
2221 int count = hdr->count;
2224 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2227 /* config the existing BE DAIs */
2228 for (i = 0; i < count; i++) {
2229 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2230 if (dai->size != sizeof(*dai)) {
2231 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2235 soc_tplg_dai_config(tplg, dai);
2236 tplg->pos += (sizeof(*dai) + dai->priv.size);
2239 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2244 * manifest_new_ver - Create a new version of manifest from the old version
2246 * @tplg: topology context
2247 * @src: old version of manifest as a source
2248 * @manifest: latest version of manifest created from the source
2250 * Support from vesion 4. Users need free the returned manifest manually.
2252 static int manifest_new_ver(struct soc_tplg *tplg,
2253 struct snd_soc_tplg_manifest *src,
2254 struct snd_soc_tplg_manifest **manifest)
2256 struct snd_soc_tplg_manifest *dest;
2257 struct snd_soc_tplg_manifest_v4 *src_v4;
2261 if (src->size != sizeof(*src_v4)) {
2262 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2266 src->size = sizeof(*src_v4);
2269 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2271 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2272 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2276 dest->size = sizeof(*dest); /* size of latest abi version */
2277 dest->control_elems = src_v4->control_elems;
2278 dest->widget_elems = src_v4->widget_elems;
2279 dest->graph_elems = src_v4->graph_elems;
2280 dest->pcm_elems = src_v4->pcm_elems;
2281 dest->dai_link_elems = src_v4->dai_link_elems;
2282 dest->priv.size = src_v4->priv.size;
2283 if (dest->priv.size)
2284 memcpy(dest->priv.data, src_v4->priv.data,
2291 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2292 struct snd_soc_tplg_hdr *hdr)
2294 struct snd_soc_tplg_manifest *manifest, *_manifest;
2298 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2301 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2303 /* check ABI version by size, create a new manifest if abi not match */
2304 if (manifest->size == sizeof(*manifest)) {
2306 _manifest = manifest;
2309 err = manifest_new_ver(tplg, manifest, &_manifest);
2314 /* pass control to component driver for optional further init */
2315 if (tplg->comp && tplg->ops && tplg->ops->manifest)
2316 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2318 if (!abi_match) /* free the duplicated one */
2324 /* validate header magic, size and type */
2325 static int soc_valid_header(struct soc_tplg *tplg,
2326 struct snd_soc_tplg_hdr *hdr)
2328 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2331 if (hdr->size != sizeof(*hdr)) {
2333 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2334 hdr->type, soc_tplg_get_hdr_offset(tplg),
2339 /* big endian firmware objects not supported atm */
2340 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2342 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2343 tplg->pass, hdr->magic,
2344 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2348 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2350 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2351 tplg->pass, hdr->magic,
2352 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2356 /* Support ABI from version 4 */
2357 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2358 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
2360 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2361 tplg->pass, hdr->abi,
2362 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2367 if (hdr->payload_size == 0) {
2368 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2369 soc_tplg_get_hdr_offset(tplg));
2373 if (tplg->pass == hdr->type)
2375 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2376 hdr->payload_size, hdr->type, hdr->version,
2377 hdr->vendor_type, tplg->pass);
2382 /* check header type and call appropriate handler */
2383 static int soc_tplg_load_header(struct soc_tplg *tplg,
2384 struct snd_soc_tplg_hdr *hdr)
2386 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2388 /* check for matching ID */
2389 if (hdr->index != tplg->req_index &&
2390 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2393 tplg->index = hdr->index;
2395 switch (hdr->type) {
2396 case SND_SOC_TPLG_TYPE_MIXER:
2397 case SND_SOC_TPLG_TYPE_ENUM:
2398 case SND_SOC_TPLG_TYPE_BYTES:
2399 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2400 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2401 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2402 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2403 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2404 case SND_SOC_TPLG_TYPE_PCM:
2405 return soc_tplg_pcm_elems_load(tplg, hdr);
2406 case SND_SOC_TPLG_TYPE_DAI:
2407 return soc_tplg_dai_elems_load(tplg, hdr);
2408 case SND_SOC_TPLG_TYPE_DAI_LINK:
2409 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2410 /* physical link configurations */
2411 return soc_tplg_link_elems_load(tplg, hdr);
2412 case SND_SOC_TPLG_TYPE_MANIFEST:
2413 return soc_tplg_manifest_load(tplg, hdr);
2415 /* bespoke vendor data object */
2416 return soc_tplg_vendor_load(tplg, hdr);
2422 /* process the topology file headers */
2423 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2425 struct snd_soc_tplg_hdr *hdr;
2428 tplg->pass = SOC_TPLG_PASS_START;
2430 /* process the header types from start to end */
2431 while (tplg->pass <= SOC_TPLG_PASS_END) {
2433 tplg->hdr_pos = tplg->fw->data;
2434 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2436 while (!soc_tplg_is_eof(tplg)) {
2438 /* make sure header is valid before loading */
2439 ret = soc_valid_header(tplg, hdr);
2445 /* load the header object */
2446 ret = soc_tplg_load_header(tplg, hdr);
2450 /* goto next header */
2451 tplg->hdr_pos += hdr->payload_size +
2452 sizeof(struct snd_soc_tplg_hdr);
2453 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2456 /* next data type pass */
2460 /* signal DAPM we are complete */
2461 ret = soc_tplg_dapm_complete(tplg);
2464 "ASoC: failed to initialise DAPM from Firmware\n");
2469 static int soc_tplg_load(struct soc_tplg *tplg)
2473 ret = soc_tplg_process_headers(tplg);
2475 soc_tplg_complete(tplg);
2480 /* load audio component topology from "firmware" file */
2481 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2482 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2484 struct soc_tplg tplg;
2486 /* setup parsing context */
2487 memset(&tplg, 0, sizeof(tplg));
2489 tplg.dev = comp->dev;
2492 tplg.req_index = id;
2493 tplg.io_ops = ops->io_ops;
2494 tplg.io_ops_count = ops->io_ops_count;
2495 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2496 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2498 return soc_tplg_load(&tplg);
2500 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2502 /* remove this dynamic widget */
2503 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2505 /* make sure we are a widget */
2506 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2509 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2511 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2513 /* remove all dynamic widgets from this DAPM context */
2514 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2517 struct snd_soc_dapm_widget *w, *next_w;
2519 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2521 /* make sure we are a widget with correct context */
2522 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2526 if (w->dobj.index != index &&
2527 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2529 /* check and free and dynamic widget kcontrols */
2530 snd_soc_tplg_widget_remove(w);
2531 snd_soc_dapm_free_widget(w);
2533 snd_soc_dapm_reset_cache(dapm);
2535 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2537 /* remove dynamic controls from the component driver */
2538 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2540 struct snd_soc_dobj *dobj, *next_dobj;
2541 int pass = SOC_TPLG_PASS_END;
2543 /* process the header types from end to start */
2544 while (pass >= SOC_TPLG_PASS_START) {
2546 /* remove mixer controls */
2547 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2551 if (dobj->index != index &&
2552 index != SND_SOC_TPLG_INDEX_ALL)
2555 switch (dobj->type) {
2556 case SND_SOC_DOBJ_MIXER:
2557 remove_mixer(comp, dobj, pass);
2559 case SND_SOC_DOBJ_ENUM:
2560 remove_enum(comp, dobj, pass);
2562 case SND_SOC_DOBJ_BYTES:
2563 remove_bytes(comp, dobj, pass);
2565 case SND_SOC_DOBJ_WIDGET:
2566 remove_widget(comp, dobj, pass);
2568 case SND_SOC_DOBJ_PCM:
2569 remove_dai(comp, dobj, pass);
2571 case SND_SOC_DOBJ_DAI_LINK:
2572 remove_link(comp, dobj, pass);
2575 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2583 /* let caller know if FW can be freed when no objects are left */
2584 return !list_empty(&comp->dobj_list);
2586 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);