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);
505 struct snd_soc_dai *dai;
507 if (pass != SOC_TPLG_PASS_PCM_DAI)
510 if (dobj->ops && dobj->ops->dai_unload)
511 dobj->ops->dai_unload(comp, dobj);
513 list_for_each_entry(dai, &comp->dai_list, list)
514 if (dai->driver == dai_drv)
517 kfree(dai_drv->name);
518 list_del(&dobj->list);
522 /* remove link configurations */
523 static void remove_link(struct snd_soc_component *comp,
524 struct snd_soc_dobj *dobj, int pass)
526 struct snd_soc_dai_link *link =
527 container_of(dobj, struct snd_soc_dai_link, dobj);
529 if (pass != SOC_TPLG_PASS_PCM_DAI)
532 if (dobj->ops && dobj->ops->link_unload)
533 dobj->ops->link_unload(comp, dobj);
536 kfree(link->stream_name);
537 kfree(link->cpu_dai_name);
539 list_del(&dobj->list);
540 snd_soc_remove_dai_link(comp->card, link);
544 /* bind a kcontrol to it's IO handlers */
545 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
546 struct snd_kcontrol_new *k,
547 const struct soc_tplg *tplg)
549 const struct snd_soc_tplg_kcontrol_ops *ops;
550 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
553 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
554 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
555 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
556 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
557 struct soc_bytes_ext *sbe;
558 struct snd_soc_tplg_bytes_control *be;
560 sbe = (struct soc_bytes_ext *)k->private_value;
561 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
563 /* TLV bytes controls need standard kcontrol info handler,
564 * TLV callback and extended put/get handlers.
566 k->info = snd_soc_bytes_info_ext;
567 k->tlv.c = snd_soc_bytes_tlv_callback;
569 ext_ops = tplg->bytes_ext_ops;
570 num_ops = tplg->bytes_ext_ops_count;
571 for (i = 0; i < num_ops; i++) {
572 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
573 sbe->put = ext_ops[i].put;
574 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
575 sbe->get = ext_ops[i].get;
578 if (sbe->put && sbe->get)
584 /* try and map vendor specific kcontrol handlers first */
586 num_ops = tplg->io_ops_count;
587 for (i = 0; i < num_ops; i++) {
589 if (k->put == NULL && ops[i].id == hdr->ops.put)
591 if (k->get == NULL && ops[i].id == hdr->ops.get)
593 if (k->info == NULL && ops[i].id == hdr->ops.info)
594 k->info = ops[i].info;
597 /* vendor specific handlers found ? */
598 if (k->put && k->get && k->info)
601 /* none found so try standard kcontrol handlers */
603 num_ops = ARRAY_SIZE(io_ops);
604 for (i = 0; i < num_ops; i++) {
606 if (k->put == NULL && ops[i].id == hdr->ops.put)
608 if (k->get == NULL && ops[i].id == hdr->ops.get)
610 if (k->info == NULL && ops[i].id == hdr->ops.info)
611 k->info = ops[i].info;
614 /* standard handlers found ? */
615 if (k->put && k->get && k->info)
618 /* nothing to bind */
622 /* bind a widgets to it's evnt handlers */
623 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
624 const struct snd_soc_tplg_widget_events *events,
625 int num_events, u16 event_type)
631 for (i = 0; i < num_events; i++) {
632 if (event_type == events[i].type) {
634 /* found - so assign event */
635 w->event = events[i].event_handler;
643 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
645 /* optionally pass new dynamic kcontrol to component driver. */
646 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
647 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
649 if (tplg->comp && tplg->ops && tplg->ops->control_load)
650 return tplg->ops->control_load(tplg->comp, tplg->index, k,
657 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
658 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
660 unsigned int item_len = 2 * sizeof(unsigned int);
663 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
667 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
670 p[3] = (scale->step & TLV_DB_SCALE_MASK)
671 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
673 kc->tlv.p = (void *)p;
677 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
678 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
680 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
682 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
685 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
687 switch (tplg_tlv->type) {
688 case SNDRV_CTL_TLVT_DB_SCALE:
689 return soc_tplg_create_tlv_db_scale(tplg, kc,
692 /* TODO: add support for other TLV types */
694 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
703 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
704 struct snd_kcontrol_new *kc)
709 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
712 struct snd_soc_tplg_bytes_control *be;
713 struct soc_bytes_ext *sbe;
714 struct snd_kcontrol_new kc;
717 if (soc_tplg_check_elem_count(tplg,
718 sizeof(struct snd_soc_tplg_bytes_control), count,
719 size, "mixer bytes")) {
720 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
725 for (i = 0; i < count; i++) {
726 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
728 /* validate kcontrol */
729 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
730 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
733 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
737 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
741 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
742 be->hdr.name, be->hdr.access);
744 memset(&kc, 0, sizeof(kc));
745 kc.name = be->hdr.name;
746 kc.private_value = (long)sbe;
747 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
748 kc.access = be->hdr.access;
751 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
752 sbe->dobj.ops = tplg->ops;
753 INIT_LIST_HEAD(&sbe->dobj.list);
755 /* map io handlers */
756 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
758 soc_control_err(tplg, &be->hdr, be->hdr.name);
763 /* pass control to driver for optional further init */
764 err = soc_tplg_init_kcontrol(tplg, &kc,
765 (struct snd_soc_tplg_ctl_hdr *)be);
767 dev_err(tplg->dev, "ASoC: failed to init %s\n",
773 /* register control here */
774 err = soc_tplg_add_kcontrol(tplg, &kc,
775 &sbe->dobj.control.kcontrol);
777 dev_err(tplg->dev, "ASoC: failed to add %s\n",
783 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
789 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
792 struct snd_soc_tplg_mixer_control *mc;
793 struct soc_mixer_control *sm;
794 struct snd_kcontrol_new kc;
797 if (soc_tplg_check_elem_count(tplg,
798 sizeof(struct snd_soc_tplg_mixer_control),
799 count, size, "mixers")) {
801 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
806 for (i = 0; i < count; i++) {
807 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
809 /* validate kcontrol */
810 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
811 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
814 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
817 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
821 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
822 mc->hdr.name, mc->hdr.access);
824 memset(&kc, 0, sizeof(kc));
825 kc.name = mc->hdr.name;
826 kc.private_value = (long)sm;
827 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
828 kc.access = mc->hdr.access;
830 /* we only support FL/FR channel mapping atm */
831 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
833 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
835 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
837 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
842 sm->invert = mc->invert;
843 sm->platform_max = mc->platform_max;
844 sm->dobj.index = tplg->index;
845 sm->dobj.ops = tplg->ops;
846 sm->dobj.type = SND_SOC_DOBJ_MIXER;
847 INIT_LIST_HEAD(&sm->dobj.list);
849 /* map io handlers */
850 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
852 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
857 /* pass control to driver for optional further init */
858 err = soc_tplg_init_kcontrol(tplg, &kc,
859 (struct snd_soc_tplg_ctl_hdr *) mc);
861 dev_err(tplg->dev, "ASoC: failed to init %s\n",
867 /* create any TLV data */
868 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
870 /* register control here */
871 err = soc_tplg_add_kcontrol(tplg, &kc,
872 &sm->dobj.control.kcontrol);
874 dev_err(tplg->dev, "ASoC: failed to add %s\n",
876 soc_tplg_free_tlv(tplg, &kc);
881 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
887 static int soc_tplg_denum_create_texts(struct soc_enum *se,
888 struct snd_soc_tplg_enum_control *ec)
892 se->dobj.control.dtexts =
893 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
894 if (se->dobj.control.dtexts == NULL)
897 for (i = 0; i < ec->items; i++) {
899 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
900 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
905 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
906 if (!se->dobj.control.dtexts[i]) {
912 se->texts = (const char * const *)se->dobj.control.dtexts;
916 for (--i; i >= 0; i--)
917 kfree(se->dobj.control.dtexts[i]);
918 kfree(se->dobj.control.dtexts);
922 static int soc_tplg_denum_create_values(struct soc_enum *se,
923 struct snd_soc_tplg_enum_control *ec)
925 if (ec->items > sizeof(*ec->values))
928 se->dobj.control.dvalues = kmemdup(ec->values,
929 ec->items * sizeof(u32),
931 if (!se->dobj.control.dvalues)
937 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
940 struct snd_soc_tplg_enum_control *ec;
942 struct snd_kcontrol_new kc;
945 if (soc_tplg_check_elem_count(tplg,
946 sizeof(struct snd_soc_tplg_enum_control),
947 count, size, "enums")) {
949 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
954 for (i = 0; i < count; i++) {
955 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
956 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
959 /* validate kcontrol */
960 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
961 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
964 se = kzalloc((sizeof(*se)), GFP_KERNEL);
968 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
969 ec->hdr.name, ec->items);
971 memset(&kc, 0, sizeof(kc));
972 kc.name = ec->hdr.name;
973 kc.private_value = (long)se;
974 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
975 kc.access = ec->hdr.access;
977 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
978 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
980 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
983 se->items = ec->items;
985 se->dobj.index = tplg->index;
986 se->dobj.type = SND_SOC_DOBJ_ENUM;
987 se->dobj.ops = tplg->ops;
988 INIT_LIST_HEAD(&se->dobj.list);
990 switch (ec->hdr.ops.info) {
991 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
992 case SND_SOC_TPLG_CTL_ENUM_VALUE:
993 err = soc_tplg_denum_create_values(se, ec);
996 "ASoC: could not create values for %s\n",
1002 case SND_SOC_TPLG_CTL_ENUM:
1003 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1004 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1005 err = soc_tplg_denum_create_texts(se, ec);
1008 "ASoC: could not create texts for %s\n",
1016 "ASoC: invalid enum control type %d for %s\n",
1017 ec->hdr.ops.info, ec->hdr.name);
1022 /* map io handlers */
1023 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1025 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1030 /* pass control to driver for optional further init */
1031 err = soc_tplg_init_kcontrol(tplg, &kc,
1032 (struct snd_soc_tplg_ctl_hdr *) ec);
1034 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1040 /* register control here */
1041 ret = soc_tplg_add_kcontrol(tplg,
1042 &kc, &se->dobj.control.kcontrol);
1044 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1050 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1056 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1057 struct snd_soc_tplg_hdr *hdr)
1059 struct snd_soc_tplg_ctl_hdr *control_hdr;
1062 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1063 tplg->pos += hdr->size + hdr->payload_size;
1067 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1068 soc_tplg_get_offset(tplg));
1070 for (i = 0; i < hdr->count; i++) {
1072 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1074 if (control_hdr->size != sizeof(*control_hdr)) {
1075 dev_err(tplg->dev, "ASoC: invalid control size\n");
1079 switch (control_hdr->ops.info) {
1080 case SND_SOC_TPLG_CTL_VOLSW:
1081 case SND_SOC_TPLG_CTL_STROBE:
1082 case SND_SOC_TPLG_CTL_VOLSW_SX:
1083 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1084 case SND_SOC_TPLG_CTL_RANGE:
1085 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1086 case SND_SOC_TPLG_DAPM_CTL_PIN:
1087 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1089 case SND_SOC_TPLG_CTL_ENUM:
1090 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1091 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1092 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1093 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1094 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1096 case SND_SOC_TPLG_CTL_BYTES:
1097 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1100 soc_bind_err(tplg, control_hdr, i);
1108 /* optionally pass new dynamic kcontrol to component driver. */
1109 static int soc_tplg_add_route(struct soc_tplg *tplg,
1110 struct snd_soc_dapm_route *route)
1112 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1113 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1119 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1120 struct snd_soc_tplg_hdr *hdr)
1122 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1123 struct snd_soc_dapm_route route;
1124 struct snd_soc_tplg_dapm_graph_elem *elem;
1125 int count = hdr->count, i;
1127 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1128 tplg->pos += hdr->size + hdr->payload_size;
1132 if (soc_tplg_check_elem_count(tplg,
1133 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1134 count, hdr->payload_size, "graph")) {
1136 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1141 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1144 for (i = 0; i < count; i++) {
1145 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1146 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1148 /* validate routes */
1149 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1150 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1152 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1153 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1155 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1156 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1159 route.source = elem->source;
1160 route.sink = elem->sink;
1161 route.connected = NULL; /* set to NULL atm for tplg users */
1162 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1163 route.control = NULL;
1165 route.control = elem->control;
1167 soc_tplg_add_route(tplg, &route);
1169 /* add route, but keep going if some fail */
1170 snd_soc_dapm_add_routes(dapm, &route, 1);
1176 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1177 struct soc_tplg *tplg, int num_kcontrols)
1179 struct snd_kcontrol_new *kc;
1180 struct soc_mixer_control *sm;
1181 struct snd_soc_tplg_mixer_control *mc;
1184 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1188 for (i = 0; i < num_kcontrols; i++) {
1189 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1190 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1194 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1197 /* validate kcontrol */
1198 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1199 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1202 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1205 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1206 if (kc[i].name == NULL)
1208 kc[i].private_value = (long)sm;
1209 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1210 kc[i].access = mc->hdr.access;
1212 /* we only support FL/FR channel mapping atm */
1213 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1215 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1217 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1219 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1224 sm->invert = mc->invert;
1225 sm->platform_max = mc->platform_max;
1226 sm->dobj.index = tplg->index;
1227 INIT_LIST_HEAD(&sm->dobj.list);
1229 /* map io handlers */
1230 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1232 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1237 /* pass control to driver for optional further init */
1238 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1239 (struct snd_soc_tplg_ctl_hdr *)mc);
1241 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1247 /* create any TLV data */
1248 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1255 for (--i; i >= 0; i--) {
1256 kfree((void *)kc[i].private_value);
1263 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1264 struct soc_tplg *tplg, int num_kcontrols)
1266 struct snd_kcontrol_new *kc;
1267 struct snd_soc_tplg_enum_control *ec;
1268 struct soc_enum *se;
1271 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1275 for (i = 0; i < num_kcontrols; i++) {
1276 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1277 /* validate kcontrol */
1278 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1279 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1282 se = kzalloc(sizeof(*se), GFP_KERNEL);
1286 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1289 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1290 if (kc[i].name == NULL) {
1294 kc[i].private_value = (long)se;
1295 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1296 kc[i].access = ec->hdr.access;
1298 /* we only support FL/FR channel mapping atm */
1299 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1300 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1302 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1305 se->items = ec->items;
1306 se->mask = ec->mask;
1307 se->dobj.index = tplg->index;
1309 switch (ec->hdr.ops.info) {
1310 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1311 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1312 err = soc_tplg_denum_create_values(se, ec);
1314 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1319 case SND_SOC_TPLG_CTL_ENUM:
1320 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1321 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1322 err = soc_tplg_denum_create_texts(se, ec);
1324 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1330 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1331 ec->hdr.ops.info, ec->hdr.name);
1335 /* map io handlers */
1336 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1338 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1342 /* pass control to driver for optional further init */
1343 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1344 (struct snd_soc_tplg_ctl_hdr *)ec);
1346 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1351 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1358 for (; i >= 0; i--) {
1359 /* free values and texts */
1360 se = (struct soc_enum *)kc[i].private_value;
1364 kfree(se->dobj.control.dvalues);
1365 for (j = 0; j < ec->items; j++)
1366 kfree(se->dobj.control.dtexts[j]);
1377 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1378 struct soc_tplg *tplg, int count)
1380 struct snd_soc_tplg_bytes_control *be;
1381 struct soc_bytes_ext *sbe;
1382 struct snd_kcontrol_new *kc;
1385 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1389 for (i = 0; i < count; i++) {
1390 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1392 /* validate kcontrol */
1393 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1394 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1397 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1401 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1405 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1406 be->hdr.name, be->hdr.access);
1408 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1409 if (kc[i].name == NULL) {
1413 kc[i].private_value = (long)sbe;
1414 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1415 kc[i].access = be->hdr.access;
1418 INIT_LIST_HEAD(&sbe->dobj.list);
1420 /* map standard io handlers and check for external handlers */
1421 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1423 soc_control_err(tplg, &be->hdr, be->hdr.name);
1428 /* pass control to driver for optional further init */
1429 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1430 (struct snd_soc_tplg_ctl_hdr *)be);
1432 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1442 for (--i; i >= 0; i--) {
1443 kfree((void *)kc[i].private_value);
1451 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1452 struct snd_soc_tplg_dapm_widget *w)
1454 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1455 struct snd_soc_dapm_widget template, *widget;
1456 struct snd_soc_tplg_ctl_hdr *control_hdr;
1457 struct snd_soc_card *card = tplg->comp->card;
1458 unsigned int kcontrol_type;
1461 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1462 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1464 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1465 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1468 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1471 memset(&template, 0, sizeof(template));
1473 /* map user to kernel widget ID */
1474 template.id = get_widget_id(w->id);
1475 if (template.id < 0)
1478 /* strings are allocated here, but used and freed by the widget */
1479 template.name = kstrdup(w->name, GFP_KERNEL);
1482 template.sname = kstrdup(w->sname, GFP_KERNEL);
1483 if (!template.sname) {
1487 template.reg = w->reg;
1488 template.shift = w->shift;
1489 template.mask = w->mask;
1490 template.subseq = w->subseq;
1491 template.on_val = w->invert ? 0 : 1;
1492 template.off_val = w->invert ? 1 : 0;
1493 template.ignore_suspend = w->ignore_suspend;
1494 template.event_flags = w->event_flags;
1495 template.dobj.index = tplg->index;
1498 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1499 if (w->num_kcontrols == 0) {
1501 template.num_kcontrols = 0;
1505 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1506 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1507 w->name, w->num_kcontrols, control_hdr->type);
1509 switch (control_hdr->ops.info) {
1510 case SND_SOC_TPLG_CTL_VOLSW:
1511 case SND_SOC_TPLG_CTL_STROBE:
1512 case SND_SOC_TPLG_CTL_VOLSW_SX:
1513 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1514 case SND_SOC_TPLG_CTL_RANGE:
1515 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1516 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
1517 template.num_kcontrols = w->num_kcontrols;
1518 template.kcontrol_news =
1519 soc_tplg_dapm_widget_dmixer_create(tplg,
1520 template.num_kcontrols);
1521 if (!template.kcontrol_news) {
1526 case SND_SOC_TPLG_CTL_ENUM:
1527 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1528 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1529 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1530 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1531 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
1532 template.num_kcontrols = w->num_kcontrols;
1533 template.kcontrol_news =
1534 soc_tplg_dapm_widget_denum_create(tplg,
1535 template.num_kcontrols);
1536 if (!template.kcontrol_news) {
1541 case SND_SOC_TPLG_CTL_BYTES:
1542 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1543 template.num_kcontrols = w->num_kcontrols;
1544 template.kcontrol_news =
1545 soc_tplg_dapm_widget_dbytes_create(tplg,
1546 template.num_kcontrols);
1547 if (!template.kcontrol_news) {
1553 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1554 control_hdr->ops.get, control_hdr->ops.put,
1555 control_hdr->ops.info);
1561 ret = soc_tplg_widget_load(tplg, &template, w);
1565 /* card dapm mutex is held by the core if we are loading topology
1566 * data during sound card init. */
1567 if (card->instantiated)
1568 widget = snd_soc_dapm_new_control(dapm, &template);
1570 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1571 if (IS_ERR(widget)) {
1572 ret = PTR_ERR(widget);
1576 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1577 widget->dobj.widget.kcontrol_type = kcontrol_type;
1578 widget->dobj.ops = tplg->ops;
1579 widget->dobj.index = tplg->index;
1580 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1582 ret = soc_tplg_widget_ready(tplg, widget, w);
1589 snd_soc_tplg_widget_remove(widget);
1590 snd_soc_dapm_free_widget(widget);
1592 kfree(template.sname);
1594 kfree(template.name);
1598 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1599 struct snd_soc_tplg_hdr *hdr)
1601 struct snd_soc_tplg_dapm_widget *widget;
1602 int ret, count = hdr->count, i;
1604 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1607 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1609 for (i = 0; i < count; i++) {
1610 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1611 if (widget->size != sizeof(*widget)) {
1612 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1616 ret = soc_tplg_dapm_widget_create(tplg, widget);
1618 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1627 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1629 struct snd_soc_card *card = tplg->comp->card;
1632 /* Card might not have been registered at this point.
1633 * If so, just return success.
1635 if (!card || !card->instantiated) {
1636 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1637 " widget card binding deferred\n");
1641 ret = snd_soc_dapm_new_widgets(card);
1643 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1649 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1650 struct snd_soc_tplg_stream_caps *caps)
1652 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1653 stream->channels_min = caps->channels_min;
1654 stream->channels_max = caps->channels_max;
1655 stream->rates = caps->rates;
1656 stream->rate_min = caps->rate_min;
1657 stream->rate_max = caps->rate_max;
1658 stream->formats = caps->formats;
1659 stream->sig_bits = caps->sig_bits;
1662 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1663 unsigned int flag_mask, unsigned int flags)
1665 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1666 dai_drv->symmetric_rates =
1667 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1669 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1670 dai_drv->symmetric_channels =
1671 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1674 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1675 dai_drv->symmetric_samplebits =
1676 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1680 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1681 struct snd_soc_tplg_pcm *pcm)
1683 struct snd_soc_dai_driver *dai_drv;
1684 struct snd_soc_pcm_stream *stream;
1685 struct snd_soc_tplg_stream_caps *caps;
1688 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1689 if (dai_drv == NULL)
1692 if (strlen(pcm->dai_name))
1693 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1694 dai_drv->id = pcm->dai_id;
1696 if (pcm->playback) {
1697 stream = &dai_drv->playback;
1698 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1699 set_stream_info(stream, caps);
1703 stream = &dai_drv->capture;
1704 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1705 set_stream_info(stream, caps);
1709 dai_drv->compress_new = snd_soc_new_compress;
1711 /* pass control to component driver for optional further init */
1712 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1714 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1719 dai_drv->dobj.index = tplg->index;
1720 dai_drv->dobj.ops = tplg->ops;
1721 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1722 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1724 /* register the DAI to the component */
1725 return snd_soc_register_dai(tplg->comp, dai_drv);
1728 static void set_link_flags(struct snd_soc_dai_link *link,
1729 unsigned int flag_mask, unsigned int flags)
1731 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1732 link->symmetric_rates =
1733 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1735 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1736 link->symmetric_channels =
1737 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1740 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1741 link->symmetric_samplebits =
1742 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1745 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1746 link->ignore_suspend =
1747 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1751 /* create the FE DAI link */
1752 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1753 struct snd_soc_tplg_pcm *pcm)
1755 struct snd_soc_dai_link *link;
1758 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1762 if (strlen(pcm->pcm_name)) {
1763 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1764 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1766 link->id = pcm->pcm_id;
1768 if (strlen(pcm->dai_name))
1769 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1771 link->codec_name = "snd-soc-dummy";
1772 link->codec_dai_name = "snd-soc-dummy-dai";
1776 link->dpcm_playback = pcm->playback;
1777 link->dpcm_capture = pcm->capture;
1779 set_link_flags(link, pcm->flag_mask, pcm->flags);
1781 /* pass control to component driver for optional further init */
1782 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1784 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1789 link->dobj.index = tplg->index;
1790 link->dobj.ops = tplg->ops;
1791 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1792 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1794 snd_soc_add_dai_link(tplg->comp->card, link);
1798 /* create a FE DAI and DAI link from the PCM object */
1799 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1800 struct snd_soc_tplg_pcm *pcm)
1804 ret = soc_tplg_dai_create(tplg, pcm);
1808 return soc_tplg_fe_link_create(tplg, pcm);
1811 /* copy stream caps from the old version 4 of source */
1812 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1813 struct snd_soc_tplg_stream_caps_v4 *src)
1815 dest->size = sizeof(*dest);
1816 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1817 dest->formats = src->formats;
1818 dest->rates = src->rates;
1819 dest->rate_min = src->rate_min;
1820 dest->rate_max = src->rate_max;
1821 dest->channels_min = src->channels_min;
1822 dest->channels_max = src->channels_max;
1823 dest->periods_min = src->periods_min;
1824 dest->periods_max = src->periods_max;
1825 dest->period_size_min = src->period_size_min;
1826 dest->period_size_max = src->period_size_max;
1827 dest->buffer_size_min = src->buffer_size_min;
1828 dest->buffer_size_max = src->buffer_size_max;
1832 * pcm_new_ver - Create the new version of PCM from the old version.
1833 * @tplg: topology context
1834 * @src: older version of pcm as a source
1835 * @pcm: latest version of pcm created from the source
1837 * Support from vesion 4. User should free the returned pcm manually.
1839 static int pcm_new_ver(struct soc_tplg *tplg,
1840 struct snd_soc_tplg_pcm *src,
1841 struct snd_soc_tplg_pcm **pcm)
1843 struct snd_soc_tplg_pcm *dest;
1844 struct snd_soc_tplg_pcm_v4 *src_v4;
1849 if (src->size != sizeof(*src_v4)) {
1850 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1854 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1855 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1856 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1860 dest->size = sizeof(*dest); /* size of latest abi version */
1861 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1862 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1863 dest->pcm_id = src_v4->pcm_id;
1864 dest->dai_id = src_v4->dai_id;
1865 dest->playback = src_v4->playback;
1866 dest->capture = src_v4->capture;
1867 dest->compress = src_v4->compress;
1868 dest->num_streams = src_v4->num_streams;
1869 for (i = 0; i < dest->num_streams; i++)
1870 memcpy(&dest->stream[i], &src_v4->stream[i],
1871 sizeof(struct snd_soc_tplg_stream));
1873 for (i = 0; i < 2; i++)
1874 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1880 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1881 struct snd_soc_tplg_hdr *hdr)
1883 struct snd_soc_tplg_pcm *pcm, *_pcm;
1884 int count = hdr->count;
1888 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1891 /* check the element size and count */
1892 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1893 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1894 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1895 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1900 if (soc_tplg_check_elem_count(tplg,
1902 hdr->payload_size, "PCM DAI")) {
1903 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1908 for (i = 0; i < count; i++) {
1909 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1911 /* check ABI version by size, create a new version of pcm
1914 if (pcm->size == sizeof(*pcm)) {
1919 pcm_new_ver(tplg, pcm, &_pcm);
1922 /* create the FE DAIs and DAI links */
1923 soc_tplg_pcm_create(tplg, _pcm);
1925 /* offset by version-specific struct size and
1926 * real priv data size
1928 tplg->pos += pcm->size + _pcm->priv.size;
1931 kfree(_pcm); /* free the duplicated one */
1934 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1940 * set_link_hw_format - Set the HW audio format of the physical DAI link.
1941 * @link: &snd_soc_dai_link which should be updated
1942 * @cfg: physical link configs.
1944 * Topology context contains a list of supported HW formats (configs) and
1945 * a default format ID for the physical link. This function will use this
1946 * default ID to choose the HW format to set the link's DAI format for init.
1948 static void set_link_hw_format(struct snd_soc_dai_link *link,
1949 struct snd_soc_tplg_link_config *cfg)
1951 struct snd_soc_tplg_hw_config *hw_config;
1952 unsigned char bclk_master, fsync_master;
1953 unsigned char invert_bclk, invert_fsync;
1956 for (i = 0; i < cfg->num_hw_configs; i++) {
1957 hw_config = &cfg->hw_config[i];
1958 if (hw_config->id != cfg->default_hw_config_id)
1961 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1964 switch (hw_config->clock_gated) {
1965 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1966 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1969 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1970 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1974 /* ignore the value */
1978 /* clock signal polarity */
1979 invert_bclk = hw_config->invert_bclk;
1980 invert_fsync = hw_config->invert_fsync;
1981 if (!invert_bclk && !invert_fsync)
1982 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1983 else if (!invert_bclk && invert_fsync)
1984 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1985 else if (invert_bclk && !invert_fsync)
1986 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1988 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1991 bclk_master = (hw_config->bclk_master ==
1992 SND_SOC_TPLG_BCLK_CM);
1993 fsync_master = (hw_config->fsync_master ==
1994 SND_SOC_TPLG_FSYNC_CM);
1995 if (bclk_master && fsync_master)
1996 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1997 else if (!bclk_master && fsync_master)
1998 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1999 else if (bclk_master && !fsync_master)
2000 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2002 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2007 * link_new_ver - Create a new physical link config from the old
2008 * version of source.
2009 * @tplg: topology context
2010 * @src: old version of phyical link config as a source
2011 * @link: latest version of physical link config created from the source
2013 * Support from vesion 4. User need free the returned link config manually.
2015 static int link_new_ver(struct soc_tplg *tplg,
2016 struct snd_soc_tplg_link_config *src,
2017 struct snd_soc_tplg_link_config **link)
2019 struct snd_soc_tplg_link_config *dest;
2020 struct snd_soc_tplg_link_config_v4 *src_v4;
2025 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2026 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2030 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2032 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2033 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2037 dest->size = sizeof(*dest);
2038 dest->id = src_v4->id;
2039 dest->num_streams = src_v4->num_streams;
2040 for (i = 0; i < dest->num_streams; i++)
2041 memcpy(&dest->stream[i], &src_v4->stream[i],
2042 sizeof(struct snd_soc_tplg_stream));
2048 /* Find and configure an existing physical DAI link */
2049 static int soc_tplg_link_config(struct soc_tplg *tplg,
2050 struct snd_soc_tplg_link_config *cfg)
2052 struct snd_soc_dai_link *link;
2053 const char *name, *stream_name;
2057 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2058 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2065 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2066 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2069 stream_name = cfg->stream_name;
2073 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2076 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2082 if (cfg->num_hw_configs)
2083 set_link_hw_format(link, cfg);
2087 set_link_flags(link, cfg->flag_mask, cfg->flags);
2089 /* pass control to component driver for optional further init */
2090 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2092 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2100 /* Load physical link config elements from the topology context */
2101 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2102 struct snd_soc_tplg_hdr *hdr)
2104 struct snd_soc_tplg_link_config *link, *_link;
2105 int count = hdr->count;
2109 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2110 tplg->pos += hdr->size + hdr->payload_size;
2114 /* check the element size and count */
2115 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2116 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2117 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2118 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2123 if (soc_tplg_check_elem_count(tplg,
2125 hdr->payload_size, "physical link config")) {
2126 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2131 /* config physical DAI links */
2132 for (i = 0; i < count; i++) {
2133 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2134 if (link->size == sizeof(*link)) {
2139 ret = link_new_ver(tplg, link, &_link);
2144 ret = soc_tplg_link_config(tplg, _link);
2148 /* offset by version-specific struct size and
2149 * real priv data size
2151 tplg->pos += link->size + _link->priv.size;
2154 kfree(_link); /* free the duplicated one */
2161 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2162 * @tplg: topology context
2163 * @d: physical DAI configs.
2165 * The physical dai should already be registered by the platform driver.
2166 * The platform driver should specify the DAI name and ID for matching.
2168 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2169 struct snd_soc_tplg_dai *d)
2171 struct snd_soc_dai_link_component dai_component = {0};
2172 struct snd_soc_dai *dai;
2173 struct snd_soc_dai_driver *dai_drv;
2174 struct snd_soc_pcm_stream *stream;
2175 struct snd_soc_tplg_stream_caps *caps;
2178 dai_component.dai_name = d->dai_name;
2179 dai = snd_soc_find_dai(&dai_component);
2181 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2186 if (d->dai_id != dai->id) {
2187 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2192 dai_drv = dai->driver;
2197 stream = &dai_drv->playback;
2198 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2199 set_stream_info(stream, caps);
2203 stream = &dai_drv->capture;
2204 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2205 set_stream_info(stream, caps);
2209 set_dai_flags(dai_drv, d->flag_mask, d->flags);
2211 /* pass control to component driver for optional further init */
2212 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2214 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2221 /* load physical DAI elements */
2222 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2223 struct snd_soc_tplg_hdr *hdr)
2225 struct snd_soc_tplg_dai *dai;
2226 int count = hdr->count;
2229 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2232 /* config the existing BE DAIs */
2233 for (i = 0; i < count; i++) {
2234 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2235 if (dai->size != sizeof(*dai)) {
2236 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2240 soc_tplg_dai_config(tplg, dai);
2241 tplg->pos += (sizeof(*dai) + dai->priv.size);
2244 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2249 * manifest_new_ver - Create a new version of manifest from the old version
2251 * @tplg: topology context
2252 * @src: old version of manifest as a source
2253 * @manifest: latest version of manifest created from the source
2255 * Support from vesion 4. Users need free the returned manifest manually.
2257 static int manifest_new_ver(struct soc_tplg *tplg,
2258 struct snd_soc_tplg_manifest *src,
2259 struct snd_soc_tplg_manifest **manifest)
2261 struct snd_soc_tplg_manifest *dest;
2262 struct snd_soc_tplg_manifest_v4 *src_v4;
2266 if (src->size != sizeof(*src_v4)) {
2267 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2271 src->size = sizeof(*src_v4);
2274 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2276 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2277 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2281 dest->size = sizeof(*dest); /* size of latest abi version */
2282 dest->control_elems = src_v4->control_elems;
2283 dest->widget_elems = src_v4->widget_elems;
2284 dest->graph_elems = src_v4->graph_elems;
2285 dest->pcm_elems = src_v4->pcm_elems;
2286 dest->dai_link_elems = src_v4->dai_link_elems;
2287 dest->priv.size = src_v4->priv.size;
2288 if (dest->priv.size)
2289 memcpy(dest->priv.data, src_v4->priv.data,
2296 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2297 struct snd_soc_tplg_hdr *hdr)
2299 struct snd_soc_tplg_manifest *manifest, *_manifest;
2303 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2306 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2308 /* check ABI version by size, create a new manifest if abi not match */
2309 if (manifest->size == sizeof(*manifest)) {
2311 _manifest = manifest;
2314 err = manifest_new_ver(tplg, manifest, &_manifest);
2319 /* pass control to component driver for optional further init */
2320 if (tplg->comp && tplg->ops && tplg->ops->manifest)
2321 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2323 if (!abi_match) /* free the duplicated one */
2329 /* validate header magic, size and type */
2330 static int soc_valid_header(struct soc_tplg *tplg,
2331 struct snd_soc_tplg_hdr *hdr)
2333 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2336 if (hdr->size != sizeof(*hdr)) {
2338 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2339 hdr->type, soc_tplg_get_hdr_offset(tplg),
2344 /* big endian firmware objects not supported atm */
2345 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2347 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2348 tplg->pass, hdr->magic,
2349 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2353 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2355 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2356 tplg->pass, hdr->magic,
2357 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2361 /* Support ABI from version 4 */
2362 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2363 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
2365 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2366 tplg->pass, hdr->abi,
2367 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2372 if (hdr->payload_size == 0) {
2373 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2374 soc_tplg_get_hdr_offset(tplg));
2378 if (tplg->pass == hdr->type)
2380 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2381 hdr->payload_size, hdr->type, hdr->version,
2382 hdr->vendor_type, tplg->pass);
2387 /* check header type and call appropriate handler */
2388 static int soc_tplg_load_header(struct soc_tplg *tplg,
2389 struct snd_soc_tplg_hdr *hdr)
2391 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2393 /* check for matching ID */
2394 if (hdr->index != tplg->req_index &&
2395 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2398 tplg->index = hdr->index;
2400 switch (hdr->type) {
2401 case SND_SOC_TPLG_TYPE_MIXER:
2402 case SND_SOC_TPLG_TYPE_ENUM:
2403 case SND_SOC_TPLG_TYPE_BYTES:
2404 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2405 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2406 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2407 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2408 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2409 case SND_SOC_TPLG_TYPE_PCM:
2410 return soc_tplg_pcm_elems_load(tplg, hdr);
2411 case SND_SOC_TPLG_TYPE_DAI:
2412 return soc_tplg_dai_elems_load(tplg, hdr);
2413 case SND_SOC_TPLG_TYPE_DAI_LINK:
2414 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2415 /* physical link configurations */
2416 return soc_tplg_link_elems_load(tplg, hdr);
2417 case SND_SOC_TPLG_TYPE_MANIFEST:
2418 return soc_tplg_manifest_load(tplg, hdr);
2420 /* bespoke vendor data object */
2421 return soc_tplg_vendor_load(tplg, hdr);
2427 /* process the topology file headers */
2428 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2430 struct snd_soc_tplg_hdr *hdr;
2433 tplg->pass = SOC_TPLG_PASS_START;
2435 /* process the header types from start to end */
2436 while (tplg->pass <= SOC_TPLG_PASS_END) {
2438 tplg->hdr_pos = tplg->fw->data;
2439 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2441 while (!soc_tplg_is_eof(tplg)) {
2443 /* make sure header is valid before loading */
2444 ret = soc_valid_header(tplg, hdr);
2450 /* load the header object */
2451 ret = soc_tplg_load_header(tplg, hdr);
2455 /* goto next header */
2456 tplg->hdr_pos += hdr->payload_size +
2457 sizeof(struct snd_soc_tplg_hdr);
2458 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2461 /* next data type pass */
2465 /* signal DAPM we are complete */
2466 ret = soc_tplg_dapm_complete(tplg);
2469 "ASoC: failed to initialise DAPM from Firmware\n");
2474 static int soc_tplg_load(struct soc_tplg *tplg)
2478 ret = soc_tplg_process_headers(tplg);
2480 soc_tplg_complete(tplg);
2485 /* load audio component topology from "firmware" file */
2486 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2487 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2489 struct soc_tplg tplg;
2492 /* setup parsing context */
2493 memset(&tplg, 0, sizeof(tplg));
2495 tplg.dev = comp->dev;
2498 tplg.req_index = id;
2499 tplg.io_ops = ops->io_ops;
2500 tplg.io_ops_count = ops->io_ops_count;
2501 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2502 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2504 ret = soc_tplg_load(&tplg);
2505 /* free the created components if fail to load topology */
2507 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2511 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2513 /* remove this dynamic widget */
2514 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2516 /* make sure we are a widget */
2517 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2520 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2522 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2524 /* remove all dynamic widgets from this DAPM context */
2525 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2528 struct snd_soc_dapm_widget *w, *next_w;
2530 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2532 /* make sure we are a widget with correct context */
2533 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2537 if (w->dobj.index != index &&
2538 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2540 /* check and free and dynamic widget kcontrols */
2541 snd_soc_tplg_widget_remove(w);
2542 snd_soc_dapm_free_widget(w);
2544 snd_soc_dapm_reset_cache(dapm);
2546 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2548 /* remove dynamic controls from the component driver */
2549 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2551 struct snd_soc_dobj *dobj, *next_dobj;
2552 int pass = SOC_TPLG_PASS_END;
2554 /* process the header types from end to start */
2555 while (pass >= SOC_TPLG_PASS_START) {
2557 /* remove mixer controls */
2558 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2562 if (dobj->index != index &&
2563 index != SND_SOC_TPLG_INDEX_ALL)
2566 switch (dobj->type) {
2567 case SND_SOC_DOBJ_MIXER:
2568 remove_mixer(comp, dobj, pass);
2570 case SND_SOC_DOBJ_ENUM:
2571 remove_enum(comp, dobj, pass);
2573 case SND_SOC_DOBJ_BYTES:
2574 remove_bytes(comp, dobj, pass);
2576 case SND_SOC_DOBJ_WIDGET:
2577 remove_widget(comp, dobj, pass);
2579 case SND_SOC_DOBJ_PCM:
2580 remove_dai(comp, dobj, pass);
2582 case SND_SOC_DOBJ_DAI_LINK:
2583 remove_link(comp, dobj, pass);
2586 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2594 /* let caller know if FW can be freed when no objects are left */
2595 return !list_empty(&comp->dobj_list);
2597 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);